1/* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2017 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21
22/* Process declarations and symbol lookup for C++ front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
25
26/* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
28
29#include "config.h"
30#include "system.h"
31#include "coretypes.h"
32#include "target.h"
33#include "c-family/c-target.h"
34#include "cp-tree.h"
35#include "timevar.h"
36#include "stringpool.h"
37#include "cgraph.h"
38#include "stor-layout.h"
39#include "varasm.h"
40#include "attribs.h"
41#include "flags.h"
42#include "tree-iterator.h"
43#include "decl.h"
44#include "intl.h"
45#include "toplev.h"
46#include "c-family/c-objc.h"
47#include "c-family/c-pragma.h"
48#include "c-family/c-ubsan.h"
49#include "debug.h"
50#include "plugin.h"
51#include "builtins.h"
52#include "gimplify.h"
53#include "asan.h"
54
55/* Possible cases of bad specifiers type used by bad_specifiers. */
56enum bad_spec_place {
57 BSP_VAR, /* variable */
58 BSP_PARM, /* parameter */
59 BSP_TYPE, /* type */
60 BSP_FIELD /* field */
61};
62
63static const char *redeclaration_error_message (tree, tree);
64
65static int decl_jump_unsafe (tree);
66static void require_complete_types_for_parms (tree);
67static void push_local_name (tree);
68static tree grok_reference_init (tree, tree, tree, int);
69static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
70 int, int, int, bool, int, tree);
71static int check_static_variable_definition (tree, tree);
72static void record_unknown_type (tree, const char *);
73static tree builtin_function_1 (tree, tree, bool);
74static int member_function_or_else (tree, tree, enum overload_flags);
75static void check_for_uninitialized_const_var (tree);
76static tree local_variable_p_walkfn (tree *, int *, void *);
77static const char *tag_name (enum tag_types);
78static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
79static void maybe_deduce_size_from_array_init (tree, tree);
80static void layout_var_decl (tree);
81static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
82static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
83static void save_function_data (tree);
84static void copy_type_enum (tree , tree);
85static void check_function_type (tree, tree);
86static void finish_constructor_body (void);
87static void begin_destructor_body (void);
88static void finish_destructor_body (void);
89static void record_key_method_defined (tree);
90static tree create_array_type_for_decl (tree, tree, tree);
91static tree get_atexit_node (void);
92static tree get_dso_handle_node (void);
93static tree start_cleanup_fn (void);
94static void end_cleanup_fn (void);
95static tree cp_make_fname_decl (location_t, tree, int);
96static void initialize_predefined_identifiers (void);
97static tree check_special_function_return_type
98 (special_function_kind, tree, tree, int, const location_t*);
99static tree push_cp_library_fn (enum tree_code, tree, int);
100static tree build_cp_library_fn (tree, enum tree_code, tree, int);
101static void store_parm_decls (tree);
102static void initialize_local_var (tree, tree);
103static void expand_static_init (tree, tree);
104
105/* The following symbols are subsumed in the cp_global_trees array, and
106 listed here individually for documentation purposes.
107
108 C++ extensions
109 tree wchar_decl_node;
110
111 tree vtable_entry_type;
112 tree delta_type_node;
113 tree __t_desc_type_node;
114
115 tree class_type_node;
116 tree unknown_type_node;
117
118 Array type `vtable_entry_type[]'
119
120 tree vtbl_type_node;
121 tree vtbl_ptr_type_node;
122
123 Namespaces,
124
125 tree std_node;
126 tree abi_node;
127
128 A FUNCTION_DECL which can call `abort'. Not necessarily the
129 one that the user will declare, but sufficient to be called
130 by routines that want to abort the program.
131
132 tree abort_fndecl;
133
134 Used by RTTI
135 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
136 tree tinfo_var_id; */
137
138tree cp_global_trees[CPTI_MAX];
139
140#define local_names cp_function_chain->x_local_names
141
142/* A list of objects which have constructors or destructors
143 which reside in the global scope. The decl is stored in
144 the TREE_VALUE slot and the initializer is stored
145 in the TREE_PURPOSE slot. */
146tree static_aggregates;
147
148/* Like static_aggregates, but for thread_local variables. */
149tree tls_aggregates;
150
151/* -- end of C++ */
152
153/* A node for the integer constant 2. */
154
155tree integer_two_node;
156
157/* vector of static decls. */
158vec<tree, va_gc> *static_decls;
159
160/* vector of keyed classes. */
161vec<tree, va_gc> *keyed_classes;
162
163/* Used only for jumps to as-yet undefined labels, since jumps to
164 defined labels can have their validity checked immediately. */
165
166struct GTY((chain_next ("%h.next"))) named_label_use_entry {
167 struct named_label_use_entry *next;
168 /* The binding level to which this entry is *currently* attached.
169 This is initially the binding level in which the goto appeared,
170 but is modified as scopes are closed. */
171 cp_binding_level *binding_level;
172 /* The head of the names list that was current when the goto appeared,
173 or the inner scope popped. These are the decls that will *not* be
174 skipped when jumping to the label. */
175 tree names_in_scope;
176 /* The location of the goto, for error reporting. */
177 location_t o_goto_locus;
178 /* True if an OpenMP structured block scope has been closed since
179 the goto appeared. This means that the branch from the label will
180 illegally exit an OpenMP scope. */
181 bool in_omp_scope;
182};
183
184/* A list of all LABEL_DECLs in the function that have names. Here so
185 we can clear out their names' definitions at the end of the
186 function, and so we can check the validity of jumps to these labels. */
187
188struct GTY((for_user)) named_label_entry {
189
190 tree name; /* Name of decl. */
191
192 tree label_decl; /* LABEL_DECL, unless deleted local label. */
193
194 named_label_entry *outer; /* Outer shadowed chain. */
195
196 /* The binding level to which the label is *currently* attached.
197 This is initially set to the binding level in which the label
198 is defined, but is modified as scopes are closed. */
199 cp_binding_level *binding_level;
200
201 /* The head of the names list that was current when the label was
202 defined, or the inner scope popped. These are the decls that will
203 be skipped when jumping to the label. */
204 tree names_in_scope;
205
206 /* A vector of all decls from all binding levels that would be
207 crossed by a backward branch to the label. */
208 vec<tree, va_gc> *bad_decls;
209
210 /* A list of uses of the label, before the label is defined. */
211 named_label_use_entry *uses;
212
213 /* The following bits are set after the label is defined, and are
214 updated as scopes are popped. They indicate that a jump to the
215 label will illegally enter a scope of the given flavor. */
216 bool in_try_scope;
217 bool in_catch_scope;
218 bool in_omp_scope;
219 bool in_transaction_scope;
220 bool in_constexpr_if;
221};
222
223#define named_labels cp_function_chain->x_named_labels
224
225/* The number of function bodies which we are currently processing.
226 (Zero if we are at namespace scope, one inside the body of a
227 function, two inside the body of a function in a local class, etc.) */
228int function_depth;
229
230/* Whether the exception-specifier is part of a function type (i.e. C++17). */
231bool flag_noexcept_type;
232
233/* States indicating how grokdeclarator() should handle declspecs marked
234 with __attribute__((deprecated)). An object declared as
235 __attribute__((deprecated)) suppresses warnings of uses of other
236 deprecated items. */
237enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
238
239
240/* A list of VAR_DECLs whose type was incomplete at the time the
241 variable was declared. */
242
243struct GTY(()) incomplete_var {
244 tree decl;
245 tree incomplete_type;
246};
247
248
249static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
250
251/* Returns the kind of template specialization we are currently
252 processing, given that it's declaration contained N_CLASS_SCOPES
253 explicit scope qualifications. */
254
255tmpl_spec_kind
256current_tmpl_spec_kind (int n_class_scopes)
257{
258 int n_template_parm_scopes = 0;
259 int seen_specialization_p = 0;
260 int innermost_specialization_p = 0;
261 cp_binding_level *b;
262
263 /* Scan through the template parameter scopes. */
264 for (b = current_binding_level;
265 b->kind == sk_template_parms;
266 b = b->level_chain)
267 {
268 /* If we see a specialization scope inside a parameter scope,
269 then something is wrong. That corresponds to a declaration
270 like:
271
272 template <class T> template <> ...
273
274 which is always invalid since [temp.expl.spec] forbids the
275 specialization of a class member template if the enclosing
276 class templates are not explicitly specialized as well. */
277 if (b->explicit_spec_p)
278 {
279 if (n_template_parm_scopes == 0)
280 innermost_specialization_p = 1;
281 else
282 seen_specialization_p = 1;
283 }
284 else if (seen_specialization_p == 1)
285 return tsk_invalid_member_spec;
286
287 ++n_template_parm_scopes;
288 }
289
290 /* Handle explicit instantiations. */
291 if (processing_explicit_instantiation)
292 {
293 if (n_template_parm_scopes != 0)
294 /* We've seen a template parameter list during an explicit
295 instantiation. For example:
296
297 template <class T> template void f(int);
298
299 This is erroneous. */
300 return tsk_invalid_expl_inst;
301 else
302 return tsk_expl_inst;
303 }
304
305 if (n_template_parm_scopes < n_class_scopes)
306 /* We've not seen enough template headers to match all the
307 specialized classes present. For example:
308
309 template <class T> void R<T>::S<T>::f(int);
310
311 This is invalid; there needs to be one set of template
312 parameters for each class. */
313 return tsk_insufficient_parms;
314 else if (n_template_parm_scopes == n_class_scopes)
315 /* We're processing a non-template declaration (even though it may
316 be a member of a template class.) For example:
317
318 template <class T> void S<T>::f(int);
319
320 The `class T' matches the `S<T>', leaving no template headers
321 corresponding to the `f'. */
322 return tsk_none;
323 else if (n_template_parm_scopes > n_class_scopes + 1)
324 /* We've got too many template headers. For example:
325
326 template <> template <class T> void f (T);
327
328 There need to be more enclosing classes. */
329 return tsk_excessive_parms;
330 else
331 /* This must be a template. It's of the form:
332
333 template <class T> template <class U> void S<T>::f(U);
334
335 This is a specialization if the innermost level was a
336 specialization; otherwise it's just a definition of the
337 template. */
338 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
339}
340
341/* Exit the current scope. */
342
343void
344finish_scope (void)
345{
346 poplevel (0, 0, 0);
347}
348
349/* When a label goes out of scope, check to see if that label was used
350 in a valid manner, and issue any appropriate warnings or errors. */
351
352static void
353check_label_used (tree label)
354{
355 if (!processing_template_decl)
356 {
357 if (DECL_INITIAL (label) == NULL_TREE)
358 {
359 location_t location;
360
361 error ("label %q+D used but not defined", label);
362 location = input_location;
363 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
364 /* Avoid crashing later. */
365 define_label (location, DECL_NAME (label));
366 }
367 else
368 warn_for_unused_label (label);
369 }
370}
371
372/* Helper function to sort named label entries in a vector by DECL_UID. */
373
374static int
375sort_labels (const void *a, const void *b)
376{
377 tree label1 = *(tree const *) a;
378 tree label2 = *(tree const *) b;
379
380 /* DECL_UIDs can never be equal. */
381 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1;
382}
383
384/* At the end of a function, all labels declared within the function
385 go out of scope. BLOCK is the top-level block for the
386 function. */
387
388static void
389pop_labels (tree block)
390{
391 if (!named_labels)
392 return;
393
394 /* We need to add the labels to the block chain, so debug
395 information is emitted. But, we want the order to be stable so
396 need to sort them first. Otherwise the debug output could be
397 randomly ordered. I guess it's mostly stable, unless the hash
398 table implementation changes. */
399 auto_vec<tree, 32> labels (named_labels->elements ());
400 hash_table<named_label_hash>::iterator end (named_labels->end ());
401 for (hash_table<named_label_hash>::iterator iter
402 (named_labels->begin ()); iter != end; ++iter)
403 {
404 named_label_entry *ent = *iter;
405
406 gcc_checking_assert (!ent->outer);
407 if (ent->label_decl)
408 labels.quick_push (ent->label_decl);
409 ggc_free (ent);
410 }
411 named_labels = NULL;
412 labels.qsort (sort_labels);
413
414 while (labels.length ())
415 {
416 tree label = labels.pop ();
417
418 DECL_CHAIN (label) = BLOCK_VARS (block);
419 BLOCK_VARS (block) = label;
420
421 check_label_used (label);
422 }
423}
424
425/* At the end of a block with local labels, restore the outer definition. */
426
427static void
428pop_local_label (tree id, tree label)
429{
430 check_label_used (label);
431 named_label_entry **slot = named_labels->find_slot_with_hash
432 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT);
433 named_label_entry *ent = *slot;
434
435 if (ent->outer)
436 ent = ent->outer;
437 else
438 {
439 ent = ggc_cleared_alloc<named_label_entry> ();
440 ent->name = id;
441 }
442 *slot = ent;
443}
444
445/* The following two routines are used to interface to Objective-C++.
446 The binding level is purposely treated as an opaque type. */
447
448void *
449objc_get_current_scope (void)
450{
451 return current_binding_level;
452}
453
454/* The following routine is used by the NeXT-style SJLJ exceptions;
455 variables get marked 'volatile' so as to not be clobbered by
456 _setjmp()/_longjmp() calls. All variables in the current scope,
457 as well as parent scopes up to (but not including) ENCLOSING_BLK
458 shall be thusly marked. */
459
460void
461objc_mark_locals_volatile (void *enclosing_blk)
462{
463 cp_binding_level *scope;
464
465 for (scope = current_binding_level;
466 scope && scope != enclosing_blk;
467 scope = scope->level_chain)
468 {
469 tree decl;
470
471 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
472 objc_volatilize_decl (decl);
473
474 /* Do not climb up past the current function. */
475 if (scope->kind == sk_function_parms)
476 break;
477 }
478}
479
480/* True if B is the level for the condition of a constexpr if. */
481
482static bool
483level_for_constexpr_if (cp_binding_level *b)
484{
485 return (b->kind == sk_cond && b->this_entity
486 && TREE_CODE (b->this_entity) == IF_STMT
487 && IF_STMT_CONSTEXPR_P (b->this_entity));
488}
489
490/* Update data for defined and undefined labels when leaving a scope. */
491
492int
493poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
494{
495 named_label_entry *ent = *slot;
496 cp_binding_level *obl = bl->level_chain;
497
498 if (ent->binding_level == bl)
499 {
500 tree decl;
501
502 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
503 TREE_LISTs representing OVERLOADs, so be careful. */
504 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
505 ? DECL_CHAIN (decl)
506 : TREE_CHAIN (decl)))
507 if (decl_jump_unsafe (decl))
508 vec_safe_push (ent->bad_decls, decl);
509
510 ent->binding_level = obl;
511 ent->names_in_scope = obl->names;
512 switch (bl->kind)
513 {
514 case sk_try:
515 ent->in_try_scope = true;
516 break;
517 case sk_catch:
518 ent->in_catch_scope = true;
519 break;
520 case sk_omp:
521 ent->in_omp_scope = true;
522 break;
523 case sk_transaction:
524 ent->in_transaction_scope = true;
525 break;
526 case sk_block:
527 if (level_for_constexpr_if (bl->level_chain))
528 ent->in_constexpr_if = true;
529 break;
530 default:
531 break;
532 }
533 }
534 else if (ent->uses)
535 {
536 struct named_label_use_entry *use;
537
538 for (use = ent->uses; use ; use = use->next)
539 if (use->binding_level == bl)
540 {
541 use->binding_level = obl;
542 use->names_in_scope = obl->names;
543 if (bl->kind == sk_omp)
544 use->in_omp_scope = true;
545 }
546 }
547
548 return 1;
549}
550
551/* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
552 when errors were reported, except for -Werror-unused-but-set-*. */
553static int unused_but_set_errorcount;
554
555/* Exit a binding level.
556 Pop the level off, and restore the state of the identifier-decl mappings
557 that were in effect when this level was entered.
558
559 If KEEP == 1, this level had explicit declarations, so
560 and create a "block" (a BLOCK node) for the level
561 to record its declarations and subblocks for symbol table output.
562
563 If FUNCTIONBODY is nonzero, this level is the body of a function,
564 so create a block as if KEEP were set and also clear out all
565 label names.
566
567 If REVERSE is nonzero, reverse the order of decls before putting
568 them into the BLOCK. */
569
570tree
571poplevel (int keep, int reverse, int functionbody)
572{
573 tree link;
574 /* The chain of decls was accumulated in reverse order.
575 Put it into forward order, just for cleanliness. */
576 tree decls;
577 tree subblocks;
578 tree block;
579 tree decl;
580 int leaving_for_scope;
581 scope_kind kind;
582 unsigned ix;
583
584 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
585 restart:
586
587 block = NULL_TREE;
588
589 gcc_assert (current_binding_level->kind != sk_class
590 && current_binding_level->kind != sk_namespace);
591
592 if (current_binding_level->kind == sk_cleanup)
593 functionbody = 0;
594 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
595
596 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
597
598 /* We used to use KEEP == 2 to indicate that the new block should go
599 at the beginning of the list of blocks at this binding level,
600 rather than the end. This hack is no longer used. */
601 gcc_assert (keep == 0 || keep == 1);
602
603 if (current_binding_level->keep)
604 keep = 1;
605
606 /* Any uses of undefined labels, and any defined labels, now operate
607 under constraints of next binding contour. */
608 if (cfun && !functionbody && named_labels)
609 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
610 (current_binding_level);
611
612 /* Get the decls in the order they were written.
613 Usually current_binding_level->names is in reverse order.
614 But parameter decls were previously put in forward order. */
615
616 decls = current_binding_level->names;
617 if (reverse)
618 {
619 decls = nreverse (decls);
620 current_binding_level->names = decls;
621 }
622
623 /* If there were any declarations or structure tags in that level,
624 or if this level is a function body,
625 create a BLOCK to record them for the life of this function. */
626 block = NULL_TREE;
627 /* Avoid function body block if possible. */
628 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
629 keep = 0;
630 else if (keep == 1 || functionbody)
631 block = make_node (BLOCK);
632 if (block != NULL_TREE)
633 {
634 BLOCK_VARS (block) = decls;
635 BLOCK_SUBBLOCKS (block) = subblocks;
636 }
637
638 /* In each subblock, record that this is its superior. */
639 if (keep >= 0)
640 for (link = subblocks; link; link = BLOCK_CHAIN (link))
641 BLOCK_SUPERCONTEXT (link) = block;
642
643 /* We still support the old for-scope rules, whereby the variables
644 in a init statement were in scope after the for-statement ended.
645 We only use the new rules if flag_new_for_scope is nonzero. */
646 leaving_for_scope
647 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
648
649 /* Before we remove the declarations first check for unused variables. */
650 if ((warn_unused_variable || warn_unused_but_set_variable)
651 && current_binding_level->kind != sk_template_parms
652 && !processing_template_decl)
653 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d))
654 {
655 /* There are cases where D itself is a TREE_LIST. See in
656 push_local_binding where the list of decls returned by
657 getdecls is built. */
658 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
659
660 tree type = TREE_TYPE (decl);
661 if (VAR_P (decl)
662 && (! TREE_USED (decl) || !DECL_READ_P (decl))
663 && ! DECL_IN_SYSTEM_HEADER (decl)
664 /* For structured bindings, consider only real variables, not
665 subobjects. */
666 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl)
667 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)))
668 && type != error_mark_node
669 && (!CLASS_TYPE_P (type)
670 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
671 || lookup_attribute ("warn_unused",
672 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
673 {
674 if (! TREE_USED (decl))
675 {
676 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
677 warning_at (DECL_SOURCE_LOCATION (decl),
678 OPT_Wunused_variable,
679 "unused structured binding declaration");
680 else
681 warning_at (DECL_SOURCE_LOCATION (decl),
682 OPT_Wunused_variable, "unused variable %qD", decl);
683 }
684 else if (DECL_CONTEXT (decl) == current_function_decl
685 // For -Wunused-but-set-variable leave references alone.
686 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
687 && errorcount == unused_but_set_errorcount)
688 {
689 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl))
690 warning_at (DECL_SOURCE_LOCATION (decl),
691 OPT_Wunused_but_set_variable, "structured "
692 "binding declaration set but not used");
693 else
694 warning_at (DECL_SOURCE_LOCATION (decl),
695 OPT_Wunused_but_set_variable,
696 "variable %qD set but not used", decl);
697 unused_but_set_errorcount = errorcount;
698 }
699 }
700 }
701
702 /* Remove declarations for all the DECLs in this level. */
703 for (link = decls; link; link = TREE_CHAIN (link))
704 {
705 decl = TREE_CODE (link) == TREE_LIST ? TREE_VALUE (link) : link;
706 tree name = OVL_NAME (decl);
707
708 if (leaving_for_scope && VAR_P (decl)
709 /* It's hard to make this ARM compatibility hack play nicely with
710 lambdas, and it really isn't necessary in C++11 mode. */
711 && cxx_dialect < cxx11
712 && name)
713 {
714 cxx_binding *ob = outer_binding (name,
715 IDENTIFIER_BINDING (name),
716 /*class_p=*/true);
717 tree ns_binding = NULL_TREE;
718 if (!ob)
719 ns_binding = get_namespace_binding (current_namespace, name);
720
721 if (ob && ob->scope == current_binding_level->level_chain)
722 /* We have something like:
723
724 int i;
725 for (int i; ;);
726
727 and we are leaving the `for' scope. There's no reason to
728 keep the binding of the inner `i' in this case. */
729 ;
730 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
731 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
732 /* Here, we have something like:
733
734 typedef int I;
735
736 void f () {
737 for (int I; ;);
738 }
739
740 We must pop the for-scope binding so we know what's a
741 type and what isn't. */
742 ;
743 else
744 {
745 /* Mark this VAR_DECL as dead so that we can tell we left it
746 there only for backward compatibility. */
747 DECL_DEAD_FOR_LOCAL (link) = 1;
748
749 /* Keep track of what should have happened when we
750 popped the binding. */
751 if (ob && ob->value)
752 {
753 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
754 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
755 }
756
757 /* Add it to the list of dead variables in the next
758 outermost binding to that we can remove these when we
759 leave that binding. */
760 vec_safe_push (
761 current_binding_level->level_chain->dead_vars_from_for,
762 link);
763
764 /* Although we don't pop the cxx_binding, we do clear
765 its SCOPE since the scope is going away now. */
766 IDENTIFIER_BINDING (name)->scope
767 = current_binding_level->level_chain;
768
769 /* Don't remove the binding. */
770 name = NULL_TREE;
771 }
772 }
773 /* Remove the binding. */
774 if (TREE_CODE (decl) == LABEL_DECL)
775 pop_local_label (name, decl);
776 else
777 pop_local_binding (name, decl);
778 }
779
780 /* Remove declarations for any `for' variables from inner scopes
781 that we kept around. */
782 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
783 ix, decl)
784 pop_local_binding (DECL_NAME (decl), decl);
785
786 /* Restore the IDENTIFIER_TYPE_VALUEs. */
787 for (link = current_binding_level->type_shadowed;
788 link; link = TREE_CHAIN (link))
789 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
790
791 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
792 list if a `using' declaration put them there. The debugging
793 back ends won't understand OVERLOAD, so we remove them here.
794 Because the BLOCK_VARS are (temporarily) shared with
795 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
796 popped all the bindings. Also remove undeduced 'auto' decls,
797 which LTO doesn't understand, and can't have been used by anything. */
798 if (block)
799 {
800 tree* d;
801
802 for (d = &BLOCK_VARS (block); *d; )
803 {
804 if (TREE_CODE (*d) == TREE_LIST
805 || (!processing_template_decl
806 && undeduced_auto_decl (*d)))
807 *d = TREE_CHAIN (*d);
808 else
809 d = &DECL_CHAIN (*d);
810 }
811 }
812
813 /* If the level being exited is the top level of a function,
814 check over all the labels. */
815 if (functionbody)
816 {
817 if (block)
818 {
819 /* Since this is the top level block of a function, the vars are
820 the function's parameters. Don't leave them in the BLOCK
821 because they are found in the FUNCTION_DECL instead. */
822 BLOCK_VARS (block) = 0;
823 pop_labels (block);
824 }
825 else
826 pop_labels (subblocks);
827 }
828
829 kind = current_binding_level->kind;
830 if (kind == sk_cleanup)
831 {
832 tree stmt;
833
834 /* If this is a temporary binding created for a cleanup, then we'll
835 have pushed a statement list level. Pop that, create a new
836 BIND_EXPR for the block, and insert it into the stream. */
837 stmt = pop_stmt_list (current_binding_level->statement_list);
838 stmt = c_build_bind_expr (input_location, block, stmt);
839 add_stmt (stmt);
840 }
841
842 leave_scope ();
843 if (functionbody)
844 {
845 /* The current function is being defined, so its DECL_INITIAL
846 should be error_mark_node. */
847 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
848 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
849 if (subblocks)
850 {
851 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
852 {
853 if (BLOCK_SUBBLOCKS (subblocks))
854 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
855 }
856 else
857 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
858 }
859 }
860 else if (block)
861 current_binding_level->blocks
862 = block_chainon (current_binding_level->blocks, block);
863
864 /* If we did not make a block for the level just exited,
865 any blocks made for inner levels
866 (since they cannot be recorded as subblocks in that level)
867 must be carried forward so they will later become subblocks
868 of something else. */
869 else if (subblocks)
870 current_binding_level->blocks
871 = block_chainon (current_binding_level->blocks, subblocks);
872
873 /* Each and every BLOCK node created here in `poplevel' is important
874 (e.g. for proper debugging information) so if we created one
875 earlier, mark it as "used". */
876 if (block)
877 TREE_USED (block) = 1;
878
879 /* All temporary bindings created for cleanups are popped silently. */
880 if (kind == sk_cleanup)
881 goto restart;
882
883 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
884 return block;
885}
886
887/* Call wrapup_globals_declarations for the globals in NAMESPACE. */
888/* Diagnose odr-used extern inline variables without definitions
889 in the current TU. */
890
891int
892wrapup_namespace_globals ()
893{
894 if (vec<tree, va_gc> *statics = static_decls)
895 {
896 tree decl;
897 unsigned int i;
898 FOR_EACH_VEC_ELT (*statics, i, decl)
899 {
900 if (warn_unused_function
901 && TREE_CODE (decl) == FUNCTION_DECL
902 && DECL_INITIAL (decl) == 0
903 && DECL_EXTERNAL (decl)
904 && !TREE_PUBLIC (decl)
905 && !DECL_ARTIFICIAL (decl)
906 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl)
907 && !TREE_NO_WARNING (decl))
908 warning_at (DECL_SOURCE_LOCATION (decl),
909 OPT_Wunused_function,
910 "%qF declared %<static%> but never defined", decl);
911
912 if (VAR_P (decl)
913 && DECL_EXTERNAL (decl)
914 && DECL_INLINE_VAR_P (decl)
915 && DECL_ODR_USED (decl))
916 error_at (DECL_SOURCE_LOCATION (decl),
917 "odr-used inline variable %qD is not defined", decl);
918 }
919
920 /* Clear out the list, so we don't rescan next time. */
921 static_decls = NULL;
922
923 /* Write out any globals that need to be output. */
924 return wrapup_global_declarations (statics->address (),
925 statics->length ());
926 }
927 return 0;
928}
929
930/* In C++, you don't have to write `struct S' to refer to `S'; you
931 can just use `S'. We accomplish this by creating a TYPE_DECL as
932 if the user had written `typedef struct S S'. Create and return
933 the TYPE_DECL for TYPE. */
934
935tree
936create_implicit_typedef (tree name, tree type)
937{
938 tree decl;
939
940 decl = build_decl (input_location, TYPE_DECL, name, type);
941 DECL_ARTIFICIAL (decl) = 1;
942 /* There are other implicit type declarations, like the one *within*
943 a class that allows you to write `S::S'. We must distinguish
944 amongst these. */
945 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
946 TYPE_NAME (type) = decl;
947 TYPE_STUB_DECL (type) = decl;
948
949 return decl;
950}
951
952/* Remember a local name for name-mangling purposes. */
953
954static void
955push_local_name (tree decl)
956{
957 size_t i, nelts;
958 tree t, name;
959
960 timevar_start (TV_NAME_LOOKUP);
961
962 name = DECL_NAME (decl);
963
964 nelts = vec_safe_length (local_names);
965 for (i = 0; i < nelts; i++)
966 {
967 t = (*local_names)[i];
968 if (DECL_NAME (t) == name)
969 {
970 retrofit_lang_decl (decl);
971 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
972 if (DECL_DISCRIMINATOR_SET_P (t))
973 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
974 else
975 DECL_DISCRIMINATOR (decl) = 1;
976
977 (*local_names)[i] = decl;
978 timevar_stop (TV_NAME_LOOKUP);
979 return;
980 }
981 }
982
983 vec_safe_push (local_names, decl);
984 timevar_stop (TV_NAME_LOOKUP);
985}
986
987/* Subroutine of duplicate_decls: return truthvalue of whether
988 or not types of these decls match.
989
990 For C++, we must compare the parameter list so that `int' can match
991 `int&' in a parameter position, but `int&' is not confused with
992 `const int&'. */
993
994int
995decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */)
996{
997 int types_match;
998
999 if (newdecl == olddecl)
1000 return 1;
1001
1002 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1003 /* If the two DECLs are not even the same kind of thing, we're not
1004 interested in their types. */
1005 return 0;
1006
1007 gcc_assert (DECL_P (newdecl));
1008
1009 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1010 {
1011 tree f1 = TREE_TYPE (newdecl);
1012 tree f2 = TREE_TYPE (olddecl);
1013 tree p1 = TYPE_ARG_TYPES (f1);
1014 tree p2 = TYPE_ARG_TYPES (f2);
1015 tree r2;
1016
1017 /* Specializations of different templates are different functions
1018 even if they have the same type. */
1019 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1020 ? DECL_TI_TEMPLATE (newdecl)
1021 : NULL_TREE);
1022 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1023 ? DECL_TI_TEMPLATE (olddecl)
1024 : NULL_TREE);
1025 if (t1 != t2)
1026 return 0;
1027
1028 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1029 && ! (DECL_EXTERN_C_P (newdecl)
1030 && DECL_EXTERN_C_P (olddecl)))
1031 return 0;
1032
1033 /* A new declaration doesn't match a built-in one unless it
1034 is also extern "C". */
1035 if (DECL_IS_BUILTIN (olddecl)
1036 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1037 return 0;
1038
1039 if (TREE_CODE (f1) != TREE_CODE (f2))
1040 return 0;
1041
1042 /* A declaration with deduced return type should use its pre-deduction
1043 type for declaration matching. */
1044 r2 = fndecl_declared_return_type (olddecl);
1045
1046 if (same_type_p (TREE_TYPE (f1), r2))
1047 {
1048 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1049 && (DECL_BUILT_IN (olddecl)
1050#ifndef NO_IMPLICIT_EXTERN_C
1051 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1052 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1053#endif
1054 ))
1055 {
1056 types_match = self_promoting_args_p (p1);
1057 if (p1 == void_list_node)
1058 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1059 }
1060#ifndef NO_IMPLICIT_EXTERN_C
1061 else if (!prototype_p (f1)
1062 && (DECL_EXTERN_C_P (olddecl)
1063 && DECL_IN_SYSTEM_HEADER (olddecl)
1064 && !DECL_CLASS_SCOPE_P (olddecl))
1065 && (DECL_EXTERN_C_P (newdecl)
1066 && DECL_IN_SYSTEM_HEADER (newdecl)
1067 && !DECL_CLASS_SCOPE_P (newdecl)))
1068 {
1069 types_match = self_promoting_args_p (p2);
1070 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1071 }
1072#endif
1073 else
1074 types_match =
1075 compparms (p1, p2)
1076 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1077 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1078 || comp_type_attributes (TREE_TYPE (newdecl),
1079 TREE_TYPE (olddecl)) != 0);
1080 }
1081 else
1082 types_match = 0;
1083
1084 /* The decls dont match if they correspond to two different versions
1085 of the same function. Disallow extern "C" functions to be
1086 versions for now. */
1087 if (types_match
1088 && !DECL_EXTERN_C_P (newdecl)
1089 && !DECL_EXTERN_C_P (olddecl)
1090 && record_versions
1091 && maybe_version_functions (newdecl, olddecl))
1092 return 0;
1093 }
1094 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1095 {
1096 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1097 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1098
1099 if (TREE_CODE (newres) != TREE_CODE (oldres))
1100 return 0;
1101
1102 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1103 DECL_TEMPLATE_PARMS (olddecl)))
1104 return 0;
1105
1106 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1107 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1108 && equivalently_constrained (olddecl, newdecl));
1109 else
1110 // We don't need to check equivalently_constrained for variable and
1111 // function templates because we check it on the results.
1112 types_match = decls_match (oldres, newres);
1113 }
1114 else
1115 {
1116 /* Need to check scope for variable declaration (VAR_DECL).
1117 For typedef (TYPE_DECL), scope is ignored. */
1118 if (VAR_P (newdecl)
1119 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1120 /* [dcl.link]
1121 Two declarations for an object with C language linkage
1122 with the same name (ignoring the namespace that qualify
1123 it) that appear in different namespace scopes refer to
1124 the same object. */
1125 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1126 return 0;
1127
1128 if (TREE_TYPE (newdecl) == error_mark_node)
1129 types_match = TREE_TYPE (olddecl) == error_mark_node;
1130 else if (TREE_TYPE (olddecl) == NULL_TREE)
1131 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1132 else if (TREE_TYPE (newdecl) == NULL_TREE)
1133 types_match = 0;
1134 else
1135 types_match = comptypes (TREE_TYPE (newdecl),
1136 TREE_TYPE (olddecl),
1137 COMPARE_REDECLARATION);
1138 }
1139
1140 // Normal functions can be constrained, as can variable partial
1141 // specializations.
1142 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1143 types_match = equivalently_constrained (newdecl, olddecl);
1144
1145 return types_match;
1146}
1147
1148/* NEWDECL and OLDDECL have identical signatures. If they are
1149 different versions adjust them and return true. */
1150
1151bool
1152maybe_version_functions (tree newdecl, tree olddecl)
1153{
1154 if (!targetm.target_option.function_versions (newdecl, olddecl))
1155 return false;
1156
1157 bool record = false;
1158
1159 if (!DECL_FUNCTION_VERSIONED (olddecl))
1160 {
1161 record = true;
1162 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1163 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1164 mangle_decl (olddecl);
1165 }
1166
1167 if (!DECL_FUNCTION_VERSIONED (newdecl))
1168 {
1169 record = true;
1170 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1171 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1172 mangle_decl (newdecl);
1173 }
1174
1175 /* Only record if at least one was not already versions. */
1176 if (record)
1177 cgraph_node::record_function_versions (olddecl, newdecl);
1178
1179 return true;
1180}
1181
1182/* If NEWDECL is `static' and an `extern' was seen previously,
1183 warn about it. OLDDECL is the previous declaration.
1184
1185 Note that this does not apply to the C++ case of declaring
1186 a variable `extern const' and then later `const'.
1187
1188 Don't complain about built-in functions, since they are beyond
1189 the user's control. */
1190
1191void
1192warn_extern_redeclared_static (tree newdecl, tree olddecl)
1193{
1194 if (TREE_CODE (newdecl) == TYPE_DECL
1195 || TREE_CODE (newdecl) == TEMPLATE_DECL
1196 || TREE_CODE (newdecl) == CONST_DECL
1197 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1198 return;
1199
1200 /* Don't get confused by static member functions; that's a different
1201 use of `static'. */
1202 if (TREE_CODE (newdecl) == FUNCTION_DECL
1203 && DECL_STATIC_FUNCTION_P (newdecl))
1204 return;
1205
1206 /* If the old declaration was `static', or the new one isn't, then
1207 everything is OK. */
1208 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1209 return;
1210
1211 /* It's OK to declare a builtin function as `static'. */
1212 if (TREE_CODE (olddecl) == FUNCTION_DECL
1213 && DECL_ARTIFICIAL (olddecl))
1214 return;
1215
1216 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1217 "%qD was declared %<extern%> and later %<static%>", newdecl))
1218 inform (DECL_SOURCE_LOCATION (olddecl),
1219 "previous declaration of %qD", olddecl);
1220}
1221
1222/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1223 function templates. If their exception specifications do not
1224 match, issue a diagnostic. */
1225
1226static void
1227check_redeclaration_exception_specification (tree new_decl,
1228 tree old_decl)
1229{
1230 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1231 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1232
1233 /* Two default specs are equivalent, don't force evaluation. */
1234 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1235 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1236 return;
1237
1238 maybe_instantiate_noexcept (new_decl);
1239 maybe_instantiate_noexcept (old_decl);
1240 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1241 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1242
1243 /* [except.spec]
1244
1245 If any declaration of a function has an exception-specification,
1246 all declarations, including the definition and an explicit
1247 specialization, of that function shall have an
1248 exception-specification with the same set of type-ids. */
1249 if (! DECL_IS_BUILTIN (old_decl)
1250 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1251 {
1252 const char *const msg
1253 = G_("declaration of %qF has a different exception specifier");
1254 bool complained = true;
1255 location_t new_loc = DECL_SOURCE_LOCATION (new_decl);
1256 if (DECL_IN_SYSTEM_HEADER (old_decl))
1257 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl);
1258 else if (!flag_exceptions)
1259 /* We used to silently permit mismatched eh specs with
1260 -fno-exceptions, so make them a pedwarn now. */
1261 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl);
1262 else
1263 error_at (new_loc, msg, new_decl);
1264 if (complained)
1265 inform (DECL_SOURCE_LOCATION (old_decl),
1266 "from previous declaration %qF", old_decl);
1267 }
1268}
1269
1270/* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1271 Otherwise issue diagnostics. */
1272
1273static bool
1274validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1275{
1276 old_decl = STRIP_TEMPLATE (old_decl);
1277 new_decl = STRIP_TEMPLATE (new_decl);
1278 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1279 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1280 return true;
1281 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1282 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1283 return true;
1284 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1285 {
1286 if (DECL_BUILT_IN (old_decl))
1287 {
1288 /* Hide a built-in declaration. */
1289 DECL_DECLARED_CONSTEXPR_P (old_decl)
1290 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1291 return true;
1292 }
1293 /* 7.1.5 [dcl.constexpr]
1294 Note: An explicit specialization can differ from the template
1295 declaration with respect to the constexpr specifier. */
1296 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1297 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1298 return true;
1299
1300 error_at (DECL_SOURCE_LOCATION (new_decl),
1301 "redeclaration %qD differs in %<constexpr%> "
1302 "from previous declaration", new_decl);
1303 inform (DECL_SOURCE_LOCATION (old_decl),
1304 "previous declaration %qD", old_decl);
1305 return false;
1306 }
1307 return true;
1308}
1309
1310// If OLDDECL and NEWDECL are concept declarations with the same type
1311// (i.e., and template parameters), but different requirements,
1312// emit diagnostics and return true. Otherwise, return false.
1313static inline bool
1314check_concept_refinement (tree olddecl, tree newdecl)
1315{
1316 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1317 return false;
1318
1319 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1320 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1321 if (TREE_CODE (d1) != TREE_CODE (d2))
1322 return false;
1323
1324 tree t1 = TREE_TYPE (d1);
1325 tree t2 = TREE_TYPE (d2);
1326 if (TREE_CODE (d1) == FUNCTION_DECL)
1327 {
1328 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1329 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1330 DECL_TEMPLATE_PARMS (newdecl))
1331 && !equivalently_constrained (olddecl, newdecl))
1332 {
1333 error ("cannot specialize concept %q#D", olddecl);
1334 return true;
1335 }
1336 }
1337 return false;
1338}
1339
1340/* DECL is a redeclaration of a function or function template. If
1341 it does have default arguments issue a diagnostic. Note: this
1342 function is used to enforce the requirements in C++11 8.3.6 about
1343 no default arguments in redeclarations. */
1344
1345static void
1346check_redeclaration_no_default_args (tree decl)
1347{
1348 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1349
1350 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1351 t && t != void_list_node; t = TREE_CHAIN (t))
1352 if (TREE_PURPOSE (t))
1353 {
1354 permerror (DECL_SOURCE_LOCATION (decl),
1355 "redeclaration of %q#D may not have default "
1356 "arguments", decl);
1357 return;
1358 }
1359}
1360
1361#define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1362 && lookup_attribute ("gnu_inline", \
1363 DECL_ATTRIBUTES (fn)))
1364
1365/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1366 If the redeclaration is invalid, a diagnostic is issued, and the
1367 error_mark_node is returned. Otherwise, OLDDECL is returned.
1368
1369 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1370 returned.
1371
1372 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1373
1374tree
1375duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1376{
1377 unsigned olddecl_uid = DECL_UID (olddecl);
1378 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1379 int new_defines_function = 0;
1380 tree new_template_info;
1381
1382 if (newdecl == olddecl)
1383 return olddecl;
1384
1385 types_match = decls_match (newdecl, olddecl);
1386
1387 /* If either the type of the new decl or the type of the old decl is an
1388 error_mark_node, then that implies that we have already issued an
1389 error (earlier) for some bogus type specification, and in that case,
1390 it is rather pointless to harass the user with yet more error message
1391 about the same declaration, so just pretend the types match here. */
1392 if (TREE_TYPE (newdecl) == error_mark_node
1393 || TREE_TYPE (olddecl) == error_mark_node)
1394 return error_mark_node;
1395
1396 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1397 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1398 {
1399 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1400 && TREE_CODE (olddecl) != TEMPLATE_DECL
1401 && check_raw_literal_operator (olddecl))
1402 error ("literal operator template %q+D conflicts with"
1403 " raw literal operator %qD", newdecl, olddecl);
1404 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1405 && TREE_CODE (olddecl) == TEMPLATE_DECL
1406 && check_raw_literal_operator (newdecl))
1407 error ("raw literal operator %q+D conflicts with"
1408 " literal operator template %qD", newdecl, olddecl);
1409 }
1410
1411 if (DECL_P (olddecl)
1412 && TREE_CODE (newdecl) == FUNCTION_DECL
1413 && TREE_CODE (olddecl) == FUNCTION_DECL
1414 && diagnose_mismatched_attributes (olddecl, newdecl))
1415 {
1416 if (DECL_INITIAL (olddecl))
1417 inform (DECL_SOURCE_LOCATION (olddecl),
1418 "previous definition of %qD was here", olddecl);
1419 else
1420 inform (DECL_SOURCE_LOCATION (olddecl),
1421 "previous declaration of %qD was here", olddecl);
1422 }
1423
1424 /* Check for redeclaration and other discrepancies. */
1425 if (TREE_CODE (olddecl) == FUNCTION_DECL
1426 && DECL_ARTIFICIAL (olddecl))
1427 {
1428 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1429 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1430 {
1431 /* Avoid warnings redeclaring built-ins which have not been
1432 explicitly declared. */
1433 if (DECL_ANTICIPATED (olddecl))
1434 {
1435 if (TREE_PUBLIC (newdecl)
1436 && CP_DECL_CONTEXT (newdecl) == global_namespace)
1437 warning_at (DECL_SOURCE_LOCATION (newdecl),
1438 OPT_Wbuiltin_declaration_mismatch,
1439 "built-in function %qD declared as non-function",
1440 newdecl);
1441 return NULL_TREE;
1442 }
1443
1444 /* If you declare a built-in or predefined function name as static,
1445 the old definition is overridden, but optionally warn this was a
1446 bad choice of name. */
1447 if (! TREE_PUBLIC (newdecl))
1448 {
1449 warning (OPT_Wshadow,
1450 DECL_BUILT_IN (olddecl)
1451 ? G_("shadowing built-in function %q#D")
1452 : G_("shadowing library function %q#D"), olddecl);
1453 /* Discard the old built-in function. */
1454 return NULL_TREE;
1455 }
1456 /* If the built-in is not ansi, then programs can override
1457 it even globally without an error. */
1458 else if (! DECL_BUILT_IN (olddecl))
1459 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1460 "library function %q#D redeclared as non-function %q#D",
1461 olddecl, newdecl);
1462 else
1463 error ("declaration of %q+#D conflicts with built-in "
1464 "declaration %q#D", newdecl, olddecl);
1465 return NULL_TREE;
1466 }
1467 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1468 {
1469 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1470 error_at (DECL_SOURCE_LOCATION (newdecl),
1471 "redeclaration of %<pragma omp declare reduction%>");
1472 inform (DECL_SOURCE_LOCATION (olddecl),
1473 "previous %<pragma omp declare reduction%> declaration");
1474 return error_mark_node;
1475 }
1476 else if (!types_match)
1477 {
1478 /* Avoid warnings redeclaring built-ins which have not been
1479 explicitly declared. */
1480 if (DECL_ANTICIPATED (olddecl))
1481 {
1482 tree t1, t2;
1483
1484 /* A new declaration doesn't match a built-in one unless it
1485 is also extern "C". */
1486 gcc_assert (DECL_IS_BUILTIN (olddecl));
1487 gcc_assert (DECL_EXTERN_C_P (olddecl));
1488 if (!DECL_EXTERN_C_P (newdecl))
1489 return NULL_TREE;
1490
1491 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1492 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1493 t1 || t2;
1494 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1495 {
1496 if (!t1 || !t2)
1497 break;
1498 /* FILE, tm types are not known at the time
1499 we create the builtins. */
1500 for (unsigned i = 0;
1501 i < sizeof (builtin_structptr_types)
1502 / sizeof (builtin_structptr_type);
1503 ++i)
1504 if (TREE_VALUE (t2) == builtin_structptr_types[i].node)
1505 {
1506 tree t = TREE_VALUE (t1);
1507
1508 if (TYPE_PTR_P (t)
1509 && TYPE_IDENTIFIER (TREE_TYPE (t))
1510 == get_identifier (builtin_structptr_types[i].str)
1511 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1512 {
1513 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1514
1515 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1516 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1517 types_match = decls_match (newdecl, olddecl);
1518 if (types_match)
1519 return duplicate_decls (newdecl, olddecl,
1520 newdecl_is_friend);
1521 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1522 }
1523 goto next_arg;
1524 }
1525
1526 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1527 break;
1528next_arg:;
1529 }
1530
1531 warning_at (DECL_SOURCE_LOCATION (newdecl),
1532 OPT_Wbuiltin_declaration_mismatch,
1533 "declaration of %q#D conflicts with built-in "
1534 "declaration %q#D", newdecl, olddecl);
1535 }
1536 else if ((DECL_EXTERN_C_P (newdecl)
1537 && DECL_EXTERN_C_P (olddecl))
1538 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1539 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1540 {
1541 /* A near match; override the builtin. */
1542
1543 if (TREE_PUBLIC (newdecl))
1544 warning_at (DECL_SOURCE_LOCATION (newdecl),
1545 OPT_Wbuiltin_declaration_mismatch,
1546 "new declaration %q#D ambiguates built-in "
1547 "declaration %q#D", newdecl, olddecl);
1548 else
1549 warning (OPT_Wshadow,
1550 DECL_BUILT_IN (olddecl)
1551 ? G_("shadowing built-in function %q#D")
1552 : G_("shadowing library function %q#D"), olddecl);
1553 }
1554 else
1555 /* Discard the old built-in function. */
1556 return NULL_TREE;
1557
1558 /* Replace the old RTL to avoid problems with inlining. */
1559 COPY_DECL_RTL (newdecl, olddecl);
1560 }
1561 /* Even if the types match, prefer the new declarations type for
1562 built-ins which have not been explicitly declared, for
1563 exception lists, etc... */
1564 else if (DECL_IS_BUILTIN (olddecl))
1565 {
1566 tree type = TREE_TYPE (newdecl);
1567 tree attribs = (*targetm.merge_type_attributes)
1568 (TREE_TYPE (olddecl), type);
1569
1570 type = cp_build_type_attribute_variant (type, attribs);
1571 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1572 }
1573
1574 /* If a function is explicitly declared "throw ()", propagate that to
1575 the corresponding builtin. */
1576 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1577 && DECL_ANTICIPATED (olddecl)
1578 && TREE_NOTHROW (newdecl)
1579 && !TREE_NOTHROW (olddecl))
1580 {
1581 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1582 tree tmpdecl = builtin_decl_explicit (fncode);
1583 if (tmpdecl && tmpdecl != olddecl && types_match)
1584 TREE_NOTHROW (tmpdecl) = 1;
1585 }
1586
1587 /* Whether or not the builtin can throw exceptions has no
1588 bearing on this declarator. */
1589 TREE_NOTHROW (olddecl) = 0;
1590
1591 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1592 {
1593 /* If a builtin function is redeclared as `static', merge
1594 the declarations, but make the original one static. */
1595 DECL_THIS_STATIC (olddecl) = 1;
1596 TREE_PUBLIC (olddecl) = 0;
1597
1598 /* Make the old declaration consistent with the new one so
1599 that all remnants of the builtin-ness of this function
1600 will be banished. */
1601 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1602 COPY_DECL_RTL (newdecl, olddecl);
1603 }
1604 }
1605 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1606 {
1607 /* C++ Standard, 3.3, clause 4:
1608 "[Note: a namespace name or a class template name must be unique
1609 in its declarative region (7.3.2, clause 14). ]" */
1610 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1611 && TREE_CODE (newdecl) != NAMESPACE_DECL
1612 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1613 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1614 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1615 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1616 {
1617 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1618 && TREE_CODE (newdecl) != TYPE_DECL)
1619 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1620 && TREE_CODE (olddecl) != TYPE_DECL))
1621 {
1622 /* We do nothing special here, because C++ does such nasty
1623 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1624 get shadowed, and know that if we need to find a TYPE_DECL
1625 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1626 slot of the identifier. */
1627 return NULL_TREE;
1628 }
1629
1630 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1631 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1632 || (TREE_CODE (olddecl) == FUNCTION_DECL
1633 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1634 return NULL_TREE;
1635 }
1636
1637 error ("%q#D redeclared as different kind of symbol", newdecl);
1638 if (TREE_CODE (olddecl) == TREE_LIST)
1639 olddecl = TREE_VALUE (olddecl);
1640 inform (DECL_SOURCE_LOCATION (olddecl),
1641 "previous declaration %q#D", olddecl);
1642
1643 return error_mark_node;
1644 }
1645 else if (!types_match)
1646 {
1647 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1648 /* These are certainly not duplicate declarations; they're
1649 from different scopes. */
1650 return NULL_TREE;
1651
1652 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1653 {
1654 /* The name of a class template may not be declared to refer to
1655 any other template, class, function, object, namespace, value,
1656 or type in the same scope. */
1657 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1658 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1659 {
1660 error ("conflicting declaration of template %q+#D", newdecl);
1661 inform (DECL_SOURCE_LOCATION (olddecl),
1662 "previous declaration %q#D", olddecl);
1663 return error_mark_node;
1664 }
1665 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1666 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1667 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1668 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1669 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1670 DECL_TEMPLATE_PARMS (olddecl))
1671 /* Template functions can be disambiguated by
1672 return type. */
1673 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1674 TREE_TYPE (TREE_TYPE (olddecl)))
1675 // Template functions can also be disambiguated by
1676 // constraints.
1677 && equivalently_constrained (olddecl, newdecl))
1678 {
1679 error ("ambiguating new declaration %q+#D", newdecl);
1680 inform (DECL_SOURCE_LOCATION (olddecl),
1681 "old declaration %q#D", olddecl);
1682 }
1683 else if (check_concept_refinement (olddecl, newdecl))
1684 return error_mark_node;
1685 return NULL_TREE;
1686 }
1687 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1688 {
1689 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1690 {
1691 error ("conflicting declaration of C function %q+#D",
1692 newdecl);
1693 inform (DECL_SOURCE_LOCATION (olddecl),
1694 "previous declaration %q#D", olddecl);
1695 return NULL_TREE;
1696 }
1697 /* For function versions, params and types match, but they
1698 are not ambiguous. */
1699 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1700 && !DECL_FUNCTION_VERSIONED (olddecl))
1701 // The functions have the same parameter types.
1702 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1703 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1704 // And the same constraints.
1705 && equivalently_constrained (newdecl, olddecl))
1706 {
1707 error ("ambiguating new declaration of %q+#D", newdecl);
1708 inform (DECL_SOURCE_LOCATION (olddecl),
1709 "old declaration %q#D", olddecl);
1710 return error_mark_node;
1711 }
1712 else
1713 return NULL_TREE;
1714 }
1715 else
1716 {
1717 error ("conflicting declaration %q+#D", newdecl);
1718 inform (DECL_SOURCE_LOCATION (olddecl),
1719 "previous declaration as %q#D", olddecl);
1720 return error_mark_node;
1721 }
1722 }
1723 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1724 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1725 && (!DECL_TEMPLATE_INFO (newdecl)
1726 || (DECL_TI_TEMPLATE (newdecl)
1727 != DECL_TI_TEMPLATE (olddecl))))
1728 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1729 && (!DECL_TEMPLATE_INFO (olddecl)
1730 || (DECL_TI_TEMPLATE (olddecl)
1731 != DECL_TI_TEMPLATE (newdecl))))))
1732 /* It's OK to have a template specialization and a non-template
1733 with the same type, or to have specializations of two
1734 different templates with the same type. Note that if one is a
1735 specialization, and the other is an instantiation of the same
1736 template, that we do not exit at this point. That situation
1737 can occur if we instantiate a template class, and then
1738 specialize one of its methods. This situation is valid, but
1739 the declarations must be merged in the usual way. */
1740 return NULL_TREE;
1741 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1742 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1743 && !DECL_USE_TEMPLATE (newdecl))
1744 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1745 && !DECL_USE_TEMPLATE (olddecl))))
1746 /* One of the declarations is a template instantiation, and the
1747 other is not a template at all. That's OK. */
1748 return NULL_TREE;
1749 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1750 {
1751 /* In [namespace.alias] we have:
1752
1753 In a declarative region, a namespace-alias-definition can be
1754 used to redefine a namespace-alias declared in that declarative
1755 region to refer only to the namespace to which it already
1756 refers.
1757
1758 Therefore, if we encounter a second alias directive for the same
1759 alias, we can just ignore the second directive. */
1760 if (DECL_NAMESPACE_ALIAS (newdecl)
1761 && (DECL_NAMESPACE_ALIAS (newdecl)
1762 == DECL_NAMESPACE_ALIAS (olddecl)))
1763 return olddecl;
1764
1765 /* Leave it to update_binding to merge or report error. */
1766 return NULL_TREE;
1767 }
1768 else
1769 {
1770 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1771 if (errmsg)
1772 {
1773 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1774 if (DECL_NAME (olddecl) != NULL_TREE)
1775 inform (DECL_SOURCE_LOCATION (olddecl),
1776 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1777 ? G_("%q#D previously defined here")
1778 : G_("%q#D previously declared here"), olddecl);
1779 return error_mark_node;
1780 }
1781 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1782 && DECL_INITIAL (olddecl) != NULL_TREE
1783 && !prototype_p (TREE_TYPE (olddecl))
1784 && prototype_p (TREE_TYPE (newdecl)))
1785 {
1786 /* Prototype decl follows defn w/o prototype. */
1787 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1788 "prototype specified for %q#D", newdecl))
1789 inform (DECL_SOURCE_LOCATION (olddecl),
1790 "previous non-prototype definition here");
1791 }
1792 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1793 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1794 {
1795 /* [dcl.link]
1796 If two declarations of the same function or object
1797 specify different linkage-specifications ..., the program
1798 is ill-formed.... Except for functions with C++ linkage,
1799 a function declaration without a linkage specification
1800 shall not precede the first linkage specification for
1801 that function. A function can be declared without a
1802 linkage specification after an explicit linkage
1803 specification has been seen; the linkage explicitly
1804 specified in the earlier declaration is not affected by
1805 such a function declaration.
1806
1807 DR 563 raises the question why the restrictions on
1808 functions should not also apply to objects. Older
1809 versions of G++ silently ignore the linkage-specification
1810 for this example:
1811
1812 namespace N {
1813 extern int i;
1814 extern "C" int i;
1815 }
1816
1817 which is clearly wrong. Therefore, we now treat objects
1818 like functions. */
1819 if (current_lang_depth () == 0)
1820 {
1821 /* There is no explicit linkage-specification, so we use
1822 the linkage from the previous declaration. */
1823 retrofit_lang_decl (newdecl);
1824 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1825 }
1826 else
1827 {
1828 error ("conflicting declaration of %q+#D with %qL linkage",
1829 newdecl, DECL_LANGUAGE (newdecl));
1830 inform (DECL_SOURCE_LOCATION (olddecl),
1831 "previous declaration with %qL linkage",
1832 DECL_LANGUAGE (olddecl));
1833 }
1834 }
1835
1836 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1837 ;
1838 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1839 {
1840 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1841 if (DECL_FUNCTION_MEMBER_P (olddecl)
1842 && (/* grokfndecl passes member function templates too
1843 as FUNCTION_DECLs. */
1844 DECL_TEMPLATE_INFO (olddecl)
1845 /* C++11 8.3.6/6.
1846 Default arguments for a member function of a class
1847 template shall be specified on the initial declaration
1848 of the member function within the class template. */
1849 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1850 check_redeclaration_no_default_args (newdecl);
1851 else
1852 {
1853 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1854 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1855 int i = 1;
1856
1857 for (; t1 && t1 != void_list_node;
1858 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1859 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1860 {
1861 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1862 TREE_PURPOSE (t2)))
1863 {
1864 if (permerror (input_location,
1865 "default argument given for parameter "
1866 "%d of %q#D", i, newdecl))
1867 inform (DECL_SOURCE_LOCATION (olddecl),
1868 "previous specification in %q#D here",
1869 olddecl);
1870 }
1871 else
1872 {
1873 error ("default argument given for parameter %d "
1874 "of %q#D", i, newdecl);
1875 inform (DECL_SOURCE_LOCATION (olddecl),
1876 "previous specification in %q#D here",
1877 olddecl);
1878 }
1879 }
1880 }
1881 }
1882 }
1883
1884 /* Do not merge an implicit typedef with an explicit one. In:
1885
1886 class A;
1887 ...
1888 typedef class A A __attribute__ ((foo));
1889
1890 the attribute should apply only to the typedef. */
1891 if (TREE_CODE (olddecl) == TYPE_DECL
1892 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1893 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1894 return NULL_TREE;
1895
1896 /* If new decl is `static' and an `extern' was seen previously,
1897 warn about it. */
1898 warn_extern_redeclared_static (newdecl, olddecl);
1899
1900 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1901 return error_mark_node;
1902
1903 /* We have committed to returning 1 at this point. */
1904 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1905 {
1906 /* Now that functions must hold information normally held
1907 by field decls, there is extra work to do so that
1908 declaration information does not get destroyed during
1909 definition. */
1910 if (DECL_VINDEX (olddecl))
1911 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1912 if (DECL_CONTEXT (olddecl))
1913 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1914 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1915 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1916 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1917 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1918 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1919 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1920 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1921 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1922 if (DECL_OVERLOADED_OPERATOR_P (olddecl))
1923 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl)
1924 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl);
1925 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1926
1927 /* Optionally warn about more than one declaration for the same
1928 name, but don't warn about a function declaration followed by a
1929 definition. */
1930 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1931 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1932 /* Don't warn about extern decl followed by definition. */
1933 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1934 /* Don't warn about friends, let add_friend take care of it. */
1935 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1936 /* Don't warn about declaration followed by specialization. */
1937 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1938 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1939 {
1940 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1941 OPT_Wredundant_decls,
1942 "redundant redeclaration of %qD in same scope",
1943 newdecl))
1944 inform (DECL_SOURCE_LOCATION (olddecl),
1945 "previous declaration of %qD", olddecl);
1946 }
1947
1948 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1949 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1950 {
1951 if (DECL_DELETED_FN (newdecl))
1952 {
1953 error ("deleted definition of %q+D", newdecl);
1954 inform (DECL_SOURCE_LOCATION (olddecl),
1955 "previous declaration of %qD", olddecl);
1956 }
1957 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1958 }
1959 }
1960
1961 /* Deal with C++: must preserve virtual function table size. */
1962 if (TREE_CODE (olddecl) == TYPE_DECL)
1963 {
1964 tree newtype = TREE_TYPE (newdecl);
1965 tree oldtype = TREE_TYPE (olddecl);
1966
1967 if (newtype != error_mark_node && oldtype != error_mark_node
1968 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1969 CLASSTYPE_FRIEND_CLASSES (newtype)
1970 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1971
1972 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1973 }
1974
1975 /* Copy all the DECL_... slots specified in the new decl
1976 except for any that we copy here from the old type. */
1977 DECL_ATTRIBUTES (newdecl)
1978 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1979
1980 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1981 {
1982 olddecl_friend = DECL_FRIEND_P (olddecl);
1983 hidden_friend = (DECL_ANTICIPATED (olddecl)
1984 && DECL_HIDDEN_FRIEND_P (olddecl)
1985 && newdecl_is_friend);
1986 if (!hidden_friend)
1987 {
1988 DECL_ANTICIPATED (olddecl) = 0;
1989 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1990 }
1991 }
1992
1993 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1994 {
1995 tree old_result;
1996 tree new_result;
1997 old_result = DECL_TEMPLATE_RESULT (olddecl);
1998 new_result = DECL_TEMPLATE_RESULT (newdecl);
1999 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
2000 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
2001 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
2002 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
2003
2004 DECL_ATTRIBUTES (old_result)
2005 = (*targetm.merge_decl_attributes) (old_result, new_result);
2006
2007 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2008 {
2009 /* Per C++11 8.3.6/4, default arguments cannot be added in later
2010 declarations of a function template. */
2011 if (DECL_SOURCE_LOCATION (newdecl)
2012 != DECL_SOURCE_LOCATION (olddecl))
2013 check_redeclaration_no_default_args (newdecl);
2014
2015 check_default_args (newdecl);
2016
2017 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2018 && DECL_INITIAL (new_result))
2019 {
2020 if (DECL_INITIAL (old_result))
2021 DECL_UNINLINABLE (old_result) = 1;
2022 else
2023 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2024 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2025 DECL_NOT_REALLY_EXTERN (old_result)
2026 = DECL_NOT_REALLY_EXTERN (new_result);
2027 DECL_INTERFACE_KNOWN (old_result)
2028 = DECL_INTERFACE_KNOWN (new_result);
2029 DECL_DECLARED_INLINE_P (old_result)
2030 = DECL_DECLARED_INLINE_P (new_result);
2031 DECL_DISREGARD_INLINE_LIMITS (old_result)
2032 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2033
2034 }
2035 else
2036 {
2037 DECL_DECLARED_INLINE_P (old_result)
2038 |= DECL_DECLARED_INLINE_P (new_result);
2039 DECL_DISREGARD_INLINE_LIMITS (old_result)
2040 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2041 check_redeclaration_exception_specification (newdecl, olddecl);
2042 }
2043 }
2044
2045 /* If the new declaration is a definition, update the file and
2046 line information on the declaration, and also make
2047 the old declaration the same definition. */
2048 if (DECL_INITIAL (new_result) != NULL_TREE)
2049 {
2050 DECL_SOURCE_LOCATION (olddecl)
2051 = DECL_SOURCE_LOCATION (old_result)
2052 = DECL_SOURCE_LOCATION (newdecl);
2053 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2054 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2055 {
2056 tree parm;
2057 DECL_ARGUMENTS (old_result)
2058 = DECL_ARGUMENTS (new_result);
2059 for (parm = DECL_ARGUMENTS (old_result); parm;
2060 parm = DECL_CHAIN (parm))
2061 DECL_CONTEXT (parm) = old_result;
2062 }
2063 }
2064
2065 return olddecl;
2066 }
2067
2068 if (types_match)
2069 {
2070 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2071 check_redeclaration_exception_specification (newdecl, olddecl);
2072
2073 /* Automatically handles default parameters. */
2074 tree oldtype = TREE_TYPE (olddecl);
2075 tree newtype;
2076
2077 /* For typedefs use the old type, as the new type's DECL_NAME points
2078 at newdecl, which will be ggc_freed. */
2079 if (TREE_CODE (newdecl) == TYPE_DECL)
2080 {
2081 /* But NEWTYPE might have an attribute, honor that. */
2082 tree tem = TREE_TYPE (newdecl);
2083 newtype = oldtype;
2084
2085 if (TYPE_USER_ALIGN (tem))
2086 {
2087 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2088 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem));
2089 TYPE_USER_ALIGN (newtype) = true;
2090 }
2091
2092 /* And remove the new type from the variants list. */
2093 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2094 {
2095 tree remove = TREE_TYPE (newdecl);
2096 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2097 t = TYPE_NEXT_VARIANT (t))
2098 if (TYPE_NEXT_VARIANT (t) == remove)
2099 {
2100 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2101 break;
2102 }
2103 }
2104 }
2105 else
2106 /* Merge the data types specified in the two decls. */
2107 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2108
2109 if (VAR_P (newdecl))
2110 {
2111 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2112 /* For already initialized vars, TREE_READONLY could have been
2113 cleared in cp_finish_decl, because the var needs runtime
2114 initialization or destruction. Make sure not to set
2115 TREE_READONLY on it again. */
2116 if (DECL_INITIALIZED_P (olddecl)
2117 && !DECL_EXTERNAL (olddecl)
2118 && !TREE_READONLY (olddecl))
2119 TREE_READONLY (newdecl) = 0;
2120 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2121 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2122 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2123 if (DECL_DEPENDENT_INIT_P (olddecl))
2124 SET_DECL_DEPENDENT_INIT_P (newdecl, true);
2125 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2126 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2127 if (DECL_CLASS_SCOPE_P (olddecl))
2128 DECL_DECLARED_CONSTEXPR_P (newdecl)
2129 |= DECL_DECLARED_CONSTEXPR_P (olddecl);
2130
2131 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2132 if (DECL_LANG_SPECIFIC (olddecl)
2133 && CP_DECL_THREADPRIVATE_P (olddecl))
2134 {
2135 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2136 retrofit_lang_decl (newdecl);
2137 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2138 }
2139 }
2140
2141 /* An explicit specialization of a function template or of a member
2142 function of a class template can be declared transaction_safe
2143 independently of whether the corresponding template entity is declared
2144 transaction_safe. */
2145 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2146 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2147 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2148 && tx_safe_fn_type_p (newtype)
2149 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2150 newtype = tx_unsafe_fn_variant (newtype);
2151
2152 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2153
2154 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2155 check_default_args (newdecl);
2156
2157 /* Lay the type out, unless already done. */
2158 if (! same_type_p (newtype, oldtype)
2159 && TREE_TYPE (newdecl) != error_mark_node
2160 && !(processing_template_decl && uses_template_parms (newdecl)))
2161 layout_type (TREE_TYPE (newdecl));
2162
2163 if ((VAR_P (newdecl)
2164 || TREE_CODE (newdecl) == PARM_DECL
2165 || TREE_CODE (newdecl) == RESULT_DECL
2166 || TREE_CODE (newdecl) == FIELD_DECL
2167 || TREE_CODE (newdecl) == TYPE_DECL)
2168 && !(processing_template_decl && uses_template_parms (newdecl)))
2169 layout_decl (newdecl, 0);
2170
2171 /* Merge the type qualifiers. */
2172 if (TREE_READONLY (newdecl))
2173 TREE_READONLY (olddecl) = 1;
2174 if (TREE_THIS_VOLATILE (newdecl))
2175 TREE_THIS_VOLATILE (olddecl) = 1;
2176 if (TREE_NOTHROW (newdecl))
2177 TREE_NOTHROW (olddecl) = 1;
2178
2179 /* Merge deprecatedness. */
2180 if (TREE_DEPRECATED (newdecl))
2181 TREE_DEPRECATED (olddecl) = 1;
2182
2183 /* Preserve function specific target and optimization options */
2184 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2185 {
2186 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2187 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2188 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2189 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2190
2191 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2192 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2193 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2194 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2195 }
2196
2197 /* Merge the initialization information. */
2198 if (DECL_INITIAL (newdecl) == NULL_TREE
2199 && DECL_INITIAL (olddecl) != NULL_TREE)
2200 {
2201 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2202 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2203 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2204 {
2205 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2206 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2207 }
2208 }
2209
2210 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2211 {
2212 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2213 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2214 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2215 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2216 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2217 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2218 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2219 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2220 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2221 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2222 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2223 /* Keep the old RTL. */
2224 COPY_DECL_RTL (olddecl, newdecl);
2225 }
2226 else if (VAR_P (newdecl)
2227 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2228 {
2229 /* Keep the old RTL. We cannot keep the old RTL if the old
2230 declaration was for an incomplete object and the new
2231 declaration is not since many attributes of the RTL will
2232 change. */
2233 COPY_DECL_RTL (olddecl, newdecl);
2234 }
2235 }
2236 /* If cannot merge, then use the new type and qualifiers,
2237 and don't preserve the old rtl. */
2238 else
2239 {
2240 /* Clean out any memory we had of the old declaration. */
2241 tree oldstatic = value_member (olddecl, static_aggregates);
2242 if (oldstatic)
2243 TREE_VALUE (oldstatic) = error_mark_node;
2244
2245 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2246 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2247 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2248 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl);
2249 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2250 }
2251
2252 /* Merge the storage class information. */
2253 merge_weak (newdecl, olddecl);
2254
2255 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2256 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2257 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2258 if (! DECL_EXTERNAL (olddecl))
2259 DECL_EXTERNAL (newdecl) = 0;
2260 if (! DECL_COMDAT (olddecl))
2261 DECL_COMDAT (newdecl) = 0;
2262
2263 new_template_info = NULL_TREE;
2264 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2265 {
2266 bool new_redefines_gnu_inline = false;
2267
2268 if (new_defines_function
2269 && ((DECL_INTERFACE_KNOWN (olddecl)
2270 && TREE_CODE (olddecl) == FUNCTION_DECL)
2271 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2272 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2273 == FUNCTION_DECL))))
2274 {
2275 tree fn = olddecl;
2276
2277 if (TREE_CODE (fn) == TEMPLATE_DECL)
2278 fn = DECL_TEMPLATE_RESULT (olddecl);
2279
2280 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2281 }
2282
2283 if (!new_redefines_gnu_inline)
2284 {
2285 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2286 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2287 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2288 }
2289 DECL_TEMPLATE_INSTANTIATED (newdecl)
2290 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2291 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2292
2293 /* If the OLDDECL is an instantiation and/or specialization,
2294 then the NEWDECL must be too. But, it may not yet be marked
2295 as such if the caller has created NEWDECL, but has not yet
2296 figured out that it is a redeclaration. */
2297 if (!DECL_USE_TEMPLATE (newdecl))
2298 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2299
2300 /* Don't really know how much of the language-specific
2301 values we should copy from old to new. */
2302 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2303 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2304 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2305 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2306
2307 if (LANG_DECL_HAS_MIN (newdecl))
2308 {
2309 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2310 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2311 if (DECL_TEMPLATE_INFO (newdecl))
2312 {
2313 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2314 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2315 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2316 /* Remember the presence of explicit specialization args. */
2317 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2318 = TINFO_USED_TEMPLATE_ID (new_template_info);
2319 }
2320 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2321 }
2322 /* Only functions have these fields. */
2323 if (DECL_DECLARES_FUNCTION_P (newdecl))
2324 {
2325 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2326 DECL_BEFRIENDING_CLASSES (newdecl)
2327 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2328 DECL_BEFRIENDING_CLASSES (olddecl));
2329 /* DECL_THUNKS is only valid for virtual functions,
2330 otherwise it is a DECL_FRIEND_CONTEXT. */
2331 if (DECL_VIRTUAL_P (newdecl))
2332 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2333 }
2334 /* Only variables have this field. */
2335 else if (VAR_P (newdecl)
2336 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2337 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2338 }
2339
2340 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2341 {
2342 tree parm;
2343
2344 /* Merge parameter attributes. */
2345 tree oldarg, newarg;
2346 for (oldarg = DECL_ARGUMENTS(olddecl),
2347 newarg = DECL_ARGUMENTS(newdecl);
2348 oldarg && newarg;
2349 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2350 DECL_ATTRIBUTES (newarg)
2351 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2352 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2353 }
2354
2355 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2356 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2357 {
2358 /* If newdecl is not a specialization, then it is not a
2359 template-related function at all. And that means that we
2360 should have exited above, returning 0. */
2361 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2362
2363 if (DECL_ODR_USED (olddecl))
2364 /* From [temp.expl.spec]:
2365
2366 If a template, a member template or the member of a class
2367 template is explicitly specialized then that
2368 specialization shall be declared before the first use of
2369 that specialization that would cause an implicit
2370 instantiation to take place, in every translation unit in
2371 which such a use occurs. */
2372 error ("explicit specialization of %qD after first use",
2373 olddecl);
2374
2375 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2376 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2377 && DECL_DECLARED_INLINE_P (newdecl));
2378
2379 /* Don't propagate visibility from the template to the
2380 specialization here. We'll do that in determine_visibility if
2381 appropriate. */
2382 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2383
2384 /* [temp.expl.spec/14] We don't inline explicit specialization
2385 just because the primary template says so. */
2386
2387 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2388 the always_inline attribute. */
2389 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2390 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2391 {
2392 if (DECL_DECLARED_INLINE_P (newdecl))
2393 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2394 else
2395 DECL_ATTRIBUTES (newdecl)
2396 = remove_attribute ("always_inline",
2397 DECL_ATTRIBUTES (newdecl));
2398 }
2399 }
2400 else if (new_defines_function && DECL_INITIAL (olddecl))
2401 {
2402 /* Never inline re-defined extern inline functions.
2403 FIXME: this could be better handled by keeping both
2404 function as separate declarations. */
2405 DECL_UNINLINABLE (newdecl) = 1;
2406 }
2407 else
2408 {
2409 if (DECL_PENDING_INLINE_P (olddecl))
2410 {
2411 DECL_PENDING_INLINE_P (newdecl) = 1;
2412 DECL_PENDING_INLINE_INFO (newdecl)
2413 = DECL_PENDING_INLINE_INFO (olddecl);
2414 }
2415 else if (DECL_PENDING_INLINE_P (newdecl))
2416 ;
2417 else if (DECL_SAVED_FUNCTION_DATA (newdecl) == NULL)
2418 DECL_SAVED_FUNCTION_DATA (newdecl)
2419 = DECL_SAVED_FUNCTION_DATA (olddecl);
2420
2421 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2422
2423 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2424 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2425
2426 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2427 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2428 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2429 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2430 }
2431
2432 /* Preserve abstractness on cloned [cd]tors. */
2433 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2434
2435 /* Update newdecl's parms to point at olddecl. */
2436 for (parm = DECL_ARGUMENTS (newdecl); parm;
2437 parm = DECL_CHAIN (parm))
2438 DECL_CONTEXT (parm) = olddecl;
2439
2440 if (! types_match)
2441 {
2442 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2443 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2444 COPY_DECL_RTL (newdecl, olddecl);
2445 }
2446 if (! types_match || new_defines_function)
2447 {
2448 /* These need to be copied so that the names are available.
2449 Note that if the types do match, we'll preserve inline
2450 info and other bits, but if not, we won't. */
2451 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2452 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2453 }
2454 /* If redeclaring a builtin function, it stays built in
2455 if newdecl is a gnu_inline definition, or if newdecl is just
2456 a declaration. */
2457 if (DECL_BUILT_IN (olddecl)
2458 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2459 {
2460 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2461 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2462 /* If we're keeping the built-in definition, keep the rtl,
2463 regardless of declaration matches. */
2464 COPY_DECL_RTL (olddecl, newdecl);
2465 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2466 {
2467 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2468 switch (fncode)
2469 {
2470 /* If a compatible prototype of these builtin functions
2471 is seen, assume the runtime implements it with the
2472 expected semantics. */
2473 case BUILT_IN_STPCPY:
2474 if (builtin_decl_explicit_p (fncode))
2475 set_builtin_decl_implicit_p (fncode, true);
2476 break;
2477 default:
2478 if (builtin_decl_explicit_p (fncode))
2479 set_builtin_decl_declared_p (fncode, true);
2480 break;
2481 }
2482 }
2483
2484 copy_attributes_to_builtin (newdecl);
2485 }
2486 if (new_defines_function)
2487 /* If defining a function declared with other language
2488 linkage, use the previously declared language linkage. */
2489 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2490 else if (types_match)
2491 {
2492 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2493 /* Don't clear out the arguments if we're just redeclaring a
2494 function. */
2495 if (DECL_ARGUMENTS (olddecl))
2496 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2497 }
2498 }
2499 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2500 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2501
2502 /* Now preserve various other info from the definition. */
2503 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2504 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2505 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2506 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2507
2508 /* Warn about conflicting visibility specifications. */
2509 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2510 && DECL_VISIBILITY_SPECIFIED (newdecl)
2511 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2512 {
2513 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2514 "%qD: visibility attribute ignored because it "
2515 "conflicts with previous declaration", newdecl))
2516 inform (DECL_SOURCE_LOCATION (olddecl),
2517 "previous declaration of %qD", olddecl);
2518 }
2519 /* Choose the declaration which specified visibility. */
2520 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2521 {
2522 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2523 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2524 }
2525 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2526 so keep this behavior. */
2527 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2528 {
2529 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2530 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2531 }
2532 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2533 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2534 {
2535 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl));
2536 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2537 }
2538 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2539 if (DECL_WARN_IF_NOT_ALIGN (olddecl)
2540 > DECL_WARN_IF_NOT_ALIGN (newdecl))
2541 SET_DECL_WARN_IF_NOT_ALIGN (newdecl,
2542 DECL_WARN_IF_NOT_ALIGN (olddecl));
2543 if (TREE_CODE (newdecl) == FIELD_DECL)
2544 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2545
2546 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2547 with that from NEWDECL below. */
2548 if (DECL_LANG_SPECIFIC (olddecl))
2549 {
2550 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2551 != DECL_LANG_SPECIFIC (newdecl));
2552 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2553 }
2554
2555 /* Merge the USED information. */
2556 if (TREE_USED (olddecl))
2557 TREE_USED (newdecl) = 1;
2558 else if (TREE_USED (newdecl))
2559 TREE_USED (olddecl) = 1;
2560 if (VAR_P (newdecl))
2561 {
2562 if (DECL_READ_P (olddecl))
2563 DECL_READ_P (newdecl) = 1;
2564 else if (DECL_READ_P (newdecl))
2565 DECL_READ_P (olddecl) = 1;
2566 }
2567 if (DECL_PRESERVE_P (olddecl))
2568 DECL_PRESERVE_P (newdecl) = 1;
2569 else if (DECL_PRESERVE_P (newdecl))
2570 DECL_PRESERVE_P (olddecl) = 1;
2571
2572 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2573 to olddecl and deleted. */
2574 if (TREE_CODE (newdecl) == FUNCTION_DECL
2575 && DECL_FUNCTION_VERSIONED (olddecl))
2576 {
2577 /* Set the flag for newdecl so that it gets copied to olddecl. */
2578 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2579 /* newdecl will be purged after copying to olddecl and is no longer
2580 a version. */
2581 cgraph_node::delete_function_version_by_decl (newdecl);
2582 }
2583
2584 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2585 {
2586 int function_size;
2587 struct symtab_node *snode = symtab_node::get (olddecl);
2588
2589 function_size = sizeof (struct tree_decl_common);
2590
2591 memcpy ((char *) olddecl + sizeof (struct tree_common),
2592 (char *) newdecl + sizeof (struct tree_common),
2593 function_size - sizeof (struct tree_common));
2594
2595 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2596 (char *) newdecl + sizeof (struct tree_decl_common),
2597 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2598
2599 /* Preserve symtab node mapping. */
2600 olddecl->decl_with_vis.symtab_node = snode;
2601
2602 if (new_template_info)
2603 /* If newdecl is a template instantiation, it is possible that
2604 the following sequence of events has occurred:
2605
2606 o A friend function was declared in a class template. The
2607 class template was instantiated.
2608
2609 o The instantiation of the friend declaration was
2610 recorded on the instantiation list, and is newdecl.
2611
2612 o Later, however, instantiate_class_template called pushdecl
2613 on the newdecl to perform name injection. But, pushdecl in
2614 turn called duplicate_decls when it discovered that another
2615 declaration of a global function with the same name already
2616 existed.
2617
2618 o Here, in duplicate_decls, we decided to clobber newdecl.
2619
2620 If we're going to do that, we'd better make sure that
2621 olddecl, and not newdecl, is on the list of
2622 instantiations so that if we try to do the instantiation
2623 again we won't get the clobbered declaration. */
2624 reregister_specialization (newdecl,
2625 new_template_info,
2626 olddecl);
2627 }
2628 else
2629 {
2630 size_t size = tree_code_size (TREE_CODE (newdecl));
2631
2632 memcpy ((char *) olddecl + sizeof (struct tree_common),
2633 (char *) newdecl + sizeof (struct tree_common),
2634 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2635 switch (TREE_CODE (newdecl))
2636 {
2637 case LABEL_DECL:
2638 case VAR_DECL:
2639 case RESULT_DECL:
2640 case PARM_DECL:
2641 case FIELD_DECL:
2642 case TYPE_DECL:
2643 case CONST_DECL:
2644 {
2645 struct symtab_node *snode = NULL;
2646
2647 if (VAR_P (newdecl)
2648 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2649 || DECL_EXTERNAL (olddecl)))
2650 snode = symtab_node::get (olddecl);
2651 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2652 (char *) newdecl + sizeof (struct tree_decl_common),
2653 size - sizeof (struct tree_decl_common)
2654 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2655 if (VAR_P (newdecl))
2656 olddecl->decl_with_vis.symtab_node = snode;
2657 }
2658 break;
2659 default:
2660 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2661 (char *) newdecl + sizeof (struct tree_decl_common),
2662 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2663 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2664 break;
2665 }
2666 }
2667
2668 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2669 {
2670 if (DECL_EXTERNAL (olddecl)
2671 || TREE_PUBLIC (olddecl)
2672 || TREE_STATIC (olddecl))
2673 {
2674 /* Merge the section attribute.
2675 We want to issue an error if the sections conflict but that must be
2676 done later in decl_attributes since we are called before attributes
2677 are assigned. */
2678 if (DECL_SECTION_NAME (newdecl) != NULL)
2679 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2680
2681 if (DECL_ONE_ONLY (newdecl))
2682 {
2683 struct symtab_node *oldsym, *newsym;
2684 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2685 oldsym = cgraph_node::get_create (olddecl);
2686 else
2687 oldsym = varpool_node::get_create (olddecl);
2688 newsym = symtab_node::get (newdecl);
2689 oldsym->set_comdat_group (newsym->get_comdat_group ());
2690 }
2691 }
2692
2693 if (VAR_P (newdecl)
2694 && CP_DECL_THREAD_LOCAL_P (newdecl))
2695 {
2696 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2697 if (!processing_template_decl)
2698 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2699 }
2700 }
2701
2702 DECL_UID (olddecl) = olddecl_uid;
2703 if (olddecl_friend)
2704 DECL_FRIEND_P (olddecl) = 1;
2705 if (hidden_friend)
2706 {
2707 DECL_ANTICIPATED (olddecl) = 1;
2708 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2709 }
2710
2711 /* NEWDECL contains the merged attribute lists.
2712 Update OLDDECL to be the same. */
2713 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2714
2715 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2716 so that encode_section_info has a chance to look at the new decl
2717 flags and attributes. */
2718 if (DECL_RTL_SET_P (olddecl)
2719 && (TREE_CODE (olddecl) == FUNCTION_DECL
2720 || (VAR_P (olddecl)
2721 && TREE_STATIC (olddecl))))
2722 make_decl_rtl (olddecl);
2723
2724 /* The NEWDECL will no longer be needed. Because every out-of-class
2725 declaration of a member results in a call to duplicate_decls,
2726 freeing these nodes represents in a significant savings.
2727
2728 Before releasing the node, be sore to remove function from symbol
2729 table that might have been inserted there to record comdat group.
2730 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2731 structure is shared in between newdecl and oldecl. */
2732 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2733 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2734 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2735 {
2736 struct symtab_node *snode = symtab_node::get (newdecl);
2737 if (snode)
2738 snode->remove ();
2739 }
2740
2741 /* Remove the associated constraints for newdecl, if any, before
2742 reclaiming memory. */
2743 if (flag_concepts)
2744 remove_constraints (newdecl);
2745
2746 ggc_free (newdecl);
2747
2748 return olddecl;
2749}
2750
2751/* Return zero if the declaration NEWDECL is valid
2752 when the declaration OLDDECL (assumed to be for the same name)
2753 has already been seen.
2754 Otherwise return an error message format string with a %s
2755 where the identifier should go. */
2756
2757static const char *
2758redeclaration_error_message (tree newdecl, tree olddecl)
2759{
2760 if (TREE_CODE (newdecl) == TYPE_DECL)
2761 {
2762 /* Because C++ can put things into name space for free,
2763 constructs like "typedef struct foo { ... } foo"
2764 would look like an erroneous redeclaration. */
2765 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2766 return NULL;
2767 else
2768 return G_("redefinition of %q#D");
2769 }
2770 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2771 {
2772 /* If this is a pure function, its olddecl will actually be
2773 the original initialization to `0' (which we force to call
2774 abort()). Don't complain about redefinition in this case. */
2775 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2776 && DECL_INITIAL (olddecl) == NULL_TREE)
2777 return NULL;
2778
2779 /* If both functions come from different namespaces, this is not
2780 a redeclaration - this is a conflict with a used function. */
2781 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2782 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2783 && ! decls_match (olddecl, newdecl))
2784 return G_("%qD conflicts with used function");
2785
2786 /* We'll complain about linkage mismatches in
2787 warn_extern_redeclared_static. */
2788
2789 /* Defining the same name twice is no good. */
2790 if (decl_defined_p (olddecl)
2791 && decl_defined_p (newdecl))
2792 {
2793 if (DECL_NAME (olddecl) == NULL_TREE)
2794 return G_("%q#D not declared in class");
2795 else if (!GNU_INLINE_P (olddecl)
2796 || GNU_INLINE_P (newdecl))
2797 return G_("redefinition of %q#D");
2798 }
2799
2800 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2801 {
2802 bool olda = GNU_INLINE_P (olddecl);
2803 bool newa = GNU_INLINE_P (newdecl);
2804
2805 if (olda != newa)
2806 {
2807 if (newa)
2808 return G_("%q+D redeclared inline with "
2809 "%<gnu_inline%> attribute");
2810 else
2811 return G_("%q+D redeclared inline without "
2812 "%<gnu_inline%> attribute");
2813 }
2814 }
2815
2816 check_abi_tag_redeclaration
2817 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2818 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2819
2820 return NULL;
2821 }
2822 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2823 {
2824 tree nt, ot;
2825
2826 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2827 {
2828 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2829 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2830 return G_("redefinition of %q#D");
2831 return NULL;
2832 }
2833
2834 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2835 || (DECL_TEMPLATE_RESULT (newdecl)
2836 == DECL_TEMPLATE_RESULT (olddecl)))
2837 return NULL;
2838
2839 nt = DECL_TEMPLATE_RESULT (newdecl);
2840 if (DECL_TEMPLATE_INFO (nt))
2841 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2842 ot = DECL_TEMPLATE_RESULT (olddecl);
2843 if (DECL_TEMPLATE_INFO (ot))
2844 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2845 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2846 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2847 return G_("redefinition of %q#D");
2848
2849 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2850 {
2851 bool olda = GNU_INLINE_P (ot);
2852 bool newa = GNU_INLINE_P (nt);
2853
2854 if (olda != newa)
2855 {
2856 if (newa)
2857 return G_("%q+D redeclared inline with "
2858 "%<gnu_inline%> attribute");
2859 else
2860 return G_("%q+D redeclared inline without "
2861 "%<gnu_inline%> attribute");
2862 }
2863 }
2864
2865 /* Core issue #226 (C++0x):
2866
2867 If a friend function template declaration specifies a
2868 default template-argument, that declaration shall be a
2869 definition and shall be the only declaration of the
2870 function template in the translation unit. */
2871 if ((cxx_dialect != cxx98)
2872 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2873 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2874 /*is_primary=*/true,
2875 /*is_partial=*/false,
2876 /*is_friend_decl=*/2))
2877 return G_("redeclaration of friend %q#D "
2878 "may not have default template arguments");
2879
2880 return NULL;
2881 }
2882 else if (VAR_P (newdecl)
2883 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2884 && (! DECL_LANG_SPECIFIC (olddecl)
2885 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2886 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2887 {
2888 /* Only variables can be thread-local, and all declarations must
2889 agree on this property. */
2890 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2891 return G_("thread-local declaration of %q#D follows "
2892 "non-thread-local declaration");
2893 else
2894 return G_("non-thread-local declaration of %q#D follows "
2895 "thread-local declaration");
2896 }
2897 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2898 {
2899 /* The objects have been declared at namespace scope. If either
2900 is a member of an anonymous union, then this is an invalid
2901 redeclaration. For example:
2902
2903 int i;
2904 union { int i; };
2905
2906 is invalid. */
2907 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2908 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2909 return G_("redeclaration of %q#D");
2910 /* If at least one declaration is a reference, there is no
2911 conflict. For example:
2912
2913 int i = 3;
2914 extern int i;
2915
2916 is valid. */
2917 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2918 return NULL;
2919
2920 /* Static data member declared outside a class definition
2921 if the variable is defined within the class with constexpr
2922 specifier is declaration rather than definition (and
2923 deprecated). */
2924 if (cxx_dialect >= cxx17
2925 && DECL_CLASS_SCOPE_P (olddecl)
2926 && DECL_DECLARED_CONSTEXPR_P (olddecl)
2927 && !DECL_INITIAL (newdecl))
2928 {
2929 DECL_EXTERNAL (newdecl) = 1;
2930 /* For now, only warn with explicit -Wdeprecated. */
2931 if (global_options_set.x_warn_deprecated
2932 && warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated,
2933 "redundant redeclaration of %<constexpr%> static "
2934 "data member %qD", newdecl))
2935 inform (DECL_SOURCE_LOCATION (olddecl),
2936 "previous declaration of %qD", olddecl);
2937 return NULL;
2938 }
2939
2940 /* Reject two definitions. */
2941 return G_("redefinition of %q#D");
2942 }
2943 else
2944 {
2945 /* Objects declared with block scope: */
2946 /* Reject two definitions, and reject a definition
2947 together with an external reference. */
2948 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2949 return G_("redeclaration of %q#D");
2950 return NULL;
2951 }
2952}
2953
2954
2955/* Hash and equality functions for the named_label table. */
2956
2957hashval_t
2958named_label_hash::hash (const value_type entry)
2959{
2960 return IDENTIFIER_HASH_VALUE (entry->name);
2961}
2962
2963bool
2964named_label_hash::equal (const value_type entry, compare_type name)
2965{
2966 return name == entry->name;
2967}
2968
2969/* Look for a label named ID in the current function. If one cannot
2970 be found, create one. Return the named_label_entry, or NULL on
2971 failure. */
2972
2973static named_label_entry *
2974lookup_label_1 (tree id, bool making_local_p)
2975{
2976 /* You can't use labels at global scope. */
2977 if (current_function_decl == NULL_TREE)
2978 {
2979 error ("label %qE referenced outside of any function", id);
2980 return NULL;
2981 }
2982
2983 if (!named_labels)
2984 named_labels = hash_table<named_label_hash>::create_ggc (13);
2985
2986 hashval_t hash = IDENTIFIER_HASH_VALUE (id);
2987 named_label_entry **slot
2988 = named_labels->find_slot_with_hash (id, hash, INSERT);
2989 named_label_entry *old = *slot;
2990
2991 if (old && old->label_decl)
2992 {
2993 if (!making_local_p)
2994 return old;
2995
2996 if (old->binding_level == current_binding_level)
2997 {
2998 error ("local label %qE conflicts with existing label", id);
2999 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label");
3000 return NULL;
3001 }
3002 }
3003
3004 /* We are making a new decl, create or reuse the named_label_entry */
3005 named_label_entry *ent = NULL;
3006 if (old && !old->label_decl)
3007 ent = old;
3008 else
3009 {
3010 ent = ggc_cleared_alloc<named_label_entry> ();
3011 ent->name = id;
3012 ent->outer = old;
3013 *slot = ent;
3014 }
3015
3016 /* Now create the LABEL_DECL. */
3017 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
3018
3019 DECL_CONTEXT (decl) = current_function_decl;
3020 SET_DECL_MODE (decl, VOIDmode);
3021 if (making_local_p)
3022 {
3023 C_DECLARED_LABEL_FLAG (decl) = true;
3024 DECL_CHAIN (decl) = current_binding_level->names;
3025 current_binding_level->names = decl;
3026 }
3027
3028 ent->label_decl = decl;
3029
3030 return ent;
3031}
3032
3033/* Wrapper for lookup_label_1. */
3034
3035tree
3036lookup_label (tree id)
3037{
3038 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3039 named_label_entry *ent = lookup_label_1 (id, false);
3040 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3041 return ent ? ent->label_decl : NULL_TREE;
3042}
3043
3044tree
3045declare_local_label (tree id)
3046{
3047 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3048 named_label_entry *ent = lookup_label_1 (id, true);
3049 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3050 return ent ? ent->label_decl : NULL_TREE;
3051}
3052
3053/* Returns nonzero if it is ill-formed to jump past the declaration of
3054 DECL. Returns 2 if it's also a real problem. */
3055
3056static int
3057decl_jump_unsafe (tree decl)
3058{
3059 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3060 with automatic storage duration is not in scope to a point where it is
3061 in scope is ill-formed unless the variable has scalar type, class type
3062 with a trivial default constructor and a trivial destructor, a
3063 cv-qualified version of one of these types, or an array of one of the
3064 preceding types and is declared without an initializer (8.5). */
3065 tree type = TREE_TYPE (decl);
3066
3067 if (!VAR_P (decl) || TREE_STATIC (decl)
3068 || type == error_mark_node)
3069 return 0;
3070
3071 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3072 || variably_modified_type_p (type, NULL_TREE))
3073 return 2;
3074
3075 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3076 return 1;
3077
3078 return 0;
3079}
3080
3081/* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3082 to the user. */
3083
3084static bool
3085identify_goto (tree decl, location_t loc, const location_t *locus,
3086 diagnostic_t diag_kind)
3087{
3088 bool complained
3089 = emit_diagnostic (diag_kind, loc, 0,
3090 decl ? N_("jump to label %qD")
3091 : N_("jump to case label"), decl);
3092 if (complained && locus)
3093 inform (*locus, " from here");
3094 return complained;
3095}
3096
3097/* Check that a single previously seen jump to a newly defined label
3098 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3099 the jump context; NAMES are the names in scope in LEVEL at the jump
3100 context; LOCUS is the source position of the jump or 0. Returns
3101 true if all is well. */
3102
3103static bool
3104check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3105 bool exited_omp, const location_t *locus)
3106{
3107 cp_binding_level *b;
3108 bool complained = false;
3109 int identified = 0;
3110 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false;
3111
3112 if (exited_omp)
3113 {
3114 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3115 if (complained)
3116 inform (input_location, " exits OpenMP structured block");
3117 saw_omp = true;
3118 identified = 2;
3119 }
3120
3121 for (b = current_binding_level; b ; b = b->level_chain)
3122 {
3123 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3124
3125 for (new_decls = b->names; new_decls != old_decls;
3126 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3127 : TREE_CHAIN (new_decls)))
3128 {
3129 int problem = decl_jump_unsafe (new_decls);
3130 if (! problem)
3131 continue;
3132
3133 if (!identified)
3134 {
3135 complained = identify_goto (decl, input_location, locus,
3136 DK_PERMERROR);
3137 identified = 1;
3138 }
3139 if (complained)
3140 {
3141 if (problem > 1)
3142 inform (DECL_SOURCE_LOCATION (new_decls),
3143 " crosses initialization of %q#D", new_decls);
3144 else
3145 inform (DECL_SOURCE_LOCATION (new_decls),
3146 " enters scope of %q#D, which has "
3147 "non-trivial destructor", new_decls);
3148 }
3149 }
3150
3151 if (b == level)
3152 break;
3153
3154 const char *inf = NULL;
3155 location_t loc = input_location;
3156 switch (b->kind)
3157 {
3158 case sk_try:
3159 if (!saw_eh)
3160 inf = N_("enters try block");
3161 saw_eh = true;
3162 break;
3163
3164 case sk_catch:
3165 if (!saw_eh)
3166 inf = N_("enters catch block");
3167 saw_eh = true;
3168 break;
3169
3170 case sk_omp:
3171 if (!saw_omp)
3172 inf = N_("enters OpenMP structured block");
3173 saw_omp = true;
3174 break;
3175
3176 case sk_transaction:
3177 if (!saw_tm)
3178 inf = N_("enters synchronized or atomic statement");
3179 saw_tm = true;
3180 break;
3181
3182 case sk_block:
3183 if (!saw_cxif && level_for_constexpr_if (b->level_chain))
3184 {
3185 inf = N_("enters constexpr if statement");
3186 loc = EXPR_LOCATION (b->level_chain->this_entity);
3187 saw_cxif = true;
3188 }
3189 break;
3190
3191 default:
3192 break;
3193 }
3194
3195 if (inf)
3196 {
3197 if (identified < 2)
3198 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3199 identified = 2;
3200 if (complained)
3201 inform (loc, " %s", inf);
3202 }
3203 }
3204
3205 return !identified;
3206}
3207
3208static void
3209check_previous_goto (tree decl, struct named_label_use_entry *use)
3210{
3211 check_previous_goto_1 (decl, use->binding_level,
3212 use->names_in_scope, use->in_omp_scope,
3213 &use->o_goto_locus);
3214}
3215
3216static bool
3217check_switch_goto (cp_binding_level* level)
3218{
3219 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3220}
3221
3222/* Check that a new jump to a label DECL is OK. Called by
3223 finish_goto_stmt. */
3224
3225void
3226check_goto (tree decl)
3227{
3228 /* We can't know where a computed goto is jumping.
3229 So we assume that it's OK. */
3230 if (TREE_CODE (decl) != LABEL_DECL)
3231 return;
3232
3233 /* We didn't record any information about this label when we created it,
3234 and there's not much point since it's trivial to analyze as a return. */
3235 if (decl == cdtor_label)
3236 return;
3237
3238 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl));
3239 named_label_entry **slot
3240 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT);
3241 named_label_entry *ent = *slot;
3242
3243 /* If the label hasn't been defined yet, defer checking. */
3244 if (! DECL_INITIAL (decl))
3245 {
3246 /* Don't bother creating another use if the last goto had the
3247 same data, and will therefore create the same set of errors. */
3248 if (ent->uses
3249 && ent->uses->names_in_scope == current_binding_level->names)
3250 return;
3251
3252 named_label_use_entry *new_use
3253 = ggc_alloc<named_label_use_entry> ();
3254 new_use->binding_level = current_binding_level;
3255 new_use->names_in_scope = current_binding_level->names;
3256 new_use->o_goto_locus = input_location;
3257 new_use->in_omp_scope = false;
3258
3259 new_use->next = ent->uses;
3260 ent->uses = new_use;
3261 return;
3262 }
3263
3264 bool saw_catch = false, complained = false;
3265 int identified = 0;
3266 tree bad;
3267 unsigned ix;
3268
3269 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3270 || ent->in_constexpr_if
3271 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3272 {
3273 diagnostic_t diag_kind = DK_PERMERROR;
3274 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if
3275 || ent->in_transaction_scope || ent->in_omp_scope)
3276 diag_kind = DK_ERROR;
3277 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3278 &input_location, diag_kind);
3279 identified = 1 + (diag_kind == DK_ERROR);
3280 }
3281
3282 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3283 {
3284 int u = decl_jump_unsafe (bad);
3285
3286 if (u > 1 && DECL_ARTIFICIAL (bad))
3287 {
3288 /* Can't skip init of __exception_info. */
3289 if (identified == 1)
3290 {
3291 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3292 &input_location, DK_ERROR);
3293 identified = 2;
3294 }
3295 if (complained)
3296 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3297 saw_catch = true;
3298 }
3299 else if (complained)
3300 {
3301 if (u > 1)
3302 inform (DECL_SOURCE_LOCATION (bad),
3303 " skips initialization of %q#D", bad);
3304 else
3305 inform (DECL_SOURCE_LOCATION (bad),
3306 " enters scope of %q#D which has "
3307 "non-trivial destructor", bad);
3308 }
3309 }
3310
3311 if (complained)
3312 {
3313 if (ent->in_try_scope)
3314 inform (input_location, " enters try block");
3315 else if (ent->in_catch_scope && !saw_catch)
3316 inform (input_location, " enters catch block");
3317 else if (ent->in_transaction_scope)
3318 inform (input_location, " enters synchronized or atomic statement");
3319 else if (ent->in_constexpr_if)
3320 inform (input_location, " enters %<constexpr%> if statement");
3321 }
3322
3323 if (ent->in_omp_scope)
3324 {
3325 if (complained)
3326 inform (input_location, " enters OpenMP structured block");
3327 }
3328 else if (flag_openmp)
3329 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3330 {
3331 if (b == ent->binding_level)
3332 break;
3333 if (b->kind == sk_omp)
3334 {
3335 if (identified < 2)
3336 {
3337 complained = identify_goto (decl,
3338 DECL_SOURCE_LOCATION (decl),
3339 &input_location, DK_ERROR);
3340 identified = 2;
3341 }
3342 if (complained)
3343 inform (input_location, " exits OpenMP structured block");
3344 break;
3345 }
3346 }
3347}
3348
3349/* Check that a return is ok wrt OpenMP structured blocks.
3350 Called by finish_return_stmt. Returns true if all is well. */
3351
3352bool
3353check_omp_return (void)
3354{
3355 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain)
3356 if (b->kind == sk_omp)
3357 {
3358 error ("invalid exit from OpenMP structured block");
3359 return false;
3360 }
3361 else if (b->kind == sk_function_parms)
3362 break;
3363 return true;
3364}
3365
3366/* Define a label, specifying the location in the source file.
3367 Return the LABEL_DECL node for the label. */
3368
3369static tree
3370define_label_1 (location_t location, tree name)
3371{
3372 /* After labels, make any new cleanups in the function go into their
3373 own new (temporary) binding contour. */
3374 for (cp_binding_level *p = current_binding_level;
3375 p->kind != sk_function_parms;
3376 p = p->level_chain)
3377 p->more_cleanups_ok = 0;
3378
3379 named_label_entry *ent = lookup_label_1 (name, false);
3380 tree decl = ent->label_decl;
3381
3382 if (DECL_INITIAL (decl) != NULL_TREE)
3383 {
3384 error ("duplicate label %qD", decl);
3385 return error_mark_node;
3386 }
3387 else
3388 {
3389 /* Mark label as having been defined. */
3390 DECL_INITIAL (decl) = error_mark_node;
3391 /* Say where in the source. */
3392 DECL_SOURCE_LOCATION (decl) = location;
3393
3394 ent->binding_level = current_binding_level;
3395 ent->names_in_scope = current_binding_level->names;
3396
3397 for (named_label_use_entry *use = ent->uses; use; use = use->next)
3398 check_previous_goto (decl, use);
3399 ent->uses = NULL;
3400 }
3401
3402 return decl;
3403}
3404
3405/* Wrapper for define_label_1. */
3406
3407tree
3408define_label (location_t location, tree name)
3409{
3410 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3411 tree ret = define_label_1 (location, name);
3412 timevar_cond_stop (TV_NAME_LOOKUP, running);
3413 return ret;
3414}
3415
3416
3417struct cp_switch
3418{
3419 cp_binding_level *level;
3420 struct cp_switch *next;
3421 /* The SWITCH_STMT being built. */
3422 tree switch_stmt;
3423 /* A splay-tree mapping the low element of a case range to the high
3424 element, or NULL_TREE if there is no high element. Used to
3425 determine whether or not a new case label duplicates an old case
3426 label. We need a tree, rather than simply a hash table, because
3427 of the GNU case range extension. */
3428 splay_tree cases;
3429 /* Remember whether there was a case value that is outside the
3430 range of the original type of the controlling expression. */
3431 bool outside_range_p;
3432 /* Remember whether a default: case label has been seen. */
3433 bool has_default_p;
3434 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */
3435 bool break_stmt_seen_p;
3436 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch,
3437 where BREAK_STMT doesn't belong to the SWITCH_STMT. */
3438 bool in_loop_body_p;
3439};
3440
3441/* A stack of the currently active switch statements. The innermost
3442 switch statement is on the top of the stack. There is no need to
3443 mark the stack for garbage collection because it is only active
3444 during the processing of the body of a function, and we never
3445 collect at that point. */
3446
3447static struct cp_switch *switch_stack;
3448
3449/* Called right after a switch-statement condition is parsed.
3450 SWITCH_STMT is the switch statement being parsed. */
3451
3452void
3453push_switch (tree switch_stmt)
3454{
3455 struct cp_switch *p = XNEW (struct cp_switch);
3456 p->level = current_binding_level;
3457 p->next = switch_stack;
3458 p->switch_stmt = switch_stmt;
3459 p->cases = splay_tree_new (case_compare, NULL, NULL);
3460 p->outside_range_p = false;
3461 p->has_default_p = false;
3462 p->break_stmt_seen_p = false;
3463 p->in_loop_body_p = false;
3464 switch_stack = p;
3465}
3466
3467void
3468pop_switch (void)
3469{
3470 struct cp_switch *cs = switch_stack;
3471 location_t switch_location;
3472
3473 /* Emit warnings as needed. */
3474 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3475 const bool bool_cond_p
3476 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3477 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3478 if (!processing_template_decl)
3479 c_do_switch_warnings (cs->cases, switch_location,
3480 SWITCH_STMT_TYPE (cs->switch_stmt),
3481 SWITCH_STMT_COND (cs->switch_stmt),
3482 bool_cond_p, cs->outside_range_p);
3483
3484 /* For the benefit of block_may_fallthru remember if the switch body
3485 case labels cover all possible values and if there are break; stmts. */
3486 if (cs->has_default_p
3487 || (!processing_template_decl
3488 && c_switch_covers_all_cases_p (cs->cases,
3489 SWITCH_STMT_TYPE (cs->switch_stmt))))
3490 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1;
3491 if (!cs->break_stmt_seen_p)
3492 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1;
3493 gcc_assert (!cs->in_loop_body_p);
3494 splay_tree_delete (cs->cases);
3495 switch_stack = switch_stack->next;
3496 free (cs);
3497}
3498
3499/* Note that a BREAK_STMT is about to be added. If it is inside of
3500 a SWITCH_STMT and not inside of a loop body inside of it, note
3501 in switch_stack we've seen a BREAK_STMT. */
3502
3503void
3504note_break_stmt (void)
3505{
3506 if (switch_stack && !switch_stack->in_loop_body_p)
3507 switch_stack->break_stmt_seen_p = true;
3508}
3509
3510/* Note the start of processing of an iteration statement's body.
3511 The note_break_stmt function will do nothing while processing it.
3512 Return a flag that should be passed to note_iteration_stmt_body_end. */
3513
3514bool
3515note_iteration_stmt_body_start (void)
3516{
3517 if (!switch_stack)
3518 return false;
3519 bool ret = switch_stack->in_loop_body_p;
3520 switch_stack->in_loop_body_p = true;
3521 return ret;
3522}
3523
3524/* Note the end of processing of an iteration statement's body. */
3525
3526void
3527note_iteration_stmt_body_end (bool prev)
3528{
3529 if (switch_stack)
3530 switch_stack->in_loop_body_p = prev;
3531}
3532
3533/* Convert a case constant VALUE in a switch to the type TYPE of the switch
3534 condition. Note that if TYPE and VALUE are already integral we don't
3535 really do the conversion because the language-independent
3536 warning/optimization code will work better that way. */
3537
3538static tree
3539case_conversion (tree type, tree value)
3540{
3541 if (value == NULL_TREE)
3542 return value;
3543
3544 if (cxx_dialect >= cxx11
3545 && (SCOPED_ENUM_P (type)
3546 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3547 {
3548 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3549 type = type_promotes_to (type);
3550 value = (perform_implicit_conversion_flags
3551 (type, value, tf_warning_or_error,
3552 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3553 }
3554 return cxx_constant_value (value);
3555}
3556
3557/* Note that we've seen a definition of a case label, and complain if this
3558 is a bad place for one. */
3559
3560tree
3561finish_case_label (location_t loc, tree low_value, tree high_value)
3562{
3563 tree cond, r;
3564 cp_binding_level *p;
3565 tree type;
3566
3567 if (low_value == NULL_TREE && high_value == NULL_TREE)
3568 switch_stack->has_default_p = true;
3569
3570 if (processing_template_decl)
3571 {
3572 tree label;
3573
3574 /* For templates, just add the case label; we'll do semantic
3575 analysis at instantiation-time. */
3576 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3577 return add_stmt (build_case_label (low_value, high_value, label));
3578 }
3579
3580 /* Find the condition on which this switch statement depends. */
3581 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3582 if (cond && TREE_CODE (cond) == TREE_LIST)
3583 cond = TREE_VALUE (cond);
3584
3585 if (!check_switch_goto (switch_stack->level))
3586 return error_mark_node;
3587
3588 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3589
3590 low_value = case_conversion (type, low_value);
3591 high_value = case_conversion (type, high_value);
3592
3593 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3594 low_value, high_value,
3595 &switch_stack->outside_range_p);
3596
3597 /* After labels, make any new cleanups in the function go into their
3598 own new (temporary) binding contour. */
3599 for (p = current_binding_level;
3600 p->kind != sk_function_parms;
3601 p = p->level_chain)
3602 p->more_cleanups_ok = 0;
3603
3604 return r;
3605}
3606
3607struct typename_info {
3608 tree scope;
3609 tree name;
3610 tree template_id;
3611 bool enum_p;
3612 bool class_p;
3613};
3614
3615struct typename_hasher : ggc_ptr_hash<tree_node>
3616{
3617 typedef typename_info *compare_type;
3618
3619 /* Hash a TYPENAME_TYPE. */
3620
3621 static hashval_t
3622 hash (tree t)
3623 {
3624 hashval_t hash;
3625
3626 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3627 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3628
3629 return hash;
3630 }
3631
3632 /* Compare two TYPENAME_TYPEs. */
3633
3634 static bool
3635 equal (tree t1, const typename_info *t2)
3636 {
3637 return (TYPE_IDENTIFIER (t1) == t2->name
3638 && TYPE_CONTEXT (t1) == t2->scope
3639 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3640 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3641 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3642 }
3643};
3644
3645/* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3646 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3647
3648 Returns the new TYPENAME_TYPE. */
3649
3650static GTY (()) hash_table<typename_hasher> *typename_htab;
3651
3652tree
3653build_typename_type (tree context, tree name, tree fullname,
3654 enum tag_types tag_type)
3655{
3656 tree t;
3657 tree d;
3658 typename_info ti;
3659 tree *e;
3660 hashval_t hash;
3661
3662 if (typename_htab == NULL)
3663 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3664
3665 ti.scope = FROB_CONTEXT (context);
3666 ti.name = name;
3667 ti.template_id = fullname;
3668 ti.enum_p = tag_type == enum_type;
3669 ti.class_p = (tag_type == class_type
3670 || tag_type == record_type
3671 || tag_type == union_type);
3672 hash = (htab_hash_pointer (ti.scope)
3673 ^ htab_hash_pointer (ti.name));
3674
3675 /* See if we already have this type. */
3676 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3677 if (*e)
3678 t = *e;
3679 else
3680 {
3681 /* Build the TYPENAME_TYPE. */
3682 t = cxx_make_type (TYPENAME_TYPE);
3683 TYPE_CONTEXT (t) = ti.scope;
3684 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3685 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3686 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3687
3688 /* Build the corresponding TYPE_DECL. */
3689 d = build_decl (input_location, TYPE_DECL, name, t);
3690 TYPE_NAME (TREE_TYPE (d)) = d;
3691 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3692 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3693 DECL_ARTIFICIAL (d) = 1;
3694
3695 /* Store it in the hash table. */
3696 *e = t;
3697
3698 /* TYPENAME_TYPEs must always be compared structurally, because
3699 they may or may not resolve down to another type depending on
3700 the currently open classes. */
3701 SET_TYPE_STRUCTURAL_EQUALITY (t);
3702 }
3703
3704 return t;
3705}
3706
3707/* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3708 provided to name the type. Returns an appropriate type, unless an
3709 error occurs, in which case error_mark_node is returned. If we
3710 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3711 return that, rather than the _TYPE it corresponds to, in other
3712 cases we look through the type decl. If TF_ERROR is set, complain
3713 about errors, otherwise be quiet. */
3714
3715tree
3716make_typename_type (tree context, tree name, enum tag_types tag_type,
3717 tsubst_flags_t complain)
3718{
3719 tree fullname;
3720 tree t;
3721 bool want_template;
3722
3723 if (name == error_mark_node
3724 || context == NULL_TREE
3725 || context == error_mark_node)
3726 return error_mark_node;
3727
3728 if (TYPE_P (name))
3729 {
3730 if (!(TYPE_LANG_SPECIFIC (name)
3731 && (CLASSTYPE_IS_TEMPLATE (name)
3732 || CLASSTYPE_USE_TEMPLATE (name))))
3733 name = TYPE_IDENTIFIER (name);
3734 else
3735 /* Create a TEMPLATE_ID_EXPR for the type. */
3736 name = build_nt (TEMPLATE_ID_EXPR,
3737 CLASSTYPE_TI_TEMPLATE (name),
3738 CLASSTYPE_TI_ARGS (name));
3739 }
3740 else if (TREE_CODE (name) == TYPE_DECL)
3741 name = DECL_NAME (name);
3742
3743 fullname = name;
3744
3745 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3746 {
3747 name = TREE_OPERAND (name, 0);
3748 if (DECL_TYPE_TEMPLATE_P (name))
3749 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3750 if (TREE_CODE (name) != IDENTIFIER_NODE)
3751 {
3752 if (complain & tf_error)
3753 error ("%qD is not a type", name);
3754 return error_mark_node;
3755 }
3756 }
3757 if (TREE_CODE (name) == TEMPLATE_DECL)
3758 {
3759 if (complain & tf_error)
3760 error ("%qD used without template parameters", name);
3761 return error_mark_node;
3762 }
3763 gcc_assert (identifier_p (name));
3764 gcc_assert (TYPE_P (context));
3765
3766 if (!MAYBE_CLASS_TYPE_P (context))
3767 {
3768 if (complain & tf_error)
3769 error ("%q#T is not a class", context);
3770 return error_mark_node;
3771 }
3772
3773 /* When the CONTEXT is a dependent type, NAME could refer to a
3774 dependent base class of CONTEXT. But look inside it anyway
3775 if CONTEXT is a currently open scope, in case it refers to a
3776 member of the current instantiation or a non-dependent base;
3777 lookup will stop when we hit a dependent base. */
3778 if (!dependent_scope_p (context))
3779 /* We should only set WANT_TYPE when we're a nested typename type.
3780 Then we can give better diagnostics if we find a non-type. */
3781 t = lookup_field (context, name, 2, /*want_type=*/true);
3782 else
3783 t = NULL_TREE;
3784
3785 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3786 return build_typename_type (context, name, fullname, tag_type);
3787
3788 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3789
3790 if (!t)
3791 {
3792 if (complain & tf_error)
3793 {
3794 if (!COMPLETE_TYPE_P (context))
3795