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