PR c++/3637
[official-gcc.git] / gcc / cp / decl2.c
blobf2bbba09f2c199690826097a3b13440696eab243
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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 implicit declarations. */
177 int warn_implicit = 1;
179 /* Nonzero means warn about usage of long long when `-pedantic'. */
181 int warn_long_long = 1;
183 /* Nonzero means warn when all ctors or dtors are private, and the class
184 has no friends. */
186 int warn_ctor_dtor_privacy = 1;
188 /* Nonzero means generate separate instantiation control files and juggle
189 them at link time. */
191 int flag_use_repository;
193 /* Nonzero if we want to issue diagnostics that the standard says are not
194 required. */
196 int flag_optional_diags = 1;
198 /* Nonzero means give string constants the type `const char *', as mandated
199 by the standard. */
201 int flag_const_strings = 1;
203 /* Nonzero means warn about deprecated conversion from string constant to
204 `char *'. */
206 int warn_write_strings;
208 /* Nonzero means warn about pointer casts that can drop a type qualifier
209 from the pointer target type. */
211 int warn_cast_qual;
213 /* Nonzero means warn about sizeof(function) or addition/subtraction
214 of function pointers. */
216 int warn_pointer_arith = 1;
218 /* Nonzero means warn for any function def without prototype decl. */
220 int warn_missing_prototypes;
222 /* Nonzero means warn about multiple (redundant) decls for the same single
223 variable or function. */
225 int warn_redundant_decls;
227 /* Warn if initializer is not completely bracketed. */
229 int warn_missing_braces;
231 /* Warn about comparison of signed and unsigned values. */
233 int warn_sign_compare;
235 /* Warn about testing equality of floating point numbers. */
237 int warn_float_equal = 0;
239 /* Warn about functions which might be candidates for format attributes. */
241 int warn_missing_format_attribute;
243 /* Warn about a subscript that has type char. */
245 int warn_char_subscripts;
247 /* Warn if a type conversion is done that might have confusing results. */
249 int warn_conversion;
251 /* Warn if adding () is suggested. */
253 int warn_parentheses;
255 /* Non-zero means warn in function declared in derived class has the
256 same name as a virtual in the base class, but fails to match the
257 type signature of any virtual function in the base class. */
259 int warn_overloaded_virtual;
261 /* Non-zero means warn when declaring a class that has a non virtual
262 destructor, when it really ought to have a virtual one. */
264 int warn_nonvdtor;
266 /* Non-zero means warn when the compiler will reorder code. */
268 int warn_reorder;
270 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
272 int warn_synth;
274 /* Non-zero means warn when we convert a pointer to member function
275 into a pointer to (void or function). */
277 int warn_pmf2ptr = 1;
279 /* Nonzero means warn about violation of some Effective C++ style rules. */
281 int warn_ecpp;
283 /* Nonzero means warn where overload resolution chooses a promotion from
284 unsigned to signed over a conversion to an unsigned of the same size. */
286 int warn_sign_promo;
288 /* Nonzero means warn when an old-style cast is used. */
290 int warn_old_style_cast;
292 /* Warn about #pragma directives that are not recognised. */
294 int warn_unknown_pragmas; /* Tri state variable. */
296 /* Nonzero means warn about use of multicharacter literals. */
298 int warn_multichar = 1;
300 /* Nonzero means warn when non-templatized friend functions are
301 declared within a template */
303 int warn_nontemplate_friend = 1;
305 /* Nonzero means complain about deprecated features. */
307 int warn_deprecated = 1;
309 /* Nonzero means `$' can be in an identifier. */
311 #ifndef DOLLARS_IN_IDENTIFIERS
312 #define DOLLARS_IN_IDENTIFIERS 1
313 #endif
314 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
316 /* Nonzero means allow Microsoft extensions without a pedwarn. */
318 int flag_ms_extensions;
320 /* C++ specific flags. */
322 /* Nonzero means we should attempt to elide constructors when possible. */
324 int flag_elide_constructors = 1;
326 /* Nonzero means that member functions defined in class scope are
327 inline by default. */
329 int flag_default_inline = 1;
331 /* Controls whether compiler generates 'type descriptor' that give
332 run-time type information. */
334 int flag_rtti = 1;
336 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
337 objects. */
339 int flag_huge_objects;
341 /* Nonzero if we want to conserve space in the .o files. We do this
342 by putting uninitialized data and runtime initialized data into
343 .common instead of .data at the expense of not flagging multiple
344 definitions. */
346 int flag_conserve_space;
348 /* Nonzero if we want to obey access control semantics. */
350 int flag_access_control = 1;
352 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
354 int flag_operator_names = 1;
356 /* Nonzero if we want to check the return value of new and avoid calling
357 constructors if it is a null pointer. */
359 int flag_check_new;
361 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
362 initialization variables.
363 0: Old rules, set by -fno-for-scope.
364 2: New ISO rules, set by -ffor-scope.
365 1: Try to implement new ISO rules, but with backup compatibility
366 (and warnings). This is the default, for now. */
368 int flag_new_for_scope = 1;
370 /* Nonzero if we want to emit defined symbols with common-like linkage as
371 weak symbols where possible, in order to conform to C++ semantics.
372 Otherwise, emit them as local symbols. */
374 int flag_weak = 1;
376 /* Nonzero to use __cxa_atexit, rather than atexit, to register
377 destructors for local statics and global objects. */
379 int flag_use_cxa_atexit;
381 /* Maximum template instantiation depth. This limit is rather
382 arbitrary, but it exists to limit the time it takes to notice
383 infinite template instantiations. */
385 int max_tinst_depth = 50;
387 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
389 int flag_vtable_gc;
391 /* Nonzero means make the default pedwarns warnings instead of errors.
392 The value of this flag is ignored if -pedantic is specified. */
394 int flag_permissive;
396 /* Nonzero means to implement standard semantics for exception
397 specifications, calling unexpected if an exception is thrown that
398 doesn't match the specification. Zero means to treat them as
399 assertions and optimize accordingly, but not check them. */
401 int flag_enforce_eh_specs = 1;
403 /* The variant of the C language being processed. */
405 c_language_kind c_language = clk_cplusplus;
407 /* Table of language-dependent -f options.
408 STRING is the option name. VARIABLE is the address of the variable.
409 ON_VALUE is the value to store in VARIABLE
410 if `-fSTRING' is seen as an option.
411 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
413 static struct { const char *const string; int *variable; int on_value;}
414 lang_f_options[] =
416 /* C/C++ options. */
417 {"signed-char", &flag_signed_char, 1},
418 {"unsigned-char", &flag_signed_char, 0},
419 {"signed-bitfields", &flag_signed_bitfields, 1},
420 {"unsigned-bitfields", &flag_signed_bitfields, 0},
421 {"short-enums", &flag_short_enums, 1},
422 {"short-double", &flag_short_double, 1},
423 {"short-wchar", &flag_short_wchar, 1},
424 {"asm", &flag_no_asm, 0},
425 {"builtin", &flag_no_builtin, 0},
427 /* C++-only options. */
428 {"access-control", &flag_access_control, 1},
429 {"check-new", &flag_check_new, 1},
430 {"conserve-space", &flag_conserve_space, 1},
431 {"const-strings", &flag_const_strings, 1},
432 {"default-inline", &flag_default_inline, 1},
433 {"dollars-in-identifiers", &dollars_in_ident, 1},
434 {"elide-constructors", &flag_elide_constructors, 1},
435 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
436 {"external-templates", &flag_external_templates, 1},
437 {"for-scope", &flag_new_for_scope, 2},
438 {"gnu-keywords", &flag_no_gnu_keywords, 0},
439 {"handle-exceptions", &flag_exceptions, 1},
440 {"implement-inlines", &flag_implement_inlines, 1},
441 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
442 {"implicit-templates", &flag_implicit_templates, 1},
443 {"ms-extensions", &flag_ms_extensions, 1},
444 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
445 {"operator-names", &flag_operator_names, 1},
446 {"optional-diags", &flag_optional_diags, 1},
447 {"permissive", &flag_permissive, 1},
448 {"repo", &flag_use_repository, 1},
449 {"rtti", &flag_rtti, 1},
450 {"stats", &flag_detailed_statistics, 1},
451 {"vtable-gc", &flag_vtable_gc, 1},
452 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
453 {"weak", &flag_weak, 1}
456 /* The list of `-f' options that we no longer support. The `-f'
457 prefix is not given in this table. The `-fno-' variants are not
458 listed here. This table must be kept in alphabetical order. */
459 static const char * const unsupported_options[] = {
460 "all-virtual",
461 "cond-mismatch",
462 "enum-int-equiv",
463 "guiding-decls",
464 "honor-std",
465 "huge-objects",
466 "labels-ok",
467 "new-abi",
468 "nonnull-objects",
469 "squangle",
470 "strict-prototype",
471 "this-is-variable",
472 "vtable-thunks",
473 "xref"
476 /* Compare two option strings, pointed two by P1 and P2, for use with
477 bsearch. */
479 static int
480 compare_options (p1, p2)
481 const PTR p1;
482 const PTR p2;
484 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
487 /* Decode the string P as a language-specific option.
488 Return the number of strings consumed for a valid option.
489 Otherwise return 0. Should not complain if it does not
490 recognise the option. */
492 int
493 cxx_decode_option (argc, argv)
494 int argc;
495 char **argv;
497 int strings_processed;
498 const char *p = argv[0];
500 strings_processed = cpp_handle_option (parse_in, argc, argv);
502 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
503 /* ignore */;
504 else if (p[0] == '-' && p[1] == 'f')
506 /* Some kind of -f option.
507 P's value is the option sans `-f'.
508 Search for it in the table of options. */
509 const char *option_value = NULL;
510 const char *positive_option;
511 size_t j;
513 p += 2;
514 /* Try special -f options. */
516 /* See if this is one of the options no longer supported. We
517 used to support these options, so we continue to accept them,
518 with a warning. */
519 if (strncmp (p, "no-", strlen ("no-")) == 0)
520 positive_option = p + strlen ("no-");
521 else
522 positive_option = p;
524 /* If the option is present, issue a warning. Indicate to our
525 caller that the option was processed successfully. */
526 if (bsearch (&positive_option,
527 unsupported_options,
528 (sizeof (unsupported_options)
529 / sizeof (unsupported_options[0])),
530 sizeof (unsupported_options[0]),
531 compare_options))
533 warning ("-f%s is no longer supported", p);
534 return 1;
537 if (!strcmp (p, "handle-exceptions")
538 || !strcmp (p, "no-handle-exceptions"))
539 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
540 else if (! strcmp (p, "alt-external-templates"))
542 flag_external_templates = 1;
543 flag_alt_external_templates = 1;
544 cp_deprecated ("-falt-external-templates");
546 else if (! strcmp (p, "no-alt-external-templates"))
547 flag_alt_external_templates = 0;
548 else if (!strcmp (p, "repo"))
550 flag_use_repository = 1;
551 flag_implicit_templates = 0;
553 else if (!strcmp (p, "external-templates"))
555 flag_external_templates = 1;
556 cp_deprecated ("-fexternal-templates");
558 else if ((option_value
559 = skip_leading_substring (p, "template-depth-")))
560 max_tinst_depth
561 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
562 else if ((option_value
563 = skip_leading_substring (p, "name-mangling-version-")))
565 warning ("-fname-mangling-version is no longer supported");
566 return 1;
568 else if (dump_switch_p (p))
570 else
572 int found = 0;
574 for (j = 0;
575 !found && j < (sizeof (lang_f_options)
576 / sizeof (lang_f_options[0]));
577 j++)
579 if (!strcmp (p, lang_f_options[j].string))
581 *lang_f_options[j].variable = lang_f_options[j].on_value;
582 /* A goto here would be cleaner,
583 but breaks the VAX pcc. */
584 found = 1;
586 else if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
587 && ! strcmp (p+3, lang_f_options[j].string))
589 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
590 found = 1;
594 return found;
597 else if (p[0] == '-' && p[1] == 'W')
599 int setting = 1;
601 /* The -W options control the warning behavior of the compiler. */
602 p += 2;
604 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
605 setting = 0, p += 3;
607 if (!strcmp (p, "implicit"))
608 warn_implicit = setting;
609 else if (!strcmp (p, "long-long"))
610 warn_long_long = setting;
611 else if (!strcmp (p, "return-type"))
612 warn_return_type = setting;
613 else if (!strcmp (p, "ctor-dtor-privacy"))
614 warn_ctor_dtor_privacy = setting;
615 else if (!strcmp (p, "write-strings"))
616 warn_write_strings = setting;
617 else if (!strcmp (p, "cast-qual"))
618 warn_cast_qual = setting;
619 else if (!strcmp (p, "char-subscripts"))
620 warn_char_subscripts = setting;
621 else if (!strcmp (p, "pointer-arith"))
622 warn_pointer_arith = setting;
623 else if (!strcmp (p, "missing-prototypes"))
624 warn_missing_prototypes = setting;
625 else if (!strcmp (p, "strict-prototypes"))
627 if (setting == 0)
628 warning ("-Wno-strict-prototypes is not supported in C++");
630 else if (!strcmp (p, "redundant-decls"))
631 warn_redundant_decls = setting;
632 else if (!strcmp (p, "missing-braces"))
633 warn_missing_braces = setting;
634 else if (!strcmp (p, "sign-compare"))
635 warn_sign_compare = setting;
636 else if (!strcmp (p, "float-equal"))
637 warn_float_equal = setting;
638 else if (!strcmp (p, "format"))
639 set_Wformat (setting);
640 else if (!strcmp (p, "format=2"))
641 set_Wformat (2);
642 else if (!strcmp (p, "format-y2k"))
643 warn_format_y2k = setting;
644 else if (!strcmp (p, "format-extra-args"))
645 warn_format_extra_args = setting;
646 else if (!strcmp (p, "format-nonliteral"))
647 warn_format_nonliteral = setting;
648 else if (!strcmp (p, "format-security"))
649 warn_format_security = setting;
650 else if (!strcmp (p, "missing-format-attribute"))
651 warn_missing_format_attribute = setting;
652 else if (!strcmp (p, "conversion"))
653 warn_conversion = setting;
654 else if (!strcmp (p, "parentheses"))
655 warn_parentheses = setting;
656 else if (!strcmp (p, "non-virtual-dtor"))
657 warn_nonvdtor = setting;
658 else if (!strcmp (p, "reorder"))
659 warn_reorder = setting;
660 else if (!strcmp (p, "synth"))
661 warn_synth = setting;
662 else if (!strcmp (p, "pmf-conversions"))
663 warn_pmf2ptr = setting;
664 else if (!strcmp (p, "effc++"))
665 warn_ecpp = setting;
666 else if (!strcmp (p, "sign-promo"))
667 warn_sign_promo = setting;
668 else if (!strcmp (p, "old-style-cast"))
669 warn_old_style_cast = setting;
670 else if (!strcmp (p, "overloaded-virtual"))
671 warn_overloaded_virtual = setting;
672 else if (!strcmp (p, "multichar"))
673 warn_multichar = setting;
674 else if (!strcmp (p, "unknown-pragmas"))
675 /* Set to greater than 1, so that even unknown pragmas in
676 system headers will be warned about. */
677 warn_unknown_pragmas = setting * 2;
678 else if (!strcmp (p, "non-template-friend"))
679 warn_nontemplate_friend = setting;
680 else if (!strcmp (p, "deprecated"))
681 warn_deprecated = setting;
682 else if (!strcmp (p, "comment"))
683 ; /* cpp handles this one. */
684 else if (!strcmp (p, "comments"))
685 ; /* cpp handles this one. */
686 else if (!strcmp (p, "trigraphs"))
687 ; /* cpp handles this one. */
688 else if (!strcmp (p, "import"))
689 ; /* cpp handles this one. */
690 else if (!strcmp (p, "all"))
692 warn_return_type = setting;
693 set_Wunused (setting);
694 warn_implicit = setting;
695 warn_switch = setting;
696 set_Wformat (setting);
697 warn_parentheses = setting;
698 warn_missing_braces = setting;
699 warn_sign_compare = setting;
700 warn_multichar = setting;
701 /* We save the value of warn_uninitialized, since if they put
702 -Wuninitialized on the command line, we need to generate a
703 warning about not using it without also specifying -O. */
704 if (warn_uninitialized != 1)
705 warn_uninitialized = (setting ? 2 : 0);
706 /* Only warn about unknown pragmas that are not in system
707 headers. */
708 warn_unknown_pragmas = 1;
710 /* C++-specific warnings. */
711 warn_ctor_dtor_privacy = setting;
712 warn_nonvdtor = setting;
713 warn_reorder = setting;
714 warn_nontemplate_friend = setting;
716 else return strings_processed;
718 else if (!strcmp (p, "-ansi"))
719 flag_no_nonansi_builtin = 1, flag_ansi = 1,
720 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
721 #ifdef SPEW_DEBUG
722 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
723 it's probably safe to assume no sane person would ever want to use this
724 under normal circumstances. */
725 else if (!strcmp (p, "-spew-debug"))
726 spew_debug = 1;
727 #endif
728 else
729 return strings_processed;
731 return 1;
734 /* Incorporate `const' and `volatile' qualifiers for member functions.
735 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
736 QUALS is a list of qualifiers. Returns any explicit
737 top-level qualifiers of the method's this pointer, anything other than
738 TYPE_UNQUALIFIED will be an extension. */
741 grok_method_quals (ctype, function, quals)
742 tree ctype, function, quals;
744 tree fntype = TREE_TYPE (function);
745 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
746 int type_quals = TYPE_UNQUALIFIED;
747 int dup_quals = TYPE_UNQUALIFIED;
748 int this_quals = TYPE_UNQUALIFIED;
752 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
754 if ((type_quals | this_quals) & tq)
755 dup_quals |= tq;
756 else if (tq & TYPE_QUAL_RESTRICT)
757 this_quals |= tq;
758 else
759 type_quals |= tq;
760 quals = TREE_CHAIN (quals);
762 while (quals);
764 if (dup_quals != TYPE_UNQUALIFIED)
765 cp_error ("duplicate type qualifiers in %s declaration",
766 TREE_CODE (function) == FUNCTION_DECL
767 ? "member function" : "type");
769 ctype = cp_build_qualified_type (ctype, type_quals);
770 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
771 (TREE_CODE (fntype) == METHOD_TYPE
772 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
773 : TYPE_ARG_TYPES (fntype)));
774 if (raises)
775 fntype = build_exception_variant (fntype, raises);
777 TREE_TYPE (function) = fntype;
778 return this_quals;
781 /* Warn when -fexternal-templates is used and #pragma
782 interface/implementation is not used all the times it should be,
783 inform the user. */
785 void
786 warn_if_unknown_interface (decl)
787 tree decl;
789 static int already_warned = 0;
790 if (already_warned++)
791 return;
793 if (flag_alt_external_templates)
795 tree til = tinst_for_decl ();
796 int sl = lineno;
797 const char *sf = input_filename;
799 if (til)
801 lineno = TINST_LINE (til);
802 input_filename = TINST_FILE (til);
804 cp_warning ("template `%#D' instantiated in file without #pragma interface",
805 decl);
806 lineno = sl;
807 input_filename = sf;
809 else
810 cp_warning_at ("template `%#D' defined in file without #pragma interface",
811 decl);
814 /* A subroutine of the parser, to handle a component list. */
816 void
817 grok_x_components (specs)
818 tree specs;
820 tree t;
822 specs = strip_attrs (specs);
824 check_tag_decl (specs);
825 t = groktypename (build_tree_list (specs, NULL_TREE));
827 /* The only case where we need to do anything additional here is an
828 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
829 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
830 return;
832 fixup_anonymous_aggr (t);
833 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
836 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
837 indicated NAME. */
839 tree
840 build_artificial_parm (name, type)
841 tree name;
842 tree type;
844 tree parm;
846 parm = build_decl (PARM_DECL, name, type);
847 DECL_ARTIFICIAL (parm) = 1;
848 /* All our artificial parms are implicitly `const'; they cannot be
849 assigned to. */
850 TREE_READONLY (parm) = 1;
851 DECL_ARG_TYPE (parm) = type;
852 return parm;
855 /* Constructors for types with virtual baseclasses need an "in-charge" flag
856 saying whether this constructor is responsible for initialization of
857 virtual baseclasses or not. All destructors also need this "in-charge"
858 flag, which additionally determines whether or not the destructor should
859 free the memory for the object.
861 This function adds the "in-charge" flag to member function FN if
862 appropriate. It is called from grokclassfn and tsubst.
863 FN must be either a constructor or destructor.
865 The in-charge flag follows the 'this' parameter, and is followed by the
866 VTT parm (if any), then the user-written parms. */
868 void
869 maybe_retrofit_in_chrg (fn)
870 tree fn;
872 tree basetype, arg_types, parms, parm, fntype;
874 /* If we've already add the in-charge parameter don't do it again. */
875 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
876 return;
878 /* When processing templates we can't know, in general, whether or
879 not we're going to have virtual baseclasses. */
880 if (uses_template_parms (fn))
881 return;
883 /* We don't need an in-charge parameter for constructors that don't
884 have virtual bases. */
885 if (DECL_CONSTRUCTOR_P (fn)
886 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
887 return;
889 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
890 basetype = TREE_TYPE (TREE_VALUE (arg_types));
891 arg_types = TREE_CHAIN (arg_types);
893 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
895 /* If this is a subobject constructor or destructor, our caller will
896 pass us a pointer to our VTT. */
897 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
899 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
901 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
902 TREE_CHAIN (parm) = parms;
903 parms = parm;
905 /* ...and then to TYPE_ARG_TYPES. */
906 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
908 DECL_HAS_VTT_PARM_P (fn) = 1;
911 /* Then add the in-charge parm (before the VTT parm). */
912 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
913 TREE_CHAIN (parm) = parms;
914 parms = parm;
915 arg_types = hash_tree_chain (integer_type_node, arg_types);
917 /* Insert our new parameter(s) into the list. */
918 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
920 /* And rebuild the function type. */
921 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
922 arg_types);
923 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
924 fntype = build_exception_variant (fntype,
925 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
926 TREE_TYPE (fn) = fntype;
928 /* Now we've got the in-charge parameter. */
929 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
932 /* Classes overload their constituent function names automatically.
933 When a function name is declared in a record structure,
934 its name is changed to it overloaded name. Since names for
935 constructors and destructors can conflict, we place a leading
936 '$' for destructors.
938 CNAME is the name of the class we are grokking for.
940 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
942 FLAGS contains bits saying what's special about today's
943 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
945 If FUNCTION is a destructor, then we must add the `auto-delete' field
946 as a second parameter. There is some hair associated with the fact
947 that we must "declare" this variable in the manner consistent with the
948 way the rest of the arguments were declared.
950 QUALS are the qualifiers for the this pointer. */
952 void
953 grokclassfn (ctype, function, flags, quals)
954 tree ctype, function;
955 enum overload_flags flags;
956 tree quals;
958 tree fn_name = DECL_NAME (function);
959 int this_quals = TYPE_UNQUALIFIED;
961 /* Even within an `extern "C"' block, members get C++ linkage. See
962 [dcl.link] for details. */
963 SET_DECL_LANGUAGE (function, lang_cplusplus);
965 if (fn_name == NULL_TREE)
967 error ("name missing for member function");
968 fn_name = get_identifier ("<anonymous>");
969 DECL_NAME (function) = fn_name;
972 if (quals)
973 this_quals = grok_method_quals (ctype, function, quals);
975 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
977 /* Must add the class instance variable up front. */
978 /* Right now we just make this a pointer. But later
979 we may wish to make it special. */
980 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
981 tree qual_type;
982 tree parm;
984 /* The `this' parameter is implicitly `const'; it cannot be
985 assigned to. */
986 this_quals |= TYPE_QUAL_CONST;
987 qual_type = cp_build_qualified_type (type, this_quals);
988 parm = build_artificial_parm (this_identifier, qual_type);
989 c_apply_type_quals_to_decl (this_quals, parm);
991 /* We can make this a register, so long as we don't
992 accidentally complain if someone tries to take its address. */
993 DECL_REGISTER (parm) = 1;
994 TREE_CHAIN (parm) = last_function_parms;
995 last_function_parms = parm;
998 DECL_ARGUMENTS (function) = last_function_parms;
999 DECL_CONTEXT (function) = ctype;
1001 if (flags == DTOR_FLAG)
1002 DECL_DESTRUCTOR_P (function) = 1;
1004 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1005 maybe_retrofit_in_chrg (function);
1007 if (flags == DTOR_FLAG)
1009 DECL_DESTRUCTOR_P (function) = 1;
1010 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1014 /* Create an ARRAY_REF, checking for the user doing things backwards
1015 along the way. */
1017 tree
1018 grok_array_decl (array_expr, index_exp)
1019 tree array_expr, index_exp;
1021 tree type = TREE_TYPE (array_expr);
1022 tree p1, p2, i1, i2;
1024 if (type == error_mark_node || index_exp == error_mark_node)
1025 return error_mark_node;
1026 if (processing_template_decl)
1027 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1028 array_expr, index_exp);
1030 if (type == NULL_TREE)
1032 /* Something has gone very wrong. Assume we are mistakenly reducing
1033 an expression instead of a declaration. */
1034 error ("parser may be lost: is there a '{' missing somewhere?");
1035 return NULL_TREE;
1038 if (TREE_CODE (type) == OFFSET_TYPE
1039 || TREE_CODE (type) == REFERENCE_TYPE)
1040 type = TREE_TYPE (type);
1042 /* If they have an `operator[]', use that. */
1043 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1044 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1045 array_expr, index_exp, NULL_TREE);
1047 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1048 is a little-known fact that, if `a' is an array and `i' is an
1049 int, you can write `i[a]', which means the same thing as `a[i]'. */
1051 if (TREE_CODE (type) == ARRAY_TYPE)
1052 p1 = array_expr;
1053 else
1054 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1056 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1057 p2 = index_exp;
1058 else
1059 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1061 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1062 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1064 if ((p1 && i2) && (i1 && p2))
1065 error ("ambiguous conversion for array subscript");
1067 if (p1 && i2)
1068 array_expr = p1, index_exp = i2;
1069 else if (i1 && p2)
1070 array_expr = p2, index_exp = i1;
1071 else
1073 cp_error ("invalid types `%T[%T]' for array subscript",
1074 type, TREE_TYPE (index_exp));
1075 return error_mark_node;
1078 if (array_expr == error_mark_node || index_exp == error_mark_node)
1079 error ("ambiguous conversion for array subscript");
1081 return build_array_ref (array_expr, index_exp);
1084 /* Given the cast expression EXP, checking out its validity. Either return
1085 an error_mark_node if there was an unavoidable error, return a cast to
1086 void for trying to delete a pointer w/ the value 0, or return the
1087 call to delete. If DOING_VEC is 1, we handle things differently
1088 for doing an array delete. If DOING_VEC is 2, they gave us the
1089 array size as an argument to delete.
1090 Implements ARM $5.3.4. This is called from the parser. */
1092 tree
1093 delete_sanity (exp, size, doing_vec, use_global_delete)
1094 tree exp, size;
1095 int doing_vec, use_global_delete;
1097 tree t, type;
1098 /* For a regular vector delete (aka, no size argument) we will pass
1099 this down as a NULL_TREE into build_vec_delete. */
1100 tree maxindex = NULL_TREE;
1102 if (exp == error_mark_node)
1103 return exp;
1105 if (processing_template_decl)
1107 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1108 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1109 DELETE_EXPR_USE_VEC (t) = doing_vec;
1110 return t;
1113 if (TREE_CODE (exp) == OFFSET_REF)
1114 exp = resolve_offset_ref (exp);
1115 exp = convert_from_reference (exp);
1116 t = stabilize_reference (exp);
1117 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1119 if (t == NULL_TREE || t == error_mark_node)
1121 cp_error ("type `%#T' argument given to `delete', expected pointer",
1122 TREE_TYPE (exp));
1123 return error_mark_node;
1126 if (doing_vec == 2)
1128 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1129 pedwarn ("anachronistic use of array size in vector delete");
1132 type = TREE_TYPE (t);
1134 /* As of Valley Forge, you can delete a pointer to const. */
1136 /* You can't delete functions. */
1137 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1139 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1140 return error_mark_node;
1143 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1144 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1146 cp_warning ("deleting `%T' is undefined", type);
1147 doing_vec = 0;
1150 /* An array can't have been allocated by new, so complain. */
1151 if (TREE_CODE (t) == ADDR_EXPR
1152 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1153 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1154 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1156 /* Deleting a pointer with the value zero is valid and has no effect. */
1157 if (integer_zerop (t))
1158 return build1 (NOP_EXPR, void_type_node, t);
1160 if (doing_vec)
1161 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1162 use_global_delete);
1163 else
1165 if (IS_AGGR_TYPE (TREE_TYPE (type))
1166 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1168 /* Only do access checking here; we'll be calling op delete
1169 from the destructor. */
1170 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1171 LOOKUP_NORMAL, NULL_TREE);
1172 if (tmp == error_mark_node)
1173 return error_mark_node;
1176 return build_delete (type, t, sfk_deleting_destructor,
1177 LOOKUP_NORMAL, use_global_delete);
1181 /* Report an error if the indicated template declaration is not the
1182 sort of thing that should be a member template. */
1184 void
1185 check_member_template (tmpl)
1186 tree tmpl;
1188 tree decl;
1190 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1191 decl = DECL_TEMPLATE_RESULT (tmpl);
1193 if (TREE_CODE (decl) == FUNCTION_DECL
1194 || (TREE_CODE (decl) == TYPE_DECL
1195 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1197 if (current_function_decl)
1198 /* 14.5.2.2 [temp.mem]
1200 A local class shall not have member templates. */
1201 cp_error ("invalid declaration of member template `%#D' in local class",
1202 decl);
1204 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1206 /* 14.5.2.3 [temp.mem]
1208 A member function template shall not be virtual. */
1209 cp_error
1210 ("invalid use of `virtual' in template declaration of `%#D'",
1211 decl);
1212 DECL_VIRTUAL_P (decl) = 0;
1215 /* The debug-information generating code doesn't know what to do
1216 with member templates. */
1217 DECL_IGNORED_P (tmpl) = 1;
1219 else
1220 cp_error ("template declaration of `%#D'", decl);
1223 /* Return true iff TYPE is a valid Java parameter or return type. */
1225 static int
1226 acceptable_java_type (type)
1227 tree type;
1229 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1230 return 1;
1231 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1233 type = TREE_TYPE (type);
1234 if (TREE_CODE (type) == RECORD_TYPE)
1236 tree args; int i;
1237 if (! TYPE_FOR_JAVA (type))
1238 return 0;
1239 if (! CLASSTYPE_TEMPLATE_INFO (type))
1240 return 1;
1241 args = CLASSTYPE_TI_ARGS (type);
1242 i = TREE_VEC_LENGTH (args);
1243 while (--i >= 0)
1245 type = TREE_VEC_ELT (args, i);
1246 if (TREE_CODE (type) == POINTER_TYPE)
1247 type = TREE_TYPE (type);
1248 if (! TYPE_FOR_JAVA (type))
1249 return 0;
1251 return 1;
1254 return 0;
1257 /* For a METHOD in a Java class CTYPE, return 1 if
1258 the parameter and return types are valid Java types.
1259 Otherwise, print appropriate error messages, and return 0. */
1262 check_java_method (method)
1263 tree method;
1265 int jerr = 0;
1266 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1267 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1268 if (! acceptable_java_type (ret_type))
1270 cp_error ("Java method '%D' has non-Java return type `%T'",
1271 method, ret_type);
1272 jerr++;
1274 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1276 tree type = TREE_VALUE (arg_types);
1277 if (! acceptable_java_type (type))
1279 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1280 method, type);
1281 jerr++;
1284 return jerr ? 0 : 1;
1287 /* Sanity check: report error if this function FUNCTION is not
1288 really a member of the class (CTYPE) it is supposed to belong to.
1289 CNAME is the same here as it is for grokclassfn above. */
1291 tree
1292 check_classfn (ctype, function)
1293 tree ctype, function;
1295 tree fn_name = DECL_NAME (function);
1296 tree fndecl, fndecls;
1297 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1298 tree *methods = 0;
1299 tree *end = 0;
1301 if (DECL_USE_TEMPLATE (function)
1302 && !(TREE_CODE (function) == TEMPLATE_DECL
1303 && DECL_TEMPLATE_SPECIALIZATION (function))
1304 && is_member_template (DECL_TI_TEMPLATE (function)))
1305 /* Since this is a specialization of a member template,
1306 we're not going to find the declaration in the class.
1307 For example, in:
1309 struct S { template <typename T> void f(T); };
1310 template <> void S::f(int);
1312 we're not going to find `S::f(int)', but there's no
1313 reason we should, either. We let our callers know we didn't
1314 find the method, but we don't complain. */
1315 return NULL_TREE;
1317 if (method_vec != 0)
1319 methods = &TREE_VEC_ELT (method_vec, 0);
1320 end = TREE_VEC_END (method_vec);
1322 /* First suss out ctors and dtors. */
1323 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1324 && DECL_CONSTRUCTOR_P (function))
1325 goto got_it;
1326 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1327 && DECL_DESTRUCTOR_P (function))
1328 goto got_it;
1330 while (++methods != end && *methods)
1332 fndecl = *methods;
1333 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1335 got_it:
1336 for (fndecls = *methods; fndecls != NULL_TREE;
1337 fndecls = OVL_NEXT (fndecls))
1339 fndecl = OVL_CURRENT (fndecls);
1341 /* We cannot simply call decls_match because this
1342 doesn't work for static member functions that are
1343 pretending to be methods, and because the name
1344 may have been changed by asm("new_name"). */
1345 if (DECL_NAME (function) == DECL_NAME (fndecl))
1347 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1348 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1350 /* Get rid of the this parameter on functions that become
1351 static. */
1352 if (DECL_STATIC_FUNCTION_P (fndecl)
1353 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1354 p1 = TREE_CHAIN (p1);
1356 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1357 TREE_TYPE (TREE_TYPE (fndecl)))
1358 && compparms (p1, p2)
1359 && (DECL_TEMPLATE_SPECIALIZATION (function)
1360 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1361 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1362 || (DECL_TI_TEMPLATE (function)
1363 == DECL_TI_TEMPLATE (fndecl))))
1364 return fndecl;
1367 break; /* loser */
1372 if (methods != end && *methods)
1374 tree fndecl = *methods;
1375 cp_error ("prototype for `%#D' does not match any in class `%T'",
1376 function, ctype);
1377 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1378 OVL_CURRENT (fndecl));
1379 while (fndecl = OVL_NEXT (fndecl), fndecl)
1380 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1382 else
1384 methods = 0;
1385 if (!COMPLETE_TYPE_P (ctype))
1386 incomplete_type_error (function, ctype);
1387 else
1388 cp_error ("no `%#D' member function declared in class `%T'",
1389 function, ctype);
1392 /* If we did not find the method in the class, add it to avoid
1393 spurious errors (unless the CTYPE is not yet defined, in which
1394 case we'll only confuse ourselves when the function is declared
1395 properly within the class. */
1396 if (COMPLETE_TYPE_P (ctype))
1397 add_method (ctype, function, /*error_p=*/1);
1398 return NULL_TREE;
1401 /* We have just processed the DECL, which is a static data member.
1402 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1403 present, is the assembly-language name for the data member.
1404 FLAGS is as for cp_finish_decl. */
1406 void
1407 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1408 tree decl;
1409 tree init;
1410 tree asmspec_tree;
1411 int flags;
1413 my_friendly_assert (TREE_PUBLIC (decl), 0);
1415 DECL_CONTEXT (decl) = current_class_type;
1417 /* We cannot call pushdecl here, because that would fill in the
1418 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1419 the right thing, namely, to put this decl out straight away. */
1420 /* current_class_type can be NULL_TREE in case of error. */
1421 if (!asmspec_tree && current_class_type)
1422 DECL_INITIAL (decl) = error_mark_node;
1424 if (! processing_template_decl)
1426 if (!pending_statics)
1427 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1428 VARRAY_PUSH_TREE (pending_statics, decl);
1431 /* Static consts need not be initialized in the class definition. */
1432 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1434 static int explained = 0;
1436 error ("initializer invalid for static member with constructor");
1437 if (!explained)
1439 error ("(an out of class initialization is required)");
1440 explained = 1;
1442 init = NULL_TREE;
1444 /* Force the compiler to know when an uninitialized static const
1445 member is being used. */
1446 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1447 TREE_USED (decl) = 1;
1448 DECL_INITIAL (decl) = init;
1449 DECL_IN_AGGR_P (decl) = 1;
1451 cp_finish_decl (decl, init, asmspec_tree, flags);
1454 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1455 of a structure component, returning a _DECL node.
1456 QUALS is a list of type qualifiers for this decl (such as for declaring
1457 const member functions).
1459 This is done during the parsing of the struct declaration.
1460 The _DECL nodes are chained together and the lot of them
1461 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1463 If class A defines that certain functions in class B are friends, then
1464 the way I have set things up, it is B who is interested in permission
1465 granted by A. However, it is in A's context that these declarations
1466 are parsed. By returning a void_type_node, class A does not attempt
1467 to incorporate the declarations of the friends within its structure.
1469 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1470 CHANGES TO CODE IN `start_method'. */
1472 tree
1473 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1474 tree declarator, declspecs, init, asmspec_tree, attrlist;
1476 tree value;
1477 const char *asmspec = 0;
1478 int flags = LOOKUP_ONLYCONVERTING;
1480 /* Convert () initializers to = initializers. */
1481 if (init == NULL_TREE && declarator != NULL_TREE
1482 && TREE_CODE (declarator) == CALL_EXPR
1483 && TREE_OPERAND (declarator, 0)
1484 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1485 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1486 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1488 /* It's invalid to try to initialize a data member using a
1489 functional notation, e.g.:
1491 struct S {
1492 static int i (3);
1495 Explain that to the user. */
1496 static int explained;
1498 cp_error ("invalid data member initialization");
1499 if (!explained)
1501 cp_error ("(use `=' to initialize static data members)");
1502 explained = 1;
1505 declarator = TREE_OPERAND (declarator, 0);
1506 flags = 0;
1509 if (declspecs == NULL_TREE
1510 && TREE_CODE (declarator) == SCOPE_REF
1511 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1513 /* Access declaration */
1514 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1516 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1517 pop_nested_class ();
1518 return do_class_using_decl (declarator);
1521 if (init
1522 && TREE_CODE (init) == TREE_LIST
1523 && TREE_VALUE (init) == error_mark_node
1524 && TREE_CHAIN (init) == NULL_TREE)
1525 init = NULL_TREE;
1527 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
1528 if (! value || value == error_mark_node)
1529 /* friend or constructor went bad. */
1530 return value;
1531 if (TREE_TYPE (value) == error_mark_node)
1532 return error_mark_node;
1534 /* Pass friendly classes back. */
1535 if (TREE_CODE (value) == VOID_TYPE)
1536 return void_type_node;
1538 if (DECL_NAME (value) != NULL_TREE
1539 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1540 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1541 cp_error ("member `%D' conflicts with virtual function table field name",
1542 value);
1544 /* Stash away type declarations. */
1545 if (TREE_CODE (value) == TYPE_DECL)
1547 DECL_NONLOCAL (value) = 1;
1548 DECL_CONTEXT (value) = current_class_type;
1550 if (CLASS_TYPE_P (TREE_TYPE (value)))
1551 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1553 if (processing_template_decl)
1554 value = push_template_decl (value);
1556 return value;
1559 if (DECL_IN_AGGR_P (value))
1561 cp_error ("`%D' is already defined in `%T'", value,
1562 DECL_CONTEXT (value));
1563 return void_type_node;
1566 if (asmspec_tree)
1567 asmspec = TREE_STRING_POINTER (asmspec_tree);
1569 if (init)
1571 if (TREE_CODE (value) == FUNCTION_DECL)
1573 grok_function_init (value, init);
1574 init = NULL_TREE;
1576 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1577 /* Already complained in grokdeclarator. */
1578 init = NULL_TREE;
1579 else
1581 /* We allow initializers to become parameters to base
1582 initializers. */
1583 if (TREE_CODE (init) == TREE_LIST)
1585 if (TREE_CHAIN (init) == NULL_TREE)
1586 init = TREE_VALUE (init);
1587 else
1588 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1591 if (TREE_CODE (init) == CONST_DECL)
1592 init = DECL_INITIAL (init);
1593 else if (TREE_READONLY_DECL_P (init))
1594 init = decl_constant_value (init);
1595 else if (TREE_CODE (init) == CONSTRUCTOR)
1596 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1597 if (init == error_mark_node)
1598 /* We must make this look different than `error_mark_node'
1599 because `decl_const_value' would mis-interpret it
1600 as only meaning that this VAR_DECL is defined. */
1601 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1602 else if (processing_template_decl)
1604 else if (! TREE_CONSTANT (init))
1606 /* We can allow references to things that are effectively
1607 static, since references are initialized with the address. */
1608 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1609 || (TREE_STATIC (init) == 0
1610 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1612 error ("field initializer is not constant");
1613 init = error_mark_node;
1619 if (processing_template_decl && ! current_function_decl
1620 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1621 value = push_template_decl (value);
1623 if (attrlist)
1624 cplus_decl_attributes (&value, attrlist, 0);
1626 if (TREE_CODE (value) == VAR_DECL)
1628 finish_static_data_member_decl (value, init, asmspec_tree,
1629 flags);
1630 return value;
1632 if (TREE_CODE (value) == FIELD_DECL)
1634 if (asmspec)
1635 cp_error ("`asm' specifiers are not permitted on non-static data members");
1636 if (DECL_INITIAL (value) == error_mark_node)
1637 init = error_mark_node;
1638 cp_finish_decl (value, init, NULL_TREE, flags);
1639 DECL_INITIAL (value) = init;
1640 DECL_IN_AGGR_P (value) = 1;
1641 return value;
1643 if (TREE_CODE (value) == FUNCTION_DECL)
1645 if (asmspec)
1647 /* This must override the asm specifier which was placed
1648 by grokclassfn. Lay this out fresh. */
1649 SET_DECL_RTL (value, NULL_RTX);
1650 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1652 cp_finish_decl (value, init, asmspec_tree, flags);
1654 /* Pass friends back this way. */
1655 if (DECL_FRIEND_P (value))
1656 return void_type_node;
1658 DECL_IN_AGGR_P (value) = 1;
1659 return value;
1661 my_friendly_abort (21);
1662 /* NOTREACHED */
1663 return NULL_TREE;
1666 /* Like `grokfield', but for bitfields.
1667 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1669 tree
1670 grokbitfield (declarator, declspecs, width)
1671 tree declarator, declspecs, width;
1673 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1674 0, NULL);
1676 if (! value) return NULL_TREE; /* friends went bad. */
1678 /* Pass friendly classes back. */
1679 if (TREE_CODE (value) == VOID_TYPE)
1680 return void_type_node;
1682 if (TREE_CODE (value) == TYPE_DECL)
1684 cp_error ("cannot declare `%D' to be a bitfield type", value);
1685 return NULL_TREE;
1688 /* Usually, finish_struct_1 catches bitifields with invalid types.
1689 But, in the case of bitfields with function type, we confuse
1690 ourselves into thinking they are member functions, so we must
1691 check here. */
1692 if (TREE_CODE (value) == FUNCTION_DECL)
1694 cp_error ("cannot declare bitfield `%D' with funcion type",
1695 DECL_NAME (value));
1696 return NULL_TREE;
1699 if (DECL_IN_AGGR_P (value))
1701 cp_error ("`%D' is already defined in the class %T", value,
1702 DECL_CONTEXT (value));
1703 return void_type_node;
1706 GNU_xref_member (current_class_name, value);
1708 if (TREE_STATIC (value))
1710 cp_error ("static member `%D' cannot be a bitfield", value);
1711 return NULL_TREE;
1713 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1715 if (width != error_mark_node)
1717 constant_expression_warning (width);
1718 DECL_INITIAL (value) = width;
1719 SET_DECL_C_BIT_FIELD (value);
1722 DECL_IN_AGGR_P (value) = 1;
1723 return value;
1726 tree
1727 grokoptypename (declspecs, declarator)
1728 tree declspecs, declarator;
1730 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1731 return mangle_conv_op_name_for_type (t);
1734 /* When a function is declared with an initializer,
1735 do the right thing. Currently, there are two possibilities:
1737 class B
1739 public:
1740 // initialization possibility #1.
1741 virtual void f () = 0;
1742 int g ();
1745 class D1 : B
1747 public:
1748 int d1;
1749 // error, no f ();
1752 class D2 : B
1754 public:
1755 int d2;
1756 void f ();
1759 class D3 : B
1761 public:
1762 int d3;
1763 // initialization possibility #2
1764 void f () = B::f;
1770 copy_assignment_arg_p (parmtype, virtualp)
1771 tree parmtype;
1772 int virtualp ATTRIBUTE_UNUSED;
1774 if (current_class_type == NULL_TREE)
1775 return 0;
1777 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1778 parmtype = TREE_TYPE (parmtype);
1780 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1781 #if 0
1782 /* Non-standard hack to support old Booch components. */
1783 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1784 #endif
1786 return 1;
1788 return 0;
1791 static void
1792 grok_function_init (decl, init)
1793 tree decl;
1794 tree init;
1796 /* An initializer for a function tells how this function should
1797 be inherited. */
1798 tree type = TREE_TYPE (decl);
1800 if (TREE_CODE (type) == FUNCTION_TYPE)
1801 cp_error ("initializer specified for non-member function `%D'", decl);
1802 else if (integer_zerop (init))
1804 DECL_PURE_VIRTUAL_P (decl) = 1;
1805 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
1807 tree parmtype
1808 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1810 if (copy_assignment_arg_p (parmtype, 1))
1811 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1814 else
1815 cp_error ("invalid initializer for virtual method `%D'", decl);
1818 void
1819 cplus_decl_attributes (decl, attributes, flags)
1820 tree *decl, attributes;
1821 int flags;
1823 if (*decl == NULL_TREE || *decl == void_type_node)
1824 return;
1826 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1827 decl = &DECL_TEMPLATE_RESULT (*decl);
1829 decl_attributes (decl, attributes, flags);
1831 if (TREE_CODE (*decl) == TYPE_DECL)
1832 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1835 /* CONSTRUCTOR_NAME:
1836 Return the name for the constructor (or destructor) for the
1837 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1838 IDENTIFIER_NODE. When given a template, this routine doesn't
1839 lose the specialization. */
1841 tree
1842 constructor_name_full (thing)
1843 tree thing;
1845 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1846 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1847 || TREE_CODE (thing) == TYPENAME_TYPE)
1848 thing = TYPE_NAME (thing);
1849 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1851 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1852 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1853 else
1854 thing = TYPE_NAME (thing);
1856 if (TREE_CODE (thing) == TYPE_DECL
1857 || (TREE_CODE (thing) == TEMPLATE_DECL
1858 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1859 thing = DECL_NAME (thing);
1860 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1861 return thing;
1864 /* CONSTRUCTOR_NAME:
1865 Return the name for the constructor (or destructor) for the
1866 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1867 IDENTIFIER_NODE. When given a template, return the plain
1868 unspecialized name. */
1870 tree
1871 constructor_name (thing)
1872 tree thing;
1874 tree t;
1875 thing = constructor_name_full (thing);
1876 t = IDENTIFIER_TEMPLATE (thing);
1877 if (!t)
1878 return thing;
1879 return t;
1882 /* Defer the compilation of the FN until the end of compilation. */
1884 void
1885 defer_fn (fn)
1886 tree fn;
1888 if (DECL_DEFERRED_FN (fn))
1889 return;
1890 DECL_DEFERRED_FN (fn) = 1;
1891 if (!deferred_fns)
1892 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1894 VARRAY_PUSH_TREE (deferred_fns, fn);
1897 /* Hunts through the global anonymous union ANON_DECL, building
1898 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1899 returns a VAR_DECL whose size is the same as the size of the
1900 ANON_DECL, if one is available.
1902 FIXME: we should really handle anonymous unions by binding the names
1903 of the members to COMPONENT_REFs rather than this kludge. */
1905 static tree
1906 build_anon_union_vars (anon_decl, elems, static_p, external_p)
1907 tree anon_decl;
1908 tree* elems;
1909 int static_p;
1910 int external_p;
1912 tree type = TREE_TYPE (anon_decl);
1913 tree main_decl = NULL_TREE;
1914 tree field;
1916 /* Rather than write the code to handle the non-union case,
1917 just give an error. */
1918 if (TREE_CODE (type) != UNION_TYPE)
1919 error ("anonymous struct not inside named type");
1921 for (field = TYPE_FIELDS (type);
1922 field != NULL_TREE;
1923 field = TREE_CHAIN (field))
1925 tree decl;
1927 if (DECL_ARTIFICIAL (field))
1928 continue;
1929 if (TREE_CODE (field) != FIELD_DECL)
1931 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
1932 field);
1933 continue;
1936 if (TREE_PRIVATE (field))
1937 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1938 else if (TREE_PROTECTED (field))
1939 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1941 if (DECL_NAME (field) == NULL_TREE
1942 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1944 decl = build_anon_union_vars (field, elems, static_p, external_p);
1945 if (!decl)
1946 continue;
1948 else if (DECL_NAME (field) == NULL_TREE)
1949 continue;
1950 else
1952 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1953 /* tell `pushdecl' that this is not tentative. */
1954 DECL_INITIAL (decl) = error_mark_node;
1955 TREE_PUBLIC (decl) = 0;
1956 TREE_STATIC (decl) = static_p;
1957 DECL_EXTERNAL (decl) = external_p;
1958 decl = pushdecl (decl);
1959 DECL_INITIAL (decl) = NULL_TREE;
1962 /* Only write out one anon union element--choose the largest
1963 one. We used to try to find one the same size as the union,
1964 but that fails if the ABI forces us to align the union more
1965 strictly. */
1966 if (main_decl == NULL_TREE
1967 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
1969 if (main_decl)
1970 TREE_ASM_WRITTEN (main_decl) = 1;
1971 main_decl = decl;
1973 else
1974 /* ??? This causes there to be no debug info written out
1975 about this decl. */
1976 TREE_ASM_WRITTEN (decl) = 1;
1978 if (DECL_NAME (field) == NULL_TREE
1979 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1980 /* The remainder of the processing was already done in the
1981 recursive call. */
1982 continue;
1984 /* If there's a cleanup to do, it belongs in the
1985 TREE_PURPOSE of the following TREE_LIST. */
1986 *elems = tree_cons (NULL_TREE, decl, *elems);
1987 TREE_TYPE (*elems) = type;
1990 return main_decl;
1993 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1994 anonymous union, then all members must be laid out together. PUBLIC_P
1995 is nonzero if this union is not declared static. */
1997 void
1998 finish_anon_union (anon_union_decl)
1999 tree anon_union_decl;
2001 tree type = TREE_TYPE (anon_union_decl);
2002 tree main_decl;
2003 int public_p = TREE_PUBLIC (anon_union_decl);
2004 int static_p = TREE_STATIC (anon_union_decl);
2005 int external_p = DECL_EXTERNAL (anon_union_decl);
2007 /* The VAR_DECL's context is the same as the TYPE's context. */
2008 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
2010 if (TYPE_FIELDS (type) == NULL_TREE)
2011 return;
2013 if (public_p)
2015 error ("namespace-scope anonymous aggregates must be static");
2016 return;
2019 main_decl = build_anon_union_vars (anon_union_decl,
2020 &DECL_ANON_UNION_ELEMS (anon_union_decl),
2021 static_p, external_p);
2023 if (main_decl == NULL_TREE)
2025 warning ("anonymous aggregate with no members");
2026 return;
2029 if (static_p)
2031 make_decl_rtl (main_decl, 0);
2032 COPY_DECL_RTL (main_decl, anon_union_decl);
2033 expand_anon_union_decl (anon_union_decl,
2034 NULL_TREE,
2035 DECL_ANON_UNION_ELEMS (anon_union_decl));
2037 else
2038 add_decl_stmt (anon_union_decl);
2041 /* Finish processing a builtin type TYPE. It's name is NAME,
2042 its fields are in the array FIELDS. LEN is the number of elements
2043 in FIELDS minus one, or put another way, it is the maximum subscript
2044 used in FIELDS.
2046 It is given the same alignment as ALIGN_TYPE. */
2048 void
2049 finish_builtin_type (type, name, fields, len, align_type)
2050 tree type;
2051 const char *name;
2052 tree fields[];
2053 int len;
2054 tree align_type;
2056 register int i;
2058 TYPE_FIELDS (type) = fields[0];
2059 for (i = 0; i < len; i++)
2061 layout_type (TREE_TYPE (fields[i]));
2062 DECL_FIELD_CONTEXT (fields[i]) = type;
2063 TREE_CHAIN (fields[i]) = fields[i+1];
2065 DECL_FIELD_CONTEXT (fields[i]) = type;
2066 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2067 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2068 layout_type (type);
2069 #if 0 /* not yet, should get fixed properly later */
2070 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2071 #else
2072 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2073 #endif
2074 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2075 layout_decl (TYPE_NAME (type), 0);
2078 /* Auxiliary functions to make type signatures for
2079 `operator new' and `operator delete' correspond to
2080 what compiler will be expecting. */
2082 tree
2083 coerce_new_type (type)
2084 tree type;
2086 int e = 0;
2087 tree args = TYPE_ARG_TYPES (type);
2089 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2091 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2092 e = 1, cp_error ("`operator new' must return type `%T'", ptr_type_node);
2094 if (!args || args == void_list_node
2095 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2097 e = 2;
2098 if (args && args != void_list_node)
2099 args = TREE_CHAIN (args);
2100 cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2102 switch (e)
2104 case 2:
2105 args = tree_cons (NULL_TREE, c_size_type_node, args);
2106 /* FALLTHROUGH */
2107 case 1:
2108 type = build_exception_variant
2109 (build_function_type (ptr_type_node, args),
2110 TYPE_RAISES_EXCEPTIONS (type));
2111 /* FALLTHROUGH */
2112 default:;
2114 return type;
2117 tree
2118 coerce_delete_type (type)
2119 tree type;
2121 int e = 0;
2122 tree args = TYPE_ARG_TYPES (type);
2124 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2126 if (!same_type_p (TREE_TYPE (type), void_type_node))
2127 e = 1, cp_error ("`operator delete' must return type `%T'", void_type_node);
2129 if (!args || args == void_list_node
2130 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2132 e = 2;
2133 if (args && args != void_list_node)
2134 args = TREE_CHAIN (args);
2135 cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2137 switch (e)
2139 case 2:
2140 args = tree_cons (NULL_TREE, ptr_type_node, args);
2141 /* FALLTHROUGH */
2142 case 1:
2143 type = build_exception_variant
2144 (build_function_type (void_type_node, args),
2145 TYPE_RAISES_EXCEPTIONS (type));
2146 /* FALLTHROUGH */
2147 default:;
2150 return type;
2153 static void
2154 mark_vtable_entries (decl)
2155 tree decl;
2157 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2159 for (; entries; entries = TREE_CHAIN (entries))
2161 tree fnaddr = TREE_VALUE (entries);
2162 tree fn;
2164 if (TREE_CODE (fnaddr) != ADDR_EXPR
2165 && TREE_CODE (fnaddr) != FDESC_EXPR)
2166 /* This entry is an offset: a virtual base class offset, a
2167 virtual call offset, an RTTI offset, etc. */
2168 continue;
2170 fn = TREE_OPERAND (fnaddr, 0);
2171 TREE_ADDRESSABLE (fn) = 1;
2172 /* When we don't have vcall offsets, we output thunks whenever
2173 we output the vtables that contain them. With vcall offsets,
2174 we know all the thunks we'll need when we emit a virtual
2175 function, so we emit the thunks there instead. */
2176 if (DECL_THUNK_P (fn))
2177 use_thunk (fn, /*emit_p=*/0);
2178 mark_used (fn);
2182 /* Set DECL up to have the closest approximation of "initialized common"
2183 linkage available. */
2185 void
2186 comdat_linkage (decl)
2187 tree decl;
2189 if (flag_weak)
2190 make_decl_one_only (decl);
2191 else if (TREE_CODE (decl) == FUNCTION_DECL
2192 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2193 /* We can just emit function and compiler-generated variables
2194 statically; having multiple copies is (for the most part) only
2195 a waste of space.
2197 There are two correctness issues, however: the address of a
2198 template instantiation with external linkage should be the
2199 same, independent of what translation unit asks for the
2200 address, and this will not hold when we emit multiple copies of
2201 the function. However, there's little else we can do.
2203 Also, by default, the typeinfo implementation assumes that
2204 there will be only one copy of the string used as the name for
2205 each type. Therefore, if weak symbols are unavailable, the
2206 run-time library should perform a more conservative check; it
2207 should perform a string comparison, rather than an address
2208 comparison. */
2209 TREE_PUBLIC (decl) = 0;
2210 else
2212 /* Static data member template instantiations, however, cannot
2213 have multiple copies. */
2214 if (DECL_INITIAL (decl) == 0
2215 || DECL_INITIAL (decl) == error_mark_node)
2216 DECL_COMMON (decl) = 1;
2217 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2219 DECL_COMMON (decl) = 1;
2220 DECL_INITIAL (decl) = error_mark_node;
2222 else
2224 /* We can't do anything useful; leave vars for explicit
2225 instantiation. */
2226 DECL_EXTERNAL (decl) = 1;
2227 DECL_NOT_REALLY_EXTERN (decl) = 0;
2231 if (DECL_LANG_SPECIFIC (decl))
2232 DECL_COMDAT (decl) = 1;
2235 /* For win32 we also want to put explicit instantiations in
2236 linkonce sections, so that they will be merged with implicit
2237 instantiations; otherwise we get duplicate symbol errors. */
2239 void
2240 maybe_make_one_only (decl)
2241 tree decl;
2243 /* We used to say that this was not necessary on targets that support weak
2244 symbols, because the implicit instantiations will defer to the explicit
2245 one. However, that's not actually the case in SVR4; a strong definition
2246 after a weak one is an error. Also, not making explicit
2247 instantiations one_only means that we can end up with two copies of
2248 some template instantiations. */
2249 if (! flag_weak)
2250 return;
2252 /* We can't set DECL_COMDAT on functions, or finish_file will think
2253 we can get away with not emitting them if they aren't used. We need
2254 to for variables so that cp_finish_decl will update their linkage,
2255 because their DECL_INITIAL may not have been set properly yet. */
2257 make_decl_one_only (decl);
2259 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2260 DECL_COMDAT (decl) = 1;
2263 /* Returns the virtual function with which the vtable for TYPE is
2264 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2266 static tree
2267 key_method (type)
2268 tree type;
2270 tree method;
2272 if (TYPE_FOR_JAVA (type)
2273 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2274 || CLASSTYPE_INTERFACE_KNOWN (type))
2275 return NULL_TREE;
2277 for (method = TYPE_METHODS (type); method != NULL_TREE;
2278 method = TREE_CHAIN (method))
2279 if (DECL_VINDEX (method) != NULL_TREE
2280 && ! DECL_DECLARED_INLINE_P (method)
2281 && (! DECL_PURE_VIRTUAL_P (method)
2282 #if 0
2283 /* This would be nice, but we didn't think of it in time. */
2284 || DECL_DESTRUCTOR_P (method)
2285 #endif
2287 return method;
2289 return NULL_TREE;
2292 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2293 based on TYPE and other static flags.
2295 Note that anything public is tagged TREE_PUBLIC, whether
2296 it's public in this file or in another one. */
2298 void
2299 import_export_vtable (decl, type, final)
2300 tree decl, type;
2301 int final;
2303 if (DECL_INTERFACE_KNOWN (decl))
2304 return;
2306 if (TYPE_FOR_JAVA (type))
2308 TREE_PUBLIC (decl) = 1;
2309 DECL_EXTERNAL (decl) = 1;
2310 DECL_INTERFACE_KNOWN (decl) = 1;
2312 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2314 TREE_PUBLIC (decl) = 1;
2315 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2316 DECL_INTERFACE_KNOWN (decl) = 1;
2318 else
2320 /* We can only wait to decide if we have real non-inline virtual
2321 functions in our class, or if we come from a template. */
2323 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2324 || key_method (type));
2326 if (final || ! found)
2328 comdat_linkage (decl);
2329 DECL_EXTERNAL (decl) = 0;
2331 else
2333 TREE_PUBLIC (decl) = 1;
2334 DECL_EXTERNAL (decl) = 1;
2339 /* Determine whether or not we want to specifically import or export CTYPE,
2340 using various heuristics. */
2342 static void
2343 import_export_class (ctype)
2344 tree ctype;
2346 /* -1 for imported, 1 for exported. */
2347 int import_export = 0;
2349 /* It only makes sense to call this function at EOF. The reason is
2350 that this function looks at whether or not the first non-inline
2351 non-abstract virtual member function has been defined in this
2352 translation unit. But, we can't possibly know that until we've
2353 seen the entire translation unit. */
2354 my_friendly_assert (at_eof, 20000226);
2356 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2357 return;
2359 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2360 we will have CLASSTYPE_INTERFACE_ONLY set but not
2361 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2362 heuristic because someone will supply a #pragma implementation
2363 elsewhere, and deducing it here would produce a conflict. */
2364 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2365 return;
2367 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2368 import_export = -1;
2369 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2370 import_export = 1;
2372 /* If we got -fno-implicit-templates, we import template classes that
2373 weren't explicitly instantiated. */
2374 if (import_export == 0
2375 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2376 && ! flag_implicit_templates)
2377 import_export = -1;
2379 /* Base our import/export status on that of the first non-inline,
2380 non-pure virtual function, if any. */
2381 if (import_export == 0
2382 && TYPE_POLYMORPHIC_P (ctype))
2384 tree method = key_method (ctype);
2385 if (method)
2386 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2389 #ifdef MULTIPLE_SYMBOL_SPACES
2390 if (import_export == -1)
2391 import_export = 0;
2392 #endif
2394 if (import_export)
2396 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2397 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2401 /* We need to describe to the assembler the relationship between
2402 a vtable and the vtable of the parent class. */
2404 static void
2405 output_vtable_inherit (vars)
2406 tree vars;
2408 tree parent;
2409 rtx child_rtx, parent_rtx;
2411 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2413 parent = binfo_for_vtable (vars);
2415 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2416 parent_rtx = const0_rtx;
2417 else if (parent)
2419 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2420 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2422 else
2423 my_friendly_abort (980826);
2425 assemble_vtable_inherit (child_rtx, parent_rtx);
2428 static int
2429 finish_vtable_vardecl (t, data)
2430 tree *t;
2431 void *data ATTRIBUTE_UNUSED;
2433 tree vars = *t;
2434 tree ctype = DECL_CONTEXT (vars);
2435 import_export_class (ctype);
2436 import_export_vtable (vars, ctype, 1);
2438 if (! DECL_EXTERNAL (vars)
2439 && DECL_NEEDED_P (vars)
2440 && ! TREE_ASM_WRITTEN (vars))
2442 if (TREE_TYPE (vars) == void_type_node)
2443 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2444 return 0;
2446 /* Write it out. */
2447 mark_vtable_entries (vars);
2448 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2449 store_init_value (vars, DECL_INITIAL (vars));
2451 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2453 /* Mark the VAR_DECL node representing the vtable itself as a
2454 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2455 It is rather important that such things be ignored because
2456 any effort to actually generate DWARF for them will run
2457 into trouble when/if we encounter code like:
2459 #pragma interface
2460 struct S { virtual void member (); };
2462 because the artificial declaration of the vtable itself (as
2463 manufactured by the g++ front end) will say that the vtable
2464 is a static member of `S' but only *after* the debug output
2465 for the definition of `S' has already been output. This causes
2466 grief because the DWARF entry for the definition of the vtable
2467 will try to refer back to an earlier *declaration* of the
2468 vtable as a static member of `S' and there won't be one.
2469 We might be able to arrange to have the "vtable static member"
2470 attached to the member list for `S' before the debug info for
2471 `S' get written (which would solve the problem) but that would
2472 require more intrusive changes to the g++ front end. */
2474 DECL_IGNORED_P (vars) = 1;
2477 /* Always make vtables weak. */
2478 if (flag_weak)
2479 comdat_linkage (vars);
2481 rest_of_decl_compilation (vars, NULL, 1, 1);
2483 if (flag_vtable_gc)
2484 output_vtable_inherit (vars);
2486 /* Because we're only doing syntax-checking, we'll never end up
2487 actually marking the variable as written. */
2488 if (flag_syntax_only)
2489 TREE_ASM_WRITTEN (vars) = 1;
2491 /* Since we're writing out the vtable here, also write the debug
2492 info. */
2493 note_debug_info_needed (ctype);
2495 return 1;
2498 /* If the references to this class' vtables were optimized away, still
2499 emit the appropriate debugging information. See dfs_debug_mark. */
2500 if (DECL_COMDAT (vars)
2501 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2502 note_debug_info_needed (ctype);
2504 return 0;
2507 static int
2508 prune_vtable_vardecl (t, data)
2509 tree *t;
2510 void *data ATTRIBUTE_UNUSED;
2512 *t = TREE_CHAIN (*t);
2513 return 1;
2516 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2517 inline function or template instantiation at end-of-file. */
2519 void
2520 import_export_decl (decl)
2521 tree decl;
2523 if (DECL_INTERFACE_KNOWN (decl))
2524 return;
2526 if (DECL_TEMPLATE_INSTANTIATION (decl)
2527 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2529 DECL_NOT_REALLY_EXTERN (decl) = 1;
2530 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2531 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2532 && (flag_implicit_templates
2533 || (flag_implicit_inline_templates
2534 && DECL_DECLARED_INLINE_P (decl))))
2536 if (!TREE_PUBLIC (decl))
2537 /* Templates are allowed to have internal linkage. See
2538 [basic.link]. */
2540 else
2541 comdat_linkage (decl);
2543 else
2544 DECL_NOT_REALLY_EXTERN (decl) = 0;
2546 else if (DECL_FUNCTION_MEMBER_P (decl))
2548 if (!DECL_DECLARED_INLINE_P (decl))
2550 tree ctype = DECL_CONTEXT (decl);
2551 import_export_class (ctype);
2552 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2554 DECL_NOT_REALLY_EXTERN (decl)
2555 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2556 || (DECL_DECLARED_INLINE_P (decl)
2557 && ! flag_implement_inlines
2558 && !DECL_VINDEX (decl)));
2560 /* Always make artificials weak. */
2561 if (DECL_ARTIFICIAL (decl) && flag_weak)
2562 comdat_linkage (decl);
2563 else
2564 maybe_make_one_only (decl);
2567 else
2568 comdat_linkage (decl);
2570 else if (tinfo_decl_p (decl, 0))
2572 tree ctype = TREE_TYPE (DECL_NAME (decl));
2574 if (IS_AGGR_TYPE (ctype))
2575 import_export_class (ctype);
2577 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2578 && TYPE_POLYMORPHIC_P (ctype)
2579 /* If -fno-rtti, we're not necessarily emitting this stuff with
2580 the class, so go ahead and emit it now. This can happen
2581 when a class is used in exception handling. */
2582 && flag_rtti
2583 /* If the type is a cv-qualified variant of a type, then we
2584 must emit the tinfo function in this translation unit
2585 since it will not be emitted when the vtable for the type
2586 is output (which is when the unqualified version is
2587 generated). */
2588 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2590 DECL_NOT_REALLY_EXTERN (decl)
2591 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2592 || (DECL_DECLARED_INLINE_P (decl)
2593 && ! flag_implement_inlines
2594 && !DECL_VINDEX (decl)));
2596 /* Always make artificials weak. */
2597 if (flag_weak)
2598 comdat_linkage (decl);
2600 else if (TYPE_BUILT_IN (ctype)
2601 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2602 DECL_NOT_REALLY_EXTERN (decl) = 0;
2603 else
2604 comdat_linkage (decl);
2606 else
2607 comdat_linkage (decl);
2609 DECL_INTERFACE_KNOWN (decl) = 1;
2612 tree
2613 build_cleanup (decl)
2614 tree decl;
2616 tree temp;
2617 tree type = TREE_TYPE (decl);
2619 if (TREE_CODE (type) == ARRAY_TYPE)
2620 temp = decl;
2621 else
2623 mark_addressable (decl);
2624 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2626 temp = build_delete (TREE_TYPE (temp), temp,
2627 sfk_complete_destructor,
2628 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2629 return temp;
2632 /* Returns the initialization guard variable for the variable DECL,
2633 which has static storage duration. */
2635 tree
2636 get_guard (decl)
2637 tree decl;
2639 tree sname;
2640 tree guard;
2642 sname = mangle_guard_variable (decl);
2643 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2644 if (! guard)
2646 tree guard_type;
2648 /* We use a type that is big enough to contain a mutex as well
2649 as an integer counter. */
2650 guard_type = long_long_integer_type_node;
2651 guard = build_decl (VAR_DECL, sname, guard_type);
2653 /* The guard should have the same linkage as what it guards. */
2654 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2655 TREE_STATIC (guard) = TREE_STATIC (decl);
2656 DECL_COMMON (guard) = DECL_COMMON (decl);
2657 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2658 if (TREE_PUBLIC (decl))
2659 DECL_WEAK (guard) = DECL_WEAK (decl);
2661 DECL_ARTIFICIAL (guard) = 1;
2662 TREE_USED (guard) = 1;
2663 pushdecl_top_level (guard);
2664 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2666 return guard;
2669 /* Return those bits of the GUARD variable that should be set when the
2670 guarded entity is actually initialized. */
2672 static tree
2673 get_guard_bits (guard)
2674 tree guard;
2676 /* We only set the first byte of the guard, in order to leave room
2677 for a mutex in the high-order bits. */
2678 guard = build1 (ADDR_EXPR,
2679 build_pointer_type (TREE_TYPE (guard)),
2680 guard);
2681 guard = build1 (NOP_EXPR,
2682 build_pointer_type (char_type_node),
2683 guard);
2684 guard = build1 (INDIRECT_REF, char_type_node, guard);
2686 return guard;
2689 /* Return an expression which determines whether or not the GUARD
2690 variable has already been initialized. */
2692 tree
2693 get_guard_cond (guard)
2694 tree guard;
2696 tree guard_value;
2698 /* Check to see if the GUARD is zero. */
2699 guard = get_guard_bits (guard);
2700 guard_value = integer_zero_node;
2701 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2702 guard_value = convert (TREE_TYPE (guard), guard_value);
2703 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2706 /* Return an expression which sets the GUARD variable, indicating that
2707 the variable being guarded has been initialized. */
2709 tree
2710 set_guard (guard)
2711 tree guard;
2713 tree guard_init;
2715 /* Set the GUARD to one. */
2716 guard = get_guard_bits (guard);
2717 guard_init = integer_one_node;
2718 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2719 guard_init = convert (TREE_TYPE (guard), guard_init);
2720 return build_modify_expr (guard, NOP_EXPR, guard_init);
2723 /* Start the process of running a particular set of global constructors
2724 or destructors. Subroutine of do_[cd]tors. */
2726 static tree
2727 start_objects (method_type, initp)
2728 int method_type, initp;
2730 tree fnname;
2731 tree body;
2732 char type[10];
2734 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2736 if (initp != DEFAULT_INIT_PRIORITY)
2738 char joiner;
2740 #ifdef JOINER
2741 joiner = JOINER;
2742 #else
2743 joiner = '_';
2744 #endif
2746 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2748 else
2749 sprintf (type, "%c", method_type);
2751 fnname = get_file_function_name_long (type);
2753 start_function (void_list_node,
2754 make_call_declarator (fnname, void_list_node, NULL_TREE,
2755 NULL_TREE),
2756 NULL_TREE, SF_DEFAULT);
2758 /* It can be a static function as long as collect2 does not have
2759 to scan the object file to find its ctor/dtor routine. */
2760 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2762 /* Mark this declaration as used to avoid spurious warnings. */
2763 TREE_USED (current_function_decl) = 1;
2765 /* Mark this function as a global constructor or destructor. */
2766 if (method_type == 'I')
2767 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2768 else
2769 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2770 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2772 body = begin_compound_stmt (/*has_no_scope=*/0);
2774 /* We cannot allow these functions to be elided, even if they do not
2775 have external linkage. And, there's no point in deferring
2776 copmilation of thes functions; they're all going to have to be
2777 out anyhow. */
2778 current_function_cannot_inline
2779 = "static constructors and destructors cannot be inlined";
2781 return body;
2784 /* Finish the process of running a particular set of global constructors
2785 or destructors. Subroutine of do_[cd]tors. */
2787 static void
2788 finish_objects (method_type, initp, body)
2789 int method_type, initp;
2790 tree body;
2792 tree fn;
2794 /* Finish up. */
2795 finish_compound_stmt (/*has_no_scope=*/0, body);
2796 fn = finish_function (0);
2797 expand_body (fn);
2799 /* When only doing semantic analysis, and no RTL generation, we
2800 can't call functions that directly emit assembly code; there is
2801 no assembly file in which to put the code. */
2802 if (flag_syntax_only)
2803 return;
2805 if (targetm.have_ctors_dtors)
2807 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2808 if (method_type == 'I')
2809 (* targetm.asm_out.constructor) (fnsym, initp);
2810 else
2811 (* targetm.asm_out.destructor) (fnsym, initp);
2815 /* The names of the parameters to the function created to handle
2816 initializations and destructions for objects with static storage
2817 duration. */
2818 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2819 #define PRIORITY_IDENTIFIER "__priority"
2821 /* The name of the function we create to handle initializations and
2822 destructions for objects with static storage duration. */
2823 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2825 /* The declaration for the __INITIALIZE_P argument. */
2826 static tree initialize_p_decl;
2828 /* The declaration for the __PRIORITY argument. */
2829 static tree priority_decl;
2831 /* The declaration for the static storage duration function. */
2832 static tree ssdf_decl;
2834 /* All the static storage duration functions created in this
2835 translation unit. */
2836 static varray_type ssdf_decls;
2838 /* A map from priority levels to information about that priority
2839 level. There may be many such levels, so efficient lookup is
2840 important. */
2841 static splay_tree priority_info_map;
2843 /* Begins the generation of the function that will handle all
2844 initialization and destruction of objects with static storage
2845 duration. The function generated takes two parameters of type
2846 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2847 non-zero, it performs initializations. Otherwise, it performs
2848 destructions. It only performs those initializations or
2849 destructions with the indicated __PRIORITY. The generated function
2850 returns no value.
2852 It is assumed that this function will only be called once per
2853 translation unit. */
2855 static tree
2856 start_static_storage_duration_function ()
2858 static unsigned ssdf_number;
2860 tree parm_types;
2861 tree type;
2862 tree body;
2863 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2865 /* Create the identifier for this function. It will be of the form
2866 SSDF_IDENTIFIER_<number>. */
2867 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2868 if (ssdf_number == 0)
2870 /* Overflow occurred. That means there are at least 4 billion
2871 initialization functions. */
2872 sorry ("too many initialization functions required");
2873 my_friendly_abort (19990430);
2876 /* Create the parameters. */
2877 parm_types = void_list_node;
2878 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2879 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2880 type = build_function_type (void_type_node, parm_types);
2882 /* Create the FUNCTION_DECL itself. */
2883 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2884 get_identifier (id),
2885 type);
2886 TREE_PUBLIC (ssdf_decl) = 0;
2887 DECL_ARTIFICIAL (ssdf_decl) = 1;
2889 /* Put this function in the list of functions to be called from the
2890 static constructors and destructors. */
2891 if (!ssdf_decls)
2893 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2895 /* Take this opportunity to initialize the map from priority
2896 numbers to information about that priority level. */
2897 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2898 /*delete_key_fn=*/0,
2899 /*delete_value_fn=*/
2900 (splay_tree_delete_value_fn) &free);
2902 /* We always need to generate functions for the
2903 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2904 priorities later, we'll be sure to find the
2905 DEFAULT_INIT_PRIORITY. */
2906 get_priority_info (DEFAULT_INIT_PRIORITY);
2909 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2911 /* Create the argument list. */
2912 initialize_p_decl = build_decl (PARM_DECL,
2913 get_identifier (INITIALIZE_P_IDENTIFIER),
2914 integer_type_node);
2915 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2916 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
2917 TREE_USED (initialize_p_decl) = 1;
2918 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
2919 integer_type_node);
2920 DECL_CONTEXT (priority_decl) = ssdf_decl;
2921 DECL_ARG_TYPE (priority_decl) = integer_type_node;
2922 TREE_USED (priority_decl) = 1;
2924 TREE_CHAIN (initialize_p_decl) = priority_decl;
2925 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2927 /* Put the function in the global scope. */
2928 pushdecl (ssdf_decl);
2930 /* Start the function itself. This is equivalent to declarating the
2931 function as:
2933 static void __ssdf (int __initialize_p, init __priority_p);
2935 It is static because we only need to call this function from the
2936 various constructor and destructor functions for this module. */
2937 start_function (/*specs=*/NULL_TREE,
2938 ssdf_decl,
2939 /*attrs=*/NULL_TREE,
2940 SF_PRE_PARSED);
2942 /* Set up the scope of the outermost block in the function. */
2943 body = begin_compound_stmt (/*has_no_scope=*/0);
2945 /* This function must not be deferred because we are depending on
2946 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2947 current_function_cannot_inline
2948 = "static storage duration functions cannot be inlined";
2950 return body;
2953 /* Finish the generation of the function which performs initialization
2954 and destruction of objects with static storage duration. After
2955 this point, no more such objects can be created. */
2957 static void
2958 finish_static_storage_duration_function (body)
2959 tree body;
2961 /* Close out the function. */
2962 finish_compound_stmt (/*has_no_scope=*/0, body);
2963 expand_body (finish_function (0));
2966 /* Return the information about the indicated PRIORITY level. If no
2967 code to handle this level has yet been generated, generate the
2968 appropriate prologue. */
2970 static priority_info
2971 get_priority_info (priority)
2972 int priority;
2974 priority_info pi;
2975 splay_tree_node n;
2977 n = splay_tree_lookup (priority_info_map,
2978 (splay_tree_key) priority);
2979 if (!n)
2981 /* Create a new priority information structure, and insert it
2982 into the map. */
2983 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2984 pi->initializations_p = 0;
2985 pi->destructions_p = 0;
2986 splay_tree_insert (priority_info_map,
2987 (splay_tree_key) priority,
2988 (splay_tree_value) pi);
2990 else
2991 pi = (priority_info) n->value;
2993 return pi;
2996 /* Set up to handle the initialization or destruction of DECL. If
2997 INITP is non-zero, we are initializing the variable. Otherwise, we
2998 are destroying it. */
3000 static tree
3001 start_static_initialization_or_destruction (decl, initp)
3002 tree decl;
3003 int initp;
3005 tree guard_if_stmt = NULL_TREE;
3006 int priority;
3007 tree cond;
3008 tree guard;
3009 tree init_cond;
3010 priority_info pi;
3012 /* Figure out the priority for this declaration. */
3013 priority = DECL_INIT_PRIORITY (decl);
3014 if (!priority)
3015 priority = DEFAULT_INIT_PRIORITY;
3017 /* Remember that we had an initialization or finalization at this
3018 priority. */
3019 pi = get_priority_info (priority);
3020 if (initp)
3021 pi->initializations_p = 1;
3022 else
3023 pi->destructions_p = 1;
3025 /* Trick the compiler into thinking we are at the file and line
3026 where DECL was declared so that error-messages make sense, and so
3027 that the debugger will show somewhat sensible file and line
3028 information. */
3029 input_filename = DECL_SOURCE_FILE (decl);
3030 lineno = DECL_SOURCE_LINE (decl);
3032 /* Because of:
3034 [class.access.spec]
3036 Access control for implicit calls to the constructors,
3037 the conversion functions, or the destructor called to
3038 create and destroy a static data member is performed as
3039 if these calls appeared in the scope of the member's
3040 class.
3042 we pretend we are in a static member function of the class of
3043 which the DECL is a member. */
3044 if (member_p (decl))
3046 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3047 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3050 /* Conditionalize this initialization on being in the right priority
3051 and being initializing/finalizing appropriately. */
3052 guard_if_stmt = begin_if_stmt ();
3053 cond = cp_build_binary_op (EQ_EXPR,
3054 priority_decl,
3055 build_int_2 (priority, 0));
3056 init_cond = initp ? integer_one_node : integer_zero_node;
3057 init_cond = cp_build_binary_op (EQ_EXPR,
3058 initialize_p_decl,
3059 init_cond);
3060 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3062 /* Assume we don't need a guard. */
3063 guard = NULL_TREE;
3064 /* We need a guard if this is an object with external linkage that
3065 might be initialized in more than one place. (For example, a
3066 static data member of a template, when the data member requires
3067 construction.) */
3068 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3069 || DECL_ONE_ONLY (decl)
3070 || DECL_WEAK (decl)))
3072 tree guard_cond;
3074 guard = get_guard (decl);
3076 /* When using __cxa_atexit, we just check the GUARD as we would
3077 for a local static. */
3078 if (flag_use_cxa_atexit)
3080 /* When using __cxa_atexit, we never try to destroy
3081 anything from a static destructor. */
3082 my_friendly_assert (initp, 20000629);
3083 guard_cond = get_guard_cond (guard);
3085 /* If we don't have __cxa_atexit, then we will be running
3086 destructors from .fini sections, or their equivalents. So,
3087 we need to know how many times we've tried to initialize this
3088 object. We do initializations only if the GUARD is zero,
3089 i.e., if we are the first to initialize the variable. We do
3090 destructions only if the GUARD is one, i.e., if we are the
3091 last to destroy the variable. */
3092 else if (initp)
3093 guard_cond
3094 = cp_build_binary_op (EQ_EXPR,
3095 build_unary_op (PREINCREMENT_EXPR,
3096 guard,
3097 /*noconvert=*/1),
3098 integer_one_node);
3099 else
3100 guard_cond
3101 = cp_build_binary_op (EQ_EXPR,
3102 build_unary_op (PREDECREMENT_EXPR,
3103 guard,
3104 /*noconvert=*/1),
3105 integer_zero_node);
3107 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3110 finish_if_stmt_cond (cond, guard_if_stmt);
3112 /* If we're using __cxa_atexit, we have not already set the GUARD,
3113 so we must do so now. */
3114 if (guard && initp && flag_use_cxa_atexit)
3115 finish_expr_stmt (set_guard (guard));
3117 return guard_if_stmt;
3120 /* We've just finished generating code to do an initialization or
3121 finalization. GUARD_IF_STMT is the if-statement we used to guard
3122 the initialization. */
3124 static void
3125 finish_static_initialization_or_destruction (guard_if_stmt)
3126 tree guard_if_stmt;
3128 finish_then_clause (guard_if_stmt);
3129 finish_if_stmt ();
3131 /* Now that we're done with DECL we don't need to pretend to be a
3132 member of its class any longer. */
3133 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3134 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3137 /* Generate code to do the static initialization of DECL. The
3138 initialization is INIT. If DECL may be initialized more than once
3139 in different object files, GUARD is the guard variable to
3140 check. PRIORITY is the priority for the initialization. */
3142 static void
3143 do_static_initialization (decl, init)
3144 tree decl;
3145 tree init;
3147 tree expr;
3148 tree guard_if_stmt;
3150 /* Set up for the initialization. */
3151 guard_if_stmt
3152 = start_static_initialization_or_destruction (decl,
3153 /*initp=*/1);
3155 /* Do the initialization itself. */
3156 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3157 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3158 expr = build_aggr_init (decl, init, 0);
3159 else
3161 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3162 TREE_SIDE_EFFECTS (expr) = 1;
3164 finish_expr_stmt (expr);
3166 /* If we're using __cxa_atexit, register a a function that calls the
3167 destructor for the object. */
3168 if (flag_use_cxa_atexit)
3169 register_dtor_fn (decl);
3171 /* Finsh up. */
3172 finish_static_initialization_or_destruction (guard_if_stmt);
3175 /* Generate code to do the static destruction of DECL. If DECL may be
3176 initialized more than once in different object files, GUARD is the
3177 guard variable to check. PRIORITY is the priority for the
3178 destruction. */
3180 static void
3181 do_static_destruction (decl)
3182 tree decl;
3184 tree guard_if_stmt;
3186 /* If we're using __cxa_atexit, then destructors are registered
3187 immediately after objects are initialized. */
3188 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3190 /* If we don't need a destructor, there's nothing to do. */
3191 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3192 return;
3194 /* Actually do the destruction. */
3195 guard_if_stmt = start_static_initialization_or_destruction (decl,
3196 /*initp=*/0);
3197 finish_expr_stmt (build_cleanup (decl));
3198 finish_static_initialization_or_destruction (guard_if_stmt);
3201 /* VARS is a list of variables with static storage duration which may
3202 need initialization and/or finalization. Remove those variables
3203 that don't really need to be initialized or finalized, and return
3204 the resulting list. The order in which the variables appear in
3205 VARS is in reverse order of the order in which they should actually
3206 be initialized. The list we return is in the unreversed order;
3207 i.e., the first variable should be initialized first. */
3209 static tree
3210 prune_vars_needing_no_initialization (vars)
3211 tree vars;
3213 tree var;
3214 tree result;
3216 for (var = vars, result = NULL_TREE;
3217 var;
3218 var = TREE_CHAIN (var))
3220 tree decl = TREE_VALUE (var);
3221 tree init = TREE_PURPOSE (var);
3223 /* Deal gracefully with error. */
3224 if (decl == error_mark_node)
3225 continue;
3227 /* The only things that can be initialized are variables. */
3228 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3230 /* If this object is not defined, we don't need to do anything
3231 here. */
3232 if (DECL_EXTERNAL (decl))
3233 continue;
3235 /* Also, if the initializer already contains errors, we can bail
3236 out now. */
3237 if (init && TREE_CODE (init) == TREE_LIST
3238 && value_member (error_mark_node, init))
3239 continue;
3241 /* This variable is going to need initialization and/or
3242 finalization, so we add it to the list. */
3243 result = tree_cons (init, decl, result);
3246 return result;
3249 /* Make sure we have told the back end about all the variables in
3250 VARS. */
3252 static void
3253 write_out_vars (vars)
3254 tree vars;
3256 tree v;
3258 for (v = vars; v; v = TREE_CHAIN (v))
3259 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3260 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3263 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3264 (otherwise) that will initialize all gobal objects with static
3265 storage duration having the indicated PRIORITY. */
3267 static void
3268 generate_ctor_or_dtor_function (constructor_p, priority)
3269 int constructor_p;
3270 int priority;
3272 char function_key;
3273 tree arguments;
3274 tree body;
3275 size_t i;
3277 /* We use `I' to indicate initialization and `D' to indicate
3278 destruction. */
3279 if (constructor_p)
3280 function_key = 'I';
3281 else
3282 function_key = 'D';
3284 /* Begin the function. */
3285 body = start_objects (function_key, priority);
3287 /* Call the static storage duration function with appropriate
3288 arguments. */
3289 for (i = 0; i < ssdf_decls->elements_used; ++i)
3291 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3292 NULL_TREE);
3293 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3294 arguments);
3295 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3296 arguments));
3299 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3300 calls to any functions marked with attributes indicating that
3301 they should be called at initialization- or destruction-time. */
3302 if (priority == DEFAULT_INIT_PRIORITY)
3304 tree fns;
3306 for (fns = constructor_p ? static_ctors : static_dtors;
3307 fns;
3308 fns = TREE_CHAIN (fns))
3309 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3312 /* Close out the function. */
3313 finish_objects (function_key, priority, body);
3316 /* Generate constructor and destructor functions for the priority
3317 indicated by N. */
3319 static int
3320 generate_ctor_and_dtor_functions_for_priority (n, data)
3321 splay_tree_node n;
3322 void *data ATTRIBUTE_UNUSED;
3324 int priority = (int) n->key;
3325 priority_info pi = (priority_info) n->value;
3327 /* Generate the functions themselves, but only if they are really
3328 needed. */
3329 if (pi->initializations_p
3330 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3331 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3332 priority);
3333 if (pi->destructions_p
3334 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3335 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3336 priority);
3338 /* Keep iterating. */
3339 return 0;
3342 /* This routine is called from the last rule in yyparse ().
3343 Its job is to create all the code needed to initialize and
3344 destroy the global aggregates. We do the destruction
3345 first, since that way we only need to reverse the decls once. */
3347 void
3348 finish_file ()
3350 tree vars;
3351 int reconsider;
3352 size_t i;
3354 at_eof = 1;
3356 /* Bad parse errors. Just forget about it. */
3357 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3358 return;
3360 /* Otherwise, GDB can get confused, because in only knows
3361 about source for LINENO-1 lines. */
3362 lineno -= 1;
3364 interface_unknown = 1;
3365 interface_only = 0;
3367 /* We now have to write out all the stuff we put off writing out.
3368 These include:
3370 o Template specializations that we have not yet instantiated,
3371 but which are needed.
3372 o Initialization and destruction for non-local objects with
3373 static storage duration. (Local objects with static storage
3374 duration are initialized when their scope is first entered,
3375 and are cleaned up via atexit.)
3376 o Virtual function tables.
3378 All of these may cause others to be needed. For example,
3379 instantiating one function may cause another to be needed, and
3380 generating the intiailzer for an object may cause templates to be
3381 instantiated, etc., etc. */
3383 timevar_push (TV_VARCONST);
3385 emit_support_tinfos ();
3389 reconsider = 0;
3391 /* If there are templates that we've put off instantiating, do
3392 them now. */
3393 instantiate_pending_templates ();
3395 /* Write out virtual tables as required. Note that writing out
3396 the virtual table for a template class may cause the
3397 instantiation of members of that class. */
3398 if (walk_globals (vtable_decl_p,
3399 finish_vtable_vardecl,
3400 /*data=*/0))
3401 reconsider = 1;
3403 /* Write out needed type info variables. Writing out one variable
3404 might cause others to be needed. */
3405 if (walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3406 reconsider = 1;
3408 /* The list of objects with static storage duration is built up
3409 in reverse order. We clear STATIC_AGGREGATES so that any new
3410 aggregates added during the initialization of these will be
3411 initialized in the correct order when we next come around the
3412 loop. */
3413 vars = prune_vars_needing_no_initialization (static_aggregates);
3414 static_aggregates = NULL_TREE;
3416 if (vars)
3418 tree v;
3420 /* We need to start a new initialization function each time
3421 through the loop. That's because we need to know which
3422 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3423 isn't computed until a function is finished, and written
3424 out. That's a deficiency in the back-end. When this is
3425 fixed, these initialization functions could all become
3426 inline, with resulting performance improvements. */
3427 tree ssdf_body = start_static_storage_duration_function ();
3429 /* Make sure the back end knows about all the variables. */
3430 write_out_vars (vars);
3432 /* First generate code to do all the initializations. */
3433 for (v = vars; v; v = TREE_CHAIN (v))
3434 do_static_initialization (TREE_VALUE (v),
3435 TREE_PURPOSE (v));
3437 /* Then, generate code to do all the destructions. Do these
3438 in reverse order so that the most recently constructed
3439 variable is the first destroyed. If we're using
3440 __cxa_atexit, then we don't need to do this; functions
3441 were registered at initialization time to destroy the
3442 local statics. */
3443 if (!flag_use_cxa_atexit)
3445 vars = nreverse (vars);
3446 for (v = vars; v; v = TREE_CHAIN (v))
3447 do_static_destruction (TREE_VALUE (v));
3449 else
3450 vars = NULL_TREE;
3452 /* Finish up the static storage duration function for this
3453 round. */
3454 finish_static_storage_duration_function (ssdf_body);
3456 /* All those initializations and finalizations might cause
3457 us to need more inline functions, more template
3458 instantiations, etc. */
3459 reconsider = 1;
3462 /* Go through the various inline functions, and see if any need
3463 synthesizing. */
3464 for (i = 0; i < deferred_fns_used; ++i)
3466 tree decl = VARRAY_TREE (deferred_fns, i);
3467 import_export_decl (decl);
3468 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3469 && TREE_USED (decl)
3470 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3472 /* Even though we're already at the top-level, we push
3473 there again. That way, when we pop back a few lines
3474 hence, all of our state is restored. Otherwise,
3475 finish_function doesn't clean things up, and we end
3476 up with CURRENT_FUNCTION_DECL set. */
3477 push_to_top_level ();
3478 synthesize_method (decl);
3479 pop_from_top_level ();
3480 reconsider = 1;
3484 /* We lie to the back-end, pretending that some functions are
3485 not defined when they really are. This keeps these functions
3486 from being put out unnecessarily. But, we must stop lying
3487 when the functions are referenced, or if they are not comdat
3488 since they need to be put out now. */
3489 for (i = 0; i < deferred_fns_used; ++i)
3491 tree decl = VARRAY_TREE (deferred_fns, i);
3493 if (DECL_NOT_REALLY_EXTERN (decl)
3494 && DECL_INITIAL (decl)
3495 && DECL_NEEDED_P (decl))
3496 DECL_EXTERNAL (decl) = 0;
3498 /* If we're going to need to write this function out, and
3499 there's already a body for it, create RTL for it now.
3500 (There might be no body if this is a method we haven't
3501 gotten around to synthesizing yet.) */
3502 if (!DECL_EXTERNAL (decl)
3503 && DECL_NEEDED_P (decl)
3504 && DECL_SAVED_TREE (decl)
3505 && !TREE_ASM_WRITTEN (decl))
3507 int saved_not_really_extern;
3509 /* When we call finish_function in expand_body, it will
3510 try to reset DECL_NOT_REALLY_EXTERN so we save and
3511 restore it here. */
3512 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3513 /* Generate RTL for this function now that we know we
3514 need it. */
3515 expand_body (decl);
3516 /* Undo the damage done by finish_function. */
3517 DECL_EXTERNAL (decl) = 0;
3518 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3519 /* If we're compiling -fsyntax-only pretend that this
3520 function has been written out so that we don't try to
3521 expand it again. */
3522 if (flag_syntax_only)
3523 TREE_ASM_WRITTEN (decl) = 1;
3524 reconsider = 1;
3528 if (deferred_fns_used
3529 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3530 deferred_fns_used))
3531 reconsider = 1;
3532 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3533 reconsider = 1;
3535 /* Static data members are just like namespace-scope globals. */
3536 for (i = 0; i < pending_statics_used; ++i)
3538 tree decl = VARRAY_TREE (pending_statics, i);
3539 if (TREE_ASM_WRITTEN (decl))
3540 continue;
3541 import_export_decl (decl);
3542 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3543 DECL_EXTERNAL (decl) = 0;
3545 if (pending_statics
3546 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3547 pending_statics_used))
3548 reconsider = 1;
3550 while (reconsider);
3552 /* We give C linkage to static constructors and destructors. */
3553 push_lang_context (lang_name_c);
3555 /* Generate initialization and destruction functions for all
3556 priorities for which they are required. */
3557 if (priority_info_map)
3558 splay_tree_foreach (priority_info_map,
3559 generate_ctor_and_dtor_functions_for_priority,
3560 /*data=*/0);
3562 /* We're done with the splay-tree now. */
3563 if (priority_info_map)
3564 splay_tree_delete (priority_info_map);
3566 /* We're done with static constructors, so we can go back to "C++"
3567 linkage now. */
3568 pop_lang_context ();
3570 /* Now delete from the chain of variables all virtual function tables.
3571 We output them all ourselves, because each will be treated
3572 specially. We don't do this if we're just doing semantic
3573 analysis, and not code-generation. */
3574 if (!flag_syntax_only)
3575 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3577 /* Now, issue warnings about static, but not defined, functions,
3578 etc., and emit debugging information. */
3579 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3580 if (pending_statics)
3581 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3582 pending_statics_used);
3584 finish_repo ();
3586 /* The entire file is now complete. If requested, dump everything
3587 to a file. */
3589 int flags;
3590 FILE *stream = dump_begin (TDI_all, &flags);
3592 if (stream)
3594 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3595 dump_end (TDI_all, stream);
3599 /* If there's some tool that wants to examine the entire translation
3600 unit, let it do so now. */
3601 if (back_end_hook)
3602 (*back_end_hook) (global_namespace);
3604 timevar_pop (TV_VARCONST);
3606 if (flag_detailed_statistics)
3608 dump_tree_statistics ();
3609 dump_time_statistics ();
3613 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3614 expr. Since it was parsed like a type, we need to wade through and fix
3615 that. Unfortunately, since operator() is left-associative, we can't use
3616 tail recursion. In the above example, TYPE is `A', and DECL is
3617 `()()()()()'.
3619 Maybe this shouldn't be recursive, but how often will it actually be
3620 used? (jason) */
3622 tree
3623 reparse_absdcl_as_expr (type, decl)
3624 tree type, decl;
3626 /* do build_functional_cast (type, NULL_TREE) at bottom */
3627 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3628 return build_functional_cast (type, NULL_TREE);
3630 /* recurse */
3631 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3633 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3635 if (TREE_CODE (decl) == CALL_EXPR
3636 && (! TREE_TYPE (decl)
3637 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3638 decl = require_complete_type (decl);
3640 return decl;
3643 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3644 out to be an expr. Since it was parsed like a type, we need to wade
3645 through and fix that. Since casts are right-associative, we are
3646 reversing the order, so we don't have to recurse.
3648 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3649 `1'. */
3651 tree
3652 reparse_absdcl_as_casts (decl, expr)
3653 tree decl, expr;
3655 tree type;
3657 if (TREE_CODE (expr) == CONSTRUCTOR
3658 && TREE_TYPE (expr) == 0)
3660 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3661 decl = TREE_OPERAND (decl, 0);
3663 expr = digest_init (type, expr, (tree *) 0);
3664 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3666 int failure = complete_array_type (type, expr, 1);
3667 if (failure)
3668 my_friendly_abort (78);
3672 while (decl)
3674 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3675 decl = TREE_OPERAND (decl, 0);
3676 expr = build_c_cast (type, expr);
3679 if (warn_old_style_cast && ! in_system_header
3680 && current_lang_name != lang_name_c)
3681 warning ("use of old-style cast");
3683 return expr;
3686 /* Given plain tree nodes for an expression, build up the full semantics. */
3688 tree
3689 build_expr_from_tree (t)
3690 tree t;
3692 if (t == NULL_TREE || t == error_mark_node)
3693 return t;
3695 switch (TREE_CODE (t))
3697 case IDENTIFIER_NODE:
3698 return do_identifier (t, 0, NULL_TREE);
3700 case LOOKUP_EXPR:
3701 if (LOOKUP_EXPR_GLOBAL (t))
3702 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3703 else
3704 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3706 case TEMPLATE_ID_EXPR:
3707 return (lookup_template_function
3708 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3709 build_expr_from_tree (TREE_OPERAND (t, 1))));
3711 case INDIRECT_REF:
3712 return build_x_indirect_ref
3713 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3715 case CAST_EXPR:
3716 return build_functional_cast
3717 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3719 case REINTERPRET_CAST_EXPR:
3720 return build_reinterpret_cast
3721 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3723 case CONST_CAST_EXPR:
3724 return build_const_cast
3725 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3727 case DYNAMIC_CAST_EXPR:
3728 return build_dynamic_cast
3729 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3731 case STATIC_CAST_EXPR:
3732 return build_static_cast
3733 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3735 case PREDECREMENT_EXPR:
3736 case PREINCREMENT_EXPR:
3737 case POSTDECREMENT_EXPR:
3738 case POSTINCREMENT_EXPR:
3739 case NEGATE_EXPR:
3740 case BIT_NOT_EXPR:
3741 case ABS_EXPR:
3742 case TRUTH_NOT_EXPR:
3743 case ADDR_EXPR:
3744 case CONVERT_EXPR: /* Unary + */
3745 case REALPART_EXPR:
3746 case IMAGPART_EXPR:
3747 if (TREE_TYPE (t))
3748 return t;
3749 return build_x_unary_op (TREE_CODE (t),
3750 build_expr_from_tree (TREE_OPERAND (t, 0)));
3752 case PLUS_EXPR:
3753 case MINUS_EXPR:
3754 case MULT_EXPR:
3755 case TRUNC_DIV_EXPR:
3756 case CEIL_DIV_EXPR:
3757 case FLOOR_DIV_EXPR:
3758 case ROUND_DIV_EXPR:
3759 case EXACT_DIV_EXPR:
3760 case BIT_AND_EXPR:
3761 case BIT_ANDTC_EXPR:
3762 case BIT_IOR_EXPR:
3763 case BIT_XOR_EXPR:
3764 case TRUNC_MOD_EXPR:
3765 case FLOOR_MOD_EXPR:
3766 case TRUTH_ANDIF_EXPR:
3767 case TRUTH_ORIF_EXPR:
3768 case TRUTH_AND_EXPR:
3769 case TRUTH_OR_EXPR:
3770 case RSHIFT_EXPR:
3771 case LSHIFT_EXPR:
3772 case RROTATE_EXPR:
3773 case LROTATE_EXPR:
3774 case EQ_EXPR:
3775 case NE_EXPR:
3776 case MAX_EXPR:
3777 case MIN_EXPR:
3778 case LE_EXPR:
3779 case GE_EXPR:
3780 case LT_EXPR:
3781 case GT_EXPR:
3782 case MEMBER_REF:
3783 return build_x_binary_op
3784 (TREE_CODE (t),
3785 build_expr_from_tree (TREE_OPERAND (t, 0)),
3786 build_expr_from_tree (TREE_OPERAND (t, 1)));
3788 case DOTSTAR_EXPR:
3789 return build_m_component_ref
3790 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3791 build_expr_from_tree (TREE_OPERAND (t, 1)));
3793 case SCOPE_REF:
3794 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3796 case ARRAY_REF:
3797 if (TREE_OPERAND (t, 0) == NULL_TREE)
3798 /* new-type-id */
3799 return build_nt (ARRAY_REF, NULL_TREE,
3800 build_expr_from_tree (TREE_OPERAND (t, 1)));
3801 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3802 build_expr_from_tree (TREE_OPERAND (t, 1)));
3804 case SIZEOF_EXPR:
3805 case ALIGNOF_EXPR:
3807 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3808 if (!TYPE_P (r))
3809 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3810 else
3811 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3814 case MODOP_EXPR:
3815 return build_x_modify_expr
3816 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3817 TREE_CODE (TREE_OPERAND (t, 1)),
3818 build_expr_from_tree (TREE_OPERAND (t, 2)));
3820 case ARROW_EXPR:
3821 return build_x_arrow
3822 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3824 case NEW_EXPR:
3825 return build_new
3826 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3827 build_expr_from_tree (TREE_OPERAND (t, 1)),
3828 build_expr_from_tree (TREE_OPERAND (t, 2)),
3829 NEW_EXPR_USE_GLOBAL (t));
3831 case DELETE_EXPR:
3832 return delete_sanity
3833 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3834 build_expr_from_tree (TREE_OPERAND (t, 1)),
3835 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3837 case COMPOUND_EXPR:
3838 if (TREE_OPERAND (t, 1) == NULL_TREE)
3839 return build_x_compound_expr
3840 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3841 else
3842 my_friendly_abort (42);
3844 case METHOD_CALL_EXPR:
3845 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3847 tree ref = TREE_OPERAND (t, 0);
3848 tree name = TREE_OPERAND (ref, 1);
3850 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3851 name = build_nt (TEMPLATE_ID_EXPR,
3852 TREE_OPERAND (name, 0),
3853 build_expr_from_tree (TREE_OPERAND (name, 1)));
3855 return build_scoped_method_call
3856 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3857 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3858 name,
3859 build_expr_from_tree (TREE_OPERAND (t, 2)));
3861 else
3863 tree fn = TREE_OPERAND (t, 0);
3865 /* We can get a TEMPLATE_ID_EXPR here on code like:
3867 x->f<2>();
3869 so we must resolve that. However, we can also get things
3870 like a BIT_NOT_EXPR here, when referring to a destructor,
3871 and things like that are not correctly resolved by
3872 build_expr_from_tree. So, just use build_expr_from_tree
3873 when we really need it. */
3874 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3875 fn = lookup_template_function
3876 (TREE_OPERAND (fn, 0),
3877 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3879 return build_method_call
3880 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3882 build_expr_from_tree (TREE_OPERAND (t, 2)),
3883 NULL_TREE, LOOKUP_NORMAL);
3886 case CALL_EXPR:
3887 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3889 tree ref = TREE_OPERAND (t, 0);
3890 tree name = TREE_OPERAND (ref, 1);
3892 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3893 name = build_nt (TEMPLATE_ID_EXPR,
3894 TREE_OPERAND (name, 0),
3895 build_expr_from_tree (TREE_OPERAND (name, 1)));
3897 return build_member_call
3898 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3899 name,
3900 build_expr_from_tree (TREE_OPERAND (t, 1)));
3902 else
3904 tree name = TREE_OPERAND (t, 0);
3905 tree id;
3906 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3907 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3908 && !LOOKUP_EXPR_GLOBAL (name)
3909 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3910 && (!current_class_type
3911 || !lookup_member (current_class_type, id, 0, 0)))
3913 /* Do Koenig lookup if there are no class members. */
3914 name = do_identifier (id, 0, args);
3916 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3917 || ! really_overloaded_fn (name))
3918 name = build_expr_from_tree (name);
3919 return build_x_function_call (name, args, current_class_ref);
3922 case COND_EXPR:
3923 return build_x_conditional_expr
3924 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3925 build_expr_from_tree (TREE_OPERAND (t, 1)),
3926 build_expr_from_tree (TREE_OPERAND (t, 2)));
3928 case PSEUDO_DTOR_EXPR:
3929 return (finish_pseudo_destructor_call_expr
3930 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3931 build_expr_from_tree (TREE_OPERAND (t, 1)),
3932 build_expr_from_tree (TREE_OPERAND (t, 2))));
3934 case TREE_LIST:
3936 tree purpose, value, chain;
3938 if (t == void_list_node)
3939 return t;
3941 purpose = TREE_PURPOSE (t);
3942 if (purpose)
3943 purpose = build_expr_from_tree (purpose);
3944 value = TREE_VALUE (t);
3945 if (value)
3946 value = build_expr_from_tree (value);
3947 chain = TREE_CHAIN (t);
3948 if (chain && chain != void_type_node)
3949 chain = build_expr_from_tree (chain);
3950 return tree_cons (purpose, value, chain);
3953 case COMPONENT_REF:
3955 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3956 tree field = TREE_OPERAND (t, 1);
3958 /* We use a COMPONENT_REF to indicate things of the form `x.b'
3959 and `x.A::b'. We must distinguish between those cases
3960 here. */
3961 if (TREE_CODE (field) == SCOPE_REF)
3962 return build_object_ref (object,
3963 TREE_OPERAND (field, 0),
3964 TREE_OPERAND (field, 1));
3965 else
3966 return build_x_component_ref (object, field,
3967 NULL_TREE, 1);
3970 case THROW_EXPR:
3971 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3973 case CONSTRUCTOR:
3975 tree r;
3977 /* digest_init will do the wrong thing if we let it. */
3978 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3979 return t;
3981 r = build_nt (CONSTRUCTOR, NULL_TREE,
3982 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
3983 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3985 if (TREE_TYPE (t))
3986 return digest_init (TREE_TYPE (t), r, 0);
3987 return r;
3990 case TYPEID_EXPR:
3991 if (TYPE_P (TREE_OPERAND (t, 0)))
3992 return get_typeid (TREE_OPERAND (t, 0));
3993 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3995 case VAR_DECL:
3996 return convert_from_reference (t);
3998 case VA_ARG_EXPR:
3999 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4000 TREE_TYPE (t));
4002 default:
4003 return t;
4007 /* This is something of the form `int (*a)++' that has turned out to be an
4008 expr. It was only converted into parse nodes, so we need to go through
4009 and build up the semantics. Most of the work is done by
4010 build_expr_from_tree, above.
4012 In the above example, TYPE is `int' and DECL is `*a'. */
4014 tree
4015 reparse_decl_as_expr (type, decl)
4016 tree type, decl;
4018 decl = build_expr_from_tree (decl);
4019 if (type)
4020 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4021 else
4022 return decl;
4025 /* This is something of the form `int (*a)' that has turned out to be a
4026 decl. It was only converted into parse nodes, so we need to do the
4027 checking that make_{pointer,reference}_declarator do. */
4029 tree
4030 finish_decl_parsing (decl)
4031 tree decl;
4033 switch (TREE_CODE (decl))
4035 case IDENTIFIER_NODE:
4036 return decl;
4037 case INDIRECT_REF:
4038 return make_pointer_declarator
4039 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4040 case ADDR_EXPR:
4041 return make_reference_declarator
4042 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4043 case BIT_NOT_EXPR:
4044 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4045 return decl;
4046 case SCOPE_REF:
4047 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4048 TREE_COMPLEXITY (decl) = current_class_depth;
4049 return decl;
4050 case ARRAY_REF:
4051 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4052 return decl;
4053 case TREE_LIST:
4054 /* For attribute handling. */
4055 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4056 return decl;
4057 case TEMPLATE_ID_EXPR:
4058 return decl;
4059 default:
4060 my_friendly_abort (5);
4061 return NULL_TREE;
4065 /* Return 1 if root encloses child. */
4067 static int
4068 is_namespace_ancestor (root, child)
4069 tree root, child;
4071 if (root == child)
4072 return 1;
4073 if (root == global_namespace)
4074 return 1;
4075 if (child == global_namespace)
4076 return 0;
4077 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4081 /* Return the namespace that is the common ancestor
4082 of two given namespaces. */
4084 tree
4085 namespace_ancestor (ns1, ns2)
4086 tree ns1, ns2;
4088 if (is_namespace_ancestor (ns1, ns2))
4089 return ns1;
4090 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4093 /* Insert used into the using list of user. Set indirect_flag if this
4094 directive is not directly from the source. Also find the common
4095 ancestor and let our users know about the new namespace */
4096 static void
4097 add_using_namespace (user, used, indirect)
4098 tree user;
4099 tree used;
4100 int indirect;
4102 tree t;
4103 /* Using oneself is a no-op. */
4104 if (user == used)
4105 return;
4106 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4107 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4108 /* Check if we already have this. */
4109 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4110 if (t != NULL_TREE)
4112 if (!indirect)
4113 /* Promote to direct usage. */
4114 TREE_INDIRECT_USING (t) = 0;
4115 return;
4118 /* Add used to the user's using list. */
4119 DECL_NAMESPACE_USING (user)
4120 = tree_cons (used, namespace_ancestor (user, used),
4121 DECL_NAMESPACE_USING (user));
4123 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4125 /* Add user to the used's users list. */
4126 DECL_NAMESPACE_USERS (used)
4127 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4129 /* Recursively add all namespaces used. */
4130 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4131 /* indirect usage */
4132 add_using_namespace (user, TREE_PURPOSE (t), 1);
4134 /* Tell everyone using us about the new used namespaces. */
4135 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4136 add_using_namespace (TREE_PURPOSE (t), used, 1);
4139 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4140 duplicates. The first list becomes the tail of the result.
4142 The algorithm is O(n^2). We could get this down to O(n log n) by
4143 doing a sort on the addresses of the functions, if that becomes
4144 necessary. */
4146 static tree
4147 merge_functions (s1, s2)
4148 tree s1;
4149 tree s2;
4151 for (; s2; s2 = OVL_NEXT (s2))
4153 tree fn = OVL_CURRENT (s2);
4154 if (! ovl_member (fn, s1))
4155 s1 = build_overload (fn, s1);
4157 return s1;
4160 /* This should return an error not all definitions define functions.
4161 It is not an error if we find two functions with exactly the
4162 same signature, only if these are selected in overload resolution.
4163 old is the current set of bindings, new the freshly-found binding.
4164 XXX Do we want to give *all* candidates in case of ambiguity?
4165 XXX In what way should I treat extern declarations?
4166 XXX I don't want to repeat the entire duplicate_decls here */
4168 static tree
4169 ambiguous_decl (name, old, new, flags)
4170 tree name;
4171 tree old;
4172 tree new;
4173 int flags;
4175 tree val, type;
4176 my_friendly_assert (old != NULL_TREE, 393);
4177 /* Copy the value. */
4178 val = BINDING_VALUE (new);
4179 if (val)
4180 switch (TREE_CODE (val))
4182 case TEMPLATE_DECL:
4183 /* If we expect types or namespaces, and not templates,
4184 or this is not a template class. */
4185 if (LOOKUP_QUALIFIERS_ONLY (flags)
4186 && !DECL_CLASS_TEMPLATE_P (val))
4187 val = NULL_TREE;
4188 break;
4189 case TYPE_DECL:
4190 if (LOOKUP_NAMESPACES_ONLY (flags))
4191 val = NULL_TREE;
4192 break;
4193 case NAMESPACE_DECL:
4194 if (LOOKUP_TYPES_ONLY (flags))
4195 val = NULL_TREE;
4196 break;
4197 default:
4198 if (LOOKUP_QUALIFIERS_ONLY (flags))
4199 val = NULL_TREE;
4202 if (!BINDING_VALUE (old))
4203 BINDING_VALUE (old) = val;
4204 else if (val && val != BINDING_VALUE (old))
4206 if (is_overloaded_fn (BINDING_VALUE (old))
4207 && is_overloaded_fn (val))
4209 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4210 val);
4212 else
4214 /* Some declarations are functions, some are not. */
4215 if (flags & LOOKUP_COMPLAIN)
4217 /* If we've already given this error for this lookup,
4218 BINDING_VALUE (old) is error_mark_node, so let's not
4219 repeat ourselves. */
4220 if (BINDING_VALUE (old) != error_mark_node)
4222 cp_error ("use of `%D' is ambiguous", name);
4223 cp_error_at (" first declared as `%#D' here",
4224 BINDING_VALUE (old));
4226 cp_error_at (" also declared as `%#D' here", val);
4228 BINDING_VALUE (old) = error_mark_node;
4231 /* ... and copy the type. */
4232 type = BINDING_TYPE (new);
4233 if (LOOKUP_NAMESPACES_ONLY (flags))
4234 type = NULL_TREE;
4235 if (!BINDING_TYPE (old))
4236 BINDING_TYPE (old) = type;
4237 else if (type && BINDING_TYPE (old) != type)
4239 if (flags & LOOKUP_COMPLAIN)
4241 cp_error ("`%D' denotes an ambiguous type",name);
4242 cp_error_at (" first type here", BINDING_TYPE (old));
4243 cp_error_at (" other type here", type);
4246 return old;
4249 /* Subroutine of unualified_namespace_lookup:
4250 Add the bindings of NAME in used namespaces to VAL.
4251 We are currently looking for names in namespace SCOPE, so we
4252 look through USINGS for using-directives of namespaces
4253 which have SCOPE as a common ancestor with the current scope.
4254 Returns zero on errors. */
4257 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4258 tree name, val, usings, scope;
4259 int flags;
4260 tree *spacesp;
4262 tree iter;
4263 tree val1;
4264 /* Iterate over all used namespaces in current, searching for using
4265 directives of scope. */
4266 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4267 if (TREE_VALUE (iter) == scope)
4269 if (spacesp)
4270 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4271 *spacesp);
4272 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4273 /* Resolve ambiguities. */
4274 val = ambiguous_decl (name, val, val1, flags);
4276 return BINDING_VALUE (val) != error_mark_node;
4279 /* [namespace.qual]
4280 Accepts the NAME to lookup and its qualifying SCOPE.
4281 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4282 or 0 on error. */
4285 qualified_lookup_using_namespace (name, scope, result, flags)
4286 tree name;
4287 tree scope;
4288 tree result;
4289 int flags;
4291 /* Maintain a list of namespaces visited... */
4292 tree seen = NULL_TREE;
4293 /* ... and a list of namespace yet to see. */
4294 tree todo = NULL_TREE;
4295 tree usings;
4296 /* Look through namespace aliases. */
4297 scope = ORIGINAL_NAMESPACE (scope);
4298 while (scope && (result != error_mark_node))
4300 seen = tree_cons (scope, NULL_TREE, seen);
4301 result = ambiguous_decl (name, result,
4302 binding_for_name (name, scope), flags);
4303 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4304 /* Consider using directives. */
4305 for (usings = DECL_NAMESPACE_USING (scope); usings;
4306 usings = TREE_CHAIN (usings))
4307 /* If this was a real directive, and we have not seen it. */
4308 if (!TREE_INDIRECT_USING (usings)
4309 && !purpose_member (TREE_PURPOSE (usings), seen))
4310 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4311 if (todo)
4313 scope = TREE_PURPOSE (todo);
4314 todo = TREE_CHAIN (todo);
4316 else
4317 scope = NULL_TREE; /* If there never was a todo list. */
4319 return result != error_mark_node;
4322 /* [namespace.memdef]/2 */
4324 /* Set the context of a declaration to scope. Complain if we are not
4325 outside scope. */
4327 void
4328 set_decl_namespace (decl, scope, friendp)
4329 tree decl;
4330 tree scope;
4331 int friendp;
4333 tree old;
4335 /* Get rid of namespace aliases. */
4336 scope = ORIGINAL_NAMESPACE (scope);
4338 /* It is ok for friends to be qualified in parallel space. */
4339 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4340 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4341 decl, scope);
4342 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4343 if (scope != current_namespace)
4345 /* See whether this has been declared in the namespace. */
4346 old = namespace_binding (DECL_NAME (decl), scope);
4347 if (!old)
4348 /* No old declaration at all. */
4349 goto complain;
4350 /* A template can be explicitly specialized in any namespace. */
4351 if (processing_explicit_instantiation)
4352 return;
4353 if (!is_overloaded_fn (decl))
4354 /* Don't compare non-function decls with decls_match here,
4355 since it can't check for the correct constness at this
4356 point. pushdecl will find those errors later. */
4357 return;
4358 /* Since decl is a function, old should contain a function decl. */
4359 if (!is_overloaded_fn (old))
4360 goto complain;
4361 if (processing_template_decl || processing_specialization)
4362 /* We have not yet called push_template_decl to turn the
4363 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4364 won't match. But, we'll check later, when we construct the
4365 template. */
4366 return;
4367 for (; old; old = OVL_NEXT (old))
4368 if (decls_match (decl, OVL_CURRENT (old)))
4369 return;
4371 else
4372 return;
4373 complain:
4374 cp_error ("`%D' should have been declared inside `%D'",
4375 decl, scope);
4378 /* Compute the namespace where a declaration is defined. */
4380 static tree
4381 decl_namespace (decl)
4382 tree decl;
4384 if (TYPE_P (decl))
4385 decl = TYPE_STUB_DECL (decl);
4386 while (DECL_CONTEXT (decl))
4388 decl = DECL_CONTEXT (decl);
4389 if (TREE_CODE (decl) == NAMESPACE_DECL)
4390 return decl;
4391 if (TYPE_P (decl))
4392 decl = TYPE_STUB_DECL (decl);
4393 my_friendly_assert (DECL_P (decl), 390);
4396 return global_namespace;
4399 /* Return the namespace where the current declaration is declared. */
4401 tree
4402 current_decl_namespace ()
4404 tree result;
4405 /* If we have been pushed into a different namespace, use it. */
4406 if (decl_namespace_list)
4407 return TREE_PURPOSE (decl_namespace_list);
4409 if (current_class_type)
4410 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4411 else if (current_function_decl)
4412 result = decl_namespace (current_function_decl);
4413 else
4414 result = current_namespace;
4415 return result;
4418 /* Temporarily set the namespace for the current declaration. */
4420 void
4421 push_decl_namespace (decl)
4422 tree decl;
4424 if (TREE_CODE (decl) != NAMESPACE_DECL)
4425 decl = decl_namespace (decl);
4426 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4427 NULL_TREE, decl_namespace_list);
4430 void
4431 pop_decl_namespace ()
4433 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4436 /* Enter a class or namespace scope. */
4438 void
4439 push_scope (t)
4440 tree t;
4442 if (TREE_CODE (t) == NAMESPACE_DECL)
4443 push_decl_namespace (t);
4444 else
4445 pushclass (t, 2);
4448 /* Leave scope pushed by push_scope. */
4450 void
4451 pop_scope (t)
4452 tree t;
4454 if (TREE_CODE (t) == NAMESPACE_DECL)
4455 pop_decl_namespace ();
4456 else
4457 popclass ();
4460 /* [basic.lookup.koenig] */
4461 /* A non-zero return value in the functions below indicates an error.
4462 All nodes allocated in the procedure are on the scratch obstack. */
4464 struct arg_lookup
4466 tree name;
4467 tree namespaces;
4468 tree classes;
4469 tree functions;
4472 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4473 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4474 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4475 static int add_function PARAMS ((struct arg_lookup *, tree));
4476 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4477 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4478 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4480 /* Add a function to the lookup structure.
4481 Returns 1 on error. */
4483 static int
4484 add_function (k, fn)
4485 struct arg_lookup *k;
4486 tree fn;
4488 /* We used to check here to see if the function was already in the list,
4489 but that's O(n^2), which is just too expensive for function lookup.
4490 Now we deal with the occasional duplicate in joust. In doing this, we
4491 assume that the number of duplicates will be small compared to the
4492 total number of functions being compared, which should usually be the
4493 case. */
4495 /* We must find only functions, or exactly one non-function. */
4496 if (!k->functions)
4497 k->functions = fn;
4498 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4499 k->functions = build_overload (fn, k->functions);
4500 else
4502 tree f1 = OVL_CURRENT (k->functions);
4503 tree f2 = fn;
4504 if (is_overloaded_fn (f1))
4506 fn = f1; f1 = f2; f2 = fn;
4508 cp_error_at ("`%D' is not a function,", f1);
4509 cp_error_at (" conflict with `%D'", f2);
4510 cp_error (" in call to `%D'", k->name);
4511 return 1;
4514 return 0;
4517 /* Add functions of a namespace to the lookup structure.
4518 Returns 1 on error. */
4520 static int
4521 arg_assoc_namespace (k, scope)
4522 struct arg_lookup *k;
4523 tree scope;
4525 tree value;
4527 if (purpose_member (scope, k->namespaces))
4528 return 0;
4529 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4531 value = namespace_binding (k->name, scope);
4532 if (!value)
4533 return 0;
4535 for (; value; value = OVL_NEXT (value))
4536 if (add_function (k, OVL_CURRENT (value)))
4537 return 1;
4539 return 0;
4542 /* Adds everything associated with a template argument to the lookup
4543 structure. Returns 1 on error. */
4545 static int
4546 arg_assoc_template_arg (k, arg)
4547 struct arg_lookup* k;
4548 tree arg;
4550 /* [basic.lookup.koenig]
4552 If T is a template-id, its associated namespaces and classes are
4553 ... the namespaces and classes associated with the types of the
4554 template arguments provided for template type parameters
4555 (excluding template template parameters); the namespaces in which
4556 any template template arguments are defined; and the classes in
4557 which any member templates used as template template arguments
4558 are defined. [Note: non-type template arguments do not
4559 contribute to the set of associated namespaces. ] */
4561 /* Consider first template template arguments. */
4562 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4563 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4564 return 0;
4565 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4567 tree ctx = CP_DECL_CONTEXT (arg);
4569 /* It's not a member template. */
4570 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4571 return arg_assoc_namespace (k, ctx);
4572 /* Otherwise, it must be member template. */
4573 else
4574 return arg_assoc_class (k, ctx);
4576 /* It's not a template template argument, but it is a type template
4577 argument. */
4578 else if (TYPE_P (arg))
4579 return arg_assoc_type (k, arg);
4580 /* It's a non-type template argument. */
4581 else
4582 return 0;
4585 /* Adds everything associated with class to the lookup structure.
4586 Returns 1 on error. */
4588 static int
4589 arg_assoc_class (k, type)
4590 struct arg_lookup* k;
4591 tree type;
4593 tree list, friends, context;
4594 int i;
4596 /* Backend build structures, such as __builtin_va_list, aren't
4597 affected by all this. */
4598 if (!CLASS_TYPE_P (type))
4599 return 0;
4601 if (purpose_member (type, k->classes))
4602 return 0;
4603 k->classes = tree_cons (type, NULL_TREE, k->classes);
4605 context = decl_namespace (TYPE_MAIN_DECL (type));
4606 if (arg_assoc_namespace (k, context))
4607 return 1;
4609 /* Process baseclasses. */
4610 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4611 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4612 return 1;
4614 /* Process friends. */
4615 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4616 list = TREE_CHAIN (list))
4617 if (k->name == TREE_PURPOSE (list))
4618 for (friends = TREE_VALUE (list); friends;
4619 friends = TREE_CHAIN (friends))
4620 /* Only interested in global functions with potentially hidden
4621 (i.e. unqualified) declarations. */
4622 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4623 && decl_namespace (TREE_VALUE (friends)) == context)
4624 if (add_function (k, TREE_VALUE (friends)))
4625 return 1;
4627 /* Process template arguments. */
4628 if (CLASSTYPE_TEMPLATE_INFO (type))
4630 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4631 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4632 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4635 return 0;
4638 /* Adds everything associated with a given type.
4639 Returns 1 on error. */
4641 static int
4642 arg_assoc_type (k, type)
4643 struct arg_lookup *k;
4644 tree type;
4646 switch (TREE_CODE (type))
4648 case VOID_TYPE:
4649 case INTEGER_TYPE:
4650 case REAL_TYPE:
4651 case COMPLEX_TYPE:
4652 case VECTOR_TYPE:
4653 case CHAR_TYPE:
4654 case BOOLEAN_TYPE:
4655 return 0;
4656 case RECORD_TYPE:
4657 if (TYPE_PTRMEMFUNC_P (type))
4658 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4659 return arg_assoc_class (k, type);
4660 case POINTER_TYPE:
4661 case REFERENCE_TYPE:
4662 case ARRAY_TYPE:
4663 return arg_assoc_type (k, TREE_TYPE (type));
4664 case UNION_TYPE:
4665 case ENUMERAL_TYPE:
4666 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4667 case OFFSET_TYPE:
4668 /* Pointer to member: associate class type and value type. */
4669 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4670 return 1;
4671 return arg_assoc_type (k, TREE_TYPE (type));
4672 case METHOD_TYPE:
4673 /* The basetype is referenced in the first arg type, so just
4674 fall through. */
4675 case FUNCTION_TYPE:
4676 /* Associate the parameter types. */
4677 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4678 return 1;
4679 /* Associate the return type. */
4680 return arg_assoc_type (k, TREE_TYPE (type));
4681 case TEMPLATE_TYPE_PARM:
4682 case BOUND_TEMPLATE_TEMPLATE_PARM:
4683 return 0;
4684 case TYPENAME_TYPE:
4685 return 0;
4686 case LANG_TYPE:
4687 if (type == unknown_type_node)
4688 return 0;
4689 /* else fall through */
4690 default:
4691 my_friendly_abort (390);
4693 return 0;
4696 /* Adds everything associated with arguments. Returns 1 on error. */
4698 static int
4699 arg_assoc_args (k, args)
4700 struct arg_lookup* k;
4701 tree args;
4703 for (; args; args = TREE_CHAIN (args))
4704 if (arg_assoc (k, TREE_VALUE (args)))
4705 return 1;
4706 return 0;
4709 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4711 static int
4712 arg_assoc (k, n)
4713 struct arg_lookup* k;
4714 tree n;
4716 if (n == error_mark_node)
4717 return 0;
4719 if (TYPE_P (n))
4720 return arg_assoc_type (k, n);
4722 if (! type_unknown_p (n))
4723 return arg_assoc_type (k, TREE_TYPE (n));
4725 if (TREE_CODE (n) == ADDR_EXPR)
4726 n = TREE_OPERAND (n, 0);
4727 if (TREE_CODE (n) == COMPONENT_REF)
4728 n = TREE_OPERAND (n, 1);
4729 if (TREE_CODE (n) == OFFSET_REF)
4730 n = TREE_OPERAND (n, 1);
4731 while (TREE_CODE (n) == TREE_LIST)
4732 n = TREE_VALUE (n);
4734 if (TREE_CODE (n) == FUNCTION_DECL)
4735 return arg_assoc_type (k, TREE_TYPE (n));
4736 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4738 /* [basic.lookup.koenig]
4740 If T is a template-id, its associated namespaces and classes
4741 are the namespace in which the template is defined; for
4742 member templates, the member template's class... */
4743 tree template = TREE_OPERAND (n, 0);
4744 tree args = TREE_OPERAND (n, 1);
4745 tree ctx;
4746 tree arg;
4748 if (TREE_CODE (template) == COMPONENT_REF)
4749 template = TREE_OPERAND (template, 1);
4751 /* First, the template. There may actually be more than one if
4752 this is an overloaded function template. But, in that case,
4753 we only need the first; all the functions will be in the same
4754 namespace. */
4755 template = OVL_CURRENT (template);
4757 ctx = CP_DECL_CONTEXT (template);
4759 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4761 if (arg_assoc_namespace (k, ctx) == 1)
4762 return 1;
4764 /* It must be a member template. */
4765 else if (arg_assoc_class (k, ctx) == 1)
4766 return 1;
4768 /* Now the arguments. */
4769 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4770 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4771 return 1;
4773 else
4775 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4777 for (; n; n = OVL_CHAIN (n))
4778 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4779 return 1;
4782 return 0;
4785 /* Performs Koenig lookup depending on arguments, where fns
4786 are the functions found in normal lookup. */
4788 tree
4789 lookup_arg_dependent (name, fns, args)
4790 tree name;
4791 tree fns;
4792 tree args;
4794 struct arg_lookup k;
4795 tree fn = NULL_TREE;
4797 k.name = name;
4798 k.functions = fns;
4799 k.classes = NULL_TREE;
4801 /* Note that we've already looked at some namespaces during normal
4802 unqualified lookup, unless we found a decl in function scope. */
4803 if (fns)
4804 fn = OVL_CURRENT (fns);
4805 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4806 k.namespaces = NULL_TREE;
4807 else
4808 unqualified_namespace_lookup (name, 0, &k.namespaces);
4810 arg_assoc_args (&k, args);
4811 return k.functions;
4814 /* Process a namespace-alias declaration. */
4816 void
4817 do_namespace_alias (alias, namespace)
4818 tree alias, namespace;
4820 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4822 /* The parser did not find it, so it's not there. */
4823 cp_error ("unknown namespace `%D'", namespace);
4824 return;
4827 namespace = ORIGINAL_NAMESPACE (namespace);
4829 /* Build the alias. */
4830 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4831 DECL_NAMESPACE_ALIAS (alias) = namespace;
4832 pushdecl (alias);
4835 /* Check a non-member using-declaration. Return the name and scope
4836 being used, and the USING_DECL, or NULL_TREE on failure. */
4838 static tree
4839 validate_nonmember_using_decl (decl, scope, name)
4840 tree decl;
4841 tree *scope;
4842 tree *name;
4844 if (TREE_CODE (decl) == SCOPE_REF)
4846 *scope = TREE_OPERAND (decl, 0);
4847 *name = TREE_OPERAND (decl, 1);
4849 if (!processing_template_decl)
4851 /* [namespace.udecl]
4852 A using-declaration for a class member shall be a
4853 member-declaration. */
4854 if(TREE_CODE (*scope) != NAMESPACE_DECL)
4856 if (TYPE_P (*scope))
4857 cp_error ("`%T' is not a namespace", *scope);
4858 else
4859 cp_error ("`%D' is not a namespace", *scope);
4860 return NULL_TREE;
4863 /* 7.3.3/5
4864 A using-declaration shall not name a template-id. */
4865 if (TREE_CODE (*name) == TEMPLATE_ID_EXPR)
4867 *name = TREE_OPERAND (*name, 0);
4868 cp_error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4869 return NULL_TREE;
4873 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4874 || TREE_CODE (decl) == TYPE_DECL
4875 || TREE_CODE (decl) == TEMPLATE_DECL)
4877 *scope = global_namespace;
4878 *name = decl;
4880 else if (TREE_CODE (decl) == NAMESPACE_DECL)
4882 cp_error ("namespace `%D' not allowed in using-declaration", decl);
4883 return NULL_TREE;
4885 else
4886 my_friendly_abort (382);
4887 if (DECL_P (*name))
4888 *name = DECL_NAME (*name);
4889 /* Make a USING_DECL. */
4890 return push_using_decl (*scope, *name);
4893 /* Process local and global using-declarations. */
4895 static void
4896 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4897 tree scope, name;
4898 tree oldval, oldtype;
4899 tree *newval, *newtype;
4901 tree decls;
4903 *newval = *newtype = NULL_TREE;
4904 decls = make_node (CPLUS_BINDING);
4905 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4906 /* Lookup error */
4907 return;
4909 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4911 cp_error ("`%D' not declared", name);
4912 return;
4915 /* Check for using functions. */
4916 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4918 tree tmp, tmp1;
4920 if (oldval && !is_overloaded_fn (oldval))
4922 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4923 oldval = NULL_TREE;
4926 *newval = oldval;
4927 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4929 tree new_fn = OVL_CURRENT (tmp);
4931 /* [namespace.udecl]
4933 If a function declaration in namespace scope or block
4934 scope has the same name and the same parameter types as a
4935 function introduced by a using declaration the program is
4936 ill-formed. */
4937 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4939 tree old_fn = OVL_CURRENT (tmp1);
4941 if (new_fn == old_fn)
4942 /* The function already exists in the current namespace. */
4943 break;
4944 else if (OVL_USED (tmp1))
4945 continue; /* this is a using decl */
4946 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4947 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4949 /* There was already a non-using declaration in
4950 this scope with the same parameter types. If both
4951 are the same extern "C" functions, that's ok. */
4952 if (!decls_match (new_fn, old_fn))
4953 cp_error ("`%D' is already declared in this scope", name);
4954 break;
4958 /* If we broke out of the loop, there's no reason to add
4959 this function to the using declarations for this
4960 scope. */
4961 if (tmp1)
4962 continue;
4964 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4965 if (TREE_CODE (*newval) != OVERLOAD)
4966 *newval = ovl_cons (*newval, NULL_TREE);
4967 OVL_USED (*newval) = 1;
4970 else
4972 *newval = BINDING_VALUE (decls);
4973 if (oldval)
4974 duplicate_decls (*newval, oldval);
4977 *newtype = BINDING_TYPE (decls);
4978 if (oldtype && *newtype && oldtype != *newtype)
4980 cp_error ("using declaration `%D' introduced ambiguous type `%T'",
4981 name, oldtype);
4982 return;
4986 /* Process a using-declaration not appearing in class or local scope. */
4988 void
4989 do_toplevel_using_decl (decl)
4990 tree decl;
4992 tree scope, name, binding;
4993 tree oldval, oldtype, newval, newtype;
4995 decl = validate_nonmember_using_decl (decl, &scope, &name);
4996 if (decl == NULL_TREE)
4997 return;
4999 binding = binding_for_name (name, current_namespace);
5001 oldval = BINDING_VALUE (binding);
5002 oldtype = BINDING_TYPE (binding);
5004 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5006 /* Copy declarations found. */
5007 if (newval)
5008 BINDING_VALUE (binding) = newval;
5009 if (newtype)
5010 BINDING_TYPE (binding) = newtype;
5011 return;
5014 /* Process a using-declaration at function scope. */
5016 void
5017 do_local_using_decl (decl)
5018 tree decl;
5020 tree scope, name;
5021 tree oldval, oldtype, newval, newtype;
5023 decl = validate_nonmember_using_decl (decl, &scope, &name);
5024 if (decl == NULL_TREE)
5025 return;
5027 if (building_stmt_tree ()
5028 && at_function_scope_p ())
5029 add_decl_stmt (decl);
5031 oldval = lookup_name_current_level (name);
5032 oldtype = lookup_type_current_level (name);
5034 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5036 if (newval)
5038 if (is_overloaded_fn (newval))
5040 tree fn, term;
5042 /* We only need to push declarations for those functions
5043 that were not already bound in the current level.
5044 The old value might be NULL_TREE, it might be a single
5045 function, or an OVERLOAD. */
5046 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5047 term = OVL_FUNCTION (oldval);
5048 else
5049 term = oldval;
5050 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5051 fn = OVL_NEXT (fn))
5052 push_overloaded_decl (OVL_CURRENT (fn),
5053 PUSH_LOCAL | PUSH_USING);
5055 else
5056 push_local_binding (name, newval, PUSH_USING);
5058 if (newtype)
5059 set_identifier_type_value (name, newtype);
5062 tree
5063 do_class_using_decl (decl)
5064 tree decl;
5066 tree name, value;
5068 if (TREE_CODE (decl) != SCOPE_REF
5069 || !TYPE_P (TREE_OPERAND (decl, 0)))
5071 cp_error ("using-declaration for non-member at class scope");
5072 return NULL_TREE;
5074 name = TREE_OPERAND (decl, 1);
5075 if (TREE_CODE (name) == BIT_NOT_EXPR)
5077 cp_error ("using-declaration for destructor");
5078 return NULL_TREE;
5080 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5082 name = TREE_OPERAND (name, 0);
5083 cp_error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
5084 return NULL_TREE;
5086 if (TREE_CODE (name) == TYPE_DECL || TREE_CODE (name) == TEMPLATE_DECL)
5087 name = DECL_NAME (name);
5089 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5091 value = build_lang_decl (USING_DECL, name, void_type_node);
5092 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5093 return value;
5096 /* Process a using-directive. */
5098 void
5099 do_using_directive (namespace)
5100 tree namespace;
5102 if (building_stmt_tree ())
5103 add_stmt (build_stmt (USING_STMT, namespace));
5105 /* using namespace A::B::C; */
5106 if (TREE_CODE (namespace) == SCOPE_REF)
5107 namespace = TREE_OPERAND (namespace, 1);
5108 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5110 /* Lookup in lexer did not find a namespace. */
5111 if (!processing_template_decl)
5112 cp_error ("namespace `%T' undeclared", namespace);
5113 return;
5115 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5117 if (!processing_template_decl)
5118 cp_error ("`%T' is not a namespace", namespace);
5119 return;
5121 namespace = ORIGINAL_NAMESPACE (namespace);
5122 if (!toplevel_bindings_p ())
5123 push_using_directive (namespace);
5124 else
5125 /* direct usage */
5126 add_using_namespace (current_namespace, namespace, 0);
5129 void
5130 check_default_args (x)
5131 tree x;
5133 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5134 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5135 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5137 if (TREE_PURPOSE (arg))
5138 saw_def = 1;
5139 else if (saw_def)
5141 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5142 i, x);
5143 break;
5148 void
5149 mark_used (decl)
5150 tree decl;
5152 TREE_USED (decl) = 1;
5153 if (processing_template_decl)
5154 return;
5155 assemble_external (decl);
5157 /* Is it a synthesized method that needs to be synthesized? */
5158 if (TREE_CODE (decl) == FUNCTION_DECL
5159 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5160 && DECL_ARTIFICIAL (decl)
5161 && ! DECL_INITIAL (decl)
5162 /* Kludge: don't synthesize for default args. */
5163 && current_function_decl)
5165 synthesize_method (decl);
5166 /* If we've already synthesized the method we don't need to
5167 instantiate it, so we can return right away. */
5168 return;
5171 /* If this is a function or variable that is an instance of some
5172 template, we now know that we will need to actually do the
5173 instantiation. We check that DECL is not an explicit
5174 instantiation because that is not checked in instantiate_decl. */
5175 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5176 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5177 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5178 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5179 instantiate_decl (decl, /*defer_ok=*/1);
5182 /* Helper function for named_class_head_sans_basetype nonterminal. We
5183 have just seen something of the form `AGGR SCOPE::ID'. Return a
5184 TYPE_DECL for the type declared by ID in SCOPE. */
5186 tree
5187 handle_class_head (aggr, scope, id)
5188 tree aggr, scope, id;
5190 tree decl = NULL_TREE;
5192 if (TREE_CODE (id) == TYPE_DECL)
5193 /* We must bash typedefs back to the main decl of the type. Otherwise
5194 we become confused about scopes. */
5195 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5196 else if (DECL_CLASS_TEMPLATE_P (id))
5197 decl = DECL_TEMPLATE_RESULT (id);
5198 else
5200 tree current = current_scope ();
5202 if (current == NULL_TREE)
5203 current = current_namespace;
5204 if (scope == NULL_TREE)
5205 scope = global_namespace;
5207 if (TYPE_P (scope))
5209 /* According to the suggested resolution of core issue 180,
5210 'typename' is assumed after a class-key. */
5211 decl = make_typename_type (scope, id, 1);
5212 if (decl != error_mark_node)
5213 decl = TYPE_MAIN_DECL (decl);
5214 else
5215 decl = NULL_TREE;
5217 else if (scope == current)
5219 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5220 Be nice about it. */
5221 if (pedantic)
5222 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5223 FROB_CONTEXT (scope), id);
5225 else if (scope != global_namespace)
5226 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5227 else
5228 cp_error ("no file-scope type named `%D'", id);
5230 /* Inject it at the current scope. */
5231 if (! decl)
5232 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, 1));
5235 /* Enter the SCOPE. If this turns out not to be a definition, the
5236 parser must leave the scope. */
5237 push_scope (CP_DECL_CONTEXT (decl));
5239 /* If we see something like:
5241 template <typename T> struct S::I ....
5243 we must create a TEMPLATE_DECL for the nested type. */
5244 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5245 decl = push_template_decl (decl);
5247 return decl;
5250 /* Initialize decl2.c. */
5252 void
5253 init_decl2 ()
5255 ggc_add_tree_varray_root (&deferred_fns, 1);
5256 ggc_add_tree_varray_root (&pending_statics, 1);
5257 ggc_add_tree_varray_root (&ssdf_decls, 1);
5258 ggc_add_tree_root (&ssdf_decl, 1);
5259 ggc_add_tree_root (&priority_decl, 1);
5260 ggc_add_tree_root (&initialize_p_decl, 1);