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