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