1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 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. */
44 #include "dwarf2out.h"
49 extern cpp_reader *parse_in;
51 /* This structure contains information about the initializations
52 and/or destructions required for a particular priority level. */
53 typedef struct priority_info_s {
54 /* Non-zero if there have been any initializations at this priority
55 throughout the translation unit. */
56 int initializations_p;
57 /* Non-zero if there have been any destructions at this priority
58 throughout the translation unit. */
62 static void mark_vtable_entries PARAMS ((tree));
63 static void grok_function_init PARAMS ((tree, tree));
64 static int finish_vtable_vardecl PARAMS ((tree *, void *));
65 static int prune_vtable_vardecl PARAMS ((tree *, void *));
66 static int is_namespace_ancestor PARAMS ((tree, tree));
67 static void add_using_namespace PARAMS ((tree, tree, int));
68 static tree ambiguous_decl PARAMS ((tree, tree, tree,int));
69 static tree build_anon_union_vars PARAMS ((tree, tree*, int, int));
70 static int acceptable_java_type PARAMS ((tree));
71 static void output_vtable_inherit PARAMS ((tree));
72 static tree start_objects PARAMS ((int, int));
73 static void finish_objects PARAMS ((int, int, tree));
74 static tree merge_functions PARAMS ((tree, tree));
75 static tree decl_namespace PARAMS ((tree));
76 static tree validate_nonmember_using_decl PARAMS ((tree, tree *, tree *));
77 static void do_nonmember_using_decl PARAMS ((tree, tree, tree, tree,
79 static tree start_static_storage_duration_function PARAMS ((void));
80 static void finish_static_storage_duration_function PARAMS ((tree));
81 static priority_info get_priority_info PARAMS ((int));
82 static void do_static_initialization PARAMS ((tree, tree));
83 static void do_static_destruction PARAMS ((tree));
84 static tree start_static_initialization_or_destruction PARAMS ((tree, int));
85 static void finish_static_initialization_or_destruction PARAMS ((tree));
86 static void generate_ctor_or_dtor_function PARAMS ((int, int));
87 static int generate_ctor_and_dtor_functions_for_priority
88 PARAMS ((splay_tree_node, void *));
89 static tree prune_vars_needing_no_initialization PARAMS ((tree));
90 static void write_out_vars PARAMS ((tree));
91 static void import_export_class PARAMS ((tree));
92 static tree key_method PARAMS ((tree));
93 static int compare_options PARAMS ((const PTR, const PTR));
94 static tree get_guard_bits PARAMS ((tree));
96 extern int current_class_depth;
98 /* A list of virtual function tables we must make sure to write out. */
101 /* A list of static class variables. This is needed, because a
102 static class variable can be declared inside the class without
103 an initializer, and then initialized, staticly, outside the class. */
104 static varray_type pending_statics;
105 #define pending_statics_used \
106 (pending_statics ? pending_statics->elements_used : 0)
108 /* A list of functions which were declared inline, but which we
109 may need to emit outline anyway. */
110 static varray_type deferred_fns;
111 #define deferred_fns_used \
112 (deferred_fns ? deferred_fns->elements_used : 0)
114 /* Same, but not reset. Local temp variables and global temp variables
115 can have the same name. */
116 static int global_temp_name_counter;
118 /* Flag used when debugging spew.c */
120 extern int spew_debug;
122 /* Nonzero if we're done parsing and into end-of-file activities. */
126 /* Functions called along with real static constructors and destructors. */
131 /* The :: namespace. */
133 tree global_namespace;
135 /* The stack for namespaces of current declarations. */
137 static tree decl_namespace_list;
140 /* C (and C++) language-specific option variables. */
142 /* Nonzero means allow type mismatches in conditional expressions;
143 just make their values `void'. */
145 int flag_cond_mismatch;
147 /* Nonzero means give `double' the same size as `float'. */
149 int flag_short_double;
151 /* Nonzero means don't recognize the keyword `asm'. */
155 /* Nonzero means don't recognize any extension keywords. */
157 int flag_no_gnu_keywords;
159 /* Nonzero means do some things the same way PCC does. Only provided so
160 the compiler will link. */
162 int flag_traditional;
164 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
166 int flag_signed_bitfields = 1;
168 /* Nonzero means enable obscure standard features and disable GNU
169 extensions that might cause standard-compliant code to be
174 /* Nonzero means do emit exported implementations of functions even if
175 they can be inlined. */
177 int flag_implement_inlines = 1;
179 /* Nonzero means do emit exported implementations of templates, instead of
180 multiple static copies in each file that needs a definition. */
182 int flag_external_templates;
184 /* Nonzero means that the decision to emit or not emit the implementation of a
185 template depends on where the template is instantiated, rather than where
188 int flag_alt_external_templates;
190 /* Nonzero means that implicit instantiations will be emitted if needed. */
192 int flag_implicit_templates = 1;
194 /* Nonzero means that implicit instantiations of inline templates will be
195 emitted if needed, even if instantiations of non-inline templates
198 int flag_implicit_inline_templates = 1;
200 /* Nonzero means warn about implicit declarations. */
202 int warn_implicit = 1;
204 /* Nonzero means warn about usage of long long when `-pedantic'. */
206 int warn_long_long = 1;
208 /* Nonzero means warn when all ctors or dtors are private, and the class
211 int warn_ctor_dtor_privacy = 1;
213 /* True if we want to implement vtables using "thunks".
214 The default is off. */
216 #ifndef DEFAULT_VTABLE_THUNKS
217 #define DEFAULT_VTABLE_THUNKS 0
219 int flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
221 /* Nonzero means generate separate instantiation control files and juggle
222 them at link time. */
224 int flag_use_repository;
226 /* Nonzero if we want to issue diagnostics that the standard says are not
229 int flag_optional_diags = 1;
231 /* Nonzero means give string constants the type `const char *', as mandated
234 int flag_const_strings = 1;
236 /* Nonzero means warn about deprecated conversion from string constant to
239 int warn_write_strings;
241 /* Nonzero means warn about pointer casts that can drop a type qualifier
242 from the pointer target type. */
246 /* Nonzero means warn about sizeof(function) or addition/subtraction
247 of function pointers. */
249 int warn_pointer_arith = 1;
251 /* Nonzero means warn for any function def without prototype decl. */
253 int warn_missing_prototypes;
255 /* Nonzero means warn about multiple (redundant) decls for the same single
256 variable or function. */
258 int warn_redundant_decls;
260 /* Warn if initializer is not completely bracketed. */
262 int warn_missing_braces;
264 /* Warn about comparison of signed and unsigned values. */
266 int warn_sign_compare;
268 /* Warn about testing equality of floating point numbers. */
270 int warn_float_equal = 0;
272 /* Warn about functions which might be candidates for format attributes. */
274 int warn_missing_format_attribute;
276 /* Warn about a subscript that has type char. */
278 int warn_char_subscripts;
280 /* Warn if a type conversion is done that might have confusing results. */
284 /* Warn if adding () is suggested. */
286 int warn_parentheses;
288 /* Non-zero means warn in function declared in derived class has the
289 same name as a virtual in the base class, but fails to match the
290 type signature of any virtual function in the base class. */
291 int warn_overloaded_virtual;
293 /* Non-zero means warn when declaring a class that has a non virtual
294 destructor, when it really ought to have a virtual one. */
297 /* Non-zero means warn when a function is declared extern and later inline. */
298 int warn_extern_inline;
300 /* Non-zero means warn when the compiler will reorder code. */
303 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
306 /* Non-zero means warn when we convert a pointer to member function
307 into a pointer to (void or function). */
308 int warn_pmf2ptr = 1;
310 /* Nonzero means warn about violation of some Effective C++ style rules. */
314 /* Nonzero means warn where overload resolution chooses a promotion from
315 unsigned to signed over a conversion to an unsigned of the same size. */
319 /* Nonzero means warn when an old-style cast is used. */
321 int warn_old_style_cast;
323 /* Warn about #pragma directives that are not recognised. */
325 int warn_unknown_pragmas; /* Tri state variable. */
327 /* Nonzero means warn about use of multicharacter literals. */
329 int warn_multichar = 1;
331 /* Nonzero means warn when non-templatized friend functions are
332 declared within a template */
334 int warn_nontemplate_friend = 1;
336 /* Nonzero means complain about deprecated features. */
338 int warn_deprecated = 1;
340 /* Nonzero means `$' can be in an identifier. */
342 #ifndef DOLLARS_IN_IDENTIFIERS
343 #define DOLLARS_IN_IDENTIFIERS 1
345 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
347 /* Nonzero means that labels can be used as first-class objects */
351 /* Nonzero means allow Microsoft extensions without a pedwarn. */
353 int flag_ms_extensions;
355 /* C++ specific flags. */
356 /* Zero means that `this' is a *const. This gives nice behavior in the
357 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
358 -2 means we're constructing an object and it has fixed type. */
360 int flag_this_is_variable;
362 /* Nonzero means we should attempt to elide constructors when possible. */
364 int flag_elide_constructors = 1;
366 /* Nonzero means that member functions defined in class scope are
367 inline by default. */
369 int flag_default_inline = 1;
371 /* Controls whether compiler generates 'type descriptor' that give
372 run-time type information. */
375 /* Nonzero if we wish to output cross-referencing information
376 for the GNU class browser. */
377 extern int flag_gnu_xref;
379 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
382 int flag_huge_objects;
384 /* Nonzero if we want to conserve space in the .o files. We do this
385 by putting uninitialized data and runtime initialized data into
386 .common instead of .data at the expense of not flagging multiple
389 int flag_conserve_space;
391 /* Nonzero if we want to obey access control semantics. */
393 int flag_access_control = 1;
395 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
397 int flag_operator_names = 1;
399 /* Nonzero if we want to check the return value of new and avoid calling
400 constructors if it is a null pointer. */
404 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
405 initialization variables.
406 0: Old rules, set by -fno-for-scope.
407 2: New ISO rules, set by -ffor-scope.
408 1: Try to implement new ISO rules, but with backup compatibility
409 (and warnings). This is the default, for now. */
411 int flag_new_for_scope = 1;
413 /* Nonzero if we want to emit defined symbols with common-like linkage as
414 weak symbols where possible, in order to conform to C++ semantics.
415 Otherwise, emit them as local symbols. */
419 /* Nonzero to enable experimental ABI changes. */
423 /* Nonzero to use __cxa_atexit, rather than atexit, to register
424 destructors for local statics and global objects. */
426 int flag_use_cxa_atexit;
428 /* Nonzero to not ignore namespace std. */
430 int flag_honor_std = ENABLE_STD_NAMESPACE;
432 /* Nonzero if we should expand functions calls inline at the tree
433 level, rather than at the RTL level. */
435 int flag_inline_trees = 0;
437 /* Maximum template instantiation depth. Must be at least 17 for ISO
440 int max_tinst_depth = 17;
442 /* The name-mangling scheme to use. Must be 1 or greater to support
443 template functions with identical types, but different template
445 int name_mangling_version = 2;
447 /* Nonzero if wchar_t should be `unsigned short' instead of whatever it
448 would normally be, for use with WINE. */
449 int flag_short_wchar;
451 /* Nonzero if squashed mangling is to be performed.
452 This uses the B and K codes to reference previously seen class types
453 and class qualifiers. */
454 int flag_do_squangling;
456 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
460 /* Nonzero means make the default pedwarns warnings instead of errors.
461 The value of this flag is ignored if -pedantic is specified. */
465 /* Nonzero means to implement standard semantics for exception
466 specifications, calling unexpected if an exception is thrown that
467 doesn't match the specification. Zero means to treat them as
468 assertions and optimize accordingly, but not check them. */
470 int flag_enforce_eh_specs = 1;
472 /* The variant of the C language being processed. */
474 c_language_kind c_language = clk_cplusplus;
476 /* Table of language-dependent -f options.
477 STRING is the option name. VARIABLE is the address of the variable.
478 ON_VALUE is the value to store in VARIABLE
479 if `-fSTRING' is seen as an option.
480 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
482 static struct { const char *string; int *variable; int on_value;}
486 {"signed-char", &flag_signed_char, 1},
487 {"unsigned-char", &flag_signed_char, 0},
488 {"signed-bitfields", &flag_signed_bitfields, 1},
489 {"unsigned-bitfields", &flag_signed_bitfields, 0},
490 {"short-enums", &flag_short_enums, 1},
491 {"short-double", &flag_short_double, 1},
492 {"short-wchar", &flag_short_wchar, 1},
493 {"cond-mismatch", &flag_cond_mismatch, 1},
494 {"asm", &flag_no_asm, 0},
495 {"builtin", &flag_no_builtin, 0},
497 /* C++-only options. */
498 {"access-control", &flag_access_control, 1},
499 {"check-new", &flag_check_new, 1},
500 {"conserve-space", &flag_conserve_space, 1},
501 {"const-strings", &flag_const_strings, 1},
502 {"default-inline", &flag_default_inline, 1},
503 {"dollars-in-identifiers", &dollars_in_ident, 1},
504 {"elide-constructors", &flag_elide_constructors, 1},
505 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
506 {"external-templates", &flag_external_templates, 1},
507 {"for-scope", &flag_new_for_scope, 2},
508 {"gnu-keywords", &flag_no_gnu_keywords, 0},
509 {"handle-exceptions", &flag_exceptions, 1},
510 {"honor-std", &flag_honor_std, 1},
511 {"huge-objects", &flag_huge_objects, 1},
512 {"implement-inlines", &flag_implement_inlines, 1},
513 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
514 {"implicit-templates", &flag_implicit_templates, 1},
515 {"labels-ok", &flag_labels_ok, 1},
516 {"ms-extensions", &flag_ms_extensions, 1},
517 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
518 {"operator-names", &flag_operator_names, 1},
519 {"optional-diags", &flag_optional_diags, 1},
520 {"permissive", &flag_permissive, 1},
521 {"repo", &flag_use_repository, 1},
522 {"rtti", &flag_rtti, 1},
523 {"squangle", &flag_do_squangling, 1},
524 {"stats", &flag_detailed_statistics, 1},
525 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
526 {"vtable-gc", &flag_vtable_gc, 1},
527 {"vtable-thunks", &flag_vtable_thunks, 1},
528 {"weak", &flag_weak, 1},
529 {"xref", &flag_gnu_xref, 1}
532 /* The list of `-f' options that we no longer support. The `-f'
533 prefix is not given in this table. The `-fno-' variants are not
534 listed here. This table must be kept in alphabetical order. */
535 static const char * const unsupported_options[] = {
544 /* Compare two option strings, pointed two by P1 and P2, for use with
548 compare_options (p1, p2)
552 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
555 /* Decode the string P as a language-specific option.
556 Return the number of strings consumed for a valid option.
557 Otherwise return 0. Should not complain if it does not
558 recognise the option. */
561 lang_decode_option (argc, argv)
565 int strings_processed;
566 const char *p = argv[0];
568 strings_processed = cpp_handle_option (parse_in, argc, argv);
570 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
572 else if (p[0] == '-' && p[1] == 'f')
574 /* Some kind of -f option.
575 P's value is the option sans `-f'.
576 Search for it in the table of options. */
577 const char *option_value = NULL;
578 const char *positive_option;
582 /* Try special -f options. */
584 /* See if this is one of the options no longer supported. We
585 used to support these options, so we continue to accept them,
587 if (strncmp (p, "no-", strlen ("no-")) == 0)
588 positive_option = p + strlen ("no-");
592 /* If the option is present, issue a warning. Indicate to our
593 caller that the option was processed successfully. */
594 if (bsearch (&positive_option,
596 (sizeof (unsupported_options)
597 / sizeof (unsupported_options[0])),
598 sizeof (unsupported_options[0]),
601 warning ("-f%s is no longer supported", p);
605 if (!strcmp (p, "handle-exceptions")
606 || !strcmp (p, "no-handle-exceptions"))
607 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
608 else if (! strcmp (p, "alt-external-templates"))
610 flag_external_templates = 1;
611 flag_alt_external_templates = 1;
612 cp_deprecated ("-falt-external-templates");
614 else if (! strcmp (p, "no-alt-external-templates"))
615 flag_alt_external_templates = 0;
616 else if (!strcmp (p, "repo"))
618 flag_use_repository = 1;
619 flag_implicit_templates = 0;
621 else if (!strcmp (p, "external-templates"))
623 flag_external_templates = 1;
624 cp_deprecated ("-fexternal-templates");
626 else if (!strcmp (p, "new-abi"))
629 flag_do_squangling = 1;
630 flag_vtable_thunks = 1;
632 else if (!strcmp (p, "no-new-abi"))
635 flag_do_squangling = 0;
637 else if ((option_value
638 = skip_leading_substring (p, "template-depth-")))
640 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
641 else if ((option_value
642 = skip_leading_substring (p, "name-mangling-version-")))
643 name_mangling_version
644 = read_integral_parameter (option_value, p - 2, name_mangling_version);
645 else if ((option_value
646 = skip_leading_substring (p, "dump-translation-unit-")))
649 error ("no file specified with -fdump-translation-unit");
651 flag_dump_translation_unit = option_value;
658 !found && j < (sizeof (lang_f_options)
659 / sizeof (lang_f_options[0]));
662 if (!strcmp (p, lang_f_options[j].string))
664 *lang_f_options[j].variable = lang_f_options[j].on_value;
665 /* A goto here would be cleaner,
666 but breaks the vax pcc. */
669 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
670 && ! strcmp (p+3, lang_f_options[j].string))
672 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
680 else if (p[0] == '-' && p[1] == 'W')
684 /* The -W options control the warning behavior of the compiler. */
687 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
690 if (!strcmp (p, "implicit"))
691 warn_implicit = setting;
692 else if (!strcmp (p, "long-long"))
693 warn_long_long = setting;
694 else if (!strcmp (p, "return-type"))
695 warn_return_type = setting;
696 else if (!strcmp (p, "ctor-dtor-privacy"))
697 warn_ctor_dtor_privacy = setting;
698 else if (!strcmp (p, "write-strings"))
699 warn_write_strings = setting;
700 else if (!strcmp (p, "cast-qual"))
701 warn_cast_qual = setting;
702 else if (!strcmp (p, "char-subscripts"))
703 warn_char_subscripts = setting;
704 else if (!strcmp (p, "pointer-arith"))
705 warn_pointer_arith = setting;
706 else if (!strcmp (p, "missing-prototypes"))
707 warn_missing_prototypes = setting;
708 else if (!strcmp (p, "strict-prototypes"))
711 warning ("-Wno-strict-prototypes is not supported in C++");
713 else if (!strcmp (p, "redundant-decls"))
714 warn_redundant_decls = setting;
715 else if (!strcmp (p, "missing-braces"))
716 warn_missing_braces = setting;
717 else if (!strcmp (p, "sign-compare"))
718 warn_sign_compare = setting;
719 else if (!strcmp (p, "float-equal"))
720 warn_float_equal = setting;
721 else if (!strcmp (p, "format"))
722 set_Wformat (setting);
723 else if (!strcmp (p, "format-y2k"))
724 warn_format_y2k = setting;
725 else if (!strcmp (p, "format-extra-args"))
726 warn_format_extra_args = setting;
727 else if (!strcmp (p, "format-nonliteral"))
728 warn_format_nonliteral = setting;
729 else if (!strcmp (p, "format-security"))
730 warn_format_security = setting;
731 else if (!strcmp (p, "missing-format-attribute"))
732 warn_missing_format_attribute = setting;
733 else if (!strcmp (p, "conversion"))
734 warn_conversion = setting;
735 else if (!strcmp (p, "parentheses"))
736 warn_parentheses = setting;
737 else if (!strcmp (p, "non-virtual-dtor"))
738 warn_nonvdtor = setting;
739 else if (!strcmp (p, "extern-inline"))
740 warn_extern_inline = setting;
741 else if (!strcmp (p, "reorder"))
742 warn_reorder = setting;
743 else if (!strcmp (p, "synth"))
744 warn_synth = setting;
745 else if (!strcmp (p, "pmf-conversions"))
746 warn_pmf2ptr = setting;
747 else if (!strcmp (p, "effc++"))
749 else if (!strcmp (p, "sign-promo"))
750 warn_sign_promo = setting;
751 else if (!strcmp (p, "old-style-cast"))
752 warn_old_style_cast = setting;
753 else if (!strcmp (p, "overloaded-virtual"))
754 warn_overloaded_virtual = setting;
755 else if (!strcmp (p, "multichar"))
756 warn_multichar = setting;
757 else if (!strcmp (p, "unknown-pragmas"))
758 /* Set to greater than 1, so that even unknown pragmas in
759 system headers will be warned about. */
760 warn_unknown_pragmas = setting * 2;
761 else if (!strcmp (p, "non-template-friend"))
762 warn_nontemplate_friend = setting;
763 else if (!strcmp (p, "deprecated"))
764 warn_deprecated = setting;
765 else if (!strcmp (p, "comment"))
766 ; /* cpp handles this one. */
767 else if (!strcmp (p, "comments"))
768 ; /* cpp handles this one. */
769 else if (!strcmp (p, "trigraphs"))
770 ; /* cpp handles this one. */
771 else if (!strcmp (p, "import"))
772 ; /* cpp handles this one. */
773 else if (!strcmp (p, "all"))
775 warn_return_type = setting;
776 set_Wunused (setting);
777 warn_implicit = setting;
778 warn_switch = setting;
779 set_Wformat (setting);
780 warn_parentheses = setting;
781 warn_missing_braces = setting;
782 warn_sign_compare = setting;
783 warn_multichar = setting;
784 /* We save the value of warn_uninitialized, since if they put
785 -Wuninitialized on the command line, we need to generate a
786 warning about not using it without also specifying -O. */
787 if (warn_uninitialized != 1)
788 warn_uninitialized = (setting ? 2 : 0);
789 /* Only warn about unknown pragmas that are not in system
791 warn_unknown_pragmas = 1;
793 /* C++-specific warnings. */
794 warn_ctor_dtor_privacy = setting;
795 warn_nonvdtor = setting;
796 warn_reorder = setting;
797 warn_nontemplate_friend = setting;
799 else return strings_processed;
801 else if (!strcmp (p, "-ansi"))
802 flag_no_nonansi_builtin = 1, flag_ansi = 1,
803 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
805 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
806 it's probably safe to assume no sane person would ever want to use this
807 under normal circumstances. */
808 else if (!strcmp (p, "-spew-debug"))
812 return strings_processed;
817 /* Incorporate `const' and `volatile' qualifiers for member functions.
818 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
819 QUALS is a list of qualifiers. Returns any explicit
820 top-level qualifiers of the method's this pointer, anything other than
821 TYPE_UNQUALIFIED will be an extension. */
824 grok_method_quals (ctype, function, quals)
825 tree ctype, function, quals;
827 tree fntype = TREE_TYPE (function);
828 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
829 int type_quals = TYPE_UNQUALIFIED;
830 int dup_quals = TYPE_UNQUALIFIED;
831 int this_quals = TYPE_UNQUALIFIED;
835 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
837 if ((type_quals | this_quals) & tq)
839 else if (tq & TYPE_QUAL_RESTRICT)
843 quals = TREE_CHAIN (quals);
847 if (dup_quals != TYPE_UNQUALIFIED)
848 cp_error ("duplicate type qualifiers in %s declaration",
849 TREE_CODE (function) == FUNCTION_DECL
850 ? "member function" : "type");
852 ctype = cp_build_qualified_type (ctype, type_quals);
853 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
854 (TREE_CODE (fntype) == METHOD_TYPE
855 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
856 : TYPE_ARG_TYPES (fntype)));
858 fntype = build_exception_variant (fntype, raises);
860 TREE_TYPE (function) = fntype;
864 /* Warn when -fexternal-templates is used and #pragma
865 interface/implementation is not used all the times it should be,
869 warn_if_unknown_interface (decl)
872 static int already_warned = 0;
873 if (already_warned++)
876 if (flag_alt_external_templates)
878 tree til = tinst_for_decl ();
880 const char *sf = input_filename;
884 lineno = TINST_LINE (til);
885 input_filename = TINST_FILE (til);
887 cp_warning ("template `%#D' instantiated in file without #pragma interface",
893 cp_warning_at ("template `%#D' defined in file without #pragma interface",
897 /* A subroutine of the parser, to handle a component list. */
900 grok_x_components (specs)
905 specs = strip_attrs (specs);
907 check_tag_decl (specs);
908 t = groktypename (build_tree_list (specs, NULL_TREE));
910 /* The only case where we need to do anything additional here is an
911 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
912 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
915 fixup_anonymous_aggr (t);
916 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
919 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
923 build_artificial_parm (name, type)
929 parm = build_decl (PARM_DECL, name, type);
930 DECL_ARTIFICIAL (parm) = 1;
931 DECL_ARG_TYPE (parm) = type;
935 /* Constructors for types with virtual baseclasses need an "in-charge" flag
936 saying whether this constructor is responsible for initialization of
937 virtual baseclasses or not. All destructors also need this "in-charge"
938 flag, which additionally determines whether or not the destructor should
939 free the memory for the object.
941 This function adds the "in-charge" flag to member function FN if
942 appropriate. It is called from grokclassfn and tsubst.
943 FN must be either a constructor or destructor. */
946 maybe_retrofit_in_chrg (fn)
949 tree basetype, arg_types, parms, parm, fntype;
951 /* If we've already add the in-charge parameter don't do it again. */
952 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
955 /* When processing templates we can't know, in general, whether or
956 not we're going to have virtual baseclasses. */
957 if (uses_template_parms (fn))
960 /* We don't need an in-charge parameter for constructors that don't
961 have virtual bases. */
962 if (DECL_CONSTRUCTOR_P (fn)
963 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
966 /* First add it to DECL_ARGUMENTS... */
967 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
968 TREE_READONLY (parm) = 1;
969 parms = DECL_ARGUMENTS (fn);
970 TREE_CHAIN (parm) = TREE_CHAIN (parms);
971 TREE_CHAIN (parms) = parm;
973 /* ...and then to TYPE_ARG_TYPES. */
974 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
975 basetype = TREE_TYPE (TREE_VALUE (arg_types));
976 arg_types = hash_tree_chain (integer_type_node, TREE_CHAIN (arg_types));
977 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
979 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
980 fntype = build_exception_variant (fntype,
981 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
982 TREE_TYPE (fn) = fntype;
984 /* Now we've got the in-charge parameter. */
985 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
987 /* If this is a subobject constructor or destructor, our caller will
988 pass us a pointer to our VTT. */
989 if (flag_new_abi && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
991 DECL_VTT_PARM (fn) = build_artificial_parm (vtt_parm_identifier,
993 DECL_CONTEXT (DECL_VTT_PARM (fn)) = fn;
994 DECL_USE_VTT_PARM (fn) = build_artificial_parm (NULL_TREE,
996 DECL_CONTEXT (DECL_USE_VTT_PARM (fn)) = fn;
1000 /* Classes overload their constituent function names automatically.
1001 When a function name is declared in a record structure,
1002 its name is changed to it overloaded name. Since names for
1003 constructors and destructors can conflict, we place a leading
1004 '$' for destructors.
1006 CNAME is the name of the class we are grokking for.
1008 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
1010 FLAGS contains bits saying what's special about today's
1011 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
1013 If FUNCTION is a destructor, then we must add the `auto-delete' field
1014 as a second parameter. There is some hair associated with the fact
1015 that we must "declare" this variable in the manner consistent with the
1016 way the rest of the arguments were declared.
1018 QUALS are the qualifiers for the this pointer. */
1021 grokclassfn (ctype, function, flags, quals)
1022 tree ctype, function;
1023 enum overload_flags flags;
1026 tree fn_name = DECL_NAME (function);
1027 int this_quals = TYPE_UNQUALIFIED;
1029 /* Even within an `extern "C"' block, members get C++ linkage. See
1030 [dcl.link] for details. */
1031 DECL_LANGUAGE (function) = lang_cplusplus;
1033 if (fn_name == NULL_TREE)
1035 error ("name missing for member function");
1036 fn_name = get_identifier ("<anonymous>");
1037 DECL_NAME (function) = fn_name;
1041 this_quals = grok_method_quals (ctype, function, quals);
1043 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1045 /* Must add the class instance variable up front. */
1046 /* Right now we just make this a pointer. But later
1047 we may wish to make it special. */
1048 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
1052 /* The `this' parameter is implicitly `const'; it cannot be
1054 this_quals |= TYPE_QUAL_CONST;
1055 qual_type = cp_build_qualified_type (type, this_quals);
1056 parm = build_artificial_parm (this_identifier, qual_type);
1057 c_apply_type_quals_to_decl (this_quals, parm);
1059 /* We can make this a register, so long as we don't
1060 accidentally complain if someone tries to take its address. */
1061 DECL_REGISTER (parm) = 1;
1062 TREE_CHAIN (parm) = last_function_parms;
1063 last_function_parms = parm;
1066 DECL_ARGUMENTS (function) = last_function_parms;
1067 DECL_CONTEXT (function) = ctype;
1069 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1070 maybe_retrofit_in_chrg (function);
1072 if (flags == DTOR_FLAG)
1074 DECL_DESTRUCTOR_P (function) = 1;
1077 set_mangled_name_for_decl (function);
1079 DECL_ASSEMBLER_NAME (function) = build_destructor_name (ctype);
1081 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1084 set_mangled_name_for_decl (function);
1087 /* Work on the expr used by alignof (this is only called by the parser). */
1096 if (processing_template_decl)
1097 return build_min (ALIGNOF_EXPR, sizetype, expr);
1099 if (TREE_CODE (expr) == COMPONENT_REF
1100 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1101 error ("`__alignof__' applied to a bit-field");
1103 if (TREE_CODE (expr) == INDIRECT_REF)
1105 best = t = TREE_OPERAND (expr, 0);
1106 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1108 while (TREE_CODE (t) == NOP_EXPR
1109 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1112 t = TREE_OPERAND (t, 0);
1113 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1114 if (thisalign > bestalign)
1115 best = t, bestalign = thisalign;
1117 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1120 return c_alignof (TREE_TYPE (expr));
1123 /* Create an ARRAY_REF, checking for the user doing things backwards
1127 grok_array_decl (array_expr, index_exp)
1128 tree array_expr, index_exp;
1130 tree type = TREE_TYPE (array_expr);
1131 tree p1, p2, i1, i2;
1133 if (type == error_mark_node || index_exp == error_mark_node)
1134 return error_mark_node;
1135 if (processing_template_decl)
1136 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1137 array_expr, index_exp);
1139 if (type == NULL_TREE)
1141 /* Something has gone very wrong. Assume we are mistakenly reducing
1142 an expression instead of a declaration. */
1143 error ("parser may be lost: is there a '{' missing somewhere?");
1147 if (TREE_CODE (type) == OFFSET_TYPE
1148 || TREE_CODE (type) == REFERENCE_TYPE)
1149 type = TREE_TYPE (type);
1151 /* If they have an `operator[]', use that. */
1152 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1153 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1154 array_expr, index_exp, NULL_TREE);
1156 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1157 is a little-known fact that, if `a' is an array and `i' is an
1158 int, you can write `i[a]', which means the same thing as `a[i]'. */
1160 if (TREE_CODE (type) == ARRAY_TYPE)
1163 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1165 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1168 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1170 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1171 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1173 if ((p1 && i2) && (i1 && p2))
1174 error ("ambiguous conversion for array subscript");
1177 array_expr = p1, index_exp = i2;
1179 array_expr = p2, index_exp = i1;
1182 cp_error ("invalid types `%T[%T]' for array subscript",
1183 type, TREE_TYPE (index_exp));
1184 return error_mark_node;
1187 if (array_expr == error_mark_node || index_exp == error_mark_node)
1188 error ("ambiguous conversion for array subscript");
1190 return build_array_ref (array_expr, index_exp);
1193 /* Given the cast expression EXP, checking out its validity. Either return
1194 an error_mark_node if there was an unavoidable error, return a cast to
1195 void for trying to delete a pointer w/ the value 0, or return the
1196 call to delete. If DOING_VEC is 1, we handle things differently
1197 for doing an array delete. If DOING_VEC is 2, they gave us the
1198 array size as an argument to delete.
1199 Implements ARM $5.3.4. This is called from the parser. */
1202 delete_sanity (exp, size, doing_vec, use_global_delete)
1204 int doing_vec, use_global_delete;
1207 /* For a regular vector delete (aka, no size argument) we will pass
1208 this down as a NULL_TREE into build_vec_delete. */
1209 tree maxindex = NULL_TREE;
1211 if (exp == error_mark_node)
1214 if (processing_template_decl)
1216 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1217 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1218 DELETE_EXPR_USE_VEC (t) = doing_vec;
1222 if (TREE_CODE (exp) == OFFSET_REF)
1223 exp = resolve_offset_ref (exp);
1224 exp = convert_from_reference (exp);
1225 t = stabilize_reference (exp);
1226 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1228 if (t == NULL_TREE || t == error_mark_node)
1230 cp_error ("type `%#T' argument given to `delete', expected pointer",
1232 return error_mark_node;
1237 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1238 pedwarn ("anachronistic use of array size in vector delete");
1241 type = TREE_TYPE (t);
1243 /* As of Valley Forge, you can delete a pointer to const. */
1245 /* You can't delete functions. */
1246 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1248 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1249 return error_mark_node;
1252 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1253 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1255 cp_warning ("deleting `%T' is undefined", type);
1259 /* An array can't have been allocated by new, so complain. */
1260 if (TREE_CODE (t) == ADDR_EXPR
1261 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1262 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1263 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1265 /* Deleting a pointer with the value zero is valid and has no effect. */
1266 if (integer_zerop (t))
1267 return build1 (NOP_EXPR, void_type_node, t);
1270 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1274 if (IS_AGGR_TYPE (TREE_TYPE (type))
1275 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1277 /* Only do access checking here; we'll be calling op delete
1278 from the destructor. */
1279 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1280 LOOKUP_NORMAL, NULL_TREE);
1281 if (tmp == error_mark_node)
1282 return error_mark_node;
1285 return build_delete (type, t, sfk_deleting_destructor,
1286 LOOKUP_NORMAL, use_global_delete);
1290 /* Report an error if the indicated template declaration is not the
1291 sort of thing that should be a member template. */
1294 check_member_template (tmpl)
1299 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1300 decl = DECL_TEMPLATE_RESULT (tmpl);
1302 if (TREE_CODE (decl) == FUNCTION_DECL
1303 || (TREE_CODE (decl) == TYPE_DECL
1304 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1306 if (current_function_decl)
1307 /* 14.5.2.2 [temp.mem]
1309 A local class shall not have member templates. */
1310 cp_error ("invalid declaration of member template `%#D' in local class",
1313 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1315 /* 14.5.2.3 [temp.mem]
1317 A member function template shall not be virtual. */
1319 ("invalid use of `virtual' in template declaration of `%#D'",
1321 DECL_VIRTUAL_P (decl) = 0;
1324 /* The debug-information generating code doesn't know what to do
1325 with member templates. */
1326 DECL_IGNORED_P (tmpl) = 1;
1329 cp_error ("template declaration of `%#D'", decl);
1332 /* Return true iff TYPE is a valid Java parameter or return type. */
1335 acceptable_java_type (type)
1338 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1340 if (TREE_CODE (type) == POINTER_TYPE)
1342 type = TREE_TYPE (type);
1343 if (TREE_CODE (type) == RECORD_TYPE)
1346 if (! TYPE_FOR_JAVA (type))
1348 if (! CLASSTYPE_TEMPLATE_INFO (type))
1350 args = CLASSTYPE_TI_ARGS (type);
1351 i = TREE_VEC_LENGTH (args);
1354 type = TREE_VEC_ELT (args, i);
1355 if (TREE_CODE (type) == POINTER_TYPE)
1356 type = TREE_TYPE (type);
1357 if (! TYPE_FOR_JAVA (type))
1366 /* For a METHOD in a Java class CTYPE, return 1 if
1367 the parameter and return types are valid Java types.
1368 Otherwise, print appropriate error messages, and return 0. */
1371 check_java_method (method)
1375 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1376 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1377 if (! acceptable_java_type (ret_type))
1379 cp_error ("Java method '%D' has non-Java return type `%T'",
1383 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1385 tree type = TREE_VALUE (arg_types);
1386 if (! acceptable_java_type (type))
1388 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1393 return jerr ? 0 : 1;
1396 /* Sanity check: report error if this function FUNCTION is not
1397 really a member of the class (CTYPE) it is supposed to belong to.
1398 CNAME is the same here as it is for grokclassfn above. */
1401 check_classfn (ctype, function)
1402 tree ctype, function;
1404 tree fn_name = DECL_NAME (function);
1405 tree fndecl, fndecls;
1406 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1410 if (DECL_USE_TEMPLATE (function)
1411 && !(TREE_CODE (function) == TEMPLATE_DECL
1412 && DECL_TEMPLATE_SPECIALIZATION (function))
1413 && is_member_template (DECL_TI_TEMPLATE (function)))
1414 /* Since this is a specialization of a member template,
1415 we're not going to find the declaration in the class.
1418 struct S { template <typename T> void f(T); };
1419 template <> void S::f(int);
1421 we're not going to find `S::f(int)', but there's no
1422 reason we should, either. We let our callers know we didn't
1423 find the method, but we don't complain. */
1426 if (method_vec != 0)
1428 methods = &TREE_VEC_ELT (method_vec, 0);
1429 end = TREE_VEC_END (method_vec);
1431 /* First suss out ctors and dtors. */
1432 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1433 && DECL_CONSTRUCTOR_P (function))
1435 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1436 && DECL_DESTRUCTOR_P (function))
1439 while (++methods != end && *methods)
1442 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1445 for (fndecls = *methods; fndecls != NULL_TREE;
1446 fndecls = OVL_NEXT (fndecls))
1448 fndecl = OVL_CURRENT (fndecls);
1449 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL, or
1450 for a for member function of a template class, is
1451 not mangled, so the check below does not work
1452 correctly in that case. Since mangled destructor
1453 names do not include the type of the arguments,
1454 we can't use this short-cut for them, either.
1455 (It's not legal to declare arguments for a
1456 destructor, but some people try.) */
1457 if (!DECL_DESTRUCTOR_P (function)
1458 && (DECL_ASSEMBLER_NAME (function)
1459 != DECL_NAME (function))
1460 && (DECL_ASSEMBLER_NAME (fndecl)
1461 != DECL_NAME (fndecl))
1462 && (DECL_ASSEMBLER_NAME (function)
1463 == DECL_ASSEMBLER_NAME (fndecl)))
1466 /* We cannot simply call decls_match because this
1467 doesn't work for static member functions that are
1468 pretending to be methods, and because the name
1469 may have been changed by asm("new_name"). */
1470 if (DECL_NAME (function) == DECL_NAME (fndecl))
1472 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1473 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1475 /* Get rid of the this parameter on functions that become
1477 if (DECL_STATIC_FUNCTION_P (fndecl)
1478 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1479 p1 = TREE_CHAIN (p1);
1481 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1482 TREE_TYPE (TREE_TYPE (fndecl)))
1483 && compparms (p1, p2)
1484 && (DECL_TEMPLATE_SPECIALIZATION (function)
1485 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1486 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1487 || (DECL_TI_TEMPLATE (function)
1488 == DECL_TI_TEMPLATE (fndecl))))
1497 if (methods != end && *methods)
1499 tree fndecl = *methods;
1500 cp_error ("prototype for `%#D' does not match any in class `%T'",
1502 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1503 OVL_CURRENT (fndecl));
1504 while (fndecl = OVL_NEXT (fndecl), fndecl)
1505 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1510 if (!COMPLETE_TYPE_P (ctype))
1511 incomplete_type_error (function, ctype);
1513 cp_error ("no `%#D' member function declared in class `%T'",
1517 /* If we did not find the method in the class, add it to avoid
1518 spurious errors (unless the CTYPE is not yet defined, in which
1519 case we'll only confuse ourselves when the function is declared
1520 properly within the class. */
1521 if (COMPLETE_TYPE_P (ctype))
1522 add_method (ctype, function, /*error_p=*/1);
1526 /* We have just processed the DECL, which is a static data member.
1527 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1528 present, is the assembly-language name for the data member.
1529 FLAGS is as for cp_finish_decl. */
1532 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1538 const char *asmspec = 0;
1541 asmspec = TREE_STRING_POINTER (asmspec_tree);
1543 my_friendly_assert (TREE_PUBLIC (decl), 0);
1545 DECL_CONTEXT (decl) = current_class_type;
1547 /* We cannot call pushdecl here, because that would fill in the
1548 decl of our TREE_CHAIN. Instead, we modify cp_finish_decl to do
1549 the right thing, namely, to put this decl out straight away. */
1550 /* current_class_type can be NULL_TREE in case of error. */
1551 if (!asmspec && current_class_type)
1553 DECL_INITIAL (decl) = error_mark_node;
1555 DECL_ASSEMBLER_NAME (decl) = mangle_decl (decl);
1557 DECL_ASSEMBLER_NAME (decl)
1558 = build_static_name (current_class_type, DECL_NAME (decl));
1560 if (! processing_template_decl)
1562 if (!pending_statics)
1563 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1564 VARRAY_PUSH_TREE (pending_statics, decl);
1567 /* Static consts need not be initialized in the class definition. */
1568 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1570 static int explained = 0;
1572 error ("initializer invalid for static member with constructor");
1575 error ("(an out of class initialization is required)");
1580 /* Force the compiler to know when an uninitialized static const
1581 member is being used. */
1582 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1583 TREE_USED (decl) = 1;
1584 DECL_INITIAL (decl) = init;
1585 DECL_IN_AGGR_P (decl) = 1;
1587 cp_finish_decl (decl, init, asmspec_tree, flags);
1590 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1591 of a structure component, returning a _DECL node.
1592 QUALS is a list of type qualifiers for this decl (such as for declaring
1593 const member functions).
1595 This is done during the parsing of the struct declaration.
1596 The _DECL nodes are chained together and the lot of them
1597 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1599 If class A defines that certain functions in class B are friends, then
1600 the way I have set things up, it is B who is interested in permission
1601 granted by A. However, it is in A's context that these declarations
1602 are parsed. By returning a void_type_node, class A does not attempt
1603 to incorporate the declarations of the friends within its structure.
1605 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1606 CHANGES TO CODE IN `start_method'. */
1609 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1610 tree declarator, declspecs, init, asmspec_tree, attrlist;
1612 register tree value;
1613 const char *asmspec = 0;
1614 int flags = LOOKUP_ONLYCONVERTING;
1616 /* Convert () initializers to = initializers. */
1617 if (init == NULL_TREE && declarator != NULL_TREE
1618 && TREE_CODE (declarator) == CALL_EXPR
1619 && TREE_OPERAND (declarator, 0)
1620 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1621 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1622 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1624 /* It's invalid to try to initialize a data member using a
1625 functional notation, e.g.:
1631 Explain that to the user. */
1632 static int explained;
1634 cp_error ("invalid data member initialization");
1637 cp_error ("(use `=' to initialize static data members)");
1641 declarator = TREE_OPERAND (declarator, 0);
1645 if (declspecs == NULL_TREE
1646 && TREE_CODE (declarator) == SCOPE_REF
1647 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1649 /* Access declaration */
1650 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1652 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1653 pop_nested_class ();
1654 return do_class_using_decl (declarator);
1658 && TREE_CODE (init) == TREE_LIST
1659 && TREE_VALUE (init) == error_mark_node
1660 && TREE_CHAIN (init) == NULL_TREE)
1663 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, attrlist);
1664 if (! value || value == error_mark_node)
1665 /* friend or constructor went bad. */
1667 if (TREE_TYPE (value) == error_mark_node)
1668 return error_mark_node;
1670 /* Pass friendly classes back. */
1671 if (TREE_CODE (value) == VOID_TYPE)
1672 return void_type_node;
1674 if (DECL_NAME (value) != NULL_TREE
1675 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1676 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1677 cp_error ("member `%D' conflicts with virtual function table field name",
1680 /* Stash away type declarations. */
1681 if (TREE_CODE (value) == TYPE_DECL)
1683 DECL_NONLOCAL (value) = 1;
1684 DECL_CONTEXT (value) = current_class_type;
1686 if (CLASS_TYPE_P (TREE_TYPE (value)))
1687 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1689 /* Now that we've updated the context, we need to remangle the
1690 name for this TYPE_DECL. */
1691 DECL_ASSEMBLER_NAME (value) = DECL_NAME (value);
1692 if (!uses_template_parms (value))
1695 DECL_ASSEMBLER_NAME (value) = mangle_type (TREE_TYPE (value));
1697 DECL_ASSEMBLER_NAME (value) =
1698 get_identifier (build_overload_name (TREE_TYPE (value), 1, 1));
1701 if (processing_template_decl)
1702 value = push_template_decl (value);
1707 if (DECL_IN_AGGR_P (value))
1709 cp_error ("`%D' is already defined in `%T'", value,
1710 DECL_CONTEXT (value));
1711 return void_type_node;
1715 asmspec = TREE_STRING_POINTER (asmspec_tree);
1719 if (TREE_CODE (value) == FUNCTION_DECL)
1721 grok_function_init (value, init);
1724 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1725 /* Already complained in grokdeclarator. */
1729 /* We allow initializers to become parameters to base
1731 if (TREE_CODE (init) == TREE_LIST)
1733 if (TREE_CHAIN (init) == NULL_TREE)
1734 init = TREE_VALUE (init);
1736 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1739 if (TREE_CODE (init) == CONST_DECL)
1740 init = DECL_INITIAL (init);
1741 else if (TREE_READONLY_DECL_P (init))
1742 init = decl_constant_value (init);
1743 else if (TREE_CODE (init) == CONSTRUCTOR)
1744 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1745 if (init == error_mark_node)
1746 /* We must make this look different than `error_mark_node'
1747 because `decl_const_value' would mis-interpret it
1748 as only meaning that this VAR_DECL is defined. */
1749 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1750 else if (processing_template_decl)
1752 else if (! TREE_CONSTANT (init))
1754 /* We can allow references to things that are effectively
1755 static, since references are initialized with the address. */
1756 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1757 || (TREE_STATIC (init) == 0
1758 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1760 error ("field initializer is not constant");
1761 init = error_mark_node;
1767 if (processing_template_decl && ! current_function_decl
1768 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1769 value = push_template_decl (value);
1772 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1773 TREE_VALUE (attrlist));
1775 if (TREE_CODE (value) == VAR_DECL)
1777 finish_static_data_member_decl (value, init, asmspec_tree,
1781 if (TREE_CODE (value) == FIELD_DECL)
1785 /* This must override the asm specifier which was placed
1786 by grokclassfn. Lay this out fresh. */
1787 DECL_RTL (value) = NULL_RTX;
1788 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1790 if (DECL_INITIAL (value) == error_mark_node)
1791 init = error_mark_node;
1792 cp_finish_decl (value, init, asmspec_tree, flags);
1793 DECL_INITIAL (value) = init;
1794 DECL_IN_AGGR_P (value) = 1;
1797 if (TREE_CODE (value) == FUNCTION_DECL)
1801 /* This must override the asm specifier which was placed
1802 by grokclassfn. Lay this out fresh. */
1803 DECL_RTL (value) = NULL_RTX;
1804 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1806 cp_finish_decl (value, init, asmspec_tree, flags);
1808 /* Pass friends back this way. */
1809 if (DECL_FRIEND_P (value))
1810 return void_type_node;
1812 DECL_IN_AGGR_P (value) = 1;
1815 my_friendly_abort (21);
1820 /* Like `grokfield', but for bitfields.
1821 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1824 grokbitfield (declarator, declspecs, width)
1825 tree declarator, declspecs, width;
1827 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1830 if (! value) return NULL_TREE; /* friends went bad. */
1832 /* Pass friendly classes back. */
1833 if (TREE_CODE (value) == VOID_TYPE)
1834 return void_type_node;
1836 if (TREE_CODE (value) == TYPE_DECL)
1838 cp_error ("cannot declare `%D' to be a bitfield type", value);
1842 /* Usually, finish_struct_1 catches bitifields with invalid types.
1843 But, in the case of bitfields with function type, we confuse
1844 ourselves into thinking they are member functions, so we must
1846 if (TREE_CODE (value) == FUNCTION_DECL)
1848 cp_error ("cannot declare bitfield `%D' with funcion type",
1853 if (DECL_IN_AGGR_P (value))
1855 cp_error ("`%D' is already defined in the class %T", value,
1856 DECL_CONTEXT (value));
1857 return void_type_node;
1860 GNU_xref_member (current_class_name, value);
1862 if (TREE_STATIC (value))
1864 cp_error ("static member `%D' cannot be a bitfield", value);
1867 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1869 if (width != error_mark_node)
1871 constant_expression_warning (width);
1872 DECL_INITIAL (value) = width;
1873 SET_DECL_C_BIT_FIELD (value);
1876 DECL_IN_AGGR_P (value) = 1;
1881 grokoptypename (declspecs, declarator)
1882 tree declspecs, declarator;
1884 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1886 return mangle_conv_op_name_for_type (t);
1888 return build_typename_overload (t);
1891 /* When a function is declared with an initializer,
1892 do the right thing. Currently, there are two possibilities:
1897 // initialization possibility #1.
1898 virtual void f () = 0;
1920 // initialization possibility #2
1927 copy_assignment_arg_p (parmtype, virtualp)
1929 int virtualp ATTRIBUTE_UNUSED;
1931 if (current_class_type == NULL_TREE)
1934 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1935 parmtype = TREE_TYPE (parmtype);
1937 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1939 /* Non-standard hack to support old Booch components. */
1940 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1949 grok_function_init (decl, init)
1953 /* An initializer for a function tells how this function should
1955 tree type = TREE_TYPE (decl);
1957 if (TREE_CODE (type) == FUNCTION_TYPE)
1958 cp_error ("initializer specified for non-member function `%D'", decl);
1960 /* We'll check for this in finish_struct_1. */
1961 else if (DECL_VINDEX (decl) == NULL_TREE)
1962 cp_error ("initializer specified for non-virtual member function `%D'", decl);
1964 else if (integer_zerop (init))
1967 /* Mark this function as being "defined". */
1968 DECL_INITIAL (decl) = error_mark_node;
1969 /* pure virtual destructors must be defined. */
1970 /* pure virtual needs to be defined (as abort) only when put in
1971 vtbl. For wellformed call, it should be itself. pr4737 */
1972 if (!DECL_DESTRUCTOR_P (decl)))
1974 /* Give this node rtl from `abort'. */
1975 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
1978 DECL_PURE_VIRTUAL_P (decl) = 1;
1979 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
1982 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1984 if (copy_assignment_arg_p (parmtype, 1))
1985 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1989 cp_error ("invalid initializer for virtual method `%D'", decl);
1993 cplus_decl_attributes (decl, attributes, prefix_attributes)
1994 tree decl, attributes, prefix_attributes;
1996 if (decl == NULL_TREE || decl == void_type_node)
1999 if (TREE_CODE (decl) == TEMPLATE_DECL)
2000 decl = DECL_TEMPLATE_RESULT (decl);
2002 decl_attributes (decl, attributes, prefix_attributes);
2004 if (TREE_CODE (decl) == TYPE_DECL)
2005 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
2008 /* CONSTRUCTOR_NAME:
2009 Return the name for the constructor (or destructor) for the
2010 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2011 IDENTIFIER_NODE. When given a template, this routine doesn't
2012 lose the specialization. */
2015 constructor_name_full (thing)
2018 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
2019 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
2020 || TREE_CODE (thing) == TYPENAME_TYPE)
2021 thing = TYPE_NAME (thing);
2022 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
2024 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
2025 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
2027 thing = TYPE_NAME (thing);
2029 if (TREE_CODE (thing) == TYPE_DECL
2030 || (TREE_CODE (thing) == TEMPLATE_DECL
2031 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
2032 thing = DECL_NAME (thing);
2033 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
2037 /* CONSTRUCTOR_NAME:
2038 Return the name for the constructor (or destructor) for the
2039 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2040 IDENTIFIER_NODE. When given a template, return the plain
2041 unspecialized name. */
2044 constructor_name (thing)
2048 thing = constructor_name_full (thing);
2049 t = IDENTIFIER_TEMPLATE (thing);
2055 /* Defer the compilation of the FN until the end of compilation. */
2061 if (DECL_DEFERRED_FN (fn))
2063 DECL_DEFERRED_FN (fn) = 1;
2065 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
2067 VARRAY_PUSH_TREE (deferred_fns, fn);
2070 /* Hand off a unique name which can be used for variable we don't really
2071 want to know about anyway, for example, the anonymous variables which
2072 are needed to make references work. Declare this thing so we can use it.
2073 The variable created will be of type TYPE, and will have internal
2077 get_temp_name (type)
2080 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
2082 int toplev = toplevel_bindings_p ();
2084 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
2085 decl = build_decl (VAR_DECL, get_identifier (buf), type);
2086 DECL_ARTIFICIAL (decl) = 1;
2087 TREE_USED (decl) = 1;
2088 TREE_STATIC (decl) = 1;
2090 decl = pushdecl_top_level (decl);
2092 /* If this is a local variable, then lay out its rtl now.
2093 Otherwise, callers of this function are responsible for dealing
2094 with this variable's rtl. */
2098 my_friendly_assert (DECL_INITIAL (decl) == NULL_TREE,
2105 /* Hunts through the global anonymous union ANON_DECL, building
2106 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2107 returns a VAR_DECL whose size is the same as the size of the
2108 ANON_DECL, if one is available. */
2111 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2117 tree type = TREE_TYPE (anon_decl);
2118 tree main_decl = NULL_TREE;
2121 /* Rather than write the code to handle the non-union case,
2122 just give an error. */
2123 if (TREE_CODE (type) != UNION_TYPE)
2124 error ("anonymous struct not inside named type");
2126 for (field = TYPE_FIELDS (type);
2128 field = TREE_CHAIN (field))
2132 if (DECL_ARTIFICIAL (field))
2134 if (TREE_CODE (field) != FIELD_DECL)
2136 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2141 if (TREE_PRIVATE (field))
2142 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2143 else if (TREE_PROTECTED (field))
2144 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2146 if (DECL_NAME (field) == NULL_TREE
2147 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2149 decl = build_anon_union_vars (field, elems, static_p, external_p);
2153 else if (DECL_NAME (field) == NULL_TREE)
2157 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2158 /* tell `pushdecl' that this is not tentative. */
2159 DECL_INITIAL (decl) = error_mark_node;
2160 TREE_PUBLIC (decl) = 0;
2161 TREE_STATIC (decl) = static_p;
2162 DECL_EXTERNAL (decl) = external_p;
2163 decl = pushdecl (decl);
2164 DECL_INITIAL (decl) = NULL_TREE;
2167 /* Only write out one anon union element--choose the largest
2168 one. We used to try to find one the same size as the union,
2169 but that fails if the ABI forces us to align the union more
2171 if (main_decl == NULL_TREE
2172 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
2175 TREE_ASM_WRITTEN (main_decl) = 1;
2179 /* ??? This causes there to be no debug info written out
2181 TREE_ASM_WRITTEN (decl) = 1;
2183 if (DECL_NAME (field) == NULL_TREE
2184 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2185 /* The remainder of the processing was already done in the
2189 /* If there's a cleanup to do, it belongs in the
2190 TREE_PURPOSE of the following TREE_LIST. */
2191 *elems = tree_cons (NULL_TREE, decl, *elems);
2192 TREE_TYPE (*elems) = type;
2198 /* Finish off the processing of a UNION_TYPE structure.
2199 If there are static members, then all members are
2200 static, and must be laid out together. If the
2201 union is an anonymous union, we arrange for that
2202 as well. PUBLIC_P is nonzero if this union is
2203 not declared static. */
2206 finish_anon_union (anon_union_decl)
2207 tree anon_union_decl;
2209 tree type = TREE_TYPE (anon_union_decl);
2211 int public_p = TREE_PUBLIC (anon_union_decl);
2212 int static_p = TREE_STATIC (anon_union_decl);
2213 int external_p = DECL_EXTERNAL (anon_union_decl);
2215 if (TYPE_FIELDS (type) == NULL_TREE)
2220 error ("namespace-scope anonymous aggregates must be static");
2224 main_decl = build_anon_union_vars (anon_union_decl,
2225 &DECL_ANON_UNION_ELEMS (anon_union_decl),
2226 static_p, external_p);
2228 if (main_decl == NULL_TREE)
2230 warning ("anonymous aggregate with no members");
2236 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2237 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2238 expand_anon_union_decl (anon_union_decl,
2240 DECL_ANON_UNION_ELEMS (anon_union_decl));
2243 add_decl_stmt (anon_union_decl);
2246 /* Finish processing a builtin type TYPE. It's name is NAME,
2247 its fields are in the array FIELDS. LEN is the number of elements
2248 in FIELDS minus one, or put another way, it is the maximum subscript
2251 It is given the same alignment as ALIGN_TYPE. */
2254 finish_builtin_type (type, name, fields, len, align_type)
2263 TYPE_FIELDS (type) = fields[0];
2264 for (i = 0; i < len; i++)
2266 layout_type (TREE_TYPE (fields[i]));
2267 DECL_FIELD_CONTEXT (fields[i]) = type;
2268 TREE_CHAIN (fields[i]) = fields[i+1];
2270 DECL_FIELD_CONTEXT (fields[i]) = type;
2271 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2272 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2274 #if 0 /* not yet, should get fixed properly later */
2275 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2277 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2279 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2280 layout_decl (TYPE_NAME (type), 0);
2283 /* Auxiliary functions to make type signatures for
2284 `operator new' and `operator delete' correspond to
2285 what compiler will be expecting. */
2288 coerce_new_type (type)
2292 tree args = TYPE_ARG_TYPES (type);
2294 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2296 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2297 e = 1, cp_error ("`operator new' must return type `%T'", ptr_type_node);
2299 if (!args || args == void_list_node
2300 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2303 if (args && args != void_list_node)
2304 args = TREE_CHAIN (args);
2305 cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2310 args = tree_cons (NULL_TREE, c_size_type_node, args);
2313 type = build_exception_variant
2314 (build_function_type (ptr_type_node, args),
2315 TYPE_RAISES_EXCEPTIONS (type));
2323 coerce_delete_type (type)
2327 tree args = TYPE_ARG_TYPES (type);
2329 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2331 if (!same_type_p (TREE_TYPE (type), void_type_node))
2332 e = 1, cp_error ("`operator delete' must return type `%T'", void_type_node);
2334 if (!args || args == void_list_node
2335 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2338 if (args && args != void_list_node)
2339 args = TREE_CHAIN (args);
2340 cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2345 args = tree_cons (NULL_TREE, ptr_type_node, args);
2348 type = build_exception_variant
2349 (build_function_type (void_type_node, args),
2350 TYPE_RAISES_EXCEPTIONS (type));
2359 mark_vtable_entries (decl)
2362 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2364 for (; entries; entries = TREE_CHAIN (entries))
2369 fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2370 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2372 if (TREE_CODE (fnaddr) != ADDR_EXPR)
2373 /* This entry is an offset: a virtual base class offset, a
2374 virtual call offset, and RTTI offset, etc. */
2377 fn = TREE_OPERAND (fnaddr, 0);
2378 TREE_ADDRESSABLE (fn) = 1;
2379 /* When we don't have vcall offsets, we output thunks whenever
2380 we output the vtables that contain them. With vcall offsets,
2381 we know all the thunks we'll need when we emit a virtual
2382 function, so we emit the thunks there instead. */
2383 if (DECL_THUNK_P (fn))
2384 use_thunk (fn, THUNK_GENERATE_WITH_VTABLE_P (fn));
2389 /* Set DECL up to have the closest approximation of "initialized common"
2390 linkage available. */
2393 comdat_linkage (decl)
2397 make_decl_one_only (decl);
2398 else if (TREE_CODE (decl) == FUNCTION_DECL || DECL_VIRTUAL_P (decl))
2399 /* We can just emit functions and vtables statically; having
2400 multiple copies is (for the most part) only a waste of space.
2401 There is at least one correctness issue, however: the address
2402 of a template instantiation with external linkage should be the
2403 same, independent of what translation unit asks for the
2404 address, and this will not hold when we emit multiple copies of
2405 the function. However, there's little else we can do. */
2406 TREE_PUBLIC (decl) = 0;
2409 /* Static data member template instantiations, however, cannot
2410 have multiple copies. */
2411 if (DECL_INITIAL (decl) == 0
2412 || DECL_INITIAL (decl) == error_mark_node)
2413 DECL_COMMON (decl) = 1;
2414 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2416 DECL_COMMON (decl) = 1;
2417 DECL_INITIAL (decl) = error_mark_node;
2421 /* We can't do anything useful; leave vars for explicit
2423 DECL_EXTERNAL (decl) = 1;
2424 DECL_NOT_REALLY_EXTERN (decl) = 0;
2428 if (DECL_LANG_SPECIFIC (decl))
2429 DECL_COMDAT (decl) = 1;
2432 /* For win32 we also want to put explicit instantiations in
2433 linkonce sections, so that they will be merged with implicit
2434 instantiations; otherwise we get duplicate symbol errors. */
2437 maybe_make_one_only (decl)
2440 /* We used to say that this was not necessary on targets that support weak
2441 symbols, because the implicit instantiations will defer to the explicit
2442 one. However, that's not actually the case in SVR4; a strong definition
2443 after a weak one is an error. Also, not making explicit
2444 instantiations one_only means that we can end up with two copies of
2445 some template instantiations. */
2446 if (! supports_one_only ())
2449 /* We can't set DECL_COMDAT on functions, or finish_file will think
2450 we can get away with not emitting them if they aren't used. We need
2451 to for variables so that cp_finish_decl will update their linkage,
2452 because their DECL_INITIAL may not have been set properly yet. */
2454 make_decl_one_only (decl);
2456 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2457 DECL_COMDAT (decl) = 1;
2460 /* Returns the virtual function with which the vtable for TYPE is
2461 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2469 if (TYPE_FOR_JAVA (type)
2470 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2471 || CLASSTYPE_INTERFACE_KNOWN (type))
2474 for (method = TYPE_METHODS (type); method != NULL_TREE;
2475 method = TREE_CHAIN (method))
2476 if (DECL_VINDEX (method) != NULL_TREE
2477 && ! DECL_THIS_INLINE (method)
2478 && ! DECL_PURE_VIRTUAL_P (method))
2484 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2485 based on TYPE and other static flags.
2487 Note that anything public is tagged TREE_PUBLIC, whether
2488 it's public in this file or in another one. */
2491 import_export_vtable (decl, type, final)
2495 if (DECL_INTERFACE_KNOWN (decl))
2498 if (TYPE_FOR_JAVA (type))
2500 TREE_PUBLIC (decl) = 1;
2501 DECL_EXTERNAL (decl) = 1;
2502 DECL_INTERFACE_KNOWN (decl) = 1;
2504 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2506 TREE_PUBLIC (decl) = 1;
2507 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2508 DECL_INTERFACE_KNOWN (decl) = 1;
2512 /* We can only wait to decide if we have real non-inline virtual
2513 functions in our class, or if we come from a template. */
2515 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2516 || key_method (type));
2518 if (final || ! found)
2520 comdat_linkage (decl);
2521 DECL_EXTERNAL (decl) = 0;
2525 TREE_PUBLIC (decl) = 1;
2526 DECL_EXTERNAL (decl) = 1;
2531 /* Determine whether or not we want to specifically import or export CTYPE,
2532 using various heuristics. */
2535 import_export_class (ctype)
2538 /* -1 for imported, 1 for exported. */
2539 int import_export = 0;
2541 /* It only makes sense to call this function at EOF. The reason is
2542 that this function looks at whether or not the first non-inline
2543 non-abstract virtual member function has been defined in this
2544 translation unit. But, we can't possibly know that until we've
2545 seen the entire translation unit. */
2546 my_friendly_assert (at_eof, 20000226);
2548 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2551 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2552 we will have CLASSTYPE_INTERFACE_ONLY set but not
2553 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2554 heuristic because someone will supply a #pragma implementation
2555 elsewhere, and deducing it here would produce a conflict. */
2556 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2559 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2560 /* FIXME this should really use some sort of target-independent macro. */
2561 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2563 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2567 /* If we got -fno-implicit-templates, we import template classes that
2568 weren't explicitly instantiated. */
2569 if (import_export == 0
2570 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2571 && ! flag_implicit_templates)
2574 /* Base our import/export status on that of the first non-inline,
2575 non-pure virtual function, if any. */
2576 if (import_export == 0
2577 && TYPE_POLYMORPHIC_P (ctype))
2579 tree method = key_method (ctype);
2581 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2584 #ifdef MULTIPLE_SYMBOL_SPACES
2585 if (import_export == -1)
2591 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2592 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = (import_export > 0);
2593 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2597 /* We need to describe to the assembler the relationship between
2598 a vtable and the vtable of the parent class. */
2601 output_vtable_inherit (vars)
2607 op[0] = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2609 parent = binfo_for_vtable (vars);
2611 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2615 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2616 op[1] = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2619 my_friendly_abort (980826);
2621 output_asm_insn (".vtable_inherit %c0, %c1", op);
2625 finish_vtable_vardecl (t, data)
2627 void *data ATTRIBUTE_UNUSED;
2630 tree ctype = DECL_CONTEXT (vars);
2631 import_export_class (ctype);
2632 import_export_vtable (vars, ctype, 1);
2634 if (! DECL_EXTERNAL (vars)
2635 && DECL_NEEDED_P (vars)
2636 && ! TREE_ASM_WRITTEN (vars))
2638 if (TREE_TYPE (vars) == void_type_node)
2639 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2643 mark_vtable_entries (vars);
2644 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2645 store_init_value (vars, DECL_INITIAL (vars));
2647 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2649 /* Mark the VAR_DECL node representing the vtable itself as a
2650 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2651 It is rather important that such things be ignored because
2652 any effort to actually generate DWARF for them will run
2653 into trouble when/if we encounter code like:
2656 struct S { virtual void member (); };
2658 because the artificial declaration of the vtable itself (as
2659 manufactured by the g++ front end) will say that the vtable
2660 is a static member of `S' but only *after* the debug output
2661 for the definition of `S' has already been output. This causes
2662 grief because the DWARF entry for the definition of the vtable
2663 will try to refer back to an earlier *declaration* of the
2664 vtable as a static member of `S' and there won't be one.
2665 We might be able to arrange to have the "vtable static member"
2666 attached to the member list for `S' before the debug info for
2667 `S' get written (which would solve the problem) but that would
2668 require more intrusive changes to the g++ front end. */
2670 DECL_IGNORED_P (vars) = 1;
2673 /* Always make vtables weak. */
2675 comdat_linkage (vars);
2677 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2680 output_vtable_inherit (vars);
2682 /* Because we're only doing syntax-checking, we'll never end up
2683 actually marking the variable as written. */
2684 if (flag_syntax_only)
2685 TREE_ASM_WRITTEN (vars) = 1;
2687 /* Since we're writing out the vtable here, also write the debug
2689 note_debug_info_needed (ctype);
2694 /* If the references to this class' vtables were optimized away, still
2695 emit the appropriate debugging information. See dfs_debug_mark. */
2696 if (DECL_COMDAT (vars)
2697 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2698 note_debug_info_needed (ctype);
2704 prune_vtable_vardecl (t, data)
2706 void *data ATTRIBUTE_UNUSED;
2708 *t = TREE_CHAIN (*t);
2712 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2713 inline function or template instantiation at end-of-file. */
2716 import_export_decl (decl)
2719 if (DECL_INTERFACE_KNOWN (decl))
2722 if (DECL_TEMPLATE_INSTANTIATION (decl)
2723 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2725 DECL_NOT_REALLY_EXTERN (decl) = 1;
2726 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2727 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2728 && (flag_implicit_templates
2729 || (flag_implicit_inline_templates && DECL_THIS_INLINE (decl))))
2731 if (!TREE_PUBLIC (decl))
2732 /* Templates are allowed to have internal linkage. See
2736 comdat_linkage (decl);
2739 DECL_NOT_REALLY_EXTERN (decl) = 0;
2741 else if (DECL_FUNCTION_MEMBER_P (decl))
2743 tree ctype = DECL_CONTEXT (decl);
2744 import_export_class (ctype);
2745 if (CLASSTYPE_INTERFACE_KNOWN (ctype)
2747 ? (! DECL_THIS_INLINE (decl))
2748 : (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))))
2750 DECL_NOT_REALLY_EXTERN (decl)
2751 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2752 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2753 && !DECL_VINDEX (decl)));
2755 /* Always make artificials weak. */
2756 if (DECL_ARTIFICIAL (decl) && flag_weak)
2757 comdat_linkage (decl);
2759 maybe_make_one_only (decl);
2762 comdat_linkage (decl);
2764 else if (DECL_TINFO_FN_P (decl))
2766 tree ctype = TREE_TYPE (DECL_NAME (decl));
2768 if (IS_AGGR_TYPE (ctype))
2769 import_export_class (ctype);
2771 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2772 && TYPE_POLYMORPHIC_P (ctype)
2773 /* If -fno-rtti, we're not necessarily emitting this stuff with
2774 the class, so go ahead and emit it now. This can happen
2775 when a class is used in exception handling. */
2777 /* If the type is a cv-qualified variant of a type, then we
2778 must emit the tinfo function in this translation unit
2779 since it will not be emitted when the vtable for the type
2780 is output (which is when the unqualified version is
2782 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2784 DECL_NOT_REALLY_EXTERN (decl)
2785 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2786 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2787 && !DECL_VINDEX (decl)));
2789 /* Always make artificials weak. */
2791 comdat_linkage (decl);
2793 else if (TYPE_BUILT_IN (ctype)
2794 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2795 DECL_NOT_REALLY_EXTERN (decl) = 0;
2797 comdat_linkage (decl);
2800 comdat_linkage (decl);
2802 DECL_INTERFACE_KNOWN (decl) = 1;
2806 build_cleanup (decl)
2810 tree type = TREE_TYPE (decl);
2812 if (TREE_CODE (type) == ARRAY_TYPE)
2816 mark_addressable (decl);
2817 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2819 temp = build_delete (TREE_TYPE (temp), temp,
2820 sfk_complete_destructor,
2821 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2825 /* Returns the initialization guard variable for the variable DECL,
2826 which has static storage duration. */
2835 /* For a local variable, under the old ABI, we do not try to get a
2836 unique mangled name for the DECL. */
2837 if (!flag_new_abi && DECL_FUNCTION_SCOPE_P (decl))
2839 guard = get_temp_name (integer_type_node);
2840 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2845 /* For struct X foo __attribute__((weak)), there is a counter
2846 __snfoo. Since base is already an assembler name, sname should
2847 be globally unique */
2848 sname = get_id_2 ("__sn", DECL_ASSEMBLER_NAME (decl));
2850 sname = mangle_guard_variable (decl);
2852 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2857 /* Under the new ABI, we use a type that is big enough to
2858 contain a mutex as well as an integer counter. */
2860 guard_type = long_long_integer_type_node;
2862 guard_type = integer_type_node;
2864 guard = build_decl (VAR_DECL, sname, guard_type);
2865 TREE_PUBLIC (guard) = 1;
2866 DECL_ARTIFICIAL (guard) = 1;
2867 TREE_STATIC (guard) = 1;
2868 TREE_USED (guard) = 1;
2869 DECL_COMMON (guard) = 1;
2870 pushdecl_top_level (guard);
2871 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2876 /* Return those bits of the GUARD variable that should be set when the
2877 guarded entity is actually initialized. */
2880 get_guard_bits (guard)
2886 /* Under the new ABI, we only set the first byte of the guard,
2887 in order to leave room for a mutex in the high-order bits. */
2888 guard = build1 (ADDR_EXPR,
2889 build_pointer_type (TREE_TYPE (guard)),
2891 guard = build1 (NOP_EXPR,
2892 build_pointer_type (char_type_node),
2894 guard = build1 (INDIRECT_REF, char_type_node, guard);
2899 /* Return an expression which determines whether or not the GUARD
2900 variable has already been initialized. */
2903 get_guard_cond (guard)
2908 /* Check to see if the GUARD is zero. */
2909 guard = get_guard_bits (guard);
2910 guard_value = integer_zero_node;
2911 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2912 guard_value = convert (TREE_TYPE (guard), guard_value);
2913 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2916 /* Return an expression which sets the GUARD variable, indicating that
2917 the variable being guarded has been initialized. */
2925 /* Set the GUARD to one. */
2926 guard = get_guard_bits (guard);
2927 guard_init = integer_one_node;
2928 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2929 guard_init = convert (TREE_TYPE (guard), guard_init);
2930 return build_modify_expr (guard, NOP_EXPR, guard_init);
2933 /* Start the process of running a particular set of global constructors
2934 or destructors. Subroutine of do_[cd]tors. */
2937 start_objects (method_type, initp)
2938 int method_type, initp;
2944 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2946 if (initp != DEFAULT_INIT_PRIORITY)
2956 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2959 sprintf (type, "%c", method_type);
2961 fnname = get_file_function_name_long (type);
2963 start_function (void_list_node,
2964 make_call_declarator (fnname, void_list_node, NULL_TREE,
2966 NULL_TREE, SF_DEFAULT);
2968 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2969 /* It can be a static function as long as collect2 does not have
2970 to scan the object file to find its ctor/dtor routine. */
2971 TREE_PUBLIC (current_function_decl) = 0;
2974 /* Mark this declaration as used to avoid spurious warnings. */
2975 TREE_USED (current_function_decl) = 1;
2977 /* Mark this function as a global constructor or destructor. */
2978 if (method_type == 'I')
2979 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2981 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2982 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2984 body = begin_compound_stmt (/*has_no_scope=*/0);
2986 /* We cannot allow these functions to be elided, even if they do not
2987 have external linkage. And, there's no point in deferring
2988 copmilation of thes functions; they're all going to have to be
2990 current_function_cannot_inline
2991 = "static constructors and destructors cannot be inlined";
2996 /* Finish the process of running a particular set of global constructors
2997 or destructors. Subroutine of do_[cd]tors. */
3000 finish_objects (method_type, initp, body)
3001 int method_type, initp;
3008 finish_compound_stmt (/*has_no_scope=*/0, body);
3009 fn = finish_function (0);
3012 /* When only doing semantic analysis, and no RTL generation, we
3013 can't call functions that directly emit assembly code; there is
3014 no assembly file in which to put the code. */
3015 if (flag_syntax_only)
3018 fnname = XSTR (XEXP (DECL_RTL (fn), 0), 0);
3019 if (initp == DEFAULT_INIT_PRIORITY)
3021 if (method_type == 'I')
3022 assemble_constructor (fnname);
3024 assemble_destructor (fnname);
3026 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
3027 /* If we're using init priority we can't use assemble_*tor, but on ELF
3028 targets we can stick the references into named sections for GNU ld
3033 sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
3034 /* invert the numbering so the linker puts us in the proper
3035 order; constructors are run from right to left, and the
3036 linker sorts in increasing order. */
3037 MAX_INIT_PRIORITY - initp);
3038 named_section (NULL_TREE, buf, 0);
3039 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
3040 POINTER_SIZE / BITS_PER_UNIT, 1);
3045 /* The names of the parameters to the function created to handle
3046 initializations and destructions for objects with static storage
3048 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3049 #define PRIORITY_IDENTIFIER "__priority"
3051 /* The name of the function we create to handle initializations and
3052 destructions for objects with static storage duration. */
3053 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3055 /* The declaration for the __INITIALIZE_P argument. */
3056 static tree initialize_p_decl;
3058 /* The declaration for the __PRIORITY argument. */
3059 static tree priority_decl;
3061 /* The declaration for the static storage duration function. */
3062 static tree ssdf_decl;
3064 /* All the static storage duration functions created in this
3065 translation unit. */
3066 static varray_type ssdf_decls;
3068 /* A map from priority levels to information about that priority
3069 level. There may be many such levels, so efficient lookup is
3071 static splay_tree priority_info_map;
3073 /* Begins the generation of the function that will handle all
3074 initialization and destruction of objects with static storage
3075 duration. The function generated takes two parameters of type
3076 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3077 non-zero, it performs initializations. Otherwise, it performs
3078 destructions. It only performs those initializations or
3079 destructions with the indicated __PRIORITY. The generated function
3082 It is assumed that this function will only be called once per
3083 translation unit. */
3086 start_static_storage_duration_function ()
3088 static unsigned ssdf_number;
3093 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3095 /* Create the identifier for this function. It will be of the form
3096 SSDF_IDENTIFIER_<number>. */
3097 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
3098 if (ssdf_number == 0)
3100 /* Overflow occurred. That means there are at least 4 billion
3101 initialization functions. */
3102 sorry ("too many initialization functions required");
3103 my_friendly_abort (19990430);
3106 /* Create the parameters. */
3107 parm_types = void_list_node;
3108 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3109 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3110 type = build_function_type (void_type_node, parm_types);
3112 /* Create the FUNCTION_DECL itself. */
3113 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3114 get_identifier (id),
3116 TREE_PUBLIC (ssdf_decl) = 0;
3117 DECL_ARTIFICIAL (ssdf_decl) = 1;
3119 /* Put this function in the list of functions to be called from the
3120 static constructors and destructors. */
3123 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
3125 /* Take this opportunity to initialize the map from priority
3126 numbers to information about that priority level. */
3127 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3128 /*delete_key_fn=*/0,
3129 /*delete_value_fn=*/
3130 (splay_tree_delete_value_fn) &free);
3132 /* We always need to generate functions for the
3133 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3134 priorities later, we'll be sure to find the
3135 DEFAULT_INIT_PRIORITY. */
3136 get_priority_info (DEFAULT_INIT_PRIORITY);
3139 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
3141 /* Create the argument list. */
3142 initialize_p_decl = build_decl (PARM_DECL,
3143 get_identifier (INITIALIZE_P_IDENTIFIER),
3145 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3146 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
3147 TREE_USED (initialize_p_decl) = 1;
3148 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
3150 DECL_CONTEXT (priority_decl) = ssdf_decl;
3151 DECL_ARG_TYPE (priority_decl) = integer_type_node;
3152 TREE_USED (priority_decl) = 1;
3154 TREE_CHAIN (initialize_p_decl) = priority_decl;
3155 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3157 /* Put the function in the global scope. */
3158 pushdecl (ssdf_decl);
3160 /* Start the function itself. This is equivalent to declarating the
3163 static void __ssdf (int __initialize_p, init __priority_p);
3165 It is static because we only need to call this function from the
3166 various constructor and destructor functions for this module. */
3167 start_function (/*specs=*/NULL_TREE,
3169 /*attrs=*/NULL_TREE,
3172 /* Set up the scope of the outermost block in the function. */
3173 body = begin_compound_stmt (/*has_no_scope=*/0);
3175 /* This function must not be deferred because we are depending on
3176 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3177 current_function_cannot_inline
3178 = "static storage duration functions cannot be inlined";
3183 /* Finish the generation of the function which performs initialization
3184 and destruction of objects with static storage duration. After
3185 this point, no more such objects can be created. */
3188 finish_static_storage_duration_function (body)
3191 /* Close out the function. */
3192 finish_compound_stmt (/*has_no_scope=*/0, body);
3193 expand_body (finish_function (0));
3196 /* Return the information about the indicated PRIORITY level. If no
3197 code to handle this level has yet been generated, generate the
3198 appropriate prologue. */
3200 static priority_info
3201 get_priority_info (priority)
3207 n = splay_tree_lookup (priority_info_map,
3208 (splay_tree_key) priority);
3211 /* Create a new priority information structure, and insert it
3213 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
3214 pi->initializations_p = 0;
3215 pi->destructions_p = 0;
3216 splay_tree_insert (priority_info_map,
3217 (splay_tree_key) priority,
3218 (splay_tree_value) pi);
3221 pi = (priority_info) n->value;
3226 /* Set up to handle the initialization or destruction of DECL. If
3227 INITP is non-zero, we are initializing the variable. Otherwise, we
3228 are destroying it. */
3231 start_static_initialization_or_destruction (decl, initp)
3235 tree guard_if_stmt = NULL_TREE;
3242 /* Figure out the priority for this declaration. */
3243 priority = DECL_INIT_PRIORITY (decl);
3245 priority = DEFAULT_INIT_PRIORITY;
3247 /* Remember that we had an initialization or finalization at this
3249 pi = get_priority_info (priority);
3251 pi->initializations_p = 1;
3253 pi->destructions_p = 1;
3255 /* Trick the compiler into thinking we are at the file and line
3256 where DECL was declared so that error-messages make sense, and so
3257 that the debugger will show somewhat sensible file and line
3259 input_filename = DECL_SOURCE_FILE (decl);
3260 lineno = DECL_SOURCE_LINE (decl);
3266 Access control for implicit calls to the constructors,
3267 the conversion functions, or the destructor called to
3268 create and destroy a static data member is performed as
3269 if these calls appeared in the scope of the member's
3272 we pretend we are in a static member function of the class of
3273 which the DECL is a member. */
3274 if (member_p (decl))
3276 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3277 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3280 /* Conditionalize this initialization on being in the right priority
3281 and being initializing/finalizing appropriately. */
3282 guard_if_stmt = begin_if_stmt ();
3283 cond = cp_build_binary_op (EQ_EXPR,
3285 build_int_2 (priority, 0));
3286 init_cond = initp ? integer_one_node : integer_zero_node;
3287 init_cond = cp_build_binary_op (EQ_EXPR,
3290 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3292 /* Assume we don't need a guard. */
3294 /* We need a guard if this is an object with external linkage that
3295 might be initialized in more than one place. (For example, a
3296 static data member of a template, when the data member requires
3298 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3299 || DECL_ONE_ONLY (decl)
3300 || DECL_WEAK (decl)))
3304 guard = get_guard (decl);
3306 /* When using __cxa_atexit, we just check the GUARD as we would
3307 for a local static. */
3308 if (flag_use_cxa_atexit)
3310 /* When using __cxa_atexit, we never try to destroy
3311 anything from a static destructor. */
3312 my_friendly_assert (initp, 20000629);
3313 guard_cond = get_guard_cond (guard);
3315 /* Under the old ABI, e do initializations only if the GUARD is
3316 zero, i.e., if we are the first to initialize the variable.
3317 We do destructions only if the GUARD is one, i.e., if we are
3318 the last to destroy the variable. */
3321 = cp_build_binary_op (EQ_EXPR,
3322 build_unary_op (PREINCREMENT_EXPR,
3328 = cp_build_binary_op (EQ_EXPR,
3329 build_unary_op (PREDECREMENT_EXPR,
3334 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3337 finish_if_stmt_cond (cond, guard_if_stmt);
3339 /* Under the new ABI, we have not already set the GUARD, so we must
3341 if (guard && initp && flag_new_abi)
3342 finish_expr_stmt (set_guard (guard));
3344 return guard_if_stmt;
3347 /* We've just finished generating code to do an initialization or
3348 finalization. GUARD_IF_STMT is the if-statement we used to guard
3349 the initialization. */
3352 finish_static_initialization_or_destruction (guard_if_stmt)
3355 finish_then_clause (guard_if_stmt);
3358 /* Now that we're done with DECL we don't need to pretend to be a
3359 member of its class any longer. */
3360 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3361 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3364 /* Generate code to do the static initialization of DECL. The
3365 initialization is INIT. If DECL may be initialized more than once
3366 in different object files, GUARD is the guard variable to
3367 check. PRIORITY is the priority for the initialization. */
3370 do_static_initialization (decl, init)
3377 /* Set up for the initialization. */
3379 = start_static_initialization_or_destruction (decl,
3382 /* Do the initialization itself. */
3383 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3384 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3385 expr = build_aggr_init (decl, init, 0);
3386 else if (TREE_CODE (init) == TREE_VEC)
3387 expr = build_vec_init (decl, TREE_VEC_ELT (init, 0),
3388 TREE_VEC_ELT (init, 1),
3389 TREE_VEC_ELT (init, 2), 0);
3392 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3393 TREE_SIDE_EFFECTS (expr) = 1;
3395 finish_expr_stmt (expr);
3397 /* If we're using __cxa_atexit, register a a function that calls the
3398 destructor for the object. */
3399 if (flag_use_cxa_atexit)
3400 register_dtor_fn (decl);
3403 finish_static_initialization_or_destruction (guard_if_stmt);
3406 /* Generate code to do the static destruction of DECL. If DECL may be
3407 initialized more than once in different object files, GUARD is the
3408 guard variable to check. PRIORITY is the priority for the
3412 do_static_destruction (decl)
3417 /* If we're using __cxa_atexit, then destructors are registered
3418 immediately after objects are initialized. */
3419 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3421 /* If we don't need a destructor, there's nothing to do. */
3422 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3425 /* Actually do the destruction. */
3426 guard_if_stmt = start_static_initialization_or_destruction (decl,
3428 finish_expr_stmt (build_cleanup (decl));
3429 finish_static_initialization_or_destruction (guard_if_stmt);
3432 /* VARS is a list of variables with static storage duration which may
3433 need initialization and/or finalization. Remove those variables
3434 that don't really need to be initialized or finalized, and return
3435 the resulting list. The order in which the variables appear in
3436 VARS is in reverse order of the order in which they should actually
3437 be initialized. The list we return is in the unreversed order;
3438 i.e., the first variable should be initialized first. */
3441 prune_vars_needing_no_initialization (vars)
3447 for (var = vars, result = NULL_TREE;
3449 var = TREE_CHAIN (var))
3451 tree decl = TREE_VALUE (var);
3452 tree init = TREE_PURPOSE (var);
3454 /* Deal gracefully with error. */
3455 if (decl == error_mark_node)
3458 /* The only things that can be initialized are variables. */
3459 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3461 /* If this object is not defined, we don't need to do anything
3463 if (DECL_EXTERNAL (decl))
3466 /* Also, if the initializer already contains errors, we can bail
3468 if (init && TREE_CODE (init) == TREE_LIST
3469 && value_member (error_mark_node, init))
3472 /* This variable is going to need initialization and/or
3473 finalization, so we add it to the list. */
3474 result = tree_cons (init, decl, result);
3480 /* Make sure we have told the back end about all the variables in
3484 write_out_vars (vars)
3489 for (v = vars; v; v = TREE_CHAIN (v))
3490 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3491 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3494 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3495 (otherwise) that will initialize all gobal objects with static
3496 storage duration having the indicated PRIORITY. */
3499 generate_ctor_or_dtor_function (constructor_p, priority)
3508 /* We use `I' to indicate initialization and `D' to indicate
3515 /* Begin the function. */
3516 body = start_objects (function_key, priority);
3518 /* Call the static storage duration function with appropriate
3520 for (i = 0; i < ssdf_decls->elements_used; ++i)
3522 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3524 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3526 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3530 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3531 calls to any functions marked with attributes indicating that
3532 they should be called at initialization- or destruction-time. */
3533 if (priority == DEFAULT_INIT_PRIORITY)
3537 for (fns = constructor_p ? static_ctors : static_dtors;
3539 fns = TREE_CHAIN (fns))
3540 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3543 /* Close out the function. */
3544 finish_objects (function_key, priority, body);
3547 /* Generate constructor and destructor functions for the priority
3551 generate_ctor_and_dtor_functions_for_priority (n, data)
3553 void *data ATTRIBUTE_UNUSED;
3555 int priority = (int) n->key;
3556 priority_info pi = (priority_info) n->value;
3558 /* Generate the functions themselves, but only if they are really
3560 if (pi->initializations_p
3561 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3562 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3564 if (pi->destructions_p
3565 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3566 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3569 /* Keep iterating. */
3573 /* This routine is called from the last rule in yyparse ().
3574 Its job is to create all the code needed to initialize and
3575 destroy the global aggregates. We do the destruction
3576 first, since that way we only need to reverse the decls once. */
3587 /* Bad parse errors. Just forget about it. */
3588 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3591 /* Otherwise, GDB can get confused, because in only knows
3592 about source for LINENO-1 lines. */
3595 interface_unknown = 1;
3598 /* We now have to write out all the stuff we put off writing out.
3601 o Template specializations that we have not yet instantiated,
3602 but which are needed.
3603 o Initialization and destruction for non-local objects with
3604 static storage duration. (Local objects with static storage
3605 duration are initialized when their scope is first entered,
3606 and are cleaned up via atexit.)
3607 o Virtual function tables.
3609 All of these may cause others to be needed. For example,
3610 instantiating one function may cause another to be needed, and
3611 generating the intiailzer for an object may cause templates to be
3612 instantiated, etc., etc. */
3614 timevar_push (TV_VARCONST);
3616 if (new_abi_rtti_p ())
3617 emit_support_tinfos ();
3623 /* If there are templates that we've put off instantiating, do
3625 instantiate_pending_templates ();
3627 /* Write out virtual tables as required. Note that writing out
3628 the virtual table for a template class may cause the
3629 instantiation of members of that class. */
3630 if (walk_globals (vtable_decl_p,
3631 finish_vtable_vardecl,
3635 /* Write out needed type info variables. Writing out one variable
3636 might cause others to be needed. */
3637 if (new_abi_rtti_p ()
3638 && walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3641 /* The list of objects with static storage duration is built up
3642 in reverse order. We clear STATIC_AGGREGATES so that any new
3643 aggregates added during the initialization of these will be
3644 initialized in the correct order when we next come around the
3646 vars = prune_vars_needing_no_initialization (static_aggregates);
3647 static_aggregates = NULL_TREE;
3653 /* We need to start a new initialization function each time
3654 through the loop. That's because we need to know which
3655 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3656 isn't computed until a function is finished, and written
3657 out. That's a deficiency in the back-end. When this is
3658 fixed, these initialization functions could all become
3659 inline, with resulting performance improvements. */
3660 tree ssdf_body = start_static_storage_duration_function ();
3662 /* Make sure the back end knows about all the variables. */
3663 write_out_vars (vars);
3665 /* First generate code to do all the initializations. */
3666 for (v = vars; v; v = TREE_CHAIN (v))
3667 do_static_initialization (TREE_VALUE (v),
3670 /* Then, generate code to do all the destructions. Do these
3671 in reverse order so that the most recently constructed
3672 variable is the first destroyed. If we're using
3673 __cxa_atexit, then we don't need to do this; functions
3674 were registered at initialization time to destroy the
3676 if (!flag_use_cxa_atexit)
3678 vars = nreverse (vars);
3679 for (v = vars; v; v = TREE_CHAIN (v))
3680 do_static_destruction (TREE_VALUE (v));
3685 /* Finish up the static storage duration function for this
3687 finish_static_storage_duration_function (ssdf_body);
3689 /* All those initializations and finalizations might cause
3690 us to need more inline functions, more template
3691 instantiations, etc. */
3695 /* Go through the various inline functions, and see if any need
3697 for (i = 0; i < deferred_fns_used; ++i)
3699 tree decl = VARRAY_TREE (deferred_fns, i);
3700 import_export_decl (decl);
3701 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3703 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3705 /* Even though we're already at the top-level, we push
3706 there again. That way, when we pop back a few lines
3707 hence, all of our state is restored. Otherwise,
3708 finish_function doesn't clean things up, and we end
3709 up with CURRENT_FUNCTION_DECL set. */
3710 push_to_top_level ();
3711 if (DECL_TINFO_FN_P (decl))
3712 synthesize_tinfo_fn (decl);
3714 synthesize_method (decl);
3715 pop_from_top_level ();
3720 /* Mark all functions that might deal with exception-handling as
3722 mark_all_runtime_matches ();
3724 /* We lie to the back-end, pretending that some functions are
3725 not defined when they really are. This keeps these functions
3726 from being put out unnecessarily. But, we must stop lying
3727 when the functions are referenced, or if they are not comdat
3728 since they need to be put out now. */
3729 for (i = 0; i < deferred_fns_used; ++i)
3731 tree decl = VARRAY_TREE (deferred_fns, i);
3733 if (DECL_NOT_REALLY_EXTERN (decl)
3734 && DECL_INITIAL (decl)
3735 && DECL_NEEDED_P (decl))
3736 DECL_EXTERNAL (decl) = 0;
3738 /* If we're going to need to write this function out, and
3739 there's already a body for it, create RTL for it now.
3740 (There might be no body if this is a method we haven't
3741 gotten around to synthesizing yet.) */
3742 if (!DECL_EXTERNAL (decl)
3743 && DECL_NEEDED_P (decl)
3744 && DECL_SAVED_TREE (decl)
3745 && !DECL_SAVED_INSNS (decl)
3746 && !TREE_ASM_WRITTEN (decl))
3748 int saved_not_really_extern;
3750 /* When we call finish_function in expand_body, it will
3751 try to reset DECL_NOT_REALLY_EXTERN so we save and
3753 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3754 /* Generate RTL for this function now that we know we
3757 /* Undo the damage done by finish_function. */
3758 DECL_EXTERNAL (decl) = 0;
3759 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3760 /* If we're compiling -fsyntax-only pretend that this
3761 function has been written out so that we don't try to
3763 if (flag_syntax_only)
3764 TREE_ASM_WRITTEN (decl) = 1;
3769 if (deferred_fns_used
3770 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3773 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3776 /* Static data members are just like namespace-scope globals. */
3777 for (i = 0; i < pending_statics_used; ++i)
3779 tree decl = VARRAY_TREE (pending_statics, i);
3780 if (TREE_ASM_WRITTEN (decl))
3782 import_export_decl (decl);
3783 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3784 DECL_EXTERNAL (decl) = 0;
3787 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3788 pending_statics_used))
3793 /* We give C linkage to static constructors and destructors. */
3794 push_lang_context (lang_name_c);
3796 /* Generate initialization and destruction functions for all
3797 priorities for which they are required. */
3798 if (priority_info_map)
3799 splay_tree_foreach (priority_info_map,
3800 generate_ctor_and_dtor_functions_for_priority,
3803 /* We're done with the splay-tree now. */
3804 if (priority_info_map)
3805 splay_tree_delete (priority_info_map);
3807 /* We're done with static constructors, so we can go back to "C++"
3809 pop_lang_context ();
3811 /* Now delete from the chain of variables all virtual function tables.
3812 We output them all ourselves, because each will be treated
3813 specially. We don't do this if we're just doing semantic
3814 analysis, and not code-generation. */
3815 if (!flag_syntax_only)
3816 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3818 /* Now, issue warnings about static, but not defined, functions,
3819 etc., and emit debugging information. */
3820 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3821 if (pending_statics)
3822 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3823 pending_statics_used);
3827 /* The entire file is now complete. If requested, dump everything
3829 if (flag_dump_translation_unit)
3830 dump_node_to_file (global_namespace, flag_dump_translation_unit);
3832 /* If there's some tool that wants to examine the entire translation
3833 unit, let it do so now. */
3835 (*back_end_hook) (global_namespace);
3837 timevar_pop (TV_VARCONST);
3839 if (flag_detailed_statistics)
3841 dump_tree_statistics ();
3842 dump_time_statistics ();
3846 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3847 expr. Since it was parsed like a type, we need to wade through and fix
3848 that. Unfortunately, since operator() is left-associative, we can't use
3849 tail recursion. In the above example, TYPE is `A', and DECL is
3852 Maybe this shouldn't be recursive, but how often will it actually be
3856 reparse_absdcl_as_expr (type, decl)
3859 /* do build_functional_cast (type, NULL_TREE) at bottom */
3860 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3861 return build_functional_cast (type, NULL_TREE);
3864 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3866 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3868 if (TREE_CODE (decl) == CALL_EXPR
3869 && (! TREE_TYPE (decl)
3870 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3871 decl = require_complete_type (decl);
3876 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3877 out to be an expr. Since it was parsed like a type, we need to wade
3878 through and fix that. Since casts are right-associative, we are
3879 reversing the order, so we don't have to recurse.
3881 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3885 reparse_absdcl_as_casts (decl, expr)
3890 if (TREE_CODE (expr) == CONSTRUCTOR
3891 && TREE_TYPE (expr) == 0)
3893 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3894 decl = TREE_OPERAND (decl, 0);
3896 expr = digest_init (type, expr, (tree *) 0);
3897 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3899 int failure = complete_array_type (type, expr, 1);
3901 my_friendly_abort (78);
3907 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3908 decl = TREE_OPERAND (decl, 0);
3909 expr = build_c_cast (type, expr);
3912 if (warn_old_style_cast && ! in_system_header
3913 && current_lang_name != lang_name_c)
3914 warning ("use of old-style cast");
3919 /* Given plain tree nodes for an expression, build up the full semantics. */
3922 build_expr_from_tree (t)
3925 if (t == NULL_TREE || t == error_mark_node)
3928 switch (TREE_CODE (t))
3930 case IDENTIFIER_NODE:
3931 return do_identifier (t, 0, NULL_TREE);
3934 if (LOOKUP_EXPR_GLOBAL (t))
3935 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3937 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3939 case TEMPLATE_ID_EXPR:
3940 return (lookup_template_function
3941 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3942 build_expr_from_tree (TREE_OPERAND (t, 1))));
3945 return build_x_indirect_ref
3946 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3949 return build_functional_cast
3950 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3952 case REINTERPRET_CAST_EXPR:
3953 return build_reinterpret_cast
3954 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3956 case CONST_CAST_EXPR:
3957 return build_const_cast
3958 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3960 case DYNAMIC_CAST_EXPR:
3961 return build_dynamic_cast
3962 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3964 case STATIC_CAST_EXPR:
3965 return build_static_cast
3966 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3968 case PREDECREMENT_EXPR:
3969 case PREINCREMENT_EXPR:
3970 case POSTDECREMENT_EXPR:
3971 case POSTINCREMENT_EXPR:
3975 case TRUTH_NOT_EXPR:
3977 case CONVERT_EXPR: /* Unary + */
3982 return build_x_unary_op (TREE_CODE (t),
3983 build_expr_from_tree (TREE_OPERAND (t, 0)));
3988 case TRUNC_DIV_EXPR:
3990 case FLOOR_DIV_EXPR:
3991 case ROUND_DIV_EXPR:
3992 case EXACT_DIV_EXPR:
3994 case BIT_ANDTC_EXPR:
3997 case TRUNC_MOD_EXPR:
3998 case FLOOR_MOD_EXPR:
3999 case TRUTH_ANDIF_EXPR:
4000 case TRUTH_ORIF_EXPR:
4001 case TRUTH_AND_EXPR:
4016 return build_x_binary_op
4018 build_expr_from_tree (TREE_OPERAND (t, 0)),
4019 build_expr_from_tree (TREE_OPERAND (t, 1)));
4022 return build_m_component_ref
4023 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4024 build_expr_from_tree (TREE_OPERAND (t, 1)));
4027 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
4030 if (TREE_OPERAND (t, 0) == NULL_TREE)
4032 return build_parse_node (ARRAY_REF, NULL_TREE,
4033 build_expr_from_tree (TREE_OPERAND (t, 1)));
4034 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
4035 build_expr_from_tree (TREE_OPERAND (t, 1)));
4040 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
4043 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
4047 return build_x_modify_expr
4048 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4049 TREE_CODE (TREE_OPERAND (t, 1)),
4050 build_expr_from_tree (TREE_OPERAND (t, 2)));
4053 return build_x_arrow
4054 (build_expr_from_tree (TREE_OPERAND (t, 0)));
4058 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4059 build_expr_from_tree (TREE_OPERAND (t, 1)),
4060 build_expr_from_tree (TREE_OPERAND (t, 2)),
4061 NEW_EXPR_USE_GLOBAL (t));
4064 return delete_sanity
4065 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4066 build_expr_from_tree (TREE_OPERAND (t, 1)),
4067 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
4070 if (TREE_OPERAND (t, 1) == NULL_TREE)
4071 return build_x_compound_expr
4072 (build_expr_from_tree (TREE_OPERAND (t, 0)));
4074 my_friendly_abort (42);
4076 case METHOD_CALL_EXPR:
4077 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4079 tree ref = TREE_OPERAND (t, 0);
4080 tree name = TREE_OPERAND (ref, 1);
4082 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4083 name = build_nt (TEMPLATE_ID_EXPR,
4084 TREE_OPERAND (name, 0),
4085 build_expr_from_tree (TREE_OPERAND (name, 1)));
4087 return build_scoped_method_call
4088 (build_expr_from_tree (TREE_OPERAND (t, 1)),
4089 build_expr_from_tree (TREE_OPERAND (ref, 0)),
4091 build_expr_from_tree (TREE_OPERAND (t, 2)));
4095 tree fn = TREE_OPERAND (t, 0);
4097 /* We can get a TEMPLATE_ID_EXPR here on code like:
4101 so we must resolve that. However, we can also get things
4102 like a BIT_NOT_EXPR here, when referring to a destructor,
4103 and things like that are not correctly resolved by
4104 build_expr_from_tree. So, just use build_expr_from_tree
4105 when we really need it. */
4106 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4107 fn = lookup_template_function
4108 (TREE_OPERAND (fn, 0),
4109 build_expr_from_tree (TREE_OPERAND (fn, 1)));
4111 return build_method_call
4112 (build_expr_from_tree (TREE_OPERAND (t, 1)),
4114 build_expr_from_tree (TREE_OPERAND (t, 2)),
4115 NULL_TREE, LOOKUP_NORMAL);
4119 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4121 tree ref = TREE_OPERAND (t, 0);
4122 tree name = TREE_OPERAND (ref, 1);
4124 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4125 name = build_nt (TEMPLATE_ID_EXPR,
4126 TREE_OPERAND (name, 0),
4127 build_expr_from_tree (TREE_OPERAND (name, 1)));
4129 return build_member_call
4130 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
4132 build_expr_from_tree (TREE_OPERAND (t, 1)));
4136 tree name = TREE_OPERAND (t, 0);
4138 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
4139 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
4140 && !LOOKUP_EXPR_GLOBAL (name)
4141 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
4142 && (!current_class_type
4143 || !lookup_member (current_class_type, id, 0, 0)))
4145 /* Do Koenig lookup if there are no class members. */
4146 name = do_identifier (id, 0, args);
4148 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
4149 || ! really_overloaded_fn (name))
4150 name = build_expr_from_tree (name);
4151 return build_x_function_call (name, args, current_class_ref);
4155 return build_x_conditional_expr
4156 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4157 build_expr_from_tree (TREE_OPERAND (t, 1)),
4158 build_expr_from_tree (TREE_OPERAND (t, 2)));
4160 case PSEUDO_DTOR_EXPR:
4161 return (finish_pseudo_destructor_call_expr
4162 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4163 build_expr_from_tree (TREE_OPERAND (t, 1)),
4164 build_expr_from_tree (TREE_OPERAND (t, 2))));
4168 tree purpose, value, chain;
4170 if (t == void_list_node)
4173 purpose = TREE_PURPOSE (t);
4175 purpose = build_expr_from_tree (purpose);
4176 value = TREE_VALUE (t);
4178 value = build_expr_from_tree (value);
4179 chain = TREE_CHAIN (t);
4180 if (chain && chain != void_type_node)
4181 chain = build_expr_from_tree (chain);
4182 return tree_cons (purpose, value, chain);
4187 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
4188 tree field = TREE_OPERAND (t, 1);
4190 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4191 and `x.A::b'. We must distinguish between those cases
4193 if (TREE_CODE (field) == SCOPE_REF)
4194 return build_object_ref (object,
4195 TREE_OPERAND (field, 0),
4196 TREE_OPERAND (field, 1));
4198 return build_x_component_ref (object, field,
4203 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
4209 /* digest_init will do the wrong thing if we let it. */
4210 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
4213 r = build_nt (CONSTRUCTOR, NULL_TREE,
4214 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
4215 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
4218 return digest_init (TREE_TYPE (t), r, 0);
4223 if (TYPE_P (TREE_OPERAND (t, 0)))
4224 return get_typeid (TREE_OPERAND (t, 0));
4225 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4228 return convert_from_reference (t);
4231 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4239 /* This is something of the form `int (*a)++' that has turned out to be an
4240 expr. It was only converted into parse nodes, so we need to go through
4241 and build up the semantics. Most of the work is done by
4242 build_expr_from_tree, above.
4244 In the above example, TYPE is `int' and DECL is `*a'. */
4247 reparse_decl_as_expr (type, decl)
4250 decl = build_expr_from_tree (decl);
4252 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4257 /* This is something of the form `int (*a)' that has turned out to be a
4258 decl. It was only converted into parse nodes, so we need to do the
4259 checking that make_{pointer,reference}_declarator do. */
4262 finish_decl_parsing (decl)
4265 extern int current_class_depth;
4267 switch (TREE_CODE (decl))
4269 case IDENTIFIER_NODE:
4272 return make_pointer_declarator
4273 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4275 return make_reference_declarator
4276 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4278 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4281 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4282 TREE_COMPLEXITY (decl) = current_class_depth;
4285 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4288 /* For attribute handling. */
4289 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4291 case TEMPLATE_ID_EXPR:
4294 my_friendly_abort (5);
4299 /* Return 1 if root encloses child. */
4302 is_namespace_ancestor (root, child)
4307 if (root == global_namespace)
4309 if (child == global_namespace)
4311 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4315 /* Return the namespace that is the common ancestor
4316 of two given namespaces. */
4319 namespace_ancestor (ns1, ns2)
4322 if (is_namespace_ancestor (ns1, ns2))
4324 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4327 /* Insert used into the using list of user. Set indirect_flag if this
4328 directive is not directly from the source. Also find the common
4329 ancestor and let our users know about the new namespace */
4331 add_using_namespace (user, used, indirect)
4337 /* Using oneself is a no-op. */
4340 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4341 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4342 /* Check if we already have this. */
4343 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4347 /* Promote to direct usage. */
4348 TREE_INDIRECT_USING (t) = 0;
4352 /* Add used to the user's using list. */
4353 DECL_NAMESPACE_USING (user)
4354 = tree_cons (used, namespace_ancestor (user, used),
4355 DECL_NAMESPACE_USING (user));
4357 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4359 /* Add user to the used's users list. */
4360 DECL_NAMESPACE_USERS (used)
4361 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4363 /* Recursively add all namespaces used. */
4364 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4365 /* indirect usage */
4366 add_using_namespace (user, TREE_PURPOSE (t), 1);
4368 /* Tell everyone using us about the new used namespaces. */
4369 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4370 add_using_namespace (TREE_PURPOSE (t), used, 1);
4373 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4374 duplicates. The first list becomes the tail of the result.
4376 The algorithm is O(n^2). We could get this down to O(n log n) by
4377 doing a sort on the addresses of the functions, if that becomes
4381 merge_functions (s1, s2)
4385 for (; s2; s2 = OVL_NEXT (s2))
4387 tree fn = OVL_CURRENT (s2);
4388 if (! ovl_member (fn, s1))
4389 s1 = build_overload (fn, s1);
4394 /* This should return an error not all definitions define functions.
4395 It is not an error if we find two functions with exactly the
4396 same signature, only if these are selected in overload resolution.
4397 old is the current set of bindings, new the freshly-found binding.
4398 XXX Do we want to give *all* candidates in case of ambiguity?
4399 XXX In what way should I treat extern declarations?
4400 XXX I don't want to repeat the entire duplicate_decls here */
4403 ambiguous_decl (name, old, new, flags)
4410 my_friendly_assert (old != NULL_TREE, 393);
4411 /* Copy the value. */
4412 val = BINDING_VALUE (new);
4414 switch (TREE_CODE (val))
4417 /* If we expect types or namespaces, and not templates,
4418 or this is not a template class. */
4419 if (LOOKUP_QUALIFIERS_ONLY (flags)
4420 && !DECL_CLASS_TEMPLATE_P (val))
4424 if (LOOKUP_NAMESPACES_ONLY (flags))
4427 case NAMESPACE_DECL:
4428 if (LOOKUP_TYPES_ONLY (flags))
4432 if (LOOKUP_QUALIFIERS_ONLY (flags))
4436 if (!BINDING_VALUE (old))
4437 BINDING_VALUE (old) = val;
4438 else if (val && val != BINDING_VALUE (old))
4440 if (is_overloaded_fn (BINDING_VALUE (old))
4441 && is_overloaded_fn (val))
4443 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4448 /* Some declarations are functions, some are not. */
4449 if (flags & LOOKUP_COMPLAIN)
4451 /* If we've already given this error for this lookup,
4452 BINDING_VALUE (old) is error_mark_node, so let's not
4453 repeat ourselves. */
4454 if (BINDING_VALUE (old) != error_mark_node)
4456 cp_error ("use of `%D' is ambiguous", name);
4457 cp_error_at (" first declared as `%#D' here",
4458 BINDING_VALUE (old));
4460 cp_error_at (" also declared as `%#D' here", val);
4462 BINDING_VALUE (old) = error_mark_node;
4465 /* ... and copy the type. */
4466 type = BINDING_TYPE (new);
4467 if (LOOKUP_NAMESPACES_ONLY (flags))
4469 if (!BINDING_TYPE (old))
4470 BINDING_TYPE (old) = type;
4471 else if (type && BINDING_TYPE (old) != type)
4473 if (flags & LOOKUP_COMPLAIN)
4475 cp_error ("`%D' denotes an ambiguous type",name);
4476 cp_error_at (" first type here", BINDING_TYPE (old));
4477 cp_error_at (" other type here", type);
4483 /* Subroutine of unualified_namespace_lookup:
4484 Add the bindings of NAME in used namespaces to VAL.
4485 We are currently looking for names in namespace SCOPE, so we
4486 look through USINGS for using-directives of namespaces
4487 which have SCOPE as a common ancestor with the current scope.
4488 Returns zero on errors. */
4491 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4492 tree name, val, usings, scope;
4498 /* Iterate over all used namespaces in current, searching for using
4499 directives of scope. */
4500 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4501 if (TREE_VALUE (iter) == scope)
4504 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4506 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4507 /* Resolve ambiguities. */
4508 val = ambiguous_decl (name, val, val1, flags);
4510 return BINDING_VALUE (val) != error_mark_node;
4514 Accepts the NAME to lookup and its qualifying SCOPE.
4515 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4519 qualified_lookup_using_namespace (name, scope, result, flags)
4525 /* Maintain a list of namespaces visited... */
4526 tree seen = NULL_TREE;
4527 /* ... and a list of namespace yet to see. */
4528 tree todo = NULL_TREE;
4530 /* Look through namespace aliases. */
4531 scope = ORIGINAL_NAMESPACE (scope);
4532 while (scope && (result != error_mark_node))
4534 seen = tree_cons (scope, NULL_TREE, seen);
4535 result = ambiguous_decl (name, result,
4536 binding_for_name (name, scope), flags);
4537 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4538 /* Consider using directives. */
4539 for (usings = DECL_NAMESPACE_USING (scope); usings;
4540 usings = TREE_CHAIN (usings))
4541 /* If this was a real directive, and we have not seen it. */
4542 if (!TREE_INDIRECT_USING (usings)
4543 && !purpose_member (TREE_PURPOSE (usings), seen))
4544 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4547 scope = TREE_PURPOSE (todo);
4548 todo = TREE_CHAIN (todo);
4551 scope = NULL_TREE; /* If there never was a todo list. */
4553 return result != error_mark_node;
4556 /* [namespace.memdef]/2 */
4558 /* Set the context of a declaration to scope. Complain if we are not
4562 set_decl_namespace (decl, scope, friendp)
4568 if (scope == fake_std_node)
4569 scope = global_namespace;
4570 /* Get rid of namespace aliases. */
4571 scope = ORIGINAL_NAMESPACE (scope);
4573 /* It is ok for friends to be qualified in parallel space. */
4574 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4575 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4577 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4578 if (scope != current_namespace)
4580 /* See whether this has been declared in the namespace. */
4581 old = namespace_binding (DECL_NAME (decl), scope);
4583 /* No old declaration at all. */
4585 if (!is_overloaded_fn (decl))
4586 /* Don't compare non-function decls with decls_match here,
4587 since it can't check for the correct constness at this
4588 point. pushdecl will find those errors later. */
4590 /* Since decl is a function, old should contain a function decl. */
4591 if (!is_overloaded_fn (old))
4593 if (processing_template_decl || processing_specialization)
4594 /* We have not yet called push_template_decl to turn the
4595 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4596 won't match. But, we'll check later, when we construct the
4599 for (; old; old = OVL_NEXT (old))
4600 if (decls_match (decl, OVL_CURRENT (old)))
4606 cp_error ("`%D' should have been declared inside `%D'",
4610 /* Compute the namespace where a declaration is defined. */
4613 decl_namespace (decl)
4617 decl = TYPE_STUB_DECL (decl);
4618 while (DECL_CONTEXT (decl))
4620 decl = DECL_CONTEXT (decl);
4621 if (TREE_CODE (decl) == NAMESPACE_DECL)
4624 decl = TYPE_STUB_DECL (decl);
4625 my_friendly_assert (DECL_P (decl), 390);
4628 return global_namespace;
4631 /* Return the namespace where the current declaration is declared. */
4634 current_decl_namespace ()
4637 /* If we have been pushed into a different namespace, use it. */
4638 if (decl_namespace_list)
4639 return TREE_PURPOSE (decl_namespace_list);
4641 if (current_class_type)
4642 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4643 else if (current_function_decl)
4644 result = decl_namespace (current_function_decl);
4646 result = current_namespace;
4650 /* Temporarily set the namespace for the current declaration. */
4653 push_decl_namespace (decl)
4656 if (TREE_CODE (decl) != NAMESPACE_DECL)
4657 decl = decl_namespace (decl);
4658 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4659 NULL_TREE, decl_namespace_list);
4663 pop_decl_namespace ()
4665 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4668 /* Enter a class or namespace scope. */
4674 if (TREE_CODE (t) == NAMESPACE_DECL)
4675 push_decl_namespace (t);
4680 /* Leave scope pushed by push_scope. */
4686 if (TREE_CODE (t) == NAMESPACE_DECL)
4687 pop_decl_namespace ();
4692 /* [basic.lookup.koenig] */
4693 /* A non-zero return value in the functions below indicates an error.
4694 All nodes allocated in the procedure are on the scratch obstack. */
4704 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4705 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4706 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4707 static int add_function PARAMS ((struct arg_lookup *, tree));
4708 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4709 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4710 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4712 /* Add a function to the lookup structure.
4713 Returns 1 on error. */
4716 add_function (k, fn)
4717 struct arg_lookup *k;
4720 /* We used to check here to see if the function was already in the list,
4721 but that's O(n^2), which is just too expensive for function lookup.
4722 Now we deal with the occasional duplicate in joust. In doing this, we
4723 assume that the number of duplicates will be small compared to the
4724 total number of functions being compared, which should usually be the
4727 /* We must find only functions, or exactly one non-function. */
4730 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4731 k->functions = build_overload (fn, k->functions);
4734 tree f1 = OVL_CURRENT (k->functions);
4736 if (is_overloaded_fn (f1))
4738 fn = f1; f1 = f2; f2 = fn;
4740 cp_error_at ("`%D' is not a function,", f1);
4741 cp_error_at (" conflict with `%D'", f2);
4742 cp_error (" in call to `%D'", k->name);
4749 /* Add functions of a namespace to the lookup structure.
4750 Returns 1 on error. */
4753 arg_assoc_namespace (k, scope)
4754 struct arg_lookup *k;
4759 if (purpose_member (scope, k->namespaces))
4761 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4763 value = namespace_binding (k->name, scope);
4767 for (; value; value = OVL_NEXT (value))
4768 if (add_function (k, OVL_CURRENT (value)))
4774 /* Adds everything associated with a template argument to the lookup
4775 structure. Returns 1 on error. */
4778 arg_assoc_template_arg (k, arg)
4779 struct arg_lookup* k;
4782 /* [basic.lookup.koenig]
4784 If T is a template-id, its associated namespaces and classes are
4785 ... the namespaces and classes associated with the types of the
4786 template arguments provided for template type parameters
4787 (excluding template template parameters); the namespaces in which
4788 any template template arguments are defined; and the classes in
4789 which any member templates used as template template arguments
4790 are defined. [Note: non-type template arguments do not
4791 contribute to the set of associated namespaces. ] */
4793 /* Consider first template template arguments. */
4794 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
4796 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4798 tree ctx = CP_DECL_CONTEXT (arg);
4800 /* It's not a member template. */
4801 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4802 return arg_assoc_namespace (k, ctx);
4803 /* Otherwise, it must be member template. */
4805 return arg_assoc_class (k, ctx);
4807 /* It's not a template template argument, but it is a type template
4809 else if (TYPE_P (arg))
4810 return arg_assoc_type (k, arg);
4811 /* It's a non-type template argument. */
4816 /* Adds everything associated with class to the lookup structure.
4817 Returns 1 on error. */
4820 arg_assoc_class (k, type)
4821 struct arg_lookup* k;
4824 tree list, friends, context;
4827 /* Backend build structures, such as __builtin_va_list, aren't
4828 affected by all this. */
4829 if (!CLASS_TYPE_P (type))
4832 if (purpose_member (type, k->classes))
4834 k->classes = tree_cons (type, NULL_TREE, k->classes);
4836 context = decl_namespace (TYPE_MAIN_DECL (type));
4837 if (arg_assoc_namespace (k, context))
4840 /* Process baseclasses. */
4841 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4842 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4845 /* Process friends. */
4846 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4847 list = TREE_CHAIN (list))
4848 if (k->name == TREE_PURPOSE (list))
4849 for (friends = TREE_VALUE (list); friends;
4850 friends = TREE_CHAIN (friends))
4851 /* Only interested in global functions with potentially hidden
4852 (i.e. unqualified) declarations. */
4853 if (TREE_PURPOSE (list) == error_mark_node && TREE_VALUE (list)
4854 && decl_namespace (TREE_VALUE (list)) == context)
4855 if (add_function (k, TREE_VALUE (list)))
4858 /* Process template arguments. */
4859 if (CLASSTYPE_TEMPLATE_INFO (type))
4861 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4862 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4863 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4869 /* Adds everything associated with a given type.
4870 Returns 1 on error. */
4873 arg_assoc_type (k, type)
4874 struct arg_lookup *k;
4877 switch (TREE_CODE (type))
4888 if (TYPE_PTRMEMFUNC_P (type))
4889 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4890 return arg_assoc_class (k, type);
4892 case REFERENCE_TYPE:
4894 return arg_assoc_type (k, TREE_TYPE (type));
4897 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4899 /* Pointer to member: associate class type and value type. */
4900 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4902 return arg_assoc_type (k, TREE_TYPE (type));
4904 /* The basetype is referenced in the first arg type, so just
4907 /* Associate the parameter types. */
4908 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4910 /* Associate the return type. */
4911 return arg_assoc_type (k, TREE_TYPE (type));
4912 case TEMPLATE_TYPE_PARM:
4913 case BOUND_TEMPLATE_TEMPLATE_PARM:
4918 if (type == unknown_type_node)
4920 /* else fall through */
4922 my_friendly_abort (390);
4927 /* Adds everything associated with arguments. Returns 1 on error. */
4930 arg_assoc_args (k, args)
4931 struct arg_lookup* k;
4934 for (; args; args = TREE_CHAIN (args))
4935 if (arg_assoc (k, TREE_VALUE (args)))
4940 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4944 struct arg_lookup* k;
4947 if (n == error_mark_node)
4951 return arg_assoc_type (k, n);
4953 if (! type_unknown_p (n))
4954 return arg_assoc_type (k, TREE_TYPE (n));
4956 if (TREE_CODE (n) == ADDR_EXPR)
4957 n = TREE_OPERAND (n, 0);
4958 if (TREE_CODE (n) == COMPONENT_REF)
4959 n = TREE_OPERAND (n, 1);
4960 if (TREE_CODE (n) == OFFSET_REF)
4961 n = TREE_OPERAND (n, 1);
4962 while (TREE_CODE (n) == TREE_LIST)
4965 if (TREE_CODE (n) == FUNCTION_DECL)
4966 return arg_assoc_type (k, TREE_TYPE (n));
4967 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4969 /* [basic.lookup.koenig]
4971 If T is a template-id, its associated namespaces and classes
4972 are the namespace in which the template is defined; for
4973 member templates, the member template's class... */
4974 tree template = TREE_OPERAND (n, 0);
4975 tree args = TREE_OPERAND (n, 1);
4979 if (TREE_CODE (template) == COMPONENT_REF)
4980 template = TREE_OPERAND (template, 1);
4982 /* First, the template. There may actually be more than one if
4983 this is an overloaded function template. But, in that case,
4984 we only need the first; all the functions will be in the same
4986 template = OVL_CURRENT (template);
4988 ctx = CP_DECL_CONTEXT (template);
4990 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4992 if (arg_assoc_namespace (k, ctx) == 1)
4995 /* It must be a member template. */
4996 else if (arg_assoc_class (k, ctx) == 1)
4999 /* Now the arguments. */
5000 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
5001 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
5006 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
5008 for (; n; n = OVL_CHAIN (n))
5009 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
5016 /* Performs Koenig lookup depending on arguments, where fns
5017 are the functions found in normal lookup. */
5020 lookup_arg_dependent (name, fns, args)
5025 struct arg_lookup k;
5026 tree fn = NULL_TREE;
5030 k.classes = NULL_TREE;
5032 /* Note that we've already looked at some namespaces during normal
5033 unqualified lookup, unless we found a decl in function scope. */
5035 fn = OVL_CURRENT (fns);
5036 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
5037 k.namespaces = NULL_TREE;
5039 unqualified_namespace_lookup (name, 0, &k.namespaces);
5041 arg_assoc_args (&k, args);
5045 /* Process a namespace-alias declaration. */
5048 do_namespace_alias (alias, namespace)
5049 tree alias, namespace;
5051 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5053 /* The parser did not find it, so it's not there. */
5054 cp_error ("unknown namespace `%D'", namespace);
5058 namespace = ORIGINAL_NAMESPACE (namespace);
5060 /* Build the alias. */
5061 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5062 DECL_NAMESPACE_ALIAS (alias) = namespace;
5066 /* Check a non-member using-declaration. Return the name and scope
5067 being used, and the USING_DECL, or NULL_TREE on failure. */
5070 validate_nonmember_using_decl (decl, scope, name)
5075 if (TREE_CODE (decl) == SCOPE_REF
5076 && TREE_OPERAND (decl, 0) == fake_std_node)
5078 if (namespace_bindings_p ()
5079 && current_namespace == global_namespace)
5080 /* There's no need for a using declaration at all, here,
5081 since `std' is the same as `::'. We can't just pass this
5082 on because we'll complain later about declaring something
5083 in the same scope as a using declaration with the same
5084 name. We return NULL_TREE which indicates to the caller
5085 that there's no need to do any further processing. */
5088 *scope = global_namespace;
5089 *name = TREE_OPERAND (decl, 1);
5091 else if (TREE_CODE (decl) == SCOPE_REF)
5093 *scope = TREE_OPERAND (decl, 0);
5094 *name = TREE_OPERAND (decl, 1);
5096 /* [namespace.udecl]
5098 A using-declaration for a class member shall be a
5099 member-declaration. */
5100 if (TREE_CODE (*scope) != NAMESPACE_DECL)
5102 if (TYPE_P (*scope))
5103 cp_error ("`%T' is not a namespace", *scope);
5105 cp_error ("`%D' is not a namespace", *scope);
5109 else if (TREE_CODE (decl) == IDENTIFIER_NODE
5110 || TREE_CODE (decl) == TYPE_DECL
5111 || TREE_CODE (decl) == TEMPLATE_DECL)
5113 *scope = global_namespace;
5117 my_friendly_abort (382);
5119 *name = DECL_NAME (*name);
5120 /* Make a USING_DECL. */
5121 return push_using_decl (*scope, *name);
5124 /* Process local and global using-declarations. */
5127 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
5129 tree oldval, oldtype;
5130 tree *newval, *newtype;
5134 *newval = *newtype = NULL_TREE;
5135 decls = make_node (CPLUS_BINDING);
5136 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
5140 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
5142 cp_error ("`%D' not declared", name);
5146 /* Check for using functions. */
5147 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
5151 if (oldval && !is_overloaded_fn (oldval))
5153 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
5158 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
5160 tree new_fn = OVL_CURRENT (tmp);
5162 /* [namespace.udecl]
5164 If a function declaration in namespace scope or block
5165 scope has the same name and the same parameter types as a
5166 function introduced by a using declaration the program is
5168 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
5170 tree old_fn = OVL_CURRENT (tmp1);
5172 if (!OVL_USED (tmp1)
5173 && compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
5174 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
5176 if (!(DECL_EXTERN_C_P (new_fn)
5177 && DECL_EXTERN_C_P (old_fn)))
5178 /* There was already a non-using declaration in
5179 this scope with the same parameter types. */
5180 cp_error ("`%D' is already declared in this scope",
5184 else if (duplicate_decls (new_fn, old_fn))
5185 /* We're re-using something we already used
5186 before. We don't need to add it again. */
5190 /* If we broke out of the loop, there's no reason to add
5191 this function to the using declarations for this
5196 *newval = build_overload (OVL_CURRENT (tmp), *newval);
5197 if (TREE_CODE (*newval) != OVERLOAD)
5198 *newval = ovl_cons (*newval, NULL_TREE);
5199 OVL_USED (*newval) = 1;
5204 *newval = BINDING_VALUE (decls);
5206 duplicate_decls (*newval, oldval);
5209 *newtype = BINDING_TYPE (decls);
5210 if (oldtype && *newtype && oldtype != *newtype)
5212 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5218 /* Process a using-declaration not appearing in class or local scope. */
5221 do_toplevel_using_decl (decl)
5224 tree scope, name, binding;
5225 tree oldval, oldtype, newval, newtype;
5227 decl = validate_nonmember_using_decl (decl, &scope, &name);
5228 if (decl == NULL_TREE)
5231 binding = binding_for_name (name, current_namespace);
5233 oldval = BINDING_VALUE (binding);
5234 oldtype = BINDING_TYPE (binding);
5236 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5238 /* Copy declarations found. */
5240 BINDING_VALUE (binding) = newval;
5242 BINDING_TYPE (binding) = newtype;
5246 /* Process a using-declaration at function scope. */
5249 do_local_using_decl (decl)
5253 tree oldval, oldtype, newval, newtype;
5255 decl = validate_nonmember_using_decl (decl, &scope, &name);
5256 if (decl == NULL_TREE)
5259 oldval = lookup_name_current_level (name);
5260 oldtype = lookup_type_current_level (name);
5262 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5266 if (is_overloaded_fn (newval))
5270 /* We only need to push declarations for those functions
5271 that were not already bound in the current level.
5272 The old value might be NULL_TREE, it might be a single
5273 function, or an OVERLOAD. */
5274 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5275 term = OVL_FUNCTION (oldval);
5278 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5280 push_overloaded_decl (OVL_CURRENT (fn),
5281 PUSH_LOCAL | PUSH_USING);
5284 push_local_binding (name, newval, PUSH_USING);
5287 set_identifier_type_value (name, newtype);
5291 do_class_using_decl (decl)
5296 if (TREE_CODE (decl) != SCOPE_REF
5297 || !TYPE_P (TREE_OPERAND (decl, 0)))
5299 cp_error ("using-declaration for non-member at class scope");
5302 name = TREE_OPERAND (decl, 1);
5303 if (TREE_CODE (name) == BIT_NOT_EXPR)
5305 cp_error ("using-declaration for destructor");
5308 if (TREE_CODE (name) == TYPE_DECL)
5309 name = DECL_NAME (name);
5311 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5313 value = build_lang_decl (USING_DECL, name, void_type_node);
5314 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5318 /* Process a using-directive. */
5321 do_using_directive (namespace)
5324 if (namespace == fake_std_node)
5326 /* using namespace A::B::C; */
5327 if (TREE_CODE (namespace) == SCOPE_REF)
5328 namespace = TREE_OPERAND (namespace, 1);
5329 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5331 /* Lookup in lexer did not find a namespace. */
5332 cp_error ("namespace `%T' undeclared", namespace);
5335 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5337 cp_error ("`%T' is not a namespace", namespace);
5340 namespace = ORIGINAL_NAMESPACE (namespace);
5341 if (!toplevel_bindings_p ())
5342 push_using_directive (namespace);
5345 add_using_namespace (current_namespace, namespace, 0);
5349 check_default_args (x)
5352 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5353 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5354 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5356 if (TREE_PURPOSE (arg))
5360 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5371 TREE_USED (decl) = 1;
5372 if (processing_template_decl)
5374 assemble_external (decl);
5376 /* Is it a synthesized method that needs to be synthesized? */
5377 if (TREE_CODE (decl) == FUNCTION_DECL
5378 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5379 && DECL_ARTIFICIAL (decl)
5380 && ! DECL_INITIAL (decl)
5381 /* Kludge: don't synthesize for default args. */
5382 && current_function_decl)
5384 synthesize_method (decl);
5385 /* If we've already synthesized the method we don't need to
5386 instantiate it, so we can return right away. */
5390 /* If this is a function or variable that is an instance of some
5391 template, we now know that we will need to actually do the
5392 instantiation. We check that DECL is not an explicit
5393 instantiation because that is not checked in instantiate_decl. */
5394 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5395 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5396 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5397 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5398 instantiate_decl (decl, /*defer_ok=*/1);
5401 /* Helper function for named_class_head_sans_basetype nonterminal. We
5402 have just seen something of the form `AGGR SCOPE::ID'. Return a
5403 TYPE_DECL for the type declared by ID in SCOPE. */
5406 handle_class_head (aggr, scope, id)
5407 tree aggr, scope, id;
5409 tree decl = NULL_TREE;
5411 if (TREE_CODE (id) == TYPE_DECL)
5412 /* We must bash typedefs back to the main decl of the type. Otherwise
5413 we become confused about scopes. */
5414 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5415 else if (DECL_CLASS_TEMPLATE_P (id))
5416 decl = DECL_TEMPLATE_RESULT (id);
5419 tree current = current_scope ();
5421 if (current == NULL_TREE)
5422 current = current_namespace;
5423 if (scope == fake_std_node)
5424 scope = global_namespace;
5425 if (scope == NULL_TREE)
5426 scope = global_namespace;
5430 /* According to the suggested resolution of core issue 180,
5431 'typename' is assumed after a class-key. */
5432 decl = make_typename_type (scope, id, 1);
5433 if (decl != error_mark_node)
5434 decl = TYPE_MAIN_DECL (decl);
5438 else if (scope == current)
5440 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5441 Be nice about it. */
5443 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5444 FROB_CONTEXT (scope), id);
5446 else if (scope != global_namespace)
5447 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5449 cp_error ("no file-scope type named `%D'", id);
5451 /* Inject it at the current scope. */
5453 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, 1));
5456 /* Enter the SCOPE. If this turns out not to be a definition, the
5457 parser must leave the scope. */
5458 push_scope (CP_DECL_CONTEXT (decl));
5460 /* If we see something like:
5462 template <typename T> struct S::I ....
5464 we must create a TEMPLATE_DECL for the nested type. */
5465 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5466 decl = push_template_decl (decl);
5471 /* Initialize decl2.c. */
5476 ggc_add_tree_root (&decl_namespace_list, 1);
5477 ggc_add_tree_varray_root (&deferred_fns, 1);
5478 ggc_add_tree_varray_root (&pending_statics, 1);
5479 ggc_add_tree_varray_root (&ssdf_decls, 1);
5480 ggc_add_tree_root (&ssdf_decl, 1);
5481 ggc_add_tree_root (&priority_decl, 1);
5482 ggc_add_tree_root (&initialize_p_decl, 1);
5483 ggc_add_tree_root (&pending_vtables, 1);