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