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