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