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