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