1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
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. */
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. */
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,
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. */
115 /* Functions called along with real static constructors and destructors. */
120 /* The :: namespace. */
122 tree global_namespace;
124 /* C (and C++) language-specific option variables. */
126 /* Nonzero means don't recognize the keyword `asm'. */
130 /* Nonzero means don't recognize any extension keywords. */
132 int flag_no_gnu_keywords;
134 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
136 int flag_signed_bitfields = 1;
138 /* Nonzero means enable obscure standard features and disable GNU
139 extensions that might cause standard-compliant code to be
144 /* Nonzero means do emit exported implementations of functions even if
145 they can be inlined. */
147 int flag_implement_inlines = 1;
149 /* Nonzero means do emit exported implementations of templates, instead of
150 multiple static copies in each file that needs a definition. */
152 int flag_external_templates;
154 /* Nonzero means that the decision to emit or not emit the implementation of a
155 template depends on where the template is instantiated, rather than where
158 int flag_alt_external_templates;
160 /* Nonzero means that implicit instantiations will be emitted if needed. */
162 int flag_implicit_templates = 1;
164 /* Nonzero means that implicit instantiations of inline templates will be
165 emitted if needed, even if instantiations of non-inline templates
168 int flag_implicit_inline_templates = 1;
170 /* Nonzero means warn about implicit declarations. */
172 int warn_implicit = 1;
174 /* Nonzero means warn about usage of long long when `-pedantic'. */
176 int warn_long_long = 1;
178 /* Nonzero means warn when all ctors or dtors are private, and the class
181 int warn_ctor_dtor_privacy = 1;
183 /* Nonzero means generate separate instantiation control files and juggle
184 them at link time. */
186 int flag_use_repository;
188 /* Nonzero if we want to issue diagnostics that the standard says are not
191 int flag_optional_diags = 1;
193 /* Nonzero means give string constants the type `const char *', as mandated
196 int flag_const_strings = 1;
198 /* Nonzero means warn about deprecated conversion from string constant to
201 int warn_write_strings;
203 /* Nonzero means warn about pointer casts that can drop a type qualifier
204 from the pointer target type. */
208 /* Nonzero means warn about sizeof(function) or addition/subtraction
209 of function pointers. */
211 int warn_pointer_arith = 1;
213 /* Nonzero means warn for any function def without prototype decl. */
215 int warn_missing_prototypes;
217 /* Nonzero means warn about multiple (redundant) decls for the same single
218 variable or function. */
220 int warn_redundant_decls;
222 /* Warn if initializer is not completely bracketed. */
224 int warn_missing_braces;
226 /* Warn about comparison of signed and unsigned values. */
228 int warn_sign_compare;
230 /* Warn about testing equality of floating point numbers. */
232 int warn_float_equal = 0;
234 /* Warn about functions which might be candidates for format attributes. */
236 int warn_missing_format_attribute;
238 /* Warn about a subscript that has type char. */
240 int warn_char_subscripts;
242 /* Warn if a type conversion is done that might have confusing results. */
246 /* Warn if adding () is suggested. */
248 int warn_parentheses;
250 /* Non-zero means warn in function declared in derived class has the
251 same name as a virtual in the base class, but fails to match the
252 type signature of any virtual function in the base class. */
254 int warn_overloaded_virtual;
256 /* Non-zero means warn when declaring a class that has a non virtual
257 destructor, when it really ought to have a virtual one. */
261 /* Non-zero means warn when the compiler will reorder code. */
265 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
269 /* Non-zero means warn when we convert a pointer to member function
270 into a pointer to (void or function). */
272 int warn_pmf2ptr = 1;
274 /* Nonzero means warn about violation of some Effective C++ style rules. */
278 /* Nonzero means warn where overload resolution chooses a promotion from
279 unsigned to signed over a conversion to an unsigned of the same size. */
283 /* Nonzero means warn when an old-style cast is used. */
285 int warn_old_style_cast;
287 /* Warn about #pragma directives that are not recognised. */
289 int warn_unknown_pragmas; /* Tri state variable. */
291 /* Nonzero means warn about use of multicharacter literals. */
293 int warn_multichar = 1;
295 /* Nonzero means warn when non-templatized friend functions are
296 declared within a template */
298 int warn_nontemplate_friend = 1;
300 /* Nonzero means complain about deprecated features. */
302 int warn_deprecated = 1;
304 /* Nonzero means `$' can be in an identifier. */
306 #ifndef DOLLARS_IN_IDENTIFIERS
307 #define DOLLARS_IN_IDENTIFIERS 1
309 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
311 /* Nonzero means allow Microsoft extensions without a pedwarn. */
313 int flag_ms_extensions;
315 /* C++ specific flags. */
317 /* Nonzero means we should attempt to elide constructors when possible. */
319 int flag_elide_constructors = 1;
321 /* Nonzero means that member functions defined in class scope are
322 inline by default. */
324 int flag_default_inline = 1;
326 /* Controls whether compiler generates 'type descriptor' that give
327 run-time type information. */
331 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
334 int flag_huge_objects;
336 /* Nonzero if we want to conserve space in the .o files. We do this
337 by putting uninitialized data and runtime initialized data into
338 .common instead of .data at the expense of not flagging multiple
341 int flag_conserve_space;
343 /* Nonzero if we want to obey access control semantics. */
345 int flag_access_control = 1;
347 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
349 int flag_operator_names = 1;
351 /* Nonzero if we want to check the return value of new and avoid calling
352 constructors if it is a null pointer. */
356 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
357 initialization variables.
358 0: Old rules, set by -fno-for-scope.
359 2: New ISO rules, set by -ffor-scope.
360 1: Try to implement new ISO rules, but with backup compatibility
361 (and warnings). This is the default, for now. */
363 int flag_new_for_scope = 1;
365 /* Nonzero if we want to emit defined symbols with common-like linkage as
366 weak symbols where possible, in order to conform to C++ semantics.
367 Otherwise, emit them as local symbols. */
371 /* Nonzero to use __cxa_atexit, rather than atexit, to register
372 destructors for local statics and global objects. */
374 int flag_use_cxa_atexit;
376 /* Maximum template instantiation depth. This limit is rather
377 arbitrary, but it exists to limit the time it takes to notice
378 infinite template instantiations. */
380 int max_tinst_depth = 500;
382 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
386 /* Nonzero means make the default pedwarns warnings instead of errors.
387 The value of this flag is ignored if -pedantic is specified. */
391 /* Nonzero means to implement standard semantics for exception
392 specifications, calling unexpected if an exception is thrown that
393 doesn't match the specification. Zero means to treat them as
394 assertions and optimize accordingly, but not check them. */
396 int flag_enforce_eh_specs = 1;
398 /* Table of language-dependent -f options.
399 STRING is the option name. VARIABLE is the address of the variable.
400 ON_VALUE is the value to store in VARIABLE
401 if `-fSTRING' is seen as an option.
402 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
404 static const struct { const char *const string; int *const variable; const int on_value;}
408 {"signed-char", &flag_signed_char, 1},
409 {"unsigned-char", &flag_signed_char, 0},
410 {"signed-bitfields", &flag_signed_bitfields, 1},
411 {"unsigned-bitfields", &flag_signed_bitfields, 0},
412 {"short-enums", &flag_short_enums, 1},
413 {"short-double", &flag_short_double, 1},
414 {"short-wchar", &flag_short_wchar, 1},
415 {"asm", &flag_no_asm, 0},
416 {"builtin", &flag_no_builtin, 0},
418 /* C++-only options. */
419 {"access-control", &flag_access_control, 1},
420 {"check-new", &flag_check_new, 1},
421 {"conserve-space", &flag_conserve_space, 1},
422 {"const-strings", &flag_const_strings, 1},
423 {"default-inline", &flag_default_inline, 1},
424 {"dollars-in-identifiers", &dollars_in_ident, 1},
425 {"elide-constructors", &flag_elide_constructors, 1},
426 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
427 {"external-templates", &flag_external_templates, 1},
428 {"for-scope", &flag_new_for_scope, 2},
429 {"gnu-keywords", &flag_no_gnu_keywords, 0},
430 {"handle-exceptions", &flag_exceptions, 1},
431 {"implement-inlines", &flag_implement_inlines, 1},
432 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
433 {"implicit-templates", &flag_implicit_templates, 1},
434 {"ms-extensions", &flag_ms_extensions, 1},
435 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
436 {"operator-names", &flag_operator_names, 1},
437 {"optional-diags", &flag_optional_diags, 1},
438 {"permissive", &flag_permissive, 1},
439 {"repo", &flag_use_repository, 1},
440 {"rtti", &flag_rtti, 1},
441 {"stats", &flag_detailed_statistics, 1},
442 {"vtable-gc", &flag_vtable_gc, 1},
443 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
444 {"weak", &flag_weak, 1}
447 /* The list of `-f' options that we no longer support. The `-f'
448 prefix is not given in this table. The `-fno-' variants are not
449 listed here. This table must be kept in alphabetical order. */
450 static const char * const unsupported_options[] = {
467 /* Compare two option strings, pointed two by P1 and P2, for use with
471 compare_options (p1, p2)
475 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
478 /* Decode the string P as a language-specific option.
479 Return the number of strings consumed for a valid option.
480 Otherwise return 0. Should not complain if it does not
481 recognise the option. */
484 cxx_decode_option (argc, argv)
488 int strings_processed;
489 const char *p = argv[0];
491 strings_processed = cpp_handle_option (parse_in, argc, argv, 0);
493 if (p[0] == '-' && p[1] == 'f')
495 /* Some kind of -f option.
496 P's value is the option sans `-f'.
497 Search for it in the table of options. */
498 const char *option_value = NULL;
499 const char *positive_option;
503 /* Try special -f options. */
505 /* See if this is one of the options no longer supported. We
506 used to support these options, so we continue to accept them,
508 if (strncmp (p, "no-", strlen ("no-")) == 0)
509 positive_option = p + strlen ("no-");
513 /* If the option is present, issue a warning. Indicate to our
514 caller that the option was processed successfully. */
515 if (bsearch (&positive_option,
517 (sizeof (unsupported_options)
518 / sizeof (unsupported_options[0])),
519 sizeof (unsupported_options[0]),
522 warning ("-f%s is no longer supported", p);
526 if (!strcmp (p, "handle-exceptions")
527 || !strcmp (p, "no-handle-exceptions"))
528 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
529 else if (! strcmp (p, "alt-external-templates"))
531 flag_external_templates = 1;
532 flag_alt_external_templates = 1;
533 cp_deprecated ("-falt-external-templates");
535 else if (! strcmp (p, "no-alt-external-templates"))
536 flag_alt_external_templates = 0;
537 else if (!strcmp (p, "repo"))
539 flag_use_repository = 1;
540 flag_implicit_templates = 0;
542 else if (!strcmp (p, "external-templates"))
544 flag_external_templates = 1;
545 cp_deprecated ("-fexternal-templates");
547 else if ((option_value
548 = skip_leading_substring (p, "template-depth-")))
550 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
551 else if ((option_value
552 = skip_leading_substring (p, "name-mangling-version-")))
554 warning ("-fname-mangling-version is no longer supported");
557 else if (dump_switch_p (p))
564 !found && j < (sizeof (lang_f_options)
565 / sizeof (lang_f_options[0]));
568 if (!strcmp (p, lang_f_options[j].string))
570 *lang_f_options[j].variable = lang_f_options[j].on_value;
571 /* A goto here would be cleaner,
572 but breaks the VAX pcc. */
575 else if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
576 && ! strcmp (p+3, lang_f_options[j].string))
578 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
586 else if (p[0] == '-' && p[1] == 'W')
590 /* The -W options control the warning behavior of the compiler. */
593 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
596 if (!strcmp (p, "implicit"))
597 warn_implicit = setting;
598 else if (!strcmp (p, "long-long"))
599 warn_long_long = setting;
600 else if (!strcmp (p, "return-type"))
601 warn_return_type = setting;
602 else if (!strcmp (p, "ctor-dtor-privacy"))
603 warn_ctor_dtor_privacy = setting;
604 else if (!strcmp (p, "write-strings"))
605 warn_write_strings = setting;
606 else if (!strcmp (p, "cast-qual"))
607 warn_cast_qual = setting;
608 else if (!strcmp (p, "char-subscripts"))
609 warn_char_subscripts = setting;
610 else if (!strcmp (p, "pointer-arith"))
611 warn_pointer_arith = setting;
612 else if (!strcmp (p, "missing-prototypes"))
613 warn_missing_prototypes = setting;
614 else if (!strcmp (p, "strict-prototypes"))
617 warning ("-Wno-strict-prototypes is not supported in C++");
619 else if (!strcmp (p, "redundant-decls"))
620 warn_redundant_decls = setting;
621 else if (!strcmp (p, "missing-braces"))
622 warn_missing_braces = setting;
623 else if (!strcmp (p, "sign-compare"))
624 warn_sign_compare = setting;
625 else if (!strcmp (p, "float-equal"))
626 warn_float_equal = setting;
627 else if (!strcmp (p, "format"))
628 set_Wformat (setting);
629 else if (!strcmp (p, "format=2"))
631 else if (!strcmp (p, "format-y2k"))
632 warn_format_y2k = setting;
633 else if (!strcmp (p, "format-extra-args"))
634 warn_format_extra_args = setting;
635 else if (!strcmp (p, "format-nonliteral"))
636 warn_format_nonliteral = setting;
637 else if (!strcmp (p, "format-security"))
638 warn_format_security = setting;
639 else if (!strcmp (p, "missing-format-attribute"))
640 warn_missing_format_attribute = setting;
641 else if (!strcmp (p, "conversion"))
642 warn_conversion = setting;
643 else if (!strcmp (p, "parentheses"))
644 warn_parentheses = setting;
645 else if (!strcmp (p, "non-virtual-dtor"))
646 warn_nonvdtor = setting;
647 else if (!strcmp (p, "reorder"))
648 warn_reorder = setting;
649 else if (!strcmp (p, "synth"))
650 warn_synth = setting;
651 else if (!strcmp (p, "pmf-conversions"))
652 warn_pmf2ptr = setting;
653 else if (!strcmp (p, "effc++"))
655 else if (!strcmp (p, "sign-promo"))
656 warn_sign_promo = setting;
657 else if (!strcmp (p, "old-style-cast"))
658 warn_old_style_cast = setting;
659 else if (!strcmp (p, "overloaded-virtual"))
660 warn_overloaded_virtual = setting;
661 else if (!strcmp (p, "multichar"))
662 warn_multichar = setting;
663 else if (!strcmp (p, "unknown-pragmas"))
664 /* Set to greater than 1, so that even unknown pragmas in
665 system headers will be warned about. */
666 warn_unknown_pragmas = setting * 2;
667 else if (!strcmp (p, "non-template-friend"))
668 warn_nontemplate_friend = setting;
669 else if (!strcmp (p, "deprecated"))
670 warn_deprecated = setting;
671 else if (!strcmp (p, "comment"))
672 ; /* cpp handles this one. */
673 else if (!strcmp (p, "comments"))
674 ; /* cpp handles this one. */
675 else if (!strcmp (p, "trigraphs"))
676 ; /* cpp handles this one. */
677 else if (!strcmp (p, "import"))
678 ; /* cpp handles this one. */
679 else if (!strcmp (p, "all"))
681 warn_return_type = setting;
682 set_Wunused (setting);
683 warn_implicit = setting;
684 warn_switch = setting;
685 set_Wformat (setting);
686 warn_parentheses = setting;
687 warn_missing_braces = setting;
688 warn_sign_compare = setting;
689 warn_multichar = setting;
690 /* We save the value of warn_uninitialized, since if they put
691 -Wuninitialized on the command line, we need to generate a
692 warning about not using it without also specifying -O. */
693 if (warn_uninitialized != 1)
694 warn_uninitialized = (setting ? 2 : 0);
695 /* Only warn about unknown pragmas that are not in system
697 warn_unknown_pragmas = 1;
699 /* C++-specific warnings. */
700 warn_ctor_dtor_privacy = setting;
701 warn_nonvdtor = setting;
702 warn_reorder = setting;
703 warn_nontemplate_friend = setting;
705 else return strings_processed;
707 else if (!strcmp (p, "-ansi"))
708 flag_no_nonansi_builtin = 1, flag_ansi = 1,
709 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
711 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
712 it's probably safe to assume no sane person would ever want to use this
713 under normal circumstances. */
714 else if (!strcmp (p, "-spew-debug"))
718 return strings_processed;
723 /* Incorporate `const' and `volatile' qualifiers for member functions.
724 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
725 QUALS is a list of qualifiers. Returns any explicit
726 top-level qualifiers of the method's this pointer, anything other than
727 TYPE_UNQUALIFIED will be an extension. */
730 grok_method_quals (ctype, function, quals)
731 tree ctype, function, quals;
733 tree fntype = TREE_TYPE (function);
734 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
735 int type_quals = TYPE_UNQUALIFIED;
736 int dup_quals = TYPE_UNQUALIFIED;
737 int this_quals = TYPE_UNQUALIFIED;
741 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
743 if ((type_quals | this_quals) & tq)
745 else if (tq & TYPE_QUAL_RESTRICT)
749 quals = TREE_CHAIN (quals);
753 if (dup_quals != TYPE_UNQUALIFIED)
754 error ("duplicate type qualifiers in %s declaration",
755 TREE_CODE (function) == FUNCTION_DECL
756 ? "member function" : "type");
758 ctype = cp_build_qualified_type (ctype, type_quals);
759 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
760 (TREE_CODE (fntype) == METHOD_TYPE
761 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
762 : TYPE_ARG_TYPES (fntype)));
764 fntype = build_exception_variant (fntype, raises);
766 TREE_TYPE (function) = fntype;
770 /* Warn when -fexternal-templates is used and #pragma
771 interface/implementation is not used all the times it should be,
775 warn_if_unknown_interface (decl)
778 static int already_warned = 0;
779 if (already_warned++)
782 if (flag_alt_external_templates)
784 tree til = tinst_for_decl ();
786 const char *sf = input_filename;
790 lineno = TINST_LINE (til);
791 input_filename = TINST_FILE (til);
793 warning ("template `%#D' instantiated in file without #pragma interface",
799 cp_warning_at ("template `%#D' defined in file without #pragma interface",
803 /* A subroutine of the parser, to handle a component list. */
806 grok_x_components (specs)
811 specs = strip_attrs (specs);
813 check_tag_decl (specs);
814 t = groktypename (build_tree_list (specs, NULL_TREE));
816 /* The only case where we need to do anything additional here is an
817 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
818 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
821 fixup_anonymous_aggr (t);
822 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
825 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
829 build_artificial_parm (name, type)
835 parm = build_decl (PARM_DECL, name, type);
836 DECL_ARTIFICIAL (parm) = 1;
837 /* All our artificial parms are implicitly `const'; they cannot be
839 TREE_READONLY (parm) = 1;
840 DECL_ARG_TYPE (parm) = type;
844 /* Constructors for types with virtual baseclasses need an "in-charge" flag
845 saying whether this constructor is responsible for initialization of
846 virtual baseclasses or not. All destructors also need this "in-charge"
847 flag, which additionally determines whether or not the destructor should
848 free the memory for the object.
850 This function adds the "in-charge" flag to member function FN if
851 appropriate. It is called from grokclassfn and tsubst.
852 FN must be either a constructor or destructor.
854 The in-charge flag follows the 'this' parameter, and is followed by the
855 VTT parm (if any), then the user-written parms. */
858 maybe_retrofit_in_chrg (fn)
861 tree basetype, arg_types, parms, parm, fntype;
863 /* If we've already add the in-charge parameter don't do it again. */
864 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
867 /* When processing templates we can't know, in general, whether or
868 not we're going to have virtual baseclasses. */
869 if (uses_template_parms (fn))
872 /* We don't need an in-charge parameter for constructors that don't
873 have virtual bases. */
874 if (DECL_CONSTRUCTOR_P (fn)
875 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
878 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
879 basetype = TREE_TYPE (TREE_VALUE (arg_types));
880 arg_types = TREE_CHAIN (arg_types);
882 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
884 /* If this is a subobject constructor or destructor, our caller will
885 pass us a pointer to our VTT. */
886 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
888 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
890 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
891 TREE_CHAIN (parm) = parms;
894 /* ...and then to TYPE_ARG_TYPES. */
895 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
897 DECL_HAS_VTT_PARM_P (fn) = 1;
900 /* Then add the in-charge parm (before the VTT parm). */
901 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
902 TREE_CHAIN (parm) = parms;
904 arg_types = hash_tree_chain (integer_type_node, arg_types);
906 /* Insert our new parameter(s) into the list. */
907 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
909 /* And rebuild the function type. */
910 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
912 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
913 fntype = build_exception_variant (fntype,
914 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
915 TREE_TYPE (fn) = fntype;
917 /* Now we've got the in-charge parameter. */
918 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
921 /* Classes overload their constituent function names automatically.
922 When a function name is declared in a record structure,
923 its name is changed to it overloaded name. Since names for
924 constructors and destructors can conflict, we place a leading
927 CNAME is the name of the class we are grokking for.
929 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
931 FLAGS contains bits saying what's special about today's
932 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
934 If FUNCTION is a destructor, then we must add the `auto-delete' field
935 as a second parameter. There is some hair associated with the fact
936 that we must "declare" this variable in the manner consistent with the
937 way the rest of the arguments were declared.
939 QUALS are the qualifiers for the this pointer. */
942 grokclassfn (ctype, function, flags, quals)
943 tree ctype, function;
944 enum overload_flags flags;
947 tree fn_name = DECL_NAME (function);
948 int this_quals = TYPE_UNQUALIFIED;
950 /* Even within an `extern "C"' block, members get C++ linkage. See
951 [dcl.link] for details. */
952 SET_DECL_LANGUAGE (function, lang_cplusplus);
954 if (fn_name == NULL_TREE)
956 error ("name missing for member function");
957 fn_name = get_identifier ("<anonymous>");
958 DECL_NAME (function) = fn_name;
962 this_quals = grok_method_quals (ctype, function, quals);
964 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
966 /* Must add the class instance variable up front. */
967 /* Right now we just make this a pointer. But later
968 we may wish to make it special. */
969 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
973 /* The `this' parameter is implicitly `const'; it cannot be
975 this_quals |= TYPE_QUAL_CONST;
976 qual_type = cp_build_qualified_type (type, this_quals);
977 parm = build_artificial_parm (this_identifier, qual_type);
978 c_apply_type_quals_to_decl (this_quals, parm);
979 TREE_CHAIN (parm) = last_function_parms;
980 last_function_parms = parm;
983 DECL_ARGUMENTS (function) = last_function_parms;
984 DECL_CONTEXT (function) = ctype;
986 if (flags == DTOR_FLAG)
987 DECL_DESTRUCTOR_P (function) = 1;
989 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
990 maybe_retrofit_in_chrg (function);
992 if (flags == DTOR_FLAG)
994 DECL_DESTRUCTOR_P (function) = 1;
995 TYPE_HAS_DESTRUCTOR (ctype) = 1;
999 /* Create an ARRAY_REF, checking for the user doing things backwards
1003 grok_array_decl (array_expr, index_exp)
1004 tree array_expr, index_exp;
1006 tree type = TREE_TYPE (array_expr);
1007 tree p1, p2, i1, i2;
1009 if (type == error_mark_node || index_exp == error_mark_node)
1010 return error_mark_node;
1011 if (processing_template_decl)
1012 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1013 array_expr, index_exp);
1015 if (type == NULL_TREE)
1017 /* Something has gone very wrong. Assume we are mistakenly reducing
1018 an expression instead of a declaration. */
1019 error ("parser may be lost: is there a '{' missing somewhere?");
1023 if (TREE_CODE (type) == OFFSET_TYPE
1024 || TREE_CODE (type) == REFERENCE_TYPE)
1025 type = TREE_TYPE (type);
1027 /* If they have an `operator[]', use that. */
1028 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1029 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1030 array_expr, index_exp, NULL_TREE);
1032 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1033 is a little-known fact that, if `a' is an array and `i' is an
1034 int, you can write `i[a]', which means the same thing as `a[i]'. */
1036 if (TREE_CODE (type) == ARRAY_TYPE)
1039 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1041 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1044 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1046 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1047 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1049 if ((p1 && i2) && (i1 && p2))
1050 error ("ambiguous conversion for array subscript");
1053 array_expr = p1, index_exp = i2;
1055 array_expr = p2, index_exp = i1;
1058 error ("invalid types `%T[%T]' for array subscript",
1059 type, TREE_TYPE (index_exp));
1060 return error_mark_node;
1063 if (array_expr == error_mark_node || index_exp == error_mark_node)
1064 error ("ambiguous conversion for array subscript");
1066 return build_array_ref (array_expr, index_exp);
1069 /* Given the cast expression EXP, checking out its validity. Either return
1070 an error_mark_node if there was an unavoidable error, return a cast to
1071 void for trying to delete a pointer w/ the value 0, or return the
1072 call to delete. If DOING_VEC is 1, we handle things differently
1073 for doing an array delete. If DOING_VEC is 2, they gave us the
1074 array size as an argument to delete.
1075 Implements ARM $5.3.4. This is called from the parser. */
1078 delete_sanity (exp, size, doing_vec, use_global_delete)
1080 int doing_vec, use_global_delete;
1083 /* For a regular vector delete (aka, no size argument) we will pass
1084 this down as a NULL_TREE into build_vec_delete. */
1085 tree maxindex = NULL_TREE;
1087 if (exp == error_mark_node)
1090 if (processing_template_decl)
1092 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1093 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1094 DELETE_EXPR_USE_VEC (t) = doing_vec;
1098 if (TREE_CODE (exp) == OFFSET_REF)
1099 exp = resolve_offset_ref (exp);
1100 exp = convert_from_reference (exp);
1101 t = stabilize_reference (exp);
1102 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1104 if (t == NULL_TREE || t == error_mark_node)
1106 error ("type `%#T' argument given to `delete', expected pointer",
1108 return error_mark_node;
1113 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1114 pedwarn ("anachronistic use of array size in vector delete");
1117 type = TREE_TYPE (t);
1119 /* As of Valley Forge, you can delete a pointer to const. */
1121 /* You can't delete functions. */
1122 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1124 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1125 return error_mark_node;
1128 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1129 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1131 warning ("deleting `%T' is undefined", type);
1135 /* An array can't have been allocated by new, so complain. */
1136 if (TREE_CODE (t) == ADDR_EXPR
1137 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1138 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1139 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1141 /* Deleting a pointer with the value zero is valid and has no effect. */
1142 if (integer_zerop (t))
1143 return build1 (NOP_EXPR, void_type_node, t);
1146 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1150 if (IS_AGGR_TYPE (TREE_TYPE (type))
1151 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1153 /* Only do access checking here; we'll be calling op delete
1154 from the destructor. */
1155 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1156 LOOKUP_NORMAL, NULL_TREE);
1157 if (tmp == error_mark_node)
1158 return error_mark_node;
1161 return build_delete (type, t, sfk_deleting_destructor,
1162 LOOKUP_NORMAL, use_global_delete);
1166 /* Report an error if the indicated template declaration is not the
1167 sort of thing that should be a member template. */
1170 check_member_template (tmpl)
1175 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1176 decl = DECL_TEMPLATE_RESULT (tmpl);
1178 if (TREE_CODE (decl) == FUNCTION_DECL
1179 || (TREE_CODE (decl) == TYPE_DECL
1180 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1182 if (current_function_decl)
1183 /* 14.5.2.2 [temp.mem]
1185 A local class shall not have member templates. */
1186 error ("invalid declaration of member template `%#D' in local class",
1189 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1191 /* 14.5.2.3 [temp.mem]
1193 A member function template shall not be virtual. */
1195 ("invalid use of `virtual' in template declaration of `%#D'",
1197 DECL_VIRTUAL_P (decl) = 0;
1200 /* The debug-information generating code doesn't know what to do
1201 with member templates. */
1202 DECL_IGNORED_P (tmpl) = 1;
1205 error ("template declaration of `%#D'", decl);
1208 /* Return true iff TYPE is a valid Java parameter or return type. */
1211 acceptable_java_type (type)
1214 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1216 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1218 type = TREE_TYPE (type);
1219 if (TREE_CODE (type) == RECORD_TYPE)
1222 if (! TYPE_FOR_JAVA (type))
1224 if (! CLASSTYPE_TEMPLATE_INFO (type))
1226 args = CLASSTYPE_TI_ARGS (type);
1227 i = TREE_VEC_LENGTH (args);
1230 type = TREE_VEC_ELT (args, i);
1231 if (TREE_CODE (type) == POINTER_TYPE)
1232 type = TREE_TYPE (type);
1233 if (! TYPE_FOR_JAVA (type))
1242 /* For a METHOD in a Java class CTYPE, return 1 if
1243 the parameter and return types are valid Java types.
1244 Otherwise, print appropriate error messages, and return 0. */
1247 check_java_method (method)
1251 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1252 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1253 if (! acceptable_java_type (ret_type))
1255 error ("Java method '%D' has non-Java return type `%T'",
1259 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1261 tree type = TREE_VALUE (arg_types);
1262 if (! acceptable_java_type (type))
1264 error ("Java method '%D' has non-Java parameter type `%T'",
1269 return jerr ? 0 : 1;
1272 /* Sanity check: report error if this function FUNCTION is not
1273 really a member of the class (CTYPE) it is supposed to belong to.
1274 CNAME is the same here as it is for grokclassfn above. */
1277 check_classfn (ctype, function)
1278 tree ctype, function;
1280 tree fn_name = DECL_NAME (function);
1281 tree fndecl, fndecls;
1282 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1286 if (DECL_USE_TEMPLATE (function)
1287 && !(TREE_CODE (function) == TEMPLATE_DECL
1288 && DECL_TEMPLATE_SPECIALIZATION (function))
1289 && is_member_template (DECL_TI_TEMPLATE (function)))
1290 /* Since this is a specialization of a member template,
1291 we're not going to find the declaration in the class.
1294 struct S { template <typename T> void f(T); };
1295 template <> void S::f(int);
1297 we're not going to find `S::f(int)', but there's no
1298 reason we should, either. We let our callers know we didn't
1299 find the method, but we don't complain. */
1302 if (method_vec != 0)
1304 methods = &TREE_VEC_ELT (method_vec, 0);
1305 end = TREE_VEC_END (method_vec);
1307 /* First suss out ctors and dtors. */
1308 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1309 && DECL_CONSTRUCTOR_P (function))
1311 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1312 && DECL_DESTRUCTOR_P (function))
1315 while (++methods != end && *methods)
1318 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1321 for (fndecls = *methods; fndecls != NULL_TREE;
1322 fndecls = OVL_NEXT (fndecls))
1324 fndecl = OVL_CURRENT (fndecls);
1326 /* We cannot simply call decls_match because this
1327 doesn't work for static member functions that are
1328 pretending to be methods, and because the name
1329 may have been changed by asm("new_name"). */
1330 if (DECL_NAME (function) == DECL_NAME (fndecl))
1332 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1333 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1335 /* Get rid of the this parameter on functions that become
1337 if (DECL_STATIC_FUNCTION_P (fndecl)
1338 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1339 p1 = TREE_CHAIN (p1);
1341 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1342 TREE_TYPE (TREE_TYPE (fndecl)))
1343 && compparms (p1, p2)
1344 && (DECL_TEMPLATE_SPECIALIZATION (function)
1345 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1346 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1347 || (DECL_TI_TEMPLATE (function)
1348 == DECL_TI_TEMPLATE (fndecl))))
1357 if (methods != end && *methods)
1359 tree fndecl = *methods;
1360 error ("prototype for `%#D' does not match any in class `%T'",
1362 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1363 OVL_CURRENT (fndecl));
1364 while (fndecl = OVL_NEXT (fndecl), fndecl)
1365 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1370 if (!COMPLETE_TYPE_P (ctype))
1371 incomplete_type_error (function, ctype);
1373 error ("no `%#D' member function declared in class `%T'",
1377 /* If we did not find the method in the class, add it to avoid
1378 spurious errors (unless the CTYPE is not yet defined, in which
1379 case we'll only confuse ourselves when the function is declared
1380 properly within the class. */
1381 if (COMPLETE_TYPE_P (ctype))
1382 add_method (ctype, function, /*error_p=*/1);
1386 /* We have just processed the DECL, which is a static data member.
1387 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1388 present, is the assembly-language name for the data member.
1389 FLAGS is as for cp_finish_decl. */
1392 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1398 my_friendly_assert (TREE_PUBLIC (decl), 0);
1400 DECL_CONTEXT (decl) = current_class_type;
1402 /* We cannot call pushdecl here, because that would fill in the
1403 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1404 the right thing, namely, to put this decl out straight away. */
1405 /* current_class_type can be NULL_TREE in case of error. */
1406 if (!asmspec_tree && current_class_type)
1407 DECL_INITIAL (decl) = error_mark_node;
1409 if (! processing_template_decl)
1411 if (!pending_statics)
1412 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1413 VARRAY_PUSH_TREE (pending_statics, decl);
1416 if (LOCAL_CLASS_P (current_class_type))
1417 pedwarn ("local class `%#T' shall not have static data member `%#D'",
1418 current_class_type, decl);
1420 /* Static consts need not be initialized in the class definition. */
1421 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1423 static int explained = 0;
1425 error ("initializer invalid for static member with constructor");
1428 error ("(an out of class initialization is required)");
1433 /* Force the compiler to know when an uninitialized static const
1434 member is being used. */
1435 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1436 TREE_USED (decl) = 1;
1437 DECL_INITIAL (decl) = init;
1438 DECL_IN_AGGR_P (decl) = 1;
1440 cp_finish_decl (decl, init, asmspec_tree, flags);
1443 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1444 of a structure component, returning a _DECL node.
1445 QUALS is a list of type qualifiers for this decl (such as for declaring
1446 const member functions).
1448 This is done during the parsing of the struct declaration.
1449 The _DECL nodes are chained together and the lot of them
1450 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1452 If class A defines that certain functions in class B are friends, then
1453 the way I have set things up, it is B who is interested in permission
1454 granted by A. However, it is in A's context that these declarations
1455 are parsed. By returning a void_type_node, class A does not attempt
1456 to incorporate the declarations of the friends within its structure.
1458 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1459 CHANGES TO CODE IN `start_method'. */
1462 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1463 tree declarator, declspecs, init, asmspec_tree, attrlist;
1466 const char *asmspec = 0;
1467 int flags = LOOKUP_ONLYCONVERTING;
1469 /* Convert () initializers to = initializers. */
1470 if (init == NULL_TREE && declarator != NULL_TREE
1471 && TREE_CODE (declarator) == CALL_EXPR
1472 && TREE_OPERAND (declarator, 0)
1473 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1474 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1475 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1477 /* It's invalid to try to initialize a data member using a
1478 functional notation, e.g.:
1484 Explain that to the user. */
1485 static int explained;
1487 error ("invalid data member initialization");
1490 error ("(use `=' to initialize static data members)");
1494 declarator = TREE_OPERAND (declarator, 0);
1498 if (declspecs == NULL_TREE
1499 && TREE_CODE (declarator) == SCOPE_REF
1500 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1502 /* Access declaration */
1503 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1505 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1506 pop_nested_class ();
1507 return do_class_using_decl (declarator);
1511 && TREE_CODE (init) == TREE_LIST
1512 && TREE_VALUE (init) == error_mark_node
1513 && TREE_CHAIN (init) == NULL_TREE)
1516 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
1517 if (! value || value == error_mark_node)
1518 /* friend or constructor went bad. */
1520 if (TREE_TYPE (value) == error_mark_node)
1521 return error_mark_node;
1523 /* Pass friendly classes back. */
1524 if (TREE_CODE (value) == VOID_TYPE)
1525 return void_type_node;
1527 if (DECL_NAME (value) != NULL_TREE
1528 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1529 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1530 error ("member `%D' conflicts with virtual function table field name",
1533 /* Stash away type declarations. */
1534 if (TREE_CODE (value) == TYPE_DECL)
1536 DECL_NONLOCAL (value) = 1;
1537 DECL_CONTEXT (value) = current_class_type;
1539 if (CLASS_TYPE_P (TREE_TYPE (value)))
1540 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1542 if (processing_template_decl)
1543 value = push_template_decl (value);
1548 if (DECL_IN_AGGR_P (value))
1550 error ("`%D' is already defined in `%T'", value,
1551 DECL_CONTEXT (value));
1552 return void_type_node;
1556 asmspec = TREE_STRING_POINTER (asmspec_tree);
1560 if (TREE_CODE (value) == FUNCTION_DECL)
1562 grok_function_init (value, init);
1565 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1566 /* Already complained in grokdeclarator. */
1570 /* We allow initializers to become parameters to base
1572 if (TREE_CODE (init) == TREE_LIST)
1574 if (TREE_CHAIN (init) == NULL_TREE)
1575 init = TREE_VALUE (init);
1577 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1580 if (TREE_CODE (init) == CONST_DECL)
1581 init = DECL_INITIAL (init);
1582 else if (TREE_READONLY_DECL_P (init))
1583 init = decl_constant_value (init);
1584 else if (TREE_CODE (init) == CONSTRUCTOR)
1585 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1586 if (init == error_mark_node)
1587 /* We must make this look different than `error_mark_node'
1588 because `decl_const_value' would mis-interpret it
1589 as only meaning that this VAR_DECL is defined. */
1590 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1591 else if (processing_template_decl)
1593 else if (! TREE_CONSTANT (init))
1595 /* We can allow references to things that are effectively
1596 static, since references are initialized with the address. */
1597 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1598 || (TREE_STATIC (init) == 0
1599 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1601 error ("field initializer is not constant");
1602 init = error_mark_node;
1608 if (processing_template_decl && ! current_function_decl
1609 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1610 value = push_template_decl (value);
1613 cplus_decl_attributes (&value, attrlist, 0);
1615 if (TREE_CODE (value) == VAR_DECL)
1617 finish_static_data_member_decl (value, init, asmspec_tree,
1621 if (TREE_CODE (value) == FIELD_DECL)
1624 error ("`asm' specifiers are not permitted on non-static data members");
1625 if (DECL_INITIAL (value) == error_mark_node)
1626 init = error_mark_node;
1627 cp_finish_decl (value, init, NULL_TREE, flags);
1628 DECL_INITIAL (value) = init;
1629 DECL_IN_AGGR_P (value) = 1;
1632 if (TREE_CODE (value) == FUNCTION_DECL)
1636 /* This must override the asm specifier which was placed
1637 by grokclassfn. Lay this out fresh. */
1638 SET_DECL_RTL (value, NULL_RTX);
1639 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1641 if (!DECL_FRIEND_P (value))
1642 grok_special_member_properties (value);
1644 cp_finish_decl (value, init, asmspec_tree, flags);
1646 /* Pass friends back this way. */
1647 if (DECL_FRIEND_P (value))
1648 return void_type_node;
1650 DECL_IN_AGGR_P (value) = 1;
1658 /* Like `grokfield', but for bitfields.
1659 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1662 grokbitfield (declarator, declspecs, width)
1663 tree declarator, declspecs, width;
1665 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1668 if (! value) return NULL_TREE; /* friends went bad. */
1670 /* Pass friendly classes back. */
1671 if (TREE_CODE (value) == VOID_TYPE)
1672 return void_type_node;
1674 if (TREE_CODE (value) == TYPE_DECL)
1676 error ("cannot declare `%D' to be a bit-field type", value);
1680 /* Usually, finish_struct_1 catches bitfields with invalid types.
1681 But, in the case of bitfields with function type, we confuse
1682 ourselves into thinking they are member functions, so we must
1684 if (TREE_CODE (value) == FUNCTION_DECL)
1686 error ("cannot declare bit-field `%D' with function type",
1691 if (DECL_IN_AGGR_P (value))
1693 error ("`%D' is already defined in the class %T", value,
1694 DECL_CONTEXT (value));
1695 return void_type_node;
1698 if (TREE_STATIC (value))
1700 error ("static member `%D' cannot be a bit-field", value);
1703 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1705 if (width != error_mark_node)
1707 constant_expression_warning (width);
1708 DECL_INITIAL (value) = width;
1709 SET_DECL_C_BIT_FIELD (value);
1712 DECL_IN_AGGR_P (value) = 1;
1717 grokoptypename (declspecs, declarator)
1718 tree declspecs, declarator;
1720 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1721 return mangle_conv_op_name_for_type (t);
1724 /* When a function is declared with an initializer,
1725 do the right thing. Currently, there are two possibilities:
1730 // initialization possibility #1.
1731 virtual void f () = 0;
1753 // initialization possibility #2
1760 grok_function_init (decl, init)
1764 /* An initializer for a function tells how this function should
1766 tree type = TREE_TYPE (decl);
1768 if (TREE_CODE (type) == FUNCTION_TYPE)
1769 error ("initializer specified for non-member function `%D'", decl);
1770 else if (integer_zerop (init))
1771 DECL_PURE_VIRTUAL_P (decl) = 1;
1773 error ("invalid initializer for virtual method `%D'", decl);
1777 cplus_decl_attributes (decl, attributes, flags)
1778 tree *decl, attributes;
1781 if (*decl == NULL_TREE || *decl == void_type_node)
1784 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1785 decl = &DECL_TEMPLATE_RESULT (*decl);
1787 decl_attributes (decl, attributes, flags);
1789 if (TREE_CODE (*decl) == TYPE_DECL)
1790 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1793 /* CONSTRUCTOR_NAME:
1794 Return the name for the constructor (or destructor) for the
1795 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1796 IDENTIFIER_NODE. When given a template, this routine doesn't
1797 lose the specialization. */
1800 constructor_name_full (thing)
1803 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1804 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1805 || TREE_CODE (thing) == TYPENAME_TYPE)
1806 thing = TYPE_NAME (thing);
1807 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1809 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1810 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1812 thing = TYPE_NAME (thing);
1814 if (TREE_CODE (thing) == TYPE_DECL
1815 || (TREE_CODE (thing) == TEMPLATE_DECL
1816 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1817 thing = DECL_NAME (thing);
1818 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1822 /* CONSTRUCTOR_NAME:
1823 Return the name for the constructor (or destructor) for the
1824 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1825 IDENTIFIER_NODE. When given a template, return the plain
1826 unspecialized name. */
1829 constructor_name (thing)
1833 thing = constructor_name_full (thing);
1834 t = IDENTIFIER_TEMPLATE (thing);
1840 /* Defer the compilation of the FN until the end of compilation. */
1846 if (DECL_DEFERRED_FN (fn))
1848 DECL_DEFERRED_FN (fn) = 1;
1850 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1852 VARRAY_PUSH_TREE (deferred_fns, fn);
1855 /* Hunts through the global anonymous union ANON_DECL, building
1856 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1857 returns a VAR_DECL whose size is the same as the size of the
1858 ANON_DECL, if one is available.
1860 FIXME: we should really handle anonymous unions by binding the names
1861 of the members to COMPONENT_REFs rather than this kludge. */
1864 build_anon_union_vars (anon_decl, elems, static_p, external_p)
1870 tree type = TREE_TYPE (anon_decl);
1871 tree main_decl = NULL_TREE;
1874 /* Rather than write the code to handle the non-union case,
1875 just give an error. */
1876 if (TREE_CODE (type) != UNION_TYPE)
1877 error ("anonymous struct not inside named type");
1879 for (field = TYPE_FIELDS (type);
1881 field = TREE_CHAIN (field))
1885 if (DECL_ARTIFICIAL (field))
1887 if (TREE_CODE (field) != FIELD_DECL)
1889 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
1894 if (TREE_PRIVATE (field))
1895 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1896 else if (TREE_PROTECTED (field))
1897 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1899 if (DECL_NAME (field) == NULL_TREE
1900 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1902 decl = build_anon_union_vars (field, elems, static_p, external_p);
1906 else if (DECL_NAME (field) == NULL_TREE)
1910 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1911 /* tell `pushdecl' that this is not tentative. */
1912 DECL_INITIAL (decl) = error_mark_node;
1913 TREE_PUBLIC (decl) = 0;
1914 TREE_STATIC (decl) = static_p;
1915 DECL_EXTERNAL (decl) = external_p;
1916 decl = pushdecl (decl);
1917 DECL_INITIAL (decl) = NULL_TREE;
1920 /* Only write out one anon union element--choose the largest
1921 one. We used to try to find one the same size as the union,
1922 but that fails if the ABI forces us to align the union more
1924 if (main_decl == NULL_TREE
1925 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
1928 TREE_ASM_WRITTEN (main_decl) = 1;
1932 /* ??? This causes there to be no debug info written out
1934 TREE_ASM_WRITTEN (decl) = 1;
1936 if (DECL_NAME (field) == NULL_TREE
1937 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1938 /* The remainder of the processing was already done in the
1942 /* If there's a cleanup to do, it belongs in the
1943 TREE_PURPOSE of the following TREE_LIST. */
1944 *elems = tree_cons (NULL_TREE, decl, *elems);
1945 TREE_TYPE (*elems) = type;
1951 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1952 anonymous union, then all members must be laid out together. PUBLIC_P
1953 is nonzero if this union is not declared static. */
1956 finish_anon_union (anon_union_decl)
1957 tree anon_union_decl;
1959 tree type = TREE_TYPE (anon_union_decl);
1961 int public_p = TREE_PUBLIC (anon_union_decl);
1962 int static_p = TREE_STATIC (anon_union_decl);
1963 int external_p = DECL_EXTERNAL (anon_union_decl);
1965 /* The VAR_DECL's context is the same as the TYPE's context. */
1966 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1968 if (TYPE_FIELDS (type) == NULL_TREE)
1973 error ("namespace-scope anonymous aggregates must be static");
1977 main_decl = build_anon_union_vars (anon_union_decl,
1978 &DECL_ANON_UNION_ELEMS (anon_union_decl),
1979 static_p, external_p);
1981 if (main_decl == NULL_TREE)
1983 warning ("anonymous aggregate with no members");
1989 make_decl_rtl (main_decl, 0);
1990 COPY_DECL_RTL (main_decl, anon_union_decl);
1991 expand_anon_union_decl (anon_union_decl,
1993 DECL_ANON_UNION_ELEMS (anon_union_decl));
1996 add_decl_stmt (anon_union_decl);
1999 /* Finish processing a builtin type TYPE. It's name is NAME,
2000 its fields are in the array FIELDS. LEN is the number of elements
2001 in FIELDS minus one, or put another way, it is the maximum subscript
2004 It is given the same alignment as ALIGN_TYPE. */
2007 finish_builtin_type (type, name, fields, len, align_type)
2016 TYPE_FIELDS (type) = fields[0];
2017 for (i = 0; i < len; i++)
2019 layout_type (TREE_TYPE (fields[i]));
2020 DECL_FIELD_CONTEXT (fields[i]) = type;
2021 TREE_CHAIN (fields[i]) = fields[i+1];
2023 DECL_FIELD_CONTEXT (fields[i]) = type;
2024 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2025 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2027 #if 0 /* not yet, should get fixed properly later */
2028 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2030 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2032 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2033 layout_decl (TYPE_NAME (type), 0);
2036 /* Auxiliary functions to make type signatures for
2037 `operator new' and `operator delete' correspond to
2038 what compiler will be expecting. */
2041 coerce_new_type (type)
2045 tree args = TYPE_ARG_TYPES (type);
2047 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2049 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2050 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
2052 if (!args || args == void_list_node
2053 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2056 if (args && args != void_list_node)
2057 args = TREE_CHAIN (args);
2058 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2063 args = tree_cons (NULL_TREE, c_size_type_node, args);
2066 type = build_exception_variant
2067 (build_function_type (ptr_type_node, args),
2068 TYPE_RAISES_EXCEPTIONS (type));
2076 coerce_delete_type (type)
2080 tree args = TYPE_ARG_TYPES (type);
2082 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2084 if (!same_type_p (TREE_TYPE (type), void_type_node))
2085 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
2087 if (!args || args == void_list_node
2088 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2091 if (args && args != void_list_node)
2092 args = TREE_CHAIN (args);
2093 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2098 args = tree_cons (NULL_TREE, ptr_type_node, args);
2101 type = build_exception_variant
2102 (build_function_type (void_type_node, args),
2103 TYPE_RAISES_EXCEPTIONS (type));
2112 mark_vtable_entries (decl)
2115 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2117 for (; entries; entries = TREE_CHAIN (entries))
2119 tree fnaddr = TREE_VALUE (entries);
2122 if (TREE_CODE (fnaddr) != ADDR_EXPR
2123 && TREE_CODE (fnaddr) != FDESC_EXPR)
2124 /* This entry is an offset: a virtual base class offset, a
2125 virtual call offset, an RTTI offset, etc. */
2128 fn = TREE_OPERAND (fnaddr, 0);
2129 TREE_ADDRESSABLE (fn) = 1;
2130 /* When we don't have vcall offsets, we output thunks whenever
2131 we output the vtables that contain them. With vcall offsets,
2132 we know all the thunks we'll need when we emit a virtual
2133 function, so we emit the thunks there instead. */
2134 if (DECL_THUNK_P (fn))
2135 use_thunk (fn, /*emit_p=*/0);
2140 /* Set DECL up to have the closest approximation of "initialized common"
2141 linkage available. */
2144 comdat_linkage (decl)
2148 make_decl_one_only (decl);
2149 else if (TREE_CODE (decl) == FUNCTION_DECL
2150 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2151 /* We can just emit function and compiler-generated variables
2152 statically; having multiple copies is (for the most part) only
2155 There are two correctness issues, however: the address of a
2156 template instantiation with external linkage should be the
2157 same, independent of what translation unit asks for the
2158 address, and this will not hold when we emit multiple copies of
2159 the function. However, there's little else we can do.
2161 Also, by default, the typeinfo implementation assumes that
2162 there will be only one copy of the string used as the name for
2163 each type. Therefore, if weak symbols are unavailable, the
2164 run-time library should perform a more conservative check; it
2165 should perform a string comparison, rather than an address
2167 TREE_PUBLIC (decl) = 0;
2170 /* Static data member template instantiations, however, cannot
2171 have multiple copies. */
2172 if (DECL_INITIAL (decl) == 0
2173 || DECL_INITIAL (decl) == error_mark_node)
2174 DECL_COMMON (decl) = 1;
2175 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2177 DECL_COMMON (decl) = 1;
2178 DECL_INITIAL (decl) = error_mark_node;
2182 /* We can't do anything useful; leave vars for explicit
2184 DECL_EXTERNAL (decl) = 1;
2185 DECL_NOT_REALLY_EXTERN (decl) = 0;
2189 if (DECL_LANG_SPECIFIC (decl))
2190 DECL_COMDAT (decl) = 1;
2193 /* For win32 we also want to put explicit instantiations in
2194 linkonce sections, so that they will be merged with implicit
2195 instantiations; otherwise we get duplicate symbol errors. */
2198 maybe_make_one_only (decl)
2201 /* We used to say that this was not necessary on targets that support weak
2202 symbols, because the implicit instantiations will defer to the explicit
2203 one. However, that's not actually the case in SVR4; a strong definition
2204 after a weak one is an error. Also, not making explicit
2205 instantiations one_only means that we can end up with two copies of
2206 some template instantiations. */
2210 /* We can't set DECL_COMDAT on functions, or finish_file will think
2211 we can get away with not emitting them if they aren't used. We need
2212 to for variables so that cp_finish_decl will update their linkage,
2213 because their DECL_INITIAL may not have been set properly yet. */
2215 make_decl_one_only (decl);
2217 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2218 DECL_COMDAT (decl) = 1;
2221 /* Returns the virtual function with which the vtable for TYPE is
2222 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2230 if (TYPE_FOR_JAVA (type)
2231 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2232 || CLASSTYPE_INTERFACE_KNOWN (type))
2235 for (method = TYPE_METHODS (type); method != NULL_TREE;
2236 method = TREE_CHAIN (method))
2237 if (DECL_VINDEX (method) != NULL_TREE
2238 && ! DECL_DECLARED_INLINE_P (method)
2239 && (! DECL_PURE_VIRTUAL_P (method)
2241 /* This would be nice, but we didn't think of it in time. */
2242 || DECL_DESTRUCTOR_P (method)
2250 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2251 based on TYPE and other static flags.
2253 Note that anything public is tagged TREE_PUBLIC, whether
2254 it's public in this file or in another one. */
2257 import_export_vtable (decl, type, final)
2261 if (DECL_INTERFACE_KNOWN (decl))
2264 if (TYPE_FOR_JAVA (type))
2266 TREE_PUBLIC (decl) = 1;
2267 DECL_EXTERNAL (decl) = 1;
2268 DECL_INTERFACE_KNOWN (decl) = 1;
2270 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2272 TREE_PUBLIC (decl) = 1;
2273 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2274 DECL_INTERFACE_KNOWN (decl) = 1;
2278 /* We can only wait to decide if we have real non-inline virtual
2279 functions in our class, or if we come from a template. */
2281 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2282 || key_method (type));
2284 if (final || ! found)
2286 comdat_linkage (decl);
2287 DECL_EXTERNAL (decl) = 0;
2291 TREE_PUBLIC (decl) = 1;
2292 DECL_EXTERNAL (decl) = 1;
2297 /* Determine whether or not we want to specifically import or export CTYPE,
2298 using various heuristics. */
2301 import_export_class (ctype)
2304 /* -1 for imported, 1 for exported. */
2305 int import_export = 0;
2307 /* It only makes sense to call this function at EOF. The reason is
2308 that this function looks at whether or not the first non-inline
2309 non-abstract virtual member function has been defined in this
2310 translation unit. But, we can't possibly know that until we've
2311 seen the entire translation unit. */
2312 my_friendly_assert (at_eof, 20000226);
2314 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2317 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2318 we will have CLASSTYPE_INTERFACE_ONLY set but not
2319 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2320 heuristic because someone will supply a #pragma implementation
2321 elsewhere, and deducing it here would produce a conflict. */
2322 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2325 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2327 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2330 /* If we got -fno-implicit-templates, we import template classes that
2331 weren't explicitly instantiated. */
2332 if (import_export == 0
2333 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2334 && ! flag_implicit_templates)
2337 /* Base our import/export status on that of the first non-inline,
2338 non-pure virtual function, if any. */
2339 if (import_export == 0
2340 && TYPE_POLYMORPHIC_P (ctype))
2342 tree method = key_method (ctype);
2344 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2347 #ifdef MULTIPLE_SYMBOL_SPACES
2348 if (import_export == -1)
2354 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2355 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2359 /* We need to describe to the assembler the relationship between
2360 a vtable and the vtable of the parent class. */
2363 output_vtable_inherit (vars)
2367 rtx child_rtx, parent_rtx;
2369 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2371 parent = binfo_for_vtable (vars);
2373 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2374 parent_rtx = const0_rtx;
2377 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2378 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2383 assemble_vtable_inherit (child_rtx, parent_rtx);
2387 finish_vtable_vardecl (t, data)
2389 void *data ATTRIBUTE_UNUSED;
2392 tree ctype = DECL_CONTEXT (vars);
2393 import_export_class (ctype);
2394 import_export_vtable (vars, ctype, 1);
2396 if (! DECL_EXTERNAL (vars)
2397 && DECL_NEEDED_P (vars)
2398 && ! TREE_ASM_WRITTEN (vars))
2400 if (TREE_TYPE (vars) == void_type_node)
2401 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2405 mark_vtable_entries (vars);
2406 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2407 store_init_value (vars, DECL_INITIAL (vars));
2409 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2411 /* Mark the VAR_DECL node representing the vtable itself as a
2412 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2413 It is rather important that such things be ignored because
2414 any effort to actually generate DWARF for them will run
2415 into trouble when/if we encounter code like:
2418 struct S { virtual void member (); };
2420 because the artificial declaration of the vtable itself (as
2421 manufactured by the g++ front end) will say that the vtable
2422 is a static member of `S' but only *after* the debug output
2423 for the definition of `S' has already been output. This causes
2424 grief because the DWARF entry for the definition of the vtable
2425 will try to refer back to an earlier *declaration* of the
2426 vtable as a static member of `S' and there won't be one.
2427 We might be able to arrange to have the "vtable static member"
2428 attached to the member list for `S' before the debug info for
2429 `S' get written (which would solve the problem) but that would
2430 require more intrusive changes to the g++ front end. */
2432 DECL_IGNORED_P (vars) = 1;
2435 /* Always make vtables weak. */
2437 comdat_linkage (vars);
2439 rest_of_decl_compilation (vars, NULL, 1, 1);
2442 output_vtable_inherit (vars);
2444 /* Because we're only doing syntax-checking, we'll never end up
2445 actually marking the variable as written. */
2446 if (flag_syntax_only)
2447 TREE_ASM_WRITTEN (vars) = 1;
2449 /* Since we're writing out the vtable here, also write the debug
2451 note_debug_info_needed (ctype);
2456 /* If the references to this class' vtables were optimized away, still
2457 emit the appropriate debugging information. See dfs_debug_mark. */
2458 if (DECL_COMDAT (vars)
2459 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2460 note_debug_info_needed (ctype);
2466 prune_vtable_vardecl (t, data)
2468 void *data ATTRIBUTE_UNUSED;
2470 *t = TREE_CHAIN (*t);
2474 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2475 inline function or template instantiation at end-of-file. */
2478 import_export_decl (decl)
2481 if (DECL_INTERFACE_KNOWN (decl))
2484 if (DECL_TEMPLATE_INSTANTIATION (decl)
2485 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2487 DECL_NOT_REALLY_EXTERN (decl) = 1;
2488 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2489 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2490 && (flag_implicit_templates
2491 || (flag_implicit_inline_templates
2492 && DECL_DECLARED_INLINE_P (decl))))
2494 if (!TREE_PUBLIC (decl))
2495 /* Templates are allowed to have internal linkage. See
2499 comdat_linkage (decl);
2502 DECL_NOT_REALLY_EXTERN (decl) = 0;
2504 else if (DECL_FUNCTION_MEMBER_P (decl))
2506 if (!DECL_DECLARED_INLINE_P (decl))
2508 tree ctype = DECL_CONTEXT (decl);
2509 import_export_class (ctype);
2510 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2512 DECL_NOT_REALLY_EXTERN (decl)
2513 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2514 || (DECL_DECLARED_INLINE_P (decl)
2515 && ! flag_implement_inlines
2516 && !DECL_VINDEX (decl)));
2518 /* Always make artificials weak. */
2519 if (DECL_ARTIFICIAL (decl) && flag_weak)
2520 comdat_linkage (decl);
2522 maybe_make_one_only (decl);
2526 comdat_linkage (decl);
2528 else if (tinfo_decl_p (decl, 0))
2530 tree ctype = TREE_TYPE (DECL_NAME (decl));
2532 if (IS_AGGR_TYPE (ctype))
2533 import_export_class (ctype);
2535 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2536 && TYPE_POLYMORPHIC_P (ctype)
2537 /* If -fno-rtti, we're not necessarily emitting this stuff with
2538 the class, so go ahead and emit it now. This can happen
2539 when a class is used in exception handling. */
2541 /* If the type is a cv-qualified variant of a type, then we
2542 must emit the tinfo function in this translation unit
2543 since it will not be emitted when the vtable for the type
2544 is output (which is when the unqualified version is
2546 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2548 DECL_NOT_REALLY_EXTERN (decl)
2549 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2550 || (DECL_DECLARED_INLINE_P (decl)
2551 && ! flag_implement_inlines
2552 && !DECL_VINDEX (decl)));
2554 /* Always make artificials weak. */
2556 comdat_linkage (decl);
2558 else if (TYPE_BUILT_IN (ctype)
2559 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2560 DECL_NOT_REALLY_EXTERN (decl) = 0;
2562 comdat_linkage (decl);
2565 comdat_linkage (decl);
2567 DECL_INTERFACE_KNOWN (decl) = 1;
2571 build_cleanup (decl)
2575 tree type = TREE_TYPE (decl);
2577 if (TREE_CODE (type) == ARRAY_TYPE)
2581 mark_addressable (decl);
2582 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2584 temp = build_delete (TREE_TYPE (temp), temp,
2585 sfk_complete_destructor,
2586 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2590 /* Returns the initialization guard variable for the variable DECL,
2591 which has static storage duration. */
2600 sname = mangle_guard_variable (decl);
2601 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2606 /* We use a type that is big enough to contain a mutex as well
2607 as an integer counter. */
2608 guard_type = long_long_integer_type_node;
2609 guard = build_decl (VAR_DECL, sname, guard_type);
2611 /* The guard should have the same linkage as what it guards. */
2612 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2613 TREE_STATIC (guard) = TREE_STATIC (decl);
2614 DECL_COMMON (guard) = DECL_COMMON (decl);
2615 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2616 if (TREE_PUBLIC (decl))
2617 DECL_WEAK (guard) = DECL_WEAK (decl);
2619 DECL_ARTIFICIAL (guard) = 1;
2620 TREE_USED (guard) = 1;
2621 pushdecl_top_level (guard);
2622 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2627 /* Return those bits of the GUARD variable that should be set when the
2628 guarded entity is actually initialized. */
2631 get_guard_bits (guard)
2634 /* We only set the first byte of the guard, in order to leave room
2635 for a mutex in the high-order bits. */
2636 guard = build1 (ADDR_EXPR,
2637 build_pointer_type (TREE_TYPE (guard)),
2639 guard = build1 (NOP_EXPR,
2640 build_pointer_type (char_type_node),
2642 guard = build1 (INDIRECT_REF, char_type_node, guard);
2647 /* Return an expression which determines whether or not the GUARD
2648 variable has already been initialized. */
2651 get_guard_cond (guard)
2656 /* Check to see if the GUARD is zero. */
2657 guard = get_guard_bits (guard);
2658 guard_value = integer_zero_node;
2659 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2660 guard_value = convert (TREE_TYPE (guard), guard_value);
2661 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2664 /* Return an expression which sets the GUARD variable, indicating that
2665 the variable being guarded has been initialized. */
2673 /* Set the GUARD to one. */
2674 guard = get_guard_bits (guard);
2675 guard_init = integer_one_node;
2676 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2677 guard_init = convert (TREE_TYPE (guard), guard_init);
2678 return build_modify_expr (guard, NOP_EXPR, guard_init);
2681 /* Start the process of running a particular set of global constructors
2682 or destructors. Subroutine of do_[cd]tors. */
2685 start_objects (method_type, initp)
2686 int method_type, initp;
2692 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2694 if (initp != DEFAULT_INIT_PRIORITY)
2704 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2707 sprintf (type, "%c", method_type);
2709 fnname = get_file_function_name_long (type);
2711 start_function (void_list_node,
2712 make_call_declarator (fnname, void_list_node, NULL_TREE,
2714 NULL_TREE, SF_DEFAULT);
2716 /* It can be a static function as long as collect2 does not have
2717 to scan the object file to find its ctor/dtor routine. */
2718 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2720 /* Mark this declaration as used to avoid spurious warnings. */
2721 TREE_USED (current_function_decl) = 1;
2723 /* Mark this function as a global constructor or destructor. */
2724 if (method_type == 'I')
2725 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2727 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2728 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2730 body = begin_compound_stmt (/*has_no_scope=*/0);
2732 /* We cannot allow these functions to be elided, even if they do not
2733 have external linkage. And, there's no point in deferring
2734 copmilation of thes functions; they're all going to have to be
2736 current_function_cannot_inline
2737 = "static constructors and destructors cannot be inlined";
2742 /* Finish the process of running a particular set of global constructors
2743 or destructors. Subroutine of do_[cd]tors. */
2746 finish_objects (method_type, initp, body)
2747 int method_type, initp;
2753 finish_compound_stmt (/*has_no_scope=*/0, body);
2754 fn = finish_function (0);
2757 /* When only doing semantic analysis, and no RTL generation, we
2758 can't call functions that directly emit assembly code; there is
2759 no assembly file in which to put the code. */
2760 if (flag_syntax_only)
2763 if (targetm.have_ctors_dtors)
2765 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2766 if (method_type == 'I')
2767 (* targetm.asm_out.constructor) (fnsym, initp);
2769 (* targetm.asm_out.destructor) (fnsym, initp);
2773 /* The names of the parameters to the function created to handle
2774 initializations and destructions for objects with static storage
2776 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2777 #define PRIORITY_IDENTIFIER "__priority"
2779 /* The name of the function we create to handle initializations and
2780 destructions for objects with static storage duration. */
2781 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2783 /* The declaration for the __INITIALIZE_P argument. */
2784 static tree initialize_p_decl;
2786 /* The declaration for the __PRIORITY argument. */
2787 static tree priority_decl;
2789 /* The declaration for the static storage duration function. */
2790 static tree ssdf_decl;
2792 /* All the static storage duration functions created in this
2793 translation unit. */
2794 static varray_type ssdf_decls;
2796 /* A map from priority levels to information about that priority
2797 level. There may be many such levels, so efficient lookup is
2799 static splay_tree priority_info_map;
2801 /* Begins the generation of the function that will handle all
2802 initialization and destruction of objects with static storage
2803 duration. The function generated takes two parameters of type
2804 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2805 non-zero, it performs initializations. Otherwise, it performs
2806 destructions. It only performs those initializations or
2807 destructions with the indicated __PRIORITY. The generated function
2810 It is assumed that this function will only be called once per
2811 translation unit. */
2814 start_static_storage_duration_function ()
2816 static unsigned ssdf_number;
2821 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2823 /* Create the identifier for this function. It will be of the form
2824 SSDF_IDENTIFIER_<number>. */
2825 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2826 if (ssdf_number == 0)
2828 /* Overflow occurred. That means there are at least 4 billion
2829 initialization functions. */
2830 sorry ("too many initialization functions required");
2834 /* Create the parameters. */
2835 parm_types = void_list_node;
2836 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2837 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2838 type = build_function_type (void_type_node, parm_types);
2840 /* Create the FUNCTION_DECL itself. */
2841 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2842 get_identifier (id),
2844 TREE_PUBLIC (ssdf_decl) = 0;
2845 DECL_ARTIFICIAL (ssdf_decl) = 1;
2847 /* Put this function in the list of functions to be called from the
2848 static constructors and destructors. */
2851 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2853 /* Take this opportunity to initialize the map from priority
2854 numbers to information about that priority level. */
2855 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2856 /*delete_key_fn=*/0,
2857 /*delete_value_fn=*/
2858 (splay_tree_delete_value_fn) &free);
2860 /* We always need to generate functions for the
2861 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2862 priorities later, we'll be sure to find the
2863 DEFAULT_INIT_PRIORITY. */
2864 get_priority_info (DEFAULT_INIT_PRIORITY);
2867 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2869 /* Create the argument list. */
2870 initialize_p_decl = build_decl (PARM_DECL,
2871 get_identifier (INITIALIZE_P_IDENTIFIER),
2873 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2874 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
2875 TREE_USED (initialize_p_decl) = 1;
2876 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
2878 DECL_CONTEXT (priority_decl) = ssdf_decl;
2879 DECL_ARG_TYPE (priority_decl) = integer_type_node;
2880 TREE_USED (priority_decl) = 1;
2882 TREE_CHAIN (initialize_p_decl) = priority_decl;
2883 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2885 /* Put the function in the global scope. */
2886 pushdecl (ssdf_decl);
2888 /* Start the function itself. This is equivalent to declarating the
2891 static void __ssdf (int __initialize_p, init __priority_p);
2893 It is static because we only need to call this function from the
2894 various constructor and destructor functions for this module. */
2895 start_function (/*specs=*/NULL_TREE,
2897 /*attrs=*/NULL_TREE,
2900 /* Set up the scope of the outermost block in the function. */
2901 body = begin_compound_stmt (/*has_no_scope=*/0);
2903 /* This function must not be deferred because we are depending on
2904 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2905 current_function_cannot_inline
2906 = "static storage duration functions cannot be inlined";
2911 /* Finish the generation of the function which performs initialization
2912 and destruction of objects with static storage duration. After
2913 this point, no more such objects can be created. */
2916 finish_static_storage_duration_function (body)
2919 /* Close out the function. */
2920 finish_compound_stmt (/*has_no_scope=*/0, body);
2921 expand_body (finish_function (0));
2924 /* Return the information about the indicated PRIORITY level. If no
2925 code to handle this level has yet been generated, generate the
2926 appropriate prologue. */
2928 static priority_info
2929 get_priority_info (priority)
2935 n = splay_tree_lookup (priority_info_map,
2936 (splay_tree_key) priority);
2939 /* Create a new priority information structure, and insert it
2941 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2942 pi->initializations_p = 0;
2943 pi->destructions_p = 0;
2944 splay_tree_insert (priority_info_map,
2945 (splay_tree_key) priority,
2946 (splay_tree_value) pi);
2949 pi = (priority_info) n->value;
2954 /* Set up to handle the initialization or destruction of DECL. If
2955 INITP is non-zero, we are initializing the variable. Otherwise, we
2956 are destroying it. */
2959 start_static_initialization_or_destruction (decl, initp)
2963 tree guard_if_stmt = NULL_TREE;
2970 /* Figure out the priority for this declaration. */
2971 priority = DECL_INIT_PRIORITY (decl);
2973 priority = DEFAULT_INIT_PRIORITY;
2975 /* Remember that we had an initialization or finalization at this
2977 pi = get_priority_info (priority);
2979 pi->initializations_p = 1;
2981 pi->destructions_p = 1;
2983 /* Trick the compiler into thinking we are at the file and line
2984 where DECL was declared so that error-messages make sense, and so
2985 that the debugger will show somewhat sensible file and line
2987 input_filename = DECL_SOURCE_FILE (decl);
2988 lineno = DECL_SOURCE_LINE (decl);
2994 Access control for implicit calls to the constructors,
2995 the conversion functions, or the destructor called to
2996 create and destroy a static data member is performed as
2997 if these calls appeared in the scope of the member's
3000 we pretend we are in a static member function of the class of
3001 which the DECL is a member. */
3002 if (member_p (decl))
3004 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3005 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3008 /* Conditionalize this initialization on being in the right priority
3009 and being initializing/finalizing appropriately. */
3010 guard_if_stmt = begin_if_stmt ();
3011 cond = cp_build_binary_op (EQ_EXPR,
3013 build_int_2 (priority, 0));
3014 init_cond = initp ? integer_one_node : integer_zero_node;
3015 init_cond = cp_build_binary_op (EQ_EXPR,
3018 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3020 /* Assume we don't need a guard. */
3022 /* We need a guard if this is an object with external linkage that
3023 might be initialized in more than one place. (For example, a
3024 static data member of a template, when the data member requires
3026 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3027 || DECL_ONE_ONLY (decl)
3028 || DECL_WEAK (decl)))
3032 guard = get_guard (decl);
3034 /* When using __cxa_atexit, we just check the GUARD as we would
3035 for a local static. */
3036 if (flag_use_cxa_atexit)
3038 /* When using __cxa_atexit, we never try to destroy
3039 anything from a static destructor. */
3040 my_friendly_assert (initp, 20000629);
3041 guard_cond = get_guard_cond (guard);
3043 /* If we don't have __cxa_atexit, then we will be running
3044 destructors from .fini sections, or their equivalents. So,
3045 we need to know how many times we've tried to initialize this
3046 object. We do initializations only if the GUARD is zero,
3047 i.e., if we are the first to initialize the variable. We do
3048 destructions only if the GUARD is one, i.e., if we are the
3049 last to destroy the variable. */
3052 = cp_build_binary_op (EQ_EXPR,
3053 build_unary_op (PREINCREMENT_EXPR,
3059 = cp_build_binary_op (EQ_EXPR,
3060 build_unary_op (PREDECREMENT_EXPR,
3065 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3068 finish_if_stmt_cond (cond, guard_if_stmt);
3070 /* If we're using __cxa_atexit, we have not already set the GUARD,
3071 so we must do so now. */
3072 if (guard && initp && flag_use_cxa_atexit)
3073 finish_expr_stmt (set_guard (guard));
3075 return guard_if_stmt;
3078 /* We've just finished generating code to do an initialization or
3079 finalization. GUARD_IF_STMT is the if-statement we used to guard
3080 the initialization. */
3083 finish_static_initialization_or_destruction (guard_if_stmt)
3086 finish_then_clause (guard_if_stmt);
3089 /* Now that we're done with DECL we don't need to pretend to be a
3090 member of its class any longer. */
3091 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3092 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3095 /* Generate code to do the static initialization of DECL. The
3096 initialization is INIT. If DECL may be initialized more than once
3097 in different object files, GUARD is the guard variable to
3098 check. PRIORITY is the priority for the initialization. */
3101 do_static_initialization (decl, init)
3108 /* Set up for the initialization. */
3110 = start_static_initialization_or_destruction (decl,
3113 /* Do the initialization itself. */
3114 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3115 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3116 expr = build_aggr_init (decl, init, 0);
3119 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3120 TREE_SIDE_EFFECTS (expr) = 1;
3122 finish_expr_stmt (expr);
3124 /* If we're using __cxa_atexit, register a a function that calls the
3125 destructor for the object. */
3126 if (flag_use_cxa_atexit)
3127 register_dtor_fn (decl);
3130 finish_static_initialization_or_destruction (guard_if_stmt);
3133 /* Generate code to do the static destruction of DECL. If DECL may be
3134 initialized more than once in different object files, GUARD is the
3135 guard variable to check. PRIORITY is the priority for the
3139 do_static_destruction (decl)
3144 /* If we're using __cxa_atexit, then destructors are registered
3145 immediately after objects are initialized. */
3146 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3148 /* If we don't need a destructor, there's nothing to do. */
3149 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3152 /* Actually do the destruction. */
3153 guard_if_stmt = start_static_initialization_or_destruction (decl,
3155 finish_expr_stmt (build_cleanup (decl));
3156 finish_static_initialization_or_destruction (guard_if_stmt);
3159 /* VARS is a list of variables with static storage duration which may
3160 need initialization and/or finalization. Remove those variables
3161 that don't really need to be initialized or finalized, and return
3162 the resulting list. The order in which the variables appear in
3163 VARS is in reverse order of the order in which they should actually
3164 be initialized. The list we return is in the unreversed order;
3165 i.e., the first variable should be initialized first. */
3168 prune_vars_needing_no_initialization (vars)
3174 for (var = vars, result = NULL_TREE;
3176 var = TREE_CHAIN (var))
3178 tree decl = TREE_VALUE (var);
3179 tree init = TREE_PURPOSE (var);
3181 /* Deal gracefully with error. */
3182 if (decl == error_mark_node)
3185 /* The only things that can be initialized are variables. */
3186 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3188 /* If this object is not defined, we don't need to do anything
3190 if (DECL_EXTERNAL (decl))
3193 /* Also, if the initializer already contains errors, we can bail
3195 if (init && TREE_CODE (init) == TREE_LIST
3196 && value_member (error_mark_node, init))
3199 /* This variable is going to need initialization and/or
3200 finalization, so we add it to the list. */
3201 result = tree_cons (init, decl, result);
3207 /* Make sure we have told the back end about all the variables in
3211 write_out_vars (vars)
3216 for (v = vars; v; v = TREE_CHAIN (v))
3217 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3218 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3221 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3222 (otherwise) that will initialize all gobal objects with static
3223 storage duration having the indicated PRIORITY. */
3226 generate_ctor_or_dtor_function (constructor_p, priority)
3235 /* We use `I' to indicate initialization and `D' to indicate
3242 /* Begin the function. */
3243 body = start_objects (function_key, priority);
3245 /* Call the static storage duration function with appropriate
3247 for (i = 0; i < ssdf_decls->elements_used; ++i)
3249 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3251 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3253 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3257 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3258 calls to any functions marked with attributes indicating that
3259 they should be called at initialization- or destruction-time. */
3260 if (priority == DEFAULT_INIT_PRIORITY)
3264 for (fns = constructor_p ? static_ctors : static_dtors;
3266 fns = TREE_CHAIN (fns))
3267 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3270 /* Close out the function. */
3271 finish_objects (function_key, priority, body);
3274 /* Generate constructor and destructor functions for the priority
3278 generate_ctor_and_dtor_functions_for_priority (n, data)
3280 void *data ATTRIBUTE_UNUSED;
3282 int priority = (int) n->key;
3283 priority_info pi = (priority_info) n->value;
3285 /* Generate the functions themselves, but only if they are really
3287 if (pi->initializations_p
3288 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3289 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3291 if (pi->destructions_p
3292 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3293 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3296 /* Keep iterating. */
3300 /* This routine is called from the last rule in yyparse ().
3301 Its job is to create all the code needed to initialize and
3302 destroy the global aggregates. We do the destruction
3303 first, since that way we only need to reverse the decls once. */
3314 /* Bad parse errors. Just forget about it. */
3315 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3318 /* Otherwise, GDB can get confused, because in only knows
3319 about source for LINENO-1 lines. */
3322 interface_unknown = 1;
3325 /* We now have to write out all the stuff we put off writing out.
3328 o Template specializations that we have not yet instantiated,
3329 but which are needed.
3330 o Initialization and destruction for non-local objects with
3331 static storage duration. (Local objects with static storage
3332 duration are initialized when their scope is first entered,
3333 and are cleaned up via atexit.)
3334 o Virtual function tables.
3336 All of these may cause others to be needed. For example,
3337 instantiating one function may cause another to be needed, and
3338 generating the intiailzer for an object may cause templates to be
3339 instantiated, etc., etc. */
3341 timevar_push (TV_VARCONST);
3343 emit_support_tinfos ();
3349 /* If there are templates that we've put off instantiating, do
3351 instantiate_pending_templates ();
3353 /* Write out virtual tables as required. Note that writing out
3354 the virtual table for a template class may cause the
3355 instantiation of members of that class. */
3356 if (walk_globals (vtable_decl_p,
3357 finish_vtable_vardecl,
3361 /* Write out needed type info variables. Writing out one variable
3362 might cause others to be needed. */
3363 if (walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3366 /* The list of objects with static storage duration is built up
3367 in reverse order. We clear STATIC_AGGREGATES so that any new
3368 aggregates added during the initialization of these will be
3369 initialized in the correct order when we next come around the
3371 vars = prune_vars_needing_no_initialization (static_aggregates);
3372 static_aggregates = NULL_TREE;
3378 /* We need to start a new initialization function each time
3379 through the loop. That's because we need to know which
3380 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3381 isn't computed until a function is finished, and written
3382 out. That's a deficiency in the back-end. When this is
3383 fixed, these initialization functions could all become
3384 inline, with resulting performance improvements. */
3385 tree ssdf_body = start_static_storage_duration_function ();
3387 /* Make sure the back end knows about all the variables. */
3388 write_out_vars (vars);
3390 /* First generate code to do all the initializations. */
3391 for (v = vars; v; v = TREE_CHAIN (v))
3392 do_static_initialization (TREE_VALUE (v),
3395 /* Then, generate code to do all the destructions. Do these
3396 in reverse order so that the most recently constructed
3397 variable is the first destroyed. If we're using
3398 __cxa_atexit, then we don't need to do this; functions
3399 were registered at initialization time to destroy the
3401 if (!flag_use_cxa_atexit)
3403 vars = nreverse (vars);
3404 for (v = vars; v; v = TREE_CHAIN (v))
3405 do_static_destruction (TREE_VALUE (v));
3410 /* Finish up the static storage duration function for this
3412 finish_static_storage_duration_function (ssdf_body);
3414 /* All those initializations and finalizations might cause
3415 us to need more inline functions, more template
3416 instantiations, etc. */
3420 /* Go through the various inline functions, and see if any need
3422 for (i = 0; i < deferred_fns_used; ++i)
3424 tree decl = VARRAY_TREE (deferred_fns, i);
3425 import_export_decl (decl);
3426 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3428 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3430 /* Even though we're already at the top-level, we push
3431 there again. That way, when we pop back a few lines
3432 hence, all of our state is restored. Otherwise,
3433 finish_function doesn't clean things up, and we end
3434 up with CURRENT_FUNCTION_DECL set. */
3435 push_to_top_level ();
3436 synthesize_method (decl);
3437 pop_from_top_level ();
3442 /* We lie to the back-end, pretending that some functions are
3443 not defined when they really are. This keeps these functions
3444 from being put out unnecessarily. But, we must stop lying
3445 when the functions are referenced, or if they are not comdat
3446 since they need to be put out now. */
3447 for (i = 0; i < deferred_fns_used; ++i)
3449 tree decl = VARRAY_TREE (deferred_fns, i);
3451 if (DECL_NOT_REALLY_EXTERN (decl)
3452 && DECL_INITIAL (decl)
3453 && DECL_NEEDED_P (decl))
3454 DECL_EXTERNAL (decl) = 0;
3456 /* If we're going to need to write this function out, and
3457 there's already a body for it, create RTL for it now.
3458 (There might be no body if this is a method we haven't
3459 gotten around to synthesizing yet.) */
3460 if (!DECL_EXTERNAL (decl)
3461 && DECL_NEEDED_P (decl)
3462 && DECL_SAVED_TREE (decl)
3463 && !TREE_ASM_WRITTEN (decl))
3465 int saved_not_really_extern;
3467 /* When we call finish_function in expand_body, it will
3468 try to reset DECL_NOT_REALLY_EXTERN so we save and
3470 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3471 /* Generate RTL for this function now that we know we
3474 /* Undo the damage done by finish_function. */
3475 DECL_EXTERNAL (decl) = 0;
3476 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3477 /* If we're compiling -fsyntax-only pretend that this
3478 function has been written out so that we don't try to
3480 if (flag_syntax_only)
3481 TREE_ASM_WRITTEN (decl) = 1;
3486 if (deferred_fns_used
3487 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3490 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3493 /* Static data members are just like namespace-scope globals. */
3494 for (i = 0; i < pending_statics_used; ++i)
3496 tree decl = VARRAY_TREE (pending_statics, i);
3497 if (TREE_ASM_WRITTEN (decl))
3499 import_export_decl (decl);
3500 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3501 DECL_EXTERNAL (decl) = 0;
3504 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3505 pending_statics_used))
3510 /* We give C linkage to static constructors and destructors. */
3511 push_lang_context (lang_name_c);
3513 /* Generate initialization and destruction functions for all
3514 priorities for which they are required. */
3515 if (priority_info_map)
3516 splay_tree_foreach (priority_info_map,
3517 generate_ctor_and_dtor_functions_for_priority,
3520 /* We're done with the splay-tree now. */
3521 if (priority_info_map)
3522 splay_tree_delete (priority_info_map);
3524 /* We're done with static constructors, so we can go back to "C++"
3526 pop_lang_context ();
3528 /* Now delete from the chain of variables all virtual function tables.
3529 We output them all ourselves, because each will be treated
3530 specially. We don't do this if we're just doing semantic
3531 analysis, and not code-generation. */
3532 if (!flag_syntax_only)
3533 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3535 /* Now, issue warnings about static, but not defined, functions,
3536 etc., and emit debugging information. */
3537 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3538 if (pending_statics)
3539 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3540 pending_statics_used);
3544 /* The entire file is now complete. If requested, dump everything
3548 FILE *stream = dump_begin (TDI_all, &flags);
3552 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3553 dump_end (TDI_all, stream);
3557 timevar_pop (TV_VARCONST);
3559 if (flag_detailed_statistics)
3561 dump_tree_statistics ();
3562 dump_time_statistics ();
3566 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3567 expr. Since it was parsed like a type, we need to wade through and fix
3568 that. Unfortunately, since operator() is left-associative, we can't use
3569 tail recursion. In the above example, TYPE is `A', and DECL is
3572 Maybe this shouldn't be recursive, but how often will it actually be
3576 reparse_absdcl_as_expr (type, decl)
3579 /* do build_functional_cast (type, NULL_TREE) at bottom */
3580 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3581 return build_functional_cast (type, NULL_TREE);
3584 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3586 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3588 if (TREE_CODE (decl) == CALL_EXPR
3589 && (! TREE_TYPE (decl)
3590 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3591 decl = require_complete_type (decl);
3596 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3597 out to be an expr. Since it was parsed like a type, we need to wade
3598 through and fix that. Since casts are right-associative, we are
3599 reversing the order, so we don't have to recurse.
3601 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3605 reparse_absdcl_as_casts (decl, expr)
3611 if (TREE_CODE (expr) == CONSTRUCTOR
3612 && TREE_TYPE (expr) == 0)
3614 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3615 decl = TREE_OPERAND (decl, 0);
3617 if (processing_template_decl)
3618 TREE_TYPE (expr) = type;
3621 expr = digest_init (type, expr, (tree *) 0);
3622 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3624 int failure = complete_array_type (type, expr, 1);
3625 my_friendly_assert (!failure, 78);
3632 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3633 decl = TREE_OPERAND (decl, 0);
3634 if (!VOID_TYPE_P (type))
3636 expr = build_c_cast (type, expr);
3639 if (warn_old_style_cast && ! in_system_header
3640 && non_void_p && current_lang_name != lang_name_c)
3641 warning ("use of old-style cast");
3646 /* Given plain tree nodes for an expression, build up the full semantics. */
3649 build_expr_from_tree (t)
3652 if (t == NULL_TREE || t == error_mark_node)
3655 switch (TREE_CODE (t))
3657 case IDENTIFIER_NODE:
3658 return do_identifier (t, 0, NULL_TREE);
3661 if (LOOKUP_EXPR_GLOBAL (t))
3662 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3664 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3666 case TEMPLATE_ID_EXPR:
3667 return (lookup_template_function
3668 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3669 build_expr_from_tree (TREE_OPERAND (t, 1))));
3672 return build_x_indirect_ref
3673 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3676 return build_functional_cast
3677 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3679 case REINTERPRET_CAST_EXPR:
3680 return build_reinterpret_cast
3681 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3683 case CONST_CAST_EXPR:
3684 return build_const_cast
3685 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3687 case DYNAMIC_CAST_EXPR:
3688 return build_dynamic_cast
3689 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3691 case STATIC_CAST_EXPR:
3692 return build_static_cast
3693 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3695 case PREDECREMENT_EXPR:
3696 case PREINCREMENT_EXPR:
3697 case POSTDECREMENT_EXPR:
3698 case POSTINCREMENT_EXPR:
3702 case TRUTH_NOT_EXPR:
3704 case CONVERT_EXPR: /* Unary + */
3709 return build_x_unary_op (TREE_CODE (t),
3710 build_expr_from_tree (TREE_OPERAND (t, 0)));
3715 case TRUNC_DIV_EXPR:
3717 case FLOOR_DIV_EXPR:
3718 case ROUND_DIV_EXPR:
3719 case EXACT_DIV_EXPR:
3721 case BIT_ANDTC_EXPR:
3724 case TRUNC_MOD_EXPR:
3725 case FLOOR_MOD_EXPR:
3726 case TRUTH_ANDIF_EXPR:
3727 case TRUTH_ORIF_EXPR:
3728 case TRUTH_AND_EXPR:
3743 return build_x_binary_op
3745 build_expr_from_tree (TREE_OPERAND (t, 0)),
3746 build_expr_from_tree (TREE_OPERAND (t, 1)));
3749 return build_m_component_ref
3750 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3751 build_expr_from_tree (TREE_OPERAND (t, 1)));
3754 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3757 if (TREE_OPERAND (t, 0) == NULL_TREE)
3759 return build_nt (ARRAY_REF, NULL_TREE,
3760 build_expr_from_tree (TREE_OPERAND (t, 1)));
3761 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3762 build_expr_from_tree (TREE_OPERAND (t, 1)));
3767 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3769 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3771 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3775 return build_x_modify_expr
3776 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3777 TREE_CODE (TREE_OPERAND (t, 1)),
3778 build_expr_from_tree (TREE_OPERAND (t, 2)));
3781 return build_x_arrow
3782 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3786 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3787 build_expr_from_tree (TREE_OPERAND (t, 1)),
3788 build_expr_from_tree (TREE_OPERAND (t, 2)),
3789 NEW_EXPR_USE_GLOBAL (t));
3792 return delete_sanity
3793 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3794 build_expr_from_tree (TREE_OPERAND (t, 1)),
3795 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3798 if (TREE_OPERAND (t, 1) == NULL_TREE)
3799 return build_x_compound_expr
3800 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3804 case METHOD_CALL_EXPR:
3805 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3807 tree ref = TREE_OPERAND (t, 0);
3808 tree name = TREE_OPERAND (ref, 1);
3810 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3811 name = build_nt (TEMPLATE_ID_EXPR,
3812 TREE_OPERAND (name, 0),
3813 build_expr_from_tree (TREE_OPERAND (name, 1)));
3815 return build_scoped_method_call
3816 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3817 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3819 build_expr_from_tree (TREE_OPERAND (t, 2)));
3823 tree fn = TREE_OPERAND (t, 0);
3825 /* We can get a TEMPLATE_ID_EXPR here on code like:
3829 so we must resolve that. However, we can also get things
3830 like a BIT_NOT_EXPR here, when referring to a destructor,
3831 and things like that are not correctly resolved by
3832 build_expr_from_tree. So, just use build_expr_from_tree
3833 when we really need it. */
3834 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3835 fn = lookup_template_function
3836 (TREE_OPERAND (fn, 0),
3837 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3839 return build_method_call
3840 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3842 build_expr_from_tree (TREE_OPERAND (t, 2)),
3843 NULL_TREE, LOOKUP_NORMAL);
3847 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3849 tree ref = TREE_OPERAND (t, 0);
3850 tree name = TREE_OPERAND (ref, 1);
3852 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3853 name = build_nt (TEMPLATE_ID_EXPR,
3854 TREE_OPERAND (name, 0),
3855 build_expr_from_tree (TREE_OPERAND (name, 1)));
3857 return build_member_call
3858 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3860 build_expr_from_tree (TREE_OPERAND (t, 1)));
3864 tree name = TREE_OPERAND (t, 0);
3866 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3867 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3868 && !LOOKUP_EXPR_GLOBAL (name)
3869 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3870 && (!current_class_type
3871 || !lookup_member (current_class_type, id, 0, 0)))
3873 /* Do Koenig lookup if there are no class members. */
3874 name = do_identifier (id, 0, args);
3876 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3877 || ! really_overloaded_fn (name))
3878 name = build_expr_from_tree (name);
3879 return build_x_function_call (name, args, current_class_ref);
3883 return build_x_conditional_expr
3884 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3885 build_expr_from_tree (TREE_OPERAND (t, 1)),
3886 build_expr_from_tree (TREE_OPERAND (t, 2)));
3888 case PSEUDO_DTOR_EXPR:
3889 return (finish_pseudo_destructor_call_expr
3890 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3891 build_expr_from_tree (TREE_OPERAND (t, 1)),
3892 build_expr_from_tree (TREE_OPERAND (t, 2))));
3896 tree purpose, value, chain;
3898 if (t == void_list_node)
3901 purpose = TREE_PURPOSE (t);
3903 purpose = build_expr_from_tree (purpose);
3904 value = TREE_VALUE (t);
3906 value = build_expr_from_tree (value);
3907 chain = TREE_CHAIN (t);
3908 if (chain && chain != void_type_node)
3909 chain = build_expr_from_tree (chain);
3910 return tree_cons (purpose, value, chain);
3915 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3916 tree field = TREE_OPERAND (t, 1);
3918 /* We use a COMPONENT_REF to indicate things of the form `x.b'
3919 and `x.A::b'. We must distinguish between those cases
3921 if (TREE_CODE (field) == SCOPE_REF)
3922 return build_object_ref (object,
3923 TREE_OPERAND (field, 0),
3924 TREE_OPERAND (field, 1));
3926 return build_x_component_ref (object, field,
3931 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3937 tree type = TREE_TYPE (t);
3940 /* digest_init will do the wrong thing if we let it. */
3941 if (type && TYPE_PTRMEMFUNC_P (type))
3945 /* We do not want to process the purpose of aggregate
3946 initializers as they are identifier nodes which will be
3947 looked up by digest_init. */
3948 purpose_p = !(type && IS_AGGR_TYPE (type));
3949 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3951 tree purpose = TREE_PURPOSE (elts);
3952 tree value = TREE_VALUE (elts);
3954 if (purpose && purpose_p)
3955 purpose = build_expr_from_tree (purpose);
3956 value = build_expr_from_tree (value);
3957 r = tree_cons (purpose, value, r);
3960 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3961 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3964 return digest_init (type, r, 0);
3969 if (TYPE_P (TREE_OPERAND (t, 0)))
3970 return get_typeid (TREE_OPERAND (t, 0));
3971 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3974 return convert_from_reference (t);
3977 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3985 /* This is something of the form `int (*a)++' that has turned out to be an
3986 expr. It was only converted into parse nodes, so we need to go through
3987 and build up the semantics. Most of the work is done by
3988 build_expr_from_tree, above.
3990 In the above example, TYPE is `int' and DECL is `*a'. */
3993 reparse_decl_as_expr (type, decl)
3996 decl = build_expr_from_tree (decl);
3998 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4003 /* This is something of the form `int (*a)' that has turned out to be a
4004 decl. It was only converted into parse nodes, so we need to do the
4005 checking that make_{pointer,reference}_declarator do. */
4008 finish_decl_parsing (decl)
4011 switch (TREE_CODE (decl))
4013 case IDENTIFIER_NODE:
4016 return make_pointer_declarator
4017 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4019 return make_reference_declarator
4020 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4022 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4025 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4026 TREE_COMPLEXITY (decl) = current_class_depth;
4029 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4032 /* For attribute handling. */
4033 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4035 case TEMPLATE_ID_EXPR:
4043 /* Return 1 if root encloses child. */
4046 is_namespace_ancestor (root, child)
4051 if (root == global_namespace)
4053 if (child == global_namespace)
4055 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4059 /* Return the namespace that is the common ancestor
4060 of two given namespaces. */
4063 namespace_ancestor (ns1, ns2)
4066 if (is_namespace_ancestor (ns1, ns2))
4068 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4071 /* Insert used into the using list of user. Set indirect_flag if this
4072 directive is not directly from the source. Also find the common
4073 ancestor and let our users know about the new namespace */
4075 add_using_namespace (user, used, indirect)
4081 /* Using oneself is a no-op. */
4084 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4085 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4086 /* Check if we already have this. */
4087 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4091 /* Promote to direct usage. */
4092 TREE_INDIRECT_USING (t) = 0;
4096 /* Add used to the user's using list. */
4097 DECL_NAMESPACE_USING (user)
4098 = tree_cons (used, namespace_ancestor (user, used),
4099 DECL_NAMESPACE_USING (user));
4101 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4103 /* Add user to the used's users list. */
4104 DECL_NAMESPACE_USERS (used)
4105 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4107 /* Recursively add all namespaces used. */
4108 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4109 /* indirect usage */
4110 add_using_namespace (user, TREE_PURPOSE (t), 1);
4112 /* Tell everyone using us about the new used namespaces. */
4113 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4114 add_using_namespace (TREE_PURPOSE (t), used, 1);
4117 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4118 duplicates. The first list becomes the tail of the result.
4120 The algorithm is O(n^2). We could get this down to O(n log n) by
4121 doing a sort on the addresses of the functions, if that becomes
4125 merge_functions (s1, s2)
4129 for (; s2; s2 = OVL_NEXT (s2))
4131 tree fn2 = OVL_CURRENT (s2);
4134 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4136 tree fn1 = OVL_CURRENT (fns1);
4138 /* If the function from S2 is already in S1, there is no
4139 need to add it again. For `extern "C"' functions, we
4140 might have two FUNCTION_DECLs for the same function, in
4141 different namespaces; again, we only need one of them. */
4143 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
4144 && DECL_NAME (fn1) == DECL_NAME (fn2)))
4148 /* If we exhausted all of the functions in S1, FN2 is new. */
4150 s1 = build_overload (fn2, s1);
4155 /* This should return an error not all definitions define functions.
4156 It is not an error if we find two functions with exactly the
4157 same signature, only if these are selected in overload resolution.
4158 old is the current set of bindings, new the freshly-found binding.
4159 XXX Do we want to give *all* candidates in case of ambiguity?
4160 XXX In what way should I treat extern declarations?
4161 XXX I don't want to repeat the entire duplicate_decls here */
4164 ambiguous_decl (name, old, new, flags)
4171 my_friendly_assert (old != NULL_TREE, 393);
4172 /* Copy the value. */
4173 val = BINDING_VALUE (new);
4175 switch (TREE_CODE (val))
4178 /* If we expect types or namespaces, and not templates,
4179 or this is not a template class. */
4180 if (LOOKUP_QUALIFIERS_ONLY (flags)
4181 && !DECL_CLASS_TEMPLATE_P (val))
4185 if (LOOKUP_NAMESPACES_ONLY (flags))
4188 case NAMESPACE_DECL:
4189 if (LOOKUP_TYPES_ONLY (flags))
4193 if (LOOKUP_QUALIFIERS_ONLY (flags))
4197 if (!BINDING_VALUE (old))
4198 BINDING_VALUE (old) = val;
4199 else if (val && val != BINDING_VALUE (old))
4201 if (is_overloaded_fn (BINDING_VALUE (old))
4202 && is_overloaded_fn (val))
4204 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4209 /* Some declarations are functions, some are not. */
4210 if (flags & LOOKUP_COMPLAIN)
4212 /* If we've already given this error for this lookup,
4213 BINDING_VALUE (old) is error_mark_node, so let's not
4214 repeat ourselves. */
4215 if (BINDING_VALUE (old) != error_mark_node)
4217 error ("use of `%D' is ambiguous", name);
4218 cp_error_at (" first declared as `%#D' here",
4219 BINDING_VALUE (old));
4221 cp_error_at (" also declared as `%#D' here", val);
4223 BINDING_VALUE (old) = error_mark_node;
4226 /* ... and copy the type. */
4227 type = BINDING_TYPE (new);
4228 if (LOOKUP_NAMESPACES_ONLY (flags))
4230 if (!BINDING_TYPE (old))
4231 BINDING_TYPE (old) = type;
4232 else if (type && BINDING_TYPE (old) != type)
4234 if (flags & LOOKUP_COMPLAIN)
4236 error ("`%D' denotes an ambiguous type",name);
4237 cp_error_at (" first type here", BINDING_TYPE (old));
4238 cp_error_at (" other type here", type);
4244 /* Subroutine of unualified_namespace_lookup:
4245 Add the bindings of NAME in used namespaces to VAL.
4246 We are currently looking for names in namespace SCOPE, so we
4247 look through USINGS for using-directives of namespaces
4248 which have SCOPE as a common ancestor with the current scope.
4249 Returns zero on errors. */
4252 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4253 tree name, val, usings, scope;
4259 /* Iterate over all used namespaces in current, searching for using
4260 directives of scope. */
4261 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4262 if (TREE_VALUE (iter) == scope)
4265 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4267 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4268 /* Resolve ambiguities. */
4269 val = ambiguous_decl (name, val, val1, flags);
4271 return BINDING_VALUE (val) != error_mark_node;
4275 Accepts the NAME to lookup and its qualifying SCOPE.
4276 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4280 qualified_lookup_using_namespace (name, scope, result, flags)
4286 /* Maintain a list of namespaces visited... */
4287 tree seen = NULL_TREE;
4288 /* ... and a list of namespace yet to see. */
4289 tree todo = NULL_TREE;
4291 /* Look through namespace aliases. */
4292 scope = ORIGINAL_NAMESPACE (scope);
4293 while (scope && (result != error_mark_node))
4295 seen = tree_cons (scope, NULL_TREE, seen);
4296 result = ambiguous_decl (name, result,
4297 binding_for_name (name, scope), flags);
4298 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4299 /* Consider using directives. */
4300 for (usings = DECL_NAMESPACE_USING (scope); usings;
4301 usings = TREE_CHAIN (usings))
4302 /* If this was a real directive, and we have not seen it. */
4303 if (!TREE_INDIRECT_USING (usings)
4304 && !purpose_member (TREE_PURPOSE (usings), seen))
4305 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4308 scope = TREE_PURPOSE (todo);
4309 todo = TREE_CHAIN (todo);
4312 scope = NULL_TREE; /* If there never was a todo list. */
4314 return result != error_mark_node;
4317 /* [namespace.memdef]/2 */
4319 /* Set the context of a declaration to scope. Complain if we are not
4323 set_decl_namespace (decl, scope, friendp)
4330 /* Get rid of namespace aliases. */
4331 scope = ORIGINAL_NAMESPACE (scope);
4333 /* It is ok for friends to be qualified in parallel space. */
4334 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4335 error ("declaration of `%D' not in a namespace surrounding `%D'",
4337 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4338 if (scope != current_namespace)
4340 /* See whether this has been declared in the namespace. */
4341 old = namespace_binding (DECL_NAME (decl), scope);
4343 /* No old declaration at all. */
4345 /* A template can be explicitly specialized in any namespace. */
4346 if (processing_explicit_instantiation)
4348 if (!is_overloaded_fn (decl))
4349 /* Don't compare non-function decls with decls_match here,
4350 since it can't check for the correct constness at this
4351 point. pushdecl will find those errors later. */
4353 /* Since decl is a function, old should contain a function decl. */
4354 if (!is_overloaded_fn (old))
4356 if (processing_template_decl || processing_specialization)
4357 /* We have not yet called push_template_decl to turn the
4358 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4359 won't match. But, we'll check later, when we construct the
4362 for (; old; old = OVL_NEXT (old))
4363 if (decls_match (decl, OVL_CURRENT (old)))
4369 error ("`%D' should have been declared inside `%D'",
4373 /* Compute the namespace where a declaration is defined. */
4376 decl_namespace (decl)
4380 decl = TYPE_STUB_DECL (decl);
4381 while (DECL_CONTEXT (decl))
4383 decl = DECL_CONTEXT (decl);
4384 if (TREE_CODE (decl) == NAMESPACE_DECL)
4387 decl = TYPE_STUB_DECL (decl);
4388 my_friendly_assert (DECL_P (decl), 390);
4391 return global_namespace;
4394 /* Return the namespace where the current declaration is declared. */
4397 current_decl_namespace ()
4400 /* If we have been pushed into a different namespace, use it. */
4401 if (decl_namespace_list)
4402 return TREE_PURPOSE (decl_namespace_list);
4404 if (current_class_type)
4405 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4406 else if (current_function_decl)
4407 result = decl_namespace (current_function_decl);
4409 result = current_namespace;
4413 /* Temporarily set the namespace for the current declaration. */
4416 push_decl_namespace (decl)
4419 if (TREE_CODE (decl) != NAMESPACE_DECL)
4420 decl = decl_namespace (decl);
4421 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4422 NULL_TREE, decl_namespace_list);
4426 pop_decl_namespace ()
4428 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4431 /* Enter a class or namespace scope. */
4437 if (TREE_CODE (t) == NAMESPACE_DECL)
4438 push_decl_namespace (t);
4443 /* Leave scope pushed by push_scope. */
4449 if (TREE_CODE (t) == NAMESPACE_DECL)
4450 pop_decl_namespace ();
4455 /* [basic.lookup.koenig] */
4456 /* A non-zero return value in the functions below indicates an error.
4457 All nodes allocated in the procedure are on the scratch obstack. */
4467 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4468 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4469 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4470 static int add_function PARAMS ((struct arg_lookup *, tree));
4471 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4472 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4473 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4475 /* Add a function to the lookup structure.
4476 Returns 1 on error. */
4479 add_function (k, fn)
4480 struct arg_lookup *k;
4483 /* We used to check here to see if the function was already in the list,
4484 but that's O(n^2), which is just too expensive for function lookup.
4485 Now we deal with the occasional duplicate in joust. In doing this, we
4486 assume that the number of duplicates will be small compared to the
4487 total number of functions being compared, which should usually be the
4490 /* We must find only functions, or exactly one non-function. */
4493 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4494 k->functions = build_overload (fn, k->functions);
4497 tree f1 = OVL_CURRENT (k->functions);
4499 if (is_overloaded_fn (f1))
4501 fn = f1; f1 = f2; f2 = fn;
4503 cp_error_at ("`%D' is not a function,", f1);
4504 cp_error_at (" conflict with `%D'", f2);
4505 error (" in call to `%D'", k->name);
4512 /* Add functions of a namespace to the lookup structure.
4513 Returns 1 on error. */
4516 arg_assoc_namespace (k, scope)
4517 struct arg_lookup *k;
4522 if (purpose_member (scope, k->namespaces))
4524 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4526 value = namespace_binding (k->name, scope);
4530 for (; value; value = OVL_NEXT (value))
4531 if (add_function (k, OVL_CURRENT (value)))
4537 /* Adds everything associated with a template argument to the lookup
4538 structure. Returns 1 on error. */
4541 arg_assoc_template_arg (k, arg)
4542 struct arg_lookup* k;
4545 /* [basic.lookup.koenig]
4547 If T is a template-id, its associated namespaces and classes are
4548 ... the namespaces and classes associated with the types of the
4549 template arguments provided for template type parameters
4550 (excluding template template parameters); the namespaces in which
4551 any template template arguments are defined; and the classes in
4552 which any member templates used as template template arguments
4553 are defined. [Note: non-type template arguments do not
4554 contribute to the set of associated namespaces. ] */
4556 /* Consider first template template arguments. */
4557 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4558 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4560 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4562 tree ctx = CP_DECL_CONTEXT (arg);
4564 /* It's not a member template. */
4565 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4566 return arg_assoc_namespace (k, ctx);
4567 /* Otherwise, it must be member template. */
4569 return arg_assoc_class (k, ctx);
4571 /* It's not a template template argument, but it is a type template
4573 else if (TYPE_P (arg))
4574 return arg_assoc_type (k, arg);
4575 /* It's a non-type template argument. */
4580 /* Adds everything associated with class to the lookup structure.
4581 Returns 1 on error. */
4584 arg_assoc_class (k, type)
4585 struct arg_lookup* k;
4588 tree list, friends, context;
4591 /* Backend build structures, such as __builtin_va_list, aren't
4592 affected by all this. */
4593 if (!CLASS_TYPE_P (type))
4596 if (purpose_member (type, k->classes))
4598 k->classes = tree_cons (type, NULL_TREE, k->classes);
4600 context = decl_namespace (TYPE_MAIN_DECL (type));
4601 if (arg_assoc_namespace (k, context))
4604 /* Process baseclasses. */
4605 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4606 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4609 /* Process friends. */
4610 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4611 list = TREE_CHAIN (list))
4612 if (k->name == TREE_PURPOSE (list))
4613 for (friends = TREE_VALUE (list); friends;
4614 friends = TREE_CHAIN (friends))
4615 /* Only interested in global functions with potentially hidden
4616 (i.e. unqualified) declarations. */
4617 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4618 && decl_namespace (TREE_VALUE (friends)) == context)
4619 if (add_function (k, TREE_VALUE (friends)))
4622 /* Process template arguments. */
4623 if (CLASSTYPE_TEMPLATE_INFO (type))
4625 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4626 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4627 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4633 /* Adds everything associated with a given type.
4634 Returns 1 on error. */
4637 arg_assoc_type (k, type)
4638 struct arg_lookup *k;
4641 switch (TREE_CODE (type))
4652 if (TYPE_PTRMEMFUNC_P (type))
4653 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4654 return arg_assoc_class (k, type);
4656 case REFERENCE_TYPE:
4658 return arg_assoc_type (k, TREE_TYPE (type));
4661 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4663 /* Pointer to member: associate class type and value type. */
4664 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4666 return arg_assoc_type (k, TREE_TYPE (type));
4668 /* The basetype is referenced in the first arg type, so just
4671 /* Associate the parameter types. */
4672 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4674 /* Associate the return type. */
4675 return arg_assoc_type (k, TREE_TYPE (type));
4676 case TEMPLATE_TYPE_PARM:
4677 case BOUND_TEMPLATE_TEMPLATE_PARM:
4682 if (type == unknown_type_node)
4684 /* else fall through */
4691 /* Adds everything associated with arguments. Returns 1 on error. */
4694 arg_assoc_args (k, args)
4695 struct arg_lookup* k;
4698 for (; args; args = TREE_CHAIN (args))
4699 if (arg_assoc (k, TREE_VALUE (args)))
4704 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4708 struct arg_lookup* k;
4711 if (n == error_mark_node)
4715 return arg_assoc_type (k, n);
4717 if (! type_unknown_p (n))
4718 return arg_assoc_type (k, TREE_TYPE (n));
4720 if (TREE_CODE (n) == ADDR_EXPR)
4721 n = TREE_OPERAND (n, 0);
4722 if (TREE_CODE (n) == COMPONENT_REF)
4723 n = TREE_OPERAND (n, 1);
4724 if (TREE_CODE (n) == OFFSET_REF)
4725 n = TREE_OPERAND (n, 1);
4726 while (TREE_CODE (n) == TREE_LIST)
4729 if (TREE_CODE (n) == FUNCTION_DECL)
4730 return arg_assoc_type (k, TREE_TYPE (n));
4731 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4733 /* [basic.lookup.koenig]
4735 If T is a template-id, its associated namespaces and classes
4736 are the namespace in which the template is defined; for
4737 member templates, the member template's class... */
4738 tree template = TREE_OPERAND (n, 0);
4739 tree args = TREE_OPERAND (n, 1);
4743 if (TREE_CODE (template) == COMPONENT_REF)
4744 template = TREE_OPERAND (template, 1);
4746 /* First, the template. There may actually be more than one if
4747 this is an overloaded function template. But, in that case,
4748 we only need the first; all the functions will be in the same
4750 template = OVL_CURRENT (template);
4752 ctx = CP_DECL_CONTEXT (template);
4754 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4756 if (arg_assoc_namespace (k, ctx) == 1)
4759 /* It must be a member template. */
4760 else if (arg_assoc_class (k, ctx) == 1)
4763 /* Now the arguments. */
4764 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4765 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4770 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4772 for (; n; n = OVL_CHAIN (n))
4773 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4780 /* Performs Koenig lookup depending on arguments, where fns
4781 are the functions found in normal lookup. */
4784 lookup_arg_dependent (name, fns, args)
4789 struct arg_lookup k;
4790 tree fn = NULL_TREE;
4794 k.classes = NULL_TREE;
4796 /* Note that we've already looked at some namespaces during normal
4797 unqualified lookup, unless we found a decl in function scope. */
4799 fn = OVL_CURRENT (fns);
4800 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4801 k.namespaces = NULL_TREE;
4803 unqualified_namespace_lookup (name, 0, &k.namespaces);
4805 arg_assoc_args (&k, args);
4809 /* Process a namespace-alias declaration. */
4812 do_namespace_alias (alias, namespace)
4813 tree alias, namespace;
4815 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4817 /* The parser did not find it, so it's not there. */
4818 error ("unknown namespace `%D'", namespace);
4822 namespace = ORIGINAL_NAMESPACE (namespace);
4824 /* Build the alias. */
4825 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4826 DECL_NAMESPACE_ALIAS (alias) = namespace;
4830 /* Check a non-member using-declaration. Return the name and scope
4831 being used, and the USING_DECL, or NULL_TREE on failure. */
4834 validate_nonmember_using_decl (decl, scope, name)
4839 if (TREE_CODE (decl) == SCOPE_REF)
4841 *scope = TREE_OPERAND (decl, 0);
4842 *name = TREE_OPERAND (decl, 1);
4844 if (!processing_template_decl)
4846 /* [namespace.udecl]
4847 A using-declaration for a class member shall be a
4848 member-declaration. */
4849 if(TREE_CODE (*scope) != NAMESPACE_DECL)
4851 if (TYPE_P (*scope))
4852 error ("`%T' is not a namespace", *scope);
4854 error ("`%D' is not a namespace", *scope);
4859 A using-declaration shall not name a template-id. */
4860 if (TREE_CODE (*name) == TEMPLATE_ID_EXPR)
4862 *name = TREE_OPERAND (*name, 0);
4863 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4868 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4869 || TREE_CODE (decl) == TYPE_DECL
4870 || TREE_CODE (decl) == TEMPLATE_DECL)
4872 *scope = global_namespace;
4875 else if (TREE_CODE (decl) == NAMESPACE_DECL)
4877 error ("namespace `%D' not allowed in using-declaration", decl);
4883 *name = DECL_NAME (*name);
4884 /* Make a USING_DECL. */
4885 return push_using_decl (*scope, *name);
4888 /* Process local and global using-declarations. */
4891 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4893 tree oldval, oldtype;
4894 tree *newval, *newtype;
4898 *newval = *newtype = NULL_TREE;
4899 decls = make_node (CPLUS_BINDING);
4900 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4904 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4906 error ("`%D' not declared", name);
4910 /* Check for using functions. */
4911 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4915 if (oldval && !is_overloaded_fn (oldval))
4917 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4922 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4924 tree new_fn = OVL_CURRENT (tmp);
4926 /* [namespace.udecl]
4928 If a function declaration in namespace scope or block
4929 scope has the same name and the same parameter types as a
4930 function introduced by a using declaration the program is
4932 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4934 tree old_fn = OVL_CURRENT (tmp1);
4936 if (new_fn == old_fn)
4937 /* The function already exists in the current namespace. */
4939 else if (OVL_USED (tmp1))
4940 continue; /* this is a using decl */
4941 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4942 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4944 /* There was already a non-using declaration in
4945 this scope with the same parameter types. If both
4946 are the same extern "C" functions, that's ok. */
4947 if (!decls_match (new_fn, old_fn))
4948 error ("`%D' is already declared in this scope", name);
4953 /* If we broke out of the loop, there's no reason to add
4954 this function to the using declarations for this
4959 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4960 if (TREE_CODE (*newval) != OVERLOAD)
4961 *newval = ovl_cons (*newval, NULL_TREE);
4962 OVL_USED (*newval) = 1;
4967 *newval = BINDING_VALUE (decls);
4969 duplicate_decls (*newval, oldval);
4972 *newtype = BINDING_TYPE (decls);
4973 if (oldtype && *newtype && oldtype != *newtype)
4975 error ("using declaration `%D' introduced ambiguous type `%T'",
4981 /* Process a using-declaration not appearing in class or local scope. */
4984 do_toplevel_using_decl (decl)
4987 tree scope, name, binding;
4988 tree oldval, oldtype, newval, newtype;
4990 decl = validate_nonmember_using_decl (decl, &scope, &name);
4991 if (decl == NULL_TREE)
4994 binding = binding_for_name (name, current_namespace);
4996 oldval = BINDING_VALUE (binding);
4997 oldtype = BINDING_TYPE (binding);
4999 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5001 /* Copy declarations found. */
5003 BINDING_VALUE (binding) = newval;
5005 BINDING_TYPE (binding) = newtype;
5009 /* Process a using-declaration at function scope. */
5012 do_local_using_decl (decl)
5016 tree oldval, oldtype, newval, newtype;
5018 decl = validate_nonmember_using_decl (decl, &scope, &name);
5019 if (decl == NULL_TREE)
5022 if (building_stmt_tree ()
5023 && at_function_scope_p ())
5024 add_decl_stmt (decl);
5026 oldval = lookup_name_current_level (name);
5027 oldtype = lookup_type_current_level (name);
5029 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5033 if (is_overloaded_fn (newval))
5037 /* We only need to push declarations for those functions
5038 that were not already bound in the current level.
5039 The old value might be NULL_TREE, it might be a single
5040 function, or an OVERLOAD. */
5041 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5042 term = OVL_FUNCTION (oldval);
5045 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5047 push_overloaded_decl (OVL_CURRENT (fn),
5048 PUSH_LOCAL | PUSH_USING);
5051 push_local_binding (name, newval, PUSH_USING);
5054 set_identifier_type_value (name, newtype);
5058 do_class_using_decl (decl)
5063 if (TREE_CODE (decl) != SCOPE_REF
5064 || !TYPE_P (TREE_OPERAND (decl, 0)))
5066 error ("using-declaration for non-member at class scope");
5069 name = TREE_OPERAND (decl, 1);
5070 if (TREE_CODE (name) == BIT_NOT_EXPR)
5072 error ("using-declaration for destructor");
5075 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5077 name = TREE_OPERAND (name, 0);
5078 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
5081 if (TREE_CODE (name) == TYPE_DECL || TREE_CODE (name) == TEMPLATE_DECL)
5082 name = DECL_NAME (name);
5084 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5086 value = build_lang_decl (USING_DECL, name, void_type_node);
5087 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5091 /* Process a using-directive. */
5094 do_using_directive (namespace)
5097 if (building_stmt_tree ())
5098 add_stmt (build_stmt (USING_STMT, namespace));
5100 /* using namespace A::B::C; */
5101 if (TREE_CODE (namespace) == SCOPE_REF)
5102 namespace = TREE_OPERAND (namespace, 1);
5103 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5105 /* Lookup in lexer did not find a namespace. */
5106 if (!processing_template_decl)
5107 error ("namespace `%T' undeclared", namespace);
5110 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5112 if (!processing_template_decl)
5113 error ("`%T' is not a namespace", namespace);
5116 namespace = ORIGINAL_NAMESPACE (namespace);
5117 if (!toplevel_bindings_p ())
5118 push_using_directive (namespace);
5121 add_using_namespace (current_namespace, namespace, 0);
5125 check_default_args (x)
5128 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5129 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5130 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5132 if (TREE_PURPOSE (arg))
5136 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5147 TREE_USED (decl) = 1;
5148 if (processing_template_decl)
5150 assemble_external (decl);
5152 /* Is it a synthesized method that needs to be synthesized? */
5153 if (TREE_CODE (decl) == FUNCTION_DECL
5154 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5155 && DECL_ARTIFICIAL (decl)
5156 && ! DECL_INITIAL (decl)
5157 /* Kludge: don't synthesize for default args. */
5158 && current_function_decl)
5160 synthesize_method (decl);
5161 /* If we've already synthesized the method we don't need to
5162 instantiate it, so we can return right away. */
5166 /* If this is a function or variable that is an instance of some
5167 template, we now know that we will need to actually do the
5168 instantiation. We check that DECL is not an explicit
5169 instantiation because that is not checked in instantiate_decl. */
5170 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5171 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5172 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5173 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5174 instantiate_decl (decl, /*defer_ok=*/1);
5177 /* Helper function for class_head_decl and class_head_defn
5178 nonterminals. AGGR is the class, union or struct tag. SCOPE is the
5179 explicit scope used (NULL for no scope resolution). ID is the
5180 name. DEFN_P is true, if this is a definition of the class and
5181 NEW_TYPE_P is set to non-zero, if we push into the scope containing
5182 the to be defined aggregate.
5184 Return a TYPE_DECL for the type declared by ID in SCOPE. */
5187 handle_class_head (aggr, scope, id, defn_p, new_type_p)
5188 tree aggr, scope, id;
5192 tree decl = NULL_TREE;
5193 tree current = current_scope ();
5194 bool xrefd_p = false;
5196 if (current == NULL_TREE)
5197 current = current_namespace;
5203 if (TREE_CODE (id) == TYPE_DECL)
5204 /* We must bash typedefs back to the main decl of the
5205 type. Otherwise we become confused about scopes. */
5206 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5207 else if (DECL_CLASS_TEMPLATE_P (id))
5208 decl = DECL_TEMPLATE_RESULT (id);
5213 /* According to the suggested resolution of core issue
5214 180, 'typename' is assumed after a class-key. */
5215 decl = make_typename_type (scope, id, tf_error);
5216 if (decl != error_mark_node)
5217 decl = TYPE_MAIN_DECL (decl);
5221 else if (scope == current)
5223 /* We've been given AGGR SCOPE::ID, when we're already
5224 inside SCOPE. Be nice about it. */
5226 pedwarn ("extra qualification `%T::' on member `%D' ignored",
5230 error ("`%T' does not have a class or union named `%D'",
5237 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, !defn_p));
5241 if (!TYPE_BINFO (TREE_TYPE (decl)))
5243 error ("`%T' is not a class or union type", decl);
5244 return error_mark_node;
5249 /* For a definition, we want to enter the containing scope
5250 before looking up any base classes etc. Only do so, if this
5251 is different to the current scope. */
5252 tree context = CP_DECL_CONTEXT (decl);
5254 *new_type_p = (current != context
5255 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
5256 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
5258 push_scope (context);
5260 if (!xrefd_p && PROCESSING_REAL_TEMPLATE_DECL_P ())
5261 decl = push_template_decl (decl);
5267 /* Initialize decl2.c. */
5272 ggc_add_tree_varray_root (&deferred_fns, 1);
5273 ggc_add_tree_varray_root (&pending_statics, 1);
5274 ggc_add_tree_varray_root (&ssdf_decls, 1);
5275 ggc_add_tree_root (&ssdf_decl, 1);
5276 ggc_add_tree_root (&priority_decl, 1);
5277 ggc_add_tree_root (&initialize_p_decl, 1);