* friend.c (do_friend): Pull the identifier out of declarator.
[official-gcc.git] / gcc / cp / decl2.c
blobccf00f58485bc6eb572e311d957b3423ed2fb978
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92, 93, 94, 95, 1996 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"
46 #if USE_CPPLIB
47 #include "cpplib.h"
48 extern cpp_reader parse_in;
49 extern cpp_options parse_options;
50 static int cpp_initialized;
51 #endif
53 static tree get_sentry PROTO((tree));
54 static void mark_vtable_entries PROTO((tree));
55 static void import_export_template PROTO((tree));
56 static void grok_function_init PROTO((tree, tree));
57 static int finish_vtable_vardecl PROTO((tree, tree));
58 static int prune_vtable_vardecl PROTO((tree, tree));
59 static void finish_sigtable_vardecl PROTO((tree, tree));
60 static int is_namespace_ancestor PROTO((tree, tree));
61 static tree namespace_ancestor PROTO((tree, tree));
62 static void add_using_namespace PROTO((tree, tree, int));
63 static tree ambiguous_decl PROTO((tree, tree, tree));
64 static tree build_anon_union_vars PROTO((tree, tree*, int, int));
66 extern int current_class_depth;
68 /* A list of virtual function tables we must make sure to write out. */
69 tree pending_vtables;
71 /* A list of static class variables. This is needed, because a
72 static class variable can be declared inside the class without
73 an initializer, and then initialized, staticly, outside the class. */
74 tree pending_statics;
76 /* A list of functions which were declared inline, but which we
77 may need to emit outline anyway. */
78 static tree saved_inlines;
80 /* Used to help generate temporary names which are unique within
81 a function. Reset to 0 by start_function. */
83 int temp_name_counter;
85 /* Same, but not reset. Local temp variables and global temp variables
86 can have the same name. */
87 static int global_temp_name_counter;
89 /* Flag used when debugging spew.c */
91 extern int spew_debug;
93 /* Nonzero if we're done parsing and into end-of-file activities. */
95 int at_eof;
97 /* Functions called along with real static constructors and destructors. */
99 tree static_ctors, static_dtors;
101 /* The current open namespace, and ::. */
103 tree current_namespace;
104 tree global_namespace;
106 /* The stack for namespaces of current declarations. */
108 static tree decl_namespace_list;
111 /* C (and C++) language-specific option variables. */
113 /* Nonzero means allow type mismatches in conditional expressions;
114 just make their values `void'. */
116 int flag_cond_mismatch;
118 /* Nonzero means give `double' the same size as `float'. */
120 int flag_short_double;
122 /* Nonzero means don't recognize the keyword `asm'. */
124 int flag_no_asm;
126 /* Nonzero means don't recognize any extension keywords. */
128 int flag_no_gnu_keywords;
130 /* Nonzero means don't recognize the non-ANSI builtin functions. */
132 int flag_no_builtin;
134 /* Nonzero means don't recognize the non-ANSI builtin functions.
135 -ansi sets this. */
137 int flag_no_nonansi_builtin;
139 /* Nonzero means do some things the same way PCC does. Only provided so
140 the compiler will link. */
142 int flag_traditional;
144 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
146 int flag_signed_bitfields = 1;
148 /* Nonzero means handle `#ident' directives. 0 means ignore them. */
150 int flag_no_ident;
152 /* Nonzero means enable obscure ANSI features and disable GNU extensions
153 that might cause ANSI-compliant code to be miscompiled. */
155 int flag_ansi;
157 /* Nonzero means do emit exported implementations of functions even if
158 they can be inlined. */
160 int flag_implement_inlines = 1;
162 /* Nonzero means do emit exported implementations of templates, instead of
163 multiple static copies in each file that needs a definition. */
165 int flag_external_templates;
167 /* Nonzero means that the decision to emit or not emit the implementation of a
168 template depends on where the template is instantiated, rather than where
169 it is defined. */
171 int flag_alt_external_templates;
173 /* Nonzero means that implicit instantiations will be emitted if needed. */
175 int flag_implicit_templates = 1;
177 /* Nonzero means warn about implicit declarations. */
179 int warn_implicit = 1;
181 /* Nonzero means warn about usage of long long when `-pedantic'. */
183 int warn_long_long = 1;
185 /* Nonzero means warn when all ctors or dtors are private, and the class
186 has no friends. */
188 int warn_ctor_dtor_privacy = 1;
190 /* True if we want to implement vtables using "thunks".
191 The default is off. */
193 #ifndef DEFAULT_VTABLE_THUNKS
194 #define DEFAULT_VTABLE_THUNKS 0
195 #endif
196 int flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
198 /* True if we want to deal with repository information. */
200 int flag_use_repository;
202 /* Nonzero if we want to issue diagnostics that the standard says are not
203 required. */
205 int flag_optional_diags = 1;
207 /* Nonzero means give string constants the type `const char *'
208 to get extra warnings from them. These warnings will be too numerous
209 to be useful, except in thoroughly ANSIfied programs. */
211 int warn_write_strings;
213 /* Nonzero means warn about pointer casts that can drop a type qualifier
214 from the pointer target type. */
216 int warn_cast_qual;
218 /* Nonzero means warn about sizeof(function) or addition/subtraction
219 of function pointers. */
221 int warn_pointer_arith = 1;
223 /* Nonzero means warn for any function def without prototype decl. */
225 int warn_missing_prototypes;
227 /* Nonzero means warn about multiple (redundant) decls for the same single
228 variable or function. */
230 int warn_redundant_decls;
232 /* Warn if initializer is not completely bracketed. */
234 int warn_missing_braces;
236 /* Warn about comparison of signed and unsigned values. */
238 int warn_sign_compare;
240 /* Warn about *printf or *scanf format/argument anomalies. */
242 int warn_format;
244 /* Warn about a subscript that has type char. */
246 int warn_char_subscripts;
248 /* Warn if a type conversion is done that might have confusing results. */
250 int warn_conversion;
252 /* Warn if adding () is suggested. */
254 int warn_parentheses;
256 /* Non-zero means warn in function declared in derived class has the
257 same name as a virtual in the base class, but fails to match the
258 type signature of any virtual function in the base class. */
259 int warn_overloaded_virtual;
261 /* Non-zero means warn when declaring a class that has a non virtual
262 destructor, when it really ought to have a virtual one. */
263 int warn_nonvdtor;
265 /* Non-zero means warn when a function is declared extern and later inline. */
266 int warn_extern_inline;
268 /* Non-zero means warn when the compiler will reorder code. */
269 int warn_reorder;
271 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
272 int warn_synth;
274 /* Non-zero means warn when we convert a pointer to member function
275 into a pointer to (void or function). */
276 int warn_pmf2ptr = 1;
278 /* Nonzero means warn about violation of some Effective C++ style rules. */
280 int warn_ecpp;
282 /* Nonzero means warn where overload resolution chooses a promotion from
283 unsigned to signed over a conversion to an unsigned of the same size. */
285 int warn_sign_promo;
287 /* Nonzero means warn when an old-style cast is used. */
289 int warn_old_style_cast;
291 /* Warn about #pragma directives that are not recognised. */
293 int warn_unknown_pragmas; /* Tri state variable. */
295 /* Nonzero means warn about use of multicharacter literals. */
297 int warn_multichar = 1;
299 /* Nonzero means `$' can be in an identifier. */
301 #ifndef DOLLARS_IN_IDENTIFIERS
302 #define DOLLARS_IN_IDENTIFIERS 1
303 #endif
304 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
306 /* Nonzero for -fno-strict-prototype switch: do not consider empty
307 argument prototype to mean function takes no arguments. */
309 int flag_strict_prototype = 2;
310 int strict_prototype = 1;
311 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
313 /* Nonzero means that labels can be used as first-class objects */
315 int flag_labels_ok;
317 /* Non-zero means to collect statistics which might be expensive
318 and to print them when we are done. */
319 int flag_detailed_statistics;
321 /* C++ specific flags. */
322 /* Nonzero for -fall-virtual: make every member function (except
323 constructors) lay down in the virtual function table. Calls
324 can then either go through the virtual function table or not,
325 depending. */
327 int flag_all_virtual;
329 /* Zero means that `this' is a *const. This gives nice behavior in the
330 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
331 -2 means we're constructing an object and it has fixed type. */
333 int flag_this_is_variable;
335 /* Nonzero means memoize our member lookups. */
337 int flag_memoize_lookups; int flag_save_memoized_contexts;
339 /* 3 means write out only virtuals function tables `defined'
340 in this implementation file.
341 2 means write out only specific virtual function tables
342 and give them (C) public access.
343 1 means write out virtual function tables and give them
344 (C) public access.
345 0 means write out virtual function tables and give them
346 (C) static access (default).
347 -1 means declare virtual function tables extern. */
349 int write_virtuals;
351 /* Nonzero means we should attempt to elide constructors when possible.
352 FIXME: This flag is obsolete, and should be torn out along with the
353 old overloading code. */
355 int flag_elide_constructors;
357 /* Nonzero means recognize and handle signature language constructs. */
359 int flag_handle_signatures;
361 /* Nonzero means that member functions defined in class scope are
362 inline by default. */
364 int flag_default_inline = 1;
366 /* Controls whether enums and ints freely convert.
367 1 means with complete freedom.
368 0 means enums can convert to ints, but not vice-versa. */
369 int flag_int_enum_equivalence;
371 /* Controls whether compiler generates 'type descriptor' that give
372 run-time type information. */
373 int flag_rtti = 1;
375 /* Nonzero if we wish to output cross-referencing information
376 for the GNU class browser. */
377 extern int flag_gnu_xref;
379 /* Nonzero if compiler can make `reasonable' assumptions about
380 references and objects. For example, the compiler must be
381 conservative about the following and not assume that `a' is nonnull:
383 obj &a = g ();
384 a.f (2);
386 In general, it is `reasonable' to assume that for many programs,
387 and better code can be generated in that case. */
389 int flag_assume_nonnull_objects = 1;
391 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
392 objects. */
394 int flag_huge_objects;
396 /* Nonzero if we want to conserve space in the .o files. We do this
397 by putting uninitialized data and runtime initialized data into
398 .common instead of .data at the expense of not flagging multiple
399 definitions. */
401 int flag_conserve_space;
403 /* Nonzero if we want to obey access control semantics. */
405 int flag_access_control = 1;
407 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
409 int flag_operator_names;
411 /* Nonzero if we want to check the return value of new and avoid calling
412 constructors if it is a null pointer. */
414 int flag_check_new;
416 /* Nonzero if we want the new ANSI rules for pushing a new scope for `for'
417 initialization variables.
418 0: Old rules, set by -fno-for-scope.
419 2: New ANSI rules, set by -ffor-scope.
420 1: Try to implement new ANSI rules, but with backup compatibility
421 (and warnings). This is the default, for now. */
423 int flag_new_for_scope = 1;
425 /* Nonzero if we want to emit defined symbols with common-like linkage as
426 weak symbols where possible, in order to conform to C++ semantics.
427 Otherwise, emit them as local symbols. */
429 int flag_weak = 1;
431 /* Nonzero to enable experimental ABI changes. */
433 int flag_new_abi;
435 /* Nonzero to not ignore namespace std. */
437 int flag_honor_std;
439 /* Maximum template instantiation depth. Must be at least 17 for ANSI
440 compliance. */
442 int max_tinst_depth = 17;
444 /* The name-mangling scheme to use. Must be 1 or greater to support
445 template functions with identical types, but different template
446 arguments. */
447 int name_mangling_version = 2;
449 /* Nonzero means that guiding declarations are allowed. */
450 int flag_guiding_decls;
452 /* Nonzero if squashed mangling is to be performed.
453 This uses the B and K codes to reference previously seen class types
454 and class qualifiers. */
455 int flag_do_squangling;
458 /* Table of language-dependent -f options.
459 STRING is the option name. VARIABLE is the address of the variable.
460 ON_VALUE is the value to store in VARIABLE
461 if `-fSTRING' is seen as an option.
462 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
464 static struct { char *string; int *variable; int on_value;} lang_f_options[] =
466 {"signed-char", &flag_signed_char, 1},
467 {"unsigned-char", &flag_signed_char, 0},
468 {"signed-bitfields", &flag_signed_bitfields, 1},
469 {"unsigned-bitfields", &flag_signed_bitfields, 0},
470 {"short-enums", &flag_short_enums, 1},
471 {"short-double", &flag_short_double, 1},
472 {"cond-mismatch", &flag_cond_mismatch, 1},
473 {"squangle", &flag_do_squangling, 1},
474 {"asm", &flag_no_asm, 0},
475 {"builtin", &flag_no_builtin, 0},
476 {"ident", &flag_no_ident, 0},
477 {"labels-ok", &flag_labels_ok, 1},
478 {"stats", &flag_detailed_statistics, 1},
479 {"this-is-variable", &flag_this_is_variable, 1},
480 {"strict-prototype", &flag_strict_prototype, 1},
481 {"all-virtual", &flag_all_virtual, 1},
482 {"memoize-lookups", &flag_memoize_lookups, 1},
483 {"elide-constructors", &flag_elide_constructors, 1},
484 {"handle-exceptions", &flag_exceptions, 1},
485 {"handle-signatures", &flag_handle_signatures, 1},
486 {"default-inline", &flag_default_inline, 1},
487 {"dollars-in-identifiers", &dollars_in_ident, 1},
488 {"enum-int-equiv", &flag_int_enum_equivalence, 1},
489 {"honor-std", &flag_honor_std, 1},
490 {"rtti", &flag_rtti, 1},
491 {"xref", &flag_gnu_xref, 1},
492 {"nonnull-objects", &flag_assume_nonnull_objects, 1},
493 {"implement-inlines", &flag_implement_inlines, 1},
494 {"external-templates", &flag_external_templates, 1},
495 {"implicit-templates", &flag_implicit_templates, 1},
496 {"huge-objects", &flag_huge_objects, 1},
497 {"conserve-space", &flag_conserve_space, 1},
498 {"vtable-thunks", &flag_vtable_thunks, 1},
499 {"access-control", &flag_access_control, 1},
500 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
501 {"gnu-keywords", &flag_no_gnu_keywords, 0},
502 {"operator-names", &flag_operator_names, 1},
503 {"optional-diags", &flag_optional_diags, 1},
504 {"check-new", &flag_check_new, 1},
505 {"repo", &flag_use_repository, 1},
506 {"for-scope", &flag_new_for_scope, 2},
507 {"weak", &flag_weak, 1}
510 /* Decode the string P as a language-specific option.
511 Return the number of strings consumed for a valid option.
512 Otherwise return 0. */
514 int
515 lang_decode_option (argc, argv)
516 int argc;
517 char **argv;
520 int strings_processed;
521 char *p = argv[0];
522 #if USE_CPPLIB
523 if (! cpp_initialized)
525 cpp_reader_init (&parse_in);
526 parse_in.data = &parse_options;
527 cpp_options_init (&parse_options);
528 cpp_initialized = 1;
530 strings_processed = cpp_handle_option (&parse_in, argc, argv);
531 #else
532 strings_processed = 0;
533 #endif /* ! USE_CPPLIB */
535 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
536 flag_writable_strings = 1,
537 flag_this_is_variable = 1, flag_new_for_scope = 0;
538 /* The +e options are for cfront compatibility. They come in as
539 `-+eN', to kludge around gcc.c's argument handling. */
540 else if (p[0] == '-' && p[1] == '+' && p[2] == 'e')
542 int old_write_virtuals = write_virtuals;
543 if (p[3] == '1')
544 write_virtuals = 1;
545 else if (p[3] == '0')
546 write_virtuals = -1;
547 else if (p[3] == '2')
548 write_virtuals = 2;
549 else error ("invalid +e option");
550 if (old_write_virtuals != 0
551 && write_virtuals != old_write_virtuals)
552 error ("conflicting +e options given");
554 else if (p[0] == '-' && p[1] == 'f')
556 /* Some kind of -f option.
557 P's value is the option sans `-f'.
558 Search for it in the table of options. */
559 int found = 0;
560 size_t j;
562 p += 2;
563 /* Try special -f options. */
565 if (!strcmp (p, "handle-exceptions")
566 || !strcmp (p, "no-handle-exceptions"))
567 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
569 if (!strcmp (p, "save-memoized"))
571 flag_memoize_lookups = 1;
572 flag_save_memoized_contexts = 1;
573 found = 1;
575 else if (!strcmp (p, "no-save-memoized"))
577 flag_memoize_lookups = 0;
578 flag_save_memoized_contexts = 0;
579 found = 1;
581 else if (! strcmp (p, "alt-external-templates"))
583 flag_external_templates = 1;
584 flag_alt_external_templates = 1;
585 found = 1;
587 else if (! strcmp (p, "no-alt-external-templates"))
589 flag_alt_external_templates = 0;
590 found = 1;
592 else if (!strcmp (p, "repo"))
594 flag_use_repository = 1;
595 flag_implicit_templates = 0;
596 found = 1;
598 else if (!strcmp (p, "guiding-decls"))
600 flag_guiding_decls = 1;
601 name_mangling_version = 0;
602 found = 1;
604 else if (!strcmp (p, "no-guiding-decls"))
606 flag_guiding_decls = 0;
607 found = 1;
609 else if (!strcmp (p, "ansi-overloading"))
610 found = 1;
611 else if (!strcmp (p, "no-ansi-overloading"))
613 error ("-fno-ansi-overloading is no longer supported");
614 found = 1;
616 else if (!strcmp (p, "new-abi"))
618 flag_new_abi = 1;
619 flag_do_squangling = 1;
620 flag_honor_std = 1;
621 flag_vtable_thunks = 1;
623 else if (!strcmp (p, "no-new-abi"))
625 flag_new_abi = 0;
626 flag_do_squangling = 0;
627 flag_honor_std = 0;
629 else if (!strncmp (p, "template-depth-", 15))
631 char *endp = p + 15;
632 while (*endp)
634 if (*endp >= '0' && *endp <= '9')
635 endp++;
636 else
638 error ("Invalid option `%s'", p - 2);
639 goto template_depth_lose;
642 max_tinst_depth = atoi (p + 15);
643 template_depth_lose: ;
645 else if (!strncmp (p, "name-mangling-version-", 22))
647 char *endp = p + 22;
648 while (*endp)
650 if (*endp >= '0' && *endp <= '9')
651 endp++;
652 else
654 error ("Invalid option `%s'", p - 2);
655 goto mangling_version_lose;
658 name_mangling_version = atoi (p + 22);
659 mangling_version_lose: ;
661 else for (j = 0;
662 !found && j < sizeof (lang_f_options) / sizeof (lang_f_options[0]);
663 j++)
665 if (!strcmp (p, lang_f_options[j].string))
667 *lang_f_options[j].variable = lang_f_options[j].on_value;
668 /* A goto here would be cleaner,
669 but breaks the vax pcc. */
670 found = 1;
672 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
673 && ! strcmp (p+3, lang_f_options[j].string))
675 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
676 found = 1;
679 return found;
681 else if (p[0] == '-' && p[1] == 'W')
683 int setting = 1;
685 /* The -W options control the warning behavior of the compiler. */
686 p += 2;
688 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
689 setting = 0, p += 3;
691 if (!strcmp (p, "implicit"))
692 warn_implicit = setting;
693 else if (!strcmp (p, "long-long"))
694 warn_long_long = setting;
695 else if (!strcmp (p, "return-type"))
696 warn_return_type = setting;
697 else if (!strcmp (p, "ctor-dtor-privacy"))
698 warn_ctor_dtor_privacy = setting;
699 else if (!strcmp (p, "write-strings"))
700 warn_write_strings = setting;
701 else if (!strcmp (p, "cast-qual"))
702 warn_cast_qual = setting;
703 else if (!strcmp (p, "char-subscripts"))
704 warn_char_subscripts = setting;
705 else if (!strcmp (p, "pointer-arith"))
706 warn_pointer_arith = setting;
707 else if (!strcmp (p, "missing-prototypes"))
708 warn_missing_prototypes = setting;
709 else if (!strcmp (p, "redundant-decls"))
710 warn_redundant_decls = setting;
711 else if (!strcmp (p, "missing-braces"))
712 warn_missing_braces = setting;
713 else if (!strcmp (p, "sign-compare"))
714 warn_sign_compare = setting;
715 else if (!strcmp (p, "format"))
716 warn_format = setting;
717 else if (!strcmp (p, "conversion"))
718 warn_conversion = setting;
719 else if (!strcmp (p, "parentheses"))
720 warn_parentheses = setting;
721 else if (!strcmp (p, "non-virtual-dtor"))
722 warn_nonvdtor = setting;
723 else if (!strcmp (p, "extern-inline"))
724 warn_extern_inline = setting;
725 else if (!strcmp (p, "reorder"))
726 warn_reorder = setting;
727 else if (!strcmp (p, "synth"))
728 warn_synth = setting;
729 else if (!strcmp (p, "pmf-conversions"))
730 warn_pmf2ptr = setting;
731 else if (!strcmp (p, "effc++"))
732 warn_ecpp = setting;
733 else if (!strcmp (p, "sign-promo"))
734 warn_sign_promo = setting;
735 else if (!strcmp (p, "old-style-cast"))
736 warn_old_style_cast = setting;
737 else if (!strcmp (p, "overloaded-virtual"))
738 warn_overloaded_virtual = setting;
739 else if (!strcmp (p, "multichar"))
740 warn_multichar = setting;
741 else if (!strcmp (p, "unknown-pragmas"))
742 /* Set to greater than 1, so that even unknown pragmas in
743 system headers will be warned about. */
744 warn_unknown_pragmas = setting * 2;
745 else if (!strcmp (p, "comment"))
746 ; /* cpp handles this one. */
747 else if (!strcmp (p, "comments"))
748 ; /* cpp handles this one. */
749 else if (!strcmp (p, "trigraphs"))
750 ; /* cpp handles this one. */
751 else if (!strcmp (p, "import"))
752 ; /* cpp handles this one. */
753 else if (!strcmp (p, "all"))
755 warn_return_type = setting;
756 warn_unused = setting;
757 warn_implicit = setting;
758 warn_ctor_dtor_privacy = setting;
759 warn_switch = setting;
760 warn_format = setting;
761 warn_parentheses = setting;
762 warn_missing_braces = setting;
763 warn_sign_compare = setting;
764 warn_extern_inline = setting;
765 warn_nonvdtor = setting;
766 warn_multichar = setting;
767 /* We save the value of warn_uninitialized, since if they put
768 -Wuninitialized on the command line, we need to generate a
769 warning about not using it without also specifying -O. */
770 if (warn_uninitialized != 1)
771 warn_uninitialized = (setting ? 2 : 0);
772 warn_reorder = setting;
773 warn_sign_promo = setting;
774 /* Only warn about unknown pragmas that are not in system
775 headers. */
776 warn_unknown_pragmas = 1;
778 else return strings_processed;
780 else if (!strcmp (p, "-ansi"))
781 flag_no_nonansi_builtin = 1, flag_ansi = 1,
782 flag_no_gnu_keywords = 1, flag_operator_names = 1;
783 #ifdef SPEW_DEBUG
784 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
785 it's probably safe to assume no sane person would ever want to use this
786 under normal circumstances. */
787 else if (!strcmp (p, "-spew-debug"))
788 spew_debug = 1;
789 #endif
790 else
791 return strings_processed;
793 return 1;
796 /* Incorporate `const' and `volatile' qualifiers for member functions.
797 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
798 QUALS is a list of qualifiers. */
800 tree
801 grok_method_quals (ctype, function, quals)
802 tree ctype, function, quals;
804 tree fntype = TREE_TYPE (function);
805 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
809 extern tree ridpointers[];
811 if (TREE_VALUE (quals) == ridpointers[(int)RID_CONST])
813 if (TYPE_READONLY (ctype))
814 error ("duplicate `%s' %s",
815 IDENTIFIER_POINTER (TREE_VALUE (quals)),
816 (TREE_CODE (function) == FUNCTION_DECL
817 ? "for member function" : "in type declaration"));
818 ctype = build_type_variant (ctype, 1, TYPE_VOLATILE (ctype));
819 build_pointer_type (ctype);
821 else if (TREE_VALUE (quals) == ridpointers[(int)RID_VOLATILE])
823 if (TYPE_VOLATILE (ctype))
824 error ("duplicate `%s' %s",
825 IDENTIFIER_POINTER (TREE_VALUE (quals)),
826 (TREE_CODE (function) == FUNCTION_DECL
827 ? "for member function" : "in type declaration"));
828 ctype = build_type_variant (ctype, TYPE_READONLY (ctype), 1);
829 build_pointer_type (ctype);
831 else
832 my_friendly_abort (20);
833 quals = TREE_CHAIN (quals);
835 while (quals);
836 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
837 (TREE_CODE (fntype) == METHOD_TYPE
838 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
839 : TYPE_ARG_TYPES (fntype)));
840 if (raises)
841 fntype = build_exception_variant (fntype, raises);
843 TREE_TYPE (function) = fntype;
844 return ctype;
847 /* Warn when -fexternal-templates is used and #pragma
848 interface/implementation is not used all the times it should be,
849 inform the user. */
851 void
852 warn_if_unknown_interface (decl)
853 tree decl;
855 static int already_warned = 0;
856 if (already_warned++)
857 return;
859 if (flag_alt_external_templates)
861 struct tinst_level *til = tinst_for_decl ();
862 int sl = lineno;
863 char *sf = input_filename;
865 if (til)
867 lineno = til->line;
868 input_filename = til->file;
870 cp_warning ("template `%#D' instantiated in file without #pragma interface",
871 decl);
872 lineno = sl;
873 input_filename = sf;
875 else
876 cp_warning_at ("template `%#D' defined in file without #pragma interface",
877 decl);
880 /* A subroutine of the parser, to handle a component list. */
882 tree
883 grok_x_components (specs, components)
884 tree specs, components;
886 register tree t, x, tcode;
888 /* We just got some friends. They have been recorded elsewhere. */
889 if (components == void_type_node)
890 return NULL_TREE;
892 if (components == NULL_TREE)
894 t = groktypename (build_decl_list (specs, NULL_TREE));
896 if (t == NULL_TREE)
898 error ("error in component specification");
899 return NULL_TREE;
902 switch (TREE_CODE (t))
904 case VAR_DECL:
905 /* Static anonymous unions come out as VAR_DECLs. */
906 if (ANON_UNION_TYPE_P (TREE_TYPE (t)))
907 return t;
909 /* We return SPECS here, because in the parser it was ending
910 up with not doing anything to $$, which is what SPECS
911 represents. */
912 return specs;
913 break;
915 case RECORD_TYPE:
916 /* This code may be needed for UNION_TYPEs as
917 well. */
918 tcode = record_type_node;
919 if (CLASSTYPE_DECLARED_CLASS (t))
920 tcode = class_type_node;
921 else if (IS_SIGNATURE (t))
922 tcode = signature_type_node;
924 if (CLASSTYPE_IS_TEMPLATE (t))
925 /* In this case, the TYPE_IDENTIFIER will be something
926 like S<T>, rather than S, so to get the correct name we
927 look at the template. */
928 x = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
929 else
930 x = TYPE_IDENTIFIER (t);
932 t = xref_tag (tcode, x, NULL_TREE, 0);
933 return NULL_TREE;
934 break;
936 case UNION_TYPE:
937 case ENUMERAL_TYPE:
938 if (TREE_CODE (t) == UNION_TYPE)
939 tcode = union_type_node;
940 else
941 tcode = enum_type_node;
943 t = xref_tag (tcode, TYPE_IDENTIFIER (t), NULL_TREE, 0);
944 if (ANON_UNION_TYPE_P (t))
946 /* See also shadow_tag. */
948 struct pending_inline **p;
949 tree *q;
950 x = build_lang_field_decl (FIELD_DECL, NULL_TREE, t);
952 /* Wipe out memory of synthesized methods */
953 TYPE_HAS_CONSTRUCTOR (t) = 0;
954 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
955 TYPE_HAS_INIT_REF (t) = 0;
956 TYPE_HAS_CONST_INIT_REF (t) = 0;
957 TYPE_HAS_ASSIGN_REF (t) = 0;
958 TYPE_HAS_ASSIGNMENT (t) = 0;
959 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
961 q = &TYPE_METHODS (t);
962 while (*q)
964 if (DECL_ARTIFICIAL (*q))
965 *q = TREE_CHAIN (*q);
966 else
967 q = &TREE_CHAIN (*q);
969 if (TYPE_METHODS (t))
970 error ("an anonymous union cannot have function members");
972 p = &pending_inlines;
973 for (; *p; *p = (*p)->next)
974 if (DECL_CONTEXT ((*p)->fndecl) != t)
975 break;
977 else if (TREE_CODE (t) == ENUMERAL_TYPE)
978 x = grok_enum_decls (NULL_TREE);
979 else
980 x = NULL_TREE;
981 return x;
982 break;
984 default:
985 if (t != void_type_node)
986 error ("empty component declaration");
987 return NULL_TREE;
990 else
991 /* There may or may not be any enum decls to grok, but
992 grok_enum_decls will just return components, if there aren't
993 any. We used to try to figure out whether or not there were
994 any enum decls based on the type of components, but that's too
995 hard; it might be something like `enum { a } *p;'. */
996 return grok_enum_decls (components);
999 /* Constructors for types with virtual baseclasses need an "in-charge" flag
1000 saying whether this constructor is responsible for initialization of
1001 virtual baseclasses or not. All destructors also need this "in-charge"
1002 flag, which additionally determines whether or not the destructor should
1003 free the memory for the object.
1005 This function adds the "in-charge" flag to member function FN if
1006 appropriate. It is called from grokclassfn and tsubst.
1007 FN must be either a constructor or destructor. */
1009 void
1010 maybe_retrofit_in_chrg (fn)
1011 tree fn;
1013 tree basetype, arg_types, parms, parm, fntype;
1015 if (DECL_CONSTRUCTOR_P (fn)
1016 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CLASS_CONTEXT (fn))
1017 && ! DECL_CONSTRUCTOR_FOR_VBASE_P (fn))
1018 /* OK */;
1019 else if (! DECL_CONSTRUCTOR_P (fn)
1020 && TREE_CHAIN (DECL_ARGUMENTS (fn)) == NULL_TREE)
1021 /* OK */;
1022 else
1023 return;
1025 if (DECL_CONSTRUCTOR_P (fn))
1026 DECL_CONSTRUCTOR_FOR_VBASE_P (fn) = 1;
1028 /* First add it to DECL_ARGUMENTS... */
1029 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
1030 /* Mark the artificial `__in_chrg' parameter as "artificial". */
1031 SET_DECL_ARTIFICIAL (parm);
1032 DECL_ARG_TYPE (parm) = integer_type_node;
1033 TREE_READONLY (parm) = 1;
1034 parms = DECL_ARGUMENTS (fn);
1035 TREE_CHAIN (parm) = TREE_CHAIN (parms);
1036 TREE_CHAIN (parms) = parm;
1038 /* ...and then to TYPE_ARG_TYPES. */
1039 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1040 basetype = TREE_TYPE (TREE_VALUE (arg_types));
1041 arg_types = hash_tree_chain (integer_type_node, TREE_CHAIN (arg_types));
1042 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
1043 arg_types);
1044 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1045 fntype = build_exception_variant (fntype,
1046 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
1047 TREE_TYPE (fn) = fntype;
1050 /* Classes overload their constituent function names automatically.
1051 When a function name is declared in a record structure,
1052 its name is changed to it overloaded name. Since names for
1053 constructors and destructors can conflict, we place a leading
1054 '$' for destructors.
1056 CNAME is the name of the class we are grokking for.
1058 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
1060 FLAGS contains bits saying what's special about today's
1061 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
1063 If FUNCTION is a destructor, then we must add the `auto-delete' field
1064 as a second parameter. There is some hair associated with the fact
1065 that we must "declare" this variable in the manner consistent with the
1066 way the rest of the arguments were declared.
1068 QUALS are the qualifiers for the this pointer. */
1070 void
1071 grokclassfn (ctype, cname, function, flags, quals)
1072 tree ctype, cname, function;
1073 enum overload_flags flags;
1074 tree quals;
1076 tree fn_name = DECL_NAME (function);
1077 tree arg_types;
1078 tree parm;
1079 tree qualtype;
1081 if (fn_name == NULL_TREE)
1083 error ("name missing for member function");
1084 fn_name = get_identifier ("<anonymous>");
1085 DECL_NAME (function) = fn_name;
1088 if (quals)
1089 qualtype = grok_method_quals (ctype, function, quals);
1090 else
1091 qualtype = ctype;
1093 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
1094 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1096 /* Must add the class instance variable up front. */
1097 /* Right now we just make this a pointer. But later
1098 we may wish to make it special. */
1099 tree type = TREE_VALUE (arg_types);
1100 int constp = 1;
1102 if ((flag_this_is_variable > 0)
1103 && (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)))
1104 constp = 0;
1106 parm = build_decl (PARM_DECL, this_identifier, type);
1107 /* Mark the artificial `this' parameter as "artificial". */
1108 SET_DECL_ARTIFICIAL (parm);
1109 DECL_ARG_TYPE (parm) = type;
1110 /* We can make this a register, so long as we don't
1111 accidentally complain if someone tries to take its address. */
1112 DECL_REGISTER (parm) = 1;
1113 if (constp)
1114 TREE_READONLY (parm) = 1;
1115 TREE_CHAIN (parm) = last_function_parms;
1116 last_function_parms = parm;
1119 DECL_ARGUMENTS (function) = last_function_parms;
1120 /* First approximations. */
1121 DECL_CONTEXT (function) = ctype;
1122 DECL_CLASS_CONTEXT (function) = ctype;
1124 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
1126 maybe_retrofit_in_chrg (function);
1127 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
1130 if (flags == DTOR_FLAG)
1132 DECL_ASSEMBLER_NAME (function) = build_destructor_name (ctype);
1133 TYPE_HAS_DESTRUCTOR (ctype) = 1;
1135 else
1137 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
1138 /* Only true for static member functions. */
1139 arg_types = hash_tree_chain (build_pointer_type (qualtype),
1140 arg_types);
1142 DECL_ASSEMBLER_NAME (function)
1143 = build_decl_overload (fn_name, arg_types,
1144 1 + DECL_CONSTRUCTOR_P (function));
1148 /* Work on the expr used by alignof (this is only called by the parser). */
1150 tree
1151 grok_alignof (expr)
1152 tree expr;
1154 tree best, t;
1155 int bestalign;
1157 if (processing_template_decl)
1158 return build_min (ALIGNOF_EXPR, sizetype, expr);
1160 if (TREE_CODE (expr) == COMPONENT_REF
1161 && DECL_BIT_FIELD (TREE_OPERAND (expr, 1)))
1162 error ("`__alignof__' applied to a bit-field");
1164 if (TREE_CODE (expr) == INDIRECT_REF)
1166 best = t = TREE_OPERAND (expr, 0);
1167 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1169 while (TREE_CODE (t) == NOP_EXPR
1170 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1172 int thisalign;
1173 t = TREE_OPERAND (t, 0);
1174 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1175 if (thisalign > bestalign)
1176 best = t, bestalign = thisalign;
1178 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1180 else
1182 /* ANSI says arrays and fns are converted inside comma.
1183 But we can't convert them in build_compound_expr
1184 because that would break commas in lvalues.
1185 So do the conversion here if operand was a comma. */
1186 if (TREE_CODE (expr) == COMPOUND_EXPR
1187 && (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1188 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
1189 expr = default_conversion (expr);
1190 return c_alignof (TREE_TYPE (expr));
1194 /* Create an ARRAY_REF, checking for the user doing things backwards
1195 along the way. */
1197 tree
1198 grok_array_decl (array_expr, index_exp)
1199 tree array_expr, index_exp;
1201 tree type = TREE_TYPE (array_expr);
1202 tree p1, p2, i1, i2;
1204 if (type == error_mark_node || index_exp == error_mark_node)
1205 return error_mark_node;
1206 if (processing_template_decl)
1207 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1208 array_expr, index_exp);
1210 if (type == NULL_TREE)
1212 /* Something has gone very wrong. Assume we are mistakenly reducing
1213 an expression instead of a declaration. */
1214 error ("parser may be lost: is there a '{' missing somewhere?");
1215 return NULL_TREE;
1218 if (TREE_CODE (type) == OFFSET_TYPE
1219 || TREE_CODE (type) == REFERENCE_TYPE)
1220 type = TREE_TYPE (type);
1222 /* If they have an `operator[]', use that. */
1223 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1224 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1225 array_expr, index_exp, NULL_TREE);
1227 /* Otherwise, create an ARRAY_REF for a pointer or array type. */
1229 if (TREE_CODE (type) == ARRAY_TYPE)
1230 p1 = array_expr;
1231 else
1232 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1234 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1235 p2 = index_exp;
1236 else
1237 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1239 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1240 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1242 if ((p1 && i2) && (i1 && p2))
1243 error ("ambiguous conversion for array subscript");
1245 if (p1 && i2)
1246 array_expr = p1, index_exp = i2;
1247 else if (i1 && p2)
1248 array_expr = p2, index_exp = i1;
1249 else
1251 cp_error ("invalid types `%T[%T]' for array subscript",
1252 type, TREE_TYPE (index_exp));
1253 return error_mark_node;
1256 if (array_expr == error_mark_node || index_exp == error_mark_node)
1257 error ("ambiguous conversion for array subscript");
1259 return build_array_ref (array_expr, index_exp);
1262 /* Given the cast expression EXP, checking out its validity. Either return
1263 an error_mark_node if there was an unavoidable error, return a cast to
1264 void for trying to delete a pointer w/ the value 0, or return the
1265 call to delete. If DOING_VEC is 1, we handle things differently
1266 for doing an array delete. If DOING_VEC is 2, they gave us the
1267 array size as an argument to delete.
1268 Implements ARM $5.3.4. This is called from the parser. */
1270 tree
1271 delete_sanity (exp, size, doing_vec, use_global_delete)
1272 tree exp, size;
1273 int doing_vec, use_global_delete;
1275 tree t, type;
1276 /* For a regular vector delete (aka, no size argument) we will pass
1277 this down as a NULL_TREE into build_vec_delete. */
1278 tree maxindex = NULL_TREE;
1280 if (exp == error_mark_node)
1281 return exp;
1283 if (processing_template_decl)
1285 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1286 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1287 DELETE_EXPR_USE_VEC (t) = doing_vec;
1288 return t;
1291 if (TREE_CODE (exp) == OFFSET_REF)
1292 exp = resolve_offset_ref (exp);
1293 exp = convert_from_reference (exp);
1294 t = stabilize_reference (exp);
1295 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1297 if (t == NULL_TREE || t == error_mark_node)
1299 cp_error ("type `%#T' argument given to `delete', expected pointer",
1300 TREE_TYPE (exp));
1301 return error_mark_node;
1304 if (doing_vec == 2)
1306 maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
1307 pedwarn ("anachronistic use of array size in vector delete");
1310 type = TREE_TYPE (t);
1312 /* As of Valley Forge, you can delete a pointer to const. */
1314 /* You can't delete functions. */
1315 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1317 error ("cannot delete a function");
1318 return error_mark_node;
1321 /* An array can't have been allocated by new, so complain. */
1322 if (TREE_CODE (t) == ADDR_EXPR
1323 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1324 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1325 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1327 /* Deleting a pointer with the value zero is valid and has no effect. */
1328 if (integer_zerop (t))
1329 return build1 (NOP_EXPR, void_type_node, t);
1331 if (doing_vec)
1332 return build_vec_delete (t, maxindex, integer_one_node,
1333 integer_two_node, use_global_delete);
1334 else
1336 if (IS_AGGR_TYPE (TREE_TYPE (type))
1337 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1339 /* Only do access checking here; we'll be calling op delete
1340 from the destructor. */
1341 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1342 LOOKUP_NORMAL, NULL_TREE);
1343 if (tmp == error_mark_node)
1344 return error_mark_node;
1347 return build_delete (type, t, integer_three_node,
1348 LOOKUP_NORMAL, use_global_delete);
1352 /* Report an error if the indicated template declaration is not the
1353 sort of thing that should be a member template. */
1355 void
1356 check_member_template (tmpl)
1357 tree tmpl;
1359 tree decl;
1361 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1362 decl = DECL_TEMPLATE_RESULT (tmpl);
1364 if (TREE_CODE (decl) == FUNCTION_DECL
1365 || (TREE_CODE (decl) == TYPE_DECL
1366 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1368 if (current_function_decl)
1369 /* 14.5.2.2 [temp.mem]
1371 A local class shall not have member templates. */
1372 cp_error ("declaration of member template `%#D' in local class",
1373 decl);
1375 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1377 /* 14.5.2.3 [temp.mem]
1379 A member function template shall not be virtual. */
1380 cp_error
1381 ("invalid use of `virtual' in template declaration of `%#D'",
1382 decl);
1383 DECL_VIRTUAL_P (decl) = 0;
1386 /* The debug-information generating code doesn't know what to do
1387 with member templates. */
1388 DECL_IGNORED_P (tmpl) = 1;
1390 else
1391 cp_error ("template declaration of `%#D'", decl);
1394 /* Return true iff TYPE is a valid Java parameter or return type. */
1397 acceptable_java_type (type)
1398 tree type;
1400 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1401 return 1;
1402 if (TREE_CODE (type) == POINTER_TYPE)
1404 type = TREE_TYPE (type);
1405 if (TREE_CODE (type) == RECORD_TYPE)
1407 complete_type (type);
1408 return TYPE_FOR_JAVA (type);
1411 return 0;
1414 /* For a METHOD in a Java class CTYPE, return 1 if
1415 the parameter and return types are valid Java types.
1416 Otherwise, print appropriate error messages, and return 0. */
1419 check_java_method (ctype, method)
1420 tree ctype, method;
1422 int jerr = 0;
1423 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1424 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1425 if (! acceptable_java_type (ret_type))
1427 cp_error ("Java method '%D' has non-Java return type `%T'",
1428 method, ret_type);
1429 jerr++;
1431 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1433 tree type = TREE_VALUE (arg_types);
1434 if (! acceptable_java_type (type))
1436 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1437 method, type);
1438 jerr++;
1441 return jerr ? 0 : 1;
1444 /* Sanity check: report error if this function FUNCTION is not
1445 really a member of the class (CTYPE) it is supposed to belong to.
1446 CNAME is the same here as it is for grokclassfn above. */
1448 tree
1449 check_classfn (ctype, function)
1450 tree ctype, function;
1452 tree fn_name = DECL_NAME (function);
1453 tree fndecl, fndecls;
1454 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1455 tree *methods = 0;
1456 tree *end = 0;
1457 tree templates = NULL_TREE;
1459 if (method_vec != 0)
1461 methods = &TREE_VEC_ELT (method_vec, 0);
1462 end = TREE_VEC_END (method_vec);
1464 /* First suss out ctors and dtors. */
1465 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1466 && DECL_CONSTRUCTOR_P (function))
1467 goto got_it;
1468 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1469 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
1470 goto got_it;
1472 while (++methods != end)
1474 fndecl = *methods;
1475 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1477 got_it:
1478 for (fndecls = *methods; fndecls != NULL_TREE;
1479 fndecls = OVL_NEXT (fndecls))
1481 fndecl = OVL_CURRENT (fndecls);
1482 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL is
1483 not mangled, so the check below does not work
1484 correctly in that case. */
1485 if (TREE_CODE (function) != TEMPLATE_DECL
1486 && TREE_CODE (fndecl) != TEMPLATE_DECL
1487 && (DECL_ASSEMBLER_NAME (function)
1488 == DECL_ASSEMBLER_NAME (fndecl)))
1489 return fndecl;
1491 /* We cannot simply call decls_match because this
1492 doesn't work for static member functions that are
1493 pretending to be methods, and because the name
1494 may have been changed by asm("new_name"). */
1495 if (DECL_NAME (function) == DECL_NAME (fndecl))
1497 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1498 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1500 /* Get rid of the this parameter on functions that become
1501 static. */
1502 if (DECL_STATIC_FUNCTION_P (fndecl)
1503 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1504 p1 = TREE_CHAIN (p1);
1506 if (comptypes (TREE_TYPE (TREE_TYPE (function)),
1507 TREE_TYPE (TREE_TYPE (fndecl)), 1)
1508 && compparms (p1, p2, 3)
1509 && (DECL_TEMPLATE_SPECIALIZATION (function)
1510 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1511 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1512 || (DECL_TI_TEMPLATE (function)
1513 == DECL_TI_TEMPLATE (fndecl))))
1514 return fndecl;
1516 if (is_member_template (fndecl))
1517 /* This function might be an instantiation
1518 or specialization of fndecl. */
1519 templates =
1520 scratch_tree_cons (NULL_TREE, fndecl, templates);
1523 break; /* loser */
1525 else if (TREE_CODE (fndecl) == TEMPLATE_DECL
1526 && IDENTIFIER_TYPENAME_P (DECL_NAME (fndecl))
1527 && IDENTIFIER_TYPENAME_P (fn_name))
1528 /* The method in the class is a member template
1529 conversion operator. We are declaring another
1530 conversion operator. It is possible that even though
1531 the names don't match, there is some specialization
1532 occurring. */
1533 templates =
1534 scratch_tree_cons (NULL_TREE, fndecl, templates);
1538 if (templates)
1539 /* This function might be an instantiation or a specialization.
1540 We should verify that this is possible. If it is, we must
1541 somehow add the new declaration to the method vector for the
1542 class. Perhaps we should use add_method? For now, we simply
1543 return NULL_TREE, which lets the caller know that this
1544 function is new, but we don't print an error message. */
1545 return NULL_TREE;
1547 if (methods != end)
1549 tree fndecl = *methods;
1550 cp_error ("prototype for `%#D' does not match any in class `%T'",
1551 function, ctype);
1552 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1553 OVL_CURRENT (fndecl));
1554 while (fndecl = OVL_NEXT (fndecl), fndecl)
1555 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1557 else
1559 methods = 0;
1560 cp_error ("no `%#D' member function declared in class `%T'",
1561 function, ctype);
1564 /* If we did not find the method in the class, add it to avoid
1565 spurious errors. */
1566 add_method (ctype, methods, function);
1567 return NULL_TREE;
1570 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1571 of a structure component, returning a FIELD_DECL node.
1572 QUALS is a list of type qualifiers for this decl (such as for declaring
1573 const member functions).
1575 This is done during the parsing of the struct declaration.
1576 The FIELD_DECL nodes are chained together and the lot of them
1577 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1579 C++:
1581 If class A defines that certain functions in class B are friends, then
1582 the way I have set things up, it is B who is interested in permission
1583 granted by A. However, it is in A's context that these declarations
1584 are parsed. By returning a void_type_node, class A does not attempt
1585 to incorporate the declarations of the friends within its structure.
1587 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1588 CHANGES TO CODE IN `start_method'. */
1590 tree
1591 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1592 tree declarator, declspecs, init, asmspec_tree, attrlist;
1594 register tree value;
1595 char *asmspec = 0;
1596 int flags = LOOKUP_ONLYCONVERTING;
1598 /* Convert () initializers to = initializers. */
1599 if (init == NULL_TREE && declarator != NULL_TREE
1600 && TREE_CODE (declarator) == CALL_EXPR
1601 && TREE_OPERAND (declarator, 0)
1602 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1603 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1604 && parmlist_is_exprlist (TREE_OPERAND (declarator, 1)))
1606 init = TREE_OPERAND (declarator, 1);
1607 declarator = TREE_OPERAND (declarator, 0);
1608 flags = 0;
1611 if (declspecs == NULL_TREE
1612 && TREE_CODE (declarator) == SCOPE_REF
1613 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1615 /* Access declaration */
1616 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1618 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1619 pop_nested_class (1);
1620 return do_class_using_decl (declarator);
1623 if (init
1624 && TREE_CODE (init) == TREE_LIST
1625 && TREE_VALUE (init) == error_mark_node
1626 && TREE_CHAIN (init) == NULL_TREE)
1627 init = NULL_TREE;
1629 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, NULL_TREE);
1630 if (! value)
1631 return value; /* friend or constructor went bad. */
1633 /* Pass friendly classes back. */
1634 if (TREE_CODE (value) == VOID_TYPE)
1635 return void_type_node;
1637 if (DECL_NAME (value) != NULL_TREE
1638 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1639 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1640 cp_error ("member `%D' conflicts with virtual function table field name",
1641 value);
1643 /* Stash away type declarations. */
1644 if (TREE_CODE (value) == TYPE_DECL)
1646 DECL_NONLOCAL (value) = 1;
1647 DECL_CONTEXT (value) = current_class_type;
1648 DECL_CLASS_CONTEXT (value) = current_class_type;
1649 CLASSTYPE_LOCAL_TYPEDECLS (current_class_type) = 1;
1651 /* Now that we've updated the context, we need to remangle the
1652 name for this TYPE_DECL. */
1653 DECL_ASSEMBLER_NAME (value) = DECL_NAME (value);
1654 DECL_ASSEMBLER_NAME (value) =
1655 get_identifier (build_overload_name (TREE_TYPE (value), 1, 1));
1657 pushdecl_class_level (value);
1658 return value;
1661 if (IS_SIGNATURE (current_class_type)
1662 && TREE_CODE (value) != FUNCTION_DECL)
1664 error ("field declaration not allowed in signature");
1665 return void_type_node;
1668 if (DECL_IN_AGGR_P (value))
1670 cp_error ("`%D' is already defined in the class %T", value,
1671 DECL_CONTEXT (value));
1672 return void_type_node;
1675 if (asmspec_tree)
1676 asmspec = TREE_STRING_POINTER (asmspec_tree);
1678 if (init)
1680 if (IS_SIGNATURE (current_class_type)
1681 && TREE_CODE (value) == FUNCTION_DECL)
1683 error ("function declarations cannot have initializers in signature");
1684 init = NULL_TREE;
1686 else if (TREE_CODE (value) == FUNCTION_DECL)
1688 grok_function_init (value, init);
1689 init = NULL_TREE;
1691 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1692 /* Already complained in grokdeclarator. */
1693 init = NULL_TREE;
1694 else
1696 /* We allow initializers to become parameters to base
1697 initializers. */
1698 if (TREE_CODE (init) == TREE_LIST)
1700 if (TREE_CHAIN (init) == NULL_TREE)
1701 init = TREE_VALUE (init);
1702 else
1703 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1706 if (TREE_CODE (init) == CONST_DECL)
1707 init = DECL_INITIAL (init);
1708 else if (TREE_READONLY_DECL_P (init))
1709 init = decl_constant_value (init);
1710 else if (TREE_CODE (init) == CONSTRUCTOR)
1711 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1712 my_friendly_assert (TREE_PERMANENT (init), 192);
1713 if (init == error_mark_node)
1714 /* We must make this look different than `error_mark_node'
1715 because `decl_const_value' would mis-interpret it
1716 as only meaning that this VAR_DECL is defined. */
1717 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1718 else if (processing_template_decl)
1720 else if (! TREE_CONSTANT (init))
1722 /* We can allow references to things that are effectively
1723 static, since references are initialized with the address. */
1724 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1725 || (TREE_STATIC (init) == 0
1726 && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
1727 || DECL_EXTERNAL (init) == 0)))
1729 error ("field initializer is not constant");
1730 init = error_mark_node;
1736 /* The corresponding pop_obstacks is in cp_finish_decl. */
1737 push_obstacks_nochange ();
1739 if (processing_template_decl && ! current_function_decl
1740 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1741 value = push_template_decl (value);
1743 if (attrlist)
1744 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1745 TREE_VALUE (attrlist));
1747 if (TREE_CODE (value) == VAR_DECL)
1749 my_friendly_assert (TREE_PUBLIC (value), 0);
1751 /* We cannot call pushdecl here, because that would
1752 fill in the value of our TREE_CHAIN. Instead, we
1753 modify cp_finish_decl to do the right thing, namely, to
1754 put this decl out straight away. */
1755 /* current_class_type can be NULL_TREE in case of error. */
1756 if (asmspec == 0 && current_class_type)
1758 TREE_PUBLIC (value) = 1;
1759 DECL_INITIAL (value) = error_mark_node;
1760 DECL_ASSEMBLER_NAME (value)
1761 = build_static_name (current_class_type, DECL_NAME (value));
1763 if (! processing_template_decl)
1764 pending_statics = perm_tree_cons (NULL_TREE, value, pending_statics);
1766 /* Static consts need not be initialized in the class definition. */
1767 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (value)))
1769 static int explanation = 0;
1771 error ("initializer invalid for static member with constructor");
1772 if (explanation++ == 0)
1773 error ("(you really want to initialize it separately)");
1774 init = 0;
1776 /* Force the compiler to know when an uninitialized static
1777 const member is being used. */
1778 if (TYPE_READONLY (value) && init == 0)
1779 TREE_USED (value) = 1;
1780 DECL_INITIAL (value) = init;
1781 DECL_IN_AGGR_P (value) = 1;
1782 DECL_CONTEXT (value) = current_class_type;
1783 DECL_CLASS_CONTEXT (value) = current_class_type;
1785 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1786 pushdecl_class_level (value);
1787 return value;
1789 if (TREE_CODE (value) == FIELD_DECL)
1791 if (asmspec)
1793 /* This must override the asm specifier which was placed
1794 by grokclassfn. Lay this out fresh. */
1795 DECL_RTL (value) = NULL_RTX;
1796 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1798 if (DECL_INITIAL (value) == error_mark_node)
1799 init = error_mark_node;
1800 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1801 DECL_INITIAL (value) = init;
1802 DECL_IN_AGGR_P (value) = 1;
1803 return value;
1805 if (TREE_CODE (value) == FUNCTION_DECL)
1807 if (asmspec)
1809 /* This must override the asm specifier which was placed
1810 by grokclassfn. Lay this out fresh. */
1811 DECL_RTL (value) = NULL_RTX;
1812 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1814 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1816 /* Pass friends back this way. */
1817 if (DECL_FRIEND_P (value))
1818 return void_type_node;
1820 #if 0 /* Just because a fn is declared doesn't mean we'll try to define it. */
1821 if (current_function_decl && ! IS_SIGNATURE (current_class_type))
1822 cp_error ("method `%#D' of local class must be defined in class body",
1823 value);
1824 #endif
1826 DECL_IN_AGGR_P (value) = 1;
1827 return value;
1829 my_friendly_abort (21);
1830 /* NOTREACHED */
1831 return NULL_TREE;
1834 /* Like `grokfield', but for bitfields.
1835 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1837 tree
1838 grokbitfield (declarator, declspecs, width)
1839 tree declarator, declspecs, width;
1841 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1842 0, NULL_TREE);
1844 if (! value) return NULL_TREE; /* friends went bad. */
1846 /* Pass friendly classes back. */
1847 if (TREE_CODE (value) == VOID_TYPE)
1848 return void_type_node;
1850 if (TREE_CODE (value) == TYPE_DECL)
1852 cp_error ("cannot declare `%D' to be a bitfield type", value);
1853 return NULL_TREE;
1856 if (IS_SIGNATURE (current_class_type))
1858 error ("field declaration not allowed in signature");
1859 return void_type_node;
1862 if (DECL_IN_AGGR_P (value))
1864 cp_error ("`%D' is already defined in the class %T", value,
1865 DECL_CONTEXT (value));
1866 return void_type_node;
1869 GNU_xref_member (current_class_name, value);
1871 if (TREE_STATIC (value))
1873 cp_error ("static member `%D' cannot be a bitfield", value);
1874 return NULL_TREE;
1876 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0, 0);
1878 if (width != error_mark_node)
1880 constant_expression_warning (width);
1881 DECL_INITIAL (value) = width;
1882 DECL_BIT_FIELD (value) = 1;
1885 DECL_IN_AGGR_P (value) = 1;
1886 return value;
1889 tree
1890 grokoptypename (declspecs, declarator)
1891 tree declspecs, declarator;
1893 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1894 return build_typename_overload (t);
1897 /* When a function is declared with an initializer,
1898 do the right thing. Currently, there are two possibilities:
1900 class B
1902 public:
1903 // initialization possibility #1.
1904 virtual void f () = 0;
1905 int g ();
1908 class D1 : B
1910 public:
1911 int d1;
1912 // error, no f ();
1915 class D2 : B
1917 public:
1918 int d2;
1919 void f ();
1922 class D3 : B
1924 public:
1925 int d3;
1926 // initialization possibility #2
1927 void f () = B::f;
1933 copy_assignment_arg_p (parmtype, virtualp)
1934 tree parmtype;
1935 int virtualp;
1937 if (current_class_type == NULL_TREE)
1938 return 0;
1940 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1941 parmtype = TREE_TYPE (parmtype);
1943 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1944 #if 0
1945 /* Non-standard hack to support old Booch components. */
1946 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1947 #endif
1949 return 1;
1951 return 0;
1954 static void
1955 grok_function_init (decl, init)
1956 tree decl;
1957 tree init;
1959 /* An initializer for a function tells how this function should
1960 be inherited. */
1961 tree type = TREE_TYPE (decl);
1963 if (TREE_CODE (type) == FUNCTION_TYPE)
1964 cp_error ("initializer specified for non-member function `%D'", decl);
1965 #if 0
1966 /* We'll check for this in finish_struct_1. */
1967 else if (DECL_VINDEX (decl) == NULL_TREE)
1968 cp_error ("initializer specified for non-virtual method `%D'", decl);
1969 #endif
1970 else if (integer_zerop (init))
1972 #if 0
1973 /* Mark this function as being "defined". */
1974 DECL_INITIAL (decl) = error_mark_node;
1975 /* pure virtual destructors must be defined. */
1976 /* pure virtual needs to be defined (as abort) only when put in
1977 vtbl. For wellformed call, it should be itself. pr4737 */
1978 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1980 extern tree abort_fndecl;
1981 /* Give this node rtl from `abort'. */
1982 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
1984 #endif
1985 DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
1986 if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
1988 tree parmtype
1989 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1991 if (copy_assignment_arg_p (parmtype, 1))
1992 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1995 else
1996 cp_error ("invalid initializer for virtual method `%D'", decl);
1999 void
2000 cplus_decl_attributes (decl, attributes, prefix_attributes)
2001 tree decl, attributes, prefix_attributes;
2003 if (decl == NULL_TREE || decl == void_type_node)
2004 return;
2006 if (TREE_CODE (decl) == TEMPLATE_DECL)
2007 decl = DECL_TEMPLATE_RESULT (decl);
2009 decl_attributes (decl, attributes, prefix_attributes);
2011 if (TREE_CODE (decl) == TYPE_DECL)
2012 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
2015 /* CONSTRUCTOR_NAME:
2016 Return the name for the constructor (or destructor) for the
2017 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2018 IDENTIFIER_NODE. When given a template, this routine doesn't
2019 lose the specialization. */
2021 tree
2022 constructor_name_full (thing)
2023 tree thing;
2025 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
2026 || TREE_CODE (thing) == TEMPLATE_TEMPLATE_PARM
2027 || TREE_CODE (thing) == TYPENAME_TYPE)
2028 thing = TYPE_NAME (thing);
2029 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
2031 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
2032 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
2033 else
2034 thing = TYPE_NAME (thing);
2036 if (TREE_CODE (thing) == TYPE_DECL
2037 || (TREE_CODE (thing) == TEMPLATE_DECL
2038 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
2039 thing = DECL_NAME (thing);
2040 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
2041 return thing;
2044 /* CONSTRUCTOR_NAME:
2045 Return the name for the constructor (or destructor) for the
2046 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
2047 IDENTIFIER_NODE. When given a template, return the plain
2048 unspecialized name. */
2050 tree
2051 constructor_name (thing)
2052 tree thing;
2054 tree t;
2055 thing = constructor_name_full (thing);
2056 t = IDENTIFIER_TEMPLATE (thing);
2057 if (!t)
2058 return thing;
2059 return t;
2062 /* Cache the value of this class's main virtual function table pointer
2063 in a register variable. This will save one indirection if a
2064 more than one virtual function call is made this function. */
2066 void
2067 setup_vtbl_ptr ()
2069 extern tree base_init_expr;
2071 if (base_init_expr == 0
2072 && DECL_CONSTRUCTOR_P (current_function_decl))
2074 if (processing_template_decl)
2075 add_tree (build_min_nt
2076 (CTOR_INITIALIZER,
2077 current_member_init_list, current_base_init_list));
2078 else
2079 emit_base_init (current_class_type, 0);
2083 /* Record the existence of an addressable inline function. */
2085 void
2086 mark_inline_for_output (decl)
2087 tree decl;
2089 decl = DECL_MAIN_VARIANT (decl);
2090 if (DECL_SAVED_INLINE (decl))
2091 return;
2092 my_friendly_assert (TREE_PERMANENT (decl), 363);
2093 DECL_SAVED_INLINE (decl) = 1;
2094 #if 0
2095 if (DECL_PENDING_INLINE_INFO (decl) != 0
2096 && ! DECL_PENDING_INLINE_INFO (decl)->deja_vu)
2098 struct pending_inline *t = pending_inlines;
2099 my_friendly_assert (DECL_SAVED_INSNS (decl) == 0, 198);
2100 while (t)
2102 if (t == DECL_PENDING_INLINE_INFO (decl))
2103 break;
2104 t = t->next;
2106 if (t == 0)
2108 t = DECL_PENDING_INLINE_INFO (decl);
2109 t->next = pending_inlines;
2110 pending_inlines = t;
2112 DECL_PENDING_INLINE_INFO (decl) = 0;
2114 #endif
2115 saved_inlines = perm_tree_cons (NULL_TREE, decl, saved_inlines);
2118 void
2119 clear_temp_name ()
2121 temp_name_counter = 0;
2124 /* Hand off a unique name which can be used for variable we don't really
2125 want to know about anyway, for example, the anonymous variables which
2126 are needed to make references work. Declare this thing so we can use it.
2127 The variable created will be of type TYPE.
2129 STATICP is nonzero if this variable should be static. */
2131 tree
2132 get_temp_name (type, staticp)
2133 tree type;
2134 int staticp;
2136 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
2137 tree decl;
2138 int toplev = toplevel_bindings_p ();
2140 push_obstacks_nochange ();
2141 if (toplev || staticp)
2143 end_temporary_allocation ();
2144 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
2145 decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
2147 else
2149 sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
2150 decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
2152 TREE_USED (decl) = 1;
2153 TREE_STATIC (decl) = staticp;
2154 DECL_ARTIFICIAL (decl) = 1;
2156 /* If this is a local variable, then lay out its rtl now.
2157 Otherwise, callers of this function are responsible for dealing
2158 with this variable's rtl. */
2159 if (! toplev)
2161 expand_decl (decl);
2162 expand_decl_init (decl);
2164 pop_obstacks ();
2166 return decl;
2169 /* Get a variable which we can use for multiple assignments.
2170 It is not entered into current_binding_level, because
2171 that breaks things when it comes time to do final cleanups
2172 (which take place "outside" the binding contour of the function). */
2174 tree
2175 get_temp_regvar (type, init)
2176 tree type, init;
2178 tree decl;
2180 decl = build_decl (VAR_DECL, NULL_TREE, type);
2181 TREE_USED (decl) = 1;
2182 DECL_REGISTER (decl) = 1;
2183 DECL_ARTIFICIAL (decl) = 1;
2185 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2186 /* We can expand these without fear, since they cannot need
2187 constructors or destructors. */
2188 expand_expr (build_modify_expr (decl, INIT_EXPR, init),
2189 NULL_RTX, VOIDmode, 0);
2191 return decl;
2194 /* Hunts through the global anonymous union ANON_DECL, building
2195 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2196 returns a VAR_DECL whose size is the same as the size of the
2197 ANON_DECL, if one is available. */
2199 tree
2200 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2201 tree anon_decl;
2202 tree* elems;
2203 int static_p;
2204 int external_p;
2206 tree type = TREE_TYPE (anon_decl);
2207 tree main_decl = NULL_TREE;
2208 tree field;
2210 for (field = TYPE_FIELDS (type);
2211 field != NULL_TREE;
2212 field = TREE_CHAIN (field))
2214 tree decl;
2215 if (TREE_CODE (field) != FIELD_DECL)
2216 continue;
2218 if (TREE_PRIVATE (field))
2219 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2220 else if (TREE_PROTECTED (field))
2221 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2223 if (DECL_NAME (field) == NULL_TREE
2224 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2226 decl = build_anon_union_vars (field, elems, static_p, external_p);
2227 if (!decl)
2228 continue;
2230 else
2232 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2233 /* tell `pushdecl' that this is not tentative. */
2234 DECL_INITIAL (decl) = error_mark_node;
2235 TREE_PUBLIC (decl) = 0;
2236 TREE_STATIC (decl) = static_p;
2237 DECL_EXTERNAL (decl) = external_p;
2238 decl = pushdecl (decl);
2239 DECL_INITIAL (decl) = NULL_TREE;
2242 /* Only write out one anon union element--choose the one that
2243 can hold them all. */
2244 if (main_decl == NULL_TREE
2245 && simple_cst_equal (DECL_SIZE (decl),
2246 DECL_SIZE (anon_decl)) == 1)
2247 main_decl = decl;
2248 else
2249 /* ??? This causes there to be no debug info written out
2250 about this decl. */
2251 TREE_ASM_WRITTEN (decl) = 1;
2253 if (DECL_NAME (field) == NULL_TREE
2254 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2255 /* The remainder of the processing was already done in the
2256 recursive call. */
2257 continue;
2259 /* If there's a cleanup to do, it belongs in the
2260 TREE_PURPOSE of the following TREE_LIST. */
2261 *elems = scratch_tree_cons (NULL_TREE, decl, *elems);
2262 TREE_TYPE (*elems) = type;
2265 return main_decl;
2268 /* Finish off the processing of a UNION_TYPE structure.
2269 If there are static members, then all members are
2270 static, and must be laid out together. If the
2271 union is an anonymous union, we arrange for that
2272 as well. PUBLIC_P is nonzero if this union is
2273 not declared static. */
2275 void
2276 finish_anon_union (anon_union_decl)
2277 tree anon_union_decl;
2279 tree type = TREE_TYPE (anon_union_decl);
2280 tree elems = NULL_TREE;
2281 tree main_decl;
2282 int public_p = TREE_PUBLIC (anon_union_decl);
2283 int static_p = TREE_STATIC (anon_union_decl);
2284 int external_p = DECL_EXTERNAL (anon_union_decl);
2286 if (TYPE_FIELDS (type) == NULL_TREE)
2287 return;
2289 if (public_p)
2291 error ("global anonymous unions must be declared static");
2292 return;
2295 main_decl = build_anon_union_vars (anon_union_decl, &elems,
2296 static_p, external_p);
2298 if (static_p)
2300 if (main_decl)
2302 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2303 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2305 else
2307 warning ("anonymous union with no members");
2308 return;
2312 /* The following call assumes that there are never any cleanups
2313 for anonymous unions--a reasonable assumption. */
2314 expand_anon_union_decl (anon_union_decl, NULL_TREE, elems);
2317 /* Finish processing a builtin type TYPE. It's name is NAME,
2318 its fields are in the array FIELDS. LEN is the number of elements
2319 in FIELDS minus one, or put another way, it is the maximum subscript
2320 used in FIELDS.
2322 It is given the same alignment as ALIGN_TYPE. */
2324 void
2325 finish_builtin_type (type, name, fields, len, align_type)
2326 tree type;
2327 char *name;
2328 tree fields[];
2329 int len;
2330 tree align_type;
2332 register int i;
2334 TYPE_FIELDS (type) = fields[0];
2335 for (i = 0; i < len; i++)
2337 layout_type (TREE_TYPE (fields[i]));
2338 DECL_FIELD_CONTEXT (fields[i]) = type;
2339 TREE_CHAIN (fields[i]) = fields[i+1];
2341 DECL_FIELD_CONTEXT (fields[i]) = type;
2342 DECL_CLASS_CONTEXT (fields[i]) = type;
2343 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2344 layout_type (type);
2345 #if 0 /* not yet, should get fixed properly later */
2346 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2347 #else
2348 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2349 #endif
2350 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2351 layout_decl (TYPE_NAME (type), 0);
2354 /* Auxiliary functions to make type signatures for
2355 `operator new' and `operator delete' correspond to
2356 what compiler will be expecting. */
2358 tree
2359 coerce_new_type (type)
2360 tree type;
2362 int e1 = 0, e2 = 0;
2364 if (TREE_CODE (type) == METHOD_TYPE)
2365 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2366 if (TREE_TYPE (type) != ptr_type_node)
2367 e1 = 1, error ("`operator new' must return type `void *'");
2369 /* Technically the type must be `size_t', but we may not know
2370 what that is. */
2371 if (TYPE_ARG_TYPES (type) == NULL_TREE)
2372 e1 = 1, error ("`operator new' takes type `size_t' parameter");
2373 else if (TREE_CODE (TREE_VALUE (TYPE_ARG_TYPES (type))) != INTEGER_TYPE
2374 || TYPE_PRECISION (TREE_VALUE (TYPE_ARG_TYPES (type))) != TYPE_PRECISION (sizetype))
2375 e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2376 if (e2)
2377 type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2378 else if (e1)
2379 type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2380 return type;
2383 tree
2384 coerce_delete_type (type)
2385 tree type;
2387 int e1 = 0, e2 = 0;
2388 #if 0
2389 e3 = 0;
2390 #endif
2391 tree arg_types = TYPE_ARG_TYPES (type);
2393 if (TREE_CODE (type) == METHOD_TYPE)
2395 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2396 arg_types = TREE_CHAIN (arg_types);
2399 if (TREE_TYPE (type) != void_type_node)
2400 e1 = 1, error ("`operator delete' must return type `void'");
2402 if (arg_types == NULL_TREE
2403 || TREE_VALUE (arg_types) != ptr_type_node)
2404 e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2406 #if 0
2407 if (arg_types
2408 && TREE_CHAIN (arg_types)
2409 && TREE_CHAIN (arg_types) != void_list_node)
2411 /* Again, technically this argument must be `size_t', but again
2412 we may not know what that is. */
2413 tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2414 if (TREE_CODE (t2) != INTEGER_TYPE
2415 || TYPE_PRECISION (t2) != TYPE_PRECISION (sizetype))
2416 e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2417 else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2419 e3 = 1;
2420 if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2421 error ("too many arguments in declaration of `operator delete'");
2422 else
2423 error ("`...' invalid in specification of `operator delete'");
2427 if (e3)
2428 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2429 build_tree_list (NULL_TREE, sizetype));
2430 else if (e3 |= e2)
2432 if (arg_types == NULL_TREE)
2433 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2434 else
2435 arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2437 else e3 |= e1;
2438 #endif
2440 if (e2)
2441 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2442 arg_types ? TREE_CHAIN (arg_types): NULL_TREE);
2443 if (e2 || e1)
2444 type = build_function_type (void_type_node, arg_types);
2446 return type;
2449 extern tree abort_fndecl;
2451 static void
2452 mark_vtable_entries (decl)
2453 tree decl;
2455 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2457 if (flag_rtti)
2459 tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (TREE_CHAIN (entries))
2460 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2461 tree fn = TREE_OPERAND (fnaddr, 0);
2462 TREE_ADDRESSABLE (fn) = 1;
2463 mark_used (fn);
2465 skip_rtti_stuff (&entries);
2467 for (; entries; entries = TREE_CHAIN (entries))
2469 tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2470 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2471 tree fn = TREE_OPERAND (fnaddr, 0);
2472 TREE_ADDRESSABLE (fn) = 1;
2473 if (DECL_LANG_SPECIFIC (fn) && DECL_ABSTRACT_VIRTUAL_P (fn))
2475 TREE_OPERAND (fnaddr, 0) = fn = copy_node (fn);
2476 DECL_RTL (fn) = DECL_RTL (abort_fndecl);
2477 mark_used (abort_fndecl);
2479 if (TREE_CODE (fn) == THUNK_DECL && DECL_EXTERNAL (fn))
2481 DECL_EXTERNAL (fn) = 0;
2482 emit_thunk (fn);
2484 mark_used (fn);
2488 /* Set DECL up to have the closest approximation of "initialized common"
2489 linkage available. */
2491 void
2492 comdat_linkage (decl)
2493 tree decl;
2495 if (flag_weak)
2496 make_decl_one_only (decl);
2497 else
2498 TREE_PUBLIC (decl) = 0;
2500 if (DECL_LANG_SPECIFIC (decl))
2501 DECL_COMDAT (decl) = 1;
2504 /* For win32 we also want to put explicit instantiations in
2505 linkonce sections, so that they will be merged with implicit
2506 instantiations; otherwise we get duplicate symbol errors. */
2508 void
2509 maybe_make_one_only (decl)
2510 tree decl;
2512 /* This is not necessary on targets that support weak symbols, because
2513 the implicit instantiations will defer to the explicit one. */
2514 if (! supports_one_only () || SUPPORTS_WEAK)
2515 return;
2517 /* We can't set DECL_COMDAT on functions, or finish_file will think
2518 we can get away with not emitting them if they aren't used.
2519 We can't use make_decl_one_only for variables, because their
2520 DECL_INITIAL may not have been set properly yet. */
2522 if (TREE_CODE (decl) == FUNCTION_DECL)
2523 make_decl_one_only (decl);
2524 else
2525 comdat_linkage (decl);
2528 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2529 based on TYPE and other static flags.
2531 Note that anything public is tagged TREE_PUBLIC, whether
2532 it's public in this file or in another one. */
2534 void
2535 import_export_vtable (decl, type, final)
2536 tree decl, type;
2537 int final;
2539 if (DECL_INTERFACE_KNOWN (decl))
2540 return;
2542 /* +e0 or +e1 */
2543 if (write_virtuals < 2 && write_virtuals != 0)
2545 TREE_PUBLIC (decl) = 1;
2546 if (write_virtuals < 0)
2547 DECL_EXTERNAL (decl) = 1;
2548 DECL_INTERFACE_KNOWN (decl) = 1;
2550 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2552 TREE_PUBLIC (decl) = 1;
2553 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2554 DECL_INTERFACE_KNOWN (decl) = 1;
2556 /* For WIN32 we also want to put explicit instantiations in
2557 linkonce sections. */
2558 if (CLASSTYPE_EXPLICIT_INSTANTIATION (type))
2559 maybe_make_one_only (decl);
2561 else
2563 /* We can only wait to decide if we have real non-inline virtual
2564 functions in our class, or if we come from a template. */
2566 int found = CLASSTYPE_TEMPLATE_INSTANTIATION (type);
2568 #ifndef MULTIPLE_SYMBOL_SPACES
2569 if (! found && ! final)
2571 tree method;
2572 for (method = TYPE_METHODS (type); method != NULL_TREE;
2573 method = TREE_CHAIN (method))
2574 if (DECL_VINDEX (method) != NULL_TREE
2575 && ! DECL_THIS_INLINE (method)
2576 && ! DECL_ABSTRACT_VIRTUAL_P (method))
2578 found = 1;
2579 break;
2582 #endif
2584 if (final || ! found)
2586 comdat_linkage (decl);
2587 DECL_EXTERNAL (decl) = 0;
2589 else
2591 TREE_PUBLIC (decl) = 1;
2592 DECL_EXTERNAL (decl) = 1;
2597 static void
2598 import_export_template (type)
2599 tree type;
2601 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
2602 && ! flag_implicit_templates
2603 && CLASSTYPE_INTERFACE_UNKNOWN (type))
2605 SET_CLASSTYPE_INTERFACE_KNOWN (type);
2606 CLASSTYPE_INTERFACE_ONLY (type) = 1;
2607 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 0;
2612 finish_prevtable_vardecl (prev, vars)
2613 tree prev, vars;
2615 tree ctype = DECL_CONTEXT (vars);
2616 import_export_template (ctype);
2618 #ifndef MULTIPLE_SYMBOL_SPACES
2619 if (CLASSTYPE_INTERFACE_UNKNOWN (ctype) && TYPE_VIRTUAL_P (ctype)
2620 && ! CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2622 tree method;
2623 for (method = TYPE_METHODS (ctype); method != NULL_TREE;
2624 method = TREE_CHAIN (method))
2626 if (DECL_VINDEX (method) != NULL_TREE
2627 && !DECL_THIS_INLINE (method)
2628 && !DECL_ABSTRACT_VIRTUAL_P (method))
2630 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2631 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype)
2632 = ! DECL_REALLY_EXTERN (method);
2633 CLASSTYPE_INTERFACE_ONLY (ctype) = DECL_REALLY_EXTERN (method);
2634 break;
2638 #endif
2640 import_export_vtable (vars, ctype, 1);
2641 return 1;
2644 static int
2645 finish_vtable_vardecl (prev, vars)
2646 tree prev, vars;
2648 if (write_virtuals >= 0
2649 && ! DECL_EXTERNAL (vars)
2650 && ((TREE_PUBLIC (vars) && ! DECL_WEAK (vars) && ! DECL_ONE_ONLY (vars))
2651 || CLASSTYPE_EXPLICIT_INSTANTIATION (DECL_CONTEXT (vars))
2652 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars))
2653 || (hack_decl_function_context (vars) && TREE_USED (vars)))
2654 && ! TREE_ASM_WRITTEN (vars))
2656 /* Write it out. */
2657 mark_vtable_entries (vars);
2658 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2659 store_init_value (vars, DECL_INITIAL (vars));
2661 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2663 /* Mark the VAR_DECL node representing the vtable itself as a
2664 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2665 It is rather important that such things be ignored because
2666 any effort to actually generate DWARF for them will run
2667 into trouble when/if we encounter code like:
2669 #pragma interface
2670 struct S { virtual void member (); };
2672 because the artificial declaration of the vtable itself (as
2673 manufactured by the g++ front end) will say that the vtable
2674 is a static member of `S' but only *after* the debug output
2675 for the definition of `S' has already been output. This causes
2676 grief because the DWARF entry for the definition of the vtable
2677 will try to refer back to an earlier *declaration* of the
2678 vtable as a static member of `S' and there won't be one.
2679 We might be able to arrange to have the "vtable static member"
2680 attached to the member list for `S' before the debug info for
2681 `S' get written (which would solve the problem) but that would
2682 require more intrusive changes to the g++ front end. */
2684 DECL_IGNORED_P (vars) = 1;
2687 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2688 return 1;
2690 else if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars)))
2691 /* We don't know what to do with this one yet. */
2692 return 0;
2694 /* We know that PREV must be non-zero here. */
2695 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2696 return 0;
2699 static int
2700 prune_vtable_vardecl (prev, vars)
2701 tree prev, vars;
2703 /* We know that PREV must be non-zero here. */
2704 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2705 return 1;
2709 walk_vtables (typedecl_fn, vardecl_fn)
2710 register void (*typedecl_fn) PROTO ((tree, tree));
2711 register int (*vardecl_fn) PROTO ((tree, tree));
2713 tree prev, vars;
2714 int flag = 0;
2716 for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2718 register tree type = TREE_TYPE (vars);
2720 if (TREE_CODE (vars) == VAR_DECL && DECL_VIRTUAL_P (vars))
2722 if (vardecl_fn)
2723 flag |= (*vardecl_fn) (prev, vars);
2725 if (prev && TREE_CHAIN (prev) != vars)
2726 continue;
2728 else if (TREE_CODE (vars) == TYPE_DECL
2729 && type != error_mark_node
2730 && TYPE_LANG_SPECIFIC (type)
2731 && CLASSTYPE_VSIZE (type))
2733 if (typedecl_fn) (*typedecl_fn) (prev, vars);
2736 prev = vars;
2739 return flag;
2742 static void
2743 finish_sigtable_vardecl (prev, vars)
2744 tree prev, vars;
2746 /* We don't need to mark sigtable entries as addressable here as is done
2747 for vtables. Since sigtables, unlike vtables, are always written out,
2748 that was already done in build_signature_table_constructor. */
2750 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2752 /* We know that PREV must be non-zero here. */
2753 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2756 void
2757 walk_sigtables (typedecl_fn, vardecl_fn)
2758 register void (*typedecl_fn) PROTO((tree, tree));
2759 register void (*vardecl_fn) PROTO((tree, tree));
2761 tree prev, vars;
2763 for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2765 register tree type = TREE_TYPE (vars);
2767 if (TREE_CODE (vars) == TYPE_DECL
2768 && type != error_mark_node
2769 && IS_SIGNATURE (type))
2771 if (typedecl_fn) (*typedecl_fn) (prev, vars);
2773 else if (TREE_CODE (vars) == VAR_DECL
2774 && TREE_TYPE (vars) != error_mark_node
2775 && IS_SIGNATURE (TREE_TYPE (vars)))
2777 if (vardecl_fn) (*vardecl_fn) (prev, vars);
2779 else
2780 prev = vars;
2784 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2785 inline function or template instantiation at end-of-file. */
2787 void
2788 import_export_decl (decl)
2789 tree decl;
2791 if (DECL_INTERFACE_KNOWN (decl))
2792 return;
2794 if (DECL_TEMPLATE_INSTANTIATION (decl))
2796 DECL_NOT_REALLY_EXTERN (decl) = 1;
2797 if (DECL_IMPLICIT_INSTANTIATION (decl)
2798 && (flag_implicit_templates || DECL_THIS_INLINE (decl)))
2800 if (!TREE_PUBLIC (decl))
2801 /* Templates are allowed to have internal linkage. See
2802 [basic.link]. */
2804 else if (TREE_CODE (decl) == FUNCTION_DECL)
2805 comdat_linkage (decl);
2806 else
2807 DECL_COMDAT (decl) = 1;
2809 else
2810 DECL_NOT_REALLY_EXTERN (decl) = 0;
2812 else if (DECL_FUNCTION_MEMBER_P (decl))
2814 tree ctype = DECL_CLASS_CONTEXT (decl);
2815 if (CLASSTYPE_INTERFACE_KNOWN (ctype)
2816 && (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl)))
2818 DECL_NOT_REALLY_EXTERN (decl)
2819 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2820 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2822 else
2823 comdat_linkage (decl);
2825 /* tinfo function */
2826 else if (DECL_ARTIFICIAL (decl) && DECL_MUTABLE_P (decl))
2828 tree ctype = TREE_TYPE (DECL_NAME (decl));
2829 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2830 && TYPE_VIRTUAL_P (ctype))
2832 /* If the type is a cv-qualified variant of a type, then we
2833 must emit the tinfo function in this translation unit
2834 since it will not be emitted when the vtable for the type
2835 is output (which is when the unqualified version is
2836 generated). */
2837 DECL_NOT_REALLY_EXTERN (decl)
2838 = TYPE_READONLY (ctype)
2839 || TYPE_VOLATILE (ctype)
2840 || ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2841 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2843 /* For WIN32 we also want to put explicit instantiations in
2844 linkonce sections. */
2845 if (CLASSTYPE_EXPLICIT_INSTANTIATION (ctype))
2846 maybe_make_one_only (decl);
2848 else if (TYPE_BUILT_IN (ctype) && ctype == TYPE_MAIN_VARIANT (ctype))
2849 DECL_NOT_REALLY_EXTERN (decl) = 0;
2850 else
2851 comdat_linkage (decl);
2853 else
2854 comdat_linkage (decl);
2856 DECL_INTERFACE_KNOWN (decl) = 1;
2859 tree
2860 build_cleanup (decl)
2861 tree decl;
2863 tree temp;
2864 tree type = TREE_TYPE (decl);
2866 if (TREE_CODE (type) == ARRAY_TYPE)
2867 temp = decl;
2868 else
2870 mark_addressable (decl);
2871 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2873 temp = build_delete (TREE_TYPE (temp), temp,
2874 integer_two_node,
2875 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2876 return temp;
2879 extern int parse_time, varconst_time;
2880 extern tree pending_templates;
2881 extern tree maybe_templates;
2883 static tree
2884 get_sentry (base)
2885 tree base;
2887 tree sname = get_id_2 ("__sn", base);
2888 /* For struct X foo __attribute__((weak)), there is a counter
2889 __snfoo. Since base is already an assembler name, sname should
2890 be globally unique */
2891 tree sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2892 if (! sentry)
2894 push_obstacks_nochange ();
2895 end_temporary_allocation ();
2896 sentry = build_decl (VAR_DECL, sname, integer_type_node);
2897 TREE_PUBLIC (sentry) = 1;
2898 DECL_ARTIFICIAL (sentry) = 1;
2899 TREE_STATIC (sentry) = 1;
2900 TREE_USED (sentry) = 1;
2901 DECL_COMMON (sentry) = 1;
2902 pushdecl_top_level (sentry);
2903 cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0, 0);
2904 pop_obstacks ();
2906 return sentry;
2909 /* Start the process of running a particular set of global constructors
2910 or destructors. Subroutine of do_[cd]tors. */
2912 static void
2913 start_objects (method_type)
2914 int method_type;
2916 tree fnname;
2918 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2920 fnname = get_file_function_name (method_type);
2922 start_function (void_list_node,
2923 make_call_declarator (fnname, void_list_node, NULL_TREE,
2924 NULL_TREE),
2925 NULL_TREE, 0);
2927 store_parm_decls ();
2928 pushlevel (0);
2929 clear_last_expr ();
2930 push_momentary ();
2931 expand_start_bindings (0);
2934 /* Finish the process of running a particular set of global constructors
2935 or destructors. Subroutine of do_[cd]tors. */
2937 static void
2938 finish_objects (method_type)
2939 int method_type;
2941 char *fnname;
2943 tree list = (method_type == 'I' ? static_ctors : static_dtors);
2945 if (! current_function_decl && list)
2946 start_objects (method_type);
2948 for (; list; list = TREE_CHAIN (list))
2949 expand_expr_stmt (build_function_call (TREE_VALUE (list), NULL_TREE));
2951 if (! current_function_decl)
2952 return;
2954 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
2956 /* Finish up. */
2957 expand_end_bindings (getdecls (), 1, 0);
2958 poplevel (1, 0, 0);
2959 pop_momentary ();
2960 finish_function (lineno, 0, 0);
2962 if (method_type == 'I')
2963 assemble_constructor (fnname);
2964 else
2965 assemble_destructor (fnname);
2968 /* Generate a function to run a set of global destructors. Subroutine of
2969 finish_file. */
2971 static void
2972 do_dtors ()
2974 tree vars = static_aggregates;
2976 for (; vars; vars = TREE_CHAIN (vars))
2978 tree decl = TREE_VALUE (vars);
2979 tree type = TREE_TYPE (decl);
2980 tree temp;
2982 if (TYPE_NEEDS_DESTRUCTOR (type) && ! TREE_STATIC (vars)
2983 && ! DECL_EXTERNAL (decl))
2985 int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
2986 || DECL_ONE_ONLY (decl)
2987 || DECL_WEAK (decl)));
2989 if (! current_function_decl)
2990 start_objects ('D');
2992 temp = build_cleanup (decl);
2994 if (protect)
2996 tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
2997 sentry = build_unary_op (PREDECREMENT_EXPR, sentry, 0);
2998 sentry = build_binary_op (EQ_EXPR, sentry, integer_zero_node, 1);
2999 expand_start_cond (sentry, 0);
3002 expand_expr_stmt (temp);
3004 if (protect)
3005 expand_end_cond ();
3009 finish_objects ('D');
3012 /* Generate a function to run a set of global constructors. Subroutine of
3013 finish_file. */
3015 static void
3016 do_ctors ()
3018 tree vars = static_aggregates;
3020 /* Reverse the list so it's in the right order for ctors. */
3021 vars = nreverse (vars);
3023 for (; vars; vars = TREE_CHAIN (vars))
3025 tree decl = TREE_VALUE (vars);
3026 tree init = TREE_PURPOSE (vars);
3028 /* If this was a static attribute within some function's scope,
3029 then don't initialize it here. Also, don't bother
3030 with initializers that contain errors. */
3031 if (TREE_STATIC (vars)
3032 || DECL_EXTERNAL (decl)
3033 || (init && TREE_CODE (init) == TREE_LIST
3034 && value_member (error_mark_node, init)))
3035 continue;
3037 if (TREE_CODE (decl) == VAR_DECL)
3039 int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3040 || DECL_ONE_ONLY (decl)
3041 || DECL_WEAK (decl)));
3043 if (! current_function_decl)
3044 start_objects ('I');
3046 /* Set these global variables so that GDB at least puts
3047 us near the declaration which required the initialization. */
3048 input_filename = DECL_SOURCE_FILE (decl);
3049 lineno = DECL_SOURCE_LINE (decl);
3050 emit_note (input_filename, lineno);
3052 /* 9.5p5: The initializer of a static member of a class has
3053 the same access rights as a member function. */
3054 if (member_p (decl))
3056 DECL_CLASS_CONTEXT (current_function_decl)
3057 = DECL_CONTEXT (decl);
3058 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3061 if (protect)
3063 tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3064 sentry = build_unary_op (PREINCREMENT_EXPR, sentry, 0);
3065 sentry = build_binary_op
3066 (EQ_EXPR, sentry, integer_one_node, 1);
3067 expand_start_cond (sentry, 0);
3070 expand_start_target_temps ();
3072 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3073 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3074 expand_aggr_init (decl, init, 0, 0);
3075 else if (TREE_CODE (init) == TREE_VEC)
3077 expand_expr (expand_vec_init (decl, TREE_VEC_ELT (init, 0),
3078 TREE_VEC_ELT (init, 1),
3079 TREE_VEC_ELT (init, 2), 0),
3080 const0_rtx, VOIDmode, EXPAND_NORMAL);
3082 else
3083 expand_assignment (decl, init, 0, 0);
3085 /* The expression might have involved increments and
3086 decrements. */
3087 emit_queue ();
3089 /* Cleanup any temporaries needed for the initial value. */
3090 expand_end_target_temps ();
3092 if (protect)
3093 expand_end_cond ();
3095 DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
3096 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3098 else if (decl == error_mark_node)
3099 /* OK */;
3100 else
3101 my_friendly_abort (22);
3104 finish_objects ('I');
3107 /* This routine is called from the last rule in yyparse ().
3108 Its job is to create all the code needed to initialize and
3109 destroy the global aggregates. We do the destruction
3110 first, since that way we only need to reverse the decls once. */
3112 void
3113 finish_file ()
3115 extern int lineno;
3116 int start_time, this_time;
3118 tree fnname;
3119 tree vars;
3120 int needs_cleaning = 0, needs_messing_up = 0;
3122 at_eof = 1;
3124 /* Bad parse errors. Just forget about it. */
3125 if (! global_bindings_p () || current_class_type)
3126 return;
3128 start_time = get_run_time ();
3130 /* Otherwise, GDB can get confused, because in only knows
3131 about source for LINENO-1 lines. */
3132 lineno -= 1;
3134 interface_unknown = 1;
3135 interface_only = 0;
3137 for (fnname = pending_templates; fnname; fnname = TREE_CHAIN (fnname))
3139 tree srcloc = TREE_PURPOSE (fnname);
3140 tree decl = TREE_VALUE (fnname);
3142 input_filename = SRCLOC_FILE (srcloc);
3143 lineno = SRCLOC_LINE (srcloc);
3145 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
3147 instantiate_class_template (decl);
3148 if (CLASSTYPE_TEMPLATE_INSTANTIATION (decl))
3149 for (vars = TYPE_METHODS (decl); vars; vars = TREE_CHAIN (vars))
3150 if (! DECL_ARTIFICIAL (vars))
3151 instantiate_decl (vars);
3153 else
3154 instantiate_decl (decl);
3157 for (fnname = maybe_templates; fnname; fnname = TREE_CHAIN (fnname))
3159 tree args, fn, decl = TREE_VALUE (fnname);
3161 if (DECL_INITIAL (decl))
3162 continue;
3164 fn = TREE_PURPOSE (fnname);
3165 args = get_bindings (fn, decl, NULL_TREE);
3166 fn = instantiate_template (fn, args);
3167 instantiate_decl (fn);
3170 cat_namespace_levels();
3172 /* Push into C language context, because that's all
3173 we'll need here. */
3174 push_lang_context (lang_name_c);
3176 #if 1
3177 /* The reason for pushing garbage onto the global_binding_level is to
3178 ensure that we can slice out _DECLs which pertain to virtual function
3179 tables. If the last thing pushed onto the global_binding_level was a
3180 virtual function table, then slicing it out would slice away all the
3181 decls (i.e., we lose the head of the chain).
3183 There are several ways of getting the same effect, from changing the
3184 way that iterators over the chain treat the elements that pertain to
3185 virtual function tables, moving the implementation of this code to
3186 decl.c (where we can manipulate global_binding_level directly),
3187 popping the garbage after pushing it and slicing away the vtable
3188 stuff, or just leaving it alone. */
3190 /* Make last thing in global scope not be a virtual function table. */
3191 #if 0 /* not yet, should get fixed properly later */
3192 vars = make_type_decl (get_identifier (" @%$#@!"), integer_type_node);
3193 #else
3194 vars = build_decl (TYPE_DECL, get_identifier (" @%$#@!"), integer_type_node);
3195 #endif
3196 DECL_IGNORED_P (vars) = 1;
3197 SET_DECL_ARTIFICIAL (vars);
3198 pushdecl (vars);
3199 #endif
3201 /* Walk to mark the inline functions we need, then output them so
3202 that we can pick up any other tdecls that those routines need. */
3203 walk_vtables ((void (*) PROTO ((tree, tree))) 0,
3204 finish_prevtable_vardecl);
3206 for (vars = static_aggregates; vars; vars = TREE_CHAIN (vars))
3207 if (! TREE_ASM_WRITTEN (TREE_VALUE (vars)))
3208 rest_of_decl_compilation (TREE_VALUE (vars), 0, 1, 1);
3209 vars = static_aggregates;
3211 if (static_ctors || vars)
3212 needs_messing_up = 1;
3213 if (static_dtors || vars)
3214 needs_cleaning = 1;
3216 /* The aggregates are listed in reverse declaration order, for cleaning. */
3217 if (needs_cleaning)
3219 do_dtors ();
3222 /* do_ctors will reverse the lists for messing up. */
3223 if (needs_messing_up)
3225 do_ctors ();
3228 permanent_allocation (1);
3230 /* Done with C language context needs. */
3231 pop_lang_context ();
3233 /* Now write out any static class variables (which may have since
3234 learned how to be initialized). */
3235 while (pending_statics)
3237 tree decl = TREE_VALUE (pending_statics);
3239 /* Output DWARF debug information. */
3240 #ifdef DWARF_DEBUGGING_INFO
3241 if (write_symbols == DWARF_DEBUG)
3242 dwarfout_file_scope_decl (decl, 1);
3243 #endif
3244 #ifdef DWARF2_DEBUGGING_INFO
3245 if (write_symbols == DWARF2_DEBUG)
3246 dwarf2out_decl (decl);
3247 #endif
3249 DECL_DEFER_OUTPUT (decl) = 0;
3250 rest_of_decl_compilation
3251 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), 1, 1);
3253 pending_statics = TREE_CHAIN (pending_statics);
3256 this_time = get_run_time ();
3257 parse_time -= this_time - start_time;
3258 varconst_time += this_time - start_time;
3260 start_time = get_run_time ();
3262 if (flag_handle_signatures)
3263 walk_sigtables ((void (*) PROTO ((tree, tree))) 0,
3264 finish_sigtable_vardecl);
3266 for (fnname = saved_inlines; fnname; fnname = TREE_CHAIN (fnname))
3268 tree decl = TREE_VALUE (fnname);
3269 import_export_decl (decl);
3272 mark_all_runtime_matches ();
3274 /* Now write out inline functions which had their addresses taken and
3275 which were not declared virtual and which were not declared `extern
3276 inline'. */
3278 int reconsider = 1; /* More may be referenced; check again */
3280 while (reconsider)
3282 tree *p = &saved_inlines;
3283 reconsider = 0;
3285 /* We need to do this each time so that newly completed template
3286 types don't wind up at the front of the list. Sigh. */
3287 vars = build_decl (TYPE_DECL, make_anon_name (), integer_type_node);
3288 DECL_IGNORED_P (vars) = 1;
3289 SET_DECL_ARTIFICIAL (vars);
3290 pushdecl (vars);
3292 reconsider |= walk_vtables ((void (*) PROTO((tree, tree))) 0,
3293 finish_vtable_vardecl);
3295 while (*p)
3297 tree decl = TREE_VALUE (*p);
3299 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3300 && TREE_USED (decl)
3301 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3303 if (DECL_MUTABLE_P (decl))
3304 synthesize_tinfo_fn (decl);
3305 else
3306 synthesize_method (decl);
3307 reconsider = 1;
3310 /* Catch new template instantiations. */
3311 if (decl != TREE_VALUE (*p))
3312 continue;
3314 if (TREE_ASM_WRITTEN (decl)
3315 || (DECL_SAVED_INSNS (decl) == 0 && ! DECL_ARTIFICIAL (decl)))
3316 *p = TREE_CHAIN (*p);
3317 else if (DECL_INITIAL (decl) == 0)
3318 p = &TREE_CHAIN (*p);
3319 else if ((TREE_PUBLIC (decl) && ! DECL_COMDAT (decl))
3320 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
3321 || flag_keep_inline_functions)
3323 if (DECL_NOT_REALLY_EXTERN (decl))
3325 DECL_EXTERNAL (decl) = 0;
3326 reconsider = 1;
3327 /* We can't inline this function after it's been
3328 emitted. We want a variant of
3329 output_inline_function that doesn't prevent
3330 subsequent integration... */
3331 DECL_INLINE (decl) = 0;
3332 output_inline_function (decl);
3333 permanent_allocation (1);
3336 *p = TREE_CHAIN (*p);
3338 else
3339 p = &TREE_CHAIN (*p);
3343 /* It's possible that some of the remaining inlines will still be
3344 needed. For example, a static inline whose address is used in
3345 the initializer for a file-scope static variable will be
3346 needed. Code in compile_file will handle this, but we mustn't
3347 pretend that there are no definitions for the inlines, or it
3348 won't be able to.
3350 FIXME: This won't catch member functions. We should really
3351 unify this stuff with the compile_file stuff. */
3352 for (vars = saved_inlines; vars != NULL_TREE; vars = TREE_CHAIN (vars))
3354 tree decl = TREE_VALUE (vars);
3356 if (DECL_NOT_REALLY_EXTERN (decl)
3357 && !DECL_COMDAT (decl)
3358 && DECL_INITIAL (decl) != NULL_TREE)
3359 DECL_EXTERNAL (decl) = 0;
3363 /* Now delete from the chain of variables all virtual function tables.
3364 We output them all ourselves, because each will be treated specially. */
3366 walk_vtables ((void (*) PROTO((tree, tree))) 0,
3367 prune_vtable_vardecl);
3369 if (write_virtuals == 2)
3371 /* Now complain about an virtual function tables promised
3372 but not delivered. */
3373 while (pending_vtables)
3375 if (TREE_PURPOSE (pending_vtables) == NULL_TREE)
3376 error ("virtual function table for `%s' not defined",
3377 IDENTIFIER_POINTER (TREE_VALUE (pending_vtables)));
3378 pending_vtables = TREE_CHAIN (pending_vtables);
3382 finish_repo ();
3384 this_time = get_run_time ();
3385 parse_time -= this_time - start_time;
3386 varconst_time += this_time - start_time;
3388 if (flag_detailed_statistics)
3390 dump_tree_statistics ();
3391 dump_time_statistics ();
3395 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3396 expr. Since it was parsed like a type, we need to wade through and fix
3397 that. Unfortunately, since operator() is left-associative, we can't use
3398 tail recursion. In the above example, TYPE is `A', and DECL is
3399 `()()()()()'.
3401 Maybe this shouldn't be recursive, but how often will it actually be
3402 used? (jason) */
3404 tree
3405 reparse_absdcl_as_expr (type, decl)
3406 tree type, decl;
3408 /* do build_functional_cast (type, NULL_TREE) at bottom */
3409 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3410 return build_functional_cast (type, NULL_TREE);
3412 /* recurse */
3413 decl = reparse_decl_as_expr (type, TREE_OPERAND (decl, 0));
3415 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3417 if (TREE_CODE (decl) == CALL_EXPR && TREE_TYPE (decl) != void_type_node)
3418 decl = require_complete_type (decl);
3420 return decl;
3423 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3424 out to be an expr. Since it was parsed like a type, we need to wade
3425 through and fix that. Since casts are right-associative, we are
3426 reversing the order, so we don't have to recurse.
3428 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3429 `1'. */
3431 tree
3432 reparse_absdcl_as_casts (decl, expr)
3433 tree decl, expr;
3435 tree type;
3437 if (TREE_CODE (expr) == CONSTRUCTOR
3438 && TREE_TYPE (expr) == 0)
3440 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3441 decl = TREE_OPERAND (decl, 0);
3443 if (IS_SIGNATURE (type))
3445 error ("cast specifies signature type");
3446 return error_mark_node;
3449 expr = digest_init (type, expr, (tree *) 0);
3450 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
3452 int failure = complete_array_type (type, expr, 1);
3453 if (failure)
3454 my_friendly_abort (78);
3458 while (decl)
3460 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3461 decl = TREE_OPERAND (decl, 0);
3462 expr = build_c_cast (type, expr);
3465 if (warn_old_style_cast)
3466 warning ("use of old-style cast");
3468 return expr;
3471 /* Given plain tree nodes for an expression, build up the full semantics. */
3473 tree
3474 build_expr_from_tree (t)
3475 tree t;
3477 if (t == NULL_TREE || t == error_mark_node)
3478 return t;
3480 switch (TREE_CODE (t))
3482 case IDENTIFIER_NODE:
3483 return do_identifier (t, 0);
3485 case LOOKUP_EXPR:
3486 if (LOOKUP_EXPR_GLOBAL (t))
3487 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3488 else
3489 return do_identifier (TREE_OPERAND (t, 0), 0);
3491 case TEMPLATE_ID_EXPR:
3492 return (lookup_template_function
3493 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3494 build_expr_from_tree (TREE_OPERAND (t, 1))));
3496 case INDIRECT_REF:
3497 return build_x_indirect_ref
3498 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3500 case CAST_EXPR:
3501 return build_functional_cast
3502 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3504 case REINTERPRET_CAST_EXPR:
3505 return build_reinterpret_cast
3506 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3508 case CONST_CAST_EXPR:
3509 return build_const_cast
3510 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3512 case DYNAMIC_CAST_EXPR:
3513 return build_dynamic_cast
3514 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3516 case STATIC_CAST_EXPR:
3517 return build_static_cast
3518 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3520 case PREDECREMENT_EXPR:
3521 case PREINCREMENT_EXPR:
3522 case POSTDECREMENT_EXPR:
3523 case POSTINCREMENT_EXPR:
3524 case NEGATE_EXPR:
3525 case BIT_NOT_EXPR:
3526 case ABS_EXPR:
3527 case TRUTH_NOT_EXPR:
3528 case ADDR_EXPR:
3529 case CONVERT_EXPR: /* Unary + */
3530 if (TREE_TYPE (t))
3531 return t;
3532 return build_x_unary_op (TREE_CODE (t),
3533 build_expr_from_tree (TREE_OPERAND (t, 0)));
3535 case PLUS_EXPR:
3536 case MINUS_EXPR:
3537 case MULT_EXPR:
3538 case TRUNC_DIV_EXPR:
3539 case CEIL_DIV_EXPR:
3540 case FLOOR_DIV_EXPR:
3541 case ROUND_DIV_EXPR:
3542 case EXACT_DIV_EXPR:
3543 case BIT_AND_EXPR:
3544 case BIT_ANDTC_EXPR:
3545 case BIT_IOR_EXPR:
3546 case BIT_XOR_EXPR:
3547 case TRUNC_MOD_EXPR:
3548 case FLOOR_MOD_EXPR:
3549 case TRUTH_ANDIF_EXPR:
3550 case TRUTH_ORIF_EXPR:
3551 case TRUTH_AND_EXPR:
3552 case TRUTH_OR_EXPR:
3553 case RSHIFT_EXPR:
3554 case LSHIFT_EXPR:
3555 case RROTATE_EXPR:
3556 case LROTATE_EXPR:
3557 case EQ_EXPR:
3558 case NE_EXPR:
3559 case MAX_EXPR:
3560 case MIN_EXPR:
3561 case LE_EXPR:
3562 case GE_EXPR:
3563 case LT_EXPR:
3564 case GT_EXPR:
3565 case MEMBER_REF:
3566 return build_x_binary_op
3567 (TREE_CODE (t),
3568 build_expr_from_tree (TREE_OPERAND (t, 0)),
3569 build_expr_from_tree (TREE_OPERAND (t, 1)));
3571 case DOTSTAR_EXPR:
3572 return build_m_component_ref
3573 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3574 build_expr_from_tree (TREE_OPERAND (t, 1)));
3576 case SCOPE_REF:
3577 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3579 case ARRAY_REF:
3580 if (TREE_OPERAND (t, 0) == NULL_TREE)
3581 /* new-type-id */
3582 return build_parse_node (ARRAY_REF, NULL_TREE,
3583 build_expr_from_tree (TREE_OPERAND (t, 1)));
3584 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3585 build_expr_from_tree (TREE_OPERAND (t, 1)));
3587 case SIZEOF_EXPR:
3588 case ALIGNOF_EXPR:
3590 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3591 if (TREE_CODE_CLASS (TREE_CODE (r)) != 't')
3592 r = TREE_TYPE (r);
3593 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3596 case MODOP_EXPR:
3597 return build_x_modify_expr
3598 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3599 TREE_CODE (TREE_OPERAND (t, 1)),
3600 build_expr_from_tree (TREE_OPERAND (t, 2)));
3602 case ARROW_EXPR:
3603 return build_x_arrow
3604 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3606 case NEW_EXPR:
3607 return build_new
3608 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3609 build_expr_from_tree (TREE_OPERAND (t, 1)),
3610 build_expr_from_tree (TREE_OPERAND (t, 2)),
3611 NEW_EXPR_USE_GLOBAL (t));
3613 case DELETE_EXPR:
3614 return delete_sanity
3615 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3616 build_expr_from_tree (TREE_OPERAND (t, 1)),
3617 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3619 case COMPOUND_EXPR:
3620 if (TREE_OPERAND (t, 1) == NULL_TREE)
3621 return build_x_compound_expr
3622 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3623 else
3624 my_friendly_abort (42);
3626 case METHOD_CALL_EXPR:
3627 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3629 tree ref = TREE_OPERAND (t, 0);
3630 return build_scoped_method_call
3631 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3632 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3633 TREE_OPERAND (ref, 1),
3634 build_expr_from_tree (TREE_OPERAND (t, 2)));
3636 return build_method_call
3637 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3638 TREE_OPERAND (t, 0),
3639 build_expr_from_tree (TREE_OPERAND (t, 2)),
3640 NULL_TREE, LOOKUP_NORMAL);
3642 case CALL_EXPR:
3643 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3645 tree ref = TREE_OPERAND (t, 0);
3646 return build_member_call
3647 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3648 TREE_OPERAND (ref, 1),
3649 build_expr_from_tree (TREE_OPERAND (t, 1)));
3651 else
3653 tree name = TREE_OPERAND (t, 0);
3654 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3655 || ! really_overloaded_fn (name))
3656 name = build_expr_from_tree (name);
3657 return build_x_function_call
3658 (name, build_expr_from_tree (TREE_OPERAND (t, 1)),
3659 current_class_ref);
3662 case COND_EXPR:
3663 return build_x_conditional_expr
3664 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3665 build_expr_from_tree (TREE_OPERAND (t, 1)),
3666 build_expr_from_tree (TREE_OPERAND (t, 2)));
3668 case TREE_LIST:
3670 tree purpose, value, chain;
3672 if (t == void_list_node)
3673 return t;
3675 purpose = TREE_PURPOSE (t);
3676 if (purpose)
3677 purpose = build_expr_from_tree (purpose);
3678 value = TREE_VALUE (t);
3679 if (value)
3680 value = build_expr_from_tree (value);
3681 chain = TREE_CHAIN (t);
3682 if (chain && chain != void_type_node)
3683 chain = build_expr_from_tree (chain);
3684 return expr_tree_cons (purpose, value, chain);
3687 case COMPONENT_REF:
3688 return build_x_component_ref
3689 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3690 TREE_OPERAND (t, 1), NULL_TREE, 1);
3692 case THROW_EXPR:
3693 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3695 case CONSTRUCTOR:
3697 tree r;
3699 /* digest_init will do the wrong thing if we let it. */
3700 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3701 return t;
3703 r = build_nt (CONSTRUCTOR, NULL_TREE,
3704 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
3706 if (TREE_TYPE (t))
3707 return digest_init (TREE_TYPE (t), r, 0);
3708 return r;
3711 case TYPEID_EXPR:
3712 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 0))) == 't')
3713 return get_typeid (TREE_OPERAND (t, 0));
3714 return build_x_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3716 case VAR_DECL:
3717 return convert_from_reference (t);
3719 default:
3720 return t;
3724 /* This is something of the form `int (*a)++' that has turned out to be an
3725 expr. It was only converted into parse nodes, so we need to go through
3726 and build up the semantics. Most of the work is done by
3727 build_expr_from_tree, above.
3729 In the above example, TYPE is `int' and DECL is `*a'. */
3731 tree
3732 reparse_decl_as_expr (type, decl)
3733 tree type, decl;
3735 decl = build_expr_from_tree (decl);
3736 if (type)
3737 return build_functional_cast (type, build_expr_list (NULL_TREE, decl));
3738 else
3739 return decl;
3742 /* This is something of the form `int (*a)' that has turned out to be a
3743 decl. It was only converted into parse nodes, so we need to do the
3744 checking that make_{pointer,reference}_declarator do. */
3746 tree
3747 finish_decl_parsing (decl)
3748 tree decl;
3750 extern int current_class_depth;
3752 switch (TREE_CODE (decl))
3754 case IDENTIFIER_NODE:
3755 return decl;
3756 case INDIRECT_REF:
3757 return make_pointer_declarator
3758 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3759 case ADDR_EXPR:
3760 return make_reference_declarator
3761 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3762 case BIT_NOT_EXPR:
3763 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3764 return decl;
3765 case SCOPE_REF:
3766 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
3767 TREE_COMPLEXITY (decl) = current_class_depth;
3768 return decl;
3769 case ARRAY_REF:
3770 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3771 return decl;
3772 default:
3773 my_friendly_abort (5);
3774 return NULL_TREE;
3778 tree
3779 check_cp_case_value (value)
3780 tree value;
3782 if (value == NULL_TREE)
3783 return value;
3785 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3786 STRIP_TYPE_NOPS (value);
3788 if (TREE_READONLY_DECL_P (value))
3790 value = decl_constant_value (value);
3791 STRIP_TYPE_NOPS (value);
3793 value = fold (value);
3795 if (TREE_CODE (value) != INTEGER_CST
3796 && value != error_mark_node)
3798 cp_error ("case label `%E' does not reduce to an integer constant",
3799 value);
3800 value = error_mark_node;
3802 else
3803 /* Promote char or short to int. */
3804 value = default_conversion (value);
3806 constant_expression_warning (value);
3808 return value;
3811 /* Return 1 if root encloses child. */
3813 static int
3814 is_namespace_ancestor (root, child)
3815 tree root, child;
3817 if (root == child)
3818 return 1;
3819 if (root == global_namespace)
3820 return 1;
3821 if (child == global_namespace)
3822 return 0;
3823 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
3827 /* Return the namespace that is the common ancestor
3828 of two given namespaces. */
3830 static tree
3831 namespace_ancestor (ns1, ns2)
3832 tree ns1, ns2;
3834 if (is_namespace_ancestor (ns1, ns2))
3835 return ns1;
3836 return namespace_ancestor (DECL_CONTEXT (ns1), ns2);
3839 /* Insert used into the using list of user. Set indirect_flag if this
3840 directive is not directly from the source. Also find the common
3841 ancestor and let our users know about the new namespace */
3842 static void
3843 add_using_namespace (user, used, indirect)
3844 tree user;
3845 tree used;
3846 int indirect;
3848 tree iter;
3849 /* Using oneself is a no-op. */
3850 if (user == used)
3851 return;
3852 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
3853 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
3854 /* Check if we already have this. */
3855 if (purpose_member (used, DECL_NAMESPACE_USING (user)) != NULL_TREE)
3856 return;
3858 /* Add used to the user's using list. */
3859 DECL_NAMESPACE_USING (user)
3860 = perm_tree_cons (used, namespace_ancestor (user, used),
3861 DECL_NAMESPACE_USING (user));
3863 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3865 /* Add user to the used's users list. */
3866 DECL_NAMESPACE_USERS (used)
3867 = perm_tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3869 for (iter = DECL_NAMESPACE_USERS (user); iter; iter = TREE_CHAIN (iter))
3870 /* indirect usage */
3871 add_using_namespace (TREE_PURPOSE (iter), used, 1);
3874 /* Combines two sets of overloaded functions into an OVERLOAD chain.
3875 The first list becomes the tail of the result. */
3877 static tree
3878 merge_functions (s1, s2)
3879 tree s1;
3880 tree s2;
3882 if (TREE_CODE (s2) == OVERLOAD)
3883 while (s2)
3885 s1 = build_overload (OVL_FUNCTION (s2), s1);
3886 s2 = OVL_CHAIN (s2);
3888 else
3889 s1 = build_overload (s2, s1);
3890 return s1;
3893 /* This should return an error not all definitions define functions.
3894 It is not an error if we find two functions with exactly the
3895 same signature, only if these are selected in overload resolution.
3896 old is the current set of bindings, new the freshly-found binding.
3897 XXX Do we want to give *all* candidates in case of ambiguity?
3898 XXX In what way should I treat extern declarations?
3899 XXX I don't want to repeat the entire duplicate_decls here */
3901 static tree
3902 ambiguous_decl (name, old, new)
3903 tree name;
3904 tree old;
3905 tree new;
3907 my_friendly_assert (old != NULL_TREE, 393);
3908 /* Copy the value. */
3909 if (!BINDING_VALUE (old))
3910 BINDING_VALUE (old) = BINDING_VALUE (new);
3911 else if (BINDING_VALUE (new)
3912 && BINDING_VALUE (new) != BINDING_VALUE (old))
3914 if (is_overloaded_fn (BINDING_VALUE (old))
3915 && is_overloaded_fn (BINDING_VALUE (new)))
3917 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
3918 BINDING_VALUE (new));
3920 else
3922 /* Some declarations are functions, some are not. */
3923 cp_error ("ambiguous definition `%D' used", name);
3924 cp_error_at ("first definition here", BINDING_VALUE (old));
3925 cp_error_at ("other definition here", BINDING_VALUE (new));
3926 return error_mark_node;
3929 /* ... and copy the type. */
3930 if (!BINDING_TYPE (old))
3931 BINDING_TYPE (old) = BINDING_TYPE (new);
3932 else if(BINDING_TYPE (new)
3933 && BINDING_TYPE (old) != BINDING_TYPE (new))
3935 cp_error ("`%D' denotes an ambiguous type",name);
3936 cp_error_at ("first type here", BINDING_TYPE (old));
3937 cp_error_at ("other type here", BINDING_TYPE (new));
3939 return old;
3942 /* Add the bindings of name in used namespaces to val.
3943 The using list is defined by current, and the lookup goes to scope.
3944 Returns zero on errors. */
3947 lookup_using_namespace (name, val, current, scope)
3948 tree name, val, current, scope;
3950 tree iter;
3951 tree val1;
3952 /* Iterate over all namespaces from current to scope. */
3953 while (val != error_mark_node)
3955 /* Iterate over all used namespaces in current, searching for
3956 using directives of scope. */
3957 for (iter = DECL_NAMESPACE_USING (current);
3958 iter; iter = TREE_CHAIN (iter))
3959 if (TREE_VALUE (iter) == scope)
3961 val1 = binding_for_name (name, TREE_PURPOSE (iter));
3962 /* Resolve ambiguities. */
3963 val = ambiguous_decl (name, val, val1);
3965 if (current == scope)
3966 break;
3967 current = CP_DECL_CONTEXT (current);
3969 return val != error_mark_node;
3972 /* [namespace.qual]
3973 Excepts the name to lookup and its qualifying scope.
3974 Returns the name/type pair found into the CPLUS_BINDING result,
3975 or 0 on error. */
3978 qualified_lookup_using_namespace (name, scope, result)
3979 tree name;
3980 tree scope;
3981 tree result;
3983 /* Maintain a list of namespaces visited... */
3984 tree seen = NULL_TREE;
3985 /* ... and a list of namespace yet to see. */
3986 tree todo = NULL_TREE;
3987 tree usings;
3988 while (scope && (result != error_mark_node))
3990 seen = temp_tree_cons (scope, NULL_TREE, seen);
3991 result = ambiguous_decl (name, result, binding_for_name (name, scope));
3992 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
3993 /* Consider using directives. */
3994 for (usings = DECL_NAMESPACE_USING (scope); usings;
3995 usings = TREE_CHAIN (usings))
3996 /* If this was a real directive, and we have not seen it. */
3997 if (!TREE_INDIRECT_USING (usings)
3998 && !purpose_member (TREE_PURPOSE (usings), seen))
3999 todo = temp_tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4000 if (todo)
4002 scope = TREE_PURPOSE (todo);
4003 todo = TREE_CHAIN (todo);
4005 else
4006 scope = NULL_TREE; /* If there never was a todo list. */
4008 return result != error_mark_node;
4011 /* [namespace.memdef]/2 */
4013 /* Set the context of a declaration to scope. Complain if we are not
4014 outside scope. */
4016 void
4017 set_decl_namespace (decl, scope)
4018 tree decl;
4019 tree scope;
4021 tree old;
4022 if (scope == std_node)
4023 scope = global_namespace;
4024 /* Get rid of namespace aliases. */
4025 scope = ORIGINAL_NAMESPACE (scope);
4027 if (!is_namespace_ancestor (current_namespace, scope))
4028 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4029 decl, scope);
4030 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4031 if (scope != current_namespace)
4033 /* See whether this has been declared in the namespace. */
4034 old = namespace_binding (DECL_NAME (decl), scope);
4035 if (!old)
4036 /* No old declaration at all. */
4037 goto complain;
4038 if (!is_overloaded_fn (decl))
4039 /* Don't compare non-function decls with decls_match here,
4040 since it can't check for the correct constness at this
4041 point. pushdecl will find those errors later. */
4042 return;
4043 /* Since decl is a function, old should contain a function decl. */
4044 if (!is_overloaded_fn (old))
4045 goto complain;
4046 for (; old; old = OVL_NEXT (old))
4047 if (decls_match (decl, OVL_CURRENT (old)))
4048 return;
4050 else
4051 return;
4052 complain:
4053 cp_error ("`%D' should have been declared inside `%D'",
4054 decl, scope);
4057 /* Compute the namespace where a declaration is defined. */
4059 tree
4060 decl_namespace (decl)
4061 tree decl;
4063 while (DECL_CONTEXT (decl))
4065 decl = DECL_CONTEXT (decl);
4066 if (TREE_CODE (decl) == NAMESPACE_DECL)
4067 return decl;
4068 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
4069 decl = TYPE_STUB_DECL (decl);
4070 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 390);
4073 return global_namespace;
4076 /* Return the namespace where the current declaration is declared. */
4078 tree
4079 current_decl_namespace ()
4081 tree result;
4082 /* If we have been pushed into a different namespace, use it. */
4083 if (decl_namespace_list)
4084 return TREE_PURPOSE (decl_namespace_list);
4086 if (current_class_type)
4087 if (CLASSTYPE_USE_TEMPLATE (current_class_type))
4088 result = decl_namespace (CLASSTYPE_TI_TEMPLATE (current_class_type));
4089 else
4090 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4091 else if (current_function_decl)
4092 if (DECL_USE_TEMPLATE (current_function_decl))
4093 result = decl_namespace (DECL_TI_TEMPLATE (current_function_decl));
4094 else
4095 result = decl_namespace (current_function_decl);
4096 else
4097 result = current_namespace;
4098 return result;
4101 /* Temporarily set the namespace for the current declaration. */
4103 void
4104 push_decl_namespace (decl)
4105 tree decl;
4107 if (TREE_CODE (decl) != NAMESPACE_DECL)
4108 decl = decl_namespace (decl);
4109 decl_namespace_list = tree_cons (decl, NULL_TREE, decl_namespace_list);
4112 void
4113 pop_decl_namespace ()
4115 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4118 /* [basic.lookup.koenig] */
4119 /* A non-zero return value in the functions below indicates an error.
4120 All nodes allocated in the procedure are on the scratch obstack. */
4122 struct arg_lookup
4124 tree name;
4125 tree namespaces;
4126 tree classes;
4127 tree functions;
4130 static int arg_assoc PROTO((struct arg_lookup*, tree));
4131 static int arg_assoc_args PROTO((struct arg_lookup*, tree));
4133 /* Add a function to the lookup structure. */
4135 static int
4136 add_function (k, fn)
4137 struct arg_lookup *k;
4138 tree fn;
4140 if (ovl_member (fn, k->functions))
4141 return 0;
4142 k->functions = build_overload (fn, k->functions);
4143 return 0;
4146 /* Add functions of a namespace to the lookup structure. */
4148 static int
4149 arg_assoc_namespace (k, scope)
4150 struct arg_lookup *k;
4151 tree scope;
4153 tree value;
4155 if (purpose_member (scope, k->namespaces))
4156 return 0;
4157 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4159 value = namespace_binding (k->name, scope);
4160 if (!value)
4161 return 0;
4163 if (!is_overloaded_fn (value))
4165 cp_error_at ("`%D' is not a function", value);
4166 cp_error ("in call to `%D'", k->name);
4167 return 1;
4170 for (; value; value = OVL_NEXT (value))
4171 if (add_function (k, OVL_CURRENT (value)))
4172 return 1;
4174 return 0;
4177 /* Adds everything associated with class to the lookup structure. */
4179 static int
4180 arg_assoc_class (k, type)
4181 struct arg_lookup* k;
4182 tree type;
4184 tree list, friends, context;
4185 int i;
4187 if (purpose_member (type, k->classes))
4188 return 0;
4189 k->classes = tree_cons (type, NULL_TREE, k->classes);
4191 context = decl_namespace (TYPE_MAIN_DECL (type));
4192 if (arg_assoc_namespace (k, context))
4193 return 1;
4195 /* Process baseclasses. */
4196 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4197 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4198 return 1;
4200 /* Process friends. */
4201 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4202 list = TREE_CHAIN (list))
4203 if (k->name == TREE_PURPOSE (list))
4204 for (friends = TREE_VALUE (list); friends;
4205 friends = TREE_CHAIN (friends))
4206 /* Only interested in global functions with potentially hidden
4207 (i.e. unqualified) declarations. */
4208 if (TREE_PURPOSE (list) == error_mark_node && TREE_VALUE (list)
4209 && decl_namespace (TREE_VALUE (list)) == context)
4210 if (add_function (k, TREE_VALUE (list)))
4211 return 1;
4212 return 0;
4215 /* Adds everything associated with a given type. */
4217 static int
4218 arg_assoc_type (k, type)
4219 struct arg_lookup *k;
4220 tree type;
4222 switch (TREE_CODE (type))
4224 case VOID_TYPE:
4225 case INTEGER_TYPE:
4226 case REAL_TYPE:
4227 case COMPLEX_TYPE:
4228 case CHAR_TYPE:
4229 case BOOLEAN_TYPE:
4230 return 0;
4231 case RECORD_TYPE:
4232 if (TYPE_PTRMEMFUNC_P (type))
4233 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4234 return arg_assoc_class (k, type);
4235 case POINTER_TYPE:
4236 case REFERENCE_TYPE:
4237 case ARRAY_TYPE:
4238 return arg_assoc_type (k, TREE_TYPE (type));
4239 case UNION_TYPE:
4240 case ENUMERAL_TYPE:
4241 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4242 case OFFSET_TYPE:
4243 /* Pointer to member: associate class type and value type. */
4244 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4245 return 1;
4246 return arg_assoc_type (k, TREE_TYPE (type));
4247 case METHOD_TYPE:
4248 /* Associate the class of the method. */
4249 if (arg_assoc_type (k, TYPE_METHOD_BASETYPE (type)))
4250 return 1;
4251 /* Fall through. */
4252 case FUNCTION_TYPE:
4253 /* Associate the parameter types. */
4254 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4255 return 1;
4256 /* Associate the return type. */
4257 return arg_assoc_type (k, TREE_TYPE (type));
4258 case LANG_TYPE:
4259 if (type == unknown_type_node)
4260 return 0;
4261 /* else fall through */
4262 default:
4263 my_friendly_abort (390);
4265 return 0;
4268 /* Adds everything associated with arguments. */
4270 static int
4271 arg_assoc_args (k, args)
4272 struct arg_lookup* k;
4273 tree args;
4275 for (; args; args = TREE_CHAIN (args))
4276 if (arg_assoc (k, TREE_VALUE (args)))
4277 return 1;
4278 return 0;
4281 /* Adds everything associated with a given tree_node. */
4283 static int
4284 arg_assoc (k, n)
4285 struct arg_lookup* k;
4286 tree n;
4288 switch (TREE_CODE_CLASS (TREE_CODE (n)))
4290 case 't':
4291 return arg_assoc_type (k, n);
4292 case 'c':
4293 case '1':
4294 case '2':
4295 case '<':
4296 case 'r':
4297 return arg_assoc_type (k, TREE_TYPE (n));
4298 case 'e':
4299 switch (TREE_CODE (n))
4301 case ADDR_EXPR:
4302 /* special processing */
4303 break;
4304 default:
4305 return arg_assoc_type (k, TREE_TYPE (n));
4307 default:
4308 break;
4311 while (n)
4312 switch (TREE_CODE (n))
4314 case CONST_DECL: /* 'd' */
4315 case VAR_DECL:
4316 case PARM_DECL:
4317 case RESULT_DECL:
4318 return arg_assoc_type (k, TREE_TYPE (n));
4319 case ADDR_EXPR: /* 'e' */
4320 /* We can't use the TREE_TYPE, as the type of an overloaded function
4321 will be useless here. */
4322 n = TREE_OPERAND (n, 0);
4323 continue;
4324 case OVERLOAD: /* 'x' */
4325 if (arg_assoc (k, OVL_CURRENT (n)))
4326 return 1;
4327 n = OVL_NEXT (n);
4328 continue;
4329 case TREE_LIST: /* 'x' */
4330 /* XXX Overloaded member, should get an OVERLOAD directly, here. */
4331 n = TREE_VALUE (n);
4332 continue;
4333 case FUNCTION_DECL: /* 'd' */
4334 if (arg_assoc_args (k, FUNCTION_ARG_CHAIN (n)))
4335 return 1;
4336 if (DECL_FUNCTION_MEMBER_P (n))
4337 if (arg_assoc_type (k, DECL_CLASS_CONTEXT (n)))
4338 return 1;
4339 return 0;
4340 default:
4341 cp_error ("sorry, Koenig lookup for `%s' of type `%T' failed",
4342 tree_code_name [(int)TREE_CODE (n)], TREE_TYPE (n));
4343 my_friendly_abort (391);
4345 return 0;
4348 /* Performs Koenig lookup depending on arguments, where fns
4349 are the functions found in normal lookup. */
4351 tree
4352 lookup_arg_dependent (name, fns, args)
4353 tree name;
4354 tree fns;
4355 tree args;
4357 struct arg_lookup k;
4358 k.name = name;
4359 k.functions = fns;
4360 k.namespaces = NULL_TREE;
4361 k.classes = NULL_TREE;
4363 push_scratch_obstack ();
4364 arg_assoc_args (&k, args);
4365 pop_obstacks ();
4366 return k.functions;
4369 /* Process a namespace-alias declaration. */
4371 void
4372 do_namespace_alias (alias, namespace)
4373 tree alias, namespace;
4375 tree binding;
4376 tree old;
4378 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4380 /* The parser did not find it, so it's not there. */
4381 cp_error ("unknown namespace `%D'", namespace);
4382 return;
4385 namespace = ORIGINAL_NAMESPACE (namespace);
4387 binding = binding_for_name (alias, current_namespace);
4388 old = BINDING_VALUE (binding);
4389 if (old)
4391 if (TREE_CODE (old) == NAMESPACE_DECL
4392 && DECL_NAMESPACE_ALIAS (old) == namespace)
4393 /* Ok: redeclaration. */
4394 return;
4395 cp_error ("invalid namespace alias `%D'", alias);
4396 cp_error_at ("`%D' previously declared here", old);
4398 else
4400 /* Build the alias. */
4401 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4402 DECL_NAMESPACE_ALIAS (alias) = namespace;
4403 DECL_CONTEXT (alias) = FROB_CONTEXT (current_namespace);
4404 BINDING_VALUE (binding) = alias;
4408 /* Process a using-declaration not appearing in class or local scope. */
4410 void
4411 do_toplevel_using_decl (decl)
4412 tree decl;
4414 tree scope, name, binding, decls, newval, newtype;
4415 struct tree_binding _decls;
4417 if (TREE_CODE (decl) == SCOPE_REF
4418 && TREE_OPERAND (decl, 0) == std_node)
4419 return;
4420 if (TREE_CODE (decl) == SCOPE_REF)
4422 scope = TREE_OPERAND (decl, 0);
4423 name = TREE_OPERAND (decl, 1);
4425 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4426 || TREE_CODE (decl) == TYPE_DECL)
4428 scope = global_namespace;
4429 name = decl;
4431 else
4432 my_friendly_abort (382);
4433 if (TREE_CODE_CLASS (TREE_CODE (name)) == 'd')
4434 name = DECL_NAME (name);
4435 /* Make a USING_DECL. */
4436 decl = push_using_decl (scope, name);
4437 if (!decl)
4438 return;
4440 binding = binding_for_name (name, current_namespace);
4441 decls = binding_init (&_decls);
4442 if (!qualified_lookup_using_namespace (name, scope, decls))
4443 /* Lookup error */
4444 return;
4446 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4448 cp_error ("`%D' not declared", name);
4449 return;
4451 newval = newtype = NULL_TREE;
4453 /* Check for using functions. */
4454 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4456 tree oldval = BINDING_VALUE (binding);
4457 tree tmp, tmp1;
4458 newval = oldval;
4459 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4462 /* Compare each new function with each old one.
4463 If the old function was also used, there is no conflict. */
4464 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4465 if (OVL_CURRENT (tmp) == OVL_CURRENT (tmp1))
4466 break;
4467 else if (OVL_USED (tmp1))
4468 continue;
4469 else if (duplicate_decls (OVL_CURRENT (tmp), OVL_CURRENT (tmp1)))
4470 return;
4472 /* Duplicate use, ignore */
4473 if (tmp1)
4474 continue;
4476 newval = build_overload (OVL_CURRENT (tmp), newval);
4477 if (TREE_CODE (newval) != OVERLOAD)
4478 newval = ovl_cons (newval, NULL_TREE);
4479 OVL_USED (newval) = 1;
4482 else
4484 tree oldval = BINDING_VALUE (binding);
4485 newval = BINDING_VALUE (decls);
4486 if (oldval && oldval != newval && !duplicate_decls (newval, oldval))
4487 newval = oldval;
4490 newtype = BINDING_TYPE (decls);
4491 if (BINDING_TYPE (binding) && newtype && BINDING_TYPE (binding) != newtype)
4493 cp_error ("using directive `%D' introduced ambiguous type `%T'",
4494 name, BINDING_TYPE (decls));
4495 return;
4497 /* Copy declarations found. */
4498 if (newval)
4499 BINDING_VALUE (binding) = newval;
4500 if (newtype)
4501 BINDING_TYPE (binding) = newtype;
4502 return;
4505 tree
4506 do_class_using_decl (decl)
4507 tree decl;
4509 tree name, value;
4511 if (TREE_CODE (decl) != SCOPE_REF
4512 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (decl, 0))) != 't')
4514 cp_error ("using-declaration for non-member at class scope");
4515 return NULL_TREE;
4517 name = TREE_OPERAND (decl, 1);
4518 if (TREE_CODE (name) == BIT_NOT_EXPR)
4520 cp_error ("using-declaration for destructor");
4521 return NULL_TREE;
4524 value = build_lang_field_decl (USING_DECL, name, void_type_node);
4525 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4526 return value;
4529 /* Process a using-directive. */
4531 void
4532 do_using_directive (namespace)
4533 tree namespace;
4535 if (namespace == std_node)
4536 return;
4537 if (!toplevel_bindings_p ())
4539 sorry ("using directives inside functions");
4540 return;
4542 /* using namespace A::B::C; */
4543 if (TREE_CODE (namespace) == SCOPE_REF)
4544 namespace = TREE_OPERAND (namespace, 1);
4545 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4547 /* Lookup in lexer did not find a namespace. */
4548 cp_error ("namespace `%T' undeclared", namespace);
4549 return;
4551 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4553 cp_error ("`%T' is not a namespace", namespace);
4554 return;
4556 namespace = ORIGINAL_NAMESPACE (namespace);
4557 /* direct usage */
4558 add_using_namespace (current_namespace, namespace, 0);
4561 void
4562 check_default_args (x)
4563 tree x;
4565 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4566 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4567 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4569 if (TREE_PURPOSE (arg))
4570 saw_def = 1;
4571 else if (saw_def)
4573 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4574 i, x);
4575 break;
4580 void
4581 mark_used (decl)
4582 tree decl;
4584 TREE_USED (decl) = 1;
4585 if (processing_template_decl)
4586 return;
4587 assemble_external (decl);
4588 /* Is it a synthesized method that needs to be synthesized? */
4589 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CLASS_CONTEXT (decl)
4590 && DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
4591 /* Kludge: don't synthesize for default args. */
4592 && current_function_decl)
4593 synthesize_method (decl);
4594 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4595 instantiate_decl (decl);
4598 /* Helper function for named_class_head_sans_basetype nonterminal. */
4600 tree
4601 handle_class_head (aggr, scope, id)
4602 tree aggr, scope, id;
4604 if (TREE_CODE (id) == TYPE_DECL)
4605 return id;
4607 if (scope)
4608 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
4609 else
4610 cp_error ("no file-scope type named `%D'", id);
4612 id = xref_tag
4613 (aggr, make_anon_name (), NULL_TREE, 1);
4614 return TYPE_MAIN_DECL (id);