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