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"
49 extern cpp_reader
*parse_in
;
51 /* This structure contains information about the initializations
52 and/or destructions required for a particular priority level. */
53 typedef struct priority_info_s
{
54 /* Non-zero if there have been any initializations at this priority
55 throughout the translation unit. */
56 int initializations_p
;
57 /* Non-zero if there have been any destructions at this priority
58 throughout the translation unit. */
62 static void mark_vtable_entries
PARAMS ((tree
));
63 static void grok_function_init
PARAMS ((tree
, tree
));
64 static int finish_vtable_vardecl
PARAMS ((tree
*, void *));
65 static int prune_vtable_vardecl
PARAMS ((tree
*, void *));
66 static int is_namespace_ancestor
PARAMS ((tree
, tree
));
67 static void add_using_namespace
PARAMS ((tree
, tree
, int));
68 static tree ambiguous_decl
PARAMS ((tree
, tree
, tree
,int));
69 static tree build_anon_union_vars
PARAMS ((tree
, tree
*, int, int));
70 static int acceptable_java_type
PARAMS ((tree
));
71 static void output_vtable_inherit
PARAMS ((tree
));
72 static tree start_objects
PARAMS ((int, int));
73 static void finish_objects
PARAMS ((int, int, tree
));
74 static tree merge_functions
PARAMS ((tree
, tree
));
75 static tree decl_namespace
PARAMS ((tree
));
76 static tree validate_nonmember_using_decl
PARAMS ((tree
, tree
*, tree
*));
77 static void do_nonmember_using_decl
PARAMS ((tree
, tree
, tree
, tree
,
79 static tree start_static_storage_duration_function
PARAMS ((void));
80 static void finish_static_storage_duration_function
PARAMS ((tree
));
81 static priority_info get_priority_info
PARAMS ((int));
82 static void do_static_initialization
PARAMS ((tree
, tree
));
83 static void do_static_destruction
PARAMS ((tree
));
84 static tree start_static_initialization_or_destruction
PARAMS ((tree
, int));
85 static void finish_static_initialization_or_destruction
PARAMS ((tree
));
86 static void generate_ctor_or_dtor_function
PARAMS ((int, int));
87 static int generate_ctor_and_dtor_functions_for_priority
88 PARAMS ((splay_tree_node
, void *));
89 static tree prune_vars_needing_no_initialization
PARAMS ((tree
));
90 static void write_out_vars
PARAMS ((tree
));
91 static void import_export_class
PARAMS ((tree
));
92 static tree key_method
PARAMS ((tree
));
93 static int compare_options
PARAMS ((const PTR
, const PTR
));
94 static tree get_guard_bits
PARAMS ((tree
));
96 /* A list of static class variables. This is needed, because a
97 static class variable can be declared inside the class without
98 an initializer, and then initialized, staticly, outside the class. */
99 static varray_type pending_statics
;
100 #define pending_statics_used \
101 (pending_statics ? pending_statics->elements_used : 0)
103 /* A list of functions which were declared inline, but which we
104 may need to emit outline anyway. */
105 static varray_type deferred_fns
;
106 #define deferred_fns_used \
107 (deferred_fns ? deferred_fns->elements_used : 0)
109 /* Same, but not reset. Local temp variables and global temp variables
110 can have the same name. */
111 static int global_temp_name_counter
;
113 /* Flag used when debugging spew.c */
115 extern int spew_debug
;
117 /* Nonzero if we're done parsing and into end-of-file activities. */
121 /* Functions called along with real static constructors and destructors. */
126 /* The :: namespace. */
128 tree global_namespace
;
130 /* C (and C++) language-specific option variables. */
132 /* Nonzero means don't recognize the keyword `asm'. */
136 /* Nonzero means don't recognize any extension keywords. */
138 int flag_no_gnu_keywords
;
140 /* Nonzero means do some things the same way PCC does. Only provided so
141 the compiler will link. */
143 int flag_traditional
;
145 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
147 int flag_signed_bitfields
= 1;
149 /* Nonzero means enable obscure standard features and disable GNU
150 extensions that might cause standard-compliant code to be
155 /* Nonzero means do emit exported implementations of functions even if
156 they can be inlined. */
158 int flag_implement_inlines
= 1;
160 /* Nonzero means do emit exported implementations of templates, instead of
161 multiple static copies in each file that needs a definition. */
163 int flag_external_templates
;
165 /* Nonzero means that the decision to emit or not emit the implementation of a
166 template depends on where the template is instantiated, rather than where
169 int flag_alt_external_templates
;
171 /* Nonzero means that implicit instantiations will be emitted if needed. */
173 int flag_implicit_templates
= 1;
175 /* Nonzero means that implicit instantiations of inline templates will be
176 emitted if needed, even if instantiations of non-inline templates
179 int flag_implicit_inline_templates
= 1;
181 /* Nonzero means warn about implicit declarations. */
183 int warn_implicit
= 1;
185 /* Nonzero means warn about usage of long long when `-pedantic'. */
187 int warn_long_long
= 1;
189 /* Nonzero means warn when all ctors or dtors are private, and the class
192 int warn_ctor_dtor_privacy
= 1;
194 /* True if we want to implement vtables using "thunks".
195 The default is off. */
197 int flag_vtable_thunks
= 1;
199 /* Nonzero means generate separate instantiation control files and juggle
200 them at link time. */
202 int flag_use_repository
;
204 /* Nonzero if we want to issue diagnostics that the standard says are not
207 int flag_optional_diags
= 1;
209 /* Nonzero means give string constants the type `const char *', as mandated
212 int flag_const_strings
= 1;
214 /* Nonzero means warn about deprecated conversion from string constant to
217 int warn_write_strings
;
219 /* Nonzero means warn about pointer casts that can drop a type qualifier
220 from the pointer target type. */
224 /* Nonzero means warn about sizeof(function) or addition/subtraction
225 of function pointers. */
227 int warn_pointer_arith
= 1;
229 /* Nonzero means warn for any function def without prototype decl. */
231 int warn_missing_prototypes
;
233 /* Nonzero means warn about multiple (redundant) decls for the same single
234 variable or function. */
236 int warn_redundant_decls
;
238 /* Warn if initializer is not completely bracketed. */
240 int warn_missing_braces
;
242 /* Warn about comparison of signed and unsigned values. */
244 int warn_sign_compare
;
246 /* Warn about testing equality of floating point numbers. */
248 int warn_float_equal
= 0;
250 /* Warn about functions which might be candidates for format attributes. */
252 int warn_missing_format_attribute
;
254 /* Warn about a subscript that has type char. */
256 int warn_char_subscripts
;
258 /* Warn if a type conversion is done that might have confusing results. */
262 /* Warn if adding () is suggested. */
264 int warn_parentheses
;
266 /* Non-zero means warn in function declared in derived class has the
267 same name as a virtual in the base class, but fails to match the
268 type signature of any virtual function in the base class. */
270 int warn_overloaded_virtual
;
272 /* Non-zero means warn when declaring a class that has a non virtual
273 destructor, when it really ought to have a virtual one. */
277 /* Non-zero means warn when the compiler will reorder code. */
281 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
285 /* Non-zero means warn when we convert a pointer to member function
286 into a pointer to (void or function). */
288 int warn_pmf2ptr
= 1;
290 /* Nonzero means warn about violation of some Effective C++ style rules. */
294 /* Nonzero means warn where overload resolution chooses a promotion from
295 unsigned to signed over a conversion to an unsigned of the same size. */
299 /* Nonzero means warn when an old-style cast is used. */
301 int warn_old_style_cast
;
303 /* Warn about #pragma directives that are not recognised. */
305 int warn_unknown_pragmas
; /* Tri state variable. */
307 /* Nonzero means warn about use of multicharacter literals. */
309 int warn_multichar
= 1;
311 /* Nonzero means warn when non-templatized friend functions are
312 declared within a template */
314 int warn_nontemplate_friend
= 1;
316 /* Nonzero means complain about deprecated features. */
318 int warn_deprecated
= 1;
320 /* Nonzero means `$' can be in an identifier. */
322 #ifndef DOLLARS_IN_IDENTIFIERS
323 #define DOLLARS_IN_IDENTIFIERS 1
325 int dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
;
327 /* Nonzero means allow Microsoft extensions without a pedwarn. */
329 int flag_ms_extensions
;
331 /* C++ specific flags. */
333 /* Nonzero means we should attempt to elide constructors when possible. */
335 int flag_elide_constructors
= 1;
337 /* Nonzero means that member functions defined in class scope are
338 inline by default. */
340 int flag_default_inline
= 1;
342 /* Controls whether compiler generates 'type descriptor' that give
343 run-time type information. */
347 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
350 int flag_huge_objects
;
352 /* Nonzero if we want to conserve space in the .o files. We do this
353 by putting uninitialized data and runtime initialized data into
354 .common instead of .data at the expense of not flagging multiple
357 int flag_conserve_space
;
359 /* Nonzero if we want to obey access control semantics. */
361 int flag_access_control
= 1;
363 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
365 int flag_operator_names
= 1;
367 /* Nonzero if we want to check the return value of new and avoid calling
368 constructors if it is a null pointer. */
372 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
373 initialization variables.
374 0: Old rules, set by -fno-for-scope.
375 2: New ISO rules, set by -ffor-scope.
376 1: Try to implement new ISO rules, but with backup compatibility
377 (and warnings). This is the default, for now. */
379 int flag_new_for_scope
= 1;
381 /* Nonzero if we want to emit defined symbols with common-like linkage as
382 weak symbols where possible, in order to conform to C++ semantics.
383 Otherwise, emit them as local symbols. */
387 /* Nonzero to use __cxa_atexit, rather than atexit, to register
388 destructors for local statics and global objects. */
390 int flag_use_cxa_atexit
;
392 /* Nonzero to not ignore namespace std. */
394 int flag_honor_std
= 1;
396 /* 0 if we should not perform inlining.
397 1 if we should expand functions calls inline at the tree level.
398 2 if we should consider *all* functions to be inline
401 int flag_inline_trees
= 0;
403 /* Maximum template instantiation depth. This limit is rather
404 arbitrary, but it exists to limit the time it takes to notice
405 infinite template instantiations. */
407 int max_tinst_depth
= 50;
409 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
413 /* Nonzero means make the default pedwarns warnings instead of errors.
414 The value of this flag is ignored if -pedantic is specified. */
418 /* Nonzero means to implement standard semantics for exception
419 specifications, calling unexpected if an exception is thrown that
420 doesn't match the specification. Zero means to treat them as
421 assertions and optimize accordingly, but not check them. */
423 int flag_enforce_eh_specs
= 1;
425 /* The variant of the C language being processed. */
427 c_language_kind c_language
= clk_cplusplus
;
429 /* Table of language-dependent -f options.
430 STRING is the option name. VARIABLE is the address of the variable.
431 ON_VALUE is the value to store in VARIABLE
432 if `-fSTRING' is seen as an option.
433 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
435 static struct { const char *string
; int *variable
; int on_value
;}
439 {"signed-char", &flag_signed_char
, 1},
440 {"unsigned-char", &flag_signed_char
, 0},
441 {"signed-bitfields", &flag_signed_bitfields
, 1},
442 {"unsigned-bitfields", &flag_signed_bitfields
, 0},
443 {"short-enums", &flag_short_enums
, 1},
444 {"short-double", &flag_short_double
, 1},
445 {"short-wchar", &flag_short_wchar
, 1},
446 {"asm", &flag_no_asm
, 0},
447 {"builtin", &flag_no_builtin
, 0},
449 /* C++-only options. */
450 {"access-control", &flag_access_control
, 1},
451 {"check-new", &flag_check_new
, 1},
452 {"conserve-space", &flag_conserve_space
, 1},
453 {"const-strings", &flag_const_strings
, 1},
454 {"default-inline", &flag_default_inline
, 1},
455 {"dollars-in-identifiers", &dollars_in_ident
, 1},
456 {"elide-constructors", &flag_elide_constructors
, 1},
457 {"enforce-eh-specs", &flag_enforce_eh_specs
, 1},
458 {"external-templates", &flag_external_templates
, 1},
459 {"for-scope", &flag_new_for_scope
, 2},
460 {"gnu-keywords", &flag_no_gnu_keywords
, 0},
461 {"handle-exceptions", &flag_exceptions
, 1},
462 {"honor-std", &flag_honor_std
, 1},
463 {"implement-inlines", &flag_implement_inlines
, 1},
464 {"implicit-inline-templates", &flag_implicit_inline_templates
, 1},
465 {"implicit-templates", &flag_implicit_templates
, 1},
466 {"ms-extensions", &flag_ms_extensions
, 1},
467 {"nonansi-builtins", &flag_no_nonansi_builtin
, 0},
468 {"operator-names", &flag_operator_names
, 1},
469 {"optional-diags", &flag_optional_diags
, 1},
470 {"permissive", &flag_permissive
, 1},
471 {"repo", &flag_use_repository
, 1},
472 {"rtti", &flag_rtti
, 1},
473 {"stats", &flag_detailed_statistics
, 1},
474 {"vtable-gc", &flag_vtable_gc
, 1},
475 {"use-cxa-atexit", &flag_use_cxa_atexit
, 1},
476 {"weak", &flag_weak
, 1}
479 /* The list of `-f' options that we no longer support. The `-f'
480 prefix is not given in this table. The `-fno-' variants are not
481 listed here. This table must be kept in alphabetical order. */
482 static const char * const unsupported_options
[] = {
498 /* Compare two option strings, pointed two by P1 and P2, for use with
502 compare_options (p1
, p2
)
506 return strcmp (*((const char *const *) p1
), *((const char *const *) p2
));
509 /* Decode the string P as a language-specific option.
510 Return the number of strings consumed for a valid option.
511 Otherwise return 0. Should not complain if it does not
512 recognise the option. */
515 cxx_decode_option (argc
, argv
)
519 int strings_processed
;
520 const char *p
= argv
[0];
522 strings_processed
= cpp_handle_option (parse_in
, argc
, argv
);
524 if (!strcmp (p
, "-ftraditional") || !strcmp (p
, "-traditional"))
526 else if (p
[0] == '-' && p
[1] == 'f')
528 /* Some kind of -f option.
529 P's value is the option sans `-f'.
530 Search for it in the table of options. */
531 const char *option_value
= NULL
;
532 const char *positive_option
;
536 /* Try special -f options. */
538 /* See if this is one of the options no longer supported. We
539 used to support these options, so we continue to accept them,
541 if (strncmp (p
, "no-", strlen ("no-")) == 0)
542 positive_option
= p
+ strlen ("no-");
546 /* If the option is present, issue a warning. Indicate to our
547 caller that the option was processed successfully. */
548 if (bsearch (&positive_option
,
550 (sizeof (unsupported_options
)
551 / sizeof (unsupported_options
[0])),
552 sizeof (unsupported_options
[0]),
555 warning ("-f%s is no longer supported", p
);
559 if (!strcmp (p
, "handle-exceptions")
560 || !strcmp (p
, "no-handle-exceptions"))
561 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
562 else if (! strcmp (p
, "alt-external-templates"))
564 flag_external_templates
= 1;
565 flag_alt_external_templates
= 1;
566 cp_deprecated ("-falt-external-templates");
568 else if (! strcmp (p
, "no-alt-external-templates"))
569 flag_alt_external_templates
= 0;
570 else if (!strcmp (p
, "repo"))
572 flag_use_repository
= 1;
573 flag_implicit_templates
= 0;
575 else if (!strcmp (p
, "external-templates"))
577 flag_external_templates
= 1;
578 cp_deprecated ("-fexternal-templates");
580 else if ((option_value
581 = skip_leading_substring (p
, "template-depth-")))
583 = read_integral_parameter (option_value
, p
- 2, max_tinst_depth
);
584 else if ((option_value
585 = skip_leading_substring (p
, "name-mangling-version-")))
587 warning ("-fname-mangling-version is no longer supported");
590 else if (dump_switch_p (p
))
597 !found
&& j
< (sizeof (lang_f_options
)
598 / sizeof (lang_f_options
[0]));
601 if (!strcmp (p
, lang_f_options
[j
].string
))
603 *lang_f_options
[j
].variable
= lang_f_options
[j
].on_value
;
604 /* A goto here would be cleaner,
605 but breaks the vax pcc. */
608 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
609 && ! strcmp (p
+3, lang_f_options
[j
].string
))
611 *lang_f_options
[j
].variable
= ! lang_f_options
[j
].on_value
;
619 else if (p
[0] == '-' && p
[1] == 'W')
623 /* The -W options control the warning behavior of the compiler. */
626 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-')
629 if (!strcmp (p
, "implicit"))
630 warn_implicit
= setting
;
631 else if (!strcmp (p
, "long-long"))
632 warn_long_long
= setting
;
633 else if (!strcmp (p
, "return-type"))
634 warn_return_type
= setting
;
635 else if (!strcmp (p
, "ctor-dtor-privacy"))
636 warn_ctor_dtor_privacy
= setting
;
637 else if (!strcmp (p
, "write-strings"))
638 warn_write_strings
= setting
;
639 else if (!strcmp (p
, "cast-qual"))
640 warn_cast_qual
= setting
;
641 else if (!strcmp (p
, "char-subscripts"))
642 warn_char_subscripts
= setting
;
643 else if (!strcmp (p
, "pointer-arith"))
644 warn_pointer_arith
= setting
;
645 else if (!strcmp (p
, "missing-prototypes"))
646 warn_missing_prototypes
= setting
;
647 else if (!strcmp (p
, "strict-prototypes"))
650 warning ("-Wno-strict-prototypes is not supported in C++");
652 else if (!strcmp (p
, "redundant-decls"))
653 warn_redundant_decls
= setting
;
654 else if (!strcmp (p
, "missing-braces"))
655 warn_missing_braces
= setting
;
656 else if (!strcmp (p
, "sign-compare"))
657 warn_sign_compare
= setting
;
658 else if (!strcmp (p
, "float-equal"))
659 warn_float_equal
= setting
;
660 else if (!strcmp (p
, "format"))
661 set_Wformat (setting
);
662 else if (!strcmp (p
, "format=2"))
664 else if (!strcmp (p
, "format-y2k"))
665 warn_format_y2k
= setting
;
666 else if (!strcmp (p
, "format-extra-args"))
667 warn_format_extra_args
= setting
;
668 else if (!strcmp (p
, "format-nonliteral"))
669 warn_format_nonliteral
= setting
;
670 else if (!strcmp (p
, "format-security"))
671 warn_format_security
= setting
;
672 else if (!strcmp (p
, "missing-format-attribute"))
673 warn_missing_format_attribute
= setting
;
674 else if (!strcmp (p
, "conversion"))
675 warn_conversion
= setting
;
676 else if (!strcmp (p
, "parentheses"))
677 warn_parentheses
= setting
;
678 else if (!strcmp (p
, "non-virtual-dtor"))
679 warn_nonvdtor
= setting
;
680 else if (!strcmp (p
, "reorder"))
681 warn_reorder
= setting
;
682 else if (!strcmp (p
, "synth"))
683 warn_synth
= setting
;
684 else if (!strcmp (p
, "pmf-conversions"))
685 warn_pmf2ptr
= setting
;
686 else if (!strcmp (p
, "effc++"))
688 else if (!strcmp (p
, "sign-promo"))
689 warn_sign_promo
= setting
;
690 else if (!strcmp (p
, "old-style-cast"))
691 warn_old_style_cast
= setting
;
692 else if (!strcmp (p
, "overloaded-virtual"))
693 warn_overloaded_virtual
= setting
;
694 else if (!strcmp (p
, "multichar"))
695 warn_multichar
= setting
;
696 else if (!strcmp (p
, "unknown-pragmas"))
697 /* Set to greater than 1, so that even unknown pragmas in
698 system headers will be warned about. */
699 warn_unknown_pragmas
= setting
* 2;
700 else if (!strcmp (p
, "non-template-friend"))
701 warn_nontemplate_friend
= setting
;
702 else if (!strcmp (p
, "deprecated"))
703 warn_deprecated
= setting
;
704 else if (!strcmp (p
, "comment"))
705 ; /* cpp handles this one. */
706 else if (!strcmp (p
, "comments"))
707 ; /* cpp handles this one. */
708 else if (!strcmp (p
, "trigraphs"))
709 ; /* cpp handles this one. */
710 else if (!strcmp (p
, "import"))
711 ; /* cpp handles this one. */
712 else if (!strcmp (p
, "all"))
714 warn_return_type
= setting
;
715 set_Wunused (setting
);
716 warn_implicit
= setting
;
717 warn_switch
= setting
;
718 set_Wformat (setting
);
719 warn_parentheses
= setting
;
720 warn_missing_braces
= setting
;
721 warn_sign_compare
= setting
;
722 warn_multichar
= setting
;
723 /* We save the value of warn_uninitialized, since if they put
724 -Wuninitialized on the command line, we need to generate a
725 warning about not using it without also specifying -O. */
726 if (warn_uninitialized
!= 1)
727 warn_uninitialized
= (setting
? 2 : 0);
728 /* Only warn about unknown pragmas that are not in system
730 warn_unknown_pragmas
= 1;
732 /* C++-specific warnings. */
733 warn_ctor_dtor_privacy
= setting
;
734 warn_nonvdtor
= setting
;
735 warn_reorder
= setting
;
736 warn_nontemplate_friend
= setting
;
738 else return strings_processed
;
740 else if (!strcmp (p
, "-ansi"))
741 flag_no_nonansi_builtin
= 1, flag_ansi
= 1,
742 flag_noniso_default_format_attributes
= 0, flag_no_gnu_keywords
= 1;
744 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
745 it's probably safe to assume no sane person would ever want to use this
746 under normal circumstances. */
747 else if (!strcmp (p
, "-spew-debug"))
751 return strings_processed
;
756 /* Incorporate `const' and `volatile' qualifiers for member functions.
757 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
758 QUALS is a list of qualifiers. Returns any explicit
759 top-level qualifiers of the method's this pointer, anything other than
760 TYPE_UNQUALIFIED will be an extension. */
763 grok_method_quals (ctype
, function
, quals
)
764 tree ctype
, function
, quals
;
766 tree fntype
= TREE_TYPE (function
);
767 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
768 int type_quals
= TYPE_UNQUALIFIED
;
769 int dup_quals
= TYPE_UNQUALIFIED
;
770 int this_quals
= TYPE_UNQUALIFIED
;
774 int tq
= cp_type_qual_from_rid (TREE_VALUE (quals
));
776 if ((type_quals
| this_quals
) & tq
)
778 else if (tq
& TYPE_QUAL_RESTRICT
)
782 quals
= TREE_CHAIN (quals
);
786 if (dup_quals
!= TYPE_UNQUALIFIED
)
787 cp_error ("duplicate type qualifiers in %s declaration",
788 TREE_CODE (function
) == FUNCTION_DECL
789 ? "member function" : "type");
791 ctype
= cp_build_qualified_type (ctype
, type_quals
);
792 fntype
= build_cplus_method_type (ctype
, TREE_TYPE (fntype
),
793 (TREE_CODE (fntype
) == METHOD_TYPE
794 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
795 : TYPE_ARG_TYPES (fntype
)));
797 fntype
= build_exception_variant (fntype
, raises
);
799 TREE_TYPE (function
) = fntype
;
803 /* Warn when -fexternal-templates is used and #pragma
804 interface/implementation is not used all the times it should be,
808 warn_if_unknown_interface (decl
)
811 static int already_warned
= 0;
812 if (already_warned
++)
815 if (flag_alt_external_templates
)
817 tree til
= tinst_for_decl ();
819 const char *sf
= input_filename
;
823 lineno
= TINST_LINE (til
);
824 input_filename
= TINST_FILE (til
);
826 cp_warning ("template `%#D' instantiated in file without #pragma interface",
832 cp_warning_at ("template `%#D' defined in file without #pragma interface",
836 /* A subroutine of the parser, to handle a component list. */
839 grok_x_components (specs
)
844 specs
= strip_attrs (specs
);
846 check_tag_decl (specs
);
847 t
= groktypename (build_tree_list (specs
, NULL_TREE
));
849 /* The only case where we need to do anything additional here is an
850 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
851 if (t
== NULL_TREE
|| !ANON_AGGR_TYPE_P (t
))
854 fixup_anonymous_aggr (t
);
855 finish_member_declaration (build_decl (FIELD_DECL
, NULL_TREE
, t
));
858 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
862 build_artificial_parm (name
, type
)
868 parm
= build_decl (PARM_DECL
, name
, type
);
869 DECL_ARTIFICIAL (parm
) = 1;
870 /* All our artificial parms are implicitly `const'; they cannot be
872 TREE_READONLY (parm
) = 1;
873 DECL_ARG_TYPE (parm
) = type
;
877 /* Constructors for types with virtual baseclasses need an "in-charge" flag
878 saying whether this constructor is responsible for initialization of
879 virtual baseclasses or not. All destructors also need this "in-charge"
880 flag, which additionally determines whether or not the destructor should
881 free the memory for the object.
883 This function adds the "in-charge" flag to member function FN if
884 appropriate. It is called from grokclassfn and tsubst.
885 FN must be either a constructor or destructor.
887 The in-charge flag follows the 'this' parameter, and is followed by the
888 VTT parm (if any), then the user-written parms. */
891 maybe_retrofit_in_chrg (fn
)
894 tree basetype
, arg_types
, parms
, parm
, fntype
;
896 /* If we've already add the in-charge parameter don't do it again. */
897 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
900 /* When processing templates we can't know, in general, whether or
901 not we're going to have virtual baseclasses. */
902 if (uses_template_parms (fn
))
905 /* We don't need an in-charge parameter for constructors that don't
906 have virtual bases. */
907 if (DECL_CONSTRUCTOR_P (fn
)
908 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
911 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
912 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
913 arg_types
= TREE_CHAIN (arg_types
);
915 parms
= TREE_CHAIN (DECL_ARGUMENTS (fn
));
917 /* If this is a subobject constructor or destructor, our caller will
918 pass us a pointer to our VTT. */
919 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
921 parm
= build_artificial_parm (vtt_parm_identifier
, vtt_parm_type
);
923 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
924 TREE_CHAIN (parm
) = parms
;
927 /* ...and then to TYPE_ARG_TYPES. */
928 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
930 DECL_HAS_VTT_PARM_P (fn
) = 1;
933 /* Then add the in-charge parm (before the VTT parm). */
934 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
935 TREE_CHAIN (parm
) = parms
;
937 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
939 /* Insert our new parameter(s) into the list. */
940 TREE_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
942 /* And rebuild the function type. */
943 fntype
= build_cplus_method_type (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
945 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
946 fntype
= build_exception_variant (fntype
,
947 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
948 TREE_TYPE (fn
) = fntype
;
950 /* Now we've got the in-charge parameter. */
951 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
954 /* Classes overload their constituent function names automatically.
955 When a function name is declared in a record structure,
956 its name is changed to it overloaded name. Since names for
957 constructors and destructors can conflict, we place a leading
960 CNAME is the name of the class we are grokking for.
962 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
964 FLAGS contains bits saying what's special about today's
965 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
967 If FUNCTION is a destructor, then we must add the `auto-delete' field
968 as a second parameter. There is some hair associated with the fact
969 that we must "declare" this variable in the manner consistent with the
970 way the rest of the arguments were declared.
972 QUALS are the qualifiers for the this pointer. */
975 grokclassfn (ctype
, function
, flags
, quals
)
976 tree ctype
, function
;
977 enum overload_flags flags
;
980 tree fn_name
= DECL_NAME (function
);
981 int this_quals
= TYPE_UNQUALIFIED
;
983 /* Even within an `extern "C"' block, members get C++ linkage. See
984 [dcl.link] for details. */
985 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
987 if (fn_name
== NULL_TREE
)
989 error ("name missing for member function");
990 fn_name
= get_identifier ("<anonymous>");
991 DECL_NAME (function
) = fn_name
;
995 this_quals
= grok_method_quals (ctype
, function
, quals
);
997 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
999 /* Must add the class instance variable up front. */
1000 /* Right now we just make this a pointer. But later
1001 we may wish to make it special. */
1002 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function
)));
1006 /* The `this' parameter is implicitly `const'; it cannot be
1008 this_quals
|= TYPE_QUAL_CONST
;
1009 qual_type
= cp_build_qualified_type (type
, this_quals
);
1010 parm
= build_artificial_parm (this_identifier
, qual_type
);
1011 c_apply_type_quals_to_decl (this_quals
, parm
);
1013 /* We can make this a register, so long as we don't
1014 accidentally complain if someone tries to take its address. */
1015 DECL_REGISTER (parm
) = 1;
1016 TREE_CHAIN (parm
) = last_function_parms
;
1017 last_function_parms
= parm
;
1020 DECL_ARGUMENTS (function
) = last_function_parms
;
1021 DECL_CONTEXT (function
) = ctype
;
1023 if (flags
== DTOR_FLAG
)
1024 DECL_DESTRUCTOR_P (function
) = 1;
1026 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
1027 maybe_retrofit_in_chrg (function
);
1029 if (flags
== DTOR_FLAG
)
1031 DECL_DESTRUCTOR_P (function
) = 1;
1032 TYPE_HAS_DESTRUCTOR (ctype
) = 1;
1036 /* Work on the expr used by alignof (this is only called by the parser). */
1045 if (processing_template_decl
)
1046 return build_min (ALIGNOF_EXPR
, sizetype
, expr
);
1048 if (TREE_CODE (expr
) == COMPONENT_REF
1049 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1050 error ("`__alignof__' applied to a bit-field");
1052 if (TREE_CODE (expr
) == INDIRECT_REF
)
1054 best
= t
= TREE_OPERAND (expr
, 0);
1055 bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
1057 while (TREE_CODE (t
) == NOP_EXPR
1058 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
1061 t
= TREE_OPERAND (t
, 0);
1062 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
1063 if (thisalign
> bestalign
)
1064 best
= t
, bestalign
= thisalign
;
1066 return c_alignof (TREE_TYPE (TREE_TYPE (best
)));
1069 return c_alignof (TREE_TYPE (expr
));
1072 /* Create an ARRAY_REF, checking for the user doing things backwards
1076 grok_array_decl (array_expr
, index_exp
)
1077 tree array_expr
, index_exp
;
1079 tree type
= TREE_TYPE (array_expr
);
1080 tree p1
, p2
, i1
, i2
;
1082 if (type
== error_mark_node
|| index_exp
== error_mark_node
)
1083 return error_mark_node
;
1084 if (processing_template_decl
)
1085 return build_min (ARRAY_REF
, type
? TREE_TYPE (type
) : NULL_TREE
,
1086 array_expr
, index_exp
);
1088 if (type
== NULL_TREE
)
1090 /* Something has gone very wrong. Assume we are mistakenly reducing
1091 an expression instead of a declaration. */
1092 error ("parser may be lost: is there a '{' missing somewhere?");
1096 if (TREE_CODE (type
) == OFFSET_TYPE
1097 || TREE_CODE (type
) == REFERENCE_TYPE
)
1098 type
= TREE_TYPE (type
);
1100 /* If they have an `operator[]', use that. */
1101 if (IS_AGGR_TYPE (type
) || IS_AGGR_TYPE (TREE_TYPE (index_exp
)))
1102 return build_opfncall (ARRAY_REF
, LOOKUP_NORMAL
,
1103 array_expr
, index_exp
, NULL_TREE
);
1105 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1106 is a little-known fact that, if `a' is an array and `i' is an
1107 int, you can write `i[a]', which means the same thing as `a[i]'. */
1109 if (TREE_CODE (type
) == ARRAY_TYPE
)
1112 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, 0);
1114 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
1117 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, 0);
1119 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
, 0);
1120 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
, 0);
1122 if ((p1
&& i2
) && (i1
&& p2
))
1123 error ("ambiguous conversion for array subscript");
1126 array_expr
= p1
, index_exp
= i2
;
1128 array_expr
= p2
, index_exp
= i1
;
1131 cp_error ("invalid types `%T[%T]' for array subscript",
1132 type
, TREE_TYPE (index_exp
));
1133 return error_mark_node
;
1136 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
1137 error ("ambiguous conversion for array subscript");
1139 return build_array_ref (array_expr
, index_exp
);
1142 /* Given the cast expression EXP, checking out its validity. Either return
1143 an error_mark_node if there was an unavoidable error, return a cast to
1144 void for trying to delete a pointer w/ the value 0, or return the
1145 call to delete. If DOING_VEC is 1, we handle things differently
1146 for doing an array delete. If DOING_VEC is 2, they gave us the
1147 array size as an argument to delete.
1148 Implements ARM $5.3.4. This is called from the parser. */
1151 delete_sanity (exp
, size
, doing_vec
, use_global_delete
)
1153 int doing_vec
, use_global_delete
;
1156 /* For a regular vector delete (aka, no size argument) we will pass
1157 this down as a NULL_TREE into build_vec_delete. */
1158 tree maxindex
= NULL_TREE
;
1160 if (exp
== error_mark_node
)
1163 if (processing_template_decl
)
1165 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
1166 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
1167 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
1171 if (TREE_CODE (exp
) == OFFSET_REF
)
1172 exp
= resolve_offset_ref (exp
);
1173 exp
= convert_from_reference (exp
);
1174 t
= stabilize_reference (exp
);
1175 t
= build_expr_type_conversion (WANT_POINTER
, t
, 1);
1177 if (t
== NULL_TREE
|| t
== error_mark_node
)
1179 cp_error ("type `%#T' argument given to `delete', expected pointer",
1181 return error_mark_node
;
1186 maxindex
= cp_build_binary_op (MINUS_EXPR
, size
, integer_one_node
);
1187 pedwarn ("anachronistic use of array size in vector delete");
1190 type
= TREE_TYPE (t
);
1192 /* As of Valley Forge, you can delete a pointer to const. */
1194 /* You can't delete functions. */
1195 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
1197 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1198 return error_mark_node
;
1201 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1202 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
1204 cp_warning ("deleting `%T' is undefined", type
);
1208 /* An array can't have been allocated by new, so complain. */
1209 if (TREE_CODE (t
) == ADDR_EXPR
1210 && TREE_CODE (TREE_OPERAND (t
, 0)) == VAR_DECL
1211 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == ARRAY_TYPE
)
1212 cp_warning ("deleting array `%#D'", TREE_OPERAND (t
, 0));
1214 /* Deleting a pointer with the value zero is valid and has no effect. */
1215 if (integer_zerop (t
))
1216 return build1 (NOP_EXPR
, void_type_node
, t
);
1219 return build_vec_delete (t
, maxindex
, sfk_deleting_destructor
,
1223 if (IS_AGGR_TYPE (TREE_TYPE (type
))
1224 && TYPE_GETS_REG_DELETE (TREE_TYPE (type
)))
1226 /* Only do access checking here; we'll be calling op delete
1227 from the destructor. */
1228 tree tmp
= build_op_delete_call (DELETE_EXPR
, t
, size_zero_node
,
1229 LOOKUP_NORMAL
, NULL_TREE
);
1230 if (tmp
== error_mark_node
)
1231 return error_mark_node
;
1234 return build_delete (type
, t
, sfk_deleting_destructor
,
1235 LOOKUP_NORMAL
, use_global_delete
);
1239 /* Report an error if the indicated template declaration is not the
1240 sort of thing that should be a member template. */
1243 check_member_template (tmpl
)
1248 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
1249 decl
= DECL_TEMPLATE_RESULT (tmpl
);
1251 if (TREE_CODE (decl
) == FUNCTION_DECL
1252 || (TREE_CODE (decl
) == TYPE_DECL
1253 && IS_AGGR_TYPE (TREE_TYPE (decl
))))
1255 if (current_function_decl
)
1256 /* 14.5.2.2 [temp.mem]
1258 A local class shall not have member templates. */
1259 cp_error ("invalid declaration of member template `%#D' in local class",
1262 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_VIRTUAL_P (decl
))
1264 /* 14.5.2.3 [temp.mem]
1266 A member function template shall not be virtual. */
1268 ("invalid use of `virtual' in template declaration of `%#D'",
1270 DECL_VIRTUAL_P (decl
) = 0;
1273 /* The debug-information generating code doesn't know what to do
1274 with member templates. */
1275 DECL_IGNORED_P (tmpl
) = 1;
1278 cp_error ("template declaration of `%#D'", decl
);
1281 /* Return true iff TYPE is a valid Java parameter or return type. */
1284 acceptable_java_type (type
)
1287 if (TREE_CODE (type
) == VOID_TYPE
|| TYPE_FOR_JAVA (type
))
1289 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
1291 type
= TREE_TYPE (type
);
1292 if (TREE_CODE (type
) == RECORD_TYPE
)
1295 if (! TYPE_FOR_JAVA (type
))
1297 if (! CLASSTYPE_TEMPLATE_INFO (type
))
1299 args
= CLASSTYPE_TI_ARGS (type
);
1300 i
= TREE_VEC_LENGTH (args
);
1303 type
= TREE_VEC_ELT (args
, i
);
1304 if (TREE_CODE (type
) == POINTER_TYPE
)
1305 type
= TREE_TYPE (type
);
1306 if (! TYPE_FOR_JAVA (type
))
1315 /* For a METHOD in a Java class CTYPE, return 1 if
1316 the parameter and return types are valid Java types.
1317 Otherwise, print appropriate error messages, and return 0. */
1320 check_java_method (method
)
1324 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1325 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
1326 if (! acceptable_java_type (ret_type
))
1328 cp_error ("Java method '%D' has non-Java return type `%T'",
1332 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
1334 tree type
= TREE_VALUE (arg_types
);
1335 if (! acceptable_java_type (type
))
1337 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1342 return jerr
? 0 : 1;
1345 /* Sanity check: report error if this function FUNCTION is not
1346 really a member of the class (CTYPE) it is supposed to belong to.
1347 CNAME is the same here as it is for grokclassfn above. */
1350 check_classfn (ctype
, function
)
1351 tree ctype
, function
;
1353 tree fn_name
= DECL_NAME (function
);
1354 tree fndecl
, fndecls
;
1355 tree method_vec
= CLASSTYPE_METHOD_VEC (complete_type (ctype
));
1359 if (DECL_USE_TEMPLATE (function
)
1360 && !(TREE_CODE (function
) == TEMPLATE_DECL
1361 && DECL_TEMPLATE_SPECIALIZATION (function
))
1362 && is_member_template (DECL_TI_TEMPLATE (function
)))
1363 /* Since this is a specialization of a member template,
1364 we're not going to find the declaration in the class.
1367 struct S { template <typename T> void f(T); };
1368 template <> void S::f(int);
1370 we're not going to find `S::f(int)', but there's no
1371 reason we should, either. We let our callers know we didn't
1372 find the method, but we don't complain. */
1375 if (method_vec
!= 0)
1377 methods
= &TREE_VEC_ELT (method_vec
, 0);
1378 end
= TREE_VEC_END (method_vec
);
1380 /* First suss out ctors and dtors. */
1381 if (*methods
&& fn_name
== DECL_NAME (OVL_CURRENT (*methods
))
1382 && DECL_CONSTRUCTOR_P (function
))
1384 if (*++methods
&& fn_name
== DECL_NAME (OVL_CURRENT (*methods
))
1385 && DECL_DESTRUCTOR_P (function
))
1388 while (++methods
!= end
&& *methods
)
1391 if (fn_name
== DECL_NAME (OVL_CURRENT (*methods
)))
1394 for (fndecls
= *methods
; fndecls
!= NULL_TREE
;
1395 fndecls
= OVL_NEXT (fndecls
))
1397 fndecl
= OVL_CURRENT (fndecls
);
1399 /* We cannot simply call decls_match because this
1400 doesn't work for static member functions that are
1401 pretending to be methods, and because the name
1402 may have been changed by asm("new_name"). */
1403 if (DECL_NAME (function
) == DECL_NAME (fndecl
))
1405 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1406 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1408 /* Get rid of the this parameter on functions that become
1410 if (DECL_STATIC_FUNCTION_P (fndecl
)
1411 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1412 p1
= TREE_CHAIN (p1
);
1414 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
1415 TREE_TYPE (TREE_TYPE (fndecl
)))
1416 && compparms (p1
, p2
)
1417 && (DECL_TEMPLATE_SPECIALIZATION (function
)
1418 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
1419 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
1420 || (DECL_TI_TEMPLATE (function
)
1421 == DECL_TI_TEMPLATE (fndecl
))))
1430 if (methods
!= end
&& *methods
)
1432 tree fndecl
= *methods
;
1433 cp_error ("prototype for `%#D' does not match any in class `%T'",
1435 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl
) ? "s are" : " is",
1436 OVL_CURRENT (fndecl
));
1437 while (fndecl
= OVL_NEXT (fndecl
), fndecl
)
1438 cp_error_at (" %#D", OVL_CURRENT(fndecl
));
1443 if (!COMPLETE_TYPE_P (ctype
))
1444 incomplete_type_error (function
, ctype
);
1446 cp_error ("no `%#D' member function declared in class `%T'",
1450 /* If we did not find the method in the class, add it to avoid
1451 spurious errors (unless the CTYPE is not yet defined, in which
1452 case we'll only confuse ourselves when the function is declared
1453 properly within the class. */
1454 if (COMPLETE_TYPE_P (ctype
))
1455 add_method (ctype
, function
, /*error_p=*/1);
1459 /* We have just processed the DECL, which is a static data member.
1460 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1461 present, is the assembly-language name for the data member.
1462 FLAGS is as for cp_finish_decl. */
1465 finish_static_data_member_decl (decl
, init
, asmspec_tree
, flags
)
1471 my_friendly_assert (TREE_PUBLIC (decl
), 0);
1473 DECL_CONTEXT (decl
) = current_class_type
;
1475 /* We cannot call pushdecl here, because that would fill in the
1476 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1477 the right thing, namely, to put this decl out straight away. */
1478 /* current_class_type can be NULL_TREE in case of error. */
1479 if (!asmspec_tree
&& current_class_type
)
1480 DECL_INITIAL (decl
) = error_mark_node
;
1482 if (! processing_template_decl
)
1484 if (!pending_statics
)
1485 VARRAY_TREE_INIT (pending_statics
, 32, "pending_statics");
1486 VARRAY_PUSH_TREE (pending_statics
, decl
);
1489 /* Static consts need not be initialized in the class definition. */
1490 if (init
!= NULL_TREE
&& TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
1492 static int explained
= 0;
1494 error ("initializer invalid for static member with constructor");
1497 error ("(an out of class initialization is required)");
1502 /* Force the compiler to know when an uninitialized static const
1503 member is being used. */
1504 if (CP_TYPE_CONST_P (TREE_TYPE (decl
)) && init
== 0)
1505 TREE_USED (decl
) = 1;
1506 DECL_INITIAL (decl
) = init
;
1507 DECL_IN_AGGR_P (decl
) = 1;
1509 cp_finish_decl (decl
, init
, asmspec_tree
, flags
);
1512 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1513 of a structure component, returning a _DECL node.
1514 QUALS is a list of type qualifiers for this decl (such as for declaring
1515 const member functions).
1517 This is done during the parsing of the struct declaration.
1518 The _DECL nodes are chained together and the lot of them
1519 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1521 If class A defines that certain functions in class B are friends, then
1522 the way I have set things up, it is B who is interested in permission
1523 granted by A. However, it is in A's context that these declarations
1524 are parsed. By returning a void_type_node, class A does not attempt
1525 to incorporate the declarations of the friends within its structure.
1527 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1528 CHANGES TO CODE IN `start_method'. */
1531 grokfield (declarator
, declspecs
, init
, asmspec_tree
, attrlist
)
1532 tree declarator
, declspecs
, init
, asmspec_tree
, attrlist
;
1534 register tree value
;
1535 const char *asmspec
= 0;
1536 int flags
= LOOKUP_ONLYCONVERTING
;
1538 /* Convert () initializers to = initializers. */
1539 if (init
== NULL_TREE
&& declarator
!= NULL_TREE
1540 && TREE_CODE (declarator
) == CALL_EXPR
1541 && TREE_OPERAND (declarator
, 0)
1542 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
1543 || TREE_CODE (TREE_OPERAND (declarator
, 0)) == SCOPE_REF
)
1544 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator
)))
1546 /* It's invalid to try to initialize a data member using a
1547 functional notation, e.g.:
1553 Explain that to the user. */
1554 static int explained
;
1556 cp_error ("invalid data member initialization");
1559 cp_error ("(use `=' to initialize static data members)");
1563 declarator
= TREE_OPERAND (declarator
, 0);
1567 if (declspecs
== NULL_TREE
1568 && TREE_CODE (declarator
) == SCOPE_REF
1569 && TREE_CODE (TREE_OPERAND (declarator
, 1)) == IDENTIFIER_NODE
)
1571 /* Access declaration */
1572 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator
, 0))))
1574 else if (TREE_COMPLEXITY (declarator
) == current_class_depth
)
1575 pop_nested_class ();
1576 return do_class_using_decl (declarator
);
1580 && TREE_CODE (init
) == TREE_LIST
1581 && TREE_VALUE (init
) == error_mark_node
1582 && TREE_CHAIN (init
) == NULL_TREE
)
1585 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, attrlist
);
1586 if (! value
|| value
== error_mark_node
)
1587 /* friend or constructor went bad. */
1589 if (TREE_TYPE (value
) == error_mark_node
)
1590 return error_mark_node
;
1592 /* Pass friendly classes back. */
1593 if (TREE_CODE (value
) == VOID_TYPE
)
1594 return void_type_node
;
1596 if (DECL_NAME (value
) != NULL_TREE
1597 && IDENTIFIER_POINTER (DECL_NAME (value
))[0] == '_'
1598 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value
)), "_vptr"))
1599 cp_error ("member `%D' conflicts with virtual function table field name",
1602 /* Stash away type declarations. */
1603 if (TREE_CODE (value
) == TYPE_DECL
)
1605 DECL_NONLOCAL (value
) = 1;
1606 DECL_CONTEXT (value
) = current_class_type
;
1608 if (CLASS_TYPE_P (TREE_TYPE (value
)))
1609 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value
)) = 1;
1611 if (processing_template_decl
)
1612 value
= push_template_decl (value
);
1617 if (DECL_IN_AGGR_P (value
))
1619 cp_error ("`%D' is already defined in `%T'", value
,
1620 DECL_CONTEXT (value
));
1621 return void_type_node
;
1625 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
1629 if (TREE_CODE (value
) == FUNCTION_DECL
)
1631 grok_function_init (value
, init
);
1634 else if (pedantic
&& TREE_CODE (value
) != VAR_DECL
)
1635 /* Already complained in grokdeclarator. */
1639 /* We allow initializers to become parameters to base
1641 if (TREE_CODE (init
) == TREE_LIST
)
1643 if (TREE_CHAIN (init
) == NULL_TREE
)
1644 init
= TREE_VALUE (init
);
1646 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
1649 if (TREE_CODE (init
) == CONST_DECL
)
1650 init
= DECL_INITIAL (init
);
1651 else if (TREE_READONLY_DECL_P (init
))
1652 init
= decl_constant_value (init
);
1653 else if (TREE_CODE (init
) == CONSTRUCTOR
)
1654 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
1655 if (init
== error_mark_node
)
1656 /* We must make this look different than `error_mark_node'
1657 because `decl_const_value' would mis-interpret it
1658 as only meaning that this VAR_DECL is defined. */
1659 init
= build1 (NOP_EXPR
, TREE_TYPE (value
), init
);
1660 else if (processing_template_decl
)
1662 else if (! TREE_CONSTANT (init
))
1664 /* We can allow references to things that are effectively
1665 static, since references are initialized with the address. */
1666 if (TREE_CODE (TREE_TYPE (value
)) != REFERENCE_TYPE
1667 || (TREE_STATIC (init
) == 0
1668 && (!DECL_P (init
) || DECL_EXTERNAL (init
) == 0)))
1670 error ("field initializer is not constant");
1671 init
= error_mark_node
;
1677 if (processing_template_decl
&& ! current_function_decl
1678 && (TREE_CODE (value
) == VAR_DECL
|| TREE_CODE (value
) == FUNCTION_DECL
))
1679 value
= push_template_decl (value
);
1682 cplus_decl_attributes (value
, TREE_PURPOSE (attrlist
),
1683 TREE_VALUE (attrlist
));
1685 if (TREE_CODE (value
) == VAR_DECL
)
1687 finish_static_data_member_decl (value
, init
, asmspec_tree
,
1691 if (TREE_CODE (value
) == FIELD_DECL
)
1694 cp_error ("`asm' specifiers are not permitted on non-static data members");
1695 if (DECL_INITIAL (value
) == error_mark_node
)
1696 init
= error_mark_node
;
1697 cp_finish_decl (value
, init
, NULL_TREE
, flags
);
1698 DECL_INITIAL (value
) = init
;
1699 DECL_IN_AGGR_P (value
) = 1;
1702 if (TREE_CODE (value
) == FUNCTION_DECL
)
1706 /* This must override the asm specifier which was placed
1707 by grokclassfn. Lay this out fresh. */
1708 SET_DECL_RTL (value
, NULL_RTX
);
1709 SET_DECL_ASSEMBLER_NAME (value
, get_identifier (asmspec
));
1711 cp_finish_decl (value
, init
, asmspec_tree
, flags
);
1713 /* Pass friends back this way. */
1714 if (DECL_FRIEND_P (value
))
1715 return void_type_node
;
1717 DECL_IN_AGGR_P (value
) = 1;
1720 my_friendly_abort (21);
1725 /* Like `grokfield', but for bitfields.
1726 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1729 grokbitfield (declarator
, declspecs
, width
)
1730 tree declarator
, declspecs
, width
;
1732 register tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1735 if (! value
) return NULL_TREE
; /* friends went bad. */
1737 /* Pass friendly classes back. */
1738 if (TREE_CODE (value
) == VOID_TYPE
)
1739 return void_type_node
;
1741 if (TREE_CODE (value
) == TYPE_DECL
)
1743 cp_error ("cannot declare `%D' to be a bitfield type", value
);
1747 /* Usually, finish_struct_1 catches bitifields with invalid types.
1748 But, in the case of bitfields with function type, we confuse
1749 ourselves into thinking they are member functions, so we must
1751 if (TREE_CODE (value
) == FUNCTION_DECL
)
1753 cp_error ("cannot declare bitfield `%D' with funcion type",
1758 if (DECL_IN_AGGR_P (value
))
1760 cp_error ("`%D' is already defined in the class %T", value
,
1761 DECL_CONTEXT (value
));
1762 return void_type_node
;
1765 GNU_xref_member (current_class_name
, value
);
1767 if (TREE_STATIC (value
))
1769 cp_error ("static member `%D' cannot be a bitfield", value
);
1772 cp_finish_decl (value
, NULL_TREE
, NULL_TREE
, 0);
1774 if (width
!= error_mark_node
)
1776 constant_expression_warning (width
);
1777 DECL_INITIAL (value
) = width
;
1778 SET_DECL_C_BIT_FIELD (value
);
1781 DECL_IN_AGGR_P (value
) = 1;
1786 grokoptypename (declspecs
, declarator
)
1787 tree declspecs
, declarator
;
1789 tree t
= grokdeclarator (declarator
, declspecs
, TYPENAME
, 0, NULL_TREE
);
1790 return mangle_conv_op_name_for_type (t
);
1793 /* When a function is declared with an initializer,
1794 do the right thing. Currently, there are two possibilities:
1799 // initialization possibility #1.
1800 virtual void f () = 0;
1822 // initialization possibility #2
1829 copy_assignment_arg_p (parmtype
, virtualp
)
1831 int virtualp ATTRIBUTE_UNUSED
;
1833 if (current_class_type
== NULL_TREE
)
1836 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
1837 parmtype
= TREE_TYPE (parmtype
);
1839 if ((TYPE_MAIN_VARIANT (parmtype
) == current_class_type
)
1841 /* Non-standard hack to support old Booch components. */
1842 || (! virtualp
&& DERIVED_FROM_P (parmtype
, current_class_type
))
1851 grok_function_init (decl
, init
)
1855 /* An initializer for a function tells how this function should
1857 tree type
= TREE_TYPE (decl
);
1859 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1860 cp_error ("initializer specified for non-member function `%D'", decl
);
1861 else if (integer_zerop (init
))
1863 DECL_PURE_VIRTUAL_P (decl
) = 1;
1864 if (DECL_OVERLOADED_OPERATOR_P (decl
) == NOP_EXPR
)
1867 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
))));
1869 if (copy_assignment_arg_p (parmtype
, 1))
1870 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type
) = 1;
1874 cp_error ("invalid initializer for virtual method `%D'", decl
);
1878 cplus_decl_attributes (decl
, attributes
, prefix_attributes
)
1879 tree decl
, attributes
, prefix_attributes
;
1881 if (decl
== NULL_TREE
|| decl
== void_type_node
)
1884 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1885 decl
= DECL_TEMPLATE_RESULT (decl
);
1887 decl_attributes (decl
, attributes
, prefix_attributes
);
1889 if (TREE_CODE (decl
) == TYPE_DECL
)
1890 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl
), TREE_TYPE (decl
));
1893 /* CONSTRUCTOR_NAME:
1894 Return the name for the constructor (or destructor) for the
1895 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1896 IDENTIFIER_NODE. When given a template, this routine doesn't
1897 lose the specialization. */
1900 constructor_name_full (thing
)
1903 if (TREE_CODE (thing
) == TEMPLATE_TYPE_PARM
1904 || TREE_CODE (thing
) == BOUND_TEMPLATE_TEMPLATE_PARM
1905 || TREE_CODE (thing
) == TYPENAME_TYPE
)
1906 thing
= TYPE_NAME (thing
);
1907 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing
)))
1909 if (TYPE_WAS_ANONYMOUS (thing
) && TYPE_HAS_CONSTRUCTOR (thing
))
1910 thing
= DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing
), 0)));
1912 thing
= TYPE_NAME (thing
);
1914 if (TREE_CODE (thing
) == TYPE_DECL
1915 || (TREE_CODE (thing
) == TEMPLATE_DECL
1916 && TREE_CODE (DECL_TEMPLATE_RESULT (thing
)) == TYPE_DECL
))
1917 thing
= DECL_NAME (thing
);
1918 my_friendly_assert (TREE_CODE (thing
) == IDENTIFIER_NODE
, 197);
1922 /* CONSTRUCTOR_NAME:
1923 Return the name for the constructor (or destructor) for the
1924 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1925 IDENTIFIER_NODE. When given a template, return the plain
1926 unspecialized name. */
1929 constructor_name (thing
)
1933 thing
= constructor_name_full (thing
);
1934 t
= IDENTIFIER_TEMPLATE (thing
);
1940 /* Defer the compilation of the FN until the end of compilation. */
1946 if (DECL_DEFERRED_FN (fn
))
1948 DECL_DEFERRED_FN (fn
) = 1;
1950 VARRAY_TREE_INIT (deferred_fns
, 32, "deferred_fns");
1952 VARRAY_PUSH_TREE (deferred_fns
, fn
);
1955 /* Hand off a unique name which can be used for variable we don't really
1956 want to know about anyway, for example, the anonymous variables which
1957 are needed to make references work. Declare this thing so we can use it.
1958 The variable created will be of type TYPE, and will have internal
1962 get_temp_name (type
)
1965 char buf
[sizeof (AUTO_TEMP_FORMAT
) + 20];
1967 int toplev
= toplevel_bindings_p ();
1969 sprintf (buf
, AUTO_TEMP_FORMAT
, global_temp_name_counter
++);
1970 decl
= build_decl (VAR_DECL
, get_identifier (buf
), type
);
1971 DECL_ARTIFICIAL (decl
) = 1;
1972 TREE_USED (decl
) = 1;
1973 TREE_STATIC (decl
) = 1;
1975 decl
= pushdecl_top_level (decl
);
1977 /* If this is a local variable, then lay out its rtl now.
1978 Otherwise, callers of this function are responsible for dealing
1979 with this variable's rtl. */
1983 my_friendly_assert (DECL_INITIAL (decl
) == NULL_TREE
,
1990 /* Hunts through the global anonymous union ANON_DECL, building
1991 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1992 returns a VAR_DECL whose size is the same as the size of the
1993 ANON_DECL, if one is available. */
1996 build_anon_union_vars (anon_decl
, elems
, static_p
, external_p
)
2002 tree type
= TREE_TYPE (anon_decl
);
2003 tree main_decl
= NULL_TREE
;
2006 /* Rather than write the code to handle the non-union case,
2007 just give an error. */
2008 if (TREE_CODE (type
) != UNION_TYPE
)
2009 error ("anonymous struct not inside named type");
2011 for (field
= TYPE_FIELDS (type
);
2013 field
= TREE_CHAIN (field
))
2017 if (DECL_ARTIFICIAL (field
))
2019 if (TREE_CODE (field
) != FIELD_DECL
)
2021 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2026 if (TREE_PRIVATE (field
))
2027 cp_pedwarn_at ("private member `%#D' in anonymous union", field
);
2028 else if (TREE_PROTECTED (field
))
2029 cp_pedwarn_at ("protected member `%#D' in anonymous union", field
);
2031 if (DECL_NAME (field
) == NULL_TREE
2032 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2034 decl
= build_anon_union_vars (field
, elems
, static_p
, external_p
);
2038 else if (DECL_NAME (field
) == NULL_TREE
)
2042 decl
= build_decl (VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
2043 /* tell `pushdecl' that this is not tentative. */
2044 DECL_INITIAL (decl
) = error_mark_node
;
2045 TREE_PUBLIC (decl
) = 0;
2046 TREE_STATIC (decl
) = static_p
;
2047 DECL_EXTERNAL (decl
) = external_p
;
2048 decl
= pushdecl (decl
);
2049 DECL_INITIAL (decl
) = NULL_TREE
;
2052 /* Only write out one anon union element--choose the largest
2053 one. We used to try to find one the same size as the union,
2054 but that fails if the ABI forces us to align the union more
2056 if (main_decl
== NULL_TREE
2057 || tree_int_cst_lt (DECL_SIZE (main_decl
), DECL_SIZE (decl
)))
2060 TREE_ASM_WRITTEN (main_decl
) = 1;
2064 /* ??? This causes there to be no debug info written out
2066 TREE_ASM_WRITTEN (decl
) = 1;
2068 if (DECL_NAME (field
) == NULL_TREE
2069 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2070 /* The remainder of the processing was already done in the
2074 /* If there's a cleanup to do, it belongs in the
2075 TREE_PURPOSE of the following TREE_LIST. */
2076 *elems
= tree_cons (NULL_TREE
, decl
, *elems
);
2077 TREE_TYPE (*elems
) = type
;
2083 /* Finish off the processing of a UNION_TYPE structure. If the union is an
2084 anonymous union, then all members must be laid out together. PUBLIC_P
2085 is nonzero if this union is not declared static. */
2088 finish_anon_union (anon_union_decl
)
2089 tree anon_union_decl
;
2091 tree type
= TREE_TYPE (anon_union_decl
);
2093 int public_p
= TREE_PUBLIC (anon_union_decl
);
2094 int static_p
= TREE_STATIC (anon_union_decl
);
2095 int external_p
= DECL_EXTERNAL (anon_union_decl
);
2097 /* The VAR_DECL's context is the same as the TYPE's context. */
2098 DECL_CONTEXT (anon_union_decl
) = DECL_CONTEXT (TYPE_NAME (type
));
2100 if (TYPE_FIELDS (type
) == NULL_TREE
)
2105 error ("namespace-scope anonymous aggregates must be static");
2109 main_decl
= build_anon_union_vars (anon_union_decl
,
2110 &DECL_ANON_UNION_ELEMS (anon_union_decl
),
2111 static_p
, external_p
);
2113 if (main_decl
== NULL_TREE
)
2115 warning ("anonymous aggregate with no members");
2121 make_decl_rtl (main_decl
, 0);
2122 COPY_DECL_RTL (main_decl
, anon_union_decl
);
2123 expand_anon_union_decl (anon_union_decl
,
2125 DECL_ANON_UNION_ELEMS (anon_union_decl
));
2128 add_decl_stmt (anon_union_decl
);
2131 /* Finish processing a builtin type TYPE. It's name is NAME,
2132 its fields are in the array FIELDS. LEN is the number of elements
2133 in FIELDS minus one, or put another way, it is the maximum subscript
2136 It is given the same alignment as ALIGN_TYPE. */
2139 finish_builtin_type (type
, name
, fields
, len
, align_type
)
2148 TYPE_FIELDS (type
) = fields
[0];
2149 for (i
= 0; i
< len
; i
++)
2151 layout_type (TREE_TYPE (fields
[i
]));
2152 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
2153 TREE_CHAIN (fields
[i
]) = fields
[i
+1];
2155 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
2156 TYPE_ALIGN (type
) = TYPE_ALIGN (align_type
);
2157 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (align_type
);
2159 #if 0 /* not yet, should get fixed properly later */
2160 TYPE_NAME (type
) = make_type_decl (get_identifier (name
), type
);
2162 TYPE_NAME (type
) = build_decl (TYPE_DECL
, get_identifier (name
), type
);
2164 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
2165 layout_decl (TYPE_NAME (type
), 0);
2168 /* Auxiliary functions to make type signatures for
2169 `operator new' and `operator delete' correspond to
2170 what compiler will be expecting. */
2173 coerce_new_type (type
)
2177 tree args
= TYPE_ARG_TYPES (type
);
2179 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
2181 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
2182 e
= 1, cp_error ("`operator new' must return type `%T'", ptr_type_node
);
2184 if (!args
|| args
== void_list_node
2185 || !same_type_p (TREE_VALUE (args
), c_size_type_node
))
2188 if (args
&& args
!= void_list_node
)
2189 args
= TREE_CHAIN (args
);
2190 cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node
);
2195 args
= tree_cons (NULL_TREE
, c_size_type_node
, args
);
2198 type
= build_exception_variant
2199 (build_function_type (ptr_type_node
, args
),
2200 TYPE_RAISES_EXCEPTIONS (type
));
2208 coerce_delete_type (type
)
2212 tree args
= TYPE_ARG_TYPES (type
);
2214 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
2216 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
2217 e
= 1, cp_error ("`operator delete' must return type `%T'", void_type_node
);
2219 if (!args
|| args
== void_list_node
2220 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
2223 if (args
&& args
!= void_list_node
)
2224 args
= TREE_CHAIN (args
);
2225 cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node
);
2230 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
2233 type
= build_exception_variant
2234 (build_function_type (void_type_node
, args
),
2235 TYPE_RAISES_EXCEPTIONS (type
));
2244 mark_vtable_entries (decl
)
2247 tree entries
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
));
2249 for (; entries
; entries
= TREE_CHAIN (entries
))
2254 fnaddr
= (flag_vtable_thunks
? TREE_VALUE (entries
)
2255 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries
)));
2257 if (TREE_CODE (fnaddr
) != ADDR_EXPR
)
2258 /* This entry is an offset: a virtual base class offset, a
2259 virtual call offset, an RTTI offset, etc. */
2262 fn
= TREE_OPERAND (fnaddr
, 0);
2263 TREE_ADDRESSABLE (fn
) = 1;
2264 /* When we don't have vcall offsets, we output thunks whenever
2265 we output the vtables that contain them. With vcall offsets,
2266 we know all the thunks we'll need when we emit a virtual
2267 function, so we emit the thunks there instead. */
2268 if (DECL_THUNK_P (fn
))
2269 use_thunk (fn
, /*emit_p=*/0);
2274 /* Set DECL up to have the closest approximation of "initialized common"
2275 linkage available. */
2278 comdat_linkage (decl
)
2282 make_decl_one_only (decl
);
2283 else if (TREE_CODE (decl
) == FUNCTION_DECL
2284 || (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
)))
2285 /* We can just emit function and compiler-generated variables
2286 statically; having multiple copies is (for the most part) only
2289 There are two correctness issues, however: the address of a
2290 template instantiation with external linkage should be the
2291 same, independent of what translation unit asks for the
2292 address, and this will not hold when we emit multiple copies of
2293 the function. However, there's little else we can do.
2295 Also, by default, the typeinfo implementation for the new ABI
2296 assumes that there will be only one copy of the string used as
2297 the name for each type. Therefore, if weak symbols are
2298 unavailable, the run-time library should perform a more
2299 conservative check; it should perform a string comparison,
2300 rather than an address comparison. */
2301 TREE_PUBLIC (decl
) = 0;
2304 /* Static data member template instantiations, however, cannot
2305 have multiple copies. */
2306 if (DECL_INITIAL (decl
) == 0
2307 || DECL_INITIAL (decl
) == error_mark_node
)
2308 DECL_COMMON (decl
) = 1;
2309 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
2311 DECL_COMMON (decl
) = 1;
2312 DECL_INITIAL (decl
) = error_mark_node
;
2316 /* We can't do anything useful; leave vars for explicit
2318 DECL_EXTERNAL (decl
) = 1;
2319 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2323 if (DECL_LANG_SPECIFIC (decl
))
2324 DECL_COMDAT (decl
) = 1;
2327 /* For win32 we also want to put explicit instantiations in
2328 linkonce sections, so that they will be merged with implicit
2329 instantiations; otherwise we get duplicate symbol errors. */
2332 maybe_make_one_only (decl
)
2335 /* We used to say that this was not necessary on targets that support weak
2336 symbols, because the implicit instantiations will defer to the explicit
2337 one. However, that's not actually the case in SVR4; a strong definition
2338 after a weak one is an error. Also, not making explicit
2339 instantiations one_only means that we can end up with two copies of
2340 some template instantiations. */
2344 /* We can't set DECL_COMDAT on functions, or finish_file will think
2345 we can get away with not emitting them if they aren't used. We need
2346 to for variables so that cp_finish_decl will update their linkage,
2347 because their DECL_INITIAL may not have been set properly yet. */
2349 make_decl_one_only (decl
);
2351 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_LANG_SPECIFIC (decl
))
2352 DECL_COMDAT (decl
) = 1;
2355 /* Returns the virtual function with which the vtable for TYPE is
2356 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2364 if (TYPE_FOR_JAVA (type
)
2365 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
2366 || CLASSTYPE_INTERFACE_KNOWN (type
))
2369 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
2370 method
= TREE_CHAIN (method
))
2371 if (DECL_VINDEX (method
) != NULL_TREE
2372 && ! DECL_DECLARED_INLINE_P (method
)
2373 && (! DECL_PURE_VIRTUAL_P (method
)
2375 /* This would be nice, but we didn't think of it in time. */
2376 || DECL_DESTRUCTOR_P (method
)
2384 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2385 based on TYPE and other static flags.
2387 Note that anything public is tagged TREE_PUBLIC, whether
2388 it's public in this file or in another one. */
2391 import_export_vtable (decl
, type
, final
)
2395 if (DECL_INTERFACE_KNOWN (decl
))
2398 if (TYPE_FOR_JAVA (type
))
2400 TREE_PUBLIC (decl
) = 1;
2401 DECL_EXTERNAL (decl
) = 1;
2402 DECL_INTERFACE_KNOWN (decl
) = 1;
2404 else if (CLASSTYPE_INTERFACE_KNOWN (type
))
2406 TREE_PUBLIC (decl
) = 1;
2407 DECL_EXTERNAL (decl
) = CLASSTYPE_INTERFACE_ONLY (type
);
2408 DECL_INTERFACE_KNOWN (decl
) = 1;
2412 /* We can only wait to decide if we have real non-inline virtual
2413 functions in our class, or if we come from a template. */
2415 int found
= (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
2416 || key_method (type
));
2418 if (final
|| ! found
)
2420 comdat_linkage (decl
);
2421 DECL_EXTERNAL (decl
) = 0;
2425 TREE_PUBLIC (decl
) = 1;
2426 DECL_EXTERNAL (decl
) = 1;
2431 /* Determine whether or not we want to specifically import or export CTYPE,
2432 using various heuristics. */
2435 import_export_class (ctype
)
2438 /* -1 for imported, 1 for exported. */
2439 int import_export
= 0;
2441 /* It only makes sense to call this function at EOF. The reason is
2442 that this function looks at whether or not the first non-inline
2443 non-abstract virtual member function has been defined in this
2444 translation unit. But, we can't possibly know that until we've
2445 seen the entire translation unit. */
2446 my_friendly_assert (at_eof
, 20000226);
2448 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2451 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2452 we will have CLASSTYPE_INTERFACE_ONLY set but not
2453 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2454 heuristic because someone will supply a #pragma implementation
2455 elsewhere, and deducing it here would produce a conflict. */
2456 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
2459 if (target
.valid_type_attribute
!= NULL
)
2461 /* FIXME this should really use some sort of target-independent
2463 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
2465 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
2469 /* If we got -fno-implicit-templates, we import template classes that
2470 weren't explicitly instantiated. */
2471 if (import_export
== 0
2472 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
2473 && ! flag_implicit_templates
)
2476 /* Base our import/export status on that of the first non-inline,
2477 non-pure virtual function, if any. */
2478 if (import_export
== 0
2479 && TYPE_POLYMORPHIC_P (ctype
))
2481 tree method
= key_method (ctype
);
2483 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
2486 #ifdef MULTIPLE_SYMBOL_SPACES
2487 if (import_export
== -1)
2493 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
2494 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
2498 /* We need to describe to the assembler the relationship between
2499 a vtable and the vtable of the parent class. */
2502 output_vtable_inherit (vars
)
2508 op
[0] = XEXP (DECL_RTL (vars
), 0); /* strip the mem ref */
2510 parent
= binfo_for_vtable (vars
);
2512 if (parent
== TYPE_BINFO (DECL_CONTEXT (vars
)))
2516 parent
= get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent
)));
2517 op
[1] = XEXP (DECL_RTL (parent
), 0); /* strip the mem ref */
2520 my_friendly_abort (980826);
2522 output_asm_insn (".vtable_inherit %c0, %c1", op
);
2526 finish_vtable_vardecl (t
, data
)
2528 void *data ATTRIBUTE_UNUSED
;
2531 tree ctype
= DECL_CONTEXT (vars
);
2532 import_export_class (ctype
);
2533 import_export_vtable (vars
, ctype
, 1);
2535 if (! DECL_EXTERNAL (vars
)
2536 && DECL_NEEDED_P (vars
)
2537 && ! TREE_ASM_WRITTEN (vars
))
2539 if (TREE_TYPE (vars
) == void_type_node
)
2540 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2544 mark_vtable_entries (vars
);
2545 if (TREE_TYPE (DECL_INITIAL (vars
)) == 0)
2546 store_init_value (vars
, DECL_INITIAL (vars
));
2548 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== DWARF2_DEBUG
)
2550 /* Mark the VAR_DECL node representing the vtable itself as a
2551 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2552 It is rather important that such things be ignored because
2553 any effort to actually generate DWARF for them will run
2554 into trouble when/if we encounter code like:
2557 struct S { virtual void member (); };
2559 because the artificial declaration of the vtable itself (as
2560 manufactured by the g++ front end) will say that the vtable
2561 is a static member of `S' but only *after* the debug output
2562 for the definition of `S' has already been output. This causes
2563 grief because the DWARF entry for the definition of the vtable
2564 will try to refer back to an earlier *declaration* of the
2565 vtable as a static member of `S' and there won't be one.
2566 We might be able to arrange to have the "vtable static member"
2567 attached to the member list for `S' before the debug info for
2568 `S' get written (which would solve the problem) but that would
2569 require more intrusive changes to the g++ front end. */
2571 DECL_IGNORED_P (vars
) = 1;
2574 /* Always make vtables weak. */
2576 comdat_linkage (vars
);
2578 rest_of_decl_compilation (vars
, NULL
, 1, 1);
2581 output_vtable_inherit (vars
);
2583 /* Because we're only doing syntax-checking, we'll never end up
2584 actually marking the variable as written. */
2585 if (flag_syntax_only
)
2586 TREE_ASM_WRITTEN (vars
) = 1;
2588 /* Since we're writing out the vtable here, also write the debug
2590 note_debug_info_needed (ctype
);
2595 /* If the references to this class' vtables were optimized away, still
2596 emit the appropriate debugging information. See dfs_debug_mark. */
2597 if (DECL_COMDAT (vars
)
2598 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2599 note_debug_info_needed (ctype
);
2605 prune_vtable_vardecl (t
, data
)
2607 void *data ATTRIBUTE_UNUSED
;
2609 *t
= TREE_CHAIN (*t
);
2613 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2614 inline function or template instantiation at end-of-file. */
2617 import_export_decl (decl
)
2620 if (DECL_INTERFACE_KNOWN (decl
))
2623 if (DECL_TEMPLATE_INSTANTIATION (decl
)
2624 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
2626 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2627 if ((DECL_IMPLICIT_INSTANTIATION (decl
)
2628 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
2629 && (flag_implicit_templates
2630 || (flag_implicit_inline_templates
2631 && DECL_DECLARED_INLINE_P (decl
))))
2633 if (!TREE_PUBLIC (decl
))
2634 /* Templates are allowed to have internal linkage. See
2638 comdat_linkage (decl
);
2641 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2643 else if (DECL_FUNCTION_MEMBER_P (decl
))
2645 if (!DECL_DECLARED_INLINE_P (decl
))
2647 tree ctype
= DECL_CONTEXT (decl
);
2648 import_export_class (ctype
);
2649 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2651 DECL_NOT_REALLY_EXTERN (decl
)
2652 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2653 || (DECL_DECLARED_INLINE_P (decl
)
2654 && ! flag_implement_inlines
2655 && !DECL_VINDEX (decl
)));
2657 /* Always make artificials weak. */
2658 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
2659 comdat_linkage (decl
);
2661 maybe_make_one_only (decl
);
2665 comdat_linkage (decl
);
2667 else if (tinfo_decl_p (decl
, 0))
2669 tree ctype
= TREE_TYPE (DECL_NAME (decl
));
2671 if (IS_AGGR_TYPE (ctype
))
2672 import_export_class (ctype
);
2674 if (IS_AGGR_TYPE (ctype
) && CLASSTYPE_INTERFACE_KNOWN (ctype
)
2675 && TYPE_POLYMORPHIC_P (ctype
)
2676 /* If -fno-rtti, we're not necessarily emitting this stuff with
2677 the class, so go ahead and emit it now. This can happen
2678 when a class is used in exception handling. */
2680 /* If the type is a cv-qualified variant of a type, then we
2681 must emit the tinfo function in this translation unit
2682 since it will not be emitted when the vtable for the type
2683 is output (which is when the unqualified version is
2685 && same_type_p (ctype
, TYPE_MAIN_VARIANT (ctype
)))
2687 DECL_NOT_REALLY_EXTERN (decl
)
2688 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2689 || (DECL_DECLARED_INLINE_P (decl
)
2690 && ! flag_implement_inlines
2691 && !DECL_VINDEX (decl
)));
2693 /* Always make artificials weak. */
2695 comdat_linkage (decl
);
2697 else if (TYPE_BUILT_IN (ctype
)
2698 && same_type_p (ctype
, TYPE_MAIN_VARIANT (ctype
)))
2699 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2701 comdat_linkage (decl
);
2704 comdat_linkage (decl
);
2706 DECL_INTERFACE_KNOWN (decl
) = 1;
2710 build_cleanup (decl
)
2714 tree type
= TREE_TYPE (decl
);
2716 if (TREE_CODE (type
) == ARRAY_TYPE
)
2720 mark_addressable (decl
);
2721 temp
= build1 (ADDR_EXPR
, build_pointer_type (type
), decl
);
2723 temp
= build_delete (TREE_TYPE (temp
), temp
,
2724 sfk_complete_destructor
,
2725 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
2729 /* Returns the initialization guard variable for the variable DECL,
2730 which has static storage duration. */
2739 sname
= mangle_guard_variable (decl
);
2740 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
2745 /* Under the new ABI, we use a type that is big enough to
2746 contain a mutex as well as an integer counter. */
2747 guard_type
= long_long_integer_type_node
;
2748 guard
= build_decl (VAR_DECL
, sname
, guard_type
);
2750 /* The guard should have the same linkage as what it guards. */
2751 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
2752 TREE_STATIC (guard
) = TREE_STATIC (decl
);
2753 DECL_COMMON (guard
) = DECL_COMMON (decl
);
2754 DECL_ONE_ONLY (guard
) = DECL_ONE_ONLY (decl
);
2755 if (TREE_PUBLIC (decl
))
2756 DECL_WEAK (guard
) = DECL_WEAK (decl
);
2758 DECL_ARTIFICIAL (guard
) = 1;
2759 TREE_USED (guard
) = 1;
2760 pushdecl_top_level (guard
);
2761 cp_finish_decl (guard
, NULL_TREE
, NULL_TREE
, 0);
2766 /* Return those bits of the GUARD variable that should be set when the
2767 guarded entity is actually initialized. */
2770 get_guard_bits (guard
)
2773 /* Under the new ABI, we only set the first byte of the guard,
2774 in order to leave room for a mutex in the high-order bits. */
2775 guard
= build1 (ADDR_EXPR
,
2776 build_pointer_type (TREE_TYPE (guard
)),
2778 guard
= build1 (NOP_EXPR
,
2779 build_pointer_type (char_type_node
),
2781 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
2786 /* Return an expression which determines whether or not the GUARD
2787 variable has already been initialized. */
2790 get_guard_cond (guard
)
2795 /* Check to see if the GUARD is zero. */
2796 guard
= get_guard_bits (guard
);
2797 guard_value
= integer_zero_node
;
2798 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
2799 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
2800 return cp_build_binary_op (EQ_EXPR
, guard
, guard_value
);
2803 /* Return an expression which sets the GUARD variable, indicating that
2804 the variable being guarded has been initialized. */
2812 /* Set the GUARD to one. */
2813 guard
= get_guard_bits (guard
);
2814 guard_init
= integer_one_node
;
2815 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
2816 guard_init
= convert (TREE_TYPE (guard
), guard_init
);
2817 return build_modify_expr (guard
, NOP_EXPR
, guard_init
);
2820 /* Start the process of running a particular set of global constructors
2821 or destructors. Subroutine of do_[cd]tors. */
2824 start_objects (method_type
, initp
)
2825 int method_type
, initp
;
2831 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2833 if (initp
!= DEFAULT_INIT_PRIORITY
)
2843 sprintf (type
, "%c%c%.5u", method_type
, joiner
, initp
);
2846 sprintf (type
, "%c", method_type
);
2848 fnname
= get_file_function_name_long (type
);
2850 start_function (void_list_node
,
2851 make_call_declarator (fnname
, void_list_node
, NULL_TREE
,
2853 NULL_TREE
, SF_DEFAULT
);
2855 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2856 /* It can be a static function as long as collect2 does not have
2857 to scan the object file to find its ctor/dtor routine. */
2858 TREE_PUBLIC (current_function_decl
) = 0;
2861 /* Mark this declaration as used to avoid spurious warnings. */
2862 TREE_USED (current_function_decl
) = 1;
2864 /* Mark this function as a global constructor or destructor. */
2865 if (method_type
== 'I')
2866 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
2868 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
2869 GLOBAL_INIT_PRIORITY (current_function_decl
) = initp
;
2871 body
= begin_compound_stmt (/*has_no_scope=*/0);
2873 /* We cannot allow these functions to be elided, even if they do not
2874 have external linkage. And, there's no point in deferring
2875 copmilation of thes functions; they're all going to have to be
2877 current_function_cannot_inline
2878 = "static constructors and destructors cannot be inlined";
2883 /* Finish the process of running a particular set of global constructors
2884 or destructors. Subroutine of do_[cd]tors. */
2887 finish_objects (method_type
, initp
, body
)
2888 int method_type
, initp
;
2895 finish_compound_stmt (/*has_no_scope=*/0, body
);
2896 fn
= finish_function (0);
2899 /* When only doing semantic analysis, and no RTL generation, we
2900 can't call functions that directly emit assembly code; there is
2901 no assembly file in which to put the code. */
2902 if (flag_syntax_only
)
2905 fnname
= XSTR (XEXP (DECL_RTL (fn
), 0), 0);
2906 if (initp
== DEFAULT_INIT_PRIORITY
)
2908 if (method_type
== 'I')
2909 assemble_constructor (fnname
);
2911 assemble_destructor (fnname
);
2913 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
2914 /* If we're using init priority we can't use assemble_*tor, but on ELF
2915 targets we can stick the references into named sections for GNU ld
2920 sprintf (buf
, ".%ctors.%.5u", method_type
== 'I' ? 'c' : 'd',
2921 /* invert the numbering so the linker puts us in the proper
2922 order; constructors are run from right to left, and the
2923 linker sorts in increasing order. */
2924 MAX_INIT_PRIORITY
- initp
);
2925 named_section (NULL_TREE
, buf
, 0);
2926 assemble_integer (XEXP (DECL_RTL (fn
), 0),
2927 POINTER_SIZE
/ BITS_PER_UNIT
, 1);
2932 /* The names of the parameters to the function created to handle
2933 initializations and destructions for objects with static storage
2935 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2936 #define PRIORITY_IDENTIFIER "__priority"
2938 /* The name of the function we create to handle initializations and
2939 destructions for objects with static storage duration. */
2940 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2942 /* The declaration for the __INITIALIZE_P argument. */
2943 static tree initialize_p_decl
;
2945 /* The declaration for the __PRIORITY argument. */
2946 static tree priority_decl
;
2948 /* The declaration for the static storage duration function. */
2949 static tree ssdf_decl
;
2951 /* All the static storage duration functions created in this
2952 translation unit. */
2953 static varray_type ssdf_decls
;
2955 /* A map from priority levels to information about that priority
2956 level. There may be many such levels, so efficient lookup is
2958 static splay_tree priority_info_map
;
2960 /* Begins the generation of the function that will handle all
2961 initialization and destruction of objects with static storage
2962 duration. The function generated takes two parameters of type
2963 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2964 non-zero, it performs initializations. Otherwise, it performs
2965 destructions. It only performs those initializations or
2966 destructions with the indicated __PRIORITY. The generated function
2969 It is assumed that this function will only be called once per
2970 translation unit. */
2973 start_static_storage_duration_function ()
2975 static unsigned ssdf_number
;
2980 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
2982 /* Create the identifier for this function. It will be of the form
2983 SSDF_IDENTIFIER_<number>. */
2984 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, ssdf_number
++);
2985 if (ssdf_number
== 0)
2987 /* Overflow occurred. That means there are at least 4 billion
2988 initialization functions. */
2989 sorry ("too many initialization functions required");
2990 my_friendly_abort (19990430);
2993 /* Create the parameters. */
2994 parm_types
= void_list_node
;
2995 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
2996 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
2997 type
= build_function_type (void_type_node
, parm_types
);
2999 /* Create the FUNCTION_DECL itself. */
3000 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3001 get_identifier (id
),
3003 TREE_PUBLIC (ssdf_decl
) = 0;
3004 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3006 /* Put this function in the list of functions to be called from the
3007 static constructors and destructors. */
3010 VARRAY_TREE_INIT (ssdf_decls
, 32, "ssdf_decls");
3012 /* Take this opportunity to initialize the map from priority
3013 numbers to information about that priority level. */
3014 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3015 /*delete_key_fn=*/0,
3016 /*delete_value_fn=*/
3017 (splay_tree_delete_value_fn
) &free
);
3019 /* We always need to generate functions for the
3020 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3021 priorities later, we'll be sure to find the
3022 DEFAULT_INIT_PRIORITY. */
3023 get_priority_info (DEFAULT_INIT_PRIORITY
);
3026 VARRAY_PUSH_TREE (ssdf_decls
, ssdf_decl
);
3028 /* Create the argument list. */
3029 initialize_p_decl
= build_decl (PARM_DECL
,
3030 get_identifier (INITIALIZE_P_IDENTIFIER
),
3032 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
3033 DECL_ARG_TYPE (initialize_p_decl
) = integer_type_node
;
3034 TREE_USED (initialize_p_decl
) = 1;
3035 priority_decl
= build_decl (PARM_DECL
, get_identifier (PRIORITY_IDENTIFIER
),
3037 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
3038 DECL_ARG_TYPE (priority_decl
) = integer_type_node
;
3039 TREE_USED (priority_decl
) = 1;
3041 TREE_CHAIN (initialize_p_decl
) = priority_decl
;
3042 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3044 /* Put the function in the global scope. */
3045 pushdecl (ssdf_decl
);
3047 /* Start the function itself. This is equivalent to declarating the
3050 static void __ssdf (int __initialize_p, init __priority_p);
3052 It is static because we only need to call this function from the
3053 various constructor and destructor functions for this module. */
3054 start_function (/*specs=*/NULL_TREE
,
3056 /*attrs=*/NULL_TREE
,
3059 /* Set up the scope of the outermost block in the function. */
3060 body
= begin_compound_stmt (/*has_no_scope=*/0);
3062 /* This function must not be deferred because we are depending on
3063 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3064 current_function_cannot_inline
3065 = "static storage duration functions cannot be inlined";
3070 /* Finish the generation of the function which performs initialization
3071 and destruction of objects with static storage duration. After
3072 this point, no more such objects can be created. */
3075 finish_static_storage_duration_function (body
)
3078 /* Close out the function. */
3079 finish_compound_stmt (/*has_no_scope=*/0, body
);
3080 expand_body (finish_function (0));
3083 /* Return the information about the indicated PRIORITY level. If no
3084 code to handle this level has yet been generated, generate the
3085 appropriate prologue. */
3087 static priority_info
3088 get_priority_info (priority
)
3094 n
= splay_tree_lookup (priority_info_map
,
3095 (splay_tree_key
) priority
);
3098 /* Create a new priority information structure, and insert it
3100 pi
= (priority_info
) xmalloc (sizeof (struct priority_info_s
));
3101 pi
->initializations_p
= 0;
3102 pi
->destructions_p
= 0;
3103 splay_tree_insert (priority_info_map
,
3104 (splay_tree_key
) priority
,
3105 (splay_tree_value
) pi
);
3108 pi
= (priority_info
) n
->value
;
3113 /* Set up to handle the initialization or destruction of DECL. If
3114 INITP is non-zero, we are initializing the variable. Otherwise, we
3115 are destroying it. */
3118 start_static_initialization_or_destruction (decl
, initp
)
3122 tree guard_if_stmt
= NULL_TREE
;
3129 /* Figure out the priority for this declaration. */
3130 priority
= DECL_INIT_PRIORITY (decl
);
3132 priority
= DEFAULT_INIT_PRIORITY
;
3134 /* Remember that we had an initialization or finalization at this
3136 pi
= get_priority_info (priority
);
3138 pi
->initializations_p
= 1;
3140 pi
->destructions_p
= 1;
3142 /* Trick the compiler into thinking we are at the file and line
3143 where DECL was declared so that error-messages make sense, and so
3144 that the debugger will show somewhat sensible file and line
3146 input_filename
= DECL_SOURCE_FILE (decl
);
3147 lineno
= DECL_SOURCE_LINE (decl
);
3153 Access control for implicit calls to the constructors,
3154 the conversion functions, or the destructor called to
3155 create and destroy a static data member is performed as
3156 if these calls appeared in the scope of the member's
3159 we pretend we are in a static member function of the class of
3160 which the DECL is a member. */
3161 if (member_p (decl
))
3163 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3164 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3167 /* Conditionalize this initialization on being in the right priority
3168 and being initializing/finalizing appropriately. */
3169 guard_if_stmt
= begin_if_stmt ();
3170 cond
= cp_build_binary_op (EQ_EXPR
,
3172 build_int_2 (priority
, 0));
3173 init_cond
= initp
? integer_one_node
: integer_zero_node
;
3174 init_cond
= cp_build_binary_op (EQ_EXPR
,
3177 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, init_cond
);
3179 /* Assume we don't need a guard. */
3181 /* We need a guard if this is an object with external linkage that
3182 might be initialized in more than one place. (For example, a
3183 static data member of a template, when the data member requires
3185 if (TREE_PUBLIC (decl
) && (DECL_COMMON (decl
)
3186 || DECL_ONE_ONLY (decl
)
3187 || DECL_WEAK (decl
)))
3191 guard
= get_guard (decl
);
3193 /* When using __cxa_atexit, we just check the GUARD as we would
3194 for a local static. */
3195 if (flag_use_cxa_atexit
)
3197 /* When using __cxa_atexit, we never try to destroy
3198 anything from a static destructor. */
3199 my_friendly_assert (initp
, 20000629);
3200 guard_cond
= get_guard_cond (guard
);
3202 /* If we don't have __cxa_atexit, then we will be running
3203 destructors from .fini sections, or their equivalents. So,
3204 we need to know how many times we've tried to initialize this
3205 object. We do initializations only if the GUARD is zero,
3206 i.e., if we are the first to initialize the variable. We do
3207 destructions only if the GUARD is one, i.e., if we are the
3208 last to destroy the variable. */
3211 = cp_build_binary_op (EQ_EXPR
,
3212 build_unary_op (PREINCREMENT_EXPR
,
3218 = cp_build_binary_op (EQ_EXPR
,
3219 build_unary_op (PREDECREMENT_EXPR
,
3224 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, guard_cond
);
3227 finish_if_stmt_cond (cond
, guard_if_stmt
);
3229 /* If we're using __cxa_atexit, we have not already set the GUARD,
3230 so we must do so now. */
3231 if (guard
&& initp
&& flag_use_cxa_atexit
)
3232 finish_expr_stmt (set_guard (guard
));
3234 return guard_if_stmt
;
3237 /* We've just finished generating code to do an initialization or
3238 finalization. GUARD_IF_STMT is the if-statement we used to guard
3239 the initialization. */
3242 finish_static_initialization_or_destruction (guard_if_stmt
)
3245 finish_then_clause (guard_if_stmt
);
3248 /* Now that we're done with DECL we don't need to pretend to be a
3249 member of its class any longer. */
3250 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3251 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3254 /* Generate code to do the static initialization of DECL. The
3255 initialization is INIT. If DECL may be initialized more than once
3256 in different object files, GUARD is the guard variable to
3257 check. PRIORITY is the priority for the initialization. */
3260 do_static_initialization (decl
, init
)
3267 /* Set up for the initialization. */
3269 = start_static_initialization_or_destruction (decl
,
3272 /* Do the initialization itself. */
3273 if (IS_AGGR_TYPE (TREE_TYPE (decl
))
3274 || TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
3275 expr
= build_aggr_init (decl
, init
, 0);
3278 expr
= build (INIT_EXPR
, TREE_TYPE (decl
), decl
, init
);
3279 TREE_SIDE_EFFECTS (expr
) = 1;
3281 finish_expr_stmt (expr
);
3283 /* If we're using __cxa_atexit, register a a function that calls the
3284 destructor for the object. */
3285 if (flag_use_cxa_atexit
)
3286 register_dtor_fn (decl
);
3289 finish_static_initialization_or_destruction (guard_if_stmt
);
3292 /* Generate code to do the static destruction of DECL. If DECL may be
3293 initialized more than once in different object files, GUARD is the
3294 guard variable to check. PRIORITY is the priority for the
3298 do_static_destruction (decl
)
3303 /* If we're using __cxa_atexit, then destructors are registered
3304 immediately after objects are initialized. */
3305 my_friendly_assert (!flag_use_cxa_atexit
, 20000121);
3307 /* If we don't need a destructor, there's nothing to do. */
3308 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3311 /* Actually do the destruction. */
3312 guard_if_stmt
= start_static_initialization_or_destruction (decl
,
3314 finish_expr_stmt (build_cleanup (decl
));
3315 finish_static_initialization_or_destruction (guard_if_stmt
);
3318 /* VARS is a list of variables with static storage duration which may
3319 need initialization and/or finalization. Remove those variables
3320 that don't really need to be initialized or finalized, and return
3321 the resulting list. The order in which the variables appear in
3322 VARS is in reverse order of the order in which they should actually
3323 be initialized. The list we return is in the unreversed order;
3324 i.e., the first variable should be initialized first. */
3327 prune_vars_needing_no_initialization (vars
)
3333 for (var
= vars
, result
= NULL_TREE
;
3335 var
= TREE_CHAIN (var
))
3337 tree decl
= TREE_VALUE (var
);
3338 tree init
= TREE_PURPOSE (var
);
3340 /* Deal gracefully with error. */
3341 if (decl
== error_mark_node
)
3344 /* The only things that can be initialized are variables. */
3345 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
, 19990420);
3347 /* If this object is not defined, we don't need to do anything
3349 if (DECL_EXTERNAL (decl
))
3352 /* Also, if the initializer already contains errors, we can bail
3354 if (init
&& TREE_CODE (init
) == TREE_LIST
3355 && value_member (error_mark_node
, init
))
3358 /* This variable is going to need initialization and/or
3359 finalization, so we add it to the list. */
3360 result
= tree_cons (init
, decl
, result
);
3366 /* Make sure we have told the back end about all the variables in
3370 write_out_vars (vars
)
3375 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3376 if (! TREE_ASM_WRITTEN (TREE_VALUE (v
)))
3377 rest_of_decl_compilation (TREE_VALUE (v
), 0, 1, 1);
3380 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3381 (otherwise) that will initialize all gobal objects with static
3382 storage duration having the indicated PRIORITY. */
3385 generate_ctor_or_dtor_function (constructor_p
, priority
)
3394 /* We use `I' to indicate initialization and `D' to indicate
3401 /* Begin the function. */
3402 body
= start_objects (function_key
, priority
);
3404 /* Call the static storage duration function with appropriate
3406 for (i
= 0; i
< ssdf_decls
->elements_used
; ++i
)
3408 arguments
= tree_cons (NULL_TREE
, build_int_2 (priority
, 0),
3410 arguments
= tree_cons (NULL_TREE
, build_int_2 (constructor_p
, 0),
3412 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls
, i
),
3416 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3417 calls to any functions marked with attributes indicating that
3418 they should be called at initialization- or destruction-time. */
3419 if (priority
== DEFAULT_INIT_PRIORITY
)
3423 for (fns
= constructor_p
? static_ctors
: static_dtors
;
3425 fns
= TREE_CHAIN (fns
))
3426 finish_expr_stmt (build_function_call (TREE_VALUE (fns
), NULL_TREE
));
3429 /* Close out the function. */
3430 finish_objects (function_key
, priority
, body
);
3433 /* Generate constructor and destructor functions for the priority
3437 generate_ctor_and_dtor_functions_for_priority (n
, data
)
3439 void *data ATTRIBUTE_UNUSED
;
3441 int priority
= (int) n
->key
;
3442 priority_info pi
= (priority_info
) n
->value
;
3444 /* Generate the functions themselves, but only if they are really
3446 if (pi
->initializations_p
3447 || (priority
== DEFAULT_INIT_PRIORITY
&& static_ctors
))
3448 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3450 if (pi
->destructions_p
3451 || (priority
== DEFAULT_INIT_PRIORITY
&& static_dtors
))
3452 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3455 /* Keep iterating. */
3459 /* This routine is called from the last rule in yyparse ().
3460 Its job is to create all the code needed to initialize and
3461 destroy the global aggregates. We do the destruction
3462 first, since that way we only need to reverse the decls once. */
3473 /* Bad parse errors. Just forget about it. */
3474 if (! global_bindings_p () || current_class_type
|| decl_namespace_list
)
3477 /* Otherwise, GDB can get confused, because in only knows
3478 about source for LINENO-1 lines. */
3481 interface_unknown
= 1;
3484 /* We now have to write out all the stuff we put off writing out.
3487 o Template specializations that we have not yet instantiated,
3488 but which are needed.
3489 o Initialization and destruction for non-local objects with
3490 static storage duration. (Local objects with static storage
3491 duration are initialized when their scope is first entered,
3492 and are cleaned up via atexit.)
3493 o Virtual function tables.
3495 All of these may cause others to be needed. For example,
3496 instantiating one function may cause another to be needed, and
3497 generating the intiailzer for an object may cause templates to be
3498 instantiated, etc., etc. */
3500 timevar_push (TV_VARCONST
);
3502 emit_support_tinfos ();
3508 /* If there are templates that we've put off instantiating, do
3510 instantiate_pending_templates ();
3512 /* Write out virtual tables as required. Note that writing out
3513 the virtual table for a template class may cause the
3514 instantiation of members of that class. */
3515 if (walk_globals (vtable_decl_p
,
3516 finish_vtable_vardecl
,
3520 /* Write out needed type info variables. Writing out one variable
3521 might cause others to be needed. */
3522 if (walk_globals (tinfo_decl_p
, emit_tinfo_decl
, /*data=*/0))
3525 /* The list of objects with static storage duration is built up
3526 in reverse order. We clear STATIC_AGGREGATES so that any new
3527 aggregates added during the initialization of these will be
3528 initialized in the correct order when we next come around the
3530 vars
= prune_vars_needing_no_initialization (static_aggregates
);
3531 static_aggregates
= NULL_TREE
;
3537 /* We need to start a new initialization function each time
3538 through the loop. That's because we need to know which
3539 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3540 isn't computed until a function is finished, and written
3541 out. That's a deficiency in the back-end. When this is
3542 fixed, these initialization functions could all become
3543 inline, with resulting performance improvements. */
3544 tree ssdf_body
= start_static_storage_duration_function ();
3546 /* Make sure the back end knows about all the variables. */
3547 write_out_vars (vars
);
3549 /* First generate code to do all the initializations. */
3550 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3551 do_static_initialization (TREE_VALUE (v
),
3554 /* Then, generate code to do all the destructions. Do these
3555 in reverse order so that the most recently constructed
3556 variable is the first destroyed. If we're using
3557 __cxa_atexit, then we don't need to do this; functions
3558 were registered at initialization time to destroy the
3560 if (!flag_use_cxa_atexit
)
3562 vars
= nreverse (vars
);
3563 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3564 do_static_destruction (TREE_VALUE (v
));
3569 /* Finish up the static storage duration function for this
3571 finish_static_storage_duration_function (ssdf_body
);
3573 /* All those initializations and finalizations might cause
3574 us to need more inline functions, more template
3575 instantiations, etc. */
3579 /* Go through the various inline functions, and see if any need
3581 for (i
= 0; i
< deferred_fns_used
; ++i
)
3583 tree decl
= VARRAY_TREE (deferred_fns
, i
);
3584 import_export_decl (decl
);
3585 if (DECL_ARTIFICIAL (decl
) && ! DECL_INITIAL (decl
)
3587 && (! DECL_REALLY_EXTERN (decl
) || DECL_INLINE (decl
)))
3589 /* Even though we're already at the top-level, we push
3590 there again. That way, when we pop back a few lines
3591 hence, all of our state is restored. Otherwise,
3592 finish_function doesn't clean things up, and we end
3593 up with CURRENT_FUNCTION_DECL set. */
3594 push_to_top_level ();
3595 synthesize_method (decl
);
3596 pop_from_top_level ();
3601 /* We lie to the back-end, pretending that some functions are
3602 not defined when they really are. This keeps these functions
3603 from being put out unnecessarily. But, we must stop lying
3604 when the functions are referenced, or if they are not comdat
3605 since they need to be put out now. */
3606 for (i
= 0; i
< deferred_fns_used
; ++i
)
3608 tree decl
= VARRAY_TREE (deferred_fns
, i
);
3610 if (DECL_NOT_REALLY_EXTERN (decl
)
3611 && DECL_INITIAL (decl
)
3612 && DECL_NEEDED_P (decl
))
3613 DECL_EXTERNAL (decl
) = 0;
3615 /* If we're going to need to write this function out, and
3616 there's already a body for it, create RTL for it now.
3617 (There might be no body if this is a method we haven't
3618 gotten around to synthesizing yet.) */
3619 if (!DECL_EXTERNAL (decl
)
3620 && DECL_NEEDED_P (decl
)
3621 && DECL_SAVED_TREE (decl
)
3622 && !DECL_SAVED_INSNS (decl
)
3623 && !TREE_ASM_WRITTEN (decl
))
3625 int saved_not_really_extern
;
3627 /* When we call finish_function in expand_body, it will
3628 try to reset DECL_NOT_REALLY_EXTERN so we save and
3630 saved_not_really_extern
= DECL_NOT_REALLY_EXTERN (decl
);
3631 /* Generate RTL for this function now that we know we
3634 /* Undo the damage done by finish_function. */
3635 DECL_EXTERNAL (decl
) = 0;
3636 DECL_NOT_REALLY_EXTERN (decl
) = saved_not_really_extern
;
3637 /* If we're compiling -fsyntax-only pretend that this
3638 function has been written out so that we don't try to
3640 if (flag_syntax_only
)
3641 TREE_ASM_WRITTEN (decl
) = 1;
3646 if (deferred_fns_used
3647 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns
, 0),
3650 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
3653 /* Static data members are just like namespace-scope globals. */
3654 for (i
= 0; i
< pending_statics_used
; ++i
)
3656 tree decl
= VARRAY_TREE (pending_statics
, i
);
3657 if (TREE_ASM_WRITTEN (decl
))
3659 import_export_decl (decl
);
3660 if (DECL_NOT_REALLY_EXTERN (decl
) && ! DECL_IN_AGGR_P (decl
))
3661 DECL_EXTERNAL (decl
) = 0;
3664 && wrapup_global_declarations (&VARRAY_TREE (pending_statics
, 0),
3665 pending_statics_used
))
3670 /* We give C linkage to static constructors and destructors. */
3671 push_lang_context (lang_name_c
);
3673 /* Generate initialization and destruction functions for all
3674 priorities for which they are required. */
3675 if (priority_info_map
)
3676 splay_tree_foreach (priority_info_map
,
3677 generate_ctor_and_dtor_functions_for_priority
,
3680 /* We're done with the splay-tree now. */
3681 if (priority_info_map
)
3682 splay_tree_delete (priority_info_map
);
3684 /* We're done with static constructors, so we can go back to "C++"
3686 pop_lang_context ();
3688 /* Now delete from the chain of variables all virtual function tables.
3689 We output them all ourselves, because each will be treated
3690 specially. We don't do this if we're just doing semantic
3691 analysis, and not code-generation. */
3692 if (!flag_syntax_only
)
3693 walk_globals (vtable_decl_p
, prune_vtable_vardecl
, /*data=*/0);
3695 /* Now, issue warnings about static, but not defined, functions,
3696 etc., and emit debugging information. */
3697 walk_namespaces (wrapup_globals_for_namespace
, /*data=*/&reconsider
);
3698 if (pending_statics
)
3699 check_global_declarations (&VARRAY_TREE (pending_statics
, 0),
3700 pending_statics_used
);
3704 /* The entire file is now complete. If requested, dump everything
3708 FILE *stream
= dump_begin (TDI_all
, &flags
);
3712 dump_node (global_namespace
, flags
& ~TDF_SLIM
, stream
);
3713 dump_end (TDI_all
, stream
);
3717 /* If there's some tool that wants to examine the entire translation
3718 unit, let it do so now. */
3720 (*back_end_hook
) (global_namespace
);
3722 timevar_pop (TV_VARCONST
);
3724 if (flag_detailed_statistics
)
3726 dump_tree_statistics ();
3727 dump_time_statistics ();
3731 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3732 expr. Since it was parsed like a type, we need to wade through and fix
3733 that. Unfortunately, since operator() is left-associative, we can't use
3734 tail recursion. In the above example, TYPE is `A', and DECL is
3737 Maybe this shouldn't be recursive, but how often will it actually be
3741 reparse_absdcl_as_expr (type
, decl
)
3744 /* do build_functional_cast (type, NULL_TREE) at bottom */
3745 if (TREE_OPERAND (decl
, 0) == NULL_TREE
)
3746 return build_functional_cast (type
, NULL_TREE
);
3749 decl
= reparse_absdcl_as_expr (type
, TREE_OPERAND (decl
, 0));
3751 decl
= build_x_function_call (decl
, NULL_TREE
, current_class_ref
);
3753 if (TREE_CODE (decl
) == CALL_EXPR
3754 && (! TREE_TYPE (decl
)
3755 || TREE_CODE (TREE_TYPE (decl
)) != VOID_TYPE
))
3756 decl
= require_complete_type (decl
);
3761 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3762 out to be an expr. Since it was parsed like a type, we need to wade
3763 through and fix that. Since casts are right-associative, we are
3764 reversing the order, so we don't have to recurse.
3766 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3770 reparse_absdcl_as_casts (decl
, expr
)
3775 if (TREE_CODE (expr
) == CONSTRUCTOR
3776 && TREE_TYPE (expr
) == 0)
3778 type
= groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl
)));
3779 decl
= TREE_OPERAND (decl
, 0);
3781 expr
= digest_init (type
, expr
, (tree
*) 0);
3782 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
3784 int failure
= complete_array_type (type
, expr
, 1);
3786 my_friendly_abort (78);
3792 type
= groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl
)));
3793 decl
= TREE_OPERAND (decl
, 0);
3794 expr
= build_c_cast (type
, expr
);
3797 if (warn_old_style_cast
&& ! in_system_header
3798 && current_lang_name
!= lang_name_c
)
3799 warning ("use of old-style cast");
3804 /* Given plain tree nodes for an expression, build up the full semantics. */
3807 build_expr_from_tree (t
)
3810 if (t
== NULL_TREE
|| t
== error_mark_node
)
3813 switch (TREE_CODE (t
))
3815 case IDENTIFIER_NODE
:
3816 return do_identifier (t
, 0, NULL_TREE
);
3819 if (LOOKUP_EXPR_GLOBAL (t
))
3820 return do_scoped_id (TREE_OPERAND (t
, 0), 0);
3822 return do_identifier (TREE_OPERAND (t
, 0), 0, NULL_TREE
);
3824 case TEMPLATE_ID_EXPR
:
3825 return (lookup_template_function
3826 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3827 build_expr_from_tree (TREE_OPERAND (t
, 1))));
3830 return build_x_indirect_ref
3831 (build_expr_from_tree (TREE_OPERAND (t
, 0)), "unary *");
3834 return build_functional_cast
3835 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3837 case REINTERPRET_CAST_EXPR
:
3838 return build_reinterpret_cast
3839 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3841 case CONST_CAST_EXPR
:
3842 return build_const_cast
3843 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3845 case DYNAMIC_CAST_EXPR
:
3846 return build_dynamic_cast
3847 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3849 case STATIC_CAST_EXPR
:
3850 return build_static_cast
3851 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3853 case PREDECREMENT_EXPR
:
3854 case PREINCREMENT_EXPR
:
3855 case POSTDECREMENT_EXPR
:
3856 case POSTINCREMENT_EXPR
:
3860 case TRUTH_NOT_EXPR
:
3862 case CONVERT_EXPR
: /* Unary + */
3867 return build_x_unary_op (TREE_CODE (t
),
3868 build_expr_from_tree (TREE_OPERAND (t
, 0)));
3873 case TRUNC_DIV_EXPR
:
3875 case FLOOR_DIV_EXPR
:
3876 case ROUND_DIV_EXPR
:
3877 case EXACT_DIV_EXPR
:
3879 case BIT_ANDTC_EXPR
:
3882 case TRUNC_MOD_EXPR
:
3883 case FLOOR_MOD_EXPR
:
3884 case TRUTH_ANDIF_EXPR
:
3885 case TRUTH_ORIF_EXPR
:
3886 case TRUTH_AND_EXPR
:
3901 return build_x_binary_op
3903 build_expr_from_tree (TREE_OPERAND (t
, 0)),
3904 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3907 return build_m_component_ref
3908 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3909 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3912 return build_offset_ref (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1));
3915 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
3917 return build_nt (ARRAY_REF
, NULL_TREE
,
3918 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3919 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3920 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3925 tree r
= build_expr_from_tree (TREE_OPERAND (t
, 0));
3928 return TREE_CODE (t
) == SIZEOF_EXPR
? c_sizeof (r
) : c_alignof (r
);
3932 return build_x_modify_expr
3933 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3934 TREE_CODE (TREE_OPERAND (t
, 1)),
3935 build_expr_from_tree (TREE_OPERAND (t
, 2)));
3938 return build_x_arrow
3939 (build_expr_from_tree (TREE_OPERAND (t
, 0)));
3943 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3944 build_expr_from_tree (TREE_OPERAND (t
, 1)),
3945 build_expr_from_tree (TREE_OPERAND (t
, 2)),
3946 NEW_EXPR_USE_GLOBAL (t
));
3949 return delete_sanity
3950 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3951 build_expr_from_tree (TREE_OPERAND (t
, 1)),
3952 DELETE_EXPR_USE_VEC (t
), DELETE_EXPR_USE_GLOBAL (t
));
3955 if (TREE_OPERAND (t
, 1) == NULL_TREE
)
3956 return build_x_compound_expr
3957 (build_expr_from_tree (TREE_OPERAND (t
, 0)));
3959 my_friendly_abort (42);
3961 case METHOD_CALL_EXPR
:
3962 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SCOPE_REF
)
3964 tree ref
= TREE_OPERAND (t
, 0);
3965 tree name
= TREE_OPERAND (ref
, 1);
3967 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3968 name
= build_nt (TEMPLATE_ID_EXPR
,
3969 TREE_OPERAND (name
, 0),
3970 build_expr_from_tree (TREE_OPERAND (name
, 1)));
3972 return build_scoped_method_call
3973 (build_expr_from_tree (TREE_OPERAND (t
, 1)),
3974 build_expr_from_tree (TREE_OPERAND (ref
, 0)),
3976 build_expr_from_tree (TREE_OPERAND (t
, 2)));
3980 tree fn
= TREE_OPERAND (t
, 0);
3982 /* We can get a TEMPLATE_ID_EXPR here on code like:
3986 so we must resolve that. However, we can also get things
3987 like a BIT_NOT_EXPR here, when referring to a destructor,
3988 and things like that are not correctly resolved by
3989 build_expr_from_tree. So, just use build_expr_from_tree
3990 when we really need it. */
3991 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
3992 fn
= lookup_template_function
3993 (TREE_OPERAND (fn
, 0),
3994 build_expr_from_tree (TREE_OPERAND (fn
, 1)));
3996 return build_method_call
3997 (build_expr_from_tree (TREE_OPERAND (t
, 1)),
3999 build_expr_from_tree (TREE_OPERAND (t
, 2)),
4000 NULL_TREE
, LOOKUP_NORMAL
);
4004 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SCOPE_REF
)
4006 tree ref
= TREE_OPERAND (t
, 0);
4007 tree name
= TREE_OPERAND (ref
, 1);
4009 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4010 name
= build_nt (TEMPLATE_ID_EXPR
,
4011 TREE_OPERAND (name
, 0),
4012 build_expr_from_tree (TREE_OPERAND (name
, 1)));
4014 return build_member_call
4015 (build_expr_from_tree (TREE_OPERAND (ref
, 0)),
4017 build_expr_from_tree (TREE_OPERAND (t
, 1)));
4021 tree name
= TREE_OPERAND (t
, 0);
4023 tree args
= build_expr_from_tree (TREE_OPERAND (t
, 1));
4024 if (args
!= NULL_TREE
&& TREE_CODE (name
) == LOOKUP_EXPR
4025 && !LOOKUP_EXPR_GLOBAL (name
)
4026 && TREE_CODE ((id
= TREE_OPERAND (name
, 0))) == IDENTIFIER_NODE
4027 && (!current_class_type
4028 || !lookup_member (current_class_type
, id
, 0, 0)))
4030 /* Do Koenig lookup if there are no class members. */
4031 name
= do_identifier (id
, 0, args
);
4033 else if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
4034 || ! really_overloaded_fn (name
))
4035 name
= build_expr_from_tree (name
);
4036 return build_x_function_call (name
, args
, current_class_ref
);
4040 return build_x_conditional_expr
4041 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4042 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4043 build_expr_from_tree (TREE_OPERAND (t
, 2)));
4045 case PSEUDO_DTOR_EXPR
:
4046 return (finish_pseudo_destructor_call_expr
4047 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4048 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4049 build_expr_from_tree (TREE_OPERAND (t
, 2))));
4053 tree purpose
, value
, chain
;
4055 if (t
== void_list_node
)
4058 purpose
= TREE_PURPOSE (t
);
4060 purpose
= build_expr_from_tree (purpose
);
4061 value
= TREE_VALUE (t
);
4063 value
= build_expr_from_tree (value
);
4064 chain
= TREE_CHAIN (t
);
4065 if (chain
&& chain
!= void_type_node
)
4066 chain
= build_expr_from_tree (chain
);
4067 return tree_cons (purpose
, value
, chain
);
4072 tree object
= build_expr_from_tree (TREE_OPERAND (t
, 0));
4073 tree field
= TREE_OPERAND (t
, 1);
4075 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4076 and `x.A::b'. We must distinguish between those cases
4078 if (TREE_CODE (field
) == SCOPE_REF
)
4079 return build_object_ref (object
,
4080 TREE_OPERAND (field
, 0),
4081 TREE_OPERAND (field
, 1));
4083 return build_x_component_ref (object
, field
,
4088 return build_throw (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4094 /* digest_init will do the wrong thing if we let it. */
4095 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4098 r
= build_nt (CONSTRUCTOR
, NULL_TREE
,
4099 build_expr_from_tree (CONSTRUCTOR_ELTS (t
)));
4100 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
4103 return digest_init (TREE_TYPE (t
), r
, 0);
4108 if (TYPE_P (TREE_OPERAND (t
, 0)))
4109 return get_typeid (TREE_OPERAND (t
, 0));
4110 return build_typeid (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4113 return convert_from_reference (t
);
4116 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4124 /* This is something of the form `int (*a)++' that has turned out to be an
4125 expr. It was only converted into parse nodes, so we need to go through
4126 and build up the semantics. Most of the work is done by
4127 build_expr_from_tree, above.
4129 In the above example, TYPE is `int' and DECL is `*a'. */
4132 reparse_decl_as_expr (type
, decl
)
4135 decl
= build_expr_from_tree (decl
);
4137 return build_functional_cast (type
, build_tree_list (NULL_TREE
, decl
));
4142 /* This is something of the form `int (*a)' that has turned out to be a
4143 decl. It was only converted into parse nodes, so we need to do the
4144 checking that make_{pointer,reference}_declarator do. */
4147 finish_decl_parsing (decl
)
4150 switch (TREE_CODE (decl
))
4152 case IDENTIFIER_NODE
:
4155 return make_pointer_declarator
4156 (NULL_TREE
, finish_decl_parsing (TREE_OPERAND (decl
, 0)));
4158 return make_reference_declarator
4159 (NULL_TREE
, finish_decl_parsing (TREE_OPERAND (decl
, 0)));
4161 TREE_OPERAND (decl
, 0) = finish_decl_parsing (TREE_OPERAND (decl
, 0));
4164 push_nested_class (TREE_TYPE (TREE_OPERAND (decl
, 0)), 3);
4165 TREE_COMPLEXITY (decl
) = current_class_depth
;
4168 TREE_OPERAND (decl
, 0) = finish_decl_parsing (TREE_OPERAND (decl
, 0));
4171 /* For attribute handling. */
4172 TREE_VALUE (decl
) = finish_decl_parsing (TREE_VALUE (decl
));
4174 case TEMPLATE_ID_EXPR
:
4177 my_friendly_abort (5);
4182 /* Return 1 if root encloses child. */
4185 is_namespace_ancestor (root
, child
)
4190 if (root
== global_namespace
)
4192 if (child
== global_namespace
)
4194 return is_namespace_ancestor (root
, CP_DECL_CONTEXT (child
));
4198 /* Return the namespace that is the common ancestor
4199 of two given namespaces. */
4202 namespace_ancestor (ns1
, ns2
)
4205 if (is_namespace_ancestor (ns1
, ns2
))
4207 return namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
);
4210 /* Insert used into the using list of user. Set indirect_flag if this
4211 directive is not directly from the source. Also find the common
4212 ancestor and let our users know about the new namespace */
4214 add_using_namespace (user
, used
, indirect
)
4220 /* Using oneself is a no-op. */
4223 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
4224 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
4225 /* Check if we already have this. */
4226 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
4230 /* Promote to direct usage. */
4231 TREE_INDIRECT_USING (t
) = 0;
4235 /* Add used to the user's using list. */
4236 DECL_NAMESPACE_USING (user
)
4237 = tree_cons (used
, namespace_ancestor (user
, used
),
4238 DECL_NAMESPACE_USING (user
));
4240 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
4242 /* Add user to the used's users list. */
4243 DECL_NAMESPACE_USERS (used
)
4244 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
4246 /* Recursively add all namespaces used. */
4247 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
4248 /* indirect usage */
4249 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
4251 /* Tell everyone using us about the new used namespaces. */
4252 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
4253 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
4256 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4257 duplicates. The first list becomes the tail of the result.
4259 The algorithm is O(n^2). We could get this down to O(n log n) by
4260 doing a sort on the addresses of the functions, if that becomes
4264 merge_functions (s1
, s2
)
4268 for (; s2
; s2
= OVL_NEXT (s2
))
4270 tree fn
= OVL_CURRENT (s2
);
4271 if (! ovl_member (fn
, s1
))
4272 s1
= build_overload (fn
, s1
);
4277 /* This should return an error not all definitions define functions.
4278 It is not an error if we find two functions with exactly the
4279 same signature, only if these are selected in overload resolution.
4280 old is the current set of bindings, new the freshly-found binding.
4281 XXX Do we want to give *all* candidates in case of ambiguity?
4282 XXX In what way should I treat extern declarations?
4283 XXX I don't want to repeat the entire duplicate_decls here */
4286 ambiguous_decl (name
, old
, new, flags
)
4293 my_friendly_assert (old
!= NULL_TREE
, 393);
4294 /* Copy the value. */
4295 val
= BINDING_VALUE (new);
4297 switch (TREE_CODE (val
))
4300 /* If we expect types or namespaces, and not templates,
4301 or this is not a template class. */
4302 if (LOOKUP_QUALIFIERS_ONLY (flags
)
4303 && !DECL_CLASS_TEMPLATE_P (val
))
4307 if (LOOKUP_NAMESPACES_ONLY (flags
))
4310 case NAMESPACE_DECL
:
4311 if (LOOKUP_TYPES_ONLY (flags
))
4315 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4319 if (!BINDING_VALUE (old
))
4320 BINDING_VALUE (old
) = val
;
4321 else if (val
&& val
!= BINDING_VALUE (old
))
4323 if (is_overloaded_fn (BINDING_VALUE (old
))
4324 && is_overloaded_fn (val
))
4326 BINDING_VALUE (old
) = merge_functions (BINDING_VALUE (old
),
4331 /* Some declarations are functions, some are not. */
4332 if (flags
& LOOKUP_COMPLAIN
)
4334 /* If we've already given this error for this lookup,
4335 BINDING_VALUE (old) is error_mark_node, so let's not
4336 repeat ourselves. */
4337 if (BINDING_VALUE (old
) != error_mark_node
)
4339 cp_error ("use of `%D' is ambiguous", name
);
4340 cp_error_at (" first declared as `%#D' here",
4341 BINDING_VALUE (old
));
4343 cp_error_at (" also declared as `%#D' here", val
);
4345 BINDING_VALUE (old
) = error_mark_node
;
4348 /* ... and copy the type. */
4349 type
= BINDING_TYPE (new);
4350 if (LOOKUP_NAMESPACES_ONLY (flags
))
4352 if (!BINDING_TYPE (old
))
4353 BINDING_TYPE (old
) = type
;
4354 else if (type
&& BINDING_TYPE (old
) != type
)
4356 if (flags
& LOOKUP_COMPLAIN
)
4358 cp_error ("`%D' denotes an ambiguous type",name
);
4359 cp_error_at (" first type here", BINDING_TYPE (old
));
4360 cp_error_at (" other type here", type
);
4366 /* Subroutine of unualified_namespace_lookup:
4367 Add the bindings of NAME in used namespaces to VAL.
4368 We are currently looking for names in namespace SCOPE, so we
4369 look through USINGS for using-directives of namespaces
4370 which have SCOPE as a common ancestor with the current scope.
4371 Returns zero on errors. */
4374 lookup_using_namespace (name
, val
, usings
, scope
, flags
, spacesp
)
4375 tree name
, val
, usings
, scope
;
4381 /* Iterate over all used namespaces in current, searching for using
4382 directives of scope. */
4383 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
4384 if (TREE_VALUE (iter
) == scope
)
4387 *spacesp
= tree_cons (TREE_PURPOSE (iter
), NULL_TREE
,
4389 val1
= binding_for_name (name
, TREE_PURPOSE (iter
));
4390 /* Resolve ambiguities. */
4391 val
= ambiguous_decl (name
, val
, val1
, flags
);
4393 return BINDING_VALUE (val
) != error_mark_node
;
4397 Accepts the NAME to lookup and its qualifying SCOPE.
4398 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4402 qualified_lookup_using_namespace (name
, scope
, result
, flags
)
4408 /* Maintain a list of namespaces visited... */
4409 tree seen
= NULL_TREE
;
4410 /* ... and a list of namespace yet to see. */
4411 tree todo
= NULL_TREE
;
4413 /* Look through namespace aliases. */
4414 scope
= ORIGINAL_NAMESPACE (scope
);
4415 while (scope
&& (result
!= error_mark_node
))
4417 seen
= tree_cons (scope
, NULL_TREE
, seen
);
4418 result
= ambiguous_decl (name
, result
,
4419 binding_for_name (name
, scope
), flags
);
4420 if (!BINDING_VALUE (result
) && !BINDING_TYPE (result
))
4421 /* Consider using directives. */
4422 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
4423 usings
= TREE_CHAIN (usings
))
4424 /* If this was a real directive, and we have not seen it. */
4425 if (!TREE_INDIRECT_USING (usings
)
4426 && !purpose_member (TREE_PURPOSE (usings
), seen
))
4427 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
4430 scope
= TREE_PURPOSE (todo
);
4431 todo
= TREE_CHAIN (todo
);
4434 scope
= NULL_TREE
; /* If there never was a todo list. */
4436 return result
!= error_mark_node
;
4439 /* [namespace.memdef]/2 */
4441 /* Set the context of a declaration to scope. Complain if we are not
4445 set_decl_namespace (decl
, scope
, friendp
)
4451 if (scope
== fake_std_node
)
4452 scope
= global_namespace
;
4453 /* Get rid of namespace aliases. */
4454 scope
= ORIGINAL_NAMESPACE (scope
);
4456 /* It is ok for friends to be qualified in parallel space. */
4457 if (!friendp
&& !is_namespace_ancestor (current_namespace
, scope
))
4458 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4460 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4461 if (scope
!= current_namespace
)
4463 /* See whether this has been declared in the namespace. */
4464 old
= namespace_binding (DECL_NAME (decl
), scope
);
4466 /* No old declaration at all. */
4468 /* A template can be explicitly specialized in any namespace. */
4469 if (processing_explicit_instantiation
)
4471 if (!is_overloaded_fn (decl
))
4472 /* Don't compare non-function decls with decls_match here,
4473 since it can't check for the correct constness at this
4474 point. pushdecl will find those errors later. */
4476 /* Since decl is a function, old should contain a function decl. */
4477 if (!is_overloaded_fn (old
))
4479 if (processing_template_decl
|| processing_specialization
)
4480 /* We have not yet called push_template_decl to turn the
4481 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4482 won't match. But, we'll check later, when we construct the
4485 for (; old
; old
= OVL_NEXT (old
))
4486 if (decls_match (decl
, OVL_CURRENT (old
)))
4492 cp_error ("`%D' should have been declared inside `%D'",
4496 /* Compute the namespace where a declaration is defined. */
4499 decl_namespace (decl
)
4503 decl
= TYPE_STUB_DECL (decl
);
4504 while (DECL_CONTEXT (decl
))
4506 decl
= DECL_CONTEXT (decl
);
4507 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
4510 decl
= TYPE_STUB_DECL (decl
);
4511 my_friendly_assert (DECL_P (decl
), 390);
4514 return global_namespace
;
4517 /* Return the namespace where the current declaration is declared. */
4520 current_decl_namespace ()
4523 /* If we have been pushed into a different namespace, use it. */
4524 if (decl_namespace_list
)
4525 return TREE_PURPOSE (decl_namespace_list
);
4527 if (current_class_type
)
4528 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
4529 else if (current_function_decl
)
4530 result
= decl_namespace (current_function_decl
);
4532 result
= current_namespace
;
4536 /* Temporarily set the namespace for the current declaration. */
4539 push_decl_namespace (decl
)
4542 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
4543 decl
= decl_namespace (decl
);
4544 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
4545 NULL_TREE
, decl_namespace_list
);
4549 pop_decl_namespace ()
4551 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
4554 /* Enter a class or namespace scope. */
4560 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4561 push_decl_namespace (t
);
4566 /* Leave scope pushed by push_scope. */
4572 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4573 pop_decl_namespace ();
4578 /* [basic.lookup.koenig] */
4579 /* A non-zero return value in the functions below indicates an error.
4580 All nodes allocated in the procedure are on the scratch obstack. */
4590 static int arg_assoc
PARAMS ((struct arg_lookup
*, tree
));
4591 static int arg_assoc_args
PARAMS ((struct arg_lookup
*, tree
));
4592 static int arg_assoc_type
PARAMS ((struct arg_lookup
*, tree
));
4593 static int add_function
PARAMS ((struct arg_lookup
*, tree
));
4594 static int arg_assoc_namespace
PARAMS ((struct arg_lookup
*, tree
));
4595 static int arg_assoc_class
PARAMS ((struct arg_lookup
*, tree
));
4596 static int arg_assoc_template_arg
PARAMS ((struct arg_lookup
*, tree
));
4598 /* Add a function to the lookup structure.
4599 Returns 1 on error. */
4602 add_function (k
, fn
)
4603 struct arg_lookup
*k
;
4606 /* We used to check here to see if the function was already in the list,
4607 but that's O(n^2), which is just too expensive for function lookup.
4608 Now we deal with the occasional duplicate in joust. In doing this, we
4609 assume that the number of duplicates will be small compared to the
4610 total number of functions being compared, which should usually be the
4613 /* We must find only functions, or exactly one non-function. */
4616 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4617 k
->functions
= build_overload (fn
, k
->functions
);
4620 tree f1
= OVL_CURRENT (k
->functions
);
4622 if (is_overloaded_fn (f1
))
4624 fn
= f1
; f1
= f2
; f2
= fn
;
4626 cp_error_at ("`%D' is not a function,", f1
);
4627 cp_error_at (" conflict with `%D'", f2
);
4628 cp_error (" in call to `%D'", k
->name
);
4635 /* Add functions of a namespace to the lookup structure.
4636 Returns 1 on error. */
4639 arg_assoc_namespace (k
, scope
)
4640 struct arg_lookup
*k
;
4645 if (purpose_member (scope
, k
->namespaces
))
4647 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
4649 value
= namespace_binding (k
->name
, scope
);
4653 for (; value
; value
= OVL_NEXT (value
))
4654 if (add_function (k
, OVL_CURRENT (value
)))
4660 /* Adds everything associated with a template argument to the lookup
4661 structure. Returns 1 on error. */
4664 arg_assoc_template_arg (k
, arg
)
4665 struct arg_lookup
* k
;
4668 /* [basic.lookup.koenig]
4670 If T is a template-id, its associated namespaces and classes are
4671 ... the namespaces and classes associated with the types of the
4672 template arguments provided for template type parameters
4673 (excluding template template parameters); the namespaces in which
4674 any template template arguments are defined; and the classes in
4675 which any member templates used as template template arguments
4676 are defined. [Note: non-type template arguments do not
4677 contribute to the set of associated namespaces. ] */
4679 /* Consider first template template arguments. */
4680 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
4682 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4684 tree ctx
= CP_DECL_CONTEXT (arg
);
4686 /* It's not a member template. */
4687 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4688 return arg_assoc_namespace (k
, ctx
);
4689 /* Otherwise, it must be member template. */
4691 return arg_assoc_class (k
, ctx
);
4693 /* It's not a template template argument, but it is a type template
4695 else if (TYPE_P (arg
))
4696 return arg_assoc_type (k
, arg
);
4697 /* It's a non-type template argument. */
4702 /* Adds everything associated with class to the lookup structure.
4703 Returns 1 on error. */
4706 arg_assoc_class (k
, type
)
4707 struct arg_lookup
* k
;
4710 tree list
, friends
, context
;
4713 /* Backend build structures, such as __builtin_va_list, aren't
4714 affected by all this. */
4715 if (!CLASS_TYPE_P (type
))
4718 if (purpose_member (type
, k
->classes
))
4720 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4722 context
= decl_namespace (TYPE_MAIN_DECL (type
));
4723 if (arg_assoc_namespace (k
, context
))
4726 /* Process baseclasses. */
4727 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4728 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
4731 /* Process friends. */
4732 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
4733 list
= TREE_CHAIN (list
))
4734 if (k
->name
== TREE_PURPOSE (list
))
4735 for (friends
= TREE_VALUE (list
); friends
;
4736 friends
= TREE_CHAIN (friends
))
4737 /* Only interested in global functions with potentially hidden
4738 (i.e. unqualified) declarations. */
4739 if (TREE_PURPOSE (friends
) == error_mark_node
&& TREE_VALUE (friends
)
4740 && decl_namespace (TREE_VALUE (friends
)) == context
)
4741 if (add_function (k
, TREE_VALUE (friends
)))
4744 /* Process template arguments. */
4745 if (CLASSTYPE_TEMPLATE_INFO (type
))
4747 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4748 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4749 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4755 /* Adds everything associated with a given type.
4756 Returns 1 on error. */
4759 arg_assoc_type (k
, type
)
4760 struct arg_lookup
*k
;
4763 switch (TREE_CODE (type
))
4774 if (TYPE_PTRMEMFUNC_P (type
))
4775 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4776 return arg_assoc_class (k
, type
);
4778 case REFERENCE_TYPE
:
4780 return arg_assoc_type (k
, TREE_TYPE (type
));
4783 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
4785 /* Pointer to member: associate class type and value type. */
4786 if (arg_assoc_type (k
, TYPE_OFFSET_BASETYPE (type
)))
4788 return arg_assoc_type (k
, TREE_TYPE (type
));
4790 /* The basetype is referenced in the first arg type, so just
4793 /* Associate the parameter types. */
4794 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4796 /* Associate the return type. */
4797 return arg_assoc_type (k
, TREE_TYPE (type
));
4798 case TEMPLATE_TYPE_PARM
:
4799 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4804 if (type
== unknown_type_node
)
4806 /* else fall through */
4808 my_friendly_abort (390);
4813 /* Adds everything associated with arguments. Returns 1 on error. */
4816 arg_assoc_args (k
, args
)
4817 struct arg_lookup
* k
;
4820 for (; args
; args
= TREE_CHAIN (args
))
4821 if (arg_assoc (k
, TREE_VALUE (args
)))
4826 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4830 struct arg_lookup
* k
;
4833 if (n
== error_mark_node
)
4837 return arg_assoc_type (k
, n
);
4839 if (! type_unknown_p (n
))
4840 return arg_assoc_type (k
, TREE_TYPE (n
));
4842 if (TREE_CODE (n
) == ADDR_EXPR
)
4843 n
= TREE_OPERAND (n
, 0);
4844 if (TREE_CODE (n
) == COMPONENT_REF
)
4845 n
= TREE_OPERAND (n
, 1);
4846 if (TREE_CODE (n
) == OFFSET_REF
)
4847 n
= TREE_OPERAND (n
, 1);
4848 while (TREE_CODE (n
) == TREE_LIST
)
4851 if (TREE_CODE (n
) == FUNCTION_DECL
)
4852 return arg_assoc_type (k
, TREE_TYPE (n
));
4853 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4855 /* [basic.lookup.koenig]
4857 If T is a template-id, its associated namespaces and classes
4858 are the namespace in which the template is defined; for
4859 member templates, the member template's class... */
4860 tree
template = TREE_OPERAND (n
, 0);
4861 tree args
= TREE_OPERAND (n
, 1);
4865 if (TREE_CODE (template) == COMPONENT_REF
)
4866 template = TREE_OPERAND (template, 1);
4868 /* First, the template. There may actually be more than one if
4869 this is an overloaded function template. But, in that case,
4870 we only need the first; all the functions will be in the same
4872 template = OVL_CURRENT (template);
4874 ctx
= CP_DECL_CONTEXT (template);
4876 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4878 if (arg_assoc_namespace (k
, ctx
) == 1)
4881 /* It must be a member template. */
4882 else if (arg_assoc_class (k
, ctx
) == 1)
4885 /* Now the arguments. */
4886 for (arg
= args
; arg
!= NULL_TREE
; arg
= TREE_CHAIN (arg
))
4887 if (arg_assoc_template_arg (k
, TREE_VALUE (arg
)) == 1)
4892 my_friendly_assert (TREE_CODE (n
) == OVERLOAD
, 980715);
4894 for (; n
; n
= OVL_CHAIN (n
))
4895 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4902 /* Performs Koenig lookup depending on arguments, where fns
4903 are the functions found in normal lookup. */
4906 lookup_arg_dependent (name
, fns
, args
)
4911 struct arg_lookup k
;
4912 tree fn
= NULL_TREE
;
4916 k
.classes
= NULL_TREE
;
4918 /* Note that we've already looked at some namespaces during normal
4919 unqualified lookup, unless we found a decl in function scope. */
4921 fn
= OVL_CURRENT (fns
);
4922 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (fn
))
4923 k
.namespaces
= NULL_TREE
;
4925 unqualified_namespace_lookup (name
, 0, &k
.namespaces
);
4927 arg_assoc_args (&k
, args
);
4931 /* Process a namespace-alias declaration. */
4934 do_namespace_alias (alias
, namespace)
4935 tree alias
, namespace;
4937 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
4939 /* The parser did not find it, so it's not there. */
4940 cp_error ("unknown namespace `%D'", namespace);
4944 namespace = ORIGINAL_NAMESPACE (namespace);
4946 /* Build the alias. */
4947 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
4948 DECL_NAMESPACE_ALIAS (alias
) = namespace;
4952 /* Check a non-member using-declaration. Return the name and scope
4953 being used, and the USING_DECL, or NULL_TREE on failure. */
4956 validate_nonmember_using_decl (decl
, scope
, name
)
4961 if (TREE_CODE (decl
) == SCOPE_REF
4962 && TREE_OPERAND (decl
, 0) == fake_std_node
)
4964 *scope
= global_namespace
;
4965 *name
= TREE_OPERAND (decl
, 1);
4967 else if (TREE_CODE (decl
) == SCOPE_REF
)
4969 *scope
= TREE_OPERAND (decl
, 0);
4970 *name
= TREE_OPERAND (decl
, 1);
4972 /* [namespace.udecl]
4974 A using-declaration for a class member shall be a
4975 member-declaration. */
4976 if (!processing_template_decl
4977 && TREE_CODE (*scope
) != NAMESPACE_DECL
)
4979 if (TYPE_P (*scope
))
4980 cp_error ("`%T' is not a namespace", *scope
);
4982 cp_error ("`%D' is not a namespace", *scope
);
4986 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
4987 || TREE_CODE (decl
) == TYPE_DECL
4988 || TREE_CODE (decl
) == TEMPLATE_DECL
)
4990 *scope
= global_namespace
;
4994 my_friendly_abort (382);
4996 *name
= DECL_NAME (*name
);
4997 /* Make a USING_DECL. */
4998 return push_using_decl (*scope
, *name
);
5001 /* Process local and global using-declarations. */
5004 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, newval
, newtype
)
5006 tree oldval
, oldtype
;
5007 tree
*newval
, *newtype
;
5011 *newval
= *newtype
= NULL_TREE
;
5012 decls
= make_node (CPLUS_BINDING
);
5013 if (!qualified_lookup_using_namespace (name
, scope
, decls
, 0))
5017 if (!BINDING_VALUE (decls
) && !BINDING_TYPE (decls
))
5019 cp_error ("`%D' not declared", name
);
5023 /* Check for using functions. */
5024 if (BINDING_VALUE (decls
) && is_overloaded_fn (BINDING_VALUE (decls
)))
5028 if (oldval
&& !is_overloaded_fn (oldval
))
5030 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls
)), oldval
);
5035 for (tmp
= BINDING_VALUE (decls
); tmp
; tmp
= OVL_NEXT (tmp
))
5037 tree new_fn
= OVL_CURRENT (tmp
);
5039 /* [namespace.udecl]
5041 If a function declaration in namespace scope or block
5042 scope has the same name and the same parameter types as a
5043 function introduced by a using declaration the program is
5045 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
5047 tree old_fn
= OVL_CURRENT (tmp1
);
5049 if (new_fn
== old_fn
)
5050 /* The function already exists in the current namespace. */
5052 else if (OVL_USED (tmp1
))
5053 continue; /* this is a using decl */
5054 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
5055 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
5057 /* There was already a non-using declaration in
5058 this scope with the same parameter types. If both
5059 are the same extern "C" functions, that's ok. */
5060 if (!decls_match (new_fn
, old_fn
))
5061 cp_error ("`%D' is already declared in this scope", name
);
5066 /* If we broke out of the loop, there's no reason to add
5067 this function to the using declarations for this
5072 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
5073 if (TREE_CODE (*newval
) != OVERLOAD
)
5074 *newval
= ovl_cons (*newval
, NULL_TREE
);
5075 OVL_USED (*newval
) = 1;
5080 *newval
= BINDING_VALUE (decls
);
5082 duplicate_decls (*newval
, oldval
);
5085 *newtype
= BINDING_TYPE (decls
);
5086 if (oldtype
&& *newtype
&& oldtype
!= *newtype
)
5088 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5094 /* Process a using-declaration not appearing in class or local scope. */
5097 do_toplevel_using_decl (decl
)
5100 tree scope
, name
, binding
;
5101 tree oldval
, oldtype
, newval
, newtype
;
5103 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
5104 if (decl
== NULL_TREE
)
5107 binding
= binding_for_name (name
, current_namespace
);
5109 oldval
= BINDING_VALUE (binding
);
5110 oldtype
= BINDING_TYPE (binding
);
5112 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
5114 /* Copy declarations found. */
5116 BINDING_VALUE (binding
) = newval
;
5118 BINDING_TYPE (binding
) = newtype
;
5122 /* Process a using-declaration at function scope. */
5125 do_local_using_decl (decl
)
5129 tree oldval
, oldtype
, newval
, newtype
;
5131 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
5132 if (decl
== NULL_TREE
)
5135 if (building_stmt_tree ()
5136 && at_function_scope_p ())
5137 add_decl_stmt (decl
);
5139 oldval
= lookup_name_current_level (name
);
5140 oldtype
= lookup_type_current_level (name
);
5142 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
5146 if (is_overloaded_fn (newval
))
5150 /* We only need to push declarations for those functions
5151 that were not already bound in the current level.
5152 The old value might be NULL_TREE, it might be a single
5153 function, or an OVERLOAD. */
5154 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
5155 term
= OVL_FUNCTION (oldval
);
5158 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
5160 push_overloaded_decl (OVL_CURRENT (fn
),
5161 PUSH_LOCAL
| PUSH_USING
);
5164 push_local_binding (name
, newval
, PUSH_USING
);
5167 set_identifier_type_value (name
, newtype
);
5171 do_class_using_decl (decl
)
5176 if (TREE_CODE (decl
) != SCOPE_REF
5177 || !TYPE_P (TREE_OPERAND (decl
, 0)))
5179 cp_error ("using-declaration for non-member at class scope");
5182 name
= TREE_OPERAND (decl
, 1);
5183 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
5185 cp_error ("using-declaration for destructor");
5188 if (TREE_CODE (name
) == TYPE_DECL
)
5189 name
= DECL_NAME (name
);
5191 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
5193 value
= build_lang_decl (USING_DECL
, name
, void_type_node
);
5194 DECL_INITIAL (value
) = TREE_OPERAND (decl
, 0);
5198 /* Process a using-directive. */
5201 do_using_directive (namespace)
5204 if (namespace == fake_std_node
)
5206 if (building_stmt_tree ())
5207 add_stmt (build_stmt (USING_STMT
, namespace));
5209 /* using namespace A::B::C; */
5210 if (TREE_CODE (namespace) == SCOPE_REF
)
5211 namespace = TREE_OPERAND (namespace, 1);
5212 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
5214 /* Lookup in lexer did not find a namespace. */
5215 if (!processing_template_decl
)
5216 cp_error ("namespace `%T' undeclared", namespace);
5219 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
5221 if (!processing_template_decl
)
5222 cp_error ("`%T' is not a namespace", namespace);
5225 namespace = ORIGINAL_NAMESPACE (namespace);
5226 if (!toplevel_bindings_p ())
5227 push_using_directive (namespace);
5230 add_using_namespace (current_namespace
, namespace, 0);
5234 check_default_args (x
)
5237 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5238 int saw_def
= 0, i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5239 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5241 if (TREE_PURPOSE (arg
))
5245 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5256 TREE_USED (decl
) = 1;
5257 if (processing_template_decl
)
5259 assemble_external (decl
);
5261 /* Is it a synthesized method that needs to be synthesized? */
5262 if (TREE_CODE (decl
) == FUNCTION_DECL
5263 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5264 && DECL_ARTIFICIAL (decl
)
5265 && ! DECL_INITIAL (decl
)
5266 /* Kludge: don't synthesize for default args. */
5267 && current_function_decl
)
5269 synthesize_method (decl
);
5270 /* If we've already synthesized the method we don't need to
5271 instantiate it, so we can return right away. */
5275 /* If this is a function or variable that is an instance of some
5276 template, we now know that we will need to actually do the
5277 instantiation. We check that DECL is not an explicit
5278 instantiation because that is not checked in instantiate_decl. */
5279 if ((DECL_NON_THUNK_FUNCTION_P (decl
) || TREE_CODE (decl
) == VAR_DECL
)
5280 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
5281 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5282 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INLINE (decl
))))
5283 instantiate_decl (decl
, /*defer_ok=*/1);
5286 /* Helper function for named_class_head_sans_basetype nonterminal. We
5287 have just seen something of the form `AGGR SCOPE::ID'. Return a
5288 TYPE_DECL for the type declared by ID in SCOPE. */
5291 handle_class_head (aggr
, scope
, id
)
5292 tree aggr
, scope
, id
;
5294 tree decl
= NULL_TREE
;
5296 if (TREE_CODE (id
) == TYPE_DECL
)
5297 /* We must bash typedefs back to the main decl of the type. Otherwise
5298 we become confused about scopes. */
5299 decl
= TYPE_MAIN_DECL (TREE_TYPE (id
));
5300 else if (DECL_CLASS_TEMPLATE_P (id
))
5301 decl
= DECL_TEMPLATE_RESULT (id
);
5304 tree current
= current_scope ();
5306 if (current
== NULL_TREE
)
5307 current
= current_namespace
;
5308 if (scope
== fake_std_node
)
5309 scope
= global_namespace
;
5310 if (scope
== NULL_TREE
)
5311 scope
= global_namespace
;
5315 /* According to the suggested resolution of core issue 180,
5316 'typename' is assumed after a class-key. */
5317 decl
= make_typename_type (scope
, id
, 1);
5318 if (decl
!= error_mark_node
)
5319 decl
= TYPE_MAIN_DECL (decl
);
5323 else if (scope
== current
)
5325 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5326 Be nice about it. */
5328 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5329 FROB_CONTEXT (scope
), id
);
5331 else if (scope
!= global_namespace
)
5332 cp_error ("`%T' does not have a nested type named `%D'", scope
, id
);
5334 cp_error ("no file-scope type named `%D'", id
);
5336 /* Inject it at the current scope. */
5338 decl
= TYPE_MAIN_DECL (xref_tag (aggr
, id
, 1));
5341 /* Enter the SCOPE. If this turns out not to be a definition, the
5342 parser must leave the scope. */
5343 push_scope (CP_DECL_CONTEXT (decl
));
5345 /* If we see something like:
5347 template <typename T> struct S::I ....
5349 we must create a TEMPLATE_DECL for the nested type. */
5350 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5351 decl
= push_template_decl (decl
);
5356 /* Initialize decl2.c. */
5361 ggc_add_tree_varray_root (&deferred_fns
, 1);
5362 ggc_add_tree_varray_root (&pending_statics
, 1);
5363 ggc_add_tree_varray_root (&ssdf_decls
, 1);
5364 ggc_add_tree_root (&ssdf_decl
, 1);
5365 ggc_add_tree_root (&priority_decl
, 1);
5366 ggc_add_tree_root (&initialize_p_decl
, 1);