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