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