1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988-2016 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along 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 "cilk.h"
52 #include "builtins.h"
53
54 /* Possible cases of bad specifiers type used by bad_specifiers. */
55 enum bad_spec_place {
56 BSP_VAR, /* variable */
57 BSP_PARM, /* parameter */
58 BSP_TYPE, /* type */
59 BSP_FIELD /* field */
60 };
61
62 static const char *redeclaration_error_message (tree, tree);
63
64 static int decl_jump_unsafe (tree);
65 static void require_complete_types_for_parms (tree);
66 static int ambi_op_p (enum tree_code);
67 static int unary_op_p (enum tree_code);
68 static void push_local_name (tree);
69 static tree grok_reference_init (tree, tree, tree, int);
70 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *,
71 int, int, int, tree);
72 static int check_static_variable_definition (tree, tree);
73 static void record_unknown_type (tree, const char *);
74 static tree builtin_function_1 (tree, tree, bool);
75 static int member_function_or_else (tree, tree, enum overload_flags);
76 static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
77 int);
78 static void check_for_uninitialized_const_var (tree);
79 static tree local_variable_p_walkfn (tree *, int *, void *);
80 static tree record_builtin_java_type (const char *, int);
81 static const char *tag_name (enum tag_types);
82 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
83 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
84 static void maybe_deduce_size_from_array_init (tree, tree);
85 static void layout_var_decl (tree);
86 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
87 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
88 static void save_function_data (tree);
89 static void copy_type_enum (tree , tree);
90 static void check_function_type (tree, tree);
91 static void finish_constructor_body (void);
92 static void begin_destructor_body (void);
93 static void finish_destructor_body (void);
94 static void record_key_method_defined (tree);
95 static tree create_array_type_for_decl (tree, tree, tree);
96 static tree get_atexit_node (void);
97 static tree get_dso_handle_node (void);
98 static tree start_cleanup_fn (void);
99 static void end_cleanup_fn (void);
100 static tree cp_make_fname_decl (location_t, tree, int);
101 static void initialize_predefined_identifiers (void);
102 static tree check_special_function_return_type
103 (special_function_kind, tree, tree, int, const location_t*);
104 static tree push_cp_library_fn (enum tree_code, tree, int);
105 static tree build_cp_library_fn (tree, enum tree_code, tree, int);
106 static void store_parm_decls (tree);
107 static void initialize_local_var (tree, tree);
108 static void expand_static_init (tree, tree);
109
110 /* The following symbols are subsumed in the cp_global_trees array, and
111 listed here individually for documentation purposes.
112
113 C++ extensions
114 tree wchar_decl_node;
115
116 tree vtable_entry_type;
117 tree delta_type_node;
118 tree __t_desc_type_node;
119
120 tree class_type_node;
121 tree unknown_type_node;
122
123 Array type `vtable_entry_type[]'
124
125 tree vtbl_type_node;
126 tree vtbl_ptr_type_node;
127
128 Namespaces,
129
130 tree std_node;
131 tree abi_node;
132
133 A FUNCTION_DECL which can call `abort'. Not necessarily the
134 one that the user will declare, but sufficient to be called
135 by routines that want to abort the program.
136
137 tree abort_fndecl;
138
139 Used by RTTI
140 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
141 tree tinfo_var_id; */
142
143 tree cp_global_trees[CPTI_MAX];
144
145 /* Indicates that there is a type value in some namespace, although
146 that is not necessarily in scope at the moment. */
147
148 tree global_type_node;
149
150 /* The node that holds the "name" of the global scope. */
151 tree global_scope_name;
152
153 #define local_names cp_function_chain->x_local_names
154
155 /* A list of objects which have constructors or destructors
156 which reside in the global scope. The decl is stored in
157 the TREE_VALUE slot and the initializer is stored
158 in the TREE_PURPOSE slot. */
159 tree static_aggregates;
160
161 /* Like static_aggregates, but for thread_local variables. */
162 tree tls_aggregates;
163
164 /* -- end of C++ */
165
166 /* A node for the integer constant 2. */
167
168 tree integer_two_node;
169
170 /* Used only for jumps to as-yet undefined labels, since jumps to
171 defined labels can have their validity checked immediately. */
172
173 struct GTY((chain_next ("%h.next"))) named_label_use_entry {
174 struct named_label_use_entry *next;
175 /* The binding level to which this entry is *currently* attached.
176 This is initially the binding level in which the goto appeared,
177 but is modified as scopes are closed. */
178 cp_binding_level *binding_level;
179 /* The head of the names list that was current when the goto appeared,
180 or the inner scope popped. These are the decls that will *not* be
181 skipped when jumping to the label. */
182 tree names_in_scope;
183 /* The location of the goto, for error reporting. */
184 location_t o_goto_locus;
185 /* True if an OpenMP structured block scope has been closed since
186 the goto appeared. This means that the branch from the label will
187 illegally exit an OpenMP scope. */
188 bool in_omp_scope;
189 };
190
191 /* A list of all LABEL_DECLs in the function that have names. Here so
192 we can clear out their names' definitions at the end of the
193 function, and so we can check the validity of jumps to these labels. */
194
195 struct GTY((for_user)) named_label_entry {
196 /* The decl itself. */
197 tree label_decl;
198
199 /* The binding level to which the label is *currently* attached.
200 This is initially set to the binding level in which the label
201 is defined, but is modified as scopes are closed. */
202 cp_binding_level *binding_level;
203 /* The head of the names list that was current when the label was
204 defined, or the inner scope popped. These are the decls that will
205 be skipped when jumping to the label. */
206 tree names_in_scope;
207 /* A vector of all decls from all binding levels that would be
208 crossed by a backward branch to the label. */
209 vec<tree, va_gc> *bad_decls;
210
211 /* A list of uses of the label, before the label is defined. */
212 struct named_label_use_entry *uses;
213
214 /* The following bits are set after the label is defined, and are
215 updated as scopes are popped. They indicate that a backward jump
216 to the label will illegally enter a scope of the given flavor. */
217 bool in_try_scope;
218 bool in_catch_scope;
219 bool in_omp_scope;
220 bool in_transaction_scope;
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.) */
228 int function_depth;
229
230 /* States indicating how grokdeclarator() should handle declspecs marked
231 with __attribute__((deprecated)). An object declared as
232 __attribute__((deprecated)) suppresses warnings of uses of other
233 deprecated items. */
234 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
235
236
237 /* A list of VAR_DECLs whose type was incomplete at the time the
238 variable was declared. */
239
240 struct GTY(()) incomplete_var {
241 tree decl;
242 tree incomplete_type;
243 };
244
245
246 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
247
248 /* Returns the kind of template specialization we are currently
249 processing, given that it's declaration contained N_CLASS_SCOPES
250 explicit scope qualifications. */
251
252 tmpl_spec_kind
current_tmpl_spec_kind(int n_class_scopes)253 current_tmpl_spec_kind (int n_class_scopes)
254 {
255 int n_template_parm_scopes = 0;
256 int seen_specialization_p = 0;
257 int innermost_specialization_p = 0;
258 cp_binding_level *b;
259
260 /* Scan through the template parameter scopes. */
261 for (b = current_binding_level;
262 b->kind == sk_template_parms;
263 b = b->level_chain)
264 {
265 /* If we see a specialization scope inside a parameter scope,
266 then something is wrong. That corresponds to a declaration
267 like:
268
269 template <class T> template <> ...
270
271 which is always invalid since [temp.expl.spec] forbids the
272 specialization of a class member template if the enclosing
273 class templates are not explicitly specialized as well. */
274 if (b->explicit_spec_p)
275 {
276 if (n_template_parm_scopes == 0)
277 innermost_specialization_p = 1;
278 else
279 seen_specialization_p = 1;
280 }
281 else if (seen_specialization_p == 1)
282 return tsk_invalid_member_spec;
283
284 ++n_template_parm_scopes;
285 }
286
287 /* Handle explicit instantiations. */
288 if (processing_explicit_instantiation)
289 {
290 if (n_template_parm_scopes != 0)
291 /* We've seen a template parameter list during an explicit
292 instantiation. For example:
293
294 template <class T> template void f(int);
295
296 This is erroneous. */
297 return tsk_invalid_expl_inst;
298 else
299 return tsk_expl_inst;
300 }
301
302 if (n_template_parm_scopes < n_class_scopes)
303 /* We've not seen enough template headers to match all the
304 specialized classes present. For example:
305
306 template <class T> void R<T>::S<T>::f(int);
307
308 This is invalid; there needs to be one set of template
309 parameters for each class. */
310 return tsk_insufficient_parms;
311 else if (n_template_parm_scopes == n_class_scopes)
312 /* We're processing a non-template declaration (even though it may
313 be a member of a template class.) For example:
314
315 template <class T> void S<T>::f(int);
316
317 The `class T' matches the `S<T>', leaving no template headers
318 corresponding to the `f'. */
319 return tsk_none;
320 else if (n_template_parm_scopes > n_class_scopes + 1)
321 /* We've got too many template headers. For example:
322
323 template <> template <class T> void f (T);
324
325 There need to be more enclosing classes. */
326 return tsk_excessive_parms;
327 else
328 /* This must be a template. It's of the form:
329
330 template <class T> template <class U> void S<T>::f(U);
331
332 This is a specialization if the innermost level was a
333 specialization; otherwise it's just a definition of the
334 template. */
335 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
336 }
337
338 /* Exit the current scope. */
339
340 void
finish_scope(void)341 finish_scope (void)
342 {
343 poplevel (0, 0, 0);
344 }
345
346 /* When a label goes out of scope, check to see if that label was used
347 in a valid manner, and issue any appropriate warnings or errors. */
348
349 static void
pop_label(tree label,tree old_value)350 pop_label (tree label, tree old_value)
351 {
352 if (!processing_template_decl)
353 {
354 if (DECL_INITIAL (label) == NULL_TREE)
355 {
356 location_t location;
357
358 error ("label %q+D used but not defined", label);
359 location = input_location;
360 /* FIXME want (LOCATION_FILE (input_location), (line)0) */
361 /* Avoid crashing later. */
362 define_label (location, DECL_NAME (label));
363 }
364 else
365 warn_for_unused_label (label);
366 }
367
368 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
369 }
370
371 /* Push all named labels into a vector, so that we can sort it on DECL_UID
372 to avoid code generation differences. */
373
374 int
note_label(named_label_entry ** slot,vec<named_label_entry ** > & labels)375 note_label (named_label_entry **slot, vec<named_label_entry **> &labels)
376 {
377 labels.quick_push (slot);
378 return 1;
379 }
380
381 /* Helper function to sort named label entries in a vector by DECL_UID. */
382
383 static int
sort_labels(const void * a,const void * b)384 sort_labels (const void *a, const void *b)
385 {
386 named_label_entry **slot1 = *(named_label_entry **const *) a;
387 named_label_entry **slot2 = *(named_label_entry **const *) b;
388 if (DECL_UID ((*slot1)->label_decl) < DECL_UID ((*slot2)->label_decl))
389 return -1;
390 if (DECL_UID ((*slot1)->label_decl) > DECL_UID ((*slot2)->label_decl))
391 return 1;
392 return 0;
393 }
394
395 /* At the end of a function, all labels declared within the function
396 go out of scope. BLOCK is the top-level block for the
397 function. */
398
399 static void
pop_labels(tree block)400 pop_labels (tree block)
401 {
402 if (named_labels)
403 {
404 auto_vec<named_label_entry **, 32> labels;
405 named_label_entry **slot;
406 unsigned int i;
407
408 /* Push all the labels into a vector and sort them by DECL_UID,
409 so that gaps between DECL_UIDs don't affect code generation. */
410 labels.reserve_exact (named_labels->elements ());
411 named_labels->traverse<vec<named_label_entry **> &, note_label> (labels);
412 labels.qsort (sort_labels);
413 FOR_EACH_VEC_ELT (labels, i, slot)
414 {
415 struct named_label_entry *ent = *slot;
416
417 pop_label (ent->label_decl, NULL_TREE);
418
419 /* Put the labels into the "variables" of the top-level block,
420 so debugger can see them. */
421 DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
422 BLOCK_VARS (block) = ent->label_decl;
423
424 named_labels->clear_slot (slot);
425 }
426 named_labels = NULL;
427 }
428 }
429
430 /* At the end of a block with local labels, restore the outer definition. */
431
432 static void
pop_local_label(tree label,tree old_value)433 pop_local_label (tree label, tree old_value)
434 {
435 struct named_label_entry dummy;
436
437 pop_label (label, old_value);
438
439 dummy.label_decl = label;
440 named_label_entry **slot = named_labels->find_slot (&dummy, NO_INSERT);
441 named_labels->clear_slot (slot);
442 }
443
444 /* The following two routines are used to interface to Objective-C++.
445 The binding level is purposely treated as an opaque type. */
446
447 void *
objc_get_current_scope(void)448 objc_get_current_scope (void)
449 {
450 return current_binding_level;
451 }
452
453 /* The following routine is used by the NeXT-style SJLJ exceptions;
454 variables get marked 'volatile' so as to not be clobbered by
455 _setjmp()/_longjmp() calls. All variables in the current scope,
456 as well as parent scopes up to (but not including) ENCLOSING_BLK
457 shall be thusly marked. */
458
459 void
objc_mark_locals_volatile(void * enclosing_blk)460 objc_mark_locals_volatile (void *enclosing_blk)
461 {
462 cp_binding_level *scope;
463
464 for (scope = current_binding_level;
465 scope && scope != enclosing_blk;
466 scope = scope->level_chain)
467 {
468 tree decl;
469
470 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
471 objc_volatilize_decl (decl);
472
473 /* Do not climb up past the current function. */
474 if (scope->kind == sk_function_parms)
475 break;
476 }
477 }
478
479 /* Update data for defined and undefined labels when leaving a scope. */
480
481 int
poplevel_named_label_1(named_label_entry ** slot,cp_binding_level * bl)482 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl)
483 {
484 named_label_entry *ent = *slot;
485 cp_binding_level *obl = bl->level_chain;
486
487 if (ent->binding_level == bl)
488 {
489 tree decl;
490
491 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
492 TREE_LISTs representing OVERLOADs, so be careful. */
493 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
494 ? DECL_CHAIN (decl)
495 : TREE_CHAIN (decl)))
496 if (decl_jump_unsafe (decl))
497 vec_safe_push (ent->bad_decls, decl);
498
499 ent->binding_level = obl;
500 ent->names_in_scope = obl->names;
501 switch (bl->kind)
502 {
503 case sk_try:
504 ent->in_try_scope = true;
505 break;
506 case sk_catch:
507 ent->in_catch_scope = true;
508 break;
509 case sk_omp:
510 ent->in_omp_scope = true;
511 break;
512 case sk_transaction:
513 ent->in_transaction_scope = true;
514 break;
515 default:
516 break;
517 }
518 }
519 else if (ent->uses)
520 {
521 struct named_label_use_entry *use;
522
523 for (use = ent->uses; use ; use = use->next)
524 if (use->binding_level == bl)
525 {
526 use->binding_level = obl;
527 use->names_in_scope = obl->names;
528 if (bl->kind == sk_omp)
529 use->in_omp_scope = true;
530 }
531 }
532
533 return 1;
534 }
535
536 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
537 when errors were reported, except for -Werror-unused-but-set-*. */
538 static int unused_but_set_errorcount;
539
540 /* Exit a binding level.
541 Pop the level off, and restore the state of the identifier-decl mappings
542 that were in effect when this level was entered.
543
544 If KEEP == 1, this level had explicit declarations, so
545 and create a "block" (a BLOCK node) for the level
546 to record its declarations and subblocks for symbol table output.
547
548 If FUNCTIONBODY is nonzero, this level is the body of a function,
549 so create a block as if KEEP were set and also clear out all
550 label names.
551
552 If REVERSE is nonzero, reverse the order of decls before putting
553 them into the BLOCK. */
554
555 tree
poplevel(int keep,int reverse,int functionbody)556 poplevel (int keep, int reverse, int functionbody)
557 {
558 tree link;
559 /* The chain of decls was accumulated in reverse order.
560 Put it into forward order, just for cleanliness. */
561 tree decls;
562 tree subblocks;
563 tree block;
564 tree decl;
565 int leaving_for_scope;
566 scope_kind kind;
567 unsigned ix;
568 cp_label_binding *label_bind;
569
570 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
571 restart:
572
573 block = NULL_TREE;
574
575 gcc_assert (current_binding_level->kind != sk_class);
576
577 if (current_binding_level->kind == sk_cleanup)
578 functionbody = 0;
579 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
580
581 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
582
583 /* We used to use KEEP == 2 to indicate that the new block should go
584 at the beginning of the list of blocks at this binding level,
585 rather than the end. This hack is no longer used. */
586 gcc_assert (keep == 0 || keep == 1);
587
588 if (current_binding_level->keep)
589 keep = 1;
590
591 /* Any uses of undefined labels, and any defined labels, now operate
592 under constraints of next binding contour. */
593 if (cfun && !functionbody && named_labels)
594 named_labels->traverse<cp_binding_level *, poplevel_named_label_1>
595 (current_binding_level);
596
597 /* Get the decls in the order they were written.
598 Usually current_binding_level->names is in reverse order.
599 But parameter decls were previously put in forward order. */
600
601 if (reverse)
602 current_binding_level->names
603 = decls = nreverse (current_binding_level->names);
604 else
605 decls = current_binding_level->names;
606
607 /* If there were any declarations or structure tags in that level,
608 or if this level is a function body,
609 create a BLOCK to record them for the life of this function. */
610 block = NULL_TREE;
611 /* Avoid function body block if possible. */
612 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE)
613 keep = 0;
614 else if (keep == 1 || functionbody)
615 block = make_node (BLOCK);
616 if (block != NULL_TREE)
617 {
618 BLOCK_VARS (block) = decls;
619 BLOCK_SUBBLOCKS (block) = subblocks;
620 }
621
622 /* In each subblock, record that this is its superior. */
623 if (keep >= 0)
624 for (link = subblocks; link; link = BLOCK_CHAIN (link))
625 BLOCK_SUPERCONTEXT (link) = block;
626
627 /* We still support the old for-scope rules, whereby the variables
628 in a for-init statement were in scope after the for-statement
629 ended. We only use the new rules if flag_new_for_scope is
630 nonzero. */
631 leaving_for_scope
632 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
633
634 /* Before we remove the declarations first check for unused variables. */
635 if ((warn_unused_variable || warn_unused_but_set_variable)
636 && current_binding_level->kind != sk_template_parms
637 && !processing_template_decl)
638 for (tree d = getdecls (); d; d = TREE_CHAIN (d))
639 {
640 /* There are cases where D itself is a TREE_LIST. See in
641 push_local_binding where the list of decls returned by
642 getdecls is built. */
643 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
644 tree type = TREE_TYPE (decl);
645 if (VAR_P (decl)
646 && (! TREE_USED (decl) || !DECL_READ_P (decl))
647 && ! DECL_IN_SYSTEM_HEADER (decl)
648 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
649 && type != error_mark_node
650 && (!CLASS_TYPE_P (type)
651 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
652 || lookup_attribute ("warn_unused",
653 TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
654 {
655 if (! TREE_USED (decl))
656 warning_at (DECL_SOURCE_LOCATION (decl),
657 OPT_Wunused_variable, "unused variable %qD", decl);
658 else if (DECL_CONTEXT (decl) == current_function_decl
659 // For -Wunused-but-set-variable leave references alone.
660 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
661 && errorcount == unused_but_set_errorcount)
662 {
663 warning_at (DECL_SOURCE_LOCATION (decl),
664 OPT_Wunused_but_set_variable,
665 "variable %qD set but not used", decl);
666 unused_but_set_errorcount = errorcount;
667 }
668 }
669 }
670
671 /* Remove declarations for all the DECLs in this level. */
672 for (link = decls; link; link = TREE_CHAIN (link))
673 {
674 if (leaving_for_scope && VAR_P (link)
675 /* It's hard to make this ARM compatibility hack play nicely with
676 lambdas, and it really isn't necessary in C++11 mode. */
677 && cxx_dialect < cxx11
678 && DECL_NAME (link))
679 {
680 tree name = DECL_NAME (link);
681 cxx_binding *ob;
682 tree ns_binding;
683
684 ob = outer_binding (name,
685 IDENTIFIER_BINDING (name),
686 /*class_p=*/true);
687 if (!ob)
688 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
689 else
690 ns_binding = NULL_TREE;
691
692 if (ob && ob->scope == current_binding_level->level_chain)
693 /* We have something like:
694
695 int i;
696 for (int i; ;);
697
698 and we are leaving the `for' scope. There's no reason to
699 keep the binding of the inner `i' in this case. */
700 pop_binding (name, link);
701 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
702 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
703 /* Here, we have something like:
704
705 typedef int I;
706
707 void f () {
708 for (int I; ;);
709 }
710
711 We must pop the for-scope binding so we know what's a
712 type and what isn't. */
713 pop_binding (name, link);
714 else
715 {
716 /* Mark this VAR_DECL as dead so that we can tell we left it
717 there only for backward compatibility. */
718 DECL_DEAD_FOR_LOCAL (link) = 1;
719
720 /* Keep track of what should have happened when we
721 popped the binding. */
722 if (ob && ob->value)
723 {
724 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
725 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
726 }
727
728 /* Add it to the list of dead variables in the next
729 outermost binding to that we can remove these when we
730 leave that binding. */
731 vec_safe_push (
732 current_binding_level->level_chain->dead_vars_from_for,
733 link);
734
735 /* Although we don't pop the cxx_binding, we do clear
736 its SCOPE since the scope is going away now. */
737 IDENTIFIER_BINDING (name)->scope
738 = current_binding_level->level_chain;
739 }
740 }
741 else
742 {
743 tree name;
744
745 /* Remove the binding. */
746 decl = link;
747
748 if (TREE_CODE (decl) == TREE_LIST)
749 decl = TREE_VALUE (decl);
750 name = decl;
751
752 if (TREE_CODE (name) == OVERLOAD)
753 name = OVL_FUNCTION (name);
754
755 gcc_assert (DECL_P (name));
756 pop_binding (DECL_NAME (name), decl);
757 }
758 }
759
760 /* Remove declarations for any `for' variables from inner scopes
761 that we kept around. */
762 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
763 ix, decl)
764 pop_binding (DECL_NAME (decl), decl);
765
766 /* Restore the IDENTIFIER_TYPE_VALUEs. */
767 for (link = current_binding_level->type_shadowed;
768 link; link = TREE_CHAIN (link))
769 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
770
771 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
772 FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
773 ix, label_bind)
774 pop_local_label (label_bind->label, label_bind->prev_value);
775
776 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
777 list if a `using' declaration put them there. The debugging
778 back ends won't understand OVERLOAD, so we remove them here.
779 Because the BLOCK_VARS are (temporarily) shared with
780 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
781 popped all the bindings. Also remove undeduced 'auto' decls,
782 which LTO doesn't understand, and can't have been used by anything. */
783 if (block)
784 {
785 tree* d;
786
787 for (d = &BLOCK_VARS (block); *d; )
788 {
789 if (TREE_CODE (*d) == TREE_LIST
790 || (!processing_template_decl
791 && undeduced_auto_decl (*d)))
792 *d = TREE_CHAIN (*d);
793 else
794 d = &DECL_CHAIN (*d);
795 }
796 }
797
798 /* If the level being exited is the top level of a function,
799 check over all the labels. */
800 if (functionbody)
801 {
802 if (block)
803 {
804 /* Since this is the top level block of a function, the vars are
805 the function's parameters. Don't leave them in the BLOCK
806 because they are found in the FUNCTION_DECL instead. */
807 BLOCK_VARS (block) = 0;
808 pop_labels (block);
809 }
810 else
811 pop_labels (subblocks);
812 }
813
814 kind = current_binding_level->kind;
815 if (kind == sk_cleanup)
816 {
817 tree stmt;
818
819 /* If this is a temporary binding created for a cleanup, then we'll
820 have pushed a statement list level. Pop that, create a new
821 BIND_EXPR for the block, and insert it into the stream. */
822 stmt = pop_stmt_list (current_binding_level->statement_list);
823 stmt = c_build_bind_expr (input_location, block, stmt);
824 add_stmt (stmt);
825 }
826
827 leave_scope ();
828 if (functionbody)
829 {
830 /* The current function is being defined, so its DECL_INITIAL
831 should be error_mark_node. */
832 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
833 DECL_INITIAL (current_function_decl) = block ? block : subblocks;
834 if (subblocks)
835 {
836 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
837 {
838 if (BLOCK_SUBBLOCKS (subblocks))
839 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1;
840 }
841 else
842 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1;
843 }
844 }
845 else if (block)
846 current_binding_level->blocks
847 = block_chainon (current_binding_level->blocks, block);
848
849 /* If we did not make a block for the level just exited,
850 any blocks made for inner levels
851 (since they cannot be recorded as subblocks in that level)
852 must be carried forward so they will later become subblocks
853 of something else. */
854 else if (subblocks)
855 current_binding_level->blocks
856 = block_chainon (current_binding_level->blocks, subblocks);
857
858 /* Each and every BLOCK node created here in `poplevel' is important
859 (e.g. for proper debugging information) so if we created one
860 earlier, mark it as "used". */
861 if (block)
862 TREE_USED (block) = 1;
863
864 /* All temporary bindings created for cleanups are popped silently. */
865 if (kind == sk_cleanup)
866 goto restart;
867
868 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
869 return block;
870 }
871
872 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
873 itself, calling F for each. The DATA is passed to F as well. */
874
875 static int
walk_namespaces_r(tree name_space,walk_namespaces_fn f,void * data)876 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
877 {
878 int result = 0;
879 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
880
881 result |= (*f) (name_space, data);
882
883 for (; current; current = DECL_CHAIN (current))
884 result |= walk_namespaces_r (current, f, data);
885
886 return result;
887 }
888
889 /* Walk all the namespaces, calling F for each. The DATA is passed to
890 F as well. */
891
892 int
walk_namespaces(walk_namespaces_fn f,void * data)893 walk_namespaces (walk_namespaces_fn f, void* data)
894 {
895 return walk_namespaces_r (global_namespace, f, data);
896 }
897
898 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */
899
900 int
wrapup_globals_for_namespace(tree name_space,void * data ATTRIBUTE_UNUSED)901 wrapup_globals_for_namespace (tree name_space, void* data ATTRIBUTE_UNUSED)
902 {
903 cp_binding_level *level = NAMESPACE_LEVEL (name_space);
904 vec<tree, va_gc> *statics = level->static_decls;
905 tree *vec = statics->address ();
906 int len = statics->length ();
907
908 if (warn_unused_function)
909 {
910 tree decl;
911 unsigned int i;
912 FOR_EACH_VEC_SAFE_ELT (statics, i, decl)
913 if (TREE_CODE (decl) == FUNCTION_DECL
914 && DECL_INITIAL (decl) == 0
915 && DECL_EXTERNAL (decl)
916 && !TREE_PUBLIC (decl)
917 && !DECL_ARTIFICIAL (decl)
918 && !TREE_NO_WARNING (decl))
919 {
920 warning (OPT_Wunused_function,
921 "%q+F declared %<static%> but never defined", decl);
922 TREE_NO_WARNING (decl) = 1;
923 }
924 }
925
926 /* Write out any globals that need to be output. */
927 return wrapup_global_declarations (vec, len);
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
935 tree
create_implicit_typedef(tree name,tree type)936 create_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
954 static void
push_local_name(tree decl)955 push_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 if (!DECL_LANG_SPECIFIC (decl))
971 retrofit_lang_decl (decl);
972 DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
973 if (DECL_DISCRIMINATOR_SET_P (t))
974 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
975 else
976 DECL_DISCRIMINATOR (decl) = 1;
977
978 (*local_names)[i] = decl;
979 timevar_stop (TV_NAME_LOOKUP);
980 return;
981 }
982 }
983
984 vec_safe_push (local_names, decl);
985 timevar_stop (TV_NAME_LOOKUP);
986 }
987
988 /* Subroutine of duplicate_decls: return truthvalue of whether
989 or not types of these decls match.
990
991 For C++, we must compare the parameter list so that `int' can match
992 `int&' in a parameter position, but `int&' is not confused with
993 `const int&'. */
994
995 int
decls_match(tree newdecl,tree olddecl)996 decls_match (tree newdecl, tree olddecl)
997 {
998 int types_match;
999
1000 if (newdecl == olddecl)
1001 return 1;
1002
1003 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
1004 /* If the two DECLs are not even the same kind of thing, we're not
1005 interested in their types. */
1006 return 0;
1007
1008 gcc_assert (DECL_P (newdecl));
1009
1010 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1011 {
1012 tree f1 = TREE_TYPE (newdecl);
1013 tree f2 = TREE_TYPE (olddecl);
1014 tree p1 = TYPE_ARG_TYPES (f1);
1015 tree p2 = TYPE_ARG_TYPES (f2);
1016 tree r2;
1017
1018 /* Specializations of different templates are different functions
1019 even if they have the same type. */
1020 tree t1 = (DECL_USE_TEMPLATE (newdecl)
1021 ? DECL_TI_TEMPLATE (newdecl)
1022 : NULL_TREE);
1023 tree t2 = (DECL_USE_TEMPLATE (olddecl)
1024 ? DECL_TI_TEMPLATE (olddecl)
1025 : NULL_TREE);
1026 if (t1 != t2)
1027 return 0;
1028
1029 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1030 && ! (DECL_EXTERN_C_P (newdecl)
1031 && DECL_EXTERN_C_P (olddecl)))
1032 return 0;
1033
1034 /* A new declaration doesn't match a built-in one unless it
1035 is also extern "C". */
1036 if (DECL_IS_BUILTIN (olddecl)
1037 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
1038 return 0;
1039
1040 if (TREE_CODE (f1) != TREE_CODE (f2))
1041 return 0;
1042
1043 /* A declaration with deduced return type should use its pre-deduction
1044 type for declaration matching. */
1045 r2 = fndecl_declared_return_type (olddecl);
1046
1047 if (same_type_p (TREE_TYPE (f1), r2))
1048 {
1049 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
1050 && (DECL_BUILT_IN (olddecl)
1051 #ifndef NO_IMPLICIT_EXTERN_C
1052 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
1053 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
1054 #endif
1055 ))
1056 {
1057 types_match = self_promoting_args_p (p1);
1058 if (p1 == void_list_node)
1059 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1060 }
1061 #ifndef NO_IMPLICIT_EXTERN_C
1062 else if (!prototype_p (f1)
1063 && (DECL_EXTERN_C_P (olddecl)
1064 && DECL_IN_SYSTEM_HEADER (olddecl)
1065 && !DECL_CLASS_SCOPE_P (olddecl))
1066 && (DECL_EXTERN_C_P (newdecl)
1067 && DECL_IN_SYSTEM_HEADER (newdecl)
1068 && !DECL_CLASS_SCOPE_P (newdecl)))
1069 {
1070 types_match = self_promoting_args_p (p2);
1071 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
1072 }
1073 #endif
1074 else
1075 types_match =
1076 compparms (p1, p2)
1077 && type_memfn_rqual (f1) == type_memfn_rqual (f2)
1078 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
1079 || comp_type_attributes (TREE_TYPE (newdecl),
1080 TREE_TYPE (olddecl)) != 0);
1081 }
1082 else
1083 types_match = 0;
1084
1085 /* The decls dont match if they correspond to two different versions
1086 of the same function. Disallow extern "C" functions to be
1087 versions for now. */
1088 if (types_match
1089 && !DECL_EXTERN_C_P (newdecl)
1090 && !DECL_EXTERN_C_P (olddecl)
1091 && targetm.target_option.function_versions (newdecl, olddecl))
1092 {
1093 /* Mark functions as versions if necessary. Modify the mangled decl
1094 name if necessary. */
1095 if (DECL_FUNCTION_VERSIONED (newdecl)
1096 && DECL_FUNCTION_VERSIONED (olddecl))
1097 return 0;
1098 if (!DECL_FUNCTION_VERSIONED (newdecl))
1099 {
1100 DECL_FUNCTION_VERSIONED (newdecl) = 1;
1101 if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
1102 mangle_decl (newdecl);
1103 }
1104 if (!DECL_FUNCTION_VERSIONED (olddecl))
1105 {
1106 DECL_FUNCTION_VERSIONED (olddecl) = 1;
1107 if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
1108 mangle_decl (olddecl);
1109 }
1110 cgraph_node::record_function_versions (olddecl, newdecl);
1111 return 0;
1112 }
1113 }
1114 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1115 {
1116 tree oldres = DECL_TEMPLATE_RESULT (olddecl);
1117 tree newres = DECL_TEMPLATE_RESULT (newdecl);
1118
1119 if (TREE_CODE (newres) != TREE_CODE (oldres))
1120 return 0;
1121
1122 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1123 DECL_TEMPLATE_PARMS (olddecl)))
1124 return 0;
1125
1126 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1127 types_match = (same_type_p (TREE_TYPE (oldres), TREE_TYPE (newres))
1128 && equivalently_constrained (olddecl, newdecl));
1129 else
1130 // We don't need to check equivalently_constrained for variable and
1131 // function templates because we check it on the results.
1132 types_match = decls_match (oldres, newres);
1133 }
1134 else
1135 {
1136 /* Need to check scope for variable declaration (VAR_DECL).
1137 For typedef (TYPE_DECL), scope is ignored. */
1138 if (VAR_P (newdecl)
1139 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1140 /* [dcl.link]
1141 Two declarations for an object with C language linkage
1142 with the same name (ignoring the namespace that qualify
1143 it) that appear in different namespace scopes refer to
1144 the same object. */
1145 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1146 return 0;
1147
1148 if (TREE_TYPE (newdecl) == error_mark_node)
1149 types_match = TREE_TYPE (olddecl) == error_mark_node;
1150 else if (TREE_TYPE (olddecl) == NULL_TREE)
1151 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1152 else if (TREE_TYPE (newdecl) == NULL_TREE)
1153 types_match = 0;
1154 else
1155 types_match = comptypes (TREE_TYPE (newdecl),
1156 TREE_TYPE (olddecl),
1157 COMPARE_REDECLARATION);
1158 }
1159
1160 // Normal functions can be constrained, as can variable partial
1161 // specializations.
1162 if (types_match && VAR_OR_FUNCTION_DECL_P (newdecl))
1163 types_match = equivalently_constrained (newdecl, olddecl);
1164
1165 return types_match;
1166 }
1167
1168 /* If NEWDECL is `static' and an `extern' was seen previously,
1169 warn about it. OLDDECL is the previous declaration.
1170
1171 Note that this does not apply to the C++ case of declaring
1172 a variable `extern const' and then later `const'.
1173
1174 Don't complain about built-in functions, since they are beyond
1175 the user's control. */
1176
1177 void
warn_extern_redeclared_static(tree newdecl,tree olddecl)1178 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1179 {
1180 if (TREE_CODE (newdecl) == TYPE_DECL
1181 || TREE_CODE (newdecl) == TEMPLATE_DECL
1182 || TREE_CODE (newdecl) == CONST_DECL
1183 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1184 return;
1185
1186 /* Don't get confused by static member functions; that's a different
1187 use of `static'. */
1188 if (TREE_CODE (newdecl) == FUNCTION_DECL
1189 && DECL_STATIC_FUNCTION_P (newdecl))
1190 return;
1191
1192 /* If the old declaration was `static', or the new one isn't, then
1193 everything is OK. */
1194 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1195 return;
1196
1197 /* It's OK to declare a builtin function as `static'. */
1198 if (TREE_CODE (olddecl) == FUNCTION_DECL
1199 && DECL_ARTIFICIAL (olddecl))
1200 return;
1201
1202 if (permerror (DECL_SOURCE_LOCATION (newdecl),
1203 "%qD was declared %<extern%> and later %<static%>", newdecl))
1204 inform (DECL_SOURCE_LOCATION (olddecl),
1205 "previous declaration of %qD", olddecl);
1206 }
1207
1208 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1209 function templates. If their exception specifications do not
1210 match, issue a diagnostic. */
1211
1212 static void
check_redeclaration_exception_specification(tree new_decl,tree old_decl)1213 check_redeclaration_exception_specification (tree new_decl,
1214 tree old_decl)
1215 {
1216 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1217 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1218
1219 /* Two default specs are equivalent, don't force evaluation. */
1220 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions)
1221 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions))
1222 return;
1223
1224 maybe_instantiate_noexcept (new_decl);
1225 maybe_instantiate_noexcept (old_decl);
1226 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl));
1227 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl));
1228
1229 /* [except.spec]
1230
1231 If any declaration of a function has an exception-specification,
1232 all declarations, including the definition and an explicit
1233 specialization, of that function shall have an
1234 exception-specification with the same set of type-ids. */
1235 if (! DECL_IS_BUILTIN (old_decl)
1236 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
1237 {
1238 const char *msg
1239 = "declaration of %q+F has a different exception specifier";
1240 bool complained = true;
1241 if (DECL_IN_SYSTEM_HEADER (old_decl))
1242 complained = pedwarn (0, OPT_Wsystem_headers, msg, new_decl);
1243 else if (!flag_exceptions)
1244 /* We used to silently permit mismatched eh specs with
1245 -fno-exceptions, so make them a pedwarn now. */
1246 complained = pedwarn (0, OPT_Wpedantic, msg, new_decl);
1247 else
1248 error (msg, new_decl);
1249 if (complained)
1250 inform (0, "from previous declaration %q+F", old_decl);
1251 }
1252 }
1253
1254 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
1255 Otherwise issue diagnostics. */
1256
1257 static bool
validate_constexpr_redeclaration(tree old_decl,tree new_decl)1258 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
1259 {
1260 old_decl = STRIP_TEMPLATE (old_decl);
1261 new_decl = STRIP_TEMPLATE (new_decl);
1262 if (!VAR_OR_FUNCTION_DECL_P (old_decl)
1263 || !VAR_OR_FUNCTION_DECL_P (new_decl))
1264 return true;
1265 if (DECL_DECLARED_CONSTEXPR_P (old_decl)
1266 == DECL_DECLARED_CONSTEXPR_P (new_decl))
1267 return true;
1268 if (TREE_CODE (old_decl) == FUNCTION_DECL)
1269 {
1270 if (DECL_BUILT_IN (old_decl))
1271 {
1272 /* Hide a built-in declaration. */
1273 DECL_DECLARED_CONSTEXPR_P (old_decl)
1274 = DECL_DECLARED_CONSTEXPR_P (new_decl);
1275 return true;
1276 }
1277 /* 7.1.5 [dcl.constexpr]
1278 Note: An explicit specialization can differ from the template
1279 declaration with respect to the constexpr specifier. */
1280 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl)
1281 && DECL_TEMPLATE_SPECIALIZATION (new_decl))
1282 return true;
1283
1284 error ("redeclaration %q+D differs in %<constexpr%>", new_decl);
1285 error ("from previous declaration %q+D", old_decl);
1286 return false;
1287 }
1288 return true;
1289 }
1290
1291 // If OLDDECL and NEWDECL are concept declarations with the same type
1292 // (i.e., and template parameters), but different requirements,
1293 // emit diagnostics and return true. Otherwise, return false.
1294 static inline bool
check_concept_refinement(tree olddecl,tree newdecl)1295 check_concept_refinement (tree olddecl, tree newdecl)
1296 {
1297 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl))
1298 return false;
1299
1300 tree d1 = DECL_TEMPLATE_RESULT (olddecl);
1301 tree d2 = DECL_TEMPLATE_RESULT (newdecl);
1302 if (TREE_CODE (d1) != TREE_CODE (d2))
1303 return false;
1304
1305 tree t1 = TREE_TYPE (d1);
1306 tree t2 = TREE_TYPE (d2);
1307 if (TREE_CODE (d1) == FUNCTION_DECL)
1308 {
1309 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2))
1310 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl),
1311 DECL_TEMPLATE_PARMS (newdecl))
1312 && !equivalently_constrained (olddecl, newdecl))
1313 {
1314 error ("cannot specialize concept %q#D", olddecl);
1315 return true;
1316 }
1317 }
1318 return false;
1319 }
1320
1321 /* DECL is a redeclaration of a function or function template. If
1322 it does have default arguments issue a diagnostic. Note: this
1323 function is used to enforce the requirements in C++11 8.3.6 about
1324 no default arguments in redeclarations. */
1325
1326 static void
check_redeclaration_no_default_args(tree decl)1327 check_redeclaration_no_default_args (tree decl)
1328 {
1329 gcc_assert (DECL_DECLARES_FUNCTION_P (decl));
1330
1331 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl);
1332 t && t != void_list_node; t = TREE_CHAIN (t))
1333 if (TREE_PURPOSE (t))
1334 {
1335 permerror (DECL_SOURCE_LOCATION (decl),
1336 "redeclaration of %q#D may not have default "
1337 "arguments", decl);
1338 return;
1339 }
1340 }
1341
1342 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1343 && lookup_attribute ("gnu_inline", \
1344 DECL_ATTRIBUTES (fn)))
1345
1346 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1347 If the redeclaration is invalid, a diagnostic is issued, and the
1348 error_mark_node is returned. Otherwise, OLDDECL is returned.
1349
1350 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1351 returned.
1352
1353 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1354
1355 tree
duplicate_decls(tree newdecl,tree olddecl,bool newdecl_is_friend)1356 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1357 {
1358 unsigned olddecl_uid = DECL_UID (olddecl);
1359 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1360 int new_defines_function = 0;
1361 tree new_template_info;
1362
1363 if (newdecl == olddecl)
1364 return olddecl;
1365
1366 types_match = decls_match (newdecl, olddecl);
1367
1368 /* If either the type of the new decl or the type of the old decl is an
1369 error_mark_node, then that implies that we have already issued an
1370 error (earlier) for some bogus type specification, and in that case,
1371 it is rather pointless to harass the user with yet more error message
1372 about the same declaration, so just pretend the types match here. */
1373 if (TREE_TYPE (newdecl) == error_mark_node
1374 || TREE_TYPE (olddecl) == error_mark_node)
1375 return error_mark_node;
1376
1377 if (UDLIT_OPER_P (DECL_NAME (newdecl))
1378 && UDLIT_OPER_P (DECL_NAME (olddecl)))
1379 {
1380 if (TREE_CODE (newdecl) == TEMPLATE_DECL
1381 && TREE_CODE (olddecl) != TEMPLATE_DECL
1382 && check_raw_literal_operator (olddecl))
1383 error ("literal operator template %q+D conflicts with"
1384 " raw literal operator %qD", newdecl, olddecl);
1385 else if (TREE_CODE (newdecl) != TEMPLATE_DECL
1386 && TREE_CODE (olddecl) == TEMPLATE_DECL
1387 && check_raw_literal_operator (newdecl))
1388 error ("raw literal operator %q+D conflicts with"
1389 " literal operator template %qD", newdecl, olddecl);
1390 }
1391
1392 if (DECL_P (olddecl)
1393 && TREE_CODE (newdecl) == FUNCTION_DECL
1394 && TREE_CODE (olddecl) == FUNCTION_DECL
1395 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1396 {
1397 if (DECL_DECLARED_INLINE_P (newdecl)
1398 && DECL_UNINLINABLE (newdecl)
1399 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1400 /* Already warned elsewhere. */;
1401 else if (DECL_DECLARED_INLINE_P (olddecl)
1402 && DECL_UNINLINABLE (olddecl)
1403 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1404 /* Already warned. */;
1405 else if (DECL_DECLARED_INLINE_P (newdecl)
1406 && DECL_UNINLINABLE (olddecl)
1407 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1408 {
1409 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1410 OPT_Wattributes, "function %qD redeclared as inline",
1411 newdecl))
1412 inform (DECL_SOURCE_LOCATION (olddecl),
1413 "previous declaration of %qD with attribute noinline",
1414 olddecl);
1415 }
1416 else if (DECL_DECLARED_INLINE_P (olddecl)
1417 && DECL_UNINLINABLE (newdecl)
1418 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1419 {
1420 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1421 OPT_Wattributes, "function %qD redeclared with "
1422 "attribute noinline", newdecl))
1423 inform (DECL_SOURCE_LOCATION (olddecl),
1424 "previous declaration of %qD was inline",
1425 olddecl);
1426 }
1427 }
1428
1429 /* Check for redeclaration and other discrepancies. */
1430 if (TREE_CODE (olddecl) == FUNCTION_DECL
1431 && DECL_ARTIFICIAL (olddecl))
1432 {
1433 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1434 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1435 {
1436 /* Avoid warnings redeclaring built-ins which have not been
1437 explicitly declared. */
1438 if (DECL_ANTICIPATED (olddecl))
1439 return NULL_TREE;
1440
1441 /* If you declare a built-in or predefined function name as static,
1442 the old definition is overridden, but optionally warn this was a
1443 bad choice of name. */
1444 if (! TREE_PUBLIC (newdecl))
1445 {
1446 warning (OPT_Wshadow,
1447 DECL_BUILT_IN (olddecl)
1448 ? G_("shadowing built-in function %q#D")
1449 : G_("shadowing library function %q#D"), olddecl);
1450 /* Discard the old built-in function. */
1451 return NULL_TREE;
1452 }
1453 /* If the built-in is not ansi, then programs can override
1454 it even globally without an error. */
1455 else if (! DECL_BUILT_IN (olddecl))
1456 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1457 "library function %q#D redeclared as non-function %q#D",
1458 olddecl, newdecl);
1459 else
1460 error ("declaration of %q+#D conflicts with built-in "
1461 "declaration %q#D", newdecl, olddecl);
1462 return NULL_TREE;
1463 }
1464 else if (DECL_OMP_DECLARE_REDUCTION_P (olddecl))
1465 {
1466 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (newdecl));
1467 error_at (DECL_SOURCE_LOCATION (newdecl),
1468 "redeclaration of %<pragma omp declare reduction%>");
1469 inform (DECL_SOURCE_LOCATION (olddecl),
1470 "previous %<pragma omp declare reduction%> declaration");
1471 return error_mark_node;
1472 }
1473 else if (!types_match)
1474 {
1475 /* Avoid warnings redeclaring built-ins which have not been
1476 explicitly declared. */
1477 if (DECL_ANTICIPATED (olddecl))
1478 {
1479 /* Deal with fileptr_type_node. FILE type is not known
1480 at the time we create the builtins. */
1481 tree t1, t2;
1482
1483 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1484 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1485 t1 || t2;
1486 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1487 if (!t1 || !t2)
1488 break;
1489 else if (TREE_VALUE (t2) == fileptr_type_node)
1490 {
1491 tree t = TREE_VALUE (t1);
1492
1493 if (TYPE_PTR_P (t)
1494 && TYPE_IDENTIFIER (TREE_TYPE (t))
1495 == get_identifier ("FILE")
1496 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1497 {
1498 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1499
1500 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1501 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1502 types_match = decls_match (newdecl, olddecl);
1503 if (types_match)
1504 return duplicate_decls (newdecl, olddecl,
1505 newdecl_is_friend);
1506 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1507 }
1508 }
1509 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1510 break;
1511 }
1512 else if ((DECL_EXTERN_C_P (newdecl)
1513 && DECL_EXTERN_C_P (olddecl))
1514 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1515 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1516 {
1517 /* A near match; override the builtin. */
1518
1519 if (TREE_PUBLIC (newdecl))
1520 warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1521 "new declaration %q#D ambiguates built-in "
1522 "declaration %q#D", newdecl, olddecl);
1523 else
1524 warning (OPT_Wshadow,
1525 DECL_BUILT_IN (olddecl)
1526 ? G_("shadowing built-in function %q#D")
1527 : G_("shadowing library function %q#D"), olddecl);
1528 }
1529 else
1530 /* Discard the old built-in function. */
1531 return NULL_TREE;
1532
1533 /* Replace the old RTL to avoid problems with inlining. */
1534 COPY_DECL_RTL (newdecl, olddecl);
1535 }
1536 /* Even if the types match, prefer the new declarations type for
1537 built-ins which have not been explicitly declared, for
1538 exception lists, etc... */
1539 else if (DECL_IS_BUILTIN (olddecl))
1540 {
1541 tree type = TREE_TYPE (newdecl);
1542 tree attribs = (*targetm.merge_type_attributes)
1543 (TREE_TYPE (olddecl), type);
1544
1545 type = cp_build_type_attribute_variant (type, attribs);
1546 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1547 }
1548
1549 /* If a function is explicitly declared "throw ()", propagate that to
1550 the corresponding builtin. */
1551 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1552 && DECL_ANTICIPATED (olddecl)
1553 && TREE_NOTHROW (newdecl)
1554 && !TREE_NOTHROW (olddecl))
1555 {
1556 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
1557 tree tmpdecl = builtin_decl_explicit (fncode);
1558 if (tmpdecl && tmpdecl != olddecl && types_match)
1559 TREE_NOTHROW (tmpdecl) = 1;
1560 }
1561
1562 /* Whether or not the builtin can throw exceptions has no
1563 bearing on this declarator. */
1564 TREE_NOTHROW (olddecl) = 0;
1565
1566 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1567 {
1568 /* If a builtin function is redeclared as `static', merge
1569 the declarations, but make the original one static. */
1570 DECL_THIS_STATIC (olddecl) = 1;
1571 TREE_PUBLIC (olddecl) = 0;
1572
1573 /* Make the old declaration consistent with the new one so
1574 that all remnants of the builtin-ness of this function
1575 will be banished. */
1576 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1577 COPY_DECL_RTL (newdecl, olddecl);
1578 }
1579 }
1580 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1581 {
1582 /* C++ Standard, 3.3, clause 4:
1583 "[Note: a namespace name or a class template name must be unique
1584 in its declarative region (7.3.2, clause 14). ]" */
1585 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1586 && TREE_CODE (newdecl) != NAMESPACE_DECL
1587 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1588 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1589 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1590 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1591 {
1592 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1593 && TREE_CODE (newdecl) != TYPE_DECL)
1594 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1595 && TREE_CODE (olddecl) != TYPE_DECL))
1596 {
1597 /* We do nothing special here, because C++ does such nasty
1598 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1599 get shadowed, and know that if we need to find a TYPE_DECL
1600 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1601 slot of the identifier. */
1602 return NULL_TREE;
1603 }
1604
1605 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1606 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1607 || (TREE_CODE (olddecl) == FUNCTION_DECL
1608 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1609 return NULL_TREE;
1610 }
1611
1612 error ("%q#D redeclared as different kind of symbol", newdecl);
1613 if (TREE_CODE (olddecl) == TREE_LIST)
1614 olddecl = TREE_VALUE (olddecl);
1615 inform (DECL_SOURCE_LOCATION (olddecl),
1616 "previous declaration %q#D", olddecl);
1617
1618 return error_mark_node;
1619 }
1620 else if (!types_match)
1621 {
1622 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1623 /* These are certainly not duplicate declarations; they're
1624 from different scopes. */
1625 return NULL_TREE;
1626
1627 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1628 {
1629 /* The name of a class template may not be declared to refer to
1630 any other template, class, function, object, namespace, value,
1631 or type in the same scope. */
1632 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1633 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1634 {
1635 error ("conflicting declaration of template %q+#D", newdecl);
1636 inform (DECL_SOURCE_LOCATION (olddecl),
1637 "previous declaration %q#D", olddecl);
1638 return error_mark_node;
1639 }
1640 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1641 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1642 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1643 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1644 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1645 DECL_TEMPLATE_PARMS (olddecl))
1646 /* Template functions can be disambiguated by
1647 return type. */
1648 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1649 TREE_TYPE (TREE_TYPE (olddecl)))
1650 // Template functions can also be disambiguated by
1651 // constraints.
1652 && equivalently_constrained (olddecl, newdecl))
1653 {
1654 error ("ambiguating new declaration %q+#D", newdecl);
1655 inform (DECL_SOURCE_LOCATION (olddecl),
1656 "old declaration %q#D", olddecl);
1657 }
1658 else if (check_concept_refinement (olddecl, newdecl))
1659 return error_mark_node;
1660 return NULL_TREE;
1661 }
1662 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1663 {
1664 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1665 {
1666 error ("conflicting declaration of C function %q+#D",
1667 newdecl);
1668 inform (DECL_SOURCE_LOCATION (olddecl),
1669 "previous declaration %q#D", olddecl);
1670 return NULL_TREE;
1671 }
1672 /* For function versions, params and types match, but they
1673 are not ambiguous. */
1674 else if ((!DECL_FUNCTION_VERSIONED (newdecl)
1675 && !DECL_FUNCTION_VERSIONED (olddecl))
1676 // The functions have the same parameter types.
1677 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1678 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))
1679 // And the same constraints.
1680 && equivalently_constrained (newdecl, olddecl))
1681 {
1682 error ("ambiguating new declaration of %q+#D", newdecl);
1683 inform (DECL_SOURCE_LOCATION (olddecl),
1684 "old declaration %q#D", olddecl);
1685 return error_mark_node;
1686 }
1687 else
1688 return NULL_TREE;
1689 }
1690 else
1691 {
1692 error ("conflicting declaration %q+#D", newdecl);
1693 inform (DECL_SOURCE_LOCATION (olddecl),
1694 "previous declaration as %q#D", olddecl);
1695 return error_mark_node;
1696 }
1697 }
1698 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1699 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1700 && (!DECL_TEMPLATE_INFO (newdecl)
1701 || (DECL_TI_TEMPLATE (newdecl)
1702 != DECL_TI_TEMPLATE (olddecl))))
1703 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1704 && (!DECL_TEMPLATE_INFO (olddecl)
1705 || (DECL_TI_TEMPLATE (olddecl)
1706 != DECL_TI_TEMPLATE (newdecl))))))
1707 /* It's OK to have a template specialization and a non-template
1708 with the same type, or to have specializations of two
1709 different templates with the same type. Note that if one is a
1710 specialization, and the other is an instantiation of the same
1711 template, that we do not exit at this point. That situation
1712 can occur if we instantiate a template class, and then
1713 specialize one of its methods. This situation is valid, but
1714 the declarations must be merged in the usual way. */
1715 return NULL_TREE;
1716 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1717 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1718 && !DECL_USE_TEMPLATE (newdecl))
1719 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1720 && !DECL_USE_TEMPLATE (olddecl))))
1721 /* One of the declarations is a template instantiation, and the
1722 other is not a template at all. That's OK. */
1723 return NULL_TREE;
1724 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1725 {
1726 /* In [namespace.alias] we have:
1727
1728 In a declarative region, a namespace-alias-definition can be
1729 used to redefine a namespace-alias declared in that declarative
1730 region to refer only to the namespace to which it already
1731 refers.
1732
1733 Therefore, if we encounter a second alias directive for the same
1734 alias, we can just ignore the second directive. */
1735 if (DECL_NAMESPACE_ALIAS (newdecl)
1736 && (DECL_NAMESPACE_ALIAS (newdecl)
1737 == DECL_NAMESPACE_ALIAS (olddecl)))
1738 return olddecl;
1739 /* [namespace.alias]
1740
1741 A namespace-name or namespace-alias shall not be declared as
1742 the name of any other entity in the same declarative region.
1743 A namespace-name defined at global scope shall not be
1744 declared as the name of any other entity in any global scope
1745 of the program. */
1746 error ("conflicting declaration of namespace %q+D", newdecl);
1747 inform (DECL_SOURCE_LOCATION (olddecl),
1748 "previous declaration of namespace %qD here", olddecl);
1749 return error_mark_node;
1750 }
1751 else
1752 {
1753 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1754 if (errmsg)
1755 {
1756 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1757 if (DECL_NAME (olddecl) != NULL_TREE)
1758 inform (DECL_SOURCE_LOCATION (olddecl),
1759 (DECL_INITIAL (olddecl) && namespace_bindings_p ())
1760 ? G_("%q#D previously defined here")
1761 : G_("%q#D previously declared here"), olddecl);
1762 return error_mark_node;
1763 }
1764 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1765 && DECL_INITIAL (olddecl) != NULL_TREE
1766 && !prototype_p (TREE_TYPE (olddecl))
1767 && prototype_p (TREE_TYPE (newdecl)))
1768 {
1769 /* Prototype decl follows defn w/o prototype. */
1770 if (warning_at (DECL_SOURCE_LOCATION (newdecl), 0,
1771 "prototype specified for %q#D", newdecl))
1772 inform (DECL_SOURCE_LOCATION (olddecl),
1773 "previous non-prototype definition here");
1774 }
1775 else if (VAR_OR_FUNCTION_DECL_P (olddecl)
1776 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1777 {
1778 /* [dcl.link]
1779 If two declarations of the same function or object
1780 specify different linkage-specifications ..., the program
1781 is ill-formed.... Except for functions with C++ linkage,
1782 a function declaration without a linkage specification
1783 shall not precede the first linkage specification for
1784 that function. A function can be declared without a
1785 linkage specification after an explicit linkage
1786 specification has been seen; the linkage explicitly
1787 specified in the earlier declaration is not affected by
1788 such a function declaration.
1789
1790 DR 563 raises the question why the restrictions on
1791 functions should not also apply to objects. Older
1792 versions of G++ silently ignore the linkage-specification
1793 for this example:
1794
1795 namespace N {
1796 extern int i;
1797 extern "C" int i;
1798 }
1799
1800 which is clearly wrong. Therefore, we now treat objects
1801 like functions. */
1802 if (current_lang_depth () == 0)
1803 {
1804 /* There is no explicit linkage-specification, so we use
1805 the linkage from the previous declaration. */
1806 if (!DECL_LANG_SPECIFIC (newdecl))
1807 retrofit_lang_decl (newdecl);
1808 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1809 }
1810 else
1811 {
1812 error ("conflicting declaration of %q+#D with %qL linkage",
1813 newdecl, DECL_LANGUAGE (newdecl));
1814 inform (DECL_SOURCE_LOCATION (olddecl),
1815 "previous declaration with %qL linkage",
1816 DECL_LANGUAGE (olddecl));
1817 }
1818 }
1819
1820 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1821 ;
1822 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1823 {
1824 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */
1825 if (DECL_FUNCTION_MEMBER_P (olddecl)
1826 && (/* grokfndecl passes member function templates too
1827 as FUNCTION_DECLs. */
1828 DECL_TEMPLATE_INFO (olddecl)
1829 /* C++11 8.3.6/6.
1830 Default arguments for a member function of a class
1831 template shall be specified on the initial declaration
1832 of the member function within the class template. */
1833 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl))))
1834 check_redeclaration_no_default_args (newdecl);
1835 else
1836 {
1837 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl);
1838 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl);
1839 int i = 1;
1840
1841 for (; t1 && t1 != void_list_node;
1842 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1843 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1844 {
1845 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1846 TREE_PURPOSE (t2)))
1847 {
1848 if (permerror (input_location,
1849 "default argument given for parameter "
1850 "%d of %q#D", i, newdecl))
1851 inform (DECL_SOURCE_LOCATION (olddecl),
1852 "previous specification in %q#D here",
1853 olddecl);
1854 }
1855 else
1856 {
1857 error ("default argument given for parameter %d "
1858 "of %q#D", i, newdecl);
1859 inform (DECL_SOURCE_LOCATION (olddecl),
1860 "previous specification in %q#D here",
1861 olddecl);
1862 }
1863 }
1864 }
1865 }
1866 }
1867
1868 /* Do not merge an implicit typedef with an explicit one. In:
1869
1870 class A;
1871 ...
1872 typedef class A A __attribute__ ((foo));
1873
1874 the attribute should apply only to the typedef. */
1875 if (TREE_CODE (olddecl) == TYPE_DECL
1876 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1877 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1878 return NULL_TREE;
1879
1880 /* If new decl is `static' and an `extern' was seen previously,
1881 warn about it. */
1882 warn_extern_redeclared_static (newdecl, olddecl);
1883
1884 if (!validate_constexpr_redeclaration (olddecl, newdecl))
1885 return error_mark_node;
1886
1887 /* We have committed to returning 1 at this point. */
1888 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1889 {
1890 /* Now that functions must hold information normally held
1891 by field decls, there is extra work to do so that
1892 declaration information does not get destroyed during
1893 definition. */
1894 if (DECL_VINDEX (olddecl))
1895 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1896 if (DECL_CONTEXT (olddecl))
1897 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1898 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1899 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1900 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1901 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1902 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1903 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl);
1904 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl);
1905 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1906 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1907 SET_OVERLOADED_OPERATOR_CODE
1908 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1909 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1910
1911 /* Optionally warn about more than one declaration for the same
1912 name, but don't warn about a function declaration followed by a
1913 definition. */
1914 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1915 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1916 /* Don't warn about extern decl followed by definition. */
1917 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1918 /* Don't warn about friends, let add_friend take care of it. */
1919 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
1920 /* Don't warn about declaration followed by specialization. */
1921 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
1922 || DECL_TEMPLATE_SPECIALIZATION (olddecl)))
1923 {
1924 if (warning_at (DECL_SOURCE_LOCATION (newdecl),
1925 OPT_Wredundant_decls,
1926 "redundant redeclaration of %qD in same scope",
1927 newdecl))
1928 inform (DECL_SOURCE_LOCATION (olddecl),
1929 "previous declaration of %qD", olddecl);
1930 }
1931
1932 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
1933 && DECL_TEMPLATE_SPECIALIZATION (newdecl)))
1934 {
1935 if (DECL_DELETED_FN (newdecl))
1936 {
1937 error ("deleted definition of %q+D", newdecl);
1938 inform (DECL_SOURCE_LOCATION (olddecl),
1939 "previous declaration of %qD", olddecl);
1940 }
1941 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
1942 }
1943 }
1944
1945 /* Deal with C++: must preserve virtual function table size. */
1946 if (TREE_CODE (olddecl) == TYPE_DECL)
1947 {
1948 tree newtype = TREE_TYPE (newdecl);
1949 tree oldtype = TREE_TYPE (olddecl);
1950
1951 if (newtype != error_mark_node && oldtype != error_mark_node
1952 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1953 CLASSTYPE_FRIEND_CLASSES (newtype)
1954 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1955
1956 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1957 }
1958
1959 /* Copy all the DECL_... slots specified in the new decl
1960 except for any that we copy here from the old type. */
1961 DECL_ATTRIBUTES (newdecl)
1962 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1963
1964 if (DECL_DECLARES_FUNCTION_P (olddecl) && DECL_DECLARES_FUNCTION_P (newdecl))
1965 {
1966 olddecl_friend = DECL_FRIEND_P (olddecl);
1967 hidden_friend = (DECL_ANTICIPATED (olddecl)
1968 && DECL_HIDDEN_FRIEND_P (olddecl)
1969 && newdecl_is_friend);
1970 if (!hidden_friend)
1971 {
1972 DECL_ANTICIPATED (olddecl) = 0;
1973 DECL_HIDDEN_FRIEND_P (olddecl) = 0;
1974 }
1975 }
1976
1977 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1978 {
1979 tree old_result;
1980 tree new_result;
1981 old_result = DECL_TEMPLATE_RESULT (olddecl);
1982 new_result = DECL_TEMPLATE_RESULT (newdecl);
1983 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1984 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1985 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1986 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1987
1988 DECL_ATTRIBUTES (old_result)
1989 = (*targetm.merge_decl_attributes) (old_result, new_result);
1990
1991 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1992 {
1993 /* Per C++11 8.3.6/4, default arguments cannot be added in later
1994 declarations of a function template. */
1995 if (DECL_SOURCE_LOCATION (newdecl)
1996 != DECL_SOURCE_LOCATION (olddecl))
1997 check_redeclaration_no_default_args (newdecl);
1998
1999 check_default_args (newdecl);
2000
2001 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
2002 && DECL_INITIAL (new_result))
2003 {
2004 if (DECL_INITIAL (old_result))
2005 DECL_UNINLINABLE (old_result) = 1;
2006 else
2007 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
2008 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
2009 DECL_NOT_REALLY_EXTERN (old_result)
2010 = DECL_NOT_REALLY_EXTERN (new_result);
2011 DECL_INTERFACE_KNOWN (old_result)
2012 = DECL_INTERFACE_KNOWN (new_result);
2013 DECL_DECLARED_INLINE_P (old_result)
2014 = DECL_DECLARED_INLINE_P (new_result);
2015 DECL_DISREGARD_INLINE_LIMITS (old_result)
2016 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2017
2018 }
2019 else
2020 {
2021 DECL_DECLARED_INLINE_P (old_result)
2022 |= DECL_DECLARED_INLINE_P (new_result);
2023 DECL_DISREGARD_INLINE_LIMITS (old_result)
2024 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
2025 check_redeclaration_exception_specification (newdecl, olddecl);
2026 }
2027 }
2028
2029 /* If the new declaration is a definition, update the file and
2030 line information on the declaration, and also make
2031 the old declaration the same definition. */
2032 if (DECL_INITIAL (new_result) != NULL_TREE)
2033 {
2034 DECL_SOURCE_LOCATION (olddecl)
2035 = DECL_SOURCE_LOCATION (old_result)
2036 = DECL_SOURCE_LOCATION (newdecl);
2037 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
2038 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
2039 {
2040 tree parm;
2041 DECL_ARGUMENTS (old_result)
2042 = DECL_ARGUMENTS (new_result);
2043 for (parm = DECL_ARGUMENTS (old_result); parm;
2044 parm = DECL_CHAIN (parm))
2045 DECL_CONTEXT (parm) = old_result;
2046 }
2047 }
2048
2049 return olddecl;
2050 }
2051
2052 if (types_match)
2053 {
2054 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2055 check_redeclaration_exception_specification (newdecl, olddecl);
2056
2057 /* Automatically handles default parameters. */
2058 tree oldtype = TREE_TYPE (olddecl);
2059 tree newtype;
2060
2061 /* For typedefs use the old type, as the new type's DECL_NAME points
2062 at newdecl, which will be ggc_freed. */
2063 if (TREE_CODE (newdecl) == TYPE_DECL)
2064 {
2065 /* But NEWTYPE might have an attribute, honor that. */
2066 tree tem = TREE_TYPE (newdecl);
2067 newtype = oldtype;
2068
2069 if (TYPE_USER_ALIGN (tem))
2070 {
2071 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype))
2072 TYPE_ALIGN (newtype) = TYPE_ALIGN (tem);
2073 TYPE_USER_ALIGN (newtype) = true;
2074 }
2075
2076 /* And remove the new type from the variants list. */
2077 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl)
2078 {
2079 tree remove = TREE_TYPE (newdecl);
2080 for (tree t = TYPE_MAIN_VARIANT (remove); ;
2081 t = TYPE_NEXT_VARIANT (t))
2082 if (TYPE_NEXT_VARIANT (t) == remove)
2083 {
2084 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove);
2085 break;
2086 }
2087 }
2088 }
2089 else
2090 /* Merge the data types specified in the two decls. */
2091 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2092
2093 if (VAR_P (newdecl))
2094 {
2095 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2096 /* For already initialized vars, TREE_READONLY could have been
2097 cleared in cp_finish_decl, because the var needs runtime
2098 initialization or destruction. Make sure not to set
2099 TREE_READONLY on it again. */
2100 if (DECL_INITIALIZED_P (olddecl)
2101 && !DECL_EXTERNAL (olddecl)
2102 && !TREE_READONLY (olddecl))
2103 TREE_READONLY (newdecl) = 0;
2104 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
2105 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
2106 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
2107 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
2108 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
2109
2110 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
2111 if (DECL_LANG_SPECIFIC (olddecl)
2112 && CP_DECL_THREADPRIVATE_P (olddecl))
2113 {
2114 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
2115 if (!DECL_LANG_SPECIFIC (newdecl))
2116 retrofit_lang_decl (newdecl);
2117
2118 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
2119 }
2120 }
2121
2122 /* An explicit specialization of a function template or of a member
2123 function of a class template can be declared transaction_safe
2124 independently of whether the corresponding template entity is declared
2125 transaction_safe. */
2126 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL
2127 && DECL_TEMPLATE_INSTANTIATION (olddecl)
2128 && DECL_TEMPLATE_SPECIALIZATION (newdecl)
2129 && tx_safe_fn_type_p (newtype)
2130 && !tx_safe_fn_type_p (TREE_TYPE (newdecl)))
2131 newtype = tx_unsafe_fn_variant (newtype);
2132
2133 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2134
2135 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2136 check_default_args (newdecl);
2137
2138 /* Lay the type out, unless already done. */
2139 if (! same_type_p (newtype, oldtype)
2140 && TREE_TYPE (newdecl) != error_mark_node
2141 && !(processing_template_decl && uses_template_parms (newdecl)))
2142 layout_type (TREE_TYPE (newdecl));
2143
2144 if ((VAR_P (newdecl)
2145 || TREE_CODE (newdecl) == PARM_DECL
2146 || TREE_CODE (newdecl) == RESULT_DECL
2147 || TREE_CODE (newdecl) == FIELD_DECL
2148 || TREE_CODE (newdecl) == TYPE_DECL)
2149 && !(processing_template_decl && uses_template_parms (newdecl)))
2150 layout_decl (newdecl, 0);
2151
2152 /* Merge the type qualifiers. */
2153 if (TREE_READONLY (newdecl))
2154 TREE_READONLY (olddecl) = 1;
2155 if (TREE_THIS_VOLATILE (newdecl))
2156 TREE_THIS_VOLATILE (olddecl) = 1;
2157 if (TREE_NOTHROW (newdecl))
2158 TREE_NOTHROW (olddecl) = 1;
2159
2160 /* Merge deprecatedness. */
2161 if (TREE_DEPRECATED (newdecl))
2162 TREE_DEPRECATED (olddecl) = 1;
2163
2164 /* Preserve function specific target and optimization options */
2165 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2166 {
2167 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2168 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2169 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2170 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2171
2172 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2173 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2174 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2175 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2176 }
2177
2178 /* Merge the initialization information. */
2179 if (DECL_INITIAL (newdecl) == NULL_TREE
2180 && DECL_INITIAL (olddecl) != NULL_TREE)
2181 {
2182 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2183 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2184 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2185 {
2186 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2187 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2188 }
2189 }
2190
2191 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2192 {
2193 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2194 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2195 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2196 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2197 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
2198 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2199 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2200 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2201 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2202 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
2203 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
2204 /* Keep the old RTL. */
2205 COPY_DECL_RTL (olddecl, newdecl);
2206 }
2207 else if (VAR_P (newdecl)
2208 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
2209 {
2210 /* Keep the old RTL. We cannot keep the old RTL if the old
2211 declaration was for an incomplete object and the new
2212 declaration is not since many attributes of the RTL will
2213 change. */
2214 COPY_DECL_RTL (olddecl, newdecl);
2215 }
2216 }
2217 /* If cannot merge, then use the new type and qualifiers,
2218 and don't preserve the old rtl. */
2219 else
2220 {
2221 /* Clean out any memory we had of the old declaration. */
2222 tree oldstatic = value_member (olddecl, static_aggregates);
2223 if (oldstatic)
2224 TREE_VALUE (oldstatic) = error_mark_node;
2225
2226 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2227 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2228 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2229 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2230 }
2231
2232 /* Merge the storage class information. */
2233 merge_weak (newdecl, olddecl);
2234
2235 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
2236 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2237 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2238 if (! DECL_EXTERNAL (olddecl))
2239 DECL_EXTERNAL (newdecl) = 0;
2240 if (! DECL_COMDAT (olddecl))
2241 DECL_COMDAT (newdecl) = 0;
2242
2243 new_template_info = NULL_TREE;
2244 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
2245 {
2246 bool new_redefines_gnu_inline = false;
2247
2248 if (new_defines_function
2249 && ((DECL_INTERFACE_KNOWN (olddecl)
2250 && TREE_CODE (olddecl) == FUNCTION_DECL)
2251 || (TREE_CODE (olddecl) == TEMPLATE_DECL
2252 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2253 == FUNCTION_DECL))))
2254 {
2255 tree fn = olddecl;
2256
2257 if (TREE_CODE (fn) == TEMPLATE_DECL)
2258 fn = DECL_TEMPLATE_RESULT (olddecl);
2259
2260 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
2261 }
2262
2263 if (!new_redefines_gnu_inline)
2264 {
2265 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2266 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2267 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
2268 }
2269 DECL_TEMPLATE_INSTANTIATED (newdecl)
2270 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
2271 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
2272
2273 /* If the OLDDECL is an instantiation and/or specialization,
2274 then the NEWDECL must be too. But, it may not yet be marked
2275 as such if the caller has created NEWDECL, but has not yet
2276 figured out that it is a redeclaration. */
2277 if (!DECL_USE_TEMPLATE (newdecl))
2278 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2279
2280 /* Don't really know how much of the language-specific
2281 values we should copy from old to new. */
2282 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2283 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
2284 DECL_INITIALIZED_IN_CLASS_P (newdecl)
2285 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
2286
2287 if (LANG_DECL_HAS_MIN (newdecl))
2288 {
2289 DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
2290 DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
2291 if (DECL_TEMPLATE_INFO (newdecl))
2292 {
2293 new_template_info = DECL_TEMPLATE_INFO (newdecl);
2294 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2295 && DECL_TEMPLATE_SPECIALIZATION (newdecl))
2296 /* Remember the presence of explicit specialization args. */
2297 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl))
2298 = TINFO_USED_TEMPLATE_ID (new_template_info);
2299 }
2300 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2301 }
2302 /* Only functions have these fields. */
2303 if (DECL_DECLARES_FUNCTION_P (newdecl))
2304 {
2305 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2306 DECL_BEFRIENDING_CLASSES (newdecl)
2307 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
2308 DECL_BEFRIENDING_CLASSES (olddecl));
2309 /* DECL_THUNKS is only valid for virtual functions,
2310 otherwise it is a DECL_FRIEND_CONTEXT. */
2311 if (DECL_VIRTUAL_P (newdecl))
2312 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
2313 }
2314 /* Only variables have this field. */
2315 else if (VAR_P (newdecl)
2316 && VAR_HAD_UNKNOWN_BOUND (olddecl))
2317 SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
2318 }
2319
2320 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2321 {
2322 tree parm;
2323
2324 /* Merge parameter attributes. */
2325 tree oldarg, newarg;
2326 for (oldarg = DECL_ARGUMENTS(olddecl),
2327 newarg = DECL_ARGUMENTS(newdecl);
2328 oldarg && newarg;
2329 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
2330 DECL_ATTRIBUTES (newarg)
2331 = (*targetm.merge_decl_attributes) (oldarg, newarg);
2332 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
2333 }
2334
2335 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
2336 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
2337 {
2338 /* If newdecl is not a specialization, then it is not a
2339 template-related function at all. And that means that we
2340 should have exited above, returning 0. */
2341 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
2342
2343 if (DECL_ODR_USED (olddecl))
2344 /* From [temp.expl.spec]:
2345
2346 If a template, a member template or the member of a class
2347 template is explicitly specialized then that
2348 specialization shall be declared before the first use of
2349 that specialization that would cause an implicit
2350 instantiation to take place, in every translation unit in
2351 which such a use occurs. */
2352 error ("explicit specialization of %qD after first use",
2353 olddecl);
2354
2355 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
2356 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl)
2357 && DECL_DECLARED_INLINE_P (newdecl));
2358
2359 /* Don't propagate visibility from the template to the
2360 specialization here. We'll do that in determine_visibility if
2361 appropriate. */
2362 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
2363
2364 /* [temp.expl.spec/14] We don't inline explicit specialization
2365 just because the primary template says so. */
2366
2367 /* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
2368 the always_inline attribute. */
2369 if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
2370 && !DECL_DISREGARD_INLINE_LIMITS (newdecl))
2371 {
2372 if (DECL_DECLARED_INLINE_P (newdecl))
2373 DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
2374 else
2375 DECL_ATTRIBUTES (newdecl)
2376 = remove_attribute ("always_inline",
2377 DECL_ATTRIBUTES (newdecl));
2378 }
2379 }
2380 else if (new_defines_function && DECL_INITIAL (olddecl))
2381 {
2382 /* Never inline re-defined extern inline functions.
2383 FIXME: this could be better handled by keeping both
2384 function as separate declarations. */
2385 DECL_UNINLINABLE (newdecl) = 1;
2386 }
2387 else
2388 {
2389 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
2390 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2391
2392 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
2393
2394 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2395 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2396
2397 DECL_DISREGARD_INLINE_LIMITS (newdecl)
2398 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2399 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2400 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2401 }
2402
2403 /* Preserve abstractness on cloned [cd]tors. */
2404 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl);
2405
2406 /* Update newdecl's parms to point at olddecl. */
2407 for (parm = DECL_ARGUMENTS (newdecl); parm;
2408 parm = DECL_CHAIN (parm))
2409 DECL_CONTEXT (parm) = olddecl;
2410
2411 if (! types_match)
2412 {
2413 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2414 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
2415 COPY_DECL_RTL (newdecl, olddecl);
2416 }
2417 if (! types_match || new_defines_function)
2418 {
2419 /* These need to be copied so that the names are available.
2420 Note that if the types do match, we'll preserve inline
2421 info and other bits, but if not, we won't. */
2422 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2423 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2424 }
2425 /* If redeclaring a builtin function, it stays built in
2426 if newdecl is a gnu_inline definition, or if newdecl is just
2427 a declaration. */
2428 if (DECL_BUILT_IN (olddecl)
2429 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
2430 {
2431 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2432 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2433 /* If we're keeping the built-in definition, keep the rtl,
2434 regardless of declaration matches. */
2435 COPY_DECL_RTL (olddecl, newdecl);
2436 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2437 {
2438 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2439 switch (fncode)
2440 {
2441 /* If a compatible prototype of these builtin functions
2442 is seen, assume the runtime implements it with the
2443 expected semantics. */
2444 case BUILT_IN_STPCPY:
2445 if (builtin_decl_explicit_p (fncode))
2446 set_builtin_decl_implicit_p (fncode, true);
2447 break;
2448 default:
2449 if (builtin_decl_explicit_p (fncode))
2450 set_builtin_decl_declared_p (fncode, true);
2451 break;
2452 }
2453 }
2454 }
2455 if (new_defines_function)
2456 /* If defining a function declared with other language
2457 linkage, use the previously declared language linkage. */
2458 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2459 else if (types_match)
2460 {
2461 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2462 /* Don't clear out the arguments if we're just redeclaring a
2463 function. */
2464 if (DECL_ARGUMENTS (olddecl))
2465 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2466 }
2467 }
2468 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2469 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2470
2471 /* Now preserve various other info from the definition. */
2472 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2473 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2474 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2475 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2476
2477 /* Warn about conflicting visibility specifications. */
2478 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2479 && DECL_VISIBILITY_SPECIFIED (newdecl)
2480 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2481 {
2482 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wattributes,
2483 "%qD: visibility attribute ignored because it "
2484 "conflicts with previous declaration", newdecl))
2485 inform (DECL_SOURCE_LOCATION (olddecl),
2486 "previous declaration of %qD", olddecl);
2487 }
2488 /* Choose the declaration which specified visibility. */
2489 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2490 {
2491 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2492 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2493 }
2494 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2495 so keep this behavior. */
2496 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl))
2497 {
2498 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2499 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2500 }
2501 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
2502 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2503 {
2504 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2505 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2506 }
2507 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2508 if (TREE_CODE (newdecl) == FIELD_DECL)
2509 DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
2510
2511 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2512 with that from NEWDECL below. */
2513 if (DECL_LANG_SPECIFIC (olddecl))
2514 {
2515 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2516 != DECL_LANG_SPECIFIC (newdecl));
2517 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2518 }
2519
2520 /* Merge the USED information. */
2521 if (TREE_USED (olddecl))
2522 TREE_USED (newdecl) = 1;
2523 else if (TREE_USED (newdecl))
2524 TREE_USED (olddecl) = 1;
2525 if (VAR_P (newdecl))
2526 {
2527 if (DECL_READ_P (olddecl))
2528 DECL_READ_P (newdecl) = 1;
2529 else if (DECL_READ_P (newdecl))
2530 DECL_READ_P (olddecl) = 1;
2531 }
2532 if (DECL_PRESERVE_P (olddecl))
2533 DECL_PRESERVE_P (newdecl) = 1;
2534 else if (DECL_PRESERVE_P (newdecl))
2535 DECL_PRESERVE_P (olddecl) = 1;
2536
2537 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
2538 to olddecl and deleted. */
2539 if (TREE_CODE (newdecl) == FUNCTION_DECL
2540 && DECL_FUNCTION_VERSIONED (olddecl))
2541 {
2542 /* Set the flag for newdecl so that it gets copied to olddecl. */
2543 DECL_FUNCTION_VERSIONED (newdecl) = 1;
2544 /* newdecl will be purged after copying to olddecl and is no longer
2545 a version. */
2546 cgraph_node::delete_function_version (newdecl);
2547 }
2548
2549 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2550 {
2551 int function_size;
2552 struct symtab_node *snode = symtab_node::get (olddecl);
2553
2554 function_size = sizeof (struct tree_decl_common);
2555
2556 memcpy ((char *) olddecl + sizeof (struct tree_common),
2557 (char *) newdecl + sizeof (struct tree_common),
2558 function_size - sizeof (struct tree_common));
2559
2560 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2561 (char *) newdecl + sizeof (struct tree_decl_common),
2562 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2563
2564 /* Preserve symtab node mapping. */
2565 olddecl->decl_with_vis.symtab_node = snode;
2566
2567 if (new_template_info)
2568 /* If newdecl is a template instantiation, it is possible that
2569 the following sequence of events has occurred:
2570
2571 o A friend function was declared in a class template. The
2572 class template was instantiated.
2573
2574 o The instantiation of the friend declaration was
2575 recorded on the instantiation list, and is newdecl.
2576
2577 o Later, however, instantiate_class_template called pushdecl
2578 on the newdecl to perform name injection. But, pushdecl in
2579 turn called duplicate_decls when it discovered that another
2580 declaration of a global function with the same name already
2581 existed.
2582
2583 o Here, in duplicate_decls, we decided to clobber newdecl.
2584
2585 If we're going to do that, we'd better make sure that
2586 olddecl, and not newdecl, is on the list of
2587 instantiations so that if we try to do the instantiation
2588 again we won't get the clobbered declaration. */
2589 reregister_specialization (newdecl,
2590 new_template_info,
2591 olddecl);
2592 }
2593 else
2594 {
2595 size_t size = tree_code_size (TREE_CODE (newdecl));
2596
2597 memcpy ((char *) olddecl + sizeof (struct tree_common),
2598 (char *) newdecl + sizeof (struct tree_common),
2599 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2600 switch (TREE_CODE (newdecl))
2601 {
2602 case LABEL_DECL:
2603 case VAR_DECL:
2604 case RESULT_DECL:
2605 case PARM_DECL:
2606 case FIELD_DECL:
2607 case TYPE_DECL:
2608 case CONST_DECL:
2609 {
2610 struct symtab_node *snode = NULL;
2611
2612 if (VAR_P (newdecl)
2613 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl)
2614 || DECL_EXTERNAL (olddecl)))
2615 snode = symtab_node::get (olddecl);
2616 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2617 (char *) newdecl + sizeof (struct tree_decl_common),
2618 size - sizeof (struct tree_decl_common)
2619 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2620 if (VAR_P (newdecl))
2621 olddecl->decl_with_vis.symtab_node = snode;
2622 }
2623 break;
2624 default:
2625 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2626 (char *) newdecl + sizeof (struct tree_decl_common),
2627 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2628 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2629 break;
2630 }
2631 }
2632
2633 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2634 {
2635 if (DECL_EXTERNAL (olddecl)
2636 || TREE_PUBLIC (olddecl)
2637 || TREE_STATIC (olddecl))
2638 {
2639 /* Merge the section attribute.
2640 We want to issue an error if the sections conflict but that must be
2641 done later in decl_attributes since we are called before attributes
2642 are assigned. */
2643 if (DECL_SECTION_NAME (newdecl) != NULL)
2644 set_decl_section_name (olddecl, DECL_SECTION_NAME (newdecl));
2645
2646 if (DECL_ONE_ONLY (newdecl))
2647 {
2648 struct symtab_node *oldsym, *newsym;
2649 if (TREE_CODE (olddecl) == FUNCTION_DECL)
2650 oldsym = cgraph_node::get_create (olddecl);
2651 else
2652 oldsym = varpool_node::get_create (olddecl);
2653 newsym = symtab_node::get (newdecl);
2654 oldsym->set_comdat_group (newsym->get_comdat_group ());
2655 }
2656 }
2657
2658 if (VAR_P (newdecl)
2659 && CP_DECL_THREAD_LOCAL_P (newdecl))
2660 {
2661 CP_DECL_THREAD_LOCAL_P (olddecl) = true;
2662 if (!processing_template_decl)
2663 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
2664 }
2665 }
2666
2667 DECL_UID (olddecl) = olddecl_uid;
2668 if (olddecl_friend)
2669 DECL_FRIEND_P (olddecl) = 1;
2670 if (hidden_friend)
2671 {
2672 DECL_ANTICIPATED (olddecl) = 1;
2673 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2674 }
2675
2676 /* NEWDECL contains the merged attribute lists.
2677 Update OLDDECL to be the same. */
2678 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2679
2680 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2681 so that encode_section_info has a chance to look at the new decl
2682 flags and attributes. */
2683 if (DECL_RTL_SET_P (olddecl)
2684 && (TREE_CODE (olddecl) == FUNCTION_DECL
2685 || (VAR_P (olddecl)
2686 && TREE_STATIC (olddecl))))
2687 make_decl_rtl (olddecl);
2688
2689 /* The NEWDECL will no longer be needed. Because every out-of-class
2690 declaration of a member results in a call to duplicate_decls,
2691 freeing these nodes represents in a significant savings.
2692
2693 Before releasing the node, be sore to remove function from symbol
2694 table that might have been inserted there to record comdat group.
2695 Be sure to however do not free DECL_STRUCT_FUNCTION because this
2696 structure is shared in between newdecl and oldecl. */
2697 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2698 DECL_STRUCT_FUNCTION (newdecl) = NULL;
2699 if (VAR_OR_FUNCTION_DECL_P (newdecl))
2700 {
2701 struct symtab_node *snode = symtab_node::get (newdecl);
2702 if (snode)
2703 snode->remove ();
2704 }
2705
2706 /* Remove the associated constraints for newdecl, if any, before
2707 reclaiming memory. */
2708 if (flag_concepts)
2709 remove_constraints (newdecl);
2710
2711 ggc_free (newdecl);
2712
2713 return olddecl;
2714 }
2715
2716 /* Return zero if the declaration NEWDECL is valid
2717 when the declaration OLDDECL (assumed to be for the same name)
2718 has already been seen.
2719 Otherwise return an error message format string with a %s
2720 where the identifier should go. */
2721
2722 static const char *
redeclaration_error_message(tree newdecl,tree olddecl)2723 redeclaration_error_message (tree newdecl, tree olddecl)
2724 {
2725 if (TREE_CODE (newdecl) == TYPE_DECL)
2726 {
2727 /* Because C++ can put things into name space for free,
2728 constructs like "typedef struct foo { ... } foo"
2729 would look like an erroneous redeclaration. */
2730 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2731 return NULL;
2732 else
2733 return G_("redefinition of %q#D");
2734 }
2735 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2736 {
2737 /* If this is a pure function, its olddecl will actually be
2738 the original initialization to `0' (which we force to call
2739 abort()). Don't complain about redefinition in this case. */
2740 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2741 && DECL_INITIAL (olddecl) == NULL_TREE)
2742 return NULL;
2743
2744 /* If both functions come from different namespaces, this is not
2745 a redeclaration - this is a conflict with a used function. */
2746 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2747 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2748 && ! decls_match (olddecl, newdecl))
2749 return G_("%qD conflicts with used function");
2750
2751 /* We'll complain about linkage mismatches in
2752 warn_extern_redeclared_static. */
2753
2754 /* Defining the same name twice is no good. */
2755 if (DECL_INITIAL (olddecl) != NULL_TREE
2756 && DECL_INITIAL (newdecl) != NULL_TREE)
2757 {
2758 if (DECL_NAME (olddecl) == NULL_TREE)
2759 return G_("%q#D not declared in class");
2760 else if (!GNU_INLINE_P (olddecl)
2761 || GNU_INLINE_P (newdecl))
2762 return G_("redefinition of %q#D");
2763 }
2764
2765 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2766 {
2767 bool olda = GNU_INLINE_P (olddecl);
2768 bool newa = GNU_INLINE_P (newdecl);
2769
2770 if (olda != newa)
2771 {
2772 if (newa)
2773 return G_("%q+D redeclared inline with "
2774 "%<gnu_inline%> attribute");
2775 else
2776 return G_("%q+D redeclared inline without "
2777 "%<gnu_inline%> attribute");
2778 }
2779 }
2780
2781 check_abi_tag_redeclaration
2782 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
2783 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
2784
2785 return NULL;
2786 }
2787 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2788 {
2789 tree nt, ot;
2790
2791 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2792 {
2793 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2794 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2795 return G_("redefinition of %q#D");
2796 return NULL;
2797 }
2798
2799 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2800 || (DECL_TEMPLATE_RESULT (newdecl)
2801 == DECL_TEMPLATE_RESULT (olddecl)))
2802 return NULL;
2803
2804 nt = DECL_TEMPLATE_RESULT (newdecl);
2805 if (DECL_TEMPLATE_INFO (nt))
2806 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2807 ot = DECL_TEMPLATE_RESULT (olddecl);
2808 if (DECL_TEMPLATE_INFO (ot))
2809 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2810 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2811 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2812 return G_("redefinition of %q#D");
2813
2814 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2815 {
2816 bool olda = GNU_INLINE_P (ot);
2817 bool newa = GNU_INLINE_P (nt);
2818
2819 if (olda != newa)
2820 {
2821 if (newa)
2822 return G_("%q+D redeclared inline with "
2823 "%<gnu_inline%> attribute");
2824 else
2825 return G_("%q+D redeclared inline without "
2826 "%<gnu_inline%> attribute");
2827 }
2828 }
2829
2830 /* Core issue #226 (C++0x):
2831
2832 If a friend function template declaration specifies a
2833 default template-argument, that declaration shall be a
2834 definition and shall be the only declaration of the
2835 function template in the translation unit. */
2836 if ((cxx_dialect != cxx98)
2837 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2838 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2839 /*is_primary=*/true,
2840 /*is_partial=*/false,
2841 /*is_friend_decl=*/2))
2842 return G_("redeclaration of friend %q#D "
2843 "may not have default template arguments");
2844
2845 return NULL;
2846 }
2847 else if (VAR_P (newdecl)
2848 && CP_DECL_THREAD_LOCAL_P (newdecl) != CP_DECL_THREAD_LOCAL_P (olddecl)
2849 && (! DECL_LANG_SPECIFIC (olddecl)
2850 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2851 || CP_DECL_THREAD_LOCAL_P (newdecl)))
2852 {
2853 /* Only variables can be thread-local, and all declarations must
2854 agree on this property. */
2855 if (CP_DECL_THREAD_LOCAL_P (newdecl))
2856 return G_("thread-local declaration of %q#D follows "
2857 "non-thread-local declaration");
2858 else
2859 return G_("non-thread-local declaration of %q#D follows "
2860 "thread-local declaration");
2861 }
2862 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2863 {
2864 /* The objects have been declared at namespace scope. If either
2865 is a member of an anonymous union, then this is an invalid
2866 redeclaration. For example:
2867
2868 int i;
2869 union { int i; };
2870
2871 is invalid. */
2872 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl))
2873 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl)))
2874 return G_("redeclaration of %q#D");
2875 /* If at least one declaration is a reference, there is no
2876 conflict. For example:
2877
2878 int i = 3;
2879 extern int i;
2880
2881 is valid. */
2882 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2883 return NULL;
2884 /* Reject two definitions. */
2885 return G_("redefinition of %q#D");
2886 }
2887 else
2888 {
2889 /* Objects declared with block scope: */
2890 /* Reject two definitions, and reject a definition
2891 together with an external reference. */
2892 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2893 return G_("redeclaration of %q#D");
2894 return NULL;
2895 }
2896 }
2897
2898 /* Hash and equality functions for the named_label table. */
2899
2900 hashval_t
hash(named_label_entry * ent)2901 named_label_hasher::hash (named_label_entry *ent)
2902 {
2903 return DECL_UID (ent->label_decl);
2904 }
2905
2906 bool
equal(named_label_entry * a,named_label_entry * b)2907 named_label_hasher::equal (named_label_entry *a, named_label_entry *b)
2908 {
2909 return a->label_decl == b->label_decl;
2910 }
2911
2912 /* Create a new label, named ID. */
2913
2914 static tree
make_label_decl(tree id,int local_p)2915 make_label_decl (tree id, int local_p)
2916 {
2917 struct named_label_entry *ent;
2918 tree decl;
2919
2920 decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
2921
2922 DECL_CONTEXT (decl) = current_function_decl;
2923 DECL_MODE (decl) = VOIDmode;
2924 C_DECLARED_LABEL_FLAG (decl) = local_p;
2925
2926 /* Say where one reference is to the label, for the sake of the
2927 error if it is not defined. */
2928 DECL_SOURCE_LOCATION (decl) = input_location;
2929
2930 /* Record the fact that this identifier is bound to this label. */
2931 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2932
2933 /* Create the label htab for the function on demand. */
2934 if (!named_labels)
2935 named_labels = hash_table<named_label_hasher>::create_ggc (13);
2936
2937 /* Record this label on the list of labels used in this function.
2938 We do this before calling make_label_decl so that we get the
2939 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2940 ent = ggc_cleared_alloc<named_label_entry> ();
2941 ent->label_decl = decl;
2942
2943 named_label_entry **slot = named_labels->find_slot (ent, INSERT);
2944 gcc_assert (*slot == NULL);
2945 *slot = ent;
2946
2947 return decl;
2948 }
2949
2950 /* Look for a label named ID in the current function. If one cannot
2951 be found, create one. (We keep track of used, but undefined,
2952 labels, and complain about them at the end of a function.) */
2953
2954 static tree
lookup_label_1(tree id)2955 lookup_label_1 (tree id)
2956 {
2957 tree decl;
2958
2959 /* You can't use labels at global scope. */
2960 if (current_function_decl == NULL_TREE)
2961 {
2962 error ("label %qE referenced outside of any function", id);
2963 return NULL_TREE;
2964 }
2965
2966 /* See if we've already got this label. */
2967 decl = IDENTIFIER_LABEL_VALUE (id);
2968 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2969 return decl;
2970
2971 decl = make_label_decl (id, /*local_p=*/0);
2972 return decl;
2973 }
2974
2975 /* Wrapper for lookup_label_1. */
2976
2977 tree
lookup_label(tree id)2978 lookup_label (tree id)
2979 {
2980 tree ret;
2981 bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2982 ret = lookup_label_1 (id);
2983 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2984 return ret;
2985 }
2986
2987 /* Declare a local label named ID. */
2988
2989 tree
declare_local_label(tree id)2990 declare_local_label (tree id)
2991 {
2992 tree decl;
2993 cp_label_binding bind;
2994
2995 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2996 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2997 bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
2998
2999 decl = make_label_decl (id, /*local_p=*/1);
3000 bind.label = decl;
3001 vec_safe_push (current_binding_level->shadowed_labels, bind);
3002
3003 return decl;
3004 }
3005
3006 /* Returns nonzero if it is ill-formed to jump past the declaration of
3007 DECL. Returns 2 if it's also a real problem. */
3008
3009 static int
decl_jump_unsafe(tree decl)3010 decl_jump_unsafe (tree decl)
3011 {
3012 /* [stmt.dcl]/3: A program that jumps from a point where a local variable
3013 with automatic storage duration is not in scope to a point where it is
3014 in scope is ill-formed unless the variable has scalar type, class type
3015 with a trivial default constructor and a trivial destructor, a
3016 cv-qualified version of one of these types, or an array of one of the
3017 preceding types and is declared without an initializer (8.5). */
3018 tree type = TREE_TYPE (decl);
3019
3020 if (!VAR_P (decl) || TREE_STATIC (decl)
3021 || type == error_mark_node)
3022 return 0;
3023
3024 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl)
3025 || variably_modified_type_p (type, NULL_TREE))
3026 return 2;
3027
3028 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3029 return 1;
3030
3031 return 0;
3032 }
3033
3034 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch
3035 to the user. */
3036
3037 static bool
identify_goto(tree decl,location_t loc,const location_t * locus,diagnostic_t diag_kind)3038 identify_goto (tree decl, location_t loc, const location_t *locus,
3039 diagnostic_t diag_kind)
3040 {
3041 bool complained
3042 = (decl ? emit_diagnostic (diag_kind, loc, 0, "jump to label %qD", decl)
3043 : emit_diagnostic (diag_kind, loc, 0, "jump to case label"));
3044 if (complained && locus)
3045 inform (*locus, " from here");
3046 return complained;
3047 }
3048
3049 /* Check that a single previously seen jump to a newly defined label
3050 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
3051 the jump context; NAMES are the names in scope in LEVEL at the jump
3052 context; LOCUS is the source position of the jump or 0. Returns
3053 true if all is well. */
3054
3055 static bool
check_previous_goto_1(tree decl,cp_binding_level * level,tree names,bool exited_omp,const location_t * locus)3056 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
3057 bool exited_omp, const location_t *locus)
3058 {
3059 cp_binding_level *b;
3060 bool complained = false;
3061 int identified = 0;
3062 bool saw_eh = false, saw_omp = false, saw_tm = false;
3063
3064 if (exited_omp)
3065 {
3066 complained = identify_goto (decl, input_location, locus, DK_ERROR);
3067 if (complained)
3068 inform (input_location, " exits OpenMP structured block");
3069 saw_omp = true;
3070 identified = 2;
3071 }
3072
3073 for (b = current_binding_level; b ; b = b->level_chain)
3074 {
3075 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
3076
3077 for (new_decls = b->names; new_decls != old_decls;
3078 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
3079 : TREE_CHAIN (new_decls)))
3080 {
3081 int problem = decl_jump_unsafe (new_decls);
3082 if (! problem)
3083 continue;
3084
3085 if (!identified)
3086 {
3087 complained = identify_goto (decl, input_location, locus,
3088 DK_PERMERROR);
3089 identified = 1;
3090 }
3091 if (complained)
3092 {
3093 if (problem > 1)
3094 inform (DECL_SOURCE_LOCATION (new_decls),
3095 " crosses initialization of %q#D", new_decls);
3096 else
3097 inform (DECL_SOURCE_LOCATION (new_decls),
3098 " enters scope of %q#D which has "
3099 "non-trivial destructor", new_decls);
3100 }
3101 }
3102
3103 if (b == level)
3104 break;
3105 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
3106 {
3107 if (identified < 2)
3108 {
3109 complained = identify_goto (decl, input_location, locus,
3110 DK_ERROR);
3111 identified = 2;
3112 }
3113 if (complained)
3114 {
3115 if (b->kind == sk_try)
3116 inform (input_location, " enters try block");
3117 else
3118 inform (input_location, " enters catch block");
3119 }
3120 saw_eh = true;
3121 }
3122 if (b->kind == sk_omp && !saw_omp)
3123 {
3124 if (identified < 2)
3125 {
3126 complained = identify_goto (decl, input_location, locus,
3127 DK_ERROR);
3128 identified = 2;
3129 }
3130 if (complained)
3131 inform (input_location, " enters OpenMP structured block");
3132 saw_omp = true;
3133 }
3134 if (b->kind == sk_transaction && !saw_tm)
3135 {
3136 if (identified < 2)
3137 {
3138 complained = identify_goto (decl, input_location, locus,
3139 DK_ERROR);
3140 identified = 2;
3141 }
3142 if (complained)
3143 inform (input_location,
3144 " enters synchronized or atomic statement");
3145 saw_tm = true;
3146 }
3147 }
3148
3149 return !identified;
3150 }
3151
3152 static void
check_previous_goto(tree decl,struct named_label_use_entry * use)3153 check_previous_goto (tree decl, struct named_label_use_entry *use)
3154 {
3155 check_previous_goto_1 (decl, use->binding_level,
3156 use->names_in_scope, use->in_omp_scope,
3157 &use->o_goto_locus);
3158 }
3159
3160 static bool
check_switch_goto(cp_binding_level * level)3161 check_switch_goto (cp_binding_level* level)
3162 {
3163 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
3164 }
3165
3166 /* Check that a new jump to a label DECL is OK. Called by
3167 finish_goto_stmt. */
3168
3169 void
check_goto(tree decl)3170 check_goto (tree decl)
3171 {
3172 struct named_label_entry *ent, dummy;
3173 bool saw_catch = false, complained = false;
3174 int identified = 0;
3175 tree bad;
3176 unsigned ix;
3177
3178 /* We can't know where a computed goto is jumping.
3179 So we assume that it's OK. */
3180 if (TREE_CODE (decl) != LABEL_DECL)
3181 return;
3182
3183 /* We didn't record any information about this label when we created it,
3184 and there's not much point since it's trivial to analyze as a return. */
3185 if (decl == cdtor_label)
3186 return;
3187
3188 dummy.label_decl = decl;
3189 ent = named_labels->find (&dummy);
3190 gcc_assert (ent != NULL);
3191
3192 /* If the label hasn't been defined yet, defer checking. */
3193 if (! DECL_INITIAL (decl))
3194 {
3195 struct named_label_use_entry *new_use;
3196
3197 /* Don't bother creating another use if the last goto had the
3198 same data, and will therefore create the same set of errors. */
3199 if (ent->uses
3200 && ent->uses->names_in_scope == current_binding_level->names)
3201 return;
3202
3203 new_use = ggc_alloc<named_label_use_entry> ();
3204 new_use->binding_level = current_binding_level;
3205 new_use->names_in_scope = current_binding_level->names;
3206 new_use->o_goto_locus = input_location;
3207 new_use->in_omp_scope = false;
3208
3209 new_use->next = ent->uses;
3210 ent->uses = new_use;
3211 return;
3212 }
3213
3214 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope
3215 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
3216 {
3217 diagnostic_t diag_kind = DK_PERMERROR;
3218 if (ent->in_try_scope || ent->in_catch_scope
3219 || ent->in_transaction_scope || ent->in_omp_scope)
3220 diag_kind = DK_ERROR;
3221 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3222 &input_location, diag_kind);
3223 identified = 1 + (diag_kind == DK_ERROR);
3224 }
3225
3226 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
3227 {
3228 int u = decl_jump_unsafe (bad);
3229
3230 if (u > 1 && DECL_ARTIFICIAL (bad))
3231 {
3232 /* Can't skip init of __exception_info. */
3233 if (identified == 1)
3234 {
3235 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl),
3236 &input_location, DK_ERROR);
3237 identified = 2;
3238 }
3239 if (complained)
3240 inform (DECL_SOURCE_LOCATION (bad), " enters catch block");
3241 saw_catch = true;
3242 }
3243 else if (complained)
3244 {
3245 if (u > 1)
3246 inform (DECL_SOURCE_LOCATION (bad),
3247 " skips initialization of %q#D", bad);
3248 else
3249 inform (DECL_SOURCE_LOCATION (bad),
3250 " enters scope of %q#D which has "
3251 "non-trivial destructor", bad);
3252 }
3253 }
3254
3255 if (complained)
3256 {
3257 if (ent->in_try_scope)
3258 inform (input_location, " enters try block");
3259 else if (ent->in_catch_scope && !saw_catch)
3260 inform (input_location, " enters catch block");
3261 else if (ent->in_transaction_scope)
3262 inform (input_location, " enters synchronized or atomic statement");
3263 }
3264
3265 if (ent->in_omp_scope)
3266 {
3267 if (complained)
3268 inform (input_location, " enters OpenMP structured block");
3269 }
3270 else if (flag_openmp)
3271 {
3272 cp_binding_level *b;
3273 for (b = current_binding_level; b ; b = b->level_chain)
3274 {
3275 if (b == ent->binding_level)
3276 break;
3277 if (b->kind == sk_omp)
3278 {
3279 if (identified < 2)
3280 {
3281 complained = identify_goto (decl,
3282 DECL_SOURCE_LOCATION (decl),
3283 &input_location, DK_ERROR);
3284 identified = 2;
3285 }
3286 if (complained)
3287 inform (input_location, " exits OpenMP structured block");
3288 break;
3289 }
3290 }
3291 }
3292 }
3293
3294 /* Check that a return is ok wrt OpenMP structured blocks.
3295 Called by finish_return_stmt. Returns true if all is well. */
3296
3297 bool
check_omp_return(void)3298 check_omp_return (void)
3299 {
3300 cp_binding_level *b;
3301 for (b = current_binding_level; b ; b = b->level_chain)
3302 if (b->kind == sk_omp)
3303 {
3304 error ("invalid exit from OpenMP structured block");
3305 return false;
3306 }
3307 else if (b->kind == sk_function_parms)
3308 break;
3309 return true;
3310 }
3311
3312 /* Define a label, specifying the location in the source file.
3313 Return the LABEL_DECL node for the label. */
3314
3315 static tree
define_label_1(location_t location,tree name)3316 define_label_1 (location_t location, tree name)
3317 {
3318 struct named_label_entry *ent, dummy;
3319 cp_binding_level *p;
3320 tree decl;
3321
3322 decl = lookup_label (name);
3323
3324 dummy.label_decl = decl;
3325 ent = named_labels->find (&dummy);
3326 gcc_assert (ent != NULL);
3327
3328 /* After labels, make any new cleanups in the function go into their
3329 own new (temporary) binding contour. */
3330 for (p = current_binding_level;
3331 p->kind != sk_function_parms;
3332 p = p->level_chain)
3333 p->more_cleanups_ok = 0;
3334
3335 if (name == get_identifier ("wchar_t"))
3336 permerror (input_location, "label named wchar_t");
3337
3338 if (DECL_INITIAL (decl) != NULL_TREE)
3339 {
3340 error ("duplicate label %qD", decl);
3341 return error_mark_node;
3342 }
3343 else
3344 {
3345 struct named_label_use_entry *use;
3346
3347 /* Mark label as having been defined. */
3348 DECL_INITIAL (decl) = error_mark_node;
3349 /* Say where in the source. */
3350 DECL_SOURCE_LOCATION (decl) = location;
3351
3352 ent->binding_level = current_binding_level;
3353 ent->names_in_scope = current_binding_level->names;
3354
3355 for (use = ent->uses; use ; use = use->next)
3356 check_previous_goto (decl, use);
3357 ent->uses = NULL;
3358 }
3359
3360 return decl;
3361 }
3362
3363 /* Wrapper for define_label_1. */
3364
3365 tree
define_label(location_t location,tree name)3366 define_label (location_t location, tree name)
3367 {
3368 tree ret;
3369 bool running = timevar_cond_start (TV_NAME_LOOKUP);
3370 ret = define_label_1 (location, name);
3371 timevar_cond_stop (TV_NAME_LOOKUP, running);
3372 return ret;
3373 }
3374
3375
3376 struct cp_switch
3377 {
3378 cp_binding_level *level;
3379 struct cp_switch *next;
3380 /* The SWITCH_STMT being built. */
3381 tree switch_stmt;
3382 /* A splay-tree mapping the low element of a case range to the high
3383 element, or NULL_TREE if there is no high element. Used to
3384 determine whether or not a new case label duplicates an old case
3385 label. We need a tree, rather than simply a hash table, because
3386 of the GNU case range extension. */
3387 splay_tree cases;
3388 /* Remember whether there was a case value that is outside the
3389 range of the original type of the controlling expression. */
3390 bool outside_range_p;
3391 };
3392
3393 /* A stack of the currently active switch statements. The innermost
3394 switch statement is on the top of the stack. There is no need to
3395 mark the stack for garbage collection because it is only active
3396 during the processing of the body of a function, and we never
3397 collect at that point. */
3398
3399 static struct cp_switch *switch_stack;
3400
3401 /* Called right after a switch-statement condition is parsed.
3402 SWITCH_STMT is the switch statement being parsed. */
3403
3404 void
push_switch(tree switch_stmt)3405 push_switch (tree switch_stmt)
3406 {
3407 struct cp_switch *p = XNEW (struct cp_switch);
3408 p->level = current_binding_level;
3409 p->next = switch_stack;
3410 p->switch_stmt = switch_stmt;
3411 p->cases = splay_tree_new (case_compare, NULL, NULL);
3412 p->outside_range_p = false;
3413 switch_stack = p;
3414 }
3415
3416 void
pop_switch(void)3417 pop_switch (void)
3418 {
3419 struct cp_switch *cs = switch_stack;
3420 location_t switch_location;
3421
3422 /* Emit warnings as needed. */
3423 switch_location = EXPR_LOC_OR_LOC (cs->switch_stmt, input_location);
3424 const bool bool_cond_p
3425 = (SWITCH_STMT_TYPE (cs->switch_stmt)
3426 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE);
3427 if (!processing_template_decl)
3428 c_do_switch_warnings (cs->cases, switch_location,
3429 SWITCH_STMT_TYPE (cs->switch_stmt),
3430 SWITCH_STMT_COND (cs->switch_stmt),
3431 bool_cond_p, cs->outside_range_p);
3432
3433 splay_tree_delete (cs->cases);
3434 switch_stack = switch_stack->next;
3435 free (cs);
3436 }
3437
3438 /* Convert a case constant VALUE in a switch to the type TYPE of the switch
3439 condition. Note that if TYPE and VALUE are already integral we don't
3440 really do the conversion because the language-independent
3441 warning/optimization code will work better that way. */
3442
3443 static tree
case_conversion(tree type,tree value)3444 case_conversion (tree type, tree value)
3445 {
3446 if (value == NULL_TREE)
3447 return value;
3448
3449 if (cxx_dialect >= cxx11
3450 && (SCOPED_ENUM_P (type)
3451 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
3452 {
3453 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
3454 type = type_promotes_to (type);
3455 value = (perform_implicit_conversion_flags
3456 (type, value, tf_warning_or_error,
3457 LOOKUP_IMPLICIT | LOOKUP_NO_NON_INTEGRAL));
3458 }
3459 return cxx_constant_value (value);
3460 }
3461
3462 /* Note that we've seen a definition of a case label, and complain if this
3463 is a bad place for one. */
3464
3465 tree
finish_case_label(location_t loc,tree low_value,tree high_value)3466 finish_case_label (location_t loc, tree low_value, tree high_value)
3467 {
3468 tree cond, r;
3469 cp_binding_level *p;
3470 tree type;
3471
3472 if (processing_template_decl)
3473 {
3474 tree label;
3475
3476 /* For templates, just add the case label; we'll do semantic
3477 analysis at instantiation-time. */
3478 label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
3479 return add_stmt (build_case_label (low_value, high_value, label));
3480 }
3481
3482 /* Find the condition on which this switch statement depends. */
3483 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
3484 if (cond && TREE_CODE (cond) == TREE_LIST)
3485 cond = TREE_VALUE (cond);
3486
3487 if (!check_switch_goto (switch_stack->level))
3488 return error_mark_node;
3489
3490 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
3491
3492 low_value = case_conversion (type, low_value);
3493 high_value = case_conversion (type, high_value);
3494
3495 r = c_add_case_label (loc, switch_stack->cases, cond, type,
3496 low_value, high_value,
3497 &switch_stack->outside_range_p);
3498
3499 /* After labels, make any new cleanups in the function go into their
3500 own new (temporary) binding contour. */
3501 for (p = current_binding_level;
3502 p->kind != sk_function_parms;
3503 p = p->level_chain)
3504 p->more_cleanups_ok = 0;
3505
3506 return r;
3507 }
3508
3509 struct typename_info {
3510 tree scope;
3511 tree name;
3512 tree template_id;
3513 bool enum_p;
3514 bool class_p;
3515 };
3516
3517 struct typename_hasher : ggc_ptr_hash<tree_node>
3518 {
3519 typedef typename_info *compare_type;
3520
3521 /* Hash a TYPENAME_TYPE. */
3522
3523 static hashval_t
hashtypename_hasher3524 hash (tree t)
3525 {
3526 hashval_t hash;
3527
3528 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
3529 ^ htab_hash_pointer (TYPE_IDENTIFIER (t)));
3530
3531 return hash;
3532 }
3533
3534 /* Compare two TYPENAME_TYPEs. */
3535
3536 static bool
equaltypename_hasher3537 equal (tree t1, const typename_info *t2)
3538 {
3539 return (TYPE_IDENTIFIER (t1) == t2->name
3540 && TYPE_CONTEXT (t1) == t2->scope
3541 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
3542 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
3543 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
3544 }
3545 };
3546
3547 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
3548 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
3549
3550 Returns the new TYPENAME_TYPE. */
3551
3552 static GTY (()) hash_table<typename_hasher> *typename_htab;
3553
3554 static tree
build_typename_type(tree context,tree name,tree fullname,enum tag_types tag_type)3555 build_typename_type (tree context, tree name, tree fullname,
3556 enum tag_types tag_type)
3557 {
3558 tree t;
3559 tree d;
3560 typename_info ti;
3561 tree *e;
3562 hashval_t hash;
3563
3564 if (typename_htab == NULL)
3565 typename_htab = hash_table<typename_hasher>::create_ggc (61);
3566
3567 ti.scope = FROB_CONTEXT (context);
3568 ti.name = name;
3569 ti.template_id = fullname;
3570 ti.enum_p = tag_type == enum_type;
3571 ti.class_p = (tag_type == class_type
3572 || tag_type == record_type
3573 || tag_type == union_type);
3574 hash = (htab_hash_pointer (ti.scope)
3575 ^ htab_hash_pointer (ti.name));
3576
3577 /* See if we already have this type. */
3578 e = typename_htab->find_slot_with_hash (&ti, hash, INSERT);
3579 if (*e)
3580 t = *e;
3581 else
3582 {
3583 /* Build the TYPENAME_TYPE. */
3584 t = cxx_make_type (TYPENAME_TYPE);
3585 TYPE_CONTEXT (t) = ti.scope;
3586 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
3587 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
3588 TYPENAME_IS_CLASS_P (t) = ti.class_p;
3589
3590 /* Build the corresponding TYPE_DECL. */
3591 d = build_decl (input_location, TYPE_DECL, name, t);
3592 TYPE_NAME (TREE_TYPE (d)) = d;
3593 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3594 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3595 DECL_ARTIFICIAL (d) = 1;
3596
3597 /* Store it in the hash table. */
3598 *e = t;
3599
3600 /* TYPENAME_TYPEs must always be compared structurally, because
3601 they may or may not resolve down to another type depending on
3602 the currently open classes. */
3603 SET_TYPE_STRUCTURAL_EQUALITY (t);
3604 }
3605
3606 return t;
3607 }
3608
3609 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
3610 provided to name the type. Returns an appropriate type, unless an
3611 error occurs, in which case error_mark_node is returned. If we
3612 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
3613 return that, rather than the _TYPE it corresponds to, in other
3614 cases we look through the type decl. If TF_ERROR is set, complain
3615 about errors, otherwise be quiet. */
3616
3617 tree
make_typename_type(tree context,tree name,enum tag_types tag_type,tsubst_flags_t complain)3618 make_typename_type (tree context, tree name, enum tag_types tag_type,
3619 tsubst_flags_t complain)
3620 {
3621 tree fullname;
3622 tree t;
3623 bool want_template;
3624
3625 if (name == error_mark_node
3626 || context == NULL_TREE
3627 || context == error_mark_node)
3628 return error_mark_node;
3629
3630 if (TYPE_P (name))
3631 {
3632 if (!(TYPE_LANG_SPECIFIC (name)
3633 && (CLASSTYPE_IS_TEMPLATE (name)
3634 || CLASSTYPE_USE_TEMPLATE (name))))
3635 name = TYPE_IDENTIFIER (name);
3636 else
3637 /* Create a TEMPLATE_ID_EXPR for the type. */
3638 name = build_nt (TEMPLATE_ID_EXPR,
3639 CLASSTYPE_TI_TEMPLATE (name),
3640 CLASSTYPE_TI_ARGS (name));
3641 }
3642 else if (TREE_CODE (name) == TYPE_DECL)
3643 name = DECL_NAME (name);
3644
3645 fullname = name;
3646
3647 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3648 {
3649 name = TREE_OPERAND (name, 0);
3650 if (DECL_TYPE_TEMPLATE_P (name))
3651 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
3652 if (TREE_CODE (name) != IDENTIFIER_NODE)
3653 {
3654 if (complain & tf_error)
3655 error ("%qD is not a type", name);
3656 return error_mark_node;
3657 }
3658 }
3659 if (TREE_CODE (name) == TEMPLATE_DECL)
3660 {
3661 if (complain & tf_error)
3662 error ("%qD used without template parameters", name);
3663 return error_mark_node;
3664 }
3665 gcc_assert (identifier_p (name));
3666 gcc_assert (TYPE_P (context));
3667
3668 if (!MAYBE_CLASS_TYPE_P (context))
3669 {
3670 if (complain & tf_error)
3671 error ("%q#T is not a class", context);
3672 return error_mark_node;
3673 }
3674
3675 /* When the CONTEXT is a dependent type, NAME could refer to a
3676 dependent base class of CONTEXT. But look inside it anyway
3677 if CONTEXT is a currently open scope, in case it refers to a
3678 member of the current instantiation or a non-dependent base;
3679 lookup will stop when we hit a dependent base. */
3680 if (!dependent_scope_p (context))
3681 /* We should only set WANT_TYPE when we're a nested typename type.
3682 Then we can give better diagnostics if we find a non-type. */
3683 t = lookup_field (context, name, 2, /*want_type=*/true);
3684 else
3685 t = NULL_TREE;
3686
3687 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context))
3688 return build_typename_type (context, name, fullname, tag_type);
3689
3690 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3691
3692 if (!t)
3693 {
3694 if (complain & tf_error)
3695 error (want_template ? G_("no class template named %q#T in %q#T")
3696 : G_("no type named %q#T in %q#T"), name, context);
3697 return error_mark_node;
3698 }
3699
3700 /* Pull out the template from an injected-class-name (or multiple). */
3701 if (want_template)
3702 t = maybe_get_template_decl_from_type_decl (t);
3703
3704 if (TREE_CODE (t) == TREE_LIST)
3705 {
3706 if (complain & tf_error)
3707 {
3708 error ("lookup of %qT in %qT is ambiguous", name, context);
3709 print_candidates (t);
3710 }
3711 return error_mark_node;
3712 }
3713
3714 if (want_template && !DECL_TYPE_TEMPLATE_P (t))
3715 {
3716 if (complain & tf_error)
3717 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3718 context, name, t);
3719 return error_mark_node;
3720 }
3721 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3722 {
3723 if (complain & tf_error)
3724 error ("%<typename %T::%D%> names %q#T, which is not a type",
3725 context, name, t);
3726 return error_mark_node;
3727 }
3728
3729 if (!perform_or_defer_access_check (TYPE_BINFO (context), t, t, complain))
3730 return error_mark_node;
3731
3732 /* If we are currently parsing a template and if T is a typedef accessed
3733 through CONTEXT then we need to remember and check access of T at
3734 template instantiation time. */
3735 add_typedef_to_current_template_for_access_check (t, context, input_location);
3736
3737 if (want_template)
3738 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3739 NULL_TREE, context,
3740 /*entering_scope=*/0,
3741 complain | tf_user);
3742
3743 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3744 t = TREE_TYPE (t);
3745
3746 maybe_record_typedef_use (t);
3747
3748 return t;
3749 }
3750
3751 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3752 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3753 in which case error_mark_node is returned.
3754
3755 If PARM_LIST is non-NULL, also make sure that the template parameter
3756 list of TEMPLATE_DECL matches.
3757
3758 If COMPLAIN zero, don't complain about any errors that occur. */
3759
3760 tree
make_unbound_class_template(tree context,tree name,tree parm_list,tsubst_flags_t complain)3761 make_unbound_class_template (tree context, tree name, tree parm_list,
3762 tsubst_flags_t complain)
3763 {
3764 tree t;
3765 tree d;
3766
3767 if (TYPE_P (name))
3768 name = TYPE_IDENTIFIER (name);
3769 else if (DECL_P (name))
3770 name = DECL_NAME (name);
3771 gcc_assert (identifier_p (name));
3772
3773 if (!dependent_type_p (context)
3774 || currently_open_class (context))
3775 {
3776 tree tmpl = NULL_TREE;
3777
3778 if (MAYBE_CLASS_TYPE_P (context))
3779 tmpl = lookup_field (context, name, 0, false);
3780
3781 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL)
3782 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
3783
3784 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl))
3785 {
3786 if (complain & tf_error)
3787 error ("no class template named %q#T in %q#T", name, context);
3788 return error_mark_node;
3789 }
3790
3791 if (parm_list
3792 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3793 {
3794 if (complain & tf_error)
3795 {
3796 error ("template parameters do not match template %qD", tmpl);
3797 inform (DECL_SOURCE_LOCATION (tmpl),
3798 "%qD declared here", tmpl);
3799 }
3800 return error_mark_node;
3801 }
3802
3803 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl,
3804 complain))
3805 return error_mark_node;
3806
3807 return tmpl;
3808 }
3809
3810 /* Build the UNBOUND_CLASS_TEMPLATE. */
3811 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3812 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3813 TREE_TYPE (t) = NULL_TREE;
3814 SET_TYPE_STRUCTURAL_EQUALITY (t);
3815
3816 /* Build the corresponding TEMPLATE_DECL. */
3817 d = build_decl (input_location, TEMPLATE_DECL, name, t);
3818 TYPE_NAME (TREE_TYPE (d)) = d;
3819 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3820 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3821 DECL_ARTIFICIAL (d) = 1;
3822 DECL_TEMPLATE_PARMS (d) = parm_list;
3823
3824 return t;
3825 }
3826
3827
3828
3829 /* Push the declarations of builtin types into the namespace.
3830 RID_INDEX is the index of the builtin type in the array
3831 RID_POINTERS. NAME is the name used when looking up the builtin
3832 type. TYPE is the _TYPE node for the builtin type. */
3833
3834 void
record_builtin_type(enum rid rid_index,const char * name,tree type)3835 record_builtin_type (enum rid rid_index,
3836 const char* name,
3837 tree type)
3838 {
3839 tree rname = NULL_TREE, tname = NULL_TREE;
3840 tree tdecl = NULL_TREE;
3841
3842 if ((int) rid_index < (int) RID_MAX)
3843 rname = ridpointers[(int) rid_index];
3844 if (name)
3845 tname = get_identifier (name);
3846
3847 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3848 eliminated. Built-in types should not be looked up name; their
3849 names are keywords that the parser can recognize. However, there
3850 is code in c-common.c that uses identifier_global_value to look
3851 up built-in types by name. */
3852 if (tname)
3853 {
3854 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type);
3855 DECL_ARTIFICIAL (tdecl) = 1;
3856 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3857 }
3858 if (rname)
3859 {
3860 if (!tdecl)
3861 {
3862 tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type);
3863 DECL_ARTIFICIAL (tdecl) = 1;
3864 }
3865 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3866 }
3867
3868 if (!TYPE_NAME (type))
3869 TYPE_NAME (type) = tdecl;
3870
3871 if (tdecl)
3872 debug_hooks->type_decl (tdecl, 0);
3873 }
3874
3875 /* Record one of the standard Java types.
3876 * Declare it as having the given NAME.
3877 * If SIZE > 0, it is the size of one of the integral types;
3878 * otherwise it is the negative of the size of one of the other types. */
3879
3880 static tree
record_builtin_java_type(const char * name,int size)3881 record_builtin_java_type (const char* name, int size)
3882 {
3883 tree type, decl;
3884 if (size > 0)
3885 {
3886 type = build_nonstandard_integer_type (size, 0);
3887 type = build_distinct_type_copy (type);
3888 }
3889 else if (size > -32)
3890 {
3891 tree stype;
3892 /* "__java_char" or ""__java_boolean". */
3893 type = build_nonstandard_integer_type (-size, 1);
3894 type = build_distinct_type_copy (type);
3895 /* Get the signed type cached and attached to the unsigned type,
3896 so it doesn't get garbage-collected at "random" times,
3897 causing potential codegen differences out of different UIDs
3898 and different alias set numbers. */
3899 stype = build_nonstandard_integer_type (-size, 0);
3900 stype = build_distinct_type_copy (stype);
3901 TREE_CHAIN (type) = stype;
3902 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3903 }
3904 else
3905 { /* "__java_float" or ""__java_double". */
3906 type = make_node (REAL_TYPE);
3907 TYPE_PRECISION (type) = - size;
3908 layout_type (type);
3909 }
3910 record_builtin_type (RID_MAX, name, type);
3911 decl = TYPE_NAME (type);
3912
3913 /* Suppress generate debug symbol entries for these types,
3914 since for normal C++ they are just clutter.
3915 However, push_lang_context undoes this if extern "Java" is seen. */
3916 DECL_IGNORED_P (decl) = 1;
3917
3918 TYPE_FOR_JAVA (type) = 1;
3919 return type;
3920 }
3921
3922 /* Push a type into the namespace so that the back ends ignore it. */
3923
3924 static void
record_unknown_type(tree type,const char * name)3925 record_unknown_type (tree type, const char* name)
3926 {
3927 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION,
3928 TYPE_DECL, get_identifier (name), type));
3929 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3930 DECL_IGNORED_P (decl) = 1;
3931 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3932 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3933 TYPE_ALIGN (type) = 1;
3934 TYPE_USER_ALIGN (type) = 0;
3935 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3936 }
3937
3938 /* A string for which we should create an IDENTIFIER_NODE at
3939 startup. */
3940
3941 struct predefined_identifier
3942 {
3943 /* The name of the identifier. */
3944 const char *const name;
3945 /* The place where the IDENTIFIER_NODE should be stored. */
3946 tree *const node;
3947 /* Nonzero if this is the name of a constructor or destructor. */
3948 const int ctor_or_dtor_p;
3949 };
3950
3951 /* Create all the predefined identifiers. */
3952
3953 static void
initialize_predefined_identifiers(void)3954 initialize_predefined_identifiers (void)
3955 {
3956 const predefined_identifier *pid;
3957
3958 /* A table of identifiers to create at startup. */
3959 static const predefined_identifier predefined_identifiers[] = {
3960 { "C++", &lang_name_cplusplus, 0 },
3961 { "C", &lang_name_c, 0 },
3962 { "Java", &lang_name_java, 0 },
3963 /* Some of these names have a trailing space so that it is
3964 impossible for them to conflict with names written by users. */
3965 { "__ct ", &ctor_identifier, 1 },
3966 { "__base_ctor ", &base_ctor_identifier, 1 },
3967 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3968 { "__dt ", &dtor_identifier, 1 },
3969 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3970 { "__base_dtor ", &base_dtor_identifier, 1 },
3971 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3972 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3973 { "nelts", &nelts_identifier, 0 },
3974 { THIS_NAME, &this_identifier, 0 },
3975 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3976 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3977 { "_vptr", &vptr_identifier, 0 },
3978 { "__vtt_parm", &vtt_parm_identifier, 0 },
3979 { "::", &global_scope_name, 0 },
3980 { "std", &std_identifier, 0 },
3981 { NULL, NULL, 0 }
3982 };
3983
3984 for (pid = predefined_identifiers; pid->name; ++pid)
3985 {
3986 *pid->node = get_identifier (pid->name);
3987 if (pid->ctor_or_dtor_p)
3988 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3989 }
3990 }
3991
3992 /* Create the predefined scalar types of C,
3993 and some nodes representing standard constants (0, 1, (void *)0).
3994 Initialize the global binding level.
3995 Make definitions for built-in primitive functions. */
3996
3997 void
cxx_init_decl_processing(void)3998 cxx_init_decl_processing (void)
3999 {
4000 tree void_ftype;
4001 tree void_ftype_ptr;
4002
4003 /* Create all the identifiers we need. */
4004 initialize_predefined_identifiers ();
4005
4006 /* Create the global variables. */
4007 push_to_top_level ();
4008
4009 current_function_decl = NULL_TREE;
4010 current_binding_level = NULL;
4011 /* Enter the global namespace. */
4012 gcc_assert (global_namespace == NULL_TREE);
4013 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
4014 void_type_node);
4015 DECL_CONTEXT (global_namespace) = build_translation_unit_decl (NULL_TREE);
4016 debug_hooks->register_main_translation_unit
4017 (DECL_CONTEXT (global_namespace));
4018 TREE_PUBLIC (global_namespace) = 1;
4019 begin_scope (sk_namespace, global_namespace);
4020
4021 if (flag_visibility_ms_compat)
4022 default_visibility = VISIBILITY_HIDDEN;
4023
4024 /* Initially, C. */
4025 current_lang_name = lang_name_c;
4026
4027 /* Create the `std' namespace. */
4028 push_namespace (std_identifier);
4029 std_node = current_namespace;
4030 pop_namespace ();
4031
4032 c_common_nodes_and_builtins ();
4033
4034 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
4035 java_short_type_node = record_builtin_java_type ("__java_short", 16);
4036 java_int_type_node = record_builtin_java_type ("__java_int", 32);
4037 java_long_type_node = record_builtin_java_type ("__java_long", 64);
4038 java_float_type_node = record_builtin_java_type ("__java_float", -32);
4039 java_double_type_node = record_builtin_java_type ("__java_double", -64);
4040 java_char_type_node = record_builtin_java_type ("__java_char", -16);
4041 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
4042
4043 integer_two_node = build_int_cst (NULL_TREE, 2);
4044
4045 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
4046 truthvalue_type_node = boolean_type_node;
4047 truthvalue_false_node = boolean_false_node;
4048 truthvalue_true_node = boolean_true_node;
4049
4050 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
4051 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE);
4052 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE);
4053
4054 #if 0
4055 record_builtin_type (RID_MAX, NULL, string_type_node);
4056 #endif
4057
4058 delta_type_node = ptrdiff_type_node;
4059 vtable_index_type = ptrdiff_type_node;
4060
4061 vtt_parm_type = build_pointer_type (const_ptr_type_node);
4062 void_ftype = build_function_type_list (void_type_node, NULL_TREE);
4063 void_ftype_ptr = build_function_type_list (void_type_node,
4064 ptr_type_node, NULL_TREE);
4065 void_ftype_ptr
4066 = build_exception_variant (void_ftype_ptr, empty_except_spec);
4067
4068 /* C++ extensions */
4069
4070 unknown_type_node = make_node (LANG_TYPE);
4071 record_unknown_type (unknown_type_node, "unknown type");
4072
4073 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
4074 TREE_TYPE (unknown_type_node) = unknown_type_node;
4075
4076 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
4077 result. */
4078 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
4079 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
4080
4081 init_list_type_node = make_node (LANG_TYPE);
4082 record_unknown_type (init_list_type_node, "init list");
4083
4084 {
4085 /* Make sure we get a unique function type, so we can give
4086 its pointer type a name. (This wins for gdb.) */
4087 tree vfunc_type = make_node (FUNCTION_TYPE);
4088 TREE_TYPE (vfunc_type) = integer_type_node;
4089 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
4090 layout_type (vfunc_type);
4091
4092 vtable_entry_type = build_pointer_type (vfunc_type);
4093 }
4094 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
4095
4096 vtbl_type_node
4097 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
4098 layout_type (vtbl_type_node);
4099 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
4100 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
4101 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
4102 layout_type (vtbl_ptr_type_node);
4103 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
4104
4105 push_namespace (get_identifier ("__cxxabiv1"));
4106 abi_node = current_namespace;
4107 pop_namespace ();
4108
4109 global_type_node = make_node (LANG_TYPE);
4110 record_unknown_type (global_type_node, "global type");
4111
4112 /* Now, C++. */
4113 current_lang_name = lang_name_cplusplus;
4114
4115 {
4116 tree newattrs, extvisattr;
4117 tree newtype, deltype;
4118 tree ptr_ftype_sizetype;
4119 tree new_eh_spec;
4120
4121 ptr_ftype_sizetype
4122 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE);
4123 if (cxx_dialect == cxx98)
4124 {
4125 tree bad_alloc_id;
4126 tree bad_alloc_type_node;
4127 tree bad_alloc_decl;
4128
4129 push_namespace (std_identifier);
4130 bad_alloc_id = get_identifier ("bad_alloc");
4131 bad_alloc_type_node = make_class_type (RECORD_TYPE);
4132 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
4133 bad_alloc_decl
4134 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
4135 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
4136 pop_namespace ();
4137
4138 new_eh_spec
4139 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1);
4140 }
4141 else
4142 new_eh_spec = noexcept_false_spec;
4143
4144 /* Ensure attribs.c is initialized. */
4145 init_attributes ();
4146
4147 /* Ensure constraint.cc is initialized. */
4148 init_constraint_processing ();
4149
4150 extvisattr = build_tree_list (get_identifier ("externally_visible"),
4151 NULL_TREE);
4152 newattrs = tree_cons (get_identifier ("alloc_size"),
4153 build_tree_list (NULL_TREE, integer_one_node),
4154 extvisattr);
4155 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs);
4156 newtype = build_exception_variant (newtype, new_eh_spec);
4157 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr);
4158 deltype = build_exception_variant (deltype, empty_except_spec);
4159 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0);
4160 DECL_IS_MALLOC (opnew) = 1;
4161 DECL_IS_OPERATOR_NEW (opnew) = 1;
4162 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0);
4163 DECL_IS_MALLOC (opnew) = 1;
4164 DECL_IS_OPERATOR_NEW (opnew) = 1;
4165 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4166 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4167 if (flag_sized_deallocation)
4168 {
4169 /* Also push the sized deallocation variants:
4170 void operator delete(void*, std::size_t) throw();
4171 void operator delete[](void*, std::size_t) throw(); */
4172 tree void_ftype_ptr_size
4173 = build_function_type_list (void_type_node, ptr_type_node,
4174 size_type_node, NULL_TREE);
4175 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size,
4176 extvisattr);
4177 deltype = build_exception_variant (deltype, empty_except_spec);
4178 push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW);
4179 push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW);
4180 }
4181
4182 nullptr_type_node = make_node (NULLPTR_TYPE);
4183 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4184 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4185 TYPE_UNSIGNED (nullptr_type_node) = 1;
4186 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4187 if (abi_version_at_least (9))
4188 TYPE_ALIGN (nullptr_type_node) = GET_MODE_ALIGNMENT (ptr_mode);
4189 SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4190 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node);
4191 nullptr_node = build_int_cst (nullptr_type_node, 0);
4192 }
4193
4194 abort_fndecl
4195 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype,
4196 ECF_NORETURN | ECF_NOTHROW);
4197
4198 /* Perform other language dependent initializations. */
4199 init_class_processing ();
4200 init_rtti_processing ();
4201 init_template_processing ();
4202
4203 if (flag_exceptions)
4204 init_exception_processing ();
4205
4206 if (! supports_one_only ())
4207 flag_weak = 0;
4208
4209 make_fname_decl = cp_make_fname_decl;
4210 start_fname_decls ();
4211
4212 /* Show we use EH for cleanups. */
4213 if (flag_exceptions)
4214 using_eh_for_cleanups ();
4215 }
4216
4217 /* Generate an initializer for a function naming variable from
4218 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
4219 filled in with the type of the init. */
4220
4221 tree
cp_fname_init(const char * name,tree * type_p)4222 cp_fname_init (const char* name, tree *type_p)
4223 {
4224 tree domain = NULL_TREE;
4225 tree type;
4226 tree init = NULL_TREE;
4227 size_t length = 0;
4228
4229 if (name)
4230 {
4231 length = strlen (name);
4232 domain = build_index_type (size_int (length));
4233 init = build_string (length + 1, name);
4234 }
4235
4236 type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST);
4237 type = build_cplus_array_type (type, domain);
4238
4239 *type_p = type;
4240
4241 if (init)
4242 TREE_TYPE (init) = type;
4243 else
4244 init = error_mark_node;
4245
4246 return init;
4247 }
4248
4249 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give
4250 the decl, LOC is the location to give the decl, NAME is the
4251 initialization string and TYPE_DEP indicates whether NAME depended
4252 on the type of the function. We make use of that to detect
4253 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily
4254 at the point of first use, so we mustn't push the decl now. */
4255
4256 static tree
cp_make_fname_decl(location_t loc,tree id,int type_dep)4257 cp_make_fname_decl (location_t loc, tree id, int type_dep)
4258 {
4259 const char *const name = (type_dep && processing_template_decl
4260 ? NULL : fname_as_string (type_dep));
4261 tree type;
4262 tree init = cp_fname_init (name, &type);
4263 tree decl = build_decl (loc, VAR_DECL, id, type);
4264
4265 if (name)
4266 free (CONST_CAST (char *, name));
4267
4268 /* As we're using pushdecl_with_scope, we must set the context. */
4269 DECL_CONTEXT (decl) = current_function_decl;
4270
4271 TREE_STATIC (decl) = 1;
4272 TREE_READONLY (decl) = 1;
4273 DECL_ARTIFICIAL (decl) = 1;
4274
4275 TREE_USED (decl) = 1;
4276
4277 if (current_function_decl)
4278 {
4279 cp_binding_level *b = current_binding_level;
4280 if (b->kind == sk_function_parms)
4281 return error_mark_node;
4282 while (b->level_chain->kind != sk_function_parms)
4283 b = b->level_chain;
4284 pushdecl_with_scope (decl, b, /*is_friend=*/false);
4285 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
4286 LOOKUP_ONLYCONVERTING);
4287 }
4288 else
4289 {
4290 DECL_THIS_STATIC (decl) = true;
4291 pushdecl_top_level_and_finish (decl, init);
4292 }
4293
4294 return decl;
4295 }
4296
4297 static tree
builtin_function_1(tree decl,tree context,bool is_global)4298 builtin_function_1 (tree decl, tree context, bool is_global)
4299 {
4300 tree id = DECL_NAME (decl);
4301 const char *name = IDENTIFIER_POINTER (id);
4302
4303 retrofit_lang_decl (decl);
4304
4305 DECL_ARTIFICIAL (decl) = 1;
4306 SET_OVERLOADED_OPERATOR_CODE (decl, ERROR_MARK);
4307 SET_DECL_LANGUAGE (decl, lang_c);
4308 /* Runtime library routines are, by definition, available in an
4309 external shared object. */
4310 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
4311 DECL_VISIBILITY_SPECIFIED (decl) = 1;
4312
4313 DECL_CONTEXT (decl) = context;
4314
4315 if (is_global)
4316 pushdecl_top_level (decl);
4317 else
4318 pushdecl (decl);
4319
4320 /* A function in the user's namespace should have an explicit
4321 declaration before it is used. Mark the built-in function as
4322 anticipated but not actually declared. */
4323 if (name[0] != '_' || name[1] != '_')
4324 DECL_ANTICIPATED (decl) = 1;
4325 else if (strncmp (name + 2, "builtin_", strlen ("builtin_")) != 0)
4326 {
4327 size_t len = strlen (name);
4328
4329 /* Treat __*_chk fortification functions as anticipated as well,
4330 unless they are __builtin_*. */
4331 if (len > strlen ("___chk")
4332 && memcmp (name + len - strlen ("_chk"),
4333 "_chk", strlen ("_chk") + 1) == 0)
4334 DECL_ANTICIPATED (decl) = 1;
4335 }
4336
4337 return decl;
4338 }
4339
4340 tree
cxx_builtin_function(tree decl)4341 cxx_builtin_function (tree decl)
4342 {
4343 tree id = DECL_NAME (decl);
4344 const char *name = IDENTIFIER_POINTER (id);
4345 /* All builtins that don't begin with an '_' should additionally
4346 go in the 'std' namespace. */
4347 if (name[0] != '_')
4348 {
4349 tree decl2 = copy_node(decl);
4350 push_namespace (std_identifier);
4351 builtin_function_1 (decl2, std_node, false);
4352 pop_namespace ();
4353 }
4354
4355 return builtin_function_1 (decl, NULL_TREE, false);
4356 }
4357
4358 /* Like cxx_builtin_function, but guarantee the function is added to the global
4359 scope. This is to allow function specific options to add new machine
4360 dependent builtins when the target ISA changes via attribute((target(...)))
4361 which saves space on program startup if the program does not use non-generic
4362 ISAs. */
4363
4364 tree
cxx_builtin_function_ext_scope(tree decl)4365 cxx_builtin_function_ext_scope (tree decl)
4366 {
4367
4368 tree id = DECL_NAME (decl);
4369 const char *name = IDENTIFIER_POINTER (id);
4370 /* All builtins that don't begin with an '_' should additionally
4371 go in the 'std' namespace. */
4372 if (name[0] != '_')
4373 {
4374 tree decl2 = copy_node(decl);
4375 push_namespace (std_identifier);
4376 builtin_function_1 (decl2, std_node, true);
4377 pop_namespace ();
4378 }
4379
4380 return builtin_function_1 (decl, NULL_TREE, true);
4381 }
4382
4383 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
4384 function. Not called directly. */
4385
4386 static tree
build_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)4387 build_library_fn (tree name, enum tree_code operator_code, tree type,
4388 int ecf_flags)
4389 {
4390 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
4391 DECL_EXTERNAL (fn) = 1;
4392 TREE_PUBLIC (fn) = 1;
4393 DECL_ARTIFICIAL (fn) = 1;
4394 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
4395 SET_DECL_LANGUAGE (fn, lang_c);
4396 /* Runtime library routines are, by definition, available in an
4397 external shared object. */
4398 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
4399 DECL_VISIBILITY_SPECIFIED (fn) = 1;
4400 set_call_expr_flags (fn, ecf_flags);
4401 return fn;
4402 }
4403
4404 /* Returns the _DECL for a library function with C++ linkage. */
4405
4406 static tree
build_cp_library_fn(tree name,enum tree_code operator_code,tree type,int ecf_flags)4407 build_cp_library_fn (tree name, enum tree_code operator_code, tree type,
4408 int ecf_flags)
4409 {
4410 tree fn = build_library_fn (name, operator_code, type, ecf_flags);
4411 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
4412 SET_DECL_LANGUAGE (fn, lang_cplusplus);
4413 return fn;
4414 }
4415
4416 /* Like build_library_fn, but takes a C string instead of an
4417 IDENTIFIER_NODE. */
4418
4419 tree
build_library_fn_ptr(const char * name,tree type,int ecf_flags)4420 build_library_fn_ptr (const char* name, tree type, int ecf_flags)
4421 {
4422 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags);
4423 }
4424
4425 /* Like build_cp_library_fn, but takes a C string instead of an
4426 IDENTIFIER_NODE. */
4427
4428 tree
build_cp_library_fn_ptr(const char * name,tree type,int ecf_flags)4429 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags)
4430 {
4431 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type,
4432 ecf_flags);
4433 }
4434
4435 /* Like build_library_fn, but also pushes the function so that we will
4436 be able to find it via IDENTIFIER_GLOBAL_VALUE. Also, the function
4437 may throw exceptions listed in RAISES. */
4438
4439 tree
push_library_fn(tree name,tree type,tree raises,int ecf_flags)4440 push_library_fn (tree name, tree type, tree raises, int ecf_flags)
4441 {
4442 tree fn;
4443
4444 if (raises)
4445 type = build_exception_variant (type, raises);
4446
4447 fn = build_library_fn (name, ERROR_MARK, type, ecf_flags);
4448 pushdecl_top_level (fn);
4449 return fn;
4450 }
4451
4452 /* Like build_cp_library_fn, but also pushes the function so that it
4453 will be found by normal lookup. */
4454
4455 static tree
push_cp_library_fn(enum tree_code operator_code,tree type,int ecf_flags)4456 push_cp_library_fn (enum tree_code operator_code, tree type,
4457 int ecf_flags)
4458 {
4459 tree fn = build_cp_library_fn (ansi_opname (operator_code),
4460 operator_code,
4461 type, ecf_flags);
4462 pushdecl (fn);
4463 if (flag_tm)
4464 apply_tm_attr (fn, get_identifier ("transaction_safe"));
4465 return fn;
4466 }
4467
4468 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
4469 a FUNCTION_TYPE. */
4470
4471 tree
push_void_library_fn(tree name,tree parmtypes,int ecf_flags)4472 push_void_library_fn (tree name, tree parmtypes, int ecf_flags)
4473 {
4474 tree type = build_function_type (void_type_node, parmtypes);
4475 return push_library_fn (name, type, NULL_TREE, ecf_flags);
4476 }
4477
4478 /* Like push_library_fn, but also note that this function throws
4479 and does not return. Used for __throw_foo and the like. */
4480
4481 tree
push_throw_library_fn(tree name,tree type)4482 push_throw_library_fn (tree name, tree type)
4483 {
4484 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN);
4485 return fn;
4486 }
4487
4488 /* When we call finish_struct for an anonymous union, we create
4489 default copy constructors and such. But, an anonymous union
4490 shouldn't have such things; this function undoes the damage to the
4491 anonymous union type T.
4492
4493 (The reason that we create the synthesized methods is that we don't
4494 distinguish `union { int i; }' from `typedef union { int i; } U'.
4495 The first is an anonymous union; the second is just an ordinary
4496 union type.) */
4497
4498 void
fixup_anonymous_aggr(tree t)4499 fixup_anonymous_aggr (tree t)
4500 {
4501 tree *q;
4502
4503 /* Wipe out memory of synthesized methods. */
4504 TYPE_HAS_USER_CONSTRUCTOR (t) = 0;
4505 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
4506 TYPE_HAS_COPY_CTOR (t) = 0;
4507 TYPE_HAS_CONST_COPY_CTOR (t) = 0;
4508 TYPE_HAS_COPY_ASSIGN (t) = 0;
4509 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0;
4510
4511 /* Splice the implicitly generated functions out of the TYPE_METHODS
4512 list. */
4513 q = &TYPE_METHODS (t);
4514 while (*q)
4515 {
4516 if (DECL_ARTIFICIAL (*q))
4517 *q = TREE_CHAIN (*q);
4518 else
4519 q = &DECL_CHAIN (*q);
4520 }
4521
4522 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
4523 if (TYPE_METHODS (t))
4524 {
4525 tree decl = TYPE_MAIN_DECL (t);
4526
4527 if (TREE_CODE (t) != UNION_TYPE)
4528 error_at (DECL_SOURCE_LOCATION (decl),
4529 "an anonymous struct cannot have function members");
4530 else
4531 error_at (DECL_SOURCE_LOCATION (decl),
4532 "an anonymous union cannot have function members");
4533 }
4534
4535 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
4536 assignment operators (because they cannot have these methods themselves).
4537 For anonymous unions this is already checked because they are not allowed
4538 in any union, otherwise we have to check it. */
4539 if (TREE_CODE (t) != UNION_TYPE)
4540 {
4541 tree field, type;
4542
4543 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4544 if (TREE_CODE (field) == FIELD_DECL)
4545 {
4546 type = TREE_TYPE (field);
4547 if (CLASS_TYPE_P (type))
4548 {
4549 if (TYPE_NEEDS_CONSTRUCTING (type))
4550 error ("member %q+#D with constructor not allowed "
4551 "in anonymous aggregate", field);
4552 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4553 error ("member %q+#D with destructor not allowed "
4554 "in anonymous aggregate", field);
4555 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type))
4556 error ("member %q+#D with copy assignment operator "
4557 "not allowed in anonymous aggregate", field);
4558 }
4559 }
4560 }
4561 }
4562
4563 /* Warn for an attribute located at LOCATION that appertains to the
4564 class type CLASS_TYPE that has not been properly placed after its
4565 class-key, in it class-specifier. */
4566
4567 void
warn_misplaced_attr_for_class_type(source_location location,tree class_type)4568 warn_misplaced_attr_for_class_type (source_location location,
4569 tree class_type)
4570 {
4571 gcc_assert (OVERLOAD_TYPE_P (class_type));
4572
4573 if (warning_at (location, OPT_Wattributes,
4574 "attribute ignored in declaration "
4575 "of %q#T", class_type))
4576 inform (location,
4577 "attribute for %q#T must follow the %qs keyword",
4578 class_type, class_key_or_enum_as_string (class_type));
4579 }
4580
4581 /* Make sure that a declaration with no declarator is well-formed, i.e.
4582 just declares a tagged type or anonymous union.
4583
4584 Returns the type declared; or NULL_TREE if none. */
4585
4586 tree
check_tag_decl(cp_decl_specifier_seq * declspecs,bool explicit_type_instantiation_p)4587 check_tag_decl (cp_decl_specifier_seq *declspecs,
4588 bool explicit_type_instantiation_p)
4589 {
4590 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend);
4591 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
4592 /* If a class, struct, or enum type is declared by the DECLSPECS
4593 (i.e, if a class-specifier, enum-specifier, or non-typename
4594 elaborated-type-specifier appears in the DECLSPECS),
4595 DECLARED_TYPE is set to the corresponding type. */
4596 tree declared_type = NULL_TREE;
4597 bool error_p = false;
4598
4599 if (declspecs->multiple_types_p)
4600 error ("multiple types in one declaration");
4601 else if (declspecs->redefined_builtin_type)
4602 {
4603 if (!in_system_header_at (input_location))
4604 permerror (declspecs->locations[ds_redefined_builtin_type_spec],
4605 "redeclaration of C++ built-in type %qT",
4606 declspecs->redefined_builtin_type);
4607 return NULL_TREE;
4608 }
4609
4610 if (declspecs->type
4611 && TYPE_P (declspecs->type)
4612 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
4613 && MAYBE_CLASS_TYPE_P (declspecs->type))
4614 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
4615 declared_type = declspecs->type;
4616 else if (declspecs->type == error_mark_node)
4617 error_p = true;
4618 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
4619 permerror (input_location, "declaration does not declare anything");
4620 else if (declared_type != NULL_TREE && type_uses_auto (declared_type))
4621 {
4622 error_at (declspecs->locations[ds_type_spec],
4623 "%<auto%> can only be specified for variables "
4624 "or function declarations");
4625 return error_mark_node;
4626 }
4627 /* Check for an anonymous union. */
4628 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type))
4629 && TYPE_ANONYMOUS_P (declared_type))
4630 {
4631 /* 7/3 In a simple-declaration, the optional init-declarator-list
4632 can be omitted only when declaring a class (clause 9) or
4633 enumeration (7.2), that is, when the decl-specifier-seq contains
4634 either a class-specifier, an elaborated-type-specifier with
4635 a class-key (9.1), or an enum-specifier. In these cases and
4636 whenever a class-specifier or enum-specifier is present in the
4637 decl-specifier-seq, the identifiers in these specifiers are among
4638 the names being declared by the declaration (as class-name,
4639 enum-names, or enumerators, depending on the syntax). In such
4640 cases, and except for the declaration of an unnamed bit-field (9.6),
4641 the decl-specifier-seq shall introduce one or more names into the
4642 program, or shall redeclare a name introduced by a previous
4643 declaration. [Example:
4644 enum { }; // ill-formed
4645 typedef class { }; // ill-formed
4646 --end example] */
4647 if (saw_typedef)
4648 {
4649 error ("missing type-name in typedef-declaration");
4650 return NULL_TREE;
4651 }
4652 /* Anonymous unions are objects, so they can have specifiers. */;
4653 SET_ANON_AGGR_TYPE_P (declared_type);
4654
4655 if (TREE_CODE (declared_type) != UNION_TYPE
4656 && !in_system_header_at (input_location))
4657 pedwarn (input_location, OPT_Wpedantic, "ISO C++ prohibits anonymous structs");
4658 }
4659
4660 else
4661 {
4662 if (decl_spec_seq_has_spec_p (declspecs, ds_inline))
4663 error_at (declspecs->locations[ds_inline],
4664 "%<inline%> can only be specified for functions");
4665 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual))
4666 error_at (declspecs->locations[ds_virtual],
4667 "%<virtual%> can only be specified for functions");
4668 else if (saw_friend
4669 && (!current_class_type
4670 || current_scope () != current_class_type))
4671 error_at (declspecs->locations[ds_friend],
4672 "%<friend%> can only be specified inside a class");
4673 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit))
4674 error_at (declspecs->locations[ds_explicit],
4675 "%<explicit%> can only be specified for constructors");
4676 else if (declspecs->storage_class)
4677 error_at (declspecs->locations[ds_storage_class],
4678 "a storage class can only be specified for objects "
4679 "and functions");
4680 else if (decl_spec_seq_has_spec_p (declspecs, ds_const))
4681 error_at (declspecs->locations[ds_const],
4682 "%<const%> can only be specified for objects and "
4683 "functions");
4684 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
4685 error_at (declspecs->locations[ds_volatile],
4686 "%<volatile%> can only be specified for objects and "
4687 "functions");
4688 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
4689 error_at (declspecs->locations[ds_restrict],
4690 "%<__restrict%> can only be specified for objects and "
4691 "functions");
4692 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
4693 error_at (declspecs->locations[ds_thread],
4694 "%<__thread%> can only be specified for objects "
4695 "and functions");
4696 else if (saw_typedef)
4697 warning_at (declspecs->locations[ds_typedef], 0,
4698 "%<typedef%> was ignored in this declaration");
4699 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr))
4700 error_at (declspecs->locations[ds_constexpr],
4701 "%<constexpr%> cannot be used for type declarations");
4702 }
4703
4704 if (declspecs->attributes && warn_attributes && declared_type)
4705 {
4706 location_t loc;
4707 if (!CLASS_TYPE_P (declared_type)
4708 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type))
4709 /* For a non-template class, use the name location. */
4710 loc = location_of (declared_type);
4711 else
4712 /* For a template class (an explicit instantiation), use the
4713 current location. */
4714 loc = input_location;
4715
4716 if (explicit_type_instantiation_p)
4717 /* [dcl.attr.grammar]/4:
4718
4719 No attribute-specifier-seq shall appertain to an explicit
4720 instantiation. */
4721 {
4722 if (warning_at (loc, OPT_Wattributes,
4723 "attribute ignored in explicit instantiation %q#T",
4724 declared_type))
4725 inform (loc,
4726 "no attribute can be applied to "
4727 "an explicit instantiation");
4728 }
4729 else
4730 warn_misplaced_attr_for_class_type (loc, declared_type);
4731 }
4732
4733 return declared_type;
4734 }
4735
4736 /* Called when a declaration is seen that contains no names to declare.
4737 If its type is a reference to a structure, union or enum inherited
4738 from a containing scope, shadow that tag name for the current scope
4739 with a forward reference.
4740 If its type defines a new named structure or union
4741 or defines an enum, it is valid but we need not do anything here.
4742 Otherwise, it is an error.
4743
4744 C++: may have to grok the declspecs to learn about static,
4745 complain for anonymous unions.
4746
4747 Returns the TYPE declared -- or NULL_TREE if none. */
4748
4749 tree
shadow_tag(cp_decl_specifier_seq * declspecs)4750 shadow_tag (cp_decl_specifier_seq *declspecs)
4751 {
4752 tree t = check_tag_decl (declspecs,
4753 /*explicit_type_instantiation_p=*/false);
4754
4755 if (!t)
4756 return NULL_TREE;
4757
4758 if (maybe_process_partial_specialization (t) == error_mark_node)
4759 return NULL_TREE;
4760
4761 /* This is where the variables in an anonymous union are
4762 declared. An anonymous union declaration looks like:
4763 union { ... } ;
4764 because there is no declarator after the union, the parser
4765 sends that declaration here. */
4766 if (ANON_AGGR_TYPE_P (t))
4767 {
4768 fixup_anonymous_aggr (t);
4769
4770 if (TYPE_FIELDS (t))
4771 {
4772 tree decl = grokdeclarator (/*declarator=*/NULL,
4773 declspecs, NORMAL, 0, NULL);
4774 finish_anon_union (decl);
4775 }
4776 }
4777
4778 return t;
4779 }
4780
4781 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
4782
4783 tree
groktypename(cp_decl_specifier_seq * type_specifiers,const cp_declarator * declarator,bool is_template_arg)4784 groktypename (cp_decl_specifier_seq *type_specifiers,
4785 const cp_declarator *declarator,
4786 bool is_template_arg)
4787 {
4788 tree attrs;
4789 tree type;
4790 enum decl_context context
4791 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME;
4792 attrs = type_specifiers->attributes;
4793 type_specifiers->attributes = NULL_TREE;
4794 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs);
4795 if (attrs && type != error_mark_node)
4796 {
4797 if (CLASS_TYPE_P (type))
4798 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT "
4799 "outside of definition", type);
4800 else if (MAYBE_CLASS_TYPE_P (type))
4801 /* A template type parameter or other dependent type. */
4802 warning (OPT_Wattributes, "ignoring attributes applied to dependent "
4803 "type %qT without an associated declaration", type);
4804 else
4805 cplus_decl_attributes (&type, attrs, 0);
4806 }
4807 return type;
4808 }
4809
4810 /* Process a DECLARATOR for a function-scope variable declaration,
4811 namespace-scope variable declaration, or function declaration.
4812 (Function definitions go through start_function; class member
4813 declarations appearing in the body of the class go through
4814 grokfield.) The DECL corresponding to the DECLARATOR is returned.
4815 If an error occurs, the error_mark_node is returned instead.
4816
4817 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is
4818 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED
4819 for an explicitly defaulted function, or SD_DELETED for an explicitly
4820 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable
4821 implicitly initialized via a default constructor. ATTRIBUTES and
4822 PREFIX_ATTRIBUTES are GNU attributes associated with this declaration.
4823
4824 The scope represented by the context of the returned DECL is pushed
4825 (if it is not the global namespace) and is assigned to
4826 *PUSHED_SCOPE_P. The caller is then responsible for calling
4827 pop_scope on *PUSHED_SCOPE_P if it is set. */
4828
4829 tree
start_decl(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,int initialized,tree attributes,tree prefix_attributes,tree * pushed_scope_p)4830 start_decl (const cp_declarator *declarator,
4831 cp_decl_specifier_seq *declspecs,
4832 int initialized,
4833 tree attributes,
4834 tree prefix_attributes,
4835 tree *pushed_scope_p)
4836 {
4837 tree decl;
4838 tree context;
4839 bool was_public;
4840 int flags;
4841 bool alias;
4842
4843 *pushed_scope_p = NULL_TREE;
4844
4845 /* An object declared as __attribute__((deprecated)) suppresses
4846 warnings of uses of other deprecated items. */
4847 if (lookup_attribute ("deprecated", attributes))
4848 deprecated_state = DEPRECATED_SUPPRESS;
4849
4850 attributes = chainon (attributes, prefix_attributes);
4851
4852 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
4853 &attributes);
4854
4855 deprecated_state = DEPRECATED_NORMAL;
4856
4857 if (decl == NULL_TREE || VOID_TYPE_P (decl)
4858 || decl == error_mark_node)
4859 return error_mark_node;
4860
4861 context = CP_DECL_CONTEXT (decl);
4862 if (context != global_namespace)
4863 *pushed_scope_p = push_scope (context);
4864
4865 /* Is it valid for this decl to have an initializer at all?
4866 If not, set INITIALIZED to zero, which will indirectly
4867 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4868 if (initialized
4869 && TREE_CODE (decl) == TYPE_DECL)
4870 {
4871 error ("typedef %qD is initialized (use decltype instead)", decl);
4872 return error_mark_node;
4873 }
4874
4875 if (initialized)
4876 {
4877 if (! toplevel_bindings_p ()
4878 && DECL_EXTERNAL (decl))
4879 warning (0, "declaration of %q#D has %<extern%> and is initialized",
4880 decl);
4881 DECL_EXTERNAL (decl) = 0;
4882 if (toplevel_bindings_p ())
4883 TREE_STATIC (decl) = 1;
4884 }
4885 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0;
4886
4887 if (alias && TREE_CODE (decl) == FUNCTION_DECL)
4888 record_key_method_defined (decl);
4889
4890 /* If this is a typedef that names the class for linkage purposes
4891 (7.1.3p8), apply any attributes directly to the type. */
4892 if (TREE_CODE (decl) == TYPE_DECL
4893 && OVERLOAD_TYPE_P (TREE_TYPE (decl))
4894 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
4895 flags = ATTR_FLAG_TYPE_IN_PLACE;
4896 else
4897 flags = 0;
4898
4899 /* Set attributes here so if duplicate decl, will have proper attributes. */
4900 cplus_decl_attributes (&decl, attributes, flags);
4901
4902 /* Dllimported symbols cannot be defined. Static data members (which
4903 can be initialized in-class and dllimported) go through grokfield,
4904 not here, so we don't need to exclude those decls when checking for
4905 a definition. */
4906 if (initialized && DECL_DLLIMPORT_P (decl))
4907 {
4908 error ("definition of %q#D is marked %<dllimport%>", decl);
4909 DECL_DLLIMPORT_P (decl) = 0;
4910 }
4911
4912 /* If #pragma weak was used, mark the decl weak now. */
4913 if (!processing_template_decl)
4914 maybe_apply_pragma_weak (decl);
4915
4916 if (TREE_CODE (decl) == FUNCTION_DECL
4917 && DECL_DECLARED_INLINE_P (decl)
4918 && DECL_UNINLINABLE (decl)
4919 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4920 warning_at (DECL_SOURCE_LOCATION (decl), 0,
4921 "inline function %qD given attribute noinline", decl);
4922
4923 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context)))
4924 {
4925 bool this_tmpl = (processing_template_decl
4926 > template_class_depth (context));
4927 if (VAR_P (decl))
4928 {
4929 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
4930 if (field == NULL_TREE
4931 || !(VAR_P (field) || variable_template_p (field)))
4932 error ("%q+#D is not a static data member of %q#T", decl, context);
4933 else if (variable_template_p (field) && !this_tmpl)
4934 {
4935 if (DECL_LANG_SPECIFIC (decl)
4936 && DECL_TEMPLATE_SPECIALIZATION (decl))
4937 /* OK, specialization was already checked. */;
4938 else
4939 {
4940 error_at (DECL_SOURCE_LOCATION (decl),
4941 "non-member-template declaration of %qD", decl);
4942 inform (DECL_SOURCE_LOCATION (field), "does not match "
4943 "member template declaration here");
4944 return error_mark_node;
4945 }
4946 }
4947 else
4948 {
4949 if (variable_template_p (field))
4950 field = DECL_TEMPLATE_RESULT (field);
4951
4952 if (DECL_CONTEXT (field) != context)
4953 {
4954 if (!same_type_p (DECL_CONTEXT (field), context))
4955 permerror (input_location, "ISO C++ does not permit %<%T::%D%> "
4956 "to be defined as %<%T::%D%>",
4957 DECL_CONTEXT (field), DECL_NAME (decl),
4958 context, DECL_NAME (decl));
4959 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
4960 }
4961 /* Static data member are tricky; an in-class initialization
4962 still doesn't provide a definition, so the in-class
4963 declaration will have DECL_EXTERNAL set, but will have an
4964 initialization. Thus, duplicate_decls won't warn
4965 about this situation, and so we check here. */
4966 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
4967 error ("duplicate initialization of %qD", decl);
4968 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
4969 decl = field;
4970 if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)
4971 && !DECL_DECLARED_CONSTEXPR_P (field))
4972 error ("%qD declared %<constexpr%> outside its class", field);
4973 }
4974 }
4975 else
4976 {
4977 tree field = check_classfn (context, decl,
4978 this_tmpl
4979 ? current_template_parms
4980 : NULL_TREE);
4981 if (field && field != error_mark_node
4982 && duplicate_decls (decl, field,
4983 /*newdecl_is_friend=*/false))
4984 decl = field;
4985 }
4986
4987 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4988 DECL_IN_AGGR_P (decl) = 0;
4989 /* Do not mark DECL as an explicit specialization if it was not
4990 already marked as an instantiation; a declaration should
4991 never be marked as a specialization unless we know what
4992 template is being specialized. */
4993 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4994 {
4995 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4996 if (TREE_CODE (decl) == FUNCTION_DECL)
4997 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
4998 && DECL_DECLARED_INLINE_P (decl));
4999 else
5000 DECL_COMDAT (decl) = false;
5001
5002 /* [temp.expl.spec] An explicit specialization of a static data
5003 member of a template is a definition if the declaration
5004 includes an initializer; otherwise, it is a declaration.
5005
5006 We check for processing_specialization so this only applies
5007 to the new specialization syntax. */
5008 if (!initialized && processing_specialization)
5009 DECL_EXTERNAL (decl) = 1;
5010 }
5011
5012 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl)
5013 /* Aliases are definitions. */
5014 && !alias)
5015 permerror (input_location, "declaration of %q#D outside of class is not definition",
5016 decl);
5017 }
5018
5019 was_public = TREE_PUBLIC (decl);
5020
5021 /* Enter this declaration into the symbol table. Don't push the plain
5022 VAR_DECL for a variable template. */
5023 if (!template_parm_scope_p ()
5024 || !VAR_P (decl))
5025 decl = maybe_push_decl (decl);
5026
5027 if (processing_template_decl)
5028 decl = push_template_decl (decl);
5029 if (decl == error_mark_node)
5030 return error_mark_node;
5031
5032 if (VAR_P (decl)
5033 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public
5034 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl))
5035 {
5036 /* This is a const variable with implicit 'static'. Set
5037 DECL_THIS_STATIC so we can tell it from variables that are
5038 !TREE_PUBLIC because of the anonymous namespace. */
5039 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount);
5040 DECL_THIS_STATIC (decl) = 1;
5041 }
5042
5043 if (current_function_decl && VAR_P (decl)
5044 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
5045 {
5046 bool ok = false;
5047 if (CP_DECL_THREAD_LOCAL_P (decl))
5048 error ("%qD declared %<thread_local%> in %<constexpr%> function",
5049 decl);
5050 else if (TREE_STATIC (decl))
5051 error ("%qD declared %<static%> in %<constexpr%> function", decl);
5052 else
5053 ok = true;
5054 if (!ok)
5055 cp_function_chain->invalid_constexpr = true;
5056 }
5057
5058 if (!processing_template_decl && VAR_P (decl))
5059 start_decl_1 (decl, initialized);
5060
5061 return decl;
5062 }
5063
5064 /* Process the declaration of a variable DECL. INITIALIZED is true
5065 iff DECL is explicitly initialized. (INITIALIZED is false if the
5066 variable is initialized via an implicitly-called constructor.)
5067 This function must be called for ordinary variables (including, for
5068 example, implicit instantiations of templates), but must not be
5069 called for template declarations. */
5070
5071 void
start_decl_1(tree decl,bool initialized)5072 start_decl_1 (tree decl, bool initialized)
5073 {
5074 tree type;
5075 bool complete_p;
5076 bool aggregate_definition_p;
5077
5078 gcc_assert (!processing_template_decl);
5079
5080 if (error_operand_p (decl))
5081 return;
5082
5083 gcc_assert (VAR_P (decl));
5084
5085 type = TREE_TYPE (decl);
5086 complete_p = COMPLETE_TYPE_P (type);
5087 aggregate_definition_p = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl);
5088
5089 /* If an explicit initializer is present, or if this is a definition
5090 of an aggregate, then we need a complete type at this point.
5091 (Scalars are always complete types, so there is nothing to
5092 check.) This code just sets COMPLETE_P; errors (if necessary)
5093 are issued below. */
5094 if ((initialized || aggregate_definition_p)
5095 && !complete_p
5096 && COMPLETE_TYPE_P (complete_type (type)))
5097 {
5098 complete_p = true;
5099 /* We will not yet have set TREE_READONLY on DECL if the type
5100 was "const", but incomplete, before this point. But, now, we
5101 have a complete type, so we can try again. */
5102 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
5103 }
5104
5105 if (initialized)
5106 /* Is it valid for this decl to have an initializer at all? */
5107 {
5108 /* Don't allow initializations for incomplete types except for
5109 arrays which might be completed by the initialization. */
5110 if (complete_p)
5111 ; /* A complete type is ok. */
5112 else if (type_uses_auto (type))
5113 ; /* An auto type is ok. */
5114 else if (TREE_CODE (type) != ARRAY_TYPE)
5115 {
5116 error ("variable %q#D has initializer but incomplete type", decl);
5117 type = TREE_TYPE (decl) = error_mark_node;
5118 }
5119 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
5120 {
5121 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
5122 error ("elements of array %q#D have incomplete type", decl);
5123 /* else we already gave an error in start_decl. */
5124 }
5125 }
5126 else if (aggregate_definition_p && !complete_p)
5127 {
5128 if (type_uses_auto (type))
5129 error ("declaration of %q#D has no initializer", decl);
5130 else
5131 error ("aggregate %q#D has incomplete type and cannot be defined",
5132 decl);
5133 /* Change the type so that assemble_variable will give
5134 DECL an rtl we can live with: (mem (const_int 0)). */
5135 type = TREE_TYPE (decl) = error_mark_node;
5136 }
5137
5138 /* Create a new scope to hold this declaration if necessary.
5139 Whether or not a new scope is necessary cannot be determined
5140 until after the type has been completed; if the type is a
5141 specialization of a class template it is not until after
5142 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
5143 will be set correctly. */
5144 maybe_push_cleanup_level (type);
5145 }
5146
5147 /* Handle initialization of references. DECL, TYPE, and INIT have the
5148 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
5149 but will be set to a new CLEANUP_STMT if a temporary is created
5150 that must be destroyed subsequently.
5151
5152 Returns an initializer expression to use to initialize DECL, or
5153 NULL if the initialization can be performed statically.
5154
5155 Quotes on semantics can be found in ARM 8.4.3. */
5156
5157 static tree
grok_reference_init(tree decl,tree type,tree init,int flags)5158 grok_reference_init (tree decl, tree type, tree init, int flags)
5159 {
5160 if (init == NULL_TREE)
5161 {
5162 if ((DECL_LANG_SPECIFIC (decl) == 0
5163 || DECL_IN_AGGR_P (decl) == 0)
5164 && ! DECL_THIS_EXTERN (decl))
5165 error ("%qD declared as reference but not initialized", decl);
5166 return NULL_TREE;
5167 }
5168
5169 if (TREE_CODE (init) == TREE_LIST)
5170 init = build_x_compound_expr_from_list (init, ELK_INIT,
5171 tf_warning_or_error);
5172
5173 tree ttype = TREE_TYPE (type);
5174 if (TREE_CODE (ttype) != ARRAY_TYPE
5175 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
5176 /* Note: default conversion is only called in very special cases. */
5177 init = decay_conversion (init, tf_warning_or_error);
5178
5179 /* check_initializer handles this for non-reference variables, but for
5180 references we need to do it here or the initializer will get the
5181 incomplete array type and confuse later calls to
5182 cp_complete_array_type. */
5183 if (TREE_CODE (ttype) == ARRAY_TYPE
5184 && TYPE_DOMAIN (ttype) == NULL_TREE
5185 && (BRACE_ENCLOSED_INITIALIZER_P (init)
5186 || TREE_CODE (init) == STRING_CST))
5187 {
5188 cp_complete_array_type (&ttype, init, false);
5189 if (ttype != TREE_TYPE (type))
5190 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type));
5191 }
5192
5193 /* Convert INIT to the reference type TYPE. This may involve the
5194 creation of a temporary, whose lifetime must be the same as that
5195 of the reference. If so, a DECL_EXPR for the temporary will be
5196 added just after the DECL_EXPR for DECL. That's why we don't set
5197 DECL_INITIAL for local references (instead assigning to them
5198 explicitly); we need to allow the temporary to be initialized
5199 first. */
5200 return initialize_reference (type, init, flags,
5201 tf_warning_or_error);
5202 }
5203
5204 /* Designated initializers in arrays are not supported in GNU C++.
5205 The parser cannot detect this error since it does not know whether
5206 a given brace-enclosed initializer is for a class type or for an
5207 array. This function checks that CE does not use a designated
5208 initializer. If it does, an error is issued. Returns true if CE
5209 is valid, i.e., does not have a designated initializer. */
5210
5211 static bool
check_array_designated_initializer(constructor_elt * ce,unsigned HOST_WIDE_INT index)5212 check_array_designated_initializer (constructor_elt *ce,
5213 unsigned HOST_WIDE_INT index)
5214 {
5215 /* Designated initializers for array elements are not supported. */
5216 if (ce->index)
5217 {
5218 /* The parser only allows identifiers as designated
5219 initializers. */
5220 if (ce->index == error_mark_node)
5221 {
5222 error ("name used in a GNU-style designated "
5223 "initializer for an array");
5224 return false;
5225 }
5226 else if (identifier_p (ce->index))
5227 {
5228 error ("name %qD used in a GNU-style designated "
5229 "initializer for an array", ce->index);
5230 return false;
5231 }
5232
5233 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5234 ce->index, true);
5235 if (ce_index
5236 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index))
5237 && (TREE_CODE (ce_index = maybe_constant_value (ce_index))
5238 == INTEGER_CST))
5239 {
5240 /* A C99 designator is OK if it matches the current index. */
5241 if (wi::eq_p (ce_index, index))
5242 return true;
5243 else
5244 sorry ("non-trivial designated initializers not supported");
5245 }
5246 else
5247 error ("C99 designator %qE is not an integral constant-expression",
5248 ce->index);
5249
5250 return false;
5251 }
5252
5253 return true;
5254 }
5255
5256 /* When parsing `int a[] = {1, 2};' we don't know the size of the
5257 array until we finish parsing the initializer. If that's the
5258 situation we're in, update DECL accordingly. */
5259
5260 static void
maybe_deduce_size_from_array_init(tree decl,tree init)5261 maybe_deduce_size_from_array_init (tree decl, tree init)
5262 {
5263 tree type = TREE_TYPE (decl);
5264
5265 if (TREE_CODE (type) == ARRAY_TYPE
5266 && TYPE_DOMAIN (type) == NULL_TREE
5267 && TREE_CODE (decl) != TYPE_DECL)
5268 {
5269 /* do_default is really a C-ism to deal with tentative definitions.
5270 But let's leave it here to ease the eventual merge. */
5271 int do_default = !DECL_EXTERNAL (decl);
5272 tree initializer = init ? init : DECL_INITIAL (decl);
5273 int failure = 0;
5274
5275 /* Check that there are no designated initializers in INIT, as
5276 those are not supported in GNU C++, and as the middle-end
5277 will crash if presented with a non-numeric designated
5278 initializer. */
5279 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer))
5280 {
5281 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer);
5282 constructor_elt *ce;
5283 HOST_WIDE_INT i;
5284 FOR_EACH_VEC_SAFE_ELT (v, i, ce)
5285 if (!check_array_designated_initializer (ce, i))
5286 failure = 1;
5287 }
5288
5289 if (!failure)
5290 {
5291 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
5292 do_default);
5293 if (failure == 1)
5294 {
5295 error ("initializer fails to determine size of %qD", decl);
5296 }
5297 else if (failure == 2)
5298 {
5299 if (do_default)
5300 {
5301 error ("array size missing in %qD", decl);
5302 }
5303 /* If a `static' var's size isn't known, make it extern as
5304 well as static, so it does not get allocated. If it's not
5305 `static', then don't mark it extern; finish_incomplete_decl
5306 will give it a default size and it will get allocated. */
5307 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
5308 DECL_EXTERNAL (decl) = 1;
5309 }
5310 else if (failure == 3)
5311 {
5312 error ("zero-size array %qD", decl);
5313 }
5314 }
5315
5316 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
5317
5318 relayout_decl (decl);
5319 }
5320 }
5321
5322 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
5323 any appropriate error messages regarding the layout. */
5324
5325 static void
layout_var_decl(tree decl)5326 layout_var_decl (tree decl)
5327 {
5328 tree type;
5329
5330 type = TREE_TYPE (decl);
5331 if (type == error_mark_node)
5332 return;
5333
5334 /* If we haven't already laid out this declaration, do so now.
5335 Note that we must not call complete type for an external object
5336 because it's type might involve templates that we are not
5337 supposed to instantiate yet. (And it's perfectly valid to say
5338 `extern X x' for some incomplete type `X'.) */
5339 if (!DECL_EXTERNAL (decl))
5340 complete_type (type);
5341 if (!DECL_SIZE (decl)
5342 && TREE_TYPE (decl) != error_mark_node
5343 && complete_or_array_type_p (type))
5344 layout_decl (decl, 0);
5345
5346 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
5347 {
5348 /* An automatic variable with an incomplete type: that is an error.
5349 Don't talk about array types here, since we took care of that
5350 message in grokdeclarator. */
5351 error ("storage size of %qD isn%'t known", decl);
5352 TREE_TYPE (decl) = error_mark_node;
5353 }
5354 #if 0
5355 /* Keep this code around in case we later want to control debug info
5356 based on whether a type is "used". (jason 1999-11-11) */
5357
5358 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype))
5359 /* Let debugger know it should output info for this type. */
5360 note_debug_info_needed (ttype);
5361
5362 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
5363 note_debug_info_needed (DECL_CONTEXT (decl));
5364 #endif
5365
5366 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
5367 && DECL_SIZE (decl) != NULL_TREE
5368 && ! TREE_CONSTANT (DECL_SIZE (decl)))
5369 {
5370 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
5371 constant_expression_warning (DECL_SIZE (decl));
5372 else
5373 {
5374 error ("storage size of %qD isn%'t constant", decl);
5375 TREE_TYPE (decl) = error_mark_node;
5376 }
5377 }
5378 }
5379
5380 /* If a local static variable is declared in an inline function, or if
5381 we have a weak definition, we must endeavor to create only one
5382 instance of the variable at link-time. */
5383
5384 void
maybe_commonize_var(tree decl)5385 maybe_commonize_var (tree decl)
5386 {
5387 /* Static data in a function with comdat linkage also has comdat
5388 linkage. */
5389 if (TREE_STATIC (decl)
5390 /* Don't mess with __FUNCTION__. */
5391 && ! DECL_ARTIFICIAL (decl)
5392 && DECL_FUNCTION_SCOPE_P (decl)
5393 && vague_linkage_p (DECL_CONTEXT (decl)))
5394 {
5395 if (flag_weak)
5396 {
5397 /* With weak symbols, we simply make the variable COMDAT;
5398 that will cause copies in multiple translations units to
5399 be merged. */
5400 comdat_linkage (decl);
5401 }
5402 else
5403 {
5404 if (DECL_INITIAL (decl) == NULL_TREE
5405 || DECL_INITIAL (decl) == error_mark_node)
5406 {
5407 /* Without weak symbols, we can use COMMON to merge
5408 uninitialized variables. */
5409 TREE_PUBLIC (decl) = 1;
5410 DECL_COMMON (decl) = 1;
5411 }
5412 else
5413 {
5414 /* While for initialized variables, we must use internal
5415 linkage -- which means that multiple copies will not
5416 be merged. */
5417 TREE_PUBLIC (decl) = 0;
5418 DECL_COMMON (decl) = 0;
5419 if (warning_at (DECL_SOURCE_LOCATION (decl), 0,
5420 "sorry: semantics of inline function static "
5421 "data %q#D are wrong (you%'ll wind up "
5422 "with multiple copies)", decl))
5423 inform (DECL_SOURCE_LOCATION (decl),
5424 "you can work around this by removing the initializer");
5425 }
5426 }
5427 }
5428 }
5429
5430 /* Issue an error message if DECL is an uninitialized const variable. */
5431
5432 static void
check_for_uninitialized_const_var(tree decl)5433 check_for_uninitialized_const_var (tree decl)
5434 {
5435 tree type = strip_array_types (TREE_TYPE (decl));
5436
5437 /* ``Unless explicitly declared extern, a const object does not have
5438 external linkage and must be initialized. ($8.4; $12.1)'' ARM
5439 7.1.6 */
5440 if (VAR_P (decl)
5441 && TREE_CODE (type) != REFERENCE_TYPE
5442 && (CP_TYPE_CONST_P (type) || var_in_constexpr_fn (decl))
5443 && !DECL_INITIAL (decl))
5444 {
5445 tree field = default_init_uninitialized_part (type);
5446 if (!field)
5447 return;
5448
5449 if (CP_TYPE_CONST_P (type))
5450 permerror (DECL_SOURCE_LOCATION (decl),
5451 "uninitialized const %qD", decl);
5452 else
5453 {
5454 error_at (DECL_SOURCE_LOCATION (decl),
5455 "uninitialized variable %qD in %<constexpr%> function",
5456 decl);
5457 cp_function_chain->invalid_constexpr = true;
5458 }
5459
5460 if (CLASS_TYPE_P (type))
5461 {
5462 tree defaulted_ctor;
5463
5464 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
5465 "%q#T has no user-provided default constructor", type);
5466 defaulted_ctor = in_class_defaulted_default_constructor (type);
5467 if (defaulted_ctor)
5468 inform (DECL_SOURCE_LOCATION (defaulted_ctor),
5469 "constructor is not user-provided because it is "
5470 "explicitly defaulted in the class body");
5471 inform (DECL_SOURCE_LOCATION (field),
5472 "and the implicitly-defined constructor does not "
5473 "initialize %q#D", field);
5474 }
5475 }
5476 }
5477
5478 /* Structure holding the current initializer being processed by reshape_init.
5479 CUR is a pointer to the current element being processed, END is a pointer
5480 after the last element present in the initializer. */
5481 struct reshape_iter
5482 {
5483 constructor_elt *cur;
5484 constructor_elt *end;
5485 };
5486
5487 static tree reshape_init_r (tree, reshape_iter *, bool, tsubst_flags_t);
5488
5489 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
5490 returned is the next FIELD_DECL (possibly FIELD itself) that can be
5491 initialized. If there are no more such fields, the return value
5492 will be NULL. */
5493
5494 tree
next_initializable_field(tree field)5495 next_initializable_field (tree field)
5496 {
5497 while (field
5498 && (TREE_CODE (field) != FIELD_DECL
5499 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
5500 || DECL_ARTIFICIAL (field)))
5501 field = DECL_CHAIN (field);
5502
5503 return field;
5504 }
5505
5506 /* Subroutine of reshape_init_array and reshape_init_vector, which does
5507 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
5508 INTEGER_CST representing the size of the array minus one (the maximum index),
5509 or NULL_TREE if the array was declared without specifying the size. D is
5510 the iterator within the constructor. */
5511
5512 static tree
reshape_init_array_1(tree elt_type,tree max_index,reshape_iter * d,tsubst_flags_t complain)5513 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d,
5514 tsubst_flags_t complain)
5515 {
5516 tree new_init;
5517 bool sized_array_p = (max_index && TREE_CONSTANT (max_index));
5518 unsigned HOST_WIDE_INT max_index_cst = 0;
5519 unsigned HOST_WIDE_INT index;
5520
5521 /* The initializer for an array is always a CONSTRUCTOR. */
5522 new_init = build_constructor (init_list_type_node, NULL);
5523
5524 if (sized_array_p)
5525 {
5526 /* Minus 1 is used for zero sized arrays. */
5527 if (integer_all_onesp (max_index))
5528 return new_init;
5529
5530 if (tree_fits_uhwi_p (max_index))
5531 max_index_cst = tree_to_uhwi (max_index);
5532 /* sizetype is sign extended, not zero extended. */
5533 else
5534 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index));
5535 }
5536
5537 /* Loop until there are no more initializers. */
5538 for (index = 0;
5539 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
5540 ++index)
5541 {
5542 tree elt_init;
5543 constructor_elt *old_cur = d->cur;
5544
5545 check_array_designated_initializer (d->cur, index);
5546 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false,
5547 complain);
5548 if (elt_init == error_mark_node)
5549 return error_mark_node;
5550 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init),
5551 size_int (index), elt_init);
5552 if (!TREE_CONSTANT (elt_init))
5553 TREE_CONSTANT (new_init) = false;
5554
5555 /* This can happen with an invalid initializer (c++/54501). */
5556 if (d->cur == old_cur && !sized_array_p)
5557 break;
5558 }
5559
5560 return new_init;
5561 }
5562
5563 /* Subroutine of reshape_init_r, processes the initializers for arrays.
5564 Parameters are the same of reshape_init_r. */
5565
5566 static tree
reshape_init_array(tree type,reshape_iter * d,tsubst_flags_t complain)5567 reshape_init_array (tree type, reshape_iter *d, tsubst_flags_t complain)
5568 {
5569 tree max_index = NULL_TREE;
5570
5571 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5572
5573 if (TYPE_DOMAIN (type))
5574 max_index = array_type_nelts (type);
5575
5576 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5577 }
5578
5579 /* Subroutine of reshape_init_r, processes the initializers for vectors.
5580 Parameters are the same of reshape_init_r. */
5581
5582 static tree
reshape_init_vector(tree type,reshape_iter * d,tsubst_flags_t complain)5583 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain)
5584 {
5585 tree max_index = NULL_TREE;
5586
5587 gcc_assert (VECTOR_TYPE_P (type));
5588
5589 if (COMPOUND_LITERAL_P (d->cur->value))
5590 {
5591 tree value = d->cur->value;
5592 if (!same_type_p (TREE_TYPE (value), type))
5593 {
5594 if (complain & tf_error)
5595 error ("invalid type %qT as initializer for a vector of type %qT",
5596 TREE_TYPE (d->cur->value), type);
5597 value = error_mark_node;
5598 }
5599 ++d->cur;
5600 return value;
5601 }
5602
5603 /* For a vector, we initialize it as an array of the appropriate size. */
5604 if (VECTOR_TYPE_P (type))
5605 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1);
5606
5607 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, complain);
5608 }
5609
5610 /* Subroutine of reshape_init_r, processes the initializers for classes
5611 or union. Parameters are the same of reshape_init_r. */
5612
5613 static tree
reshape_init_class(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)5614 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
5615 tsubst_flags_t complain)
5616 {
5617 tree field;
5618 tree new_init;
5619
5620 gcc_assert (CLASS_TYPE_P (type));
5621
5622 /* The initializer for a class is always a CONSTRUCTOR. */
5623 new_init = build_constructor (init_list_type_node, NULL);
5624 field = next_initializable_field (TYPE_FIELDS (type));
5625
5626 if (!field)
5627 {
5628 /* [dcl.init.aggr]
5629
5630 An initializer for an aggregate member that is an
5631 empty class shall have the form of an empty
5632 initializer-list {}. */
5633 if (!first_initializer_p)
5634 {
5635 if (complain & tf_error)
5636 error ("initializer for %qT must be brace-enclosed", type);
5637 return error_mark_node;
5638 }
5639 return new_init;
5640 }
5641
5642 /* Loop through the initializable fields, gathering initializers. */
5643 while (d->cur != d->end)
5644 {
5645 tree field_init;
5646 constructor_elt *old_cur = d->cur;
5647
5648 /* Handle designated initializers, as an extension. */
5649 if (d->cur->index)
5650 {
5651 if (d->cur->index == error_mark_node)
5652 return error_mark_node;
5653
5654 if (TREE_CODE (d->cur->index) == FIELD_DECL)
5655 /* We already reshaped this. */
5656 gcc_assert (d->cur->index == field);
5657 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE)
5658 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
5659 else
5660 {
5661 if (complain & tf_error)
5662 error ("%<[%E] =%> used in a GNU-style designated initializer"
5663 " for class %qT", d->cur->index, type);
5664 return error_mark_node;
5665 }
5666
5667 if (!field || TREE_CODE (field) != FIELD_DECL)
5668 {
5669 if (complain & tf_error)
5670 error ("%qT has no non-static data member named %qD", type,
5671 d->cur->index);
5672 return error_mark_node;
5673 }
5674 }
5675
5676 /* If we processed all the member of the class, we are done. */
5677 if (!field)
5678 break;
5679
5680 field_init = reshape_init_r (TREE_TYPE (field), d,
5681 /*first_initializer_p=*/false, complain);
5682 if (field_init == error_mark_node)
5683 return error_mark_node;
5684
5685 if (d->cur == old_cur && d->cur->index)
5686 {
5687 /* This can happen with an invalid initializer for a flexible
5688 array member (c++/54441). */
5689 if (complain & tf_error)
5690 error ("invalid initializer for %q#D", field);
5691 return error_mark_node;
5692 }
5693
5694 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
5695
5696 /* [dcl.init.aggr]
5697
5698 When a union is initialized with a brace-enclosed
5699 initializer, the braces shall only contain an
5700 initializer for the first member of the union. */
5701 if (TREE_CODE (type) == UNION_TYPE)
5702 break;
5703
5704 field = next_initializable_field (DECL_CHAIN (field));
5705 }
5706
5707 return new_init;
5708 }
5709
5710 /* Subroutine of reshape_init_r. We're in a context where C99 initializer
5711 designators are not valid; either complain or return true to indicate
5712 that reshape_init_r should return error_mark_node. */
5713
5714 static bool
has_designator_problem(reshape_iter * d,tsubst_flags_t complain)5715 has_designator_problem (reshape_iter *d, tsubst_flags_t complain)
5716 {
5717 if (d->cur->index)
5718 {
5719 if (complain & tf_error)
5720 error ("C99 designator %qE outside aggregate initializer",
5721 d->cur->index);
5722 else
5723 return true;
5724 }
5725 return false;
5726 }
5727
5728 /* Subroutine of reshape_init, which processes a single initializer (part of
5729 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
5730 iterator within the CONSTRUCTOR which points to the initializer to process.
5731 FIRST_INITIALIZER_P is true if this is the first initializer of the
5732 outermost CONSTRUCTOR node. */
5733
5734 static tree
reshape_init_r(tree type,reshape_iter * d,bool first_initializer_p,tsubst_flags_t complain)5735 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p,
5736 tsubst_flags_t complain)
5737 {
5738 tree init = d->cur->value;
5739
5740 if (error_operand_p (init))
5741 return error_mark_node;
5742
5743 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type)
5744 && has_designator_problem (d, complain))
5745 return error_mark_node;
5746
5747 if (TREE_CODE (type) == COMPLEX_TYPE)
5748 {
5749 /* A complex type can be initialized from one or two initializers,
5750 but braces are not elided. */
5751 d->cur++;
5752 if (BRACE_ENCLOSED_INITIALIZER_P (init))
5753 {
5754 if (CONSTRUCTOR_NELTS (init) > 2)
5755 {
5756 if (complain & tf_error)
5757 error ("too many initializers for %qT", type);
5758 else
5759 return error_mark_node;
5760 }
5761 }
5762 else if (first_initializer_p && d->cur != d->end)
5763 {
5764 vec<constructor_elt, va_gc> *v = 0;
5765 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init);
5766 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value);
5767 if (has_designator_problem (d, complain))
5768 return error_mark_node;
5769 d->cur++;
5770 init = build_constructor (init_list_type_node, v);
5771 }
5772 return init;
5773 }
5774
5775 /* A non-aggregate type is always initialized with a single
5776 initializer. */
5777 if (!CP_AGGREGATE_TYPE_P (type))
5778 {
5779 /* It is invalid to initialize a non-aggregate type with a
5780 brace-enclosed initializer before C++0x.
5781 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
5782 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
5783 a CONSTRUCTOR (with a record type). */
5784 if (TREE_CODE (init) == CONSTRUCTOR
5785 /* Don't complain about a capture-init. */
5786 && !CONSTRUCTOR_IS_DIRECT_INIT (init)
5787 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
5788 {
5789 if (SCALAR_TYPE_P (type))
5790 {
5791 if (cxx_dialect < cxx11
5792 /* Isn't value-initialization. */
5793 || CONSTRUCTOR_NELTS (init) > 0)
5794 {
5795 if (complain & tf_error)
5796 error ("braces around scalar initializer for type %qT",
5797 type);
5798 init = error_mark_node;
5799 }
5800 }
5801 else
5802 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5803 }
5804
5805 d->cur++;
5806 return init;
5807 }
5808
5809 /* "If T is a class type and the initializer list has a single element of
5810 type cv U, where U is T or a class derived from T, the object is
5811 initialized from that element." Even if T is an aggregate. */
5812 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type))
5813 && first_initializer_p
5814 && d->end - d->cur == 1
5815 && reference_related_p (type, TREE_TYPE (init)))
5816 {
5817 d->cur++;
5818 return init;
5819 }
5820
5821 /* [dcl.init.aggr]
5822
5823 All implicit type conversions (clause _conv_) are considered when
5824 initializing the aggregate member with an initializer from an
5825 initializer-list. If the initializer can initialize a member,
5826 the member is initialized. Otherwise, if the member is itself a
5827 non-empty subaggregate, brace elision is assumed and the
5828 initializer is considered for the initialization of the first
5829 member of the subaggregate. */
5830 if (TREE_CODE (init) != CONSTRUCTOR
5831 /* But don't try this for the first initializer, since that would be
5832 looking through the outermost braces; A a2 = { a1 }; is not a
5833 valid aggregate initialization. */
5834 && !first_initializer_p
5835 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init))
5836 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL,
5837 complain)))
5838 {
5839 d->cur++;
5840 return init;
5841 }
5842
5843 /* [dcl.init.string]
5844
5845 A char array (whether plain char, signed char, or unsigned char)
5846 can be initialized by a string-literal (optionally enclosed in
5847 braces); a wchar_t array can be initialized by a wide
5848 string-literal (optionally enclosed in braces). */
5849 if (TREE_CODE (type) == ARRAY_TYPE
5850 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
5851 {
5852 tree str_init = init;
5853
5854 /* Strip one level of braces if and only if they enclose a single
5855 element (as allowed by [dcl.init.string]). */
5856 if (!first_initializer_p
5857 && TREE_CODE (str_init) == CONSTRUCTOR
5858 && vec_safe_length (CONSTRUCTOR_ELTS (str_init)) == 1)
5859 {
5860 str_init = (*CONSTRUCTOR_ELTS (str_init))[0].value;
5861 }
5862
5863 /* If it's a string literal, then it's the initializer for the array
5864 as a whole. Otherwise, continue with normal initialization for
5865 array types (one value per array element). */
5866 if (TREE_CODE (str_init) == STRING_CST)
5867 {
5868 if (has_designator_problem (d, complain))
5869 return error_mark_node;
5870 d->cur++;
5871 return str_init;
5872 }
5873 }
5874
5875 /* The following cases are about aggregates. If we are not within a full
5876 initializer already, and there is not a CONSTRUCTOR, it means that there
5877 is a missing set of braces (that is, we are processing the case for
5878 which reshape_init exists). */
5879 if (!first_initializer_p)
5880 {
5881 if (TREE_CODE (init) == CONSTRUCTOR)
5882 {
5883 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
5884 /* There is no need to reshape pointer-to-member function
5885 initializers, as they are always constructed correctly
5886 by the front end. */
5887 ;
5888 else if (COMPOUND_LITERAL_P (init))
5889 /* For a nested compound literal, there is no need to reshape since
5890 brace elision is not allowed. Even if we decided to allow it,
5891 we should add a call to reshape_init in finish_compound_literal,
5892 before calling digest_init, so changing this code would still
5893 not be necessary. */
5894 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
5895 else
5896 {
5897 ++d->cur;
5898 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5899 return reshape_init (type, init, complain);
5900 }
5901 }
5902
5903 if (complain & tf_warning)
5904 warning (OPT_Wmissing_braces,
5905 "missing braces around initializer for %qT",
5906 type);
5907 }
5908
5909 /* Dispatch to specialized routines. */
5910 if (CLASS_TYPE_P (type))
5911 return reshape_init_class (type, d, first_initializer_p, complain);
5912 else if (TREE_CODE (type) == ARRAY_TYPE)
5913 return reshape_init_array (type, d, complain);
5914 else if (VECTOR_TYPE_P (type))
5915 return reshape_init_vector (type, d, complain);
5916 else
5917 gcc_unreachable();
5918 }
5919
5920 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
5921 brace-enclosed aggregate initializer.
5922
5923 INIT is the CONSTRUCTOR containing the list of initializers describing
5924 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
5925 It may not presently match the shape of the TYPE; for example:
5926
5927 struct S { int a; int b; };
5928 struct S a[] = { 1, 2, 3, 4 };
5929
5930 Here INIT will hold a vector of four elements, rather than a
5931 vector of two elements, each itself a vector of two elements. This
5932 routine transforms INIT from the former form into the latter. The
5933 revised CONSTRUCTOR node is returned. */
5934
5935 tree
reshape_init(tree type,tree init,tsubst_flags_t complain)5936 reshape_init (tree type, tree init, tsubst_flags_t complain)
5937 {
5938 vec<constructor_elt, va_gc> *v;
5939 reshape_iter d;
5940 tree new_init;
5941
5942 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
5943
5944 v = CONSTRUCTOR_ELTS (init);
5945
5946 /* An empty constructor does not need reshaping, and it is always a valid
5947 initializer. */
5948 if (vec_safe_is_empty (v))
5949 return init;
5950
5951 /* Recurse on this CONSTRUCTOR. */
5952 d.cur = &(*v)[0];
5953 d.end = d.cur + v->length ();
5954
5955 new_init = reshape_init_r (type, &d, true, complain);
5956 if (new_init == error_mark_node)
5957 return error_mark_node;
5958
5959 /* Make sure all the element of the constructor were used. Otherwise,
5960 issue an error about exceeding initializers. */
5961 if (d.cur != d.end)
5962 {
5963 if (complain & tf_error)
5964 error ("too many initializers for %qT", type);
5965 else
5966 return error_mark_node;
5967 }
5968
5969 return new_init;
5970 }
5971
5972 /* Verify array initializer. Returns true if errors have been reported. */
5973
5974 bool
check_array_initializer(tree decl,tree type,tree init)5975 check_array_initializer (tree decl, tree type, tree init)
5976 {
5977 tree element_type = TREE_TYPE (type);
5978
5979 /* The array type itself need not be complete, because the
5980 initializer may tell us how many elements are in the array.
5981 But, the elements of the array must be complete. */
5982 if (!COMPLETE_TYPE_P (complete_type (element_type)))
5983 {
5984 if (decl)
5985 error ("elements of array %q#D have incomplete type", decl);
5986 else
5987 error ("elements of array %q#T have incomplete type", type);
5988 return true;
5989 }
5990 /* A compound literal can't have variable size. */
5991 if (init && !decl
5992 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
5993 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
5994 {
5995 error ("variable-sized compound literal");
5996 return true;
5997 }
5998 return false;
5999 }
6000
6001 /* Subroutine of check_initializer; args are passed down from that function.
6002 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */
6003
6004 static tree
build_aggr_init_full_exprs(tree decl,tree init,int flags)6005 build_aggr_init_full_exprs (tree decl, tree init, int flags)
6006
6007 {
6008 gcc_assert (stmts_are_full_exprs_p ());
6009 return build_aggr_init (decl, init, flags, tf_warning_or_error);
6010 }
6011
6012 /* Verify INIT (the initializer for DECL), and record the
6013 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
6014 grok_reference_init.
6015
6016 If the return value is non-NULL, it is an expression that must be
6017 evaluated dynamically to initialize DECL. */
6018
6019 static tree
check_initializer(tree decl,tree init,int flags,vec<tree,va_gc> ** cleanups)6020 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
6021 {
6022 tree type = TREE_TYPE (decl);
6023 tree init_code = NULL;
6024 tree core_type;
6025
6026 /* Things that are going to be initialized need to have complete
6027 type. */
6028 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
6029
6030 if (DECL_HAS_VALUE_EXPR_P (decl))
6031 {
6032 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder,
6033 it doesn't have storage to be initialized. */
6034 gcc_assert (init == NULL_TREE);
6035 return NULL_TREE;
6036 }
6037
6038 if (type == error_mark_node)
6039 /* We will have already complained. */
6040 return NULL_TREE;
6041
6042 if (TREE_CODE (type) == ARRAY_TYPE)
6043 {
6044 if (check_array_initializer (decl, type, init))
6045 return NULL_TREE;
6046 }
6047 else if (!COMPLETE_TYPE_P (type))
6048 {
6049 error ("%q#D has incomplete type", decl);
6050 TREE_TYPE (decl) = error_mark_node;
6051 return NULL_TREE;
6052 }
6053 else
6054 /* There is no way to make a variable-sized class type in GNU C++. */
6055 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
6056
6057 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
6058 {
6059 int init_len = vec_safe_length (CONSTRUCTOR_ELTS (init));
6060 if (SCALAR_TYPE_P (type))
6061 {
6062 if (init_len == 0)
6063 {
6064 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6065 init = build_zero_init (type, NULL_TREE, false);
6066 }
6067 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE)
6068 {
6069 error ("scalar object %qD requires one element in initializer",
6070 decl);
6071 TREE_TYPE (decl) = error_mark_node;
6072 return NULL_TREE;
6073 }
6074 }
6075 }
6076
6077 if (TREE_CODE (decl) == CONST_DECL)
6078 {
6079 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
6080
6081 DECL_INITIAL (decl) = init;
6082
6083 gcc_assert (init != NULL_TREE);
6084 init = NULL_TREE;
6085 }
6086 else if (!init && DECL_REALLY_EXTERN (decl))
6087 ;
6088 else if (init || type_build_ctor_call (type)
6089 || TREE_CODE (type) == REFERENCE_TYPE)
6090 {
6091 if (TREE_CODE (type) == REFERENCE_TYPE)
6092 {
6093 init = grok_reference_init (decl, type, init, flags);
6094 flags |= LOOKUP_ALREADY_DIGESTED;
6095 }
6096 else if (!init)
6097 check_for_uninitialized_const_var (decl);
6098 /* Do not reshape constructors of vectors (they don't need to be
6099 reshaped. */
6100 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
6101 {
6102 if (is_std_init_list (type))
6103 {
6104 init = perform_implicit_conversion (type, init,
6105 tf_warning_or_error);
6106 flags |= LOOKUP_ALREADY_DIGESTED;
6107 }
6108 else if (TYPE_NON_AGGREGATE_CLASS (type))
6109 {
6110 /* Don't reshape if the class has constructors. */
6111 if (cxx_dialect == cxx98)
6112 error ("in C++98 %qD must be initialized by constructor, "
6113 "not by %<{...}%>",
6114 decl);
6115 }
6116 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type))
6117 {
6118 error ("opaque vector types cannot be initialized");
6119 init = error_mark_node;
6120 }
6121 else
6122 {
6123 init = reshape_init (type, init, tf_warning_or_error);
6124 flags |= LOOKUP_NO_NARROWING;
6125 }
6126 }
6127 else if (TREE_CODE (init) == TREE_LIST
6128 && TREE_TYPE (init) != unknown_type_node
6129 && !MAYBE_CLASS_TYPE_P (type))
6130 {
6131 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6132
6133 /* We get here with code like `int a (2);' */
6134 init = build_x_compound_expr_from_list (init, ELK_INIT,
6135 tf_warning_or_error);
6136 }
6137
6138 /* If DECL has an array type without a specific bound, deduce the
6139 array size from the initializer. */
6140 maybe_deduce_size_from_array_init (decl, init);
6141 type = TREE_TYPE (decl);
6142 if (type == error_mark_node)
6143 return NULL_TREE;
6144
6145 if ((type_build_ctor_call (type) || CLASS_TYPE_P (type))
6146 && !(flags & LOOKUP_ALREADY_DIGESTED)
6147 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init)
6148 && CP_AGGREGATE_TYPE_P (type)
6149 && (CLASS_TYPE_P (type)
6150 || !TYPE_NEEDS_CONSTRUCTING (type)
6151 || type_has_extended_temps (type))))
6152 {
6153 init_code = build_aggr_init_full_exprs (decl, init, flags);
6154
6155 /* A constructor call is a non-trivial initializer even if
6156 it isn't explicitly written. */
6157 if (TREE_SIDE_EFFECTS (init_code))
6158 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true;
6159
6160 /* If this is a constexpr initializer, expand_default_init will
6161 have returned an INIT_EXPR rather than a CALL_EXPR. In that
6162 case, pull the initializer back out and pass it down into
6163 store_init_value. */
6164 while (TREE_CODE (init_code) == EXPR_STMT
6165 || TREE_CODE (init_code) == CONVERT_EXPR)
6166 init_code = TREE_OPERAND (init_code, 0);
6167 if (TREE_CODE (init_code) == INIT_EXPR)
6168 {
6169 init = TREE_OPERAND (init_code, 1);
6170 init_code = NULL_TREE;
6171 /* Don't call digest_init; it's unnecessary and will complain
6172 about aggregate initialization of non-aggregate classes. */
6173 flags |= LOOKUP_ALREADY_DIGESTED;
6174 }
6175 else if (DECL_DECLARED_CONSTEXPR_P (decl))
6176 {
6177 /* Declared constexpr, but no suitable initializer; massage
6178 init appropriately so we can pass it into store_init_value
6179 for the error. */
6180 if (CLASS_TYPE_P (type)
6181 && (!init || TREE_CODE (init) == TREE_LIST))
6182 {
6183 init = build_functional_cast (type, init, tf_none);
6184 if (TREE_CODE (init) == TARGET_EXPR)
6185 TARGET_EXPR_DIRECT_INIT_P (init) = true;
6186 }
6187 init_code = NULL_TREE;
6188 }
6189 else
6190 init = NULL_TREE;
6191 }
6192
6193 if (init && TREE_CODE (init) != TREE_VEC)
6194 {
6195 /* In aggregate initialization of a variable, each element
6196 initialization is a full-expression because there is no
6197 enclosing expression. */
6198 gcc_assert (stmts_are_full_exprs_p ());
6199
6200 init_code = store_init_value (decl, init, cleanups, flags);
6201
6202 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
6203 && DECL_INITIAL (decl)
6204 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
6205 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
6206 warning (0, "array %qD initialized by parenthesized string literal %qE",
6207 decl, DECL_INITIAL (decl));
6208 init = NULL;
6209 }
6210 }
6211 else
6212 {
6213 if (CLASS_TYPE_P (core_type = strip_array_types (type))
6214 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type)
6215 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type)))
6216 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false,
6217 /*complain=*/true);
6218
6219 check_for_uninitialized_const_var (decl);
6220 }
6221
6222 if (init && init != error_mark_node)
6223 init_code = build2 (INIT_EXPR, type, decl, init);
6224
6225 if (init_code)
6226 {
6227 /* We might have set these in cp_finish_decl. */
6228 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
6229 TREE_CONSTANT (decl) = false;
6230 }
6231
6232 if (init_code && DECL_IN_AGGR_P (decl))
6233 {
6234 static int explained = 0;
6235
6236 if (cxx_dialect < cxx11)
6237 error ("initializer invalid for static member with constructor");
6238 else
6239 error ("non-constant in-class initialization invalid for static "
6240 "member %qD", decl);
6241 if (!explained)
6242 {
6243 inform (input_location,
6244 "(an out of class initialization is required)");
6245 explained = 1;
6246 }
6247 return NULL_TREE;
6248 }
6249
6250 return init_code;
6251 }
6252
6253 /* If DECL is not a local variable, give it RTL. */
6254
6255 static void
make_rtl_for_nonlocal_decl(tree decl,tree init,const char * asmspec)6256 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
6257 {
6258 int toplev = toplevel_bindings_p ();
6259 int defer_p;
6260
6261 /* Set the DECL_ASSEMBLER_NAME for the object. */
6262 if (asmspec)
6263 {
6264 /* The `register' keyword, when used together with an
6265 asm-specification, indicates that the variable should be
6266 placed in a particular register. */
6267 if (VAR_P (decl) && DECL_REGISTER (decl))
6268 {
6269 set_user_assembler_name (decl, asmspec);
6270 DECL_HARD_REGISTER (decl) = 1;
6271 }
6272 else
6273 {
6274 if (TREE_CODE (decl) == FUNCTION_DECL
6275 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
6276 set_builtin_user_assembler_name (decl, asmspec);
6277 set_user_assembler_name (decl, asmspec);
6278 }
6279 }
6280
6281 /* Handle non-variables up front. */
6282 if (!VAR_P (decl))
6283 {
6284 rest_of_decl_compilation (decl, toplev, at_eof);
6285 return;
6286 }
6287
6288 /* If we see a class member here, it should be a static data
6289 member. */
6290 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
6291 {
6292 gcc_assert (TREE_STATIC (decl));
6293 /* An in-class declaration of a static data member should be
6294 external; it is only a declaration, and not a definition. */
6295 if (init == NULL_TREE)
6296 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
6297 }
6298
6299 /* We don't create any RTL for local variables. */
6300 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6301 return;
6302
6303 /* We defer emission of local statics until the corresponding
6304 DECL_EXPR is expanded. But with constexpr its function might never
6305 be expanded, so go ahead and tell cgraph about the variable now. */
6306 defer_p = ((DECL_FUNCTION_SCOPE_P (decl)
6307 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (decl)))
6308 || DECL_VIRTUAL_P (decl));
6309
6310 /* Defer template instantiations. */
6311 if (DECL_LANG_SPECIFIC (decl)
6312 && DECL_IMPLICIT_INSTANTIATION (decl))
6313 defer_p = 1;
6314
6315 /* If we're not deferring, go ahead and assemble the variable. */
6316 if (!defer_p)
6317 rest_of_decl_compilation (decl, toplev, at_eof);
6318 }
6319
6320 /* walk_tree helper for wrap_temporary_cleanups, below. */
6321
6322 static tree
wrap_cleanups_r(tree * stmt_p,int * walk_subtrees,void * data)6323 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data)
6324 {
6325 /* Stop at types or full-expression boundaries. */
6326 if (TYPE_P (*stmt_p)
6327 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR)
6328 {
6329 *walk_subtrees = 0;
6330 return NULL_TREE;
6331 }
6332
6333 if (TREE_CODE (*stmt_p) == TARGET_EXPR)
6334 {
6335 tree guard = (tree)data;
6336 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p);
6337
6338 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard);
6339 /* Tell honor_protect_cleanup_actions to handle this as a separate
6340 cleanup. */
6341 TRY_CATCH_IS_CLEANUP (tcleanup) = 1;
6342
6343 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup;
6344 }
6345
6346 return NULL_TREE;
6347 }
6348
6349 /* We're initializing a local variable which has a cleanup GUARD. If there
6350 are any temporaries used in the initializer INIT of this variable, we
6351 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the
6352 variable will be cleaned up properly if one of them throws.
6353
6354 Unfortunately, there's no way to express this properly in terms of
6355 nesting, as the regions for the temporaries overlap the region for the
6356 variable itself; if there are two temporaries, the variable needs to be
6357 the first thing destroyed if either of them throws. However, we only
6358 want to run the variable's cleanup if it actually got constructed. So
6359 we need to guard the temporary cleanups with the variable's cleanup if
6360 they are run on the normal path, but not if they are run on the
6361 exceptional path. We implement this by telling
6362 honor_protect_cleanup_actions to strip the variable cleanup from the
6363 exceptional path. */
6364
6365 static void
wrap_temporary_cleanups(tree init,tree guard)6366 wrap_temporary_cleanups (tree init, tree guard)
6367 {
6368 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard);
6369 }
6370
6371 /* Generate code to initialize DECL (a local variable). */
6372
6373 static void
initialize_local_var(tree decl,tree init)6374 initialize_local_var (tree decl, tree init)
6375 {
6376 tree type = TREE_TYPE (decl);
6377 tree cleanup;
6378 int already_used;
6379
6380 gcc_assert (VAR_P (decl)
6381 || TREE_CODE (decl) == RESULT_DECL);
6382 gcc_assert (!TREE_STATIC (decl));
6383
6384 if (DECL_SIZE (decl) == NULL_TREE)
6385 {
6386 /* If we used it already as memory, it must stay in memory. */
6387 DECL_INITIAL (decl) = NULL_TREE;
6388 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
6389 return;
6390 }
6391
6392 if (type == error_mark_node)
6393 return;
6394
6395 /* Compute and store the initial value. */
6396 already_used = TREE_USED (decl) || TREE_USED (type);
6397 if (TREE_USED (type))
6398 DECL_READ_P (decl) = 1;
6399
6400 /* Generate a cleanup, if necessary. */
6401 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error);
6402
6403 /* Perform the initialization. */
6404 if (init)
6405 {
6406 tree rinit = (TREE_CODE (init) == INIT_EXPR
6407 ? TREE_OPERAND (init, 1) : NULL_TREE);
6408 if (rinit && !TREE_SIDE_EFFECTS (rinit))
6409 {
6410 /* Stick simple initializers in DECL_INITIAL so that
6411 -Wno-init-self works (c++/34772). */
6412 gcc_assert (TREE_OPERAND (init, 0) == decl);
6413 DECL_INITIAL (decl) = rinit;
6414
6415 if (warn_init_self && TREE_CODE (type) == REFERENCE_TYPE)
6416 {
6417 STRIP_NOPS (rinit);
6418 if (rinit == decl)
6419 warning_at (DECL_SOURCE_LOCATION (decl),
6420 OPT_Winit_self,
6421 "reference %qD is initialized with itself", decl);
6422 }
6423 }
6424 else
6425 {
6426 int saved_stmts_are_full_exprs_p;
6427
6428 /* If we're only initializing a single object, guard the
6429 destructors of any temporaries used in its initializer with
6430 its destructor. This isn't right for arrays because each
6431 element initialization is a full-expression. */
6432 if (cleanup && TREE_CODE (type) != ARRAY_TYPE)
6433 wrap_temporary_cleanups (init, cleanup);
6434
6435 gcc_assert (building_stmt_list_p ());
6436 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
6437 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
6438 finish_expr_stmt (init);
6439 current_stmt_tree ()->stmts_are_full_exprs_p =
6440 saved_stmts_are_full_exprs_p;
6441 }
6442 }
6443
6444 /* Set this to 0 so we can tell whether an aggregate which was
6445 initialized was ever used. Don't do this if it has a
6446 destructor, so we don't complain about the 'resource
6447 allocation is initialization' idiom. Now set
6448 attribute((unused)) on types so decls of that type will be
6449 marked used. (see TREE_USED, above.) */
6450 if (TYPE_NEEDS_CONSTRUCTING (type)
6451 && ! already_used
6452 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
6453 && DECL_NAME (decl))
6454 TREE_USED (decl) = 0;
6455 else if (already_used)
6456 TREE_USED (decl) = 1;
6457
6458 if (cleanup)
6459 finish_decl_cleanup (decl, cleanup);
6460 }
6461
6462 /* DECL is a VAR_DECL for a compiler-generated variable with static
6463 storage duration (like a virtual table) whose initializer is a
6464 compile-time constant. Initialize the variable and provide it to the
6465 back end. */
6466
6467 void
initialize_artificial_var(tree decl,vec<constructor_elt,va_gc> * v)6468 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v)
6469 {
6470 tree init;
6471 gcc_assert (DECL_ARTIFICIAL (decl));
6472 init = build_constructor (TREE_TYPE (decl), v);
6473 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
6474 DECL_INITIAL (decl) = init;
6475 DECL_INITIALIZED_P (decl) = 1;
6476 determine_visibility (decl);
6477 layout_var_decl (decl);
6478 maybe_commonize_var (decl);
6479 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
6480 }
6481
6482 /* INIT is the initializer for a variable, as represented by the
6483 parser. Returns true iff INIT is type-dependent. */
6484
6485 static bool
type_dependent_init_p(tree init)6486 type_dependent_init_p (tree init)
6487 {
6488 if (TREE_CODE (init) == TREE_LIST)
6489 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6490 return any_type_dependent_elements_p (init);
6491 else if (TREE_CODE (init) == CONSTRUCTOR)
6492 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6493 {
6494 if (dependent_type_p (TREE_TYPE (init)))
6495 return true;
6496
6497 vec<constructor_elt, va_gc> *elts;
6498 size_t nelts;
6499 size_t i;
6500
6501 elts = CONSTRUCTOR_ELTS (init);
6502 nelts = vec_safe_length (elts);
6503 for (i = 0; i < nelts; ++i)
6504 if (type_dependent_init_p ((*elts)[i].value))
6505 return true;
6506 }
6507 else
6508 /* It must be a simple expression, e.g., int i = 3; */
6509 return type_dependent_expression_p (init);
6510
6511 return false;
6512 }
6513
6514 /* INIT is the initializer for a variable, as represented by the
6515 parser. Returns true iff INIT is value-dependent. */
6516
6517 static bool
value_dependent_init_p(tree init)6518 value_dependent_init_p (tree init)
6519 {
6520 if (TREE_CODE (init) == TREE_LIST)
6521 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
6522 return any_value_dependent_elements_p (init);
6523 else if (TREE_CODE (init) == CONSTRUCTOR)
6524 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
6525 {
6526 vec<constructor_elt, va_gc> *elts;
6527 size_t nelts;
6528 size_t i;
6529
6530 elts = CONSTRUCTOR_ELTS (init);
6531 nelts = vec_safe_length (elts);
6532 for (i = 0; i < nelts; ++i)
6533 if (value_dependent_init_p ((*elts)[i].value))
6534 return true;
6535 }
6536 else
6537 /* It must be a simple expression, e.g., int i = 3; */
6538 return value_dependent_expression_p (init);
6539
6540 return false;
6541 }
6542
6543 // Returns true if a DECL is VAR_DECL with the concept specifier.
6544 static inline bool
is_concept_var(tree decl)6545 is_concept_var (tree decl)
6546 {
6547 return (VAR_P (decl)
6548 // Not all variables have DECL_LANG_SPECIFIC.
6549 && DECL_LANG_SPECIFIC (decl)
6550 && DECL_DECLARED_CONCEPT_P (decl));
6551 }
6552
6553 /* A helper function to be called via walk_tree. If any label exists
6554 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */
6555
6556 static tree
notice_forced_label_r(tree * tp,int * walk_subtrees,void *)6557 notice_forced_label_r (tree *tp, int *walk_subtrees, void *)
6558 {
6559 if (TYPE_P (*tp))
6560 *walk_subtrees = 0;
6561 if (TREE_CODE (*tp) == LABEL_DECL)
6562 cfun->has_forced_label_in_static = 1;
6563 return NULL_TREE;
6564 }
6565
6566 /* Finish processing of a declaration;
6567 install its line number and initial value.
6568 If the length of an array type is not known before,
6569 it must be determined now, from the initial value, or it is an error.
6570
6571 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
6572 true, then INIT is an integral constant expression.
6573
6574 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
6575 if the (init) syntax was used. */
6576
6577 void
cp_finish_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)6578 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
6579 tree asmspec_tree, int flags)
6580 {
6581 tree type;
6582 vec<tree, va_gc> *cleanups = NULL;
6583 const char *asmspec = NULL;
6584 int was_readonly = 0;
6585 bool var_definition_p = false;
6586 tree auto_node;
6587
6588 if (decl == error_mark_node)
6589 return;
6590 else if (! decl)
6591 {
6592 if (init)
6593 error ("assignment (not initialization) in declaration");
6594 return;
6595 }
6596
6597 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
6598 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
6599 gcc_assert (TREE_CODE (decl) != PARM_DECL);
6600
6601 type = TREE_TYPE (decl);
6602 if (type == error_mark_node)
6603 return;
6604
6605 /* If a name was specified, get the string. */
6606 if (at_namespace_scope_p ())
6607 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
6608 if (asmspec_tree && asmspec_tree != error_mark_node)
6609 asmspec = TREE_STRING_POINTER (asmspec_tree);
6610
6611 if (current_class_type
6612 && CP_DECL_CONTEXT (decl) == current_class_type
6613 && TYPE_BEING_DEFINED (current_class_type)
6614 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)
6615 && (DECL_INITIAL (decl) || init))
6616 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
6617
6618 if (TREE_CODE (decl) != FUNCTION_DECL
6619 && (auto_node = type_uses_auto (type)))
6620 {
6621 tree d_init;
6622 if (init == NULL_TREE)
6623 {
6624 if (DECL_LANG_SPECIFIC (decl)
6625 && DECL_TEMPLATE_INSTANTIATION (decl)
6626 && !DECL_TEMPLATE_INSTANTIATED (decl))
6627 {
6628 /* init is null because we're deferring instantiating the
6629 initializer until we need it. Well, we need it now. */
6630 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false);
6631 return;
6632 }
6633
6634 error ("declaration of %q#D has no initializer", decl);
6635 TREE_TYPE (decl) = error_mark_node;
6636 return;
6637 }
6638 d_init = init;
6639 if (TREE_CODE (d_init) == TREE_LIST)
6640 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT,
6641 tf_warning_or_error);
6642 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error);
6643 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init,
6644 auto_node,
6645 tf_warning_or_error,
6646 adc_variable_type);
6647 if (type == error_mark_node)
6648 return;
6649 if (TREE_CODE (type) == FUNCTION_TYPE)
6650 {
6651 error ("initializer for %<decltype(auto) %D%> has function type "
6652 "(did you forget the %<()%> ?)", decl);
6653 TREE_TYPE (decl) = error_mark_node;
6654 return;
6655 }
6656 cp_apply_type_quals_to_decl (cp_type_quals (type), decl);
6657 }
6658
6659 if (!ensure_literal_type_for_constexpr_object (decl))
6660 DECL_DECLARED_CONSTEXPR_P (decl) = 0;
6661
6662 if (VAR_P (decl)
6663 && DECL_CLASS_SCOPE_P (decl)
6664 && DECL_INITIALIZED_IN_CLASS_P (decl))
6665 check_static_variable_definition (decl, type);
6666
6667 if (init && TREE_CODE (decl) == FUNCTION_DECL)
6668 {
6669 tree clone;
6670 if (init == ridpointers[(int)RID_DELETE])
6671 {
6672 /* FIXME check this is 1st decl. */
6673 DECL_DELETED_FN (decl) = 1;
6674 DECL_DECLARED_INLINE_P (decl) = 1;
6675 DECL_INITIAL (decl) = error_mark_node;
6676 FOR_EACH_CLONE (clone, decl)
6677 {
6678 DECL_DELETED_FN (clone) = 1;
6679 DECL_DECLARED_INLINE_P (clone) = 1;
6680 DECL_INITIAL (clone) = error_mark_node;
6681 }
6682 init = NULL_TREE;
6683 }
6684 else if (init == ridpointers[(int)RID_DEFAULT])
6685 {
6686 if (defaultable_fn_check (decl))
6687 DECL_DEFAULTED_FN (decl) = 1;
6688 else
6689 DECL_INITIAL (decl) = NULL_TREE;
6690 }
6691 }
6692
6693 if (init && VAR_P (decl))
6694 {
6695 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
6696 /* If DECL is a reference, then we want to know whether init is a
6697 reference constant; init_const_expr_p as passed tells us whether
6698 it's an rvalue constant. */
6699 if (TREE_CODE (type) == REFERENCE_TYPE)
6700 init_const_expr_p = potential_constant_expression (init);
6701 if (init_const_expr_p)
6702 {
6703 /* Set these flags now for templates. We'll update the flags in
6704 store_init_value for instantiations. */
6705 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
6706 if (decl_maybe_constant_var_p (decl))
6707 TREE_CONSTANT (decl) = 1;
6708 }
6709 }
6710
6711 if (processing_template_decl)
6712 {
6713 bool type_dependent_p;
6714
6715 /* Add this declaration to the statement-tree. */
6716 if (at_function_scope_p ())
6717 add_decl_expr (decl);
6718
6719 type_dependent_p = dependent_type_p (type);
6720
6721 if (check_for_bare_parameter_packs (init))
6722 {
6723 init = NULL_TREE;
6724 DECL_INITIAL (decl) = NULL_TREE;
6725 }
6726
6727 /* Generally, initializers in templates are expanded when the
6728 template is instantiated. But, if DECL is a variable constant
6729 then it can be used in future constant expressions, so its value
6730 must be available. */
6731
6732 if (!VAR_P (decl) || type_dependent_p)
6733 /* We can't do anything if the decl has dependent type. */;
6734 else if (init
6735 && init_const_expr_p
6736 && TREE_CODE (type) != REFERENCE_TYPE
6737 && decl_maybe_constant_var_p (decl)
6738 && !type_dependent_init_p (init)
6739 && !value_dependent_init_p (init))
6740 {
6741 /* This variable seems to be a non-dependent constant, so process
6742 its initializer. If check_initializer returns non-null the
6743 initialization wasn't constant after all. */
6744 tree init_code;
6745 cleanups = make_tree_vector ();
6746 init_code = check_initializer (decl, init, flags, &cleanups);
6747 if (init_code == NULL_TREE)
6748 init = NULL_TREE;
6749 release_tree_vector (cleanups);
6750 }
6751 else if (!init && is_concept_var (decl))
6752 error ("variable concept has no initializer");
6753 else if (!DECL_PRETTY_FUNCTION_P (decl))
6754 {
6755 /* Deduce array size even if the initializer is dependent. */
6756 maybe_deduce_size_from_array_init (decl, init);
6757 /* And complain about multiple initializers. */
6758 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init)
6759 && !MAYBE_CLASS_TYPE_P (type))
6760 init = build_x_compound_expr_from_list (init, ELK_INIT,
6761 tf_warning_or_error);
6762 }
6763
6764 if (init)
6765 DECL_INITIAL (decl) = init;
6766 return;
6767 }
6768
6769 /* Just store non-static data member initializers for later. */
6770 if (init && TREE_CODE (decl) == FIELD_DECL)
6771 DECL_INITIAL (decl) = init;
6772
6773 /* Take care of TYPE_DECLs up front. */
6774 if (TREE_CODE (decl) == TYPE_DECL)
6775 {
6776 if (type != error_mark_node
6777 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl))
6778 {
6779 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
6780 warning (0, "shadowing previous type declaration of %q#D", decl);
6781 set_identifier_type_value (DECL_NAME (decl), decl);
6782 }
6783
6784 /* If we have installed this as the canonical typedef for this
6785 type, and that type has not been defined yet, delay emitting
6786 the debug information for it, as we will emit it later. */
6787 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
6788 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
6789 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6790
6791 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl),
6792 at_eof);
6793 return;
6794 }
6795
6796 /* A reference will be modified here, as it is initialized. */
6797 if (! DECL_EXTERNAL (decl)
6798 && TREE_READONLY (decl)
6799 && TREE_CODE (type) == REFERENCE_TYPE)
6800 {
6801 was_readonly = 1;
6802 TREE_READONLY (decl) = 0;
6803 }
6804
6805 if (VAR_P (decl))
6806 {
6807 /* If this is a local variable that will need a mangled name,
6808 register it now. We must do this before processing the
6809 initializer for the variable, since the initialization might
6810 require a guard variable, and since the mangled name of the
6811 guard variable will depend on the mangled name of this
6812 variable. */
6813 if (DECL_FUNCTION_SCOPE_P (decl)
6814 && TREE_STATIC (decl)
6815 && !DECL_ARTIFICIAL (decl))
6816 {
6817 push_local_name (decl);
6818 /* Normally has_forced_label_in_static is set during GIMPLE
6819 lowering, but [cd]tors are never actually compiled directly.
6820 We need to set this early so we can deal with the label
6821 address extension. */
6822 if ((DECL_CONSTRUCTOR_P (current_function_decl)
6823 || DECL_DESTRUCTOR_P (current_function_decl))
6824 && init)
6825 {
6826 walk_tree (&init, notice_forced_label_r, NULL, NULL);
6827 add_local_decl (cfun, decl);
6828 }
6829 /* And make sure it's in the symbol table for
6830 c_parse_final_cleanups to find. */
6831 varpool_node::get_create (decl);
6832 }
6833
6834 /* Convert the initializer to the type of DECL, if we have not
6835 already initialized DECL. */
6836 if (!DECL_INITIALIZED_P (decl)
6837 /* If !DECL_EXTERNAL then DECL is being defined. In the
6838 case of a static data member initialized inside the
6839 class-specifier, there can be an initializer even if DECL
6840 is *not* defined. */
6841 && (!DECL_EXTERNAL (decl) || init))
6842 {
6843 if (TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6844 {
6845 tree jclass
6846 = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
6847 /* Allow libjava/prims.cc define primitive classes. */
6848 if (init != NULL_TREE
6849 || jclass == NULL_TREE
6850 || TREE_CODE (jclass) != TYPE_DECL
6851 || !POINTER_TYPE_P (TREE_TYPE (jclass))
6852 || !same_type_ignoring_top_level_qualifiers_p
6853 (type, TREE_TYPE (TREE_TYPE (jclass))))
6854 error ("Java object %qD not allocated with %<new%>", decl);
6855 init = NULL_TREE;
6856 }
6857 cleanups = make_tree_vector ();
6858 init = check_initializer (decl, init, flags, &cleanups);
6859
6860 /* Handle:
6861
6862 [dcl.init]
6863
6864 The memory occupied by any object of static storage
6865 duration is zero-initialized at program startup before
6866 any other initialization takes place.
6867
6868 We cannot create an appropriate initializer until after
6869 the type of DECL is finalized. If DECL_INITIAL is set,
6870 then the DECL is statically initialized, and any
6871 necessary zero-initialization has already been performed. */
6872 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
6873 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
6874 /*nelts=*/NULL_TREE,
6875 /*static_storage_p=*/true);
6876 /* Remember that the initialization for this variable has
6877 taken place. */
6878 DECL_INITIALIZED_P (decl) = 1;
6879 /* This declaration is the definition of this variable,
6880 unless we are initializing a static data member within
6881 the class specifier. */
6882 if (!DECL_EXTERNAL (decl))
6883 var_definition_p = true;
6884 }
6885 /* If the variable has an array type, lay out the type, even if
6886 there is no initializer. It is valid to index through the
6887 array, and we must get TYPE_ALIGN set correctly on the array
6888 type. */
6889 else if (TREE_CODE (type) == ARRAY_TYPE)
6890 layout_type (type);
6891
6892 if (TREE_STATIC (decl)
6893 && !at_function_scope_p ()
6894 && current_function_decl == NULL)
6895 /* So decl is a global variable or a static member of a
6896 non local class. Record the types it uses
6897 so that we can decide later to emit debug info for them. */
6898 record_types_used_by_current_var_decl (decl);
6899 }
6900 else if (TREE_CODE (decl) == FIELD_DECL
6901 && TYPE_FOR_JAVA (type) && MAYBE_CLASS_TYPE_P (type))
6902 error ("non-static data member %qD has Java class type", decl);
6903
6904 /* Add this declaration to the statement-tree. This needs to happen
6905 after the call to check_initializer so that the DECL_EXPR for a
6906 reference temp is added before the DECL_EXPR for the reference itself. */
6907 if (DECL_FUNCTION_SCOPE_P (decl))
6908 {
6909 /* If we're building a variable sized type, and we might be
6910 reachable other than via the top of the current binding
6911 level, then create a new BIND_EXPR so that we deallocate
6912 the object at the right time. */
6913 if (VAR_P (decl)
6914 && DECL_SIZE (decl)
6915 && !TREE_CONSTANT (DECL_SIZE (decl))
6916 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
6917 {
6918 tree bind;
6919 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
6920 TREE_SIDE_EFFECTS (bind) = 1;
6921 add_stmt (bind);
6922 BIND_EXPR_BODY (bind) = push_stmt_list ();
6923 }
6924 add_decl_expr (decl);
6925 }
6926
6927 /* Let the middle end know about variables and functions -- but not
6928 static data members in uninstantiated class templates. */
6929 if (VAR_OR_FUNCTION_DECL_P (decl))
6930 {
6931 if (VAR_P (decl))
6932 {
6933 layout_var_decl (decl);
6934 maybe_commonize_var (decl);
6935 }
6936
6937 /* This needs to happen after the linkage is set. */
6938 determine_visibility (decl);
6939
6940 if (var_definition_p && TREE_STATIC (decl))
6941 {
6942 /* If a TREE_READONLY variable needs initialization
6943 at runtime, it is no longer readonly and we need to
6944 avoid MEM_READONLY_P being set on RTL created for it. */
6945 if (init)
6946 {
6947 if (TREE_READONLY (decl))
6948 TREE_READONLY (decl) = 0;
6949 was_readonly = 0;
6950 }
6951 else if (was_readonly)
6952 TREE_READONLY (decl) = 1;
6953
6954 /* Likewise if it needs destruction. */
6955 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6956 TREE_READONLY (decl) = 0;
6957 }
6958
6959 make_rtl_for_nonlocal_decl (decl, init, asmspec);
6960
6961 /* Check for abstractness of the type. Notice that there is no
6962 need to strip array types here since the check for those types
6963 is already done within create_array_type_for_decl. */
6964 abstract_virtuals_error (decl, type);
6965
6966 if (TREE_TYPE (decl) == error_mark_node)
6967 /* No initialization required. */
6968 ;
6969 else if (TREE_CODE (decl) == FUNCTION_DECL)
6970 {
6971 if (init)
6972 {
6973 if (init == ridpointers[(int)RID_DEFAULT])
6974 {
6975 /* An out-of-class default definition is defined at
6976 the point where it is explicitly defaulted. */
6977 if (DECL_DELETED_FN (decl))
6978 maybe_explain_implicit_delete (decl);
6979 else if (DECL_INITIAL (decl) == error_mark_node)
6980 synthesize_method (decl);
6981 }
6982 else
6983 error ("function %q#D is initialized like a variable", decl);
6984 }
6985 /* else no initialization required. */
6986 }
6987 else if (DECL_EXTERNAL (decl)
6988 && ! (DECL_LANG_SPECIFIC (decl)
6989 && DECL_NOT_REALLY_EXTERN (decl)))
6990 {
6991 if (init)
6992 DECL_INITIAL (decl) = init;
6993 }
6994 /* A variable definition. */
6995 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
6996 /* Initialize the local variable. */
6997 initialize_local_var (decl, init);
6998
6999 /* If a variable is defined, and then a subsequent
7000 definition with external linkage is encountered, we will
7001 get here twice for the same variable. We want to avoid
7002 calling expand_static_init more than once. For variables
7003 that are not static data members, we can call
7004 expand_static_init only when we actually process the
7005 initializer. It is not legal to redeclare a static data
7006 member, so this issue does not arise in that case. */
7007 else if (var_definition_p && TREE_STATIC (decl))
7008 expand_static_init (decl, init);
7009 }
7010
7011 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
7012 reference, insert it in the statement-tree now. */
7013 if (cleanups)
7014 {
7015 unsigned i; tree t;
7016 FOR_EACH_VEC_ELT (*cleanups, i, t)
7017 push_cleanup (decl, t, false);
7018 release_tree_vector (cleanups);
7019 }
7020
7021 if (was_readonly)
7022 TREE_READONLY (decl) = 1;
7023
7024 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
7025 }
7026
7027 /* Returns a declaration for a VAR_DECL as if:
7028
7029 extern "C" TYPE NAME;
7030
7031 had been seen. Used to create compiler-generated global
7032 variables. */
7033
7034 static tree
declare_global_var(tree name,tree type)7035 declare_global_var (tree name, tree type)
7036 {
7037 tree decl;
7038
7039 push_to_top_level ();
7040 decl = build_decl (input_location, VAR_DECL, name, type);
7041 TREE_PUBLIC (decl) = 1;
7042 DECL_EXTERNAL (decl) = 1;
7043 DECL_ARTIFICIAL (decl) = 1;
7044 /* If the user has explicitly declared this variable (perhaps
7045 because the code we are compiling is part of a low-level runtime
7046 library), then it is possible that our declaration will be merged
7047 with theirs by pushdecl. */
7048 decl = pushdecl (decl);
7049 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0);
7050 pop_from_top_level ();
7051
7052 return decl;
7053 }
7054
7055 /* Returns the type for the argument to "__cxa_atexit" (or "atexit",
7056 if "__cxa_atexit" is not being used) corresponding to the function
7057 to be called when the program exits. */
7058
7059 static tree
get_atexit_fn_ptr_type(void)7060 get_atexit_fn_ptr_type (void)
7061 {
7062 tree fn_type;
7063
7064 if (!atexit_fn_ptr_type_node)
7065 {
7066 tree arg_type;
7067 if (flag_use_cxa_atexit
7068 && !targetm.cxx.use_atexit_for_cxa_atexit ())
7069 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */
7070 arg_type = ptr_type_node;
7071 else
7072 /* The parameter to "atexit" is "void (*)(void)". */
7073 arg_type = NULL_TREE;
7074
7075 fn_type = build_function_type_list (void_type_node,
7076 arg_type, NULL_TREE);
7077 atexit_fn_ptr_type_node = build_pointer_type (fn_type);
7078 }
7079
7080 return atexit_fn_ptr_type_node;
7081 }
7082
7083 /* Returns a pointer to the `atexit' function. Note that if
7084 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
7085 `__cxa_atexit' function specified in the IA64 C++ ABI. */
7086
7087 static tree
get_atexit_node(void)7088 get_atexit_node (void)
7089 {
7090 tree atexit_fndecl;
7091 tree fn_type;
7092 tree fn_ptr_type;
7093 const char *name;
7094 bool use_aeabi_atexit;
7095
7096 if (atexit_node)
7097 return atexit_node;
7098
7099 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ())
7100 {
7101 /* The declaration for `__cxa_atexit' is:
7102
7103 int __cxa_atexit (void (*)(void *), void *, void *)
7104
7105 We build up the argument types and then the function type
7106 itself. */
7107 tree argtype0, argtype1, argtype2;
7108
7109 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
7110 /* First, build the pointer-to-function type for the first
7111 argument. */
7112 fn_ptr_type = get_atexit_fn_ptr_type ();
7113 /* Then, build the rest of the argument types. */
7114 argtype2 = ptr_type_node;
7115 if (use_aeabi_atexit)
7116 {
7117 argtype1 = fn_ptr_type;
7118 argtype0 = ptr_type_node;
7119 }
7120 else
7121 {
7122 argtype1 = ptr_type_node;
7123 argtype0 = fn_ptr_type;
7124 }
7125 /* And the final __cxa_atexit type. */
7126 fn_type = build_function_type_list (integer_type_node,
7127 argtype0, argtype1, argtype2,
7128 NULL_TREE);
7129 if (use_aeabi_atexit)
7130 name = "__aeabi_atexit";
7131 else
7132 name = "__cxa_atexit";
7133 }
7134 else
7135 {
7136 /* The declaration for `atexit' is:
7137
7138 int atexit (void (*)());
7139
7140 We build up the argument types and then the function type
7141 itself. */
7142 fn_ptr_type = get_atexit_fn_ptr_type ();
7143 /* Build the final atexit type. */
7144 fn_type = build_function_type_list (integer_type_node,
7145 fn_ptr_type, NULL_TREE);
7146 name = "atexit";
7147 }
7148
7149 /* Now, build the function declaration. */
7150 push_lang_context (lang_name_c);
7151 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW);
7152 mark_used (atexit_fndecl);
7153 pop_lang_context ();
7154 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error);
7155
7156 return atexit_node;
7157 }
7158
7159 /* Like get_atexit_node, but for thread-local cleanups. */
7160
7161 static tree
get_thread_atexit_node(void)7162 get_thread_atexit_node (void)
7163 {
7164 /* The declaration for `__cxa_thread_atexit' is:
7165
7166 int __cxa_thread_atexit (void (*)(void *), void *, void *) */
7167 tree fn_type = build_function_type_list (integer_type_node,
7168 get_atexit_fn_ptr_type (),
7169 ptr_type_node, ptr_type_node,
7170 NULL_TREE);
7171
7172 /* Now, build the function declaration. */
7173 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type,
7174 ECF_LEAF | ECF_NOTHROW);
7175 return decay_conversion (atexit_fndecl, tf_warning_or_error);
7176 }
7177
7178 /* Returns the __dso_handle VAR_DECL. */
7179
7180 static tree
get_dso_handle_node(void)7181 get_dso_handle_node (void)
7182 {
7183 if (dso_handle_node)
7184 return dso_handle_node;
7185
7186 /* Declare the variable. */
7187 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
7188 ptr_type_node);
7189
7190 #ifdef HAVE_GAS_HIDDEN
7191 if (dso_handle_node != error_mark_node)
7192 {
7193 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN;
7194 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1;
7195 }
7196 #endif
7197
7198 return dso_handle_node;
7199 }
7200
7201 /* Begin a new function with internal linkage whose job will be simply
7202 to destroy some particular variable. */
7203
7204 static GTY(()) int start_cleanup_cnt;
7205
7206 static tree
start_cleanup_fn(void)7207 start_cleanup_fn (void)
7208 {
7209 char name[32];
7210 tree fntype;
7211 tree fndecl;
7212 bool use_cxa_atexit = flag_use_cxa_atexit
7213 && !targetm.cxx.use_atexit_for_cxa_atexit ();
7214
7215 push_to_top_level ();
7216
7217 /* No need to mangle this. */
7218 push_lang_context (lang_name_c);
7219
7220 /* Build the name of the function. */
7221 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
7222 /* Build the function declaration. */
7223 fntype = TREE_TYPE (get_atexit_fn_ptr_type ());
7224 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
7225 /* It's a function with internal linkage, generated by the
7226 compiler. */
7227 TREE_PUBLIC (fndecl) = 0;
7228 DECL_ARTIFICIAL (fndecl) = 1;
7229 /* Make the function `inline' so that it is only emitted if it is
7230 actually needed. It is unlikely that it will be inlined, since
7231 it is only called via a function pointer, but we avoid unnecessary
7232 emissions this way. */
7233 DECL_DECLARED_INLINE_P (fndecl) = 1;
7234 DECL_INTERFACE_KNOWN (fndecl) = 1;
7235 /* Build the parameter. */
7236 if (use_cxa_atexit)
7237 {
7238 tree parmdecl;
7239
7240 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
7241 DECL_CONTEXT (parmdecl) = fndecl;
7242 TREE_USED (parmdecl) = 1;
7243 DECL_READ_P (parmdecl) = 1;
7244 DECL_ARGUMENTS (fndecl) = parmdecl;
7245 }
7246
7247 pushdecl (fndecl);
7248 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
7249
7250 pop_lang_context ();
7251
7252 return current_function_decl;
7253 }
7254
7255 /* Finish the cleanup function begun by start_cleanup_fn. */
7256
7257 static void
end_cleanup_fn(void)7258 end_cleanup_fn (void)
7259 {
7260 expand_or_defer_fn (finish_function (0));
7261
7262 pop_from_top_level ();
7263 }
7264
7265 /* Generate code to handle the destruction of DECL, an object with
7266 static storage duration. */
7267
7268 tree
register_dtor_fn(tree decl)7269 register_dtor_fn (tree decl)
7270 {
7271 tree cleanup;
7272 tree addr;
7273 tree compound_stmt;
7274 tree fcall;
7275 tree type;
7276 bool ob_parm, dso_parm, use_dtor;
7277 tree arg0, arg1, arg2;
7278 tree atex_node;
7279
7280 type = TREE_TYPE (decl);
7281 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
7282 return void_node;
7283
7284 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or
7285 "__aeabi_atexit"), and DECL is a class object, we can just pass the
7286 destructor to "__cxa_atexit"; we don't have to build a temporary
7287 function to do the cleanup. */
7288 dso_parm = (flag_use_cxa_atexit
7289 && !targetm.cxx.use_atexit_for_cxa_atexit ());
7290 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm);
7291 use_dtor = ob_parm && CLASS_TYPE_P (type);
7292 if (use_dtor)
7293 {
7294 int idx;
7295
7296 /* Find the destructor. */
7297 idx = lookup_fnfields_1 (type, complete_dtor_identifier);
7298 gcc_assert (idx >= 0);
7299 cleanup = (*CLASSTYPE_METHOD_VEC (type))[idx];
7300 /* Make sure it is accessible. */
7301 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup,
7302 tf_warning_or_error);
7303 }
7304 else
7305 {
7306 /* Call build_cleanup before we enter the anonymous function so
7307 that any access checks will be done relative to the current
7308 scope, rather than the scope of the anonymous function. */
7309 build_cleanup (decl);
7310
7311 /* Now start the function. */
7312 cleanup = start_cleanup_fn ();
7313
7314 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
7315 to the original function, rather than the anonymous one. That
7316 will make the back end think that nested functions are in use,
7317 which causes confusion. */
7318 push_deferring_access_checks (dk_no_check);
7319 fcall = build_cleanup (decl);
7320 pop_deferring_access_checks ();
7321
7322 /* Create the body of the anonymous function. */
7323 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
7324 finish_expr_stmt (fcall);
7325 finish_compound_stmt (compound_stmt);
7326 end_cleanup_fn ();
7327 }
7328
7329 /* Call atexit with the cleanup function. */
7330 mark_used (cleanup);
7331 cleanup = build_address (cleanup);
7332
7333 if (CP_DECL_THREAD_LOCAL_P (decl))
7334 atex_node = get_thread_atexit_node ();
7335 else
7336 atex_node = get_atexit_node ();
7337
7338 if (use_dtor)
7339 {
7340 /* We must convert CLEANUP to the type that "__cxa_atexit"
7341 expects. */
7342 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup);
7343 /* "__cxa_atexit" will pass the address of DECL to the
7344 cleanup function. */
7345 mark_used (decl);
7346 addr = build_address (decl);
7347 /* The declared type of the parameter to "__cxa_atexit" is
7348 "void *". For plain "T*", we could just let the
7349 machinery in cp_build_function_call convert it -- but if the
7350 type is "cv-qualified T *", then we need to convert it
7351 before passing it in, to avoid spurious errors. */
7352 addr = build_nop (ptr_type_node, addr);
7353 }
7354 else
7355 /* Since the cleanup functions we build ignore the address
7356 they're given, there's no reason to pass the actual address
7357 in, and, in general, it's cheaper to pass NULL than any
7358 other value. */
7359 addr = null_pointer_node;
7360
7361 if (dso_parm)
7362 arg2 = cp_build_addr_expr (get_dso_handle_node (),
7363 tf_warning_or_error);
7364 else if (ob_parm)
7365 /* Just pass NULL to the dso handle parm if we don't actually
7366 have a DSO handle on this target. */
7367 arg2 = null_pointer_node;
7368 else
7369 arg2 = NULL_TREE;
7370
7371 if (ob_parm)
7372 {
7373 if (!CP_DECL_THREAD_LOCAL_P (decl)
7374 && targetm.cxx.use_aeabi_atexit ())
7375 {
7376 arg1 = cleanup;
7377 arg0 = addr;
7378 }
7379 else
7380 {
7381 arg1 = addr;
7382 arg0 = cleanup;
7383 }
7384 }
7385 else
7386 {
7387 arg0 = cleanup;
7388 arg1 = NULL_TREE;
7389 }
7390 return cp_build_function_call_nary (atex_node, tf_warning_or_error,
7391 arg0, arg1, arg2, NULL_TREE);
7392 }
7393
7394 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
7395 is its initializer. Generate code to handle the construction
7396 and destruction of DECL. */
7397
7398 static void
expand_static_init(tree decl,tree init)7399 expand_static_init (tree decl, tree init)
7400 {
7401 gcc_assert (VAR_P (decl));
7402 gcc_assert (TREE_STATIC (decl));
7403
7404 /* Some variables require no dynamic initialization. */
7405 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
7406 {
7407 /* Make sure the destructor is callable. */
7408 cxx_maybe_build_cleanup (decl, tf_warning_or_error);
7409 if (!init)
7410 return;
7411 }
7412
7413 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl)
7414 && !DECL_FUNCTION_SCOPE_P (decl))
7415 {
7416 if (init)
7417 error ("non-local variable %qD declared %<__thread%> "
7418 "needs dynamic initialization", decl);
7419 else
7420 error ("non-local variable %qD declared %<__thread%> "
7421 "has a non-trivial destructor", decl);
7422 static bool informed;
7423 if (!informed)
7424 {
7425 inform (DECL_SOURCE_LOCATION (decl),
7426 "C++11 %<thread_local%> allows dynamic initialization "
7427 "and destruction");
7428 informed = true;
7429 }
7430 return;
7431 }
7432
7433 if (DECL_FUNCTION_SCOPE_P (decl))
7434 {
7435 /* Emit code to perform this initialization but once. */
7436 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
7437 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
7438 tree guard, guard_addr;
7439 tree flag, begin;
7440 /* We don't need thread-safety code for thread-local vars. */
7441 bool thread_guard = (flag_threadsafe_statics
7442 && !CP_DECL_THREAD_LOCAL_P (decl));
7443
7444 /* Emit code to perform this initialization but once. This code
7445 looks like:
7446
7447 static <type> guard;
7448 if (!__atomic_load (guard.first_byte)) {
7449 if (__cxa_guard_acquire (&guard)) {
7450 bool flag = false;
7451 try {
7452 // Do initialization.
7453 flag = true; __cxa_guard_release (&guard);
7454 // Register variable for destruction at end of program.
7455 } catch {
7456 if (!flag) __cxa_guard_abort (&guard);
7457 }
7458 }
7459
7460 Note that the `flag' variable is only set to 1 *after* the
7461 initialization is complete. This ensures that an exception,
7462 thrown during the construction, will cause the variable to
7463 reinitialized when we pass through this code again, as per:
7464
7465 [stmt.dcl]
7466
7467 If the initialization exits by throwing an exception, the
7468 initialization is not complete, so it will be tried again
7469 the next time control enters the declaration.
7470
7471 This process should be thread-safe, too; multiple threads
7472 should not be able to initialize the variable more than
7473 once. */
7474
7475 /* Create the guard variable. */
7476 guard = get_guard (decl);
7477
7478 /* Begin the conditional initialization. */
7479 if_stmt = begin_if_stmt ();
7480
7481 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt);
7482 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7483
7484 if (thread_guard)
7485 {
7486 tree vfntype = NULL_TREE;
7487 tree acquire_name, release_name, abort_name;
7488 tree acquire_fn, release_fn, abort_fn;
7489 guard_addr = build_address (guard);
7490
7491 acquire_name = get_identifier ("__cxa_guard_acquire");
7492 release_name = get_identifier ("__cxa_guard_release");
7493 abort_name = get_identifier ("__cxa_guard_abort");
7494 acquire_fn = identifier_global_value (acquire_name);
7495 release_fn = identifier_global_value (release_name);
7496 abort_fn = identifier_global_value (abort_name);
7497 if (!acquire_fn)
7498 acquire_fn = push_library_fn
7499 (acquire_name, build_function_type_list (integer_type_node,
7500 TREE_TYPE (guard_addr),
7501 NULL_TREE),
7502 NULL_TREE, ECF_NOTHROW | ECF_LEAF);
7503 if (!release_fn || !abort_fn)
7504 vfntype = build_function_type_list (void_type_node,
7505 TREE_TYPE (guard_addr),
7506 NULL_TREE);
7507 if (!release_fn)
7508 release_fn = push_library_fn (release_name, vfntype, NULL_TREE,
7509 ECF_NOTHROW | ECF_LEAF);
7510 if (!abort_fn)
7511 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE,
7512 ECF_NOTHROW | ECF_LEAF);
7513
7514 inner_if_stmt = begin_if_stmt ();
7515 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr),
7516 inner_if_stmt);
7517
7518 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
7519 begin = get_target_expr (boolean_false_node);
7520 flag = TARGET_EXPR_SLOT (begin);
7521
7522 TARGET_EXPR_CLEANUP (begin)
7523 = build3 (COND_EXPR, void_type_node, flag,
7524 void_node,
7525 build_call_n (abort_fn, 1, guard_addr));
7526 CLEANUP_EH_ONLY (begin) = 1;
7527
7528 /* Do the initialization itself. */
7529 init = add_stmt_to_compound (begin, init);
7530 init = add_stmt_to_compound
7531 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
7532 init = add_stmt_to_compound
7533 (init, build_call_n (release_fn, 1, guard_addr));
7534 }
7535 else
7536 init = add_stmt_to_compound (init, set_guard (guard));
7537
7538 /* Use atexit to register a function for destroying this static
7539 variable. */
7540 init = add_stmt_to_compound (init, register_dtor_fn (decl));
7541
7542 finish_expr_stmt (init);
7543
7544 if (thread_guard)
7545 {
7546 finish_compound_stmt (inner_then_clause);
7547 finish_then_clause (inner_if_stmt);
7548 finish_if_stmt (inner_if_stmt);
7549 }
7550
7551 finish_compound_stmt (then_clause);
7552 finish_then_clause (if_stmt);
7553 finish_if_stmt (if_stmt);
7554 }
7555 else if (CP_DECL_THREAD_LOCAL_P (decl))
7556 tls_aggregates = tree_cons (init, decl, tls_aggregates);
7557 else
7558 static_aggregates = tree_cons (init, decl, static_aggregates);
7559 }
7560
7561
7562 /* Make TYPE a complete type based on INITIAL_VALUE.
7563 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7564 2 if there was no information (in which case assume 0 if DO_DEFAULT),
7565 3 if the initializer list is empty (in pedantic mode). */
7566
7567 int
cp_complete_array_type(tree * ptype,tree initial_value,bool do_default)7568 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
7569 {
7570 int failure;
7571 tree type, elt_type;
7572
7573 /* Don't get confused by a CONSTRUCTOR for some other type. */
7574 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR
7575 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value)
7576 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE)
7577 return 1;
7578
7579 if (initial_value)
7580 {
7581 unsigned HOST_WIDE_INT i;
7582 tree value;
7583
7584 /* An array of character type can be initialized from a
7585 brace-enclosed string constant.
7586
7587 FIXME: this code is duplicated from reshape_init. Probably
7588 we should just call reshape_init here? */
7589 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
7590 && TREE_CODE (initial_value) == CONSTRUCTOR
7591 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
7592 {
7593 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7594 tree value = (*v)[0].value;
7595
7596 if (TREE_CODE (value) == STRING_CST
7597 && v->length () == 1)
7598 initial_value = value;
7599 }
7600
7601 /* If any of the elements are parameter packs, we can't actually
7602 complete this type now because the array size is dependent. */
7603 if (TREE_CODE (initial_value) == CONSTRUCTOR)
7604 {
7605 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (initial_value),
7606 i, value)
7607 {
7608 if (PACK_EXPANSION_P (value))
7609 return 0;
7610 }
7611 }
7612 }
7613
7614 failure = complete_array_type (ptype, initial_value, do_default);
7615
7616 /* We can create the array before the element type is complete, which
7617 means that we didn't have these two bits set in the original type
7618 either. In completing the type, we are expected to propagate these
7619 bits. See also complete_type which does the same thing for arrays
7620 of fixed size. */
7621 type = *ptype;
7622 if (TYPE_DOMAIN (type))
7623 {
7624 elt_type = TREE_TYPE (type);
7625 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
7626 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
7627 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
7628 }
7629
7630 return failure;
7631 }
7632
7633 /* As above, but either give an error or reject zero-size arrays, depending
7634 on COMPLAIN. */
7635
7636 int
cp_complete_array_type_or_error(tree * ptype,tree initial_value,bool do_default,tsubst_flags_t complain)7637 cp_complete_array_type_or_error (tree *ptype, tree initial_value,
7638 bool do_default, tsubst_flags_t complain)
7639 {
7640 int failure;
7641 bool sfinae = !(complain & tf_error);
7642 /* In SFINAE context we can't be lenient about zero-size arrays. */
7643 if (sfinae)
7644 ++pedantic;
7645 failure = cp_complete_array_type (ptype, initial_value, do_default);
7646 if (sfinae)
7647 --pedantic;
7648 if (failure)
7649 {
7650 if (sfinae)
7651 /* Not an error. */;
7652 else if (failure == 1)
7653 error ("initializer fails to determine size of %qT", *ptype);
7654 else if (failure == 2)
7655 {
7656 if (do_default)
7657 error ("array size missing in %qT", *ptype);
7658 }
7659 else if (failure == 3)
7660 error ("zero-size array %qT", *ptype);
7661 *ptype = error_mark_node;
7662 }
7663 return failure;
7664 }
7665
7666 /* Return zero if something is declared to be a member of type
7667 CTYPE when in the context of CUR_TYPE. STRING is the error
7668 message to print in that case. Otherwise, quietly return 1. */
7669
7670 static int
member_function_or_else(tree ctype,tree cur_type,enum overload_flags flags)7671 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
7672 {
7673 if (ctype && ctype != cur_type)
7674 {
7675 if (flags == DTOR_FLAG)
7676 error ("destructor for alien class %qT cannot be a member", ctype);
7677 else
7678 error ("constructor for alien class %qT cannot be a member", ctype);
7679 return 0;
7680 }
7681 return 1;
7682 }
7683
7684 /* Subroutine of `grokdeclarator'. */
7685
7686 /* Generate errors possibly applicable for a given set of specifiers.
7687 This is for ARM $7.1.2. */
7688
7689 static void
bad_specifiers(tree object,enum bad_spec_place type,int virtualp,int quals,int inlinep,int friendp,int raises)7690 bad_specifiers (tree object,
7691 enum bad_spec_place type,
7692 int virtualp,
7693 int quals,
7694 int inlinep,
7695 int friendp,
7696 int raises)
7697 {
7698 switch (type)
7699 {
7700 case BSP_VAR:
7701 if (virtualp)
7702 error ("%qD declared as a %<virtual%> variable", object);
7703 if (inlinep)
7704 error ("%qD declared as an %<inline%> variable", object);
7705 if (quals)
7706 error ("%<const%> and %<volatile%> function specifiers on "
7707 "%qD invalid in variable declaration", object);
7708 break;
7709 case BSP_PARM:
7710 if (virtualp)
7711 error ("%qD declared as a %<virtual%> parameter", object);
7712 if (inlinep)
7713 error ("%qD declared as an %<inline%> parameter", object);
7714 if (quals)
7715 error ("%<const%> and %<volatile%> function specifiers on "
7716 "%qD invalid in parameter declaration", object);
7717 break;
7718 case BSP_TYPE:
7719 if (virtualp)
7720 error ("%qD declared as a %<virtual%> type", object);
7721 if (inlinep)
7722 error ("%qD declared as an %<inline%> type", object);
7723 if (quals)
7724 error ("%<const%> and %<volatile%> function specifiers on "
7725 "%qD invalid in type declaration", object);
7726 break;
7727 case BSP_FIELD:
7728 if (virtualp)
7729 error ("%qD declared as a %<virtual%> field", object);
7730 if (inlinep)
7731 error ("%qD declared as an %<inline%> field", object);
7732 if (quals)
7733 error ("%<const%> and %<volatile%> function specifiers on "
7734 "%qD invalid in field declaration", object);
7735 break;
7736 default:
7737 gcc_unreachable();
7738 }
7739 if (friendp)
7740 error ("%q+D declared as a friend", object);
7741 if (raises
7742 && (TREE_CODE (object) == TYPE_DECL
7743 || (!TYPE_PTRFN_P (TREE_TYPE (object))
7744 && !TYPE_REFFN_P (TREE_TYPE (object))
7745 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
7746 error ("%q+D declared with an exception specification", object);
7747 }
7748
7749 /* DECL is a member function or static data member and is presently
7750 being defined. Check that the definition is taking place in a
7751 valid namespace. */
7752
7753 static void
check_class_member_definition_namespace(tree decl)7754 check_class_member_definition_namespace (tree decl)
7755 {
7756 /* These checks only apply to member functions and static data
7757 members. */
7758 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
7759 /* We check for problems with specializations in pt.c in
7760 check_specialization_namespace, where we can issue better
7761 diagnostics. */
7762 if (processing_specialization)
7763 return;
7764 /* There are no restrictions on the placement of
7765 explicit instantiations. */
7766 if (processing_explicit_instantiation)
7767 return;
7768 /* [class.mfct]
7769
7770 A member function definition that appears outside of the
7771 class definition shall appear in a namespace scope enclosing
7772 the class definition.
7773
7774 [class.static.data]
7775
7776 The definition for a static data member shall appear in a
7777 namespace scope enclosing the member's class definition. */
7778 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
7779 permerror (input_location, "definition of %qD is not in namespace enclosing %qT",
7780 decl, DECL_CONTEXT (decl));
7781 }
7782
7783 /* Build a PARM_DECL for the "this" parameter. TYPE is the
7784 METHOD_TYPE for a non-static member function; QUALS are the
7785 cv-qualifiers that apply to the function. */
7786
7787 tree
build_this_parm(tree type,cp_cv_quals quals)7788 build_this_parm (tree type, cp_cv_quals quals)
7789 {
7790 tree this_type;
7791 tree qual_type;
7792 tree parm;
7793 cp_cv_quals this_quals;
7794
7795 if (CLASS_TYPE_P (type))
7796 {
7797 this_type
7798 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
7799 this_type = build_pointer_type (this_type);
7800 }
7801 else
7802 this_type = type_of_this_parm (type);
7803 /* The `this' parameter is implicitly `const'; it cannot be
7804 assigned to. */
7805 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
7806 qual_type = cp_build_qualified_type (this_type, this_quals);
7807 parm = build_artificial_parm (this_identifier, qual_type);
7808 cp_apply_type_quals_to_decl (this_quals, parm);
7809 return parm;
7810 }
7811
7812 /* DECL is a static member function. Complain if it was declared
7813 with function-cv-quals. */
7814
7815 static void
check_static_quals(tree decl,cp_cv_quals quals)7816 check_static_quals (tree decl, cp_cv_quals quals)
7817 {
7818 if (quals != TYPE_UNQUALIFIED)
7819 error ("static member function %q#D declared with type qualifiers",
7820 decl);
7821 }
7822
7823 // Check that FN takes no arguments and returns bool.
7824 static void
check_concept_fn(tree fn)7825 check_concept_fn (tree fn)
7826 {
7827 // A constraint is nullary.
7828 if (DECL_ARGUMENTS (fn))
7829 error ("concept %q#D declared with function parameters", fn);
7830
7831 // The declared return type of the concept shall be bool, and
7832 // it shall not be deduced from it definition.
7833 tree type = TREE_TYPE (TREE_TYPE (fn));
7834 if (is_auto (type))
7835 error ("concept %q#D declared with a deduced return type", fn);
7836 else if (type != boolean_type_node)
7837 error ("concept %q#D with non-%<bool%> return type %qT", fn, type);
7838 }
7839
7840 /* Helper function. Replace the temporary this parameter injected
7841 during cp_finish_omp_declare_simd with the real this parameter. */
7842
7843 static tree
declare_simd_adjust_this(tree * tp,int * walk_subtrees,void * data)7844 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data)
7845 {
7846 tree this_parm = (tree) data;
7847 if (TREE_CODE (*tp) == PARM_DECL
7848 && DECL_NAME (*tp) == this_identifier
7849 && *tp != this_parm)
7850 *tp = this_parm;
7851 else if (TYPE_P (*tp))
7852 *walk_subtrees = 0;
7853 return NULL_TREE;
7854 }
7855
7856 /* CTYPE is class type, or null if non-class.
7857 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
7858 or METHOD_TYPE.
7859 DECLARATOR is the function's name.
7860 PARMS is a chain of PARM_DECLs for the function.
7861 VIRTUALP is truthvalue of whether the function is virtual or not.
7862 FLAGS are to be passed through to `grokclassfn'.
7863 QUALS are qualifiers indicating whether the function is `const'
7864 or `volatile'.
7865 RAISES is a list of exceptions that this function can raise.
7866 CHECK is 1 if we must find this method in CTYPE, 0 if we should
7867 not look, and -1 if we should not call `grokclassfn' at all.
7868
7869 SFK is the kind of special function (if any) for the new function.
7870
7871 Returns `NULL_TREE' if something goes wrong, after issuing
7872 applicable error messages. */
7873
7874 static tree
grokfndecl(tree ctype,tree type,tree declarator,tree parms,tree orig_declarator,tree decl_reqs,int virtualp,enum overload_flags flags,cp_cv_quals quals,cp_ref_qualifier rqual,tree raises,int check,int friendp,int publicp,int inlinep,bool deletedp,special_function_kind sfk,bool funcdef_flag,int template_count,tree in_namespace,tree * attrlist,location_t location)7875 grokfndecl (tree ctype,
7876 tree type,
7877 tree declarator,
7878 tree parms,
7879 tree orig_declarator,
7880 tree decl_reqs,
7881 int virtualp,
7882 enum overload_flags flags,
7883 cp_cv_quals quals,
7884 cp_ref_qualifier rqual,
7885 tree raises,
7886 int check,
7887 int friendp,
7888 int publicp,
7889 int inlinep,
7890 bool deletedp,
7891 special_function_kind sfk,
7892 bool funcdef_flag,
7893 int template_count,
7894 tree in_namespace,
7895 tree* attrlist,
7896 location_t location)
7897 {
7898 tree decl;
7899 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
7900 tree t;
7901
7902 // Was the concept specifier present?
7903 bool concept_p = inlinep & 4;
7904
7905 // Concept declarations must have a corresponding definition.
7906 if (concept_p && !funcdef_flag)
7907 {
7908 error ("concept %qD has no definition", declarator);
7909 return NULL_TREE;
7910 }
7911
7912 if (rqual)
7913 type = build_ref_qualified_type (type, rqual);
7914 if (raises)
7915 type = build_exception_variant (type, raises);
7916
7917 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
7918
7919 /* Set the constraints on the declaration. */
7920 if (flag_concepts)
7921 {
7922 tree tmpl_reqs = NULL_TREE;
7923 if (processing_template_decl > template_class_depth (ctype))
7924 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
7925
7926 /* Adjust the required expression into a constraint. */
7927 if (decl_reqs)
7928 decl_reqs = normalize_expression (decl_reqs);
7929
7930 tree ci = build_constraints (tmpl_reqs, decl_reqs);
7931 set_constraints (decl, ci);
7932 }
7933
7934 /* If we have an explicit location, use it, otherwise use whatever
7935 build_lang_decl used (probably input_location). */
7936 if (location != UNKNOWN_LOCATION)
7937 DECL_SOURCE_LOCATION (decl) = location;
7938
7939 if (TREE_CODE (type) == METHOD_TYPE)
7940 {
7941 tree parm;
7942 parm = build_this_parm (type, quals);
7943 DECL_CHAIN (parm) = parms;
7944 parms = parm;
7945
7946 /* Allocate space to hold the vptr bit if needed. */
7947 DECL_ALIGN (decl) = MINIMUM_METHOD_BOUNDARY;
7948 }
7949 DECL_ARGUMENTS (decl) = parms;
7950 for (t = parms; t; t = DECL_CHAIN (t))
7951 DECL_CONTEXT (t) = decl;
7952 /* Propagate volatile out from type to decl. */
7953 if (TYPE_VOLATILE (type))
7954 TREE_THIS_VOLATILE (decl) = 1;
7955
7956 /* Setup decl according to sfk. */
7957 switch (sfk)
7958 {
7959 case sfk_constructor:
7960 case sfk_copy_constructor:
7961 case sfk_move_constructor:
7962 DECL_CONSTRUCTOR_P (decl) = 1;
7963 break;
7964 case sfk_destructor:
7965 DECL_DESTRUCTOR_P (decl) = 1;
7966 break;
7967 default:
7968 break;
7969 }
7970
7971 if (friendp
7972 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
7973 {
7974 if (funcdef_flag)
7975 error
7976 ("defining explicit specialization %qD in friend declaration",
7977 orig_declarator);
7978 else
7979 {
7980 tree fns = TREE_OPERAND (orig_declarator, 0);
7981 tree args = TREE_OPERAND (orig_declarator, 1);
7982
7983 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7984 {
7985 /* Something like `template <class T> friend void f<T>()'. */
7986 error ("invalid use of template-id %qD in declaration "
7987 "of primary template",
7988 orig_declarator);
7989 return NULL_TREE;
7990 }
7991
7992
7993 /* A friend declaration of the form friend void f<>(). Record
7994 the information in the TEMPLATE_ID_EXPR. */
7995 SET_DECL_IMPLICIT_INSTANTIATION (decl);
7996
7997 gcc_assert (identifier_p (fns) || TREE_CODE (fns) == OVERLOAD);
7998 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args);
7999
8000 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8001 if (TREE_PURPOSE (t)
8002 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8003 {
8004 error ("default arguments are not allowed in declaration "
8005 "of friend template specialization %qD",
8006 decl);
8007 return NULL_TREE;
8008 }
8009
8010 if (inlinep & 1)
8011 {
8012 error ("%<inline%> is not allowed in declaration of friend "
8013 "template specialization %qD",
8014 decl);
8015 return NULL_TREE;
8016 }
8017 }
8018 }
8019
8020 /* If this decl has namespace scope, set that up. */
8021 if (in_namespace)
8022 set_decl_namespace (decl, in_namespace, friendp);
8023 else if (!ctype)
8024 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ());
8025
8026 /* `main' and builtins have implicit 'C' linkage. */
8027 if ((MAIN_NAME_P (declarator)
8028 || (IDENTIFIER_LENGTH (declarator) > 10
8029 && IDENTIFIER_POINTER (declarator)[0] == '_'
8030 && IDENTIFIER_POINTER (declarator)[1] == '_'
8031 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0)
8032 || (targetcm.cxx_implicit_extern_c
8033 && targetcm.cxx_implicit_extern_c(IDENTIFIER_POINTER (declarator))))
8034 && current_lang_name == lang_name_cplusplus
8035 && ctype == NULL_TREE
8036 && DECL_FILE_SCOPE_P (decl))
8037 SET_DECL_LANGUAGE (decl, lang_c);
8038
8039 /* Should probably propagate const out from type to decl I bet (mrs). */
8040 if (staticp)
8041 {
8042 DECL_STATIC_FUNCTION_P (decl) = 1;
8043 DECL_CONTEXT (decl) = ctype;
8044 }
8045
8046 if (deletedp)
8047 DECL_DELETED_FN (decl) = 1;
8048
8049 if (ctype)
8050 {
8051 DECL_CONTEXT (decl) = ctype;
8052 if (funcdef_flag)
8053 check_class_member_definition_namespace (decl);
8054 }
8055
8056 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8057 {
8058 if (PROCESSING_REAL_TEMPLATE_DECL_P())
8059 error ("cannot declare %<::main%> to be a template");
8060 if (inlinep & 1)
8061 error ("cannot declare %<::main%> to be inline");
8062 if (inlinep & 2)
8063 error ("cannot declare %<::main%> to be constexpr");
8064 if (!publicp)
8065 error ("cannot declare %<::main%> to be static");
8066 inlinep = 0;
8067 publicp = 1;
8068 }
8069
8070 /* Members of anonymous types and local classes have no linkage; make
8071 them internal. If a typedef is made later, this will be changed. */
8072 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
8073 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8074 publicp = 0;
8075
8076 if (publicp && cxx_dialect == cxx98)
8077 {
8078 /* [basic.link]: A name with no linkage (notably, the name of a class
8079 or enumeration declared in a local scope) shall not be used to
8080 declare an entity with linkage.
8081
8082 DR 757 relaxes this restriction for C++0x. */
8083 no_linkage_error (decl);
8084 }
8085
8086 TREE_PUBLIC (decl) = publicp;
8087 if (! publicp)
8088 {
8089 DECL_INTERFACE_KNOWN (decl) = 1;
8090 DECL_NOT_REALLY_EXTERN (decl) = 1;
8091 }
8092
8093 /* If the declaration was declared inline, mark it as such. */
8094 if (inlinep)
8095 {
8096 DECL_DECLARED_INLINE_P (decl) = 1;
8097 if (publicp)
8098 DECL_COMDAT (decl) = 1;
8099 }
8100 if (inlinep & 2)
8101 DECL_DECLARED_CONSTEXPR_P (decl) = true;
8102
8103 // If the concept declaration specifier was found, check
8104 // that the declaration satisfies the necessary requirements.
8105 if (concept_p)
8106 {
8107 DECL_DECLARED_CONCEPT_P (decl) = true;
8108 check_concept_fn (decl);
8109 }
8110
8111 DECL_EXTERNAL (decl) = 1;
8112 if (TREE_CODE (type) == FUNCTION_TYPE)
8113 {
8114 if (quals || rqual)
8115 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl),
8116 TYPE_UNQUALIFIED,
8117 REF_QUAL_NONE);
8118
8119 if (quals)
8120 {
8121 error (ctype
8122 ? G_("static member function %qD cannot have cv-qualifier")
8123 : G_("non-member function %qD cannot have cv-qualifier"),
8124 decl);
8125 quals = TYPE_UNQUALIFIED;
8126 }
8127
8128 if (rqual)
8129 {
8130 error (ctype
8131 ? G_("static member function %qD cannot have ref-qualifier")
8132 : G_("non-member function %qD cannot have ref-qualifier"),
8133 decl);
8134 rqual = REF_QUAL_NONE;
8135 }
8136 }
8137
8138 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
8139 && !grok_op_properties (decl, /*complain=*/true))
8140 return NULL_TREE;
8141 else if (UDLIT_OPER_P (DECL_NAME (decl)))
8142 {
8143 bool long_long_unsigned_p;
8144 bool long_double_p;
8145 const char *suffix = NULL;
8146 /* [over.literal]/6: Literal operators shall not have C linkage. */
8147 if (DECL_LANGUAGE (decl) == lang_c)
8148 {
8149 error ("literal operator with C linkage");
8150 return NULL_TREE;
8151 }
8152
8153 if (DECL_NAMESPACE_SCOPE_P (decl))
8154 {
8155 if (!check_literal_operator_args (decl, &long_long_unsigned_p,
8156 &long_double_p))
8157 {
8158 error ("%qD has invalid argument list", decl);
8159 return NULL_TREE;
8160 }
8161
8162 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl));
8163 if (long_long_unsigned_p)
8164 {
8165 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix)))
8166 warning (0, "integer suffix %<%s%>"
8167 " shadowed by implementation", suffix);
8168 }
8169 else if (long_double_p)
8170 {
8171 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix)))
8172 warning (0, "floating point suffix %<%s%>"
8173 " shadowed by implementation", suffix);
8174 }
8175 }
8176 else
8177 {
8178 error ("%qD must be a non-member function", decl);
8179 return NULL_TREE;
8180 }
8181 }
8182
8183 if (funcdef_flag)
8184 /* Make the init_value nonzero so pushdecl knows this is not
8185 tentative. error_mark_node is replaced later with the BLOCK. */
8186 DECL_INITIAL (decl) = error_mark_node;
8187
8188 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8189 TREE_NOTHROW (decl) = 1;
8190
8191 if (flag_openmp || flag_openmp_simd || flag_cilkplus)
8192 {
8193 /* Adjust "omp declare simd" attributes. */
8194 tree ods = lookup_attribute ("omp declare simd", *attrlist);
8195 if (ods)
8196 {
8197 tree attr;
8198 for (attr = ods; attr;
8199 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr)))
8200 {
8201 if (TREE_CODE (type) == METHOD_TYPE)
8202 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this,
8203 DECL_ARGUMENTS (decl), NULL);
8204 if (TREE_VALUE (attr) != NULL_TREE)
8205 {
8206 tree cl = TREE_VALUE (TREE_VALUE (attr));
8207 cl = c_omp_declare_simd_clauses_to_numbers
8208 (DECL_ARGUMENTS (decl), cl);
8209 if (cl)
8210 TREE_VALUE (TREE_VALUE (attr)) = cl;
8211 else
8212 TREE_VALUE (attr) = NULL_TREE;
8213 }
8214 }
8215 }
8216 }
8217
8218 /* Caller will do the rest of this. */
8219 if (check < 0)
8220 return decl;
8221
8222 if (ctype != NULL_TREE)
8223 grokclassfn (ctype, decl, flags);
8224
8225 /* 12.4/3 */
8226 if (cxx_dialect >= cxx11
8227 && DECL_DESTRUCTOR_P (decl)
8228 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl))
8229 && !processing_template_decl)
8230 deduce_noexcept_on_destructor (decl);
8231
8232 decl = check_explicit_specialization (orig_declarator, decl,
8233 template_count,
8234 2 * funcdef_flag +
8235 4 * (friendp != 0) +
8236 8 * concept_p);
8237 if (decl == error_mark_node)
8238 return NULL_TREE;
8239
8240 if (DECL_STATIC_FUNCTION_P (decl))
8241 check_static_quals (decl, quals);
8242
8243 if (attrlist)
8244 {
8245 cplus_decl_attributes (&decl, *attrlist, 0);
8246 *attrlist = NULL_TREE;
8247 }
8248
8249 /* Check main's type after attributes have been applied. */
8250 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8251 {
8252 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8253 integer_type_node))
8254 {
8255 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
8256 tree newtype;
8257 error ("%<::main%> must return %<int%>");
8258 newtype = build_function_type (integer_type_node, oldtypeargs);
8259 TREE_TYPE (decl) = newtype;
8260 }
8261 if (warn_main)
8262 check_main_parameter_types (decl);
8263 }
8264
8265 if (ctype != NULL_TREE
8266 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
8267 && check)
8268 {
8269 tree old_decl = check_classfn (ctype, decl,
8270 (processing_template_decl
8271 > template_class_depth (ctype))
8272 ? current_template_parms
8273 : NULL_TREE);
8274
8275 if (old_decl == error_mark_node)
8276 return NULL_TREE;
8277
8278 if (old_decl)
8279 {
8280 tree ok;
8281 tree pushed_scope;
8282
8283 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
8284 /* Because grokfndecl is always supposed to return a
8285 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
8286 here. We depend on our callers to figure out that its
8287 really a template that's being returned. */
8288 old_decl = DECL_TEMPLATE_RESULT (old_decl);
8289
8290 if (DECL_STATIC_FUNCTION_P (old_decl)
8291 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
8292 {
8293 /* Remove the `this' parm added by grokclassfn. */
8294 revert_static_member_fn (decl);
8295 check_static_quals (decl, quals);
8296 }
8297 if (DECL_ARTIFICIAL (old_decl))
8298 {
8299 error ("definition of implicitly-declared %qD", old_decl);
8300 return NULL_TREE;
8301 }
8302 else if (DECL_DEFAULTED_FN (old_decl))
8303 {
8304 error ("definition of explicitly-defaulted %q+D", decl);
8305 error ("%q+#D explicitly defaulted here", old_decl);
8306 return NULL_TREE;
8307 }
8308
8309 /* Since we've smashed OLD_DECL to its
8310 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
8311 if (TREE_CODE (decl) == TEMPLATE_DECL)
8312 decl = DECL_TEMPLATE_RESULT (decl);
8313
8314 /* Attempt to merge the declarations. This can fail, in
8315 the case of some invalid specialization declarations. */
8316 pushed_scope = push_scope (ctype);
8317 ok = duplicate_decls (decl, old_decl, friendp);
8318 if (pushed_scope)
8319 pop_scope (pushed_scope);
8320 if (!ok)
8321 {
8322 error ("no %q#D member function declared in class %qT",
8323 decl, ctype);
8324 return NULL_TREE;
8325 }
8326 if (ok == error_mark_node)
8327 return NULL_TREE;
8328 return old_decl;
8329 }
8330 }
8331
8332 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
8333 return NULL_TREE;
8334
8335 if (ctype == NULL_TREE || check)
8336 return decl;
8337
8338 if (virtualp)
8339 DECL_VIRTUAL_P (decl) = 1;
8340
8341 return decl;
8342 }
8343
8344 /* decl is a FUNCTION_DECL.
8345 specifiers are the parsed virt-specifiers.
8346
8347 Set flags to reflect the virt-specifiers.
8348
8349 Returns decl. */
8350
8351 static tree
set_virt_specifiers(tree decl,cp_virt_specifiers specifiers)8352 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers)
8353 {
8354 if (decl == NULL_TREE)
8355 return decl;
8356 if (specifiers & VIRT_SPEC_OVERRIDE)
8357 DECL_OVERRIDE_P (decl) = 1;
8358 if (specifiers & VIRT_SPEC_FINAL)
8359 DECL_FINAL_P (decl) = 1;
8360 return decl;
8361 }
8362
8363 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
8364 the linkage that DECL will receive in the object file. */
8365
8366 static void
set_linkage_for_static_data_member(tree decl)8367 set_linkage_for_static_data_member (tree decl)
8368 {
8369 /* A static data member always has static storage duration and
8370 external linkage. Note that static data members are forbidden in
8371 local classes -- the only situation in which a class has
8372 non-external linkage. */
8373 TREE_PUBLIC (decl) = 1;
8374 TREE_STATIC (decl) = 1;
8375 /* For non-template classes, static data members are always put
8376 out in exactly those files where they are defined, just as
8377 with ordinary namespace-scope variables. */
8378 if (!processing_template_decl)
8379 DECL_INTERFACE_KNOWN (decl) = 1;
8380 }
8381
8382 /* Create a VAR_DECL named NAME with the indicated TYPE.
8383
8384 If SCOPE is non-NULL, it is the class type or namespace containing
8385 the variable. If SCOPE is NULL, the variable should is created in
8386 the innermost enclosing scope. */
8387
8388 static tree
grokvardecl(tree type,tree name,tree orig_declarator,const cp_decl_specifier_seq * declspecs,int initialized,int flags,int template_count,tree scope)8389 grokvardecl (tree type,
8390 tree name,
8391 tree orig_declarator,
8392 const cp_decl_specifier_seq *declspecs,
8393 int initialized,
8394 int flags,
8395 int template_count,
8396 tree scope)
8397 {
8398 tree decl;
8399 tree explicit_scope;
8400
8401 gcc_assert (!name || identifier_p (name));
8402
8403 bool constp = flags&1;
8404 bool conceptp = flags&2;
8405
8406 /* Compute the scope in which to place the variable, but remember
8407 whether or not that scope was explicitly specified by the user. */
8408 explicit_scope = scope;
8409 if (!scope)
8410 {
8411 /* An explicit "extern" specifier indicates a namespace-scope
8412 variable. */
8413 if (declspecs->storage_class == sc_extern)
8414 scope = current_decl_namespace ();
8415 else if (!at_function_scope_p ())
8416 scope = current_scope ();
8417 }
8418
8419 if (scope
8420 && (/* If the variable is a namespace-scope variable declared in a
8421 template, we need DECL_LANG_SPECIFIC. */
8422 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
8423 /* Similarly for namespace-scope variables with language linkage
8424 other than C++. */
8425 || (TREE_CODE (scope) == NAMESPACE_DECL
8426 && current_lang_name != lang_name_cplusplus)
8427 /* Similarly for static data members. */
8428 || TYPE_P (scope)
8429 /* Similarly for explicit specializations. */
8430 || (orig_declarator
8431 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)))
8432 decl = build_lang_decl (VAR_DECL, name, type);
8433 else
8434 decl = build_decl (input_location, VAR_DECL, name, type);
8435
8436 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
8437 set_decl_namespace (decl, explicit_scope, 0);
8438 else
8439 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
8440
8441 if (declspecs->storage_class == sc_extern)
8442 {
8443 DECL_THIS_EXTERN (decl) = 1;
8444 DECL_EXTERNAL (decl) = !initialized;
8445 }
8446
8447 if (DECL_CLASS_SCOPE_P (decl))
8448 {
8449 set_linkage_for_static_data_member (decl);
8450 /* This function is only called with out-of-class definitions. */
8451 DECL_EXTERNAL (decl) = 0;
8452 check_class_member_definition_namespace (decl);
8453 }
8454 /* At top level, either `static' or no s.c. makes a definition
8455 (perhaps tentative), and absence of `static' makes it public. */
8456 else if (toplevel_bindings_p ())
8457 {
8458 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
8459 && (DECL_THIS_EXTERN (decl) || ! constp));
8460 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
8461 }
8462 /* Not at top level, only `static' makes a static definition. */
8463 else
8464 {
8465 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
8466 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
8467 }
8468
8469 if (decl_spec_seq_has_spec_p (declspecs, ds_thread))
8470 {
8471 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8472 {
8473 CP_DECL_THREAD_LOCAL_P (decl) = true;
8474 if (!processing_template_decl)
8475 set_decl_tls_model (decl, decl_default_tls_model (decl));
8476 }
8477 if (declspecs->gnu_thread_keyword_p)
8478 SET_DECL_GNU_TLS_P (decl);
8479 }
8480
8481 /* If the type of the decl has no linkage, make sure that we'll
8482 notice that in mark_used. */
8483 if (cxx_dialect > cxx98
8484 && decl_linkage (decl) != lk_none
8485 && DECL_LANG_SPECIFIC (decl) == NULL
8486 && !DECL_EXTERN_C_P (decl)
8487 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false))
8488 retrofit_lang_decl (decl);
8489
8490 if (TREE_PUBLIC (decl))
8491 {
8492 /* [basic.link]: A name with no linkage (notably, the name of a class
8493 or enumeration declared in a local scope) shall not be used to
8494 declare an entity with linkage.
8495
8496 DR 757 relaxes this restriction for C++0x. */
8497 if (cxx_dialect < cxx11)
8498 no_linkage_error (decl);
8499 }
8500 else
8501 DECL_INTERFACE_KNOWN (decl) = 1;
8502
8503 if (DECL_NAME (decl)
8504 && MAIN_NAME_P (DECL_NAME (decl))
8505 && scope == global_namespace)
8506 error ("cannot declare %<::main%> to be a global variable");
8507
8508 /* Check that the variable can be safely declared as a concept.
8509 Note that this also forbids explicit specializations. */
8510 if (conceptp)
8511 {
8512 if (!processing_template_decl)
8513 {
8514 error ("a non-template variable cannot be %<concept%>");
8515 return NULL_TREE;
8516 }
8517 else
8518 DECL_DECLARED_CONCEPT_P (decl) = true;
8519 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node))
8520 error_at (declspecs->locations[ds_type_spec],
8521 "concept must have type %<bool%>");
8522 }
8523 else if (flag_concepts
8524 && processing_template_decl > template_class_depth (scope))
8525 {
8526 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
8527 tree ci = build_constraints (reqs, NULL_TREE);
8528 set_constraints (decl, ci);
8529 }
8530
8531 // Handle explicit specializations and instantiations of variable templates.
8532 if (orig_declarator)
8533 decl = check_explicit_specialization (orig_declarator, decl,
8534 template_count, conceptp * 8);
8535
8536 return decl != error_mark_node ? decl : NULL_TREE;
8537 }
8538
8539 /* Create and return a canonical pointer to member function type, for
8540 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
8541
8542 tree
build_ptrmemfunc_type(tree type)8543 build_ptrmemfunc_type (tree type)
8544 {
8545 tree field, fields;
8546 tree t;
8547
8548 if (type == error_mark_node)
8549 return type;
8550
8551 /* Make sure that we always have the unqualified pointer-to-member
8552 type first. */
8553 if (cp_cv_quals quals = cp_type_quals (type))
8554 {
8555 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
8556 return cp_build_qualified_type (unqual, quals);
8557 }
8558
8559 /* If a canonical type already exists for this type, use it. We use
8560 this method instead of type_hash_canon, because it only does a
8561 simple equality check on the list of field members. */
8562
8563 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
8564 return t;
8565
8566 t = make_node (RECORD_TYPE);
8567
8568 /* Let the front end know this is a pointer to member function. */
8569 TYPE_PTRMEMFUNC_FLAG (t) = 1;
8570
8571 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type);
8572 fields = field;
8573
8574 field = build_decl (input_location, FIELD_DECL, delta_identifier,
8575 delta_type_node);
8576 DECL_CHAIN (field) = fields;
8577 fields = field;
8578
8579 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
8580
8581 /* Zap out the name so that the back end will give us the debugging
8582 information for this anonymous RECORD_TYPE. */
8583 TYPE_NAME (t) = NULL_TREE;
8584
8585 /* Cache this pointer-to-member type so that we can find it again
8586 later. */
8587 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
8588
8589 if (TYPE_STRUCTURAL_EQUALITY_P (type))
8590 SET_TYPE_STRUCTURAL_EQUALITY (t);
8591 else if (TYPE_CANONICAL (type) != type)
8592 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type));
8593
8594 return t;
8595 }
8596
8597 /* Create and return a pointer to data member type. */
8598
8599 tree
build_ptrmem_type(tree class_type,tree member_type)8600 build_ptrmem_type (tree class_type, tree member_type)
8601 {
8602 if (TREE_CODE (member_type) == METHOD_TYPE)
8603 {
8604 cp_cv_quals quals = type_memfn_quals (member_type);
8605 cp_ref_qualifier rqual = type_memfn_rqual (member_type);
8606 member_type = build_memfn_type (member_type, class_type, quals, rqual);
8607 return build_ptrmemfunc_type (build_pointer_type (member_type));
8608 }
8609 else
8610 {
8611 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
8612 return build_offset_type (class_type, member_type);
8613 }
8614 }
8615
8616 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
8617 Check to see that the definition is valid. Issue appropriate error
8618 messages. Return 1 if the definition is particularly bad, or 0
8619 otherwise. */
8620
8621 static int
check_static_variable_definition(tree decl,tree type)8622 check_static_variable_definition (tree decl, tree type)
8623 {
8624 /* Can't check yet if we don't know the type. */
8625 if (dependent_type_p (type))
8626 return 0;
8627 /* If DECL is declared constexpr, we'll do the appropriate checks
8628 in check_initializer. */
8629 if (DECL_P (decl) && DECL_DECLARED_CONSTEXPR_P (decl))
8630 return 0;
8631 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8632 {
8633 if (!COMPLETE_TYPE_P (type))
8634 error ("in-class initialization of static data member %q#D of "
8635 "incomplete type", decl);
8636 else if (literal_type_p (type))
8637 permerror (input_location,
8638 "%<constexpr%> needed for in-class initialization of "
8639 "static data member %q#D of non-integral type", decl);
8640 else
8641 error ("in-class initialization of static data member %q#D of "
8642 "non-literal type", decl);
8643 return 1;
8644 }
8645
8646 /* Motion 10 at San Diego: If a static const integral data member is
8647 initialized with an integral constant expression, the initializer
8648 may appear either in the declaration (within the class), or in
8649 the definition, but not both. If it appears in the class, the
8650 member is a member constant. The file-scope definition is always
8651 required. */
8652 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
8653 {
8654 error ("invalid in-class initialization of static data member "
8655 "of non-integral type %qT",
8656 type);
8657 return 1;
8658 }
8659 else if (!CP_TYPE_CONST_P (type))
8660 error ("ISO C++ forbids in-class initialization of non-const "
8661 "static member %qD",
8662 decl);
8663 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8664 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids initialization of member constant "
8665 "%qD of non-integral type %qT", decl, type);
8666
8667 return 0;
8668 }
8669
8670 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any
8671 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those
8672 expressions out into temporary variables so that walk_tree doesn't
8673 step into them (c++/15764). */
8674
8675 static tree
stabilize_save_expr_r(tree * expr_p,int * walk_subtrees,void * data)8676 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data)
8677 {
8678 hash_set<tree> *pset = (hash_set<tree> *)data;
8679 tree expr = *expr_p;
8680 if (TREE_CODE (expr) == SAVE_EXPR)
8681 {
8682 tree op = TREE_OPERAND (expr, 0);
8683 cp_walk_tree (&op, stabilize_save_expr_r, data, pset);
8684 if (TREE_SIDE_EFFECTS (op))
8685 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op);
8686 *walk_subtrees = 0;
8687 }
8688 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr))
8689 *walk_subtrees = 0;
8690 return NULL;
8691 }
8692
8693 /* Entry point for the above. */
8694
8695 static void
stabilize_vla_size(tree size)8696 stabilize_vla_size (tree size)
8697 {
8698 hash_set<tree> pset;
8699 /* Break out any function calls into temporary variables. */
8700 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset);
8701 }
8702
8703 /* Reduce a SIZEOF_EXPR to its value. */
8704
8705 tree
fold_sizeof_expr(tree t)8706 fold_sizeof_expr (tree t)
8707 {
8708 tree r;
8709 if (SIZEOF_EXPR_TYPE_P (t))
8710 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
8711 SIZEOF_EXPR, false);
8712 else if (TYPE_P (TREE_OPERAND (t, 0)))
8713 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
8714 false);
8715 else
8716 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
8717 false);
8718 if (r == error_mark_node)
8719 r = size_one_node;
8720 return r;
8721 }
8722
8723 /* Given the SIZE (i.e., number of elements) in an array, compute
8724 an appropriate index type for the array. If non-NULL, NAME is
8725 the name of the entity being declared. */
8726
8727 tree
compute_array_index_type(tree name,tree size,tsubst_flags_t complain)8728 compute_array_index_type (tree name, tree size, tsubst_flags_t complain)
8729 {
8730 tree itype;
8731 tree osize = size;
8732
8733 if (error_operand_p (size))
8734 return error_mark_node;
8735
8736 if (!type_dependent_expression_p (size))
8737 {
8738 tree type = TREE_TYPE (size);
8739
8740 mark_rvalue_use (size);
8741
8742 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR
8743 && TREE_SIDE_EFFECTS (size))
8744 /* In C++98, we mark a non-constant array bound with a magic
8745 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */;
8746 else
8747 {
8748 size = instantiate_non_dependent_expr_sfinae (size, complain);
8749
8750 if (CLASS_TYPE_P (type)
8751 && CLASSTYPE_LITERAL_P (type))
8752 {
8753 size = build_expr_type_conversion (WANT_INT, size, true);
8754 if (!size)
8755 {
8756 if (!(complain & tf_error))
8757 return error_mark_node;
8758 if (name)
8759 error ("size of array %qD has non-integral type %qT",
8760 name, type);
8761 else
8762 error ("size of array has non-integral type %qT", type);
8763 size = integer_one_node;
8764 }
8765 if (size == error_mark_node)
8766 return error_mark_node;
8767 type = TREE_TYPE (size);
8768 }
8769
8770 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8771 size = maybe_constant_value (size);
8772
8773 if (!TREE_CONSTANT (size))
8774 size = osize;
8775 }
8776
8777 if (error_operand_p (size))
8778 return error_mark_node;
8779
8780 /* The array bound must be an integer type. */
8781 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
8782 {
8783 if (!(complain & tf_error))
8784 return error_mark_node;
8785 if (name)
8786 error ("size of array %qD has non-integral type %qT", name, type);
8787 else
8788 error ("size of array has non-integral type %qT", type);
8789 size = integer_one_node;
8790 type = TREE_TYPE (size);
8791 }
8792 }
8793
8794 /* A type is dependent if it is...an array type constructed from any
8795 dependent type or whose size is specified by a constant expression
8796 that is value-dependent. */
8797 /* We can only call value_dependent_expression_p on integral constant
8798 expressions; treat non-constant expressions as dependent, too. */
8799 if (processing_template_decl
8800 && (type_dependent_expression_p (size)
8801 || !TREE_CONSTANT (size) || value_dependent_expression_p (size)))
8802 {
8803 /* We cannot do any checking for a SIZE that isn't known to be
8804 constant. Just build the index type and mark that it requires
8805 structural equality checks. */
8806 itype = build_index_type (build_min (MINUS_EXPR, sizetype,
8807 size, size_one_node));
8808 TYPE_DEPENDENT_P (itype) = 1;
8809 TYPE_DEPENDENT_P_VALID (itype) = 1;
8810 SET_TYPE_STRUCTURAL_EQUALITY (itype);
8811 return itype;
8812 }
8813
8814 if (TREE_CODE (size) != INTEGER_CST)
8815 {
8816 tree folded = cp_fully_fold (size);
8817 if (TREE_CODE (folded) == INTEGER_CST)
8818 pedwarn (location_of (size), OPT_Wpedantic,
8819 "size of array is not an integral constant-expression");
8820 /* Use the folded result for VLAs, too; it will have resolved
8821 SIZEOF_EXPR. */
8822 size = folded;
8823 }
8824
8825 /* Normally, the array-bound will be a constant. */
8826 if (TREE_CODE (size) == INTEGER_CST)
8827 {
8828 /* Check to see if the array bound overflowed. Make that an
8829 error, no matter how generous we're being. */
8830 constant_expression_error (size);
8831
8832 /* An array must have a positive number of elements. */
8833 if (tree_int_cst_lt (size, integer_zero_node))
8834 {
8835 if (!(complain & tf_error))
8836 return error_mark_node;
8837 if (name)
8838 error ("size of array %qD is negative", name);
8839 else
8840 error ("size of array is negative");
8841 size = integer_one_node;
8842 }
8843 /* As an extension we allow zero-sized arrays. */
8844 else if (integer_zerop (size))
8845 {
8846 if (!(complain & tf_error))
8847 /* We must fail if performing argument deduction (as
8848 indicated by the state of complain), so that
8849 another substitution can be found. */
8850 return error_mark_node;
8851 else if (in_system_header_at (input_location))
8852 /* Allow them in system headers because glibc uses them. */;
8853 else if (name)
8854 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array %qD", name);
8855 else
8856 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids zero-size array");
8857 }
8858 }
8859 else if (TREE_CONSTANT (size)
8860 /* We don't allow VLAs at non-function scopes, or during
8861 tentative template substitution. */
8862 || !at_function_scope_p ()
8863 || !(complain & tf_error))
8864 {
8865 if (!(complain & tf_error))
8866 return error_mark_node;
8867 /* `(int) &fn' is not a valid array bound. */
8868 if (name)
8869 error ("size of array %qD is not an integral constant-expression",
8870 name);
8871 else
8872 error ("size of array is not an integral constant-expression");
8873 size = integer_one_node;
8874 }
8875 else if (pedantic && warn_vla != 0)
8876 {
8877 if (name)
8878 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array %qD", name);
8879 else
8880 pedwarn (input_location, OPT_Wvla, "ISO C++ forbids variable length array");
8881 }
8882 else if (warn_vla > 0)
8883 {
8884 if (name)
8885 warning (OPT_Wvla,
8886 "variable length array %qD is used", name);
8887 else
8888 warning (OPT_Wvla,
8889 "variable length array is used");
8890 }
8891
8892 if (processing_template_decl && !TREE_CONSTANT (size))
8893 /* A variable sized array. */
8894 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
8895 else
8896 {
8897 HOST_WIDE_INT saved_processing_template_decl;
8898
8899 /* Compute the index of the largest element in the array. It is
8900 one less than the number of elements in the array. We save
8901 and restore PROCESSING_TEMPLATE_DECL so that computations in
8902 cp_build_binary_op will be appropriately folded. */
8903 saved_processing_template_decl = processing_template_decl;
8904 processing_template_decl = 0;
8905 itype = cp_build_binary_op (input_location,
8906 MINUS_EXPR,
8907 cp_convert (ssizetype, size, complain),
8908 cp_convert (ssizetype, integer_one_node,
8909 complain),
8910 complain);
8911 itype = maybe_constant_value (itype);
8912 processing_template_decl = saved_processing_template_decl;
8913
8914 if (!TREE_CONSTANT (itype))
8915 {
8916 /* A variable sized array. */
8917 itype = variable_size (itype);
8918
8919 stabilize_vla_size (itype);
8920
8921 if (flag_sanitize & SANITIZE_VLA
8922 && do_ubsan_in_current_function ())
8923 {
8924 /* We have to add 1 -- in the ubsan routine we generate
8925 LE_EXPR rather than LT_EXPR. */
8926 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype,
8927 build_one_cst (TREE_TYPE (itype)));
8928 t = ubsan_instrument_vla (input_location, t);
8929 finish_expr_stmt (t);
8930 }
8931 }
8932 /* Make sure that there was no overflow when creating to a signed
8933 index type. (For example, on a 32-bit machine, an array with
8934 size 2^32 - 1 is too big.) */
8935 else if (TREE_CODE (itype) == INTEGER_CST
8936 && TREE_OVERFLOW (itype))
8937 {
8938 if (!(complain & tf_error))
8939 return error_mark_node;
8940 error ("overflow in array dimension");
8941 TREE_OVERFLOW (itype) = 0;
8942 }
8943 }
8944
8945 /* Create and return the appropriate index type. */
8946 itype = build_index_type (itype);
8947
8948 /* If the index type were dependent, we would have returned early, so
8949 remember that it isn't. */
8950 TYPE_DEPENDENT_P (itype) = 0;
8951 TYPE_DEPENDENT_P_VALID (itype) = 1;
8952 return itype;
8953 }
8954
8955 /* Returns the scope (if any) in which the entity declared by
8956 DECLARATOR will be located. If the entity was declared with an
8957 unqualified name, NULL_TREE is returned. */
8958
8959 tree
get_scope_of_declarator(const cp_declarator * declarator)8960 get_scope_of_declarator (const cp_declarator *declarator)
8961 {
8962 while (declarator && declarator->kind != cdk_id)
8963 declarator = declarator->declarator;
8964
8965 /* If the declarator-id is a SCOPE_REF, the scope in which the
8966 declaration occurs is the first operand. */
8967 if (declarator
8968 && declarator->u.id.qualifying_scope)
8969 return declarator->u.id.qualifying_scope;
8970
8971 /* Otherwise, the declarator is not a qualified name; the entity will
8972 be declared in the current scope. */
8973 return NULL_TREE;
8974 }
8975
8976 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
8977 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
8978 with this type. */
8979
8980 static tree
create_array_type_for_decl(tree name,tree type,tree size)8981 create_array_type_for_decl (tree name, tree type, tree size)
8982 {
8983 tree itype = NULL_TREE;
8984
8985 /* If things have already gone awry, bail now. */
8986 if (type == error_mark_node || size == error_mark_node)
8987 return error_mark_node;
8988
8989 /* 8.3.4/1: If the type of the identifier of D contains the auto
8990 type-specifier, the program is ill-formed. */
8991 if (type_uses_auto (type))
8992 {
8993 error ("%qD declared as array of %qT", name, type);
8994 return error_mark_node;
8995 }
8996
8997 /* If there are some types which cannot be array elements,
8998 issue an error-message and return. */
8999 switch (TREE_CODE (type))
9000 {
9001 case VOID_TYPE:
9002 if (name)
9003 error ("declaration of %qD as array of void", name);
9004 else
9005 error ("creating array of void");
9006 return error_mark_node;
9007
9008 case FUNCTION_TYPE:
9009 if (name)
9010 error ("declaration of %qD as array of functions", name);
9011 else
9012 error ("creating array of functions");
9013 return error_mark_node;
9014
9015 case REFERENCE_TYPE:
9016 if (name)
9017 error ("declaration of %qD as array of references", name);
9018 else
9019 error ("creating array of references");
9020 return error_mark_node;
9021
9022 case METHOD_TYPE:
9023 if (name)
9024 error ("declaration of %qD as array of function members", name);
9025 else
9026 error ("creating array of function members");
9027 return error_mark_node;
9028
9029 default:
9030 break;
9031 }
9032
9033 /* [dcl.array]
9034
9035 The constant expressions that specify the bounds of the arrays
9036 can be omitted only for the first member of the sequence. */
9037 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9038 {
9039 if (name)
9040 error ("declaration of %qD as multidimensional array must "
9041 "have bounds for all dimensions except the first",
9042 name);
9043 else
9044 error ("multidimensional array must have bounds for all "
9045 "dimensions except the first");
9046
9047 return error_mark_node;
9048 }
9049
9050 /* Figure out the index type for the array. */
9051 if (size)
9052 itype = compute_array_index_type (name, size, tf_warning_or_error);
9053
9054 /* [dcl.array]
9055 T is called the array element type; this type shall not be [...] an
9056 abstract class type. */
9057 abstract_virtuals_error (name, type);
9058
9059 return build_cplus_array_type (type, itype);
9060 }
9061
9062 /* Returns the smallest location != UNKNOWN_LOCATION among the
9063 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile],
9064 and LOCATIONS[ds_restrict]. */
9065
9066 static location_t
smallest_type_quals_location(int type_quals,const location_t * locations)9067 smallest_type_quals_location (int type_quals, const location_t* locations)
9068 {
9069 location_t loc = UNKNOWN_LOCATION;
9070
9071 if (type_quals & TYPE_QUAL_CONST)
9072 loc = locations[ds_const];
9073
9074 if ((type_quals & TYPE_QUAL_VOLATILE)
9075 && (loc == UNKNOWN_LOCATION || locations[ds_volatile] < loc))
9076 loc = locations[ds_volatile];
9077
9078 if ((type_quals & TYPE_QUAL_RESTRICT)
9079 && (loc == UNKNOWN_LOCATION || locations[ds_restrict] < loc))
9080 loc = locations[ds_restrict];
9081
9082 return loc;
9083 }
9084
9085 /* Check that it's OK to declare a function with the indicated TYPE
9086 and TYPE_QUALS. SFK indicates the kind of special function (if any)
9087 that this function is. OPTYPE is the type given in a conversion
9088 operator declaration, or the class type for a constructor/destructor.
9089 Returns the actual return type of the function; that may be different
9090 than TYPE if an error occurs, or for certain special functions. */
9091
9092 static tree
check_special_function_return_type(special_function_kind sfk,tree type,tree optype,int type_quals,const location_t * locations)9093 check_special_function_return_type (special_function_kind sfk,
9094 tree type,
9095 tree optype,
9096 int type_quals,
9097 const location_t* locations)
9098 {
9099 switch (sfk)
9100 {
9101 case sfk_constructor:
9102 if (type)
9103 error ("return type specification for constructor invalid");
9104 else if (type_quals != TYPE_UNQUALIFIED)
9105 error_at (smallest_type_quals_location (type_quals, locations),
9106 "qualifiers are not allowed on constructor declaration");
9107
9108 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
9109 type = build_pointer_type (optype);
9110 else
9111 type = void_type_node;
9112 break;
9113
9114 case sfk_destructor:
9115 if (type)
9116 error ("return type specification for destructor invalid");
9117 else if (type_quals != TYPE_UNQUALIFIED)
9118 error_at (smallest_type_quals_location (type_quals, locations),
9119 "qualifiers are not allowed on destructor declaration");
9120
9121 /* We can't use the proper return type here because we run into
9122 problems with ambiguous bases and covariant returns.
9123 Java classes are left unchanged because (void *) isn't a valid
9124 Java type, and we don't want to change the Java ABI. */
9125 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
9126 type = build_pointer_type (void_type_node);
9127 else
9128 type = void_type_node;
9129 break;
9130
9131 case sfk_conversion:
9132 if (type)
9133 error ("return type specified for %<operator %T%>", optype);
9134 else if (type_quals != TYPE_UNQUALIFIED)
9135 error_at (smallest_type_quals_location (type_quals, locations),
9136 "qualifiers are not allowed on declaration of "
9137 "%<operator %T%>", optype);
9138
9139 type = optype;
9140 break;
9141
9142 default:
9143 gcc_unreachable ();
9144 }
9145
9146 return type;
9147 }
9148
9149 /* A variable or data member (whose unqualified name is IDENTIFIER)
9150 has been declared with the indicated TYPE. If the TYPE is not
9151 acceptable, issue an error message and return a type to use for
9152 error-recovery purposes. */
9153
9154 tree
check_var_type(tree identifier,tree type)9155 check_var_type (tree identifier, tree type)
9156 {
9157 if (VOID_TYPE_P (type))
9158 {
9159 if (!identifier)
9160 error ("unnamed variable or field declared void");
9161 else if (identifier_p (identifier))
9162 {
9163 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
9164 error ("variable or field %qE declared void", identifier);
9165 }
9166 else
9167 error ("variable or field declared void");
9168 type = error_mark_node;
9169 }
9170
9171 return type;
9172 }
9173
9174 /* Given declspecs and a declarator (abstract or otherwise), determine
9175 the name and type of the object declared and construct a DECL node
9176 for it.
9177
9178 DECLSPECS points to the representation of declaration-specifier
9179 sequence that precedes declarator.
9180
9181 DECL_CONTEXT says which syntactic context this declaration is in:
9182 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9183 FUNCDEF for a function definition. Like NORMAL but a few different
9184 error messages in each case. Return value may be zero meaning
9185 this definition is too screwy to try to parse.
9186 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
9187 handle member functions (which have FIELD context).
9188 Return value may be zero meaning this definition is too screwy to
9189 try to parse.
9190 PARM for a parameter declaration (either within a function prototype
9191 or before a function body). Make a PARM_DECL, or return void_type_node.
9192 TPARM for a template parameter declaration.
9193 CATCHPARM for a parameter declaration before a catch clause.
9194 TYPENAME if for a typename (in a cast or sizeof).
9195 Don't make a DECL node; just return the ..._TYPE node.
9196 FIELD for a struct or union field; make a FIELD_DECL.
9197 BITFIELD for a field with specified width.
9198
9199 INITIALIZED is as for start_decl.
9200
9201 ATTRLIST is a pointer to the list of attributes, which may be NULL
9202 if there are none; *ATTRLIST may be modified if attributes from inside
9203 the declarator should be applied to the declaration.
9204
9205 When this function is called, scoping variables (such as
9206 CURRENT_CLASS_TYPE) should reflect the scope in which the
9207 declaration occurs, not the scope in which the new declaration will
9208 be placed. For example, on:
9209
9210 void S::f() { ... }
9211
9212 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9213 should not be `S'.
9214
9215 Returns a DECL (if a declarator is present), a TYPE (if there is no
9216 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
9217 error occurs. */
9218
9219 tree
grokdeclarator(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,enum decl_context decl_context,int initialized,tree * attrlist)9220 grokdeclarator (const cp_declarator *declarator,
9221 cp_decl_specifier_seq *declspecs,
9222 enum decl_context decl_context,
9223 int initialized,
9224 tree* attrlist)
9225 {
9226 tree type = NULL_TREE;
9227 int longlong = 0;
9228 int explicit_intN = 0;
9229 int virtualp, explicitp, friendp, inlinep, staticp;
9230 int explicit_int = 0;
9231 int explicit_char = 0;
9232 int defaulted_int = 0;
9233
9234 tree typedef_decl = NULL_TREE;
9235 const char *name = NULL;
9236 tree typedef_type = NULL_TREE;
9237 /* True if this declarator is a function definition. */
9238 bool funcdef_flag = false;
9239 cp_declarator_kind innermost_code = cdk_error;
9240 int bitfield = 0;
9241 #if 0
9242 /* See the code below that used this. */
9243 tree decl_attr = NULL_TREE;
9244 #endif
9245
9246 /* Keep track of what sort of function is being processed
9247 so that we can warn about default return values, or explicit
9248 return values which do not match prescribed defaults. */
9249 special_function_kind sfk = sfk_none;
9250
9251 tree dname = NULL_TREE;
9252 tree ctor_return_type = NULL_TREE;
9253 enum overload_flags flags = NO_SPECIAL;
9254 /* cv-qualifiers that apply to the declarator, for a declaration of
9255 a member function. */
9256 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
9257 /* virt-specifiers that apply to the declarator, for a declaration of
9258 a member function. */
9259 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
9260 /* ref-qualifier that applies to the declarator, for a declaration of
9261 a member function. */
9262 cp_ref_qualifier rqual = REF_QUAL_NONE;
9263 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
9264 int type_quals = TYPE_UNQUALIFIED;
9265 tree raises = NULL_TREE;
9266 int template_count = 0;
9267 tree returned_attrs = NULL_TREE;
9268 tree parms = NULL_TREE;
9269 const cp_declarator *id_declarator;
9270 /* The unqualified name of the declarator; either an
9271 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
9272 tree unqualified_id;
9273 /* The class type, if any, in which this entity is located,
9274 or NULL_TREE if none. Note that this value may be different from
9275 the current class type; for example if an attempt is made to declare
9276 "A::f" inside "B", this value will be "A". */
9277 tree ctype = current_class_type;
9278 /* The NAMESPACE_DECL for the namespace in which this entity is
9279 located. If an unqualified name is used to declare the entity,
9280 this value will be NULL_TREE, even if the entity is located at
9281 namespace scope. */
9282 tree in_namespace = NULL_TREE;
9283 cp_storage_class storage_class;
9284 bool unsigned_p, signed_p, short_p, long_p, thread_p;
9285 bool type_was_error_mark_node = false;
9286 bool parameter_pack_p = declarator? declarator->parameter_pack_p : false;
9287 bool template_type_arg = false;
9288 bool template_parm_flag = false;
9289 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef);
9290 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr);
9291 bool late_return_type_p = false;
9292 bool array_parameter_p = false;
9293 source_location saved_loc = input_location;
9294 const char *errmsg;
9295 tree reqs = NULL_TREE;
9296
9297 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed);
9298 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned);
9299 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short);
9300 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long);
9301 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long);
9302 explicit_intN = declspecs->explicit_intN_p;
9303 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread);
9304
9305 // Was concept_p specified? Note that ds_concept
9306 // implies ds_constexpr!
9307 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept);
9308 if (concept_p)
9309 constexpr_p = true;
9310
9311 if (decl_spec_seq_has_spec_p (declspecs, ds_const))
9312 type_quals |= TYPE_QUAL_CONST;
9313 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile))
9314 type_quals |= TYPE_QUAL_VOLATILE;
9315 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict))
9316 type_quals |= TYPE_QUAL_RESTRICT;
9317
9318 if (decl_context == FUNCDEF)
9319 funcdef_flag = true, decl_context = NORMAL;
9320 else if (decl_context == MEMFUNCDEF)
9321 funcdef_flag = true, decl_context = FIELD;
9322 else if (decl_context == BITFIELD)
9323 bitfield = 1, decl_context = FIELD;
9324 else if (decl_context == TEMPLATE_TYPE_ARG)
9325 template_type_arg = true, decl_context = TYPENAME;
9326 else if (decl_context == TPARM)
9327 template_parm_flag = true, decl_context = PARM;
9328
9329 if (initialized > 1)
9330 funcdef_flag = true;
9331
9332 /* Look inside a declarator for the name being declared
9333 and get it as a string, for an error message. */
9334 for (id_declarator = declarator;
9335 id_declarator;
9336 id_declarator = id_declarator->declarator)
9337 {
9338 if (id_declarator->kind != cdk_id)
9339 innermost_code = id_declarator->kind;
9340
9341 switch (id_declarator->kind)
9342 {
9343 case cdk_function:
9344 if (id_declarator->declarator
9345 && id_declarator->declarator->kind == cdk_id)
9346 {
9347 sfk = id_declarator->declarator->u.id.sfk;
9348 if (sfk == sfk_destructor)
9349 flags = DTOR_FLAG;
9350 }
9351 break;
9352
9353 case cdk_id:
9354 {
9355 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
9356 tree decl = id_declarator->u.id.unqualified_name;
9357 if (!decl)
9358 break;
9359 if (qualifying_scope)
9360 {
9361 if (at_function_scope_p ())
9362 {
9363 /* [dcl.meaning]
9364
9365 A declarator-id shall not be qualified except
9366 for ...
9367
9368 None of the cases are permitted in block
9369 scope. */
9370 if (qualifying_scope == global_namespace)
9371 error ("invalid use of qualified-name %<::%D%>",
9372 decl);
9373 else if (TYPE_P (qualifying_scope))
9374 error ("invalid use of qualified-name %<%T::%D%>",
9375 qualifying_scope, decl);
9376 else
9377 error ("invalid use of qualified-name %<%D::%D%>",
9378 qualifying_scope, decl);
9379 return error_mark_node;
9380 }
9381 else if (TYPE_P (qualifying_scope))
9382 {
9383 ctype = qualifying_scope;
9384 if (!MAYBE_CLASS_TYPE_P (ctype))
9385 {
9386 error ("%q#T is not a class or a namespace", ctype);
9387 ctype = NULL_TREE;
9388 }
9389 else if (innermost_code != cdk_function
9390 && current_class_type
9391 && !uniquely_derived_from_p (ctype,
9392 current_class_type))
9393 {
9394 error ("invalid use of qualified-name %<%T::%D%>",
9395 qualifying_scope, decl);
9396 return error_mark_node;
9397 }
9398 }
9399 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
9400 in_namespace = qualifying_scope;
9401 }
9402 switch (TREE_CODE (decl))
9403 {
9404 case BIT_NOT_EXPR:
9405 {
9406 tree type;
9407
9408 if (innermost_code != cdk_function)
9409 {
9410 error ("declaration of %qD as non-function", decl);
9411 return error_mark_node;
9412 }
9413 else if (!qualifying_scope
9414 && !(current_class_type && at_class_scope_p ()))
9415 {
9416 error ("declaration of %qD as non-member", decl);
9417 return error_mark_node;
9418 }
9419
9420 type = TREE_OPERAND (decl, 0);
9421 if (TYPE_P (type))
9422 type = constructor_name (type);
9423 name = identifier_to_locale (IDENTIFIER_POINTER (type));
9424 dname = decl;
9425 }
9426 break;
9427
9428 case TEMPLATE_ID_EXPR:
9429 {
9430 tree fns = TREE_OPERAND (decl, 0);
9431
9432 dname = fns;
9433 if (!identifier_p (dname))
9434 {
9435 if (variable_template_p (dname))
9436 dname = DECL_NAME (dname);
9437 else
9438 {
9439 gcc_assert (is_overloaded_fn (dname));
9440 dname = DECL_NAME (get_first_fn (dname));
9441 }
9442 }
9443 }
9444 /* Fall through. */
9445
9446 case IDENTIFIER_NODE:
9447 if (identifier_p (decl))
9448 dname = decl;
9449
9450 if (C_IS_RESERVED_WORD (dname))
9451 {
9452 error ("declarator-id missing; using reserved word %qD",
9453 dname);
9454 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9455 }
9456 else if (!IDENTIFIER_TYPENAME_P (dname))
9457 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9458 else
9459 {
9460 gcc_assert (flags == NO_SPECIAL);
9461 flags = TYPENAME_FLAG;
9462 ctor_return_type = TREE_TYPE (dname);
9463 sfk = sfk_conversion;
9464 if (is_typename_at_global_scope (dname))
9465 name = identifier_to_locale (IDENTIFIER_POINTER (dname));
9466 else
9467 name = "<invalid operator>";
9468 }
9469 break;
9470
9471 default:
9472 gcc_unreachable ();
9473 }
9474 break;
9475 }
9476
9477 case cdk_array:
9478 case cdk_pointer:
9479 case cdk_reference:
9480 case cdk_ptrmem:
9481 break;
9482
9483 case cdk_error:
9484 return error_mark_node;
9485
9486 default:
9487 gcc_unreachable ();
9488 }
9489 if (id_declarator->kind == cdk_id)
9490 break;
9491 }
9492
9493 /* [dcl.fct.edf]
9494
9495 The declarator in a function-definition shall have the form
9496 D1 ( parameter-declaration-clause) ... */
9497 if (funcdef_flag && innermost_code != cdk_function)
9498 {
9499 error ("function definition does not declare parameters");
9500 return error_mark_node;
9501 }
9502
9503 if (flags == TYPENAME_FLAG
9504 && innermost_code != cdk_function
9505 && ! (ctype && !declspecs->any_specifiers_p))
9506 {
9507 error ("declaration of %qD as non-function", dname);
9508 return error_mark_node;
9509 }
9510
9511 if (dname
9512 && identifier_p (dname)
9513 && UDLIT_OPER_P (dname)
9514 && innermost_code != cdk_function)
9515 {
9516 error ("declaration of %qD as non-function", dname);
9517 return error_mark_node;
9518 }
9519
9520 if (dname && IDENTIFIER_OPNAME_P (dname))
9521 {
9522 if (typedef_p)
9523 {
9524 error ("declaration of %qD as %<typedef%>", dname);
9525 return error_mark_node;
9526 }
9527 else if (decl_context == PARM || decl_context == CATCHPARM)
9528 {
9529 error ("declaration of %qD as parameter", dname);
9530 return error_mark_node;
9531 }
9532 }
9533
9534 /* Anything declared one level down from the top level
9535 must be one of the parameters of a function
9536 (because the body is at least two levels down). */
9537
9538 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9539 by not allowing C++ class definitions to specify their parameters
9540 with xdecls (must be spec.d in the parmlist).
9541
9542 Since we now wait to push a class scope until we are sure that
9543 we are in a legitimate method context, we must set oldcname
9544 explicitly (since current_class_name is not yet alive).
9545
9546 We also want to avoid calling this a PARM if it is in a namespace. */
9547
9548 if (decl_context == NORMAL && !toplevel_bindings_p ())
9549 {
9550 cp_binding_level *b = current_binding_level;
9551 current_binding_level = b->level_chain;
9552 if (current_binding_level != 0 && toplevel_bindings_p ())
9553 decl_context = PARM;
9554 current_binding_level = b;
9555 }
9556
9557 if (name == NULL)
9558 name = decl_context == PARM ? "parameter" : "type name";
9559
9560 if (concept_p && typedef_p)
9561 {
9562 error ("%<concept%> cannot appear in a typedef declaration");
9563 return error_mark_node;
9564 }
9565
9566 if (constexpr_p && typedef_p)
9567 {
9568 error ("%<constexpr%> cannot appear in a typedef declaration");
9569 return error_mark_node;
9570 }
9571
9572 /* If there were multiple types specified in the decl-specifier-seq,
9573 issue an error message. */
9574 if (declspecs->multiple_types_p)
9575 {
9576 error ("two or more data types in declaration of %qs", name);
9577 return error_mark_node;
9578 }
9579
9580 if (declspecs->conflicting_specifiers_p)
9581 {
9582 error ("conflicting specifiers in declaration of %qs", name);
9583 return error_mark_node;
9584 }
9585
9586 /* Extract the basic type from the decl-specifier-seq. */
9587 type = declspecs->type;
9588 if (type == error_mark_node)
9589 {
9590 type = NULL_TREE;
9591 type_was_error_mark_node = true;
9592 }
9593 /* If the entire declaration is itself tagged as deprecated then
9594 suppress reports of deprecated items. */
9595 if (type && TREE_DEPRECATED (type)
9596 && deprecated_state != DEPRECATED_SUPPRESS)
9597 warn_deprecated_use (type, NULL_TREE);
9598 if (type && TREE_CODE (type) == TYPE_DECL)
9599 {
9600 typedef_decl = type;
9601 type = TREE_TYPE (typedef_decl);
9602 if (TREE_DEPRECATED (type)
9603 && DECL_ARTIFICIAL (typedef_decl)
9604 && deprecated_state != DEPRECATED_SUPPRESS)
9605 warn_deprecated_use (type, NULL_TREE);
9606 }
9607 /* No type at all: default to `int', and set DEFAULTED_INT
9608 because it was not a user-defined typedef. */
9609 if (type == NULL_TREE)
9610 {
9611 if (signed_p || unsigned_p || long_p || short_p)
9612 {
9613 /* These imply 'int'. */
9614 type = integer_type_node;
9615 defaulted_int = 1;
9616 }
9617 /* If we just have "complex", it is equivalent to "complex double". */
9618 else if (!longlong && !explicit_intN
9619 && decl_spec_seq_has_spec_p (declspecs, ds_complex))
9620 {
9621 type = double_type_node;
9622 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic,
9623 "ISO C++ does not support plain %<complex%> meaning "
9624 "%<double complex%>");
9625 }
9626 }
9627 /* Gather flags. */
9628 explicit_int = declspecs->explicit_int_p;
9629 explicit_char = declspecs->explicit_char_p;
9630
9631 #if 0
9632 /* See the code below that used this. */
9633 if (typedef_decl)
9634 decl_attr = DECL_ATTRIBUTES (typedef_decl);
9635 #endif
9636 typedef_type = type;
9637
9638
9639 if (sfk != sfk_conversion)
9640 ctor_return_type = ctype;
9641
9642 if (sfk != sfk_none)
9643 {
9644 type = check_special_function_return_type (sfk, type,
9645 ctor_return_type,
9646 type_quals,
9647 declspecs->locations);
9648 type_quals = TYPE_UNQUALIFIED;
9649 }
9650 else if (type == NULL_TREE)
9651 {
9652 int is_main;
9653
9654 explicit_int = -1;
9655
9656 /* We handle `main' specially here, because 'main () { }' is so
9657 common. With no options, it is allowed. With -Wreturn-type,
9658 it is a warning. It is only an error with -pedantic-errors. */
9659 is_main = (funcdef_flag
9660 && dname && identifier_p (dname)
9661 && MAIN_NAME_P (dname)
9662 && ctype == NULL_TREE
9663 && in_namespace == NULL_TREE
9664 && current_namespace == global_namespace);
9665
9666 if (type_was_error_mark_node)
9667 /* We've already issued an error, don't complain more. */;
9668 else if (in_system_header_at (input_location) || flag_ms_extensions)
9669 /* Allow it, sigh. */;
9670 else if (! is_main)
9671 permerror (input_location, "ISO C++ forbids declaration of %qs with no type", name);
9672 else if (pedantic)
9673 pedwarn (input_location, OPT_Wpedantic,
9674 "ISO C++ forbids declaration of %qs with no type", name);
9675 else
9676 warning (OPT_Wreturn_type,
9677 "ISO C++ forbids declaration of %qs with no type", name);
9678
9679 if (type_was_error_mark_node && template_parm_flag)
9680 /* FIXME we should be able to propagate the error_mark_node as is
9681 for other contexts too. */
9682 type = error_mark_node;
9683 else
9684 type = integer_type_node;
9685 }
9686
9687 ctype = NULL_TREE;
9688
9689 if (explicit_intN)
9690 {
9691 if (! int_n_enabled_p[declspecs->int_n_idx])
9692 {
9693 error ("%<__int%d%> is not supported by this target",
9694 int_n_data[declspecs->int_n_idx].bitsize);
9695 explicit_intN = false;
9696 }
9697 else if (pedantic && ! in_system_header_at (input_location))
9698 pedwarn (input_location, OPT_Wpedantic,
9699 "ISO C++ does not support %<__int%d%> for %qs",
9700 int_n_data[declspecs->int_n_idx].bitsize, name);
9701 }
9702
9703 /* Now process the modifiers that were specified
9704 and check for invalid combinations. */
9705
9706 /* Long double is a special combination. */
9707 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
9708 {
9709 long_p = false;
9710 type = cp_build_qualified_type (long_double_type_node,
9711 cp_type_quals (type));
9712 }
9713
9714 /* Check all other uses of type modifiers. */
9715
9716 if (unsigned_p || signed_p || long_p || short_p)
9717 {
9718 int ok = 0;
9719
9720 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
9721 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9722 else if (signed_p && unsigned_p)
9723 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
9724 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
9725 error ("%<long long%> invalid for %qs", name);
9726 else if (long_p && TREE_CODE (type) == REAL_TYPE)
9727 error ("%<long%> invalid for %qs", name);
9728 else if (short_p && TREE_CODE (type) == REAL_TYPE)
9729 error ("%<short%> invalid for %qs", name);
9730 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
9731 error ("%<long%> or %<short%> invalid for %qs", name);
9732 else if ((long_p || short_p || explicit_char || explicit_int) && explicit_intN)
9733 error ("%<long%>, %<int%>, %<short%>, or %<char%> invalid for %qs", name);
9734 else if ((long_p || short_p) && explicit_char)
9735 error ("%<long%> or %<short%> specified with char for %qs", name);
9736 else if (long_p && short_p)
9737 error ("%<long%> and %<short%> specified together for %qs", name);
9738 else if (type == char16_type_node || type == char32_type_node)
9739 {
9740 if (signed_p || unsigned_p)
9741 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
9742 else if (short_p || long_p)
9743 error ("%<short%> or %<long%> invalid for %qs", name);
9744 }
9745 else
9746 {
9747 ok = 1;
9748 if (!explicit_int && !defaulted_int && !explicit_char && !explicit_intN && pedantic)
9749 {
9750 pedwarn (input_location, OPT_Wpedantic,
9751 "long, short, signed or unsigned used invalidly for %qs",
9752 name);
9753 if (flag_pedantic_errors)
9754 ok = 0;
9755 }
9756 }
9757
9758 /* Discard the type modifiers if they are invalid. */
9759 if (! ok)
9760 {
9761 unsigned_p = false;
9762 signed_p = false;
9763 long_p = false;
9764 short_p = false;
9765 longlong = 0;
9766 }
9767 }
9768
9769 /* Decide whether an integer type is signed or not.
9770 Optionally treat bitfields as signed by default. */
9771 if (unsigned_p
9772 /* [class.bit]
9773
9774 It is implementation-defined whether a plain (neither
9775 explicitly signed or unsigned) char, short, int, or long
9776 bit-field is signed or unsigned.
9777
9778 Naturally, we extend this to long long as well. Note that
9779 this does not include wchar_t. */
9780 || (bitfield && !flag_signed_bitfields
9781 && !signed_p
9782 /* A typedef for plain `int' without `signed' can be
9783 controlled just like plain `int', but a typedef for
9784 `signed int' cannot be so controlled. */
9785 && !(typedef_decl
9786 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
9787 && TREE_CODE (type) == INTEGER_TYPE
9788 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
9789 {
9790 if (explicit_intN)
9791 type = int_n_trees[declspecs->int_n_idx].unsigned_type;
9792 else if (longlong)
9793 type = long_long_unsigned_type_node;
9794 else if (long_p)
9795 type = long_unsigned_type_node;
9796 else if (short_p)
9797 type = short_unsigned_type_node;
9798 else if (type == char_type_node)
9799 type = unsigned_char_type_node;
9800 else if (typedef_decl)
9801 type = unsigned_type_for (type);
9802 else
9803 type = unsigned_type_node;
9804 }
9805 else if (signed_p && type == char_type_node)
9806 type = signed_char_type_node;
9807 else if (explicit_intN)
9808 type = int_n_trees[declspecs->int_n_idx].signed_type;
9809 else if (longlong)
9810 type = long_long_integer_type_node;
9811 else if (long_p)
9812 type = long_integer_type_node;
9813 else if (short_p)
9814 type = short_integer_type_node;
9815
9816 if (decl_spec_seq_has_spec_p (declspecs, ds_complex))
9817 {
9818 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9819 error ("complex invalid for %qs", name);
9820 /* If a modifier is specified, the resulting complex is the complex
9821 form of TYPE. E.g, "complex short" is "complex short int". */
9822 else if (type == integer_type_node)
9823 type = complex_integer_type_node;
9824 else if (type == float_type_node)
9825 type = complex_float_type_node;
9826 else if (type == double_type_node)
9827 type = complex_double_type_node;
9828 else if (type == long_double_type_node)
9829 type = complex_long_double_type_node;
9830 else
9831 type = build_complex_type (type);
9832 }
9833
9834 /* If we're using the injected-class-name to form a compound type or a
9835 declaration, replace it with the underlying class so we don't get
9836 redundant typedefs in the debug output. But if we are returning the
9837 type unchanged, leave it alone so that it's available to
9838 maybe_get_template_decl_from_type_decl. */
9839 if (CLASS_TYPE_P (type)
9840 && DECL_SELF_REFERENCE_P (TYPE_NAME (type))
9841 && type == TREE_TYPE (TYPE_NAME (type))
9842 && (declarator || type_quals))
9843 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
9844
9845 type_quals |= cp_type_quals (type);
9846 type = cp_build_qualified_type_real
9847 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl))
9848 || declspecs->decltype_p)
9849 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
9850 /* We might have ignored or rejected some of the qualifiers. */
9851 type_quals = cp_type_quals (type);
9852
9853 staticp = 0;
9854 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline);
9855 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual);
9856 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit);
9857
9858 storage_class = declspecs->storage_class;
9859 if (storage_class == sc_static)
9860 staticp = 1 + (decl_context == FIELD);
9861
9862 if (virtualp)
9863 {
9864 if (staticp == 2)
9865 {
9866 error ("member %qD cannot be declared both %<virtual%> "
9867 "and %<static%>", dname);
9868 storage_class = sc_none;
9869 staticp = 0;
9870 }
9871 if (constexpr_p)
9872 error ("member %qD cannot be declared both %<virtual%> "
9873 "and %<constexpr%>", dname);
9874 }
9875 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend);
9876
9877 /* Issue errors about use of storage classes for parameters. */
9878 if (decl_context == PARM)
9879 {
9880 if (typedef_p)
9881 {
9882 error ("typedef declaration invalid in parameter declaration");
9883 return error_mark_node;
9884 }
9885 else if (template_parm_flag && storage_class != sc_none)
9886 {
9887 error ("storage class specified for template parameter %qs", name);
9888 return error_mark_node;
9889 }
9890 else if (storage_class == sc_static
9891 || storage_class == sc_extern
9892 || thread_p)
9893 error ("storage class specifiers invalid in parameter declarations");
9894
9895 /* Function parameters cannot be concept. */
9896 if (concept_p)
9897 error ("a parameter cannot be declared %<concept%>");
9898 /* Function parameters cannot be constexpr. If we saw one, moan
9899 and pretend it wasn't there. */
9900 else if (constexpr_p)
9901 {
9902 error ("a parameter cannot be declared %<constexpr%>");
9903 constexpr_p = 0;
9904 }
9905 }
9906
9907 /* Give error if `virtual' is used outside of class declaration. */
9908 if (virtualp
9909 && (current_class_name == NULL_TREE || decl_context != FIELD))
9910 {
9911 error_at (declspecs->locations[ds_virtual],
9912 "%<virtual%> outside class declaration");
9913 virtualp = 0;
9914 }
9915
9916 /* Static anonymous unions are dealt with here. */
9917 if (staticp && decl_context == TYPENAME
9918 && declspecs->type
9919 && ANON_AGGR_TYPE_P (declspecs->type))
9920 decl_context = FIELD;
9921
9922 /* Warn about storage classes that are invalid for certain
9923 kinds of declarations (parameters, typenames, etc.). */
9924 if (thread_p
9925 && ((storage_class
9926 && storage_class != sc_extern
9927 && storage_class != sc_static)
9928 || typedef_p))
9929 {
9930 error ("multiple storage classes in declaration of %qs", name);
9931 thread_p = false;
9932 }
9933 if (decl_context != NORMAL
9934 && ((storage_class != sc_none
9935 && storage_class != sc_mutable)
9936 || thread_p))
9937 {
9938 if ((decl_context == PARM || decl_context == CATCHPARM)
9939 && (storage_class == sc_register
9940 || storage_class == sc_auto))
9941 ;
9942 else if (typedef_p)
9943 ;
9944 else if (decl_context == FIELD
9945 /* C++ allows static class elements. */
9946 && storage_class == sc_static)
9947 /* C++ also allows inlines and signed and unsigned elements,
9948 but in those cases we don't come in here. */
9949 ;
9950 else
9951 {
9952 if (decl_context == FIELD)
9953 error ("storage class specified for %qs", name);
9954 else
9955 {
9956 if (decl_context == PARM || decl_context == CATCHPARM)
9957 error ("storage class specified for parameter %qs", name);
9958 else
9959 error ("storage class specified for typename");
9960 }
9961 if (storage_class == sc_register
9962 || storage_class == sc_auto
9963 || storage_class == sc_extern
9964 || thread_p)
9965 storage_class = sc_none;
9966 }
9967 }
9968 else if (storage_class == sc_extern && funcdef_flag
9969 && ! toplevel_bindings_p ())
9970 error ("nested function %qs declared %<extern%>", name);
9971 else if (toplevel_bindings_p ())
9972 {
9973 if (storage_class == sc_auto)
9974 error ("top-level declaration of %qs specifies %<auto%>", name);
9975 }
9976 else if (thread_p
9977 && storage_class != sc_extern
9978 && storage_class != sc_static)
9979 {
9980 if (declspecs->gnu_thread_keyword_p)
9981 pedwarn (input_location, 0, "function-scope %qs implicitly auto and "
9982 "declared %<__thread%>", name);
9983
9984 /* When thread_local is applied to a variable of block scope the
9985 storage-class-specifier static is implied if it does not appear
9986 explicitly. */
9987 storage_class = declspecs->storage_class = sc_static;
9988 staticp = 1;
9989 }
9990
9991 if (storage_class && friendp)
9992 {
9993 error ("storage class specifiers invalid in friend function declarations");
9994 storage_class = sc_none;
9995 staticp = 0;
9996 }
9997
9998 if (!id_declarator)
9999 unqualified_id = NULL_TREE;
10000 else
10001 {
10002 unqualified_id = id_declarator->u.id.unqualified_name;
10003 switch (TREE_CODE (unqualified_id))
10004 {
10005 case BIT_NOT_EXPR:
10006 unqualified_id = TREE_OPERAND (unqualified_id, 0);
10007 if (TYPE_P (unqualified_id))
10008 unqualified_id = constructor_name (unqualified_id);
10009 break;
10010
10011 case IDENTIFIER_NODE:
10012 case TEMPLATE_ID_EXPR:
10013 break;
10014
10015 default:
10016 gcc_unreachable ();
10017 }
10018 }
10019
10020 if (declspecs->std_attributes)
10021 {
10022 /* Apply the c++11 attributes to the type preceding them. */
10023 input_location = declspecs->locations[ds_std_attribute];
10024 decl_attributes (&type, declspecs->std_attributes, 0);
10025 input_location = saved_loc;
10026 }
10027
10028 /* Determine the type of the entity declared by recurring on the
10029 declarator. */
10030 for (; declarator; declarator = declarator->declarator)
10031 {
10032 const cp_declarator *inner_declarator;
10033 tree attrs;
10034
10035 if (type == error_mark_node)
10036 return error_mark_node;
10037
10038 attrs = declarator->attributes;
10039 if (attrs)
10040 {
10041 int attr_flags;
10042
10043 attr_flags = 0;
10044 if (declarator == NULL || declarator->kind == cdk_id)
10045 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10046 if (declarator->kind == cdk_function)
10047 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10048 if (declarator->kind == cdk_array)
10049 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10050 returned_attrs = decl_attributes (&type,
10051 chainon (returned_attrs, attrs),
10052 attr_flags);
10053 }
10054
10055 if (declarator->kind == cdk_id)
10056 break;
10057
10058 inner_declarator = declarator->declarator;
10059
10060 switch (declarator->kind)
10061 {
10062 case cdk_array:
10063 type = create_array_type_for_decl (dname, type,
10064 declarator->u.array.bounds);
10065 if (!valid_array_size_p (input_location, type, dname))
10066 type = error_mark_node;
10067
10068 if (declarator->std_attributes)
10069 /* [dcl.array]/1:
10070
10071 The optional attribute-specifier-seq appertains to the
10072 array. */
10073 returned_attrs = chainon (returned_attrs,
10074 declarator->std_attributes);
10075 break;
10076
10077 case cdk_function:
10078 {
10079 tree arg_types;
10080 int funcdecl_p;
10081
10082 /* Declaring a function type.
10083 Make sure we have a valid type for the function to return. */
10084
10085 if (type_quals != TYPE_UNQUALIFIED)
10086 {
10087 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))
10088 warning (OPT_Wignored_qualifiers,
10089 "type qualifiers ignored on function return type");
10090 /* We now know that the TYPE_QUALS don't apply to the
10091 decl, but to its return type. */
10092 type_quals = TYPE_UNQUALIFIED;
10093 }
10094 errmsg = targetm.invalid_return_type (type);
10095 if (errmsg)
10096 {
10097 error (errmsg);
10098 type = integer_type_node;
10099 }
10100
10101 /* Error about some types functions can't return. */
10102
10103 if (TREE_CODE (type) == FUNCTION_TYPE)
10104 {
10105 error ("%qs declared as function returning a function", name);
10106 return error_mark_node;
10107 }
10108 if (TREE_CODE (type) == ARRAY_TYPE)
10109 {
10110 error ("%qs declared as function returning an array", name);
10111 return error_mark_node;
10112 }
10113
10114 input_location = declspecs->locations[ds_type_spec];
10115 abstract_virtuals_error (ACU_RETURN, type);
10116 input_location = saved_loc;
10117
10118 /* Pick up type qualifiers which should be applied to `this'. */
10119 memfn_quals = declarator->u.function.qualifiers;
10120 /* Pick up virt-specifiers. */
10121 virt_specifiers = declarator->u.function.virt_specifiers;
10122 /* And ref-qualifier, too */
10123 rqual = declarator->u.function.ref_qualifier;
10124 /* And tx-qualifier. */
10125 tree tx_qual = declarator->u.function.tx_qualifier;
10126 /* Pick up the exception specifications. */
10127 raises = declarator->u.function.exception_specification;
10128 /* If the exception-specification is ill-formed, let's pretend
10129 there wasn't one. */
10130 if (raises == error_mark_node)
10131 raises = NULL_TREE;
10132
10133 if (reqs)
10134 error_at (location_of (reqs), "requires-clause on return type");
10135 reqs = declarator->u.function.requires_clause;
10136
10137 /* Say it's a definition only for the CALL_EXPR
10138 closest to the identifier. */
10139 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
10140
10141 /* Handle a late-specified return type. */
10142 if (funcdecl_p)
10143 {
10144 if (type_uses_auto (type))
10145 {
10146 if (!declarator->u.function.late_return_type)
10147 {
10148 if (current_class_type
10149 && LAMBDA_TYPE_P (current_class_type))
10150 /* OK for C++11 lambdas. */;
10151 else if (cxx_dialect < cxx14)
10152 {
10153 error ("%qs function uses "
10154 "%<auto%> type specifier without trailing "
10155 "return type", name);
10156 inform (input_location, "deduced return type "
10157 "only available with -std=c++14 or "
10158 "-std=gnu++14");
10159 }
10160 else if (virtualp)
10161 {
10162 error ("virtual function cannot "
10163 "have deduced return type");
10164 virtualp = false;
10165 }
10166 }
10167 else if (!is_auto (type) && sfk != sfk_conversion)
10168 {
10169 error ("%qs function with trailing return type has"
10170 " %qT as its type rather than plain %<auto%>",
10171 name, type);
10172 return error_mark_node;
10173 }
10174 }
10175 else if (declarator->u.function.late_return_type
10176 && sfk != sfk_conversion)
10177 {
10178 if (cxx_dialect < cxx11)
10179 /* Not using maybe_warn_cpp0x because this should
10180 always be an error. */
10181 error ("trailing return type only available with "
10182 "-std=c++11 or -std=gnu++11");
10183 else
10184 error ("%qs function with trailing return type not "
10185 "declared with %<auto%> type specifier", name);
10186 return error_mark_node;
10187 }
10188 }
10189 type = splice_late_return_type
10190 (type, declarator->u.function.late_return_type);
10191 if (type == error_mark_node)
10192 return error_mark_node;
10193
10194 if (declarator->u.function.late_return_type)
10195 late_return_type_p = true;
10196
10197 if (ctype == NULL_TREE
10198 && decl_context == FIELD
10199 && funcdecl_p
10200 && friendp == 0)
10201 ctype = current_class_type;
10202
10203 if (ctype && (sfk == sfk_constructor
10204 || sfk == sfk_destructor))
10205 {
10206 /* We are within a class's scope. If our declarator name
10207 is the same as the class name, and we are defining
10208 a function, then it is a constructor/destructor, and
10209 therefore returns a void type. */
10210
10211 /* ISO C++ 12.4/2. A destructor may not be declared
10212 const or volatile. A destructor may not be static.
10213 A destructor may not be declared with ref-qualifier.
10214
10215 ISO C++ 12.1. A constructor may not be declared
10216 const or volatile. A constructor may not be
10217 virtual. A constructor may not be static.
10218 A constructor may not be declared with ref-qualifier. */
10219 if (staticp == 2)
10220 error ((flags == DTOR_FLAG)
10221 ? G_("destructor cannot be static member function")
10222 : G_("constructor cannot be static member function"));
10223 if (memfn_quals)
10224 {
10225 error ((flags == DTOR_FLAG)
10226 ? G_("destructors may not be cv-qualified")
10227 : G_("constructors may not be cv-qualified"));
10228 memfn_quals = TYPE_UNQUALIFIED;
10229 }
10230
10231 if (rqual)
10232 {
10233 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER);
10234 error ((flags == DTOR_FLAG)
10235 ? "destructors may not be ref-qualified"
10236 : "constructors may not be ref-qualified");
10237 rqual = REF_QUAL_NONE;
10238 }
10239
10240 if (decl_context == FIELD
10241 && !member_function_or_else (ctype,
10242 current_class_type,
10243 flags))
10244 return error_mark_node;
10245
10246 if (flags != DTOR_FLAG)
10247 {
10248 /* It's a constructor. */
10249 if (explicitp == 1)
10250 explicitp = 2;
10251 if (virtualp)
10252 {
10253 permerror (input_location,
10254 "constructors cannot be declared %<virtual%>");
10255 virtualp = 0;
10256 }
10257 if (decl_context == FIELD
10258 && sfk != sfk_constructor)
10259 return error_mark_node;
10260 }
10261 if (decl_context == FIELD)
10262 staticp = 0;
10263 }
10264 else if (friendp)
10265 {
10266 if (virtualp)
10267 {
10268 /* Cannot be both friend and virtual. */
10269 error ("virtual functions cannot be friends");
10270 friendp = 0;
10271 }
10272 if (decl_context == NORMAL)
10273 error ("friend declaration not in class definition");
10274 if (current_function_decl && funcdef_flag)
10275 error ("can%'t define friend function %qs in a local "
10276 "class definition",
10277 name);
10278 }
10279 else if (ctype && sfk == sfk_conversion)
10280 {
10281 if (explicitp == 1)
10282 {
10283 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION);
10284 explicitp = 2;
10285 }
10286 if (late_return_type_p)
10287 error ("a conversion function cannot have a trailing return type");
10288 }
10289
10290 arg_types = grokparms (declarator->u.function.parameters,
10291 &parms);
10292
10293 if (inner_declarator
10294 && inner_declarator->kind == cdk_id
10295 && inner_declarator->u.id.sfk == sfk_destructor
10296 && arg_types != void_list_node)
10297 {
10298 error ("destructors may not have parameters");
10299 arg_types = void_list_node;
10300 parms = NULL_TREE;
10301 }
10302
10303 type = build_function_type (type, arg_types);
10304
10305 tree attrs = declarator->std_attributes;
10306 if (tx_qual)
10307 {
10308 tree att = build_tree_list (tx_qual, NULL_TREE);
10309 /* transaction_safe applies to the type, but
10310 transaction_safe_dynamic applies to the function. */
10311 if (is_attribute_p ("transaction_safe", tx_qual))
10312 attrs = chainon (attrs, att);
10313 else
10314 returned_attrs = chainon (returned_attrs, att);
10315 }
10316 if (attrs)
10317 /* [dcl.fct]/2:
10318
10319 The optional attribute-specifier-seq appertains to
10320 the function type. */
10321 decl_attributes (&type, attrs, 0);
10322 }
10323 break;
10324
10325 case cdk_pointer:
10326 case cdk_reference:
10327 case cdk_ptrmem:
10328 /* Filter out pointers-to-references and references-to-references.
10329 We can get these if a TYPE_DECL is used. */
10330
10331 if (TREE_CODE (type) == REFERENCE_TYPE)
10332 {
10333 if (declarator->kind != cdk_reference)
10334 {
10335 error ("cannot declare pointer to %q#T", type);
10336 type = TREE_TYPE (type);
10337 }
10338
10339 /* In C++0x, we allow reference to reference declarations
10340 that occur indirectly through typedefs [7.1.3/8 dcl.typedef]
10341 and template type arguments [14.3.1/4 temp.arg.type]. The
10342 check for direct reference to reference declarations, which
10343 are still forbidden, occurs below. Reasoning behind the change
10344 can be found in DR106, DR540, and the rvalue reference
10345 proposals. */
10346 else if (cxx_dialect == cxx98)
10347 {
10348 error ("cannot declare reference to %q#T", type);
10349 type = TREE_TYPE (type);
10350 }
10351 }
10352 else if (VOID_TYPE_P (type))
10353 {
10354 if (declarator->kind == cdk_reference)
10355 error ("cannot declare reference to %q#T", type);
10356 else if (declarator->kind == cdk_ptrmem)
10357 error ("cannot declare pointer to %q#T member", type);
10358 }
10359
10360 /* We now know that the TYPE_QUALS don't apply to the decl,
10361 but to the target of the pointer. */
10362 type_quals = TYPE_UNQUALIFIED;
10363
10364 /* This code used to handle METHOD_TYPE, but I don't think it's
10365 possible to get it here anymore. */
10366 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
10367 if (declarator->kind == cdk_ptrmem
10368 && TREE_CODE (type) == FUNCTION_TYPE)
10369 {
10370 memfn_quals |= type_memfn_quals (type);
10371 type = build_memfn_type (type,
10372 declarator->u.pointer.class_type,
10373 memfn_quals,
10374 rqual);
10375 if (type == error_mark_node)
10376 return error_mark_node;
10377
10378 rqual = REF_QUAL_NONE;
10379 memfn_quals = TYPE_UNQUALIFIED;
10380 }
10381
10382 if (TREE_CODE (type) == FUNCTION_TYPE
10383 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
10384 || type_memfn_rqual (type) != REF_QUAL_NONE))
10385 error (declarator->kind == cdk_reference
10386 ? G_("cannot declare reference to qualified function type %qT")
10387 : G_("cannot declare pointer to qualified function type %qT"),
10388 type);
10389
10390 /* When the pointed-to type involves components of variable size,
10391 care must be taken to ensure that the size evaluation code is
10392 emitted early enough to dominate all the possible later uses
10393 and late enough for the variables on which it depends to have
10394 been assigned.
10395
10396 This is expected to happen automatically when the pointed-to
10397 type has a name/declaration of it's own, but special attention
10398 is required if the type is anonymous.
10399
10400 We handle the NORMAL and FIELD contexts here by inserting a
10401 dummy statement that just evaluates the size at a safe point
10402 and ensures it is not deferred until e.g. within a deeper
10403 conditional context (c++/43555).
10404
10405 We expect nothing to be needed here for PARM or TYPENAME.
10406 Evaluating the size at this point for TYPENAME would
10407 actually be incorrect, as we might be in the middle of an
10408 expression with side effects on the pointed-to type size
10409 "arguments" prior to the pointer declaration point and the
10410 size evaluation could end up prior to the side effects. */
10411
10412 if (!TYPE_NAME (type)
10413 && (decl_context == NORMAL || decl_context == FIELD)
10414 && at_function_scope_p ()
10415 && variably_modified_type_p (type, NULL_TREE))
10416 /* Force evaluation of the SAVE_EXPR. */
10417 finish_expr_stmt (TYPE_SIZE (type));
10418
10419 if (declarator->kind == cdk_reference)
10420 {
10421 /* In C++0x, the type we are creating a reference to might be
10422 a typedef which is itself a reference type. In that case,
10423 we follow the reference collapsing rules in
10424 [7.1.3/8 dcl.typedef] to create the final reference type:
10425
10426 "If a typedef TD names a type that is a reference to a type
10427 T, an attempt to create the type 'lvalue reference to cv TD'
10428 creates the type 'lvalue reference to T,' while an attempt
10429 to create the type "rvalue reference to cv TD' creates the
10430 type TD."
10431 */
10432 if (VOID_TYPE_P (type))
10433 /* We already gave an error. */;
10434 else if (TREE_CODE (type) == REFERENCE_TYPE)
10435 {
10436 if (declarator->u.reference.rvalue_ref)
10437 /* Leave type alone. */;
10438 else
10439 type = cp_build_reference_type (TREE_TYPE (type), false);
10440 }
10441 else
10442 type = cp_build_reference_type
10443 (type, declarator->u.reference.rvalue_ref);
10444
10445 /* In C++0x, we need this check for direct reference to
10446 reference declarations, which are forbidden by
10447 [8.3.2/5 dcl.ref]. Reference to reference declarations
10448 are only allowed indirectly through typedefs and template
10449 type arguments. Example:
10450
10451 void foo(int & &); // invalid ref-to-ref decl
10452
10453 typedef int & int_ref;
10454 void foo(int_ref &); // valid ref-to-ref decl
10455 */
10456 if (inner_declarator && inner_declarator->kind == cdk_reference)
10457 error ("cannot declare reference to %q#T, which is not "
10458 "a typedef or a template type argument", type);
10459 }
10460 else if (TREE_CODE (type) == METHOD_TYPE)
10461 type = build_ptrmemfunc_type (build_pointer_type (type));
10462 else if (declarator->kind == cdk_ptrmem)
10463 {
10464 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
10465 != NAMESPACE_DECL);
10466 if (declarator->u.pointer.class_type == error_mark_node)
10467 /* We will already have complained. */
10468 type = error_mark_node;
10469 else
10470 type = build_ptrmem_type (declarator->u.pointer.class_type,
10471 type);
10472 }
10473 else
10474 type = build_pointer_type (type);
10475
10476 /* Process a list of type modifier keywords (such as
10477 const or volatile) that were given inside the `*' or `&'. */
10478
10479 if (declarator->u.pointer.qualifiers)
10480 {
10481 type
10482 = cp_build_qualified_type (type,
10483 declarator->u.pointer.qualifiers);
10484 type_quals = cp_type_quals (type);
10485 }
10486
10487 /* Apply C++11 attributes to the pointer, and not to the
10488 type pointed to. This is unlike what is done for GNU
10489 attributes above. It is to comply with [dcl.ptr]/1:
10490
10491 [the optional attribute-specifier-seq (7.6.1) appertains
10492 to the pointer and not to the object pointed to]. */
10493 if (declarator->std_attributes)
10494 decl_attributes (&type, declarator->std_attributes,
10495 0);
10496
10497 ctype = NULL_TREE;
10498 break;
10499
10500 case cdk_error:
10501 break;
10502
10503 default:
10504 gcc_unreachable ();
10505 }
10506 }
10507
10508 /* A `constexpr' specifier used in an object declaration declares
10509 the object as `const'. */
10510 if (constexpr_p && innermost_code != cdk_function)
10511 {
10512 /* DR1688 says that a `constexpr' specifier in combination with
10513 `volatile' is valid. */
10514
10515 if (TREE_CODE (type) != REFERENCE_TYPE)
10516 {
10517 type_quals |= TYPE_QUAL_CONST;
10518 type = cp_build_qualified_type (type, type_quals);
10519 }
10520 }
10521
10522 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
10523 && TREE_CODE (type) != FUNCTION_TYPE
10524 && TREE_CODE (type) != METHOD_TYPE
10525 && !variable_template_p (TREE_OPERAND (unqualified_id, 0)))
10526 {
10527 error ("template-id %qD used as a declarator",
10528 unqualified_id);
10529 unqualified_id = dname;
10530 }
10531
10532 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
10533 qualified with a class-name, turn it into a METHOD_TYPE, unless
10534 we know that the function is static. We take advantage of this
10535 opportunity to do other processing that pertains to entities
10536 explicitly declared to be class members. Note that if DECLARATOR
10537 is non-NULL, we know it is a cdk_id declarator; otherwise, we
10538 would not have exited the loop above. */
10539 if (declarator
10540 && declarator->u.id.qualifying_scope
10541 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope))
10542 {
10543 ctype = declarator->u.id.qualifying_scope;
10544 ctype = TYPE_MAIN_VARIANT (ctype);
10545 template_count = num_template_headers_for_class (ctype);
10546
10547 if (ctype == current_class_type)
10548 {
10549 if (friendp)
10550 {
10551 permerror (input_location, "member functions are implicitly friends of their class");
10552 friendp = 0;
10553 }
10554 else
10555 permerror (declarator->id_loc,
10556 "extra qualification %<%T::%> on member %qs",
10557 ctype, name);
10558 }
10559 else if (/* If the qualifying type is already complete, then we
10560 can skip the following checks. */
10561 !COMPLETE_TYPE_P (ctype)
10562 && (/* If the function is being defined, then
10563 qualifying type must certainly be complete. */
10564 funcdef_flag
10565 /* A friend declaration of "T::f" is OK, even if
10566 "T" is a template parameter. But, if this
10567 function is not a friend, the qualifying type
10568 must be a class. */
10569 || (!friendp && !CLASS_TYPE_P (ctype))
10570 /* For a declaration, the type need not be
10571 complete, if either it is dependent (since there
10572 is no meaningful definition of complete in that
10573 case) or the qualifying class is currently being
10574 defined. */
10575 || !(dependent_type_p (ctype)
10576 || currently_open_class (ctype)))
10577 /* Check that the qualifying type is complete. */
10578 && !complete_type_or_else (ctype, NULL_TREE))
10579 return error_mark_node;
10580 else if (TREE_CODE (type) == FUNCTION_TYPE)
10581 {
10582 if (current_class_type
10583 && (!friendp || funcdef_flag || initialized))
10584 {
10585 error (funcdef_flag || initialized
10586 ? G_("cannot define member function %<%T::%s%> "
10587 "within %<%T%>")
10588 : G_("cannot declare member function %<%T::%s%> "
10589 "within %<%T%>"),
10590 ctype, name, current_class_type);
10591 return error_mark_node;
10592 }
10593 }
10594 else if (typedef_p && current_class_type)
10595 {
10596 error ("cannot declare member %<%T::%s%> within %qT",
10597 ctype, name, current_class_type);
10598 return error_mark_node;
10599 }
10600 }
10601
10602 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
10603 ctype = current_class_type;
10604
10605 /* Now TYPE has the actual type. */
10606
10607 if (returned_attrs)
10608 {
10609 if (attrlist)
10610 *attrlist = chainon (returned_attrs, *attrlist);
10611 else
10612 attrlist = &returned_attrs;
10613 }
10614
10615 if (declarator
10616 && declarator->kind == cdk_id
10617 && declarator->std_attributes)
10618 /* [dcl.meaning]/1: The optional attribute-specifier-seq following
10619 a declarator-id appertains to the entity that is declared. */
10620 *attrlist = chainon (*attrlist, declarator->std_attributes);
10621
10622 /* Handle parameter packs. */
10623 if (parameter_pack_p)
10624 {
10625 if (decl_context == PARM)
10626 /* Turn the type into a pack expansion.*/
10627 type = make_pack_expansion (type);
10628 else
10629 error ("non-parameter %qs cannot be a parameter pack", name);
10630 }
10631
10632 if ((decl_context == FIELD || decl_context == PARM)
10633 && !processing_template_decl
10634 && variably_modified_type_p (type, NULL_TREE))
10635 {
10636 if (decl_context == FIELD)
10637 error ("data member may not have variably modified type %qT", type);
10638 else
10639 error ("parameter may not have variably modified type %qT", type);
10640 type = error_mark_node;
10641 }
10642
10643 if (explicitp == 1 || (explicitp && friendp))
10644 {
10645 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only
10646 in the declaration of a constructor or conversion function within
10647 a class definition. */
10648 if (!current_class_type)
10649 error_at (declspecs->locations[ds_explicit],
10650 "%<explicit%> outside class declaration");
10651 else if (friendp)
10652 error_at (declspecs->locations[ds_explicit],
10653 "%<explicit%> in friend declaration");
10654 else
10655 error_at (declspecs->locations[ds_explicit],
10656 "only declarations of constructors and conversion operators "
10657 "can be %<explicit%>");
10658 explicitp = 0;
10659 }
10660
10661 if (storage_class == sc_mutable)
10662 {
10663 if (decl_context != FIELD || friendp)
10664 {
10665 error ("non-member %qs cannot be declared %<mutable%>", name);
10666 storage_class = sc_none;
10667 }
10668 else if (decl_context == TYPENAME || typedef_p)
10669 {
10670 error ("non-object member %qs cannot be declared %<mutable%>", name);
10671 storage_class = sc_none;
10672 }
10673 else if (TREE_CODE (type) == FUNCTION_TYPE
10674 || TREE_CODE (type) == METHOD_TYPE)
10675 {
10676 error ("function %qs cannot be declared %<mutable%>", name);
10677 storage_class = sc_none;
10678 }
10679 else if (staticp)
10680 {
10681 error ("static %qs cannot be declared %<mutable%>", name);
10682 storage_class = sc_none;
10683 }
10684 else if (type_quals & TYPE_QUAL_CONST)
10685 {
10686 error ("const %qs cannot be declared %<mutable%>", name);
10687 storage_class = sc_none;
10688 }
10689 else if (TREE_CODE (type) == REFERENCE_TYPE)
10690 {
10691 permerror (input_location, "reference %qs cannot be declared "
10692 "%<mutable%>", name);
10693 storage_class = sc_none;
10694 }
10695 }
10696
10697 /* If this is declaring a typedef name, return a TYPE_DECL. */
10698 if (typedef_p && decl_context != TYPENAME)
10699 {
10700 tree decl;
10701
10702 /* Note that the grammar rejects storage classes
10703 in typenames, fields or parameters. */
10704 if (current_lang_name == lang_name_java)
10705 TYPE_FOR_JAVA (type) = 1;
10706
10707 /* This declaration:
10708
10709 typedef void f(int) const;
10710
10711 declares a function type which is not a member of any
10712 particular class, but which is cv-qualified; for
10713 example "f S::*" declares a pointer to a const-qualified
10714 member function of S. We record the cv-qualification in the
10715 function type. */
10716 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE)
10717 {
10718 type = apply_memfn_quals (type, memfn_quals, rqual);
10719
10720 /* We have now dealt with these qualifiers. */
10721 memfn_quals = TYPE_UNQUALIFIED;
10722 rqual = REF_QUAL_NONE;
10723 }
10724
10725 if (type_uses_auto (type))
10726 {
10727 error ("typedef declared %<auto%>");
10728 type = error_mark_node;
10729 }
10730
10731 if (reqs)
10732 error_at (location_of (reqs), "requires-clause on typedef");
10733
10734 if (decl_context == FIELD)
10735 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
10736 else
10737 decl = build_decl (input_location, TYPE_DECL, unqualified_id, type);
10738 if (id_declarator && declarator->u.id.qualifying_scope) {
10739 error_at (DECL_SOURCE_LOCATION (decl),
10740 "typedef name may not be a nested-name-specifier");
10741 TREE_TYPE (decl) = error_mark_node;
10742 }
10743
10744 if (decl_context != FIELD)
10745 {
10746 if (!current_function_decl)
10747 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
10748 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
10749 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
10750 (current_function_decl)))
10751 /* The TYPE_DECL is "abstract" because there will be
10752 clones of this constructor/destructor, and there will
10753 be copies of this TYPE_DECL generated in those
10754 clones. The decloning optimization (for space) may
10755 revert this subsequently if it determines that
10756 the clones should share a common implementation. */
10757 DECL_ABSTRACT_P (decl) = true;
10758 }
10759 else if (current_class_type
10760 && constructor_name_p (unqualified_id, current_class_type))
10761 permerror (input_location, "ISO C++ forbids nested type %qD with same name "
10762 "as enclosing class",
10763 unqualified_id);
10764
10765 /* If the user declares "typedef struct {...} foo" then the
10766 struct will have an anonymous name. Fill that name in now.
10767 Nothing can refer to it, so nothing needs know about the name
10768 change. */
10769 if (type != error_mark_node
10770 && unqualified_id
10771 && TYPE_NAME (type)
10772 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10773 && TYPE_ANONYMOUS_P (type)
10774 && declspecs->type_definition_p
10775 && attributes_naming_typedef_ok (*attrlist)
10776 && cp_type_quals (type) == TYPE_UNQUALIFIED)
10777 {
10778 tree t;
10779
10780 /* Replace the anonymous name with the real name everywhere. */
10781 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10782 {
10783 if (anon_aggrname_p (TYPE_IDENTIFIER (t)))
10784 /* We do not rename the debug info representing the
10785 anonymous tagged type because the standard says in
10786 [dcl.typedef] that the naming applies only for
10787 linkage purposes. */
10788 /*debug_hooks->set_name (t, decl);*/
10789 TYPE_NAME (t) = decl;
10790 }
10791
10792 if (TYPE_LANG_SPECIFIC (type))
10793 TYPE_WAS_ANONYMOUS (type) = 1;
10794
10795 /* If this is a typedef within a template class, the nested
10796 type is a (non-primary) template. The name for the
10797 template needs updating as well. */
10798 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
10799 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
10800 = TYPE_IDENTIFIER (type);
10801
10802 /* Adjust linkage now that we aren't anonymous anymore. */
10803 reset_type_linkage (type);
10804
10805 /* FIXME remangle member functions; member functions of a
10806 type with external linkage have external linkage. */
10807 }
10808
10809 if (signed_p
10810 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10811 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
10812
10813 bad_specifiers (decl, BSP_TYPE, virtualp,
10814 memfn_quals != TYPE_UNQUALIFIED,
10815 inlinep, friendp, raises != NULL_TREE);
10816
10817 if (decl_spec_seq_has_spec_p (declspecs, ds_alias))
10818 /* Acknowledge that this was written:
10819 `using analias = atype;'. */
10820 TYPE_DECL_ALIAS_P (decl) = 1;
10821
10822 return decl;
10823 }
10824
10825 /* Detect the case of an array type of unspecified size
10826 which came, as such, direct from a typedef name.
10827 We must copy the type, so that the array's domain can be
10828 individually set by the object's initializer. */
10829
10830 if (type && typedef_type
10831 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
10832 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
10833 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
10834
10835 /* Detect where we're using a typedef of function type to declare a
10836 function. PARMS will not be set, so we must create it now. */
10837
10838 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
10839 {
10840 tree decls = NULL_TREE;
10841 tree args;
10842
10843 for (args = TYPE_ARG_TYPES (type);
10844 args && args != void_list_node;
10845 args = TREE_CHAIN (args))
10846 {
10847 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
10848
10849 DECL_CHAIN (decl) = decls;
10850 decls = decl;
10851 }
10852
10853 parms = nreverse (decls);
10854
10855 if (decl_context != TYPENAME)
10856 {
10857 /* The qualifiers on the function type become the qualifiers on
10858 the non-static member function. */
10859 memfn_quals |= type_memfn_quals (type);
10860 rqual = type_memfn_rqual (type);
10861 type_quals = TYPE_UNQUALIFIED;
10862 }
10863 }
10864
10865 /* If this is a type name (such as, in a cast or sizeof),
10866 compute the type and return it now. */
10867
10868 if (decl_context == TYPENAME)
10869 {
10870 /* Note that here we don't care about type_quals. */
10871
10872 /* Special case: "friend class foo" looks like a TYPENAME context. */
10873 if (friendp)
10874 {
10875 if (inlinep)
10876 {
10877 error ("%<inline%> specified for friend class declaration");
10878 inlinep = 0;
10879 }
10880
10881 if (!current_aggr)
10882 {
10883 /* Don't allow friend declaration without a class-key. */
10884 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
10885 permerror (input_location, "template parameters cannot be friends");
10886 else if (TREE_CODE (type) == TYPENAME_TYPE)
10887 permerror (input_location, "friend declaration requires class-key, "
10888 "i.e. %<friend class %T::%D%>",
10889 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
10890 else
10891 permerror (input_location, "friend declaration requires class-key, "
10892 "i.e. %<friend %#T%>",
10893 type);
10894 }
10895
10896 /* Only try to do this stuff if we didn't already give up. */
10897 if (type != integer_type_node)
10898 {
10899 /* A friendly class? */
10900 if (current_class_type)
10901 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
10902 /*complain=*/true);
10903 else
10904 error ("trying to make class %qT a friend of global scope",
10905 type);
10906
10907 type = void_type_node;
10908 }
10909 }
10910 else if (memfn_quals || rqual)
10911 {
10912 if (ctype == NULL_TREE
10913 && TREE_CODE (type) == METHOD_TYPE)
10914 ctype = TYPE_METHOD_BASETYPE (type);
10915
10916 if (ctype)
10917 type = build_memfn_type (type, ctype, memfn_quals, rqual);
10918 /* Core issue #547: need to allow this in template type args.
10919 Allow it in general in C++11 for alias-declarations. */
10920 else if ((template_type_arg || cxx_dialect >= cxx11)
10921 && TREE_CODE (type) == FUNCTION_TYPE)
10922 type = apply_memfn_quals (type, memfn_quals, rqual);
10923 else
10924 error ("invalid qualifiers on non-member function type");
10925 }
10926
10927 if (reqs)
10928 error_at (location_of (reqs), "requires-clause on type-id");
10929
10930 return type;
10931 }
10932 else if (unqualified_id == NULL_TREE && decl_context != PARM
10933 && decl_context != CATCHPARM
10934 && TREE_CODE (type) != UNION_TYPE
10935 && ! bitfield)
10936 {
10937 error ("abstract declarator %qT used as declaration", type);
10938 return error_mark_node;
10939 }
10940
10941 /* Only functions may be declared using an operator-function-id. */
10942 if (unqualified_id
10943 && IDENTIFIER_OPNAME_P (unqualified_id)
10944 && TREE_CODE (type) != FUNCTION_TYPE
10945 && TREE_CODE (type) != METHOD_TYPE)
10946 {
10947 error ("declaration of %qD as non-function", unqualified_id);
10948 return error_mark_node;
10949 }
10950
10951 if (reqs
10952 && TREE_CODE (type) != FUNCTION_TYPE
10953 && TREE_CODE (type) != METHOD_TYPE)
10954 error_at (location_of (reqs),
10955 "requires-clause on declaration of non-function type %qT",
10956 type);
10957
10958 /* We don't check parameter types here because we can emit a better
10959 error message later. */
10960 if (decl_context != PARM)
10961 {
10962 type = check_var_type (unqualified_id, type);
10963 if (type == error_mark_node)
10964 return error_mark_node;
10965 }
10966
10967 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
10968 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
10969
10970 if (decl_context == PARM || decl_context == CATCHPARM)
10971 {
10972 if (ctype || in_namespace)
10973 error ("cannot use %<::%> in parameter declaration");
10974
10975 if (type_uses_auto (type))
10976 {
10977 if (cxx_dialect >= cxx14)
10978 error ("%<auto%> parameter not permitted in this context");
10979 else
10980 error ("parameter declared %<auto%>");
10981 type = error_mark_node;
10982 }
10983
10984 /* A parameter declared as an array of T is really a pointer to T.
10985 One declared as a function is really a pointer to a function.
10986 One declared as a member is really a pointer to member. */
10987
10988 if (TREE_CODE (type) == ARRAY_TYPE)
10989 {
10990 /* Transfer const-ness of array into that of type pointed to. */
10991 type = build_pointer_type (TREE_TYPE (type));
10992 type_quals = TYPE_UNQUALIFIED;
10993 array_parameter_p = true;
10994 }
10995 else if (TREE_CODE (type) == FUNCTION_TYPE)
10996 type = build_pointer_type (type);
10997 }
10998
10999 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
11000 && !NEW_DELETE_OPNAME_P (unqualified_id))
11001 {
11002 cp_cv_quals real_quals = memfn_quals;
11003 if (cxx_dialect < cxx14 && constexpr_p
11004 && sfk != sfk_constructor && sfk != sfk_destructor)
11005 real_quals |= TYPE_QUAL_CONST;
11006 type = build_memfn_type (type, ctype, real_quals, rqual);
11007 }
11008
11009 {
11010 tree decl = NULL_TREE;
11011
11012 if (decl_context == PARM)
11013 {
11014 decl = cp_build_parm_decl (unqualified_id, type);
11015 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p;
11016
11017 bad_specifiers (decl, BSP_PARM, virtualp,
11018 memfn_quals != TYPE_UNQUALIFIED,
11019 inlinep, friendp, raises != NULL_TREE);
11020 }
11021 else if (decl_context == FIELD)
11022 {
11023 if (!staticp && !friendp && TREE_CODE (type) != METHOD_TYPE
11024 && type_uses_auto (type))
11025 {
11026 error ("non-static data member declared %<auto%>");
11027 type = error_mark_node;
11028 }
11029
11030 /* The C99 flexible array extension. */
11031 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
11032 && TYPE_DOMAIN (type) == NULL_TREE)
11033 {
11034 if (ctype
11035 && (TREE_CODE (ctype) == UNION_TYPE
11036 || TREE_CODE (ctype) == QUAL_UNION_TYPE))
11037 {
11038 error ("flexible array member in union");
11039 type = error_mark_node;
11040 }
11041 else
11042 {
11043 /* Flexible array member has a null domain. */
11044 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11045 }
11046 }
11047
11048 if (type == error_mark_node)
11049 {
11050 /* Happens when declaring arrays of sizes which
11051 are error_mark_node, for example. */
11052 decl = NULL_TREE;
11053 }
11054 else if (in_namespace && !friendp)
11055 {
11056 /* Something like struct S { int N::j; }; */
11057 error ("invalid use of %<::%>");
11058 return error_mark_node;
11059 }
11060 else if (TREE_CODE (type) == FUNCTION_TYPE
11061 || TREE_CODE (type) == METHOD_TYPE)
11062 {
11063 int publicp = 0;
11064 tree function_context;
11065
11066 if (friendp == 0)
11067 {
11068 /* This should never happen in pure C++ (the check
11069 could be an assert). It could happen in
11070 Objective-C++ if someone writes invalid code that
11071 uses a function declaration for an instance
11072 variable or property (instance variables and
11073 properties are parsed as FIELD_DECLs, but they are
11074 part of an Objective-C class, not a C++ class).
11075 That code is invalid and is caught by this
11076 check. */
11077 if (!ctype)
11078 {
11079 error ("declaration of function %qD in invalid context",
11080 unqualified_id);
11081 return error_mark_node;
11082 }
11083
11084 /* ``A union may [ ... ] not [ have ] virtual functions.''
11085 ARM 9.5 */
11086 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11087 {
11088 error ("function %qD declared %<virtual%> inside a union",
11089 unqualified_id);
11090 return error_mark_node;
11091 }
11092
11093 if (NEW_DELETE_OPNAME_P (unqualified_id))
11094 {
11095 if (virtualp)
11096 {
11097 error ("%qD cannot be declared %<virtual%>, since it "
11098 "is always static",
11099 unqualified_id);
11100 virtualp = 0;
11101 }
11102 }
11103 }
11104
11105 /* Check that the name used for a destructor makes sense. */
11106 if (sfk == sfk_destructor)
11107 {
11108 tree uqname = id_declarator->u.id.unqualified_name;
11109
11110 if (!ctype)
11111 {
11112 gcc_assert (friendp);
11113 error ("expected qualified name in friend declaration "
11114 "for destructor %qD", uqname);
11115 return error_mark_node;
11116 }
11117
11118 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0)))
11119 {
11120 error ("declaration of %qD as member of %qT",
11121 uqname, ctype);
11122 return error_mark_node;
11123 }
11124 if (concept_p)
11125 {
11126 error ("a destructor cannot be %<concept%>");
11127 return error_mark_node;
11128 }
11129 if (constexpr_p)
11130 {
11131 error ("a destructor cannot be %<constexpr%>");
11132 return error_mark_node;
11133 }
11134 }
11135 else if (sfk == sfk_constructor && friendp && !ctype)
11136 {
11137 error ("expected qualified name in friend declaration "
11138 "for constructor %qD",
11139 id_declarator->u.id.unqualified_name);
11140 return error_mark_node;
11141 }
11142 if (sfk == sfk_constructor)
11143 if (concept_p)
11144 {
11145 error ("a constructor cannot be %<concept%>");
11146 return error_mark_node;
11147 }
11148 if (concept_p)
11149 {
11150 error ("a concept cannot be a member function");
11151 concept_p = false;
11152 }
11153
11154 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11155 {
11156 tree tmpl = TREE_OPERAND (unqualified_id, 0);
11157 if (variable_template_p (tmpl))
11158 {
11159 error ("specialization of variable template %qD "
11160 "declared as function", tmpl);
11161 inform (DECL_SOURCE_LOCATION (tmpl),
11162 "variable template declared here");
11163 return error_mark_node;
11164 }
11165 }
11166
11167 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
11168 function_context = (ctype != NULL_TREE) ?
11169 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11170 publicp = (! friendp || ! staticp)
11171 && function_context == NULL_TREE;
11172
11173 if (late_return_type_p)
11174 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11175
11176 decl = grokfndecl (ctype, type,
11177 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
11178 ? unqualified_id : dname,
11179 parms,
11180 unqualified_id,
11181 reqs,
11182 virtualp, flags, memfn_quals, rqual, raises,
11183 friendp ? -1 : 0, friendp, publicp,
11184 inlinep | (2 * constexpr_p) | (4 * concept_p),
11185 initialized == SD_DELETED, sfk,
11186 funcdef_flag, template_count, in_namespace,
11187 attrlist, declarator->id_loc);
11188 decl = set_virt_specifiers (decl, virt_specifiers);
11189 if (decl == NULL_TREE)
11190 return error_mark_node;
11191 #if 0
11192 /* This clobbers the attrs stored in `decl' from `attrlist'. */
11193 /* The decl and setting of decl_attr is also turned off. */
11194 decl = build_decl_attribute_variant (decl, decl_attr);
11195 #endif
11196
11197 /* [class.conv.ctor]
11198
11199 A constructor declared without the function-specifier
11200 explicit that can be called with a single parameter
11201 specifies a conversion from the type of its first
11202 parameter to the type of its class. Such a constructor
11203 is called a converting constructor. */
11204 if (explicitp == 2)
11205 DECL_NONCONVERTING_P (decl) = 1;
11206 }
11207 else if (!staticp && !dependent_type_p (type)
11208 && !COMPLETE_TYPE_P (complete_type (type))
11209 && (!complete_or_array_type_p (type)
11210 || initialized == 0))
11211 {
11212 if (TREE_CODE (type) != ARRAY_TYPE
11213 || !COMPLETE_TYPE_P (TREE_TYPE (type)))
11214 {
11215 if (unqualified_id)
11216 {
11217 error ("field %qD has incomplete type %qT",
11218 unqualified_id, type);
11219 cxx_incomplete_type_inform (strip_array_types (type));
11220 }
11221 else
11222 error ("name %qT has incomplete type", type);
11223
11224 type = error_mark_node;
11225 decl = NULL_TREE;
11226 }
11227 }
11228 else
11229 {
11230 if (friendp)
11231 {
11232 error ("%qE is neither function nor member function; "
11233 "cannot be declared friend", unqualified_id);
11234 friendp = 0;
11235 }
11236 decl = NULL_TREE;
11237 }
11238
11239 if (friendp)
11240 {
11241 /* Friends are treated specially. */
11242 if (ctype == current_class_type)
11243 ; /* We already issued a permerror. */
11244 else if (decl && DECL_NAME (decl))
11245 {
11246 if (template_class_depth (current_class_type) == 0)
11247 {
11248 decl = check_explicit_specialization
11249 (unqualified_id, decl, template_count,
11250 2 * funcdef_flag + 4);
11251 if (decl == error_mark_node)
11252 return error_mark_node;
11253 }
11254
11255 decl = do_friend (ctype, unqualified_id, decl,
11256 *attrlist, flags,
11257 funcdef_flag);
11258 return decl;
11259 }
11260 else
11261 return error_mark_node;
11262 }
11263
11264 /* Structure field. It may not be a function, except for C++. */
11265
11266 if (decl == NULL_TREE)
11267 {
11268 if (staticp)
11269 {
11270 /* C++ allows static class members. All other work
11271 for this is done by grokfield. */
11272 decl = build_lang_decl_loc (declarator
11273 ? declarator->id_loc
11274 : input_location,
11275 VAR_DECL, unqualified_id, type);
11276 set_linkage_for_static_data_member (decl);
11277 /* Even if there is an in-class initialization, DECL
11278 is considered undefined until an out-of-class
11279 definition is provided. */
11280 DECL_EXTERNAL (decl) = 1;
11281
11282 if (thread_p)
11283 {
11284 CP_DECL_THREAD_LOCAL_P (decl) = true;
11285 if (!processing_template_decl)
11286 set_decl_tls_model (decl, decl_default_tls_model (decl));
11287 if (declspecs->gnu_thread_keyword_p)
11288 SET_DECL_GNU_TLS_P (decl);
11289 }
11290 if (concept_p)
11291 error ("static data member %qE declared %<concept%>",
11292 unqualified_id);
11293 else if (constexpr_p && !initialized)
11294 {
11295 error ("constexpr static data member %qD must have an "
11296 "initializer", decl);
11297 constexpr_p = false;
11298 }
11299 }
11300 else
11301 {
11302 if (concept_p)
11303 error ("non-static data member %qE declared %<concept%>",
11304 unqualified_id);
11305 else if (constexpr_p)
11306 {
11307 error ("non-static data member %qE declared %<constexpr%>",
11308 unqualified_id);
11309 constexpr_p = false;
11310 }
11311 decl = build_decl (input_location,
11312 FIELD_DECL, unqualified_id, type);
11313 DECL_NONADDRESSABLE_P (decl) = bitfield;
11314 if (bitfield && !unqualified_id)
11315 TREE_NO_WARNING (decl) = 1;
11316
11317 if (storage_class == sc_mutable)
11318 {
11319 DECL_MUTABLE_P (decl) = 1;
11320 storage_class = sc_none;
11321 }
11322
11323 if (initialized)
11324 {
11325 /* An attempt is being made to initialize a non-static
11326 member. This is new in C++11. */
11327 maybe_warn_cpp0x (CPP0X_NSDMI);
11328
11329 /* If this has been parsed with static storage class, but
11330 errors forced staticp to be cleared, ensure NSDMI is
11331 not present. */
11332 if (declspecs->storage_class == sc_static)
11333 DECL_INITIAL (decl) = error_mark_node;
11334 }
11335 }
11336
11337 bad_specifiers (decl, BSP_FIELD, virtualp,
11338 memfn_quals != TYPE_UNQUALIFIED,
11339 inlinep, friendp, raises != NULL_TREE);
11340 }
11341 }
11342 else if (TREE_CODE (type) == FUNCTION_TYPE
11343 || TREE_CODE (type) == METHOD_TYPE)
11344 {
11345 tree original_name;
11346 int publicp = 0;
11347
11348 if (!unqualified_id)
11349 return error_mark_node;
11350
11351 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
11352 original_name = dname;
11353 else
11354 original_name = unqualified_id;
11355
11356 if (storage_class == sc_auto)
11357 error ("storage class %<auto%> invalid for function %qs", name);
11358 else if (storage_class == sc_register)
11359 error ("storage class %<register%> invalid for function %qs", name);
11360 else if (thread_p)
11361 {
11362 if (declspecs->gnu_thread_keyword_p)
11363 error ("storage class %<__thread%> invalid for function %qs",
11364 name);
11365 else
11366 error ("storage class %<thread_local%> invalid for function %qs",
11367 name);
11368 }
11369
11370 if (virt_specifiers)
11371 error ("virt-specifiers in %qs not allowed outside a class definition", name);
11372 /* Function declaration not at top level.
11373 Storage classes other than `extern' are not allowed
11374 and `extern' makes no difference. */
11375 if (! toplevel_bindings_p ()
11376 && (storage_class == sc_static
11377 || decl_spec_seq_has_spec_p (declspecs, ds_inline))
11378 && pedantic)
11379 {
11380 if (storage_class == sc_static)
11381 pedwarn (input_location, OPT_Wpedantic,
11382 "%<static%> specified invalid for function %qs "
11383 "declared out of global scope", name);
11384 else
11385 pedwarn (input_location, OPT_Wpedantic,
11386 "%<inline%> specifier invalid for function %qs "
11387 "declared out of global scope", name);
11388 }
11389
11390 if (ctype == NULL_TREE)
11391 {
11392 if (virtualp)
11393 {
11394 error ("virtual non-class function %qs", name);
11395 virtualp = 0;
11396 }
11397 else if (sfk == sfk_constructor
11398 || sfk == sfk_destructor)
11399 {
11400 error (funcdef_flag
11401 ? G_("%qs defined in a non-class scope")
11402 : G_("%qs declared in a non-class scope"), name);
11403 sfk = sfk_none;
11404 }
11405 }
11406
11407 /* Record whether the function is public. */
11408 publicp = (ctype != NULL_TREE
11409 || storage_class != sc_static);
11410
11411 if (late_return_type_p)
11412 TYPE_HAS_LATE_RETURN_TYPE (type) = 1;
11413
11414 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
11415 reqs, virtualp, flags, memfn_quals, rqual, raises,
11416 1, friendp,
11417 publicp,
11418 inlinep | (2 * constexpr_p) | (4 * concept_p),
11419 initialized == SD_DELETED,
11420 sfk,
11421 funcdef_flag,
11422 template_count, in_namespace, attrlist,
11423 declarator->id_loc);
11424 if (decl == NULL_TREE)
11425 return error_mark_node;
11426
11427 if (staticp == 1)
11428 {
11429 int invalid_static = 0;
11430
11431 /* Don't allow a static member function in a class, and forbid
11432 declaring main to be static. */
11433 if (TREE_CODE (type) == METHOD_TYPE)
11434 {
11435 permerror (input_location, "cannot declare member function %qD to have "
11436 "static linkage", decl);
11437 invalid_static = 1;
11438 }
11439 else if (current_function_decl)
11440 {
11441 /* FIXME need arm citation */
11442 error ("cannot declare static function inside another function");
11443 invalid_static = 1;
11444 }
11445
11446 if (invalid_static)
11447 {
11448 staticp = 0;
11449 storage_class = sc_none;
11450 }
11451 }
11452 }
11453 else
11454 {
11455 /* It's a variable. */
11456
11457 /* An uninitialized decl with `extern' is a reference. */
11458 decl = grokvardecl (type, dname, unqualified_id,
11459 declspecs,
11460 initialized,
11461 ((type_quals & TYPE_QUAL_CONST) != 0) | (2 * concept_p),
11462 template_count,
11463 ctype ? ctype : in_namespace);
11464 if (decl == NULL_TREE)
11465 return error_mark_node;
11466
11467 bad_specifiers (decl, BSP_VAR, virtualp,
11468 memfn_quals != TYPE_UNQUALIFIED,
11469 inlinep, friendp, raises != NULL_TREE);
11470
11471 if (ctype)
11472 {
11473 DECL_CONTEXT (decl) = ctype;
11474 if (staticp == 1)
11475 {
11476 permerror (input_location, "%<static%> may not be used when defining "
11477 "(as opposed to declaring) a static data member");
11478 staticp = 0;
11479 storage_class = sc_none;
11480 }
11481 if (storage_class == sc_register && TREE_STATIC (decl))
11482 {
11483 error ("static member %qD declared %<register%>", decl);
11484 storage_class = sc_none;
11485 }
11486 if (storage_class == sc_extern && pedantic)
11487 {
11488 pedwarn (input_location, OPT_Wpedantic,
11489 "cannot explicitly declare member %q#D to have "
11490 "extern linkage", decl);
11491 storage_class = sc_none;
11492 }
11493 }
11494 else if (constexpr_p && DECL_EXTERNAL (decl))
11495 {
11496 error ("declaration of constexpr variable %qD is not a definition",
11497 decl);
11498 constexpr_p = false;
11499 }
11500 }
11501
11502 if (storage_class == sc_extern && initialized && !funcdef_flag)
11503 {
11504 if (toplevel_bindings_p ())
11505 {
11506 /* It's common practice (and completely valid) to have a const
11507 be initialized and declared extern. */
11508 if (!(type_quals & TYPE_QUAL_CONST))
11509 warning (0, "%qs initialized and declared %<extern%>", name);
11510 }
11511 else
11512 {
11513 error ("%qs has both %<extern%> and initializer", name);
11514 return error_mark_node;
11515 }
11516 }
11517
11518 /* Record `register' declaration for warnings on &
11519 and in case doing stupid register allocation. */
11520
11521 if (storage_class == sc_register)
11522 DECL_REGISTER (decl) = 1;
11523 else if (storage_class == sc_extern)
11524 DECL_THIS_EXTERN (decl) = 1;
11525 else if (storage_class == sc_static)
11526 DECL_THIS_STATIC (decl) = 1;
11527
11528 /* Set constexpr flag on vars (functions got it in grokfndecl). */
11529 if (constexpr_p && VAR_P (decl))
11530 DECL_DECLARED_CONSTEXPR_P (decl) = true;
11531
11532 /* Record constancy and volatility on the DECL itself . There's
11533 no need to do this when processing a template; we'll do this
11534 for the instantiated declaration based on the type of DECL. */
11535 if (!processing_template_decl)
11536 cp_apply_type_quals_to_decl (type_quals, decl);
11537
11538 return decl;
11539 }
11540 }
11541
11542 /* Subroutine of start_function. Ensure that each of the parameter
11543 types (as listed in PARMS) is complete, as is required for a
11544 function definition. */
11545
11546 static void
require_complete_types_for_parms(tree parms)11547 require_complete_types_for_parms (tree parms)
11548 {
11549 for (; parms; parms = DECL_CHAIN (parms))
11550 {
11551 if (dependent_type_p (TREE_TYPE (parms)))
11552 continue;
11553 if (!VOID_TYPE_P (TREE_TYPE (parms))
11554 && complete_type_or_else (TREE_TYPE (parms), parms))
11555 {
11556 relayout_decl (parms);
11557 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11558 }
11559 else
11560 /* grokparms or complete_type_or_else will have already issued
11561 an error. */
11562 TREE_TYPE (parms) = error_mark_node;
11563 }
11564 }
11565
11566 /* Returns nonzero if T is a local variable. */
11567
11568 int
local_variable_p(const_tree t)11569 local_variable_p (const_tree t)
11570 {
11571 if ((VAR_P (t)
11572 /* A VAR_DECL with a context that is a _TYPE is a static data
11573 member. */
11574 && !TYPE_P (CP_DECL_CONTEXT (t))
11575 /* Any other non-local variable must be at namespace scope. */
11576 && !DECL_NAMESPACE_SCOPE_P (t))
11577 || (TREE_CODE (t) == PARM_DECL))
11578 return 1;
11579
11580 return 0;
11581 }
11582
11583 /* Like local_variable_p, but suitable for use as a tree-walking
11584 function. */
11585
11586 static tree
local_variable_p_walkfn(tree * tp,int * walk_subtrees,void *)11587 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
11588 void * /*data*/)
11589 {
11590 if (local_variable_p (*tp)
11591 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier))
11592 return *tp;
11593 else if (TYPE_P (*tp))
11594 *walk_subtrees = 0;
11595
11596 return NULL_TREE;
11597 }
11598
11599 /* Check that ARG, which is a default-argument expression for a
11600 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
11601 something goes wrong. DECL may also be a _TYPE node, rather than a
11602 DECL, if there is no DECL available. */
11603
11604 tree
check_default_argument(tree decl,tree arg,tsubst_flags_t complain)11605 check_default_argument (tree decl, tree arg, tsubst_flags_t complain)
11606 {
11607 tree var;
11608 tree decl_type;
11609
11610 if (TREE_CODE (arg) == DEFAULT_ARG)
11611 /* We get a DEFAULT_ARG when looking at an in-class declaration
11612 with a default argument. Ignore the argument for now; we'll
11613 deal with it after the class is complete. */
11614 return arg;
11615
11616 if (TYPE_P (decl))
11617 {
11618 decl_type = decl;
11619 decl = NULL_TREE;
11620 }
11621 else
11622 decl_type = TREE_TYPE (decl);
11623
11624 if (arg == error_mark_node
11625 || decl == error_mark_node
11626 || TREE_TYPE (arg) == error_mark_node
11627 || decl_type == error_mark_node)
11628 /* Something already went wrong. There's no need to check
11629 further. */
11630 return error_mark_node;
11631
11632 /* [dcl.fct.default]
11633
11634 A default argument expression is implicitly converted to the
11635 parameter type. */
11636 ++cp_unevaluated_operand;
11637 perform_implicit_conversion_flags (decl_type, arg, complain,
11638 LOOKUP_IMPLICIT);
11639 --cp_unevaluated_operand;
11640
11641 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at
11642 the call sites. */
11643 if (TYPE_PTR_OR_PTRMEM_P (decl_type)
11644 && null_ptr_cst_p (arg))
11645 return nullptr_node;
11646
11647 /* [dcl.fct.default]
11648
11649 Local variables shall not be used in default argument
11650 expressions.
11651
11652 The keyword `this' shall not be used in a default argument of a
11653 member function. */
11654 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL);
11655 if (var)
11656 {
11657 if (complain & tf_warning_or_error)
11658 {
11659 if (DECL_NAME (var) == this_identifier)
11660 permerror (input_location, "default argument %qE uses %qD",
11661 arg, var);
11662 else
11663 error ("default argument %qE uses local variable %qD", arg, var);
11664 }
11665 return error_mark_node;
11666 }
11667
11668 /* All is well. */
11669 return arg;
11670 }
11671
11672 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */
11673
11674 static tree
type_is_deprecated(tree type)11675 type_is_deprecated (tree type)
11676 {
11677 enum tree_code code;
11678 if (TREE_DEPRECATED (type))
11679 return type;
11680 if (TYPE_NAME (type))
11681 {
11682 if (TREE_DEPRECATED (TYPE_NAME (type)))
11683 return type;
11684 else
11685 return NULL_TREE;
11686 }
11687
11688 /* Do warn about using typedefs to a deprecated class. */
11689 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type))
11690 return type_is_deprecated (TYPE_MAIN_VARIANT (type));
11691
11692 code = TREE_CODE (type);
11693
11694 if (code == POINTER_TYPE || code == REFERENCE_TYPE
11695 || code == OFFSET_TYPE || code == FUNCTION_TYPE
11696 || code == METHOD_TYPE || code == ARRAY_TYPE)
11697 return type_is_deprecated (TREE_TYPE (type));
11698
11699 if (TYPE_PTRMEMFUNC_P (type))
11700 return type_is_deprecated
11701 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type))));
11702
11703 return NULL_TREE;
11704 }
11705
11706 /* Decode the list of parameter types for a function type.
11707 Given the list of things declared inside the parens,
11708 return a list of types.
11709
11710 If this parameter does not end with an ellipsis, we append
11711 void_list_node.
11712
11713 *PARMS is set to the chain of PARM_DECLs created. */
11714
11715 tree
grokparms(tree parmlist,tree * parms)11716 grokparms (tree parmlist, tree *parms)
11717 {
11718 tree result = NULL_TREE;
11719 tree decls = NULL_TREE;
11720 tree parm;
11721 int any_error = 0;
11722
11723 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm))
11724 {
11725 tree type = NULL_TREE;
11726 tree init = TREE_PURPOSE (parm);
11727 tree decl = TREE_VALUE (parm);
11728 const char *errmsg;
11729
11730 if (parm == void_list_node)
11731 break;
11732
11733 if (! decl || TREE_TYPE (decl) == error_mark_node)
11734 continue;
11735
11736 type = TREE_TYPE (decl);
11737 if (VOID_TYPE_P (type))
11738 {
11739 if (same_type_p (type, void_type_node)
11740 && !init
11741 && !DECL_NAME (decl) && !result
11742 && TREE_CHAIN (parm) == void_list_node)
11743 /* DR 577: A parameter list consisting of a single
11744 unnamed parameter of non-dependent type 'void'. */
11745 break;
11746 else if (cv_qualified_p (type))
11747 error_at (DECL_SOURCE_LOCATION (decl),
11748 "invalid use of cv-qualified type %qT in "
11749 "parameter declaration", type);
11750 else
11751 error_at (DECL_SOURCE_LOCATION (decl),
11752 "invalid use of type %<void%> in parameter "
11753 "declaration");
11754 /* It's not a good idea to actually create parameters of
11755 type `void'; other parts of the compiler assume that a
11756 void type terminates the parameter list. */
11757 type = error_mark_node;
11758 TREE_TYPE (decl) = error_mark_node;
11759 }
11760
11761 if (type != error_mark_node
11762 && TYPE_FOR_JAVA (type)
11763 && MAYBE_CLASS_TYPE_P (type))
11764 {
11765 error ("parameter %qD has Java class type", decl);
11766 type = error_mark_node;
11767 TREE_TYPE (decl) = error_mark_node;
11768 init = NULL_TREE;
11769 }
11770
11771 if (type != error_mark_node
11772 && (errmsg = targetm.invalid_parameter_type (type)))
11773 {
11774 error (errmsg);
11775 type = error_mark_node;
11776 TREE_TYPE (decl) = error_mark_node;
11777 }
11778
11779 if (type != error_mark_node)
11780 {
11781 if (deprecated_state != DEPRECATED_SUPPRESS)
11782 {
11783 tree deptype = type_is_deprecated (type);
11784 if (deptype)
11785 warn_deprecated_use (deptype, NULL_TREE);
11786 }
11787
11788 /* Top-level qualifiers on the parameters are
11789 ignored for function types. */
11790 type = cp_build_qualified_type (type, 0);
11791 if (TREE_CODE (type) == METHOD_TYPE)
11792 {
11793 error ("parameter %qD invalidly declared method type", decl);
11794 type = build_pointer_type (type);
11795 TREE_TYPE (decl) = type;
11796 }
11797 else if (abstract_virtuals_error (decl, type))
11798 any_error = 1; /* Seems like a good idea. */
11799 else if (POINTER_TYPE_P (type))
11800 {
11801 /* [dcl.fct]/6, parameter types cannot contain pointers
11802 (references) to arrays of unknown bound. */
11803 tree t = TREE_TYPE (type);
11804 int ptr = TYPE_PTR_P (type);
11805
11806 while (1)
11807 {
11808 if (TYPE_PTR_P (t))
11809 ptr = 1;
11810 else if (TREE_CODE (t) != ARRAY_TYPE)
11811 break;
11812 else if (!TYPE_DOMAIN (t))
11813 break;
11814 t = TREE_TYPE (t);
11815 }
11816 if (TREE_CODE (t) == ARRAY_TYPE)
11817 error (ptr
11818 ? G_("parameter %qD includes pointer to array of "
11819 "unknown bound %qT")
11820 : G_("parameter %qD includes reference to array of "
11821 "unknown bound %qT"),
11822 decl, t);
11823 }
11824
11825 if (any_error)
11826 init = NULL_TREE;
11827 else if (init && !processing_template_decl)
11828 init = check_default_argument (decl, init, tf_warning_or_error);
11829 }
11830
11831 DECL_CHAIN (decl) = decls;
11832 decls = decl;
11833 result = tree_cons (init, type, result);
11834 }
11835 decls = nreverse (decls);
11836 result = nreverse (result);
11837 if (parm)
11838 result = chainon (result, void_list_node);
11839 *parms = decls;
11840
11841 return result;
11842 }
11843
11844
11845 /* D is a constructor or overloaded `operator='.
11846
11847 Let T be the class in which D is declared. Then, this function
11848 returns:
11849
11850 -1 if D's is an ill-formed constructor or copy assignment operator
11851 whose first parameter is of type `T'.
11852 0 if D is not a copy constructor or copy assignment
11853 operator.
11854 1 if D is a copy constructor or copy assignment operator whose
11855 first parameter is a reference to non-const qualified T.
11856 2 if D is a copy constructor or copy assignment operator whose
11857 first parameter is a reference to const qualified T.
11858
11859 This function can be used as a predicate. Positive values indicate
11860 a copy constructor and nonzero values indicate a copy assignment
11861 operator. */
11862
11863 int
copy_fn_p(const_tree d)11864 copy_fn_p (const_tree d)
11865 {
11866 tree args;
11867 tree arg_type;
11868 int result = 1;
11869
11870 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11871
11872 if (TREE_CODE (d) == TEMPLATE_DECL
11873 || (DECL_TEMPLATE_INFO (d)
11874 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11875 /* Instantiations of template member functions are never copy
11876 functions. Note that member functions of templated classes are
11877 represented as template functions internally, and we must
11878 accept those as copy functions. */
11879 return 0;
11880
11881 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11882 if (!args)
11883 return 0;
11884
11885 arg_type = TREE_VALUE (args);
11886 if (arg_type == error_mark_node)
11887 return 0;
11888
11889 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
11890 {
11891 /* Pass by value copy assignment operator. */
11892 result = -1;
11893 }
11894 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
11895 && !TYPE_REF_IS_RVALUE (arg_type)
11896 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
11897 {
11898 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
11899 result = 2;
11900 }
11901 else
11902 return 0;
11903
11904 args = TREE_CHAIN (args);
11905
11906 if (args && args != void_list_node && !TREE_PURPOSE (args))
11907 /* There are more non-optional args. */
11908 return 0;
11909
11910 return result;
11911 }
11912
11913 /* D is a constructor or overloaded `operator='.
11914
11915 Let T be the class in which D is declared. Then, this function
11916 returns true when D is a move constructor or move assignment
11917 operator, false otherwise. */
11918
11919 bool
move_fn_p(const_tree d)11920 move_fn_p (const_tree d)
11921 {
11922 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
11923
11924 if (cxx_dialect == cxx98)
11925 /* There are no move constructors if we are in C++98 mode. */
11926 return false;
11927
11928 if (TREE_CODE (d) == TEMPLATE_DECL
11929 || (DECL_TEMPLATE_INFO (d)
11930 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
11931 /* Instantiations of template member functions are never move
11932 functions. Note that member functions of templated classes are
11933 represented as template functions internally, and we must
11934 accept those as move functions. */
11935 return 0;
11936
11937 return move_signature_fn_p (d);
11938 }
11939
11940 /* D is a constructor or overloaded `operator='.
11941
11942 Then, this function returns true when D has the same signature as a move
11943 constructor or move assignment operator (because either it is such a
11944 ctor/op= or it is a template specialization with the same signature),
11945 false otherwise. */
11946
11947 bool
move_signature_fn_p(const_tree d)11948 move_signature_fn_p (const_tree d)
11949 {
11950 tree args;
11951 tree arg_type;
11952 bool result = false;
11953
11954 args = FUNCTION_FIRST_USER_PARMTYPE (d);
11955 if (!args)
11956 return 0;
11957
11958 arg_type = TREE_VALUE (args);
11959 if (arg_type == error_mark_node)
11960 return 0;
11961
11962 if (TREE_CODE (arg_type) == REFERENCE_TYPE
11963 && TYPE_REF_IS_RVALUE (arg_type)
11964 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)),
11965 DECL_CONTEXT (d)))
11966 result = true;
11967
11968 args = TREE_CHAIN (args);
11969
11970 if (args && args != void_list_node && !TREE_PURPOSE (args))
11971 /* There are more non-optional args. */
11972 return false;
11973
11974 return result;
11975 }
11976
11977 /* Remember any special properties of member function DECL. */
11978
11979 void
grok_special_member_properties(tree decl)11980 grok_special_member_properties (tree decl)
11981 {
11982 tree class_type;
11983
11984 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
11985 return;
11986
11987 class_type = DECL_CONTEXT (decl);
11988 if (DECL_CONSTRUCTOR_P (decl))
11989 {
11990 int ctor = copy_fn_p (decl);
11991
11992 if (!DECL_ARTIFICIAL (decl))
11993 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1;
11994
11995 if (ctor > 0)
11996 {
11997 /* [class.copy]
11998
11999 A non-template constructor for class X is a copy
12000 constructor if its first parameter is of type X&, const
12001 X&, volatile X& or const volatile X&, and either there
12002 are no other parameters or else all other parameters have
12003 default arguments. */
12004 TYPE_HAS_COPY_CTOR (class_type) = 1;
12005 if (user_provided_p (decl))
12006 TYPE_HAS_COMPLEX_COPY_CTOR (class_type) = 1;
12007 if (ctor > 1)
12008 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1;
12009 }
12010 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12011 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
12012 else if (move_fn_p (decl) && user_provided_p (decl))
12013 TYPE_HAS_COMPLEX_MOVE_CTOR (class_type) = 1;
12014 else if (is_list_ctor (decl))
12015 TYPE_HAS_LIST_CTOR (class_type) = 1;
12016
12017 if (DECL_DECLARED_CONSTEXPR_P (decl)
12018 && !copy_fn_p (decl) && !move_fn_p (decl))
12019 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1;
12020 }
12021 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12022 {
12023 /* [class.copy]
12024
12025 A non-template assignment operator for class X is a copy
12026 assignment operator if its parameter is of type X, X&, const
12027 X&, volatile X& or const volatile X&. */
12028
12029 int assop = copy_fn_p (decl);
12030
12031 if (assop)
12032 {
12033 TYPE_HAS_COPY_ASSIGN (class_type) = 1;
12034 if (user_provided_p (decl))
12035 TYPE_HAS_COMPLEX_COPY_ASSIGN (class_type) = 1;
12036 if (assop != 1)
12037 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1;
12038 }
12039 else if (move_fn_p (decl) && user_provided_p (decl))
12040 TYPE_HAS_COMPLEX_MOVE_ASSIGN (class_type) = 1;
12041 }
12042 /* Destructors are handled in check_methods. */
12043 }
12044
12045 /* Check a constructor DECL has the correct form. Complains
12046 if the class has a constructor of the form X(X). */
12047
12048 int
grok_ctor_properties(const_tree ctype,const_tree decl)12049 grok_ctor_properties (const_tree ctype, const_tree decl)
12050 {
12051 int ctor_parm = copy_fn_p (decl);
12052
12053 if (ctor_parm < 0)
12054 {
12055 /* [class.copy]
12056
12057 A declaration of a constructor for a class X is ill-formed if
12058 its first parameter is of type (optionally cv-qualified) X
12059 and either there are no other parameters or else all other
12060 parameters have default arguments.
12061
12062 We *don't* complain about member template instantiations that
12063 have this form, though; they can occur as we try to decide
12064 what constructor to use during overload resolution. Since
12065 overload resolution will never prefer such a constructor to
12066 the non-template copy constructor (which is either explicitly
12067 or implicitly defined), there's no need to worry about their
12068 existence. Theoretically, they should never even be
12069 instantiated, but that's hard to forestall. */
12070 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
12071 ctype, ctype);
12072 return 0;
12073 }
12074
12075 return 1;
12076 }
12077
12078 /* An operator with this code is unary, but can also be binary. */
12079
12080 static int
ambi_op_p(enum tree_code code)12081 ambi_op_p (enum tree_code code)
12082 {
12083 return (code == INDIRECT_REF
12084 || code == ADDR_EXPR
12085 || code == UNARY_PLUS_EXPR
12086 || code == NEGATE_EXPR
12087 || code == PREINCREMENT_EXPR
12088 || code == PREDECREMENT_EXPR);
12089 }
12090
12091 /* An operator with this name can only be unary. */
12092
12093 static int
unary_op_p(enum tree_code code)12094 unary_op_p (enum tree_code code)
12095 {
12096 return (code == TRUTH_NOT_EXPR
12097 || code == BIT_NOT_EXPR
12098 || code == COMPONENT_REF
12099 || code == TYPE_EXPR);
12100 }
12101
12102 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
12103 errors are issued for invalid declarations. */
12104
12105 bool
grok_op_properties(tree decl,bool complain)12106 grok_op_properties (tree decl, bool complain)
12107 {
12108 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12109 tree argtype;
12110 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12111 tree name = DECL_NAME (decl);
12112 enum tree_code operator_code;
12113 int arity;
12114 bool ellipsis_p;
12115 tree class_type;
12116
12117 /* Count the number of arguments and check for ellipsis. */
12118 for (argtype = argtypes, arity = 0;
12119 argtype && argtype != void_list_node;
12120 argtype = TREE_CHAIN (argtype))
12121 ++arity;
12122 ellipsis_p = !argtype;
12123
12124 class_type = DECL_CONTEXT (decl);
12125 if (class_type && !CLASS_TYPE_P (class_type))
12126 class_type = NULL_TREE;
12127
12128 if (DECL_CONV_FN_P (decl))
12129 operator_code = TYPE_EXPR;
12130 else
12131 do
12132 {
12133 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
12134 if (ansi_opname (CODE) == name) \
12135 { \
12136 operator_code = (CODE); \
12137 break; \
12138 } \
12139 else if (ansi_assopname (CODE) == name) \
12140 { \
12141 operator_code = (CODE); \
12142 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
12143 break; \
12144 }
12145
12146 #include "operators.def"
12147 #undef DEF_OPERATOR
12148
12149 gcc_unreachable ();
12150 }
12151 while (0);
12152 gcc_assert (operator_code != MAX_TREE_CODES);
12153 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12154
12155 if (class_type)
12156 switch (operator_code)
12157 {
12158 case NEW_EXPR:
12159 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
12160 break;
12161
12162 case DELETE_EXPR:
12163 TYPE_GETS_DELETE (class_type) |= 1;
12164 break;
12165
12166 case VEC_NEW_EXPR:
12167 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
12168 break;
12169
12170 case VEC_DELETE_EXPR:
12171 TYPE_GETS_DELETE (class_type) |= 2;
12172 break;
12173
12174 default:
12175 break;
12176 }
12177
12178 /* [basic.std.dynamic.allocation]/1:
12179
12180 A program is ill-formed if an allocation function is declared
12181 in a namespace scope other than global scope or declared static
12182 in global scope.
12183
12184 The same also holds true for deallocation functions. */
12185 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
12186 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12187 {
12188 if (DECL_NAMESPACE_SCOPE_P (decl))
12189 {
12190 if (CP_DECL_CONTEXT (decl) != global_namespace)
12191 {
12192 error ("%qD may not be declared within a namespace", decl);
12193 return false;
12194 }
12195 else if (!TREE_PUBLIC (decl))
12196 {
12197 error ("%qD may not be declared as static", decl);
12198 return false;
12199 }
12200 }
12201 }
12202
12203 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12204 {
12205 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12206 DECL_IS_OPERATOR_NEW (decl) = 1;
12207 }
12208 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12209 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12210 else
12211 {
12212 /* An operator function must either be a non-static member function
12213 or have at least one parameter of a class, a reference to a class,
12214 an enumeration, or a reference to an enumeration. 13.4.0.6 */
12215 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12216 {
12217 if (operator_code == TYPE_EXPR
12218 || operator_code == CALL_EXPR
12219 || operator_code == COMPONENT_REF
12220 || operator_code == ARRAY_REF
12221 || operator_code == NOP_EXPR)
12222 {
12223 error ("%qD must be a nonstatic member function", decl);
12224 return false;
12225 }
12226 else
12227 {
12228 tree p;
12229
12230 if (DECL_STATIC_FUNCTION_P (decl))
12231 {
12232 error ("%qD must be either a non-static member "
12233 "function or a non-member function", decl);
12234 return false;
12235 }
12236
12237 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
12238 {
12239 tree arg = non_reference (TREE_VALUE (p));
12240 if (arg == error_mark_node)
12241 return false;
12242
12243 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used
12244 because these checks are performed even on
12245 template functions. */
12246 if (MAYBE_CLASS_TYPE_P (arg)
12247 || TREE_CODE (arg) == ENUMERAL_TYPE)
12248 break;
12249 }
12250
12251 if (!p || p == void_list_node)
12252 {
12253 if (complain)
12254 error ("%qD must have an argument of class or "
12255 "enumerated type", decl);
12256 return false;
12257 }
12258 }
12259 }
12260
12261 /* There are no restrictions on the arguments to an overloaded
12262 "operator ()". */
12263 if (operator_code == CALL_EXPR)
12264 return true;
12265
12266 /* Warn about conversion operators that will never be used. */
12267 if (IDENTIFIER_TYPENAME_P (name)
12268 && ! DECL_TEMPLATE_INFO (decl)
12269 && warn_conversion
12270 /* Warn only declaring the function; there is no need to
12271 warn again about out-of-class definitions. */
12272 && class_type == current_class_type)
12273 {
12274 tree t = TREE_TYPE (name);
12275 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12276
12277 if (ref)
12278 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12279
12280 if (VOID_TYPE_P (t))
12281 warning (OPT_Wconversion,
12282 ref
12283 ? G_("conversion to a reference to void "
12284 "will never use a type conversion operator")
12285 : G_("conversion to void "
12286 "will never use a type conversion operator"));
12287 else if (class_type)
12288 {
12289 if (t == class_type)
12290 warning (OPT_Wconversion,
12291 ref
12292 ? G_("conversion to a reference to the same type "
12293 "will never use a type conversion operator")
12294 : G_("conversion to the same type "
12295 "will never use a type conversion operator"));
12296 /* Don't force t to be complete here. */
12297 else if (MAYBE_CLASS_TYPE_P (t)
12298 && COMPLETE_TYPE_P (t)
12299 && DERIVED_FROM_P (t, class_type))
12300 warning (OPT_Wconversion,
12301 ref
12302 ? G_("conversion to a reference to a base class "
12303 "will never use a type conversion operator")
12304 : G_("conversion to a base class "
12305 "will never use a type conversion operator"));
12306 }
12307
12308 }
12309
12310 if (operator_code == COND_EXPR)
12311 {
12312 /* 13.4.0.3 */
12313 error ("ISO C++ prohibits overloading operator ?:");
12314 return false;
12315 }
12316 else if (ellipsis_p)
12317 {
12318 error ("%qD must not have variable number of arguments", decl);
12319 return false;
12320 }
12321 else if (ambi_op_p (operator_code))
12322 {
12323 if (arity == 1)
12324 /* We pick the one-argument operator codes by default, so
12325 we don't have to change anything. */
12326 ;
12327 else if (arity == 2)
12328 {
12329 /* If we thought this was a unary operator, we now know
12330 it to be a binary operator. */
12331 switch (operator_code)
12332 {
12333 case INDIRECT_REF:
12334 operator_code = MULT_EXPR;
12335 break;
12336
12337 case ADDR_EXPR:
12338 operator_code = BIT_AND_EXPR;
12339 break;
12340
12341 case UNARY_PLUS_EXPR:
12342 operator_code = PLUS_EXPR;
12343 break;
12344
12345 case NEGATE_EXPR:
12346 operator_code = MINUS_EXPR;
12347 break;
12348
12349 case PREINCREMENT_EXPR:
12350 operator_code = POSTINCREMENT_EXPR;
12351 break;
12352
12353 case PREDECREMENT_EXPR:
12354 operator_code = POSTDECREMENT_EXPR;
12355 break;
12356
12357 default:
12358 gcc_unreachable ();
12359 }
12360
12361 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12362
12363 if ((operator_code == POSTINCREMENT_EXPR
12364 || operator_code == POSTDECREMENT_EXPR)
12365 && ! processing_template_decl
12366 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12367 {
12368 if (methodp)
12369 error ("postfix %qD must take %<int%> as its argument",
12370 decl);
12371 else
12372 error ("postfix %qD must take %<int%> as its second "
12373 "argument", decl);
12374 return false;
12375 }
12376 }
12377 else
12378 {
12379 if (methodp)
12380 error ("%qD must take either zero or one argument", decl);
12381 else
12382 error ("%qD must take either one or two arguments", decl);
12383 return false;
12384 }
12385
12386 /* More Effective C++ rule 6. */
12387 if (warn_ecpp
12388 && (operator_code == POSTINCREMENT_EXPR
12389 || operator_code == POSTDECREMENT_EXPR
12390 || operator_code == PREINCREMENT_EXPR
12391 || operator_code == PREDECREMENT_EXPR))
12392 {
12393 tree arg = TREE_VALUE (argtypes);
12394 tree ret = TREE_TYPE (TREE_TYPE (decl));
12395 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12396 arg = TREE_TYPE (arg);
12397 arg = TYPE_MAIN_VARIANT (arg);
12398 if (operator_code == PREINCREMENT_EXPR
12399 || operator_code == PREDECREMENT_EXPR)
12400 {
12401 if (TREE_CODE (ret) != REFERENCE_TYPE
12402 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12403 arg))
12404 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
12405 build_reference_type (arg));
12406 }
12407 else
12408 {
12409 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12410 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
12411 }
12412 }
12413 }
12414 else if (unary_op_p (operator_code))
12415 {
12416 if (arity != 1)
12417 {
12418 if (methodp)
12419 error ("%qD must take %<void%>", decl);
12420 else
12421 error ("%qD must take exactly one argument", decl);
12422 return false;
12423 }
12424 }
12425 else /* if (binary_op_p (operator_code)) */
12426 {
12427 if (arity != 2)
12428 {
12429 if (methodp)
12430 error ("%qD must take exactly one argument", decl);
12431 else
12432 error ("%qD must take exactly two arguments", decl);
12433 return false;
12434 }
12435
12436 /* More Effective C++ rule 7. */
12437 if (warn_ecpp
12438 && (operator_code == TRUTH_ANDIF_EXPR
12439 || operator_code == TRUTH_ORIF_EXPR
12440 || operator_code == COMPOUND_EXPR))
12441 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
12442 decl);
12443 }
12444
12445 /* Effective C++ rule 23. */
12446 if (warn_ecpp
12447 && arity == 2
12448 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12449 && (operator_code == PLUS_EXPR
12450 || operator_code == MINUS_EXPR
12451 || operator_code == TRUNC_DIV_EXPR
12452 || operator_code == MULT_EXPR
12453 || operator_code == TRUNC_MOD_EXPR)
12454 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12455 warning (OPT_Weffc__, "%qD should return by value", decl);
12456
12457 /* [over.oper]/8 */
12458 for (; argtypes && argtypes != void_list_node;
12459 argtypes = TREE_CHAIN (argtypes))
12460 if (TREE_PURPOSE (argtypes))
12461 {
12462 TREE_PURPOSE (argtypes) = NULL_TREE;
12463 if (operator_code == POSTINCREMENT_EXPR
12464 || operator_code == POSTDECREMENT_EXPR)
12465 {
12466 pedwarn (input_location, OPT_Wpedantic, "%qD cannot have default arguments",
12467 decl);
12468 }
12469 else
12470 {
12471 error ("%qD cannot have default arguments", decl);
12472 return false;
12473 }
12474 }
12475 }
12476 return true;
12477 }
12478
12479 /* Return a string giving the keyword associate with CODE. */
12480
12481 static const char *
tag_name(enum tag_types code)12482 tag_name (enum tag_types code)
12483 {
12484 switch (code)
12485 {
12486 case record_type:
12487 return "struct";
12488 case class_type:
12489 return "class";
12490 case union_type:
12491 return "union";
12492 case enum_type:
12493 return "enum";
12494 case typename_type:
12495 return "typename";
12496 default:
12497 gcc_unreachable ();
12498 }
12499 }
12500
12501 /* Name lookup in an elaborated-type-specifier (after the keyword
12502 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
12503 elaborated-type-specifier is invalid, issue a diagnostic and return
12504 error_mark_node; otherwise, return the *_TYPE to which it referred.
12505 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
12506
12507 tree
check_elaborated_type_specifier(enum tag_types tag_code,tree decl,bool allow_template_p)12508 check_elaborated_type_specifier (enum tag_types tag_code,
12509 tree decl,
12510 bool allow_template_p)
12511 {
12512 tree type;
12513
12514 /* In the case of:
12515
12516 struct S { struct S *p; };
12517
12518 name lookup will find the TYPE_DECL for the implicit "S::S"
12519 typedef. Adjust for that here. */
12520 if (DECL_SELF_REFERENCE_P (decl))
12521 decl = TYPE_NAME (TREE_TYPE (decl));
12522
12523 type = TREE_TYPE (decl);
12524
12525 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
12526 is false for this case as well. */
12527 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12528 {
12529 error ("using template type parameter %qT after %qs",
12530 type, tag_name (tag_code));
12531 return error_mark_node;
12532 }
12533 /* Accept template template parameters. */
12534 else if (allow_template_p
12535 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
12536 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM))
12537 ;
12538 /* [dcl.type.elab]
12539
12540 If the identifier resolves to a typedef-name or the
12541 simple-template-id resolves to an alias template
12542 specialization, the elaborated-type-specifier is ill-formed.
12543
12544 In other words, the only legitimate declaration to use in the
12545 elaborated type specifier is the implicit typedef created when
12546 the type is declared. */
12547 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
12548 && !DECL_SELF_REFERENCE_P (decl)
12549 && tag_code != typename_type)
12550 {
12551 if (alias_template_specialization_p (type))
12552 error ("using alias template specialization %qT after %qs",
12553 type, tag_name (tag_code));
12554 else
12555 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
12556 inform (DECL_SOURCE_LOCATION (decl),
12557 "%qD has a previous declaration here", decl);
12558 return error_mark_node;
12559 }
12560 else if (TREE_CODE (type) != RECORD_TYPE
12561 && TREE_CODE (type) != UNION_TYPE
12562 && tag_code != enum_type
12563 && tag_code != typename_type)
12564 {
12565 error ("%qT referred to as %qs", type, tag_name (tag_code));
12566 inform (input_location, "%q+T has a previous declaration here", type);
12567 return error_mark_node;
12568 }
12569 else if (TREE_CODE (type) != ENUMERAL_TYPE
12570 && tag_code == enum_type)
12571 {
12572 error ("%qT referred to as enum", type);
12573 inform (input_location, "%q+T has a previous declaration here", type);
12574 return error_mark_node;
12575 }
12576 else if (!allow_template_p
12577 && TREE_CODE (type) == RECORD_TYPE
12578 && CLASSTYPE_IS_TEMPLATE (type))
12579 {
12580 /* If a class template appears as elaborated type specifier
12581 without a template header such as:
12582
12583 template <class T> class C {};
12584 void f(class C); // No template header here
12585
12586 then the required template argument is missing. */
12587 error ("template argument required for %<%s %T%>",
12588 tag_name (tag_code),
12589 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
12590 return error_mark_node;
12591 }
12592
12593 return type;
12594 }
12595
12596 /* Lookup NAME in elaborate type specifier in scope according to
12597 SCOPE and issue diagnostics if necessary.
12598 Return *_TYPE node upon success, NULL_TREE when the NAME is not
12599 found, and ERROR_MARK_NODE for type error. */
12600
12601 static tree
lookup_and_check_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)12602 lookup_and_check_tag (enum tag_types tag_code, tree name,
12603 tag_scope scope, bool template_header_p)
12604 {
12605 tree t;
12606 tree decl;
12607 if (scope == ts_global)
12608 {
12609 /* First try ordinary name lookup, ignoring hidden class name
12610 injected via friend declaration. */
12611 decl = lookup_name_prefer_type (name, 2);
12612 decl = strip_using_decl (decl);
12613 /* If that fails, the name will be placed in the smallest
12614 non-class, non-function-prototype scope according to 3.3.1/5.
12615 We may already have a hidden name declared as friend in this
12616 scope. So lookup again but not ignoring hidden names.
12617 If we find one, that name will be made visible rather than
12618 creating a new tag. */
12619 if (!decl)
12620 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
12621 }
12622 else
12623 decl = lookup_type_scope (name, scope);
12624
12625 if (decl
12626 && (DECL_CLASS_TEMPLATE_P (decl)
12627 /* If scope is ts_current we're defining a class, so ignore a
12628 template template parameter. */
12629 || (scope != ts_current
12630 && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))))
12631 decl = DECL_TEMPLATE_RESULT (decl);
12632
12633 if (decl && TREE_CODE (decl) == TYPE_DECL)
12634 {
12635 /* Look for invalid nested type:
12636 class C {
12637 class C {};
12638 }; */
12639 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
12640 {
12641 error ("%qD has the same name as the class in which it is "
12642 "declared",
12643 decl);
12644 return error_mark_node;
12645 }
12646
12647 /* Two cases we need to consider when deciding if a class
12648 template is allowed as an elaborated type specifier:
12649 1. It is a self reference to its own class.
12650 2. It comes with a template header.
12651
12652 For example:
12653
12654 template <class T> class C {
12655 class C *c1; // DECL_SELF_REFERENCE_P is true
12656 class D;
12657 };
12658 template <class U> class C; // template_header_p is true
12659 template <class T> class C<T>::D {
12660 class C *c2; // DECL_SELF_REFERENCE_P is true
12661 }; */
12662
12663 t = check_elaborated_type_specifier (tag_code,
12664 decl,
12665 template_header_p
12666 | DECL_SELF_REFERENCE_P (decl));
12667 if (template_header_p && t && CLASS_TYPE_P (t)
12668 && (!CLASSTYPE_TEMPLATE_INFO (t)
12669 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))))
12670 {
12671 error ("%qT is not a template", t);
12672 inform (location_of (t), "previous declaration here");
12673 if (TYPE_CLASS_SCOPE_P (t)
12674 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t)))
12675 inform (input_location,
12676 "perhaps you want to explicitly add %<%T::%>",
12677 TYPE_CONTEXT (t));
12678 t = error_mark_node;
12679 }
12680
12681 return t;
12682 }
12683 else if (decl && TREE_CODE (decl) == TREE_LIST)
12684 {
12685 error ("reference to %qD is ambiguous", name);
12686 print_candidates (decl);
12687 return error_mark_node;
12688 }
12689 else
12690 return NULL_TREE;
12691 }
12692
12693 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
12694 Define the tag as a forward-reference if it is not defined.
12695
12696 If a declaration is given, process it here, and report an error if
12697 multiple declarations are not identical.
12698
12699 SCOPE is TS_CURRENT when this is also a definition. Only look in
12700 the current frame for the name (since C++ allows new names in any
12701 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
12702 declaration. Only look beginning from the current scope outward up
12703 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
12704
12705 TEMPLATE_HEADER_P is true when this declaration is preceded by
12706 a set of template parameters. */
12707
12708 static tree
xref_tag_1(enum tag_types tag_code,tree name,tag_scope orig_scope,bool template_header_p)12709 xref_tag_1 (enum tag_types tag_code, tree name,
12710 tag_scope orig_scope, bool template_header_p)
12711 {
12712 enum tree_code code;
12713 tree t;
12714 tree context = NULL_TREE;
12715 tag_scope scope;
12716
12717 gcc_assert (identifier_p (name));
12718
12719 switch (tag_code)
12720 {
12721 case record_type:
12722 case class_type:
12723 code = RECORD_TYPE;
12724 break;
12725 case union_type:
12726 code = UNION_TYPE;
12727 break;
12728 case enum_type:
12729 code = ENUMERAL_TYPE;
12730 break;
12731 default:
12732 gcc_unreachable ();
12733 }
12734
12735 if (orig_scope == ts_lambda)
12736 scope = ts_current;
12737 else
12738 scope = orig_scope;
12739
12740 /* In case of anonymous name, xref_tag is only called to
12741 make type node and push name. Name lookup is not required. */
12742 if (anon_aggrname_p (name))
12743 t = NULL_TREE;
12744 else
12745 t = lookup_and_check_tag (tag_code, name,
12746 scope, template_header_p);
12747
12748 if (t == error_mark_node)
12749 return error_mark_node;
12750
12751 if (scope != ts_current && t && current_class_type
12752 && template_class_depth (current_class_type)
12753 && template_header_p)
12754 {
12755 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
12756 return t;
12757
12758 /* Since SCOPE is not TS_CURRENT, we are not looking at a
12759 definition of this tag. Since, in addition, we are currently
12760 processing a (member) template declaration of a template
12761 class, we must be very careful; consider:
12762
12763 template <class X>
12764 struct S1
12765
12766 template <class U>
12767 struct S2
12768 { template <class V>
12769 friend struct S1; };
12770
12771 Here, the S2::S1 declaration should not be confused with the
12772 outer declaration. In particular, the inner version should
12773 have a template parameter of level 2, not level 1. This
12774 would be particularly important if the member declaration
12775 were instead:
12776
12777 template <class V = U> friend struct S1;
12778
12779 say, when we should tsubst into `U' when instantiating
12780 S2. On the other hand, when presented with:
12781
12782 template <class T>
12783 struct S1 {
12784 template <class U>
12785 struct S2 {};
12786 template <class U>
12787 friend struct S2;
12788 };
12789
12790 we must find the inner binding eventually. We
12791 accomplish this by making sure that the new type we
12792 create to represent this declaration has the right
12793 TYPE_CONTEXT. */
12794 context = TYPE_CONTEXT (t);
12795 t = NULL_TREE;
12796 }
12797
12798 if (! t)
12799 {
12800 /* If no such tag is yet defined, create a forward-reference node
12801 and record it as the "definition".
12802 When a real declaration of this type is found,
12803 the forward-reference will be altered into a real type. */
12804 if (code == ENUMERAL_TYPE)
12805 {
12806 error ("use of enum %q#D without previous declaration", name);
12807 return error_mark_node;
12808 }
12809 else
12810 {
12811 t = make_class_type (code);
12812 TYPE_CONTEXT (t) = context;
12813 if (orig_scope == ts_lambda)
12814 /* Remember that we're declaring a lambda to avoid bogus errors
12815 in push_template_decl. */
12816 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node;
12817 t = pushtag (name, t, scope);
12818 }
12819 }
12820 else
12821 {
12822 if (template_header_p && MAYBE_CLASS_TYPE_P (t))
12823 {
12824 /* Check that we aren't trying to overload a class with different
12825 constraints. */
12826 tree constr = NULL_TREE;
12827 if (current_template_parms)
12828 {
12829 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
12830 constr = build_constraints (reqs, NULL_TREE);
12831 }
12832 if (!redeclare_class_template (t, current_template_parms, constr))
12833 return error_mark_node;
12834 }
12835 else if (!processing_template_decl
12836 && CLASS_TYPE_P (t)
12837 && CLASSTYPE_IS_TEMPLATE (t))
12838 {
12839 error ("redeclaration of %qT as a non-template", t);
12840 error ("previous declaration %q+D", t);
12841 return error_mark_node;
12842 }
12843
12844 /* Make injected friend class visible. */
12845 if (scope != ts_within_enclosing_non_class
12846 && hidden_name_p (TYPE_NAME (t)))
12847 {
12848 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
12849 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
12850
12851 if (TYPE_TEMPLATE_INFO (t))
12852 {
12853 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
12854 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
12855 }
12856 }
12857 }
12858
12859 return t;
12860 }
12861
12862 /* Wrapper for xref_tag_1. */
12863
12864 tree
xref_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)12865 xref_tag (enum tag_types tag_code, tree name,
12866 tag_scope scope, bool template_header_p)
12867 {
12868 tree ret;
12869 bool subtime;
12870 subtime = timevar_cond_start (TV_NAME_LOOKUP);
12871 ret = xref_tag_1 (tag_code, name, scope, template_header_p);
12872 timevar_cond_stop (TV_NAME_LOOKUP, subtime);
12873 return ret;
12874 }
12875
12876
12877 tree
xref_tag_from_type(tree old,tree id,tag_scope scope)12878 xref_tag_from_type (tree old, tree id, tag_scope scope)
12879 {
12880 enum tag_types tag_kind;
12881
12882 if (TREE_CODE (old) == RECORD_TYPE)
12883 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12884 else
12885 tag_kind = union_type;
12886
12887 if (id == NULL_TREE)
12888 id = TYPE_IDENTIFIER (old);
12889
12890 return xref_tag (tag_kind, id, scope, false);
12891 }
12892
12893 /* Create the binfo hierarchy for REF with (possibly NULL) base list
12894 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
12895 access_* node, and the TREE_VALUE is the type of the base-class.
12896 Non-NULL TREE_TYPE indicates virtual inheritance.
12897
12898 Returns true if the binfo hierarchy was successfully created,
12899 false if an error was detected. */
12900
12901 bool
xref_basetypes(tree ref,tree base_list)12902 xref_basetypes (tree ref, tree base_list)
12903 {
12904 tree *basep;
12905 tree binfo, base_binfo;
12906 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
12907 unsigned max_bases = 0; /* Maximum direct bases. */
12908 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */
12909 int i;
12910 tree default_access;
12911 tree igo_prev; /* Track Inheritance Graph Order. */
12912
12913 if (ref == error_mark_node)
12914 return false;
12915
12916 /* The base of a derived class is private by default, all others are
12917 public. */
12918 default_access = (TREE_CODE (ref) == RECORD_TYPE
12919 && CLASSTYPE_DECLARED_CLASS (ref)
12920 ? access_private_node : access_public_node);
12921
12922 /* First, make sure that any templates in base-classes are
12923 instantiated. This ensures that if we call ourselves recursively
12924 we do not get confused about which classes are marked and which
12925 are not. */
12926 basep = &base_list;
12927 while (*basep)
12928 {
12929 tree basetype = TREE_VALUE (*basep);
12930
12931 /* The dependent_type_p call below should really be dependent_scope_p
12932 so that we give a hard error about using an incomplete type as a
12933 base, but we allow it with a pedwarn for backward
12934 compatibility. */
12935 if (processing_template_decl
12936 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype))
12937 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN);
12938 if (!dependent_type_p (basetype)
12939 && !complete_type_or_else (basetype, NULL))
12940 /* An incomplete type. Remove it from the list. */
12941 *basep = TREE_CHAIN (*basep);
12942 else
12943 {
12944 max_bases++;
12945 if (TREE_TYPE (*basep))
12946 max_dvbases++;
12947 if (CLASS_TYPE_P (basetype))
12948 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype));
12949 basep = &TREE_CHAIN (*basep);
12950 }
12951 }
12952 max_vbases += max_dvbases;
12953
12954 TYPE_MARKED_P (ref) = 1;
12955
12956 /* The binfo slot should be empty, unless this is an (ill-formed)
12957 redefinition. */
12958 if (TYPE_BINFO (ref) && !TYPE_SIZE (ref))
12959 {
12960 error ("redefinition of %q#T", ref);
12961 return false;
12962 }
12963
12964 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
12965
12966 binfo = make_tree_binfo (max_bases);
12967
12968 TYPE_BINFO (ref) = binfo;
12969 BINFO_OFFSET (binfo) = size_zero_node;
12970 BINFO_TYPE (binfo) = ref;
12971
12972 /* Apply base-class info set up to the variants of this type. */
12973 fixup_type_variants (ref);
12974
12975 if (max_bases)
12976 {
12977 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases);
12978 /* An aggregate cannot have baseclasses. */
12979 CLASSTYPE_NON_AGGREGATE (ref) = 1;
12980
12981 if (TREE_CODE (ref) == UNION_TYPE)
12982 {
12983 error ("derived union %qT invalid", ref);
12984 return false;
12985 }
12986 }
12987
12988 if (max_bases > 1)
12989 {
12990 if (TYPE_FOR_JAVA (ref))
12991 {
12992 error ("Java class %qT cannot have multiple bases", ref);
12993 return false;
12994 }
12995 else
12996 warning (OPT_Wmultiple_inheritance,
12997 "%qT defined with multiple direct bases", ref);
12998 }
12999
13000 if (max_vbases)
13001 {
13002 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases);
13003
13004 if (TYPE_FOR_JAVA (ref))
13005 {
13006 error ("Java class %qT cannot have virtual bases", ref);
13007 return false;
13008 }
13009 else if (max_dvbases)
13010 warning (OPT_Wvirtual_inheritance,
13011 "%qT defined with direct virtual base", ref);
13012 }
13013
13014 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
13015 {
13016 tree access = TREE_PURPOSE (base_list);
13017 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
13018 tree basetype = TREE_VALUE (base_list);
13019
13020 if (access == access_default_node)
13021 access = default_access;
13022
13023 if (PACK_EXPANSION_P (basetype))
13024 basetype = PACK_EXPANSION_PATTERN (basetype);
13025 if (TREE_CODE (basetype) == TYPE_DECL)
13026 basetype = TREE_TYPE (basetype);
13027 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE)
13028 {
13029 error ("base type %qT fails to be a struct or class type",
13030 basetype);
13031 return false;
13032 }
13033
13034 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
13035 TYPE_FOR_JAVA (ref) = 1;
13036
13037 base_binfo = NULL_TREE;
13038 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype))
13039 {
13040 base_binfo = TYPE_BINFO (basetype);
13041 /* The original basetype could have been a typedef'd type. */
13042 basetype = BINFO_TYPE (base_binfo);
13043
13044 /* Inherit flags from the base. */
13045 TYPE_HAS_NEW_OPERATOR (ref)
13046 |= TYPE_HAS_NEW_OPERATOR (basetype);
13047 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13048 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13049 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13050 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
13051 CLASSTYPE_DIAMOND_SHAPED_P (ref)
13052 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
13053 CLASSTYPE_REPEATED_BASE_P (ref)
13054 |= CLASSTYPE_REPEATED_BASE_P (basetype);
13055 }
13056
13057 /* We must do this test after we've seen through a typedef
13058 type. */
13059 if (TYPE_MARKED_P (basetype))
13060 {
13061 if (basetype == ref)
13062 error ("recursive type %qT undefined", basetype);
13063 else
13064 error ("duplicate base type %qT invalid", basetype);
13065 return false;
13066 }
13067
13068 if (PACK_EXPANSION_P (TREE_VALUE (base_list)))
13069 /* Regenerate the pack expansion for the bases. */
13070 basetype = make_pack_expansion (basetype);
13071
13072 TYPE_MARKED_P (basetype) = 1;
13073
13074 base_binfo = copy_binfo (base_binfo, basetype, ref,
13075 &igo_prev, via_virtual);
13076 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
13077 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
13078
13079 BINFO_BASE_APPEND (binfo, base_binfo);
13080 BINFO_BASE_ACCESS_APPEND (binfo, access);
13081 }
13082
13083 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases)
13084 /* If we didn't get max_vbases vbases, we must have shared at
13085 least one of them, and are therefore diamond shaped. */
13086 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
13087
13088 /* Unmark all the types. */
13089 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
13090 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13091 TYPE_MARKED_P (ref) = 0;
13092
13093 /* Now see if we have a repeated base type. */
13094 if (!CLASSTYPE_REPEATED_BASE_P (ref))
13095 {
13096 for (base_binfo = binfo; base_binfo;
13097 base_binfo = TREE_CHAIN (base_binfo))
13098 {
13099 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13100 {
13101 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
13102 break;
13103 }
13104 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
13105 }
13106 for (base_binfo = binfo; base_binfo;
13107 base_binfo = TREE_CHAIN (base_binfo))
13108 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
13109 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
13110 else
13111 break;
13112 }
13113
13114 return true;
13115 }
13116
13117
13118 /* Copies the enum-related properties from type SRC to type DST.
13119 Used with the underlying type of an enum and the enum itself. */
13120 static void
copy_type_enum(tree dst,tree src)13121 copy_type_enum (tree dst, tree src)
13122 {
13123 tree t;
13124 for (t = dst; t; t = TYPE_NEXT_VARIANT (t))
13125 {
13126 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src);
13127 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src);
13128 TYPE_SIZE (t) = TYPE_SIZE (src);
13129 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src);
13130 SET_TYPE_MODE (dst, TYPE_MODE (src));
13131 TYPE_PRECISION (t) = TYPE_PRECISION (src);
13132 unsigned valign = TYPE_ALIGN (src);
13133 if (TYPE_USER_ALIGN (t))
13134 valign = MAX (valign, TYPE_ALIGN (t));
13135 else
13136 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src);
13137 TYPE_ALIGN (t) = valign;
13138 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src);
13139 }
13140 }
13141
13142 /* Begin compiling the definition of an enumeration type.
13143 NAME is its name,
13144
13145 if ENUMTYPE is not NULL_TREE then the type has alredy been found.
13146
13147 UNDERLYING_TYPE is the type that will be used as the storage for
13148 the enumeration type. This should be NULL_TREE if no storage type
13149 was specified.
13150
13151 ATTRIBUTES are any attributes specified after the enum-key.
13152
13153 SCOPED_ENUM_P is true if this is a scoped enumeration type.
13154
13155 if IS_NEW is not NULL, gets TRUE iff a new type is created.
13156
13157 Returns the type object, as yet incomplete.
13158 Also records info about it so that build_enumerator
13159 may be used to declare the individual values as they are read. */
13160
13161 tree
start_enum(tree name,tree enumtype,tree underlying_type,tree attributes,bool scoped_enum_p,bool * is_new)13162 start_enum (tree name, tree enumtype, tree underlying_type,
13163 tree attributes, bool scoped_enum_p, bool *is_new)
13164 {
13165 tree prevtype = NULL_TREE;
13166 gcc_assert (identifier_p (name));
13167
13168 if (is_new)
13169 *is_new = false;
13170 /* [C++0x dcl.enum]p5:
13171
13172 If not explicitly specified, the underlying type of a scoped
13173 enumeration type is int. */
13174 if (!underlying_type && scoped_enum_p)
13175 underlying_type = integer_type_node;
13176
13177 if (underlying_type)
13178 underlying_type = cv_unqualified (underlying_type);
13179
13180 /* If this is the real definition for a previous forward reference,
13181 fill in the contents in the same object that used to be the
13182 forward reference. */
13183 if (!enumtype)
13184 enumtype = lookup_and_check_tag (enum_type, name,
13185 /*tag_scope=*/ts_current,
13186 /*template_header_p=*/false);
13187
13188 /* In case of a template_decl, the only check that should be deferred
13189 to instantiation time is the comparison of underlying types. */
13190 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13191 {
13192 if (scoped_enum_p != SCOPED_ENUM_P (enumtype))
13193 {
13194 error_at (input_location, "scoped/unscoped mismatch "
13195 "in enum %q#T", enumtype);
13196 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13197 "previous definition here");
13198 enumtype = error_mark_node;
13199 }
13200 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type)
13201 {
13202 error_at (input_location, "underlying type mismatch "
13203 "in enum %q#T", enumtype);
13204 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13205 "previous definition here");
13206 enumtype = error_mark_node;
13207 }
13208 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype)
13209 && !dependent_type_p (underlying_type)
13210 && !dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))
13211 && !same_type_p (underlying_type,
13212 ENUM_UNDERLYING_TYPE (enumtype)))
13213 {
13214 error_at (input_location, "different underlying type "
13215 "in enum %q#T", enumtype);
13216 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)),
13217 "previous definition here");
13218 underlying_type = NULL_TREE;
13219 }
13220 }
13221
13222 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE
13223 || processing_template_decl)
13224 {
13225 /* In case of error, make a dummy enum to allow parsing to
13226 continue. */
13227 if (enumtype == error_mark_node)
13228 {
13229 name = make_anon_name ();
13230 enumtype = NULL_TREE;
13231 }
13232
13233 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition
13234 of an opaque enum, or an opaque enum of an already defined
13235 enumeration (C++0x only).
13236 In any other case, it'll be NULL_TREE. */
13237 if (!enumtype)
13238 {
13239 if (is_new)
13240 *is_new = true;
13241 }
13242 prevtype = enumtype;
13243
13244 /* Do not push the decl more than once, unless we need to
13245 compare underlying types at instantiation time */
13246 if (!enumtype
13247 || TREE_CODE (enumtype) != ENUMERAL_TYPE
13248 || (underlying_type
13249 && dependent_type_p (underlying_type))
13250 || (ENUM_UNDERLYING_TYPE (enumtype)
13251 && dependent_type_p (ENUM_UNDERLYING_TYPE (enumtype))))
13252 {
13253 enumtype = cxx_make_type (ENUMERAL_TYPE);
13254 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
13255 }
13256 else
13257 enumtype = xref_tag (enum_type, name, /*tag_scope=*/ts_current,
13258 false);
13259
13260 if (enumtype == error_mark_node)
13261 return error_mark_node;
13262
13263 /* The enum is considered opaque until the opening '{' of the
13264 enumerator list. */
13265 SET_OPAQUE_ENUM_P (enumtype, true);
13266 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type;
13267 }
13268
13269 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p);
13270
13271 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
13272
13273 if (underlying_type)
13274 {
13275 if (ENUM_UNDERLYING_TYPE (enumtype))
13276 /* We already checked that it matches, don't change it to a different
13277 typedef variant. */;
13278 else if (CP_INTEGRAL_TYPE_P (underlying_type))
13279 {
13280 copy_type_enum (enumtype, underlying_type);
13281 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13282 }
13283 else if (dependent_type_p (underlying_type))
13284 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type;
13285 else
13286 error ("underlying type %<%T%> of %<%T%> must be an integral type",
13287 underlying_type, enumtype);
13288 }
13289
13290 /* If into a template class, the returned enum is always the first
13291 declaration (opaque or not) seen. This way all the references to
13292 this type will be to the same declaration. The following ones are used
13293 only to check for definition errors. */
13294 if (prevtype && processing_template_decl)
13295 return prevtype;
13296 else
13297 return enumtype;
13298 }
13299
13300 /* After processing and defining all the values of an enumeration type,
13301 install their decls in the enumeration type.
13302 ENUMTYPE is the type object. */
13303
13304 void
finish_enum_value_list(tree enumtype)13305 finish_enum_value_list (tree enumtype)
13306 {
13307 tree values;
13308 tree underlying_type;
13309 tree decl;
13310 tree value;
13311 tree minnode, maxnode;
13312 tree t;
13313
13314 bool fixed_underlying_type_p
13315 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE;
13316
13317 /* We built up the VALUES in reverse order. */
13318 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13319
13320 /* For an enum defined in a template, just set the type of the values;
13321 all further processing is postponed until the template is
13322 instantiated. We need to set the type so that tsubst of a CONST_DECL
13323 works. */
13324 if (processing_template_decl)
13325 {
13326 for (values = TYPE_VALUES (enumtype);
13327 values;
13328 values = TREE_CHAIN (values))
13329 TREE_TYPE (TREE_VALUE (values)) = enumtype;
13330 return;
13331 }
13332
13333 /* Determine the minimum and maximum values of the enumerators. */
13334 if (TYPE_VALUES (enumtype))
13335 {
13336 minnode = maxnode = NULL_TREE;
13337
13338 for (values = TYPE_VALUES (enumtype);
13339 values;
13340 values = TREE_CHAIN (values))
13341 {
13342 decl = TREE_VALUE (values);
13343
13344 /* [dcl.enum]: Following the closing brace of an enum-specifier,
13345 each enumerator has the type of its enumeration. Prior to the
13346 closing brace, the type of each enumerator is the type of its
13347 initializing value. */
13348 TREE_TYPE (decl) = enumtype;
13349
13350 /* Update the minimum and maximum values, if appropriate. */
13351 value = DECL_INITIAL (decl);
13352 if (value == error_mark_node)
13353 value = integer_zero_node;
13354 /* Figure out what the minimum and maximum values of the
13355 enumerators are. */
13356 if (!minnode)
13357 minnode = maxnode = value;
13358 else if (tree_int_cst_lt (maxnode, value))
13359 maxnode = value;
13360 else if (tree_int_cst_lt (value, minnode))
13361 minnode = value;
13362 }
13363 }
13364 else
13365 /* [dcl.enum]
13366
13367 If the enumerator-list is empty, the underlying type is as if
13368 the enumeration had a single enumerator with value 0. */
13369 minnode = maxnode = integer_zero_node;
13370
13371 if (!fixed_underlying_type_p)
13372 {
13373 /* Compute the number of bits require to represent all values of the
13374 enumeration. We must do this before the type of MINNODE and
13375 MAXNODE are transformed, since tree_int_cst_min_precision relies
13376 on the TREE_TYPE of the value it is passed. */
13377 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED;
13378 int lowprec = tree_int_cst_min_precision (minnode, sgn);
13379 int highprec = tree_int_cst_min_precision (maxnode, sgn);
13380 int precision = MAX (lowprec, highprec);
13381 unsigned int itk;
13382 bool use_short_enum;
13383
13384 /* Determine the underlying type of the enumeration.
13385
13386 [dcl.enum]
13387
13388 The underlying type of an enumeration is an integral type that
13389 can represent all the enumerator values defined in the
13390 enumeration. It is implementation-defined which integral type is
13391 used as the underlying type for an enumeration except that the
13392 underlying type shall not be larger than int unless the value of
13393 an enumerator cannot fit in an int or unsigned int.
13394
13395 We use "int" or an "unsigned int" as the underlying type, even if
13396 a smaller integral type would work, unless the user has
13397 explicitly requested that we use the smallest possible type. The
13398 user can request that for all enumerations with a command line
13399 flag, or for just one enumeration with an attribute. */
13400
13401 use_short_enum = flag_short_enums
13402 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
13403
13404 /* If the precision of the type was specified with an attribute and it
13405 was too small, give an error. Otherwise, use it. */
13406 if (TYPE_PRECISION (enumtype))
13407 {
13408 if (precision > TYPE_PRECISION (enumtype))
13409 error ("specified mode too small for enumeral values");
13410 else
13411 {
13412 use_short_enum = true;
13413 precision = TYPE_PRECISION (enumtype);
13414 }
13415 }
13416
13417 for (itk = (use_short_enum ? itk_char : itk_int);
13418 itk != itk_none;
13419 itk++)
13420 {
13421 underlying_type = integer_types[itk];
13422 if (underlying_type != NULL_TREE
13423 && TYPE_PRECISION (underlying_type) >= precision
13424 && TYPE_SIGN (underlying_type) == sgn)
13425 break;
13426 }
13427 if (itk == itk_none)
13428 {
13429 /* DR 377
13430
13431 IF no integral type can represent all the enumerator values, the
13432 enumeration is ill-formed. */
13433 error ("no integral type can represent all of the enumerator values "
13434 "for %qT", enumtype);
13435 precision = TYPE_PRECISION (long_long_integer_type_node);
13436 underlying_type = integer_types[itk_unsigned_long_long];
13437 }
13438
13439 /* [dcl.enum]
13440
13441 The value of sizeof() applied to an enumeration type, an object
13442 of an enumeration type, or an enumerator, is the value of sizeof()
13443 applied to the underlying type. */
13444 copy_type_enum (enumtype, underlying_type);
13445
13446 /* Compute the minimum and maximum values for the type.
13447
13448 [dcl.enum]
13449
13450 For an enumeration where emin is the smallest enumerator and emax
13451 is the largest, the values of the enumeration are the values of the
13452 underlying type in the range bmin to bmax, where bmin and bmax are,
13453 respectively, the smallest and largest values of the smallest bit-
13454 field that can store emin and emax. */
13455
13456 /* The middle-end currently assumes that types with TYPE_PRECISION
13457 narrower than their underlying type are suitably zero or sign
13458 extended to fill their mode. Similarly, it assumes that the front
13459 end assures that a value of a particular type must be within
13460 TYPE_MIN_VALUE and TYPE_MAX_VALUE.
13461
13462 We used to set these fields based on bmin and bmax, but that led
13463 to invalid assumptions like optimizing away bounds checking. So
13464 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and
13465 TYPE_MAX_VALUE to the values for the mode above and only restrict
13466 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */
13467 ENUM_UNDERLYING_TYPE (enumtype)
13468 = build_distinct_type_copy (underlying_type);
13469 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision;
13470 set_min_and_max_values_for_integral_type
13471 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn);
13472
13473 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */
13474 if (flag_strict_enums)
13475 set_min_and_max_values_for_integral_type (enumtype, precision, sgn);
13476 }
13477 else
13478 underlying_type = ENUM_UNDERLYING_TYPE (enumtype);
13479
13480 /* Convert each of the enumerators to the type of the underlying
13481 type of the enumeration. */
13482 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13483 {
13484 location_t saved_location;
13485
13486 decl = TREE_VALUE (values);
13487 saved_location = input_location;
13488 input_location = DECL_SOURCE_LOCATION (decl);
13489 if (fixed_underlying_type_p)
13490 /* If the enumeration type has a fixed underlying type, we
13491 already checked all of the enumerator values. */
13492 value = DECL_INITIAL (decl);
13493 else
13494 value = perform_implicit_conversion (underlying_type,
13495 DECL_INITIAL (decl),
13496 tf_warning_or_error);
13497 input_location = saved_location;
13498
13499 /* Do not clobber shared ints. */
13500 if (value != error_mark_node)
13501 {
13502 value = copy_node (value);
13503
13504 TREE_TYPE (value) = enumtype;
13505 }
13506 DECL_INITIAL (decl) = value;
13507 }
13508
13509 /* Fix up all variant types of this enum type. */
13510 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13511 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13512
13513 if (at_class_scope_p ()
13514 && COMPLETE_TYPE_P (current_class_type)
13515 && UNSCOPED_ENUM_P (enumtype))
13516 {
13517 insert_late_enum_def_into_classtype_sorted_fields (enumtype,
13518 current_class_type);
13519 fixup_type_variants (current_class_type);
13520 }
13521
13522 /* Finish debugging output for this type. */
13523 rest_of_type_compilation (enumtype, namespace_bindings_p ());
13524
13525 /* Each enumerator now has the type of its enumeration. Clear the cache
13526 so that this change in types doesn't confuse us later on. */
13527 clear_cv_and_fold_caches ();
13528 }
13529
13530 /* Finishes the enum type. This is called only the first time an
13531 enumeration is seen, be it opaque or odinary.
13532 ENUMTYPE is the type object. */
13533
13534 void
finish_enum(tree enumtype)13535 finish_enum (tree enumtype)
13536 {
13537 if (processing_template_decl)
13538 {
13539 if (at_function_scope_p ())
13540 add_stmt (build_min (TAG_DEFN, enumtype));
13541 return;
13542 }
13543
13544 /* If this is a forward declaration, there should not be any variants,
13545 though we can get a variant in the middle of an enum-specifier with
13546 wacky code like 'enum E { e = sizeof(const E*) };' */
13547 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype)
13548 && (TYPE_VALUES (enumtype)
13549 || !TYPE_NEXT_VARIANT (enumtype)));
13550 }
13551
13552 /* Build and install a CONST_DECL for an enumeration constant of the
13553 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13554 Apply ATTRIBUTES if available. LOC is the location of NAME.
13555 Assignment of sequential values by default is handled here. */
13556
13557 void
build_enumerator(tree name,tree value,tree enumtype,tree attributes,location_t loc)13558 build_enumerator (tree name, tree value, tree enumtype, tree attributes,
13559 location_t loc)
13560 {
13561 tree decl;
13562 tree context;
13563 tree type;
13564
13565 /* scalar_constant_value will pull out this expression, so make sure
13566 it's folded as appropriate. */
13567 if (processing_template_decl)
13568 value = fold_non_dependent_expr (value);
13569
13570 /* If the VALUE was erroneous, pretend it wasn't there; that will
13571 result in the enum being assigned the next value in sequence. */
13572 if (value == error_mark_node)
13573 value = NULL_TREE;
13574
13575 /* Remove no-op casts from the value. */
13576 if (value)
13577 STRIP_TYPE_NOPS (value);
13578
13579 if (! processing_template_decl)
13580 {
13581 /* Validate and default VALUE. */
13582 if (value != NULL_TREE)
13583 {
13584 if (!ENUM_UNDERLYING_TYPE (enumtype))
13585 {
13586 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM,
13587 value, true);
13588 if (tmp_value)
13589 value = tmp_value;
13590 }
13591 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P
13592 (TREE_TYPE (value)))
13593 value = perform_implicit_conversion_flags
13594 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error,
13595 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING);
13596
13597 if (value == error_mark_node)
13598 value = NULL_TREE;
13599
13600 if (value != NULL_TREE)
13601 {
13602 value = cxx_constant_value (value);
13603
13604 if (TREE_CODE (value) != INTEGER_CST
13605 || ! INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (value)))
13606 {
13607 error ("enumerator value for %qD is not an integer constant",
13608 name);
13609 value = NULL_TREE;
13610 }
13611 }
13612 }
13613
13614 /* Default based on previous value. */
13615 if (value == NULL_TREE)
13616 {
13617 if (TYPE_VALUES (enumtype))
13618 {
13619 tree prev_value;
13620 bool overflowed;
13621
13622 /* C++03 7.2/4: If no initializer is specified for the first
13623 enumerator, the type is an unspecified integral
13624 type. Otherwise the type is the same as the type of the
13625 initializing value of the preceding enumerator unless the
13626 incremented value is not representable in that type, in
13627 which case the type is an unspecified integral type
13628 sufficient to contain the incremented value. */
13629 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13630 if (error_operand_p (prev_value))
13631 value = error_mark_node;
13632 else
13633 {
13634 tree type = TREE_TYPE (prev_value);
13635 signop sgn = TYPE_SIGN (type);
13636 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn,
13637 &overflowed);
13638 if (!overflowed)
13639 {
13640 bool pos = !wi::neg_p (wi, sgn);
13641 if (!wi::fits_to_tree_p (wi, type))
13642 {
13643 unsigned int itk;
13644 for (itk = itk_int; itk != itk_none; itk++)
13645 {
13646 type = integer_types[itk];
13647 if (type != NULL_TREE
13648 && (pos || !TYPE_UNSIGNED (type))
13649 && wi::fits_to_tree_p (wi, type))
13650 break;
13651 }
13652 if (type && cxx_dialect < cxx11
13653 && itk > itk_unsigned_long)
13654 pedwarn (input_location, OPT_Wlong_long, pos ? "\
13655 incremented enumerator value is too large for %<unsigned long%>" : "\
13656 incremented enumerator value is too large for %<long%>");
13657 }
13658 if (type == NULL_TREE)
13659 overflowed = true;
13660 else
13661 value = wide_int_to_tree (type, wi);
13662 }
13663
13664 if (overflowed)
13665 {
13666 error ("overflow in enumeration values at %qD", name);
13667 value = error_mark_node;
13668 }
13669 }
13670 }
13671 else
13672 value = integer_zero_node;
13673 }
13674
13675 /* Remove no-op casts from the value. */
13676 STRIP_TYPE_NOPS (value);
13677
13678 /* If the underlying type of the enum is fixed, check whether
13679 the enumerator values fits in the underlying type. If it
13680 does not fit, the program is ill-formed [C++0x dcl.enum]. */
13681 if (ENUM_UNDERLYING_TYPE (enumtype)
13682 && value
13683 && TREE_CODE (value) == INTEGER_CST)
13684 {
13685 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype)))
13686 error ("enumerator value %E is outside the range of underlying "
13687 "type %<%T%>", value, ENUM_UNDERLYING_TYPE (enumtype));
13688
13689 /* Convert the value to the appropriate type. */
13690 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value);
13691 }
13692 }
13693
13694 /* C++ associates enums with global, function, or class declarations. */
13695 context = current_scope ();
13696
13697 /* Build the actual enumeration constant. Note that the enumeration
13698 constants have the underlying type of the enum (if it is fixed)
13699 or the type of their initializer (if the underlying type of the
13700 enum is not fixed):
13701
13702 [ C++0x dcl.enum ]
13703
13704 If the underlying type is fixed, the type of each enumerator
13705 prior to the closing brace is the underlying type; if the
13706 initializing value of an enumerator cannot be represented by
13707 the underlying type, the program is ill-formed. If the
13708 underlying type is not fixed, the type of each enumerator is
13709 the type of its initializing value.
13710
13711 If the underlying type is not fixed, it will be computed by
13712 finish_enum and we will reset the type of this enumerator. Of
13713 course, if we're processing a template, there may be no value. */
13714 type = value ? TREE_TYPE (value) : NULL_TREE;
13715
13716 decl = build_decl (loc, CONST_DECL, name, type);
13717
13718 DECL_CONTEXT (decl) = enumtype;
13719 TREE_CONSTANT (decl) = 1;
13720 TREE_READONLY (decl) = 1;
13721 DECL_INITIAL (decl) = value;
13722
13723 if (attributes)
13724 cplus_decl_attributes (&decl, attributes, 0);
13725
13726 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype))
13727 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13728 on the TYPE_FIELDS list for `S'. (That's so that you can say
13729 things like `S::i' later.) */
13730 finish_member_declaration (decl);
13731 else
13732 pushdecl (decl);
13733
13734 /* Add this enumeration constant to the list for this type. */
13735 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13736 }
13737
13738 /* Look for an enumerator with the given NAME within the enumeration
13739 type ENUMTYPE. This routine is used primarily for qualified name
13740 lookup into an enumerator in C++0x, e.g.,
13741
13742 enum class Color { Red, Green, Blue };
13743
13744 Color color = Color::Red;
13745
13746 Returns the value corresponding to the enumerator, or
13747 NULL_TREE if no such enumerator was found. */
13748 tree
lookup_enumerator(tree enumtype,tree name)13749 lookup_enumerator (tree enumtype, tree name)
13750 {
13751 tree e;
13752 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE);
13753
13754 e = purpose_member (name, TYPE_VALUES (enumtype));
13755 return e? TREE_VALUE (e) : NULL_TREE;
13756 }
13757
13758
13759 /* We're defining DECL. Make sure that its type is OK. */
13760
13761 static void
check_function_type(tree decl,tree current_function_parms)13762 check_function_type (tree decl, tree current_function_parms)
13763 {
13764 tree fntype = TREE_TYPE (decl);
13765 tree return_type = complete_type (TREE_TYPE (fntype));
13766
13767 /* In a function definition, arg types must be complete. */
13768 require_complete_types_for_parms (current_function_parms);
13769
13770 if (dependent_type_p (return_type)
13771 || type_uses_auto (return_type))
13772 return;
13773 if (!COMPLETE_OR_VOID_TYPE_P (return_type)
13774 || (TYPE_FOR_JAVA (return_type) && MAYBE_CLASS_TYPE_P (return_type)))
13775 {
13776 tree args = TYPE_ARG_TYPES (fntype);
13777
13778 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13779 error ("return type %q#T is incomplete", return_type);
13780 else
13781 error ("return type has Java class type %q#T", return_type);
13782
13783 /* Make it return void instead. */
13784 if (TREE_CODE (fntype) == METHOD_TYPE)
13785 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
13786 void_type_node,
13787 TREE_CHAIN (args));
13788 else
13789 fntype = build_function_type (void_type_node, args);
13790 fntype
13791 = build_exception_variant (fntype,
13792 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
13793 fntype = (cp_build_type_attribute_variant
13794 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl))));
13795 TREE_TYPE (decl) = fntype;
13796 }
13797 else
13798 abstract_virtuals_error (decl, TREE_TYPE (fntype));
13799 }
13800
13801 /* True iff FN is an implicitly-defined default constructor. */
13802
13803 static bool
implicit_default_ctor_p(tree fn)13804 implicit_default_ctor_p (tree fn)
13805 {
13806 return (DECL_CONSTRUCTOR_P (fn)
13807 && !user_provided_p (fn)
13808 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn)));
13809 }
13810
13811 /* Clobber the contents of *this to let the back end know that the object
13812 storage is dead when we enter the constructor or leave the destructor. */
13813
13814 static tree
build_clobber_this()13815 build_clobber_this ()
13816 {
13817 /* Clobbering an empty base is pointless, and harmful if its one byte
13818 TYPE_SIZE overlays real data. */
13819 if (is_empty_class (current_class_type))
13820 return void_node;
13821
13822 /* If we have virtual bases, clobber the whole object, but only if we're in
13823 charge. If we don't have virtual bases, clobber the as-base type so we
13824 don't mess with tail padding. */
13825 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type);
13826
13827 tree ctype = current_class_type;
13828 if (!vbases)
13829 ctype = CLASSTYPE_AS_BASE (ctype);
13830
13831 tree clobber = build_constructor (ctype, NULL);
13832 TREE_THIS_VOLATILE (clobber) = true;
13833
13834 tree thisref = current_class_ref;
13835 if (ctype != current_class_type)
13836 {
13837 thisref = build_nop (build_reference_type (ctype), current_class_ptr);
13838 thisref = convert_from_reference (thisref);
13839 }
13840
13841 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber);
13842 if (vbases)
13843 exprstmt = build_if_in_charge (exprstmt);
13844
13845 return exprstmt;
13846 }
13847
13848 /* Create the FUNCTION_DECL for a function definition.
13849 DECLSPECS and DECLARATOR are the parts of the declaration;
13850 they describe the function's name and the type it returns,
13851 but twisted together in a fashion that parallels the syntax of C.
13852
13853 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13854 DECLARATOR is really the DECL for the function we are about to
13855 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13856 indicating that the function is an inline defined in-class.
13857
13858 This function creates a binding context for the function body
13859 as well as setting up the FUNCTION_DECL in current_function_decl.
13860
13861 For C++, we must first check whether that datum makes any sense.
13862 For example, "class A local_a(1,2);" means that variable local_a
13863 is an aggregate of type A, which should have a constructor
13864 applied to it with the argument list [1, 2].
13865
13866 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
13867 or may be a BLOCK if the function has been defined previously
13868 in this translation unit. On exit, DECL_INITIAL (decl1) will be
13869 error_mark_node if the function has never been defined, or
13870 a BLOCK if the function has been defined somewhere. */
13871
13872 bool
start_preparsed_function(tree decl1,tree attrs,int flags)13873 start_preparsed_function (tree decl1, tree attrs, int flags)
13874 {
13875 tree ctype = NULL_TREE;
13876 tree fntype;
13877 tree restype;
13878 int doing_friend = 0;
13879 cp_binding_level *bl;
13880 tree current_function_parms;
13881 struct c_fileinfo *finfo
13882 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
13883 bool honor_interface;
13884
13885 /* Sanity check. */
13886 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node)));
13887 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
13888
13889 fntype = TREE_TYPE (decl1);
13890 if (TREE_CODE (fntype) == METHOD_TYPE)
13891 ctype = TYPE_METHOD_BASETYPE (fntype);
13892
13893 /* ISO C++ 11.4/5. A friend function defined in a class is in
13894 the (lexical) scope of the class in which it is defined. */
13895 if (!ctype && DECL_FRIEND_P (decl1))
13896 {
13897 ctype = DECL_FRIEND_CONTEXT (decl1);
13898
13899 /* CTYPE could be null here if we're dealing with a template;
13900 for example, `inline friend float foo()' inside a template
13901 will have no CTYPE set. */
13902 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13903 ctype = NULL_TREE;
13904 else
13905 doing_friend = 1;
13906 }
13907
13908 if (DECL_DECLARED_INLINE_P (decl1)
13909 && lookup_attribute ("noinline", attrs))
13910 warning_at (DECL_SOURCE_LOCATION (decl1), 0,
13911 "inline function %qD given attribute noinline", decl1);
13912
13913 /* Handle gnu_inline attribute. */
13914 if (GNU_INLINE_P (decl1))
13915 {
13916 DECL_EXTERNAL (decl1) = 1;
13917 DECL_NOT_REALLY_EXTERN (decl1) = 0;
13918 DECL_INTERFACE_KNOWN (decl1) = 1;
13919 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1;
13920 }
13921
13922 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13923 /* This is a constructor, we must ensure that any default args
13924 introduced by this definition are propagated to the clones
13925 now. The clones are used directly in overload resolution. */
13926 adjust_clone_args (decl1);
13927
13928 /* Sometimes we don't notice that a function is a static member, and
13929 build a METHOD_TYPE for it. Fix that up now. */
13930 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13931 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
13932
13933 /* Set up current_class_type, and enter the scope of the class, if
13934 appropriate. */
13935 if (ctype)
13936 push_nested_class (ctype);
13937 else if (DECL_STATIC_FUNCTION_P (decl1))
13938 push_nested_class (DECL_CONTEXT (decl1));
13939
13940 /* Now that we have entered the scope of the class, we must restore
13941 the bindings for any template parameters surrounding DECL1, if it
13942 is an inline member template. (Order is important; consider the
13943 case where a template parameter has the same name as a field of
13944 the class.) It is not until after this point that
13945 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
13946 if (flags & SF_INCLASS_INLINE)
13947 maybe_begin_member_template_processing (decl1);
13948
13949 /* Effective C++ rule 15. */
13950 if (warn_ecpp
13951 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13952 && VOID_TYPE_P (TREE_TYPE (fntype)))
13953 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
13954
13955 /* Make the init_value nonzero so pushdecl knows this is not tentative.
13956 error_mark_node is replaced below (in poplevel) with the BLOCK. */
13957 if (!DECL_INITIAL (decl1))
13958 DECL_INITIAL (decl1) = error_mark_node;
13959
13960 /* This function exists in static storage.
13961 (This does not mean `static' in the C sense!) */
13962 TREE_STATIC (decl1) = 1;
13963
13964 /* We must call push_template_decl after current_class_type is set
13965 up. (If we are processing inline definitions after exiting a
13966 class scope, current_class_type will be NULL_TREE until set above
13967 by push_nested_class.) */
13968 if (processing_template_decl)
13969 {
13970 tree newdecl1 = push_template_decl (decl1);
13971 if (newdecl1 == error_mark_node)
13972 {
13973 if (ctype || DECL_STATIC_FUNCTION_P (decl1))
13974 pop_nested_class ();
13975 return false;
13976 }
13977 decl1 = newdecl1;
13978 }
13979
13980 /* We are now in the scope of the function being defined. */
13981 current_function_decl = decl1;
13982
13983 /* Save the parm names or decls from this function's declarator
13984 where store_parm_decls will find them. */
13985 current_function_parms = DECL_ARGUMENTS (decl1);
13986
13987 /* Make sure the parameter and return types are reasonable. When
13988 you declare a function, these types can be incomplete, but they
13989 must be complete when you define the function. */
13990 check_function_type (decl1, current_function_parms);
13991
13992 /* Build the return declaration for the function. */
13993 restype = TREE_TYPE (fntype);
13994
13995 if (DECL_RESULT (decl1) == NULL_TREE)
13996 {
13997 tree resdecl;
13998
13999 resdecl = build_decl (input_location, RESULT_DECL, 0, restype);
14000 DECL_ARTIFICIAL (resdecl) = 1;
14001 DECL_IGNORED_P (resdecl) = 1;
14002 DECL_RESULT (decl1) = resdecl;
14003
14004 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
14005 }
14006
14007 /* Let the user know we're compiling this function. */
14008 announce_function (decl1);
14009
14010 /* Record the decl so that the function name is defined.
14011 If we already have a decl for this name, and it is a FUNCTION_DECL,
14012 use the old decl. */
14013 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14014 {
14015 /* A specialization is not used to guide overload resolution. */
14016 if (!DECL_FUNCTION_MEMBER_P (decl1)
14017 && !(DECL_USE_TEMPLATE (decl1) &&
14018 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
14019 {
14020 tree olddecl = pushdecl (decl1);
14021
14022 if (olddecl == error_mark_node)
14023 /* If something went wrong when registering the declaration,
14024 use DECL1; we have to have a FUNCTION_DECL to use when
14025 parsing the body of the function. */
14026 ;
14027 else
14028 {
14029 /* Otherwise, OLDDECL is either a previous declaration
14030 of the same function or DECL1 itself. */
14031
14032 if (warn_missing_declarations
14033 && olddecl == decl1
14034 && !DECL_MAIN_P (decl1)
14035 && TREE_PUBLIC (decl1)
14036 && !DECL_DECLARED_INLINE_P (decl1))
14037 {
14038 tree context;
14039
14040 /* Check whether DECL1 is in an anonymous
14041 namespace. */
14042 for (context = DECL_CONTEXT (decl1);
14043 context;
14044 context = DECL_CONTEXT (context))
14045 {
14046 if (TREE_CODE (context) == NAMESPACE_DECL
14047 && DECL_NAME (context) == NULL_TREE)
14048 break;
14049 }
14050
14051 if (context == NULL)
14052 warning_at (DECL_SOURCE_LOCATION (decl1),
14053 OPT_Wmissing_declarations,
14054 "no previous declaration for %qD", decl1);
14055 }
14056
14057 decl1 = olddecl;
14058 }
14059 }
14060 else
14061 {
14062 /* We need to set the DECL_CONTEXT. */
14063 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14064 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14065 }
14066 fntype = TREE_TYPE (decl1);
14067 restype = TREE_TYPE (fntype);
14068
14069 /* If #pragma weak applies, mark the decl appropriately now.
14070 The pragma only applies to global functions. Because
14071 determining whether or not the #pragma applies involves
14072 computing the mangled name for the declaration, we cannot
14073 apply the pragma until after we have merged this declaration
14074 with any previous declarations; if the original declaration
14075 has a linkage specification, that specification applies to
14076 the definition as well, and may affect the mangled name. */
14077 if (DECL_FILE_SCOPE_P (decl1))
14078 maybe_apply_pragma_weak (decl1);
14079 }
14080
14081 /* Reset this in case the call to pushdecl changed it. */
14082 current_function_decl = decl1;
14083
14084 gcc_assert (DECL_INITIAL (decl1));
14085
14086 /* This function may already have been parsed, in which case just
14087 return; our caller will skip over the body without parsing. */
14088 if (DECL_INITIAL (decl1) != error_mark_node)
14089 return true;
14090
14091 /* Initialize RTL machinery. We cannot do this until
14092 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
14093 even when processing a template; this is how we get
14094 CFUN set up, and our per-function variables initialized.
14095 FIXME factor out the non-RTL stuff. */
14096 bl = current_binding_level;
14097 allocate_struct_function (decl1, processing_template_decl);
14098
14099 /* Initialize the language data structures. Whenever we start
14100 a new function, we destroy temporaries in the usual way. */
14101 cfun->language = ggc_cleared_alloc<language_function> ();
14102 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14103 current_binding_level = bl;
14104
14105 if (!processing_template_decl && type_uses_auto (restype))
14106 {
14107 FNDECL_USED_AUTO (decl1) = true;
14108 current_function_auto_return_pattern = restype;
14109 }
14110
14111 /* Start the statement-tree, start the tree now. */
14112 DECL_SAVED_TREE (decl1) = push_stmt_list ();
14113
14114 /* If we are (erroneously) defining a function that we have already
14115 defined before, wipe out what we knew before. */
14116 if (!DECL_PENDING_INLINE_P (decl1))
14117 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14118
14119 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14120 {
14121 /* We know that this was set up by `grokclassfn'. We do not
14122 wait until `store_parm_decls', since evil parse errors may
14123 never get us to that point. Here we keep the consistency
14124 between `current_class_type' and `current_class_ptr'. */
14125 tree t = DECL_ARGUMENTS (decl1);
14126
14127 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
14128 gcc_assert (TYPE_PTR_P (TREE_TYPE (t)));
14129
14130 cp_function_chain->x_current_class_ref
14131 = cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
14132 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
14133 cp_function_chain->x_current_class_ptr = t;
14134
14135 /* Constructors and destructors need to know whether they're "in
14136 charge" of initializing virtual base classes. */
14137 t = DECL_CHAIN (t);
14138 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14139 {
14140 current_in_charge_parm = t;
14141 t = DECL_CHAIN (t);
14142 }
14143 if (DECL_HAS_VTT_PARM_P (decl1))
14144 {
14145 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
14146 current_vtt_parm = t;
14147 }
14148 }
14149
14150 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
14151 /* Implicitly-defined methods (like the
14152 destructor for a class in which no destructor
14153 is explicitly declared) must not be defined
14154 until their definition is needed. So, we
14155 ignore interface specifications for
14156 compiler-generated functions. */
14157 && !DECL_ARTIFICIAL (decl1));
14158
14159 if (processing_template_decl)
14160 /* Don't mess with interface flags. */;
14161 else if (DECL_INTERFACE_KNOWN (decl1))
14162 {
14163 tree ctx = decl_function_context (decl1);
14164
14165 if (DECL_NOT_REALLY_EXTERN (decl1))
14166 DECL_EXTERNAL (decl1) = 0;
14167
14168 if (ctx != NULL_TREE && vague_linkage_p (ctx))
14169 /* This is a function in a local class in an extern inline
14170 or template function. */
14171 comdat_linkage (decl1);
14172 }
14173 /* If this function belongs to an interface, it is public.
14174 If it belongs to someone else's interface, it is also external.
14175 This only affects inlines and template instantiations. */
14176 else if (!finfo->interface_unknown && honor_interface)
14177 {
14178 if (DECL_DECLARED_INLINE_P (decl1)
14179 || DECL_TEMPLATE_INSTANTIATION (decl1))
14180 {
14181 DECL_EXTERNAL (decl1)
14182 = (finfo->interface_only
14183 || (DECL_DECLARED_INLINE_P (decl1)
14184 && ! flag_implement_inlines
14185 && !DECL_VINDEX (decl1)));
14186
14187 /* For WIN32 we also want to put these in linkonce sections. */
14188 maybe_make_one_only (decl1);
14189 }
14190 else
14191 DECL_EXTERNAL (decl1) = 0;
14192 DECL_INTERFACE_KNOWN (decl1) = 1;
14193 /* If this function is in an interface implemented in this file,
14194 make sure that the back end knows to emit this function
14195 here. */
14196 if (!DECL_EXTERNAL (decl1))
14197 mark_needed (decl1);
14198 }
14199 else if (finfo->interface_unknown && finfo->interface_only
14200 && honor_interface)
14201 {
14202 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
14203 interface, we will have both finfo->interface_unknown and
14204 finfo->interface_only set. In that case, we don't want to
14205 use the normal heuristics because someone will supply a
14206 #pragma implementation elsewhere, and deducing it here would
14207 produce a conflict. */
14208 comdat_linkage (decl1);
14209 DECL_EXTERNAL (decl1) = 0;
14210 DECL_INTERFACE_KNOWN (decl1) = 1;
14211 DECL_DEFER_OUTPUT (decl1) = 1;
14212 }
14213 else
14214 {
14215 /* This is a definition, not a reference.
14216 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */
14217 if (!GNU_INLINE_P (decl1))
14218 DECL_EXTERNAL (decl1) = 0;
14219
14220 if ((DECL_DECLARED_INLINE_P (decl1)
14221 || DECL_TEMPLATE_INSTANTIATION (decl1))
14222 && ! DECL_INTERFACE_KNOWN (decl1))
14223 DECL_DEFER_OUTPUT (decl1) = 1;
14224 else
14225 DECL_INTERFACE_KNOWN (decl1) = 1;
14226 }
14227
14228 /* Determine the ELF visibility attribute for the function. We must not
14229 do this before calling "pushdecl", as we must allow "duplicate_decls"
14230 to merge any attributes appropriately. We also need to wait until
14231 linkage is set. */
14232 if (!DECL_CLONED_FUNCTION_P (decl1))
14233 determine_visibility (decl1);
14234
14235 if (!processing_template_decl)
14236 maybe_instantiate_noexcept (decl1);
14237
14238 begin_scope (sk_function_parms, decl1);
14239
14240 ++function_depth;
14241
14242 if (DECL_DESTRUCTOR_P (decl1)
14243 || (DECL_CONSTRUCTOR_P (decl1)
14244 && targetm.cxx.cdtor_returns_this ()))
14245 cdtor_label = create_artificial_label (input_location);
14246
14247 start_fname_decls ();
14248
14249 store_parm_decls (current_function_parms);
14250
14251 if (!processing_template_decl
14252 && (flag_lifetime_dse > 1)
14253 && DECL_CONSTRUCTOR_P (decl1)
14254 && !DECL_CLONED_FUNCTION_P (decl1)
14255 /* Clobbering an empty base is harmful if it overlays real data. */
14256 && !is_empty_class (current_class_type)
14257 /* We can't clobber safely for an implicitly-defined default constructor
14258 because part of the initialization might happen before we enter the
14259 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */
14260 && !implicit_default_ctor_p (decl1))
14261 finish_expr_stmt (build_clobber_this ());
14262
14263 if (!processing_template_decl
14264 && DECL_CONSTRUCTOR_P (decl1)
14265 && (flag_sanitize & SANITIZE_VPTR)
14266 && !DECL_CLONED_FUNCTION_P (decl1)
14267 && !implicit_default_ctor_p (decl1))
14268 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr);
14269
14270 return true;
14271 }
14272
14273
14274 /* Like start_preparsed_function, except that instead of a
14275 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
14276
14277 Returns true on success. If the DECLARATOR is not suitable
14278 for a function, we return false, which tells the parser to
14279 skip the entire function. */
14280
14281 bool
start_function(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrs)14282 start_function (cp_decl_specifier_seq *declspecs,
14283 const cp_declarator *declarator,
14284 tree attrs)
14285 {
14286 tree decl1;
14287
14288 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
14289 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
14290 if (decl1 == error_mark_node)
14291 return false;
14292 /* If the declarator is not suitable for a function definition,
14293 cause a syntax error. */
14294 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
14295 {
14296 error ("invalid function declaration");
14297 return false;
14298 }
14299
14300 if (DECL_MAIN_P (decl1))
14301 /* main must return int. grokfndecl should have corrected it
14302 (and issued a diagnostic) if the user got it wrong. */
14303 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
14304 integer_type_node));
14305
14306 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
14307 }
14308
14309 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
14310 FN. */
14311
14312 static bool
use_eh_spec_block(tree fn)14313 use_eh_spec_block (tree fn)
14314 {
14315 return (flag_exceptions && flag_enforce_eh_specs
14316 && !processing_template_decl
14317 && !type_throw_all_p (TREE_TYPE (fn))
14318 /* We insert the EH_SPEC_BLOCK only in the original
14319 function; then, it is copied automatically to the
14320 clones. */
14321 && !DECL_CLONED_FUNCTION_P (fn)
14322 /* Implicitly-generated constructors and destructors have
14323 exception specifications. However, those specifications
14324 are the union of the possible exceptions specified by the
14325 constructors/destructors for bases and members, so no
14326 unallowed exception will ever reach this function. By
14327 not creating the EH_SPEC_BLOCK we save a little memory,
14328 and we avoid spurious warnings about unreachable
14329 code. */
14330 && !DECL_DEFAULTED_FN (fn));
14331 }
14332
14333 /* Store the parameter declarations into the current function declaration.
14334 This is called after parsing the parameter declarations, before
14335 digesting the body of the function.
14336
14337 Also install to binding contour return value identifier, if any. */
14338
14339 static void
store_parm_decls(tree current_function_parms)14340 store_parm_decls (tree current_function_parms)
14341 {
14342 tree fndecl = current_function_decl;
14343 tree parm;
14344
14345 /* This is a chain of any other decls that came in among the parm
14346 declarations. If a parm is declared with enum {foo, bar} x;
14347 then CONST_DECLs for foo and bar are put here. */
14348 tree nonparms = NULL_TREE;
14349
14350 if (current_function_parms)
14351 {
14352 /* This case is when the function was defined with an ANSI prototype.
14353 The parms already have decls, so we need not do anything here
14354 except record them as in effect
14355 and complain if any redundant old-style parm decls were written. */
14356
14357 tree specparms = current_function_parms;
14358 tree next;
14359
14360 /* Must clear this because it might contain TYPE_DECLs declared
14361 at class level. */
14362 current_binding_level->names = NULL;
14363
14364 /* If we're doing semantic analysis, then we'll call pushdecl
14365 for each of these. We must do them in reverse order so that
14366 they end in the correct forward order. */
14367 specparms = nreverse (specparms);
14368
14369 for (parm = specparms; parm; parm = next)
14370 {
14371 next = DECL_CHAIN (parm);
14372 if (TREE_CODE (parm) == PARM_DECL)
14373 {
14374 if (DECL_NAME (parm) == NULL_TREE
14375 || !VOID_TYPE_P (parm))
14376 pushdecl (parm);
14377 else
14378 error ("parameter %qD declared void", parm);
14379 }
14380 else
14381 {
14382 /* If we find an enum constant or a type tag,
14383 put it aside for the moment. */
14384 TREE_CHAIN (parm) = NULL_TREE;
14385 nonparms = chainon (nonparms, parm);
14386 }
14387 }
14388
14389 /* Get the decls in their original chain order and record in the
14390 function. This is all and only the PARM_DECLs that were
14391 pushed into scope by the loop above. */
14392 DECL_ARGUMENTS (fndecl) = getdecls ();
14393 }
14394 else
14395 DECL_ARGUMENTS (fndecl) = NULL_TREE;
14396
14397 /* Now store the final chain of decls for the arguments
14398 as the decl-chain of the current lexical scope.
14399 Put the enumerators in as well, at the front so that
14400 DECL_ARGUMENTS is not modified. */
14401 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
14402
14403 if (use_eh_spec_block (current_function_decl))
14404 current_eh_spec_block = begin_eh_spec_block ();
14405 }
14406
14407
14408 /* We have finished doing semantic analysis on DECL, but have not yet
14409 generated RTL for its body. Save away our current state, so that
14410 when we want to generate RTL later we know what to do. */
14411
14412 static void
save_function_data(tree decl)14413 save_function_data (tree decl)
14414 {
14415 struct language_function *f;
14416
14417 /* Save the language-specific per-function data so that we can
14418 get it back when we really expand this function. */
14419 gcc_assert (!DECL_PENDING_INLINE_P (decl));
14420
14421 /* Make a copy. */
14422 f = ggc_alloc<language_function> ();
14423 memcpy (f, cp_function_chain, sizeof (struct language_function));
14424 DECL_SAVED_FUNCTION_DATA (decl) = f;
14425
14426 /* Clear out the bits we don't need. */
14427 f->base.x_stmt_tree.x_cur_stmt_list = NULL;
14428 f->bindings = NULL;
14429 f->x_local_names = NULL;
14430 f->base.local_typedefs = NULL;
14431 }
14432
14433
14434 /* Set the return value of the constructor (if present). */
14435
14436 static void
finish_constructor_body(void)14437 finish_constructor_body (void)
14438 {
14439 tree val;
14440 tree exprstmt;
14441
14442 if (targetm.cxx.cdtor_returns_this ()
14443 && (! TYPE_FOR_JAVA (current_class_type)))
14444 {
14445 /* Any return from a constructor will end up here. */
14446 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14447
14448 val = DECL_ARGUMENTS (current_function_decl);
14449 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14450 DECL_RESULT (current_function_decl), val);
14451 /* Return the address of the object. */
14452 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14453 add_stmt (exprstmt);
14454 }
14455 }
14456
14457 /* Do all the processing for the beginning of a destructor; set up the
14458 vtable pointers and cleanups for bases and members. */
14459
14460 static void
begin_destructor_body(void)14461 begin_destructor_body (void)
14462 {
14463 tree compound_stmt;
14464
14465 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
14466 issued an error message. We still want to try to process the
14467 body of the function, but initialize_vtbl_ptrs will crash if
14468 TYPE_BINFO is NULL. */
14469 if (COMPLETE_TYPE_P (current_class_type))
14470 {
14471 compound_stmt = begin_compound_stmt (0);
14472 /* Make all virtual function table pointers in non-virtual base
14473 classes point to CURRENT_CLASS_TYPE's virtual function
14474 tables. */
14475 initialize_vtbl_ptrs (current_class_ptr);
14476 finish_compound_stmt (compound_stmt);
14477
14478 if (flag_lifetime_dse
14479 /* Clobbering an empty base is harmful if it overlays real data. */
14480 && !is_empty_class (current_class_type))
14481 finish_decl_cleanup (NULL_TREE, build_clobber_this ());
14482
14483 /* And insert cleanups for our bases and members so that they
14484 will be properly destroyed if we throw. */
14485 push_base_cleanups ();
14486 }
14487 }
14488
14489 /* At the end of every destructor we generate code to delete the object if
14490 necessary. Do that now. */
14491
14492 static void
finish_destructor_body(void)14493 finish_destructor_body (void)
14494 {
14495 tree exprstmt;
14496
14497 /* Any return from a destructor will end up here; that way all base
14498 and member cleanups will be run when the function returns. */
14499 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label));
14500
14501 if (targetm.cxx.cdtor_returns_this ())
14502 {
14503 tree val;
14504
14505 val = DECL_ARGUMENTS (current_function_decl);
14506 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
14507 DECL_RESULT (current_function_decl), val);
14508 /* Return the address of the object. */
14509 exprstmt = build_stmt (input_location, RETURN_EXPR, val);
14510 add_stmt (exprstmt);
14511 }
14512 }
14513
14514 /* Do the necessary processing for the beginning of a function body, which
14515 in this case includes member-initializers, but not the catch clauses of
14516 a function-try-block. Currently, this means opening a binding level
14517 for the member-initializers (in a ctor), member cleanups (in a dtor),
14518 and capture proxies (in a lambda operator()). */
14519
14520 tree
begin_function_body(void)14521 begin_function_body (void)
14522 {
14523 tree stmt;
14524
14525 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
14526 return NULL_TREE;
14527
14528 if (processing_template_decl)
14529 /* Do nothing now. */;
14530 else
14531 /* Always keep the BLOCK node associated with the outermost pair of
14532 curly braces of a function. These are needed for correct
14533 operation of dwarfout.c. */
14534 keep_next_level (true);
14535
14536 stmt = begin_compound_stmt (BCS_FN_BODY);
14537
14538 if (processing_template_decl)
14539 /* Do nothing now. */;
14540 else if (DECL_DESTRUCTOR_P (current_function_decl))
14541 begin_destructor_body ();
14542
14543 return stmt;
14544 }
14545
14546 /* Do the processing for the end of a function body. Currently, this means
14547 closing out the cleanups for fully-constructed bases and members, and in
14548 the case of the destructor, deleting the object if desired. Again, this
14549 is only meaningful for [cd]tors, since they are the only functions where
14550 there is a significant distinction between the main body and any
14551 function catch clauses. Handling, say, main() return semantics here
14552 would be wrong, as flowing off the end of a function catch clause for
14553 main() would also need to return 0. */
14554
14555 void
finish_function_body(tree compstmt)14556 finish_function_body (tree compstmt)
14557 {
14558 if (compstmt == NULL_TREE)
14559 return;
14560
14561 /* Close the block. */
14562 finish_compound_stmt (compstmt);
14563
14564 if (processing_template_decl)
14565 /* Do nothing now. */;
14566 else if (DECL_CONSTRUCTOR_P (current_function_decl))
14567 finish_constructor_body ();
14568 else if (DECL_DESTRUCTOR_P (current_function_decl))
14569 finish_destructor_body ();
14570 }
14571
14572 /* Given a function, returns the BLOCK corresponding to the outermost level
14573 of curly braces, skipping the artificial block created for constructor
14574 initializers. */
14575
14576 tree
outer_curly_brace_block(tree fndecl)14577 outer_curly_brace_block (tree fndecl)
14578 {
14579 tree block = DECL_INITIAL (fndecl);
14580 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14581 return block;
14582 block = BLOCK_SUBBLOCKS (block);
14583 if (BLOCK_OUTER_CURLY_BRACE_P (block))
14584 return block;
14585 block = BLOCK_SUBBLOCKS (block);
14586 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block));
14587 return block;
14588 }
14589
14590 /* If FNDECL is a class's key method, add the class to the list of
14591 keyed classes that should be emitted. */
14592
14593 static void
record_key_method_defined(tree fndecl)14594 record_key_method_defined (tree fndecl)
14595 {
14596 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14597 && DECL_VIRTUAL_P (fndecl)
14598 && !processing_template_decl)
14599 {
14600 tree fnclass = DECL_CONTEXT (fndecl);
14601 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14602 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14603 }
14604 }
14605
14606 /* Subroutine of finish_function.
14607 Save the body of constexpr functions for possible
14608 future compile time evaluation. */
14609
14610 static void
maybe_save_function_definition(tree fun)14611 maybe_save_function_definition (tree fun)
14612 {
14613 if (!processing_template_decl
14614 && DECL_DECLARED_CONSTEXPR_P (fun)
14615 && !cp_function_chain->invalid_constexpr
14616 && !DECL_CLONED_FUNCTION_P (fun))
14617 register_constexpr_fundef (fun, DECL_SAVED_TREE (fun));
14618 }
14619
14620 /* Finish up a function declaration and compile that function
14621 all the way to assembler language output. The free the storage
14622 for the function definition.
14623
14624 FLAGS is a bitwise or of the following values:
14625 2 - INCLASS_INLINE
14626 We just finished processing the body of an in-class inline
14627 function definition. (This processing will have taken place
14628 after the class definition is complete.) */
14629
14630 tree
finish_function(int flags)14631 finish_function (int flags)
14632 {
14633 tree fndecl = current_function_decl;
14634 tree fntype, ctype = NULL_TREE;
14635 int inclass_inline = (flags & 2) != 0;
14636
14637 /* When we get some parse errors, we can end up without a
14638 current_function_decl, so cope. */
14639 if (fndecl == NULL_TREE)
14640 return error_mark_node;
14641
14642 if (c_dialect_objc ())
14643 objc_finish_function ();
14644
14645 record_key_method_defined (fndecl);
14646
14647 fntype = TREE_TYPE (fndecl);
14648
14649 /* TREE_READONLY (fndecl) = 1;
14650 This caused &foo to be of type ptr-to-const-function
14651 which then got a warning when stored in a ptr-to-function variable. */
14652
14653 gcc_assert (building_stmt_list_p ());
14654 /* The current function is being defined, so its DECL_INITIAL should
14655 be set, and unless there's a multiple definition, it should be
14656 error_mark_node. */
14657 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
14658
14659 /* For a cloned function, we've already got all the code we need;
14660 there's no need to add any extra bits. */
14661 if (!DECL_CLONED_FUNCTION_P (fndecl))
14662 {
14663 /* Make it so that `main' always returns 0 by default. */
14664 if (DECL_MAIN_P (current_function_decl))
14665 finish_return_stmt (integer_zero_node);
14666
14667 if (use_eh_spec_block (current_function_decl))
14668 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14669 (TREE_TYPE (current_function_decl)),
14670 current_eh_spec_block);
14671 }
14672
14673 /* If we're saving up tree structure, tie off the function now. */
14674 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
14675
14676 if (fn_contains_cilk_spawn_p (cfun) && !processing_template_decl)
14677 cfun->cilk_frame_decl = insert_cilk_frame (fndecl);
14678
14679 finish_fname_decls ();
14680
14681 /* If this function can't throw any exceptions, remember that. */
14682 if (!processing_template_decl
14683 && !cp_function_chain->can_throw
14684 && !flag_non_call_exceptions
14685 && !decl_replaceable_p (fndecl))
14686 TREE_NOTHROW (fndecl) = 1;
14687
14688 /* This must come after expand_function_end because cleanups might
14689 have declarations (from inline functions) that need to go into
14690 this function's blocks. */
14691
14692 /* If the current binding level isn't the outermost binding level
14693 for this function, either there is a bug, or we have experienced
14694 syntax errors and the statement tree is malformed. */
14695 if (current_binding_level->kind != sk_function_parms)
14696 {
14697 /* Make sure we have already experienced errors. */
14698 gcc_assert (errorcount);
14699
14700 /* Throw away the broken statement tree and extra binding
14701 levels. */
14702 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
14703
14704 while (current_binding_level->kind != sk_function_parms)
14705 {
14706 if (current_binding_level->kind == sk_class)
14707 pop_nested_class ();
14708 else
14709 poplevel (0, 0, 0);
14710 }
14711 }
14712 poplevel (1, 0, 1);
14713
14714 /* Statements should always be full-expressions at the outermost set
14715 of curly braces for a function. */
14716 gcc_assert (stmts_are_full_exprs_p ());
14717
14718 /* If there are no return statements in a function with auto return type,
14719 the return type is void. But if the declared type is something like
14720 auto*, this is an error. */
14721 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl)
14722 && TREE_TYPE (fntype) == current_function_auto_return_pattern)
14723 {
14724 if (!is_auto (current_function_auto_return_pattern)
14725 && !current_function_returns_value && !current_function_returns_null)
14726 {
14727 error ("no return statements in function returning %qT",
14728 current_function_auto_return_pattern);
14729 inform (input_location, "only plain %<auto%> return type can be "
14730 "deduced to %<void%>");
14731 }
14732 apply_deduced_return_type (fndecl, void_type_node);
14733 fntype = TREE_TYPE (fndecl);
14734 }
14735
14736 // If this is a concept, check that the definition is reasonable.
14737 if (DECL_DECLARED_CONCEPT_P (fndecl))
14738 check_function_concept (fndecl);
14739
14740 /* Save constexpr function body before it gets munged by
14741 the NRV transformation. */
14742 maybe_save_function_definition (fndecl);
14743
14744 /* Invoke the pre-genericize plugin before we start munging things. */
14745 if (!processing_template_decl)
14746 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
14747
14748 /* Perform delayed folding before NRV transformation. */
14749 if (!processing_template_decl)
14750 cp_fold_function (fndecl);
14751
14752 /* Set up the named return value optimization, if we can. Candidate
14753 variables are selected in check_return_expr. */
14754 if (current_function_return_value)
14755 {
14756 tree r = current_function_return_value;
14757 tree outer;
14758
14759 if (r != error_mark_node
14760 /* This is only worth doing for fns that return in memory--and
14761 simpler, since we don't have to worry about promoted modes. */
14762 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
14763 /* Only allow this for variables declared in the outer scope of
14764 the function so we know that their lifetime always ends with a
14765 return; see g++.dg/opt/nrv6.C. We could be more flexible if
14766 we were to do this optimization in tree-ssa. */
14767 && (outer = outer_curly_brace_block (fndecl))
14768 && chain_member (r, BLOCK_VARS (outer)))
14769 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
14770
14771 current_function_return_value = NULL_TREE;
14772 }
14773
14774 /* Remember that we were in class scope. */
14775 if (current_class_name)
14776 ctype = current_class_type;
14777
14778 /* Must mark the RESULT_DECL as being in this function. */
14779 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14780
14781 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14782 to the FUNCTION_DECL node itself. */
14783 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14784
14785 /* Save away current state, if appropriate. */
14786 if (!processing_template_decl)
14787 save_function_data (fndecl);
14788
14789 /* Complain if there's just no return statement. */
14790 if (warn_return_type
14791 && !VOID_TYPE_P (TREE_TYPE (fntype))
14792 && !dependent_type_p (TREE_TYPE (fntype))
14793 && !current_function_returns_value && !current_function_returns_null
14794 /* Don't complain if we abort or throw. */
14795 && !current_function_returns_abnormally
14796 /* Don't complain if there's an infinite loop. */
14797 && !current_function_infinite_loop
14798 /* Don't complain if we are declared noreturn. */
14799 && !TREE_THIS_VOLATILE (fndecl)
14800 && !DECL_NAME (DECL_RESULT (fndecl))
14801 && !TREE_NO_WARNING (fndecl)
14802 /* Structor return values (if any) are set by the compiler. */
14803 && !DECL_CONSTRUCTOR_P (fndecl)
14804 && !DECL_DESTRUCTOR_P (fndecl)
14805 && targetm.warn_func_return (fndecl))
14806 {
14807 warning (OPT_Wreturn_type,
14808 "no return statement in function returning non-void");
14809 TREE_NO_WARNING (fndecl) = 1;
14810 }
14811
14812 /* Store the end of the function, so that we get good line number
14813 info for the epilogue. */
14814 cfun->function_end_locus = input_location;
14815
14816 /* Complain about parameters that are only set, but never otherwise used. */
14817 if (warn_unused_but_set_parameter
14818 && !processing_template_decl
14819 && errorcount == unused_but_set_errorcount
14820 && !DECL_CLONED_FUNCTION_P (fndecl))
14821 {
14822 tree decl;
14823
14824 for (decl = DECL_ARGUMENTS (fndecl);
14825 decl;
14826 decl = DECL_CHAIN (decl))
14827 if (TREE_USED (decl)
14828 && TREE_CODE (decl) == PARM_DECL
14829 && !DECL_READ_P (decl)
14830 && DECL_NAME (decl)
14831 && !DECL_ARTIFICIAL (decl)
14832 && !TREE_NO_WARNING (decl)
14833 && !DECL_IN_SYSTEM_HEADER (decl)
14834 && TREE_TYPE (decl) != error_mark_node
14835 && TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
14836 && (!CLASS_TYPE_P (TREE_TYPE (decl))
14837 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
14838 warning_at (DECL_SOURCE_LOCATION (decl),
14839 OPT_Wunused_but_set_parameter,
14840 "parameter %qD set but not used", decl);
14841 unused_but_set_errorcount = errorcount;
14842 }
14843
14844 /* Complain about locally defined typedefs that are not used in this
14845 function. */
14846 maybe_warn_unused_local_typedefs ();
14847
14848 /* Possibly warn about unused parameters. */
14849 if (warn_unused_parameter
14850 && !processing_template_decl
14851 && !DECL_CLONED_FUNCTION_P (fndecl))
14852 do_warn_unused_parameter (fndecl);
14853
14854 /* Genericize before inlining. */
14855 if (!processing_template_decl)
14856 {
14857 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
14858 cp_genericize (fndecl);
14859 /* Clear out the bits we don't need. */
14860 f->x_current_class_ptr = NULL;
14861 f->x_current_class_ref = NULL;
14862 f->x_eh_spec_block = NULL;
14863 f->x_in_charge_parm = NULL;
14864 f->x_vtt_parm = NULL;
14865 f->x_return_value = NULL;
14866 f->bindings = NULL;
14867 f->extern_decl_map = NULL;
14868 f->infinite_loops = NULL;
14869 }
14870 /* Clear out the bits we don't need. */
14871 local_names = NULL;
14872
14873 /* We're leaving the context of this function, so zap cfun. It's still in
14874 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
14875 set_cfun (NULL);
14876 current_function_decl = NULL;
14877
14878 /* If this is an in-class inline definition, we may have to pop the
14879 bindings for the template parameters that we added in
14880 maybe_begin_member_template_processing when start_function was
14881 called. */
14882 if (inclass_inline)
14883 maybe_end_member_template_processing ();
14884
14885 /* Leave the scope of the class. */
14886 if (ctype)
14887 pop_nested_class ();
14888
14889 --function_depth;
14890
14891 /* Clean up. */
14892 current_function_decl = NULL_TREE;
14893
14894 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl);
14895 return fndecl;
14896 }
14897
14898 /* Create the FUNCTION_DECL for a function definition.
14899 DECLSPECS and DECLARATOR are the parts of the declaration;
14900 they describe the return type and the name of the function,
14901 but twisted together in a fashion that parallels the syntax of C.
14902
14903 This function creates a binding context for the function body
14904 as well as setting up the FUNCTION_DECL in current_function_decl.
14905
14906 Returns a FUNCTION_DECL on success.
14907
14908 If the DECLARATOR is not suitable for a function (it defines a datum
14909 instead), we return 0, which tells yyparse to report a parse error.
14910
14911 May return void_type_node indicating that this method is actually
14912 a friend. See grokfield for more details.
14913
14914 Came here with a `.pushlevel' .
14915
14916 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14917 CHANGES TO CODE IN `grokfield'. */
14918
14919 tree
grokmethod(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrlist)14920 grokmethod (cp_decl_specifier_seq *declspecs,
14921 const cp_declarator *declarator, tree attrlist)
14922 {
14923 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14924 &attrlist);
14925
14926 if (fndecl == error_mark_node)
14927 return error_mark_node;
14928
14929 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14930 {
14931 error ("invalid member function declaration");
14932 return error_mark_node;
14933 }
14934
14935 if (attrlist)
14936 cplus_decl_attributes (&fndecl, attrlist, 0);
14937
14938 /* Pass friends other than inline friend functions back. */
14939 if (fndecl == void_type_node)
14940 return fndecl;
14941
14942 if (DECL_IN_AGGR_P (fndecl))
14943 {
14944 if (DECL_CLASS_SCOPE_P (fndecl))
14945 error ("%qD is already defined in class %qT", fndecl,
14946 DECL_CONTEXT (fndecl));
14947 return error_mark_node;
14948 }
14949
14950 check_template_shadow (fndecl);
14951
14952 if (TREE_PUBLIC (fndecl))
14953 DECL_COMDAT (fndecl) = 1;
14954 DECL_DECLARED_INLINE_P (fndecl) = 1;
14955 DECL_NO_INLINE_WARNING_P (fndecl) = 1;
14956
14957 /* We process method specializations in finish_struct_1. */
14958 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14959 {
14960 fndecl = push_template_decl (fndecl);
14961 if (fndecl == error_mark_node)
14962 return fndecl;
14963 }
14964
14965 if (! DECL_FRIEND_P (fndecl))
14966 {
14967 if (DECL_CHAIN (fndecl))
14968 {
14969 fndecl = copy_node (fndecl);
14970 TREE_CHAIN (fndecl) = NULL_TREE;
14971 }
14972 }
14973
14974 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0);
14975
14976 DECL_IN_AGGR_P (fndecl) = 1;
14977 return fndecl;
14978 }
14979
14980
14981 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
14982 we can lay it out later, when and if its type becomes complete.
14983
14984 Also handle constexpr variables where the initializer involves
14985 an unlowered PTRMEM_CST because the class isn't complete yet. */
14986
14987 void
maybe_register_incomplete_var(tree var)14988 maybe_register_incomplete_var (tree var)
14989 {
14990 gcc_assert (VAR_P (var));
14991
14992 /* Keep track of variables with incomplete types. */
14993 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
14994 && DECL_EXTERNAL (var))
14995 {
14996 tree inner_type = TREE_TYPE (var);
14997
14998 while (TREE_CODE (inner_type) == ARRAY_TYPE)
14999 inner_type = TREE_TYPE (inner_type);
15000 inner_type = TYPE_MAIN_VARIANT (inner_type);
15001
15002 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15003 /* RTTI TD entries are created while defining the type_info. */
15004 || (TYPE_LANG_SPECIFIC (inner_type)
15005 && TYPE_BEING_DEFINED (inner_type)))
15006 {
15007 incomplete_var iv = {var, inner_type};
15008 vec_safe_push (incomplete_vars, iv);
15009 }
15010 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var))
15011 && decl_constant_var_p (var)
15012 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type)))
15013 {
15014 /* When the outermost open class is complete we can resolve any
15015 pointers-to-members. */
15016 tree context = outermost_open_class ();
15017 incomplete_var iv = {var, context};
15018 vec_safe_push (incomplete_vars, iv);
15019 }
15020 }
15021 }
15022
15023 /* Called when a class type (given by TYPE) is defined. If there are
15024 any existing VAR_DECLs whose type has been completed by this
15025 declaration, update them now. */
15026
15027 void
complete_vars(tree type)15028 complete_vars (tree type)
15029 {
15030 unsigned ix;
15031 incomplete_var *iv;
15032
15033 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); )
15034 {
15035 if (same_type_p (type, iv->incomplete_type))
15036 {
15037 tree var = iv->decl;
15038 tree type = TREE_TYPE (var);
15039
15040 if (type != error_mark_node
15041 && (TYPE_MAIN_VARIANT (strip_array_types (type))
15042 == iv->incomplete_type))
15043 {
15044 /* Complete the type of the variable. The VAR_DECL itself
15045 will be laid out in expand_expr. */
15046 complete_type (type);
15047 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
15048 }
15049
15050 /* Remove this entry from the list. */
15051 incomplete_vars->unordered_remove (ix);
15052 }
15053 else
15054 ix++;
15055 }
15056
15057 /* Check for pending declarations which may have abstract type. */
15058 complete_type_check_abstract (type);
15059 }
15060
15061 /* If DECL is of a type which needs a cleanup, build and return an
15062 expression to perform that cleanup here. Return NULL_TREE if no
15063 cleanup need be done. DECL can also be a _REF when called from
15064 split_nonconstant_init_1. */
15065
15066 tree
cxx_maybe_build_cleanup(tree decl,tsubst_flags_t complain)15067 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain)
15068 {
15069 tree type;
15070 tree attr;
15071 tree cleanup;
15072
15073 /* Assume no cleanup is required. */
15074 cleanup = NULL_TREE;
15075
15076 if (error_operand_p (decl))
15077 return cleanup;
15078
15079 /* Handle "__attribute__((cleanup))". We run the cleanup function
15080 before the destructor since the destructor is what actually
15081 terminates the lifetime of the object. */
15082 if (DECL_P (decl))
15083 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
15084 else
15085 attr = NULL_TREE;
15086 if (attr)
15087 {
15088 tree id;
15089 tree fn;
15090 tree arg;
15091
15092 /* Get the name specified by the user for the cleanup function. */
15093 id = TREE_VALUE (TREE_VALUE (attr));
15094 /* Look up the name to find the cleanup function to call. It is
15095 important to use lookup_name here because that is what is
15096 used in c-common.c:handle_cleanup_attribute when performing
15097 initial checks on the attribute. Note that those checks
15098 include ensuring that the function found is not an overloaded
15099 function, or an object with an overloaded call operator,
15100 etc.; we can rely on the fact that the function found is an
15101 ordinary FUNCTION_DECL. */
15102 fn = lookup_name (id);
15103 arg = build_address (decl);
15104 if (!mark_used (decl, complain) && !(complain & tf_error))
15105 return error_mark_node;
15106 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE);
15107 if (cleanup == error_mark_node)
15108 return error_mark_node;
15109 }
15110 /* Handle ordinary C++ destructors. */
15111 type = TREE_TYPE (decl);
15112 if (type_build_dtor_call (type))
15113 {
15114 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR;
15115 tree addr;
15116 tree call;
15117
15118 if (TREE_CODE (type) == ARRAY_TYPE)
15119 addr = decl;
15120 else
15121 addr = build_address (decl);
15122
15123 call = build_delete (TREE_TYPE (addr), addr,
15124 sfk_complete_destructor, flags, 0, complain);
15125 if (call == error_mark_node)
15126 cleanup = error_mark_node;
15127 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
15128 /* Discard the call. */;
15129 else if (cleanup)
15130 cleanup = cp_build_compound_expr (cleanup, call, complain);
15131 else
15132 cleanup = call;
15133 }
15134
15135 /* build_delete sets the location of the destructor call to the
15136 current location, even though the destructor is going to be
15137 called later, at the end of the current scope. This can lead to
15138 a "jumpy" behavior for users of debuggers when they step around
15139 the end of the block. So let's unset the location of the
15140 destructor call instead. */
15141 protected_set_expr_location (cleanup, UNKNOWN_LOCATION);
15142
15143 if (cleanup
15144 && DECL_P (decl)
15145 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl)))
15146 /* Treat objects with destructors as used; the destructor may do
15147 something substantive. */
15148 && !mark_used (decl, complain) && !(complain & tf_error))
15149 return error_mark_node;
15150
15151 return cleanup;
15152 }
15153
15154
15155 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a
15156 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to
15157 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */
15158
15159 tree
static_fn_type(tree memfntype)15160 static_fn_type (tree memfntype)
15161 {
15162 tree fntype;
15163 tree args;
15164
15165 if (TYPE_PTRMEMFUNC_P (memfntype))
15166 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype);
15167 if (POINTER_TYPE_P (memfntype)
15168 || TREE_CODE (memfntype) == FUNCTION_DECL)
15169 memfntype = TREE_TYPE (memfntype);
15170 if (TREE_CODE (memfntype) == FUNCTION_TYPE)
15171 return memfntype;
15172 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE);
15173 args = TYPE_ARG_TYPES (memfntype);
15174 cp_ref_qualifier rqual = type_memfn_rqual (memfntype);
15175 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args));
15176 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype), rqual);
15177 fntype = (cp_build_type_attribute_variant
15178 (fntype, TYPE_ATTRIBUTES (memfntype)));
15179 fntype = (build_exception_variant
15180 (fntype, TYPE_RAISES_EXCEPTIONS (memfntype)));
15181 if (TYPE_HAS_LATE_RETURN_TYPE (memfntype))
15182 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
15183 return fntype;
15184 }
15185
15186 /* DECL was originally constructed as a non-static member function,
15187 but turned out to be static. Update it accordingly. */
15188
15189 void
revert_static_member_fn(tree decl)15190 revert_static_member_fn (tree decl)
15191 {
15192 tree stype = static_fn_type (decl);
15193 cp_cv_quals quals = type_memfn_quals (stype);
15194 cp_ref_qualifier rqual = type_memfn_rqual (stype);
15195
15196 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE)
15197 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE);
15198
15199 TREE_TYPE (decl) = stype;
15200
15201 if (DECL_ARGUMENTS (decl))
15202 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl));
15203 DECL_STATIC_FUNCTION_P (decl) = 1;
15204 }
15205
15206 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
15207 one of the language-independent trees. */
15208
15209 enum cp_tree_node_structure_enum
cp_tree_node_structure(union lang_tree_node * t)15210 cp_tree_node_structure (union lang_tree_node * t)
15211 {
15212 switch (TREE_CODE (&t->generic))
15213 {
15214 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
15215 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT;
15216 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
15217 case OVERLOAD: return TS_CP_OVERLOAD;
15218 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
15219 case PTRMEM_CST: return TS_CP_PTRMEM;
15220 case BASELINK: return TS_CP_BASELINK;
15221 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL;
15222 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT;
15223 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT;
15224 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR;
15225 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR;
15226 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO;
15227 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO;
15228 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL;
15229 default: return TS_CP_GENERIC;
15230 }
15231 }
15232
15233 /* Build the void_list_node (void_type_node having been created). */
15234 tree
build_void_list_node(void)15235 build_void_list_node (void)
15236 {
15237 tree t = build_tree_list (NULL_TREE, void_type_node);
15238 return t;
15239 }
15240
15241 bool
cp_missing_noreturn_ok_p(tree decl)15242 cp_missing_noreturn_ok_p (tree decl)
15243 {
15244 /* A missing noreturn is ok for the `main' function. */
15245 return DECL_MAIN_P (decl);
15246 }
15247
15248 /* Return the decl used to identify the COMDAT group into which DECL should
15249 be placed. */
15250
15251 tree
cxx_comdat_group(tree decl)15252 cxx_comdat_group (tree decl)
15253 {
15254 /* Virtual tables, construction virtual tables, and virtual table
15255 tables all go in a single COMDAT group, named after the primary
15256 virtual table. */
15257 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl))
15258 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl));
15259 /* For all other DECLs, the COMDAT group is the mangled name of the
15260 declaration itself. */
15261 else
15262 {
15263 while (DECL_THUNK_P (decl))
15264 {
15265 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
15266 into the same section as the target function. In that case
15267 we must return target's name. */
15268 tree target = THUNK_TARGET (decl);
15269 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
15270 && DECL_SECTION_NAME (target) != NULL
15271 && DECL_ONE_ONLY (target))
15272 decl = target;
15273 else
15274 break;
15275 }
15276 }
15277
15278 return decl;
15279 }
15280
15281 /* Returns the return type for FN as written by the user, which may include
15282 a placeholder for a deduced return type. */
15283
15284 tree
fndecl_declared_return_type(tree fn)15285 fndecl_declared_return_type (tree fn)
15286 {
15287 fn = STRIP_TEMPLATE (fn);
15288 if (FNDECL_USED_AUTO (fn))
15289 {
15290 struct language_function *f = NULL;
15291 if (DECL_STRUCT_FUNCTION (fn))
15292 f = DECL_STRUCT_FUNCTION (fn)->language;
15293 if (f == NULL)
15294 f = DECL_SAVED_FUNCTION_DATA (fn);
15295 return f->x_auto_return_pattern;
15296 }
15297 return TREE_TYPE (TREE_TYPE (fn));
15298 }
15299
15300 /* Returns true iff DECL was declared with an auto return type and it has
15301 not yet been deduced to a real type. */
15302
15303 bool
undeduced_auto_decl(tree decl)15304 undeduced_auto_decl (tree decl)
15305 {
15306 if (cxx_dialect < cxx14)
15307 return false;
15308 return type_uses_auto (TREE_TYPE (decl));
15309 }
15310
15311 /* Complain if DECL has an undeduced return type. */
15312
15313 void
require_deduced_type(tree decl)15314 require_deduced_type (tree decl)
15315 {
15316 if (undeduced_auto_decl (decl))
15317 error ("use of %qD before deduction of %<auto%>", decl);
15318 }
15319
15320 #include "gt-cp-decl.h"
15321