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