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