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"
48 #include "diagnostic.h"
52 extern cpp_reader parse_in;
55 /* This structure contains information about the initializations
56 and/or destructions required for a particular priority level. */
57 typedef struct priority_info_s {
58 /* Non-zero if there have been any initializations at this priority
59 throughout the translation unit. */
60 int initializations_p;
61 /* Non-zero if there have been any destructions at this priority
62 throughout the translation unit. */
66 static tree get_sentry PARAMS ((tree));
67 static void mark_vtable_entries PARAMS ((tree));
68 static void grok_function_init PARAMS ((tree, tree));
69 static int finish_vtable_vardecl PARAMS ((tree *, void *));
70 static int prune_vtable_vardecl PARAMS ((tree *, void *));
71 static int is_namespace_ancestor PARAMS ((tree, tree));
72 static void add_using_namespace PARAMS ((tree, tree, int));
73 static tree ambiguous_decl PARAMS ((tree, tree, tree,int));
74 static tree build_anon_union_vars PARAMS ((tree, tree*, int, int));
75 static int acceptable_java_type PARAMS ((tree));
76 static void output_vtable_inherit PARAMS ((tree));
77 static tree start_objects PARAMS ((int, int));
78 static void finish_objects PARAMS ((int, int, tree));
79 static tree merge_functions PARAMS ((tree, tree));
80 static tree decl_namespace PARAMS ((tree));
81 static tree validate_nonmember_using_decl PARAMS ((tree, tree *, tree *));
82 static void do_nonmember_using_decl PARAMS ((tree, tree, tree, tree,
84 static tree start_static_storage_duration_function PARAMS ((void));
85 static void finish_static_storage_duration_function PARAMS ((tree));
86 static priority_info get_priority_info PARAMS ((int));
87 static void do_static_initialization PARAMS ((tree, tree));
88 static void do_static_destruction PARAMS ((tree));
89 static tree start_static_initialization_or_destruction PARAMS ((tree, int));
90 static void finish_static_initialization_or_destruction PARAMS ((tree));
91 static void generate_ctor_or_dtor_function PARAMS ((int, int));
92 static int generate_ctor_and_dtor_functions_for_priority
93 PARAMS ((splay_tree_node, void *));
94 static tree prune_vars_needing_no_initialization PARAMS ((tree));
95 static void write_out_vars PARAMS ((tree));
96 static void import_export_class PARAMS ((tree));
97 static tree key_method PARAMS ((tree));
98 static int compare_options PARAMS ((const PTR, const PTR));
100 extern int current_class_depth;
102 /* A list of virtual function tables we must make sure to write out. */
103 tree pending_vtables;
105 /* A list of static class variables. This is needed, because a
106 static class variable can be declared inside the class without
107 an initializer, and then initialized, staticly, outside the class. */
108 static varray_type pending_statics;
109 #define pending_statics_used \
110 (pending_statics ? pending_statics->elements_used : 0)
112 /* A list of functions which were declared inline, but which we
113 may need to emit outline anyway. */
114 static varray_type deferred_fns;
115 #define deferred_fns_used \
116 (deferred_fns ? deferred_fns->elements_used : 0)
118 /* Same, but not reset. Local temp variables and global temp variables
119 can have the same name. */
120 static int global_temp_name_counter;
122 /* Flag used when debugging spew.c */
124 extern int spew_debug;
126 /* Nonzero if we're done parsing and into end-of-file activities. */
130 /* Functions called along with real static constructors and destructors. */
132 tree static_ctors, static_dtors;
134 /* The :: namespace. */
136 tree global_namespace;
138 /* The stack for namespaces of current declarations. */
140 static tree decl_namespace_list;
143 /* C (and C++) language-specific option variables. */
145 /* Nonzero means allow type mismatches in conditional expressions;
146 just make their values `void'. */
148 int flag_cond_mismatch;
150 /* Nonzero means give `double' the same size as `float'. */
152 int flag_short_double;
154 /* Nonzero means don't recognize the keyword `asm'. */
158 /* Nonzero means don't recognize any extension keywords. */
160 int flag_no_gnu_keywords;
162 /* Nonzero means don't recognize the non-ANSI builtin functions. */
166 /* Nonzero means don't recognize the non-ANSI builtin functions.
169 int flag_no_nonansi_builtin;
171 /* Nonzero means do some things the same way PCC does. Only provided so
172 the compiler will link. */
174 int flag_traditional;
176 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
178 int flag_signed_bitfields = 1;
180 /* Nonzero means enable obscure standard features and disable GNU
181 extensions that might cause standard-compliant code to be
186 /* Nonzero means do emit exported implementations of functions even if
187 they can be inlined. */
189 int flag_implement_inlines = 1;
191 /* Nonzero means do emit exported implementations of templates, instead of
192 multiple static copies in each file that needs a definition. */
194 int flag_external_templates;
196 /* Nonzero means that the decision to emit or not emit the implementation of a
197 template depends on where the template is instantiated, rather than where
200 int flag_alt_external_templates;
202 /* Nonzero means that implicit instantiations will be emitted if needed. */
204 int flag_implicit_templates = 1;
206 /* Nonzero means that implicit instantiations of inline templates will be
207 emitted if needed, even if instantiations of non-inline templates
210 int flag_implicit_inline_templates = 1;
212 /* Nonzero means warn about implicit declarations. */
214 int warn_implicit = 1;
216 /* Nonzero means warn about usage of long long when `-pedantic'. */
218 int warn_long_long = 1;
220 /* Nonzero means warn when all ctors or dtors are private, and the class
223 int warn_ctor_dtor_privacy = 1;
225 /* True if we want to implement vtables using "thunks".
226 The default is off. */
228 #ifndef DEFAULT_VTABLE_THUNKS
229 #define DEFAULT_VTABLE_THUNKS 0
231 int flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
233 /* Nonzero means generate separate instantiation control files and juggle
234 them at link time. */
236 int flag_use_repository;
238 /* Nonzero if we want to issue diagnostics that the standard says are not
241 int flag_optional_diags = 1;
243 /* Nonzero means give string constants the type `const char *', as mandated
246 int flag_const_strings = 1;
248 /* If non-NULL, dump the tree structure for the entire translation
249 unit to this file. */
251 const char *flag_dump_translation_unit = 0;
253 /* Nonzero means warn about deprecated conversion from string constant to
256 int warn_write_strings;
258 /* Nonzero means warn about pointer casts that can drop a type qualifier
259 from the pointer target type. */
263 /* Nonzero means warn about sizeof(function) or addition/subtraction
264 of function pointers. */
266 int warn_pointer_arith = 1;
268 /* Nonzero means warn for any function def without prototype decl. */
270 int warn_missing_prototypes;
272 /* Nonzero means warn about multiple (redundant) decls for the same single
273 variable or function. */
275 int warn_redundant_decls;
277 /* Warn if initializer is not completely bracketed. */
279 int warn_missing_braces;
281 /* Warn about comparison of signed and unsigned values. */
283 int warn_sign_compare;
285 /* Warn about testing equality of floating point numbers. */
287 int warn_float_equal = 0;
289 /* Warn about *printf or *scanf format/argument anomalies. */
293 /* Warn about a subscript that has type char. */
295 int warn_char_subscripts;
297 /* Warn if a type conversion is done that might have confusing results. */
301 /* Warn if adding () is suggested. */
303 int warn_parentheses;
305 /* Non-zero means warn in function declared in derived class has the
306 same name as a virtual in the base class, but fails to match the
307 type signature of any virtual function in the base class. */
308 int warn_overloaded_virtual;
310 /* Non-zero means warn when declaring a class that has a non virtual
311 destructor, when it really ought to have a virtual one. */
314 /* Non-zero means warn when a function is declared extern and later inline. */
315 int warn_extern_inline;
317 /* Non-zero means warn when the compiler will reorder code. */
320 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
323 /* Non-zero means warn when we convert a pointer to member function
324 into a pointer to (void or function). */
325 int warn_pmf2ptr = 1;
327 /* Nonzero means warn about violation of some Effective C++ style rules. */
331 /* Nonzero means warn where overload resolution chooses a promotion from
332 unsigned to signed over a conversion to an unsigned of the same size. */
336 /* Nonzero means warn when an old-style cast is used. */
338 int warn_old_style_cast;
340 /* Warn about #pragma directives that are not recognised. */
342 int warn_unknown_pragmas; /* Tri state variable. */
344 /* Nonzero means warn about use of multicharacter literals. */
346 int warn_multichar = 1;
348 /* Nonzero means warn when non-templatized friend functions are
349 declared within a template */
351 int warn_nontemplate_friend = 1;
353 /* Nonzero means complain about deprecated features. */
355 int warn_deprecated = 1;
357 /* Nonzero means `$' can be in an identifier. */
359 #ifndef DOLLARS_IN_IDENTIFIERS
360 #define DOLLARS_IN_IDENTIFIERS 1
362 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
364 /* Nonzero for -fno-strict-prototype switch: do not consider empty
365 argument prototype to mean function takes no arguments. */
367 int flag_strict_prototype = 2;
368 int strict_prototype = 1;
369 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
371 /* Nonzero means that labels can be used as first-class objects */
375 /* Nonzero means allow Microsoft extensions without a pedwarn. */
377 int flag_ms_extensions;
379 /* Non-zero means to collect statistics which might be expensive
380 and to print them when we are done. */
381 int flag_detailed_statistics;
383 /* C++ specific flags. */
384 /* Zero means that `this' is a *const. This gives nice behavior in the
385 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
386 -2 means we're constructing an object and it has fixed type. */
388 int flag_this_is_variable;
390 /* Nonzero means we should attempt to elide constructors when possible. */
392 int flag_elide_constructors = 1;
394 /* Nonzero means that member functions defined in class scope are
395 inline by default. */
397 int flag_default_inline = 1;
399 /* Controls whether compiler generates 'type descriptor' that give
400 run-time type information. */
403 /* Nonzero if we wish to output cross-referencing information
404 for the GNU class browser. */
405 extern int flag_gnu_xref;
407 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
410 int flag_huge_objects;
412 /* Nonzero if we want to conserve space in the .o files. We do this
413 by putting uninitialized data and runtime initialized data into
414 .common instead of .data at the expense of not flagging multiple
417 int flag_conserve_space;
419 /* Nonzero if we want to obey access control semantics. */
421 int flag_access_control = 1;
423 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
425 int flag_operator_names = 1;
427 /* Nonzero if we want to check the return value of new and avoid calling
428 constructors if it is a null pointer. */
432 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
433 initialization variables.
434 0: Old rules, set by -fno-for-scope.
435 2: New ISO rules, set by -ffor-scope.
436 1: Try to implement new ISO rules, but with backup compatibility
437 (and warnings). This is the default, for now. */
439 int flag_new_for_scope = 1;
441 /* Nonzero if we want to emit defined symbols with common-like linkage as
442 weak symbols where possible, in order to conform to C++ semantics.
443 Otherwise, emit them as local symbols. */
447 /* Nonzero to enable experimental ABI changes. */
451 /* Nonzero to use __cxa_atexit, rather than atexit, to register
452 destructors for local statics and global objects. */
454 int flag_use_cxa_atexit;
456 /* Nonzero to not ignore namespace std. */
458 int flag_honor_std = ENABLE_STD_NAMESPACE;
460 /* Nonzero if we should expand functions calls inline at the tree
461 level, rather than at the RTL level. */
463 int flag_inline_trees = 0;
465 /* Maximum template instantiation depth. Must be at least 17 for ISO
468 int max_tinst_depth = 17;
470 /* The name-mangling scheme to use. Must be 1 or greater to support
471 template functions with identical types, but different template
473 int name_mangling_version = 2;
475 /* Nonzero if wchar_t should be `unsigned short' instead of whatever it
476 would normally be, for use with WINE. */
477 int flag_short_wchar;
479 /* Nonzero if squashed mangling is to be performed.
480 This uses the B and K codes to reference previously seen class types
481 and class qualifiers. */
482 int flag_do_squangling;
484 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
488 /* Nonzero means make the default pedwarns warnings instead of errors.
489 The value of this flag is ignored if -pedantic is specified. */
493 /* Nonzero means to implement standard semantics for exception
494 specifications, calling unexpected if an exception is thrown that
495 doesn't match the specification. Zero means to treat them as
496 assertions and optimize accordingly, but not check them. */
498 int flag_enforce_eh_specs = 1;
500 /* If this variable is defined to a non-NULL value, it will be called
501 after the file has been completely parsed. */
503 void (*back_end_hook) PARAMS ((tree));
505 /* Table of language-dependent -f options.
506 STRING is the option name. VARIABLE is the address of the variable.
507 ON_VALUE is the value to store in VARIABLE
508 if `-fSTRING' is seen as an option.
509 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
511 static struct { const char *string; int *variable; int on_value;}
515 {"signed-char", &flag_signed_char, 1},
516 {"unsigned-char", &flag_signed_char, 0},
517 {"signed-bitfields", &flag_signed_bitfields, 1},
518 {"unsigned-bitfields", &flag_signed_bitfields, 0},
519 {"short-enums", &flag_short_enums, 1},
520 {"short-double", &flag_short_double, 1},
521 {"short-wchar", &flag_short_wchar, 1},
522 {"cond-mismatch", &flag_cond_mismatch, 1},
523 {"asm", &flag_no_asm, 0},
524 {"builtin", &flag_no_builtin, 0},
526 /* C++-only options. */
527 {"access-control", &flag_access_control, 1},
528 {"check-new", &flag_check_new, 1},
529 {"conserve-space", &flag_conserve_space, 1},
530 {"const-strings", &flag_const_strings, 1},
531 {"default-inline", &flag_default_inline, 1},
532 {"dollars-in-identifiers", &dollars_in_ident, 1},
533 {"elide-constructors", &flag_elide_constructors, 1},
534 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
535 {"external-templates", &flag_external_templates, 1},
536 {"for-scope", &flag_new_for_scope, 2},
537 {"gnu-keywords", &flag_no_gnu_keywords, 0},
538 {"handle-exceptions", &flag_exceptions, 1},
539 {"honor-std", &flag_honor_std, 1},
540 {"huge-objects", &flag_huge_objects, 1},
541 {"implement-inlines", &flag_implement_inlines, 1},
542 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
543 {"implicit-templates", &flag_implicit_templates, 1},
544 {"labels-ok", &flag_labels_ok, 1},
545 {"ms-extensions", &flag_ms_extensions, 1},
546 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
547 {"operator-names", &flag_operator_names, 1},
548 {"optional-diags", &flag_optional_diags, 1},
549 {"permissive", &flag_permissive, 1},
550 {"repo", &flag_use_repository, 1},
551 {"rtti", &flag_rtti, 1},
552 {"squangle", &flag_do_squangling, 1},
553 {"stats", &flag_detailed_statistics, 1},
554 {"strict-prototype", &flag_strict_prototype, 1},
555 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
556 {"vtable-gc", &flag_vtable_gc, 1},
557 {"vtable-thunks", &flag_vtable_thunks, 1},
558 {"weak", &flag_weak, 1},
559 {"xref", &flag_gnu_xref, 1}
562 /* The list of `-f' options that we no longer support. The `-f'
563 prefix is not given in this table. The `-fno-' variants are not
564 listed here. This table must be kept in alphabetical order. */
565 static const char * const unsupported_options[] = {
573 /* Compare two option strings, pointed two by P1 and P2, for use with
577 compare_options (p1, p2)
581 return strcmp (*((char **) p1), *((char **) p2));
584 /* Decode the string P as a language-specific option.
585 Return the number of strings consumed for a valid option.
586 Otherwise return 0. Should not complain if it does not
587 recognise the option. */
590 lang_decode_option (argc, argv)
598 int strings_processed;
599 const char *p = argv[0];
601 strings_processed = cpp_handle_option (&parse_in, argc, argv);
603 strings_processed = 0;
604 #endif /* ! USE_CPPLIB */
606 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
608 else if (p[0] == '-' && p[1] == 'f')
610 /* Some kind of -f option.
611 P's value is the option sans `-f'.
612 Search for it in the table of options. */
613 const char *option_value = NULL;
614 const char *positive_option;
618 /* Try special -f options. */
620 /* See if this is one of the options no longer supported. We
621 used to support these options, so we continue to accept them,
623 if (strncmp (p, "no-", strlen ("no-")) == 0)
624 positive_option = p + strlen ("no-");
628 /* If the option is present, issue a warning. Indicate to our
629 caller that the option was processed successfully. */
630 if (bsearch (&positive_option,
632 (sizeof (unsupported_options)
633 / sizeof (unsupported_options[0])),
634 sizeof (unsupported_options[0]),
637 warning ("-f%s is no longer supported", p);
641 if (!strcmp (p, "handle-exceptions")
642 || !strcmp (p, "no-handle-exceptions"))
643 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
644 else if (! strcmp (p, "alt-external-templates"))
646 flag_external_templates = 1;
647 flag_alt_external_templates = 1;
648 cp_deprecated ("-falt-external-templates");
650 else if (! strcmp (p, "no-alt-external-templates"))
651 flag_alt_external_templates = 0;
652 else if (!strcmp (p, "repo"))
654 flag_use_repository = 1;
655 flag_implicit_templates = 0;
657 else if (!strcmp (p, "external-templates"))
659 flag_external_templates = 1;
660 cp_deprecated ("-fexternal-templates");
662 else if (!strcmp (p, "new-abi"))
665 flag_do_squangling = 1;
666 flag_vtable_thunks = 1;
668 else if (!strcmp (p, "no-new-abi"))
671 flag_do_squangling = 0;
673 else if ((option_value
674 = skip_leading_substring (p, "template-depth-")))
676 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
677 else if ((option_value
678 = skip_leading_substring (p, "name-mangling-version-")))
679 name_mangling_version
680 = read_integral_parameter (option_value, p - 2, name_mangling_version);
681 else if ((option_value
682 = skip_leading_substring (p, "message-length=")))
684 (read_integral_parameter (option_value, p - 2,
685 /* default line-wrap length */ 72));
686 else if ((option_value
687 = skip_leading_substring (p, "diagnostics-show-location=")))
689 if (!strcmp (option_value, "once"))
690 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_ONCE);
691 else if (!strcmp (option_value, "every-line"))
692 set_message_prefixing_rule (DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE);
694 error ("Unrecognized option `%s'", p - 2);
696 else if ((option_value
697 = skip_leading_substring (p, "dump-translation-unit-")))
700 error ("no file specified with -fdump-translation-unit");
702 flag_dump_translation_unit = option_value;
709 !found && j < (sizeof (lang_f_options)
710 / sizeof (lang_f_options[0]));
713 if (!strcmp (p, lang_f_options[j].string))
715 *lang_f_options[j].variable = lang_f_options[j].on_value;
716 /* A goto here would be cleaner,
717 but breaks the vax pcc. */
720 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
721 && ! strcmp (p+3, lang_f_options[j].string))
723 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
731 else if (p[0] == '-' && p[1] == 'W')
735 /* The -W options control the warning behavior of the compiler. */
738 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
741 if (!strcmp (p, "implicit"))
742 warn_implicit = setting;
743 else if (!strcmp (p, "long-long"))
744 warn_long_long = setting;
745 else if (!strcmp (p, "return-type"))
746 warn_return_type = setting;
747 else if (!strcmp (p, "ctor-dtor-privacy"))
748 warn_ctor_dtor_privacy = setting;
749 else if (!strcmp (p, "write-strings"))
750 warn_write_strings = setting;
751 else if (!strcmp (p, "cast-qual"))
752 warn_cast_qual = setting;
753 else if (!strcmp (p, "char-subscripts"))
754 warn_char_subscripts = setting;
755 else if (!strcmp (p, "pointer-arith"))
756 warn_pointer_arith = setting;
757 else if (!strcmp (p, "missing-prototypes"))
758 warn_missing_prototypes = setting;
759 else if (!strcmp (p, "redundant-decls"))
760 warn_redundant_decls = setting;
761 else if (!strcmp (p, "missing-braces"))
762 warn_missing_braces = setting;
763 else if (!strcmp (p, "sign-compare"))
764 warn_sign_compare = setting;
765 else if (!strcmp (p, "float-equal"))
766 warn_float_equal = setting;
767 else if (!strcmp (p, "format"))
768 warn_format = setting;
769 else if (!strcmp (p, "conversion"))
770 warn_conversion = setting;
771 else if (!strcmp (p, "parentheses"))
772 warn_parentheses = setting;
773 else if (!strcmp (p, "non-virtual-dtor"))
774 warn_nonvdtor = setting;
775 else if (!strcmp (p, "extern-inline"))
776 warn_extern_inline = setting;
777 else if (!strcmp (p, "reorder"))
778 warn_reorder = setting;
779 else if (!strcmp (p, "synth"))
780 warn_synth = setting;
781 else if (!strcmp (p, "pmf-conversions"))
782 warn_pmf2ptr = setting;
783 else if (!strcmp (p, "effc++"))
785 else if (!strcmp (p, "sign-promo"))
786 warn_sign_promo = setting;
787 else if (!strcmp (p, "old-style-cast"))
788 warn_old_style_cast = setting;
789 else if (!strcmp (p, "overloaded-virtual"))
790 warn_overloaded_virtual = setting;
791 else if (!strcmp (p, "multichar"))
792 warn_multichar = setting;
793 else if (!strcmp (p, "unknown-pragmas"))
794 /* Set to greater than 1, so that even unknown pragmas in
795 system headers will be warned about. */
796 warn_unknown_pragmas = setting * 2;
797 else if (!strcmp (p, "non-template-friend"))
798 warn_nontemplate_friend = setting;
799 else if (!strcmp (p, "deprecated"))
800 warn_deprecated = setting;
801 else if (!strcmp (p, "comment"))
802 ; /* cpp handles this one. */
803 else if (!strcmp (p, "comments"))
804 ; /* cpp handles this one. */
805 else if (!strcmp (p, "trigraphs"))
806 ; /* cpp handles this one. */
807 else if (!strcmp (p, "import"))
808 ; /* cpp handles this one. */
809 else if (!strcmp (p, "all"))
811 warn_return_type = setting;
812 set_Wunused (setting);
813 warn_implicit = setting;
814 warn_switch = setting;
815 warn_format = setting;
816 warn_parentheses = setting;
817 warn_missing_braces = setting;
818 warn_sign_compare = setting;
819 warn_multichar = setting;
820 /* We save the value of warn_uninitialized, since if they put
821 -Wuninitialized on the command line, we need to generate a
822 warning about not using it without also specifying -O. */
823 if (warn_uninitialized != 1)
824 warn_uninitialized = (setting ? 2 : 0);
825 /* Only warn about unknown pragmas that are not in system
827 warn_unknown_pragmas = 1;
829 /* C++-specific warnings. */
830 warn_ctor_dtor_privacy = setting;
831 warn_nonvdtor = setting;
832 warn_reorder = setting;
833 warn_nontemplate_friend = setting;
835 else return strings_processed;
837 else if (!strcmp (p, "-ansi"))
838 flag_no_nonansi_builtin = 1, flag_ansi = 1,
839 flag_no_gnu_keywords = 1;
841 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
842 it's probably safe to assume no sane person would ever want to use this
843 under normal circumstances. */
844 else if (!strcmp (p, "-spew-debug"))
848 return strings_processed;
853 /* Incorporate `const' and `volatile' qualifiers for member functions.
854 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
855 QUALS is a list of qualifiers. Returns any explicit
856 top-level qualifiers of the method's this pointer, anything other than
857 TYPE_UNQUALIFIED will be an extension. */
860 grok_method_quals (ctype, function, quals)
861 tree ctype, function, quals;
863 tree fntype = TREE_TYPE (function);
864 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
865 int type_quals = TYPE_UNQUALIFIED;
866 int dup_quals = TYPE_UNQUALIFIED;
867 int this_quals = TYPE_UNQUALIFIED;
871 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
873 if ((type_quals | this_quals) & tq)
875 else if (tq & TYPE_QUAL_RESTRICT)
879 quals = TREE_CHAIN (quals);
883 if (dup_quals != TYPE_UNQUALIFIED)
884 cp_error ("duplicate type qualifiers in %s declaration",
885 TREE_CODE (function) == FUNCTION_DECL
886 ? "member function" : "type");
888 ctype = cp_build_qualified_type (ctype, type_quals);
889 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
890 (TREE_CODE (fntype) == METHOD_TYPE
891 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
892 : TYPE_ARG_TYPES (fntype)));
894 fntype = build_exception_variant (fntype, raises);
896 TREE_TYPE (function) = fntype;
900 /* Warn when -fexternal-templates is used and #pragma
901 interface/implementation is not used all the times it should be,
905 warn_if_unknown_interface (decl)
908 static int already_warned = 0;
909 if (already_warned++)
912 if (flag_alt_external_templates)
914 tree til = tinst_for_decl ();
916 const char *sf = input_filename;
920 lineno = TINST_LINE (til);
921 input_filename = TINST_FILE (til);
923 cp_warning ("template `%#D' instantiated in file without #pragma interface",
929 cp_warning_at ("template `%#D' defined in file without #pragma interface",
933 /* A subroutine of the parser, to handle a component list. */
936 grok_x_components (specs)
939 struct pending_inline **p;
942 specs = strip_attrs (specs);
944 check_tag_decl (specs);
945 t = groktypename (build_decl_list (specs, NULL_TREE));
947 /* The only case where we need to do anything additional here is an
948 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
949 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
952 fixup_anonymous_aggr (t);
953 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
955 /* Ignore any inline function definitions in the anonymous union
956 since an anonymous union may not have function members. */
957 p = &pending_inlines;
958 for (; *p; *p = (*p)->next)
959 if (DECL_CONTEXT ((*p)->fndecl) != t)
963 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
967 build_artificial_parm (name, type)
973 parm = build_decl (PARM_DECL, name, type);
974 SET_DECL_ARTIFICIAL (parm);
975 DECL_ARG_TYPE (parm) = type;
979 /* Constructors for types with virtual baseclasses need an "in-charge" flag
980 saying whether this constructor is responsible for initialization of
981 virtual baseclasses or not. All destructors also need this "in-charge"
982 flag, which additionally determines whether or not the destructor should
983 free the memory for the object.
985 This function adds the "in-charge" flag to member function FN if
986 appropriate. It is called from grokclassfn and tsubst.
987 FN must be either a constructor or destructor. */
990 maybe_retrofit_in_chrg (fn)
993 tree basetype, arg_types, parms, parm, fntype;
995 /* If we've already add the in-charge parameter don't do it again. */
996 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
999 /* When processing templates we can't know, in general, whether or
1000 not we're going to have virtual baseclasses. */
1001 if (uses_template_parms (fn))
1004 /* We don't need an in-charge parameter for constructors that don't
1005 have virtual bases. */
1006 if (DECL_CONSTRUCTOR_P (fn)
1007 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1010 /* First add it to DECL_ARGUMENTS... */
1011 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
1012 TREE_READONLY (parm) = 1;
1013 parms = DECL_ARGUMENTS (fn);
1014 TREE_CHAIN (parm) = TREE_CHAIN (parms);
1015 TREE_CHAIN (parms) = parm;
1017 /* ...and then to TYPE_ARG_TYPES. */
1018 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1019 basetype = TREE_TYPE (TREE_VALUE (arg_types));
1020 arg_types = hash_tree_chain (integer_type_node, TREE_CHAIN (arg_types));
1021 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
1023 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1024 fntype = build_exception_variant (fntype,
1025 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
1026 TREE_TYPE (fn) = fntype;
1028 /* Now we've got the in-charge parameter. */
1029 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
1031 /* If this is a subobject constructor or destructor, our caller will
1032 pass us a pointer to our VTT. */
1033 if (flag_new_abi && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
1035 DECL_VTT_PARM (fn) = build_artificial_parm (vtt_parm_identifier,
1037 DECL_CONTEXT (DECL_VTT_PARM (fn)) = fn;
1038 DECL_USE_VTT_PARM (fn) = build_artificial_parm (NULL_TREE,
1040 DECL_CONTEXT (DECL_USE_VTT_PARM (fn)) = fn;
1044 /* Classes overload their constituent function names automatically.
1045 When a function name is declared in a record structure,
1046 its name is changed to it overloaded name. Since names for
1047 constructors and destructors can conflict, we place a leading
1048 '$' for destructors.
1050 CNAME is the name of the class we are grokking for.
1052 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
1054 FLAGS contains bits saying what's special about today's
1055 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
1057 If FUNCTION is a destructor, then we must add the `auto-delete' field
1058 as a second parameter. There is some hair associated with the fact
1059 that we must "declare" this variable in the manner consistent with the
1060 way the rest of the arguments were declared.
1062 QUALS are the qualifiers for the this pointer. */
1065 grokclassfn (ctype, function, flags, quals)
1066 tree ctype, function;
1067 enum overload_flags flags;
1070 tree fn_name = DECL_NAME (function);
1071 int this_quals = TYPE_UNQUALIFIED;
1073 /* Even within an `extern "C"' block, members get C++ linkage. See
1074 [dcl.link] for details. */
1075 DECL_LANGUAGE (function) = lang_cplusplus;
1077 if (fn_name == NULL_TREE)
1079 error ("name missing for member function");
1080 fn_name = get_identifier ("<anonymous>");
1081 DECL_NAME (function) = fn_name;
1085 this_quals = grok_method_quals (ctype, function, quals);
1087 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1089 /* Must add the class instance variable up front. */
1090 /* Right now we just make this a pointer. But later
1091 we may wish to make it special. */
1092 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
1096 /* The `this' parameter is implicitly `const'; it cannot be
1098 this_quals |= TYPE_QUAL_CONST;
1099 qual_type = cp_build_qualified_type (type, this_quals);
1100 parm = build_artificial_parm (this_identifier, qual_type);
1101 c_apply_type_quals_to_decl (this_quals, parm);
1103 /* We can make this a register, so long as we don't
1104 accidentally complain if someone tries to take its address. */
1105 DECL_REGISTER (parm) = 1;
1106 TREE_CHAIN (parm) = last_function_parms;
1107 last_function_parms = parm;
1110 DECL_ARGUMENTS (function) = last_function_parms;
1111 DECL_CONTEXT (function) = ctype;
1113 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1114 maybe_retrofit_in_chrg (function);
1116 if (flags == DTOR_FLAG)
1118 DECL_DESTRUCTOR_P (function) = 1;
1121 set_mangled_name_for_decl (function);
1123 DECL_ASSEMBLER_NAME (function) = build_destructor_name (ctype);
1125 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1128 set_mangled_name_for_decl (function);
1131 /* Work on the expr used by alignof (this is only called by the parser). */
1140 if (processing_template_decl)
1141 return build_min (ALIGNOF_EXPR, sizetype, expr);
1143 if (TREE_CODE (expr) == COMPONENT_REF
1144 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1145 error ("`__alignof__' applied to a bit-field");
1147 if (TREE_CODE (expr) == INDIRECT_REF)
1149 best = t = TREE_OPERAND (expr, 0);
1150 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1152 while (TREE_CODE (t) == NOP_EXPR
1153 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1156 t = TREE_OPERAND (t, 0);
1157 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1158 if (thisalign > bestalign)
1159 best = t, bestalign = thisalign;
1161 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1164 return c_alignof (TREE_TYPE (expr));
1167 /* Create an ARRAY_REF, checking for the user doing things backwards
1171 grok_array_decl (array_expr, index_exp)
1172 tree array_expr, index_exp;
1174 tree type = TREE_TYPE (array_expr);
1175 tree p1, p2, i1, i2;
1177 if (type == error_mark_node || index_exp == error_mark_node)
1178 return error_mark_node;
1179 if (processing_template_decl)
1180 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1181 array_expr, index_exp);
1183 if (type == NULL_TREE)
1185 /* Something has gone very wrong. Assume we are mistakenly reducing
1186 an expression instead of a declaration. */
1187 error ("parser may be lost: is there a '{' missing somewhere?");
1191 if (TREE_CODE (type) == OFFSET_TYPE
1192 || TREE_CODE (type) == REFERENCE_TYPE)
1193 type = TREE_TYPE (type);
1195 /* If they have an `operator[]', use that. */
1196 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1197 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1198 array_expr, index_exp, NULL_TREE);
1200 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1201 is a little-known fact that, if `a' is an array and `i' is an
1202 int, you can write `i[a]', which means the same thing as `a[i]'. */
1204 if (TREE_CODE (type) == ARRAY_TYPE)
1207 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1209 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1212 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1214 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1215 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1217 if ((p1 && i2) && (i1 && p2))
1218 error ("ambiguous conversion for array subscript");
1221 array_expr = p1, index_exp = i2;
1223 array_expr = p2, index_exp = i1;
1226 cp_error ("invalid types `%T[%T]' for array subscript",
1227 type, TREE_TYPE (index_exp));
1228 return error_mark_node;
1231 if (array_expr == error_mark_node || index_exp == error_mark_node)
1232 error ("ambiguous conversion for array subscript");
1234 return build_array_ref (array_expr, index_exp);
1237 /* Given the cast expression EXP, checking out its validity. Either return
1238 an error_mark_node if there was an unavoidable error, return a cast to
1239 void for trying to delete a pointer w/ the value 0, or return the
1240 call to delete. If DOING_VEC is 1, we handle things differently
1241 for doing an array delete. If DOING_VEC is 2, they gave us the
1242 array size as an argument to delete.
1243 Implements ARM $5.3.4. This is called from the parser. */
1246 delete_sanity (exp, size, doing_vec, use_global_delete)
1248 int doing_vec, use_global_delete;
1251 /* For a regular vector delete (aka, no size argument) we will pass
1252 this down as a NULL_TREE into build_vec_delete. */
1253 tree maxindex = NULL_TREE;
1255 if (exp == error_mark_node)
1258 if (processing_template_decl)
1260 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1261 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1262 DELETE_EXPR_USE_VEC (t) = doing_vec;
1266 if (TREE_CODE (exp) == OFFSET_REF)
1267 exp = resolve_offset_ref (exp);
1268 exp = convert_from_reference (exp);
1269 t = stabilize_reference (exp);
1270 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1272 if (t == NULL_TREE || t == error_mark_node)
1274 cp_error ("type `%#T' argument given to `delete', expected pointer",
1276 return error_mark_node;
1281 maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node);
1282 pedwarn ("anachronistic use of array size in vector delete");
1285 type = TREE_TYPE (t);
1287 /* As of Valley Forge, you can delete a pointer to const. */
1289 /* You can't delete functions. */
1290 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1292 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1293 return error_mark_node;
1296 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1297 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1298 cp_warning ("`%T' is not a pointer-to-object type", type);
1300 /* An array can't have been allocated by new, so complain. */
1301 if (TREE_CODE (t) == ADDR_EXPR
1302 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1303 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1304 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1306 /* Deleting a pointer with the value zero is valid and has no effect. */
1307 if (integer_zerop (t))
1308 return build1 (NOP_EXPR, void_type_node, t);
1311 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1315 if (IS_AGGR_TYPE (TREE_TYPE (type))
1316 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1318 /* Only do access checking here; we'll be calling op delete
1319 from the destructor. */
1320 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1321 LOOKUP_NORMAL, NULL_TREE);
1322 if (tmp == error_mark_node)
1323 return error_mark_node;
1326 return build_delete (type, t, sfk_deleting_destructor,
1327 LOOKUP_NORMAL, use_global_delete);
1331 /* Report an error if the indicated template declaration is not the
1332 sort of thing that should be a member template. */
1335 check_member_template (tmpl)
1340 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1341 decl = DECL_TEMPLATE_RESULT (tmpl);
1343 if (TREE_CODE (decl) == FUNCTION_DECL
1344 || (TREE_CODE (decl) == TYPE_DECL
1345 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1347 if (current_function_decl)
1348 /* 14.5.2.2 [temp.mem]
1350 A local class shall not have member templates. */
1351 cp_error ("invalid declaration of member template `%#D' in local class",
1354 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1356 /* 14.5.2.3 [temp.mem]
1358 A member function template shall not be virtual. */
1360 ("invalid use of `virtual' in template declaration of `%#D'",
1362 DECL_VIRTUAL_P (decl) = 0;
1365 /* The debug-information generating code doesn't know what to do
1366 with member templates. */
1367 DECL_IGNORED_P (tmpl) = 1;
1370 cp_error ("template declaration of `%#D'", decl);
1373 /* Return true iff TYPE is a valid Java parameter or return type. */
1376 acceptable_java_type (type)
1379 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1381 if (TREE_CODE (type) == POINTER_TYPE)
1383 type = TREE_TYPE (type);
1384 if (TREE_CODE (type) == RECORD_TYPE)
1387 if (! TYPE_FOR_JAVA (type))
1389 if (! CLASSTYPE_TEMPLATE_INFO (type))
1391 args = CLASSTYPE_TI_ARGS (type);
1392 i = TREE_VEC_LENGTH (args);
1395 type = TREE_VEC_ELT (args, i);
1396 if (TREE_CODE (type) == POINTER_TYPE)
1397 type = TREE_TYPE (type);
1398 if (! TYPE_FOR_JAVA (type))
1407 /* For a METHOD in a Java class CTYPE, return 1 if
1408 the parameter and return types are valid Java types.
1409 Otherwise, print appropriate error messages, and return 0. */
1412 check_java_method (method)
1416 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1417 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1418 if (! acceptable_java_type (ret_type))
1420 cp_error ("Java method '%D' has non-Java return type `%T'",
1424 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1426 tree type = TREE_VALUE (arg_types);
1427 if (! acceptable_java_type (type))
1429 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1434 return jerr ? 0 : 1;
1437 /* Sanity check: report error if this function FUNCTION is not
1438 really a member of the class (CTYPE) it is supposed to belong to.
1439 CNAME is the same here as it is for grokclassfn above. */
1442 check_classfn (ctype, function)
1443 tree ctype, function;
1445 tree fn_name = DECL_NAME (function);
1446 tree fndecl, fndecls;
1447 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1451 if (DECL_USE_TEMPLATE (function)
1452 && !(TREE_CODE (function) == TEMPLATE_DECL
1453 && DECL_TEMPLATE_SPECIALIZATION (function))
1454 && is_member_template (DECL_TI_TEMPLATE (function)))
1455 /* Since this is a specialization of a member template,
1456 we're not going to find the declaration in the class.
1459 struct S { template <typename T> void f(T); };
1460 template <> void S::f(int);
1462 we're not going to find `S::f(int)', but there's no
1463 reason we should, either. We let our callers know we didn't
1464 find the method, but we don't complain. */
1467 if (method_vec != 0)
1469 methods = &TREE_VEC_ELT (method_vec, 0);
1470 end = TREE_VEC_END (method_vec);
1472 /* First suss out ctors and dtors. */
1473 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1474 && DECL_CONSTRUCTOR_P (function))
1476 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1477 && DECL_DESTRUCTOR_P (function))
1480 while (++methods != end && *methods)
1483 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1486 for (fndecls = *methods; fndecls != NULL_TREE;
1487 fndecls = OVL_NEXT (fndecls))
1489 fndecl = OVL_CURRENT (fndecls);
1490 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL, or
1491 for a for member function of a template class, is
1492 not mangled, so the check below does not work
1493 correctly in that case. Since mangled destructor
1494 names do not include the type of the arguments,
1495 we can't use this short-cut for them, either.
1496 (It's not legal to declare arguments for a
1497 destructor, but some people try.) */
1498 if (!DECL_DESTRUCTOR_P (function)
1499 && (DECL_ASSEMBLER_NAME (function)
1500 != DECL_NAME (function))
1501 && (DECL_ASSEMBLER_NAME (fndecl)
1502 != DECL_NAME (fndecl))
1503 && (DECL_ASSEMBLER_NAME (function)
1504 == DECL_ASSEMBLER_NAME (fndecl)))
1507 /* We cannot simply call decls_match because this
1508 doesn't work for static member functions that are
1509 pretending to be methods, and because the name
1510 may have been changed by asm("new_name"). */
1511 if (DECL_NAME (function) == DECL_NAME (fndecl))
1513 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1514 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1516 /* Get rid of the this parameter on functions that become
1518 if (DECL_STATIC_FUNCTION_P (fndecl)
1519 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1520 p1 = TREE_CHAIN (p1);
1522 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1523 TREE_TYPE (TREE_TYPE (fndecl)))
1524 && compparms (p1, p2)
1525 && (DECL_TEMPLATE_SPECIALIZATION (function)
1526 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1527 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1528 || (DECL_TI_TEMPLATE (function)
1529 == DECL_TI_TEMPLATE (fndecl))))
1538 if (methods != end && *methods)
1540 tree fndecl = *methods;
1541 cp_error ("prototype for `%#D' does not match any in class `%T'",
1543 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1544 OVL_CURRENT (fndecl));
1545 while (fndecl = OVL_NEXT (fndecl), fndecl)
1546 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1551 if (!COMPLETE_TYPE_P (ctype))
1552 incomplete_type_error (function, ctype);
1554 cp_error ("no `%#D' member function declared in class `%T'",
1558 /* If we did not find the method in the class, add it to avoid
1559 spurious errors (unless the CTYPE is not yet defined, in which
1560 case we'll only confuse ourselves when the function is declared
1561 properly within the class. */
1562 if (COMPLETE_TYPE_P (ctype))
1563 add_method (ctype, methods, function);
1567 /* We have just processed the DECL, which is a static data member.
1568 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1569 present, is the assembly-language name for the data member.
1570 FLAGS is as for cp_finish_decl. */
1573 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1579 const char *asmspec = 0;
1582 asmspec = TREE_STRING_POINTER (asmspec_tree);
1584 my_friendly_assert (TREE_PUBLIC (decl), 0);
1586 DECL_CONTEXT (decl) = current_class_type;
1588 /* We cannot call pushdecl here, because that would fill in the
1589 decl of our TREE_CHAIN. Instead, we modify cp_finish_decl to do
1590 the right thing, namely, to put this decl out straight away. */
1591 /* current_class_type can be NULL_TREE in case of error. */
1592 if (!asmspec && current_class_type)
1594 DECL_INITIAL (decl) = error_mark_node;
1596 DECL_ASSEMBLER_NAME (decl) = mangle_decl (decl);
1598 DECL_ASSEMBLER_NAME (decl)
1599 = build_static_name (current_class_type, DECL_NAME (decl));
1601 if (! processing_template_decl)
1603 if (!pending_statics)
1604 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1605 VARRAY_PUSH_TREE (pending_statics, decl);
1608 /* Static consts need not be initialized in the class definition. */
1609 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1611 static int explanation = 0;
1613 error ("initializer invalid for static member with constructor");
1614 if (explanation++ == 0)
1615 error ("(you really want to initialize it separately)");
1618 /* Force the compiler to know when an uninitialized static const
1619 member is being used. */
1620 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1621 TREE_USED (decl) = 1;
1622 DECL_INITIAL (decl) = init;
1623 DECL_IN_AGGR_P (decl) = 1;
1625 cp_finish_decl (decl, init, asmspec_tree, flags);
1628 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1629 of a structure component, returning a FIELD_DECL node.
1630 QUALS is a list of type qualifiers for this decl (such as for declaring
1631 const member functions).
1633 This is done during the parsing of the struct declaration.
1634 The FIELD_DECL nodes are chained together and the lot of them
1635 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1639 If class A defines that certain functions in class B are friends, then
1640 the way I have set things up, it is B who is interested in permission
1641 granted by A. However, it is in A's context that these declarations
1642 are parsed. By returning a void_type_node, class A does not attempt
1643 to incorporate the declarations of the friends within its structure.
1645 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1646 CHANGES TO CODE IN `start_method'. */
1649 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1650 tree declarator, declspecs, init, asmspec_tree, attrlist;
1652 register tree value;
1653 const char *asmspec = 0;
1654 int flags = LOOKUP_ONLYCONVERTING;
1656 /* Convert () initializers to = initializers. */
1657 if (init == NULL_TREE && declarator != NULL_TREE
1658 && TREE_CODE (declarator) == CALL_EXPR
1659 && TREE_OPERAND (declarator, 0)
1660 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1661 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1662 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1664 /* It's invalid to try to initialize a data member using a
1665 functional notation, e.g.:
1671 Explain that to the user. */
1672 static int explained_p;
1674 cp_error ("invalid data member initiailization");
1677 cp_error ("use `=' to initialize static data members");
1681 declarator = TREE_OPERAND (declarator, 0);
1685 if (declspecs == NULL_TREE
1686 && TREE_CODE (declarator) == SCOPE_REF
1687 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1689 /* Access declaration */
1690 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1692 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1693 pop_nested_class ();
1694 return do_class_using_decl (declarator);
1698 && TREE_CODE (init) == TREE_LIST
1699 && TREE_VALUE (init) == error_mark_node
1700 && TREE_CHAIN (init) == NULL_TREE)
1703 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, attrlist);
1704 if (! value || value == error_mark_node)
1705 /* friend or constructor went bad. */
1707 if (TREE_TYPE (value) == error_mark_node)
1708 return error_mark_node;
1710 /* Pass friendly classes back. */
1711 if (TREE_CODE (value) == VOID_TYPE)
1712 return void_type_node;
1714 if (DECL_NAME (value) != NULL_TREE
1715 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1716 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1717 cp_error ("member `%D' conflicts with virtual function table field name",
1720 /* Stash away type declarations. */
1721 if (TREE_CODE (value) == TYPE_DECL)
1723 DECL_NONLOCAL (value) = 1;
1724 DECL_CONTEXT (value) = current_class_type;
1726 /* Now that we've updated the context, we need to remangle the
1727 name for this TYPE_DECL. */
1728 DECL_ASSEMBLER_NAME (value) = DECL_NAME (value);
1729 if (!uses_template_parms (value))
1732 DECL_ASSEMBLER_NAME (value) = mangle_type (TREE_TYPE (value));
1734 DECL_ASSEMBLER_NAME (value) =
1735 get_identifier (build_overload_name (TREE_TYPE (value), 1, 1));
1738 if (processing_template_decl)
1739 value = push_template_decl (value);
1744 if (DECL_IN_AGGR_P (value))
1746 cp_error ("`%D' is already defined in `%T'", value,
1747 DECL_CONTEXT (value));
1748 return void_type_node;
1752 asmspec = TREE_STRING_POINTER (asmspec_tree);
1756 if (TREE_CODE (value) == FUNCTION_DECL)
1758 grok_function_init (value, init);
1761 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1762 /* Already complained in grokdeclarator. */
1766 /* We allow initializers to become parameters to base
1768 if (TREE_CODE (init) == TREE_LIST)
1770 if (TREE_CHAIN (init) == NULL_TREE)
1771 init = TREE_VALUE (init);
1773 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1776 if (TREE_CODE (init) == CONST_DECL)
1777 init = DECL_INITIAL (init);
1778 else if (TREE_READONLY_DECL_P (init))
1779 init = decl_constant_value (init);
1780 else if (TREE_CODE (init) == CONSTRUCTOR)
1781 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1782 if (init == error_mark_node)
1783 /* We must make this look different than `error_mark_node'
1784 because `decl_const_value' would mis-interpret it
1785 as only meaning that this VAR_DECL is defined. */
1786 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1787 else if (processing_template_decl)
1789 else if (! TREE_CONSTANT (init))
1791 /* We can allow references to things that are effectively
1792 static, since references are initialized with the address. */
1793 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1794 || (TREE_STATIC (init) == 0
1795 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1797 error ("field initializer is not constant");
1798 init = error_mark_node;
1804 if (processing_template_decl && ! current_function_decl
1805 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1806 value = push_template_decl (value);
1809 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1810 TREE_VALUE (attrlist));
1812 if (TREE_CODE (value) == VAR_DECL)
1814 finish_static_data_member_decl (value, init, asmspec_tree,
1818 if (TREE_CODE (value) == FIELD_DECL)
1822 /* This must override the asm specifier which was placed
1823 by grokclassfn. Lay this out fresh. */
1824 DECL_RTL (value) = NULL_RTX;
1825 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1827 if (DECL_INITIAL (value) == error_mark_node)
1828 init = error_mark_node;
1829 cp_finish_decl (value, init, asmspec_tree, flags);
1830 DECL_INITIAL (value) = init;
1831 DECL_IN_AGGR_P (value) = 1;
1834 if (TREE_CODE (value) == FUNCTION_DECL)
1838 /* This must override the asm specifier which was placed
1839 by grokclassfn. Lay this out fresh. */
1840 DECL_RTL (value) = NULL_RTX;
1841 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1843 cp_finish_decl (value, init, asmspec_tree, flags);
1845 /* Pass friends back this way. */
1846 if (DECL_FRIEND_P (value))
1847 return void_type_node;
1849 DECL_IN_AGGR_P (value) = 1;
1852 my_friendly_abort (21);
1857 /* Like `grokfield', but for bitfields.
1858 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1861 grokbitfield (declarator, declspecs, width)
1862 tree declarator, declspecs, width;
1864 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1867 if (! value) return NULL_TREE; /* friends went bad. */
1869 /* Pass friendly classes back. */
1870 if (TREE_CODE (value) == VOID_TYPE)
1871 return void_type_node;
1873 if (TREE_CODE (value) == TYPE_DECL)
1875 cp_error ("cannot declare `%D' to be a bitfield type", value);
1879 /* Usually, finish_struct_1 catches bitifields with invalid types.
1880 But, in the case of bitfields with function type, we confuse
1881 ourselves into thinking they are member functions, so we must
1883 if (TREE_CODE (value) == FUNCTION_DECL)
1885 cp_error ("cannot declare bitfield `%D' with funcion type",
1890 if (DECL_IN_AGGR_P (value))
1892 cp_error ("`%D' is already defined in the class %T", value,
1893 DECL_CONTEXT (value));
1894 return void_type_node;
1897 GNU_xref_member (current_class_name, value);
1899 if (TREE_STATIC (value))
1901 cp_error ("static member `%D' cannot be a bitfield", value);
1904 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1906 if (width != error_mark_node)
1908 constant_expression_warning (width);
1909 DECL_INITIAL (value) = width;
1910 SET_DECL_C_BIT_FIELD (value);
1913 DECL_IN_AGGR_P (value) = 1;
1918 grokoptypename (declspecs, declarator)
1919 tree declspecs, declarator;
1921 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1923 return mangle_conv_op_name_for_type (t);
1925 return build_typename_overload (t);
1928 /* When a function is declared with an initializer,
1929 do the right thing. Currently, there are two possibilities:
1934 // initialization possibility #1.
1935 virtual void f () = 0;
1957 // initialization possibility #2
1964 copy_assignment_arg_p (parmtype, virtualp)
1966 int virtualp ATTRIBUTE_UNUSED;
1968 if (current_class_type == NULL_TREE)
1971 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1972 parmtype = TREE_TYPE (parmtype);
1974 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1976 /* Non-standard hack to support old Booch components. */
1977 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1986 grok_function_init (decl, init)
1990 /* An initializer for a function tells how this function should
1992 tree type = TREE_TYPE (decl);
1994 if (TREE_CODE (type) == FUNCTION_TYPE)
1995 cp_error ("initializer specified for non-member function `%D'", decl);
1997 /* We'll check for this in finish_struct_1. */
1998 else if (DECL_VINDEX (decl) == NULL_TREE)
1999 cp_error ("initializer specified for non-virtual member function `%D'", decl);
2001 else if (integer_zerop (init))
2004 /* Mark this function as being "defined". */
2005 DECL_INITIAL (decl) = error_mark_node;
2006 /* pure virtual destructors must be defined. */
2007 /* pure virtual needs to be defined (as abort) only when put in
2008 vtbl. For wellformed call, it should be itself. pr4737 */
2009 if (!DECL_DESTRUCTOR_P (decl)))
2011 /* Give this node rtl from `abort'. */
2012 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
2015 DECL_PURE_VIRTUAL_P (decl) = 1;
2016 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2019 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
2021 if (copy_assignment_arg_p (parmtype, 1))
2022 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
2026 cp_error ("invalid initializer for virtual method `%D'", decl);
2030 cplus_decl_attributes (decl, attributes, prefix_attributes)
2031 tree decl, attributes, prefix_attributes;
2033 if (decl == NULL_TREE || decl == void_type_node)
2036 if (TREE_CODE (decl) == TEMPLATE_DECL)
2037 decl = DECL_TEMPLATE_RESULT (decl);
2039 decl_attributes (decl, attributes, prefix_attributes);
2041 if (TREE_CODE (decl) == TYPE_DECL)
2042 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
2045 /* CONSTRUCTOR_NAME:
2046 Return the name for the constructor (or destructor) for the
2047 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2048 IDENTIFIER_NODE. When given a template, this routine doesn't
2049 lose the specialization. */
2052 constructor_name_full (thing)
2055 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
2056 || TREE_CODE (thing) == TEMPLATE_TEMPLATE_PARM
2057 || TREE_CODE (thing) == TYPENAME_TYPE)
2058 thing = TYPE_NAME (thing);
2059 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
2061 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
2062 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
2064 thing = TYPE_NAME (thing);
2066 if (TREE_CODE (thing) == TYPE_DECL
2067 || (TREE_CODE (thing) == TEMPLATE_DECL
2068 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
2069 thing = DECL_NAME (thing);
2070 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
2074 /* CONSTRUCTOR_NAME:
2075 Return the name for the constructor (or destructor) for the
2076 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2077 IDENTIFIER_NODE. When given a template, return the plain
2078 unspecialized name. */
2081 constructor_name (thing)
2085 thing = constructor_name_full (thing);
2086 t = IDENTIFIER_TEMPLATE (thing);
2092 /* Defer the compilation of the FN until the end of compilation. */
2098 if (DECL_DEFERRED_FN (fn))
2100 DECL_DEFERRED_FN (fn) = 1;
2102 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
2104 VARRAY_PUSH_TREE (deferred_fns, fn);
2107 /* Hand off a unique name which can be used for variable we don't really
2108 want to know about anyway, for example, the anonymous variables which
2109 are needed to make references work. Declare this thing so we can use it.
2110 The variable created will be of type TYPE.
2112 STATICP is nonzero if this variable should be static. */
2115 get_temp_name (type, staticp)
2119 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
2121 int toplev = toplevel_bindings_p ();
2123 if (toplev || staticp)
2125 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
2126 decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
2130 sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
2131 decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
2133 TREE_USED (decl) = 1;
2134 TREE_STATIC (decl) = staticp;
2135 DECL_ARTIFICIAL (decl) = 1;
2137 /* If this is a local variable, then lay out its rtl now.
2138 Otherwise, callers of this function are responsible for dealing
2139 with this variable's rtl. */
2143 my_friendly_assert (DECL_INITIAL (decl) == NULL_TREE,
2150 /* Hunts through the global anonymous union ANON_DECL, building
2151 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2152 returns a VAR_DECL whose size is the same as the size of the
2153 ANON_DECL, if one is available. */
2156 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2162 tree type = TREE_TYPE (anon_decl);
2163 tree main_decl = NULL_TREE;
2166 /* Rather than write the code to handle the non-union case,
2167 just give an error. */
2168 if (TREE_CODE (type) != UNION_TYPE)
2169 error ("anonymous struct not inside named type");
2171 for (field = TYPE_FIELDS (type);
2173 field = TREE_CHAIN (field))
2177 if (DECL_ARTIFICIAL (field))
2179 if (TREE_CODE (field) != FIELD_DECL)
2181 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2186 if (TREE_PRIVATE (field))
2187 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2188 else if (TREE_PROTECTED (field))
2189 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2191 if (DECL_NAME (field) == NULL_TREE
2192 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2194 decl = build_anon_union_vars (field, elems, static_p, external_p);
2198 else if (DECL_NAME (field) == NULL_TREE)
2202 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2203 /* tell `pushdecl' that this is not tentative. */
2204 DECL_INITIAL (decl) = error_mark_node;
2205 TREE_PUBLIC (decl) = 0;
2206 TREE_STATIC (decl) = static_p;
2207 DECL_EXTERNAL (decl) = external_p;
2208 decl = pushdecl (decl);
2209 DECL_INITIAL (decl) = NULL_TREE;
2212 /* Only write out one anon union element--choose the one that
2213 can hold them all. */
2214 if (main_decl == NULL_TREE
2215 && simple_cst_equal (DECL_SIZE (decl),
2216 DECL_SIZE (anon_decl)) == 1)
2219 /* ??? This causes there to be no debug info written out
2221 TREE_ASM_WRITTEN (decl) = 1;
2223 if (DECL_NAME (field) == NULL_TREE
2224 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2225 /* The remainder of the processing was already done in the
2229 /* If there's a cleanup to do, it belongs in the
2230 TREE_PURPOSE of the following TREE_LIST. */
2231 *elems = tree_cons (NULL_TREE, decl, *elems);
2232 TREE_TYPE (*elems) = type;
2238 /* Finish off the processing of a UNION_TYPE structure.
2239 If there are static members, then all members are
2240 static, and must be laid out together. If the
2241 union is an anonymous union, we arrange for that
2242 as well. PUBLIC_P is nonzero if this union is
2243 not declared static. */
2246 finish_anon_union (anon_union_decl)
2247 tree anon_union_decl;
2249 tree type = TREE_TYPE (anon_union_decl);
2251 int public_p = TREE_PUBLIC (anon_union_decl);
2252 int static_p = TREE_STATIC (anon_union_decl);
2253 int external_p = DECL_EXTERNAL (anon_union_decl);
2255 if (TYPE_FIELDS (type) == NULL_TREE)
2260 error ("namespace-scope anonymous aggregates must be static");
2264 main_decl = build_anon_union_vars (anon_union_decl,
2265 &DECL_ANON_UNION_ELEMS (anon_union_decl),
2266 static_p, external_p);
2268 if (main_decl == NULL_TREE)
2270 warning ("anonymous aggregate with no members");
2276 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2277 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2278 expand_anon_union_decl (anon_union_decl,
2280 DECL_ANON_UNION_ELEMS (anon_union_decl));
2283 add_decl_stmt (anon_union_decl);
2286 /* Finish processing a builtin type TYPE. It's name is NAME,
2287 its fields are in the array FIELDS. LEN is the number of elements
2288 in FIELDS minus one, or put another way, it is the maximum subscript
2291 It is given the same alignment as ALIGN_TYPE. */
2294 finish_builtin_type (type, name, fields, len, align_type)
2303 TYPE_FIELDS (type) = fields[0];
2304 for (i = 0; i < len; i++)
2306 layout_type (TREE_TYPE (fields[i]));
2307 DECL_FIELD_CONTEXT (fields[i]) = type;
2308 TREE_CHAIN (fields[i]) = fields[i+1];
2310 DECL_FIELD_CONTEXT (fields[i]) = type;
2311 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2313 #if 0 /* not yet, should get fixed properly later */
2314 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2316 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2318 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2319 layout_decl (TYPE_NAME (type), 0);
2322 /* Auxiliary functions to make type signatures for
2323 `operator new' and `operator delete' correspond to
2324 what compiler will be expecting. */
2327 coerce_new_type (type)
2332 if (TREE_CODE (type) == METHOD_TYPE)
2333 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2334 if (! same_type_p (TREE_TYPE (type), ptr_type_node))
2335 e1 = 1, error ("`operator new' must return type `void *'");
2337 /* Technically the type must be `size_t', but we may not know
2339 if (TYPE_ARG_TYPES (type) == NULL_TREE)
2340 e1 = 1, error ("`operator new' takes type `size_t' parameter");
2341 else if (! same_type_p (TREE_VALUE (TYPE_ARG_TYPES (type)), sizetype))
2342 e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2344 type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2346 type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2351 coerce_delete_type (type)
2358 tree arg_types = TYPE_ARG_TYPES (type);
2360 if (TREE_CODE (type) == METHOD_TYPE)
2362 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2363 arg_types = TREE_CHAIN (arg_types);
2366 if (TREE_TYPE (type) != void_type_node)
2367 e1 = 1, error ("`operator delete' must return type `void'");
2369 if (arg_types == NULL_TREE
2370 || ! same_type_p (TREE_VALUE (arg_types), ptr_type_node))
2371 e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2375 && TREE_CHAIN (arg_types)
2376 && TREE_CHAIN (arg_types) != void_list_node)
2378 /* Again, technically this argument must be `size_t', but again
2379 we may not know what that is. */
2380 tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2381 if (! same_type_p (t2, sizetype))
2382 e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2383 else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2386 if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2387 error ("too many arguments in declaration of `operator delete'");
2389 error ("`...' invalid in specification of `operator delete'");
2394 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2395 build_tree_list (NULL_TREE, sizetype));
2398 if (arg_types == NULL_TREE)
2399 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2401 arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2407 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2408 arg_types ? TREE_CHAIN (arg_types): NULL_TREE);
2410 type = build_function_type (void_type_node, arg_types);
2416 mark_vtable_entries (decl)
2419 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2421 for (; entries; entries = TREE_CHAIN (entries))
2426 fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2427 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2429 if (TREE_CODE (fnaddr) != ADDR_EXPR)
2430 /* This entry is an offset: a virtual base class offset, a
2431 virtual call offset, and RTTI offset, etc. */
2434 fn = TREE_OPERAND (fnaddr, 0);
2435 TREE_ADDRESSABLE (fn) = 1;
2436 if (DECL_THUNK_P (fn) && DECL_EXTERNAL (fn))
2438 DECL_EXTERNAL (fn) = 0;
2445 /* Set DECL up to have the closest approximation of "initialized common"
2446 linkage available. */
2449 comdat_linkage (decl)
2453 make_decl_one_only (decl);
2454 else if (TREE_CODE (decl) == FUNCTION_DECL || DECL_VIRTUAL_P (decl))
2455 /* We can just emit functions and vtables statically; it doesn't really
2456 matter if we have multiple copies. */
2457 TREE_PUBLIC (decl) = 0;
2460 /* Static data member template instantiations, however, cannot
2461 have multiple copies. */
2462 if (DECL_INITIAL (decl) == 0
2463 || DECL_INITIAL (decl) == error_mark_node)
2464 DECL_COMMON (decl) = 1;
2465 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2467 DECL_COMMON (decl) = 1;
2468 DECL_INITIAL (decl) = error_mark_node;
2472 /* We can't do anything useful; leave vars for explicit
2474 DECL_EXTERNAL (decl) = 1;
2475 DECL_NOT_REALLY_EXTERN (decl) = 0;
2479 if (DECL_LANG_SPECIFIC (decl))
2480 DECL_COMDAT (decl) = 1;
2483 /* For win32 we also want to put explicit instantiations in
2484 linkonce sections, so that they will be merged with implicit
2485 instantiations; otherwise we get duplicate symbol errors. */
2488 maybe_make_one_only (decl)
2491 /* We used to say that this was not necessary on targets that support weak
2492 symbols, because the implicit instantiations will defer to the explicit
2493 one. However, that's not actually the case in SVR4; a strong definition
2494 after a weak one is an error. Also, not making explicit
2495 instantiations one_only means that we can end up with two copies of
2496 some template instantiations. */
2497 if (! supports_one_only ())
2500 /* We can't set DECL_COMDAT on functions, or finish_file will think
2501 we can get away with not emitting them if they aren't used. We need
2502 to for variables so that cp_finish_decl will update their linkage,
2503 because their DECL_INITIAL may not have been set properly yet. */
2505 make_decl_one_only (decl);
2507 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2508 DECL_COMDAT (decl) = 1;
2511 /* Returns the virtual function with which the vtable for TYPE is
2512 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2520 if (TYPE_FOR_JAVA (type)
2521 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2522 || CLASSTYPE_INTERFACE_KNOWN (type))
2525 for (method = TYPE_METHODS (type); method != NULL_TREE;
2526 method = TREE_CHAIN (method))
2527 if (DECL_VINDEX (method) != NULL_TREE
2528 && ! DECL_THIS_INLINE (method)
2529 && ! DECL_PURE_VIRTUAL_P (method))
2535 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2536 based on TYPE and other static flags.
2538 Note that anything public is tagged TREE_PUBLIC, whether
2539 it's public in this file or in another one. */
2542 import_export_vtable (decl, type, final)
2546 if (DECL_INTERFACE_KNOWN (decl))
2549 if (TYPE_FOR_JAVA (type))
2551 TREE_PUBLIC (decl) = 1;
2552 DECL_EXTERNAL (decl) = 1;
2553 DECL_INTERFACE_KNOWN (decl) = 1;
2555 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2557 TREE_PUBLIC (decl) = 1;
2558 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2559 DECL_INTERFACE_KNOWN (decl) = 1;
2563 /* We can only wait to decide if we have real non-inline virtual
2564 functions in our class, or if we come from a template. */
2566 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2567 || key_method (type));
2569 if (final || ! found)
2571 comdat_linkage (decl);
2572 DECL_EXTERNAL (decl) = 0;
2576 TREE_PUBLIC (decl) = 1;
2577 DECL_EXTERNAL (decl) = 1;
2582 /* Determine whether or not we want to specifically import or export CTYPE,
2583 using various heuristics. */
2586 import_export_class (ctype)
2589 /* -1 for imported, 1 for exported. */
2590 int import_export = 0;
2592 /* It only makes sense to call this function at EOF. The reason is
2593 that this function looks at whether or not the first non-inline
2594 non-abstract virtual member function has been defined in this
2595 translation unit. But, we can't possibly know that until we've
2596 seen the entire translation unit. */
2597 my_friendly_assert (at_eof, 20000226);
2599 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2602 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2603 we will have CLASSTYPE_INTERFACE_ONLY set but not
2604 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2605 heuristic because someone will supply a #pragma implementation
2606 elsewhere, and deducing it here would produce a conflict. */
2607 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2610 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2611 /* FIXME this should really use some sort of target-independent macro. */
2612 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2614 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2618 /* If we got -fno-implicit-templates, we import template classes that
2619 weren't explicitly instantiated. */
2620 if (import_export == 0
2621 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2622 && ! flag_implicit_templates)
2625 /* Base our import/export status on that of the first non-inline,
2626 non-pure virtual function, if any. */
2627 if (import_export == 0
2628 && TYPE_POLYMORPHIC_P (ctype))
2630 tree method = key_method (ctype);
2632 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2635 #ifdef MULTIPLE_SYMBOL_SPACES
2636 if (import_export == -1)
2642 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2643 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = (import_export > 0);
2644 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2648 /* We need to describe to the assembler the relationship between
2649 a vtable and the vtable of the parent class. */
2652 output_vtable_inherit (vars)
2658 op[0] = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2660 parent = binfo_for_vtable (vars);
2662 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2666 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2667 op[1] = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2670 my_friendly_abort (980826);
2672 output_asm_insn (".vtable_inherit %c0, %c1", op);
2676 finish_vtable_vardecl (t, data)
2678 void *data ATTRIBUTE_UNUSED;
2681 tree ctype = DECL_CONTEXT (vars);
2682 import_export_class (ctype);
2683 import_export_vtable (vars, ctype, 1);
2685 if (! DECL_EXTERNAL (vars)
2686 && DECL_NEEDED_P (vars)
2687 && ! TREE_ASM_WRITTEN (vars))
2689 if (TREE_TYPE (vars) == void_type_node)
2690 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2694 mark_vtable_entries (vars);
2695 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2696 store_init_value (vars, DECL_INITIAL (vars));
2698 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2700 /* Mark the VAR_DECL node representing the vtable itself as a
2701 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2702 It is rather important that such things be ignored because
2703 any effort to actually generate DWARF for them will run
2704 into trouble when/if we encounter code like:
2707 struct S { virtual void member (); };
2709 because the artificial declaration of the vtable itself (as
2710 manufactured by the g++ front end) will say that the vtable
2711 is a static member of `S' but only *after* the debug output
2712 for the definition of `S' has already been output. This causes
2713 grief because the DWARF entry for the definition of the vtable
2714 will try to refer back to an earlier *declaration* of the
2715 vtable as a static member of `S' and there won't be one.
2716 We might be able to arrange to have the "vtable static member"
2717 attached to the member list for `S' before the debug info for
2718 `S' get written (which would solve the problem) but that would
2719 require more intrusive changes to the g++ front end. */
2721 DECL_IGNORED_P (vars) = 1;
2724 /* Always make vtables weak. */
2726 comdat_linkage (vars);
2728 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2731 output_vtable_inherit (vars);
2733 /* Because we're only doing syntax-checking, we'll never end up
2734 actually marking the variable as written. */
2735 if (flag_syntax_only)
2736 TREE_ASM_WRITTEN (vars) = 1;
2738 /* Since we're writing out the vtable here, also write the debug
2740 note_debug_info_needed (ctype);
2745 /* If the references to this class' vtables were optimized away, still
2746 emit the appropriate debugging information. See dfs_debug_mark. */
2747 if (DECL_COMDAT (vars)
2748 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2749 note_debug_info_needed (ctype);
2755 prune_vtable_vardecl (t, data)
2757 void *data ATTRIBUTE_UNUSED;
2759 *t = TREE_CHAIN (*t);
2763 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2764 inline function or template instantiation at end-of-file. */
2767 import_export_decl (decl)
2770 if (DECL_INTERFACE_KNOWN (decl))
2773 if (DECL_TEMPLATE_INSTANTIATION (decl)
2774 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2776 DECL_NOT_REALLY_EXTERN (decl) = 1;
2777 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2778 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2779 && (flag_implicit_templates
2780 || (flag_implicit_inline_templates && DECL_THIS_INLINE (decl))))
2782 if (!TREE_PUBLIC (decl))
2783 /* Templates are allowed to have internal linkage. See
2787 comdat_linkage (decl);
2790 DECL_NOT_REALLY_EXTERN (decl) = 0;
2792 else if (DECL_FUNCTION_MEMBER_P (decl))
2794 tree ctype = DECL_CONTEXT (decl);
2795 import_export_class (ctype);
2796 if (CLASSTYPE_INTERFACE_KNOWN (ctype)
2798 ? (! DECL_THIS_INLINE (decl))
2799 : (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))))
2801 DECL_NOT_REALLY_EXTERN (decl)
2802 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2803 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2804 && !DECL_VINDEX (decl)));
2806 /* Always make artificials weak. */
2807 if (DECL_ARTIFICIAL (decl) && flag_weak)
2808 comdat_linkage (decl);
2810 maybe_make_one_only (decl);
2813 comdat_linkage (decl);
2815 else if (DECL_TINFO_FN_P (decl))
2817 tree ctype = TREE_TYPE (DECL_NAME (decl));
2819 if (IS_AGGR_TYPE (ctype))
2820 import_export_class (ctype);
2822 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2823 && TYPE_POLYMORPHIC_P (ctype)
2824 /* If -fno-rtti, we're not necessarily emitting this stuff with
2825 the class, so go ahead and emit it now. This can happen
2826 when a class is used in exception handling. */
2828 /* If the type is a cv-qualified variant of a type, then we
2829 must emit the tinfo function in this translation unit
2830 since it will not be emitted when the vtable for the type
2831 is output (which is when the unqualified version is
2833 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2835 DECL_NOT_REALLY_EXTERN (decl)
2836 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2837 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2838 && !DECL_VINDEX (decl)));
2840 /* Always make artificials weak. */
2842 comdat_linkage (decl);
2844 else if (TYPE_BUILT_IN (ctype)
2845 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2846 DECL_NOT_REALLY_EXTERN (decl) = 0;
2848 comdat_linkage (decl);
2851 comdat_linkage (decl);
2853 DECL_INTERFACE_KNOWN (decl) = 1;
2857 build_cleanup (decl)
2861 tree type = TREE_TYPE (decl);
2863 if (TREE_CODE (type) == ARRAY_TYPE)
2867 mark_addressable (decl);
2868 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2870 temp = build_delete (TREE_TYPE (temp), temp,
2871 sfk_complete_destructor,
2872 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2876 /* Returns the initialization guard variable for the non-local
2887 sname = get_id_2 ("__sn", DECL_ASSEMBLER_NAME (decl));
2889 sname = mangle_guard_variable (decl);
2891 /* For struct X foo __attribute__((weak)), there is a counter
2892 __snfoo. Since base is already an assembler name, sname should
2893 be globally unique */
2894 sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2897 sentry = build_decl (VAR_DECL, sname, integer_type_node);
2898 TREE_PUBLIC (sentry) = 1;
2899 DECL_ARTIFICIAL (sentry) = 1;
2900 TREE_STATIC (sentry) = 1;
2901 TREE_USED (sentry) = 1;
2902 DECL_COMMON (sentry) = 1;
2903 pushdecl_top_level (sentry);
2904 cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0);
2909 /* Start the process of running a particular set of global constructors
2910 or destructors. Subroutine of do_[cd]tors. */
2913 start_objects (method_type, initp)
2914 int method_type, initp;
2920 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2922 if (initp != DEFAULT_INIT_PRIORITY)
2932 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2935 sprintf (type, "%c", method_type);
2937 fnname = get_file_function_name_long (type);
2939 start_function (void_list_node,
2940 make_call_declarator (fnname, void_list_node, NULL_TREE,
2942 NULL_TREE, SF_DEFAULT);
2944 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2945 /* It can be a static function as long as collect2 does not have
2946 to scan the object file to find its ctor/dtor routine. */
2947 TREE_PUBLIC (current_function_decl) = 0;
2950 /* Mark this declaration as used to avoid spurious warnings. */
2951 TREE_USED (current_function_decl) = 1;
2953 /* Mark this function as a global constructor or destructor. */
2954 if (method_type == 'I')
2955 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2957 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2958 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2960 body = begin_compound_stmt (/*has_no_scope=*/0);
2962 /* We cannot allow these functions to be elided, even if they do not
2963 have external linkage. And, there's no point in deferring
2964 copmilation of thes functions; they're all going to have to be
2966 current_function_cannot_inline
2967 = "static constructors and destructors cannot be inlined";
2972 /* Finish the process of running a particular set of global constructors
2973 or destructors. Subroutine of do_[cd]tors. */
2976 finish_objects (method_type, initp, body)
2977 int method_type, initp;
2984 finish_compound_stmt(/*has_no_scope=*/0, body);
2985 fn = finish_function (0);
2988 /* When only doing semantic analysis, and no RTL generation, we
2989 can't call functions that directly emit assembly code; there is
2990 no assembly file in which to put the code. */
2991 if (flag_syntax_only)
2994 fnname = XSTR (XEXP (DECL_RTL (fn), 0), 0);
2995 if (initp == DEFAULT_INIT_PRIORITY)
2997 if (method_type == 'I')
2998 assemble_constructor (fnname);
3000 assemble_destructor (fnname);
3002 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
3003 /* If we're using init priority we can't use assemble_*tor, but on ELF
3004 targets we can stick the references into named sections for GNU ld
3009 sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
3010 /* invert the numbering so the linker puts us in the proper
3011 order; constructors are run from right to left, and the
3012 linker sorts in increasing order. */
3013 MAX_INIT_PRIORITY - initp);
3014 named_section (NULL_TREE, buf, 0);
3015 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
3016 POINTER_SIZE / BITS_PER_UNIT, 1);
3021 /* The names of the parameters to the function created to handle
3022 initializations and destructions for objects with static storage
3024 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3025 #define PRIORITY_IDENTIFIER "__priority"
3027 /* The name of the function we create to handle initializations and
3028 destructions for objects with static storage duration. */
3029 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3031 /* The declaration for the __INITIALIZE_P argument. */
3032 static tree initialize_p_decl;
3034 /* The declaration for the __PRIORITY argument. */
3035 static tree priority_decl;
3037 /* The declaration for the static storage duration function. */
3038 static tree ssdf_decl;
3040 /* All the static storage duration functions created in this
3041 translation unit. */
3042 static varray_type ssdf_decls;
3044 /* A map from priority levels to information about that priority
3045 level. There may be many such levels, so efficient lookup is
3047 static splay_tree priority_info_map;
3049 /* Begins the generation of the function that will handle all
3050 initialization and destruction of objects with static storage
3051 duration. The function generated takes two parameters of type
3052 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3053 non-zero, it performs initializations. Otherwise, it performs
3054 destructions. It only performs those initializations or
3055 destructions with the indicated __PRIORITY. The generated function
3058 It is assumed that this function will only be called once per
3059 translation unit. */
3062 start_static_storage_duration_function ()
3064 static unsigned ssdf_number;
3069 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3071 /* Create the identifier for this function. It will be of the form
3072 SSDF_IDENTIFIER_<number>. */
3073 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
3074 if (ssdf_number == 0)
3076 /* Overflow occurred. That means there are at least 4 billion
3077 initialization functions. */
3078 sorry ("too many initialization functions required");
3079 my_friendly_abort (19990430);
3082 /* Create the parameters. */
3083 parm_types = void_list_node;
3084 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3085 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3086 type = build_function_type (void_type_node, parm_types);
3088 /* Create the FUNCTION_DECL itself. */
3089 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3090 get_identifier (id),
3092 TREE_PUBLIC (ssdf_decl) = 0;
3093 DECL_ARTIFICIAL (ssdf_decl) = 1;
3095 /* Put this function in the list of functions to be called from the
3096 static constructors and destructors. */
3099 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
3101 /* Take this opportunity to initialize the map from priority
3102 numbers to information about that priority level. */
3103 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3104 /*delete_key_fn=*/0,
3105 /*delete_value_fn=*/
3106 (splay_tree_delete_value_fn) &free);
3108 /* We always need to generate functions for the
3109 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3110 priorities later, we'll be sure to find the
3111 DEFAULT_INIT_PRIORITY. */
3112 get_priority_info (DEFAULT_INIT_PRIORITY);
3115 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
3117 /* Create the argument list. */
3118 initialize_p_decl = build_decl (PARM_DECL,
3119 get_identifier (INITIALIZE_P_IDENTIFIER),
3121 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3122 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
3123 TREE_USED (initialize_p_decl) = 1;
3124 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
3126 DECL_CONTEXT (priority_decl) = ssdf_decl;
3127 DECL_ARG_TYPE (priority_decl) = integer_type_node;
3128 TREE_USED (priority_decl) = 1;
3130 TREE_CHAIN (initialize_p_decl) = priority_decl;
3131 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3133 /* Put the function in the global scope. */
3134 pushdecl (ssdf_decl);
3136 /* Start the function itself. This is equivalent to declarating the
3139 static void __ssdf (int __initialize_p, init __priority_p);
3141 It is static because we only need to call this function from the
3142 various constructor and destructor functions for this module. */
3143 start_function (/*specs=*/NULL_TREE,
3145 /*attrs=*/NULL_TREE,
3148 /* Set up the scope of the outermost block in the function. */
3149 body = begin_compound_stmt (/*has_no_scope=*/0);
3151 /* This function must not be deferred because we are depending on
3152 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3153 current_function_cannot_inline
3154 = "static storage duration functions cannot be inlined";
3159 /* Finish the generation of the function which performs initialization
3160 and destruction of objects with static storage duration. After
3161 this point, no more such objects can be created. */
3164 finish_static_storage_duration_function (body)
3167 /* Close out the function. */
3168 finish_compound_stmt (/*has_no_scope=*/0, body);
3169 expand_body (finish_function (0));
3172 /* Return the information about the indicated PRIORITY level. If no
3173 code to handle this level has yet been generated, generate the
3174 appropriate prologue. */
3176 static priority_info
3177 get_priority_info (priority)
3183 n = splay_tree_lookup (priority_info_map,
3184 (splay_tree_key) priority);
3187 /* Create a new priority information structure, and insert it
3189 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
3190 pi->initializations_p = 0;
3191 pi->destructions_p = 0;
3192 splay_tree_insert (priority_info_map,
3193 (splay_tree_key) priority,
3194 (splay_tree_value) pi);
3197 pi = (priority_info) n->value;
3202 /* Set up to handle the initialization or destruction of DECL. If
3203 INITP is non-zero, we are initializing the variable. Otherwise, we
3204 are destroying it. */
3207 start_static_initialization_or_destruction (decl, initp)
3211 tree sentry_if_stmt = NULL_TREE;
3217 /* Figure out the priority for this declaration. */
3218 priority = DECL_INIT_PRIORITY (decl);
3220 priority = DEFAULT_INIT_PRIORITY;
3222 /* Remember that we had an initialization or finalization at this
3224 pi = get_priority_info (priority);
3226 pi->initializations_p = 1;
3228 pi->destructions_p = 1;
3230 /* Trick the compiler into thinking we are at the file and line
3231 where DECL was declared so that error-messages make sense, and so
3232 that the debugger will show somewhat sensible file and line
3234 input_filename = DECL_SOURCE_FILE (decl);
3235 lineno = DECL_SOURCE_LINE (decl);
3241 Access control for implicit calls to the constructors,
3242 the conversion functions, or the destructor called to
3243 create and destroy a static data member is performed as
3244 if these calls appeared in the scope of the member's
3247 we pretend we are in a static member function of the class of
3248 which the DECL is a member. */
3249 if (member_p (decl))
3251 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3252 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3255 /* Conditionalize this initialization on being in the right priority
3256 and being initializing/finalizing appropriately. */
3257 sentry_if_stmt = begin_if_stmt ();
3258 cond = build_binary_op (EQ_EXPR,
3260 build_int_2 (priority, 0));
3261 init_cond = initp ? integer_one_node : integer_zero_node;
3262 init_cond = build_binary_op (EQ_EXPR,
3265 cond = build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3267 /* We need a sentry if this is an object with external linkage that
3268 might be initialized in more than one place. (For example, a
3269 static data member of a template, when the data member requires
3271 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3272 || DECL_ONE_ONLY (decl)
3273 || DECL_WEAK (decl)))
3278 sentry = get_sentry (decl);
3280 /* We do initializations only if the SENTRY is zero, i.e., if we
3281 are the first to initialize the variable. We do destructions
3282 only if the SENTRY is one, i.e., if we are the last to
3283 destroy the variable. */
3285 sentry_cond = build_binary_op (EQ_EXPR,
3286 build_unary_op (PREINCREMENT_EXPR,
3291 sentry_cond = build_binary_op (EQ_EXPR,
3292 build_unary_op (PREDECREMENT_EXPR,
3297 cond = build_binary_op (TRUTH_ANDIF_EXPR, cond, sentry_cond);
3300 finish_if_stmt_cond (cond, sentry_if_stmt);
3302 return sentry_if_stmt;
3305 /* We've just finished generating code to do an initialization or
3306 finalization. SENTRY_IF_STMT is the if-statement we used to guard
3307 the initialization. */
3310 finish_static_initialization_or_destruction (sentry_if_stmt)
3311 tree sentry_if_stmt;
3313 finish_then_clause (sentry_if_stmt);
3316 /* Now that we're done with DECL we don't need to pretend to be a
3317 member of its class any longer. */
3318 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3319 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3322 /* Generate code to do the static initialization of DECL. The
3323 initialization is INIT. If DECL may be initialized more than once
3324 in different object files, SENTRY is the guard variable to
3325 check. PRIORITY is the priority for the initialization. */
3328 do_static_initialization (decl, init)
3333 tree sentry_if_stmt;
3335 /* Set up for the initialization. */
3337 = start_static_initialization_or_destruction (decl,
3340 /* Do the initialization itself. */
3341 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3342 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3343 expr = build_aggr_init (decl, init, 0);
3344 else if (TREE_CODE (init) == TREE_VEC)
3345 expr = build_vec_init (decl, TREE_VEC_ELT (init, 0),
3346 TREE_VEC_ELT (init, 1),
3347 TREE_VEC_ELT (init, 2), 0);
3350 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3351 TREE_SIDE_EFFECTS (expr) = 1;
3353 finish_expr_stmt (expr);
3355 /* If we're using __cxa_atexit, register a a function that calls the
3356 destructor for the object. */
3357 if (flag_use_cxa_atexit)
3358 register_dtor_fn (decl);
3361 finish_static_initialization_or_destruction (sentry_if_stmt);
3364 /* Generate code to do the static destruction of DECL. If DECL may be
3365 initialized more than once in different object files, SENTRY is the
3366 guard variable to check. PRIORITY is the priority for the
3370 do_static_destruction (decl)
3373 tree sentry_if_stmt;
3375 /* If we're using __cxa_atexit, then destructors are registered
3376 immediately after objects are initialized. */
3377 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3379 /* If we don't need a destructor, there's nothing to do. */
3380 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3383 /* Actually do the destruction. */
3384 sentry_if_stmt = start_static_initialization_or_destruction (decl,
3386 finish_expr_stmt (build_cleanup (decl));
3387 finish_static_initialization_or_destruction (sentry_if_stmt);
3390 /* VARS is a list of variables with static storage duration which may
3391 need initialization and/or finalization. Remove those variables
3392 that don't really need to be initialized or finalized, and return
3393 the resulting list. The order in which the variables appear in
3394 VARS is in reverse order of the order in which they should actually
3395 be initialized. The list we return is in the unreversed order;
3396 i.e., the first variable should be initialized first. */
3399 prune_vars_needing_no_initialization (vars)
3405 for (var = vars, result = NULL_TREE;
3407 var = TREE_CHAIN (var))
3409 tree decl = TREE_VALUE (var);
3410 tree init = TREE_PURPOSE (var);
3412 /* Deal gracefully with error. */
3413 if (decl == error_mark_node)
3416 /* The only things that can be initialized are variables. */
3417 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3419 /* If this object is not defined, we don't need to do anything
3421 if (DECL_EXTERNAL (decl))
3424 /* Also, if the initializer already contains errors, we can bail
3426 if (init && TREE_CODE (init) == TREE_LIST
3427 && value_member (error_mark_node, init))
3430 /* This variable is going to need initialization and/or
3431 finalization, so we add it to the list. */
3432 result = tree_cons (init, decl, result);
3438 /* Make sure we have told the back end about all the variables in
3442 write_out_vars (vars)
3447 for (v = vars; v; v = TREE_CHAIN (v))
3448 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3449 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3452 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3453 (otherwise) that will initialize all gobal objects with static
3454 storage duration having the indicated PRIORITY. */
3457 generate_ctor_or_dtor_function (constructor_p, priority)
3466 /* We use `I' to indicate initialization and `D' to indicate
3473 /* Begin the function. */
3474 body = start_objects (function_key, priority);
3476 /* Call the static storage duration function with appropriate
3478 for (i = 0; i < ssdf_decls->elements_used; ++i)
3480 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3482 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3484 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3488 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3489 calls to any functions marked with attributes indicating that
3490 they should be called at initialization- or destruction-time. */
3491 if (priority == DEFAULT_INIT_PRIORITY)
3495 for (fns = constructor_p ? static_ctors : static_dtors;
3497 fns = TREE_CHAIN (fns))
3498 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3501 /* Close out the function. */
3502 finish_objects (function_key, priority, body);
3505 /* Generate constructor and destructor functions for the priority
3509 generate_ctor_and_dtor_functions_for_priority (n, data)
3511 void *data ATTRIBUTE_UNUSED;
3513 int priority = (int) n->key;
3514 priority_info pi = (priority_info) n->value;
3516 /* Generate the functions themselves, but only if they are really
3518 if (pi->initializations_p
3519 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3520 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3522 if (pi->destructions_p
3523 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3524 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3527 /* Keep iterating. */
3531 /* This routine is called from the last rule in yyparse ().
3532 Its job is to create all the code needed to initialize and
3533 destroy the global aggregates. We do the destruction
3534 first, since that way we only need to reverse the decls once. */
3545 /* Bad parse errors. Just forget about it. */
3546 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3549 /* Otherwise, GDB can get confused, because in only knows
3550 about source for LINENO-1 lines. */
3553 interface_unknown = 1;
3556 /* We now have to write out all the stuff we put off writing out.
3559 o Template specializations that we have not yet instantiated,
3560 but which are needed.
3561 o Initialization and destruction for non-local objects with
3562 static storage duration. (Local objects with static storage
3563 duration are initialized when their scope is first entered,
3564 and are cleaned up via atexit.)
3565 o Virtual function tables.
3567 All of these may cause others to be needed. For example,
3568 instantiating one function may cause another to be needed, and
3569 generating the intiailzer for an object may cause templates to be
3570 instantiated, etc., etc. */
3572 timevar_push (TV_VARCONST);
3574 if (new_abi_rtti_p ())
3575 emit_support_tinfos ();
3581 /* If there are templates that we've put off instantiating, do
3583 instantiate_pending_templates ();
3585 /* Write out virtual tables as required. Note that writing out
3586 the virtual table for a template class may cause the
3587 instantiation of members of that class. */
3588 if (walk_globals (vtable_decl_p,
3589 finish_vtable_vardecl,
3593 /* Write out needed type info variables. Writing out one variable
3594 might cause others to be needed. */
3595 if (new_abi_rtti_p ()
3596 && walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3599 /* The list of objects with static storage duration is built up
3600 in reverse order. We clear STATIC_AGGREGATES so that any new
3601 aggregates added during the initialization of these will be
3602 initialized in the correct order when we next come around the
3604 vars = prune_vars_needing_no_initialization (static_aggregates);
3605 static_aggregates = NULL_TREE;
3611 /* We need to start a new initialization function each time
3612 through the loop. That's because we need to know which
3613 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3614 isn't computed until a function is finished, and written
3615 out. That's a deficiency in the back-end. When this is
3616 fixed, these initialization functions could all become
3617 inline, with resulting performance improvements. */
3618 tree ssdf_body = start_static_storage_duration_function ();
3620 /* Make sure the back end knows about all the variables. */
3621 write_out_vars (vars);
3623 /* First generate code to do all the initializations. */
3624 for (v = vars; v; v = TREE_CHAIN (v))
3625 do_static_initialization (TREE_VALUE (v),
3628 /* Then, generate code to do all the destructions. Do these
3629 in reverse order so that the most recently constructed
3630 variable is the first destroyed. If we're using
3631 __cxa_atexit, then we don't need to do this; functions
3632 were registered at initialization time to destroy the
3634 if (!flag_use_cxa_atexit)
3636 vars = nreverse (vars);
3637 for (v = vars; v; v = TREE_CHAIN (v))
3638 do_static_destruction (TREE_VALUE (v));
3643 /* Finish up the static storage duration function for this
3645 finish_static_storage_duration_function (ssdf_body);
3647 /* All those initializations and finalizations might cause
3648 us to need more inline functions, more template
3649 instantiations, etc. */
3653 /* Go through the various inline functions, and see if any need
3655 for (i = 0; i < deferred_fns_used; ++i)
3657 tree decl = VARRAY_TREE (deferred_fns, i);
3658 import_export_decl (decl);
3659 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3661 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3663 /* Even though we're already at the top-level, we push
3664 there again. That way, when we pop back a few lines
3665 hence, all of our state is restored. Otherwise,
3666 finish_function doesn't clean things up, and we end
3667 up with CURRENT_FUNCTION_DECL set. */
3668 push_to_top_level ();
3669 if (DECL_TINFO_FN_P (decl))
3670 synthesize_tinfo_fn (decl);
3672 synthesize_method (decl);
3673 pop_from_top_level ();
3678 /* Mark all functions that might deal with exception-handling as
3680 mark_all_runtime_matches ();
3682 /* We lie to the back-end, pretending that some functions are
3683 not defined when they really are. This keeps these functions
3684 from being put out unnecessarily. But, we must stop lying
3685 when the functions are referenced, or if they are not comdat
3686 since they need to be put out now. */
3687 for (i = 0; i < deferred_fns_used; ++i)
3689 tree decl = VARRAY_TREE (deferred_fns, i);
3691 if (DECL_NOT_REALLY_EXTERN (decl)
3692 && DECL_INITIAL (decl)
3693 && DECL_NEEDED_P (decl))
3694 DECL_EXTERNAL (decl) = 0;
3696 /* If we're going to need to write this function out, and
3697 there's already a body for it, create RTL for it now.
3698 (There might be no body if this is a method we haven't
3699 gotten around to synthesizing yet.) */
3700 if (!DECL_EXTERNAL (decl)
3701 && DECL_NEEDED_P (decl)
3702 && DECL_SAVED_TREE (decl)
3703 && !DECL_SAVED_INSNS (decl)
3704 && !TREE_ASM_WRITTEN (decl))
3706 int saved_not_really_extern;
3708 /* When we call finish_function in expand_body, it will
3709 try to reset DECL_NOT_REALLY_EXTERN so we save and
3711 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3712 /* Generate RTL for this function now that we know we
3715 /* Undo the damage done by finish_function. */
3716 DECL_EXTERNAL (decl) = 0;
3717 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3718 /* If we're compiling -fsyntax-only pretend that this
3719 function has been written out so that we don't try to
3721 if (flag_syntax_only)
3722 TREE_ASM_WRITTEN (decl) = 1;
3727 if (deferred_fns_used
3728 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3731 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3734 /* Static data members are just like namespace-scope globals. */
3735 for (i = 0; i < pending_statics_used; ++i)
3737 tree decl = VARRAY_TREE (pending_statics, i);
3738 if (TREE_ASM_WRITTEN (decl))
3740 import_export_decl (decl);
3741 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3742 DECL_EXTERNAL (decl) = 0;
3745 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3746 pending_statics_used))
3751 /* We give C linkage to static constructors and destructors. */
3752 push_lang_context (lang_name_c);
3754 /* Generate initialization and destruction functions for all
3755 priorities for which they are required. */
3756 if (priority_info_map)
3757 splay_tree_foreach (priority_info_map,
3758 generate_ctor_and_dtor_functions_for_priority,
3761 /* We're done with the splay-tree now. */
3762 if (priority_info_map)
3763 splay_tree_delete (priority_info_map);
3765 /* We're done with static constructors, so we can go back to "C++"
3767 pop_lang_context ();
3769 /* Now delete from the chain of variables all virtual function tables.
3770 We output them all ourselves, because each will be treated
3771 specially. We don't do this if we're just doing semantic
3772 analysis, and not code-generation. */
3773 if (!flag_syntax_only)
3774 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3776 /* Now, issue warnings about static, but not defined, functions,
3777 etc., and emit debugging information. */
3778 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3779 if (pending_statics)
3780 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3781 pending_statics_used);
3785 /* The entire file is now complete. If requested, dump everything
3787 if (flag_dump_translation_unit)
3788 dump_node_to_file (global_namespace, flag_dump_translation_unit);
3790 /* If there's some tool that wants to examine the entire translation
3791 unit, let it do so now. */
3793 (*back_end_hook) (global_namespace);
3795 timevar_pop (TV_VARCONST);
3797 if (flag_detailed_statistics)
3799 dump_tree_statistics ();
3800 dump_time_statistics ();
3804 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3805 expr. Since it was parsed like a type, we need to wade through and fix
3806 that. Unfortunately, since operator() is left-associative, we can't use
3807 tail recursion. In the above example, TYPE is `A', and DECL is
3810 Maybe this shouldn't be recursive, but how often will it actually be
3814 reparse_absdcl_as_expr (type, decl)
3817 /* do build_functional_cast (type, NULL_TREE) at bottom */
3818 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3819 return build_functional_cast (type, NULL_TREE);
3822 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3824 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3826 if (TREE_CODE (decl) == CALL_EXPR
3827 && (! TREE_TYPE (decl)
3828 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3829 decl = require_complete_type (decl);
3834 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3835 out to be an expr. Since it was parsed like a type, we need to wade
3836 through and fix that. Since casts are right-associative, we are
3837 reversing the order, so we don't have to recurse.
3839 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3843 reparse_absdcl_as_casts (decl, expr)
3848 if (TREE_CODE (expr) == CONSTRUCTOR
3849 && TREE_TYPE (expr) == 0)
3851 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3852 decl = TREE_OPERAND (decl, 0);
3854 expr = digest_init (type, expr, (tree *) 0);
3855 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3857 int failure = complete_array_type (type, expr, 1);
3859 my_friendly_abort (78);
3865 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3866 decl = TREE_OPERAND (decl, 0);
3867 expr = build_c_cast (type, expr);
3870 if (warn_old_style_cast && ! in_system_header
3871 && current_lang_name != lang_name_c)
3872 warning ("use of old-style cast");
3877 /* Given plain tree nodes for an expression, build up the full semantics. */
3880 build_expr_from_tree (t)
3883 if (t == NULL_TREE || t == error_mark_node)
3886 switch (TREE_CODE (t))
3888 case IDENTIFIER_NODE:
3889 return do_identifier (t, 0, NULL_TREE);
3892 if (LOOKUP_EXPR_GLOBAL (t))
3893 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3895 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3897 case TEMPLATE_ID_EXPR:
3898 return (lookup_template_function
3899 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3900 build_expr_from_tree (TREE_OPERAND (t, 1))));
3903 return build_x_indirect_ref
3904 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3907 return build_functional_cast
3908 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3910 case REINTERPRET_CAST_EXPR:
3911 return build_reinterpret_cast
3912 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3914 case CONST_CAST_EXPR:
3915 return build_const_cast
3916 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3918 case DYNAMIC_CAST_EXPR:
3919 return build_dynamic_cast
3920 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3922 case STATIC_CAST_EXPR:
3923 return build_static_cast
3924 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3926 case PREDECREMENT_EXPR:
3927 case PREINCREMENT_EXPR:
3928 case POSTDECREMENT_EXPR:
3929 case POSTINCREMENT_EXPR:
3933 case TRUTH_NOT_EXPR:
3935 case CONVERT_EXPR: /* Unary + */
3940 return build_x_unary_op (TREE_CODE (t),
3941 build_expr_from_tree (TREE_OPERAND (t, 0)));
3946 case TRUNC_DIV_EXPR:
3948 case FLOOR_DIV_EXPR:
3949 case ROUND_DIV_EXPR:
3950 case EXACT_DIV_EXPR:
3952 case BIT_ANDTC_EXPR:
3955 case TRUNC_MOD_EXPR:
3956 case FLOOR_MOD_EXPR:
3957 case TRUTH_ANDIF_EXPR:
3958 case TRUTH_ORIF_EXPR:
3959 case TRUTH_AND_EXPR:
3974 return build_x_binary_op
3976 build_expr_from_tree (TREE_OPERAND (t, 0)),
3977 build_expr_from_tree (TREE_OPERAND (t, 1)));
3980 return build_m_component_ref
3981 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3982 build_expr_from_tree (TREE_OPERAND (t, 1)));
3985 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3988 if (TREE_OPERAND (t, 0) == NULL_TREE)
3990 return build_parse_node (ARRAY_REF, NULL_TREE,
3991 build_expr_from_tree (TREE_OPERAND (t, 1)));
3992 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3993 build_expr_from_tree (TREE_OPERAND (t, 1)));
3998 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
4001 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
4005 return build_x_modify_expr
4006 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4007 TREE_CODE (TREE_OPERAND (t, 1)),
4008 build_expr_from_tree (TREE_OPERAND (t, 2)));
4011 return build_x_arrow
4012 (build_expr_from_tree (TREE_OPERAND (t, 0)));
4016 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4017 build_expr_from_tree (TREE_OPERAND (t, 1)),
4018 build_expr_from_tree (TREE_OPERAND (t, 2)),
4019 NEW_EXPR_USE_GLOBAL (t));
4022 return delete_sanity
4023 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4024 build_expr_from_tree (TREE_OPERAND (t, 1)),
4025 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
4028 if (TREE_OPERAND (t, 1) == NULL_TREE)
4029 return build_x_compound_expr
4030 (build_expr_from_tree (TREE_OPERAND (t, 0)));
4032 my_friendly_abort (42);
4034 case METHOD_CALL_EXPR:
4035 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4037 tree ref = TREE_OPERAND (t, 0);
4038 return build_scoped_method_call
4039 (build_expr_from_tree (TREE_OPERAND (t, 1)),
4040 build_expr_from_tree (TREE_OPERAND (ref, 0)),
4041 TREE_OPERAND (ref, 1),
4042 build_expr_from_tree (TREE_OPERAND (t, 2)));
4046 tree fn = TREE_OPERAND (t, 0);
4048 /* We can get a TEMPLATE_ID_EXPR here on code like:
4052 so we must resolve that. However, we can also get things
4053 like a BIT_NOT_EXPR here, when referring to a destructor,
4054 and things like that are not correctly resolved by
4055 build_expr_from_tree. So, just use build_expr_from_tree
4056 when we really need it. */
4057 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4058 fn = lookup_template_function
4059 (TREE_OPERAND (fn, 0),
4060 build_expr_from_tree (TREE_OPERAND (fn, 1)));
4062 return build_method_call
4063 (build_expr_from_tree (TREE_OPERAND (t, 1)),
4065 build_expr_from_tree (TREE_OPERAND (t, 2)),
4066 NULL_TREE, LOOKUP_NORMAL);
4070 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4072 tree ref = TREE_OPERAND (t, 0);
4073 return build_member_call
4074 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
4075 TREE_OPERAND (ref, 1),
4076 build_expr_from_tree (TREE_OPERAND (t, 1)));
4080 tree name = TREE_OPERAND (t, 0);
4082 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
4083 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
4084 && !LOOKUP_EXPR_GLOBAL (name)
4085 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
4086 && (!current_class_type
4087 || !lookup_member (current_class_type, id, 0, 0)))
4089 /* Do Koenig lookup if there are no class members. */
4090 name = do_identifier (id, 0, args);
4092 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
4093 || ! really_overloaded_fn (name))
4094 name = build_expr_from_tree (name);
4095 return build_x_function_call (name, args, current_class_ref);
4099 return build_x_conditional_expr
4100 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4101 build_expr_from_tree (TREE_OPERAND (t, 1)),
4102 build_expr_from_tree (TREE_OPERAND (t, 2)));
4104 case PSEUDO_DTOR_EXPR:
4105 return (finish_pseudo_destructor_call_expr
4106 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4107 build_expr_from_tree (TREE_OPERAND (t, 1)),
4108 build_expr_from_tree (TREE_OPERAND (t, 2))));
4112 tree purpose, value, chain;
4114 if (t == void_list_node)
4117 purpose = TREE_PURPOSE (t);
4119 purpose = build_expr_from_tree (purpose);
4120 value = TREE_VALUE (t);
4122 value = build_expr_from_tree (value);
4123 chain = TREE_CHAIN (t);
4124 if (chain && chain != void_type_node)
4125 chain = build_expr_from_tree (chain);
4126 return tree_cons (purpose, value, chain);
4131 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
4132 tree field = TREE_OPERAND (t, 1);
4134 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4135 and `x.A::b'. We must distinguish between those cases
4137 if (TREE_CODE (field) == SCOPE_REF)
4138 return build_object_ref (object,
4139 TREE_OPERAND (field, 0),
4140 TREE_OPERAND (field, 1));
4142 return build_x_component_ref (object, field,
4147 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
4153 /* digest_init will do the wrong thing if we let it. */
4154 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
4157 r = build_nt (CONSTRUCTOR, NULL_TREE,
4158 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
4159 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
4162 return digest_init (TREE_TYPE (t), r, 0);
4167 if (TYPE_P (TREE_OPERAND (t, 0)))
4168 return get_typeid (TREE_OPERAND (t, 0));
4169 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4172 return convert_from_reference (t);
4175 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4183 /* This is something of the form `int (*a)++' that has turned out to be an
4184 expr. It was only converted into parse nodes, so we need to go through
4185 and build up the semantics. Most of the work is done by
4186 build_expr_from_tree, above.
4188 In the above example, TYPE is `int' and DECL is `*a'. */
4191 reparse_decl_as_expr (type, decl)
4194 decl = build_expr_from_tree (decl);
4196 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4201 /* This is something of the form `int (*a)' that has turned out to be a
4202 decl. It was only converted into parse nodes, so we need to do the
4203 checking that make_{pointer,reference}_declarator do. */
4206 finish_decl_parsing (decl)
4209 extern int current_class_depth;
4211 switch (TREE_CODE (decl))
4213 case IDENTIFIER_NODE:
4216 return make_pointer_declarator
4217 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4219 return make_reference_declarator
4220 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4222 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4225 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4226 TREE_COMPLEXITY (decl) = current_class_depth;
4229 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4232 /* For attribute handling. */
4233 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4236 my_friendly_abort (5);
4242 check_cp_case_value (value)
4245 if (value == NULL_TREE)
4248 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4249 STRIP_TYPE_NOPS (value);
4250 value = decl_constant_value (value);
4251 STRIP_TYPE_NOPS (value);
4252 value = fold (value);
4254 if (TREE_CODE (value) != INTEGER_CST
4255 && value != error_mark_node)
4257 cp_error ("case label `%E' does not reduce to an integer constant",
4259 value = error_mark_node;
4262 /* Promote char or short to int. */
4263 value = default_conversion (value);
4265 constant_expression_warning (value);
4270 /* Return 1 if root encloses child. */
4273 is_namespace_ancestor (root, child)
4278 if (root == global_namespace)
4280 if (child == global_namespace)
4282 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4286 /* Return the namespace that is the common ancestor
4287 of two given namespaces. */
4290 namespace_ancestor (ns1, ns2)
4293 if (is_namespace_ancestor (ns1, ns2))
4295 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4298 /* Insert used into the using list of user. Set indirect_flag if this
4299 directive is not directly from the source. Also find the common
4300 ancestor and let our users know about the new namespace */
4302 add_using_namespace (user, used, indirect)
4308 /* Using oneself is a no-op. */
4311 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4312 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4313 /* Check if we already have this. */
4314 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4318 /* Promote to direct usage. */
4319 TREE_INDIRECT_USING (t) = 0;
4323 /* Add used to the user's using list. */
4324 DECL_NAMESPACE_USING (user)
4325 = tree_cons (used, namespace_ancestor (user, used),
4326 DECL_NAMESPACE_USING (user));
4328 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4330 /* Add user to the used's users list. */
4331 DECL_NAMESPACE_USERS (used)
4332 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4334 /* Recursively add all namespaces used. */
4335 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4336 /* indirect usage */
4337 add_using_namespace (user, TREE_PURPOSE (t), 1);
4339 /* Tell everyone using us about the new used namespaces. */
4340 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4341 add_using_namespace (TREE_PURPOSE (t), used, 1);
4344 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4345 duplicates. The first list becomes the tail of the result.
4347 The algorithm is O(n^2). We could get this down to O(n log n) by
4348 doing a sort on the addresses of the functions, if that becomes
4352 merge_functions (s1, s2)
4356 for (; s2; s2 = OVL_NEXT (s2))
4358 tree fn = OVL_CURRENT (s2);
4359 if (! ovl_member (fn, s1))
4360 s1 = build_overload (fn, s1);
4365 /* This should return an error not all definitions define functions.
4366 It is not an error if we find two functions with exactly the
4367 same signature, only if these are selected in overload resolution.
4368 old is the current set of bindings, new the freshly-found binding.
4369 XXX Do we want to give *all* candidates in case of ambiguity?
4370 XXX In what way should I treat extern declarations?
4371 XXX I don't want to repeat the entire duplicate_decls here */
4374 ambiguous_decl (name, old, new, flags)
4381 my_friendly_assert (old != NULL_TREE, 393);
4382 /* Copy the value. */
4383 val = BINDING_VALUE (new);
4385 switch (TREE_CODE (val))
4388 /* If we expect types or namespaces, and not templates,
4389 or this is not a template class. */
4390 if (LOOKUP_QUALIFIERS_ONLY (flags)
4391 && !DECL_CLASS_TEMPLATE_P (val))
4395 if (LOOKUP_NAMESPACES_ONLY (flags))
4398 case NAMESPACE_DECL:
4399 if (LOOKUP_TYPES_ONLY (flags))
4403 if (LOOKUP_QUALIFIERS_ONLY (flags))
4407 if (!BINDING_VALUE (old))
4408 BINDING_VALUE (old) = val;
4409 else if (val && val != BINDING_VALUE (old))
4411 if (is_overloaded_fn (BINDING_VALUE (old))
4412 && is_overloaded_fn (val))
4414 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4419 /* Some declarations are functions, some are not. */
4420 if (flags & LOOKUP_COMPLAIN)
4422 /* If we've already given this error for this lookup,
4423 BINDING_VALUE (old) is error_mark_node, so let's not
4424 repeat ourselves. */
4425 if (BINDING_VALUE (old) != error_mark_node)
4427 cp_error ("use of `%D' is ambiguous", name);
4428 cp_error_at (" first declared as `%#D' here",
4429 BINDING_VALUE (old));
4431 cp_error_at (" also declared as `%#D' here", val);
4433 BINDING_VALUE (old) = error_mark_node;
4436 /* ... and copy the type. */
4437 type = BINDING_TYPE (new);
4438 if (LOOKUP_NAMESPACES_ONLY (flags))
4440 if (!BINDING_TYPE (old))
4441 BINDING_TYPE (old) = type;
4442 else if (type && BINDING_TYPE (old) != type)
4444 if (flags & LOOKUP_COMPLAIN)
4446 cp_error ("`%D' denotes an ambiguous type",name);
4447 cp_error_at (" first type here", BINDING_TYPE (old));
4448 cp_error_at (" other type here", type);
4454 /* Subroutine of unualified_namespace_lookup:
4455 Add the bindings of NAME in used namespaces to VAL.
4456 We are currently looking for names in namespace SCOPE, so we
4457 look through USINGS for using-directives of namespaces
4458 which have SCOPE as a common ancestor with the current scope.
4459 Returns zero on errors. */
4462 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4463 tree name, val, usings, scope;
4469 /* Iterate over all used namespaces in current, searching for using
4470 directives of scope. */
4471 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4472 if (TREE_VALUE (iter) == scope)
4475 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4477 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4478 /* Resolve ambiguities. */
4479 val = ambiguous_decl (name, val, val1, flags);
4481 return BINDING_VALUE (val) != error_mark_node;
4485 Accepts the NAME to lookup and its qualifying SCOPE.
4486 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4490 qualified_lookup_using_namespace (name, scope, result, flags)
4496 /* Maintain a list of namespaces visited... */
4497 tree seen = NULL_TREE;
4498 /* ... and a list of namespace yet to see. */
4499 tree todo = NULL_TREE;
4501 /* Look through namespace aliases. */
4502 scope = ORIGINAL_NAMESPACE (scope);
4503 while (scope && (result != error_mark_node))
4505 seen = tree_cons (scope, NULL_TREE, seen);
4506 result = ambiguous_decl (name, result,
4507 binding_for_name (name, scope), flags);
4508 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4509 /* Consider using directives. */
4510 for (usings = DECL_NAMESPACE_USING (scope); usings;
4511 usings = TREE_CHAIN (usings))
4512 /* If this was a real directive, and we have not seen it. */
4513 if (!TREE_INDIRECT_USING (usings)
4514 && !purpose_member (TREE_PURPOSE (usings), seen))
4515 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4518 scope = TREE_PURPOSE (todo);
4519 todo = TREE_CHAIN (todo);
4522 scope = NULL_TREE; /* If there never was a todo list. */
4524 return result != error_mark_node;
4527 /* [namespace.memdef]/2 */
4529 /* Set the context of a declaration to scope. Complain if we are not
4533 set_decl_namespace (decl, scope, friendp)
4539 if (scope == std_node)
4540 scope = global_namespace;
4541 /* Get rid of namespace aliases. */
4542 scope = ORIGINAL_NAMESPACE (scope);
4544 /* It is ok for friends to be qualified in parallel space. */
4545 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4546 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4548 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4549 if (scope != current_namespace)
4551 /* See whether this has been declared in the namespace. */
4552 old = namespace_binding (DECL_NAME (decl), scope);
4554 /* No old declaration at all. */
4556 if (!is_overloaded_fn (decl))
4557 /* Don't compare non-function decls with decls_match here,
4558 since it can't check for the correct constness at this
4559 point. pushdecl will find those errors later. */
4561 /* Since decl is a function, old should contain a function decl. */
4562 if (!is_overloaded_fn (old))
4564 if (processing_template_decl || processing_specialization)
4565 /* We have not yet called push_template_decl to turn the
4566 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4567 won't match. But, we'll check later, when we construct the
4570 for (; old; old = OVL_NEXT (old))
4571 if (decls_match (decl, OVL_CURRENT (old)))
4577 cp_error ("`%D' should have been declared inside `%D'",
4581 /* Compute the namespace where a declaration is defined. */
4584 decl_namespace (decl)
4588 decl = TYPE_STUB_DECL (decl);
4589 while (DECL_CONTEXT (decl))
4591 decl = DECL_CONTEXT (decl);
4592 if (TREE_CODE (decl) == NAMESPACE_DECL)
4595 decl = TYPE_STUB_DECL (decl);
4596 my_friendly_assert (DECL_P (decl), 390);
4599 return global_namespace;
4602 /* Return the namespace where the current declaration is declared. */
4605 current_decl_namespace ()
4608 /* If we have been pushed into a different namespace, use it. */
4609 if (decl_namespace_list)
4610 return TREE_PURPOSE (decl_namespace_list);
4612 if (current_class_type)
4613 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4614 else if (current_function_decl)
4615 result = decl_namespace (current_function_decl);
4617 result = current_namespace;
4621 /* Temporarily set the namespace for the current declaration. */
4624 push_decl_namespace (decl)
4627 if (TREE_CODE (decl) != NAMESPACE_DECL)
4628 decl = decl_namespace (decl);
4629 decl_namespace_list = tree_cons (decl, NULL_TREE, decl_namespace_list);
4633 pop_decl_namespace ()
4635 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4638 /* Enter a class or namespace scope. */
4644 if (TREE_CODE (t) == NAMESPACE_DECL)
4645 push_decl_namespace (t);
4650 /* Leave scope pushed by push_scope. */
4656 if (TREE_CODE (t) == NAMESPACE_DECL)
4657 pop_decl_namespace ();
4662 /* [basic.lookup.koenig] */
4663 /* A non-zero return value in the functions below indicates an error.
4664 All nodes allocated in the procedure are on the scratch obstack. */
4674 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4675 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4676 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4677 static int add_function PARAMS ((struct arg_lookup *, tree));
4678 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4679 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4680 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4682 /* Add a function to the lookup structure.
4683 Returns 1 on error. */
4686 add_function (k, fn)
4687 struct arg_lookup *k;
4690 /* We used to check here to see if the function was already in the list,
4691 but that's O(n^2), which is just too expensive for function lookup.
4692 Now we deal with the occasional duplicate in joust. In doing this, we
4693 assume that the number of duplicates will be small compared to the
4694 total number of functions being compared, which should usually be the
4697 /* We must find only functions, or exactly one non-function. */
4698 if (k->functions && is_overloaded_fn (k->functions)
4699 && is_overloaded_fn (fn))
4700 k->functions = build_overload (fn, k->functions);
4701 else if (k->functions)
4703 tree f1 = OVL_CURRENT (k->functions);
4705 if (is_overloaded_fn (f1))
4707 fn = f1; f1 = f2; f2 = fn;
4709 cp_error_at ("`%D' is not a function,", f1);
4710 cp_error_at (" conflict with `%D'", f2);
4711 cp_error (" in call to `%D'", k->name);
4719 /* Add functions of a namespace to the lookup structure.
4720 Returns 1 on error. */
4723 arg_assoc_namespace (k, scope)
4724 struct arg_lookup *k;
4729 if (purpose_member (scope, k->namespaces))
4731 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4733 value = namespace_binding (k->name, scope);
4737 for (; value; value = OVL_NEXT (value))
4738 if (add_function (k, OVL_CURRENT (value)))
4744 /* Adds everything associated with a template argument to the lookup
4745 structure. Returns 1 on error. */
4748 arg_assoc_template_arg (k, arg)
4749 struct arg_lookup* k;
4752 /* [basic.lookup.koenig]
4754 If T is a template-id, its associated namespaces and classes are
4755 ... the namespaces and classes associated with the types of the
4756 template arguments provided for template type parameters
4757 (excluding template template parameters); the namespaces in which
4758 any template template arguments are defined; and the classes in
4759 which any member templates used as template template arguments
4760 are defined. [Note: non-type template arguments do not
4761 contribute to the set of associated namespaces. ] */
4763 /* Consider first template template arguments. */
4764 if (TREE_CODE (arg) == TEMPLATE_DECL)
4766 tree ctx = CP_DECL_CONTEXT (arg);
4768 /* It's not a member template. */
4769 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4770 return arg_assoc_namespace (k, ctx);
4771 /* Otherwise, it must be member template. */
4773 return arg_assoc_class (k, ctx);
4775 /* It's not a template template argument, but it is a type template
4777 else if (TYPE_P (arg))
4778 return arg_assoc_type (k, arg);
4779 /* It's a non-type template argument. */
4784 /* Adds everything associated with class to the lookup structure.
4785 Returns 1 on error. */
4788 arg_assoc_class (k, type)
4789 struct arg_lookup* k;
4792 tree list, friends, context;
4795 /* Backend build structures, such as __builtin_va_list, aren't
4796 affected by all this. */
4797 if (!CLASS_TYPE_P (type))
4800 if (purpose_member (type, k->classes))
4802 k->classes = tree_cons (type, NULL_TREE, k->classes);
4804 context = decl_namespace (TYPE_MAIN_DECL (type));
4805 if (arg_assoc_namespace (k, context))
4808 /* Process baseclasses. */
4809 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4810 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4813 /* Process friends. */
4814 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4815 list = TREE_CHAIN (list))
4816 if (k->name == TREE_PURPOSE (list))
4817 for (friends = TREE_VALUE (list); friends;
4818 friends = TREE_CHAIN (friends))
4819 /* Only interested in global functions with potentially hidden
4820 (i.e. unqualified) declarations. */
4821 if (TREE_PURPOSE (list) == error_mark_node && TREE_VALUE (list)
4822 && decl_namespace (TREE_VALUE (list)) == context)
4823 if (add_function (k, TREE_VALUE (list)))
4826 /* Process template arguments. */
4827 if (CLASSTYPE_TEMPLATE_INFO (type))
4829 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4830 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4831 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4837 /* Adds everything associated with a given type.
4838 Returns 1 on error. */
4841 arg_assoc_type (k, type)
4842 struct arg_lookup *k;
4845 switch (TREE_CODE (type))
4855 if (TYPE_PTRMEMFUNC_P (type))
4856 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4857 return arg_assoc_class (k, type);
4859 case REFERENCE_TYPE:
4861 return arg_assoc_type (k, TREE_TYPE (type));
4864 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4866 /* Pointer to member: associate class type and value type. */
4867 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4869 return arg_assoc_type (k, TREE_TYPE (type));
4871 /* The basetype is referenced in the first arg type, so just
4874 /* Associate the parameter types. */
4875 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4877 /* Associate the return type. */
4878 return arg_assoc_type (k, TREE_TYPE (type));
4879 case TEMPLATE_TYPE_PARM:
4880 case TEMPLATE_TEMPLATE_PARM:
4883 if (type == unknown_type_node)
4885 /* else fall through */
4887 my_friendly_abort (390);
4892 /* Adds everything associated with arguments. Returns 1 on error. */
4895 arg_assoc_args (k, args)
4896 struct arg_lookup* k;
4899 for (; args; args = TREE_CHAIN (args))
4900 if (arg_assoc (k, TREE_VALUE (args)))
4905 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4909 struct arg_lookup* k;
4912 if (n == error_mark_node)
4916 return arg_assoc_type (k, n);
4918 if (! type_unknown_p (n))
4919 return arg_assoc_type (k, TREE_TYPE (n));
4921 if (TREE_CODE (n) == ADDR_EXPR)
4922 n = TREE_OPERAND (n, 0);
4923 if (TREE_CODE (n) == COMPONENT_REF)
4924 n = TREE_OPERAND (n, 1);
4925 if (TREE_CODE (n) == OFFSET_REF)
4926 n = TREE_OPERAND (n, 1);
4927 while (TREE_CODE (n) == TREE_LIST)
4930 if (TREE_CODE (n) == FUNCTION_DECL)
4931 return arg_assoc_type (k, TREE_TYPE (n));
4932 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4934 /* [basic.lookup.koenig]
4936 If T is a template-id, its associated namespaces and classes
4937 are the namespace in which the template is defined; for
4938 member templates, the member template's class... */
4939 tree template = TREE_OPERAND (n, 0);
4940 tree args = TREE_OPERAND (n, 1);
4944 /* First, the template. There may actually be more than one if
4945 this is an overloaded function template. But, in that case,
4946 we only need the first; all the functions will be in the same
4948 template = OVL_CURRENT (template);
4950 ctx = CP_DECL_CONTEXT (template);
4952 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4954 if (arg_assoc_namespace (k, ctx) == 1)
4957 /* It must be a member template. */
4958 else if (arg_assoc_class (k, ctx) == 1)
4961 /* Now the arguments. */
4962 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4963 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4968 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4970 for (; n; n = OVL_CHAIN (n))
4971 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4978 /* Performs Koenig lookup depending on arguments, where fns
4979 are the functions found in normal lookup. */
4982 lookup_arg_dependent (name, fns, args)
4987 struct arg_lookup k;
4988 tree fn = NULL_TREE;
4992 k.classes = NULL_TREE;
4994 /* Note that we've already looked at some namespaces during normal
4995 unqualified lookup, unless we found a decl in function scope. */
4997 fn = OVL_CURRENT (fns);
4998 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4999 k.namespaces = NULL_TREE;
5001 unqualified_namespace_lookup (name, 0, &k.namespaces);
5003 arg_assoc_args (&k, args);
5007 /* Process a namespace-alias declaration. */
5010 do_namespace_alias (alias, namespace)
5011 tree alias, namespace;
5013 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5015 /* The parser did not find it, so it's not there. */
5016 cp_error ("unknown namespace `%D'", namespace);
5020 namespace = ORIGINAL_NAMESPACE (namespace);
5022 /* Build the alias. */
5023 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5024 DECL_NAMESPACE_ALIAS (alias) = namespace;
5028 /* Check a non-member using-declaration. Return the name and scope
5029 being used, and the USING_DECL, or NULL_TREE on failure. */
5032 validate_nonmember_using_decl (decl, scope, name)
5037 if (TREE_CODE (decl) == SCOPE_REF
5038 && TREE_OPERAND (decl, 0) == std_node)
5040 if (namespace_bindings_p ()
5041 && current_namespace == global_namespace)
5042 /* There's no need for a using declaration at all, here,
5043 since `std' is the same as `::'. We can't just pass this
5044 on because we'll complain later about declaring something
5045 in the same scope as a using declaration with the same
5046 name. We return NULL_TREE which indicates to the caller
5047 that there's no need to do any further processing. */
5050 *scope = global_namespace;
5051 *name = TREE_OPERAND (decl, 1);
5053 else if (TREE_CODE (decl) == SCOPE_REF)
5055 *scope = TREE_OPERAND (decl, 0);
5056 *name = TREE_OPERAND (decl, 1);
5058 /* [namespace.udecl]
5060 A using-declaration for a class member shall be a
5061 member-declaration. */
5062 if (TREE_CODE (*scope) != NAMESPACE_DECL)
5064 if (TYPE_P (*scope))
5065 cp_error ("`%T' is not a namespace", *scope);
5067 cp_error ("`%D' is not a namespace", *scope);
5071 else if (TREE_CODE (decl) == IDENTIFIER_NODE
5072 || TREE_CODE (decl) == TYPE_DECL
5073 || TREE_CODE (decl) == TEMPLATE_DECL)
5075 *scope = global_namespace;
5079 my_friendly_abort (382);
5081 *name = DECL_NAME (*name);
5082 /* Make a USING_DECL. */
5083 return push_using_decl (*scope, *name);
5086 /* Process local and global using-declarations. */
5089 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
5091 tree oldval, oldtype;
5092 tree *newval, *newtype;
5096 *newval = *newtype = NULL_TREE;
5097 decls = make_node (CPLUS_BINDING);
5098 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
5102 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
5104 cp_error ("`%D' not declared", name);
5108 /* Check for using functions. */
5109 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
5113 if (oldval && !is_overloaded_fn (oldval))
5115 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
5120 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
5122 tree new_fn = OVL_CURRENT (tmp);
5124 /* [namespace.udecl]
5126 If a function declaration in namespace scope or block
5127 scope has the same name and the same parameter types as a
5128 function introduced by a using declaration the program is
5130 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
5132 tree old_fn = OVL_CURRENT (tmp1);
5134 if (!OVL_USED (tmp1)
5135 && compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
5136 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
5138 /* There was already a non-using declaration in
5139 this scope with the same parameter types. */
5140 cp_error ("`%D' is already declared in this scope",
5144 else if (duplicate_decls (new_fn, old_fn))
5145 /* We're re-using something we already used
5146 before. We don't need to add it again. */
5150 /* If we broke out of the loop, there's no reason to add
5151 this function to the using declarations for this
5156 *newval = build_overload (OVL_CURRENT (tmp), *newval);
5157 if (TREE_CODE (*newval) != OVERLOAD)
5158 *newval = ovl_cons (*newval, NULL_TREE);
5159 OVL_USED (*newval) = 1;
5164 *newval = BINDING_VALUE (decls);
5166 duplicate_decls (*newval, oldval);
5169 *newtype = BINDING_TYPE (decls);
5170 if (oldtype && *newtype && oldtype != *newtype)
5172 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5178 /* Process a using-declaration not appearing in class or local scope. */
5181 do_toplevel_using_decl (decl)
5184 tree scope, name, binding;
5185 tree oldval, oldtype, newval, newtype;
5187 decl = validate_nonmember_using_decl (decl, &scope, &name);
5188 if (decl == NULL_TREE)
5191 binding = binding_for_name (name, current_namespace);
5193 oldval = BINDING_VALUE (binding);
5194 oldtype = BINDING_TYPE (binding);
5196 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5198 /* Copy declarations found. */
5200 BINDING_VALUE (binding) = newval;
5202 BINDING_TYPE (binding) = newtype;
5206 /* Process a using-declaration at function scope. */
5209 do_local_using_decl (decl)
5213 tree oldval, oldtype, newval, newtype;
5215 decl = validate_nonmember_using_decl (decl, &scope, &name);
5216 if (decl == NULL_TREE)
5219 oldval = lookup_name_current_level (name);
5220 oldtype = lookup_type_current_level (name);
5222 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5226 if (is_overloaded_fn (newval))
5230 /* We only need to push declarations for those functions
5231 that were not already bound in the current level.
5232 The old value might be NULL_TREE, it might be a single
5233 function, or an OVERLOAD. */
5234 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5235 term = OVL_FUNCTION (oldval);
5238 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5240 push_overloaded_decl (OVL_CURRENT (fn),
5241 PUSH_LOCAL | PUSH_USING);
5244 push_local_binding (name, newval, PUSH_USING);
5247 set_identifier_type_value (name, newtype);
5251 do_class_using_decl (decl)
5256 if (TREE_CODE (decl) != SCOPE_REF
5257 || !TYPE_P (TREE_OPERAND (decl, 0)))
5259 cp_error ("using-declaration for non-member at class scope");
5262 name = TREE_OPERAND (decl, 1);
5263 if (TREE_CODE (name) == BIT_NOT_EXPR)
5265 cp_error ("using-declaration for destructor");
5268 if (TREE_CODE (name) == TYPE_DECL)
5269 name = DECL_NAME (name);
5271 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5273 value = build_lang_decl (USING_DECL, name, void_type_node);
5274 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5278 /* Process a using-directive. */
5281 do_using_directive (namespace)
5284 if (namespace == std_node)
5286 /* using namespace A::B::C; */
5287 if (TREE_CODE (namespace) == SCOPE_REF)
5288 namespace = TREE_OPERAND (namespace, 1);
5289 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5291 /* Lookup in lexer did not find a namespace. */
5292 cp_error ("namespace `%T' undeclared", namespace);
5295 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5297 cp_error ("`%T' is not a namespace", namespace);
5300 namespace = ORIGINAL_NAMESPACE (namespace);
5301 if (!toplevel_bindings_p ())
5302 push_using_directive (namespace);
5305 add_using_namespace (current_namespace, namespace, 0);
5309 check_default_args (x)
5312 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5313 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5314 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5316 if (TREE_PURPOSE (arg))
5320 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5331 TREE_USED (decl) = 1;
5332 if (processing_template_decl)
5334 assemble_external (decl);
5336 /* Is it a synthesized method that needs to be synthesized? */
5337 if (TREE_CODE (decl) == FUNCTION_DECL
5338 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5339 && DECL_ARTIFICIAL (decl)
5340 && ! DECL_INITIAL (decl)
5341 /* Kludge: don't synthesize for default args. */
5342 && current_function_decl)
5344 synthesize_method (decl);
5345 /* If we've already synthesized the method we don't need to
5346 instantiate it, so we can return right away. */
5350 /* If this is a function or variable that is an instance of some
5351 template, we now know that we will need to actually do the
5352 instantiation. We check that DECL is not an explicit
5353 instantiation because that is not checked in instantiate_decl. */
5354 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5355 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5356 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5357 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5358 instantiate_decl (decl, /*defer_ok=*/1);
5361 /* Helper function for named_class_head_sans_basetype nonterminal. We
5362 have just seen something of the form `AGGR SCOPE::ID'. Return a
5363 TYPE_DECL for the type declared by ID in SCOPE. */
5366 handle_class_head (aggr, scope, id)
5367 tree aggr, scope, id;
5371 if (TREE_CODE (id) == TYPE_DECL)
5373 else if (DECL_CLASS_TEMPLATE_P (id))
5374 decl = DECL_TEMPLATE_RESULT (id);
5377 tree current = current_scope();
5379 if (current == NULL_TREE)
5380 current = current_namespace;
5381 if (scope == std_node)
5382 scope = global_namespace;
5383 if (scope == NULL_TREE)
5384 scope = global_namespace;
5385 if (scope == current)
5387 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5388 Be nice about it. */
5390 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5391 FROB_CONTEXT (scope), id);
5393 else if (scope != global_namespace)
5394 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5396 cp_error ("no file-scope type named `%D'", id);
5398 /* Inject it at the current scope. */
5399 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, 1));
5402 /* Enter the SCOPE. If this turns out not to be a definition, the
5403 parser must leave the scope. */
5404 push_scope (CP_DECL_CONTEXT (decl));
5406 /* If we see something like:
5408 template <typename T> struct S::I ....
5410 we must create a TEMPLATE_DECL for the nested type. */
5411 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5412 decl = push_template_decl (decl);
5417 /* Initialize decl2.c. */
5422 ggc_add_tree_root (&decl_namespace_list, 1);
5423 ggc_add_tree_varray_root (&deferred_fns, 1);
5424 ggc_add_tree_varray_root (&pending_statics, 1);
5425 ggc_add_tree_varray_root (&ssdf_decls, 1);
5426 ggc_add_tree_root (&ssdf_decl, 1);
5427 ggc_add_tree_root (&priority_decl, 1);
5428 ggc_add_tree_root (&initialize_p_decl, 1);
5429 ggc_add_tree_root (&pending_vtables, 1);