PR c++/7754
[official-gcc.git] / gcc / cp / decl2.c
blob43349631b4de6c6431903f1372b6f12b04a87980
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 extern cpp_reader *parse_in;
49 /* This structure contains information about the initializations
50 and/or destructions required for a particular priority level. */
51 typedef struct priority_info_s {
52 /* Non-zero if there have been any initializations at this priority
53 throughout the translation unit. */
54 int initializations_p;
55 /* Non-zero if there have been any destructions at this priority
56 throughout the translation unit. */
57 int destructions_p;
58 } *priority_info;
60 static void mark_vtable_entries PARAMS ((tree));
61 static void grok_function_init PARAMS ((tree, tree));
62 static int finish_vtable_vardecl PARAMS ((tree *, void *));
63 static int prune_vtable_vardecl PARAMS ((tree *, void *));
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 int compare_options PARAMS ((const PTR, const PTR));
92 static tree get_guard_bits PARAMS ((tree));
94 /* A list of static class variables. This is needed, because a
95 static class variable can be declared inside the class without
96 an initializer, and then initialized, statically, outside the class. */
97 static varray_type pending_statics;
98 #define pending_statics_used \
99 (pending_statics ? pending_statics->elements_used : 0)
101 /* A list of functions which were declared inline, but which we
102 may need to emit outline anyway. */
103 static varray_type deferred_fns;
104 #define deferred_fns_used \
105 (deferred_fns ? deferred_fns->elements_used : 0)
107 /* Flag used when debugging spew.c */
109 extern int spew_debug;
111 /* Nonzero if we're done parsing and into end-of-file activities. */
113 int at_eof;
115 /* Functions called along with real static constructors and destructors. */
117 tree static_ctors;
118 tree static_dtors;
120 /* The :: namespace. */
122 tree global_namespace;
124 /* C (and C++) language-specific option variables. */
126 /* Nonzero means don't recognize the keyword `asm'. */
128 int flag_no_asm;
130 /* Nonzero means don't recognize any extension keywords. */
132 int flag_no_gnu_keywords;
134 /* Nonzero means do some things the same way PCC does. Only provided so
135 the compiler will link. */
137 int flag_traditional;
139 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
141 int flag_signed_bitfields = 1;
143 /* Nonzero means enable obscure standard features and disable GNU
144 extensions that might cause standard-compliant code to be
145 miscompiled. */
147 int flag_ansi;
149 /* Nonzero means do emit exported implementations of functions even if
150 they can be inlined. */
152 int flag_implement_inlines = 1;
154 /* Nonzero means do emit exported implementations of templates, instead of
155 multiple static copies in each file that needs a definition. */
157 int flag_external_templates;
159 /* Nonzero means that the decision to emit or not emit the implementation of a
160 template depends on where the template is instantiated, rather than where
161 it is defined. */
163 int flag_alt_external_templates;
165 /* Nonzero means that implicit instantiations will be emitted if needed. */
167 int flag_implicit_templates = 1;
169 /* Nonzero means that implicit instantiations of inline templates will be
170 emitted if needed, even if instantiations of non-inline templates
171 aren't. */
173 int flag_implicit_inline_templates = 1;
175 /* Nonzero means warn about things that will change when compiling
176 with an ABI-compliant compiler. */
178 int warn_abi = 0;
180 /* Nonzero means warn about implicit declarations. */
182 int warn_implicit = 1;
184 /* Nonzero means warn about usage of long long when `-pedantic'. */
186 int warn_long_long = 1;
188 /* Nonzero means warn when all ctors or dtors are private, and the class
189 has no friends. */
191 int warn_ctor_dtor_privacy = 1;
193 /* Nonzero means generate separate instantiation control files and juggle
194 them at link time. */
196 int flag_use_repository;
198 /* Nonzero if we want to issue diagnostics that the standard says are not
199 required. */
201 int flag_optional_diags = 1;
203 /* Nonzero means give string constants the type `const char *', as mandated
204 by the standard. */
206 int flag_const_strings = 1;
208 /* Nonzero means warn about deprecated conversion from string constant to
209 `char *'. */
211 int warn_write_strings;
213 /* Nonzero means warn about pointer casts that can drop a type qualifier
214 from the pointer target type. */
216 int warn_cast_qual;
218 /* Nonzero means warn about sizeof(function) or addition/subtraction
219 of function pointers. */
221 int warn_pointer_arith = 1;
223 /* Nonzero means warn for any function def without prototype decl. */
225 int warn_missing_prototypes;
227 /* Nonzero means warn about multiple (redundant) decls for the same single
228 variable or function. */
230 int warn_redundant_decls;
232 /* Warn if initializer is not completely bracketed. */
234 int warn_missing_braces;
236 /* Warn about comparison of signed and unsigned values. */
238 int warn_sign_compare;
240 /* Warn about testing equality of floating point numbers. */
242 int warn_float_equal = 0;
244 /* Warn about functions which might be candidates for format attributes. */
246 int warn_missing_format_attribute;
248 /* Warn about a subscript that has type char. */
250 int warn_char_subscripts;
252 /* Warn if a type conversion is done that might have confusing results. */
254 int warn_conversion;
256 /* Warn if adding () is suggested. */
258 int warn_parentheses;
260 /* Non-zero means warn in function declared in derived class has the
261 same name as a virtual in the base class, but fails to match the
262 type signature of any virtual function in the base class. */
264 int warn_overloaded_virtual;
266 /* Non-zero means warn when declaring a class that has a non virtual
267 destructor, when it really ought to have a virtual one. */
269 int warn_nonvdtor;
271 /* Non-zero means warn when the compiler will reorder code. */
273 int warn_reorder;
275 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
277 int warn_synth;
279 /* Non-zero means warn when we convert a pointer to member function
280 into a pointer to (void or function). */
282 int warn_pmf2ptr = 1;
284 /* Nonzero means warn about violation of some Effective C++ style rules. */
286 int warn_ecpp;
288 /* Nonzero means warn where overload resolution chooses a promotion from
289 unsigned to signed over a conversion to an unsigned of the same size. */
291 int warn_sign_promo;
293 /* Nonzero means warn when an old-style cast is used. */
295 int warn_old_style_cast;
297 /* Warn about #pragma directives that are not recognised. */
299 int warn_unknown_pragmas; /* Tri state variable. */
301 /* Nonzero means warn about use of multicharacter literals. */
303 int warn_multichar = 1;
305 /* Nonzero means warn when non-templatized friend functions are
306 declared within a template */
308 int warn_nontemplate_friend = 1;
310 /* Nonzero means complain about deprecated features. */
312 int warn_deprecated = 1;
314 /* Nonzero means `$' can be in an identifier. */
316 #ifndef DOLLARS_IN_IDENTIFIERS
317 #define DOLLARS_IN_IDENTIFIERS 1
318 #endif
319 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
321 /* Nonzero means allow Microsoft extensions without a pedwarn. */
323 int flag_ms_extensions;
325 /* C++ specific flags. */
327 /* Nonzero means we should attempt to elide constructors when possible. */
329 int flag_elide_constructors = 1;
331 /* Nonzero means that member functions defined in class scope are
332 inline by default. */
334 int flag_default_inline = 1;
336 /* Controls whether compiler generates 'type descriptor' that give
337 run-time type information. */
339 int flag_rtti = 1;
341 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
342 objects. */
344 int flag_huge_objects;
346 /* Nonzero if we want to conserve space in the .o files. We do this
347 by putting uninitialized data and runtime initialized data into
348 .common instead of .data at the expense of not flagging multiple
349 definitions. */
351 int flag_conserve_space;
353 /* Nonzero if we want to obey access control semantics. */
355 int flag_access_control = 1;
357 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
359 int flag_operator_names = 1;
361 /* Nonzero if we want to check the return value of new and avoid calling
362 constructors if it is a null pointer. */
364 int flag_check_new;
366 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
367 initialization variables.
368 0: Old rules, set by -fno-for-scope.
369 2: New ISO rules, set by -ffor-scope.
370 1: Try to implement new ISO rules, but with backup compatibility
371 (and warnings). This is the default, for now. */
373 int flag_new_for_scope = 1;
375 /* Nonzero if we want to emit defined symbols with common-like linkage as
376 weak symbols where possible, in order to conform to C++ semantics.
377 Otherwise, emit them as local symbols. */
379 int flag_weak = 1;
381 /* Nonzero to use __cxa_atexit, rather than atexit, to register
382 destructors for local statics and global objects. */
384 int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
386 /* Maximum template instantiation depth. This limit is rather
387 arbitrary, but it exists to limit the time it takes to notice
388 infinite template instantiations. */
390 int max_tinst_depth = 500;
392 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
394 int flag_vtable_gc;
396 /* Nonzero means make the default pedwarns warnings instead of errors.
397 The value of this flag is ignored if -pedantic is specified. */
399 int flag_permissive;
401 /* Nonzero means to implement standard semantics for exception
402 specifications, calling unexpected if an exception is thrown that
403 doesn't match the specification. Zero means to treat them as
404 assertions and optimize accordingly, but not check them. */
406 int flag_enforce_eh_specs = 1;
408 /* Table of language-dependent -f options.
409 STRING is the option name. VARIABLE is the address of the variable.
410 ON_VALUE is the value to store in VARIABLE
411 if `-fSTRING' is seen as an option.
412 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
414 static const struct { const char *const string; int *const variable; const int on_value;}
415 lang_f_options[] =
417 /* C/C++ options. */
418 {"signed-char", &flag_signed_char, 1},
419 {"unsigned-char", &flag_signed_char, 0},
420 {"signed-bitfields", &flag_signed_bitfields, 1},
421 {"unsigned-bitfields", &flag_signed_bitfields, 0},
422 {"short-enums", &flag_short_enums, 1},
423 {"short-double", &flag_short_double, 1},
424 {"short-wchar", &flag_short_wchar, 1},
425 {"asm", &flag_no_asm, 0},
426 {"builtin", &flag_no_builtin, 0},
428 /* C++-only options. */
429 {"access-control", &flag_access_control, 1},
430 {"check-new", &flag_check_new, 1},
431 {"conserve-space", &flag_conserve_space, 1},
432 {"const-strings", &flag_const_strings, 1},
433 {"default-inline", &flag_default_inline, 1},
434 {"dollars-in-identifiers", &dollars_in_ident, 1},
435 {"elide-constructors", &flag_elide_constructors, 1},
436 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
437 {"external-templates", &flag_external_templates, 1},
438 {"for-scope", &flag_new_for_scope, 2},
439 {"gnu-keywords", &flag_no_gnu_keywords, 0},
440 {"handle-exceptions", &flag_exceptions, 1},
441 {"implement-inlines", &flag_implement_inlines, 1},
442 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
443 {"implicit-templates", &flag_implicit_templates, 1},
444 {"ms-extensions", &flag_ms_extensions, 1},
445 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
446 {"operator-names", &flag_operator_names, 1},
447 {"optional-diags", &flag_optional_diags, 1},
448 {"permissive", &flag_permissive, 1},
449 {"repo", &flag_use_repository, 1},
450 {"rtti", &flag_rtti, 1},
451 {"stats", &flag_detailed_statistics, 1},
452 {"vtable-gc", &flag_vtable_gc, 1},
453 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
454 {"weak", &flag_weak, 1}
457 /* The list of `-f' options that we no longer support. The `-f'
458 prefix is not given in this table. The `-fno-' variants are not
459 listed here. This table must be kept in alphabetical order. */
460 static const char * const unsupported_options[] = {
461 "all-virtual",
462 "cond-mismatch",
463 "enum-int-equiv",
464 "guiding-decls",
465 "honor-std",
466 "huge-objects",
467 "labels-ok",
468 "new-abi",
469 "nonnull-objects",
470 "squangle",
471 "strict-prototype",
472 "this-is-variable",
473 "vtable-thunks",
474 "xref"
477 /* Compare two option strings, pointed two by P1 and P2, for use with
478 bsearch. */
480 static int
481 compare_options (p1, p2)
482 const PTR p1;
483 const PTR p2;
485 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
488 /* Decode the string P as a language-specific option.
489 Return the number of strings consumed for a valid option.
490 Otherwise return 0. Should not complain if it does not
491 recognise the option. */
493 int
494 cxx_decode_option (argc, argv)
495 int argc;
496 char **argv;
498 int strings_processed;
499 const char *p = argv[0];
501 strings_processed = cpp_handle_option (parse_in, argc, argv, 0);
503 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
504 /* ignore */;
505 else if (p[0] == '-' && p[1] == 'f')
507 /* Some kind of -f option.
508 P's value is the option sans `-f'.
509 Search for it in the table of options. */
510 const char *option_value = NULL;
511 const char *positive_option;
512 size_t j;
514 p += 2;
515 /* Try special -f options. */
517 /* See if this is one of the options no longer supported. We
518 used to support these options, so we continue to accept them,
519 with a warning. */
520 if (strncmp (p, "no-", strlen ("no-")) == 0)
521 positive_option = p + strlen ("no-");
522 else
523 positive_option = p;
525 /* If the option is present, issue a warning. Indicate to our
526 caller that the option was processed successfully. */
527 if (bsearch (&positive_option,
528 unsupported_options,
529 (sizeof (unsupported_options)
530 / sizeof (unsupported_options[0])),
531 sizeof (unsupported_options[0]),
532 compare_options))
534 warning ("-f%s is no longer supported", p);
535 return 1;
538 if (!strcmp (p, "handle-exceptions")
539 || !strcmp (p, "no-handle-exceptions"))
540 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
541 else if (! strcmp (p, "alt-external-templates"))
543 flag_external_templates = 1;
544 flag_alt_external_templates = 1;
545 cp_deprecated ("-falt-external-templates");
547 else if (! strcmp (p, "no-alt-external-templates"))
548 flag_alt_external_templates = 0;
549 else if (!strcmp (p, "repo"))
551 flag_use_repository = 1;
552 flag_implicit_templates = 0;
554 else if (!strcmp (p, "external-templates"))
556 flag_external_templates = 1;
557 cp_deprecated ("-fexternal-templates");
559 else if ((option_value
560 = skip_leading_substring (p, "template-depth-")))
561 max_tinst_depth
562 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
563 else if ((option_value
564 = skip_leading_substring (p, "name-mangling-version-")))
566 warning ("-fname-mangling-version is no longer supported");
567 return 1;
569 else if (dump_switch_p (p))
571 else
573 int found = 0;
575 for (j = 0;
576 !found && j < (sizeof (lang_f_options)
577 / sizeof (lang_f_options[0]));
578 j++)
580 if (!strcmp (p, lang_f_options[j].string))
582 *lang_f_options[j].variable = lang_f_options[j].on_value;
583 /* A goto here would be cleaner,
584 but breaks the VAX pcc. */
585 found = 1;
587 else if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
588 && ! strcmp (p+3, lang_f_options[j].string))
590 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
591 found = 1;
595 return found;
598 else if (p[0] == '-' && p[1] == 'W')
600 int setting = 1;
602 /* The -W options control the warning behavior of the compiler. */
603 p += 2;
605 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
606 setting = 0, p += 3;
608 if (!strcmp (p, "abi"))
609 warn_abi = setting;
610 else if (!strcmp (p, "implicit"))
611 warn_implicit = setting;
612 else if (!strcmp (p, "long-long"))
613 warn_long_long = setting;
614 else if (!strcmp (p, "return-type"))
615 warn_return_type = setting;
616 else if (!strcmp (p, "ctor-dtor-privacy"))
617 warn_ctor_dtor_privacy = setting;
618 else if (!strcmp (p, "write-strings"))
619 warn_write_strings = setting;
620 else if (!strcmp (p, "cast-qual"))
621 warn_cast_qual = setting;
622 else if (!strcmp (p, "char-subscripts"))
623 warn_char_subscripts = setting;
624 else if (!strcmp (p, "pointer-arith"))
625 warn_pointer_arith = setting;
626 else if (!strcmp (p, "missing-prototypes"))
627 warn_missing_prototypes = setting;
628 else if (!strcmp (p, "strict-prototypes"))
630 if (setting == 0)
631 warning ("-Wno-strict-prototypes is not supported in C++");
633 else if (!strcmp (p, "redundant-decls"))
634 warn_redundant_decls = setting;
635 else if (!strcmp (p, "missing-braces"))
636 warn_missing_braces = setting;
637 else if (!strcmp (p, "sign-compare"))
638 warn_sign_compare = setting;
639 else if (!strcmp (p, "float-equal"))
640 warn_float_equal = setting;
641 else if (!strcmp (p, "format"))
642 set_Wformat (setting);
643 else if (!strcmp (p, "format=2"))
644 set_Wformat (2);
645 else if (!strcmp (p, "format-y2k"))
646 warn_format_y2k = setting;
647 else if (!strcmp (p, "format-extra-args"))
648 warn_format_extra_args = setting;
649 else if (!strcmp (p, "format-nonliteral"))
650 warn_format_nonliteral = setting;
651 else if (!strcmp (p, "format-security"))
652 warn_format_security = setting;
653 else if (!strcmp (p, "missing-format-attribute"))
654 warn_missing_format_attribute = setting;
655 else if (!strcmp (p, "conversion"))
656 warn_conversion = setting;
657 else if (!strcmp (p, "parentheses"))
658 warn_parentheses = setting;
659 else if (!strcmp (p, "non-virtual-dtor"))
660 warn_nonvdtor = setting;
661 else if (!strcmp (p, "reorder"))
662 warn_reorder = setting;
663 else if (!strcmp (p, "synth"))
664 warn_synth = setting;
665 else if (!strcmp (p, "pmf-conversions"))
666 warn_pmf2ptr = setting;
667 else if (!strcmp (p, "effc++"))
668 warn_ecpp = setting;
669 else if (!strcmp (p, "sign-promo"))
670 warn_sign_promo = setting;
671 else if (!strcmp (p, "old-style-cast"))
672 warn_old_style_cast = setting;
673 else if (!strcmp (p, "overloaded-virtual"))
674 warn_overloaded_virtual = setting;
675 else if (!strcmp (p, "multichar"))
676 warn_multichar = setting;
677 else if (!strcmp (p, "unknown-pragmas"))
678 /* Set to greater than 1, so that even unknown pragmas in
679 system headers will be warned about. */
680 warn_unknown_pragmas = setting * 2;
681 else if (!strcmp (p, "non-template-friend"))
682 warn_nontemplate_friend = setting;
683 else if (!strcmp (p, "deprecated"))
684 warn_deprecated = setting;
685 else if (!strcmp (p, "comment"))
686 ; /* cpp handles this one. */
687 else if (!strcmp (p, "comments"))
688 ; /* cpp handles this one. */
689 else if (!strcmp (p, "trigraphs"))
690 ; /* cpp handles this one. */
691 else if (!strcmp (p, "import"))
692 ; /* cpp handles this one. */
693 else if (!strcmp (p, "all"))
695 warn_return_type = setting;
696 set_Wunused (setting);
697 warn_implicit = setting;
698 warn_switch = setting;
699 set_Wformat (setting);
700 warn_parentheses = setting;
701 warn_missing_braces = setting;
702 warn_sign_compare = setting;
703 warn_multichar = setting;
704 /* We save the value of warn_uninitialized, since if they put
705 -Wuninitialized on the command line, we need to generate a
706 warning about not using it without also specifying -O. */
707 if (warn_uninitialized != 1)
708 warn_uninitialized = (setting ? 2 : 0);
709 /* Only warn about unknown pragmas that are not in system
710 headers. */
711 warn_unknown_pragmas = 1;
713 /* C++-specific warnings. */
714 warn_ctor_dtor_privacy = setting;
715 warn_nonvdtor = setting;
716 warn_reorder = setting;
717 warn_nontemplate_friend = setting;
719 else return strings_processed;
721 else if (!strcmp (p, "-ansi"))
722 flag_no_nonansi_builtin = 1, flag_ansi = 1,
723 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
724 #ifdef SPEW_DEBUG
725 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
726 it's probably safe to assume no sane person would ever want to use this
727 under normal circumstances. */
728 else if (!strcmp (p, "-spew-debug"))
729 spew_debug = 1;
730 #endif
731 else
732 return strings_processed;
734 return 1;
737 /* Incorporate `const' and `volatile' qualifiers for member functions.
738 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
739 QUALS is a list of qualifiers. Returns any explicit
740 top-level qualifiers of the method's this pointer, anything other than
741 TYPE_UNQUALIFIED will be an extension. */
744 grok_method_quals (ctype, function, quals)
745 tree ctype, function, quals;
747 tree fntype = TREE_TYPE (function);
748 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
749 int type_quals = TYPE_UNQUALIFIED;
750 int dup_quals = TYPE_UNQUALIFIED;
751 int this_quals = TYPE_UNQUALIFIED;
755 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
757 if ((type_quals | this_quals) & tq)
758 dup_quals |= tq;
759 else if (tq & TYPE_QUAL_RESTRICT)
760 this_quals |= tq;
761 else
762 type_quals |= tq;
763 quals = TREE_CHAIN (quals);
765 while (quals);
767 if (dup_quals != TYPE_UNQUALIFIED)
768 error ("duplicate type qualifiers in %s declaration",
769 TREE_CODE (function) == FUNCTION_DECL
770 ? "member function" : "type");
772 ctype = cp_build_qualified_type (ctype, type_quals);
773 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
774 (TREE_CODE (fntype) == METHOD_TYPE
775 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
776 : TYPE_ARG_TYPES (fntype)));
777 if (raises)
778 fntype = build_exception_variant (fntype, raises);
780 TREE_TYPE (function) = fntype;
781 return this_quals;
784 /* Warn when -fexternal-templates is used and #pragma
785 interface/implementation is not used all the times it should be,
786 inform the user. */
788 void
789 warn_if_unknown_interface (decl)
790 tree decl;
792 static int already_warned = 0;
793 if (already_warned++)
794 return;
796 if (flag_alt_external_templates)
798 tree til = tinst_for_decl ();
799 int sl = lineno;
800 const char *sf = input_filename;
802 if (til)
804 lineno = TINST_LINE (til);
805 input_filename = TINST_FILE (til);
807 warning ("template `%#D' instantiated in file without #pragma interface",
808 decl);
809 lineno = sl;
810 input_filename = sf;
812 else
813 cp_warning_at ("template `%#D' defined in file without #pragma interface",
814 decl);
817 /* A subroutine of the parser, to handle a component list. */
819 void
820 grok_x_components (specs)
821 tree specs;
823 tree t;
825 specs = strip_attrs (specs);
827 check_tag_decl (specs);
828 t = groktypename (build_tree_list (specs, NULL_TREE));
830 /* The only case where we need to do anything additional here is an
831 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
832 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
833 return;
835 fixup_anonymous_aggr (t);
836 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
839 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
840 indicated NAME. */
842 tree
843 build_artificial_parm (name, type)
844 tree name;
845 tree type;
847 tree parm;
849 parm = build_decl (PARM_DECL, name, type);
850 DECL_ARTIFICIAL (parm) = 1;
851 /* All our artificial parms are implicitly `const'; they cannot be
852 assigned to. */
853 TREE_READONLY (parm) = 1;
854 DECL_ARG_TYPE (parm) = type;
855 return parm;
858 /* Constructors for types with virtual baseclasses need an "in-charge" flag
859 saying whether this constructor is responsible for initialization of
860 virtual baseclasses or not. All destructors also need this "in-charge"
861 flag, which additionally determines whether or not the destructor should
862 free the memory for the object.
864 This function adds the "in-charge" flag to member function FN if
865 appropriate. It is called from grokclassfn and tsubst.
866 FN must be either a constructor or destructor.
868 The in-charge flag follows the 'this' parameter, and is followed by the
869 VTT parm (if any), then the user-written parms. */
871 void
872 maybe_retrofit_in_chrg (fn)
873 tree fn;
875 tree basetype, arg_types, parms, parm, fntype;
877 /* If we've already add the in-charge parameter don't do it again. */
878 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
879 return;
881 /* When processing templates we can't know, in general, whether or
882 not we're going to have virtual baseclasses. */
883 if (uses_template_parms (fn))
884 return;
886 /* We don't need an in-charge parameter for constructors that don't
887 have virtual bases. */
888 if (DECL_CONSTRUCTOR_P (fn)
889 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
890 return;
892 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
893 basetype = TREE_TYPE (TREE_VALUE (arg_types));
894 arg_types = TREE_CHAIN (arg_types);
896 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
898 /* If this is a subobject constructor or destructor, our caller will
899 pass us a pointer to our VTT. */
900 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
902 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
904 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
905 TREE_CHAIN (parm) = parms;
906 parms = parm;
908 /* ...and then to TYPE_ARG_TYPES. */
909 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
911 DECL_HAS_VTT_PARM_P (fn) = 1;
914 /* Then add the in-charge parm (before the VTT parm). */
915 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
916 TREE_CHAIN (parm) = parms;
917 parms = parm;
918 arg_types = hash_tree_chain (integer_type_node, arg_types);
920 /* Insert our new parameter(s) into the list. */
921 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
923 /* And rebuild the function type. */
924 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
925 arg_types);
926 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
927 fntype = build_exception_variant (fntype,
928 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
929 TREE_TYPE (fn) = fntype;
931 /* Now we've got the in-charge parameter. */
932 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
935 /* Classes overload their constituent function names automatically.
936 When a function name is declared in a record structure,
937 its name is changed to it overloaded name. Since names for
938 constructors and destructors can conflict, we place a leading
939 '$' for destructors.
941 CNAME is the name of the class we are grokking for.
943 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
945 FLAGS contains bits saying what's special about today's
946 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
948 If FUNCTION is a destructor, then we must add the `auto-delete' field
949 as a second parameter. There is some hair associated with the fact
950 that we must "declare" this variable in the manner consistent with the
951 way the rest of the arguments were declared.
953 QUALS are the qualifiers for the this pointer. */
955 void
956 grokclassfn (ctype, function, flags, quals)
957 tree ctype, function;
958 enum overload_flags flags;
959 tree quals;
961 tree fn_name = DECL_NAME (function);
962 int this_quals = TYPE_UNQUALIFIED;
964 /* Even within an `extern "C"' block, members get C++ linkage. See
965 [dcl.link] for details. */
966 SET_DECL_LANGUAGE (function, lang_cplusplus);
968 if (fn_name == NULL_TREE)
970 error ("name missing for member function");
971 fn_name = get_identifier ("<anonymous>");
972 DECL_NAME (function) = fn_name;
975 if (quals)
976 this_quals = grok_method_quals (ctype, function, quals);
978 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
980 /* Must add the class instance variable up front. */
981 /* Right now we just make this a pointer. But later
982 we may wish to make it special. */
983 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
984 tree qual_type;
985 tree parm;
987 /* The `this' parameter is implicitly `const'; it cannot be
988 assigned to. */
989 this_quals |= TYPE_QUAL_CONST;
990 qual_type = cp_build_qualified_type (type, this_quals);
991 parm = build_artificial_parm (this_identifier, qual_type);
992 c_apply_type_quals_to_decl (this_quals, parm);
993 TREE_CHAIN (parm) = last_function_parms;
994 last_function_parms = parm;
997 DECL_ARGUMENTS (function) = last_function_parms;
998 DECL_CONTEXT (function) = ctype;
1000 if (flags == DTOR_FLAG)
1001 DECL_DESTRUCTOR_P (function) = 1;
1003 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1004 maybe_retrofit_in_chrg (function);
1006 if (flags == DTOR_FLAG)
1008 DECL_DESTRUCTOR_P (function) = 1;
1009 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1013 /* Create an ARRAY_REF, checking for the user doing things backwards
1014 along the way. */
1016 tree
1017 grok_array_decl (array_expr, index_exp)
1018 tree array_expr, index_exp;
1020 tree type = TREE_TYPE (array_expr);
1021 tree p1, p2, i1, i2;
1023 if (type == error_mark_node || index_exp == error_mark_node)
1024 return error_mark_node;
1025 if (processing_template_decl)
1026 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1027 array_expr, index_exp);
1029 if (type == NULL_TREE)
1031 /* Something has gone very wrong. Assume we are mistakenly reducing
1032 an expression instead of a declaration. */
1033 error ("parser may be lost: is there a '{' missing somewhere?");
1034 return NULL_TREE;
1037 if (TREE_CODE (type) == OFFSET_TYPE
1038 || TREE_CODE (type) == REFERENCE_TYPE)
1039 type = TREE_TYPE (type);
1041 /* If they have an `operator[]', use that. */
1042 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1043 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1044 array_expr, index_exp, NULL_TREE);
1046 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1047 is a little-known fact that, if `a' is an array and `i' is an
1048 int, you can write `i[a]', which means the same thing as `a[i]'. */
1050 if (TREE_CODE (type) == ARRAY_TYPE)
1051 p1 = array_expr;
1052 else
1053 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1055 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1056 p2 = index_exp;
1057 else
1058 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1060 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1061 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1063 if ((p1 && i2) && (i1 && p2))
1064 error ("ambiguous conversion for array subscript");
1066 if (p1 && i2)
1067 array_expr = p1, index_exp = i2;
1068 else if (i1 && p2)
1069 array_expr = p2, index_exp = i1;
1070 else
1072 error ("invalid types `%T[%T]' for array subscript",
1073 type, TREE_TYPE (index_exp));
1074 return error_mark_node;
1077 if (array_expr == error_mark_node || index_exp == error_mark_node)
1078 error ("ambiguous conversion for array subscript");
1080 return build_array_ref (array_expr, index_exp);
1083 /* Given the cast expression EXP, checking out its validity. Either return
1084 an error_mark_node if there was an unavoidable error, return a cast to
1085 void for trying to delete a pointer w/ the value 0, or return the
1086 call to delete. If DOING_VEC is 1, we handle things differently
1087 for doing an array delete. If DOING_VEC is 2, they gave us the
1088 array size as an argument to delete.
1089 Implements ARM $5.3.4. This is called from the parser. */
1091 tree
1092 delete_sanity (exp, size, doing_vec, use_global_delete)
1093 tree exp, size;
1094 int doing_vec, use_global_delete;
1096 tree t, type;
1097 /* For a regular vector delete (aka, no size argument) we will pass
1098 this down as a NULL_TREE into build_vec_delete. */
1099 tree maxindex = NULL_TREE;
1101 if (exp == error_mark_node)
1102 return exp;
1104 if (processing_template_decl)
1106 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1107 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1108 DELETE_EXPR_USE_VEC (t) = doing_vec;
1109 return t;
1112 if (TREE_CODE (exp) == OFFSET_REF)
1113 exp = resolve_offset_ref (exp);
1114 exp = convert_from_reference (exp);
1115 t = stabilize_reference (exp);
1116 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1118 if (t == NULL_TREE || t == error_mark_node)
1120 error ("type `%#T' argument given to `delete', expected pointer",
1121 TREE_TYPE (exp));
1122 return error_mark_node;
1125 if (doing_vec == 2)
1127 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1128 pedwarn ("anachronistic use of array size in vector delete");
1131 type = TREE_TYPE (t);
1133 /* As of Valley Forge, you can delete a pointer to const. */
1135 /* You can't delete functions. */
1136 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1138 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1139 return error_mark_node;
1142 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1143 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1145 warning ("deleting `%T' is undefined", type);
1146 doing_vec = 0;
1149 /* An array can't have been allocated by new, so complain. */
1150 if (TREE_CODE (t) == ADDR_EXPR
1151 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1152 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1153 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1155 /* Deleting a pointer with the value zero is valid and has no effect. */
1156 if (integer_zerop (t))
1157 return build1 (NOP_EXPR, void_type_node, t);
1159 if (doing_vec)
1160 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1161 use_global_delete);
1162 else
1163 return build_delete (type, t, sfk_deleting_destructor,
1164 LOOKUP_NORMAL, use_global_delete);
1167 /* Report an error if the indicated template declaration is not the
1168 sort of thing that should be a member template. */
1170 void
1171 check_member_template (tmpl)
1172 tree tmpl;
1174 tree decl;
1176 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1177 decl = DECL_TEMPLATE_RESULT (tmpl);
1179 if (TREE_CODE (decl) == FUNCTION_DECL
1180 || (TREE_CODE (decl) == TYPE_DECL
1181 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1183 if (current_function_decl)
1184 /* 14.5.2.2 [temp.mem]
1186 A local class shall not have member templates. */
1187 error ("invalid declaration of member template `%#D' in local class",
1188 decl);
1190 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1192 /* 14.5.2.3 [temp.mem]
1194 A member function template shall not be virtual. */
1195 error
1196 ("invalid use of `virtual' in template declaration of `%#D'",
1197 decl);
1198 DECL_VIRTUAL_P (decl) = 0;
1201 /* The debug-information generating code doesn't know what to do
1202 with member templates. */
1203 DECL_IGNORED_P (tmpl) = 1;
1205 else
1206 error ("template declaration of `%#D'", decl);
1209 /* Return true iff TYPE is a valid Java parameter or return type. */
1211 static int
1212 acceptable_java_type (type)
1213 tree type;
1215 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1216 return 1;
1217 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1219 type = TREE_TYPE (type);
1220 if (TREE_CODE (type) == RECORD_TYPE)
1222 tree args; int i;
1223 if (! TYPE_FOR_JAVA (type))
1224 return 0;
1225 if (! CLASSTYPE_TEMPLATE_INFO (type))
1226 return 1;
1227 args = CLASSTYPE_TI_ARGS (type);
1228 i = TREE_VEC_LENGTH (args);
1229 while (--i >= 0)
1231 type = TREE_VEC_ELT (args, i);
1232 if (TREE_CODE (type) == POINTER_TYPE)
1233 type = TREE_TYPE (type);
1234 if (! TYPE_FOR_JAVA (type))
1235 return 0;
1237 return 1;
1240 return 0;
1243 /* For a METHOD in a Java class CTYPE, return 1 if
1244 the parameter and return types are valid Java types.
1245 Otherwise, print appropriate error messages, and return 0. */
1248 check_java_method (method)
1249 tree method;
1251 int jerr = 0;
1252 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1253 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1254 if (! acceptable_java_type (ret_type))
1256 error ("Java method '%D' has non-Java return type `%T'",
1257 method, ret_type);
1258 jerr++;
1260 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1262 tree type = TREE_VALUE (arg_types);
1263 if (! acceptable_java_type (type))
1265 error ("Java method '%D' has non-Java parameter type `%T'",
1266 method, type);
1267 jerr++;
1270 return jerr ? 0 : 1;
1273 /* Sanity check: report error if this function FUNCTION is not
1274 really a member of the class (CTYPE) it is supposed to belong to.
1275 CNAME is the same here as it is for grokclassfn above. */
1277 tree
1278 check_classfn (ctype, function)
1279 tree ctype, function;
1281 tree fn_name = DECL_NAME (function);
1282 tree fndecl, fndecls;
1283 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1284 tree *methods = 0;
1285 tree *end = 0;
1287 if (DECL_USE_TEMPLATE (function)
1288 && !(TREE_CODE (function) == TEMPLATE_DECL
1289 && DECL_TEMPLATE_SPECIALIZATION (function))
1290 && is_member_template (DECL_TI_TEMPLATE (function)))
1291 /* Since this is a specialization of a member template,
1292 we're not going to find the declaration in the class.
1293 For example, in:
1295 struct S { template <typename T> void f(T); };
1296 template <> void S::f(int);
1298 we're not going to find `S::f(int)', but there's no
1299 reason we should, either. We let our callers know we didn't
1300 find the method, but we don't complain. */
1301 return NULL_TREE;
1303 if (method_vec != 0)
1305 methods = &TREE_VEC_ELT (method_vec, 0);
1306 end = TREE_VEC_END (method_vec);
1308 /* First suss out ctors and dtors. */
1309 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1310 && DECL_CONSTRUCTOR_P (function))
1311 goto got_it;
1312 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1313 && DECL_DESTRUCTOR_P (function))
1314 goto got_it;
1316 while (++methods != end && *methods)
1318 fndecl = *methods;
1319 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1321 got_it:
1322 for (fndecls = *methods; fndecls != NULL_TREE;
1323 fndecls = OVL_NEXT (fndecls))
1325 fndecl = OVL_CURRENT (fndecls);
1327 /* We cannot simply call decls_match because this
1328 doesn't work for static member functions that are
1329 pretending to be methods, and because the name
1330 may have been changed by asm("new_name"). */
1331 if (DECL_NAME (function) == DECL_NAME (fndecl))
1333 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1334 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1336 /* Get rid of the this parameter on functions that become
1337 static. */
1338 if (DECL_STATIC_FUNCTION_P (fndecl)
1339 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1340 p1 = TREE_CHAIN (p1);
1342 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1343 TREE_TYPE (TREE_TYPE (fndecl)))
1344 && compparms (p1, p2)
1345 && (DECL_TEMPLATE_SPECIALIZATION (function)
1346 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1347 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1348 || (DECL_TI_TEMPLATE (function)
1349 == DECL_TI_TEMPLATE (fndecl))))
1350 return fndecl;
1353 break; /* loser */
1358 if (methods != end && *methods)
1360 tree fndecl = *methods;
1361 error ("prototype for `%#D' does not match any in class `%T'",
1362 function, ctype);
1363 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1364 OVL_CURRENT (fndecl));
1365 while (fndecl = OVL_NEXT (fndecl), fndecl)
1366 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1368 else
1370 methods = 0;
1371 if (!COMPLETE_TYPE_P (ctype))
1372 incomplete_type_error (function, ctype);
1373 else
1374 error ("no `%#D' member function declared in class `%T'",
1375 function, ctype);
1378 /* If we did not find the method in the class, add it to avoid
1379 spurious errors (unless the CTYPE is not yet defined, in which
1380 case we'll only confuse ourselves when the function is declared
1381 properly within the class. */
1382 if (COMPLETE_TYPE_P (ctype))
1383 add_method (ctype, function, /*error_p=*/1);
1384 return NULL_TREE;
1387 /* We have just processed the DECL, which is a static data member.
1388 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1389 present, is the assembly-language name for the data member.
1390 FLAGS is as for cp_finish_decl. */
1392 void
1393 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1394 tree decl;
1395 tree init;
1396 tree asmspec_tree;
1397 int flags;
1399 my_friendly_assert (TREE_PUBLIC (decl), 0);
1401 DECL_CONTEXT (decl) = current_class_type;
1403 /* We cannot call pushdecl here, because that would fill in the
1404 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1405 the right thing, namely, to put this decl out straight away. */
1406 /* current_class_type can be NULL_TREE in case of error. */
1407 if (!asmspec_tree && current_class_type)
1408 DECL_INITIAL (decl) = error_mark_node;
1410 if (! processing_template_decl)
1412 if (!pending_statics)
1413 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1414 VARRAY_PUSH_TREE (pending_statics, decl);
1417 if (LOCAL_CLASS_P (current_class_type))
1418 pedwarn ("local class `%#T' shall not have static data member `%#D'",
1419 current_class_type, decl);
1421 /* Static consts need not be initialized in the class definition. */
1422 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1424 static int explained = 0;
1426 error ("initializer invalid for static member with constructor");
1427 if (!explained)
1429 error ("(an out of class initialization is required)");
1430 explained = 1;
1432 init = NULL_TREE;
1434 /* Force the compiler to know when an uninitialized static const
1435 member is being used. */
1436 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1437 TREE_USED (decl) = 1;
1438 DECL_INITIAL (decl) = init;
1439 DECL_IN_AGGR_P (decl) = 1;
1441 cp_finish_decl (decl, init, asmspec_tree, flags);
1444 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1445 of a structure component, returning a _DECL node.
1446 QUALS is a list of type qualifiers for this decl (such as for declaring
1447 const member functions).
1449 This is done during the parsing of the struct declaration.
1450 The _DECL nodes are chained together and the lot of them
1451 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1453 If class A defines that certain functions in class B are friends, then
1454 the way I have set things up, it is B who is interested in permission
1455 granted by A. However, it is in A's context that these declarations
1456 are parsed. By returning a void_type_node, class A does not attempt
1457 to incorporate the declarations of the friends within its structure.
1459 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1460 CHANGES TO CODE IN `start_method'. */
1462 tree
1463 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1464 tree declarator, declspecs, init, asmspec_tree, attrlist;
1466 tree value;
1467 const char *asmspec = 0;
1468 int flags = LOOKUP_ONLYCONVERTING;
1470 /* Convert () initializers to = initializers. */
1471 if (init == NULL_TREE && declarator != NULL_TREE
1472 && TREE_CODE (declarator) == CALL_EXPR
1473 && TREE_OPERAND (declarator, 0)
1474 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1475 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1476 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1478 /* It's invalid to try to initialize a data member using a
1479 functional notation, e.g.:
1481 struct S {
1482 static int i (3);
1485 Explain that to the user. */
1486 static int explained;
1488 error ("invalid data member initialization");
1489 if (!explained)
1491 error ("(use `=' to initialize static data members)");
1492 explained = 1;
1495 declarator = TREE_OPERAND (declarator, 0);
1496 flags = 0;
1499 if (declspecs == NULL_TREE
1500 && TREE_CODE (declarator) == SCOPE_REF
1501 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1503 /* Access declaration */
1504 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1506 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1507 pop_nested_class ();
1508 return do_class_using_decl (declarator);
1511 if (init
1512 && TREE_CODE (init) == TREE_LIST
1513 && TREE_VALUE (init) == error_mark_node
1514 && TREE_CHAIN (init) == NULL_TREE)
1515 init = NULL_TREE;
1517 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
1518 if (! value || value == error_mark_node)
1519 /* friend or constructor went bad. */
1520 return value;
1521 if (TREE_TYPE (value) == error_mark_node)
1522 return error_mark_node;
1524 /* Pass friendly classes back. */
1525 if (TREE_CODE (value) == VOID_TYPE)
1526 return void_type_node;
1528 if (DECL_NAME (value) != NULL_TREE
1529 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1530 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1531 error ("member `%D' conflicts with virtual function table field name",
1532 value);
1534 /* Stash away type declarations. */
1535 if (TREE_CODE (value) == TYPE_DECL)
1537 DECL_NONLOCAL (value) = 1;
1538 DECL_CONTEXT (value) = current_class_type;
1540 if (CLASS_TYPE_P (TREE_TYPE (value)))
1541 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1543 if (processing_template_decl)
1544 value = push_template_decl (value);
1546 return value;
1549 if (DECL_IN_AGGR_P (value))
1551 error ("`%D' is already defined in `%T'", value,
1552 DECL_CONTEXT (value));
1553 return void_type_node;
1556 if (asmspec_tree)
1557 asmspec = TREE_STRING_POINTER (asmspec_tree);
1559 if (init)
1561 if (TREE_CODE (value) == FUNCTION_DECL)
1563 grok_function_init (value, init);
1564 init = NULL_TREE;
1566 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1567 /* Already complained in grokdeclarator. */
1568 init = NULL_TREE;
1569 else
1571 /* We allow initializers to become parameters to base
1572 initializers. */
1573 if (TREE_CODE (init) == TREE_LIST)
1575 if (TREE_CHAIN (init) == NULL_TREE)
1576 init = TREE_VALUE (init);
1577 else
1578 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1581 if (TREE_CODE (init) == CONST_DECL)
1582 init = DECL_INITIAL (init);
1583 else if (TREE_READONLY_DECL_P (init))
1584 init = decl_constant_value (init);
1585 else if (TREE_CODE (init) == CONSTRUCTOR)
1586 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1587 if (init == error_mark_node)
1588 /* We must make this look different than `error_mark_node'
1589 because `decl_const_value' would mis-interpret it
1590 as only meaning that this VAR_DECL is defined. */
1591 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1592 else if (processing_template_decl)
1594 else if (! TREE_CONSTANT (init))
1596 /* We can allow references to things that are effectively
1597 static, since references are initialized with the address. */
1598 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1599 || (TREE_STATIC (init) == 0
1600 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1602 error ("field initializer is not constant");
1603 init = error_mark_node;
1609 if (processing_template_decl && ! current_function_decl
1610 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1611 value = push_template_decl (value);
1613 if (attrlist)
1614 cplus_decl_attributes (&value, attrlist, 0);
1616 if (TREE_CODE (value) == VAR_DECL)
1618 finish_static_data_member_decl (value, init, asmspec_tree,
1619 flags);
1620 return value;
1622 if (TREE_CODE (value) == FIELD_DECL)
1624 if (asmspec)
1625 error ("`asm' specifiers are not permitted on non-static data members");
1626 if (DECL_INITIAL (value) == error_mark_node)
1627 init = error_mark_node;
1628 cp_finish_decl (value, init, NULL_TREE, flags);
1629 DECL_INITIAL (value) = init;
1630 DECL_IN_AGGR_P (value) = 1;
1631 return value;
1633 if (TREE_CODE (value) == FUNCTION_DECL)
1635 if (asmspec)
1637 /* This must override the asm specifier which was placed
1638 by grokclassfn. Lay this out fresh. */
1639 SET_DECL_RTL (value, NULL_RTX);
1640 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1642 if (!DECL_FRIEND_P (value))
1643 grok_special_member_properties (value);
1645 cp_finish_decl (value, init, asmspec_tree, flags);
1647 /* Pass friends back this way. */
1648 if (DECL_FRIEND_P (value))
1649 return void_type_node;
1651 DECL_IN_AGGR_P (value) = 1;
1652 return value;
1654 abort ();
1655 /* NOTREACHED */
1656 return NULL_TREE;
1659 /* Like `grokfield', but for bitfields.
1660 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1662 tree
1663 grokbitfield (declarator, declspecs, width)
1664 tree declarator, declspecs, width;
1666 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1667 0, NULL);
1669 if (! value) return NULL_TREE; /* friends went bad. */
1671 /* Pass friendly classes back. */
1672 if (TREE_CODE (value) == VOID_TYPE)
1673 return void_type_node;
1675 if (TREE_CODE (value) == TYPE_DECL)
1677 error ("cannot declare `%D' to be a bit-field type", value);
1678 return NULL_TREE;
1681 /* Usually, finish_struct_1 catches bitfields with invalid types.
1682 But, in the case of bitfields with function type, we confuse
1683 ourselves into thinking they are member functions, so we must
1684 check here. */
1685 if (TREE_CODE (value) == FUNCTION_DECL)
1687 error ("cannot declare bit-field `%D' with function type",
1688 DECL_NAME (value));
1689 return NULL_TREE;
1692 if (DECL_IN_AGGR_P (value))
1694 error ("`%D' is already defined in the class %T", value,
1695 DECL_CONTEXT (value));
1696 return void_type_node;
1699 if (TREE_STATIC (value))
1701 error ("static member `%D' cannot be a bit-field", value);
1702 return NULL_TREE;
1704 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1706 if (width != error_mark_node)
1708 constant_expression_warning (width);
1709 DECL_INITIAL (value) = width;
1710 SET_DECL_C_BIT_FIELD (value);
1713 DECL_IN_AGGR_P (value) = 1;
1714 return value;
1717 tree
1718 grokoptypename (declspecs, declarator)
1719 tree declspecs, declarator;
1721 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1722 return mangle_conv_op_name_for_type (t);
1725 /* When a function is declared with an initializer,
1726 do the right thing. Currently, there are two possibilities:
1728 class B
1730 public:
1731 // initialization possibility #1.
1732 virtual void f () = 0;
1733 int g ();
1736 class D1 : B
1738 public:
1739 int d1;
1740 // error, no f ();
1743 class D2 : B
1745 public:
1746 int d2;
1747 void f ();
1750 class D3 : B
1752 public:
1753 int d3;
1754 // initialization possibility #2
1755 void f () = B::f;
1760 static void
1761 grok_function_init (decl, init)
1762 tree decl;
1763 tree init;
1765 /* An initializer for a function tells how this function should
1766 be inherited. */
1767 tree type = TREE_TYPE (decl);
1769 if (TREE_CODE (type) == FUNCTION_TYPE)
1770 error ("initializer specified for non-member function `%D'", decl);
1771 else if (integer_zerop (init))
1772 DECL_PURE_VIRTUAL_P (decl) = 1;
1773 else
1774 error ("invalid initializer for virtual method `%D'", decl);
1777 void
1778 cplus_decl_attributes (decl, attributes, flags)
1779 tree *decl, attributes;
1780 int flags;
1782 if (*decl == NULL_TREE || *decl == void_type_node)
1783 return;
1785 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1786 decl = &DECL_TEMPLATE_RESULT (*decl);
1788 decl_attributes (decl, attributes, flags);
1790 if (TREE_CODE (*decl) == TYPE_DECL)
1791 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1794 /* CONSTRUCTOR_NAME:
1795 Return the name for the constructor (or destructor) for the
1796 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1797 IDENTIFIER_NODE. When given a template, this routine doesn't
1798 lose the specialization. */
1800 tree
1801 constructor_name_full (thing)
1802 tree thing;
1804 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1805 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1806 || TREE_CODE (thing) == TYPENAME_TYPE)
1807 thing = TYPE_NAME (thing);
1808 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1810 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1811 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1812 else
1813 thing = TYPE_NAME (thing);
1815 if (TREE_CODE (thing) == TYPE_DECL
1816 || (TREE_CODE (thing) == TEMPLATE_DECL
1817 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1818 thing = DECL_NAME (thing);
1819 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1820 return thing;
1823 /* CONSTRUCTOR_NAME:
1824 Return the name for the constructor (or destructor) for the
1825 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1826 IDENTIFIER_NODE. When given a template, return the plain
1827 unspecialized name. */
1829 tree
1830 constructor_name (thing)
1831 tree thing;
1833 tree t;
1834 thing = constructor_name_full (thing);
1835 t = IDENTIFIER_TEMPLATE (thing);
1836 if (!t)
1837 return thing;
1838 return t;
1841 /* Defer the compilation of the FN until the end of compilation. */
1843 void
1844 defer_fn (fn)
1845 tree fn;
1847 if (DECL_DEFERRED_FN (fn))
1848 return;
1849 DECL_DEFERRED_FN (fn) = 1;
1850 if (!deferred_fns)
1851 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1853 VARRAY_PUSH_TREE (deferred_fns, fn);
1856 /* Hunts through the global anonymous union ANON_DECL, building
1857 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1858 returns a VAR_DECL whose size is the same as the size of the
1859 ANON_DECL, if one is available.
1861 FIXME: we should really handle anonymous unions by binding the names
1862 of the members to COMPONENT_REFs rather than this kludge. */
1864 static tree
1865 build_anon_union_vars (anon_decl, elems, static_p, external_p)
1866 tree anon_decl;
1867 tree* elems;
1868 int static_p;
1869 int external_p;
1871 tree type = TREE_TYPE (anon_decl);
1872 tree main_decl = NULL_TREE;
1873 tree field;
1875 /* Rather than write the code to handle the non-union case,
1876 just give an error. */
1877 if (TREE_CODE (type) != UNION_TYPE)
1878 error ("anonymous struct not inside named type");
1880 for (field = TYPE_FIELDS (type);
1881 field != NULL_TREE;
1882 field = TREE_CHAIN (field))
1884 tree decl;
1886 if (DECL_ARTIFICIAL (field))
1887 continue;
1888 if (TREE_CODE (field) != FIELD_DECL)
1890 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
1891 field);
1892 continue;
1895 if (TREE_PRIVATE (field))
1896 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1897 else if (TREE_PROTECTED (field))
1898 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1900 if (DECL_NAME (field) == NULL_TREE
1901 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1903 decl = build_anon_union_vars (field, elems, static_p, external_p);
1904 if (!decl)
1905 continue;
1907 else if (DECL_NAME (field) == NULL_TREE)
1908 continue;
1909 else
1911 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1912 /* tell `pushdecl' that this is not tentative. */
1913 DECL_INITIAL (decl) = error_mark_node;
1914 TREE_PUBLIC (decl) = 0;
1915 TREE_STATIC (decl) = static_p;
1916 DECL_EXTERNAL (decl) = external_p;
1917 decl = pushdecl (decl);
1918 DECL_INITIAL (decl) = NULL_TREE;
1921 /* Only write out one anon union element--choose the largest
1922 one. We used to try to find one the same size as the union,
1923 but that fails if the ABI forces us to align the union more
1924 strictly. */
1925 if (main_decl == NULL_TREE
1926 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
1928 if (main_decl)
1929 TREE_ASM_WRITTEN (main_decl) = 1;
1930 main_decl = decl;
1932 else
1933 /* ??? This causes there to be no debug info written out
1934 about this decl. */
1935 TREE_ASM_WRITTEN (decl) = 1;
1937 if (DECL_NAME (field) == NULL_TREE
1938 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1939 /* The remainder of the processing was already done in the
1940 recursive call. */
1941 continue;
1943 /* If there's a cleanup to do, it belongs in the
1944 TREE_PURPOSE of the following TREE_LIST. */
1945 *elems = tree_cons (NULL_TREE, decl, *elems);
1946 TREE_TYPE (*elems) = type;
1949 return main_decl;
1952 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1953 anonymous union, then all members must be laid out together. PUBLIC_P
1954 is nonzero if this union is not declared static. */
1956 void
1957 finish_anon_union (anon_union_decl)
1958 tree anon_union_decl;
1960 tree type = TREE_TYPE (anon_union_decl);
1961 tree main_decl;
1962 int public_p = TREE_PUBLIC (anon_union_decl);
1963 int static_p = TREE_STATIC (anon_union_decl);
1964 int external_p = DECL_EXTERNAL (anon_union_decl);
1966 /* The VAR_DECL's context is the same as the TYPE's context. */
1967 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1969 if (TYPE_FIELDS (type) == NULL_TREE)
1970 return;
1972 if (public_p)
1974 error ("namespace-scope anonymous aggregates must be static");
1975 return;
1978 if (!processing_template_decl)
1980 main_decl
1981 = build_anon_union_vars (anon_union_decl,
1982 &DECL_ANON_UNION_ELEMS (anon_union_decl),
1983 static_p, external_p);
1985 if (main_decl == NULL_TREE)
1987 warning ("anonymous aggregate with no members");
1988 return;
1991 if (static_p)
1993 make_decl_rtl (main_decl, 0);
1994 COPY_DECL_RTL (main_decl, anon_union_decl);
1995 expand_anon_union_decl (anon_union_decl,
1996 NULL_TREE,
1997 DECL_ANON_UNION_ELEMS (anon_union_decl));
1998 return;
2002 add_decl_stmt (anon_union_decl);
2005 /* Finish processing a builtin type TYPE. It's name is NAME,
2006 its fields are in the array FIELDS. LEN is the number of elements
2007 in FIELDS minus one, or put another way, it is the maximum subscript
2008 used in FIELDS.
2010 It is given the same alignment as ALIGN_TYPE. */
2012 void
2013 finish_builtin_type (type, name, fields, len, align_type)
2014 tree type;
2015 const char *name;
2016 tree fields[];
2017 int len;
2018 tree align_type;
2020 register int i;
2022 TYPE_FIELDS (type) = fields[0];
2023 for (i = 0; i < len; i++)
2025 layout_type (TREE_TYPE (fields[i]));
2026 DECL_FIELD_CONTEXT (fields[i]) = type;
2027 TREE_CHAIN (fields[i]) = fields[i+1];
2029 DECL_FIELD_CONTEXT (fields[i]) = type;
2030 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2031 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2032 layout_type (type);
2033 #if 0 /* not yet, should get fixed properly later */
2034 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2035 #else
2036 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2037 #endif
2038 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2039 layout_decl (TYPE_NAME (type), 0);
2042 /* Auxiliary functions to make type signatures for
2043 `operator new' and `operator delete' correspond to
2044 what compiler will be expecting. */
2046 tree
2047 coerce_new_type (type)
2048 tree type;
2050 int e = 0;
2051 tree args = TYPE_ARG_TYPES (type);
2053 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2055 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2056 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
2058 if (!args || args == void_list_node
2059 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2061 e = 2;
2062 if (args && args != void_list_node)
2063 args = TREE_CHAIN (args);
2064 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2066 switch (e)
2068 case 2:
2069 args = tree_cons (NULL_TREE, c_size_type_node, args);
2070 /* FALLTHROUGH */
2071 case 1:
2072 type = build_exception_variant
2073 (build_function_type (ptr_type_node, args),
2074 TYPE_RAISES_EXCEPTIONS (type));
2075 /* FALLTHROUGH */
2076 default:;
2078 return type;
2081 tree
2082 coerce_delete_type (type)
2083 tree type;
2085 int e = 0;
2086 tree args = TYPE_ARG_TYPES (type);
2088 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2090 if (!same_type_p (TREE_TYPE (type), void_type_node))
2091 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
2093 if (!args || args == void_list_node
2094 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2096 e = 2;
2097 if (args && args != void_list_node)
2098 args = TREE_CHAIN (args);
2099 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2101 switch (e)
2103 case 2:
2104 args = tree_cons (NULL_TREE, ptr_type_node, args);
2105 /* FALLTHROUGH */
2106 case 1:
2107 type = build_exception_variant
2108 (build_function_type (void_type_node, args),
2109 TYPE_RAISES_EXCEPTIONS (type));
2110 /* FALLTHROUGH */
2111 default:;
2114 return type;
2117 static void
2118 mark_vtable_entries (decl)
2119 tree decl;
2121 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2123 for (; entries; entries = TREE_CHAIN (entries))
2125 tree fnaddr = TREE_VALUE (entries);
2126 tree fn;
2128 if (TREE_CODE (fnaddr) != ADDR_EXPR
2129 && TREE_CODE (fnaddr) != FDESC_EXPR)
2130 /* This entry is an offset: a virtual base class offset, a
2131 virtual call offset, an RTTI offset, etc. */
2132 continue;
2134 fn = TREE_OPERAND (fnaddr, 0);
2135 TREE_ADDRESSABLE (fn) = 1;
2136 /* When we don't have vcall offsets, we output thunks whenever
2137 we output the vtables that contain them. With vcall offsets,
2138 we know all the thunks we'll need when we emit a virtual
2139 function, so we emit the thunks there instead. */
2140 if (DECL_THUNK_P (fn))
2141 use_thunk (fn, /*emit_p=*/0);
2142 mark_used (fn);
2146 /* Set DECL up to have the closest approximation of "initialized common"
2147 linkage available. */
2149 void
2150 comdat_linkage (decl)
2151 tree decl;
2153 if (flag_weak)
2154 make_decl_one_only (decl);
2155 else if (TREE_CODE (decl) == FUNCTION_DECL
2156 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2157 /* We can just emit function and compiler-generated variables
2158 statically; having multiple copies is (for the most part) only
2159 a waste of space.
2161 There are two correctness issues, however: the address of a
2162 template instantiation with external linkage should be the
2163 same, independent of what translation unit asks for the
2164 address, and this will not hold when we emit multiple copies of
2165 the function. However, there's little else we can do.
2167 Also, by default, the typeinfo implementation assumes that
2168 there will be only one copy of the string used as the name for
2169 each type. Therefore, if weak symbols are unavailable, the
2170 run-time library should perform a more conservative check; it
2171 should perform a string comparison, rather than an address
2172 comparison. */
2173 TREE_PUBLIC (decl) = 0;
2174 else
2176 /* Static data member template instantiations, however, cannot
2177 have multiple copies. */
2178 if (DECL_INITIAL (decl) == 0
2179 || DECL_INITIAL (decl) == error_mark_node)
2180 DECL_COMMON (decl) = 1;
2181 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2183 DECL_COMMON (decl) = 1;
2184 DECL_INITIAL (decl) = error_mark_node;
2186 else
2188 /* We can't do anything useful; leave vars for explicit
2189 instantiation. */
2190 DECL_EXTERNAL (decl) = 1;
2191 DECL_NOT_REALLY_EXTERN (decl) = 0;
2195 if (DECL_LANG_SPECIFIC (decl))
2196 DECL_COMDAT (decl) = 1;
2199 /* For win32 we also want to put explicit instantiations in
2200 linkonce sections, so that they will be merged with implicit
2201 instantiations; otherwise we get duplicate symbol errors. */
2203 void
2204 maybe_make_one_only (decl)
2205 tree decl;
2207 /* We used to say that this was not necessary on targets that support weak
2208 symbols, because the implicit instantiations will defer to the explicit
2209 one. However, that's not actually the case in SVR4; a strong definition
2210 after a weak one is an error. Also, not making explicit
2211 instantiations one_only means that we can end up with two copies of
2212 some template instantiations. */
2213 if (! flag_weak)
2214 return;
2216 /* We can't set DECL_COMDAT on functions, or finish_file will think
2217 we can get away with not emitting them if they aren't used. We need
2218 to for variables so that cp_finish_decl will update their linkage,
2219 because their DECL_INITIAL may not have been set properly yet. */
2221 make_decl_one_only (decl);
2223 if (TREE_CODE (decl) == VAR_DECL)
2225 DECL_COMDAT (decl) = 1;
2226 /* Mark it needed so we don't forget to emit it. */
2227 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
2231 /* Returns the virtual function with which the vtable for TYPE is
2232 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2234 static tree
2235 key_method (type)
2236 tree type;
2238 tree method;
2240 if (TYPE_FOR_JAVA (type)
2241 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2242 || CLASSTYPE_INTERFACE_KNOWN (type))
2243 return NULL_TREE;
2245 for (method = TYPE_METHODS (type); method != NULL_TREE;
2246 method = TREE_CHAIN (method))
2247 if (DECL_VINDEX (method) != NULL_TREE
2248 && ! DECL_DECLARED_INLINE_P (method)
2249 && (! DECL_PURE_VIRTUAL_P (method)
2250 #if 0
2251 /* This would be nice, but we didn't think of it in time. */
2252 || DECL_DESTRUCTOR_P (method)
2253 #endif
2255 return method;
2257 return NULL_TREE;
2260 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2261 based on TYPE and other static flags.
2263 Note that anything public is tagged TREE_PUBLIC, whether
2264 it's public in this file or in another one. */
2266 void
2267 import_export_vtable (decl, type, final)
2268 tree decl, type;
2269 int final;
2271 if (DECL_INTERFACE_KNOWN (decl))
2272 return;
2274 if (TYPE_FOR_JAVA (type))
2276 TREE_PUBLIC (decl) = 1;
2277 DECL_EXTERNAL (decl) = 1;
2278 DECL_INTERFACE_KNOWN (decl) = 1;
2280 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2282 TREE_PUBLIC (decl) = 1;
2283 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2284 DECL_INTERFACE_KNOWN (decl) = 1;
2286 else
2288 /* We can only wait to decide if we have real non-inline virtual
2289 functions in our class, or if we come from a template. */
2291 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2292 || key_method (type));
2294 if (final || ! found)
2296 comdat_linkage (decl);
2297 DECL_EXTERNAL (decl) = 0;
2299 else
2301 TREE_PUBLIC (decl) = 1;
2302 DECL_EXTERNAL (decl) = 1;
2307 /* Determine whether or not we want to specifically import or export CTYPE,
2308 using various heuristics. */
2310 static void
2311 import_export_class (ctype)
2312 tree ctype;
2314 /* -1 for imported, 1 for exported. */
2315 int import_export = 0;
2317 /* It only makes sense to call this function at EOF. The reason is
2318 that this function looks at whether or not the first non-inline
2319 non-abstract virtual member function has been defined in this
2320 translation unit. But, we can't possibly know that until we've
2321 seen the entire translation unit. */
2322 my_friendly_assert (at_eof, 20000226);
2324 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2325 return;
2327 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2328 we will have CLASSTYPE_INTERFACE_ONLY set but not
2329 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2330 heuristic because someone will supply a #pragma implementation
2331 elsewhere, and deducing it here would produce a conflict. */
2332 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2333 return;
2335 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2336 import_export = -1;
2337 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2338 import_export = 1;
2340 /* If we got -fno-implicit-templates, we import template classes that
2341 weren't explicitly instantiated. */
2342 if (import_export == 0
2343 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2344 && ! flag_implicit_templates)
2345 import_export = -1;
2347 /* Base our import/export status on that of the first non-inline,
2348 non-pure virtual function, if any. */
2349 if (import_export == 0
2350 && TYPE_POLYMORPHIC_P (ctype))
2352 tree method = key_method (ctype);
2353 if (method)
2354 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2357 #ifdef MULTIPLE_SYMBOL_SPACES
2358 if (import_export == -1)
2359 import_export = 0;
2360 #endif
2362 if (import_export)
2364 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2365 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2369 /* We need to describe to the assembler the relationship between
2370 a vtable and the vtable of the parent class. */
2372 static void
2373 output_vtable_inherit (vars)
2374 tree vars;
2376 tree parent;
2377 rtx child_rtx, parent_rtx;
2379 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2381 parent = binfo_for_vtable (vars);
2383 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2384 parent_rtx = const0_rtx;
2385 else if (parent)
2387 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2388 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2390 else
2391 abort ();
2393 assemble_vtable_inherit (child_rtx, parent_rtx);
2396 static int
2397 finish_vtable_vardecl (t, data)
2398 tree *t;
2399 void *data ATTRIBUTE_UNUSED;
2401 tree vars = *t;
2402 tree ctype = DECL_CONTEXT (vars);
2403 import_export_class (ctype);
2404 import_export_vtable (vars, ctype, 1);
2406 if (! DECL_EXTERNAL (vars)
2407 && DECL_NEEDED_P (vars)
2408 && ! TREE_ASM_WRITTEN (vars))
2410 if (TREE_TYPE (vars) == void_type_node)
2411 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2412 return 0;
2414 /* Write it out. */
2415 mark_vtable_entries (vars);
2416 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2417 store_init_value (vars, DECL_INITIAL (vars));
2419 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2421 /* Mark the VAR_DECL node representing the vtable itself as a
2422 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2423 It is rather important that such things be ignored because
2424 any effort to actually generate DWARF for them will run
2425 into trouble when/if we encounter code like:
2427 #pragma interface
2428 struct S { virtual void member (); };
2430 because the artificial declaration of the vtable itself (as
2431 manufactured by the g++ front end) will say that the vtable
2432 is a static member of `S' but only *after* the debug output
2433 for the definition of `S' has already been output. This causes
2434 grief because the DWARF entry for the definition of the vtable
2435 will try to refer back to an earlier *declaration* of the
2436 vtable as a static member of `S' and there won't be one.
2437 We might be able to arrange to have the "vtable static member"
2438 attached to the member list for `S' before the debug info for
2439 `S' get written (which would solve the problem) but that would
2440 require more intrusive changes to the g++ front end. */
2442 DECL_IGNORED_P (vars) = 1;
2445 /* Always make vtables weak. */
2446 if (flag_weak)
2447 comdat_linkage (vars);
2449 rest_of_decl_compilation (vars, NULL, 1, 1);
2451 if (flag_vtable_gc)
2452 output_vtable_inherit (vars);
2454 /* Because we're only doing syntax-checking, we'll never end up
2455 actually marking the variable as written. */
2456 if (flag_syntax_only)
2457 TREE_ASM_WRITTEN (vars) = 1;
2459 /* Since we're writing out the vtable here, also write the debug
2460 info. */
2461 note_debug_info_needed (ctype);
2463 return 1;
2466 /* If the references to this class' vtables were optimized away, still
2467 emit the appropriate debugging information. See dfs_debug_mark. */
2468 if (DECL_COMDAT (vars)
2469 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2470 note_debug_info_needed (ctype);
2472 return 0;
2475 static int
2476 prune_vtable_vardecl (t, data)
2477 tree *t;
2478 void *data ATTRIBUTE_UNUSED;
2480 *t = TREE_CHAIN (*t);
2481 return 1;
2484 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2485 inline function or template instantiation at end-of-file. */
2487 void
2488 import_export_decl (decl)
2489 tree decl;
2491 if (DECL_INTERFACE_KNOWN (decl))
2492 return;
2494 if (DECL_TEMPLATE_INSTANTIATION (decl)
2495 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2497 DECL_NOT_REALLY_EXTERN (decl) = 1;
2498 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2499 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2500 && (flag_implicit_templates
2501 || (flag_implicit_inline_templates
2502 && DECL_DECLARED_INLINE_P (decl))))
2504 if (!TREE_PUBLIC (decl))
2505 /* Templates are allowed to have internal linkage. See
2506 [basic.link]. */
2508 else
2509 comdat_linkage (decl);
2511 else
2513 DECL_EXTERNAL (decl) = 1;
2514 DECL_NOT_REALLY_EXTERN (decl) = 0;
2517 else if (DECL_FUNCTION_MEMBER_P (decl))
2519 if (!DECL_DECLARED_INLINE_P (decl))
2521 tree ctype = DECL_CONTEXT (decl);
2522 import_export_class (ctype);
2523 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2525 DECL_NOT_REALLY_EXTERN (decl)
2526 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2527 || (DECL_DECLARED_INLINE_P (decl)
2528 && ! flag_implement_inlines
2529 && !DECL_VINDEX (decl)));
2531 if (!DECL_NOT_REALLY_EXTERN (decl))
2532 DECL_EXTERNAL (decl) = 1;
2534 /* Always make artificials weak. */
2535 if (DECL_ARTIFICIAL (decl) && flag_weak)
2536 comdat_linkage (decl);
2537 else
2538 maybe_make_one_only (decl);
2541 else
2542 comdat_linkage (decl);
2544 else
2545 comdat_linkage (decl);
2547 DECL_INTERFACE_KNOWN (decl) = 1;
2550 /* Here, we only decide whether or not the tinfo node should be
2551 emitted with the vtable. IS_IN_LIBRARY is non-zero iff the
2552 typeinfo for TYPE should be in the runtime library. */
2554 void
2555 import_export_tinfo (decl, type, is_in_library)
2556 tree decl;
2557 tree type;
2558 int is_in_library;
2560 if (DECL_INTERFACE_KNOWN (decl))
2561 return;
2563 if (IS_AGGR_TYPE (type))
2564 import_export_class (type);
2566 if (IS_AGGR_TYPE (type) && CLASSTYPE_INTERFACE_KNOWN (type)
2567 && TYPE_POLYMORPHIC_P (type)
2568 /* If -fno-rtti, we're not necessarily emitting this stuff with
2569 the class, so go ahead and emit it now. This can happen when
2570 a class is used in exception handling. */
2571 && flag_rtti)
2573 DECL_NOT_REALLY_EXTERN (decl) = !CLASSTYPE_INTERFACE_ONLY (type);
2574 DECL_COMDAT (decl) = 0;
2576 else
2578 DECL_NOT_REALLY_EXTERN (decl) = 1;
2579 DECL_COMDAT (decl) = 1;
2582 /* Now override some cases. */
2583 if (flag_weak)
2584 DECL_COMDAT (decl) = 1;
2585 else if (is_in_library)
2586 DECL_COMDAT (decl) = 0;
2588 DECL_INTERFACE_KNOWN (decl) = 1;
2591 tree
2592 build_cleanup (decl)
2593 tree decl;
2595 tree temp;
2596 tree type = TREE_TYPE (decl);
2598 if (TREE_CODE (type) == ARRAY_TYPE)
2599 temp = decl;
2600 else
2602 mark_addressable (decl);
2603 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2605 temp = build_delete (TREE_TYPE (temp), temp,
2606 sfk_complete_destructor,
2607 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2608 return temp;
2611 /* Returns the initialization guard variable for the variable DECL,
2612 which has static storage duration. */
2614 tree
2615 get_guard (decl)
2616 tree decl;
2618 tree sname;
2619 tree guard;
2621 sname = mangle_guard_variable (decl);
2622 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2623 if (! guard)
2625 tree guard_type;
2627 /* We use a type that is big enough to contain a mutex as well
2628 as an integer counter. */
2629 guard_type = long_long_integer_type_node;
2630 guard = build_decl (VAR_DECL, sname, guard_type);
2632 /* The guard should have the same linkage as what it guards. */
2633 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2634 TREE_STATIC (guard) = TREE_STATIC (decl);
2635 DECL_COMMON (guard) = DECL_COMMON (decl);
2636 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2637 if (TREE_PUBLIC (decl))
2638 DECL_WEAK (guard) = DECL_WEAK (decl);
2640 DECL_ARTIFICIAL (guard) = 1;
2641 TREE_USED (guard) = 1;
2642 pushdecl_top_level (guard);
2643 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2645 return guard;
2648 /* Return those bits of the GUARD variable that should be set when the
2649 guarded entity is actually initialized. */
2651 static tree
2652 get_guard_bits (guard)
2653 tree guard;
2655 /* We only set the first byte of the guard, in order to leave room
2656 for a mutex in the high-order bits. */
2657 guard = build1 (ADDR_EXPR,
2658 build_pointer_type (TREE_TYPE (guard)),
2659 guard);
2660 guard = build1 (NOP_EXPR,
2661 build_pointer_type (char_type_node),
2662 guard);
2663 guard = build1 (INDIRECT_REF, char_type_node, guard);
2665 return guard;
2668 /* Return an expression which determines whether or not the GUARD
2669 variable has already been initialized. */
2671 tree
2672 get_guard_cond (guard)
2673 tree guard;
2675 tree guard_value;
2677 /* Check to see if the GUARD is zero. */
2678 guard = get_guard_bits (guard);
2679 guard_value = integer_zero_node;
2680 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2681 guard_value = convert (TREE_TYPE (guard), guard_value);
2682 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2685 /* Return an expression which sets the GUARD variable, indicating that
2686 the variable being guarded has been initialized. */
2688 tree
2689 set_guard (guard)
2690 tree guard;
2692 tree guard_init;
2694 /* Set the GUARD to one. */
2695 guard = get_guard_bits (guard);
2696 guard_init = integer_one_node;
2697 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2698 guard_init = convert (TREE_TYPE (guard), guard_init);
2699 return build_modify_expr (guard, NOP_EXPR, guard_init);
2702 /* Start the process of running a particular set of global constructors
2703 or destructors. Subroutine of do_[cd]tors. */
2705 static tree
2706 start_objects (method_type, initp)
2707 int method_type, initp;
2709 tree fnname;
2710 tree body;
2711 char type[10];
2713 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2715 if (initp != DEFAULT_INIT_PRIORITY)
2717 char joiner;
2719 #ifdef JOINER
2720 joiner = JOINER;
2721 #else
2722 joiner = '_';
2723 #endif
2725 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2727 else
2728 sprintf (type, "%c", method_type);
2730 fnname = get_file_function_name_long (type);
2732 start_function (void_list_node,
2733 make_call_declarator (fnname, void_list_node, NULL_TREE,
2734 NULL_TREE),
2735 NULL_TREE, SF_DEFAULT);
2737 /* It can be a static function as long as collect2 does not have
2738 to scan the object file to find its ctor/dtor routine. */
2739 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2741 /* Mark this declaration as used to avoid spurious warnings. */
2742 TREE_USED (current_function_decl) = 1;
2744 /* Mark this function as a global constructor or destructor. */
2745 if (method_type == 'I')
2746 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2747 else
2748 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2749 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2751 body = begin_compound_stmt (/*has_no_scope=*/0);
2753 /* We cannot allow these functions to be elided, even if they do not
2754 have external linkage. And, there's no point in deferring
2755 copmilation of thes functions; they're all going to have to be
2756 out anyhow. */
2757 current_function_cannot_inline
2758 = "static constructors and destructors cannot be inlined";
2760 return body;
2763 /* Finish the process of running a particular set of global constructors
2764 or destructors. Subroutine of do_[cd]tors. */
2766 static void
2767 finish_objects (method_type, initp, body)
2768 int method_type, initp;
2769 tree body;
2771 tree fn;
2773 /* Finish up. */
2774 finish_compound_stmt (/*has_no_scope=*/0, body);
2775 fn = finish_function (0);
2776 expand_body (fn);
2778 /* When only doing semantic analysis, and no RTL generation, we
2779 can't call functions that directly emit assembly code; there is
2780 no assembly file in which to put the code. */
2781 if (flag_syntax_only)
2782 return;
2784 if (targetm.have_ctors_dtors)
2786 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2787 if (method_type == 'I')
2788 (* targetm.asm_out.constructor) (fnsym, initp);
2789 else
2790 (* targetm.asm_out.destructor) (fnsym, initp);
2794 /* The names of the parameters to the function created to handle
2795 initializations and destructions for objects with static storage
2796 duration. */
2797 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2798 #define PRIORITY_IDENTIFIER "__priority"
2800 /* The name of the function we create to handle initializations and
2801 destructions for objects with static storage duration. */
2802 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2804 /* The declaration for the __INITIALIZE_P argument. */
2805 static tree initialize_p_decl;
2807 /* The declaration for the __PRIORITY argument. */
2808 static tree priority_decl;
2810 /* The declaration for the static storage duration function. */
2811 static tree ssdf_decl;
2813 /* All the static storage duration functions created in this
2814 translation unit. */
2815 static varray_type ssdf_decls;
2817 /* A map from priority levels to information about that priority
2818 level. There may be many such levels, so efficient lookup is
2819 important. */
2820 static splay_tree priority_info_map;
2822 /* Begins the generation of the function that will handle all
2823 initialization and destruction of objects with static storage
2824 duration. The function generated takes two parameters of type
2825 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2826 non-zero, it performs initializations. Otherwise, it performs
2827 destructions. It only performs those initializations or
2828 destructions with the indicated __PRIORITY. The generated function
2829 returns no value.
2831 It is assumed that this function will only be called once per
2832 translation unit. */
2834 static tree
2835 start_static_storage_duration_function ()
2837 static unsigned ssdf_number;
2839 tree parm_types;
2840 tree type;
2841 tree body;
2842 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2844 /* Create the identifier for this function. It will be of the form
2845 SSDF_IDENTIFIER_<number>. */
2846 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2847 if (ssdf_number == 0)
2849 /* Overflow occurred. That means there are at least 4 billion
2850 initialization functions. */
2851 sorry ("too many initialization functions required");
2852 abort ();
2855 /* Create the parameters. */
2856 parm_types = void_list_node;
2857 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2858 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2859 type = build_function_type (void_type_node, parm_types);
2861 /* Create the FUNCTION_DECL itself. */
2862 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2863 get_identifier (id),
2864 type);
2865 TREE_PUBLIC (ssdf_decl) = 0;
2866 DECL_ARTIFICIAL (ssdf_decl) = 1;
2868 /* Put this function in the list of functions to be called from the
2869 static constructors and destructors. */
2870 if (!ssdf_decls)
2872 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2874 /* Take this opportunity to initialize the map from priority
2875 numbers to information about that priority level. */
2876 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2877 /*delete_key_fn=*/0,
2878 /*delete_value_fn=*/
2879 (splay_tree_delete_value_fn) &free);
2881 /* We always need to generate functions for the
2882 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2883 priorities later, we'll be sure to find the
2884 DEFAULT_INIT_PRIORITY. */
2885 get_priority_info (DEFAULT_INIT_PRIORITY);
2888 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2890 /* Create the argument list. */
2891 initialize_p_decl = build_decl (PARM_DECL,
2892 get_identifier (INITIALIZE_P_IDENTIFIER),
2893 integer_type_node);
2894 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2895 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
2896 TREE_USED (initialize_p_decl) = 1;
2897 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
2898 integer_type_node);
2899 DECL_CONTEXT (priority_decl) = ssdf_decl;
2900 DECL_ARG_TYPE (priority_decl) = integer_type_node;
2901 TREE_USED (priority_decl) = 1;
2903 TREE_CHAIN (initialize_p_decl) = priority_decl;
2904 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2906 /* Put the function in the global scope. */
2907 pushdecl (ssdf_decl);
2909 /* Start the function itself. This is equivalent to declarating the
2910 function as:
2912 static void __ssdf (int __initialize_p, init __priority_p);
2914 It is static because we only need to call this function from the
2915 various constructor and destructor functions for this module. */
2916 start_function (/*specs=*/NULL_TREE,
2917 ssdf_decl,
2918 /*attrs=*/NULL_TREE,
2919 SF_PRE_PARSED);
2921 /* Set up the scope of the outermost block in the function. */
2922 body = begin_compound_stmt (/*has_no_scope=*/0);
2924 /* This function must not be deferred because we are depending on
2925 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2926 current_function_cannot_inline
2927 = "static storage duration functions cannot be inlined";
2929 return body;
2932 /* Finish the generation of the function which performs initialization
2933 and destruction of objects with static storage duration. After
2934 this point, no more such objects can be created. */
2936 static void
2937 finish_static_storage_duration_function (body)
2938 tree body;
2940 /* Close out the function. */
2941 finish_compound_stmt (/*has_no_scope=*/0, body);
2942 expand_body (finish_function (0));
2945 /* Return the information about the indicated PRIORITY level. If no
2946 code to handle this level has yet been generated, generate the
2947 appropriate prologue. */
2949 static priority_info
2950 get_priority_info (priority)
2951 int priority;
2953 priority_info pi;
2954 splay_tree_node n;
2956 n = splay_tree_lookup (priority_info_map,
2957 (splay_tree_key) priority);
2958 if (!n)
2960 /* Create a new priority information structure, and insert it
2961 into the map. */
2962 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2963 pi->initializations_p = 0;
2964 pi->destructions_p = 0;
2965 splay_tree_insert (priority_info_map,
2966 (splay_tree_key) priority,
2967 (splay_tree_value) pi);
2969 else
2970 pi = (priority_info) n->value;
2972 return pi;
2975 /* Set up to handle the initialization or destruction of DECL. If
2976 INITP is non-zero, we are initializing the variable. Otherwise, we
2977 are destroying it. */
2979 static tree
2980 start_static_initialization_or_destruction (decl, initp)
2981 tree decl;
2982 int initp;
2984 tree guard_if_stmt = NULL_TREE;
2985 int priority;
2986 tree cond;
2987 tree guard;
2988 tree init_cond;
2989 priority_info pi;
2991 /* Figure out the priority for this declaration. */
2992 priority = DECL_INIT_PRIORITY (decl);
2993 if (!priority)
2994 priority = DEFAULT_INIT_PRIORITY;
2996 /* Remember that we had an initialization or finalization at this
2997 priority. */
2998 pi = get_priority_info (priority);
2999 if (initp)
3000 pi->initializations_p = 1;
3001 else
3002 pi->destructions_p = 1;
3004 /* Trick the compiler into thinking we are at the file and line
3005 where DECL was declared so that error-messages make sense, and so
3006 that the debugger will show somewhat sensible file and line
3007 information. */
3008 input_filename = DECL_SOURCE_FILE (decl);
3009 lineno = DECL_SOURCE_LINE (decl);
3011 /* Because of:
3013 [class.access.spec]
3015 Access control for implicit calls to the constructors,
3016 the conversion functions, or the destructor called to
3017 create and destroy a static data member is performed as
3018 if these calls appeared in the scope of the member's
3019 class.
3021 we pretend we are in a static member function of the class of
3022 which the DECL is a member. */
3023 if (member_p (decl))
3025 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3026 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3029 /* Conditionalize this initialization on being in the right priority
3030 and being initializing/finalizing appropriately. */
3031 guard_if_stmt = begin_if_stmt ();
3032 cond = cp_build_binary_op (EQ_EXPR,
3033 priority_decl,
3034 build_int_2 (priority, 0));
3035 init_cond = initp ? integer_one_node : integer_zero_node;
3036 init_cond = cp_build_binary_op (EQ_EXPR,
3037 initialize_p_decl,
3038 init_cond);
3039 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3041 /* Assume we don't need a guard. */
3042 guard = NULL_TREE;
3043 /* We need a guard if this is an object with external linkage that
3044 might be initialized in more than one place. (For example, a
3045 static data member of a template, when the data member requires
3046 construction.) */
3047 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3048 || DECL_ONE_ONLY (decl)
3049 || DECL_WEAK (decl)))
3051 tree guard_cond;
3053 guard = get_guard (decl);
3055 /* When using __cxa_atexit, we just check the GUARD as we would
3056 for a local static. */
3057 if (flag_use_cxa_atexit)
3059 /* When using __cxa_atexit, we never try to destroy
3060 anything from a static destructor. */
3061 my_friendly_assert (initp, 20000629);
3062 guard_cond = get_guard_cond (guard);
3064 /* If we don't have __cxa_atexit, then we will be running
3065 destructors from .fini sections, or their equivalents. So,
3066 we need to know how many times we've tried to initialize this
3067 object. We do initializations only if the GUARD is zero,
3068 i.e., if we are the first to initialize the variable. We do
3069 destructions only if the GUARD is one, i.e., if we are the
3070 last to destroy the variable. */
3071 else if (initp)
3072 guard_cond
3073 = cp_build_binary_op (EQ_EXPR,
3074 build_unary_op (PREINCREMENT_EXPR,
3075 guard,
3076 /*noconvert=*/1),
3077 integer_one_node);
3078 else
3079 guard_cond
3080 = cp_build_binary_op (EQ_EXPR,
3081 build_unary_op (PREDECREMENT_EXPR,
3082 guard,
3083 /*noconvert=*/1),
3084 integer_zero_node);
3086 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3089 finish_if_stmt_cond (cond, guard_if_stmt);
3091 /* If we're using __cxa_atexit, we have not already set the GUARD,
3092 so we must do so now. */
3093 if (guard && initp && flag_use_cxa_atexit)
3094 finish_expr_stmt (set_guard (guard));
3096 return guard_if_stmt;
3099 /* We've just finished generating code to do an initialization or
3100 finalization. GUARD_IF_STMT is the if-statement we used to guard
3101 the initialization. */
3103 static void
3104 finish_static_initialization_or_destruction (guard_if_stmt)
3105 tree guard_if_stmt;
3107 finish_then_clause (guard_if_stmt);
3108 finish_if_stmt ();
3110 /* Now that we're done with DECL we don't need to pretend to be a
3111 member of its class any longer. */
3112 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3113 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3116 /* Generate code to do the static initialization of DECL. The
3117 initialization is INIT. If DECL may be initialized more than once
3118 in different object files, GUARD is the guard variable to
3119 check. PRIORITY is the priority for the initialization. */
3121 static void
3122 do_static_initialization (decl, init)
3123 tree decl;
3124 tree init;
3126 tree expr;
3127 tree guard_if_stmt;
3129 /* Set up for the initialization. */
3130 guard_if_stmt
3131 = start_static_initialization_or_destruction (decl,
3132 /*initp=*/1);
3134 /* Do the initialization itself. */
3135 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3136 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3137 expr = build_aggr_init (decl, init, 0);
3138 else
3140 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3141 TREE_SIDE_EFFECTS (expr) = 1;
3143 finish_expr_stmt (expr);
3145 /* If we're using __cxa_atexit, register a a function that calls the
3146 destructor for the object. */
3147 if (flag_use_cxa_atexit)
3148 register_dtor_fn (decl);
3150 /* Finsh up. */
3151 finish_static_initialization_or_destruction (guard_if_stmt);
3154 /* Generate code to do the static destruction of DECL. If DECL may be
3155 initialized more than once in different object files, GUARD is the
3156 guard variable to check. PRIORITY is the priority for the
3157 destruction. */
3159 static void
3160 do_static_destruction (decl)
3161 tree decl;
3163 tree guard_if_stmt;
3165 /* If we're using __cxa_atexit, then destructors are registered
3166 immediately after objects are initialized. */
3167 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3169 /* If we don't need a destructor, there's nothing to do. */
3170 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3171 return;
3173 /* Actually do the destruction. */
3174 guard_if_stmt = start_static_initialization_or_destruction (decl,
3175 /*initp=*/0);
3176 finish_expr_stmt (build_cleanup (decl));
3177 finish_static_initialization_or_destruction (guard_if_stmt);
3180 /* VARS is a list of variables with static storage duration which may
3181 need initialization and/or finalization. Remove those variables
3182 that don't really need to be initialized or finalized, and return
3183 the resulting list. The order in which the variables appear in
3184 VARS is in reverse order of the order in which they should actually
3185 be initialized. The list we return is in the unreversed order;
3186 i.e., the first variable should be initialized first. */
3188 static tree
3189 prune_vars_needing_no_initialization (vars)
3190 tree vars;
3192 tree var;
3193 tree result;
3195 for (var = vars, result = NULL_TREE;
3196 var;
3197 var = TREE_CHAIN (var))
3199 tree decl = TREE_VALUE (var);
3200 tree init = TREE_PURPOSE (var);
3202 /* Deal gracefully with error. */
3203 if (decl == error_mark_node)
3204 continue;
3206 /* The only things that can be initialized are variables. */
3207 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3209 /* If this object is not defined, we don't need to do anything
3210 here. */
3211 if (DECL_EXTERNAL (decl))
3212 continue;
3214 /* Also, if the initializer already contains errors, we can bail
3215 out now. */
3216 if (init && TREE_CODE (init) == TREE_LIST
3217 && value_member (error_mark_node, init))
3218 continue;
3220 /* This variable is going to need initialization and/or
3221 finalization, so we add it to the list. */
3222 result = tree_cons (init, decl, result);
3225 return result;
3228 /* Make sure we have told the back end about all the variables in
3229 VARS. */
3231 static void
3232 write_out_vars (vars)
3233 tree vars;
3235 tree v;
3237 for (v = vars; v; v = TREE_CHAIN (v))
3238 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3239 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3242 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3243 (otherwise) that will initialize all gobal objects with static
3244 storage duration having the indicated PRIORITY. */
3246 static void
3247 generate_ctor_or_dtor_function (constructor_p, priority)
3248 int constructor_p;
3249 int priority;
3251 char function_key;
3252 tree arguments;
3253 tree body;
3254 size_t i;
3256 /* We use `I' to indicate initialization and `D' to indicate
3257 destruction. */
3258 if (constructor_p)
3259 function_key = 'I';
3260 else
3261 function_key = 'D';
3263 /* Begin the function. */
3264 body = start_objects (function_key, priority);
3266 /* Call the static storage duration function with appropriate
3267 arguments. */
3268 for (i = 0; i < ssdf_decls->elements_used; ++i)
3270 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3271 NULL_TREE);
3272 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3273 arguments);
3274 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3275 arguments));
3278 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3279 calls to any functions marked with attributes indicating that
3280 they should be called at initialization- or destruction-time. */
3281 if (priority == DEFAULT_INIT_PRIORITY)
3283 tree fns;
3285 for (fns = constructor_p ? static_ctors : static_dtors;
3286 fns;
3287 fns = TREE_CHAIN (fns))
3288 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3291 /* Close out the function. */
3292 finish_objects (function_key, priority, body);
3295 /* Generate constructor and destructor functions for the priority
3296 indicated by N. */
3298 static int
3299 generate_ctor_and_dtor_functions_for_priority (n, data)
3300 splay_tree_node n;
3301 void *data ATTRIBUTE_UNUSED;
3303 int priority = (int) n->key;
3304 priority_info pi = (priority_info) n->value;
3306 /* Generate the functions themselves, but only if they are really
3307 needed. */
3308 if (pi->initializations_p
3309 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3310 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3311 priority);
3312 if (pi->destructions_p
3313 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3314 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3315 priority);
3317 /* Keep iterating. */
3318 return 0;
3321 /* This routine is called from the last rule in yyparse ().
3322 Its job is to create all the code needed to initialize and
3323 destroy the global aggregates. We do the destruction
3324 first, since that way we only need to reverse the decls once. */
3326 void
3327 finish_file ()
3329 tree vars;
3330 int reconsider;
3331 size_t i;
3333 at_eof = 1;
3335 /* Bad parse errors. Just forget about it. */
3336 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3337 return;
3339 /* Otherwise, GDB can get confused, because in only knows
3340 about source for LINENO-1 lines. */
3341 lineno -= 1;
3343 interface_unknown = 1;
3344 interface_only = 0;
3346 /* We now have to write out all the stuff we put off writing out.
3347 These include:
3349 o Template specializations that we have not yet instantiated,
3350 but which are needed.
3351 o Initialization and destruction for non-local objects with
3352 static storage duration. (Local objects with static storage
3353 duration are initialized when their scope is first entered,
3354 and are cleaned up via atexit.)
3355 o Virtual function tables.
3357 All of these may cause others to be needed. For example,
3358 instantiating one function may cause another to be needed, and
3359 generating the intiailzer for an object may cause templates to be
3360 instantiated, etc., etc. */
3362 timevar_push (TV_VARCONST);
3364 emit_support_tinfos ();
3368 reconsider = 0;
3370 /* If there are templates that we've put off instantiating, do
3371 them now. */
3372 instantiate_pending_templates ();
3374 /* Write out virtual tables as required. Note that writing out
3375 the virtual table for a template class may cause the
3376 instantiation of members of that class. */
3377 if (walk_globals (vtable_decl_p,
3378 finish_vtable_vardecl,
3379 /*data=*/0))
3380 reconsider = 1;
3382 /* Write out needed type info variables. Writing out one variable
3383 might cause others to be needed. */
3384 if (walk_globals (unemitted_tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3385 reconsider = 1;
3387 /* The list of objects with static storage duration is built up
3388 in reverse order. We clear STATIC_AGGREGATES so that any new
3389 aggregates added during the initialization of these will be
3390 initialized in the correct order when we next come around the
3391 loop. */
3392 vars = prune_vars_needing_no_initialization (static_aggregates);
3393 static_aggregates = NULL_TREE;
3395 if (vars)
3397 tree v;
3399 /* We need to start a new initialization function each time
3400 through the loop. That's because we need to know which
3401 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3402 isn't computed until a function is finished, and written
3403 out. That's a deficiency in the back-end. When this is
3404 fixed, these initialization functions could all become
3405 inline, with resulting performance improvements. */
3406 tree ssdf_body = start_static_storage_duration_function ();
3408 /* Make sure the back end knows about all the variables. */
3409 write_out_vars (vars);
3411 /* First generate code to do all the initializations. */
3412 for (v = vars; v; v = TREE_CHAIN (v))
3413 do_static_initialization (TREE_VALUE (v),
3414 TREE_PURPOSE (v));
3416 /* Then, generate code to do all the destructions. Do these
3417 in reverse order so that the most recently constructed
3418 variable is the first destroyed. If we're using
3419 __cxa_atexit, then we don't need to do this; functions
3420 were registered at initialization time to destroy the
3421 local statics. */
3422 if (!flag_use_cxa_atexit)
3424 vars = nreverse (vars);
3425 for (v = vars; v; v = TREE_CHAIN (v))
3426 do_static_destruction (TREE_VALUE (v));
3428 else
3429 vars = NULL_TREE;
3431 /* Finish up the static storage duration function for this
3432 round. */
3433 finish_static_storage_duration_function (ssdf_body);
3435 /* All those initializations and finalizations might cause
3436 us to need more inline functions, more template
3437 instantiations, etc. */
3438 reconsider = 1;
3441 /* Go through the various inline functions, and see if any need
3442 synthesizing. */
3443 for (i = 0; i < deferred_fns_used; ++i)
3445 tree decl = VARRAY_TREE (deferred_fns, i);
3446 import_export_decl (decl);
3447 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3448 && TREE_USED (decl)
3449 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3451 /* Even though we're already at the top-level, we push
3452 there again. That way, when we pop back a few lines
3453 hence, all of our state is restored. Otherwise,
3454 finish_function doesn't clean things up, and we end
3455 up with CURRENT_FUNCTION_DECL set. */
3456 push_to_top_level ();
3457 synthesize_method (decl);
3458 pop_from_top_level ();
3459 reconsider = 1;
3463 /* We lie to the back-end, pretending that some functions are
3464 not defined when they really are. This keeps these functions
3465 from being put out unnecessarily. But, we must stop lying
3466 when the functions are referenced, or if they are not comdat
3467 since they need to be put out now.
3468 This is done in a separate for cycle, because if some deferred
3469 function is contained in another deferred function later in
3470 deferred_fns varray, rest_of_compilation would skip this
3471 function and we really cannot expand the same function twice. */
3472 for (i = 0; i < deferred_fns_used; ++i)
3474 tree decl = VARRAY_TREE (deferred_fns, i);
3476 if (DECL_NOT_REALLY_EXTERN (decl)
3477 && DECL_INITIAL (decl)
3478 && DECL_NEEDED_P (decl))
3479 DECL_EXTERNAL (decl) = 0;
3482 for (i = 0; i < deferred_fns_used; ++i)
3484 tree decl = VARRAY_TREE (deferred_fns, i);
3486 /* If we're going to need to write this function out, and
3487 there's already a body for it, create RTL for it now.
3488 (There might be no body if this is a method we haven't
3489 gotten around to synthesizing yet.) */
3490 if (!DECL_EXTERNAL (decl)
3491 && DECL_NEEDED_P (decl)
3492 && DECL_SAVED_TREE (decl)
3493 && !TREE_ASM_WRITTEN (decl))
3495 int saved_not_really_extern;
3497 /* When we call finish_function in expand_body, it will
3498 try to reset DECL_NOT_REALLY_EXTERN so we save and
3499 restore it here. */
3500 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3501 /* Generate RTL for this function now that we know we
3502 need it. */
3503 expand_body (decl);
3504 /* Undo the damage done by finish_function. */
3505 DECL_EXTERNAL (decl) = 0;
3506 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3507 /* If we're compiling -fsyntax-only pretend that this
3508 function has been written out so that we don't try to
3509 expand it again. */
3510 if (flag_syntax_only)
3511 TREE_ASM_WRITTEN (decl) = 1;
3512 reconsider = 1;
3516 if (deferred_fns_used
3517 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3518 deferred_fns_used))
3519 reconsider = 1;
3520 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3521 reconsider = 1;
3523 /* Static data members are just like namespace-scope globals. */
3524 for (i = 0; i < pending_statics_used; ++i)
3526 tree decl = VARRAY_TREE (pending_statics, i);
3527 if (TREE_ASM_WRITTEN (decl))
3528 continue;
3529 import_export_decl (decl);
3530 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3531 DECL_EXTERNAL (decl) = 0;
3533 if (pending_statics
3534 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3535 pending_statics_used))
3536 reconsider = 1;
3538 while (reconsider);
3540 /* We give C linkage to static constructors and destructors. */
3541 push_lang_context (lang_name_c);
3543 /* Generate initialization and destruction functions for all
3544 priorities for which they are required. */
3545 if (priority_info_map)
3546 splay_tree_foreach (priority_info_map,
3547 generate_ctor_and_dtor_functions_for_priority,
3548 /*data=*/0);
3550 /* We're done with the splay-tree now. */
3551 if (priority_info_map)
3552 splay_tree_delete (priority_info_map);
3554 /* We're done with static constructors, so we can go back to "C++"
3555 linkage now. */
3556 pop_lang_context ();
3558 /* Now delete from the chain of variables all virtual function tables.
3559 We output them all ourselves, because each will be treated
3560 specially. We don't do this if we're just doing semantic
3561 analysis, and not code-generation. */
3562 if (!flag_syntax_only)
3563 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3565 /* Now, issue warnings about static, but not defined, functions,
3566 etc., and emit debugging information. */
3567 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3568 if (pending_statics)
3569 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3570 pending_statics_used);
3572 finish_repo ();
3574 /* The entire file is now complete. If requested, dump everything
3575 to a file. */
3577 int flags;
3578 FILE *stream = dump_begin (TDI_all, &flags);
3580 if (stream)
3582 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3583 dump_end (TDI_all, stream);
3587 timevar_pop (TV_VARCONST);
3589 if (flag_detailed_statistics)
3591 dump_tree_statistics ();
3592 dump_time_statistics ();
3596 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3597 expr. Since it was parsed like a type, we need to wade through and fix
3598 that. Unfortunately, since operator() is left-associative, we can't use
3599 tail recursion. In the above example, TYPE is `A', and DECL is
3600 `()()()()()'.
3602 Maybe this shouldn't be recursive, but how often will it actually be
3603 used? (jason) */
3605 tree
3606 reparse_absdcl_as_expr (type, decl)
3607 tree type, decl;
3609 /* do build_functional_cast (type, NULL_TREE) at bottom */
3610 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3611 return build_functional_cast (type, NULL_TREE);
3613 /* recurse */
3614 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3616 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3618 if (TREE_CODE (decl) == CALL_EXPR
3619 && (! TREE_TYPE (decl)
3620 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3621 decl = require_complete_type (decl);
3623 return decl;
3626 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3627 out to be an expr. Since it was parsed like a type, we need to wade
3628 through and fix that. Since casts are right-associative, we are
3629 reversing the order, so we don't have to recurse.
3631 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3632 `1'. */
3634 tree
3635 reparse_absdcl_as_casts (decl, expr)
3636 tree decl, expr;
3638 tree type;
3639 int non_void_p = 0;
3641 if (TREE_CODE (expr) == CONSTRUCTOR
3642 && TREE_TYPE (expr) == 0)
3644 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3645 decl = TREE_OPERAND (decl, 0);
3647 if (processing_template_decl)
3648 TREE_TYPE (expr) = type;
3649 else
3651 expr = digest_init (type, expr, (tree *) 0);
3652 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3654 int failure = complete_array_type (type, expr, 1);
3655 my_friendly_assert (!failure, 78);
3660 while (decl)
3662 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3663 decl = TREE_OPERAND (decl, 0);
3664 if (!VOID_TYPE_P (type))
3665 non_void_p = 1;
3666 expr = build_c_cast (type, expr);
3669 if (warn_old_style_cast && ! in_system_header
3670 && non_void_p && current_lang_name != lang_name_c)
3671 warning ("use of old-style cast");
3673 return expr;
3676 /* Given plain tree nodes for an expression, build up the full semantics. */
3678 tree
3679 build_expr_from_tree (t)
3680 tree t;
3682 if (t == NULL_TREE || t == error_mark_node)
3683 return t;
3685 switch (TREE_CODE (t))
3687 case IDENTIFIER_NODE:
3688 return do_identifier (t, 0, NULL_TREE);
3690 case LOOKUP_EXPR:
3691 if (LOOKUP_EXPR_GLOBAL (t))
3692 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3693 else
3694 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3696 case TEMPLATE_ID_EXPR:
3697 return (lookup_template_function
3698 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3699 build_expr_from_tree (TREE_OPERAND (t, 1))));
3701 case INDIRECT_REF:
3702 return build_x_indirect_ref
3703 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3705 case CAST_EXPR:
3706 return build_functional_cast
3707 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3709 case REINTERPRET_CAST_EXPR:
3710 return build_reinterpret_cast
3711 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3713 case CONST_CAST_EXPR:
3714 return build_const_cast
3715 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3717 case DYNAMIC_CAST_EXPR:
3718 return build_dynamic_cast
3719 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3721 case STATIC_CAST_EXPR:
3722 return build_static_cast
3723 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3725 case PREDECREMENT_EXPR:
3726 case PREINCREMENT_EXPR:
3727 case POSTDECREMENT_EXPR:
3728 case POSTINCREMENT_EXPR:
3729 case NEGATE_EXPR:
3730 case BIT_NOT_EXPR:
3731 case ABS_EXPR:
3732 case TRUTH_NOT_EXPR:
3733 case ADDR_EXPR:
3734 case CONVERT_EXPR: /* Unary + */
3735 case REALPART_EXPR:
3736 case IMAGPART_EXPR:
3737 if (TREE_TYPE (t))
3738 return t;
3739 return build_x_unary_op (TREE_CODE (t),
3740 build_expr_from_tree (TREE_OPERAND (t, 0)));
3742 case PLUS_EXPR:
3743 case MINUS_EXPR:
3744 case MULT_EXPR:
3745 case TRUNC_DIV_EXPR:
3746 case CEIL_DIV_EXPR:
3747 case FLOOR_DIV_EXPR:
3748 case ROUND_DIV_EXPR:
3749 case EXACT_DIV_EXPR:
3750 case BIT_AND_EXPR:
3751 case BIT_ANDTC_EXPR:
3752 case BIT_IOR_EXPR:
3753 case BIT_XOR_EXPR:
3754 case TRUNC_MOD_EXPR:
3755 case FLOOR_MOD_EXPR:
3756 case TRUTH_ANDIF_EXPR:
3757 case TRUTH_ORIF_EXPR:
3758 case TRUTH_AND_EXPR:
3759 case TRUTH_OR_EXPR:
3760 case RSHIFT_EXPR:
3761 case LSHIFT_EXPR:
3762 case RROTATE_EXPR:
3763 case LROTATE_EXPR:
3764 case EQ_EXPR:
3765 case NE_EXPR:
3766 case MAX_EXPR:
3767 case MIN_EXPR:
3768 case LE_EXPR:
3769 case GE_EXPR:
3770 case LT_EXPR:
3771 case GT_EXPR:
3772 case MEMBER_REF:
3773 return build_x_binary_op
3774 (TREE_CODE (t),
3775 build_expr_from_tree (TREE_OPERAND (t, 0)),
3776 build_expr_from_tree (TREE_OPERAND (t, 1)));
3778 case DOTSTAR_EXPR:
3779 return build_m_component_ref
3780 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3781 build_expr_from_tree (TREE_OPERAND (t, 1)));
3783 case SCOPE_REF:
3784 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3786 case ARRAY_REF:
3787 if (TREE_OPERAND (t, 0) == NULL_TREE)
3788 /* new-type-id */
3789 return build_nt (ARRAY_REF, NULL_TREE,
3790 build_expr_from_tree (TREE_OPERAND (t, 1)));
3791 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3792 build_expr_from_tree (TREE_OPERAND (t, 1)));
3794 case SIZEOF_EXPR:
3795 case ALIGNOF_EXPR:
3797 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3798 if (!TYPE_P (r))
3799 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3800 else
3801 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3804 case MODOP_EXPR:
3805 return build_x_modify_expr
3806 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3807 TREE_CODE (TREE_OPERAND (t, 1)),
3808 build_expr_from_tree (TREE_OPERAND (t, 2)));
3810 case ARROW_EXPR:
3811 return build_x_arrow
3812 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3814 case NEW_EXPR:
3815 return build_new
3816 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3817 build_expr_from_tree (TREE_OPERAND (t, 1)),
3818 build_expr_from_tree (TREE_OPERAND (t, 2)),
3819 NEW_EXPR_USE_GLOBAL (t));
3821 case DELETE_EXPR:
3822 return delete_sanity
3823 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3824 build_expr_from_tree (TREE_OPERAND (t, 1)),
3825 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3827 case COMPOUND_EXPR:
3828 if (TREE_OPERAND (t, 1) == NULL_TREE)
3829 return build_x_compound_expr
3830 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3831 else
3832 abort ();
3834 case METHOD_CALL_EXPR:
3835 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3837 tree ref = TREE_OPERAND (t, 0);
3838 tree name = TREE_OPERAND (ref, 1);
3840 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3841 name = build_nt (TEMPLATE_ID_EXPR,
3842 TREE_OPERAND (name, 0),
3843 build_expr_from_tree (TREE_OPERAND (name, 1)));
3845 return build_scoped_method_call
3846 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3847 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3848 name,
3849 build_expr_from_tree (TREE_OPERAND (t, 2)));
3851 else
3853 tree fn = TREE_OPERAND (t, 0);
3855 /* We can get a TEMPLATE_ID_EXPR here on code like:
3857 x->f<2>();
3859 so we must resolve that. However, we can also get things
3860 like a BIT_NOT_EXPR here, when referring to a destructor,
3861 and things like that are not correctly resolved by
3862 build_expr_from_tree. So, just use build_expr_from_tree
3863 when we really need it. */
3864 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3865 fn = lookup_template_function
3866 (TREE_OPERAND (fn, 0),
3867 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3869 return build_method_call
3870 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3872 build_expr_from_tree (TREE_OPERAND (t, 2)),
3873 NULL_TREE, LOOKUP_NORMAL);
3876 case CALL_EXPR:
3877 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3879 tree ref = TREE_OPERAND (t, 0);
3880 tree name = TREE_OPERAND (ref, 1);
3882 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3883 name = build_nt (TEMPLATE_ID_EXPR,
3884 TREE_OPERAND (name, 0),
3885 build_expr_from_tree (TREE_OPERAND (name, 1)));
3887 return build_member_call
3888 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3889 name,
3890 build_expr_from_tree (TREE_OPERAND (t, 1)));
3892 else
3894 tree name = TREE_OPERAND (t, 0);
3895 tree id;
3896 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3897 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3898 && !LOOKUP_EXPR_GLOBAL (name)
3899 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3900 && (!current_class_type
3901 || !lookup_member (current_class_type, id, 0, 0)))
3903 /* Do Koenig lookup if there are no class members. */
3904 name = do_identifier (id, 0, args);
3906 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3907 || ! really_overloaded_fn (name))
3908 name = build_expr_from_tree (name);
3909 return build_x_function_call (name, args, current_class_ref);
3912 case COND_EXPR:
3913 return build_x_conditional_expr
3914 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3915 build_expr_from_tree (TREE_OPERAND (t, 1)),
3916 build_expr_from_tree (TREE_OPERAND (t, 2)));
3918 case PSEUDO_DTOR_EXPR:
3919 return (finish_pseudo_destructor_call_expr
3920 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3921 build_expr_from_tree (TREE_OPERAND (t, 1)),
3922 build_expr_from_tree (TREE_OPERAND (t, 2))));
3924 case TREE_LIST:
3926 tree purpose, value, chain;
3928 if (t == void_list_node)
3929 return t;
3931 purpose = TREE_PURPOSE (t);
3932 if (purpose)
3933 purpose = build_expr_from_tree (purpose);
3934 value = TREE_VALUE (t);
3935 if (value)
3936 value = build_expr_from_tree (value);
3937 chain = TREE_CHAIN (t);
3938 if (chain && chain != void_type_node)
3939 chain = build_expr_from_tree (chain);
3940 return tree_cons (purpose, value, chain);
3943 case COMPONENT_REF:
3945 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3946 tree field = TREE_OPERAND (t, 1);
3948 /* We use a COMPONENT_REF to indicate things of the form `x.b'
3949 and `x.A::b'. We must distinguish between those cases
3950 here. */
3951 if (TREE_CODE (field) == SCOPE_REF)
3952 return build_object_ref (object,
3953 TREE_OPERAND (field, 0),
3954 TREE_OPERAND (field, 1));
3955 else
3956 return build_x_component_ref (object, field,
3957 NULL_TREE, 1);
3960 case THROW_EXPR:
3961 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3963 case CONSTRUCTOR:
3965 tree r;
3966 tree elts;
3967 tree type = TREE_TYPE (t);
3968 bool purpose_p;
3970 /* digest_init will do the wrong thing if we let it. */
3971 if (type && TYPE_PTRMEMFUNC_P (type))
3972 return t;
3974 r = NULL_TREE;
3975 /* We do not want to process the purpose of aggregate
3976 initializers as they are identifier nodes which will be
3977 looked up by digest_init. */
3978 purpose_p = !(type && IS_AGGR_TYPE (type));
3979 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3981 tree purpose = TREE_PURPOSE (elts);
3982 tree value = TREE_VALUE (elts);
3984 if (purpose && purpose_p)
3985 purpose = build_expr_from_tree (purpose);
3986 value = build_expr_from_tree (value);
3987 r = tree_cons (purpose, value, r);
3990 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3991 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3993 if (type)
3994 return digest_init (type, r, 0);
3995 return r;
3998 case TYPEID_EXPR:
3999 if (TYPE_P (TREE_OPERAND (t, 0)))
4000 return get_typeid (TREE_OPERAND (t, 0));
4001 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4003 case VAR_DECL:
4004 return convert_from_reference (t);
4006 case VA_ARG_EXPR:
4007 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4008 TREE_TYPE (t));
4010 default:
4011 return t;
4015 /* This is something of the form `int (*a)++' that has turned out to be an
4016 expr. It was only converted into parse nodes, so we need to go through
4017 and build up the semantics. Most of the work is done by
4018 build_expr_from_tree, above.
4020 In the above example, TYPE is `int' and DECL is `*a'. */
4022 tree
4023 reparse_decl_as_expr (type, decl)
4024 tree type, decl;
4026 decl = build_expr_from_tree (decl);
4027 if (type)
4028 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4029 else
4030 return decl;
4033 /* This is something of the form `int (*a)' that has turned out to be a
4034 decl. It was only converted into parse nodes, so we need to do the
4035 checking that make_{pointer,reference}_declarator do. */
4037 tree
4038 finish_decl_parsing (decl)
4039 tree decl;
4041 switch (TREE_CODE (decl))
4043 case IDENTIFIER_NODE:
4044 return decl;
4045 case INDIRECT_REF:
4046 return make_pointer_declarator
4047 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4048 case ADDR_EXPR:
4049 return make_reference_declarator
4050 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4051 case BIT_NOT_EXPR:
4052 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4053 return decl;
4054 case SCOPE_REF:
4055 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4056 TREE_COMPLEXITY (decl) = current_class_depth;
4057 return decl;
4058 case ARRAY_REF:
4059 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4060 return decl;
4061 case TREE_LIST:
4062 /* For attribute handling. */
4063 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4064 return decl;
4065 case TEMPLATE_ID_EXPR:
4066 return decl;
4067 default:
4068 abort ();
4069 return NULL_TREE;
4073 /* Return 1 if root encloses child. */
4075 static int
4076 is_namespace_ancestor (root, child)
4077 tree root, child;
4079 if (root == child)
4080 return 1;
4081 if (root == global_namespace)
4082 return 1;
4083 if (child == global_namespace)
4084 return 0;
4085 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4089 /* Return the namespace that is the common ancestor
4090 of two given namespaces. */
4092 tree
4093 namespace_ancestor (ns1, ns2)
4094 tree ns1, ns2;
4096 if (is_namespace_ancestor (ns1, ns2))
4097 return ns1;
4098 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4101 /* Insert used into the using list of user. Set indirect_flag if this
4102 directive is not directly from the source. Also find the common
4103 ancestor and let our users know about the new namespace */
4104 static void
4105 add_using_namespace (user, used, indirect)
4106 tree user;
4107 tree used;
4108 int indirect;
4110 tree t;
4111 /* Using oneself is a no-op. */
4112 if (user == used)
4113 return;
4114 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4115 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4116 /* Check if we already have this. */
4117 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4118 if (t != NULL_TREE)
4120 if (!indirect)
4121 /* Promote to direct usage. */
4122 TREE_INDIRECT_USING (t) = 0;
4123 return;
4126 /* Add used to the user's using list. */
4127 DECL_NAMESPACE_USING (user)
4128 = tree_cons (used, namespace_ancestor (user, used),
4129 DECL_NAMESPACE_USING (user));
4131 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4133 /* Add user to the used's users list. */
4134 DECL_NAMESPACE_USERS (used)
4135 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4137 /* Recursively add all namespaces used. */
4138 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4139 /* indirect usage */
4140 add_using_namespace (user, TREE_PURPOSE (t), 1);
4142 /* Tell everyone using us about the new used namespaces. */
4143 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4144 add_using_namespace (TREE_PURPOSE (t), used, 1);
4147 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4148 duplicates. The first list becomes the tail of the result.
4150 The algorithm is O(n^2). We could get this down to O(n log n) by
4151 doing a sort on the addresses of the functions, if that becomes
4152 necessary. */
4154 static tree
4155 merge_functions (s1, s2)
4156 tree s1;
4157 tree s2;
4159 for (; s2; s2 = OVL_NEXT (s2))
4161 tree fn2 = OVL_CURRENT (s2);
4162 tree fns1;
4164 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4166 tree fn1 = OVL_CURRENT (fns1);
4168 /* If the function from S2 is already in S1, there is no
4169 need to add it again. For `extern "C"' functions, we
4170 might have two FUNCTION_DECLs for the same function, in
4171 different namespaces; again, we only need one of them. */
4172 if (fn1 == fn2
4173 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
4174 && DECL_NAME (fn1) == DECL_NAME (fn2)))
4175 break;
4178 /* If we exhausted all of the functions in S1, FN2 is new. */
4179 if (!fns1)
4180 s1 = build_overload (fn2, s1);
4182 return s1;
4185 /* This should return an error not all definitions define functions.
4186 It is not an error if we find two functions with exactly the
4187 same signature, only if these are selected in overload resolution.
4188 old is the current set of bindings, new the freshly-found binding.
4189 XXX Do we want to give *all* candidates in case of ambiguity?
4190 XXX In what way should I treat extern declarations?
4191 XXX I don't want to repeat the entire duplicate_decls here */
4193 static tree
4194 ambiguous_decl (name, old, new, flags)
4195 tree name;
4196 tree old;
4197 tree new;
4198 int flags;
4200 tree val, type;
4201 my_friendly_assert (old != NULL_TREE, 393);
4202 /* Copy the value. */
4203 val = BINDING_VALUE (new);
4204 if (val)
4205 switch (TREE_CODE (val))
4207 case TEMPLATE_DECL:
4208 /* If we expect types or namespaces, and not templates,
4209 or this is not a template class. */
4210 if (LOOKUP_QUALIFIERS_ONLY (flags)
4211 && !DECL_CLASS_TEMPLATE_P (val))
4212 val = NULL_TREE;
4213 break;
4214 case TYPE_DECL:
4215 if (LOOKUP_NAMESPACES_ONLY (flags))
4216 val = NULL_TREE;
4217 break;
4218 case NAMESPACE_DECL:
4219 if (LOOKUP_TYPES_ONLY (flags))
4220 val = NULL_TREE;
4221 break;
4222 case FUNCTION_DECL:
4223 /* Ignore built-in functions that are still anticipated. */
4224 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
4225 val = NULL_TREE;
4226 break;
4227 default:
4228 if (LOOKUP_QUALIFIERS_ONLY (flags))
4229 val = NULL_TREE;
4232 if (!BINDING_VALUE (old))
4233 BINDING_VALUE (old) = val;
4234 else if (val && val != BINDING_VALUE (old))
4236 if (is_overloaded_fn (BINDING_VALUE (old))
4237 && is_overloaded_fn (val))
4239 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4240 val);
4242 else
4244 /* Some declarations are functions, some are not. */
4245 if (flags & LOOKUP_COMPLAIN)
4247 /* If we've already given this error for this lookup,
4248 BINDING_VALUE (old) is error_mark_node, so let's not
4249 repeat ourselves. */
4250 if (BINDING_VALUE (old) != error_mark_node)
4252 error ("use of `%D' is ambiguous", name);
4253 cp_error_at (" first declared as `%#D' here",
4254 BINDING_VALUE (old));
4256 cp_error_at (" also declared as `%#D' here", val);
4258 BINDING_VALUE (old) = error_mark_node;
4261 /* ... and copy the type. */
4262 type = BINDING_TYPE (new);
4263 if (LOOKUP_NAMESPACES_ONLY (flags))
4264 type = NULL_TREE;
4265 if (!BINDING_TYPE (old))
4266 BINDING_TYPE (old) = type;
4267 else if (type && BINDING_TYPE (old) != type)
4269 if (flags & LOOKUP_COMPLAIN)
4271 error ("`%D' denotes an ambiguous type",name);
4272 cp_error_at (" first type here", BINDING_TYPE (old));
4273 cp_error_at (" other type here", type);
4276 return old;
4279 /* Subroutine of unualified_namespace_lookup:
4280 Add the bindings of NAME in used namespaces to VAL.
4281 We are currently looking for names in namespace SCOPE, so we
4282 look through USINGS for using-directives of namespaces
4283 which have SCOPE as a common ancestor with the current scope.
4284 Returns zero on errors. */
4287 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4288 tree name, val, usings, scope;
4289 int flags;
4290 tree *spacesp;
4292 tree iter;
4293 tree val1;
4294 /* Iterate over all used namespaces in current, searching for using
4295 directives of scope. */
4296 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4297 if (TREE_VALUE (iter) == scope)
4299 if (spacesp)
4300 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4301 *spacesp);
4302 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4303 /* Resolve ambiguities. */
4304 val = ambiguous_decl (name, val, val1, flags);
4306 return BINDING_VALUE (val) != error_mark_node;
4309 /* [namespace.qual]
4310 Accepts the NAME to lookup and its qualifying SCOPE.
4311 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4312 or 0 on error. */
4315 qualified_lookup_using_namespace (name, scope, result, flags)
4316 tree name;
4317 tree scope;
4318 tree result;
4319 int flags;
4321 /* Maintain a list of namespaces visited... */
4322 tree seen = NULL_TREE;
4323 /* ... and a list of namespace yet to see. */
4324 tree todo = NULL_TREE;
4325 tree usings;
4326 /* Look through namespace aliases. */
4327 scope = ORIGINAL_NAMESPACE (scope);
4328 while (scope && (result != error_mark_node))
4330 seen = tree_cons (scope, NULL_TREE, seen);
4331 result = ambiguous_decl (name, result,
4332 binding_for_name (name, scope), flags);
4333 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4334 /* Consider using directives. */
4335 for (usings = DECL_NAMESPACE_USING (scope); usings;
4336 usings = TREE_CHAIN (usings))
4337 /* If this was a real directive, and we have not seen it. */
4338 if (!TREE_INDIRECT_USING (usings)
4339 && !purpose_member (TREE_PURPOSE (usings), seen))
4340 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4341 if (todo)
4343 scope = TREE_PURPOSE (todo);
4344 todo = TREE_CHAIN (todo);
4346 else
4347 scope = NULL_TREE; /* If there never was a todo list. */
4349 return result != error_mark_node;
4352 /* [namespace.memdef]/2 */
4354 /* Set the context of a declaration to scope. Complain if we are not
4355 outside scope. */
4357 void
4358 set_decl_namespace (decl, scope, friendp)
4359 tree decl;
4360 tree scope;
4361 int friendp;
4363 tree old;
4365 /* Get rid of namespace aliases. */
4366 scope = ORIGINAL_NAMESPACE (scope);
4368 /* It is ok for friends to be qualified in parallel space. */
4369 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4370 error ("declaration of `%D' not in a namespace surrounding `%D'",
4371 decl, scope);
4372 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4373 if (scope != current_namespace)
4375 /* See whether this has been declared in the namespace. */
4376 old = namespace_binding (DECL_NAME (decl), scope);
4377 if (!old)
4378 /* No old declaration at all. */
4379 goto complain;
4380 /* A template can be explicitly specialized in any namespace. */
4381 if (processing_explicit_instantiation)
4382 return;
4383 if (!is_overloaded_fn (decl))
4384 /* Don't compare non-function decls with decls_match here,
4385 since it can't check for the correct constness at this
4386 point. pushdecl will find those errors later. */
4387 return;
4388 /* Since decl is a function, old should contain a function decl. */
4389 if (!is_overloaded_fn (old))
4390 goto complain;
4391 if (processing_template_decl || processing_specialization)
4392 /* We have not yet called push_template_decl to turn the
4393 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4394 won't match. But, we'll check later, when we construct the
4395 template. */
4396 return;
4397 for (; old; old = OVL_NEXT (old))
4398 if (decls_match (decl, OVL_CURRENT (old)))
4399 return;
4401 else
4402 return;
4403 complain:
4404 error ("`%D' should have been declared inside `%D'",
4405 decl, scope);
4408 /* Compute the namespace where a declaration is defined. */
4410 static tree
4411 decl_namespace (decl)
4412 tree decl;
4414 if (TYPE_P (decl))
4415 decl = TYPE_STUB_DECL (decl);
4416 while (DECL_CONTEXT (decl))
4418 decl = DECL_CONTEXT (decl);
4419 if (TREE_CODE (decl) == NAMESPACE_DECL)
4420 return decl;
4421 if (TYPE_P (decl))
4422 decl = TYPE_STUB_DECL (decl);
4423 my_friendly_assert (DECL_P (decl), 390);
4426 return global_namespace;
4429 /* Return the namespace where the current declaration is declared. */
4431 tree
4432 current_decl_namespace ()
4434 tree result;
4435 /* If we have been pushed into a different namespace, use it. */
4436 if (decl_namespace_list)
4437 return TREE_PURPOSE (decl_namespace_list);
4439 if (current_class_type)
4440 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4441 else if (current_function_decl)
4442 result = decl_namespace (current_function_decl);
4443 else
4444 result = current_namespace;
4445 return result;
4448 /* Temporarily set the namespace for the current declaration. */
4450 void
4451 push_decl_namespace (decl)
4452 tree decl;
4454 if (TREE_CODE (decl) != NAMESPACE_DECL)
4455 decl = decl_namespace (decl);
4456 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4457 NULL_TREE, decl_namespace_list);
4460 void
4461 pop_decl_namespace ()
4463 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4466 /* Enter a class or namespace scope. */
4468 void
4469 push_scope (t)
4470 tree t;
4472 if (TREE_CODE (t) == NAMESPACE_DECL)
4473 push_decl_namespace (t);
4474 else
4475 pushclass (t, 2);
4478 /* Leave scope pushed by push_scope. */
4480 void
4481 pop_scope (t)
4482 tree t;
4484 if (TREE_CODE (t) == NAMESPACE_DECL)
4485 pop_decl_namespace ();
4486 else
4487 popclass ();
4490 /* [basic.lookup.koenig] */
4491 /* A non-zero return value in the functions below indicates an error.
4492 All nodes allocated in the procedure are on the scratch obstack. */
4494 struct arg_lookup
4496 tree name;
4497 tree namespaces;
4498 tree classes;
4499 tree functions;
4502 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4503 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4504 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4505 static int add_function PARAMS ((struct arg_lookup *, tree));
4506 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4507 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4508 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4510 /* Add a function to the lookup structure.
4511 Returns 1 on error. */
4513 static int
4514 add_function (k, fn)
4515 struct arg_lookup *k;
4516 tree fn;
4518 /* We used to check here to see if the function was already in the list,
4519 but that's O(n^2), which is just too expensive for function lookup.
4520 Now we deal with the occasional duplicate in joust. In doing this, we
4521 assume that the number of duplicates will be small compared to the
4522 total number of functions being compared, which should usually be the
4523 case. */
4525 /* We must find only functions, or exactly one non-function. */
4526 if (!k->functions)
4527 k->functions = fn;
4528 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4529 k->functions = build_overload (fn, k->functions);
4530 else
4532 tree f1 = OVL_CURRENT (k->functions);
4533 tree f2 = fn;
4534 if (is_overloaded_fn (f1))
4536 fn = f1; f1 = f2; f2 = fn;
4538 cp_error_at ("`%D' is not a function,", f1);
4539 cp_error_at (" conflict with `%D'", f2);
4540 error (" in call to `%D'", k->name);
4541 return 1;
4544 return 0;
4547 /* Add functions of a namespace to the lookup structure.
4548 Returns 1 on error. */
4550 static int
4551 arg_assoc_namespace (k, scope)
4552 struct arg_lookup *k;
4553 tree scope;
4555 tree value;
4557 if (purpose_member (scope, k->namespaces))
4558 return 0;
4559 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4561 value = namespace_binding (k->name, scope);
4562 if (!value)
4563 return 0;
4565 for (; value; value = OVL_NEXT (value))
4566 if (add_function (k, OVL_CURRENT (value)))
4567 return 1;
4569 return 0;
4572 /* Adds everything associated with a template argument to the lookup
4573 structure. Returns 1 on error. */
4575 static int
4576 arg_assoc_template_arg (k, arg)
4577 struct arg_lookup* k;
4578 tree arg;
4580 /* [basic.lookup.koenig]
4582 If T is a template-id, its associated namespaces and classes are
4583 ... the namespaces and classes associated with the types of the
4584 template arguments provided for template type parameters
4585 (excluding template template parameters); the namespaces in which
4586 any template template arguments are defined; and the classes in
4587 which any member templates used as template template arguments
4588 are defined. [Note: non-type template arguments do not
4589 contribute to the set of associated namespaces. ] */
4591 /* Consider first template template arguments. */
4592 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4593 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4594 return 0;
4595 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4597 tree ctx = CP_DECL_CONTEXT (arg);
4599 /* It's not a member template. */
4600 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4601 return arg_assoc_namespace (k, ctx);
4602 /* Otherwise, it must be member template. */
4603 else
4604 return arg_assoc_class (k, ctx);
4606 /* It's not a template template argument, but it is a type template
4607 argument. */
4608 else if (TYPE_P (arg))
4609 return arg_assoc_type (k, arg);
4610 /* It's a non-type template argument. */
4611 else
4612 return 0;
4615 /* Adds everything associated with class to the lookup structure.
4616 Returns 1 on error. */
4618 static int
4619 arg_assoc_class (k, type)
4620 struct arg_lookup* k;
4621 tree type;
4623 tree list, friends, context;
4624 int i;
4626 /* Backend build structures, such as __builtin_va_list, aren't
4627 affected by all this. */
4628 if (!CLASS_TYPE_P (type))
4629 return 0;
4631 if (purpose_member (type, k->classes))
4632 return 0;
4633 k->classes = tree_cons (type, NULL_TREE, k->classes);
4635 context = decl_namespace (TYPE_MAIN_DECL (type));
4636 if (arg_assoc_namespace (k, context))
4637 return 1;
4639 /* Process baseclasses. */
4640 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4641 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4642 return 1;
4644 /* Process friends. */
4645 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4646 list = TREE_CHAIN (list))
4647 if (k->name == TREE_PURPOSE (list))
4648 for (friends = TREE_VALUE (list); friends;
4649 friends = TREE_CHAIN (friends))
4650 /* Only interested in global functions with potentially hidden
4651 (i.e. unqualified) declarations. */
4652 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4653 && decl_namespace (TREE_VALUE (friends)) == context)
4654 if (add_function (k, TREE_VALUE (friends)))
4655 return 1;
4657 /* Process template arguments. */
4658 if (CLASSTYPE_TEMPLATE_INFO (type))
4660 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4661 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4662 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4665 return 0;
4668 /* Adds everything associated with a given type.
4669 Returns 1 on error. */
4671 static int
4672 arg_assoc_type (k, type)
4673 struct arg_lookup *k;
4674 tree type;
4676 switch (TREE_CODE (type))
4678 case VOID_TYPE:
4679 case INTEGER_TYPE:
4680 case REAL_TYPE:
4681 case COMPLEX_TYPE:
4682 case VECTOR_TYPE:
4683 case CHAR_TYPE:
4684 case BOOLEAN_TYPE:
4685 return 0;
4686 case RECORD_TYPE:
4687 if (TYPE_PTRMEMFUNC_P (type))
4688 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4689 return arg_assoc_class (k, type);
4690 case POINTER_TYPE:
4691 case REFERENCE_TYPE:
4692 case ARRAY_TYPE:
4693 return arg_assoc_type (k, TREE_TYPE (type));
4694 case UNION_TYPE:
4695 case ENUMERAL_TYPE:
4696 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4697 case OFFSET_TYPE:
4698 /* Pointer to member: associate class type and value type. */
4699 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4700 return 1;
4701 return arg_assoc_type (k, TREE_TYPE (type));
4702 case METHOD_TYPE:
4703 /* The basetype is referenced in the first arg type, so just
4704 fall through. */
4705 case FUNCTION_TYPE:
4706 /* Associate the parameter types. */
4707 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4708 return 1;
4709 /* Associate the return type. */
4710 return arg_assoc_type (k, TREE_TYPE (type));
4711 case TEMPLATE_TYPE_PARM:
4712 case BOUND_TEMPLATE_TEMPLATE_PARM:
4713 return 0;
4714 case TYPENAME_TYPE:
4715 return 0;
4716 case LANG_TYPE:
4717 if (type == unknown_type_node)
4718 return 0;
4719 /* else fall through */
4720 default:
4721 abort ();
4723 return 0;
4726 /* Adds everything associated with arguments. Returns 1 on error. */
4728 static int
4729 arg_assoc_args (k, args)
4730 struct arg_lookup* k;
4731 tree args;
4733 for (; args; args = TREE_CHAIN (args))
4734 if (arg_assoc (k, TREE_VALUE (args)))
4735 return 1;
4736 return 0;
4739 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4741 static int
4742 arg_assoc (k, n)
4743 struct arg_lookup* k;
4744 tree n;
4746 if (n == error_mark_node)
4747 return 0;
4749 if (TYPE_P (n))
4750 return arg_assoc_type (k, n);
4752 if (! type_unknown_p (n))
4753 return arg_assoc_type (k, TREE_TYPE (n));
4755 if (TREE_CODE (n) == ADDR_EXPR)
4756 n = TREE_OPERAND (n, 0);
4757 if (TREE_CODE (n) == COMPONENT_REF)
4758 n = TREE_OPERAND (n, 1);
4759 if (TREE_CODE (n) == OFFSET_REF)
4760 n = TREE_OPERAND (n, 1);
4761 while (TREE_CODE (n) == TREE_LIST)
4762 n = TREE_VALUE (n);
4764 if (TREE_CODE (n) == FUNCTION_DECL)
4765 return arg_assoc_type (k, TREE_TYPE (n));
4766 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4768 /* [basic.lookup.koenig]
4770 If T is a template-id, its associated namespaces and classes
4771 are the namespace in which the template is defined; for
4772 member templates, the member template's class... */
4773 tree template = TREE_OPERAND (n, 0);
4774 tree args = TREE_OPERAND (n, 1);
4775 tree ctx;
4776 tree arg;
4778 if (TREE_CODE (template) == COMPONENT_REF)
4779 template = TREE_OPERAND (template, 1);
4781 /* First, the template. There may actually be more than one if
4782 this is an overloaded function template. But, in that case,
4783 we only need the first; all the functions will be in the same
4784 namespace. */
4785 template = OVL_CURRENT (template);
4787 ctx = CP_DECL_CONTEXT (template);
4789 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4791 if (arg_assoc_namespace (k, ctx) == 1)
4792 return 1;
4794 /* It must be a member template. */
4795 else if (arg_assoc_class (k, ctx) == 1)
4796 return 1;
4798 /* Now the arguments. */
4799 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4800 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4801 return 1;
4803 else
4805 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4807 for (; n; n = OVL_CHAIN (n))
4808 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4809 return 1;
4812 return 0;
4815 /* Performs Koenig lookup depending on arguments, where fns
4816 are the functions found in normal lookup. */
4818 tree
4819 lookup_arg_dependent (name, fns, args)
4820 tree name;
4821 tree fns;
4822 tree args;
4824 struct arg_lookup k;
4825 tree fn = NULL_TREE;
4827 k.name = name;
4828 k.functions = fns;
4829 k.classes = NULL_TREE;
4831 /* Note that we've already looked at some namespaces during normal
4832 unqualified lookup, unless we found a decl in function scope. */
4833 if (fns)
4834 fn = OVL_CURRENT (fns);
4835 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4836 k.namespaces = NULL_TREE;
4837 else
4838 unqualified_namespace_lookup (name, 0, &k.namespaces);
4840 arg_assoc_args (&k, args);
4841 return k.functions;
4844 /* Process a namespace-alias declaration. */
4846 void
4847 do_namespace_alias (alias, namespace)
4848 tree alias, namespace;
4850 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4852 /* The parser did not find it, so it's not there. */
4853 error ("unknown namespace `%D'", namespace);
4854 return;
4857 namespace = ORIGINAL_NAMESPACE (namespace);
4859 /* Build the alias. */
4860 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4861 DECL_NAMESPACE_ALIAS (alias) = namespace;
4862 pushdecl (alias);
4865 /* Check a non-member using-declaration. Return the name and scope
4866 being used, and the USING_DECL, or NULL_TREE on failure. */
4868 static tree
4869 validate_nonmember_using_decl (decl, scope, name)
4870 tree decl;
4871 tree *scope;
4872 tree *name;
4874 if (TREE_CODE (decl) == SCOPE_REF)
4876 *scope = TREE_OPERAND (decl, 0);
4877 *name = TREE_OPERAND (decl, 1);
4879 if (!processing_template_decl)
4881 /* [namespace.udecl]
4882 A using-declaration for a class member shall be a
4883 member-declaration. */
4884 if(TREE_CODE (*scope) != NAMESPACE_DECL)
4886 if (TYPE_P (*scope))
4887 error ("`%T' is not a namespace", *scope);
4888 else
4889 error ("`%D' is not a namespace", *scope);
4890 return NULL_TREE;
4893 /* 7.3.3/5
4894 A using-declaration shall not name a template-id. */
4895 if (TREE_CODE (*name) == TEMPLATE_ID_EXPR)
4897 *name = TREE_OPERAND (*name, 0);
4898 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4899 return NULL_TREE;
4903 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4904 || TREE_CODE (decl) == TYPE_DECL
4905 || TREE_CODE (decl) == TEMPLATE_DECL)
4907 *scope = global_namespace;
4908 *name = decl;
4910 else if (TREE_CODE (decl) == NAMESPACE_DECL)
4912 error ("namespace `%D' not allowed in using-declaration", decl);
4913 return NULL_TREE;
4915 else
4916 abort ();
4917 if (DECL_P (*name))
4918 *name = DECL_NAME (*name);
4919 /* Make a USING_DECL. */
4920 return push_using_decl (*scope, *name);
4923 /* Process local and global using-declarations. */
4925 static void
4926 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4927 tree scope, name;
4928 tree oldval, oldtype;
4929 tree *newval, *newtype;
4931 tree decls;
4933 *newval = *newtype = NULL_TREE;
4934 decls = make_node (CPLUS_BINDING);
4935 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4936 /* Lookup error */
4937 return;
4939 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4941 error ("`%D' not declared", name);
4942 return;
4945 /* Check for using functions. */
4946 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4948 tree tmp, tmp1;
4950 if (oldval && !is_overloaded_fn (oldval))
4952 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4953 oldval = NULL_TREE;
4956 *newval = oldval;
4957 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4959 tree new_fn = OVL_CURRENT (tmp);
4961 /* [namespace.udecl]
4963 If a function declaration in namespace scope or block
4964 scope has the same name and the same parameter types as a
4965 function introduced by a using declaration the program is
4966 ill-formed. */
4967 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4969 tree old_fn = OVL_CURRENT (tmp1);
4971 if (new_fn == old_fn)
4972 /* The function already exists in the current namespace. */
4973 break;
4974 else if (OVL_USED (tmp1))
4975 continue; /* this is a using decl */
4976 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4977 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4979 /* If this using declaration introduces a function
4980 recognized as a built-in, no longer mark it as
4981 anticipated in this scope. */
4982 if (DECL_ANTICIPATED (old_fn))
4984 DECL_ANTICIPATED (old_fn) = 0;
4985 break;
4988 /* There was already a non-using declaration in
4989 this scope with the same parameter types. If both
4990 are the same extern "C" functions, that's ok. */
4991 if (!decls_match (new_fn, old_fn))
4992 error ("`%D' is already declared in this scope", name);
4993 break;
4997 /* If we broke out of the loop, there's no reason to add
4998 this function to the using declarations for this
4999 scope. */
5000 if (tmp1)
5001 continue;
5003 *newval = build_overload (OVL_CURRENT (tmp), *newval);
5004 if (TREE_CODE (*newval) != OVERLOAD)
5005 *newval = ovl_cons (*newval, NULL_TREE);
5006 OVL_USED (*newval) = 1;
5009 else
5011 *newval = BINDING_VALUE (decls);
5012 if (oldval)
5013 duplicate_decls (*newval, oldval);
5016 *newtype = BINDING_TYPE (decls);
5017 if (oldtype && *newtype && oldtype != *newtype)
5019 error ("using declaration `%D' introduced ambiguous type `%T'",
5020 name, oldtype);
5021 return;
5025 /* Process a using-declaration not appearing in class or local scope. */
5027 void
5028 do_toplevel_using_decl (decl)
5029 tree decl;
5031 tree scope, name, binding;
5032 tree oldval, oldtype, newval, newtype;
5034 decl = validate_nonmember_using_decl (decl, &scope, &name);
5035 if (decl == NULL_TREE)
5036 return;
5038 binding = binding_for_name (name, current_namespace);
5040 oldval = BINDING_VALUE (binding);
5041 oldtype = BINDING_TYPE (binding);
5043 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5045 /* Copy declarations found. */
5046 if (newval)
5047 BINDING_VALUE (binding) = newval;
5048 if (newtype)
5049 BINDING_TYPE (binding) = newtype;
5050 return;
5053 /* Process a using-declaration at function scope. */
5055 void
5056 do_local_using_decl (decl)
5057 tree decl;
5059 tree scope, name;
5060 tree oldval, oldtype, newval, newtype;
5062 decl = validate_nonmember_using_decl (decl, &scope, &name);
5063 if (decl == NULL_TREE)
5064 return;
5066 if (building_stmt_tree ()
5067 && at_function_scope_p ())
5068 add_decl_stmt (decl);
5070 oldval = lookup_name_current_level (name);
5071 oldtype = lookup_type_current_level (name);
5073 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5075 if (newval)
5077 if (is_overloaded_fn (newval))
5079 tree fn, term;
5081 /* We only need to push declarations for those functions
5082 that were not already bound in the current level.
5083 The old value might be NULL_TREE, it might be a single
5084 function, or an OVERLOAD. */
5085 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5086 term = OVL_FUNCTION (oldval);
5087 else
5088 term = oldval;
5089 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5090 fn = OVL_NEXT (fn))
5091 push_overloaded_decl (OVL_CURRENT (fn),
5092 PUSH_LOCAL | PUSH_USING);
5094 else
5095 push_local_binding (name, newval, PUSH_USING);
5097 if (newtype)
5098 set_identifier_type_value (name, newtype);
5101 tree
5102 do_class_using_decl (decl)
5103 tree decl;
5105 tree name, value;
5107 if (TREE_CODE (decl) != SCOPE_REF
5108 || !TYPE_P (TREE_OPERAND (decl, 0)))
5110 error ("using-declaration for non-member at class scope");
5111 return NULL_TREE;
5113 name = TREE_OPERAND (decl, 1);
5114 if (TREE_CODE (name) == BIT_NOT_EXPR)
5116 error ("using-declaration for destructor");
5117 return NULL_TREE;
5119 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5121 name = TREE_OPERAND (name, 0);
5122 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
5123 return NULL_TREE;
5125 if (TREE_CODE (name) == TYPE_DECL || TREE_CODE (name) == TEMPLATE_DECL)
5126 name = DECL_NAME (name);
5128 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5130 value = build_lang_decl (USING_DECL, name, void_type_node);
5131 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5132 return value;
5135 /* Process a using-directive. */
5137 void
5138 do_using_directive (namespace)
5139 tree namespace;
5141 if (building_stmt_tree ())
5142 add_stmt (build_stmt (USING_STMT, namespace));
5144 /* using namespace A::B::C; */
5145 if (TREE_CODE (namespace) == SCOPE_REF)
5146 namespace = TREE_OPERAND (namespace, 1);
5147 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5149 /* Lookup in lexer did not find a namespace. */
5150 if (!processing_template_decl)
5151 error ("namespace `%T' undeclared", namespace);
5152 return;
5154 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5156 if (!processing_template_decl)
5157 error ("`%T' is not a namespace", namespace);
5158 return;
5160 namespace = ORIGINAL_NAMESPACE (namespace);
5161 if (!toplevel_bindings_p ())
5162 push_using_directive (namespace);
5163 else
5164 /* direct usage */
5165 add_using_namespace (current_namespace, namespace, 0);
5168 void
5169 check_default_args (x)
5170 tree x;
5172 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5173 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5174 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5176 if (TREE_PURPOSE (arg))
5177 saw_def = 1;
5178 else if (saw_def)
5180 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5181 i, x);
5182 break;
5187 void
5188 mark_used (decl)
5189 tree decl;
5191 TREE_USED (decl) = 1;
5192 if (processing_template_decl)
5193 return;
5194 if (!skip_evaluation)
5195 assemble_external (decl);
5197 /* Is it a synthesized method that needs to be synthesized? */
5198 if (TREE_CODE (decl) == FUNCTION_DECL
5199 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5200 && DECL_ARTIFICIAL (decl)
5201 && ! DECL_INITIAL (decl)
5202 /* Kludge: don't synthesize for default args. */
5203 && current_function_decl)
5205 synthesize_method (decl);
5206 /* If we've already synthesized the method we don't need to
5207 instantiate it, so we can return right away. */
5208 return;
5211 /* If this is a function or variable that is an instance of some
5212 template, we now know that we will need to actually do the
5213 instantiation. We check that DECL is not an explicit
5214 instantiation because that is not checked in instantiate_decl. */
5215 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5216 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5217 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5218 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5219 instantiate_decl (decl, /*defer_ok=*/1);
5222 /* Helper function for class_head_decl and class_head_defn
5223 nonterminals. AGGR is the class, union or struct tag. SCOPE is the
5224 explicit scope used (NULL for no scope resolution). ID is the
5225 name. DEFN_P is true, if this is a definition of the class and
5226 NEW_TYPE_P is set to non-zero, if we push into the scope containing
5227 the to be defined aggregate.
5229 Return a TYPE_DECL for the type declared by ID in SCOPE. */
5231 tree
5232 handle_class_head (aggr, scope, id, defn_p, new_type_p)
5233 tree aggr, scope, id;
5234 int defn_p;
5235 int *new_type_p;
5237 tree decl = NULL_TREE;
5238 tree current = current_scope ();
5239 bool xrefd_p = false;
5241 if (current == NULL_TREE)
5242 current = current_namespace;
5244 *new_type_p = 0;
5246 if (scope)
5248 if (TREE_CODE (id) == TYPE_DECL)
5249 /* We must bash typedefs back to the main decl of the
5250 type. Otherwise we become confused about scopes. */
5251 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5252 else if (DECL_CLASS_TEMPLATE_P (id))
5253 decl = DECL_TEMPLATE_RESULT (id);
5254 else
5256 if (TYPE_P (scope))
5258 /* According to the suggested resolution of core issue
5259 180, 'typename' is assumed after a class-key. */
5260 decl = make_typename_type (scope, id, tf_error);
5261 if (decl != error_mark_node)
5262 decl = TYPE_MAIN_DECL (decl);
5263 else
5264 decl = NULL_TREE;
5266 else if (scope == current)
5268 /* We've been given AGGR SCOPE::ID, when we're already
5269 inside SCOPE. Be nice about it. */
5270 if (pedantic)
5271 pedwarn ("extra qualification `%T::' on member `%D' ignored",
5272 scope, id);
5274 else
5275 error ("`%T' does not have a class or union named `%D'",
5276 scope, id);
5280 if (!decl)
5282 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, !defn_p));
5283 xrefd_p = true;
5286 if (!TYPE_BINFO (TREE_TYPE (decl)))
5288 error ("`%T' is not a class or union type", decl);
5289 return error_mark_node;
5292 if (defn_p)
5294 /* For a definition, we want to enter the containing scope
5295 before looking up any base classes etc. Only do so, if this
5296 is different to the current scope. */
5297 tree context = CP_DECL_CONTEXT (decl);
5299 *new_type_p = (current != context
5300 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
5301 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
5302 if (*new_type_p)
5303 push_scope (context);
5305 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5306 /* It is legal to define a class with a different class key,
5307 and this changes the default member access. */
5308 CLASSTYPE_DECLARED_CLASS (TREE_TYPE (decl))
5309 = aggr == class_type_node;
5311 if (!xrefd_p && PROCESSING_REAL_TEMPLATE_DECL_P ())
5312 decl = push_template_decl (decl);
5315 return decl;
5318 /* Initialize decl2.c. */
5320 void
5321 init_decl2 ()
5323 ggc_add_tree_varray_root (&deferred_fns, 1);
5324 ggc_add_tree_varray_root (&pending_statics, 1);
5325 ggc_add_tree_varray_root (&ssdf_decls, 1);
5326 ggc_add_tree_root (&ssdf_decl, 1);
5327 ggc_add_tree_root (&priority_decl, 1);
5328 ggc_add_tree_root (&initialize_p_decl, 1);