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