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