1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
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. */
43 #include "dwarf2out.h"
48 extern cpp_reader *parse_in;
50 /* This structure contains information about the initializations
51 and/or destructions required for a particular priority level. */
52 typedef struct priority_info_s {
53 /* Non-zero if there have been any initializations at this priority
54 throughout the translation unit. */
55 int initializations_p;
56 /* Non-zero if there have been any destructions at this priority
57 throughout the translation unit. */
61 static void mark_vtable_entries PARAMS ((tree));
62 static void grok_function_init PARAMS ((tree, tree));
63 static int finish_vtable_vardecl PARAMS ((tree *, void *));
64 static int prune_vtable_vardecl PARAMS ((tree *, void *));
65 static int is_namespace_ancestor PARAMS ((tree, tree));
66 static void add_using_namespace PARAMS ((tree, tree, int));
67 static tree ambiguous_decl PARAMS ((tree, tree, tree,int));
68 static tree build_anon_union_vars PARAMS ((tree, tree*, int, int));
69 static int acceptable_java_type PARAMS ((tree));
70 static void output_vtable_inherit PARAMS ((tree));
71 static tree start_objects PARAMS ((int, int));
72 static void finish_objects PARAMS ((int, int, tree));
73 static tree merge_functions PARAMS ((tree, tree));
74 static tree decl_namespace PARAMS ((tree));
75 static tree validate_nonmember_using_decl PARAMS ((tree, tree *, tree *));
76 static void do_nonmember_using_decl PARAMS ((tree, tree, tree, tree,
78 static tree start_static_storage_duration_function PARAMS ((void));
79 static void finish_static_storage_duration_function PARAMS ((tree));
80 static priority_info get_priority_info PARAMS ((int));
81 static void do_static_initialization PARAMS ((tree, tree));
82 static void do_static_destruction PARAMS ((tree));
83 static tree start_static_initialization_or_destruction PARAMS ((tree, int));
84 static void finish_static_initialization_or_destruction PARAMS ((tree));
85 static void generate_ctor_or_dtor_function PARAMS ((int, int));
86 static int generate_ctor_and_dtor_functions_for_priority
87 PARAMS ((splay_tree_node, void *));
88 static tree prune_vars_needing_no_initialization PARAMS ((tree));
89 static void write_out_vars PARAMS ((tree));
90 static void import_export_class PARAMS ((tree));
91 static tree key_method PARAMS ((tree));
92 static int compare_options PARAMS ((const PTR, const PTR));
93 static tree get_guard_bits PARAMS ((tree));
95 /* A list of static class variables. This is needed, because a
96 static class variable can be declared inside the class without
97 an initializer, and then initialized, staticly, outside the class. */
98 static varray_type pending_statics;
99 #define pending_statics_used \
100 (pending_statics ? pending_statics->elements_used : 0)
102 /* A list of functions which were declared inline, but which we
103 may need to emit outline anyway. */
104 static varray_type deferred_fns;
105 #define deferred_fns_used \
106 (deferred_fns ? deferred_fns->elements_used : 0)
108 /* Same, but not reset. Local temp variables and global temp variables
109 can have the same name. */
110 static int global_temp_name_counter;
112 /* Flag used when debugging spew.c */
114 extern int spew_debug;
116 /* Nonzero if we're done parsing and into end-of-file activities. */
120 /* Functions called along with real static constructors and destructors. */
125 /* The :: namespace. */
127 tree global_namespace;
129 /* C (and C++) language-specific option variables. */
131 /* Nonzero means don't recognize the keyword `asm'. */
135 /* Nonzero means don't recognize any extension keywords. */
137 int flag_no_gnu_keywords;
139 /* Nonzero means do some things the same way PCC does. Only provided so
140 the compiler will link. */
142 int flag_traditional;
144 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
146 int flag_signed_bitfields = 1;
148 /* Nonzero means enable obscure standard features and disable GNU
149 extensions that might cause standard-compliant code to be
154 /* Nonzero means do emit exported implementations of functions even if
155 they can be inlined. */
157 int flag_implement_inlines = 1;
159 /* Nonzero means do emit exported implementations of templates, instead of
160 multiple static copies in each file that needs a definition. */
162 int flag_external_templates;
164 /* Nonzero means that the decision to emit or not emit the implementation of a
165 template depends on where the template is instantiated, rather than where
168 int flag_alt_external_templates;
170 /* Nonzero means that implicit instantiations will be emitted if needed. */
172 int flag_implicit_templates = 1;
174 /* Nonzero means that implicit instantiations of inline templates will be
175 emitted if needed, even if instantiations of non-inline templates
178 int flag_implicit_inline_templates = 1;
180 /* Nonzero means warn about implicit declarations. */
182 int warn_implicit = 1;
184 /* Nonzero means warn about usage of long long when `-pedantic'. */
186 int warn_long_long = 1;
188 /* Nonzero means warn when all ctors or dtors are private, and the class
191 int warn_ctor_dtor_privacy = 1;
193 /* True if we want to implement vtables using "thunks".
194 The default is off. */
196 int flag_vtable_thunks = 1;
198 /* Nonzero means generate separate instantiation control files and juggle
199 them at link time. */
201 int flag_use_repository;
203 /* Nonzero if we want to issue diagnostics that the standard says are not
206 int flag_optional_diags = 1;
208 /* Nonzero means give string constants the type `const char *', as mandated
211 int flag_const_strings = 1;
213 /* Nonzero means warn about deprecated conversion from string constant to
216 int warn_write_strings;
218 /* Nonzero means warn about pointer casts that can drop a type qualifier
219 from the pointer target type. */
223 /* Nonzero means warn about sizeof(function) or addition/subtraction
224 of function pointers. */
226 int warn_pointer_arith = 1;
228 /* Nonzero means warn for any function def without prototype decl. */
230 int warn_missing_prototypes;
232 /* Nonzero means warn about multiple (redundant) decls for the same single
233 variable or function. */
235 int warn_redundant_decls;
237 /* Warn if initializer is not completely bracketed. */
239 int warn_missing_braces;
241 /* Warn about comparison of signed and unsigned values. */
243 int warn_sign_compare;
245 /* Warn about testing equality of floating point numbers. */
247 int warn_float_equal = 0;
249 /* Warn about functions which might be candidates for format attributes. */
251 int warn_missing_format_attribute;
253 /* Warn about a subscript that has type char. */
255 int warn_char_subscripts;
257 /* Warn if a type conversion is done that might have confusing results. */
261 /* Warn if adding () is suggested. */
263 int warn_parentheses;
265 /* Non-zero means warn in function declared in derived class has the
266 same name as a virtual in the base class, but fails to match the
267 type signature of any virtual function in the base class. */
269 int warn_overloaded_virtual;
271 /* Non-zero means warn when declaring a class that has a non virtual
272 destructor, when it really ought to have a virtual one. */
276 /* Non-zero means warn when the compiler will reorder code. */
280 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
284 /* Non-zero means warn when we convert a pointer to member function
285 into a pointer to (void or function). */
287 int warn_pmf2ptr = 1;
289 /* Nonzero means warn about violation of some Effective C++ style rules. */
293 /* Nonzero means warn where overload resolution chooses a promotion from
294 unsigned to signed over a conversion to an unsigned of the same size. */
298 /* Nonzero means warn when an old-style cast is used. */
300 int warn_old_style_cast;
302 /* Warn about #pragma directives that are not recognised. */
304 int warn_unknown_pragmas; /* Tri state variable. */
306 /* Nonzero means warn about use of multicharacter literals. */
308 int warn_multichar = 1;
310 /* Nonzero means warn when non-templatized friend functions are
311 declared within a template */
313 int warn_nontemplate_friend = 1;
315 /* Nonzero means complain about deprecated features. */
317 int warn_deprecated = 1;
319 /* Nonzero means `$' can be in an identifier. */
321 #ifndef DOLLARS_IN_IDENTIFIERS
322 #define DOLLARS_IN_IDENTIFIERS 1
324 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
326 /* Nonzero means allow Microsoft extensions without a pedwarn. */
328 int flag_ms_extensions;
330 /* C++ specific flags. */
332 /* Nonzero means we should attempt to elide constructors when possible. */
334 int flag_elide_constructors = 1;
336 /* Nonzero means that member functions defined in class scope are
337 inline by default. */
339 int flag_default_inline = 1;
341 /* Controls whether compiler generates 'type descriptor' that give
342 run-time type information. */
346 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
349 int flag_huge_objects;
351 /* Nonzero if we want to conserve space in the .o files. We do this
352 by putting uninitialized data and runtime initialized data into
353 .common instead of .data at the expense of not flagging multiple
356 int flag_conserve_space;
358 /* Nonzero if we want to obey access control semantics. */
360 int flag_access_control = 1;
362 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
364 int flag_operator_names = 1;
366 /* Nonzero if we want to check the return value of new and avoid calling
367 constructors if it is a null pointer. */
371 /* Nonnull if we want to dump class heirarchies. */
373 const char *flag_dump_class_layout;
375 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
376 initialization variables.
377 0: Old rules, set by -fno-for-scope.
378 2: New ISO rules, set by -ffor-scope.
379 1: Try to implement new ISO rules, but with backup compatibility
380 (and warnings). This is the default, for now. */
382 int flag_new_for_scope = 1;
384 /* Nonzero if we want to emit defined symbols with common-like linkage as
385 weak symbols where possible, in order to conform to C++ semantics.
386 Otherwise, emit them as local symbols. */
390 /* Nonzero to use __cxa_atexit, rather than atexit, to register
391 destructors for local statics and global objects. */
393 int flag_use_cxa_atexit;
395 /* Nonzero to not ignore namespace std. */
397 int flag_honor_std = 1;
399 /* Nonzero if we should expand functions calls inline at the tree
400 level, rather than at the RTL level. */
402 int flag_inline_trees = 0;
404 /* Maximum template instantiation depth. This limit is rather
405 arbitrary, but it exists to limit the time it takes to notice
406 infinite template instantiations. */
408 int max_tinst_depth = 50;
410 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
414 /* Nonzero means make the default pedwarns warnings instead of errors.
415 The value of this flag is ignored if -pedantic is specified. */
419 /* Nonzero means to implement standard semantics for exception
420 specifications, calling unexpected if an exception is thrown that
421 doesn't match the specification. Zero means to treat them as
422 assertions and optimize accordingly, but not check them. */
424 int flag_enforce_eh_specs = 1;
426 /* The variant of the C language being processed. */
428 c_language_kind c_language = clk_cplusplus;
430 /* Table of language-dependent -f options.
431 STRING is the option name. VARIABLE is the address of the variable.
432 ON_VALUE is the value to store in VARIABLE
433 if `-fSTRING' is seen as an option.
434 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
436 static struct { const char *string; int *variable; int on_value;}
440 {"signed-char", &flag_signed_char, 1},
441 {"unsigned-char", &flag_signed_char, 0},
442 {"signed-bitfields", &flag_signed_bitfields, 1},
443 {"unsigned-bitfields", &flag_signed_bitfields, 0},
444 {"short-enums", &flag_short_enums, 1},
445 {"short-double", &flag_short_double, 1},
446 {"short-wchar", &flag_short_wchar, 1},
447 {"asm", &flag_no_asm, 0},
448 {"builtin", &flag_no_builtin, 0},
450 /* C++-only options. */
451 {"access-control", &flag_access_control, 1},
452 {"check-new", &flag_check_new, 1},
453 {"conserve-space", &flag_conserve_space, 1},
454 {"const-strings", &flag_const_strings, 1},
455 {"default-inline", &flag_default_inline, 1},
456 {"dollars-in-identifiers", &dollars_in_ident, 1},
457 {"elide-constructors", &flag_elide_constructors, 1},
458 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
459 {"external-templates", &flag_external_templates, 1},
460 {"for-scope", &flag_new_for_scope, 2},
461 {"gnu-keywords", &flag_no_gnu_keywords, 0},
462 {"handle-exceptions", &flag_exceptions, 1},
463 {"honor-std", &flag_honor_std, 1},
464 {"huge-objects", &flag_huge_objects, 1},
465 {"implement-inlines", &flag_implement_inlines, 1},
466 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
467 {"implicit-templates", &flag_implicit_templates, 1},
468 {"ms-extensions", &flag_ms_extensions, 1},
469 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
470 {"operator-names", &flag_operator_names, 1},
471 {"optional-diags", &flag_optional_diags, 1},
472 {"permissive", &flag_permissive, 1},
473 {"repo", &flag_use_repository, 1},
474 {"rtti", &flag_rtti, 1},
475 {"stats", &flag_detailed_statistics, 1},
476 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
477 {"vtable-thunks", &flag_vtable_thunks, 1},
478 {"weak", &flag_weak, 1}
481 /* The list of `-f' options that we no longer support. The `-f'
482 prefix is not given in this table. The `-fno-' variants are not
483 listed here. This table must be kept in alphabetical order. */
484 static const char * const unsupported_options[] = {
499 /* Compare two option strings, pointed two by P1 and P2, for use with
503 compare_options (p1, p2)
507 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
510 /* Decode the string P as a language-specific option.
511 Return the number of strings consumed for a valid option.
512 Otherwise return 0. Should not complain if it does not
513 recognise the option. */
516 cxx_decode_option (argc, argv)
520 int strings_processed;
521 const char *p = argv[0];
523 strings_processed = cpp_handle_option (parse_in, argc, argv);
525 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
527 else if (p[0] == '-' && p[1] == 'f')
529 /* Some kind of -f option.
530 P's value is the option sans `-f'.
531 Search for it in the table of options. */
532 const char *option_value = NULL;
533 const char *positive_option;
537 /* Try special -f options. */
539 /* See if this is one of the options no longer supported. We
540 used to support these options, so we continue to accept them,
542 if (strncmp (p, "no-", strlen ("no-")) == 0)
543 positive_option = p + strlen ("no-");
547 /* If the option is present, issue a warning. Indicate to our
548 caller that the option was processed successfully. */
549 if (bsearch (&positive_option,
551 (sizeof (unsupported_options)
552 / sizeof (unsupported_options[0])),
553 sizeof (unsupported_options[0]),
556 warning ("-f%s is no longer supported", p);
560 if (!strcmp (p, "handle-exceptions")
561 || !strcmp (p, "no-handle-exceptions"))
562 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
563 else if (! strcmp (p, "alt-external-templates"))
565 flag_external_templates = 1;
566 flag_alt_external_templates = 1;
567 cp_deprecated ("-falt-external-templates");
569 else if (! strcmp (p, "no-alt-external-templates"))
570 flag_alt_external_templates = 0;
571 else if (!strcmp (p, "repo"))
573 flag_use_repository = 1;
574 flag_implicit_templates = 0;
576 else if (!strcmp (p, "external-templates"))
578 flag_external_templates = 1;
579 cp_deprecated ("-fexternal-templates");
581 else if ((option_value
582 = skip_leading_substring (p, "template-depth-")))
584 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
585 else if ((option_value
586 = skip_leading_substring (p, "name-mangling-version-")))
588 warning ("-fname-mangling-version is no longer supported");
591 else if ((option_value
592 = skip_leading_substring (p, "dump-translation-unit=")))
595 error ("no file specified with -fdump-translation-unit");
597 flag_dump_translation_unit = option_value;
599 else if ((option_value
600 = skip_leading_substring (p, "dump-class-layout=")))
603 error ("no file specified with -fdump-class-layout");
605 flag_dump_class_layout = option_value;
607 else if (!strcmp (p, "dump-class-layout"))
608 flag_dump_class_layout = ""; /* empty string for stderr */
614 !found && j < (sizeof (lang_f_options)
615 / sizeof (lang_f_options[0]));
618 if (!strcmp (p, lang_f_options[j].string))
620 *lang_f_options[j].variable = lang_f_options[j].on_value;
621 /* A goto here would be cleaner,
622 but breaks the vax pcc. */
625 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
626 && ! strcmp (p+3, lang_f_options[j].string))
628 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
636 else if (p[0] == '-' && p[1] == 'W')
640 /* The -W options control the warning behavior of the compiler. */
643 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
646 if (!strcmp (p, "implicit"))
647 warn_implicit = setting;
648 else if (!strcmp (p, "long-long"))
649 warn_long_long = setting;
650 else if (!strcmp (p, "return-type"))
651 warn_return_type = setting;
652 else if (!strcmp (p, "ctor-dtor-privacy"))
653 warn_ctor_dtor_privacy = setting;
654 else if (!strcmp (p, "write-strings"))
655 warn_write_strings = setting;
656 else if (!strcmp (p, "cast-qual"))
657 warn_cast_qual = setting;
658 else if (!strcmp (p, "char-subscripts"))
659 warn_char_subscripts = setting;
660 else if (!strcmp (p, "pointer-arith"))
661 warn_pointer_arith = setting;
662 else if (!strcmp (p, "missing-prototypes"))
663 warn_missing_prototypes = setting;
664 else if (!strcmp (p, "strict-prototypes"))
667 warning ("-Wno-strict-prototypes is not supported in C++");
669 else if (!strcmp (p, "redundant-decls"))
670 warn_redundant_decls = setting;
671 else if (!strcmp (p, "missing-braces"))
672 warn_missing_braces = setting;
673 else if (!strcmp (p, "sign-compare"))
674 warn_sign_compare = setting;
675 else if (!strcmp (p, "float-equal"))
676 warn_float_equal = setting;
677 else if (!strcmp (p, "format"))
678 set_Wformat (setting);
679 else if (!strcmp (p, "format=2"))
681 else if (!strcmp (p, "format-y2k"))
682 warn_format_y2k = setting;
683 else if (!strcmp (p, "format-extra-args"))
684 warn_format_extra_args = setting;
685 else if (!strcmp (p, "format-nonliteral"))
686 warn_format_nonliteral = setting;
687 else if (!strcmp (p, "format-security"))
688 warn_format_security = setting;
689 else if (!strcmp (p, "missing-format-attribute"))
690 warn_missing_format_attribute = setting;
691 else if (!strcmp (p, "conversion"))
692 warn_conversion = setting;
693 else if (!strcmp (p, "parentheses"))
694 warn_parentheses = setting;
695 else if (!strcmp (p, "non-virtual-dtor"))
696 warn_nonvdtor = setting;
697 else if (!strcmp (p, "reorder"))
698 warn_reorder = setting;
699 else if (!strcmp (p, "synth"))
700 warn_synth = setting;
701 else if (!strcmp (p, "pmf-conversions"))
702 warn_pmf2ptr = setting;
703 else if (!strcmp (p, "effc++"))
705 else if (!strcmp (p, "sign-promo"))
706 warn_sign_promo = setting;
707 else if (!strcmp (p, "old-style-cast"))
708 warn_old_style_cast = setting;
709 else if (!strcmp (p, "overloaded-virtual"))
710 warn_overloaded_virtual = setting;
711 else if (!strcmp (p, "multichar"))
712 warn_multichar = setting;
713 else if (!strcmp (p, "unknown-pragmas"))
714 /* Set to greater than 1, so that even unknown pragmas in
715 system headers will be warned about. */
716 warn_unknown_pragmas = setting * 2;
717 else if (!strcmp (p, "non-template-friend"))
718 warn_nontemplate_friend = setting;
719 else if (!strcmp (p, "deprecated"))
720 warn_deprecated = setting;
721 else if (!strcmp (p, "comment"))
722 ; /* cpp handles this one. */
723 else if (!strcmp (p, "comments"))
724 ; /* cpp handles this one. */
725 else if (!strcmp (p, "trigraphs"))
726 ; /* cpp handles this one. */
727 else if (!strcmp (p, "import"))
728 ; /* cpp handles this one. */
729 else if (!strcmp (p, "all"))
731 warn_return_type = setting;
732 set_Wunused (setting);
733 warn_implicit = setting;
734 warn_switch = setting;
735 set_Wformat (setting);
736 warn_parentheses = setting;
737 warn_missing_braces = setting;
738 warn_sign_compare = setting;
739 warn_multichar = setting;
740 /* We save the value of warn_uninitialized, since if they put
741 -Wuninitialized on the command line, we need to generate a
742 warning about not using it without also specifying -O. */
743 if (warn_uninitialized != 1)
744 warn_uninitialized = (setting ? 2 : 0);
745 /* Only warn about unknown pragmas that are not in system
747 warn_unknown_pragmas = 1;
749 /* C++-specific warnings. */
750 warn_ctor_dtor_privacy = setting;
751 warn_nonvdtor = setting;
752 warn_reorder = setting;
753 warn_nontemplate_friend = setting;
755 else return strings_processed;
757 else if (!strcmp (p, "-ansi"))
758 flag_no_nonansi_builtin = 1, flag_ansi = 1,
759 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
761 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
762 it's probably safe to assume no sane person would ever want to use this
763 under normal circumstances. */
764 else if (!strcmp (p, "-spew-debug"))
768 return strings_processed;
773 /* Incorporate `const' and `volatile' qualifiers for member functions.
774 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
775 QUALS is a list of qualifiers. Returns any explicit
776 top-level qualifiers of the method's this pointer, anything other than
777 TYPE_UNQUALIFIED will be an extension. */
780 grok_method_quals (ctype, function, quals)
781 tree ctype, function, quals;
783 tree fntype = TREE_TYPE (function);
784 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
785 int type_quals = TYPE_UNQUALIFIED;
786 int dup_quals = TYPE_UNQUALIFIED;
787 int this_quals = TYPE_UNQUALIFIED;
791 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
793 if ((type_quals | this_quals) & tq)
795 else if (tq & TYPE_QUAL_RESTRICT)
799 quals = TREE_CHAIN (quals);
803 if (dup_quals != TYPE_UNQUALIFIED)
804 cp_error ("duplicate type qualifiers in %s declaration",
805 TREE_CODE (function) == FUNCTION_DECL
806 ? "member function" : "type");
808 ctype = cp_build_qualified_type (ctype, type_quals);
809 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
810 (TREE_CODE (fntype) == METHOD_TYPE
811 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
812 : TYPE_ARG_TYPES (fntype)));
814 fntype = build_exception_variant (fntype, raises);
816 TREE_TYPE (function) = fntype;
820 /* Warn when -fexternal-templates is used and #pragma
821 interface/implementation is not used all the times it should be,
825 warn_if_unknown_interface (decl)
828 static int already_warned = 0;
829 if (already_warned++)
832 if (flag_alt_external_templates)
834 tree til = tinst_for_decl ();
836 const char *sf = input_filename;
840 lineno = TINST_LINE (til);
841 input_filename = TINST_FILE (til);
843 cp_warning ("template `%#D' instantiated in file without #pragma interface",
849 cp_warning_at ("template `%#D' defined in file without #pragma interface",
853 /* A subroutine of the parser, to handle a component list. */
856 grok_x_components (specs)
861 specs = strip_attrs (specs);
863 check_tag_decl (specs);
864 t = groktypename (build_tree_list (specs, NULL_TREE));
866 /* The only case where we need to do anything additional here is an
867 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
868 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
871 fixup_anonymous_aggr (t);
872 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
875 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
879 build_artificial_parm (name, type)
885 parm = build_decl (PARM_DECL, name, type);
886 DECL_ARTIFICIAL (parm) = 1;
887 /* All our artificial parms are implicitly `const'; they cannot be
889 TREE_READONLY (parm) = 1;
890 DECL_ARG_TYPE (parm) = type;
894 /* Constructors for types with virtual baseclasses need an "in-charge" flag
895 saying whether this constructor is responsible for initialization of
896 virtual baseclasses or not. All destructors also need this "in-charge"
897 flag, which additionally determines whether or not the destructor should
898 free the memory for the object.
900 This function adds the "in-charge" flag to member function FN if
901 appropriate. It is called from grokclassfn and tsubst.
902 FN must be either a constructor or destructor.
904 The in-charge flag follows the 'this' parameter, and is followed by the
905 VTT parm (if any), then the user-written parms. */
908 maybe_retrofit_in_chrg (fn)
911 tree basetype, arg_types, parms, parm, fntype;
913 /* If we've already add the in-charge parameter don't do it again. */
914 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
917 /* When processing templates we can't know, in general, whether or
918 not we're going to have virtual baseclasses. */
919 if (uses_template_parms (fn))
922 /* We don't need an in-charge parameter for constructors that don't
923 have virtual bases. */
924 if (DECL_CONSTRUCTOR_P (fn)
925 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
928 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
929 basetype = TREE_TYPE (TREE_VALUE (arg_types));
930 arg_types = TREE_CHAIN (arg_types);
932 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
934 /* If this is a subobject constructor or destructor, our caller will
935 pass us a pointer to our VTT. */
936 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
938 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
940 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
941 TREE_CHAIN (parm) = parms;
944 /* ...and then to TYPE_ARG_TYPES. */
945 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
947 DECL_HAS_VTT_PARM_P (fn) = 1;
950 /* Then add the in-charge parm (before the VTT parm). */
951 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
952 TREE_CHAIN (parm) = parms;
954 arg_types = hash_tree_chain (integer_type_node, arg_types);
956 /* Insert our new parameter(s) into the list. */
957 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
959 /* And rebuild the function type. */
960 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
962 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
963 fntype = build_exception_variant (fntype,
964 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
965 TREE_TYPE (fn) = fntype;
967 /* Now we've got the in-charge parameter. */
968 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
971 /* Classes overload their constituent function names automatically.
972 When a function name is declared in a record structure,
973 its name is changed to it overloaded name. Since names for
974 constructors and destructors can conflict, we place a leading
977 CNAME is the name of the class we are grokking for.
979 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
981 FLAGS contains bits saying what's special about today's
982 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
984 If FUNCTION is a destructor, then we must add the `auto-delete' field
985 as a second parameter. There is some hair associated with the fact
986 that we must "declare" this variable in the manner consistent with the
987 way the rest of the arguments were declared.
989 QUALS are the qualifiers for the this pointer. */
992 grokclassfn (ctype, function, flags, quals)
993 tree ctype, function;
994 enum overload_flags flags;
997 tree fn_name = DECL_NAME (function);
998 int this_quals = TYPE_UNQUALIFIED;
1000 /* Even within an `extern "C"' block, members get C++ linkage. See
1001 [dcl.link] for details. */
1002 SET_DECL_LANGUAGE (function, lang_cplusplus);
1004 if (fn_name == NULL_TREE)
1006 error ("name missing for member function");
1007 fn_name = get_identifier ("<anonymous>");
1008 DECL_NAME (function) = fn_name;
1012 this_quals = grok_method_quals (ctype, function, quals);
1014 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1016 /* Must add the class instance variable up front. */
1017 /* Right now we just make this a pointer. But later
1018 we may wish to make it special. */
1019 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
1023 /* The `this' parameter is implicitly `const'; it cannot be
1025 this_quals |= TYPE_QUAL_CONST;
1026 qual_type = cp_build_qualified_type (type, this_quals);
1027 parm = build_artificial_parm (this_identifier, qual_type);
1028 c_apply_type_quals_to_decl (this_quals, parm);
1030 /* We can make this a register, so long as we don't
1031 accidentally complain if someone tries to take its address. */
1032 DECL_REGISTER (parm) = 1;
1033 TREE_CHAIN (parm) = last_function_parms;
1034 last_function_parms = parm;
1037 DECL_ARGUMENTS (function) = last_function_parms;
1038 DECL_CONTEXT (function) = ctype;
1040 if (flags == DTOR_FLAG)
1041 DECL_DESTRUCTOR_P (function) = 1;
1043 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1044 maybe_retrofit_in_chrg (function);
1046 if (flags == DTOR_FLAG)
1048 DECL_DESTRUCTOR_P (function) = 1;
1049 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1053 /* Work on the expr used by alignof (this is only called by the parser). */
1062 if (processing_template_decl)
1063 return build_min (ALIGNOF_EXPR, sizetype, expr);
1065 if (TREE_CODE (expr) == COMPONENT_REF
1066 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1067 error ("`__alignof__' applied to a bit-field");
1069 if (TREE_CODE (expr) == INDIRECT_REF)
1071 best = t = TREE_OPERAND (expr, 0);
1072 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1074 while (TREE_CODE (t) == NOP_EXPR
1075 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1078 t = TREE_OPERAND (t, 0);
1079 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1080 if (thisalign > bestalign)
1081 best = t, bestalign = thisalign;
1083 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1086 return c_alignof (TREE_TYPE (expr));
1089 /* Create an ARRAY_REF, checking for the user doing things backwards
1093 grok_array_decl (array_expr, index_exp)
1094 tree array_expr, index_exp;
1096 tree type = TREE_TYPE (array_expr);
1097 tree p1, p2, i1, i2;
1099 if (type == error_mark_node || index_exp == error_mark_node)
1100 return error_mark_node;
1101 if (processing_template_decl)
1102 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1103 array_expr, index_exp);
1105 if (type == NULL_TREE)
1107 /* Something has gone very wrong. Assume we are mistakenly reducing
1108 an expression instead of a declaration. */
1109 error ("parser may be lost: is there a '{' missing somewhere?");
1113 if (TREE_CODE (type) == OFFSET_TYPE
1114 || TREE_CODE (type) == REFERENCE_TYPE)
1115 type = TREE_TYPE (type);
1117 /* If they have an `operator[]', use that. */
1118 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1119 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1120 array_expr, index_exp, NULL_TREE);
1122 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1123 is a little-known fact that, if `a' is an array and `i' is an
1124 int, you can write `i[a]', which means the same thing as `a[i]'. */
1126 if (TREE_CODE (type) == ARRAY_TYPE)
1129 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1131 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1134 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1136 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1137 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1139 if ((p1 && i2) && (i1 && p2))
1140 error ("ambiguous conversion for array subscript");
1143 array_expr = p1, index_exp = i2;
1145 array_expr = p2, index_exp = i1;
1148 cp_error ("invalid types `%T[%T]' for array subscript",
1149 type, TREE_TYPE (index_exp));
1150 return error_mark_node;
1153 if (array_expr == error_mark_node || index_exp == error_mark_node)
1154 error ("ambiguous conversion for array subscript");
1156 return build_array_ref (array_expr, index_exp);
1159 /* Given the cast expression EXP, checking out its validity. Either return
1160 an error_mark_node if there was an unavoidable error, return a cast to
1161 void for trying to delete a pointer w/ the value 0, or return the
1162 call to delete. If DOING_VEC is 1, we handle things differently
1163 for doing an array delete. If DOING_VEC is 2, they gave us the
1164 array size as an argument to delete.
1165 Implements ARM $5.3.4. This is called from the parser. */
1168 delete_sanity (exp, size, doing_vec, use_global_delete)
1170 int doing_vec, use_global_delete;
1173 /* For a regular vector delete (aka, no size argument) we will pass
1174 this down as a NULL_TREE into build_vec_delete. */
1175 tree maxindex = NULL_TREE;
1177 if (exp == error_mark_node)
1180 if (processing_template_decl)
1182 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1183 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1184 DELETE_EXPR_USE_VEC (t) = doing_vec;
1188 if (TREE_CODE (exp) == OFFSET_REF)
1189 exp = resolve_offset_ref (exp);
1190 exp = convert_from_reference (exp);
1191 t = stabilize_reference (exp);
1192 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1194 if (t == NULL_TREE || t == error_mark_node)
1196 cp_error ("type `%#T' argument given to `delete', expected pointer",
1198 return error_mark_node;
1203 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1204 pedwarn ("anachronistic use of array size in vector delete");
1207 type = TREE_TYPE (t);
1209 /* As of Valley Forge, you can delete a pointer to const. */
1211 /* You can't delete functions. */
1212 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1214 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1215 return error_mark_node;
1218 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1219 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1221 cp_warning ("deleting `%T' is undefined", type);
1225 /* An array can't have been allocated by new, so complain. */
1226 if (TREE_CODE (t) == ADDR_EXPR
1227 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1228 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1229 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1231 /* Deleting a pointer with the value zero is valid and has no effect. */
1232 if (integer_zerop (t))
1233 return build1 (NOP_EXPR, void_type_node, t);
1236 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1240 if (IS_AGGR_TYPE (TREE_TYPE (type))
1241 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1243 /* Only do access checking here; we'll be calling op delete
1244 from the destructor. */
1245 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1246 LOOKUP_NORMAL, NULL_TREE);
1247 if (tmp == error_mark_node)
1248 return error_mark_node;
1251 return build_delete (type, t, sfk_deleting_destructor,
1252 LOOKUP_NORMAL, use_global_delete);
1256 /* Report an error if the indicated template declaration is not the
1257 sort of thing that should be a member template. */
1260 check_member_template (tmpl)
1265 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1266 decl = DECL_TEMPLATE_RESULT (tmpl);
1268 if (TREE_CODE (decl) == FUNCTION_DECL
1269 || (TREE_CODE (decl) == TYPE_DECL
1270 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1272 if (current_function_decl)
1273 /* 14.5.2.2 [temp.mem]
1275 A local class shall not have member templates. */
1276 cp_error ("invalid declaration of member template `%#D' in local class",
1279 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1281 /* 14.5.2.3 [temp.mem]
1283 A member function template shall not be virtual. */
1285 ("invalid use of `virtual' in template declaration of `%#D'",
1287 DECL_VIRTUAL_P (decl) = 0;
1290 /* The debug-information generating code doesn't know what to do
1291 with member templates. */
1292 DECL_IGNORED_P (tmpl) = 1;
1295 cp_error ("template declaration of `%#D'", decl);
1298 /* Return true iff TYPE is a valid Java parameter or return type. */
1301 acceptable_java_type (type)
1304 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1306 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1308 type = TREE_TYPE (type);
1309 if (TREE_CODE (type) == RECORD_TYPE)
1312 if (! TYPE_FOR_JAVA (type))
1314 if (! CLASSTYPE_TEMPLATE_INFO (type))
1316 args = CLASSTYPE_TI_ARGS (type);
1317 i = TREE_VEC_LENGTH (args);
1320 type = TREE_VEC_ELT (args, i);
1321 if (TREE_CODE (type) == POINTER_TYPE)
1322 type = TREE_TYPE (type);
1323 if (! TYPE_FOR_JAVA (type))
1332 /* For a METHOD in a Java class CTYPE, return 1 if
1333 the parameter and return types are valid Java types.
1334 Otherwise, print appropriate error messages, and return 0. */
1337 check_java_method (method)
1341 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1342 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1343 if (! acceptable_java_type (ret_type))
1345 cp_error ("Java method '%D' has non-Java return type `%T'",
1349 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1351 tree type = TREE_VALUE (arg_types);
1352 if (! acceptable_java_type (type))
1354 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1359 return jerr ? 0 : 1;
1362 /* Sanity check: report error if this function FUNCTION is not
1363 really a member of the class (CTYPE) it is supposed to belong to.
1364 CNAME is the same here as it is for grokclassfn above. */
1367 check_classfn (ctype, function)
1368 tree ctype, function;
1370 tree fn_name = DECL_NAME (function);
1371 tree fndecl, fndecls;
1372 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1376 if (DECL_USE_TEMPLATE (function)
1377 && !(TREE_CODE (function) == TEMPLATE_DECL
1378 && DECL_TEMPLATE_SPECIALIZATION (function))
1379 && is_member_template (DECL_TI_TEMPLATE (function)))
1380 /* Since this is a specialization of a member template,
1381 we're not going to find the declaration in the class.
1384 struct S { template <typename T> void f(T); };
1385 template <> void S::f(int);
1387 we're not going to find `S::f(int)', but there's no
1388 reason we should, either. We let our callers know we didn't
1389 find the method, but we don't complain. */
1392 if (method_vec != 0)
1394 methods = &TREE_VEC_ELT (method_vec, 0);
1395 end = TREE_VEC_END (method_vec);
1397 /* First suss out ctors and dtors. */
1398 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1399 && DECL_CONSTRUCTOR_P (function))
1401 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1402 && DECL_DESTRUCTOR_P (function))
1405 while (++methods != end && *methods)
1408 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1411 for (fndecls = *methods; fndecls != NULL_TREE;
1412 fndecls = OVL_NEXT (fndecls))
1414 fndecl = OVL_CURRENT (fndecls);
1416 /* We cannot simply call decls_match because this
1417 doesn't work for static member functions that are
1418 pretending to be methods, and because the name
1419 may have been changed by asm("new_name"). */
1420 if (DECL_NAME (function) == DECL_NAME (fndecl))
1422 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1423 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1425 /* Get rid of the this parameter on functions that become
1427 if (DECL_STATIC_FUNCTION_P (fndecl)
1428 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1429 p1 = TREE_CHAIN (p1);
1431 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1432 TREE_TYPE (TREE_TYPE (fndecl)))
1433 && compparms (p1, p2)
1434 && (DECL_TEMPLATE_SPECIALIZATION (function)
1435 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1436 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1437 || (DECL_TI_TEMPLATE (function)
1438 == DECL_TI_TEMPLATE (fndecl))))
1447 if (methods != end && *methods)
1449 tree fndecl = *methods;
1450 cp_error ("prototype for `%#D' does not match any in class `%T'",
1452 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1453 OVL_CURRENT (fndecl));
1454 while (fndecl = OVL_NEXT (fndecl), fndecl)
1455 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1460 if (!COMPLETE_TYPE_P (ctype))
1461 incomplete_type_error (function, ctype);
1463 cp_error ("no `%#D' member function declared in class `%T'",
1467 /* If we did not find the method in the class, add it to avoid
1468 spurious errors (unless the CTYPE is not yet defined, in which
1469 case we'll only confuse ourselves when the function is declared
1470 properly within the class. */
1471 if (COMPLETE_TYPE_P (ctype))
1472 add_method (ctype, function, /*error_p=*/1);
1476 /* We have just processed the DECL, which is a static data member.
1477 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1478 present, is the assembly-language name for the data member.
1479 FLAGS is as for cp_finish_decl. */
1482 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1488 my_friendly_assert (TREE_PUBLIC (decl), 0);
1490 DECL_CONTEXT (decl) = current_class_type;
1492 /* We cannot call pushdecl here, because that would fill in the
1493 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1494 the right thing, namely, to put this decl out straight away. */
1495 /* current_class_type can be NULL_TREE in case of error. */
1496 if (!asmspec_tree && current_class_type)
1497 DECL_INITIAL (decl) = error_mark_node;
1499 if (! processing_template_decl)
1501 if (!pending_statics)
1502 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1503 VARRAY_PUSH_TREE (pending_statics, decl);
1506 /* Static consts need not be initialized in the class definition. */
1507 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1509 static int explained = 0;
1511 error ("initializer invalid for static member with constructor");
1514 error ("(an out of class initialization is required)");
1519 /* Force the compiler to know when an uninitialized static const
1520 member is being used. */
1521 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1522 TREE_USED (decl) = 1;
1523 DECL_INITIAL (decl) = init;
1524 DECL_IN_AGGR_P (decl) = 1;
1526 cp_finish_decl (decl, init, asmspec_tree, flags);
1529 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1530 of a structure component, returning a _DECL node.
1531 QUALS is a list of type qualifiers for this decl (such as for declaring
1532 const member functions).
1534 This is done during the parsing of the struct declaration.
1535 The _DECL nodes are chained together and the lot of them
1536 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1538 If class A defines that certain functions in class B are friends, then
1539 the way I have set things up, it is B who is interested in permission
1540 granted by A. However, it is in A's context that these declarations
1541 are parsed. By returning a void_type_node, class A does not attempt
1542 to incorporate the declarations of the friends within its structure.
1544 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1545 CHANGES TO CODE IN `start_method'. */
1548 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1549 tree declarator, declspecs, init, asmspec_tree, attrlist;
1551 register tree value;
1552 const char *asmspec = 0;
1553 int flags = LOOKUP_ONLYCONVERTING;
1555 /* Convert () initializers to = initializers. */
1556 if (init == NULL_TREE && declarator != NULL_TREE
1557 && TREE_CODE (declarator) == CALL_EXPR
1558 && TREE_OPERAND (declarator, 0)
1559 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1560 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1561 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1563 /* It's invalid to try to initialize a data member using a
1564 functional notation, e.g.:
1570 Explain that to the user. */
1571 static int explained;
1573 cp_error ("invalid data member initialization");
1576 cp_error ("(use `=' to initialize static data members)");
1580 declarator = TREE_OPERAND (declarator, 0);
1584 if (declspecs == NULL_TREE
1585 && TREE_CODE (declarator) == SCOPE_REF
1586 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1588 /* Access declaration */
1589 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1591 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1592 pop_nested_class ();
1593 return do_class_using_decl (declarator);
1597 && TREE_CODE (init) == TREE_LIST
1598 && TREE_VALUE (init) == error_mark_node
1599 && TREE_CHAIN (init) == NULL_TREE)
1602 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, attrlist);
1603 if (! value || value == error_mark_node)
1604 /* friend or constructor went bad. */
1606 if (TREE_TYPE (value) == error_mark_node)
1607 return error_mark_node;
1609 /* Pass friendly classes back. */
1610 if (TREE_CODE (value) == VOID_TYPE)
1611 return void_type_node;
1613 if (DECL_NAME (value) != NULL_TREE
1614 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1615 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1616 cp_error ("member `%D' conflicts with virtual function table field name",
1619 /* Stash away type declarations. */
1620 if (TREE_CODE (value) == TYPE_DECL)
1622 DECL_NONLOCAL (value) = 1;
1623 DECL_CONTEXT (value) = current_class_type;
1625 if (CLASS_TYPE_P (TREE_TYPE (value)))
1626 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1628 if (processing_template_decl)
1629 value = push_template_decl (value);
1634 if (DECL_IN_AGGR_P (value))
1636 cp_error ("`%D' is already defined in `%T'", value,
1637 DECL_CONTEXT (value));
1638 return void_type_node;
1642 asmspec = TREE_STRING_POINTER (asmspec_tree);
1646 if (TREE_CODE (value) == FUNCTION_DECL)
1648 grok_function_init (value, init);
1651 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1652 /* Already complained in grokdeclarator. */
1656 /* We allow initializers to become parameters to base
1658 if (TREE_CODE (init) == TREE_LIST)
1660 if (TREE_CHAIN (init) == NULL_TREE)
1661 init = TREE_VALUE (init);
1663 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1666 if (TREE_CODE (init) == CONST_DECL)
1667 init = DECL_INITIAL (init);
1668 else if (TREE_READONLY_DECL_P (init))
1669 init = decl_constant_value (init);
1670 else if (TREE_CODE (init) == CONSTRUCTOR)
1671 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1672 if (init == error_mark_node)
1673 /* We must make this look different than `error_mark_node'
1674 because `decl_const_value' would mis-interpret it
1675 as only meaning that this VAR_DECL is defined. */
1676 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1677 else if (processing_template_decl)
1679 else if (! TREE_CONSTANT (init))
1681 /* We can allow references to things that are effectively
1682 static, since references are initialized with the address. */
1683 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1684 || (TREE_STATIC (init) == 0
1685 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1687 error ("field initializer is not constant");
1688 init = error_mark_node;
1694 if (processing_template_decl && ! current_function_decl
1695 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1696 value = push_template_decl (value);
1699 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1700 TREE_VALUE (attrlist));
1702 if (TREE_CODE (value) == VAR_DECL)
1704 finish_static_data_member_decl (value, init, asmspec_tree,
1708 if (TREE_CODE (value) == FIELD_DECL)
1711 cp_error ("`asm' specifiers are not permitted on non-static data members");
1712 if (DECL_INITIAL (value) == error_mark_node)
1713 init = error_mark_node;
1714 cp_finish_decl (value, init, NULL_TREE, flags);
1715 DECL_INITIAL (value) = init;
1716 DECL_IN_AGGR_P (value) = 1;
1719 if (TREE_CODE (value) == FUNCTION_DECL)
1723 /* This must override the asm specifier which was placed
1724 by grokclassfn. Lay this out fresh. */
1725 SET_DECL_RTL (value, NULL_RTX);
1726 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1728 cp_finish_decl (value, init, asmspec_tree, flags);
1730 /* Pass friends back this way. */
1731 if (DECL_FRIEND_P (value))
1732 return void_type_node;
1734 DECL_IN_AGGR_P (value) = 1;
1737 my_friendly_abort (21);
1742 /* Like `grokfield', but for bitfields.
1743 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1746 grokbitfield (declarator, declspecs, width)
1747 tree declarator, declspecs, width;
1749 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1752 if (! value) return NULL_TREE; /* friends went bad. */
1754 /* Pass friendly classes back. */
1755 if (TREE_CODE (value) == VOID_TYPE)
1756 return void_type_node;
1758 if (TREE_CODE (value) == TYPE_DECL)
1760 cp_error ("cannot declare `%D' to be a bitfield type", value);
1764 /* Usually, finish_struct_1 catches bitifields with invalid types.
1765 But, in the case of bitfields with function type, we confuse
1766 ourselves into thinking they are member functions, so we must
1768 if (TREE_CODE (value) == FUNCTION_DECL)
1770 cp_error ("cannot declare bitfield `%D' with funcion type",
1775 if (DECL_IN_AGGR_P (value))
1777 cp_error ("`%D' is already defined in the class %T", value,
1778 DECL_CONTEXT (value));
1779 return void_type_node;
1782 GNU_xref_member (current_class_name, value);
1784 if (TREE_STATIC (value))
1786 cp_error ("static member `%D' cannot be a bitfield", value);
1789 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1791 if (width != error_mark_node)
1793 constant_expression_warning (width);
1794 DECL_INITIAL (value) = width;
1795 SET_DECL_C_BIT_FIELD (value);
1798 DECL_IN_AGGR_P (value) = 1;
1803 grokoptypename (declspecs, declarator)
1804 tree declspecs, declarator;
1806 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1807 return mangle_conv_op_name_for_type (t);
1810 /* When a function is declared with an initializer,
1811 do the right thing. Currently, there are two possibilities:
1816 // initialization possibility #1.
1817 virtual void f () = 0;
1839 // initialization possibility #2
1846 copy_assignment_arg_p (parmtype, virtualp)
1848 int virtualp ATTRIBUTE_UNUSED;
1850 if (current_class_type == NULL_TREE)
1853 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1854 parmtype = TREE_TYPE (parmtype);
1856 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1858 /* Non-standard hack to support old Booch components. */
1859 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1868 grok_function_init (decl, init)
1872 /* An initializer for a function tells how this function should
1874 tree type = TREE_TYPE (decl);
1876 if (TREE_CODE (type) == FUNCTION_TYPE)
1877 cp_error ("initializer specified for non-member function `%D'", decl);
1878 else if (integer_zerop (init))
1880 DECL_PURE_VIRTUAL_P (decl) = 1;
1881 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
1884 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1886 if (copy_assignment_arg_p (parmtype, 1))
1887 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1891 cp_error ("invalid initializer for virtual method `%D'", decl);
1895 cplus_decl_attributes (decl, attributes, prefix_attributes)
1896 tree decl, attributes, prefix_attributes;
1898 if (decl == NULL_TREE || decl == void_type_node)
1901 if (TREE_CODE (decl) == TEMPLATE_DECL)
1902 decl = DECL_TEMPLATE_RESULT (decl);
1904 decl_attributes (decl, attributes, prefix_attributes);
1906 if (TREE_CODE (decl) == TYPE_DECL)
1907 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
1910 /* CONSTRUCTOR_NAME:
1911 Return the name for the constructor (or destructor) for the
1912 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1913 IDENTIFIER_NODE. When given a template, this routine doesn't
1914 lose the specialization. */
1917 constructor_name_full (thing)
1920 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1921 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1922 || TREE_CODE (thing) == TYPENAME_TYPE)
1923 thing = TYPE_NAME (thing);
1924 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1926 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1927 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1929 thing = TYPE_NAME (thing);
1931 if (TREE_CODE (thing) == TYPE_DECL
1932 || (TREE_CODE (thing) == TEMPLATE_DECL
1933 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1934 thing = DECL_NAME (thing);
1935 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1939 /* CONSTRUCTOR_NAME:
1940 Return the name for the constructor (or destructor) for the
1941 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1942 IDENTIFIER_NODE. When given a template, return the plain
1943 unspecialized name. */
1946 constructor_name (thing)
1950 thing = constructor_name_full (thing);
1951 t = IDENTIFIER_TEMPLATE (thing);
1957 /* Defer the compilation of the FN until the end of compilation. */
1963 if (DECL_DEFERRED_FN (fn))
1965 DECL_DEFERRED_FN (fn) = 1;
1967 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1969 VARRAY_PUSH_TREE (deferred_fns, fn);
1972 /* Hand off a unique name which can be used for variable we don't really
1973 want to know about anyway, for example, the anonymous variables which
1974 are needed to make references work. Declare this thing so we can use it.
1975 The variable created will be of type TYPE, and will have internal
1979 get_temp_name (type)
1982 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
1984 int toplev = toplevel_bindings_p ();
1986 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
1987 decl = build_decl (VAR_DECL, get_identifier (buf), type);
1988 DECL_ARTIFICIAL (decl) = 1;
1989 TREE_USED (decl) = 1;
1990 TREE_STATIC (decl) = 1;
1992 decl = pushdecl_top_level (decl);
1994 /* If this is a local variable, then lay out its rtl now.
1995 Otherwise, callers of this function are responsible for dealing
1996 with this variable's rtl. */
2000 my_friendly_assert (DECL_INITIAL (decl) == NULL_TREE,
2007 /* Hunts through the global anonymous union ANON_DECL, building
2008 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2009 returns a VAR_DECL whose size is the same as the size of the
2010 ANON_DECL, if one is available. */
2013 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2019 tree type = TREE_TYPE (anon_decl);
2020 tree main_decl = NULL_TREE;
2023 /* Rather than write the code to handle the non-union case,
2024 just give an error. */
2025 if (TREE_CODE (type) != UNION_TYPE)
2026 error ("anonymous struct not inside named type");
2028 for (field = TYPE_FIELDS (type);
2030 field = TREE_CHAIN (field))
2034 if (DECL_ARTIFICIAL (field))
2036 if (TREE_CODE (field) != FIELD_DECL)
2038 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2043 if (TREE_PRIVATE (field))
2044 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2045 else if (TREE_PROTECTED (field))
2046 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2048 if (DECL_NAME (field) == NULL_TREE
2049 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2051 decl = build_anon_union_vars (field, elems, static_p, external_p);
2055 else if (DECL_NAME (field) == NULL_TREE)
2059 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2060 /* tell `pushdecl' that this is not tentative. */
2061 DECL_INITIAL (decl) = error_mark_node;
2062 TREE_PUBLIC (decl) = 0;
2063 TREE_STATIC (decl) = static_p;
2064 DECL_EXTERNAL (decl) = external_p;
2065 decl = pushdecl (decl);
2066 DECL_INITIAL (decl) = NULL_TREE;
2069 /* Only write out one anon union element--choose the largest
2070 one. We used to try to find one the same size as the union,
2071 but that fails if the ABI forces us to align the union more
2073 if (main_decl == NULL_TREE
2074 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
2077 TREE_ASM_WRITTEN (main_decl) = 1;
2081 /* ??? This causes there to be no debug info written out
2083 TREE_ASM_WRITTEN (decl) = 1;
2085 if (DECL_NAME (field) == NULL_TREE
2086 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2087 /* The remainder of the processing was already done in the
2091 /* If there's a cleanup to do, it belongs in the
2092 TREE_PURPOSE of the following TREE_LIST. */
2093 *elems = tree_cons (NULL_TREE, decl, *elems);
2094 TREE_TYPE (*elems) = type;
2100 /* Finish off the processing of a UNION_TYPE structure. If the union is an
2101 anonymous union, then all members must be laid out together. PUBLIC_P
2102 is nonzero if this union is not declared static. */
2105 finish_anon_union (anon_union_decl)
2106 tree anon_union_decl;
2108 tree type = TREE_TYPE (anon_union_decl);
2110 int public_p = TREE_PUBLIC (anon_union_decl);
2111 int static_p = TREE_STATIC (anon_union_decl);
2112 int external_p = DECL_EXTERNAL (anon_union_decl);
2114 if (TYPE_FIELDS (type) == NULL_TREE)
2119 error ("namespace-scope anonymous aggregates must be static");
2123 main_decl = build_anon_union_vars (anon_union_decl,
2124 &DECL_ANON_UNION_ELEMS (anon_union_decl),
2125 static_p, external_p);
2127 if (main_decl == NULL_TREE)
2129 warning ("anonymous aggregate with no members");
2135 make_decl_rtl (main_decl, 0);
2136 COPY_DECL_RTL (main_decl, anon_union_decl);
2137 expand_anon_union_decl (anon_union_decl,
2139 DECL_ANON_UNION_ELEMS (anon_union_decl));
2142 add_decl_stmt (anon_union_decl);
2145 /* Finish processing a builtin type TYPE. It's name is NAME,
2146 its fields are in the array FIELDS. LEN is the number of elements
2147 in FIELDS minus one, or put another way, it is the maximum subscript
2150 It is given the same alignment as ALIGN_TYPE. */
2153 finish_builtin_type (type, name, fields, len, align_type)
2162 TYPE_FIELDS (type) = fields[0];
2163 for (i = 0; i < len; i++)
2165 layout_type (TREE_TYPE (fields[i]));
2166 DECL_FIELD_CONTEXT (fields[i]) = type;
2167 TREE_CHAIN (fields[i]) = fields[i+1];
2169 DECL_FIELD_CONTEXT (fields[i]) = type;
2170 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2171 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2173 #if 0 /* not yet, should get fixed properly later */
2174 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2176 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2178 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2179 layout_decl (TYPE_NAME (type), 0);
2182 /* Auxiliary functions to make type signatures for
2183 `operator new' and `operator delete' correspond to
2184 what compiler will be expecting. */
2187 coerce_new_type (type)
2191 tree args = TYPE_ARG_TYPES (type);
2193 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2195 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2196 e = 1, cp_error ("`operator new' must return type `%T'", ptr_type_node);
2198 if (!args || args == void_list_node
2199 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2202 if (args && args != void_list_node)
2203 args = TREE_CHAIN (args);
2204 cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2209 args = tree_cons (NULL_TREE, c_size_type_node, args);
2212 type = build_exception_variant
2213 (build_function_type (ptr_type_node, args),
2214 TYPE_RAISES_EXCEPTIONS (type));
2222 coerce_delete_type (type)
2226 tree args = TYPE_ARG_TYPES (type);
2228 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2230 if (!same_type_p (TREE_TYPE (type), void_type_node))
2231 e = 1, cp_error ("`operator delete' must return type `%T'", void_type_node);
2233 if (!args || args == void_list_node
2234 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2237 if (args && args != void_list_node)
2238 args = TREE_CHAIN (args);
2239 cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2244 args = tree_cons (NULL_TREE, ptr_type_node, args);
2247 type = build_exception_variant
2248 (build_function_type (void_type_node, args),
2249 TYPE_RAISES_EXCEPTIONS (type));
2258 mark_vtable_entries (decl)
2261 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2263 for (; entries; entries = TREE_CHAIN (entries))
2268 fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2269 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2271 if (TREE_CODE (fnaddr) != ADDR_EXPR)
2272 /* This entry is an offset: a virtual base class offset, a
2273 virtual call offset, an RTTI offset, etc. */
2276 fn = TREE_OPERAND (fnaddr, 0);
2277 TREE_ADDRESSABLE (fn) = 1;
2278 /* When we don't have vcall offsets, we output thunks whenever
2279 we output the vtables that contain them. With vcall offsets,
2280 we know all the thunks we'll need when we emit a virtual
2281 function, so we emit the thunks there instead. */
2282 if (DECL_THUNK_P (fn))
2283 use_thunk (fn, THUNK_GENERATE_WITH_VTABLE_P (fn));
2288 /* Set DECL up to have the closest approximation of "initialized common"
2289 linkage available. */
2292 comdat_linkage (decl)
2296 make_decl_one_only (decl);
2297 else if (TREE_CODE (decl) == FUNCTION_DECL
2298 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2299 /* We can just emit function and compiler-generated variables
2300 statically; having multiple copies is (for the most part) only
2303 There are two correctness issues, however: the address of a
2304 template instantiation with external linkage should be the
2305 same, independent of what translation unit asks for the
2306 address, and this will not hold when we emit multiple copies of
2307 the function. However, there's little else we can do.
2309 Also, by default, the typeinfo implementation for the new ABI
2310 assumes that there will be only one copy of the string used as
2311 the name for each type. Therefore, if weak symbols are
2312 unavailable, the run-time library should perform a more
2313 conservative check; it should perform a string comparison,
2314 rather than an address comparison. */
2315 TREE_PUBLIC (decl) = 0;
2318 /* Static data member template instantiations, however, cannot
2319 have multiple copies. */
2320 if (DECL_INITIAL (decl) == 0
2321 || DECL_INITIAL (decl) == error_mark_node)
2322 DECL_COMMON (decl) = 1;
2323 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2325 DECL_COMMON (decl) = 1;
2326 DECL_INITIAL (decl) = error_mark_node;
2330 /* We can't do anything useful; leave vars for explicit
2332 DECL_EXTERNAL (decl) = 1;
2333 DECL_NOT_REALLY_EXTERN (decl) = 0;
2337 if (DECL_LANG_SPECIFIC (decl))
2338 DECL_COMDAT (decl) = 1;
2341 /* For win32 we also want to put explicit instantiations in
2342 linkonce sections, so that they will be merged with implicit
2343 instantiations; otherwise we get duplicate symbol errors. */
2346 maybe_make_one_only (decl)
2349 /* We used to say that this was not necessary on targets that support weak
2350 symbols, because the implicit instantiations will defer to the explicit
2351 one. However, that's not actually the case in SVR4; a strong definition
2352 after a weak one is an error. Also, not making explicit
2353 instantiations one_only means that we can end up with two copies of
2354 some template instantiations. */
2355 if (! supports_one_only ())
2358 /* We can't set DECL_COMDAT on functions, or finish_file will think
2359 we can get away with not emitting them if they aren't used. We need
2360 to for variables so that cp_finish_decl will update their linkage,
2361 because their DECL_INITIAL may not have been set properly yet. */
2363 make_decl_one_only (decl);
2365 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2366 DECL_COMDAT (decl) = 1;
2369 /* Returns the virtual function with which the vtable for TYPE is
2370 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2378 if (TYPE_FOR_JAVA (type)
2379 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2380 || CLASSTYPE_INTERFACE_KNOWN (type))
2383 for (method = TYPE_METHODS (type); method != NULL_TREE;
2384 method = TREE_CHAIN (method))
2385 if (DECL_VINDEX (method) != NULL_TREE
2386 && ! DECL_DECLARED_INLINE_P (method)
2387 && (! DECL_PURE_VIRTUAL_P (method) || DECL_DESTRUCTOR_P (method)))
2393 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2394 based on TYPE and other static flags.
2396 Note that anything public is tagged TREE_PUBLIC, whether
2397 it's public in this file or in another one. */
2400 import_export_vtable (decl, type, final)
2404 if (DECL_INTERFACE_KNOWN (decl))
2407 if (TYPE_FOR_JAVA (type))
2409 TREE_PUBLIC (decl) = 1;
2410 DECL_EXTERNAL (decl) = 1;
2411 DECL_INTERFACE_KNOWN (decl) = 1;
2413 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2415 TREE_PUBLIC (decl) = 1;
2416 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2417 DECL_INTERFACE_KNOWN (decl) = 1;
2421 /* We can only wait to decide if we have real non-inline virtual
2422 functions in our class, or if we come from a template. */
2424 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2425 || key_method (type));
2427 if (final || ! found)
2429 comdat_linkage (decl);
2430 DECL_EXTERNAL (decl) = 0;
2434 TREE_PUBLIC (decl) = 1;
2435 DECL_EXTERNAL (decl) = 1;
2440 /* Determine whether or not we want to specifically import or export CTYPE,
2441 using various heuristics. */
2444 import_export_class (ctype)
2447 /* -1 for imported, 1 for exported. */
2448 int import_export = 0;
2450 /* It only makes sense to call this function at EOF. The reason is
2451 that this function looks at whether or not the first non-inline
2452 non-abstract virtual member function has been defined in this
2453 translation unit. But, we can't possibly know that until we've
2454 seen the entire translation unit. */
2455 my_friendly_assert (at_eof, 20000226);
2457 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2460 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2461 we will have CLASSTYPE_INTERFACE_ONLY set but not
2462 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2463 heuristic because someone will supply a #pragma implementation
2464 elsewhere, and deducing it here would produce a conflict. */
2465 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2468 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2469 /* FIXME this should really use some sort of target-independent macro. */
2470 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2472 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2476 /* If we got -fno-implicit-templates, we import template classes that
2477 weren't explicitly instantiated. */
2478 if (import_export == 0
2479 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2480 && ! flag_implicit_templates)
2483 /* Base our import/export status on that of the first non-inline,
2484 non-pure virtual function, if any. */
2485 if (import_export == 0
2486 && TYPE_POLYMORPHIC_P (ctype))
2488 tree method = key_method (ctype);
2490 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2493 #ifdef MULTIPLE_SYMBOL_SPACES
2494 if (import_export == -1)
2500 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2501 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2505 /* We need to describe to the assembler the relationship between
2506 a vtable and the vtable of the parent class. */
2509 output_vtable_inherit (vars)
2515 op[0] = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2517 parent = binfo_for_vtable (vars);
2519 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2523 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2524 op[1] = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2527 my_friendly_abort (980826);
2529 output_asm_insn (".vtable_inherit %c0, %c1", op);
2533 finish_vtable_vardecl (t, data)
2535 void *data ATTRIBUTE_UNUSED;
2538 tree ctype = DECL_CONTEXT (vars);
2539 import_export_class (ctype);
2540 import_export_vtable (vars, ctype, 1);
2542 if (! DECL_EXTERNAL (vars)
2543 && DECL_NEEDED_P (vars)
2544 && ! TREE_ASM_WRITTEN (vars))
2546 if (TREE_TYPE (vars) == void_type_node)
2547 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2551 mark_vtable_entries (vars);
2552 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2553 store_init_value (vars, DECL_INITIAL (vars));
2555 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2557 /* Mark the VAR_DECL node representing the vtable itself as a
2558 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2559 It is rather important that such things be ignored because
2560 any effort to actually generate DWARF for them will run
2561 into trouble when/if we encounter code like:
2564 struct S { virtual void member (); };
2566 because the artificial declaration of the vtable itself (as
2567 manufactured by the g++ front end) will say that the vtable
2568 is a static member of `S' but only *after* the debug output
2569 for the definition of `S' has already been output. This causes
2570 grief because the DWARF entry for the definition of the vtable
2571 will try to refer back to an earlier *declaration* of the
2572 vtable as a static member of `S' and there won't be one.
2573 We might be able to arrange to have the "vtable static member"
2574 attached to the member list for `S' before the debug info for
2575 `S' get written (which would solve the problem) but that would
2576 require more intrusive changes to the g++ front end. */
2578 DECL_IGNORED_P (vars) = 1;
2581 /* Always make vtables weak. */
2583 comdat_linkage (vars);
2585 rest_of_decl_compilation (vars, NULL, 1, 1);
2588 output_vtable_inherit (vars);
2590 /* Because we're only doing syntax-checking, we'll never end up
2591 actually marking the variable as written. */
2592 if (flag_syntax_only)
2593 TREE_ASM_WRITTEN (vars) = 1;
2595 /* Since we're writing out the vtable here, also write the debug
2597 note_debug_info_needed (ctype);
2602 /* If the references to this class' vtables were optimized away, still
2603 emit the appropriate debugging information. See dfs_debug_mark. */
2604 if (DECL_COMDAT (vars)
2605 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2606 note_debug_info_needed (ctype);
2612 prune_vtable_vardecl (t, data)
2614 void *data ATTRIBUTE_UNUSED;
2616 *t = TREE_CHAIN (*t);
2620 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2621 inline function or template instantiation at end-of-file. */
2624 import_export_decl (decl)
2627 if (DECL_INTERFACE_KNOWN (decl))
2630 if (DECL_TEMPLATE_INSTANTIATION (decl)
2631 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2633 DECL_NOT_REALLY_EXTERN (decl) = 1;
2634 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2635 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2636 && (flag_implicit_templates
2637 || (flag_implicit_inline_templates
2638 && DECL_DECLARED_INLINE_P (decl))))
2640 if (!TREE_PUBLIC (decl))
2641 /* Templates are allowed to have internal linkage. See
2645 comdat_linkage (decl);
2648 DECL_NOT_REALLY_EXTERN (decl) = 0;
2650 else if (DECL_FUNCTION_MEMBER_P (decl))
2652 if (!DECL_DECLARED_INLINE_P (decl))
2654 tree ctype = DECL_CONTEXT (decl);
2655 import_export_class (ctype);
2656 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2658 DECL_NOT_REALLY_EXTERN (decl)
2659 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2660 || (DECL_DECLARED_INLINE_P (decl)
2661 && ! flag_implement_inlines
2662 && !DECL_VINDEX (decl)));
2664 /* Always make artificials weak. */
2665 if (DECL_ARTIFICIAL (decl) && flag_weak)
2666 comdat_linkage (decl);
2668 maybe_make_one_only (decl);
2672 comdat_linkage (decl);
2674 else if (tinfo_decl_p (decl, 0))
2676 tree ctype = TREE_TYPE (DECL_NAME (decl));
2678 if (IS_AGGR_TYPE (ctype))
2679 import_export_class (ctype);
2681 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2682 && TYPE_POLYMORPHIC_P (ctype)
2683 /* If -fno-rtti, we're not necessarily emitting this stuff with
2684 the class, so go ahead and emit it now. This can happen
2685 when a class is used in exception handling. */
2687 /* If the type is a cv-qualified variant of a type, then we
2688 must emit the tinfo function in this translation unit
2689 since it will not be emitted when the vtable for the type
2690 is output (which is when the unqualified version is
2692 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2694 DECL_NOT_REALLY_EXTERN (decl)
2695 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2696 || (DECL_DECLARED_INLINE_P (decl)
2697 && ! flag_implement_inlines
2698 && !DECL_VINDEX (decl)));
2700 /* Always make artificials weak. */
2702 comdat_linkage (decl);
2704 else if (TYPE_BUILT_IN (ctype)
2705 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2706 DECL_NOT_REALLY_EXTERN (decl) = 0;
2708 comdat_linkage (decl);
2711 comdat_linkage (decl);
2713 DECL_INTERFACE_KNOWN (decl) = 1;
2717 build_cleanup (decl)
2721 tree type = TREE_TYPE (decl);
2723 if (TREE_CODE (type) == ARRAY_TYPE)
2727 mark_addressable (decl);
2728 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2730 temp = build_delete (TREE_TYPE (temp), temp,
2731 sfk_complete_destructor,
2732 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2736 /* Returns the initialization guard variable for the variable DECL,
2737 which has static storage duration. */
2746 sname = mangle_guard_variable (decl);
2747 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2752 /* Under the new ABI, we use a type that is big enough to
2753 contain a mutex as well as an integer counter. */
2754 guard_type = long_long_integer_type_node;
2755 guard = build_decl (VAR_DECL, sname, guard_type);
2757 /* The guard should have the same linkage as what it guards. */
2758 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2759 TREE_STATIC (guard) = TREE_STATIC (decl);
2760 DECL_COMMON (guard) = DECL_COMMON (decl);
2761 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2762 if (TREE_PUBLIC (decl))
2763 DECL_WEAK (guard) = DECL_WEAK (decl);
2765 DECL_ARTIFICIAL (guard) = 1;
2766 TREE_USED (guard) = 1;
2767 pushdecl_top_level (guard);
2768 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2773 /* Return those bits of the GUARD variable that should be set when the
2774 guarded entity is actually initialized. */
2777 get_guard_bits (guard)
2780 /* Under the new ABI, we only set the first byte of the guard,
2781 in order to leave room for a mutex in the high-order bits. */
2782 guard = build1 (ADDR_EXPR,
2783 build_pointer_type (TREE_TYPE (guard)),
2785 guard = build1 (NOP_EXPR,
2786 build_pointer_type (char_type_node),
2788 guard = build1 (INDIRECT_REF, char_type_node, guard);
2793 /* Return an expression which determines whether or not the GUARD
2794 variable has already been initialized. */
2797 get_guard_cond (guard)
2802 /* Check to see if the GUARD is zero. */
2803 guard = get_guard_bits (guard);
2804 guard_value = integer_zero_node;
2805 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2806 guard_value = convert (TREE_TYPE (guard), guard_value);
2807 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2810 /* Return an expression which sets the GUARD variable, indicating that
2811 the variable being guarded has been initialized. */
2819 /* Set the GUARD to one. */
2820 guard = get_guard_bits (guard);
2821 guard_init = integer_one_node;
2822 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2823 guard_init = convert (TREE_TYPE (guard), guard_init);
2824 return build_modify_expr (guard, NOP_EXPR, guard_init);
2827 /* Start the process of running a particular set of global constructors
2828 or destructors. Subroutine of do_[cd]tors. */
2831 start_objects (method_type, initp)
2832 int method_type, initp;
2838 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2840 if (initp != DEFAULT_INIT_PRIORITY)
2850 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2853 sprintf (type, "%c", method_type);
2855 fnname = get_file_function_name_long (type);
2857 start_function (void_list_node,
2858 make_call_declarator (fnname, void_list_node, NULL_TREE,
2860 NULL_TREE, SF_DEFAULT);
2862 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2863 /* It can be a static function as long as collect2 does not have
2864 to scan the object file to find its ctor/dtor routine. */
2865 TREE_PUBLIC (current_function_decl) = 0;
2868 /* Mark this declaration as used to avoid spurious warnings. */
2869 TREE_USED (current_function_decl) = 1;
2871 /* Mark this function as a global constructor or destructor. */
2872 if (method_type == 'I')
2873 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2875 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2876 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2878 body = begin_compound_stmt (/*has_no_scope=*/0);
2880 /* We cannot allow these functions to be elided, even if they do not
2881 have external linkage. And, there's no point in deferring
2882 copmilation of thes functions; they're all going to have to be
2884 current_function_cannot_inline
2885 = "static constructors and destructors cannot be inlined";
2890 /* Finish the process of running a particular set of global constructors
2891 or destructors. Subroutine of do_[cd]tors. */
2894 finish_objects (method_type, initp, body)
2895 int method_type, initp;
2902 finish_compound_stmt (/*has_no_scope=*/0, body);
2903 fn = finish_function (0);
2906 /* When only doing semantic analysis, and no RTL generation, we
2907 can't call functions that directly emit assembly code; there is
2908 no assembly file in which to put the code. */
2909 if (flag_syntax_only)
2912 fnname = XSTR (XEXP (DECL_RTL (fn), 0), 0);
2913 if (initp == DEFAULT_INIT_PRIORITY)
2915 if (method_type == 'I')
2916 assemble_constructor (fnname);
2918 assemble_destructor (fnname);
2920 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
2921 /* If we're using init priority we can't use assemble_*tor, but on ELF
2922 targets we can stick the references into named sections for GNU ld
2927 sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
2928 /* invert the numbering so the linker puts us in the proper
2929 order; constructors are run from right to left, and the
2930 linker sorts in increasing order. */
2931 MAX_INIT_PRIORITY - initp);
2932 named_section (NULL_TREE, buf, 0);
2933 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
2934 POINTER_SIZE / BITS_PER_UNIT, 1);
2939 /* The names of the parameters to the function created to handle
2940 initializations and destructions for objects with static storage
2942 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2943 #define PRIORITY_IDENTIFIER "__priority"
2945 /* The name of the function we create to handle initializations and
2946 destructions for objects with static storage duration. */
2947 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2949 /* The declaration for the __INITIALIZE_P argument. */
2950 static tree initialize_p_decl;
2952 /* The declaration for the __PRIORITY argument. */
2953 static tree priority_decl;
2955 /* The declaration for the static storage duration function. */
2956 static tree ssdf_decl;
2958 /* All the static storage duration functions created in this
2959 translation unit. */
2960 static varray_type ssdf_decls;
2962 /* A map from priority levels to information about that priority
2963 level. There may be many such levels, so efficient lookup is
2965 static splay_tree priority_info_map;
2967 /* Begins the generation of the function that will handle all
2968 initialization and destruction of objects with static storage
2969 duration. The function generated takes two parameters of type
2970 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2971 non-zero, it performs initializations. Otherwise, it performs
2972 destructions. It only performs those initializations or
2973 destructions with the indicated __PRIORITY. The generated function
2976 It is assumed that this function will only be called once per
2977 translation unit. */
2980 start_static_storage_duration_function ()
2982 static unsigned ssdf_number;
2987 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2989 /* Create the identifier for this function. It will be of the form
2990 SSDF_IDENTIFIER_<number>. */
2991 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2992 if (ssdf_number == 0)
2994 /* Overflow occurred. That means there are at least 4 billion
2995 initialization functions. */
2996 sorry ("too many initialization functions required");
2997 my_friendly_abort (19990430);
3000 /* Create the parameters. */
3001 parm_types = void_list_node;
3002 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3003 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
3004 type = build_function_type (void_type_node, parm_types);
3006 /* Create the FUNCTION_DECL itself. */
3007 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3008 get_identifier (id),
3010 TREE_PUBLIC (ssdf_decl) = 0;
3011 DECL_ARTIFICIAL (ssdf_decl) = 1;
3013 /* Put this function in the list of functions to be called from the
3014 static constructors and destructors. */
3017 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
3019 /* Take this opportunity to initialize the map from priority
3020 numbers to information about that priority level. */
3021 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3022 /*delete_key_fn=*/0,
3023 /*delete_value_fn=*/
3024 (splay_tree_delete_value_fn) &free);
3026 /* We always need to generate functions for the
3027 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3028 priorities later, we'll be sure to find the
3029 DEFAULT_INIT_PRIORITY. */
3030 get_priority_info (DEFAULT_INIT_PRIORITY);
3033 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
3035 /* Create the argument list. */
3036 initialize_p_decl = build_decl (PARM_DECL,
3037 get_identifier (INITIALIZE_P_IDENTIFIER),
3039 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3040 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
3041 TREE_USED (initialize_p_decl) = 1;
3042 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
3044 DECL_CONTEXT (priority_decl) = ssdf_decl;
3045 DECL_ARG_TYPE (priority_decl) = integer_type_node;
3046 TREE_USED (priority_decl) = 1;
3048 TREE_CHAIN (initialize_p_decl) = priority_decl;
3049 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3051 /* Put the function in the global scope. */
3052 pushdecl (ssdf_decl);
3054 /* Start the function itself. This is equivalent to declarating the
3057 static void __ssdf (int __initialize_p, init __priority_p);
3059 It is static because we only need to call this function from the
3060 various constructor and destructor functions for this module. */
3061 start_function (/*specs=*/NULL_TREE,
3063 /*attrs=*/NULL_TREE,
3066 /* Set up the scope of the outermost block in the function. */
3067 body = begin_compound_stmt (/*has_no_scope=*/0);
3069 /* This function must not be deferred because we are depending on
3070 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3071 current_function_cannot_inline
3072 = "static storage duration functions cannot be inlined";
3077 /* Finish the generation of the function which performs initialization
3078 and destruction of objects with static storage duration. After
3079 this point, no more such objects can be created. */
3082 finish_static_storage_duration_function (body)
3085 /* Close out the function. */
3086 finish_compound_stmt (/*has_no_scope=*/0, body);
3087 expand_body (finish_function (0));
3090 /* Return the information about the indicated PRIORITY level. If no
3091 code to handle this level has yet been generated, generate the
3092 appropriate prologue. */
3094 static priority_info
3095 get_priority_info (priority)
3101 n = splay_tree_lookup (priority_info_map,
3102 (splay_tree_key) priority);
3105 /* Create a new priority information structure, and insert it
3107 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
3108 pi->initializations_p = 0;
3109 pi->destructions_p = 0;
3110 splay_tree_insert (priority_info_map,
3111 (splay_tree_key) priority,
3112 (splay_tree_value) pi);
3115 pi = (priority_info) n->value;
3120 /* Set up to handle the initialization or destruction of DECL. If
3121 INITP is non-zero, we are initializing the variable. Otherwise, we
3122 are destroying it. */
3125 start_static_initialization_or_destruction (decl, initp)
3129 tree guard_if_stmt = NULL_TREE;
3136 /* Figure out the priority for this declaration. */
3137 priority = DECL_INIT_PRIORITY (decl);
3139 priority = DEFAULT_INIT_PRIORITY;
3141 /* Remember that we had an initialization or finalization at this
3143 pi = get_priority_info (priority);
3145 pi->initializations_p = 1;
3147 pi->destructions_p = 1;
3149 /* Trick the compiler into thinking we are at the file and line
3150 where DECL was declared so that error-messages make sense, and so
3151 that the debugger will show somewhat sensible file and line
3153 input_filename = DECL_SOURCE_FILE (decl);
3154 lineno = DECL_SOURCE_LINE (decl);
3160 Access control for implicit calls to the constructors,
3161 the conversion functions, or the destructor called to
3162 create and destroy a static data member is performed as
3163 if these calls appeared in the scope of the member's
3166 we pretend we are in a static member function of the class of
3167 which the DECL is a member. */
3168 if (member_p (decl))
3170 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3171 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3174 /* Conditionalize this initialization on being in the right priority
3175 and being initializing/finalizing appropriately. */
3176 guard_if_stmt = begin_if_stmt ();
3177 cond = cp_build_binary_op (EQ_EXPR,
3179 build_int_2 (priority, 0));
3180 init_cond = initp ? integer_one_node : integer_zero_node;
3181 init_cond = cp_build_binary_op (EQ_EXPR,
3184 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3186 /* Assume we don't need a guard. */
3188 /* We need a guard if this is an object with external linkage that
3189 might be initialized in more than one place. (For example, a
3190 static data member of a template, when the data member requires
3192 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3193 || DECL_ONE_ONLY (decl)
3194 || DECL_WEAK (decl)))
3198 guard = get_guard (decl);
3200 /* When using __cxa_atexit, we just check the GUARD as we would
3201 for a local static. */
3202 if (flag_use_cxa_atexit)
3204 /* When using __cxa_atexit, we never try to destroy
3205 anything from a static destructor. */
3206 my_friendly_assert (initp, 20000629);
3207 guard_cond = get_guard_cond (guard);
3209 /* If we don't have __cxa_atexit, then we will be running
3210 destructors from .fini sections, or their equivalents. So,
3211 we need to know how many times we've tried to initialize this
3212 object. We do initializations only if the GUARD is zero,
3213 i.e., if we are the first to initialize the variable. We do
3214 destructions only if the GUARD is one, i.e., if we are the
3215 last to destroy the variable. */
3218 = cp_build_binary_op (EQ_EXPR,
3219 build_unary_op (PREINCREMENT_EXPR,
3225 = cp_build_binary_op (EQ_EXPR,
3226 build_unary_op (PREDECREMENT_EXPR,
3231 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3234 finish_if_stmt_cond (cond, guard_if_stmt);
3236 /* If we're using __cxa_atexit, we have not already set the GUARD,
3237 so we must do so now. */
3238 if (guard && initp && flag_use_cxa_atexit)
3239 finish_expr_stmt (set_guard (guard));
3241 return guard_if_stmt;
3244 /* We've just finished generating code to do an initialization or
3245 finalization. GUARD_IF_STMT is the if-statement we used to guard
3246 the initialization. */
3249 finish_static_initialization_or_destruction (guard_if_stmt)
3252 finish_then_clause (guard_if_stmt);
3255 /* Now that we're done with DECL we don't need to pretend to be a
3256 member of its class any longer. */
3257 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3258 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3261 /* Generate code to do the static initialization of DECL. The
3262 initialization is INIT. If DECL may be initialized more than once
3263 in different object files, GUARD is the guard variable to
3264 check. PRIORITY is the priority for the initialization. */
3267 do_static_initialization (decl, init)
3274 /* Set up for the initialization. */
3276 = start_static_initialization_or_destruction (decl,
3279 /* Do the initialization itself. */
3280 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3281 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3282 expr = build_aggr_init (decl, init, 0);
3285 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3286 TREE_SIDE_EFFECTS (expr) = 1;
3288 finish_expr_stmt (expr);
3290 /* If we're using __cxa_atexit, register a a function that calls the
3291 destructor for the object. */
3292 if (flag_use_cxa_atexit)
3293 register_dtor_fn (decl);
3296 finish_static_initialization_or_destruction (guard_if_stmt);
3299 /* Generate code to do the static destruction of DECL. If DECL may be
3300 initialized more than once in different object files, GUARD is the
3301 guard variable to check. PRIORITY is the priority for the
3305 do_static_destruction (decl)
3310 /* If we're using __cxa_atexit, then destructors are registered
3311 immediately after objects are initialized. */
3312 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3314 /* If we don't need a destructor, there's nothing to do. */
3315 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3318 /* Actually do the destruction. */
3319 guard_if_stmt = start_static_initialization_or_destruction (decl,
3321 finish_expr_stmt (build_cleanup (decl));
3322 finish_static_initialization_or_destruction (guard_if_stmt);
3325 /* VARS is a list of variables with static storage duration which may
3326 need initialization and/or finalization. Remove those variables
3327 that don't really need to be initialized or finalized, and return
3328 the resulting list. The order in which the variables appear in
3329 VARS is in reverse order of the order in which they should actually
3330 be initialized. The list we return is in the unreversed order;
3331 i.e., the first variable should be initialized first. */
3334 prune_vars_needing_no_initialization (vars)
3340 for (var = vars, result = NULL_TREE;
3342 var = TREE_CHAIN (var))
3344 tree decl = TREE_VALUE (var);
3345 tree init = TREE_PURPOSE (var);
3347 /* Deal gracefully with error. */
3348 if (decl == error_mark_node)
3351 /* The only things that can be initialized are variables. */
3352 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3354 /* If this object is not defined, we don't need to do anything
3356 if (DECL_EXTERNAL (decl))
3359 /* Also, if the initializer already contains errors, we can bail
3361 if (init && TREE_CODE (init) == TREE_LIST
3362 && value_member (error_mark_node, init))
3365 /* This variable is going to need initialization and/or
3366 finalization, so we add it to the list. */
3367 result = tree_cons (init, decl, result);
3373 /* Make sure we have told the back end about all the variables in
3377 write_out_vars (vars)
3382 for (v = vars; v; v = TREE_CHAIN (v))
3383 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3384 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3387 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3388 (otherwise) that will initialize all gobal objects with static
3389 storage duration having the indicated PRIORITY. */
3392 generate_ctor_or_dtor_function (constructor_p, priority)
3401 /* We use `I' to indicate initialization and `D' to indicate
3408 /* Begin the function. */
3409 body = start_objects (function_key, priority);
3411 /* Call the static storage duration function with appropriate
3413 for (i = 0; i < ssdf_decls->elements_used; ++i)
3415 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3417 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3419 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3423 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3424 calls to any functions marked with attributes indicating that
3425 they should be called at initialization- or destruction-time. */
3426 if (priority == DEFAULT_INIT_PRIORITY)
3430 for (fns = constructor_p ? static_ctors : static_dtors;
3432 fns = TREE_CHAIN (fns))
3433 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3436 /* Close out the function. */
3437 finish_objects (function_key, priority, body);
3440 /* Generate constructor and destructor functions for the priority
3444 generate_ctor_and_dtor_functions_for_priority (n, data)
3446 void *data ATTRIBUTE_UNUSED;
3448 int priority = (int) n->key;
3449 priority_info pi = (priority_info) n->value;
3451 /* Generate the functions themselves, but only if they are really
3453 if (pi->initializations_p
3454 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3455 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3457 if (pi->destructions_p
3458 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3459 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3462 /* Keep iterating. */
3466 /* This routine is called from the last rule in yyparse ().
3467 Its job is to create all the code needed to initialize and
3468 destroy the global aggregates. We do the destruction
3469 first, since that way we only need to reverse the decls once. */
3480 /* Bad parse errors. Just forget about it. */
3481 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3484 /* Otherwise, GDB can get confused, because in only knows
3485 about source for LINENO-1 lines. */
3488 interface_unknown = 1;
3491 /* We now have to write out all the stuff we put off writing out.
3494 o Template specializations that we have not yet instantiated,
3495 but which are needed.
3496 o Initialization and destruction for non-local objects with
3497 static storage duration. (Local objects with static storage
3498 duration are initialized when their scope is first entered,
3499 and are cleaned up via atexit.)
3500 o Virtual function tables.
3502 All of these may cause others to be needed. For example,
3503 instantiating one function may cause another to be needed, and
3504 generating the intiailzer for an object may cause templates to be
3505 instantiated, etc., etc. */
3507 timevar_push (TV_VARCONST);
3509 emit_support_tinfos ();
3515 /* If there are templates that we've put off instantiating, do
3517 instantiate_pending_templates ();
3519 /* Write out virtual tables as required. Note that writing out
3520 the virtual table for a template class may cause the
3521 instantiation of members of that class. */
3522 if (walk_globals (vtable_decl_p,
3523 finish_vtable_vardecl,
3527 /* Write out needed type info variables. Writing out one variable
3528 might cause others to be needed. */
3529 if (walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3532 /* The list of objects with static storage duration is built up
3533 in reverse order. We clear STATIC_AGGREGATES so that any new
3534 aggregates added during the initialization of these will be
3535 initialized in the correct order when we next come around the
3537 vars = prune_vars_needing_no_initialization (static_aggregates);
3538 static_aggregates = NULL_TREE;
3544 /* We need to start a new initialization function each time
3545 through the loop. That's because we need to know which
3546 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3547 isn't computed until a function is finished, and written
3548 out. That's a deficiency in the back-end. When this is
3549 fixed, these initialization functions could all become
3550 inline, with resulting performance improvements. */
3551 tree ssdf_body = start_static_storage_duration_function ();
3553 /* Make sure the back end knows about all the variables. */
3554 write_out_vars (vars);
3556 /* First generate code to do all the initializations. */
3557 for (v = vars; v; v = TREE_CHAIN (v))
3558 do_static_initialization (TREE_VALUE (v),
3561 /* Then, generate code to do all the destructions. Do these
3562 in reverse order so that the most recently constructed
3563 variable is the first destroyed. If we're using
3564 __cxa_atexit, then we don't need to do this; functions
3565 were registered at initialization time to destroy the
3567 if (!flag_use_cxa_atexit)
3569 vars = nreverse (vars);
3570 for (v = vars; v; v = TREE_CHAIN (v))
3571 do_static_destruction (TREE_VALUE (v));
3576 /* Finish up the static storage duration function for this
3578 finish_static_storage_duration_function (ssdf_body);
3580 /* All those initializations and finalizations might cause
3581 us to need more inline functions, more template
3582 instantiations, etc. */
3586 /* Go through the various inline functions, and see if any need
3588 for (i = 0; i < deferred_fns_used; ++i)
3590 tree decl = VARRAY_TREE (deferred_fns, i);
3591 import_export_decl (decl);
3592 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3594 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3596 /* Even though we're already at the top-level, we push
3597 there again. That way, when we pop back a few lines
3598 hence, all of our state is restored. Otherwise,
3599 finish_function doesn't clean things up, and we end
3600 up with CURRENT_FUNCTION_DECL set. */
3601 push_to_top_level ();
3602 synthesize_method (decl);
3603 pop_from_top_level ();
3608 /* We lie to the back-end, pretending that some functions are
3609 not defined when they really are. This keeps these functions
3610 from being put out unnecessarily. But, we must stop lying
3611 when the functions are referenced, or if they are not comdat
3612 since they need to be put out now. */
3613 for (i = 0; i < deferred_fns_used; ++i)
3615 tree decl = VARRAY_TREE (deferred_fns, i);
3617 if (DECL_NOT_REALLY_EXTERN (decl)
3618 && DECL_INITIAL (decl)
3619 && DECL_NEEDED_P (decl))
3620 DECL_EXTERNAL (decl) = 0;
3622 /* If we're going to need to write this function out, and
3623 there's already a body for it, create RTL for it now.
3624 (There might be no body if this is a method we haven't
3625 gotten around to synthesizing yet.) */
3626 if (!DECL_EXTERNAL (decl)
3627 && DECL_NEEDED_P (decl)
3628 && DECL_SAVED_TREE (decl)
3629 && !DECL_SAVED_INSNS (decl)
3630 && !TREE_ASM_WRITTEN (decl))
3632 int saved_not_really_extern;
3634 /* When we call finish_function in expand_body, it will
3635 try to reset DECL_NOT_REALLY_EXTERN so we save and
3637 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3638 /* Generate RTL for this function now that we know we
3641 /* Undo the damage done by finish_function. */
3642 DECL_EXTERNAL (decl) = 0;
3643 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3644 /* If we're compiling -fsyntax-only pretend that this
3645 function has been written out so that we don't try to
3647 if (flag_syntax_only)
3648 TREE_ASM_WRITTEN (decl) = 1;
3653 if (deferred_fns_used
3654 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3657 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3660 /* Static data members are just like namespace-scope globals. */
3661 for (i = 0; i < pending_statics_used; ++i)
3663 tree decl = VARRAY_TREE (pending_statics, i);
3664 if (TREE_ASM_WRITTEN (decl))
3666 import_export_decl (decl);
3667 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3668 DECL_EXTERNAL (decl) = 0;
3671 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3672 pending_statics_used))
3677 /* We give C linkage to static constructors and destructors. */
3678 push_lang_context (lang_name_c);
3680 /* Generate initialization and destruction functions for all
3681 priorities for which they are required. */
3682 if (priority_info_map)
3683 splay_tree_foreach (priority_info_map,
3684 generate_ctor_and_dtor_functions_for_priority,
3687 /* We're done with the splay-tree now. */
3688 if (priority_info_map)
3689 splay_tree_delete (priority_info_map);
3691 /* We're done with static constructors, so we can go back to "C++"
3693 pop_lang_context ();
3695 /* Now delete from the chain of variables all virtual function tables.
3696 We output them all ourselves, because each will be treated
3697 specially. We don't do this if we're just doing semantic
3698 analysis, and not code-generation. */
3699 if (!flag_syntax_only)
3700 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3702 /* Now, issue warnings about static, but not defined, functions,
3703 etc., and emit debugging information. */
3704 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3705 if (pending_statics)
3706 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3707 pending_statics_used);
3711 /* The entire file is now complete. If requested, dump everything
3713 if (flag_dump_translation_unit)
3714 dump_node_to_file (global_namespace, flag_dump_translation_unit);
3716 /* If there's some tool that wants to examine the entire translation
3717 unit, let it do so now. */
3719 (*back_end_hook) (global_namespace);
3721 timevar_pop (TV_VARCONST);
3723 if (flag_detailed_statistics)
3725 dump_tree_statistics ();
3726 dump_time_statistics ();
3730 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3731 expr. Since it was parsed like a type, we need to wade through and fix
3732 that. Unfortunately, since operator() is left-associative, we can't use
3733 tail recursion. In the above example, TYPE is `A', and DECL is
3736 Maybe this shouldn't be recursive, but how often will it actually be
3740 reparse_absdcl_as_expr (type, decl)
3743 /* do build_functional_cast (type, NULL_TREE) at bottom */
3744 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3745 return build_functional_cast (type, NULL_TREE);
3748 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3750 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3752 if (TREE_CODE (decl) == CALL_EXPR
3753 && (! TREE_TYPE (decl)
3754 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3755 decl = require_complete_type (decl);
3760 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3761 out to be an expr. Since it was parsed like a type, we need to wade
3762 through and fix that. Since casts are right-associative, we are
3763 reversing the order, so we don't have to recurse.
3765 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3769 reparse_absdcl_as_casts (decl, expr)
3774 if (TREE_CODE (expr) == CONSTRUCTOR
3775 && TREE_TYPE (expr) == 0)
3777 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3778 decl = TREE_OPERAND (decl, 0);
3780 expr = digest_init (type, expr, (tree *) 0);
3781 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3783 int failure = complete_array_type (type, expr, 1);
3785 my_friendly_abort (78);
3791 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3792 decl = TREE_OPERAND (decl, 0);
3793 expr = build_c_cast (type, expr);
3796 if (warn_old_style_cast && ! in_system_header
3797 && current_lang_name != lang_name_c)
3798 warning ("use of old-style cast");
3803 /* Given plain tree nodes for an expression, build up the full semantics. */
3806 build_expr_from_tree (t)
3809 if (t == NULL_TREE || t == error_mark_node)
3812 switch (TREE_CODE (t))
3814 case IDENTIFIER_NODE:
3815 return do_identifier (t, 0, NULL_TREE);
3818 if (LOOKUP_EXPR_GLOBAL (t))
3819 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3821 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3823 case TEMPLATE_ID_EXPR:
3824 return (lookup_template_function
3825 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3826 build_expr_from_tree (TREE_OPERAND (t, 1))));
3829 return build_x_indirect_ref
3830 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3833 return build_functional_cast
3834 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3836 case REINTERPRET_CAST_EXPR:
3837 return build_reinterpret_cast
3838 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3840 case CONST_CAST_EXPR:
3841 return build_const_cast
3842 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3844 case DYNAMIC_CAST_EXPR:
3845 return build_dynamic_cast
3846 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3848 case STATIC_CAST_EXPR:
3849 return build_static_cast
3850 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3852 case PREDECREMENT_EXPR:
3853 case PREINCREMENT_EXPR:
3854 case POSTDECREMENT_EXPR:
3855 case POSTINCREMENT_EXPR:
3859 case TRUTH_NOT_EXPR:
3861 case CONVERT_EXPR: /* Unary + */
3866 return build_x_unary_op (TREE_CODE (t),
3867 build_expr_from_tree (TREE_OPERAND (t, 0)));
3872 case TRUNC_DIV_EXPR:
3874 case FLOOR_DIV_EXPR:
3875 case ROUND_DIV_EXPR:
3876 case EXACT_DIV_EXPR:
3878 case BIT_ANDTC_EXPR:
3881 case TRUNC_MOD_EXPR:
3882 case FLOOR_MOD_EXPR:
3883 case TRUTH_ANDIF_EXPR:
3884 case TRUTH_ORIF_EXPR:
3885 case TRUTH_AND_EXPR:
3900 return build_x_binary_op
3902 build_expr_from_tree (TREE_OPERAND (t, 0)),
3903 build_expr_from_tree (TREE_OPERAND (t, 1)));
3906 return build_m_component_ref
3907 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3908 build_expr_from_tree (TREE_OPERAND (t, 1)));
3911 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3914 if (TREE_OPERAND (t, 0) == NULL_TREE)
3916 return build_nt (ARRAY_REF, NULL_TREE,
3917 build_expr_from_tree (TREE_OPERAND (t, 1)));
3918 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3919 build_expr_from_tree (TREE_OPERAND (t, 1)));
3924 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3927 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3931 return build_x_modify_expr
3932 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3933 TREE_CODE (TREE_OPERAND (t, 1)),
3934 build_expr_from_tree (TREE_OPERAND (t, 2)));
3937 return build_x_arrow
3938 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3942 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3943 build_expr_from_tree (TREE_OPERAND (t, 1)),
3944 build_expr_from_tree (TREE_OPERAND (t, 2)),
3945 NEW_EXPR_USE_GLOBAL (t));
3948 return delete_sanity
3949 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3950 build_expr_from_tree (TREE_OPERAND (t, 1)),
3951 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3954 if (TREE_OPERAND (t, 1) == NULL_TREE)
3955 return build_x_compound_expr
3956 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3958 my_friendly_abort (42);
3960 case METHOD_CALL_EXPR:
3961 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3963 tree ref = TREE_OPERAND (t, 0);
3964 tree name = TREE_OPERAND (ref, 1);
3966 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3967 name = build_nt (TEMPLATE_ID_EXPR,
3968 TREE_OPERAND (name, 0),
3969 build_expr_from_tree (TREE_OPERAND (name, 1)));
3971 return build_scoped_method_call
3972 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3973 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3975 build_expr_from_tree (TREE_OPERAND (t, 2)));
3979 tree fn = TREE_OPERAND (t, 0);
3981 /* We can get a TEMPLATE_ID_EXPR here on code like:
3985 so we must resolve that. However, we can also get things
3986 like a BIT_NOT_EXPR here, when referring to a destructor,
3987 and things like that are not correctly resolved by
3988 build_expr_from_tree. So, just use build_expr_from_tree
3989 when we really need it. */
3990 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3991 fn = lookup_template_function
3992 (TREE_OPERAND (fn, 0),
3993 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3995 return build_method_call
3996 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3998 build_expr_from_tree (TREE_OPERAND (t, 2)),
3999 NULL_TREE, LOOKUP_NORMAL);
4003 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4005 tree ref = TREE_OPERAND (t, 0);
4006 tree name = TREE_OPERAND (ref, 1);
4008 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4009 name = build_nt (TEMPLATE_ID_EXPR,
4010 TREE_OPERAND (name, 0),
4011 build_expr_from_tree (TREE_OPERAND (name, 1)));
4013 return build_member_call
4014 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
4016 build_expr_from_tree (TREE_OPERAND (t, 1)));
4020 tree name = TREE_OPERAND (t, 0);
4022 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
4023 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
4024 && !LOOKUP_EXPR_GLOBAL (name)
4025 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
4026 && (!current_class_type
4027 || !lookup_member (current_class_type, id, 0, 0)))
4029 /* Do Koenig lookup if there are no class members. */
4030 name = do_identifier (id, 0, args);
4032 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
4033 || ! really_overloaded_fn (name))
4034 name = build_expr_from_tree (name);
4035 return build_x_function_call (name, args, current_class_ref);
4039 return build_x_conditional_expr
4040 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4041 build_expr_from_tree (TREE_OPERAND (t, 1)),
4042 build_expr_from_tree (TREE_OPERAND (t, 2)));
4044 case PSEUDO_DTOR_EXPR:
4045 return (finish_pseudo_destructor_call_expr
4046 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4047 build_expr_from_tree (TREE_OPERAND (t, 1)),
4048 build_expr_from_tree (TREE_OPERAND (t, 2))));
4052 tree purpose, value, chain;
4054 if (t == void_list_node)
4057 purpose = TREE_PURPOSE (t);
4059 purpose = build_expr_from_tree (purpose);
4060 value = TREE_VALUE (t);
4062 value = build_expr_from_tree (value);
4063 chain = TREE_CHAIN (t);
4064 if (chain && chain != void_type_node)
4065 chain = build_expr_from_tree (chain);
4066 return tree_cons (purpose, value, chain);
4071 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
4072 tree field = TREE_OPERAND (t, 1);
4074 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4075 and `x.A::b'. We must distinguish between those cases
4077 if (TREE_CODE (field) == SCOPE_REF)
4078 return build_object_ref (object,
4079 TREE_OPERAND (field, 0),
4080 TREE_OPERAND (field, 1));
4082 return build_x_component_ref (object, field,
4087 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
4093 /* digest_init will do the wrong thing if we let it. */
4094 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
4097 r = build_nt (CONSTRUCTOR, NULL_TREE,
4098 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
4099 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
4102 return digest_init (TREE_TYPE (t), r, 0);
4107 if (TYPE_P (TREE_OPERAND (t, 0)))
4108 return get_typeid (TREE_OPERAND (t, 0));
4109 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4112 return convert_from_reference (t);
4115 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4123 /* This is something of the form `int (*a)++' that has turned out to be an
4124 expr. It was only converted into parse nodes, so we need to go through
4125 and build up the semantics. Most of the work is done by
4126 build_expr_from_tree, above.
4128 In the above example, TYPE is `int' and DECL is `*a'. */
4131 reparse_decl_as_expr (type, decl)
4134 decl = build_expr_from_tree (decl);
4136 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4141 /* This is something of the form `int (*a)' that has turned out to be a
4142 decl. It was only converted into parse nodes, so we need to do the
4143 checking that make_{pointer,reference}_declarator do. */
4146 finish_decl_parsing (decl)
4149 switch (TREE_CODE (decl))
4151 case IDENTIFIER_NODE:
4154 return make_pointer_declarator
4155 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4157 return make_reference_declarator
4158 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4160 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4163 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4164 TREE_COMPLEXITY (decl) = current_class_depth;
4167 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4170 /* For attribute handling. */
4171 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4173 case TEMPLATE_ID_EXPR:
4176 my_friendly_abort (5);
4181 /* Return 1 if root encloses child. */
4184 is_namespace_ancestor (root, child)
4189 if (root == global_namespace)
4191 if (child == global_namespace)
4193 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4197 /* Return the namespace that is the common ancestor
4198 of two given namespaces. */
4201 namespace_ancestor (ns1, ns2)
4204 if (is_namespace_ancestor (ns1, ns2))
4206 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4209 /* Insert used into the using list of user. Set indirect_flag if this
4210 directive is not directly from the source. Also find the common
4211 ancestor and let our users know about the new namespace */
4213 add_using_namespace (user, used, indirect)
4219 /* Using oneself is a no-op. */
4222 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4223 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4224 /* Check if we already have this. */
4225 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4229 /* Promote to direct usage. */
4230 TREE_INDIRECT_USING (t) = 0;
4234 /* Add used to the user's using list. */
4235 DECL_NAMESPACE_USING (user)
4236 = tree_cons (used, namespace_ancestor (user, used),
4237 DECL_NAMESPACE_USING (user));
4239 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4241 /* Add user to the used's users list. */
4242 DECL_NAMESPACE_USERS (used)
4243 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4245 /* Recursively add all namespaces used. */
4246 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4247 /* indirect usage */
4248 add_using_namespace (user, TREE_PURPOSE (t), 1);
4250 /* Tell everyone using us about the new used namespaces. */
4251 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4252 add_using_namespace (TREE_PURPOSE (t), used, 1);
4255 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4256 duplicates. The first list becomes the tail of the result.
4258 The algorithm is O(n^2). We could get this down to O(n log n) by
4259 doing a sort on the addresses of the functions, if that becomes
4263 merge_functions (s1, s2)
4267 for (; s2; s2 = OVL_NEXT (s2))
4269 tree fn = OVL_CURRENT (s2);
4270 if (! ovl_member (fn, s1))
4271 s1 = build_overload (fn, s1);
4276 /* This should return an error not all definitions define functions.
4277 It is not an error if we find two functions with exactly the
4278 same signature, only if these are selected in overload resolution.
4279 old is the current set of bindings, new the freshly-found binding.
4280 XXX Do we want to give *all* candidates in case of ambiguity?
4281 XXX In what way should I treat extern declarations?
4282 XXX I don't want to repeat the entire duplicate_decls here */
4285 ambiguous_decl (name, old, new, flags)
4292 my_friendly_assert (old != NULL_TREE, 393);
4293 /* Copy the value. */
4294 val = BINDING_VALUE (new);
4296 switch (TREE_CODE (val))
4299 /* If we expect types or namespaces, and not templates,
4300 or this is not a template class. */
4301 if (LOOKUP_QUALIFIERS_ONLY (flags)
4302 && !DECL_CLASS_TEMPLATE_P (val))
4306 if (LOOKUP_NAMESPACES_ONLY (flags))
4309 case NAMESPACE_DECL:
4310 if (LOOKUP_TYPES_ONLY (flags))
4314 if (LOOKUP_QUALIFIERS_ONLY (flags))
4318 if (!BINDING_VALUE (old))
4319 BINDING_VALUE (old) = val;
4320 else if (val && val != BINDING_VALUE (old))
4322 if (is_overloaded_fn (BINDING_VALUE (old))
4323 && is_overloaded_fn (val))
4325 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4330 /* Some declarations are functions, some are not. */
4331 if (flags & LOOKUP_COMPLAIN)
4333 /* If we've already given this error for this lookup,
4334 BINDING_VALUE (old) is error_mark_node, so let's not
4335 repeat ourselves. */
4336 if (BINDING_VALUE (old) != error_mark_node)
4338 cp_error ("use of `%D' is ambiguous", name);
4339 cp_error_at (" first declared as `%#D' here",
4340 BINDING_VALUE (old));
4342 cp_error_at (" also declared as `%#D' here", val);
4344 BINDING_VALUE (old) = error_mark_node;
4347 /* ... and copy the type. */
4348 type = BINDING_TYPE (new);
4349 if (LOOKUP_NAMESPACES_ONLY (flags))
4351 if (!BINDING_TYPE (old))
4352 BINDING_TYPE (old) = type;
4353 else if (type && BINDING_TYPE (old) != type)
4355 if (flags & LOOKUP_COMPLAIN)
4357 cp_error ("`%D' denotes an ambiguous type",name);
4358 cp_error_at (" first type here", BINDING_TYPE (old));
4359 cp_error_at (" other type here", type);
4365 /* Subroutine of unualified_namespace_lookup:
4366 Add the bindings of NAME in used namespaces to VAL.
4367 We are currently looking for names in namespace SCOPE, so we
4368 look through USINGS for using-directives of namespaces
4369 which have SCOPE as a common ancestor with the current scope.
4370 Returns zero on errors. */
4373 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4374 tree name, val, usings, scope;
4380 /* Iterate over all used namespaces in current, searching for using
4381 directives of scope. */
4382 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4383 if (TREE_VALUE (iter) == scope)
4386 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4388 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4389 /* Resolve ambiguities. */
4390 val = ambiguous_decl (name, val, val1, flags);
4392 return BINDING_VALUE (val) != error_mark_node;
4396 Accepts the NAME to lookup and its qualifying SCOPE.
4397 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4401 qualified_lookup_using_namespace (name, scope, result, flags)
4407 /* Maintain a list of namespaces visited... */
4408 tree seen = NULL_TREE;
4409 /* ... and a list of namespace yet to see. */
4410 tree todo = NULL_TREE;
4412 /* Look through namespace aliases. */
4413 scope = ORIGINAL_NAMESPACE (scope);
4414 while (scope && (result != error_mark_node))
4416 seen = tree_cons (scope, NULL_TREE, seen);
4417 result = ambiguous_decl (name, result,
4418 binding_for_name (name, scope), flags);
4419 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4420 /* Consider using directives. */
4421 for (usings = DECL_NAMESPACE_USING (scope); usings;
4422 usings = TREE_CHAIN (usings))
4423 /* If this was a real directive, and we have not seen it. */
4424 if (!TREE_INDIRECT_USING (usings)
4425 && !purpose_member (TREE_PURPOSE (usings), seen))
4426 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4429 scope = TREE_PURPOSE (todo);
4430 todo = TREE_CHAIN (todo);
4433 scope = NULL_TREE; /* If there never was a todo list. */
4435 return result != error_mark_node;
4438 /* [namespace.memdef]/2 */
4440 /* Set the context of a declaration to scope. Complain if we are not
4444 set_decl_namespace (decl, scope, friendp)
4450 if (scope == fake_std_node)
4451 scope = global_namespace;
4452 /* Get rid of namespace aliases. */
4453 scope = ORIGINAL_NAMESPACE (scope);
4455 /* It is ok for friends to be qualified in parallel space. */
4456 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4457 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4459 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4460 if (scope != current_namespace)
4462 /* See whether this has been declared in the namespace. */
4463 old = namespace_binding (DECL_NAME (decl), scope);
4465 /* No old declaration at all. */
4467 /* A template can be explicitly specialized in any namespace. */
4468 if (processing_explicit_instantiation)
4470 if (!is_overloaded_fn (decl))
4471 /* Don't compare non-function decls with decls_match here,
4472 since it can't check for the correct constness at this
4473 point. pushdecl will find those errors later. */
4475 /* Since decl is a function, old should contain a function decl. */
4476 if (!is_overloaded_fn (old))
4478 if (processing_template_decl || processing_specialization)
4479 /* We have not yet called push_template_decl to turn the
4480 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4481 won't match. But, we'll check later, when we construct the
4484 for (; old; old = OVL_NEXT (old))
4485 if (decls_match (decl, OVL_CURRENT (old)))
4491 cp_error ("`%D' should have been declared inside `%D'",
4495 /* Compute the namespace where a declaration is defined. */
4498 decl_namespace (decl)
4502 decl = TYPE_STUB_DECL (decl);
4503 while (DECL_CONTEXT (decl))
4505 decl = DECL_CONTEXT (decl);
4506 if (TREE_CODE (decl) == NAMESPACE_DECL)
4509 decl = TYPE_STUB_DECL (decl);
4510 my_friendly_assert (DECL_P (decl), 390);
4513 return global_namespace;
4516 /* Return the namespace where the current declaration is declared. */
4519 current_decl_namespace ()
4522 /* If we have been pushed into a different namespace, use it. */
4523 if (decl_namespace_list)
4524 return TREE_PURPOSE (decl_namespace_list);
4526 if (current_class_type)
4527 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4528 else if (current_function_decl)
4529 result = decl_namespace (current_function_decl);
4531 result = current_namespace;
4535 /* Temporarily set the namespace for the current declaration. */
4538 push_decl_namespace (decl)
4541 if (TREE_CODE (decl) != NAMESPACE_DECL)
4542 decl = decl_namespace (decl);
4543 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4544 NULL_TREE, decl_namespace_list);
4548 pop_decl_namespace ()
4550 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4553 /* Enter a class or namespace scope. */
4559 if (TREE_CODE (t) == NAMESPACE_DECL)
4560 push_decl_namespace (t);
4565 /* Leave scope pushed by push_scope. */
4571 if (TREE_CODE (t) == NAMESPACE_DECL)
4572 pop_decl_namespace ();
4577 /* [basic.lookup.koenig] */
4578 /* A non-zero return value in the functions below indicates an error.
4579 All nodes allocated in the procedure are on the scratch obstack. */
4589 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4590 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4591 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4592 static int add_function PARAMS ((struct arg_lookup *, tree));
4593 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4594 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4595 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4597 /* Add a function to the lookup structure.
4598 Returns 1 on error. */
4601 add_function (k, fn)
4602 struct arg_lookup *k;
4605 /* We used to check here to see if the function was already in the list,
4606 but that's O(n^2), which is just too expensive for function lookup.
4607 Now we deal with the occasional duplicate in joust. In doing this, we
4608 assume that the number of duplicates will be small compared to the
4609 total number of functions being compared, which should usually be the
4612 /* We must find only functions, or exactly one non-function. */
4615 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4616 k->functions = build_overload (fn, k->functions);
4619 tree f1 = OVL_CURRENT (k->functions);
4621 if (is_overloaded_fn (f1))
4623 fn = f1; f1 = f2; f2 = fn;
4625 cp_error_at ("`%D' is not a function,", f1);
4626 cp_error_at (" conflict with `%D'", f2);
4627 cp_error (" in call to `%D'", k->name);
4634 /* Add functions of a namespace to the lookup structure.
4635 Returns 1 on error. */
4638 arg_assoc_namespace (k, scope)
4639 struct arg_lookup *k;
4644 if (purpose_member (scope, k->namespaces))
4646 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4648 value = namespace_binding (k->name, scope);
4652 for (; value; value = OVL_NEXT (value))
4653 if (add_function (k, OVL_CURRENT (value)))
4659 /* Adds everything associated with a template argument to the lookup
4660 structure. Returns 1 on error. */
4663 arg_assoc_template_arg (k, arg)
4664 struct arg_lookup* k;
4667 /* [basic.lookup.koenig]
4669 If T is a template-id, its associated namespaces and classes are
4670 ... the namespaces and classes associated with the types of the
4671 template arguments provided for template type parameters
4672 (excluding template template parameters); the namespaces in which
4673 any template template arguments are defined; and the classes in
4674 which any member templates used as template template arguments
4675 are defined. [Note: non-type template arguments do not
4676 contribute to the set of associated namespaces. ] */
4678 /* Consider first template template arguments. */
4679 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
4681 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4683 tree ctx = CP_DECL_CONTEXT (arg);
4685 /* It's not a member template. */
4686 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4687 return arg_assoc_namespace (k, ctx);
4688 /* Otherwise, it must be member template. */
4690 return arg_assoc_class (k, ctx);
4692 /* It's not a template template argument, but it is a type template
4694 else if (TYPE_P (arg))
4695 return arg_assoc_type (k, arg);
4696 /* It's a non-type template argument. */
4701 /* Adds everything associated with class to the lookup structure.
4702 Returns 1 on error. */
4705 arg_assoc_class (k, type)
4706 struct arg_lookup* k;
4709 tree list, friends, context;
4712 /* Backend build structures, such as __builtin_va_list, aren't
4713 affected by all this. */
4714 if (!CLASS_TYPE_P (type))
4717 if (purpose_member (type, k->classes))
4719 k->classes = tree_cons (type, NULL_TREE, k->classes);
4721 context = decl_namespace (TYPE_MAIN_DECL (type));
4722 if (arg_assoc_namespace (k, context))
4725 /* Process baseclasses. */
4726 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4727 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4730 /* Process friends. */
4731 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4732 list = TREE_CHAIN (list))
4733 if (k->name == TREE_PURPOSE (list))
4734 for (friends = TREE_VALUE (list); friends;
4735 friends = TREE_CHAIN (friends))
4736 /* Only interested in global functions with potentially hidden
4737 (i.e. unqualified) declarations. */
4738 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4739 && decl_namespace (TREE_VALUE (friends)) == context)
4740 if (add_function (k, TREE_VALUE (friends)))
4743 /* Process template arguments. */
4744 if (CLASSTYPE_TEMPLATE_INFO (type))
4746 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4747 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4748 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4754 /* Adds everything associated with a given type.
4755 Returns 1 on error. */
4758 arg_assoc_type (k, type)
4759 struct arg_lookup *k;
4762 switch (TREE_CODE (type))
4773 if (TYPE_PTRMEMFUNC_P (type))
4774 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4775 return arg_assoc_class (k, type);
4777 case REFERENCE_TYPE:
4779 return arg_assoc_type (k, TREE_TYPE (type));
4782 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4784 /* Pointer to member: associate class type and value type. */
4785 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4787 return arg_assoc_type (k, TREE_TYPE (type));
4789 /* The basetype is referenced in the first arg type, so just
4792 /* Associate the parameter types. */
4793 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4795 /* Associate the return type. */
4796 return arg_assoc_type (k, TREE_TYPE (type));
4797 case TEMPLATE_TYPE_PARM:
4798 case BOUND_TEMPLATE_TEMPLATE_PARM:
4803 if (type == unknown_type_node)
4805 /* else fall through */
4807 my_friendly_abort (390);
4812 /* Adds everything associated with arguments. Returns 1 on error. */
4815 arg_assoc_args (k, args)
4816 struct arg_lookup* k;
4819 for (; args; args = TREE_CHAIN (args))
4820 if (arg_assoc (k, TREE_VALUE (args)))
4825 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4829 struct arg_lookup* k;
4832 if (n == error_mark_node)
4836 return arg_assoc_type (k, n);
4838 if (! type_unknown_p (n))
4839 return arg_assoc_type (k, TREE_TYPE (n));
4841 if (TREE_CODE (n) == ADDR_EXPR)
4842 n = TREE_OPERAND (n, 0);
4843 if (TREE_CODE (n) == COMPONENT_REF)
4844 n = TREE_OPERAND (n, 1);
4845 if (TREE_CODE (n) == OFFSET_REF)
4846 n = TREE_OPERAND (n, 1);
4847 while (TREE_CODE (n) == TREE_LIST)
4850 if (TREE_CODE (n) == FUNCTION_DECL)
4851 return arg_assoc_type (k, TREE_TYPE (n));
4852 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4854 /* [basic.lookup.koenig]
4856 If T is a template-id, its associated namespaces and classes
4857 are the namespace in which the template is defined; for
4858 member templates, the member template's class... */
4859 tree template = TREE_OPERAND (n, 0);
4860 tree args = TREE_OPERAND (n, 1);
4864 if (TREE_CODE (template) == COMPONENT_REF)
4865 template = TREE_OPERAND (template, 1);
4867 /* First, the template. There may actually be more than one if
4868 this is an overloaded function template. But, in that case,
4869 we only need the first; all the functions will be in the same
4871 template = OVL_CURRENT (template);
4873 ctx = CP_DECL_CONTEXT (template);
4875 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4877 if (arg_assoc_namespace (k, ctx) == 1)
4880 /* It must be a member template. */
4881 else if (arg_assoc_class (k, ctx) == 1)
4884 /* Now the arguments. */
4885 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4886 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4891 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4893 for (; n; n = OVL_CHAIN (n))
4894 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4901 /* Performs Koenig lookup depending on arguments, where fns
4902 are the functions found in normal lookup. */
4905 lookup_arg_dependent (name, fns, args)
4910 struct arg_lookup k;
4911 tree fn = NULL_TREE;
4915 k.classes = NULL_TREE;
4917 /* Note that we've already looked at some namespaces during normal
4918 unqualified lookup, unless we found a decl in function scope. */
4920 fn = OVL_CURRENT (fns);
4921 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4922 k.namespaces = NULL_TREE;
4924 unqualified_namespace_lookup (name, 0, &k.namespaces);
4926 arg_assoc_args (&k, args);
4930 /* Process a namespace-alias declaration. */
4933 do_namespace_alias (alias, namespace)
4934 tree alias, namespace;
4936 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4938 /* The parser did not find it, so it's not there. */
4939 cp_error ("unknown namespace `%D'", namespace);
4943 namespace = ORIGINAL_NAMESPACE (namespace);
4945 /* Build the alias. */
4946 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4947 DECL_NAMESPACE_ALIAS (alias) = namespace;
4951 /* Check a non-member using-declaration. Return the name and scope
4952 being used, and the USING_DECL, or NULL_TREE on failure. */
4955 validate_nonmember_using_decl (decl, scope, name)
4960 if (TREE_CODE (decl) == SCOPE_REF
4961 && TREE_OPERAND (decl, 0) == fake_std_node)
4963 *scope = global_namespace;
4964 *name = TREE_OPERAND (decl, 1);
4966 else if (TREE_CODE (decl) == SCOPE_REF)
4968 *scope = TREE_OPERAND (decl, 0);
4969 *name = TREE_OPERAND (decl, 1);
4971 /* [namespace.udecl]
4973 A using-declaration for a class member shall be a
4974 member-declaration. */
4975 if (!processing_template_decl
4976 && TREE_CODE (*scope) != NAMESPACE_DECL)
4978 if (TYPE_P (*scope))
4979 cp_error ("`%T' is not a namespace", *scope);
4981 cp_error ("`%D' is not a namespace", *scope);
4985 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4986 || TREE_CODE (decl) == TYPE_DECL
4987 || TREE_CODE (decl) == TEMPLATE_DECL)
4989 *scope = global_namespace;
4993 my_friendly_abort (382);
4995 *name = DECL_NAME (*name);
4996 /* Make a USING_DECL. */
4997 return push_using_decl (*scope, *name);
5000 /* Process local and global using-declarations. */
5003 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
5005 tree oldval, oldtype;
5006 tree *newval, *newtype;
5010 *newval = *newtype = NULL_TREE;
5011 decls = make_node (CPLUS_BINDING);
5012 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
5016 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
5018 cp_error ("`%D' not declared", name);
5022 /* Check for using functions. */
5023 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
5027 if (oldval && !is_overloaded_fn (oldval))
5029 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
5034 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
5036 tree new_fn = OVL_CURRENT (tmp);
5038 /* [namespace.udecl]
5040 If a function declaration in namespace scope or block
5041 scope has the same name and the same parameter types as a
5042 function introduced by a using declaration the program is
5044 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
5046 tree old_fn = OVL_CURRENT (tmp1);
5048 if (new_fn == old_fn)
5049 /* The function already exists in the current namespace. */
5051 else if (OVL_USED (tmp1))
5052 continue; /* this is a using decl */
5053 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
5054 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
5056 /* There was already a non-using declaration in
5057 this scope with the same parameter types. If both
5058 are the same extern "C" functions, that's ok. */
5059 if (!decls_match (new_fn, old_fn))
5060 cp_error ("`%D' is already declared in this scope", name);
5065 /* If we broke out of the loop, there's no reason to add
5066 this function to the using declarations for this
5071 *newval = build_overload (OVL_CURRENT (tmp), *newval);
5072 if (TREE_CODE (*newval) != OVERLOAD)
5073 *newval = ovl_cons (*newval, NULL_TREE);
5074 OVL_USED (*newval) = 1;
5079 *newval = BINDING_VALUE (decls);
5081 duplicate_decls (*newval, oldval);
5084 *newtype = BINDING_TYPE (decls);
5085 if (oldtype && *newtype && oldtype != *newtype)
5087 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5093 /* Process a using-declaration not appearing in class or local scope. */
5096 do_toplevel_using_decl (decl)
5099 tree scope, name, binding;
5100 tree oldval, oldtype, newval, newtype;
5102 decl = validate_nonmember_using_decl (decl, &scope, &name);
5103 if (decl == NULL_TREE)
5106 binding = binding_for_name (name, current_namespace);
5108 oldval = BINDING_VALUE (binding);
5109 oldtype = BINDING_TYPE (binding);
5111 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5113 /* Copy declarations found. */
5115 BINDING_VALUE (binding) = newval;
5117 BINDING_TYPE (binding) = newtype;
5121 /* Process a using-declaration at function scope. */
5124 do_local_using_decl (decl)
5128 tree oldval, oldtype, newval, newtype;
5130 decl = validate_nonmember_using_decl (decl, &scope, &name);
5131 if (decl == NULL_TREE)
5134 if (building_stmt_tree ()
5135 && at_function_scope_p ())
5136 add_decl_stmt (decl);
5137 if (processing_template_decl)
5140 oldval = lookup_name_current_level (name);
5141 oldtype = lookup_type_current_level (name);
5143 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5147 if (is_overloaded_fn (newval))
5151 /* We only need to push declarations for those functions
5152 that were not already bound in the current level.
5153 The old value might be NULL_TREE, it might be a single
5154 function, or an OVERLOAD. */
5155 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5156 term = OVL_FUNCTION (oldval);
5159 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5161 push_overloaded_decl (OVL_CURRENT (fn),
5162 PUSH_LOCAL | PUSH_USING);
5165 push_local_binding (name, newval, PUSH_USING);
5168 set_identifier_type_value (name, newtype);
5172 do_class_using_decl (decl)
5177 if (TREE_CODE (decl) != SCOPE_REF
5178 || !TYPE_P (TREE_OPERAND (decl, 0)))
5180 cp_error ("using-declaration for non-member at class scope");
5183 name = TREE_OPERAND (decl, 1);
5184 if (TREE_CODE (name) == BIT_NOT_EXPR)
5186 cp_error ("using-declaration for destructor");
5189 if (TREE_CODE (name) == TYPE_DECL)
5190 name = DECL_NAME (name);
5192 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5194 value = build_lang_decl (USING_DECL, name, void_type_node);
5195 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5199 /* Process a using-directive. */
5202 do_using_directive (namespace)
5205 if (namespace == fake_std_node)
5208 /* using namespace A::B::C; */
5209 if (TREE_CODE (namespace) == SCOPE_REF)
5210 namespace = TREE_OPERAND (namespace, 1);
5211 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5213 /* Lookup in lexer did not find a namespace. */
5214 if (!processing_template_decl)
5215 cp_error ("namespace `%T' undeclared", namespace);
5218 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5220 if (!processing_template_decl)
5221 cp_error ("`%T' is not a namespace", namespace);
5224 namespace = ORIGINAL_NAMESPACE (namespace);
5225 if (!toplevel_bindings_p ())
5226 push_using_directive (namespace);
5229 add_using_namespace (current_namespace, namespace, 0);
5233 check_default_args (x)
5236 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5237 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5238 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5240 if (TREE_PURPOSE (arg))
5244 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5255 TREE_USED (decl) = 1;
5256 if (processing_template_decl)
5258 assemble_external (decl);
5260 /* Is it a synthesized method that needs to be synthesized? */
5261 if (TREE_CODE (decl) == FUNCTION_DECL
5262 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5263 && DECL_ARTIFICIAL (decl)
5264 && ! DECL_INITIAL (decl)
5265 /* Kludge: don't synthesize for default args. */
5266 && current_function_decl)
5268 synthesize_method (decl);
5269 /* If we've already synthesized the method we don't need to
5270 instantiate it, so we can return right away. */
5274 /* If this is a function or variable that is an instance of some
5275 template, we now know that we will need to actually do the
5276 instantiation. We check that DECL is not an explicit
5277 instantiation because that is not checked in instantiate_decl. */
5278 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5279 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5280 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5281 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5282 instantiate_decl (decl, /*defer_ok=*/1);
5285 /* Helper function for named_class_head_sans_basetype nonterminal. We
5286 have just seen something of the form `AGGR SCOPE::ID'. Return a
5287 TYPE_DECL for the type declared by ID in SCOPE. */
5290 handle_class_head (aggr, scope, id)
5291 tree aggr, scope, id;
5293 tree decl = NULL_TREE;
5295 if (TREE_CODE (id) == TYPE_DECL)
5296 /* We must bash typedefs back to the main decl of the type. Otherwise
5297 we become confused about scopes. */
5298 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5299 else if (DECL_CLASS_TEMPLATE_P (id))
5300 decl = DECL_TEMPLATE_RESULT (id);
5303 tree current = current_scope ();
5305 if (current == NULL_TREE)
5306 current = current_namespace;
5307 if (scope == fake_std_node)
5308 scope = global_namespace;
5309 if (scope == NULL_TREE)
5310 scope = global_namespace;
5314 /* According to the suggested resolution of core issue 180,
5315 'typename' is assumed after a class-key. */
5316 decl = make_typename_type (scope, id, 1);
5317 if (decl != error_mark_node)
5318 decl = TYPE_MAIN_DECL (decl);
5322 else if (scope == current)
5324 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5325 Be nice about it. */
5327 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5328 FROB_CONTEXT (scope), id);
5330 else if (scope != global_namespace)
5331 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5333 cp_error ("no file-scope type named `%D'", id);
5335 /* Inject it at the current scope. */
5337 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, 1));
5340 /* Enter the SCOPE. If this turns out not to be a definition, the
5341 parser must leave the scope. */
5342 push_scope (CP_DECL_CONTEXT (decl));
5344 /* If we see something like:
5346 template <typename T> struct S::I ....
5348 we must create a TEMPLATE_DECL for the nested type. */
5349 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5350 decl = push_template_decl (decl);
5355 /* Initialize decl2.c. */
5360 ggc_add_tree_varray_root (&deferred_fns, 1);
5361 ggc_add_tree_varray_root (&pending_statics, 1);
5362 ggc_add_tree_varray_root (&ssdf_decls, 1);
5363 ggc_add_tree_root (&ssdf_decl, 1);
5364 ggc_add_tree_root (&priority_decl, 1);
5365 ggc_add_tree_root (&initialize_p_decl, 1);