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