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