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