2002-04-28 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / cp / decl2.c
bloba904c0dd4bb14bdbb9628fe79e886c4d0aa4df4a
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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)
11 any later version.
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. */
31 #include "config.h"
32 #include "system.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "decl.h"
39 #include "lex.h"
40 #include "output.h"
41 #include "except.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "timevar.h"
45 #include "cpplib.h"
46 #include "target.h"
47 extern cpp_reader *parse_in;
49 /* This structure contains information about the initializations
50 and/or destructions required for a particular priority level. */
51 typedef struct priority_info_s {
52 /* Non-zero if there have been any initializations at this priority
53 throughout the translation unit. */
54 int initializations_p;
55 /* Non-zero if there have been any destructions at this priority
56 throughout the translation unit. */
57 int destructions_p;
58 } *priority_info;
60 static void mark_vtable_entries PARAMS ((tree));
61 static void grok_function_init PARAMS ((tree, tree));
62 static int finish_vtable_vardecl PARAMS ((tree *, void *));
63 static int prune_vtable_vardecl PARAMS ((tree *, void *));
64 static int is_namespace_ancestor PARAMS ((tree, tree));
65 static void add_using_namespace PARAMS ((tree, tree, int));
66 static tree ambiguous_decl PARAMS ((tree, tree, tree,int));
67 static tree build_anon_union_vars PARAMS ((tree, tree*, int, int));
68 static int acceptable_java_type PARAMS ((tree));
69 static void output_vtable_inherit PARAMS ((tree));
70 static tree start_objects PARAMS ((int, int));
71 static void finish_objects PARAMS ((int, int, tree));
72 static tree merge_functions PARAMS ((tree, tree));
73 static tree decl_namespace PARAMS ((tree));
74 static tree validate_nonmember_using_decl PARAMS ((tree, tree *, tree *));
75 static void do_nonmember_using_decl PARAMS ((tree, tree, tree, tree,
76 tree *, tree *));
77 static tree start_static_storage_duration_function PARAMS ((void));
78 static void finish_static_storage_duration_function PARAMS ((tree));
79 static priority_info get_priority_info PARAMS ((int));
80 static void do_static_initialization PARAMS ((tree, tree));
81 static void do_static_destruction PARAMS ((tree));
82 static tree start_static_initialization_or_destruction PARAMS ((tree, int));
83 static void finish_static_initialization_or_destruction PARAMS ((tree));
84 static void generate_ctor_or_dtor_function PARAMS ((int, int));
85 static int generate_ctor_and_dtor_functions_for_priority
86 PARAMS ((splay_tree_node, void *));
87 static tree prune_vars_needing_no_initialization PARAMS ((tree));
88 static void write_out_vars PARAMS ((tree));
89 static void import_export_class PARAMS ((tree));
90 static tree key_method PARAMS ((tree));
91 static int compare_options PARAMS ((const PTR, const PTR));
92 static tree get_guard_bits PARAMS ((tree));
94 /* A list of static class variables. This is needed, because a
95 static class variable can be declared inside the class without
96 an initializer, and then initialized, statically, outside the class. */
97 static varray_type pending_statics;
98 #define pending_statics_used \
99 (pending_statics ? pending_statics->elements_used : 0)
101 /* A list of functions which were declared inline, but which we
102 may need to emit outline anyway. */
103 static varray_type deferred_fns;
104 #define deferred_fns_used \
105 (deferred_fns ? deferred_fns->elements_used : 0)
107 /* Flag used when debugging spew.c */
109 extern int spew_debug;
111 /* Nonzero if we're done parsing and into end-of-file activities. */
113 int at_eof;
115 /* Functions called along with real static constructors and destructors. */
117 tree static_ctors;
118 tree static_dtors;
120 /* The :: namespace. */
122 tree global_namespace;
124 /* C (and C++) language-specific option variables. */
126 /* Nonzero means don't recognize the keyword `asm'. */
128 int flag_no_asm;
130 /* Nonzero means don't recognize any extension keywords. */
132 int flag_no_gnu_keywords;
134 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
136 int flag_signed_bitfields = 1;
138 /* Nonzero means enable obscure standard features and disable GNU
139 extensions that might cause standard-compliant code to be
140 miscompiled. */
142 int flag_ansi;
144 /* Nonzero means do emit exported implementations of functions even if
145 they can be inlined. */
147 int flag_implement_inlines = 1;
149 /* Nonzero means do emit exported implementations of templates, instead of
150 multiple static copies in each file that needs a definition. */
152 int flag_external_templates;
154 /* Nonzero means that the decision to emit or not emit the implementation of a
155 template depends on where the template is instantiated, rather than where
156 it is defined. */
158 int flag_alt_external_templates;
160 /* Nonzero means that implicit instantiations will be emitted if needed. */
162 int flag_implicit_templates = 1;
164 /* Nonzero means that implicit instantiations of inline templates will be
165 emitted if needed, even if instantiations of non-inline templates
166 aren't. */
168 int flag_implicit_inline_templates = 1;
170 /* Nonzero means warn about implicit declarations. */
172 int warn_implicit = 1;
174 /* Nonzero means warn about usage of long long when `-pedantic'. */
176 int warn_long_long = 1;
178 /* Nonzero means warn when all ctors or dtors are private, and the class
179 has no friends. */
181 int warn_ctor_dtor_privacy = 1;
183 /* Nonzero means generate separate instantiation control files and juggle
184 them at link time. */
186 int flag_use_repository;
188 /* Nonzero if we want to issue diagnostics that the standard says are not
189 required. */
191 int flag_optional_diags = 1;
193 /* Nonzero means give string constants the type `const char *', as mandated
194 by the standard. */
196 int flag_const_strings = 1;
198 /* Nonzero means warn about deprecated conversion from string constant to
199 `char *'. */
201 int warn_write_strings;
203 /* Nonzero means warn about pointer casts that can drop a type qualifier
204 from the pointer target type. */
206 int warn_cast_qual;
208 /* Nonzero means warn about sizeof(function) or addition/subtraction
209 of function pointers. */
211 int warn_pointer_arith = 1;
213 /* Nonzero means warn for any function def without prototype decl. */
215 int warn_missing_prototypes;
217 /* Nonzero means warn about multiple (redundant) decls for the same single
218 variable or function. */
220 int warn_redundant_decls;
222 /* Warn if initializer is not completely bracketed. */
224 int warn_missing_braces;
226 /* Warn about comparison of signed and unsigned values. */
228 int warn_sign_compare;
230 /* Warn about testing equality of floating point numbers. */
232 int warn_float_equal = 0;
234 /* Warn about functions which might be candidates for format attributes. */
236 int warn_missing_format_attribute;
238 /* Warn about a subscript that has type char. */
240 int warn_char_subscripts;
242 /* Warn if a type conversion is done that might have confusing results. */
244 int warn_conversion;
246 /* Warn if adding () is suggested. */
248 int warn_parentheses;
250 /* Non-zero means warn in function declared in derived class has the
251 same name as a virtual in the base class, but fails to match the
252 type signature of any virtual function in the base class. */
254 int warn_overloaded_virtual;
256 /* Non-zero means warn when declaring a class that has a non virtual
257 destructor, when it really ought to have a virtual one. */
259 int warn_nonvdtor;
261 /* Non-zero means warn when the compiler will reorder code. */
263 int warn_reorder;
265 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
267 int warn_synth;
269 /* Non-zero means warn when we convert a pointer to member function
270 into a pointer to (void or function). */
272 int warn_pmf2ptr = 1;
274 /* Nonzero means warn about violation of some Effective C++ style rules. */
276 int warn_ecpp;
278 /* Nonzero means warn where overload resolution chooses a promotion from
279 unsigned to signed over a conversion to an unsigned of the same size. */
281 int warn_sign_promo;
283 /* Nonzero means warn when an old-style cast is used. */
285 int warn_old_style_cast;
287 /* Warn about #pragma directives that are not recognised. */
289 int warn_unknown_pragmas; /* Tri state variable. */
291 /* Nonzero means warn about use of multicharacter literals. */
293 int warn_multichar = 1;
295 /* Nonzero means warn when non-templatized friend functions are
296 declared within a template */
298 int warn_nontemplate_friend = 1;
300 /* Nonzero means complain about deprecated features. */
302 int warn_deprecated = 1;
304 /* Nonzero means `$' can be in an identifier. */
306 #ifndef DOLLARS_IN_IDENTIFIERS
307 #define DOLLARS_IN_IDENTIFIERS 1
308 #endif
309 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
311 /* Nonzero means allow Microsoft extensions without a pedwarn. */
313 int flag_ms_extensions;
315 /* C++ specific flags. */
317 /* Nonzero means we should attempt to elide constructors when possible. */
319 int flag_elide_constructors = 1;
321 /* Nonzero means that member functions defined in class scope are
322 inline by default. */
324 int flag_default_inline = 1;
326 /* Controls whether compiler generates 'type descriptor' that give
327 run-time type information. */
329 int flag_rtti = 1;
331 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
332 objects. */
334 int flag_huge_objects;
336 /* Nonzero if we want to conserve space in the .o files. We do this
337 by putting uninitialized data and runtime initialized data into
338 .common instead of .data at the expense of not flagging multiple
339 definitions. */
341 int flag_conserve_space;
343 /* Nonzero if we want to obey access control semantics. */
345 int flag_access_control = 1;
347 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
349 int flag_operator_names = 1;
351 /* Nonzero if we want to check the return value of new and avoid calling
352 constructors if it is a null pointer. */
354 int flag_check_new;
356 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
357 initialization variables.
358 0: Old rules, set by -fno-for-scope.
359 2: New ISO rules, set by -ffor-scope.
360 1: Try to implement new ISO rules, but with backup compatibility
361 (and warnings). This is the default, for now. */
363 int flag_new_for_scope = 1;
365 /* Nonzero if we want to emit defined symbols with common-like linkage as
366 weak symbols where possible, in order to conform to C++ semantics.
367 Otherwise, emit them as local symbols. */
369 int flag_weak = 1;
371 /* Nonzero to use __cxa_atexit, rather than atexit, to register
372 destructors for local statics and global objects. */
374 int flag_use_cxa_atexit;
376 /* Maximum template instantiation depth. This limit is rather
377 arbitrary, but it exists to limit the time it takes to notice
378 infinite template instantiations. */
380 int max_tinst_depth = 500;
382 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
384 int flag_vtable_gc;
386 /* Nonzero means make the default pedwarns warnings instead of errors.
387 The value of this flag is ignored if -pedantic is specified. */
389 int flag_permissive;
391 /* Nonzero means to implement standard semantics for exception
392 specifications, calling unexpected if an exception is thrown that
393 doesn't match the specification. Zero means to treat them as
394 assertions and optimize accordingly, but not check them. */
396 int flag_enforce_eh_specs = 1;
398 /* Table of language-dependent -f options.
399 STRING is the option name. VARIABLE is the address of the variable.
400 ON_VALUE is the value to store in VARIABLE
401 if `-fSTRING' is seen as an option.
402 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
404 static const struct { const char *const string; int *const variable; const int on_value;}
405 lang_f_options[] =
407 /* C/C++ options. */
408 {"signed-char", &flag_signed_char, 1},
409 {"unsigned-char", &flag_signed_char, 0},
410 {"signed-bitfields", &flag_signed_bitfields, 1},
411 {"unsigned-bitfields", &flag_signed_bitfields, 0},
412 {"short-enums", &flag_short_enums, 1},
413 {"short-double", &flag_short_double, 1},
414 {"short-wchar", &flag_short_wchar, 1},
415 {"asm", &flag_no_asm, 0},
416 {"builtin", &flag_no_builtin, 0},
418 /* C++-only options. */
419 {"access-control", &flag_access_control, 1},
420 {"check-new", &flag_check_new, 1},
421 {"conserve-space", &flag_conserve_space, 1},
422 {"const-strings", &flag_const_strings, 1},
423 {"default-inline", &flag_default_inline, 1},
424 {"dollars-in-identifiers", &dollars_in_ident, 1},
425 {"elide-constructors", &flag_elide_constructors, 1},
426 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
427 {"external-templates", &flag_external_templates, 1},
428 {"for-scope", &flag_new_for_scope, 2},
429 {"gnu-keywords", &flag_no_gnu_keywords, 0},
430 {"handle-exceptions", &flag_exceptions, 1},
431 {"implement-inlines", &flag_implement_inlines, 1},
432 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
433 {"implicit-templates", &flag_implicit_templates, 1},
434 {"ms-extensions", &flag_ms_extensions, 1},
435 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
436 {"operator-names", &flag_operator_names, 1},
437 {"optional-diags", &flag_optional_diags, 1},
438 {"permissive", &flag_permissive, 1},
439 {"repo", &flag_use_repository, 1},
440 {"rtti", &flag_rtti, 1},
441 {"stats", &flag_detailed_statistics, 1},
442 {"vtable-gc", &flag_vtable_gc, 1},
443 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
444 {"weak", &flag_weak, 1}
447 /* The list of `-f' options that we no longer support. The `-f'
448 prefix is not given in this table. The `-fno-' variants are not
449 listed here. This table must be kept in alphabetical order. */
450 static const char * const unsupported_options[] = {
451 "all-virtual",
452 "cond-mismatch",
453 "enum-int-equiv",
454 "guiding-decls",
455 "honor-std",
456 "huge-objects",
457 "labels-ok",
458 "new-abi",
459 "nonnull-objects",
460 "squangle",
461 "strict-prototype",
462 "this-is-variable",
463 "vtable-thunks",
464 "xref"
467 /* Compare two option strings, pointed two by P1 and P2, for use with
468 bsearch. */
470 static int
471 compare_options (p1, p2)
472 const PTR p1;
473 const PTR p2;
475 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
478 /* Decode the string P as a language-specific option.
479 Return the number of strings consumed for a valid option.
480 Otherwise return 0. Should not complain if it does not
481 recognise the option. */
483 int
484 cxx_decode_option (argc, argv)
485 int argc;
486 char **argv;
488 int strings_processed;
489 const char *p = argv[0];
491 strings_processed = cpp_handle_option (parse_in, argc, argv, 0);
493 if (p[0] == '-' && p[1] == 'f')
495 /* Some kind of -f option.
496 P's value is the option sans `-f'.
497 Search for it in the table of options. */
498 const char *option_value = NULL;
499 const char *positive_option;
500 size_t j;
502 p += 2;
503 /* Try special -f options. */
505 /* See if this is one of the options no longer supported. We
506 used to support these options, so we continue to accept them,
507 with a warning. */
508 if (strncmp (p, "no-", strlen ("no-")) == 0)
509 positive_option = p + strlen ("no-");
510 else
511 positive_option = p;
513 /* If the option is present, issue a warning. Indicate to our
514 caller that the option was processed successfully. */
515 if (bsearch (&positive_option,
516 unsupported_options,
517 ARRAY_SIZE (unsupported_options),
518 sizeof (unsupported_options[0]),
519 compare_options))
521 warning ("-f%s is no longer supported", p);
522 return 1;
525 if (!strcmp (p, "handle-exceptions")
526 || !strcmp (p, "no-handle-exceptions"))
527 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
528 else if (! strcmp (p, "alt-external-templates"))
530 flag_external_templates = 1;
531 flag_alt_external_templates = 1;
532 cp_deprecated ("-falt-external-templates");
534 else if (! strcmp (p, "no-alt-external-templates"))
535 flag_alt_external_templates = 0;
536 else if (!strcmp (p, "repo"))
538 flag_use_repository = 1;
539 flag_implicit_templates = 0;
541 else if (!strcmp (p, "external-templates"))
543 flag_external_templates = 1;
544 cp_deprecated ("-fexternal-templates");
546 else if ((option_value
547 = skip_leading_substring (p, "template-depth-")))
548 max_tinst_depth
549 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
550 else if ((option_value
551 = skip_leading_substring (p, "name-mangling-version-")))
553 warning ("-fname-mangling-version is no longer supported");
554 return 1;
556 else if (dump_switch_p (p))
558 else
560 int found = 0;
562 for (j = 0; !found && j < ARRAY_SIZE (lang_f_options); j++)
564 if (!strcmp (p, lang_f_options[j].string))
566 *lang_f_options[j].variable = lang_f_options[j].on_value;
567 /* A goto here would be cleaner,
568 but breaks the VAX pcc. */
569 found = 1;
571 else if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
572 && ! strcmp (p+3, lang_f_options[j].string))
574 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
575 found = 1;
579 return found;
582 else if (p[0] == '-' && p[1] == 'W')
584 int setting = 1;
586 /* The -W options control the warning behavior of the compiler. */
587 p += 2;
589 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
590 setting = 0, p += 3;
592 if (!strcmp (p, "implicit"))
593 warn_implicit = setting;
594 else if (!strcmp (p, "long-long"))
595 warn_long_long = setting;
596 else if (!strcmp (p, "return-type"))
597 warn_return_type = setting;
598 else if (!strcmp (p, "ctor-dtor-privacy"))
599 warn_ctor_dtor_privacy = setting;
600 else if (!strcmp (p, "write-strings"))
601 warn_write_strings = setting;
602 else if (!strcmp (p, "cast-qual"))
603 warn_cast_qual = setting;
604 else if (!strcmp (p, "char-subscripts"))
605 warn_char_subscripts = setting;
606 else if (!strcmp (p, "pointer-arith"))
607 warn_pointer_arith = setting;
608 else if (!strcmp (p, "missing-prototypes"))
609 warn_missing_prototypes = setting;
610 else if (!strcmp (p, "strict-prototypes"))
612 if (setting == 0)
613 warning ("-Wno-strict-prototypes is not supported in C++");
615 else if (!strcmp (p, "redundant-decls"))
616 warn_redundant_decls = setting;
617 else if (!strcmp (p, "missing-braces"))
618 warn_missing_braces = setting;
619 else if (!strcmp (p, "sign-compare"))
620 warn_sign_compare = setting;
621 else if (!strcmp (p, "float-equal"))
622 warn_float_equal = setting;
623 else if (!strcmp (p, "format"))
624 set_Wformat (setting);
625 else if (!strcmp (p, "format=2"))
626 set_Wformat (2);
627 else if (!strcmp (p, "format-y2k"))
628 warn_format_y2k = setting;
629 else if (!strcmp (p, "format-extra-args"))
630 warn_format_extra_args = setting;
631 else if (!strcmp (p, "format-nonliteral"))
632 warn_format_nonliteral = setting;
633 else if (!strcmp (p, "format-security"))
634 warn_format_security = setting;
635 else if (!strcmp (p, "missing-format-attribute"))
636 warn_missing_format_attribute = setting;
637 else if (!strcmp (p, "conversion"))
638 warn_conversion = setting;
639 else if (!strcmp (p, "parentheses"))
640 warn_parentheses = setting;
641 else if (!strcmp (p, "non-virtual-dtor"))
642 warn_nonvdtor = setting;
643 else if (!strcmp (p, "reorder"))
644 warn_reorder = setting;
645 else if (!strcmp (p, "synth"))
646 warn_synth = setting;
647 else if (!strcmp (p, "pmf-conversions"))
648 warn_pmf2ptr = setting;
649 else if (!strcmp (p, "effc++"))
650 warn_ecpp = setting;
651 else if (!strcmp (p, "sign-promo"))
652 warn_sign_promo = setting;
653 else if (!strcmp (p, "old-style-cast"))
654 warn_old_style_cast = setting;
655 else if (!strcmp (p, "overloaded-virtual"))
656 warn_overloaded_virtual = setting;
657 else if (!strcmp (p, "multichar"))
658 warn_multichar = setting;
659 else if (!strcmp (p, "unknown-pragmas"))
660 /* Set to greater than 1, so that even unknown pragmas in
661 system headers will be warned about. */
662 warn_unknown_pragmas = setting * 2;
663 else if (!strcmp (p, "non-template-friend"))
664 warn_nontemplate_friend = setting;
665 else if (!strcmp (p, "deprecated"))
666 warn_deprecated = setting;
667 else if (!strcmp (p, "comment"))
668 ; /* cpp handles this one. */
669 else if (!strcmp (p, "comments"))
670 ; /* cpp handles this one. */
671 else if (!strcmp (p, "trigraphs"))
672 ; /* cpp handles this one. */
673 else if (!strcmp (p, "import"))
674 ; /* cpp handles this one. */
675 else if (!strcmp (p, "all"))
677 warn_return_type = setting;
678 set_Wunused (setting);
679 warn_implicit = setting;
680 warn_switch = setting;
681 set_Wformat (setting);
682 warn_parentheses = setting;
683 warn_missing_braces = setting;
684 warn_sign_compare = setting;
685 warn_multichar = setting;
686 /* We save the value of warn_uninitialized, since if they put
687 -Wuninitialized on the command line, we need to generate a
688 warning about not using it without also specifying -O. */
689 if (warn_uninitialized != 1)
690 warn_uninitialized = (setting ? 2 : 0);
691 /* Only warn about unknown pragmas that are not in system
692 headers. */
693 warn_unknown_pragmas = 1;
695 /* C++-specific warnings. */
696 warn_ctor_dtor_privacy = setting;
697 warn_nonvdtor = setting;
698 warn_reorder = setting;
699 warn_nontemplate_friend = setting;
701 else return strings_processed;
703 else if (!strcmp (p, "-E"))
704 flag_preprocess_only = 1;
705 else if (!strcmp (p, "-ansi"))
706 flag_no_nonansi_builtin = 1, flag_ansi = 1,
707 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
708 #ifdef SPEW_DEBUG
709 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
710 it's probably safe to assume no sane person would ever want to use this
711 under normal circumstances. */
712 else if (!strcmp (p, "-spew-debug"))
713 spew_debug = 1;
714 #endif
715 else
716 return strings_processed;
718 return 1;
721 /* Incorporate `const' and `volatile' qualifiers for member functions.
722 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
723 QUALS is a list of qualifiers. Returns any explicit
724 top-level qualifiers of the method's this pointer, anything other than
725 TYPE_UNQUALIFIED will be an extension. */
728 grok_method_quals (ctype, function, quals)
729 tree ctype, function, quals;
731 tree fntype = TREE_TYPE (function);
732 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
733 int type_quals = TYPE_UNQUALIFIED;
734 int dup_quals = TYPE_UNQUALIFIED;
735 int this_quals = TYPE_UNQUALIFIED;
739 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
741 if ((type_quals | this_quals) & tq)
742 dup_quals |= tq;
743 else if (tq & TYPE_QUAL_RESTRICT)
744 this_quals |= tq;
745 else
746 type_quals |= tq;
747 quals = TREE_CHAIN (quals);
749 while (quals);
751 if (dup_quals != TYPE_UNQUALIFIED)
752 error ("duplicate type qualifiers in %s declaration",
753 TREE_CODE (function) == FUNCTION_DECL
754 ? "member function" : "type");
756 ctype = cp_build_qualified_type (ctype, type_quals);
757 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
758 (TREE_CODE (fntype) == METHOD_TYPE
759 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
760 : TYPE_ARG_TYPES (fntype)));
761 if (raises)
762 fntype = build_exception_variant (fntype, raises);
764 TREE_TYPE (function) = fntype;
765 return this_quals;
768 /* Warn when -fexternal-templates is used and #pragma
769 interface/implementation is not used all the times it should be,
770 inform the user. */
772 void
773 warn_if_unknown_interface (decl)
774 tree decl;
776 static int already_warned = 0;
777 if (already_warned++)
778 return;
780 if (flag_alt_external_templates)
782 tree til = tinst_for_decl ();
783 int sl = lineno;
784 const char *sf = input_filename;
786 if (til)
788 lineno = TINST_LINE (til);
789 input_filename = TINST_FILE (til);
791 warning ("template `%#D' instantiated in file without #pragma interface",
792 decl);
793 lineno = sl;
794 input_filename = sf;
796 else
797 cp_warning_at ("template `%#D' defined in file without #pragma interface",
798 decl);
801 /* A subroutine of the parser, to handle a component list. */
803 void
804 grok_x_components (specs)
805 tree specs;
807 tree t;
809 specs = strip_attrs (specs);
811 check_tag_decl (specs);
812 t = groktypename (build_tree_list (specs, NULL_TREE));
814 /* The only case where we need to do anything additional here is an
815 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
816 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
817 return;
819 fixup_anonymous_aggr (t);
820 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
823 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
824 indicated NAME. */
826 tree
827 build_artificial_parm (name, type)
828 tree name;
829 tree type;
831 tree parm;
833 parm = build_decl (PARM_DECL, name, type);
834 DECL_ARTIFICIAL (parm) = 1;
835 /* All our artificial parms are implicitly `const'; they cannot be
836 assigned to. */
837 TREE_READONLY (parm) = 1;
838 DECL_ARG_TYPE (parm) = type;
839 return parm;
842 /* Constructors for types with virtual baseclasses need an "in-charge" flag
843 saying whether this constructor is responsible for initialization of
844 virtual baseclasses or not. All destructors also need this "in-charge"
845 flag, which additionally determines whether or not the destructor should
846 free the memory for the object.
848 This function adds the "in-charge" flag to member function FN if
849 appropriate. It is called from grokclassfn and tsubst.
850 FN must be either a constructor or destructor.
852 The in-charge flag follows the 'this' parameter, and is followed by the
853 VTT parm (if any), then the user-written parms. */
855 void
856 maybe_retrofit_in_chrg (fn)
857 tree fn;
859 tree basetype, arg_types, parms, parm, fntype;
861 /* If we've already add the in-charge parameter don't do it again. */
862 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
863 return;
865 /* When processing templates we can't know, in general, whether or
866 not we're going to have virtual baseclasses. */
867 if (uses_template_parms (fn))
868 return;
870 /* We don't need an in-charge parameter for constructors that don't
871 have virtual bases. */
872 if (DECL_CONSTRUCTOR_P (fn)
873 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
874 return;
876 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
877 basetype = TREE_TYPE (TREE_VALUE (arg_types));
878 arg_types = TREE_CHAIN (arg_types);
880 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
882 /* If this is a subobject constructor or destructor, our caller will
883 pass us a pointer to our VTT. */
884 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
886 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
888 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
889 TREE_CHAIN (parm) = parms;
890 parms = parm;
892 /* ...and then to TYPE_ARG_TYPES. */
893 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
895 DECL_HAS_VTT_PARM_P (fn) = 1;
898 /* Then add the in-charge parm (before the VTT parm). */
899 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
900 TREE_CHAIN (parm) = parms;
901 parms = parm;
902 arg_types = hash_tree_chain (integer_type_node, arg_types);
904 /* Insert our new parameter(s) into the list. */
905 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
907 /* And rebuild the function type. */
908 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
909 arg_types);
910 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
911 fntype = build_exception_variant (fntype,
912 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
913 TREE_TYPE (fn) = fntype;
915 /* Now we've got the in-charge parameter. */
916 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
919 /* Classes overload their constituent function names automatically.
920 When a function name is declared in a record structure,
921 its name is changed to it overloaded name. Since names for
922 constructors and destructors can conflict, we place a leading
923 '$' for destructors.
925 CNAME is the name of the class we are grokking for.
927 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
929 FLAGS contains bits saying what's special about today's
930 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
932 If FUNCTION is a destructor, then we must add the `auto-delete' field
933 as a second parameter. There is some hair associated with the fact
934 that we must "declare" this variable in the manner consistent with the
935 way the rest of the arguments were declared.
937 QUALS are the qualifiers for the this pointer. */
939 void
940 grokclassfn (ctype, function, flags, quals)
941 tree ctype, function;
942 enum overload_flags flags;
943 tree quals;
945 tree fn_name = DECL_NAME (function);
946 int this_quals = TYPE_UNQUALIFIED;
948 /* Even within an `extern "C"' block, members get C++ linkage. See
949 [dcl.link] for details. */
950 SET_DECL_LANGUAGE (function, lang_cplusplus);
952 if (fn_name == NULL_TREE)
954 error ("name missing for member function");
955 fn_name = get_identifier ("<anonymous>");
956 DECL_NAME (function) = fn_name;
959 if (quals)
960 this_quals = grok_method_quals (ctype, function, quals);
962 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
964 /* Must add the class instance variable up front. */
965 /* Right now we just make this a pointer. But later
966 we may wish to make it special. */
967 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
968 tree qual_type;
969 tree parm;
971 /* The `this' parameter is implicitly `const'; it cannot be
972 assigned to. */
973 this_quals |= TYPE_QUAL_CONST;
974 qual_type = cp_build_qualified_type (type, this_quals);
975 parm = build_artificial_parm (this_identifier, qual_type);
976 c_apply_type_quals_to_decl (this_quals, parm);
977 TREE_CHAIN (parm) = last_function_parms;
978 last_function_parms = parm;
981 DECL_ARGUMENTS (function) = last_function_parms;
982 DECL_CONTEXT (function) = ctype;
984 if (flags == DTOR_FLAG)
985 DECL_DESTRUCTOR_P (function) = 1;
987 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
988 maybe_retrofit_in_chrg (function);
990 if (flags == DTOR_FLAG)
992 DECL_DESTRUCTOR_P (function) = 1;
993 TYPE_HAS_DESTRUCTOR (ctype) = 1;
997 /* Create an ARRAY_REF, checking for the user doing things backwards
998 along the way. */
1000 tree
1001 grok_array_decl (array_expr, index_exp)
1002 tree array_expr, index_exp;
1004 tree type = TREE_TYPE (array_expr);
1005 tree p1, p2, i1, i2;
1007 if (type == error_mark_node || index_exp == error_mark_node)
1008 return error_mark_node;
1009 if (processing_template_decl)
1010 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1011 array_expr, index_exp);
1013 if (type == NULL_TREE)
1015 /* Something has gone very wrong. Assume we are mistakenly reducing
1016 an expression instead of a declaration. */
1017 error ("parser may be lost: is there a '{' missing somewhere?");
1018 return NULL_TREE;
1021 if (TREE_CODE (type) == OFFSET_TYPE
1022 || TREE_CODE (type) == REFERENCE_TYPE)
1023 type = TREE_TYPE (type);
1025 /* If they have an `operator[]', use that. */
1026 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1027 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1028 array_expr, index_exp, NULL_TREE);
1030 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1031 is a little-known fact that, if `a' is an array and `i' is an
1032 int, you can write `i[a]', which means the same thing as `a[i]'. */
1034 if (TREE_CODE (type) == ARRAY_TYPE)
1035 p1 = array_expr;
1036 else
1037 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1039 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1040 p2 = index_exp;
1041 else
1042 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1044 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1045 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1047 if ((p1 && i2) && (i1 && p2))
1048 error ("ambiguous conversion for array subscript");
1050 if (p1 && i2)
1051 array_expr = p1, index_exp = i2;
1052 else if (i1 && p2)
1053 array_expr = p2, index_exp = i1;
1054 else
1056 error ("invalid types `%T[%T]' for array subscript",
1057 type, TREE_TYPE (index_exp));
1058 return error_mark_node;
1061 if (array_expr == error_mark_node || index_exp == error_mark_node)
1062 error ("ambiguous conversion for array subscript");
1064 return build_array_ref (array_expr, index_exp);
1067 /* Given the cast expression EXP, checking out its validity. Either return
1068 an error_mark_node if there was an unavoidable error, return a cast to
1069 void for trying to delete a pointer w/ the value 0, or return the
1070 call to delete. If DOING_VEC is 1, we handle things differently
1071 for doing an array delete. If DOING_VEC is 2, they gave us the
1072 array size as an argument to delete.
1073 Implements ARM $5.3.4. This is called from the parser. */
1075 tree
1076 delete_sanity (exp, size, doing_vec, use_global_delete)
1077 tree exp, size;
1078 int doing_vec, use_global_delete;
1080 tree t, type;
1081 /* For a regular vector delete (aka, no size argument) we will pass
1082 this down as a NULL_TREE into build_vec_delete. */
1083 tree maxindex = NULL_TREE;
1085 if (exp == error_mark_node)
1086 return exp;
1088 if (processing_template_decl)
1090 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1091 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1092 DELETE_EXPR_USE_VEC (t) = doing_vec;
1093 return t;
1096 if (TREE_CODE (exp) == OFFSET_REF)
1097 exp = resolve_offset_ref (exp);
1098 exp = convert_from_reference (exp);
1099 t = stabilize_reference (exp);
1100 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1102 if (t == NULL_TREE || t == error_mark_node)
1104 error ("type `%#T' argument given to `delete', expected pointer",
1105 TREE_TYPE (exp));
1106 return error_mark_node;
1109 if (doing_vec == 2)
1111 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1112 pedwarn ("anachronistic use of array size in vector delete");
1115 type = TREE_TYPE (t);
1117 /* As of Valley Forge, you can delete a pointer to const. */
1119 /* You can't delete functions. */
1120 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1122 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1123 return error_mark_node;
1126 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1127 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1129 warning ("deleting `%T' is undefined", type);
1130 doing_vec = 0;
1133 /* An array can't have been allocated by new, so complain. */
1134 if (TREE_CODE (t) == ADDR_EXPR
1135 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1136 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1137 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1139 /* Deleting a pointer with the value zero is valid and has no effect. */
1140 if (integer_zerop (t))
1141 return build1 (NOP_EXPR, void_type_node, t);
1143 if (doing_vec)
1144 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1145 use_global_delete);
1146 else
1147 return build_delete (type, t, sfk_deleting_destructor,
1148 LOOKUP_NORMAL, use_global_delete);
1151 /* Report an error if the indicated template declaration is not the
1152 sort of thing that should be a member template. */
1154 void
1155 check_member_template (tmpl)
1156 tree tmpl;
1158 tree decl;
1160 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1161 decl = DECL_TEMPLATE_RESULT (tmpl);
1163 if (TREE_CODE (decl) == FUNCTION_DECL
1164 || (TREE_CODE (decl) == TYPE_DECL
1165 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1167 if (current_function_decl)
1168 /* 14.5.2.2 [temp.mem]
1170 A local class shall not have member templates. */
1171 error ("invalid declaration of member template `%#D' in local class",
1172 decl);
1174 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1176 /* 14.5.2.3 [temp.mem]
1178 A member function template shall not be virtual. */
1179 error
1180 ("invalid use of `virtual' in template declaration of `%#D'",
1181 decl);
1182 DECL_VIRTUAL_P (decl) = 0;
1185 /* The debug-information generating code doesn't know what to do
1186 with member templates. */
1187 DECL_IGNORED_P (tmpl) = 1;
1189 else
1190 error ("template declaration of `%#D'", decl);
1193 /* Return true iff TYPE is a valid Java parameter or return type. */
1195 static int
1196 acceptable_java_type (type)
1197 tree type;
1199 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1200 return 1;
1201 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1203 type = TREE_TYPE (type);
1204 if (TREE_CODE (type) == RECORD_TYPE)
1206 tree args; int i;
1207 if (! TYPE_FOR_JAVA (type))
1208 return 0;
1209 if (! CLASSTYPE_TEMPLATE_INFO (type))
1210 return 1;
1211 args = CLASSTYPE_TI_ARGS (type);
1212 i = TREE_VEC_LENGTH (args);
1213 while (--i >= 0)
1215 type = TREE_VEC_ELT (args, i);
1216 if (TREE_CODE (type) == POINTER_TYPE)
1217 type = TREE_TYPE (type);
1218 if (! TYPE_FOR_JAVA (type))
1219 return 0;
1221 return 1;
1224 return 0;
1227 /* For a METHOD in a Java class CTYPE, return 1 if
1228 the parameter and return types are valid Java types.
1229 Otherwise, print appropriate error messages, and return 0. */
1232 check_java_method (method)
1233 tree method;
1235 int jerr = 0;
1236 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1237 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1238 if (! acceptable_java_type (ret_type))
1240 error ("Java method '%D' has non-Java return type `%T'",
1241 method, ret_type);
1242 jerr++;
1244 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1246 tree type = TREE_VALUE (arg_types);
1247 if (! acceptable_java_type (type))
1249 error ("Java method '%D' has non-Java parameter type `%T'",
1250 method, type);
1251 jerr++;
1254 return jerr ? 0 : 1;
1257 /* Sanity check: report error if this function FUNCTION is not
1258 really a member of the class (CTYPE) it is supposed to belong to.
1259 CNAME is the same here as it is for grokclassfn above. */
1261 tree
1262 check_classfn (ctype, function)
1263 tree ctype, function;
1265 tree fn_name = DECL_NAME (function);
1266 tree fndecl, fndecls;
1267 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1268 tree *methods = 0;
1269 tree *end = 0;
1271 if (DECL_USE_TEMPLATE (function)
1272 && !(TREE_CODE (function) == TEMPLATE_DECL
1273 && DECL_TEMPLATE_SPECIALIZATION (function))
1274 && is_member_template (DECL_TI_TEMPLATE (function)))
1275 /* Since this is a specialization of a member template,
1276 we're not going to find the declaration in the class.
1277 For example, in:
1279 struct S { template <typename T> void f(T); };
1280 template <> void S::f(int);
1282 we're not going to find `S::f(int)', but there's no
1283 reason we should, either. We let our callers know we didn't
1284 find the method, but we don't complain. */
1285 return NULL_TREE;
1287 if (method_vec != 0)
1289 methods = &TREE_VEC_ELT (method_vec, 0);
1290 end = TREE_VEC_END (method_vec);
1292 /* First suss out ctors and dtors. */
1293 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1294 && DECL_CONSTRUCTOR_P (function))
1295 goto got_it;
1296 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1297 && DECL_DESTRUCTOR_P (function))
1298 goto got_it;
1300 while (++methods != end && *methods)
1302 fndecl = *methods;
1303 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1305 got_it:
1306 for (fndecls = *methods; fndecls != NULL_TREE;
1307 fndecls = OVL_NEXT (fndecls))
1309 fndecl = OVL_CURRENT (fndecls);
1311 /* We cannot simply call decls_match because this
1312 doesn't work for static member functions that are
1313 pretending to be methods, and because the name
1314 may have been changed by asm("new_name"). */
1315 if (DECL_NAME (function) == DECL_NAME (fndecl))
1317 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1318 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1320 /* Get rid of the this parameter on functions that become
1321 static. */
1322 if (DECL_STATIC_FUNCTION_P (fndecl)
1323 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1324 p1 = TREE_CHAIN (p1);
1326 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1327 TREE_TYPE (TREE_TYPE (fndecl)))
1328 && compparms (p1, p2)
1329 && (DECL_TEMPLATE_SPECIALIZATION (function)
1330 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1331 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1332 || (DECL_TI_TEMPLATE (function)
1333 == DECL_TI_TEMPLATE (fndecl))))
1334 return fndecl;
1337 break; /* loser */
1342 if (methods != end && *methods)
1344 tree fndecl = *methods;
1345 error ("prototype for `%#D' does not match any in class `%T'",
1346 function, ctype);
1347 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1348 OVL_CURRENT (fndecl));
1349 while (fndecl = OVL_NEXT (fndecl), fndecl)
1350 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1352 else
1354 methods = 0;
1355 if (!COMPLETE_TYPE_P (ctype))
1356 cxx_incomplete_type_error (function, ctype);
1357 else
1358 error ("no `%#D' member function declared in class `%T'",
1359 function, ctype);
1362 /* If we did not find the method in the class, add it to avoid
1363 spurious errors (unless the CTYPE is not yet defined, in which
1364 case we'll only confuse ourselves when the function is declared
1365 properly within the class. */
1366 if (COMPLETE_TYPE_P (ctype))
1367 add_method (ctype, function, /*error_p=*/1);
1368 return NULL_TREE;
1371 /* We have just processed the DECL, which is a static data member.
1372 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1373 present, is the assembly-language name for the data member.
1374 FLAGS is as for cp_finish_decl. */
1376 void
1377 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1378 tree decl;
1379 tree init;
1380 tree asmspec_tree;
1381 int flags;
1383 my_friendly_assert (TREE_PUBLIC (decl), 0);
1385 DECL_CONTEXT (decl) = current_class_type;
1387 /* We cannot call pushdecl here, because that would fill in the
1388 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1389 the right thing, namely, to put this decl out straight away. */
1390 /* current_class_type can be NULL_TREE in case of error. */
1391 if (!asmspec_tree && current_class_type)
1392 DECL_INITIAL (decl) = error_mark_node;
1394 if (! processing_template_decl)
1396 if (!pending_statics)
1397 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1398 VARRAY_PUSH_TREE (pending_statics, decl);
1401 if (LOCAL_CLASS_P (current_class_type))
1402 pedwarn ("local class `%#T' shall not have static data member `%#D'",
1403 current_class_type, decl);
1405 /* Static consts need not be initialized in the class definition. */
1406 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1408 static int explained = 0;
1410 error ("initializer invalid for static member with constructor");
1411 if (!explained)
1413 error ("(an out of class initialization is required)");
1414 explained = 1;
1416 init = NULL_TREE;
1418 /* Force the compiler to know when an uninitialized static const
1419 member is being used. */
1420 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1421 TREE_USED (decl) = 1;
1422 DECL_INITIAL (decl) = init;
1423 DECL_IN_AGGR_P (decl) = 1;
1425 cp_finish_decl (decl, init, asmspec_tree, flags);
1428 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1429 of a structure component, returning a _DECL node.
1430 QUALS is a list of type qualifiers for this decl (such as for declaring
1431 const member functions).
1433 This is done during the parsing of the struct declaration.
1434 The _DECL nodes are chained together and the lot of them
1435 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1437 If class A defines that certain functions in class B are friends, then
1438 the way I have set things up, it is B who is interested in permission
1439 granted by A. However, it is in A's context that these declarations
1440 are parsed. By returning a void_type_node, class A does not attempt
1441 to incorporate the declarations of the friends within its structure.
1443 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1444 CHANGES TO CODE IN `start_method'. */
1446 tree
1447 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1448 tree declarator, declspecs, init, asmspec_tree, attrlist;
1450 tree value;
1451 const char *asmspec = 0;
1452 int flags = LOOKUP_ONLYCONVERTING;
1454 /* Convert () initializers to = initializers. */
1455 if (init == NULL_TREE && declarator != NULL_TREE
1456 && TREE_CODE (declarator) == CALL_EXPR
1457 && TREE_OPERAND (declarator, 0)
1458 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1459 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1460 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1462 /* It's invalid to try to initialize a data member using a
1463 functional notation, e.g.:
1465 struct S {
1466 static int i (3);
1469 Explain that to the user. */
1470 static int explained;
1472 error ("invalid data member initialization");
1473 if (!explained)
1475 error ("(use `=' to initialize static data members)");
1476 explained = 1;
1479 declarator = TREE_OPERAND (declarator, 0);
1480 flags = 0;
1483 if (declspecs == NULL_TREE
1484 && TREE_CODE (declarator) == SCOPE_REF
1485 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1487 /* Access declaration */
1488 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1490 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1491 pop_nested_class ();
1492 return do_class_using_decl (declarator);
1495 if (init
1496 && TREE_CODE (init) == TREE_LIST
1497 && TREE_VALUE (init) == error_mark_node
1498 && TREE_CHAIN (init) == NULL_TREE)
1499 init = NULL_TREE;
1501 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
1502 if (! value || value == error_mark_node)
1503 /* friend or constructor went bad. */
1504 return value;
1505 if (TREE_TYPE (value) == error_mark_node)
1506 return error_mark_node;
1508 /* Pass friendly classes back. */
1509 if (TREE_CODE (value) == VOID_TYPE)
1510 return void_type_node;
1512 if (DECL_NAME (value) != NULL_TREE
1513 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1514 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1515 error ("member `%D' conflicts with virtual function table field name",
1516 value);
1518 /* Stash away type declarations. */
1519 if (TREE_CODE (value) == TYPE_DECL)
1521 DECL_NONLOCAL (value) = 1;
1522 DECL_CONTEXT (value) = current_class_type;
1524 if (CLASS_TYPE_P (TREE_TYPE (value)))
1525 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1527 if (processing_template_decl)
1528 value = push_template_decl (value);
1530 return value;
1533 if (DECL_IN_AGGR_P (value))
1535 error ("`%D' is already defined in `%T'", value,
1536 DECL_CONTEXT (value));
1537 return void_type_node;
1540 if (asmspec_tree)
1541 asmspec = TREE_STRING_POINTER (asmspec_tree);
1543 if (init)
1545 if (TREE_CODE (value) == FUNCTION_DECL)
1547 grok_function_init (value, init);
1548 init = NULL_TREE;
1550 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1551 /* Already complained in grokdeclarator. */
1552 init = NULL_TREE;
1553 else
1555 /* We allow initializers to become parameters to base
1556 initializers. */
1557 if (TREE_CODE (init) == TREE_LIST)
1559 if (TREE_CHAIN (init) == NULL_TREE)
1560 init = TREE_VALUE (init);
1561 else
1562 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1565 if (TREE_CODE (init) == CONST_DECL)
1566 init = DECL_INITIAL (init);
1567 else if (TREE_READONLY_DECL_P (init))
1568 init = decl_constant_value (init);
1569 else if (TREE_CODE (init) == CONSTRUCTOR)
1570 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1571 if (init == error_mark_node)
1572 /* We must make this look different than `error_mark_node'
1573 because `decl_const_value' would mis-interpret it
1574 as only meaning that this VAR_DECL is defined. */
1575 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1576 else if (processing_template_decl)
1578 else if (! TREE_CONSTANT (init))
1580 /* We can allow references to things that are effectively
1581 static, since references are initialized with the address. */
1582 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1583 || (TREE_STATIC (init) == 0
1584 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1586 error ("field initializer is not constant");
1587 init = error_mark_node;
1593 if (processing_template_decl && ! current_function_decl
1594 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1595 value = push_template_decl (value);
1597 if (attrlist)
1598 cplus_decl_attributes (&value, attrlist, 0);
1600 if (TREE_CODE (value) == VAR_DECL)
1602 finish_static_data_member_decl (value, init, asmspec_tree,
1603 flags);
1604 return value;
1606 if (TREE_CODE (value) == FIELD_DECL)
1608 if (asmspec)
1609 error ("`asm' specifiers are not permitted on non-static data members");
1610 if (DECL_INITIAL (value) == error_mark_node)
1611 init = error_mark_node;
1612 cp_finish_decl (value, init, NULL_TREE, flags);
1613 DECL_INITIAL (value) = init;
1614 DECL_IN_AGGR_P (value) = 1;
1615 return value;
1617 if (TREE_CODE (value) == FUNCTION_DECL)
1619 if (asmspec)
1621 /* This must override the asm specifier which was placed
1622 by grokclassfn. Lay this out fresh. */
1623 SET_DECL_RTL (value, NULL_RTX);
1624 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1626 if (!DECL_FRIEND_P (value))
1627 grok_special_member_properties (value);
1629 cp_finish_decl (value, init, asmspec_tree, flags);
1631 /* Pass friends back this way. */
1632 if (DECL_FRIEND_P (value))
1633 return void_type_node;
1635 DECL_IN_AGGR_P (value) = 1;
1636 return value;
1638 abort ();
1639 /* NOTREACHED */
1640 return NULL_TREE;
1643 /* Like `grokfield', but for bitfields.
1644 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1646 tree
1647 grokbitfield (declarator, declspecs, width)
1648 tree declarator, declspecs, width;
1650 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1651 0, NULL);
1653 if (! value) return NULL_TREE; /* friends went bad. */
1655 /* Pass friendly classes back. */
1656 if (TREE_CODE (value) == VOID_TYPE)
1657 return void_type_node;
1659 if (TREE_CODE (value) == TYPE_DECL)
1661 error ("cannot declare `%D' to be a bit-field type", value);
1662 return NULL_TREE;
1665 /* Usually, finish_struct_1 catches bitfields with invalid types.
1666 But, in the case of bitfields with function type, we confuse
1667 ourselves into thinking they are member functions, so we must
1668 check here. */
1669 if (TREE_CODE (value) == FUNCTION_DECL)
1671 error ("cannot declare bit-field `%D' with function type",
1672 DECL_NAME (value));
1673 return NULL_TREE;
1676 if (DECL_IN_AGGR_P (value))
1678 error ("`%D' is already defined in the class %T", value,
1679 DECL_CONTEXT (value));
1680 return void_type_node;
1683 if (TREE_STATIC (value))
1685 error ("static member `%D' cannot be a bit-field", value);
1686 return NULL_TREE;
1688 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1690 if (width != error_mark_node)
1692 constant_expression_warning (width);
1693 DECL_INITIAL (value) = width;
1694 SET_DECL_C_BIT_FIELD (value);
1697 DECL_IN_AGGR_P (value) = 1;
1698 return value;
1701 tree
1702 grokoptypename (declspecs, declarator)
1703 tree declspecs, declarator;
1705 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1706 return mangle_conv_op_name_for_type (t);
1709 /* When a function is declared with an initializer,
1710 do the right thing. Currently, there are two possibilities:
1712 class B
1714 public:
1715 // initialization possibility #1.
1716 virtual void f () = 0;
1717 int g ();
1720 class D1 : B
1722 public:
1723 int d1;
1724 // error, no f ();
1727 class D2 : B
1729 public:
1730 int d2;
1731 void f ();
1734 class D3 : B
1736 public:
1737 int d3;
1738 // initialization possibility #2
1739 void f () = B::f;
1744 static void
1745 grok_function_init (decl, init)
1746 tree decl;
1747 tree init;
1749 /* An initializer for a function tells how this function should
1750 be inherited. */
1751 tree type = TREE_TYPE (decl);
1753 if (TREE_CODE (type) == FUNCTION_TYPE)
1754 error ("initializer specified for non-member function `%D'", decl);
1755 else if (integer_zerop (init))
1756 DECL_PURE_VIRTUAL_P (decl) = 1;
1757 else
1758 error ("invalid initializer for virtual method `%D'", decl);
1761 void
1762 cplus_decl_attributes (decl, attributes, flags)
1763 tree *decl, attributes;
1764 int flags;
1766 if (*decl == NULL_TREE || *decl == void_type_node)
1767 return;
1769 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1770 decl = &DECL_TEMPLATE_RESULT (*decl);
1772 decl_attributes (decl, attributes, flags);
1774 if (TREE_CODE (*decl) == TYPE_DECL)
1775 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1778 /* CONSTRUCTOR_NAME:
1779 Return the name for the constructor (or destructor) for the
1780 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1781 IDENTIFIER_NODE. When given a template, this routine doesn't
1782 lose the specialization. */
1784 tree
1785 constructor_name_full (thing)
1786 tree thing;
1788 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1789 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1790 || TREE_CODE (thing) == TYPENAME_TYPE)
1791 thing = TYPE_NAME (thing);
1792 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1794 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1795 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1796 else
1797 thing = TYPE_NAME (thing);
1799 if (TREE_CODE (thing) == TYPE_DECL
1800 || (TREE_CODE (thing) == TEMPLATE_DECL
1801 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1802 thing = DECL_NAME (thing);
1803 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1804 return thing;
1807 /* CONSTRUCTOR_NAME:
1808 Return the name for the constructor (or destructor) for the
1809 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1810 IDENTIFIER_NODE. When given a template, return the plain
1811 unspecialized name. */
1813 tree
1814 constructor_name (thing)
1815 tree thing;
1817 tree t;
1818 thing = constructor_name_full (thing);
1819 t = IDENTIFIER_TEMPLATE (thing);
1820 if (!t)
1821 return thing;
1822 return t;
1825 /* Defer the compilation of the FN until the end of compilation. */
1827 void
1828 defer_fn (fn)
1829 tree fn;
1831 if (DECL_DEFERRED_FN (fn))
1832 return;
1833 DECL_DEFERRED_FN (fn) = 1;
1834 if (!deferred_fns)
1835 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1837 VARRAY_PUSH_TREE (deferred_fns, fn);
1840 /* Hunts through the global anonymous union ANON_DECL, building
1841 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1842 returns a VAR_DECL whose size is the same as the size of the
1843 ANON_DECL, if one is available.
1845 FIXME: we should really handle anonymous unions by binding the names
1846 of the members to COMPONENT_REFs rather than this kludge. */
1848 static tree
1849 build_anon_union_vars (anon_decl, elems, static_p, external_p)
1850 tree anon_decl;
1851 tree* elems;
1852 int static_p;
1853 int external_p;
1855 tree type = TREE_TYPE (anon_decl);
1856 tree main_decl = NULL_TREE;
1857 tree field;
1859 /* Rather than write the code to handle the non-union case,
1860 just give an error. */
1861 if (TREE_CODE (type) != UNION_TYPE)
1862 error ("anonymous struct not inside named type");
1864 for (field = TYPE_FIELDS (type);
1865 field != NULL_TREE;
1866 field = TREE_CHAIN (field))
1868 tree decl;
1870 if (DECL_ARTIFICIAL (field))
1871 continue;
1872 if (TREE_CODE (field) != FIELD_DECL)
1874 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
1875 field);
1876 continue;
1879 if (TREE_PRIVATE (field))
1880 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1881 else if (TREE_PROTECTED (field))
1882 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1884 if (DECL_NAME (field) == NULL_TREE
1885 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1887 decl = build_anon_union_vars (field, elems, static_p, external_p);
1888 if (!decl)
1889 continue;
1891 else if (DECL_NAME (field) == NULL_TREE)
1892 continue;
1893 else
1895 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1896 /* tell `pushdecl' that this is not tentative. */
1897 DECL_INITIAL (decl) = error_mark_node;
1898 TREE_PUBLIC (decl) = 0;
1899 TREE_STATIC (decl) = static_p;
1900 DECL_EXTERNAL (decl) = external_p;
1901 decl = pushdecl (decl);
1902 DECL_INITIAL (decl) = NULL_TREE;
1905 /* Only write out one anon union element--choose the largest
1906 one. We used to try to find one the same size as the union,
1907 but that fails if the ABI forces us to align the union more
1908 strictly. */
1909 if (main_decl == NULL_TREE
1910 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
1912 if (main_decl)
1913 TREE_ASM_WRITTEN (main_decl) = 1;
1914 main_decl = decl;
1916 else
1917 /* ??? This causes there to be no debug info written out
1918 about this decl. */
1919 TREE_ASM_WRITTEN (decl) = 1;
1921 if (DECL_NAME (field) == NULL_TREE
1922 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1923 /* The remainder of the processing was already done in the
1924 recursive call. */
1925 continue;
1927 /* If there's a cleanup to do, it belongs in the
1928 TREE_PURPOSE of the following TREE_LIST. */
1929 *elems = tree_cons (NULL_TREE, decl, *elems);
1930 TREE_TYPE (*elems) = type;
1933 return main_decl;
1936 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1937 anonymous union, then all members must be laid out together. PUBLIC_P
1938 is nonzero if this union is not declared static. */
1940 void
1941 finish_anon_union (anon_union_decl)
1942 tree anon_union_decl;
1944 tree type = TREE_TYPE (anon_union_decl);
1945 tree main_decl;
1946 int public_p = TREE_PUBLIC (anon_union_decl);
1947 int static_p = TREE_STATIC (anon_union_decl);
1948 int external_p = DECL_EXTERNAL (anon_union_decl);
1950 /* The VAR_DECL's context is the same as the TYPE's context. */
1951 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1953 if (TYPE_FIELDS (type) == NULL_TREE)
1954 return;
1956 if (public_p)
1958 error ("namespace-scope anonymous aggregates must be static");
1959 return;
1962 main_decl = build_anon_union_vars (anon_union_decl,
1963 &DECL_ANON_UNION_ELEMS (anon_union_decl),
1964 static_p, external_p);
1966 if (main_decl == NULL_TREE)
1968 warning ("anonymous aggregate with no members");
1969 return;
1972 if (static_p)
1974 make_decl_rtl (main_decl, 0);
1975 COPY_DECL_RTL (main_decl, anon_union_decl);
1976 expand_anon_union_decl (anon_union_decl,
1977 NULL_TREE,
1978 DECL_ANON_UNION_ELEMS (anon_union_decl));
1980 else
1981 add_decl_stmt (anon_union_decl);
1984 /* Finish processing a builtin type TYPE. It's name is NAME,
1985 its fields are in the array FIELDS. LEN is the number of elements
1986 in FIELDS minus one, or put another way, it is the maximum subscript
1987 used in FIELDS.
1989 It is given the same alignment as ALIGN_TYPE. */
1991 void
1992 finish_builtin_type (type, name, fields, len, align_type)
1993 tree type;
1994 const char *name;
1995 tree fields[];
1996 int len;
1997 tree align_type;
1999 register int i;
2001 TYPE_FIELDS (type) = fields[0];
2002 for (i = 0; i < len; i++)
2004 layout_type (TREE_TYPE (fields[i]));
2005 DECL_FIELD_CONTEXT (fields[i]) = type;
2006 TREE_CHAIN (fields[i]) = fields[i+1];
2008 DECL_FIELD_CONTEXT (fields[i]) = type;
2009 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2010 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2011 layout_type (type);
2012 #if 0 /* not yet, should get fixed properly later */
2013 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2014 #else
2015 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2016 #endif
2017 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2018 layout_decl (TYPE_NAME (type), 0);
2021 /* Auxiliary functions to make type signatures for
2022 `operator new' and `operator delete' correspond to
2023 what compiler will be expecting. */
2025 tree
2026 coerce_new_type (type)
2027 tree type;
2029 int e = 0;
2030 tree args = TYPE_ARG_TYPES (type);
2032 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2034 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2035 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
2037 if (!args || args == void_list_node
2038 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2040 e = 2;
2041 if (args && args != void_list_node)
2042 args = TREE_CHAIN (args);
2043 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2045 switch (e)
2047 case 2:
2048 args = tree_cons (NULL_TREE, c_size_type_node, args);
2049 /* FALLTHROUGH */
2050 case 1:
2051 type = build_exception_variant
2052 (build_function_type (ptr_type_node, args),
2053 TYPE_RAISES_EXCEPTIONS (type));
2054 /* FALLTHROUGH */
2055 default:;
2057 return type;
2060 tree
2061 coerce_delete_type (type)
2062 tree type;
2064 int e = 0;
2065 tree args = TYPE_ARG_TYPES (type);
2067 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2069 if (!same_type_p (TREE_TYPE (type), void_type_node))
2070 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
2072 if (!args || args == void_list_node
2073 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2075 e = 2;
2076 if (args && args != void_list_node)
2077 args = TREE_CHAIN (args);
2078 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2080 switch (e)
2082 case 2:
2083 args = tree_cons (NULL_TREE, ptr_type_node, args);
2084 /* FALLTHROUGH */
2085 case 1:
2086 type = build_exception_variant
2087 (build_function_type (void_type_node, args),
2088 TYPE_RAISES_EXCEPTIONS (type));
2089 /* FALLTHROUGH */
2090 default:;
2093 return type;
2096 static void
2097 mark_vtable_entries (decl)
2098 tree decl;
2100 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2102 for (; entries; entries = TREE_CHAIN (entries))
2104 tree fnaddr = TREE_VALUE (entries);
2105 tree fn;
2107 if (TREE_CODE (fnaddr) != ADDR_EXPR
2108 && TREE_CODE (fnaddr) != FDESC_EXPR)
2109 /* This entry is an offset: a virtual base class offset, a
2110 virtual call offset, an RTTI offset, etc. */
2111 continue;
2113 fn = TREE_OPERAND (fnaddr, 0);
2114 TREE_ADDRESSABLE (fn) = 1;
2115 /* When we don't have vcall offsets, we output thunks whenever
2116 we output the vtables that contain them. With vcall offsets,
2117 we know all the thunks we'll need when we emit a virtual
2118 function, so we emit the thunks there instead. */
2119 if (DECL_THUNK_P (fn))
2120 use_thunk (fn, /*emit_p=*/0);
2121 mark_used (fn);
2125 /* Set DECL up to have the closest approximation of "initialized common"
2126 linkage available. */
2128 void
2129 comdat_linkage (decl)
2130 tree decl;
2132 if (flag_weak)
2133 make_decl_one_only (decl);
2134 else if (TREE_CODE (decl) == FUNCTION_DECL
2135 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2136 /* We can just emit function and compiler-generated variables
2137 statically; having multiple copies is (for the most part) only
2138 a waste of space.
2140 There are two correctness issues, however: the address of a
2141 template instantiation with external linkage should be the
2142 same, independent of what translation unit asks for the
2143 address, and this will not hold when we emit multiple copies of
2144 the function. However, there's little else we can do.
2146 Also, by default, the typeinfo implementation assumes that
2147 there will be only one copy of the string used as the name for
2148 each type. Therefore, if weak symbols are unavailable, the
2149 run-time library should perform a more conservative check; it
2150 should perform a string comparison, rather than an address
2151 comparison. */
2152 TREE_PUBLIC (decl) = 0;
2153 else
2155 /* Static data member template instantiations, however, cannot
2156 have multiple copies. */
2157 if (DECL_INITIAL (decl) == 0
2158 || DECL_INITIAL (decl) == error_mark_node)
2159 DECL_COMMON (decl) = 1;
2160 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2162 DECL_COMMON (decl) = 1;
2163 DECL_INITIAL (decl) = error_mark_node;
2165 else
2167 /* We can't do anything useful; leave vars for explicit
2168 instantiation. */
2169 DECL_EXTERNAL (decl) = 1;
2170 DECL_NOT_REALLY_EXTERN (decl) = 0;
2174 if (DECL_LANG_SPECIFIC (decl))
2175 DECL_COMDAT (decl) = 1;
2178 /* For win32 we also want to put explicit instantiations in
2179 linkonce sections, so that they will be merged with implicit
2180 instantiations; otherwise we get duplicate symbol errors. */
2182 void
2183 maybe_make_one_only (decl)
2184 tree decl;
2186 /* We used to say that this was not necessary on targets that support weak
2187 symbols, because the implicit instantiations will defer to the explicit
2188 one. However, that's not actually the case in SVR4; a strong definition
2189 after a weak one is an error. Also, not making explicit
2190 instantiations one_only means that we can end up with two copies of
2191 some template instantiations. */
2192 if (! flag_weak)
2193 return;
2195 /* We can't set DECL_COMDAT on functions, or finish_file will think
2196 we can get away with not emitting them if they aren't used. We need
2197 to for variables so that cp_finish_decl will update their linkage,
2198 because their DECL_INITIAL may not have been set properly yet. */
2200 make_decl_one_only (decl);
2202 if (TREE_CODE (decl) == VAR_DECL)
2204 DECL_COMDAT (decl) = 1;
2205 /* Mark it needed so we don't forget to emit it. */
2206 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)) = 1;
2210 /* Returns the virtual function with which the vtable for TYPE is
2211 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2213 static tree
2214 key_method (type)
2215 tree type;
2217 tree method;
2219 if (TYPE_FOR_JAVA (type)
2220 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2221 || CLASSTYPE_INTERFACE_KNOWN (type))
2222 return NULL_TREE;
2224 for (method = TYPE_METHODS (type); method != NULL_TREE;
2225 method = TREE_CHAIN (method))
2226 if (DECL_VINDEX (method) != NULL_TREE
2227 && ! DECL_DECLARED_INLINE_P (method)
2228 && (! DECL_PURE_VIRTUAL_P (method)
2229 #if 0
2230 /* This would be nice, but we didn't think of it in time. */
2231 || DECL_DESTRUCTOR_P (method)
2232 #endif
2234 return method;
2236 return NULL_TREE;
2239 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2240 based on TYPE and other static flags.
2242 Note that anything public is tagged TREE_PUBLIC, whether
2243 it's public in this file or in another one. */
2245 void
2246 import_export_vtable (decl, type, final)
2247 tree decl, type;
2248 int final;
2250 if (DECL_INTERFACE_KNOWN (decl))
2251 return;
2253 if (TYPE_FOR_JAVA (type))
2255 TREE_PUBLIC (decl) = 1;
2256 DECL_EXTERNAL (decl) = 1;
2257 DECL_INTERFACE_KNOWN (decl) = 1;
2259 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2261 TREE_PUBLIC (decl) = 1;
2262 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2263 DECL_INTERFACE_KNOWN (decl) = 1;
2265 else
2267 /* We can only wait to decide if we have real non-inline virtual
2268 functions in our class, or if we come from a template. */
2270 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2271 || key_method (type));
2273 if (final || ! found)
2275 comdat_linkage (decl);
2276 DECL_EXTERNAL (decl) = 0;
2278 else
2280 TREE_PUBLIC (decl) = 1;
2281 DECL_EXTERNAL (decl) = 1;
2286 /* Determine whether or not we want to specifically import or export CTYPE,
2287 using various heuristics. */
2289 static void
2290 import_export_class (ctype)
2291 tree ctype;
2293 /* -1 for imported, 1 for exported. */
2294 int import_export = 0;
2296 /* It only makes sense to call this function at EOF. The reason is
2297 that this function looks at whether or not the first non-inline
2298 non-abstract virtual member function has been defined in this
2299 translation unit. But, we can't possibly know that until we've
2300 seen the entire translation unit. */
2301 my_friendly_assert (at_eof, 20000226);
2303 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2304 return;
2306 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2307 we will have CLASSTYPE_INTERFACE_ONLY set but not
2308 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2309 heuristic because someone will supply a #pragma implementation
2310 elsewhere, and deducing it here would produce a conflict. */
2311 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2312 return;
2314 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2315 import_export = -1;
2316 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2317 import_export = 1;
2319 /* If we got -fno-implicit-templates, we import template classes that
2320 weren't explicitly instantiated. */
2321 if (import_export == 0
2322 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2323 && ! flag_implicit_templates)
2324 import_export = -1;
2326 /* Base our import/export status on that of the first non-inline,
2327 non-pure virtual function, if any. */
2328 if (import_export == 0
2329 && TYPE_POLYMORPHIC_P (ctype))
2331 tree method = key_method (ctype);
2332 if (method)
2333 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2336 #ifdef MULTIPLE_SYMBOL_SPACES
2337 if (import_export == -1)
2338 import_export = 0;
2339 #endif
2341 if (import_export)
2343 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2344 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2348 /* We need to describe to the assembler the relationship between
2349 a vtable and the vtable of the parent class. */
2351 static void
2352 output_vtable_inherit (vars)
2353 tree vars;
2355 tree parent;
2356 rtx child_rtx, parent_rtx;
2358 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2360 parent = binfo_for_vtable (vars);
2362 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2363 parent_rtx = const0_rtx;
2364 else if (parent)
2366 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2367 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2369 else
2370 abort ();
2372 assemble_vtable_inherit (child_rtx, parent_rtx);
2375 static int
2376 finish_vtable_vardecl (t, data)
2377 tree *t;
2378 void *data ATTRIBUTE_UNUSED;
2380 tree vars = *t;
2381 tree ctype = DECL_CONTEXT (vars);
2382 import_export_class (ctype);
2383 import_export_vtable (vars, ctype, 1);
2385 if (! DECL_EXTERNAL (vars)
2386 && DECL_NEEDED_P (vars)
2387 && ! TREE_ASM_WRITTEN (vars))
2389 if (TREE_TYPE (vars) == void_type_node)
2390 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2391 return 0;
2393 /* Write it out. */
2394 mark_vtable_entries (vars);
2395 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2396 store_init_value (vars, DECL_INITIAL (vars));
2398 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2400 /* Mark the VAR_DECL node representing the vtable itself as a
2401 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2402 It is rather important that such things be ignored because
2403 any effort to actually generate DWARF for them will run
2404 into trouble when/if we encounter code like:
2406 #pragma interface
2407 struct S { virtual void member (); };
2409 because the artificial declaration of the vtable itself (as
2410 manufactured by the g++ front end) will say that the vtable
2411 is a static member of `S' but only *after* the debug output
2412 for the definition of `S' has already been output. This causes
2413 grief because the DWARF entry for the definition of the vtable
2414 will try to refer back to an earlier *declaration* of the
2415 vtable as a static member of `S' and there won't be one.
2416 We might be able to arrange to have the "vtable static member"
2417 attached to the member list for `S' before the debug info for
2418 `S' get written (which would solve the problem) but that would
2419 require more intrusive changes to the g++ front end. */
2421 DECL_IGNORED_P (vars) = 1;
2424 /* Always make vtables weak. */
2425 if (flag_weak)
2426 comdat_linkage (vars);
2428 rest_of_decl_compilation (vars, NULL, 1, 1);
2430 if (flag_vtable_gc)
2431 output_vtable_inherit (vars);
2433 /* Because we're only doing syntax-checking, we'll never end up
2434 actually marking the variable as written. */
2435 if (flag_syntax_only)
2436 TREE_ASM_WRITTEN (vars) = 1;
2438 /* Since we're writing out the vtable here, also write the debug
2439 info. */
2440 note_debug_info_needed (ctype);
2442 return 1;
2445 /* If the references to this class' vtables were optimized away, still
2446 emit the appropriate debugging information. See dfs_debug_mark. */
2447 if (DECL_COMDAT (vars)
2448 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2449 note_debug_info_needed (ctype);
2451 return 0;
2454 static int
2455 prune_vtable_vardecl (t, data)
2456 tree *t;
2457 void *data ATTRIBUTE_UNUSED;
2459 *t = TREE_CHAIN (*t);
2460 return 1;
2463 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2464 inline function or template instantiation at end-of-file. */
2466 void
2467 import_export_decl (decl)
2468 tree decl;
2470 if (DECL_INTERFACE_KNOWN (decl))
2471 return;
2473 if (DECL_TEMPLATE_INSTANTIATION (decl)
2474 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2476 DECL_NOT_REALLY_EXTERN (decl) = 1;
2477 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2478 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2479 && (flag_implicit_templates
2480 || (flag_implicit_inline_templates
2481 && DECL_DECLARED_INLINE_P (decl))))
2483 if (!TREE_PUBLIC (decl))
2484 /* Templates are allowed to have internal linkage. See
2485 [basic.link]. */
2487 else
2488 comdat_linkage (decl);
2490 else
2491 DECL_NOT_REALLY_EXTERN (decl) = 0;
2493 else if (DECL_FUNCTION_MEMBER_P (decl))
2495 if (!DECL_DECLARED_INLINE_P (decl))
2497 tree ctype = DECL_CONTEXT (decl);
2498 import_export_class (ctype);
2499 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2501 DECL_NOT_REALLY_EXTERN (decl)
2502 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2503 || (DECL_DECLARED_INLINE_P (decl)
2504 && ! flag_implement_inlines
2505 && !DECL_VINDEX (decl)));
2507 /* Always make artificials weak. */
2508 if (DECL_ARTIFICIAL (decl) && flag_weak)
2509 comdat_linkage (decl);
2510 else
2511 maybe_make_one_only (decl);
2514 else
2515 comdat_linkage (decl);
2517 else if (tinfo_decl_p (decl, 0))
2519 tree ctype = TREE_TYPE (DECL_NAME (decl));
2521 if (IS_AGGR_TYPE (ctype))
2522 import_export_class (ctype);
2524 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2525 && TYPE_POLYMORPHIC_P (ctype)
2526 /* If -fno-rtti, we're not necessarily emitting this stuff with
2527 the class, so go ahead and emit it now. This can happen
2528 when a class is used in exception handling. */
2529 && flag_rtti
2530 /* If the type is a cv-qualified variant of a type, then we
2531 must emit the tinfo function in this translation unit
2532 since it will not be emitted when the vtable for the type
2533 is output (which is when the unqualified version is
2534 generated). */
2535 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2537 DECL_NOT_REALLY_EXTERN (decl)
2538 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2539 || (DECL_DECLARED_INLINE_P (decl)
2540 && ! flag_implement_inlines
2541 && !DECL_VINDEX (decl)));
2543 /* Always make artificials weak. */
2544 if (flag_weak)
2545 comdat_linkage (decl);
2547 else if (TYPE_BUILT_IN (ctype)
2548 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2549 DECL_NOT_REALLY_EXTERN (decl) = 0;
2550 else
2551 comdat_linkage (decl);
2553 else
2554 comdat_linkage (decl);
2556 DECL_INTERFACE_KNOWN (decl) = 1;
2559 tree
2560 build_cleanup (decl)
2561 tree decl;
2563 tree temp;
2564 tree type = TREE_TYPE (decl);
2566 if (TREE_CODE (type) == ARRAY_TYPE)
2567 temp = decl;
2568 else
2570 cxx_mark_addressable (decl);
2571 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2573 temp = build_delete (TREE_TYPE (temp), temp,
2574 sfk_complete_destructor,
2575 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2576 return temp;
2579 /* Returns the initialization guard variable for the variable DECL,
2580 which has static storage duration. */
2582 tree
2583 get_guard (decl)
2584 tree decl;
2586 tree sname;
2587 tree guard;
2589 sname = mangle_guard_variable (decl);
2590 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2591 if (! guard)
2593 tree guard_type;
2595 /* We use a type that is big enough to contain a mutex as well
2596 as an integer counter. */
2597 guard_type = long_long_integer_type_node;
2598 guard = build_decl (VAR_DECL, sname, guard_type);
2600 /* The guard should have the same linkage as what it guards. */
2601 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2602 TREE_STATIC (guard) = TREE_STATIC (decl);
2603 DECL_COMMON (guard) = DECL_COMMON (decl);
2604 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2605 if (TREE_PUBLIC (decl))
2606 DECL_WEAK (guard) = DECL_WEAK (decl);
2608 DECL_ARTIFICIAL (guard) = 1;
2609 TREE_USED (guard) = 1;
2610 pushdecl_top_level (guard);
2611 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2613 return guard;
2616 /* Return those bits of the GUARD variable that should be set when the
2617 guarded entity is actually initialized. */
2619 static tree
2620 get_guard_bits (guard)
2621 tree guard;
2623 /* We only set the first byte of the guard, in order to leave room
2624 for a mutex in the high-order bits. */
2625 guard = build1 (ADDR_EXPR,
2626 build_pointer_type (TREE_TYPE (guard)),
2627 guard);
2628 guard = build1 (NOP_EXPR,
2629 build_pointer_type (char_type_node),
2630 guard);
2631 guard = build1 (INDIRECT_REF, char_type_node, guard);
2633 return guard;
2636 /* Return an expression which determines whether or not the GUARD
2637 variable has already been initialized. */
2639 tree
2640 get_guard_cond (guard)
2641 tree guard;
2643 tree guard_value;
2645 /* Check to see if the GUARD is zero. */
2646 guard = get_guard_bits (guard);
2647 guard_value = integer_zero_node;
2648 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2649 guard_value = convert (TREE_TYPE (guard), guard_value);
2650 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2653 /* Return an expression which sets the GUARD variable, indicating that
2654 the variable being guarded has been initialized. */
2656 tree
2657 set_guard (guard)
2658 tree guard;
2660 tree guard_init;
2662 /* Set the GUARD to one. */
2663 guard = get_guard_bits (guard);
2664 guard_init = integer_one_node;
2665 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2666 guard_init = convert (TREE_TYPE (guard), guard_init);
2667 return build_modify_expr (guard, NOP_EXPR, guard_init);
2670 /* Start the process of running a particular set of global constructors
2671 or destructors. Subroutine of do_[cd]tors. */
2673 static tree
2674 start_objects (method_type, initp)
2675 int method_type, initp;
2677 tree fnname;
2678 tree body;
2679 char type[10];
2681 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2683 if (initp != DEFAULT_INIT_PRIORITY)
2685 char joiner;
2687 #ifdef JOINER
2688 joiner = JOINER;
2689 #else
2690 joiner = '_';
2691 #endif
2693 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2695 else
2696 sprintf (type, "%c", method_type);
2698 fnname = get_file_function_name_long (type);
2700 start_function (void_list_node,
2701 make_call_declarator (fnname, void_list_node, NULL_TREE,
2702 NULL_TREE),
2703 NULL_TREE, SF_DEFAULT);
2705 /* It can be a static function as long as collect2 does not have
2706 to scan the object file to find its ctor/dtor routine. */
2707 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2709 /* Mark this declaration as used to avoid spurious warnings. */
2710 TREE_USED (current_function_decl) = 1;
2712 /* Mark this function as a global constructor or destructor. */
2713 if (method_type == 'I')
2714 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2715 else
2716 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2717 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2719 body = begin_compound_stmt (/*has_no_scope=*/0);
2721 /* We cannot allow these functions to be elided, even if they do not
2722 have external linkage. And, there's no point in deferring
2723 copmilation of thes functions; they're all going to have to be
2724 out anyhow. */
2725 current_function_cannot_inline
2726 = "static constructors and destructors cannot be inlined";
2728 return body;
2731 /* Finish the process of running a particular set of global constructors
2732 or destructors. Subroutine of do_[cd]tors. */
2734 static void
2735 finish_objects (method_type, initp, body)
2736 int method_type, initp;
2737 tree body;
2739 tree fn;
2741 /* Finish up. */
2742 finish_compound_stmt (/*has_no_scope=*/0, body);
2743 fn = finish_function (0);
2744 expand_body (fn);
2746 /* When only doing semantic analysis, and no RTL generation, we
2747 can't call functions that directly emit assembly code; there is
2748 no assembly file in which to put the code. */
2749 if (flag_syntax_only)
2750 return;
2752 if (targetm.have_ctors_dtors)
2754 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2755 if (method_type == 'I')
2756 (* targetm.asm_out.constructor) (fnsym, initp);
2757 else
2758 (* targetm.asm_out.destructor) (fnsym, initp);
2762 /* The names of the parameters to the function created to handle
2763 initializations and destructions for objects with static storage
2764 duration. */
2765 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2766 #define PRIORITY_IDENTIFIER "__priority"
2768 /* The name of the function we create to handle initializations and
2769 destructions for objects with static storage duration. */
2770 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2772 /* The declaration for the __INITIALIZE_P argument. */
2773 static tree initialize_p_decl;
2775 /* The declaration for the __PRIORITY argument. */
2776 static tree priority_decl;
2778 /* The declaration for the static storage duration function. */
2779 static tree ssdf_decl;
2781 /* All the static storage duration functions created in this
2782 translation unit. */
2783 static varray_type ssdf_decls;
2785 /* A map from priority levels to information about that priority
2786 level. There may be many such levels, so efficient lookup is
2787 important. */
2788 static splay_tree priority_info_map;
2790 /* Begins the generation of the function that will handle all
2791 initialization and destruction of objects with static storage
2792 duration. The function generated takes two parameters of type
2793 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2794 non-zero, it performs initializations. Otherwise, it performs
2795 destructions. It only performs those initializations or
2796 destructions with the indicated __PRIORITY. The generated function
2797 returns no value.
2799 It is assumed that this function will only be called once per
2800 translation unit. */
2802 static tree
2803 start_static_storage_duration_function ()
2805 static unsigned ssdf_number;
2807 tree parm_types;
2808 tree type;
2809 tree body;
2810 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2812 /* Create the identifier for this function. It will be of the form
2813 SSDF_IDENTIFIER_<number>. */
2814 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2815 if (ssdf_number == 0)
2817 /* Overflow occurred. That means there are at least 4 billion
2818 initialization functions. */
2819 sorry ("too many initialization functions required");
2820 abort ();
2823 /* Create the parameters. */
2824 parm_types = void_list_node;
2825 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2826 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2827 type = build_function_type (void_type_node, parm_types);
2829 /* Create the FUNCTION_DECL itself. */
2830 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2831 get_identifier (id),
2832 type);
2833 TREE_PUBLIC (ssdf_decl) = 0;
2834 DECL_ARTIFICIAL (ssdf_decl) = 1;
2836 /* Put this function in the list of functions to be called from the
2837 static constructors and destructors. */
2838 if (!ssdf_decls)
2840 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2842 /* Take this opportunity to initialize the map from priority
2843 numbers to information about that priority level. */
2844 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2845 /*delete_key_fn=*/0,
2846 /*delete_value_fn=*/
2847 (splay_tree_delete_value_fn) &free);
2849 /* We always need to generate functions for the
2850 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2851 priorities later, we'll be sure to find the
2852 DEFAULT_INIT_PRIORITY. */
2853 get_priority_info (DEFAULT_INIT_PRIORITY);
2856 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2858 /* Create the argument list. */
2859 initialize_p_decl = build_decl (PARM_DECL,
2860 get_identifier (INITIALIZE_P_IDENTIFIER),
2861 integer_type_node);
2862 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2863 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
2864 TREE_USED (initialize_p_decl) = 1;
2865 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
2866 integer_type_node);
2867 DECL_CONTEXT (priority_decl) = ssdf_decl;
2868 DECL_ARG_TYPE (priority_decl) = integer_type_node;
2869 TREE_USED (priority_decl) = 1;
2871 TREE_CHAIN (initialize_p_decl) = priority_decl;
2872 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2874 /* Put the function in the global scope. */
2875 pushdecl (ssdf_decl);
2877 /* Start the function itself. This is equivalent to declarating the
2878 function as:
2880 static void __ssdf (int __initialize_p, init __priority_p);
2882 It is static because we only need to call this function from the
2883 various constructor and destructor functions for this module. */
2884 start_function (/*specs=*/NULL_TREE,
2885 ssdf_decl,
2886 /*attrs=*/NULL_TREE,
2887 SF_PRE_PARSED);
2889 /* Set up the scope of the outermost block in the function. */
2890 body = begin_compound_stmt (/*has_no_scope=*/0);
2892 /* This function must not be deferred because we are depending on
2893 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2894 current_function_cannot_inline
2895 = "static storage duration functions cannot be inlined";
2897 return body;
2900 /* Finish the generation of the function which performs initialization
2901 and destruction of objects with static storage duration. After
2902 this point, no more such objects can be created. */
2904 static void
2905 finish_static_storage_duration_function (body)
2906 tree body;
2908 /* Close out the function. */
2909 finish_compound_stmt (/*has_no_scope=*/0, body);
2910 expand_body (finish_function (0));
2913 /* Return the information about the indicated PRIORITY level. If no
2914 code to handle this level has yet been generated, generate the
2915 appropriate prologue. */
2917 static priority_info
2918 get_priority_info (priority)
2919 int priority;
2921 priority_info pi;
2922 splay_tree_node n;
2924 n = splay_tree_lookup (priority_info_map,
2925 (splay_tree_key) priority);
2926 if (!n)
2928 /* Create a new priority information structure, and insert it
2929 into the map. */
2930 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2931 pi->initializations_p = 0;
2932 pi->destructions_p = 0;
2933 splay_tree_insert (priority_info_map,
2934 (splay_tree_key) priority,
2935 (splay_tree_value) pi);
2937 else
2938 pi = (priority_info) n->value;
2940 return pi;
2943 /* Set up to handle the initialization or destruction of DECL. If
2944 INITP is non-zero, we are initializing the variable. Otherwise, we
2945 are destroying it. */
2947 static tree
2948 start_static_initialization_or_destruction (decl, initp)
2949 tree decl;
2950 int initp;
2952 tree guard_if_stmt = NULL_TREE;
2953 int priority;
2954 tree cond;
2955 tree guard;
2956 tree init_cond;
2957 priority_info pi;
2959 /* Figure out the priority for this declaration. */
2960 priority = DECL_INIT_PRIORITY (decl);
2961 if (!priority)
2962 priority = DEFAULT_INIT_PRIORITY;
2964 /* Remember that we had an initialization or finalization at this
2965 priority. */
2966 pi = get_priority_info (priority);
2967 if (initp)
2968 pi->initializations_p = 1;
2969 else
2970 pi->destructions_p = 1;
2972 /* Trick the compiler into thinking we are at the file and line
2973 where DECL was declared so that error-messages make sense, and so
2974 that the debugger will show somewhat sensible file and line
2975 information. */
2976 input_filename = DECL_SOURCE_FILE (decl);
2977 lineno = DECL_SOURCE_LINE (decl);
2979 /* Because of:
2981 [class.access.spec]
2983 Access control for implicit calls to the constructors,
2984 the conversion functions, or the destructor called to
2985 create and destroy a static data member is performed as
2986 if these calls appeared in the scope of the member's
2987 class.
2989 we pretend we are in a static member function of the class of
2990 which the DECL is a member. */
2991 if (member_p (decl))
2993 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
2994 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
2997 /* Conditionalize this initialization on being in the right priority
2998 and being initializing/finalizing appropriately. */
2999 guard_if_stmt = begin_if_stmt ();
3000 cond = cp_build_binary_op (EQ_EXPR,
3001 priority_decl,
3002 build_int_2 (priority, 0));
3003 init_cond = initp ? integer_one_node : integer_zero_node;
3004 init_cond = cp_build_binary_op (EQ_EXPR,
3005 initialize_p_decl,
3006 init_cond);
3007 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3009 /* Assume we don't need a guard. */
3010 guard = NULL_TREE;
3011 /* We need a guard if this is an object with external linkage that
3012 might be initialized in more than one place. (For example, a
3013 static data member of a template, when the data member requires
3014 construction.) */
3015 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3016 || DECL_ONE_ONLY (decl)
3017 || DECL_WEAK (decl)))
3019 tree guard_cond;
3021 guard = get_guard (decl);
3023 /* When using __cxa_atexit, we just check the GUARD as we would
3024 for a local static. */
3025 if (flag_use_cxa_atexit)
3027 /* When using __cxa_atexit, we never try to destroy
3028 anything from a static destructor. */
3029 my_friendly_assert (initp, 20000629);
3030 guard_cond = get_guard_cond (guard);
3032 /* If we don't have __cxa_atexit, then we will be running
3033 destructors from .fini sections, or their equivalents. So,
3034 we need to know how many times we've tried to initialize this
3035 object. We do initializations only if the GUARD is zero,
3036 i.e., if we are the first to initialize the variable. We do
3037 destructions only if the GUARD is one, i.e., if we are the
3038 last to destroy the variable. */
3039 else if (initp)
3040 guard_cond
3041 = cp_build_binary_op (EQ_EXPR,
3042 build_unary_op (PREINCREMENT_EXPR,
3043 guard,
3044 /*noconvert=*/1),
3045 integer_one_node);
3046 else
3047 guard_cond
3048 = cp_build_binary_op (EQ_EXPR,
3049 build_unary_op (PREDECREMENT_EXPR,
3050 guard,
3051 /*noconvert=*/1),
3052 integer_zero_node);
3054 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3057 finish_if_stmt_cond (cond, guard_if_stmt);
3059 /* If we're using __cxa_atexit, we have not already set the GUARD,
3060 so we must do so now. */
3061 if (guard && initp && flag_use_cxa_atexit)
3062 finish_expr_stmt (set_guard (guard));
3064 return guard_if_stmt;
3067 /* We've just finished generating code to do an initialization or
3068 finalization. GUARD_IF_STMT is the if-statement we used to guard
3069 the initialization. */
3071 static void
3072 finish_static_initialization_or_destruction (guard_if_stmt)
3073 tree guard_if_stmt;
3075 finish_then_clause (guard_if_stmt);
3076 finish_if_stmt ();
3078 /* Now that we're done with DECL we don't need to pretend to be a
3079 member of its class any longer. */
3080 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3081 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3084 /* Generate code to do the static initialization of DECL. The
3085 initialization is INIT. If DECL may be initialized more than once
3086 in different object files, GUARD is the guard variable to
3087 check. PRIORITY is the priority for the initialization. */
3089 static void
3090 do_static_initialization (decl, init)
3091 tree decl;
3092 tree init;
3094 tree expr;
3095 tree guard_if_stmt;
3097 /* Set up for the initialization. */
3098 guard_if_stmt
3099 = start_static_initialization_or_destruction (decl,
3100 /*initp=*/1);
3102 /* Do the initialization itself. */
3103 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3104 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3105 expr = build_aggr_init (decl, init, 0);
3106 else
3108 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3109 TREE_SIDE_EFFECTS (expr) = 1;
3111 finish_expr_stmt (expr);
3113 /* If we're using __cxa_atexit, register a a function that calls the
3114 destructor for the object. */
3115 if (flag_use_cxa_atexit)
3116 register_dtor_fn (decl);
3118 /* Finsh up. */
3119 finish_static_initialization_or_destruction (guard_if_stmt);
3122 /* Generate code to do the static destruction of DECL. If DECL may be
3123 initialized more than once in different object files, GUARD is the
3124 guard variable to check. PRIORITY is the priority for the
3125 destruction. */
3127 static void
3128 do_static_destruction (decl)
3129 tree decl;
3131 tree guard_if_stmt;
3133 /* If we're using __cxa_atexit, then destructors are registered
3134 immediately after objects are initialized. */
3135 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3137 /* If we don't need a destructor, there's nothing to do. */
3138 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3139 return;
3141 /* Actually do the destruction. */
3142 guard_if_stmt = start_static_initialization_or_destruction (decl,
3143 /*initp=*/0);
3144 finish_expr_stmt (build_cleanup (decl));
3145 finish_static_initialization_or_destruction (guard_if_stmt);
3148 /* VARS is a list of variables with static storage duration which may
3149 need initialization and/or finalization. Remove those variables
3150 that don't really need to be initialized or finalized, and return
3151 the resulting list. The order in which the variables appear in
3152 VARS is in reverse order of the order in which they should actually
3153 be initialized. The list we return is in the unreversed order;
3154 i.e., the first variable should be initialized first. */
3156 static tree
3157 prune_vars_needing_no_initialization (vars)
3158 tree vars;
3160 tree var;
3161 tree result;
3163 for (var = vars, result = NULL_TREE;
3164 var;
3165 var = TREE_CHAIN (var))
3167 tree decl = TREE_VALUE (var);
3168 tree init = TREE_PURPOSE (var);
3170 /* Deal gracefully with error. */
3171 if (decl == error_mark_node)
3172 continue;
3174 /* The only things that can be initialized are variables. */
3175 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3177 /* If this object is not defined, we don't need to do anything
3178 here. */
3179 if (DECL_EXTERNAL (decl))
3180 continue;
3182 /* Also, if the initializer already contains errors, we can bail
3183 out now. */
3184 if (init && TREE_CODE (init) == TREE_LIST
3185 && value_member (error_mark_node, init))
3186 continue;
3188 /* This variable is going to need initialization and/or
3189 finalization, so we add it to the list. */
3190 result = tree_cons (init, decl, result);
3193 return result;
3196 /* Make sure we have told the back end about all the variables in
3197 VARS. */
3199 static void
3200 write_out_vars (vars)
3201 tree vars;
3203 tree v;
3205 for (v = vars; v; v = TREE_CHAIN (v))
3206 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3207 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3210 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3211 (otherwise) that will initialize all gobal objects with static
3212 storage duration having the indicated PRIORITY. */
3214 static void
3215 generate_ctor_or_dtor_function (constructor_p, priority)
3216 int constructor_p;
3217 int priority;
3219 char function_key;
3220 tree arguments;
3221 tree body;
3222 size_t i;
3224 /* We use `I' to indicate initialization and `D' to indicate
3225 destruction. */
3226 if (constructor_p)
3227 function_key = 'I';
3228 else
3229 function_key = 'D';
3231 /* Begin the function. */
3232 body = start_objects (function_key, priority);
3234 /* Call the static storage duration function with appropriate
3235 arguments. */
3236 for (i = 0; i < ssdf_decls->elements_used; ++i)
3238 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3239 NULL_TREE);
3240 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3241 arguments);
3242 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3243 arguments));
3246 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3247 calls to any functions marked with attributes indicating that
3248 they should be called at initialization- or destruction-time. */
3249 if (priority == DEFAULT_INIT_PRIORITY)
3251 tree fns;
3253 for (fns = constructor_p ? static_ctors : static_dtors;
3254 fns;
3255 fns = TREE_CHAIN (fns))
3256 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3259 /* Close out the function. */
3260 finish_objects (function_key, priority, body);
3263 /* Generate constructor and destructor functions for the priority
3264 indicated by N. */
3266 static int
3267 generate_ctor_and_dtor_functions_for_priority (n, data)
3268 splay_tree_node n;
3269 void *data ATTRIBUTE_UNUSED;
3271 int priority = (int) n->key;
3272 priority_info pi = (priority_info) n->value;
3274 /* Generate the functions themselves, but only if they are really
3275 needed. */
3276 if (pi->initializations_p
3277 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3278 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3279 priority);
3280 if (pi->destructions_p
3281 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3282 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3283 priority);
3285 /* Keep iterating. */
3286 return 0;
3289 /* This routine is called from the last rule in yyparse ().
3290 Its job is to create all the code needed to initialize and
3291 destroy the global aggregates. We do the destruction
3292 first, since that way we only need to reverse the decls once. */
3294 void
3295 finish_file ()
3297 tree vars;
3298 int reconsider;
3299 size_t i;
3301 at_eof = 1;
3303 /* Bad parse errors. Just forget about it. */
3304 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3305 return;
3307 /* Otherwise, GDB can get confused, because in only knows
3308 about source for LINENO-1 lines. */
3309 lineno -= 1;
3311 interface_unknown = 1;
3312 interface_only = 0;
3314 /* We now have to write out all the stuff we put off writing out.
3315 These include:
3317 o Template specializations that we have not yet instantiated,
3318 but which are needed.
3319 o Initialization and destruction for non-local objects with
3320 static storage duration. (Local objects with static storage
3321 duration are initialized when their scope is first entered,
3322 and are cleaned up via atexit.)
3323 o Virtual function tables.
3325 All of these may cause others to be needed. For example,
3326 instantiating one function may cause another to be needed, and
3327 generating the intiailzer for an object may cause templates to be
3328 instantiated, etc., etc. */
3330 timevar_push (TV_VARCONST);
3332 emit_support_tinfos ();
3336 reconsider = 0;
3338 /* If there are templates that we've put off instantiating, do
3339 them now. */
3340 instantiate_pending_templates ();
3342 /* Write out virtual tables as required. Note that writing out
3343 the virtual table for a template class may cause the
3344 instantiation of members of that class. */
3345 if (walk_globals (vtable_decl_p,
3346 finish_vtable_vardecl,
3347 /*data=*/0))
3348 reconsider = 1;
3350 /* Write out needed type info variables. Writing out one variable
3351 might cause others to be needed. */
3352 if (walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3353 reconsider = 1;
3355 /* The list of objects with static storage duration is built up
3356 in reverse order. We clear STATIC_AGGREGATES so that any new
3357 aggregates added during the initialization of these will be
3358 initialized in the correct order when we next come around the
3359 loop. */
3360 vars = prune_vars_needing_no_initialization (static_aggregates);
3361 static_aggregates = NULL_TREE;
3363 if (vars)
3365 tree v;
3367 /* We need to start a new initialization function each time
3368 through the loop. That's because we need to know which
3369 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3370 isn't computed until a function is finished, and written
3371 out. That's a deficiency in the back-end. When this is
3372 fixed, these initialization functions could all become
3373 inline, with resulting performance improvements. */
3374 tree ssdf_body = start_static_storage_duration_function ();
3376 /* Make sure the back end knows about all the variables. */
3377 write_out_vars (vars);
3379 /* First generate code to do all the initializations. */
3380 for (v = vars; v; v = TREE_CHAIN (v))
3381 do_static_initialization (TREE_VALUE (v),
3382 TREE_PURPOSE (v));
3384 /* Then, generate code to do all the destructions. Do these
3385 in reverse order so that the most recently constructed
3386 variable is the first destroyed. If we're using
3387 __cxa_atexit, then we don't need to do this; functions
3388 were registered at initialization time to destroy the
3389 local statics. */
3390 if (!flag_use_cxa_atexit)
3392 vars = nreverse (vars);
3393 for (v = vars; v; v = TREE_CHAIN (v))
3394 do_static_destruction (TREE_VALUE (v));
3396 else
3397 vars = NULL_TREE;
3399 /* Finish up the static storage duration function for this
3400 round. */
3401 finish_static_storage_duration_function (ssdf_body);
3403 /* All those initializations and finalizations might cause
3404 us to need more inline functions, more template
3405 instantiations, etc. */
3406 reconsider = 1;
3409 /* Go through the various inline functions, and see if any need
3410 synthesizing. */
3411 for (i = 0; i < deferred_fns_used; ++i)
3413 tree decl = VARRAY_TREE (deferred_fns, i);
3414 import_export_decl (decl);
3415 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3416 && TREE_USED (decl)
3417 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3419 /* Even though we're already at the top-level, we push
3420 there again. That way, when we pop back a few lines
3421 hence, all of our state is restored. Otherwise,
3422 finish_function doesn't clean things up, and we end
3423 up with CURRENT_FUNCTION_DECL set. */
3424 push_to_top_level ();
3425 synthesize_method (decl);
3426 pop_from_top_level ();
3427 reconsider = 1;
3431 /* We lie to the back-end, pretending that some functions are
3432 not defined when they really are. This keeps these functions
3433 from being put out unnecessarily. But, we must stop lying
3434 when the functions are referenced, or if they are not comdat
3435 since they need to be put out now.
3436 This is done in a separate for cycle, because if some deferred
3437 function is contained in another deferred function later in
3438 deferred_fns varray, rest_of_compilation would skip this
3439 function and we really cannot expand the same function twice. */
3440 for (i = 0; i < deferred_fns_used; ++i)
3442 tree decl = VARRAY_TREE (deferred_fns, i);
3444 if (DECL_NOT_REALLY_EXTERN (decl)
3445 && DECL_INITIAL (decl)
3446 && DECL_NEEDED_P (decl))
3447 DECL_EXTERNAL (decl) = 0;
3450 for (i = 0; i < deferred_fns_used; ++i)
3452 tree decl = VARRAY_TREE (deferred_fns, i);
3454 /* If we're going to need to write this function out, and
3455 there's already a body for it, create RTL for it now.
3456 (There might be no body if this is a method we haven't
3457 gotten around to synthesizing yet.) */
3458 if (!DECL_EXTERNAL (decl)
3459 && DECL_NEEDED_P (decl)
3460 && DECL_SAVED_TREE (decl)
3461 && !TREE_ASM_WRITTEN (decl))
3463 int saved_not_really_extern;
3465 /* When we call finish_function in expand_body, it will
3466 try to reset DECL_NOT_REALLY_EXTERN so we save and
3467 restore it here. */
3468 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3469 /* Generate RTL for this function now that we know we
3470 need it. */
3471 expand_body (decl);
3472 /* Undo the damage done by finish_function. */
3473 DECL_EXTERNAL (decl) = 0;
3474 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3475 /* If we're compiling -fsyntax-only pretend that this
3476 function has been written out so that we don't try to
3477 expand it again. */
3478 if (flag_syntax_only)
3479 TREE_ASM_WRITTEN (decl) = 1;
3480 reconsider = 1;
3484 if (deferred_fns_used
3485 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3486 deferred_fns_used))
3487 reconsider = 1;
3488 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3489 reconsider = 1;
3491 /* Static data members are just like namespace-scope globals. */
3492 for (i = 0; i < pending_statics_used; ++i)
3494 tree decl = VARRAY_TREE (pending_statics, i);
3495 if (TREE_ASM_WRITTEN (decl))
3496 continue;
3497 import_export_decl (decl);
3498 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3499 DECL_EXTERNAL (decl) = 0;
3501 if (pending_statics
3502 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3503 pending_statics_used))
3504 reconsider = 1;
3506 while (reconsider);
3508 /* We give C linkage to static constructors and destructors. */
3509 push_lang_context (lang_name_c);
3511 /* Generate initialization and destruction functions for all
3512 priorities for which they are required. */
3513 if (priority_info_map)
3514 splay_tree_foreach (priority_info_map,
3515 generate_ctor_and_dtor_functions_for_priority,
3516 /*data=*/0);
3518 /* We're done with the splay-tree now. */
3519 if (priority_info_map)
3520 splay_tree_delete (priority_info_map);
3522 /* We're done with static constructors, so we can go back to "C++"
3523 linkage now. */
3524 pop_lang_context ();
3526 /* Now delete from the chain of variables all virtual function tables.
3527 We output them all ourselves, because each will be treated
3528 specially. We don't do this if we're just doing semantic
3529 analysis, and not code-generation. */
3530 if (!flag_syntax_only)
3531 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3533 /* Now, issue warnings about static, but not defined, functions,
3534 etc., and emit debugging information. */
3535 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3536 if (pending_statics)
3537 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3538 pending_statics_used);
3540 finish_repo ();
3542 /* The entire file is now complete. If requested, dump everything
3543 to a file. */
3545 int flags;
3546 FILE *stream = dump_begin (TDI_all, &flags);
3548 if (stream)
3550 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3551 dump_end (TDI_all, stream);
3555 timevar_pop (TV_VARCONST);
3557 if (flag_detailed_statistics)
3559 dump_tree_statistics ();
3560 dump_time_statistics ();
3564 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3565 expr. Since it was parsed like a type, we need to wade through and fix
3566 that. Unfortunately, since operator() is left-associative, we can't use
3567 tail recursion. In the above example, TYPE is `A', and DECL is
3568 `()()()()()'.
3570 Maybe this shouldn't be recursive, but how often will it actually be
3571 used? (jason) */
3573 tree
3574 reparse_absdcl_as_expr (type, decl)
3575 tree type, decl;
3577 /* do build_functional_cast (type, NULL_TREE) at bottom */
3578 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3579 return build_functional_cast (type, NULL_TREE);
3581 /* recurse */
3582 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3584 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3586 if (TREE_CODE (decl) == CALL_EXPR
3587 && (! TREE_TYPE (decl)
3588 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3589 decl = require_complete_type (decl);
3591 return decl;
3594 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3595 out to be an expr. Since it was parsed like a type, we need to wade
3596 through and fix that. Since casts are right-associative, we are
3597 reversing the order, so we don't have to recurse.
3599 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3600 `1'. */
3602 tree
3603 reparse_absdcl_as_casts (decl, expr)
3604 tree decl, expr;
3606 tree type;
3607 int non_void_p = 0;
3609 if (TREE_CODE (expr) == CONSTRUCTOR
3610 && TREE_TYPE (expr) == 0)
3612 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3613 decl = TREE_OPERAND (decl, 0);
3615 if (processing_template_decl)
3616 TREE_TYPE (expr) = type;
3617 else
3619 expr = digest_init (type, expr, (tree *) 0);
3620 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3622 int failure = complete_array_type (type, expr, 1);
3623 my_friendly_assert (!failure, 78);
3628 while (decl)
3630 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3631 decl = TREE_OPERAND (decl, 0);
3632 if (!VOID_TYPE_P (type))
3633 non_void_p = 1;
3634 expr = build_c_cast (type, expr);
3637 if (warn_old_style_cast && ! in_system_header
3638 && non_void_p && current_lang_name != lang_name_c)
3639 warning ("use of old-style cast");
3641 return expr;
3644 /* Given plain tree nodes for an expression, build up the full semantics. */
3646 tree
3647 build_expr_from_tree (t)
3648 tree t;
3650 if (t == NULL_TREE || t == error_mark_node)
3651 return t;
3653 switch (TREE_CODE (t))
3655 case IDENTIFIER_NODE:
3656 return do_identifier (t, 0, NULL_TREE);
3658 case LOOKUP_EXPR:
3659 if (LOOKUP_EXPR_GLOBAL (t))
3660 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3661 else
3662 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3664 case TEMPLATE_ID_EXPR:
3665 return (lookup_template_function
3666 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3667 build_expr_from_tree (TREE_OPERAND (t, 1))));
3669 case INDIRECT_REF:
3670 return build_x_indirect_ref
3671 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3673 case CAST_EXPR:
3674 return build_functional_cast
3675 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3677 case REINTERPRET_CAST_EXPR:
3678 return build_reinterpret_cast
3679 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3681 case CONST_CAST_EXPR:
3682 return build_const_cast
3683 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3685 case DYNAMIC_CAST_EXPR:
3686 return build_dynamic_cast
3687 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3689 case STATIC_CAST_EXPR:
3690 return build_static_cast
3691 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3693 case PREDECREMENT_EXPR:
3694 case PREINCREMENT_EXPR:
3695 case POSTDECREMENT_EXPR:
3696 case POSTINCREMENT_EXPR:
3697 case NEGATE_EXPR:
3698 case BIT_NOT_EXPR:
3699 case ABS_EXPR:
3700 case TRUTH_NOT_EXPR:
3701 case ADDR_EXPR:
3702 case CONVERT_EXPR: /* Unary + */
3703 case REALPART_EXPR:
3704 case IMAGPART_EXPR:
3705 if (TREE_TYPE (t))
3706 return t;
3707 return build_x_unary_op (TREE_CODE (t),
3708 build_expr_from_tree (TREE_OPERAND (t, 0)));
3710 case PLUS_EXPR:
3711 case MINUS_EXPR:
3712 case MULT_EXPR:
3713 case TRUNC_DIV_EXPR:
3714 case CEIL_DIV_EXPR:
3715 case FLOOR_DIV_EXPR:
3716 case ROUND_DIV_EXPR:
3717 case EXACT_DIV_EXPR:
3718 case BIT_AND_EXPR:
3719 case BIT_ANDTC_EXPR:
3720 case BIT_IOR_EXPR:
3721 case BIT_XOR_EXPR:
3722 case TRUNC_MOD_EXPR:
3723 case FLOOR_MOD_EXPR:
3724 case TRUTH_ANDIF_EXPR:
3725 case TRUTH_ORIF_EXPR:
3726 case TRUTH_AND_EXPR:
3727 case TRUTH_OR_EXPR:
3728 case RSHIFT_EXPR:
3729 case LSHIFT_EXPR:
3730 case RROTATE_EXPR:
3731 case LROTATE_EXPR:
3732 case EQ_EXPR:
3733 case NE_EXPR:
3734 case MAX_EXPR:
3735 case MIN_EXPR:
3736 case LE_EXPR:
3737 case GE_EXPR:
3738 case LT_EXPR:
3739 case GT_EXPR:
3740 case MEMBER_REF:
3741 return build_x_binary_op
3742 (TREE_CODE (t),
3743 build_expr_from_tree (TREE_OPERAND (t, 0)),
3744 build_expr_from_tree (TREE_OPERAND (t, 1)));
3746 case DOTSTAR_EXPR:
3747 return build_m_component_ref
3748 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3749 build_expr_from_tree (TREE_OPERAND (t, 1)));
3751 case SCOPE_REF:
3752 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3754 case ARRAY_REF:
3755 if (TREE_OPERAND (t, 0) == NULL_TREE)
3756 /* new-type-id */
3757 return build_nt (ARRAY_REF, NULL_TREE,
3758 build_expr_from_tree (TREE_OPERAND (t, 1)));
3759 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3760 build_expr_from_tree (TREE_OPERAND (t, 1)));
3762 case SIZEOF_EXPR:
3763 case ALIGNOF_EXPR:
3765 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3766 if (!TYPE_P (r))
3767 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3768 else
3769 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3772 case MODOP_EXPR:
3773 return build_x_modify_expr
3774 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3775 TREE_CODE (TREE_OPERAND (t, 1)),
3776 build_expr_from_tree (TREE_OPERAND (t, 2)));
3778 case ARROW_EXPR:
3779 return build_x_arrow
3780 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3782 case NEW_EXPR:
3783 return build_new
3784 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3785 build_expr_from_tree (TREE_OPERAND (t, 1)),
3786 build_expr_from_tree (TREE_OPERAND (t, 2)),
3787 NEW_EXPR_USE_GLOBAL (t));
3789 case DELETE_EXPR:
3790 return delete_sanity
3791 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3792 build_expr_from_tree (TREE_OPERAND (t, 1)),
3793 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3795 case COMPOUND_EXPR:
3796 if (TREE_OPERAND (t, 1) == NULL_TREE)
3797 return build_x_compound_expr
3798 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3799 else
3800 abort ();
3802 case METHOD_CALL_EXPR:
3803 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3805 tree ref = TREE_OPERAND (t, 0);
3806 tree name = TREE_OPERAND (ref, 1);
3808 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3809 name = build_nt (TEMPLATE_ID_EXPR,
3810 TREE_OPERAND (name, 0),
3811 build_expr_from_tree (TREE_OPERAND (name, 1)));
3813 return build_scoped_method_call
3814 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3815 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3816 name,
3817 build_expr_from_tree (TREE_OPERAND (t, 2)));
3819 else
3821 tree fn = TREE_OPERAND (t, 0);
3823 /* We can get a TEMPLATE_ID_EXPR here on code like:
3825 x->f<2>();
3827 so we must resolve that. However, we can also get things
3828 like a BIT_NOT_EXPR here, when referring to a destructor,
3829 and things like that are not correctly resolved by
3830 build_expr_from_tree. So, just use build_expr_from_tree
3831 when we really need it. */
3832 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3833 fn = lookup_template_function
3834 (TREE_OPERAND (fn, 0),
3835 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3837 return build_method_call
3838 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3840 build_expr_from_tree (TREE_OPERAND (t, 2)),
3841 NULL_TREE, LOOKUP_NORMAL);
3844 case CALL_EXPR:
3845 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3847 tree ref = TREE_OPERAND (t, 0);
3848 tree name = TREE_OPERAND (ref, 1);
3850 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3851 name = build_nt (TEMPLATE_ID_EXPR,
3852 TREE_OPERAND (name, 0),
3853 build_expr_from_tree (TREE_OPERAND (name, 1)));
3855 return build_member_call
3856 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3857 name,
3858 build_expr_from_tree (TREE_OPERAND (t, 1)));
3860 else
3862 tree name = TREE_OPERAND (t, 0);
3863 tree id;
3864 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3865 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3866 && !LOOKUP_EXPR_GLOBAL (name)
3867 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3868 && (!current_class_type
3869 || !lookup_member (current_class_type, id, 0, 0)))
3871 /* Do Koenig lookup if there are no class members. */
3872 name = do_identifier (id, 0, args);
3874 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3875 || ! really_overloaded_fn (name))
3876 name = build_expr_from_tree (name);
3877 return build_x_function_call (name, args, current_class_ref);
3880 case COND_EXPR:
3881 return build_x_conditional_expr
3882 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3883 build_expr_from_tree (TREE_OPERAND (t, 1)),
3884 build_expr_from_tree (TREE_OPERAND (t, 2)));
3886 case PSEUDO_DTOR_EXPR:
3887 return (finish_pseudo_destructor_call_expr
3888 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3889 build_expr_from_tree (TREE_OPERAND (t, 1)),
3890 build_expr_from_tree (TREE_OPERAND (t, 2))));
3892 case TREE_LIST:
3894 tree purpose, value, chain;
3896 if (t == void_list_node)
3897 return t;
3899 purpose = TREE_PURPOSE (t);
3900 if (purpose)
3901 purpose = build_expr_from_tree (purpose);
3902 value = TREE_VALUE (t);
3903 if (value)
3904 value = build_expr_from_tree (value);
3905 chain = TREE_CHAIN (t);
3906 if (chain && chain != void_type_node)
3907 chain = build_expr_from_tree (chain);
3908 return tree_cons (purpose, value, chain);
3911 case COMPONENT_REF:
3913 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3914 tree field = TREE_OPERAND (t, 1);
3916 /* We use a COMPONENT_REF to indicate things of the form `x.b'
3917 and `x.A::b'. We must distinguish between those cases
3918 here. */
3919 if (TREE_CODE (field) == SCOPE_REF)
3920 return build_object_ref (object,
3921 TREE_OPERAND (field, 0),
3922 TREE_OPERAND (field, 1));
3923 else
3924 return build_x_component_ref (object, field,
3925 NULL_TREE, 1);
3928 case THROW_EXPR:
3929 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3931 case CONSTRUCTOR:
3933 tree r;
3934 tree elts;
3935 tree type = TREE_TYPE (t);
3936 bool purpose_p;
3938 /* digest_init will do the wrong thing if we let it. */
3939 if (type && TYPE_PTRMEMFUNC_P (type))
3940 return t;
3942 r = NULL_TREE;
3943 /* We do not want to process the purpose of aggregate
3944 initializers as they are identifier nodes which will be
3945 looked up by digest_init. */
3946 purpose_p = !(type && IS_AGGR_TYPE (type));
3947 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3949 tree purpose = TREE_PURPOSE (elts);
3950 tree value = TREE_VALUE (elts);
3952 if (purpose && purpose_p)
3953 purpose = build_expr_from_tree (purpose);
3954 value = build_expr_from_tree (value);
3955 r = tree_cons (purpose, value, r);
3958 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3959 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3961 if (type)
3962 return digest_init (type, r, 0);
3963 return r;
3966 case TYPEID_EXPR:
3967 if (TYPE_P (TREE_OPERAND (t, 0)))
3968 return get_typeid (TREE_OPERAND (t, 0));
3969 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3971 case VAR_DECL:
3972 return convert_from_reference (t);
3974 case VA_ARG_EXPR:
3975 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3976 TREE_TYPE (t));
3978 default:
3979 return t;
3983 /* This is something of the form `int (*a)++' that has turned out to be an
3984 expr. It was only converted into parse nodes, so we need to go through
3985 and build up the semantics. Most of the work is done by
3986 build_expr_from_tree, above.
3988 In the above example, TYPE is `int' and DECL is `*a'. */
3990 tree
3991 reparse_decl_as_expr (type, decl)
3992 tree type, decl;
3994 decl = build_expr_from_tree (decl);
3995 if (type)
3996 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
3997 else
3998 return decl;
4001 /* This is something of the form `int (*a)' that has turned out to be a
4002 decl. It was only converted into parse nodes, so we need to do the
4003 checking that make_{pointer,reference}_declarator do. */
4005 tree
4006 finish_decl_parsing (decl)
4007 tree decl;
4009 switch (TREE_CODE (decl))
4011 case IDENTIFIER_NODE:
4012 return decl;
4013 case INDIRECT_REF:
4014 return make_pointer_declarator
4015 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4016 case ADDR_EXPR:
4017 return make_reference_declarator
4018 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4019 case BIT_NOT_EXPR:
4020 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4021 return decl;
4022 case SCOPE_REF:
4023 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4024 TREE_COMPLEXITY (decl) = current_class_depth;
4025 return decl;
4026 case ARRAY_REF:
4027 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4028 return decl;
4029 case TREE_LIST:
4030 /* For attribute handling. */
4031 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4032 return decl;
4033 case TEMPLATE_ID_EXPR:
4034 return decl;
4035 default:
4036 abort ();
4037 return NULL_TREE;
4041 /* Return 1 if root encloses child. */
4043 static int
4044 is_namespace_ancestor (root, child)
4045 tree root, child;
4047 if (root == child)
4048 return 1;
4049 if (root == global_namespace)
4050 return 1;
4051 if (child == global_namespace)
4052 return 0;
4053 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4057 /* Return the namespace that is the common ancestor
4058 of two given namespaces. */
4060 tree
4061 namespace_ancestor (ns1, ns2)
4062 tree ns1, ns2;
4064 if (is_namespace_ancestor (ns1, ns2))
4065 return ns1;
4066 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4069 /* Insert used into the using list of user. Set indirect_flag if this
4070 directive is not directly from the source. Also find the common
4071 ancestor and let our users know about the new namespace */
4072 static void
4073 add_using_namespace (user, used, indirect)
4074 tree user;
4075 tree used;
4076 int indirect;
4078 tree t;
4079 /* Using oneself is a no-op. */
4080 if (user == used)
4081 return;
4082 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4083 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4084 /* Check if we already have this. */
4085 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4086 if (t != NULL_TREE)
4088 if (!indirect)
4089 /* Promote to direct usage. */
4090 TREE_INDIRECT_USING (t) = 0;
4091 return;
4094 /* Add used to the user's using list. */
4095 DECL_NAMESPACE_USING (user)
4096 = tree_cons (used, namespace_ancestor (user, used),
4097 DECL_NAMESPACE_USING (user));
4099 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4101 /* Add user to the used's users list. */
4102 DECL_NAMESPACE_USERS (used)
4103 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4105 /* Recursively add all namespaces used. */
4106 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4107 /* indirect usage */
4108 add_using_namespace (user, TREE_PURPOSE (t), 1);
4110 /* Tell everyone using us about the new used namespaces. */
4111 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4112 add_using_namespace (TREE_PURPOSE (t), used, 1);
4115 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4116 duplicates. The first list becomes the tail of the result.
4118 The algorithm is O(n^2). We could get this down to O(n log n) by
4119 doing a sort on the addresses of the functions, if that becomes
4120 necessary. */
4122 static tree
4123 merge_functions (s1, s2)
4124 tree s1;
4125 tree s2;
4127 for (; s2; s2 = OVL_NEXT (s2))
4129 tree fn2 = OVL_CURRENT (s2);
4130 tree fns1;
4132 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4134 tree fn1 = OVL_CURRENT (fns1);
4136 /* If the function from S2 is already in S1, there is no
4137 need to add it again. For `extern "C"' functions, we
4138 might have two FUNCTION_DECLs for the same function, in
4139 different namespaces; again, we only need one of them. */
4140 if (fn1 == fn2
4141 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
4142 && DECL_NAME (fn1) == DECL_NAME (fn2)))
4143 break;
4146 /* If we exhausted all of the functions in S1, FN2 is new. */
4147 if (!fns1)
4148 s1 = build_overload (fn2, s1);
4150 return s1;
4153 /* This should return an error not all definitions define functions.
4154 It is not an error if we find two functions with exactly the
4155 same signature, only if these are selected in overload resolution.
4156 old is the current set of bindings, new the freshly-found binding.
4157 XXX Do we want to give *all* candidates in case of ambiguity?
4158 XXX In what way should I treat extern declarations?
4159 XXX I don't want to repeat the entire duplicate_decls here */
4161 static tree
4162 ambiguous_decl (name, old, new, flags)
4163 tree name;
4164 tree old;
4165 tree new;
4166 int flags;
4168 tree val, type;
4169 my_friendly_assert (old != NULL_TREE, 393);
4170 /* Copy the value. */
4171 val = BINDING_VALUE (new);
4172 if (val)
4173 switch (TREE_CODE (val))
4175 case TEMPLATE_DECL:
4176 /* If we expect types or namespaces, and not templates,
4177 or this is not a template class. */
4178 if (LOOKUP_QUALIFIERS_ONLY (flags)
4179 && !DECL_CLASS_TEMPLATE_P (val))
4180 val = NULL_TREE;
4181 break;
4182 case TYPE_DECL:
4183 if (LOOKUP_NAMESPACES_ONLY (flags))
4184 val = NULL_TREE;
4185 break;
4186 case NAMESPACE_DECL:
4187 if (LOOKUP_TYPES_ONLY (flags))
4188 val = NULL_TREE;
4189 break;
4190 case FUNCTION_DECL:
4191 /* Ignore built-in functions that are still anticipated. */
4192 if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
4193 val = NULL_TREE;
4194 break;
4195 default:
4196 if (LOOKUP_QUALIFIERS_ONLY (flags))
4197 val = NULL_TREE;
4200 if (!BINDING_VALUE (old))
4201 BINDING_VALUE (old) = val;
4202 else if (val && val != BINDING_VALUE (old))
4204 if (is_overloaded_fn (BINDING_VALUE (old))
4205 && is_overloaded_fn (val))
4207 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4208 val);
4210 else
4212 /* Some declarations are functions, some are not. */
4213 if (flags & LOOKUP_COMPLAIN)
4215 /* If we've already given this error for this lookup,
4216 BINDING_VALUE (old) is error_mark_node, so let's not
4217 repeat ourselves. */
4218 if (BINDING_VALUE (old) != error_mark_node)
4220 error ("use of `%D' is ambiguous", name);
4221 cp_error_at (" first declared as `%#D' here",
4222 BINDING_VALUE (old));
4224 cp_error_at (" also declared as `%#D' here", val);
4226 BINDING_VALUE (old) = error_mark_node;
4229 /* ... and copy the type. */
4230 type = BINDING_TYPE (new);
4231 if (LOOKUP_NAMESPACES_ONLY (flags))
4232 type = NULL_TREE;
4233 if (!BINDING_TYPE (old))
4234 BINDING_TYPE (old) = type;
4235 else if (type && BINDING_TYPE (old) != type)
4237 if (flags & LOOKUP_COMPLAIN)
4239 error ("`%D' denotes an ambiguous type",name);
4240 cp_error_at (" first type here", BINDING_TYPE (old));
4241 cp_error_at (" other type here", type);
4244 return old;
4247 /* Subroutine of unualified_namespace_lookup:
4248 Add the bindings of NAME in used namespaces to VAL.
4249 We are currently looking for names in namespace SCOPE, so we
4250 look through USINGS for using-directives of namespaces
4251 which have SCOPE as a common ancestor with the current scope.
4252 Returns zero on errors. */
4255 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4256 tree name, val, usings, scope;
4257 int flags;
4258 tree *spacesp;
4260 tree iter;
4261 tree val1;
4262 /* Iterate over all used namespaces in current, searching for using
4263 directives of scope. */
4264 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4265 if (TREE_VALUE (iter) == scope)
4267 if (spacesp)
4268 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4269 *spacesp);
4270 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4271 /* Resolve ambiguities. */
4272 val = ambiguous_decl (name, val, val1, flags);
4274 return BINDING_VALUE (val) != error_mark_node;
4277 /* [namespace.qual]
4278 Accepts the NAME to lookup and its qualifying SCOPE.
4279 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4280 or 0 on error. */
4283 qualified_lookup_using_namespace (name, scope, result, flags)
4284 tree name;
4285 tree scope;
4286 tree result;
4287 int flags;
4289 /* Maintain a list of namespaces visited... */
4290 tree seen = NULL_TREE;
4291 /* ... and a list of namespace yet to see. */
4292 tree todo = NULL_TREE;
4293 tree usings;
4294 /* Look through namespace aliases. */
4295 scope = ORIGINAL_NAMESPACE (scope);
4296 while (scope && (result != error_mark_node))
4298 seen = tree_cons (scope, NULL_TREE, seen);
4299 result = ambiguous_decl (name, result,
4300 binding_for_name (name, scope), flags);
4301 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4302 /* Consider using directives. */
4303 for (usings = DECL_NAMESPACE_USING (scope); usings;
4304 usings = TREE_CHAIN (usings))
4305 /* If this was a real directive, and we have not seen it. */
4306 if (!TREE_INDIRECT_USING (usings)
4307 && !purpose_member (TREE_PURPOSE (usings), seen))
4308 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4309 if (todo)
4311 scope = TREE_PURPOSE (todo);
4312 todo = TREE_CHAIN (todo);
4314 else
4315 scope = NULL_TREE; /* If there never was a todo list. */
4317 return result != error_mark_node;
4320 /* [namespace.memdef]/2 */
4322 /* Set the context of a declaration to scope. Complain if we are not
4323 outside scope. */
4325 void
4326 set_decl_namespace (decl, scope, friendp)
4327 tree decl;
4328 tree scope;
4329 int friendp;
4331 tree old;
4333 /* Get rid of namespace aliases. */
4334 scope = ORIGINAL_NAMESPACE (scope);
4336 /* It is ok for friends to be qualified in parallel space. */
4337 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4338 error ("declaration of `%D' not in a namespace surrounding `%D'",
4339 decl, scope);
4340 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4341 if (scope != current_namespace)
4343 /* See whether this has been declared in the namespace. */
4344 old = namespace_binding (DECL_NAME (decl), scope);
4345 if (!old)
4346 /* No old declaration at all. */
4347 goto complain;
4348 /* A template can be explicitly specialized in any namespace. */
4349 if (processing_explicit_instantiation)
4350 return;
4351 if (!is_overloaded_fn (decl))
4352 /* Don't compare non-function decls with decls_match here,
4353 since it can't check for the correct constness at this
4354 point. pushdecl will find those errors later. */
4355 return;
4356 /* Since decl is a function, old should contain a function decl. */
4357 if (!is_overloaded_fn (old))
4358 goto complain;
4359 if (processing_template_decl || processing_specialization)
4360 /* We have not yet called push_template_decl to turn the
4361 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4362 won't match. But, we'll check later, when we construct the
4363 template. */
4364 return;
4365 for (; old; old = OVL_NEXT (old))
4366 if (decls_match (decl, OVL_CURRENT (old)))
4367 return;
4369 else
4370 return;
4371 complain:
4372 error ("`%D' should have been declared inside `%D'",
4373 decl, scope);
4376 /* Compute the namespace where a declaration is defined. */
4378 static tree
4379 decl_namespace (decl)
4380 tree decl;
4382 if (TYPE_P (decl))
4383 decl = TYPE_STUB_DECL (decl);
4384 while (DECL_CONTEXT (decl))
4386 decl = DECL_CONTEXT (decl);
4387 if (TREE_CODE (decl) == NAMESPACE_DECL)
4388 return decl;
4389 if (TYPE_P (decl))
4390 decl = TYPE_STUB_DECL (decl);
4391 my_friendly_assert (DECL_P (decl), 390);
4394 return global_namespace;
4397 /* Return the namespace where the current declaration is declared. */
4399 tree
4400 current_decl_namespace ()
4402 tree result;
4403 /* If we have been pushed into a different namespace, use it. */
4404 if (decl_namespace_list)
4405 return TREE_PURPOSE (decl_namespace_list);
4407 if (current_class_type)
4408 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4409 else if (current_function_decl)
4410 result = decl_namespace (current_function_decl);
4411 else
4412 result = current_namespace;
4413 return result;
4416 /* Temporarily set the namespace for the current declaration. */
4418 void
4419 push_decl_namespace (decl)
4420 tree decl;
4422 if (TREE_CODE (decl) != NAMESPACE_DECL)
4423 decl = decl_namespace (decl);
4424 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4425 NULL_TREE, decl_namespace_list);
4428 void
4429 pop_decl_namespace ()
4431 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4434 /* Enter a class or namespace scope. */
4436 void
4437 push_scope (t)
4438 tree t;
4440 if (TREE_CODE (t) == NAMESPACE_DECL)
4441 push_decl_namespace (t);
4442 else
4443 pushclass (t, 2);
4446 /* Leave scope pushed by push_scope. */
4448 void
4449 pop_scope (t)
4450 tree t;
4452 if (TREE_CODE (t) == NAMESPACE_DECL)
4453 pop_decl_namespace ();
4454 else
4455 popclass ();
4458 /* [basic.lookup.koenig] */
4459 /* A non-zero return value in the functions below indicates an error.
4460 All nodes allocated in the procedure are on the scratch obstack. */
4462 struct arg_lookup
4464 tree name;
4465 tree namespaces;
4466 tree classes;
4467 tree functions;
4470 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4471 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4472 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4473 static int add_function PARAMS ((struct arg_lookup *, tree));
4474 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4475 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4476 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4478 /* Add a function to the lookup structure.
4479 Returns 1 on error. */
4481 static int
4482 add_function (k, fn)
4483 struct arg_lookup *k;
4484 tree fn;
4486 /* We used to check here to see if the function was already in the list,
4487 but that's O(n^2), which is just too expensive for function lookup.
4488 Now we deal with the occasional duplicate in joust. In doing this, we
4489 assume that the number of duplicates will be small compared to the
4490 total number of functions being compared, which should usually be the
4491 case. */
4493 /* We must find only functions, or exactly one non-function. */
4494 if (!k->functions)
4495 k->functions = fn;
4496 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4497 k->functions = build_overload (fn, k->functions);
4498 else
4500 tree f1 = OVL_CURRENT (k->functions);
4501 tree f2 = fn;
4502 if (is_overloaded_fn (f1))
4504 fn = f1; f1 = f2; f2 = fn;
4506 cp_error_at ("`%D' is not a function,", f1);
4507 cp_error_at (" conflict with `%D'", f2);
4508 error (" in call to `%D'", k->name);
4509 return 1;
4512 return 0;
4515 /* Add functions of a namespace to the lookup structure.
4516 Returns 1 on error. */
4518 static int
4519 arg_assoc_namespace (k, scope)
4520 struct arg_lookup *k;
4521 tree scope;
4523 tree value;
4525 if (purpose_member (scope, k->namespaces))
4526 return 0;
4527 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4529 value = namespace_binding (k->name, scope);
4530 if (!value)
4531 return 0;
4533 for (; value; value = OVL_NEXT (value))
4534 if (add_function (k, OVL_CURRENT (value)))
4535 return 1;
4537 return 0;
4540 /* Adds everything associated with a template argument to the lookup
4541 structure. Returns 1 on error. */
4543 static int
4544 arg_assoc_template_arg (k, arg)
4545 struct arg_lookup* k;
4546 tree arg;
4548 /* [basic.lookup.koenig]
4550 If T is a template-id, its associated namespaces and classes are
4551 ... the namespaces and classes associated with the types of the
4552 template arguments provided for template type parameters
4553 (excluding template template parameters); the namespaces in which
4554 any template template arguments are defined; and the classes in
4555 which any member templates used as template template arguments
4556 are defined. [Note: non-type template arguments do not
4557 contribute to the set of associated namespaces. ] */
4559 /* Consider first template template arguments. */
4560 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4561 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4562 return 0;
4563 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4565 tree ctx = CP_DECL_CONTEXT (arg);
4567 /* It's not a member template. */
4568 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4569 return arg_assoc_namespace (k, ctx);
4570 /* Otherwise, it must be member template. */
4571 else
4572 return arg_assoc_class (k, ctx);
4574 /* It's not a template template argument, but it is a type template
4575 argument. */
4576 else if (TYPE_P (arg))
4577 return arg_assoc_type (k, arg);
4578 /* It's a non-type template argument. */
4579 else
4580 return 0;
4583 /* Adds everything associated with class to the lookup structure.
4584 Returns 1 on error. */
4586 static int
4587 arg_assoc_class (k, type)
4588 struct arg_lookup* k;
4589 tree type;
4591 tree list, friends, context;
4592 int i;
4594 /* Backend build structures, such as __builtin_va_list, aren't
4595 affected by all this. */
4596 if (!CLASS_TYPE_P (type))
4597 return 0;
4599 if (purpose_member (type, k->classes))
4600 return 0;
4601 k->classes = tree_cons (type, NULL_TREE, k->classes);
4603 context = decl_namespace (TYPE_MAIN_DECL (type));
4604 if (arg_assoc_namespace (k, context))
4605 return 1;
4607 /* Process baseclasses. */
4608 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4609 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4610 return 1;
4612 /* Process friends. */
4613 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4614 list = TREE_CHAIN (list))
4615 if (k->name == TREE_PURPOSE (list))
4616 for (friends = TREE_VALUE (list); friends;
4617 friends = TREE_CHAIN (friends))
4618 /* Only interested in global functions with potentially hidden
4619 (i.e. unqualified) declarations. */
4620 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4621 && decl_namespace (TREE_VALUE (friends)) == context)
4622 if (add_function (k, TREE_VALUE (friends)))
4623 return 1;
4625 /* Process template arguments. */
4626 if (CLASSTYPE_TEMPLATE_INFO (type))
4628 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4629 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4630 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4633 return 0;
4636 /* Adds everything associated with a given type.
4637 Returns 1 on error. */
4639 static int
4640 arg_assoc_type (k, type)
4641 struct arg_lookup *k;
4642 tree type;
4644 switch (TREE_CODE (type))
4646 case VOID_TYPE:
4647 case INTEGER_TYPE:
4648 case REAL_TYPE:
4649 case COMPLEX_TYPE:
4650 case VECTOR_TYPE:
4651 case CHAR_TYPE:
4652 case BOOLEAN_TYPE:
4653 return 0;
4654 case RECORD_TYPE:
4655 if (TYPE_PTRMEMFUNC_P (type))
4656 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4657 return arg_assoc_class (k, type);
4658 case POINTER_TYPE:
4659 case REFERENCE_TYPE:
4660 case ARRAY_TYPE:
4661 return arg_assoc_type (k, TREE_TYPE (type));
4662 case UNION_TYPE:
4663 case ENUMERAL_TYPE:
4664 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4665 case OFFSET_TYPE:
4666 /* Pointer to member: associate class type and value type. */
4667 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4668 return 1;
4669 return arg_assoc_type (k, TREE_TYPE (type));
4670 case METHOD_TYPE:
4671 /* The basetype is referenced in the first arg type, so just
4672 fall through. */
4673 case FUNCTION_TYPE:
4674 /* Associate the parameter types. */
4675 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4676 return 1;
4677 /* Associate the return type. */
4678 return arg_assoc_type (k, TREE_TYPE (type));
4679 case TEMPLATE_TYPE_PARM:
4680 case BOUND_TEMPLATE_TEMPLATE_PARM:
4681 return 0;
4682 case TYPENAME_TYPE:
4683 return 0;
4684 case LANG_TYPE:
4685 if (type == unknown_type_node)
4686 return 0;
4687 /* else fall through */
4688 default:
4689 abort ();
4691 return 0;
4694 /* Adds everything associated with arguments. Returns 1 on error. */
4696 static int
4697 arg_assoc_args (k, args)
4698 struct arg_lookup* k;
4699 tree args;
4701 for (; args; args = TREE_CHAIN (args))
4702 if (arg_assoc (k, TREE_VALUE (args)))
4703 return 1;
4704 return 0;
4707 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4709 static int
4710 arg_assoc (k, n)
4711 struct arg_lookup* k;
4712 tree n;
4714 if (n == error_mark_node)
4715 return 0;
4717 if (TYPE_P (n))
4718 return arg_assoc_type (k, n);
4720 if (! type_unknown_p (n))
4721 return arg_assoc_type (k, TREE_TYPE (n));
4723 if (TREE_CODE (n) == ADDR_EXPR)
4724 n = TREE_OPERAND (n, 0);
4725 if (TREE_CODE (n) == COMPONENT_REF)
4726 n = TREE_OPERAND (n, 1);
4727 if (TREE_CODE (n) == OFFSET_REF)
4728 n = TREE_OPERAND (n, 1);
4729 while (TREE_CODE (n) == TREE_LIST)
4730 n = TREE_VALUE (n);
4732 if (TREE_CODE (n) == FUNCTION_DECL)
4733 return arg_assoc_type (k, TREE_TYPE (n));
4734 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4736 /* [basic.lookup.koenig]
4738 If T is a template-id, its associated namespaces and classes
4739 are the namespace in which the template is defined; for
4740 member templates, the member template's class... */
4741 tree template = TREE_OPERAND (n, 0);
4742 tree args = TREE_OPERAND (n, 1);
4743 tree ctx;
4744 tree arg;
4746 if (TREE_CODE (template) == COMPONENT_REF)
4747 template = TREE_OPERAND (template, 1);
4749 /* First, the template. There may actually be more than one if
4750 this is an overloaded function template. But, in that case,
4751 we only need the first; all the functions will be in the same
4752 namespace. */
4753 template = OVL_CURRENT (template);
4755 ctx = CP_DECL_CONTEXT (template);
4757 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4759 if (arg_assoc_namespace (k, ctx) == 1)
4760 return 1;
4762 /* It must be a member template. */
4763 else if (arg_assoc_class (k, ctx) == 1)
4764 return 1;
4766 /* Now the arguments. */
4767 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4768 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4769 return 1;
4771 else
4773 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4775 for (; n; n = OVL_CHAIN (n))
4776 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4777 return 1;
4780 return 0;
4783 /* Performs Koenig lookup depending on arguments, where fns
4784 are the functions found in normal lookup. */
4786 tree
4787 lookup_arg_dependent (name, fns, args)
4788 tree name;
4789 tree fns;
4790 tree args;
4792 struct arg_lookup k;
4793 tree fn = NULL_TREE;
4795 k.name = name;
4796 k.functions = fns;
4797 k.classes = NULL_TREE;
4799 /* Note that we've already looked at some namespaces during normal
4800 unqualified lookup, unless we found a decl in function scope. */
4801 if (fns)
4802 fn = OVL_CURRENT (fns);
4803 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4804 k.namespaces = NULL_TREE;
4805 else
4806 unqualified_namespace_lookup (name, 0, &k.namespaces);
4808 arg_assoc_args (&k, args);
4809 return k.functions;
4812 /* Process a namespace-alias declaration. */
4814 void
4815 do_namespace_alias (alias, namespace)
4816 tree alias, namespace;
4818 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4820 /* The parser did not find it, so it's not there. */
4821 error ("unknown namespace `%D'", namespace);
4822 return;
4825 namespace = ORIGINAL_NAMESPACE (namespace);
4827 /* Build the alias. */
4828 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4829 DECL_NAMESPACE_ALIAS (alias) = namespace;
4830 pushdecl (alias);
4833 /* Check a non-member using-declaration. Return the name and scope
4834 being used, and the USING_DECL, or NULL_TREE on failure. */
4836 static tree
4837 validate_nonmember_using_decl (decl, scope, name)
4838 tree decl;
4839 tree *scope;
4840 tree *name;
4842 if (TREE_CODE (decl) == SCOPE_REF)
4844 *scope = TREE_OPERAND (decl, 0);
4845 *name = TREE_OPERAND (decl, 1);
4847 if (!processing_template_decl)
4849 /* [namespace.udecl]
4850 A using-declaration for a class member shall be a
4851 member-declaration. */
4852 if(TREE_CODE (*scope) != NAMESPACE_DECL)
4854 if (TYPE_P (*scope))
4855 error ("`%T' is not a namespace", *scope);
4856 else
4857 error ("`%D' is not a namespace", *scope);
4858 return NULL_TREE;
4861 /* 7.3.3/5
4862 A using-declaration shall not name a template-id. */
4863 if (TREE_CODE (*name) == TEMPLATE_ID_EXPR)
4865 *name = TREE_OPERAND (*name, 0);
4866 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4867 return NULL_TREE;
4871 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4872 || TREE_CODE (decl) == TYPE_DECL
4873 || TREE_CODE (decl) == TEMPLATE_DECL)
4875 *scope = global_namespace;
4876 *name = decl;
4878 else if (TREE_CODE (decl) == NAMESPACE_DECL)
4880 error ("namespace `%D' not allowed in using-declaration", decl);
4881 return NULL_TREE;
4883 else
4884 abort ();
4885 if (DECL_P (*name))
4886 *name = DECL_NAME (*name);
4887 /* Make a USING_DECL. */
4888 return push_using_decl (*scope, *name);
4891 /* Process local and global using-declarations. */
4893 static void
4894 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4895 tree scope, name;
4896 tree oldval, oldtype;
4897 tree *newval, *newtype;
4899 tree decls;
4901 *newval = *newtype = NULL_TREE;
4902 decls = make_node (CPLUS_BINDING);
4903 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4904 /* Lookup error */
4905 return;
4907 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4909 error ("`%D' not declared", name);
4910 return;
4913 /* Check for using functions. */
4914 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4916 tree tmp, tmp1;
4918 if (oldval && !is_overloaded_fn (oldval))
4920 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4921 oldval = NULL_TREE;
4924 *newval = oldval;
4925 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4927 tree new_fn = OVL_CURRENT (tmp);
4929 /* [namespace.udecl]
4931 If a function declaration in namespace scope or block
4932 scope has the same name and the same parameter types as a
4933 function introduced by a using declaration the program is
4934 ill-formed. */
4935 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4937 tree old_fn = OVL_CURRENT (tmp1);
4939 if (new_fn == old_fn)
4940 /* The function already exists in the current namespace. */
4941 break;
4942 else if (OVL_USED (tmp1))
4943 continue; /* this is a using decl */
4944 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4945 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4947 /* If this using declaration introduces a function
4948 recognized as a built-in, no longer mark it as
4949 anticipated in this scope. */
4950 if (DECL_ANTICIPATED (old_fn))
4952 DECL_ANTICIPATED (old_fn) = 0;
4953 break;
4956 /* There was already a non-using declaration in
4957 this scope with the same parameter types. If both
4958 are the same extern "C" functions, that's ok. */
4959 if (!decls_match (new_fn, old_fn))
4960 error ("`%D' is already declared in this scope", name);
4961 break;
4965 /* If we broke out of the loop, there's no reason to add
4966 this function to the using declarations for this
4967 scope. */
4968 if (tmp1)
4969 continue;
4971 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4972 if (TREE_CODE (*newval) != OVERLOAD)
4973 *newval = ovl_cons (*newval, NULL_TREE);
4974 OVL_USED (*newval) = 1;
4977 else
4979 *newval = BINDING_VALUE (decls);
4980 if (oldval)
4981 duplicate_decls (*newval, oldval);
4984 *newtype = BINDING_TYPE (decls);
4985 if (oldtype && *newtype && oldtype != *newtype)
4987 error ("using declaration `%D' introduced ambiguous type `%T'",
4988 name, oldtype);
4989 return;
4993 /* Process a using-declaration not appearing in class or local scope. */
4995 void
4996 do_toplevel_using_decl (decl)
4997 tree decl;
4999 tree scope, name, binding;
5000 tree oldval, oldtype, newval, newtype;
5002 decl = validate_nonmember_using_decl (decl, &scope, &name);
5003 if (decl == NULL_TREE)
5004 return;
5006 binding = binding_for_name (name, current_namespace);
5008 oldval = BINDING_VALUE (binding);
5009 oldtype = BINDING_TYPE (binding);
5011 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5013 /* Copy declarations found. */
5014 if (newval)
5015 BINDING_VALUE (binding) = newval;
5016 if (newtype)
5017 BINDING_TYPE (binding) = newtype;
5018 return;
5021 /* Process a using-declaration at function scope. */
5023 void
5024 do_local_using_decl (decl)
5025 tree decl;
5027 tree scope, name;
5028 tree oldval, oldtype, newval, newtype;
5030 decl = validate_nonmember_using_decl (decl, &scope, &name);
5031 if (decl == NULL_TREE)
5032 return;
5034 if (building_stmt_tree ()
5035 && at_function_scope_p ())
5036 add_decl_stmt (decl);
5038 oldval = lookup_name_current_level (name);
5039 oldtype = lookup_type_current_level (name);
5041 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5043 if (newval)
5045 if (is_overloaded_fn (newval))
5047 tree fn, term;
5049 /* We only need to push declarations for those functions
5050 that were not already bound in the current level.
5051 The old value might be NULL_TREE, it might be a single
5052 function, or an OVERLOAD. */
5053 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5054 term = OVL_FUNCTION (oldval);
5055 else
5056 term = oldval;
5057 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5058 fn = OVL_NEXT (fn))
5059 push_overloaded_decl (OVL_CURRENT (fn),
5060 PUSH_LOCAL | PUSH_USING);
5062 else
5063 push_local_binding (name, newval, PUSH_USING);
5065 if (newtype)
5066 set_identifier_type_value (name, newtype);
5069 tree
5070 do_class_using_decl (decl)
5071 tree decl;
5073 tree name, value;
5075 if (TREE_CODE (decl) != SCOPE_REF
5076 || !TYPE_P (TREE_OPERAND (decl, 0)))
5078 error ("using-declaration for non-member at class scope");
5079 return NULL_TREE;
5081 name = TREE_OPERAND (decl, 1);
5082 if (TREE_CODE (name) == BIT_NOT_EXPR)
5084 error ("using-declaration for destructor");
5085 return NULL_TREE;
5087 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5089 name = TREE_OPERAND (name, 0);
5090 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
5091 return NULL_TREE;
5093 if (TREE_CODE (name) == TYPE_DECL || TREE_CODE (name) == TEMPLATE_DECL)
5094 name = DECL_NAME (name);
5096 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5098 value = build_lang_decl (USING_DECL, name, void_type_node);
5099 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5100 return value;
5103 /* Process a using-directive. */
5105 void
5106 do_using_directive (namespace)
5107 tree namespace;
5109 if (building_stmt_tree ())
5110 add_stmt (build_stmt (USING_STMT, namespace));
5112 /* using namespace A::B::C; */
5113 if (TREE_CODE (namespace) == SCOPE_REF)
5114 namespace = TREE_OPERAND (namespace, 1);
5115 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5117 /* Lookup in lexer did not find a namespace. */
5118 if (!processing_template_decl)
5119 error ("namespace `%T' undeclared", namespace);
5120 return;
5122 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5124 if (!processing_template_decl)
5125 error ("`%T' is not a namespace", namespace);
5126 return;
5128 namespace = ORIGINAL_NAMESPACE (namespace);
5129 if (!toplevel_bindings_p ())
5130 push_using_directive (namespace);
5131 else
5132 /* direct usage */
5133 add_using_namespace (current_namespace, namespace, 0);
5136 void
5137 check_default_args (x)
5138 tree x;
5140 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5141 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5142 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5144 if (TREE_PURPOSE (arg))
5145 saw_def = 1;
5146 else if (saw_def)
5148 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5149 i, x);
5150 break;
5155 void
5156 mark_used (decl)
5157 tree decl;
5159 TREE_USED (decl) = 1;
5160 if (processing_template_decl)
5161 return;
5162 assemble_external (decl);
5164 /* Is it a synthesized method that needs to be synthesized? */
5165 if (TREE_CODE (decl) == FUNCTION_DECL
5166 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5167 && DECL_ARTIFICIAL (decl)
5168 && ! DECL_INITIAL (decl)
5169 /* Kludge: don't synthesize for default args. */
5170 && current_function_decl)
5172 synthesize_method (decl);
5173 /* If we've already synthesized the method we don't need to
5174 instantiate it, so we can return right away. */
5175 return;
5178 /* If this is a function or variable that is an instance of some
5179 template, we now know that we will need to actually do the
5180 instantiation. We check that DECL is not an explicit
5181 instantiation because that is not checked in instantiate_decl. */
5182 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5183 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5184 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5185 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5186 instantiate_decl (decl, /*defer_ok=*/1);
5189 /* Helper function for class_head_decl and class_head_defn
5190 nonterminals. AGGR is the class, union or struct tag. SCOPE is the
5191 explicit scope used (NULL for no scope resolution). ID is the
5192 name. DEFN_P is true, if this is a definition of the class and
5193 NEW_TYPE_P is set to non-zero, if we push into the scope containing
5194 the to be defined aggregate.
5196 Return a TYPE_DECL for the type declared by ID in SCOPE. */
5198 tree
5199 handle_class_head (aggr, scope, id, defn_p, new_type_p)
5200 tree aggr, scope, id;
5201 int defn_p;
5202 int *new_type_p;
5204 tree decl = NULL_TREE;
5205 tree current = current_scope ();
5206 bool xrefd_p = false;
5208 if (current == NULL_TREE)
5209 current = current_namespace;
5211 *new_type_p = 0;
5213 if (scope)
5215 if (TREE_CODE (id) == TYPE_DECL)
5216 /* We must bash typedefs back to the main decl of the
5217 type. Otherwise we become confused about scopes. */
5218 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5219 else if (DECL_CLASS_TEMPLATE_P (id))
5220 decl = DECL_TEMPLATE_RESULT (id);
5221 else
5223 if (TYPE_P (scope))
5225 /* According to the suggested resolution of core issue
5226 180, 'typename' is assumed after a class-key. */
5227 decl = make_typename_type (scope, id, tf_error);
5228 if (decl != error_mark_node)
5229 decl = TYPE_MAIN_DECL (decl);
5230 else
5231 decl = NULL_TREE;
5233 else if (scope == current)
5235 /* We've been given AGGR SCOPE::ID, when we're already
5236 inside SCOPE. Be nice about it. */
5237 if (pedantic)
5238 pedwarn ("extra qualification `%T::' on member `%D' ignored",
5239 scope, id);
5241 else
5242 error ("`%T' does not have a class or union named `%D'",
5243 scope, id);
5247 if (!decl)
5249 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, !defn_p));
5250 xrefd_p = true;
5253 if (!TYPE_BINFO (TREE_TYPE (decl)))
5255 error ("`%T' is not a class or union type", decl);
5256 return error_mark_node;
5259 if (defn_p)
5261 /* For a definition, we want to enter the containing scope
5262 before looking up any base classes etc. Only do so, if this
5263 is different to the current scope. */
5264 tree context = CP_DECL_CONTEXT (decl);
5266 *new_type_p = (current != context
5267 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
5268 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
5269 if (*new_type_p)
5270 push_scope (context);
5272 if (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
5273 /* It is legal to define a class with a different class key,
5274 and this changes the default member access. */
5275 CLASSTYPE_DECLARED_CLASS (TREE_TYPE (decl))
5276 = aggr == class_type_node;
5278 if (!xrefd_p && PROCESSING_REAL_TEMPLATE_DECL_P ())
5279 decl = push_template_decl (decl);
5282 return decl;
5285 /* Initialize decl2.c. */
5287 void
5288 init_decl2 ()
5290 ggc_add_tree_varray_root (&deferred_fns, 1);
5291 ggc_add_tree_varray_root (&pending_statics, 1);
5292 ggc_add_tree_varray_root (&ssdf_decls, 1);
5293 ggc_add_tree_root (&ssdf_decl, 1);
5294 ggc_add_tree_root (&priority_decl, 1);
5295 ggc_add_tree_root (&initialize_p_decl, 1);