Update to gcc-3.4.6
[dragonfly.git] / contrib / gcc-3.4 / gcc / cp / decl.c
CommitLineData
003757ed
MD
1/* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
1c1138ce 3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
003757ed
MD
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GCC.
7
8GCC 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
13GCC 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 GCC; see the file COPYING. If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
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"
32#include "system.h"
33#include "coretypes.h"
34#include "tm.h"
35#include "tree.h"
36#include "rtl.h"
37#include "expr.h"
38#include "flags.h"
39#include "cp-tree.h"
40#include "tree-inline.h"
41#include "decl.h"
42#include "lex.h"
43#include "output.h"
44#include "except.h"
45#include "toplev.h"
46#include "hashtab.h"
47#include "tm_p.h"
48#include "target.h"
49#include "c-common.h"
50#include "c-pragma.h"
51#include "diagnostic.h"
52#include "debug.h"
53#include "timevar.h"
54
55static tree grokparms (tree, tree *);
56static const char *redeclaration_error_message (tree, tree);
57
58static int decl_jump_unsafe (tree);
59static void require_complete_types_for_parms (tree);
60static int ambi_op_p (enum tree_code);
61static int unary_op_p (enum tree_code);
62static void push_local_name (tree);
63static tree grok_reference_init (tree, tree, tree, tree *);
64static tree grokfndecl (tree, tree, tree, tree, tree, int,
65 enum overload_flags, tree,
66 tree, int, int, int, int, int, int, tree);
67static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
68static void record_unknown_type (tree, const char *);
69static tree builtin_function_1 (const char *, tree, tree, int,
70 enum built_in_class, const char *,
71 tree);
72static tree build_library_fn_1 (tree, enum tree_code, tree);
73static int member_function_or_else (tree, tree, enum overload_flags);
74static void bad_specifiers (tree, const char *, int, int, int, int,
75 int);
76static void check_for_uninitialized_const_var (tree);
77static hashval_t typename_hash (const void *);
78static int typename_compare (const void *, const void *);
79static tree local_variable_p_walkfn (tree *, int *, void *);
80static tree record_builtin_java_type (const char *, int);
81static const char *tag_name (enum tag_types code);
82static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
83static int walk_globals_r (tree, void*);
84static int walk_vtables_r (tree, void*);
85static tree make_label_decl (tree, int);
86static void use_label (tree);
87static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
88 const location_t *);
89static void check_previous_goto (struct named_label_use_list *);
90static void check_switch_goto (struct cp_binding_level *);
91static void check_previous_gotos (tree);
92static void pop_label (tree, tree);
93static void pop_labels (tree);
94static void maybe_deduce_size_from_array_init (tree, tree);
95static void layout_var_decl (tree);
96static void maybe_commonize_var (tree);
97static tree check_initializer (tree, tree, int, tree *);
98static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
99static void save_function_data (tree);
100static void check_function_type (tree, tree);
101static void begin_constructor_body (void);
102static void finish_constructor_body (void);
103static void begin_destructor_body (void);
104static void finish_destructor_body (void);
105static tree create_array_type_for_decl (tree, tree, tree);
106static tree get_atexit_node (void);
107static tree get_dso_handle_node (void);
108static tree start_cleanup_fn (void);
109static void end_cleanup_fn (void);
110static tree cp_make_fname_decl (tree, int);
111static void initialize_predefined_identifiers (void);
112static tree check_special_function_return_type
113 (special_function_kind, tree, tree);
114static tree push_cp_library_fn (enum tree_code, tree);
115static tree build_cp_library_fn (tree, enum tree_code, tree);
116static void store_parm_decls (tree);
117static int cp_missing_noreturn_ok_p (tree);
118static void initialize_local_var (tree, tree);
119static void expand_static_init (tree, tree);
120static tree next_initializable_field (tree);
121static tree reshape_init (tree, tree *);
3a327f56 122static bool reshape_init_array (tree, tree, tree *, tree);
003757ed
MD
123static tree build_typename_type (tree, tree, tree);
124
125/* Erroneous argument lists can use this *IFF* they do not modify it. */
126tree error_mark_list;
127
128/* The following symbols are subsumed in the cp_global_trees array, and
129 listed here individually for documentation purposes.
130
131 C++ extensions
132 tree wchar_decl_node;
133
134 tree vtable_entry_type;
135 tree delta_type_node;
136 tree __t_desc_type_node;
137 tree ti_desc_type_node;
138 tree bltn_desc_type_node, ptr_desc_type_node;
139 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
140 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
141 tree ptm_desc_type_node;
142 tree base_desc_type_node;
143
144 tree class_type_node;
145 tree unknown_type_node;
146
147 Array type `vtable_entry_type[]'
148
149 tree vtbl_type_node;
150 tree vtbl_ptr_type_node;
151
152 Namespaces,
153
154 tree std_node;
155 tree abi_node;
156
157 A FUNCTION_DECL which can call `abort'. Not necessarily the
158 one that the user will declare, but sufficient to be called
159 by routines that want to abort the program.
160
161 tree abort_fndecl;
162
163 The FUNCTION_DECL for the default `::operator delete'.
164
165 tree global_delete_fndecl;
166
167 Used by RTTI
168 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
169 tree tinfo_var_id;
170
171*/
172
173tree cp_global_trees[CPTI_MAX];
174
175/* Indicates that there is a type value in some namespace, although
176 that is not necessarily in scope at the moment. */
177
178tree global_type_node;
179
180/* The node that holds the "name" of the global scope. */
181tree global_scope_name;
182
183/* Used only for jumps to as-yet undefined labels, since jumps to
184 defined labels can have their validity checked immediately. */
185
186struct named_label_use_list GTY(())
187{
188 struct cp_binding_level *binding_level;
189 tree names_in_scope;
190 tree label_decl;
191 location_t o_goto_locus;
192 struct named_label_use_list *next;
193};
194
195#define named_label_uses cp_function_chain->x_named_label_uses
196
197#define local_names cp_function_chain->x_local_names
198
199/* A list of objects which have constructors or destructors
200 which reside in the global scope. The decl is stored in
201 the TREE_VALUE slot and the initializer is stored
202 in the TREE_PURPOSE slot. */
203tree static_aggregates;
204
205/* -- end of C++ */
206
207/* A node for the integer constants 2, and 3. */
208
209tree integer_two_node, integer_three_node;
210
211/* A list of all LABEL_DECLs in the function that have names. Here so
212 we can clear out their names' definitions at the end of the
213 function, and so we can check the validity of jumps to these labels. */
214
215struct named_label_list GTY(())
216{
217 struct cp_binding_level *binding_level;
218 tree names_in_scope;
219 tree old_value;
220 tree label_decl;
221 tree bad_decls;
222 struct named_label_list *next;
223 unsigned int in_try_scope : 1;
224 unsigned int in_catch_scope : 1;
225};
226
227#define named_labels cp_function_chain->x_named_labels
228\f
229/* The number of function bodies which we are currently processing.
230 (Zero if we are at namespace scope, one inside the body of a
231 function, two inside the body of a function in a local class, etc.) */
232int function_depth;
233
234/* States indicating how grokdeclarator() should handle declspecs marked
235 with __attribute__((deprecated)). An object declared as
236 __attribute__((deprecated)) suppresses warnings of uses of other
237 deprecated items. */
238
239enum deprecated_states {
240 DEPRECATED_NORMAL,
241 DEPRECATED_SUPPRESS
242};
243
244static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
245
246/* Set by add_implicitly_declared_members() to keep those members from
247 being flagged as deprecated or reported as using deprecated
248 types. */
249int adding_implicit_members = 0;
250
251/* True if a declaration with an `extern' linkage specifier is being
252 processed. */
253bool have_extern_spec;
254
255\f
256/* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
257 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
258 time the VAR_DECL was declared, the type was incomplete. */
259
260static GTY(()) tree incomplete_vars;
261\f
262/* Returns the kind of template specialization we are currently
263 processing, given that it's declaration contained N_CLASS_SCOPES
264 explicit scope qualifications. */
265
266tmpl_spec_kind
267current_tmpl_spec_kind (int n_class_scopes)
268{
269 int n_template_parm_scopes = 0;
270 int seen_specialization_p = 0;
271 int innermost_specialization_p = 0;
272 struct cp_binding_level *b;
273
274 /* Scan through the template parameter scopes. */
275 for (b = current_binding_level;
276 b->kind == sk_template_parms;
277 b = b->level_chain)
278 {
279 /* If we see a specialization scope inside a parameter scope,
280 then something is wrong. That corresponds to a declaration
281 like:
282
283 template <class T> template <> ...
284
285 which is always invalid since [temp.expl.spec] forbids the
286 specialization of a class member template if the enclosing
287 class templates are not explicitly specialized as well. */
288 if (b->explicit_spec_p)
289 {
290 if (n_template_parm_scopes == 0)
291 innermost_specialization_p = 1;
292 else
293 seen_specialization_p = 1;
294 }
295 else if (seen_specialization_p == 1)
296 return tsk_invalid_member_spec;
297
298 ++n_template_parm_scopes;
299 }
300
301 /* Handle explicit instantiations. */
302 if (processing_explicit_instantiation)
303 {
304 if (n_template_parm_scopes != 0)
305 /* We've seen a template parameter list during an explicit
306 instantiation. For example:
307
308 template <class T> template void f(int);
309
310 This is erroneous. */
311 return tsk_invalid_expl_inst;
312 else
313 return tsk_expl_inst;
314 }
315
316 if (n_template_parm_scopes < n_class_scopes)
317 /* We've not seen enough template headers to match all the
318 specialized classes present. For example:
319
320 template <class T> void R<T>::S<T>::f(int);
321
322 This is invalid; there needs to be one set of template
323 parameters for each class. */
324 return tsk_insufficient_parms;
325 else if (n_template_parm_scopes == n_class_scopes)
326 /* We're processing a non-template declaration (even though it may
327 be a member of a template class.) For example:
328
329 template <class T> void S<T>::f(int);
330
331 The `class T' maches the `S<T>', leaving no template headers
332 corresponding to the `f'. */
333 return tsk_none;
334 else if (n_template_parm_scopes > n_class_scopes + 1)
335 /* We've got too many template headers. For example:
336
337 template <> template <class T> void f (T);
338
339 There need to be more enclosing classes. */
340 return tsk_excessive_parms;
341 else
342 /* This must be a template. It's of the form:
343
344 template <class T> template <class U> void S<T>::f(U);
345
346 This is a specialization if the innermost level was a
347 specialization; otherwise it's just a definition of the
348 template. */
349 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
350}
351
352/* Exit the current scope. */
353
354void
355finish_scope (void)
356{
357 poplevel (0, 0, 0);
358}
359
360/* When a label goes out of scope, check to see if that label was used
361 in a valid manner, and issue any appropriate warnings or errors. */
362
363static void
364pop_label (tree label, tree old_value)
365{
366 if (!processing_template_decl)
367 {
368 if (DECL_INITIAL (label) == NULL_TREE)
369 {
370 location_t location;
371
372 cp_error_at ("label `%D' used but not defined", label);
373 location.file = input_filename;
374 location.line = 0;
375 /* Avoid crashing later. */
376 define_label (location, DECL_NAME (label));
377 }
378 else if (warn_unused_label && !TREE_USED (label))
379 cp_warning_at ("label `%D' defined but not used", label);
380 }
381
382 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
383}
384
385/* At the end of a function, all labels declared within the function
386 go out of scope. BLOCK is the top-level block for the
387 function. */
388
389static void
390pop_labels (tree block)
391{
392 struct named_label_list *link;
393
394 /* Clear out the definitions of all label names, since their scopes
395 end here. */
396 for (link = named_labels; link; link = link->next)
397 {
398 pop_label (link->label_decl, link->old_value);
399 /* Put the labels into the "variables" of the top-level block,
400 so debugger can see them. */
401 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
402 BLOCK_VARS (block) = link->label_decl;
403 }
404
405 named_labels = NULL;
406}
407
408/* Exit a binding level.
409 Pop the level off, and restore the state of the identifier-decl mappings
410 that were in effect when this level was entered.
411
412 If KEEP == 1, this level had explicit declarations, so
413 and create a "block" (a BLOCK node) for the level
414 to record its declarations and subblocks for symbol table output.
415
416 If FUNCTIONBODY is nonzero, this level is the body of a function,
417 so create a block as if KEEP were set and also clear out all
418 label names.
419
420 If REVERSE is nonzero, reverse the order of decls before putting
421 them into the BLOCK. */
422
423tree
424poplevel (int keep, int reverse, int functionbody)
425{
426 tree link;
427 /* The chain of decls was accumulated in reverse order.
428 Put it into forward order, just for cleanliness. */
429 tree decls;
430 int tmp = functionbody;
431 int real_functionbody;
432 tree subblocks;
433 tree block = NULL_TREE;
434 tree decl;
435 int leaving_for_scope;
436 scope_kind kind;
437
438 timevar_push (TV_NAME_LOOKUP);
439
440 my_friendly_assert (current_binding_level->kind != sk_class, 19990916);
441
442 real_functionbody = (current_binding_level->kind == sk_cleanup
443 ? ((functionbody = 0), tmp) : functionbody);
444 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
445
446 my_friendly_assert (!current_binding_level->class_shadowed,
447 19990414);
448
449 /* We used to use KEEP == 2 to indicate that the new block should go
450 at the beginning of the list of blocks at this binding level,
451 rather than the end. This hack is no longer used. */
452 my_friendly_assert (keep == 0 || keep == 1, 0);
453
454 if (current_binding_level->keep)
455 keep = 1;
456
457 /* Any uses of undefined labels, and any defined labels, now operate
458 under constraints of next binding contour. */
459 if (cfun && !functionbody)
460 {
461 struct cp_binding_level *level_chain;
462 level_chain = current_binding_level->level_chain;
463 if (level_chain)
464 {
465 struct named_label_use_list *uses;
466 struct named_label_list *labels;
467 for (labels = named_labels; labels; labels = labels->next)
468 if (labels->binding_level == current_binding_level)
469 {
470 tree decl;
471 if (current_binding_level->kind == sk_try)
472 labels->in_try_scope = 1;
473 if (current_binding_level->kind == sk_catch)
474 labels->in_catch_scope = 1;
475 for (decl = labels->names_in_scope; decl;
476 decl = TREE_CHAIN (decl))
477 if (decl_jump_unsafe (decl))
478 labels->bad_decls = tree_cons (NULL_TREE, decl,
479 labels->bad_decls);
480 labels->binding_level = level_chain;
481 labels->names_in_scope = level_chain->names;
482 }
483
484 for (uses = named_label_uses; uses; uses = uses->next)
485 if (uses->binding_level == current_binding_level)
486 {
487 uses->binding_level = level_chain;
488 uses->names_in_scope = level_chain->names;
489 }
490 }
491 }
492
493 /* Get the decls in the order they were written.
494 Usually current_binding_level->names is in reverse order.
495 But parameter decls were previously put in forward order. */
496
497 if (reverse)
498 current_binding_level->names
499 = decls = nreverse (current_binding_level->names);
500 else
501 decls = current_binding_level->names;
502
503 /* Output any nested inline functions within this block
504 if they weren't already output. */
505 for (decl = decls; decl; decl = TREE_CHAIN (decl))
506 if (TREE_CODE (decl) == FUNCTION_DECL
507 && ! TREE_ASM_WRITTEN (decl)
508 && DECL_INITIAL (decl) != NULL_TREE
509 && TREE_ADDRESSABLE (decl)
510 && decl_function_context (decl) == current_function_decl)
511 {
512 /* If this decl was copied from a file-scope decl
513 on account of a block-scope extern decl,
514 propagate TREE_ADDRESSABLE to the file-scope decl. */
515 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
516 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
517 else
518 {
519 push_function_context ();
520 output_inline_function (decl);
521 pop_function_context ();
522 }
523 }
524
525 /* When not in function-at-a-time mode, expand_end_bindings will
526 warn about unused variables. But, in function-at-a-time mode
527 expand_end_bindings is not passed the list of variables in the
528 current scope, and therefore no warning is emitted. So, we
529 explicitly warn here. */
530 if (!processing_template_decl)
531 warn_about_unused_variables (getdecls ());
532
533 /* If there were any declarations or structure tags in that level,
534 or if this level is a function body,
535 create a BLOCK to record them for the life of this function. */
536 block = NULL_TREE;
537 if (keep == 1 || functionbody)
538 block = make_node (BLOCK);
539 if (block != NULL_TREE)
540 {
541 BLOCK_VARS (block) = decls;
542 BLOCK_SUBBLOCKS (block) = subblocks;
543 }
544
545 /* In each subblock, record that this is its superior. */
546 if (keep >= 0)
547 for (link = subblocks; link; link = TREE_CHAIN (link))
548 BLOCK_SUPERCONTEXT (link) = block;
549
550 /* We still support the old for-scope rules, whereby the variables
551 in a for-init statement were in scope after the for-statement
552 ended. We only use the new rules if flag_new_for_scope is
553 nonzero. */
554 leaving_for_scope
555 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
556
557 /* Remove declarations for all the DECLs in this level. */
558 for (link = decls; link; link = TREE_CHAIN (link))
559 {
560 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
561 && DECL_NAME (link))
562 {
563 cxx_binding *outer_binding
564 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
565 tree ns_binding;
566
567 if (!outer_binding)
568 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
569 else
570 ns_binding = NULL_TREE;
571
572 if (outer_binding
573 && outer_binding->scope == current_binding_level->level_chain)
574 /* We have something like:
575
576 int i;
577 for (int i; ;);
578
579 and we are leaving the `for' scope. There's no reason to
580 keep the binding of the inner `i' in this case. */
581 pop_binding (DECL_NAME (link), link);
582 else if ((outer_binding
583 && (TREE_CODE (outer_binding->value) == TYPE_DECL))
584 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
585 /* Here, we have something like:
586
587 typedef int I;
588
589 void f () {
590 for (int I; ;);
591 }
592
593 We must pop the for-scope binding so we know what's a
594 type and what isn't. */
595 pop_binding (DECL_NAME (link), link);
596 else
597 {
598 /* Mark this VAR_DECL as dead so that we can tell we left it
599 there only for backward compatibility. */
600 DECL_DEAD_FOR_LOCAL (link) = 1;
601
602 /* Keep track of what should have happened when we
603 popped the binding. */
604 if (outer_binding && outer_binding->value)
605 DECL_SHADOWED_FOR_VAR (link) = outer_binding->value;
606
607 /* Add it to the list of dead variables in the next
608 outermost binding to that we can remove these when we
609 leave that binding. */
610 current_binding_level->level_chain->dead_vars_from_for
611 = tree_cons (NULL_TREE, link,
612 current_binding_level->level_chain->
613 dead_vars_from_for);
614
615 /* Although we don't pop the cxx_binding, we do clear
616 its SCOPE since the scope is going away now. */
617 IDENTIFIER_BINDING (DECL_NAME (link))->scope = NULL;
618 }
619 }
620 else
621 {
622 /* Remove the binding. */
623 decl = link;
624 if (TREE_CODE (decl) == TREE_LIST)
625 decl = TREE_VALUE (decl);
626 if (DECL_P (decl))
627 pop_binding (DECL_NAME (decl), decl);
628 else if (TREE_CODE (decl) == OVERLOAD)
629 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
630 else
631 abort ();
632 }
633 }
634
635 /* Remove declarations for any `for' variables from inner scopes
636 that we kept around. */
637 for (link = current_binding_level->dead_vars_from_for;
638 link; link = TREE_CHAIN (link))
639 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
640
641 /* Restore the IDENTIFIER_TYPE_VALUEs. */
642 for (link = current_binding_level->type_shadowed;
643 link; link = TREE_CHAIN (link))
644 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
645
646 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
647 for (link = current_binding_level->shadowed_labels;
648 link;
649 link = TREE_CHAIN (link))
650 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
651
652 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
653 list if a `using' declaration put them there. The debugging
654 back-ends won't understand OVERLOAD, so we remove them here.
655 Because the BLOCK_VARS are (temporarily) shared with
656 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
657 popped all the bindings. */
658 if (block)
659 {
660 tree* d;
661
662 for (d = &BLOCK_VARS (block); *d; )
663 {
664 if (TREE_CODE (*d) == TREE_LIST)
665 *d = TREE_CHAIN (*d);
666 else
667 d = &TREE_CHAIN (*d);
668 }
669 }
670
671 /* If the level being exited is the top level of a function,
672 check over all the labels. */
673 if (functionbody)
674 {
675 /* Since this is the top level block of a function, the vars are
676 the function's parameters. Don't leave them in the BLOCK
677 because they are found in the FUNCTION_DECL instead. */
678 BLOCK_VARS (block) = 0;
679 pop_labels (block);
680 }
681
682 kind = current_binding_level->kind;
683
684 leave_scope ();
685 if (functionbody)
686 DECL_INITIAL (current_function_decl) = block;
687 else if (block)
688 current_binding_level->blocks
689 = chainon (current_binding_level->blocks, block);
690
691 /* If we did not make a block for the level just exited,
692 any blocks made for inner levels
693 (since they cannot be recorded as subblocks in that level)
694 must be carried forward so they will later become subblocks
695 of something else. */
696 else if (subblocks)
697 current_binding_level->blocks
698 = chainon (current_binding_level->blocks, subblocks);
699
700 /* Each and every BLOCK node created here in `poplevel' is important
701 (e.g. for proper debugging information) so if we created one
702 earlier, mark it as "used". */
703 if (block)
704 TREE_USED (block) = 1;
705
706 /* Take care of compiler's internal binding structures. */
707 if (kind == sk_cleanup)
708 {
709 tree scope_stmts;
710
711 scope_stmts
712 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
713 if (block)
714 {
715 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
716 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
717 }
718
719 block = poplevel (keep, reverse, functionbody);
720 }
721
722 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
723}
724
725/* Delete the node BLOCK from the current binding level.
726 This is used for the block inside a stmt expr ({...})
727 so that the block can be reinserted where appropriate. */
728
729void
730delete_block (tree block)
731{
732 tree t;
733 if (current_binding_level->blocks == block)
734 current_binding_level->blocks = TREE_CHAIN (block);
735 for (t = current_binding_level->blocks; t;)
736 {
737 if (TREE_CHAIN (t) == block)
738 TREE_CHAIN (t) = TREE_CHAIN (block);
739 else
740 t = TREE_CHAIN (t);
741 }
742 TREE_CHAIN (block) = NULL_TREE;
743 /* Clear TREE_USED which is always set by poplevel.
744 The flag is set again if insert_block is called. */
745 TREE_USED (block) = 0;
746}
747
748/* Insert BLOCK at the end of the list of subblocks of the
749 current binding level. This is used when a BIND_EXPR is expanded,
750 to handle the BLOCK node inside the BIND_EXPR. */
751
752void
753insert_block (tree block)
754{
755 TREE_USED (block) = 1;
756 current_binding_level->blocks
757 = chainon (current_binding_level->blocks, block);
758}
759
760/* Set the BLOCK node for the innermost scope
761 (the one we are currently in). */
762
763void
764set_block (tree block ATTRIBUTE_UNUSED )
765{
766 /* The RTL expansion machinery requires us to provide this callback,
767 but it is not applicable in function-at-a-time mode. */
768}
769
770/* Returns nonzero if T is a virtual function table. */
771
772int
773vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
774{
775 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
776}
777
778/* Returns nonzero if T is a TYPE_DECL for a type with virtual
779 functions. */
780
781int
782vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
783{
784 return (TREE_CODE (t) == TYPE_DECL
785 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
786 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
787}
788
789struct walk_globals_data {
790 walk_globals_pred p;
791 walk_globals_fn f;
792 void *data;
793};
794
795/* Walk the vtable declarations in NAMESPACE. Whenever one is found
796 for which P returns nonzero, call F with its address. If any call
797 to F returns a nonzero value, return a nonzero value. */
798
799static int
800walk_vtables_r (tree namespace, void* data)
801{
802 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
803 walk_globals_fn f = wgd->f;
804 void *d = wgd->data;
805 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
806 int result = 0;
807
808 for (; decl ; decl = TREE_CHAIN (decl))
809 result |= (*f) (&decl, d);
810
811 return result;
812}
813
814/* Walk the vtable declarations. Whenever one is found for which P
815 returns nonzero, call F with its address. If any call to F
816 returns a nonzero value, return a nonzero value. */
817bool
818walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
819{
820 struct walk_globals_data wgd;
821 wgd.p = p;
822 wgd.f = f;
823 wgd.data = data;
824
825 return walk_namespaces (walk_vtables_r, &wgd);
826}
827
828/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
829 itself, calling F for each. The DATA is passed to F as well. */
830
831static int
832walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
833{
834 int result = 0;
835 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
836
837 result |= (*f) (namespace, data);
838
839 for (; current; current = TREE_CHAIN (current))
840 result |= walk_namespaces_r (current, f, data);
841
842 return result;
843}
844
845/* Walk all the namespaces, calling F for each. The DATA is passed to
846 F as well. */
847
848int
849walk_namespaces (walk_namespaces_fn f, void* data)
850{
851 return walk_namespaces_r (global_namespace, f, data);
852}
853
854/* Walk the global declarations in NAMESPACE. Whenever one is found
855 for which P returns nonzero, call F with its address. If any call
856 to F returns a nonzero value, return a nonzero value. */
857
858static int
859walk_globals_r (tree namespace, void* data)
860{
861 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
862 walk_globals_pred p = wgd->p;
863 walk_globals_fn f = wgd->f;
864 void *d = wgd->data;
865 tree *t;
866 int result = 0;
867
868 t = &NAMESPACE_LEVEL (namespace)->names;
869
870 while (*t)
871 {
872 tree glbl = *t;
873
874 if ((*p) (glbl, d))
875 result |= (*f) (t, d);
876
877 /* If F changed *T, then *T still points at the next item to
878 examine. */
879 if (*t == glbl)
880 t = &TREE_CHAIN (*t);
881 }
882
883 return result;
884}
885
886/* Walk the global declarations. Whenever one is found for which P
887 returns true, call F with its address. If any call to F
888 returns true, return true. */
889
890bool
891walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
892{
893 struct walk_globals_data wgd;
894 wgd.p = p;
895 wgd.f = f;
896 wgd.data = data;
897
898 return walk_namespaces (walk_globals_r, &wgd);
899}
900
901/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
902 DATA is non-NULL, this is the last time we will call
903 wrapup_global_declarations for this NAMESPACE. */
904
905int
906wrapup_globals_for_namespace (tree namespace, void* data)
907{
908 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
909 varray_type statics = level->static_decls;
910 tree *vec = &VARRAY_TREE (statics, 0);
911 int len = VARRAY_ACTIVE_SIZE (statics);
912 int last_time = (data != 0);
913
914 if (last_time)
915 {
916 check_global_declarations (vec, len);
917 return 0;
918 }
919
920 /* Write out any globals that need to be output. */
921 return wrapup_global_declarations (vec, len);
922}
923
924\f
925/* In C++, you don't have to write `struct S' to refer to `S'; you
926 can just use `S'. We accomplish this by creating a TYPE_DECL as
927 if the user had written `typedef struct S S'. Create and return
928 the TYPE_DECL for TYPE. */
929
930tree
931create_implicit_typedef (tree name, tree type)
932{
933 tree decl;
934
935 decl = build_decl (TYPE_DECL, name, type);
936 DECL_ARTIFICIAL (decl) = 1;
937 /* There are other implicit type declarations, like the one *within*
938 a class that allows you to write `S::S'. We must distinguish
939 amongst these. */
940 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
941 TYPE_NAME (type) = decl;
942
943 return decl;
944}
945
946/* Remember a local name for name-mangling purposes. */
947
948static void
949push_local_name (tree decl)
950{
951 size_t i, nelts;
952 tree t, name;
953
954 timevar_push (TV_NAME_LOOKUP);
955 if (!local_names)
956 VARRAY_TREE_INIT (local_names, 8, "local_names");
957
958 name = DECL_NAME (decl);
959
960 nelts = VARRAY_ACTIVE_SIZE (local_names);
961 for (i = 0; i < nelts; i++)
962 {
963 t = VARRAY_TREE (local_names, i);
964 if (DECL_NAME (t) == name)
965 {
966 if (!DECL_LANG_SPECIFIC (decl))
967 retrofit_lang_decl (decl);
968 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
969 if (DECL_LANG_SPECIFIC (t))
970 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
971 else
972 DECL_DISCRIMINATOR (decl) = 1;
973
974 VARRAY_TREE (local_names, i) = decl;
975 timevar_pop (TV_NAME_LOOKUP);
976 return;
977 }
978 }
979
980 VARRAY_PUSH_TREE (local_names, decl);
981 timevar_pop (TV_NAME_LOOKUP);
982}
983\f
984/* Subroutine of duplicate_decls: return truthvalue of whether
985 or not types of these decls match.
986
987 For C++, we must compare the parameter list so that `int' can match
988 `int&' in a parameter position, but `int&' is not confused with
989 `const int&'. */
990
991int
992decls_match (tree newdecl, tree olddecl)
993{
994 int types_match;
995
996 if (newdecl == olddecl)
997 return 1;
998
999 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1000 /* If the two DECLs are not even the same kind of thing, we're not
1001 interested in their types. */
1002 return 0;
1003
1004 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1005 {
1006 tree f1 = TREE_TYPE (newdecl);
1007 tree f2 = TREE_TYPE (olddecl);
1008 tree p1 = TYPE_ARG_TYPES (f1);
1009 tree p2 = TYPE_ARG_TYPES (f2);
1010
1011 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1012 && ! (DECL_EXTERN_C_P (newdecl)
1013 && DECL_EXTERN_C_P (olddecl)))
1014 return 0;
1015
1016 if (TREE_CODE (f1) != TREE_CODE (f2))
1017 return 0;
1018
1019 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
1020 {
1021 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
1022 && (DECL_BUILT_IN (olddecl)
1023#ifndef NO_IMPLICIT_EXTERN_C
1024 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1025 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1026#endif
1027 ))
1028 {
1029 types_match = self_promoting_args_p (p1);
1030 if (p1 == void_list_node)
1031 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1032 }
1033#ifndef NO_IMPLICIT_EXTERN_C
1034 else if (p1 == NULL_TREE
1035 && (DECL_EXTERN_C_P (olddecl)
1036 && DECL_IN_SYSTEM_HEADER (olddecl)
1037 && !DECL_CLASS_SCOPE_P (olddecl))
1038 && (DECL_EXTERN_C_P (newdecl)
1039 && DECL_IN_SYSTEM_HEADER (newdecl)
1040 && !DECL_CLASS_SCOPE_P (newdecl)))
1041 {
1042 types_match = self_promoting_args_p (p2);
1043 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1044 }
1045#endif
1046 else
1047 types_match = compparms (p1, p2);
1048 }
1049 else
1050 types_match = 0;
1051 }
1052 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1053 {
1054 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
1055 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
1056 return 0;
1057
1058 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1059 DECL_TEMPLATE_PARMS (olddecl)))
1060 return 0;
1061
1062 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1063 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1064 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1065 else
1066 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1067 DECL_TEMPLATE_RESULT (newdecl));
1068 }
1069 else
1070 {
1c1138ce
JS
1071 /* Need to check scope for variable declaration (VAR_DECL).
1072 For typedef (TYPE_DECL), scope is ignored. */
1073 if (TREE_CODE (newdecl) == VAR_DECL
1074 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1075 return 0;
1076
003757ed
MD
1077 if (TREE_TYPE (newdecl) == error_mark_node)
1078 types_match = TREE_TYPE (olddecl) == error_mark_node;
1079 else if (TREE_TYPE (olddecl) == NULL_TREE)
1080 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1081 else if (TREE_TYPE (newdecl) == NULL_TREE)
1082 types_match = 0;
1083 else
1084 types_match = comptypes (TREE_TYPE (newdecl),
1085 TREE_TYPE (olddecl),
1086 COMPARE_REDECLARATION);
1087 }
1088
1089 return types_match;
1090}
1091
1092/* If NEWDECL is `static' and an `extern' was seen previously,
1093 warn about it. OLDDECL is the previous declaration.
1094
1095 Note that this does not apply to the C++ case of declaring
1096 a variable `extern const' and then later `const'.
1097
1098 Don't complain about built-in functions, since they are beyond
1099 the user's control. */
1100
1101void
1102warn_extern_redeclared_static (tree newdecl, tree olddecl)
1103{
1104 static const char *const explicit_extern_static_warning
1105 = "`%D' was declared `extern' and later `static'";
1106 static const char *const implicit_extern_static_warning
1107 = "`%D' was declared implicitly `extern' and later `static'";
1108
1109 tree name;
1110
1111 if (TREE_CODE (newdecl) == TYPE_DECL
1112 || TREE_CODE (newdecl) == TEMPLATE_DECL
1113 || TREE_CODE (newdecl) == CONST_DECL
1114 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1115 return;
1116
1117 /* Don't get confused by static member functions; that's a different
1118 use of `static'. */
1119 if (TREE_CODE (newdecl) == FUNCTION_DECL
1120 && DECL_STATIC_FUNCTION_P (newdecl))
1121 return;
1122
1123 /* If the old declaration was `static', or the new one isn't, then
1124 then everything is OK. */
1125 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1126 return;
1127
1128 /* It's OK to declare a builtin function as `static'. */
1129 if (TREE_CODE (olddecl) == FUNCTION_DECL
1130 && DECL_ARTIFICIAL (olddecl))
1131 return;
1132
1133 name = DECL_ASSEMBLER_NAME (newdecl);
1134 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
1135 ? implicit_extern_static_warning
1136 : explicit_extern_static_warning, newdecl);
1137 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
1138}
1139
1140/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1141 If the redeclaration is invalid, a diagnostic is issued, and the
1142 error_mark_node is returned. Otherwise, OLDDECL is returned.
1143
1144 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1145 returned. */
1146
1147tree
1148duplicate_decls (tree newdecl, tree olddecl)
1149{
1150 unsigned olddecl_uid = DECL_UID (olddecl);
1151 int olddecl_friend = 0, types_match = 0;
1152 int new_defines_function = 0;
1153
1154 if (newdecl == olddecl)
1155 return olddecl;
1156
1157 types_match = decls_match (newdecl, olddecl);
1158
1159 /* If either the type of the new decl or the type of the old decl is an
1160 error_mark_node, then that implies that we have already issued an
1161 error (earlier) for some bogus type specification, and in that case,
1162 it is rather pointless to harass the user with yet more error message
1163 about the same declaration, so just pretend the types match here. */
1164 if (TREE_TYPE (newdecl) == error_mark_node
1165 || TREE_TYPE (olddecl) == error_mark_node)
1166 types_match = 1;
1167
1168 if (DECL_P (olddecl)
1169 && TREE_CODE (newdecl) == FUNCTION_DECL
1170 && TREE_CODE (olddecl) == FUNCTION_DECL
1171 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1172 {
1173 if (DECL_DECLARED_INLINE_P (newdecl)
1174 && DECL_UNINLINABLE (newdecl)
1175 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1176 /* Already warned elsewhere. */;
1177 else if (DECL_DECLARED_INLINE_P (olddecl)
1178 && DECL_UNINLINABLE (olddecl)
1179 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1180 /* Already warned. */;
1181 else if (DECL_DECLARED_INLINE_P (newdecl)
1182 && DECL_UNINLINABLE (olddecl)
1183 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1184 {
1185 warning ("%Jfunction '%D' redeclared as inline", newdecl, newdecl);
1186 warning ("%Jprevious declaration of '%D' with attribute noinline",
1187 olddecl, olddecl);
1188 }
1189 else if (DECL_DECLARED_INLINE_P (olddecl)
1190 && DECL_UNINLINABLE (newdecl)
1191 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1192 {
1193 warning ("%Jfunction '%D' redeclared with attribute noinline",
1194 newdecl, newdecl);
1195 warning ("%Jprevious declaration of '%D' was inline",
1196 olddecl, olddecl);
1197 }
1198 }
1199
1200 /* Check for redeclaration and other discrepancies. */
1201 if (TREE_CODE (olddecl) == FUNCTION_DECL
1202 && DECL_ARTIFICIAL (olddecl))
1203 {
1204 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1205 {
1206 /* Avoid warnings redeclaring anticipated built-ins. */
1207 if (DECL_ANTICIPATED (olddecl))
1208 return NULL_TREE;
1209
1210 /* If you declare a built-in or predefined function name as static,
1211 the old definition is overridden, but optionally warn this was a
1212 bad choice of name. */
1213 if (! TREE_PUBLIC (newdecl))
1214 {
1215 if (warn_shadow)
1216 warning ("shadowing %s function `%#D'",
1217 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1218 olddecl);
1219 /* Discard the old built-in function. */
1220 return NULL_TREE;
1221 }
1222 /* If the built-in is not ansi, then programs can override
1223 it even globally without an error. */
1224 else if (! DECL_BUILT_IN (olddecl))
1225 warning ("library function `%#D' redeclared as non-function `%#D'",
1226 olddecl, newdecl);
1227 else
1228 {
1229 error ("declaration of `%#D'", newdecl);
1230 error ("conflicts with built-in declaration `%#D'",
1231 olddecl);
1232 }
1233 return NULL_TREE;
1234 }
1235 else if (!types_match)
1236 {
1237 /* Avoid warnings redeclaring anticipated built-ins. */
1238 if (DECL_ANTICIPATED (olddecl))
1239 ; /* Do nothing yet. */
1240 else if ((DECL_EXTERN_C_P (newdecl)
1241 && DECL_EXTERN_C_P (olddecl))
1242 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1243 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1244 {
1245 /* A near match; override the builtin. */
1246
1247 if (TREE_PUBLIC (newdecl))
1248 {
1249 warning ("new declaration `%#D'", newdecl);
1250 warning ("ambiguates built-in declaration `%#D'",
1251 olddecl);
1252 }
1253 else if (warn_shadow)
1254 warning ("shadowing %s function `%#D'",
1255 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1256 olddecl);
1257 }
1258 else
1259 /* Discard the old built-in function. */
1260 return NULL_TREE;
1261
1262 /* Replace the old RTL to avoid problems with inlining. */
1263 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
1264 }
1265 /* Even if the types match, prefer the new declarations type
1266 for anticipated built-ins, for exception lists, etc... */
1267 else if (DECL_ANTICIPATED (olddecl))
1268 {
1269 tree type = TREE_TYPE (newdecl);
1270 tree attribs = (*targetm.merge_type_attributes)
1271 (TREE_TYPE (olddecl), type);
1272
1273 type = cp_build_type_attribute_variant (type, attribs);
1274 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1275 }
1276
1277 /* Whether or not the builtin can throw exceptions has no
1278 bearing on this declarator. */
1279 TREE_NOTHROW (olddecl) = 0;
1280
1281 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1282 {
1283 /* If a builtin function is redeclared as `static', merge
1284 the declarations, but make the original one static. */
1285 DECL_THIS_STATIC (olddecl) = 1;
1286 TREE_PUBLIC (olddecl) = 0;
1287
1288 /* Make the old declaration consistent with the new one so
1289 that all remnants of the builtin-ness of this function
1290 will be banished. */
1291 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1292 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
1293 }
1294 }
1295 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1296 {
1297 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1298 && TREE_CODE (newdecl) != TYPE_DECL
1299 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1300 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1301 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1302 && TREE_CODE (olddecl) != TYPE_DECL
1303 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1304 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1305 == TYPE_DECL))))
1306 {
1307 /* We do nothing special here, because C++ does such nasty
1308 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1309 get shadowed, and know that if we need to find a TYPE_DECL
1310 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1311 slot of the identifier. */
1312 return NULL_TREE;
1313 }
1314
1315 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1316 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1317 || (TREE_CODE (olddecl) == FUNCTION_DECL
1318 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1319 return NULL_TREE;
1320
1321 error ("`%#D' redeclared as different kind of symbol", newdecl);
1322 if (TREE_CODE (olddecl) == TREE_LIST)
1323 olddecl = TREE_VALUE (olddecl);
1324 cp_error_at ("previous declaration of `%#D'", olddecl);
1325
1326 /* New decl is completely inconsistent with the old one =>
1327 tell caller to replace the old one. */
1328
1329 return NULL_TREE;
1330 }
1331 else if (!types_match)
1332 {
1333 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1334 /* These are certainly not duplicate declarations; they're
1335 from different scopes. */
1336 return NULL_TREE;
1337
1338 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1339 {
1340 /* The name of a class template may not be declared to refer to
1341 any other template, class, function, object, namespace, value,
1342 or type in the same scope. */
1343 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1344 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1345 {
1346 error ("declaration of template `%#D'", newdecl);
1347 cp_error_at ("conflicts with previous declaration `%#D'",
1348 olddecl);
1349 }
1350 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1351 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1352 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1353 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1354 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1355 DECL_TEMPLATE_PARMS (olddecl))
1356 /* Template functions can be disambiguated by
1357 return type. */
1358 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1359 TREE_TYPE (TREE_TYPE (olddecl))))
1360 {
1361 error ("new declaration `%#D'", newdecl);
1362 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
1363 }
1364 return NULL_TREE;
1365 }
1366 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1367 {
1368 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1369 {
1370 error ("declaration of C function `%#D' conflicts with",
1371 newdecl);
1372 cp_error_at ("previous declaration `%#D' here", olddecl);
1373 }
1374 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1375 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1376 {
1377 error ("new declaration `%#D'", newdecl);
1378 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
1379 }
1380 else
1381 return NULL_TREE;
1382 }
1378ea41 1383 else
003757ed
MD
1384 {
1385 error ("conflicting declaration '%#D'", newdecl);
1386 cp_error_at ("'%D' has a previous declaration as `%#D'",
1387 olddecl, olddecl);
1388 return NULL_TREE;
1389 }
1390 }
1391 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1392 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1393 && (!DECL_TEMPLATE_INFO (newdecl)
1394 || (DECL_TI_TEMPLATE (newdecl)
1395 != DECL_TI_TEMPLATE (olddecl))))
1396 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1397 && (!DECL_TEMPLATE_INFO (olddecl)
1398 || (DECL_TI_TEMPLATE (olddecl)
1399 != DECL_TI_TEMPLATE (newdecl))))))
1400 /* It's OK to have a template specialization and a non-template
1401 with the same type, or to have specializations of two
1402 different templates with the same type. Note that if one is a
1403 specialization, and the other is an instantiation of the same
1404 template, that we do not exit at this point. That situation
1405 can occur if we instantiate a template class, and then
1406 specialize one of its methods. This situation is valid, but
1407 the declarations must be merged in the usual way. */
1408 return NULL_TREE;
1409 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1410 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1411 && !DECL_USE_TEMPLATE (newdecl))
1412 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1413 && !DECL_USE_TEMPLATE (olddecl))))
1414 /* One of the declarations is a template instantiation, and the
1415 other is not a template at all. That's OK. */
1416 return NULL_TREE;
3a327f56
JS
1417 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1418 {
1419 /* In [namespace.alias] we have:
1420
1421 In a declarative region, a namespace-alias-definition can be
1422 used to redefine a namespace-alias declared in that declarative
1423 region to refer only to the namespace to which it already
1424 refers.
1425
1426 Therefore, if we encounter a second alias directive for the same
1427 alias, we can just ignore the second directive. */
1428 if (DECL_NAMESPACE_ALIAS (newdecl)
1429 && (DECL_NAMESPACE_ALIAS (newdecl)
1430 == DECL_NAMESPACE_ALIAS (olddecl)))
1431 return olddecl;
1432 /* [namespace.alias]
1433
1434 A namespace-name or namespace-alias shall not be declared as
1435 the name of any other entity in the same declarative region.
1436 A namespace-name defined at global scope shall not be
1437 declared as the name of any other entity in any glogal scope
1438 of the program. */
1439 error ("declaration of `namespace %D' conflicts with", newdecl);
1440 cp_error_at ("previous declaration of `namespace %D' here", olddecl);
1441 return error_mark_node;
1442 }
003757ed
MD
1443 else
1444 {
1445 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1446 if (errmsg)
1447 {
1448 error (errmsg, newdecl);
1449 if (DECL_NAME (olddecl) != NULL_TREE)
1450 cp_error_at ((DECL_INITIAL (olddecl)
1451 && namespace_bindings_p ())
1452 ? "`%#D' previously defined here"
1453 : "`%#D' previously declared here", olddecl);
1454 return error_mark_node;
1455 }
1456 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1457 && DECL_INITIAL (olddecl) != NULL_TREE
1458 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1459 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1460 {
1461 /* Prototype decl follows defn w/o prototype. */
1462 cp_warning_at ("prototype for `%#D'", newdecl);
1463 warning ("%Jfollows non-prototype definition here", olddecl);
1464 }
1465 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1466 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1467 {
1468 /* extern "C" int foo ();
1469 int foo () { bar (); }
1470 is OK. */
1471 if (current_lang_depth () == 0)
1472 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1473 else
1474 {
1475 cp_error_at ("previous declaration of `%#D' with %L linkage",
1476 olddecl, DECL_LANGUAGE (olddecl));
1477 error ("conflicts with new declaration with %L linkage",
1478 DECL_LANGUAGE (newdecl));
1479 }
1480 }
1481
1482 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1483 ;
1484 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1485 {
1486 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1487 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1488 int i = 1;
1489
1490 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1491 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1492
1493 for (; t1 && t1 != void_list_node;
1494 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1495 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1496 {
1497 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1498 TREE_PURPOSE (t2)))
1499 {
1500 pedwarn ("default argument given for parameter %d of `%#D'",
1501 i, newdecl);
1502 cp_pedwarn_at ("after previous specification in `%#D'",
1503 olddecl);
1504 }
1505 else
1506 {
1507 error ("default argument given for parameter %d of `%#D'",
1508 i, newdecl);
1509 cp_error_at ("after previous specification in `%#D'",
1510 olddecl);
1511 }
1512 }
1513
1514 if (DECL_DECLARED_INLINE_P (newdecl)
1515 && ! DECL_DECLARED_INLINE_P (olddecl)
1516 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1517 {
1518 warning ("`%#D' was used before it was declared inline", newdecl);
1519 warning ("%Jprevious non-inline declaration here", olddecl);
1520 }
1521 }
1522 }
1523
1524 /* Do not merge an implicit typedef with an explicit one. In:
1525
1526 class A;
1527 ...
1528 typedef class A A __attribute__ ((foo));
1529
1530 the attribute should apply only to the typedef. */
1531 if (TREE_CODE (olddecl) == TYPE_DECL
1532 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1533 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1534 return NULL_TREE;
1535
1536 /* If new decl is `static' and an `extern' was seen previously,
1537 warn about it. */
1538 warn_extern_redeclared_static (newdecl, olddecl);
1539
1540 /* We have committed to returning 1 at this point. */
1541 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1542 {
1543 /* Now that functions must hold information normally held
1544 by field decls, there is extra work to do so that
1545 declaration information does not get destroyed during
1546 definition. */
1547 if (DECL_VINDEX (olddecl))
1548 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1549 if (DECL_CONTEXT (olddecl))
1550 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1551 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1552 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1553 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1554 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1555 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
1556 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1557 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1558 SET_OVERLOADED_OPERATOR_CODE
1559 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1560 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1561
1562 /* Optionally warn about more than one declaration for the same
1563 name, but don't warn about a function declaration followed by a
1564 definition. */
1565 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1566 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1567 /* Don't warn about extern decl followed by definition. */
1568 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1569 /* Don't warn about friends, let add_friend take care of it. */
1570 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
1571 {
1572 warning ("redundant redeclaration of `%D' in same scope", newdecl);
1573 cp_warning_at ("previous declaration of `%D'", olddecl);
1574 }
1575 }
1576
1577 /* Deal with C++: must preserve virtual function table size. */
1578 if (TREE_CODE (olddecl) == TYPE_DECL)
1579 {
1580 tree newtype = TREE_TYPE (newdecl);
1581 tree oldtype = TREE_TYPE (olddecl);
1582
1583 if (newtype != error_mark_node && oldtype != error_mark_node
1584 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1585 CLASSTYPE_FRIEND_CLASSES (newtype)
1586 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1587
1588 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1589 }
1590
1591 /* Copy all the DECL_... slots specified in the new decl
1592 except for any that we copy here from the old type. */
1593 DECL_ATTRIBUTES (newdecl)
1594 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1595
1596 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1597 {
1598 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
1599 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1600 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1601 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1602
1603 /* If the new declaration is a definition, update the file and
1604 line information on the declaration. */
1605 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
1606 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
1607 {
1608 DECL_SOURCE_LOCATION (olddecl)
1609 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
1610 = DECL_SOURCE_LOCATION (newdecl);
1611 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1612 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (olddecl))
1613 = DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (newdecl));
1614 }
1615
1616 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1617 {
1618 DECL_INLINE (DECL_TEMPLATE_RESULT (olddecl))
1619 |= DECL_INLINE (DECL_TEMPLATE_RESULT (newdecl));
1620 DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (olddecl))
1621 |= DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (newdecl));
1622 }
1623
1624 return olddecl;
1625 }
1626
1627 if (types_match)
1628 {
1629 /* Automatically handles default parameters. */
1630 tree oldtype = TREE_TYPE (olddecl);
1631 tree newtype;
1632
1633 /* Merge the data types specified in the two decls. */
1634 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1635
1636 /* If merge_types produces a non-typedef type, just use the old type. */
1637 if (TREE_CODE (newdecl) == TYPE_DECL
1638 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1639 newtype = oldtype;
1640
1641 if (TREE_CODE (newdecl) == VAR_DECL)
1642 {
1643 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1644 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1645 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1646 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1647 }
1648
1649 /* Do this after calling `merge_types' so that default
1650 parameters don't confuse us. */
1651 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1652 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
1653 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
1654 {
1655 TREE_TYPE (newdecl) = build_exception_variant (newtype,
1656 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
1657 TREE_TYPE (olddecl) = build_exception_variant (newtype,
1658 TYPE_RAISES_EXCEPTIONS (oldtype));
1659
1660 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
1661 && DECL_SOURCE_LINE (olddecl) != 0
1662 && flag_exceptions
1663 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
1664 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
1665 {
1666 error ("declaration of `%F' throws different exceptions",
1667 newdecl);
1668 cp_error_at ("than previous declaration `%F'", olddecl);
1669 }
1670 }
1671 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1672
1673 /* Lay the type out, unless already done. */
1674 if (! same_type_p (newtype, oldtype)
1675 && TREE_TYPE (newdecl) != error_mark_node
1676 && !(processing_template_decl && uses_template_parms (newdecl)))
1677 layout_type (TREE_TYPE (newdecl));
1678
1679 if ((TREE_CODE (newdecl) == VAR_DECL
1680 || TREE_CODE (newdecl) == PARM_DECL
1681 || TREE_CODE (newdecl) == RESULT_DECL
1682 || TREE_CODE (newdecl) == FIELD_DECL
1683 || TREE_CODE (newdecl) == TYPE_DECL)
1684 && !(processing_template_decl && uses_template_parms (newdecl)))
1685 layout_decl (newdecl, 0);
1686
1687 /* Merge the type qualifiers. */
1688 if (TREE_READONLY (newdecl))
1689 TREE_READONLY (olddecl) = 1;
1690 if (TREE_THIS_VOLATILE (newdecl))
1691 TREE_THIS_VOLATILE (olddecl) = 1;
1692
1693 /* Merge the initialization information. */
1694 if (DECL_INITIAL (newdecl) == NULL_TREE
1695 && DECL_INITIAL (olddecl) != NULL_TREE)
1696 {
1697 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1698 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1699 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1700 && DECL_LANG_SPECIFIC (newdecl)
1701 && DECL_LANG_SPECIFIC (olddecl))
1702 {
1703 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1704 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1705 }
1706 }
1707
1708 /* Merge the section attribute.
1709 We want to issue an error if the sections conflict but that must be
1710 done later in decl_attributes since we are called before attributes
1711 are assigned. */
1712 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1713 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1714
1715 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1716 {
1717 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1718 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1719 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1720 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1721 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1722 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1723 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1724 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1725 /* Keep the old RTL. */
1726 COPY_DECL_RTL (olddecl, newdecl);
1727 }
1728 else if (TREE_CODE (newdecl) == VAR_DECL
1729 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1730 {
1731 /* Keep the old RTL. We cannot keep the old RTL if the old
1732 declaration was for an incomplete object and the new
1733 declaration is not since many attributes of the RTL will
1734 change. */
1735 COPY_DECL_RTL (olddecl, newdecl);
1736 }
1737 }
1738 /* If cannot merge, then use the new type and qualifiers,
1739 and don't preserve the old rtl. */
1740 else
1741 {
1742 /* Clean out any memory we had of the old declaration. */
1743 tree oldstatic = value_member (olddecl, static_aggregates);
1744 if (oldstatic)
1745 TREE_VALUE (oldstatic) = error_mark_node;
1746
1747 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1748 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1749 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1750 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1751 }
1752
1753 /* Merge the storage class information. */
1754 merge_weak (newdecl, olddecl);
1755
1756 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1757 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1758 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1759 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1760 if (! DECL_EXTERNAL (olddecl))
1761 DECL_EXTERNAL (newdecl) = 0;
1762
1763 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1764 {
1765 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1766 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1767 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1768 DECL_TEMPLATE_INSTANTIATED (newdecl)
1769 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1770 /* Don't really know how much of the language-specific
1771 values we should copy from old to new. */
1772 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1773 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1774 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1775 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1776 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1777 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1778 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1779 olddecl_friend = DECL_FRIEND_P (olddecl);
1780
1781 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1782 if (TREE_CODE (newdecl) == FUNCTION_DECL
1783 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1784 {
1785 DECL_BEFRIENDING_CLASSES (newdecl)
1786 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1787 DECL_BEFRIENDING_CLASSES (olddecl));
1788 /* DECL_THUNKS is only valid for virtual functions,
1789 otherwise it is a DECL_FRIEND_CONTEXT. */
1790 if (DECL_VIRTUAL_P (newdecl))
1791 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1792 }
1793 }
1794
1795 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1796 {
1797 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1798 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1799 {
1800 /* If newdecl is not a specialization, then it is not a
1801 template-related function at all. And that means that we
1802 should have exited above, returning 0. */
1803 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
1804 0);
1805
1806 if (TREE_USED (olddecl))
1807 /* From [temp.expl.spec]:
1808
1809 If a template, a member template or the member of a class
1810 template is explicitly specialized then that
1811 specialization shall be declared before the first use of
1812 that specialization that would cause an implicit
1813 instantiation to take place, in every translation unit in
1814 which such a use occurs. */
1815 error ("explicit specialization of %D after first use",
1816 olddecl);
1817
1818 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1819
1820 /* [temp.expl.spec/14] We don't inline explicit specialization
1821 just because the primary template says so. */
1822 }
1823 else
1824 {
1825 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1826 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1827
1828 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1829
1830 /* If either decl says `inline', this fn is inline, unless
1831 its definition was passed already. */
1832 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1833 DECL_INLINE (olddecl) = 1;
1834 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1835
1836 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1837 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1838 }
1839
1840 /* Preserve abstractness on cloned [cd]tors. */
1841 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1842
1843 if (! types_match)
1844 {
1845 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1846 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1847 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
1848 }
1849 if (! types_match || new_defines_function)
1850 {
1851 /* These need to be copied so that the names are available.
1852 Note that if the types do match, we'll preserve inline
1853 info and other bits, but if not, we won't. */
1854 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1855 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1856 }
1857 if (new_defines_function)
1858 /* If defining a function declared with other language
1859 linkage, use the previously declared language linkage. */
1860 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1861 else if (types_match)
1862 {
1863 /* If redeclaring a builtin function, and not a definition,
1864 it stays built in. */
1865 if (DECL_BUILT_IN (olddecl))
1866 {
1867 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1868 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1869 /* If we're keeping the built-in definition, keep the rtl,
1870 regardless of declaration matches. */
1871 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
1872 }
1873
1874 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1875 /* Don't clear out the arguments if we're redefining a function. */
1876 if (DECL_ARGUMENTS (olddecl))
1877 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1878 }
1879 }
1880 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1881 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1882
1883 /* Now preserve various other info from the definition. */
1884 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1885 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1886 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1887 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1888
1889 /* If either declaration has a nondefault visibility, use it. */
1890 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1891 {
1892 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1893 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1894 {
1895 warning ("%J'%D': visibility attribute ignored because it",
1896 newdecl, newdecl);
1897 warning ("%Jconflicts with previous declaration here", olddecl);
1898 }
1899 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1900 }
1901
1902 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1903 {
1904 int function_size;
1905
1906 function_size = sizeof (struct tree_decl);
1907
1908 memcpy ((char *) olddecl + sizeof (struct tree_common),
1909 (char *) newdecl + sizeof (struct tree_common),
1910 function_size - sizeof (struct tree_common));
1911
1912 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
1913 /* If newdecl is a template instantiation, it is possible that
1914 the following sequence of events has occurred:
1915
1916 o A friend function was declared in a class template. The
1917 class template was instantiated.
1918
1919 o The instantiation of the friend declaration was
1920 recorded on the instantiation list, and is newdecl.
1921
1922 o Later, however, instantiate_class_template called pushdecl
1923 on the newdecl to perform name injection. But, pushdecl in
1924 turn called duplicate_decls when it discovered that another
1925 declaration of a global function with the same name already
1926 existed.
1927
1928 o Here, in duplicate_decls, we decided to clobber newdecl.
1929
1930 If we're going to do that, we'd better make sure that
1931 olddecl, and not newdecl, is on the list of
1932 instantiations so that if we try to do the instantiation
1933 again we won't get the clobbered declaration. */
1934 reregister_specialization (newdecl,
1935 DECL_TI_TEMPLATE (newdecl),
1936 olddecl);
1937 }
1938 else
1939 {
1940 memcpy ((char *) olddecl + sizeof (struct tree_common),
1941 (char *) newdecl + sizeof (struct tree_common),
1942 sizeof (struct tree_decl) - sizeof (struct tree_common)
1943 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
1944 }
1945
1946 DECL_UID (olddecl) = olddecl_uid;
1947 if (olddecl_friend)
1948 DECL_FRIEND_P (olddecl) = 1;
1949
1950 /* NEWDECL contains the merged attribute lists.
1951 Update OLDDECL to be the same. */
1952 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1953
1954 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1955 so that encode_section_info has a chance to look at the new decl
1956 flags and attributes. */
1957 if (DECL_RTL_SET_P (olddecl)
1958 && (TREE_CODE (olddecl) == FUNCTION_DECL
1959 || (TREE_CODE (olddecl) == VAR_DECL
1960 && TREE_STATIC (olddecl))))
1961 make_decl_rtl (olddecl, NULL);
1962
1963 return olddecl;
1964}
1965\f
1966/* Generate an implicit declaration for identifier FUNCTIONID
1967 as a function of type int (). Print a warning if appropriate. */
1968
1969tree
1970implicitly_declare (tree functionid)
1971{
1972 tree decl;
1973
1974 /* We used to reuse an old implicit decl here,
1975 but this loses with inline functions because it can clobber
1976 the saved decl chains. */
1977 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
1978
1979 DECL_EXTERNAL (decl) = 1;
1980 TREE_PUBLIC (decl) = 1;
1981
1982 /* ISO standard says implicit declarations are in the innermost block.
1983 So we record the decl in the standard fashion. */
1984 pushdecl (decl);
1985 rest_of_decl_compilation (decl, NULL, 0, 0);
1986
1987 if (warn_implicit
1988 /* Only one warning per identifier. */
1989 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
1990 {
1991 pedwarn ("implicit declaration of function `%#D'", decl);
1992 }
1993
1994 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
1995
1996 return decl;
1997}
1998
1999/* Return zero if the declaration NEWDECL is valid
2000 when the declaration OLDDECL (assumed to be for the same name)
2001 has already been seen.
2002 Otherwise return an error message format string with a %s
2003 where the identifier should go. */
2004
2005static const char *
2006redeclaration_error_message (tree newdecl, tree olddecl)
2007{
2008 if (TREE_CODE (newdecl) == TYPE_DECL)
2009 {
2010 /* Because C++ can put things into name space for free,
2011 constructs like "typedef struct foo { ... } foo"
2012 would look like an erroneous redeclaration. */
2013 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2014 return 0;
2015 else
2016 return "redefinition of `%#D'";
2017 }
2018 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2019 {
2020 /* If this is a pure function, its olddecl will actually be
2021 the original initialization to `0' (which we force to call
2022 abort()). Don't complain about redefinition in this case. */
2023 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
2024 return 0;
2025
2026 /* If both functions come from different namespaces, this is not
2027 a redeclaration - this is a conflict with a used function. */
2028 if (DECL_NAMESPACE_SCOPE_P (olddecl)
1c1138ce
JS
2029 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2030 && ! decls_match (olddecl, newdecl))
003757ed
MD
2031 return "`%D' conflicts with used function";
2032
2033 /* We'll complain about linkage mismatches in
2034 warn_extern_redeclared_static. */
2035
2036 /* Defining the same name twice is no good. */
2037 if (DECL_INITIAL (olddecl) != NULL_TREE
2038 && DECL_INITIAL (newdecl) != NULL_TREE)
2039 {
2040 if (DECL_NAME (olddecl) == NULL_TREE)
2041 return "`%#D' not declared in class";
2042 else
2043 return "redefinition of `%#D'";
2044 }
2045 return 0;
2046 }
2047 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2048 {
2049 tree nt, ot;
2050
2051 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2052 {
2053 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2054 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2055 return "redefinition of `%#D'";
2056 return NULL;
2057 }
2058
2059 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2060 || (DECL_TEMPLATE_RESULT (newdecl)
2061 == DECL_TEMPLATE_RESULT (olddecl)))
2062 return NULL;
2063
2064 nt = DECL_TEMPLATE_RESULT (newdecl);
2065 if (DECL_TEMPLATE_INFO (nt))
2066 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2067 ot = DECL_TEMPLATE_RESULT (olddecl);
2068 if (DECL_TEMPLATE_INFO (ot))
2069 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2070 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2071 return "redefinition of `%#D'";
2072
2073 return NULL;
2074 }
2075 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2076 {
2077 /* Objects declared at top level: */
2078 /* If at least one is a reference, it's ok. */
2079 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2080 return 0;
2081 /* Reject two definitions. */
2082 return "redefinition of `%#D'";
2083 }
2084 else
2085 {
2086 /* Objects declared with block scope: */
2087 /* Reject two definitions, and reject a definition
2088 together with an external reference. */
2089 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2090 return "redeclaration of `%#D'";
2091 return 0;
2092 }
2093}
2094\f
2095/* Create a new label, named ID. */
2096
2097static tree
2098make_label_decl (tree id, int local_p)
2099{
2100 tree decl;
2101
2102 decl = build_decl (LABEL_DECL, id, void_type_node);
2103
2104 DECL_CONTEXT (decl) = current_function_decl;
2105 DECL_MODE (decl) = VOIDmode;
2106 C_DECLARED_LABEL_FLAG (decl) = local_p;
2107
2108 /* Say where one reference is to the label, for the sake of the
2109 error if it is not defined. */
2110 DECL_SOURCE_LOCATION (decl) = input_location;
2111
2112 /* Record the fact that this identifier is bound to this label. */
2113 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2114
2115 return decl;
2116}
2117
2118/* Record this label on the list of used labels so that we can check
2119 at the end of the function to see whether or not the label was
2120 actually defined, and so we can check when the label is defined whether
2121 this use is valid. */
2122
2123static void
2124use_label (tree decl)
2125{
2126 if (named_label_uses == NULL
2127 || named_label_uses->names_in_scope != current_binding_level->names
2128 || named_label_uses->label_decl != decl)
2129 {
2130 struct named_label_use_list *new_ent;
2131 new_ent = ggc_alloc (sizeof (struct named_label_use_list));
2132 new_ent->label_decl = decl;
2133 new_ent->names_in_scope = current_binding_level->names;
2134 new_ent->binding_level = current_binding_level;
2135 new_ent->o_goto_locus = input_location;
2136 new_ent->next = named_label_uses;
2137 named_label_uses = new_ent;
2138 }
2139}
2140
2141/* Look for a label named ID in the current function. If one cannot
2142 be found, create one. (We keep track of used, but undefined,
2143 labels, and complain about them at the end of a function.) */
2144
2145tree
2146lookup_label (tree id)
2147{
2148 tree decl;
2149 struct named_label_list *ent;
2150
2151 timevar_push (TV_NAME_LOOKUP);
2152 /* You can't use labels at global scope. */
2153 if (current_function_decl == NULL_TREE)
2154 {
2155 error ("label `%s' referenced outside of any function",
2156 IDENTIFIER_POINTER (id));
2157 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2158 }
2159
2160 /* See if we've already got this label. */
2161 decl = IDENTIFIER_LABEL_VALUE (id);
2162 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2163 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2164
2165 /* Record this label on the list of labels used in this function.
2166 We do this before calling make_label_decl so that we get the
2167 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2168 ent = ggc_alloc_cleared (sizeof (struct named_label_list));
2169 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
2170 ent->next = named_labels;
2171 named_labels = ent;
2172
2173 /* We need a new label. */
2174 decl = make_label_decl (id, /*local_p=*/0);
2175
2176 /* Now fill in the information we didn't have before. */
2177 ent->label_decl = decl;
2178
2179 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2180}
2181
2182/* Declare a local label named ID. */
2183
2184tree
2185declare_local_label (tree id)
2186{
2187 tree decl;
2188
2189 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2190 this scope we can restore the old value of
2191 IDENTIFIER_TYPE_VALUE. */
2192 current_binding_level->shadowed_labels
2193 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2194 current_binding_level->shadowed_labels);
2195 /* Look for the label. */
2196 decl = make_label_decl (id, /*local_p=*/1);
2197 /* Now fill in the information we didn't have before. */
2198 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
2199
2200 return decl;
2201}
2202
2203/* Returns nonzero if it is ill-formed to jump past the declaration of
2204 DECL. Returns 2 if it's also a real problem. */
2205
2206static int
2207decl_jump_unsafe (tree decl)
2208{
2209 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
2210 return 0;
2211
2212 if (DECL_INITIAL (decl) == NULL_TREE
2213 && pod_type_p (TREE_TYPE (decl)))
2214 return 0;
2215
2216 /* This is really only important if we're crossing an initialization.
2217 The POD stuff is just pedantry; why should it matter if the class
2218 contains a field of pointer to member type? */
2219 if (DECL_INITIAL (decl)
2220 || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
2221 return 2;
2222 return 1;
2223}
2224
2225/* Check that a single previously seen jump to a newly defined label
2226 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2227 the jump context; NAMES are the names in scope in LEVEL at the jump
2228 context; FILE and LINE are the source position of the jump or 0. */
2229
2230static void
2231check_previous_goto_1 (tree decl,
2232 struct cp_binding_level* level,
2233 tree names, const location_t *locus)
2234{
2235 int identified = 0;
2236 int saw_eh = 0;
2237 struct cp_binding_level *b = current_binding_level;
2238 for (; b; b = b->level_chain)
2239 {
2240 tree new_decls = b->names;
2241 tree old_decls = (b == level ? names : NULL_TREE);
2242 for (; new_decls != old_decls;
2243 new_decls = TREE_CHAIN (new_decls))
2244 {
2245 int problem = decl_jump_unsafe (new_decls);
2246 if (! problem)
2247 continue;
2248
2249 if (! identified)
2250 {
2251 if (decl)
2252 pedwarn ("jump to label `%D'", decl);
2253 else
2254 pedwarn ("jump to case label");
2255
2256 if (locus)
2257 pedwarn ("%H from here", locus);
2258 identified = 1;
2259 }
2260
2261 if (problem > 1)
2262 cp_error_at (" crosses initialization of `%#D'",
2263 new_decls);
2264 else
2265 cp_pedwarn_at (" enters scope of non-POD `%#D'",
2266 new_decls);
2267 }
2268
2269 if (b == level)
2270 break;
2271 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
2272 {
2273 if (! identified)
2274 {
2275 if (decl)
2276 pedwarn ("jump to label `%D'", decl);
2277 else
2278 pedwarn ("jump to case label");
2279
2280 if (locus)
2281 pedwarn ("%H from here", locus);
2282 identified = 1;
2283 }
2284 if (b->kind == sk_try)
2285 error (" enters try block");
2286 else
2287 error (" enters catch block");
2288 saw_eh = 1;
2289 }
2290 }
2291}
2292
2293static void
2294check_previous_goto (struct named_label_use_list* use)
2295{
2296 check_previous_goto_1 (use->label_decl, use->binding_level,
2297 use->names_in_scope, &use->o_goto_locus);
2298}
2299
2300static void
2301check_switch_goto (struct cp_binding_level* level)
2302{
2303 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
2304}
2305
2306/* Check that any previously seen jumps to a newly defined label DECL
2307 are OK. Called by define_label. */
2308
2309static void
2310check_previous_gotos (tree decl)
2311{
2312 struct named_label_use_list **usep;
2313
2314 if (! TREE_USED (decl))
2315 return;
2316
2317 for (usep = &named_label_uses; *usep; )
2318 {
2319 struct named_label_use_list *use = *usep;
2320 if (use->label_decl == decl)
2321 {
2322 check_previous_goto (use);
2323 *usep = use->next;
2324 }
2325 else
2326 usep = &(use->next);
2327 }
2328}
2329
2330/* Check that a new jump to a label DECL is OK. Called by
2331 finish_goto_stmt. */
2332
2333void
2334check_goto (tree decl)
2335{
2336 int identified = 0;
2337 tree bad;
2338 struct named_label_list *lab;
2339
2340 /* We can't know where a computed goto is jumping. So we assume
2341 that it's OK. */
2342 if (! DECL_P (decl))
2343 return;
2344
2345 /* If the label hasn't been defined yet, defer checking. */
2346 if (! DECL_INITIAL (decl))
2347 {
2348 use_label (decl);
2349 return;
2350 }
2351
2352 for (lab = named_labels; lab; lab = lab->next)
2353 if (decl == lab->label_decl)
2354 break;
2355
2356 /* If the label is not on named_labels it's a gcc local label, so
2357 it must be in an outer scope, so jumping to it is always OK. */
2358 if (lab == 0)
2359 return;
2360
2361 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
2362 && !identified)
2363 {
2364 cp_pedwarn_at ("jump to label `%D'", decl);
2365 pedwarn (" from here");
2366 identified = 1;
2367 }
2368
2369 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
2370 {
2371 tree b = TREE_VALUE (bad);
2372 int u = decl_jump_unsafe (b);
2373
2374 if (u > 1 && DECL_ARTIFICIAL (b))
2375 /* Can't skip init of __exception_info. */
2376 error ("%J enters catch block", b);
2377 else if (u > 1)
2378 cp_error_at (" skips initialization of `%#D'", b);
2379 else
2380 cp_pedwarn_at (" enters scope of non-POD `%#D'", b);
2381 }
2382
2383 if (lab->in_try_scope)
2384 error (" enters try block");
2385 else if (lab->in_catch_scope)
2386 error (" enters catch block");
2387}
2388
2389/* Define a label, specifying the location in the source file.
2390 Return the LABEL_DECL node for the label. */
2391
2392tree
2393define_label (location_t location, tree name)
2394{
2395 tree decl = lookup_label (name);
2396 struct named_label_list *ent;
2397 struct cp_binding_level *p;
2398
2399 timevar_push (TV_NAME_LOOKUP);
2400 for (ent = named_labels; ent; ent = ent->next)
2401 if (ent->label_decl == decl)
2402 break;
2403
2404 /* After labels, make any new cleanups in the function go into their
2405 own new (temporary) binding contour. */
2406 for (p = current_binding_level;
2407 p->kind != sk_function_parms;
2408 p = p->level_chain)
2409 p->more_cleanups_ok = 0;
2410
2411 if (name == get_identifier ("wchar_t"))
2412 pedwarn ("label named wchar_t");
2413
2414 if (DECL_INITIAL (decl) != NULL_TREE)
2415 error ("duplicate label `%D'", decl);
2416 else
2417 {
2418 /* Mark label as having been defined. */
2419 DECL_INITIAL (decl) = error_mark_node;
2420 /* Say where in the source. */
2421 DECL_SOURCE_LOCATION (decl) = location;
2422 if (ent)
2423 {
2424 ent->names_in_scope = current_binding_level->names;
2425 ent->binding_level = current_binding_level;
2426 }
2427 check_previous_gotos (decl);
2428 }
2429
2430 timevar_pop (TV_NAME_LOOKUP);
2431 return decl;
2432}
2433
2434struct cp_switch
2435{
2436 struct cp_binding_level *level;
2437 struct cp_switch *next;
2438 /* The SWITCH_STMT being built. */
2439 tree switch_stmt;
2440 /* A splay-tree mapping the low element of a case range to the high
2441 element, or NULL_TREE if there is no high element. Used to
2442 determine whether or not a new case label duplicates an old case
2443 label. We need a tree, rather than simply a hash table, because
2444 of the GNU case range extension. */
2445 splay_tree cases;
2446};
2447
2448/* A stack of the currently active switch statements. The innermost
2449 switch statement is on the top of the stack. There is no need to
2450 mark the stack for garbage collection because it is only active
2451 during the processing of the body of a function, and we never
2452 collect at that point. */
2453
2454static struct cp_switch *switch_stack;
2455
2456/* Called right after a switch-statement condition is parsed.
2457 SWITCH_STMT is the switch statement being parsed. */
2458
2459void
2460push_switch (tree switch_stmt)
2461{
2462 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
2463 p->level = current_binding_level;
2464 p->next = switch_stack;
2465 p->switch_stmt = switch_stmt;
2466 p->cases = splay_tree_new (case_compare, NULL, NULL);
2467 switch_stack = p;
2468}
2469
2470void
2471pop_switch (void)
2472{
2473 struct cp_switch *cs;
2474
2475 cs = switch_stack;
2476 splay_tree_delete (cs->cases);
2477 switch_stack = switch_stack->next;
2478 free (cs);
2479}
2480
2481/* Note that we've seen a definition of a case label, and complain if this
2482 is a bad place for one. */
2483
2484tree
2485finish_case_label (tree low_value, tree high_value)
2486{
2487 tree cond, r;
2488 struct cp_binding_level *p;
2489
2490 if (processing_template_decl)
2491 {
2492 tree label;
2493
2494 /* For templates, just add the case label; we'll do semantic
2495 analysis at instantiation-time. */
2496 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2497 return add_stmt (build_case_label (low_value, high_value, label));
2498 }
2499
2500 /* Find the condition on which this switch statement depends. */
2501 cond = SWITCH_COND (switch_stack->switch_stmt);
2502 if (cond && TREE_CODE (cond) == TREE_LIST)
2503 cond = TREE_VALUE (cond);
2504
2505 r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
2506
2507 check_switch_goto (switch_stack->level);
2508
2509 /* After labels, make any new cleanups in the function go into their
2510 own new (temporary) binding contour. */
2511 for (p = current_binding_level;
2512 p->kind != sk_function_parms;
2513 p = p->level_chain)
2514 p->more_cleanups_ok = 0;
2515
2516 return r;
2517}
2518\f
2519/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2520
2521static hashval_t
2522typename_hash (const void* k)
2523{
2524 hashval_t hash;
2525 tree t = (tree) k;
2526
2527 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2528 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2529
2530 return hash;
2531}
2532
2533/* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2534
2535static int
2536typename_compare (const void * k1, const void * k2)
2537{
2538 tree t1;
2539 tree t2;
2540 tree d1;
2541 tree d2;
2542
2543 t1 = (tree) k1;
2544 t2 = (tree) k2;
2545 d1 = TYPE_NAME (t1);
2546 d2 = TYPE_NAME (t2);
2547
2548 return (DECL_NAME (d1) == DECL_NAME (d2)
2549 && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
2550 && ((TREE_TYPE (t1) != NULL_TREE)
2551 == (TREE_TYPE (t2) != NULL_TREE))
2552 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2553 && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
2554}
2555
2556/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2557 the type of `T', NAME is the IDENTIFIER_NODE for `t'. If BASE_TYPE
2558 is non-NULL, this type is being created by the implicit typename
2559 extension, and BASE_TYPE is a type named `t' in some base class of
2560 `T' which depends on template parameters.
2561
2562 Returns the new TYPENAME_TYPE. */
2563
2564static GTY ((param_is (union tree_node))) htab_t typename_htab;
2565
2566static tree
2567build_typename_type (tree context, tree name, tree fullname)
2568{
2569 tree t;
2570 tree d;
2571 void **e;
2572
2573 if (typename_htab == NULL)
2574 {
2575 typename_htab = htab_create_ggc (61, &typename_hash,
2576 &typename_compare, NULL);
2577 }
2578
2579 /* Build the TYPENAME_TYPE. */
2580 t = make_aggr_type (TYPENAME_TYPE);
2581 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2582 TYPENAME_TYPE_FULLNAME (t) = fullname;
2583
2584 /* Build the corresponding TYPE_DECL. */
2585 d = build_decl (TYPE_DECL, name, t);
2586 TYPE_NAME (TREE_TYPE (d)) = d;
2587 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2588 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2589 DECL_ARTIFICIAL (d) = 1;
2590
2591 /* See if we already have this type. */
2592 e = htab_find_slot (typename_htab, t, INSERT);
2593 if (*e)
2594 t = (tree) *e;
2595 else
2596 *e = t;
2597
2598 return t;
2599}
2600
2601/* Resolve `typename CONTEXT::NAME'. Returns an appropriate type,
2602 unless an error occurs, in which case error_mark_node is returned.
2603 If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
2604 set, we return that, rather than the _TYPE it corresponds to, in
2605 other cases we look through the type decl. If TF_ERROR is set,
2606 complain about errors, otherwise be quiet. */
2607
2608tree
2609make_typename_type (tree context, tree name, tsubst_flags_t complain)
2610{
2611 tree fullname;
2612
2613 if (name == error_mark_node
2614 || context == NULL_TREE
2615 || context == error_mark_node)
2616 return error_mark_node;
2617
2618 if (TYPE_P (name))
2619 {
2620 if (!(TYPE_LANG_SPECIFIC (name)
2621 && (CLASSTYPE_IS_TEMPLATE (name)
2622 || CLASSTYPE_USE_TEMPLATE (name))))
2623 name = TYPE_IDENTIFIER (name);
2624 else
2625 /* Create a TEMPLATE_ID_EXPR for the type. */
2626 name = build_nt (TEMPLATE_ID_EXPR,
2627 CLASSTYPE_TI_TEMPLATE (name),
2628 CLASSTYPE_TI_ARGS (name));
2629 }
2630 else if (TREE_CODE (name) == TYPE_DECL)
2631 name = DECL_NAME (name);
2632
2633 fullname = name;
2634
2635 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2636 {
2637 name = TREE_OPERAND (name, 0);
2638 if (TREE_CODE (name) == TEMPLATE_DECL)
2639 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2640 }
2641 if (TREE_CODE (name) == TEMPLATE_DECL)
2642 {
2643 error ("`%D' used without template parameters", name);
2644 return error_mark_node;
2645 }
2646 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030802);
1378ea41 2647 my_friendly_assert (TYPE_P (context), 20050905);
003757ed
MD
2648
2649 if (!dependent_type_p (context)
2650 || currently_open_class (context))
2651 {
2652 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
2653 {
2654 tree tmpl = NULL_TREE;
2655 if (IS_AGGR_TYPE (context))
2656 tmpl = lookup_field (context, name, 0, false);
2657 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2658 {
2659 if (complain & tf_error)
2660 error ("no class template named `%#T' in `%#T'",
2661 name, context);
2662 return error_mark_node;
2663 }
2664
2665 if (complain & tf_error)
2666 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2667
2668 return lookup_template_class (tmpl,
2669 TREE_OPERAND (fullname, 1),
2670 NULL_TREE, context,
2671 /*entering_scope=*/0,
2672 tf_error | tf_warning | tf_user);
2673 }
2674 else
2675 {
2676 tree t;
2677
2678 if (!IS_AGGR_TYPE (context))
2679 {
2680 if (complain & tf_error)
2681 error ("no type named `%#T' in `%#T'", name, context);
2682 return error_mark_node;
2683 }
2684
2685 t = lookup_field (context, name, 0, true);
2686 if (t)
2687 {
2688 if (TREE_CODE (t) != TYPE_DECL)
2689 {
2690 if (complain & tf_error)
2691 error ("no type named `%#T' in `%#T'", name, context);
2692 return error_mark_node;
2693 }
2694
2695 if (complain & tf_error)
2696 perform_or_defer_access_check (TYPE_BINFO (context), t);
2697
2698 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2699 t = TREE_TYPE (t);
2700
2701 return t;
2702 }
2703 }
2704 }
2705
2706 /* If the CONTEXT is not a template type, then either the field is
2707 there now or its never going to be. */
2708 if (!dependent_type_p (context))
2709 {
2710 if (complain & tf_error)
2711 error ("no type named `%#T' in `%#T'", name, context);
2712 return error_mark_node;
2713 }
2714
2715 return build_typename_type (context, name, fullname);
2716}
2717
2718/* Resolve `CONTEXT::template NAME'. Returns an appropriate type,
2719 unless an error occurs, in which case error_mark_node is returned.
2720 If we locate a TYPE_DECL, we return that, rather than the _TYPE it
2721 corresponds to. If COMPLAIN zero, don't complain about any errors
2722 that occur. */
2723
2724tree
2725make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
2726{
2727 tree t;
2728 tree d;
2729
2730 if (TYPE_P (name))
2731 name = TYPE_IDENTIFIER (name);
2732 else if (DECL_P (name))
2733 name = DECL_NAME (name);
2734 if (TREE_CODE (name) != IDENTIFIER_NODE)
2735 abort ();
2736
2737 if (!dependent_type_p (context)
2738 || currently_open_class (context))
2739 {
2740 tree tmpl = NULL_TREE;
2741
2742 if (IS_AGGR_TYPE (context))
2743 tmpl = lookup_field (context, name, 0, false);
2744
2745 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2746 {
2747 if (complain & tf_error)
2748 error ("no class template named `%#T' in `%#T'", name, context);
2749 return error_mark_node;
2750 }
2751
2752 if (complain & tf_error)
2753 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2754
2755 return tmpl;
2756 }
2757
2758 /* Build the UNBOUND_CLASS_TEMPLATE. */
2759 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2760 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2761 TREE_TYPE (t) = NULL_TREE;
2762
2763 /* Build the corresponding TEMPLATE_DECL. */
2764 d = build_decl (TEMPLATE_DECL, name, t);
2765 TYPE_NAME (TREE_TYPE (d)) = d;
2766 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2767 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2768 DECL_ARTIFICIAL (d) = 1;
2769
2770 return t;
2771}
2772
2773\f
2774
2775/* A chain of TYPE_DECLs for the builtin types. */
2776
2777static GTY(()) tree builtin_type_decls;
2778
2779/* Return a chain of TYPE_DECLs for the builtin types. */
2780
2781tree
2782cxx_builtin_type_decls (void)
2783{
2784 return builtin_type_decls;
2785}
2786
2787/* Push the declarations of builtin types into the namespace.
2788 RID_INDEX is the index of the builtin type in the array
2789 RID_POINTERS. NAME is the name used when looking up the builtin
2790 type. TYPE is the _TYPE node for the builtin type. */
2791
2792void
2793record_builtin_type (enum rid rid_index,
2794 const char* name,
2795 tree type)
2796{
2797 tree rname = NULL_TREE, tname = NULL_TREE;
2798 tree tdecl = NULL_TREE;
2799
2800 if ((int) rid_index < (int) RID_MAX)
2801 rname = ridpointers[(int) rid_index];
2802 if (name)
2803 tname = get_identifier (name);
2804
2805 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
2806 eliminated. Built-in types should not be looked up name; their
2807 names are keywords that the parser can recognize. However, there
2808 is code in c-common.c that uses identifier_global_value to look
2809 up built-in types by name. */
2810 if (tname)
2811 {
2812 tdecl = build_decl (TYPE_DECL, tname, type);
2813 DECL_ARTIFICIAL (tdecl) = 1;
2814 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
2815 }
2816 if (rname)
2817 {
2818 if (!tdecl)
2819 {
2820 tdecl = build_decl (TYPE_DECL, rname, type);
2821 DECL_ARTIFICIAL (tdecl) = 1;
2822 }
2823 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
2824 }
2825
2826 if (!TYPE_NAME (type))
2827 TYPE_NAME (type) = tdecl;
2828
2829 if (tdecl)
2830 {
2831 TREE_CHAIN (tdecl) = builtin_type_decls;
2832 builtin_type_decls = tdecl;
2833 }
2834}
2835
2836/* Record one of the standard Java types.
2837 * Declare it as having the given NAME.
2838 * If SIZE > 0, it is the size of one of the integral types;
2839 * otherwise it is the negative of the size of one of the other types. */
2840
2841static tree
2842record_builtin_java_type (const char* name, int size)
2843{
2844 tree type, decl;
2845 if (size > 0)
2846 type = make_signed_type (size);
2847 else if (size > -32)
2848 { /* "__java_char" or ""__java_boolean". */
2849 type = make_unsigned_type (-size);
2850 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
2851 }
2852 else
2853 { /* "__java_float" or ""__java_double". */
2854 type = make_node (REAL_TYPE);
2855 TYPE_PRECISION (type) = - size;
2856 layout_type (type);
2857 }
2858 record_builtin_type (RID_MAX, name, type);
2859 decl = TYPE_NAME (type);
2860
2861 /* Suppress generate debug symbol entries for these types,
2862 since for normal C++ they are just clutter.
2863 However, push_lang_context undoes this if extern "Java" is seen. */
2864 DECL_IGNORED_P (decl) = 1;
2865
2866 TYPE_FOR_JAVA (type) = 1;
2867 return type;
2868}
2869
2870/* Push a type into the namespace so that the back-ends ignore it. */
2871
2872static void
2873record_unknown_type (tree type, const char* name)
2874{
2875 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
2876 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
2877 DECL_IGNORED_P (decl) = 1;
2878 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
2879 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
2880 TYPE_ALIGN (type) = 1;
2881 TYPE_USER_ALIGN (type) = 0;
2882 TYPE_MODE (type) = TYPE_MODE (void_type_node);
2883}
2884
2885/* An string for which we should create an IDENTIFIER_NODE at
2886 startup. */
2887
2888typedef struct predefined_identifier
2889{
2890 /* The name of the identifier. */
2891 const char *const name;
2892 /* The place where the IDENTIFIER_NODE should be stored. */
2893 tree *const node;
2894 /* Nonzero if this is the name of a constructor or destructor. */
2895 const int ctor_or_dtor_p;
2896} predefined_identifier;
2897
2898/* Create all the predefined identifiers. */
2899
2900static void
2901initialize_predefined_identifiers (void)
2902{
2903 const predefined_identifier *pid;
2904
2905 /* A table of identifiers to create at startup. */
2906 static const predefined_identifier predefined_identifiers[] = {
2907 { "C++", &lang_name_cplusplus, 0 },
2908 { "C", &lang_name_c, 0 },
2909 { "Java", &lang_name_java, 0 },
2910 { CTOR_NAME, &ctor_identifier, 1 },
2911 { "__base_ctor", &base_ctor_identifier, 1 },
2912 { "__comp_ctor", &complete_ctor_identifier, 1 },
2913 { DTOR_NAME, &dtor_identifier, 1 },
2914 { "__comp_dtor", &complete_dtor_identifier, 1 },
2915 { "__base_dtor", &base_dtor_identifier, 1 },
2916 { "__deleting_dtor", &deleting_dtor_identifier, 1 },
2917 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
2918 { "nelts", &nelts_identifier, 0 },
2919 { THIS_NAME, &this_identifier, 0 },
2920 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
2921 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
2922 { "_vptr", &vptr_identifier, 0 },
2923 { "__vtt_parm", &vtt_parm_identifier, 0 },
2924 { "::", &global_scope_name, 0 },
2925 { "std", &std_identifier, 0 },
2926 { NULL, NULL, 0 }
2927 };
2928
2929 for (pid = predefined_identifiers; pid->name; ++pid)
2930 {
2931 *pid->node = get_identifier (pid->name);
2932 if (pid->ctor_or_dtor_p)
2933 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
2934 }
2935}
2936
2937/* Create the predefined scalar types of C,
2938 and some nodes representing standard constants (0, 1, (void *)0).
2939 Initialize the global binding level.
2940 Make definitions for built-in primitive functions. */
2941
2942void
2943cxx_init_decl_processing (void)
2944{
2945 tree void_ftype;
2946 tree void_ftype_ptr;
2947
2948 /* Create all the identifiers we need. */
2949 initialize_predefined_identifiers ();
2950
2951 /* Fill in back-end hooks. */
2952 lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
2953
2954 /* Create the global variables. */
2955 push_to_top_level ();
2956
2957 current_function_decl = NULL_TREE;
2958 current_binding_level = NULL;
2959 /* Enter the global namespace. */
2960 my_friendly_assert (global_namespace == NULL_TREE, 375);
2961 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
2962 void_type_node);
2963 begin_scope (sk_namespace, global_namespace);
2964
2965 current_lang_name = NULL_TREE;
2966
2967 /* Adjust various flags based on command-line settings. */
2968 if (!flag_permissive)
2969 flag_pedantic_errors = 1;
2970 if (!flag_no_inline)
2971 {
2972 flag_inline_trees = 1;
2973 flag_no_inline = 1;
2974 }
2975 if (flag_inline_functions)
2976 {
2977 flag_inline_trees = 2;
2978 flag_inline_functions = 0;
2979 }
2980
2981 /* Force minimum function alignment if using the least significant
2982 bit of function pointers to store the virtual bit. */
2983 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
2984 && force_align_functions_log < 1)
2985 force_align_functions_log = 1;
2986
2987 /* Initially, C. */
2988 current_lang_name = lang_name_c;
2989
2990 build_common_tree_nodes (flag_signed_char);
2991
2992 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
2993 TREE_TYPE (error_mark_list) = error_mark_node;
2994
2995 /* Create the `std' namespace. */
2996 push_namespace (std_identifier);
2997 std_node = current_namespace;
2998 pop_namespace ();
2999
3000 c_common_nodes_and_builtins ();
3001
3002 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3003 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3004 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3005 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3006 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3007 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3008 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3009 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3010
3011 integer_two_node = build_int_2 (2, 0);
3012 TREE_TYPE (integer_two_node) = integer_type_node;
3013 integer_three_node = build_int_2 (3, 0);
3014 TREE_TYPE (integer_three_node) = integer_type_node;
3015
3016 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3017 truthvalue_type_node = boolean_type_node;
3018 truthvalue_false_node = boolean_false_node;
3019 truthvalue_true_node = boolean_true_node;
3020
3021 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3022
3023#if 0
3024 record_builtin_type (RID_MAX, NULL, string_type_node);
3025#endif
3026
3027 delta_type_node = ptrdiff_type_node;
3028 vtable_index_type = ptrdiff_type_node;
3029
3030 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3031 void_ftype = build_function_type (void_type_node, void_list_node);
3032 void_ftype_ptr = build_function_type (void_type_node,
3033 tree_cons (NULL_TREE,
3034 ptr_type_node,
3035 void_list_node));
3036 void_ftype_ptr
3037 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3038
3039 /* C++ extensions */
3040
3041 unknown_type_node = make_node (UNKNOWN_TYPE);
3042 record_unknown_type (unknown_type_node, "unknown type");
3043
3044 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3045 TREE_TYPE (unknown_type_node) = unknown_type_node;
3046
3047 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3048 result. */
3049 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3050 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3051
3052 {
3053 /* Make sure we get a unique function type, so we can give
3054 its pointer type a name. (This wins for gdb.) */
3055 tree vfunc_type = make_node (FUNCTION_TYPE);
3056 TREE_TYPE (vfunc_type) = integer_type_node;
3057 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3058 layout_type (vfunc_type);
3059
3060 vtable_entry_type = build_pointer_type (vfunc_type);
3061 }
3062 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3063
3064 vtbl_type_node
3065 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3066 layout_type (vtbl_type_node);
3067 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3068 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3069 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3070 layout_type (vtbl_ptr_type_node);
3071 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3072
3073 push_namespace (get_identifier ("__cxxabiv1"));
3074 abi_node = current_namespace;
3075 pop_namespace ();
3076
3077 global_type_node = make_node (LANG_TYPE);
3078 record_unknown_type (global_type_node, "global type");
3079
3080 /* Now, C++. */
3081 current_lang_name = lang_name_cplusplus;
3082
3083 {
3084 tree bad_alloc_id;
3085 tree bad_alloc_type_node;
3086 tree bad_alloc_decl;
3087 tree newtype, deltype;
3088 tree ptr_ftype_sizetype;
3089
3090 push_namespace (std_identifier);
3091 bad_alloc_id = get_identifier ("bad_alloc");
3092 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3093 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3094 bad_alloc_decl
3095 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3096 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3097 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3098 pop_namespace ();
3099
3100 ptr_ftype_sizetype
3101 = build_function_type (ptr_type_node,
3102 tree_cons (NULL_TREE,
3103 size_type_node,
3104 void_list_node));
3105 newtype = build_exception_variant
3106 (ptr_ftype_sizetype, add_exception_specifier
3107 (NULL_TREE, bad_alloc_type_node, -1));
3108 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3109 push_cp_library_fn (NEW_EXPR, newtype);
3110 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3111 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3112 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3113 }
3114
3115 abort_fndecl
3116 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3117
3118 /* Perform other language dependent initializations. */
3119 init_class_processing ();
3120 init_search_processing ();
3121 init_rtti_processing ();
3122
3123 if (flag_exceptions)
3124 init_exception_processing ();
3125
3126 if (! supports_one_only ())
3127 flag_weak = 0;
3128
3129 make_fname_decl = cp_make_fname_decl;
3130 start_fname_decls ();
3131
3132 /* Show we use EH for cleanups. */
3133 using_eh_for_cleanups ();
3134
3135 /* Maintain consistency. Perhaps we should just complain if they
3136 say -fwritable-strings? */
3137 if (flag_writable_strings)
3138 flag_const_strings = 0;
3139}
3140
3141/* Generate an initializer for a function naming variable from
3142 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3143 filled in with the type of the init. */
3144
3145tree
3146cp_fname_init (const char* name, tree *type_p)
3147{
3148 tree domain = NULL_TREE;
3149 tree type;
3150 tree init = NULL_TREE;
3151 size_t length = 0;
3152
3153 if (name)
3154 {
3155 length = strlen (name);
3156 domain = build_index_type (size_int (length));
3157 init = build_string (length + 1, name);
3158 }
3159
3160 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3161 type = build_cplus_array_type (type, domain);
3162
3163 *type_p = type;
3164
3165 if (init)
3166 TREE_TYPE (init) = type;
3167 else
3168 init = error_mark_node;
3169
3170 return init;
3171}
3172
3173/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3174 decl, NAME is the initialization string and TYPE_DEP indicates whether
3175 NAME depended on the type of the function. We make use of that to detect
3176 __PRETTY_FUNCTION__ inside a template fn. This is being done
3177 lazily at the point of first use, so we musn't push the decl now. */
3178
3179static tree
3180cp_make_fname_decl (tree id, int type_dep)
3181{
3182 const char *const name = (type_dep && processing_template_decl
3183 ? NULL : fname_as_string (type_dep));
3184 tree type;
3185 tree init = cp_fname_init (name, &type);
3186 tree decl = build_decl (VAR_DECL, id, type);
3187
3188 /* As we're using pushdecl_with_scope, we must set the context. */
3189 DECL_CONTEXT (decl) = current_function_decl;
3190 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3191
3192 TREE_STATIC (decl) = 1;
3193 TREE_READONLY (decl) = 1;
3194 DECL_ARTIFICIAL (decl) = 1;
3195 DECL_INITIAL (decl) = init;
3196
3197 TREE_USED (decl) = 1;
3198
3199 if (current_function_decl)
3200 {
3201 struct cp_binding_level *b = current_binding_level;
3202 while (b->level_chain->kind != sk_function_parms)
3203 b = b->level_chain;
3204 pushdecl_with_scope (decl, b);
3205 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
3206 }
3207 else
3208 pushdecl_top_level_and_finish (decl, init);
3209
3210 return decl;
3211}
3212
3213/* Make a definition for a builtin function named NAME in the current
3214 namespace, whose data type is TYPE and whose context is CONTEXT.
3215 TYPE should be a function type with argument types.
3216
3217 CLASS and CODE tell later passes how to compile calls to this function.
3218 See tree.h for possible values.
3219
3220 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3221 the name to be called if we can't opencode the function.
3222 If ATTRS is nonzero, use that for the function's attribute
3223 list. */
3224
3225static tree
3226builtin_function_1 (const char* name,
3227 tree type,
3228 tree context,
3229 int code,
3230 enum built_in_class class,
3231 const char* libname,
3232 tree attrs)
3233{
3234 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
3235 DECL_BUILT_IN_CLASS (decl) = class;
3236 DECL_FUNCTION_CODE (decl) = code;
3237 DECL_CONTEXT (decl) = context;
3238
3239 pushdecl (decl);
3240
3241 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3242 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3243 function in the namespace. */
3244 if (libname)
3245 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3246 make_decl_rtl (decl, NULL);
3247
3248 /* Warn if a function in the namespace for users
3249 is used without an occasion to consider it declared. */
3250 if (name[0] != '_' || name[1] != '_')
3251 DECL_ANTICIPATED (decl) = 1;
3252
3253 /* Possibly apply some default attributes to this built-in function. */
3254 if (attrs)
3255 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3256 else
3257 decl_attributes (&decl, NULL_TREE, 0);
3258
3259 return decl;
3260}
3261
3262/* Entry point for the benefit of c_common_nodes_and_builtins.
3263
3264 Make a definition for a builtin function named NAME and whose data type
3265 is TYPE. TYPE should be a function type with argument types. This
3266 function places the anticipated declaration in the global namespace
3267 and additionally in the std namespace if appropriate.
3268
3269 CLASS and CODE tell later passes how to compile calls to this function.
3270 See tree.h for possible values.
3271
3272 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3273 the name to be called if we can't opencode the function.
3274
3275 If ATTRS is nonzero, use that for the function's attribute
3276 list. */
3277
3278tree
3279builtin_function (const char* name,
3280 tree type,
3281 int code,
3282 enum built_in_class class,
3283 const char* libname,
3284 tree attrs)
3285{
3286 /* All builtins that don't begin with an '_' should additionally
3287 go in the 'std' namespace. */
3288 if (name[0] != '_')
3289 {
3290 push_namespace (std_identifier);
3291 builtin_function_1 (name, type, std_node, code, class, libname, attrs);
3292 pop_namespace ();
3293 }
3294
3295 return builtin_function_1 (name, type, NULL_TREE, code,
3296 class, libname, attrs);
3297}
3298
3299/* Generate a FUNCTION_DECL with the typical flags for a runtime library
3300 function. Not called directly. */
3301
3302static tree
3303build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3304{
3305 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3306 DECL_EXTERNAL (fn) = 1;
3307 TREE_PUBLIC (fn) = 1;
3308 DECL_ARTIFICIAL (fn) = 1;
3309 TREE_NOTHROW (fn) = 1;
3310 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3311 SET_DECL_LANGUAGE (fn, lang_c);
3312 return fn;
3313}
3314
3315/* Returns the _DECL for a library function with C linkage.
3316 We assume that such functions never throw; if this is incorrect,
3317 callers should unset TREE_NOTHROW. */
3318
3319tree
3320build_library_fn (tree name, tree type)
3321{
3322 return build_library_fn_1 (name, ERROR_MARK, type);
3323}
3324
3325/* Returns the _DECL for a library function with C++ linkage. */
3326
3327static tree
3328build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3329{
3330 tree fn = build_library_fn_1 (name, operator_code, type);
3331 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3332 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3333 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3334 set_mangled_name_for_decl (fn);
3335 return fn;
3336}
3337
3338/* Like build_library_fn, but takes a C string instead of an
3339 IDENTIFIER_NODE. */
3340
3341tree
3342build_library_fn_ptr (const char* name, tree type)
3343{
3344 return build_library_fn (get_identifier (name), type);
3345}
3346
3347/* Like build_cp_library_fn, but takes a C string instead of an
3348 IDENTIFIER_NODE. */
3349
3350tree
3351build_cp_library_fn_ptr (const char* name, tree type)
3352{
3353 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3354}
3355
3356/* Like build_library_fn, but also pushes the function so that we will
3357 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3358
3359tree
3360push_library_fn (tree name, tree type)
3361{
3362 tree fn = build_library_fn (name, type);
3363 pushdecl_top_level (fn);
3364 return fn;
3365}
3366
3367/* Like build_cp_library_fn, but also pushes the function so that it
3368 will be found by normal lookup. */
3369
3370static tree
3371push_cp_library_fn (enum tree_code operator_code, tree type)
3372{
3373 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3374 operator_code,
3375 type);
3376 pushdecl (fn);
3377 return fn;
3378}
3379
3380/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3381 a FUNCTION_TYPE. */
3382
3383tree
3384push_void_library_fn (tree name, tree parmtypes)
3385{
3386 tree type = build_function_type (void_type_node, parmtypes);
3387 return push_library_fn (name, type);
3388}
3389
3390/* Like push_library_fn, but also note that this function throws
3391 and does not return. Used for __throw_foo and the like. */
3392
3393tree
3394push_throw_library_fn (tree name, tree type)
3395{
3396 tree fn = push_library_fn (name, type);
3397 TREE_THIS_VOLATILE (fn) = 1;
3398 TREE_NOTHROW (fn) = 0;
3399 return fn;
3400}
3401\f
3402/* When we call finish_struct for an anonymous union, we create
3403 default copy constructors and such. But, an anonymous union
3404 shouldn't have such things; this function undoes the damage to the
3405 anonymous union type T.
3406
3407 (The reason that we create the synthesized methods is that we don't
3408 distinguish `union { int i; }' from `typedef union { int i; } U'.
3409 The first is an anonymous union; the second is just an ordinary
3410 union type.) */
3411
3412void
3413fixup_anonymous_aggr (tree t)
3414{
3415 tree *q;
3416
3417 /* Wipe out memory of synthesized methods. */
3418 TYPE_HAS_CONSTRUCTOR (t) = 0;
3419 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3420 TYPE_HAS_INIT_REF (t) = 0;
3421 TYPE_HAS_CONST_INIT_REF (t) = 0;
3422 TYPE_HAS_ASSIGN_REF (t) = 0;
3423 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3424
3425 /* Splice the implicitly generated functions out of the TYPE_METHODS
3426 list. */
3427 q = &TYPE_METHODS (t);
3428 while (*q)
3429 {
3430 if (DECL_ARTIFICIAL (*q))
3431 *q = TREE_CHAIN (*q);
3432 else
3433 q = &TREE_CHAIN (*q);
3434 }
3435
3436 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3437 if (TYPE_METHODS (t))
3438 error ("%Jan anonymous union cannot have function members",
3439 TYPE_MAIN_DECL (t));
3440
3441 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3442 assignment operators (because they cannot have these methods themselves).
3443 For anonymous unions this is already checked because they are not allowed
3444 in any union, otherwise we have to check it. */
3445 if (TREE_CODE (t) != UNION_TYPE)
3446 {
3447 tree field, type;
3448
3449 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3450 if (TREE_CODE (field) == FIELD_DECL)
3451 {
3452 type = TREE_TYPE (field);
3453 if (CLASS_TYPE_P (type))
3454 {
3455 if (TYPE_NEEDS_CONSTRUCTING (type))
3456 cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
3457 field);
3458 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3459 cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
3460 field);
3461 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3462 cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
3463 field);
3464 }
3465 }
3466 }
3467}
3468
3469/* Make sure that a declaration with no declarator is well-formed, i.e.
3470 just declares a tagged type or anonymous union.
3471
3472 Returns the type declared; or NULL_TREE if none. */
3473
3474tree
3475check_tag_decl (tree declspecs)
3476{
3477 int found_type = 0;
3478 int saw_friend = 0;
3479 int saw_typedef = 0;
3480 tree ob_modifier = NULL_TREE;
3481 tree link;
3482 /* If a class, struct, or enum type is declared by the DECLSPECS
3483 (i.e, if a class-specifier, enum-specifier, or non-typename
3484 elaborated-type-specifier appears in the DECLSPECS),
3485 DECLARED_TYPE is set to the corresponding type. */
3486 tree declared_type = NULL_TREE;
3487 bool error_p = false;
3488
3489 for (link = declspecs; link; link = TREE_CHAIN (link))
3490 {
3491 tree value = TREE_VALUE (link);
3492
3493 if (TYPE_P (value) || TREE_CODE (value) == TYPE_DECL
3494 || (TREE_CODE (value) == IDENTIFIER_NODE
3495 && is_typename_at_global_scope (value)))
3496 {
3497 ++found_type;
3498
3499 if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
3500 {
3501 if (! in_system_header)
3502 pedwarn ("redeclaration of C++ built-in type `%T'", value);
3503 return NULL_TREE;
3504 }
3505
3506 if (TYPE_P (value)
3507 && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
3508 || TREE_CODE (value) == ENUMERAL_TYPE))
3509 {
3510 my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
3511 declared_type = value;
3512 }
3513 }
3514 else if (value == ridpointers[(int) RID_TYPEDEF])
3515 saw_typedef = 1;
3516 else if (value == ridpointers[(int) RID_FRIEND])
3517 {
3518 if (current_class_type == NULL_TREE
3519 || current_scope () != current_class_type)
3520 ob_modifier = value;
3521 else
3522 saw_friend = 1;
3523 }
3524 else if (value == ridpointers[(int) RID_STATIC]
3525 || value == ridpointers[(int) RID_EXTERN]
3526 || value == ridpointers[(int) RID_AUTO]
3527 || value == ridpointers[(int) RID_REGISTER]
3528 || value == ridpointers[(int) RID_INLINE]
3529 || value == ridpointers[(int) RID_VIRTUAL]
3530 || value == ridpointers[(int) RID_CONST]
3531 || value == ridpointers[(int) RID_VOLATILE]
3532 || value == ridpointers[(int) RID_EXPLICIT]
3533 || value == ridpointers[(int) RID_THREAD])
3534 ob_modifier = value;
3535 else if (value == error_mark_node)
3536 error_p = true;
3537 }
3538
3539 if (found_type > 1)
3540 error ("multiple types in one declaration");
3541
3542 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3543 pedwarn ("declaration does not declare anything");
3544 /* Check for an anonymous union. */
3545 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3546 && TYPE_ANONYMOUS_P (declared_type))
3547 {
3548 /* 7/3 In a simple-declaration, the optional init-declarator-list
3549 can be omitted only when declaring a class (clause 9) or
3550 enumeration (7.2), that is, when the decl-specifier-seq contains
3551 either a class-specifier, an elaborated-type-specifier with
3552 a class-key (9.1), or an enum-specifier. In these cases and
3553 whenever a class-specifier or enum-specifier is present in the
3554 decl-specifier-seq, the identifiers in these specifiers are among
3555 the names being declared by the declaration (as class-name,
3556 enum-names, or enumerators, depending on the syntax). In such
3557 cases, and except for the declaration of an unnamed bit-field (9.6),
3558 the decl-specifier-seq shall introduce one or more names into the
3559 program, or shall redeclare a name introduced by a previous
3560 declaration. [Example:
3561 enum { }; // ill-formed
3562 typedef class { }; // ill-formed
3563 --end example] */
3564 if (saw_typedef)
3565 {
3566 error ("missing type-name in typedef-declaration");
3567 return NULL_TREE;
3568 }
3569 /* Anonymous unions are objects, so they can have specifiers. */;
3570 SET_ANON_AGGR_TYPE_P (declared_type);
3571
3572 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3573 && !in_system_header)
3574 pedwarn ("ISO C++ prohibits anonymous structs");
3575 }
3576
3577 else if (ob_modifier)
3578 {
3579 if (ob_modifier == ridpointers[(int) RID_INLINE]
3580 || ob_modifier == ridpointers[(int) RID_VIRTUAL])
3581 error ("`%D' can only be specified for functions", ob_modifier);
3582 else if (ob_modifier == ridpointers[(int) RID_FRIEND])
3583 error ("`%D' can only be specified inside a class", ob_modifier);
3584 else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
3585 error ("`%D' can only be specified for constructors",
3586 ob_modifier);
3587 else
3588 error ("`%D' can only be specified for objects and functions",
3589 ob_modifier);
3590 }
3591
3592 return declared_type;
3593}
3594
3595/* Called when a declaration is seen that contains no names to declare.
3596 If its type is a reference to a structure, union or enum inherited
3597 from a containing scope, shadow that tag name for the current scope
3598 with a forward reference.
3599 If its type defines a new named structure or union
3600 or defines an enum, it is valid but we need not do anything here.
3601 Otherwise, it is an error.
3602
3603 C++: may have to grok the declspecs to learn about static,
3604 complain for anonymous unions.
3605
3606 Returns the TYPE declared -- or NULL_TREE if none. */
3607
3608tree
3609shadow_tag (tree declspecs)
3610{
3611 tree t = check_tag_decl (declspecs);
3612
3613 if (!t)
3614 return NULL_TREE;
3615
3616 maybe_process_partial_specialization (t);
3617
3618 /* This is where the variables in an anonymous union are
3619 declared. An anonymous union declaration looks like:
3620 union { ... } ;
3621 because there is no declarator after the union, the parser
3622 sends that declaration here. */
3623 if (ANON_AGGR_TYPE_P (t))
3624 {
3625 fixup_anonymous_aggr (t);
3626
3627 if (TYPE_FIELDS (t))
3628 {
3629 tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
3630 NULL);
3631 finish_anon_union (decl);
3632 }
3633 }
3634
3635 return t;
3636}
3637\f
3638/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3639
3640tree
3641groktypename (tree typename)
3642{
3643 tree specs, attrs;
3644 tree type;
3645 if (TREE_CODE (typename) != TREE_LIST)
3646 return typename;
3647 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
3648 type = grokdeclarator (TREE_VALUE (typename), specs,
3649 TYPENAME, 0, &attrs);
3650 if (attrs)
3651 cplus_decl_attributes (&type, attrs, 0);
3652 return type;
3653}
3654
3655/* Decode a declarator in an ordinary declaration or data definition.
3656 This is called as soon as the type information and variable name
3657 have been parsed, before parsing the initializer if any.
3658 Here we create the ..._DECL node, fill in its type,
3659 and put it on the list of decls for the current context.
3660 The ..._DECL node is returned as the value.
3661
3662 Exception: for arrays where the length is not specified,
3663 the type is left null, to be filled in by `cp_finish_decl'.
3664
3665 Function definitions do not come here; they go to start_function
3666 instead. However, external and forward declarations of functions
3667 do go through here. Structure field declarations are done by
3668 grokfield and not through here. */
3669
3670tree
3671start_decl (tree declarator,
3672 tree declspecs,
3673 int initialized,
3674 tree attributes,
3675 tree prefix_attributes)
3676{
3677 tree decl;
3678 tree type, tem;
3679 tree context;
3680
3681 /* This should only be done once on the top most decl. */
3682 if (have_extern_spec)
3683 {
3684 declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
3685 declspecs);
3686 have_extern_spec = false;
3687 }
3688
3689 /* An object declared as __attribute__((deprecated)) suppresses
3690 warnings of uses of other deprecated items. */
3691 if (lookup_attribute ("deprecated", attributes))
3692 deprecated_state = DEPRECATED_SUPPRESS;
3693
3694 attributes = chainon (attributes, prefix_attributes);
3695
3696 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
3697 &attributes);
3698
3699 deprecated_state = DEPRECATED_NORMAL;
3700
3701 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
1c1138ce 3702 return error_mark_node;
003757ed
MD
3703
3704 type = TREE_TYPE (decl);
3705
3706 if (type == error_mark_node)
1c1138ce 3707 return error_mark_node;
003757ed
MD
3708
3709 context = DECL_CONTEXT (decl);
3710
3711 if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
3712 && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
3713 {
3714 /* When parsing the initializer, lookup should use the object's
3715 namespace. */
3716 push_decl_namespace (context);
3717 }
3718
3719 /* We are only interested in class contexts, later. */
3720 if (context && TREE_CODE (context) == NAMESPACE_DECL)
3721 context = NULL_TREE;
3722
3723 if (initialized)
3724 /* Is it valid for this decl to have an initializer at all?
3725 If not, set INITIALIZED to zero, which will indirectly
3726 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3727 switch (TREE_CODE (decl))
3728 {
3729 case TYPE_DECL:
3730 error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
3731 initialized = 0;
3732 break;
3733
3734 case FUNCTION_DECL:
3735 error ("function `%#D' is initialized like a variable", decl);
3736 initialized = 0;
3737 break;
3738
3739 default:
3740 break;
3741 }
3742
3743 if (initialized)
3744 {
3745 if (! toplevel_bindings_p ()
3746 && DECL_EXTERNAL (decl))
3747 warning ("declaration of `%#D' has `extern' and is initialized",
3748 decl);
3749 DECL_EXTERNAL (decl) = 0;
3750 if (toplevel_bindings_p ())
3751 TREE_STATIC (decl) = 1;
3752
3753 /* Tell `pushdecl' this is an initialized decl
3754 even though we don't yet have the initializer expression.
3755 Also tell `cp_finish_decl' it may store the real initializer. */
3756 DECL_INITIAL (decl) = error_mark_node;
3757 }
3758
3759 /* Set attributes here so if duplicate decl, will have proper attributes. */
3760 cplus_decl_attributes (&decl, attributes, 0);
3761
3762 /* If #pragma weak was used, mark the decl weak now. */
3763 if (global_scope_p (current_binding_level))
3764 maybe_apply_pragma_weak (decl);
3765
3766 if (TREE_CODE (decl) == FUNCTION_DECL
3767 && DECL_DECLARED_INLINE_P (decl)
3768 && DECL_UNINLINABLE (decl)
3769 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3770 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
3771
3772 if (context && COMPLETE_TYPE_P (complete_type (context)))
3773 {
3774 push_nested_class (context);
3775
3776 if (TREE_CODE (decl) == VAR_DECL)
3777 {
3778 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
3779 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
3780 error ("`%#D' is not a static member of `%#T'", decl, context);
3781 else