bb7071e522111a602cc4c5acb734d15afc0a53fb
[gcc.git] / gcc / cp / decl2.c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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. */
21
22
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. */
26
27 /* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
29
30 #include "config.h"
31 #include "system.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "cp-tree.h"
36 #include "decl.h"
37 #include "lex.h"
38 #include "output.h"
39 #include "except.h"
40 #include "expr.h"
41 #include "defaults.h"
42 #include "toplev.h"
43 #include "dwarf2out.h"
44 #include "dwarfout.h"
45
46 #if USE_CPPLIB
47 #include "cpplib.h"
48 extern cpp_reader parse_in;
49 #endif
50
51 static tree get_sentry PROTO((tree));
52 static void mark_vtable_entries PROTO((tree));
53 static void grok_function_init PROTO((tree, tree));
54 static int finish_vtable_vardecl PROTO((tree, tree));
55 static int prune_vtable_vardecl PROTO((tree, tree));
56 static void finish_sigtable_vardecl PROTO((tree, tree));
57 static int is_namespace_ancestor PROTO((tree, tree));
58 static void add_using_namespace PROTO((tree, tree, int));
59 static tree ambiguous_decl PROTO((tree, tree, tree,int));
60 static tree build_anon_union_vars PROTO((tree, tree*, int, int));
61 static int acceptable_java_type PROTO((tree));
62 static void output_vtable_inherit PROTO((tree));
63 static void setup_initp PROTO((void));
64 static void start_objects PROTO((int, int));
65 static void finish_objects PROTO((int, int));
66 static void do_dtors PROTO((tree));
67 static void do_ctors PROTO((tree));
68 static tree merge_functions PROTO((tree, tree));
69 static tree decl_namespace PROTO((tree));
70 static tree validate_nonmember_using_decl PROTO((tree, tree *, tree *));
71 static void do_nonmember_using_decl PROTO((tree, tree, tree, tree,
72 tree *, tree *));
73
74 extern int current_class_depth;
75
76 /* A list of virtual function tables we must make sure to write out. */
77 tree pending_vtables;
78
79 /* A list of static class variables. This is needed, because a
80 static class variable can be declared inside the class without
81 an initializer, and then initialized, staticly, outside the class. */
82 tree pending_statics;
83
84 /* A list of functions which were declared inline, but which we
85 may need to emit outline anyway. */
86 static tree saved_inlines;
87
88 /* Used to help generate temporary names which are unique within
89 a function. Reset to 0 by start_function. */
90
91 int temp_name_counter;
92
93 /* Same, but not reset. Local temp variables and global temp variables
94 can have the same name. */
95 static int global_temp_name_counter;
96
97 /* Flag used when debugging spew.c */
98
99 extern int spew_debug;
100
101 /* Nonzero if we're done parsing and into end-of-file activities. */
102
103 int at_eof;
104
105 /* Functions called along with real static constructors and destructors. */
106
107 tree static_ctors, static_dtors;
108
109 /* The current open namespace, and ::. */
110
111 tree current_namespace;
112 tree global_namespace;
113
114 /* The stack for namespaces of current declarations. */
115
116 static tree decl_namespace_list;
117
118 \f
119 /* C (and C++) language-specific option variables. */
120
121 /* Nonzero means allow type mismatches in conditional expressions;
122 just make their values `void'. */
123
124 int flag_cond_mismatch;
125
126 /* Nonzero means give `double' the same size as `float'. */
127
128 int flag_short_double;
129
130 /* Nonzero means don't recognize the keyword `asm'. */
131
132 int flag_no_asm;
133
134 /* Nonzero means don't recognize any extension keywords. */
135
136 int flag_no_gnu_keywords;
137
138 /* Nonzero means don't recognize the non-ANSI builtin functions. */
139
140 int flag_no_builtin;
141
142 /* Nonzero means don't recognize the non-ANSI builtin functions.
143 -ansi sets this. */
144
145 int flag_no_nonansi_builtin;
146
147 /* Nonzero means do some things the same way PCC does. Only provided so
148 the compiler will link. */
149
150 int flag_traditional;
151
152 /* Nonzero means to treat bitfields as unsigned unless they say `signed'. */
153
154 int flag_signed_bitfields = 1;
155
156 /* Nonzero means enable obscure ANSI features and disable GNU extensions
157 that might cause ANSI-compliant code to be miscompiled. */
158
159 int flag_ansi;
160
161 /* Nonzero means do emit exported implementations of functions even if
162 they can be inlined. */
163
164 int flag_implement_inlines = 1;
165
166 /* Nonzero means do emit exported implementations of templates, instead of
167 multiple static copies in each file that needs a definition. */
168
169 int flag_external_templates;
170
171 /* Nonzero means that the decision to emit or not emit the implementation of a
172 template depends on where the template is instantiated, rather than where
173 it is defined. */
174
175 int flag_alt_external_templates;
176
177 /* Nonzero means that implicit instantiations will be emitted if needed. */
178
179 int flag_implicit_templates = 1;
180
181 /* Nonzero means that implicit instantiations of inline templates will be
182 emitted if needed, even if instantiations of non-inline templates
183 aren't. */
184
185 int flag_implicit_inline_templates = 1;
186
187 /* Nonzero means warn about implicit declarations. */
188
189 int warn_implicit = 1;
190
191 /* Nonzero means warn about usage of long long when `-pedantic'. */
192
193 int warn_long_long = 1;
194
195 /* Nonzero means warn when all ctors or dtors are private, and the class
196 has no friends. */
197
198 int warn_ctor_dtor_privacy = 1;
199
200 /* True if we want to implement vtables using "thunks".
201 The default is off. */
202
203 #ifndef DEFAULT_VTABLE_THUNKS
204 #define DEFAULT_VTABLE_THUNKS 0
205 #endif
206 int flag_vtable_thunks = DEFAULT_VTABLE_THUNKS;
207
208 /* True if we want to deal with repository information. */
209
210 int flag_use_repository;
211
212 /* Nonzero if we want to issue diagnostics that the standard says are not
213 required. */
214
215 int flag_optional_diags = 1;
216
217 /* Nonzero means give string constants the type `const char *', as mandated
218 by the standard. */
219
220 int flag_const_strings = 1;
221
222 /* Nonzero means warn about deprecated conversion from string constant to
223 `char *'. */
224
225 int warn_write_strings;
226
227 /* Nonzero means warn about pointer casts that can drop a type qualifier
228 from the pointer target type. */
229
230 int warn_cast_qual;
231
232 /* Nonzero means warn about sizeof(function) or addition/subtraction
233 of function pointers. */
234
235 int warn_pointer_arith = 1;
236
237 /* Nonzero means warn for any function def without prototype decl. */
238
239 int warn_missing_prototypes;
240
241 /* Nonzero means warn about multiple (redundant) decls for the same single
242 variable or function. */
243
244 int warn_redundant_decls;
245
246 /* Warn if initializer is not completely bracketed. */
247
248 int warn_missing_braces;
249
250 /* Warn about comparison of signed and unsigned values. */
251
252 int warn_sign_compare;
253
254 /* Warn about *printf or *scanf format/argument anomalies. */
255
256 int warn_format;
257
258 /* Warn about a subscript that has type char. */
259
260 int warn_char_subscripts;
261
262 /* Warn if a type conversion is done that might have confusing results. */
263
264 int warn_conversion;
265
266 /* Warn if adding () is suggested. */
267
268 int warn_parentheses;
269
270 /* Non-zero means warn in function declared in derived class has the
271 same name as a virtual in the base class, but fails to match the
272 type signature of any virtual function in the base class. */
273 int warn_overloaded_virtual;
274
275 /* Non-zero means warn when declaring a class that has a non virtual
276 destructor, when it really ought to have a virtual one. */
277 int warn_nonvdtor;
278
279 /* Non-zero means warn when a function is declared extern and later inline. */
280 int warn_extern_inline;
281
282 /* Non-zero means warn when the compiler will reorder code. */
283 int warn_reorder;
284
285 /* Non-zero means warn when synthesis behavior differs from Cfront's. */
286 int warn_synth;
287
288 /* Non-zero means warn when we convert a pointer to member function
289 into a pointer to (void or function). */
290 int warn_pmf2ptr = 1;
291
292 /* Nonzero means warn about violation of some Effective C++ style rules. */
293
294 int warn_ecpp;
295
296 /* Nonzero means warn where overload resolution chooses a promotion from
297 unsigned to signed over a conversion to an unsigned of the same size. */
298
299 int warn_sign_promo;
300
301 /* Nonzero means warn when an old-style cast is used. */
302
303 int warn_old_style_cast;
304
305 /* Warn about #pragma directives that are not recognised. */
306
307 int warn_unknown_pragmas; /* Tri state variable. */
308
309 /* Nonzero means warn about use of multicharacter literals. */
310
311 int warn_multichar = 1;
312
313 /* Nonzero means warn when non-templatized friend functions are
314 declared within a template */
315
316 int warn_nontemplate_friend = 1;
317
318 /* Nonzero means `$' can be in an identifier. */
319
320 #ifndef DOLLARS_IN_IDENTIFIERS
321 #define DOLLARS_IN_IDENTIFIERS 1
322 #endif
323 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
324
325 /* Nonzero for -fno-strict-prototype switch: do not consider empty
326 argument prototype to mean function takes no arguments. */
327
328 int flag_strict_prototype = 2;
329 int strict_prototype = 1;
330 int strict_prototypes_lang_c, strict_prototypes_lang_cplusplus = 1;
331
332 /* Nonzero means that labels can be used as first-class objects */
333
334 int flag_labels_ok;
335
336 /* Non-zero means to collect statistics which might be expensive
337 and to print them when we are done. */
338 int flag_detailed_statistics;
339
340 /* C++ specific flags. */
341 /* Zero means that `this' is a *const. This gives nice behavior in the
342 2.0 world. 1 gives 1.2-compatible behavior. 2 gives Spring behavior.
343 -2 means we're constructing an object and it has fixed type. */
344
345 int flag_this_is_variable;
346
347 /* 3 means write out only virtuals function tables `defined'
348 in this implementation file.
349 0 means write out virtual function tables and give them
350 (C) static access (default). */
351
352 int write_virtuals;
353
354 /* Nonzero means we should attempt to elide constructors when possible. */
355
356 int flag_elide_constructors = 1;
357
358 /* Nonzero means recognize and handle signature language constructs. */
359
360 int flag_handle_signatures;
361
362 /* Nonzero means that member functions defined in class scope are
363 inline by default. */
364
365 int flag_default_inline = 1;
366
367 /* Controls whether compiler generates 'type descriptor' that give
368 run-time type information. */
369 int flag_rtti = 1;
370
371 /* Nonzero if we wish to output cross-referencing information
372 for the GNU class browser. */
373 extern int flag_gnu_xref;
374
375 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
376 objects. */
377
378 int flag_huge_objects;
379
380 /* Nonzero if we want to conserve space in the .o files. We do this
381 by putting uninitialized data and runtime initialized data into
382 .common instead of .data at the expense of not flagging multiple
383 definitions. */
384
385 int flag_conserve_space;
386
387 /* Nonzero if we want to obey access control semantics. */
388
389 int flag_access_control = 1;
390
391 /* Nonzero if we want to understand the operator names, i.e. 'bitand'. */
392
393 int flag_operator_names;
394
395 /* Nonzero if we want to check the return value of new and avoid calling
396 constructors if it is a null pointer. */
397
398 int flag_check_new;
399
400 /* Nonzero if we want the new ANSI rules for pushing a new scope for `for'
401 initialization variables.
402 0: Old rules, set by -fno-for-scope.
403 2: New ANSI rules, set by -ffor-scope.
404 1: Try to implement new ANSI rules, but with backup compatibility
405 (and warnings). This is the default, for now. */
406
407 int flag_new_for_scope = 1;
408
409 /* Nonzero if we want to emit defined symbols with common-like linkage as
410 weak symbols where possible, in order to conform to C++ semantics.
411 Otherwise, emit them as local symbols. */
412
413 int flag_weak = 1;
414
415 /* Nonzero to enable experimental ABI changes. */
416
417 int flag_new_abi;
418
419 /* Nonzero to not ignore namespace std. */
420
421 int flag_honor_std;
422
423 /* Maximum template instantiation depth. Must be at least 17 for ANSI
424 compliance. */
425
426 int max_tinst_depth = 17;
427
428 /* The name-mangling scheme to use. Must be 1 or greater to support
429 template functions with identical types, but different template
430 arguments. */
431 int name_mangling_version = 2;
432
433 /* Nonzero means that guiding declarations are allowed. */
434 int flag_guiding_decls;
435
436 /* Nonzero if squashed mangling is to be performed.
437 This uses the B and K codes to reference previously seen class types
438 and class qualifiers. */
439 int flag_do_squangling;
440
441 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
442
443 int flag_vtable_gc;
444
445 /* Nonzero means make the default pedwarns warnings instead of errors.
446 The value of this flag is ignored if -pedantic is specified. */
447
448 int flag_permissive;
449
450 /* Table of language-dependent -f options.
451 STRING is the option name. VARIABLE is the address of the variable.
452 ON_VALUE is the value to store in VARIABLE
453 if `-fSTRING' is seen as an option.
454 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
455
456 static struct { const char *string; int *variable; int on_value;}
457 lang_f_options[] =
458 {
459 /* C/C++ options. */
460 {"signed-char", &flag_signed_char, 1},
461 {"unsigned-char", &flag_signed_char, 0},
462 {"signed-bitfields", &flag_signed_bitfields, 1},
463 {"unsigned-bitfields", &flag_signed_bitfields, 0},
464 {"short-enums", &flag_short_enums, 1},
465 {"short-double", &flag_short_double, 1},
466 {"cond-mismatch", &flag_cond_mismatch, 1},
467 {"asm", &flag_no_asm, 0},
468 {"builtin", &flag_no_builtin, 0},
469
470 /* C++-only options. */
471 {"access-control", &flag_access_control, 1},
472 {"check-new", &flag_check_new, 1},
473 {"conserve-space", &flag_conserve_space, 1},
474 {"const-strings", &flag_const_strings, 1},
475 {"default-inline", &flag_default_inline, 1},
476 {"dollars-in-identifiers", &dollars_in_ident, 1},
477 {"elide-constructors", &flag_elide_constructors, 1},
478 {"external-templates", &flag_external_templates, 1},
479 {"for-scope", &flag_new_for_scope, 2},
480 {"gnu-keywords", &flag_no_gnu_keywords, 0},
481 {"handle-exceptions", &flag_exceptions, 1},
482 {"handle-signatures", &flag_handle_signatures, 1},
483 {"honor-std", &flag_honor_std, 1},
484 {"huge-objects", &flag_huge_objects, 1},
485 {"implement-inlines", &flag_implement_inlines, 1},
486 {"implicit-inline-templates", &flag_implicit_inline_templates, 1},
487 {"implicit-templates", &flag_implicit_templates, 1},
488 {"labels-ok", &flag_labels_ok, 1},
489 {"nonansi-builtins", &flag_no_nonansi_builtin, 0},
490 {"operator-names", &flag_operator_names, 1},
491 {"optional-diags", &flag_optional_diags, 1},
492 {"permissive", &flag_permissive, 1},
493 {"repo", &flag_use_repository, 1},
494 {"rtti", &flag_rtti, 1},
495 {"squangle", &flag_do_squangling, 1},
496 {"stats", &flag_detailed_statistics, 1},
497 {"strict-prototype", &flag_strict_prototype, 1},
498 {"this-is-variable", &flag_this_is_variable, 1},
499 {"vtable-gc", &flag_vtable_gc, 1},
500 {"vtable-thunks", &flag_vtable_thunks, 1},
501 {"weak", &flag_weak, 1},
502 {"xref", &flag_gnu_xref, 1}
503 };
504
505 /* Decode the string P as a language-specific option.
506 Return the number of strings consumed for a valid option.
507 Otherwise return 0. */
508
509 int
510 lang_decode_option (argc, argv)
511 int argc
512 #if !USE_CPPLIB
513 ATTRIBUTE_UNUSED
514 #endif
515 ;
516 char **argv;
517
518 {
519 int strings_processed;
520 char *p = argv[0];
521 #if USE_CPPLIB
522 strings_processed = cpp_handle_option (&parse_in, argc, argv);
523 #else
524 strings_processed = 0;
525 #endif /* ! USE_CPPLIB */
526
527 if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional"))
528 /* ignore */;
529 else if (p[0] == '-' && p[1] == 'f')
530 {
531 /* Some kind of -f option.
532 P's value is the option sans `-f'.
533 Search for it in the table of options. */
534 int found = 0;
535 size_t j;
536
537 p += 2;
538 /* Try special -f options. */
539
540 if (!strcmp (p, "handle-exceptions")
541 || !strcmp (p, "no-handle-exceptions"))
542 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
543
544 if (!strcmp (p, "memoize-lookups")
545 || !strcmp (p, "no-memoize-lookups")
546 || !strcmp (p, "save-memoized")
547 || !strcmp (p, "no-save-memoized")
548 || !strcmp (p, "no-all-virtual")
549 || !strcmp (p, "no-enum-int-equiv")
550 || !strcmp (p, "nonnull-objects")
551 || !strcmp (p, "ansi-overloading"))
552 {
553 /* ignore */
554 found = 1;
555 }
556 else if (!strcmp (p, "all-virtual")
557 || !strcmp (p, "enum-int-equiv")
558 || !strcmp (p, "no-nonnull-objects")
559 || !strcmp (p, "no-ansi-overloading"))
560 {
561 warning ("-f%s is no longer supported", p);
562 found = 1;
563 }
564 else if (! strcmp (p, "alt-external-templates"))
565 {
566 flag_external_templates = 1;
567 flag_alt_external_templates = 1;
568 found = 1;
569 }
570 else if (! strcmp (p, "no-alt-external-templates"))
571 {
572 flag_alt_external_templates = 0;
573 found = 1;
574 }
575 else if (!strcmp (p, "repo"))
576 {
577 flag_use_repository = 1;
578 flag_implicit_templates = 0;
579 found = 1;
580 }
581 else if (!strcmp (p, "guiding-decls"))
582 {
583 flag_guiding_decls = 1;
584 name_mangling_version = 0;
585 found = 1;
586 }
587 else if (!strcmp (p, "no-guiding-decls"))
588 {
589 flag_guiding_decls = 0;
590 found = 1;
591 }
592 else if (!strcmp (p, "new-abi"))
593 {
594 flag_new_abi = 1;
595 flag_do_squangling = 1;
596 flag_honor_std = 1;
597 flag_vtable_thunks = 1;
598 }
599 else if (!strcmp (p, "no-new-abi"))
600 {
601 flag_new_abi = 0;
602 flag_do_squangling = 0;
603 flag_honor_std = 0;
604 }
605 else if (!strncmp (p, "template-depth-", 15))
606 {
607 max_tinst_depth =
608 read_integral_parameter (p + 15, p - 2, max_tinst_depth);
609 }
610 else if (!strncmp (p, "name-mangling-version-", 22))
611 {
612 name_mangling_version =
613 read_integral_parameter (p + 22, p - 2, name_mangling_version);
614 }
615 else for (j = 0;
616 !found && j < sizeof (lang_f_options) / sizeof (lang_f_options[0]);
617 j++)
618 {
619 if (!strcmp (p, lang_f_options[j].string))
620 {
621 *lang_f_options[j].variable = lang_f_options[j].on_value;
622 /* A goto here would be cleaner,
623 but breaks the vax pcc. */
624 found = 1;
625 }
626 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
627 && ! strcmp (p+3, lang_f_options[j].string))
628 {
629 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
630 found = 1;
631 }
632 }
633 return found;
634 }
635 else if (p[0] == '-' && p[1] == 'W')
636 {
637 int setting = 1;
638
639 /* The -W options control the warning behavior of the compiler. */
640 p += 2;
641
642 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-')
643 setting = 0, p += 3;
644
645 if (!strcmp (p, "implicit"))
646 warn_implicit = setting;
647 else if (!strcmp (p, "long-long"))
648 warn_long_long = setting;
649 else if (!strcmp (p, "return-type"))
650 warn_return_type = setting;
651 else if (!strcmp (p, "ctor-dtor-privacy"))
652 warn_ctor_dtor_privacy = setting;
653 else if (!strcmp (p, "write-strings"))
654 warn_write_strings = setting;
655 else if (!strcmp (p, "cast-qual"))
656 warn_cast_qual = setting;
657 else if (!strcmp (p, "char-subscripts"))
658 warn_char_subscripts = setting;
659 else if (!strcmp (p, "pointer-arith"))
660 warn_pointer_arith = setting;
661 else if (!strcmp (p, "missing-prototypes"))
662 warn_missing_prototypes = setting;
663 else if (!strcmp (p, "redundant-decls"))
664 warn_redundant_decls = setting;
665 else if (!strcmp (p, "missing-braces"))
666 warn_missing_braces = setting;
667 else if (!strcmp (p, "sign-compare"))
668 warn_sign_compare = setting;
669 else if (!strcmp (p, "format"))
670 warn_format = setting;
671 else if (!strcmp (p, "conversion"))
672 warn_conversion = setting;
673 else if (!strcmp (p, "parentheses"))
674 warn_parentheses = setting;
675 else if (!strcmp (p, "non-virtual-dtor"))
676 warn_nonvdtor = setting;
677 else if (!strcmp (p, "extern-inline"))
678 warn_extern_inline = setting;
679 else if (!strcmp (p, "reorder"))
680 warn_reorder = setting;
681 else if (!strcmp (p, "synth"))
682 warn_synth = setting;
683 else if (!strcmp (p, "pmf-conversions"))
684 warn_pmf2ptr = setting;
685 else if (!strcmp (p, "effc++"))
686 warn_ecpp = setting;
687 else if (!strcmp (p, "sign-promo"))
688 warn_sign_promo = setting;
689 else if (!strcmp (p, "old-style-cast"))
690 warn_old_style_cast = setting;
691 else if (!strcmp (p, "overloaded-virtual"))
692 warn_overloaded_virtual = setting;
693 else if (!strcmp (p, "multichar"))
694 warn_multichar = setting;
695 else if (!strcmp (p, "unknown-pragmas"))
696 /* Set to greater than 1, so that even unknown pragmas in
697 system headers will be warned about. */
698 warn_unknown_pragmas = setting * 2;
699 else if (!strcmp (p, "non-template-friend"))
700 warn_nontemplate_friend = setting;
701 else if (!strcmp (p, "comment"))
702 ; /* cpp handles this one. */
703 else if (!strcmp (p, "comments"))
704 ; /* cpp handles this one. */
705 else if (!strcmp (p, "trigraphs"))
706 ; /* cpp handles this one. */
707 else if (!strcmp (p, "import"))
708 ; /* cpp handles this one. */
709 else if (!strcmp (p, "all"))
710 {
711 warn_return_type = setting;
712 warn_unused = setting;
713 warn_implicit = setting;
714 warn_switch = setting;
715 warn_format = setting;
716 warn_parentheses = setting;
717 warn_missing_braces = setting;
718 warn_sign_compare = setting;
719 warn_multichar = setting;
720 /* We save the value of warn_uninitialized, since if they put
721 -Wuninitialized on the command line, we need to generate a
722 warning about not using it without also specifying -O. */
723 if (warn_uninitialized != 1)
724 warn_uninitialized = (setting ? 2 : 0);
725 /* Only warn about unknown pragmas that are not in system
726 headers. */
727 warn_unknown_pragmas = 1;
728
729 /* C++-specific warnings. */
730 warn_ctor_dtor_privacy = setting;
731 warn_nonvdtor = setting;
732 warn_reorder = setting;
733 warn_nontemplate_friend = setting;
734 }
735 else return strings_processed;
736 }
737 else if (!strcmp (p, "-ansi"))
738 flag_no_nonansi_builtin = 1, flag_ansi = 1,
739 flag_no_gnu_keywords = 1, flag_operator_names = 1;
740 #ifdef SPEW_DEBUG
741 /* Undocumented, only ever used when you're invoking cc1plus by hand, since
742 it's probably safe to assume no sane person would ever want to use this
743 under normal circumstances. */
744 else if (!strcmp (p, "-spew-debug"))
745 spew_debug = 1;
746 #endif
747 else
748 return strings_processed;
749
750 return 1;
751 }
752 \f
753 /* Incorporate `const' and `volatile' qualifiers for member functions.
754 FUNCTION is a TYPE_DECL or a FUNCTION_DECL.
755 QUALS is a list of qualifiers. */
756
757 tree
758 grok_method_quals (ctype, function, quals)
759 tree ctype, function, quals;
760 {
761 tree fntype = TREE_TYPE (function);
762 tree raises = TYPE_RAISES_EXCEPTIONS (fntype);
763 int type_quals = TYPE_UNQUALIFIED;
764 int dup_quals = TYPE_UNQUALIFIED;
765
766 do
767 {
768 int tq = cp_type_qual_from_rid (TREE_VALUE (quals));
769
770 if (type_quals & tq)
771 dup_quals |= tq;
772 else
773 type_quals |= tq;
774 quals = TREE_CHAIN (quals);
775 }
776 while (quals);
777
778 if (dup_quals != TYPE_UNQUALIFIED)
779 cp_error ("duplicate type qualifiers in %s declaration",
780 TREE_CODE (function) == FUNCTION_DECL
781 ? "member function" : "type");
782
783 ctype = cp_build_qualified_type (ctype, type_quals);
784 fntype = build_cplus_method_type (ctype, TREE_TYPE (fntype),
785 (TREE_CODE (fntype) == METHOD_TYPE
786 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype))
787 : TYPE_ARG_TYPES (fntype)));
788 if (raises)
789 fntype = build_exception_variant (fntype, raises);
790
791 TREE_TYPE (function) = fntype;
792 return ctype;
793 }
794
795 /* Warn when -fexternal-templates is used and #pragma
796 interface/implementation is not used all the times it should be,
797 inform the user. */
798
799 void
800 warn_if_unknown_interface (decl)
801 tree decl;
802 {
803 static int already_warned = 0;
804 if (already_warned++)
805 return;
806
807 if (flag_alt_external_templates)
808 {
809 struct tinst_level *til = tinst_for_decl ();
810 int sl = lineno;
811 char *sf = input_filename;
812
813 if (til)
814 {
815 lineno = til->line;
816 input_filename = til->file;
817 }
818 cp_warning ("template `%#D' instantiated in file without #pragma interface",
819 decl);
820 lineno = sl;
821 input_filename = sf;
822 }
823 else
824 cp_warning_at ("template `%#D' defined in file without #pragma interface",
825 decl);
826 }
827
828 /* A subroutine of the parser, to handle a component list. */
829
830 void
831 grok_x_components (specs)
832 tree specs;
833 {
834 struct pending_inline **p;
835 tree t;
836
837 specs = strip_attrs (specs);
838
839 check_tag_decl (specs);
840 t = groktypename (build_decl_list (specs, NULL_TREE));
841
842 /* The only case where we need to do anything additional here is an
843 anonymous union field, e.g.: `struct S { union { int i; }; };'. */
844 if (t == NULL_TREE || !ANON_UNION_TYPE_P (t))
845 return;
846
847 fixup_anonymous_union (t);
848 finish_member_declaration (build_lang_field_decl (FIELD_DECL,
849 NULL_TREE,
850 t));
851
852 /* Ignore any inline function definitions in the anonymous union
853 since an anonymous union may not have function members. */
854 p = &pending_inlines;
855 for (; *p; *p = (*p)->next)
856 if (DECL_CONTEXT ((*p)->fndecl) != t)
857 break;
858 }
859
860 /* Constructors for types with virtual baseclasses need an "in-charge" flag
861 saying whether this constructor is responsible for initialization of
862 virtual baseclasses or not. All destructors also need this "in-charge"
863 flag, which additionally determines whether or not the destructor should
864 free the memory for the object.
865
866 This function adds the "in-charge" flag to member function FN if
867 appropriate. It is called from grokclassfn and tsubst.
868 FN must be either a constructor or destructor. */
869
870 void
871 maybe_retrofit_in_chrg (fn)
872 tree fn;
873 {
874 tree basetype, arg_types, parms, parm, fntype;
875
876 if (DECL_CONSTRUCTOR_P (fn)
877 && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CLASS_CONTEXT (fn))
878 && ! DECL_CONSTRUCTOR_FOR_VBASE_P (fn))
879 /* OK */;
880 else if (! DECL_CONSTRUCTOR_P (fn)
881 && TREE_CHAIN (DECL_ARGUMENTS (fn)) == NULL_TREE)
882 /* OK */;
883 else
884 return;
885
886 if (DECL_CONSTRUCTOR_P (fn))
887 DECL_CONSTRUCTOR_FOR_VBASE_P (fn) = 1;
888
889 /* First add it to DECL_ARGUMENTS... */
890 parm = build_decl (PARM_DECL, in_charge_identifier, integer_type_node);
891 /* Mark the artificial `__in_chrg' parameter as "artificial". */
892 SET_DECL_ARTIFICIAL (parm);
893 DECL_ARG_TYPE (parm) = integer_type_node;
894 TREE_READONLY (parm) = 1;
895 parms = DECL_ARGUMENTS (fn);
896 TREE_CHAIN (parm) = TREE_CHAIN (parms);
897 TREE_CHAIN (parms) = parm;
898
899 /* ...and then to TYPE_ARG_TYPES. */
900 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
901 basetype = TREE_TYPE (TREE_VALUE (arg_types));
902 arg_types = hash_tree_chain (integer_type_node, TREE_CHAIN (arg_types));
903 fntype = build_cplus_method_type (basetype, TREE_TYPE (TREE_TYPE (fn)),
904 arg_types);
905 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
906 fntype = build_exception_variant (fntype,
907 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)));
908 TREE_TYPE (fn) = fntype;
909 }
910
911 /* Classes overload their constituent function names automatically.
912 When a function name is declared in a record structure,
913 its name is changed to it overloaded name. Since names for
914 constructors and destructors can conflict, we place a leading
915 '$' for destructors.
916
917 CNAME is the name of the class we are grokking for.
918
919 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'.
920
921 FLAGS contains bits saying what's special about today's
922 arguments. 1 == DESTRUCTOR. 2 == OPERATOR.
923
924 If FUNCTION is a destructor, then we must add the `auto-delete' field
925 as a second parameter. There is some hair associated with the fact
926 that we must "declare" this variable in the manner consistent with the
927 way the rest of the arguments were declared.
928
929 QUALS are the qualifiers for the this pointer. */
930
931 void
932 grokclassfn (ctype, function, flags, quals)
933 tree ctype, function;
934 enum overload_flags flags;
935 tree quals;
936 {
937 tree fn_name = DECL_NAME (function);
938 tree arg_types;
939 tree parm;
940 tree qualtype;
941
942 if (fn_name == NULL_TREE)
943 {
944 error ("name missing for member function");
945 fn_name = get_identifier ("<anonymous>");
946 DECL_NAME (function) = fn_name;
947 }
948
949 if (quals)
950 qualtype = grok_method_quals (ctype, function, quals);
951 else
952 qualtype = ctype;
953
954 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
955 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
956 {
957 /* Must add the class instance variable up front. */
958 /* Right now we just make this a pointer. But later
959 we may wish to make it special. */
960 tree type = TREE_VALUE (arg_types);
961 int constp = 1;
962
963 if ((flag_this_is_variable > 0)
964 && (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)))
965 constp = 0;
966
967 parm = build_decl (PARM_DECL, this_identifier, type);
968 /* Mark the artificial `this' parameter as "artificial". */
969 SET_DECL_ARTIFICIAL (parm);
970 DECL_ARG_TYPE (parm) = type;
971 /* We can make this a register, so long as we don't
972 accidentally complain if someone tries to take its address. */
973 DECL_REGISTER (parm) = 1;
974 if (constp)
975 TREE_READONLY (parm) = 1;
976 TREE_CHAIN (parm) = last_function_parms;
977 last_function_parms = parm;
978 }
979
980 DECL_ARGUMENTS (function) = last_function_parms;
981 /* First approximations. */
982 DECL_CONTEXT (function) = ctype;
983 DECL_CLASS_CONTEXT (function) = ctype;
984
985 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function))
986 {
987 maybe_retrofit_in_chrg (function);
988 arg_types = TYPE_ARG_TYPES (TREE_TYPE (function));
989 }
990
991 if (flags == DTOR_FLAG)
992 {
993 DECL_ASSEMBLER_NAME (function) = build_destructor_name (ctype);
994 TYPE_HAS_DESTRUCTOR (ctype) = 1;
995 }
996 else
997 set_mangled_name_for_decl (function);
998 }
999
1000 /* Work on the expr used by alignof (this is only called by the parser). */
1001
1002 tree
1003 grok_alignof (expr)
1004 tree expr;
1005 {
1006 tree best, t;
1007 int bestalign;
1008
1009 if (processing_template_decl)
1010 return build_min (ALIGNOF_EXPR, sizetype, expr);
1011
1012 if (TREE_CODE (expr) == COMPONENT_REF
1013 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1014 error ("`__alignof__' applied to a bit-field");
1015
1016 if (TREE_CODE (expr) == INDIRECT_REF)
1017 {
1018 best = t = TREE_OPERAND (expr, 0);
1019 bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1020
1021 while (TREE_CODE (t) == NOP_EXPR
1022 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
1023 {
1024 int thisalign;
1025 t = TREE_OPERAND (t, 0);
1026 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1027 if (thisalign > bestalign)
1028 best = t, bestalign = thisalign;
1029 }
1030 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
1031 }
1032 else
1033 {
1034 /* ANSI says arrays and fns are converted inside comma.
1035 But we can't convert them in build_compound_expr
1036 because that would break commas in lvalues.
1037 So do the conversion here if operand was a comma. */
1038 if (TREE_CODE (expr) == COMPOUND_EXPR
1039 && (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1040 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE))
1041 expr = default_conversion (expr);
1042 return c_alignof (TREE_TYPE (expr));
1043 }
1044 }
1045
1046 /* Create an ARRAY_REF, checking for the user doing things backwards
1047 along the way. */
1048
1049 tree
1050 grok_array_decl (array_expr, index_exp)
1051 tree array_expr, index_exp;
1052 {
1053 tree type = TREE_TYPE (array_expr);
1054 tree p1, p2, i1, i2;
1055
1056 if (type == error_mark_node || index_exp == error_mark_node)
1057 return error_mark_node;
1058 if (processing_template_decl)
1059 return build_min (ARRAY_REF, type ? TREE_TYPE (type) : NULL_TREE,
1060 array_expr, index_exp);
1061
1062 if (type == NULL_TREE)
1063 {
1064 /* Something has gone very wrong. Assume we are mistakenly reducing
1065 an expression instead of a declaration. */
1066 error ("parser may be lost: is there a '{' missing somewhere?");
1067 return NULL_TREE;
1068 }
1069
1070 if (TREE_CODE (type) == OFFSET_TYPE
1071 || TREE_CODE (type) == REFERENCE_TYPE)
1072 type = TREE_TYPE (type);
1073
1074 /* If they have an `operator[]', use that. */
1075 if (IS_AGGR_TYPE (type) || IS_AGGR_TYPE (TREE_TYPE (index_exp)))
1076 return build_opfncall (ARRAY_REF, LOOKUP_NORMAL,
1077 array_expr, index_exp, NULL_TREE);
1078
1079 /* Otherwise, create an ARRAY_REF for a pointer or array type. It
1080 is a little-known fact that, if `a' is an array and `i' is an
1081 int, you can write `i[a]', which means the same thing as `a[i]'. */
1082
1083 if (TREE_CODE (type) == ARRAY_TYPE)
1084 p1 = array_expr;
1085 else
1086 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, 0);
1087
1088 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
1089 p2 = index_exp;
1090 else
1091 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, 0);
1092
1093 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 0);
1094 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 0);
1095
1096 if ((p1 && i2) && (i1 && p2))
1097 error ("ambiguous conversion for array subscript");
1098
1099 if (p1 && i2)
1100 array_expr = p1, index_exp = i2;
1101 else if (i1 && p2)
1102 array_expr = p2, index_exp = i1;
1103 else
1104 {
1105 cp_error ("invalid types `%T[%T]' for array subscript",
1106 type, TREE_TYPE (index_exp));
1107 return error_mark_node;
1108 }
1109
1110 if (array_expr == error_mark_node || index_exp == error_mark_node)
1111 error ("ambiguous conversion for array subscript");
1112
1113 return build_array_ref (array_expr, index_exp);
1114 }
1115
1116 /* Given the cast expression EXP, checking out its validity. Either return
1117 an error_mark_node if there was an unavoidable error, return a cast to
1118 void for trying to delete a pointer w/ the value 0, or return the
1119 call to delete. If DOING_VEC is 1, we handle things differently
1120 for doing an array delete. If DOING_VEC is 2, they gave us the
1121 array size as an argument to delete.
1122 Implements ARM $5.3.4. This is called from the parser. */
1123
1124 tree
1125 delete_sanity (exp, size, doing_vec, use_global_delete)
1126 tree exp, size;
1127 int doing_vec, use_global_delete;
1128 {
1129 tree t, type;
1130 /* For a regular vector delete (aka, no size argument) we will pass
1131 this down as a NULL_TREE into build_vec_delete. */
1132 tree maxindex = NULL_TREE;
1133
1134 if (exp == error_mark_node)
1135 return exp;
1136
1137 if (processing_template_decl)
1138 {
1139 t = build_min (DELETE_EXPR, void_type_node, exp, size);
1140 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete;
1141 DELETE_EXPR_USE_VEC (t) = doing_vec;
1142 return t;
1143 }
1144
1145 if (TREE_CODE (exp) == OFFSET_REF)
1146 exp = resolve_offset_ref (exp);
1147 exp = convert_from_reference (exp);
1148 t = stabilize_reference (exp);
1149 t = build_expr_type_conversion (WANT_POINTER, t, 1);
1150
1151 if (t == NULL_TREE || t == error_mark_node)
1152 {
1153 cp_error ("type `%#T' argument given to `delete', expected pointer",
1154 TREE_TYPE (exp));
1155 return error_mark_node;
1156 }
1157
1158 if (doing_vec == 2)
1159 {
1160 maxindex = build_binary_op (MINUS_EXPR, size, integer_one_node, 1);
1161 pedwarn ("anachronistic use of array size in vector delete");
1162 }
1163
1164 type = TREE_TYPE (t);
1165
1166 /* As of Valley Forge, you can delete a pointer to const. */
1167
1168 /* You can't delete functions. */
1169 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
1170 {
1171 error ("cannot delete a function");
1172 return error_mark_node;
1173 }
1174
1175 /* An array can't have been allocated by new, so complain. */
1176 if (TREE_CODE (t) == ADDR_EXPR
1177 && TREE_CODE (TREE_OPERAND (t, 0)) == VAR_DECL
1178 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == ARRAY_TYPE)
1179 cp_warning ("deleting array `%#D'", TREE_OPERAND (t, 0));
1180
1181 /* Deleting a pointer with the value zero is valid and has no effect. */
1182 if (integer_zerop (t))
1183 return build1 (NOP_EXPR, void_type_node, t);
1184
1185 if (doing_vec)
1186 return build_vec_delete (t, maxindex, integer_one_node,
1187 integer_zero_node, use_global_delete);
1188 else
1189 {
1190 if (IS_AGGR_TYPE (TREE_TYPE (type))
1191 && TYPE_GETS_REG_DELETE (TREE_TYPE (type)))
1192 {
1193 /* Only do access checking here; we'll be calling op delete
1194 from the destructor. */
1195 tree tmp = build_op_delete_call (DELETE_EXPR, t, size_zero_node,
1196 LOOKUP_NORMAL, NULL_TREE);
1197 if (tmp == error_mark_node)
1198 return error_mark_node;
1199 }
1200
1201 return build_delete (type, t, integer_three_node,
1202 LOOKUP_NORMAL, use_global_delete);
1203 }
1204 }
1205
1206 /* Report an error if the indicated template declaration is not the
1207 sort of thing that should be a member template. */
1208
1209 void
1210 check_member_template (tmpl)
1211 tree tmpl;
1212 {
1213 tree decl;
1214
1215 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
1216 decl = DECL_TEMPLATE_RESULT (tmpl);
1217
1218 if (TREE_CODE (decl) == FUNCTION_DECL
1219 || (TREE_CODE (decl) == TYPE_DECL
1220 && IS_AGGR_TYPE (TREE_TYPE (decl))))
1221 {
1222 if (current_function_decl)
1223 /* 14.5.2.2 [temp.mem]
1224
1225 A local class shall not have member templates. */
1226 cp_error ("declaration of member template `%#D' in local class",
1227 decl);
1228
1229 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
1230 {
1231 /* 14.5.2.3 [temp.mem]
1232
1233 A member function template shall not be virtual. */
1234 cp_error
1235 ("invalid use of `virtual' in template declaration of `%#D'",
1236 decl);
1237 DECL_VIRTUAL_P (decl) = 0;
1238 }
1239
1240 /* The debug-information generating code doesn't know what to do
1241 with member templates. */
1242 DECL_IGNORED_P (tmpl) = 1;
1243 }
1244 else
1245 cp_error ("template declaration of `%#D'", decl);
1246 }
1247
1248 /* Return true iff TYPE is a valid Java parameter or return type. */
1249
1250 static int
1251 acceptable_java_type (type)
1252 tree type;
1253 {
1254 if (TREE_CODE (type) == VOID_TYPE || TYPE_FOR_JAVA (type))
1255 return 1;
1256 if (TREE_CODE (type) == POINTER_TYPE)
1257 {
1258 type = TREE_TYPE (type);
1259 if (TREE_CODE (type) == RECORD_TYPE)
1260 {
1261 tree args; int i;
1262 if (! TYPE_FOR_JAVA (type))
1263 return 0;
1264 if (! CLASSTYPE_TEMPLATE_INFO (type))
1265 return 1;
1266 args = CLASSTYPE_TI_ARGS (type);
1267 i = TREE_VEC_LENGTH (args);
1268 while (--i >= 0)
1269 {
1270 type = TREE_VEC_ELT (args, i);
1271 if (TREE_CODE (type) == POINTER_TYPE)
1272 type = TREE_TYPE (type);
1273 if (! TYPE_FOR_JAVA (type))
1274 return 0;
1275 }
1276 return 1;
1277 }
1278 }
1279 return 0;
1280 }
1281
1282 /* For a METHOD in a Java class CTYPE, return 1 if
1283 the parameter and return types are valid Java types.
1284 Otherwise, print appropriate error messages, and return 0. */
1285
1286 int
1287 check_java_method (method)
1288 tree method;
1289 {
1290 int jerr = 0;
1291 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
1292 tree ret_type = TREE_TYPE (TREE_TYPE (method));
1293 if (! acceptable_java_type (ret_type))
1294 {
1295 cp_error ("Java method '%D' has non-Java return type `%T'",
1296 method, ret_type);
1297 jerr++;
1298 }
1299 for (; arg_types != NULL_TREE; arg_types = TREE_CHAIN (arg_types))
1300 {
1301 tree type = TREE_VALUE (arg_types);
1302 if (! acceptable_java_type (type))
1303 {
1304 cp_error ("Java method '%D' has non-Java parameter type `%T'",
1305 method, type);
1306 jerr++;
1307 }
1308 }
1309 return jerr ? 0 : 1;
1310 }
1311
1312 /* Sanity check: report error if this function FUNCTION is not
1313 really a member of the class (CTYPE) it is supposed to belong to.
1314 CNAME is the same here as it is for grokclassfn above. */
1315
1316 tree
1317 check_classfn (ctype, function)
1318 tree ctype, function;
1319 {
1320 tree fn_name = DECL_NAME (function);
1321 tree fndecl, fndecls;
1322 tree method_vec = CLASSTYPE_METHOD_VEC (complete_type (ctype));
1323 tree *methods = 0;
1324 tree *end = 0;
1325
1326 if (DECL_USE_TEMPLATE (function)
1327 && is_member_template (DECL_TI_TEMPLATE (function)))
1328 /* Since this is a specialization of a member template,
1329 we're not going to find the declaration in the class.
1330 For example, in:
1331
1332 struct S { template <typename T> void f(T); };
1333 template <> void S::f(int);
1334
1335 we're not going to find `S::f(int)', but there's no
1336 reason we should, either. We let our callers know we didn't
1337 find the method, but we don't complain. */
1338 return NULL_TREE;
1339
1340 if (method_vec != 0)
1341 {
1342 methods = &TREE_VEC_ELT (method_vec, 0);
1343 end = TREE_VEC_END (method_vec);
1344
1345 /* First suss out ctors and dtors. */
1346 if (*methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1347 && DECL_CONSTRUCTOR_P (function))
1348 goto got_it;
1349 if (*++methods && fn_name == DECL_NAME (OVL_CURRENT (*methods))
1350 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function)))
1351 goto got_it;
1352
1353 while (++methods != end && *methods)
1354 {
1355 fndecl = *methods;
1356 if (fn_name == DECL_NAME (OVL_CURRENT (*methods)))
1357 {
1358 got_it:
1359 for (fndecls = *methods; fndecls != NULL_TREE;
1360 fndecls = OVL_NEXT (fndecls))
1361 {
1362 fndecl = OVL_CURRENT (fndecls);
1363 /* The DECL_ASSEMBLER_NAME for a TEMPLATE_DECL, or
1364 for a for member function of a template class, is
1365 not mangled, so the check below does not work
1366 correctly in that case. Since mangled destructor
1367 names do not include the type of the arguments,
1368 we can't use this short-cut for them, either.
1369 (It's not legal to declare arguments for a
1370 destructor, but some people try.) */
1371 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (function))
1372 && (DECL_ASSEMBLER_NAME (function)
1373 != DECL_NAME (function))
1374 && (DECL_ASSEMBLER_NAME (fndecl)
1375 != DECL_NAME (fndecl))
1376 && (DECL_ASSEMBLER_NAME (function)
1377 == DECL_ASSEMBLER_NAME (fndecl)))
1378 return fndecl;
1379
1380 /* We cannot simply call decls_match because this
1381 doesn't work for static member functions that are
1382 pretending to be methods, and because the name
1383 may have been changed by asm("new_name"). */
1384 if (DECL_NAME (function) == DECL_NAME (fndecl))
1385 {
1386 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function));
1387 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1388
1389 /* Get rid of the this parameter on functions that become
1390 static. */
1391 if (DECL_STATIC_FUNCTION_P (fndecl)
1392 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE)
1393 p1 = TREE_CHAIN (p1);
1394
1395 if (same_type_p (TREE_TYPE (TREE_TYPE (function)),
1396 TREE_TYPE (TREE_TYPE (fndecl)))
1397 && compparms (p1, p2)
1398 && (DECL_TEMPLATE_SPECIALIZATION (function)
1399 == DECL_TEMPLATE_SPECIALIZATION (fndecl))
1400 && (!DECL_TEMPLATE_SPECIALIZATION (function)
1401 || (DECL_TI_TEMPLATE (function)
1402 == DECL_TI_TEMPLATE (fndecl))))
1403 return fndecl;
1404 }
1405 }
1406 break; /* loser */
1407 }
1408 }
1409 }
1410
1411 if (methods != end && *methods)
1412 {
1413 tree fndecl = *methods;
1414 cp_error ("prototype for `%#D' does not match any in class `%T'",
1415 function, ctype);
1416 cp_error_at ("candidate%s: %+#D", OVL_NEXT (fndecl) ? "s are" : " is",
1417 OVL_CURRENT (fndecl));
1418 while (fndecl = OVL_NEXT (fndecl), fndecl)
1419 cp_error_at (" %#D", OVL_CURRENT(fndecl));
1420 }
1421 else
1422 {
1423 methods = 0;
1424 if (TYPE_SIZE (ctype) == 0)
1425 incomplete_type_error (function, ctype);
1426 else
1427 cp_error ("no `%#D' member function declared in class `%T'",
1428 function, ctype);
1429 }
1430
1431 /* If we did not find the method in the class, add it to avoid
1432 spurious errors (unless the CTYPE is not yet defined, in which
1433 case we'll only confuse ourselves when the function is declared
1434 properly within the class. */
1435 if (TYPE_SIZE (ctype))
1436 add_method (ctype, methods, function);
1437 return NULL_TREE;
1438 }
1439
1440 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
1441 of a structure component, returning a FIELD_DECL node.
1442 QUALS is a list of type qualifiers for this decl (such as for declaring
1443 const member functions).
1444
1445 This is done during the parsing of the struct declaration.
1446 The FIELD_DECL nodes are chained together and the lot of them
1447 are ultimately passed to `build_struct' to make the RECORD_TYPE node.
1448
1449 C++:
1450
1451 If class A defines that certain functions in class B are friends, then
1452 the way I have set things up, it is B who is interested in permission
1453 granted by A. However, it is in A's context that these declarations
1454 are parsed. By returning a void_type_node, class A does not attempt
1455 to incorporate the declarations of the friends within its structure.
1456
1457 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
1458 CHANGES TO CODE IN `start_method'. */
1459
1460 tree
1461 grokfield (declarator, declspecs, init, asmspec_tree, attrlist)
1462 tree declarator, declspecs, init, asmspec_tree, attrlist;
1463 {
1464 register tree value;
1465 char *asmspec = 0;
1466 int flags = LOOKUP_ONLYCONVERTING;
1467
1468 /* Convert () initializers to = initializers. */
1469 if (init == NULL_TREE && declarator != NULL_TREE
1470 && TREE_CODE (declarator) == CALL_EXPR
1471 && TREE_OPERAND (declarator, 0)
1472 && (TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE
1473 || TREE_CODE (TREE_OPERAND (declarator, 0)) == SCOPE_REF)
1474 && parmlist_is_exprlist (TREE_OPERAND (declarator, 1)))
1475 {
1476 init = TREE_OPERAND (declarator, 1);
1477 declarator = TREE_OPERAND (declarator, 0);
1478 flags = 0;
1479 }
1480
1481 if (declspecs == NULL_TREE
1482 && TREE_CODE (declarator) == SCOPE_REF
1483 && TREE_CODE (TREE_OPERAND (declarator, 1)) == IDENTIFIER_NODE)
1484 {
1485 /* Access declaration */
1486 if (! IS_AGGR_TYPE_CODE (TREE_CODE (TREE_OPERAND (declarator, 0))))
1487 ;
1488 else if (TREE_COMPLEXITY (declarator) == current_class_depth)
1489 pop_nested_class (1);
1490 return do_class_using_decl (declarator);
1491 }
1492
1493 if (init
1494 && TREE_CODE (init) == TREE_LIST
1495 && TREE_VALUE (init) == error_mark_node
1496 && TREE_CHAIN (init) == NULL_TREE)
1497 init = NULL_TREE;
1498
1499 value = grokdeclarator (declarator, declspecs, FIELD, init != 0, NULL_TREE);
1500 if (! value || value == error_mark_node)
1501 /* friend or constructor went bad. */
1502 return value;
1503
1504 /* Pass friendly classes back. */
1505 if (TREE_CODE (value) == VOID_TYPE)
1506 return void_type_node;
1507
1508 if (DECL_NAME (value) != NULL_TREE
1509 && IDENTIFIER_POINTER (DECL_NAME (value))[0] == '_'
1510 && ! strcmp (IDENTIFIER_POINTER (DECL_NAME (value)), "_vptr"))
1511 cp_error ("member `%D' conflicts with virtual function table field name",
1512 value);
1513
1514 /* Stash away type declarations. */
1515 if (TREE_CODE (value) == TYPE_DECL)
1516 {
1517 DECL_NONLOCAL (value) = 1;
1518 DECL_CONTEXT (value) = current_class_type;
1519 DECL_CLASS_CONTEXT (value) = current_class_type;
1520
1521 /* Now that we've updated the context, we need to remangle the
1522 name for this TYPE_DECL. */
1523 DECL_ASSEMBLER_NAME (value) = DECL_NAME (value);
1524 if (!uses_template_parms (value))
1525 DECL_ASSEMBLER_NAME (value) =
1526 get_identifier (build_overload_name (TREE_TYPE (value), 1, 1));
1527
1528 pushdecl_class_level (value);
1529 return value;
1530 }
1531
1532 if (IS_SIGNATURE (current_class_type)
1533 && TREE_CODE (value) != FUNCTION_DECL)
1534 {
1535 error ("field declaration not allowed in signature");
1536 return void_type_node;
1537 }
1538
1539 if (DECL_IN_AGGR_P (value))
1540 {
1541 cp_error ("`%D' is already defined in `%T'", value,
1542 DECL_CONTEXT (value));
1543 return void_type_node;
1544 }
1545
1546 if (asmspec_tree)
1547 asmspec = TREE_STRING_POINTER (asmspec_tree);
1548
1549 if (init)
1550 {
1551 if (IS_SIGNATURE (current_class_type)
1552 && TREE_CODE (value) == FUNCTION_DECL)
1553 {
1554 error ("function declarations cannot have initializers in signature");
1555 init = NULL_TREE;
1556 }
1557 else if (TREE_CODE (value) == FUNCTION_DECL)
1558 {
1559 grok_function_init (value, init);
1560 init = NULL_TREE;
1561 }
1562 else if (pedantic && TREE_CODE (value) != VAR_DECL)
1563 /* Already complained in grokdeclarator. */
1564 init = NULL_TREE;
1565 else
1566 {
1567 /* We allow initializers to become parameters to base
1568 initializers. */
1569 if (TREE_CODE (init) == TREE_LIST)
1570 {
1571 if (TREE_CHAIN (init) == NULL_TREE)
1572 init = TREE_VALUE (init);
1573 else
1574 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1575 }
1576
1577 if (TREE_CODE (init) == CONST_DECL)
1578 init = DECL_INITIAL (init);
1579 else if (TREE_READONLY_DECL_P (init))
1580 init = decl_constant_value (init);
1581 else if (TREE_CODE (init) == CONSTRUCTOR)
1582 init = digest_init (TREE_TYPE (value), init, (tree *)0);
1583 my_friendly_assert (TREE_PERMANENT (init), 192);
1584 if (init == error_mark_node)
1585 /* We must make this look different than `error_mark_node'
1586 because `decl_const_value' would mis-interpret it
1587 as only meaning that this VAR_DECL is defined. */
1588 init = build1 (NOP_EXPR, TREE_TYPE (value), init);
1589 else if (processing_template_decl)
1590 ;
1591 else if (! TREE_CONSTANT (init))
1592 {
1593 /* We can allow references to things that are effectively
1594 static, since references are initialized with the address. */
1595 if (TREE_CODE (TREE_TYPE (value)) != REFERENCE_TYPE
1596 || (TREE_STATIC (init) == 0
1597 && (TREE_CODE_CLASS (TREE_CODE (init)) != 'd'
1598 || DECL_EXTERNAL (init) == 0)))
1599 {
1600 error ("field initializer is not constant");
1601 init = error_mark_node;
1602 }
1603 }
1604 }
1605 }
1606
1607 /* The corresponding pop_obstacks is in cp_finish_decl. */
1608 push_obstacks_nochange ();
1609
1610 if (processing_template_decl && ! current_function_decl
1611 && (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == FUNCTION_DECL))
1612 value = push_template_decl (value);
1613
1614 check_template_shadow (value);
1615
1616 if (attrlist)
1617 cplus_decl_attributes (value, TREE_PURPOSE (attrlist),
1618 TREE_VALUE (attrlist));
1619
1620 if (TREE_CODE (value) == VAR_DECL)
1621 {
1622 my_friendly_assert (TREE_PUBLIC (value), 0);
1623
1624 /* We cannot call pushdecl here, because that would
1625 fill in the value of our TREE_CHAIN. Instead, we
1626 modify cp_finish_decl to do the right thing, namely, to
1627 put this decl out straight away. */
1628 /* current_class_type can be NULL_TREE in case of error. */
1629 if (asmspec == 0 && current_class_type)
1630 {
1631 TREE_PUBLIC (value) = 1;
1632 DECL_INITIAL (value) = error_mark_node;
1633 DECL_ASSEMBLER_NAME (value)
1634 = build_static_name (current_class_type, DECL_NAME (value));
1635 }
1636 if (! processing_template_decl)
1637 pending_statics = perm_tree_cons (NULL_TREE, value, pending_statics);
1638
1639 /* Static consts need not be initialized in the class definition. */
1640 if (init != NULL_TREE && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (value)))
1641 {
1642 static int explanation = 0;
1643
1644 error ("initializer invalid for static member with constructor");
1645 if (explanation++ == 0)
1646 error ("(you really want to initialize it separately)");
1647 init = 0;
1648 }
1649 /* Force the compiler to know when an uninitialized static
1650 const member is being used. */
1651 if (CP_TYPE_CONST_P (TREE_TYPE (value)) && init == 0)
1652 TREE_USED (value) = 1;
1653 DECL_INITIAL (value) = init;
1654 DECL_IN_AGGR_P (value) = 1;
1655 DECL_CONTEXT (value) = current_class_type;
1656 DECL_CLASS_CONTEXT (value) = current_class_type;
1657
1658 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1659 pushdecl_class_level (value);
1660 return value;
1661 }
1662 if (TREE_CODE (value) == FIELD_DECL)
1663 {
1664 if (asmspec)
1665 {
1666 /* This must override the asm specifier which was placed
1667 by grokclassfn. Lay this out fresh. */
1668 DECL_RTL (value) = NULL_RTX;
1669 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1670 }
1671 if (DECL_INITIAL (value) == error_mark_node)
1672 init = error_mark_node;
1673 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1674 DECL_INITIAL (value) = init;
1675 DECL_IN_AGGR_P (value) = 1;
1676 return value;
1677 }
1678 if (TREE_CODE (value) == FUNCTION_DECL)
1679 {
1680 if (asmspec)
1681 {
1682 /* This must override the asm specifier which was placed
1683 by grokclassfn. Lay this out fresh. */
1684 DECL_RTL (value) = NULL_RTX;
1685 DECL_ASSEMBLER_NAME (value) = get_identifier (asmspec);
1686 }
1687 cp_finish_decl (value, init, asmspec_tree, 1, flags);
1688
1689 /* Pass friends back this way. */
1690 if (DECL_FRIEND_P (value))
1691 return void_type_node;
1692
1693 #if 0 /* Just because a fn is declared doesn't mean we'll try to define it. */
1694 if (current_function_decl && ! IS_SIGNATURE (current_class_type))
1695 cp_error ("method `%#D' of local class must be defined in class body",
1696 value);
1697 #endif
1698
1699 DECL_IN_AGGR_P (value) = 1;
1700 return value;
1701 }
1702 my_friendly_abort (21);
1703 /* NOTREACHED */
1704 return NULL_TREE;
1705 }
1706
1707 /* Like `grokfield', but for bitfields.
1708 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. */
1709
1710 tree
1711 grokbitfield (declarator, declspecs, width)
1712 tree declarator, declspecs, width;
1713 {
1714 register tree value = grokdeclarator (declarator, declspecs, BITFIELD,
1715 0, NULL_TREE);
1716
1717 if (! value) return NULL_TREE; /* friends went bad. */
1718
1719 /* Pass friendly classes back. */
1720 if (TREE_CODE (value) == VOID_TYPE)
1721 return void_type_node;
1722
1723 if (TREE_CODE (value) == TYPE_DECL)
1724 {
1725 cp_error ("cannot declare `%D' to be a bitfield type", value);
1726 return NULL_TREE;
1727 }
1728
1729 /* Usually, finish_struct_1 catches bitifields with invalid types.
1730 But, in the case of bitfields with function type, we confuse
1731 ourselves into thinking they are member functions, so we must
1732 check here. */
1733 if (TREE_CODE (value) == FUNCTION_DECL)
1734 {
1735 cp_error ("cannot declare bitfield `%D' with funcion type",
1736 DECL_NAME (value));
1737 return NULL_TREE;
1738 }
1739
1740 if (IS_SIGNATURE (current_class_type))
1741 {
1742 error ("field declaration not allowed in signature");
1743 return void_type_node;
1744 }
1745
1746 if (DECL_IN_AGGR_P (value))
1747 {
1748 cp_error ("`%D' is already defined in the class %T", value,
1749 DECL_CONTEXT (value));
1750 return void_type_node;
1751 }
1752
1753 GNU_xref_member (current_class_name, value);
1754
1755 if (TREE_STATIC (value))
1756 {
1757 cp_error ("static member `%D' cannot be a bitfield", value);
1758 return NULL_TREE;
1759 }
1760 cp_finish_decl (value, NULL_TREE, NULL_TREE, 0, 0);
1761
1762 if (width != error_mark_node)
1763 {
1764 constant_expression_warning (width);
1765 DECL_INITIAL (value) = width;
1766 SET_DECL_C_BIT_FIELD (value);
1767 }
1768
1769 DECL_IN_AGGR_P (value) = 1;
1770 return value;
1771 }
1772
1773 tree
1774 grokoptypename (declspecs, declarator)
1775 tree declspecs, declarator;
1776 {
1777 tree t = grokdeclarator (declarator, declspecs, TYPENAME, 0, NULL_TREE);
1778 return build_typename_overload (t);
1779 }
1780
1781 /* When a function is declared with an initializer,
1782 do the right thing. Currently, there are two possibilities:
1783
1784 class B
1785 {
1786 public:
1787 // initialization possibility #1.
1788 virtual void f () = 0;
1789 int g ();
1790 };
1791
1792 class D1 : B
1793 {
1794 public:
1795 int d1;
1796 // error, no f ();
1797 };
1798
1799 class D2 : B
1800 {
1801 public:
1802 int d2;
1803 void f ();
1804 };
1805
1806 class D3 : B
1807 {
1808 public:
1809 int d3;
1810 // initialization possibility #2
1811 void f () = B::f;
1812 };
1813
1814 */
1815
1816 int
1817 copy_assignment_arg_p (parmtype, virtualp)
1818 tree parmtype;
1819 int virtualp ATTRIBUTE_UNUSED;
1820 {
1821 if (current_class_type == NULL_TREE)
1822 return 0;
1823
1824 if (TREE_CODE (parmtype) == REFERENCE_TYPE)
1825 parmtype = TREE_TYPE (parmtype);
1826
1827 if ((TYPE_MAIN_VARIANT (parmtype) == current_class_type)
1828 #if 0
1829 /* Non-standard hack to support old Booch components. */
1830 || (! virtualp && DERIVED_FROM_P (parmtype, current_class_type))
1831 #endif
1832 )
1833 return 1;
1834
1835 return 0;
1836 }
1837
1838 static void
1839 grok_function_init (decl, init)
1840 tree decl;
1841 tree init;
1842 {
1843 /* An initializer for a function tells how this function should
1844 be inherited. */
1845 tree type = TREE_TYPE (decl);
1846
1847 if (TREE_CODE (type) == FUNCTION_TYPE)
1848 cp_error ("initializer specified for non-member function `%D'", decl);
1849 #if 0
1850 /* We'll check for this in finish_struct_1. */
1851 else if (DECL_VINDEX (decl) == NULL_TREE)
1852 cp_error ("initializer specified for non-virtual method `%D'", decl);
1853 #endif
1854 else if (integer_zerop (init))
1855 {
1856 #if 0
1857 /* Mark this function as being "defined". */
1858 DECL_INITIAL (decl) = error_mark_node;
1859 /* pure virtual destructors must be defined. */
1860 /* pure virtual needs to be defined (as abort) only when put in
1861 vtbl. For wellformed call, it should be itself. pr4737 */
1862 if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)))
1863 {
1864 extern tree abort_fndecl;
1865 /* Give this node rtl from `abort'. */
1866 DECL_RTL (decl) = DECL_RTL (abort_fndecl);
1867 }
1868 #endif
1869 DECL_ABSTRACT_VIRTUAL_P (decl) = 1;
1870 if (DECL_NAME (decl) == ansi_opname [(int) MODIFY_EXPR])
1871 {
1872 tree parmtype
1873 = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl))));
1874
1875 if (copy_assignment_arg_p (parmtype, 1))
1876 TYPE_HAS_ABSTRACT_ASSIGN_REF (current_class_type) = 1;
1877 }
1878 }
1879 else
1880 cp_error ("invalid initializer for virtual method `%D'", decl);
1881 }
1882 \f
1883 void
1884 cplus_decl_attributes (decl, attributes, prefix_attributes)
1885 tree decl, attributes, prefix_attributes;
1886 {
1887 if (decl == NULL_TREE || decl == void_type_node)
1888 return;
1889
1890 if (TREE_CODE (decl) == TEMPLATE_DECL)
1891 decl = DECL_TEMPLATE_RESULT (decl);
1892
1893 decl_attributes (decl, attributes, prefix_attributes);
1894
1895 if (TREE_CODE (decl) == TYPE_DECL)
1896 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (decl), TREE_TYPE (decl));
1897 }
1898 \f
1899 /* CONSTRUCTOR_NAME:
1900 Return the name for the constructor (or destructor) for the
1901 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1902 IDENTIFIER_NODE. When given a template, this routine doesn't
1903 lose the specialization. */
1904
1905 tree
1906 constructor_name_full (thing)
1907 tree thing;
1908 {
1909 if (TREE_CODE (thing) == TEMPLATE_TYPE_PARM
1910 || TREE_CODE (thing) == TEMPLATE_TEMPLATE_PARM
1911 || TREE_CODE (thing) == TYPENAME_TYPE)
1912 thing = TYPE_NAME (thing);
1913 else if (IS_AGGR_TYPE_CODE (TREE_CODE (thing)))
1914 {
1915 if (TYPE_WAS_ANONYMOUS (thing) && TYPE_HAS_CONSTRUCTOR (thing))
1916 thing = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (thing), 0)));
1917 else
1918 thing = TYPE_NAME (thing);
1919 }
1920 if (TREE_CODE (thing) == TYPE_DECL
1921 || (TREE_CODE (thing) == TEMPLATE_DECL
1922 && TREE_CODE (DECL_TEMPLATE_RESULT (thing)) == TYPE_DECL))
1923 thing = DECL_NAME (thing);
1924 my_friendly_assert (TREE_CODE (thing) == IDENTIFIER_NODE, 197);
1925 return thing;
1926 }
1927
1928 /* CONSTRUCTOR_NAME:
1929 Return the name for the constructor (or destructor) for the
1930 specified class. Argument can be RECORD_TYPE, TYPE_DECL, or
1931 IDENTIFIER_NODE. When given a template, return the plain
1932 unspecialized name. */
1933
1934 tree
1935 constructor_name (thing)
1936 tree thing;
1937 {
1938 tree t;
1939 thing = constructor_name_full (thing);
1940 t = IDENTIFIER_TEMPLATE (thing);
1941 if (!t)
1942 return thing;
1943 return t;
1944 }
1945 \f
1946 /* Cache the value of this class's main virtual function table pointer
1947 in a register variable. This will save one indirection if a
1948 more than one virtual function call is made this function. */
1949
1950 void
1951 setup_vtbl_ptr ()
1952 {
1953 extern tree base_init_expr;
1954
1955 if (base_init_expr == 0
1956 && DECL_CONSTRUCTOR_P (current_function_decl))
1957 {
1958 if (processing_template_decl)
1959 add_tree (build_min_nt
1960 (CTOR_INITIALIZER,
1961 current_member_init_list, current_base_init_list));
1962 else
1963 emit_base_init (current_class_type, 0);
1964 }
1965 }
1966
1967 /* Record the existence of an addressable inline function. */
1968
1969 void
1970 mark_inline_for_output (decl)
1971 tree decl;
1972 {
1973 decl = DECL_MAIN_VARIANT (decl);
1974 if (DECL_SAVED_INLINE (decl))
1975 return;
1976 my_friendly_assert (TREE_PERMANENT (decl), 363);
1977 DECL_SAVED_INLINE (decl) = 1;
1978 #if 0
1979 if (DECL_PENDING_INLINE_INFO (decl) != 0
1980 && ! DECL_PENDING_INLINE_INFO (decl)->deja_vu)
1981 {
1982 struct pending_inline *t = pending_inlines;
1983 my_friendly_assert (DECL_SAVED_INSNS (decl) == 0, 198);
1984 while (t)
1985 {
1986 if (t == DECL_PENDING_INLINE_INFO (decl))
1987 break;
1988 t = t->next;
1989 }
1990 if (t == 0)
1991 {
1992 t = DECL_PENDING_INLINE_INFO (decl);
1993 t->next = pending_inlines;
1994 pending_inlines = t;
1995 }
1996 DECL_PENDING_INLINE_INFO (decl) = 0;
1997 }
1998 #endif
1999 saved_inlines = perm_tree_cons (NULL_TREE, decl, saved_inlines);
2000 }
2001
2002 void
2003 clear_temp_name ()
2004 {
2005 temp_name_counter = 0;
2006 }
2007
2008 /* Hand off a unique name which can be used for variable we don't really
2009 want to know about anyway, for example, the anonymous variables which
2010 are needed to make references work. Declare this thing so we can use it.
2011 The variable created will be of type TYPE.
2012
2013 STATICP is nonzero if this variable should be static. */
2014
2015 tree
2016 get_temp_name (type, staticp)
2017 tree type;
2018 int staticp;
2019 {
2020 char buf[sizeof (AUTO_TEMP_FORMAT) + 20];
2021 tree decl;
2022 int toplev = toplevel_bindings_p ();
2023
2024 push_obstacks_nochange ();
2025 if (toplev || staticp)
2026 {
2027 end_temporary_allocation ();
2028 sprintf (buf, AUTO_TEMP_FORMAT, global_temp_name_counter++);
2029 decl = pushdecl_top_level (build_decl (VAR_DECL, get_identifier (buf), type));
2030 }
2031 else
2032 {
2033 sprintf (buf, AUTO_TEMP_FORMAT, temp_name_counter++);
2034 decl = pushdecl (build_decl (VAR_DECL, get_identifier (buf), type));
2035 }
2036 TREE_USED (decl) = 1;
2037 TREE_STATIC (decl) = staticp;
2038 DECL_ARTIFICIAL (decl) = 1;
2039
2040 /* If this is a local variable, then lay out its rtl now.
2041 Otherwise, callers of this function are responsible for dealing
2042 with this variable's rtl. */
2043 if (! toplev)
2044 {
2045 expand_decl (decl);
2046 expand_decl_init (decl);
2047 }
2048 pop_obstacks ();
2049
2050 return decl;
2051 }
2052
2053 /* Get a variable which we can use for multiple assignments.
2054 It is not entered into current_binding_level, because
2055 that breaks things when it comes time to do final cleanups
2056 (which take place "outside" the binding contour of the function). */
2057
2058 tree
2059 get_temp_regvar (type, init)
2060 tree type, init;
2061 {
2062 tree decl;
2063
2064 decl = build_decl (VAR_DECL, NULL_TREE, type);
2065 TREE_USED (decl) = 1;
2066 DECL_REGISTER (decl) = 1;
2067 DECL_ARTIFICIAL (decl) = 1;
2068
2069 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2070 /* We can expand these without fear, since they cannot need
2071 constructors or destructors. */
2072 expand_expr (build_modify_expr (decl, INIT_EXPR, init),
2073 NULL_RTX, VOIDmode, 0);
2074
2075 return decl;
2076 }
2077
2078 /* Hunts through the global anonymous union ANON_DECL, building
2079 appropriate VAR_DECLs. Stores cleanups on the list of ELEMS, and
2080 returns a VAR_DECL whose size is the same as the size of the
2081 ANON_DECL, if one is available. */
2082
2083 static tree
2084 build_anon_union_vars (anon_decl, elems, static_p, external_p)
2085 tree anon_decl;
2086 tree* elems;
2087 int static_p;
2088 int external_p;
2089 {
2090 tree type = TREE_TYPE (anon_decl);
2091 tree main_decl = NULL_TREE;
2092 tree field;
2093
2094 for (field = TYPE_FIELDS (type);
2095 field != NULL_TREE;
2096 field = TREE_CHAIN (field))
2097 {
2098 tree decl;
2099
2100 if (DECL_ARTIFICIAL (field))
2101 continue;
2102 if (TREE_CODE (field) != FIELD_DECL)
2103 {
2104 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2105 field);
2106 continue;
2107 }
2108
2109 if (TREE_PRIVATE (field))
2110 cp_pedwarn_at ("private member `%#D' in anonymous union", field);
2111 else if (TREE_PROTECTED (field))
2112 cp_pedwarn_at ("protected member `%#D' in anonymous union", field);
2113
2114 if (DECL_NAME (field) == NULL_TREE
2115 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2116 {
2117 decl = build_anon_union_vars (field, elems, static_p, external_p);
2118 if (!decl)
2119 continue;
2120 }
2121 else if (DECL_NAME (field) == NULL_TREE)
2122 continue;
2123 else
2124 {
2125 decl = build_decl (VAR_DECL, DECL_NAME (field), TREE_TYPE (field));
2126 /* tell `pushdecl' that this is not tentative. */
2127 DECL_INITIAL (decl) = error_mark_node;
2128 TREE_PUBLIC (decl) = 0;
2129 TREE_STATIC (decl) = static_p;
2130 DECL_EXTERNAL (decl) = external_p;
2131 decl = pushdecl (decl);
2132 DECL_INITIAL (decl) = NULL_TREE;
2133 }
2134
2135 /* Only write out one anon union element--choose the one that
2136 can hold them all. */
2137 if (main_decl == NULL_TREE
2138 && simple_cst_equal (DECL_SIZE (decl),
2139 DECL_SIZE (anon_decl)) == 1)
2140 main_decl = decl;
2141 else
2142 /* ??? This causes there to be no debug info written out
2143 about this decl. */
2144 TREE_ASM_WRITTEN (decl) = 1;
2145
2146 if (DECL_NAME (field) == NULL_TREE
2147 && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2148 /* The remainder of the processing was already done in the
2149 recursive call. */
2150 continue;
2151
2152 /* If there's a cleanup to do, it belongs in the
2153 TREE_PURPOSE of the following TREE_LIST. */
2154 *elems = scratch_tree_cons (NULL_TREE, decl, *elems);
2155 TREE_TYPE (*elems) = type;
2156 }
2157
2158 return main_decl;
2159 }
2160
2161 /* Finish off the processing of a UNION_TYPE structure.
2162 If there are static members, then all members are
2163 static, and must be laid out together. If the
2164 union is an anonymous union, we arrange for that
2165 as well. PUBLIC_P is nonzero if this union is
2166 not declared static. */
2167
2168 void
2169 finish_anon_union (anon_union_decl)
2170 tree anon_union_decl;
2171 {
2172 tree type = TREE_TYPE (anon_union_decl);
2173 tree elems = NULL_TREE;
2174 tree main_decl;
2175 int public_p = TREE_PUBLIC (anon_union_decl);
2176 int static_p = TREE_STATIC (anon_union_decl);
2177 int external_p = DECL_EXTERNAL (anon_union_decl);
2178
2179 if (TYPE_FIELDS (type) == NULL_TREE)
2180 return;
2181
2182 if (public_p)
2183 {
2184 error ("global anonymous unions must be declared static");
2185 return;
2186 }
2187
2188 main_decl = build_anon_union_vars (anon_union_decl, &elems,
2189 static_p, external_p);
2190
2191 if (main_decl == NULL_TREE)
2192 {
2193 warning ("anonymous union with no members");
2194 return;
2195 }
2196
2197 if (static_p)
2198 {
2199 make_decl_rtl (main_decl, 0, toplevel_bindings_p ());
2200 DECL_RTL (anon_union_decl) = DECL_RTL (main_decl);
2201 }
2202
2203 /* The following call assumes that there are never any cleanups
2204 for anonymous unions--a reasonable assumption. */
2205 expand_anon_union_decl (anon_union_decl, NULL_TREE, elems);
2206 }
2207
2208 /* Finish processing a builtin type TYPE. It's name is NAME,
2209 its fields are in the array FIELDS. LEN is the number of elements
2210 in FIELDS minus one, or put another way, it is the maximum subscript
2211 used in FIELDS.
2212
2213 It is given the same alignment as ALIGN_TYPE. */
2214
2215 void
2216 finish_builtin_type (type, name, fields, len, align_type)
2217 tree type;
2218 const char *name;
2219 tree fields[];
2220 int len;
2221 tree align_type;
2222 {
2223 register int i;
2224
2225 TYPE_FIELDS (type) = fields[0];
2226 for (i = 0; i < len; i++)
2227 {
2228 layout_type (TREE_TYPE (fields[i]));
2229 DECL_FIELD_CONTEXT (fields[i]) = type;
2230 TREE_CHAIN (fields[i]) = fields[i+1];
2231 }
2232 DECL_FIELD_CONTEXT (fields[i]) = type;
2233 DECL_CLASS_CONTEXT (fields[i]) = type;
2234 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
2235 layout_type (type);
2236 #if 0 /* not yet, should get fixed properly later */
2237 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
2238 #else
2239 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
2240 #endif
2241 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2242 layout_decl (TYPE_NAME (type), 0);
2243 }
2244 \f
2245 /* Auxiliary functions to make type signatures for
2246 `operator new' and `operator delete' correspond to
2247 what compiler will be expecting. */
2248
2249 tree
2250 coerce_new_type (type)
2251 tree type;
2252 {
2253 int e1 = 0, e2 = 0;
2254
2255 if (TREE_CODE (type) == METHOD_TYPE)
2256 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (TYPE_ARG_TYPES (type)));
2257 if (! same_type_p (TREE_TYPE (type), ptr_type_node))
2258 e1 = 1, error ("`operator new' must return type `void *'");
2259
2260 /* Technically the type must be `size_t', but we may not know
2261 what that is. */
2262 if (TYPE_ARG_TYPES (type) == NULL_TREE)
2263 e1 = 1, error ("`operator new' takes type `size_t' parameter");
2264 else if (! same_type_p (TREE_VALUE (TYPE_ARG_TYPES (type)), sizetype))
2265 e2 = 1, error ("`operator new' takes type `size_t' as first parameter");
2266 if (e2)
2267 type = build_function_type (ptr_type_node, tree_cons (NULL_TREE, sizetype, TREE_CHAIN (TYPE_ARG_TYPES (type))));
2268 else if (e1)
2269 type = build_function_type (ptr_type_node, TYPE_ARG_TYPES (type));
2270 return type;
2271 }
2272
2273 tree
2274 coerce_delete_type (type)
2275 tree type;
2276 {
2277 int e1 = 0, e2 = 0;
2278 #if 0
2279 e3 = 0;
2280 #endif
2281 tree arg_types = TYPE_ARG_TYPES (type);
2282
2283 if (TREE_CODE (type) == METHOD_TYPE)
2284 {
2285 type = build_function_type (TREE_TYPE (type), TREE_CHAIN (arg_types));
2286 arg_types = TREE_CHAIN (arg_types);
2287 }
2288
2289 if (TREE_TYPE (type) != void_type_node)
2290 e1 = 1, error ("`operator delete' must return type `void'");
2291
2292 if (arg_types == NULL_TREE
2293 || ! same_type_p (TREE_VALUE (arg_types), ptr_type_node))
2294 e2 = 1, error ("`operator delete' takes type `void *' as first parameter");
2295
2296 #if 0
2297 if (arg_types
2298 && TREE_CHAIN (arg_types)
2299 && TREE_CHAIN (arg_types) != void_list_node)
2300 {
2301 /* Again, technically this argument must be `size_t', but again
2302 we may not know what that is. */
2303 tree t2 = TREE_VALUE (TREE_CHAIN (arg_types));
2304 if (! same_type_p (t2, sizetype))
2305 e3 = 1, error ("second argument to `operator delete' must be of type `size_t'");
2306 else if (TREE_CHAIN (TREE_CHAIN (arg_types)) != void_list_node)
2307 {
2308 e3 = 1;
2309 if (TREE_CHAIN (TREE_CHAIN (arg_types)))
2310 error ("too many arguments in declaration of `operator delete'");
2311 else
2312 error ("`...' invalid in specification of `operator delete'");
2313 }
2314 }
2315
2316 if (e3)
2317 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2318 build_tree_list (NULL_TREE, sizetype));
2319 else if (e3 |= e2)
2320 {
2321 if (arg_types == NULL_TREE)
2322 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
2323 else
2324 arg_types = tree_cons (NULL_TREE, ptr_type_node, TREE_CHAIN (arg_types));
2325 }
2326 else e3 |= e1;
2327 #endif
2328
2329 if (e2)
2330 arg_types = tree_cons (NULL_TREE, ptr_type_node,
2331 arg_types ? TREE_CHAIN (arg_types): NULL_TREE);
2332 if (e2 || e1)
2333 type = build_function_type (void_type_node, arg_types);
2334
2335 return type;
2336 }
2337 \f
2338 extern tree abort_fndecl;
2339
2340 static void
2341 mark_vtable_entries (decl)
2342 tree decl;
2343 {
2344 tree entries = CONSTRUCTOR_ELTS (DECL_INITIAL (decl));
2345
2346 if (flag_rtti)
2347 {
2348 tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (TREE_CHAIN (entries))
2349 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2350 tree fn = TREE_OPERAND (fnaddr, 0);
2351 TREE_ADDRESSABLE (fn) = 1;
2352 mark_used (fn);
2353 }
2354 skip_rtti_stuff (&entries);
2355
2356 for (; entries; entries = TREE_CHAIN (entries))
2357 {
2358 tree fnaddr = (flag_vtable_thunks ? TREE_VALUE (entries)
2359 : FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (entries)));
2360 tree fn = TREE_OPERAND (fnaddr, 0);
2361 TREE_ADDRESSABLE (fn) = 1;
2362 if (DECL_LANG_SPECIFIC (fn) && DECL_ABSTRACT_VIRTUAL_P (fn))
2363 {
2364 TREE_OPERAND (fnaddr, 0) = fn = copy_node (fn);
2365 DECL_RTL (fn) = DECL_RTL (abort_fndecl);
2366 mark_used (abort_fndecl);
2367 }
2368 if (TREE_CODE (fn) == THUNK_DECL && DECL_EXTERNAL (fn))
2369 {
2370 DECL_EXTERNAL (fn) = 0;
2371 emit_thunk (fn);
2372 }
2373 mark_used (fn);
2374 }
2375 }
2376
2377 /* Set DECL up to have the closest approximation of "initialized common"
2378 linkage available. */
2379
2380 void
2381 comdat_linkage (decl)
2382 tree decl;
2383 {
2384 if (flag_weak)
2385 make_decl_one_only (decl);
2386 else if (TREE_CODE (decl) == FUNCTION_DECL || DECL_VIRTUAL_P (decl))
2387 /* We can just emit functions and vtables statically; it doesn't really
2388 matter if we have multiple copies. */
2389 TREE_PUBLIC (decl) = 0;
2390 else
2391 {
2392 /* Static data member template instantiations, however, cannot
2393 have multiple copies. */
2394 if (DECL_INITIAL (decl) == 0
2395 || DECL_INITIAL (decl) == error_mark_node)
2396 DECL_COMMON (decl) = 1;
2397 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
2398 {
2399 DECL_COMMON (decl) = 1;
2400 DECL_INITIAL (decl) = error_mark_node;
2401 }
2402 else
2403 {
2404 /* We can't do anything useful; leave vars for explicit
2405 instantiation. */
2406 DECL_EXTERNAL (decl) = 1;
2407 DECL_NOT_REALLY_EXTERN (decl) = 0;
2408 }
2409 }
2410
2411 if (DECL_LANG_SPECIFIC (decl))
2412 DECL_COMDAT (decl) = 1;
2413 }
2414
2415 /* For win32 we also want to put explicit instantiations in
2416 linkonce sections, so that they will be merged with implicit
2417 instantiations; otherwise we get duplicate symbol errors. */
2418
2419 void
2420 maybe_make_one_only (decl)
2421 tree decl;
2422 {
2423 /* This is not necessary on targets that support weak symbols, because
2424 the implicit instantiations will defer to the explicit one. */
2425 if (! supports_one_only () || SUPPORTS_WEAK)
2426 return;
2427
2428 /* We can't set DECL_COMDAT on functions, or finish_file will think
2429 we can get away with not emitting them if they aren't used. We need
2430 to for variables so that cp_finish_decl will update their linkage,
2431 because their DECL_INITIAL may not have been set properly yet. */
2432
2433 make_decl_one_only (decl);
2434
2435 if (TREE_CODE (decl) == VAR_DECL && DECL_LANG_SPECIFIC (decl))
2436 DECL_COMDAT (decl) = 1;
2437 }
2438
2439 /* Set TREE_PUBLIC and/or DECL_EXTERN on the vtable DECL,
2440 based on TYPE and other static flags.
2441
2442 Note that anything public is tagged TREE_PUBLIC, whether
2443 it's public in this file or in another one. */
2444
2445 void
2446 import_export_vtable (decl, type, final)
2447 tree decl, type;
2448 int final;
2449 {
2450 if (DECL_INTERFACE_KNOWN (decl))
2451 return;
2452
2453 if (TYPE_FOR_JAVA (type))
2454 {
2455 TREE_PUBLIC (decl) = 1;
2456 DECL_EXTERNAL (decl) = 1;
2457 DECL_INTERFACE_KNOWN (decl) = 1;
2458 }
2459 else if (CLASSTYPE_INTERFACE_KNOWN (type))
2460 {
2461 TREE_PUBLIC (decl) = 1;
2462 DECL_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
2463 DECL_INTERFACE_KNOWN (decl) = 1;
2464 }
2465 else
2466 {
2467 /* We can only wait to decide if we have real non-inline virtual
2468 functions in our class, or if we come from a template. */
2469
2470 int found = CLASSTYPE_TEMPLATE_INSTANTIATION (type);
2471
2472 if (! found && ! final)
2473 {
2474 tree method;
2475 for (method = TYPE_METHODS (type); method != NULL_TREE;
2476 method = TREE_CHAIN (method))
2477 if (DECL_VINDEX (method) != NULL_TREE
2478 && ! DECL_THIS_INLINE (method)
2479 && ! DECL_ABSTRACT_VIRTUAL_P (method))
2480 {
2481 found = 1;
2482 break;
2483 }
2484 }
2485
2486 if (final || ! found)
2487 {
2488 comdat_linkage (decl);
2489 DECL_EXTERNAL (decl) = 0;
2490 }
2491 else
2492 {
2493 TREE_PUBLIC (decl) = 1;
2494 DECL_EXTERNAL (decl) = 1;
2495 }
2496 }
2497 }
2498
2499 /* Determine whether or not we want to specifically import or export CTYPE,
2500 using various heuristics. */
2501
2502 void
2503 import_export_class (ctype)
2504 tree ctype;
2505 {
2506 /* -1 for imported, 1 for exported. */
2507 int import_export = 0;
2508
2509 if (CLASSTYPE_INTERFACE_KNOWN (ctype))
2510 return;
2511
2512 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma interface,
2513 we will have CLASSTYPE_INTERFACE_ONLY set but not
2514 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this
2515 heuristic because someone will supply a #pragma implementation
2516 elsewhere, and deducing it here would produce a conflict. */
2517 if (CLASSTYPE_INTERFACE_ONLY (ctype))
2518 return;
2519
2520 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2521 /* FIXME this should really use some sort of target-independent macro. */
2522 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype)))
2523 import_export = -1;
2524 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype)))
2525 import_export = 1;
2526 #endif
2527
2528 /* If we got -fno-implicit-templates, we import template classes that
2529 weren't explicitly instantiated. */
2530 if (import_export == 0
2531 && CLASSTYPE_IMPLICIT_INSTANTIATION (ctype)
2532 && ! flag_implicit_templates)
2533 import_export = -1;
2534
2535 /* Base our import/export status on that of the first non-inline,
2536 non-abstract virtual function, if any. */
2537 if (import_export == 0
2538 && TYPE_VIRTUAL_P (ctype)
2539 && ! CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2540 {
2541 tree method;
2542 for (method = TYPE_METHODS (ctype); method != NULL_TREE;
2543 method = TREE_CHAIN (method))
2544 {
2545 if (DECL_VINDEX (method) != NULL_TREE
2546 && !DECL_THIS_INLINE (method)
2547 && !DECL_ABSTRACT_VIRTUAL_P (method))
2548 {
2549 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1);
2550 break;
2551 }
2552 }
2553 }
2554
2555 #ifdef MULTIPLE_SYMBOL_SPACES
2556 if (import_export == -1)
2557 import_export = 0;
2558 #endif
2559
2560 if (import_export)
2561 {
2562 SET_CLASSTYPE_INTERFACE_KNOWN (ctype);
2563 CLASSTYPE_VTABLE_NEEDS_WRITING (ctype) = (import_export > 0);
2564 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0);
2565 }
2566 }
2567
2568 /* We need to describe to the assembler the relationship between
2569 a vtable and the vtable of the parent class. */
2570
2571 static void
2572 output_vtable_inherit (vars)
2573 tree vars;
2574 {
2575 tree parent;
2576 rtx op[2];
2577
2578 op[0] = XEXP (DECL_RTL (vars), 0); /* strip the mem ref */
2579
2580 parent = binfo_for_vtable (vars);
2581
2582 if (parent == TYPE_BINFO (DECL_CONTEXT (vars)))
2583 op[1] = const0_rtx;
2584 else if (parent)
2585 {
2586 parent = TYPE_BINFO_VTABLE (BINFO_TYPE (parent));
2587 op[1] = XEXP (DECL_RTL (parent), 0); /* strip the mem ref */
2588 }
2589 else
2590 my_friendly_abort (980826);
2591
2592 output_asm_insn (".vtable_inherit %c0, %c1", op);
2593 }
2594
2595 static int
2596 finish_vtable_vardecl (prev, vars)
2597 tree prev, vars;
2598 {
2599 tree ctype = DECL_CONTEXT (vars);
2600 import_export_class (ctype);
2601 import_export_vtable (vars, ctype, 1);
2602
2603 if (! DECL_EXTERNAL (vars)
2604 && (DECL_INTERFACE_KNOWN (vars)
2605 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars))
2606 || (hack_decl_function_context (vars) && TREE_USED (vars)))
2607 && ! TREE_ASM_WRITTEN (vars))
2608 {
2609 /* Write it out. */
2610 mark_vtable_entries (vars);
2611 if (TREE_TYPE (DECL_INITIAL (vars)) == 0)
2612 store_init_value (vars, DECL_INITIAL (vars));
2613
2614 if (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG)
2615 {
2616 /* Mark the VAR_DECL node representing the vtable itself as a
2617 "gratuitous" one, thereby forcing dwarfout.c to ignore it.
2618 It is rather important that such things be ignored because
2619 any effort to actually generate DWARF for them will run
2620 into trouble when/if we encounter code like:
2621
2622 #pragma interface
2623 struct S { virtual void member (); };
2624
2625 because the artificial declaration of the vtable itself (as
2626 manufactured by the g++ front end) will say that the vtable
2627 is a static member of `S' but only *after* the debug output
2628 for the definition of `S' has already been output. This causes
2629 grief because the DWARF entry for the definition of the vtable
2630 will try to refer back to an earlier *declaration* of the
2631 vtable as a static member of `S' and there won't be one.
2632 We might be able to arrange to have the "vtable static member"
2633 attached to the member list for `S' before the debug info for
2634 `S' get written (which would solve the problem) but that would
2635 require more intrusive changes to the g++ front end. */
2636
2637 DECL_IGNORED_P (vars) = 1;
2638 }
2639
2640 /* Always make vtables weak. */
2641 if (flag_weak)
2642 comdat_linkage (vars);
2643
2644 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2645
2646 if (flag_vtable_gc)
2647 output_vtable_inherit (vars);
2648
2649 return 1;
2650 }
2651 else if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (vars)))
2652 /* We don't know what to do with this one yet. */
2653 return 0;
2654
2655 /* We know that PREV must be non-zero here. */
2656 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2657 return 0;
2658 }
2659
2660 static int
2661 prune_vtable_vardecl (prev, vars)
2662 tree prev, vars;
2663 {
2664 /* We know that PREV must be non-zero here. */
2665 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2666 return 1;
2667 }
2668
2669 int
2670 walk_vtables (typedecl_fn, vardecl_fn)
2671 register void (*typedecl_fn) PROTO ((tree, tree));
2672 register int (*vardecl_fn) PROTO ((tree, tree));
2673 {
2674 tree prev, vars;
2675 int flag = 0;
2676
2677 for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2678 {
2679 register tree type = TREE_TYPE (vars);
2680
2681 if (TREE_CODE (vars) == VAR_DECL && DECL_VIRTUAL_P (vars))
2682 {
2683 if (vardecl_fn)
2684 flag |= (*vardecl_fn) (prev, vars);
2685
2686 if (prev && TREE_CHAIN (prev) != vars)
2687 continue;
2688 }
2689 else if (TREE_CODE (vars) == TYPE_DECL
2690 && type != error_mark_node
2691 && TYPE_LANG_SPECIFIC (type)
2692 && CLASSTYPE_VSIZE (type))
2693 {
2694 if (typedecl_fn) (*typedecl_fn) (prev, vars);
2695 }
2696
2697 prev = vars;
2698 }
2699
2700 return flag;
2701 }
2702
2703 static void
2704 finish_sigtable_vardecl (prev, vars)
2705 tree prev, vars;
2706 {
2707 /* We don't need to mark sigtable entries as addressable here as is done
2708 for vtables. Since sigtables, unlike vtables, are always written out,
2709 that was already done in build_signature_table_constructor. */
2710
2711 rest_of_decl_compilation (vars, NULL_PTR, 1, 1);
2712
2713 /* We know that PREV must be non-zero here. */
2714 TREE_CHAIN (prev) = TREE_CHAIN (vars);
2715 }
2716
2717 void
2718 walk_sigtables (typedecl_fn, vardecl_fn)
2719 register void (*typedecl_fn) PROTO((tree, tree));
2720 register void (*vardecl_fn) PROTO((tree, tree));
2721 {
2722 tree prev, vars;
2723
2724 for (prev = 0, vars = getdecls (); vars; vars = TREE_CHAIN (vars))
2725 {
2726 register tree type = TREE_TYPE (vars);
2727
2728 if (TREE_CODE (vars) == TYPE_DECL
2729 && type != error_mark_node
2730 && IS_SIGNATURE (type))
2731 {
2732 if (typedecl_fn) (*typedecl_fn) (prev, vars);
2733 }
2734 else if (TREE_CODE (vars) == VAR_DECL
2735 && TREE_TYPE (vars) != error_mark_node
2736 && IS_SIGNATURE (TREE_TYPE (vars)))
2737 {
2738 if (vardecl_fn) (*vardecl_fn) (prev, vars);
2739 }
2740 else
2741 prev = vars;
2742 }
2743 }
2744
2745 /* Determines the proper settings of TREE_PUBLIC and DECL_EXTERNAL for an
2746 inline function or template instantiation at end-of-file. */
2747
2748 void
2749 import_export_decl (decl)
2750 tree decl;
2751 {
2752 if (DECL_INTERFACE_KNOWN (decl))
2753 return;
2754
2755 if (DECL_TEMPLATE_INSTANTIATION (decl)
2756 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2757 {
2758 DECL_NOT_REALLY_EXTERN (decl) = 1;
2759 if ((DECL_IMPLICIT_INSTANTIATION (decl)
2760 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl))
2761 && (flag_implicit_templates
2762 || (flag_implicit_inline_templates && DECL_THIS_INLINE (decl))))
2763 {
2764 if (!TREE_PUBLIC (decl))
2765 /* Templates are allowed to have internal linkage. See
2766 [basic.link]. */
2767 ;
2768 else
2769 comdat_linkage (decl);
2770 }
2771 else
2772 DECL_NOT_REALLY_EXTERN (decl) = 0;
2773 }
2774 else if (DECL_FUNCTION_MEMBER_P (decl))
2775 {
2776 tree ctype = DECL_CLASS_CONTEXT (decl);
2777 import_export_class (ctype);
2778 if (CLASSTYPE_INTERFACE_KNOWN (ctype)
2779 && (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl)))
2780 {
2781 DECL_NOT_REALLY_EXTERN (decl)
2782 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2783 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2784
2785 /* Always make artificials weak. */
2786 if (DECL_ARTIFICIAL (decl) && flag_weak)
2787 comdat_linkage (decl);
2788 else
2789 maybe_make_one_only (decl);
2790 }
2791 else
2792 comdat_linkage (decl);
2793 }
2794 /* tinfo function */
2795 else if (DECL_ARTIFICIAL (decl) && DECL_MUTABLE_P (decl))
2796 {
2797 tree ctype = TREE_TYPE (DECL_NAME (decl));
2798
2799 if (IS_AGGR_TYPE (ctype))
2800 import_export_class (ctype);
2801
2802 if (IS_AGGR_TYPE (ctype) && CLASSTYPE_INTERFACE_KNOWN (ctype)
2803 && TYPE_VIRTUAL_P (ctype)
2804 /* If the type is a cv-qualified variant of a type, then we
2805 must emit the tinfo function in this translation unit
2806 since it will not be emitted when the vtable for the type
2807 is output (which is when the unqualified version is
2808 generated). */
2809 && ctype == TYPE_MAIN_VARIANT (ctype))
2810 {
2811 DECL_NOT_REALLY_EXTERN (decl)
2812 = ! (CLASSTYPE_INTERFACE_ONLY (ctype)
2813 || (DECL_THIS_INLINE (decl) && ! flag_implement_inlines));
2814
2815 /* Always make artificials weak. */
2816 if (flag_weak)
2817 comdat_linkage (decl);
2818 }
2819 else if (TYPE_BUILT_IN (ctype) && ctype == TYPE_MAIN_VARIANT (ctype))
2820 DECL_NOT_REALLY_EXTERN (decl) = 0;
2821 else
2822 comdat_linkage (decl);
2823 }
2824 else
2825 comdat_linkage (decl);
2826
2827 DECL_INTERFACE_KNOWN (decl) = 1;
2828 }
2829
2830 tree
2831 build_cleanup (decl)
2832 tree decl;
2833 {
2834 tree temp;
2835 tree type = TREE_TYPE (decl);
2836
2837 if (TREE_CODE (type) == ARRAY_TYPE)
2838 temp = decl;
2839 else
2840 {
2841 mark_addressable (decl);
2842 temp = build1 (ADDR_EXPR, build_pointer_type (type), decl);
2843 }
2844 temp = build_delete (TREE_TYPE (temp), temp,
2845 integer_two_node,
2846 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
2847 return temp;
2848 }
2849
2850 extern int parse_time, varconst_time;
2851 extern tree pending_templates;
2852 extern tree maybe_templates;
2853
2854 static tree
2855 get_sentry (base)
2856 tree base;
2857 {
2858 tree sname = get_id_2 ("__sn", base);
2859 /* For struct X foo __attribute__((weak)), there is a counter
2860 __snfoo. Since base is already an assembler name, sname should
2861 be globally unique */
2862 tree sentry = IDENTIFIER_GLOBAL_VALUE (sname);
2863 if (! sentry)
2864 {
2865 push_obstacks_nochange ();
2866 end_temporary_allocation ();
2867 sentry = build_decl (VAR_DECL, sname, integer_type_node);
2868 TREE_PUBLIC (sentry) = 1;
2869 DECL_ARTIFICIAL (sentry) = 1;
2870 TREE_STATIC (sentry) = 1;
2871 TREE_USED (sentry) = 1;
2872 DECL_COMMON (sentry) = 1;
2873 pushdecl_top_level (sentry);
2874 cp_finish_decl (sentry, NULL_TREE, NULL_TREE, 0, 0);
2875 pop_obstacks ();
2876 }
2877 return sentry;
2878 }
2879
2880 /* A list of objects which have constructors or destructors
2881 which reside in the global scope. The decl is stored in
2882 the TREE_VALUE slot and the initializer is stored
2883 in the TREE_PURPOSE slot. */
2884 extern tree static_aggregates_initp;
2885
2886 /* Set up the static_aggregates* lists for processing. Subroutine of
2887 finish_file. Note that this function changes the format of
2888 static_aggregates_initp, from (priority . decl) to
2889 (priority . ((initializer . decl) ...)). */
2890
2891 static void
2892 setup_initp ()
2893 {
2894 tree t, *p, next_t;
2895 tree default_pri = build_int_2 (DEFAULT_INIT_PRIORITY, 0);
2896 int saw_default;
2897
2898 /* First, remove any entries from static_aggregates that are also in
2899 static_aggregates_initp, and update the entries in _initp to include
2900 the initializer. For entries not in static_aggregates_initp, update
2901 them in place to look the same way. */
2902 p = &static_aggregates;
2903 for (; *p; )
2904 {
2905 t = value_member (TREE_VALUE (*p), static_aggregates_initp);
2906
2907 if (t)
2908 {
2909 /* We found an entry in s_a_initp; replace that entry with the
2910 format we want and remove the entry in s_a. */
2911 TREE_VALUE (t) = *p;
2912 *p = TREE_CHAIN (*p);
2913 TREE_CHAIN (TREE_VALUE (t)) = NULL_TREE;
2914 }
2915 else
2916 {
2917 /* We didn't find an entry in s_a_i; replace the entry in s_a
2918 with the format we want. */
2919 *p = perm_tree_cons (default_pri, *p, TREE_CHAIN (*p));
2920 TREE_CHAIN (TREE_VALUE (*p)) = NULL_TREE;
2921 p = &TREE_CHAIN (*p);
2922 }
2923 }
2924
2925 /* And then attach the two lists. By doing it this way, ctors with an
2926 explicit priority equal to the default are run before ctors with no
2927 explicit priority (i.e. the ones in s_a). */
2928 static_aggregates_initp = chainon (static_aggregates,
2929 static_aggregates_initp);
2930 static_aggregates = NULL_TREE;
2931
2932 /* Then, group static_aggregates_initp. After this step, there will only
2933 be one entry for each priority, with a chain coming off it. */
2934 t = static_aggregates_initp;
2935 static_aggregates_initp = NULL_TREE;
2936
2937 saw_default = 0;
2938 for (; t; t = next_t)
2939 {
2940 next_t = TREE_CHAIN (t);
2941 if (TREE_INT_CST_LOW (TREE_PURPOSE (t)) == DEFAULT_INIT_PRIORITY)
2942 saw_default = 1;
2943
2944 for (p = &static_aggregates_initp; ; p = &TREE_CHAIN (*p))
2945 {
2946 if (*p == NULL_TREE
2947 || tree_int_cst_lt (TREE_PURPOSE (*p), TREE_PURPOSE (t)))
2948 {
2949 TREE_CHAIN (t) = *p;
2950 *p = t;
2951 break;
2952 }
2953 else if (tree_int_cst_equal (TREE_PURPOSE (*p), TREE_PURPOSE (t)))
2954 {
2955 TREE_CHAIN (TREE_VALUE (t)) = TREE_VALUE (*p);
2956 TREE_VALUE (*p) = TREE_VALUE (t);
2957 break;
2958 }
2959 }
2960 }
2961
2962 if (! saw_default)
2963 static_aggregates_initp = perm_tree_cons (default_pri, error_mark_node,
2964 static_aggregates_initp);
2965
2966 /* Reverse each list to preserve the order (currently reverse declaration
2967 order, for destructors). */
2968 for (t = static_aggregates_initp; t; t = TREE_CHAIN (t))
2969 TREE_VALUE (t) = nreverse (TREE_VALUE (t));
2970 }
2971
2972 /* Start the process of running a particular set of global constructors
2973 or destructors. Subroutine of do_[cd]tors. */
2974
2975 static void
2976 start_objects (method_type, initp)
2977 int method_type, initp;
2978 {
2979 tree fnname;
2980 char type[10];
2981
2982 /* Make ctor or dtor function. METHOD_TYPE may be 'I' or 'D'. */
2983
2984 if (initp != DEFAULT_INIT_PRIORITY)
2985 {
2986 char joiner;
2987
2988 #ifdef JOINER
2989 joiner = JOINER;
2990 #else
2991 joiner = '_';
2992 #endif
2993
2994 sprintf (type, "%c%c%.5u", method_type, joiner, initp);
2995 }
2996 else
2997 sprintf (type, "%c", method_type);
2998
2999 fnname = get_file_function_name_long (type);
3000
3001 start_function (void_list_node,
3002 make_call_declarator (fnname, void_list_node, NULL_TREE,
3003 NULL_TREE),
3004 NULL_TREE, 0);
3005
3006 #if defined(ASM_OUTPUT_CONSTRUCTOR) && defined(ASM_OUTPUT_DESTRUCTOR)
3007 /* It can be a static function as long as collect2 does not have
3008 to scan the object file to find its ctor/dtor routine. */
3009 TREE_PUBLIC (current_function_decl) = 0;
3010 #endif
3011
3012 store_parm_decls ();
3013 pushlevel (0);
3014 clear_last_expr ();
3015 push_momentary ();
3016 expand_start_bindings (0);
3017 }
3018
3019 /* Finish the process of running a particular set of global constructors
3020 or destructors. Subroutine of do_[cd]tors. */
3021
3022 static void
3023 finish_objects (method_type, initp)
3024 int method_type, initp;
3025 {
3026 char *fnname;
3027
3028 if (initp == DEFAULT_INIT_PRIORITY)
3029 {
3030 tree list = (method_type == 'I' ? static_ctors : static_dtors);
3031
3032 if (! current_function_decl && list)
3033 start_objects (method_type, initp);
3034
3035 for (; list; list = TREE_CHAIN (list))
3036 expand_expr_stmt (build_function_call (TREE_VALUE (list), NULL_TREE));
3037 }
3038
3039 if (! current_function_decl)
3040 return;
3041
3042 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
3043
3044 /* Finish up. */
3045 expand_end_bindings (getdecls (), 1, 0);
3046 poplevel (1, 0, 0);
3047 pop_momentary ();
3048 finish_function (lineno, 0, 0);
3049
3050 if (initp == DEFAULT_INIT_PRIORITY)
3051 {
3052 if (method_type == 'I')
3053 assemble_constructor (fnname);
3054 else
3055 assemble_destructor (fnname);
3056 }
3057
3058 #ifdef ASM_OUTPUT_SECTION_NAME
3059 /* If we're using init priority we can't use assemble_*tor, but on ELF
3060 targets we can stick the references into named sections for GNU ld
3061 to collect. */
3062 else
3063 {
3064 char buf[15];
3065 sprintf (buf, ".%ctors.%.5u", method_type == 'I' ? 'c' : 'd',
3066 /* invert the numbering so the linker puts us in the proper
3067 order; constructors are run from right to left, and the
3068 linker sorts in increasing order. */
3069 MAX_INIT_PRIORITY - initp);
3070 named_section (NULL_TREE, buf, 0);
3071 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, fnname),
3072 POINTER_SIZE / BITS_PER_UNIT, 1);
3073 }
3074 #endif
3075 }
3076
3077 /* Generate a function to run a set of global destructors. START is either
3078 NULL_TREE or a node indicating a set of destructors with the same
3079 init priority. Subroutine of finish_file. */
3080
3081 static void
3082 do_dtors (start)
3083 tree start;
3084 {
3085 tree vars;
3086 int initp;
3087
3088 initp = TREE_INT_CST_LOW (TREE_PURPOSE (start));
3089 vars = TREE_VALUE (start);
3090
3091 for (; vars && vars != error_mark_node; vars = TREE_CHAIN (vars))
3092 {
3093 tree decl = TREE_VALUE (vars);
3094 tree type = TREE_TYPE (decl);
3095 tree temp;
3096
3097 if (TYPE_NEEDS_DESTRUCTOR (type) && ! TREE_STATIC (vars)
3098 && ! DECL_EXTERNAL (decl))
3099 {
3100 int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3101 || DECL_ONE_ONLY (decl)
3102 || DECL_WEAK (decl)));
3103
3104 if (! current_function_decl)
3105 start_objects ('D', initp);
3106
3107 /* Set these global variables so that GDB at least puts
3108 us near the declaration which required the initialization. */
3109 input_filename = DECL_SOURCE_FILE (decl);
3110 lineno = DECL_SOURCE_LINE (decl);
3111 emit_note (input_filename, lineno);
3112
3113 /* Because of:
3114
3115 [class.access.spec]
3116
3117 Access control for implicit calls to the constructors,
3118 the conversion functions, or the destructor called to
3119 create and destroy a static data member is performed as
3120 if these calls appeared in the scope of the member's
3121 class.
3122
3123 we must convince enforce_access to let us access the
3124 DECL. */
3125 if (member_p (decl))
3126 {
3127 DECL_CLASS_CONTEXT (current_function_decl)
3128 = DECL_CONTEXT (decl);
3129 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3130 }
3131
3132 temp = build_cleanup (decl);
3133
3134 if (protect)
3135 {
3136 tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3137 sentry = build_unary_op (PREDECREMENT_EXPR, sentry, 0);
3138 sentry = build_binary_op (EQ_EXPR, sentry, integer_zero_node, 1);
3139 expand_start_cond (sentry, 0);
3140 }
3141
3142 expand_expr_stmt (temp);
3143
3144 if (protect)
3145 expand_end_cond ();
3146
3147 /* Now that we're done with DECL we don't need to pretend to
3148 be a member of its class any longer. */
3149 DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
3150 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3151 }
3152 }
3153
3154 finish_objects ('D', initp);
3155 }
3156
3157 /* Generate a function to run a set of global constructors. START is
3158 either NULL_TREE or a node indicating a set of constructors with the
3159 same init priority. Subroutine of finish_file. */
3160
3161 static void
3162 do_ctors (start)
3163 tree start;
3164 {
3165 tree vars;
3166 int initp;
3167
3168 initp = TREE_INT_CST_LOW (TREE_PURPOSE (start));
3169 vars = TREE_VALUE (start);
3170
3171 /* Reverse the list so it's in the right order for ctors. */
3172 vars = nreverse (vars);
3173
3174 for (; vars && vars != error_mark_node; vars = TREE_CHAIN (vars))
3175 {
3176 tree decl = TREE_VALUE (vars);
3177 tree init = TREE_PURPOSE (vars);
3178
3179 /* If this was a static attribute within some function's scope,
3180 then don't initialize it here. Also, don't bother
3181 with initializers that contain errors. */
3182 if (TREE_STATIC (vars)
3183 || DECL_EXTERNAL (decl)
3184 || (init && TREE_CODE (init) == TREE_LIST
3185 && value_member (error_mark_node, init)))
3186 continue;
3187
3188 if (TREE_CODE (decl) == VAR_DECL)
3189 {
3190 int protect = (TREE_PUBLIC (decl) && (DECL_COMMON (decl)
3191 || DECL_ONE_ONLY (decl)
3192 || DECL_WEAK (decl)));
3193
3194 if (! current_function_decl)
3195 start_objects ('I', initp);
3196
3197 /* Set these global variables so that GDB at least puts
3198 us near the declaration which required the initialization. */
3199 input_filename = DECL_SOURCE_FILE (decl);
3200 lineno = DECL_SOURCE_LINE (decl);
3201 emit_note (input_filename, lineno);
3202
3203 /* 9.5p5: The initializer of a static member of a class has
3204 the same access rights as a member function. */
3205 if (member_p (decl))
3206 {
3207 DECL_CLASS_CONTEXT (current_function_decl)
3208 = DECL_CONTEXT (decl);
3209 DECL_STATIC_FUNCTION_P (current_function_decl) = 1;
3210 }
3211
3212 if (protect)
3213 {
3214 tree sentry = get_sentry (DECL_ASSEMBLER_NAME (decl));
3215 sentry = build_unary_op (PREINCREMENT_EXPR, sentry, 0);
3216 sentry = build_binary_op
3217 (EQ_EXPR, sentry, integer_one_node, 1);
3218 expand_start_cond (sentry, 0);
3219 }
3220
3221 expand_start_target_temps ();
3222
3223 if (IS_AGGR_TYPE (TREE_TYPE (decl))
3224 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
3225 expand_aggr_init (decl, init, 0);
3226 else if (TREE_CODE (init) == TREE_VEC)
3227 {
3228 expand_expr (expand_vec_init (decl, TREE_VEC_ELT (init, 0),
3229 TREE_VEC_ELT (init, 1),
3230 TREE_VEC_ELT (init, 2), 0),
3231 const0_rtx, VOIDmode, EXPAND_NORMAL);
3232 }
3233 else
3234 expand_assignment (decl, init, 0, 0);
3235
3236 /* The expression might have involved increments and
3237 decrements. */
3238 emit_queue ();
3239
3240 /* Cleanup any temporaries needed for the initial value. */
3241 expand_end_target_temps ();
3242
3243 if (protect)
3244 expand_end_cond ();
3245
3246 DECL_CLASS_CONTEXT (current_function_decl) = NULL_TREE;
3247 DECL_STATIC_FUNCTION_P (current_function_decl) = 0;
3248 }
3249 else if (decl == error_mark_node)
3250 /* OK */;
3251 else
3252 my_friendly_abort (22);
3253 }
3254
3255 finish_objects ('I', initp);
3256 }
3257
3258 /* This routine is called from the last rule in yyparse ().
3259 Its job is to create all the code needed to initialize and
3260 destroy the global aggregates. We do the destruction
3261 first, since that way we only need to reverse the decls once. */
3262
3263 void
3264 finish_file ()
3265 {
3266 extern int lineno;
3267 int start_time, this_time;
3268
3269 tree fnname;
3270 tree vars;
3271 int needs_cleaning = 0, needs_messing_up = 0;
3272
3273 at_eof = 1;
3274
3275 /* Bad parse errors. Just forget about it. */
3276 if (! global_bindings_p () || current_class_type || decl_namespace_list)
3277 return;
3278
3279 start_time = get_run_time ();
3280
3281 /* Otherwise, GDB can get confused, because in only knows
3282 about source for LINENO-1 lines. */
3283 lineno -= 1;
3284
3285 interface_unknown = 1;
3286 interface_only = 0;
3287
3288 for (fnname = pending_templates; fnname; fnname = TREE_CHAIN (fnname))
3289 {
3290 tree srcloc = TREE_PURPOSE (fnname);
3291 tree decl = TREE_VALUE (fnname);
3292
3293 input_filename = SRCLOC_FILE (srcloc);
3294 lineno = SRCLOC_LINE (srcloc);
3295
3296 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
3297 {
3298 instantiate_class_template (decl);
3299 if (CLASSTYPE_TEMPLATE_INSTANTIATION (decl))
3300 for (vars = TYPE_METHODS (decl); vars; vars = TREE_CHAIN (vars))
3301 if (! DECL_ARTIFICIAL (vars))
3302 instantiate_decl (vars);
3303 }
3304 else
3305 instantiate_decl (decl);
3306 }
3307
3308 for (fnname = maybe_templates; fnname; fnname = TREE_CHAIN (fnname))
3309 {
3310 tree args, fn, decl = TREE_VALUE (fnname);
3311
3312 if (DECL_INITIAL (decl))
3313 continue;
3314
3315 fn = TREE_PURPOSE (fnname);
3316 args = get_bindings (fn, decl, NULL_TREE);
3317 fn = instantiate_template (fn, args);
3318 instantiate_decl (fn);
3319 }
3320
3321 cat_namespace_levels();
3322
3323 /* Push into C language context, because that's all
3324 we'll need here. */
3325 push_lang_context (lang_name_c);
3326
3327 #if 1
3328 /* The reason for pushing garbage onto the global_binding_level is to
3329 ensure that we can slice out _DECLs which pertain to virtual function
3330 tables. If the last thing pushed onto the global_binding_level was a
3331 virtual function table, then slicing it out would slice away all the
3332 decls (i.e., we lose the head of the chain).
3333
3334 There are several ways of getting the same effect, from changing the
3335 way that iterators over the chain treat the elements that pertain to
3336 virtual function tables, moving the implementation of this code to
3337 decl.c (where we can manipulate global_binding_level directly),
3338 popping the garbage after pushing it and slicing away the vtable
3339 stuff, or just leaving it alone. */
3340
3341 /* Make last thing in global scope not be a virtual function table. */
3342 #if 0 /* not yet, should get fixed properly later */
3343 vars = make_type_decl (get_identifier (" @%$#@!"), integer_type_node);
3344 #else
3345 vars = build_decl (TYPE_DECL, get_identifier (" @%$#@!"), integer_type_node);
3346 #endif
3347 DECL_IGNORED_P (vars) = 1;
3348 SET_DECL_ARTIFICIAL (vars);
3349 pushdecl (vars);
3350 #endif
3351
3352 for (vars = static_aggregates; vars; vars = TREE_CHAIN (vars))
3353 if (! TREE_ASM_WRITTEN (TREE_VALUE (vars)))
3354 rest_of_decl_compilation (TREE_VALUE (vars), 0, 1, 1);
3355 vars = static_aggregates;
3356
3357 if (static_ctors || vars)
3358 needs_messing_up = 1;
3359 if (static_dtors || vars)
3360 needs_cleaning = 1;
3361
3362 setup_initp ();
3363
3364 /* After setup_initp, the aggregates are listed in reverse declaration
3365 order, for cleaning. */
3366 if (needs_cleaning)
3367 for (vars = static_aggregates_initp; vars; vars = TREE_CHAIN (vars))
3368 do_dtors (vars);
3369
3370 /* do_ctors will reverse the lists for messing up. */
3371 if (needs_messing_up)
3372 for (vars = static_aggregates_initp; vars; vars = TREE_CHAIN (vars))
3373 do_ctors (vars);
3374
3375 permanent_allocation (1);
3376
3377 /* Done with C language context needs. */
3378 pop_lang_context ();
3379
3380 /* Let expand_static_init know it's too late for more ctors. */
3381 at_eof = 2;
3382
3383 /* Now write out any static class variables (which may have since
3384 learned how to be initialized). */
3385 for (; pending_statics; pending_statics = TREE_CHAIN (pending_statics))
3386 {
3387 tree decl = TREE_VALUE (pending_statics);
3388
3389 if (TREE_ASM_WRITTEN (decl))
3390 continue;
3391
3392 /* Output DWARF debug information. */
3393 #ifdef DWARF_DEBUGGING_INFO
3394 if (write_symbols == DWARF_DEBUG)
3395 dwarfout_file_scope_decl (decl, 1);
3396 #endif
3397 #ifdef DWARF2_DEBUGGING_INFO
3398 if (write_symbols == DWARF2_DEBUG)
3399 dwarf2out_decl (decl);
3400 #endif
3401
3402 /* We currently handle template statics here. We ought to handle
3403 them the same way we do template functions, i.e. only emit them if
3404 the symbol is needed. */
3405 import_export_decl (decl);
3406 if (DECL_NOT_REALLY_EXTERN (decl) && ! DECL_IN_AGGR_P (decl))
3407 DECL_EXTERNAL (decl) = 0;
3408
3409 rest_of_decl_compilation
3410 (decl, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), 1, 1);
3411 }
3412
3413 this_time = get_run_time ();
3414 parse_time -= this_time - start_time;
3415 varconst_time += this_time - start_time;
3416
3417 start_time = get_run_time ();
3418
3419 if (flag_handle_signatures)
3420 walk_sigtables ((void (*) PROTO ((tree, tree))) 0,
3421 finish_sigtable_vardecl);
3422
3423 for (fnname = saved_inlines; fnname; fnname = TREE_CHAIN (fnname))
3424 {
3425 tree decl = TREE_VALUE (fnname);
3426 import_export_decl (decl);
3427 }
3428
3429 mark_all_runtime_matches ();
3430
3431 /* Now write out inline functions which had their addresses taken and
3432 which were not declared virtual and which were not declared `extern
3433 inline'. */
3434 {
3435 int reconsider = 1; /* More may be referenced; check again */
3436
3437 while (reconsider)
3438 {
3439 tree *p = &saved_inlines;
3440 reconsider = 0;
3441
3442 /* We need to do this each time so that newly completed template
3443 types don't wind up at the front of the list. Sigh. */
3444 vars = build_decl (TYPE_DECL, make_anon_name (), integer_type_node);
3445 DECL_IGNORED_P (vars) = 1;
3446 SET_DECL_ARTIFICIAL (vars);
3447 pushdecl (vars);
3448
3449 reconsider |= walk_vtables ((void (*) PROTO((tree, tree))) 0,
3450 finish_vtable_vardecl);
3451
3452 while (*p)
3453 {
3454 tree decl = TREE_VALUE (*p);
3455
3456 if (DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
3457 && TREE_USED (decl)
3458 && (! DECL_REALLY_EXTERN (decl) || DECL_INLINE (decl)))
3459 {
3460 if (DECL_MUTABLE_P (decl))
3461 synthesize_tinfo_fn (decl);
3462 else
3463 synthesize_method (decl);
3464 reconsider = 1;
3465 }
3466
3467 /* Catch new template instantiations. */
3468 if (decl != TREE_VALUE (*p))
3469 continue;
3470
3471 if (TREE_ASM_WRITTEN (decl)
3472 || (DECL_SAVED_INSNS (decl) == 0
3473 && ! DECL_ARTIFICIAL (decl)))
3474 *p = TREE_CHAIN (*p);
3475 else if (DECL_INITIAL (decl) == 0)
3476 p = &TREE_CHAIN (*p);
3477 else if ((TREE_PUBLIC (decl) && ! DECL_COMDAT (decl))
3478 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
3479 || flag_keep_inline_functions)
3480 {
3481 if (DECL_NOT_REALLY_EXTERN (decl))
3482 {
3483 DECL_EXTERNAL (decl) = 0;
3484 reconsider = 1;
3485 /* We can't inline this function after it's been
3486 emitted. We want a variant of
3487 output_inline_function that doesn't prevent
3488 subsequent integration... */
3489 DECL_INLINE (decl) = 0;
3490 output_inline_function (decl);
3491 permanent_allocation (1);
3492 }
3493
3494 *p = TREE_CHAIN (*p);
3495 }
3496 else
3497 p = &TREE_CHAIN (*p);
3498 }
3499 }
3500
3501 /* It's possible that some of the remaining inlines will still be
3502 needed. For example, a static inline whose address is used in
3503 the initializer for a file-scope static variable will be
3504 needed. Code in compile_file will handle this, but we mustn't
3505 pretend that there are no definitions for the inlines, or it
3506 won't be able to.
3507
3508 FIXME: This won't catch member functions. We should really
3509 unify this stuff with the compile_file stuff. */
3510 for (vars = saved_inlines; vars != NULL_TREE; vars = TREE_CHAIN (vars))
3511 {
3512 tree decl = TREE_VALUE (vars);
3513
3514 if (DECL_NOT_REALLY_EXTERN (decl)
3515 && !DECL_COMDAT (decl)
3516 && DECL_INITIAL (decl) != NULL_TREE)
3517 DECL_EXTERNAL (decl) = 0;
3518 }
3519 }
3520
3521 /* Now delete from the chain of variables all virtual function tables.
3522 We output them all ourselves, because each will be treated
3523 specially. */
3524
3525 walk_vtables ((void (*) PROTO((tree, tree))) 0,
3526 prune_vtable_vardecl);
3527
3528 finish_repo ();
3529
3530 this_time = get_run_time ();
3531 parse_time -= this_time - start_time;
3532 varconst_time += this_time - start_time;
3533
3534 if (flag_detailed_statistics)
3535 {
3536 dump_tree_statistics ();
3537 dump_time_statistics ();
3538 }
3539 }
3540
3541 /* This is something of the form 'A()()()()()+1' that has turned out to be an
3542 expr. Since it was parsed like a type, we need to wade through and fix
3543 that. Unfortunately, since operator() is left-associative, we can't use
3544 tail recursion. In the above example, TYPE is `A', and DECL is
3545 `()()()()()'.
3546
3547 Maybe this shouldn't be recursive, but how often will it actually be
3548 used? (jason) */
3549
3550 tree
3551 reparse_absdcl_as_expr (type, decl)
3552 tree type, decl;
3553 {
3554 /* do build_functional_cast (type, NULL_TREE) at bottom */
3555 if (TREE_OPERAND (decl, 0) == NULL_TREE)
3556 return build_functional_cast (type, NULL_TREE);
3557
3558 /* recurse */
3559 decl = reparse_absdcl_as_expr (type, TREE_OPERAND (decl, 0));
3560
3561 decl = build_x_function_call (decl, NULL_TREE, current_class_ref);
3562
3563 if (TREE_CODE (decl) == CALL_EXPR
3564 && (! TREE_TYPE (decl)
3565 || TREE_CODE (TREE_TYPE (decl)) != VOID_TYPE))
3566 decl = require_complete_type (decl);
3567
3568 return decl;
3569 }
3570
3571 /* This is something of the form `int ((int)(int)(int)1)' that has turned
3572 out to be an expr. Since it was parsed like a type, we need to wade
3573 through and fix that. Since casts are right-associative, we are
3574 reversing the order, so we don't have to recurse.
3575
3576 In the above example, DECL is the `(int)(int)(int)', and EXPR is the
3577 `1'. */
3578
3579 tree
3580 reparse_absdcl_as_casts (decl, expr)
3581 tree decl, expr;
3582 {
3583 tree type;
3584
3585 if (TREE_CODE (expr) == CONSTRUCTOR
3586 && TREE_TYPE (expr) == 0)
3587 {
3588 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3589 decl = TREE_OPERAND (decl, 0);
3590
3591 if (IS_SIGNATURE (type))
3592 {
3593 error ("cast specifies signature type");
3594 return error_mark_node;
3595 }
3596
3597 expr = digest_init (type, expr, (tree *) 0);
3598 if (TREE_CODE (type) == ARRAY_TYPE && TYPE_SIZE (type) == 0)
3599 {
3600 int failure = complete_array_type (type, expr, 1);
3601 if (failure)
3602 my_friendly_abort (78);
3603 }
3604 }
3605
3606 while (decl)
3607 {
3608 type = groktypename (TREE_VALUE (TREE_OPERAND (decl, 1)));
3609 decl = TREE_OPERAND (decl, 0);
3610 expr = build_c_cast (type, expr);
3611 }
3612
3613 if (warn_old_style_cast)
3614 warning ("use of old-style cast");
3615
3616 return expr;
3617 }
3618
3619 /* Given plain tree nodes for an expression, build up the full semantics. */
3620
3621 tree
3622 build_expr_from_tree (t)
3623 tree t;
3624 {
3625 if (t == NULL_TREE || t == error_mark_node)
3626 return t;
3627
3628 switch (TREE_CODE (t))
3629 {
3630 case IDENTIFIER_NODE:
3631 return do_identifier (t, 0, NULL_TREE);
3632
3633 case LOOKUP_EXPR:
3634 if (LOOKUP_EXPR_GLOBAL (t))
3635 return do_scoped_id (TREE_OPERAND (t, 0), 0);
3636 else
3637 return do_identifier (TREE_OPERAND (t, 0), 0, NULL_TREE);
3638
3639 case TEMPLATE_ID_EXPR:
3640 return (lookup_template_function
3641 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3642 build_expr_from_tree (TREE_OPERAND (t, 1))));
3643
3644 case INDIRECT_REF:
3645 return build_x_indirect_ref
3646 (build_expr_from_tree (TREE_OPERAND (t, 0)), "unary *");
3647
3648 case CAST_EXPR:
3649 return build_functional_cast
3650 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3651
3652 case REINTERPRET_CAST_EXPR:
3653 return build_reinterpret_cast
3654 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3655
3656 case CONST_CAST_EXPR:
3657 return build_const_cast
3658 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3659
3660 case DYNAMIC_CAST_EXPR:
3661 return build_dynamic_cast
3662 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3663
3664 case STATIC_CAST_EXPR:
3665 return build_static_cast
3666 (TREE_TYPE (t), build_expr_from_tree (TREE_OPERAND (t, 0)));
3667
3668 case PREDECREMENT_EXPR:
3669 case PREINCREMENT_EXPR:
3670 case POSTDECREMENT_EXPR:
3671 case POSTINCREMENT_EXPR:
3672 case NEGATE_EXPR:
3673 case BIT_NOT_EXPR:
3674 case ABS_EXPR:
3675 case TRUTH_NOT_EXPR:
3676 case ADDR_EXPR:
3677 case CONVERT_EXPR: /* Unary + */
3678 if (TREE_TYPE (t))
3679 return t;
3680 return build_x_unary_op (TREE_CODE (t),
3681 build_expr_from_tree (TREE_OPERAND (t, 0)));
3682
3683 case PLUS_EXPR:
3684 case MINUS_EXPR:
3685 case MULT_EXPR:
3686 case TRUNC_DIV_EXPR:
3687 case CEIL_DIV_EXPR:
3688 case FLOOR_DIV_EXPR:
3689 case ROUND_DIV_EXPR:
3690 case EXACT_DIV_EXPR:
3691 case BIT_AND_EXPR:
3692 case BIT_ANDTC_EXPR:
3693 case BIT_IOR_EXPR:
3694 case BIT_XOR_EXPR:
3695 case TRUNC_MOD_EXPR:
3696 case FLOOR_MOD_EXPR:
3697 case TRUTH_ANDIF_EXPR:
3698 case TRUTH_ORIF_EXPR:
3699 case TRUTH_AND_EXPR:
3700 case TRUTH_OR_EXPR:
3701 case RSHIFT_EXPR:
3702 case LSHIFT_EXPR:
3703 case RROTATE_EXPR:
3704 case LROTATE_EXPR:
3705 case EQ_EXPR:
3706 case NE_EXPR:
3707 case MAX_EXPR:
3708 case MIN_EXPR:
3709 case LE_EXPR:
3710 case GE_EXPR:
3711 case LT_EXPR:
3712 case GT_EXPR:
3713 case MEMBER_REF:
3714 return build_x_binary_op
3715 (TREE_CODE (t),
3716 build_expr_from_tree (TREE_OPERAND (t, 0)),
3717 build_expr_from_tree (TREE_OPERAND (t, 1)));
3718
3719 case DOTSTAR_EXPR:
3720 return build_m_component_ref
3721 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3722 build_expr_from_tree (TREE_OPERAND (t, 1)));
3723
3724 case SCOPE_REF:
3725 return build_offset_ref (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1));
3726
3727 case ARRAY_REF:
3728 if (TREE_OPERAND (t, 0) == NULL_TREE)
3729 /* new-type-id */
3730 return build_parse_node (ARRAY_REF, NULL_TREE,
3731 build_expr_from_tree (TREE_OPERAND (t, 1)));
3732 return grok_array_decl (build_expr_from_tree (TREE_OPERAND (t, 0)),
3733 build_expr_from_tree (TREE_OPERAND (t, 1)));
3734
3735 case SIZEOF_EXPR:
3736 case ALIGNOF_EXPR:
3737 {
3738 tree r = build_expr_from_tree (TREE_OPERAND (t, 0));
3739 if (TREE_CODE_CLASS (TREE_CODE (r)) != 't')
3740 r = TREE_TYPE (r);
3741 return TREE_CODE (t) == SIZEOF_EXPR ? c_sizeof (r) : c_alignof (r);
3742 }
3743
3744 case MODOP_EXPR:
3745 return build_x_modify_expr
3746 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3747 TREE_CODE (TREE_OPERAND (t, 1)),
3748 build_expr_from_tree (TREE_OPERAND (t, 2)));
3749
3750 case ARROW_EXPR:
3751 return build_x_arrow
3752 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3753
3754 case NEW_EXPR:
3755 return build_new
3756 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3757 build_expr_from_tree (TREE_OPERAND (t, 1)),
3758 build_expr_from_tree (TREE_OPERAND (t, 2)),
3759 NEW_EXPR_USE_GLOBAL (t));
3760
3761 case DELETE_EXPR:
3762 return delete_sanity
3763 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3764 build_expr_from_tree (TREE_OPERAND (t, 1)),
3765 DELETE_EXPR_USE_VEC (t), DELETE_EXPR_USE_GLOBAL (t));
3766
3767 case COMPOUND_EXPR:
3768 if (TREE_OPERAND (t, 1) == NULL_TREE)
3769 return build_x_compound_expr
3770 (build_expr_from_tree (TREE_OPERAND (t, 0)));
3771 else
3772 my_friendly_abort (42);
3773
3774 case METHOD_CALL_EXPR:
3775 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3776 {
3777 tree ref = TREE_OPERAND (t, 0);
3778 return build_scoped_method_call
3779 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3780 build_expr_from_tree (TREE_OPERAND (ref, 0)),
3781 TREE_OPERAND (ref, 1),
3782 build_expr_from_tree (TREE_OPERAND (t, 2)));
3783 }
3784 else
3785 {
3786 tree fn = TREE_OPERAND (t, 0);
3787
3788 /* We can get a TEMPLATE_ID_EXPR here on code like:
3789
3790 x->f<2>();
3791
3792 so we must resolve that. However, we can also get things
3793 like a BIT_NOT_EXPR here, when referring to a destructor,
3794 and things like that are not correctly resolved by
3795 build_expr_from_tree. So, just use build_expr_from_tree
3796 when we really need it. */
3797 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
3798 fn = build_expr_from_tree (fn);
3799
3800 return build_method_call
3801 (build_expr_from_tree (TREE_OPERAND (t, 1)),
3802 fn,
3803 build_expr_from_tree (TREE_OPERAND (t, 2)),
3804 NULL_TREE, LOOKUP_NORMAL);
3805 }
3806
3807 case CALL_EXPR:
3808 if (TREE_CODE (TREE_OPERAND (t, 0)) == SCOPE_REF)
3809 {
3810 tree ref = TREE_OPERAND (t, 0);
3811 return build_member_call
3812 (build_expr_from_tree (TREE_OPERAND (ref, 0)),
3813 TREE_OPERAND (ref, 1),
3814 build_expr_from_tree (TREE_OPERAND (t, 1)));
3815 }
3816 else
3817 {
3818 tree name = TREE_OPERAND (t, 0);
3819 tree id;
3820 tree args = build_expr_from_tree (TREE_OPERAND (t, 1));
3821 if (args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
3822 && !LOOKUP_EXPR_GLOBAL (name)
3823 && TREE_CODE ((id = TREE_OPERAND (name, 0))) == IDENTIFIER_NODE
3824 && (!current_class_type
3825 || !lookup_member (current_class_type, id, 0, 0)))
3826 {
3827 /* Do Koenig lookup if there are no class members. */
3828 name = do_identifier (id, 0, args);
3829 }
3830 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
3831 || ! really_overloaded_fn (name))
3832 name = build_expr_from_tree (name);
3833 return build_x_function_call (name, args, current_class_ref);
3834 }
3835
3836 case COND_EXPR:
3837 return build_x_conditional_expr
3838 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3839 build_expr_from_tree (TREE_OPERAND (t, 1)),
3840 build_expr_from_tree (TREE_OPERAND (t, 2)));
3841
3842 case TREE_LIST:
3843 {
3844 tree purpose, value, chain;
3845
3846 if (t == void_list_node)
3847 return t;
3848
3849 purpose = TREE_PURPOSE (t);
3850 if (purpose)
3851 purpose = build_expr_from_tree (purpose);
3852 value = TREE_VALUE (t);
3853 if (value)
3854 value = build_expr_from_tree (value);
3855 chain = TREE_CHAIN (t);
3856 if (chain && chain != void_type_node)
3857 chain = build_expr_from_tree (chain);
3858 return expr_tree_cons (purpose, value, chain);
3859 }
3860
3861 case COMPONENT_REF:
3862 return build_x_component_ref
3863 (build_expr_from_tree (TREE_OPERAND (t, 0)),
3864 TREE_OPERAND (t, 1), NULL_TREE, 1);
3865
3866 case THROW_EXPR:
3867 return build_throw (build_expr_from_tree (TREE_OPERAND (t, 0)));
3868
3869 case CONSTRUCTOR:
3870 {
3871 tree r;
3872
3873 /* digest_init will do the wrong thing if we let it. */
3874 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3875 return t;
3876
3877 r = build_nt (CONSTRUCTOR, NULL_TREE,
3878 build_expr_from_tree (CONSTRUCTOR_ELTS (t)));
3879 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
3880
3881 if (TREE_TYPE (t))
3882 return digest_init (TREE_TYPE (t), r, 0);
3883 return r;
3884 }
3885
3886 case TYPEID_EXPR:
3887 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t, 0))) == 't')
3888 return get_typeid (TREE_OPERAND (t, 0));
3889 return build_x_typeid (build_expr_from_tree (TREE_OPERAND (t, 0)));
3890
3891 case VAR_DECL:
3892 return convert_from_reference (t);
3893
3894 default:
3895 return t;
3896 }
3897 }
3898
3899 /* This is something of the form `int (*a)++' that has turned out to be an
3900 expr. It was only converted into parse nodes, so we need to go through
3901 and build up the semantics. Most of the work is done by
3902 build_expr_from_tree, above.
3903
3904 In the above example, TYPE is `int' and DECL is `*a'. */
3905
3906 tree
3907 reparse_decl_as_expr (type, decl)
3908 tree type, decl;
3909 {
3910 decl = build_expr_from_tree (decl);
3911 if (type)
3912 return build_functional_cast (type, build_expr_list (NULL_TREE, decl));
3913 else
3914 return decl;
3915 }
3916
3917 /* This is something of the form `int (*a)' that has turned out to be a
3918 decl. It was only converted into parse nodes, so we need to do the
3919 checking that make_{pointer,reference}_declarator do. */
3920
3921 tree
3922 finish_decl_parsing (decl)
3923 tree decl;
3924 {
3925 extern int current_class_depth;
3926
3927 switch (TREE_CODE (decl))
3928 {
3929 case IDENTIFIER_NODE:
3930 return decl;
3931 case INDIRECT_REF:
3932 return make_pointer_declarator
3933 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3934 case ADDR_EXPR:
3935 return make_reference_declarator
3936 (NULL_TREE, finish_decl_parsing (TREE_OPERAND (decl, 0)));
3937 case BIT_NOT_EXPR:
3938 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3939 return decl;
3940 case SCOPE_REF:
3941 push_nested_class (TREE_TYPE (TREE_OPERAND (decl, 0)), 3);
3942 TREE_COMPLEXITY (decl) = current_class_depth;
3943 return decl;
3944 case ARRAY_REF:
3945 TREE_OPERAND (decl, 0) = finish_decl_parsing (TREE_OPERAND (decl, 0));
3946 return decl;
3947 case TREE_LIST:
3948 /* For attribute handling. */
3949 TREE_VALUE (decl) = finish_decl_parsing (TREE_VALUE (decl));
3950 return decl;
3951 default:
3952 my_friendly_abort (5);
3953 return NULL_TREE;
3954 }
3955 }
3956
3957 tree
3958 check_cp_case_value (value)
3959 tree value;
3960 {
3961 if (value == NULL_TREE)
3962 return value;
3963
3964 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3965 STRIP_TYPE_NOPS (value);
3966
3967 if (TREE_READONLY_DECL_P (value))
3968 {
3969 value = decl_constant_value (value);
3970 STRIP_TYPE_NOPS (value);
3971 }
3972 value = fold (value);
3973
3974 if (TREE_CODE (value) != INTEGER_CST
3975 && value != error_mark_node)
3976 {
3977 cp_error ("case label `%E' does not reduce to an integer constant",
3978 value);
3979 value = error_mark_node;
3980 }
3981 else
3982 /* Promote char or short to int. */
3983 value = default_conversion (value);
3984
3985 constant_expression_warning (value);
3986
3987 return value;
3988 }
3989
3990 /* Return 1 if root encloses child. */
3991
3992 static int
3993 is_namespace_ancestor (root, child)
3994 tree root, child;
3995 {
3996 if (root == child)
3997 return 1;
3998 if (root == global_namespace)
3999 return 1;
4000 if (child == global_namespace)
4001 return 0;
4002 return is_namespace_ancestor (root, CP_DECL_CONTEXT (child));
4003 }
4004
4005
4006 /* Return the namespace that is the common ancestor
4007 of two given namespaces. */
4008
4009 tree
4010 namespace_ancestor (ns1, ns2)
4011 tree ns1, ns2;
4012 {
4013 if (is_namespace_ancestor (ns1, ns2))
4014 return ns1;
4015 return namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2);
4016 }
4017
4018 /* Insert used into the using list of user. Set indirect_flag if this
4019 directive is not directly from the source. Also find the common
4020 ancestor and let our users know about the new namespace */
4021 static void
4022 add_using_namespace (user, used, indirect)
4023 tree user;
4024 tree used;
4025 int indirect;
4026 {
4027 tree t;
4028 /* Using oneself is a no-op. */
4029 if (user == used)
4030 return;
4031 my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
4032 my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
4033 /* Check if we already have this. */
4034 t = purpose_member (used, DECL_NAMESPACE_USING (user));
4035 if (t != NULL_TREE)
4036 {
4037 if (!indirect)
4038 /* Promote to direct usage. */
4039 TREE_INDIRECT_USING (t) = 0;
4040 return;
4041 }
4042
4043 /* Add used to the user's using list. */
4044 DECL_NAMESPACE_USING (user)
4045 = perm_tree_cons (used, namespace_ancestor (user, used),
4046 DECL_NAMESPACE_USING (user));
4047
4048 TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
4049
4050 /* Add user to the used's users list. */
4051 DECL_NAMESPACE_USERS (used)
4052 = perm_tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
4053
4054 /* Recursively add all namespaces used. */
4055 for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
4056 /* indirect usage */
4057 add_using_namespace (user, TREE_PURPOSE (t), 1);
4058
4059 /* Tell everyone using us about the new used namespaces. */
4060 for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
4061 add_using_namespace (TREE_PURPOSE (t), used, 1);
4062 }
4063
4064 /* Combines two sets of overloaded functions into an OVERLOAD chain, removing
4065 duplicates. The first list becomes the tail of the result.
4066
4067 The algorithm is O(n^2). We could get this down to O(n log n) by
4068 doing a sort on the addresses of the functions, if that becomes
4069 necessary. */
4070
4071 static tree
4072 merge_functions (s1, s2)
4073 tree s1;
4074 tree s2;
4075 {
4076 for (; s2; s2 = OVL_NEXT (s2))
4077 {
4078 tree fn = OVL_CURRENT (s2);
4079 if (! ovl_member (fn, s1))
4080 s1 = build_overload (fn, s1);
4081 }
4082 return s1;
4083 }
4084
4085 /* This should return an error not all definitions define functions.
4086 It is not an error if we find two functions with exactly the
4087 same signature, only if these are selected in overload resolution.
4088 old is the current set of bindings, new the freshly-found binding.
4089 XXX Do we want to give *all* candidates in case of ambiguity?
4090 XXX In what way should I treat extern declarations?
4091 XXX I don't want to repeat the entire duplicate_decls here */
4092
4093 static tree
4094 ambiguous_decl (name, old, new, flags)
4095 tree name;
4096 tree old;
4097 tree new;
4098 int flags;
4099 {
4100 tree val, type;
4101 my_friendly_assert (old != NULL_TREE, 393);
4102 /* Copy the value. */
4103 val = BINDING_VALUE (new);
4104 if (val)
4105 switch (TREE_CODE (val))
4106 {
4107 case TEMPLATE_DECL:
4108 /* If we expect types or namespaces, and not templates,
4109 or this is not a template class. */
4110 if (LOOKUP_QUALIFIERS_ONLY (flags)
4111 && !DECL_CLASS_TEMPLATE_P (val))
4112 val = NULL_TREE;
4113 break;
4114 case TYPE_DECL:
4115 if (LOOKUP_NAMESPACES_ONLY (flags))
4116 val = NULL_TREE;
4117 break;
4118 case NAMESPACE_DECL:
4119 if (LOOKUP_TYPES_ONLY (flags))
4120 val = NULL_TREE;
4121 break;
4122 default:
4123 if (LOOKUP_QUALIFIERS_ONLY (flags))
4124 val = NULL_TREE;
4125 }
4126
4127 if (!BINDING_VALUE (old))
4128 BINDING_VALUE (old) = val;
4129 else if (val && val != BINDING_VALUE (old))
4130 {
4131 if (is_overloaded_fn (BINDING_VALUE (old))
4132 && is_overloaded_fn (val))
4133 {
4134 BINDING_VALUE (old) = merge_functions (BINDING_VALUE (old),
4135 val);
4136 }
4137 else
4138 {
4139 /* Some declarations are functions, some are not. */
4140 if (flags & LOOKUP_COMPLAIN)
4141 {
4142 /* If we've already given this error for this lookup,
4143 BINDING_VALUE (old) is error_mark_node, so let's not
4144 repeat ourselves. */
4145 if (BINDING_VALUE (old) != error_mark_node)
4146 {
4147 cp_error ("use of `%D' is ambiguous", name);
4148 cp_error_at (" first declared as `%#D' here",
4149 BINDING_VALUE (old));
4150 }
4151 cp_error_at (" also declared as `%#D' here", val);
4152 }
4153 return error_mark_node;
4154 }
4155 }
4156 /* ... and copy the type. */
4157 type = BINDING_TYPE (new);
4158 if (LOOKUP_NAMESPACES_ONLY (flags))
4159 type = NULL_TREE;
4160 if (!BINDING_TYPE (old))
4161 BINDING_TYPE (old) = type;
4162 else if (type && BINDING_TYPE (old) != type)
4163 {
4164 if (flags & LOOKUP_COMPLAIN)
4165 {
4166 cp_error ("`%D' denotes an ambiguous type",name);
4167 cp_error_at (" first type here", BINDING_TYPE (old));
4168 cp_error_at (" other type here", type);
4169 }
4170 }
4171 return old;
4172 }
4173
4174 /* Add the bindings of name in used namespaces to val.
4175 The using list is defined by usings, and the lookup goes to scope.
4176 Returns zero on errors. */
4177
4178 int
4179 lookup_using_namespace (name, val, usings, scope, flags)
4180 tree name, val, usings, scope;
4181 int flags;
4182 {
4183 tree iter;
4184 tree val1;
4185 /* Iterate over all used namespaces in current, searching for using
4186 directives of scope. */
4187 for (iter = usings; iter; iter = TREE_CHAIN (iter))
4188 if (TREE_VALUE (iter) == scope)
4189 {
4190 val1 = binding_for_name (name, TREE_PURPOSE (iter));
4191 /* Resolve ambiguities. */
4192 val = ambiguous_decl (name, val, val1, flags);
4193 }
4194 return val != error_mark_node;
4195 }
4196
4197 /* [namespace.qual]
4198 Excepts the name to lookup and its qualifying scope.
4199 Returns the name/type pair found into the CPLUS_BINDING result,
4200 or 0 on error. */
4201
4202 int
4203 qualified_lookup_using_namespace (name, scope, result, flags)
4204 tree name;
4205 tree scope;
4206 tree result;
4207 int flags;
4208 {
4209 /* Maintain a list of namespaces visited... */
4210 tree seen = NULL_TREE;
4211 /* ... and a list of namespace yet to see. */
4212 tree todo = NULL_TREE;
4213 tree usings;
4214 while (scope && (result != error_mark_node))
4215 {
4216 seen = temp_tree_cons (scope, NULL_TREE, seen);
4217 result = ambiguous_decl (name, result,
4218 binding_for_name (name, scope), flags);
4219 if (!BINDING_VALUE (result) && !BINDING_TYPE (result))
4220 /* Consider using directives. */
4221 for (usings = DECL_NAMESPACE_USING (scope); usings;
4222 usings = TREE_CHAIN (usings))
4223 /* If this was a real directive, and we have not seen it. */
4224 if (!TREE_INDIRECT_USING (usings)
4225 && !purpose_member (TREE_PURPOSE (usings), seen))
4226 todo = temp_tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
4227 if (todo)
4228 {
4229 scope = TREE_PURPOSE (todo);
4230 todo = TREE_CHAIN (todo);
4231 }
4232 else
4233 scope = NULL_TREE; /* If there never was a todo list. */
4234 }
4235 return result != error_mark_node;
4236 }
4237
4238 /* [namespace.memdef]/2 */
4239
4240 /* Set the context of a declaration to scope. Complain if we are not
4241 outside scope. */
4242
4243 void
4244 set_decl_namespace (decl, scope, friendp)
4245 tree decl;
4246 tree scope;
4247 int friendp;
4248 {
4249 tree old;
4250 if (scope == std_node)
4251 scope = global_namespace;
4252 /* Get rid of namespace aliases. */
4253 scope = ORIGINAL_NAMESPACE (scope);
4254
4255 /* It is ok for friends to be qualified in parallel space. */
4256 if (!friendp && !is_namespace_ancestor (current_namespace, scope))
4257 cp_error ("declaration of `%D' not in a namespace surrounding `%D'",
4258 decl, scope);
4259 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
4260 if (scope != current_namespace)
4261 {
4262 /* See whether this has been declared in the namespace. */
4263 old = namespace_binding (DECL_NAME (decl), scope);
4264 if (!old)
4265 /* No old declaration at all. */
4266 goto complain;
4267 if (!is_overloaded_fn (decl))
4268 /* Don't compare non-function decls with decls_match here,
4269 since it can't check for the correct constness at this
4270 point. pushdecl will find those errors later. */
4271 return;
4272 /* Since decl is a function, old should contain a function decl. */
4273 if (!is_overloaded_fn (old))
4274 goto complain;
4275 for (; old; old = OVL_NEXT (old))
4276 if (decls_match (decl, OVL_CURRENT (old)))
4277 return;
4278 }
4279 else
4280 return;
4281 complain:
4282 cp_error ("`%D' should have been declared inside `%D'",
4283 decl, scope);
4284 }
4285
4286 /* Compute the namespace where a declaration is defined. */
4287
4288 static tree
4289 decl_namespace (decl)
4290 tree decl;
4291 {
4292 while (DECL_CONTEXT (decl))
4293 {
4294 decl = DECL_CONTEXT (decl);
4295 if (TREE_CODE (decl) == NAMESPACE_DECL)
4296 return decl;
4297 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
4298 decl = TYPE_STUB_DECL (decl);
4299 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 390);
4300 }
4301
4302 return global_namespace;
4303 }
4304
4305 /* Return the namespace where the current declaration is declared. */
4306
4307 tree
4308 current_decl_namespace ()
4309 {
4310 tree result;
4311 /* If we have been pushed into a different namespace, use it. */
4312 if (decl_namespace_list)
4313 return TREE_PURPOSE (decl_namespace_list);
4314
4315 if (current_class_type)
4316 result = decl_namespace (TYPE_STUB_DECL (current_class_type));
4317 else if (current_function_decl)
4318 result = decl_namespace (current_function_decl);
4319 else
4320 result = current_namespace;
4321 return result;
4322 }
4323
4324 /* Temporarily set the namespace for the current declaration. */
4325
4326 void
4327 push_decl_namespace (decl)
4328 tree decl;
4329 {
4330 if (TREE_CODE (decl) != NAMESPACE_DECL)
4331 decl = decl_namespace (decl);
4332 decl_namespace_list = tree_cons (decl, NULL_TREE, decl_namespace_list);
4333 }
4334
4335 void
4336 pop_decl_namespace ()
4337 {
4338 decl_namespace_list = TREE_CHAIN (decl_namespace_list);
4339 }
4340
4341 /* Enter a class or namespace scope. */
4342
4343 void
4344 push_scope (t)
4345 tree t;
4346 {
4347 if (TREE_CODE (t) == NAMESPACE_DECL)
4348 push_decl_namespace (t);
4349 else
4350 pushclass (t, 2);
4351 }
4352
4353 /* Leave scope pushed by push_scope. */
4354
4355 void
4356 pop_scope (t)
4357 tree t;
4358 {
4359 if (TREE_CODE (t) == NAMESPACE_DECL)
4360 pop_decl_namespace ();
4361 else
4362 popclass (1);
4363 }
4364
4365 /* [basic.lookup.koenig] */
4366 /* A non-zero return value in the functions below indicates an error.
4367 All nodes allocated in the procedure are on the scratch obstack. */
4368
4369 struct arg_lookup
4370 {
4371 tree name;
4372 tree namespaces;
4373 tree classes;
4374 tree functions;
4375 };
4376
4377 static int arg_assoc PROTO((struct arg_lookup*, tree));
4378 static int arg_assoc_args PROTO((struct arg_lookup*, tree));
4379 static int arg_assoc_type PROTO((struct arg_lookup*, tree));
4380 static int add_function PROTO((struct arg_lookup *, tree));
4381 static int arg_assoc_namespace PROTO((struct arg_lookup *, tree));
4382 static int arg_assoc_class PROTO((struct arg_lookup *, tree));
4383
4384 /* Add a function to the lookup structure.
4385 Returns 1 on error. */
4386
4387 static int
4388 add_function (k, fn)
4389 struct arg_lookup *k;
4390 tree fn;
4391 {
4392 if (ovl_member (fn, k->functions))
4393 return 0;
4394 /* We must find only functions, or exactly one non-function. */
4395 if (k->functions && is_overloaded_fn (k->functions)
4396 && is_overloaded_fn (fn))
4397 k->functions = build_overload (fn, k->functions);
4398 else
4399 if(k->functions)
4400 {
4401 tree f1 = OVL_CURRENT (k->functions);
4402 tree f2 = fn;
4403 if (is_overloaded_fn (f1))
4404 {
4405 fn = f1; f1 = f2; f2 = fn;
4406 }
4407 cp_error_at ("`%D' is not a function,", f1);
4408 cp_error_at (" conflict with `%D'", f2);
4409 cp_error (" in call to `%D'", k->name);
4410 return 1;
4411 }
4412 else
4413 k->functions = fn;
4414 return 0;
4415 }
4416
4417 /* Add functions of a namespace to the lookup structure.
4418 Returns 1 on error. */
4419
4420 static int
4421 arg_assoc_namespace (k, scope)
4422 struct arg_lookup *k;
4423 tree scope;
4424 {
4425 tree value;
4426
4427 if (purpose_member (scope, k->namespaces))
4428 return 0;
4429 k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
4430
4431 value = namespace_binding (k->name, scope);
4432 if (!value)
4433 return 0;
4434
4435 for (; value; value = OVL_NEXT (value))
4436 if (add_function (k, OVL_CURRENT (value)))
4437 return 1;
4438
4439 return 0;
4440 }
4441
4442 /* Adds everything associated with class to the lookup structure.
4443 Returns 1 on error. */
4444
4445 static int
4446 arg_assoc_class (k, type)
4447 struct arg_lookup* k;
4448 tree type;
4449 {
4450 tree list, friends, context;
4451 int i;
4452
4453 if (purpose_member (type, k->classes))
4454 return 0;
4455 k->classes = tree_cons (type, NULL_TREE, k->classes);
4456
4457 context = decl_namespace (TYPE_MAIN_DECL (type));
4458 if (arg_assoc_namespace (k, context))
4459 return 1;
4460
4461 /* Process baseclasses. */
4462 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
4463 if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
4464 return 1;
4465
4466 /* Process friends. */
4467 for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
4468 list = TREE_CHAIN (list))
4469 if (k->name == TREE_PURPOSE (list))
4470 for (friends = TREE_VALUE (list); friends;
4471 friends = TREE_CHAIN (friends))
4472 /* Only interested in global functions with potentially hidden
4473 (i.e. unqualified) declarations. */
4474 if (TREE_PURPOSE (list) == error_mark_node && TREE_VALUE (list)
4475 && decl_namespace (TREE_VALUE (list)) == context)
4476 if (add_function (k, TREE_VALUE (list)))
4477 return 1;
4478
4479 /* Process template arguments. */
4480 if (CLASSTYPE_TEMPLATE_INFO (type))
4481 {
4482 list = innermost_args (CLASSTYPE_TI_ARGS (type));
4483 for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
4484 arg_assoc (k, TREE_VEC_ELT (list, i));
4485 }
4486
4487 return 0;
4488 }
4489
4490 /* Adds everything associated with a given type.
4491 Returns 1 on error. */
4492
4493 static int
4494 arg_assoc_type (k, type)
4495 struct arg_lookup *k;
4496 tree type;
4497 {
4498 switch (TREE_CODE (type))
4499 {
4500 case VOID_TYPE:
4501 case INTEGER_TYPE:
4502 case REAL_TYPE:
4503 case COMPLEX_TYPE:
4504 case CHAR_TYPE:
4505 case BOOLEAN_TYPE:
4506 return 0;
4507 case RECORD_TYPE:
4508 if (TYPE_PTRMEMFUNC_P (type))
4509 return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
4510 return arg_assoc_class (k, type);
4511 case POINTER_TYPE:
4512 case REFERENCE_TYPE:
4513 case ARRAY_TYPE:
4514 return arg_assoc_type (k, TREE_TYPE (type));
4515 case UNION_TYPE:
4516 case ENUMERAL_TYPE:
4517 return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type)));
4518 case OFFSET_TYPE:
4519 /* Pointer to member: associate class type and value type. */
4520 if (arg_assoc_type (k, TYPE_OFFSET_BASETYPE (type)))
4521 return 1;
4522 return arg_assoc_type (k, TREE_TYPE (type));
4523 case METHOD_TYPE:
4524 /* The basetype is referenced in the first arg type, so just
4525 fall through. */
4526 case FUNCTION_TYPE:
4527 /* Associate the parameter types. */
4528 if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
4529 return 1;
4530 /* Associate the return type. */
4531 return arg_assoc_type (k, TREE_TYPE (type));
4532 case TEMPLATE_TYPE_PARM:
4533 case TEMPLATE_TEMPLATE_PARM:
4534 return 0;
4535 case LANG_TYPE:
4536 if (type == unknown_type_node)
4537 return 0;
4538 /* else fall through */
4539 default:
4540 my_friendly_abort (390);
4541 }
4542 return 0;
4543 }
4544
4545 /* Adds everything associated with arguments. Returns 1 on error. */
4546
4547 static int
4548 arg_assoc_args (k, args)
4549 struct arg_lookup* k;
4550 tree args;
4551 {
4552 for (; args; args = TREE_CHAIN (args))
4553 if (arg_assoc (k, TREE_VALUE (args)))
4554 return 1;
4555 return 0;
4556 }
4557
4558 /* Adds everything associated with a given tree_node. Returns 1 on error. */
4559
4560 static int
4561 arg_assoc (k, n)
4562 struct arg_lookup* k;
4563 tree n;
4564 {
4565 if (n == error_mark_node)
4566 return 0;
4567
4568 if (TREE_CODE_CLASS (TREE_CODE (n)) == 't')
4569 return arg_assoc_type (k, n);
4570
4571 if (! type_unknown_p (n))
4572 return arg_assoc_type (k, TREE_TYPE (n));
4573
4574 if (TREE_CODE (n) == ADDR_EXPR)
4575 n = TREE_OPERAND (n, 0);
4576 if (TREE_CODE (n) == COMPONENT_REF)
4577 n = TREE_OPERAND (n, 1);
4578 while (TREE_CODE (n) == TREE_LIST)
4579 n = TREE_VALUE (n);
4580
4581 if (TREE_CODE (n) == FUNCTION_DECL)
4582 return arg_assoc_type (k, TREE_TYPE (n));
4583 if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
4584 {
4585 /* [basic.lookup.koenig]
4586
4587 If T is a template-id, its associated namespaces and classes
4588 are the namespace in which the template is defined; for
4589 member templates, the member template's class; the namespaces
4590 and classes associated with the types of the template
4591 arguments provided for template type parameters (excluding
4592 template template parameters); the namespaces in which any
4593 template template arguments are defined; and the classes in
4594 which any member templates used as template template
4595 arguments are defined. [Note: non-type template arguments do
4596 not contribute to the set of associated namespaces. ] */
4597 tree template = TREE_OPERAND (n, 0);
4598 tree args = TREE_OPERAND (n, 1);
4599 tree ctx;
4600 tree arg;
4601
4602 /* First, the template. There may actually be more than one if
4603 this is an overloaded function template. But, in that case,
4604 we only need the first; all the functions will be in the same
4605 namespace. */
4606 template = OVL_CURRENT (template);
4607
4608 ctx = CP_DECL_CONTEXT (template);
4609
4610 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4611 {
4612 if (arg_assoc_namespace (k, ctx) == 1)
4613 return 1;
4614 }
4615 /* It must be a member template. */
4616 else if (arg_assoc_class (k, ctx) == 1)
4617 return 1;
4618
4619 /* Now the arguments. */
4620 for (arg = args; arg != NULL_TREE; arg = TREE_CHAIN (arg))
4621 {
4622 tree t = TREE_VALUE (arg);
4623
4624 if (TREE_CODE (t) == TEMPLATE_DECL)
4625 {
4626 ctx = CP_DECL_CONTEXT (t);
4627 if (TREE_CODE (ctx) == NAMESPACE_DECL)
4628 {
4629 if (arg_assoc_namespace (k, ctx) == 1)
4630 return 1;
4631 }
4632 else if (arg_assoc_class (k, ctx) == 1)
4633 return 1;
4634 }
4635 else if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
4636 && arg_assoc_type (k, t) == 1)
4637 return 1;
4638 }
4639 }
4640 else
4641 {
4642 my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
4643
4644 for (; n; n = OVL_CHAIN (n))
4645 if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
4646 return 1;
4647 }
4648
4649 return 0;
4650 }
4651
4652 /* Performs Koenig lookup depending on arguments, where fns
4653 are the functions found in normal lookup. */
4654
4655 tree
4656 lookup_arg_dependent (name, fns, args)
4657 tree name;
4658 tree fns;
4659 tree args;
4660 {
4661 struct arg_lookup k;
4662 k.name = name;
4663 k.functions = fns;
4664 k.namespaces = NULL_TREE;
4665 k.classes = NULL_TREE;
4666
4667 push_scratch_obstack ();
4668 arg_assoc_args (&k, args);
4669 pop_obstacks ();
4670 return k.functions;
4671 }
4672
4673 /* Process a namespace-alias declaration. */
4674
4675 void
4676 do_namespace_alias (alias, namespace)
4677 tree alias, namespace;
4678 {
4679 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4680 {
4681 /* The parser did not find it, so it's not there. */
4682 cp_error ("unknown namespace `%D'", namespace);
4683 return;
4684 }
4685
4686 namespace = ORIGINAL_NAMESPACE (namespace);
4687
4688 /* Build the alias. */
4689 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
4690 DECL_NAMESPACE_ALIAS (alias) = namespace;
4691 pushdecl (alias);
4692 }
4693
4694 /* Check a non-member using-declaration. Return the name and scope
4695 being used, and the USING_DECL, or NULL_TREE on failure. */
4696
4697 static tree
4698 validate_nonmember_using_decl (decl, scope, name)
4699 tree decl;
4700 tree *scope;
4701 tree *name;
4702 {
4703 if (TREE_CODE (decl) == SCOPE_REF
4704 && TREE_OPERAND (decl, 0) == std_node)
4705 {
4706 if (namespace_bindings_p ()
4707 && current_namespace == global_namespace)
4708 /* There's no need for a using declaration at all, here,
4709 since `std' is the same as `::'. We can't just pass this
4710 on because we'll complain later about declaring something
4711 in the same scope as a using declaration with the same
4712 name. We return NULL_TREE which indicates to the caller
4713 that there's no need to do any further processing. */
4714 return NULL_TREE;
4715
4716 *scope = global_namespace;
4717 *name = TREE_OPERAND (decl, 1);
4718 }
4719 else if (TREE_CODE (decl) == SCOPE_REF)
4720 {
4721 *scope = TREE_OPERAND (decl, 0);
4722 *name = TREE_OPERAND (decl, 1);
4723
4724 /* [namespace.udecl]
4725
4726 A using-declaration for a class member shall be a
4727 member-declaration. */
4728 if (TREE_CODE (*scope) != NAMESPACE_DECL)
4729 {
4730 if (TYPE_P (*scope))
4731 cp_error ("`%T' is not a namespace", *scope);
4732 else
4733 cp_error ("`%D' is not a namespace", *scope);
4734 return NULL_TREE;
4735 }
4736 }
4737 else if (TREE_CODE (decl) == IDENTIFIER_NODE
4738 || TREE_CODE (decl) == TYPE_DECL
4739 || TREE_CODE (decl) == TEMPLATE_DECL)
4740 {
4741 *scope = global_namespace;
4742 *name = decl;
4743 }
4744 else
4745 my_friendly_abort (382);
4746 if (TREE_CODE_CLASS (TREE_CODE (*name)) == 'd')
4747 *name = DECL_NAME (*name);
4748 /* Make a USING_DECL. */
4749 return push_using_decl (*scope, *name);
4750 }
4751
4752 /* Process local and global using-declarations. */
4753
4754 static void
4755 do_nonmember_using_decl (scope, name, oldval, oldtype, newval, newtype)
4756 tree scope, name;
4757 tree oldval, oldtype;
4758 tree *newval, *newtype;
4759 {
4760 tree decls;
4761 struct tree_binding _decls;
4762
4763 *newval = *newtype = NULL_TREE;
4764 decls = binding_init (&_decls);
4765 if (!qualified_lookup_using_namespace (name, scope, decls, 0))
4766 /* Lookup error */
4767 return;
4768
4769 if (!BINDING_VALUE (decls) && !BINDING_TYPE (decls))
4770 {
4771 cp_error ("`%D' not declared", name);
4772 return;
4773 }
4774
4775 /* Check for using functions. */
4776 if (BINDING_VALUE (decls) && is_overloaded_fn (BINDING_VALUE (decls)))
4777 {
4778 tree tmp, tmp1;
4779
4780 if (oldval && !is_overloaded_fn (oldval))
4781 {
4782 duplicate_decls (OVL_CURRENT (BINDING_VALUE (decls)), oldval);
4783 oldval = NULL_TREE;
4784 }
4785
4786 *newval = oldval;
4787 for (tmp = BINDING_VALUE (decls); tmp; tmp = OVL_NEXT (tmp))
4788 {
4789 tree new_fn = OVL_CURRENT (tmp);
4790
4791 /* [namespace.udecl]
4792
4793 If a function declaration in namespace scope or block
4794 scope has the same name and the same parameter types as a
4795 function introduced by a using declaration the program is
4796 ill-formed. */
4797 for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
4798 {
4799 tree old_fn = OVL_CURRENT (tmp1);
4800
4801 if (!OVL_USED (tmp1)
4802 && compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
4803 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
4804 {
4805 /* There was already a non-using declaration in
4806 this scope with the same parameter types. */
4807 cp_error ("`%D' is already declared in this scope",
4808 name);
4809 break;
4810 }
4811 else if (duplicate_decls (new_fn, old_fn))
4812 /* We're re-using something we already used
4813 before. We don't need to add it again. */
4814 break;
4815 }
4816
4817 /* If we broke out of the loop, there's no reason to add
4818 this function to the using declarations for this
4819 scope. */
4820 if (tmp1)
4821 continue;
4822
4823 *newval = build_overload (OVL_CURRENT (tmp), *newval);
4824 if (TREE_CODE (*newval) != OVERLOAD)
4825 *newval = ovl_cons (*newval, NULL_TREE);
4826 OVL_USED (*newval) = 1;
4827 }
4828 }
4829 else
4830 {
4831 *newval = BINDING_VALUE (decls);
4832 if (oldval)
4833 duplicate_decls (*newval, oldval);
4834 }
4835
4836 *newtype = BINDING_TYPE (decls);
4837 if (oldtype && *newtype && oldtype != *newtype)
4838 {
4839 cp_error ("using directive `%D' introduced ambiguous type `%T'",
4840 name, oldtype);
4841 return;
4842 }
4843 }
4844
4845 /* Process a using-declaration not appearing in class or local scope. */
4846
4847 void
4848 do_toplevel_using_decl (decl)
4849 tree decl;
4850 {
4851 tree scope, name, binding;
4852 tree oldval, oldtype, newval, newtype;
4853
4854 decl = validate_nonmember_using_decl (decl, &scope, &name);
4855 if (decl == NULL_TREE)
4856 return;
4857
4858 binding = binding_for_name (name, current_namespace);
4859
4860 oldval = BINDING_VALUE (binding);
4861 oldtype = BINDING_TYPE (binding);
4862
4863 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4864
4865 /* Copy declarations found. */
4866 if (newval)
4867 BINDING_VALUE (binding) = newval;
4868 if (newtype)
4869 BINDING_TYPE (binding) = newtype;
4870 return;
4871 }
4872
4873 /* Process a using-declaration at function scope. */
4874
4875 void
4876 do_local_using_decl (decl)
4877 tree decl;
4878 {
4879 tree scope, name;
4880 tree oldval, oldtype, newval, newtype;
4881
4882 decl = validate_nonmember_using_decl (decl, &scope, &name);
4883 if (decl == NULL_TREE)
4884 return;
4885
4886 oldval = lookup_name_current_level (name);
4887 oldtype = lookup_type_current_level (name);
4888
4889 do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
4890
4891 if (newval)
4892 {
4893 if (is_overloaded_fn (newval))
4894 {
4895 tree fn, term;
4896
4897 /* We only need to push declarations for those functions
4898 that were not already bound in the current level.
4899 The old value might be NULL_TREE, it might be a single
4900 function, or an OVERLOAD. */
4901 if (oldval && TREE_CODE (oldval) == OVERLOAD)
4902 term = OVL_FUNCTION (oldval);
4903 else
4904 term = oldval;
4905 for (fn = newval; fn && OVL_CURRENT (fn) != term;
4906 fn = OVL_NEXT (fn))
4907 push_overloaded_decl (OVL_CURRENT (fn),
4908 PUSH_LOCAL | PUSH_USING);
4909 }
4910 else
4911 push_local_binding (name, newval, PUSH_USING);
4912 }
4913 if (newtype)
4914 set_identifier_type_value (name, newtype);
4915 }
4916
4917 tree
4918 do_class_using_decl (decl)
4919 tree decl;
4920 {
4921 tree name, value;
4922
4923 if (TREE_CODE (decl) != SCOPE_REF
4924 || TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (decl, 0))) != 't')
4925 {
4926 cp_error ("using-declaration for non-member at class scope");
4927 return NULL_TREE;
4928 }
4929 name = TREE_OPERAND (decl, 1);
4930 if (TREE_CODE (name) == BIT_NOT_EXPR)
4931 {
4932 cp_error ("using-declaration for destructor");
4933 return NULL_TREE;
4934 }
4935 if (TREE_CODE (name) == TYPE_DECL)
4936 name = DECL_NAME (name);
4937
4938 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
4939
4940 value = build_lang_field_decl (USING_DECL, name, void_type_node);
4941 DECL_INITIAL (value) = TREE_OPERAND (decl, 0);
4942 return value;
4943 }
4944
4945 /* Process a using-directive. */
4946
4947 void
4948 do_using_directive (namespace)
4949 tree namespace;
4950 {
4951 if (namespace == std_node)
4952 return;
4953 /* using namespace A::B::C; */
4954 if (TREE_CODE (namespace) == SCOPE_REF)
4955 namespace = TREE_OPERAND (namespace, 1);
4956 if (TREE_CODE (namespace) == IDENTIFIER_NODE)
4957 {
4958 /* Lookup in lexer did not find a namespace. */
4959 cp_error ("namespace `%T' undeclared", namespace);
4960 return;
4961 }
4962 if (TREE_CODE (namespace) != NAMESPACE_DECL)
4963 {
4964 cp_error ("`%T' is not a namespace", namespace);
4965 return;
4966 }
4967 namespace = ORIGINAL_NAMESPACE (namespace);
4968 if (!toplevel_bindings_p ())
4969 push_using_directive (namespace);
4970 else
4971 /* direct usage */
4972 add_using_namespace (current_namespace, namespace, 0);
4973 }
4974
4975 void
4976 check_default_args (x)
4977 tree x;
4978 {
4979 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x));
4980 int saw_def = 0, i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE);
4981 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i)
4982 {
4983 if (TREE_PURPOSE (arg))
4984 saw_def = 1;
4985 else if (saw_def)
4986 {
4987 cp_error_at ("default argument missing for parameter %P of `%+#D'",
4988 i, x);
4989 break;
4990 }
4991 }
4992 }
4993
4994 void
4995 mark_used (decl)
4996 tree decl;
4997 {
4998 TREE_USED (decl) = 1;
4999 if (processing_template_decl)
5000 return;
5001 assemble_external (decl);
5002
5003 /* Is it a synthesized method that needs to be synthesized? */
5004 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_CLASS_CONTEXT (decl)
5005 && DECL_ARTIFICIAL (decl) && ! DECL_INITIAL (decl)
5006 /* Kludge: don't synthesize for default args. */
5007 && current_function_decl)
5008 synthesize_method (decl);
5009
5010 /* If this is a function or variable that is an instance of some
5011 template, we now know that we will need to actually do the
5012 instantiation. A TEMPLATE_DECL may also have DECL_TEMPLATE_INFO,
5013 if it's a partial instantiation, but there's no need to
5014 instantiate such a thing. We check that DECL is not an explicit
5015 instantiation because that is not checked in instantiate_decl. */
5016 if (TREE_CODE (decl) != TEMPLATE_DECL
5017 && DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
5018 && !DECL_EXPLICIT_INSTANTIATION (decl))
5019 instantiate_decl (decl);
5020 }
5021
5022 /* Helper function for named_class_head_sans_basetype nonterminal. */
5023
5024 tree
5025 handle_class_head (aggr, scope, id)
5026 tree aggr, scope, id;
5027 {
5028 if (TREE_CODE (id) == TYPE_DECL)
5029 return id;
5030 if (DECL_CLASS_TEMPLATE_P (id))
5031 return DECL_TEMPLATE_RESULT (id);
5032
5033 if (scope)
5034 cp_error ("`%T' does not have a nested type named `%D'", scope, id);
5035 else
5036 cp_error ("no file-scope type named `%D'", id);
5037
5038 id = xref_tag
5039 (aggr, make_anon_name (), 1);
5040 return TYPE_MAIN_DECL (id);
5041 }