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