* class.c (create_vtable_ptr): Put the vtable at the beginning of
[official-gcc.git] / gcc / cp / decl2.c
blob5cc3da59488503e45078282e0d75a1ad494355b9
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
27 /* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
30 #include "config.h"
31 #include "system.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "lex.h"
38 #include "output.h"
39 #include "except.h"
40 #include "expr.h"
41 #include "defaults.h"
42 #include "toplev.h"
43 #include "dwarf2out.h"
44 #include "dwarfout.h"
45 #include "ggc.h"
47 #if USE_CPPLIB
48 #include "cpplib.h"
49 extern cpp_reader parse_in;
50 #endif
52 /* This structure contains information about the initializations
53 and/or destructions required for a particular priority level. */
54 typedef struct priority_info_s {
55 /* Non-zero if there have been any initializations at this priority
56 throughout the translation unit. */
57 int initializations_p;
58 /* Non-zero if there have been any destructions at this priority
59 throughout the translation unit. */
60 int destructions_p;
61 } *priority_info;
63 static tree get_sentry PROTO((tree));
64 static void mark_vtable_entries PROTO((tree));
65 static void grok_function_init PROTO((tree, tree));
66 static int finish_vtable_vardecl PROTO((tree *, void *));
67 static int prune_vtable_vardecl PROTO((tree *, void *));
68 static int is_namespace_ancestor PROTO((tree, tree));
69 static void add_using_namespace PROTO((tree, tree, int));
70 static tree ambiguous_decl PROTO((tree, tree, tree,int));
71 static tree build_anon_union_vars PROTO((tree, tree*, int, int));
72 static int acceptable_java_type PROTO((tree));
73 static void output_vtable_inherit PROTO((tree));
74 static tree start_objects PROTO((int, int));
75 static void finish_objects PROTO((int, int, tree));
76 static tree merge_functions PROTO((tree, tree));
77 static tree decl_namespace PROTO((tree));
78 static tree validate_nonmember_using_decl PROTO((tree, tree *, tree *));
79 static void do_nonmember_using_decl PROTO((tree, tree, tree, tree,
80 tree *, tree *));
81 static tree start_static_storage_duration_function PROTO((void));
82 static void finish_static_storage_duration_function PROTO((tree));
83 static priority_info get_priority_info PROTO((int));
84 static void do_static_initialization PROTO((tree, tree));
85 static void do_static_destruction PROTO((tree));
86 static tree start_static_initialization_or_destruction PROTO((tree, int));
87 static void finish_static_initialization_or_destruction PROTO((tree));
88 static void generate_ctor_or_dtor_function PROTO((int, int));
89 static int generate_ctor_and_dtor_functions_for_priority
90 PROTO((splay_tree_node, void *));
91 static tree prune_vars_needing_no_initialization PROTO((tree));
92 static void write_out_vars PROTO((tree));
94 extern int current_class_depth;
96 /* A list of virtual function tables we must make sure to write out. */
97 tree pending_vtables;
99 /* A list of static class variables. This is needed, because a
100 static class variable can be declared inside the class without
101 an initializer, and then initialized, staticly, outside the class. */
102 static varray_type pending_statics;
103 #define pending_statics_used \
104 (pending_statics ? pending_statics->elements_used : 0)
106 /* A list of functions which were declared inline, but which we
107 may need to emit outline anyway. */
108 static varray_type saved_inlines;
109 #define saved_inlines_used \
110 (saved_inlines ? saved_inlines->elements_used : 0)
112 /* Same, but not reset. Local temp variables and global temp variables
113 can have the same name. */
114 static int global_temp_name_counter;
116 /* Flag used when debugging spew.c */
118 extern int spew_debug;
120 /* Nonzero if we're done parsing and into end-of-file activities. */
122 int at_eof;
124 /* Functions called along with real static constructors and destructors. */
126 tree static_ctors, static_dtors;
128 /* The :: namespace. */
130 tree global_namespace;
132 /* The stack for namespaces of current declarations. */
134 static tree decl_namespace_list;
137 /* C (and C++) language-specific option variables. */
139 /* Nonzero means allow type mismatches in conditional expressions;
140 just make their values `void'. */
142 int flag_cond_mismatch;
144 /* Nonzero means give `double' the same size as `float'. */
146 int flag_short_double;
148 /* Nonzero means don't recognize the keyword `asm'. */
150 int flag_no_asm;
152 /* Nonzero means don't recognize any extension keywords. */
154 int flag_no_gnu_keywords;
156 /* Nonzero means don't recognize the non-ANSI builtin functions. */
158 int flag_no_builtin;
160 /* Nonzero means don't recognize the non-ANSI builtin functions.
161 -ansi sets this. */
163 int flag_no_nonansi_builtin;
165 /* Nonzero means do some things the same way PCC does. Only provided so
166 the compiler will link. */
168 int flag_traditional;
170 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
172 int flag_signed_bitfields = 1;
174 /* Nonzero means enable obscure ANSI features and disable GNU extensions
175 that might cause ANSI-compliant code to be miscompiled. */
177 int flag_ansi;
179 /* Nonzero means do emit exported implementations of functions even if
180 they can be inlined. */
182 int flag_implement_inlines = 1;
184 /* Nonzero means do emit exported implementations of templates, instead of
185 multiple static copies in each file that needs a definition. */
187 int flag_external_templates;
189 /* Nonzero means that the decision to emit or not emit the implementation of a
190 template depends on where the template is instantiated, rather than where
191 it is defined. */
193 int flag_alt_external_templates;
195 /* Nonzero means that implicit instantiations will be emitted if needed. */
197 int flag_implicit_templates = 1;
199 /* Nonzero means that implicit instantiations of inline templates will be
200 emitted if needed, even if instantiations of non-inline templates
201 aren't. */
203 int flag_implicit_inline_templates = 1;
205 /* Nonzero means warn about implicit declarations. */
207 int warn_implicit = 1;
209 /* Nonzero means warn about usage of long long when `-pedantic'. */
211 int warn_long_long = 1;
213 /* Nonzero means warn when all ctors or dtors are private, and the class
214 has no friends. */
216 int warn_ctor_dtor_privacy = 1;
218 /* True if we want to implement vtables using "thunks".
219 The default is off. */
221 #ifndef DEFAULT_VTABLE_THUNKS
222 #define DEFAULT_VTABLE_THUNKS 0
223 #endif
224 int flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
226 /* Nonzero means generate separate instantiation control files and juggle
227 them at link time. */
229 int flag_use_repository;
231 /* Nonzero if we want to issue diagnostics that the standard says are not
232 required. */
234 int flag_optional_diags = 1;
236 /* Nonzero means give string constants the type `const char *', as mandated
237 by the standard. */
239 int flag_const_strings = 1;
241 /* If non-NULL, dump the tree structure for the entire translation
242 unit to this file. */
244 char *flag_dump_translation_unit = 0;
246 /* Nonzero means warn about deprecated conversion from string constant to
247 `char *'. */
249 int warn_write_strings;
251 /* Nonzero means warn about pointer casts that can drop a type qualifier
252 from the pointer target type. */
254 int warn_cast_qual;
256 /* Nonzero means warn about sizeof(function) or addition/subtraction
257 of function pointers. */
259 int warn_pointer_arith = 1;
261 /* Nonzero means warn for any function def without prototype decl. */
263 int warn_missing_prototypes;
265 /* Nonzero means warn about multiple (redundant) decls for the same single
266 variable or function. */
268 int warn_redundant_decls;
270 /* Warn if initializer is not completely bracketed. */
272 int warn_missing_braces;
274 /* Warn about comparison of signed and unsigned values. */
276 int warn_sign_compare;
278 /* Warn about testing equality of floating point numbers. */
280 int warn_float_equal = 0;
282 /* Warn about *printf or *scanf format/argument anomalies. */
284 int warn_format;
286 /* Warn about a subscript that has type char. */
288 int warn_char_subscripts;
290 /* Warn if a type conversion is done that might have confusing results. */
292 int warn_conversion;
294 /* Warn if adding () is suggested. */
296 int warn_parentheses;
298 /* Non-zero means warn in function declared in derived class has the
299 same name as a virtual in the base class, but fails to match the
300 type signature of any virtual function in the base class. */
301 int warn_overloaded_virtual;
303 /* Non-zero means warn when declaring a class that has a non virtual
304 destructor, when it really ought to have a virtual one. */
305 int warn_nonvdtor;
307 /* Non-zero means warn when a function is declared extern and later inline. */
308 int warn_extern_inline;
310 /* Non-zero means warn when the compiler will reorder code. */
311 int warn_reorder;
313 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
314 int warn_synth;
316 /* Non-zero means warn when we convert a pointer to member function
317 into a pointer to (void or function). */
318 int warn_pmf2ptr = 1;
320 /* Nonzero means warn about violation of some Effective C++ style rules. */
322 int warn_ecpp;
324 /* Nonzero means warn where overload resolution chooses a promotion from
325 unsigned to signed over a conversion to an unsigned of the same size. */
327 int warn_sign_promo;
329 /* Nonzero means warn when an old-style cast is used. */
331 int warn_old_style_cast;
333 /* Warn about #pragma directives that are not recognised. */
335 int warn_unknown_pragmas; /* Tri state variable. */
337 /* Nonzero means warn about use of multicharacter literals. */
339 int warn_multichar = 1;
341 /* Nonzero means warn when non-templatized friend functions are
342 declared within a template */
344 int warn_nontemplate_friend = 1;
346 /* Nonzero means complain about deprecated features. */
348 int warn_deprecated = 1;
350 /* Nonzero means `$' can be in an identifier. */
352 #ifndef DOLLARS_IN_IDENTIFIERS
353 #define DOLLARS_IN_IDENTIFIERS 1
354 #endif
355 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
357 /* Nonzero for -fno-strict-prototype switch: do not consider empty
358 argument prototype to mean function takes no arguments. */
360 int flag_strict_prototype = 2;
361 int strict_prototype = 1;
362 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
364 /* Nonzero means that labels can be used as first-class objects */
366 int flag_labels_ok;
368 /* Nonzero means allow Microsoft extensions without a pedwarn. */
370 int flag_ms_extensions;
372 /* Non-zero means to collect statistics which might be expensive
373 and to print them when we are done. */
374 int flag_detailed_statistics;
376 /* C++ specific flags. */
377 /* Zero means that `this' is a *const. This gives nice behavior in the
378 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
379 -2 means we're constructing an object and it has fixed type. */
381 int flag_this_is_variable;
383 /* Nonzero means we should attempt to elide constructors when possible. */
385 int flag_elide_constructors = 1;
387 /* Nonzero means that member functions defined in class scope are
388 inline by default. */
390 int flag_default_inline = 1;
392 /* Controls whether compiler generates 'type descriptor' that give
393 run-time type information. */
394 int flag_rtti = 1;
396 /* Nonzero if we wish to output cross-referencing information
397 for the GNU class browser. */
398 extern int flag_gnu_xref;
400 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
401 objects. */
403 int flag_huge_objects;
405 /* Nonzero if we want to conserve space in the .o files. We do this
406 by putting uninitialized data and runtime initialized data into
407 .common instead of .data at the expense of not flagging multiple
408 definitions. */
410 int flag_conserve_space;
412 /* Nonzero if we want to obey access control semantics. */
414 int flag_access_control = 1;
416 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
418 int flag_operator_names;
420 /* Nonzero if we want to check the return value of new and avoid calling
421 constructors if it is a null pointer. */
423 int flag_check_new;
425 /* Nonzero if we want the new ANSI rules for pushing a new scope for `for'
426 initialization variables.
427 0: Old rules, set by -fno-for-scope.
428 2: New ANSI rules, set by -ffor-scope.
429 1: Try to implement new ANSI rules, but with backup compatibility
430 (and warnings). This is the default, for now. */
432 int flag_new_for_scope = 1;
434 /* Nonzero if we want to emit defined symbols with common-like linkage as
435 weak symbols where possible, in order to conform to C++ semantics.
436 Otherwise, emit them as local symbols. */
438 int flag_weak = 1;
440 /* Nonzero to enable experimental ABI changes. */
442 int flag_new_abi = 1;
444 /* Nonzero to use __cxa_atexit, rather than atexit, to register
445 destructors for local statics and global objects. */
447 int flag_use_cxa_atexit;
449 /* Nonzero to not ignore namespace std. */
451 int flag_honor_std;
453 /* Nonzero if we should expand functions calls inline at the tree
454 level, rather than at the RTL level. */
456 int flag_inline_trees = 0;
458 /* Maximum template instantiation depth. Must be at least 17 for ANSI
459 compliance. */
461 int max_tinst_depth = 17;
463 /* The name-mangling scheme to use. Must be 1 or greater to support
464 template functions with identical types, but different template
465 arguments. */
466 int name_mangling_version = 2;
468 /* Nonzero means that guiding declarations are allowed. */
469 int flag_guiding_decls;
471 /* Nonzero if wchar_t should be `unsigned short' instead of whatever it
472 would normally be, for use with WINE. */
473 int flag_short_wchar;
475 /* Nonzero if squashed mangling is to be performed.
476 This uses the B and K codes to reference previously seen class types
477 and class qualifiers. */
478 int flag_do_squangling;
480 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
482 int flag_vtable_gc;
484 /* Nonzero means make the default pedwarns warnings instead of errors.
485 The value of this flag is ignored if -pedantic is specified. */
487 int flag_permissive;
489 /* If this variable is defined to a non-NULL value, it will be called
490 after the file has been completely parsed. */
492 void (*back_end_hook) PROTO((tree));
494 /* Table of language-dependent -f options.
495 STRING is the option name. VARIABLE is the address of the variable.
496 ON_VALUE is the value to store in VARIABLE
497 if `-fSTRING' is seen as an option.
498 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
500 static struct { const char *string; int *variable; int on_value;}
501 lang_f_options[] =
503 /* C/C++ options. */
504 {"signed-char", &flag_signed_char, 1},
505 {"unsigned-char", &flag_signed_char, 0},
506 {"signed-bitfields", &flag_signed_bitfields, 1},
507 {"unsigned-bitfields", &flag_signed_bitfields, 0},
508 {"short-enums", &flag_short_enums, 1},
509 {"short-double", &flag_short_double, 1},
510 {"short-wchar", &flag_short_wchar, 1},
511 {"cond-mismatch", &flag_cond_mismatch, 1},
512 {"asm", &flag_no_asm, 0},
513 {"builtin", &flag_no_builtin, 0},
515 /* C++-only options. */
516 {"access-control", &flag_access_control, 1},
517 {"check-new", &flag_check_new, 1},
518 {"conserve-space", &flag_conserve_space, 1},
519 {"const-strings", &flag_const_strings, 1},
520 {"default-inline", &flag_default_inline, 1},
521 {"dollars-in-identifiers", &dollars_in_ident, 1},
522 {"elide-constructors", &flag_elide_constructors, 1},
523 {"external-templates", &flag_external_templates, 1},
524 {"for-scope", &flag_new_for_scope, 2},
525 {"gnu-keywords", &flag_no_gnu_keywords, 0},
526 {"handle-exceptions", &flag_exceptions, 1},
527 {"honor-std", &flag_honor_std, 1},
528 {"huge-objects", &flag_huge_objects, 1},
529 {"implement-inlines", &flag_implement_inlines, 1},
530 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
531 {"implicit-templates", &flag_implicit_templates, 1},
532 {"labels-ok", &flag_labels_ok, 1},
533 {"ms-extensions", &flag_ms_extensions, 1},
534 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
535 {"operator-names", &flag_operator_names, 1},
536 {"optional-diags", &flag_optional_diags, 1},
537 {"permissive", &flag_permissive, 1},
538 {"repo", &flag_use_repository, 1},
539 {"rtti", &flag_rtti, 1},
540 {"squangle", &flag_do_squangling, 1},
541 {"stats", &flag_detailed_statistics, 1},
542 {"strict-prototype", &flag_strict_prototype, 1},
543 {"use-cxa-atexit", &flag_use_cxa_atexit, 1},
544 {"vtable-gc", &flag_vtable_gc, 1},
545 {"vtable-thunks", &flag_vtable_thunks, 1},
546 {"weak", &flag_weak, 1},
547 {"xref", &flag_gnu_xref, 1}
550 /* Decode the string P as a language-specific option.
551 Return the number of strings consumed for a valid option.
552 Otherwise return 0. Should not complain if it does not
553 recognise the option. */
555 int
556 lang_decode_option (argc, argv)
557 int argc
558 #if !USE_CPPLIB
559 ATTRIBUTE_UNUSED
560 #endif
562 char **argv;
565 int strings_processed;
566 char *p = argv[0];
567 #if USE_CPPLIB
568 strings_processed = cpp_handle_option (&parse_in, argc, argv);
569 #else
570 strings_processed = 0;
571 #endif /* ! USE_CPPLIB */
573 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
574 /* ignore */;
575 else if (p[0] == '-' && p[1] == 'f')
577 /* Some kind of -f option.
578 P's value is the option sans `-f'.
579 Search for it in the table of options. */
580 size_t j;
582 p += 2;
583 /* Try special -f options. */
585 if (!strcmp (p, "handle-exceptions")
586 || !strcmp (p, "no-handle-exceptions"))
587 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
588 else if (!strcmp (p, "all-virtual")
589 || !strcmp (p, "enum-int-equiv")
590 || !strcmp (p, "no-nonnull-objects")
591 || !strcmp (p, "this-is-variable"))
592 warning ("-f%s is no longer supported", p);
593 else if (! strcmp (p, "alt-external-templates"))
595 flag_external_templates = 1;
596 flag_alt_external_templates = 1;
597 cp_deprecated ("-falt-external-templates");
599 else if (! strcmp (p, "no-alt-external-templates"))
600 flag_alt_external_templates = 0;
601 else if (!strcmp (p, "repo"))
603 flag_use_repository = 1;
604 flag_implicit_templates = 0;
606 else if (!strcmp (p, "guiding-decls"))
608 flag_guiding_decls = 1;
609 name_mangling_version = 0;
611 else if (!strcmp (p, "no-guiding-decls"))
612 flag_guiding_decls = 0;
613 else if (!strcmp (p, "external-templates"))
615 flag_external_templates = 1;
616 cp_deprecated ("-fexternal-templates");
618 else if (!strcmp (p, "new-abi"))
620 flag_new_abi = 1;
621 flag_do_squangling = 1;
622 flag_honor_std = 1;
623 flag_vtable_thunks = 1;
625 else if (!strcmp (p, "no-new-abi"))
627 flag_new_abi = 0;
628 flag_do_squangling = 0;
629 flag_honor_std = 0;
631 else if (!strncmp (p, "template-depth-", 15))
632 max_tinst_depth
633 = read_integral_parameter (p + 15, p - 2, max_tinst_depth);
634 else if (!strncmp (p, "name-mangling-version-", 22))
635 name_mangling_version
636 = read_integral_parameter (p + 22, p - 2, name_mangling_version);
637 else if (!strncmp (p, "dump-translation-unit-", 22))
639 if (p[22] == '\0')
640 error ("no file specified with -fdump-translation-unit");
641 else
642 flag_dump_translation_unit = p + 22;
644 else
646 int found = 0;
648 for (j = 0;
649 !found && j < (sizeof (lang_f_options)
650 / sizeof (lang_f_options[0]));
651 j++)
653 if (!strcmp (p, lang_f_options[j].string))
655 *lang_f_options[j].variable = lang_f_options[j].on_value;
656 /* A goto here would be cleaner,
657 but breaks the vax pcc. */
658 found = 1;
660 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
661 && ! strcmp (p+3, lang_f_options[j].string))
663 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
664 found = 1;
668 return found;
671 else if (p[0] == '-' && p[1] == 'W')
673 int setting = 1;
675 /* The -W options control the warning behavior of the compiler. */
676 p += 2;
678 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
679 setting = 0, p += 3;
681 if (!strcmp (p, "implicit"))
682 warn_implicit = setting;
683 else if (!strcmp (p, "long-long"))
684 warn_long_long = setting;
685 else if (!strcmp (p, "return-type"))
686 warn_return_type = setting;
687 else if (!strcmp (p, "ctor-dtor-privacy"))
688 warn_ctor_dtor_privacy = setting;
689 else if (!strcmp (p, "write-strings"))
690 warn_write_strings = setting;
691 else if (!strcmp (p, "cast-qual"))
692 warn_cast_qual = setting;
693 else if (!strcmp (p, "char-subscripts"))
694 warn_char_subscripts = setting;
695 else if (!strcmp (p, "pointer-arith"))
696 warn_pointer_arith = setting;
697 else if (!strcmp (p, "missing-prototypes"))
698 warn_missing_prototypes = setting;
699 else if (!strcmp (p, "redundant-decls"))
700 warn_redundant_decls = setting;
701 else if (!strcmp (p, "missing-braces"))
702 warn_missing_braces = setting;
703 else if (!strcmp (p, "sign-compare"))
704 warn_sign_compare = setting;
705 else if (!strcmp (p, "float-equal"))
706 warn_float_equal = setting;
707 else if (!strcmp (p, "format"))
708 warn_format = setting;
709 else if (!strcmp (p, "conversion"))
710 warn_conversion = setting;
711 else if (!strcmp (p, "parentheses"))
712 warn_parentheses = setting;
713 else if (!strcmp (p, "non-virtual-dtor"))
714 warn_nonvdtor = setting;
715 else if (!strcmp (p, "extern-inline"))
716 warn_extern_inline = setting;
717 else if (!strcmp (p, "reorder"))
718 warn_reorder = setting;
719 else if (!strcmp (p, "synth"))
720 warn_synth = setting;
721 else if (!strcmp (p, "pmf-conversions"))
722 warn_pmf2ptr = setting;
723 else if (!strcmp (p, "effc++"))
724 warn_ecpp = setting;
725 else if (!strcmp (p, "sign-promo"))
726 warn_sign_promo = setting;
727 else if (!strcmp (p, "old-style-cast"))
728 warn_old_style_cast = setting;
729 else if (!strcmp (p, "overloaded-virtual"))
730 warn_overloaded_virtual = setting;
731 else if (!strcmp (p, "multichar"))
732 warn_multichar = setting;
733 else if (!strcmp (p, "unknown-pragmas"))
734 /* Set to greater than 1, so that even unknown pragmas in
735 system headers will be warned about. */
736 warn_unknown_pragmas = setting * 2;
737 else if (!strcmp (p, "non-template-friend"))
738 warn_nontemplate_friend = setting;
739 else if (!strcmp (p, "deprecated"))
740 warn_deprecated = setting;
741 else if (!strcmp (p, "comment"))
742 ; /* cpp handles this one. */
743 else if (!strcmp (p, "comments"))
744 ; /* cpp handles this one. */
745 else if (!strcmp (p, "trigraphs"))
746 ; /* cpp handles this one. */
747 else if (!strcmp (p, "import"))
748 ; /* cpp handles this one. */
749 else if (!strcmp (p, "all"))
751 warn_return_type = setting;
752 warn_unused = setting;
753 warn_implicit = setting;
754 warn_switch = setting;
755 warn_format = setting;
756 warn_parentheses = setting;
757 warn_missing_braces = setting;
758 warn_sign_compare = setting;
759 warn_multichar = setting;
760 /* We save the value of warn_uninitialized, since if they put
761 -Wuninitialized on the command line, we need to generate a
762 warning about not using it without also specifying -O. */
763 if (warn_uninitialized != 1)
764 warn_uninitialized = (setting ? 2 : 0);
765 /* Only warn about unknown pragmas that are not in system
766 headers. */
767 warn_unknown_pragmas = 1;
769 /* C++-specific warnings. */
770 warn_ctor_dtor_privacy = setting;
771 warn_nonvdtor = setting;
772 warn_reorder = setting;
773 warn_nontemplate_friend = setting;
775 else return strings_processed;
777 else if (!strcmp (p, "-ansi"))
778 flag_no_nonansi_builtin = 1, flag_ansi = 1,
779 flag_no_gnu_keywords = 1, flag_operator_names = 1;
780 #ifdef SPEW_DEBUG
781 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
782 it's probably safe to assume no sane person would ever want to use this
783 under normal circumstances. */
784 else if (!strcmp (p, "-spew-debug"))
785 spew_debug = 1;
786 #endif
787 else
788 return strings_processed;
790 return 1;
793 /* Incorporate `const' and `volatile' qualifiers for member functions.
794 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
795 QUALS is a list of qualifiers. Returns any explicit
796 top-level qualifiers of the method's this pointer, anything other than
797 TYPE_UNQUALIFIED will be an extension. */
800 grok_method_quals (ctype, function, quals)
801 tree ctype, function, quals;
803 tree fntype = TREE_TYPE (function);
804 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
805 int type_quals = TYPE_UNQUALIFIED;
806 int dup_quals = TYPE_UNQUALIFIED;
807 int this_quals = TYPE_UNQUALIFIED;
811 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
813 if ((type_quals | this_quals) & tq)
814 dup_quals |= tq;
815 else if (tq & TYPE_QUAL_RESTRICT)
816 this_quals |= tq;
817 else
818 type_quals |= tq;
819 quals = TREE_CHAIN (quals);
821 while (quals);
823 if (dup_quals != TYPE_UNQUALIFIED)
824 cp_error ("duplicate type qualifiers in %s declaration",
825 TREE_CODE (function) == FUNCTION_DECL
826 ? "member function" : "type");
828 ctype = cp_build_qualified_type (ctype, type_quals);
829 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
830 (TREE_CODE (fntype) == METHOD_TYPE
831 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
832 : TYPE_ARG_TYPES (fntype)));
833 if (raises)
834 fntype = build_exception_variant (fntype, raises);
836 TREE_TYPE (function) = fntype;
837 return this_quals;
840 /* Warn when -fexternal-templates is used and #pragma
841 interface/implementation is not used all the times it should be,
842 inform the user. */
844 void
845 warn_if_unknown_interface (decl)
846 tree decl;
848 static int already_warned = 0;
849 if (already_warned++)
850 return;
852 if (flag_alt_external_templates)
854 struct tinst_level *til = tinst_for_decl ();
855 int sl = lineno;
856 char *sf = input_filename;
858 if (til)
860 lineno = til->line;
861 input_filename = til->file;
863 cp_warning ("template `%#D' instantiated in file without #pragma interface",
864 decl);
865 lineno = sl;
866 input_filename = sf;
868 else
869 cp_warning_at ("template `%#D' defined in file without #pragma interface",
870 decl);
873 /* A subroutine of the parser, to handle a component list. */
875 void
876 grok_x_components (specs)
877 tree specs;
879 struct pending_inline **p;
880 tree t;
882 specs = strip_attrs (specs);
884 check_tag_decl (specs);
885 t = groktypename (build_decl_list (specs, NULL_TREE));
887 /* The only case where we need to do anything additional here is an
888 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
889 if (t == NULL_TREE || !ANON_AGGR_TYPE_P (t))
890 return;
892 fixup_anonymous_aggr (t);
893 finish_member_declaration (build_lang_decl (FIELD_DECL, NULL_TREE, t));
895 /* Ignore any inline function definitions in the anonymous union
896 since an anonymous union may not have function members. */
897 p = &pending_inlines;
898 for (; *p; *p = (*p)->next)
899 if (DECL_CONTEXT ((*p)->fndecl) != t)
900 break;
903 /* Constructors for types with virtual baseclasses need an "in-charge" flag
904 saying whether this constructor is responsible for initialization of
905 virtual baseclasses or not. All destructors also need this "in-charge"
906 flag, which additionally determines whether or not the destructor should
907 free the memory for the object.
909 This function adds the "in-charge" flag to member function FN if
910 appropriate. It is called from grokclassfn and tsubst.
911 FN must be either a constructor or destructor. */
913 void
914 maybe_retrofit_in_chrg (fn)
915 tree fn;
917 tree basetype, arg_types, parms, parm, fntype;
919 if (DECL_CONSTRUCTOR_P (fn)
920 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CLASS_CONTEXT (fn))
921 && ! DECL_CONSTRUCTOR_FOR_VBASE_P (fn))
922 /* OK */;
923 else if (! DECL_CONSTRUCTOR_P (fn)
924 && TREE_CHAIN (DECL_ARGUMENTS (fn)) == NULL_TREE)
925 /* OK */;
926 else
927 return;
929 if (DECL_CONSTRUCTOR_P (fn))
930 DECL_CONSTRUCTOR_FOR_VBASE_P (fn) = 1;
932 /* First add it to DECL_ARGUMENTS... */
933 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
934 /* Mark the artificial `__in_chrg' parameter as "artificial". */
935 SET_DECL_ARTIFICIAL (parm);
936 DECL_ARG_TYPE (parm) = integer_type_node;
937 TREE_READONLY (parm) = 1;
938 parms = DECL_ARGUMENTS (fn);
939 TREE_CHAIN (parm) = TREE_CHAIN (parms);
940 TREE_CHAIN (parms) = parm;
942 /* ...and then to TYPE_ARG_TYPES. */
943 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
944 basetype = TREE_TYPE (TREE_VALUE (arg_types));
945 arg_types = hash_tree_chain (integer_type_node, TREE_CHAIN (arg_types));
946 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
947 arg_types);
948 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
949 fntype = build_exception_variant (fntype,
950 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
951 TREE_TYPE (fn) = fntype;
954 /* Classes overload their constituent function names automatically.
955 When a function name is declared in a record structure,
956 its name is changed to it overloaded name. Since names for
957 constructors and destructors can conflict, we place a leading
958 '$' for destructors.
960 CNAME is the name of the class we are grokking for.
962 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
964 FLAGS contains bits saying what's special about today's
965 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
967 If FUNCTION is a destructor, then we must add the `auto-delete' field
968 as a second parameter. There is some hair associated with the fact
969 that we must "declare" this variable in the manner consistent with the
970 way the rest of the arguments were declared.
972 QUALS are the qualifiers for the this pointer. */
974 void
975 grokclassfn (ctype, function, flags, quals)
976 tree ctype, function;
977 enum overload_flags flags;
978 tree quals;
980 tree fn_name = DECL_NAME (function);
981 int this_quals = TYPE_UNQUALIFIED;
983 if (fn_name == NULL_TREE)
985 error ("name missing for member function");
986 fn_name = get_identifier ("<anonymous>");
987 DECL_NAME (function) = fn_name;
990 if (quals)
991 this_quals = grok_method_quals (ctype, function, quals);
993 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
995 /* Must add the class instance variable up front. */
996 /* Right now we just make this a pointer. But later
997 we may wish to make it special. */
998 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (function)));
1000 tree parm = build_decl (PARM_DECL, this_identifier,
1001 cp_build_qualified_type (type, this_quals | TYPE_QUAL_CONST));
1002 /* Mark the artificial `this' parameter as "artificial". */
1003 SET_DECL_ARTIFICIAL (parm);
1004 DECL_ARG_TYPE (parm) = type;
1005 /* We can make this a register, so long as we don't
1006 accidentally complain if someone tries to take its address. */
1007 DECL_REGISTER (parm) = 1;
1008 TREE_CHAIN (parm) = last_function_parms;
1009 last_function_parms = parm;
1012 DECL_ARGUMENTS (function) = last_function_parms;
1013 /* First approximations. */
1014 DECL_CONTEXT (function) = ctype;
1015 DECL_CLASS_CONTEXT (function) = ctype;
1017 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1018 maybe_retrofit_in_chrg (function);
1020 if (flags == DTOR_FLAG)
1022 DECL_ASSEMBLER_NAME (function) = build_destructor_name (ctype);
1023 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1025 else
1026 set_mangled_name_for_decl (function);
1029 /* Work on the expr used by alignof (this is only called by the parser). */
1031 tree
1032 grok_alignof (expr)
1033 tree expr;
1035 tree best, t;
1036 int bestalign;
1038 if (processing_template_decl)
1039 return build_min (ALIGNOF_EXPR, sizetype, expr);
1041 if (TREE_CODE (expr) == COMPONENT_REF
1042 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1043 error ("`__alignof__' applied to a bit-field");
1045 if (TREE_CODE (expr) == INDIRECT_REF)
1047 best = t = TREE_OPERAND (expr, 0);
1048 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1050 while (TREE_CODE (t) == NOP_EXPR
1051 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1053 int thisalign;
1054 t = TREE_OPERAND (t, 0);
1055 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1056 if (thisalign > bestalign)
1057 best = t, bestalign = thisalign;
1059 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1061 else
1062 return c_alignof (TREE_TYPE (expr));
1065 /* Create an ARRAY_REF, checking for the user doing things backwards
1066 along the way. */
1068 tree
1069 grok_array_decl (array_expr, index_exp)
1070 tree array_expr, index_exp;
1072 tree type = TREE_TYPE (array_expr);
1073 tree p1, p2, i1, i2;
1075 if (type == error_mark_node || index_exp == error_mark_node)
1076 return error_mark_node;
1077 if (processing_template_decl)
1078 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1079 array_expr, index_exp);
1081 if (type == NULL_TREE)
1083 /* Something has gone very wrong. Assume we are mistakenly reducing
1084 an expression instead of a declaration. */
1085 error ("parser may be lost: is there a '{' missing somewhere?");
1086 return NULL_TREE;
1089 if (TREE_CODE (type) == OFFSET_TYPE
1090 || TREE_CODE (type) == REFERENCE_TYPE)
1091 type = TREE_TYPE (type);
1093 /* If they have an `operator[]', use that. */
1094 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1095 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1096 array_expr, index_exp, NULL_TREE);
1098 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1099 is a little-known fact that, if `a' is an array and `i' is an
1100 int, you can write `i[a]', which means the same thing as `a[i]'. */
1102 if (TREE_CODE (type) == ARRAY_TYPE)
1103 p1 = array_expr;
1104 else
1105 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1107 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1108 p2 = index_exp;
1109 else
1110 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1112 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1113 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1115 if ((p1 && i2) && (i1 && p2))
1116 error ("ambiguous conversion for array subscript");
1118 if (p1 && i2)
1119 array_expr = p1, index_exp = i2;
1120 else if (i1 && p2)
1121 array_expr = p2, index_exp = i1;
1122 else
1124 cp_error ("invalid types `%T[%T]' for array subscript",
1125 type, TREE_TYPE (index_exp));
1126 return error_mark_node;
1129 if (array_expr == error_mark_node || index_exp == error_mark_node)
1130 error ("ambiguous conversion for array subscript");
1132 return build_array_ref (array_expr, index_exp);
1135 /* Given the cast expression EXP, checking out its validity. Either return
1136 an error_mark_node if there was an unavoidable error, return a cast to
1137 void for trying to delete a pointer w/ the value 0, or return the
1138 call to delete. If DOING_VEC is 1, we handle things differently
1139 for doing an array delete. If DOING_VEC is 2, they gave us the
1140 array size as an argument to delete.
1141 Implements ARM $5.3.4. This is called from the parser. */
1143 tree
1144 delete_sanity (exp, size, doing_vec, use_global_delete)
1145 tree exp, size;
1146 int doing_vec, use_global_delete;
1148 tree t, type;
1149 /* For a regular vector delete (aka, no size argument) we will pass
1150 this down as a NULL_TREE into build_vec_delete. */
1151 tree maxindex = NULL_TREE;
1153 if (exp == error_mark_node)
1154 return exp;
1156 if (processing_template_decl)
1158 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1159 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1160 DELETE_EXPR_USE_VEC (t) = doing_vec;
1161 return t;
1164 if (TREE_CODE (exp) == OFFSET_REF)
1165 exp = resolve_offset_ref (exp);
1166 exp = convert_from_reference (exp);
1167 t = stabilize_reference (exp);
1168 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1170 if (t == NULL_TREE || t == error_mark_node)
1172 cp_error ("type `%#T' argument given to `delete', expected pointer",
1173 TREE_TYPE (exp));
1174 return error_mark_node;
1177 if (doing_vec == 2)
1179 maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node);
1180 pedwarn ("anachronistic use of array size in vector delete");
1183 type = TREE_TYPE (t);
1185 /* As of Valley Forge, you can delete a pointer to const. */
1187 /* You can't delete functions. */
1188 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1190 error ("cannot delete a function");
1191 return error_mark_node;
1194 /* Deleting ptr to void is undefined behaviour [expr.delete/3]. */
1195 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
1196 cp_warning ("`%T' is not a pointer-to-object type", type);
1198 /* An array can't have been allocated by new, so complain. */
1199 if (TREE_CODE (t) == ADDR_EXPR
1200 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1201 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1202 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1204 /* Deleting a pointer with the value zero is valid and has no effect. */
1205 if (integer_zerop (t))
1206 return build1 (NOP_EXPR, void_type_node, t);
1208 if (doing_vec)
1209 return build_vec_delete (t, maxindex, integer_one_node, use_global_delete);
1210 else
1212 if (IS_AGGR_TYPE (TREE_TYPE (type))
1213 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1215 /* Only do access checking here; we'll be calling op delete
1216 from the destructor. */
1217 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1218 LOOKUP_NORMAL, NULL_TREE);
1219 if (tmp == error_mark_node)
1220 return error_mark_node;
1223 return build_delete (type, t, integer_three_node,
1224 LOOKUP_NORMAL, use_global_delete);
1228 /* Report an error if the indicated template declaration is not the
1229 sort of thing that should be a member template. */
1231 void
1232 check_member_template (tmpl)
1233 tree tmpl;
1235 tree decl;
1237 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1238 decl = DECL_TEMPLATE_RESULT (tmpl);
1240 if (TREE_CODE (decl) == FUNCTION_DECL
1241 || (TREE_CODE (decl) == TYPE_DECL
1242 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1244 if (current_function_decl)
1245 /* 14.5.2.2 [temp.mem]
1247 A local class shall not have member templates. */
1248 cp_error ("declaration of member template `%#D' in local class",
1249 decl);
1251 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1253 /* 14.5.2.3 [temp.mem]
1255 A member function template shall not be virtual. */
1256 cp_error
1257 ("invalid use of `virtual' in template declaration of `%#D'",
1258 decl);
1259 DECL_VIRTUAL_P (decl) = 0;
1262 /* The debug-information generating code doesn't know what to do
1263 with member templates. */
1264 DECL_IGNORED_P (tmpl) = 1;
1266 else
1267 cp_error ("template declaration of `%#D'", decl);
1270 /* Return true iff TYPE is a valid Java parameter or return type. */
1272 static int
1273 acceptable_java_type (type)
1274 tree type;
1276 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1277 return 1;
1278 if (TREE_CODE (type) == POINTER_TYPE)
1280 type = TREE_TYPE (type);
1281 if (TREE_CODE (type) == RECORD_TYPE)
1283 tree args; int i;
1284 if (! TYPE_FOR_JAVA (type))
1285 return 0;
1286 if (! CLASSTYPE_TEMPLATE_INFO (type))
1287 return 1;
1288 args = CLASSTYPE_TI_ARGS (type);
1289 i = TREE_VEC_LENGTH (args);
1290 while (--i >= 0)
1292 type = TREE_VEC_ELT (args, i);
1293 if (TREE_CODE (type) == POINTER_TYPE)
1294 type = TREE_TYPE (type);
1295 if (! TYPE_FOR_JAVA (type))
1296 return 0;
1298 return 1;
1301 return 0;
1304 /* For a METHOD in a Java class CTYPE, return 1 if
1305 the parameter and return types are valid Java types.
1306 Otherwise, print appropriate error messages, and return 0. */
1309 check_java_method (method)
1310 tree method;
1312 int jerr = 0;
1313 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1314 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1315 if (! acceptable_java_type (ret_type))
1317 cp_error ("Java method '%D' has non-Java return type `%T'",
1318 method, ret_type);
1319 jerr++;
1321 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1323 tree type = TREE_VALUE (arg_types);
1324 if (! acceptable_java_type (type))
1326 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1327 method, type);
1328 jerr++;
1331 return jerr ? 0 : 1;
1334 /* Sanity check: report error if this function FUNCTION is not
1335 really a member of the class (CTYPE) it is supposed to belong to.
1336 CNAME is the same here as it is for grokclassfn above. */
1338 tree
1339 check_classfn (ctype, function)
1340 tree ctype, function;
1342 tree fn_name = DECL_NAME (function);
1343 tree fndecl, fndecls;
1344 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1345 tree *methods = 0;
1346 tree *end = 0;
1348 if (DECL_USE_TEMPLATE (function)
1349 && is_member_template (DECL_TI_TEMPLATE (function)))
1350 /* Since this is a specialization of a member template,
1351 we're not going to find the declaration in the class.
1352 For example, in:
1354 struct S { template <typename T> void f(T); };
1355 template <> void S::f(int);
1357 we're not going to find `S::f(int)', but there's no
1358 reason we should, either. We let our callers know we didn't
1359 find the method, but we don't complain. */
1360 return NULL_TREE;
1362 if (method_vec != 0)
1364 methods = &TREE_VEC_ELT (method_vec, 0);
1365 end = TREE_VEC_END (method_vec);
1367 /* First suss out ctors and dtors. */
1368 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1369 && DECL_CONSTRUCTOR_P (function))
1370 goto got_it;
1371 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1372 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
1373 goto got_it;
1375 while (++methods != end && *methods)
1377 fndecl = *methods;
1378 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1380 got_it:
1381 for (fndecls = *methods; fndecls != NULL_TREE;
1382 fndecls = OVL_NEXT (fndecls))
1384 fndecl = OVL_CURRENT (fndecls);
1385 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL, or
1386 for a for member function of a template class, is
1387 not mangled, so the check below does not work
1388 correctly in that case. Since mangled destructor
1389 names do not include the type of the arguments,
1390 we can't use this short-cut for them, either.
1391 (It's not legal to declare arguments for a
1392 destructor, but some people try.) */
1393 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function))
1394 && (DECL_ASSEMBLER_NAME (function)
1395 != DECL_NAME (function))
1396 && (DECL_ASSEMBLER_NAME (fndecl)
1397 != DECL_NAME (fndecl))
1398 && (DECL_ASSEMBLER_NAME (function)
1399 == DECL_ASSEMBLER_NAME (fndecl)))
1400 return fndecl;
1402 /* We cannot simply call decls_match because this
1403 doesn't work for static member functions that are
1404 pretending to be methods, and because the name
1405 may have been changed by asm("new_name"). */
1406 if (DECL_NAME (function) == DECL_NAME (fndecl))
1408 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1409 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1411 /* Get rid of the this parameter on functions that become
1412 static. */
1413 if (DECL_STATIC_FUNCTION_P (fndecl)
1414 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1415 p1 = TREE_CHAIN (p1);
1417 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1418 TREE_TYPE (TREE_TYPE (fndecl)))
1419 && compparms (p1, p2)
1420 && (DECL_TEMPLATE_SPECIALIZATION (function)
1421 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1422 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1423 || (DECL_TI_TEMPLATE (function)
1424 == DECL_TI_TEMPLATE (fndecl))))
1425 return fndecl;
1428 break; /* loser */
1433 if (methods != end && *methods)
1435 tree fndecl = *methods;
1436 cp_error ("prototype for `%#D' does not match any in class `%T'",
1437 function, ctype);
1438 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1439 OVL_CURRENT (fndecl));
1440 while (fndecl = OVL_NEXT (fndecl), fndecl)
1441 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1443 else
1445 methods = 0;
1446 if (TYPE_SIZE (ctype) == 0)
1447 incomplete_type_error (function, ctype);
1448 else
1449 cp_error ("no `%#D' member function declared in class `%T'",
1450 function, ctype);
1453 /* If we did not find the method in the class, add it to avoid
1454 spurious errors (unless the CTYPE is not yet defined, in which
1455 case we'll only confuse ourselves when the function is declared
1456 properly within the class. */
1457 if (TYPE_SIZE (ctype))
1458 add_method (ctype, methods, function);
1459 return NULL_TREE;
1462 /* We have just processed the DECL, which is a static data member.
1463 Its initializer, if present, is INIT. The ASMSPEC_TREE, if
1464 present, is the assembly-language name for the data member.
1465 FLAGS is as for cp_finish_decl. */
1467 void
1468 finish_static_data_member_decl (decl, init, asmspec_tree, flags)
1469 tree decl;
1470 tree init;
1471 tree asmspec_tree;
1472 int flags;
1474 const char *asmspec = 0;
1476 if (asmspec_tree)
1477 asmspec = TREE_STRING_POINTER (asmspec_tree);
1479 my_friendly_assert (TREE_PUBLIC (decl), 0);
1481 /* We cannot call pushdecl here, because that would fill in the
1482 decl of our TREE_CHAIN. Instead, we modify cp_finish_decl to do
1483 the right thing, namely, to put this decl out straight away. */
1484 /* current_class_type can be NULL_TREE in case of error. */
1485 if (!asmspec && current_class_type)
1487 DECL_INITIAL (decl) = error_mark_node;
1488 DECL_ASSEMBLER_NAME (decl)
1489 = build_static_name (current_class_type, DECL_NAME (decl));
1491 if (! processing_template_decl)
1493 if (!pending_statics)
1494 VARRAY_TREE_INIT (pending_statics, 32, "pending_statics");
1495 VARRAY_PUSH_TREE (pending_statics, decl);
1498 /* Static consts need not be initialized in the class definition. */
1499 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
1501 static int explanation = 0;
1503 error ("initializer invalid for static member with constructor");
1504 if (explanation++ == 0)
1505 error ("(you really want to initialize it separately)");
1506 init = 0;
1508 /* Force the compiler to know when an uninitialized static const
1509 member is being used. */
1510 if (CP_TYPE_CONST_P (TREE_TYPE (decl)) && init == 0)
1511 TREE_USED (decl) = 1;
1512 DECL_INITIAL (decl) = init;
1513 DECL_IN_AGGR_P (decl) = 1;
1514 DECL_CONTEXT (decl) = current_class_type;
1515 DECL_CLASS_CONTEXT (decl) = current_class_type;
1517 cp_finish_decl (decl, init, asmspec_tree, flags);
1520 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1521 of a structure component, returning a FIELD_DECL node.
1522 QUALS is a list of type qualifiers for this decl (such as for declaring
1523 const member functions).
1525 This is done during the parsing of the struct declaration.
1526 The FIELD_DECL nodes are chained together and the lot of them
1527 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1529 C++:
1531 If class A defines that certain functions in class B are friends, then
1532 the way I have set things up, it is B who is interested in permission
1533 granted by A. However, it is in A's context that these declarations
1534 are parsed. By returning a void_type_node, class A does not attempt
1535 to incorporate the declarations of the friends within its structure.
1537 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1538 CHANGES TO CODE IN `start_method'. */
1540 tree
1541 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1542 tree declarator, declspecs, init, asmspec_tree, attrlist;
1544 register tree value;
1545 const char *asmspec = 0;
1546 int flags = LOOKUP_ONLYCONVERTING;
1548 /* Convert () initializers to = initializers. */
1549 if (init == NULL_TREE && declarator != NULL_TREE
1550 && TREE_CODE (declarator) == CALL_EXPR
1551 && TREE_OPERAND (declarator, 0)
1552 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1553 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1554 && parmlist_is_exprlist (CALL_DECLARATOR_PARMS (declarator)))
1556 init = TREE_OPERAND (declarator, 1);
1557 declarator = TREE_OPERAND (declarator, 0);
1558 flags = 0;
1561 if (declspecs == NULL_TREE
1562 && TREE_CODE (declarator) == SCOPE_REF
1563 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1565 /* Access declaration */
1566 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1568 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1569 pop_nested_class ();
1570 return do_class_using_decl (declarator);
1573 if (init
1574 && TREE_CODE (init) == TREE_LIST
1575 && TREE_VALUE (init) == error_mark_node
1576 && TREE_CHAIN (init) == NULL_TREE)
1577 init = NULL_TREE;
1579 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, attrlist);
1580 if (! value || value == error_mark_node)
1581 /* friend or constructor went bad. */
1582 return value;
1584 /* Pass friendly classes back. */
1585 if (TREE_CODE (value) == VOID_TYPE)
1586 return void_type_node;
1588 if (DECL_NAME (value) != NULL_TREE
1589 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1590 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1591 cp_error ("member `%D' conflicts with virtual function table field name",
1592 value);
1594 /* Stash away type declarations. */
1595 if (TREE_CODE (value) == TYPE_DECL)
1597 DECL_NONLOCAL (value) = 1;
1598 DECL_CONTEXT (value) = current_class_type;
1599 DECL_CLASS_CONTEXT (value) = current_class_type;
1601 /* Now that we've updated the context, we need to remangle the
1602 name for this TYPE_DECL. */
1603 DECL_ASSEMBLER_NAME (value) = DECL_NAME (value);
1604 if (!uses_template_parms (value))
1605 DECL_ASSEMBLER_NAME (value) =
1606 get_identifier (build_overload_name (TREE_TYPE (value), 1, 1));
1608 if (processing_template_decl)
1609 value = push_template_decl (value);
1611 return value;
1614 if (DECL_IN_AGGR_P (value))
1616 cp_error ("`%D' is already defined in `%T'", value,
1617 DECL_CONTEXT (value));
1618 return void_type_node;
1621 if (asmspec_tree)
1622 asmspec = TREE_STRING_POINTER (asmspec_tree);
1624 if (init)
1626 if (TREE_CODE (value) == FUNCTION_DECL)
1628 grok_function_init (value, init);
1629 init = NULL_TREE;
1631 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1632 /* Already complained in grokdeclarator. */
1633 init = NULL_TREE;
1634 else
1636 /* We allow initializers to become parameters to base
1637 initializers. */
1638 if (TREE_CODE (init) == TREE_LIST)
1640 if (TREE_CHAIN (init) == NULL_TREE)
1641 init = TREE_VALUE (init);
1642 else
1643 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1646 if (TREE_CODE (init) == CONST_DECL)
1647 init = DECL_INITIAL (init);
1648 else if (TREE_READONLY_DECL_P (init))
1649 init = decl_constant_value (init);
1650 else if (TREE_CODE (init) == CONSTRUCTOR)
1651 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1652 if (init == error_mark_node)
1653 /* We must make this look different than `error_mark_node'
1654 because `decl_const_value' would mis-interpret it
1655 as only meaning that this VAR_DECL is defined. */
1656 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1657 else if (processing_template_decl)
1659 else if (! TREE_CONSTANT (init))
1661 /* We can allow references to things that are effectively
1662 static, since references are initialized with the address. */
1663 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1664 || (TREE_STATIC (init) == 0
1665 && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
1666 || DECL_EXTERNAL (init) == 0)))
1668 error ("field initializer is not constant");
1669 init = error_mark_node;
1675 if (processing_template_decl && ! current_function_decl
1676 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1677 value = push_template_decl (value);
1679 if (attrlist)
1680 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1681 TREE_VALUE (attrlist));
1683 if (TREE_CODE (value) == VAR_DECL)
1685 finish_static_data_member_decl (value, init, asmspec_tree,
1686 flags);
1687 return value;
1689 if (TREE_CODE (value) == FIELD_DECL)
1691 if (asmspec)
1693 /* This must override the asm specifier which was placed
1694 by grokclassfn. Lay this out fresh. */
1695 DECL_RTL (value) = NULL_RTX;
1696 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1698 if (DECL_INITIAL (value) == error_mark_node)
1699 init = error_mark_node;
1700 cp_finish_decl (value, init, asmspec_tree, flags);
1701 DECL_INITIAL (value) = init;
1702 DECL_IN_AGGR_P (value) = 1;
1703 return value;
1705 if (TREE_CODE (value) == FUNCTION_DECL)
1707 if (asmspec)
1709 /* This must override the asm specifier which was placed
1710 by grokclassfn. Lay this out fresh. */
1711 DECL_RTL (value) = NULL_RTX;
1712 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1714 cp_finish_decl (value, init, asmspec_tree, flags);
1716 /* Pass friends back this way. */
1717 if (DECL_FRIEND_P (value))
1718 return void_type_node;
1720 DECL_IN_AGGR_P (value) = 1;
1721 return value;
1723 my_friendly_abort (21);
1724 /* NOTREACHED */
1725 return NULL_TREE;
1728 /* Like `grokfield', but for bitfields.
1729 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1731 tree
1732 grokbitfield (declarator, declspecs, width)
1733 tree declarator, declspecs, width;
1735 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1736 0, NULL_TREE);
1738 if (! value) return NULL_TREE; /* friends went bad. */
1740 /* Pass friendly classes back. */
1741 if (TREE_CODE (value) == VOID_TYPE)
1742 return void_type_node;
1744 if (TREE_CODE (value) == TYPE_DECL)
1746 cp_error ("cannot declare `%D' to be a bitfield type", value);
1747 return NULL_TREE;
1750 /* Usually, finish_struct_1 catches bitifields with invalid types.
1751 But, in the case of bitfields with function type, we confuse
1752 ourselves into thinking they are member functions, so we must
1753 check here. */
1754 if (TREE_CODE (value) == FUNCTION_DECL)
1756 cp_error ("cannot declare bitfield `%D' with funcion type",
1757 DECL_NAME (value));
1758 return NULL_TREE;
1761 if (DECL_IN_AGGR_P (value))
1763 cp_error ("`%D' is already defined in the class %T", value,
1764 DECL_CONTEXT (value));
1765 return void_type_node;
1768 GNU_xref_member (current_class_name, value);
1770 if (TREE_STATIC (value))
1772 cp_error ("static member `%D' cannot be a bitfield", value);
1773 return NULL_TREE;
1775 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0);
1777 if (width != error_mark_node)
1779 constant_expression_warning (width);
1780 DECL_INITIAL (value) = width;
1781 SET_DECL_C_BIT_FIELD (value);
1784 DECL_IN_AGGR_P (value) = 1;
1785 return value;
1788 tree
1789 grokoptypename (declspecs, declarator)
1790 tree declspecs, declarator;
1792 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1793 return build_typename_overload (t);
1796 /* When a function is declared with an initializer,
1797 do the right thing. Currently, there are two possibilities:
1799 class B
1801 public:
1802 // initialization possibility #1.
1803 virtual void f () = 0;
1804 int g ();
1807 class D1 : B
1809 public:
1810 int d1;
1811 // error, no f ();
1814 class D2 : B
1816 public:
1817 int d2;
1818 void f ();
1821 class D3 : B
1823 public:
1824 int d3;
1825 // initialization possibility #2
1826 void f () = B::f;
1832 copy_assignment_arg_p (parmtype, virtualp)
1833 tree parmtype;
1834 int virtualp ATTRIBUTE_UNUSED;
1836 if (current_class_type == NULL_TREE)
1837 return 0;
1839 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1840 parmtype = TREE_TYPE (parmtype);
1842 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1843 #if 0
1844 /* Non-standard hack to support old Booch components. */
1845 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1846 #endif
1848 return 1;
1850 return 0;
1853 static void
1854 grok_function_init (decl, init)
1855 tree decl;
1856 tree init;
1858 /* An initializer for a function tells how this function should
1859 be inherited. */
1860 tree type = TREE_TYPE (decl);
1862 if (TREE_CODE (type) == FUNCTION_TYPE)
1863 cp_error ("initializer specified for non-member function `%D'", decl);
1864 #if 0
1865 /* We'll check for this in finish_struct_1. */
1866 else if (DECL_VINDEX (decl) == NULL_TREE)
1867 cp_error ("initializer specified for non-virtual method `%D'", decl);
1868 #endif
1869 else if (integer_zerop (init))
1871 #if 0
1872 /* Mark this function as being "defined". */
1873 DECL_INITIAL (decl) = error_mark_node;
1874 /* pure virtual destructors must be defined. */
1875 /* pure virtual needs to be defined (as abort) only when put in
1876 vtbl. For wellformed call, it should be itself. pr4737 */
1877 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1879 /* Give this node rtl from `abort'. */
1880 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
1882 #endif
1883 DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
1884 if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
1886 tree parmtype
1887 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1889 if (copy_assignment_arg_p (parmtype, 1))
1890 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1893 else
1894 cp_error ("invalid initializer for virtual method `%D'", decl);
1897 void
1898 cplus_decl_attributes (decl, attributes, prefix_attributes)
1899 tree decl, attributes, prefix_attributes;
1901 if (decl == NULL_TREE || decl == void_type_node)
1902 return;
1904 if (TREE_CODE (decl) == TEMPLATE_DECL)
1905 decl = DECL_TEMPLATE_RESULT (decl);
1907 decl_attributes (decl, attributes, prefix_attributes);
1909 if (TREE_CODE (decl) == TYPE_DECL)
1910 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
1913 /* CONSTRUCTOR_NAME:
1914 Return the name for the constructor (or destructor) for the
1915 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1916 IDENTIFIER_NODE. When given a template, this routine doesn't
1917 lose the specialization. */
1919 tree
1920 constructor_name_full (thing)
1921 tree thing;
1923 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1924 || TREE_CODE (thing) == TEMPLATE_TEMPLATE_PARM
1925 || TREE_CODE (thing) == TYPENAME_TYPE)
1926 thing = TYPE_NAME (thing);
1927 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1929 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1930 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1931 else
1932 thing = TYPE_NAME (thing);
1934 if (TREE_CODE (thing) == TYPE_DECL
1935 || (TREE_CODE (thing) == TEMPLATE_DECL
1936 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1937 thing = DECL_NAME (thing);
1938 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1939 return thing;
1942 /* CONSTRUCTOR_NAME:
1943 Return the name for the constructor (or destructor) for the
1944 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1945 IDENTIFIER_NODE. When given a template, return the plain
1946 unspecialized name. */
1948 tree
1949 constructor_name (thing)
1950 tree thing;
1952 tree t;
1953 thing = constructor_name_full (thing);
1954 t = IDENTIFIER_TEMPLATE (thing);
1955 if (!t)
1956 return thing;
1957 return t;
1960 /* Record the existence of an addressable inline function. */
1962 void
1963 mark_inline_for_output (decl)
1964 tree decl;
1966 decl = DECL_MAIN_VARIANT (decl);
1967 if (DECL_SAVED_INLINE (decl))
1968 return;
1969 DECL_SAVED_INLINE (decl) = 1;
1970 if (!saved_inlines)
1971 VARRAY_TREE_INIT (saved_inlines, 32, "saved_inlines");
1973 VARRAY_PUSH_TREE (saved_inlines, decl);
1976 /* Hand off a unique name which can be used for variable we don't really
1977 want to know about anyway, for example, the anonymous variables which
1978 are needed to make references work. Declare this thing so we can use it.
1979 The variable created will be of type TYPE.
1981 STATICP is nonzero if this variable should be static. */
1983 tree
1984 get_temp_name (type, staticp)
1985 tree type;
1986 int staticp;
1988 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
1989 tree decl;
1990 int toplev = toplevel_bindings_p ();
1992 if (toplev || staticp)
1994 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
1995 decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
1997 else
1999 sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
2000 decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
2002 TREE_USED (decl) = 1;
2003 TREE_STATIC (decl) = staticp;
2004 DECL_ARTIFICIAL (decl) = 1;
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 one that
2082 can hold them all. */
2083 if (main_decl == NULL_TREE
2084 && simple_cst_equal (DECL_SIZE (decl),
2085 DECL_SIZE (anon_decl)) == 1)
2086 main_decl = decl;
2087 else
2088 /* ??? This causes there to be no debug info written out
2089 about this decl. */
2090 TREE_ASM_WRITTEN (decl) = 1;
2092 if (DECL_NAME (field) == NULL_TREE
2093 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2094 /* The remainder of the processing was already done in the
2095 recursive call. */
2096 continue;
2098 /* If there's a cleanup to do, it belongs in the
2099 TREE_PURPOSE of the following TREE_LIST. */
2100 *elems = tree_cons (NULL_TREE, decl, *elems);
2101 TREE_TYPE (*elems) = type;
2104 return main_decl;
2107 /* Finish off the processing of a UNION_TYPE structure.
2108 If there are static members, then all members are
2109 static, and must be laid out together. If the
2110 union is an anonymous union, we arrange for that
2111 as well. PUBLIC_P is nonzero if this union is
2112 not declared static. */
2114 void
2115 finish_anon_union (anon_union_decl)
2116 tree anon_union_decl;
2118 tree type = TREE_TYPE (anon_union_decl);
2119 tree main_decl;
2120 int public_p = TREE_PUBLIC (anon_union_decl);
2121 int static_p = TREE_STATIC (anon_union_decl);
2122 int external_p = DECL_EXTERNAL (anon_union_decl);
2124 if (TYPE_FIELDS (type) == NULL_TREE)
2125 return;
2127 if (public_p)
2129 error ("global anonymous unions must be declared static");
2130 return;
2133 main_decl = build_anon_union_vars (anon_union_decl,
2134 &DECL_ANON_UNION_ELEMS (anon_union_decl),
2135 static_p, external_p);
2137 if (main_decl == NULL_TREE)
2139 warning ("anonymous union with no members");
2140 return;
2143 if (static_p)
2145 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2146 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2147 expand_anon_union_decl (anon_union_decl,
2148 NULL_TREE,
2149 DECL_ANON_UNION_ELEMS (anon_union_decl));
2151 else
2152 add_decl_stmt (anon_union_decl);
2155 /* Finish processing a builtin type TYPE. It's name is NAME,
2156 its fields are in the array FIELDS. LEN is the number of elements
2157 in FIELDS minus one, or put another way, it is the maximum subscript
2158 used in FIELDS.
2160 It is given the same alignment as ALIGN_TYPE. */
2162 void
2163 finish_builtin_type (type, name, fields, len, align_type)
2164 tree type;
2165 const char *name;
2166 tree fields[];
2167 int len;
2168 tree align_type;
2170 register int i;
2172 TYPE_FIELDS (type) = fields[0];
2173 for (i = 0; i < len; i++)
2175 layout_type (TREE_TYPE (fields[i]));
2176 DECL_FIELD_CONTEXT (fields[i]) = type;
2177 TREE_CHAIN (fields[i]) = fields[i+1];
2179 DECL_FIELD_CONTEXT (fields[i]) = type;
2180 DECL_CLASS_CONTEXT (fields[i]) = type;
2181 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2182 layout_type (type);
2183 #if 0 /* not yet, should get fixed properly later */
2184 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2185 #else
2186 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2187 #endif
2188 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2189 layout_decl (TYPE_NAME (type), 0);
2192 /* Auxiliary functions to make type signatures for
2193 `operator new' and `operator delete' correspond to
2194 what compiler will be expecting. */
2196 tree
2197 coerce_new_type (type)
2198 tree type;
2200 int e1 = 0, e2 = 0;
2202 if (TREE_CODE (type) == METHOD_TYPE)
2203 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2204 if (! same_type_p (TREE_TYPE (type), ptr_type_node))
2205 e1 = 1, error ("`operator new' must return type `void *'");
2207 /* Technically the type must be `size_t', but we may not know
2208 what that is. */
2209 if (TYPE_ARG_TYPES (type) == NULL_TREE)
2210 e1 = 1, error ("`operator new' takes type `size_t' parameter");
2211 else if (! same_type_p (TREE_VALUE (TYPE_ARG_TYPES (type)), sizetype))
2212 e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2213 if (e2)
2214 type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2215 else if (e1)
2216 type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2217 return type;
2220 tree
2221 coerce_delete_type (type)
2222 tree type;
2224 int e1 = 0, e2 = 0;
2225 #if 0
2226 e3 = 0;
2227 #endif
2228 tree arg_types = TYPE_ARG_TYPES (type);
2230 if (TREE_CODE (type) == METHOD_TYPE)
2232 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2233 arg_types = TREE_CHAIN (arg_types);
2236 if (TREE_TYPE (type) != void_type_node)
2237 e1 = 1, error ("`operator delete' must return type `void'");
2239 if (arg_types == NULL_TREE
2240 || ! same_type_p (TREE_VALUE (arg_types), ptr_type_node))
2241 e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2243 #if 0
2244 if (arg_types
2245 && TREE_CHAIN (arg_types)
2246 && TREE_CHAIN (arg_types) != void_list_node)
2248 /* Again, technically this argument must be `size_t', but again
2249 we may not know what that is. */
2250 tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2251 if (! same_type_p (t2, sizetype))
2252 e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2253 else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2255 e3 = 1;
2256 if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2257 error ("too many arguments in declaration of `operator delete'");
2258 else
2259 error ("`...' invalid in specification of `operator delete'");
2263 if (e3)
2264 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2265 build_tree_list (NULL_TREE, sizetype));
2266 else if (e3 |= e2)
2268 if (arg_types == NULL_TREE)
2269 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2270 else
2271 arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2273 else e3 |= e1;
2274 #endif
2276 if (e2)
2277 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2278 arg_types ? TREE_CHAIN (arg_types): NULL_TREE);
2279 if (e2 || e1)
2280 type = build_function_type (void_type_node, arg_types);
2282 return type;
2285 static void
2286 mark_vtable_entries (decl)
2287 tree decl;
2289 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2291 for (; entries; entries = TREE_CHAIN (entries))
2293 tree fnaddr;
2294 tree fn;
2296 fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2297 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2299 if (TREE_CODE (fnaddr) == NOP_EXPR)
2300 /* RTTI offset. */
2301 continue;
2303 fn = TREE_OPERAND (fnaddr, 0);
2304 TREE_ADDRESSABLE (fn) = 1;
2305 if (TREE_CODE (fn) == THUNK_DECL && DECL_EXTERNAL (fn))
2307 DECL_EXTERNAL (fn) = 0;
2308 emit_thunk (fn);
2310 mark_used (fn);
2314 /* Set DECL up to have the closest approximation of "initialized common"
2315 linkage available. */
2317 void
2318 comdat_linkage (decl)
2319 tree decl;
2321 if (flag_weak)
2322 make_decl_one_only (decl);
2323 else if (TREE_CODE (decl) == FUNCTION_DECL || DECL_VIRTUAL_P (decl))
2324 /* We can just emit functions and vtables statically; it doesn't really
2325 matter if we have multiple copies. */
2326 TREE_PUBLIC (decl) = 0;
2327 else
2329 /* Static data member template instantiations, however, cannot
2330 have multiple copies. */
2331 if (DECL_INITIAL (decl) == 0
2332 || DECL_INITIAL (decl) == error_mark_node)
2333 DECL_COMMON (decl) = 1;
2334 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2336 DECL_COMMON (decl) = 1;
2337 DECL_INITIAL (decl) = error_mark_node;
2339 else
2341 /* We can't do anything useful; leave vars for explicit
2342 instantiation. */
2343 DECL_EXTERNAL (decl) = 1;
2344 DECL_NOT_REALLY_EXTERN (decl) = 0;
2348 if (DECL_LANG_SPECIFIC (decl))
2349 DECL_COMDAT (decl) = 1;
2351 if (TREE_CODE (decl) == FUNCTION_DECL)
2352 DECL_DEFER_OUTPUT (decl) = 1;
2355 /* For win32 we also want to put explicit instantiations in
2356 linkonce sections, so that they will be merged with implicit
2357 instantiations; otherwise we get duplicate symbol errors. */
2359 void
2360 maybe_make_one_only (decl)
2361 tree decl;
2363 /* We used to say that this was not necessary on targets that support weak
2364 symbols, because the implicit instantiations will defer to the explicit
2365 one. However, that's not actually the case in SVR4; a strong definition
2366 after a weak one is an error. Also, not making explicit
2367 instantiations one_only means that we can end up with two copies of
2368 some template instantiations. */
2369 if (! supports_one_only ())
2370 return;
2372 /* We can't set DECL_COMDAT on functions, or finish_file will think
2373 we can get away with not emitting them if they aren't used. We need
2374 to for variables so that cp_finish_decl will update their linkage,
2375 because their DECL_INITIAL may not have been set properly yet. */
2377 make_decl_one_only (decl);
2379 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2380 DECL_COMDAT (decl) = 1;
2383 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2384 based on TYPE and other static flags.
2386 Note that anything public is tagged TREE_PUBLIC, whether
2387 it's public in this file or in another one. */
2389 void
2390 import_export_vtable (decl, type, final)
2391 tree decl, type;
2392 int final;
2394 if (DECL_INTERFACE_KNOWN (decl))
2395 return;
2397 if (TYPE_FOR_JAVA (type))
2399 TREE_PUBLIC (decl) = 1;
2400 DECL_EXTERNAL (decl) = 1;
2401 DECL_INTERFACE_KNOWN (decl) = 1;
2403 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2405 TREE_PUBLIC (decl) = 1;
2406 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2407 DECL_INTERFACE_KNOWN (decl) = 1;
2409 else
2411 /* We can only wait to decide if we have real non-inline virtual
2412 functions in our class, or if we come from a template. */
2414 int found = CLASSTYPE_TEMPLATE_INSTANTIATION (type);
2416 if (! found && ! final)
2418 tree method;
2419 for (method = TYPE_METHODS (type); method != NULL_TREE;
2420 method = TREE_CHAIN (method))
2421 if (DECL_VINDEX (method) != NULL_TREE
2422 && ! DECL_THIS_INLINE (method)
2423 && ! DECL_ABSTRACT_VIRTUAL_P (method))
2425 found = 1;
2426 break;
2430 if (final || ! found)
2432 comdat_linkage (decl);
2433 DECL_EXTERNAL (decl) = 0;
2435 else
2437 TREE_PUBLIC (decl) = 1;
2438 DECL_EXTERNAL (decl) = 1;
2443 /* Determine whether or not we want to specifically import or export CTYPE,
2444 using various heuristics. */
2446 void
2447 import_export_class (ctype)
2448 tree ctype;
2450 /* -1 for imported, 1 for exported. */
2451 int import_export = 0;
2453 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2454 return;
2456 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2457 we will have CLASSTYPE_INTERFACE_ONLY set but not
2458 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2459 heuristic because someone will supply a #pragma implementation
2460 elsewhere, and deducing it here would produce a conflict. */
2461 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2462 return;
2464 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2465 /* FIXME this should really use some sort of target-independent macro. */
2466 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2467 /* Use -2 so we survive the MULTIPLE_SYMBOL_SPACES check below. */
2468 import_export = -2;
2469 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2470 import_export = 2;
2471 #endif
2473 /* If we got -fno-implicit-templates, we import template classes that
2474 weren't explicitly instantiated. */
2475 if (import_export == 0
2476 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2477 && ! flag_implicit_templates)
2478 import_export = -1;
2480 /* Base our import/export status on that of the first non-inline,
2481 non-abstract virtual function, if any. */
2482 if (import_export == 0
2483 && TYPE_POLYMORPHIC_P (ctype)
2484 && ! CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2486 tree method;
2487 for (method = TYPE_METHODS (ctype); method != NULL_TREE;
2488 method = TREE_CHAIN (method))
2490 if (DECL_VINDEX (method) != NULL_TREE
2491 && !DECL_THIS_INLINE (method)
2492 && !DECL_ABSTRACT_VIRTUAL_P (method))
2494 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2495 break;
2500 #ifdef MULTIPLE_SYMBOL_SPACES
2501 if (import_export == -1)
2502 import_export = 0;
2503 #endif
2505 if (import_export)
2507 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2508 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = (import_export > 0);
2509 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2513 /* We need to describe to the assembler the relationship between
2514 a vtable and the vtable of the parent class. */
2516 static void
2517 output_vtable_inherit (vars)
2518 tree vars;
2520 tree parent;
2521 rtx op[2];
2523 op[0] = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2525 parent = binfo_for_vtable (vars);
2527 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2528 op[1] = const0_rtx;
2529 else if (parent)
2531 parent = TYPE_BINFO_VTABLE (BINFO_TYPE (parent));
2532 op[1] = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2534 else
2535 my_friendly_abort (980826);
2537 output_asm_insn (".vtable_inherit %c0, %c1", op);
2540 static int
2541 finish_vtable_vardecl (t, data)
2542 tree *t;
2543 void *data ATTRIBUTE_UNUSED;
2545 tree vars = *t;
2546 tree ctype = DECL_CONTEXT (vars);
2547 import_export_class (ctype);
2548 import_export_vtable (vars, ctype, 1);
2550 if (! DECL_EXTERNAL (vars)
2551 && (DECL_INTERFACE_KNOWN (vars)
2552 || DECL_NEEDED_P (vars)
2553 || (hack_decl_function_context (vars) && TREE_USED (vars)))
2554 && ! TREE_ASM_WRITTEN (vars))
2556 /* Write it out. */
2557 mark_vtable_entries (vars);
2558 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2559 store_init_value (vars, DECL_INITIAL (vars));
2561 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2563 /* Mark the VAR_DECL node representing the vtable itself as a
2564 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2565 It is rather important that such things be ignored because
2566 any effort to actually generate DWARF for them will run
2567 into trouble when/if we encounter code like:
2569 #pragma interface
2570 struct S { virtual void member (); };
2572 because the artificial declaration of the vtable itself (as
2573 manufactured by the g++ front end) will say that the vtable
2574 is a static member of `S' but only *after* the debug output
2575 for the definition of `S' has already been output. This causes
2576 grief because the DWARF entry for the definition of the vtable
2577 will try to refer back to an earlier *declaration* of the
2578 vtable as a static member of `S' and there won't be one.
2579 We might be able to arrange to have the "vtable static member"
2580 attached to the member list for `S' before the debug info for
2581 `S' get written (which would solve the problem) but that would
2582 require more intrusive changes to the g++ front end. */
2584 DECL_IGNORED_P (vars) = 1;
2587 /* Always make vtables weak. */
2588 if (flag_weak)
2589 comdat_linkage (vars);
2591 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2593 if (flag_vtable_gc)
2594 output_vtable_inherit (vars);
2596 /* Because we're only doing syntax-checking, we'll never end up
2597 actually marking the variable as written. */
2598 if (flag_syntax_only)
2599 TREE_ASM_WRITTEN (vars) = 1;
2601 /* Since we're writing out the vtable here, also write the debug
2602 info. */
2603 if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (ctype)))
2605 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (ctype)) = 0;
2606 rest_of_type_compilation (ctype, toplevel_bindings_p ());
2609 return 1;
2611 else if (!DECL_NEEDED_P (vars))
2612 /* We don't know what to do with this one yet. */
2613 return 0;
2615 return 0;
2618 static int
2619 prune_vtable_vardecl (t, data)
2620 tree *t;
2621 void *data ATTRIBUTE_UNUSED;
2623 *t = TREE_CHAIN (*t);
2624 return 1;
2627 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2628 inline function or template instantiation at end-of-file. */
2630 void
2631 import_export_decl (decl)
2632 tree decl;
2634 if (DECL_INTERFACE_KNOWN (decl))
2635 return;
2637 if (DECL_TEMPLATE_INSTANTIATION (decl)
2638 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2640 DECL_NOT_REALLY_EXTERN (decl) = 1;
2641 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2642 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2643 && (flag_implicit_templates
2644 || (flag_implicit_inline_templates && DECL_THIS_INLINE (decl))))
2646 if (!TREE_PUBLIC (decl))
2647 /* Templates are allowed to have internal linkage. See
2648 [basic.link]. */
2650 else
2651 comdat_linkage (decl);
2653 else
2654 DECL_NOT_REALLY_EXTERN (decl) = 0;
2656 else if (DECL_FUNCTION_MEMBER_P (decl))
2658 tree ctype = DECL_CLASS_CONTEXT (decl);
2659 import_export_class (ctype);
2660 if (CLASSTYPE_INTERFACE_KNOWN (ctype)
2661 && (flag_new_abi
2662 ? (! DECL_THIS_INLINE (decl))
2663 : (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))))
2665 DECL_NOT_REALLY_EXTERN (decl)
2666 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2667 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2668 && !DECL_VINDEX (decl)));
2670 /* Always make artificials weak. */
2671 if (DECL_ARTIFICIAL (decl) && flag_weak)
2672 comdat_linkage (decl);
2673 else
2674 maybe_make_one_only (decl);
2676 else
2677 comdat_linkage (decl);
2679 else if (DECL_TINFO_FN_P (decl))
2681 tree ctype = TREE_TYPE (DECL_NAME (decl));
2683 if (IS_AGGR_TYPE (ctype))
2684 import_export_class (ctype);
2686 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2687 && TYPE_POLYMORPHIC_P (ctype)
2688 /* If -fno-rtti, we're not necessarily emitting this stuff with
2689 the class, so go ahead and emit it now. This can happen
2690 when a class is used in exception handling. */
2691 && flag_rtti
2692 /* If the type is a cv-qualified variant of a type, then we
2693 must emit the tinfo function in this translation unit
2694 since it will not be emitted when the vtable for the type
2695 is output (which is when the unqualified version is
2696 generated). */
2697 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2699 DECL_NOT_REALLY_EXTERN (decl)
2700 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2701 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines
2702 && !DECL_VINDEX (decl)));
2704 /* Always make artificials weak. */
2705 if (flag_weak)
2706 comdat_linkage (decl);
2708 else if (TYPE_BUILT_IN (ctype)
2709 && same_type_p (ctype, TYPE_MAIN_VARIANT (ctype)))
2710 DECL_NOT_REALLY_EXTERN (decl) = 0;
2711 else
2712 comdat_linkage (decl);
2714 else
2715 comdat_linkage (decl);
2717 DECL_INTERFACE_KNOWN (decl) = 1;
2720 tree
2721 build_cleanup (decl)
2722 tree decl;
2724 tree temp;
2725 tree type = TREE_TYPE (decl);
2727 if (TREE_CODE (type) == ARRAY_TYPE)
2728 temp = decl;
2729 else
2731 mark_addressable (decl);
2732 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2734 temp = build_delete (TREE_TYPE (temp), temp,
2735 integer_two_node,
2736 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2737 return temp;
2740 extern int parse_time, varconst_time;
2742 static tree
2743 get_sentry (base)
2744 tree base;
2746 tree sname = get_id_2 ("__sn", base);
2747 /* For struct X foo __attribute__((weak)), there is a counter
2748 __snfoo. Since base is already an assembler name, sname should
2749 be globally unique */
2750 tree sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2751 if (! sentry)
2753 sentry = build_decl (VAR_DECL, sname, integer_type_node);
2754 TREE_PUBLIC (sentry) = 1;
2755 DECL_ARTIFICIAL (sentry) = 1;
2756 TREE_STATIC (sentry) = 1;
2757 TREE_USED (sentry) = 1;
2758 DECL_COMMON (sentry) = 1;
2759 pushdecl_top_level (sentry);
2760 cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0);
2762 return sentry;
2765 /* Start the process of running a particular set of global constructors
2766 or destructors. Subroutine of do_[cd]tors. */
2768 static tree
2769 start_objects (method_type, initp)
2770 int method_type, initp;
2772 tree fnname;
2773 tree body;
2774 char type[10];
2776 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2778 if (initp != DEFAULT_INIT_PRIORITY)
2780 char joiner;
2782 #ifdef JOINER
2783 joiner = JOINER;
2784 #else
2785 joiner = '_';
2786 #endif
2788 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2790 else
2791 sprintf (type, "%c", method_type);
2793 fnname = get_file_function_name_long (type);
2795 start_function (void_list_node,
2796 make_call_declarator (fnname, void_list_node, NULL_TREE,
2797 NULL_TREE),
2798 NULL_TREE, SF_DEFAULT);
2800 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
2801 /* It can be a static function as long as collect2 does not have
2802 to scan the object file to find its ctor/dtor routine. */
2803 TREE_PUBLIC (current_function_decl) = 0;
2804 #endif
2806 /* Mark this declaration as used to avoid spurious warnings. */
2807 TREE_USED (current_function_decl) = 1;
2809 /* Mark this function as a global constructor or destructor. */
2810 if (method_type == 'I')
2811 DECL_GLOBAL_CTOR_P (current_function_decl) = 1;
2812 else
2813 DECL_GLOBAL_DTOR_P (current_function_decl) = 1;
2814 GLOBAL_INIT_PRIORITY (current_function_decl) = initp;
2816 body = begin_compound_stmt (/*has_no_scope=*/0);
2818 /* We cannot allow these functions to be elided, even if they do not
2819 have external linkage. And, there's no point in deferring
2820 copmilation of thes functions; they're all going to have to be
2821 out anyhow. */
2822 current_function_cannot_inline
2823 = "static constructors and destructors cannot be inlined";
2825 return body;
2828 /* Finish the process of running a particular set of global constructors
2829 or destructors. Subroutine of do_[cd]tors. */
2831 static void
2832 finish_objects (method_type, initp, body)
2833 int method_type, initp;
2834 tree body;
2836 char *fnname;
2837 tree fn;
2839 /* Finish up. */
2840 finish_compound_stmt(/*has_no_scope=*/0, body);
2841 fn = finish_function (lineno, 0);
2842 expand_body (fn);
2844 /* When only doing semantic analysis, and no RTL generation, we
2845 can't call functions that directly emit assembly code; there is
2846 no assembly file in which to put the code. */
2847 if (flag_syntax_only)
2848 return;
2850 fnname = XSTR (XEXP (DECL_RTL (fn), 0), 0);
2851 if (initp == DEFAULT_INIT_PRIORITY)
2853 if (method_type == 'I')
2854 assemble_constructor (fnname);
2855 else
2856 assemble_destructor (fnname);
2858 #if defined (ASM_OUTPUT_SECTION_NAME) && defined (ASM_OUTPUT_CONSTRUCTOR)
2859 /* If we're using init priority we can't use assemble_*tor, but on ELF
2860 targets we can stick the references into named sections for GNU ld
2861 to collect. */
2862 else
2864 char buf[15];
2865 sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
2866 /* invert the numbering so the linker puts us in the proper
2867 order; constructors are run from right to left, and the
2868 linker sorts in increasing order. */
2869 MAX_INIT_PRIORITY - initp);
2870 named_section (NULL_TREE, buf, 0);
2871 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
2872 POINTER_SIZE / BITS_PER_UNIT, 1);
2874 #endif
2877 /* The names of the parameters to the function created to handle
2878 initializations and destructions for objects with static storage
2879 duration. */
2880 #define INITIALIZE_P_IDENTIFIER "__initialize_p"
2881 #define PRIORITY_IDENTIFIER "__priority"
2883 /* The name of the function we create to handle initializations and
2884 destructions for objects with static storage duration. */
2885 #define SSDF_IDENTIFIER "__static_initialization_and_destruction"
2887 /* The declaration for the __INITIALIZE_P argument. */
2888 static tree initialize_p_decl;
2890 /* The declaration for the __PRIORITY argument. */
2891 static tree priority_decl;
2893 /* The declaration for the static storage duration function. */
2894 static tree ssdf_decl;
2896 /* All the static storage duration functions created in this
2897 translation unit. */
2898 static varray_type ssdf_decls;
2900 /* A map from priority levels to information about that priority
2901 level. There may be many such levels, so efficient lookup is
2902 important. */
2903 static splay_tree priority_info_map;
2905 /* Begins the generation of the function that will handle all
2906 initialization and destruction of objects with static storage
2907 duration. The function generated takes two parameters of type
2908 `int': __INITIALIZE_P and __PRIORITY. If __INITIALIZE_P is
2909 non-zero, it performs initializations. Otherwise, it performs
2910 destructions. It only performs those initializations or
2911 destructions with the indicated __PRIORITY. The generated function
2912 returns no value.
2914 It is assumed that this function will only be called once per
2915 translation unit. */
2917 static tree
2918 start_static_storage_duration_function ()
2920 static unsigned ssdf_number;
2922 tree parm_types;
2923 tree type;
2924 tree body;
2925 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32];
2927 /* Create the identifier for this function. It will be of the form
2928 SSDF_IDENTIFIER_<number>. */
2929 sprintf (id, "%s_%u", SSDF_IDENTIFIER, ssdf_number++);
2930 if (ssdf_number == 0)
2932 /* Overflow occurred. That means there are at least 4 billion
2933 initialization functions. */
2934 sorry ("too many initialization functions required");
2935 my_friendly_abort (19990430);
2938 /* Create the parameters. */
2939 parm_types = void_list_node;
2940 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2941 parm_types = tree_cons (NULL_TREE, integer_type_node, parm_types);
2942 type = build_function_type (void_type_node, parm_types);
2944 /* Create the FUNCTION_DECL itself. */
2945 ssdf_decl = build_lang_decl (FUNCTION_DECL,
2946 get_identifier (id),
2947 type);
2948 TREE_PUBLIC (ssdf_decl) = 0;
2949 DECL_ARTIFICIAL (ssdf_decl) = 1;
2951 /* Put this function in the list of functions to be called from the
2952 static constructors and destructors. */
2953 if (!ssdf_decls)
2955 VARRAY_TREE_INIT (ssdf_decls, 32, "ssdf_decls");
2957 /* Take this opportunity to initialize the map from priority
2958 numbers to information about that priority level. */
2959 priority_info_map = splay_tree_new (splay_tree_compare_ints,
2960 /*delete_key_fn=*/0,
2961 /*delete_value_fn=*/
2962 (splay_tree_delete_value_fn) &free);
2964 /* We always need to generate functions for the
2965 DEFAULT_INIT_PRIORITY so enter it now. That way when we walk
2966 priorities later, we'll be sure to find the
2967 DEFAULT_INIT_PRIORITY. */
2968 get_priority_info (DEFAULT_INIT_PRIORITY);
2971 VARRAY_PUSH_TREE (ssdf_decls, ssdf_decl);
2973 /* Create the argument list. */
2974 initialize_p_decl = build_decl (PARM_DECL,
2975 get_identifier (INITIALIZE_P_IDENTIFIER),
2976 integer_type_node);
2977 DECL_CONTEXT (initialize_p_decl) = ssdf_decl;
2978 DECL_ARG_TYPE (initialize_p_decl) = integer_type_node;
2979 TREE_USED (initialize_p_decl) = 1;
2980 priority_decl = build_decl (PARM_DECL, get_identifier (PRIORITY_IDENTIFIER),
2981 integer_type_node);
2982 DECL_CONTEXT (priority_decl) = ssdf_decl;
2983 DECL_ARG_TYPE (priority_decl) = integer_type_node;
2984 TREE_USED (priority_decl) = 1;
2986 TREE_CHAIN (initialize_p_decl) = priority_decl;
2987 DECL_ARGUMENTS (ssdf_decl) = initialize_p_decl;
2989 /* Put the function in the global scope. */
2990 pushdecl (ssdf_decl);
2992 /* Start the function itself. This is equivalent to declarating the
2993 function as:
2995 static void __ssdf (int __initialize_p, init __priority_p);
2997 It is static because we only need to call this function from the
2998 various constructor and destructor functions for this module. */
2999 start_function (/*specs=*/NULL_TREE,
3000 ssdf_decl,
3001 /*attrs=*/NULL_TREE,
3002 SF_PRE_PARSED);
3004 /* Set up the scope of the outermost block in the function. */
3005 body = begin_compound_stmt (/*has_no_scope=*/0);
3007 /* This function must not be deferred because we are depending on
3008 its compilation to tell us what is TREE_SYMBOL_REFERENCED. */
3009 current_function_cannot_inline
3010 = "static storage duration functions cannot be inlined";
3012 return body;
3015 /* Finish the generation of the function which performs initialization
3016 and destruction of objects with static storage duration. After
3017 this point, no more such objects can be created. */
3019 static void
3020 finish_static_storage_duration_function (body)
3021 tree body;
3023 /* Close out the function. */
3024 finish_compound_stmt (/*has_no_scope=*/0, body);
3025 expand_body (finish_function (lineno, 0));
3028 /* Return the information about the indicated PRIORITY level. If no
3029 code to handle this level has yet been generated, generate the
3030 appropriate prologue. */
3032 static priority_info
3033 get_priority_info (priority)
3034 int priority;
3036 priority_info pi;
3037 splay_tree_node n;
3039 n = splay_tree_lookup (priority_info_map,
3040 (splay_tree_key) priority);
3041 if (!n)
3043 /* Create a new priority information structure, and insert it
3044 into the map. */
3045 pi = (priority_info) xmalloc (sizeof (struct priority_info_s));
3046 pi->initializations_p = 0;
3047 pi->destructions_p = 0;
3048 splay_tree_insert (priority_info_map,
3049 (splay_tree_key) priority,
3050 (splay_tree_value) pi);
3052 else
3053 pi = (priority_info) n->value;
3055 return pi;
3058 /* Set up to handle the initialization or destruction of DECL. If
3059 INITP is non-zero, we are initializing the variable. Otherwise, we
3060 are destroying it. */
3062 static tree
3063 start_static_initialization_or_destruction (decl, initp)
3064 tree decl;
3065 int initp;
3067 tree sentry_if_stmt = NULL_TREE;
3068 int priority;
3069 tree cond;
3070 tree init_cond;
3071 priority_info pi;
3073 /* Figure out the priority for this declaration. */
3074 priority = DECL_INIT_PRIORITY (decl);
3075 if (!priority)
3076 priority = DEFAULT_INIT_PRIORITY;
3078 /* Remember that we had an initialization or finalization at this
3079 priority. */
3080 pi = get_priority_info (priority);
3081 if (initp)
3082 pi->initializations_p = 1;
3083 else
3084 pi->destructions_p = 1;
3086 /* Trick the compiler into thinking we are at the file and line
3087 where DECL was declared so that error-messages make sense, and so
3088 that the debugger will show somewhat sensible file and line
3089 information. */
3090 input_filename = DECL_SOURCE_FILE (decl);
3091 lineno = DECL_SOURCE_LINE (decl);
3093 /* Because of:
3095 [class.access.spec]
3097 Access control for implicit calls to the constructors,
3098 the conversion functions, or the destructor called to
3099 create and destroy a static data member is performed as
3100 if these calls appeared in the scope of the member's
3101 class.
3103 we pretend we are in a static member function of the class of
3104 which the DECL is a member. */
3105 if (member_p (decl))
3107 DECL_CLASS_CONTEXT (current_function_decl) = DECL_CONTEXT (decl);
3108 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3111 /* Conditionalize this initialization on being in the right priority
3112 and being initializing/finalizing appropriately. */
3113 sentry_if_stmt = begin_if_stmt ();
3114 cond = build_binary_op (EQ_EXPR,
3115 priority_decl,
3116 build_int_2 (priority, 0));
3117 init_cond = initp ? integer_one_node : integer_zero_node;
3118 init_cond = build_binary_op (EQ_EXPR,
3119 initialize_p_decl,
3120 init_cond);
3121 cond = build_binary_op (TRUTH_ANDIF_EXPR, cond, init_cond);
3123 /* We need a sentry if this is an object with external linkage that
3124 might be initialized in more than one place. */
3125 if (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3126 || DECL_ONE_ONLY (decl)
3127 || DECL_WEAK (decl)))
3129 tree sentry;
3130 tree sentry_cond;
3132 sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3134 /* We do initializations only if the SENTRY is zero, i.e., if we
3135 are the first to initialize the variable. We do destructions
3136 only if the SENTRY is one, i.e., if we are the last to
3137 destroy the variable. */
3138 if (initp)
3139 sentry_cond = build_binary_op (EQ_EXPR,
3140 build_unary_op (PREINCREMENT_EXPR,
3141 sentry,
3142 /*noconvert=*/1),
3143 integer_one_node);
3144 else
3145 sentry_cond = build_binary_op (EQ_EXPR,
3146 build_unary_op (PREDECREMENT_EXPR,
3147 sentry,
3148 /*noconvert=*/1),
3149 integer_zero_node);
3151 cond = build_binary_op (TRUTH_ANDIF_EXPR, cond, sentry_cond);
3154 finish_if_stmt_cond (cond, sentry_if_stmt);
3156 return sentry_if_stmt;
3159 /* We've just finished generating code to do an initialization or
3160 finalization. SENTRY_IF_STMT is the if-statement we used to guard
3161 the initialization. */
3163 static void
3164 finish_static_initialization_or_destruction (sentry_if_stmt)
3165 tree sentry_if_stmt;
3167 finish_then_clause (sentry_if_stmt);
3168 finish_if_stmt ();
3170 /* Now that we're done with DECL we don't need to pretend to be a
3171 member of its class any longer. */
3172 DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
3173 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3176 /* Generate code to do the static initialization of DECL. The
3177 initialization is INIT. If DECL may be initialized more than once
3178 in different object files, SENTRY is the guard variable to
3179 check. PRIORITY is the priority for the initialization. */
3181 static void
3182 do_static_initialization (decl, init)
3183 tree decl;
3184 tree init;
3186 tree expr;
3187 tree sentry_if_stmt;
3189 /* Set up for the initialization. */
3190 sentry_if_stmt
3191 = start_static_initialization_or_destruction (decl,
3192 /*initp=*/1);
3194 /* Do the initialization itself. */
3195 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3196 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3197 expr = build_aggr_init (decl, init, 0);
3198 else if (TREE_CODE (init) == TREE_VEC)
3199 expr = build_vec_init (decl, TREE_VEC_ELT (init, 0),
3200 TREE_VEC_ELT (init, 1),
3201 TREE_VEC_ELT (init, 2), 0);
3202 else
3204 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
3205 TREE_SIDE_EFFECTS (expr) = 1;
3207 finish_expr_stmt (expr);
3209 /* Finsh up. */
3210 finish_static_initialization_or_destruction (sentry_if_stmt);
3213 /* Generate code to do the static destruction of DECL. If DECL may be
3214 initialized more than once in different object files, SENTRY is the
3215 guard variable to check. PRIORITY is the priority for the
3216 destruction. */
3218 static void
3219 do_static_destruction (decl)
3220 tree decl;
3222 tree sentry_if_stmt;
3224 /* If we don't need a destructor, there's nothing to do. */
3225 if (!TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
3226 return;
3228 /* Actually do the destruction. */
3229 sentry_if_stmt = start_static_initialization_or_destruction (decl,
3230 /*initp=*/0);
3231 finish_expr_stmt (build_cleanup (decl));
3232 finish_static_initialization_or_destruction (sentry_if_stmt);
3235 /* VARS is a list of variables with static storage duration which may
3236 need initialization and/or finalization. Remove those variables
3237 that don't really need to be initialized or finalized, and return
3238 the resulting list. The order in which the variables appear in
3239 VARS is in reverse order of the order in which they should actually
3240 be initialized. The list we return is in the unreversed order;
3241 i.e., the first variable should be initialized first. */
3243 static tree
3244 prune_vars_needing_no_initialization (vars)
3245 tree vars;
3247 tree var;
3248 tree result;
3250 for (var = vars, result = NULL_TREE;
3251 var;
3252 var = TREE_CHAIN (var))
3254 tree decl = TREE_VALUE (var);
3255 tree init = TREE_PURPOSE (var);
3257 /* Deal gracefully with error. */
3258 if (decl == error_mark_node)
3259 continue;
3261 /* The only things that can be initialized are variables. */
3262 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 19990420);
3264 /* If this object is not defined, we don't need to do anything
3265 here. */
3266 if (DECL_EXTERNAL (decl))
3267 continue;
3269 /* Also, if the initializer already contains errors, we can bail
3270 out now. */
3271 if (init && TREE_CODE (init) == TREE_LIST
3272 && value_member (error_mark_node, init))
3273 continue;
3275 /* This variable is going to need initialization and/or
3276 finalization, so we add it to the list. */
3277 result = tree_cons (init, decl, result);
3280 return result;
3283 /* Make sure we have told the back end about all the variables in
3284 VARS. */
3286 static void
3287 write_out_vars (vars)
3288 tree vars;
3290 tree v;
3292 for (v = vars; v; v = TREE_CHAIN (v))
3293 if (! TREE_ASM_WRITTEN (TREE_VALUE (v)))
3294 rest_of_decl_compilation (TREE_VALUE (v), 0, 1, 1);
3297 /* Generate a static constructor (if CONSTRUCTOR_P) or destructor
3298 (otherwise) that will initialize all gobal objects with static
3299 storage duration having the indicated PRIORITY. */
3301 static void
3302 generate_ctor_or_dtor_function (constructor_p, priority)
3303 int constructor_p;
3304 int priority;
3306 char function_key;
3307 tree arguments;
3308 tree body;
3309 size_t i;
3311 /* We use `I' to indicate initialization and `D' to indicate
3312 destruction. */
3313 if (constructor_p)
3314 function_key = 'I';
3315 else
3316 function_key = 'D';
3318 /* Begin the function. */
3319 body = start_objects (function_key, priority);
3321 /* Call the static storage duration function with appropriate
3322 arguments. */
3323 for (i = 0; i < ssdf_decls->elements_used; ++i)
3325 arguments = tree_cons (NULL_TREE, build_int_2 (priority, 0),
3326 NULL_TREE);
3327 arguments = tree_cons (NULL_TREE, build_int_2 (constructor_p, 0),
3328 arguments);
3329 finish_expr_stmt (build_function_call (VARRAY_TREE (ssdf_decls, i),
3330 arguments));
3333 /* If we're generating code for the DEFAULT_INIT_PRIORITY, throw in
3334 calls to any functions marked with attributes indicating that
3335 they should be called at initialization- or destruction-time. */
3336 if (priority == DEFAULT_INIT_PRIORITY)
3338 tree fns;
3340 for (fns = constructor_p ? static_ctors : static_dtors;
3341 fns;
3342 fns = TREE_CHAIN (fns))
3343 finish_expr_stmt (build_function_call (TREE_VALUE (fns), NULL_TREE));
3346 /* Close out the function. */
3347 finish_objects (function_key, priority, body);
3350 /* Generate constructor and destructor functions for the priority
3351 indicated by N. */
3353 static int
3354 generate_ctor_and_dtor_functions_for_priority (n, data)
3355 splay_tree_node n;
3356 void *data ATTRIBUTE_UNUSED;
3358 int priority = (int) n->key;
3359 priority_info pi = (priority_info) n->value;
3361 /* Generate the functions themselves, but only if they are really
3362 needed. */
3363 if (pi->initializations_p
3364 || (priority == DEFAULT_INIT_PRIORITY && static_ctors))
3365 generate_ctor_or_dtor_function (/*constructor_p=*/1,
3366 priority);
3367 if (pi->destructions_p
3368 || (priority == DEFAULT_INIT_PRIORITY && static_dtors))
3369 generate_ctor_or_dtor_function (/*constructor_p=*/0,
3370 priority);
3372 /* Keep iterating. */
3373 return 0;
3376 /* This routine is called from the last rule in yyparse ().
3377 Its job is to create all the code needed to initialize and
3378 destroy the global aggregates. We do the destruction
3379 first, since that way we only need to reverse the decls once. */
3381 void
3382 finish_file ()
3384 extern int lineno;
3385 int start_time, this_time;
3386 tree vars;
3387 int reconsider;
3388 size_t i;
3390 at_eof = 1;
3392 /* Bad parse errors. Just forget about it. */
3393 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3394 return;
3396 start_time = get_run_time ();
3398 /* Otherwise, GDB can get confused, because in only knows
3399 about source for LINENO-1 lines. */
3400 lineno -= 1;
3402 interface_unknown = 1;
3403 interface_only = 0;
3405 /* We now have to write out all the stuff we put off writing out.
3406 These include:
3408 o Template specializations that we have not yet instantiated,
3409 but which are needed.
3410 o Initialization and destruction for non-local objects with
3411 static storage duration. (Local objects with static storage
3412 duration are initialized when their scope is first entered,
3413 and are cleaned up via atexit.)
3414 o Virtual function tables.
3416 All of these may cause others to be needed. For example,
3417 instantiating one function may cause another to be needed, and
3418 generating the intiailzer for an object may cause templates to be
3419 instantiated, etc., etc. */
3421 this_time = get_run_time ();
3422 parse_time -= this_time - start_time;
3423 varconst_time += this_time - start_time;
3424 start_time = get_run_time ();
3428 reconsider = 0;
3430 /* If there are templates that we've put off instantiating, do
3431 them now. */
3432 instantiate_pending_templates ();
3434 /* Write out virtual tables as required. Note that writing out
3435 the virtual table for a template class may cause the
3436 instantiation of members of that class. */
3437 if (walk_globals (vtable_decl_p,
3438 finish_vtable_vardecl,
3439 /*data=*/0))
3440 reconsider = 1;
3442 /* The list of objects with static storage duration is built up
3443 in reverse order. We clear STATIC_AGGREGATES so that any new
3444 aggregates added during the initialization of these will be
3445 initialized in the correct order when we next come around the
3446 loop. */
3447 vars = prune_vars_needing_no_initialization (static_aggregates);
3448 static_aggregates = NULL_TREE;
3450 if (vars)
3452 tree v;
3454 /* We need to start a new initialization function each time
3455 through the loop. That's because we need to know which
3456 vtables have been referenced, and TREE_SYMBOL_REFERENCED
3457 isn't computed until a function is finished, and written
3458 out. That's a deficiency in the back-end. When this is
3459 fixed, these initialization functions could all become
3460 inline, with resulting performance improvements. */
3461 tree ssdf_body = start_static_storage_duration_function ();
3463 /* Make sure the back end knows about all the variables. */
3464 write_out_vars (vars);
3466 /* First generate code to do all the initializations. */
3467 for (v = vars; v; v = TREE_CHAIN (v))
3468 do_static_initialization (TREE_VALUE (v),
3469 TREE_PURPOSE (v));
3471 /* Then, generate code to do all the destructions. Do these
3472 in reverse order so that the most recently constructed
3473 variable is the first destroyed. */
3474 vars = nreverse (vars);
3475 for (v = vars; v; v = TREE_CHAIN (v))
3476 do_static_destruction (TREE_VALUE (v));
3478 /* Finish up the static storage duration function for this
3479 round. */
3480 finish_static_storage_duration_function (ssdf_body);
3482 /* All those initializations and finalizations might cause
3483 us to need more inline functions, more template
3484 instantiations, etc. */
3485 reconsider = 1;
3488 /* Go through the various inline functions, and see if any need
3489 synthesizing. */
3490 for (i = 0; i < saved_inlines_used; ++i)
3492 tree decl = VARRAY_TREE (saved_inlines, i);
3493 import_export_decl (decl);
3494 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3495 && TREE_USED (decl)
3496 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3498 /* Even though we're already at the top-level, we push
3499 there again. That way, when we pop back a few lines
3500 hence, all of our state is restored. Otherwise,
3501 finish_function doesn't clean things up, and we end
3502 up with CURRENT_FUNCTION_DECL set. */
3503 push_to_top_level ();
3504 if (DECL_TINFO_FN_P (decl))
3505 synthesize_tinfo_fn (decl);
3506 else
3507 synthesize_method (decl);
3508 pop_from_top_level ();
3509 reconsider = 1;
3513 /* Mark all functions that might deal with exception-handling as
3514 referenced. */
3515 mark_all_runtime_matches ();
3517 /* We lie to the back-end, pretending that some functions are
3518 not defined when they really are. This keeps these functions
3519 from being put out unncessarily. But, we must stop lying
3520 when the functions are referenced, or if they are not comdat
3521 since they need to be put out now. */
3522 for (i = 0; i < saved_inlines_used; ++i)
3524 tree decl = VARRAY_TREE (saved_inlines, i);
3526 if (DECL_NOT_REALLY_EXTERN (decl)
3527 && DECL_INITIAL (decl)
3528 && (DECL_NEEDED_P (decl) || !DECL_COMDAT (decl)))
3529 DECL_EXTERNAL (decl) = 0;
3531 /* If we're going to need to write this function out, and
3532 there's already a body for it, create RTL for it now.
3533 (There might be no body if this is a method we haven't
3534 gotten around to synthesizing yet.) */
3535 if (!DECL_EXTERNAL (decl)
3536 && (DECL_NEEDED_P (decl) || !DECL_COMDAT (decl))
3537 && DECL_SAVED_TREE (decl)
3538 && !DECL_SAVED_INSNS (decl)
3539 && !TREE_ASM_WRITTEN (decl))
3541 int saved_not_really_extern;
3543 /* When we call finish_function in expand_body, it will
3544 try to reset DECL_NOT_REALLY_EXTERN so we save and
3545 restore it here. */
3546 saved_not_really_extern = DECL_NOT_REALLY_EXTERN (decl);
3547 /* Generate RTL for this function now that we know we
3548 need it. */
3549 expand_body (decl);
3550 /* Undo the damage done by finish_function. */
3551 DECL_EXTERNAL (decl) = 0;
3552 DECL_NOT_REALLY_EXTERN (decl) = saved_not_really_extern;
3553 /* If we're compiling -fsyntax-only pretend that this
3554 function has been written out so that we don't try to
3555 expand it again. */
3556 if (flag_syntax_only)
3557 TREE_ASM_WRITTEN (decl) = 1;
3558 reconsider = 1;
3562 if (saved_inlines_used
3563 && wrapup_global_declarations (&VARRAY_TREE (saved_inlines, 0),
3564 saved_inlines_used))
3565 reconsider = 1;
3566 if (walk_namespaces (wrapup_globals_for_namespace, /*data=*/0))
3567 reconsider = 1;
3569 /* Static data members are just like namespace-scope globals. */
3570 for (i = 0; i < pending_statics_used; ++i)
3572 tree decl = VARRAY_TREE (pending_statics, i);
3573 if (TREE_ASM_WRITTEN (decl))
3574 continue;
3575 import_export_decl (decl);
3576 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3577 DECL_EXTERNAL (decl) = 0;
3579 if (pending_statics
3580 && wrapup_global_declarations (&VARRAY_TREE (pending_statics, 0),
3581 pending_statics_used))
3582 reconsider = 1;
3584 while (reconsider);
3586 /* We give C linkage to static constructors and destructors. */
3587 push_lang_context (lang_name_c);
3589 /* Generate initialization and destruction functions for all
3590 priorities for which they are required. */
3591 if (priority_info_map)
3592 splay_tree_foreach (priority_info_map,
3593 generate_ctor_and_dtor_functions_for_priority,
3594 /*data=*/0);
3596 /* We're done with the splay-tree now. */
3597 if (priority_info_map)
3598 splay_tree_delete (priority_info_map);
3600 /* We're done with static constructors, so we can go back to "C++"
3601 linkage now. */
3602 pop_lang_context ();
3604 /* Now delete from the chain of variables all virtual function tables.
3605 We output them all ourselves, because each will be treated
3606 specially. We don't do this if we're just doing semantic
3607 analysis, and not code-generation. */
3608 if (!flag_syntax_only)
3609 walk_globals (vtable_decl_p, prune_vtable_vardecl, /*data=*/0);
3611 /* Now, issue warnings about static, but not defined, functions,
3612 etc., and emit debugging information. */
3613 walk_namespaces (wrapup_globals_for_namespace, /*data=*/&reconsider);
3614 if (pending_statics)
3615 check_global_declarations (&VARRAY_TREE (pending_statics, 0),
3616 pending_statics_used);
3618 finish_repo ();
3620 /* The entire file is now complete. If requested, dump everything
3621 to a file. */
3622 if (flag_dump_translation_unit)
3623 dump_node_to_file (global_namespace, flag_dump_translation_unit);
3625 /* If there's some tool that wants to examine the entire translation
3626 unit, let it do so now. */
3627 if (back_end_hook)
3628 (*back_end_hook) (global_namespace);
3630 this_time = get_run_time ();
3631 parse_time -= this_time - start_time;
3632 varconst_time += this_time - start_time;
3634 if (flag_detailed_statistics)
3636 dump_tree_statistics ();
3637 dump_time_statistics ();
3641 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3642 expr. Since it was parsed like a type, we need to wade through and fix
3643 that. Unfortunately, since operator() is left-associative, we can't use
3644 tail recursion. In the above example, TYPE is `A', and DECL is
3645 `()()()()()'.
3647 Maybe this shouldn't be recursive, but how often will it actually be
3648 used? (jason) */
3650 tree
3651 reparse_absdcl_as_expr (type, decl)
3652 tree type, decl;
3654 /* do build_functional_cast (type, NULL_TREE) at bottom */
3655 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3656 return build_functional_cast (type, NULL_TREE);
3658 /* recurse */
3659 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3661 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3663 if (TREE_CODE (decl) == CALL_EXPR
3664 && (! TREE_TYPE (decl)
3665 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3666 decl = require_complete_type (decl);
3668 return decl;
3671 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3672 out to be an expr. Since it was parsed like a type, we need to wade
3673 through and fix that. Since casts are right-associative, we are
3674 reversing the order, so we don't have to recurse.
3676 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3677 `1'. */
3679 tree
3680 reparse_absdcl_as_casts (decl, expr)
3681 tree decl, expr;
3683 tree type;
3685 if (TREE_CODE (expr) == CONSTRUCTOR
3686 && TREE_TYPE (expr) == 0)
3688 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3689 decl = TREE_OPERAND (decl, 0);
3691 expr = digest_init (type, expr, (tree *) 0);
3692 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
3694 int failure = complete_array_type (type, expr, 1);
3695 if (failure)
3696 my_friendly_abort (78);
3700 while (decl)
3702 type = groktypename (TREE_VALUE (CALL_DECLARATOR_PARMS (decl)));
3703 decl = TREE_OPERAND (decl, 0);
3704 expr = build_c_cast (type, expr);
3707 if (warn_old_style_cast && ! in_system_header
3708 && current_lang_name != lang_name_c)
3709 warning ("use of old-style cast");
3711 return expr;
3714 /* Given plain tree nodes for an expression, build up the full semantics. */
3716 tree
3717 build_expr_from_tree (t)
3718 tree t;
3720 if (t == NULL_TREE || t == error_mark_node)
3721 return t;
3723 switch (TREE_CODE (t))
3725 case IDENTIFIER_NODE:
3726 return do_identifier (t, 0, NULL_TREE);
3728 case LOOKUP_EXPR:
3729 if (LOOKUP_EXPR_GLOBAL (t))
3730 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3731 else
3732 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3734 case TEMPLATE_ID_EXPR:
3735 return (lookup_template_function
3736 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3737 build_expr_from_tree (TREE_OPERAND (t, 1))));
3739 case INDIRECT_REF:
3740 return build_x_indirect_ref
3741 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3743 case CAST_EXPR:
3744 return build_functional_cast
3745 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3747 case REINTERPRET_CAST_EXPR:
3748 return build_reinterpret_cast
3749 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3751 case CONST_CAST_EXPR:
3752 return build_const_cast
3753 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3755 case DYNAMIC_CAST_EXPR:
3756 return build_dynamic_cast
3757 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3759 case STATIC_CAST_EXPR:
3760 return build_static_cast
3761 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3763 case PREDECREMENT_EXPR:
3764 case PREINCREMENT_EXPR:
3765 case POSTDECREMENT_EXPR:
3766 case POSTINCREMENT_EXPR:
3767 case NEGATE_EXPR:
3768 case BIT_NOT_EXPR:
3769 case ABS_EXPR:
3770 case TRUTH_NOT_EXPR:
3771 case ADDR_EXPR:
3772 case CONVERT_EXPR: /* Unary + */
3773 case REALPART_EXPR:
3774 case IMAGPART_EXPR:
3775 if (TREE_TYPE (t))
3776 return t;
3777 return build_x_unary_op (TREE_CODE (t),
3778 build_expr_from_tree (TREE_OPERAND (t, 0)));
3780 case PLUS_EXPR:
3781 case MINUS_EXPR:
3782 case MULT_EXPR:
3783 case TRUNC_DIV_EXPR:
3784 case CEIL_DIV_EXPR:
3785 case FLOOR_DIV_EXPR:
3786 case ROUND_DIV_EXPR:
3787 case EXACT_DIV_EXPR:
3788 case BIT_AND_EXPR:
3789 case BIT_ANDTC_EXPR:
3790 case BIT_IOR_EXPR:
3791 case BIT_XOR_EXPR:
3792 case TRUNC_MOD_EXPR:
3793 case FLOOR_MOD_EXPR:
3794 case TRUTH_ANDIF_EXPR:
3795 case TRUTH_ORIF_EXPR:
3796 case TRUTH_AND_EXPR:
3797 case TRUTH_OR_EXPR:
3798 case RSHIFT_EXPR:
3799 case LSHIFT_EXPR:
3800 case RROTATE_EXPR:
3801 case LROTATE_EXPR:
3802 case EQ_EXPR:
3803 case NE_EXPR:
3804 case MAX_EXPR:
3805 case MIN_EXPR:
3806 case LE_EXPR:
3807 case GE_EXPR:
3808 case LT_EXPR:
3809 case GT_EXPR:
3810 case MEMBER_REF:
3811 return build_x_binary_op
3812 (TREE_CODE (t),
3813 build_expr_from_tree (TREE_OPERAND (t, 0)),
3814 build_expr_from_tree (TREE_OPERAND (t, 1)));
3816 case DOTSTAR_EXPR:
3817 return build_m_component_ref
3818 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3819 build_expr_from_tree (TREE_OPERAND (t, 1)));
3821 case SCOPE_REF:
3822 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3824 case ARRAY_REF:
3825 if (TREE_OPERAND (t, 0) == NULL_TREE)
3826 /* new-type-id */
3827 return build_parse_node (ARRAY_REF, NULL_TREE,
3828 build_expr_from_tree (TREE_OPERAND (t, 1)));
3829 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3830 build_expr_from_tree (TREE_OPERAND (t, 1)));
3832 case SIZEOF_EXPR:
3833 case ALIGNOF_EXPR:
3835 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3836 if (TREE_CODE_CLASS (TREE_CODE (r)) != 't')
3837 r = TREE_TYPE (r);
3838 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3841 case MODOP_EXPR:
3842 return build_x_modify_expr
3843 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3844 TREE_CODE (TREE_OPERAND (t, 1)),
3845 build_expr_from_tree (TREE_OPERAND (t, 2)));
3847 case ARROW_EXPR:
3848 return build_x_arrow
3849 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3851 case NEW_EXPR:
3852 return build_new
3853 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3854 build_expr_from_tree (TREE_OPERAND (t, 1)),
3855 build_expr_from_tree (TREE_OPERAND (t, 2)),
3856 NEW_EXPR_USE_GLOBAL (t));
3858 case DELETE_EXPR:
3859 return delete_sanity
3860 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3861 build_expr_from_tree (TREE_OPERAND (t, 1)),
3862 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3864 case COMPOUND_EXPR:
3865 if (TREE_OPERAND (t, 1) == NULL_TREE)
3866 return build_x_compound_expr
3867 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3868 else
3869 my_friendly_abort (42);
3871 case METHOD_CALL_EXPR:
3872 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3874 tree ref = TREE_OPERAND (t, 0);
3875 return build_scoped_method_call
3876 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3877 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3878 TREE_OPERAND (ref, 1),
3879 build_expr_from_tree (TREE_OPERAND (t, 2)));
3881 else
3883 tree fn = TREE_OPERAND (t, 0);
3885 /* We can get a TEMPLATE_ID_EXPR here on code like:
3887 x->f<2>();
3889 so we must resolve that. However, we can also get things
3890 like a BIT_NOT_EXPR here, when referring to a destructor,
3891 and things like that are not correctly resolved by
3892 build_expr_from_tree. So, just use build_expr_from_tree
3893 when we really need it. */
3894 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3895 fn = lookup_template_function
3896 (TREE_OPERAND (fn, 0),
3897 build_expr_from_tree (TREE_OPERAND (fn, 1)));
3899 return build_method_call
3900 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3902 build_expr_from_tree (TREE_OPERAND (t, 2)),
3903 NULL_TREE, LOOKUP_NORMAL);
3906 case CALL_EXPR:
3907 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3909 tree ref = TREE_OPERAND (t, 0);
3910 return build_member_call
3911 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3912 TREE_OPERAND (ref, 1),
3913 build_expr_from_tree (TREE_OPERAND (t, 1)));
3915 else
3917 tree name = TREE_OPERAND (t, 0);
3918 tree id;
3919 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3920 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3921 && !LOOKUP_EXPR_GLOBAL (name)
3922 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3923 && (!current_class_type
3924 || !lookup_member (current_class_type, id, 0, 0)))
3926 /* Do Koenig lookup if there are no class members. */
3927 name = do_identifier (id, 0, args);
3929 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3930 || ! really_overloaded_fn (name))
3931 name = build_expr_from_tree (name);
3932 return build_x_function_call (name, args, current_class_ref);
3935 case COND_EXPR:
3936 return build_x_conditional_expr
3937 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3938 build_expr_from_tree (TREE_OPERAND (t, 1)),
3939 build_expr_from_tree (TREE_OPERAND (t, 2)));
3941 case PSEUDO_DTOR_EXPR:
3942 return (finish_pseudo_destructor_call_expr
3943 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3944 build_expr_from_tree (TREE_OPERAND (t, 1)),
3945 build_expr_from_tree (TREE_OPERAND (t, 2))));
3947 case TREE_LIST:
3949 tree purpose, value, chain;
3951 if (t == void_list_node)
3952 return t;
3954 purpose = TREE_PURPOSE (t);
3955 if (purpose)
3956 purpose = build_expr_from_tree (purpose);
3957 value = TREE_VALUE (t);
3958 if (value)
3959 value = build_expr_from_tree (value);
3960 chain = TREE_CHAIN (t);
3961 if (chain && chain != void_type_node)
3962 chain = build_expr_from_tree (chain);
3963 return tree_cons (purpose, value, chain);
3966 case COMPONENT_REF:
3968 tree object = build_expr_from_tree (TREE_OPERAND (t, 0));
3969 tree field = TREE_OPERAND (t, 1);
3971 /* We use a COMPONENT_REF to indicate things of the form `x.b'
3972 and `x.A::b'. We must distinguish between those cases
3973 here. */
3974 if (TREE_CODE (field) == SCOPE_REF)
3975 return build_object_ref (object,
3976 TREE_OPERAND (field, 0),
3977 TREE_OPERAND (field, 1));
3978 else
3979 return build_x_component_ref (object, field,
3980 NULL_TREE, 1);
3983 case THROW_EXPR:
3984 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3986 case CONSTRUCTOR:
3988 tree r;
3990 /* digest_init will do the wrong thing if we let it. */
3991 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3992 return t;
3994 r = build_nt (CONSTRUCTOR, NULL_TREE,
3995 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
3996 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3998 if (TREE_TYPE (t))
3999 return digest_init (TREE_TYPE (t), r, 0);
4000 return r;
4003 case TYPEID_EXPR:
4004 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 0))) == 't')
4005 return get_typeid (TREE_OPERAND (t, 0));
4006 return build_x_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
4008 case VAR_DECL:
4009 return convert_from_reference (t);
4011 case VA_ARG_EXPR:
4012 return build_va_arg (build_expr_from_tree (TREE_OPERAND (t, 0)),
4013 TREE_TYPE (t));
4015 default:
4016 return t;
4020 /* This is something of the form `int (*a)++' that has turned out to be an
4021 expr. It was only converted into parse nodes, so we need to go through
4022 and build up the semantics. Most of the work is done by
4023 build_expr_from_tree, above.
4025 In the above example, TYPE is `int' and DECL is `*a'. */
4027 tree
4028 reparse_decl_as_expr (type, decl)
4029 tree type, decl;
4031 decl = build_expr_from_tree (decl);
4032 if (type)
4033 return build_functional_cast (type, build_expr_list (NULL_TREE, decl));
4034 else
4035 return decl;
4038 /* This is something of the form `int (*a)' that has turned out to be a
4039 decl. It was only converted into parse nodes, so we need to do the
4040 checking that make_{pointer,reference}_declarator do. */
4042 tree
4043 finish_decl_parsing (decl)
4044 tree decl;
4046 extern int current_class_depth;
4048 switch (TREE_CODE (decl))
4050 case IDENTIFIER_NODE:
4051 return decl;
4052 case INDIRECT_REF:
4053 return make_pointer_declarator
4054 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4055 case ADDR_EXPR:
4056 return make_reference_declarator
4057 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
4058 case BIT_NOT_EXPR:
4059 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4060 return decl;
4061 case SCOPE_REF:
4062 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
4063 TREE_COMPLEXITY (decl) = current_class_depth;
4064 return decl;
4065 case ARRAY_REF:
4066 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
4067 return decl;
4068 case TREE_LIST:
4069 /* For attribute handling. */
4070 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
4071 return decl;
4072 default:
4073 my_friendly_abort (5);
4074 return NULL_TREE;
4078 tree
4079 check_cp_case_value (value)
4080 tree value;
4082 if (value == NULL_TREE)
4083 return value;
4085 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4086 STRIP_TYPE_NOPS (value);
4088 if (TREE_READONLY_DECL_P (value))
4090 value = decl_constant_value (value);
4091 STRIP_TYPE_NOPS (value);
4093 value = fold (value);
4095 if (TREE_CODE (value) != INTEGER_CST
4096 && value != error_mark_node)
4098 cp_error ("case label `%E' does not reduce to an integer constant",
4099 value);
4100 value = error_mark_node;
4102 else
4103 /* Promote char or short to int. */
4104 value = default_conversion (value);
4106 constant_expression_warning (value);
4108 return value;
4111 /* Return 1 if root encloses child. */
4113 static int
4114 is_namespace_ancestor (root, child)
4115 tree root, child;
4117 if (root == child)
4118 return 1;
4119 if (root == global_namespace)
4120 return 1;
4121 if (child == global_namespace)
4122 return 0;
4123 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4127 /* Return the namespace that is the common ancestor
4128 of two given namespaces. */
4130 tree
4131 namespace_ancestor (ns1, ns2)
4132 tree ns1, ns2;
4134 if (is_namespace_ancestor (ns1, ns2))
4135 return ns1;
4136 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4139 /* Insert used into the using list of user. Set indirect_flag if this
4140 directive is not directly from the source. Also find the common
4141 ancestor and let our users know about the new namespace */
4142 static void
4143 add_using_namespace (user, used, indirect)
4144 tree user;
4145 tree used;
4146 int indirect;
4148 tree t;
4149 /* Using oneself is a no-op. */
4150 if (user == used)
4151 return;
4152 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4153 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4154 /* Check if we already have this. */
4155 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4156 if (t != NULL_TREE)
4158 if (!indirect)
4159 /* Promote to direct usage. */
4160 TREE_INDIRECT_USING (t) = 0;
4161 return;
4164 /* Add used to the user's using list. */
4165 DECL_NAMESPACE_USING (user)
4166 = tree_cons (used, namespace_ancestor (user, used),
4167 DECL_NAMESPACE_USING (user));
4169 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4171 /* Add user to the used's users list. */
4172 DECL_NAMESPACE_USERS (used)
4173 = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4175 /* Recursively add all namespaces used. */
4176 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4177 /* indirect usage */
4178 add_using_namespace (user, TREE_PURPOSE (t), 1);
4180 /* Tell everyone using us about the new used namespaces. */
4181 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4182 add_using_namespace (TREE_PURPOSE (t), used, 1);
4185 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4186 duplicates. The first list becomes the tail of the result.
4188 The algorithm is O(n^2). We could get this down to O(n log n) by
4189 doing a sort on the addresses of the functions, if that becomes
4190 necessary. */
4192 static tree
4193 merge_functions (s1, s2)
4194 tree s1;
4195 tree s2;
4197 for (; s2; s2 = OVL_NEXT (s2))
4199 tree fn = OVL_CURRENT (s2);
4200 if (! ovl_member (fn, s1))
4201 s1 = build_overload (fn, s1);
4203 return s1;
4206 /* This should return an error not all definitions define functions.
4207 It is not an error if we find two functions with exactly the
4208 same signature, only if these are selected in overload resolution.
4209 old is the current set of bindings, new the freshly-found binding.
4210 XXX Do we want to give *all* candidates in case of ambiguity?
4211 XXX In what way should I treat extern declarations?
4212 XXX I don't want to repeat the entire duplicate_decls here */
4214 static tree
4215 ambiguous_decl (name, old, new, flags)
4216 tree name;
4217 tree old;
4218 tree new;
4219 int flags;
4221 tree val, type;
4222 my_friendly_assert (old != NULL_TREE, 393);
4223 /* Copy the value. */
4224 val = BINDING_VALUE (new);
4225 if (val)
4226 switch (TREE_CODE (val))
4228 case TEMPLATE_DECL:
4229 /* If we expect types or namespaces, and not templates,
4230 or this is not a template class. */
4231 if (LOOKUP_QUALIFIERS_ONLY (flags)
4232 && !DECL_CLASS_TEMPLATE_P (val))
4233 val = NULL_TREE;
4234 break;
4235 case TYPE_DECL:
4236 if (LOOKUP_NAMESPACES_ONLY (flags))
4237 val = NULL_TREE;
4238 break;
4239 case NAMESPACE_DECL:
4240 if (LOOKUP_TYPES_ONLY (flags))
4241 val = NULL_TREE;
4242 break;
4243 default:
4244 if (LOOKUP_QUALIFIERS_ONLY (flags))
4245 val = NULL_TREE;
4248 if (!BINDING_VALUE (old))
4249 BINDING_VALUE (old) = val;
4250 else if (val && val != BINDING_VALUE (old))
4252 if (is_overloaded_fn (BINDING_VALUE (old))
4253 && is_overloaded_fn (val))
4255 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4256 val);
4258 else
4260 /* Some declarations are functions, some are not. */
4261 if (flags & LOOKUP_COMPLAIN)
4263 /* If we've already given this error for this lookup,
4264 BINDING_VALUE (old) is error_mark_node, so let's not
4265 repeat ourselves. */
4266 if (BINDING_VALUE (old) != error_mark_node)
4268 cp_error ("use of `%D' is ambiguous", name);
4269 cp_error_at (" first declared as `%#D' here",
4270 BINDING_VALUE (old));
4272 cp_error_at (" also declared as `%#D' here", val);
4274 BINDING_VALUE (old) = error_mark_node;
4277 /* ... and copy the type. */
4278 type = BINDING_TYPE (new);
4279 if (LOOKUP_NAMESPACES_ONLY (flags))
4280 type = NULL_TREE;
4281 if (!BINDING_TYPE (old))
4282 BINDING_TYPE (old) = type;
4283 else if (type && BINDING_TYPE (old) != type)
4285 if (flags & LOOKUP_COMPLAIN)
4287 cp_error ("`%D' denotes an ambiguous type",name);
4288 cp_error_at (" first type here", BINDING_TYPE (old));
4289 cp_error_at (" other type here", type);
4292 return old;
4295 /* Subroutine of unualified_namespace_lookup:
4296 Add the bindings of NAME in used namespaces to VAL.
4297 We are currently looking for names in namespace SCOPE, so we
4298 look through USINGS for using-directives of namespaces
4299 which have SCOPE as a common ancestor with the current scope.
4300 Returns zero on errors. */
4303 lookup_using_namespace (name, val, usings, scope, flags, spacesp)
4304 tree name, val, usings, scope;
4305 int flags;
4306 tree *spacesp;
4308 tree iter;
4309 tree val1;
4310 /* Iterate over all used namespaces in current, searching for using
4311 directives of scope. */
4312 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4313 if (TREE_VALUE (iter) == scope)
4315 if (spacesp)
4316 *spacesp = tree_cons (TREE_PURPOSE (iter), NULL_TREE,
4317 *spacesp);
4318 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4319 /* Resolve ambiguities. */
4320 val = ambiguous_decl (name, val, val1, flags);
4322 return BINDING_VALUE (val) != error_mark_node;
4325 /* [namespace.qual]
4326 Accepts the NAME to lookup and its qualifying SCOPE.
4327 Returns the name/type pair found into the CPLUS_BINDING RESULT,
4328 or 0 on error. */
4331 qualified_lookup_using_namespace (name, scope, result, flags)
4332 tree name;
4333 tree scope;
4334 tree result;
4335 int flags;
4337 /* Maintain a list of namespaces visited... */
4338 tree seen = NULL_TREE;
4339 /* ... and a list of namespace yet to see. */
4340 tree todo = NULL_TREE;
4341 tree usings;
4342 while (scope && (result != error_mark_node))
4344 seen = tree_cons (scope, NULL_TREE, seen);
4345 result = ambiguous_decl (name, result,
4346 binding_for_name (name, scope), flags);
4347 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4348 /* Consider using directives. */
4349 for (usings = DECL_NAMESPACE_USING (scope); usings;
4350 usings = TREE_CHAIN (usings))
4351 /* If this was a real directive, and we have not seen it. */
4352 if (!TREE_INDIRECT_USING (usings)
4353 && !purpose_member (TREE_PURPOSE (usings), seen))
4354 todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4355 if (todo)
4357 scope = TREE_PURPOSE (todo);
4358 todo = TREE_CHAIN (todo);
4360 else
4361 scope = NULL_TREE; /* If there never was a todo list. */
4363 return result != error_mark_node;
4366 /* [namespace.memdef]/2 */
4368 /* Set the context of a declaration to scope. Complain if we are not
4369 outside scope. */
4371 void
4372 set_decl_namespace (decl, scope, friendp)
4373 tree decl;
4374 tree scope;
4375 int friendp;
4377 tree old;
4378 if (scope == std_node)
4379 scope = global_namespace;
4380 /* Get rid of namespace aliases. */
4381 scope = ORIGINAL_NAMESPACE (scope);
4383 /* It is ok for friends to be qualified in parallel space. */
4384 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4385 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4386 decl, scope);
4387 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4388 if (scope != current_namespace)
4390 /* See whether this has been declared in the namespace. */
4391 old = namespace_binding (DECL_NAME (decl), scope);
4392 if (!old)
4393 /* No old declaration at all. */
4394 goto complain;
4395 if (!is_overloaded_fn (decl))
4396 /* Don't compare non-function decls with decls_match here,
4397 since it can't check for the correct constness at this
4398 point. pushdecl will find those errors later. */
4399 return;
4400 /* Since decl is a function, old should contain a function decl. */
4401 if (!is_overloaded_fn (old))
4402 goto complain;
4403 if (processing_template_decl || processing_specialization)
4404 /* We have not yet called push_template_decl to turn the
4405 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
4406 won't match. But, we'll check later, when we construct the
4407 template. */
4408 return;
4409 for (; old; old = OVL_NEXT (old))
4410 if (decls_match (decl, OVL_CURRENT (old)))
4411 return;
4413 else
4414 return;
4415 complain:
4416 cp_error ("`%D' should have been declared inside `%D'",
4417 decl, scope);
4420 /* Compute the namespace where a declaration is defined. */
4422 static tree
4423 decl_namespace (decl)
4424 tree decl;
4426 while (DECL_CONTEXT (decl))
4428 decl = DECL_CONTEXT (decl);
4429 if (TREE_CODE (decl) == NAMESPACE_DECL)
4430 return decl;
4431 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
4432 decl = TYPE_STUB_DECL (decl);
4433 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 390);
4436 return global_namespace;
4439 /* Return the namespace where the current declaration is declared. */
4441 tree
4442 current_decl_namespace ()
4444 tree result;
4445 /* If we have been pushed into a different namespace, use it. */
4446 if (decl_namespace_list)
4447 return TREE_PURPOSE (decl_namespace_list);
4449 if (current_class_type)
4450 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4451 else if (current_function_decl)
4452 result = decl_namespace (current_function_decl);
4453 else
4454 result = current_namespace;
4455 return result;
4458 /* Temporarily set the namespace for the current declaration. */
4460 void
4461 push_decl_namespace (decl)
4462 tree decl;
4464 if (TREE_CODE (decl) != NAMESPACE_DECL)
4465 decl = decl_namespace (decl);
4466 decl_namespace_list = tree_cons (decl, NULL_TREE, decl_namespace_list);
4469 void
4470 pop_decl_namespace ()
4472 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4475 /* Enter a class or namespace scope. */
4477 void
4478 push_scope (t)
4479 tree t;
4481 if (TREE_CODE (t) == NAMESPACE_DECL)
4482 push_decl_namespace (t);
4483 else
4484 pushclass (t, 2);
4487 /* Leave scope pushed by push_scope. */
4489 void
4490 pop_scope (t)
4491 tree t;
4493 if (TREE_CODE (t) == NAMESPACE_DECL)
4494 pop_decl_namespace ();
4495 else
4496 popclass ();
4499 /* [basic.lookup.koenig] */
4500 /* A non-zero return value in the functions below indicates an error.
4501 All nodes allocated in the procedure are on the scratch obstack. */
4503 struct arg_lookup
4505 tree name;
4506 tree namespaces;
4507 tree classes;
4508 tree functions;
4511 static int arg_assoc PROTO((struct arg_lookup*, tree));
4512 static int arg_assoc_args PROTO((struct arg_lookup*, tree));
4513 static int arg_assoc_type PROTO((struct arg_lookup*, tree));
4514 static int add_function PROTO((struct arg_lookup *, tree));
4515 static int arg_assoc_namespace PROTO((struct arg_lookup *, tree));
4516 static int arg_assoc_class PROTO((struct arg_lookup *, tree));
4517 static int arg_assoc_template_arg PROTO((struct arg_lookup*, tree));
4519 /* Add a function to the lookup structure.
4520 Returns 1 on error. */
4522 static int
4523 add_function (k, fn)
4524 struct arg_lookup *k;
4525 tree fn;
4527 /* We used to check here to see if the function was already in the list,
4528 but that's O(n^2), which is just too expensive for function lookup.
4529 Now we deal with the occasional duplicate in joust. In doing this, we
4530 assume that the number of duplicates will be small compared to the
4531 total number of functions being compared, which should usually be the
4532 case. */
4534 /* We must find only functions, or exactly one non-function. */
4535 if (k->functions && is_overloaded_fn (k->functions)
4536 && is_overloaded_fn (fn))
4537 k->functions = build_overload (fn, k->functions);
4538 else if (k->functions)
4540 tree f1 = OVL_CURRENT (k->functions);
4541 tree f2 = fn;
4542 if (is_overloaded_fn (f1))
4544 fn = f1; f1 = f2; f2 = fn;
4546 cp_error_at ("`%D' is not a function,", f1);
4547 cp_error_at (" conflict with `%D'", f2);
4548 cp_error (" in call to `%D'", k->name);
4549 return 1;
4551 else
4552 k->functions = fn;
4553 return 0;
4556 /* Add functions of a namespace to the lookup structure.
4557 Returns 1 on error. */
4559 static int
4560 arg_assoc_namespace (k, scope)
4561 struct arg_lookup *k;
4562 tree scope;
4564 tree value;
4566 if (purpose_member (scope, k->namespaces))
4567 return 0;
4568 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4570 value = namespace_binding (k->name, scope);
4571 if (!value)
4572 return 0;
4574 for (; value; value = OVL_NEXT (value))
4575 if (add_function (k, OVL_CURRENT (value)))
4576 return 1;
4578 return 0;
4581 /* Adds everything associated with a template argument to the lookup
4582 structure. Returns 1 on error. */
4584 static int
4585 arg_assoc_template_arg (k, arg)
4586 struct arg_lookup* k;
4587 tree arg;
4589 /* [basic.lookup.koenig]
4591 If T is a template-id, its associated namespaces and classes are
4592 ... the namespaces and classes associated with the types of the
4593 template arguments provided for template type parameters
4594 (excluding template template parameters); the namespaces in which
4595 any template template arguments are defined; and the classes in
4596 which any member templates used as template template arguments
4597 are defined. [Note: non-type template arguments do not
4598 contribute to the set of associated namespaces. ] */
4600 /* Consider first template template arguments. */
4601 if (TREE_CODE (arg) == TEMPLATE_DECL)
4603 tree ctx = CP_DECL_CONTEXT (arg);
4605 /* It's not a member template. */
4606 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4607 return arg_assoc_namespace (k, ctx);
4608 /* Otherwise, it must be member template. */
4609 else
4610 return arg_assoc_class (k, ctx);
4612 /* It's not a template template argument, but it is a type template
4613 argument. */
4614 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 't')
4615 return arg_assoc_type (k, arg);
4616 /* It's a non-type template argument. */
4617 else
4618 return 0;
4621 /* Adds everything associated with class to the lookup structure.
4622 Returns 1 on error. */
4624 static int
4625 arg_assoc_class (k, type)
4626 struct arg_lookup* k;
4627 tree type;
4629 tree list, friends, context;
4630 int i;
4632 /* Backend build structures, such as __builtin_va_list, aren't
4633 affected by all this. */
4634 if (!CLASS_TYPE_P (type))
4635 return 0;
4637 if (purpose_member (type, k->classes))
4638 return 0;
4639 k->classes = tree_cons (type, NULL_TREE, k->classes);
4641 context = decl_namespace (TYPE_MAIN_DECL (type));
4642 if (arg_assoc_namespace (k, context))
4643 return 1;
4645 /* Process baseclasses. */
4646 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4647 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4648 return 1;
4650 /* Process friends. */
4651 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4652 list = TREE_CHAIN (list))
4653 if (k->name == TREE_PURPOSE (list))
4654 for (friends = TREE_VALUE (list); friends;
4655 friends = TREE_CHAIN (friends))
4656 /* Only interested in global functions with potentially hidden
4657 (i.e. unqualified) declarations. */
4658 if (TREE_PURPOSE (list) == error_mark_node && TREE_VALUE (list)
4659 && decl_namespace (TREE_VALUE (list)) == context)
4660 if (add_function (k, TREE_VALUE (list)))
4661 return 1;
4663 /* Process template arguments. */
4664 if (CLASSTYPE_TEMPLATE_INFO (type))
4666 list = innermost_args (CLASSTYPE_TI_ARGS (type));
4667 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4668 arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
4671 return 0;
4674 /* Adds everything associated with a given type.
4675 Returns 1 on error. */
4677 static int
4678 arg_assoc_type (k, type)
4679 struct arg_lookup *k;
4680 tree type;
4682 switch (TREE_CODE (type))
4684 case VOID_TYPE:
4685 case INTEGER_TYPE:
4686 case REAL_TYPE:
4687 case COMPLEX_TYPE:
4688 case CHAR_TYPE:
4689 case BOOLEAN_TYPE:
4690 return 0;
4691 case RECORD_TYPE:
4692 if (TYPE_PTRMEMFUNC_P (type))
4693 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4694 return arg_assoc_class (k, type);
4695 case POINTER_TYPE:
4696 case REFERENCE_TYPE:
4697 case ARRAY_TYPE:
4698 return arg_assoc_type (k, TREE_TYPE (type));
4699 case UNION_TYPE:
4700 case ENUMERAL_TYPE:
4701 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4702 case OFFSET_TYPE:
4703 /* Pointer to member: associate class type and value type. */
4704 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4705 return 1;
4706 return arg_assoc_type (k, TREE_TYPE (type));
4707 case METHOD_TYPE:
4708 /* The basetype is referenced in the first arg type, so just
4709 fall through. */
4710 case FUNCTION_TYPE:
4711 /* Associate the parameter types. */
4712 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4713 return 1;
4714 /* Associate the return type. */
4715 return arg_assoc_type (k, TREE_TYPE (type));
4716 case TEMPLATE_TYPE_PARM:
4717 case TEMPLATE_TEMPLATE_PARM:
4718 return 0;
4719 case LANG_TYPE:
4720 if (type == unknown_type_node)
4721 return 0;
4722 /* else fall through */
4723 default:
4724 my_friendly_abort (390);
4726 return 0;
4729 /* Adds everything associated with arguments. Returns 1 on error. */
4731 static int
4732 arg_assoc_args (k, args)
4733 struct arg_lookup* k;
4734 tree args;
4736 for (; args; args = TREE_CHAIN (args))
4737 if (arg_assoc (k, TREE_VALUE (args)))
4738 return 1;
4739 return 0;
4742 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4744 static int
4745 arg_assoc (k, n)
4746 struct arg_lookup* k;
4747 tree n;
4749 if (n == error_mark_node)
4750 return 0;
4752 if (TREE_CODE_CLASS (TREE_CODE (n)) == 't')
4753 return arg_assoc_type (k, n);
4755 if (! type_unknown_p (n))
4756 return arg_assoc_type (k, TREE_TYPE (n));
4758 if (TREE_CODE (n) == ADDR_EXPR)
4759 n = TREE_OPERAND (n, 0);
4760 if (TREE_CODE (n) == COMPONENT_REF)
4761 n = TREE_OPERAND (n, 1);
4762 if (TREE_CODE (n) == OFFSET_REF)
4763 n = TREE_OPERAND (n, 1);
4764 while (TREE_CODE (n) == TREE_LIST)
4765 n = TREE_VALUE (n);
4767 if (TREE_CODE (n) == FUNCTION_DECL)
4768 return arg_assoc_type (k, TREE_TYPE (n));
4769 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4771 /* [basic.lookup.koenig]
4773 If T is a template-id, its associated namespaces and classes
4774 are the namespace in which the template is defined; for
4775 member templates, the member template's class... */
4776 tree template = TREE_OPERAND (n, 0);
4777 tree args = TREE_OPERAND (n, 1);
4778 tree ctx;
4779 tree arg;
4781 /* First, the template. There may actually be more than one if
4782 this is an overloaded function template. But, in that case,
4783 we only need the first; all the functions will be in the same
4784 namespace. */
4785 template = OVL_CURRENT (template);
4787 ctx = CP_DECL_CONTEXT (template);
4789 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4791 if (arg_assoc_namespace (k, ctx) == 1)
4792 return 1;
4794 /* It must be a member template. */
4795 else if (arg_assoc_class (k, ctx) == 1)
4796 return 1;
4798 /* Now the arguments. */
4799 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4800 if (arg_assoc_template_arg (k, TREE_VALUE (arg)) == 1)
4801 return 1;
4803 else
4805 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4807 for (; n; n = OVL_CHAIN (n))
4808 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4809 return 1;
4812 return 0;
4815 /* Performs Koenig lookup depending on arguments, where fns
4816 are the functions found in normal lookup. */
4818 tree
4819 lookup_arg_dependent (name, fns, args)
4820 tree name;
4821 tree fns;
4822 tree args;
4824 struct arg_lookup k;
4826 k.name = name;
4827 k.functions = fns;
4828 k.classes = NULL_TREE;
4830 /* Note that we've already looked at some namespaces during normal
4831 unqualified lookup, unless we found a decl in function scope. */
4832 if (fns && DECL_LOCAL_FUNCTION_P (OVL_CURRENT (fns)))
4833 k.namespaces = NULL_TREE;
4834 else
4835 unqualified_namespace_lookup (name, 0, &k.namespaces);
4837 arg_assoc_args (&k, args);
4838 return k.functions;
4841 /* Process a namespace-alias declaration. */
4843 void
4844 do_namespace_alias (alias, namespace)
4845 tree alias, namespace;
4847 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4849 /* The parser did not find it, so it's not there. */
4850 cp_error ("unknown namespace `%D'", namespace);
4851 return;
4854 namespace = ORIGINAL_NAMESPACE (namespace);
4856 /* Build the alias. */
4857 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4858 DECL_NAMESPACE_ALIAS (alias) = namespace;
4859 pushdecl (alias);
4862 /* Check a non-member using-declaration. Return the name and scope
4863 being used, and the USING_DECL, or NULL_TREE on failure. */
4865 static tree
4866 validate_nonmember_using_decl (decl, scope, name)
4867 tree decl;
4868 tree *scope;
4869 tree *name;
4871 if (TREE_CODE (decl) == SCOPE_REF
4872 && TREE_OPERAND (decl, 0) == std_node)
4874 if (namespace_bindings_p ()
4875 && current_namespace == global_namespace)
4876 /* There's no need for a using declaration at all, here,
4877 since `std' is the same as `::'. We can't just pass this
4878 on because we'll complain later about declaring something
4879 in the same scope as a using declaration with the same
4880 name. We return NULL_TREE which indicates to the caller
4881 that there's no need to do any further processing. */
4882 return NULL_TREE;
4884 *scope = global_namespace;
4885 *name = TREE_OPERAND (decl, 1);
4887 else if (TREE_CODE (decl) == SCOPE_REF)
4889 *scope = TREE_OPERAND (decl, 0);
4890 *name = TREE_OPERAND (decl, 1);
4892 /* [namespace.udecl]
4894 A using-declaration for a class member shall be a
4895 member-declaration. */
4896 if (TREE_CODE (*scope) != NAMESPACE_DECL)
4898 if (TYPE_P (*scope))
4899 cp_error ("`%T' is not a namespace", *scope);
4900 else
4901 cp_error ("`%D' is not a namespace", *scope);
4902 return NULL_TREE;
4905 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4906 || TREE_CODE (decl) == TYPE_DECL
4907 || TREE_CODE (decl) == TEMPLATE_DECL)
4909 *scope = global_namespace;
4910 *name = decl;
4912 else
4913 my_friendly_abort (382);
4914 if (TREE_CODE_CLASS (TREE_CODE (*name)) == 'd')
4915 *name = DECL_NAME (*name);
4916 /* Make a USING_DECL. */
4917 return push_using_decl (*scope, *name);
4920 /* Process local and global using-declarations. */
4922 static void
4923 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4924 tree scope, name;
4925 tree oldval, oldtype;
4926 tree *newval, *newtype;
4928 tree decls;
4930 *newval = *newtype = NULL_TREE;
4931 decls = make_node (CPLUS_BINDING);
4932 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4933 /* Lookup error */
4934 return;
4936 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4938 cp_error ("`%D' not declared", name);
4939 return;
4942 /* Check for using functions. */
4943 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4945 tree tmp, tmp1;
4947 if (oldval && !is_overloaded_fn (oldval))
4949 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4950 oldval = NULL_TREE;
4953 *newval = oldval;
4954 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4956 tree new_fn = OVL_CURRENT (tmp);
4958 /* [namespace.udecl]
4960 If a function declaration in namespace scope or block
4961 scope has the same name and the same parameter types as a
4962 function introduced by a using declaration the program is
4963 ill-formed. */
4964 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4966 tree old_fn = OVL_CURRENT (tmp1);
4968 if (!OVL_USED (tmp1)
4969 && compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4970 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4972 /* There was already a non-using declaration in
4973 this scope with the same parameter types. */
4974 cp_error ("`%D' is already declared in this scope",
4975 name);
4976 break;
4978 else if (duplicate_decls (new_fn, old_fn))
4979 /* We're re-using something we already used
4980 before. We don't need to add it again. */
4981 break;
4984 /* If we broke out of the loop, there's no reason to add
4985 this function to the using declarations for this
4986 scope. */
4987 if (tmp1)
4988 continue;
4990 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4991 if (TREE_CODE (*newval) != OVERLOAD)
4992 *newval = ovl_cons (*newval, NULL_TREE);
4993 OVL_USED (*newval) = 1;
4996 else
4998 *newval = BINDING_VALUE (decls);
4999 if (oldval)
5000 duplicate_decls (*newval, oldval);
5003 *newtype = BINDING_TYPE (decls);
5004 if (oldtype && *newtype && oldtype != *newtype)
5006 cp_error ("using directive `%D' introduced ambiguous type `%T'",
5007 name, oldtype);
5008 return;
5012 /* Process a using-declaration not appearing in class or local scope. */
5014 void
5015 do_toplevel_using_decl (decl)
5016 tree decl;
5018 tree scope, name, binding;
5019 tree oldval, oldtype, newval, newtype;
5021 decl = validate_nonmember_using_decl (decl, &scope, &name);
5022 if (decl == NULL_TREE)
5023 return;
5025 binding = binding_for_name (name, current_namespace);
5027 oldval = BINDING_VALUE (binding);
5028 oldtype = BINDING_TYPE (binding);
5030 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5032 /* Copy declarations found. */
5033 if (newval)
5034 BINDING_VALUE (binding) = newval;
5035 if (newtype)
5036 BINDING_TYPE (binding) = newtype;
5037 return;
5040 /* Process a using-declaration at function scope. */
5042 void
5043 do_local_using_decl (decl)
5044 tree decl;
5046 tree scope, name;
5047 tree oldval, oldtype, newval, newtype;
5049 decl = validate_nonmember_using_decl (decl, &scope, &name);
5050 if (decl == NULL_TREE)
5051 return;
5053 oldval = lookup_name_current_level (name);
5054 oldtype = lookup_type_current_level (name);
5056 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
5058 if (newval)
5060 if (is_overloaded_fn (newval))
5062 tree fn, term;
5064 /* We only need to push declarations for those functions
5065 that were not already bound in the current level.
5066 The old value might be NULL_TREE, it might be a single
5067 function, or an OVERLOAD. */
5068 if (oldval && TREE_CODE (oldval) == OVERLOAD)
5069 term = OVL_FUNCTION (oldval);
5070 else
5071 term = oldval;
5072 for (fn = newval; fn && OVL_CURRENT (fn) != term;
5073 fn = OVL_NEXT (fn))
5074 push_overloaded_decl (OVL_CURRENT (fn),
5075 PUSH_LOCAL | PUSH_USING);
5077 else
5078 push_local_binding (name, newval, PUSH_USING);
5080 if (newtype)
5081 set_identifier_type_value (name, newtype);
5084 tree
5085 do_class_using_decl (decl)
5086 tree decl;
5088 tree name, value;
5090 if (TREE_CODE (decl) != SCOPE_REF
5091 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (decl, 0))) != 't')
5093 cp_error ("using-declaration for non-member at class scope");
5094 return NULL_TREE;
5096 name = TREE_OPERAND (decl, 1);
5097 if (TREE_CODE (name) == BIT_NOT_EXPR)
5099 cp_error ("using-declaration for destructor");
5100 return NULL_TREE;
5102 if (TREE_CODE (name) == TYPE_DECL)
5103 name = DECL_NAME (name);
5105 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
5107 value = build_lang_decl (USING_DECL, name, void_type_node);
5108 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
5109 return value;
5112 /* Process a using-directive. */
5114 void
5115 do_using_directive (namespace)
5116 tree namespace;
5118 if (namespace == std_node)
5119 return;
5120 /* using namespace A::B::C; */
5121 if (TREE_CODE (namespace) == SCOPE_REF)
5122 namespace = TREE_OPERAND (namespace, 1);
5123 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
5125 /* Lookup in lexer did not find a namespace. */
5126 cp_error ("namespace `%T' undeclared", namespace);
5127 return;
5129 if (TREE_CODE (namespace) != NAMESPACE_DECL)
5131 cp_error ("`%T' is not a namespace", namespace);
5132 return;
5134 namespace = ORIGINAL_NAMESPACE (namespace);
5135 if (!toplevel_bindings_p ())
5136 push_using_directive (namespace);
5137 else
5138 /* direct usage */
5139 add_using_namespace (current_namespace, namespace, 0);
5142 void
5143 check_default_args (x)
5144 tree x;
5146 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
5147 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
5148 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
5150 if (TREE_PURPOSE (arg))
5151 saw_def = 1;
5152 else if (saw_def)
5154 cp_error_at ("default argument missing for parameter %P of `%+#D'",
5155 i, x);
5156 break;
5161 void
5162 mark_used (decl)
5163 tree decl;
5165 TREE_USED (decl) = 1;
5166 if (processing_template_decl)
5167 return;
5168 assemble_external (decl);
5170 /* Is it a synthesized method that needs to be synthesized? */
5171 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CLASS_CONTEXT (decl)
5172 && DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
5173 /* Kludge: don't synthesize for default args. */
5174 && current_function_decl)
5175 synthesize_method (decl);
5177 /* If this is a function or variable that is an instance of some
5178 template, we now know that we will need to actually do the
5179 instantiation. We check that DECL is not an explicit
5180 instantiation because that is not checked in instantiate_decl. */
5181 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
5182 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5183 && !DECL_EXPLICIT_INSTANTIATION (decl))
5184 instantiate_decl (decl);
5187 /* Helper function for named_class_head_sans_basetype nonterminal. We
5188 have just seen something of the form `AGGR SCOPE::ID'. Return a
5189 TYPE_DECL for the type declared by ID in SCOPE. */
5191 tree
5192 handle_class_head (aggr, scope, id)
5193 tree aggr, scope, id;
5195 tree decl;
5197 if (TREE_CODE (id) == TYPE_DECL)
5198 decl = id;
5199 else if (DECL_CLASS_TEMPLATE_P (id))
5200 decl = DECL_TEMPLATE_RESULT (id);
5201 else
5203 tree current = current_scope();
5205 if (current == NULL_TREE)
5206 current = current_namespace;
5207 if (scope == std_node)
5208 scope = global_namespace;
5209 if (scope == NULL_TREE)
5210 scope = global_namespace;
5211 if (scope == current)
5213 /* We've been given AGGR SCOPE::ID, when we're already inside SCOPE.
5214 Be nice about it. */
5215 if (pedantic)
5216 cp_pedwarn ("extra qualification `%T::' on member `%D' ignored",
5217 FROB_CONTEXT (scope), id);
5219 else if (scope != global_namespace)
5220 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5221 else
5222 cp_error ("no file-scope type named `%D'", id);
5224 /* Inject it at the current scope. */
5225 decl = TYPE_MAIN_DECL (xref_tag (aggr, id, 1));
5228 /* Enter the SCOPE. If this turns out not to be a definition, the
5229 parser must leave the scope. */
5230 push_scope (CP_DECL_CONTEXT (decl));
5232 /* If we see something like:
5234 template <typename T> struct S::I ....
5236 we must create a TEMPLATE_DECL for the nested type. */
5237 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
5238 decl = push_template_decl (decl);
5240 return decl;
5243 /* Initialize decl2.c. */
5245 void
5246 init_decl2 ()
5248 ggc_add_tree_root (&decl_namespace_list, 1);
5249 ggc_add_tree_varray_root (&saved_inlines, 1);
5250 ggc_add_tree_varray_root (&pending_statics, 1);
5251 ggc_add_tree_varray_root (&ssdf_decls, 1);
5252 ggc_add_tree_root (&ssdf_decl, 1);
5253 ggc_add_tree_root (&priority_decl, 1);
5254 ggc_add_tree_root (&initialize_p_decl, 1);
5255 ggc_add_tree_root (&pending_vtables, 1);