1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
27 /* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
43 #include "dwarf2out.h"
45 #include "splay-tree.h"
50 extern cpp_reader parse_in;
53 /* This structure contains information about the initializations
54 and/or destructions required for a particular priority level. */
55 typedef struct priority_info_s {
56 /* A label indicating where we should generate the next
57 initialization with this priority. */
58 rtx initialization_sequence;
59 /* A label indicating where we should generate the next destruction
60 with this priority. */
61 rtx destruction_sequence;
62 /* Non-zero if there have been any initializations at this priority
63 throughout the translation unit. */
64 int initializations_p;
65 /* Non-zero if there have been any destructions at this priority
66 throughout the translation unit. */
70 static tree get_sentry PROTO((tree));
71 static void mark_vtable_entries PROTO((tree));
72 static void grok_function_init PROTO((tree, tree));
73 static int finish_vtable_vardecl PROTO((tree *, void *));
74 static int prune_vtable_vardecl PROTO((tree *, void *));
75 static int finish_sigtable_vardecl PROTO((tree *, void *));
76 static int is_namespace_ancestor PROTO((tree, tree));
77 static void add_using_namespace PROTO((tree, tree, int));
78 static tree ambiguous_decl PROTO((tree, tree, tree,int));
79 static tree build_anon_union_vars PROTO((tree, tree*, int, int));
80 static int acceptable_java_type PROTO((tree));
81 static void output_vtable_inherit PROTO((tree));
82 static void start_objects PROTO((int, int));
83 static void finish_objects PROTO((int, int));
84 static tree merge_functions PROTO((tree, tree));
85 static tree decl_namespace PROTO((tree));
86 static tree validate_nonmember_using_decl PROTO((tree, tree *, tree *));
87 static void do_nonmember_using_decl PROTO((tree, tree, tree, tree,
89 static void start_static_storage_duration_function PROTO((void));
90 static int generate_inits_for_priority PROTO((splay_tree_node, void *));
91 static void finish_static_storage_duration_function PROTO((void));
92 static priority_info get_priority_info PROTO((int));
93 static void do_static_initialization PROTO((tree, tree, tree, int));
94 static void do_static_destruction PROTO((tree, tree, int));
95 static void do_static_initialization_and_destruction PROTO((tree, tree));
96 static void generate_ctor_or_dtor_function PROTO((int, int));
97 static int generate_ctor_and_dtor_functions_for_priority
98 PROTO((splay_tree_node, void *));
99 extern int current_class_depth;
101 /* A list of virtual function tables we must make sure to write out. */
102 tree pending_vtables;
104 /* A list of static class variables. This is needed, because a
105 static class variable can be declared inside the class without
106 an initializer, and then initialized, staticly, outside the class. */
107 static varray_type pending_statics;
108 static size_t pending_statics_used;
110 /* A list of functions which were declared inline, but which we
111 may need to emit outline anyway. */
112 static varray_type saved_inlines;
113 static size_t saved_inlines_used;
115 /* Used to help generate temporary names which are unique within
116 a function. Reset to 0 by start_function. */
118 int temp_name_counter;
120 /* Same, but not reset. Local temp variables and global temp variables
121 can have the same name. */
122 static int global_temp_name_counter;
124 /* Flag used when debugging spew.c */
126 extern int spew_debug;
128 /* Nonzero if we're done parsing and into end-of-file activities. */
132 /* Functions called along with real static constructors and destructors. */
134 tree static_ctors, static_dtors;
136 /* The current open namespace, and ::. */
138 tree current_namespace;
139 tree global_namespace;
141 /* The stack for namespaces of current declarations. */
143 static tree decl_namespace_list;
146 /* C (and C++) language-specific option variables. */
148 /* Nonzero means allow type mismatches in conditional expressions;
149 just make their values `void'. */
151 int flag_cond_mismatch;
153 /* Nonzero means give `double' the same size as `float'. */
155 int flag_short_double;
157 /* Nonzero means don't recognize the keyword `asm'. */
161 /* Nonzero means don't recognize any extension keywords. */
163 int flag_no_gnu_keywords;
165 /* Nonzero means don't recognize the non-ANSI builtin functions. */
169 /* Nonzero means don't recognize the non-ANSI builtin functions.
172 int flag_no_nonansi_builtin;
174 /* Nonzero means do some things the same way PCC does. Only provided so
175 the compiler will link. */
177 int flag_traditional;
179 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
181 int flag_signed_bitfields = 1;
183 /* Nonzero means enable obscure ANSI features and disable GNU extensions
184 that might cause ANSI-compliant code to be miscompiled. */
188 /* Nonzero means do emit exported implementations of functions even if
189 they can be inlined. */
191 int flag_implement_inlines = 1;
193 /* Nonzero means do emit exported implementations of templates, instead of
194 multiple static copies in each file that needs a definition. */
196 int flag_external_templates;
198 /* Nonzero means that the decision to emit or not emit the implementation of a
199 template depends on where the template is instantiated, rather than where
202 int flag_alt_external_templates;
204 /* Nonzero means that implicit instantiations will be emitted if needed. */
206 int flag_implicit_templates = 1;
208 /* Nonzero means that implicit instantiations of inline templates will be
209 emitted if needed, even if instantiations of non-inline templates
212 int flag_implicit_inline_templates = 1;
214 /* Nonzero means warn about implicit declarations. */
216 int warn_implicit = 1;
218 /* Nonzero means warn about usage of long long when `-pedantic'. */
220 int warn_long_long = 1;
222 /* Nonzero means warn when all ctors or dtors are private, and the class
225 int warn_ctor_dtor_privacy = 1;
227 /* 1 or 2 if we want to implement vtables using "thunks".
228 The default is off. Version 1 indicates "old" implementation;
229 Version 2 passes the __vlist argument in pvbase cases. */
231 #ifndef DEFAULT_VTABLE_THUNKS
232 #define DEFAULT_VTABLE_THUNKS 0
234 int flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
236 #if DEFAULT_VTABLE_THUNKS == 2
237 int flag_vtable_thunks_compat = 1;
239 int flag_vtable_thunks_compat = 0;
242 /* True if we want to deal with repository information. */
244 int flag_use_repository;
246 /* Nonzero if we want to issue diagnostics that the standard says are not
249 int flag_optional_diags = 1;
251 /* Nonzero means give string constants the type `const char *', as mandated
254 int flag_const_strings = 1;
256 /* Nonzero means warn about deprecated conversion from string constant to
259 int warn_write_strings;
261 /* Nonzero means warn about pointer casts that can drop a type qualifier
262 from the pointer target type. */
266 /* Nonzero means warn about sizeof(function) or addition/subtraction
267 of function pointers. */
269 int warn_pointer_arith = 1;
271 /* Nonzero means warn for any function def without prototype decl. */
273 int warn_missing_prototypes;
275 /* Nonzero means warn about multiple (redundant) decls for the same single
276 variable or function. */
278 int warn_redundant_decls;
280 /* Warn if initializer is not completely bracketed. */
282 int warn_missing_braces;
284 /* Warn about comparison of signed and unsigned values. */
286 int warn_sign_compare;
288 /* Warn about *printf or *scanf format/argument anomalies. */
291 int warn_format_extra_args = 1;
293 /* Warn about a subscript that has type char. */
295 int warn_char_subscripts;
297 /* Warn if a type conversion is done that might have confusing results. */
301 /* Warn if adding () is suggested. */
303 int warn_parentheses;
305 /* Non-zero means warn in function declared in derived class has the
306 same name as a virtual in the base class, but fails to match the
307 type signature of any virtual function in the base class. */
308 int warn_overloaded_virtual;
310 /* Non-zero means warn when declaring a class that has a non virtual
311 destructor, when it really ought to have a virtual one. */
314 /* Non-zero means warn when a function is declared extern and later inline. */
315 int warn_extern_inline;
317 /* Non-zero means warn when the compiler will reorder code. */
320 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
323 /* Non-zero means warn when we convert a pointer to member function
324 into a pointer to (void or function). */
325 int warn_pmf2ptr = 1;
327 /* Nonzero means warn about violation of some Effective C++ style rules. */
331 /* Nonzero means warn where overload resolution chooses a promotion from
332 unsigned to signed over a conversion to an unsigned of the same size. */
336 /* Nonzero means warn when an old-style cast is used. */
338 int warn_old_style_cast;
340 /* Warn about #pragma directives that are not recognised. */
342 int warn_unknown_pragmas; /* Tri state variable. */
344 /* Nonzero means warn about use of multicharacter literals. */
346 int warn_multichar = 1;
348 /* Nonzero means warn when non-templatized friend functions are
349 declared within a template */
351 int warn_nontemplate_friend = 1;
353 /* Nonzero means complain about deprecated features. */
355 int warn_deprecated = 1;
357 /* Nonzero means `$' can be in an identifier. */
359 #ifndef DOLLARS_IN_IDENTIFIERS
360 #define DOLLARS_IN_IDENTIFIERS 1
362 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
364 /* Nonzero for -fno-strict-prototype switch: do not consider empty
365 argument prototype to mean function takes no arguments. */
367 int flag_strict_prototype = 2;
368 int strict_prototype = 1;
369 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
371 /* Nonzero means that labels can be used as first-class objects */
375 /* Non-zero means to collect statistics which might be expensive
376 and to print them when we are done. */
377 int flag_detailed_statistics;
379 /* C++ specific flags. */
380 /* Zero means that `this' is a *const. This gives nice behavior in the
381 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
382 -2 means we're constructing an object and it has fixed type. */
384 int flag_this_is_variable;
386 /* 3 means write out only virtuals function tables `defined'
387 in this implementation file.
388 0 means write out virtual function tables and give them
389 (C) static access (default). */
393 /* Nonzero means we should attempt to elide constructors when possible. */
395 int flag_elide_constructors = 1;
397 /* Nonzero means recognize and handle signature language constructs. */
399 int flag_handle_signatures;
401 /* Nonzero means that member functions defined in class scope are
402 inline by default. */
404 int flag_default_inline = 1;
406 /* Controls whether compiler generates 'type descriptor' that give
407 run-time type information. */
410 /* Nonzero if we wish to output cross-referencing information
411 for the GNU class browser. */
412 extern int flag_gnu_xref;
414 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
417 int flag_huge_objects;
419 /* Nonzero if we want to conserve space in the .o files. We do this
420 by putting uninitialized data and runtime initialized data into
421 .common instead of .data at the expense of not flagging multiple
424 int flag_conserve_space;
426 /* Nonzero if we want to obey access control semantics. */
428 int flag_access_control = 1;
430 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
432 int flag_operator_names;
434 /* Nonzero if we want to check the return value of new and avoid calling
435 constructors if it is a null pointer. */
439 /* Nonzero if we want the new ANSI rules for pushing a new scope for `for'
440 initialization variables.
441 0: Old rules, set by -fno-for-scope.
442 2: New ANSI rules, set by -ffor-scope.
443 1: Try to implement new ANSI rules, but with backup compatibility
444 (and warnings). This is the default, for now. */
446 int flag_new_for_scope = 1;
448 /* Nonzero if we want to emit defined symbols with common-like linkage as
449 weak symbols where possible, in order to conform to C++ semantics.
450 Otherwise, emit them as local symbols. */
454 /* Nonzero to enable experimental ABI changes. */
458 /* Nonzero to not ignore namespace std. */
462 /* Maximum template instantiation depth. Must be at least 17 for ANSI
465 int max_tinst_depth = 17;
467 /* The name-mangling scheme to use. Must be 1 or greater to support
468 template functions with identical types, but different template
470 int name_mangling_version = 2;
472 /* Nonzero means that guiding declarations are allowed. */
473 int flag_guiding_decls;
475 /* Nonzero if squashed mangling is to be performed.
476 This uses the B and K codes to reference previously seen class types
477 and class qualifiers. */
478 int flag_do_squangling;
480 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
484 /* Nonzero means make the default pedwarns warnings instead of errors.
485 The value of this flag is ignored if -pedantic is specified. */
489 /* Table of language-dependent -f options.
490 STRING is the option name. VARIABLE is the address of the variable.
491 ON_VALUE is the value to store in VARIABLE
492 if `-fSTRING' is seen as an option.
493 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
495 static struct { const char *string; int *variable; int on_value;}
499 {"signed-char", &flag_signed_char, 1},
500 {"unsigned-char", &flag_signed_char, 0},
501 {"signed-bitfields", &flag_signed_bitfields, 1},
502 {"unsigned-bitfields", &flag_signed_bitfields, 0},
503 {"short-enums", &flag_short_enums, 1},
504 {"short-double", &flag_short_double, 1},
505 {"cond-mismatch", &flag_cond_mismatch, 1},
506 {"asm", &flag_no_asm, 0},
507 {"builtin", &flag_no_builtin, 0},
509 /* C++-only options. */
510 {"access-control", &flag_access_control, 1},
511 {"check-new", &flag_check_new, 1},
512 {"conserve-space", &flag_conserve_space, 1},
513 {"const-strings", &flag_const_strings, 1},
514 {"default-inline", &flag_default_inline, 1},
515 {"dollars-in-identifiers", &dollars_in_ident, 1},
516 {"elide-constructors", &flag_elide_constructors, 1},
517 {"external-templates", &flag_external_templates, 1},
518 {"for-scope", &flag_new_for_scope, 2},
519 {"gnu-keywords", &flag_no_gnu_keywords, 0},
520 {"handle-exceptions", &flag_exceptions, 1},
521 {"handle-signatures", &flag_handle_signatures, 1},
522 {"honor-std", &flag_honor_std, 1},
523 {"huge-objects", &flag_huge_objects, 1},
524 {"implement-inlines", &flag_implement_inlines, 1},
525 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
526 {"implicit-templates", &flag_implicit_templates, 1},
527 {"labels-ok", &flag_labels_ok, 1},
528 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
529 {"operator-names", &flag_operator_names, 1},
530 {"optional-diags", &flag_optional_diags, 1},
531 {"permissive", &flag_permissive, 1},
532 {"repo", &flag_use_repository, 1},
533 {"rtti", &flag_rtti, 1},
534 {"squangle", &flag_do_squangling, 1},
535 {"stats", &flag_detailed_statistics, 1},
536 {"strict-prototype", &flag_strict_prototype, 1},
537 {"this-is-variable", &flag_this_is_variable, 1},
538 {"vtable-gc", &flag_vtable_gc, 1},
539 {"vtable-thunks", &flag_vtable_thunks, 1},
540 {"weak", &flag_weak, 1},
541 {"xref", &flag_gnu_xref, 1}
544 /* Decode the string P as a language-specific option.
545 Return the number of strings consumed for a valid option.
546 Otherwise return 0. */
549 lang_decode_option (argc, argv)
558 int strings_processed;
561 strings_processed = cpp_handle_option (&parse_in, argc, argv);
563 strings_processed = 0;
564 #endif /* ! USE_CPPLIB */
566 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
568 else if (p[0] == '-' && p[1] == 'f')
570 /* Some kind of -f option.
571 P's value is the option sans `-f'.
572 Search for it in the table of options. */
577 /* Try special -f options. */
579 if (!strcmp (p, "handle-exceptions")
580 || !strcmp (p, "no-handle-exceptions"))
581 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
583 if (!strcmp (p, "memoize-lookups")
584 || !strcmp (p, "no-memoize-lookups")
585 || !strcmp (p, "save-memoized")
586 || !strcmp (p, "no-save-memoized")
587 || !strcmp (p, "no-all-virtual")
588 || !strcmp (p, "no-enum-int-equiv")
589 || !strcmp (p, "nonnull-objects")
590 || !strcmp (p, "ansi-overloading"))
595 else if (!strcmp (p, "all-virtual")
596 || !strcmp (p, "enum-int-equiv")
597 || !strcmp (p, "no-nonnull-objects")
598 || !strcmp (p, "no-ansi-overloading"))
600 warning ("-f%s is no longer supported", p);
603 else if (! strcmp (p, "alt-external-templates"))
605 flag_external_templates = 1;
606 flag_alt_external_templates = 1;
608 cp_deprecated ("-falt-external-templates");
610 else if (! strcmp (p, "no-alt-external-templates"))
612 flag_alt_external_templates = 0;
615 else if (!strcmp (p, "repo"))
617 flag_use_repository = 1;
618 flag_implicit_templates = 0;
621 else if (!strcmp (p, "guiding-decls"))
623 flag_guiding_decls = 1;
624 name_mangling_version = 0;
627 else if (!strcmp (p, "no-guiding-decls"))
629 flag_guiding_decls = 0;
632 else if (!strcmp (p, "this-is-variable"))
634 flag_this_is_variable = 1;
636 cp_deprecated ("-fthis-is-variable");
638 else if (!strcmp (p, "external-templates"))
640 flag_external_templates = 1;
642 cp_deprecated ("-fexternal-templates");
644 else if (!strncmp (p, "vtable-thunks", 13))
649 read_integral_parameter (p+14, p, 1);
653 /* If the machine file has a default setting, use that
654 for -fvtable-thunks. Otherwise, set it to version
656 #if DEFAULT_VTABLE_THUNKS
657 flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
659 flag_vtable_thunks = 1;
662 if (flag_vtable_thunks == 2)
663 /* v2 is a compatibility mode between v1 and v3. */
664 flag_vtable_thunks_compat = 1;
665 else if(flag_vtable_thunks == 3)
666 flag_vtable_thunks_compat = 0;
669 else if (!strcmp (p, "handle-signatures"))
671 flag_handle_signatures = 1;
673 cp_deprecated ("-fhandle-signatures");
675 else if (!strcmp (p, "new-abi"))
678 flag_do_squangling = 1;
680 flag_vtable_thunks = 2;
682 else if (!strcmp (p, "no-new-abi"))
685 flag_do_squangling = 0;
688 else if (!strncmp (p, "template-depth-", 15))
691 read_integral_parameter (p + 15, p - 2, max_tinst_depth);
693 else if (!strncmp (p, "name-mangling-version-", 22))
695 name_mangling_version =
696 read_integral_parameter (p + 22, p - 2, name_mangling_version);
699 !found && j < sizeof (lang_f_options) / sizeof (lang_f_options[0]);
702 if (!strcmp (p, lang_f_options[j].string))
704 *lang_f_options[j].variable = lang_f_options[j].on_value;
705 /* A goto here would be cleaner,
706 but breaks the vax pcc. */
709 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
710 && ! strcmp (p+3, lang_f_options[j].string))
712 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
718 else if (p[0] == '-' && p[1] == 'W')
722 /* The -W options control the warning behavior of the compiler. */
725 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
728 if (!strcmp (p, "implicit"))
729 warn_implicit = setting;
730 else if (!strcmp (p, "long-long"))
731 warn_long_long = setting;
732 else if (!strcmp (p, "return-type"))
733 warn_return_type = setting;
734 else if (!strcmp (p, "ctor-dtor-privacy"))
735 warn_ctor_dtor_privacy = setting;
736 else if (!strcmp (p, "write-strings"))
737 warn_write_strings = setting;
738 else if (!strcmp (p, "cast-qual"))
739 warn_cast_qual = setting;
740 else if (!strcmp (p, "char-subscripts"))
741 warn_char_subscripts = setting;
742 else if (!strcmp (p, "pointer-arith"))
743 warn_pointer_arith = setting;
744 else if (!strcmp (p, "missing-prototypes"))
745 warn_missing_prototypes = setting;
746 else if (!strcmp (p, "redundant-decls"))
747 warn_redundant_decls = setting;
748 else if (!strcmp (p, "missing-braces"))
749 warn_missing_braces = setting;
750 else if (!strcmp (p, "sign-compare"))
751 warn_sign_compare = setting;
752 else if (!strcmp (p, "format"))
753 warn_format = setting;
754 else if (!strcmp (p, "conversion"))
755 warn_conversion = setting;
756 else if (!strcmp (p, "parentheses"))
757 warn_parentheses = setting;
758 else if (!strcmp (p, "non-virtual-dtor"))
759 warn_nonvdtor = setting;
760 else if (!strcmp (p, "extern-inline"))
761 warn_extern_inline = setting;
762 else if (!strcmp (p, "reorder"))
763 warn_reorder = setting;
764 else if (!strcmp (p, "synth"))
765 warn_synth = setting;
766 else if (!strcmp (p, "pmf-conversions"))
767 warn_pmf2ptr = setting;
768 else if (!strcmp (p, "effc++"))
770 else if (!strcmp (p, "sign-promo"))
771 warn_sign_promo = setting;
772 else if (!strcmp (p, "old-style-cast"))
773 warn_old_style_cast = setting;
774 else if (!strcmp (p, "overloaded-virtual"))
775 warn_overloaded_virtual = setting;
776 else if (!strcmp (p, "multichar"))
777 warn_multichar = setting;
778 else if (!strcmp (p, "unknown-pragmas"))
779 /* Set to greater than 1, so that even unknown pragmas in
780 system headers will be warned about. */
781 warn_unknown_pragmas = setting * 2;
782 else if (!strcmp (p, "non-template-friend"))
783 warn_nontemplate_friend = setting;
784 else if (!strcmp (p, "deprecated"))
785 warn_deprecated = setting;
786 else if (!strcmp (p, "comment"))
787 ; /* cpp handles this one. */
788 else if (!strcmp (p, "comments"))
789 ; /* cpp handles this one. */
790 else if (!strcmp (p, "trigraphs"))
791 ; /* cpp handles this one. */
792 else if (!strcmp (p, "import"))
793 ; /* cpp handles this one. */
794 else if (!strcmp (p, "all"))
796 warn_return_type = setting;
797 warn_unused = setting;
798 warn_implicit = setting;
799 warn_switch = setting;
800 warn_format = setting;
801 warn_parentheses = setting;
802 warn_missing_braces = setting;
803 warn_sign_compare = setting;
804 warn_multichar = setting;
805 /* We save the value of warn_uninitialized, since if they put
806 -Wuninitialized on the command line, we need to generate a
807 warning about not using it without also specifying -O. */
808 if (warn_uninitialized != 1)
809 warn_uninitialized = (setting ? 2 : 0);
810 /* Only warn about unknown pragmas that are not in system
812 warn_unknown_pragmas = 1;
814 /* C++-specific warnings. */
815 warn_ctor_dtor_privacy = setting;
816 warn_nonvdtor = setting;
817 warn_reorder = setting;
818 warn_nontemplate_friend = setting;
820 else return strings_processed;
822 else if (!strcmp (p, "-ansi"))
823 flag_no_nonansi_builtin = 1, flag_ansi = 1,
824 flag_no_gnu_keywords = 1, flag_operator_names = 1;
826 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
827 it's probably safe to assume no sane person would ever want to use this
828 under normal circumstances. */
829 else if (!strcmp (p, "-spew-debug"))
833 return strings_processed;
838 /* Incorporate `const' and `volatile' qualifiers for member functions.
839 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
840 QUALS is a list of qualifiers. */
843 grok_method_quals (ctype, function, quals)
844 tree ctype, function, quals;
846 tree fntype = TREE_TYPE (function);
847 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
848 int type_quals = TYPE_UNQUALIFIED;
849 int dup_quals = TYPE_UNQUALIFIED;
853 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
859 quals = TREE_CHAIN (quals);
863 if (dup_quals != TYPE_UNQUALIFIED)
864 cp_error ("duplicate type qualifiers in %s declaration",
865 TREE_CODE (function) == FUNCTION_DECL
866 ? "member function" : "type");
868 ctype = cp_build_qualified_type (ctype, type_quals);
869 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
870 (TREE_CODE (fntype) == METHOD_TYPE
871 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
872 : TYPE_ARG_TYPES (fntype)));
874 fntype = build_exception_variant (fntype, raises);
876 TREE_TYPE (function) = fntype;
880 /* Warn when -fexternal-templates is used and #pragma
881 interface/implementation is not used all the times it should be,
885 warn_if_unknown_interface (decl)
888 static int already_warned = 0;
889 if (already_warned++)
892 if (flag_alt_external_templates)
894 struct tinst_level *til = tinst_for_decl ();
896 char *sf = input_filename;
901 input_filename = til->file;
903 cp_warning ("template `%#D' instantiated in file without #pragma interface",
909 cp_warning_at ("template `%#D' defined in file without #pragma interface",
913 /* A subroutine of the parser, to handle a component list. */
916 grok_x_components (specs)
919 struct pending_inline **p;
922 specs = strip_attrs (specs);
924 check_tag_decl (specs);
925 t = groktypename (build_decl_list (specs, NULL_TREE));
927 /* The only case where we need to do anything additional here is an
928 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
929 if (t == NULL_TREE || !ANON_UNION_TYPE_P (t))
932 fixup_anonymous_union (t);
933 finish_member_declaration (build_lang_field_decl (FIELD_DECL,
937 /* Ignore any inline function definitions in the anonymous union
938 since an anonymous union may not have function members. */
939 p = &pending_inlines;
940 for (; *p; *p = (*p)->next)
941 if (DECL_CONTEXT ((*p)->fndecl) != t)
945 /* Constructors for types with virtual baseclasses need an "in-charge" flag
946 saying whether this constructor is responsible for initialization of
947 virtual baseclasses or not. All destructors also need this "in-charge"
948 flag, which additionally determines whether or not the destructor should
949 free the memory for the object.
951 This function adds the "in-charge" flag to member function FN if
952 appropriate. It is called from grokclassfn and tsubst.
953 FN must be either a constructor or destructor.
955 For vtable thunks, types with polymorphic virtual bases need an
956 additional "vlist" argument which is an array of virtual tables.
957 In addition, if backwards-compatibility to v1 thunks is requested,
958 a wrapper constructor may be needed as well. */
961 maybe_retrofit_in_chrg (fn)
964 tree basetype, arg_types, parms, parm, fntype;
967 if (CLASSTYPE_IS_TEMPLATE (DECL_CLASS_CONTEXT (fn)))
968 /* Never retrofit arguments on template methods. */
971 if (DECL_CONSTRUCTOR_P (fn)
972 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CLASS_CONTEXT (fn))
973 && DECL_CONSTRUCTOR_FOR_VBASE (fn) == 0)
975 else if (! DECL_CONSTRUCTOR_P (fn)
976 && TREE_CHAIN (DECL_ARGUMENTS (fn)) == NULL_TREE)
981 if (DECL_CONSTRUCTOR_P (fn))
983 if (TYPE_USES_PVBASES (DECL_CLASS_CONTEXT (fn)))
985 DECL_CONSTRUCTOR_FOR_VBASE (fn) = CONSTRUCTOR_FOR_PVBASE;
986 if (flag_vtable_thunks_compat && varargs_function_p (fn))
987 sorry ("-fvtable-thunks=2 for vararg constructor", fn);
990 DECL_CONSTRUCTOR_FOR_VBASE (fn) = CONSTRUCTOR_FOR_VBASE;
992 else if (TYPE_USES_PVBASES (DECL_CLASS_CONTEXT (fn)))
993 DECL_CONSTRUCTOR_FOR_VBASE (fn) = DESTRUCTOR_FOR_PVBASE;
995 /* Retrieve the arguments, because it is potentially modified twice. */
996 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
997 basetype = TREE_TYPE (TREE_VALUE (arg_types));
998 arg_types = TREE_CHAIN (arg_types);
1000 if (DECL_CONSTRUCTOR_FOR_PVBASE_P (fn)
1001 || DECL_DESTRUCTOR_FOR_PVBASE_P (fn))
1003 /* Add the __vlist argument first. See __in_chrg below. */
1004 tree id = vlist_identifier;
1005 if (DECL_DESTRUCTOR_FOR_PVBASE_P (fn))
1006 id = get_identifier (VLIST1_NAME);
1007 parm = build_decl (PARM_DECL, id, vlist_type_node);
1008 SET_DECL_ARTIFICIAL (parm);
1009 DECL_ARG_TYPE (parm) = vlist_type_node;
1010 parms = DECL_ARGUMENTS (fn);
1011 /* Add it after 'this'. */
1012 TREE_CHAIN (parm) = TREE_CHAIN (parms);
1013 TREE_CHAIN (parms) = parm;
1015 arg_types = hash_tree_chain (vlist_type_node, arg_types);
1018 /* First add it to DECL_ARGUMENTS... */
1019 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
1020 /* Mark the artificial `__in_chrg' parameter as "artificial". */
1021 SET_DECL_ARTIFICIAL (parm);
1022 DECL_ARG_TYPE (parm) = integer_type_node;
1023 TREE_READONLY (parm) = 1;
1024 parms = DECL_ARGUMENTS (fn);
1025 TREE_CHAIN (parm) = TREE_CHAIN (parms);
1026 TREE_CHAIN (parms) = parm;
1028 /* ...and then to TYPE_ARG_TYPES. */
1029 arg_types = hash_tree_chain (integer_type_node, arg_types);
1030 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
1032 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1033 fntype = build_exception_variant (fntype,
1034 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
1035 TREE_TYPE (fn) = fntype;
1038 /* Classes overload their constituent function names automatically.
1039 When a function name is declared in a record structure,
1040 its name is changed to it overloaded name. Since names for
1041 constructors and destructors can conflict, we place a leading
1042 '$' for destructors.
1044 CNAME is the name of the class we are grokking for.
1046 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
1048 FLAGS contains bits saying what's special about today's
1049 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
1051 If FUNCTION is a destructor, then we must add the `auto-delete' field
1052 as a second parameter. There is some hair associated with the fact
1053 that we must "declare" this variable in the manner consistent with the
1054 way the rest of the arguments were declared.
1056 QUALS are the qualifiers for the this pointer. */
1059 grokclassfn (ctype, function, flags, quals)
1060 tree ctype, function;
1061 enum overload_flags flags;
1064 tree fn_name = DECL_NAME (function);
1069 if (fn_name == NULL_TREE)
1071 error ("name missing for member function");
1072 fn_name = get_identifier ("<anonymous>");
1073 DECL_NAME (function) = fn_name;
1077 qualtype = grok_method_quals (ctype, function, quals);
1081 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
1082 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1084 /* Must add the class instance variable up front. */
1085 /* Right now we just make this a pointer. But later
1086 we may wish to make it special. */
1087 tree type = TREE_VALUE (arg_types);
1090 if ((flag_this_is_variable > 0)
1091 && (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)))
1094 parm = build_decl (PARM_DECL, this_identifier, type);
1095 /* Mark the artificial `this' parameter as "artificial". */
1096 SET_DECL_ARTIFICIAL (parm);
1097 DECL_ARG_TYPE (parm) = type;
1098 /* We can make this a register, so long as we don't
1099 accidentally complain if someone tries to take its address. */
1100 DECL_REGISTER (parm) = 1;
1102 TREE_READONLY (parm) = 1;
1103 TREE_CHAIN (parm) = last_function_parms;
1104 last_function_parms = parm;
1107 DECL_ARGUMENTS (function) = last_function_parms;
1108 /* First approximations. */
1109 DECL_CONTEXT (function) = ctype;
1110 DECL_CLASS_CONTEXT (function) = ctype;
1112 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1114 maybe_retrofit_in_chrg (function);
1115 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
1118 if (flags == DTOR_FLAG)
1120 DECL_ASSEMBLER_NAME (function) =
1121 build_destructor_name (ctype, DECL_DESTRUCTOR_FOR_PVBASE_P (function));
1122 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1125 set_mangled_name_for_decl (function);
1128 /* Work on the expr used by alignof (this is only called by the parser). */
1137 if (processing_template_decl)
1138 return build_min (ALIGNOF_EXPR, sizetype, expr);
1140 if (TREE_CODE (expr) == COMPONENT_REF
1141 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1142 error ("`__alignof__' applied to a bit-field");
1144 if (TREE_CODE (expr) == INDIRECT_REF)
1146 best = t = TREE_OPERAND (expr, 0);
1147 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1149 while (TREE_CODE (t) == NOP_EXPR
1150 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1153 t = TREE_OPERAND (t, 0);
1154 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1155 if (thisalign > bestalign)
1156 best = t, bestalign = thisalign;
1158 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1162 /* ANSI says arrays and fns are converted inside comma.
1163 But we can't convert them in build_compound_expr
1164 because that would break commas in lvalues.
1165 So do the conversion here if operand was a comma. */
1166 if (TREE_CODE (expr) == COMPOUND_EXPR
1167 && (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1168 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
1169 expr = default_conversion (expr);
1170 return c_alignof (TREE_TYPE (expr));
1174 /* Create an ARRAY_REF, checking for the user doing things backwards
1178 grok_array_decl (array_expr, index_exp)
1179 tree array_expr, index_exp;
1181 tree type = TREE_TYPE (array_expr);
1182 tree p1, p2, i1, i2;
1184 if (type == error_mark_node || index_exp == error_mark_node)
1185 return error_mark_node;
1186 if (processing_template_decl)
1187 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1188 array_expr, index_exp);
1190 if (type == NULL_TREE)
1192 /* Something has gone very wrong. Assume we are mistakenly reducing
1193 an expression instead of a declaration. */
1194 error ("parser may be lost: is there a '{' missing somewhere?");
1198 if (TREE_CODE (type) == OFFSET_TYPE
1199 || TREE_CODE (type) == REFERENCE_TYPE)
1200 type = TREE_TYPE (type);
1202 /* If they have an `operator[]', use that. */
1203 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1204 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1205 array_expr, index_exp, NULL_TREE);
1207 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1208 is a little-known fact that, if `a' is an array and `i' is an
1209 int, you can write `i[a]', which means the same thing as `a[i]'. */
1211 if (TREE_CODE (type) == ARRAY_TYPE)
1214 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1216 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1219 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1221 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1222 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1224 if ((p1 && i2) && (i1 && p2))
1225 error ("ambiguous conversion for array subscript");
1228 array_expr = p1, index_exp = i2;
1230 array_expr = p2, index_exp = i1;
1233 cp_error ("invalid types `%T[%T]' for array subscript",
1234 type, TREE_TYPE (index_exp));
1235 return error_mark_node;
1238 if (array_expr == error_mark_node || index_exp == error_mark_node)
1239 error ("ambiguous conversion for array subscript");
1241 return build_array_ref (array_expr, index_exp);
1244 /* Given the cast expression EXP, checking out its validity. Either return
1245 an error_mark_node if there was an unavoidable error, return a cast to
1246 void for trying to delete a pointer w/ the value 0, or return the
1247 call to delete. If DOING_VEC is 1, we handle things differently
1248 for doing an array delete. If DOING_VEC is 2, they gave us the
1249 array size as an argument to delete.
1250 Implements ARM $5.3.4. This is called from the parser. */
1253 delete_sanity (exp, size, doing_vec, use_global_delete)
1255 int doing_vec, use_global_delete;
1258 /* For a regular vector delete (aka, no size argument) we will pass
1259 this down as a NULL_TREE into build_vec_delete. */
1260 tree maxindex = NULL_TREE;
1262 if (exp == error_mark_node)
1265 if (processing_template_decl)
1267 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1268 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1269 DELETE_EXPR_USE_VEC (t) = doing_vec;
1273 if (TREE_CODE (exp) == OFFSET_REF)
1274 exp = resolve_offset_ref (exp);
1275 exp = convert_from_reference (exp);
1276 t = stabilize_reference (exp);
1277 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1279 if (t == NULL_TREE || t == error_mark_node)
1281 cp_error ("type `%#T' argument given to `delete', expected pointer",
1283 return error_mark_node;
1288 maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node);
1289 pedwarn ("anachronistic use of array size in vector delete");
1292 type = TREE_TYPE (t);
1294 /* As of Valley Forge, you can delete a pointer to const. */
1296 /* You can't delete functions. */
1297 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1299 error ("cannot delete a function");
1300 return error_mark_node;
1303 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1304 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1305 cp_warning ("`%T' is not a pointer-to-object type", type);
1307 /* An array can't have been allocated by new, so complain. */
1308 if (TREE_CODE (t) == ADDR_EXPR
1309 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1311 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1313 /* Deleting a pointer with the value zero is valid and has no effect. */
1314 if (integer_zerop (t))
1315 return build1 (NOP_EXPR, void_type_node, t);
1318 return build_vec_delete (t, maxindex, integer_one_node,
1319 integer_zero_node, use_global_delete);
1322 if (IS_AGGR_TYPE (TREE_TYPE (type))
1323 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1325 /* Only do access checking here; we'll be calling op delete
1326 from the destructor. */
1327 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1328 LOOKUP_NORMAL, NULL_TREE);
1329 if (tmp == error_mark_node)
1330 return error_mark_node;
1333 return build_delete (type, t, integer_three_node,
1334 LOOKUP_NORMAL, use_global_delete);
1338 /* Report an error if the indicated template declaration is not the
1339 sort of thing that should be a member template. */
1342 check_member_template (tmpl)
1347 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1348 decl = DECL_TEMPLATE_RESULT (tmpl);
1350 if (TREE_CODE (decl) == FUNCTION_DECL
1351 || (TREE_CODE (decl) == TYPE_DECL
1352 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1354 if (current_function_decl)
1355 /* 14.5.2.2 [temp.mem]
1357 A local class shall not have member templates. */
1358 cp_error ("declaration of member template `%#D' in local class",
1361 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1363 /* 14.5.2.3 [temp.mem]
1365 A member function template shall not be virtual. */
1367 ("invalid use of `virtual' in template declaration of `%#D'",
1369 DECL_VIRTUAL_P (decl) = 0;
1372 /* The debug-information generating code doesn't know what to do
1373 with member templates. */
1374 DECL_IGNORED_P (tmpl) = 1;
1377 cp_error ("template declaration of `%#D'", decl);
1380 /* Return true iff TYPE is a valid Java parameter or return type. */
1383 acceptable_java_type (type)
1386 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1388 if (TREE_CODE (type) == POINTER_TYPE)
1390 type = TREE_TYPE (type);
1391 if (TREE_CODE (type) == RECORD_TYPE)
1394 if (! TYPE_FOR_JAVA (type))
1396 if (! CLASSTYPE_TEMPLATE_INFO (type))
1398 args = CLASSTYPE_TI_ARGS (type);
1399 i = TREE_VEC_LENGTH (args);
1402 type = TREE_VEC_ELT (args, i);
1403 if (TREE_CODE (type) == POINTER_TYPE)
1404 type = TREE_TYPE (type);
1405 if (! TYPE_FOR_JAVA (type))
1414 /* For a METHOD in a Java class CTYPE, return 1 if
1415 the parameter and return types are valid Java types.
1416 Otherwise, print appropriate error messages, and return 0. */
1419 check_java_method (method)
1423 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1424 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1425 if (! acceptable_java_type (ret_type))
1427 cp_error ("Java method '%D' has non-Java return type `%T'",
1431 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1433 tree type = TREE_VALUE (arg_types);
1434 if (! acceptable_java_type (type))
1436 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1441 return jerr ? 0 : 1;
1444 /* Sanity check: report error if this function FUNCTION is not
1445 really a member of the class (CTYPE) it is supposed to belong to.
1446 CNAME is the same here as it is for grokclassfn above. */
1449 check_classfn (ctype, function)
1450 tree ctype, function;
1452 tree fn_name = DECL_NAME (function);
1453 tree fndecl, fndecls;
1454 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1458 if (DECL_USE_TEMPLATE (function)
1459 && is_member_template (DECL_TI_TEMPLATE (function)))
1460 /* Since this is a specialization of a member template,
1461 we're not going to find the declaration in the class.
1464 struct S { template <typename T> void f(T); };
1465 template <> void S::f(int);
1467 we're not going to find `S::f(int)', but there's no
1468 reason we should, either. We let our callers know we didn't
1469 find the method, but we don't complain. */
1472 if (method_vec != 0)
1474 methods = &TREE_VEC_ELT (method_vec, 0);
1475 end = TREE_VEC_END (method_vec);
1477 /* First suss out ctors and dtors. */
1478 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1479 && DECL_CONSTRUCTOR_P (function))
1481 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1482 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
1485 while (++methods != end && *methods)
1488 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1491 for (fndecls = *methods; fndecls != NULL_TREE;
1492 fndecls = OVL_NEXT (fndecls))
1494 fndecl = OVL_CURRENT (fndecls);
1495 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL, or
1496 for a for member function of a template class, is
1497 not mangled, so the check below does not work
1498 correctly in that case. Since mangled destructor
1499 names do not include the type of the arguments,
1500 we can't use this short-cut for them, either.
1501 (It's not legal to declare arguments for a
1502 destructor, but some people try.) */
1503 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function))
1504 && (DECL_ASSEMBLER_NAME (function)
1505 != DECL_NAME (function))
1506 && (DECL_ASSEMBLER_NAME (fndecl)
1507 != DECL_NAME (fndecl))
1508 && (DECL_ASSEMBLER_NAME (function)
1509 == DECL_ASSEMBLER_NAME (fndecl)))
1512 /* We cannot simply call decls_match because this
1513 doesn't work for static member functions that are
1514 pretending to be methods, and because the name
1515 may have been changed by asm("new_name"). */
1516 if (DECL_NAME (function) == DECL_NAME (fndecl))
1518 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1519 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1521 /* Get rid of the this parameter on functions that become
1523 if (DECL_STATIC_FUNCTION_P (fndecl)
1524 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1525 p1 = TREE_CHAIN (p1);
1527 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1528 TREE_TYPE (TREE_TYPE (fndecl)))
1529 && compparms (p1, p2)
1530 && (DECL_TEMPLATE_SPECIALIZATION (function)
1531 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1532 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1533 || (DECL_TI_TEMPLATE (function)
1534 == DECL_TI_TEMPLATE (fndecl))))
1543 if (methods != end && *methods)
1545 tree fndecl = *methods;
1546 cp_error ("prototype for `%#D' does not match any in class `%T'",
1548 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1549 OVL_CURRENT (fndecl));
1550 while (fndecl = OVL_NEXT (fndecl), fndecl)
1551 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1556 if (TYPE_SIZE (ctype) == 0)
1557 incomplete_type_error (function, ctype);
1559 cp_error ("no `%#D' member function declared in class `%T'",
1563 /* If we did not find the method in the class, add it to avoid
1564 spurious errors (unless the CTYPE is not yet defined, in which
1565 case we'll only confuse ourselves when the function is declared
1566 properly within the class. */
1567 if (TYPE_SIZE (ctype))
1568 add_method (ctype, methods, function);
1572 /* We have just processed the DECL, which is a static data member.
1573 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1574 present, is the assembly-language name for the data member.
1575 NEED_POP and FLAGS are as for cp_finish_decl. */
1578 finish_static_data_member_decl (decl, init, asmspec_tree, need_pop, flags)
1588 asmspec = TREE_STRING_POINTER (asmspec_tree);
1590 my_friendly_assert (TREE_PUBLIC (decl), 0);
1592 /* We cannot call pushdecl here, because that would fill in the
1593 decl of our TREE_CHAIN. Instead, we modify cp_finish_decl to do
1594 the right thing, namely, to put this decl out straight away. */
1595 /* current_class_type can be NULL_TREE in case of error. */
1596 if (!asmspec && current_class_type)
1598 DECL_INITIAL (decl) = error_mark_node;
1599 DECL_ASSEMBLER_NAME (decl)
1600 = build_static_name (current_class_type, DECL_NAME (decl));
1602 if (! processing_template_decl)
1604 if (!pending_statics)
1605 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1607 if (pending_statics_used == pending_statics->num_elements)
1608 VARRAY_GROW (pending_statics,
1609 2 * pending_statics->num_elements);
1610 VARRAY_TREE (pending_statics, pending_statics_used) = decl;
1611 ++pending_statics_used;
1614 /* Static consts need not be initialized in the class definition. */
1615 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1617 static int explanation = 0;
1619 error ("initializer invalid for static member with constructor");
1620 if (explanation++ == 0)
1621 error ("(you really want to initialize it separately)");
1624 /* Force the compiler to know when an uninitialized static const
1625 member is being used. */
1626 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1627 TREE_USED (decl) = 1;
1628 DECL_INITIAL (decl) = init;
1629 DECL_IN_AGGR_P (decl) = 1;
1630 DECL_CONTEXT (decl) = current_class_type;
1631 DECL_CLASS_CONTEXT (decl) = current_class_type;
1633 cp_finish_decl (decl, init, asmspec_tree, need_pop, flags);
1636 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1637 of a structure component, returning a FIELD_DECL node.
1638 QUALS is a list of type qualifiers for this decl (such as for declaring
1639 const member functions).
1641 This is done during the parsing of the struct declaration.
1642 The FIELD_DECL nodes are chained together and the lot of them
1643 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1647 If class A defines that certain functions in class B are friends, then
1648 the way I have set things up, it is B who is interested in permission
1649 granted by A. However, it is in A's context that these declarations
1650 are parsed. By returning a void_type_node, class A does not attempt
1651 to incorporate the declarations of the friends within its structure.
1653 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1654 CHANGES TO CODE IN `start_method'. */
1657 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1658 tree declarator, declspecs, init, asmspec_tree, attrlist;
1660 register tree value;
1662 int flags = LOOKUP_ONLYCONVERTING;
1664 /* Convert () initializers to = initializers. */
1665 if (init == NULL_TREE && declarator != NULL_TREE
1666 && TREE_CODE (declarator) == CALL_EXPR
1667 && TREE_OPERAND (declarator, 0)
1668 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1669 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1670 && parmlist_is_exprlist (TREE_OPERAND (declarator, 1)))
1672 init = TREE_OPERAND (declarator, 1);
1673 declarator = TREE_OPERAND (declarator, 0);
1677 if (declspecs == NULL_TREE
1678 && TREE_CODE (declarator) == SCOPE_REF
1679 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1681 /* Access declaration */
1682 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1684 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1685 pop_nested_class ();
1686 return do_class_using_decl (declarator);
1690 && TREE_CODE (init) == TREE_LIST
1691 && TREE_VALUE (init) == error_mark_node
1692 && TREE_CHAIN (init) == NULL_TREE)
1695 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, attrlist);
1696 if (! value || value == error_mark_node)
1697 /* friend or constructor went bad. */
1700 /* Pass friendly classes back. */
1701 if (TREE_CODE (value) == VOID_TYPE)
1702 return void_type_node;
1704 if (DECL_NAME (value) != NULL_TREE
1705 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1706 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1707 cp_error ("member `%D' conflicts with virtual function table field name",
1710 /* Stash away type declarations. */
1711 if (TREE_CODE (value) == TYPE_DECL)
1713 DECL_NONLOCAL (value) = 1;
1714 DECL_CONTEXT (value) = current_class_type;
1715 DECL_CLASS_CONTEXT (value) = current_class_type;
1717 /* Now that we've updated the context, we need to remangle the
1718 name for this TYPE_DECL. */
1719 DECL_ASSEMBLER_NAME (value) = DECL_NAME (value);
1720 if (!uses_template_parms (value))
1721 DECL_ASSEMBLER_NAME (value) =
1722 get_identifier (build_overload_name (TREE_TYPE (value), 1, 1));
1727 if (IS_SIGNATURE (current_class_type)
1728 && TREE_CODE (value) != FUNCTION_DECL)
1730 error ("field declaration not allowed in signature");
1731 return void_type_node;
1734 if (DECL_IN_AGGR_P (value))
1736 cp_error ("`%D' is already defined in `%T'", value,
1737 DECL_CONTEXT (value));
1738 return void_type_node;
1742 asmspec = TREE_STRING_POINTER (asmspec_tree);
1746 if (IS_SIGNATURE (current_class_type)
1747 && TREE_CODE (value) == FUNCTION_DECL)
1749 error ("function declarations cannot have initializers in signature");
1752 else if (TREE_CODE (value) == FUNCTION_DECL)
1754 grok_function_init (value, init);
1757 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1758 /* Already complained in grokdeclarator. */
1762 /* We allow initializers to become parameters to base
1764 if (TREE_CODE (init) == TREE_LIST)
1766 if (TREE_CHAIN (init) == NULL_TREE)
1767 init = TREE_VALUE (init);
1769 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1772 if (TREE_CODE (init) == CONST_DECL)
1773 init = DECL_INITIAL (init);
1774 else if (TREE_READONLY_DECL_P (init))
1775 init = decl_constant_value (init);
1776 else if (TREE_CODE (init) == CONSTRUCTOR)
1777 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1778 my_friendly_assert (TREE_PERMANENT (init), 192);
1779 if (init == error_mark_node)
1780 /* We must make this look different than `error_mark_node'
1781 because `decl_const_value' would mis-interpret it
1782 as only meaning that this VAR_DECL is defined. */
1783 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1784 else if (processing_template_decl)
1786 else if (! TREE_CONSTANT (init))
1788 /* We can allow references to things that are effectively
1789 static, since references are initialized with the address. */
1790 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1791 || (TREE_STATIC (init) == 0
1792 && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
1793 || DECL_EXTERNAL (init) == 0)))
1795 error ("field initializer is not constant");
1796 init = error_mark_node;
1802 /* The corresponding pop_obstacks is in cp_finish_decl. */
1803 push_obstacks_nochange ();
1805 if (processing_template_decl && ! current_function_decl
1806 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1807 value = push_template_decl (value);
1810 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1811 TREE_VALUE (attrlist));
1813 if (TREE_CODE (value) == VAR_DECL)
1815 finish_static_data_member_decl (value, init, asmspec_tree,
1816 /*need_pop=*/1, flags);
1819 if (TREE_CODE (value) == FIELD_DECL)
1823 /* This must override the asm specifier which was placed
1824 by grokclassfn. Lay this out fresh. */
1825 DECL_RTL (value) = NULL_RTX;
1826 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1828 if (DECL_INITIAL (value) == error_mark_node)
1829 init = error_mark_node;
1830 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1831 DECL_INITIAL (value) = init;
1832 DECL_IN_AGGR_P (value) = 1;
1835 if (TREE_CODE (value) == FUNCTION_DECL)
1839 /* This must override the asm specifier which was placed
1840 by grokclassfn. Lay this out fresh. */
1841 DECL_RTL (value) = NULL_RTX;
1842 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1844 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1846 /* Pass friends back this way. */
1847 if (DECL_FRIEND_P (value))
1848 return void_type_node;
1850 #if 0 /* Just because a fn is declared doesn't mean we'll try to define it. */
1851 if (current_function_decl && ! IS_SIGNATURE (current_class_type))
1852 cp_error ("method `%#D' of local class must be defined in class body",
1856 DECL_IN_AGGR_P (value) = 1;
1859 my_friendly_abort (21);
1864 /* Like `grokfield', but for bitfields.
1865 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1868 grokbitfield (declarator, declspecs, width)
1869 tree declarator, declspecs, width;
1871 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1874 if (! value) return NULL_TREE; /* friends went bad. */
1876 /* Pass friendly classes back. */
1877 if (TREE_CODE (value) == VOID_TYPE)
1878 return void_type_node;
1880 if (TREE_CODE (value) == TYPE_DECL)
1882 cp_error ("cannot declare `%D' to be a bitfield type", value);
1886 /* Usually, finish_struct_1 catches bitifields with invalid types.
1887 But, in the case of bitfields with function type, we confuse
1888 ourselves into thinking they are member functions, so we must
1890 if (TREE_CODE (value) == FUNCTION_DECL)
1892 cp_error ("cannot declare bitfield `%D' with funcion type",
1897 if (IS_SIGNATURE (current_class_type))
1899 error ("field declaration not allowed in signature");
1900 return void_type_node;
1903 if (DECL_IN_AGGR_P (value))
1905 cp_error ("`%D' is already defined in the class %T", value,
1906 DECL_CONTEXT (value));
1907 return void_type_node;
1910 GNU_xref_member (current_class_name, value);
1912 if (TREE_STATIC (value))
1914 cp_error ("static member `%D' cannot be a bitfield", value);
1917 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0, 0);
1919 if (width != error_mark_node)
1921 constant_expression_warning (width);
1922 DECL_INITIAL (value) = width;
1923 SET_DECL_C_BIT_FIELD (value);
1926 DECL_IN_AGGR_P (value) = 1;
1931 grokoptypename (declspecs, declarator)
1932 tree declspecs, declarator;
1934 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1935 return build_typename_overload (t);
1938 /* When a function is declared with an initializer,
1939 do the right thing. Currently, there are two possibilities:
1944 // initialization possibility #1.
1945 virtual void f () = 0;
1967 // initialization possibility #2
1974 copy_assignment_arg_p (parmtype, virtualp)
1976 int virtualp ATTRIBUTE_UNUSED;
1978 if (current_class_type == NULL_TREE)
1981 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1982 parmtype = TREE_TYPE (parmtype);
1984 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1986 /* Non-standard hack to support old Booch components. */
1987 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1996 grok_function_init (decl, init)
2000 /* An initializer for a function tells how this function should
2002 tree type = TREE_TYPE (decl);
2004 if (TREE_CODE (type) == FUNCTION_TYPE)
2005 cp_error ("initializer specified for non-member function `%D'", decl);
2007 /* We'll check for this in finish_struct_1. */
2008 else if (DECL_VINDEX (decl) == NULL_TREE)
2009 cp_error ("initializer specified for non-virtual method `%D'", decl);
2011 else if (integer_zerop (init))
2014 /* Mark this function as being "defined". */
2015 DECL_INITIAL (decl) = error_mark_node;
2016 /* pure virtual destructors must be defined. */
2017 /* pure virtual needs to be defined (as abort) only when put in
2018 vtbl. For wellformed call, it should be itself. pr4737 */
2019 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
2021 extern tree abort_fndecl;
2022 /* Give this node rtl from `abort'. */
2023 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
2026 DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
2027 if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
2030 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
2032 if (copy_assignment_arg_p (parmtype, 1))
2033 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
2037 cp_error ("invalid initializer for virtual method `%D'", decl);
2041 cplus_decl_attributes (decl, attributes, prefix_attributes)
2042 tree decl, attributes, prefix_attributes;
2044 if (decl == NULL_TREE || decl == void_type_node)
2047 if (TREE_CODE (decl) == TEMPLATE_DECL)
2048 decl = DECL_TEMPLATE_RESULT (decl);
2050 decl_attributes (decl, attributes, prefix_attributes);
2052 if (TREE_CODE (decl) == TYPE_DECL)
2053 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
2056 /* CONSTRUCTOR_NAME:
2057 Return the name for the constructor (or destructor) for the
2058 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2059 IDENTIFIER_NODE. When given a template, this routine doesn't
2060 lose the specialization. */
2063 constructor_name_full (thing)
2066 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
2067 || TREE_CODE (thing) == TEMPLATE_TEMPLATE_PARM
2068 || TREE_CODE (thing) == TYPENAME_TYPE)
2069 thing = TYPE_NAME (thing);
2070 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
2072 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
2073 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
2075 thing = TYPE_NAME (thing);
2077 if (TREE_CODE (thing) == TYPE_DECL
2078 || (TREE_CODE (thing) == TEMPLATE_DECL
2079 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
2080 thing = DECL_NAME (thing);
2081 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
2085 /* CONSTRUCTOR_NAME:
2086 Return the name for the constructor (or destructor) for the
2087 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2088 IDENTIFIER_NODE. When given a template, return the plain
2089 unspecialized name. */
2092 constructor_name (thing)
2096 thing = constructor_name_full (thing);
2097 t = IDENTIFIER_TEMPLATE (thing);
2103 /* Cache the value of this class's main virtual function table pointer
2104 in a register variable. This will save one indirection if a
2105 more than one virtual function call is made this function. */
2110 extern tree base_init_expr;
2112 if (base_init_expr == 0
2113 && DECL_CONSTRUCTOR_P (current_function_decl))
2115 if (processing_template_decl)
2116 add_tree (build_min_nt
2118 current_member_init_list, current_base_init_list));
2120 emit_base_init (current_class_type, 0);
2124 /* Record the existence of an addressable inline function. */
2127 mark_inline_for_output (decl)
2130 decl = DECL_MAIN_VARIANT (decl);
2131 if (DECL_SAVED_INLINE (decl))
2133 my_friendly_assert (TREE_PERMANENT (decl), 363);
2134 DECL_SAVED_INLINE (decl) = 1;
2136 VARRAY_TREE_INIT (saved_inlines, 32, "saved_inlines");
2138 if (saved_inlines_used == saved_inlines->num_elements)
2139 VARRAY_GROW (saved_inlines,
2140 2 * saved_inlines->num_elements);
2141 VARRAY_TREE (saved_inlines, saved_inlines_used) = decl;
2142 ++saved_inlines_used;
2148 temp_name_counter = 0;
2151 /* Hand off a unique name which can be used for variable we don't really
2152 want to know about anyway, for example, the anonymous variables which
2153 are needed to make references work. Declare this thing so we can use it.
2154 The variable created will be of type TYPE.
2156 STATICP is nonzero if this variable should be static. */
2159 get_temp_name (type, staticp)
2163 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
2165 int toplev = toplevel_bindings_p ();
2167 push_obstacks_nochange ();
2168 if (toplev || staticp)
2170 end_temporary_allocation ();
2171 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
2172 decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
2176 sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
2177 decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
2179 TREE_USED (decl) = 1;
2180 TREE_STATIC (decl) = staticp;
2181 DECL_ARTIFICIAL (decl) = 1;
2183 /* If this is a local variable, then lay out its rtl now.
2184 Otherwise, callers of this function are responsible for dealing
2185 with this variable's rtl. */
2189 expand_decl_init (decl);
2196 /* Get a variable which we can use for multiple assignments.
2197 It is not entered into current_binding_level, because
2198 that breaks things when it comes time to do final cleanups
2199 (which take place "outside" the binding contour of the function). */
2202 get_temp_regvar (type, init)
2207 decl = build_decl (VAR_DECL, NULL_TREE, type);
2208 TREE_USED (decl) = 1;
2209 DECL_REGISTER (decl) = 1;
2210 DECL_ARTIFICIAL (decl) = 1;
2212 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2213 /* We can expand these without fear, since they cannot need
2214 constructors or destructors. */
2215 expand_expr (build_modify_expr (decl, INIT_EXPR, init),
2216 NULL_RTX, VOIDmode, 0);
2221 /* Hunts through the global anonymous union ANON_DECL, building
2222 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2223 returns a VAR_DECL whose size is the same as the size of the
2224 ANON_DECL, if one is available. */
2227 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2233 tree type = TREE_TYPE (anon_decl);
2234 tree main_decl = NULL_TREE;
2237 for (field = TYPE_FIELDS (type);
2239 field = TREE_CHAIN (field))
2243 if (DECL_ARTIFICIAL (field))
2245 if (TREE_CODE (field) != FIELD_DECL)
2247 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2252 if (TREE_PRIVATE (field))
2253 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2254 else if (TREE_PROTECTED (field))
2255 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2257 if (DECL_NAME (field) == NULL_TREE
2258 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2260 decl = build_anon_union_vars (field, elems, static_p, external_p);
2264 else if (DECL_NAME (field) == NULL_TREE)
2268 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2269 /* tell `pushdecl' that this is not tentative. */
2270 DECL_INITIAL (decl) = error_mark_node;
2271 TREE_PUBLIC (decl) = 0;
2272 TREE_STATIC (decl) = static_p;
2273 DECL_EXTERNAL (decl) = external_p;
2274 decl = pushdecl (decl);
2275 DECL_INITIAL (decl) = NULL_TREE;
2278 /* Only write out one anon union element--choose the one that
2279 can hold them all. */
2280 if (main_decl == NULL_TREE
2281 && simple_cst_equal (DECL_SIZE (decl),
2282 DECL_SIZE (anon_decl)) == 1)
2285 /* ??? This causes there to be no debug info written out
2287 TREE_ASM_WRITTEN (decl) = 1;
2289 if (DECL_NAME (field) == NULL_TREE
2290 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2291 /* The remainder of the processing was already done in the
2295 /* If there's a cleanup to do, it belongs in the
2296 TREE_PURPOSE of the following TREE_LIST. */
2297 *elems = scratch_tree_cons (NULL_TREE, decl, *elems);
2298 TREE_TYPE (*elems) = type;
2304 /* Finish off the processing of a UNION_TYPE structure.
2305 If there are static members, then all members are
2306 static, and must be laid out together. If the
2307 union is an anonymous union, we arrange for that
2308 as well. PUBLIC_P is nonzero if this union is
2309 not declared static. */
2312 finish_anon_union (anon_union_decl)
2313 tree anon_union_decl;
2315 tree type = TREE_TYPE (anon_union_decl);
2316 tree elems = NULL_TREE;
2318 int public_p = TREE_PUBLIC (anon_union_decl);
2319 int static_p = TREE_STATIC (anon_union_decl);
2320 int external_p = DECL_EXTERNAL (anon_union_decl);
2322 if (TYPE_FIELDS (type) == NULL_TREE)
2327 error ("global anonymous unions must be declared static");
2331 main_decl = build_anon_union_vars (anon_union_decl, &elems,
2332 static_p, external_p);
2334 if (main_decl == NULL_TREE)
2336 warning ("anonymous union with no members");
2342 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2343 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2346 /* The following call assumes that there are never any cleanups
2347 for anonymous unions--a reasonable assumption. */
2348 expand_anon_union_decl (anon_union_decl, NULL_TREE, elems);
2351 /* Finish processing a builtin type TYPE. It's name is NAME,
2352 its fields are in the array FIELDS. LEN is the number of elements
2353 in FIELDS minus one, or put another way, it is the maximum subscript
2356 It is given the same alignment as ALIGN_TYPE. */
2359 finish_builtin_type (type, name, fields, len, align_type)
2368 TYPE_FIELDS (type) = fields[0];
2369 for (i = 0; i < len; i++)
2371 layout_type (TREE_TYPE (fields[i]));
2372 DECL_FIELD_CONTEXT (fields[i]) = type;
2373 TREE_CHAIN (fields[i]) = fields[i+1];
2375 DECL_FIELD_CONTEXT (fields[i]) = type;
2376 DECL_CLASS_CONTEXT (fields[i]) = type;
2377 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2379 #if 0 /* not yet, should get fixed properly later */
2380 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2382 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2384 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2385 layout_decl (TYPE_NAME (type), 0);
2388 /* Auxiliary functions to make type signatures for
2389 `operator new' and `operator delete' correspond to
2390 what compiler will be expecting. */
2393 coerce_new_type (type)
2398 if (TREE_CODE (type) == METHOD_TYPE)
2399 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2400 if (! same_type_p (TREE_TYPE (type), ptr_type_node))
2401 e1 = 1, error ("`operator new' must return type `void *'");
2403 /* Technically the type must be `size_t', but we may not know
2405 if (TYPE_ARG_TYPES (type) == NULL_TREE)
2406 e1 = 1, error ("`operator new' takes type `size_t' parameter");
2407 else if (! same_type_p (TREE_VALUE (TYPE_ARG_TYPES (type)), sizetype))
2408 e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2410 type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2412 type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2417 coerce_delete_type (type)
2424 tree arg_types = TYPE_ARG_TYPES (type);
2426 if (TREE_CODE (type) == METHOD_TYPE)
2428 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2429 arg_types = TREE_CHAIN (arg_types);
2432 if (TREE_TYPE (type) != void_type_node)
2433 e1 = 1, error ("`operator delete' must return type `void'");
2435 if (arg_types == NULL_TREE
2436 || ! same_type_p (TREE_VALUE (arg_types), ptr_type_node))
2437 e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2441 && TREE_CHAIN (arg_types)
2442 && TREE_CHAIN (arg_types) != void_list_node)
2444 /* Again, technically this argument must be `size_t', but again
2445 we may not know what that is. */
2446 tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2447 if (! same_type_p (t2, sizetype))
2448 e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2449 else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2452 if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2453 error ("too many arguments in declaration of `operator delete'");
2455 error ("`...' invalid in specification of `operator delete'");
2460 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2461 build_tree_list (NULL_TREE, sizetype));
2464 if (arg_types == NULL_TREE)
2465 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2467 arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2473 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2474 arg_types ? TREE_CHAIN (arg_types): NULL_TREE);
2476 type = build_function_type (void_type_node, arg_types);
2481 extern tree abort_fndecl;
2484 mark_vtable_entries (decl)
2487 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2489 for (; entries; entries = TREE_CHAIN (entries))
2494 fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2495 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2497 if (TREE_CODE (fnaddr) == NOP_EXPR)
2501 fn = TREE_OPERAND (fnaddr, 0);
2502 TREE_ADDRESSABLE (fn) = 1;
2503 if (DECL_LANG_SPECIFIC (fn) && DECL_ABSTRACT_VIRTUAL_P (fn))
2505 TREE_OPERAND (fnaddr, 0) = fn = copy_node (fn);
2506 DECL_RTL (fn) = DECL_RTL (abort_fndecl);
2507 mark_used (abort_fndecl);
2509 if (TREE_CODE (fn) == THUNK_DECL && DECL_EXTERNAL (fn))
2511 DECL_EXTERNAL (fn) = 0;
2518 /* Set DECL up to have the closest approximation of "initialized common"
2519 linkage available. */
2522 comdat_linkage (decl)
2526 make_decl_one_only (decl);
2527 else if (TREE_CODE (decl) == FUNCTION_DECL || DECL_VIRTUAL_P (decl))
2528 /* We can just emit functions and vtables statically; it doesn't really
2529 matter if we have multiple copies. */
2530 TREE_PUBLIC (decl) = 0;
2533 /* Static data member template instantiations, however, cannot
2534 have multiple copies. */
2535 if (DECL_INITIAL (decl) == 0
2536 || DECL_INITIAL (decl) == error_mark_node)
2537 DECL_COMMON (decl) = 1;
2538 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2540 DECL_COMMON (decl) = 1;
2541 DECL_INITIAL (decl) = error_mark_node;
2545 /* We can't do anything useful; leave vars for explicit
2547 DECL_EXTERNAL (decl) = 1;
2548 DECL_NOT_REALLY_EXTERN (decl) = 0;
2552 if (DECL_LANG_SPECIFIC (decl))
2553 DECL_COMDAT (decl) = 1;
2556 /* For win32 we also want to put explicit instantiations in
2557 linkonce sections, so that they will be merged with implicit
2558 instantiations; otherwise we get duplicate symbol errors. */
2561 maybe_make_one_only (decl)
2564 /* We used to say that this was not necessary on targets that support weak
2565 symbols, because the implicit instantiations will defer to the explicit
2566 one. However, that's not actually the case in SVR4; a strong definition
2567 after a weak one is an error. Also, not making explicit
2568 instantiations one_only means that we can end up with two copies of
2569 some template instantiations. */
2570 if (! supports_one_only ())
2573 /* We can't set DECL_COMDAT on functions, or finish_file will think
2574 we can get away with not emitting them if they aren't used. We need
2575 to for variables so that cp_finish_decl will update their linkage,
2576 because their DECL_INITIAL may not have been set properly yet. */
2578 make_decl_one_only (decl);
2580 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2581 DECL_COMDAT (decl) = 1;
2584 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2585 based on TYPE and other static flags.
2587 Note that anything public is tagged TREE_PUBLIC, whether
2588 it's public in this file or in another one. */
2591 import_export_vtable (decl, type, final)
2595 if (DECL_INTERFACE_KNOWN (decl))
2598 if (TYPE_FOR_JAVA (type))
2600 TREE_PUBLIC (decl) = 1;
2601 DECL_EXTERNAL (decl) = 1;
2602 DECL_INTERFACE_KNOWN (decl) = 1;
2604 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2606 TREE_PUBLIC (decl) = 1;
2607 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2608 DECL_INTERFACE_KNOWN (decl) = 1;
2612 /* We can only wait to decide if we have real non-inline virtual
2613 functions in our class, or if we come from a template. */
2615 int found = CLASSTYPE_TEMPLATE_INSTANTIATION (type);
2617 if (! found && ! final)
2620 for (method = TYPE_METHODS (type); method != NULL_TREE;
2621 method = TREE_CHAIN (method))
2622 if (DECL_VINDEX (method) != NULL_TREE
2623 && ! DECL_THIS_INLINE (method)
2624 && ! DECL_ABSTRACT_VIRTUAL_P (method))
2631 if (final || ! found)
2633 comdat_linkage (decl);
2634 DECL_EXTERNAL (decl) = 0;
2638 TREE_PUBLIC (decl) = 1;
2639 DECL_EXTERNAL (decl) = 1;
2644 /* Determine whether or not we want to specifically import or export CTYPE,
2645 using various heuristics. */
2648 import_export_class (ctype)
2651 /* -1 for imported, 1 for exported. */
2652 int import_export = 0;
2654 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2657 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2658 we will have CLASSTYPE_INTERFACE_ONLY set but not
2659 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2660 heuristic because someone will supply a #pragma implementation
2661 elsewhere, and deducing it here would produce a conflict. */
2662 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2665 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2666 /* FIXME this should really use some sort of target-independent macro. */
2667 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2669 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2673 /* If we got -fno-implicit-templates, we import template classes that
2674 weren't explicitly instantiated. */
2675 if (import_export == 0
2676 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2677 && ! flag_implicit_templates)
2680 /* Base our import/export status on that of the first non-inline,
2681 non-abstract virtual function, if any. */
2682 if (import_export == 0
2683 && TYPE_VIRTUAL_P (ctype)
2684 && ! CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2687 for (method = TYPE_METHODS (ctype); method != NULL_TREE;
2688 method = TREE_CHAIN (method))
2690 if (DECL_VINDEX (method) != NULL_TREE
2691 && !DECL_THIS_INLINE (method)
2692 && !DECL_ABSTRACT_VIRTUAL_P (method))
2694 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2700 #ifdef MULTIPLE_SYMBOL_SPACES
2701 if (import_export == -1)
2707 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2708 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = (import_export > 0);
2709 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2713 /* We need to describe to the assembler the relationship between
2714 a vtable and the vtable of the parent class. */
2717 output_vtable_inherit (vars)
2723 op[0] = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2725 parent = binfo_for_vtable (vars);
2727 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2731 parent = TYPE_BINFO_VTABLE (BINFO_TYPE (parent));
2732 op[1] = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2735 my_friendly_abort (980826);
2737 output_asm_insn (".vtable_inherit %c0, %c1", op);
2741 finish_vtable_vardecl (t, data)
2743 void *data ATTRIBUTE_UNUSED;
2746 tree ctype = DECL_CONTEXT (vars);
2747 import_export_class (ctype);
2748 import_export_vtable (vars, ctype, 1);
2750 if (! DECL_EXTERNAL (vars)
2751 && (DECL_INTERFACE_KNOWN (vars)
2752 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars))
2753 || (hack_decl_function_context (vars) && TREE_USED (vars)))
2754 && ! TREE_ASM_WRITTEN (vars))
2757 mark_vtable_entries (vars);
2758 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2759 store_init_value (vars, DECL_INITIAL (vars));
2761 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2763 /* Mark the VAR_DECL node representing the vtable itself as a
2764 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2765 It is rather important that such things be ignored because
2766 any effort to actually generate DWARF for them will run
2767 into trouble when/if we encounter code like:
2770 struct S { virtual void member (); };
2772 because the artificial declaration of the vtable itself (as
2773 manufactured by the g++ front end) will say that the vtable
2774 is a static member of `S' but only *after* the debug output
2775 for the definition of `S' has already been output. This causes
2776 grief because the DWARF entry for the definition of the vtable
2777 will try to refer back to an earlier *declaration* of the
2778 vtable as a static member of `S' and there won't be one.
2779 We might be able to arrange to have the "vtable static member"
2780 attached to the member list for `S' before the debug info for
2781 `S' get written (which would solve the problem) but that would
2782 require more intrusive changes to the g++ front end. */
2784 DECL_IGNORED_P (vars) = 1;
2787 /* Always make vtables weak. */
2789 comdat_linkage (vars);
2791 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2794 output_vtable_inherit (vars);
2798 else if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars)))
2799 /* We don't know what to do with this one yet. */
2802 *t = TREE_CHAIN (vars);
2807 prune_vtable_vardecl (t, data)
2809 void *data ATTRIBUTE_UNUSED;
2811 *t = TREE_CHAIN (*t);
2816 finish_sigtable_vardecl (t, data)
2818 void *data ATTRIBUTE_UNUSED;
2820 /* We don't need to mark sigtable entries as addressable here as is done
2821 for vtables. Since sigtables, unlike vtables, are always written out,
2822 that was already done in build_signature_table_constructor. */
2824 rest_of_decl_compilation (*t, NULL_PTR, 1, 1);
2825 *t = TREE_CHAIN (*t);
2829 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2830 inline function or template instantiation at end-of-file. */
2833 import_export_decl (decl)
2836 if (DECL_INTERFACE_KNOWN (decl))
2839 if (DECL_TEMPLATE_INSTANTIATION (decl)
2840 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2842 DECL_NOT_REALLY_EXTERN (decl) = 1;
2843 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2844 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2845 && (flag_implicit_templates
2846 || (flag_implicit_inline_templates && DECL_THIS_INLINE (decl))))
2848 if (!TREE_PUBLIC (decl))
2849 /* Templates are allowed to have internal linkage. See
2853 comdat_linkage (decl);
2856 DECL_NOT_REALLY_EXTERN (decl) = 0;
2858 else if (DECL_VLIST_CTOR_WRAPPER_P (decl))
2861 tree ctype = DECL_CLASS_CONTEXT (decl);
2862 import_export_class (ctype);
2863 if (!DECL_THIS_INLINE (DECL_VLIST_CTOR_WRAPPED (decl)))
2867 else if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2869 implement = !CLASSTYPE_INTERFACE_ONLY (ctype)
2870 && flag_implement_inlines;
2871 DECL_NOT_REALLY_EXTERN (decl) = implement;
2872 DECL_EXTERNAL (decl) = !implement;
2876 DECL_NOT_REALLY_EXTERN (decl) = 1;
2877 DECL_EXTERNAL (decl) = 1;
2880 comdat_linkage (decl);
2882 else if (DECL_FUNCTION_MEMBER_P (decl))
2884 tree ctype = DECL_CLASS_CONTEXT (decl);
2885 import_export_class (ctype);
2886 if (CLASSTYPE_INTERFACE_KNOWN (ctype)
2887 && (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl)))
2889 DECL_NOT_REALLY_EXTERN (decl)
2890 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2891 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2892 && !DECL_VINDEX (decl)));
2894 /* Always make artificials weak. */
2895 if (DECL_ARTIFICIAL (decl) && flag_weak)
2896 comdat_linkage (decl);
2898 maybe_make_one_only (decl);
2901 comdat_linkage (decl);
2903 else if (DECL_TINFO_FN_P (decl))
2905 tree ctype = TREE_TYPE (DECL_NAME (decl));
2907 if (IS_AGGR_TYPE (ctype))
2908 import_export_class (ctype);
2910 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2911 && TYPE_VIRTUAL_P (ctype)
2912 /* If the type is a cv-qualified variant of a type, then we
2913 must emit the tinfo function in this translation unit
2914 since it will not be emitted when the vtable for the type
2915 is output (which is when the unqualified version is
2917 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2919 DECL_NOT_REALLY_EXTERN (decl)
2920 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2921 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2922 && !DECL_VINDEX (decl)));
2924 /* Always make artificials weak. */
2926 comdat_linkage (decl);
2928 else if (TYPE_BUILT_IN (ctype)
2929 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2930 DECL_NOT_REALLY_EXTERN (decl) = 0;
2932 comdat_linkage (decl);
2935 comdat_linkage (decl);
2937 DECL_INTERFACE_KNOWN (decl) = 1;
2941 build_cleanup (decl)
2945 tree type = TREE_TYPE (decl);
2947 if (TREE_CODE (type) == ARRAY_TYPE)
2951 mark_addressable (decl);
2952 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2954 temp = build_delete (TREE_TYPE (temp), temp,
2956 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2960 extern int parse_time, varconst_time;
2966 tree sname = get_id_2 ("__sn", base);
2967 /* For struct X foo __attribute__((weak)), there is a counter
2968 __snfoo. Since base is already an assembler name, sname should
2969 be globally unique */
2970 tree sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2973 push_obstacks_nochange ();
2974 end_temporary_allocation ();
2975 sentry = build_decl (VAR_DECL, sname, integer_type_node);
2976 TREE_PUBLIC (sentry) = 1;
2977 DECL_ARTIFICIAL (sentry) = 1;
2978 TREE_STATIC (sentry) = 1;
2979 TREE_USED (sentry) = 1;
2980 DECL_COMMON (sentry) = 1;
2981 pushdecl_top_level (sentry);
2982 cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0, 0);
2988 /* Start the process of running a particular set of global constructors
2989 or destructors. Subroutine of do_[cd]tors. */
2992 start_objects (method_type, initp)
2993 int method_type, initp;
2998 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
3000 if (initp != DEFAULT_INIT_PRIORITY)
3010 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
3013 sprintf (type, "%c", method_type);
3015 fnname = get_file_function_name_long (type);
3017 start_function (void_list_node,
3018 make_call_declarator (fnname, void_list_node, NULL_TREE,
3022 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
3023 /* It can be a static function as long as collect2 does not have
3024 to scan the object file to find its ctor/dtor routine. */
3025 TREE_PUBLIC (current_function_decl) = 0;
3028 store_parm_decls ();
3032 expand_start_bindings (0);
3034 /* We cannot allow these functions to be elided, even if they do not
3035 have external linkage. And, there's no point in deferring
3036 copmilation of thes functions; they're all going to have to be
3038 current_function_cannot_inline
3039 = "static constructors and destructors cannot be inlined";
3042 /* Finish the process of running a particular set of global constructors
3043 or destructors. Subroutine of do_[cd]tors. */
3046 finish_objects (method_type, initp)
3047 int method_type, initp;
3049 char *fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
3052 expand_end_bindings (getdecls (), 1, 0);
3055 finish_function (lineno, 0, 0);
3057 if (initp == DEFAULT_INIT_PRIORITY)
3059 if (method_type == 'I')
3060 assemble_constructor (fnname);
3062 assemble_destructor (fnname);
3065 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
3066 /* If we're using init priority we can't use assemble_*tor, but on ELF
3067 targets we can stick the references into named sections for GNU ld
3072 sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
3073 /* invert the numbering so the linker puts us in the proper
3074 order; constructors are run from right to left, and the
3075 linker sorts in increasing order. */
3076 MAX_INIT_PRIORITY - initp);
3077 named_section (NULL_TREE, buf, 0);
3078 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
3079 POINTER_SIZE / BITS_PER_UNIT, 1);
3084 /* The names of the parameters to the function created to handle
3085 initializations and destructions for objects with static storage
3087 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3088 #define PRIORITY_IDENTIFIER "__priority"
3090 /* The name of the function we create to handle initializations and
3091 destructions for objects with static storage duration. */
3092 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3094 /* The declaration for the __INITIALIZE_P argument. */
3095 static tree initialize_p_decl;
3097 /* The declaration for the __PRIORITY argument. */
3098 static tree priority_decl;
3100 /* The declaration for the static storage duration function. */
3101 static tree ssdf_decl;
3103 /* All the static storage duration functions created in this
3104 translation unit. */
3105 static varray_type ssdf_decls;
3106 static size_t ssdf_decls_used;
3108 /* A map from priority levels to information about that priority
3109 level. There may be many such levels, so efficient lookup is
3111 static splay_tree priority_info_map;
3113 /* Begins the generation of the function that will handle all
3114 initialization and destruction of objects with static storage
3115 duration. The function generated takes two parameters of type
3116 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3117 non-zero, it performs initializations. Otherwise, it performs
3118 destructions. It only performs those initializations or
3119 destructions with the indicated __PRIORITY. The generated function
3122 It is assumed that this function will only be called once per
3123 translation unit. */
3126 start_static_storage_duration_function ()
3128 static unsigned ssdf_number;
3132 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
3134 /* Create the identifier for this function. It will be of the form
3135 SSDF_IDENTIFIER_<number>. */
3136 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
3137 if (ssdf_number == 0)
3139 /* Overflow occurred. That means there are at least 4 billion
3140 initialization functions. */
3141 sorry ("too many initialization functions required");
3142 my_friendly_abort (19990430);
3145 /* Create the parameters. */
3146 parm_types = void_list_node;
3147 parm_types = perm_tree_cons (NULL_TREE, integer_type_node, parm_types);
3148 parm_types = perm_tree_cons (NULL_TREE, integer_type_node, parm_types);
3149 type = build_function_type (void_type_node, parm_types);
3151 /* Create the FUNCTION_DECL itself. */
3152 ssdf_decl = build_lang_decl (FUNCTION_DECL,
3153 get_identifier (id),
3155 TREE_PUBLIC (ssdf_decl) = 0;
3156 DECL_ARTIFICIAL (ssdf_decl) = 1;
3158 /* Put this function in the list of functions to be called from the
3159 static constructors and destructors. */
3162 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
3164 /* Take this opportunity to initialize the map from priority
3165 numbers to information about that priority level. */
3166 priority_info_map = splay_tree_new (splay_tree_compare_ints,
3167 /*delete_key_fn=*/0,
3168 /*delete_value_fn=*/
3169 (splay_tree_delete_value_fn) &free);
3171 /* We always need to generate functions for the
3172 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3173 priorities later, we'll be sure to find the
3174 DEFAULT_INIT_PRIORITY. */
3175 get_priority_info (DEFAULT_INIT_PRIORITY);
3178 if (ssdf_decls_used == ssdf_decls->num_elements)
3179 VARRAY_GROW (ssdf_decls, 2 * ssdf_decls_used);
3180 VARRAY_TREE (ssdf_decls, ssdf_decls_used) = ssdf_decl;
3183 /* Create the argument list. */
3184 initialize_p_decl = build_decl (PARM_DECL,
3185 get_identifier (INITIALIZE_P_IDENTIFIER),
3187 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
3188 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
3189 TREE_USED (initialize_p_decl) = 1;
3190 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
3192 DECL_CONTEXT (priority_decl) = ssdf_decl;
3193 DECL_ARG_TYPE (priority_decl) = integer_type_node;
3194 TREE_USED (priority_decl) = 1;
3196 TREE_CHAIN (initialize_p_decl) = priority_decl;
3197 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
3199 /* Start the function itself. This is equivalent to declarating the
3202 static void __ssdf (int __initialize_p, init __priority_p);
3204 It is static because we only need to call this function from the
3205 various constructor and destructor functions for this module. */
3206 start_function (/*specs=*/NULL_TREE,
3208 /*attrs=*/NULL_TREE,
3209 /*pre_parsed_p=*/1);
3211 /* Set up the scope of the outermost block in the function. */
3212 store_parm_decls ();
3216 expand_start_bindings (0);
3218 /* This function must not be deferred because we are depending on
3219 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3220 current_function_cannot_inline
3221 = "static storage duration functions cannot be inlined";
3224 /* Generate the initialization code for the priority indicated in N. */
3227 generate_inits_for_priority (n, data)
3229 void *data ATTRIBUTE_UNUSED;
3231 int priority = (int) n->key;
3232 priority_info pi = (priority_info) n->value;
3234 /* For each priority N which has been used generate code which looks
3237 if (__priority == N) {
3244 We use the sequences we've accumulated to fill in the `...'s. */
3245 expand_start_cond (build_binary_op (EQ_EXPR,
3247 build_int_2 (priority, 0)),
3250 /* Do the initializations. */
3251 expand_start_cond (build_binary_op (NE_EXPR,
3255 if (pi->initialization_sequence)
3259 push_to_sequence (pi->initialization_sequence);
3260 insns = gen_sequence ();
3264 pi->initialization_sequence = NULL_RTX;
3265 pi->initializations_p = 1;
3268 /* Do the destructions. */
3269 expand_start_else ();
3270 if (pi->destruction_sequence)
3274 push_to_sequence (pi->destruction_sequence);
3275 insns = gen_sequence ();
3279 pi->destruction_sequence = NULL_RTX;
3280 pi->destructions_p = 1;
3283 /* Close out the conditionals. */
3287 /* Don't stop iterating. */
3291 /* Finish the generation of the function which performs initialization
3292 and destruction of objects with static storage duration. After
3293 this point, no more such objects can be created. */
3296 finish_static_storage_duration_function ()
3298 splay_tree_foreach (priority_info_map,
3299 generate_inits_for_priority,
3302 /* Close out the function. */
3303 expand_end_bindings (getdecls (), 1, 0);
3306 finish_function (lineno, 0, 0);
3309 /* Return the information about the indicated PRIORITY level. If no
3310 code to handle this level has yet been generated, generate the
3311 appropriate prologue. */
3313 static priority_info
3314 get_priority_info (priority)
3320 n = splay_tree_lookup (priority_info_map,
3321 (splay_tree_key) priority);
3324 /* Create a new priority information structure, and insert it
3326 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
3327 pi->initialization_sequence = NULL_RTX;
3328 pi->destruction_sequence = NULL_RTX;
3329 pi->initializations_p = 0;
3330 pi->destructions_p = 0;
3331 splay_tree_insert (priority_info_map,
3332 (splay_tree_key) priority,
3333 (splay_tree_value) pi);
3336 pi = (priority_info) n->value;
3341 /* Generate code to do the static initialization of DECL. The
3342 initialization is INIT. If DECL may be initialized more than once
3343 in different object files, SENTRY is the guard variable to
3344 check. PRIORITY is the priority for the initialization. */
3347 do_static_initialization (decl, init, sentry, priority)
3355 /* Get the priority information for this PRIORITY, */
3356 pi = get_priority_info (priority);
3357 if (!pi->initialization_sequence)
3360 push_to_sequence (pi->initialization_sequence);
3362 /* Tell the debugger that we are at the location of the static
3363 variable in question. */
3364 emit_note (input_filename, lineno);
3366 /* If there's a SENTRY, we only do the initialization if it is
3367 zero, i.e., if we are the first to initialize it. */
3369 expand_start_cond (build_binary_op (EQ_EXPR,
3370 build_unary_op (PREINCREMENT_EXPR,
3376 /* Prepare a binding level for temporaries created during the
3378 expand_start_target_temps ();
3380 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3381 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3382 expand_aggr_init (decl, init, 0);
3383 else if (TREE_CODE (init) == TREE_VEC)
3384 expand_expr (expand_vec_init (decl, TREE_VEC_ELT (init, 0),
3385 TREE_VEC_ELT (init, 1),
3386 TREE_VEC_ELT (init, 2), 0),
3387 const0_rtx, VOIDmode, EXPAND_NORMAL);
3389 expand_assignment (decl, init, 0, 0);
3391 /* The expression might have involved increments and decrements. */
3394 /* Cleanup any temporaries needed for the initial value. */
3395 expand_end_target_temps ();
3397 /* Cleanup any deferred pops from function calls. This would be done
3398 by expand_end_cond, but we also need it when !SENTRY, since we are
3399 constructing these sequences by parts. */
3400 do_pending_stack_adjust ();
3402 /* Close the conditional opened above. */
3406 /* Save the sequence for later use. */
3407 pi->initialization_sequence = get_insns ();
3411 /* Generate code to do the static destruction of DECL. If DECL may be
3412 initialized more than once in different object files, SENTRY is the
3413 guard variable to check. PRIORITY is the priority for the
3417 do_static_destruction (decl, sentry, priority)
3425 /* If we don't need a destructor, there's nothing to do. */
3426 if (!TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
3429 /* Get the priority information for this PRIORITY, */
3430 pi = get_priority_info (priority);
3431 if (!pi->destruction_sequence)
3434 push_to_sequence (pi->destruction_sequence);
3436 /* Start a new sequence to handle just this destruction. */
3439 /* Tell the debugger that we are at the location of the static
3440 variable in question. */
3441 emit_note (input_filename, lineno);
3443 /* If there's a SENTRY, we only do the destruction if it is one,
3444 i.e., if we are the last to destroy it. */
3446 expand_start_cond (build_binary_op (EQ_EXPR,
3447 build_unary_op (PREDECREMENT_EXPR,
3453 /* Actually to the destruction. */
3454 expand_expr_stmt (build_cleanup (decl));
3456 /* Cleanup any deferred pops from function calls. This would be done
3457 by expand_end_cond, but we also need it when !SENTRY, since we are
3458 constructing these sequences by parts. */
3459 do_pending_stack_adjust ();
3461 /* Close the conditional opened above. */
3465 /* Insert the NEW_INSNS before the current insns. (Destructions are
3466 run in reverse order of initializations.) */
3467 new_insns = gen_sequence ();
3469 if (pi->destruction_sequence)
3470 emit_insn_before (new_insns, pi->destruction_sequence);
3472 emit_insn (new_insns);
3474 /* Save the sequence for later use. */
3475 pi->destruction_sequence = get_insns ();
3479 /* Add code to the static storage duration function that will handle
3480 DECL (a static variable that needs initializing and/or destruction)
3481 with the indicated PRIORITY. If DECL needs initializing, INIT is
3485 do_static_initialization_and_destruction (decl, init)
3489 tree sentry = NULL_TREE;
3492 /* Deal gracefully with error. */
3493 if (decl == error_mark_node)
3496 /* The only things that can be initialized are variables. */
3497 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3499 /* If this object is not defined, we don't need to do anything
3501 if (DECL_EXTERNAL (decl))
3504 /* Also, if the initializer already contains errors, we can bail out
3506 if (init && TREE_CODE (init) == TREE_LIST
3507 && value_member (error_mark_node, init))
3510 /* Trick the compiler into thinking we are at the file and line
3511 where DECL was declared so that error-messages make sense, and so
3512 that the debugger will show somewhat sensible file and line
3514 input_filename = DECL_SOURCE_FILE (decl);
3515 lineno = DECL_SOURCE_LINE (decl);
3521 Access control for implicit calls to the constructors,
3522 the conversion functions, or the destructor called to
3523 create and destroy a static data member is performed as
3524 if these calls appeared in the scope of the member's
3527 we pretend we are in a static member function of the class of
3528 which the DECL is a member. */
3529 if (member_p (decl))
3531 DECL_CLASS_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3532 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3535 /* We need a sentry if this is an object with external linkage that
3536 might be initialized in more than one place. */
3537 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3538 || DECL_ONE_ONLY (decl)
3539 || DECL_WEAK (decl)))
3540 sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3542 /* Generate the code to actually do the intialization and
3544 priority = DECL_INIT_PRIORITY (decl);
3546 priority = DEFAULT_INIT_PRIORITY;
3547 do_static_initialization (decl, init, sentry, priority);
3548 do_static_destruction (decl, sentry, priority);
3550 /* Now that we're done with DECL we don't need to pretend to be a
3551 member of its class any longer. */
3552 DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
3553 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3556 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3557 (otherwise) that will initialize all gobal objects with static
3558 storage duration having the indicated PRIORITY. */
3561 generate_ctor_or_dtor_function (constructor_p, priority)
3569 /* We use `I' to indicate initialization and `D' to indicate
3576 /* Begin the function. */
3577 start_objects (function_key, priority);
3579 /* Call the static storage duration function with appropriate
3581 for (i = 0; i < ssdf_decls_used; ++i)
3583 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3585 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3587 expand_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3591 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3592 calls to any functions marked with attributes indicating that
3593 they should be called at initialization- or destruction-time. */
3594 if (priority == DEFAULT_INIT_PRIORITY)
3598 for (fns = constructor_p ? static_ctors : static_dtors;
3600 fns = TREE_CHAIN (fns))
3601 expand_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3604 /* Close out the function. */
3605 finish_objects (function_key, priority);
3608 /* Generate constructor and destructor functions for the priority
3612 generate_ctor_and_dtor_functions_for_priority (n, data)
3614 void *data ATTRIBUTE_UNUSED;
3616 int priority = (int) n->key;
3617 priority_info pi = (priority_info) n->value;
3619 /* Generate the functions themselves, but only if they are really
3621 if (pi->initializations_p
3622 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3623 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3625 if (pi->destructions_p
3626 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3627 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3630 /* Keep iterating. */
3634 /* Returns non-zero if T is a vlist ctor wrapper. */
3637 vlist_ctor_wrapper_p (t, data)
3639 void *data ATTRIBUTE_UNUSED;
3641 return (TREE_CODE (t) == FUNCTION_DECL) && DECL_VLIST_CTOR_WRAPPER_P (t);
3644 /* Emits a vlist ctor wrapper if necessary. */
3647 finish_vlist_ctor_wrapper (t, data)
3649 void *data ATTRIBUTE_UNUSED;
3651 import_export_decl (*t);
3652 if (!DECL_EXTERNAL (*t) && !TREE_USED (*t))
3655 synthesize_method (*t);
3661 /* This routine is called from the last rule in yyparse ().
3662 Its job is to create all the code needed to initialize and
3663 destroy the global aggregates. We do the destruction
3664 first, since that way we only need to reverse the decls once. */
3670 int start_time, this_time;
3677 /* Bad parse errors. Just forget about it. */
3678 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3681 start_time = get_run_time ();
3683 /* Otherwise, GDB can get confused, because in only knows
3684 about source for LINENO-1 lines. */
3687 interface_unknown = 1;
3690 /* We now have to write out all the stuff we put off writing out.
3693 o Template specializations that we have not yet instantiated,
3694 but which are needed.
3695 o Initialization and destruction for non-local objects with
3696 static storage duration. (Local objects with static storage
3697 duration are initialized when their scope is first entered,
3698 and are cleaned up via atexit.)
3699 o Virtual function tables.
3701 All of these may cause others to be needed. For example,
3702 instantiating one function may cause another to be needed, and
3703 generating the intiailzer for an object may cause templates to be
3704 instantiated, etc., etc. */
3706 this_time = get_run_time ();
3707 parse_time -= this_time - start_time;
3708 varconst_time += this_time - start_time;
3709 start_time = get_run_time ();
3710 permanent_allocation (1);
3714 /* Non-zero if we need a static storage duration function on
3715 this iteration through the loop. */
3716 int need_ssdf_p = 0;
3720 /* If there are templates that we've put off instantiating, do
3722 instantiate_pending_templates ();
3724 /* Write out signature-tables and virtual tables as required.
3725 Note that writing out the virtual table for a template class
3726 may cause the instantiation of members of that class. */
3727 if (flag_handle_signatures
3728 && walk_globals (sigtable_decl_p,
3729 finish_sigtable_vardecl,
3732 if (walk_globals (vtable_decl_p,
3733 finish_vtable_vardecl,
3737 if (walk_globals (vlist_ctor_wrapper_p,
3738 finish_vlist_ctor_wrapper,
3743 /* The list of objects with static storage duration is built up
3744 in reverse order, so we reverse it here. We also clear
3745 STATIC_AGGREGATES so that any new aggregates added during the
3746 initialization of these will be initialized in the correct
3747 order when we next come around the loop. */
3748 vars = nreverse (static_aggregates);
3749 static_aggregates = NULL_TREE;
3752 if (! TREE_ASM_WRITTEN (TREE_VALUE (vars)))
3753 rest_of_decl_compilation (TREE_VALUE (vars), 0, 1, 1);
3756 /* We need to start a new initialization function each
3757 time through the loop. That's because we need to
3758 know which vtables have been referenced, and
3759 TREE_SYMBOL_REFERENCED isn't computed until a
3760 function is finished, and written out. That's a
3761 deficiency in the back-end. When this is fixed,
3762 these initialization functions could all become
3763 inline, with resulting performance improvements. */
3764 start_static_storage_duration_function ();
3768 do_static_initialization_and_destruction (TREE_VALUE (vars),
3769 TREE_PURPOSE (vars));
3771 vars = TREE_CHAIN (vars);
3774 /* Finish up the static storage duration function for this
3777 finish_static_storage_duration_function ();
3779 /* Go through the various inline functions, and see if any need
3781 for (i = 0; i < saved_inlines_used; ++i)
3783 tree decl = VARRAY_TREE (saved_inlines, i);
3784 import_export_decl (decl);
3785 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3787 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3789 /* Even though we're already at the top-level, we push
3790 there again. That way, when we pop back a few lines
3791 hence, all of our state is restored. Otherwise,
3792 finish_function doesn't clean things up, and we end
3793 up with CURRENT_FUNCTION_DECL set. */
3794 push_to_top_level ();
3795 if (DECL_TINFO_FN_P (decl))
3796 synthesize_tinfo_fn (decl);
3798 synthesize_method (decl);
3799 pop_from_top_level ();
3804 /* Mark all functions that might deal with exception-handling as
3806 mark_all_runtime_matches ();
3808 /* We lie to the back-end, pretending that some functions are
3809 not defined when they really are. This keeps these functions
3810 from being put out unncessarily. But, we must stop lying
3811 when the functions are referenced, or if they are not comdat
3812 since they need to be put out now. */
3813 for (i = 0; i < saved_inlines_used; ++i)
3815 tree decl = VARRAY_TREE (saved_inlines, i);
3817 if (DECL_NOT_REALLY_EXTERN (decl)
3818 && DECL_INITIAL (decl)
3819 && (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
3820 || !DECL_COMDAT (decl)))
3821 DECL_EXTERNAL (decl) = 0;
3824 if (saved_inlines_used
3825 && wrapup_global_declarations (&VARRAY_TREE (saved_inlines, 0),
3826 saved_inlines_used))
3828 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3831 /* Static data members are just like namespace-scope globals. */
3832 for (i = 0; i < pending_statics_used; ++i)
3834 tree decl = VARRAY_TREE (pending_statics, i);
3835 if (TREE_ASM_WRITTEN (decl))
3837 import_export_decl (decl);
3838 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3839 DECL_EXTERNAL (decl) = 0;
3842 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3843 pending_statics_used))
3848 /* We give C linkage to static constructors and destructors. */
3849 push_lang_context (lang_name_c);
3851 /* Generate initialization and destruction functions for all
3852 priorities for which they are required. */
3853 if (priority_info_map)
3854 splay_tree_foreach (priority_info_map,
3855 generate_ctor_and_dtor_functions_for_priority,
3858 /* We're done with the splay-tree now. */
3859 if (priority_info_map)
3860 splay_tree_delete (priority_info_map);
3862 /* We're done with static constructors, so we can go back to "C++"
3864 pop_lang_context ();
3866 /* Now delete from the chain of variables all virtual function tables.
3867 We output them all ourselves, because each will be treated
3869 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3871 /* Now, issue warnings about static, but not defined, functions,
3873 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3877 this_time = get_run_time ();
3878 parse_time -= this_time - start_time;
3879 varconst_time += this_time - start_time;
3881 if (flag_detailed_statistics)
3883 dump_tree_statistics ();
3884 dump_time_statistics ();
3888 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3889 expr. Since it was parsed like a type, we need to wade through and fix
3890 that. Unfortunately, since operator() is left-associative, we can't use
3891 tail recursion. In the above example, TYPE is `A', and DECL is
3894 Maybe this shouldn't be recursive, but how often will it actually be
3898 reparse_absdcl_as_expr (type, decl)
3901 /* do build_functional_cast (type, NULL_TREE) at bottom */
3902 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3903 return build_functional_cast (type, NULL_TREE);
3906 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3908 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3910 if (TREE_CODE (decl) == CALL_EXPR
3911 && (! TREE_TYPE (decl)
3912 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3913 decl = require_complete_type (decl);
3918 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3919 out to be an expr. Since it was parsed like a type, we need to wade
3920 through and fix that. Since casts are right-associative, we are
3921 reversing the order, so we don't have to recurse.
3923 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3927 reparse_absdcl_as_casts (decl, expr)
3932 if (TREE_CODE (expr) == CONSTRUCTOR
3933 && TREE_TYPE (expr) == 0)
3935 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3936 decl = TREE_OPERAND (decl, 0);
3938 if (IS_SIGNATURE (type))
3940 error ("cast specifies signature type");
3941 return error_mark_node;
3944 expr = digest_init (type, expr, (tree *) 0);
3945 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
3947 int failure = complete_array_type (type, expr, 1);
3949 my_friendly_abort (78);
3955 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3956 decl = TREE_OPERAND (decl, 0);
3957 expr = build_c_cast (type, expr);
3960 if (warn_old_style_cast && ! in_system_header
3961 && current_lang_name != lang_name_c)
3962 warning ("use of old-style cast");
3967 /* Given plain tree nodes for an expression, build up the full semantics. */
3970 build_expr_from_tree (t)
3973 if (t == NULL_TREE || t == error_mark_node)
3976 switch (TREE_CODE (t))
3978 case IDENTIFIER_NODE:
3979 return do_identifier (t, 0, NULL_TREE);
3982 if (LOOKUP_EXPR_GLOBAL (t))
3983 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3985 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3987 case TEMPLATE_ID_EXPR:
3988 return (lookup_template_function
3989 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3990 build_expr_from_tree (TREE_OPERAND (t, 1))));
3993 return build_x_indirect_ref
3994 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3997 return build_functional_cast
3998 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
4000 case REINTERPRET_CAST_EXPR:
4001 return build_reinterpret_cast
4002 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
4004 case CONST_CAST_EXPR:
4005 return build_const_cast
4006 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
4008 case DYNAMIC_CAST_EXPR:
4009 return build_dynamic_cast
4010 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
4012 case STATIC_CAST_EXPR:
4013 return build_static_cast
4014 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
4016 case PREDECREMENT_EXPR:
4017 case PREINCREMENT_EXPR:
4018 case POSTDECREMENT_EXPR:
4019 case POSTINCREMENT_EXPR:
4023 case TRUTH_NOT_EXPR:
4025 case CONVERT_EXPR: /* Unary + */
4028 return build_x_unary_op (TREE_CODE (t),
4029 build_expr_from_tree (TREE_OPERAND (t, 0)));
4034 case TRUNC_DIV_EXPR:
4036 case FLOOR_DIV_EXPR:
4037 case ROUND_DIV_EXPR:
4038 case EXACT_DIV_EXPR:
4040 case BIT_ANDTC_EXPR:
4043 case TRUNC_MOD_EXPR:
4044 case FLOOR_MOD_EXPR:
4045 case TRUTH_ANDIF_EXPR:
4046 case TRUTH_ORIF_EXPR:
4047 case TRUTH_AND_EXPR:
4062 return build_x_binary_op
4064 build_expr_from_tree (TREE_OPERAND (t, 0)),
4065 build_expr_from_tree (TREE_OPERAND (t, 1)));
4068 return build_m_component_ref
4069 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4070 build_expr_from_tree (TREE_OPERAND (t, 1)));
4073 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
4076 if (TREE_OPERAND (t, 0) == NULL_TREE)
4078 return build_parse_node (ARRAY_REF, NULL_TREE,
4079 build_expr_from_tree (TREE_OPERAND (t, 1)));
4080 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
4081 build_expr_from_tree (TREE_OPERAND (t, 1)));
4086 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
4087 if (TREE_CODE_CLASS (TREE_CODE (r)) != 't')
4089 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
4093 return build_x_modify_expr
4094 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4095 TREE_CODE (TREE_OPERAND (t, 1)),
4096 build_expr_from_tree (TREE_OPERAND (t, 2)));
4099 return build_x_arrow
4100 (build_expr_from_tree (TREE_OPERAND (t, 0)));
4104 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4105 build_expr_from_tree (TREE_OPERAND (t, 1)),
4106 build_expr_from_tree (TREE_OPERAND (t, 2)),
4107 NEW_EXPR_USE_GLOBAL (t));
4110 return delete_sanity
4111 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4112 build_expr_from_tree (TREE_OPERAND (t, 1)),
4113 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
4116 if (TREE_OPERAND (t, 1) == NULL_TREE)
4117 return build_x_compound_expr
4118 (build_expr_from_tree (TREE_OPERAND (t, 0)));
4120 my_friendly_abort (42);
4122 case METHOD_CALL_EXPR:
4123 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4125 tree ref = TREE_OPERAND (t, 0);
4126 return build_scoped_method_call
4127 (build_expr_from_tree (TREE_OPERAND (t, 1)),
4128 build_expr_from_tree (TREE_OPERAND (ref, 0)),
4129 TREE_OPERAND (ref, 1),
4130 build_expr_from_tree (TREE_OPERAND (t, 2)));
4134 tree fn = TREE_OPERAND (t, 0);
4136 /* We can get a TEMPLATE_ID_EXPR here on code like:
4140 so we must resolve that. However, we can also get things
4141 like a BIT_NOT_EXPR here, when referring to a destructor,
4142 and things like that are not correctly resolved by
4143 build_expr_from_tree. So, just use build_expr_from_tree
4144 when we really need it. */
4145 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4146 fn = lookup_template_function
4147 (TREE_OPERAND (fn, 0),
4148 build_expr_from_tree (TREE_OPERAND (fn, 1)));
4150 return build_method_call
4151 (build_expr_from_tree (TREE_OPERAND (t, 1)),
4153 build_expr_from_tree (TREE_OPERAND (t, 2)),
4154 NULL_TREE, LOOKUP_NORMAL);
4158 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
4160 tree ref = TREE_OPERAND (t, 0);
4161 return build_member_call
4162 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
4163 TREE_OPERAND (ref, 1),
4164 build_expr_from_tree (TREE_OPERAND (t, 1)));
4168 tree name = TREE_OPERAND (t, 0);
4170 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
4171 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
4172 && !LOOKUP_EXPR_GLOBAL (name)
4173 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
4174 && (!current_class_type
4175 || !lookup_member (current_class_type, id, 0, 0)))
4177 /* Do Koenig lookup if there are no class members. */
4178 name = do_identifier (id, 0, args);
4180 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
4181 || ! really_overloaded_fn (name))
4182 name = build_expr_from_tree (name);
4183 return build_x_function_call (name, args, current_class_ref);
4187 return build_x_conditional_expr
4188 (build_expr_from_tree (TREE_OPERAND (t, 0)),
4189 build_expr_from_tree (TREE_OPERAND (t, 1)),
4190 build_expr_from_tree (TREE_OPERAND (t, 2)));
4194 tree purpose, value, chain;
4196 if (t == void_list_node)
4199 purpose = TREE_PURPOSE (t);
4201 purpose = build_expr_from_tree (purpose);
4202 value = TREE_VALUE (t);
4204 value = build_expr_from_tree (value);
4205 chain = TREE_CHAIN (t);
4206 if (chain && chain != void_type_node)
4207 chain = build_expr_from_tree (chain);
4208 return expr_tree_cons (purpose, value, chain);
4213 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
4214 tree field = TREE_OPERAND (t, 1);
4216 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4217 and `x.A::b'. We must distinguish between those cases
4219 if (TREE_CODE (field) == SCOPE_REF)
4220 return build_object_ref (object,
4221 TREE_OPERAND (field, 0),
4222 TREE_OPERAND (field, 1));
4224 return build_x_component_ref (object, field,
4229 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
4235 /* digest_init will do the wrong thing if we let it. */
4236 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
4239 r = build_nt (CONSTRUCTOR, NULL_TREE,
4240 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
4241 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
4244 return digest_init (TREE_TYPE (t), r, 0);
4249 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 0))) == 't')
4250 return get_typeid (TREE_OPERAND (t, 0));
4251 return build_x_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4254 return convert_from_reference (t);
4261 /* This is something of the form `int (*a)++' that has turned out to be an
4262 expr. It was only converted into parse nodes, so we need to go through
4263 and build up the semantics. Most of the work is done by
4264 build_expr_from_tree, above.
4266 In the above example, TYPE is `int' and DECL is `*a'. */
4269 reparse_decl_as_expr (type, decl)
4272 decl = build_expr_from_tree (decl);
4274 return build_functional_cast (type, build_expr_list (NULL_TREE, decl));
4279 /* This is something of the form `int (*a)' that has turned out to be a
4280 decl. It was only converted into parse nodes, so we need to do the
4281 checking that make_{pointer,reference}_declarator do. */
4284 finish_decl_parsing (decl)
4287 extern int current_class_depth;
4289 switch (TREE_CODE (decl))
4291 case IDENTIFIER_NODE:
4294 return make_pointer_declarator
4295 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4297 return make_reference_declarator
4298 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4300 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4303 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4304 TREE_COMPLEXITY (decl) = current_class_depth;
4307 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4310 /* For attribute handling. */
4311 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4314 my_friendly_abort (5);
4320 check_cp_case_value (value)
4323 if (value == NULL_TREE)
4326 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4327 STRIP_TYPE_NOPS (value);
4329 if (TREE_READONLY_DECL_P (value))
4331 value = decl_constant_value (value);
4332 STRIP_TYPE_NOPS (value);
4334 value = fold (value);
4336 if (TREE_CODE (value) != INTEGER_CST
4337 && value != error_mark_node)
4339 cp_error ("case label `%E' does not reduce to an integer constant",
4341 value = error_mark_node;
4344 /* Promote char or short to int. */
4345 value = default_conversion (value);
4347 constant_expression_warning (value);
4352 /* Return 1 if root encloses child. */
4355 is_namespace_ancestor (root, child)
4360 if (root == global_namespace)
4362 if (child == global_namespace)
4364 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4368 /* Return the namespace that is the common ancestor
4369 of two given namespaces. */
4372 namespace_ancestor (ns1, ns2)
4375 if (is_namespace_ancestor (ns1, ns2))
4377 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4380 /* Insert used into the using list of user. Set indirect_flag if this
4381 directive is not directly from the source. Also find the common
4382 ancestor and let our users know about the new namespace */
4384 add_using_namespace (user, used, indirect)
4390 /* Using oneself is a no-op. */
4393 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4394 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4395 /* Check if we already have this. */
4396 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4400 /* Promote to direct usage. */
4401 TREE_INDIRECT_USING (t) = 0;
4405 /* Add used to the user's using list. */
4406 DECL_NAMESPACE_USING (user)
4407 = perm_tree_cons (used, namespace_ancestor (user, used),
4408 DECL_NAMESPACE_USING (user));
4410 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4412 /* Add user to the used's users list. */
4413 DECL_NAMESPACE_USERS (used)
4414 = perm_tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4416 /* Recursively add all namespaces used. */
4417 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4418 /* indirect usage */
4419 add_using_namespace (user, TREE_PURPOSE (t), 1);
4421 /* Tell everyone using us about the new used namespaces. */
4422 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4423 add_using_namespace (TREE_PURPOSE (t), used, 1);
4426 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4427 duplicates. The first list becomes the tail of the result.
4429 The algorithm is O(n^2). We could get this down to O(n log n) by
4430 doing a sort on the addresses of the functions, if that becomes
4434 merge_functions (s1, s2)
4438 for (; s2; s2 = OVL_NEXT (s2))
4440 tree fn = OVL_CURRENT (s2);
4441 if (! ovl_member (fn, s1))
4442 s1 = build_overload (fn, s1);
4447 /* This should return an error not all definitions define functions.
4448 It is not an error if we find two functions with exactly the
4449 same signature, only if these are selected in overload resolution.
4450 old is the current set of bindings, new the freshly-found binding.
4451 XXX Do we want to give *all* candidates in case of ambiguity?
4452 XXX In what way should I treat extern declarations?
4453 XXX I don't want to repeat the entire duplicate_decls here */
4456 ambiguous_decl (name, old, new, flags)
4463 my_friendly_assert (old != NULL_TREE, 393);
4464 /* Copy the value. */
4465 val = BINDING_VALUE (new);
4467 switch (TREE_CODE (val))
4470 /* If we expect types or namespaces, and not templates,
4471 or this is not a template class. */
4472 if (LOOKUP_QUALIFIERS_ONLY (flags)
4473 && !DECL_CLASS_TEMPLATE_P (val))
4477 if (LOOKUP_NAMESPACES_ONLY (flags))
4480 case NAMESPACE_DECL:
4481 if (LOOKUP_TYPES_ONLY (flags))
4485 if (LOOKUP_QUALIFIERS_ONLY (flags))
4489 if (!BINDING_VALUE (old))
4490 BINDING_VALUE (old) = val;
4491 else if (val && val != BINDING_VALUE (old))
4493 if (is_overloaded_fn (BINDING_VALUE (old))
4494 && is_overloaded_fn (val))
4496 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4501 /* Some declarations are functions, some are not. */
4502 if (flags & LOOKUP_COMPLAIN)
4504 /* If we've already given this error for this lookup,
4505 BINDING_VALUE (old) is error_mark_node, so let's not
4506 repeat ourselves. */
4507 if (BINDING_VALUE (old) != error_mark_node)
4509 cp_error ("use of `%D' is ambiguous", name);
4510 cp_error_at (" first declared as `%#D' here",
4511 BINDING_VALUE (old));
4513 cp_error_at (" also declared as `%#D' here", val);
4515 return error_mark_node;
4518 /* ... and copy the type. */
4519 type = BINDING_TYPE (new);
4520 if (LOOKUP_NAMESPACES_ONLY (flags))
4522 if (!BINDING_TYPE (old))
4523 BINDING_TYPE (old) = type;
4524 else if (type && BINDING_TYPE (old) != type)
4526 if (flags & LOOKUP_COMPLAIN)
4528 cp_error ("`%D' denotes an ambiguous type",name);
4529 cp_error_at (" first type here", BINDING_TYPE (old));
4530 cp_error_at (" other type here", type);
4536 /* Add the bindings of name in used namespaces to val.
4537 The using list is defined by usings, and the lookup goes to scope.
4538 Returns zero on errors. */
4541 lookup_using_namespace (name, val, usings, scope, flags)
4542 tree name, val, usings, scope;
4547 /* Iterate over all used namespaces in current, searching for using
4548 directives of scope. */
4549 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4550 if (TREE_VALUE (iter) == scope)
4552 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4553 /* Resolve ambiguities. */
4554 val = ambiguous_decl (name, val, val1, flags);
4556 return val != error_mark_node;
4560 Excepts the name to lookup and its qualifying scope.
4561 Returns the name/type pair found into the CPLUS_BINDING result,
4565 qualified_lookup_using_namespace (name, scope, result, flags)
4571 /* Maintain a list of namespaces visited... */
4572 tree seen = NULL_TREE;
4573 /* ... and a list of namespace yet to see. */
4574 tree todo = NULL_TREE;
4576 while (scope && (result != error_mark_node))
4578 seen = temp_tree_cons (scope, NULL_TREE, seen);
4579 result = ambiguous_decl (name, result,
4580 binding_for_name (name, scope), flags);
4581 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4582 /* Consider using directives. */
4583 for (usings = DECL_NAMESPACE_USING (scope); usings;
4584 usings = TREE_CHAIN (usings))
4585 /* If this was a real directive, and we have not seen it. */
4586 if (!TREE_INDIRECT_USING (usings)
4587 && !purpose_member (TREE_PURPOSE (usings), seen))
4588 todo = temp_tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4591 scope = TREE_PURPOSE (todo);
4592 todo = TREE_CHAIN (todo);
4595 scope = NULL_TREE; /* If there never was a todo list. */
4597 return result != error_mark_node;
4600 /* [namespace.memdef]/2 */
4602 /* Set the context of a declaration to scope. Complain if we are not
4606 set_decl_namespace (decl, scope, friendp)
4612 if (scope == std_node)
4613 scope = global_namespace;
4614 /* Get rid of namespace aliases. */
4615 scope = ORIGINAL_NAMESPACE (scope);
4617 /* It is ok for friends to be qualified in parallel space. */
4618 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4619 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4621 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4622 if (scope != current_namespace)
4624 /* See whether this has been declared in the namespace. */
4625 old = namespace_binding (DECL_NAME (decl), scope);
4627 /* No old declaration at all. */
4629 if (!is_overloaded_fn (decl))
4630 /* Don't compare non-function decls with decls_match here,
4631 since it can't check for the correct constness at this
4632 point. pushdecl will find those errors later. */
4634 /* Since decl is a function, old should contain a function decl. */
4635 if (!is_overloaded_fn (old))
4637 if (processing_template_decl || processing_specialization)
4638 /* We have not yet called push_template_decl to turn the
4639 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4640 won't match. But, we'll check later, when we construct the
4643 for (; old; old = OVL_NEXT (old))
4644 if (decls_match (decl, OVL_CURRENT (old)))
4650 cp_error ("`%D' should have been declared inside `%D'",
4654 /* Compute the namespace where a declaration is defined. */
4657 decl_namespace (decl)
4660 while (DECL_CONTEXT (decl))
4662 decl = DECL_CONTEXT (decl);
4663 if (TREE_CODE (decl) == NAMESPACE_DECL)
4665 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
4666 decl = TYPE_STUB_DECL (decl);
4667 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 390);
4670 return global_namespace;
4673 /* Return the namespace where the current declaration is declared. */
4676 current_decl_namespace ()
4679 /* If we have been pushed into a different namespace, use it. */
4680 if (decl_namespace_list)
4681 return TREE_PURPOSE (decl_namespace_list);
4683 if (current_class_type)
4684 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4685 else if (current_function_decl)
4686 result = decl_namespace (current_function_decl);
4688 result = current_namespace;
4692 /* Temporarily set the namespace for the current declaration. */
4695 push_decl_namespace (decl)
4698 if (TREE_CODE (decl) != NAMESPACE_DECL)
4699 decl = decl_namespace (decl);
4700 decl_namespace_list = tree_cons (decl, NULL_TREE, decl_namespace_list);
4704 pop_decl_namespace ()
4706 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4709 /* Enter a class or namespace scope. */
4715 if (TREE_CODE (t) == NAMESPACE_DECL)
4716 push_decl_namespace (t);
4721 /* Leave scope pushed by push_scope. */
4727 if (TREE_CODE (t) == NAMESPACE_DECL)
4728 pop_decl_namespace ();
4733 /* [basic.lookup.koenig] */
4734 /* A non-zero return value in the functions below indicates an error.
4735 All nodes allocated in the procedure are on the scratch obstack. */
4745 static int arg_assoc PROTO((struct arg_lookup*, tree));
4746 static int arg_assoc_args PROTO((struct arg_lookup*, tree));
4747 static int arg_assoc_type PROTO((struct arg_lookup*, tree));
4748 static int add_function PROTO((struct arg_lookup *, tree));
4749 static int arg_assoc_namespace PROTO((struct arg_lookup *, tree));
4750 static int arg_assoc_class PROTO((struct arg_lookup *, tree));
4752 /* Add a function to the lookup structure.
4753 Returns 1 on error. */
4756 add_function (k, fn)
4757 struct arg_lookup *k;
4760 if (ovl_member (fn, k->functions))
4762 /* We must find only functions, or exactly one non-function. */
4763 if (k->functions && is_overloaded_fn (k->functions)
4764 && is_overloaded_fn (fn))
4765 k->functions = build_overload (fn, k->functions);
4769 tree f1 = OVL_CURRENT (k->functions);
4771 if (is_overloaded_fn (f1))
4773 fn = f1; f1 = f2; f2 = fn;
4775 cp_error_at ("`%D' is not a function,", f1);
4776 cp_error_at (" conflict with `%D'", f2);
4777 cp_error (" in call to `%D'", k->name);
4785 /* Add functions of a namespace to the lookup structure.
4786 Returns 1 on error. */
4789 arg_assoc_namespace (k, scope)
4790 struct arg_lookup *k;
4795 if (purpose_member (scope, k->namespaces))
4797 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4799 value = namespace_binding (k->name, scope);
4803 for (; value; value = OVL_NEXT (value))
4804 if (add_function (k, OVL_CURRENT (value)))
4810 /* Adds everything associated with class to the lookup structure.
4811 Returns 1 on error. */
4814 arg_assoc_class (k, type)
4815 struct arg_lookup* k;
4818 tree list, friends, context;
4821 if (purpose_member (type, k->classes))
4823 k->classes = tree_cons (type, NULL_TREE, k->classes);
4825 context = decl_namespace (TYPE_MAIN_DECL (type));
4826 if (arg_assoc_namespace (k, context))
4829 /* Process baseclasses. */
4830 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4831 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4834 /* Process friends. */
4835 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4836 list = TREE_CHAIN (list))
4837 if (k->name == TREE_PURPOSE (list))
4838 for (friends = TREE_VALUE (list); friends;
4839 friends = TREE_CHAIN (friends))
4840 /* Only interested in global functions with potentially hidden
4841 (i.e. unqualified) declarations. */
4842 if (TREE_PURPOSE (list) == error_mark_node && TREE_VALUE (list)
4843 && decl_namespace (TREE_VALUE (list)) == context)
4844 if (add_function (k, TREE_VALUE (list)))
4847 /* Process template arguments. */
4848 if (CLASSTYPE_TEMPLATE_INFO (type))
4850 list = innermost_args (CLASSTYPE_TI_ARGS (type));
4851 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4852 arg_assoc (k, TREE_VEC_ELT (list, i));
4858 /* Adds everything associated with a given type.
4859 Returns 1 on error. */
4862 arg_assoc_type (k, type)
4863 struct arg_lookup *k;
4866 switch (TREE_CODE (type))
4876 if (TYPE_PTRMEMFUNC_P (type))
4877 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4878 return arg_assoc_class (k, type);
4880 case REFERENCE_TYPE:
4882 return arg_assoc_type (k, TREE_TYPE (type));
4885 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4887 /* Pointer to member: associate class type and value type. */
4888 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4890 return arg_assoc_type (k, TREE_TYPE (type));
4892 /* The basetype is referenced in the first arg type, so just
4895 /* Associate the parameter types. */
4896 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4898 /* Associate the return type. */
4899 return arg_assoc_type (k, TREE_TYPE (type));
4900 case TEMPLATE_TYPE_PARM:
4901 case TEMPLATE_TEMPLATE_PARM:
4904 if (type == unknown_type_node)
4906 /* else fall through */
4908 my_friendly_abort (390);
4913 /* Adds everything associated with arguments. Returns 1 on error. */
4916 arg_assoc_args (k, args)
4917 struct arg_lookup* k;
4920 for (; args; args = TREE_CHAIN (args))
4921 if (arg_assoc (k, TREE_VALUE (args)))
4926 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4930 struct arg_lookup* k;
4933 if (n == error_mark_node)
4936 if (TREE_CODE_CLASS (TREE_CODE (n)) == 't')
4937 return arg_assoc_type (k, n);
4939 if (! type_unknown_p (n))
4940 return arg_assoc_type (k, TREE_TYPE (n));
4942 if (TREE_CODE (n) == ADDR_EXPR)
4943 n = TREE_OPERAND (n, 0);
4944 if (TREE_CODE (n) == COMPONENT_REF)
4945 n = TREE_OPERAND (n, 1);
4946 if (TREE_CODE (n) == OFFSET_REF)
4947 n = TREE_OPERAND (n, 1);
4948 while (TREE_CODE (n) == TREE_LIST)
4951 if (TREE_CODE (n) == FUNCTION_DECL)
4952 return arg_assoc_type (k, TREE_TYPE (n));
4953 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4955 /* [basic.lookup.koenig]
4957 If T is a template-id, its associated namespaces and classes
4958 are the namespace in which the template is defined; for
4959 member templates, the member template's class; the namespaces
4960 and classes associated with the types of the template
4961 arguments provided for template type parameters (excluding
4962 template template parameters); the namespaces in which any
4963 template template arguments are defined; and the classes in
4964 which any member templates used as template template
4965 arguments are defined. [Note: non-type template arguments do
4966 not contribute to the set of associated namespaces. ] */
4967 tree template = TREE_OPERAND (n, 0);
4968 tree args = TREE_OPERAND (n, 1);
4972 /* First, the template. There may actually be more than one if
4973 this is an overloaded function template. But, in that case,
4974 we only need the first; all the functions will be in the same
4976 template = OVL_CURRENT (template);
4978 ctx = CP_DECL_CONTEXT (template);
4980 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4982 if (arg_assoc_namespace (k, ctx) == 1)
4985 /* It must be a member template. */
4986 else if (arg_assoc_class (k, ctx) == 1)
4989 /* Now the arguments. */
4990 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4992 tree t = TREE_VALUE (arg);
4994 if (TREE_CODE (t) == TEMPLATE_DECL)
4996 ctx = CP_DECL_CONTEXT (t);
4997 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4999 if (arg_assoc_namespace (k, ctx) == 1)
5002 else if (arg_assoc_class (k, ctx) == 1)
5005 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
5006 && arg_assoc_type (k, t) == 1)
5012 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
5014 for (; n; n = OVL_CHAIN (n))
5015 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
5022 /* Performs Koenig lookup depending on arguments, where fns
5023 are the functions found in normal lookup. */
5026 lookup_arg_dependent (name, fns, args)
5031 struct arg_lookup k;
5035 k.classes = NULL_TREE;
5037 /* Note that we've already looked at the current namespace during normal
5038 unqualified lookup, unless we found a decl in function scope. */
5039 if (fns && ! TREE_PERMANENT (OVL_CURRENT (fns)))
5040 k.namespaces = NULL_TREE;
5042 k.namespaces = scratch_tree_cons (current_decl_namespace (),
5043 NULL_TREE, NULL_TREE);
5045 push_scratch_obstack ();
5046 arg_assoc_args (&k, args);
5051 /* Process a namespace-alias declaration. */
5054 do_namespace_alias (alias, namespace)
5055 tree alias, namespace;
5057 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5059 /* The parser did not find it, so it's not there. */
5060 cp_error ("unknown namespace `%D'", namespace);
5064 namespace = ORIGINAL_NAMESPACE (namespace);
5066 /* Build the alias. */
5067 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
5068 DECL_NAMESPACE_ALIAS (alias) = namespace;
5072 /* Check a non-member using-declaration. Return the name and scope
5073 being used, and the USING_DECL, or NULL_TREE on failure. */
5076 validate_nonmember_using_decl (decl, scope, name)
5081 if (TREE_CODE (decl) == SCOPE_REF
5082 && TREE_OPERAND (decl, 0) == std_node)
5084 if (namespace_bindings_p ()
5085 && current_namespace == global_namespace)
5086 /* There's no need for a using declaration at all, here,
5087 since `std' is the same as `::'. We can't just pass this
5088 on because we'll complain later about declaring something
5089 in the same scope as a using declaration with the same
5090 name. We return NULL_TREE which indicates to the caller
5091 that there's no need to do any further processing. */
5094 *scope = global_namespace;
5095 *name = TREE_OPERAND (decl, 1);
5097 else if (TREE_CODE (decl) == SCOPE_REF)
5099 *scope = TREE_OPERAND (decl, 0);
5100 *name = TREE_OPERAND (decl, 1);
5102 /* [namespace.udecl]
5104 A using-declaration for a class member shall be a
5105 member-declaration. */
5106 if (TREE_CODE (*scope) != NAMESPACE_DECL)
5108 if (TYPE_P (*scope))
5109 cp_error ("`%T' is not a namespace", *scope);
5111 cp_error ("`%D' is not a namespace", *scope);
5115 else if (TREE_CODE (decl) == IDENTIFIER_NODE
5116 || TREE_CODE (decl) == TYPE_DECL
5117 || TREE_CODE (decl) == TEMPLATE_DECL)
5119 *scope = global_namespace;
5123 my_friendly_abort (382);
5124 if (TREE_CODE_CLASS (TREE_CODE (*name)) == 'd')
5125 *name = DECL_NAME (*name);
5126 /* Make a USING_DECL. */
5127 return push_using_decl (*scope, *name);
5130 /* Process local and global using-declarations. */
5133 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
5135 tree oldval, oldtype;
5136 tree *newval, *newtype;
5139 struct tree_binding _decls;
5141 *newval = *newtype = NULL_TREE;
5142 decls = binding_init (&_decls);
5143 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
5147 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
5149 cp_error ("`%D' not declared", name);
5153 /* Check for using functions. */
5154 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
5158 if (oldval && !is_overloaded_fn (oldval))
5160 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
5165 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
5167 tree new_fn = OVL_CURRENT (tmp);
5169 /* [namespace.udecl]
5171 If a function declaration in namespace scope or block
5172 scope has the same name and the same parameter types as a
5173 function introduced by a using declaration the program is
5175 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
5177 tree old_fn = OVL_CURRENT (tmp1);
5179 if (!OVL_USED (tmp1)
5180 && compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
5181 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
5183 /* There was already a non-using declaration in
5184 this scope with the same parameter types. */
5185 cp_error ("`%D' is already declared in this scope",
5189 else if (duplicate_decls (new_fn, old_fn))
5190 /* We're re-using something we already used
5191 before. We don't need to add it again. */
5195 /* If we broke out of the loop, there's no reason to add
5196 this function to the using declarations for this
5201 *newval = build_overload (OVL_CURRENT (tmp), *newval);
5202 if (TREE_CODE (*newval) != OVERLOAD)
5203 *newval = ovl_cons (*newval, NULL_TREE);
5204 OVL_USED (*newval) = 1;
5209 *newval = BINDING_VALUE (decls);
5211 duplicate_decls (*newval, oldval);
5214 *newtype = BINDING_TYPE (decls);
5215 if (oldtype && *newtype && oldtype != *newtype)
5217 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5223 /* Process a using-declaration not appearing in class or local scope. */
5226 do_toplevel_using_decl (decl)
5229 tree scope, name, binding;
5230 tree oldval, oldtype, newval, newtype;
5232 decl = validate_nonmember_using_decl (decl, &scope, &name);
5233 if (decl == NULL_TREE)
5236 binding = binding_for_name (name, current_namespace);
5238 oldval = BINDING_VALUE (binding);
5239 oldtype = BINDING_TYPE (binding);
5241 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5243 /* Copy declarations found. */
5245 BINDING_VALUE (binding) = newval;
5247 BINDING_TYPE (binding) = newtype;
5251 /* Process a using-declaration at function scope. */
5254 do_local_using_decl (decl)
5258 tree oldval, oldtype, newval, newtype;
5260 decl = validate_nonmember_using_decl (decl, &scope, &name);
5261 if (decl == NULL_TREE)
5264 oldval = lookup_name_current_level (name);
5265 oldtype = lookup_type_current_level (name);
5267 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5271 if (is_overloaded_fn (newval))
5275 /* We only need to push declarations for those functions
5276 that were not already bound in the current level.
5277 The old value might be NULL_TREE, it might be a single
5278 function, or an OVERLOAD. */
5279 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5280 term = OVL_FUNCTION (oldval);
5283 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5285 push_overloaded_decl (OVL_CURRENT (fn),
5286 PUSH_LOCAL | PUSH_USING);
5289 push_local_binding (name, newval, PUSH_USING);
5292 set_identifier_type_value (name, newtype);
5296 do_class_using_decl (decl)
5301 if (TREE_CODE (decl) != SCOPE_REF
5302 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (decl, 0))) != 't')
5304 cp_error ("using-declaration for non-member at class scope");
5307 name = TREE_OPERAND (decl, 1);
5308 if (TREE_CODE (name) == BIT_NOT_EXPR)
5310 cp_error ("using-declaration for destructor");
5313 if (TREE_CODE (name) == TYPE_DECL)
5314 name = DECL_NAME (name);
5316 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5318 value = build_lang_field_decl (USING_DECL, name, void_type_node);
5319 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5323 /* Process a using-directive. */
5326 do_using_directive (namespace)
5329 if (namespace == std_node)
5331 /* using namespace A::B::C; */
5332 if (TREE_CODE (namespace) == SCOPE_REF)
5333 namespace = TREE_OPERAND (namespace, 1);
5334 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5336 /* Lookup in lexer did not find a namespace. */
5337 cp_error ("namespace `%T' undeclared", namespace);
5340 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5342 cp_error ("`%T' is not a namespace", namespace);
5345 namespace = ORIGINAL_NAMESPACE (namespace);
5346 if (!toplevel_bindings_p ())
5347 push_using_directive (namespace);
5350 add_using_namespace (current_namespace, namespace, 0);
5354 check_default_args (x)
5357 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5358 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5359 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5361 if (TREE_PURPOSE (arg))
5365 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5376 TREE_USED (decl) = 1;
5377 if (processing_template_decl)
5379 assemble_external (decl);
5381 /* Is it a synthesized method that needs to be synthesized? */
5382 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CLASS_CONTEXT (decl)
5383 && DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
5384 /* Kludge: don't synthesize for default args. */
5385 && current_function_decl)
5386 synthesize_method (decl);
5388 /* If this is a function or variable that is an instance of some
5389 template, we now know that we will need to actually do the
5390 instantiation. A TEMPLATE_DECL may also have DECL_TEMPLATE_INFO,
5391 if it's a partial instantiation, but there's no need to
5392 instantiate such a thing. We check that DECL is not an explicit
5393 instantiation because that is not checked in instantiate_decl. */
5394 if (TREE_CODE (decl) != TEMPLATE_DECL
5395 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5396 && !DECL_EXPLICIT_INSTANTIATION (decl))
5397 instantiate_decl (decl);
5400 /* Helper function for named_class_head_sans_basetype nonterminal. We
5401 have just seen something of the form `AGGR SCOPE::ID'. Return a
5402 TYPE_DECL for the type declared by ID in SCOPE. */
5405 handle_class_head (aggr, scope, id)
5406 tree aggr, scope, id;
5410 if (TREE_CODE (id) == TYPE_DECL)
5412 else if (DECL_CLASS_TEMPLATE_P (id))
5413 decl = DECL_TEMPLATE_RESULT (id);
5417 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5419 cp_error ("no file-scope type named `%D'", id);
5421 decl = TYPE_MAIN_DECL (xref_tag (aggr, make_anon_name (), 1));
5424 /* This syntax is only allowed when we're defining a type, so we
5426 push_scope (CP_DECL_CONTEXT (decl));
5428 /* If we see something like:
5430 template <typename T> struct S::I ....
5432 we must create a TEMPLATE_DECL for the nested type. */
5433 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5434 decl = push_template_decl (decl);