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