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