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