cp:
[official-gcc.git] / gcc / cp / decl2.c
blob0cbb32100efc2e7a4f4ce5559b88f948d6b67495
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 do some things the same way PCC does. Only provided so
135 the compiler will link. */
137 int flag_traditional;
139 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
141 int flag_signed_bitfields = 1;
143 /* Nonzero means enable obscure standard features and disable GNU
144 extensions that might cause standard-compliant code to be
145 miscompiled. */
147 int flag_ansi;
149 /* Nonzero means do emit exported implementations of functions even if
150 they can be inlined. */
152 int flag_implement_inlines = 1;
154 /* Nonzero means do emit exported implementations of templates, instead of
155 multiple static copies in each file that needs a definition. */
157 int flag_external_templates;
159 /* Nonzero means that the decision to emit or not emit the implementation of a
160 template depends on where the template is instantiated, rather than where
161 it is defined. */
163 int flag_alt_external_templates;
165 /* Nonzero means that implicit instantiations will be emitted if needed. */
167 int flag_implicit_templates = 1;
169 /* Nonzero means that implicit instantiations of inline templates will be
170 emitted if needed, even if instantiations of non-inline templates
171 aren't. */
173 int flag_implicit_inline_templates = 1;
175 /* Nonzero means warn about implicit declarations. */
177 int warn_implicit = 1;
179 /* Nonzero means warn about usage of long long when `-pedantic'. */
181 int warn_long_long = 1;
183 /* Nonzero means warn when all ctors or dtors are private, and the class
184 has no friends. */
186 int warn_ctor_dtor_privacy = 1;
188 /* Nonzero means generate separate instantiation control files and juggle
189 them at link time. */
191 int flag_use_repository;
193 /* Nonzero if we want to issue diagnostics that the standard says are not
194 required. */
196 int flag_optional_diags = 1;
198 /* Nonzero means give string constants the type `const char *', as mandated
199 by the standard. */
201 int flag_const_strings = 1;
203 /* Nonzero means warn about deprecated conversion from string constant to
204 `char *'. */
206 int warn_write_strings;
208 /* Nonzero means warn about pointer casts that can drop a type qualifier
209 from the pointer target type. */
211 int warn_cast_qual;
213 /* Nonzero means warn about sizeof(function) or addition/subtraction
214 of function pointers. */
216 int warn_pointer_arith = 1;
218 /* Nonzero means warn for any function def without prototype decl. */
220 int warn_missing_prototypes;
222 /* Nonzero means warn about multiple (redundant) decls for the same single
223 variable or function. */
225 int warn_redundant_decls;
227 /* Warn if initializer is not completely bracketed. */
229 int warn_missing_braces;
231 /* Warn about comparison of signed and unsigned values. */
233 int warn_sign_compare;
235 /* Warn about testing equality of floating point numbers. */
237 int warn_float_equal = 0;
239 /* Warn about functions which might be candidates for format attributes. */
241 int warn_missing_format_attribute;
243 /* Warn about a subscript that has type char. */
245 int warn_char_subscripts;
247 /* Warn if a type conversion is done that might have confusing results. */
249 int warn_conversion;
251 /* Warn if adding () is suggested. */
253 int warn_parentheses;
255 /* Non-zero means warn in function declared in derived class has the
256 same name as a virtual in the base class, but fails to match the
257 type signature of any virtual function in the base class. */
259 int warn_overloaded_virtual;
261 /* Non-zero means warn when declaring a class that has a non virtual
262 destructor, when it really ought to have a virtual one. */
264 int warn_nonvdtor;
266 /* Non-zero means warn when the compiler will reorder code. */
268 int warn_reorder;
270 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
272 int warn_synth;
274 /* Non-zero means warn when we convert a pointer to member function
275 into a pointer to (void or function). */
277 int warn_pmf2ptr = 1;
279 /* Nonzero means warn about violation of some Effective C++ style rules. */
281 int warn_ecpp;
283 /* Nonzero means warn where overload resolution chooses a promotion from
284 unsigned to signed over a conversion to an unsigned of the same size. */
286 int warn_sign_promo;
288 /* Nonzero means warn when an old-style cast is used. */
290 int warn_old_style_cast;
292 /* Warn about #pragma directives that are not recognised. */
294 int warn_unknown_pragmas; /* Tri state variable. */
296 /* Nonzero means warn about use of multicharacter literals. */
298 int warn_multichar = 1;
300 /* Nonzero means warn when non-templatized friend functions are
301 declared within a template */
303 int warn_nontemplate_friend = 1;
305 /* Nonzero means complain about deprecated features. */
307 int warn_deprecated = 1;
309 /* Nonzero means `$' can be in an identifier. */
311 #ifndef DOLLARS_IN_IDENTIFIERS
312 #define DOLLARS_IN_IDENTIFIERS 1
313 #endif
314 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
316 /* Nonzero means allow Microsoft extensions without a pedwarn. */
318 int flag_ms_extensions;
320 /* C++ specific flags. */
322 /* Nonzero means we should attempt to elide constructors when possible. */
324 int flag_elide_constructors = 1;
326 /* Nonzero means that member functions defined in class scope are
327 inline by default. */
329 int flag_default_inline = 1;
331 /* Controls whether compiler generates 'type descriptor' that give
332 run-time type information. */
334 int flag_rtti = 1;
336 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
337 objects. */
339 int flag_huge_objects;
341 /* Nonzero if we want to conserve space in the .o files. We do this
342 by putting uninitialized data and runtime initialized data into
343 .common instead of .data at the expense of not flagging multiple
344 definitions. */
346 int flag_conserve_space;
348 /* Nonzero if we want to obey access control semantics. */
350 int flag_access_control = 1;
352 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
354 int flag_operator_names = 1;
356 /* Nonzero if we want to check the return value of new and avoid calling
357 constructors if it is a null pointer. */
359 int flag_check_new;
361 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
362 initialization variables.
363 0: Old rules, set by -fno-for-scope.
364 2: New ISO rules, set by -ffor-scope.
365 1: Try to implement new ISO rules, but with backup compatibility
366 (and warnings). This is the default, for now. */
368 int flag_new_for_scope = 1;
370 /* Nonzero if we want to emit defined symbols with common-like linkage as
371 weak symbols where possible, in order to conform to C++ semantics.
372 Otherwise, emit them as local symbols. */
374 int flag_weak = 1;
376 /* Nonzero to use __cxa_atexit, rather than atexit, to register
377 destructors for local statics and global objects. */
379 int flag_use_cxa_atexit;
381 /* Maximum template instantiation depth. This limit is rather
382 arbitrary, but it exists to limit the time it takes to notice
383 infinite template instantiations. */
385 int max_tinst_depth = 500;
387 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
389 int flag_vtable_gc;
391 /* Nonzero means make the default pedwarns warnings instead of errors.
392 The value of this flag is ignored if -pedantic is specified. */
394 int flag_permissive;
396 /* Nonzero means to implement standard semantics for exception
397 specifications, calling unexpected if an exception is thrown that
398 doesn't match the specification. Zero means to treat them as
399 assertions and optimize accordingly, but not check them. */
401 int flag_enforce_eh_specs = 1;
403 /* Table of language-dependent -f options.
404 STRING is the option name. VARIABLE is the address of the variable.
405 ON_VALUE is the value to store in VARIABLE
406 if `-fSTRING' is seen as an option.
407 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
409 static const struct { const char *const string; int *const variable; const int on_value;}
410 lang_f_options[] =
412 /* C/C++ options. */
413 {"signed-char", &flag_signed_char, 1},
414 {"unsigned-char", &flag_signed_char, 0},
415 {"signed-bitfields", &flag_signed_bitfields, 1},
416 {"unsigned-bitfields", &flag_signed_bitfields, 0},
417 {"short-enums", &flag_short_enums, 1},
418 {"short-double", &flag_short_double, 1},
419 {"short-wchar", &flag_short_wchar, 1},
420 {"asm", &flag_no_asm, 0},
421 {"builtin", &flag_no_builtin, 0},
423 /* C++-only options. */
424 {"access-control", &flag_access_control, 1},
425 {"check-new", &flag_check_new, 1},
426 {"conserve-space", &flag_conserve_space, 1},
427 {"const-strings", &flag_const_strings, 1},
428 {"default-inline", &flag_default_inline, 1},
429 {"dollars-in-identifiers", &dollars_in_ident, 1},
430 {"elide-constructors", &flag_elide_constructors, 1},
431 {"enforce-eh-specs", &flag_enforce_eh_specs, 1},
432 {"external-templates", &flag_external_templates, 1},
433 {"for-scope", &flag_new_for_scope, 2},
434 {"gnu-keywords", &flag_no_gnu_keywords, 0},
435 {"handle-exceptions", &flag_exceptions, 1},
436 {"implement-inlines", &flag_implement_inlines, 1},
437 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
438 {"implicit-templates", &flag_implicit_templates, 1},
439 {"ms-extensions", &flag_ms_extensions, 1},
440 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
441 {"operator-names", &flag_operator_names, 1},
442 {"optional-diags", &flag_optional_diags, 1},
443 {"permissive", &flag_permissive, 1},
444 {"repo", &flag_use_repository, 1},
445 {"rtti", &flag_rtti, 1},
446 {"stats", &flag_detailed_statistics, 1},
447 {"vtable-gc", &flag_vtable_gc, 1},
448 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
449 {"weak", &flag_weak, 1}
452 /* The list of `-f' options that we no longer support. The `-f'
453 prefix is not given in this table. The `-fno-' variants are not
454 listed here. This table must be kept in alphabetical order. */
455 static const char * const unsupported_options[] = {
456 "all-virtual",
457 "cond-mismatch",
458 "enum-int-equiv",
459 "guiding-decls",
460 "honor-std",
461 "huge-objects",
462 "labels-ok",
463 "new-abi",
464 "nonnull-objects",
465 "squangle",
466 "strict-prototype",
467 "this-is-variable",
468 "vtable-thunks",
469 "xref"
472 /* Compare two option strings, pointed two by P1 and P2, for use with
473 bsearch. */
475 static int
476 compare_options (p1, p2)
477 const PTR p1;
478 const PTR p2;
480 return strcmp (*((const char *const *) p1), *((const char *const *) p2));
483 /* Decode the string P as a language-specific option.
484 Return the number of strings consumed for a valid option.
485 Otherwise return 0. Should not complain if it does not
486 recognise the option. */
488 int
489 cxx_decode_option (argc, argv)
490 int argc;
491 char **argv;
493 int strings_processed;
494 const char *p = argv[0];
496 strings_processed = cpp_handle_option (parse_in, argc, argv, 0);
498 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
499 /* ignore */;
500 else if (p[0] == '-' && p[1] == 'f')
502 /* Some kind of -f option.
503 P's value is the option sans `-f'.
504 Search for it in the table of options. */
505 const char *option_value = NULL;
506 const char *positive_option;
507 size_t j;
509 p += 2;
510 /* Try special -f options. */
512 /* See if this is one of the options no longer supported. We
513 used to support these options, so we continue to accept them,
514 with a warning. */
515 if (strncmp (p, "no-", strlen ("no-")) == 0)
516 positive_option = p + strlen ("no-");
517 else
518 positive_option = p;
520 /* If the option is present, issue a warning. Indicate to our
521 caller that the option was processed successfully. */
522 if (bsearch (&positive_option,
523 unsupported_options,
524 (sizeof (unsupported_options)
525 / sizeof (unsupported_options[0])),
526 sizeof (unsupported_options[0]),
527 compare_options))
529 warning ("-f%s is no longer supported", p);
530 return 1;
533 if (!strcmp (p, "handle-exceptions")
534 || !strcmp (p, "no-handle-exceptions"))
535 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
536 else if (! strcmp (p, "alt-external-templates"))
538 flag_external_templates = 1;
539 flag_alt_external_templates = 1;
540 cp_deprecated ("-falt-external-templates");
542 else if (! strcmp (p, "no-alt-external-templates"))
543 flag_alt_external_templates = 0;
544 else if (!strcmp (p, "repo"))
546 flag_use_repository = 1;
547 flag_implicit_templates = 0;
549 else if (!strcmp (p, "external-templates"))
551 flag_external_templates = 1;
552 cp_deprecated ("-fexternal-templates");
554 else if ((option_value
555 = skip_leading_substring (p, "template-depth-")))
556 max_tinst_depth
557 = read_integral_parameter (option_value, p - 2, max_tinst_depth);
558 else if ((option_value
559 = skip_leading_substring (p, "name-mangling-version-")))
561 warning ("-fname-mangling-version is no longer supported");
562 return 1;
564 else if (dump_switch_p (p))
566 else
568 int found = 0;
570 for (j = 0;
571 !found && j < (sizeof (lang_f_options)
572 / sizeof (lang_f_options[0]));
573 j++)
575 if (!strcmp (p, lang_f_options[j].string))
577 *lang_f_options[j].variable = lang_f_options[j].on_value;
578 /* A goto here would be cleaner,
579 but breaks the VAX pcc. */
580 found = 1;
582 else if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
583 && ! strcmp (p+3, lang_f_options[j].string))
585 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
586 found = 1;
590 return found;
593 else if (p[0] == '-' && p[1] == 'W')
595 int setting = 1;
597 /* The -W options control the warning behavior of the compiler. */
598 p += 2;
600 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
601 setting = 0, p += 3;
603 if (!strcmp (p, "implicit"))
604 warn_implicit = setting;
605 else if (!strcmp (p, "long-long"))
606 warn_long_long = setting;
607 else if (!strcmp (p, "return-type"))
608 warn_return_type = setting;
609 else if (!strcmp (p, "ctor-dtor-privacy"))
610 warn_ctor_dtor_privacy = setting;
611 else if (!strcmp (p, "write-strings"))
612 warn_write_strings = setting;
613 else if (!strcmp (p, "cast-qual"))
614 warn_cast_qual = setting;
615 else if (!strcmp (p, "char-subscripts"))
616 warn_char_subscripts = setting;
617 else if (!strcmp (p, "pointer-arith"))
618 warn_pointer_arith = setting;
619 else if (!strcmp (p, "missing-prototypes"))
620 warn_missing_prototypes = setting;
621 else if (!strcmp (p, "strict-prototypes"))
623 if (setting == 0)
624 warning ("-Wno-strict-prototypes is not supported in C++");
626 else if (!strcmp (p, "redundant-decls"))
627 warn_redundant_decls = setting;
628 else if (!strcmp (p, "missing-braces"))
629 warn_missing_braces = setting;
630 else if (!strcmp (p, "sign-compare"))
631 warn_sign_compare = setting;
632 else if (!strcmp (p, "float-equal"))
633 warn_float_equal = setting;
634 else if (!strcmp (p, "format"))
635 set_Wformat (setting);
636 else if (!strcmp (p, "format=2"))
637 set_Wformat (2);
638 else if (!strcmp (p, "format-y2k"))
639 warn_format_y2k = setting;
640 else if (!strcmp (p, "format-extra-args"))
641 warn_format_extra_args = setting;
642 else if (!strcmp (p, "format-nonliteral"))
643 warn_format_nonliteral = setting;
644 else if (!strcmp (p, "format-security"))
645 warn_format_security = setting;
646 else if (!strcmp (p, "missing-format-attribute"))
647 warn_missing_format_attribute = setting;
648 else if (!strcmp (p, "conversion"))
649 warn_conversion = setting;
650 else if (!strcmp (p, "parentheses"))
651 warn_parentheses = setting;
652 else if (!strcmp (p, "non-virtual-dtor"))
653 warn_nonvdtor = setting;
654 else if (!strcmp (p, "reorder"))
655 warn_reorder = setting;
656 else if (!strcmp (p, "synth"))
657 warn_synth = setting;
658 else if (!strcmp (p, "pmf-conversions"))
659 warn_pmf2ptr = setting;
660 else if (!strcmp (p, "effc++"))
661 warn_ecpp = setting;
662 else if (!strcmp (p, "sign-promo"))
663 warn_sign_promo = setting;
664 else if (!strcmp (p, "old-style-cast"))
665 warn_old_style_cast = setting;
666 else if (!strcmp (p, "overloaded-virtual"))
667 warn_overloaded_virtual = setting;
668 else if (!strcmp (p, "multichar"))
669 warn_multichar = setting;
670 else if (!strcmp (p, "unknown-pragmas"))
671 /* Set to greater than 1, so that even unknown pragmas in
672 system headers will be warned about. */
673 warn_unknown_pragmas = setting * 2;
674 else if (!strcmp (p, "non-template-friend"))
675 warn_nontemplate_friend = setting;
676 else if (!strcmp (p, "deprecated"))
677 warn_deprecated = setting;
678 else if (!strcmp (p, "comment"))
679 ; /* cpp handles this one. */
680 else if (!strcmp (p, "comments"))
681 ; /* cpp handles this one. */
682 else if (!strcmp (p, "trigraphs"))
683 ; /* cpp handles this one. */
684 else if (!strcmp (p, "import"))
685 ; /* cpp handles this one. */
686 else if (!strcmp (p, "all"))
688 warn_return_type = setting;
689 set_Wunused (setting);
690 warn_implicit = setting;
691 warn_switch = setting;
692 set_Wformat (setting);
693 warn_parentheses = setting;
694 warn_missing_braces = setting;
695 warn_sign_compare = setting;
696 warn_multichar = setting;
697 /* We save the value of warn_uninitialized, since if they put
698 -Wuninitialized on the command line, we need to generate a
699 warning about not using it without also specifying -O. */
700 if (warn_uninitialized != 1)
701 warn_uninitialized = (setting ? 2 : 0);
702 /* Only warn about unknown pragmas that are not in system
703 headers. */
704 warn_unknown_pragmas = 1;
706 /* C++-specific warnings. */
707 warn_ctor_dtor_privacy = setting;
708 warn_nonvdtor = setting;
709 warn_reorder = setting;
710 warn_nontemplate_friend = setting;
712 else return strings_processed;
714 else if (!strcmp (p, "-ansi"))
715 flag_no_nonansi_builtin = 1, flag_ansi = 1,
716 flag_noniso_default_format_attributes = 0, flag_no_gnu_keywords = 1;
717 #ifdef SPEW_DEBUG
718 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
719 it's probably safe to assume no sane person would ever want to use this
720 under normal circumstances. */
721 else if (!strcmp (p, "-spew-debug"))
722 spew_debug = 1;
723 #endif
724 else
725 return strings_processed;
727 return 1;
730 /* Incorporate `const' and `volatile' qualifiers for member functions.
731 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
732 QUALS is a list of qualifiers. Returns any explicit
733 top-level qualifiers of the method's this pointer, anything other than
734 TYPE_UNQUALIFIED will be an extension. */
737 grok_method_quals (ctype, function, quals)
738 tree ctype, function, quals;
740 tree fntype = TREE_TYPE (function);
741 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
742 int type_quals = TYPE_UNQUALIFIED;
743 int dup_quals = TYPE_UNQUALIFIED;
744 int this_quals = TYPE_UNQUALIFIED;
748 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
750 if ((type_quals | this_quals) & tq)
751 dup_quals |= tq;
752 else if (tq & TYPE_QUAL_RESTRICT)
753 this_quals |= tq;
754 else
755 type_quals |= tq;
756 quals = TREE_CHAIN (quals);
758 while (quals);
760 if (dup_quals != TYPE_UNQUALIFIED)
761 error ("duplicate type qualifiers in %s declaration",
762 TREE_CODE (function) == FUNCTION_DECL
763 ? "member function" : "type");
765 ctype = cp_build_qualified_type (ctype, type_quals);
766 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
767 (TREE_CODE (fntype) == METHOD_TYPE
768 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
769 : TYPE_ARG_TYPES (fntype)));
770 if (raises)
771 fntype = build_exception_variant (fntype, raises);
773 TREE_TYPE (function) = fntype;
774 return this_quals;
777 /* Warn when -fexternal-templates is used and #pragma
778 interface/implementation is not used all the times it should be,
779 inform the user. */
781 void
782 warn_if_unknown_interface (decl)
783 tree decl;
785 static int already_warned = 0;
786 if (already_warned++)
787 return;
789 if (flag_alt_external_templates)
791 tree til = tinst_for_decl ();
792 int sl = lineno;
793 const char *sf = input_filename;
795 if (til)
797 lineno = TINST_LINE (til);
798 input_filename = TINST_FILE (til);
800 warning ("template `%#D' instantiated in file without #pragma interface",
801 decl);
802 lineno = sl;
803 input_filename = sf;
805 else
806 cp_warning_at ("template `%#D' defined in file without #pragma interface",
807 decl);
810 /* A subroutine of the parser, to handle a component list. */
812 void
813 grok_x_components (specs)
814 tree specs;
816 tree t;
818 specs = strip_attrs (specs);
820 check_tag_decl (specs);
821 t = groktypename (build_tree_list (specs, NULL_TREE));
823 /* The only case where we need to do anything additional here is an
824 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
825 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
826 return;
828 fixup_anonymous_aggr (t);
829 finish_member_declaration (build_decl (FIELD_DECL, NULL_TREE, t));
832 /* Returns a PARM_DECL for a parameter of the indicated TYPE, with the
833 indicated NAME. */
835 tree
836 build_artificial_parm (name, type)
837 tree name;
838 tree type;
840 tree parm;
842 parm = build_decl (PARM_DECL, name, type);
843 DECL_ARTIFICIAL (parm) = 1;
844 /* All our artificial parms are implicitly `const'; they cannot be
845 assigned to. */
846 TREE_READONLY (parm) = 1;
847 DECL_ARG_TYPE (parm) = type;
848 return parm;
851 /* Constructors for types with virtual baseclasses need an "in-charge" flag
852 saying whether this constructor is responsible for initialization of
853 virtual baseclasses or not. All destructors also need this "in-charge"
854 flag, which additionally determines whether or not the destructor should
855 free the memory for the object.
857 This function adds the "in-charge" flag to member function FN if
858 appropriate. It is called from grokclassfn and tsubst.
859 FN must be either a constructor or destructor.
861 The in-charge flag follows the 'this' parameter, and is followed by the
862 VTT parm (if any), then the user-written parms. */
864 void
865 maybe_retrofit_in_chrg (fn)
866 tree fn;
868 tree basetype, arg_types, parms, parm, fntype;
870 /* If we've already add the in-charge parameter don't do it again. */
871 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
872 return;
874 /* When processing templates we can't know, in general, whether or
875 not we're going to have virtual baseclasses. */
876 if (uses_template_parms (fn))
877 return;
879 /* We don't need an in-charge parameter for constructors that don't
880 have virtual bases. */
881 if (DECL_CONSTRUCTOR_P (fn)
882 && !TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
883 return;
885 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
886 basetype = TREE_TYPE (TREE_VALUE (arg_types));
887 arg_types = TREE_CHAIN (arg_types);
889 parms = TREE_CHAIN (DECL_ARGUMENTS (fn));
891 /* If this is a subobject constructor or destructor, our caller will
892 pass us a pointer to our VTT. */
893 if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (fn)))
895 parm = build_artificial_parm (vtt_parm_identifier, vtt_parm_type);
897 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */
898 TREE_CHAIN (parm) = parms;
899 parms = parm;
901 /* ...and then to TYPE_ARG_TYPES. */
902 arg_types = hash_tree_chain (vtt_parm_type, arg_types);
904 DECL_HAS_VTT_PARM_P (fn) = 1;
907 /* Then add the in-charge parm (before the VTT parm). */
908 parm = build_artificial_parm (in_charge_identifier, integer_type_node);
909 TREE_CHAIN (parm) = parms;
910 parms = parm;
911 arg_types = hash_tree_chain (integer_type_node, arg_types);
913 /* Insert our new parameter(s) into the list. */
914 TREE_CHAIN (DECL_ARGUMENTS (fn)) = parms;
916 /* And rebuild the function type. */
917 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
918 arg_types);
919 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
920 fntype = build_exception_variant (fntype,
921 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
922 TREE_TYPE (fn) = fntype;
924 /* Now we've got the in-charge parameter. */
925 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1;
928 /* Classes overload their constituent function names automatically.
929 When a function name is declared in a record structure,
930 its name is changed to it overloaded name. Since names for
931 constructors and destructors can conflict, we place a leading
932 '$' for destructors.
934 CNAME is the name of the class we are grokking for.
936 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
938 FLAGS contains bits saying what's special about today's
939 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
941 If FUNCTION is a destructor, then we must add the `auto-delete' field
942 as a second parameter. There is some hair associated with the fact
943 that we must "declare" this variable in the manner consistent with the
944 way the rest of the arguments were declared.
946 QUALS are the qualifiers for the this pointer. */
948 void
949 grokclassfn (ctype, function, flags, quals)
950 tree ctype, function;
951 enum overload_flags flags;
952 tree quals;
954 tree fn_name = DECL_NAME (function);
955 int this_quals = TYPE_UNQUALIFIED;
957 /* Even within an `extern "C"' block, members get C++ linkage. See
958 [dcl.link] for details. */
959 SET_DECL_LANGUAGE (function, lang_cplusplus);
961 if (fn_name == NULL_TREE)
963 error ("name missing for member function");
964 fn_name = get_identifier ("<anonymous>");
965 DECL_NAME (function) = fn_name;
968 if (quals)
969 this_quals = grok_method_quals (ctype, function, quals);
971 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
973 /* Must add the class instance variable up front. */
974 /* Right now we just make this a pointer. But later
975 we may wish to make it special. */
976 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
977 tree qual_type;
978 tree parm;
980 /* The `this' parameter is implicitly `const'; it cannot be
981 assigned to. */
982 this_quals |= TYPE_QUAL_CONST;
983 qual_type = cp_build_qualified_type (type, this_quals);
984 parm = build_artificial_parm (this_identifier, qual_type);
985 c_apply_type_quals_to_decl (this_quals, parm);
986 TREE_CHAIN (parm) = last_function_parms;
987 last_function_parms = parm;
990 DECL_ARGUMENTS (function) = last_function_parms;
991 DECL_CONTEXT (function) = ctype;
993 if (flags == DTOR_FLAG)
994 DECL_DESTRUCTOR_P (function) = 1;
996 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
997 maybe_retrofit_in_chrg (function);
999 if (flags == DTOR_FLAG)
1001 DECL_DESTRUCTOR_P (function) = 1;
1002 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1006 /* Create an ARRAY_REF, checking for the user doing things backwards
1007 along the way. */
1009 tree
1010 grok_array_decl (array_expr, index_exp)
1011 tree array_expr, index_exp;
1013 tree type = TREE_TYPE (array_expr);
1014 tree p1, p2, i1, i2;
1016 if (type == error_mark_node || index_exp == error_mark_node)
1017 return error_mark_node;
1018 if (processing_template_decl)
1019 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1020 array_expr, index_exp);
1022 if (type == NULL_TREE)
1024 /* Something has gone very wrong. Assume we are mistakenly reducing
1025 an expression instead of a declaration. */
1026 error ("parser may be lost: is there a '{' missing somewhere?");
1027 return NULL_TREE;
1030 if (TREE_CODE (type) == OFFSET_TYPE
1031 || TREE_CODE (type) == REFERENCE_TYPE)
1032 type = TREE_TYPE (type);
1034 /* If they have an `operator[]', use that. */
1035 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1036 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1037 array_expr, index_exp, NULL_TREE);
1039 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1040 is a little-known fact that, if `a' is an array and `i' is an
1041 int, you can write `i[a]', which means the same thing as `a[i]'. */
1043 if (TREE_CODE (type) == ARRAY_TYPE)
1044 p1 = array_expr;
1045 else
1046 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1048 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1049 p2 = index_exp;
1050 else
1051 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1053 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1054 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1056 if ((p1 && i2) && (i1 && p2))
1057 error ("ambiguous conversion for array subscript");
1059 if (p1 && i2)
1060 array_expr = p1, index_exp = i2;
1061 else if (i1 && p2)
1062 array_expr = p2, index_exp = i1;
1063 else
1065 error ("invalid types `%T[%T]' for array subscript",
1066 type, TREE_TYPE (index_exp));
1067 return error_mark_node;
1070 if (array_expr == error_mark_node || index_exp == error_mark_node)
1071 error ("ambiguous conversion for array subscript");
1073 return build_array_ref (array_expr, index_exp);
1076 /* Given the cast expression EXP, checking out its validity. Either return
1077 an error_mark_node if there was an unavoidable error, return a cast to
1078 void for trying to delete a pointer w/ the value 0, or return the
1079 call to delete. If DOING_VEC is 1, we handle things differently
1080 for doing an array delete. If DOING_VEC is 2, they gave us the
1081 array size as an argument to delete.
1082 Implements ARM $5.3.4. This is called from the parser. */
1084 tree
1085 delete_sanity (exp, size, doing_vec, use_global_delete)
1086 tree exp, size;
1087 int doing_vec, use_global_delete;
1089 tree t, type;
1090 /* For a regular vector delete (aka, no size argument) we will pass
1091 this down as a NULL_TREE into build_vec_delete. */
1092 tree maxindex = NULL_TREE;
1094 if (exp == error_mark_node)
1095 return exp;
1097 if (processing_template_decl)
1099 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1100 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1101 DELETE_EXPR_USE_VEC (t) = doing_vec;
1102 return t;
1105 if (TREE_CODE (exp) == OFFSET_REF)
1106 exp = resolve_offset_ref (exp);
1107 exp = convert_from_reference (exp);
1108 t = stabilize_reference (exp);
1109 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1111 if (t == NULL_TREE || t == error_mark_node)
1113 error ("type `%#T' argument given to `delete', expected pointer",
1114 TREE_TYPE (exp));
1115 return error_mark_node;
1118 if (doing_vec == 2)
1120 maxindex = cp_build_binary_op (MINUS_EXPR, size, integer_one_node);
1121 pedwarn ("anachronistic use of array size in vector delete");
1124 type = TREE_TYPE (t);
1126 /* As of Valley Forge, you can delete a pointer to const. */
1128 /* You can't delete functions. */
1129 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1131 error ("cannot delete a function. Only pointer-to-objects are valid arguments to `delete'");
1132 return error_mark_node;
1135 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1136 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1138 warning ("deleting `%T' is undefined", type);
1139 doing_vec = 0;
1142 /* An array can't have been allocated by new, so complain. */
1143 if (TREE_CODE (t) == ADDR_EXPR
1144 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1145 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1146 warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1148 /* Deleting a pointer with the value zero is valid and has no effect. */
1149 if (integer_zerop (t))
1150 return build1 (NOP_EXPR, void_type_node, t);
1152 if (doing_vec)
1153 return build_vec_delete (t, maxindex, sfk_deleting_destructor,
1154 use_global_delete);
1155 else
1157 if (IS_AGGR_TYPE (TREE_TYPE (type))
1158 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1160 /* Only do access checking here; we'll be calling op delete
1161 from the destructor. */
1162 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1163 LOOKUP_NORMAL, NULL_TREE);
1164 if (tmp == error_mark_node)
1165 return error_mark_node;
1168 return build_delete (type, t, sfk_deleting_destructor,
1169 LOOKUP_NORMAL, use_global_delete);
1173 /* Report an error if the indicated template declaration is not the
1174 sort of thing that should be a member template. */
1176 void
1177 check_member_template (tmpl)
1178 tree tmpl;
1180 tree decl;
1182 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1183 decl = DECL_TEMPLATE_RESULT (tmpl);
1185 if (TREE_CODE (decl) == FUNCTION_DECL
1186 || (TREE_CODE (decl) == TYPE_DECL
1187 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1189 if (current_function_decl)
1190 /* 14.5.2.2 [temp.mem]
1192 A local class shall not have member templates. */
1193 error ("invalid declaration of member template `%#D' in local class",
1194 decl);
1196 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1198 /* 14.5.2.3 [temp.mem]
1200 A member function template shall not be virtual. */
1201 error
1202 ("invalid use of `virtual' in template declaration of `%#D'",
1203 decl);
1204 DECL_VIRTUAL_P (decl) = 0;
1207 /* The debug-information generating code doesn't know what to do
1208 with member templates. */
1209 DECL_IGNORED_P (tmpl) = 1;
1211 else
1212 error ("template declaration of `%#D'", decl);
1215 /* Return true iff TYPE is a valid Java parameter or return type. */
1217 static int
1218 acceptable_java_type (type)
1219 tree type;
1221 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1222 return 1;
1223 if (TREE_CODE (type) == POINTER_TYPE || TREE_CODE (type) == REFERENCE_TYPE)
1225 type = TREE_TYPE (type);
1226 if (TREE_CODE (type) == RECORD_TYPE)
1228 tree args; int i;
1229 if (! TYPE_FOR_JAVA (type))
1230 return 0;
1231 if (! CLASSTYPE_TEMPLATE_INFO (type))
1232 return 1;
1233 args = CLASSTYPE_TI_ARGS (type);
1234 i = TREE_VEC_LENGTH (args);
1235 while (--i >= 0)
1237 type = TREE_VEC_ELT (args, i);
1238 if (TREE_CODE (type) == POINTER_TYPE)
1239 type = TREE_TYPE (type);
1240 if (! TYPE_FOR_JAVA (type))
1241 return 0;
1243 return 1;
1246 return 0;
1249 /* For a METHOD in a Java class CTYPE, return 1 if
1250 the parameter and return types are valid Java types.
1251 Otherwise, print appropriate error messages, and return 0. */
1254 check_java_method (method)
1255 tree method;
1257 int jerr = 0;
1258 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1259 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1260 if (! acceptable_java_type (ret_type))
1262 error ("Java method '%D' has non-Java return type `%T'",
1263 method, ret_type);
1264 jerr++;
1266 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1268 tree type = TREE_VALUE (arg_types);
1269 if (! acceptable_java_type (type))
1271 error ("Java method '%D' has non-Java parameter type `%T'",
1272 method, type);
1273 jerr++;
1276 return jerr ? 0 : 1;
1279 /* Sanity check: report error if this function FUNCTION is not
1280 really a member of the class (CTYPE) it is supposed to belong to.
1281 CNAME is the same here as it is for grokclassfn above. */
1283 tree
1284 check_classfn (ctype, function)
1285 tree ctype, function;
1287 tree fn_name = DECL_NAME (function);
1288 tree fndecl, fndecls;
1289 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1290 tree *methods = 0;
1291 tree *end = 0;
1293 if (DECL_USE_TEMPLATE (function)
1294 && !(TREE_CODE (function) == TEMPLATE_DECL
1295 && DECL_TEMPLATE_SPECIALIZATION (function))
1296 && is_member_template (DECL_TI_TEMPLATE (function)))
1297 /* Since this is a specialization of a member template,
1298 we're not going to find the declaration in the class.
1299 For example, in:
1301 struct S { template <typename T> void f(T); };
1302 template <> void S::f(int);
1304 we're not going to find `S::f(int)', but there's no
1305 reason we should, either. We let our callers know we didn't
1306 find the method, but we don't complain. */
1307 return NULL_TREE;
1309 if (method_vec != 0)
1311 methods = &TREE_VEC_ELT (method_vec, 0);
1312 end = TREE_VEC_END (method_vec);
1314 /* First suss out ctors and dtors. */
1315 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1316 && DECL_CONSTRUCTOR_P (function))
1317 goto got_it;
1318 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1319 && DECL_DESTRUCTOR_P (function))
1320 goto got_it;
1322 while (++methods != end && *methods)
1324 fndecl = *methods;
1325 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1327 got_it:
1328 for (fndecls = *methods; fndecls != NULL_TREE;
1329 fndecls = OVL_NEXT (fndecls))
1331 fndecl = OVL_CURRENT (fndecls);
1333 /* We cannot simply call decls_match because this
1334 doesn't work for static member functions that are
1335 pretending to be methods, and because the name
1336 may have been changed by asm("new_name"). */
1337 if (DECL_NAME (function) == DECL_NAME (fndecl))
1339 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1340 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1342 /* Get rid of the this parameter on functions that become
1343 static. */
1344 if (DECL_STATIC_FUNCTION_P (fndecl)
1345 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1346 p1 = TREE_CHAIN (p1);
1348 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1349 TREE_TYPE (TREE_TYPE (fndecl)))
1350 && compparms (p1, p2)
1351 && (DECL_TEMPLATE_SPECIALIZATION (function)
1352 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1353 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1354 || (DECL_TI_TEMPLATE (function)
1355 == DECL_TI_TEMPLATE (fndecl))))
1356 return fndecl;
1359 break; /* loser */
1364 if (methods != end && *methods)
1366 tree fndecl = *methods;
1367 error ("prototype for `%#D' does not match any in class `%T'",
1368 function, ctype);
1369 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1370 OVL_CURRENT (fndecl));
1371 while (fndecl = OVL_NEXT (fndecl), fndecl)
1372 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1374 else
1376 methods = 0;
1377 if (!COMPLETE_TYPE_P (ctype))
1378 incomplete_type_error (function, ctype);
1379 else
1380 error ("no `%#D' member function declared in class `%T'",
1381 function, ctype);
1384 /* If we did not find the method in the class, add it to avoid
1385 spurious errors (unless the CTYPE is not yet defined, in which
1386 case we'll only confuse ourselves when the function is declared
1387 properly within the class. */
1388 if (COMPLETE_TYPE_P (ctype))
1389 add_method (ctype, function, /*error_p=*/1);
1390 return NULL_TREE;
1393 /* We have just processed the DECL, which is a static data member.
1394 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1395 present, is the assembly-language name for the data member.
1396 FLAGS is as for cp_finish_decl. */
1398 void
1399 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1400 tree decl;
1401 tree init;
1402 tree asmspec_tree;
1403 int flags;
1405 my_friendly_assert (TREE_PUBLIC (decl), 0);
1407 DECL_CONTEXT (decl) = current_class_type;
1409 /* We cannot call pushdecl here, because that would fill in the
1410 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do
1411 the right thing, namely, to put this decl out straight away. */
1412 /* current_class_type can be NULL_TREE in case of error. */
1413 if (!asmspec_tree && current_class_type)
1414 DECL_INITIAL (decl) = error_mark_node;
1416 if (! processing_template_decl)
1418 if (!pending_statics)
1419 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1420 VARRAY_PUSH_TREE (pending_statics, decl);
1423 if (LOCAL_CLASS_P (current_class_type))
1424 pedwarn ("local class `%#T' shall not have static data member `%#D'",
1425 current_class_type, decl);
1427 /* Static consts need not be initialized in the class definition. */
1428 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1430 static int explained = 0;
1432 error ("initializer invalid for static member with constructor");
1433 if (!explained)
1435 error ("(an out of class initialization is required)");
1436 explained = 1;
1438 init = NULL_TREE;
1440 /* Force the compiler to know when an uninitialized static const
1441 member is being used. */
1442 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1443 TREE_USED (decl) = 1;
1444 DECL_INITIAL (decl) = init;
1445 DECL_IN_AGGR_P (decl) = 1;
1447 cp_finish_decl (decl, init, asmspec_tree, flags);
1450 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1451 of a structure component, returning a _DECL node.
1452 QUALS is a list of type qualifiers for this decl (such as for declaring
1453 const member functions).
1455 This is done during the parsing of the struct declaration.
1456 The _DECL nodes are chained together and the lot of them
1457 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1459 If class A defines that certain functions in class B are friends, then
1460 the way I have set things up, it is B who is interested in permission
1461 granted by A. However, it is in A's context that these declarations
1462 are parsed. By returning a void_type_node, class A does not attempt
1463 to incorporate the declarations of the friends within its structure.
1465 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1466 CHANGES TO CODE IN `start_method'. */
1468 tree
1469 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1470 tree declarator, declspecs, init, asmspec_tree, attrlist;
1472 tree value;
1473 const char *asmspec = 0;
1474 int flags = LOOKUP_ONLYCONVERTING;
1476 /* Convert () initializers to = initializers. */
1477 if (init == NULL_TREE && declarator != NULL_TREE
1478 && TREE_CODE (declarator) == CALL_EXPR
1479 && TREE_OPERAND (declarator, 0)
1480 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1481 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1482 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1484 /* It's invalid to try to initialize a data member using a
1485 functional notation, e.g.:
1487 struct S {
1488 static int i (3);
1491 Explain that to the user. */
1492 static int explained;
1494 error ("invalid data member initialization");
1495 if (!explained)
1497 error ("(use `=' to initialize static data members)");
1498 explained = 1;
1501 declarator = TREE_OPERAND (declarator, 0);
1502 flags = 0;
1505 if (declspecs == NULL_TREE
1506 && TREE_CODE (declarator) == SCOPE_REF
1507 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1509 /* Access declaration */
1510 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1512 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1513 pop_nested_class ();
1514 return do_class_using_decl (declarator);
1517 if (init
1518 && TREE_CODE (init) == TREE_LIST
1519 && TREE_VALUE (init) == error_mark_node
1520 && TREE_CHAIN (init) == NULL_TREE)
1521 init = NULL_TREE;
1523 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, &attrlist);
1524 if (! value || value == error_mark_node)
1525 /* friend or constructor went bad. */
1526 return value;
1527 if (TREE_TYPE (value) == error_mark_node)
1528 return error_mark_node;
1530 /* Pass friendly classes back. */
1531 if (TREE_CODE (value) == VOID_TYPE)
1532 return void_type_node;
1534 if (DECL_NAME (value) != NULL_TREE
1535 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1536 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1537 error ("member `%D' conflicts with virtual function table field name",
1538 value);
1540 /* Stash away type declarations. */
1541 if (TREE_CODE (value) == TYPE_DECL)
1543 DECL_NONLOCAL (value) = 1;
1544 DECL_CONTEXT (value) = current_class_type;
1546 if (CLASS_TYPE_P (TREE_TYPE (value)))
1547 CLASSTYPE_GOT_SEMICOLON (TREE_TYPE (value)) = 1;
1549 if (processing_template_decl)
1550 value = push_template_decl (value);
1552 return value;
1555 if (DECL_IN_AGGR_P (value))
1557 error ("`%D' is already defined in `%T'", value,
1558 DECL_CONTEXT (value));
1559 return void_type_node;
1562 if (asmspec_tree)
1563 asmspec = TREE_STRING_POINTER (asmspec_tree);
1565 if (init)
1567 if (TREE_CODE (value) == FUNCTION_DECL)
1569 grok_function_init (value, init);
1570 init = NULL_TREE;
1572 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1573 /* Already complained in grokdeclarator. */
1574 init = NULL_TREE;
1575 else
1577 /* We allow initializers to become parameters to base
1578 initializers. */
1579 if (TREE_CODE (init) == TREE_LIST)
1581 if (TREE_CHAIN (init) == NULL_TREE)
1582 init = TREE_VALUE (init);
1583 else
1584 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1587 if (TREE_CODE (init) == CONST_DECL)
1588 init = DECL_INITIAL (init);
1589 else if (TREE_READONLY_DECL_P (init))
1590 init = decl_constant_value (init);
1591 else if (TREE_CODE (init) == CONSTRUCTOR)
1592 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1593 if (init == error_mark_node)
1594 /* We must make this look different than `error_mark_node'
1595 because `decl_const_value' would mis-interpret it
1596 as only meaning that this VAR_DECL is defined. */
1597 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1598 else if (processing_template_decl)
1600 else if (! TREE_CONSTANT (init))
1602 /* We can allow references to things that are effectively
1603 static, since references are initialized with the address. */
1604 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1605 || (TREE_STATIC (init) == 0
1606 && (!DECL_P (init) || DECL_EXTERNAL (init) == 0)))
1608 error ("field initializer is not constant");
1609 init = error_mark_node;
1615 if (processing_template_decl && ! current_function_decl
1616 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1617 value = push_template_decl (value);
1619 if (attrlist)
1620 cplus_decl_attributes (&value, attrlist, 0);
1622 if (TREE_CODE (value) == VAR_DECL)
1624 finish_static_data_member_decl (value, init, asmspec_tree,
1625 flags);
1626 return value;
1628 if (TREE_CODE (value) == FIELD_DECL)
1630 if (asmspec)
1631 error ("`asm' specifiers are not permitted on non-static data members");
1632 if (DECL_INITIAL (value) == error_mark_node)
1633 init = error_mark_node;
1634 cp_finish_decl (value, init, NULL_TREE, flags);
1635 DECL_INITIAL (value) = init;
1636 DECL_IN_AGGR_P (value) = 1;
1637 return value;
1639 if (TREE_CODE (value) == FUNCTION_DECL)
1641 if (asmspec)
1643 /* This must override the asm specifier which was placed
1644 by grokclassfn. Lay this out fresh. */
1645 SET_DECL_RTL (value, NULL_RTX);
1646 SET_DECL_ASSEMBLER_NAME (value, get_identifier (asmspec));
1648 if (!DECL_FRIEND_P (value))
1649 grok_special_member_properties (value);
1651 cp_finish_decl (value, init, asmspec_tree, flags);
1653 /* Pass friends back this way. */
1654 if (DECL_FRIEND_P (value))
1655 return void_type_node;
1657 DECL_IN_AGGR_P (value) = 1;
1658 return value;
1660 abort ();
1661 /* NOTREACHED */
1662 return NULL_TREE;
1665 /* Like `grokfield', but for bitfields.
1666 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1668 tree
1669 grokbitfield (declarator, declspecs, width)
1670 tree declarator, declspecs, width;
1672 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1673 0, NULL);
1675 if (! value) return NULL_TREE; /* friends went bad. */
1677 /* Pass friendly classes back. */
1678 if (TREE_CODE (value) == VOID_TYPE)
1679 return void_type_node;
1681 if (TREE_CODE (value) == TYPE_DECL)
1683 error ("cannot declare `%D' to be a bit-field type", value);
1684 return NULL_TREE;
1687 /* Usually, finish_struct_1 catches bitfields with invalid types.
1688 But, in the case of bitfields with function type, we confuse
1689 ourselves into thinking they are member functions, so we must
1690 check here. */
1691 if (TREE_CODE (value) == FUNCTION_DECL)
1693 error ("cannot declare bit-field `%D' with function type",
1694 DECL_NAME (value));
1695 return NULL_TREE;
1698 if (DECL_IN_AGGR_P (value))
1700 error ("`%D' is already defined in the class %T", value,
1701 DECL_CONTEXT (value));
1702 return void_type_node;
1705 if (TREE_STATIC (value))
1707 error ("static member `%D' cannot be a bit-field", value);
1708 return NULL_TREE;
1710 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1712 if (width != error_mark_node)
1714 constant_expression_warning (width);
1715 DECL_INITIAL (value) = width;
1716 SET_DECL_C_BIT_FIELD (value);
1719 DECL_IN_AGGR_P (value) = 1;
1720 return value;
1723 tree
1724 grokoptypename (declspecs, declarator)
1725 tree declspecs, declarator;
1727 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL);
1728 return mangle_conv_op_name_for_type (t);
1731 /* When a function is declared with an initializer,
1732 do the right thing. Currently, there are two possibilities:
1734 class B
1736 public:
1737 // initialization possibility #1.
1738 virtual void f () = 0;
1739 int g ();
1742 class D1 : B
1744 public:
1745 int d1;
1746 // error, no f ();
1749 class D2 : B
1751 public:
1752 int d2;
1753 void f ();
1756 class D3 : B
1758 public:
1759 int d3;
1760 // initialization possibility #2
1761 void f () = B::f;
1766 static void
1767 grok_function_init (decl, init)
1768 tree decl;
1769 tree init;
1771 /* An initializer for a function tells how this function should
1772 be inherited. */
1773 tree type = TREE_TYPE (decl);
1775 if (TREE_CODE (type) == FUNCTION_TYPE)
1776 error ("initializer specified for non-member function `%D'", decl);
1777 else if (integer_zerop (init))
1778 DECL_PURE_VIRTUAL_P (decl) = 1;
1779 else
1780 error ("invalid initializer for virtual method `%D'", decl);
1783 void
1784 cplus_decl_attributes (decl, attributes, flags)
1785 tree *decl, attributes;
1786 int flags;
1788 if (*decl == NULL_TREE || *decl == void_type_node)
1789 return;
1791 if (TREE_CODE (*decl) == TEMPLATE_DECL)
1792 decl = &DECL_TEMPLATE_RESULT (*decl);
1794 decl_attributes (decl, attributes, flags);
1796 if (TREE_CODE (*decl) == TYPE_DECL)
1797 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (*decl), TREE_TYPE (*decl));
1800 /* CONSTRUCTOR_NAME:
1801 Return the name for the constructor (or destructor) for the
1802 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1803 IDENTIFIER_NODE. When given a template, this routine doesn't
1804 lose the specialization. */
1806 tree
1807 constructor_name_full (thing)
1808 tree thing;
1810 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1811 || TREE_CODE (thing) == BOUND_TEMPLATE_TEMPLATE_PARM
1812 || TREE_CODE (thing) == TYPENAME_TYPE)
1813 thing = TYPE_NAME (thing);
1814 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1816 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1817 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1818 else
1819 thing = TYPE_NAME (thing);
1821 if (TREE_CODE (thing) == TYPE_DECL
1822 || (TREE_CODE (thing) == TEMPLATE_DECL
1823 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1824 thing = DECL_NAME (thing);
1825 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1826 return thing;
1829 /* CONSTRUCTOR_NAME:
1830 Return the name for the constructor (or destructor) for the
1831 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1832 IDENTIFIER_NODE. When given a template, return the plain
1833 unspecialized name. */
1835 tree
1836 constructor_name (thing)
1837 tree thing;
1839 tree t;
1840 thing = constructor_name_full (thing);
1841 t = IDENTIFIER_TEMPLATE (thing);
1842 if (!t)
1843 return thing;
1844 return t;
1847 /* Defer the compilation of the FN until the end of compilation. */
1849 void
1850 defer_fn (fn)
1851 tree fn;
1853 if (DECL_DEFERRED_FN (fn))
1854 return;
1855 DECL_DEFERRED_FN (fn) = 1;
1856 if (!deferred_fns)
1857 VARRAY_TREE_INIT (deferred_fns, 32, "deferred_fns");
1859 VARRAY_PUSH_TREE (deferred_fns, fn);
1862 /* Hunts through the global anonymous union ANON_DECL, building
1863 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
1864 returns a VAR_DECL whose size is the same as the size of the
1865 ANON_DECL, if one is available.
1867 FIXME: we should really handle anonymous unions by binding the names
1868 of the members to COMPONENT_REFs rather than this kludge. */
1870 static tree
1871 build_anon_union_vars (anon_decl, elems, static_p, external_p)
1872 tree anon_decl;
1873 tree* elems;
1874 int static_p;
1875 int external_p;
1877 tree type = TREE_TYPE (anon_decl);
1878 tree main_decl = NULL_TREE;
1879 tree field;
1881 /* Rather than write the code to handle the non-union case,
1882 just give an error. */
1883 if (TREE_CODE (type) != UNION_TYPE)
1884 error ("anonymous struct not inside named type");
1886 for (field = TYPE_FIELDS (type);
1887 field != NULL_TREE;
1888 field = TREE_CHAIN (field))
1890 tree decl;
1892 if (DECL_ARTIFICIAL (field))
1893 continue;
1894 if (TREE_CODE (field) != FIELD_DECL)
1896 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
1897 field);
1898 continue;
1901 if (TREE_PRIVATE (field))
1902 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
1903 else if (TREE_PROTECTED (field))
1904 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
1906 if (DECL_NAME (field) == NULL_TREE
1907 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1909 decl = build_anon_union_vars (field, elems, static_p, external_p);
1910 if (!decl)
1911 continue;
1913 else if (DECL_NAME (field) == NULL_TREE)
1914 continue;
1915 else
1917 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
1918 /* tell `pushdecl' that this is not tentative. */
1919 DECL_INITIAL (decl) = error_mark_node;
1920 TREE_PUBLIC (decl) = 0;
1921 TREE_STATIC (decl) = static_p;
1922 DECL_EXTERNAL (decl) = external_p;
1923 decl = pushdecl (decl);
1924 DECL_INITIAL (decl) = NULL_TREE;
1927 /* Only write out one anon union element--choose the largest
1928 one. We used to try to find one the same size as the union,
1929 but that fails if the ABI forces us to align the union more
1930 strictly. */
1931 if (main_decl == NULL_TREE
1932 || tree_int_cst_lt (DECL_SIZE (main_decl), DECL_SIZE (decl)))
1934 if (main_decl)
1935 TREE_ASM_WRITTEN (main_decl) = 1;
1936 main_decl = decl;
1938 else
1939 /* ??? This causes there to be no debug info written out
1940 about this decl. */
1941 TREE_ASM_WRITTEN (decl) = 1;
1943 if (DECL_NAME (field) == NULL_TREE
1944 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1945 /* The remainder of the processing was already done in the
1946 recursive call. */
1947 continue;
1949 /* If there's a cleanup to do, it belongs in the
1950 TREE_PURPOSE of the following TREE_LIST. */
1951 *elems = tree_cons (NULL_TREE, decl, *elems);
1952 TREE_TYPE (*elems) = type;
1955 return main_decl;
1958 /* Finish off the processing of a UNION_TYPE structure. If the union is an
1959 anonymous union, then all members must be laid out together. PUBLIC_P
1960 is nonzero if this union is not declared static. */
1962 void
1963 finish_anon_union (anon_union_decl)
1964 tree anon_union_decl;
1966 tree type = TREE_TYPE (anon_union_decl);
1967 tree main_decl;
1968 int public_p = TREE_PUBLIC (anon_union_decl);
1969 int static_p = TREE_STATIC (anon_union_decl);
1970 int external_p = DECL_EXTERNAL (anon_union_decl);
1972 /* The VAR_DECL's context is the same as the TYPE's context. */
1973 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type));
1975 if (TYPE_FIELDS (type) == NULL_TREE)
1976 return;
1978 if (public_p)
1980 error ("namespace-scope anonymous aggregates must be static");
1981 return;
1984 main_decl = build_anon_union_vars (anon_union_decl,
1985 &DECL_ANON_UNION_ELEMS (anon_union_decl),
1986 static_p, external_p);
1988 if (main_decl == NULL_TREE)
1990 warning ("anonymous aggregate with no members");
1991 return;
1994 if (static_p)
1996 make_decl_rtl (main_decl, 0);
1997 COPY_DECL_RTL (main_decl, anon_union_decl);
1998 expand_anon_union_decl (anon_union_decl,
1999 NULL_TREE,
2000 DECL_ANON_UNION_ELEMS (anon_union_decl));
2002 else
2003 add_decl_stmt (anon_union_decl);
2006 /* Finish processing a builtin type TYPE. It's name is NAME,
2007 its fields are in the array FIELDS. LEN is the number of elements
2008 in FIELDS minus one, or put another way, it is the maximum subscript
2009 used in FIELDS.
2011 It is given the same alignment as ALIGN_TYPE. */
2013 void
2014 finish_builtin_type (type, name, fields, len, align_type)
2015 tree type;
2016 const char *name;
2017 tree fields[];
2018 int len;
2019 tree align_type;
2021 register int i;
2023 TYPE_FIELDS (type) = fields[0];
2024 for (i = 0; i < len; i++)
2026 layout_type (TREE_TYPE (fields[i]));
2027 DECL_FIELD_CONTEXT (fields[i]) = type;
2028 TREE_CHAIN (fields[i]) = fields[i+1];
2030 DECL_FIELD_CONTEXT (fields[i]) = type;
2031 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2032 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
2033 layout_type (type);
2034 #if 0 /* not yet, should get fixed properly later */
2035 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2036 #else
2037 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2038 #endif
2039 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2040 layout_decl (TYPE_NAME (type), 0);
2043 /* Auxiliary functions to make type signatures for
2044 `operator new' and `operator delete' correspond to
2045 what compiler will be expecting. */
2047 tree
2048 coerce_new_type (type)
2049 tree type;
2051 int e = 0;
2052 tree args = TYPE_ARG_TYPES (type);
2054 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2056 if (!same_type_p (TREE_TYPE (type), ptr_type_node))
2057 e = 1, error ("`operator new' must return type `%T'", ptr_type_node);
2059 if (!args || args == void_list_node
2060 || !same_type_p (TREE_VALUE (args), c_size_type_node))
2062 e = 2;
2063 if (args && args != void_list_node)
2064 args = TREE_CHAIN (args);
2065 pedwarn ("`operator new' takes type `size_t' (`%T') as first parameter", c_size_type_node);
2067 switch (e)
2069 case 2:
2070 args = tree_cons (NULL_TREE, c_size_type_node, args);
2071 /* FALLTHROUGH */
2072 case 1:
2073 type = build_exception_variant
2074 (build_function_type (ptr_type_node, args),
2075 TYPE_RAISES_EXCEPTIONS (type));
2076 /* FALLTHROUGH */
2077 default:;
2079 return type;
2082 tree
2083 coerce_delete_type (type)
2084 tree type;
2086 int e = 0;
2087 tree args = TYPE_ARG_TYPES (type);
2089 my_friendly_assert (TREE_CODE (type) == FUNCTION_TYPE, 20001107);
2091 if (!same_type_p (TREE_TYPE (type), void_type_node))
2092 e = 1, error ("`operator delete' must return type `%T'", void_type_node);
2094 if (!args || args == void_list_node
2095 || !same_type_p (TREE_VALUE (args), ptr_type_node))
2097 e = 2;
2098 if (args && args != void_list_node)
2099 args = TREE_CHAIN (args);
2100 error ("`operator delete' takes type `%T' as first parameter", ptr_type_node);
2102 switch (e)
2104 case 2:
2105 args = tree_cons (NULL_TREE, ptr_type_node, args);
2106 /* FALLTHROUGH */
2107 case 1:
2108 type = build_exception_variant
2109 (build_function_type (void_type_node, args),
2110 TYPE_RAISES_EXCEPTIONS (type));
2111 /* FALLTHROUGH */
2112 default:;
2115 return type;
2118 static void
2119 mark_vtable_entries (decl)
2120 tree decl;
2122 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2124 for (; entries; entries = TREE_CHAIN (entries))
2126 tree fnaddr = TREE_VALUE (entries);
2127 tree fn;
2129 if (TREE_CODE (fnaddr) != ADDR_EXPR
2130 && TREE_CODE (fnaddr) != FDESC_EXPR)
2131 /* This entry is an offset: a virtual base class offset, a
2132 virtual call offset, an RTTI offset, etc. */
2133 continue;
2135 fn = TREE_OPERAND (fnaddr, 0);
2136 TREE_ADDRESSABLE (fn) = 1;
2137 /* When we don't have vcall offsets, we output thunks whenever
2138 we output the vtables that contain them. With vcall offsets,
2139 we know all the thunks we'll need when we emit a virtual
2140 function, so we emit the thunks there instead. */
2141 if (DECL_THUNK_P (fn))
2142 use_thunk (fn, /*emit_p=*/0);
2143 mark_used (fn);
2147 /* Set DECL up to have the closest approximation of "initialized common"
2148 linkage available. */
2150 void
2151 comdat_linkage (decl)
2152 tree decl;
2154 if (flag_weak)
2155 make_decl_one_only (decl);
2156 else if (TREE_CODE (decl) == FUNCTION_DECL
2157 || (TREE_CODE (decl) == VAR_DECL && DECL_ARTIFICIAL (decl)))
2158 /* We can just emit function and compiler-generated variables
2159 statically; having multiple copies is (for the most part) only
2160 a waste of space.
2162 There are two correctness issues, however: the address of a
2163 template instantiation with external linkage should be the
2164 same, independent of what translation unit asks for the
2165 address, and this will not hold when we emit multiple copies of
2166 the function. However, there's little else we can do.
2168 Also, by default, the typeinfo implementation assumes that
2169 there will be only one copy of the string used as the name for
2170 each type. Therefore, if weak symbols are unavailable, the
2171 run-time library should perform a more conservative check; it
2172 should perform a string comparison, rather than an address
2173 comparison. */
2174 TREE_PUBLIC (decl) = 0;
2175 else
2177 /* Static data member template instantiations, however, cannot
2178 have multiple copies. */
2179 if (DECL_INITIAL (decl) == 0
2180 || DECL_INITIAL (decl) == error_mark_node)
2181 DECL_COMMON (decl) = 1;
2182 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2184 DECL_COMMON (decl) = 1;
2185 DECL_INITIAL (decl) = error_mark_node;
2187 else
2189 /* We can't do anything useful; leave vars for explicit
2190 instantiation. */
2191 DECL_EXTERNAL (decl) = 1;
2192 DECL_NOT_REALLY_EXTERN (decl) = 0;
2196 if (DECL_LANG_SPECIFIC (decl))
2197 DECL_COMDAT (decl) = 1;
2200 /* For win32 we also want to put explicit instantiations in
2201 linkonce sections, so that they will be merged with implicit
2202 instantiations; otherwise we get duplicate symbol errors. */
2204 void
2205 maybe_make_one_only (decl)
2206 tree decl;
2208 /* We used to say that this was not necessary on targets that support weak
2209 symbols, because the implicit instantiations will defer to the explicit
2210 one. However, that's not actually the case in SVR4; a strong definition
2211 after a weak one is an error. Also, not making explicit
2212 instantiations one_only means that we can end up with two copies of
2213 some template instantiations. */
2214 if (! flag_weak)
2215 return;
2217 /* We can't set DECL_COMDAT on functions, or finish_file will think
2218 we can get away with not emitting them if they aren't used. We need
2219 to for variables so that cp_finish_decl will update their linkage,
2220 because their DECL_INITIAL may not have been set properly yet. */
2222 make_decl_one_only (decl);
2224 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2225 DECL_COMDAT (decl) = 1;
2228 /* Returns the virtual function with which the vtable for TYPE is
2229 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
2231 static tree
2232 key_method (type)
2233 tree type;
2235 tree method;
2237 if (TYPE_FOR_JAVA (type)
2238 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2239 || CLASSTYPE_INTERFACE_KNOWN (type))
2240 return NULL_TREE;
2242 for (method = TYPE_METHODS (type); method != NULL_TREE;
2243 method = TREE_CHAIN (method))
2244 if (DECL_VINDEX (method) != NULL_TREE
2245 && ! DECL_DECLARED_INLINE_P (method)
2246 && (! DECL_PURE_VIRTUAL_P (method)
2247 #if 0
2248 /* This would be nice, but we didn't think of it in time. */
2249 || DECL_DESTRUCTOR_P (method)
2250 #endif
2252 return method;
2254 return NULL_TREE;
2257 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2258 based on TYPE and other static flags.
2260 Note that anything public is tagged TREE_PUBLIC, whether
2261 it's public in this file or in another one. */
2263 void
2264 import_export_vtable (decl, type, final)
2265 tree decl, type;
2266 int final;
2268 if (DECL_INTERFACE_KNOWN (decl))
2269 return;
2271 if (TYPE_FOR_JAVA (type))
2273 TREE_PUBLIC (decl) = 1;
2274 DECL_EXTERNAL (decl) = 1;
2275 DECL_INTERFACE_KNOWN (decl) = 1;
2277 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2279 TREE_PUBLIC (decl) = 1;
2280 DECL_EXTERNAL (decl) = CLASSTYPE_INTERFACE_ONLY (type);
2281 DECL_INTERFACE_KNOWN (decl) = 1;
2283 else
2285 /* We can only wait to decide if we have real non-inline virtual
2286 functions in our class, or if we come from a template. */
2288 int found = (CLASSTYPE_TEMPLATE_INSTANTIATION (type)
2289 || key_method (type));
2291 if (final || ! found)
2293 comdat_linkage (decl);
2294 DECL_EXTERNAL (decl) = 0;
2296 else
2298 TREE_PUBLIC (decl) = 1;
2299 DECL_EXTERNAL (decl) = 1;
2304 /* Determine whether or not we want to specifically import or export CTYPE,
2305 using various heuristics. */
2307 static void
2308 import_export_class (ctype)
2309 tree ctype;
2311 /* -1 for imported, 1 for exported. */
2312 int import_export = 0;
2314 /* It only makes sense to call this function at EOF. The reason is
2315 that this function looks at whether or not the first non-inline
2316 non-abstract virtual member function has been defined in this
2317 translation unit. But, we can't possibly know that until we've
2318 seen the entire translation unit. */
2319 my_friendly_assert (at_eof, 20000226);
2321 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2322 return;
2324 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2325 we will have CLASSTYPE_INTERFACE_ONLY set but not
2326 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2327 heuristic because someone will supply a #pragma implementation
2328 elsewhere, and deducing it here would produce a conflict. */
2329 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2330 return;
2332 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2333 import_export = -1;
2334 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2335 import_export = 1;
2337 /* If we got -fno-implicit-templates, we import template classes that
2338 weren't explicitly instantiated. */
2339 if (import_export == 0
2340 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2341 && ! flag_implicit_templates)
2342 import_export = -1;
2344 /* Base our import/export status on that of the first non-inline,
2345 non-pure virtual function, if any. */
2346 if (import_export == 0
2347 && TYPE_POLYMORPHIC_P (ctype))
2349 tree method = key_method (ctype);
2350 if (method)
2351 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2354 #ifdef MULTIPLE_SYMBOL_SPACES
2355 if (import_export == -1)
2356 import_export = 0;
2357 #endif
2359 if (import_export)
2361 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2362 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2366 /* We need to describe to the assembler the relationship between
2367 a vtable and the vtable of the parent class. */
2369 static void
2370 output_vtable_inherit (vars)
2371 tree vars;
2373 tree parent;
2374 rtx child_rtx, parent_rtx;
2376 child_rtx = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2378 parent = binfo_for_vtable (vars);
2380 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2381 parent_rtx = const0_rtx;
2382 else if (parent)
2384 parent = get_vtbl_decl_for_binfo (TYPE_BINFO (BINFO_TYPE (parent)));
2385 parent_rtx = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2387 else
2388 abort ();
2390 assemble_vtable_inherit (child_rtx, parent_rtx);
2393 static int
2394 finish_vtable_vardecl (t, data)
2395 tree *t;
2396 void *data ATTRIBUTE_UNUSED;
2398 tree vars = *t;
2399 tree ctype = DECL_CONTEXT (vars);
2400 import_export_class (ctype);
2401 import_export_vtable (vars, ctype, 1);
2403 if (! DECL_EXTERNAL (vars)
2404 && DECL_NEEDED_P (vars)
2405 && ! TREE_ASM_WRITTEN (vars))
2407 if (TREE_TYPE (vars) == void_type_node)
2408 /* It is a dummy vtable made by get_vtable_decl. Ignore it. */
2409 return 0;
2411 /* Write it out. */
2412 mark_vtable_entries (vars);
2413 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2414 store_init_value (vars, DECL_INITIAL (vars));
2416 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2418 /* Mark the VAR_DECL node representing the vtable itself as a
2419 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2420 It is rather important that such things be ignored because
2421 any effort to actually generate DWARF for them will run
2422 into trouble when/if we encounter code like:
2424 #pragma interface
2425 struct S { virtual void member (); };
2427 because the artificial declaration of the vtable itself (as
2428 manufactured by the g++ front end) will say that the vtable
2429 is a static member of `S' but only *after* the debug output
2430 for the definition of `S' has already been output. This causes
2431 grief because the DWARF entry for the definition of the vtable
2432 will try to refer back to an earlier *declaration* of the
2433 vtable as a static member of `S' and there won't be one.
2434 We might be able to arrange to have the "vtable static member"
2435 attached to the member list for `S' before the debug info for
2436 `S' get written (which would solve the problem) but that would
2437 require more intrusive changes to the g++ front end. */
2439 DECL_IGNORED_P (vars) = 1;
2442 /* Always make vtables weak. */
2443 if (flag_weak)
2444 comdat_linkage (vars);
2446 rest_of_decl_compilation (vars, NULL, 1, 1);
2448 if (flag_vtable_gc)
2449 output_vtable_inherit (vars);
2451 /* Because we're only doing syntax-checking, we'll never end up
2452 actually marking the variable as written. */
2453 if (flag_syntax_only)
2454 TREE_ASM_WRITTEN (vars) = 1;
2456 /* Since we're writing out the vtable here, also write the debug
2457 info. */
2458 note_debug_info_needed (ctype);
2460 return 1;
2463 /* If the references to this class' vtables were optimized away, still
2464 emit the appropriate debugging information. See dfs_debug_mark. */
2465 if (DECL_COMDAT (vars)
2466 && CLASSTYPE_DEBUG_REQUESTED (ctype))
2467 note_debug_info_needed (ctype);
2469 return 0;
2472 static int
2473 prune_vtable_vardecl (t, data)
2474 tree *t;
2475 void *data ATTRIBUTE_UNUSED;
2477 *t = TREE_CHAIN (*t);
2478 return 1;
2481 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2482 inline function or template instantiation at end-of-file. */
2484 void
2485 import_export_decl (decl)
2486 tree decl;
2488 if (DECL_INTERFACE_KNOWN (decl))
2489 return;
2491 if (DECL_TEMPLATE_INSTANTIATION (decl)
2492 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2494 DECL_NOT_REALLY_EXTERN (decl) = 1;
2495 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2496 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2497 && (flag_implicit_templates
2498 || (flag_implicit_inline_templates
2499 && DECL_DECLARED_INLINE_P (decl))))
2501 if (!TREE_PUBLIC (decl))
2502 /* Templates are allowed to have internal linkage. See
2503 [basic.link]. */
2505 else
2506 comdat_linkage (decl);
2508 else
2509 DECL_NOT_REALLY_EXTERN (decl) = 0;
2511 else if (DECL_FUNCTION_MEMBER_P (decl))
2513 if (!DECL_DECLARED_INLINE_P (decl))
2515 tree ctype = DECL_CONTEXT (decl);
2516 import_export_class (ctype);
2517 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2519 DECL_NOT_REALLY_EXTERN (decl)
2520 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2521 || (DECL_DECLARED_INLINE_P (decl)
2522 && ! flag_implement_inlines
2523 && !DECL_VINDEX (decl)));
2525 /* Always make artificials weak. */
2526 if (DECL_ARTIFICIAL (decl) && flag_weak)
2527 comdat_linkage (decl);
2528 else
2529 maybe_make_one_only (decl);
2532 else
2533 comdat_linkage (decl);
2535 else if (tinfo_decl_p (decl, 0))
2537 tree ctype = TREE_TYPE (DECL_NAME (decl));
2539 if (IS_AGGR_TYPE (ctype))
2540 import_export_class (ctype);
2542 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2543 && TYPE_POLYMORPHIC_P (ctype)
2544 /* If -fno-rtti, we're not necessarily emitting this stuff with
2545 the class, so go ahead and emit it now. This can happen
2546 when a class is used in exception handling. */
2547 && flag_rtti
2548 /* If the type is a cv-qualified variant of a type, then we
2549 must emit the tinfo function in this translation unit
2550 since it will not be emitted when the vtable for the type
2551 is output (which is when the unqualified version is
2552 generated). */
2553 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2555 DECL_NOT_REALLY_EXTERN (decl)
2556 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2557 || (DECL_DECLARED_INLINE_P (decl)
2558 && ! flag_implement_inlines
2559 && !DECL_VINDEX (decl)));
2561 /* Always make artificials weak. */
2562 if (flag_weak)
2563 comdat_linkage (decl);
2565 else if (TYPE_BUILT_IN (ctype)
2566 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2567 DECL_NOT_REALLY_EXTERN (decl) = 0;
2568 else
2569 comdat_linkage (decl);
2571 else
2572 comdat_linkage (decl);
2574 DECL_INTERFACE_KNOWN (decl) = 1;
2577 tree
2578 build_cleanup (decl)
2579 tree decl;
2581 tree temp;
2582 tree type = TREE_TYPE (decl);
2584 if (TREE_CODE (type) == ARRAY_TYPE)
2585 temp = decl;
2586 else
2588 mark_addressable (decl);
2589 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2591 temp = build_delete (TREE_TYPE (temp), temp,
2592 sfk_complete_destructor,
2593 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2594 return temp;
2597 /* Returns the initialization guard variable for the variable DECL,
2598 which has static storage duration. */
2600 tree
2601 get_guard (decl)
2602 tree decl;
2604 tree sname;
2605 tree guard;
2607 sname = mangle_guard_variable (decl);
2608 guard = IDENTIFIER_GLOBAL_VALUE (sname);
2609 if (! guard)
2611 tree guard_type;
2613 /* We use a type that is big enough to contain a mutex as well
2614 as an integer counter. */
2615 guard_type = long_long_integer_type_node;
2616 guard = build_decl (VAR_DECL, sname, guard_type);
2618 /* The guard should have the same linkage as what it guards. */
2619 TREE_PUBLIC (guard) = TREE_PUBLIC (decl);
2620 TREE_STATIC (guard) = TREE_STATIC (decl);
2621 DECL_COMMON (guard) = DECL_COMMON (decl);
2622 DECL_ONE_ONLY (guard) = DECL_ONE_ONLY (decl);
2623 if (TREE_PUBLIC (decl))
2624 DECL_WEAK (guard) = DECL_WEAK (decl);
2626 DECL_ARTIFICIAL (guard) = 1;
2627 TREE_USED (guard) = 1;
2628 pushdecl_top_level (guard);
2629 cp_finish_decl (guard, NULL_TREE, NULL_TREE, 0);
2631 return guard;
2634 /* Return those bits of the GUARD variable that should be set when the
2635 guarded entity is actually initialized. */
2637 static tree
2638 get_guard_bits (guard)
2639 tree guard;
2641 /* We only set the first byte of the guard, in order to leave room
2642 for a mutex in the high-order bits. */
2643 guard = build1 (ADDR_EXPR,
2644 build_pointer_type (TREE_TYPE (guard)),
2645 guard);
2646 guard = build1 (NOP_EXPR,
2647 build_pointer_type (char_type_node),
2648 guard);
2649 guard = build1 (INDIRECT_REF, char_type_node, guard);
2651 return guard;
2654 /* Return an expression which determines whether or not the GUARD
2655 variable has already been initialized. */
2657 tree
2658 get_guard_cond (guard)
2659 tree guard;
2661 tree guard_value;
2663 /* Check to see if the GUARD is zero. */
2664 guard = get_guard_bits (guard);
2665 guard_value = integer_zero_node;
2666 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard)))
2667 guard_value = convert (TREE_TYPE (guard), guard_value);
2668 return cp_build_binary_op (EQ_EXPR, guard, guard_value);
2671 /* Return an expression which sets the GUARD variable, indicating that
2672 the variable being guarded has been initialized. */
2674 tree
2675 set_guard (guard)
2676 tree guard;
2678 tree guard_init;
2680 /* Set the GUARD to one. */
2681 guard = get_guard_bits (guard);
2682 guard_init = integer_one_node;
2683 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard)))
2684 guard_init = convert (TREE_TYPE (guard), guard_init);
2685 return build_modify_expr (guard, NOP_EXPR, guard_init);
2688 /* Start the process of running a particular set of global constructors
2689 or destructors. Subroutine of do_[cd]tors. */
2691 static tree
2692 start_objects (method_type, initp)
2693 int method_type, initp;
2695 tree fnname;
2696 tree body;
2697 char type[10];
2699 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2701 if (initp != DEFAULT_INIT_PRIORITY)
2703 char joiner;
2705 #ifdef JOINER
2706 joiner = JOINER;
2707 #else
2708 joiner = '_';
2709 #endif
2711 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2713 else
2714 sprintf (type, "%c", method_type);
2716 fnname = get_file_function_name_long (type);
2718 start_function (void_list_node,
2719 make_call_declarator (fnname, void_list_node, NULL_TREE,
2720 NULL_TREE),
2721 NULL_TREE, SF_DEFAULT);
2723 /* It can be a static function as long as collect2 does not have
2724 to scan the object file to find its ctor/dtor routine. */
2725 TREE_PUBLIC (current_function_decl) = ! targetm.have_ctors_dtors;
2727 /* Mark this declaration as used to avoid spurious warnings. */
2728 TREE_USED (current_function_decl) = 1;
2730 /* Mark this function as a global constructor or destructor. */
2731 if (method_type == 'I')
2732 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2733 else
2734 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2735 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2737 body = begin_compound_stmt (/*has_no_scope=*/0);
2739 /* We cannot allow these functions to be elided, even if they do not
2740 have external linkage. And, there's no point in deferring
2741 copmilation of thes functions; they're all going to have to be
2742 out anyhow. */
2743 current_function_cannot_inline
2744 = "static constructors and destructors cannot be inlined";
2746 return body;
2749 /* Finish the process of running a particular set of global constructors
2750 or destructors. Subroutine of do_[cd]tors. */
2752 static void
2753 finish_objects (method_type, initp, body)
2754 int method_type, initp;
2755 tree body;
2757 tree fn;
2759 /* Finish up. */
2760 finish_compound_stmt (/*has_no_scope=*/0, body);
2761 fn = finish_function (0);
2762 expand_body (fn);
2764 /* When only doing semantic analysis, and no RTL generation, we
2765 can't call functions that directly emit assembly code; there is
2766 no assembly file in which to put the code. */
2767 if (flag_syntax_only)
2768 return;
2770 if (targetm.have_ctors_dtors)
2772 rtx fnsym = XEXP (DECL_RTL (fn), 0);
2773 if (method_type == 'I')
2774 (* targetm.asm_out.constructor) (fnsym, initp);
2775 else
2776 (* targetm.asm_out.destructor) (fnsym, initp);
2780 /* The names of the parameters to the function created to handle
2781 initializations and destructions for objects with static storage
2782 duration. */
2783 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2784 #define PRIORITY_IDENTIFIER "__priority"
2786 /* The name of the function we create to handle initializations and
2787 destructions for objects with static storage duration. */
2788 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2790 /* The declaration for the __INITIALIZE_P argument. */
2791 static tree initialize_p_decl;
2793 /* The declaration for the __PRIORITY argument. */
2794 static tree priority_decl;
2796 /* The declaration for the static storage duration function. */
2797 static tree ssdf_decl;
2799 /* All the static storage duration functions created in this
2800 translation unit. */
2801 static varray_type ssdf_decls;
2803 /* A map from priority levels to information about that priority
2804 level. There may be many such levels, so efficient lookup is
2805 important. */
2806 static splay_tree priority_info_map;
2808 /* Begins the generation of the function that will handle all
2809 initialization and destruction of objects with static storage
2810 duration. The function generated takes two parameters of type
2811 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2812 non-zero, it performs initializations. Otherwise, it performs
2813 destructions. It only performs those initializations or
2814 destructions with the indicated __PRIORITY. The generated function
2815 returns no value.
2817 It is assumed that this function will only be called once per
2818 translation unit. */
2820 static tree
2821 start_static_storage_duration_function ()
2823 static unsigned ssdf_number;
2825 tree parm_types;
2826 tree type;
2827 tree body;
2828 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2830 /* Create the identifier for this function. It will be of the form
2831 SSDF_IDENTIFIER_<number>. */
2832 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2833 if (ssdf_number == 0)
2835 /* Overflow occurred. That means there are at least 4 billion
2836 initialization functions. */
2837 sorry ("too many initialization functions required");
2838 abort ();
2841 /* Create the parameters. */
2842 parm_types = void_list_node;
2843 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2844 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2845 type = build_function_type (void_type_node, parm_types);
2847 /* Create the FUNCTION_DECL itself. */
2848 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2849 get_identifier (id),
2850 type);
2851 TREE_PUBLIC (ssdf_decl) = 0;
2852 DECL_ARTIFICIAL (ssdf_decl) = 1;
2854 /* Put this function in the list of functions to be called from the
2855 static constructors and destructors. */
2856 if (!ssdf_decls)
2858 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2860 /* Take this opportunity to initialize the map from priority
2861 numbers to information about that priority level. */
2862 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2863 /*delete_key_fn=*/0,
2864 /*delete_value_fn=*/
2865 (splay_tree_delete_value_fn) &free);
2867 /* We always need to generate functions for the
2868 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2869 priorities later, we'll be sure to find the
2870 DEFAULT_INIT_PRIORITY. */
2871 get_priority_info (DEFAULT_INIT_PRIORITY);
2874 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2876 /* Create the argument list. */
2877 initialize_p_decl = build_decl (PARM_DECL,
2878 get_identifier (INITIALIZE_P_IDENTIFIER),
2879 integer_type_node);
2880 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2881 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
2882 TREE_USED (initialize_p_decl) = 1;
2883 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
2884 integer_type_node);
2885 DECL_CONTEXT (priority_decl) = ssdf_decl;
2886 DECL_ARG_TYPE (priority_decl) = integer_type_node;
2887 TREE_USED (priority_decl) = 1;
2889 TREE_CHAIN (initialize_p_decl) = priority_decl;
2890 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2892 /* Put the function in the global scope. */
2893 pushdecl (ssdf_decl);
2895 /* Start the function itself. This is equivalent to declarating the
2896 function as:
2898 static void __ssdf (int __initialize_p, init __priority_p);
2900 It is static because we only need to call this function from the
2901 various constructor and destructor functions for this module. */
2902 start_function (/*specs=*/NULL_TREE,
2903 ssdf_decl,
2904 /*attrs=*/NULL_TREE,
2905 SF_PRE_PARSED);
2907 /* Set up the scope of the outermost block in the function. */
2908 body = begin_compound_stmt (/*has_no_scope=*/0);
2910 /* This function must not be deferred because we are depending on
2911 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
2912 current_function_cannot_inline
2913 = "static storage duration functions cannot be inlined";
2915 return body;
2918 /* Finish the generation of the function which performs initialization
2919 and destruction of objects with static storage duration. After
2920 this point, no more such objects can be created. */
2922 static void
2923 finish_static_storage_duration_function (body)
2924 tree body;
2926 /* Close out the function. */
2927 finish_compound_stmt (/*has_no_scope=*/0, body);
2928 expand_body (finish_function (0));
2931 /* Return the information about the indicated PRIORITY level. If no
2932 code to handle this level has yet been generated, generate the
2933 appropriate prologue. */
2935 static priority_info
2936 get_priority_info (priority)
2937 int priority;
2939 priority_info pi;
2940 splay_tree_node n;
2942 n = splay_tree_lookup (priority_info_map,
2943 (splay_tree_key) priority);
2944 if (!n)
2946 /* Create a new priority information structure, and insert it
2947 into the map. */
2948 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
2949 pi->initializations_p = 0;
2950 pi->destructions_p = 0;
2951 splay_tree_insert (priority_info_map,
2952 (splay_tree_key) priority,
2953 (splay_tree_value) pi);
2955 else
2956 pi = (priority_info) n->value;
2958 return pi;
2961 /* Set up to handle the initialization or destruction of DECL. If
2962 INITP is non-zero, we are initializing the variable. Otherwise, we
2963 are destroying it. */
2965 static tree
2966 start_static_initialization_or_destruction (decl, initp)
2967 tree decl;
2968 int initp;
2970 tree guard_if_stmt = NULL_TREE;
2971 int priority;
2972 tree cond;
2973 tree guard;
2974 tree init_cond;
2975 priority_info pi;
2977 /* Figure out the priority for this declaration. */
2978 priority = DECL_INIT_PRIORITY (decl);
2979 if (!priority)
2980 priority = DEFAULT_INIT_PRIORITY;
2982 /* Remember that we had an initialization or finalization at this
2983 priority. */
2984 pi = get_priority_info (priority);
2985 if (initp)
2986 pi->initializations_p = 1;
2987 else
2988 pi->destructions_p = 1;
2990 /* Trick the compiler into thinking we are at the file and line
2991 where DECL was declared so that error-messages make sense, and so
2992 that the debugger will show somewhat sensible file and line
2993 information. */
2994 input_filename = DECL_SOURCE_FILE (decl);
2995 lineno = DECL_SOURCE_LINE (decl);
2997 /* Because of:
2999 [class.access.spec]
3001 Access control for implicit calls to the constructors,
3002 the conversion functions, or the destructor called to
3003 create and destroy a static data member is performed as
3004 if these calls appeared in the scope of the member's
3005 class.
3007 we pretend we are in a static member function of the class of
3008 which the DECL is a member. */
3009 if (member_p (decl))
3011 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3012 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3015 /* Conditionalize this initialization on being in the right priority
3016 and being initializing/finalizing appropriately. */
3017 guard_if_stmt = begin_if_stmt ();
3018 cond = cp_build_binary_op (EQ_EXPR,
3019 priority_decl,
3020 build_int_2 (priority, 0));
3021 init_cond = initp ? integer_one_node : integer_zero_node;
3022 init_cond = cp_build_binary_op (EQ_EXPR,
3023 initialize_p_decl,
3024 init_cond);
3025 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3027 /* Assume we don't need a guard. */
3028 guard = NULL_TREE;
3029 /* We need a guard if this is an object with external linkage that
3030 might be initialized in more than one place. (For example, a
3031 static data member of a template, when the data member requires
3032 construction.) */
3033 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3034 || DECL_ONE_ONLY (decl)
3035 || DECL_WEAK (decl)))
3037 tree guard_cond;
3039 guard = get_guard (decl);
3041 /* When using __cxa_atexit, we just check the GUARD as we would
3042 for a local static. */
3043 if (flag_use_cxa_atexit)
3045 /* When using __cxa_atexit, we never try to destroy
3046 anything from a static destructor. */
3047 my_friendly_assert (initp, 20000629);
3048 guard_cond = get_guard_cond (guard);
3050 /* If we don't have __cxa_atexit, then we will be running
3051 destructors from .fini sections, or their equivalents. So,
3052 we need to know how many times we've tried to initialize this
3053 object. We do initializations only if the GUARD is zero,
3054 i.e., if we are the first to initialize the variable. We do
3055 destructions only if the GUARD is one, i.e., if we are the
3056 last to destroy the variable. */
3057 else if (initp)
3058 guard_cond
3059 = cp_build_binary_op (EQ_EXPR,
3060 build_unary_op (PREINCREMENT_EXPR,
3061 guard,
3062 /*noconvert=*/1),
3063 integer_one_node);
3064 else
3065 guard_cond
3066 = cp_build_binary_op (EQ_EXPR,
3067 build_unary_op (PREDECREMENT_EXPR,
3068 guard,
3069 /*noconvert=*/1),
3070 integer_zero_node);
3072 cond = cp_build_binary_op (TRUTH_ANDIF_EXPR, cond, guard_cond);
3075 finish_if_stmt_cond (cond, guard_if_stmt);
3077 /* If we're using __cxa_atexit, we have not already set the GUARD,
3078 so we must do so now. */
3079 if (guard && initp && flag_use_cxa_atexit)
3080 finish_expr_stmt (set_guard (guard));
3082 return guard_if_stmt;
3085 /* We've just finished generating code to do an initialization or
3086 finalization. GUARD_IF_STMT is the if-statement we used to guard
3087 the initialization. */
3089 static void
3090 finish_static_initialization_or_destruction (guard_if_stmt)
3091 tree guard_if_stmt;
3093 finish_then_clause (guard_if_stmt);
3094 finish_if_stmt ();
3096 /* Now that we're done with DECL we don't need to pretend to be a
3097 member of its class any longer. */
3098 DECL_CONTEXT (current_function_decl) = NULL_TREE;
3099 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3102 /* Generate code to do the static initialization of DECL. The
3103 initialization is INIT. If DECL may be initialized more than once
3104 in different object files, GUARD is the guard variable to
3105 check. PRIORITY is the priority for the initialization. */
3107 static void
3108 do_static_initialization (decl, init)
3109 tree decl;
3110 tree init;
3112 tree expr;
3113 tree guard_if_stmt;
3115 /* Set up for the initialization. */
3116 guard_if_stmt
3117 = start_static_initialization_or_destruction (decl,
3118 /*initp=*/1);
3120 /* Do the initialization itself. */
3121 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3122 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3123 expr = build_aggr_init (decl, init, 0);
3124 else
3126 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3127 TREE_SIDE_EFFECTS (expr) = 1;
3129 finish_expr_stmt (expr);
3131 /* If we're using __cxa_atexit, register a a function that calls the
3132 destructor for the object. */
3133 if (flag_use_cxa_atexit)
3134 register_dtor_fn (decl);
3136 /* Finsh up. */
3137 finish_static_initialization_or_destruction (guard_if_stmt);
3140 /* Generate code to do the static destruction of DECL. If DECL may be
3141 initialized more than once in different object files, GUARD is the
3142 guard variable to check. PRIORITY is the priority for the
3143 destruction. */
3145 static void
3146 do_static_destruction (decl)
3147 tree decl;
3149 tree guard_if_stmt;
3151 /* If we're using __cxa_atexit, then destructors are registered
3152 immediately after objects are initialized. */
3153 my_friendly_assert (!flag_use_cxa_atexit, 20000121);
3155 /* If we don't need a destructor, there's nothing to do. */
3156 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
3157 return;
3159 /* Actually do the destruction. */
3160 guard_if_stmt = start_static_initialization_or_destruction (decl,
3161 /*initp=*/0);
3162 finish_expr_stmt (build_cleanup (decl));
3163 finish_static_initialization_or_destruction (guard_if_stmt);
3166 /* VARS is a list of variables with static storage duration which may
3167 need initialization and/or finalization. Remove those variables
3168 that don't really need to be initialized or finalized, and return
3169 the resulting list. The order in which the variables appear in
3170 VARS is in reverse order of the order in which they should actually
3171 be initialized. The list we return is in the unreversed order;
3172 i.e., the first variable should be initialized first. */
3174 static tree
3175 prune_vars_needing_no_initialization (vars)
3176 tree vars;
3178 tree var;
3179 tree result;
3181 for (var = vars, result = NULL_TREE;
3182 var;
3183 var = TREE_CHAIN (var))
3185 tree decl = TREE_VALUE (var);
3186 tree init = TREE_PURPOSE (var);
3188 /* Deal gracefully with error. */
3189 if (decl == error_mark_node)
3190 continue;
3192 /* The only things that can be initialized are variables. */
3193 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3195 /* If this object is not defined, we don't need to do anything
3196 here. */
3197 if (DECL_EXTERNAL (decl))
3198 continue;
3200 /* Also, if the initializer already contains errors, we can bail
3201 out now. */
3202 if (init && TREE_CODE (init) == TREE_LIST
3203 && value_member (error_mark_node, init))
3204 continue;
3206 /* This variable is going to need initialization and/or
3207 finalization, so we add it to the list. */
3208 result = tree_cons (init, decl, result);
3211 return result;
3214 /* Make sure we have told the back end about all the variables in
3215 VARS. */
3217 static void
3218 write_out_vars (vars)
3219 tree vars;
3221 tree v;
3223 for (v = vars; v; v = TREE_CHAIN (v))
3224 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3225 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3228 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3229 (otherwise) that will initialize all gobal objects with static
3230 storage duration having the indicated PRIORITY. */
3232 static void
3233 generate_ctor_or_dtor_function (constructor_p, priority)
3234 int constructor_p;
3235 int priority;
3237 char function_key;
3238 tree arguments;
3239 tree body;
3240 size_t i;
3242 /* We use `I' to indicate initialization and `D' to indicate
3243 destruction. */
3244 if (constructor_p)
3245 function_key = 'I';
3246 else
3247 function_key = 'D';
3249 /* Begin the function. */
3250 body = start_objects (function_key, priority);
3252 /* Call the static storage duration function with appropriate
3253 arguments. */
3254 for (i = 0; i < ssdf_decls->elements_used; ++i)
3256 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3257 NULL_TREE);
3258 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3259 arguments);
3260 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3261 arguments));
3264 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3265 calls to any functions marked with attributes indicating that
3266 they should be called at initialization- or destruction-time. */
3267 if (priority == DEFAULT_INIT_PRIORITY)
3269 tree fns;
3271 for (fns = constructor_p ? static_ctors : static_dtors;
3272 fns;
3273 fns = TREE_CHAIN (fns))
3274 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3277 /* Close out the function. */
3278 finish_objects (function_key, priority, body);
3281 /* Generate constructor and destructor functions for the priority
3282 indicated by N. */
3284 static int
3285 generate_ctor_and_dtor_functions_for_priority (n, data)
3286 splay_tree_node n;
3287 void *data ATTRIBUTE_UNUSED;
3289 int priority = (int) n->key;
3290 priority_info pi = (priority_info) n->value;
3292 /* Generate the functions themselves, but only if they are really
3293 needed. */
3294 if (pi->initializations_p
3295 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3296 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3297 priority);
3298 if (pi->destructions_p
3299 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3300 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3301 priority);
3303 /* Keep iterating. */
3304 return 0;
3307 /* This routine is called from the last rule in yyparse ().
3308 Its job is to create all the code needed to initialize and
3309 destroy the global aggregates. We do the destruction
3310 first, since that way we only need to reverse the decls once. */
3312 void
3313 finish_file ()
3315 tree vars;
3316 int reconsider;
3317 size_t i;
3319 at_eof = 1;
3321 /* Bad parse errors. Just forget about it. */
3322 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3323 return;
3325 /* Otherwise, GDB can get confused, because in only knows
3326 about source for LINENO-1 lines. */
3327 lineno -= 1;
3329 interface_unknown = 1;
3330 interface_only = 0;
3332 /* We now have to write out all the stuff we put off writing out.
3333 These include:
3335 o Template specializations that we have not yet instantiated,
3336 but which are needed.
3337 o Initialization and destruction for non-local objects with
3338 static storage duration. (Local objects with static storage
3339 duration are initialized when their scope is first entered,
3340 and are cleaned up via atexit.)
3341 o Virtual function tables.
3343 All of these may cause others to be needed. For example,
3344 instantiating one function may cause another to be needed, and
3345 generating the intiailzer for an object may cause templates to be
3346 instantiated, etc., etc. */
3348 timevar_push (TV_VARCONST);
3350 emit_support_tinfos ();
3354 reconsider = 0;
3356 /* If there are templates that we've put off instantiating, do
3357 them now. */
3358 instantiate_pending_templates ();
3360 /* Write out virtual tables as required. Note that writing out
3361 the virtual table for a template class may cause the
3362 instantiation of members of that class. */
3363 if (walk_globals (vtable_decl_p,
3364 finish_vtable_vardecl,
3365 /*data=*/0))
3366 reconsider = 1;
3368 /* Write out needed type info variables. Writing out one variable
3369 might cause others to be needed. */
3370 if (walk_globals (tinfo_decl_p, emit_tinfo_decl, /*data=*/0))
3371 reconsider = 1;
3373 /* The list of objects with static storage duration is built up
3374 in reverse order. We clear STATIC_AGGREGATES so that any new
3375 aggregates added during the initialization of these will be
3376 initialized in the correct order when we next come around the
3377 loop. */
3378 vars = prune_vars_needing_no_initialization (static_aggregates);
3379 static_aggregates = NULL_TREE;
3381 if (vars)
3383 tree v;
3385 /* We need to start a new initialization function each time
3386 through the loop. That's because we need to know which
3387 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3388 isn't computed until a function is finished, and written
3389 out. That's a deficiency in the back-end. When this is
3390 fixed, these initialization functions could all become
3391 inline, with resulting performance improvements. */
3392 tree ssdf_body = start_static_storage_duration_function ();
3394 /* Make sure the back end knows about all the variables. */
3395 write_out_vars (vars);
3397 /* First generate code to do all the initializations. */
3398 for (v = vars; v; v = TREE_CHAIN (v))
3399 do_static_initialization (TREE_VALUE (v),
3400 TREE_PURPOSE (v));
3402 /* Then, generate code to do all the destructions. Do these
3403 in reverse order so that the most recently constructed
3404 variable is the first destroyed. If we're using
3405 __cxa_atexit, then we don't need to do this; functions
3406 were registered at initialization time to destroy the
3407 local statics. */
3408 if (!flag_use_cxa_atexit)
3410 vars = nreverse (vars);
3411 for (v = vars; v; v = TREE_CHAIN (v))
3412 do_static_destruction (TREE_VALUE (v));
3414 else
3415 vars = NULL_TREE;
3417 /* Finish up the static storage duration function for this
3418 round. */
3419 finish_static_storage_duration_function (ssdf_body);
3421 /* All those initializations and finalizations might cause
3422 us to need more inline functions, more template
3423 instantiations, etc. */
3424 reconsider = 1;
3427 /* Go through the various inline functions, and see if any need
3428 synthesizing. */
3429 for (i = 0; i < deferred_fns_used; ++i)
3431 tree decl = VARRAY_TREE (deferred_fns, i);
3432 import_export_decl (decl);
3433 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3434 && TREE_USED (decl)
3435 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3437 /* Even though we're already at the top-level, we push
3438 there again. That way, when we pop back a few lines
3439 hence, all of our state is restored. Otherwise,
3440 finish_function doesn't clean things up, and we end
3441 up with CURRENT_FUNCTION_DECL set. */
3442 push_to_top_level ();
3443 synthesize_method (decl);
3444 pop_from_top_level ();
3445 reconsider = 1;
3449 /* We lie to the back-end, pretending that some functions are
3450 not defined when they really are. This keeps these functions
3451 from being put out unnecessarily. But, we must stop lying
3452 when the functions are referenced, or if they are not comdat
3453 since they need to be put out now. */
3454 for (i = 0; i < deferred_fns_used; ++i)
3456 tree decl = VARRAY_TREE (deferred_fns, i);
3458 if (DECL_NOT_REALLY_EXTERN (decl)
3459 && DECL_INITIAL (decl)
3460 && DECL_NEEDED_P (decl))
3461 DECL_EXTERNAL (decl) = 0;
3463 /* If we're going to need to write this function out, and
3464 there's already a body for it, create RTL for it now.
3465 (There might be no body if this is a method we haven't
3466 gotten around to synthesizing yet.) */
3467 if (!DECL_EXTERNAL (decl)
3468 && DECL_NEEDED_P (decl)
3469 && DECL_SAVED_TREE (decl)
3470 && !TREE_ASM_WRITTEN (decl))
3472 int saved_not_really_extern;
3474 /* When we call finish_function in expand_body, it will
3475 try to reset DECL_NOT_REALLY_EXTERN so we save and
3476 restore it here. */
3477 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3478 /* Generate RTL for this function now that we know we
3479 need it. */
3480 expand_body (decl);
3481 /* Undo the damage done by finish_function. */
3482 DECL_EXTERNAL (decl) = 0;
3483 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3484 /* If we're compiling -fsyntax-only pretend that this
3485 function has been written out so that we don't try to
3486 expand it again. */
3487 if (flag_syntax_only)
3488 TREE_ASM_WRITTEN (decl) = 1;
3489 reconsider = 1;
3493 if (deferred_fns_used
3494 && wrapup_global_declarations (&VARRAY_TREE (deferred_fns, 0),
3495 deferred_fns_used))
3496 reconsider = 1;
3497 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3498 reconsider = 1;
3500 /* Static data members are just like namespace-scope globals. */
3501 for (i = 0; i < pending_statics_used; ++i)
3503 tree decl = VARRAY_TREE (pending_statics, i);
3504 if (TREE_ASM_WRITTEN (decl))
3505 continue;
3506 import_export_decl (decl);
3507 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3508 DECL_EXTERNAL (decl) = 0;
3510 if (pending_statics
3511 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3512 pending_statics_used))
3513 reconsider = 1;
3515 while (reconsider);
3517 /* We give C linkage to static constructors and destructors. */
3518 push_lang_context (lang_name_c);
3520 /* Generate initialization and destruction functions for all
3521 priorities for which they are required. */
3522 if (priority_info_map)
3523 splay_tree_foreach (priority_info_map,
3524 generate_ctor_and_dtor_functions_for_priority,
3525 /*data=*/0);
3527 /* We're done with the splay-tree now. */
3528 if (priority_info_map)
3529 splay_tree_delete (priority_info_map);
3531 /* We're done with static constructors, so we can go back to "C++"
3532 linkage now. */
3533 pop_lang_context ();
3535 /* Now delete from the chain of variables all virtual function tables.
3536 We output them all ourselves, because each will be treated
3537 specially. We don't do this if we're just doing semantic
3538 analysis, and not code-generation. */
3539 if (!flag_syntax_only)
3540 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3542 /* Now, issue warnings about static, but not defined, functions,
3543 etc., and emit debugging information. */
3544 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3545 if (pending_statics)
3546 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3547 pending_statics_used);
3549 finish_repo ();
3551 /* The entire file is now complete. If requested, dump everything
3552 to a file. */
3554 int flags;
3555 FILE *stream = dump_begin (TDI_all, &flags);
3557 if (stream)
3559 dump_node (global_namespace, flags & ~TDF_SLIM, stream);
3560 dump_end (TDI_all, stream);
3564 timevar_pop (TV_VARCONST);
3566 if (flag_detailed_statistics)
3568 dump_tree_statistics ();
3569 dump_time_statistics ();
3573 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3574 expr. Since it was parsed like a type, we need to wade through and fix
3575 that. Unfortunately, since operator() is left-associative, we can't use
3576 tail recursion. In the above example, TYPE is `A', and DECL is
3577 `()()()()()'.
3579 Maybe this shouldn't be recursive, but how often will it actually be
3580 used? (jason) */
3582 tree
3583 reparse_absdcl_as_expr (type, decl)
3584 tree type, decl;
3586 /* do build_functional_cast (type, NULL_TREE) at bottom */
3587 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3588 return build_functional_cast (type, NULL_TREE);
3590 /* recurse */
3591 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3593 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3595 if (TREE_CODE (decl) == CALL_EXPR
3596 && (! TREE_TYPE (decl)
3597 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3598 decl = require_complete_type (decl);
3600 return decl;
3603 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3604 out to be an expr. Since it was parsed like a type, we need to wade
3605 through and fix that. Since casts are right-associative, we are
3606 reversing the order, so we don't have to recurse.
3608 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3609 `1'. */
3611 tree
3612 reparse_absdcl_as_casts (decl, expr)
3613 tree decl, expr;
3615 tree type;
3616 int non_void_p = 0;
3618 if (TREE_CODE (expr) == CONSTRUCTOR
3619 && TREE_TYPE (expr) == 0)
3621 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3622 decl = TREE_OPERAND (decl, 0);
3624 if (processing_template_decl)
3625 TREE_TYPE (expr) = type;
3626 else
3628 expr = digest_init (type, expr, (tree *) 0);
3629 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3631 int failure = complete_array_type (type, expr, 1);
3632 my_friendly_assert (!failure, 78);
3637 while (decl)
3639 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3640 decl = TREE_OPERAND (decl, 0);
3641 if (!VOID_TYPE_P (type))
3642 non_void_p = 1;
3643 expr = build_c_cast (type, expr);
3646 if (warn_old_style_cast && ! in_system_header
3647 && non_void_p && current_lang_name != lang_name_c)
3648 warning ("use of old-style cast");
3650 return expr;
3653 /* Given plain tree nodes for an expression, build up the full semantics. */
3655 tree
3656 build_expr_from_tree (t)
3657 tree t;
3659 if (t == NULL_TREE || t == error_mark_node)
3660 return t;
3662 switch (TREE_CODE (t))
3664 case IDENTIFIER_NODE:
3665 return do_identifier (t, 0, NULL_TREE);
3667 case LOOKUP_EXPR:
3668 if (LOOKUP_EXPR_GLOBAL (t))
3669 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3670 else
3671 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3673 case TEMPLATE_ID_EXPR:
3674 return (lookup_template_function
3675 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3676 build_expr_from_tree (TREE_OPERAND (t, 1))));
3678 case INDIRECT_REF:
3679 return build_x_indirect_ref
3680 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3682 case CAST_EXPR:
3683 return build_functional_cast
3684 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3686 case REINTERPRET_CAST_EXPR:
3687 return build_reinterpret_cast
3688 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3690 case CONST_CAST_EXPR:
3691 return build_const_cast
3692 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3694 case DYNAMIC_CAST_EXPR:
3695 return build_dynamic_cast
3696 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3698 case STATIC_CAST_EXPR:
3699 return build_static_cast
3700 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3702 case PREDECREMENT_EXPR:
3703 case PREINCREMENT_EXPR:
3704 case POSTDECREMENT_EXPR:
3705 case POSTINCREMENT_EXPR:
3706 case NEGATE_EXPR:
3707 case BIT_NOT_EXPR:
3708 case ABS_EXPR:
3709 case TRUTH_NOT_EXPR:
3710 case ADDR_EXPR:
3711 case CONVERT_EXPR: /* Unary + */
3712 case REALPART_EXPR:
3713 case IMAGPART_EXPR:
3714 if (TREE_TYPE (t))
3715 return t;
3716 return build_x_unary_op (TREE_CODE (t),
3717 build_expr_from_tree (TREE_OPERAND (t, 0)));
3719 case PLUS_EXPR:
3720 case MINUS_EXPR:
3721 case MULT_EXPR:
3722 case TRUNC_DIV_EXPR:
3723 case CEIL_DIV_EXPR:
3724 case FLOOR_DIV_EXPR:
3725 case ROUND_DIV_EXPR:
3726 case EXACT_DIV_EXPR:
3727 case BIT_AND_EXPR:
3728 case BIT_ANDTC_EXPR:
3729 case BIT_IOR_EXPR:
3730 case BIT_XOR_EXPR:
3731 case TRUNC_MOD_EXPR:
3732 case FLOOR_MOD_EXPR:
3733 case TRUTH_ANDIF_EXPR:
3734 case TRUTH_ORIF_EXPR:
3735 case TRUTH_AND_EXPR:
3736 case TRUTH_OR_EXPR:
3737 case RSHIFT_EXPR:
3738 case LSHIFT_EXPR:
3739 case RROTATE_EXPR:
3740 case LROTATE_EXPR:
3741 case EQ_EXPR:
3742 case NE_EXPR:
3743 case MAX_EXPR:
3744 case MIN_EXPR:
3745 case LE_EXPR:
3746 case GE_EXPR:
3747 case LT_EXPR:
3748 case GT_EXPR:
3749 case MEMBER_REF:
3750 return build_x_binary_op
3751 (TREE_CODE (t),
3752 build_expr_from_tree (TREE_OPERAND (t, 0)),
3753 build_expr_from_tree (TREE_OPERAND (t, 1)));
3755 case DOTSTAR_EXPR:
3756 return build_m_component_ref
3757 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3758 build_expr_from_tree (TREE_OPERAND (t, 1)));
3760 case SCOPE_REF:
3761 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3763 case ARRAY_REF:
3764 if (TREE_OPERAND (t, 0) == NULL_TREE)
3765 /* new-type-id */
3766 return build_nt (ARRAY_REF, NULL_TREE,
3767 build_expr_from_tree (TREE_OPERAND (t, 1)));
3768 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3769 build_expr_from_tree (TREE_OPERAND (t, 1)));
3771 case SIZEOF_EXPR:
3772 case ALIGNOF_EXPR:
3774 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3775 if (!TYPE_P (r))
3776 return TREE_CODE (t) == SIZEOF_EXPR ? expr_sizeof (r) : c_alignof_expr (r);
3777 else
3778 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3781 case MODOP_EXPR:
3782 return build_x_modify_expr
3783 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3784 TREE_CODE (TREE_OPERAND (t, 1)),
3785 build_expr_from_tree (TREE_OPERAND (t, 2)));
3787 case ARROW_EXPR:
3788 return build_x_arrow
3789 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3791 case NEW_EXPR:
3792 return build_new
3793 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3794 build_expr_from_tree (TREE_OPERAND (t, 1)),
3795 build_expr_from_tree (TREE_OPERAND (t, 2)),
3796 NEW_EXPR_USE_GLOBAL (t));
3798 case DELETE_EXPR:
3799 return delete_sanity
3800 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3801 build_expr_from_tree (TREE_OPERAND (t, 1)),
3802 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3804 case COMPOUND_EXPR:
3805 if (TREE_OPERAND (t, 1) == NULL_TREE)
3806 return build_x_compound_expr
3807 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3808 else
3809 abort ();
3811 case METHOD_CALL_EXPR:
3812 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3814 tree ref = TREE_OPERAND (t, 0);
3815 tree name = TREE_OPERAND (ref, 1);
3817 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3818 name = build_nt (TEMPLATE_ID_EXPR,
3819 TREE_OPERAND (name, 0),
3820 build_expr_from_tree (TREE_OPERAND (name, 1)));
3822 return build_scoped_method_call
3823 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3824 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3825 name,
3826 build_expr_from_tree (TREE_OPERAND (t, 2)));
3828 else
3830 tree fn = TREE_OPERAND (t, 0);
3832 /* We can get a TEMPLATE_ID_EXPR here on code like:
3834 x->f<2>();
3836 so we must resolve that. However, we can also get things
3837 like a BIT_NOT_EXPR here, when referring to a destructor,
3838 and things like that are not correctly resolved by
3839 build_expr_from_tree. So, just use build_expr_from_tree
3840 when we really need it. */
3841 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3842 fn = lookup_template_function
3843 (TREE_OPERAND (fn, 0),
3844 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3846 return build_method_call
3847 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3849 build_expr_from_tree (TREE_OPERAND (t, 2)),
3850 NULL_TREE, LOOKUP_NORMAL);
3853 case CALL_EXPR:
3854 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3856 tree ref = TREE_OPERAND (t, 0);
3857 tree name = TREE_OPERAND (ref, 1);
3859 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3860 name = build_nt (TEMPLATE_ID_EXPR,
3861 TREE_OPERAND (name, 0),
3862 build_expr_from_tree (TREE_OPERAND (name, 1)));
3864 return build_member_call
3865 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3866 name,
3867 build_expr_from_tree (TREE_OPERAND (t, 1)));
3869 else
3871 tree name = TREE_OPERAND (t, 0);
3872 tree id;
3873 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3874 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3875 && !LOOKUP_EXPR_GLOBAL (name)
3876 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3877 && (!current_class_type
3878 || !lookup_member (current_class_type, id, 0, 0)))
3880 /* Do Koenig lookup if there are no class members. */
3881 name = do_identifier (id, 0, args);
3883 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3884 || ! really_overloaded_fn (name))
3885 name = build_expr_from_tree (name);
3886 return build_x_function_call (name, args, current_class_ref);
3889 case COND_EXPR:
3890 return build_x_conditional_expr
3891 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3892 build_expr_from_tree (TREE_OPERAND (t, 1)),
3893 build_expr_from_tree (TREE_OPERAND (t, 2)));
3895 case PSEUDO_DTOR_EXPR:
3896 return (finish_pseudo_destructor_call_expr
3897 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3898 build_expr_from_tree (TREE_OPERAND (t, 1)),
3899 build_expr_from_tree (TREE_OPERAND (t, 2))));
3901 case TREE_LIST:
3903 tree purpose, value, chain;
3905 if (t == void_list_node)
3906 return t;
3908 purpose = TREE_PURPOSE (t);
3909 if (purpose)
3910 purpose = build_expr_from_tree (purpose);
3911 value = TREE_VALUE (t);
3912 if (value)
3913 value = build_expr_from_tree (value);
3914 chain = TREE_CHAIN (t);
3915 if (chain && chain != void_type_node)
3916 chain = build_expr_from_tree (chain);
3917 return tree_cons (purpose, value, chain);
3920 case COMPONENT_REF:
3922 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3923 tree field = TREE_OPERAND (t, 1);
3925 /* We use a COMPONENT_REF to indicate things of the form `x.b'
3926 and `x.A::b'. We must distinguish between those cases
3927 here. */
3928 if (TREE_CODE (field) == SCOPE_REF)
3929 return build_object_ref (object,
3930 TREE_OPERAND (field, 0),
3931 TREE_OPERAND (field, 1));
3932 else
3933 return build_x_component_ref (object, field,
3934 NULL_TREE, 1);
3937 case THROW_EXPR:
3938 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3940 case CONSTRUCTOR:
3942 tree r;
3943 tree elts;
3944 tree type = TREE_TYPE (t);
3945 bool purpose_p;
3947 /* digest_init will do the wrong thing if we let it. */
3948 if (type && TYPE_PTRMEMFUNC_P (type))
3949 return t;
3951 r = NULL_TREE;
3952 /* We do not want to process the purpose of aggregate
3953 initializers as they are identifier nodes which will be
3954 looked up by digest_init. */
3955 purpose_p = !(type && IS_AGGR_TYPE (type));
3956 for (elts = CONSTRUCTOR_ELTS (t); elts; elts = TREE_CHAIN (elts))
3958 tree purpose = TREE_PURPOSE (elts);
3959 tree value = TREE_VALUE (elts);
3961 if (purpose && purpose_p)
3962 purpose = build_expr_from_tree (purpose);
3963 value = build_expr_from_tree (value);
3964 r = tree_cons (purpose, value, r);
3967 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
3968 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3970 if (type)
3971 return digest_init (type, r, 0);
3972 return r;
3975 case TYPEID_EXPR:
3976 if (TYPE_P (TREE_OPERAND (t, 0)))
3977 return get_typeid (TREE_OPERAND (t, 0));
3978 return build_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3980 case VAR_DECL:
3981 return convert_from_reference (t);
3983 case VA_ARG_EXPR:
3984 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
3985 TREE_TYPE (t));
3987 default:
3988 return t;
3992 /* This is something of the form `int (*a)++' that has turned out to be an
3993 expr. It was only converted into parse nodes, so we need to go through
3994 and build up the semantics. Most of the work is done by
3995 build_expr_from_tree, above.
3997 In the above example, TYPE is `int' and DECL is `*a'. */
3999 tree
4000 reparse_decl_as_expr (type, decl)
4001 tree type, decl;
4003 decl = build_expr_from_tree (decl);
4004 if (type)
4005 return build_functional_cast (type, build_tree_list (NULL_TREE, decl));
4006 else
4007 return decl;
4010 /* This is something of the form `int (*a)' that has turned out to be a
4011 decl. It was only converted into parse nodes, so we need to do the
4012 checking that make_{pointer,reference}_declarator do. */
4014 tree
4015 finish_decl_parsing (decl)
4016 tree decl;
4018 switch (TREE_CODE (decl))
4020 case IDENTIFIER_NODE:
4021 return decl;
4022 case INDIRECT_REF:
4023 return make_pointer_declarator
4024 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4025 case ADDR_EXPR:
4026 return make_reference_declarator
4027 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4028 case BIT_NOT_EXPR:
4029 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4030 return decl;
4031 case SCOPE_REF:
4032 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4033 TREE_COMPLEXITY (decl) = current_class_depth;
4034 return decl;
4035 case ARRAY_REF:
4036 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4037 return decl;
4038 case TREE_LIST:
4039 /* For attribute handling. */
4040 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4041 return decl;
4042 case TEMPLATE_ID_EXPR:
4043 return decl;
4044 default:
4045 abort ();
4046 return NULL_TREE;
4050 /* Return 1 if root encloses child. */
4052 static int
4053 is_namespace_ancestor (root, child)
4054 tree root, child;
4056 if (root == child)
4057 return 1;
4058 if (root == global_namespace)
4059 return 1;
4060 if (child == global_namespace)
4061 return 0;
4062 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4066 /* Return the namespace that is the common ancestor
4067 of two given namespaces. */
4069 tree
4070 namespace_ancestor (ns1, ns2)
4071 tree ns1, ns2;
4073 if (is_namespace_ancestor (ns1, ns2))
4074 return ns1;
4075 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4078 /* Insert used into the using list of user. Set indirect_flag if this
4079 directive is not directly from the source. Also find the common
4080 ancestor and let our users know about the new namespace */
4081 static void
4082 add_using_namespace (user, used, indirect)
4083 tree user;
4084 tree used;
4085 int indirect;
4087 tree t;
4088 /* Using oneself is a no-op. */
4089 if (user == used)
4090 return;
4091 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4092 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4093 /* Check if we already have this. */
4094 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4095 if (t != NULL_TREE)
4097 if (!indirect)
4098 /* Promote to direct usage. */
4099 TREE_INDIRECT_USING (t) = 0;
4100 return;
4103 /* Add used to the user's using list. */
4104 DECL_NAMESPACE_USING (user)
4105 = tree_cons (used, namespace_ancestor (user, used),
4106 DECL_NAMESPACE_USING (user));
4108 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4110 /* Add user to the used's users list. */
4111 DECL_NAMESPACE_USERS (used)
4112 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4114 /* Recursively add all namespaces used. */
4115 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4116 /* indirect usage */
4117 add_using_namespace (user, TREE_PURPOSE (t), 1);
4119 /* Tell everyone using us about the new used namespaces. */
4120 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4121 add_using_namespace (TREE_PURPOSE (t), used, 1);
4124 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4125 duplicates. The first list becomes the tail of the result.
4127 The algorithm is O(n^2). We could get this down to O(n log n) by
4128 doing a sort on the addresses of the functions, if that becomes
4129 necessary. */
4131 static tree
4132 merge_functions (s1, s2)
4133 tree s1;
4134 tree s2;
4136 for (; s2; s2 = OVL_NEXT (s2))
4138 tree fn2 = OVL_CURRENT (s2);
4139 tree fns1;
4141 for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
4143 tree fn1 = OVL_CURRENT (fns1);
4145 /* If the function from S2 is already in S1, there is no
4146 need to add it again. For `extern "C"' functions, we
4147 might have two FUNCTION_DECLs for the same function, in
4148 different namespaces; again, we only need one of them. */
4149 if (fn1 == fn2
4150 || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
4151 && DECL_NAME (fn1) == DECL_NAME (fn2)))
4152 break;
4155 /* If we exhausted all of the functions in S1, FN2 is new. */
4156 if (!fns1)
4157 s1 = build_overload (fn2, s1);
4159 return s1;
4162 /* This should return an error not all definitions define functions.
4163 It is not an error if we find two functions with exactly the
4164 same signature, only if these are selected in overload resolution.
4165 old is the current set of bindings, new the freshly-found binding.
4166 XXX Do we want to give *all* candidates in case of ambiguity?
4167 XXX In what way should I treat extern declarations?
4168 XXX I don't want to repeat the entire duplicate_decls here */
4170 static tree
4171 ambiguous_decl (name, old, new, flags)
4172 tree name;
4173 tree old;
4174 tree new;
4175 int flags;
4177 tree val, type;
4178 my_friendly_assert (old != NULL_TREE, 393);
4179 /* Copy the value. */
4180 val = BINDING_VALUE (new);
4181 if (val)
4182 switch (TREE_CODE (val))
4184 case TEMPLATE_DECL:
4185 /* If we expect types or namespaces, and not templates,
4186 or this is not a template class. */
4187 if (LOOKUP_QUALIFIERS_ONLY (flags)
4188 && !DECL_CLASS_TEMPLATE_P (val))
4189 val = NULL_TREE;
4190 break;
4191 case TYPE_DECL:
4192 if (LOOKUP_NAMESPACES_ONLY (flags))
4193 val = NULL_TREE;
4194 break;
4195 case NAMESPACE_DECL:
4196 if (LOOKUP_TYPES_ONLY (flags))
4197 val = NULL_TREE;
4198 break;
4199 default:
4200 if (LOOKUP_QUALIFIERS_ONLY (flags))
4201 val = NULL_TREE;
4204 if (!BINDING_VALUE (old))
4205 BINDING_VALUE (old) = val;
4206 else if (val && val != BINDING_VALUE (old))
4208 if (is_overloaded_fn (BINDING_VALUE (old))
4209 && is_overloaded_fn (val))
4211 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4212 val);
4214 else
4216 /* Some declarations are functions, some are not. */
4217 if (flags & LOOKUP_COMPLAIN)
4219 /* If we've already given this error for this lookup,
4220 BINDING_VALUE (old) is error_mark_node, so let's not
4221 repeat ourselves. */
4222 if (BINDING_VALUE (old) != error_mark_node)
4224 error ("use of `%D' is ambiguous", name);
4225 cp_error_at (" first declared as `%#D' here",
4226 BINDING_VALUE (old));
4228 cp_error_at (" also declared as `%#D' here", val);
4230 BINDING_VALUE (old) = error_mark_node;
4233 /* ... and copy the type. */
4234 type = BINDING_TYPE (new);
4235 if (LOOKUP_NAMESPACES_ONLY (flags))
4236 type = NULL_TREE;
4237 if (!BINDING_TYPE (old))
4238 BINDING_TYPE (old) = type;
4239 else if (type && BINDING_TYPE (old) != type)
4241 if (flags & LOOKUP_COMPLAIN)
4243 error ("`%D' denotes an ambiguous type",name);
4244 cp_error_at (" first type here", BINDING_TYPE (old));
4245 cp_error_at (" other type here", type);
4248 return old;
4251 /* Subroutine of unualified_namespace_lookup:
4252 Add the bindings of NAME in used namespaces to VAL.
4253 We are currently looking for names in namespace SCOPE, so we
4254 look through USINGS for using-directives of namespaces
4255 which have SCOPE as a common ancestor with the current scope.
4256 Returns zero on errors. */
4259 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4260 tree name, val, usings, scope;
4261 int flags;
4262 tree *spacesp;
4264 tree iter;
4265 tree val1;
4266 /* Iterate over all used namespaces in current, searching for using
4267 directives of scope. */
4268 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4269 if (TREE_VALUE (iter) == scope)
4271 if (spacesp)
4272 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4273 *spacesp);
4274 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4275 /* Resolve ambiguities. */
4276 val = ambiguous_decl (name, val, val1, flags);
4278 return BINDING_VALUE (val) != error_mark_node;
4281 /* [namespace.qual]
4282 Accepts the NAME to lookup and its qualifying SCOPE.
4283 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4284 or 0 on error. */
4287 qualified_lookup_using_namespace (name, scope, result, flags)
4288 tree name;
4289 tree scope;
4290 tree result;
4291 int flags;
4293 /* Maintain a list of namespaces visited... */
4294 tree seen = NULL_TREE;
4295 /* ... and a list of namespace yet to see. */
4296 tree todo = NULL_TREE;
4297 tree usings;
4298 /* Look through namespace aliases. */
4299 scope = ORIGINAL_NAMESPACE (scope);
4300 while (scope && (result != error_mark_node))
4302 seen = tree_cons (scope, NULL_TREE, seen);
4303 result = ambiguous_decl (name, result,
4304 binding_for_name (name, scope), flags);
4305 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4306 /* Consider using directives. */
4307 for (usings = DECL_NAMESPACE_USING (scope); usings;
4308 usings = TREE_CHAIN (usings))
4309 /* If this was a real directive, and we have not seen it. */
4310 if (!TREE_INDIRECT_USING (usings)
4311 && !purpose_member (TREE_PURPOSE (usings), seen))
4312 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4313 if (todo)
4315 scope = TREE_PURPOSE (todo);
4316 todo = TREE_CHAIN (todo);
4318 else
4319 scope = NULL_TREE; /* If there never was a todo list. */
4321 return result != error_mark_node;
4324 /* [namespace.memdef]/2 */
4326 /* Set the context of a declaration to scope. Complain if we are not
4327 outside scope. */
4329 void
4330 set_decl_namespace (decl, scope, friendp)
4331 tree decl;
4332 tree scope;
4333 int friendp;
4335 tree old;
4337 /* Get rid of namespace aliases. */
4338 scope = ORIGINAL_NAMESPACE (scope);
4340 /* It is ok for friends to be qualified in parallel space. */
4341 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4342 error ("declaration of `%D' not in a namespace surrounding `%D'",
4343 decl, scope);
4344 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4345 if (scope != current_namespace)
4347 /* See whether this has been declared in the namespace. */
4348 old = namespace_binding (DECL_NAME (decl), scope);
4349 if (!old)
4350 /* No old declaration at all. */
4351 goto complain;
4352 /* A template can be explicitly specialized in any namespace. */
4353 if (processing_explicit_instantiation)
4354 return;
4355 if (!is_overloaded_fn (decl))
4356 /* Don't compare non-function decls with decls_match here,
4357 since it can't check for the correct constness at this
4358 point. pushdecl will find those errors later. */
4359 return;
4360 /* Since decl is a function, old should contain a function decl. */
4361 if (!is_overloaded_fn (old))
4362 goto complain;
4363 if (processing_template_decl || processing_specialization)
4364 /* We have not yet called push_template_decl to turn the
4365 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4366 won't match. But, we'll check later, when we construct the
4367 template. */
4368 return;
4369 for (; old; old = OVL_NEXT (old))
4370 if (decls_match (decl, OVL_CURRENT (old)))
4371 return;
4373 else
4374 return;
4375 complain:
4376 error ("`%D' should have been declared inside `%D'",
4377 decl, scope);
4380 /* Compute the namespace where a declaration is defined. */
4382 static tree
4383 decl_namespace (decl)
4384 tree decl;
4386 if (TYPE_P (decl))
4387 decl = TYPE_STUB_DECL (decl);
4388 while (DECL_CONTEXT (decl))
4390 decl = DECL_CONTEXT (decl);
4391 if (TREE_CODE (decl) == NAMESPACE_DECL)
4392 return decl;
4393 if (TYPE_P (decl))
4394 decl = TYPE_STUB_DECL (decl);
4395 my_friendly_assert (DECL_P (decl), 390);
4398 return global_namespace;
4401 /* Return the namespace where the current declaration is declared. */
4403 tree
4404 current_decl_namespace ()
4406 tree result;
4407 /* If we have been pushed into a different namespace, use it. */
4408 if (decl_namespace_list)
4409 return TREE_PURPOSE (decl_namespace_list);
4411 if (current_class_type)
4412 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4413 else if (current_function_decl)
4414 result = decl_namespace (current_function_decl);
4415 else
4416 result = current_namespace;
4417 return result;
4420 /* Temporarily set the namespace for the current declaration. */
4422 void
4423 push_decl_namespace (decl)
4424 tree decl;
4426 if (TREE_CODE (decl) != NAMESPACE_DECL)
4427 decl = decl_namespace (decl);
4428 decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
4429 NULL_TREE, decl_namespace_list);
4432 void
4433 pop_decl_namespace ()
4435 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4438 /* Enter a class or namespace scope. */
4440 void
4441 push_scope (t)
4442 tree t;
4444 if (TREE_CODE (t) == NAMESPACE_DECL)
4445 push_decl_namespace (t);
4446 else
4447 pushclass (t, 2);
4450 /* Leave scope pushed by push_scope. */
4452 void
4453 pop_scope (t)
4454 tree t;
4456 if (TREE_CODE (t) == NAMESPACE_DECL)
4457 pop_decl_namespace ();
4458 else
4459 popclass ();
4462 /* [basic.lookup.koenig] */
4463 /* A non-zero return value in the functions below indicates an error.
4464 All nodes allocated in the procedure are on the scratch obstack. */
4466 struct arg_lookup
4468 tree name;
4469 tree namespaces;
4470 tree classes;
4471 tree functions;
4474 static int arg_assoc PARAMS ((struct arg_lookup*, tree));
4475 static int arg_assoc_args PARAMS ((struct arg_lookup*, tree));
4476 static int arg_assoc_type PARAMS ((struct arg_lookup*, tree));
4477 static int add_function PARAMS ((struct arg_lookup *, tree));
4478 static int arg_assoc_namespace PARAMS ((struct arg_lookup *, tree));
4479 static int arg_assoc_class PARAMS ((struct arg_lookup *, tree));
4480 static int arg_assoc_template_arg PARAMS ((struct arg_lookup*, tree));
4482 /* Add a function to the lookup structure.
4483 Returns 1 on error. */
4485 static int
4486 add_function (k, fn)
4487 struct arg_lookup *k;
4488 tree fn;
4490 /* We used to check here to see if the function was already in the list,
4491 but that's O(n^2), which is just too expensive for function lookup.
4492 Now we deal with the occasional duplicate in joust. In doing this, we
4493 assume that the number of duplicates will be small compared to the
4494 total number of functions being compared, which should usually be the
4495 case. */
4497 /* We must find only functions, or exactly one non-function. */
4498 if (!k->functions)
4499 k->functions = fn;
4500 else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
4501 k->functions = build_overload (fn, k->functions);
4502 else
4504 tree f1 = OVL_CURRENT (k->functions);
4505 tree f2 = fn;
4506 if (is_overloaded_fn (f1))
4508 fn = f1; f1 = f2; f2 = fn;
4510 cp_error_at ("`%D' is not a function,", f1);
4511 cp_error_at (" conflict with `%D'", f2);
4512 error (" in call to `%D'", k->name);
4513 return 1;
4516 return 0;
4519 /* Add functions of a namespace to the lookup structure.
4520 Returns 1 on error. */
4522 static int
4523 arg_assoc_namespace (k, scope)
4524 struct arg_lookup *k;
4525 tree scope;
4527 tree value;
4529 if (purpose_member (scope, k->namespaces))
4530 return 0;
4531 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4533 value = namespace_binding (k->name, scope);
4534 if (!value)
4535 return 0;
4537 for (; value; value = OVL_NEXT (value))
4538 if (add_function (k, OVL_CURRENT (value)))
4539 return 1;
4541 return 0;
4544 /* Adds everything associated with a template argument to the lookup
4545 structure. Returns 1 on error. */
4547 static int
4548 arg_assoc_template_arg (k, arg)
4549 struct arg_lookup* k;
4550 tree arg;
4552 /* [basic.lookup.koenig]
4554 If T is a template-id, its associated namespaces and classes are
4555 ... the namespaces and classes associated with the types of the
4556 template arguments provided for template type parameters
4557 (excluding template template parameters); the namespaces in which
4558 any template template arguments are defined; and the classes in
4559 which any member templates used as template template arguments
4560 are defined. [Note: non-type template arguments do not
4561 contribute to the set of associated namespaces. ] */
4563 /* Consider first template template arguments. */
4564 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
4565 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
4566 return 0;
4567 else if (TREE_CODE (arg) == TEMPLATE_DECL)
4569 tree ctx = CP_DECL_CONTEXT (arg);
4571 /* It's not a member template. */
4572 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4573 return arg_assoc_namespace (k, ctx);
4574 /* Otherwise, it must be member template. */
4575 else
4576 return arg_assoc_class (k, ctx);
4578 /* It's not a template template argument, but it is a type template
4579 argument. */
4580 else if (TYPE_P (arg))
4581 return arg_assoc_type (k, arg);
4582 /* It's a non-type template argument. */
4583 else
4584 return 0;
4587 /* Adds everything associated with class to the lookup structure.
4588 Returns 1 on error. */
4590 static int
4591 arg_assoc_class (k, type)
4592 struct arg_lookup* k;
4593 tree type;
4595 tree list, friends, context;
4596 int i;
4598 /* Backend build structures, such as __builtin_va_list, aren't
4599 affected by all this. */
4600 if (!CLASS_TYPE_P (type))
4601 return 0;
4603 if (purpose_member (type, k->classes))
4604 return 0;
4605 k->classes = tree_cons (type, NULL_TREE, k->classes);
4607 context = decl_namespace (TYPE_MAIN_DECL (type));
4608 if (arg_assoc_namespace (k, context))
4609 return 1;
4611 /* Process baseclasses. */
4612 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4613 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4614 return 1;
4616 /* Process friends. */
4617 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4618 list = TREE_CHAIN (list))
4619 if (k->name == TREE_PURPOSE (list))
4620 for (friends = TREE_VALUE (list); friends;
4621 friends = TREE_CHAIN (friends))
4622 /* Only interested in global functions with potentially hidden
4623 (i.e. unqualified) declarations. */
4624 if (TREE_PURPOSE (friends) == error_mark_node && TREE_VALUE (friends)
4625 && decl_namespace (TREE_VALUE (friends)) == context)
4626 if (add_function (k, TREE_VALUE (friends)))
4627 return 1;
4629 /* Process template arguments. */
4630 if (CLASSTYPE_TEMPLATE_INFO (type))
4632 list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
4633 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4634 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4637 return 0;
4640 /* Adds everything associated with a given type.
4641 Returns 1 on error. */
4643 static int
4644 arg_assoc_type (k, type)
4645 struct arg_lookup *k;
4646 tree type;
4648 switch (TREE_CODE (type))
4650 case VOID_TYPE:
4651 case INTEGER_TYPE:
4652 case REAL_TYPE:
4653 case COMPLEX_TYPE:
4654 case VECTOR_TYPE:
4655 case CHAR_TYPE:
4656 case BOOLEAN_TYPE:
4657 return 0;
4658 case RECORD_TYPE:
4659 if (TYPE_PTRMEMFUNC_P (type))
4660 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4661 return arg_assoc_class (k, type);
4662 case POINTER_TYPE:
4663 case REFERENCE_TYPE:
4664 case ARRAY_TYPE:
4665 return arg_assoc_type (k, TREE_TYPE (type));
4666 case UNION_TYPE:
4667 case ENUMERAL_TYPE:
4668 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4669 case OFFSET_TYPE:
4670 /* Pointer to member: associate class type and value type. */
4671 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4672 return 1;
4673 return arg_assoc_type (k, TREE_TYPE (type));
4674 case METHOD_TYPE:
4675 /* The basetype is referenced in the first arg type, so just
4676 fall through. */
4677 case FUNCTION_TYPE:
4678 /* Associate the parameter types. */
4679 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4680 return 1;
4681 /* Associate the return type. */
4682 return arg_assoc_type (k, TREE_TYPE (type));
4683 case TEMPLATE_TYPE_PARM:
4684 case BOUND_TEMPLATE_TEMPLATE_PARM:
4685 return 0;
4686 case TYPENAME_TYPE:
4687 return 0;
4688 case LANG_TYPE:
4689 if (type == unknown_type_node)
4690 return 0;
4691 /* else fall through */
4692 default:
4693 abort ();
4695 return 0;
4698 /* Adds everything associated with arguments. Returns 1 on error. */
4700 static int
4701 arg_assoc_args (k, args)
4702 struct arg_lookup* k;
4703 tree args;
4705 for (; args; args = TREE_CHAIN (args))
4706 if (arg_assoc (k, TREE_VALUE (args)))
4707 return 1;
4708 return 0;
4711 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4713 static int
4714 arg_assoc (k, n)
4715 struct arg_lookup* k;
4716 tree n;
4718 if (n == error_mark_node)
4719 return 0;
4721 if (TYPE_P (n))
4722 return arg_assoc_type (k, n);
4724 if (! type_unknown_p (n))
4725 return arg_assoc_type (k, TREE_TYPE (n));
4727 if (TREE_CODE (n) == ADDR_EXPR)
4728 n = TREE_OPERAND (n, 0);
4729 if (TREE_CODE (n) == COMPONENT_REF)
4730 n = TREE_OPERAND (n, 1);
4731 if (TREE_CODE (n) == OFFSET_REF)
4732 n = TREE_OPERAND (n, 1);
4733 while (TREE_CODE (n) == TREE_LIST)
4734 n = TREE_VALUE (n);
4736 if (TREE_CODE (n) == FUNCTION_DECL)
4737 return arg_assoc_type (k, TREE_TYPE (n));
4738 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4740 /* [basic.lookup.koenig]
4742 If T is a template-id, its associated namespaces and classes
4743 are the namespace in which the template is defined; for
4744 member templates, the member template's class... */
4745 tree template = TREE_OPERAND (n, 0);
4746 tree args = TREE_OPERAND (n, 1);
4747 tree ctx;
4748 tree arg;
4750 if (TREE_CODE (template) == COMPONENT_REF)
4751 template = TREE_OPERAND (template, 1);
4753 /* First, the template. There may actually be more than one if
4754 this is an overloaded function template. But, in that case,
4755 we only need the first; all the functions will be in the same
4756 namespace. */
4757 template = OVL_CURRENT (template);
4759 ctx = CP_DECL_CONTEXT (template);
4761 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4763 if (arg_assoc_namespace (k, ctx) == 1)
4764 return 1;
4766 /* It must be a member template. */
4767 else if (arg_assoc_class (k, ctx) == 1)
4768 return 1;
4770 /* Now the arguments. */
4771 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4772 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4773 return 1;
4775 else
4777 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4779 for (; n; n = OVL_CHAIN (n))
4780 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4781 return 1;
4784 return 0;
4787 /* Performs Koenig lookup depending on arguments, where fns
4788 are the functions found in normal lookup. */
4790 tree
4791 lookup_arg_dependent (name, fns, args)
4792 tree name;
4793 tree fns;
4794 tree args;
4796 struct arg_lookup k;
4797 tree fn = NULL_TREE;
4799 k.name = name;
4800 k.functions = fns;
4801 k.classes = NULL_TREE;
4803 /* Note that we've already looked at some namespaces during normal
4804 unqualified lookup, unless we found a decl in function scope. */
4805 if (fns)
4806 fn = OVL_CURRENT (fns);
4807 if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (fn))
4808 k.namespaces = NULL_TREE;
4809 else
4810 unqualified_namespace_lookup (name, 0, &k.namespaces);
4812 arg_assoc_args (&k, args);
4813 return k.functions;
4816 /* Process a namespace-alias declaration. */
4818 void
4819 do_namespace_alias (alias, namespace)
4820 tree alias, namespace;
4822 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4824 /* The parser did not find it, so it's not there. */
4825 error ("unknown namespace `%D'", namespace);
4826 return;
4829 namespace = ORIGINAL_NAMESPACE (namespace);
4831 /* Build the alias. */
4832 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4833 DECL_NAMESPACE_ALIAS (alias) = namespace;
4834 pushdecl (alias);
4837 /* Check a non-member using-declaration. Return the name and scope
4838 being used, and the USING_DECL, or NULL_TREE on failure. */
4840 static tree
4841 validate_nonmember_using_decl (decl, scope, name)
4842 tree decl;
4843 tree *scope;
4844 tree *name;
4846 if (TREE_CODE (decl) == SCOPE_REF)
4848 *scope = TREE_OPERAND (decl, 0);
4849 *name = TREE_OPERAND (decl, 1);
4851 if (!processing_template_decl)
4853 /* [namespace.udecl]
4854 A using-declaration for a class member shall be a
4855 member-declaration. */
4856 if(TREE_CODE (*scope) != NAMESPACE_DECL)
4858 if (TYPE_P (*scope))
4859 error ("`%T' is not a namespace", *scope);
4860 else
4861 error ("`%D' is not a namespace", *scope);
4862 return NULL_TREE;
4865 /* 7.3.3/5
4866 A using-declaration shall not name a template-id. */
4867 if (TREE_CODE (*name) == TEMPLATE_ID_EXPR)
4869 *name = TREE_OPERAND (*name, 0);
4870 error ("a using-declaration cannot specify a template-id. Try `using %D'", *name);
4871 return NULL_TREE;
4875 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4876 || TREE_CODE (decl) == TYPE_DECL
4877 || TREE_CODE (decl) == TEMPLATE_DECL)
4879 *scope = global_namespace;
4880 *name = decl;
4882 else if (TREE_CODE (decl) == NAMESPACE_DECL)
4884 error ("namespace `%D' not allowed in using-declaration", decl);
4885 return NULL_TREE;
4887 else
4888 abort ();
4889 if (DECL_P (*name))
4890 *name = DECL_NAME (*name);
4891 /* Make a USING_DECL. */
4892 return push_using_decl (*scope, *name);
4895 /* Process local and global using-declarations. */
4897 static void
4898 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4899 tree scope, name;
4900 tree oldval, oldtype;
4901 tree *newval, *newtype;
4903 tree decls;
4905 *newval = *newtype = NULL_TREE;
4906 decls = make_node (CPLUS_BINDING);
4907 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4908 /* Lookup error */
4909 return;
4911 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4913 error ("`%D' not declared", name);
4914 return;
4917 /* Check for using functions. */
4918 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4920 tree tmp, tmp1;
4922 if (oldval && !is_overloaded_fn (oldval))
4924 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4925 oldval = NULL_TREE;
4928 *newval = oldval;
4929 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4931 tree new_fn = OVL_CURRENT (tmp);
4933 /* [namespace.udecl]
4935 If a function declaration in namespace scope or block
4936 scope has the same name and the same parameter types as a
4937 function introduced by a using declaration the program is
4938 ill-formed. */
4939 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4941 tree old_fn = OVL_CURRENT (tmp1);
4943 if (new_fn == old_fn)
4944 /* The function already exists in the current namespace. */
4945 break;
4946 else if (OVL_USED (tmp1))
4947 continue; /* this is a using decl */
4948 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4949 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4951 /* There was already a non-using declaration in
4952 this scope with the same parameter types. If both
4953 are the same extern "C" functions, that's ok. */
4954 if (!decls_match (new_fn, old_fn))
4955 error ("`%D' is already declared in this scope", name);
4956 break;
4960 /* If we broke out of the loop, there's no reason to add
4961 this function to the using declarations for this
4962 scope. */
4963 if (tmp1)
4964 continue;
4966 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4967 if (TREE_CODE (*newval) != OVERLOAD)
4968 *newval = ovl_cons (*newval, NULL_TREE);
4969 OVL_USED (*newval) = 1;
4972 else
4974 *newval = BINDING_VALUE (decls);
4975 if (oldval)
4976 duplicate_decls (*newval, oldval);
4979 *newtype = BINDING_TYPE (decls);
4980 if (oldtype && *newtype && oldtype != *newtype)
4982 error ("using declaration `%D' introduced ambiguous type `%T'",
4983 name, oldtype);
4984 return;
4988 /* Process a using-declaration not appearing in class or local scope. */
4990 void
4991 do_toplevel_using_decl (decl)
4992 tree decl;
4994 tree scope, name, binding;
4995 tree oldval, oldtype, newval, newtype;
4997 decl = validate_nonmember_using_decl (decl, &scope, &name);
4998 if (decl == NULL_TREE)
4999 return;
5001 binding = binding_for_name (name, current_namespace);
5003 oldval = BINDING_VALUE (binding);
5004 oldtype = BINDING_TYPE (binding);
5006 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5008 /* Copy declarations found. */
5009 if (newval)
5010 BINDING_VALUE (binding) = newval;
5011 if (newtype)
5012 BINDING_TYPE (binding) = newtype;
5013 return;
5016 /* Process a using-declaration at function scope. */
5018 void
5019 do_local_using_decl (decl)
5020 tree decl;
5022 tree scope, name;
5023 tree oldval, oldtype, newval, newtype;
5025 decl = validate_nonmember_using_decl (decl, &scope, &name);
5026 if (decl == NULL_TREE)
5027 return;
5029 if (building_stmt_tree ()
5030 && at_function_scope_p ())
5031 add_decl_stmt (decl);
5033 oldval = lookup_name_current_level (name);
5034 oldtype = lookup_type_current_level (name);
5036 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5038 if (newval)
5040 if (is_overloaded_fn (newval))
5042 tree fn, term;
5044 /* We only need to push declarations for those functions
5045 that were not already bound in the current level.
5046 The old value might be NULL_TREE, it might be a single
5047 function, or an OVERLOAD. */
5048 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5049 term = OVL_FUNCTION (oldval);
5050 else
5051 term = oldval;
5052 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5053 fn = OVL_NEXT (fn))
5054 push_overloaded_decl (OVL_CURRENT (fn),
5055 PUSH_LOCAL | PUSH_USING);
5057 else
5058 push_local_binding (name, newval, PUSH_USING);
5060 if (newtype)
5061 set_identifier_type_value (name, newtype);
5064 tree
5065 do_class_using_decl (decl)
5066 tree decl;
5068 tree name, value;
5070 if (TREE_CODE (decl) != SCOPE_REF
5071 || !TYPE_P (TREE_OPERAND (decl, 0)))
5073 error ("using-declaration for non-member at class scope");
5074 return NULL_TREE;
5076 name = TREE_OPERAND (decl, 1);
5077 if (TREE_CODE (name) == BIT_NOT_EXPR)
5079 error ("using-declaration for destructor");
5080 return NULL_TREE;
5082 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5084 name = TREE_OPERAND (name, 0);
5085 error ("a using-declaration cannot specify a template-id. Try `using %T::%D'", TREE_OPERAND (decl, 0), name);
5086 return NULL_TREE;
5088 if (TREE_CODE (name) == TYPE_DECL || TREE_CODE (name) == TEMPLATE_DECL)
5089 name = DECL_NAME (name);
5091 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5093 value = build_lang_decl (USING_DECL, name, void_type_node);
5094 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5095 return value;
5098 /* Process a using-directive. */
5100 void
5101 do_using_directive (namespace)
5102 tree namespace;
5104 if (building_stmt_tree ())
5105 add_stmt (build_stmt (USING_STMT, namespace));
5107 /* using namespace A::B::C; */
5108 if (TREE_CODE (namespace) == SCOPE_REF)
5109 namespace = TREE_OPERAND (namespace, 1);
5110 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5112 /* Lookup in lexer did not find a namespace. */
5113 if (!processing_template_decl)
5114 error ("namespace `%T' undeclared", namespace);
5115 return;
5117 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5119 if (!processing_template_decl)
5120 error ("`%T' is not a namespace", namespace);
5121 return;
5123 namespace = ORIGINAL_NAMESPACE (namespace);
5124 if (!toplevel_bindings_p ())
5125 push_using_directive (namespace);
5126 else
5127 /* direct usage */
5128 add_using_namespace (current_namespace, namespace, 0);
5131 void
5132 check_default_args (x)
5133 tree x;
5135 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5136 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5137 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5139 if (TREE_PURPOSE (arg))
5140 saw_def = 1;
5141 else if (saw_def)
5143 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5144 i, x);
5145 break;
5150 void
5151 mark_used (decl)
5152 tree decl;
5154 TREE_USED (decl) = 1;
5155 if (processing_template_decl)
5156 return;
5157 assemble_external (decl);
5159 /* Is it a synthesized method that needs to be synthesized? */
5160 if (TREE_CODE (decl) == FUNCTION_DECL
5161 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)
5162 && DECL_ARTIFICIAL (decl)
5163 && ! DECL_INITIAL (decl)
5164 /* Kludge: don't synthesize for default args. */
5165 && current_function_decl)
5167 synthesize_method (decl);
5168 /* If we've already synthesized the method we don't need to
5169 instantiate it, so we can return right away. */
5170 return;
5173 /* If this is a function or variable that is an instance of some
5174 template, we now know that we will need to actually do the
5175 instantiation. We check that DECL is not an explicit
5176 instantiation because that is not checked in instantiate_decl. */
5177 if ((DECL_NON_THUNK_FUNCTION_P (decl) || TREE_CODE (decl) == VAR_DECL)
5178 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5179 && (!DECL_EXPLICIT_INSTANTIATION (decl)
5180 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))))
5181 instantiate_decl (decl, /*defer_ok=*/1);
5184 /* Helper function for class_head_decl and class_head_defn
5185 nonterminals. AGGR is the class, union or struct tag. SCOPE is the
5186 explicit scope used (NULL for no scope resolution). ID is the
5187 name. DEFN_P is true, if this is a definition of the class and
5188 NEW_TYPE_P is set to non-zero, if we push into the scope containing
5189 the to be defined aggregate.
5191 Return a TYPE_DECL for the type declared by ID in SCOPE. */
5193 tree
5194 handle_class_head (aggr, scope, id, defn_p, new_type_p)
5195 tree aggr, scope, id;
5196 int defn_p;
5197 int *new_type_p;
5199 tree decl = NULL_TREE;
5200 tree current = current_scope ();
5201 bool xrefd_p = false;
5203 if (current == NULL_TREE)
5204 current = current_namespace;
5206 *new_type_p = 0;
5208 if (scope)
5210 if (TREE_CODE (id) == TYPE_DECL)
5211 /* We must bash typedefs back to the main decl of the
5212 type. Otherwise we become confused about scopes. */
5213 decl = TYPE_MAIN_DECL (TREE_TYPE (id));
5214 else if (DECL_CLASS_TEMPLATE_P (id))
5215 decl = DECL_TEMPLATE_RESULT (id);
5216 else
5218 if (TYPE_P (scope))
5220 /* According to the suggested resolution of core issue
5221 180, 'typename' is assumed after a class-key. */
5222 decl = make_typename_type (scope, id, 1);
5223 if (decl != error_mark_node)
5224 decl = TYPE_MAIN_DECL (decl);
5225 else
5226 decl = NULL_TREE;
5228 else if (scope == current)
5230 /* We've been given AGGR SCOPE::ID, when we're already
5231 inside SCOPE. Be nice about it. */
5232 if (pedantic)
5233 pedwarn ("extra qualification `%T::' on member `%D' ignored",
5234 scope, id);
5236 else
5237 error ("`%T' does not have a class or union named `%D'",
5238 scope, id);
5242 if (!decl)
5244 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, !defn_p));
5245 xrefd_p = true;
5248 if (!TYPE_BINFO (TREE_TYPE (decl)))
5250 error ("`%T' is not a class or union type", decl);
5251 return error_mark_node;
5254 if (defn_p)
5256 /* For a definition, we want to enter the containing scope
5257 before looking up any base classes etc. Only do so, if this
5258 is different to the current scope. */
5259 tree context = CP_DECL_CONTEXT (decl);
5261 *new_type_p = (current != context
5262 && TREE_CODE (context) != TEMPLATE_TYPE_PARM
5263 && TREE_CODE (context) != BOUND_TEMPLATE_TEMPLATE_PARM);
5264 if (*new_type_p)
5265 push_scope (context);
5267 if (!xrefd_p && PROCESSING_REAL_TEMPLATE_DECL_P ())
5268 decl = push_template_decl (decl);
5271 return decl;
5274 /* Initialize decl2.c. */
5276 void
5277 init_decl2 ()
5279 ggc_add_tree_varray_root (&deferred_fns, 1);
5280 ggc_add_tree_varray_root (&pending_statics, 1);
5281 ggc_add_tree_varray_root (&ssdf_decls, 1);
5282 ggc_add_tree_root (&ssdf_decl, 1);
5283 ggc_add_tree_root (&priority_decl, 1);
5284 ggc_add_tree_root (&initialize_p_decl, 1);