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