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