1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* Process declarations and symbol lookup for C front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
43 #include "dwarf2out.h"
48 extern cpp_reader
*parse_in
;
50 /* This structure contains information about the initializations
51 and/or destructions required for a particular priority level. */
52 typedef struct priority_info_s
{
53 /* Non-zero if there have been any initializations at this priority
54 throughout the translation unit. */
55 int initializations_p
;
56 /* Non-zero if there have been any destructions at this priority
57 throughout the translation unit. */
61 static void mark_vtable_entries
PARAMS ((tree
));
62 static void grok_function_init
PARAMS ((tree
, tree
));
63 static int finish_vtable_vardecl
PARAMS ((tree
*, void *));
64 static int prune_vtable_vardecl
PARAMS ((tree
*, void *));
65 static int is_namespace_ancestor
PARAMS ((tree
, tree
));
66 static void add_using_namespace
PARAMS ((tree
, tree
, int));
67 static tree ambiguous_decl
PARAMS ((tree
, tree
, tree
,int));
68 static tree build_anon_union_vars
PARAMS ((tree
, tree
*, int, int));
69 static int acceptable_java_type
PARAMS ((tree
));
70 static void output_vtable_inherit
PARAMS ((tree
));
71 static tree start_objects
PARAMS ((int, int));
72 static void finish_objects
PARAMS ((int, int, tree
));
73 static tree merge_functions
PARAMS ((tree
, tree
));
74 static tree decl_namespace
PARAMS ((tree
));
75 static tree validate_nonmember_using_decl
PARAMS ((tree
, tree
*, tree
*));
76 static void do_nonmember_using_decl
PARAMS ((tree
, tree
, tree
, tree
,
78 static tree start_static_storage_duration_function
PARAMS ((void));
79 static void finish_static_storage_duration_function
PARAMS ((tree
));
80 static priority_info get_priority_info
PARAMS ((int));
81 static void do_static_initialization
PARAMS ((tree
, tree
));
82 static void do_static_destruction
PARAMS ((tree
));
83 static tree start_static_initialization_or_destruction
PARAMS ((tree
, int));
84 static void finish_static_initialization_or_destruction
PARAMS ((tree
));
85 static void generate_ctor_or_dtor_function
PARAMS ((int, int));
86 static int generate_ctor_and_dtor_functions_for_priority
87 PARAMS ((splay_tree_node
, void *));
88 static tree prune_vars_needing_no_initialization
PARAMS ((tree
));
89 static void write_out_vars
PARAMS ((tree
));
90 static void import_export_class
PARAMS ((tree
));
91 static tree key_method
PARAMS ((tree
));
92 static int compare_options
PARAMS ((const PTR
, const PTR
));
93 static tree get_guard_bits
PARAMS ((tree
));
95 extern int current_class_depth
;
97 /* A list of virtual function tables we must make sure to write out. */
100 /* A list of static class variables. This is needed, because a
101 static class variable can be declared inside the class without
102 an initializer, and then initialized, staticly, outside the class. */
103 static varray_type pending_statics
;
104 #define pending_statics_used \
105 (pending_statics ? pending_statics->elements_used : 0)
107 /* A list of functions which were declared inline, but which we
108 may need to emit outline anyway. */
109 static varray_type deferred_fns
;
110 #define deferred_fns_used \
111 (deferred_fns ? deferred_fns->elements_used : 0)
113 /* Same, but not reset. Local temp variables and global temp variables
114 can have the same name. */
115 static int global_temp_name_counter
;
117 /* Flag used when debugging spew.c */
119 extern int spew_debug
;
121 /* Nonzero if we're done parsing and into end-of-file activities. */
125 /* Functions called along with real static constructors and destructors. */
130 /* The :: namespace. */
132 tree global_namespace
;
134 /* The stack for namespaces of current declarations. */
136 static tree decl_namespace_list
;
139 /* C (and C++) language-specific option variables. */
141 /* Nonzero means don't recognize the keyword `asm'. */
145 /* Nonzero means don't recognize any extension keywords. */
147 int flag_no_gnu_keywords
;
149 /* Nonzero means do some things the same way PCC does. Only provided so
150 the compiler will link. */
152 int flag_traditional
;
154 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
156 int flag_signed_bitfields
= 1;
158 /* Nonzero means enable obscure standard features and disable GNU
159 extensions that might cause standard-compliant code to be
164 /* Nonzero means do emit exported implementations of functions even if
165 they can be inlined. */
167 int flag_implement_inlines
= 1;
169 /* Nonzero means do emit exported implementations of templates, instead of
170 multiple static copies in each file that needs a definition. */
172 int flag_external_templates
;
174 /* Nonzero means that the decision to emit or not emit the implementation of a
175 template depends on where the template is instantiated, rather than where
178 int flag_alt_external_templates
;
180 /* Nonzero means that implicit instantiations will be emitted if needed. */
182 int flag_implicit_templates
= 1;
184 /* Nonzero means that implicit instantiations of inline templates will be
185 emitted if needed, even if instantiations of non-inline templates
188 int flag_implicit_inline_templates
= 1;
190 /* Nonzero means warn about implicit declarations. */
192 int warn_implicit
= 1;
194 /* Nonzero means warn about usage of long long when `-pedantic'. */
196 int warn_long_long
= 1;
198 /* Nonzero means warn when all ctors or dtors are private, and the class
201 int warn_ctor_dtor_privacy
= 1;
203 /* True if we want to implement vtables using "thunks".
204 The default is off. */
206 int flag_vtable_thunks
= 1;
208 /* Nonzero means generate separate instantiation control files and juggle
209 them at link time. */
211 int flag_use_repository
;
213 /* Nonzero if we want to issue diagnostics that the standard says are not
216 int flag_optional_diags
= 1;
218 /* Nonzero means give string constants the type `const char *', as mandated
221 int flag_const_strings
= 1;
223 /* Nonzero means warn about deprecated conversion from string constant to
226 int warn_write_strings
;
228 /* Nonzero means warn about pointer casts that can drop a type qualifier
229 from the pointer target type. */
233 /* Nonzero means warn about sizeof(function) or addition/subtraction
234 of function pointers. */
236 int warn_pointer_arith
= 1;
238 /* Nonzero means warn for any function def without prototype decl. */
240 int warn_missing_prototypes
;
242 /* Nonzero means warn about multiple (redundant) decls for the same single
243 variable or function. */
245 int warn_redundant_decls
;
247 /* Warn if initializer is not completely bracketed. */
249 int warn_missing_braces
;
251 /* Warn about comparison of signed and unsigned values. */
253 int warn_sign_compare
;
255 /* Warn about testing equality of floating point numbers. */
257 int warn_float_equal
= 0;
259 /* Warn about functions which might be candidates for format attributes. */
261 int warn_missing_format_attribute
;
263 /* Warn about a subscript that has type char. */
265 int warn_char_subscripts
;
267 /* Warn if a type conversion is done that might have confusing results. */
271 /* Warn if adding () is suggested. */
273 int warn_parentheses
;
275 /* Non-zero means warn in function declared in derived class has the
276 same name as a virtual in the base class, but fails to match the
277 type signature of any virtual function in the base class. */
279 int warn_overloaded_virtual
;
281 /* Non-zero means warn when declaring a class that has a non virtual
282 destructor, when it really ought to have a virtual one. */
286 /* Non-zero means warn when a function is declared extern and later inline. */
288 int warn_extern_inline
;
290 /* Non-zero means warn when the compiler will reorder code. */
294 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
298 /* Non-zero means warn when we convert a pointer to member function
299 into a pointer to (void or function). */
301 int warn_pmf2ptr
= 1;
303 /* Nonzero means warn about violation of some Effective C++ style rules. */
307 /* Nonzero means warn where overload resolution chooses a promotion from
308 unsigned to signed over a conversion to an unsigned of the same size. */
312 /* Nonzero means warn when an old-style cast is used. */
314 int warn_old_style_cast
;
316 /* Warn about #pragma directives that are not recognised. */
318 int warn_unknown_pragmas
; /* Tri state variable. */
320 /* Nonzero means warn about use of multicharacter literals. */
322 int warn_multichar
= 1;
324 /* Nonzero means warn when non-templatized friend functions are
325 declared within a template */
327 int warn_nontemplate_friend
= 1;
329 /* Nonzero means complain about deprecated features. */
331 int warn_deprecated
= 1;
333 /* Nonzero means `$' can be in an identifier. */
335 #ifndef DOLLARS_IN_IDENTIFIERS
336 #define DOLLARS_IN_IDENTIFIERS 1
338 int dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
;
340 /* Nonzero means that labels can be used as first-class objects */
344 /* Nonzero means allow Microsoft extensions without a pedwarn. */
346 int flag_ms_extensions
;
348 /* C++ specific flags. */
350 /* Nonzero means we should attempt to elide constructors when possible. */
352 int flag_elide_constructors
= 1;
354 /* Nonzero means that member functions defined in class scope are
355 inline by default. */
357 int flag_default_inline
= 1;
359 /* Controls whether compiler generates 'type descriptor' that give
360 run-time type information. */
364 /* Nonzero if we wish to output cross-referencing information
365 for the GNU class browser. */
367 extern int flag_gnu_xref
;
369 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
372 int flag_huge_objects
;
374 /* Nonzero if we want to conserve space in the .o files. We do this
375 by putting uninitialized data and runtime initialized data into
376 .common instead of .data at the expense of not flagging multiple
379 int flag_conserve_space
;
381 /* Nonzero if we want to obey access control semantics. */
383 int flag_access_control
= 1;
385 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
387 int flag_operator_names
= 1;
389 /* Nonzero if we want to check the return value of new and avoid calling
390 constructors if it is a null pointer. */
394 /* Nonnull if we want to dump class heirarchies. */
396 const char *flag_dump_class_layout
;
398 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
399 initialization variables.
400 0: Old rules, set by -fno-for-scope.
401 2: New ISO rules, set by -ffor-scope.
402 1: Try to implement new ISO rules, but with backup compatibility
403 (and warnings). This is the default, for now. */
405 int flag_new_for_scope
= 1;
407 /* Nonzero if we want to emit defined symbols with common-like linkage as
408 weak symbols where possible, in order to conform to C++ semantics.
409 Otherwise, emit them as local symbols. */
413 /* Nonzero to use __cxa_atexit, rather than atexit, to register
414 destructors for local statics and global objects. */
416 int flag_use_cxa_atexit
;
418 /* Nonzero to not ignore namespace std. */
420 int flag_honor_std
= ENABLE_STD_NAMESPACE
;
422 /* Nonzero if we should expand functions calls inline at the tree
423 level, rather than at the RTL level. */
425 int flag_inline_trees
= 0;
427 /* Maximum template instantiation depth. This limit is rather
428 arbitrary, but it exists to limit the time it takes to notice
429 infinite template instantiations. */
431 int max_tinst_depth
= 50;
433 /* The name-mangling scheme to use. Must be 1 or greater to support
434 template functions with identical types, but different template
436 int name_mangling_version
= 2;
438 /* Nonzero if squashed mangling is to be performed.
439 This uses the B and K codes to reference previously seen class types
440 and class qualifiers. */
442 int flag_do_squangling
;
444 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
448 /* Nonzero means make the default pedwarns warnings instead of errors.
449 The value of this flag is ignored if -pedantic is specified. */
453 /* Nonzero means to implement standard semantics for exception
454 specifications, calling unexpected if an exception is thrown that
455 doesn't match the specification. Zero means to treat them as
456 assertions and optimize accordingly, but not check them. */
458 int flag_enforce_eh_specs
= 1;
460 /* The variant of the C language being processed. */
462 c_language_kind c_language
= clk_cplusplus
;
464 /* Table of language-dependent -f options.
465 STRING is the option name. VARIABLE is the address of the variable.
466 ON_VALUE is the value to store in VARIABLE
467 if `-fSTRING' is seen as an option.
468 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
470 static struct { const char *string
; int *variable
; int on_value
;}
474 {"signed-char", &flag_signed_char
, 1},
475 {"unsigned-char", &flag_signed_char
, 0},
476 {"signed-bitfields", &flag_signed_bitfields
, 1},
477 {"unsigned-bitfields", &flag_signed_bitfields
, 0},
478 {"short-enums", &flag_short_enums
, 1},
479 {"short-double", &flag_short_double
, 1},
480 {"short-wchar", &flag_short_wchar
, 1},
481 {"asm", &flag_no_asm
, 0},
482 {"builtin", &flag_no_builtin
, 0},
484 /* C++-only options. */
485 {"access-control", &flag_access_control
, 1},
486 {"check-new", &flag_check_new
, 1},
487 {"conserve-space", &flag_conserve_space
, 1},
488 {"const-strings", &flag_const_strings
, 1},
489 {"default-inline", &flag_default_inline
, 1},
490 {"dollars-in-identifiers", &dollars_in_ident
, 1},
491 {"elide-constructors", &flag_elide_constructors
, 1},
492 {"enforce-eh-specs", &flag_enforce_eh_specs
, 1},
493 {"external-templates", &flag_external_templates
, 1},
494 {"for-scope", &flag_new_for_scope
, 2},
495 {"gnu-keywords", &flag_no_gnu_keywords
, 0},
496 {"handle-exceptions", &flag_exceptions
, 1},
497 {"honor-std", &flag_honor_std
, 1},
498 {"huge-objects", &flag_huge_objects
, 1},
499 {"implement-inlines", &flag_implement_inlines
, 1},
500 {"implicit-inline-templates", &flag_implicit_inline_templates
, 1},
501 {"implicit-templates", &flag_implicit_templates
, 1},
502 {"labels-ok", &flag_labels_ok
, 1},
503 {"ms-extensions", &flag_ms_extensions
, 1},
504 {"nonansi-builtins", &flag_no_nonansi_builtin
, 0},
505 {"operator-names", &flag_operator_names
, 1},
506 {"optional-diags", &flag_optional_diags
, 1},
507 {"permissive", &flag_permissive
, 1},
508 {"repo", &flag_use_repository
, 1},
509 {"rtti", &flag_rtti
, 1},
510 {"squangle", &flag_do_squangling
, 1},
511 {"stats", &flag_detailed_statistics
, 1},
512 {"use-cxa-atexit", &flag_use_cxa_atexit
, 1},
513 {"vtable-gc", &flag_vtable_gc
, 1},
514 {"vtable-thunks", &flag_vtable_thunks
, 1},
515 {"weak", &flag_weak
, 1},
516 {"xref", &flag_gnu_xref
, 1}
519 /* The list of `-f' options that we no longer support. The `-f'
520 prefix is not given in this table. The `-fno-' variants are not
521 listed here. This table must be kept in alphabetical order. */
522 static const char * const unsupported_options
[] = {
532 /* Compare two option strings, pointed two by P1 and P2, for use with
536 compare_options (p1
, p2
)
540 return strcmp (*((const char *const *) p1
), *((const char *const *) p2
));
543 /* Decode the string P as a language-specific option.
544 Return the number of strings consumed for a valid option.
545 Otherwise return 0. Should not complain if it does not
546 recognise the option. */
549 cxx_decode_option (argc
, argv
)
553 int strings_processed
;
554 const char *p
= argv
[0];
556 strings_processed
= cpp_handle_option (parse_in
, argc
, argv
);
558 if (!strcmp (p
, "-ftraditional") || !strcmp (p
, "-traditional"))
560 else if (p
[0] == '-' && p
[1] == 'f')
562 /* Some kind of -f option.
563 P's value is the option sans `-f'.
564 Search for it in the table of options. */
565 const char *option_value
= NULL
;
566 const char *positive_option
;
570 /* Try special -f options. */
572 /* See if this is one of the options no longer supported. We
573 used to support these options, so we continue to accept them,
575 if (strncmp (p
, "no-", strlen ("no-")) == 0)
576 positive_option
= p
+ strlen ("no-");
580 /* If the option is present, issue a warning. Indicate to our
581 caller that the option was processed successfully. */
582 if (bsearch (&positive_option
,
584 (sizeof (unsupported_options
)
585 / sizeof (unsupported_options
[0])),
586 sizeof (unsupported_options
[0]),
589 warning ("-f%s is no longer supported", p
);
593 if (!strcmp (p
, "handle-exceptions")
594 || !strcmp (p
, "no-handle-exceptions"))
595 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
596 else if (! strcmp (p
, "alt-external-templates"))
598 flag_external_templates
= 1;
599 flag_alt_external_templates
= 1;
600 cp_deprecated ("-falt-external-templates");
602 else if (! strcmp (p
, "no-alt-external-templates"))
603 flag_alt_external_templates
= 0;
604 else if (!strcmp (p
, "repo"))
606 flag_use_repository
= 1;
607 flag_implicit_templates
= 0;
609 else if (!strcmp (p
, "external-templates"))
611 flag_external_templates
= 1;
612 cp_deprecated ("-fexternal-templates");
614 else if ((option_value
615 = skip_leading_substring (p
, "template-depth-")))
617 = read_integral_parameter (option_value
, p
- 2, max_tinst_depth
);
618 else if ((option_value
619 = skip_leading_substring (p
, "name-mangling-version-")))
620 name_mangling_version
621 = read_integral_parameter (option_value
, p
- 2, name_mangling_version
);
622 else if ((option_value
623 = skip_leading_substring (p
, "dump-translation-unit=")))
626 error ("no file specified with -fdump-translation-unit");
628 flag_dump_translation_unit
= option_value
;
630 else if ((option_value
631 = skip_leading_substring (p
, "dump-class-layout=")))
634 error ("no file specified with -fdump-class-layout");
636 flag_dump_class_layout
= option_value
;
638 else if (!strcmp (p
, "dump-class-layout"))
639 flag_dump_class_layout
= ""; /* empty string for stderr */
645 !found
&& j
< (sizeof (lang_f_options
)
646 / sizeof (lang_f_options
[0]));
649 if (!strcmp (p
, lang_f_options
[j
].string
))
651 *lang_f_options
[j
].variable
= lang_f_options
[j
].on_value
;
652 /* A goto here would be cleaner,
653 but breaks the vax pcc. */
656 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
657 && ! strcmp (p
+3, lang_f_options
[j
].string
))
659 *lang_f_options
[j
].variable
= ! lang_f_options
[j
].on_value
;
667 else if (p
[0] == '-' && p
[1] == 'W')
671 /* The -W options control the warning behavior of the compiler. */
674 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-')
677 if (!strcmp (p
, "implicit"))
678 warn_implicit
= setting
;
679 else if (!strcmp (p
, "long-long"))
680 warn_long_long
= setting
;
681 else if (!strcmp (p
, "return-type"))
682 warn_return_type
= setting
;
683 else if (!strcmp (p
, "ctor-dtor-privacy"))
684 warn_ctor_dtor_privacy
= setting
;
685 else if (!strcmp (p
, "write-strings"))
686 warn_write_strings
= setting
;
687 else if (!strcmp (p
, "cast-qual"))
688 warn_cast_qual
= setting
;
689 else if (!strcmp (p
, "char-subscripts"))
690 warn_char_subscripts
= setting
;
691 else if (!strcmp (p
, "pointer-arith"))
692 warn_pointer_arith
= setting
;
693 else if (!strcmp (p
, "missing-prototypes"))
694 warn_missing_prototypes
= setting
;
695 else if (!strcmp (p
, "strict-prototypes"))
698 warning ("-Wno-strict-prototypes is not supported in C++");
700 else if (!strcmp (p
, "redundant-decls"))
701 warn_redundant_decls
= setting
;
702 else if (!strcmp (p
, "missing-braces"))
703 warn_missing_braces
= setting
;
704 else if (!strcmp (p
, "sign-compare"))
705 warn_sign_compare
= setting
;
706 else if (!strcmp (p
, "float-equal"))
707 warn_float_equal
= setting
;
708 else if (!strcmp (p
, "format"))
709 set_Wformat (setting
);
710 else if (!strcmp (p
, "format=2"))
712 else if (!strcmp (p
, "format-y2k"))
713 warn_format_y2k
= setting
;
714 else if (!strcmp (p
, "format-extra-args"))
715 warn_format_extra_args
= setting
;
716 else if (!strcmp (p
, "format-nonliteral"))
717 warn_format_nonliteral
= setting
;
718 else if (!strcmp (p
, "format-security"))
719 warn_format_security
= setting
;
720 else if (!strcmp (p
, "missing-format-attribute"))
721 warn_missing_format_attribute
= setting
;
722 else if (!strcmp (p
, "conversion"))
723 warn_conversion
= setting
;
724 else if (!strcmp (p
, "parentheses"))
725 warn_parentheses
= setting
;
726 else if (!strcmp (p
, "non-virtual-dtor"))
727 warn_nonvdtor
= setting
;
728 else if (!strcmp (p
, "extern-inline"))
729 warn_extern_inline
= setting
;
730 else if (!strcmp (p
, "reorder"))
731 warn_reorder
= setting
;
732 else if (!strcmp (p
, "synth"))
733 warn_synth
= setting
;
734 else if (!strcmp (p
, "pmf-conversions"))
735 warn_pmf2ptr
= setting
;
736 else if (!strcmp (p
, "effc++"))
738 else if (!strcmp (p
, "sign-promo"))
739 warn_sign_promo
= setting
;
740 else if (!strcmp (p
, "old-style-cast"))
741 warn_old_style_cast
= setting
;
742 else if (!strcmp (p
, "overloaded-virtual"))
743 warn_overloaded_virtual
= setting
;
744 else if (!strcmp (p
, "multichar"))
745 warn_multichar
= setting
;
746 else if (!strcmp (p
, "unknown-pragmas"))
747 /* Set to greater than 1, so that even unknown pragmas in
748 system headers will be warned about. */
749 warn_unknown_pragmas
= setting
* 2;
750 else if (!strcmp (p
, "non-template-friend"))
751 warn_nontemplate_friend
= setting
;
752 else if (!strcmp (p
, "deprecated"))
753 warn_deprecated
= setting
;
754 else if (!strcmp (p
, "comment"))
755 ; /* cpp handles this one. */
756 else if (!strcmp (p
, "comments"))
757 ; /* cpp handles this one. */
758 else if (!strcmp (p
, "trigraphs"))
759 ; /* cpp handles this one. */
760 else if (!strcmp (p
, "import"))
761 ; /* cpp handles this one. */
762 else if (!strcmp (p
, "all"))
764 warn_return_type
= setting
;
765 set_Wunused (setting
);
766 warn_implicit
= setting
;
767 warn_switch
= setting
;
768 set_Wformat (setting
);
769 warn_parentheses
= setting
;
770 warn_missing_braces
= setting
;
771 warn_sign_compare
= setting
;
772 warn_multichar
= setting
;
773 /* We save the value of warn_uninitialized, since if they put
774 -Wuninitialized on the command line, we need to generate a
775 warning about not using it without also specifying -O. */
776 if (warn_uninitialized
!= 1)
777 warn_uninitialized
= (setting
? 2 : 0);
778 /* Only warn about unknown pragmas that are not in system
780 warn_unknown_pragmas
= 1;
782 /* C++-specific warnings. */
783 warn_ctor_dtor_privacy
= setting
;
784 warn_nonvdtor
= setting
;
785 warn_reorder
= setting
;
786 warn_nontemplate_friend
= setting
;
788 else return strings_processed
;
790 else if (!strcmp (p
, "-ansi"))
791 flag_no_nonansi_builtin
= 1, flag_ansi
= 1,
792 flag_noniso_default_format_attributes
= 0, flag_no_gnu_keywords
= 1;
794 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
795 it's probably safe to assume no sane person would ever want to use this
796 under normal circumstances. */
797 else if (!strcmp (p
, "-spew-debug"))
801 return strings_processed
;
806 /* Incorporate `const' and `volatile' qualifiers for member functions.
807 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
808 QUALS is a list of qualifiers. Returns any explicit
809 top-level qualifiers of the method's this pointer, anything other than
810 TYPE_UNQUALIFIED will be an extension. */
813 grok_method_quals (ctype
, function
, quals
)
814 tree ctype
, function
, quals
;
816 tree fntype
= TREE_TYPE (function
);
817 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
818 int type_quals
= TYPE_UNQUALIFIED
;
819 int dup_quals
= TYPE_UNQUALIFIED
;
820 int this_quals
= TYPE_UNQUALIFIED
;
824 int tq
= cp_type_qual_from_rid (TREE_VALUE (quals
));
826 if ((type_quals
| this_quals
) & tq
)
828 else if (tq
& TYPE_QUAL_RESTRICT
)
832 quals
= TREE_CHAIN (quals
);
836 if (dup_quals
!= TYPE_UNQUALIFIED
)
837 cp_error ("duplicate type qualifiers in %s declaration",
838 TREE_CODE (function
) == FUNCTION_DECL
839 ? "member function" : "type");
841 ctype
= cp_build_qualified_type (ctype
, type_quals
);
842 fntype
= build_cplus_method_type (ctype
, TREE_TYPE (fntype
),
843 (TREE_CODE (fntype
) == METHOD_TYPE
844 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
845 : TYPE_ARG_TYPES (fntype
)));
847 fntype
= build_exception_variant (fntype
, raises
);
849 TREE_TYPE (function
) = fntype
;
853 /* Warn when -fexternal-templates is used and #pragma
854 interface/implementation is not used all the times it should be,
858 warn_if_unknown_interface (decl
)
861 static int already_warned
= 0;
862 if (already_warned
++)
865 if (flag_alt_external_templates
)
867 tree til
= tinst_for_decl ();
869 const char *sf
= input_filename
;
873 lineno
= TINST_LINE (til
);
874 input_filename
= TINST_FILE (til
);
876 cp_warning ("template `%#D' instantiated in file without #pragma interface",
882 cp_warning_at ("template `%#D' defined in file without #pragma interface",
886 /* A subroutine of the parser, to handle a component list. */
889 grok_x_components (specs
)
894 specs
= strip_attrs (specs
);
896 check_tag_decl (specs
);
897 t
= groktypename (build_tree_list (specs
, NULL_TREE
));
899 /* The only case where we need to do anything additional here is an
900 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
901 if (t
== NULL_TREE
|| !ANON_AGGR_TYPE_P (t
))
904 fixup_anonymous_aggr (t
);
905 finish_member_declaration (build_decl (FIELD_DECL
, NULL_TREE
, t
));
908 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
912 build_artificial_parm (name
, type
)
918 parm
= build_decl (PARM_DECL
, name
, type
);
919 DECL_ARTIFICIAL (parm
) = 1;
920 DECL_ARG_TYPE (parm
) = type
;
924 /* Constructors for types with virtual baseclasses need an "in-charge" flag
925 saying whether this constructor is responsible for initialization of
926 virtual baseclasses or not. All destructors also need this "in-charge"
927 flag, which additionally determines whether or not the destructor should
928 free the memory for the object.
930 This function adds the "in-charge" flag to member function FN if
931 appropriate. It is called from grokclassfn and tsubst.
932 FN must be either a constructor or destructor. */
935 maybe_retrofit_in_chrg (fn
)
938 tree basetype
, arg_types
, parms
, parm
, fntype
;
940 /* If we've already add the in-charge parameter don't do it again. */
941 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
944 /* When processing templates we can't know, in general, whether or
945 not we're going to have virtual baseclasses. */
946 if (uses_template_parms (fn
))
949 /* We don't need an in-charge parameter for constructors that don't
950 have virtual bases. */
951 if (DECL_CONSTRUCTOR_P (fn
)
952 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
955 /* First add it to DECL_ARGUMENTS... */
956 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
957 TREE_READONLY (parm
) = 1;
958 parms
= DECL_ARGUMENTS (fn
);
959 TREE_CHAIN (parm
) = TREE_CHAIN (parms
);
960 TREE_CHAIN (parms
) = parm
;
962 /* ...and then to TYPE_ARG_TYPES. */
963 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
964 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
965 arg_types
= hash_tree_chain (integer_type_node
, TREE_CHAIN (arg_types
));
966 fntype
= build_cplus_method_type (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
968 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
969 fntype
= build_exception_variant (fntype
,
970 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
971 TREE_TYPE (fn
) = fntype
;
973 /* Now we've got the in-charge parameter. */
974 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
976 /* If this is a subobject constructor or destructor, our caller will
977 pass us a pointer to our VTT. */
978 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
980 DECL_VTT_PARM (fn
) = build_artificial_parm (vtt_parm_identifier
,
982 DECL_CONTEXT (DECL_VTT_PARM (fn
)) = fn
;
983 DECL_USE_VTT_PARM (fn
) = build_artificial_parm (NULL_TREE
,
985 DECL_CONTEXT (DECL_USE_VTT_PARM (fn
)) = fn
;
989 /* Classes overload their constituent function names automatically.
990 When a function name is declared in a record structure,
991 its name is changed to it overloaded name. Since names for
992 constructors and destructors can conflict, we place a leading
995 CNAME is the name of the class we are grokking for.
997 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
999 FLAGS contains bits saying what's special about today's
1000 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
1002 If FUNCTION is a destructor, then we must add the `auto-delete' field
1003 as a second parameter. There is some hair associated with the fact
1004 that we must "declare" this variable in the manner consistent with the
1005 way the rest of the arguments were declared.
1007 QUALS are the qualifiers for the this pointer. */
1010 grokclassfn (ctype
, function
, flags
, quals
)
1011 tree ctype
, function
;
1012 enum overload_flags flags
;
1015 tree fn_name
= DECL_NAME (function
);
1016 int this_quals
= TYPE_UNQUALIFIED
;
1018 /* Even within an `extern "C"' block, members get C++ linkage. See
1019 [dcl.link] for details. */
1020 DECL_LANGUAGE (function
) = lang_cplusplus
;
1022 if (fn_name
== NULL_TREE
)
1024 error ("name missing for member function");
1025 fn_name
= get_identifier ("<anonymous>");
1026 DECL_NAME (function
) = fn_name
;
1030 this_quals
= grok_method_quals (ctype
, function
, quals
);
1032 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1034 /* Must add the class instance variable up front. */
1035 /* Right now we just make this a pointer. But later
1036 we may wish to make it special. */
1037 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function
)));
1041 /* The `this' parameter is implicitly `const'; it cannot be
1043 this_quals
|= TYPE_QUAL_CONST
;
1044 qual_type
= cp_build_qualified_type (type
, this_quals
);
1045 parm
= build_artificial_parm (this_identifier
, qual_type
);
1046 c_apply_type_quals_to_decl (this_quals
, parm
);
1048 /* We can make this a register, so long as we don't
1049 accidentally complain if someone tries to take its address. */
1050 DECL_REGISTER (parm
) = 1;
1051 TREE_CHAIN (parm
) = last_function_parms
;
1052 last_function_parms
= parm
;
1055 DECL_ARGUMENTS (function
) = last_function_parms
;
1056 DECL_CONTEXT (function
) = ctype
;
1058 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
1059 maybe_retrofit_in_chrg (function
);
1061 if (flags
== DTOR_FLAG
)
1063 DECL_DESTRUCTOR_P (function
) = 1;
1064 set_mangled_name_for_decl (function
);
1065 TYPE_HAS_DESTRUCTOR (ctype
) = 1;
1068 set_mangled_name_for_decl (function
);
1071 /* Work on the expr used by alignof (this is only called by the parser). */
1080 if (processing_template_decl
)
1081 return build_min (ALIGNOF_EXPR
, sizetype
, expr
);
1083 if (TREE_CODE (expr
) == COMPONENT_REF
1084 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1085 error ("`__alignof__' applied to a bit-field");
1087 if (TREE_CODE (expr
) == INDIRECT_REF
)
1089 best
= t
= TREE_OPERAND (expr
, 0);
1090 bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
1092 while (TREE_CODE (t
) == NOP_EXPR
1093 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
1096 t
= TREE_OPERAND (t
, 0);
1097 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
1098 if (thisalign
> bestalign
)
1099 best
= t
, bestalign
= thisalign
;
1101 return c_alignof (TREE_TYPE (TREE_TYPE (best
)));
1104 return c_alignof (TREE_TYPE (expr
));
1107 /* Create an ARRAY_REF, checking for the user doing things backwards
1111 grok_array_decl (array_expr
, index_exp
)
1112 tree array_expr
, index_exp
;
1114 tree type
= TREE_TYPE (array_expr
);
1115 tree p1
, p2
, i1
, i2
;
1117 if (type
== error_mark_node
|| index_exp
== error_mark_node
)
1118 return error_mark_node
;
1119 if (processing_template_decl
)
1120 return build_min (ARRAY_REF
, type
? TREE_TYPE (type
) : NULL_TREE
,
1121 array_expr
, index_exp
);
1123 if (type
== NULL_TREE
)
1125 /* Something has gone very wrong. Assume we are mistakenly reducing
1126 an expression instead of a declaration. */
1127 error ("parser may be lost: is there a '{' missing somewhere?");
1131 if (TREE_CODE (type
) == OFFSET_TYPE
1132 || TREE_CODE (type
) == REFERENCE_TYPE
)
1133 type
= TREE_TYPE (type
);
1135 /* If they have an `operator[]', use that. */
1136 if (IS_AGGR_TYPE (type
) || IS_AGGR_TYPE (TREE_TYPE (index_exp
)))
1137 return build_opfncall (ARRAY_REF
, LOOKUP_NORMAL
,
1138 array_expr
, index_exp
, NULL_TREE
);
1140 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1141 is a little-known fact that, if `a' is an array and `i' is an
1142 int, you can write `i[a]', which means the same thing as `a[i]'. */
1144 if (TREE_CODE (type
) == ARRAY_TYPE
)
1147 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, 0);
1149 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
1152 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, 0);
1154 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
, 0);
1155 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
, 0);
1157 if ((p1
&& i2
) && (i1
&& p2
))
1158 error ("ambiguous conversion for array subscript");
1161 array_expr
= p1
, index_exp
= i2
;
1163 array_expr
= p2
, index_exp
= i1
;
1166 cp_error ("invalid types `%T[%T]' for array subscript",
1167 type
, TREE_TYPE (index_exp
));
1168 return error_mark_node
;
1171 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
1172 error ("ambiguous conversion for array subscript");
1174 return build_array_ref (array_expr
, index_exp
);
1177 /* Given the cast expression EXP, checking out its validity. Either return
1178 an error_mark_node if there was an unavoidable error, return a cast to
1179 void for trying to delete a pointer w/ the value 0, or return the
1180 call to delete. If DOING_VEC is 1, we handle things differently
1181 for doing an array delete. If DOING_VEC is 2, they gave us the
1182 array size as an argument to delete.
1183 Implements ARM $5.3.4. This is called from the parser. */
1186 delete_sanity (exp
, size
, doing_vec
, use_global_delete
)
1188 int doing_vec
, use_global_delete
;
1191 /* For a regular vector delete (aka, no size argument) we will pass
1192 this down as a NULL_TREE into build_vec_delete. */
1193 tree maxindex
= NULL_TREE
;
1195 if (exp
== error_mark_node
)
1198 if (processing_template_decl
)
1200 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
1201 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
1202 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
1206 if (TREE_CODE (exp
) == OFFSET_REF
)
1207 exp
= resolve_offset_ref (exp
);
1208 exp
= convert_from_reference (exp
);
1209 t
= stabilize_reference (exp
);
1210 t
= build_expr_type_conversion (WANT_POINTER
, t
, 1);
1212 if (t
== NULL_TREE
|| t
== error_mark_node
)
1214 cp_error ("type `%#T' argument given to `delete', expected pointer",
1216 return error_mark_node
;
1221 maxindex
= cp_build_binary_op (MINUS_EXPR
, size
, integer_one_node
);
1222 pedwarn ("anachronistic use of array size in vector delete");
1225 type
= TREE_TYPE (t
);
1227 /* As of Valley Forge, you can delete a pointer to const. */
1229 /* You can't delete functions. */
1230 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
1232 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1233 return error_mark_node
;
1236 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1237 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
1239 cp_warning ("deleting `%T' is undefined", type
);
1243 /* An array can't have been allocated by new, so complain. */
1244 if (TREE_CODE (t
) == ADDR_EXPR
1245 && TREE_CODE (TREE_OPERAND (t
, 0)) == VAR_DECL
1246 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == ARRAY_TYPE
)
1247 cp_warning ("deleting array `%#D'", TREE_OPERAND (t
, 0));
1249 /* Deleting a pointer with the value zero is valid and has no effect. */
1250 if (integer_zerop (t
))
1251 return build1 (NOP_EXPR
, void_type_node
, t
);
1254 return build_vec_delete (t
, maxindex
, sfk_deleting_destructor
,
1258 if (IS_AGGR_TYPE (TREE_TYPE (type
))
1259 && TYPE_GETS_REG_DELETE (TREE_TYPE (type
)))
1261 /* Only do access checking here; we'll be calling op delete
1262 from the destructor. */
1263 tree tmp
= build_op_delete_call (DELETE_EXPR
, t
, size_zero_node
,
1264 LOOKUP_NORMAL
, NULL_TREE
);
1265 if (tmp
== error_mark_node
)
1266 return error_mark_node
;
1269 return build_delete (type
, t
, sfk_deleting_destructor
,
1270 LOOKUP_NORMAL
, use_global_delete
);
1274 /* Report an error if the indicated template declaration is not the
1275 sort of thing that should be a member template. */
1278 check_member_template (tmpl
)
1283 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
1284 decl
= DECL_TEMPLATE_RESULT (tmpl
);
1286 if (TREE_CODE (decl
) == FUNCTION_DECL
1287 || (TREE_CODE (decl
) == TYPE_DECL
1288 && IS_AGGR_TYPE (TREE_TYPE (decl
))))
1290 if (current_function_decl
)
1291 /* 14.5.2.2 [temp.mem]
1293 A local class shall not have member templates. */
1294 cp_error ("invalid declaration of member template `%#D' in local class",
1297 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_VIRTUAL_P (decl
))
1299 /* 14.5.2.3 [temp.mem]
1301 A member function template shall not be virtual. */
1303 ("invalid use of `virtual' in template declaration of `%#D'",
1305 DECL_VIRTUAL_P (decl
) = 0;
1308 /* The debug-information generating code doesn't know what to do
1309 with member templates. */
1310 DECL_IGNORED_P (tmpl
) = 1;
1313 cp_error ("template declaration of `%#D'", decl
);
1316 /* Return true iff TYPE is a valid Java parameter or return type. */
1319 acceptable_java_type (type
)
1322 if (TREE_CODE (type
) == VOID_TYPE
|| TYPE_FOR_JAVA (type
))
1324 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
1326 type
= TREE_TYPE (type
);
1327 if (TREE_CODE (type
) == RECORD_TYPE
)
1330 if (! TYPE_FOR_JAVA (type
))
1332 if (! CLASSTYPE_TEMPLATE_INFO (type
))
1334 args
= CLASSTYPE_TI_ARGS (type
);
1335 i
= TREE_VEC_LENGTH (args
);
1338 type
= TREE_VEC_ELT (args
, i
);
1339 if (TREE_CODE (type
) == POINTER_TYPE
)
1340 type
= TREE_TYPE (type
);
1341 if (! TYPE_FOR_JAVA (type
))
1350 /* For a METHOD in a Java class CTYPE, return 1 if
1351 the parameter and return types are valid Java types.
1352 Otherwise, print appropriate error messages, and return 0. */
1355 check_java_method (method
)
1359 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1360 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
1361 if (! acceptable_java_type (ret_type
))
1363 cp_error ("Java method '%D' has non-Java return type `%T'",
1367 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
1369 tree type
= TREE_VALUE (arg_types
);
1370 if (! acceptable_java_type (type
))
1372 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1377 return jerr
? 0 : 1;
1380 /* Sanity check: report error if this function FUNCTION is not
1381 really a member of the class (CTYPE) it is supposed to belong to.
1382 CNAME is the same here as it is for grokclassfn above. */
1385 check_classfn (ctype
, function
)
1386 tree ctype
, function
;
1388 tree fn_name
= DECL_NAME (function
);
1389 tree fndecl
, fndecls
;
1390 tree method_vec
= CLASSTYPE_METHOD_VEC (complete_type (ctype
));
1394 if (DECL_USE_TEMPLATE (function
)
1395 && !(TREE_CODE (function
) == TEMPLATE_DECL
1396 && DECL_TEMPLATE_SPECIALIZATION (function
))
1397 && is_member_template (DECL_TI_TEMPLATE (function
)))
1398 /* Since this is a specialization of a member template,
1399 we're not going to find the declaration in the class.
1402 struct S { template <typename T> void f(T); };
1403 template <> void S::f(int);
1405 we're not going to find `S::f(int)', but there's no
1406 reason we should, either. We let our callers know we didn't
1407 find the method, but we don't complain. */
1410 if (method_vec
!= 0)
1412 methods
= &TREE_VEC_ELT (method_vec
, 0);
1413 end
= TREE_VEC_END (method_vec
);
1415 /* First suss out ctors and dtors. */
1416 if (*methods
&& fn_name
== DECL_NAME (OVL_CURRENT (*methods
))
1417 && DECL_CONSTRUCTOR_P (function
))
1419 if (*++methods
&& fn_name
== DECL_NAME (OVL_CURRENT (*methods
))
1420 && DECL_DESTRUCTOR_P (function
))
1423 while (++methods
!= end
&& *methods
)
1426 if (fn_name
== DECL_NAME (OVL_CURRENT (*methods
)))
1429 for (fndecls
= *methods
; fndecls
!= NULL_TREE
;
1430 fndecls
= OVL_NEXT (fndecls
))
1432 fndecl
= OVL_CURRENT (fndecls
);
1433 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL, or
1434 for a for member function of a template class, is
1435 not mangled, so the check below does not work
1436 correctly in that case. Since mangled destructor
1437 names do not include the type of the arguments,
1438 we can't use this short-cut for them, either.
1439 (It's not legal to declare arguments for a
1440 destructor, but some people try.) */
1441 if (!DECL_DESTRUCTOR_P (function
)
1442 && (DECL_ASSEMBLER_NAME (function
)
1443 != DECL_NAME (function
))
1444 && (DECL_ASSEMBLER_NAME (fndecl
)
1445 != DECL_NAME (fndecl
))
1446 && (DECL_ASSEMBLER_NAME (function
)
1447 == DECL_ASSEMBLER_NAME (fndecl
)))
1450 /* We cannot simply call decls_match because this
1451 doesn't work for static member functions that are
1452 pretending to be methods, and because the name
1453 may have been changed by asm("new_name"). */
1454 if (DECL_NAME (function
) == DECL_NAME (fndecl
))
1456 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1457 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1459 /* Get rid of the this parameter on functions that become
1461 if (DECL_STATIC_FUNCTION_P (fndecl
)
1462 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1463 p1
= TREE_CHAIN (p1
);
1465 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
1466 TREE_TYPE (TREE_TYPE (fndecl
)))
1467 && compparms (p1
, p2
)
1468 && (DECL_TEMPLATE_SPECIALIZATION (function
)
1469 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
1470 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
1471 || (DECL_TI_TEMPLATE (function
)
1472 == DECL_TI_TEMPLATE (fndecl
))))
1481 if (methods
!= end
&& *methods
)
1483 tree fndecl
= *methods
;
1484 cp_error ("prototype for `%#D' does not match any in class `%T'",
1486 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl
) ? "s are" : " is",
1487 OVL_CURRENT (fndecl
));
1488 while (fndecl
= OVL_NEXT (fndecl
), fndecl
)
1489 cp_error_at (" %#D", OVL_CURRENT(fndecl
));
1494 if (!COMPLETE_TYPE_P (ctype
))
1495 incomplete_type_error (function
, ctype
);
1497 cp_error ("no `%#D' member function declared in class `%T'",
1501 /* If we did not find the method in the class, add it to avoid
1502 spurious errors (unless the CTYPE is not yet defined, in which
1503 case we'll only confuse ourselves when the function is declared
1504 properly within the class. */
1505 if (COMPLETE_TYPE_P (ctype
))
1506 add_method (ctype
, function
, /*error_p=*/1);
1510 /* We have just processed the DECL, which is a static data member.
1511 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1512 present, is the assembly-language name for the data member.
1513 FLAGS is as for cp_finish_decl. */
1516 finish_static_data_member_decl (decl
, init
, asmspec_tree
, flags
)
1522 const char *asmspec
= 0;
1525 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
1527 my_friendly_assert (TREE_PUBLIC (decl
), 0);
1529 DECL_CONTEXT (decl
) = current_class_type
;
1531 /* We cannot call pushdecl here, because that would fill in the
1532 decl of our TREE_CHAIN. Instead, we modify cp_finish_decl to do
1533 the right thing, namely, to put this decl out straight away. */
1534 /* current_class_type can be NULL_TREE in case of error. */
1535 if (!asmspec
&& current_class_type
)
1537 DECL_INITIAL (decl
) = error_mark_node
;
1538 DECL_ASSEMBLER_NAME (decl
) = mangle_decl (decl
);
1540 if (! processing_template_decl
)
1542 if (!pending_statics
)
1543 VARRAY_TREE_INIT (pending_statics
, 32, "pending_statics");
1544 VARRAY_PUSH_TREE (pending_statics
, decl
);
1547 /* Static consts need not be initialized in the class definition. */
1548 if (init
!= NULL_TREE
&& TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
1550 static int explained
= 0;
1552 error ("initializer invalid for static member with constructor");
1555 error ("(an out of class initialization is required)");
1560 /* Force the compiler to know when an uninitialized static const
1561 member is being used. */
1562 if (CP_TYPE_CONST_P (TREE_TYPE (decl
)) && init
== 0)
1563 TREE_USED (decl
) = 1;
1564 DECL_INITIAL (decl
) = init
;
1565 DECL_IN_AGGR_P (decl
) = 1;
1567 cp_finish_decl (decl
, init
, asmspec_tree
, flags
);
1570 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1571 of a structure component, returning a _DECL node.
1572 QUALS is a list of type qualifiers for this decl (such as for declaring
1573 const member functions).
1575 This is done during the parsing of the struct declaration.
1576 The _DECL nodes are chained together and the lot of them
1577 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1579 If class A defines that certain functions in class B are friends, then
1580 the way I have set things up, it is B who is interested in permission
1581 granted by A. However, it is in A's context that these declarations
1582 are parsed. By returning a void_type_node, class A does not attempt
1583 to incorporate the declarations of the friends within its structure.
1585 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1586 CHANGES TO CODE IN `start_method'. */
1589 grokfield (declarator
, declspecs
, init
, asmspec_tree
, attrlist
)
1590 tree declarator
, declspecs
, init
, asmspec_tree
, attrlist
;
1592 register tree value
;
1593 const char *asmspec
= 0;
1594 int flags
= LOOKUP_ONLYCONVERTING
;
1596 /* Convert () initializers to = initializers. */
1597 if (init
== NULL_TREE
&& declarator
!= NULL_TREE
1598 && TREE_CODE (declarator
) == CALL_EXPR
1599 && TREE_OPERAND (declarator
, 0)
1600 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
1601 || TREE_CODE (TREE_OPERAND (declarator
, 0)) == SCOPE_REF
)
1602 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator
)))
1604 /* It's invalid to try to initialize a data member using a
1605 functional notation, e.g.:
1611 Explain that to the user. */
1612 static int explained
;
1614 cp_error ("invalid data member initialization");
1617 cp_error ("(use `=' to initialize static data members)");
1621 declarator
= TREE_OPERAND (declarator
, 0);
1625 if (declspecs
== NULL_TREE
1626 && TREE_CODE (declarator
) == SCOPE_REF
1627 && TREE_CODE (TREE_OPERAND (declarator
, 1)) == IDENTIFIER_NODE
)
1629 /* Access declaration */
1630 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator
, 0))))
1632 else if (TREE_COMPLEXITY (declarator
) == current_class_depth
)
1633 pop_nested_class ();
1634 return do_class_using_decl (declarator
);
1638 && TREE_CODE (init
) == TREE_LIST
1639 && TREE_VALUE (init
) == error_mark_node
1640 && TREE_CHAIN (init
) == NULL_TREE
)
1643 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, attrlist
);
1644 if (! value
|| value
== error_mark_node
)
1645 /* friend or constructor went bad. */
1647 if (TREE_TYPE (value
) == error_mark_node
)
1648 return error_mark_node
;
1650 /* Pass friendly classes back. */
1651 if (TREE_CODE (value
) == VOID_TYPE
)
1652 return void_type_node
;
1654 if (DECL_NAME (value
) != NULL_TREE
1655 && IDENTIFIER_POINTER (DECL_NAME (value
))[0] == '_'
1656 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value
)), "_vptr"))
1657 cp_error ("member `%D' conflicts with virtual function table field name",
1660 /* Stash away type declarations. */
1661 if (TREE_CODE (value
) == TYPE_DECL
)
1663 DECL_NONLOCAL (value
) = 1;
1664 DECL_CONTEXT (value
) = current_class_type
;
1666 if (CLASS_TYPE_P (TREE_TYPE (value
)))
1667 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value
)) = 1;
1669 /* Now that we've updated the context, we need to remangle the
1670 name for this TYPE_DECL. */
1671 DECL_ASSEMBLER_NAME (value
) = DECL_NAME (value
);
1672 if (!uses_template_parms (value
))
1673 DECL_ASSEMBLER_NAME (value
) = mangle_type (TREE_TYPE (value
));
1675 if (processing_template_decl
)
1676 value
= push_template_decl (value
);
1681 if (DECL_IN_AGGR_P (value
))
1683 cp_error ("`%D' is already defined in `%T'", value
,
1684 DECL_CONTEXT (value
));
1685 return void_type_node
;
1689 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
1693 if (TREE_CODE (value
) == FUNCTION_DECL
)
1695 grok_function_init (value
, init
);
1698 else if (pedantic
&& TREE_CODE (value
) != VAR_DECL
)
1699 /* Already complained in grokdeclarator. */
1703 /* We allow initializers to become parameters to base
1705 if (TREE_CODE (init
) == TREE_LIST
)
1707 if (TREE_CHAIN (init
) == NULL_TREE
)
1708 init
= TREE_VALUE (init
);
1710 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
1713 if (TREE_CODE (init
) == CONST_DECL
)
1714 init
= DECL_INITIAL (init
);
1715 else if (TREE_READONLY_DECL_P (init
))
1716 init
= decl_constant_value (init
);
1717 else if (TREE_CODE (init
) == CONSTRUCTOR
)
1718 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
1719 if (init
== error_mark_node
)
1720 /* We must make this look different than `error_mark_node'
1721 because `decl_const_value' would mis-interpret it
1722 as only meaning that this VAR_DECL is defined. */
1723 init
= build1 (NOP_EXPR
, TREE_TYPE (value
), init
);
1724 else if (processing_template_decl
)
1726 else if (! TREE_CONSTANT (init
))
1728 /* We can allow references to things that are effectively
1729 static, since references are initialized with the address. */
1730 if (TREE_CODE (TREE_TYPE (value
)) != REFERENCE_TYPE
1731 || (TREE_STATIC (init
) == 0
1732 && (!DECL_P (init
) || DECL_EXTERNAL (init
) == 0)))
1734 error ("field initializer is not constant");
1735 init
= error_mark_node
;
1741 if (processing_template_decl
&& ! current_function_decl
1742 && (TREE_CODE (value
) == VAR_DECL
|| TREE_CODE (value
) == FUNCTION_DECL
))
1743 value
= push_template_decl (value
);
1746 cplus_decl_attributes (value
, TREE_PURPOSE (attrlist
),
1747 TREE_VALUE (attrlist
));
1749 if (TREE_CODE (value
) == VAR_DECL
)
1751 finish_static_data_member_decl (value
, init
, asmspec_tree
,
1755 if (TREE_CODE (value
) == FIELD_DECL
)
1758 cp_error ("`asm' specifiers are not permitted on non-static data members");
1759 if (DECL_INITIAL (value
) == error_mark_node
)
1760 init
= error_mark_node
;
1761 cp_finish_decl (value
, init
, NULL_TREE
, flags
);
1762 DECL_INITIAL (value
) = init
;
1763 DECL_IN_AGGR_P (value
) = 1;
1766 if (TREE_CODE (value
) == FUNCTION_DECL
)
1770 /* This must override the asm specifier which was placed
1771 by grokclassfn. Lay this out fresh. */
1772 DECL_RTL (value
) = NULL_RTX
;
1773 DECL_ASSEMBLER_NAME (value
) = get_identifier (asmspec
);
1775 cp_finish_decl (value
, init
, asmspec_tree
, flags
);
1777 /* Pass friends back this way. */
1778 if (DECL_FRIEND_P (value
))
1779 return void_type_node
;
1781 DECL_IN_AGGR_P (value
) = 1;
1784 my_friendly_abort (21);
1789 /* Like `grokfield', but for bitfields.
1790 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1793 grokbitfield (declarator
, declspecs
, width
)
1794 tree declarator
, declspecs
, width
;
1796 register tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1799 if (! value
) return NULL_TREE
; /* friends went bad. */
1801 /* Pass friendly classes back. */
1802 if (TREE_CODE (value
) == VOID_TYPE
)
1803 return void_type_node
;
1805 if (TREE_CODE (value
) == TYPE_DECL
)
1807 cp_error ("cannot declare `%D' to be a bitfield type", value
);
1811 /* Usually, finish_struct_1 catches bitifields with invalid types.
1812 But, in the case of bitfields with function type, we confuse
1813 ourselves into thinking they are member functions, so we must
1815 if (TREE_CODE (value
) == FUNCTION_DECL
)
1817 cp_error ("cannot declare bitfield `%D' with funcion type",
1822 if (DECL_IN_AGGR_P (value
))
1824 cp_error ("`%D' is already defined in the class %T", value
,
1825 DECL_CONTEXT (value
));
1826 return void_type_node
;
1829 GNU_xref_member (current_class_name
, value
);
1831 if (TREE_STATIC (value
))
1833 cp_error ("static member `%D' cannot be a bitfield", value
);
1836 cp_finish_decl (value
, NULL_TREE
, NULL_TREE
, 0);
1838 if (width
!= error_mark_node
)
1840 constant_expression_warning (width
);
1841 DECL_INITIAL (value
) = width
;
1842 SET_DECL_C_BIT_FIELD (value
);
1845 DECL_IN_AGGR_P (value
) = 1;
1850 grokoptypename (declspecs
, declarator
)
1851 tree declspecs
, declarator
;
1853 tree t
= grokdeclarator (declarator
, declspecs
, TYPENAME
, 0, NULL_TREE
);
1854 return mangle_conv_op_name_for_type (t
);
1857 /* When a function is declared with an initializer,
1858 do the right thing. Currently, there are two possibilities:
1863 // initialization possibility #1.
1864 virtual void f () = 0;
1886 // initialization possibility #2
1893 copy_assignment_arg_p (parmtype
, virtualp
)
1895 int virtualp ATTRIBUTE_UNUSED
;
1897 if (current_class_type
== NULL_TREE
)
1900 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
1901 parmtype
= TREE_TYPE (parmtype
);
1903 if ((TYPE_MAIN_VARIANT (parmtype
) == current_class_type
)
1905 /* Non-standard hack to support old Booch components. */
1906 || (! virtualp
&& DERIVED_FROM_P (parmtype
, current_class_type
))
1915 grok_function_init (decl
, init
)
1919 /* An initializer for a function tells how this function should
1921 tree type
= TREE_TYPE (decl
);
1923 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1924 cp_error ("initializer specified for non-member function `%D'", decl
);
1926 /* We'll check for this in finish_struct_1. */
1927 else if (DECL_VINDEX (decl
) == NULL_TREE
)
1928 cp_error ("initializer specified for non-virtual member function `%D'", decl
);
1930 else if (integer_zerop (init
))
1933 /* Mark this function as being "defined". */
1934 DECL_INITIAL (decl
) = error_mark_node
;
1935 /* pure virtual destructors must be defined. */
1936 /* pure virtual needs to be defined (as abort) only when put in
1937 vtbl. For wellformed call, it should be itself. pr4737 */
1938 if (!DECL_DESTRUCTOR_P (decl
)))
1940 /* Give this node rtl from `abort'. */
1941 DECL_RTL (decl
) = DECL_RTL (abort_fndecl
);
1944 DECL_PURE_VIRTUAL_P (decl
) = 1;
1945 if (DECL_OVERLOADED_OPERATOR_P (decl
) == NOP_EXPR
)
1948 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
))));
1950 if (copy_assignment_arg_p (parmtype
, 1))
1951 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type
) = 1;
1955 cp_error ("invalid initializer for virtual method `%D'", decl
);
1959 cplus_decl_attributes (decl
, attributes
, prefix_attributes
)
1960 tree decl
, attributes
, prefix_attributes
;
1962 if (decl
== NULL_TREE
|| decl
== void_type_node
)
1965 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1966 decl
= DECL_TEMPLATE_RESULT (decl
);
1968 decl_attributes (decl
, attributes
, prefix_attributes
);
1970 if (TREE_CODE (decl
) == TYPE_DECL
)
1971 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl
), TREE_TYPE (decl
));
1974 /* CONSTRUCTOR_NAME:
1975 Return the name for the constructor (or destructor) for the
1976 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1977 IDENTIFIER_NODE. When given a template, this routine doesn't
1978 lose the specialization. */
1981 constructor_name_full (thing
)
1984 if (TREE_CODE (thing
) == TEMPLATE_TYPE_PARM
1985 || TREE_CODE (thing
) == BOUND_TEMPLATE_TEMPLATE_PARM
1986 || TREE_CODE (thing
) == TYPENAME_TYPE
)
1987 thing
= TYPE_NAME (thing
);
1988 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing
)))
1990 if (TYPE_WAS_ANONYMOUS (thing
) && TYPE_HAS_CONSTRUCTOR (thing
))
1991 thing
= DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing
), 0)));
1993 thing
= TYPE_NAME (thing
);
1995 if (TREE_CODE (thing
) == TYPE_DECL
1996 || (TREE_CODE (thing
) == TEMPLATE_DECL
1997 && TREE_CODE (DECL_TEMPLATE_RESULT (thing
)) == TYPE_DECL
))
1998 thing
= DECL_NAME (thing
);
1999 my_friendly_assert (TREE_CODE (thing
) == IDENTIFIER_NODE
, 197);
2003 /* CONSTRUCTOR_NAME:
2004 Return the name for the constructor (or destructor) for the
2005 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2006 IDENTIFIER_NODE. When given a template, return the plain
2007 unspecialized name. */
2010 constructor_name (thing
)
2014 thing
= constructor_name_full (thing
);
2015 t
= IDENTIFIER_TEMPLATE (thing
);
2021 /* Defer the compilation of the FN until the end of compilation. */
2027 if (DECL_DEFERRED_FN (fn
))
2029 DECL_DEFERRED_FN (fn
) = 1;
2031 VARRAY_TREE_INIT (deferred_fns
, 32, "deferred_fns");
2033 VARRAY_PUSH_TREE (deferred_fns
, fn
);
2036 /* Hand off a unique name which can be used for variable we don't really
2037 want to know about anyway, for example, the anonymous variables which
2038 are needed to make references work. Declare this thing so we can use it.
2039 The variable created will be of type TYPE, and will have internal
2043 get_temp_name (type
)
2046 char buf
[sizeof (AUTO_TEMP_FORMAT
) + 20];
2048 int toplev
= toplevel_bindings_p ();
2050 sprintf (buf
, AUTO_TEMP_FORMAT
, global_temp_name_counter
++);
2051 decl
= build_decl (VAR_DECL
, get_identifier (buf
), type
);
2052 DECL_ARTIFICIAL (decl
) = 1;
2053 TREE_USED (decl
) = 1;
2054 TREE_STATIC (decl
) = 1;
2056 decl
= pushdecl_top_level (decl
);
2058 /* If this is a local variable, then lay out its rtl now.
2059 Otherwise, callers of this function are responsible for dealing
2060 with this variable's rtl. */
2064 my_friendly_assert (DECL_INITIAL (decl
) == NULL_TREE
,
2071 /* Hunts through the global anonymous union ANON_DECL, building
2072 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2073 returns a VAR_DECL whose size is the same as the size of the
2074 ANON_DECL, if one is available. */
2077 build_anon_union_vars (anon_decl
, elems
, static_p
, external_p
)
2083 tree type
= TREE_TYPE (anon_decl
);
2084 tree main_decl
= NULL_TREE
;
2087 /* Rather than write the code to handle the non-union case,
2088 just give an error. */
2089 if (TREE_CODE (type
) != UNION_TYPE
)
2090 error ("anonymous struct not inside named type");
2092 for (field
= TYPE_FIELDS (type
);
2094 field
= TREE_CHAIN (field
))
2098 if (DECL_ARTIFICIAL (field
))
2100 if (TREE_CODE (field
) != FIELD_DECL
)
2102 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2107 if (TREE_PRIVATE (field
))
2108 cp_pedwarn_at ("private member `%#D' in anonymous union", field
);
2109 else if (TREE_PROTECTED (field
))
2110 cp_pedwarn_at ("protected member `%#D' in anonymous union", field
);
2112 if (DECL_NAME (field
) == NULL_TREE
2113 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2115 decl
= build_anon_union_vars (field
, elems
, static_p
, external_p
);
2119 else if (DECL_NAME (field
) == NULL_TREE
)
2123 decl
= build_decl (VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
2124 /* tell `pushdecl' that this is not tentative. */
2125 DECL_INITIAL (decl
) = error_mark_node
;
2126 TREE_PUBLIC (decl
) = 0;
2127 TREE_STATIC (decl
) = static_p
;
2128 DECL_EXTERNAL (decl
) = external_p
;
2129 decl
= pushdecl (decl
);
2130 DECL_INITIAL (decl
) = NULL_TREE
;
2133 /* Only write out one anon union element--choose the largest
2134 one. We used to try to find one the same size as the union,
2135 but that fails if the ABI forces us to align the union more
2137 if (main_decl
== NULL_TREE
2138 || tree_int_cst_lt (DECL_SIZE (main_decl
), DECL_SIZE (decl
)))
2141 TREE_ASM_WRITTEN (main_decl
) = 1;
2145 /* ??? This causes there to be no debug info written out
2147 TREE_ASM_WRITTEN (decl
) = 1;
2149 if (DECL_NAME (field
) == NULL_TREE
2150 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2151 /* The remainder of the processing was already done in the
2155 /* If there's a cleanup to do, it belongs in the
2156 TREE_PURPOSE of the following TREE_LIST. */
2157 *elems
= tree_cons (NULL_TREE
, decl
, *elems
);
2158 TREE_TYPE (*elems
) = type
;
2164 /* Finish off the processing of a UNION_TYPE structure.
2165 If there are static members, then all members are
2166 static, and must be laid out together. If the
2167 union is an anonymous union, we arrange for that
2168 as well. PUBLIC_P is nonzero if this union is
2169 not declared static. */
2172 finish_anon_union (anon_union_decl
)
2173 tree anon_union_decl
;
2175 tree type
= TREE_TYPE (anon_union_decl
);
2177 int public_p
= TREE_PUBLIC (anon_union_decl
);
2178 int static_p
= TREE_STATIC (anon_union_decl
);
2179 int external_p
= DECL_EXTERNAL (anon_union_decl
);
2181 if (TYPE_FIELDS (type
) == NULL_TREE
)
2186 error ("namespace-scope anonymous aggregates must be static");
2190 main_decl
= build_anon_union_vars (anon_union_decl
,
2191 &DECL_ANON_UNION_ELEMS (anon_union_decl
),
2192 static_p
, external_p
);
2194 if (main_decl
== NULL_TREE
)
2196 warning ("anonymous aggregate with no members");
2202 make_decl_rtl (main_decl
, 0);
2203 DECL_RTL (anon_union_decl
) = DECL_RTL (main_decl
);
2204 expand_anon_union_decl (anon_union_decl
,
2206 DECL_ANON_UNION_ELEMS (anon_union_decl
));
2209 add_decl_stmt (anon_union_decl
);
2212 /* Finish processing a builtin type TYPE. It's name is NAME,
2213 its fields are in the array FIELDS. LEN is the number of elements
2214 in FIELDS minus one, or put another way, it is the maximum subscript
2217 It is given the same alignment as ALIGN_TYPE. */
2220 finish_builtin_type (type
, name
, fields
, len
, align_type
)
2229 TYPE_FIELDS (type
) = fields
[0];
2230 for (i
= 0; i
< len
; i
++)
2232 layout_type (TREE_TYPE (fields
[i
]));
2233 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
2234 TREE_CHAIN (fields
[i
]) = fields
[i
+1];
2236 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
2237 TYPE_ALIGN (type
) = TYPE_ALIGN (align_type
);
2238 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (align_type
);
2240 #if 0 /* not yet, should get fixed properly later */
2241 TYPE_NAME (type
) = make_type_decl (get_identifier (name
), type
);
2243 TYPE_NAME (type
) = build_decl (TYPE_DECL
, get_identifier (name
), type
);
2245 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
2246 layout_decl (TYPE_NAME (type
), 0);
2249 /* Auxiliary functions to make type signatures for
2250 `operator new' and `operator delete' correspond to
2251 what compiler will be expecting. */
2254 coerce_new_type (type
)
2258 tree args
= TYPE_ARG_TYPES (type
);
2260 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
2262 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
2263 e
= 1, cp_error ("`operator new' must return type `%T'", ptr_type_node
);
2265 if (!args
|| args
== void_list_node
2266 || !same_type_p (TREE_VALUE (args
), c_size_type_node
))
2269 if (args
&& args
!= void_list_node
)
2270 args
= TREE_CHAIN (args
);
2271 cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node
);
2276 args
= tree_cons (NULL_TREE
, c_size_type_node
, args
);
2279 type
= build_exception_variant
2280 (build_function_type (ptr_type_node
, args
),
2281 TYPE_RAISES_EXCEPTIONS (type
));
2289 coerce_delete_type (type
)
2293 tree args
= TYPE_ARG_TYPES (type
);
2295 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
2297 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
2298 e
= 1, cp_error ("`operator delete' must return type `%T'", void_type_node
);
2300 if (!args
|| args
== void_list_node
2301 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
2304 if (args
&& args
!= void_list_node
)
2305 args
= TREE_CHAIN (args
);
2306 cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node
);
2311 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
2314 type
= build_exception_variant
2315 (build_function_type (void_type_node
, args
),
2316 TYPE_RAISES_EXCEPTIONS (type
));
2325 mark_vtable_entries (decl
)
2328 tree entries
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
));
2330 for (; entries
; entries
= TREE_CHAIN (entries
))
2335 fnaddr
= (flag_vtable_thunks
? TREE_VALUE (entries
)
2336 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries
)));
2338 if (TREE_CODE (fnaddr
) != ADDR_EXPR
)
2339 /* This entry is an offset: a virtual base class offset, a
2340 virtual call offset, and RTTI offset, etc. */
2343 fn
= TREE_OPERAND (fnaddr
, 0);
2344 TREE_ADDRESSABLE (fn
) = 1;
2345 /* When we don't have vcall offsets, we output thunks whenever
2346 we output the vtables that contain them. With vcall offsets,
2347 we know all the thunks we'll need when we emit a virtual
2348 function, so we emit the thunks there instead. */
2349 if (DECL_THUNK_P (fn
))
2350 use_thunk (fn
, THUNK_GENERATE_WITH_VTABLE_P (fn
));
2355 /* Set DECL up to have the closest approximation of "initialized common"
2356 linkage available. */
2359 comdat_linkage (decl
)
2363 make_decl_one_only (decl
);
2364 else if (TREE_CODE (decl
) == FUNCTION_DECL
2365 || (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
)))
2366 /* We can just emit function and compiler-generated variables
2367 statically; having multiple copies is (for the most part) only
2370 There are two correctness issues, however: the address of a
2371 template instantiation with external linkage should be the
2372 same, independent of what translation unit asks for the
2373 address, and this will not hold when we emit multiple copies of
2374 the function. However, there's little else we can do.
2376 Also, by default, the typeinfo implementation for the new ABI
2377 assumes that there will be only one copy of the string used as
2378 the name for each type. Therefore, if weak symbols are
2379 unavailable, the run-time library should perform a more
2380 conservative check; it should perform a string comparison,
2381 rather than an address comparison. */
2382 TREE_PUBLIC (decl
) = 0;
2385 /* Static data member template instantiations, however, cannot
2386 have multiple copies. */
2387 if (DECL_INITIAL (decl
) == 0
2388 || DECL_INITIAL (decl
) == error_mark_node
)
2389 DECL_COMMON (decl
) = 1;
2390 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
2392 DECL_COMMON (decl
) = 1;
2393 DECL_INITIAL (decl
) = error_mark_node
;
2397 /* We can't do anything useful; leave vars for explicit
2399 DECL_EXTERNAL (decl
) = 1;
2400 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2404 if (DECL_LANG_SPECIFIC (decl
))
2405 DECL_COMDAT (decl
) = 1;
2408 /* For win32 we also want to put explicit instantiations in
2409 linkonce sections, so that they will be merged with implicit
2410 instantiations; otherwise we get duplicate symbol errors. */
2413 maybe_make_one_only (decl
)
2416 /* We used to say that this was not necessary on targets that support weak
2417 symbols, because the implicit instantiations will defer to the explicit
2418 one. However, that's not actually the case in SVR4; a strong definition
2419 after a weak one is an error. Also, not making explicit
2420 instantiations one_only means that we can end up with two copies of
2421 some template instantiations. */
2422 if (! supports_one_only ())
2425 /* We can't set DECL_COMDAT on functions, or finish_file will think
2426 we can get away with not emitting them if they aren't used. We need
2427 to for variables so that cp_finish_decl will update their linkage,
2428 because their DECL_INITIAL may not have been set properly yet. */
2430 make_decl_one_only (decl
);
2432 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_LANG_SPECIFIC (decl
))
2433 DECL_COMDAT (decl
) = 1;
2436 /* Returns the virtual function with which the vtable for TYPE is
2437 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2445 if (TYPE_FOR_JAVA (type
)
2446 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
2447 || CLASSTYPE_INTERFACE_KNOWN (type
))
2450 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
2451 method
= TREE_CHAIN (method
))
2452 if (DECL_VINDEX (method
) != NULL_TREE
2453 && ! DECL_THIS_INLINE (method
)
2454 && ! DECL_PURE_VIRTUAL_P (method
))
2460 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2461 based on TYPE and other static flags.
2463 Note that anything public is tagged TREE_PUBLIC, whether
2464 it's public in this file or in another one. */
2467 import_export_vtable (decl
, type
, final
)
2471 if (DECL_INTERFACE_KNOWN (decl
))
2474 if (TYPE_FOR_JAVA (type
))
2476 TREE_PUBLIC (decl
) = 1;
2477 DECL_EXTERNAL (decl
) = 1;
2478 DECL_INTERFACE_KNOWN (decl
) = 1;
2480 else if (CLASSTYPE_INTERFACE_KNOWN (type
))
2482 TREE_PUBLIC (decl
) = 1;
2483 DECL_EXTERNAL (decl
) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type
);
2484 DECL_INTERFACE_KNOWN (decl
) = 1;
2488 /* We can only wait to decide if we have real non-inline virtual
2489 functions in our class, or if we come from a template. */
2491 int found
= (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
2492 || key_method (type
));
2494 if (final
|| ! found
)
2496 comdat_linkage (decl
);
2497 DECL_EXTERNAL (decl
) = 0;
2501 TREE_PUBLIC (decl
) = 1;
2502 DECL_EXTERNAL (decl
) = 1;
2507 /* Determine whether or not we want to specifically import or export CTYPE,
2508 using various heuristics. */
2511 import_export_class (ctype
)
2514 /* -1 for imported, 1 for exported. */
2515 int import_export
= 0;
2517 /* It only makes sense to call this function at EOF. The reason is
2518 that this function looks at whether or not the first non-inline
2519 non-abstract virtual member function has been defined in this
2520 translation unit. But, we can't possibly know that until we've
2521 seen the entire translation unit. */
2522 my_friendly_assert (at_eof
, 20000226);
2524 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2527 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2528 we will have CLASSTYPE_INTERFACE_ONLY set but not
2529 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2530 heuristic because someone will supply a #pragma implementation
2531 elsewhere, and deducing it here would produce a conflict. */
2532 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
2535 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2536 /* FIXME this should really use some sort of target-independent macro. */
2537 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
2539 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
2543 /* If we got -fno-implicit-templates, we import template classes that
2544 weren't explicitly instantiated. */
2545 if (import_export
== 0
2546 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
2547 && ! flag_implicit_templates
)
2550 /* Base our import/export status on that of the first non-inline,
2551 non-pure virtual function, if any. */
2552 if (import_export
== 0
2553 && TYPE_POLYMORPHIC_P (ctype
))
2555 tree method
= key_method (ctype
);
2557 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
2560 #ifdef MULTIPLE_SYMBOL_SPACES
2561 if (import_export
== -1)
2567 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
2568 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype
) = (import_export
> 0);
2569 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
2573 /* We need to describe to the assembler the relationship between
2574 a vtable and the vtable of the parent class. */
2577 output_vtable_inherit (vars
)
2583 op
[0] = XEXP (DECL_RTL (vars
), 0); /* strip the mem ref */
2585 parent
= binfo_for_vtable (vars
);
2587 if (parent
== TYPE_BINFO (DECL_CONTEXT (vars
)))
2591 parent
= get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent
)));
2592 op
[1] = XEXP (DECL_RTL (parent
), 0); /* strip the mem ref */
2595 my_friendly_abort (980826);
2597 output_asm_insn (".vtable_inherit %c0, %c1", op
);
2601 finish_vtable_vardecl (t
, data
)
2603 void *data ATTRIBUTE_UNUSED
;
2606 tree ctype
= DECL_CONTEXT (vars
);
2607 import_export_class (ctype
);
2608 import_export_vtable (vars
, ctype
, 1);
2610 if (! DECL_EXTERNAL (vars
)
2611 && DECL_NEEDED_P (vars
)
2612 && ! TREE_ASM_WRITTEN (vars
))
2614 if (TREE_TYPE (vars
) == void_type_node
)
2615 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2619 mark_vtable_entries (vars
);
2620 if (TREE_TYPE (DECL_INITIAL (vars
)) == 0)
2621 store_init_value (vars
, DECL_INITIAL (vars
));
2623 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== DWARF2_DEBUG
)
2625 /* Mark the VAR_DECL node representing the vtable itself as a
2626 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2627 It is rather important that such things be ignored because
2628 any effort to actually generate DWARF for them will run
2629 into trouble when/if we encounter code like:
2632 struct S { virtual void member (); };
2634 because the artificial declaration of the vtable itself (as
2635 manufactured by the g++ front end) will say that the vtable
2636 is a static member of `S' but only *after* the debug output
2637 for the definition of `S' has already been output. This causes
2638 grief because the DWARF entry for the definition of the vtable
2639 will try to refer back to an earlier *declaration* of the
2640 vtable as a static member of `S' and there won't be one.
2641 We might be able to arrange to have the "vtable static member"
2642 attached to the member list for `S' before the debug info for
2643 `S' get written (which would solve the problem) but that would
2644 require more intrusive changes to the g++ front end. */
2646 DECL_IGNORED_P (vars
) = 1;
2649 /* Always make vtables weak. */
2651 comdat_linkage (vars
);
2653 rest_of_decl_compilation (vars
, NULL_PTR
, 1, 1);
2656 output_vtable_inherit (vars
);
2658 /* Because we're only doing syntax-checking, we'll never end up
2659 actually marking the variable as written. */
2660 if (flag_syntax_only
)
2661 TREE_ASM_WRITTEN (vars
) = 1;
2663 /* Since we're writing out the vtable here, also write the debug
2665 note_debug_info_needed (ctype
);
2670 /* If the references to this class' vtables were optimized away, still
2671 emit the appropriate debugging information. See dfs_debug_mark. */
2672 if (DECL_COMDAT (vars
)
2673 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2674 note_debug_info_needed (ctype
);
2680 prune_vtable_vardecl (t
, data
)
2682 void *data ATTRIBUTE_UNUSED
;
2684 *t
= TREE_CHAIN (*t
);
2688 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2689 inline function or template instantiation at end-of-file. */
2692 import_export_decl (decl
)
2695 if (DECL_INTERFACE_KNOWN (decl
))
2698 if (DECL_TEMPLATE_INSTANTIATION (decl
)
2699 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
2701 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2702 if ((DECL_IMPLICIT_INSTANTIATION (decl
)
2703 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
2704 && (flag_implicit_templates
2705 || (flag_implicit_inline_templates
&& DECL_THIS_INLINE (decl
))))
2707 if (!TREE_PUBLIC (decl
))
2708 /* Templates are allowed to have internal linkage. See
2712 comdat_linkage (decl
);
2715 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2717 else if (DECL_FUNCTION_MEMBER_P (decl
))
2719 tree ctype
= DECL_CONTEXT (decl
);
2720 import_export_class (ctype
);
2721 if (CLASSTYPE_INTERFACE_KNOWN (ctype
)
2722 && ! DECL_THIS_INLINE (decl
))
2724 DECL_NOT_REALLY_EXTERN (decl
)
2725 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2726 || (DECL_THIS_INLINE (decl
) && ! flag_implement_inlines
2727 && !DECL_VINDEX (decl
)));
2729 /* Always make artificials weak. */
2730 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
2731 comdat_linkage (decl
);
2733 maybe_make_one_only (decl
);
2736 comdat_linkage (decl
);
2738 else if (DECL_TINFO_FN_P (decl
))
2740 tree ctype
= TREE_TYPE (DECL_NAME (decl
));
2742 if (IS_AGGR_TYPE (ctype
))
2743 import_export_class (ctype
);
2745 if (IS_AGGR_TYPE (ctype
) && CLASSTYPE_INTERFACE_KNOWN (ctype
)
2746 && TYPE_POLYMORPHIC_P (ctype
)
2747 /* If -fno-rtti, we're not necessarily emitting this stuff with
2748 the class, so go ahead and emit it now. This can happen
2749 when a class is used in exception handling. */
2751 /* If the type is a cv-qualified variant of a type, then we
2752 must emit the tinfo function in this translation unit
2753 since it will not be emitted when the vtable for the type
2754 is output (which is when the unqualified version is
2756 && same_type_p (ctype
, TYPE_MAIN_VARIANT (ctype
)))
2758 DECL_NOT_REALLY_EXTERN (decl
)
2759 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2760 || (DECL_THIS_INLINE (decl
) && ! flag_implement_inlines
2761 && !DECL_VINDEX (decl
)));
2763 /* Always make artificials weak. */
2765 comdat_linkage (decl
);
2767 else if (TYPE_BUILT_IN (ctype
)
2768 && same_type_p (ctype
, TYPE_MAIN_VARIANT (ctype
)))
2769 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2771 comdat_linkage (decl
);
2774 comdat_linkage (decl
);
2776 DECL_INTERFACE_KNOWN (decl
) = 1;
2780 build_cleanup (decl
)
2784 tree type
= TREE_TYPE (decl
);
2786 if (TREE_CODE (type
) == ARRAY_TYPE
)
2790 mark_addressable (decl
);
2791 temp
= build1 (ADDR_EXPR
, build_pointer_type (type
), decl
);
2793 temp
= build_delete (TREE_TYPE (temp
), temp
,
2794 sfk_complete_destructor
,
2795 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
2799 /* Returns the initialization guard variable for the variable DECL,
2800 which has static storage duration. */
2809 sname
= mangle_guard_variable (decl
);
2810 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
2815 /* Under the new ABI, we use a type that is big enough to
2816 contain a mutex as well as an integer counter. */
2817 guard_type
= long_long_integer_type_node
;
2818 guard
= build_decl (VAR_DECL
, sname
, guard_type
);
2820 /* The guard should have the same linkage as what it guards. */
2821 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
2822 TREE_STATIC (guard
) = TREE_STATIC (decl
);
2823 DECL_COMMON (guard
) = DECL_COMMON (decl
);
2824 DECL_ONE_ONLY (guard
) = DECL_ONE_ONLY (decl
);
2825 if (TREE_PUBLIC (decl
))
2826 DECL_WEAK (guard
) = DECL_WEAK (decl
);
2828 DECL_ARTIFICIAL (guard
) = 1;
2829 TREE_USED (guard
) = 1;
2830 pushdecl_top_level (guard
);
2831 cp_finish_decl (guard
, NULL_TREE
, NULL_TREE
, 0);
2836 /* Return those bits of the GUARD variable that should be set when the
2837 guarded entity is actually initialized. */
2840 get_guard_bits (guard
)
2843 /* Under the new ABI, we only set the first byte of the guard,
2844 in order to leave room for a mutex in the high-order bits. */
2845 guard
= build1 (ADDR_EXPR
,
2846 build_pointer_type (TREE_TYPE (guard
)),
2848 guard
= build1 (NOP_EXPR
,
2849 build_pointer_type (char_type_node
),
2851 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
2856 /* Return an expression which determines whether or not the GUARD
2857 variable has already been initialized. */
2860 get_guard_cond (guard
)
2865 /* Check to see if the GUARD is zero. */
2866 guard
= get_guard_bits (guard
);
2867 guard_value
= integer_zero_node
;
2868 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
2869 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
2870 return cp_build_binary_op (EQ_EXPR
, guard
, guard_value
);
2873 /* Return an expression which sets the GUARD variable, indicating that
2874 the variable being guarded has been initialized. */
2882 /* Set the GUARD to one. */
2883 guard
= get_guard_bits (guard
);
2884 guard_init
= integer_one_node
;
2885 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
2886 guard_init
= convert (TREE_TYPE (guard
), guard_init
);
2887 return build_modify_expr (guard
, NOP_EXPR
, guard_init
);
2890 /* Start the process of running a particular set of global constructors
2891 or destructors. Subroutine of do_[cd]tors. */
2894 start_objects (method_type
, initp
)
2895 int method_type
, initp
;
2901 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2903 if (initp
!= DEFAULT_INIT_PRIORITY
)
2913 sprintf (type
, "%c%c%.5u", method_type
, joiner
, initp
);
2916 sprintf (type
, "%c", method_type
);
2918 fnname
= get_file_function_name_long (type
);
2920 start_function (void_list_node
,
2921 make_call_declarator (fnname
, void_list_node
, NULL_TREE
,
2923 NULL_TREE
, SF_DEFAULT
);
2925 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2926 /* It can be a static function as long as collect2 does not have
2927 to scan the object file to find its ctor/dtor routine. */
2928 TREE_PUBLIC (current_function_decl
) = 0;
2931 /* Mark this declaration as used to avoid spurious warnings. */
2932 TREE_USED (current_function_decl
) = 1;
2934 /* Mark this function as a global constructor or destructor. */
2935 if (method_type
== 'I')
2936 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
2938 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
2939 GLOBAL_INIT_PRIORITY (current_function_decl
) = initp
;
2941 body
= begin_compound_stmt (/*has_no_scope=*/0);
2943 /* We cannot allow these functions to be elided, even if they do not
2944 have external linkage. And, there's no point in deferring
2945 copmilation of thes functions; they're all going to have to be
2947 current_function_cannot_inline
2948 = "static constructors and destructors cannot be inlined";
2953 /* Finish the process of running a particular set of global constructors
2954 or destructors. Subroutine of do_[cd]tors. */
2957 finish_objects (method_type
, initp
, body
)
2958 int method_type
, initp
;
2965 finish_compound_stmt (/*has_no_scope=*/0, body
);
2966 fn
= finish_function (0);
2969 /* When only doing semantic analysis, and no RTL generation, we
2970 can't call functions that directly emit assembly code; there is
2971 no assembly file in which to put the code. */
2972 if (flag_syntax_only
)
2975 fnname
= XSTR (XEXP (DECL_RTL (fn
), 0), 0);
2976 if (initp
== DEFAULT_INIT_PRIORITY
)
2978 if (method_type
== 'I')
2979 assemble_constructor (fnname
);
2981 assemble_destructor (fnname
);
2983 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
2984 /* If we're using init priority we can't use assemble_*tor, but on ELF
2985 targets we can stick the references into named sections for GNU ld
2990 sprintf (buf
, ".%ctors.%.5u", method_type
== 'I' ? 'c' : 'd',
2991 /* invert the numbering so the linker puts us in the proper
2992 order; constructors are run from right to left, and the
2993 linker sorts in increasing order. */
2994 MAX_INIT_PRIORITY
- initp
);
2995 named_section (NULL_TREE
, buf
, 0);
2996 assemble_integer (gen_rtx_SYMBOL_REF (Pmode
, fnname
),
2997 POINTER_SIZE
/ BITS_PER_UNIT
, 1);
3002 /* The names of the parameters to the function created to handle
3003 initializations and destructions for objects with static storage
3005 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
3006 #define PRIORITY_IDENTIFIER "__priority"
3008 /* The name of the function we create to handle initializations and
3009 destructions for objects with static storage duration. */
3010 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
3012 /* The declaration for the __INITIALIZE_P argument. */
3013 static tree initialize_p_decl
;
3015 /* The declaration for the __PRIORITY argument. */
3016 static tree priority_decl
;
3018 /* The declaration for the static storage duration function. */
3019 static tree ssdf_decl
;
3021 /* All the static storage duration functions created in this
3022 translation unit. */
3023 static varray_type ssdf_decls
;
3025 /* A map from priority levels to information about that priority
3026 level. There may be many such levels, so efficient lookup is
3028 static splay_tree priority_info_map
;
3030 /* Begins the generation of the function that will handle all
3031 initialization and destruction of objects with static storage
3032 duration. The function generated takes two parameters of type
3033 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
3034 non-zero, it performs initializations. Otherwise, it performs
3035 destructions. It only performs those initializations or
3036 destructions with the indicated __PRIORITY. The generated function
3039 It is assumed that this function will only be called once per
3040 translation unit. */
3043 start_static_storage_duration_function ()
3045 static unsigned ssdf_number
;
3050 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3052 /* Create the identifier for this function. It will be of the form
3053 SSDF_IDENTIFIER_<number>. */
3054 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, ssdf_number
++);
3055 if (ssdf_number
== 0)
3057 /* Overflow occurred. That means there are at least 4 billion
3058 initialization functions. */
3059 sorry ("too many initialization functions required");
3060 my_friendly_abort (19990430);
3063 /* Create the parameters. */
3064 parm_types
= void_list_node
;
3065 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
3066 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
3067 type
= build_function_type (void_type_node
, parm_types
);
3069 /* Create the FUNCTION_DECL itself. */
3070 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3071 get_identifier (id
),
3073 TREE_PUBLIC (ssdf_decl
) = 0;
3074 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3076 /* Put this function in the list of functions to be called from the
3077 static constructors and destructors. */
3080 VARRAY_TREE_INIT (ssdf_decls
, 32, "ssdf_decls");
3082 /* Take this opportunity to initialize the map from priority
3083 numbers to information about that priority level. */
3084 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3085 /*delete_key_fn=*/0,
3086 /*delete_value_fn=*/
3087 (splay_tree_delete_value_fn
) &free
);
3089 /* We always need to generate functions for the
3090 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3091 priorities later, we'll be sure to find the
3092 DEFAULT_INIT_PRIORITY. */
3093 get_priority_info (DEFAULT_INIT_PRIORITY
);
3096 VARRAY_PUSH_TREE (ssdf_decls
, ssdf_decl
);
3098 /* Create the argument list. */
3099 initialize_p_decl
= build_decl (PARM_DECL
,
3100 get_identifier (INITIALIZE_P_IDENTIFIER
),
3102 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
3103 DECL_ARG_TYPE (initialize_p_decl
) = integer_type_node
;
3104 TREE_USED (initialize_p_decl
) = 1;
3105 priority_decl
= build_decl (PARM_DECL
, get_identifier (PRIORITY_IDENTIFIER
),
3107 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
3108 DECL_ARG_TYPE (priority_decl
) = integer_type_node
;
3109 TREE_USED (priority_decl
) = 1;
3111 TREE_CHAIN (initialize_p_decl
) = priority_decl
;
3112 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3114 /* Put the function in the global scope. */
3115 pushdecl (ssdf_decl
);
3117 /* Start the function itself. This is equivalent to declarating the
3120 static void __ssdf (int __initialize_p, init __priority_p);
3122 It is static because we only need to call this function from the
3123 various constructor and destructor functions for this module. */
3124 start_function (/*specs=*/NULL_TREE
,
3126 /*attrs=*/NULL_TREE
,
3129 /* Set up the scope of the outermost block in the function. */
3130 body
= begin_compound_stmt (/*has_no_scope=*/0);
3132 /* This function must not be deferred because we are depending on
3133 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3134 current_function_cannot_inline
3135 = "static storage duration functions cannot be inlined";
3140 /* Finish the generation of the function which performs initialization
3141 and destruction of objects with static storage duration. After
3142 this point, no more such objects can be created. */
3145 finish_static_storage_duration_function (body
)
3148 /* Close out the function. */
3149 finish_compound_stmt (/*has_no_scope=*/0, body
);
3150 expand_body (finish_function (0));
3153 /* Return the information about the indicated PRIORITY level. If no
3154 code to handle this level has yet been generated, generate the
3155 appropriate prologue. */
3157 static priority_info
3158 get_priority_info (priority
)
3164 n
= splay_tree_lookup (priority_info_map
,
3165 (splay_tree_key
) priority
);
3168 /* Create a new priority information structure, and insert it
3170 pi
= (priority_info
) xmalloc (sizeof (struct priority_info_s
));
3171 pi
->initializations_p
= 0;
3172 pi
->destructions_p
= 0;
3173 splay_tree_insert (priority_info_map
,
3174 (splay_tree_key
) priority
,
3175 (splay_tree_value
) pi
);
3178 pi
= (priority_info
) n
->value
;
3183 /* Set up to handle the initialization or destruction of DECL. If
3184 INITP is non-zero, we are initializing the variable. Otherwise, we
3185 are destroying it. */
3188 start_static_initialization_or_destruction (decl
, initp
)
3192 tree guard_if_stmt
= NULL_TREE
;
3199 /* Figure out the priority for this declaration. */
3200 priority
= DECL_INIT_PRIORITY (decl
);
3202 priority
= DEFAULT_INIT_PRIORITY
;
3204 /* Remember that we had an initialization or finalization at this
3206 pi
= get_priority_info (priority
);
3208 pi
->initializations_p
= 1;
3210 pi
->destructions_p
= 1;
3212 /* Trick the compiler into thinking we are at the file and line
3213 where DECL was declared so that error-messages make sense, and so
3214 that the debugger will show somewhat sensible file and line
3216 input_filename
= DECL_SOURCE_FILE (decl
);
3217 lineno
= DECL_SOURCE_LINE (decl
);
3223 Access control for implicit calls to the constructors,
3224 the conversion functions, or the destructor called to
3225 create and destroy a static data member is performed as
3226 if these calls appeared in the scope of the member's
3229 we pretend we are in a static member function of the class of
3230 which the DECL is a member. */
3231 if (member_p (decl
))
3233 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3234 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3237 /* Conditionalize this initialization on being in the right priority
3238 and being initializing/finalizing appropriately. */
3239 guard_if_stmt
= begin_if_stmt ();
3240 cond
= cp_build_binary_op (EQ_EXPR
,
3242 build_int_2 (priority
, 0));
3243 init_cond
= initp
? integer_one_node
: integer_zero_node
;
3244 init_cond
= cp_build_binary_op (EQ_EXPR
,
3247 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, init_cond
);
3249 /* Assume we don't need a guard. */
3251 /* We need a guard if this is an object with external linkage that
3252 might be initialized in more than one place. (For example, a
3253 static data member of a template, when the data member requires
3255 if (TREE_PUBLIC (decl
) && (DECL_COMMON (decl
)
3256 || DECL_ONE_ONLY (decl
)
3257 || DECL_WEAK (decl
)))
3261 guard
= get_guard (decl
);
3263 /* When using __cxa_atexit, we just check the GUARD as we would
3264 for a local static. */
3265 if (flag_use_cxa_atexit
)
3267 /* When using __cxa_atexit, we never try to destroy
3268 anything from a static destructor. */
3269 my_friendly_assert (initp
, 20000629);
3270 guard_cond
= get_guard_cond (guard
);
3272 /* Under the old ABI, e do initializations only if the GUARD is
3273 zero, i.e., if we are the first to initialize the variable.
3274 We do destructions only if the GUARD is one, i.e., if we are
3275 the last to destroy the variable. */
3278 = cp_build_binary_op (EQ_EXPR
,
3279 build_unary_op (PREINCREMENT_EXPR
,
3285 = cp_build_binary_op (EQ_EXPR
,
3286 build_unary_op (PREDECREMENT_EXPR
,
3291 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, guard_cond
);
3294 finish_if_stmt_cond (cond
, guard_if_stmt
);
3296 /* Under the new ABI, we have not already set the GUARD, so we must
3299 finish_expr_stmt (set_guard (guard
));
3301 return guard_if_stmt
;
3304 /* We've just finished generating code to do an initialization or
3305 finalization. GUARD_IF_STMT is the if-statement we used to guard
3306 the initialization. */
3309 finish_static_initialization_or_destruction (guard_if_stmt
)
3312 finish_then_clause (guard_if_stmt
);
3315 /* Now that we're done with DECL we don't need to pretend to be a
3316 member of its class any longer. */
3317 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3318 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3321 /* Generate code to do the static initialization of DECL. The
3322 initialization is INIT. If DECL may be initialized more than once
3323 in different object files, GUARD is the guard variable to
3324 check. PRIORITY is the priority for the initialization. */
3327 do_static_initialization (decl
, init
)
3334 /* Set up for the initialization. */
3336 = start_static_initialization_or_destruction (decl
,
3339 /* Do the initialization itself. */
3340 if (IS_AGGR_TYPE (TREE_TYPE (decl
))
3341 || TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
3342 expr
= build_aggr_init (decl
, init
, 0);
3345 expr
= build (INIT_EXPR
, TREE_TYPE (decl
), decl
, init
);
3346 TREE_SIDE_EFFECTS (expr
) = 1;
3348 finish_expr_stmt (expr
);
3350 /* If we're using __cxa_atexit, register a a function that calls the
3351 destructor for the object. */
3352 if (flag_use_cxa_atexit
)
3353 register_dtor_fn (decl
);
3356 finish_static_initialization_or_destruction (guard_if_stmt
);
3359 /* Generate code to do the static destruction of DECL. If DECL may be
3360 initialized more than once in different object files, GUARD is the
3361 guard variable to check. PRIORITY is the priority for the
3365 do_static_destruction (decl
)
3370 /* If we're using __cxa_atexit, then destructors are registered
3371 immediately after objects are initialized. */
3372 my_friendly_assert (!flag_use_cxa_atexit
, 20000121);
3374 /* If we don't need a destructor, there's nothing to do. */
3375 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3378 /* Actually do the destruction. */
3379 guard_if_stmt
= start_static_initialization_or_destruction (decl
,
3381 finish_expr_stmt (build_cleanup (decl
));
3382 finish_static_initialization_or_destruction (guard_if_stmt
);
3385 /* VARS is a list of variables with static storage duration which may
3386 need initialization and/or finalization. Remove those variables
3387 that don't really need to be initialized or finalized, and return
3388 the resulting list. The order in which the variables appear in
3389 VARS is in reverse order of the order in which they should actually
3390 be initialized. The list we return is in the unreversed order;
3391 i.e., the first variable should be initialized first. */
3394 prune_vars_needing_no_initialization (vars
)
3400 for (var
= vars
, result
= NULL_TREE
;
3402 var
= TREE_CHAIN (var
))
3404 tree decl
= TREE_VALUE (var
);
3405 tree init
= TREE_PURPOSE (var
);
3407 /* Deal gracefully with error. */
3408 if (decl
== error_mark_node
)
3411 /* The only things that can be initialized are variables. */
3412 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
, 19990420);
3414 /* If this object is not defined, we don't need to do anything
3416 if (DECL_EXTERNAL (decl
))
3419 /* Also, if the initializer already contains errors, we can bail
3421 if (init
&& TREE_CODE (init
) == TREE_LIST
3422 && value_member (error_mark_node
, init
))
3425 /* This variable is going to need initialization and/or
3426 finalization, so we add it to the list. */
3427 result
= tree_cons (init
, decl
, result
);
3433 /* Make sure we have told the back end about all the variables in
3437 write_out_vars (vars
)
3442 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3443 if (! TREE_ASM_WRITTEN (TREE_VALUE (v
)))
3444 rest_of_decl_compilation (TREE_VALUE (v
), 0, 1, 1);
3447 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3448 (otherwise) that will initialize all gobal objects with static
3449 storage duration having the indicated PRIORITY. */
3452 generate_ctor_or_dtor_function (constructor_p
, priority
)
3461 /* We use `I' to indicate initialization and `D' to indicate
3468 /* Begin the function. */
3469 body
= start_objects (function_key
, priority
);
3471 /* Call the static storage duration function with appropriate
3473 for (i
= 0; i
< ssdf_decls
->elements_used
; ++i
)
3475 arguments
= tree_cons (NULL_TREE
, build_int_2 (priority
, 0),
3477 arguments
= tree_cons (NULL_TREE
, build_int_2 (constructor_p
, 0),
3479 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls
, i
),
3483 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3484 calls to any functions marked with attributes indicating that
3485 they should be called at initialization- or destruction-time. */
3486 if (priority
== DEFAULT_INIT_PRIORITY
)
3490 for (fns
= constructor_p
? static_ctors
: static_dtors
;
3492 fns
= TREE_CHAIN (fns
))
3493 finish_expr_stmt (build_function_call (TREE_VALUE (fns
), NULL_TREE
));
3496 /* Close out the function. */
3497 finish_objects (function_key
, priority
, body
);
3500 /* Generate constructor and destructor functions for the priority
3504 generate_ctor_and_dtor_functions_for_priority (n
, data
)
3506 void *data ATTRIBUTE_UNUSED
;
3508 int priority
= (int) n
->key
;
3509 priority_info pi
= (priority_info
) n
->value
;
3511 /* Generate the functions themselves, but only if they are really
3513 if (pi
->initializations_p
3514 || (priority
== DEFAULT_INIT_PRIORITY
&& static_ctors
))
3515 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3517 if (pi
->destructions_p
3518 || (priority
== DEFAULT_INIT_PRIORITY
&& static_dtors
))
3519 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3522 /* Keep iterating. */
3526 /* This routine is called from the last rule in yyparse ().
3527 Its job is to create all the code needed to initialize and
3528 destroy the global aggregates. We do the destruction
3529 first, since that way we only need to reverse the decls once. */
3540 /* Bad parse errors. Just forget about it. */
3541 if (! global_bindings_p () || current_class_type
|| decl_namespace_list
)
3544 /* Otherwise, GDB can get confused, because in only knows
3545 about source for LINENO-1 lines. */
3548 interface_unknown
= 1;
3551 /* We now have to write out all the stuff we put off writing out.
3554 o Template specializations that we have not yet instantiated,
3555 but which are needed.
3556 o Initialization and destruction for non-local objects with
3557 static storage duration. (Local objects with static storage
3558 duration are initialized when their scope is first entered,
3559 and are cleaned up via atexit.)
3560 o Virtual function tables.
3562 All of these may cause others to be needed. For example,
3563 instantiating one function may cause another to be needed, and
3564 generating the intiailzer for an object may cause templates to be
3565 instantiated, etc., etc. */
3567 timevar_push (TV_VARCONST
);
3569 if (new_abi_rtti_p ())
3570 emit_support_tinfos ();
3576 /* If there are templates that we've put off instantiating, do
3578 instantiate_pending_templates ();
3580 /* Write out virtual tables as required. Note that writing out
3581 the virtual table for a template class may cause the
3582 instantiation of members of that class. */
3583 if (walk_globals (vtable_decl_p
,
3584 finish_vtable_vardecl
,
3588 /* Write out needed type info variables. Writing out one variable
3589 might cause others to be needed. */
3590 if (new_abi_rtti_p ()
3591 && walk_globals (tinfo_decl_p
, emit_tinfo_decl
, /*data=*/0))
3594 /* The list of objects with static storage duration is built up
3595 in reverse order. We clear STATIC_AGGREGATES so that any new
3596 aggregates added during the initialization of these will be
3597 initialized in the correct order when we next come around the
3599 vars
= prune_vars_needing_no_initialization (static_aggregates
);
3600 static_aggregates
= NULL_TREE
;
3606 /* We need to start a new initialization function each time
3607 through the loop. That's because we need to know which
3608 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3609 isn't computed until a function is finished, and written
3610 out. That's a deficiency in the back-end. When this is
3611 fixed, these initialization functions could all become
3612 inline, with resulting performance improvements. */
3613 tree ssdf_body
= start_static_storage_duration_function ();
3615 /* Make sure the back end knows about all the variables. */
3616 write_out_vars (vars
);
3618 /* First generate code to do all the initializations. */
3619 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3620 do_static_initialization (TREE_VALUE (v
),
3623 /* Then, generate code to do all the destructions. Do these
3624 in reverse order so that the most recently constructed
3625 variable is the first destroyed. If we're using
3626 __cxa_atexit, then we don't need to do this; functions
3627 were registered at initialization time to destroy the
3629 if (!flag_use_cxa_atexit
)
3631 vars
= nreverse (vars
);
3632 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3633 do_static_destruction (TREE_VALUE (v
));
3638 /* Finish up the static storage duration function for this
3640 finish_static_storage_duration_function (ssdf_body
);
3642 /* All those initializations and finalizations might cause
3643 us to need more inline functions, more template
3644 instantiations, etc. */
3648 /* Go through the various inline functions, and see if any need
3650 for (i
= 0; i
< deferred_fns_used
; ++i
)
3652 tree decl
= VARRAY_TREE (deferred_fns
, i
);
3653 import_export_decl (decl
);
3654 if (DECL_ARTIFICIAL (decl
) && ! DECL_INITIAL (decl
)
3656 && (! DECL_REALLY_EXTERN (decl
) || DECL_INLINE (decl
)))
3658 /* Even though we're already at the top-level, we push
3659 there again. That way, when we pop back a few lines
3660 hence, all of our state is restored. Otherwise,
3661 finish_function doesn't clean things up, and we end
3662 up with CURRENT_FUNCTION_DECL set. */
3663 push_to_top_level ();
3664 synthesize_method (decl
);
3665 pop_from_top_level ();
3670 /* Mark all functions that might deal with exception-handling as
3672 mark_all_runtime_matches ();
3674 /* We lie to the back-end, pretending that some functions are
3675 not defined when they really are. This keeps these functions
3676 from being put out unnecessarily. But, we must stop lying
3677 when the functions are referenced, or if they are not comdat
3678 since they need to be put out now. */
3679 for (i
= 0; i
< deferred_fns_used
; ++i
)
3681 tree decl
= VARRAY_TREE (deferred_fns
, i
);
3683 if (DECL_NOT_REALLY_EXTERN (decl
)
3684 && DECL_INITIAL (decl
)
3685 && DECL_NEEDED_P (decl
))
3686 DECL_EXTERNAL (decl
) = 0;
3688 /* If we're going to need to write this function out, and
3689 there's already a body for it, create RTL for it now.
3690 (There might be no body if this is a method we haven't
3691 gotten around to synthesizing yet.) */
3692 if (!DECL_EXTERNAL (decl
)
3693 && DECL_NEEDED_P (decl
)
3694 && DECL_SAVED_TREE (decl
)
3695 && !DECL_SAVED_INSNS (decl
)
3696 && !TREE_ASM_WRITTEN (decl
))
3698 int saved_not_really_extern
;
3700 /* When we call finish_function in expand_body, it will
3701 try to reset DECL_NOT_REALLY_EXTERN so we save and
3703 saved_not_really_extern
= DECL_NOT_REALLY_EXTERN (decl
);
3704 /* Generate RTL for this function now that we know we
3707 /* Undo the damage done by finish_function. */
3708 DECL_EXTERNAL (decl
) = 0;
3709 DECL_NOT_REALLY_EXTERN (decl
) = saved_not_really_extern
;
3710 /* If we're compiling -fsyntax-only pretend that this
3711 function has been written out so that we don't try to
3713 if (flag_syntax_only
)
3714 TREE_ASM_WRITTEN (decl
) = 1;
3719 if (deferred_fns_used
3720 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns
, 0),
3723 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
3726 /* Static data members are just like namespace-scope globals. */
3727 for (i
= 0; i
< pending_statics_used
; ++i
)
3729 tree decl
= VARRAY_TREE (pending_statics
, i
);
3730 if (TREE_ASM_WRITTEN (decl
))
3732 import_export_decl (decl
);
3733 if (DECL_NOT_REALLY_EXTERN (decl
) && ! DECL_IN_AGGR_P (decl
))
3734 DECL_EXTERNAL (decl
) = 0;
3737 && wrapup_global_declarations (&VARRAY_TREE (pending_statics
, 0),
3738 pending_statics_used
))
3743 /* We give C linkage to static constructors and destructors. */
3744 push_lang_context (lang_name_c
);
3746 /* Generate initialization and destruction functions for all
3747 priorities for which they are required. */
3748 if (priority_info_map
)
3749 splay_tree_foreach (priority_info_map
,
3750 generate_ctor_and_dtor_functions_for_priority
,
3753 /* We're done with the splay-tree now. */
3754 if (priority_info_map
)
3755 splay_tree_delete (priority_info_map
);
3757 /* We're done with static constructors, so we can go back to "C++"
3759 pop_lang_context ();
3761 /* Now delete from the chain of variables all virtual function tables.
3762 We output them all ourselves, because each will be treated
3763 specially. We don't do this if we're just doing semantic
3764 analysis, and not code-generation. */
3765 if (!flag_syntax_only
)
3766 walk_globals (vtable_decl_p
, prune_vtable_vardecl
, /*data=*/0);
3768 /* Now, issue warnings about static, but not defined, functions,
3769 etc., and emit debugging information. */
3770 walk_namespaces (wrapup_globals_for_namespace
, /*data=*/&reconsider
);
3771 if (pending_statics
)
3772 check_global_declarations (&VARRAY_TREE (pending_statics
, 0),
3773 pending_statics_used
);
3777 /* The entire file is now complete. If requested, dump everything
3779 if (flag_dump_translation_unit
)
3780 dump_node_to_file (global_namespace
, flag_dump_translation_unit
);
3782 /* If there's some tool that wants to examine the entire translation
3783 unit, let it do so now. */
3785 (*back_end_hook
) (global_namespace
);
3787 timevar_pop (TV_VARCONST
);
3789 if (flag_detailed_statistics
)
3791 dump_tree_statistics ();
3792 dump_time_statistics ();
3796 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3797 expr. Since it was parsed like a type, we need to wade through and fix
3798 that. Unfortunately, since operator() is left-associative, we can't use
3799 tail recursion. In the above example, TYPE is `A', and DECL is
3802 Maybe this shouldn't be recursive, but how often will it actually be
3806 reparse_absdcl_as_expr (type
, decl
)
3809 /* do build_functional_cast (type, NULL_TREE) at bottom */
3810 if (TREE_OPERAND (decl
, 0) == NULL_TREE
)
3811 return build_functional_cast (type
, NULL_TREE
);
3814 decl
= reparse_absdcl_as_expr (type
, TREE_OPERAND (decl
, 0));
3816 decl
= build_x_function_call (decl
, NULL_TREE
, current_class_ref
);
3818 if (TREE_CODE (decl
) == CALL_EXPR
3819 && (! TREE_TYPE (decl
)
3820 || TREE_CODE (TREE_TYPE (decl
)) != VOID_TYPE
))
3821 decl
= require_complete_type (decl
);
3826 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3827 out to be an expr. Since it was parsed like a type, we need to wade
3828 through and fix that. Since casts are right-associative, we are
3829 reversing the order, so we don't have to recurse.
3831 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3835 reparse_absdcl_as_casts (decl
, expr
)
3840 if (TREE_CODE (expr
) == CONSTRUCTOR
3841 && TREE_TYPE (expr
) == 0)
3843 type
= groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl
)));
3844 decl
= TREE_OPERAND (decl
, 0);
3846 expr
= digest_init (type
, expr
, (tree
*) 0);
3847 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
3849 int failure
= complete_array_type (type
, expr
, 1);
3851 my_friendly_abort (78);
3857 type
= groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl
)));
3858 decl
= TREE_OPERAND (decl
, 0);
3859 expr
= build_c_cast (type
, expr
);
3862 if (warn_old_style_cast
&& ! in_system_header
3863 && current_lang_name
!= lang_name_c
)
3864 warning ("use of old-style cast");
3869 /* Given plain tree nodes for an expression, build up the full semantics. */
3872 build_expr_from_tree (t
)
3875 if (t
== NULL_TREE
|| t
== error_mark_node
)
3878 switch (TREE_CODE (t
))
3880 case IDENTIFIER_NODE
:
3881 return do_identifier (t
, 0, NULL_TREE
);
3884 if (LOOKUP_EXPR_GLOBAL (t
))
3885 return do_scoped_id (TREE_OPERAND (t
, 0), 0);
3887 return do_identifier (TREE_OPERAND (t
, 0), 0, NULL_TREE
);
3889 case TEMPLATE_ID_EXPR
:
3890 return (lookup_template_function
3891 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3892 build_expr_from_tree (TREE_OPERAND (t
, 1))));
3895 return build_x_indirect_ref
3896 (build_expr_from_tree (TREE_OPERAND (t
, 0)), "unary *");
3899 return build_functional_cast
3900 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3902 case REINTERPRET_CAST_EXPR
:
3903 return build_reinterpret_cast
3904 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3906 case CONST_CAST_EXPR
:
3907 return build_const_cast
3908 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3910 case DYNAMIC_CAST_EXPR
:
3911 return build_dynamic_cast
3912 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3914 case STATIC_CAST_EXPR
:
3915 return build_static_cast
3916 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3918 case PREDECREMENT_EXPR
:
3919 case PREINCREMENT_EXPR
:
3920 case POSTDECREMENT_EXPR
:
3921 case POSTINCREMENT_EXPR
:
3925 case TRUTH_NOT_EXPR
:
3927 case CONVERT_EXPR
: /* Unary + */
3932 return build_x_unary_op (TREE_CODE (t
),
3933 build_expr_from_tree (TREE_OPERAND (t
, 0)));
3938 case TRUNC_DIV_EXPR
:
3940 case FLOOR_DIV_EXPR
:
3941 case ROUND_DIV_EXPR
:
3942 case EXACT_DIV_EXPR
:
3944 case BIT_ANDTC_EXPR
:
3947 case TRUNC_MOD_EXPR
:
3948 case FLOOR_MOD_EXPR
:
3949 case TRUTH_ANDIF_EXPR
:
3950 case TRUTH_ORIF_EXPR
:
3951 case TRUTH_AND_EXPR
:
3966 return build_x_binary_op
3968 build_expr_from_tree (TREE_OPERAND (t
, 0)),
3969 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3972 return build_m_component_ref
3973 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3974 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3977 return build_offset_ref (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1));
3980 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
3982 return build_nt (ARRAY_REF
, NULL_TREE
,
3983 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3984 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3985 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3990 tree r
= build_expr_from_tree (TREE_OPERAND (t
, 0));
3993 return TREE_CODE (t
) == SIZEOF_EXPR
? c_sizeof (r
) : c_alignof (r
);
3997 return build_x_modify_expr
3998 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3999 TREE_CODE (TREE_OPERAND (t
, 1)),
4000 build_expr_from_tree (TREE_OPERAND (t
, 2)));
4003 return build_x_arrow
4004 (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4008 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4009 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4010 build_expr_from_tree (TREE_OPERAND (t
, 2)),
4011 NEW_EXPR_USE_GLOBAL (t
));
4014 return delete_sanity
4015 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4016 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4017 DELETE_EXPR_USE_VEC (t
), DELETE_EXPR_USE_GLOBAL (t
));
4020 if (TREE_OPERAND (t
, 1) == NULL_TREE
)
4021 return build_x_compound_expr
4022 (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4024 my_friendly_abort (42);
4026 case METHOD_CALL_EXPR
:
4027 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SCOPE_REF
)
4029 tree ref
= TREE_OPERAND (t
, 0);
4030 tree name
= TREE_OPERAND (ref
, 1);
4032 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4033 name
= build_nt (TEMPLATE_ID_EXPR
,
4034 TREE_OPERAND (name
, 0),
4035 build_expr_from_tree (TREE_OPERAND (name
, 1)));
4037 return build_scoped_method_call
4038 (build_expr_from_tree (TREE_OPERAND (t
, 1)),
4039 build_expr_from_tree (TREE_OPERAND (ref
, 0)),
4041 build_expr_from_tree (TREE_OPERAND (t
, 2)));
4045 tree fn
= TREE_OPERAND (t
, 0);
4047 /* We can get a TEMPLATE_ID_EXPR here on code like:
4051 so we must resolve that. However, we can also get things
4052 like a BIT_NOT_EXPR here, when referring to a destructor,
4053 and things like that are not correctly resolved by
4054 build_expr_from_tree. So, just use build_expr_from_tree
4055 when we really need it. */
4056 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4057 fn
= lookup_template_function
4058 (TREE_OPERAND (fn
, 0),
4059 build_expr_from_tree (TREE_OPERAND (fn
, 1)));
4061 return build_method_call
4062 (build_expr_from_tree (TREE_OPERAND (t
, 1)),
4064 build_expr_from_tree (TREE_OPERAND (t
, 2)),
4065 NULL_TREE
, LOOKUP_NORMAL
);
4069 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SCOPE_REF
)
4071 tree ref
= TREE_OPERAND (t
, 0);
4072 tree name
= TREE_OPERAND (ref
, 1);
4074 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4075 name
= build_nt (TEMPLATE_ID_EXPR
,
4076 TREE_OPERAND (name
, 0),
4077 build_expr_from_tree (TREE_OPERAND (name
, 1)));
4079 return build_member_call
4080 (build_expr_from_tree (TREE_OPERAND (ref
, 0)),
4082 build_expr_from_tree (TREE_OPERAND (t
, 1)));
4086 tree name
= TREE_OPERAND (t
, 0);
4088 tree args
= build_expr_from_tree (TREE_OPERAND (t
, 1));
4089 if (args
!= NULL_TREE
&& TREE_CODE (name
) == LOOKUP_EXPR
4090 && !LOOKUP_EXPR_GLOBAL (name
)
4091 && TREE_CODE ((id
= TREE_OPERAND (name
, 0))) == IDENTIFIER_NODE
4092 && (!current_class_type
4093 || !lookup_member (current_class_type
, id
, 0, 0)))
4095 /* Do Koenig lookup if there are no class members. */
4096 name
= do_identifier (id
, 0, args
);
4098 else if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
4099 || ! really_overloaded_fn (name
))
4100 name
= build_expr_from_tree (name
);
4101 return build_x_function_call (name
, args
, current_class_ref
);
4105 return build_x_conditional_expr
4106 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4107 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4108 build_expr_from_tree (TREE_OPERAND (t
, 2)));
4110 case PSEUDO_DTOR_EXPR
:
4111 return (finish_pseudo_destructor_call_expr
4112 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4113 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4114 build_expr_from_tree (TREE_OPERAND (t
, 2))));
4118 tree purpose
, value
, chain
;
4120 if (t
== void_list_node
)
4123 purpose
= TREE_PURPOSE (t
);
4125 purpose
= build_expr_from_tree (purpose
);
4126 value
= TREE_VALUE (t
);
4128 value
= build_expr_from_tree (value
);
4129 chain
= TREE_CHAIN (t
);
4130 if (chain
&& chain
!= void_type_node
)
4131 chain
= build_expr_from_tree (chain
);
4132 return tree_cons (purpose
, value
, chain
);
4137 tree object
= build_expr_from_tree (TREE_OPERAND (t
, 0));
4138 tree field
= TREE_OPERAND (t
, 1);
4140 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4141 and `x.A::b'. We must distinguish between those cases
4143 if (TREE_CODE (field
) == SCOPE_REF
)
4144 return build_object_ref (object
,
4145 TREE_OPERAND (field
, 0),
4146 TREE_OPERAND (field
, 1));
4148 return build_x_component_ref (object
, field
,
4153 return build_throw (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4159 /* digest_init will do the wrong thing if we let it. */
4160 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4163 r
= build_nt (CONSTRUCTOR
, NULL_TREE
,
4164 build_expr_from_tree (CONSTRUCTOR_ELTS (t
)));
4165 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
4168 return digest_init (TREE_TYPE (t
), r
, 0);
4173 if (TYPE_P (TREE_OPERAND (t
, 0)))
4174 return get_typeid (TREE_OPERAND (t
, 0));
4175 return build_typeid (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4178 return convert_from_reference (t
);
4181 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4189 /* This is something of the form `int (*a)++' that has turned out to be an
4190 expr. It was only converted into parse nodes, so we need to go through
4191 and build up the semantics. Most of the work is done by
4192 build_expr_from_tree, above.
4194 In the above example, TYPE is `int' and DECL is `*a'. */
4197 reparse_decl_as_expr (type
, decl
)
4200 decl
= build_expr_from_tree (decl
);
4202 return build_functional_cast (type
, build_tree_list (NULL_TREE
, decl
));
4207 /* This is something of the form `int (*a)' that has turned out to be a
4208 decl. It was only converted into parse nodes, so we need to do the
4209 checking that make_{pointer,reference}_declarator do. */
4212 finish_decl_parsing (decl
)
4215 extern int current_class_depth
;
4217 switch (TREE_CODE (decl
))
4219 case IDENTIFIER_NODE
:
4222 return make_pointer_declarator
4223 (NULL_TREE
, finish_decl_parsing (TREE_OPERAND (decl
, 0)));
4225 return make_reference_declarator
4226 (NULL_TREE
, finish_decl_parsing (TREE_OPERAND (decl
, 0)));
4228 TREE_OPERAND (decl
, 0) = finish_decl_parsing (TREE_OPERAND (decl
, 0));
4231 push_nested_class (TREE_TYPE (TREE_OPERAND (decl
, 0)), 3);
4232 TREE_COMPLEXITY (decl
) = current_class_depth
;
4235 TREE_OPERAND (decl
, 0) = finish_decl_parsing (TREE_OPERAND (decl
, 0));
4238 /* For attribute handling. */
4239 TREE_VALUE (decl
) = finish_decl_parsing (TREE_VALUE (decl
));
4241 case TEMPLATE_ID_EXPR
:
4244 my_friendly_abort (5);
4249 /* Return 1 if root encloses child. */
4252 is_namespace_ancestor (root
, child
)
4257 if (root
== global_namespace
)
4259 if (child
== global_namespace
)
4261 return is_namespace_ancestor (root
, CP_DECL_CONTEXT (child
));
4265 /* Return the namespace that is the common ancestor
4266 of two given namespaces. */
4269 namespace_ancestor (ns1
, ns2
)
4272 if (is_namespace_ancestor (ns1
, ns2
))
4274 return namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
);
4277 /* Insert used into the using list of user. Set indirect_flag if this
4278 directive is not directly from the source. Also find the common
4279 ancestor and let our users know about the new namespace */
4281 add_using_namespace (user
, used
, indirect
)
4287 /* Using oneself is a no-op. */
4290 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
4291 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
4292 /* Check if we already have this. */
4293 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
4297 /* Promote to direct usage. */
4298 TREE_INDIRECT_USING (t
) = 0;
4302 /* Add used to the user's using list. */
4303 DECL_NAMESPACE_USING (user
)
4304 = tree_cons (used
, namespace_ancestor (user
, used
),
4305 DECL_NAMESPACE_USING (user
));
4307 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
4309 /* Add user to the used's users list. */
4310 DECL_NAMESPACE_USERS (used
)
4311 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
4313 /* Recursively add all namespaces used. */
4314 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
4315 /* indirect usage */
4316 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
4318 /* Tell everyone using us about the new used namespaces. */
4319 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
4320 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
4323 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4324 duplicates. The first list becomes the tail of the result.
4326 The algorithm is O(n^2). We could get this down to O(n log n) by
4327 doing a sort on the addresses of the functions, if that becomes
4331 merge_functions (s1
, s2
)
4335 for (; s2
; s2
= OVL_NEXT (s2
))
4337 tree fn
= OVL_CURRENT (s2
);
4338 if (! ovl_member (fn
, s1
))
4339 s1
= build_overload (fn
, s1
);
4344 /* This should return an error not all definitions define functions.
4345 It is not an error if we find two functions with exactly the
4346 same signature, only if these are selected in overload resolution.
4347 old is the current set of bindings, new the freshly-found binding.
4348 XXX Do we want to give *all* candidates in case of ambiguity?
4349 XXX In what way should I treat extern declarations?
4350 XXX I don't want to repeat the entire duplicate_decls here */
4353 ambiguous_decl (name
, old
, new, flags
)
4360 my_friendly_assert (old
!= NULL_TREE
, 393);
4361 /* Copy the value. */
4362 val
= BINDING_VALUE (new);
4364 switch (TREE_CODE (val
))
4367 /* If we expect types or namespaces, and not templates,
4368 or this is not a template class. */
4369 if (LOOKUP_QUALIFIERS_ONLY (flags
)
4370 && !DECL_CLASS_TEMPLATE_P (val
))
4374 if (LOOKUP_NAMESPACES_ONLY (flags
))
4377 case NAMESPACE_DECL
:
4378 if (LOOKUP_TYPES_ONLY (flags
))
4382 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4386 if (!BINDING_VALUE (old
))
4387 BINDING_VALUE (old
) = val
;
4388 else if (val
&& val
!= BINDING_VALUE (old
))
4390 if (is_overloaded_fn (BINDING_VALUE (old
))
4391 && is_overloaded_fn (val
))
4393 BINDING_VALUE (old
) = merge_functions (BINDING_VALUE (old
),
4398 /* Some declarations are functions, some are not. */
4399 if (flags
& LOOKUP_COMPLAIN
)
4401 /* If we've already given this error for this lookup,
4402 BINDING_VALUE (old) is error_mark_node, so let's not
4403 repeat ourselves. */
4404 if (BINDING_VALUE (old
) != error_mark_node
)
4406 cp_error ("use of `%D' is ambiguous", name
);
4407 cp_error_at (" first declared as `%#D' here",
4408 BINDING_VALUE (old
));
4410 cp_error_at (" also declared as `%#D' here", val
);
4412 BINDING_VALUE (old
) = error_mark_node
;
4415 /* ... and copy the type. */
4416 type
= BINDING_TYPE (new);
4417 if (LOOKUP_NAMESPACES_ONLY (flags
))
4419 if (!BINDING_TYPE (old
))
4420 BINDING_TYPE (old
) = type
;
4421 else if (type
&& BINDING_TYPE (old
) != type
)
4423 if (flags
& LOOKUP_COMPLAIN
)
4425 cp_error ("`%D' denotes an ambiguous type",name
);
4426 cp_error_at (" first type here", BINDING_TYPE (old
));
4427 cp_error_at (" other type here", type
);
4433 /* Subroutine of unualified_namespace_lookup:
4434 Add the bindings of NAME in used namespaces to VAL.
4435 We are currently looking for names in namespace SCOPE, so we
4436 look through USINGS for using-directives of namespaces
4437 which have SCOPE as a common ancestor with the current scope.
4438 Returns zero on errors. */
4441 lookup_using_namespace (name
, val
, usings
, scope
, flags
, spacesp
)
4442 tree name
, val
, usings
, scope
;
4448 /* Iterate over all used namespaces in current, searching for using
4449 directives of scope. */
4450 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
4451 if (TREE_VALUE (iter
) == scope
)
4454 *spacesp
= tree_cons (TREE_PURPOSE (iter
), NULL_TREE
,
4456 val1
= binding_for_name (name
, TREE_PURPOSE (iter
));
4457 /* Resolve ambiguities. */
4458 val
= ambiguous_decl (name
, val
, val1
, flags
);
4460 return BINDING_VALUE (val
) != error_mark_node
;
4464 Accepts the NAME to lookup and its qualifying SCOPE.
4465 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4469 qualified_lookup_using_namespace (name
, scope
, result
, flags
)
4475 /* Maintain a list of namespaces visited... */
4476 tree seen
= NULL_TREE
;
4477 /* ... and a list of namespace yet to see. */
4478 tree todo
= NULL_TREE
;
4480 /* Look through namespace aliases. */
4481 scope
= ORIGINAL_NAMESPACE (scope
);
4482 while (scope
&& (result
!= error_mark_node
))
4484 seen
= tree_cons (scope
, NULL_TREE
, seen
);
4485 result
= ambiguous_decl (name
, result
,
4486 binding_for_name (name
, scope
), flags
);
4487 if (!BINDING_VALUE (result
) && !BINDING_TYPE (result
))
4488 /* Consider using directives. */
4489 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
4490 usings
= TREE_CHAIN (usings
))
4491 /* If this was a real directive, and we have not seen it. */
4492 if (!TREE_INDIRECT_USING (usings
)
4493 && !purpose_member (TREE_PURPOSE (usings
), seen
))
4494 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
4497 scope
= TREE_PURPOSE (todo
);
4498 todo
= TREE_CHAIN (todo
);
4501 scope
= NULL_TREE
; /* If there never was a todo list. */
4503 return result
!= error_mark_node
;
4506 /* [namespace.memdef]/2 */
4508 /* Set the context of a declaration to scope. Complain if we are not
4512 set_decl_namespace (decl
, scope
, friendp
)
4518 if (scope
== fake_std_node
)
4519 scope
= global_namespace
;
4520 /* Get rid of namespace aliases. */
4521 scope
= ORIGINAL_NAMESPACE (scope
);
4523 /* It is ok for friends to be qualified in parallel space. */
4524 if (!friendp
&& !is_namespace_ancestor (current_namespace
, scope
))
4525 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4527 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4528 if (scope
!= current_namespace
)
4530 /* See whether this has been declared in the namespace. */
4531 old
= namespace_binding (DECL_NAME (decl
), scope
);
4533 /* No old declaration at all. */
4535 if (!is_overloaded_fn (decl
))
4536 /* Don't compare non-function decls with decls_match here,
4537 since it can't check for the correct constness at this
4538 point. pushdecl will find those errors later. */
4540 /* Since decl is a function, old should contain a function decl. */
4541 if (!is_overloaded_fn (old
))
4543 if (processing_template_decl
|| processing_specialization
)
4544 /* We have not yet called push_template_decl to turn the
4545 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4546 won't match. But, we'll check later, when we construct the
4549 for (; old
; old
= OVL_NEXT (old
))
4550 if (decls_match (decl
, OVL_CURRENT (old
)))
4556 cp_error ("`%D' should have been declared inside `%D'",
4560 /* Compute the namespace where a declaration is defined. */
4563 decl_namespace (decl
)
4567 decl
= TYPE_STUB_DECL (decl
);
4568 while (DECL_CONTEXT (decl
))
4570 decl
= DECL_CONTEXT (decl
);
4571 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
4574 decl
= TYPE_STUB_DECL (decl
);
4575 my_friendly_assert (DECL_P (decl
), 390);
4578 return global_namespace
;
4581 /* Return the namespace where the current declaration is declared. */
4584 current_decl_namespace ()
4587 /* If we have been pushed into a different namespace, use it. */
4588 if (decl_namespace_list
)
4589 return TREE_PURPOSE (decl_namespace_list
);
4591 if (current_class_type
)
4592 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
4593 else if (current_function_decl
)
4594 result
= decl_namespace (current_function_decl
);
4596 result
= current_namespace
;
4600 /* Temporarily set the namespace for the current declaration. */
4603 push_decl_namespace (decl
)
4606 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
4607 decl
= decl_namespace (decl
);
4608 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
4609 NULL_TREE
, decl_namespace_list
);
4613 pop_decl_namespace ()
4615 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
4618 /* Enter a class or namespace scope. */
4624 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4625 push_decl_namespace (t
);
4630 /* Leave scope pushed by push_scope. */
4636 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4637 pop_decl_namespace ();
4642 /* [basic.lookup.koenig] */
4643 /* A non-zero return value in the functions below indicates an error.
4644 All nodes allocated in the procedure are on the scratch obstack. */
4654 static int arg_assoc
PARAMS ((struct arg_lookup
*, tree
));
4655 static int arg_assoc_args
PARAMS ((struct arg_lookup
*, tree
));
4656 static int arg_assoc_type
PARAMS ((struct arg_lookup
*, tree
));
4657 static int add_function
PARAMS ((struct arg_lookup
*, tree
));
4658 static int arg_assoc_namespace
PARAMS ((struct arg_lookup
*, tree
));
4659 static int arg_assoc_class
PARAMS ((struct arg_lookup
*, tree
));
4660 static int arg_assoc_template_arg
PARAMS ((struct arg_lookup
*, tree
));
4662 /* Add a function to the lookup structure.
4663 Returns 1 on error. */
4666 add_function (k
, fn
)
4667 struct arg_lookup
*k
;
4670 /* We used to check here to see if the function was already in the list,
4671 but that's O(n^2), which is just too expensive for function lookup.
4672 Now we deal with the occasional duplicate in joust. In doing this, we
4673 assume that the number of duplicates will be small compared to the
4674 total number of functions being compared, which should usually be the
4677 /* We must find only functions, or exactly one non-function. */
4680 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4681 k
->functions
= build_overload (fn
, k
->functions
);
4684 tree f1
= OVL_CURRENT (k
->functions
);
4686 if (is_overloaded_fn (f1
))
4688 fn
= f1
; f1
= f2
; f2
= fn
;
4690 cp_error_at ("`%D' is not a function,", f1
);
4691 cp_error_at (" conflict with `%D'", f2
);
4692 cp_error (" in call to `%D'", k
->name
);
4699 /* Add functions of a namespace to the lookup structure.
4700 Returns 1 on error. */
4703 arg_assoc_namespace (k
, scope
)
4704 struct arg_lookup
*k
;
4709 if (purpose_member (scope
, k
->namespaces
))
4711 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
4713 value
= namespace_binding (k
->name
, scope
);
4717 for (; value
; value
= OVL_NEXT (value
))
4718 if (add_function (k
, OVL_CURRENT (value
)))
4724 /* Adds everything associated with a template argument to the lookup
4725 structure. Returns 1 on error. */
4728 arg_assoc_template_arg (k
, arg
)
4729 struct arg_lookup
* k
;
4732 /* [basic.lookup.koenig]
4734 If T is a template-id, its associated namespaces and classes are
4735 ... the namespaces and classes associated with the types of the
4736 template arguments provided for template type parameters
4737 (excluding template template parameters); the namespaces in which
4738 any template template arguments are defined; and the classes in
4739 which any member templates used as template template arguments
4740 are defined. [Note: non-type template arguments do not
4741 contribute to the set of associated namespaces. ] */
4743 /* Consider first template template arguments. */
4744 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
4746 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4748 tree ctx
= CP_DECL_CONTEXT (arg
);
4750 /* It's not a member template. */
4751 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4752 return arg_assoc_namespace (k
, ctx
);
4753 /* Otherwise, it must be member template. */
4755 return arg_assoc_class (k
, ctx
);
4757 /* It's not a template template argument, but it is a type template
4759 else if (TYPE_P (arg
))
4760 return arg_assoc_type (k
, arg
);
4761 /* It's a non-type template argument. */
4766 /* Adds everything associated with class to the lookup structure.
4767 Returns 1 on error. */
4770 arg_assoc_class (k
, type
)
4771 struct arg_lookup
* k
;
4774 tree list
, friends
, context
;
4777 /* Backend build structures, such as __builtin_va_list, aren't
4778 affected by all this. */
4779 if (!CLASS_TYPE_P (type
))
4782 if (purpose_member (type
, k
->classes
))
4784 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4786 context
= decl_namespace (TYPE_MAIN_DECL (type
));
4787 if (arg_assoc_namespace (k
, context
))
4790 /* Process baseclasses. */
4791 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4792 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
4795 /* Process friends. */
4796 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
4797 list
= TREE_CHAIN (list
))
4798 if (k
->name
== TREE_PURPOSE (list
))
4799 for (friends
= TREE_VALUE (list
); friends
;
4800 friends
= TREE_CHAIN (friends
))
4801 /* Only interested in global functions with potentially hidden
4802 (i.e. unqualified) declarations. */
4803 if (TREE_PURPOSE (friends
) == error_mark_node
&& TREE_VALUE (friends
)
4804 && decl_namespace (TREE_VALUE (friends
)) == context
)
4805 if (add_function (k
, TREE_VALUE (friends
)))
4808 /* Process template arguments. */
4809 if (CLASSTYPE_TEMPLATE_INFO (type
))
4811 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4812 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4813 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4819 /* Adds everything associated with a given type.
4820 Returns 1 on error. */
4823 arg_assoc_type (k
, type
)
4824 struct arg_lookup
*k
;
4827 switch (TREE_CODE (type
))
4838 if (TYPE_PTRMEMFUNC_P (type
))
4839 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4840 return arg_assoc_class (k
, type
);
4842 case REFERENCE_TYPE
:
4844 return arg_assoc_type (k
, TREE_TYPE (type
));
4847 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
4849 /* Pointer to member: associate class type and value type. */
4850 if (arg_assoc_type (k
, TYPE_OFFSET_BASETYPE (type
)))
4852 return arg_assoc_type (k
, TREE_TYPE (type
));
4854 /* The basetype is referenced in the first arg type, so just
4857 /* Associate the parameter types. */
4858 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4860 /* Associate the return type. */
4861 return arg_assoc_type (k
, TREE_TYPE (type
));
4862 case TEMPLATE_TYPE_PARM
:
4863 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4868 if (type
== unknown_type_node
)
4870 /* else fall through */
4872 my_friendly_abort (390);
4877 /* Adds everything associated with arguments. Returns 1 on error. */
4880 arg_assoc_args (k
, args
)
4881 struct arg_lookup
* k
;
4884 for (; args
; args
= TREE_CHAIN (args
))
4885 if (arg_assoc (k
, TREE_VALUE (args
)))
4890 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4894 struct arg_lookup
* k
;
4897 if (n
== error_mark_node
)
4901 return arg_assoc_type (k
, n
);
4903 if (! type_unknown_p (n
))
4904 return arg_assoc_type (k
, TREE_TYPE (n
));
4906 if (TREE_CODE (n
) == ADDR_EXPR
)
4907 n
= TREE_OPERAND (n
, 0);
4908 if (TREE_CODE (n
) == COMPONENT_REF
)
4909 n
= TREE_OPERAND (n
, 1);
4910 if (TREE_CODE (n
) == OFFSET_REF
)
4911 n
= TREE_OPERAND (n
, 1);
4912 while (TREE_CODE (n
) == TREE_LIST
)
4915 if (TREE_CODE (n
) == FUNCTION_DECL
)
4916 return arg_assoc_type (k
, TREE_TYPE (n
));
4917 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4919 /* [basic.lookup.koenig]
4921 If T is a template-id, its associated namespaces and classes
4922 are the namespace in which the template is defined; for
4923 member templates, the member template's class... */
4924 tree
template = TREE_OPERAND (n
, 0);
4925 tree args
= TREE_OPERAND (n
, 1);
4929 if (TREE_CODE (template) == COMPONENT_REF
)
4930 template = TREE_OPERAND (template, 1);
4932 /* First, the template. There may actually be more than one if
4933 this is an overloaded function template. But, in that case,
4934 we only need the first; all the functions will be in the same
4936 template = OVL_CURRENT (template);
4938 ctx
= CP_DECL_CONTEXT (template);
4940 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4942 if (arg_assoc_namespace (k
, ctx
) == 1)
4945 /* It must be a member template. */
4946 else if (arg_assoc_class (k
, ctx
) == 1)
4949 /* Now the arguments. */
4950 for (arg
= args
; arg
!= NULL_TREE
; arg
= TREE_CHAIN (arg
))
4951 if (arg_assoc_template_arg (k
, TREE_VALUE (arg
)) == 1)
4956 my_friendly_assert (TREE_CODE (n
) == OVERLOAD
, 980715);
4958 for (; n
; n
= OVL_CHAIN (n
))
4959 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4966 /* Performs Koenig lookup depending on arguments, where fns
4967 are the functions found in normal lookup. */
4970 lookup_arg_dependent (name
, fns
, args
)
4975 struct arg_lookup k
;
4976 tree fn
= NULL_TREE
;
4980 k
.classes
= NULL_TREE
;
4982 /* Note that we've already looked at some namespaces during normal
4983 unqualified lookup, unless we found a decl in function scope. */
4985 fn
= OVL_CURRENT (fns
);
4986 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (fn
))
4987 k
.namespaces
= NULL_TREE
;
4989 unqualified_namespace_lookup (name
, 0, &k
.namespaces
);
4991 arg_assoc_args (&k
, args
);
4995 /* Process a namespace-alias declaration. */
4998 do_namespace_alias (alias
, namespace)
4999 tree alias
, namespace;
5001 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
5003 /* The parser did not find it, so it's not there. */
5004 cp_error ("unknown namespace `%D'", namespace);
5008 namespace = ORIGINAL_NAMESPACE (namespace);
5010 /* Build the alias. */
5011 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5012 DECL_NAMESPACE_ALIAS (alias
) = namespace;
5016 /* Check a non-member using-declaration. Return the name and scope
5017 being used, and the USING_DECL, or NULL_TREE on failure. */
5020 validate_nonmember_using_decl (decl
, scope
, name
)
5025 if (TREE_CODE (decl
) == SCOPE_REF
5026 && TREE_OPERAND (decl
, 0) == fake_std_node
)
5028 if (namespace_bindings_p ()
5029 && current_namespace
== global_namespace
)
5030 /* There's no need for a using declaration at all, here,
5031 since `std' is the same as `::'. We can't just pass this
5032 on because we'll complain later about declaring something
5033 in the same scope as a using declaration with the same
5034 name. We return NULL_TREE which indicates to the caller
5035 that there's no need to do any further processing. */
5038 *scope
= global_namespace
;
5039 *name
= TREE_OPERAND (decl
, 1);
5041 else if (TREE_CODE (decl
) == SCOPE_REF
)
5043 *scope
= TREE_OPERAND (decl
, 0);
5044 *name
= TREE_OPERAND (decl
, 1);
5046 /* [namespace.udecl]
5048 A using-declaration for a class member shall be a
5049 member-declaration. */
5050 if (TREE_CODE (*scope
) != NAMESPACE_DECL
)
5052 if (TYPE_P (*scope
))
5053 cp_error ("`%T' is not a namespace", *scope
);
5055 cp_error ("`%D' is not a namespace", *scope
);
5059 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
5060 || TREE_CODE (decl
) == TYPE_DECL
5061 || TREE_CODE (decl
) == TEMPLATE_DECL
)
5063 *scope
= global_namespace
;
5067 my_friendly_abort (382);
5069 *name
= DECL_NAME (*name
);
5070 /* Make a USING_DECL. */
5071 return push_using_decl (*scope
, *name
);
5074 /* Process local and global using-declarations. */
5077 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, newval
, newtype
)
5079 tree oldval
, oldtype
;
5080 tree
*newval
, *newtype
;
5084 *newval
= *newtype
= NULL_TREE
;
5085 decls
= make_node (CPLUS_BINDING
);
5086 if (!qualified_lookup_using_namespace (name
, scope
, decls
, 0))
5090 if (!BINDING_VALUE (decls
) && !BINDING_TYPE (decls
))
5092 cp_error ("`%D' not declared", name
);
5096 /* Check for using functions. */
5097 if (BINDING_VALUE (decls
) && is_overloaded_fn (BINDING_VALUE (decls
)))
5101 if (oldval
&& !is_overloaded_fn (oldval
))
5103 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls
)), oldval
);
5108 for (tmp
= BINDING_VALUE (decls
); tmp
; tmp
= OVL_NEXT (tmp
))
5110 tree new_fn
= OVL_CURRENT (tmp
);
5112 /* [namespace.udecl]
5114 If a function declaration in namespace scope or block
5115 scope has the same name and the same parameter types as a
5116 function introduced by a using declaration the program is
5118 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
5120 tree old_fn
= OVL_CURRENT (tmp1
);
5122 if (!OVL_USED (tmp1
)
5123 && compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
5124 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
5126 if (!(DECL_EXTERN_C_P (new_fn
)
5127 && DECL_EXTERN_C_P (old_fn
)))
5128 /* There was already a non-using declaration in
5129 this scope with the same parameter types. */
5130 cp_error ("`%D' is already declared in this scope",
5134 else if (duplicate_decls (new_fn
, old_fn
))
5135 /* We're re-using something we already used
5136 before. We don't need to add it again. */
5140 /* If we broke out of the loop, there's no reason to add
5141 this function to the using declarations for this
5146 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
5147 if (TREE_CODE (*newval
) != OVERLOAD
)
5148 *newval
= ovl_cons (*newval
, NULL_TREE
);
5149 OVL_USED (*newval
) = 1;
5154 *newval
= BINDING_VALUE (decls
);
5156 duplicate_decls (*newval
, oldval
);
5159 *newtype
= BINDING_TYPE (decls
);
5160 if (oldtype
&& *newtype
&& oldtype
!= *newtype
)
5162 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5168 /* Process a using-declaration not appearing in class or local scope. */
5171 do_toplevel_using_decl (decl
)
5174 tree scope
, name
, binding
;
5175 tree oldval
, oldtype
, newval
, newtype
;
5177 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
5178 if (decl
== NULL_TREE
)
5181 binding
= binding_for_name (name
, current_namespace
);
5183 oldval
= BINDING_VALUE (binding
);
5184 oldtype
= BINDING_TYPE (binding
);
5186 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
5188 /* Copy declarations found. */
5190 BINDING_VALUE (binding
) = newval
;
5192 BINDING_TYPE (binding
) = newtype
;
5196 /* Process a using-declaration at function scope. */
5199 do_local_using_decl (decl
)
5203 tree oldval
, oldtype
, newval
, newtype
;
5205 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
5206 if (decl
== NULL_TREE
)
5209 oldval
= lookup_name_current_level (name
);
5210 oldtype
= lookup_type_current_level (name
);
5212 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
5216 if (is_overloaded_fn (newval
))
5220 /* We only need to push declarations for those functions
5221 that were not already bound in the current level.
5222 The old value might be NULL_TREE, it might be a single
5223 function, or an OVERLOAD. */
5224 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
5225 term
= OVL_FUNCTION (oldval
);
5228 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
5230 push_overloaded_decl (OVL_CURRENT (fn
),
5231 PUSH_LOCAL
| PUSH_USING
);
5234 push_local_binding (name
, newval
, PUSH_USING
);
5237 set_identifier_type_value (name
, newtype
);
5241 do_class_using_decl (decl
)
5246 if (TREE_CODE (decl
) != SCOPE_REF
5247 || !TYPE_P (TREE_OPERAND (decl
, 0)))
5249 cp_error ("using-declaration for non-member at class scope");
5252 name
= TREE_OPERAND (decl
, 1);
5253 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
5255 cp_error ("using-declaration for destructor");
5258 if (TREE_CODE (name
) == TYPE_DECL
)
5259 name
= DECL_NAME (name
);
5261 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
5263 value
= build_lang_decl (USING_DECL
, name
, void_type_node
);
5264 DECL_INITIAL (value
) = TREE_OPERAND (decl
, 0);
5268 /* Process a using-directive. */
5271 do_using_directive (namespace)
5274 if (namespace == fake_std_node
)
5276 /* using namespace A::B::C; */
5277 if (TREE_CODE (namespace) == SCOPE_REF
)
5278 namespace = TREE_OPERAND (namespace, 1);
5279 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
5281 /* Lookup in lexer did not find a namespace. */
5282 cp_error ("namespace `%T' undeclared", namespace);
5285 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
5287 cp_error ("`%T' is not a namespace", namespace);
5290 namespace = ORIGINAL_NAMESPACE (namespace);
5291 if (!toplevel_bindings_p ())
5292 push_using_directive (namespace);
5295 add_using_namespace (current_namespace
, namespace, 0);
5299 check_default_args (x
)
5302 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5303 int saw_def
= 0, i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5304 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5306 if (TREE_PURPOSE (arg
))
5310 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5321 TREE_USED (decl
) = 1;
5322 if (processing_template_decl
)
5324 assemble_external (decl
);
5326 /* Is it a synthesized method that needs to be synthesized? */
5327 if (TREE_CODE (decl
) == FUNCTION_DECL
5328 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5329 && DECL_ARTIFICIAL (decl
)
5330 && ! DECL_INITIAL (decl
)
5331 /* Kludge: don't synthesize for default args. */
5332 && current_function_decl
)
5334 synthesize_method (decl
);
5335 /* If we've already synthesized the method we don't need to
5336 instantiate it, so we can return right away. */
5340 /* If this is a function or variable that is an instance of some
5341 template, we now know that we will need to actually do the
5342 instantiation. We check that DECL is not an explicit
5343 instantiation because that is not checked in instantiate_decl. */
5344 if ((DECL_NON_THUNK_FUNCTION_P (decl
) || TREE_CODE (decl
) == VAR_DECL
)
5345 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
5346 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5347 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INLINE (decl
))))
5348 instantiate_decl (decl
, /*defer_ok=*/1);
5351 /* Helper function for named_class_head_sans_basetype nonterminal. We
5352 have just seen something of the form `AGGR SCOPE::ID'. Return a
5353 TYPE_DECL for the type declared by ID in SCOPE. */
5356 handle_class_head (aggr
, scope
, id
)
5357 tree aggr
, scope
, id
;
5359 tree decl
= NULL_TREE
;
5361 if (TREE_CODE (id
) == TYPE_DECL
)
5362 /* We must bash typedefs back to the main decl of the type. Otherwise
5363 we become confused about scopes. */
5364 decl
= TYPE_MAIN_DECL (TREE_TYPE (id
));
5365 else if (DECL_CLASS_TEMPLATE_P (id
))
5366 decl
= DECL_TEMPLATE_RESULT (id
);
5369 tree current
= current_scope ();
5371 if (current
== NULL_TREE
)
5372 current
= current_namespace
;
5373 if (scope
== fake_std_node
)
5374 scope
= global_namespace
;
5375 if (scope
== NULL_TREE
)
5376 scope
= global_namespace
;
5380 /* According to the suggested resolution of core issue 180,
5381 'typename' is assumed after a class-key. */
5382 decl
= make_typename_type (scope
, id
, 1);
5383 if (decl
!= error_mark_node
)
5384 decl
= TYPE_MAIN_DECL (decl
);
5388 else if (scope
== current
)
5390 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5391 Be nice about it. */
5393 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5394 FROB_CONTEXT (scope
), id
);
5396 else if (scope
!= global_namespace
)
5397 cp_error ("`%T' does not have a nested type named `%D'", scope
, id
);
5399 cp_error ("no file-scope type named `%D'", id
);
5401 /* Inject it at the current scope. */
5403 decl
= TYPE_MAIN_DECL (xref_tag (aggr
, id
, 1));
5406 /* Enter the SCOPE. If this turns out not to be a definition, the
5407 parser must leave the scope. */
5408 push_scope (CP_DECL_CONTEXT (decl
));
5410 /* If we see something like:
5412 template <typename T> struct S::I ....
5414 we must create a TEMPLATE_DECL for the nested type. */
5415 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5416 decl
= push_template_decl (decl
);
5421 /* Initialize decl2.c. */
5426 ggc_add_tree_root (&decl_namespace_list
, 1);
5427 ggc_add_tree_varray_root (&deferred_fns
, 1);
5428 ggc_add_tree_varray_root (&pending_statics
, 1);
5429 ggc_add_tree_varray_root (&ssdf_decls
, 1);
5430 ggc_add_tree_root (&ssdf_decl
, 1);
5431 ggc_add_tree_root (&priority_decl
, 1);
5432 ggc_add_tree_root (&initialize_p_decl
, 1);
5433 ggc_add_tree_root (&pending_vtables
, 1);