1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* Process declarations and symbol lookup for C front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
43 #include "dwarf2out.h"
48 extern cpp_reader
*parse_in
;
50 /* This structure contains information about the initializations
51 and/or destructions required for a particular priority level. */
52 typedef struct priority_info_s
{
53 /* Non-zero if there have been any initializations at this priority
54 throughout the translation unit. */
55 int initializations_p
;
56 /* Non-zero if there have been any destructions at this priority
57 throughout the translation unit. */
61 static void mark_vtable_entries
PARAMS ((tree
));
62 static void grok_function_init
PARAMS ((tree
, tree
));
63 static int finish_vtable_vardecl
PARAMS ((tree
*, void *));
64 static int prune_vtable_vardecl
PARAMS ((tree
*, void *));
65 static int is_namespace_ancestor
PARAMS ((tree
, tree
));
66 static void add_using_namespace
PARAMS ((tree
, tree
, int));
67 static tree ambiguous_decl
PARAMS ((tree
, tree
, tree
,int));
68 static tree build_anon_union_vars
PARAMS ((tree
, tree
*, int, int));
69 static int acceptable_java_type
PARAMS ((tree
));
70 static void output_vtable_inherit
PARAMS ((tree
));
71 static tree start_objects
PARAMS ((int, int));
72 static void finish_objects
PARAMS ((int, int, tree
));
73 static tree merge_functions
PARAMS ((tree
, tree
));
74 static tree decl_namespace
PARAMS ((tree
));
75 static tree validate_nonmember_using_decl
PARAMS ((tree
, tree
*, tree
*));
76 static void do_nonmember_using_decl
PARAMS ((tree
, tree
, tree
, tree
,
78 static tree start_static_storage_duration_function
PARAMS ((void));
79 static void finish_static_storage_duration_function
PARAMS ((tree
));
80 static priority_info get_priority_info
PARAMS ((int));
81 static void do_static_initialization
PARAMS ((tree
, tree
));
82 static void do_static_destruction
PARAMS ((tree
));
83 static tree start_static_initialization_or_destruction
PARAMS ((tree
, int));
84 static void finish_static_initialization_or_destruction
PARAMS ((tree
));
85 static void generate_ctor_or_dtor_function
PARAMS ((int, int));
86 static int generate_ctor_and_dtor_functions_for_priority
87 PARAMS ((splay_tree_node
, void *));
88 static tree prune_vars_needing_no_initialization
PARAMS ((tree
));
89 static void write_out_vars
PARAMS ((tree
));
90 static void import_export_class
PARAMS ((tree
));
91 static tree key_method
PARAMS ((tree
));
92 static int compare_options
PARAMS ((const PTR
, const PTR
));
93 static tree get_guard_bits
PARAMS ((tree
));
95 extern int current_class_depth
;
97 /* A list of virtual function tables we must make sure to write out. */
100 /* A list of static class variables. This is needed, because a
101 static class variable can be declared inside the class without
102 an initializer, and then initialized, staticly, outside the class. */
103 static varray_type pending_statics
;
104 #define pending_statics_used \
105 (pending_statics ? pending_statics->elements_used : 0)
107 /* A list of functions which were declared inline, but which we
108 may need to emit outline anyway. */
109 static varray_type deferred_fns
;
110 #define deferred_fns_used \
111 (deferred_fns ? deferred_fns->elements_used : 0)
113 /* Same, but not reset. Local temp variables and global temp variables
114 can have the same name. */
115 static int global_temp_name_counter
;
117 /* Flag used when debugging spew.c */
119 extern int spew_debug
;
121 /* Nonzero if we're done parsing and into end-of-file activities. */
125 /* Functions called along with real static constructors and destructors. */
130 /* The :: namespace. */
132 tree global_namespace
;
134 /* The stack for namespaces of current declarations. */
136 static tree decl_namespace_list
;
139 /* C (and C++) language-specific option variables. */
141 /* Nonzero means don't recognize the keyword `asm'. */
145 /* Nonzero means don't recognize any extension keywords. */
147 int flag_no_gnu_keywords
;
149 /* Nonzero means do some things the same way PCC does. Only provided so
150 the compiler will link. */
152 int flag_traditional
;
154 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
156 int flag_signed_bitfields
= 1;
158 /* Nonzero means enable obscure standard features and disable GNU
159 extensions that might cause standard-compliant code to be
164 /* Nonzero means do emit exported implementations of functions even if
165 they can be inlined. */
167 int flag_implement_inlines
= 1;
169 /* Nonzero means do emit exported implementations of templates, instead of
170 multiple static copies in each file that needs a definition. */
172 int flag_external_templates
;
174 /* Nonzero means that the decision to emit or not emit the implementation of a
175 template depends on where the template is instantiated, rather than where
178 int flag_alt_external_templates
;
180 /* Nonzero means that implicit instantiations will be emitted if needed. */
182 int flag_implicit_templates
= 1;
184 /* Nonzero means that implicit instantiations of inline templates will be
185 emitted if needed, even if instantiations of non-inline templates
188 int flag_implicit_inline_templates
= 1;
190 /* Nonzero means warn about implicit declarations. */
192 int warn_implicit
= 1;
194 /* Nonzero means warn about usage of long long when `-pedantic'. */
196 int warn_long_long
= 1;
198 /* Nonzero means warn when all ctors or dtors are private, and the class
201 int warn_ctor_dtor_privacy
= 1;
203 /* True if we want to implement vtables using "thunks".
204 The default is off. */
206 int flag_vtable_thunks
= 1;
208 /* Nonzero means generate separate instantiation control files and juggle
209 them at link time. */
211 int flag_use_repository
;
213 /* Nonzero if we want to issue diagnostics that the standard says are not
216 int flag_optional_diags
= 1;
218 /* Nonzero means give string constants the type `const char *', as mandated
221 int flag_const_strings
= 1;
223 /* Nonzero means warn about deprecated conversion from string constant to
226 int warn_write_strings
;
228 /* Nonzero means warn about pointer casts that can drop a type qualifier
229 from the pointer target type. */
233 /* Nonzero means warn about sizeof(function) or addition/subtraction
234 of function pointers. */
236 int warn_pointer_arith
= 1;
238 /* Nonzero means warn for any function def without prototype decl. */
240 int warn_missing_prototypes
;
242 /* Nonzero means warn about multiple (redundant) decls for the same single
243 variable or function. */
245 int warn_redundant_decls
;
247 /* Warn if initializer is not completely bracketed. */
249 int warn_missing_braces
;
251 /* Warn about comparison of signed and unsigned values. */
253 int warn_sign_compare
;
255 /* Warn about testing equality of floating point numbers. */
257 int warn_float_equal
= 0;
259 /* Warn about functions which might be candidates for format attributes. */
261 int warn_missing_format_attribute
;
263 /* Warn about a subscript that has type char. */
265 int warn_char_subscripts
;
267 /* Warn if a type conversion is done that might have confusing results. */
271 /* Warn if adding () is suggested. */
273 int warn_parentheses
;
275 /* Non-zero means warn in function declared in derived class has the
276 same name as a virtual in the base class, but fails to match the
277 type signature of any virtual function in the base class. */
279 int warn_overloaded_virtual
;
281 /* Non-zero means warn when declaring a class that has a non virtual
282 destructor, when it really ought to have a virtual one. */
286 /* Non-zero means warn when the compiler will reorder code. */
290 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
294 /* Non-zero means warn when we convert a pointer to member function
295 into a pointer to (void or function). */
297 int warn_pmf2ptr
= 1;
299 /* Nonzero means warn about violation of some Effective C++ style rules. */
303 /* Nonzero means warn where overload resolution chooses a promotion from
304 unsigned to signed over a conversion to an unsigned of the same size. */
308 /* Nonzero means warn when an old-style cast is used. */
310 int warn_old_style_cast
;
312 /* Warn about #pragma directives that are not recognised. */
314 int warn_unknown_pragmas
; /* Tri state variable. */
316 /* Nonzero means warn about use of multicharacter literals. */
318 int warn_multichar
= 1;
320 /* Nonzero means warn when non-templatized friend functions are
321 declared within a template */
323 int warn_nontemplate_friend
= 1;
325 /* Nonzero means complain about deprecated features. */
327 int warn_deprecated
= 1;
329 /* Nonzero means `$' can be in an identifier. */
331 #ifndef DOLLARS_IN_IDENTIFIERS
332 #define DOLLARS_IN_IDENTIFIERS 1
334 int dollars_in_ident
= DOLLARS_IN_IDENTIFIERS
;
336 /* Nonzero means allow Microsoft extensions without a pedwarn. */
338 int flag_ms_extensions
;
340 /* C++ specific flags. */
342 /* Nonzero means we should attempt to elide constructors when possible. */
344 int flag_elide_constructors
= 1;
346 /* Nonzero means that member functions defined in class scope are
347 inline by default. */
349 int flag_default_inline
= 1;
351 /* Controls whether compiler generates 'type descriptor' that give
352 run-time type information. */
356 /* Nonzero if we wish to output cross-referencing information
357 for the GNU class browser. */
359 extern int flag_gnu_xref
;
361 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
364 int flag_huge_objects
;
366 /* Nonzero if we want to conserve space in the .o files. We do this
367 by putting uninitialized data and runtime initialized data into
368 .common instead of .data at the expense of not flagging multiple
371 int flag_conserve_space
;
373 /* Nonzero if we want to obey access control semantics. */
375 int flag_access_control
= 1;
377 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
379 int flag_operator_names
= 1;
381 /* Nonzero if we want to check the return value of new and avoid calling
382 constructors if it is a null pointer. */
386 /* Nonnull if we want to dump class heirarchies. */
388 const char *flag_dump_class_layout
;
390 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
391 initialization variables.
392 0: Old rules, set by -fno-for-scope.
393 2: New ISO rules, set by -ffor-scope.
394 1: Try to implement new ISO rules, but with backup compatibility
395 (and warnings). This is the default, for now. */
397 int flag_new_for_scope
= 1;
399 /* Nonzero if we want to emit defined symbols with common-like linkage as
400 weak symbols where possible, in order to conform to C++ semantics.
401 Otherwise, emit them as local symbols. */
405 /* Nonzero to use __cxa_atexit, rather than atexit, to register
406 destructors for local statics and global objects. */
408 int flag_use_cxa_atexit
;
410 /* Nonzero to not ignore namespace std. */
412 int flag_honor_std
= ENABLE_STD_NAMESPACE
;
414 /* Nonzero if we should expand functions calls inline at the tree
415 level, rather than at the RTL level. */
417 int flag_inline_trees
= 0;
419 /* Maximum template instantiation depth. This limit is rather
420 arbitrary, but it exists to limit the time it takes to notice
421 infinite template instantiations. */
423 int max_tinst_depth
= 50;
425 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
429 /* Nonzero means make the default pedwarns warnings instead of errors.
430 The value of this flag is ignored if -pedantic is specified. */
434 /* Nonzero means to implement standard semantics for exception
435 specifications, calling unexpected if an exception is thrown that
436 doesn't match the specification. Zero means to treat them as
437 assertions and optimize accordingly, but not check them. */
439 int flag_enforce_eh_specs
= 1;
441 /* The variant of the C language being processed. */
443 c_language_kind c_language
= clk_cplusplus
;
445 /* Table of language-dependent -f options.
446 STRING is the option name. VARIABLE is the address of the variable.
447 ON_VALUE is the value to store in VARIABLE
448 if `-fSTRING' is seen as an option.
449 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
451 static struct { const char *string
; int *variable
; int on_value
;}
455 {"signed-char", &flag_signed_char
, 1},
456 {"unsigned-char", &flag_signed_char
, 0},
457 {"signed-bitfields", &flag_signed_bitfields
, 1},
458 {"unsigned-bitfields", &flag_signed_bitfields
, 0},
459 {"short-enums", &flag_short_enums
, 1},
460 {"short-double", &flag_short_double
, 1},
461 {"short-wchar", &flag_short_wchar
, 1},
462 {"asm", &flag_no_asm
, 0},
463 {"builtin", &flag_no_builtin
, 0},
465 /* C++-only options. */
466 {"access-control", &flag_access_control
, 1},
467 {"check-new", &flag_check_new
, 1},
468 {"conserve-space", &flag_conserve_space
, 1},
469 {"const-strings", &flag_const_strings
, 1},
470 {"default-inline", &flag_default_inline
, 1},
471 {"dollars-in-identifiers", &dollars_in_ident
, 1},
472 {"elide-constructors", &flag_elide_constructors
, 1},
473 {"enforce-eh-specs", &flag_enforce_eh_specs
, 1},
474 {"external-templates", &flag_external_templates
, 1},
475 {"for-scope", &flag_new_for_scope
, 2},
476 {"gnu-keywords", &flag_no_gnu_keywords
, 0},
477 {"handle-exceptions", &flag_exceptions
, 1},
478 {"honor-std", &flag_honor_std
, 1},
479 {"huge-objects", &flag_huge_objects
, 1},
480 {"implement-inlines", &flag_implement_inlines
, 1},
481 {"implicit-inline-templates", &flag_implicit_inline_templates
, 1},
482 {"implicit-templates", &flag_implicit_templates
, 1},
483 {"ms-extensions", &flag_ms_extensions
, 1},
484 {"nonansi-builtins", &flag_no_nonansi_builtin
, 0},
485 {"operator-names", &flag_operator_names
, 1},
486 {"optional-diags", &flag_optional_diags
, 1},
487 {"permissive", &flag_permissive
, 1},
488 {"repo", &flag_use_repository
, 1},
489 {"rtti", &flag_rtti
, 1},
490 {"stats", &flag_detailed_statistics
, 1},
491 {"use-cxa-atexit", &flag_use_cxa_atexit
, 1},
492 {"vtable-thunks", &flag_vtable_thunks
, 1},
493 {"weak", &flag_weak
, 1}
496 /* The list of `-f' options that we no longer support. The `-f'
497 prefix is not given in this table. The `-fno-' variants are not
498 listed here. This table must be kept in alphabetical order. */
499 static const char * const unsupported_options
[] = {
514 /* Compare two option strings, pointed two by P1 and P2, for use with
518 compare_options (p1
, p2
)
522 return strcmp (*((const char *const *) p1
), *((const char *const *) p2
));
525 /* Decode the string P as a language-specific option.
526 Return the number of strings consumed for a valid option.
527 Otherwise return 0. Should not complain if it does not
528 recognise the option. */
531 cxx_decode_option (argc
, argv
)
535 int strings_processed
;
536 const char *p
= argv
[0];
538 strings_processed
= cpp_handle_option (parse_in
, argc
, argv
);
540 if (!strcmp (p
, "-ftraditional") || !strcmp (p
, "-traditional"))
542 else if (p
[0] == '-' && p
[1] == 'f')
544 /* Some kind of -f option.
545 P's value is the option sans `-f'.
546 Search for it in the table of options. */
547 const char *option_value
= NULL
;
548 const char *positive_option
;
552 /* Try special -f options. */
554 /* See if this is one of the options no longer supported. We
555 used to support these options, so we continue to accept them,
557 if (strncmp (p
, "no-", strlen ("no-")) == 0)
558 positive_option
= p
+ strlen ("no-");
562 /* If the option is present, issue a warning. Indicate to our
563 caller that the option was processed successfully. */
564 if (bsearch (&positive_option
,
566 (sizeof (unsupported_options
)
567 / sizeof (unsupported_options
[0])),
568 sizeof (unsupported_options
[0]),
571 warning ("-f%s is no longer supported", p
);
575 if (!strcmp (p
, "handle-exceptions")
576 || !strcmp (p
, "no-handle-exceptions"))
577 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
578 else if (! strcmp (p
, "alt-external-templates"))
580 flag_external_templates
= 1;
581 flag_alt_external_templates
= 1;
582 cp_deprecated ("-falt-external-templates");
584 else if (! strcmp (p
, "no-alt-external-templates"))
585 flag_alt_external_templates
= 0;
586 else if (!strcmp (p
, "repo"))
588 flag_use_repository
= 1;
589 flag_implicit_templates
= 0;
591 else if (!strcmp (p
, "external-templates"))
593 flag_external_templates
= 1;
594 cp_deprecated ("-fexternal-templates");
596 else if ((option_value
597 = skip_leading_substring (p
, "template-depth-")))
599 = read_integral_parameter (option_value
, p
- 2, max_tinst_depth
);
600 else if ((option_value
601 = skip_leading_substring (p
, "name-mangling-version-")))
603 warning ("-fname-mangling-version is no longer supported");
606 else if ((option_value
607 = skip_leading_substring (p
, "dump-translation-unit=")))
610 error ("no file specified with -fdump-translation-unit");
612 flag_dump_translation_unit
= option_value
;
614 else if ((option_value
615 = skip_leading_substring (p
, "dump-class-layout=")))
618 error ("no file specified with -fdump-class-layout");
620 flag_dump_class_layout
= option_value
;
622 else if (!strcmp (p
, "dump-class-layout"))
623 flag_dump_class_layout
= ""; /* empty string for stderr */
629 !found
&& j
< (sizeof (lang_f_options
)
630 / sizeof (lang_f_options
[0]));
633 if (!strcmp (p
, lang_f_options
[j
].string
))
635 *lang_f_options
[j
].variable
= lang_f_options
[j
].on_value
;
636 /* A goto here would be cleaner,
637 but breaks the vax pcc. */
640 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
641 && ! strcmp (p
+3, lang_f_options
[j
].string
))
643 *lang_f_options
[j
].variable
= ! lang_f_options
[j
].on_value
;
651 else if (p
[0] == '-' && p
[1] == 'W')
655 /* The -W options control the warning behavior of the compiler. */
658 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-')
661 if (!strcmp (p
, "implicit"))
662 warn_implicit
= setting
;
663 else if (!strcmp (p
, "long-long"))
664 warn_long_long
= setting
;
665 else if (!strcmp (p
, "return-type"))
666 warn_return_type
= setting
;
667 else if (!strcmp (p
, "ctor-dtor-privacy"))
668 warn_ctor_dtor_privacy
= setting
;
669 else if (!strcmp (p
, "write-strings"))
670 warn_write_strings
= setting
;
671 else if (!strcmp (p
, "cast-qual"))
672 warn_cast_qual
= setting
;
673 else if (!strcmp (p
, "char-subscripts"))
674 warn_char_subscripts
= setting
;
675 else if (!strcmp (p
, "pointer-arith"))
676 warn_pointer_arith
= setting
;
677 else if (!strcmp (p
, "missing-prototypes"))
678 warn_missing_prototypes
= setting
;
679 else if (!strcmp (p
, "strict-prototypes"))
682 warning ("-Wno-strict-prototypes is not supported in C++");
684 else if (!strcmp (p
, "redundant-decls"))
685 warn_redundant_decls
= setting
;
686 else if (!strcmp (p
, "missing-braces"))
687 warn_missing_braces
= setting
;
688 else if (!strcmp (p
, "sign-compare"))
689 warn_sign_compare
= setting
;
690 else if (!strcmp (p
, "float-equal"))
691 warn_float_equal
= setting
;
692 else if (!strcmp (p
, "format"))
693 set_Wformat (setting
);
694 else if (!strcmp (p
, "format=2"))
696 else if (!strcmp (p
, "format-y2k"))
697 warn_format_y2k
= setting
;
698 else if (!strcmp (p
, "format-extra-args"))
699 warn_format_extra_args
= setting
;
700 else if (!strcmp (p
, "format-nonliteral"))
701 warn_format_nonliteral
= setting
;
702 else if (!strcmp (p
, "format-security"))
703 warn_format_security
= setting
;
704 else if (!strcmp (p
, "missing-format-attribute"))
705 warn_missing_format_attribute
= setting
;
706 else if (!strcmp (p
, "conversion"))
707 warn_conversion
= setting
;
708 else if (!strcmp (p
, "parentheses"))
709 warn_parentheses
= setting
;
710 else if (!strcmp (p
, "non-virtual-dtor"))
711 warn_nonvdtor
= setting
;
712 else if (!strcmp (p
, "reorder"))
713 warn_reorder
= setting
;
714 else if (!strcmp (p
, "synth"))
715 warn_synth
= setting
;
716 else if (!strcmp (p
, "pmf-conversions"))
717 warn_pmf2ptr
= setting
;
718 else if (!strcmp (p
, "effc++"))
720 else if (!strcmp (p
, "sign-promo"))
721 warn_sign_promo
= setting
;
722 else if (!strcmp (p
, "old-style-cast"))
723 warn_old_style_cast
= setting
;
724 else if (!strcmp (p
, "overloaded-virtual"))
725 warn_overloaded_virtual
= setting
;
726 else if (!strcmp (p
, "multichar"))
727 warn_multichar
= setting
;
728 else if (!strcmp (p
, "unknown-pragmas"))
729 /* Set to greater than 1, so that even unknown pragmas in
730 system headers will be warned about. */
731 warn_unknown_pragmas
= setting
* 2;
732 else if (!strcmp (p
, "non-template-friend"))
733 warn_nontemplate_friend
= setting
;
734 else if (!strcmp (p
, "deprecated"))
735 warn_deprecated
= setting
;
736 else if (!strcmp (p
, "comment"))
737 ; /* cpp handles this one. */
738 else if (!strcmp (p
, "comments"))
739 ; /* cpp handles this one. */
740 else if (!strcmp (p
, "trigraphs"))
741 ; /* cpp handles this one. */
742 else if (!strcmp (p
, "import"))
743 ; /* cpp handles this one. */
744 else if (!strcmp (p
, "all"))
746 warn_return_type
= setting
;
747 set_Wunused (setting
);
748 warn_implicit
= setting
;
749 warn_switch
= setting
;
750 set_Wformat (setting
);
751 warn_parentheses
= setting
;
752 warn_missing_braces
= setting
;
753 warn_sign_compare
= setting
;
754 warn_multichar
= setting
;
755 /* We save the value of warn_uninitialized, since if they put
756 -Wuninitialized on the command line, we need to generate a
757 warning about not using it without also specifying -O. */
758 if (warn_uninitialized
!= 1)
759 warn_uninitialized
= (setting
? 2 : 0);
760 /* Only warn about unknown pragmas that are not in system
762 warn_unknown_pragmas
= 1;
764 /* C++-specific warnings. */
765 warn_ctor_dtor_privacy
= setting
;
766 warn_nonvdtor
= setting
;
767 warn_reorder
= setting
;
768 warn_nontemplate_friend
= setting
;
770 else return strings_processed
;
772 else if (!strcmp (p
, "-ansi"))
773 flag_no_nonansi_builtin
= 1, flag_ansi
= 1,
774 flag_noniso_default_format_attributes
= 0, flag_no_gnu_keywords
= 1;
776 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
777 it's probably safe to assume no sane person would ever want to use this
778 under normal circumstances. */
779 else if (!strcmp (p
, "-spew-debug"))
783 return strings_processed
;
788 /* Incorporate `const' and `volatile' qualifiers for member functions.
789 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
790 QUALS is a list of qualifiers. Returns any explicit
791 top-level qualifiers of the method's this pointer, anything other than
792 TYPE_UNQUALIFIED will be an extension. */
795 grok_method_quals (ctype
, function
, quals
)
796 tree ctype
, function
, quals
;
798 tree fntype
= TREE_TYPE (function
);
799 tree raises
= TYPE_RAISES_EXCEPTIONS (fntype
);
800 int type_quals
= TYPE_UNQUALIFIED
;
801 int dup_quals
= TYPE_UNQUALIFIED
;
802 int this_quals
= TYPE_UNQUALIFIED
;
806 int tq
= cp_type_qual_from_rid (TREE_VALUE (quals
));
808 if ((type_quals
| this_quals
) & tq
)
810 else if (tq
& TYPE_QUAL_RESTRICT
)
814 quals
= TREE_CHAIN (quals
);
818 if (dup_quals
!= TYPE_UNQUALIFIED
)
819 cp_error ("duplicate type qualifiers in %s declaration",
820 TREE_CODE (function
) == FUNCTION_DECL
821 ? "member function" : "type");
823 ctype
= cp_build_qualified_type (ctype
, type_quals
);
824 fntype
= build_cplus_method_type (ctype
, TREE_TYPE (fntype
),
825 (TREE_CODE (fntype
) == METHOD_TYPE
826 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype
))
827 : TYPE_ARG_TYPES (fntype
)));
829 fntype
= build_exception_variant (fntype
, raises
);
831 TREE_TYPE (function
) = fntype
;
835 /* Warn when -fexternal-templates is used and #pragma
836 interface/implementation is not used all the times it should be,
840 warn_if_unknown_interface (decl
)
843 static int already_warned
= 0;
844 if (already_warned
++)
847 if (flag_alt_external_templates
)
849 tree til
= tinst_for_decl ();
851 const char *sf
= input_filename
;
855 lineno
= TINST_LINE (til
);
856 input_filename
= TINST_FILE (til
);
858 cp_warning ("template `%#D' instantiated in file without #pragma interface",
864 cp_warning_at ("template `%#D' defined in file without #pragma interface",
868 /* A subroutine of the parser, to handle a component list. */
871 grok_x_components (specs
)
876 specs
= strip_attrs (specs
);
878 check_tag_decl (specs
);
879 t
= groktypename (build_tree_list (specs
, NULL_TREE
));
881 /* The only case where we need to do anything additional here is an
882 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
883 if (t
== NULL_TREE
|| !ANON_AGGR_TYPE_P (t
))
886 fixup_anonymous_aggr (t
);
887 finish_member_declaration (build_decl (FIELD_DECL
, NULL_TREE
, t
));
890 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
894 build_artificial_parm (name
, type
)
900 parm
= build_decl (PARM_DECL
, name
, type
);
901 DECL_ARTIFICIAL (parm
) = 1;
902 DECL_ARG_TYPE (parm
) = type
;
906 /* Constructors for types with virtual baseclasses need an "in-charge" flag
907 saying whether this constructor is responsible for initialization of
908 virtual baseclasses or not. All destructors also need this "in-charge"
909 flag, which additionally determines whether or not the destructor should
910 free the memory for the object.
912 This function adds the "in-charge" flag to member function FN if
913 appropriate. It is called from grokclassfn and tsubst.
914 FN must be either a constructor or destructor.
916 The in-charge flag follows the 'this' parameter, and is followed by the
917 VTT parm (if any), then the user-written parms. */
920 maybe_retrofit_in_chrg (fn
)
923 tree basetype
, arg_types
, parms
, parm
, fntype
;
925 /* If we've already add the in-charge parameter don't do it again. */
926 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
929 /* When processing templates we can't know, in general, whether or
930 not we're going to have virtual baseclasses. */
931 if (uses_template_parms (fn
))
934 /* We don't need an in-charge parameter for constructors that don't
935 have virtual bases. */
936 if (DECL_CONSTRUCTOR_P (fn
)
937 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
940 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
941 basetype
= TREE_TYPE (TREE_VALUE (arg_types
));
942 arg_types
= TREE_CHAIN (arg_types
);
944 parms
= TREE_CHAIN (DECL_ARGUMENTS (fn
));
946 /* If this is a subobject constructor or destructor, our caller will
947 pass us a pointer to our VTT. */
948 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn
)))
950 parm
= build_artificial_parm (vtt_parm_identifier
, vtt_parm_type
);
952 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
953 TREE_CHAIN (parm
) = parms
;
956 /* ...and then to TYPE_ARG_TYPES. */
957 arg_types
= hash_tree_chain (vtt_parm_type
, arg_types
);
959 DECL_HAS_VTT_PARM_P (fn
) = 1;
962 /* Then add the in-charge parm (before the VTT parm). */
963 parm
= build_artificial_parm (in_charge_identifier
, integer_type_node
);
964 TREE_CHAIN (parm
) = parms
;
966 arg_types
= hash_tree_chain (integer_type_node
, arg_types
);
968 /* Insert our new parameter(s) into the list. */
969 TREE_CHAIN (DECL_ARGUMENTS (fn
)) = parms
;
971 /* And rebuild the function type. */
972 fntype
= build_cplus_method_type (basetype
, TREE_TYPE (TREE_TYPE (fn
)),
974 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)))
975 fntype
= build_exception_variant (fntype
,
976 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)));
977 TREE_TYPE (fn
) = fntype
;
979 /* Now we've got the in-charge parameter. */
980 DECL_HAS_IN_CHARGE_PARM_P (fn
) = 1;
983 /* Classes overload their constituent function names automatically.
984 When a function name is declared in a record structure,
985 its name is changed to it overloaded name. Since names for
986 constructors and destructors can conflict, we place a leading
989 CNAME is the name of the class we are grokking for.
991 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
993 FLAGS contains bits saying what's special about today's
994 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
996 If FUNCTION is a destructor, then we must add the `auto-delete' field
997 as a second parameter. There is some hair associated with the fact
998 that we must "declare" this variable in the manner consistent with the
999 way the rest of the arguments were declared.
1001 QUALS are the qualifiers for the this pointer. */
1004 grokclassfn (ctype
, function
, flags
, quals
)
1005 tree ctype
, function
;
1006 enum overload_flags flags
;
1009 tree fn_name
= DECL_NAME (function
);
1010 int this_quals
= TYPE_UNQUALIFIED
;
1012 /* Even within an `extern "C"' block, members get C++ linkage. See
1013 [dcl.link] for details. */
1014 SET_DECL_LANGUAGE (function
, lang_cplusplus
);
1016 if (fn_name
== NULL_TREE
)
1018 error ("name missing for member function");
1019 fn_name
= get_identifier ("<anonymous>");
1020 DECL_NAME (function
) = fn_name
;
1024 this_quals
= grok_method_quals (ctype
, function
, quals
);
1026 if (TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1028 /* Must add the class instance variable up front. */
1029 /* Right now we just make this a pointer. But later
1030 we may wish to make it special. */
1031 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function
)));
1035 /* The `this' parameter is implicitly `const'; it cannot be
1037 this_quals
|= TYPE_QUAL_CONST
;
1038 qual_type
= cp_build_qualified_type (type
, this_quals
);
1039 parm
= build_artificial_parm (this_identifier
, qual_type
);
1040 c_apply_type_quals_to_decl (this_quals
, parm
);
1042 /* We can make this a register, so long as we don't
1043 accidentally complain if someone tries to take its address. */
1044 DECL_REGISTER (parm
) = 1;
1045 TREE_CHAIN (parm
) = last_function_parms
;
1046 last_function_parms
= parm
;
1049 DECL_ARGUMENTS (function
) = last_function_parms
;
1050 DECL_CONTEXT (function
) = ctype
;
1052 if (flags
== DTOR_FLAG
)
1053 DECL_DESTRUCTOR_P (function
) = 1;
1055 if (flags
== DTOR_FLAG
|| DECL_CONSTRUCTOR_P (function
))
1056 maybe_retrofit_in_chrg (function
);
1058 if (flags
== DTOR_FLAG
)
1060 DECL_DESTRUCTOR_P (function
) = 1;
1061 TYPE_HAS_DESTRUCTOR (ctype
) = 1;
1065 /* Work on the expr used by alignof (this is only called by the parser). */
1074 if (processing_template_decl
)
1075 return build_min (ALIGNOF_EXPR
, sizetype
, expr
);
1077 if (TREE_CODE (expr
) == COMPONENT_REF
1078 && DECL_C_BIT_FIELD (TREE_OPERAND (expr
, 1)))
1079 error ("`__alignof__' applied to a bit-field");
1081 if (TREE_CODE (expr
) == INDIRECT_REF
)
1083 best
= t
= TREE_OPERAND (expr
, 0);
1084 bestalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
1086 while (TREE_CODE (t
) == NOP_EXPR
1087 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == POINTER_TYPE
)
1090 t
= TREE_OPERAND (t
, 0);
1091 thisalign
= TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t
)));
1092 if (thisalign
> bestalign
)
1093 best
= t
, bestalign
= thisalign
;
1095 return c_alignof (TREE_TYPE (TREE_TYPE (best
)));
1098 return c_alignof (TREE_TYPE (expr
));
1101 /* Create an ARRAY_REF, checking for the user doing things backwards
1105 grok_array_decl (array_expr
, index_exp
)
1106 tree array_expr
, index_exp
;
1108 tree type
= TREE_TYPE (array_expr
);
1109 tree p1
, p2
, i1
, i2
;
1111 if (type
== error_mark_node
|| index_exp
== error_mark_node
)
1112 return error_mark_node
;
1113 if (processing_template_decl
)
1114 return build_min (ARRAY_REF
, type
? TREE_TYPE (type
) : NULL_TREE
,
1115 array_expr
, index_exp
);
1117 if (type
== NULL_TREE
)
1119 /* Something has gone very wrong. Assume we are mistakenly reducing
1120 an expression instead of a declaration. */
1121 error ("parser may be lost: is there a '{' missing somewhere?");
1125 if (TREE_CODE (type
) == OFFSET_TYPE
1126 || TREE_CODE (type
) == REFERENCE_TYPE
)
1127 type
= TREE_TYPE (type
);
1129 /* If they have an `operator[]', use that. */
1130 if (IS_AGGR_TYPE (type
) || IS_AGGR_TYPE (TREE_TYPE (index_exp
)))
1131 return build_opfncall (ARRAY_REF
, LOOKUP_NORMAL
,
1132 array_expr
, index_exp
, NULL_TREE
);
1134 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1135 is a little-known fact that, if `a' is an array and `i' is an
1136 int, you can write `i[a]', which means the same thing as `a[i]'. */
1138 if (TREE_CODE (type
) == ARRAY_TYPE
)
1141 p1
= build_expr_type_conversion (WANT_POINTER
, array_expr
, 0);
1143 if (TREE_CODE (TREE_TYPE (index_exp
)) == ARRAY_TYPE
)
1146 p2
= build_expr_type_conversion (WANT_POINTER
, index_exp
, 0);
1148 i1
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, array_expr
, 0);
1149 i2
= build_expr_type_conversion (WANT_INT
| WANT_ENUM
, index_exp
, 0);
1151 if ((p1
&& i2
) && (i1
&& p2
))
1152 error ("ambiguous conversion for array subscript");
1155 array_expr
= p1
, index_exp
= i2
;
1157 array_expr
= p2
, index_exp
= i1
;
1160 cp_error ("invalid types `%T[%T]' for array subscript",
1161 type
, TREE_TYPE (index_exp
));
1162 return error_mark_node
;
1165 if (array_expr
== error_mark_node
|| index_exp
== error_mark_node
)
1166 error ("ambiguous conversion for array subscript");
1168 return build_array_ref (array_expr
, index_exp
);
1171 /* Given the cast expression EXP, checking out its validity. Either return
1172 an error_mark_node if there was an unavoidable error, return a cast to
1173 void for trying to delete a pointer w/ the value 0, or return the
1174 call to delete. If DOING_VEC is 1, we handle things differently
1175 for doing an array delete. If DOING_VEC is 2, they gave us the
1176 array size as an argument to delete.
1177 Implements ARM $5.3.4. This is called from the parser. */
1180 delete_sanity (exp
, size
, doing_vec
, use_global_delete
)
1182 int doing_vec
, use_global_delete
;
1185 /* For a regular vector delete (aka, no size argument) we will pass
1186 this down as a NULL_TREE into build_vec_delete. */
1187 tree maxindex
= NULL_TREE
;
1189 if (exp
== error_mark_node
)
1192 if (processing_template_decl
)
1194 t
= build_min (DELETE_EXPR
, void_type_node
, exp
, size
);
1195 DELETE_EXPR_USE_GLOBAL (t
) = use_global_delete
;
1196 DELETE_EXPR_USE_VEC (t
) = doing_vec
;
1200 if (TREE_CODE (exp
) == OFFSET_REF
)
1201 exp
= resolve_offset_ref (exp
);
1202 exp
= convert_from_reference (exp
);
1203 t
= stabilize_reference (exp
);
1204 t
= build_expr_type_conversion (WANT_POINTER
, t
, 1);
1206 if (t
== NULL_TREE
|| t
== error_mark_node
)
1208 cp_error ("type `%#T' argument given to `delete', expected pointer",
1210 return error_mark_node
;
1215 maxindex
= cp_build_binary_op (MINUS_EXPR
, size
, integer_one_node
);
1216 pedwarn ("anachronistic use of array size in vector delete");
1219 type
= TREE_TYPE (t
);
1221 /* As of Valley Forge, you can delete a pointer to const. */
1223 /* You can't delete functions. */
1224 if (TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
1226 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1227 return error_mark_node
;
1230 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1231 if (TREE_CODE (TREE_TYPE (type
)) == VOID_TYPE
)
1233 cp_warning ("deleting `%T' is undefined", type
);
1237 /* An array can't have been allocated by new, so complain. */
1238 if (TREE_CODE (t
) == ADDR_EXPR
1239 && TREE_CODE (TREE_OPERAND (t
, 0)) == VAR_DECL
1240 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t
, 0))) == ARRAY_TYPE
)
1241 cp_warning ("deleting array `%#D'", TREE_OPERAND (t
, 0));
1243 /* Deleting a pointer with the value zero is valid and has no effect. */
1244 if (integer_zerop (t
))
1245 return build1 (NOP_EXPR
, void_type_node
, t
);
1248 return build_vec_delete (t
, maxindex
, sfk_deleting_destructor
,
1252 if (IS_AGGR_TYPE (TREE_TYPE (type
))
1253 && TYPE_GETS_REG_DELETE (TREE_TYPE (type
)))
1255 /* Only do access checking here; we'll be calling op delete
1256 from the destructor. */
1257 tree tmp
= build_op_delete_call (DELETE_EXPR
, t
, size_zero_node
,
1258 LOOKUP_NORMAL
, NULL_TREE
);
1259 if (tmp
== error_mark_node
)
1260 return error_mark_node
;
1263 return build_delete (type
, t
, sfk_deleting_destructor
,
1264 LOOKUP_NORMAL
, use_global_delete
);
1268 /* Report an error if the indicated template declaration is not the
1269 sort of thing that should be a member template. */
1272 check_member_template (tmpl
)
1277 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
1278 decl
= DECL_TEMPLATE_RESULT (tmpl
);
1280 if (TREE_CODE (decl
) == FUNCTION_DECL
1281 || (TREE_CODE (decl
) == TYPE_DECL
1282 && IS_AGGR_TYPE (TREE_TYPE (decl
))))
1284 if (current_function_decl
)
1285 /* 14.5.2.2 [temp.mem]
1287 A local class shall not have member templates. */
1288 cp_error ("invalid declaration of member template `%#D' in local class",
1291 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_VIRTUAL_P (decl
))
1293 /* 14.5.2.3 [temp.mem]
1295 A member function template shall not be virtual. */
1297 ("invalid use of `virtual' in template declaration of `%#D'",
1299 DECL_VIRTUAL_P (decl
) = 0;
1302 /* The debug-information generating code doesn't know what to do
1303 with member templates. */
1304 DECL_IGNORED_P (tmpl
) = 1;
1307 cp_error ("template declaration of `%#D'", decl
);
1310 /* Return true iff TYPE is a valid Java parameter or return type. */
1313 acceptable_java_type (type
)
1316 if (TREE_CODE (type
) == VOID_TYPE
|| TYPE_FOR_JAVA (type
))
1318 if (TREE_CODE (type
) == POINTER_TYPE
|| TREE_CODE (type
) == REFERENCE_TYPE
)
1320 type
= TREE_TYPE (type
);
1321 if (TREE_CODE (type
) == RECORD_TYPE
)
1324 if (! TYPE_FOR_JAVA (type
))
1326 if (! CLASSTYPE_TEMPLATE_INFO (type
))
1328 args
= CLASSTYPE_TI_ARGS (type
);
1329 i
= TREE_VEC_LENGTH (args
);
1332 type
= TREE_VEC_ELT (args
, i
);
1333 if (TREE_CODE (type
) == POINTER_TYPE
)
1334 type
= TREE_TYPE (type
);
1335 if (! TYPE_FOR_JAVA (type
))
1344 /* For a METHOD in a Java class CTYPE, return 1 if
1345 the parameter and return types are valid Java types.
1346 Otherwise, print appropriate error messages, and return 0. */
1349 check_java_method (method
)
1353 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (method
));
1354 tree ret_type
= TREE_TYPE (TREE_TYPE (method
));
1355 if (! acceptable_java_type (ret_type
))
1357 cp_error ("Java method '%D' has non-Java return type `%T'",
1361 for (; arg_types
!= NULL_TREE
; arg_types
= TREE_CHAIN (arg_types
))
1363 tree type
= TREE_VALUE (arg_types
);
1364 if (! acceptable_java_type (type
))
1366 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1371 return jerr
? 0 : 1;
1374 /* Sanity check: report error if this function FUNCTION is not
1375 really a member of the class (CTYPE) it is supposed to belong to.
1376 CNAME is the same here as it is for grokclassfn above. */
1379 check_classfn (ctype
, function
)
1380 tree ctype
, function
;
1382 tree fn_name
= DECL_NAME (function
);
1383 tree fndecl
, fndecls
;
1384 tree method_vec
= CLASSTYPE_METHOD_VEC (complete_type (ctype
));
1388 if (DECL_USE_TEMPLATE (function
)
1389 && !(TREE_CODE (function
) == TEMPLATE_DECL
1390 && DECL_TEMPLATE_SPECIALIZATION (function
))
1391 && is_member_template (DECL_TI_TEMPLATE (function
)))
1392 /* Since this is a specialization of a member template,
1393 we're not going to find the declaration in the class.
1396 struct S { template <typename T> void f(T); };
1397 template <> void S::f(int);
1399 we're not going to find `S::f(int)', but there's no
1400 reason we should, either. We let our callers know we didn't
1401 find the method, but we don't complain. */
1404 if (method_vec
!= 0)
1406 methods
= &TREE_VEC_ELT (method_vec
, 0);
1407 end
= TREE_VEC_END (method_vec
);
1409 /* First suss out ctors and dtors. */
1410 if (*methods
&& fn_name
== DECL_NAME (OVL_CURRENT (*methods
))
1411 && DECL_CONSTRUCTOR_P (function
))
1413 if (*++methods
&& fn_name
== DECL_NAME (OVL_CURRENT (*methods
))
1414 && DECL_DESTRUCTOR_P (function
))
1417 while (++methods
!= end
&& *methods
)
1420 if (fn_name
== DECL_NAME (OVL_CURRENT (*methods
)))
1423 for (fndecls
= *methods
; fndecls
!= NULL_TREE
;
1424 fndecls
= OVL_NEXT (fndecls
))
1426 fndecl
= OVL_CURRENT (fndecls
);
1428 /* We cannot simply call decls_match because this
1429 doesn't work for static member functions that are
1430 pretending to be methods, and because the name
1431 may have been changed by asm("new_name"). */
1432 if (DECL_NAME (function
) == DECL_NAME (fndecl
))
1434 tree p1
= TYPE_ARG_TYPES (TREE_TYPE (function
));
1435 tree p2
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
1437 /* Get rid of the this parameter on functions that become
1439 if (DECL_STATIC_FUNCTION_P (fndecl
)
1440 && TREE_CODE (TREE_TYPE (function
)) == METHOD_TYPE
)
1441 p1
= TREE_CHAIN (p1
);
1443 if (same_type_p (TREE_TYPE (TREE_TYPE (function
)),
1444 TREE_TYPE (TREE_TYPE (fndecl
)))
1445 && compparms (p1
, p2
)
1446 && (DECL_TEMPLATE_SPECIALIZATION (function
)
1447 == DECL_TEMPLATE_SPECIALIZATION (fndecl
))
1448 && (!DECL_TEMPLATE_SPECIALIZATION (function
)
1449 || (DECL_TI_TEMPLATE (function
)
1450 == DECL_TI_TEMPLATE (fndecl
))))
1459 if (methods
!= end
&& *methods
)
1461 tree fndecl
= *methods
;
1462 cp_error ("prototype for `%#D' does not match any in class `%T'",
1464 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl
) ? "s are" : " is",
1465 OVL_CURRENT (fndecl
));
1466 while (fndecl
= OVL_NEXT (fndecl
), fndecl
)
1467 cp_error_at (" %#D", OVL_CURRENT(fndecl
));
1472 if (!COMPLETE_TYPE_P (ctype
))
1473 incomplete_type_error (function
, ctype
);
1475 cp_error ("no `%#D' member function declared in class `%T'",
1479 /* If we did not find the method in the class, add it to avoid
1480 spurious errors (unless the CTYPE is not yet defined, in which
1481 case we'll only confuse ourselves when the function is declared
1482 properly within the class. */
1483 if (COMPLETE_TYPE_P (ctype
))
1484 add_method (ctype
, function
, /*error_p=*/1);
1488 /* We have just processed the DECL, which is a static data member.
1489 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1490 present, is the assembly-language name for the data member.
1491 FLAGS is as for cp_finish_decl. */
1494 finish_static_data_member_decl (decl
, init
, asmspec_tree
, flags
)
1500 my_friendly_assert (TREE_PUBLIC (decl
), 0);
1502 DECL_CONTEXT (decl
) = current_class_type
;
1504 /* We cannot call pushdecl here, because that would fill in the
1505 decl of our TREE_CHAIN. Instead, we modify cp_finish_decl to do
1506 the right thing, namely, to put this decl out straight away. */
1507 /* current_class_type can be NULL_TREE in case of error. */
1508 if (!asmspec_tree
&& current_class_type
)
1509 DECL_INITIAL (decl
) = error_mark_node
;
1511 if (! processing_template_decl
)
1513 if (!pending_statics
)
1514 VARRAY_TREE_INIT (pending_statics
, 32, "pending_statics");
1515 VARRAY_PUSH_TREE (pending_statics
, decl
);
1518 /* Static consts need not be initialized in the class definition. */
1519 if (init
!= NULL_TREE
&& TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
)))
1521 static int explained
= 0;
1523 error ("initializer invalid for static member with constructor");
1526 error ("(an out of class initialization is required)");
1531 /* Force the compiler to know when an uninitialized static const
1532 member is being used. */
1533 if (CP_TYPE_CONST_P (TREE_TYPE (decl
)) && init
== 0)
1534 TREE_USED (decl
) = 1;
1535 DECL_INITIAL (decl
) = init
;
1536 DECL_IN_AGGR_P (decl
) = 1;
1538 cp_finish_decl (decl
, init
, asmspec_tree
, flags
);
1541 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1542 of a structure component, returning a _DECL node.
1543 QUALS is a list of type qualifiers for this decl (such as for declaring
1544 const member functions).
1546 This is done during the parsing of the struct declaration.
1547 The _DECL nodes are chained together and the lot of them
1548 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1550 If class A defines that certain functions in class B are friends, then
1551 the way I have set things up, it is B who is interested in permission
1552 granted by A. However, it is in A's context that these declarations
1553 are parsed. By returning a void_type_node, class A does not attempt
1554 to incorporate the declarations of the friends within its structure.
1556 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1557 CHANGES TO CODE IN `start_method'. */
1560 grokfield (declarator
, declspecs
, init
, asmspec_tree
, attrlist
)
1561 tree declarator
, declspecs
, init
, asmspec_tree
, attrlist
;
1563 register tree value
;
1564 const char *asmspec
= 0;
1565 int flags
= LOOKUP_ONLYCONVERTING
;
1567 /* Convert () initializers to = initializers. */
1568 if (init
== NULL_TREE
&& declarator
!= NULL_TREE
1569 && TREE_CODE (declarator
) == CALL_EXPR
1570 && TREE_OPERAND (declarator
, 0)
1571 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
1572 || TREE_CODE (TREE_OPERAND (declarator
, 0)) == SCOPE_REF
)
1573 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator
)))
1575 /* It's invalid to try to initialize a data member using a
1576 functional notation, e.g.:
1582 Explain that to the user. */
1583 static int explained
;
1585 cp_error ("invalid data member initialization");
1588 cp_error ("(use `=' to initialize static data members)");
1592 declarator
= TREE_OPERAND (declarator
, 0);
1596 if (declspecs
== NULL_TREE
1597 && TREE_CODE (declarator
) == SCOPE_REF
1598 && TREE_CODE (TREE_OPERAND (declarator
, 1)) == IDENTIFIER_NODE
)
1600 /* Access declaration */
1601 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator
, 0))))
1603 else if (TREE_COMPLEXITY (declarator
) == current_class_depth
)
1604 pop_nested_class ();
1605 return do_class_using_decl (declarator
);
1609 && TREE_CODE (init
) == TREE_LIST
1610 && TREE_VALUE (init
) == error_mark_node
1611 && TREE_CHAIN (init
) == NULL_TREE
)
1614 value
= grokdeclarator (declarator
, declspecs
, FIELD
, init
!= 0, attrlist
);
1615 if (! value
|| value
== error_mark_node
)
1616 /* friend or constructor went bad. */
1618 if (TREE_TYPE (value
) == error_mark_node
)
1619 return error_mark_node
;
1621 /* Pass friendly classes back. */
1622 if (TREE_CODE (value
) == VOID_TYPE
)
1623 return void_type_node
;
1625 if (DECL_NAME (value
) != NULL_TREE
1626 && IDENTIFIER_POINTER (DECL_NAME (value
))[0] == '_'
1627 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value
)), "_vptr"))
1628 cp_error ("member `%D' conflicts with virtual function table field name",
1631 /* Stash away type declarations. */
1632 if (TREE_CODE (value
) == TYPE_DECL
)
1634 DECL_NONLOCAL (value
) = 1;
1635 DECL_CONTEXT (value
) = current_class_type
;
1637 if (CLASS_TYPE_P (TREE_TYPE (value
)))
1638 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value
)) = 1;
1640 if (processing_template_decl
)
1641 value
= push_template_decl (value
);
1646 if (DECL_IN_AGGR_P (value
))
1648 cp_error ("`%D' is already defined in `%T'", value
,
1649 DECL_CONTEXT (value
));
1650 return void_type_node
;
1654 asmspec
= TREE_STRING_POINTER (asmspec_tree
);
1658 if (TREE_CODE (value
) == FUNCTION_DECL
)
1660 grok_function_init (value
, init
);
1663 else if (pedantic
&& TREE_CODE (value
) != VAR_DECL
)
1664 /* Already complained in grokdeclarator. */
1668 /* We allow initializers to become parameters to base
1670 if (TREE_CODE (init
) == TREE_LIST
)
1672 if (TREE_CHAIN (init
) == NULL_TREE
)
1673 init
= TREE_VALUE (init
);
1675 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
1678 if (TREE_CODE (init
) == CONST_DECL
)
1679 init
= DECL_INITIAL (init
);
1680 else if (TREE_READONLY_DECL_P (init
))
1681 init
= decl_constant_value (init
);
1682 else if (TREE_CODE (init
) == CONSTRUCTOR
)
1683 init
= digest_init (TREE_TYPE (value
), init
, (tree
*)0);
1684 if (init
== error_mark_node
)
1685 /* We must make this look different than `error_mark_node'
1686 because `decl_const_value' would mis-interpret it
1687 as only meaning that this VAR_DECL is defined. */
1688 init
= build1 (NOP_EXPR
, TREE_TYPE (value
), init
);
1689 else if (processing_template_decl
)
1691 else if (! TREE_CONSTANT (init
))
1693 /* We can allow references to things that are effectively
1694 static, since references are initialized with the address. */
1695 if (TREE_CODE (TREE_TYPE (value
)) != REFERENCE_TYPE
1696 || (TREE_STATIC (init
) == 0
1697 && (!DECL_P (init
) || DECL_EXTERNAL (init
) == 0)))
1699 error ("field initializer is not constant");
1700 init
= error_mark_node
;
1706 if (processing_template_decl
&& ! current_function_decl
1707 && (TREE_CODE (value
) == VAR_DECL
|| TREE_CODE (value
) == FUNCTION_DECL
))
1708 value
= push_template_decl (value
);
1711 cplus_decl_attributes (value
, TREE_PURPOSE (attrlist
),
1712 TREE_VALUE (attrlist
));
1714 if (TREE_CODE (value
) == VAR_DECL
)
1716 finish_static_data_member_decl (value
, init
, asmspec_tree
,
1720 if (TREE_CODE (value
) == FIELD_DECL
)
1723 cp_error ("`asm' specifiers are not permitted on non-static data members");
1724 if (DECL_INITIAL (value
) == error_mark_node
)
1725 init
= error_mark_node
;
1726 cp_finish_decl (value
, init
, NULL_TREE
, flags
);
1727 DECL_INITIAL (value
) = init
;
1728 DECL_IN_AGGR_P (value
) = 1;
1731 if (TREE_CODE (value
) == FUNCTION_DECL
)
1735 /* This must override the asm specifier which was placed
1736 by grokclassfn. Lay this out fresh. */
1737 SET_DECL_RTL (value
, NULL_RTX
);
1738 SET_DECL_ASSEMBLER_NAME (value
, get_identifier (asmspec
));
1740 cp_finish_decl (value
, init
, asmspec_tree
, flags
);
1742 /* Pass friends back this way. */
1743 if (DECL_FRIEND_P (value
))
1744 return void_type_node
;
1746 DECL_IN_AGGR_P (value
) = 1;
1749 my_friendly_abort (21);
1754 /* Like `grokfield', but for bitfields.
1755 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1758 grokbitfield (declarator
, declspecs
, width
)
1759 tree declarator
, declspecs
, width
;
1761 register tree value
= grokdeclarator (declarator
, declspecs
, BITFIELD
,
1764 if (! value
) return NULL_TREE
; /* friends went bad. */
1766 /* Pass friendly classes back. */
1767 if (TREE_CODE (value
) == VOID_TYPE
)
1768 return void_type_node
;
1770 if (TREE_CODE (value
) == TYPE_DECL
)
1772 cp_error ("cannot declare `%D' to be a bitfield type", value
);
1776 /* Usually, finish_struct_1 catches bitifields with invalid types.
1777 But, in the case of bitfields with function type, we confuse
1778 ourselves into thinking they are member functions, so we must
1780 if (TREE_CODE (value
) == FUNCTION_DECL
)
1782 cp_error ("cannot declare bitfield `%D' with funcion type",
1787 if (DECL_IN_AGGR_P (value
))
1789 cp_error ("`%D' is already defined in the class %T", value
,
1790 DECL_CONTEXT (value
));
1791 return void_type_node
;
1794 GNU_xref_member (current_class_name
, value
);
1796 if (TREE_STATIC (value
))
1798 cp_error ("static member `%D' cannot be a bitfield", value
);
1801 cp_finish_decl (value
, NULL_TREE
, NULL_TREE
, 0);
1803 if (width
!= error_mark_node
)
1805 constant_expression_warning (width
);
1806 DECL_INITIAL (value
) = width
;
1807 SET_DECL_C_BIT_FIELD (value
);
1810 DECL_IN_AGGR_P (value
) = 1;
1815 grokoptypename (declspecs
, declarator
)
1816 tree declspecs
, declarator
;
1818 tree t
= grokdeclarator (declarator
, declspecs
, TYPENAME
, 0, NULL_TREE
);
1819 return mangle_conv_op_name_for_type (t
);
1822 /* When a function is declared with an initializer,
1823 do the right thing. Currently, there are two possibilities:
1828 // initialization possibility #1.
1829 virtual void f () = 0;
1851 // initialization possibility #2
1858 copy_assignment_arg_p (parmtype
, virtualp
)
1860 int virtualp ATTRIBUTE_UNUSED
;
1862 if (current_class_type
== NULL_TREE
)
1865 if (TREE_CODE (parmtype
) == REFERENCE_TYPE
)
1866 parmtype
= TREE_TYPE (parmtype
);
1868 if ((TYPE_MAIN_VARIANT (parmtype
) == current_class_type
)
1870 /* Non-standard hack to support old Booch components. */
1871 || (! virtualp
&& DERIVED_FROM_P (parmtype
, current_class_type
))
1880 grok_function_init (decl
, init
)
1884 /* An initializer for a function tells how this function should
1886 tree type
= TREE_TYPE (decl
);
1888 if (TREE_CODE (type
) == FUNCTION_TYPE
)
1889 cp_error ("initializer specified for non-member function `%D'", decl
);
1890 else if (integer_zerop (init
))
1892 DECL_PURE_VIRTUAL_P (decl
) = 1;
1893 if (DECL_OVERLOADED_OPERATOR_P (decl
) == NOP_EXPR
)
1896 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl
))));
1898 if (copy_assignment_arg_p (parmtype
, 1))
1899 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type
) = 1;
1903 cp_error ("invalid initializer for virtual method `%D'", decl
);
1907 cplus_decl_attributes (decl
, attributes
, prefix_attributes
)
1908 tree decl
, attributes
, prefix_attributes
;
1910 if (decl
== NULL_TREE
|| decl
== void_type_node
)
1913 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
1914 decl
= DECL_TEMPLATE_RESULT (decl
);
1916 decl_attributes (decl
, attributes
, prefix_attributes
);
1918 if (TREE_CODE (decl
) == TYPE_DECL
)
1919 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl
), TREE_TYPE (decl
));
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, this routine doesn't
1926 lose the specialization. */
1929 constructor_name_full (thing
)
1932 if (TREE_CODE (thing
) == TEMPLATE_TYPE_PARM
1933 || TREE_CODE (thing
) == BOUND_TEMPLATE_TEMPLATE_PARM
1934 || TREE_CODE (thing
) == TYPENAME_TYPE
)
1935 thing
= TYPE_NAME (thing
);
1936 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing
)))
1938 if (TYPE_WAS_ANONYMOUS (thing
) && TYPE_HAS_CONSTRUCTOR (thing
))
1939 thing
= DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing
), 0)));
1941 thing
= TYPE_NAME (thing
);
1943 if (TREE_CODE (thing
) == TYPE_DECL
1944 || (TREE_CODE (thing
) == TEMPLATE_DECL
1945 && TREE_CODE (DECL_TEMPLATE_RESULT (thing
)) == TYPE_DECL
))
1946 thing
= DECL_NAME (thing
);
1947 my_friendly_assert (TREE_CODE (thing
) == IDENTIFIER_NODE
, 197);
1951 /* CONSTRUCTOR_NAME:
1952 Return the name for the constructor (or destructor) for the
1953 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1954 IDENTIFIER_NODE. When given a template, return the plain
1955 unspecialized name. */
1958 constructor_name (thing
)
1962 thing
= constructor_name_full (thing
);
1963 t
= IDENTIFIER_TEMPLATE (thing
);
1969 /* Defer the compilation of the FN until the end of compilation. */
1975 if (DECL_DEFERRED_FN (fn
))
1977 DECL_DEFERRED_FN (fn
) = 1;
1979 VARRAY_TREE_INIT (deferred_fns
, 32, "deferred_fns");
1981 VARRAY_PUSH_TREE (deferred_fns
, fn
);
1984 /* Hand off a unique name which can be used for variable we don't really
1985 want to know about anyway, for example, the anonymous variables which
1986 are needed to make references work. Declare this thing so we can use it.
1987 The variable created will be of type TYPE, and will have internal
1991 get_temp_name (type
)
1994 char buf
[sizeof (AUTO_TEMP_FORMAT
) + 20];
1996 int toplev
= toplevel_bindings_p ();
1998 sprintf (buf
, AUTO_TEMP_FORMAT
, global_temp_name_counter
++);
1999 decl
= build_decl (VAR_DECL
, get_identifier (buf
), type
);
2000 DECL_ARTIFICIAL (decl
) = 1;
2001 TREE_USED (decl
) = 1;
2002 TREE_STATIC (decl
) = 1;
2004 decl
= pushdecl_top_level (decl
);
2006 /* If this is a local variable, then lay out its rtl now.
2007 Otherwise, callers of this function are responsible for dealing
2008 with this variable's rtl. */
2012 my_friendly_assert (DECL_INITIAL (decl
) == NULL_TREE
,
2019 /* Hunts through the global anonymous union ANON_DECL, building
2020 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2021 returns a VAR_DECL whose size is the same as the size of the
2022 ANON_DECL, if one is available. */
2025 build_anon_union_vars (anon_decl
, elems
, static_p
, external_p
)
2031 tree type
= TREE_TYPE (anon_decl
);
2032 tree main_decl
= NULL_TREE
;
2035 /* Rather than write the code to handle the non-union case,
2036 just give an error. */
2037 if (TREE_CODE (type
) != UNION_TYPE
)
2038 error ("anonymous struct not inside named type");
2040 for (field
= TYPE_FIELDS (type
);
2042 field
= TREE_CHAIN (field
))
2046 if (DECL_ARTIFICIAL (field
))
2048 if (TREE_CODE (field
) != FIELD_DECL
)
2050 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2055 if (TREE_PRIVATE (field
))
2056 cp_pedwarn_at ("private member `%#D' in anonymous union", field
);
2057 else if (TREE_PROTECTED (field
))
2058 cp_pedwarn_at ("protected member `%#D' in anonymous union", field
);
2060 if (DECL_NAME (field
) == NULL_TREE
2061 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2063 decl
= build_anon_union_vars (field
, elems
, static_p
, external_p
);
2067 else if (DECL_NAME (field
) == NULL_TREE
)
2071 decl
= build_decl (VAR_DECL
, DECL_NAME (field
), TREE_TYPE (field
));
2072 /* tell `pushdecl' that this is not tentative. */
2073 DECL_INITIAL (decl
) = error_mark_node
;
2074 TREE_PUBLIC (decl
) = 0;
2075 TREE_STATIC (decl
) = static_p
;
2076 DECL_EXTERNAL (decl
) = external_p
;
2077 decl
= pushdecl (decl
);
2078 DECL_INITIAL (decl
) = NULL_TREE
;
2081 /* Only write out one anon union element--choose the largest
2082 one. We used to try to find one the same size as the union,
2083 but that fails if the ABI forces us to align the union more
2085 if (main_decl
== NULL_TREE
2086 || tree_int_cst_lt (DECL_SIZE (main_decl
), DECL_SIZE (decl
)))
2089 TREE_ASM_WRITTEN (main_decl
) = 1;
2093 /* ??? This causes there to be no debug info written out
2095 TREE_ASM_WRITTEN (decl
) = 1;
2097 if (DECL_NAME (field
) == NULL_TREE
2098 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2099 /* The remainder of the processing was already done in the
2103 /* If there's a cleanup to do, it belongs in the
2104 TREE_PURPOSE of the following TREE_LIST. */
2105 *elems
= tree_cons (NULL_TREE
, decl
, *elems
);
2106 TREE_TYPE (*elems
) = type
;
2112 /* Finish off the processing of a UNION_TYPE structure.
2113 If there are static members, then all members are
2114 static, and must be laid out together. If the
2115 union is an anonymous union, we arrange for that
2116 as well. PUBLIC_P is nonzero if this union is
2117 not declared static. */
2120 finish_anon_union (anon_union_decl
)
2121 tree anon_union_decl
;
2123 tree type
= TREE_TYPE (anon_union_decl
);
2125 int public_p
= TREE_PUBLIC (anon_union_decl
);
2126 int static_p
= TREE_STATIC (anon_union_decl
);
2127 int external_p
= DECL_EXTERNAL (anon_union_decl
);
2129 if (TYPE_FIELDS (type
) == NULL_TREE
)
2134 error ("namespace-scope anonymous aggregates must be static");
2138 main_decl
= build_anon_union_vars (anon_union_decl
,
2139 &DECL_ANON_UNION_ELEMS (anon_union_decl
),
2140 static_p
, external_p
);
2142 if (main_decl
== NULL_TREE
)
2144 warning ("anonymous aggregate with no members");
2150 make_decl_rtl (main_decl
, 0);
2151 COPY_DECL_RTL (main_decl
, anon_union_decl
);
2152 expand_anon_union_decl (anon_union_decl
,
2154 DECL_ANON_UNION_ELEMS (anon_union_decl
));
2157 add_decl_stmt (anon_union_decl
);
2160 /* Finish processing a builtin type TYPE. It's name is NAME,
2161 its fields are in the array FIELDS. LEN is the number of elements
2162 in FIELDS minus one, or put another way, it is the maximum subscript
2165 It is given the same alignment as ALIGN_TYPE. */
2168 finish_builtin_type (type
, name
, fields
, len
, align_type
)
2177 TYPE_FIELDS (type
) = fields
[0];
2178 for (i
= 0; i
< len
; i
++)
2180 layout_type (TREE_TYPE (fields
[i
]));
2181 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
2182 TREE_CHAIN (fields
[i
]) = fields
[i
+1];
2184 DECL_FIELD_CONTEXT (fields
[i
]) = type
;
2185 TYPE_ALIGN (type
) = TYPE_ALIGN (align_type
);
2186 TYPE_USER_ALIGN (type
) = TYPE_USER_ALIGN (align_type
);
2188 #if 0 /* not yet, should get fixed properly later */
2189 TYPE_NAME (type
) = make_type_decl (get_identifier (name
), type
);
2191 TYPE_NAME (type
) = build_decl (TYPE_DECL
, get_identifier (name
), type
);
2193 TYPE_STUB_DECL (type
) = TYPE_NAME (type
);
2194 layout_decl (TYPE_NAME (type
), 0);
2197 /* Auxiliary functions to make type signatures for
2198 `operator new' and `operator delete' correspond to
2199 what compiler will be expecting. */
2202 coerce_new_type (type
)
2206 tree args
= TYPE_ARG_TYPES (type
);
2208 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
2210 if (!same_type_p (TREE_TYPE (type
), ptr_type_node
))
2211 e
= 1, cp_error ("`operator new' must return type `%T'", ptr_type_node
);
2213 if (!args
|| args
== void_list_node
2214 || !same_type_p (TREE_VALUE (args
), c_size_type_node
))
2217 if (args
&& args
!= void_list_node
)
2218 args
= TREE_CHAIN (args
);
2219 cp_error ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node
);
2224 args
= tree_cons (NULL_TREE
, c_size_type_node
, args
);
2227 type
= build_exception_variant
2228 (build_function_type (ptr_type_node
, args
),
2229 TYPE_RAISES_EXCEPTIONS (type
));
2237 coerce_delete_type (type
)
2241 tree args
= TYPE_ARG_TYPES (type
);
2243 my_friendly_assert (TREE_CODE (type
) == FUNCTION_TYPE
, 20001107);
2245 if (!same_type_p (TREE_TYPE (type
), void_type_node
))
2246 e
= 1, cp_error ("`operator delete' must return type `%T'", void_type_node
);
2248 if (!args
|| args
== void_list_node
2249 || !same_type_p (TREE_VALUE (args
), ptr_type_node
))
2252 if (args
&& args
!= void_list_node
)
2253 args
= TREE_CHAIN (args
);
2254 cp_error ("`operator delete' takes type `%T' as first parameter", ptr_type_node
);
2259 args
= tree_cons (NULL_TREE
, ptr_type_node
, args
);
2262 type
= build_exception_variant
2263 (build_function_type (void_type_node
, args
),
2264 TYPE_RAISES_EXCEPTIONS (type
));
2273 mark_vtable_entries (decl
)
2276 tree entries
= CONSTRUCTOR_ELTS (DECL_INITIAL (decl
));
2278 for (; entries
; entries
= TREE_CHAIN (entries
))
2283 fnaddr
= (flag_vtable_thunks
? TREE_VALUE (entries
)
2284 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries
)));
2286 if (TREE_CODE (fnaddr
) != ADDR_EXPR
)
2287 /* This entry is an offset: a virtual base class offset, a
2288 virtual call offset, and RTTI offset, etc. */
2291 fn
= TREE_OPERAND (fnaddr
, 0);
2292 TREE_ADDRESSABLE (fn
) = 1;
2293 /* When we don't have vcall offsets, we output thunks whenever
2294 we output the vtables that contain them. With vcall offsets,
2295 we know all the thunks we'll need when we emit a virtual
2296 function, so we emit the thunks there instead. */
2297 if (DECL_THUNK_P (fn
))
2298 use_thunk (fn
, THUNK_GENERATE_WITH_VTABLE_P (fn
));
2303 /* Set DECL up to have the closest approximation of "initialized common"
2304 linkage available. */
2307 comdat_linkage (decl
)
2311 make_decl_one_only (decl
);
2312 else if (TREE_CODE (decl
) == FUNCTION_DECL
2313 || (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
)))
2314 /* We can just emit function and compiler-generated variables
2315 statically; having multiple copies is (for the most part) only
2318 There are two correctness issues, however: the address of a
2319 template instantiation with external linkage should be the
2320 same, independent of what translation unit asks for the
2321 address, and this will not hold when we emit multiple copies of
2322 the function. However, there's little else we can do.
2324 Also, by default, the typeinfo implementation for the new ABI
2325 assumes that there will be only one copy of the string used as
2326 the name for each type. Therefore, if weak symbols are
2327 unavailable, the run-time library should perform a more
2328 conservative check; it should perform a string comparison,
2329 rather than an address comparison. */
2330 TREE_PUBLIC (decl
) = 0;
2333 /* Static data member template instantiations, however, cannot
2334 have multiple copies. */
2335 if (DECL_INITIAL (decl
) == 0
2336 || DECL_INITIAL (decl
) == error_mark_node
)
2337 DECL_COMMON (decl
) = 1;
2338 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl
)))
2340 DECL_COMMON (decl
) = 1;
2341 DECL_INITIAL (decl
) = error_mark_node
;
2345 /* We can't do anything useful; leave vars for explicit
2347 DECL_EXTERNAL (decl
) = 1;
2348 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2352 if (DECL_LANG_SPECIFIC (decl
))
2353 DECL_COMDAT (decl
) = 1;
2356 /* For win32 we also want to put explicit instantiations in
2357 linkonce sections, so that they will be merged with implicit
2358 instantiations; otherwise we get duplicate symbol errors. */
2361 maybe_make_one_only (decl
)
2364 /* We used to say that this was not necessary on targets that support weak
2365 symbols, because the implicit instantiations will defer to the explicit
2366 one. However, that's not actually the case in SVR4; a strong definition
2367 after a weak one is an error. Also, not making explicit
2368 instantiations one_only means that we can end up with two copies of
2369 some template instantiations. */
2370 if (! supports_one_only ())
2373 /* We can't set DECL_COMDAT on functions, or finish_file will think
2374 we can get away with not emitting them if they aren't used. We need
2375 to for variables so that cp_finish_decl will update their linkage,
2376 because their DECL_INITIAL may not have been set properly yet. */
2378 make_decl_one_only (decl
);
2380 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_LANG_SPECIFIC (decl
))
2381 DECL_COMDAT (decl
) = 1;
2384 /* Returns the virtual function with which the vtable for TYPE is
2385 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2393 if (TYPE_FOR_JAVA (type
)
2394 || CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
2395 || CLASSTYPE_INTERFACE_KNOWN (type
))
2398 for (method
= TYPE_METHODS (type
); method
!= NULL_TREE
;
2399 method
= TREE_CHAIN (method
))
2400 if (DECL_VINDEX (method
) != NULL_TREE
2401 && ! DECL_DECLARED_INLINE_P (method
)
2402 && ! DECL_PURE_VIRTUAL_P (method
))
2408 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2409 based on TYPE and other static flags.
2411 Note that anything public is tagged TREE_PUBLIC, whether
2412 it's public in this file or in another one. */
2415 import_export_vtable (decl
, type
, final
)
2419 if (DECL_INTERFACE_KNOWN (decl
))
2422 if (TYPE_FOR_JAVA (type
))
2424 TREE_PUBLIC (decl
) = 1;
2425 DECL_EXTERNAL (decl
) = 1;
2426 DECL_INTERFACE_KNOWN (decl
) = 1;
2428 else if (CLASSTYPE_INTERFACE_KNOWN (type
))
2430 TREE_PUBLIC (decl
) = 1;
2431 DECL_EXTERNAL (decl
) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type
);
2432 DECL_INTERFACE_KNOWN (decl
) = 1;
2436 /* We can only wait to decide if we have real non-inline virtual
2437 functions in our class, or if we come from a template. */
2439 int found
= (CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
2440 || key_method (type
));
2442 if (final
|| ! found
)
2444 comdat_linkage (decl
);
2445 DECL_EXTERNAL (decl
) = 0;
2449 TREE_PUBLIC (decl
) = 1;
2450 DECL_EXTERNAL (decl
) = 1;
2455 /* Determine whether or not we want to specifically import or export CTYPE,
2456 using various heuristics. */
2459 import_export_class (ctype
)
2462 /* -1 for imported, 1 for exported. */
2463 int import_export
= 0;
2465 /* It only makes sense to call this function at EOF. The reason is
2466 that this function looks at whether or not the first non-inline
2467 non-abstract virtual member function has been defined in this
2468 translation unit. But, we can't possibly know that until we've
2469 seen the entire translation unit. */
2470 my_friendly_assert (at_eof
, 20000226);
2472 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2475 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2476 we will have CLASSTYPE_INTERFACE_ONLY set but not
2477 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2478 heuristic because someone will supply a #pragma implementation
2479 elsewhere, and deducing it here would produce a conflict. */
2480 if (CLASSTYPE_INTERFACE_ONLY (ctype
))
2483 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2484 /* FIXME this should really use some sort of target-independent macro. */
2485 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype
)))
2487 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype
)))
2491 /* If we got -fno-implicit-templates, we import template classes that
2492 weren't explicitly instantiated. */
2493 if (import_export
== 0
2494 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype
)
2495 && ! flag_implicit_templates
)
2498 /* Base our import/export status on that of the first non-inline,
2499 non-pure virtual function, if any. */
2500 if (import_export
== 0
2501 && TYPE_POLYMORPHIC_P (ctype
))
2503 tree method
= key_method (ctype
);
2505 import_export
= (DECL_REALLY_EXTERN (method
) ? -1 : 1);
2508 #ifdef MULTIPLE_SYMBOL_SPACES
2509 if (import_export
== -1)
2515 SET_CLASSTYPE_INTERFACE_KNOWN (ctype
);
2516 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype
) = (import_export
> 0);
2517 CLASSTYPE_INTERFACE_ONLY (ctype
) = (import_export
< 0);
2521 /* We need to describe to the assembler the relationship between
2522 a vtable and the vtable of the parent class. */
2525 output_vtable_inherit (vars
)
2531 op
[0] = XEXP (DECL_RTL (vars
), 0); /* strip the mem ref */
2533 parent
= binfo_for_vtable (vars
);
2535 if (parent
== TYPE_BINFO (DECL_CONTEXT (vars
)))
2539 parent
= get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent
)));
2540 op
[1] = XEXP (DECL_RTL (parent
), 0); /* strip the mem ref */
2543 my_friendly_abort (980826);
2545 output_asm_insn (".vtable_inherit %c0, %c1", op
);
2549 finish_vtable_vardecl (t
, data
)
2551 void *data ATTRIBUTE_UNUSED
;
2554 tree ctype
= DECL_CONTEXT (vars
);
2555 import_export_class (ctype
);
2556 import_export_vtable (vars
, ctype
, 1);
2558 if (! DECL_EXTERNAL (vars
)
2559 && DECL_NEEDED_P (vars
)
2560 && ! TREE_ASM_WRITTEN (vars
))
2562 if (TREE_TYPE (vars
) == void_type_node
)
2563 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2567 mark_vtable_entries (vars
);
2568 if (TREE_TYPE (DECL_INITIAL (vars
)) == 0)
2569 store_init_value (vars
, DECL_INITIAL (vars
));
2571 if (write_symbols
== DWARF_DEBUG
|| write_symbols
== DWARF2_DEBUG
)
2573 /* Mark the VAR_DECL node representing the vtable itself as a
2574 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2575 It is rather important that such things be ignored because
2576 any effort to actually generate DWARF for them will run
2577 into trouble when/if we encounter code like:
2580 struct S { virtual void member (); };
2582 because the artificial declaration of the vtable itself (as
2583 manufactured by the g++ front end) will say that the vtable
2584 is a static member of `S' but only *after* the debug output
2585 for the definition of `S' has already been output. This causes
2586 grief because the DWARF entry for the definition of the vtable
2587 will try to refer back to an earlier *declaration* of the
2588 vtable as a static member of `S' and there won't be one.
2589 We might be able to arrange to have the "vtable static member"
2590 attached to the member list for `S' before the debug info for
2591 `S' get written (which would solve the problem) but that would
2592 require more intrusive changes to the g++ front end. */
2594 DECL_IGNORED_P (vars
) = 1;
2597 /* Always make vtables weak. */
2599 comdat_linkage (vars
);
2601 rest_of_decl_compilation (vars
, NULL_PTR
, 1, 1);
2604 output_vtable_inherit (vars
);
2606 /* Because we're only doing syntax-checking, we'll never end up
2607 actually marking the variable as written. */
2608 if (flag_syntax_only
)
2609 TREE_ASM_WRITTEN (vars
) = 1;
2611 /* Since we're writing out the vtable here, also write the debug
2613 note_debug_info_needed (ctype
);
2618 /* If the references to this class' vtables were optimized away, still
2619 emit the appropriate debugging information. See dfs_debug_mark. */
2620 if (DECL_COMDAT (vars
)
2621 && CLASSTYPE_DEBUG_REQUESTED (ctype
))
2622 note_debug_info_needed (ctype
);
2628 prune_vtable_vardecl (t
, data
)
2630 void *data ATTRIBUTE_UNUSED
;
2632 *t
= TREE_CHAIN (*t
);
2636 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2637 inline function or template instantiation at end-of-file. */
2640 import_export_decl (decl
)
2643 if (DECL_INTERFACE_KNOWN (decl
))
2646 if (DECL_TEMPLATE_INSTANTIATION (decl
)
2647 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
2649 DECL_NOT_REALLY_EXTERN (decl
) = 1;
2650 if ((DECL_IMPLICIT_INSTANTIATION (decl
)
2651 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl
))
2652 && (flag_implicit_templates
2653 || (flag_implicit_inline_templates
2654 && DECL_DECLARED_INLINE_P (decl
))))
2656 if (!TREE_PUBLIC (decl
))
2657 /* Templates are allowed to have internal linkage. See
2661 comdat_linkage (decl
);
2664 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2666 else if (DECL_FUNCTION_MEMBER_P (decl
))
2668 if (!DECL_DECLARED_INLINE_P (decl
))
2670 tree ctype
= DECL_CONTEXT (decl
);
2671 import_export_class (ctype
);
2672 if (CLASSTYPE_INTERFACE_KNOWN (ctype
))
2674 DECL_NOT_REALLY_EXTERN (decl
)
2675 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2676 || (DECL_DECLARED_INLINE_P (decl
)
2677 && ! flag_implement_inlines
2678 && !DECL_VINDEX (decl
)));
2680 /* Always make artificials weak. */
2681 if (DECL_ARTIFICIAL (decl
) && flag_weak
)
2682 comdat_linkage (decl
);
2684 maybe_make_one_only (decl
);
2688 comdat_linkage (decl
);
2690 else if (DECL_TINFO_FN_P (decl
))
2692 tree ctype
= TREE_TYPE (DECL_NAME (decl
));
2694 if (IS_AGGR_TYPE (ctype
))
2695 import_export_class (ctype
);
2697 if (IS_AGGR_TYPE (ctype
) && CLASSTYPE_INTERFACE_KNOWN (ctype
)
2698 && TYPE_POLYMORPHIC_P (ctype
)
2699 /* If -fno-rtti, we're not necessarily emitting this stuff with
2700 the class, so go ahead and emit it now. This can happen
2701 when a class is used in exception handling. */
2703 /* If the type is a cv-qualified variant of a type, then we
2704 must emit the tinfo function in this translation unit
2705 since it will not be emitted when the vtable for the type
2706 is output (which is when the unqualified version is
2708 && same_type_p (ctype
, TYPE_MAIN_VARIANT (ctype
)))
2710 DECL_NOT_REALLY_EXTERN (decl
)
2711 = ! (CLASSTYPE_INTERFACE_ONLY (ctype
)
2712 || (DECL_DECLARED_INLINE_P (decl
)
2713 && ! flag_implement_inlines
2714 && !DECL_VINDEX (decl
)));
2716 /* Always make artificials weak. */
2718 comdat_linkage (decl
);
2720 else if (TYPE_BUILT_IN (ctype
)
2721 && same_type_p (ctype
, TYPE_MAIN_VARIANT (ctype
)))
2722 DECL_NOT_REALLY_EXTERN (decl
) = 0;
2724 comdat_linkage (decl
);
2727 comdat_linkage (decl
);
2729 DECL_INTERFACE_KNOWN (decl
) = 1;
2733 build_cleanup (decl
)
2737 tree type
= TREE_TYPE (decl
);
2739 if (TREE_CODE (type
) == ARRAY_TYPE
)
2743 mark_addressable (decl
);
2744 temp
= build1 (ADDR_EXPR
, build_pointer_type (type
), decl
);
2746 temp
= build_delete (TREE_TYPE (temp
), temp
,
2747 sfk_complete_destructor
,
2748 LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
|LOOKUP_DESTRUCTOR
, 0);
2752 /* Returns the initialization guard variable for the variable DECL,
2753 which has static storage duration. */
2762 sname
= mangle_guard_variable (decl
);
2763 guard
= IDENTIFIER_GLOBAL_VALUE (sname
);
2768 /* Under the new ABI, we use a type that is big enough to
2769 contain a mutex as well as an integer counter. */
2770 guard_type
= long_long_integer_type_node
;
2771 guard
= build_decl (VAR_DECL
, sname
, guard_type
);
2773 /* The guard should have the same linkage as what it guards. */
2774 TREE_PUBLIC (guard
) = TREE_PUBLIC (decl
);
2775 TREE_STATIC (guard
) = TREE_STATIC (decl
);
2776 DECL_COMMON (guard
) = DECL_COMMON (decl
);
2777 DECL_ONE_ONLY (guard
) = DECL_ONE_ONLY (decl
);
2778 if (TREE_PUBLIC (decl
))
2779 DECL_WEAK (guard
) = DECL_WEAK (decl
);
2781 DECL_ARTIFICIAL (guard
) = 1;
2782 TREE_USED (guard
) = 1;
2783 pushdecl_top_level (guard
);
2784 cp_finish_decl (guard
, NULL_TREE
, NULL_TREE
, 0);
2789 /* Return those bits of the GUARD variable that should be set when the
2790 guarded entity is actually initialized. */
2793 get_guard_bits (guard
)
2796 /* Under the new ABI, we only set the first byte of the guard,
2797 in order to leave room for a mutex in the high-order bits. */
2798 guard
= build1 (ADDR_EXPR
,
2799 build_pointer_type (TREE_TYPE (guard
)),
2801 guard
= build1 (NOP_EXPR
,
2802 build_pointer_type (char_type_node
),
2804 guard
= build1 (INDIRECT_REF
, char_type_node
, guard
);
2809 /* Return an expression which determines whether or not the GUARD
2810 variable has already been initialized. */
2813 get_guard_cond (guard
)
2818 /* Check to see if the GUARD is zero. */
2819 guard
= get_guard_bits (guard
);
2820 guard_value
= integer_zero_node
;
2821 if (!same_type_p (TREE_TYPE (guard_value
), TREE_TYPE (guard
)))
2822 guard_value
= convert (TREE_TYPE (guard
), guard_value
);
2823 return cp_build_binary_op (EQ_EXPR
, guard
, guard_value
);
2826 /* Return an expression which sets the GUARD variable, indicating that
2827 the variable being guarded has been initialized. */
2835 /* Set the GUARD to one. */
2836 guard
= get_guard_bits (guard
);
2837 guard_init
= integer_one_node
;
2838 if (!same_type_p (TREE_TYPE (guard_init
), TREE_TYPE (guard
)))
2839 guard_init
= convert (TREE_TYPE (guard
), guard_init
);
2840 return build_modify_expr (guard
, NOP_EXPR
, guard_init
);
2843 /* Start the process of running a particular set of global constructors
2844 or destructors. Subroutine of do_[cd]tors. */
2847 start_objects (method_type
, initp
)
2848 int method_type
, initp
;
2854 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2856 if (initp
!= DEFAULT_INIT_PRIORITY
)
2866 sprintf (type
, "%c%c%.5u", method_type
, joiner
, initp
);
2869 sprintf (type
, "%c", method_type
);
2871 fnname
= get_file_function_name_long (type
);
2873 start_function (void_list_node
,
2874 make_call_declarator (fnname
, void_list_node
, NULL_TREE
,
2876 NULL_TREE
, SF_DEFAULT
);
2878 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2879 /* It can be a static function as long as collect2 does not have
2880 to scan the object file to find its ctor/dtor routine. */
2881 TREE_PUBLIC (current_function_decl
) = 0;
2884 /* Mark this declaration as used to avoid spurious warnings. */
2885 TREE_USED (current_function_decl
) = 1;
2887 /* Mark this function as a global constructor or destructor. */
2888 if (method_type
== 'I')
2889 DECL_GLOBAL_CTOR_P (current_function_decl
) = 1;
2891 DECL_GLOBAL_DTOR_P (current_function_decl
) = 1;
2892 GLOBAL_INIT_PRIORITY (current_function_decl
) = initp
;
2894 body
= begin_compound_stmt (/*has_no_scope=*/0);
2896 /* We cannot allow these functions to be elided, even if they do not
2897 have external linkage. And, there's no point in deferring
2898 copmilation of thes functions; they're all going to have to be
2900 current_function_cannot_inline
2901 = "static constructors and destructors cannot be inlined";
2906 /* Finish the process of running a particular set of global constructors
2907 or destructors. Subroutine of do_[cd]tors. */
2910 finish_objects (method_type
, initp
, body
)
2911 int method_type
, initp
;
2918 finish_compound_stmt (/*has_no_scope=*/0, body
);
2919 fn
= finish_function (0);
2922 /* When only doing semantic analysis, and no RTL generation, we
2923 can't call functions that directly emit assembly code; there is
2924 no assembly file in which to put the code. */
2925 if (flag_syntax_only
)
2928 fnname
= XSTR (XEXP (DECL_RTL (fn
), 0), 0);
2929 if (initp
== DEFAULT_INIT_PRIORITY
)
2931 if (method_type
== 'I')
2932 assemble_constructor (fnname
);
2934 assemble_destructor (fnname
);
2936 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
2937 /* If we're using init priority we can't use assemble_*tor, but on ELF
2938 targets we can stick the references into named sections for GNU ld
2943 sprintf (buf
, ".%ctors.%.5u", method_type
== 'I' ? 'c' : 'd',
2944 /* invert the numbering so the linker puts us in the proper
2945 order; constructors are run from right to left, and the
2946 linker sorts in increasing order. */
2947 MAX_INIT_PRIORITY
- initp
);
2948 named_section (NULL_TREE
, buf
, 0);
2949 assemble_integer (gen_rtx_SYMBOL_REF (Pmode
, fnname
),
2950 POINTER_SIZE
/ BITS_PER_UNIT
, 1);
2955 /* The names of the parameters to the function created to handle
2956 initializations and destructions for objects with static storage
2958 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2959 #define PRIORITY_IDENTIFIER "__priority"
2961 /* The name of the function we create to handle initializations and
2962 destructions for objects with static storage duration. */
2963 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2965 /* The declaration for the __INITIALIZE_P argument. */
2966 static tree initialize_p_decl
;
2968 /* The declaration for the __PRIORITY argument. */
2969 static tree priority_decl
;
2971 /* The declaration for the static storage duration function. */
2972 static tree ssdf_decl
;
2974 /* All the static storage duration functions created in this
2975 translation unit. */
2976 static varray_type ssdf_decls
;
2978 /* A map from priority levels to information about that priority
2979 level. There may be many such levels, so efficient lookup is
2981 static splay_tree priority_info_map
;
2983 /* Begins the generation of the function that will handle all
2984 initialization and destruction of objects with static storage
2985 duration. The function generated takes two parameters of type
2986 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2987 non-zero, it performs initializations. Otherwise, it performs
2988 destructions. It only performs those initializations or
2989 destructions with the indicated __PRIORITY. The generated function
2992 It is assumed that this function will only be called once per
2993 translation unit. */
2996 start_static_storage_duration_function ()
2998 static unsigned ssdf_number
;
3003 char id
[sizeof (SSDF_IDENTIFIER
) + 1 /* '\0' */ + 32];
3005 /* Create the identifier for this function. It will be of the form
3006 SSDF_IDENTIFIER_<number>. */
3007 sprintf (id
, "%s_%u", SSDF_IDENTIFIER
, ssdf_number
++);
3008 if (ssdf_number
== 0)
3010 /* Overflow occurred. That means there are at least 4 billion
3011 initialization functions. */
3012 sorry ("too many initialization functions required");
3013 my_friendly_abort (19990430);
3016 /* Create the parameters. */
3017 parm_types
= void_list_node
;
3018 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
3019 parm_types
= tree_cons (NULL_TREE
, integer_type_node
, parm_types
);
3020 type
= build_function_type (void_type_node
, parm_types
);
3022 /* Create the FUNCTION_DECL itself. */
3023 ssdf_decl
= build_lang_decl (FUNCTION_DECL
,
3024 get_identifier (id
),
3026 TREE_PUBLIC (ssdf_decl
) = 0;
3027 DECL_ARTIFICIAL (ssdf_decl
) = 1;
3029 /* Put this function in the list of functions to be called from the
3030 static constructors and destructors. */
3033 VARRAY_TREE_INIT (ssdf_decls
, 32, "ssdf_decls");
3035 /* Take this opportunity to initialize the map from priority
3036 numbers to information about that priority level. */
3037 priority_info_map
= splay_tree_new (splay_tree_compare_ints
,
3038 /*delete_key_fn=*/0,
3039 /*delete_value_fn=*/
3040 (splay_tree_delete_value_fn
) &free
);
3042 /* We always need to generate functions for the
3043 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
3044 priorities later, we'll be sure to find the
3045 DEFAULT_INIT_PRIORITY. */
3046 get_priority_info (DEFAULT_INIT_PRIORITY
);
3049 VARRAY_PUSH_TREE (ssdf_decls
, ssdf_decl
);
3051 /* Create the argument list. */
3052 initialize_p_decl
= build_decl (PARM_DECL
,
3053 get_identifier (INITIALIZE_P_IDENTIFIER
),
3055 DECL_CONTEXT (initialize_p_decl
) = ssdf_decl
;
3056 DECL_ARG_TYPE (initialize_p_decl
) = integer_type_node
;
3057 TREE_USED (initialize_p_decl
) = 1;
3058 priority_decl
= build_decl (PARM_DECL
, get_identifier (PRIORITY_IDENTIFIER
),
3060 DECL_CONTEXT (priority_decl
) = ssdf_decl
;
3061 DECL_ARG_TYPE (priority_decl
) = integer_type_node
;
3062 TREE_USED (priority_decl
) = 1;
3064 TREE_CHAIN (initialize_p_decl
) = priority_decl
;
3065 DECL_ARGUMENTS (ssdf_decl
) = initialize_p_decl
;
3067 /* Put the function in the global scope. */
3068 pushdecl (ssdf_decl
);
3070 /* Start the function itself. This is equivalent to declarating the
3073 static void __ssdf (int __initialize_p, init __priority_p);
3075 It is static because we only need to call this function from the
3076 various constructor and destructor functions for this module. */
3077 start_function (/*specs=*/NULL_TREE
,
3079 /*attrs=*/NULL_TREE
,
3082 /* Set up the scope of the outermost block in the function. */
3083 body
= begin_compound_stmt (/*has_no_scope=*/0);
3085 /* This function must not be deferred because we are depending on
3086 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3087 current_function_cannot_inline
3088 = "static storage duration functions cannot be inlined";
3093 /* Finish the generation of the function which performs initialization
3094 and destruction of objects with static storage duration. After
3095 this point, no more such objects can be created. */
3098 finish_static_storage_duration_function (body
)
3101 /* Close out the function. */
3102 finish_compound_stmt (/*has_no_scope=*/0, body
);
3103 expand_body (finish_function (0));
3106 /* Return the information about the indicated PRIORITY level. If no
3107 code to handle this level has yet been generated, generate the
3108 appropriate prologue. */
3110 static priority_info
3111 get_priority_info (priority
)
3117 n
= splay_tree_lookup (priority_info_map
,
3118 (splay_tree_key
) priority
);
3121 /* Create a new priority information structure, and insert it
3123 pi
= (priority_info
) xmalloc (sizeof (struct priority_info_s
));
3124 pi
->initializations_p
= 0;
3125 pi
->destructions_p
= 0;
3126 splay_tree_insert (priority_info_map
,
3127 (splay_tree_key
) priority
,
3128 (splay_tree_value
) pi
);
3131 pi
= (priority_info
) n
->value
;
3136 /* Set up to handle the initialization or destruction of DECL. If
3137 INITP is non-zero, we are initializing the variable. Otherwise, we
3138 are destroying it. */
3141 start_static_initialization_or_destruction (decl
, initp
)
3145 tree guard_if_stmt
= NULL_TREE
;
3152 /* Figure out the priority for this declaration. */
3153 priority
= DECL_INIT_PRIORITY (decl
);
3155 priority
= DEFAULT_INIT_PRIORITY
;
3157 /* Remember that we had an initialization or finalization at this
3159 pi
= get_priority_info (priority
);
3161 pi
->initializations_p
= 1;
3163 pi
->destructions_p
= 1;
3165 /* Trick the compiler into thinking we are at the file and line
3166 where DECL was declared so that error-messages make sense, and so
3167 that the debugger will show somewhat sensible file and line
3169 input_filename
= DECL_SOURCE_FILE (decl
);
3170 lineno
= DECL_SOURCE_LINE (decl
);
3176 Access control for implicit calls to the constructors,
3177 the conversion functions, or the destructor called to
3178 create and destroy a static data member is performed as
3179 if these calls appeared in the scope of the member's
3182 we pretend we are in a static member function of the class of
3183 which the DECL is a member. */
3184 if (member_p (decl
))
3186 DECL_CONTEXT (current_function_decl
) = DECL_CONTEXT (decl
);
3187 DECL_STATIC_FUNCTION_P (current_function_decl
) = 1;
3190 /* Conditionalize this initialization on being in the right priority
3191 and being initializing/finalizing appropriately. */
3192 guard_if_stmt
= begin_if_stmt ();
3193 cond
= cp_build_binary_op (EQ_EXPR
,
3195 build_int_2 (priority
, 0));
3196 init_cond
= initp
? integer_one_node
: integer_zero_node
;
3197 init_cond
= cp_build_binary_op (EQ_EXPR
,
3200 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, init_cond
);
3202 /* Assume we don't need a guard. */
3204 /* We need a guard if this is an object with external linkage that
3205 might be initialized in more than one place. (For example, a
3206 static data member of a template, when the data member requires
3208 if (TREE_PUBLIC (decl
) && (DECL_COMMON (decl
)
3209 || DECL_ONE_ONLY (decl
)
3210 || DECL_WEAK (decl
)))
3214 guard
= get_guard (decl
);
3216 /* When using __cxa_atexit, we just check the GUARD as we would
3217 for a local static. */
3218 if (flag_use_cxa_atexit
)
3220 /* When using __cxa_atexit, we never try to destroy
3221 anything from a static destructor. */
3222 my_friendly_assert (initp
, 20000629);
3223 guard_cond
= get_guard_cond (guard
);
3225 /* If we don't have __cxa_atexit, then we will be running
3226 destructors from .fini sections, or their equivalents. So,
3227 we need to know how many times we've tried to initialize this
3228 object. We do initializations only if the GUARD is zero,
3229 i.e., if we are the first to initialize the variable. We do
3230 destructions only if the GUARD is one, i.e., if we are the
3231 last to destroy the variable. */
3234 = cp_build_binary_op (EQ_EXPR
,
3235 build_unary_op (PREINCREMENT_EXPR
,
3241 = cp_build_binary_op (EQ_EXPR
,
3242 build_unary_op (PREDECREMENT_EXPR
,
3247 cond
= cp_build_binary_op (TRUTH_ANDIF_EXPR
, cond
, guard_cond
);
3250 finish_if_stmt_cond (cond
, guard_if_stmt
);
3252 /* If we're using __cxa_atexit, we have not already set the GUARD,
3253 so we must do so now. */
3254 if (guard
&& initp
&& flag_use_cxa_atexit
)
3255 finish_expr_stmt (set_guard (guard
));
3257 return guard_if_stmt
;
3260 /* We've just finished generating code to do an initialization or
3261 finalization. GUARD_IF_STMT is the if-statement we used to guard
3262 the initialization. */
3265 finish_static_initialization_or_destruction (guard_if_stmt
)
3268 finish_then_clause (guard_if_stmt
);
3271 /* Now that we're done with DECL we don't need to pretend to be a
3272 member of its class any longer. */
3273 DECL_CONTEXT (current_function_decl
) = NULL_TREE
;
3274 DECL_STATIC_FUNCTION_P (current_function_decl
) = 0;
3277 /* Generate code to do the static initialization of DECL. The
3278 initialization is INIT. If DECL may be initialized more than once
3279 in different object files, GUARD is the guard variable to
3280 check. PRIORITY is the priority for the initialization. */
3283 do_static_initialization (decl
, init
)
3290 /* Set up for the initialization. */
3292 = start_static_initialization_or_destruction (decl
,
3295 /* Do the initialization itself. */
3296 if (IS_AGGR_TYPE (TREE_TYPE (decl
))
3297 || TREE_CODE (TREE_TYPE (decl
)) == ARRAY_TYPE
)
3298 expr
= build_aggr_init (decl
, init
, 0);
3301 expr
= build (INIT_EXPR
, TREE_TYPE (decl
), decl
, init
);
3302 TREE_SIDE_EFFECTS (expr
) = 1;
3304 finish_expr_stmt (expr
);
3306 /* If we're using __cxa_atexit, register a a function that calls the
3307 destructor for the object. */
3308 if (flag_use_cxa_atexit
)
3309 register_dtor_fn (decl
);
3312 finish_static_initialization_or_destruction (guard_if_stmt
);
3315 /* Generate code to do the static destruction of DECL. If DECL may be
3316 initialized more than once in different object files, GUARD is the
3317 guard variable to check. PRIORITY is the priority for the
3321 do_static_destruction (decl
)
3326 /* If we're using __cxa_atexit, then destructors are registered
3327 immediately after objects are initialized. */
3328 my_friendly_assert (!flag_use_cxa_atexit
, 20000121);
3330 /* If we don't need a destructor, there's nothing to do. */
3331 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3334 /* Actually do the destruction. */
3335 guard_if_stmt
= start_static_initialization_or_destruction (decl
,
3337 finish_expr_stmt (build_cleanup (decl
));
3338 finish_static_initialization_or_destruction (guard_if_stmt
);
3341 /* VARS is a list of variables with static storage duration which may
3342 need initialization and/or finalization. Remove those variables
3343 that don't really need to be initialized or finalized, and return
3344 the resulting list. The order in which the variables appear in
3345 VARS is in reverse order of the order in which they should actually
3346 be initialized. The list we return is in the unreversed order;
3347 i.e., the first variable should be initialized first. */
3350 prune_vars_needing_no_initialization (vars
)
3356 for (var
= vars
, result
= NULL_TREE
;
3358 var
= TREE_CHAIN (var
))
3360 tree decl
= TREE_VALUE (var
);
3361 tree init
= TREE_PURPOSE (var
);
3363 /* Deal gracefully with error. */
3364 if (decl
== error_mark_node
)
3367 /* The only things that can be initialized are variables. */
3368 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
, 19990420);
3370 /* If this object is not defined, we don't need to do anything
3372 if (DECL_EXTERNAL (decl
))
3375 /* Also, if the initializer already contains errors, we can bail
3377 if (init
&& TREE_CODE (init
) == TREE_LIST
3378 && value_member (error_mark_node
, init
))
3381 /* This variable is going to need initialization and/or
3382 finalization, so we add it to the list. */
3383 result
= tree_cons (init
, decl
, result
);
3389 /* Make sure we have told the back end about all the variables in
3393 write_out_vars (vars
)
3398 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3399 if (! TREE_ASM_WRITTEN (TREE_VALUE (v
)))
3400 rest_of_decl_compilation (TREE_VALUE (v
), 0, 1, 1);
3403 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3404 (otherwise) that will initialize all gobal objects with static
3405 storage duration having the indicated PRIORITY. */
3408 generate_ctor_or_dtor_function (constructor_p
, priority
)
3417 /* We use `I' to indicate initialization and `D' to indicate
3424 /* Begin the function. */
3425 body
= start_objects (function_key
, priority
);
3427 /* Call the static storage duration function with appropriate
3429 for (i
= 0; i
< ssdf_decls
->elements_used
; ++i
)
3431 arguments
= tree_cons (NULL_TREE
, build_int_2 (priority
, 0),
3433 arguments
= tree_cons (NULL_TREE
, build_int_2 (constructor_p
, 0),
3435 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls
, i
),
3439 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3440 calls to any functions marked with attributes indicating that
3441 they should be called at initialization- or destruction-time. */
3442 if (priority
== DEFAULT_INIT_PRIORITY
)
3446 for (fns
= constructor_p
? static_ctors
: static_dtors
;
3448 fns
= TREE_CHAIN (fns
))
3449 finish_expr_stmt (build_function_call (TREE_VALUE (fns
), NULL_TREE
));
3452 /* Close out the function. */
3453 finish_objects (function_key
, priority
, body
);
3456 /* Generate constructor and destructor functions for the priority
3460 generate_ctor_and_dtor_functions_for_priority (n
, data
)
3462 void *data ATTRIBUTE_UNUSED
;
3464 int priority
= (int) n
->key
;
3465 priority_info pi
= (priority_info
) n
->value
;
3467 /* Generate the functions themselves, but only if they are really
3469 if (pi
->initializations_p
3470 || (priority
== DEFAULT_INIT_PRIORITY
&& static_ctors
))
3471 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3473 if (pi
->destructions_p
3474 || (priority
== DEFAULT_INIT_PRIORITY
&& static_dtors
))
3475 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3478 /* Keep iterating. */
3482 /* This routine is called from the last rule in yyparse ().
3483 Its job is to create all the code needed to initialize and
3484 destroy the global aggregates. We do the destruction
3485 first, since that way we only need to reverse the decls once. */
3496 /* Bad parse errors. Just forget about it. */
3497 if (! global_bindings_p () || current_class_type
|| decl_namespace_list
)
3500 /* Otherwise, GDB can get confused, because in only knows
3501 about source for LINENO-1 lines. */
3504 interface_unknown
= 1;
3507 /* We now have to write out all the stuff we put off writing out.
3510 o Template specializations that we have not yet instantiated,
3511 but which are needed.
3512 o Initialization and destruction for non-local objects with
3513 static storage duration. (Local objects with static storage
3514 duration are initialized when their scope is first entered,
3515 and are cleaned up via atexit.)
3516 o Virtual function tables.
3518 All of these may cause others to be needed. For example,
3519 instantiating one function may cause another to be needed, and
3520 generating the intiailzer for an object may cause templates to be
3521 instantiated, etc., etc. */
3523 timevar_push (TV_VARCONST
);
3525 emit_support_tinfos ();
3531 /* If there are templates that we've put off instantiating, do
3533 instantiate_pending_templates ();
3535 /* Write out virtual tables as required. Note that writing out
3536 the virtual table for a template class may cause the
3537 instantiation of members of that class. */
3538 if (walk_globals (vtable_decl_p
,
3539 finish_vtable_vardecl
,
3543 /* Write out needed type info variables. Writing out one variable
3544 might cause others to be needed. */
3545 if (walk_globals (tinfo_decl_p
, emit_tinfo_decl
, /*data=*/0))
3548 /* The list of objects with static storage duration is built up
3549 in reverse order. We clear STATIC_AGGREGATES so that any new
3550 aggregates added during the initialization of these will be
3551 initialized in the correct order when we next come around the
3553 vars
= prune_vars_needing_no_initialization (static_aggregates
);
3554 static_aggregates
= NULL_TREE
;
3560 /* We need to start a new initialization function each time
3561 through the loop. That's because we need to know which
3562 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3563 isn't computed until a function is finished, and written
3564 out. That's a deficiency in the back-end. When this is
3565 fixed, these initialization functions could all become
3566 inline, with resulting performance improvements. */
3567 tree ssdf_body
= start_static_storage_duration_function ();
3569 /* Make sure the back end knows about all the variables. */
3570 write_out_vars (vars
);
3572 /* First generate code to do all the initializations. */
3573 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3574 do_static_initialization (TREE_VALUE (v
),
3577 /* Then, generate code to do all the destructions. Do these
3578 in reverse order so that the most recently constructed
3579 variable is the first destroyed. If we're using
3580 __cxa_atexit, then we don't need to do this; functions
3581 were registered at initialization time to destroy the
3583 if (!flag_use_cxa_atexit
)
3585 vars
= nreverse (vars
);
3586 for (v
= vars
; v
; v
= TREE_CHAIN (v
))
3587 do_static_destruction (TREE_VALUE (v
));
3592 /* Finish up the static storage duration function for this
3594 finish_static_storage_duration_function (ssdf_body
);
3596 /* All those initializations and finalizations might cause
3597 us to need more inline functions, more template
3598 instantiations, etc. */
3602 /* Go through the various inline functions, and see if any need
3604 for (i
= 0; i
< deferred_fns_used
; ++i
)
3606 tree decl
= VARRAY_TREE (deferred_fns
, i
);
3607 import_export_decl (decl
);
3608 if (DECL_ARTIFICIAL (decl
) && ! DECL_INITIAL (decl
)
3610 && (! DECL_REALLY_EXTERN (decl
) || DECL_INLINE (decl
)))
3612 /* Even though we're already at the top-level, we push
3613 there again. That way, when we pop back a few lines
3614 hence, all of our state is restored. Otherwise,
3615 finish_function doesn't clean things up, and we end
3616 up with CURRENT_FUNCTION_DECL set. */
3617 push_to_top_level ();
3618 synthesize_method (decl
);
3619 pop_from_top_level ();
3624 /* Mark all functions that might deal with exception-handling as
3626 mark_all_runtime_matches ();
3628 /* We lie to the back-end, pretending that some functions are
3629 not defined when they really are. This keeps these functions
3630 from being put out unnecessarily. But, we must stop lying
3631 when the functions are referenced, or if they are not comdat
3632 since they need to be put out now. */
3633 for (i
= 0; i
< deferred_fns_used
; ++i
)
3635 tree decl
= VARRAY_TREE (deferred_fns
, i
);
3637 if (DECL_NOT_REALLY_EXTERN (decl
)
3638 && DECL_INITIAL (decl
)
3639 && DECL_NEEDED_P (decl
))
3640 DECL_EXTERNAL (decl
) = 0;
3642 /* If we're going to need to write this function out, and
3643 there's already a body for it, create RTL for it now.
3644 (There might be no body if this is a method we haven't
3645 gotten around to synthesizing yet.) */
3646 if (!DECL_EXTERNAL (decl
)
3647 && DECL_NEEDED_P (decl
)
3648 && DECL_SAVED_TREE (decl
)
3649 && !DECL_SAVED_INSNS (decl
)
3650 && !TREE_ASM_WRITTEN (decl
))
3652 int saved_not_really_extern
;
3654 /* When we call finish_function in expand_body, it will
3655 try to reset DECL_NOT_REALLY_EXTERN so we save and
3657 saved_not_really_extern
= DECL_NOT_REALLY_EXTERN (decl
);
3658 /* Generate RTL for this function now that we know we
3661 /* Undo the damage done by finish_function. */
3662 DECL_EXTERNAL (decl
) = 0;
3663 DECL_NOT_REALLY_EXTERN (decl
) = saved_not_really_extern
;
3664 /* If we're compiling -fsyntax-only pretend that this
3665 function has been written out so that we don't try to
3667 if (flag_syntax_only
)
3668 TREE_ASM_WRITTEN (decl
) = 1;
3673 if (deferred_fns_used
3674 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns
, 0),
3677 if (walk_namespaces (wrapup_globals_for_namespace
, /*data=*/0))
3680 /* Static data members are just like namespace-scope globals. */
3681 for (i
= 0; i
< pending_statics_used
; ++i
)
3683 tree decl
= VARRAY_TREE (pending_statics
, i
);
3684 if (TREE_ASM_WRITTEN (decl
))
3686 import_export_decl (decl
);
3687 if (DECL_NOT_REALLY_EXTERN (decl
) && ! DECL_IN_AGGR_P (decl
))
3688 DECL_EXTERNAL (decl
) = 0;
3691 && wrapup_global_declarations (&VARRAY_TREE (pending_statics
, 0),
3692 pending_statics_used
))
3697 /* We give C linkage to static constructors and destructors. */
3698 push_lang_context (lang_name_c
);
3700 /* Generate initialization and destruction functions for all
3701 priorities for which they are required. */
3702 if (priority_info_map
)
3703 splay_tree_foreach (priority_info_map
,
3704 generate_ctor_and_dtor_functions_for_priority
,
3707 /* We're done with the splay-tree now. */
3708 if (priority_info_map
)
3709 splay_tree_delete (priority_info_map
);
3711 /* We're done with static constructors, so we can go back to "C++"
3713 pop_lang_context ();
3715 /* Now delete from the chain of variables all virtual function tables.
3716 We output them all ourselves, because each will be treated
3717 specially. We don't do this if we're just doing semantic
3718 analysis, and not code-generation. */
3719 if (!flag_syntax_only
)
3720 walk_globals (vtable_decl_p
, prune_vtable_vardecl
, /*data=*/0);
3722 /* Now, issue warnings about static, but not defined, functions,
3723 etc., and emit debugging information. */
3724 walk_namespaces (wrapup_globals_for_namespace
, /*data=*/&reconsider
);
3725 if (pending_statics
)
3726 check_global_declarations (&VARRAY_TREE (pending_statics
, 0),
3727 pending_statics_used
);
3731 /* The entire file is now complete. If requested, dump everything
3733 if (flag_dump_translation_unit
)
3734 dump_node_to_file (global_namespace
, flag_dump_translation_unit
);
3736 /* If there's some tool that wants to examine the entire translation
3737 unit, let it do so now. */
3739 (*back_end_hook
) (global_namespace
);
3741 timevar_pop (TV_VARCONST
);
3743 if (flag_detailed_statistics
)
3745 dump_tree_statistics ();
3746 dump_time_statistics ();
3750 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3751 expr. Since it was parsed like a type, we need to wade through and fix
3752 that. Unfortunately, since operator() is left-associative, we can't use
3753 tail recursion. In the above example, TYPE is `A', and DECL is
3756 Maybe this shouldn't be recursive, but how often will it actually be
3760 reparse_absdcl_as_expr (type
, decl
)
3763 /* do build_functional_cast (type, NULL_TREE) at bottom */
3764 if (TREE_OPERAND (decl
, 0) == NULL_TREE
)
3765 return build_functional_cast (type
, NULL_TREE
);
3768 decl
= reparse_absdcl_as_expr (type
, TREE_OPERAND (decl
, 0));
3770 decl
= build_x_function_call (decl
, NULL_TREE
, current_class_ref
);
3772 if (TREE_CODE (decl
) == CALL_EXPR
3773 && (! TREE_TYPE (decl
)
3774 || TREE_CODE (TREE_TYPE (decl
)) != VOID_TYPE
))
3775 decl
= require_complete_type (decl
);
3780 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3781 out to be an expr. Since it was parsed like a type, we need to wade
3782 through and fix that. Since casts are right-associative, we are
3783 reversing the order, so we don't have to recurse.
3785 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3789 reparse_absdcl_as_casts (decl
, expr
)
3794 if (TREE_CODE (expr
) == CONSTRUCTOR
3795 && TREE_TYPE (expr
) == 0)
3797 type
= groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl
)));
3798 decl
= TREE_OPERAND (decl
, 0);
3800 expr
= digest_init (type
, expr
, (tree
*) 0);
3801 if (TREE_CODE (type
) == ARRAY_TYPE
&& !COMPLETE_TYPE_P (type
))
3803 int failure
= complete_array_type (type
, expr
, 1);
3805 my_friendly_abort (78);
3811 type
= groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl
)));
3812 decl
= TREE_OPERAND (decl
, 0);
3813 expr
= build_c_cast (type
, expr
);
3816 if (warn_old_style_cast
&& ! in_system_header
3817 && current_lang_name
!= lang_name_c
)
3818 warning ("use of old-style cast");
3823 /* Given plain tree nodes for an expression, build up the full semantics. */
3826 build_expr_from_tree (t
)
3829 if (t
== NULL_TREE
|| t
== error_mark_node
)
3832 switch (TREE_CODE (t
))
3834 case IDENTIFIER_NODE
:
3835 return do_identifier (t
, 0, NULL_TREE
);
3838 if (LOOKUP_EXPR_GLOBAL (t
))
3839 return do_scoped_id (TREE_OPERAND (t
, 0), 0);
3841 return do_identifier (TREE_OPERAND (t
, 0), 0, NULL_TREE
);
3843 case TEMPLATE_ID_EXPR
:
3844 return (lookup_template_function
3845 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3846 build_expr_from_tree (TREE_OPERAND (t
, 1))));
3849 return build_x_indirect_ref
3850 (build_expr_from_tree (TREE_OPERAND (t
, 0)), "unary *");
3853 return build_functional_cast
3854 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3856 case REINTERPRET_CAST_EXPR
:
3857 return build_reinterpret_cast
3858 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3860 case CONST_CAST_EXPR
:
3861 return build_const_cast
3862 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3864 case DYNAMIC_CAST_EXPR
:
3865 return build_dynamic_cast
3866 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3868 case STATIC_CAST_EXPR
:
3869 return build_static_cast
3870 (TREE_TYPE (t
), build_expr_from_tree (TREE_OPERAND (t
, 0)));
3872 case PREDECREMENT_EXPR
:
3873 case PREINCREMENT_EXPR
:
3874 case POSTDECREMENT_EXPR
:
3875 case POSTINCREMENT_EXPR
:
3879 case TRUTH_NOT_EXPR
:
3881 case CONVERT_EXPR
: /* Unary + */
3886 return build_x_unary_op (TREE_CODE (t
),
3887 build_expr_from_tree (TREE_OPERAND (t
, 0)));
3892 case TRUNC_DIV_EXPR
:
3894 case FLOOR_DIV_EXPR
:
3895 case ROUND_DIV_EXPR
:
3896 case EXACT_DIV_EXPR
:
3898 case BIT_ANDTC_EXPR
:
3901 case TRUNC_MOD_EXPR
:
3902 case FLOOR_MOD_EXPR
:
3903 case TRUTH_ANDIF_EXPR
:
3904 case TRUTH_ORIF_EXPR
:
3905 case TRUTH_AND_EXPR
:
3920 return build_x_binary_op
3922 build_expr_from_tree (TREE_OPERAND (t
, 0)),
3923 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3926 return build_m_component_ref
3927 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3928 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3931 return build_offset_ref (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1));
3934 if (TREE_OPERAND (t
, 0) == NULL_TREE
)
3936 return build_nt (ARRAY_REF
, NULL_TREE
,
3937 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3938 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3939 build_expr_from_tree (TREE_OPERAND (t
, 1)));
3944 tree r
= build_expr_from_tree (TREE_OPERAND (t
, 0));
3947 return TREE_CODE (t
) == SIZEOF_EXPR
? c_sizeof (r
) : c_alignof (r
);
3951 return build_x_modify_expr
3952 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3953 TREE_CODE (TREE_OPERAND (t
, 1)),
3954 build_expr_from_tree (TREE_OPERAND (t
, 2)));
3957 return build_x_arrow
3958 (build_expr_from_tree (TREE_OPERAND (t
, 0)));
3962 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3963 build_expr_from_tree (TREE_OPERAND (t
, 1)),
3964 build_expr_from_tree (TREE_OPERAND (t
, 2)),
3965 NEW_EXPR_USE_GLOBAL (t
));
3968 return delete_sanity
3969 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
3970 build_expr_from_tree (TREE_OPERAND (t
, 1)),
3971 DELETE_EXPR_USE_VEC (t
), DELETE_EXPR_USE_GLOBAL (t
));
3974 if (TREE_OPERAND (t
, 1) == NULL_TREE
)
3975 return build_x_compound_expr
3976 (build_expr_from_tree (TREE_OPERAND (t
, 0)));
3978 my_friendly_abort (42);
3980 case METHOD_CALL_EXPR
:
3981 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SCOPE_REF
)
3983 tree ref
= TREE_OPERAND (t
, 0);
3984 tree name
= TREE_OPERAND (ref
, 1);
3986 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
3987 name
= build_nt (TEMPLATE_ID_EXPR
,
3988 TREE_OPERAND (name
, 0),
3989 build_expr_from_tree (TREE_OPERAND (name
, 1)));
3991 return build_scoped_method_call
3992 (build_expr_from_tree (TREE_OPERAND (t
, 1)),
3993 build_expr_from_tree (TREE_OPERAND (ref
, 0)),
3995 build_expr_from_tree (TREE_OPERAND (t
, 2)));
3999 tree fn
= TREE_OPERAND (t
, 0);
4001 /* We can get a TEMPLATE_ID_EXPR here on code like:
4005 so we must resolve that. However, we can also get things
4006 like a BIT_NOT_EXPR here, when referring to a destructor,
4007 and things like that are not correctly resolved by
4008 build_expr_from_tree. So, just use build_expr_from_tree
4009 when we really need it. */
4010 if (TREE_CODE (fn
) == TEMPLATE_ID_EXPR
)
4011 fn
= lookup_template_function
4012 (TREE_OPERAND (fn
, 0),
4013 build_expr_from_tree (TREE_OPERAND (fn
, 1)));
4015 return build_method_call
4016 (build_expr_from_tree (TREE_OPERAND (t
, 1)),
4018 build_expr_from_tree (TREE_OPERAND (t
, 2)),
4019 NULL_TREE
, LOOKUP_NORMAL
);
4023 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SCOPE_REF
)
4025 tree ref
= TREE_OPERAND (t
, 0);
4026 tree name
= TREE_OPERAND (ref
, 1);
4028 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
4029 name
= build_nt (TEMPLATE_ID_EXPR
,
4030 TREE_OPERAND (name
, 0),
4031 build_expr_from_tree (TREE_OPERAND (name
, 1)));
4033 return build_member_call
4034 (build_expr_from_tree (TREE_OPERAND (ref
, 0)),
4036 build_expr_from_tree (TREE_OPERAND (t
, 1)));
4040 tree name
= TREE_OPERAND (t
, 0);
4042 tree args
= build_expr_from_tree (TREE_OPERAND (t
, 1));
4043 if (args
!= NULL_TREE
&& TREE_CODE (name
) == LOOKUP_EXPR
4044 && !LOOKUP_EXPR_GLOBAL (name
)
4045 && TREE_CODE ((id
= TREE_OPERAND (name
, 0))) == IDENTIFIER_NODE
4046 && (!current_class_type
4047 || !lookup_member (current_class_type
, id
, 0, 0)))
4049 /* Do Koenig lookup if there are no class members. */
4050 name
= do_identifier (id
, 0, args
);
4052 else if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
4053 || ! really_overloaded_fn (name
))
4054 name
= build_expr_from_tree (name
);
4055 return build_x_function_call (name
, args
, current_class_ref
);
4059 return build_x_conditional_expr
4060 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4061 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4062 build_expr_from_tree (TREE_OPERAND (t
, 2)));
4064 case PSEUDO_DTOR_EXPR
:
4065 return (finish_pseudo_destructor_call_expr
4066 (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4067 build_expr_from_tree (TREE_OPERAND (t
, 1)),
4068 build_expr_from_tree (TREE_OPERAND (t
, 2))));
4072 tree purpose
, value
, chain
;
4074 if (t
== void_list_node
)
4077 purpose
= TREE_PURPOSE (t
);
4079 purpose
= build_expr_from_tree (purpose
);
4080 value
= TREE_VALUE (t
);
4082 value
= build_expr_from_tree (value
);
4083 chain
= TREE_CHAIN (t
);
4084 if (chain
&& chain
!= void_type_node
)
4085 chain
= build_expr_from_tree (chain
);
4086 return tree_cons (purpose
, value
, chain
);
4091 tree object
= build_expr_from_tree (TREE_OPERAND (t
, 0));
4092 tree field
= TREE_OPERAND (t
, 1);
4094 /* We use a COMPONENT_REF to indicate things of the form `x.b'
4095 and `x.A::b'. We must distinguish between those cases
4097 if (TREE_CODE (field
) == SCOPE_REF
)
4098 return build_object_ref (object
,
4099 TREE_OPERAND (field
, 0),
4100 TREE_OPERAND (field
, 1));
4102 return build_x_component_ref (object
, field
,
4107 return build_throw (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4113 /* digest_init will do the wrong thing if we let it. */
4114 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
4117 r
= build_nt (CONSTRUCTOR
, NULL_TREE
,
4118 build_expr_from_tree (CONSTRUCTOR_ELTS (t
)));
4119 TREE_HAS_CONSTRUCTOR (r
) = TREE_HAS_CONSTRUCTOR (t
);
4122 return digest_init (TREE_TYPE (t
), r
, 0);
4127 if (TYPE_P (TREE_OPERAND (t
, 0)))
4128 return get_typeid (TREE_OPERAND (t
, 0));
4129 return build_typeid (build_expr_from_tree (TREE_OPERAND (t
, 0)));
4132 return convert_from_reference (t
);
4135 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t
, 0)),
4143 /* This is something of the form `int (*a)++' that has turned out to be an
4144 expr. It was only converted into parse nodes, so we need to go through
4145 and build up the semantics. Most of the work is done by
4146 build_expr_from_tree, above.
4148 In the above example, TYPE is `int' and DECL is `*a'. */
4151 reparse_decl_as_expr (type
, decl
)
4154 decl
= build_expr_from_tree (decl
);
4156 return build_functional_cast (type
, build_tree_list (NULL_TREE
, decl
));
4161 /* This is something of the form `int (*a)' that has turned out to be a
4162 decl. It was only converted into parse nodes, so we need to do the
4163 checking that make_{pointer,reference}_declarator do. */
4166 finish_decl_parsing (decl
)
4169 extern int current_class_depth
;
4171 switch (TREE_CODE (decl
))
4173 case IDENTIFIER_NODE
:
4176 return make_pointer_declarator
4177 (NULL_TREE
, finish_decl_parsing (TREE_OPERAND (decl
, 0)));
4179 return make_reference_declarator
4180 (NULL_TREE
, finish_decl_parsing (TREE_OPERAND (decl
, 0)));
4182 TREE_OPERAND (decl
, 0) = finish_decl_parsing (TREE_OPERAND (decl
, 0));
4185 push_nested_class (TREE_TYPE (TREE_OPERAND (decl
, 0)), 3);
4186 TREE_COMPLEXITY (decl
) = current_class_depth
;
4189 TREE_OPERAND (decl
, 0) = finish_decl_parsing (TREE_OPERAND (decl
, 0));
4192 /* For attribute handling. */
4193 TREE_VALUE (decl
) = finish_decl_parsing (TREE_VALUE (decl
));
4195 case TEMPLATE_ID_EXPR
:
4198 my_friendly_abort (5);
4203 /* Return 1 if root encloses child. */
4206 is_namespace_ancestor (root
, child
)
4211 if (root
== global_namespace
)
4213 if (child
== global_namespace
)
4215 return is_namespace_ancestor (root
, CP_DECL_CONTEXT (child
));
4219 /* Return the namespace that is the common ancestor
4220 of two given namespaces. */
4223 namespace_ancestor (ns1
, ns2
)
4226 if (is_namespace_ancestor (ns1
, ns2
))
4228 return namespace_ancestor (CP_DECL_CONTEXT (ns1
), ns2
);
4231 /* Insert used into the using list of user. Set indirect_flag if this
4232 directive is not directly from the source. Also find the common
4233 ancestor and let our users know about the new namespace */
4235 add_using_namespace (user
, used
, indirect
)
4241 /* Using oneself is a no-op. */
4244 my_friendly_assert (TREE_CODE (user
) == NAMESPACE_DECL
, 380);
4245 my_friendly_assert (TREE_CODE (used
) == NAMESPACE_DECL
, 380);
4246 /* Check if we already have this. */
4247 t
= purpose_member (used
, DECL_NAMESPACE_USING (user
));
4251 /* Promote to direct usage. */
4252 TREE_INDIRECT_USING (t
) = 0;
4256 /* Add used to the user's using list. */
4257 DECL_NAMESPACE_USING (user
)
4258 = tree_cons (used
, namespace_ancestor (user
, used
),
4259 DECL_NAMESPACE_USING (user
));
4261 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user
)) = indirect
;
4263 /* Add user to the used's users list. */
4264 DECL_NAMESPACE_USERS (used
)
4265 = tree_cons (user
, 0, DECL_NAMESPACE_USERS (used
));
4267 /* Recursively add all namespaces used. */
4268 for (t
= DECL_NAMESPACE_USING (used
); t
; t
= TREE_CHAIN (t
))
4269 /* indirect usage */
4270 add_using_namespace (user
, TREE_PURPOSE (t
), 1);
4272 /* Tell everyone using us about the new used namespaces. */
4273 for (t
= DECL_NAMESPACE_USERS (user
); t
; t
= TREE_CHAIN (t
))
4274 add_using_namespace (TREE_PURPOSE (t
), used
, 1);
4277 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4278 duplicates. The first list becomes the tail of the result.
4280 The algorithm is O(n^2). We could get this down to O(n log n) by
4281 doing a sort on the addresses of the functions, if that becomes
4285 merge_functions (s1
, s2
)
4289 for (; s2
; s2
= OVL_NEXT (s2
))
4291 tree fn
= OVL_CURRENT (s2
);
4292 if (! ovl_member (fn
, s1
))
4293 s1
= build_overload (fn
, s1
);
4298 /* This should return an error not all definitions define functions.
4299 It is not an error if we find two functions with exactly the
4300 same signature, only if these are selected in overload resolution.
4301 old is the current set of bindings, new the freshly-found binding.
4302 XXX Do we want to give *all* candidates in case of ambiguity?
4303 XXX In what way should I treat extern declarations?
4304 XXX I don't want to repeat the entire duplicate_decls here */
4307 ambiguous_decl (name
, old
, new, flags
)
4314 my_friendly_assert (old
!= NULL_TREE
, 393);
4315 /* Copy the value. */
4316 val
= BINDING_VALUE (new);
4318 switch (TREE_CODE (val
))
4321 /* If we expect types or namespaces, and not templates,
4322 or this is not a template class. */
4323 if (LOOKUP_QUALIFIERS_ONLY (flags
)
4324 && !DECL_CLASS_TEMPLATE_P (val
))
4328 if (LOOKUP_NAMESPACES_ONLY (flags
))
4331 case NAMESPACE_DECL
:
4332 if (LOOKUP_TYPES_ONLY (flags
))
4336 if (LOOKUP_QUALIFIERS_ONLY (flags
))
4340 if (!BINDING_VALUE (old
))
4341 BINDING_VALUE (old
) = val
;
4342 else if (val
&& val
!= BINDING_VALUE (old
))
4344 if (is_overloaded_fn (BINDING_VALUE (old
))
4345 && is_overloaded_fn (val
))
4347 BINDING_VALUE (old
) = merge_functions (BINDING_VALUE (old
),
4352 /* Some declarations are functions, some are not. */
4353 if (flags
& LOOKUP_COMPLAIN
)
4355 /* If we've already given this error for this lookup,
4356 BINDING_VALUE (old) is error_mark_node, so let's not
4357 repeat ourselves. */
4358 if (BINDING_VALUE (old
) != error_mark_node
)
4360 cp_error ("use of `%D' is ambiguous", name
);
4361 cp_error_at (" first declared as `%#D' here",
4362 BINDING_VALUE (old
));
4364 cp_error_at (" also declared as `%#D' here", val
);
4366 BINDING_VALUE (old
) = error_mark_node
;
4369 /* ... and copy the type. */
4370 type
= BINDING_TYPE (new);
4371 if (LOOKUP_NAMESPACES_ONLY (flags
))
4373 if (!BINDING_TYPE (old
))
4374 BINDING_TYPE (old
) = type
;
4375 else if (type
&& BINDING_TYPE (old
) != type
)
4377 if (flags
& LOOKUP_COMPLAIN
)
4379 cp_error ("`%D' denotes an ambiguous type",name
);
4380 cp_error_at (" first type here", BINDING_TYPE (old
));
4381 cp_error_at (" other type here", type
);
4387 /* Subroutine of unualified_namespace_lookup:
4388 Add the bindings of NAME in used namespaces to VAL.
4389 We are currently looking for names in namespace SCOPE, so we
4390 look through USINGS for using-directives of namespaces
4391 which have SCOPE as a common ancestor with the current scope.
4392 Returns zero on errors. */
4395 lookup_using_namespace (name
, val
, usings
, scope
, flags
, spacesp
)
4396 tree name
, val
, usings
, scope
;
4402 /* Iterate over all used namespaces in current, searching for using
4403 directives of scope. */
4404 for (iter
= usings
; iter
; iter
= TREE_CHAIN (iter
))
4405 if (TREE_VALUE (iter
) == scope
)
4408 *spacesp
= tree_cons (TREE_PURPOSE (iter
), NULL_TREE
,
4410 val1
= binding_for_name (name
, TREE_PURPOSE (iter
));
4411 /* Resolve ambiguities. */
4412 val
= ambiguous_decl (name
, val
, val1
, flags
);
4414 return BINDING_VALUE (val
) != error_mark_node
;
4418 Accepts the NAME to lookup and its qualifying SCOPE.
4419 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4423 qualified_lookup_using_namespace (name
, scope
, result
, flags
)
4429 /* Maintain a list of namespaces visited... */
4430 tree seen
= NULL_TREE
;
4431 /* ... and a list of namespace yet to see. */
4432 tree todo
= NULL_TREE
;
4434 /* Look through namespace aliases. */
4435 scope
= ORIGINAL_NAMESPACE (scope
);
4436 while (scope
&& (result
!= error_mark_node
))
4438 seen
= tree_cons (scope
, NULL_TREE
, seen
);
4439 result
= ambiguous_decl (name
, result
,
4440 binding_for_name (name
, scope
), flags
);
4441 if (!BINDING_VALUE (result
) && !BINDING_TYPE (result
))
4442 /* Consider using directives. */
4443 for (usings
= DECL_NAMESPACE_USING (scope
); usings
;
4444 usings
= TREE_CHAIN (usings
))
4445 /* If this was a real directive, and we have not seen it. */
4446 if (!TREE_INDIRECT_USING (usings
)
4447 && !purpose_member (TREE_PURPOSE (usings
), seen
))
4448 todo
= tree_cons (TREE_PURPOSE (usings
), NULL_TREE
, todo
);
4451 scope
= TREE_PURPOSE (todo
);
4452 todo
= TREE_CHAIN (todo
);
4455 scope
= NULL_TREE
; /* If there never was a todo list. */
4457 return result
!= error_mark_node
;
4460 /* [namespace.memdef]/2 */
4462 /* Set the context of a declaration to scope. Complain if we are not
4466 set_decl_namespace (decl
, scope
, friendp
)
4472 if (scope
== fake_std_node
)
4473 scope
= global_namespace
;
4474 /* Get rid of namespace aliases. */
4475 scope
= ORIGINAL_NAMESPACE (scope
);
4477 /* It is ok for friends to be qualified in parallel space. */
4478 if (!friendp
&& !is_namespace_ancestor (current_namespace
, scope
))
4479 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4481 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4482 if (scope
!= current_namespace
)
4484 /* See whether this has been declared in the namespace. */
4485 old
= namespace_binding (DECL_NAME (decl
), scope
);
4487 /* No old declaration at all. */
4489 /* A template can be explicitly specialized in any namespace. */
4490 if (processing_explicit_instantiation
)
4492 if (!is_overloaded_fn (decl
))
4493 /* Don't compare non-function decls with decls_match here,
4494 since it can't check for the correct constness at this
4495 point. pushdecl will find those errors later. */
4497 /* Since decl is a function, old should contain a function decl. */
4498 if (!is_overloaded_fn (old
))
4500 if (processing_template_decl
|| processing_specialization
)
4501 /* We have not yet called push_template_decl to turn the
4502 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4503 won't match. But, we'll check later, when we construct the
4506 for (; old
; old
= OVL_NEXT (old
))
4507 if (decls_match (decl
, OVL_CURRENT (old
)))
4513 cp_error ("`%D' should have been declared inside `%D'",
4517 /* Compute the namespace where a declaration is defined. */
4520 decl_namespace (decl
)
4524 decl
= TYPE_STUB_DECL (decl
);
4525 while (DECL_CONTEXT (decl
))
4527 decl
= DECL_CONTEXT (decl
);
4528 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
4531 decl
= TYPE_STUB_DECL (decl
);
4532 my_friendly_assert (DECL_P (decl
), 390);
4535 return global_namespace
;
4538 /* Return the namespace where the current declaration is declared. */
4541 current_decl_namespace ()
4544 /* If we have been pushed into a different namespace, use it. */
4545 if (decl_namespace_list
)
4546 return TREE_PURPOSE (decl_namespace_list
);
4548 if (current_class_type
)
4549 result
= decl_namespace (TYPE_STUB_DECL (current_class_type
));
4550 else if (current_function_decl
)
4551 result
= decl_namespace (current_function_decl
);
4553 result
= current_namespace
;
4557 /* Temporarily set the namespace for the current declaration. */
4560 push_decl_namespace (decl
)
4563 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
4564 decl
= decl_namespace (decl
);
4565 decl_namespace_list
= tree_cons (ORIGINAL_NAMESPACE (decl
),
4566 NULL_TREE
, decl_namespace_list
);
4570 pop_decl_namespace ()
4572 decl_namespace_list
= TREE_CHAIN (decl_namespace_list
);
4575 /* Enter a class or namespace scope. */
4581 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4582 push_decl_namespace (t
);
4587 /* Leave scope pushed by push_scope. */
4593 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4594 pop_decl_namespace ();
4599 /* [basic.lookup.koenig] */
4600 /* A non-zero return value in the functions below indicates an error.
4601 All nodes allocated in the procedure are on the scratch obstack. */
4611 static int arg_assoc
PARAMS ((struct arg_lookup
*, tree
));
4612 static int arg_assoc_args
PARAMS ((struct arg_lookup
*, tree
));
4613 static int arg_assoc_type
PARAMS ((struct arg_lookup
*, tree
));
4614 static int add_function
PARAMS ((struct arg_lookup
*, tree
));
4615 static int arg_assoc_namespace
PARAMS ((struct arg_lookup
*, tree
));
4616 static int arg_assoc_class
PARAMS ((struct arg_lookup
*, tree
));
4617 static int arg_assoc_template_arg
PARAMS ((struct arg_lookup
*, tree
));
4619 /* Add a function to the lookup structure.
4620 Returns 1 on error. */
4623 add_function (k
, fn
)
4624 struct arg_lookup
*k
;
4627 /* We used to check here to see if the function was already in the list,
4628 but that's O(n^2), which is just too expensive for function lookup.
4629 Now we deal with the occasional duplicate in joust. In doing this, we
4630 assume that the number of duplicates will be small compared to the
4631 total number of functions being compared, which should usually be the
4634 /* We must find only functions, or exactly one non-function. */
4637 else if (is_overloaded_fn (k
->functions
) && is_overloaded_fn (fn
))
4638 k
->functions
= build_overload (fn
, k
->functions
);
4641 tree f1
= OVL_CURRENT (k
->functions
);
4643 if (is_overloaded_fn (f1
))
4645 fn
= f1
; f1
= f2
; f2
= fn
;
4647 cp_error_at ("`%D' is not a function,", f1
);
4648 cp_error_at (" conflict with `%D'", f2
);
4649 cp_error (" in call to `%D'", k
->name
);
4656 /* Add functions of a namespace to the lookup structure.
4657 Returns 1 on error. */
4660 arg_assoc_namespace (k
, scope
)
4661 struct arg_lookup
*k
;
4666 if (purpose_member (scope
, k
->namespaces
))
4668 k
->namespaces
= tree_cons (scope
, NULL_TREE
, k
->namespaces
);
4670 value
= namespace_binding (k
->name
, scope
);
4674 for (; value
; value
= OVL_NEXT (value
))
4675 if (add_function (k
, OVL_CURRENT (value
)))
4681 /* Adds everything associated with a template argument to the lookup
4682 structure. Returns 1 on error. */
4685 arg_assoc_template_arg (k
, arg
)
4686 struct arg_lookup
* k
;
4689 /* [basic.lookup.koenig]
4691 If T is a template-id, its associated namespaces and classes are
4692 ... the namespaces and classes associated with the types of the
4693 template arguments provided for template type parameters
4694 (excluding template template parameters); the namespaces in which
4695 any template template arguments are defined; and the classes in
4696 which any member templates used as template template arguments
4697 are defined. [Note: non-type template arguments do not
4698 contribute to the set of associated namespaces. ] */
4700 /* Consider first template template arguments. */
4701 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
4703 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
4705 tree ctx
= CP_DECL_CONTEXT (arg
);
4707 /* It's not a member template. */
4708 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4709 return arg_assoc_namespace (k
, ctx
);
4710 /* Otherwise, it must be member template. */
4712 return arg_assoc_class (k
, ctx
);
4714 /* It's not a template template argument, but it is a type template
4716 else if (TYPE_P (arg
))
4717 return arg_assoc_type (k
, arg
);
4718 /* It's a non-type template argument. */
4723 /* Adds everything associated with class to the lookup structure.
4724 Returns 1 on error. */
4727 arg_assoc_class (k
, type
)
4728 struct arg_lookup
* k
;
4731 tree list
, friends
, context
;
4734 /* Backend build structures, such as __builtin_va_list, aren't
4735 affected by all this. */
4736 if (!CLASS_TYPE_P (type
))
4739 if (purpose_member (type
, k
->classes
))
4741 k
->classes
= tree_cons (type
, NULL_TREE
, k
->classes
);
4743 context
= decl_namespace (TYPE_MAIN_DECL (type
));
4744 if (arg_assoc_namespace (k
, context
))
4747 /* Process baseclasses. */
4748 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); i
++)
4749 if (arg_assoc_class (k
, TYPE_BINFO_BASETYPE (type
, i
)))
4752 /* Process friends. */
4753 for (list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
4754 list
= TREE_CHAIN (list
))
4755 if (k
->name
== TREE_PURPOSE (list
))
4756 for (friends
= TREE_VALUE (list
); friends
;
4757 friends
= TREE_CHAIN (friends
))
4758 /* Only interested in global functions with potentially hidden
4759 (i.e. unqualified) declarations. */
4760 if (TREE_PURPOSE (friends
) == error_mark_node
&& TREE_VALUE (friends
)
4761 && decl_namespace (TREE_VALUE (friends
)) == context
)
4762 if (add_function (k
, TREE_VALUE (friends
)))
4765 /* Process template arguments. */
4766 if (CLASSTYPE_TEMPLATE_INFO (type
))
4768 list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
4769 for (i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
4770 arg_assoc_template_arg (k
, TREE_VEC_ELT (list
, i
));
4776 /* Adds everything associated with a given type.
4777 Returns 1 on error. */
4780 arg_assoc_type (k
, type
)
4781 struct arg_lookup
*k
;
4784 switch (TREE_CODE (type
))
4795 if (TYPE_PTRMEMFUNC_P (type
))
4796 return arg_assoc_type (k
, TYPE_PTRMEMFUNC_FN_TYPE (type
));
4797 return arg_assoc_class (k
, type
);
4799 case REFERENCE_TYPE
:
4801 return arg_assoc_type (k
, TREE_TYPE (type
));
4804 return arg_assoc_namespace (k
, decl_namespace (TYPE_MAIN_DECL (type
)));
4806 /* Pointer to member: associate class type and value type. */
4807 if (arg_assoc_type (k
, TYPE_OFFSET_BASETYPE (type
)))
4809 return arg_assoc_type (k
, TREE_TYPE (type
));
4811 /* The basetype is referenced in the first arg type, so just
4814 /* Associate the parameter types. */
4815 if (arg_assoc_args (k
, TYPE_ARG_TYPES (type
)))
4817 /* Associate the return type. */
4818 return arg_assoc_type (k
, TREE_TYPE (type
));
4819 case TEMPLATE_TYPE_PARM
:
4820 case BOUND_TEMPLATE_TEMPLATE_PARM
:
4825 if (type
== unknown_type_node
)
4827 /* else fall through */
4829 my_friendly_abort (390);
4834 /* Adds everything associated with arguments. Returns 1 on error. */
4837 arg_assoc_args (k
, args
)
4838 struct arg_lookup
* k
;
4841 for (; args
; args
= TREE_CHAIN (args
))
4842 if (arg_assoc (k
, TREE_VALUE (args
)))
4847 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4851 struct arg_lookup
* k
;
4854 if (n
== error_mark_node
)
4858 return arg_assoc_type (k
, n
);
4860 if (! type_unknown_p (n
))
4861 return arg_assoc_type (k
, TREE_TYPE (n
));
4863 if (TREE_CODE (n
) == ADDR_EXPR
)
4864 n
= TREE_OPERAND (n
, 0);
4865 if (TREE_CODE (n
) == COMPONENT_REF
)
4866 n
= TREE_OPERAND (n
, 1);
4867 if (TREE_CODE (n
) == OFFSET_REF
)
4868 n
= TREE_OPERAND (n
, 1);
4869 while (TREE_CODE (n
) == TREE_LIST
)
4872 if (TREE_CODE (n
) == FUNCTION_DECL
)
4873 return arg_assoc_type (k
, TREE_TYPE (n
));
4874 if (TREE_CODE (n
) == TEMPLATE_ID_EXPR
)
4876 /* [basic.lookup.koenig]
4878 If T is a template-id, its associated namespaces and classes
4879 are the namespace in which the template is defined; for
4880 member templates, the member template's class... */
4881 tree
template = TREE_OPERAND (n
, 0);
4882 tree args
= TREE_OPERAND (n
, 1);
4886 if (TREE_CODE (template) == COMPONENT_REF
)
4887 template = TREE_OPERAND (template, 1);
4889 /* First, the template. There may actually be more than one if
4890 this is an overloaded function template. But, in that case,
4891 we only need the first; all the functions will be in the same
4893 template = OVL_CURRENT (template);
4895 ctx
= CP_DECL_CONTEXT (template);
4897 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
4899 if (arg_assoc_namespace (k
, ctx
) == 1)
4902 /* It must be a member template. */
4903 else if (arg_assoc_class (k
, ctx
) == 1)
4906 /* Now the arguments. */
4907 for (arg
= args
; arg
!= NULL_TREE
; arg
= TREE_CHAIN (arg
))
4908 if (arg_assoc_template_arg (k
, TREE_VALUE (arg
)) == 1)
4913 my_friendly_assert (TREE_CODE (n
) == OVERLOAD
, 980715);
4915 for (; n
; n
= OVL_CHAIN (n
))
4916 if (arg_assoc_type (k
, TREE_TYPE (OVL_FUNCTION (n
))))
4923 /* Performs Koenig lookup depending on arguments, where fns
4924 are the functions found in normal lookup. */
4927 lookup_arg_dependent (name
, fns
, args
)
4932 struct arg_lookup k
;
4933 tree fn
= NULL_TREE
;
4937 k
.classes
= NULL_TREE
;
4939 /* Note that we've already looked at some namespaces during normal
4940 unqualified lookup, unless we found a decl in function scope. */
4942 fn
= OVL_CURRENT (fns
);
4943 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_LOCAL_FUNCTION_P (fn
))
4944 k
.namespaces
= NULL_TREE
;
4946 unqualified_namespace_lookup (name
, 0, &k
.namespaces
);
4948 arg_assoc_args (&k
, args
);
4952 /* Process a namespace-alias declaration. */
4955 do_namespace_alias (alias
, namespace)
4956 tree alias
, namespace;
4958 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
4960 /* The parser did not find it, so it's not there. */
4961 cp_error ("unknown namespace `%D'", namespace);
4965 namespace = ORIGINAL_NAMESPACE (namespace);
4967 /* Build the alias. */
4968 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
4969 DECL_NAMESPACE_ALIAS (alias
) = namespace;
4973 /* Check a non-member using-declaration. Return the name and scope
4974 being used, and the USING_DECL, or NULL_TREE on failure. */
4977 validate_nonmember_using_decl (decl
, scope
, name
)
4982 if (TREE_CODE (decl
) == SCOPE_REF
4983 && TREE_OPERAND (decl
, 0) == fake_std_node
)
4985 *scope
= global_namespace
;
4986 *name
= TREE_OPERAND (decl
, 1);
4988 else if (TREE_CODE (decl
) == SCOPE_REF
)
4990 *scope
= TREE_OPERAND (decl
, 0);
4991 *name
= TREE_OPERAND (decl
, 1);
4993 /* [namespace.udecl]
4995 A using-declaration for a class member shall be a
4996 member-declaration. */
4997 if (!processing_template_decl
4998 && TREE_CODE (*scope
) != NAMESPACE_DECL
)
5000 if (TYPE_P (*scope
))
5001 cp_error ("`%T' is not a namespace", *scope
);
5003 cp_error ("`%D' is not a namespace", *scope
);
5007 else if (TREE_CODE (decl
) == IDENTIFIER_NODE
5008 || TREE_CODE (decl
) == TYPE_DECL
5009 || TREE_CODE (decl
) == TEMPLATE_DECL
)
5011 *scope
= global_namespace
;
5015 my_friendly_abort (382);
5017 *name
= DECL_NAME (*name
);
5018 /* Make a USING_DECL. */
5019 return push_using_decl (*scope
, *name
);
5022 /* Process local and global using-declarations. */
5025 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, newval
, newtype
)
5027 tree oldval
, oldtype
;
5028 tree
*newval
, *newtype
;
5032 *newval
= *newtype
= NULL_TREE
;
5033 decls
= make_node (CPLUS_BINDING
);
5034 if (!qualified_lookup_using_namespace (name
, scope
, decls
, 0))
5038 if (!BINDING_VALUE (decls
) && !BINDING_TYPE (decls
))
5040 cp_error ("`%D' not declared", name
);
5044 /* Check for using functions. */
5045 if (BINDING_VALUE (decls
) && is_overloaded_fn (BINDING_VALUE (decls
)))
5049 if (oldval
&& !is_overloaded_fn (oldval
))
5051 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls
)), oldval
);
5056 for (tmp
= BINDING_VALUE (decls
); tmp
; tmp
= OVL_NEXT (tmp
))
5058 tree new_fn
= OVL_CURRENT (tmp
);
5060 /* [namespace.udecl]
5062 If a function declaration in namespace scope or block
5063 scope has the same name and the same parameter types as a
5064 function introduced by a using declaration the program is
5066 for (tmp1
= oldval
; tmp1
; tmp1
= OVL_NEXT (tmp1
))
5068 tree old_fn
= OVL_CURRENT (tmp1
);
5070 if (new_fn
== old_fn
)
5071 /* The function already exists in the current namespace. */
5073 else if (OVL_USED (tmp1
))
5074 continue; /* this is a using decl */
5075 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn
)),
5076 TYPE_ARG_TYPES (TREE_TYPE (old_fn
))))
5078 /* There was already a non-using declaration in
5079 this scope with the same parameter types. If both
5080 are the same extern "C" functions, that's ok. */
5081 if (!decls_match (new_fn
, old_fn
))
5082 cp_error ("`%D' is already declared in this scope", name
);
5087 /* If we broke out of the loop, there's no reason to add
5088 this function to the using declarations for this
5093 *newval
= build_overload (OVL_CURRENT (tmp
), *newval
);
5094 if (TREE_CODE (*newval
) != OVERLOAD
)
5095 *newval
= ovl_cons (*newval
, NULL_TREE
);
5096 OVL_USED (*newval
) = 1;
5101 *newval
= BINDING_VALUE (decls
);
5103 duplicate_decls (*newval
, oldval
);
5106 *newtype
= BINDING_TYPE (decls
);
5107 if (oldtype
&& *newtype
&& oldtype
!= *newtype
)
5109 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5115 /* Process a using-declaration not appearing in class or local scope. */
5118 do_toplevel_using_decl (decl
)
5121 tree scope
, name
, binding
;
5122 tree oldval
, oldtype
, newval
, newtype
;
5124 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
5125 if (decl
== NULL_TREE
)
5128 binding
= binding_for_name (name
, current_namespace
);
5130 oldval
= BINDING_VALUE (binding
);
5131 oldtype
= BINDING_TYPE (binding
);
5133 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
5135 /* Copy declarations found. */
5137 BINDING_VALUE (binding
) = newval
;
5139 BINDING_TYPE (binding
) = newtype
;
5143 /* Process a using-declaration at function scope. */
5146 do_local_using_decl (decl
)
5150 tree oldval
, oldtype
, newval
, newtype
;
5152 decl
= validate_nonmember_using_decl (decl
, &scope
, &name
);
5153 if (decl
== NULL_TREE
)
5156 if (building_stmt_tree ()
5157 && at_function_scope_p ())
5158 add_decl_stmt (decl
);
5159 if (processing_template_decl
)
5162 oldval
= lookup_name_current_level (name
);
5163 oldtype
= lookup_type_current_level (name
);
5165 do_nonmember_using_decl (scope
, name
, oldval
, oldtype
, &newval
, &newtype
);
5169 if (is_overloaded_fn (newval
))
5173 /* We only need to push declarations for those functions
5174 that were not already bound in the current level.
5175 The old value might be NULL_TREE, it might be a single
5176 function, or an OVERLOAD. */
5177 if (oldval
&& TREE_CODE (oldval
) == OVERLOAD
)
5178 term
= OVL_FUNCTION (oldval
);
5181 for (fn
= newval
; fn
&& OVL_CURRENT (fn
) != term
;
5183 push_overloaded_decl (OVL_CURRENT (fn
),
5184 PUSH_LOCAL
| PUSH_USING
);
5187 push_local_binding (name
, newval
, PUSH_USING
);
5190 set_identifier_type_value (name
, newtype
);
5194 do_class_using_decl (decl
)
5199 if (TREE_CODE (decl
) != SCOPE_REF
5200 || !TYPE_P (TREE_OPERAND (decl
, 0)))
5202 cp_error ("using-declaration for non-member at class scope");
5205 name
= TREE_OPERAND (decl
, 1);
5206 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
5208 cp_error ("using-declaration for destructor");
5211 if (TREE_CODE (name
) == TYPE_DECL
)
5212 name
= DECL_NAME (name
);
5214 my_friendly_assert (TREE_CODE (name
) == IDENTIFIER_NODE
, 980716);
5216 value
= build_lang_decl (USING_DECL
, name
, void_type_node
);
5217 DECL_INITIAL (value
) = TREE_OPERAND (decl
, 0);
5221 /* Process a using-directive. */
5224 do_using_directive (namespace)
5227 if (namespace == fake_std_node
)
5229 /* using namespace A::B::C; */
5230 if (TREE_CODE (namespace) == SCOPE_REF
)
5231 namespace = TREE_OPERAND (namespace, 1);
5232 if (TREE_CODE (namespace) == IDENTIFIER_NODE
)
5234 /* Lookup in lexer did not find a namespace. */
5235 cp_error ("namespace `%T' undeclared", namespace);
5238 if (TREE_CODE (namespace) != NAMESPACE_DECL
)
5240 cp_error ("`%T' is not a namespace", namespace);
5243 namespace = ORIGINAL_NAMESPACE (namespace);
5244 if (!toplevel_bindings_p ())
5245 push_using_directive (namespace);
5248 add_using_namespace (current_namespace
, namespace, 0);
5252 check_default_args (x
)
5255 tree arg
= TYPE_ARG_TYPES (TREE_TYPE (x
));
5256 int saw_def
= 0, i
= 0 - (TREE_CODE (TREE_TYPE (x
)) == METHOD_TYPE
);
5257 for (; arg
&& arg
!= void_list_node
; arg
= TREE_CHAIN (arg
), ++i
)
5259 if (TREE_PURPOSE (arg
))
5263 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5274 TREE_USED (decl
) = 1;
5275 if (processing_template_decl
)
5277 assemble_external (decl
);
5279 /* Is it a synthesized method that needs to be synthesized? */
5280 if (TREE_CODE (decl
) == FUNCTION_DECL
5281 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
)
5282 && DECL_ARTIFICIAL (decl
)
5283 && ! DECL_INITIAL (decl
)
5284 /* Kludge: don't synthesize for default args. */
5285 && current_function_decl
)
5287 synthesize_method (decl
);
5288 /* If we've already synthesized the method we don't need to
5289 instantiate it, so we can return right away. */
5293 /* If this is a function or variable that is an instance of some
5294 template, we now know that we will need to actually do the
5295 instantiation. We check that DECL is not an explicit
5296 instantiation because that is not checked in instantiate_decl. */
5297 if ((DECL_NON_THUNK_FUNCTION_P (decl
) || TREE_CODE (decl
) == VAR_DECL
)
5298 && DECL_LANG_SPECIFIC (decl
) && DECL_TEMPLATE_INFO (decl
)
5299 && (!DECL_EXPLICIT_INSTANTIATION (decl
)
5300 || (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INLINE (decl
))))
5301 instantiate_decl (decl
, /*defer_ok=*/1);
5304 /* Helper function for named_class_head_sans_basetype nonterminal. We
5305 have just seen something of the form `AGGR SCOPE::ID'. Return a
5306 TYPE_DECL for the type declared by ID in SCOPE. */
5309 handle_class_head (aggr
, scope
, id
)
5310 tree aggr
, scope
, id
;
5312 tree decl
= NULL_TREE
;
5314 if (TREE_CODE (id
) == TYPE_DECL
)
5315 /* We must bash typedefs back to the main decl of the type. Otherwise
5316 we become confused about scopes. */
5317 decl
= TYPE_MAIN_DECL (TREE_TYPE (id
));
5318 else if (DECL_CLASS_TEMPLATE_P (id
))
5319 decl
= DECL_TEMPLATE_RESULT (id
);
5322 tree current
= current_scope ();
5324 if (current
== NULL_TREE
)
5325 current
= current_namespace
;
5326 if (scope
== fake_std_node
)
5327 scope
= global_namespace
;
5328 if (scope
== NULL_TREE
)
5329 scope
= global_namespace
;
5333 /* According to the suggested resolution of core issue 180,
5334 'typename' is assumed after a class-key. */
5335 decl
= make_typename_type (scope
, id
, 1);
5336 if (decl
!= error_mark_node
)
5337 decl
= TYPE_MAIN_DECL (decl
);
5341 else if (scope
== current
)
5343 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5344 Be nice about it. */
5346 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5347 FROB_CONTEXT (scope
), id
);
5349 else if (scope
!= global_namespace
)
5350 cp_error ("`%T' does not have a nested type named `%D'", scope
, id
);
5352 cp_error ("no file-scope type named `%D'", id
);
5354 /* Inject it at the current scope. */
5356 decl
= TYPE_MAIN_DECL (xref_tag (aggr
, id
, 1));
5359 /* Enter the SCOPE. If this turns out not to be a definition, the
5360 parser must leave the scope. */
5361 push_scope (CP_DECL_CONTEXT (decl
));
5363 /* If we see something like:
5365 template <typename T> struct S::I ....
5367 we must create a TEMPLATE_DECL for the nested type. */
5368 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5369 decl
= push_template_decl (decl
);
5374 /* Initialize decl2.c. */
5379 ggc_add_tree_root (&decl_namespace_list
, 1);
5380 ggc_add_tree_varray_root (&deferred_fns
, 1);
5381 ggc_add_tree_varray_root (&pending_statics
, 1);
5382 ggc_add_tree_varray_root (&ssdf_decls
, 1);
5383 ggc_add_tree_root (&ssdf_decl
, 1);
5384 ggc_add_tree_root (&priority_decl
, 1);
5385 ggc_add_tree_root (&initialize_p_decl
, 1);
5386 ggc_add_tree_root (&pending_vtables
, 1);