1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23
24 /* Process declarations and symbol lookup for C++ front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
27
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "output.h"
43 #include "except.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "tm_p.h"
47 #include "target.h"
48 #include "c-common.h"
49 #include "c-pragma.h"
50 #include "diagnostic.h"
51 #include "debug.h"
52 #include "timevar.h"
53 #include "tree-flow.h"
54
55 static tree grokparms (cp_parameter_declarator *, tree *);
56 static const char *redeclaration_error_message (tree, tree);
57
58 static int decl_jump_unsafe (tree);
59 static void require_complete_types_for_parms (tree);
60 static int ambi_op_p (enum tree_code);
61 static int unary_op_p (enum tree_code);
62 static void push_local_name (tree);
63 static tree grok_reference_init (tree, tree, tree, tree *);
64 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
65 int, int, tree);
66 static void record_unknown_type (tree, const char *);
67 static tree builtin_function_1 (const char *, tree, tree,
68 enum built_in_function code,
69 enum built_in_class cl, const char *,
70 tree);
71 static tree build_library_fn_1 (tree, enum tree_code, tree);
72 static int member_function_or_else (tree, tree, enum overload_flags);
73 static void bad_specifiers (tree, const char *, int, int, int, int,
74 int);
75 static void check_for_uninitialized_const_var (tree);
76 static hashval_t typename_hash (const void *);
77 static int typename_compare (const void *, const void *);
78 static tree local_variable_p_walkfn (tree *, int *, void *);
79 static tree record_builtin_java_type (const char *, int);
80 static const char *tag_name (enum tag_types);
81 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
82 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
83 static void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static void maybe_commonize_var (tree);
86 static tree check_initializer (tree, tree, int, tree *);
87 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
88 static void save_function_data (tree);
89 static void check_function_type (tree, tree);
90 static void finish_constructor_body (void);
91 static void begin_destructor_body (void);
92 static void finish_destructor_body (void);
93 static tree create_array_type_for_decl (tree, tree, tree);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (void);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree);
102 static tree push_cp_library_fn (enum tree_code, tree);
103 static tree build_cp_library_fn (tree, enum tree_code, tree);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
107 static tree next_initializable_field (tree);
108
109 /* The following symbols are subsumed in the cp_global_trees array, and
110 listed here individually for documentation purposes.
111
112 C++ extensions
113 tree wchar_decl_node;
114
115 tree vtable_entry_type;
116 tree delta_type_node;
117 tree __t_desc_type_node;
118
119 tree class_type_node;
120 tree unknown_type_node;
121
122 Array type `vtable_entry_type[]'
123
124 tree vtbl_type_node;
125 tree vtbl_ptr_type_node;
126
127 Namespaces,
128
129 tree std_node;
130 tree abi_node;
131
132 A FUNCTION_DECL which can call `abort'. Not necessarily the
133 one that the user will declare, but sufficient to be called
134 by routines that want to abort the program.
135
136 tree abort_fndecl;
137
138 The FUNCTION_DECL for the default `::operator delete'.
139
140 tree global_delete_fndecl;
141
142 Used by RTTI
143 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
144 tree tinfo_var_id; */
145
146 tree cp_global_trees[CPTI_MAX];
147
148 /* Indicates that there is a type value in some namespace, although
149 that is not necessarily in scope at the moment. */
150
151 tree global_type_node;
152
153 /* The node that holds the "name" of the global scope. */
154 tree global_scope_name;
155
156 #define local_names cp_function_chain->x_local_names
157
158 /* A list of objects which have constructors or destructors
159 which reside in the global scope. The decl is stored in
160 the TREE_VALUE slot and the initializer is stored
161 in the TREE_PURPOSE slot. */
162 tree static_aggregates;
163
164 /* -- end of C++ */
165
166 /* A node for the integer constants 2, and 3. */
167
168 tree integer_two_node, integer_three_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 named_label_use_entry GTY(())
174 {
175 struct named_label_use_entry *next;
176 /* The binding level to which this entry is *currently* attached.
177 This is initially the binding level in which the goto appeared,
178 but is modified as scopes are closed. */
179 struct cp_binding_level *binding_level;
180 /* The head of the names list that was current when the goto appeared,
181 or the inner scope popped. These are the decls that will *not* be
182 skipped when jumping to the label. */
183 tree names_in_scope;
184 /* The location of the goto, for error reporting. */
185 location_t o_goto_locus;
186 /* True if an OpenMP structured block scope has been closed since
187 the goto appeared. This means that the branch from the label will
188 illegally exit an OpenMP scope. */
189 bool in_omp_scope;
190 };
191
192 /* A list of all LABEL_DECLs in the function that have names. Here so
193 we can clear out their names' definitions at the end of the
194 function, and so we can check the validity of jumps to these labels. */
195
196 struct named_label_entry GTY(())
197 {
198 /* The decl itself. */
199 tree label_decl;
200
201 /* The binding level to which the label is *currently* attached.
202 This is initially set to the binding level in which the label
203 is defined, but is modified as scopes are closed. */
204 struct cp_binding_level *binding_level;
205 /* The head of the names list that was current when the label was
206 defined, or the inner scope popped. These are the decls that will
207 be skipped when jumping to the label. */
208 tree names_in_scope;
209 /* A tree list of all decls from all binding levels that would be
210 crossed by a backward branch to the label. */
211 tree bad_decls;
212
213 /* A list of uses of the label, before the label is defined. */
214 struct named_label_use_entry *uses;
215
216 /* The following bits are set after the label is defined, and are
217 updated as scopes are popped. They indicate that a backward jump
218 to the label will illegally enter a scope of the given flavor. */
219 bool in_try_scope;
220 bool in_catch_scope;
221 bool in_omp_scope;
222 };
223
224 #define named_labels cp_function_chain->x_named_labels
225
226 /* The number of function bodies which we are currently processing.
227 (Zero if we are at namespace scope, one inside the body of a
228 function, two inside the body of a function in a local class, etc.) */
229 int function_depth;
230
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
234 deprecated items. */
235 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
236 /* An object declared as __attribute__((unavailable)) suppresses
237 any reports of being declared with unavailable or deprecated
238 items. */
239 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
240
241 enum deprecated_states {
242 DEPRECATED_NORMAL,
243 DEPRECATED_SUPPRESS
244 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
245 , DEPRECATED_UNAVAILABLE_SUPPRESS
246 };
247
248 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
249
250
251 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
252 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
253 time the VAR_DECL was declared, the type was incomplete. */
254
255 static GTY(()) tree incomplete_vars;
256
257 /* Returns the kind of template specialization we are currently
258 processing, given that it's declaration contained N_CLASS_SCOPES
259 explicit scope qualifications. */
260
261 tmpl_spec_kind
current_tmpl_spec_kind(int n_class_scopes)262 current_tmpl_spec_kind (int n_class_scopes)
263 {
264 int n_template_parm_scopes = 0;
265 int seen_specialization_p = 0;
266 int innermost_specialization_p = 0;
267 struct cp_binding_level *b;
268
269 /* Scan through the template parameter scopes. */
270 for (b = current_binding_level;
271 b->kind == sk_template_parms;
272 b = b->level_chain)
273 {
274 /* If we see a specialization scope inside a parameter scope,
275 then something is wrong. That corresponds to a declaration
276 like:
277
278 template <class T> template <> ...
279
280 which is always invalid since [temp.expl.spec] forbids the
281 specialization of a class member template if the enclosing
282 class templates are not explicitly specialized as well. */
283 if (b->explicit_spec_p)
284 {
285 if (n_template_parm_scopes == 0)
286 innermost_specialization_p = 1;
287 else
288 seen_specialization_p = 1;
289 }
290 else if (seen_specialization_p == 1)
291 return tsk_invalid_member_spec;
292
293 ++n_template_parm_scopes;
294 }
295
296 /* Handle explicit instantiations. */
297 if (processing_explicit_instantiation)
298 {
299 if (n_template_parm_scopes != 0)
300 /* We've seen a template parameter list during an explicit
301 instantiation. For example:
302
303 template <class T> template void f(int);
304
305 This is erroneous. */
306 return tsk_invalid_expl_inst;
307 else
308 return tsk_expl_inst;
309 }
310
311 if (n_template_parm_scopes < n_class_scopes)
312 /* We've not seen enough template headers to match all the
313 specialized classes present. For example:
314
315 template <class T> void R<T>::S<T>::f(int);
316
317 This is invalid; there needs to be one set of template
318 parameters for each class. */
319 return tsk_insufficient_parms;
320 else if (n_template_parm_scopes == n_class_scopes)
321 /* We're processing a non-template declaration (even though it may
322 be a member of a template class.) For example:
323
324 template <class T> void S<T>::f(int);
325
326 The `class T' maches the `S<T>', leaving no template headers
327 corresponding to the `f'. */
328 return tsk_none;
329 else if (n_template_parm_scopes > n_class_scopes + 1)
330 /* We've got too many template headers. For example:
331
332 template <> template <class T> void f (T);
333
334 There need to be more enclosing classes. */
335 return tsk_excessive_parms;
336 else
337 /* This must be a template. It's of the form:
338
339 template <class T> template <class U> void S<T>::f(U);
340
341 This is a specialization if the innermost level was a
342 specialization; otherwise it's just a definition of the
343 template. */
344 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
345 }
346
347 /* Exit the current scope. */
348
349 void
finish_scope(void)350 finish_scope (void)
351 {
352 poplevel (0, 0, 0);
353 }
354
355 /* When a label goes out of scope, check to see if that label was used
356 in a valid manner, and issue any appropriate warnings or errors. */
357
358 static void
pop_label(tree label,tree old_value)359 pop_label (tree label, tree old_value)
360 {
361 if (!processing_template_decl)
362 {
363 if (DECL_INITIAL (label) == NULL_TREE)
364 {
365 location_t location;
366
367 error ("label %q+D used but not defined", label);
368 #ifdef USE_MAPPED_LOCATION
369 location = input_location; /* FIXME want (input_filename, (line)0) */
370 #else
371 location.file = input_filename;
372 location.line = 0;
373 #endif
374 /* Avoid crashing later. */
375 define_label (location, DECL_NAME (label));
376 }
377 else if (!TREE_USED (label))
378 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
379 }
380
381 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
382 }
383
384 /* At the end of a function, all labels declared within the function
385 go out of scope. BLOCK is the top-level block for the
386 function. */
387
388 static int
pop_labels_1(void ** slot,void * data)389 pop_labels_1 (void **slot, void *data)
390 {
391 struct named_label_entry *ent = (struct named_label_entry *) *slot;
392 tree block = (tree) data;
393
394 pop_label (ent->label_decl, NULL_TREE);
395
396 /* Put the labels into the "variables" of the top-level block,
397 so debugger can see them. */
398 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
399 BLOCK_VARS (block) = ent->label_decl;
400
401 htab_clear_slot (named_labels, slot);
402
403 return 1;
404 }
405
406 static void
pop_labels(tree block)407 pop_labels (tree block)
408 {
409 if (named_labels)
410 {
411 htab_traverse (named_labels, pop_labels_1, block);
412 named_labels = NULL;
413 }
414 }
415
416 /* At the end of a block with local labels, restore the outer definition. */
417
418 static void
pop_local_label(tree label,tree old_value)419 pop_local_label (tree label, tree old_value)
420 {
421 struct named_label_entry dummy;
422 void **slot;
423
424 pop_label (label, old_value);
425
426 dummy.label_decl = label;
427 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
428 htab_clear_slot (named_labels, slot);
429 }
430
431 /* The following two routines are used to interface to Objective-C++.
432 The binding level is purposely treated as an opaque type. */
433
434 void *
objc_get_current_scope(void)435 objc_get_current_scope (void)
436 {
437 return current_binding_level;
438 }
439
440 /* The following routine is used by the NeXT-style SJLJ exceptions;
441 variables get marked 'volatile' so as to not be clobbered by
442 _setjmp()/_longjmp() calls. All variables in the current scope,
443 as well as parent scopes up to (but not including) ENCLOSING_BLK
444 shall be thusly marked. */
445
446 void
objc_mark_locals_volatile(void * enclosing_blk)447 objc_mark_locals_volatile (void *enclosing_blk)
448 {
449 struct cp_binding_level *scope;
450
451 for (scope = current_binding_level;
452 scope && scope != enclosing_blk;
453 scope = scope->level_chain)
454 {
455 tree decl;
456
457 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
458 objc_volatilize_decl (decl);
459
460 /* Do not climb up past the current function. */
461 if (scope->kind == sk_function_parms)
462 break;
463 }
464 }
465
466 /* Update data for defined and undefined labels when leaving a scope. */
467
468 static int
poplevel_named_label_1(void ** slot,void * data)469 poplevel_named_label_1 (void **slot, void *data)
470 {
471 struct named_label_entry *ent = (struct named_label_entry *) *slot;
472 struct cp_binding_level *bl = (struct cp_binding_level *) data;
473 struct cp_binding_level *obl = bl->level_chain;
474
475 if (ent->binding_level == bl)
476 {
477 tree decl;
478
479 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
480 if (decl_jump_unsafe (decl))
481 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
482
483 ent->binding_level = obl;
484 ent->names_in_scope = obl->names;
485 switch (bl->kind)
486 {
487 case sk_try:
488 ent->in_try_scope = true;
489 break;
490 case sk_catch:
491 ent->in_catch_scope = true;
492 break;
493 case sk_omp:
494 ent->in_omp_scope = true;
495 break;
496 default:
497 break;
498 }
499 }
500 else if (ent->uses)
501 {
502 struct named_label_use_entry *use;
503
504 for (use = ent->uses; use ; use = use->next)
505 if (use->binding_level == bl)
506 {
507 use->binding_level = obl;
508 use->names_in_scope = obl->names;
509 if (bl->kind == sk_omp)
510 use->in_omp_scope = true;
511 }
512 }
513
514 return 1;
515 }
516
517 /* Exit a binding level.
518 Pop the level off, and restore the state of the identifier-decl mappings
519 that were in effect when this level was entered.
520
521 If KEEP == 1, this level had explicit declarations, so
522 and create a "block" (a BLOCK node) for the level
523 to record its declarations and subblocks for symbol table output.
524
525 If FUNCTIONBODY is nonzero, this level is the body of a function,
526 so create a block as if KEEP were set and also clear out all
527 label names.
528
529 If REVERSE is nonzero, reverse the order of decls before putting
530 them into the BLOCK. */
531
532 tree
poplevel(int keep,int reverse,int functionbody)533 poplevel (int keep, int reverse, int functionbody)
534 {
535 tree link;
536 /* The chain of decls was accumulated in reverse order.
537 Put it into forward order, just for cleanliness. */
538 tree decls;
539 int tmp = functionbody;
540 int real_functionbody;
541 tree subblocks;
542 tree block;
543 tree decl;
544 int leaving_for_scope;
545 scope_kind kind;
546
547 timevar_push (TV_NAME_LOOKUP);
548 restart:
549
550 block = NULL_TREE;
551
552 gcc_assert (current_binding_level->kind != sk_class);
553
554 real_functionbody = (current_binding_level->kind == sk_cleanup
555 ? ((functionbody = 0), tmp) : functionbody);
556 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
557
558 gcc_assert (!VEC_length(cp_class_binding,
559 current_binding_level->class_shadowed));
560
561 /* We used to use KEEP == 2 to indicate that the new block should go
562 at the beginning of the list of blocks at this binding level,
563 rather than the end. This hack is no longer used. */
564 gcc_assert (keep == 0 || keep == 1);
565
566 if (current_binding_level->keep)
567 keep = 1;
568
569 /* Any uses of undefined labels, and any defined labels, now operate
570 under constraints of next binding contour. */
571 if (cfun && !functionbody && named_labels)
572 htab_traverse (named_labels, poplevel_named_label_1,
573 current_binding_level);
574
575 /* Get the decls in the order they were written.
576 Usually current_binding_level->names is in reverse order.
577 But parameter decls were previously put in forward order. */
578
579 if (reverse)
580 current_binding_level->names
581 = decls = nreverse (current_binding_level->names);
582 else
583 decls = current_binding_level->names;
584
585 /* If there were any declarations or structure tags in that level,
586 or if this level is a function body,
587 create a BLOCK to record them for the life of this function. */
588 block = NULL_TREE;
589 if (keep == 1 || functionbody)
590 block = make_node (BLOCK);
591 if (block != NULL_TREE)
592 {
593 BLOCK_VARS (block) = decls;
594 BLOCK_SUBBLOCKS (block) = subblocks;
595 }
596
597 /* In each subblock, record that this is its superior. */
598 if (keep >= 0)
599 for (link = subblocks; link; link = TREE_CHAIN (link))
600 BLOCK_SUPERCONTEXT (link) = block;
601
602 /* We still support the old for-scope rules, whereby the variables
603 in a for-init statement were in scope after the for-statement
604 ended. We only use the new rules if flag_new_for_scope is
605 nonzero. */
606 leaving_for_scope
607 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
608
609 /* Before we remove the declarations first check for unused variables. */
610 if (warn_unused_variable
611 && !processing_template_decl)
612 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
613 if (TREE_CODE (decl) == VAR_DECL
614 && ! TREE_USED (decl)
615 && ! DECL_IN_SYSTEM_HEADER (decl)
616 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
617 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
618
619 /* Remove declarations for all the DECLs in this level. */
620 for (link = decls; link; link = TREE_CHAIN (link))
621 {
622 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
623 && DECL_NAME (link))
624 {
625 tree name = DECL_NAME (link);
626 cxx_binding *ob;
627 tree ns_binding;
628
629 ob = outer_binding (name,
630 IDENTIFIER_BINDING (name),
631 /*class_p=*/true);
632 if (!ob)
633 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
634 else
635 ns_binding = NULL_TREE;
636
637 if (ob && ob->scope == current_binding_level->level_chain)
638 /* We have something like:
639
640 int i;
641 for (int i; ;);
642
643 and we are leaving the `for' scope. There's no reason to
644 keep the binding of the inner `i' in this case. */
645 pop_binding (name, link);
646 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
647 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
648 /* Here, we have something like:
649
650 typedef int I;
651
652 void f () {
653 for (int I; ;);
654 }
655
656 We must pop the for-scope binding so we know what's a
657 type and what isn't. */
658 pop_binding (name, link);
659 else
660 {
661 /* Mark this VAR_DECL as dead so that we can tell we left it
662 there only for backward compatibility. */
663 DECL_DEAD_FOR_LOCAL (link) = 1;
664
665 /* Keep track of what should have happened when we
666 popped the binding. */
667 if (ob && ob->value)
668 {
669 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
670 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
671 }
672
673 /* Add it to the list of dead variables in the next
674 outermost binding to that we can remove these when we
675 leave that binding. */
676 current_binding_level->level_chain->dead_vars_from_for
677 = tree_cons (NULL_TREE, link,
678 current_binding_level->level_chain->
679 dead_vars_from_for);
680
681 /* Although we don't pop the cxx_binding, we do clear
682 its SCOPE since the scope is going away now. */
683 IDENTIFIER_BINDING (name)->scope
684 = current_binding_level->level_chain;
685 }
686 }
687 else
688 {
689 tree name;
690
691 /* Remove the binding. */
692 decl = link;
693
694 if (TREE_CODE (decl) == TREE_LIST)
695 decl = TREE_VALUE (decl);
696 name = decl;
697
698 if (TREE_CODE (name) == OVERLOAD)
699 name = OVL_FUNCTION (name);
700
701 gcc_assert (DECL_P (name));
702 pop_binding (DECL_NAME (name), decl);
703 }
704 }
705
706 /* Remove declarations for any `for' variables from inner scopes
707 that we kept around. */
708 for (link = current_binding_level->dead_vars_from_for;
709 link; link = TREE_CHAIN (link))
710 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
711
712 /* Restore the IDENTIFIER_TYPE_VALUEs. */
713 for (link = current_binding_level->type_shadowed;
714 link; link = TREE_CHAIN (link))
715 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
716
717 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
718 for (link = current_binding_level->shadowed_labels;
719 link;
720 link = TREE_CHAIN (link))
721 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
722
723 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
724 list if a `using' declaration put them there. The debugging
725 back-ends won't understand OVERLOAD, so we remove them here.
726 Because the BLOCK_VARS are (temporarily) shared with
727 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
728 popped all the bindings. */
729 if (block)
730 {
731 tree* d;
732
733 for (d = &BLOCK_VARS (block); *d; )
734 {
735 if (TREE_CODE (*d) == TREE_LIST)
736 *d = TREE_CHAIN (*d);
737 else
738 d = &TREE_CHAIN (*d);
739 }
740 }
741
742 /* If the level being exited is the top level of a function,
743 check over all the labels. */
744 if (functionbody)
745 {
746 /* Since this is the top level block of a function, the vars are
747 the function's parameters. Don't leave them in the BLOCK
748 because they are found in the FUNCTION_DECL instead. */
749 BLOCK_VARS (block) = 0;
750 pop_labels (block);
751 }
752
753 kind = current_binding_level->kind;
754 if (kind == sk_cleanup)
755 {
756 tree stmt;
757
758 /* If this is a temporary binding created for a cleanup, then we'll
759 have pushed a statement list level. Pop that, create a new
760 BIND_EXPR for the block, and insert it into the stream. */
761 stmt = pop_stmt_list (current_binding_level->statement_list);
762 stmt = c_build_bind_expr (block, stmt);
763 add_stmt (stmt);
764 }
765
766 leave_scope ();
767 if (functionbody)
768 {
769 /* The current function is being defined, so its DECL_INITIAL
770 should be error_mark_node. */
771 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
772 DECL_INITIAL (current_function_decl) = block;
773 }
774 else if (block)
775 current_binding_level->blocks
776 = chainon (current_binding_level->blocks, block);
777
778 /* If we did not make a block for the level just exited,
779 any blocks made for inner levels
780 (since they cannot be recorded as subblocks in that level)
781 must be carried forward so they will later become subblocks
782 of something else. */
783 else if (subblocks)
784 current_binding_level->blocks
785 = chainon (current_binding_level->blocks, subblocks);
786
787 /* Each and every BLOCK node created here in `poplevel' is important
788 (e.g. for proper debugging information) so if we created one
789 earlier, mark it as "used". */
790 if (block)
791 TREE_USED (block) = 1;
792
793 /* All temporary bindings created for cleanups are popped silently. */
794 if (kind == sk_cleanup)
795 goto restart;
796
797 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
798 }
799
800 /* Insert BLOCK at the end of the list of subblocks of the
801 current binding level. This is used when a BIND_EXPR is expanded,
802 to handle the BLOCK node inside the BIND_EXPR. */
803
804 void
insert_block(tree block)805 insert_block (tree block)
806 {
807 TREE_USED (block) = 1;
808 current_binding_level->blocks
809 = chainon (current_binding_level->blocks, block);
810 }
811
812 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
813 itself, calling F for each. The DATA is passed to F as well. */
814
815 static int
walk_namespaces_r(tree namespace,walk_namespaces_fn f,void * data)816 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
817 {
818 int result = 0;
819 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
820
821 result |= (*f) (namespace, data);
822
823 for (; current; current = TREE_CHAIN (current))
824 result |= walk_namespaces_r (current, f, data);
825
826 return result;
827 }
828
829 /* Walk all the namespaces, calling F for each. The DATA is passed to
830 F as well. */
831
832 int
walk_namespaces(walk_namespaces_fn f,void * data)833 walk_namespaces (walk_namespaces_fn f, void* data)
834 {
835 return walk_namespaces_r (global_namespace, f, data);
836 }
837
838 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
839 DATA is non-NULL, this is the last time we will call
840 wrapup_global_declarations for this NAMESPACE. */
841
842 int
wrapup_globals_for_namespace(tree namespace,void * data)843 wrapup_globals_for_namespace (tree namespace, void* data)
844 {
845 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
846 VEC(tree,gc) *statics = level->static_decls;
847 tree *vec = VEC_address (tree, statics);
848 int len = VEC_length (tree, statics);
849 int last_time = (data != 0);
850
851 if (last_time)
852 {
853 check_global_declarations (vec, len);
854 emit_debug_global_declarations (vec, len);
855 return 0;
856 }
857
858 /* Write out any globals that need to be output. */
859 return wrapup_global_declarations (vec, len);
860 }
861
862
863 /* In C++, you don't have to write `struct S' to refer to `S'; you
864 can just use `S'. We accomplish this by creating a TYPE_DECL as
865 if the user had written `typedef struct S S'. Create and return
866 the TYPE_DECL for TYPE. */
867
868 tree
create_implicit_typedef(tree name,tree type)869 create_implicit_typedef (tree name, tree type)
870 {
871 tree decl;
872
873 decl = build_decl (TYPE_DECL, name, type);
874 DECL_ARTIFICIAL (decl) = 1;
875 /* There are other implicit type declarations, like the one *within*
876 a class that allows you to write `S::S'. We must distinguish
877 amongst these. */
878 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
879 TYPE_NAME (type) = decl;
880
881 return decl;
882 }
883
884 /* Remember a local name for name-mangling purposes. */
885
886 static void
push_local_name(tree decl)887 push_local_name (tree decl)
888 {
889 size_t i, nelts;
890 tree t, name;
891
892 timevar_push (TV_NAME_LOOKUP);
893
894 name = DECL_NAME (decl);
895
896 nelts = VEC_length (tree, local_names);
897 for (i = 0; i < nelts; i++)
898 {
899 t = VEC_index (tree, local_names, i);
900 if (DECL_NAME (t) == name)
901 {
902 if (!DECL_LANG_SPECIFIC (decl))
903 retrofit_lang_decl (decl);
904 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
905 if (DECL_LANG_SPECIFIC (t))
906 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
907 else
908 DECL_DISCRIMINATOR (decl) = 1;
909
910 VEC_replace (tree, local_names, i, decl);
911 timevar_pop (TV_NAME_LOOKUP);
912 return;
913 }
914 }
915
916 VEC_safe_push (tree, gc, local_names, decl);
917 timevar_pop (TV_NAME_LOOKUP);
918 }
919
920 /* Subroutine of duplicate_decls: return truthvalue of whether
921 or not types of these decls match.
922
923 For C++, we must compare the parameter list so that `int' can match
924 `int&' in a parameter position, but `int&' is not confused with
925 `const int&'. */
926
927 int
decls_match(tree newdecl,tree olddecl)928 decls_match (tree newdecl, tree olddecl)
929 {
930 int types_match;
931
932 if (newdecl == olddecl)
933 return 1;
934
935 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
936 /* If the two DECLs are not even the same kind of thing, we're not
937 interested in their types. */
938 return 0;
939
940 if (TREE_CODE (newdecl) == FUNCTION_DECL)
941 {
942 tree f1 = TREE_TYPE (newdecl);
943 tree f2 = TREE_TYPE (olddecl);
944 tree p1 = TYPE_ARG_TYPES (f1);
945 tree p2 = TYPE_ARG_TYPES (f2);
946
947 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
948 && ! (DECL_EXTERN_C_P (newdecl)
949 && DECL_EXTERN_C_P (olddecl)))
950 return 0;
951
952 if (TREE_CODE (f1) != TREE_CODE (f2))
953 return 0;
954
955 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
956 {
957 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
958 && (DECL_BUILT_IN (olddecl)
959 #ifndef NO_IMPLICIT_EXTERN_C
960 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
961 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
962 #endif
963 ))
964 {
965 types_match = self_promoting_args_p (p1);
966 if (p1 == void_list_node)
967 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
968 }
969 #ifndef NO_IMPLICIT_EXTERN_C
970 else if (p1 == NULL_TREE
971 && (DECL_EXTERN_C_P (olddecl)
972 && DECL_IN_SYSTEM_HEADER (olddecl)
973 && !DECL_CLASS_SCOPE_P (olddecl))
974 && (DECL_EXTERN_C_P (newdecl)
975 && DECL_IN_SYSTEM_HEADER (newdecl)
976 && !DECL_CLASS_SCOPE_P (newdecl)))
977 {
978 types_match = self_promoting_args_p (p2);
979 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
980 }
981 #endif
982 else
983 types_match = compparms (p1, p2);
984 }
985 else
986 types_match = 0;
987 }
988 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
989 {
990 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
991 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
992 return 0;
993
994 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
995 DECL_TEMPLATE_PARMS (olddecl)))
996 return 0;
997
998 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
999 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
1000 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
1001 else
1002 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
1003 DECL_TEMPLATE_RESULT (newdecl));
1004 }
1005 else
1006 {
1007 /* Need to check scope for variable declaration (VAR_DECL).
1008 For typedef (TYPE_DECL), scope is ignored. */
1009 if (TREE_CODE (newdecl) == VAR_DECL
1010 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1011 /* [dcl.link]
1012 Two declarations for an object with C language linkage
1013 with the same name (ignoring the namespace that qualify
1014 it) that appear in different namespace scopes refer to
1015 the same object. */
1016 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1017 return 0;
1018
1019 if (TREE_TYPE (newdecl) == error_mark_node)
1020 types_match = TREE_TYPE (olddecl) == error_mark_node;
1021 else if (TREE_TYPE (olddecl) == NULL_TREE)
1022 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1023 else if (TREE_TYPE (newdecl) == NULL_TREE)
1024 types_match = 0;
1025 else
1026 types_match = comptypes (TREE_TYPE (newdecl),
1027 TREE_TYPE (olddecl),
1028 COMPARE_REDECLARATION);
1029 }
1030
1031 return types_match;
1032 }
1033
1034 /* If NEWDECL is `static' and an `extern' was seen previously,
1035 warn about it. OLDDECL is the previous declaration.
1036
1037 Note that this does not apply to the C++ case of declaring
1038 a variable `extern const' and then later `const'.
1039
1040 Don't complain about built-in functions, since they are beyond
1041 the user's control. */
1042
1043 void
warn_extern_redeclared_static(tree newdecl,tree olddecl)1044 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1045 {
1046 tree name;
1047
1048 if (TREE_CODE (newdecl) == TYPE_DECL
1049 || TREE_CODE (newdecl) == TEMPLATE_DECL
1050 || TREE_CODE (newdecl) == CONST_DECL
1051 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1052 return;
1053
1054 /* Don't get confused by static member functions; that's a different
1055 use of `static'. */
1056 if (TREE_CODE (newdecl) == FUNCTION_DECL
1057 && DECL_STATIC_FUNCTION_P (newdecl))
1058 return;
1059
1060 /* If the old declaration was `static', or the new one isn't, then
1061 then everything is OK. */
1062 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1063 return;
1064
1065 /* It's OK to declare a builtin function as `static'. */
1066 if (TREE_CODE (olddecl) == FUNCTION_DECL
1067 && DECL_ARTIFICIAL (olddecl))
1068 return;
1069
1070 name = DECL_ASSEMBLER_NAME (newdecl);
1071 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
1072 pedwarn ("previous declaration of %q+D", olddecl);
1073 }
1074
1075 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1076 function templates. If their exception specifications do not
1077 match, issue an a diagnostic. */
1078
1079 static void
check_redeclaration_exception_specification(tree new_decl,tree old_decl)1080 check_redeclaration_exception_specification (tree new_decl,
1081 tree old_decl)
1082 {
1083 tree new_type;
1084 tree old_type;
1085 tree new_exceptions;
1086 tree old_exceptions;
1087
1088 new_type = TREE_TYPE (new_decl);
1089 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1090 old_type = TREE_TYPE (old_decl);
1091 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1092
1093 /* [except.spec]
1094
1095 If any declaration of a function has an exception-specification,
1096 all declarations, including the definition and an explicit
1097 specialization, of that function shall have an
1098 exception-specification with the same set of type-ids. */
1099 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1100 && ! DECL_IS_BUILTIN (old_decl)
1101 && flag_exceptions
1102 && !comp_except_specs (new_exceptions, old_exceptions,
1103 /*exact=*/true))
1104 {
1105 error ("declaration of %qF throws different exceptions", new_decl);
1106 error ("from previous declaration %q+F", old_decl);
1107 }
1108 }
1109
1110 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1111 If the redeclaration is invalid, a diagnostic is issued, and the
1112 error_mark_node is returned. Otherwise, OLDDECL is returned.
1113
1114 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1115 returned.
1116
1117 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1118
1119 tree
duplicate_decls(tree newdecl,tree olddecl,bool newdecl_is_friend)1120 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1121 {
1122 unsigned olddecl_uid = DECL_UID (olddecl);
1123 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1124 int new_defines_function = 0;
1125 tree new_template;
1126
1127 if (newdecl == olddecl)
1128 return olddecl;
1129
1130 types_match = decls_match (newdecl, olddecl);
1131
1132 /* If either the type of the new decl or the type of the old decl is an
1133 error_mark_node, then that implies that we have already issued an
1134 error (earlier) for some bogus type specification, and in that case,
1135 it is rather pointless to harass the user with yet more error message
1136 about the same declaration, so just pretend the types match here. */
1137 if (TREE_TYPE (newdecl) == error_mark_node
1138 || TREE_TYPE (olddecl) == error_mark_node)
1139 return error_mark_node;
1140
1141 if (DECL_P (olddecl)
1142 && TREE_CODE (newdecl) == FUNCTION_DECL
1143 && TREE_CODE (olddecl) == FUNCTION_DECL
1144 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1145 {
1146 if (DECL_DECLARED_INLINE_P (newdecl)
1147 && DECL_UNINLINABLE (newdecl)
1148 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1149 /* Already warned elsewhere. */;
1150 else if (DECL_DECLARED_INLINE_P (olddecl)
1151 && DECL_UNINLINABLE (olddecl)
1152 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1153 /* Already warned. */;
1154 else if (DECL_DECLARED_INLINE_P (newdecl)
1155 && DECL_UNINLINABLE (olddecl)
1156 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1157 {
1158 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1159 newdecl);
1160 warning (OPT_Wattributes, "previous declaration of %q+D "
1161 "with attribute noinline", olddecl);
1162 }
1163 else if (DECL_DECLARED_INLINE_P (olddecl)
1164 && DECL_UNINLINABLE (newdecl)
1165 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1166 {
1167 warning (OPT_Wattributes, "function %q+D redeclared with "
1168 "attribute noinline", newdecl);
1169 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1170 olddecl);
1171 }
1172 }
1173
1174 /* Check for redeclaration and other discrepancies. */
1175 if (TREE_CODE (olddecl) == FUNCTION_DECL
1176 && DECL_ARTIFICIAL (olddecl))
1177 {
1178 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1179 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1180 {
1181 /* Avoid warnings redeclaring built-ins which have not been
1182 explicitly declared. */
1183 if (DECL_ANTICIPATED (olddecl))
1184 return NULL_TREE;
1185
1186 /* If you declare a built-in or predefined function name as static,
1187 the old definition is overridden, but optionally warn this was a
1188 bad choice of name. */
1189 if (! TREE_PUBLIC (newdecl))
1190 {
1191 warning (OPT_Wshadow, "shadowing %s function %q#D",
1192 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1193 olddecl);
1194 /* Discard the old built-in function. */
1195 return NULL_TREE;
1196 }
1197 /* If the built-in is not ansi, then programs can override
1198 it even globally without an error. */
1199 else if (! DECL_BUILT_IN (olddecl))
1200 warning (0, "library function %q#D redeclared as non-function %q#D",
1201 olddecl, newdecl);
1202 else
1203 {
1204 error ("declaration of %q#D", newdecl);
1205 error ("conflicts with built-in declaration %q#D",
1206 olddecl);
1207 }
1208 return NULL_TREE;
1209 }
1210 else if (!types_match)
1211 {
1212 /* Avoid warnings redeclaring built-ins which have not been
1213 explicitly declared. */
1214 if (DECL_ANTICIPATED (olddecl))
1215 {
1216 /* Deal with fileptr_type_node. FILE type is not known
1217 at the time we create the builtins. */
1218 tree t1, t2;
1219
1220 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1221 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1222 t1 || t2;
1223 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1224 if (!t1 || !t2)
1225 break;
1226 else if (TREE_VALUE (t2) == fileptr_type_node)
1227 {
1228 tree t = TREE_VALUE (t1);
1229
1230 if (TREE_CODE (t) == POINTER_TYPE
1231 && TYPE_NAME (TREE_TYPE (t))
1232 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1233 == get_identifier ("FILE")
1234 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1235 {
1236 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1237
1238 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1239 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1240 types_match = decls_match (newdecl, olddecl);
1241 if (types_match)
1242 return duplicate_decls (newdecl, olddecl,
1243 newdecl_is_friend);
1244 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1245 }
1246 }
1247 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1248 break;
1249 }
1250 else if ((DECL_EXTERN_C_P (newdecl)
1251 && DECL_EXTERN_C_P (olddecl))
1252 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1253 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1254 {
1255 /* A near match; override the builtin. */
1256
1257 if (TREE_PUBLIC (newdecl))
1258 {
1259 warning (0, "new declaration %q#D", newdecl);
1260 warning (0, "ambiguates built-in declaration %q#D",
1261 olddecl);
1262 }
1263 else
1264 warning (OPT_Wshadow, "shadowing %s function %q#D",
1265 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1266 olddecl);
1267 }
1268 else
1269 /* Discard the old built-in function. */
1270 return NULL_TREE;
1271
1272 /* Replace the old RTL to avoid problems with inlining. */
1273 COPY_DECL_RTL (newdecl, olddecl);
1274 }
1275 /* Even if the types match, prefer the new declarations type for
1276 built-ins which have not been explicitly declared, for
1277 exception lists, etc... */
1278 else if (DECL_ANTICIPATED (olddecl))
1279 {
1280 tree type = TREE_TYPE (newdecl);
1281 tree attribs = (*targetm.merge_type_attributes)
1282 (TREE_TYPE (olddecl), type);
1283
1284 type = cp_build_type_attribute_variant (type, attribs);
1285 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1286 }
1287
1288 /* Whether or not the builtin can throw exceptions has no
1289 bearing on this declarator. */
1290 TREE_NOTHROW (olddecl) = 0;
1291
1292 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1293 {
1294 /* If a builtin function is redeclared as `static', merge
1295 the declarations, but make the original one static. */
1296 DECL_THIS_STATIC (olddecl) = 1;
1297 TREE_PUBLIC (olddecl) = 0;
1298
1299 /* Make the old declaration consistent with the new one so
1300 that all remnants of the builtin-ness of this function
1301 will be banished. */
1302 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1303 COPY_DECL_RTL (newdecl, olddecl);
1304 }
1305 }
1306 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1307 {
1308 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1309 && TREE_CODE (newdecl) != TYPE_DECL
1310 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1311 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1312 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1313 && TREE_CODE (olddecl) != TYPE_DECL
1314 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1315 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1316 == TYPE_DECL))))
1317 {
1318 /* We do nothing special here, because C++ does such nasty
1319 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1320 get shadowed, and know that if we need to find a TYPE_DECL
1321 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1322 slot of the identifier. */
1323 return NULL_TREE;
1324 }
1325
1326 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1327 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1328 || (TREE_CODE (olddecl) == FUNCTION_DECL
1329 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1330 return NULL_TREE;
1331
1332 error ("%q#D redeclared as different kind of symbol", newdecl);
1333 if (TREE_CODE (olddecl) == TREE_LIST)
1334 olddecl = TREE_VALUE (olddecl);
1335 error ("previous declaration of %q+#D", olddecl);
1336
1337 return error_mark_node;
1338 }
1339 else if (!types_match)
1340 {
1341 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1342 /* These are certainly not duplicate declarations; they're
1343 from different scopes. */
1344 return NULL_TREE;
1345
1346 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1347 {
1348 /* The name of a class template may not be declared to refer to
1349 any other template, class, function, object, namespace, value,
1350 or type in the same scope. */
1351 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1352 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1353 {
1354 error ("declaration of template %q#D", newdecl);
1355 error ("conflicts with previous declaration %q+#D", olddecl);
1356 }
1357 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1358 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1359 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1360 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1361 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1362 DECL_TEMPLATE_PARMS (olddecl))
1363 /* Template functions can be disambiguated by
1364 return type. */
1365 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1366 TREE_TYPE (TREE_TYPE (olddecl))))
1367 {
1368 error ("new declaration %q#D", newdecl);
1369 error ("ambiguates old declaration %q+#D", olddecl);
1370 }
1371 return NULL_TREE;
1372 }
1373 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1374 {
1375 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1376 {
1377 error ("declaration of C function %q#D conflicts with",
1378 newdecl);
1379 error ("previous declaration %q+#D here", olddecl);
1380 }
1381 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1382 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1383 {
1384 error ("new declaration %q#D", newdecl);
1385 error ("ambiguates old declaration %q+#D", olddecl);
1386 return error_mark_node;
1387 }
1388 else
1389 return NULL_TREE;
1390 }
1391 else
1392 {
1393 error ("conflicting declaration %q#D", newdecl);
1394 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1395 return error_mark_node;
1396 }
1397 }
1398 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1399 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1400 && (!DECL_TEMPLATE_INFO (newdecl)
1401 || (DECL_TI_TEMPLATE (newdecl)
1402 != DECL_TI_TEMPLATE (olddecl))))
1403 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1404 && (!DECL_TEMPLATE_INFO (olddecl)
1405 || (DECL_TI_TEMPLATE (olddecl)
1406 != DECL_TI_TEMPLATE (newdecl))))))
1407 /* It's OK to have a template specialization and a non-template
1408 with the same type, or to have specializations of two
1409 different templates with the same type. Note that if one is a
1410 specialization, and the other is an instantiation of the same
1411 template, that we do not exit at this point. That situation
1412 can occur if we instantiate a template class, and then
1413 specialize one of its methods. This situation is valid, but
1414 the declarations must be merged in the usual way. */
1415 return NULL_TREE;
1416 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1417 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1418 && !DECL_USE_TEMPLATE (newdecl))
1419 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1420 && !DECL_USE_TEMPLATE (olddecl))))
1421 /* One of the declarations is a template instantiation, and the
1422 other is not a template at all. That's OK. */
1423 return NULL_TREE;
1424 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1425 {
1426 /* In [namespace.alias] we have:
1427
1428 In a declarative region, a namespace-alias-definition can be
1429 used to redefine a namespace-alias declared in that declarative
1430 region to refer only to the namespace to which it already
1431 refers.
1432
1433 Therefore, if we encounter a second alias directive for the same
1434 alias, we can just ignore the second directive. */
1435 if (DECL_NAMESPACE_ALIAS (newdecl)
1436 && (DECL_NAMESPACE_ALIAS (newdecl)
1437 == DECL_NAMESPACE_ALIAS (olddecl)))
1438 return olddecl;
1439 /* [namespace.alias]
1440
1441 A namespace-name or namespace-alias shall not be declared as
1442 the name of any other entity in the same declarative region.
1443 A namespace-name defined at global scope shall not be
1444 declared as the name of any other entity in any global scope
1445 of the program. */
1446 error ("declaration of namespace %qD conflicts with", newdecl);
1447 error ("previous declaration of namespace %q+D here", olddecl);
1448 return error_mark_node;
1449 }
1450 else
1451 {
1452 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1453 if (errmsg)
1454 {
1455 error (errmsg, newdecl);
1456 if (DECL_NAME (olddecl) != NULL_TREE)
1457 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1458 ? "%q+#D previously defined here"
1459 : "%q+#D previously declared here", olddecl);
1460 return error_mark_node;
1461 }
1462 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1463 && DECL_INITIAL (olddecl) != NULL_TREE
1464 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1465 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1466 {
1467 /* Prototype decl follows defn w/o prototype. */
1468 warning (0, "prototype for %q+#D", newdecl);
1469 warning (0, "%Jfollows non-prototype definition here", olddecl);
1470 }
1471 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1472 || TREE_CODE (olddecl) == VAR_DECL)
1473 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1474 {
1475 /* [dcl.link]
1476 If two declarations of the same function or object
1477 specify different linkage-specifications ..., the program
1478 is ill-formed.... Except for functions with C++ linkage,
1479 a function declaration without a linkage specification
1480 shall not precede the first linkage specification for
1481 that function. A function can be declared without a
1482 linkage specification after an explicit linkage
1483 specification has been seen; the linkage explicitly
1484 specified in the earlier declaration is not affected by
1485 such a function declaration.
1486
1487 DR 563 raises the question why the restrictions on
1488 functions should not also apply to objects. Older
1489 versions of G++ silently ignore the linkage-specification
1490 for this example:
1491
1492 namespace N {
1493 extern int i;
1494 extern "C" int i;
1495 }
1496
1497 which is clearly wrong. Therefore, we now treat objects
1498 like functions. */
1499 if (current_lang_depth () == 0)
1500 {
1501 /* There is no explicit linkage-specification, so we use
1502 the linkage from the previous declaration. */
1503 if (!DECL_LANG_SPECIFIC (newdecl))
1504 retrofit_lang_decl (newdecl);
1505 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1506 }
1507 else
1508 {
1509 error ("previous declaration of %q+#D with %qL linkage",
1510 olddecl, DECL_LANGUAGE (olddecl));
1511 error ("conflicts with new declaration with %qL linkage",
1512 DECL_LANGUAGE (newdecl));
1513 }
1514 }
1515
1516 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1517 ;
1518 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1519 {
1520 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1521 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1522 int i = 1;
1523
1524 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1525 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1526
1527 for (; t1 && t1 != void_list_node;
1528 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1529 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1530 {
1531 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1532 TREE_PURPOSE (t2)))
1533 {
1534 pedwarn ("default argument given for parameter %d of %q#D",
1535 i, newdecl);
1536 pedwarn ("after previous specification in %q+#D", olddecl);
1537 }
1538 else
1539 {
1540 error ("default argument given for parameter %d of %q#D",
1541 i, newdecl);
1542 error ("after previous specification in %q+#D",
1543 olddecl);
1544 }
1545 }
1546
1547 if (DECL_DECLARED_INLINE_P (newdecl)
1548 && ! DECL_DECLARED_INLINE_P (olddecl)
1549 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1550 {
1551 warning (0, "%q#D was used before it was declared inline", newdecl);
1552 warning (0, "%Jprevious non-inline declaration here", olddecl);
1553 }
1554 }
1555 }
1556
1557 /* Do not merge an implicit typedef with an explicit one. In:
1558
1559 class A;
1560 ...
1561 typedef class A A __attribute__ ((foo));
1562
1563 the attribute should apply only to the typedef. */
1564 if (TREE_CODE (olddecl) == TYPE_DECL
1565 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1566 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1567 return NULL_TREE;
1568
1569 /* If new decl is `static' and an `extern' was seen previously,
1570 warn about it. */
1571 warn_extern_redeclared_static (newdecl, olddecl);
1572
1573 /* We have committed to returning 1 at this point. */
1574 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1575 {
1576 /* Now that functions must hold information normally held
1577 by field decls, there is extra work to do so that
1578 declaration information does not get destroyed during
1579 definition. */
1580 if (DECL_VINDEX (olddecl))
1581 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1582 if (DECL_CONTEXT (olddecl))
1583 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1584 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1585 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1586 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1587 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1588 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1589 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1590 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1591 SET_OVERLOADED_OPERATOR_CODE
1592 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1593 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1594
1595 /* Optionally warn about more than one declaration for the same
1596 name, but don't warn about a function declaration followed by a
1597 definition. */
1598 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1599 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1600 /* Don't warn about extern decl followed by definition. */
1601 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1602 /* Don't warn about friends, let add_friend take care of it. */
1603 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1604 {
1605 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1606 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1607 }
1608 }
1609
1610 /* Deal with C++: must preserve virtual function table size. */
1611 if (TREE_CODE (olddecl) == TYPE_DECL)
1612 {
1613 tree newtype = TREE_TYPE (newdecl);
1614 tree oldtype = TREE_TYPE (olddecl);
1615
1616 if (newtype != error_mark_node && oldtype != error_mark_node
1617 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1618 CLASSTYPE_FRIEND_CLASSES (newtype)
1619 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1620
1621 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1622 }
1623
1624 /* Copy all the DECL_... slots specified in the new decl
1625 except for any that we copy here from the old type. */
1626 DECL_ATTRIBUTES (newdecl)
1627 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1628
1629 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1630 {
1631 tree old_result;
1632 tree new_result;
1633 old_result = DECL_TEMPLATE_RESULT (olddecl);
1634 new_result = DECL_TEMPLATE_RESULT (newdecl);
1635 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1636 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1637 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1638 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1639
1640 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1641 {
1642 DECL_INLINE (old_result)
1643 |= DECL_INLINE (new_result);
1644 DECL_DECLARED_INLINE_P (old_result)
1645 |= DECL_DECLARED_INLINE_P (new_result);
1646 check_redeclaration_exception_specification (newdecl, olddecl);
1647 }
1648
1649 /* If the new declaration is a definition, update the file and
1650 line information on the declaration, and also make
1651 the old declaration the same definition. */
1652 if (DECL_INITIAL (old_result) == NULL_TREE
1653 && DECL_INITIAL (new_result) != NULL_TREE)
1654 {
1655 DECL_SOURCE_LOCATION (olddecl)
1656 = DECL_SOURCE_LOCATION (old_result)
1657 = DECL_SOURCE_LOCATION (newdecl);
1658 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1659 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1660 DECL_ARGUMENTS (old_result)
1661 = DECL_ARGUMENTS (new_result);
1662 }
1663
1664 return olddecl;
1665 }
1666
1667 if (types_match)
1668 {
1669 /* Automatically handles default parameters. */
1670 tree oldtype = TREE_TYPE (olddecl);
1671 tree newtype;
1672
1673 /* Merge the data types specified in the two decls. */
1674 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1675
1676 /* If merge_types produces a non-typedef type, just use the old type. */
1677 if (TREE_CODE (newdecl) == TYPE_DECL
1678 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1679 newtype = oldtype;
1680
1681 if (TREE_CODE (newdecl) == VAR_DECL)
1682 {
1683 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1684 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1685 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1686 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1687 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1688 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1689
1690 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1691 if (DECL_LANG_SPECIFIC (olddecl)
1692 && CP_DECL_THREADPRIVATE_P (olddecl))
1693 {
1694 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1695 if (!DECL_LANG_SPECIFIC (newdecl))
1696 retrofit_lang_decl (newdecl);
1697
1698 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1699 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1700 }
1701 }
1702
1703 /* Do this after calling `merge_types' so that default
1704 parameters don't confuse us. */
1705 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1706 check_redeclaration_exception_specification (newdecl, olddecl);
1707 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1708
1709 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1710 check_default_args (newdecl);
1711
1712 /* Lay the type out, unless already done. */
1713 if (! same_type_p (newtype, oldtype)
1714 && TREE_TYPE (newdecl) != error_mark_node
1715 && !(processing_template_decl && uses_template_parms (newdecl)))
1716 layout_type (TREE_TYPE (newdecl));
1717
1718 if ((TREE_CODE (newdecl) == VAR_DECL
1719 || TREE_CODE (newdecl) == PARM_DECL
1720 || TREE_CODE (newdecl) == RESULT_DECL
1721 || TREE_CODE (newdecl) == FIELD_DECL
1722 || TREE_CODE (newdecl) == TYPE_DECL)
1723 && !(processing_template_decl && uses_template_parms (newdecl)))
1724 layout_decl (newdecl, 0);
1725
1726 /* Merge the type qualifiers. */
1727 if (TREE_READONLY (newdecl))
1728 TREE_READONLY (olddecl) = 1;
1729 if (TREE_THIS_VOLATILE (newdecl))
1730 TREE_THIS_VOLATILE (olddecl) = 1;
1731 if (TREE_NOTHROW (newdecl))
1732 TREE_NOTHROW (olddecl) = 1;
1733
1734 /* Merge deprecatedness. */
1735 if (TREE_DEPRECATED (newdecl))
1736 TREE_DEPRECATED (olddecl) = 1;
1737
1738 /* Merge the initialization information. */
1739 if (DECL_INITIAL (newdecl) == NULL_TREE
1740 && DECL_INITIAL (olddecl) != NULL_TREE)
1741 {
1742 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1743 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1744 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1745 && DECL_LANG_SPECIFIC (newdecl)
1746 && DECL_LANG_SPECIFIC (olddecl))
1747 {
1748 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1749 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1750 }
1751 }
1752
1753 /* Merge the section attribute.
1754 We want to issue an error if the sections conflict but that must be
1755 done later in decl_attributes since we are called before attributes
1756 are assigned. */
1757 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1758 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1759
1760 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1761 {
1762 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1763 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1764 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1765 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1766 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1767 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1768 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1769 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1770 /* Keep the old RTL. */
1771 COPY_DECL_RTL (olddecl, newdecl);
1772 }
1773 else if (TREE_CODE (newdecl) == VAR_DECL
1774 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1775 {
1776 /* Keep the old RTL. We cannot keep the old RTL if the old
1777 declaration was for an incomplete object and the new
1778 declaration is not since many attributes of the RTL will
1779 change. */
1780 COPY_DECL_RTL (olddecl, newdecl);
1781 }
1782 }
1783 /* If cannot merge, then use the new type and qualifiers,
1784 and don't preserve the old rtl. */
1785 else
1786 {
1787 /* Clean out any memory we had of the old declaration. */
1788 tree oldstatic = value_member (olddecl, static_aggregates);
1789 if (oldstatic)
1790 TREE_VALUE (oldstatic) = error_mark_node;
1791
1792 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1793 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1794 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1795 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1796 }
1797
1798 /* Merge the storage class information. */
1799 merge_weak (newdecl, olddecl);
1800
1801 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1802 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1803 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1804 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1805 if (! DECL_EXTERNAL (olddecl))
1806 DECL_EXTERNAL (newdecl) = 0;
1807
1808 new_template = NULL_TREE;
1809 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1810 {
1811 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1812 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1813 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1814 DECL_TEMPLATE_INSTANTIATED (newdecl)
1815 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1816
1817 /* If the OLDDECL is an instantiation and/or specialization,
1818 then the NEWDECL must be too. But, it may not yet be marked
1819 as such if the caller has created NEWDECL, but has not yet
1820 figured out that it is a redeclaration. */
1821 if (!DECL_USE_TEMPLATE (newdecl))
1822 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1823
1824 /* Don't really know how much of the language-specific
1825 values we should copy from old to new. */
1826 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1827 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1828 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1829 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1830 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1831 if (DECL_TEMPLATE_INFO (newdecl))
1832 new_template = DECL_TI_TEMPLATE (newdecl);
1833 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1834 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1835 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1836 olddecl_friend = DECL_FRIEND_P (olddecl);
1837 hidden_friend = (DECL_ANTICIPATED (olddecl)
1838 && DECL_HIDDEN_FRIEND_P (olddecl)
1839 && newdecl_is_friend);
1840
1841 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1842 if (TREE_CODE (newdecl) == FUNCTION_DECL
1843 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1844 {
1845 DECL_BEFRIENDING_CLASSES (newdecl)
1846 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1847 DECL_BEFRIENDING_CLASSES (olddecl));
1848 /* DECL_THUNKS is only valid for virtual functions,
1849 otherwise it is a DECL_FRIEND_CONTEXT. */
1850 if (DECL_VIRTUAL_P (newdecl))
1851 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1852 }
1853 }
1854
1855 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1856 {
1857 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1858 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1859 {
1860 /* If newdecl is not a specialization, then it is not a
1861 template-related function at all. And that means that we
1862 should have exited above, returning 0. */
1863 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1864
1865 if (TREE_USED (olddecl))
1866 /* From [temp.expl.spec]:
1867
1868 If a template, a member template or the member of a class
1869 template is explicitly specialized then that
1870 specialization shall be declared before the first use of
1871 that specialization that would cause an implicit
1872 instantiation to take place, in every translation unit in
1873 which such a use occurs. */
1874 error ("explicit specialization of %qD after first use",
1875 olddecl);
1876
1877 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1878
1879 /* Don't propagate visibility from the template to the
1880 specialization here. We'll do that in determine_visibility if
1881 appropriate. */
1882 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1883
1884 /* [temp.expl.spec/14] We don't inline explicit specialization
1885 just because the primary template says so. */
1886 }
1887 else
1888 {
1889 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1890 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1891
1892 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1893
1894 /* If either decl says `inline', this fn is inline, unless
1895 its definition was passed already. */
1896 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1897 DECL_INLINE (olddecl) = 1;
1898 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1899
1900 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1901 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1902 }
1903
1904 /* Preserve abstractness on cloned [cd]tors. */
1905 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1906
1907 if (! types_match)
1908 {
1909 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1910 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1911 COPY_DECL_RTL (newdecl, olddecl);
1912 }
1913 if (! types_match || new_defines_function)
1914 {
1915 /* These need to be copied so that the names are available.
1916 Note that if the types do match, we'll preserve inline
1917 info and other bits, but if not, we won't. */
1918 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1919 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1920 }
1921 if (new_defines_function)
1922 /* If defining a function declared with other language
1923 linkage, use the previously declared language linkage. */
1924 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1925 else if (types_match)
1926 {
1927 /* If redeclaring a builtin function, and not a definition,
1928 it stays built in. */
1929 if (DECL_BUILT_IN (olddecl))
1930 {
1931 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1932 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1933 /* If we're keeping the built-in definition, keep the rtl,
1934 regardless of declaration matches. */
1935 COPY_DECL_RTL (olddecl, newdecl);
1936 }
1937
1938 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1939 /* Don't clear out the arguments if we're redefining a function. */
1940 if (DECL_ARGUMENTS (olddecl))
1941 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1942 }
1943 }
1944 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1945 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1946
1947 /* Now preserve various other info from the definition. */
1948 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1949 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1950 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1951 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1952
1953 /* Warn about conflicting visibility specifications. */
1954 if (DECL_VISIBILITY_SPECIFIED (olddecl)
1955 && DECL_VISIBILITY_SPECIFIED (newdecl)
1956 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1957 {
1958 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
1959 "because it", newdecl);
1960 warning (OPT_Wattributes, "%Jconflicts with previous "
1961 "declaration here", olddecl);
1962 }
1963 /* Choose the declaration which specified visibility. */
1964 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1965 {
1966 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1967 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1968 }
1969 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
1970 so keep this behavior. */
1971 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
1972 {
1973 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
1974 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
1975 }
1976
1977 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1978 with that from NEWDECL below. */
1979 if (DECL_LANG_SPECIFIC (olddecl))
1980 {
1981 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1982 != DECL_LANG_SPECIFIC (newdecl));
1983 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1984 }
1985
1986 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1987 {
1988 int function_size;
1989
1990 function_size = sizeof (struct tree_decl_common);
1991
1992 memcpy ((char *) olddecl + sizeof (struct tree_common),
1993 (char *) newdecl + sizeof (struct tree_common),
1994 function_size - sizeof (struct tree_common));
1995
1996 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1997 (char *) newdecl + sizeof (struct tree_decl_common),
1998 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
1999 if (new_template)
2000 /* If newdecl is a template instantiation, it is possible that
2001 the following sequence of events has occurred:
2002
2003 o A friend function was declared in a class template. The
2004 class template was instantiated.
2005
2006 o The instantiation of the friend declaration was
2007 recorded on the instantiation list, and is newdecl.
2008
2009 o Later, however, instantiate_class_template called pushdecl
2010 on the newdecl to perform name injection. But, pushdecl in
2011 turn called duplicate_decls when it discovered that another
2012 declaration of a global function with the same name already
2013 existed.
2014
2015 o Here, in duplicate_decls, we decided to clobber newdecl.
2016
2017 If we're going to do that, we'd better make sure that
2018 olddecl, and not newdecl, is on the list of
2019 instantiations so that if we try to do the instantiation
2020 again we won't get the clobbered declaration. */
2021 reregister_specialization (newdecl,
2022 new_template,
2023 olddecl);
2024 }
2025 else
2026 {
2027 size_t size = tree_code_size (TREE_CODE (olddecl));
2028 memcpy ((char *) olddecl + sizeof (struct tree_common),
2029 (char *) newdecl + sizeof (struct tree_common),
2030 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2031 switch (TREE_CODE (olddecl))
2032 {
2033 case LABEL_DECL:
2034 case VAR_DECL:
2035 case RESULT_DECL:
2036 case PARM_DECL:
2037 case FIELD_DECL:
2038 case TYPE_DECL:
2039 case CONST_DECL:
2040 {
2041 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2042 (char *) newdecl + sizeof (struct tree_decl_common),
2043 size - sizeof (struct tree_decl_common)
2044 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2045 }
2046 break;
2047 default:
2048 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2049 (char *) newdecl + sizeof (struct tree_decl_common),
2050 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2051 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2052 break;
2053 }
2054 }
2055 DECL_UID (olddecl) = olddecl_uid;
2056 if (olddecl_friend)
2057 DECL_FRIEND_P (olddecl) = 1;
2058 if (hidden_friend)
2059 {
2060 DECL_ANTICIPATED (olddecl) = 1;
2061 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2062 }
2063
2064 /* NEWDECL contains the merged attribute lists.
2065 Update OLDDECL to be the same. */
2066 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2067
2068 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2069 so that encode_section_info has a chance to look at the new decl
2070 flags and attributes. */
2071 if (DECL_RTL_SET_P (olddecl)
2072 && (TREE_CODE (olddecl) == FUNCTION_DECL
2073 || (TREE_CODE (olddecl) == VAR_DECL
2074 && TREE_STATIC (olddecl))))
2075 make_decl_rtl (olddecl);
2076
2077 /* The NEWDECL will no longer be needed. Because every out-of-class
2078 declaration of a member results in a call to duplicate_decls,
2079 freeing these nodes represents in a significant savings. */
2080 ggc_free (newdecl);
2081
2082 return olddecl;
2083 }
2084
2085 /* Return zero if the declaration NEWDECL is valid
2086 when the declaration OLDDECL (assumed to be for the same name)
2087 has already been seen.
2088 Otherwise return an error message format string with a %s
2089 where the identifier should go. */
2090
2091 static const char *
redeclaration_error_message(tree newdecl,tree olddecl)2092 redeclaration_error_message (tree newdecl, tree olddecl)
2093 {
2094 if (TREE_CODE (newdecl) == TYPE_DECL)
2095 {
2096 /* Because C++ can put things into name space for free,
2097 constructs like "typedef struct foo { ... } foo"
2098 would look like an erroneous redeclaration. */
2099 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2100 return NULL;
2101 else
2102 return "redefinition of %q#D";
2103 }
2104 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2105 {
2106 /* If this is a pure function, its olddecl will actually be
2107 the original initialization to `0' (which we force to call
2108 abort()). Don't complain about redefinition in this case. */
2109 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2110 && DECL_INITIAL (olddecl) == NULL_TREE)
2111 return NULL;
2112
2113 /* If both functions come from different namespaces, this is not
2114 a redeclaration - this is a conflict with a used function. */
2115 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2116 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2117 && ! decls_match (olddecl, newdecl))
2118 return "%qD conflicts with used function";
2119
2120 /* We'll complain about linkage mismatches in
2121 warn_extern_redeclared_static. */
2122
2123 /* Defining the same name twice is no good. */
2124 if (DECL_INITIAL (olddecl) != NULL_TREE
2125 && DECL_INITIAL (newdecl) != NULL_TREE)
2126 {
2127 if (DECL_NAME (olddecl) == NULL_TREE)
2128 return "%q#D not declared in class";
2129 else
2130 return "redefinition of %q#D";
2131 }
2132 return NULL;
2133 }
2134 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2135 {
2136 tree nt, ot;
2137
2138 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2139 {
2140 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2141 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2142 return "redefinition of %q#D";
2143 return NULL;
2144 }
2145
2146 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2147 || (DECL_TEMPLATE_RESULT (newdecl)
2148 == DECL_TEMPLATE_RESULT (olddecl)))
2149 return NULL;
2150
2151 nt = DECL_TEMPLATE_RESULT (newdecl);
2152 if (DECL_TEMPLATE_INFO (nt))
2153 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2154 ot = DECL_TEMPLATE_RESULT (olddecl);
2155 if (DECL_TEMPLATE_INFO (ot))
2156 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2157 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2158 return "redefinition of %q#D";
2159
2160 return NULL;
2161 }
2162 else if (TREE_CODE (newdecl) == VAR_DECL
2163 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2164 && (! DECL_LANG_SPECIFIC (olddecl)
2165 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2166 || DECL_THREAD_LOCAL_P (newdecl)))
2167 {
2168 /* Only variables can be thread-local, and all declarations must
2169 agree on this property. */
2170 if (DECL_THREAD_LOCAL_P (newdecl))
2171 return "thread-local declaration of %q#D follows "
2172 "non-thread-local declaration";
2173 else
2174 return "non-thread-local declaration of %q#D follows "
2175 "thread-local declaration";
2176 }
2177 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2178 {
2179 /* The objects have been declared at namespace scope. If either
2180 is a member of an anonymous union, then this is an invalid
2181 redeclaration. For example:
2182
2183 int i;
2184 union { int i; };
2185
2186 is invalid. */
2187 if (DECL_ANON_UNION_VAR_P (newdecl)
2188 || DECL_ANON_UNION_VAR_P (olddecl))
2189 return "redeclaration of %q#D";
2190 /* If at least one declaration is a reference, there is no
2191 conflict. For example:
2192
2193 int i = 3;
2194 extern int i;
2195
2196 is valid. */
2197 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2198 return NULL;
2199 /* Reject two definitions. */
2200 return "redefinition of %q#D";
2201 }
2202 else
2203 {
2204 /* Objects declared with block scope: */
2205 /* Reject two definitions, and reject a definition
2206 together with an external reference. */
2207 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2208 return "redeclaration of %q#D";
2209 return NULL;
2210 }
2211 }
2212
2213 /* Hash and equality functions for the named_label table. */
2214
2215 static hashval_t
named_label_entry_hash(const void * data)2216 named_label_entry_hash (const void *data)
2217 {
2218 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2219 return DECL_UID (ent->label_decl);
2220 }
2221
2222 static int
named_label_entry_eq(const void * a,const void * b)2223 named_label_entry_eq (const void *a, const void *b)
2224 {
2225 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2226 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2227 return ent_a->label_decl == ent_b->label_decl;
2228 }
2229
2230 /* Create a new label, named ID. */
2231
2232 static tree
make_label_decl(tree id,int local_p)2233 make_label_decl (tree id, int local_p)
2234 {
2235 struct named_label_entry *ent;
2236 void **slot;
2237 tree decl;
2238
2239 decl = build_decl (LABEL_DECL, id, void_type_node);
2240
2241 DECL_CONTEXT (decl) = current_function_decl;
2242 DECL_MODE (decl) = VOIDmode;
2243 C_DECLARED_LABEL_FLAG (decl) = local_p;
2244
2245 /* Say where one reference is to the label, for the sake of the
2246 error if it is not defined. */
2247 DECL_SOURCE_LOCATION (decl) = input_location;
2248
2249 /* Record the fact that this identifier is bound to this label. */
2250 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2251
2252 /* Create the label htab for the function on demand. */
2253 if (!named_labels)
2254 named_labels = htab_create_ggc (13, named_label_entry_hash,
2255 named_label_entry_eq, NULL);
2256
2257 /* Record this label on the list of labels used in this function.
2258 We do this before calling make_label_decl so that we get the
2259 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2260 ent = GGC_CNEW (struct named_label_entry);
2261 ent->label_decl = decl;
2262
2263 slot = htab_find_slot (named_labels, ent, INSERT);
2264 gcc_assert (*slot == NULL);
2265 *slot = ent;
2266
2267 return decl;
2268 }
2269
2270 /* Look for a label named ID in the current function. If one cannot
2271 be found, create one. (We keep track of used, but undefined,
2272 labels, and complain about them at the end of a function.) */
2273
2274 tree
lookup_label(tree id)2275 lookup_label (tree id)
2276 {
2277 tree decl;
2278
2279 timevar_push (TV_NAME_LOOKUP);
2280 /* You can't use labels at global scope. */
2281 if (current_function_decl == NULL_TREE)
2282 {
2283 error ("label %qE referenced outside of any function", id);
2284 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2285 }
2286
2287 /* See if we've already got this label. */
2288 decl = IDENTIFIER_LABEL_VALUE (id);
2289 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2290 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2291
2292 decl = make_label_decl (id, /*local_p=*/0);
2293 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2294 }
2295
2296 /* Declare a local label named ID. */
2297
2298 tree
declare_local_label(tree id)2299 declare_local_label (tree id)
2300 {
2301 tree decl, shadow;
2302
2303 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2304 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2305 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2306 current_binding_level->shadowed_labels);
2307 current_binding_level->shadowed_labels = shadow;
2308
2309 decl = make_label_decl (id, /*local_p=*/1);
2310 TREE_VALUE (shadow) = decl;
2311
2312 return decl;
2313 }
2314
2315 /* Returns nonzero if it is ill-formed to jump past the declaration of
2316 DECL. Returns 2 if it's also a real problem. */
2317
2318 static int
decl_jump_unsafe(tree decl)2319 decl_jump_unsafe (tree decl)
2320 {
2321 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2322 || TREE_TYPE (decl) == error_mark_node)
2323 return 0;
2324
2325 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2326 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2327 return 2;
2328
2329 if (pod_type_p (TREE_TYPE (decl)))
2330 return 0;
2331
2332 /* The POD stuff is just pedantry; why should it matter if the class
2333 contains a field of pointer to member type? */
2334 return 1;
2335 }
2336
2337 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2338
2339 static void
identify_goto(tree decl,const location_t * locus)2340 identify_goto (tree decl, const location_t *locus)
2341 {
2342 if (decl)
2343 pedwarn ("jump to label %qD", decl);
2344 else
2345 pedwarn ("jump to case label");
2346 if (locus)
2347 pedwarn ("%H from here", locus);
2348 }
2349
2350 /* Check that a single previously seen jump to a newly defined label
2351 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2352 the jump context; NAMES are the names in scope in LEVEL at the jump
2353 context; LOCUS is the source position of the jump or 0. Returns
2354 true if all is well. */
2355
2356 static bool
check_previous_goto_1(tree decl,struct cp_binding_level * level,tree names,bool exited_omp,const location_t * locus)2357 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2358 bool exited_omp, const location_t *locus)
2359 {
2360 struct cp_binding_level *b;
2361 bool identified = false, saw_eh = false, saw_omp = false;
2362
2363 if (exited_omp)
2364 {
2365 identify_goto (decl, locus);
2366 error (" exits OpenMP structured block");
2367 identified = saw_omp = true;
2368 }
2369
2370 for (b = current_binding_level; b ; b = b->level_chain)
2371 {
2372 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2373
2374 for (new_decls = b->names; new_decls != old_decls;
2375 new_decls = TREE_CHAIN (new_decls))
2376 {
2377 int problem = decl_jump_unsafe (new_decls);
2378 if (! problem)
2379 continue;
2380
2381 if (!identified)
2382 {
2383 identify_goto (decl, locus);
2384 identified = true;
2385 }
2386 if (problem > 1)
2387 error (" crosses initialization of %q+#D", new_decls);
2388 else
2389 pedwarn (" enters scope of non-POD %q+#D", new_decls);
2390 }
2391
2392 if (b == level)
2393 break;
2394 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2395 {
2396 if (!identified)
2397 {
2398 identify_goto (decl, locus);
2399 identified = true;
2400 }
2401 if (b->kind == sk_try)
2402 error (" enters try block");
2403 else
2404 error (" enters catch block");
2405 saw_eh = true;
2406 }
2407 if (b->kind == sk_omp && !saw_omp)
2408 {
2409 if (!identified)
2410 {
2411 identify_goto (decl, locus);
2412 identified = true;
2413 }
2414 error (" enters OpenMP structured block");
2415 saw_omp = true;
2416 }
2417 }
2418
2419 return !identified;
2420 }
2421
2422 static void
check_previous_goto(tree decl,struct named_label_use_entry * use)2423 check_previous_goto (tree decl, struct named_label_use_entry *use)
2424 {
2425 check_previous_goto_1 (decl, use->binding_level,
2426 use->names_in_scope, use->in_omp_scope,
2427 &use->o_goto_locus);
2428 }
2429
2430 static bool
check_switch_goto(struct cp_binding_level * level)2431 check_switch_goto (struct cp_binding_level* level)
2432 {
2433 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2434 }
2435
2436 /* Check that a new jump to a label DECL is OK. Called by
2437 finish_goto_stmt. */
2438
2439 void
check_goto(tree decl)2440 check_goto (tree decl)
2441 {
2442 struct named_label_entry *ent, dummy;
2443 bool saw_catch = false, identified = false;
2444 tree bad;
2445
2446 /* We can't know where a computed goto is jumping.
2447 So we assume that it's OK. */
2448 if (TREE_CODE (decl) != LABEL_DECL)
2449 return;
2450
2451 /* We didn't record any information about this label when we created it,
2452 and there's not much point since it's trivial to analyze as a return. */
2453 if (decl == cdtor_label)
2454 return;
2455
2456 dummy.label_decl = decl;
2457 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2458 gcc_assert (ent != NULL);
2459
2460 /* If the label hasn't been defined yet, defer checking. */
2461 if (! DECL_INITIAL (decl))
2462 {
2463 struct named_label_use_entry *new_use;
2464
2465 /* Don't bother creating another use if the last goto had the
2466 same data, and will therefore create the same set of errors. */
2467 if (ent->uses
2468 && ent->uses->names_in_scope == current_binding_level->names)
2469 return;
2470
2471 new_use = GGC_NEW (struct named_label_use_entry);
2472 new_use->binding_level = current_binding_level;
2473 new_use->names_in_scope = current_binding_level->names;
2474 new_use->o_goto_locus = input_location;
2475 new_use->in_omp_scope = false;
2476
2477 new_use->next = ent->uses;
2478 ent->uses = new_use;
2479 return;
2480 }
2481
2482 if (ent->in_try_scope || ent->in_catch_scope
2483 || ent->in_omp_scope || ent->bad_decls)
2484 {
2485 pedwarn ("jump to label %q+D", decl);
2486 pedwarn (" from here");
2487 identified = true;
2488 }
2489
2490 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
2491 {
2492 tree b = TREE_VALUE (bad);
2493 int u = decl_jump_unsafe (b);
2494
2495 if (u > 1 && DECL_ARTIFICIAL (b))
2496 {
2497 /* Can't skip init of __exception_info. */
2498 error ("%J enters catch block", b);
2499 saw_catch = true;
2500 }
2501 else if (u > 1)
2502 error (" skips initialization of %q+#D", b);
2503 else
2504 pedwarn (" enters scope of non-POD %q+#D", b);
2505 }
2506
2507 if (ent->in_try_scope)
2508 error (" enters try block");
2509 else if (ent->in_catch_scope && !saw_catch)
2510 error (" enters catch block");
2511
2512 if (ent->in_omp_scope)
2513 error (" enters OpenMP structured block");
2514 else if (flag_openmp)
2515 {
2516 struct cp_binding_level *b;
2517 for (b = current_binding_level; b ; b = b->level_chain)
2518 {
2519 if (b == ent->binding_level)
2520 break;
2521 if (b->kind == sk_omp)
2522 {
2523 if (!identified)
2524 {
2525 pedwarn ("jump to label %q+D", decl);
2526 pedwarn (" from here");
2527 identified = true;
2528 }
2529 error (" exits OpenMP structured block");
2530 break;
2531 }
2532 }
2533 }
2534 }
2535
2536 /* Check that a return is ok wrt OpenMP structured blocks.
2537 Called by finish_return_stmt. Returns true if all is well. */
2538
2539 bool
check_omp_return(void)2540 check_omp_return (void)
2541 {
2542 struct cp_binding_level *b;
2543 for (b = current_binding_level; b ; b = b->level_chain)
2544 if (b->kind == sk_omp)
2545 {
2546 error ("invalid exit from OpenMP structured block");
2547 return false;
2548 }
2549 return true;
2550 }
2551
2552 /* Define a label, specifying the location in the source file.
2553 Return the LABEL_DECL node for the label. */
2554
2555 tree
define_label(location_t location,tree name)2556 define_label (location_t location, tree name)
2557 {
2558 struct named_label_entry *ent, dummy;
2559 struct cp_binding_level *p;
2560 tree decl;
2561
2562 timevar_push (TV_NAME_LOOKUP);
2563
2564 decl = lookup_label (name);
2565
2566 dummy.label_decl = decl;
2567 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2568 gcc_assert (ent != NULL);
2569
2570 /* After labels, make any new cleanups in the function go into their
2571 own new (temporary) binding contour. */
2572 for (p = current_binding_level;
2573 p->kind != sk_function_parms;
2574 p = p->level_chain)
2575 p->more_cleanups_ok = 0;
2576
2577 if (name == get_identifier ("wchar_t"))
2578 pedwarn ("label named wchar_t");
2579
2580 if (DECL_INITIAL (decl) != NULL_TREE)
2581 {
2582 error ("duplicate label %qD", decl);
2583 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2584 }
2585 else
2586 {
2587 struct named_label_use_entry *use;
2588
2589 /* Mark label as having been defined. */
2590 DECL_INITIAL (decl) = error_mark_node;
2591 /* Say where in the source. */
2592 DECL_SOURCE_LOCATION (decl) = location;
2593
2594 ent->binding_level = current_binding_level;
2595 ent->names_in_scope = current_binding_level->names;
2596
2597 for (use = ent->uses; use ; use = use->next)
2598 check_previous_goto (decl, use);
2599 ent->uses = NULL;
2600 }
2601
2602 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2603 }
2604
2605 struct cp_switch
2606 {
2607 struct cp_binding_level *level;
2608 struct cp_switch *next;
2609 /* The SWITCH_STMT being built. */
2610 tree switch_stmt;
2611 /* A splay-tree mapping the low element of a case range to the high
2612 element, or NULL_TREE if there is no high element. Used to
2613 determine whether or not a new case label duplicates an old case
2614 label. We need a tree, rather than simply a hash table, because
2615 of the GNU case range extension. */
2616 splay_tree cases;
2617 };
2618
2619 /* A stack of the currently active switch statements. The innermost
2620 switch statement is on the top of the stack. There is no need to
2621 mark the stack for garbage collection because it is only active
2622 during the processing of the body of a function, and we never
2623 collect at that point. */
2624
2625 static struct cp_switch *switch_stack;
2626
2627 /* Called right after a switch-statement condition is parsed.
2628 SWITCH_STMT is the switch statement being parsed. */
2629
2630 void
push_switch(tree switch_stmt)2631 push_switch (tree switch_stmt)
2632 {
2633 struct cp_switch *p = XNEW (struct cp_switch);
2634 p->level = current_binding_level;
2635 p->next = switch_stack;
2636 p->switch_stmt = switch_stmt;
2637 p->cases = splay_tree_new (case_compare, NULL, NULL);
2638 switch_stack = p;
2639 }
2640
2641 void
pop_switch(void)2642 pop_switch (void)
2643 {
2644 struct cp_switch *cs = switch_stack;
2645 location_t switch_location;
2646
2647 /* Emit warnings as needed. */
2648 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2649 switch_location = EXPR_LOCATION (cs->switch_stmt);
2650 else
2651 switch_location = input_location;
2652 if (!processing_template_decl)
2653 c_do_switch_warnings (cs->cases, switch_location,
2654 SWITCH_STMT_TYPE (cs->switch_stmt),
2655 SWITCH_STMT_COND (cs->switch_stmt));
2656
2657 splay_tree_delete (cs->cases);
2658 switch_stack = switch_stack->next;
2659 free (cs);
2660 }
2661
2662 /* Note that we've seen a definition of a case label, and complain if this
2663 is a bad place for one. */
2664
2665 tree
finish_case_label(tree low_value,tree high_value)2666 finish_case_label (tree low_value, tree high_value)
2667 {
2668 tree cond, r;
2669 struct cp_binding_level *p;
2670
2671 if (processing_template_decl)
2672 {
2673 tree label;
2674
2675 /* For templates, just add the case label; we'll do semantic
2676 analysis at instantiation-time. */
2677 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2678 return add_stmt (build_case_label (low_value, high_value, label));
2679 }
2680
2681 /* Find the condition on which this switch statement depends. */
2682 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2683 if (cond && TREE_CODE (cond) == TREE_LIST)
2684 cond = TREE_VALUE (cond);
2685
2686 if (!check_switch_goto (switch_stack->level))
2687 return error_mark_node;
2688
2689 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2690 low_value, high_value);
2691
2692 /* After labels, make any new cleanups in the function go into their
2693 own new (temporary) binding contour. */
2694 for (p = current_binding_level;
2695 p->kind != sk_function_parms;
2696 p = p->level_chain)
2697 p->more_cleanups_ok = 0;
2698
2699 return r;
2700 }
2701
2702 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2703
2704 static hashval_t
typename_hash(const void * k)2705 typename_hash (const void* k)
2706 {
2707 hashval_t hash;
2708 tree t = (tree) k;
2709
2710 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2711 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2712
2713 return hash;
2714 }
2715
2716 typedef struct typename_info {
2717 tree scope;
2718 tree name;
2719 tree template_id;
2720 bool enum_p;
2721 bool class_p;
2722 } typename_info;
2723
2724 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2725
2726 static int
typename_compare(const void * k1,const void * k2)2727 typename_compare (const void * k1, const void * k2)
2728 {
2729 tree t1;
2730 const typename_info *t2;
2731
2732 t1 = (tree) k1;
2733 t2 = (const typename_info *) k2;
2734
2735 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2736 && TYPE_CONTEXT (t1) == t2->scope
2737 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2738 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2739 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2740 }
2741
2742 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2743 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2744
2745 Returns the new TYPENAME_TYPE. */
2746
2747 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2748
2749 static tree
build_typename_type(tree context,tree name,tree fullname,enum tag_types tag_type)2750 build_typename_type (tree context, tree name, tree fullname,
2751 enum tag_types tag_type)
2752 {
2753 tree t;
2754 tree d;
2755 typename_info ti;
2756 void **e;
2757 hashval_t hash;
2758
2759 if (typename_htab == NULL)
2760 typename_htab = htab_create_ggc (61, &typename_hash,
2761 &typename_compare, NULL);
2762
2763 ti.scope = FROB_CONTEXT (context);
2764 ti.name = name;
2765 ti.template_id = fullname;
2766 ti.enum_p = tag_type == enum_type;
2767 ti.class_p = (tag_type == class_type
2768 || tag_type == record_type
2769 || tag_type == union_type);
2770 hash = (htab_hash_pointer (ti.scope)
2771 ^ htab_hash_pointer (ti.name));
2772
2773 /* See if we already have this type. */
2774 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2775 if (*e)
2776 t = (tree) *e;
2777 else
2778 {
2779 /* Build the TYPENAME_TYPE. */
2780 t = make_aggr_type (TYPENAME_TYPE);
2781 TYPE_CONTEXT (t) = ti.scope;
2782 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2783 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2784 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2785
2786 /* Build the corresponding TYPE_DECL. */
2787 d = build_decl (TYPE_DECL, name, t);
2788 TYPE_NAME (TREE_TYPE (d)) = d;
2789 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2790 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2791 DECL_ARTIFICIAL (d) = 1;
2792
2793 /* Store it in the hash table. */
2794 *e = t;
2795 }
2796
2797 return t;
2798 }
2799
2800 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2801 provided to name the type. Returns an appropriate type, unless an
2802 error occurs, in which case error_mark_node is returned. If we
2803 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2804 return that, rather than the _TYPE it corresponds to, in other
2805 cases we look through the type decl. If TF_ERROR is set, complain
2806 about errors, otherwise be quiet. */
2807
2808 tree
make_typename_type(tree context,tree name,enum tag_types tag_type,tsubst_flags_t complain)2809 make_typename_type (tree context, tree name, enum tag_types tag_type,
2810 tsubst_flags_t complain)
2811 {
2812 tree fullname;
2813 tree t;
2814 bool want_template;
2815
2816 if (name == error_mark_node
2817 || context == NULL_TREE
2818 || context == error_mark_node)
2819 return error_mark_node;
2820
2821 if (TYPE_P (name))
2822 {
2823 if (!(TYPE_LANG_SPECIFIC (name)
2824 && (CLASSTYPE_IS_TEMPLATE (name)
2825 || CLASSTYPE_USE_TEMPLATE (name))))
2826 name = TYPE_IDENTIFIER (name);
2827 else
2828 /* Create a TEMPLATE_ID_EXPR for the type. */
2829 name = build_nt (TEMPLATE_ID_EXPR,
2830 CLASSTYPE_TI_TEMPLATE (name),
2831 CLASSTYPE_TI_ARGS (name));
2832 }
2833 else if (TREE_CODE (name) == TYPE_DECL)
2834 name = DECL_NAME (name);
2835
2836 fullname = name;
2837
2838 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2839 {
2840 name = TREE_OPERAND (name, 0);
2841 if (TREE_CODE (name) == TEMPLATE_DECL)
2842 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2843 else if (TREE_CODE (name) == OVERLOAD)
2844 {
2845 error ("%qD is not a type", name);
2846 return error_mark_node;
2847 }
2848 }
2849 if (TREE_CODE (name) == TEMPLATE_DECL)
2850 {
2851 error ("%qD used without template parameters", name);
2852 return error_mark_node;
2853 }
2854 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2855 gcc_assert (TYPE_P (context));
2856
2857 /* When the CONTEXT is a dependent type, NAME could refer to a
2858 dependent base class of CONTEXT. So we cannot peek inside it,
2859 even if CONTEXT is a currently open scope. */
2860 if (dependent_type_p (context))
2861 return build_typename_type (context, name, fullname, tag_type);
2862
2863 if (!IS_AGGR_TYPE (context))
2864 {
2865 if (complain & tf_error)
2866 error ("%q#T is not a class", context);
2867 return error_mark_node;
2868 }
2869
2870 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
2871
2872 /* We should only set WANT_TYPE when we're a nested typename type.
2873 Then we can give better diagnostics if we find a non-type. */
2874 t = lookup_field (context, name, 0, /*want_type=*/true);
2875 if (!t)
2876 {
2877 if (complain & tf_error)
2878 error (want_template ? "no class template named %q#T in %q#T"
2879 : "no type named %q#T in %q#T", name, context);
2880 return error_mark_node;
2881 }
2882
2883 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
2884 {
2885 if (complain & tf_error)
2886 error ("%<typename %T::%D%> names %q#T, which is not a class template",
2887 context, name, t);
2888 return error_mark_node;
2889 }
2890 if (!want_template && TREE_CODE (t) != TYPE_DECL)
2891 {
2892 if (complain & tf_error)
2893 error ("%<typename %T::%D%> names %q#T, which is not a type",
2894 context, name, t);
2895 return error_mark_node;
2896 }
2897
2898 if (complain & tf_error)
2899 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
2900
2901 if (want_template)
2902 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
2903 NULL_TREE, context,
2904 /*entering_scope=*/0,
2905 tf_warning_or_error | tf_user);
2906
2907 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2908 t = TREE_TYPE (t);
2909
2910 return t;
2911 }
2912
2913 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
2914 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
2915 in which case error_mark_node is returned.
2916
2917 If PARM_LIST is non-NULL, also make sure that the template parameter
2918 list of TEMPLATE_DECL matches.
2919
2920 If COMPLAIN zero, don't complain about any errors that occur. */
2921
2922 tree
make_unbound_class_template(tree context,tree name,tree parm_list,tsubst_flags_t complain)2923 make_unbound_class_template (tree context, tree name, tree parm_list,
2924 tsubst_flags_t complain)
2925 {
2926 tree t;
2927 tree d;
2928
2929 if (TYPE_P (name))
2930 name = TYPE_IDENTIFIER (name);
2931 else if (DECL_P (name))
2932 name = DECL_NAME (name);
2933 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2934
2935 if (!dependent_type_p (context)
2936 || currently_open_class (context))
2937 {
2938 tree tmpl = NULL_TREE;
2939
2940 if (IS_AGGR_TYPE (context))
2941 tmpl = lookup_field (context, name, 0, false);
2942
2943 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2944 {
2945 if (complain & tf_error)
2946 error ("no class template named %q#T in %q#T", name, context);
2947 return error_mark_node;
2948 }
2949
2950 if (parm_list
2951 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2952 {
2953 if (complain & tf_error)
2954 {
2955 error ("template parameters do not match template");
2956 error ("%q+D declared here", tmpl);
2957 }
2958 return error_mark_node;
2959 }
2960
2961 if (complain & tf_error)
2962 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
2963
2964 return tmpl;
2965 }
2966
2967 /* Build the UNBOUND_CLASS_TEMPLATE. */
2968 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2969 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2970 TREE_TYPE (t) = NULL_TREE;
2971
2972 /* Build the corresponding TEMPLATE_DECL. */
2973 d = build_decl (TEMPLATE_DECL, name, t);
2974 TYPE_NAME (TREE_TYPE (d)) = d;
2975 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2976 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2977 DECL_ARTIFICIAL (d) = 1;
2978 DECL_TEMPLATE_PARMS (d) = parm_list;
2979
2980 return t;
2981 }
2982
2983
2984
2985 /* Push the declarations of builtin types into the namespace.
2986 RID_INDEX is the index of the builtin type in the array
2987 RID_POINTERS. NAME is the name used when looking up the builtin
2988 type. TYPE is the _TYPE node for the builtin type. */
2989
2990 void
record_builtin_type(enum rid rid_index,const char * name,tree type)2991 record_builtin_type (enum rid rid_index,
2992 const char* name,
2993 tree type)
2994 {
2995 tree rname = NULL_TREE, tname = NULL_TREE;
2996 tree tdecl = NULL_TREE;
2997
2998 if ((int) rid_index < (int) RID_MAX)
2999 rname = ridpointers[(int) rid_index];
3000 if (name)
3001 tname = get_identifier (name);
3002
3003 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3004 eliminated. Built-in types should not be looked up name; their
3005 names are keywords that the parser can recognize. However, there
3006 is code in c-common.c that uses identifier_global_value to look
3007 up built-in types by name. */
3008 if (tname)
3009 {
3010 tdecl = build_decl (TYPE_DECL, tname, type);
3011 DECL_ARTIFICIAL (tdecl) = 1;
3012 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3013 }
3014 if (rname)
3015 {
3016 if (!tdecl)
3017 {
3018 tdecl = build_decl (TYPE_DECL, rname, type);
3019 DECL_ARTIFICIAL (tdecl) = 1;
3020 }
3021 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3022 }
3023
3024 if (!TYPE_NAME (type))
3025 TYPE_NAME (type) = tdecl;
3026
3027 if (tdecl)
3028 debug_hooks->type_decl (tdecl, 0);
3029 }
3030
3031 /* Record one of the standard Java types.
3032 * Declare it as having the given NAME.
3033 * If SIZE > 0, it is the size of one of the integral types;
3034 * otherwise it is the negative of the size of one of the other types. */
3035
3036 static tree
record_builtin_java_type(const char * name,int size)3037 record_builtin_java_type (const char* name, int size)
3038 {
3039 tree type, decl;
3040 if (size > 0)
3041 type = make_signed_type (size);
3042 else if (size > -32)
3043 { /* "__java_char" or ""__java_boolean". */
3044 type = make_unsigned_type (-size);
3045 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3046 }
3047 else
3048 { /* "__java_float" or ""__java_double". */
3049 type = make_node (REAL_TYPE);
3050 TYPE_PRECISION (type) = - size;
3051 layout_type (type);
3052 }
3053 record_builtin_type (RID_MAX, name, type);
3054 decl = TYPE_NAME (type);
3055
3056 /* Suppress generate debug symbol entries for these types,
3057 since for normal C++ they are just clutter.
3058 However, push_lang_context undoes this if extern "Java" is seen. */
3059 DECL_IGNORED_P (decl) = 1;
3060
3061 TYPE_FOR_JAVA (type) = 1;
3062 return type;
3063 }
3064
3065 /* Push a type into the namespace so that the back-ends ignore it. */
3066
3067 static void
record_unknown_type(tree type,const char * name)3068 record_unknown_type (tree type, const char* name)
3069 {
3070 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3071 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3072 DECL_IGNORED_P (decl) = 1;
3073 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3074 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3075 TYPE_ALIGN (type) = 1;
3076 TYPE_USER_ALIGN (type) = 0;
3077 TYPE_MODE (type) = TYPE_MODE (void_type_node);
3078 }
3079
3080 /* A string for which we should create an IDENTIFIER_NODE at
3081 startup. */
3082
3083 typedef struct predefined_identifier
3084 {
3085 /* The name of the identifier. */
3086 const char *const name;
3087 /* The place where the IDENTIFIER_NODE should be stored. */
3088 tree *const node;
3089 /* Nonzero if this is the name of a constructor or destructor. */
3090 const int ctor_or_dtor_p;
3091 } predefined_identifier;
3092
3093 /* Create all the predefined identifiers. */
3094
3095 static void
initialize_predefined_identifiers(void)3096 initialize_predefined_identifiers (void)
3097 {
3098 const predefined_identifier *pid;
3099
3100 /* A table of identifiers to create at startup. */
3101 static const predefined_identifier predefined_identifiers[] = {
3102 { "C++", &lang_name_cplusplus, 0 },
3103 { "C", &lang_name_c, 0 },
3104 { "Java", &lang_name_java, 0 },
3105 /* Some of these names have a trailing space so that it is
3106 impossible for them to conflict with names written by users. */
3107 { "__ct ", &ctor_identifier, 1 },
3108 { "__base_ctor ", &base_ctor_identifier, 1 },
3109 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3110 { "__dt ", &dtor_identifier, 1 },
3111 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3112 { "__base_dtor ", &base_dtor_identifier, 1 },
3113 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3114 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3115 { "nelts", &nelts_identifier, 0 },
3116 { THIS_NAME, &this_identifier, 0 },
3117 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3118 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3119 { "_vptr", &vptr_identifier, 0 },
3120 { "__vtt_parm", &vtt_parm_identifier, 0 },
3121 { "::", &global_scope_name, 0 },
3122 { "std", &std_identifier, 0 },
3123 { NULL, NULL, 0 }
3124 };
3125
3126 for (pid = predefined_identifiers; pid->name; ++pid)
3127 {
3128 *pid->node = get_identifier (pid->name);
3129 if (pid->ctor_or_dtor_p)
3130 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3131 }
3132 }
3133
3134 /* Create the predefined scalar types of C,
3135 and some nodes representing standard constants (0, 1, (void *)0).
3136 Initialize the global binding level.
3137 Make definitions for built-in primitive functions. */
3138
3139 void
cxx_init_decl_processing(void)3140 cxx_init_decl_processing (void)
3141 {
3142 tree void_ftype;
3143 tree void_ftype_ptr;
3144
3145 build_common_tree_nodes (flag_signed_char, false);
3146
3147 /* Create all the identifiers we need. */
3148 initialize_predefined_identifiers ();
3149
3150 /* Create the global variables. */
3151 push_to_top_level ();
3152
3153 current_function_decl = NULL_TREE;
3154 current_binding_level = NULL;
3155 /* Enter the global namespace. */
3156 gcc_assert (global_namespace == NULL_TREE);
3157 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3158 void_type_node);
3159 TREE_PUBLIC (global_namespace) = 1;
3160 begin_scope (sk_namespace, global_namespace);
3161
3162 current_lang_name = NULL_TREE;
3163
3164 /* Adjust various flags based on command-line settings. */
3165 if (!flag_permissive)
3166 flag_pedantic_errors = 1;
3167 if (!flag_no_inline)
3168 {
3169 flag_inline_trees = 1;
3170 flag_no_inline = 1;
3171 }
3172 if (flag_inline_functions)
3173 flag_inline_trees = 2;
3174
3175 if (flag_visibility_ms_compat)
3176 default_visibility = VISIBILITY_HIDDEN;
3177
3178 /* Initially, C. */
3179 current_lang_name = lang_name_c;
3180
3181 /* Create the `std' namespace. */
3182 push_namespace (std_identifier);
3183 std_node = current_namespace;
3184 pop_namespace ();
3185
3186 c_common_nodes_and_builtins ();
3187
3188 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3189 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3190 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3191 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3192 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3193 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3194 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3195 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3196
3197 integer_two_node = build_int_cst (NULL_TREE, 2);
3198 integer_three_node = build_int_cst (NULL_TREE, 3);
3199
3200 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3201 truthvalue_type_node = boolean_type_node;
3202 truthvalue_false_node = boolean_false_node;
3203 truthvalue_true_node = boolean_true_node;
3204
3205 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3206
3207 #if 0
3208 record_builtin_type (RID_MAX, NULL, string_type_node);
3209 #endif
3210
3211 delta_type_node = ptrdiff_type_node;
3212 vtable_index_type = ptrdiff_type_node;
3213
3214 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3215 void_ftype = build_function_type (void_type_node, void_list_node);
3216 void_ftype_ptr = build_function_type (void_type_node,
3217 tree_cons (NULL_TREE,
3218 ptr_type_node,
3219 void_list_node));
3220 void_ftype_ptr
3221 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3222
3223 /* C++ extensions */
3224
3225 unknown_type_node = make_node (UNKNOWN_TYPE);
3226 record_unknown_type (unknown_type_node, "unknown type");
3227
3228 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3229 TREE_TYPE (unknown_type_node) = unknown_type_node;
3230
3231 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3232 result. */
3233 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3234 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3235
3236 {
3237 /* Make sure we get a unique function type, so we can give
3238 its pointer type a name. (This wins for gdb.) */
3239 tree vfunc_type = make_node (FUNCTION_TYPE);
3240 TREE_TYPE (vfunc_type) = integer_type_node;
3241 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3242 layout_type (vfunc_type);
3243
3244 vtable_entry_type = build_pointer_type (vfunc_type);
3245 }
3246 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3247
3248 vtbl_type_node
3249 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3250 layout_type (vtbl_type_node);
3251 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3252 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3253 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3254 layout_type (vtbl_ptr_type_node);
3255 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3256
3257 push_namespace (get_identifier ("__cxxabiv1"));
3258 abi_node = current_namespace;
3259 pop_namespace ();
3260
3261 global_type_node = make_node (LANG_TYPE);
3262 record_unknown_type (global_type_node, "global type");
3263
3264 /* Now, C++. */
3265 current_lang_name = lang_name_cplusplus;
3266
3267 {
3268 tree bad_alloc_id;
3269 tree bad_alloc_type_node;
3270 tree bad_alloc_decl;
3271 tree newtype, deltype;
3272 tree ptr_ftype_sizetype;
3273
3274 push_namespace (std_identifier);
3275 bad_alloc_id = get_identifier ("bad_alloc");
3276 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3277 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3278 bad_alloc_decl
3279 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3280 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3281 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3282 pop_namespace ();
3283
3284 ptr_ftype_sizetype
3285 = build_function_type (ptr_type_node,
3286 tree_cons (NULL_TREE,
3287 size_type_node,
3288 void_list_node));
3289 newtype = build_exception_variant
3290 (ptr_ftype_sizetype, add_exception_specifier
3291 (NULL_TREE, bad_alloc_type_node, -1));
3292 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3293 push_cp_library_fn (NEW_EXPR, newtype);
3294 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3295 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3296 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3297 }
3298
3299 abort_fndecl
3300 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3301
3302 /* Perform other language dependent initializations. */
3303 init_class_processing ();
3304 init_rtti_processing ();
3305
3306 if (flag_exceptions)
3307 init_exception_processing ();
3308
3309 if (! supports_one_only ())
3310 flag_weak = 0;
3311
3312 make_fname_decl = cp_make_fname_decl;
3313 start_fname_decls ();
3314
3315 /* Show we use EH for cleanups. */
3316 if (flag_exceptions)
3317 using_eh_for_cleanups ();
3318 }
3319
3320 /* Generate an initializer for a function naming variable from
3321 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3322 filled in with the type of the init. */
3323
3324 tree
cp_fname_init(const char * name,tree * type_p)3325 cp_fname_init (const char* name, tree *type_p)
3326 {
3327 tree domain = NULL_TREE;
3328 tree type;
3329 tree init = NULL_TREE;
3330 size_t length = 0;
3331
3332 if (name)
3333 {
3334 length = strlen (name);
3335 domain = build_index_type (size_int (length));
3336 init = build_string (length + 1, name);
3337 }
3338
3339 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3340 type = build_cplus_array_type (type, domain);
3341
3342 *type_p = type;
3343
3344 if (init)
3345 TREE_TYPE (init) = type;
3346 else
3347 init = error_mark_node;
3348
3349 return init;
3350 }
3351
3352 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3353 decl, NAME is the initialization string and TYPE_DEP indicates whether
3354 NAME depended on the type of the function. We make use of that to detect
3355 __PRETTY_FUNCTION__ inside a template fn. This is being done
3356 lazily at the point of first use, so we mustn't push the decl now. */
3357
3358 static tree
cp_make_fname_decl(tree id,int type_dep)3359 cp_make_fname_decl (tree id, int type_dep)
3360 {
3361 const char *const name = (type_dep && processing_template_decl
3362 ? NULL : fname_as_string (type_dep));
3363 tree type;
3364 tree init = cp_fname_init (name, &type);
3365 tree decl = build_decl (VAR_DECL, id, type);
3366
3367 if (name)
3368 free ((char *) name);
3369
3370 /* As we're using pushdecl_with_scope, we must set the context. */
3371 DECL_CONTEXT (decl) = current_function_decl;
3372 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3373
3374 TREE_STATIC (decl) = 1;
3375 TREE_READONLY (decl) = 1;
3376 DECL_ARTIFICIAL (decl) = 1;
3377
3378 TREE_USED (decl) = 1;
3379
3380 if (current_function_decl)
3381 {
3382 struct cp_binding_level *b = current_binding_level;
3383 while (b->level_chain->kind != sk_function_parms)
3384 b = b->level_chain;
3385 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3386 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3387 LOOKUP_ONLYCONVERTING);
3388 }
3389 else
3390 pushdecl_top_level_and_finish (decl, init);
3391
3392 return decl;
3393 }
3394
3395 /* Make a definition for a builtin function named NAME in the current
3396 namespace, whose data type is TYPE and whose context is CONTEXT.
3397 TYPE should be a function type with argument types.
3398
3399 CLASS and CODE tell later passes how to compile calls to this function.
3400 See tree.h for possible values.
3401
3402 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3403 the name to be called if we can't opencode the function.
3404 If ATTRS is nonzero, use that for the function's attribute
3405 list. */
3406
3407 static tree
builtin_function_1(const char * name,tree type,tree context,enum built_in_function code,enum built_in_class class,const char * libname,tree attrs)3408 builtin_function_1 (const char* name,
3409 tree type,
3410 tree context,
3411 enum built_in_function code,
3412 enum built_in_class class,
3413 const char* libname,
3414 tree attrs)
3415 {
3416 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
3417 DECL_BUILT_IN_CLASS (decl) = class;
3418 DECL_FUNCTION_CODE (decl) = code;
3419 DECL_CONTEXT (decl) = context;
3420
3421 pushdecl (decl);
3422
3423 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3424 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3425 function in the namespace. */
3426 if (libname)
3427 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3428
3429 /* A function in the user's namespace should have an explicit
3430 declaration before it is used. Mark the built-in function as
3431 anticipated but not actually declared. */
3432 if (name[0] != '_' || name[1] != '_')
3433 DECL_ANTICIPATED (decl) = 1;
3434
3435 /* Possibly apply some default attributes to this built-in function. */
3436 if (attrs)
3437 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3438 else
3439 decl_attributes (&decl, NULL_TREE, 0);
3440
3441 return decl;
3442 }
3443
3444 /* Entry point for the benefit of c_common_nodes_and_builtins.
3445
3446 Make a definition for a builtin function named NAME and whose data type
3447 is TYPE. TYPE should be a function type with argument types. This
3448 function places the anticipated declaration in the global namespace
3449 and additionally in the std namespace if appropriate.
3450
3451 CLASS and CODE tell later passes how to compile calls to this function.
3452 See tree.h for possible values.
3453
3454 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3455 the name to be called if we can't opencode the function.
3456
3457 If ATTRS is nonzero, use that for the function's attribute
3458 list. */
3459
3460 tree
builtin_function(const char * name,tree type,int code,enum built_in_class cl,const char * libname,tree attrs)3461 builtin_function (const char* name,
3462 tree type,
3463 int code,
3464 enum built_in_class cl,
3465 const char* libname,
3466 tree attrs)
3467 {
3468 /* All builtins that don't begin with an '_' should additionally
3469 go in the 'std' namespace. */
3470 if (name[0] != '_')
3471 {
3472 push_namespace (std_identifier);
3473 builtin_function_1 (name, type, std_node, code, cl, libname, attrs);
3474 pop_namespace ();
3475 }
3476
3477 return builtin_function_1 (name, type, NULL_TREE, code,
3478 cl, libname, attrs);
3479 }
3480
3481 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3482 function. Not called directly. */
3483
3484 static tree
build_library_fn_1(tree name,enum tree_code operator_code,tree type)3485 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3486 {
3487 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3488 DECL_EXTERNAL (fn) = 1;
3489 TREE_PUBLIC (fn) = 1;
3490 DECL_ARTIFICIAL (fn) = 1;
3491 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3492 SET_DECL_LANGUAGE (fn, lang_c);
3493 /* Runtime library routines are, by definition, available in an
3494 external shared object. */
3495 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3496 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3497 return fn;
3498 }
3499
3500 /* Returns the _DECL for a library function with C linkage.
3501 We assume that such functions never throw; if this is incorrect,
3502 callers should unset TREE_NOTHROW. */
3503
3504 tree
build_library_fn(tree name,tree type)3505 build_library_fn (tree name, tree type)
3506 {
3507 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3508 TREE_NOTHROW (fn) = 1;
3509 return fn;
3510 }
3511
3512 /* Returns the _DECL for a library function with C++ linkage. */
3513
3514 static tree
build_cp_library_fn(tree name,enum tree_code operator_code,tree type)3515 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3516 {
3517 tree fn = build_library_fn_1 (name, operator_code, type);
3518 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3519 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3520 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3521 return fn;
3522 }
3523
3524 /* Like build_library_fn, but takes a C string instead of an
3525 IDENTIFIER_NODE. */
3526
3527 tree
build_library_fn_ptr(const char * name,tree type)3528 build_library_fn_ptr (const char* name, tree type)
3529 {
3530 return build_library_fn (get_identifier (name), type);
3531 }
3532
3533 /* Like build_cp_library_fn, but takes a C string instead of an
3534 IDENTIFIER_NODE. */
3535
3536 tree
build_cp_library_fn_ptr(const char * name,tree type)3537 build_cp_library_fn_ptr (const char* name, tree type)
3538 {
3539 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3540 }
3541
3542 /* Like build_library_fn, but also pushes the function so that we will
3543 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3544
3545 tree
push_library_fn(tree name,tree type)3546 push_library_fn (tree name, tree type)
3547 {
3548 tree fn = build_library_fn (name, type);
3549 pushdecl_top_level (fn);
3550 return fn;
3551 }
3552
3553 /* Like build_cp_library_fn, but also pushes the function so that it
3554 will be found by normal lookup. */
3555
3556 static tree
push_cp_library_fn(enum tree_code operator_code,tree type)3557 push_cp_library_fn (enum tree_code operator_code, tree type)
3558 {
3559 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3560 operator_code,
3561 type);
3562 pushdecl (fn);
3563 return fn;
3564 }
3565
3566 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3567 a FUNCTION_TYPE. */
3568
3569 tree
push_void_library_fn(tree name,tree parmtypes)3570 push_void_library_fn (tree name, tree parmtypes)
3571 {
3572 tree type = build_function_type (void_type_node, parmtypes);
3573 return push_library_fn (name, type);
3574 }
3575
3576 /* Like push_library_fn, but also note that this function throws
3577 and does not return. Used for __throw_foo and the like. */
3578
3579 tree
push_throw_library_fn(tree name,tree type)3580 push_throw_library_fn (tree name, tree type)
3581 {
3582 tree fn = push_library_fn (name, type);
3583 TREE_THIS_VOLATILE (fn) = 1;
3584 TREE_NOTHROW (fn) = 0;
3585 return fn;
3586 }
3587
3588 /* When we call finish_struct for an anonymous union, we create
3589 default copy constructors and such. But, an anonymous union
3590 shouldn't have such things; this function undoes the damage to the
3591 anonymous union type T.
3592
3593 (The reason that we create the synthesized methods is that we don't
3594 distinguish `union { int i; }' from `typedef union { int i; } U'.
3595 The first is an anonymous union; the second is just an ordinary
3596 union type.) */
3597
3598 void
fixup_anonymous_aggr(tree t)3599 fixup_anonymous_aggr (tree t)
3600 {
3601 tree *q;
3602
3603 /* Wipe out memory of synthesized methods. */
3604 TYPE_HAS_CONSTRUCTOR (t) = 0;
3605 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3606 TYPE_HAS_INIT_REF (t) = 0;
3607 TYPE_HAS_CONST_INIT_REF (t) = 0;
3608 TYPE_HAS_ASSIGN_REF (t) = 0;
3609 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3610
3611 /* Splice the implicitly generated functions out of the TYPE_METHODS
3612 list. */
3613 q = &TYPE_METHODS (t);
3614 while (*q)
3615 {
3616 if (DECL_ARTIFICIAL (*q))
3617 *q = TREE_CHAIN (*q);
3618 else
3619 q = &TREE_CHAIN (*q);
3620 }
3621
3622 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3623 if (TYPE_METHODS (t))
3624 error ("%Jan anonymous union cannot have function members",
3625 TYPE_MAIN_DECL (t));
3626
3627 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3628 assignment operators (because they cannot have these methods themselves).
3629 For anonymous unions this is already checked because they are not allowed
3630 in any union, otherwise we have to check it. */
3631 if (TREE_CODE (t) != UNION_TYPE)
3632 {
3633 tree field, type;
3634
3635 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3636 if (TREE_CODE (field) == FIELD_DECL)
3637 {
3638 type = TREE_TYPE (field);
3639 if (CLASS_TYPE_P (type))
3640 {
3641 if (TYPE_NEEDS_CONSTRUCTING (type))
3642 error ("member %q+#D with constructor not allowed "
3643 "in anonymous aggregate", field);
3644 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3645 error ("member %q+#D with destructor not allowed "
3646 "in anonymous aggregate", field);
3647 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3648 error ("member %q+#D with copy assignment operator "
3649 "not allowed in anonymous aggregate", field);
3650 }
3651 }
3652 }
3653 }
3654
3655 /* Make sure that a declaration with no declarator is well-formed, i.e.
3656 just declares a tagged type or anonymous union.
3657
3658 Returns the type declared; or NULL_TREE if none. */
3659
3660 tree
check_tag_decl(cp_decl_specifier_seq * declspecs)3661 check_tag_decl (cp_decl_specifier_seq *declspecs)
3662 {
3663 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3664 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3665 /* If a class, struct, or enum type is declared by the DECLSPECS
3666 (i.e, if a class-specifier, enum-specifier, or non-typename
3667 elaborated-type-specifier appears in the DECLSPECS),
3668 DECLARED_TYPE is set to the corresponding type. */
3669 tree declared_type = NULL_TREE;
3670 bool error_p = false;
3671
3672 if (declspecs->multiple_types_p)
3673 error ("multiple types in one declaration");
3674 else if (declspecs->redefined_builtin_type)
3675 {
3676 if (!in_system_header)
3677 pedwarn ("redeclaration of C++ built-in type %qT",
3678 declspecs->redefined_builtin_type);
3679 return NULL_TREE;
3680 }
3681
3682 if (declspecs->type
3683 && TYPE_P (declspecs->type)
3684 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3685 && IS_AGGR_TYPE (declspecs->type))
3686 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3687 declared_type = declspecs->type;
3688 else if (declspecs->type == error_mark_node)
3689 error_p = true;
3690 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3691 pedwarn ("declaration does not declare anything");
3692 /* Check for an anonymous union. */
3693 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3694 && TYPE_ANONYMOUS_P (declared_type))
3695 {
3696 /* 7/3 In a simple-declaration, the optional init-declarator-list
3697 can be omitted only when declaring a class (clause 9) or
3698 enumeration (7.2), that is, when the decl-specifier-seq contains
3699 either a class-specifier, an elaborated-type-specifier with
3700 a class-key (9.1), or an enum-specifier. In these cases and
3701 whenever a class-specifier or enum-specifier is present in the
3702 decl-specifier-seq, the identifiers in these specifiers are among
3703 the names being declared by the declaration (as class-name,
3704 enum-names, or enumerators, depending on the syntax). In such
3705 cases, and except for the declaration of an unnamed bit-field (9.6),
3706 the decl-specifier-seq shall introduce one or more names into the
3707 program, or shall redeclare a name introduced by a previous
3708 declaration. [Example:
3709 enum { }; // ill-formed
3710 typedef class { }; // ill-formed
3711 --end example] */
3712 if (saw_typedef)
3713 {
3714 error ("missing type-name in typedef-declaration");
3715 return NULL_TREE;
3716 }
3717 /* Anonymous unions are objects, so they can have specifiers. */;
3718 SET_ANON_AGGR_TYPE_P (declared_type);
3719
3720 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3721 && !in_system_header)
3722 pedwarn ("ISO C++ prohibits anonymous structs");
3723 }
3724
3725 else
3726 {
3727 if (declspecs->specs[(int)ds_inline]
3728 || declspecs->specs[(int)ds_virtual])
3729 error ("%qs can only be specified for functions",
3730 declspecs->specs[(int)ds_inline]
3731 ? "inline" : "virtual");
3732 else if (saw_friend
3733 && (!current_class_type
3734 || current_scope () != current_class_type))
3735 error ("%<friend%> can only be specified inside a class");
3736 else if (declspecs->specs[(int)ds_explicit])
3737 error ("%<explicit%> can only be specified for constructors");
3738 else if (declspecs->storage_class)
3739 error ("a storage class can only be specified for objects "
3740 "and functions");
3741 else if (declspecs->specs[(int)ds_const]
3742 || declspecs->specs[(int)ds_volatile]
3743 || declspecs->specs[(int)ds_restrict]
3744 || declspecs->specs[(int)ds_thread])
3745 error ("qualifiers can only be specified for objects "
3746 "and functions");
3747 }
3748
3749 return declared_type;
3750 }
3751
3752 /* Called when a declaration is seen that contains no names to declare.
3753 If its type is a reference to a structure, union or enum inherited
3754 from a containing scope, shadow that tag name for the current scope
3755 with a forward reference.
3756 If its type defines a new named structure or union
3757 or defines an enum, it is valid but we need not do anything here.
3758 Otherwise, it is an error.
3759
3760 C++: may have to grok the declspecs to learn about static,
3761 complain for anonymous unions.
3762
3763 Returns the TYPE declared -- or NULL_TREE if none. */
3764
3765 tree
shadow_tag(cp_decl_specifier_seq * declspecs)3766 shadow_tag (cp_decl_specifier_seq *declspecs)
3767 {
3768 tree t = check_tag_decl (declspecs);
3769
3770 if (!t)
3771 return NULL_TREE;
3772
3773 if (declspecs->attributes)
3774 {
3775 warning (0, "attribute ignored in declaration of %q+#T", t);
3776 warning (0, "attribute for %q+#T must follow the %qs keyword",
3777 t, class_key_or_enum_as_string (t));
3778
3779 }
3780
3781 if (maybe_process_partial_specialization (t) == error_mark_node)
3782 return NULL_TREE;
3783
3784 /* This is where the variables in an anonymous union are
3785 declared. An anonymous union declaration looks like:
3786 union { ... } ;
3787 because there is no declarator after the union, the parser
3788 sends that declaration here. */
3789 if (ANON_AGGR_TYPE_P (t))
3790 {
3791 fixup_anonymous_aggr (t);
3792
3793 if (TYPE_FIELDS (t))
3794 {
3795 tree decl = grokdeclarator (/*declarator=*/NULL,
3796 declspecs, NORMAL, 0, NULL);
3797 finish_anon_union (decl);
3798 }
3799 }
3800
3801 return t;
3802 }
3803
3804 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3805
3806 tree
groktypename(cp_decl_specifier_seq * type_specifiers,const cp_declarator * declarator)3807 groktypename (cp_decl_specifier_seq *type_specifiers,
3808 const cp_declarator *declarator)
3809 {
3810 tree attrs;
3811 tree type;
3812 attrs = type_specifiers->attributes;
3813 type_specifiers->attributes = NULL_TREE;
3814 type = grokdeclarator (declarator, type_specifiers, TYPENAME, 0, &attrs);
3815 if (attrs)
3816 cplus_decl_attributes (&type, attrs, 0);
3817 return type;
3818 }
3819
3820 /* Decode a declarator in an ordinary declaration or data definition.
3821 This is called as soon as the type information and variable name
3822 have been parsed, before parsing the initializer if any.
3823 Here we create the ..._DECL node, fill in its type,
3824 and put it on the list of decls for the current context.
3825 The ..._DECL node is returned as the value.
3826
3827 Exception: for arrays where the length is not specified,
3828 the type is left null, to be filled in by `cp_finish_decl'.
3829
3830 Function definitions do not come here; they go to start_function
3831 instead. However, external and forward declarations of functions
3832 do go through here. Structure field declarations are done by
3833 grokfield and not through here. */
3834
3835 tree
start_decl(const cp_declarator * declarator,cp_decl_specifier_seq * declspecs,int initialized,tree attributes,tree prefix_attributes,tree * pushed_scope_p)3836 start_decl (const cp_declarator *declarator,
3837 cp_decl_specifier_seq *declspecs,
3838 int initialized,
3839 tree attributes,
3840 tree prefix_attributes,
3841 tree *pushed_scope_p)
3842 {
3843 tree decl;
3844 tree type, tem;
3845 tree context;
3846 /* APPLE LOCAL "unavailable" attribute (radar 2809697) */
3847 tree a;
3848 bool was_public;
3849
3850 *pushed_scope_p = NULL_TREE;
3851
3852 /* APPLE LOCAL begin "unavailable" attribute (radar 2809697) */
3853 /* An object declared as __attribute__((unavailable)) suppresses
3854 any reports of being declared with unavailable or deprecated
3855 items. An object declared as __attribute__((deprecated))
3856 suppresses warnings of uses of other deprecated items. */
3857 #ifdef A_LESS_INEFFICENT_WAY /* which I really don't want to do! */
3858 if (lookup_attribute ("deprecated", attributes))
3859 deprecated_state = DEPRECATED_SUPPRESS;
3860 else if (lookup_attribute ("unavailable", attributes))
3861 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3862 #else /* a more efficient way doing what lookup_attribute would do */
3863 for (a = attributes; a; a = TREE_CHAIN (a))
3864 {
3865 tree name = TREE_PURPOSE (a);
3866 if (TREE_CODE (name) == IDENTIFIER_NODE)
3867 if (is_attribute_p ("deprecated", name))
3868 {
3869 deprecated_state = DEPRECATED_SUPPRESS;
3870 break;
3871 }
3872 if (is_attribute_p ("unavailable", name))
3873 {
3874 deprecated_state = DEPRECATED_UNAVAILABLE_SUPPRESS;
3875 break;
3876 }
3877 }
3878 #endif
3879 /* APPLE LOCAL end "unavailable" attribute (radar 2809697) */
3880
3881 attributes = chainon (attributes, prefix_attributes);
3882
3883 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
3884 &attributes);
3885
3886 deprecated_state = DEPRECATED_NORMAL;
3887
3888 if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE
3889 || decl == error_mark_node)
3890 return error_mark_node;
3891
3892 type = TREE_TYPE (decl);
3893
3894 context = DECL_CONTEXT (decl);
3895
3896 if (context)
3897 {
3898 *pushed_scope_p = push_scope (context);
3899
3900 /* We are only interested in class contexts, later. */
3901 if (TREE_CODE (context) == NAMESPACE_DECL)
3902 context = NULL_TREE;
3903 }
3904
3905 if (initialized)
3906 /* Is it valid for this decl to have an initializer at all?
3907 If not, set INITIALIZED to zero, which will indirectly
3908 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
3909 switch (TREE_CODE (decl))
3910 {
3911 case TYPE_DECL:
3912 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3913 return error_mark_node;
3914
3915 case FUNCTION_DECL:
3916 error ("function %q#D is initialized like a variable", decl);
3917 return error_mark_node;
3918
3919 default:
3920 break;
3921 }
3922
3923 if (initialized)
3924 {
3925 if (! toplevel_bindings_p ()
3926 && DECL_EXTERNAL (decl))
3927 warning (0, "declaration of %q#D has %<extern%> and is initialized",
3928 decl);
3929 DECL_EXTERNAL (decl) = 0;
3930 if (toplevel_bindings_p ())
3931 TREE_STATIC (decl) = 1;
3932 }
3933
3934 /* Set attributes here so if duplicate decl, will have proper attributes. */
3935 cplus_decl_attributes (&decl, attributes, 0);
3936
3937 /* Dllimported symbols cannot be defined. Static data members (which
3938 can be initialized in-class and dllimported) go through grokfield,
3939 not here, so we don't need to exclude those decls when checking for
3940 a definition. */
3941 if (initialized && DECL_DLLIMPORT_P (decl))
3942 {
3943 error ("definition of %q#D is marked %<dllimport%>", decl);
3944 DECL_DLLIMPORT_P (decl) = 0;
3945 }
3946
3947 /* If #pragma weak was used, mark the decl weak now. */
3948 maybe_apply_pragma_weak (decl);
3949
3950 if (TREE_CODE (decl) == FUNCTION_DECL
3951 && DECL_DECLARED_INLINE_P (decl)
3952 && DECL_UNINLINABLE (decl)
3953 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3954 warning (0, "inline function %q+D given attribute noinline", decl);
3955
3956 if (context && COMPLETE_TYPE_P (complete_type (context)))
3957 {
3958 if (TREE_CODE (decl) == VAR_DECL)
3959 {
3960 tree field = lookup_field (context, DECL_NAME (decl), 0, false);
3961 if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
3962 error ("%q#D is not a static member of %q#T", decl, context);
3963 else
3964 {
3965 if (DECL_CONTEXT (field) != context)
3966 {
3967 if (!same_type_p (DECL_CONTEXT (field), context))
3968 pedwarn ("ISO C++ does not permit %<%T::%D%> "
3969 "to be defined as %<%T::%D%>",
3970 DECL_CONTEXT (field), DECL_NAME (decl),
3971 context, DECL_NAME (decl));
3972 DECL_CONTEXT (decl) = DECL_CONTEXT (field);
3973 }
3974 if (processing_specialization
3975 && template_class_depth (context) == 0
3976 && CLASSTYPE_TEMPLATE_SPECIALIZATION (context))
3977 error ("template header not allowed in member definition "
3978 "of explicitly specialized class");
3979 /* Static data member are tricky; an in-class initialization
3980 still doesn't provide a definition, so the in-class
3981 declaration will have DECL_EXTERNAL set, but will have an
3982 initialization. Thus, duplicate_decls won't warn
3983 about this situation, and so we check here. */
3984 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field))
3985 error ("duplicate initialization of %qD", decl);
3986 if (duplicate_decls (decl, field, /*newdecl_is_friend=*/false))
3987 decl = field;
3988 }
3989 }
3990 else
3991 {
3992 tree field = check_classfn (context, decl,
3993 (processing_template_decl
3994 > template_class_depth (context))
3995 ? current_template_parms
3996 : NULL_TREE);
3997 if (field && duplicate_decls (decl, field,
3998 /*newdecl_is_friend=*/false))
3999 decl = field;
4000 }
4001
4002 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */
4003 DECL_IN_AGGR_P (decl) = 0;
4004 /* Do not mark DECL as an explicit specialization if it was not
4005 already marked as an instantiation; a declaration should
4006 never be marked as a specialization unless we know what
4007 template is being specialized. */
4008 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
4009 {
4010 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
4011
4012 /* [temp.expl.spec] An explicit specialization of a static data
4013 member of a template is a definition if the declaration
4014 includes an initializer; otherwise, it is a declaration.
4015
4016 We check for processing_specialization so this only applies
4017 to the new specialization syntax. */
4018 if (!initialized && processing_specialization)
4019 DECL_EXTERNAL (decl) = 1;
4020 }
4021
4022 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
4023 pedwarn ("declaration of %q#D outside of class is not definition",
4024 decl);
4025 }
4026
4027 was_public = TREE_PUBLIC (decl);
4028
4029 /* Enter this declaration into the symbol table. */
4030 tem = maybe_push_decl (decl);
4031
4032 if (processing_template_decl)
4033 tem = push_template_decl (tem);
4034 if (tem == error_mark_node)
4035 return error_mark_node;
4036
4037 /* Tell the back-end to use or not use .common as appropriate. If we say
4038 -fconserve-space, we want this to save .data space, at the expense of
4039 wrong semantics. If we say -fno-conserve-space, we want this to
4040 produce errors about redefs; to do this we force variables into the
4041 data segment. */
4042 if (flag_conserve_space
4043 && TREE_CODE (tem) == VAR_DECL
4044 && TREE_PUBLIC (tem)
4045 && !DECL_THREAD_LOCAL_P (tem)
4046 && !have_global_bss_p ())
4047 DECL_COMMON (tem) = 1;
4048
4049 if (TREE_CODE (tem) == VAR_DECL
4050 && DECL_NAMESPACE_SCOPE_P (tem) && !TREE_PUBLIC (tem) && !was_public
4051 && !DECL_THIS_STATIC (tem) && !DECL_ARTIFICIAL (tem))
4052 {
4053 /* This is a const variable with implicit 'static'. Set
4054 DECL_THIS_STATIC so we can tell it from variables that are
4055 !TREE_PUBLIC because of the anonymous namespace. */
4056 gcc_assert (cp_type_readonly (TREE_TYPE (tem)));
4057 DECL_THIS_STATIC (tem) = 1;
4058 }
4059
4060 if (!processing_template_decl && TREE_CODE (tem) == VAR_DECL)
4061 start_decl_1 (tem, initialized);
4062
4063 return tem;
4064 }
4065
4066 void
start_decl_1(tree decl,bool initialized)4067 start_decl_1 (tree decl, bool initialized)
4068 {
4069 tree type;
4070
4071 gcc_assert (!processing_template_decl);
4072
4073 if (error_operand_p (decl))
4074 return;
4075
4076 gcc_assert (TREE_CODE (decl) == VAR_DECL);
4077 type = TREE_TYPE (decl);
4078
4079 if (initialized)
4080 /* Is it valid for this decl to have an initializer at all?
4081 If not, set INITIALIZED to zero, which will indirectly
4082 tell `cp_finish_decl' to ignore the initializer once it is parsed. */
4083 {
4084 /* Don't allow initializations for incomplete types except for
4085 arrays which might be completed by the initialization. */
4086 if (COMPLETE_TYPE_P (complete_type (type)))
4087 ; /* A complete type is ok. */
4088 else if (TREE_CODE (type) != ARRAY_TYPE)
4089 {
4090 error ("variable %q#D has initializer but incomplete type", decl);
4091 initialized = 0;
4092 type = TREE_TYPE (decl) = error_mark_node;
4093 }
4094 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
4095 {
4096 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
4097 error ("elements of array %q#D have incomplete type", decl);
4098 /* else we already gave an error in start_decl. */
4099 initialized = 0;
4100 }
4101 }
4102 else if (IS_AGGR_TYPE (type)
4103 && ! DECL_EXTERNAL (decl))
4104 {
4105 if (!COMPLETE_TYPE_P (complete_type (type)))
4106 {
4107 error ("aggregate %q#D has incomplete type and cannot be defined",
4108 decl);
4109 /* Change the type so that assemble_variable will give
4110 DECL an rtl we can live with: (mem (const_int 0)). */
4111 type = TREE_TYPE (decl) = error_mark_node;
4112 }
4113 else
4114 {
4115 /* If any base type in the hierarchy of TYPE needs a constructor,
4116 then we set initialized to 1. This way any nodes which are
4117 created for the purposes of initializing this aggregate
4118 will live as long as it does. This is necessary for global
4119 aggregates which do not have their initializers processed until
4120 the end of the file. */
4121 initialized = TYPE_NEEDS_CONSTRUCTING (type);
4122 }
4123 }
4124
4125 /* Create a new scope to hold this declaration if necessary.
4126 Whether or not a new scope is necessary cannot be determined
4127 until after the type has been completed; if the type is a
4128 specialization of a class template it is not until after
4129 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
4130 will be set correctly. */
4131 maybe_push_cleanup_level (type);
4132 }
4133
4134 /* Handle initialization of references. DECL, TYPE, and INIT have the
4135 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry,
4136 but will be set to a new CLEANUP_STMT if a temporary is created
4137 that must be destroyed subsequently.
4138
4139 Returns an initializer expression to use to initialize DECL, or
4140 NULL if the initialization can be performed statically.
4141
4142 Quotes on semantics can be found in ARM 8.4.3. */
4143
4144 static tree
grok_reference_init(tree decl,tree type,tree init,tree * cleanup)4145 grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
4146 {
4147 tree tmp;
4148
4149 if (init == NULL_TREE)
4150 {
4151 if ((DECL_LANG_SPECIFIC (decl) == 0
4152 || DECL_IN_AGGR_P (decl) == 0)
4153 && ! DECL_THIS_EXTERN (decl))
4154 error ("%qD declared as reference but not initialized", decl);
4155 return NULL_TREE;
4156 }
4157
4158 if (TREE_CODE (init) == CONSTRUCTOR)
4159 {
4160 error ("ISO C++ forbids use of initializer list to "
4161 "initialize reference %qD", decl);
4162 return NULL_TREE;
4163 }
4164
4165 if (TREE_CODE (init) == TREE_LIST)
4166 init = build_x_compound_expr_from_list (init, "initializer");
4167
4168 if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
4169 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
4170 /* Note: default conversion is only called in very special cases. */
4171 init = decay_conversion (init);
4172
4173 /* Convert INIT to the reference type TYPE. This may involve the
4174 creation of a temporary, whose lifetime must be the same as that
4175 of the reference. If so, a DECL_EXPR for the temporary will be
4176 added just after the DECL_EXPR for DECL. That's why we don't set
4177 DECL_INITIAL for local references (instead assigning to them
4178 explicitly); we need to allow the temporary to be initialized
4179 first. */
4180 tmp = initialize_reference (type, init, decl, cleanup);
4181
4182 if (tmp == error_mark_node)
4183 return NULL_TREE;
4184 else if (tmp == NULL_TREE)
4185 {
4186 error ("cannot initialize %qT from %qT", type, TREE_TYPE (init));
4187 return NULL_TREE;
4188 }
4189
4190 if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
4191 return tmp;
4192
4193 DECL_INITIAL (decl) = tmp;
4194
4195 return NULL_TREE;
4196 }
4197
4198 /* Designated initializers in arrays are not supported in GNU C++.
4199 The parser cannot detect this error since it does not know whether
4200 a given brace-enclosed initializer is for a class type or for an
4201 array. This function checks that CE does not use a designated
4202 initializer. If it does, an error is issued. Returns true if CE
4203 is valid, i.e., does not have a designated initializer. */
4204
4205 static bool
check_array_designated_initializer(const constructor_elt * ce)4206 check_array_designated_initializer (const constructor_elt *ce)
4207 {
4208 /* Designated initializers for array elements arenot supported. */
4209 if (ce->index)
4210 {
4211 /* The parser only allows identifiers as designated
4212 intializers. */
4213 gcc_assert (TREE_CODE (ce->index) == IDENTIFIER_NODE);
4214 error ("name %qD used in a GNU-style designated "
4215 "initializer for an array", ce->index);
4216 return false;
4217 }
4218
4219 return true;
4220 }
4221
4222 /* When parsing `int a[] = {1, 2};' we don't know the size of the
4223 array until we finish parsing the initializer. If that's the
4224 situation we're in, update DECL accordingly. */
4225
4226 static void
maybe_deduce_size_from_array_init(tree decl,tree init)4227 maybe_deduce_size_from_array_init (tree decl, tree init)
4228 {
4229 tree type = TREE_TYPE (decl);
4230
4231 if (TREE_CODE (type) == ARRAY_TYPE
4232 && TYPE_DOMAIN (type) == NULL_TREE
4233 && TREE_CODE (decl) != TYPE_DECL)
4234 {
4235 /* do_default is really a C-ism to deal with tentative definitions.
4236 But let's leave it here to ease the eventual merge. */
4237 int do_default = !DECL_EXTERNAL (decl);
4238 tree initializer = init ? init : DECL_INITIAL (decl);
4239 int failure = 0;
4240
4241 /* Check that there are no designated initializers in INIT, as
4242 those are not supported in GNU C++, and as the middle-end
4243 will crash if presented with a non-numeric designated
4244 initializer. */
4245 if (initializer && TREE_CODE (initializer) == CONSTRUCTOR)
4246 {
4247 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initializer);
4248 constructor_elt *ce;
4249 HOST_WIDE_INT i;
4250 for (i = 0;
4251 VEC_iterate (constructor_elt, v, i, ce);
4252 ++i)
4253 if (!check_array_designated_initializer (ce))
4254 failure = 1;
4255 }
4256
4257 if (!failure)
4258 {
4259 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer,
4260 do_default);
4261 if (failure == 1)
4262 {
4263 error ("initializer fails to determine size of %qD", decl);
4264 TREE_TYPE (decl) = error_mark_node;
4265 }
4266 else if (failure == 2)
4267 {
4268 if (do_default)
4269 {
4270 error ("array size missing in %qD", decl);
4271 TREE_TYPE (decl) = error_mark_node;
4272 }
4273 /* If a `static' var's size isn't known, make it extern as
4274 well as static, so it does not get allocated. If it's not
4275 `static', then don't mark it extern; finish_incomplete_decl
4276 will give it a default size and it will get allocated. */
4277 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4278 DECL_EXTERNAL (decl) = 1;
4279 }
4280 else if (failure == 3)
4281 {
4282 error ("zero-size array %qD", decl);
4283 TREE_TYPE (decl) = error_mark_node;
4284 }
4285 }
4286
4287 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl);
4288
4289 layout_decl (decl, 0);
4290 }
4291 }
4292
4293 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
4294 any appropriate error messages regarding the layout. */
4295
4296 static void
layout_var_decl(tree decl)4297 layout_var_decl (tree decl)
4298 {
4299 tree type;
4300
4301 type = TREE_TYPE (decl);
4302 if (type == error_mark_node)
4303 return;
4304
4305 /* If we haven't already layed out this declaration, do so now.
4306 Note that we must not call complete type for an external object
4307 because it's type might involve templates that we are not
4308 supposed to instantiate yet. (And it's perfectly valid to say
4309 `extern X x' for some incomplete type `X'.) */
4310 if (!DECL_EXTERNAL (decl))
4311 complete_type (type);
4312 if (!DECL_SIZE (decl)
4313 && TREE_TYPE (decl) != error_mark_node
4314 && (COMPLETE_TYPE_P (type)
4315 || (TREE_CODE (type) == ARRAY_TYPE
4316 && !TYPE_DOMAIN (type)
4317 && COMPLETE_TYPE_P (TREE_TYPE (type)))))
4318 layout_decl (decl, 0);
4319
4320 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
4321 {
4322 /* An automatic variable with an incomplete type: that is an error.
4323 Don't talk about array types here, since we took care of that
4324 message in grokdeclarator. */
4325 error ("storage size of %qD isn't known", decl);
4326 TREE_TYPE (decl) = error_mark_node;
4327 }
4328 #if 0
4329 /* Keep this code around in case we later want to control debug info
4330 based on whether a type is "used". (jason 1999-11-11) */
4331
4332 else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
4333 /* Let debugger know it should output info for this type. */
4334 note_debug_info_needed (ttype);
4335
4336 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
4337 note_debug_info_needed (DECL_CONTEXT (decl));
4338 #endif
4339
4340 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4341 && DECL_SIZE (decl) != NULL_TREE
4342 && ! TREE_CONSTANT (DECL_SIZE (decl)))
4343 {
4344 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4345 constant_expression_warning (DECL_SIZE (decl));
4346 else
4347 error ("storage size of %qD isn't constant", decl);
4348 }
4349 }
4350
4351 /* If a local static variable is declared in an inline function, or if
4352 we have a weak definition, we must endeavor to create only one
4353 instance of the variable at link-time. */
4354
4355 static void
maybe_commonize_var(tree decl)4356 maybe_commonize_var (tree decl)
4357 {
4358 /* Static data in a function with comdat linkage also has comdat
4359 linkage. */
4360 if (TREE_STATIC (decl)
4361 /* Don't mess with __FUNCTION__. */
4362 && ! DECL_ARTIFICIAL (decl)
4363 && DECL_FUNCTION_SCOPE_P (decl)
4364 /* Unfortunately, import_export_decl has not always been called
4365 before the function is processed, so we cannot simply check
4366 DECL_COMDAT. */
4367 && (DECL_COMDAT (DECL_CONTEXT (decl))
4368 || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
4369 || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
4370 && TREE_PUBLIC (DECL_CONTEXT (decl)))))
4371 {
4372 if (flag_weak)
4373 {
4374 /* With weak symbols, we simply make the variable COMDAT;
4375 that will cause copies in multiple translations units to
4376 be merged. */
4377 comdat_linkage (decl);
4378 }
4379 else
4380 {
4381 if (DECL_INITIAL (decl) == NULL_TREE
4382 || DECL_INITIAL (decl) == error_mark_node)
4383 {
4384 /* Without weak symbols, we can use COMMON to merge
4385 uninitialized variables. */
4386 TREE_PUBLIC (decl) = 1;
4387 DECL_COMMON (decl) = 1;
4388 }
4389 else
4390 {
4391 /* While for initialized variables, we must use internal
4392 linkage -- which means that multiple copies will not
4393 be merged. */
4394 TREE_PUBLIC (decl) = 0;
4395 DECL_COMMON (decl) = 0;
4396 warning (0, "sorry: semantics of inline function static "
4397 "data %q+#D are wrong (you'll wind up "
4398 "with multiple copies)", decl);
4399 warning (0, "%J you can work around this by removing "
4400 "the initializer",
4401 decl);
4402 }
4403 }
4404 }
4405 else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
4406 /* Set it up again; we might have set DECL_INITIAL since the last
4407 time. */
4408 comdat_linkage (decl);
4409 }
4410
4411 /* Issue an error message if DECL is an uninitialized const variable. */
4412
4413 static void
check_for_uninitialized_const_var(tree decl)4414 check_for_uninitialized_const_var (tree decl)
4415 {
4416 tree type = TREE_TYPE (decl);
4417
4418 /* ``Unless explicitly declared extern, a const object does not have
4419 external linkage and must be initialized. ($8.4; $12.1)'' ARM
4420 7.1.6 */
4421 if (TREE_CODE (decl) == VAR_DECL
4422 && TREE_CODE (type) != REFERENCE_TYPE
4423 && CP_TYPE_CONST_P (type)
4424 && !TYPE_NEEDS_CONSTRUCTING (type)
4425 && !DECL_INITIAL (decl))
4426 error ("uninitialized const %qD", decl);
4427 }
4428
4429
4430 /* Structure holding the current initializer being processed by reshape_init.
4431 CUR is a pointer to the current element being processed, END is a pointer
4432 after the last element present in the initializer. */
4433 typedef struct reshape_iterator_t
4434 {
4435 constructor_elt *cur;
4436 constructor_elt *end;
4437 } reshape_iter;
4438
4439 static tree reshape_init_r (tree, reshape_iter *, bool);
4440
4441 /* FIELD is a FIELD_DECL or NULL. In the former case, the value
4442 returned is the next FIELD_DECL (possibly FIELD itself) that can be
4443 initialized. If there are no more such fields, the return value
4444 will be NULL. */
4445
4446 static tree
next_initializable_field(tree field)4447 next_initializable_field (tree field)
4448 {
4449 while (field
4450 && (TREE_CODE (field) != FIELD_DECL
4451 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
4452 || DECL_ARTIFICIAL (field)))
4453 field = TREE_CHAIN (field);
4454
4455 return field;
4456 }
4457
4458 /* Subroutine of reshape_init_array and reshape_init_vector, which does
4459 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an
4460 INTEGER_CST representing the size of the array minus one (the maximum index),
4461 or NULL_TREE if the array was declared without specifying the size. D is
4462 the iterator within the constructor. */
4463
4464 static tree
reshape_init_array_1(tree elt_type,tree max_index,reshape_iter * d)4465 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d)
4466 {
4467 tree new_init;
4468 bool sized_array_p = (max_index != NULL_TREE);
4469 unsigned HOST_WIDE_INT max_index_cst = 0;
4470 unsigned HOST_WIDE_INT index;
4471
4472 /* The initializer for an array is always a CONSTRUCTOR. */
4473 new_init = build_constructor (NULL_TREE, NULL);
4474
4475 if (sized_array_p)
4476 {
4477 /* Minus 1 is used for zero sized arrays. */
4478 if (integer_all_onesp (max_index))
4479 return new_init;
4480
4481 if (host_integerp (max_index, 1))
4482 max_index_cst = tree_low_cst (max_index, 1);
4483 /* sizetype is sign extended, not zero extended. */
4484 else
4485 max_index_cst = tree_low_cst (fold_convert (size_type_node, max_index),
4486 1);
4487 }
4488
4489 /* Loop until there are no more initializers. */
4490 for (index = 0;
4491 d->cur != d->end && (!sized_array_p || index <= max_index_cst);
4492 ++index)
4493 {
4494 tree elt_init;
4495
4496 check_array_designated_initializer (d->cur);
4497 elt_init = reshape_init_r (elt_type, d, /*first_initializer_p=*/false);
4498 if (elt_init == error_mark_node)
4499 return error_mark_node;
4500 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), NULL_TREE, elt_init);
4501 }
4502
4503 return new_init;
4504 }
4505
4506 /* Subroutine of reshape_init_r, processes the initializers for arrays.
4507 Parameters are the same of reshape_init_r. */
4508
4509 static tree
reshape_init_array(tree type,reshape_iter * d)4510 reshape_init_array (tree type, reshape_iter *d)
4511 {
4512 tree max_index = NULL_TREE;
4513
4514 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4515
4516 if (TYPE_DOMAIN (type))
4517 max_index = array_type_nelts (type);
4518
4519 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4520 }
4521
4522 /* Subroutine of reshape_init_r, processes the initializers for vectors.
4523 Parameters are the same of reshape_init_r. */
4524
4525 static tree
reshape_init_vector(tree type,reshape_iter * d)4526 reshape_init_vector (tree type, reshape_iter *d)
4527 {
4528 tree max_index = NULL_TREE;
4529 tree rtype;
4530
4531 gcc_assert (TREE_CODE (type) == VECTOR_TYPE);
4532
4533 if (COMPOUND_LITERAL_P (d->cur->value))
4534 {
4535 tree value = d->cur->value;
4536 if (!same_type_p (TREE_TYPE (value), type))
4537 {
4538 error ("invalid type %qT as initializer for a vector of type %qT",
4539 TREE_TYPE (d->cur->value), type);
4540 value = error_mark_node;
4541 }
4542 ++d->cur;
4543 return value;
4544 }
4545
4546 /* For a vector, the representation type is a struct
4547 containing a single member which is an array of the
4548 appropriate size. */
4549 rtype = TYPE_DEBUG_REPRESENTATION_TYPE (type);
4550 if (rtype && TYPE_DOMAIN (TREE_TYPE (TYPE_FIELDS (rtype))))
4551 max_index = array_type_nelts (TREE_TYPE (TYPE_FIELDS (rtype)));
4552
4553 return reshape_init_array_1 (TREE_TYPE (type), max_index, d);
4554 }
4555
4556 /* Subroutine of reshape_init_r, processes the initializers for classes
4557 or union. Parameters are the same of reshape_init_r. */
4558
4559 static tree
reshape_init_class(tree type,reshape_iter * d,bool first_initializer_p)4560 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p)
4561 {
4562 tree field;
4563 tree new_init;
4564
4565 gcc_assert (CLASS_TYPE_P (type));
4566
4567 /* The initializer for a class is always a CONSTRUCTOR. */
4568 new_init = build_constructor (NULL_TREE, NULL);
4569 field = next_initializable_field (TYPE_FIELDS (type));
4570
4571 if (!field)
4572 {
4573 /* [dcl.init.aggr]
4574
4575 An initializer for an aggregate member that is an
4576 empty class shall have the form of an empty
4577 initializer-list {}. */
4578 if (!first_initializer_p)
4579 {
4580 error ("initializer for %qT must be brace-enclosed", type);
4581 return error_mark_node;
4582 }
4583 return new_init;
4584 }
4585
4586 /* Loop through the initializable fields, gathering initializers. */
4587 while (d->cur != d->end)
4588 {
4589 tree field_init;
4590
4591 /* Handle designated initializers, as an extension. */
4592 if (d->cur->index)
4593 {
4594 field = lookup_field_1 (type, d->cur->index, /*want_type=*/false);
4595
4596 if (!field || TREE_CODE (field) != FIELD_DECL)
4597 {
4598 error ("%qT has no non-static data member named %qD", type,
4599 d->cur->index);
4600 return error_mark_node;
4601 }
4602 }
4603
4604 /* If we processed all the member of the class, we are done. */
4605 if (!field)
4606 break;
4607
4608 field_init = reshape_init_r (TREE_TYPE (field), d,
4609 /*first_initializer_p=*/false);
4610 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init);
4611
4612 /* [dcl.init.aggr]
4613
4614 When a union is initialized with a brace-enclosed
4615 initializer, the braces shall only contain an
4616 initializer for the first member of the union. */
4617 if (TREE_CODE (type) == UNION_TYPE)
4618 break;
4619
4620 field = next_initializable_field (TREE_CHAIN (field));
4621 }
4622
4623 return new_init;
4624 }
4625
4626 /* Subroutine of reshape_init, which processes a single initializer (part of
4627 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the
4628 iterator within the CONSTRUCTOR which points to the initializer to process.
4629 FIRST_INITIALIZER_P is true if this is the first initializer of the
4630 CONSTRUCTOR node. */
4631
4632 static tree
reshape_init_r(tree type,reshape_iter * d,bool first_initializer_p)4633 reshape_init_r (tree type, reshape_iter *d, bool first_initializer_p)
4634 {
4635 tree init = d->cur->value;
4636
4637 /* A non-aggregate type is always initialized with a single
4638 initializer. */
4639 if (!CP_AGGREGATE_TYPE_P (type))
4640 {
4641 /* It is invalid to initialize a non-aggregate type with a
4642 brace-enclosed initializer.
4643 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because
4644 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is
4645 a CONSTRUCTOR (with a record type). */
4646 if (TREE_CODE (init) == CONSTRUCTOR
4647 && BRACE_ENCLOSED_INITIALIZER_P (init)) /* p7626.C */
4648 {
4649 error ("braces around scalar initializer for type %qT", type);
4650 init = error_mark_node;
4651 }
4652
4653 d->cur++;
4654 return init;
4655 }
4656
4657 /* [dcl.init.aggr]
4658
4659 All implicit type conversions (clause _conv_) are considered when
4660 initializing the aggregate member with an initializer from an
4661 initializer-list. If the initializer can initialize a member,
4662 the member is initialized. Otherwise, if the member is itself a
4663 non-empty subaggregate, brace elision is assumed and the
4664 initializer is considered for the initialization of the first
4665 member of the subaggregate. */
4666 if (TREE_CODE (init) != CONSTRUCTOR
4667 && can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL))
4668 {
4669 d->cur++;
4670 return init;
4671 }
4672
4673 /* [dcl.init.string]
4674
4675 A char array (whether plain char, signed char, or unsigned char)
4676 can be initialized by a string-literal (optionally enclosed in
4677 braces); a wchar_t array can be initialized by a wide
4678 string-literal (optionally enclosed in braces). */
4679 if (TREE_CODE (type) == ARRAY_TYPE
4680 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
4681 {
4682 tree str_init = init;
4683
4684 /* Strip one level of braces if and only if they enclose a single
4685 element (as allowed by [dcl.init.string]). */
4686 if (!first_initializer_p
4687 && TREE_CODE (str_init) == CONSTRUCTOR
4688 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (str_init)) == 1)
4689 {
4690 str_init = VEC_index (constructor_elt,
4691 CONSTRUCTOR_ELTS (str_init), 0)->value;
4692 }
4693
4694 /* If it's a string literal, then it's the initializer for the array
4695 as a whole. Otherwise, continue with normal initialization for
4696 array types (one value per array element). */
4697 if (TREE_CODE (str_init) == STRING_CST)
4698 {
4699 d->cur++;
4700 return str_init;
4701 }
4702 }
4703
4704 /* The following cases are about aggregates. If we are not within a full
4705 initializer already, and there is not a CONSTRUCTOR, it means that there
4706 is a missing set of braces (that is, we are processing the case for
4707 which reshape_init exists). */
4708 if (!first_initializer_p)
4709 {
4710 if (TREE_CODE (init) == CONSTRUCTOR)
4711 {
4712 if (TREE_TYPE (init) && TYPE_PTRMEMFUNC_P (TREE_TYPE (init)))
4713 /* There is no need to reshape pointer-to-member function
4714 initializers, as they are always constructed correctly
4715 by the front end. */
4716 ;
4717 else if (COMPOUND_LITERAL_P (init))
4718 /* For a nested compound literal, there is no need to reshape since
4719 brace elision is not allowed. Even if we decided to allow it,
4720 we should add a call to reshape_init in finish_compound_literal,
4721 before calling digest_init, so changing this code would still
4722 not be necessary. */
4723 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (init));
4724 else
4725 {
4726 ++d->cur;
4727 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4728 return reshape_init (type, init);
4729 }
4730 }
4731
4732 warning (OPT_Wmissing_braces, "missing braces around initializer for %qT",
4733 type);
4734 }
4735
4736 /* Dispatch to specialized routines. */
4737 if (CLASS_TYPE_P (type))
4738 return reshape_init_class (type, d, first_initializer_p);
4739 else if (TREE_CODE (type) == ARRAY_TYPE)
4740 return reshape_init_array (type, d);
4741 else if (TREE_CODE (type) == VECTOR_TYPE)
4742 return reshape_init_vector (type, d);
4743 else
4744 gcc_unreachable();
4745 }
4746
4747 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
4748 brace-enclosed aggregate initializer.
4749
4750 INIT is the CONSTRUCTOR containing the list of initializers describing
4751 a brace-enclosed initializer for an entity of the indicated aggregate TYPE.
4752 It may not presently match the shape of the TYPE; for example:
4753
4754 struct S { int a; int b; };
4755 struct S a[] = { 1, 2, 3, 4 };
4756
4757 Here INIT will hold a VEC of four elements, rather than a
4758 VEC of two elements, each itself a VEC of two elements. This
4759 routine transforms INIT from the former form into the latter. The
4760 revised CONSTRUCTOR node is returned. */
4761
4762 tree
reshape_init(tree type,tree init)4763 reshape_init (tree type, tree init)
4764 {
4765 VEC(constructor_elt, gc) *v;
4766 reshape_iter d;
4767 tree new_init;
4768
4769 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
4770
4771 v = CONSTRUCTOR_ELTS (init);
4772
4773 /* An empty constructor does not need reshaping, and it is always a valid
4774 initializer. */
4775 if (VEC_empty (constructor_elt, v))
4776 return init;
4777
4778 /* Recurse on this CONSTRUCTOR. */
4779 d.cur = VEC_index (constructor_elt, v, 0);
4780 d.end = d.cur + VEC_length (constructor_elt, v);
4781
4782 new_init = reshape_init_r (type, &d, true);
4783 if (new_init == error_mark_node)
4784 return error_mark_node;
4785
4786 /* Make sure all the element of the constructor were used. Otherwise,
4787 issue an error about exceeding initializers. */
4788 if (d.cur != d.end)
4789 error ("too many initializers for %qT", type);
4790
4791 return new_init;
4792 }
4793
4794 /* Verify INIT (the initializer for DECL), and record the
4795 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for
4796 grok_reference_init.
4797
4798 If the return value is non-NULL, it is an expression that must be
4799 evaluated dynamically to initialize DECL. */
4800
4801 static tree
check_initializer(tree decl,tree init,int flags,tree * cleanup)4802 check_initializer (tree decl, tree init, int flags, tree *cleanup)
4803 {
4804 tree type = TREE_TYPE (decl);
4805 tree init_code = NULL;
4806
4807 /* Things that are going to be initialized need to have complete
4808 type. */
4809 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
4810
4811 if (type == error_mark_node)
4812 /* We will have already complained. */
4813 return NULL_TREE;
4814
4815 if (TREE_CODE (type) == ARRAY_TYPE)
4816 {
4817 tree element_type = TREE_TYPE (type);
4818
4819 /* The array type itself need not be complete, because the
4820 initializer may tell us how many elements are in the array.
4821 But, the elements of the array must be complete. */
4822 if (!COMPLETE_TYPE_P (complete_type (element_type)))
4823 {
4824 error ("elements of array %q#D have incomplete type", decl);
4825 return NULL_TREE;
4826 }
4827 /* It is not valid to initialize an a VLA. */
4828 if (init
4829 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
4830 || !TREE_CONSTANT (TYPE_SIZE (element_type))))
4831 {
4832 error ("variable-sized object %qD may not be initialized", decl);
4833 return NULL_TREE;
4834 }
4835 }
4836 else if (!COMPLETE_TYPE_P (type))
4837 {
4838 error ("%qD has incomplete type", decl);
4839 TREE_TYPE (decl) = error_mark_node;
4840 return NULL_TREE;
4841 }
4842 else
4843 /* There is no way to make a variable-sized class type in GNU C++. */
4844 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
4845
4846 if (!CP_AGGREGATE_TYPE_P (type)
4847 && init && BRACE_ENCLOSED_INITIALIZER_P (init)
4848 && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1)
4849 {
4850 error ("scalar object %qD requires one element in initializer", decl);
4851 TREE_TYPE (decl) = error_mark_node;
4852 return NULL_TREE;
4853 }
4854
4855 if (TREE_CODE (decl) == CONST_DECL)
4856 {
4857 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
4858
4859 DECL_INITIAL (decl) = init;
4860
4861 gcc_assert (init != NULL_TREE);
4862 init = NULL_TREE;
4863 }
4864 else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
4865 init = grok_reference_init (decl, type, init, cleanup);
4866 else if (init)
4867 {
4868 /* Do not reshape constructors of vectors (they don't need to be
4869 reshaped. */
4870 if (TREE_CODE (init) == CONSTRUCTOR
4871 && !COMPOUND_LITERAL_P (init)
4872 && !TREE_TYPE (init)) /* ptrmemfunc */
4873 {
4874 init = reshape_init (type, init);
4875
4876 if ((*targetm.vector_opaque_p) (type))
4877 {
4878 error ("opaque vector types cannot be initialized");
4879 init = error_mark_node;
4880 }
4881 }
4882
4883 /* If DECL has an array type without a specific bound, deduce the
4884 array size from the initializer. */
4885 maybe_deduce_size_from_array_init (decl, init);
4886 type = TREE_TYPE (decl);
4887 if (type == error_mark_node)
4888 return NULL_TREE;
4889
4890 if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
4891 {
4892 if (TREE_CODE (type) == ARRAY_TYPE)
4893 goto initialize_aggr;
4894 else if (TREE_CODE (init) == CONSTRUCTOR)
4895 {
4896 if (TYPE_NON_AGGREGATE_CLASS (type))
4897 {
4898 error ("%qD must be initialized by constructor, "
4899 "not by %<{...}%>",
4900 decl);
4901 init = error_mark_node;
4902 }
4903 else
4904 goto dont_use_constructor;
4905 }
4906 else
4907 {
4908 int saved_stmts_are_full_exprs_p;
4909
4910 initialize_aggr:
4911 saved_stmts_are_full_exprs_p = 0;
4912 if (building_stmt_tree ())
4913 {
4914 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
4915 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
4916 }
4917 init = build_aggr_init (decl, init, flags);
4918 if (building_stmt_tree ())
4919 current_stmt_tree ()->stmts_are_full_exprs_p =
4920 saved_stmts_are_full_exprs_p;
4921 return init;
4922 }
4923 }
4924 else
4925 {
4926 dont_use_constructor:
4927 if (TREE_CODE (init) != TREE_VEC)
4928 {
4929 init_code = store_init_value (decl, init);
4930 if (pedantic && TREE_CODE (type) == ARRAY_TYPE
4931 && DECL_INITIAL (decl)
4932 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
4933 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl)))
4934 warning (0, "array %qD initialized by parenthesized string literal %qE",
4935 decl, DECL_INITIAL (decl));
4936 init = NULL;
4937 }
4938 }
4939 }
4940 else if (DECL_EXTERNAL (decl))
4941 ;
4942 else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
4943 goto initialize_aggr;
4944 else if (IS_AGGR_TYPE (type))
4945 {
4946 tree core_type = strip_array_types (type);
4947
4948 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
4949 error ("structure %qD with uninitialized const members", decl);
4950 if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
4951 error ("structure %qD with uninitialized reference members", decl);
4952
4953 check_for_uninitialized_const_var (decl);
4954 }
4955 else
4956 check_for_uninitialized_const_var (decl);
4957
4958 if (init && init != error_mark_node)
4959 init_code = build2 (INIT_EXPR, type, decl, init);
4960
4961 return init_code;
4962 }
4963
4964 /* If DECL is not a local variable, give it RTL. */
4965
4966 static void
make_rtl_for_nonlocal_decl(tree decl,tree init,const char * asmspec)4967 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
4968 {
4969 int toplev = toplevel_bindings_p ();
4970 int defer_p;
4971 const char *filename;
4972
4973 /* Set the DECL_ASSEMBLER_NAME for the object. */
4974 if (asmspec)
4975 {
4976 /* The `register' keyword, when used together with an
4977 asm-specification, indicates that the variable should be
4978 placed in a particular register. */
4979 if (TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
4980 {
4981 set_user_assembler_name (decl, asmspec);
4982 DECL_HARD_REGISTER (decl) = 1;
4983 }
4984 else
4985 {
4986 if (TREE_CODE (decl) == FUNCTION_DECL
4987 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4988 set_builtin_user_assembler_name (decl, asmspec);
4989 set_user_assembler_name (decl, asmspec);
4990 }
4991 }
4992
4993 /* Handle non-variables up front. */
4994 if (TREE_CODE (decl) != VAR_DECL)
4995 {
4996 rest_of_decl_compilation (decl, toplev, at_eof);
4997 return;
4998 }
4999
5000 /* If we see a class member here, it should be a static data
5001 member. */
5002 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
5003 {
5004 gcc_assert (TREE_STATIC (decl));
5005 /* An in-class declaration of a static data member should be
5006 external; it is only a declaration, and not a definition. */
5007 if (init == NULL_TREE)
5008 gcc_assert (DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl));
5009 }
5010
5011 /* We don't create any RTL for local variables. */
5012 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
5013 return;
5014
5015 /* We defer emission of local statics until the corresponding
5016 DECL_EXPR is expanded. */
5017 defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
5018
5019 /* We try to defer namespace-scope static constants so that they are
5020 not emitted into the object file unnecessarily. */
5021 filename = input_filename;
5022 if (!DECL_VIRTUAL_P (decl)
5023 && TREE_READONLY (decl)
5024 && DECL_INITIAL (decl) != NULL_TREE
5025 && DECL_INITIAL (decl) != error_mark_node
5026 && filename != NULL
5027 && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
5028 && toplev
5029 && !TREE_PUBLIC (decl))
5030 {
5031 /* Fool with the linkage of static consts according to #pragma
5032 interface. */
5033 struct c_fileinfo *finfo = get_fileinfo (filename);
5034 if (!finfo->interface_unknown && !TREE_PUBLIC (decl))
5035 {
5036 TREE_PUBLIC (decl) = 1;
5037 DECL_EXTERNAL (decl) = finfo->interface_only;
5038 }
5039
5040 defer_p = 1;
5041 }
5042 /* Likewise for template instantiations. */
5043 else if (DECL_LANG_SPECIFIC (decl)
5044 && DECL_IMPLICIT_INSTANTIATION (decl))
5045 defer_p = 1;
5046
5047 /* If we're not deferring, go ahead and assemble the variable. */
5048 if (!defer_p)
5049 rest_of_decl_compilation (decl, toplev, at_eof);
5050 }
5051
5052 /* Generate code to initialize DECL (a local variable). */
5053
5054 static void
initialize_local_var(tree decl,tree init)5055 initialize_local_var (tree decl, tree init)
5056 {
5057 tree type = TREE_TYPE (decl);
5058 tree cleanup;
5059
5060 gcc_assert (TREE_CODE (decl) == VAR_DECL
5061 || TREE_CODE (decl) == RESULT_DECL);
5062 gcc_assert (!TREE_STATIC (decl));
5063
5064 if (DECL_SIZE (decl) == NULL_TREE)
5065 {
5066 /* If we used it already as memory, it must stay in memory. */
5067 DECL_INITIAL (decl) = NULL_TREE;
5068 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
5069 }
5070
5071 if (DECL_SIZE (decl) && type != error_mark_node)
5072 {
5073 int already_used;
5074
5075 /* Compute and store the initial value. */
5076 already_used = TREE_USED (decl) || TREE_USED (type);
5077
5078 /* Perform the initialization. */
5079 if (init)
5080 {
5081 int saved_stmts_are_full_exprs_p;
5082
5083 gcc_assert (building_stmt_tree ());
5084 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
5085 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
5086 finish_expr_stmt (init);
5087 current_stmt_tree ()->stmts_are_full_exprs_p =
5088 saved_stmts_are_full_exprs_p;
5089 }
5090
5091 /* Set this to 0 so we can tell whether an aggregate which was
5092 initialized was ever used. Don't do this if it has a
5093 destructor, so we don't complain about the 'resource
5094 allocation is initialization' idiom. Now set
5095 attribute((unused)) on types so decls of that type will be
5096 marked used. (see TREE_USED, above.) */
5097 if (TYPE_NEEDS_CONSTRUCTING (type)
5098 && ! already_used
5099 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
5100 && DECL_NAME (decl))
5101 TREE_USED (decl) = 0;
5102 else if (already_used)
5103 TREE_USED (decl) = 1;
5104 }
5105
5106 /* Generate a cleanup, if necessary. */
5107 cleanup = cxx_maybe_build_cleanup (decl);
5108 if (DECL_SIZE (decl) && cleanup)
5109 finish_decl_cleanup (decl, cleanup);
5110 }
5111
5112 /* DECL is a VAR_DECL for a compiler-generated variable with static
5113 storage duration (like a virtual table) whose initializer is a
5114 compile-time constant. INIT must be either a TREE_LIST of values,
5115 or a CONSTRUCTOR. Initialize the variable and provide it to the
5116 back end. */
5117
5118 void
initialize_artificial_var(tree decl,tree init)5119 initialize_artificial_var (tree decl, tree init)
5120 {
5121 gcc_assert (DECL_ARTIFICIAL (decl));
5122 if (TREE_CODE (init) == TREE_LIST)
5123 init = build_constructor_from_list (NULL_TREE, init);
5124 gcc_assert (TREE_CODE (init) == CONSTRUCTOR);
5125 DECL_INITIAL (decl) = init;
5126 DECL_INITIALIZED_P (decl) = 1;
5127 determine_visibility (decl);
5128 layout_var_decl (decl);
5129 maybe_commonize_var (decl);
5130 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL);
5131 }
5132
5133 /* INIT is the initializer for a variable, as represented by the
5134 parser. Returns true iff INIT is value-dependent. */
5135
5136 static bool
value_dependent_init_p(tree init)5137 value_dependent_init_p (tree init)
5138 {
5139 if (TREE_CODE (init) == TREE_LIST)
5140 /* A parenthesized initializer, e.g.: int i (3, 2); ? */
5141 return any_value_dependent_elements_p (init);
5142 else if (TREE_CODE (init) == CONSTRUCTOR)
5143 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */
5144 {
5145 VEC(constructor_elt, gc) *elts;
5146 size_t nelts;
5147 size_t i;
5148
5149 elts = CONSTRUCTOR_ELTS (init);
5150 nelts = VEC_length (constructor_elt, elts);
5151 for (i = 0; i < nelts; ++i)
5152 if (value_dependent_init_p (VEC_index (constructor_elt,
5153 elts, i)->value))
5154 return true;
5155 }
5156 else
5157 /* It must be a simple expression, e.g., int i = 3; */
5158 return value_dependent_expression_p (init);
5159
5160 return false;
5161 }
5162
5163 /* Finish processing of a declaration;
5164 install its line number and initial value.
5165 If the length of an array type is not known before,
5166 it must be determined now, from the initial value, or it is an error.
5167
5168 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is
5169 true, then INIT is an integral constant expression.
5170
5171 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
5172 if the (init) syntax was used. */
5173
5174 void
cp_finish_decl(tree decl,tree init,bool init_const_expr_p,tree asmspec_tree,int flags)5175 cp_finish_decl (tree decl, tree init, bool init_const_expr_p,
5176 tree asmspec_tree, int flags)
5177 {
5178 tree type;
5179 tree cleanup;
5180 const char *asmspec = NULL;
5181 int was_readonly = 0;
5182 bool var_definition_p = false;
5183 int saved_processing_template_decl;
5184
5185 if (decl == error_mark_node)
5186 return;
5187 else if (! decl)
5188 {
5189 if (init)
5190 error ("assignment (not initialization) in declaration");
5191 return;
5192 }
5193
5194 gcc_assert (TREE_CODE (decl) != RESULT_DECL);
5195 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */
5196 gcc_assert (TREE_CODE (decl) != PARM_DECL);
5197
5198 type = TREE_TYPE (decl);
5199 if (type == error_mark_node)
5200 return;
5201
5202 /* Assume no cleanup is required. */
5203 cleanup = NULL_TREE;
5204 saved_processing_template_decl = processing_template_decl;
5205
5206 /* If a name was specified, get the string. */
5207 if (global_scope_p (current_binding_level))
5208 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
5209 if (asmspec_tree && asmspec_tree != error_mark_node)
5210 asmspec = TREE_STRING_POINTER (asmspec_tree);
5211
5212 if (current_class_type
5213 && CP_DECL_CONTEXT (decl) == current_class_type
5214 && TYPE_BEING_DEFINED (current_class_type)
5215 && (DECL_INITIAL (decl) || init))
5216 DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
5217
5218 if (processing_template_decl)
5219 {
5220 bool type_dependent_p;
5221
5222 /* Add this declaration to the statement-tree. */
5223 if (at_function_scope_p ())
5224 add_decl_expr (decl);
5225
5226 type_dependent_p = dependent_type_p (type);
5227
5228 if (init && init_const_expr_p)
5229 {
5230 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5231 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5232 TREE_CONSTANT (decl) = 1;
5233 }
5234
5235 /* Generally, initializers in templates are expanded when the
5236 template is instantiated. But, if DECL is an integral
5237 constant static data member, then it can be used in future
5238 integral constant expressions, and its value must be
5239 available. */
5240 if (!(init
5241 && DECL_CLASS_SCOPE_P (decl)
5242 && DECL_INTEGRAL_CONSTANT_VAR_P (decl)
5243 && !type_dependent_p
5244 && !value_dependent_init_p (init)))
5245 {
5246 if (init)
5247 DECL_INITIAL (decl) = init;
5248 if (TREE_CODE (decl) == VAR_DECL
5249 && !DECL_PRETTY_FUNCTION_P (decl)
5250 && !type_dependent_p)
5251 maybe_deduce_size_from_array_init (decl, init);
5252 goto finish_end;
5253 }
5254
5255 init = fold_non_dependent_expr (init);
5256 processing_template_decl = 0;
5257 }
5258
5259 /* Take care of TYPE_DECLs up front. */
5260 if (TREE_CODE (decl) == TYPE_DECL)
5261 {
5262 if (type != error_mark_node
5263 && IS_AGGR_TYPE (type) && DECL_NAME (decl))
5264 {
5265 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
5266 warning (0, "shadowing previous type declaration of %q#D", decl);
5267 set_identifier_type_value (DECL_NAME (decl), decl);
5268 }
5269
5270 /* If we have installed this as the canonical typedef for this
5271 type, and that type has not been defined yet, delay emitting
5272 the debug information for it, as we will emit it later. */
5273 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
5274 && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
5275 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
5276
5277 rest_of_decl_compilation (decl, DECL_CONTEXT (decl) == NULL_TREE,
5278 at_eof);
5279 goto finish_end;
5280 }
5281
5282 /* A reference will be modified here, as it is initialized. */
5283 if (! DECL_EXTERNAL (decl)
5284 && TREE_READONLY (decl)
5285 && TREE_CODE (type) == REFERENCE_TYPE)
5286 {
5287 was_readonly = 1;
5288 TREE_READONLY (decl) = 0;
5289 }
5290
5291 if (TREE_CODE (decl) == VAR_DECL)
5292 {
5293 /* Only PODs can have thread-local storage. Other types may require
5294 various kinds of non-trivial initialization. */
5295 if (DECL_THREAD_LOCAL_P (decl) && !pod_type_p (TREE_TYPE (decl)))
5296 error ("%qD cannot be thread-local because it has non-POD type %qT",
5297 decl, TREE_TYPE (decl));
5298 /* If this is a local variable that will need a mangled name,
5299 register it now. We must do this before processing the
5300 initializer for the variable, since the initialization might
5301 require a guard variable, and since the mangled name of the
5302 guard variable will depend on the mangled name of this
5303 variable. */
5304 if (!processing_template_decl
5305 && DECL_FUNCTION_SCOPE_P (decl)
5306 && TREE_STATIC (decl)
5307 && !DECL_ARTIFICIAL (decl))
5308 push_local_name (decl);
5309 /* Convert the initializer to the type of DECL, if we have not
5310 already initialized DECL. */
5311 if (!DECL_INITIALIZED_P (decl)
5312 /* If !DECL_EXTERNAL then DECL is being defined. In the
5313 case of a static data member initialized inside the
5314 class-specifier, there can be an initializer even if DECL
5315 is *not* defined. */
5316 && (!DECL_EXTERNAL (decl) || init))
5317 {
5318 if (init)
5319 {
5320 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1;
5321 if (init_const_expr_p)
5322 {
5323 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
5324 if (DECL_INTEGRAL_CONSTANT_VAR_P (decl))
5325 TREE_CONSTANT (decl) = 1;
5326 }
5327 }
5328 init = check_initializer (decl, init, flags, &cleanup);
5329 /* Thread-local storage cannot be dynamically initialized. */
5330 if (DECL_THREAD_LOCAL_P (decl) && init)
5331 {
5332 error ("%qD is thread-local and so cannot be dynamically "
5333 "initialized", decl);
5334 init = NULL_TREE;
5335 }
5336
5337 /* Check that the initializer for a static data member was a
5338 constant. Although we check in the parser that the
5339 initializer is an integral constant expression, we do not
5340 simplify division-by-zero at the point at which it
5341 occurs. Therefore, in:
5342
5343 struct S { static const int i = 7 / 0; };
5344
5345 we issue an error at this point. It would
5346 probably be better to forbid division by zero in
5347 integral constant expressions. */
5348 if (DECL_EXTERNAL (decl) && init)
5349 {
5350 error ("%qD cannot be initialized by a non-constant expression"
5351 " when being declared", decl);
5352 DECL_INITIALIZED_IN_CLASS_P (decl) = 0;
5353 init = NULL_TREE;
5354 }
5355
5356 /* Handle:
5357
5358 [dcl.init]
5359
5360 The memory occupied by any object of static storage
5361 duration is zero-initialized at program startup before
5362 any other initialization takes place.
5363
5364 We cannot create an appropriate initializer until after
5365 the type of DECL is finalized. If DECL_INITIAL is set,
5366 then the DECL is statically initialized, and any
5367 necessary zero-initialization has already been performed. */
5368 if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
5369 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
5370 /*nelts=*/NULL_TREE,
5371 /*static_storage_p=*/true);
5372 /* Remember that the initialization for this variable has
5373 taken place. */
5374 DECL_INITIALIZED_P (decl) = 1;
5375 /* This declaration is the definition of this variable,
5376 unless we are initializing a static data member within
5377 the class specifier. */
5378 if (!DECL_EXTERNAL (decl))
5379 var_definition_p = true;
5380 }
5381 /* If the variable has an array type, lay out the type, even if
5382 there is no initializer. It is valid to index through the
5383 array, and we must get TYPE_ALIGN set correctly on the array
5384 type. */
5385 else if (TREE_CODE (type) == ARRAY_TYPE)
5386 layout_type (type);
5387 }
5388
5389 /* Add this declaration to the statement-tree. This needs to happen
5390 after the call to check_initializer so that the DECL_EXPR for a
5391 reference temp is added before the DECL_EXPR for the reference itself. */
5392 if (at_function_scope_p ())
5393 add_decl_expr (decl);
5394
5395 /* Let the middle end know about variables and functions -- but not
5396 static data members in uninstantiated class templates. */
5397 if (!saved_processing_template_decl
5398 && (TREE_CODE (decl) == VAR_DECL
5399 || TREE_CODE (decl) == FUNCTION_DECL))
5400 {
5401 if (TREE_CODE (decl) == VAR_DECL)
5402 {
5403 layout_var_decl (decl);
5404 maybe_commonize_var (decl);
5405 }
5406
5407 make_rtl_for_nonlocal_decl (decl, init, asmspec);
5408
5409 /* Check for abstractness of the type. Notice that there is no
5410 need to strip array types here since the check for those types
5411 is already done within create_array_type_for_decl. */
5412 if (TREE_CODE (type) == FUNCTION_TYPE
5413 || TREE_CODE (type) == METHOD_TYPE)
5414 abstract_virtuals_error (decl, TREE_TYPE (type));
5415 else
5416 abstract_virtuals_error (decl, type);
5417
5418 /* This needs to happen after the linkage is set. */
5419 determine_visibility (decl);
5420
5421 if (TREE_CODE (decl) == FUNCTION_DECL
5422 || TREE_TYPE (decl) == error_mark_node)
5423 /* No initialization required. */
5424 ;
5425 else if (DECL_EXTERNAL (decl)
5426 && ! (DECL_LANG_SPECIFIC (decl)
5427 && DECL_NOT_REALLY_EXTERN (decl)))
5428 {
5429 if (init)
5430 DECL_INITIAL (decl) = init;
5431 }
5432 else
5433 {
5434 /* A variable definition. */
5435 if (DECL_FUNCTION_SCOPE_P (decl))
5436 {
5437 /* Initialize the local variable. */
5438 if (processing_template_decl)
5439 DECL_INITIAL (decl) = init;
5440 else if (!TREE_STATIC (decl))
5441 initialize_local_var (decl, init);
5442 }
5443
5444 /* If a variable is defined, and then a subsequent
5445 definition with external linkage is encountered, we will
5446 get here twice for the same variable. We want to avoid
5447 calling expand_static_init more than once. For variables
5448 that are not static data members, we can call
5449 expand_static_init only when we actually process the
5450 initializer. It is not legal to redeclare a static data
5451 member, so this issue does not arise in that case. */
5452 if (var_definition_p && TREE_STATIC (decl))
5453 {
5454 /* If a TREE_READONLY variable needs initialization
5455 at runtime, it is no longer readonly and we need to
5456 avoid MEM_READONLY_P being set on RTL created for it. */
5457 if (init)
5458 {
5459 if (TREE_READONLY (decl))
5460 TREE_READONLY (decl) = 0;
5461 was_readonly = 0;
5462 }
5463 expand_static_init (decl, init);
5464 }
5465 }
5466 }
5467
5468 /* If a CLEANUP_STMT was created to destroy a temporary bound to a
5469 reference, insert it in the statement-tree now. */
5470 if (cleanup)
5471 push_cleanup (decl, cleanup, false);
5472
5473 finish_end:
5474 processing_template_decl = saved_processing_template_decl;
5475
5476 if (was_readonly)
5477 TREE_READONLY (decl) = 1;
5478
5479 /* If this was marked 'used', be sure it will be output. */
5480 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
5481 mark_decl_referenced (decl);
5482 }
5483
5484 /* This is here for a midend callback from c-common.c. */
5485
5486 void
finish_decl(tree decl,tree init,tree asmspec_tree)5487 finish_decl (tree decl, tree init, tree asmspec_tree)
5488 {
5489 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, asmspec_tree, 0);
5490 }
5491
5492 /* Returns a declaration for a VAR_DECL as if:
5493
5494 extern "C" TYPE NAME;
5495
5496 had been seen. Used to create compiler-generated global
5497 variables. */
5498
5499 static tree
declare_global_var(tree name,tree type)5500 declare_global_var (tree name, tree type)
5501 {
5502 tree decl;
5503
5504 push_to_top_level ();
5505 decl = build_decl (VAR_DECL, name, type);
5506 TREE_PUBLIC (decl) = 1;
5507 DECL_EXTERNAL (decl) = 1;
5508 DECL_ARTIFICIAL (decl) = 1;
5509 /* If the user has explicitly declared this variable (perhaps
5510 because the code we are compiling is part of a low-level runtime
5511 library), then it is possible that our declaration will be merged
5512 with theirs by pushdecl. */
5513 decl = pushdecl (decl);
5514 finish_decl (decl, NULL_TREE, NULL_TREE);
5515 pop_from_top_level ();
5516
5517 return decl;
5518 }
5519
5520 /* Returns a pointer to the `atexit' function. Note that if
5521 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
5522 `__cxa_atexit' function specified in the IA64 C++ ABI. */
5523
5524 static tree
get_atexit_node(void)5525 get_atexit_node (void)
5526 {
5527 tree atexit_fndecl;
5528 tree arg_types;
5529 tree fn_type;
5530 tree fn_ptr_type;
5531 const char *name;
5532 bool use_aeabi_atexit;
5533
5534 if (atexit_node)
5535 return atexit_node;
5536
5537 if (flag_use_cxa_atexit)
5538 {
5539 /* The declaration for `__cxa_atexit' is:
5540
5541 int __cxa_atexit (void (*)(void *), void *, void *)
5542
5543 We build up the argument types and then then function type
5544 itself. */
5545
5546 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit ();
5547 /* First, build the pointer-to-function type for the first
5548 argument. */
5549 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5550 fn_type = build_function_type (void_type_node, arg_types);
5551 fn_ptr_type = build_pointer_type (fn_type);
5552 /* Then, build the rest of the argument types. */
5553 arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
5554 if (use_aeabi_atexit)
5555 {
5556 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5557 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5558 }
5559 else
5560 {
5561 arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
5562 arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
5563 }
5564 /* And the final __cxa_atexit type. */
5565 fn_type = build_function_type (integer_type_node, arg_types);
5566 fn_ptr_type = build_pointer_type (fn_type);
5567 if (use_aeabi_atexit)
5568 name = "__aeabi_atexit";
5569 else
5570 name = "__cxa_atexit";
5571 }
5572 else
5573 {
5574 /* The declaration for `atexit' is:
5575
5576 int atexit (void (*)());
5577
5578 We build up the argument types and then then function type
5579 itself. */
5580 fn_type = build_function_type (void_type_node, void_list_node);
5581 fn_ptr_type = build_pointer_type (fn_type);
5582 arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
5583 /* Build the final atexit type. */
5584 fn_type = build_function_type (integer_type_node, arg_types);
5585 name = "atexit";
5586 }
5587
5588 /* Now, build the function declaration. */
5589 push_lang_context (lang_name_c);
5590 atexit_fndecl = build_library_fn_ptr (name, fn_type);
5591 mark_used (atexit_fndecl);
5592 pop_lang_context ();
5593 atexit_node = decay_conversion (atexit_fndecl);
5594
5595 return atexit_node;
5596 }
5597
5598 /* Returns the __dso_handle VAR_DECL. */
5599
5600 static tree
get_dso_handle_node(void)5601 get_dso_handle_node (void)
5602 {
5603 if (dso_handle_node)
5604 return dso_handle_node;
5605
5606 /* Declare the variable. */
5607 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
5608 ptr_type_node);
5609
5610 return dso_handle_node;
5611 }
5612
5613 /* Begin a new function with internal linkage whose job will be simply
5614 to destroy some particular variable. */
5615
5616 static GTY(()) int start_cleanup_cnt;
5617
5618 static tree
start_cleanup_fn(void)5619 start_cleanup_fn (void)
5620 {
5621 char name[32];
5622 tree parmtypes;
5623 tree fntype;
5624 tree fndecl;
5625
5626 push_to_top_level ();
5627
5628 /* No need to mangle this. */
5629 push_lang_context (lang_name_c);
5630
5631 /* Build the parameter-types. */
5632 parmtypes = void_list_node;
5633 /* Functions passed to __cxa_atexit take an additional parameter.
5634 We'll just ignore it. After we implement the new calling
5635 convention for destructors, we can eliminate the use of
5636 additional cleanup functions entirely in the -fnew-abi case. */
5637 if (flag_use_cxa_atexit)
5638 parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
5639 /* Build the function type itself. */
5640 fntype = build_function_type (void_type_node, parmtypes);
5641 /* Build the name of the function. */
5642 sprintf (name, "__tcf_%d", start_cleanup_cnt++);
5643 /* Build the function declaration. */
5644 fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
5645 /* It's a function with internal linkage, generated by the
5646 compiler. */
5647 TREE_PUBLIC (fndecl) = 0;
5648 DECL_ARTIFICIAL (fndecl) = 1;
5649 /* Make the function `inline' so that it is only emitted if it is
5650 actually needed. It is unlikely that it will be inlined, since
5651 it is only called via a function pointer, but we avoid unnecessary
5652 emissions this way. */
5653 DECL_INLINE (fndecl) = 1;
5654 DECL_DECLARED_INLINE_P (fndecl) = 1;
5655 DECL_INTERFACE_KNOWN (fndecl) = 1;
5656 /* Build the parameter. */
5657 if (flag_use_cxa_atexit)
5658 {
5659 tree parmdecl;
5660
5661 parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
5662 DECL_CONTEXT (parmdecl) = fndecl;
5663 TREE_USED (parmdecl) = 1;
5664 DECL_ARGUMENTS (fndecl) = parmdecl;
5665 }
5666
5667 pushdecl (fndecl);
5668 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
5669
5670 pop_lang_context ();
5671
5672 return current_function_decl;
5673 }
5674
5675 /* Finish the cleanup function begun by start_cleanup_fn. */
5676
5677 static void
end_cleanup_fn(void)5678 end_cleanup_fn (void)
5679 {
5680 expand_or_defer_fn (finish_function (0));
5681
5682 pop_from_top_level ();
5683 }
5684
5685 /* Generate code to handle the destruction of DECL, an object with
5686 static storage duration. */
5687
5688 tree
register_dtor_fn(tree decl)5689 register_dtor_fn (tree decl)
5690 {
5691 tree cleanup;
5692 tree compound_stmt;
5693 tree args;
5694 tree fcall;
5695
5696 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5697 return void_zero_node;
5698
5699 /* Call build_cleanup before we enter the anonymous function so that
5700 any access checks will be done relative to the current scope,
5701 rather than the scope of the anonymous function. */
5702 build_cleanup (decl);
5703
5704 /* Now start the function. */
5705 cleanup = start_cleanup_fn ();
5706
5707 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer
5708 to the original function, rather than the anonymous one. That
5709 will make the back-end think that nested functions are in use,
5710 which causes confusion. */
5711
5712 push_deferring_access_checks (dk_no_check);
5713 fcall = build_cleanup (decl);
5714 pop_deferring_access_checks ();
5715
5716 /* Create the body of the anonymous function. */
5717 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
5718 finish_expr_stmt (fcall);
5719 finish_compound_stmt (compound_stmt);
5720 end_cleanup_fn ();
5721
5722 /* Call atexit with the cleanup function. */
5723 cxx_mark_addressable (cleanup);
5724 mark_used (cleanup);
5725 cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
5726 if (flag_use_cxa_atexit)
5727 {
5728 args = tree_cons (NULL_TREE,
5729 build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
5730 NULL_TREE);
5731 if (targetm.cxx.use_aeabi_atexit ())
5732 {
5733 args = tree_cons (NULL_TREE, cleanup, args);
5734 args = tree_cons (NULL_TREE, null_pointer_node, args);
5735 }
5736 else
5737 {
5738 args = tree_cons (NULL_TREE, null_pointer_node, args);
5739 args = tree_cons (NULL_TREE, cleanup, args);
5740 }
5741 }
5742 else
5743 args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
5744 return build_function_call (get_atexit_node (), args);
5745 }
5746
5747 /* DECL is a VAR_DECL with static storage duration. INIT, if present,
5748 is its initializer. Generate code to handle the construction
5749 and destruction of DECL. */
5750
5751 static void
expand_static_init(tree decl,tree init)5752 expand_static_init (tree decl, tree init)
5753 {
5754 gcc_assert (TREE_CODE (decl) == VAR_DECL);
5755 gcc_assert (TREE_STATIC (decl));
5756
5757 /* Some variables require no initialization. */
5758 if (!init
5759 && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
5760 && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5761 return;
5762
5763 if (DECL_FUNCTION_SCOPE_P (decl))
5764 {
5765 /* Emit code to perform this initialization but once. */
5766 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE;
5767 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE;
5768 tree guard, guard_addr, guard_addr_list;
5769 tree acquire_fn, release_fn, abort_fn;
5770 tree flag, begin;
5771
5772 /* Emit code to perform this initialization but once. This code
5773 looks like:
5774
5775 static <type> guard;
5776 if (!guard.first_byte) {
5777 if (__cxa_guard_acquire (&guard)) {
5778 bool flag = false;
5779 try {
5780 // Do initialization.
5781 flag = true; __cxa_guard_release (&guard);
5782 // Register variable for destruction at end of program.
5783 } catch {
5784 if (!flag) __cxa_guard_abort (&guard);
5785 }
5786 }
5787
5788 Note that the `flag' variable is only set to 1 *after* the
5789 initialization is complete. This ensures that an exception,
5790 thrown during the construction, will cause the variable to
5791 reinitialized when we pass through this code again, as per:
5792
5793 [stmt.dcl]
5794
5795 If the initialization exits by throwing an exception, the
5796 initialization is not complete, so it will be tried again
5797 the next time control enters the declaration.
5798
5799 This process should be thread-safe, too; multiple threads
5800 should not be able to initialize the variable more than
5801 once. */
5802
5803 /* Create the guard variable. */
5804 guard = get_guard (decl);
5805
5806 /* This optimization isn't safe on targets with relaxed memory
5807 consistency. On such targets we force synchronization in
5808 __cxa_guard_acquire. */
5809 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5810 {
5811 /* Begin the conditional initialization. */
5812 if_stmt = begin_if_stmt ();
5813 finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
5814 then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5815 }
5816
5817 if (flag_threadsafe_statics)
5818 {
5819 guard_addr = build_address (guard);
5820 guard_addr_list = build_tree_list (NULL_TREE, guard_addr);
5821
5822 acquire_fn = get_identifier ("__cxa_guard_acquire");
5823 release_fn = get_identifier ("__cxa_guard_release");
5824 abort_fn = get_identifier ("__cxa_guard_abort");
5825 if (!get_global_value_if_present (acquire_fn, &acquire_fn))
5826 {
5827 tree argtypes = tree_cons (NULL_TREE, TREE_TYPE (guard_addr),
5828 void_list_node);
5829 tree vfntype = build_function_type (void_type_node, argtypes);
5830 acquire_fn = push_library_fn
5831 (acquire_fn, build_function_type (integer_type_node, argtypes));
5832 release_fn = push_library_fn (release_fn, vfntype);
5833 abort_fn = push_library_fn (abort_fn, vfntype);
5834 }
5835 else
5836 {
5837 release_fn = identifier_global_value (release_fn);
5838 abort_fn = identifier_global_value (abort_fn);
5839 }
5840
5841 inner_if_stmt = begin_if_stmt ();
5842 finish_if_stmt_cond (build_call (acquire_fn, guard_addr_list),
5843 inner_if_stmt);
5844
5845 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE);
5846 begin = get_target_expr (boolean_false_node);
5847 flag = TARGET_EXPR_SLOT (begin);
5848
5849 TARGET_EXPR_CLEANUP (begin)
5850 = build3 (COND_EXPR, void_type_node, flag,
5851 void_zero_node,
5852 build_call (abort_fn, guard_addr_list));
5853 CLEANUP_EH_ONLY (begin) = 1;
5854
5855 /* Do the initialization itself. */
5856 init = add_stmt_to_compound (begin, init);
5857 init = add_stmt_to_compound
5858 (init, build2 (MODIFY_EXPR, void_type_node, flag, boolean_true_node));
5859 init = add_stmt_to_compound
5860 (init, build_call (release_fn, guard_addr_list));
5861 }
5862 else
5863 init = add_stmt_to_compound (init, set_guard (guard));
5864
5865 /* Use atexit to register a function for destroying this static
5866 variable. */
5867 init = add_stmt_to_compound (init, register_dtor_fn (decl));
5868
5869 finish_expr_stmt (init);
5870
5871 if (flag_threadsafe_statics)
5872 {
5873 finish_compound_stmt (inner_then_clause);
5874 finish_then_clause (inner_if_stmt);
5875 finish_if_stmt (inner_if_stmt);
5876 }
5877
5878 if (!targetm.relaxed_ordering || !flag_threadsafe_statics)
5879 {
5880 finish_compound_stmt (then_clause);
5881 finish_then_clause (if_stmt);
5882 finish_if_stmt (if_stmt);
5883 }
5884 }
5885 else
5886 static_aggregates = tree_cons (init, decl, static_aggregates);
5887 }
5888
5889
5890 /* Make TYPE a complete type based on INITIAL_VALUE.
5891 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
5892 2 if there was no information (in which case assume 0 if DO_DEFAULT),
5893 3 if the initializer list is empty (in pedantic mode). */
5894
5895 int
cp_complete_array_type(tree * ptype,tree initial_value,bool do_default)5896 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default)
5897 {
5898 int failure;
5899 tree type, elt_type;
5900
5901 if (initial_value)
5902 {
5903 /* An array of character type can be initialized from a
5904 brace-enclosed string constant.
5905
5906 FIXME: this code is duplicated from reshape_init. Probably
5907 we should just call reshape_init here? */
5908 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
5909 && TREE_CODE (initial_value) == CONSTRUCTOR
5910 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (initial_value)))
5911 {
5912 VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (initial_value);
5913 tree value = VEC_index (constructor_elt, v, 0)->value;
5914
5915 if (TREE_CODE (value) == STRING_CST
5916 && VEC_length (constructor_elt, v) == 1)
5917 initial_value = value;
5918 }
5919 }
5920
5921 failure = complete_array_type (ptype, initial_value, do_default);
5922
5923 /* We can create the array before the element type is complete, which
5924 means that we didn't have these two bits set in the original type
5925 either. In completing the type, we are expected to propagate these
5926 bits. See also complete_type which does the same thing for arrays
5927 of fixed size. */
5928 type = *ptype;
5929 if (TYPE_DOMAIN (type))
5930 {
5931 elt_type = TREE_TYPE (type);
5932 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type);
5933 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
5934 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type);
5935 }
5936
5937 return failure;
5938 }
5939
5940 /* Return zero if something is declared to be a member of type
5941 CTYPE when in the context of CUR_TYPE. STRING is the error
5942 message to print in that case. Otherwise, quietly return 1. */
5943
5944 static int
member_function_or_else(tree ctype,tree cur_type,enum overload_flags flags)5945 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
5946 {
5947 if (ctype && ctype != cur_type)
5948 {
5949 if (flags == DTOR_FLAG)
5950 error ("destructor for alien class %qT cannot be a member", ctype);
5951 else
5952 error ("constructor for alien class %qT cannot be a member", ctype);
5953 return 0;
5954 }
5955 return 1;
5956 }
5957
5958 /* Subroutine of `grokdeclarator'. */
5959
5960 /* Generate errors possibly applicable for a given set of specifiers.
5961 This is for ARM $7.1.2. */
5962
5963 static void
bad_specifiers(tree object,const char * type,int virtualp,int quals,int inlinep,int friendp,int raises)5964 bad_specifiers (tree object,
5965 const char* type,
5966 int virtualp,
5967 int quals,
5968 int inlinep,
5969 int friendp,
5970 int raises)
5971 {
5972 if (virtualp)
5973 error ("%qD declared as a %<virtual%> %s", object, type);
5974 if (inlinep)
5975 error ("%qD declared as an %<inline%> %s", object, type);
5976 if (quals)
5977 error ("%<const%> and %<volatile%> function specifiers on "
5978 "%qD invalid in %s declaration",
5979 object, type);
5980 if (friendp)
5981 error ("%q+D declared as a friend", object);
5982 if (raises
5983 && (TREE_CODE (object) == TYPE_DECL
5984 || (!TYPE_PTRFN_P (TREE_TYPE (object))
5985 && !TYPE_REFFN_P (TREE_TYPE (object))
5986 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
5987 error ("%q+D declared with an exception specification", object);
5988 }
5989
5990 /* DECL is a member function or static data member and is presently
5991 being defined. Check that the definition is taking place in a
5992 valid namespace. */
5993
5994 static void
check_class_member_definition_namespace(tree decl)5995 check_class_member_definition_namespace (tree decl)
5996 {
5997 /* These checks only apply to member functions and static data
5998 members. */
5999 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6000 || TREE_CODE (decl) == VAR_DECL);
6001 /* We check for problems with specializations in pt.c in
6002 check_specialization_namespace, where we can issue better
6003 diagnostics. */
6004 if (processing_specialization)
6005 return;
6006 /* There are no restrictions on the placement of
6007 explicit instantiations. */
6008 if (processing_explicit_instantiation)
6009 return;
6010 /* [class.mfct]
6011
6012 A member function definition that appears outside of the
6013 class definition shall appear in a namespace scope enclosing
6014 the class definition.
6015
6016 [class.static.data]
6017
6018 The definition for a static data member shall appear in a
6019 namespace scope enclosing the member's class definition. */
6020 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl)))
6021 pedwarn ("definition of %qD is not in namespace enclosing %qT",
6022 decl, DECL_CONTEXT (decl));
6023 }
6024
6025 /* Build a PARM_DECL for the "this" parameter. TYPE is the
6026 METHOD_TYPE for a non-static member function; QUALS are the
6027 cv-qualifiers that apply to the function. */
6028
6029 tree
build_this_parm(tree type,cp_cv_quals quals)6030 build_this_parm (tree type, cp_cv_quals quals)
6031 {
6032 tree this_type;
6033 tree qual_type;
6034 tree parm;
6035 cp_cv_quals this_quals;
6036
6037 this_type = TREE_VALUE (TYPE_ARG_TYPES (type));
6038 /* The `this' parameter is implicitly `const'; it cannot be
6039 assigned to. */
6040 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
6041 qual_type = cp_build_qualified_type (this_type, this_quals);
6042 parm = build_artificial_parm (this_identifier, qual_type);
6043 cp_apply_type_quals_to_decl (this_quals, parm);
6044 return parm;
6045 }
6046
6047 /* CTYPE is class type, or null if non-class.
6048 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
6049 or METHOD_TYPE.
6050 DECLARATOR is the function's name.
6051 PARMS is a chain of PARM_DECLs for the function.
6052 VIRTUALP is truthvalue of whether the function is virtual or not.
6053 FLAGS are to be passed through to `grokclassfn'.
6054 QUALS are qualifiers indicating whether the function is `const'
6055 or `volatile'.
6056 RAISES is a list of exceptions that this function can raise.
6057 CHECK is 1 if we must find this method in CTYPE, 0 if we should
6058 not look, and -1 if we should not call `grokclassfn' at all.
6059
6060 SFK is the kind of special function (if any) for the new function.
6061
6062 Returns `NULL_TREE' if something goes wrong, after issuing
6063 applicable error messages. */
6064
6065 static tree
grokfndecl(tree ctype,tree type,tree declarator,tree parms,tree orig_declarator,int virtualp,enum overload_flags flags,cp_cv_quals quals,tree raises,int check,int friendp,int publicp,int inlinep,special_function_kind sfk,bool funcdef_flag,int template_count,tree in_namespace,tree * attrlist)6066 grokfndecl (tree ctype,
6067 tree type,
6068 tree declarator,
6069 tree parms,
6070 tree orig_declarator,
6071 int virtualp,
6072 enum overload_flags flags,
6073 cp_cv_quals quals,
6074 tree raises,
6075 int check,
6076 int friendp,
6077 int publicp,
6078 int inlinep,
6079 special_function_kind sfk,
6080 bool funcdef_flag,
6081 int template_count,
6082 tree in_namespace,
6083 tree* attrlist)
6084 {
6085 tree decl;
6086 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
6087 tree t;
6088
6089 if (raises)
6090 type = build_exception_variant (type, raises);
6091
6092 decl = build_lang_decl (FUNCTION_DECL, declarator, type);
6093 if (TREE_CODE (type) == METHOD_TYPE)
6094 {
6095 tree parm;
6096 parm = build_this_parm (type, quals);
6097 TREE_CHAIN (parm) = parms;
6098 parms = parm;
6099 }
6100 DECL_ARGUMENTS (decl) = parms;
6101 /* Propagate volatile out from type to decl. */
6102 if (TYPE_VOLATILE (type))
6103 TREE_THIS_VOLATILE (decl) = 1;
6104
6105 /* If pointers to member functions use the least significant bit to
6106 indicate whether a function is virtual, ensure a pointer
6107 to this function will have that bit clear. */
6108 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6109 && TREE_CODE (type) == METHOD_TYPE
6110 && DECL_ALIGN (decl) < 2 * BITS_PER_UNIT)
6111 DECL_ALIGN (decl) = 2 * BITS_PER_UNIT;
6112
6113 if (friendp
6114 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
6115 {
6116 if (funcdef_flag)
6117 error
6118 ("defining explicit specialization %qD in friend declaration",
6119 orig_declarator);
6120 else
6121 {
6122 tree fns = TREE_OPERAND (orig_declarator, 0);
6123 tree args = TREE_OPERAND (orig_declarator, 1);
6124
6125 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
6126 {
6127 /* Something like `template <class T> friend void f<T>()'. */
6128 error ("invalid use of template-id %qD in declaration "
6129 "of primary template",
6130 orig_declarator);
6131 return NULL_TREE;
6132 }
6133
6134
6135 /* A friend declaration of the form friend void f<>(). Record
6136 the information in the TEMPLATE_ID_EXPR. */
6137 SET_DECL_IMPLICIT_INSTANTIATION (decl);
6138
6139 if (TREE_CODE (fns) == COMPONENT_REF)
6140 {
6141 /* Due to bison parser ickiness, we will have already looked
6142 up an operator_name or PFUNCNAME within the current class
6143 (see template_id in parse.y). If the current class contains
6144 such a name, we'll get a COMPONENT_REF here. Undo that. */
6145
6146 gcc_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
6147 == current_class_type);
6148 fns = TREE_OPERAND (fns, 1);
6149 }
6150 gcc_assert (TREE_CODE (fns) == IDENTIFIER_NODE
6151 || TREE_CODE (fns) == OVERLOAD);
6152 DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
6153
6154 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
6155 if (TREE_PURPOSE (t)
6156 && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
6157 {
6158 error ("default arguments are not allowed in declaration "
6159 "of friend template specialization %qD",
6160 decl);
6161 return NULL_TREE;
6162 }
6163
6164 if (inlinep)
6165 {
6166 error ("%<inline%> is not allowed in declaration of friend "
6167 "template specialization %qD",
6168 decl);
6169 return NULL_TREE;
6170 }
6171 }
6172 }
6173
6174 /* If this decl has namespace scope, set that up. */
6175 if (in_namespace)
6176 set_decl_namespace (decl, in_namespace, friendp);
6177 else if (!ctype)
6178 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
6179
6180 /* `main' and builtins have implicit 'C' linkage. */
6181 if ((MAIN_NAME_P (declarator)
6182 || (IDENTIFIER_LENGTH (declarator) > 10
6183 && IDENTIFIER_POINTER (declarator)[0] == '_'
6184 && IDENTIFIER_POINTER (declarator)[1] == '_'
6185 && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
6186 && current_lang_name == lang_name_cplusplus
6187 && ctype == NULL_TREE
6188 /* NULL_TREE means global namespace. */
6189 && DECL_CONTEXT (decl) == NULL_TREE)
6190 SET_DECL_LANGUAGE (decl, lang_c);
6191
6192 /* Should probably propagate const out from type to decl I bet (mrs). */
6193 if (staticp)
6194 {
6195 DECL_STATIC_FUNCTION_P (decl) = 1;
6196 DECL_CONTEXT (decl) = ctype;
6197 }
6198
6199 if (ctype)
6200 {
6201 DECL_CONTEXT (decl) = ctype;
6202 if (funcdef_flag)
6203 check_class_member_definition_namespace (decl);
6204 }
6205
6206 if (ctype == NULL_TREE && DECL_MAIN_P (decl))
6207 {
6208 if (processing_template_decl)
6209 error ("cannot declare %<::main%> to be a template");
6210 if (inlinep)
6211 error ("cannot declare %<::main%> to be inline");
6212 if (!publicp)
6213 error ("cannot declare %<::main%> to be static");
6214 inlinep = 0;
6215 publicp = 1;
6216 }
6217
6218 /* Members of anonymous types and local classes have no linkage; make
6219 them internal. If a typedef is made later, this will be changed. */
6220 if (ctype && (TYPE_ANONYMOUS_P (ctype)
6221 || decl_function_context (TYPE_MAIN_DECL (ctype))))
6222 publicp = 0;
6223
6224 if (publicp)
6225 {
6226 /* [basic.link]: A name with no linkage (notably, the name of a class
6227 or enumeration declared in a local scope) shall not be used to
6228 declare an entity with linkage.
6229
6230 Only check this for public decls for now. See core 319, 389. */
6231 t = no_linkage_check (TREE_TYPE (decl),
6232 /*relaxed_p=*/false);
6233 if (t)
6234 {
6235 if (TYPE_ANONYMOUS_P (t))
6236 {
6237 if (DECL_EXTERN_C_P (decl))
6238 /* Allow this; it's pretty common in C. */;
6239 else
6240 {
6241 pedwarn ("non-local function %q#D uses anonymous type",
6242 decl);
6243 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6244 pedwarn ("%q+#D does not refer to the unqualified "
6245 "type, so it is not used for linkage",
6246 TYPE_NAME (t));
6247 }
6248 }
6249 else
6250 pedwarn ("non-local function %q#D uses local type %qT", decl, t);
6251 }
6252 }
6253
6254 TREE_PUBLIC (decl) = publicp;
6255 if (! publicp)
6256 {
6257 DECL_INTERFACE_KNOWN (decl) = 1;
6258 DECL_NOT_REALLY_EXTERN (decl) = 1;
6259 }
6260
6261 /* If the declaration was declared inline, mark it as such. */
6262 if (inlinep)
6263 DECL_DECLARED_INLINE_P (decl) = 1;
6264 /* We inline functions that are explicitly declared inline, or, when
6265 the user explicitly asks us to, all functions. */
6266 if (DECL_DECLARED_INLINE_P (decl)
6267 || (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag))
6268 DECL_INLINE (decl) = 1;
6269
6270 DECL_EXTERNAL (decl) = 1;
6271 if (quals && TREE_CODE (type) == FUNCTION_TYPE)
6272 {
6273 error ("%smember function %qD cannot have cv-qualifier",
6274 (ctype ? "static " : "non-"), decl);
6275 quals = TYPE_UNQUALIFIED;
6276 }
6277
6278 if (IDENTIFIER_OPNAME_P (DECL_NAME (decl))
6279 && !grok_op_properties (decl, /*complain=*/true))
6280 return NULL_TREE;
6281
6282 if (ctype && decl_function_context (decl))
6283 DECL_NO_STATIC_CHAIN (decl) = 1;
6284
6285 if (funcdef_flag)
6286 /* Make the init_value nonzero so pushdecl knows this is not
6287 tentative. error_mark_node is replaced later with the BLOCK. */
6288 DECL_INITIAL (decl) = error_mark_node;
6289
6290 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
6291 TREE_NOTHROW (decl) = 1;
6292
6293 /* Caller will do the rest of this. */
6294 if (check < 0)
6295 return decl;
6296
6297 if (ctype != NULL_TREE)
6298 {
6299 if (sfk == sfk_constructor)
6300 DECL_CONSTRUCTOR_P (decl) = 1;
6301
6302 grokclassfn (ctype, decl, flags);
6303 }
6304
6305 decl = check_explicit_specialization (orig_declarator, decl,
6306 template_count,
6307 2 * funcdef_flag +
6308 4 * (friendp != 0));
6309 if (decl == error_mark_node)
6310 return NULL_TREE;
6311
6312 if (attrlist)
6313 {
6314 cplus_decl_attributes (&decl, *attrlist, 0);
6315 *attrlist = NULL_TREE;
6316 }
6317
6318 /* Check main's type after attributes have been applied. */
6319 if (ctype == NULL_TREE && DECL_MAIN_P (decl)
6320 && !same_type_p (TREE_TYPE (TREE_TYPE (decl)),
6321 integer_type_node))
6322 {
6323 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
6324 tree newtype;
6325 error ("%<::main%> must return %<int%>");
6326 newtype = build_function_type (integer_type_node, oldtypeargs);
6327 TREE_TYPE (decl) = newtype;
6328 }
6329
6330 if (ctype != NULL_TREE
6331 && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
6332 && check)
6333 {
6334 tree old_decl;
6335
6336 old_decl = check_classfn (ctype, decl,
6337 (processing_template_decl
6338 > template_class_depth (ctype))
6339 ? current_template_parms
6340 : NULL_TREE);
6341 if (old_decl)
6342 {
6343 tree ok;
6344 tree pushed_scope;
6345
6346 if (TREE_CODE (old_decl) == TEMPLATE_DECL)
6347 /* Because grokfndecl is always supposed to return a
6348 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
6349 here. We depend on our callers to figure out that its
6350 really a template that's being returned. */
6351 old_decl = DECL_TEMPLATE_RESULT (old_decl);
6352
6353 if (DECL_STATIC_FUNCTION_P (old_decl)
6354 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
6355 /* Remove the `this' parm added by grokclassfn.
6356 XXX Isn't this done in start_function, too? */
6357 revert_static_member_fn (decl);
6358 if (DECL_ARTIFICIAL (old_decl))
6359 error ("definition of implicitly-declared %qD", old_decl);
6360
6361 /* Since we've smashed OLD_DECL to its
6362 DECL_TEMPLATE_RESULT, we must do the same to DECL. */
6363 if (TREE_CODE (decl) == TEMPLATE_DECL)
6364 decl = DECL_TEMPLATE_RESULT (decl);
6365
6366 /* Attempt to merge the declarations. This can fail, in
6367 the case of some invalid specialization declarations. */
6368 pushed_scope = push_scope (ctype);
6369 ok = duplicate_decls (decl, old_decl, friendp);
6370 if (pushed_scope)
6371 pop_scope (pushed_scope);
6372 if (!ok)
6373 {
6374 error ("no %q#D member function declared in class %qT",
6375 decl, ctype);
6376 return NULL_TREE;
6377 }
6378 return old_decl;
6379 }
6380 }
6381
6382 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
6383 return NULL_TREE;
6384
6385 if (ctype == NULL_TREE || check)
6386 return decl;
6387
6388 if (virtualp)
6389 DECL_VIRTUAL_P (decl) = 1;
6390
6391 return decl;
6392 }
6393
6394 /* DECL is a VAR_DECL for a static data member. Set flags to reflect
6395 the linkage that DECL will receive in the object file. */
6396
6397 static void
set_linkage_for_static_data_member(tree decl)6398 set_linkage_for_static_data_member (tree decl)
6399 {
6400 /* A static data member always has static storage duration and
6401 external linkage. Note that static data members are forbidden in
6402 local classes -- the only situation in which a class has
6403 non-external linkage. */
6404 TREE_PUBLIC (decl) = 1;
6405 TREE_STATIC (decl) = 1;
6406 /* For non-template classes, static data members are always put
6407 out in exactly those files where they are defined, just as
6408 with ordinary namespace-scope variables. */
6409 if (!processing_template_decl)
6410 DECL_INTERFACE_KNOWN (decl) = 1;
6411 }
6412
6413 /* Create a VAR_DECL named NAME with the indicated TYPE.
6414
6415 If SCOPE is non-NULL, it is the class type or namespace containing
6416 the variable. If SCOPE is NULL, the variable should is created in
6417 the innermost enclosings scope. */
6418
6419 static tree
grokvardecl(tree type,tree name,const cp_decl_specifier_seq * declspecs,int initialized,int constp,tree scope)6420 grokvardecl (tree type,
6421 tree name,
6422 const cp_decl_specifier_seq *declspecs,
6423 int initialized,
6424 int constp,
6425 tree scope)
6426 {
6427 tree decl;
6428 tree explicit_scope;
6429
6430 gcc_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE);
6431
6432 /* Compute the scope in which to place the variable, but remember
6433 whether or not that scope was explicitly specified by the user. */
6434 explicit_scope = scope;
6435 if (!scope)
6436 {
6437 /* An explicit "extern" specifier indicates a namespace-scope
6438 variable. */
6439 if (declspecs->storage_class == sc_extern)
6440 scope = current_namespace;
6441 else if (!at_function_scope_p ())
6442 scope = current_scope ();
6443 }
6444
6445 if (scope
6446 && (/* If the variable is a namespace-scope variable declared in a
6447 template, we need DECL_LANG_SPECIFIC. */
6448 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
6449 /* Similarly for namespace-scope variables with language linkage
6450 other than C++. */
6451 || (TREE_CODE (scope) == NAMESPACE_DECL
6452 && current_lang_name != lang_name_cplusplus)
6453 /* Similarly for static data members. */
6454 || TYPE_P (scope)))
6455 decl = build_lang_decl (VAR_DECL, name, type);
6456 else
6457 decl = build_decl (VAR_DECL, name, type);
6458
6459 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL)
6460 set_decl_namespace (decl, explicit_scope, 0);
6461 else
6462 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
6463
6464 if (declspecs->storage_class == sc_extern)
6465 {
6466 DECL_THIS_EXTERN (decl) = 1;
6467 DECL_EXTERNAL (decl) = !initialized;
6468 }
6469
6470 if (DECL_CLASS_SCOPE_P (decl))
6471 {
6472 set_linkage_for_static_data_member (decl);
6473 /* This function is only called with out-of-class definitions. */
6474 DECL_EXTERNAL (decl) = 0;
6475 check_class_member_definition_namespace (decl);
6476 }
6477 /* At top level, either `static' or no s.c. makes a definition
6478 (perhaps tentative), and absence of `static' makes it public. */
6479 else if (toplevel_bindings_p ())
6480 {
6481 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static
6482 && (DECL_THIS_EXTERN (decl) || ! constp));
6483 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
6484 }
6485 /* Not at top level, only `static' makes a static definition. */
6486 else
6487 {
6488 TREE_STATIC (decl) = declspecs->storage_class == sc_static;
6489 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
6490 }
6491
6492 if (declspecs->specs[(int)ds_thread])
6493 {
6494 if (targetm.have_tls)
6495 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6496 else
6497 /* A mere warning is sure to result in improper semantics
6498 at runtime. Don't bother to allow this to compile. */
6499 error ("thread-local storage not supported for this target");
6500 }
6501
6502 if (TREE_PUBLIC (decl))
6503 {
6504 /* [basic.link]: A name with no linkage (notably, the name of a class
6505 or enumeration declared in a local scope) shall not be used to
6506 declare an entity with linkage.
6507
6508 Only check this for public decls for now. */
6509 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false);
6510 if (t)
6511 {
6512 if (TYPE_ANONYMOUS_P (t))
6513 {
6514 if (DECL_EXTERN_C_P (decl))
6515 /* Allow this; it's pretty common in C. */
6516 ;
6517 else
6518 {
6519 /* DRs 132, 319 and 389 seem to indicate types with
6520 no linkage can only be used to declare extern "C"
6521 entities. Since it's not always an error in the
6522 ISO C++ 90 Standard, we only issue a warning. */
6523 warning (0, "non-local variable %q#D uses anonymous type",
6524 decl);
6525 if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
6526 warning (0, "%q+#D does not refer to the unqualified "
6527 "type, so it is not used for linkage",
6528 TYPE_NAME (t));
6529 }
6530 }
6531 else
6532 warning (0, "non-local variable %q#D uses local type %qT", decl, t);
6533 }
6534 }
6535 else
6536 DECL_INTERFACE_KNOWN (decl) = 1;
6537
6538 return decl;
6539 }
6540
6541 /* Create and return a canonical pointer to member function type, for
6542 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */
6543
6544 tree
build_ptrmemfunc_type(tree type)6545 build_ptrmemfunc_type (tree type)
6546 {
6547 tree field, fields;
6548 tree t;
6549 tree unqualified_variant = NULL_TREE;
6550
6551 if (type == error_mark_node)
6552 return type;
6553
6554 /* If a canonical type already exists for this type, use it. We use
6555 this method instead of type_hash_canon, because it only does a
6556 simple equality check on the list of field members. */
6557
6558 if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
6559 return t;
6560
6561 /* Make sure that we always have the unqualified pointer-to-member
6562 type first. */
6563 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
6564 unqualified_variant
6565 = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
6566
6567 t = make_aggr_type (RECORD_TYPE);
6568 xref_basetypes (t, NULL_TREE);
6569
6570 /* Let the front-end know this is a pointer to member function... */
6571 TYPE_PTRMEMFUNC_FLAG (t) = 1;
6572 /* ... and not really an aggregate. */
6573 SET_IS_AGGR_TYPE (t, 0);
6574
6575 field = build_decl (FIELD_DECL, pfn_identifier, type);
6576 fields = field;
6577
6578 field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
6579 TREE_CHAIN (field) = fields;
6580 fields = field;
6581
6582 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
6583
6584 /* Zap out the name so that the back-end will give us the debugging
6585 information for this anonymous RECORD_TYPE. */
6586 TYPE_NAME (t) = NULL_TREE;
6587
6588 /* If this is not the unqualified form of this pointer-to-member
6589 type, set the TYPE_MAIN_VARIANT for this type to be the
6590 unqualified type. Since they are actually RECORD_TYPEs that are
6591 not variants of each other, we must do this manually. */
6592 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
6593 {
6594 t = build_qualified_type (t, cp_type_quals (type));
6595 TYPE_MAIN_VARIANT (t) = unqualified_variant;
6596 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
6597 TYPE_NEXT_VARIANT (unqualified_variant) = t;
6598 }
6599
6600 /* Cache this pointer-to-member type so that we can find it again
6601 later. */
6602 TYPE_SET_PTRMEMFUNC_TYPE (type, t);
6603
6604 return t;
6605 }
6606
6607 /* Create and return a pointer to data member type. */
6608
6609 tree
build_ptrmem_type(tree class_type,tree member_type)6610 build_ptrmem_type (tree class_type, tree member_type)
6611 {
6612 if (TREE_CODE (member_type) == METHOD_TYPE)
6613 {
6614 tree arg_types;
6615
6616 arg_types = TYPE_ARG_TYPES (member_type);
6617 class_type = (cp_build_qualified_type
6618 (class_type,
6619 cp_type_quals (TREE_TYPE (TREE_VALUE (arg_types)))));
6620 member_type
6621 = build_method_type_directly (class_type,
6622 TREE_TYPE (member_type),
6623 TREE_CHAIN (arg_types));
6624 return build_ptrmemfunc_type (build_pointer_type (member_type));
6625 }
6626 else
6627 {
6628 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE);
6629 return build_offset_type (class_type, member_type);
6630 }
6631 }
6632
6633 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
6634 Check to see that the definition is valid. Issue appropriate error
6635 messages. Return 1 if the definition is particularly bad, or 0
6636 otherwise. */
6637
6638 int
check_static_variable_definition(tree decl,tree type)6639 check_static_variable_definition (tree decl, tree type)
6640 {
6641 /* Motion 10 at San Diego: If a static const integral data member is
6642 initialized with an integral constant expression, the initializer
6643 may appear either in the declaration (within the class), or in
6644 the definition, but not both. If it appears in the class, the
6645 member is a member constant. The file-scope definition is always
6646 required. */
6647 if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
6648 {
6649 error ("invalid in-class initialization of static data member "
6650 "of non-integral type %qT",
6651 type);
6652 /* If we just return the declaration, crashes will sometimes
6653 occur. We therefore return void_type_node, as if this were a
6654 friend declaration, to cause callers to completely ignore
6655 this declaration. */
6656 return 1;
6657 }
6658 else if (!CP_TYPE_CONST_P (type))
6659 error ("ISO C++ forbids in-class initialization of non-const "
6660 "static member %qD",
6661 decl);
6662 else if (pedantic && !INTEGRAL_TYPE_P (type))
6663 pedwarn ("ISO C++ forbids initialization of member constant "
6664 "%qD of non-integral type %qT", decl, type);
6665
6666 return 0;
6667 }
6668
6669 /* Given the SIZE (i.e., number of elements) in an array, compute an
6670 appropriate index type for the array. If non-NULL, NAME is the
6671 name of the thing being declared. */
6672
6673 tree
compute_array_index_type(tree name,tree size)6674 compute_array_index_type (tree name, tree size)
6675 {
6676 tree type;
6677 tree itype;
6678
6679 if (error_operand_p (size))
6680 return error_mark_node;
6681
6682 type = TREE_TYPE (size);
6683 /* The array bound must be an integer type. */
6684 if (!dependent_type_p (type) && !INTEGRAL_TYPE_P (type))
6685 {
6686 if (name)
6687 error ("size of array %qD has non-integral type %qT", name, type);
6688 else
6689 error ("size of array has non-integral type %qT", type);
6690 size = integer_one_node;
6691 type = TREE_TYPE (size);
6692 }
6693
6694 if (abi_version_at_least (2)
6695 /* We should only handle value dependent expressions specially. */
6696 ? value_dependent_expression_p (size)
6697 /* But for abi-1, we handled all instances in templates. This
6698 effects the manglings produced. */
6699 : processing_template_decl)
6700 return build_index_type (build_min (MINUS_EXPR, sizetype,
6701 size, integer_one_node));
6702
6703 /* The size might be the result of a cast. */
6704 STRIP_TYPE_NOPS (size);
6705
6706 /* It might be a const variable or enumeration constant. */
6707 size = integral_constant_value (size);
6708
6709 /* Normally, the array-bound will be a constant. */
6710 if (TREE_CODE (size) == INTEGER_CST)
6711 {
6712 /* Check to see if the array bound overflowed. Make that an
6713 error, no matter how generous we're being. */
6714 int old_flag_pedantic_errors = flag_pedantic_errors;
6715 int old_pedantic = pedantic;
6716 pedantic = flag_pedantic_errors = 1;
6717 constant_expression_warning (size);
6718 pedantic = old_pedantic;
6719 flag_pedantic_errors = old_flag_pedantic_errors;
6720
6721 /* An array must have a positive number of elements. */
6722 if (INT_CST_LT (size, integer_zero_node))
6723 {
6724 if (name)
6725 error ("size of array %qD is negative", name);
6726 else
6727 error ("size of array is negative");
6728 size = integer_one_node;
6729 }
6730 /* As an extension we allow zero-sized arrays. We always allow
6731 them in system headers because glibc uses them. */
6732 else if (integer_zerop (size) && pedantic && !in_system_header)
6733 {
6734 if (name)
6735 pedwarn ("ISO C++ forbids zero-size array %qD", name);
6736 else
6737 pedwarn ("ISO C++ forbids zero-size array");
6738 }
6739 }
6740 else if (TREE_CONSTANT (size))
6741 {
6742 /* `(int) &fn' is not a valid array bound. */
6743 if (name)
6744 error ("size of array %qD is not an integral constant-expression",
6745 name);
6746 else
6747 error ("size of array is not an integral constant-expression");
6748 size = integer_one_node;
6749 }
6750 else if (pedantic && warn_vla != 0)
6751 {
6752 if (name)
6753 pedwarn ("ISO C++ forbids variable length array %qD", name);
6754 else
6755 pedwarn ("ISO C++ forbids variable length array");
6756 }
6757 else if (warn_vla > 0)
6758 {
6759 if (name)
6760 warning (OPT_Wvla,
6761 "variable length array %qD is used", name);
6762 else
6763 warning (OPT_Wvla,
6764 "variable length array is used");
6765 }
6766
6767 if (processing_template_decl && !TREE_CONSTANT (size))
6768 /* A variable sized array. */
6769 itype = build_min (MINUS_EXPR, sizetype, size, integer_one_node);
6770 else
6771 {
6772 HOST_WIDE_INT saved_processing_template_decl;
6773
6774 /* Compute the index of the largest element in the array. It is
6775 one less than the number of elements in the array. We save
6776 and restore PROCESSING_TEMPLATE_DECL so that computations in
6777 cp_build_binary_op will be appropriately folded. */
6778 saved_processing_template_decl = processing_template_decl;
6779 processing_template_decl = 0;
6780 itype = cp_build_binary_op (MINUS_EXPR,
6781 cp_convert (ssizetype, size),
6782 cp_convert (ssizetype, integer_one_node));
6783 itype = fold (itype);
6784 processing_template_decl = saved_processing_template_decl;
6785
6786 if (!TREE_CONSTANT (itype))
6787 /* A variable sized array. */
6788 itype = variable_size (itype);
6789 /* Make sure that there was no overflow when creating to a signed
6790 index type. (For example, on a 32-bit machine, an array with
6791 size 2^32 - 1 is too big.) */
6792 else if (TREE_CODE (itype) == INTEGER_CST
6793 && TREE_OVERFLOW (itype))
6794 {
6795 error ("overflow in array dimension");
6796 TREE_OVERFLOW (itype) = 0;
6797 }
6798 }
6799
6800 /* Create and return the appropriate index type. */
6801 return build_index_type (itype);
6802 }
6803
6804 /* Returns the scope (if any) in which the entity declared by
6805 DECLARATOR will be located. If the entity was declared with an
6806 unqualified name, NULL_TREE is returned. */
6807
6808 tree
get_scope_of_declarator(const cp_declarator * declarator)6809 get_scope_of_declarator (const cp_declarator *declarator)
6810 {
6811 while (declarator && declarator->kind != cdk_id)
6812 declarator = declarator->declarator;
6813
6814 /* If the declarator-id is a SCOPE_REF, the scope in which the
6815 declaration occurs is the first operand. */
6816 if (declarator
6817 && declarator->u.id.qualifying_scope)
6818 return declarator->u.id.qualifying_scope;
6819
6820 /* Otherwise, the declarator is not a qualified name; the entity will
6821 be declared in the current scope. */
6822 return NULL_TREE;
6823 }
6824
6825 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
6826 indicated TYPE. If non-NULL, NAME is the NAME of the declaration
6827 with this type. */
6828
6829 static tree
create_array_type_for_decl(tree name,tree type,tree size)6830 create_array_type_for_decl (tree name, tree type, tree size)
6831 {
6832 tree itype = NULL_TREE;
6833 const char* error_msg;
6834
6835 /* If things have already gone awry, bail now. */
6836 if (type == error_mark_node || size == error_mark_node)
6837 return error_mark_node;
6838
6839 /* Assume that everything will go OK. */
6840 error_msg = NULL;
6841
6842 /* There are some types which cannot be array elements. */
6843 switch (TREE_CODE (type))
6844 {
6845 case VOID_TYPE:
6846 error_msg = "array of void";
6847 break;
6848
6849 case FUNCTION_TYPE:
6850 error_msg = "array of functions";
6851 break;
6852
6853 case REFERENCE_TYPE:
6854 error_msg = "array of references";
6855 break;
6856
6857 case METHOD_TYPE:
6858 error_msg = "array of function members";
6859 break;
6860
6861 default:
6862 break;
6863 }
6864
6865 /* If something went wrong, issue an error-message and return. */
6866 if (error_msg)
6867 {
6868 if (name)
6869 error ("declaration of %qD as %s", name, error_msg);
6870 else
6871 error ("creating %s", error_msg);
6872
6873 return error_mark_node;
6874 }
6875
6876 /* [dcl.array]
6877
6878 The constant expressions that specify the bounds of the arrays
6879 can be omitted only for the first member of the sequence. */
6880 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
6881 {
6882 if (name)
6883 error ("declaration of %qD as multidimensional array must "
6884 "have bounds for all dimensions except the first",
6885 name);
6886 else
6887 error ("multidimensional array must have bounds for all "
6888 "dimensions except the first");
6889
6890 return error_mark_node;
6891 }
6892
6893 /* Figure out the index type for the array. */
6894 if (size)
6895 itype = compute_array_index_type (name, size);
6896
6897 /* [dcl.array]
6898 T is called the array element type; this type shall not be [...] an
6899 abstract class type. */
6900 abstract_virtuals_error (name, type);
6901
6902 return build_cplus_array_type (type, itype);
6903 }
6904
6905 /* Check that it's OK to declare a function with the indicated TYPE.
6906 SFK indicates the kind of special function (if any) that this
6907 function is. OPTYPE is the type given in a conversion operator
6908 declaration, or the class type for a constructor/destructor.
6909 Returns the actual return type of the function; that
6910 may be different than TYPE if an error occurs, or for certain
6911 special functions. */
6912
6913 static tree
check_special_function_return_type(special_function_kind sfk,tree type,tree optype)6914 check_special_function_return_type (special_function_kind sfk,
6915 tree type,
6916 tree optype)
6917 {
6918 switch (sfk)
6919 {
6920 case sfk_constructor:
6921 if (type)
6922 error ("return type specification for constructor invalid");
6923
6924 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6925 type = build_pointer_type (optype);
6926 else
6927 type = void_type_node;
6928 break;
6929
6930 case sfk_destructor:
6931 if (type)
6932 error ("return type specification for destructor invalid");
6933 /* We can't use the proper return type here because we run into
6934 problems with ambiguous bases and covariant returns.
6935 Java classes are left unchanged because (void *) isn't a valid
6936 Java type, and we don't want to change the Java ABI. */
6937 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (optype))
6938 type = build_pointer_type (void_type_node);
6939 else
6940 type = void_type_node;
6941 break;
6942
6943 case sfk_conversion:
6944 if (type && !same_type_p (type, optype))
6945 error ("operator %qT declared to return %qT", optype, type);
6946 else if (type)
6947 pedwarn ("return type specified for %<operator %T%>", optype);
6948 type = optype;
6949 break;
6950
6951 default:
6952 gcc_unreachable ();
6953 }
6954
6955 return type;
6956 }
6957
6958 /* A variable or data member (whose unqualified name is IDENTIFIER)
6959 has been declared with the indicated TYPE. If the TYPE is not
6960 acceptable, issue an error message and return a type to use for
6961 error-recovery purposes. */
6962
6963 tree
check_var_type(tree identifier,tree type)6964 check_var_type (tree identifier, tree type)
6965 {
6966 if (VOID_TYPE_P (type))
6967 {
6968 if (!identifier)
6969 error ("unnamed variable or field declared void");
6970 else if (TREE_CODE (identifier) == IDENTIFIER_NODE)
6971 {
6972 gcc_assert (!IDENTIFIER_OPNAME_P (identifier));
6973 error ("variable or field %qE declared void", identifier);
6974 }
6975 else
6976 error ("variable or field declared void");
6977 type = error_mark_node;
6978 }
6979
6980 return type;
6981 }
6982
6983 /* Given declspecs and a declarator (abstract or otherwise), determine
6984 the name and type of the object declared and construct a DECL node
6985 for it.
6986
6987 DECLSPECS is a chain of tree_list nodes whose value fields
6988 are the storage classes and type specifiers.
6989
6990 DECL_CONTEXT says which syntactic context this declaration is in:
6991 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
6992 FUNCDEF for a function definition. Like NORMAL but a few different
6993 error messages in each case. Return value may be zero meaning
6994 this definition is too screwy to try to parse.
6995 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to
6996 handle member functions (which have FIELD context).
6997 Return value may be zero meaning this definition is too screwy to
6998 try to parse.
6999 PARM for a parameter declaration (either within a function prototype
7000 or before a function body). Make a PARM_DECL, or return void_type_node.
7001 CATCHPARM for a parameter declaration before a catch clause.
7002 TYPENAME if for a typename (in a cast or sizeof).
7003 Don't make a DECL node; just return the ..._TYPE node.
7004 FIELD for a struct or union field; make a FIELD_DECL.
7005 BITFIELD for a field with specified width.
7006 INITIALIZED is 1 if the decl has an initializer.
7007
7008 ATTRLIST is a pointer to the list of attributes, which may be NULL
7009 if there are none; *ATTRLIST may be modified if attributes from inside
7010 the declarator should be applied to the declaration.
7011
7012 When this function is called, scoping variables (such as
7013 CURRENT_CLASS_TYPE) should reflect the scope in which the
7014 declaration occurs, not the scope in which the new declaration will
7015 be placed. For example, on:
7016
7017 void S::f() { ... }
7018
7019 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
7020 should not be `S'.
7021
7022 Returns a DECL (if a declarator is present), a TYPE (if there is no
7023 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an
7024 error occurs. */
7025
7026 tree
grokdeclarator(const cp_declarator * declarator,const cp_decl_specifier_seq * declspecs,enum decl_context decl_context,int initialized,tree * attrlist)7027 grokdeclarator (const cp_declarator *declarator,
7028 const cp_decl_specifier_seq *declspecs,
7029 enum decl_context decl_context,
7030 int initialized,
7031 tree* attrlist)
7032 {
7033 tree type = NULL_TREE;
7034 int longlong = 0;
7035 int virtualp, explicitp, friendp, inlinep, staticp;
7036 int explicit_int = 0;
7037 int explicit_char = 0;
7038 int defaulted_int = 0;
7039 tree dependent_name = NULL_TREE;
7040
7041 tree typedef_decl = NULL_TREE;
7042 const char *name = NULL;
7043 tree typedef_type = NULL_TREE;
7044 /* True if this declarator is a function definition. */
7045 bool funcdef_flag = false;
7046 cp_declarator_kind innermost_code = cdk_error;
7047 int bitfield = 0;
7048 #if 0
7049 /* See the code below that used this. */
7050 tree decl_attr = NULL_TREE;
7051 #endif
7052
7053 /* Keep track of what sort of function is being processed
7054 so that we can warn about default return values, or explicit
7055 return values which do not match prescribed defaults. */
7056 special_function_kind sfk = sfk_none;
7057
7058 tree dname = NULL_TREE;
7059 tree ctor_return_type = NULL_TREE;
7060 enum overload_flags flags = NO_SPECIAL;
7061 /* cv-qualifiers that apply to the declarator, for a declaration of
7062 a member function. */
7063 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED;
7064 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */
7065 int type_quals;
7066 tree raises = NULL_TREE;
7067 int template_count = 0;
7068 tree returned_attrs = NULL_TREE;
7069 tree parms = NULL_TREE;
7070 const cp_declarator *id_declarator;
7071 /* The unqualified name of the declarator; either an
7072 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */
7073 tree unqualified_id;
7074 /* The class type, if any, in which this entity is located,
7075 or NULL_TREE if none. Note that this value may be different from
7076 the current class type; for example if an attempt is made to declare
7077 "A::f" inside "B", this value will be "A". */
7078 tree ctype = current_class_type;
7079 /* The NAMESPACE_DECL for the namespace in which this entity is
7080 located. If an unqualified name is used to declare the entity,
7081 this value will be NULL_TREE, even if the entity is located at
7082 namespace scope. */
7083 tree in_namespace = NULL_TREE;
7084 cp_storage_class storage_class;
7085 bool unsigned_p, signed_p, short_p, long_p, thread_p;
7086 bool type_was_error_mark_node = false;
7087
7088 signed_p = declspecs->specs[(int)ds_signed];
7089 unsigned_p = declspecs->specs[(int)ds_unsigned];
7090 short_p = declspecs->specs[(int)ds_short];
7091 long_p = declspecs->specs[(int)ds_long];
7092 longlong = declspecs->specs[(int)ds_long] >= 2;
7093 thread_p = declspecs->specs[(int)ds_thread];
7094
7095 if (decl_context == FUNCDEF)
7096 funcdef_flag = true, decl_context = NORMAL;
7097 else if (decl_context == MEMFUNCDEF)
7098 funcdef_flag = true, decl_context = FIELD;
7099 else if (decl_context == BITFIELD)
7100 bitfield = 1, decl_context = FIELD;
7101
7102 /* Look inside a declarator for the name being declared
7103 and get it as a string, for an error message. */
7104 for (id_declarator = declarator;
7105 id_declarator;
7106 id_declarator = id_declarator->declarator)
7107 {
7108 if (id_declarator->kind != cdk_id)
7109 innermost_code = id_declarator->kind;
7110
7111 switch (id_declarator->kind)
7112 {
7113 case cdk_function:
7114 if (id_declarator->declarator
7115 && id_declarator->declarator->kind == cdk_id)
7116 {
7117 sfk = id_declarator->declarator->u.id.sfk;
7118 if (sfk == sfk_destructor)
7119 flags = DTOR_FLAG;
7120 }
7121 break;
7122
7123 case cdk_id:
7124 {
7125 tree qualifying_scope = id_declarator->u.id.qualifying_scope;
7126 tree decl = id_declarator->u.id.unqualified_name;
7127 if (!decl)
7128 break;
7129 if (qualifying_scope)
7130 {
7131 if (at_function_scope_p ())
7132 {
7133 /* [dcl.meaning]
7134
7135 A declarator-id shall not be qualified except
7136 for ...
7137
7138 None of the cases are permitted in block
7139 scope. */
7140 if (qualifying_scope == global_namespace)
7141 error ("invalid use of qualified-name %<::%D%>",
7142 decl);
7143 else if (TYPE_P (qualifying_scope))
7144 error ("invalid use of qualified-name %<%T::%D%>",
7145 qualifying_scope, decl);
7146 else
7147 error ("invalid use of qualified-name %<%D::%D%>",
7148 qualifying_scope, decl);
7149 return error_mark_node;
7150 }
7151 else if (TYPE_P (qualifying_scope))
7152 {
7153 ctype = qualifying_scope;
7154 if (innermost_code != cdk_function
7155 && current_class_type
7156 && !UNIQUELY_DERIVED_FROM_P (ctype,
7157 current_class_type))
7158 {
7159 error ("type %qT is not derived from type %qT",
7160 ctype, current_class_type);
7161 return error_mark_node;
7162 }
7163 }
7164 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL)
7165 in_namespace = qualifying_scope;
7166 }
7167 switch (TREE_CODE (decl))
7168 {
7169 case BIT_NOT_EXPR:
7170 {
7171 tree type;
7172
7173 if (innermost_code != cdk_function)
7174 {
7175 error ("declaration of %qD as non-function", decl);
7176 return error_mark_node;
7177 }
7178 else if (!qualifying_scope
7179 && !(current_class_type && at_class_scope_p ()))
7180 {
7181 error ("declaration of %qD as non-member", decl);
7182 return error_mark_node;
7183 }
7184
7185 type = TREE_OPERAND (decl, 0);
7186 name = IDENTIFIER_POINTER (constructor_name (type));
7187 dname = decl;
7188 }
7189 break;
7190
7191 case TEMPLATE_ID_EXPR:
7192 {
7193 tree fns = TREE_OPERAND (decl, 0);
7194
7195 dname = fns;
7196 if (TREE_CODE (dname) != IDENTIFIER_NODE)
7197 {
7198 gcc_assert (is_overloaded_fn (dname));
7199 dname = DECL_NAME (get_first_fn (dname));
7200 }
7201 }
7202 /* Fall through. */
7203
7204 case IDENTIFIER_NODE:
7205 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7206 dname = decl;
7207
7208 if (C_IS_RESERVED_WORD (dname))
7209 {
7210 error ("declarator-id missing; using reserved word %qD",
7211 dname);
7212 name = IDENTIFIER_POINTER (dname);
7213 }
7214 else if (!IDENTIFIER_TYPENAME_P (dname))
7215 name = IDENTIFIER_POINTER (dname);
7216 else
7217 {
7218 gcc_assert (flags == NO_SPECIAL);
7219 flags = TYPENAME_FLAG;
7220 ctor_return_type = TREE_TYPE (dname);
7221 sfk = sfk_conversion;
7222 if (is_typename_at_global_scope (dname))
7223 name = IDENTIFIER_POINTER (dname);
7224 else
7225 name = "<invalid operator>";
7226 }
7227 break;
7228
7229 default:
7230 gcc_unreachable ();
7231 }
7232 break;
7233
7234 case cdk_array:
7235 case cdk_pointer:
7236 case cdk_reference:
7237 case cdk_ptrmem:
7238 break;
7239
7240 case cdk_error:
7241 return error_mark_node;
7242
7243 default:
7244 gcc_unreachable ();
7245 }
7246 }
7247 if (id_declarator->kind == cdk_id)
7248 break;
7249 }
7250
7251 /* [dcl.fct.edf]
7252
7253 The declarator in a function-definition shall have the form
7254 D1 ( parameter-declaration-clause) ... */
7255 if (funcdef_flag && innermost_code != cdk_function)
7256 {
7257 error ("function definition does not declare parameters");
7258 return error_mark_node;
7259 }
7260
7261 if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
7262 && innermost_code != cdk_function
7263 && ! (ctype && !declspecs->any_specifiers_p))
7264 {
7265 error ("declaration of %qD as non-function", dname);
7266 return error_mark_node;
7267 }
7268
7269 /* Anything declared one level down from the top level
7270 must be one of the parameters of a function
7271 (because the body is at least two levels down). */
7272
7273 /* This heuristic cannot be applied to C++ nodes! Fixed, however,
7274 by not allowing C++ class definitions to specify their parameters
7275 with xdecls (must be spec.d in the parmlist).
7276
7277 Since we now wait to push a class scope until we are sure that
7278 we are in a legitimate method context, we must set oldcname
7279 explicitly (since current_class_name is not yet alive).
7280
7281 We also want to avoid calling this a PARM if it is in a namespace. */
7282
7283 if (decl_context == NORMAL && !toplevel_bindings_p ())
7284 {
7285 struct cp_binding_level *b = current_binding_level;
7286 current_binding_level = b->level_chain;
7287 if (current_binding_level != 0 && toplevel_bindings_p ())
7288 decl_context = PARM;
7289 current_binding_level = b;
7290 }
7291
7292 if (name == NULL)
7293 name = decl_context == PARM ? "parameter" : "type name";
7294
7295 /* If there were multiple types specified in the decl-specifier-seq,
7296 issue an error message. */
7297 if (declspecs->multiple_types_p)
7298 {
7299 error ("two or more data types in declaration of %qs", name);
7300 return error_mark_node;
7301 }
7302
7303 /* Extract the basic type from the decl-specifier-seq. */
7304 type = declspecs->type;
7305 if (type == error_mark_node)
7306 {
7307 type = NULL_TREE;
7308 type_was_error_mark_node = true;
7309 }
7310
7311 /* APPLE LOCAL begin unavailable attribute (radar 2809697) --bowdidge */
7312 /* If the entire declaration is itself tagged as unavailable then
7313 suppress reports of unavailable/deprecated items. If the
7314 entire declaration is tagged as only deprecated we still
7315 report unavailable uses. */
7316 if (type && TREE_DEPRECATED (type) && TREE_UNAVAILABLE (type))
7317 {
7318 if (deprecated_state != DEPRECATED_UNAVAILABLE_SUPPRESS)
7319 warn_deprecated_use (type);
7320 }
7321 else
7322 /* APPLE LOCAL end unavailable attribute (radar 2809697) --bowdidge */
7323 /* If the entire declaration is itself tagged as deprecated then
7324 suppress reports of deprecated items. */
7325 if (type && TREE_DEPRECATED (type)
7326 && deprecated_state != DEPRECATED_SUPPRESS)
7327 warn_deprecated_use (type);
7328 if (type && TREE_CODE (type) == TYPE_DECL)
7329 {
7330 typedef_decl = type;
7331 type = TREE_TYPE (typedef_decl);
7332 }
7333 /* No type at all: default to `int', and set DEFAULTED_INT
7334 because it was not a user-defined typedef. */
7335 if (type == NULL_TREE && (signed_p || unsigned_p || long_p || short_p))
7336 {
7337 /* These imply 'int'. */
7338 type = integer_type_node;
7339 defaulted_int = 1;
7340 }
7341 /* Gather flags. */
7342 explicit_int = declspecs->explicit_int_p;
7343 explicit_char = declspecs->explicit_char_p;
7344
7345 #if 0
7346 /* See the code below that used this. */
7347 if (typedef_decl)
7348 decl_attr = DECL_ATTRIBUTES (typedef_decl);
7349 #endif
7350 typedef_type = type;
7351
7352
7353 if (sfk != sfk_conversion)
7354 ctor_return_type = ctype;
7355
7356 if (sfk != sfk_none)
7357 type = check_special_function_return_type (sfk, type,
7358 ctor_return_type);
7359 else if (type == NULL_TREE)
7360 {
7361 int is_main;
7362
7363 explicit_int = -1;
7364
7365 /* We handle `main' specially here, because 'main () { }' is so
7366 common. With no options, it is allowed. With -Wreturn-type,
7367 it is a warning. It is only an error with -pedantic-errors. */
7368 is_main = (funcdef_flag
7369 && dname && MAIN_NAME_P (dname)
7370 && ctype == NULL_TREE
7371 && in_namespace == NULL_TREE
7372 && current_namespace == global_namespace);
7373
7374 if (type_was_error_mark_node)
7375 /* We've already issued an error, don't complain more. */;
7376 else if (in_system_header || flag_ms_extensions)
7377 /* Allow it, sigh. */;
7378 else if (pedantic || ! is_main)
7379 pedwarn ("ISO C++ forbids declaration of %qs with no type", name);
7380 else if (warn_return_type)
7381 warning (0, "ISO C++ forbids declaration of %qs with no type", name);
7382
7383 type = integer_type_node;
7384 }
7385
7386 ctype = NULL_TREE;
7387
7388 /* Now process the modifiers that were specified
7389 and check for invalid combinations. */
7390
7391 /* Long double is a special combination. */
7392 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node)
7393 {
7394 long_p = false;
7395 type = build_qualified_type (long_double_type_node,
7396 cp_type_quals (type));
7397 }
7398
7399 /* Check all other uses of type modifiers. */
7400
7401 if (unsigned_p || signed_p || long_p || short_p)
7402 {
7403 int ok = 0;
7404
7405 if ((signed_p || unsigned_p) && TREE_CODE (type) != INTEGER_TYPE)
7406 error ("%<signed%> or %<unsigned%> invalid for %qs", name);
7407 else if (signed_p && unsigned_p)
7408 error ("%<signed%> and %<unsigned%> specified together for %qs", name);
7409 else if (longlong && TREE_CODE (type) != INTEGER_TYPE)
7410 error ("%<long long%> invalid for %qs", name);
7411 else if (long_p && TREE_CODE (type) == REAL_TYPE)
7412 error ("%<long%> invalid for %qs", name);
7413 else if (short_p && TREE_CODE (type) == REAL_TYPE)
7414 error ("%<short%> invalid for %qs", name);
7415 else if ((long_p || short_p) && TREE_CODE (type) != INTEGER_TYPE)
7416 error ("%<long%> or %<short%> invalid for %qs", name);
7417 else if ((long_p || short_p) && explicit_char)
7418 error ("%<long%> or %<short%> specified with char for %qs", name);
7419 else if (long_p && short_p)
7420 error ("%<long%> and %<short%> specified together for %qs", name);
7421 else
7422 {
7423 ok = 1;
7424 if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
7425 {
7426 pedwarn ("long, short, signed or unsigned used invalidly for %qs",
7427 name);
7428 if (flag_pedantic_errors)
7429 ok = 0;
7430 }
7431 }
7432
7433 /* Discard the type modifiers if they are invalid. */
7434 if (! ok)
7435 {
7436 unsigned_p = false;
7437 signed_p = false;
7438 long_p = false;
7439 short_p = false;
7440 longlong = 0;
7441 }
7442 }
7443
7444 /* Decide whether an integer type is signed or not.
7445 Optionally treat bitfields as signed by default. */
7446 if (unsigned_p
7447 /* [class.bit]
7448
7449 It is implementation-defined whether a plain (neither
7450 explicitly signed or unsigned) char, short, int, or long
7451 bit-field is signed or unsigned.
7452
7453 Naturally, we extend this to long long as well. Note that
7454 this does not include wchar_t. */
7455 || (bitfield && !flag_signed_bitfields
7456 && !signed_p
7457 /* A typedef for plain `int' without `signed' can be
7458 controlled just like plain `int', but a typedef for
7459 `signed int' cannot be so controlled. */
7460 && !(typedef_decl
7461 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
7462 && TREE_CODE (type) == INTEGER_TYPE
7463 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
7464 {
7465 if (longlong)
7466 type = long_long_unsigned_type_node;
7467 else if (long_p)
7468 type = long_unsigned_type_node;
7469 else if (short_p)
7470 type = short_unsigned_type_node;
7471 else if (type == char_type_node)
7472 type = unsigned_char_type_node;
7473 else if (typedef_decl)
7474 type = c_common_unsigned_type (type);
7475 else
7476 type = unsigned_type_node;
7477 }
7478 else if (signed_p && type == char_type_node)
7479 type = signed_char_type_node;
7480 else if (longlong)
7481 type = long_long_integer_type_node;
7482 else if (long_p)
7483 type = long_integer_type_node;
7484 else if (short_p)
7485 type = short_integer_type_node;
7486
7487 if (declspecs->specs[(int)ds_complex])
7488 {
7489 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
7490 error ("complex invalid for %qs", name);
7491 /* If we just have "complex", it is equivalent to
7492 "complex double", but if any modifiers at all are specified it is
7493 the complex form of TYPE. E.g, "complex short" is
7494 "complex short int". */
7495
7496 else if (defaulted_int && ! longlong
7497 && ! (long_p || short_p || signed_p || unsigned_p))
7498 type = complex_double_type_node;
7499 else if (type == integer_type_node)
7500 type = complex_integer_type_node;
7501 else if (type == float_type_node)
7502 type = complex_float_type_node;
7503 else if (type == double_type_node)
7504 type = complex_double_type_node;
7505 else if (type == long_double_type_node)
7506 type = complex_long_double_type_node;
7507 else
7508 type = build_complex_type (type);
7509 }
7510
7511 type_quals = TYPE_UNQUALIFIED;
7512 if (declspecs->specs[(int)ds_const])
7513 type_quals |= TYPE_QUAL_CONST;
7514 if (declspecs->specs[(int)ds_volatile])
7515 type_quals |= TYPE_QUAL_VOLATILE;
7516 if (declspecs->specs[(int)ds_restrict])
7517 type_quals |= TYPE_QUAL_RESTRICT;
7518 if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
7519 error ("qualifiers are not allowed on declaration of %<operator %T%>",
7520 ctor_return_type);
7521
7522 if (TREE_CODE (type) == FUNCTION_TYPE
7523 && type_quals != TYPE_UNQUALIFIED)
7524 {
7525 /* This was an error in C++98 (cv-qualifiers cannot be added to
7526 a function type), but DR 295 makes the code well-formed by
7527 dropping the extra qualifiers. */
7528 if (pedantic)
7529 {
7530 tree bad_type = build_qualified_type (type, type_quals);
7531 pedwarn ("ignoring %qV qualifiers added to function type %qT",
7532 bad_type, type);
7533 }
7534 type_quals = TYPE_UNQUALIFIED;
7535 }
7536 type_quals |= cp_type_quals (type);
7537 type = cp_build_qualified_type_real
7538 (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
7539 ? tf_ignore_bad_quals : 0) | tf_warning_or_error));
7540 /* We might have ignored or rejected some of the qualifiers. */
7541 type_quals = cp_type_quals (type);
7542
7543 staticp = 0;
7544 inlinep = !! declspecs->specs[(int)ds_inline];
7545 virtualp = !! declspecs->specs[(int)ds_virtual];
7546 explicitp = !! declspecs->specs[(int)ds_explicit];
7547
7548 storage_class = declspecs->storage_class;
7549 if (storage_class == sc_static)
7550 staticp = 1 + (decl_context == FIELD);
7551
7552 if (virtualp && staticp == 2)
7553 {
7554 error ("member %qD cannot be declared both virtual and static", dname);
7555 storage_class = sc_none;
7556 staticp = 0;
7557 }
7558 friendp = !! declspecs->specs[(int)ds_friend];
7559
7560 if (dependent_name && !friendp)
7561 {
7562 error ("%<%T::%D%> is not a valid declarator", ctype, dependent_name);
7563 return error_mark_node;
7564 }
7565
7566 /* Issue errors about use of storage classes for parameters. */
7567 if (decl_context == PARM)
7568 {
7569 if (declspecs->specs[(int)ds_typedef])
7570 {
7571 error ("typedef declaration invalid in parameter declaration");
7572 return error_mark_node;
7573 }
7574 else if (storage_class == sc_static
7575 || storage_class == sc_extern
7576 || thread_p)
7577 error ("storage class specifiers invalid in parameter declarations");
7578 }
7579
7580 /* Give error if `virtual' is used outside of class declaration. */
7581 if (virtualp
7582 && (current_class_name == NULL_TREE || decl_context != FIELD))
7583 {
7584 error ("virtual outside class declaration");
7585 virtualp = 0;
7586 }
7587
7588 /* Static anonymous unions are dealt with here. */
7589 if (staticp && decl_context == TYPENAME
7590 && declspecs->type
7591 && ANON_AGGR_TYPE_P (declspecs->type))
7592 decl_context = FIELD;
7593
7594 /* Warn about storage classes that are invalid for certain
7595 kinds of declarations (parameters, typenames, etc.). */
7596 if (thread_p
7597 && ((storage_class
7598 && storage_class != sc_extern
7599 && storage_class != sc_static)
7600 || declspecs->specs[(int)ds_typedef]))
7601 {
7602 error ("multiple storage classes in declaration of %qs", name);
7603 thread_p = false;
7604 }
7605 if (declspecs->conflicting_specifiers_p)
7606 {
7607 error ("conflicting specifiers in declaration of %qs", name);
7608 storage_class = sc_none;
7609 }
7610 else if (decl_context != NORMAL
7611 && ((storage_class != sc_none
7612 && storage_class != sc_mutable)
7613 || thread_p))
7614 {
7615 if ((decl_context == PARM || decl_context == CATCHPARM)
7616 && (storage_class == sc_register
7617 || storage_class == sc_auto))
7618 ;
7619 else if (declspecs->specs[(int)ds_typedef])
7620 ;
7621 else if (decl_context == FIELD
7622 /* C++ allows static class elements. */
7623 && storage_class == sc_static)
7624 /* C++ also allows inlines and signed and unsigned elements,
7625 but in those cases we don't come in here. */
7626 ;
7627 else
7628 {
7629 if (decl_context == FIELD)
7630 error ("storage class specified for %qs", name);
7631 else
7632 {
7633 if (decl_context == PARM || decl_context == CATCHPARM)
7634 error ("storage class specified for parameter %qs", name);
7635 else
7636 error ("storage class specified for typename");
7637 }
7638 if (storage_class == sc_register
7639 || storage_class == sc_auto
7640 || storage_class == sc_extern
7641 || thread_p)
7642 storage_class = sc_none;
7643 }
7644 }
7645 else if (storage_class == sc_extern && initialized
7646 && !funcdef_flag)
7647 {
7648 if (toplevel_bindings_p ())
7649 {
7650 /* It's common practice (and completely valid) to have a const
7651 be initialized and declared extern. */
7652 if (!(type_quals & TYPE_QUAL_CONST))
7653 warning (0, "%qs initialized and declared %<extern%>", name);
7654 }
7655 else
7656 error ("%qs has both %<extern%> and initializer", name);
7657 }
7658 else if (storage_class == sc_extern && funcdef_flag
7659 && ! toplevel_bindings_p ())
7660 error ("nested function %qs declared %<extern%>", name);
7661 else if (toplevel_bindings_p ())
7662 {
7663 if (storage_class == sc_auto)
7664 error ("top-level declaration of %qs specifies %<auto%>", name);
7665 }
7666 else if (thread_p
7667 && storage_class != sc_extern
7668 && storage_class != sc_static)
7669 {
7670 error ("function-scope %qs implicitly auto and declared %<__thread%>",
7671 name);
7672 thread_p = false;
7673 }
7674
7675 if (storage_class && friendp)
7676 error ("storage class specifiers invalid in friend function declarations");
7677
7678 if (!id_declarator)
7679 unqualified_id = NULL_TREE;
7680 else
7681 {
7682 unqualified_id = id_declarator->u.id.unqualified_name;
7683 switch (TREE_CODE (unqualified_id))
7684 {
7685 case BIT_NOT_EXPR:
7686 unqualified_id
7687 = constructor_name (TREE_OPERAND (unqualified_id, 0));
7688 break;
7689
7690 case IDENTIFIER_NODE:
7691 case TEMPLATE_ID_EXPR:
7692 break;
7693
7694 default:
7695 gcc_unreachable ();
7696 }
7697 }
7698
7699 /* Determine the type of the entity declared by recurring on the
7700 declarator. */
7701 for (; declarator; declarator = declarator->declarator)
7702 {
7703 const cp_declarator *inner_declarator;
7704 tree attrs;
7705
7706 if (type == error_mark_node)
7707 return error_mark_node;
7708
7709 attrs = declarator->attributes;
7710 if (attrs)
7711 {
7712 int attr_flags;
7713
7714 attr_flags = 0;
7715 if (declarator == NULL || declarator->kind == cdk_id)
7716 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
7717 if (declarator->kind == cdk_function)
7718 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
7719 if (declarator->kind == cdk_array)
7720 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
7721 returned_attrs = decl_attributes (&type,
7722 chainon (returned_attrs, attrs),
7723 attr_flags);
7724 }
7725
7726 if (declarator->kind == cdk_id)
7727 break;
7728
7729 inner_declarator = declarator->declarator;
7730
7731 switch (declarator->kind)
7732 {
7733 case cdk_array:
7734 type = create_array_type_for_decl (dname, type,
7735 declarator->u.array.bounds);
7736 break;
7737
7738 case cdk_function:
7739 {
7740 tree arg_types;
7741 int funcdecl_p;
7742
7743 /* Declaring a function type.
7744 Make sure we have a valid type for the function to return. */
7745
7746 /* We now know that the TYPE_QUALS don't apply to the
7747 decl, but to its return type. */
7748 type_quals = TYPE_UNQUALIFIED;
7749
7750 /* Warn about some types functions can't return. */
7751
7752 if (TREE_CODE (type) == FUNCTION_TYPE)
7753 {
7754 error ("%qs declared as function returning a function", name);
7755 type = integer_type_node;
7756 }
7757 if (TREE_CODE (type) == ARRAY_TYPE)
7758 {
7759 error ("%qs declared as function returning an array", name);
7760 type = integer_type_node;
7761 }
7762
7763 /* Pick up type qualifiers which should be applied to `this'. */
7764 memfn_quals = declarator->u.function.qualifiers;
7765
7766 /* Pick up the exception specifications. */
7767 raises = declarator->u.function.exception_specification;
7768
7769 /* Say it's a definition only for the CALL_EXPR
7770 closest to the identifier. */
7771 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id;
7772
7773 if (ctype == NULL_TREE
7774 && decl_context == FIELD
7775 && funcdecl_p
7776 && (friendp == 0 || dname == current_class_name))
7777 ctype = current_class_type;
7778
7779 if (ctype && (sfk == sfk_constructor
7780 || sfk == sfk_destructor))
7781 {
7782 /* We are within a class's scope. If our declarator name
7783 is the same as the class name, and we are defining
7784 a function, then it is a constructor/destructor, and
7785 therefore returns a void type. */
7786
7787 /* ISO C++ 12.4/2. A destructor may not be declared
7788 const or volatile. A destructor may not be
7789 static.
7790
7791 ISO C++ 12.1. A constructor may not be declared
7792 const or volatile. A constructor may not be
7793 virtual. A constructor may not be static. */
7794 if (staticp == 2)
7795 error ((flags == DTOR_FLAG)
7796 ? "destructor cannot be static member function"
7797 : "constructor cannot be static member function");
7798 if (memfn_quals)
7799 {
7800 error ((flags == DTOR_FLAG)
7801 ? "destructors may not be cv-qualified"
7802 : "constructors may not be cv-qualified");
7803 memfn_quals = TYPE_UNQUALIFIED;
7804 }
7805
7806 if (decl_context == FIELD
7807 && !member_function_or_else (ctype,
7808 current_class_type,
7809 flags))
7810 return error_mark_node;
7811
7812 if (flags != DTOR_FLAG)
7813 {
7814 /* It's a constructor. */
7815 if (explicitp == 1)
7816 explicitp = 2;
7817 if (virtualp)
7818 {
7819 pedwarn ("constructors cannot be declared virtual");
7820 virtualp = 0;
7821 }
7822 if (decl_context == FIELD
7823 && sfk != sfk_constructor)
7824 return error_mark_node;
7825 }
7826 if (decl_context == FIELD)
7827 staticp = 0;
7828 }
7829 else if (friendp)
7830 {
7831 if (initialized)
7832 error ("can't initialize friend function %qs", name);
7833 if (virtualp)
7834 {
7835 /* Cannot be both friend and virtual. */
7836 error ("virtual functions cannot be friends");
7837 friendp = 0;
7838 }
7839 if (decl_context == NORMAL)
7840 error ("friend declaration not in class definition");
7841 if (current_function_decl && funcdef_flag)
7842 error ("can't define friend function %qs in a local "
7843 "class definition",
7844 name);
7845 }
7846
7847 arg_types = grokparms (declarator->u.function.parameters,
7848 &parms);
7849
7850 if (inner_declarator
7851 && inner_declarator->kind == cdk_id
7852 && inner_declarator->u.id.sfk == sfk_destructor
7853 && arg_types != void_list_node)
7854 {
7855 error ("destructors may not have parameters");
7856 arg_types = void_list_node;
7857 parms = NULL_TREE;
7858 }
7859
7860 type = build_function_type (type, arg_types);
7861 }
7862 break;
7863
7864 case cdk_pointer:
7865 case cdk_reference:
7866 case cdk_ptrmem:
7867 /* Filter out pointers-to-references and references-to-references.
7868 We can get these if a TYPE_DECL is used. */
7869
7870 if (TREE_CODE (type) == REFERENCE_TYPE)
7871 {
7872 error (declarator->kind == cdk_reference
7873 ? "cannot declare reference to %q#T"
7874 : "cannot declare pointer to %q#T", type);
7875 type = TREE_TYPE (type);
7876 }
7877 else if (VOID_TYPE_P (type))
7878 {
7879 if (declarator->kind == cdk_reference)
7880 error ("cannot declare reference to %q#T", type);
7881 else if (declarator->kind == cdk_ptrmem)
7882 error ("cannot declare pointer to %q#T member", type);
7883 }
7884
7885 /* We now know that the TYPE_QUALS don't apply to the decl,
7886 but to the target of the pointer. */
7887 type_quals = TYPE_UNQUALIFIED;
7888
7889 if (declarator->kind == cdk_ptrmem
7890 && (TREE_CODE (type) == FUNCTION_TYPE || memfn_quals))
7891 {
7892 memfn_quals |= cp_type_quals (type);
7893 type = build_memfn_type (type,
7894 declarator->u.pointer.class_type,
7895 memfn_quals);
7896 memfn_quals = TYPE_UNQUALIFIED;
7897 }
7898
7899 if (declarator->kind == cdk_reference)
7900 {
7901 if (!VOID_TYPE_P (type))
7902 type = build_reference_type (type);
7903 }
7904 else if (TREE_CODE (type) == METHOD_TYPE)
7905 type = build_ptrmemfunc_type (build_pointer_type (type));
7906 else if (declarator->kind == cdk_ptrmem)
7907 {
7908 gcc_assert (TREE_CODE (declarator->u.pointer.class_type)
7909 != NAMESPACE_DECL);
7910 if (declarator->u.pointer.class_type == error_mark_node)
7911 /* We will already have complained. */
7912 type = error_mark_node;
7913 else
7914 type = build_ptrmem_type (declarator->u.pointer.class_type,
7915 type);
7916 }
7917 else
7918 type = build_pointer_type (type);
7919
7920 /* Process a list of type modifier keywords (such as
7921 const or volatile) that were given inside the `*' or `&'. */
7922
7923 if (declarator->u.pointer.qualifiers)
7924 {
7925 type
7926 = cp_build_qualified_type (type,
7927 declarator->u.pointer.qualifiers);
7928 type_quals = cp_type_quals (type);
7929 }
7930 ctype = NULL_TREE;
7931 break;
7932
7933 case cdk_error:
7934 break;
7935
7936 default:
7937 gcc_unreachable ();
7938 }
7939 }
7940
7941 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR
7942 && TREE_CODE (type) != FUNCTION_TYPE
7943 && TREE_CODE (type) != METHOD_TYPE)
7944 {
7945 error ("template-id %qD used as a declarator",
7946 unqualified_id);
7947 unqualified_id = dname;
7948 }
7949
7950 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly
7951 qualified with a class-name, turn it into a METHOD_TYPE, unless
7952 we know that the function is static. We take advantage of this
7953 opportunity to do other processing that pertains to entities
7954 explicitly declared to be class members. Note that if DECLARATOR
7955 is non-NULL, we know it is a cdk_id declarator; otherwise, we
7956 would not have exited the loop above. */
7957 if (declarator
7958 && declarator->u.id.qualifying_scope
7959 && TYPE_P (declarator->u.id.qualifying_scope))
7960 {
7961 tree t;
7962
7963 ctype = declarator->u.id.qualifying_scope;
7964 ctype = TYPE_MAIN_VARIANT (ctype);
7965 t = ctype;
7966 while (t != NULL_TREE && CLASS_TYPE_P (t))
7967 {
7968 /* You're supposed to have one `template <...>' for every
7969 template class, but you don't need one for a full
7970 specialization. For example:
7971
7972 template <class T> struct S{};
7973 template <> struct S<int> { void f(); };
7974 void S<int>::f () {}
7975
7976 is correct; there shouldn't be a `template <>' for the
7977 definition of `S<int>::f'. */
7978 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
7979 && !any_dependent_template_arguments_p (CLASSTYPE_TI_ARGS (t)))
7980 /* T is an explicit (not partial) specialization. All
7981 containing classes must therefore also be explicitly
7982 specialized. */
7983 break;
7984 if ((CLASSTYPE_USE_TEMPLATE (t) || CLASSTYPE_IS_TEMPLATE (t))
7985 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
7986 template_count += 1;
7987
7988 t = TYPE_MAIN_DECL (t);
7989 t = DECL_CONTEXT (t);
7990 }
7991
7992 if (ctype == current_class_type)
7993 {
7994 if (friendp)
7995 pedwarn ("member functions are implicitly friends of their class");
7996 else
7997 pedwarn ("extra qualification %<%T::%> on member %qs",
7998 ctype, name);
7999 }
8000 else if (/* If the qualifying type is already complete, then we
8001 can skip the following checks. */
8002 !COMPLETE_TYPE_P (ctype)
8003 && (/* If the function is being defined, then
8004 qualifying type must certainly be complete. */
8005 funcdef_flag
8006 /* A friend declaration of "T::f" is OK, even if
8007 "T" is a template parameter. But, if this
8008 function is not a friend, the qualifying type
8009 must be a class. */
8010 || (!friendp && !CLASS_TYPE_P (ctype))
8011 /* For a declaration, the type need not be
8012 complete, if either it is dependent (since there
8013 is no meaningful definition of complete in that
8014 case) or the qualifying class is currently being
8015 defined. */
8016 || !(dependent_type_p (ctype)
8017 || currently_open_class (ctype)))
8018 /* Check that the qualifying type is complete. */
8019 && !complete_type_or_else (ctype, NULL_TREE))
8020 return error_mark_node;
8021 else if (TREE_CODE (type) == FUNCTION_TYPE)
8022 {
8023 tree sname = declarator->u.id.unqualified_name;
8024
8025 if (current_class_type
8026 && (!friendp || funcdef_flag))
8027 {
8028 error (funcdef_flag
8029 ? "cannot define member function %<%T::%s%> within %<%T%>"
8030 : "cannot declare member function %<%T::%s%> within %<%T%>",
8031 ctype, name, current_class_type);
8032 return error_mark_node;
8033 }
8034
8035 if (TREE_CODE (sname) == IDENTIFIER_NODE
8036 && NEW_DELETE_OPNAME_P (sname))
8037 /* Overloaded operator new and operator delete
8038 are always static functions. */
8039 ;
8040 else
8041 type = build_memfn_type (type, ctype, memfn_quals);
8042 }
8043 else if (declspecs->specs[(int)ds_typedef]
8044 && current_class_type)
8045 {
8046 error ("cannot declare member %<%T::%s%> within %qT",
8047 ctype, name, current_class_type);
8048 return error_mark_node;
8049 }
8050 }
8051
8052 /* Now TYPE has the actual type. */
8053
8054 if (returned_attrs)
8055 {
8056 if (attrlist)
8057 *attrlist = chainon (returned_attrs, *attrlist);
8058 else
8059 attrlist = &returned_attrs;
8060 }
8061
8062 /* Did array size calculations overflow? */
8063
8064 if (TREE_CODE (type) == ARRAY_TYPE
8065 && COMPLETE_TYPE_P (type)
8066 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8067 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
8068 {
8069 error ("size of array %qs is too large", name);
8070 /* If we proceed with the array type as it is, we'll eventually
8071 crash in tree_low_cst(). */
8072 type = error_mark_node;
8073 }
8074
8075 if ((decl_context == FIELD || decl_context == PARM)
8076 && !processing_template_decl
8077 && variably_modified_type_p (type, NULL_TREE))
8078 {
8079 if (decl_context == FIELD)
8080 error ("data member may not have variably modified type %qT", type);
8081 else
8082 error ("parameter may not have variably modified type %qT", type);
8083 type = error_mark_node;
8084 }
8085
8086 if (explicitp == 1 || (explicitp && friendp))
8087 {
8088 /* [dcl.fct.spec] The explicit specifier shall only be used in
8089 declarations of constructors within a class definition. */
8090 error ("only declarations of constructors can be %<explicit%>");
8091 explicitp = 0;
8092 }
8093
8094 if (storage_class == sc_mutable)
8095 {
8096 if (decl_context != FIELD || friendp)
8097 {
8098 error ("non-member %qs cannot be declared %<mutable%>", name);
8099 storage_class = sc_none;
8100 }
8101 else if (decl_context == TYPENAME || declspecs->specs[(int)ds_typedef])
8102 {
8103 error ("non-object member %qs cannot be declared %<mutable%>", name);
8104 storage_class = sc_none;
8105 }
8106 else if (TREE_CODE (type) == FUNCTION_TYPE
8107 || TREE_CODE (type) == METHOD_TYPE)
8108 {
8109 error ("function %qs cannot be declared %<mutable%>", name);
8110 storage_class = sc_none;
8111 }
8112 else if (staticp)
8113 {
8114 error ("static %qs cannot be declared %<mutable%>", name);
8115 storage_class = sc_none;
8116 }
8117 else if (type_quals & TYPE_QUAL_CONST)
8118 {
8119 error ("const %qs cannot be declared %<mutable%>", name);
8120 storage_class = sc_none;
8121 }
8122 }
8123
8124 /* If this is declaring a typedef name, return a TYPE_DECL. */
8125 if (declspecs->specs[(int)ds_typedef] && decl_context != TYPENAME)
8126 {
8127 tree decl;
8128
8129 /* Note that the grammar rejects storage classes
8130 in typenames, fields or parameters. */
8131 if (current_lang_name == lang_name_java)
8132 TYPE_FOR_JAVA (type) = 1;
8133
8134 /* This declaration:
8135
8136 typedef void f(int) const;
8137
8138 declares a function type which is not a member of any
8139 particular class, but which is cv-qualified; for
8140 example "f S::*" declares a pointer to a const-qualified
8141 member function of S. We record the cv-qualification in the
8142 function type. */
8143 if (memfn_quals && TREE_CODE (type) == FUNCTION_TYPE)
8144 type = cp_build_qualified_type (type, memfn_quals);
8145
8146 if (decl_context == FIELD)
8147 decl = build_lang_decl (TYPE_DECL, unqualified_id, type);
8148 else
8149 decl = build_decl (TYPE_DECL, unqualified_id, type);
8150 if (id_declarator && declarator->u.id.qualifying_scope)
8151 error ("%Jtypedef name may not be a nested-name-specifier", decl);
8152
8153 if (decl_context != FIELD)
8154 {
8155 if (!current_function_decl)
8156 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8157 else if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (current_function_decl)
8158 || (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P
8159 (current_function_decl)))
8160 /* The TYPE_DECL is "abstract" because there will be
8161 clones of this constructor/destructor, and there will
8162 be copies of this TYPE_DECL generated in those
8163 clones. */
8164 DECL_ABSTRACT (decl) = 1;
8165 }
8166 else if (constructor_name_p (unqualified_id, current_class_type))
8167 pedwarn ("ISO C++ forbids nested type %qD with same name "
8168 "as enclosing class",
8169 unqualified_id);
8170
8171 /* If the user declares "typedef struct {...} foo" then the
8172 struct will have an anonymous name. Fill that name in now.
8173 Nothing can refer to it, so nothing needs know about the name
8174 change. */
8175 if (type != error_mark_node
8176 && unqualified_id
8177 && TYPE_NAME (type)
8178 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8179 && TYPE_ANONYMOUS_P (type)
8180 /* Don't do this if there are attributes. */
8181 && (!attrlist || !*attrlist)
8182 && cp_type_quals (type) == TYPE_UNQUALIFIED)
8183 {
8184 tree oldname = TYPE_NAME (type);
8185 tree t;
8186
8187 /* Replace the anonymous name with the real name everywhere. */
8188 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
8189 if (TYPE_NAME (t) == oldname)
8190 TYPE_NAME (t) = decl;
8191
8192 if (TYPE_LANG_SPECIFIC (type))
8193 TYPE_WAS_ANONYMOUS (type) = 1;
8194
8195 /* If this is a typedef within a template class, the nested
8196 type is a (non-primary) template. The name for the
8197 template needs updating as well. */
8198 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
8199 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
8200 = TYPE_IDENTIFIER (type);
8201
8202 /* FIXME remangle member functions; member functions of a
8203 type with external linkage have external linkage. */
8204 }
8205
8206 /* Any qualifiers on a function type typedef have already been
8207 dealt with. */
8208 if (memfn_quals && !ctype && TREE_CODE (type) == FUNCTION_TYPE)
8209 memfn_quals = TYPE_UNQUALIFIED;
8210
8211 if (signed_p
8212 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
8213 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
8214
8215 bad_specifiers (decl, "type", virtualp,
8216 memfn_quals != TYPE_UNQUALIFIED,
8217 inlinep, friendp, raises != NULL_TREE);
8218
8219 return decl;
8220 }
8221
8222 /* Detect the case of an array type of unspecified size
8223 which came, as such, direct from a typedef name.
8224 We must copy the type, so that the array's domain can be
8225 individually set by the object's initializer. */
8226
8227 if (type && typedef_type
8228 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
8229 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
8230 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
8231
8232 /* Detect where we're using a typedef of function type to declare a
8233 function. PARMS will not be set, so we must create it now. */
8234
8235 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
8236 {
8237 tree decls = NULL_TREE;
8238 tree args;
8239
8240 for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
8241 {
8242 tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
8243
8244 TREE_CHAIN (decl) = decls;
8245 decls = decl;
8246 }
8247
8248 parms = nreverse (decls);
8249
8250 if (decl_context != TYPENAME)
8251 {
8252 /* A cv-qualifier-seq shall only be part of the function type
8253 for a non-static member function. [8.3.5/4 dcl.fct] */
8254 if (cp_type_quals (type) != TYPE_UNQUALIFIED
8255 && (current_class_type == NULL_TREE || staticp) )
8256 {
8257 error ("qualified function types cannot be used to declare %s functions",
8258 (staticp? "static member" : "free"));
8259 type = TYPE_MAIN_VARIANT (type);
8260 }
8261
8262 /* The qualifiers on the function type become the qualifiers on
8263 the non-static member function. */
8264 memfn_quals |= cp_type_quals (type);
8265 }
8266 }
8267
8268 /* If this is a type name (such as, in a cast or sizeof),
8269 compute the type and return it now. */
8270
8271 if (decl_context == TYPENAME)
8272 {
8273 /* Note that the grammar rejects storage classes
8274 in typenames, fields or parameters. */
8275 if (type_quals != TYPE_UNQUALIFIED)
8276 type_quals = TYPE_UNQUALIFIED;
8277
8278 /* Special case: "friend class foo" looks like a TYPENAME context. */
8279 if (friendp)
8280 {
8281 if (type_quals != TYPE_UNQUALIFIED)
8282 {
8283 error ("type qualifiers specified for friend class declaration");
8284 type_quals = TYPE_UNQUALIFIED;
8285 }
8286 if (inlinep)
8287 {
8288 error ("%<inline%> specified for friend class declaration");
8289 inlinep = 0;
8290 }
8291
8292 if (!current_aggr)
8293 {
8294 /* Don't allow friend declaration without a class-key. */
8295 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
8296 pedwarn ("template parameters cannot be friends");
8297 else if (TREE_CODE (type) == TYPENAME_TYPE)
8298 pedwarn ("friend declaration requires class-key, "
8299 "i.e. %<friend class %T::%D%>",
8300 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
8301 else
8302 pedwarn ("friend declaration requires class-key, "
8303 "i.e. %<friend %#T%>",
8304 type);
8305 }
8306
8307 /* Only try to do this stuff if we didn't already give up. */
8308 if (type != integer_type_node)
8309 {
8310 /* A friendly class? */
8311 if (current_class_type)
8312 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type),
8313 /*complain=*/true);
8314 else
8315 error ("trying to make class %qT a friend of global scope",
8316 type);
8317
8318 type = void_type_node;
8319 }
8320 }
8321 else if (memfn_quals)
8322 {
8323 if (ctype == NULL_TREE)
8324 {
8325 if (TREE_CODE (type) != METHOD_TYPE)
8326 error ("invalid qualifiers on non-member function type");
8327 else
8328 ctype = TYPE_METHOD_BASETYPE (type);
8329 }
8330 if (ctype)
8331 type = build_memfn_type (type, ctype, memfn_quals);
8332 }
8333
8334 return type;
8335 }
8336 else if (unqualified_id == NULL_TREE && decl_context != PARM
8337 && decl_context != CATCHPARM
8338 && TREE_CODE (type) != UNION_TYPE
8339 && ! bitfield)
8340 {
8341 error ("abstract declarator %qT used as declaration", type);
8342 return error_mark_node;
8343 }
8344
8345 /* Only functions may be declared using an operator-function-id. */
8346 if (unqualified_id
8347 && IDENTIFIER_OPNAME_P (unqualified_id)
8348 && TREE_CODE (type) != FUNCTION_TYPE
8349 && TREE_CODE (type) != METHOD_TYPE)
8350 {
8351 error ("declaration of %qD as non-function", unqualified_id);
8352 return error_mark_node;
8353 }
8354
8355 /* We don't check parameter types here because we can emit a better
8356 error message later. */
8357 if (decl_context != PARM)
8358 {
8359 type = check_var_type (unqualified_id, type);
8360 if (type == error_mark_node)
8361 return error_mark_node;
8362 }
8363
8364 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
8365 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
8366
8367 if (decl_context == PARM || decl_context == CATCHPARM)
8368 {
8369 if (ctype || in_namespace)
8370 error ("cannot use %<::%> in parameter declaration");
8371
8372 /* A parameter declared as an array of T is really a pointer to T.
8373 One declared as a function is really a pointer to a function.
8374 One declared as a member is really a pointer to member. */
8375
8376 if (TREE_CODE (type) == ARRAY_TYPE)
8377 {
8378 /* Transfer const-ness of array into that of type pointed to. */
8379 type = build_pointer_type (TREE_TYPE (type));
8380 type_quals = TYPE_UNQUALIFIED;
8381 }
8382 else if (TREE_CODE (type) == FUNCTION_TYPE)
8383 type = build_pointer_type (type);
8384 }
8385
8386 {
8387 tree decl;
8388
8389 if (decl_context == PARM)
8390 {
8391 decl = cp_build_parm_decl (unqualified_id, type);
8392
8393 bad_specifiers (decl, "parameter", virtualp,
8394 memfn_quals != TYPE_UNQUALIFIED,
8395 inlinep, friendp, raises != NULL_TREE);
8396 }
8397 else if (decl_context == FIELD)
8398 {
8399 /* The C99 flexible array extension. */
8400 if (!staticp && TREE_CODE (type) == ARRAY_TYPE
8401 && TYPE_DOMAIN (type) == NULL_TREE)
8402 {
8403 tree itype = compute_array_index_type (dname, integer_zero_node);
8404 type = build_cplus_array_type (TREE_TYPE (type), itype);
8405 }
8406
8407 if (type == error_mark_node)
8408 {
8409 /* Happens when declaring arrays of sizes which
8410 are error_mark_node, for example. */
8411 decl = NULL_TREE;
8412 }
8413 else if (in_namespace && !friendp)
8414 {
8415 /* Something like struct S { int N::j; }; */
8416 error ("invalid use of %<::%>");
8417 return error_mark_node;
8418 }
8419 else if (TREE_CODE (type) == FUNCTION_TYPE)
8420 {
8421 int publicp = 0;
8422 tree function_context;
8423
8424 if (friendp == 0)
8425 {
8426 if (ctype == NULL_TREE)
8427 ctype = current_class_type;
8428
8429 if (ctype == NULL_TREE)
8430 {
8431 error ("can't make %qD into a method -- not in a class",
8432 unqualified_id);
8433 return error_mark_node;
8434 }
8435
8436 /* ``A union may [ ... ] not [ have ] virtual functions.''
8437 ARM 9.5 */
8438 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
8439 {
8440 error ("function %qD declared virtual inside a union",
8441 unqualified_id);
8442 return error_mark_node;
8443 }
8444
8445 if (NEW_DELETE_OPNAME_P (unqualified_id))
8446 {
8447 if (virtualp)
8448 {
8449 error ("%qD cannot be declared virtual, since it "
8450 "is always static",
8451 unqualified_id);
8452 virtualp = 0;
8453 }
8454 }
8455 else if (staticp < 2)
8456 type = build_memfn_type (type, ctype, memfn_quals);
8457 }
8458
8459 /* Check that the name used for a destructor makes sense. */
8460 if (sfk == sfk_destructor)
8461 {
8462 if (!ctype)
8463 {
8464 gcc_assert (friendp);
8465 error ("expected qualified name in friend declaration "
8466 "for destructor %qD",
8467 id_declarator->u.id.unqualified_name);
8468 return error_mark_node;
8469 }
8470
8471 if (!same_type_p (TREE_OPERAND
8472 (id_declarator->u.id.unqualified_name, 0),
8473 ctype))
8474 {
8475 error ("declaration of %qD as member of %qT",
8476 id_declarator->u.id.unqualified_name, ctype);
8477 return error_mark_node;
8478 }
8479 }
8480
8481 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */
8482 function_context = (ctype != NULL_TREE) ?
8483 decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
8484 publicp = (! friendp || ! staticp)
8485 && function_context == NULL_TREE;
8486 decl = grokfndecl (ctype, type,
8487 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8488 ? unqualified_id : dname,
8489 parms,
8490 unqualified_id,
8491 virtualp, flags, memfn_quals, raises,
8492 friendp ? -1 : 0, friendp, publicp, inlinep,
8493 sfk,
8494 funcdef_flag, template_count, in_namespace, attrlist);
8495 if (decl == NULL_TREE)
8496 return error_mark_node;
8497 #if 0
8498 /* This clobbers the attrs stored in `decl' from `attrlist'. */
8499 /* The decl and setting of decl_attr is also turned off. */
8500 decl = build_decl_attribute_variant (decl, decl_attr);
8501 #endif
8502
8503 /* [class.conv.ctor]
8504
8505 A constructor declared without the function-specifier
8506 explicit that can be called with a single parameter
8507 specifies a conversion from the type of its first
8508 parameter to the type of its class. Such a constructor
8509 is called a converting constructor. */
8510 if (explicitp == 2)
8511 DECL_NONCONVERTING_P (decl) = 1;
8512 else if (DECL_CONSTRUCTOR_P (decl))
8513 {
8514 /* The constructor can be called with exactly one
8515 parameter if there is at least one parameter, and
8516 any subsequent parameters have default arguments.
8517 Ignore any compiler-added parms. */
8518 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
8519
8520 if (arg_types == void_list_node
8521 || (arg_types
8522 && TREE_CHAIN (arg_types)
8523 && TREE_CHAIN (arg_types) != void_list_node
8524 && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
8525 DECL_NONCONVERTING_P (decl) = 1;
8526 }
8527 }
8528 else if (TREE_CODE (type) == METHOD_TYPE)
8529 {
8530 /* We only get here for friend declarations of
8531 members of other classes. */
8532 /* All method decls are public, so tell grokfndecl to set
8533 TREE_PUBLIC, also. */
8534 decl = grokfndecl (ctype, type,
8535 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR
8536 ? unqualified_id : dname,
8537 parms,
8538 unqualified_id,
8539 virtualp, flags, memfn_quals, raises,
8540 friendp ? -1 : 0, friendp, 1, 0, sfk,
8541 funcdef_flag, template_count, in_namespace,
8542 attrlist);
8543 if (decl == NULL_TREE)
8544 return error_mark_node;
8545 }
8546 else if (!staticp && !dependent_type_p (type)
8547 && !COMPLETE_TYPE_P (complete_type (type))
8548 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
8549 {
8550 if (unqualified_id)
8551 error ("field %qD has incomplete type", unqualified_id);
8552 else
8553 error ("name %qT has incomplete type", type);
8554
8555 /* If we're instantiating a template, tell them which
8556 instantiation made the field's type be incomplete. */
8557 if (current_class_type
8558 && TYPE_NAME (current_class_type)
8559 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
8560 && declspecs->type
8561 && declspecs->type == type)
8562 error (" in instantiation of template %qT",
8563 current_class_type);
8564
8565 return error_mark_node;
8566 }
8567 else
8568 {
8569 if (friendp)
8570 {
8571 error ("%qE is neither function nor member function; "
8572 "cannot be declared friend", unqualified_id);
8573 friendp = 0;
8574 }
8575 decl = NULL_TREE;
8576 }
8577
8578 if (friendp)
8579 {
8580 /* Friends are treated specially. */
8581 if (ctype == current_class_type)
8582 ; /* We already issued a pedwarn. */
8583 else if (decl && DECL_NAME (decl))
8584 {
8585 if (template_class_depth (current_class_type) == 0)
8586 {
8587 decl = check_explicit_specialization
8588 (unqualified_id, decl, template_count,
8589 2 * funcdef_flag + 4);
8590 if (decl == error_mark_node)
8591 return error_mark_node;
8592 }
8593
8594 decl = do_friend (ctype, unqualified_id, decl,
8595 *attrlist, flags,
8596 funcdef_flag);
8597 return decl;
8598 }
8599 else
8600 return error_mark_node;
8601 }
8602
8603 /* Structure field. It may not be a function, except for C++. */
8604
8605 if (decl == NULL_TREE)
8606 {
8607 if (initialized)
8608 {
8609 if (!staticp)
8610 {
8611 /* An attempt is being made to initialize a non-static
8612 member. But, from [class.mem]:
8613
8614 4 A member-declarator can contain a
8615 constant-initializer only if it declares a static
8616 member (_class.static_) of integral or enumeration
8617 type, see _class.static.data_.
8618
8619 This used to be relatively common practice, but
8620 the rest of the compiler does not correctly
8621 handle the initialization unless the member is
8622 static so we make it static below. */
8623 pedwarn ("ISO C++ forbids initialization of member %qD",
8624 unqualified_id);
8625 pedwarn ("making %qD static", unqualified_id);
8626 staticp = 1;
8627 }
8628
8629 if (uses_template_parms (type))
8630 /* We'll check at instantiation time. */
8631 ;
8632 else if (check_static_variable_definition (unqualified_id,
8633 type))
8634 /* If we just return the declaration, crashes
8635 will sometimes occur. We therefore return
8636 void_type_node, as if this was a friend
8637 declaration, to cause callers to completely
8638 ignore this declaration. */
8639 return error_mark_node;
8640 }
8641
8642 if (staticp)
8643 {
8644 /* C++ allows static class members. All other work
8645 for this is done by grokfield. */
8646 decl = build_lang_decl (VAR_DECL, unqualified_id, type);
8647 set_linkage_for_static_data_member (decl);
8648 /* Even if there is an in-class initialization, DECL
8649 is considered undefined until an out-of-class
8650 definition is provided. */
8651 DECL_EXTERNAL (decl) = 1;
8652
8653 if (thread_p)
8654 {
8655 if (targetm.have_tls)
8656 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
8657 else
8658 /* A mere warning is sure to result in improper
8659 semantics at runtime. Don't bother to allow this to
8660 compile. */
8661 error ("thread-local storage not supported for this target");
8662 }
8663 }
8664 else
8665 {
8666 decl = build_decl (FIELD_DECL, unqualified_id, type);
8667 DECL_NONADDRESSABLE_P (decl) = bitfield;
8668 if (storage_class == sc_mutable)
8669 {
8670 DECL_MUTABLE_P (decl) = 1;
8671 storage_class = sc_none;
8672 }
8673 }
8674
8675 bad_specifiers (decl, "field", virtualp,
8676 memfn_quals != TYPE_UNQUALIFIED,
8677 inlinep, friendp, raises != NULL_TREE);
8678 }
8679 }
8680 else if (TREE_CODE (type) == FUNCTION_TYPE
8681 || TREE_CODE (type) == METHOD_TYPE)
8682 {
8683 tree original_name;
8684 int publicp = 0;
8685
8686 if (!unqualified_id)
8687 return error_mark_node;
8688
8689 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
8690 original_name = dname;
8691 else
8692 original_name = unqualified_id;
8693
8694 if (storage_class == sc_auto)
8695 error ("storage class %<auto%> invalid for function %qs", name);
8696 else if (storage_class == sc_register)
8697 error ("storage class %<register%> invalid for function %qs", name);
8698 else if (thread_p)
8699 error ("storage class %<__thread%> invalid for function %qs", name);
8700
8701 /* Function declaration not at top level.
8702 Storage classes other than `extern' are not allowed
8703 and `extern' makes no difference. */
8704 if (! toplevel_bindings_p ()
8705 && (storage_class == sc_static
8706 || declspecs->specs[(int)ds_inline])
8707 && pedantic)
8708 {
8709 if (storage_class == sc_static)
8710 pedwarn ("%<static%> specified invalid for function %qs "
8711 "declared out of global scope", name);
8712 else
8713 pedwarn ("%<inline%> specifier invalid for function %qs "
8714 "declared out of global scope", name);
8715 }
8716
8717 if (ctype == NULL_TREE)
8718 {
8719 if (virtualp)
8720 {
8721 error ("virtual non-class function %qs", name);
8722 virtualp = 0;
8723 }
8724 }
8725 else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
8726 && !NEW_DELETE_OPNAME_P (original_name))
8727 type = build_method_type_directly (ctype,
8728 TREE_TYPE (type),
8729 TYPE_ARG_TYPES (type));
8730
8731 /* Record presence of `static'. */
8732 publicp = (ctype != NULL_TREE
8733 || storage_class == sc_extern
8734 || storage_class != sc_static);
8735
8736 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id,
8737 virtualp, flags, memfn_quals, raises,
8738 1, friendp,
8739 publicp, inlinep, sfk, funcdef_flag,
8740 template_count, in_namespace, attrlist);
8741 if (decl == NULL_TREE)
8742 return error_mark_node;
8743
8744 if (staticp == 1)
8745 {
8746 int invalid_static = 0;
8747
8748 /* Don't allow a static member function in a class, and forbid
8749 declaring main to be static. */
8750 if (TREE_CODE (type) == METHOD_TYPE)
8751 {
8752 pedwarn ("cannot declare member function %qD to have "
8753 "static linkage", decl);
8754 invalid_static = 1;
8755 }
8756 else if (current_function_decl)
8757 {
8758 /* FIXME need arm citation */
8759 error ("cannot declare static function inside another function");
8760 invalid_static = 1;
8761 }
8762
8763 if (invalid_static)
8764 {
8765 staticp = 0;
8766 storage_class = sc_none;
8767 }
8768 }
8769 }
8770 else
8771 {
8772 /* It's a variable. */
8773
8774 /* An uninitialized decl with `extern' is a reference. */
8775 decl = grokvardecl (type, unqualified_id,
8776 declspecs,
8777 initialized,
8778 (type_quals & TYPE_QUAL_CONST) != 0,
8779 ctype ? ctype : in_namespace);
8780 bad_specifiers (decl, "variable", virtualp,
8781 memfn_quals != TYPE_UNQUALIFIED,
8782 inlinep, friendp, raises != NULL_TREE);
8783
8784 if (ctype)
8785 {
8786 DECL_CONTEXT (decl) = ctype;
8787 if (staticp == 1)
8788 {
8789 pedwarn ("%<static%> may not be used when defining "
8790 "(as opposed to declaring) a static data member");
8791 staticp = 0;
8792 storage_class = sc_none;
8793 }
8794 if (storage_class == sc_register && TREE_STATIC (decl))
8795 {
8796 error ("static member %qD declared %<register%>", decl);
8797 storage_class = sc_none;
8798 }
8799 if (storage_class == sc_extern && pedantic)
8800 {
8801 pedwarn ("cannot explicitly declare member %q#D to have "
8802 "extern linkage",
8803 decl);
8804 storage_class = sc_none;
8805 }
8806 }
8807 }
8808
8809 /* Record `register' declaration for warnings on &
8810 and in case doing stupid register allocation. */
8811
8812 if (storage_class == sc_register)
8813 DECL_REGISTER (decl) = 1;
8814 else if (storage_class == sc_extern)
8815 DECL_THIS_EXTERN (decl) = 1;
8816 else if (storage_class == sc_static)
8817 DECL_THIS_STATIC (decl) = 1;
8818
8819 /* Record constancy and volatility. There's no need to do this
8820 when processing a template; we'll do this for the instantiated
8821 declaration based on the type of DECL. */
8822 if (!processing_template_decl)
8823 cp_apply_type_quals_to_decl (type_quals, decl);
8824
8825 return decl;
8826 }
8827 }
8828
8829 /* Subroutine of start_function. Ensure that each of the parameter
8830 types (as listed in PARMS) is complete, as is required for a
8831 function definition. */
8832
8833 static void
require_complete_types_for_parms(tree parms)8834 require_complete_types_for_parms (tree parms)
8835 {
8836 for (; parms; parms = TREE_CHAIN (parms))
8837 {
8838 if (dependent_type_p (TREE_TYPE (parms)))
8839 continue;
8840 if (!VOID_TYPE_P (TREE_TYPE (parms))
8841 && complete_type_or_else (TREE_TYPE (parms), parms))
8842 {
8843 relayout_decl (parms);
8844 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
8845 }
8846 else
8847 /* grokparms or complete_type_or_else will have already issued
8848 an error. */
8849 TREE_TYPE (parms) = error_mark_node;
8850 }
8851 }
8852
8853 /* Returns nonzero if T is a local variable. */
8854
8855 int
local_variable_p(tree t)8856 local_variable_p (tree t)
8857 {
8858 if ((TREE_CODE (t) == VAR_DECL
8859 /* A VAR_DECL with a context that is a _TYPE is a static data
8860 member. */
8861 && !TYPE_P (CP_DECL_CONTEXT (t))
8862 /* Any other non-local variable must be at namespace scope. */
8863 && !DECL_NAMESPACE_SCOPE_P (t))
8864 || (TREE_CODE (t) == PARM_DECL))
8865 return 1;
8866
8867 return 0;
8868 }
8869
8870 /* Returns nonzero if T is an automatic local variable or a label.
8871 (These are the declarations that need to be remapped when the code
8872 containing them is duplicated.) */
8873
8874 int
nonstatic_local_decl_p(tree t)8875 nonstatic_local_decl_p (tree t)
8876 {
8877 return ((local_variable_p (t) && !TREE_STATIC (t))
8878 || TREE_CODE (t) == LABEL_DECL
8879 || TREE_CODE (t) == RESULT_DECL);
8880 }
8881
8882 /* Like local_variable_p, but suitable for use as a tree-walking
8883 function. */
8884
8885 static tree
local_variable_p_walkfn(tree * tp,int * walk_subtrees,void * data ATTRIBUTE_UNUSED)8886 local_variable_p_walkfn (tree *tp, int *walk_subtrees,
8887 void *data ATTRIBUTE_UNUSED)
8888 {
8889 if (local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
8890 return *tp;
8891 else if (TYPE_P (*tp))
8892 *walk_subtrees = 0;
8893
8894 return NULL_TREE;
8895 }
8896
8897
8898 /* Check that ARG, which is a default-argument expression for a
8899 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if
8900 something goes wrong. DECL may also be a _TYPE node, rather than a
8901 DECL, if there is no DECL available. */
8902
8903 tree
check_default_argument(tree decl,tree arg)8904 check_default_argument (tree decl, tree arg)
8905 {
8906 tree var;
8907 tree decl_type;
8908
8909 if (TREE_CODE (arg) == DEFAULT_ARG)
8910 /* We get a DEFAULT_ARG when looking at an in-class declaration
8911 with a default argument. Ignore the argument for now; we'll
8912 deal with it after the class is complete. */
8913 return arg;
8914
8915 if (TYPE_P (decl))
8916 {
8917 decl_type = decl;
8918 decl = NULL_TREE;
8919 }
8920 else
8921 decl_type = TREE_TYPE (decl);
8922
8923 if (arg == error_mark_node
8924 || decl == error_mark_node
8925 || TREE_TYPE (arg) == error_mark_node
8926 || decl_type == error_mark_node)
8927 /* Something already went wrong. There's no need to check
8928 further. */
8929 return error_mark_node;
8930
8931 /* [dcl.fct.default]
8932
8933 A default argument expression is implicitly converted to the
8934 parameter type. */
8935 if (!TREE_TYPE (arg)
8936 || !can_convert_arg (decl_type, TREE_TYPE (arg), arg, LOOKUP_NORMAL))
8937 {
8938 if (decl)
8939 error ("default argument for %q#D has type %qT",
8940 decl, TREE_TYPE (arg));
8941 else
8942 error ("default argument for parameter of type %qT has type %qT",
8943 decl_type, TREE_TYPE (arg));
8944
8945 return error_mark_node;
8946 }
8947
8948 /* [dcl.fct.default]
8949
8950 Local variables shall not be used in default argument
8951 expressions.
8952
8953 The keyword `this' shall not be used in a default argument of a
8954 member function. */
8955 var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
8956 NULL);
8957 if (var)
8958 {
8959 error ("default argument %qE uses local variable %qD", arg, var);
8960 return error_mark_node;
8961 }
8962
8963 /* All is well. */
8964 return arg;
8965 }
8966
8967 /* Decode the list of parameter types for a function type.
8968 Given the list of things declared inside the parens,
8969 return a list of types.
8970
8971 If this parameter does not end with an ellipsis, we append
8972 void_list_node.
8973
8974 *PARMS is set to the chain of PARM_DECLs created. */
8975
8976 static tree
grokparms(cp_parameter_declarator * first_parm,tree * parms)8977 grokparms (cp_parameter_declarator *first_parm, tree *parms)
8978 {
8979 tree result = NULL_TREE;
8980 tree decls = NULL_TREE;
8981 int ellipsis = !first_parm || first_parm->ellipsis_p;
8982 cp_parameter_declarator *parm;
8983 int any_error = 0;
8984
8985 for (parm = first_parm; parm != NULL; parm = parm->next)
8986 {
8987 tree type = NULL_TREE;
8988 tree init = parm->default_argument;
8989 tree attrs;
8990 tree decl;
8991
8992 if (parm == no_parameters)
8993 break;
8994
8995 attrs = parm->decl_specifiers.attributes;
8996 parm->decl_specifiers.attributes = NULL_TREE;
8997 decl = grokdeclarator (parm->declarator, &parm->decl_specifiers,
8998 PARM, init != NULL_TREE, &attrs);
8999 if (! decl || TREE_TYPE (decl) == error_mark_node)
9000 continue;
9001
9002 if (attrs)
9003 cplus_decl_attributes (&decl, attrs, 0);
9004
9005 type = TREE_TYPE (decl);
9006 if (VOID_TYPE_P (type))
9007 {
9008 if (same_type_p (type, void_type_node)
9009 && DECL_SELF_REFERENCE_P (type)
9010 && !DECL_NAME (decl) && !result && !parm->next && !ellipsis)
9011 /* this is a parmlist of `(void)', which is ok. */
9012 break;
9013 cxx_incomplete_type_error (decl, type);
9014 /* It's not a good idea to actually create parameters of
9015 type `void'; other parts of the compiler assume that a
9016 void type terminates the parameter list. */
9017 type = error_mark_node;
9018 TREE_TYPE (decl) = error_mark_node;
9019 }
9020
9021 if (type != error_mark_node)
9022 {
9023 /* Top-level qualifiers on the parameters are
9024 ignored for function types. */
9025 type = cp_build_qualified_type (type, 0);
9026 if (TREE_CODE (type) == METHOD_TYPE)
9027 {
9028 error ("parameter %qD invalidly declared method type", decl);
9029 type = build_pointer_type (type);
9030 TREE_TYPE (decl) = type;
9031 }
9032 else if (abstract_virtuals_error (decl, type))
9033 any_error = 1; /* Seems like a good idea. */
9034 else if (POINTER_TYPE_P (type))
9035 {
9036 /* [dcl.fct]/6, parameter types cannot contain pointers
9037 (references) to arrays of unknown bound. */
9038 tree t = TREE_TYPE (type);
9039 int ptr = TYPE_PTR_P (type);
9040
9041 while (1)
9042 {
9043 if (TYPE_PTR_P (t))
9044 ptr = 1;
9045 else if (TREE_CODE (t) != ARRAY_TYPE)
9046 break;
9047 else if (!TYPE_DOMAIN (t))
9048 break;
9049 t = TREE_TYPE (t);
9050 }
9051 if (TREE_CODE (t) == ARRAY_TYPE)
9052 error ("parameter %qD includes %s to array of unknown "
9053 "bound %qT",
9054 decl, ptr ? "pointer" : "reference", t);
9055 }
9056
9057 if (any_error)
9058 init = NULL_TREE;
9059 else if (init && !processing_template_decl)
9060 init = check_default_argument (decl, init);
9061 }
9062
9063 TREE_CHAIN (decl) = decls;
9064 decls = decl;
9065 result = tree_cons (init, type, result);
9066 }
9067 decls = nreverse (decls);
9068 result = nreverse (result);
9069 if (!ellipsis)
9070 result = chainon (result, void_list_node);
9071 *parms = decls;
9072
9073 return result;
9074 }
9075
9076
9077 /* D is a constructor or overloaded `operator='.
9078
9079 Let T be the class in which D is declared. Then, this function
9080 returns:
9081
9082 -1 if D's is an ill-formed constructor or copy assignment operator
9083 whose first parameter is of type `T'.
9084 0 if D is not a copy constructor or copy assignment
9085 operator.
9086 1 if D is a copy constructor or copy assignment operator whose
9087 first parameter is a reference to const qualified T.
9088 2 if D is a copy constructor or copy assignment operator whose
9089 first parameter is a reference to non-const qualified T.
9090
9091 This function can be used as a predicate. Positive values indicate
9092 a copy constructor and nonzero values indicate a copy assignment
9093 operator. */
9094
9095 int
copy_fn_p(tree d)9096 copy_fn_p (tree d)
9097 {
9098 tree args;
9099 tree arg_type;
9100 int result = 1;
9101
9102 gcc_assert (DECL_FUNCTION_MEMBER_P (d));
9103
9104 if (TREE_CODE (d) == TEMPLATE_DECL
9105 || (DECL_TEMPLATE_INFO (d)
9106 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d))))
9107 /* Instantiations of template member functions are never copy
9108 functions. Note that member functions of templated classes are
9109 represented as template functions internally, and we must
9110 accept those as copy functions. */
9111 return 0;
9112
9113 args = FUNCTION_FIRST_USER_PARMTYPE (d);
9114 if (!args)
9115 return 0;
9116
9117 arg_type = TREE_VALUE (args);
9118 if (arg_type == error_mark_node)
9119 return 0;
9120
9121 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
9122 {
9123 /* Pass by value copy assignment operator. */
9124 result = -1;
9125 }
9126 else if (TREE_CODE (arg_type) == REFERENCE_TYPE
9127 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
9128 {
9129 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
9130 result = 2;
9131 }
9132 else
9133 return 0;
9134
9135 args = TREE_CHAIN (args);
9136
9137 if (args && args != void_list_node && !TREE_PURPOSE (args))
9138 /* There are more non-optional args. */
9139 return 0;
9140
9141 return result;
9142 }
9143
9144 /* Remember any special properties of member function DECL. */
9145
grok_special_member_properties(tree decl)9146 void grok_special_member_properties (tree decl)
9147 {
9148 tree class_type;
9149
9150 if (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
9151 return;
9152
9153 class_type = DECL_CONTEXT (decl);
9154 if (DECL_CONSTRUCTOR_P (decl))
9155 {
9156 int ctor = copy_fn_p (decl);
9157
9158 TYPE_HAS_CONSTRUCTOR (class_type) = 1;
9159
9160 if (ctor > 0)
9161 {
9162 /* [class.copy]
9163
9164 A non-template constructor for class X is a copy
9165 constructor if its first parameter is of type X&, const
9166 X&, volatile X& or const volatile X&, and either there
9167 are no other parameters or else all other parameters have
9168 default arguments. */
9169 TYPE_HAS_INIT_REF (class_type) = 1;
9170 if (ctor > 1)
9171 TYPE_HAS_CONST_INIT_REF (class_type) = 1;
9172 }
9173 else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
9174 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1;
9175 }
9176 else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
9177 {
9178 /* [class.copy]
9179
9180 A non-template assignment operator for class X is a copy
9181 assignment operator if its parameter is of type X, X&, const
9182 X&, volatile X& or const volatile X&. */
9183
9184 int assop = copy_fn_p (decl);
9185
9186 if (assop)
9187 {
9188 TYPE_HAS_ASSIGN_REF (class_type) = 1;
9189 if (assop != 1)
9190 TYPE_HAS_CONST_ASSIGN_REF (class_type) = 1;
9191 }
9192 }
9193 }
9194
9195 /* Check a constructor DECL has the correct form. Complains
9196 if the class has a constructor of the form X(X). */
9197
9198 int
grok_ctor_properties(tree ctype,tree decl)9199 grok_ctor_properties (tree ctype, tree decl)
9200 {
9201 int ctor_parm = copy_fn_p (decl);
9202
9203 if (ctor_parm < 0)
9204 {
9205 /* [class.copy]
9206
9207 A declaration of a constructor for a class X is ill-formed if
9208 its first parameter is of type (optionally cv-qualified) X
9209 and either there are no other parameters or else all other
9210 parameters have default arguments.
9211
9212 We *don't* complain about member template instantiations that
9213 have this form, though; they can occur as we try to decide
9214 what constructor to use during overload resolution. Since
9215 overload resolution will never prefer such a constructor to
9216 the non-template copy constructor (which is either explicitly
9217 or implicitly defined), there's no need to worry about their
9218 existence. Theoretically, they should never even be
9219 instantiated, but that's hard to forestall. */
9220 error ("invalid constructor; you probably meant %<%T (const %T&)%>",
9221 ctype, ctype);
9222 return 0;
9223 }
9224
9225 return 1;
9226 }
9227
9228 /* An operator with this code is unary, but can also be binary. */
9229
9230 static int
ambi_op_p(enum tree_code code)9231 ambi_op_p (enum tree_code code)
9232 {
9233 return (code == INDIRECT_REF
9234 || code == ADDR_EXPR
9235 || code == UNARY_PLUS_EXPR
9236 || code == NEGATE_EXPR
9237 || code == PREINCREMENT_EXPR
9238 || code == PREDECREMENT_EXPR);
9239 }
9240
9241 /* An operator with this name can only be unary. */
9242
9243 static int
unary_op_p(enum tree_code code)9244 unary_op_p (enum tree_code code)
9245 {
9246 return (code == TRUTH_NOT_EXPR
9247 || code == BIT_NOT_EXPR
9248 || code == COMPONENT_REF
9249 || code == TYPE_EXPR);
9250 }
9251
9252 /* DECL is a declaration for an overloaded operator. If COMPLAIN is true,
9253 errors are issued for invalid declarations. */
9254
9255 bool
grok_op_properties(tree decl,bool complain)9256 grok_op_properties (tree decl, bool complain)
9257 {
9258 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
9259 tree argtype;
9260 int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
9261 tree name = DECL_NAME (decl);
9262 enum tree_code operator_code;
9263 int arity;
9264 bool ellipsis_p;
9265 tree class_type;
9266
9267 /* Count the number of arguments and check for ellipsis. */
9268 for (argtype = argtypes, arity = 0;
9269 argtype && argtype != void_list_node;
9270 argtype = TREE_CHAIN (argtype))
9271 ++arity;
9272 ellipsis_p = !argtype;
9273
9274 class_type = DECL_CONTEXT (decl);
9275 if (class_type && !CLASS_TYPE_P (class_type))
9276 class_type = NULL_TREE;
9277
9278 if (DECL_CONV_FN_P (decl))
9279 operator_code = TYPE_EXPR;
9280 else
9281 do
9282 {
9283 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P) \
9284 if (ansi_opname (CODE) == name) \
9285 { \
9286 operator_code = (CODE); \
9287 break; \
9288 } \
9289 else if (ansi_assopname (CODE) == name) \
9290 { \
9291 operator_code = (CODE); \
9292 DECL_ASSIGNMENT_OPERATOR_P (decl) = 1; \
9293 break; \
9294 }
9295
9296 #include "operators.def"
9297 #undef DEF_OPERATOR
9298
9299 gcc_unreachable ();
9300 }
9301 while (0);
9302 gcc_assert (operator_code != LAST_CPLUS_TREE_CODE);
9303 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9304
9305 if (class_type)
9306 switch (operator_code)
9307 {
9308 case NEW_EXPR:
9309 TYPE_HAS_NEW_OPERATOR (class_type) = 1;
9310 break;
9311
9312 case DELETE_EXPR:
9313 TYPE_GETS_DELETE (class_type) |= 1;
9314 break;
9315
9316 case VEC_NEW_EXPR:
9317 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1;
9318 break;
9319
9320 case VEC_DELETE_EXPR:
9321 TYPE_GETS_DELETE (class_type) |= 2;
9322 break;
9323
9324 default:
9325 break;
9326 }
9327
9328 /* [basic.std.dynamic.allocation]/1:
9329
9330 A program is ill-formed if an allocation function is declared
9331 in a namespace scope other than global scope or declared static
9332 in global scope.
9333
9334 The same also holds true for deallocation functions. */
9335 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR
9336 || operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9337 {
9338 if (DECL_NAMESPACE_SCOPE_P (decl))
9339 {
9340 if (CP_DECL_CONTEXT (decl) != global_namespace)
9341 {
9342 error ("%qD may not be declared within a namespace", decl);
9343 return false;
9344 }
9345 else if (!TREE_PUBLIC (decl))
9346 {
9347 error ("%qD may not be declared as static", decl);
9348 return false;
9349 }
9350 }
9351 }
9352
9353 if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
9354 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
9355 else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
9356 TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
9357 else
9358 {
9359 /* An operator function must either be a non-static member function
9360 or have at least one parameter of a class, a reference to a class,
9361 an enumeration, or a reference to an enumeration. 13.4.0.6 */
9362 if (! methodp || DECL_STATIC_FUNCTION_P (decl))
9363 {
9364 if (operator_code == TYPE_EXPR
9365 || operator_code == CALL_EXPR
9366 || operator_code == COMPONENT_REF
9367 || operator_code == ARRAY_REF
9368 || operator_code == NOP_EXPR)
9369 {
9370 error ("%qD must be a nonstatic member function", decl);
9371 return false;
9372 }
9373 else
9374 {
9375 tree p;
9376
9377 if (DECL_STATIC_FUNCTION_P (decl))
9378 {
9379 error ("%qD must be either a non-static member "
9380 "function or a non-member function", decl);
9381 return false;
9382 }
9383
9384 for (p = argtypes; p && p != void_list_node; p = TREE_CHAIN (p))
9385 {
9386 tree arg = non_reference (TREE_VALUE (p));
9387 if (arg == error_mark_node)
9388 return false;
9389
9390 /* IS_AGGR_TYPE, rather than CLASS_TYPE_P, is used
9391 because these checks are performed even on
9392 template functions. */
9393 if (IS_AGGR_TYPE (arg) || TREE_CODE (arg) == ENUMERAL_TYPE)
9394 break;
9395 }
9396
9397 if (!p || p == void_list_node)
9398 {
9399 if (complain)
9400 error ("%qD must have an argument of class or "
9401 "enumerated type", decl);
9402 return false;
9403 }
9404 }
9405 }
9406
9407 /* There are no restrictions on the arguments to an overloaded
9408 "operator ()". */
9409 if (operator_code == CALL_EXPR)
9410 return true;
9411
9412 /* Warn about conversion operators that will never be used. */
9413 if (IDENTIFIER_TYPENAME_P (name)
9414 && ! DECL_TEMPLATE_INFO (decl)
9415 && warn_conversion
9416 /* Warn only declaring the function; there is no need to
9417 warn again about out-of-class definitions. */
9418 && class_type == current_class_type)
9419 {
9420 tree t = TREE_TYPE (name);
9421 int ref = (TREE_CODE (t) == REFERENCE_TYPE);
9422 const char *what = 0;
9423
9424 if (ref)
9425 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
9426
9427 if (TREE_CODE (t) == VOID_TYPE)
9428 what = "void";
9429 else if (class_type)
9430 {
9431 if (t == class_type)
9432 what = "the same type";
9433 /* Don't force t to be complete here. */
9434 else if (IS_AGGR_TYPE (t)
9435 && COMPLETE_TYPE_P (t)
9436 && DERIVED_FROM_P (t, class_type))
9437 what = "a base class";
9438 }
9439
9440 if (what)
9441 warning (OPT_Wconversion, "conversion to %s%s will never use a type "
9442 "conversion operator",
9443 ref ? "a reference to " : "", what);
9444 }
9445
9446 if (operator_code == COND_EXPR)
9447 {
9448 /* 13.4.0.3 */
9449 error ("ISO C++ prohibits overloading operator ?:");
9450 return false;
9451 }
9452 else if (ellipsis_p)
9453 {
9454 error ("%qD must not have variable number of arguments", decl);
9455 return false;
9456 }
9457 else if (ambi_op_p (operator_code))
9458 {
9459 if (arity == 1)
9460 /* We pick the one-argument operator codes by default, so
9461 we don't have to change anything. */
9462 ;
9463 else if (arity == 2)
9464 {
9465 /* If we thought this was a unary operator, we now know
9466 it to be a binary operator. */
9467 switch (operator_code)
9468 {
9469 case INDIRECT_REF:
9470 operator_code = MULT_EXPR;
9471 break;
9472
9473 case ADDR_EXPR:
9474 operator_code = BIT_AND_EXPR;
9475 break;
9476
9477 case UNARY_PLUS_EXPR:
9478 operator_code = PLUS_EXPR;
9479 break;
9480
9481 case NEGATE_EXPR:
9482 operator_code = MINUS_EXPR;
9483 break;
9484
9485 case PREINCREMENT_EXPR:
9486 operator_code = POSTINCREMENT_EXPR;
9487 break;
9488
9489 case PREDECREMENT_EXPR:
9490 operator_code = POSTDECREMENT_EXPR;
9491 break;
9492
9493 default:
9494 gcc_unreachable ();
9495 }
9496
9497 SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
9498
9499 if ((operator_code == POSTINCREMENT_EXPR
9500 || operator_code == POSTDECREMENT_EXPR)
9501 && ! processing_template_decl
9502 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
9503 {
9504 if (methodp)
9505 error ("postfix %qD must take %<int%> as its argument",
9506 decl);
9507 else
9508 error ("postfix %qD must take %<int%> as its second "
9509 "argument", decl);
9510 return false;
9511 }
9512 }
9513 else
9514 {
9515 if (methodp)
9516 error ("%qD must take either zero or one argument", decl);
9517 else
9518 error ("%qD must take either one or two arguments", decl);
9519 return false;
9520 }
9521
9522 /* More Effective C++ rule 6. */
9523 if (warn_ecpp
9524 && (operator_code == POSTINCREMENT_EXPR
9525 || operator_code == POSTDECREMENT_EXPR
9526 || operator_code == PREINCREMENT_EXPR
9527 || operator_code == PREDECREMENT_EXPR))
9528 {
9529 tree arg = TREE_VALUE (argtypes);
9530 tree ret = TREE_TYPE (TREE_TYPE (decl));
9531 if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
9532 arg = TREE_TYPE (arg);
9533 arg = TYPE_MAIN_VARIANT (arg);
9534 if (operator_code == PREINCREMENT_EXPR
9535 || operator_code == PREDECREMENT_EXPR)
9536 {
9537 if (TREE_CODE (ret) != REFERENCE_TYPE
9538 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
9539 arg))
9540 warning (OPT_Weffc__, "prefix %qD should return %qT", decl,
9541 build_reference_type (arg));
9542 }
9543 else
9544 {
9545 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
9546 warning (OPT_Weffc__, "postfix %qD should return %qT", decl, arg);
9547 }
9548 }
9549 }
9550 else if (unary_op_p (operator_code))
9551 {
9552 if (arity != 1)
9553 {
9554 if (methodp)
9555 error ("%qD must take %<void%>", decl);
9556 else
9557 error ("%qD must take exactly one argument", decl);
9558 return false;
9559 }
9560 }
9561 else /* if (binary_op_p (operator_code)) */
9562 {
9563 if (arity != 2)
9564 {
9565 if (methodp)
9566 error ("%qD must take exactly one argument", decl);
9567 else
9568 error ("%qD must take exactly two arguments", decl);
9569 return false;
9570 }
9571
9572 /* More Effective C++ rule 7. */
9573 if (warn_ecpp
9574 && (operator_code == TRUTH_ANDIF_EXPR
9575 || operator_code == TRUTH_ORIF_EXPR
9576 || operator_code == COMPOUND_EXPR))
9577 warning (OPT_Weffc__, "user-defined %qD always evaluates both arguments",
9578 decl);
9579 }
9580
9581 /* Effective C++ rule 23. */
9582 if (warn_ecpp
9583 && arity == 2
9584 && !DECL_ASSIGNMENT_OPERATOR_P (decl)
9585 && (operator_code == PLUS_EXPR
9586 || operator_code == MINUS_EXPR
9587 || operator_code == TRUNC_DIV_EXPR
9588 || operator_code == MULT_EXPR
9589 || operator_code == TRUNC_MOD_EXPR)
9590 && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
9591 warning (OPT_Weffc__, "%qD should return by value", decl);
9592
9593 /* [over.oper]/8 */
9594 for (; argtypes && argtypes != void_list_node;
9595 argtypes = TREE_CHAIN (argtypes))
9596 if (TREE_PURPOSE (argtypes))
9597 {
9598 TREE_PURPOSE (argtypes) = NULL_TREE;
9599 if (operator_code == POSTINCREMENT_EXPR
9600 || operator_code == POSTDECREMENT_EXPR)
9601 {
9602 if (pedantic)
9603 pedwarn ("%qD cannot have default arguments", decl);
9604 }
9605 else
9606 {
9607 error ("%qD cannot have default arguments", decl);
9608 return false;
9609 }
9610 }
9611 }
9612 return true;
9613 }
9614
9615 /* Return a string giving the keyword associate with CODE. */
9616
9617 static const char *
tag_name(enum tag_types code)9618 tag_name (enum tag_types code)
9619 {
9620 switch (code)
9621 {
9622 case record_type:
9623 return "struct";
9624 case class_type:
9625 return "class";
9626 case union_type:
9627 return "union";
9628 case enum_type:
9629 return "enum";
9630 case typename_type:
9631 return "typename";
9632 default:
9633 gcc_unreachable ();
9634 }
9635 }
9636
9637 /* Name lookup in an elaborated-type-specifier (after the keyword
9638 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the
9639 elaborated-type-specifier is invalid, issue a diagnostic and return
9640 error_mark_node; otherwise, return the *_TYPE to which it referred.
9641 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */
9642
9643 tree
check_elaborated_type_specifier(enum tag_types tag_code,tree decl,bool allow_template_p)9644 check_elaborated_type_specifier (enum tag_types tag_code,
9645 tree decl,
9646 bool allow_template_p)
9647 {
9648 tree type;
9649
9650 /* In the case of:
9651
9652 struct S { struct S *p; };
9653
9654 name lookup will find the TYPE_DECL for the implicit "S::S"
9655 typedef. Adjust for that here. */
9656 if (DECL_SELF_REFERENCE_P (decl))
9657 decl = TYPE_NAME (TREE_TYPE (decl));
9658
9659 type = TREE_TYPE (decl);
9660
9661 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P
9662 is false for this case as well. */
9663 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
9664 {
9665 error ("using template type parameter %qT after %qs",
9666 type, tag_name (tag_code));
9667 return error_mark_node;
9668 }
9669 /* [dcl.type.elab]
9670
9671 If the identifier resolves to a typedef-name or a template
9672 type-parameter, the elaborated-type-specifier is ill-formed.
9673
9674 In other words, the only legitimate declaration to use in the
9675 elaborated type specifier is the implicit typedef created when
9676 the type is declared. */
9677 else if (!DECL_IMPLICIT_TYPEDEF_P (decl)
9678 && tag_code != typename_type)
9679 {
9680 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code));
9681 error ("%q+D has a previous declaration here", decl);
9682 return error_mark_node;
9683 }
9684 else if (TREE_CODE (type) != RECORD_TYPE
9685 && TREE_CODE (type) != UNION_TYPE
9686 && tag_code != enum_type
9687 && tag_code != typename_type)
9688 {
9689 error ("%qT referred to as %qs", type, tag_name (tag_code));
9690 error ("%q+T has a previous declaration here", type);
9691 return error_mark_node;
9692 }
9693 else if (TREE_CODE (type) != ENUMERAL_TYPE
9694 && tag_code == enum_type)
9695 {
9696 error ("%qT referred to as enum", type);
9697 error ("%q+T has a previous declaration here", type);
9698 return error_mark_node;
9699 }
9700 else if (!allow_template_p
9701 && TREE_CODE (type) == RECORD_TYPE
9702 && CLASSTYPE_IS_TEMPLATE (type))
9703 {
9704 /* If a class template appears as elaborated type specifier
9705 without a template header such as:
9706
9707 template <class T> class C {};
9708 void f(class C); // No template header here
9709
9710 then the required template argument is missing. */
9711 error ("template argument required for %<%s %T%>",
9712 tag_name (tag_code),
9713 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)));
9714 return error_mark_node;
9715 }
9716
9717 return type;
9718 }
9719
9720 /* Lookup NAME in elaborate type specifier in scope according to
9721 SCOPE and issue diagnostics if necessary.
9722 Return *_TYPE node upon success, NULL_TREE when the NAME is not
9723 found, and ERROR_MARK_NODE for type error. */
9724
9725 static tree
lookup_and_check_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)9726 lookup_and_check_tag (enum tag_types tag_code, tree name,
9727 tag_scope scope, bool template_header_p)
9728 {
9729 tree t;
9730 tree decl;
9731 if (scope == ts_global)
9732 {
9733 /* First try ordinary name lookup, ignoring hidden class name
9734 injected via friend declaration. */
9735 decl = lookup_name_prefer_type (name, 2);
9736 /* If that fails, the name will be placed in the smallest
9737 non-class, non-function-prototype scope according to 3.3.1/5.
9738 We may already have a hidden name declared as friend in this
9739 scope. So lookup again but not ignoring hidden names.
9740 If we find one, that name will be made visible rather than
9741 creating a new tag. */
9742 if (!decl)
9743 decl = lookup_type_scope (name, ts_within_enclosing_non_class);
9744 }
9745 else
9746 decl = lookup_type_scope (name, scope);
9747
9748 if (decl && DECL_CLASS_TEMPLATE_P (decl))
9749 decl = DECL_TEMPLATE_RESULT (decl);
9750
9751 if (decl && TREE_CODE (decl) == TYPE_DECL)
9752 {
9753 /* Look for invalid nested type:
9754 class C {
9755 class C {};
9756 }; */
9757 if (scope == ts_current && DECL_SELF_REFERENCE_P (decl))
9758 {
9759 error ("%qD has the same name as the class in which it is "
9760 "declared",
9761 decl);
9762 return error_mark_node;
9763 }
9764
9765 /* Two cases we need to consider when deciding if a class
9766 template is allowed as an elaborated type specifier:
9767 1. It is a self reference to its own class.
9768 2. It comes with a template header.
9769
9770 For example:
9771
9772 template <class T> class C {
9773 class C *c1; // DECL_SELF_REFERENCE_P is true
9774 class D;
9775 };
9776 template <class U> class C; // template_header_p is true
9777 template <class T> class C<T>::D {
9778 class C *c2; // DECL_SELF_REFERENCE_P is true
9779 }; */
9780
9781 t = check_elaborated_type_specifier (tag_code,
9782 decl,
9783 template_header_p
9784 | DECL_SELF_REFERENCE_P (decl));
9785 return t;
9786 }
9787 else if (decl && TREE_CODE (decl) == TREE_LIST)
9788 {
9789 error ("reference to %qD is ambiguous", name);
9790 print_candidates (decl);
9791 return error_mark_node;
9792 }
9793 else
9794 return NULL_TREE;
9795 }
9796
9797 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME.
9798 Define the tag as a forward-reference if it is not defined.
9799
9800 If a declaration is given, process it here, and report an error if
9801 multiple declarations are not identical.
9802
9803 SCOPE is TS_CURRENT when this is also a definition. Only look in
9804 the current frame for the name (since C++ allows new names in any
9805 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend
9806 declaration. Only look beginning from the current scope outward up
9807 till the nearest non-class scope. Otherwise it is TS_GLOBAL.
9808
9809 TEMPLATE_HEADER_P is true when this declaration is preceded by
9810 a set of template parameters. */
9811
9812 tree
xref_tag(enum tag_types tag_code,tree name,tag_scope scope,bool template_header_p)9813 xref_tag (enum tag_types tag_code, tree name,
9814 tag_scope scope, bool template_header_p)
9815 {
9816 enum tree_code code;
9817 tree t;
9818 tree context = NULL_TREE;
9819
9820 timevar_push (TV_NAME_LOOKUP);
9821
9822 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
9823
9824 switch (tag_code)
9825 {
9826 case record_type:
9827 case class_type:
9828 code = RECORD_TYPE;
9829 break;
9830 case union_type:
9831 code = UNION_TYPE;
9832 break;
9833 case enum_type:
9834 code = ENUMERAL_TYPE;
9835 break;
9836 default:
9837 gcc_unreachable ();
9838 }
9839
9840 /* In case of anonymous name, xref_tag is only called to
9841 make type node and push name. Name lookup is not required. */
9842 if (ANON_AGGRNAME_P (name))
9843 t = NULL_TREE;
9844 else
9845 t = lookup_and_check_tag (tag_code, name,
9846 scope, template_header_p);
9847
9848 if (t == error_mark_node)
9849 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9850
9851 if (scope != ts_current && t && current_class_type
9852 && template_class_depth (current_class_type)
9853 && template_header_p)
9854 {
9855 /* Since SCOPE is not TS_CURRENT, we are not looking at a
9856 definition of this tag. Since, in addition, we are currently
9857 processing a (member) template declaration of a template
9858 class, we must be very careful; consider:
9859
9860 template <class X>
9861 struct S1
9862
9863 template <class U>
9864 struct S2
9865 { template <class V>
9866 friend struct S1; };
9867
9868 Here, the S2::S1 declaration should not be confused with the
9869 outer declaration. In particular, the inner version should
9870 have a template parameter of level 2, not level 1. This
9871 would be particularly important if the member declaration
9872 were instead:
9873
9874 template <class V = U> friend struct S1;
9875
9876 say, when we should tsubst into `U' when instantiating
9877 S2. On the other hand, when presented with:
9878
9879 template <class T>
9880 struct S1 {
9881 template <class U>
9882 struct S2 {};
9883 template <class U>
9884 friend struct S2;
9885 };
9886
9887 we must find the inner binding eventually. We
9888 accomplish this by making sure that the new type we
9889 create to represent this declaration has the right
9890 TYPE_CONTEXT. */
9891 context = TYPE_CONTEXT (t);
9892 t = NULL_TREE;
9893 }
9894
9895 if (! t)
9896 {
9897 /* If no such tag is yet defined, create a forward-reference node
9898 and record it as the "definition".
9899 When a real declaration of this type is found,
9900 the forward-reference will be altered into a real type. */
9901 if (code == ENUMERAL_TYPE)
9902 {
9903 error ("use of enum %q#D without previous declaration", name);
9904 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9905 }
9906 else
9907 {
9908 t = make_aggr_type (code);
9909 TYPE_CONTEXT (t) = context;
9910 t = pushtag (name, t, scope);
9911 }
9912 }
9913 else
9914 {
9915 if (template_header_p && IS_AGGR_TYPE (t))
9916 {
9917 if (!redeclare_class_template (t, current_template_parms))
9918 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9919 }
9920 else if (!processing_template_decl
9921 && CLASS_TYPE_P (t)
9922 && CLASSTYPE_IS_TEMPLATE (t))
9923 {
9924 error ("redeclaration of %qT as a non-template", t);
9925 error ("previous declaration %q+D", t);
9926 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
9927 }
9928
9929 /* Make injected friend class visible. */
9930 if (scope != ts_within_enclosing_non_class
9931 && hidden_name_p (TYPE_NAME (t)))
9932 {
9933 DECL_ANTICIPATED (TYPE_NAME (t)) = 0;
9934 DECL_FRIEND_P (TYPE_NAME (t)) = 0;
9935
9936 if (TYPE_TEMPLATE_INFO (t))
9937 {
9938 DECL_ANTICIPATED (TYPE_TI_TEMPLATE (t)) = 0;
9939 DECL_FRIEND_P (TYPE_TI_TEMPLATE (t)) = 0;
9940 }
9941 }
9942 }
9943
9944 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
9945 }
9946
9947 tree
xref_tag_from_type(tree old,tree id,tag_scope scope)9948 xref_tag_from_type (tree old, tree id, tag_scope scope)
9949 {
9950 enum tag_types tag_kind;
9951
9952 if (TREE_CODE (old) == RECORD_TYPE)
9953 tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
9954 else
9955 tag_kind = union_type;
9956
9957 if (id == NULL_TREE)
9958 id = TYPE_IDENTIFIER (old);
9959
9960 return xref_tag (tag_kind, id, scope, false);
9961 }
9962
9963 /* Create the binfo hierarchy for REF with (possibly NULL) base list
9964 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an
9965 access_* node, and the TREE_VALUE is the type of the base-class.
9966 Non-NULL TREE_TYPE indicates virtual inheritance.
9967
9968 Returns true if the binfo heirarchy was successfully created,
9969 false if an error was detected. */
9970
9971 bool
xref_basetypes(tree ref,tree base_list)9972 xref_basetypes (tree ref, tree base_list)
9973 {
9974 tree *basep;
9975 tree binfo, base_binfo;
9976 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */
9977 unsigned max_bases = 0; /* Maximum direct bases. */
9978 int i;
9979 tree default_access;
9980 tree igo_prev; /* Track Inheritance Graph Order. */
9981
9982 if (ref == error_mark_node)
9983 return false;
9984
9985 /* The base of a derived class is private by default, all others are
9986 public. */
9987 default_access = (TREE_CODE (ref) == RECORD_TYPE
9988 && CLASSTYPE_DECLARED_CLASS (ref)
9989 ? access_private_node : access_public_node);
9990
9991 /* First, make sure that any templates in base-classes are
9992 instantiated. This ensures that if we call ourselves recursively
9993 we do not get confused about which classes are marked and which
9994 are not. */
9995 basep = &base_list;
9996 while (*basep)
9997 {
9998 tree basetype = TREE_VALUE (*basep);
9999
10000 if (!(processing_template_decl && uses_template_parms (basetype))
10001 && !complete_type_or_else (basetype, NULL))
10002 /* An incomplete type. Remove it from the list. */
10003 *basep = TREE_CHAIN (*basep);
10004 else
10005 {
10006 max_bases++;
10007 if (TREE_TYPE (*basep))
10008 max_vbases++;
10009 if (CLASS_TYPE_P (basetype))
10010 max_vbases += VEC_length (tree, CLASSTYPE_VBASECLASSES (basetype));
10011 basep = &TREE_CHAIN (*basep);
10012 }
10013 }
10014
10015 TYPE_MARKED_P (ref) = 1;
10016
10017 /* The binfo slot should be empty, unless this is an (ill-formed)
10018 redefinition. */
10019 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref));
10020 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref);
10021
10022 binfo = make_tree_binfo (max_bases);
10023
10024 TYPE_BINFO (ref) = binfo;
10025 BINFO_OFFSET (binfo) = size_zero_node;
10026 BINFO_TYPE (binfo) = ref;
10027
10028 if (max_bases)
10029 {
10030 BINFO_BASE_ACCESSES (binfo) = VEC_alloc (tree, gc, max_bases);
10031 /* An aggregate cannot have baseclasses. */
10032 CLASSTYPE_NON_AGGREGATE (ref) = 1;
10033
10034 if (TREE_CODE (ref) == UNION_TYPE)
10035 {
10036 error ("derived union %qT invalid", ref);
10037 return false;
10038 }
10039 }
10040
10041 if (max_bases > 1)
10042 {
10043 if (TYPE_FOR_JAVA (ref))
10044 {
10045 error ("Java class %qT cannot have multiple bases", ref);
10046 return false;
10047 }
10048 }
10049
10050 if (max_vbases)
10051 {
10052 CLASSTYPE_VBASECLASSES (ref) = VEC_alloc (tree, gc, max_vbases);
10053
10054 if (TYPE_FOR_JAVA (ref))
10055 {
10056 error ("Java class %qT cannot have virtual bases", ref);
10057 return false;
10058 }
10059 }
10060
10061 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list))
10062 {
10063 tree access = TREE_PURPOSE (base_list);
10064 int via_virtual = TREE_TYPE (base_list) != NULL_TREE;
10065 tree basetype = TREE_VALUE (base_list);
10066
10067 if (access == access_default_node)
10068 access = default_access;
10069
10070 if (TREE_CODE (basetype) == TYPE_DECL)
10071 basetype = TREE_TYPE (basetype);
10072 if (TREE_CODE (basetype) != RECORD_TYPE
10073 && TREE_CODE (basetype) != TYPENAME_TYPE
10074 && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
10075 && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM)
10076 {
10077 error ("base type %qT fails to be a struct or class type",
10078 basetype);
10079 return false;
10080 }
10081
10082 if (TYPE_FOR_JAVA (basetype) && (current_lang_depth () == 0))
10083 TYPE_FOR_JAVA (ref) = 1;
10084
10085 base_binfo = NULL_TREE;
10086 if (CLASS_TYPE_P (basetype) && !dependent_type_p (basetype))
10087 {
10088 base_binfo = TYPE_BINFO (basetype);
10089 /* The original basetype could have been a typedef'd type. */
10090 basetype = BINFO_TYPE (base_binfo);
10091
10092 /* Inherit flags from the base. */
10093 TYPE_HAS_NEW_OPERATOR (ref)
10094 |= TYPE_HAS_NEW_OPERATOR (basetype);
10095 TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
10096 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
10097 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
10098 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype);
10099 CLASSTYPE_DIAMOND_SHAPED_P (ref)
10100 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype);
10101 CLASSTYPE_REPEATED_BASE_P (ref)
10102 |= CLASSTYPE_REPEATED_BASE_P (basetype);
10103 }
10104
10105 /* We must do this test after we've seen through a typedef
10106 type. */
10107 if (TYPE_MARKED_P (basetype))
10108 {
10109 if (basetype == ref)
10110 error ("recursive type %qT undefined", basetype);
10111 else
10112 error ("duplicate base type %qT invalid", basetype);
10113 return false;
10114 }
10115 TYPE_MARKED_P (basetype) = 1;
10116
10117 base_binfo = copy_binfo (base_binfo, basetype, ref,
10118 &igo_prev, via_virtual);
10119 if (!BINFO_INHERITANCE_CHAIN (base_binfo))
10120 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo;
10121
10122 BINFO_BASE_APPEND (binfo, base_binfo);
10123 BINFO_BASE_ACCESS_APPEND (binfo, access);
10124 }
10125
10126 if (VEC_space (tree, CLASSTYPE_VBASECLASSES (ref), 1))
10127 /* If we have space in the vbase vector, we must have shared at
10128 least one of them, and are therefore diamond shaped. */
10129 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1;
10130
10131 /* Unmark all the types. */
10132 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
10133 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10134 TYPE_MARKED_P (ref) = 0;
10135
10136 /* Now see if we have a repeated base type. */
10137 if (!CLASSTYPE_REPEATED_BASE_P (ref))
10138 {
10139 for (base_binfo = binfo; base_binfo;
10140 base_binfo = TREE_CHAIN (base_binfo))
10141 {
10142 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10143 {
10144 CLASSTYPE_REPEATED_BASE_P (ref) = 1;
10145 break;
10146 }
10147 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1;
10148 }
10149 for (base_binfo = binfo; base_binfo;
10150 base_binfo = TREE_CHAIN (base_binfo))
10151 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo)))
10152 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0;
10153 else
10154 break;
10155 }
10156
10157 return true;
10158 }
10159
10160
10161 /* Begin compiling the definition of an enumeration type.
10162 NAME is its name.
10163 Returns the type object, as yet incomplete.
10164 Also records info about it so that build_enumerator
10165 may be used to declare the individual values as they are read. */
10166
10167 tree
start_enum(tree name)10168 start_enum (tree name)
10169 {
10170 tree enumtype;
10171
10172 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
10173
10174 /* If this is the real definition for a previous forward reference,
10175 fill in the contents in the same object that used to be the
10176 forward reference. */
10177
10178 enumtype = lookup_and_check_tag (enum_type, name,
10179 /*tag_scope=*/ts_current,
10180 /*template_header_p=*/false);
10181
10182 if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
10183 {
10184 error ("multiple definition of %q#T", enumtype);
10185 error ("%Jprevious definition here", TYPE_MAIN_DECL (enumtype));
10186 /* Clear out TYPE_VALUES, and start again. */
10187 TYPE_VALUES (enumtype) = NULL_TREE;
10188 }
10189 else
10190 {
10191 /* In case of error, make a dummy enum to allow parsing to
10192 continue. */
10193 if (enumtype == error_mark_node)
10194 name = make_anon_name ();
10195
10196 enumtype = make_node (ENUMERAL_TYPE);
10197 enumtype = pushtag (name, enumtype, /*tag_scope=*/ts_current);
10198 }
10199
10200 return enumtype;
10201 }
10202
10203 /* After processing and defining all the values of an enumeration type,
10204 install their decls in the enumeration type and finish it off.
10205 ENUMTYPE is the type object and VALUES a list of name-value pairs. */
10206
10207 void
finish_enum(tree enumtype)10208 finish_enum (tree enumtype)
10209 {
10210 tree values;
10211 tree decl;
10212 tree value;
10213 tree minnode;
10214 tree maxnode;
10215 tree t;
10216 bool unsignedp;
10217 bool use_short_enum;
10218 int lowprec;
10219 int highprec;
10220 int precision;
10221 integer_type_kind itk;
10222 tree underlying_type = NULL_TREE;
10223
10224 /* We built up the VALUES in reverse order. */
10225 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
10226
10227 /* For an enum defined in a template, just set the type of the values;
10228 all further processing is postponed until the template is
10229 instantiated. We need to set the type so that tsubst of a CONST_DECL
10230 works. */
10231 if (processing_template_decl)
10232 {
10233 for (values = TYPE_VALUES (enumtype);
10234 values;
10235 values = TREE_CHAIN (values))
10236 TREE_TYPE (TREE_VALUE (values)) = enumtype;
10237 if (at_function_scope_p ())
10238 add_stmt (build_min (TAG_DEFN, enumtype));
10239 return;
10240 }
10241
10242 /* Determine the minimum and maximum values of the enumerators. */
10243 if (TYPE_VALUES (enumtype))
10244 {
10245 minnode = maxnode = NULL_TREE;
10246
10247 for (values = TYPE_VALUES (enumtype);
10248 values;
10249 values = TREE_CHAIN (values))
10250 {
10251 decl = TREE_VALUE (values);
10252
10253 /* [dcl.enum]: Following the closing brace of an enum-specifier,
10254 each enumerator has the type of its enumeration. Prior to the
10255 closing brace, the type of each enumerator is the type of its
10256 initializing value. */
10257 TREE_TYPE (decl) = enumtype;
10258
10259 /* Update the minimum and maximum values, if appropriate. */
10260 value = DECL_INITIAL (decl);
10261 if (value == error_mark_node)
10262 value = integer_zero_node;
10263 /* Figure out what the minimum and maximum values of the
10264 enumerators are. */
10265 if (!minnode)
10266 minnode = maxnode = value;
10267 else if (tree_int_cst_lt (maxnode, value))
10268 maxnode = value;
10269 else if (tree_int_cst_lt (value, minnode))
10270 minnode = value;
10271 }
10272 }
10273 else
10274 /* [dcl.enum]
10275
10276 If the enumerator-list is empty, the underlying type is as if
10277 the enumeration had a single enumerator with value 0. */
10278 minnode = maxnode = integer_zero_node;
10279
10280 /* Compute the number of bits require to represent all values of the
10281 enumeration. We must do this before the type of MINNODE and
10282 MAXNODE are transformed, since min_precision relies on the
10283 TREE_TYPE of the value it is passed. */
10284 unsignedp = tree_int_cst_sgn (minnode) >= 0;
10285 lowprec = min_precision (minnode, unsignedp);
10286 highprec = min_precision (maxnode, unsignedp);
10287 precision = MAX (lowprec, highprec);
10288
10289 /* Determine the underlying type of the enumeration.
10290
10291 [dcl.enum]
10292
10293 The underlying type of an enumeration is an integral type that
10294 can represent all the enumerator values defined in the
10295 enumeration. It is implementation-defined which integral type is
10296 used as the underlying type for an enumeration except that the
10297 underlying type shall not be larger than int unless the value of
10298 an enumerator cannot fit in an int or unsigned int.
10299
10300 We use "int" or an "unsigned int" as the underlying type, even if
10301 a smaller integral type would work, unless the user has
10302 explicitly requested that we use the smallest possible type. The
10303 user can request that for all enumerations with a command line
10304 flag, or for just one enumeration with an attribute. */
10305
10306 use_short_enum = flag_short_enums
10307 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype));
10308
10309 for (itk = (use_short_enum ? itk_char : itk_int);
10310 itk != itk_none;
10311 itk++)
10312 {
10313 underlying_type = integer_types[itk];
10314 if (TYPE_PRECISION (underlying_type) >= precision
10315 && TYPE_UNSIGNED (underlying_type) == unsignedp)
10316 break;
10317 }
10318 if (itk == itk_none)
10319 {
10320 /* DR 377
10321
10322 IF no integral type can represent all the enumerator values, the
10323 enumeration is ill-formed. */
10324 error ("no integral type can represent all of the enumerator values "
10325 "for %qT", enumtype);
10326 precision = TYPE_PRECISION (long_long_integer_type_node);
10327 underlying_type = integer_types[itk_unsigned_long_long];
10328 }
10329
10330 /* Compute the minium and maximum values for the type.
10331
10332 [dcl.enum]
10333
10334 For an enumeration where emin is the smallest enumerator and emax
10335 is the largest, the values of the enumeration are the values of the
10336 underlying type in the range bmin to bmax, where bmin and bmax are,
10337 respectively, the smallest and largest values of the smallest bit-
10338 field that can store emin and emax. */
10339
10340 /* The middle-end currently assumes that types with TYPE_PRECISION
10341 narrower than their underlying type are suitably zero or sign
10342 extended to fill their mode. g++ doesn't make these guarantees.
10343 Until the middle-end can represent such paradoxical types, we
10344 set the TYPE_PRECISION to the width of the underlying type. */
10345 TYPE_PRECISION (enumtype) = TYPE_PRECISION (underlying_type);
10346
10347 set_min_and_max_values_for_integral_type (enumtype, precision, unsignedp);
10348
10349 /* [dcl.enum]
10350
10351 The value of sizeof() applied to an enumeration type, an object
10352 of an enumeration type, or an enumerator, is the value of sizeof()
10353 applied to the underlying type. */
10354 TYPE_SIZE (enumtype) = TYPE_SIZE (underlying_type);
10355 TYPE_SIZE_UNIT (enumtype) = TYPE_SIZE_UNIT (underlying_type);
10356 TYPE_MODE (enumtype) = TYPE_MODE (underlying_type);
10357 TYPE_ALIGN (enumtype) = TYPE_ALIGN (underlying_type);
10358 TYPE_USER_ALIGN (enumtype) = TYPE_USER_ALIGN (underlying_type);
10359 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (underlying_type);
10360
10361 /* Convert each of the enumerators to the type of the underlying
10362 type of the enumeration. */
10363 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
10364 {
10365 location_t saved_location;
10366
10367 decl = TREE_VALUE (values);
10368 saved_location = input_location;
10369 input_location = DECL_SOURCE_LOCATION (decl);
10370 value = perform_implicit_conversion (underlying_type,
10371 DECL_INITIAL (decl));
10372 input_location = saved_location;
10373
10374 /* Do not clobber shared ints. */
10375 value = copy_node (value);
10376
10377 TREE_TYPE (value) = enumtype;
10378 DECL_INITIAL (decl) = value;
10379 TREE_VALUE (values) = value;
10380 }
10381
10382 /* Fix up all variant types of this enum type. */
10383 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
10384 {
10385 TYPE_VALUES (t) = TYPE_VALUES (enumtype);
10386 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
10387 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
10388 TYPE_SIZE (t) = TYPE_SIZE (enumtype);
10389 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
10390 TYPE_MODE (t) = TYPE_MODE (enumtype);
10391 TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
10392 TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
10393 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
10394 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (enumtype);
10395 }
10396
10397 /* Finish debugging output for this type. */
10398 rest_of_type_compilation (enumtype, namespace_bindings_p ());
10399 }
10400
10401 /* Build and install a CONST_DECL for an enumeration constant of the
10402 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
10403 Assignment of sequential values by default is handled here. */
10404
10405 void
build_enumerator(tree name,tree value,tree enumtype)10406 build_enumerator (tree name, tree value, tree enumtype)
10407 {
10408 tree decl;
10409 tree context;
10410 tree type;
10411
10412 /* If the VALUE was erroneous, pretend it wasn't there; that will
10413 result in the enum being assigned the next value in sequence. */
10414 if (value == error_mark_node)
10415 value = NULL_TREE;
10416
10417 /* Remove no-op casts from the value. */
10418 if (value)
10419 STRIP_TYPE_NOPS (value);
10420
10421 if (! processing_template_decl)
10422 {
10423 /* Validate and default VALUE. */
10424 if (value != NULL_TREE)
10425 {
10426 value = integral_constant_value (value);
10427
10428 if (TREE_CODE (value) == INTEGER_CST)
10429 {
10430 value = perform_integral_promotions (value);
10431 constant_expression_warning (value);
10432 }
10433 else
10434 {
10435 error ("enumerator value for %qD not integer constant", name);
10436 value = NULL_TREE;
10437 }
10438 }
10439
10440 /* Default based on previous value. */
10441 if (value == NULL_TREE)
10442 {
10443 if (TYPE_VALUES (enumtype))
10444 {
10445 HOST_WIDE_INT hi;
10446 unsigned HOST_WIDE_INT lo;
10447 tree prev_value;
10448 bool overflowed;
10449
10450 /* The next value is the previous value plus one. We can
10451 safely assume that the previous value is an INTEGER_CST.
10452 add_double doesn't know the type of the target expression,
10453 so we must check with int_fits_type_p as well. */
10454 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
10455 overflowed = add_double (TREE_INT_CST_LOW (prev_value),
10456 TREE_INT_CST_HIGH (prev_value),
10457 1, 0, &lo, &hi);
10458 value = build_int_cst_wide (TREE_TYPE (prev_value), lo, hi);
10459 overflowed |= !int_fits_type_p (value, TREE_TYPE (prev_value));
10460
10461 if (overflowed)
10462 {
10463 error ("overflow in enumeration values at %qD", name);
10464 value = error_mark_node;
10465 }
10466 }
10467 else
10468 value = integer_zero_node;
10469 }
10470
10471 /* Remove no-op casts from the value. */
10472 STRIP_TYPE_NOPS (value);
10473 }
10474
10475 /* C++ associates enums with global, function, or class declarations. */
10476 context = current_scope ();
10477
10478 /* Build the actual enumeration constant. Note that the enumeration
10479 constants have the type of their initializers until the
10480 enumeration is complete:
10481
10482 [ dcl.enum ]
10483
10484 Following the closing brace of an enum-specifier, each enumer-
10485 ator has the type of its enumeration. Prior to the closing
10486 brace, the type of each enumerator is the type of its
10487 initializing value.
10488
10489 In finish_enum we will reset the type. Of course, if we're
10490 processing a template, there may be no value. */
10491 type = value ? TREE_TYPE (value) : NULL_TREE;
10492
10493 if (context && context == current_class_type)
10494 /* This enum declaration is local to the class. We need the full
10495 lang_decl so that we can record DECL_CLASS_CONTEXT, for example. */
10496 decl = build_lang_decl (CONST_DECL, name, type);
10497 else
10498 /* It's a global enum, or it's local to a function. (Note local to
10499 a function could mean local to a class method. */
10500 decl = build_decl (CONST_DECL, name, type);
10501
10502 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
10503 TREE_CONSTANT (decl) = 1;
10504 TREE_INVARIANT (decl) = 1;
10505 TREE_READONLY (decl) = 1;
10506 DECL_INITIAL (decl) = value;
10507
10508 if (context && context == current_class_type)
10509 /* In something like `struct S { enum E { i = 7 }; };' we put `i'
10510 on the TYPE_FIELDS list for `S'. (That's so that you can say
10511 things like `S::i' later.) */
10512 finish_member_declaration (decl);
10513 else
10514 pushdecl (decl);
10515
10516 /* Add this enumeration constant to the list for this type. */
10517 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
10518 }
10519
10520
10521 /* We're defining DECL. Make sure that it's type is OK. */
10522
10523 static void
check_function_type(tree decl,tree current_function_parms)10524 check_function_type (tree decl, tree current_function_parms)
10525 {
10526 tree fntype = TREE_TYPE (decl);
10527 tree return_type = complete_type (TREE_TYPE (fntype));
10528
10529 /* In a function definition, arg types must be complete. */
10530 require_complete_types_for_parms (current_function_parms);
10531
10532 if (dependent_type_p (return_type))
10533 return;
10534 if (!COMPLETE_OR_VOID_TYPE_P (return_type))
10535 {
10536 tree args = TYPE_ARG_TYPES (fntype);
10537
10538 error ("return type %q#T is incomplete", return_type);
10539
10540 /* Make it return void instead. */
10541 if (TREE_CODE (fntype) == METHOD_TYPE)
10542 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)),
10543 void_type_node,
10544 TREE_CHAIN (args));
10545 else
10546 fntype = build_function_type (void_type_node, args);
10547 TREE_TYPE (decl)
10548 = build_exception_variant (fntype,
10549 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)));
10550 }
10551 else
10552 abstract_virtuals_error (decl, TREE_TYPE (fntype));
10553 }
10554
10555 /* Create the FUNCTION_DECL for a function definition.
10556 DECLSPECS and DECLARATOR are the parts of the declaration;
10557 they describe the function's name and the type it returns,
10558 but twisted together in a fashion that parallels the syntax of C.
10559
10560 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
10561 DECLARATOR is really the DECL for the function we are about to
10562 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
10563 indicating that the function is an inline defined in-class.
10564
10565 This function creates a binding context for the function body
10566 as well as setting up the FUNCTION_DECL in current_function_decl.
10567
10568 For C++, we must first check whether that datum makes any sense.
10569 For example, "class A local_a(1,2);" means that variable local_a
10570 is an aggregate of type A, which should have a constructor
10571 applied to it with the argument list [1, 2].
10572
10573 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node,
10574 or may be a BLOCK if the function has been defined previously
10575 in this translation unit. On exit, DECL_INITIAL (decl1) will be
10576 error_mark_node if the function has never been defined, or
10577 a BLOCK if the function has been defined somewhere. */
10578
10579 void
start_preparsed_function(tree decl1,tree attrs,int flags)10580 start_preparsed_function (tree decl1, tree attrs, int flags)
10581 {
10582 tree ctype = NULL_TREE;
10583 tree fntype;
10584 tree restype;
10585 int doing_friend = 0;
10586 struct cp_binding_level *bl;
10587 tree current_function_parms;
10588 struct c_fileinfo *finfo
10589 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1)));
10590 bool honor_interface;
10591
10592 /* Sanity check. */
10593 gcc_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE);
10594 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE);
10595
10596 fntype = TREE_TYPE (decl1);
10597 if (TREE_CODE (fntype) == METHOD_TYPE)
10598 ctype = TYPE_METHOD_BASETYPE (fntype);
10599
10600 /* ISO C++ 11.4/5. A friend function defined in a class is in
10601 the (lexical) scope of the class in which it is defined. */
10602 if (!ctype && DECL_FRIEND_P (decl1))
10603 {
10604 ctype = DECL_FRIEND_CONTEXT (decl1);
10605
10606 /* CTYPE could be null here if we're dealing with a template;
10607 for example, `inline friend float foo()' inside a template
10608 will have no CTYPE set. */
10609 if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
10610 ctype = NULL_TREE;
10611 else
10612 doing_friend = 1;
10613 }
10614
10615 if (DECL_DECLARED_INLINE_P (decl1)
10616 && lookup_attribute ("noinline", attrs))
10617 warning (0, "inline function %q+D given attribute noinline", decl1);
10618
10619 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
10620 /* This is a constructor, we must ensure that any default args
10621 introduced by this definition are propagated to the clones
10622 now. The clones are used directly in overload resolution. */
10623 adjust_clone_args (decl1);
10624
10625 /* Sometimes we don't notice that a function is a static member, and
10626 build a METHOD_TYPE for it. Fix that up now. */
10627 if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
10628 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
10629 {
10630 revert_static_member_fn (decl1);
10631 ctype = NULL_TREE;
10632 }
10633
10634 /* Set up current_class_type, and enter the scope of the class, if
10635 appropriate. */
10636 if (ctype)
10637 push_nested_class (ctype);
10638 else if (DECL_STATIC_FUNCTION_P (decl1))
10639 push_nested_class (DECL_CONTEXT (decl1));
10640
10641 /* Now that we have entered the scope of the class, we must restore
10642 the bindings for any template parameters surrounding DECL1, if it
10643 is an inline member template. (Order is important; consider the
10644 case where a template parameter has the same name as a field of
10645 the class.) It is not until after this point that
10646 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */
10647 if (flags & SF_INCLASS_INLINE)
10648 maybe_begin_member_template_processing (decl1);
10649
10650 /* Effective C++ rule 15. */
10651 if (warn_ecpp
10652 && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
10653 && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
10654 warning (OPT_Weffc__, "%<operator=%> should return a reference to %<*this%>");
10655
10656 /* Make the init_value nonzero so pushdecl knows this is not tentative.
10657 error_mark_node is replaced below (in poplevel) with the BLOCK. */
10658 if (!DECL_INITIAL (decl1))
10659 DECL_INITIAL (decl1) = error_mark_node;
10660
10661 /* This function exists in static storage.
10662 (This does not mean `static' in the C sense!) */
10663 TREE_STATIC (decl1) = 1;
10664
10665 /* We must call push_template_decl after current_class_type is set
10666 up. (If we are processing inline definitions after exiting a
10667 class scope, current_class_type will be NULL_TREE until set above
10668 by push_nested_class.) */
10669 if (processing_template_decl)
10670 {
10671 /* FIXME: Handle error_mark_node more gracefully. */
10672 tree newdecl1 = push_template_decl (decl1);
10673 if (newdecl1 != error_mark_node)
10674 decl1 = newdecl1;
10675 }
10676
10677 /* We are now in the scope of the function being defined. */
10678 current_function_decl = decl1;
10679
10680 /* Save the parm names or decls from this function's declarator
10681 where store_parm_decls will find them. */
10682 current_function_parms = DECL_ARGUMENTS (decl1);
10683
10684 /* Make sure the parameter and return types are reasonable. When
10685 you declare a function, these types can be incomplete, but they
10686 must be complete when you define the function. */
10687 check_function_type (decl1, current_function_parms);
10688
10689 /* Build the return declaration for the function. */
10690 restype = TREE_TYPE (fntype);
10691 /* Promote the value to int before returning it. */
10692 if (c_promoting_integer_type_p (restype))
10693 restype = type_promotes_to (restype);
10694 if (DECL_RESULT (decl1) == NULL_TREE)
10695 {
10696 tree resdecl;
10697
10698 resdecl = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
10699 DECL_ARTIFICIAL (resdecl) = 1;
10700 DECL_IGNORED_P (resdecl) = 1;
10701 DECL_RESULT (decl1) = resdecl;
10702
10703 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl);
10704 }
10705
10706 /* Let the user know we're compiling this function. */
10707 announce_function (decl1);
10708
10709 /* Record the decl so that the function name is defined.
10710 If we already have a decl for this name, and it is a FUNCTION_DECL,
10711 use the old decl. */
10712 if (!processing_template_decl && !(flags & SF_PRE_PARSED))
10713 {
10714 /* A specialization is not used to guide overload resolution. */
10715 if (!DECL_FUNCTION_MEMBER_P (decl1)
10716 && !(DECL_USE_TEMPLATE (decl1) &&
10717 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1))))
10718 {
10719 tree olddecl = pushdecl (decl1);
10720
10721 if (olddecl == error_mark_node)
10722 /* If something went wrong when registering the declaration,
10723 use DECL1; we have to have a FUNCTION_DECL to use when
10724 parsing the body of the function. */
10725 ;
10726 else
10727 /* Otherwise, OLDDECL is either a previous declaration of
10728 the same function or DECL1 itself. */
10729 decl1 = olddecl;
10730 }
10731 else
10732 {
10733 /* We need to set the DECL_CONTEXT. */
10734 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
10735 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
10736 }
10737 fntype = TREE_TYPE (decl1);
10738
10739 /* If #pragma weak applies, mark the decl appropriately now.
10740 The pragma only applies to global functions. Because
10741 determining whether or not the #pragma applies involves
10742 computing the mangled name for the declaration, we cannot
10743 apply the pragma until after we have merged this declaration
10744 with any previous declarations; if the original declaration
10745 has a linkage specification, that specification applies to
10746 the definition as well, and may affect the mangled name. */
10747 if (!DECL_CONTEXT (decl1))
10748 maybe_apply_pragma_weak (decl1);
10749 }
10750
10751 /* Reset this in case the call to pushdecl changed it. */
10752 current_function_decl = decl1;
10753
10754 gcc_assert (DECL_INITIAL (decl1));
10755
10756 /* This function may already have been parsed, in which case just
10757 return; our caller will skip over the body without parsing. */
10758 if (DECL_INITIAL (decl1) != error_mark_node)
10759 return;
10760
10761 /* Initialize RTL machinery. We cannot do this until
10762 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this
10763 even when processing a template; this is how we get
10764 CFUN set up, and our per-function variables initialized.
10765 FIXME factor out the non-RTL stuff. */
10766 bl = current_binding_level;
10767 allocate_struct_function (decl1);
10768 current_binding_level = bl;
10769
10770 /* Even though we're inside a function body, we still don't want to
10771 call expand_expr to calculate the size of a variable-sized array.
10772 We haven't necessarily assigned RTL to all variables yet, so it's
10773 not safe to try to expand expressions involving them. */
10774 cfun->x_dont_save_pending_sizes_p = 1;
10775
10776 /* Start the statement-tree, start the tree now. */
10777 DECL_SAVED_TREE (decl1) = push_stmt_list ();
10778
10779 /* If we are (erroneously) defining a function that we have already
10780 defined before, wipe out what we knew before. */
10781 if (!DECL_PENDING_INLINE_P (decl1))
10782 DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
10783
10784 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
10785 {
10786 /* We know that this was set up by `grokclassfn'. We do not
10787 wait until `store_parm_decls', since evil parse errors may
10788 never get us to that point. Here we keep the consistency
10789 between `current_class_type' and `current_class_ptr'. */
10790 tree t = DECL_ARGUMENTS (decl1);
10791
10792 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL);
10793 gcc_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE);
10794
10795 cp_function_chain->x_current_class_ref
10796 = build_indirect_ref (t, NULL);
10797 cp_function_chain->x_current_class_ptr = t;
10798
10799 /* Constructors and destructors need to know whether they're "in
10800 charge" of initializing virtual base classes. */
10801 t = TREE_CHAIN (t);
10802 if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
10803 {
10804 current_in_charge_parm = t;
10805 t = TREE_CHAIN (t);
10806 }
10807 if (DECL_HAS_VTT_PARM_P (decl1))
10808 {
10809 gcc_assert (DECL_NAME (t) == vtt_parm_identifier);
10810 current_vtt_parm = t;
10811 }
10812 }
10813
10814 honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1)
10815 /* Implicitly-defined methods (like the
10816 destructor for a class in which no destructor
10817 is explicitly declared) must not be defined
10818 until their definition is needed. So, we
10819 ignore interface specifications for
10820 compiler-generated functions. */
10821 && !DECL_ARTIFICIAL (decl1));
10822
10823 if (DECL_INTERFACE_KNOWN (decl1))
10824 {
10825 tree ctx = decl_function_context (decl1);
10826
10827 if (DECL_NOT_REALLY_EXTERN (decl1))
10828 DECL_EXTERNAL (decl1) = 0;
10829
10830 if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
10831 && TREE_PUBLIC (ctx))
10832 /* This is a function in a local class in an extern inline
10833 function. */
10834 comdat_linkage (decl1);
10835 }
10836 /* If this function belongs to an interface, it is public.
10837 If it belongs to someone else's interface, it is also external.
10838 This only affects inlines and template instantiations. */
10839 else if (!finfo->interface_unknown && honor_interface)
10840 {
10841 if (DECL_DECLARED_INLINE_P (decl1)
10842 || DECL_TEMPLATE_INSTANTIATION (decl1)
10843 || processing_template_decl)
10844 {
10845 DECL_EXTERNAL (decl1)
10846 = (finfo->interface_only
10847 || (DECL_DECLARED_INLINE_P (decl1)
10848 && ! flag_implement_inlines
10849 && !DECL_VINDEX (decl1)));
10850
10851 /* For WIN32 we also want to put these in linkonce sections. */
10852 maybe_make_one_only (decl1);
10853 }
10854 else
10855 DECL_EXTERNAL (decl1) = 0;
10856 DECL_INTERFACE_KNOWN (decl1) = 1;
10857 /* If this function is in an interface implemented in this file,
10858 make sure that the backend knows to emit this function
10859 here. */
10860 if (!DECL_EXTERNAL (decl1))
10861 mark_needed (decl1);
10862 }
10863 else if (finfo->interface_unknown && finfo->interface_only
10864 && honor_interface)
10865 {
10866 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
10867 interface, we will have both finfo->interface_unknown and
10868 finfo->interface_only set. In that case, we don't want to
10869 use the normal heuristics because someone will supply a
10870 #pragma implementation elsewhere, and deducing it here would
10871 produce a conflict. */
10872 comdat_linkage (decl1);
10873 DECL_EXTERNAL (decl1) = 0;
10874 DECL_INTERFACE_KNOWN (decl1) = 1;
10875 DECL_DEFER_OUTPUT (decl1) = 1;
10876 }
10877 else
10878 {
10879 /* This is a definition, not a reference.
10880 So clear DECL_EXTERNAL. */
10881 DECL_EXTERNAL (decl1) = 0;
10882
10883 if ((DECL_DECLARED_INLINE_P (decl1)
10884 || DECL_TEMPLATE_INSTANTIATION (decl1))
10885 && ! DECL_INTERFACE_KNOWN (decl1)
10886 /* Don't try to defer nested functions for now. */
10887 && ! decl_function_context (decl1))
10888 DECL_DEFER_OUTPUT (decl1) = 1;
10889 else
10890 DECL_INTERFACE_KNOWN (decl1) = 1;
10891 }
10892
10893 /* Determine the ELF visibility attribute for the function. We must not
10894 do this before calling "pushdecl", as we must allow "duplicate_decls"
10895 to merge any attributes appropriately. We also need to wait until
10896 linkage is set. */
10897 if (!DECL_CLONED_FUNCTION_P (decl1))
10898 determine_visibility (decl1);
10899
10900 begin_scope (sk_function_parms, decl1);
10901
10902 ++function_depth;
10903
10904 if (DECL_DESTRUCTOR_P (decl1)
10905 || (DECL_CONSTRUCTOR_P (decl1)
10906 && targetm.cxx.cdtor_returns_this ()))
10907 {
10908 cdtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
10909 DECL_CONTEXT (cdtor_label) = current_function_decl;
10910 }
10911
10912 start_fname_decls ();
10913
10914 store_parm_decls (current_function_parms);
10915 }
10916
10917
10918 /* Like start_preparsed_function, except that instead of a
10919 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR.
10920
10921 Returns 1 on success. If the DECLARATOR is not suitable for a function
10922 (it defines a datum instead), we return 0, which tells
10923 yyparse to report a parse error. */
10924
10925 int
start_function(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrs)10926 start_function (cp_decl_specifier_seq *declspecs,
10927 const cp_declarator *declarator,
10928 tree attrs)
10929 {
10930 tree decl1;
10931
10932 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
10933 /* If the declarator is not suitable for a function definition,
10934 cause a syntax error. */
10935 if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
10936 return 0;
10937
10938 if (DECL_MAIN_P (decl1))
10939 /* main must return int. grokfndecl should have corrected it
10940 (and issued a diagnostic) if the user got it wrong. */
10941 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)),
10942 integer_type_node));
10943
10944 start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT);
10945
10946 return 1;
10947 }
10948
10949 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of
10950 FN. */
10951
10952 static bool
use_eh_spec_block(tree fn)10953 use_eh_spec_block (tree fn)
10954 {
10955 return (flag_exceptions && flag_enforce_eh_specs
10956 && !processing_template_decl
10957 && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
10958 /* We insert the EH_SPEC_BLOCK only in the original
10959 function; then, it is copied automatically to the
10960 clones. */
10961 && !DECL_CLONED_FUNCTION_P (fn)
10962 /* Implicitly-generated constructors and destructors have
10963 exception specifications. However, those specifications
10964 are the union of the possible exceptions specified by the
10965 constructors/destructors for bases and members, so no
10966 unallowed exception will ever reach this function. By
10967 not creating the EH_SPEC_BLOCK we save a little memory,
10968 and we avoid spurious warnings about unreachable
10969 code. */
10970 && !DECL_ARTIFICIAL (fn));
10971 }
10972
10973 /* Store the parameter declarations into the current function declaration.
10974 This is called after parsing the parameter declarations, before
10975 digesting the body of the function.
10976
10977 Also install to binding contour return value identifier, if any. */
10978
10979 static void
store_parm_decls(tree current_function_parms)10980 store_parm_decls (tree current_function_parms)
10981 {
10982 tree fndecl = current_function_decl;
10983 tree parm;
10984
10985 /* This is a chain of any other decls that came in among the parm
10986 declarations. If a parm is declared with enum {foo, bar} x;
10987 then CONST_DECLs for foo and bar are put here. */
10988 tree nonparms = NULL_TREE;
10989
10990 if (current_function_parms)
10991 {
10992 /* This case is when the function was defined with an ANSI prototype.
10993 The parms already have decls, so we need not do anything here
10994 except record them as in effect
10995 and complain if any redundant old-style parm decls were written. */
10996
10997 tree specparms = current_function_parms;
10998 tree next;
10999
11000 /* Must clear this because it might contain TYPE_DECLs declared
11001 at class level. */
11002 current_binding_level->names = NULL;
11003
11004 /* If we're doing semantic analysis, then we'll call pushdecl
11005 for each of these. We must do them in reverse order so that
11006 they end in the correct forward order. */
11007 specparms = nreverse (specparms);
11008
11009 for (parm = specparms; parm; parm = next)
11010 {
11011 next = TREE_CHAIN (parm);
11012 if (TREE_CODE (parm) == PARM_DECL)
11013 {
11014 if (DECL_NAME (parm) == NULL_TREE
11015 || TREE_CODE (parm) != VOID_TYPE)
11016 pushdecl (parm);
11017 else
11018 error ("parameter %qD declared void", parm);
11019 }
11020 else
11021 {
11022 /* If we find an enum constant or a type tag,
11023 put it aside for the moment. */
11024 TREE_CHAIN (parm) = NULL_TREE;
11025 nonparms = chainon (nonparms, parm);
11026 }
11027 }
11028
11029 /* Get the decls in their original chain order and record in the
11030 function. This is all and only the PARM_DECLs that were
11031 pushed into scope by the loop above. */
11032 DECL_ARGUMENTS (fndecl) = getdecls ();
11033 }
11034 else
11035 DECL_ARGUMENTS (fndecl) = NULL_TREE;
11036
11037 /* Now store the final chain of decls for the arguments
11038 as the decl-chain of the current lexical scope.
11039 Put the enumerators in as well, at the front so that
11040 DECL_ARGUMENTS is not modified. */
11041 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl));
11042
11043 if (use_eh_spec_block (current_function_decl))
11044 current_eh_spec_block = begin_eh_spec_block ();
11045 }
11046
11047
11048 /* We have finished doing semantic analysis on DECL, but have not yet
11049 generated RTL for its body. Save away our current state, so that
11050 when we want to generate RTL later we know what to do. */
11051
11052 static void
save_function_data(tree decl)11053 save_function_data (tree decl)
11054 {
11055 struct language_function *f;
11056
11057 /* Save the language-specific per-function data so that we can
11058 get it back when we really expand this function. */
11059 gcc_assert (!DECL_PENDING_INLINE_P (decl));
11060
11061 /* Make a copy. */
11062 f = GGC_NEW (struct language_function);
11063 memcpy (f, cp_function_chain, sizeof (struct language_function));
11064 DECL_SAVED_FUNCTION_DATA (decl) = f;
11065
11066 /* Clear out the bits we don't need. */
11067 f->base.x_stmt_tree.x_cur_stmt_list = NULL_TREE;
11068 f->bindings = NULL;
11069 f->x_local_names = NULL;
11070 }
11071
11072
11073 /* Set the return value of the constructor (if present). */
11074
11075 static void
finish_constructor_body(void)11076 finish_constructor_body (void)
11077 {
11078 tree val;
11079 tree exprstmt;
11080
11081 if (targetm.cxx.cdtor_returns_this ())
11082 {
11083 /* Any return from a constructor will end up here. */
11084 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
11085
11086 val = DECL_ARGUMENTS (current_function_decl);
11087 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11088 DECL_RESULT (current_function_decl), val);
11089 /* Return the address of the object. */
11090 exprstmt = build_stmt (RETURN_EXPR, val);
11091 add_stmt (exprstmt);
11092 }
11093 }
11094
11095 /* Do all the processing for the beginning of a destructor; set up the
11096 vtable pointers and cleanups for bases and members. */
11097
11098 static void
begin_destructor_body(void)11099 begin_destructor_body (void)
11100 {
11101 tree compound_stmt;
11102
11103 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already
11104 issued an error message. We still want to try to process the
11105 body of the function, but initialize_vtbl_ptrs will crash if
11106 TYPE_BINFO is NULL. */
11107 if (COMPLETE_TYPE_P (current_class_type))
11108 {
11109 compound_stmt = begin_compound_stmt (0);
11110 /* Make all virtual function table pointers in non-virtual base
11111 classes point to CURRENT_CLASS_TYPE's virtual function
11112 tables. */
11113 initialize_vtbl_ptrs (current_class_ptr);
11114 finish_compound_stmt (compound_stmt);
11115
11116 /* And insert cleanups for our bases and members so that they
11117 will be properly destroyed if we throw. */
11118 push_base_cleanups ();
11119 }
11120 }
11121
11122 /* At the end of every destructor we generate code to delete the object if
11123 necessary. Do that now. */
11124
11125 static void
finish_destructor_body(void)11126 finish_destructor_body (void)
11127 {
11128 tree exprstmt;
11129
11130 /* Any return from a destructor will end up here; that way all base
11131 and member cleanups will be run when the function returns. */
11132 add_stmt (build_stmt (LABEL_EXPR, cdtor_label));
11133
11134 /* In a virtual destructor, we must call delete. */
11135 if (DECL_VIRTUAL_P (current_function_decl))
11136 {
11137 tree if_stmt;
11138 tree virtual_size = cxx_sizeof (current_class_type);
11139
11140 /* [class.dtor]
11141
11142 At the point of definition of a virtual destructor (including
11143 an implicit definition), non-placement operator delete shall
11144 be looked up in the scope of the destructor's class and if
11145 found shall be accessible and unambiguous. */
11146 exprstmt = build_op_delete_call(DELETE_EXPR, current_class_ptr,
11147 virtual_size,
11148 /*global_p=*/false,
11149 /*placement=*/NULL_TREE,
11150 /*alloc_fn=*/NULL_TREE);
11151
11152 if_stmt = begin_if_stmt ();
11153 finish_if_stmt_cond (build2 (BIT_AND_EXPR, integer_type_node,
11154 current_in_charge_parm,
11155 integer_one_node),
11156 if_stmt);
11157 finish_expr_stmt (exprstmt);
11158 finish_then_clause (if_stmt);
11159 finish_if_stmt (if_stmt);
11160 }
11161
11162 if (targetm.cxx.cdtor_returns_this ())
11163 {
11164 tree val;
11165
11166 val = DECL_ARGUMENTS (current_function_decl);
11167 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
11168 DECL_RESULT (current_function_decl), val);
11169 /* Return the address of the object. */
11170 exprstmt = build_stmt (RETURN_EXPR, val);
11171 add_stmt (exprstmt);
11172 }
11173 }
11174
11175 /* Do the necessary processing for the beginning of a function body, which
11176 in this case includes member-initializers, but not the catch clauses of
11177 a function-try-block. Currently, this means opening a binding level
11178 for the member-initializers (in a ctor) and member cleanups (in a dtor). */
11179
11180 tree
begin_function_body(void)11181 begin_function_body (void)
11182 {
11183 tree stmt;
11184
11185 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11186 return NULL_TREE;
11187
11188 if (processing_template_decl)
11189 /* Do nothing now. */;
11190 else
11191 /* Always keep the BLOCK node associated with the outermost pair of
11192 curly braces of a function. These are needed for correct
11193 operation of dwarfout.c. */
11194 keep_next_level (true);
11195
11196 stmt = begin_compound_stmt (BCS_FN_BODY);
11197
11198 if (processing_template_decl)
11199 /* Do nothing now. */;
11200 else if (DECL_DESTRUCTOR_P (current_function_decl))
11201 begin_destructor_body ();
11202
11203 return stmt;
11204 }
11205
11206 /* Do the processing for the end of a function body. Currently, this means
11207 closing out the cleanups for fully-constructed bases and members, and in
11208 the case of the destructor, deleting the object if desired. Again, this
11209 is only meaningful for [cd]tors, since they are the only functions where
11210 there is a significant distinction between the main body and any
11211 function catch clauses. Handling, say, main() return semantics here
11212 would be wrong, as flowing off the end of a function catch clause for
11213 main() would also need to return 0. */
11214
11215 void
finish_function_body(tree compstmt)11216 finish_function_body (tree compstmt)
11217 {
11218 if (compstmt == NULL_TREE)
11219 return;
11220
11221 /* Close the block. */
11222 finish_compound_stmt (compstmt);
11223
11224 if (processing_template_decl)
11225 /* Do nothing now. */;
11226 else if (DECL_CONSTRUCTOR_P (current_function_decl))
11227 finish_constructor_body ();
11228 else if (DECL_DESTRUCTOR_P (current_function_decl))
11229 finish_destructor_body ();
11230 }
11231
11232 /* Given a function, returns the BLOCK corresponding to the outermost level
11233 of curly braces, skipping the artificial block created for constructor
11234 initializers. */
11235
11236 static tree
outer_curly_brace_block(tree fndecl)11237 outer_curly_brace_block (tree fndecl)
11238 {
11239 tree block = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl));
11240 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
11241 /* Skip the artificial function body block. */
11242 block = BLOCK_SUBBLOCKS (block);
11243 return block;
11244 }
11245
11246 /* Finish up a function declaration and compile that function
11247 all the way to assembler language output. The free the storage
11248 for the function definition.
11249
11250 FLAGS is a bitwise or of the following values:
11251 2 - INCLASS_INLINE
11252 We just finished processing the body of an in-class inline
11253 function definition. (This processing will have taken place
11254 after the class definition is complete.) */
11255
11256 tree
finish_function(int flags)11257 finish_function (int flags)
11258 {
11259 tree fndecl = current_function_decl;
11260 tree fntype, ctype = NULL_TREE;
11261 int inclass_inline = (flags & 2) != 0;
11262 int nested;
11263
11264 /* When we get some parse errors, we can end up without a
11265 current_function_decl, so cope. */
11266 if (fndecl == NULL_TREE)
11267 return error_mark_node;
11268
11269 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
11270 && DECL_VIRTUAL_P (fndecl)
11271 && !processing_template_decl)
11272 {
11273 tree fnclass = DECL_CONTEXT (fndecl);
11274 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
11275 keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
11276 }
11277
11278 nested = function_depth > 1;
11279 fntype = TREE_TYPE (fndecl);
11280
11281 /* TREE_READONLY (fndecl) = 1;
11282 This caused &foo to be of type ptr-to-const-function
11283 which then got a warning when stored in a ptr-to-function variable. */
11284
11285 gcc_assert (building_stmt_tree ());
11286 /* The current function is being defined, so its DECL_INITIAL should
11287 be set, and unless there's a multiple definition, it should be
11288 error_mark_node. */
11289 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node);
11290
11291 /* For a cloned function, we've already got all the code we need;
11292 there's no need to add any extra bits. */
11293 if (!DECL_CLONED_FUNCTION_P (fndecl))
11294 {
11295 if (DECL_MAIN_P (current_function_decl))
11296 {
11297 tree stmt;
11298
11299 /* Make it so that `main' always returns 0 by default (or
11300 1 for VMS). */
11301 #if VMS_TARGET
11302 stmt = finish_return_stmt (integer_one_node);
11303 #else
11304 stmt = finish_return_stmt (integer_zero_node);
11305 #endif
11306 /* Hack. We don't want the middle-end to warn that this
11307 return is unreachable, so put the statement on the
11308 special line 0. */
11309 #ifdef USE_MAPPED_LOCATION
11310 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
11311 #else
11312 annotate_with_file_line (stmt, input_filename, 0);
11313 #endif
11314 }
11315
11316 if (use_eh_spec_block (current_function_decl))
11317 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
11318 (TREE_TYPE (current_function_decl)),
11319 current_eh_spec_block);
11320 }
11321
11322 /* If we're saving up tree structure, tie off the function now. */
11323 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
11324
11325 finish_fname_decls ();
11326
11327 /* If this function can't throw any exceptions, remember that. */
11328 if (!processing_template_decl
11329 && !cp_function_chain->can_throw
11330 && !flag_non_call_exceptions
11331 && !DECL_REPLACEABLE_P (fndecl))
11332 TREE_NOTHROW (fndecl) = 1;
11333
11334 /* This must come after expand_function_end because cleanups might
11335 have declarations (from inline functions) that need to go into
11336 this function's blocks. */
11337
11338 /* If the current binding level isn't the outermost binding level
11339 for this function, either there is a bug, or we have experienced
11340 syntax errors and the statement tree is malformed. */
11341 if (current_binding_level->kind != sk_function_parms)
11342 {
11343 /* Make sure we have already experienced errors. */
11344 gcc_assert (errorcount);
11345
11346 /* Throw away the broken statement tree and extra binding
11347 levels. */
11348 DECL_SAVED_TREE (fndecl) = alloc_stmt_list ();
11349
11350 while (current_binding_level->kind != sk_function_parms)
11351 {
11352 if (current_binding_level->kind == sk_class)
11353 pop_nested_class ();
11354 else
11355 poplevel (0, 0, 0);
11356 }
11357 }
11358 poplevel (1, 0, 1);
11359
11360 /* Statements should always be full-expressions at the outermost set
11361 of curly braces for a function. */
11362 gcc_assert (stmts_are_full_exprs_p ());
11363
11364 /* Set up the named return value optimization, if we can. Candidate
11365 variables are selected in check_return_expr. */
11366 if (current_function_return_value)
11367 {
11368 tree r = current_function_return_value;
11369 tree outer;
11370
11371 if (r != error_mark_node
11372 /* This is only worth doing for fns that return in memory--and
11373 simpler, since we don't have to worry about promoted modes. */
11374 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl)
11375 /* Only allow this for variables declared in the outer scope of
11376 the function so we know that their lifetime always ends with a
11377 return; see g++.dg/opt/nrv6.C. We could be more flexible if
11378 we were to do this optimization in tree-ssa. */
11379 && (outer = outer_curly_brace_block (fndecl))
11380 && chain_member (r, BLOCK_VARS (outer)))
11381 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl));
11382
11383 current_function_return_value = NULL_TREE;
11384 }
11385
11386 /* Remember that we were in class scope. */
11387 if (current_class_name)
11388 ctype = current_class_type;
11389
11390 /* Must mark the RESULT_DECL as being in this function. */
11391 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
11392
11393 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
11394 to the FUNCTION_DECL node itself. */
11395 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
11396
11397 /* Save away current state, if appropriate. */
11398 if (!processing_template_decl)
11399 save_function_data (fndecl);
11400
11401 /* Complain if there's just no return statement. */
11402 if (warn_return_type
11403 && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
11404 && !dependent_type_p (TREE_TYPE (fntype))
11405 && !current_function_returns_value && !current_function_returns_null
11406 /* Don't complain if we abort or throw. */
11407 && !current_function_returns_abnormally
11408 && !DECL_NAME (DECL_RESULT (fndecl))
11409 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
11410 inline function, as we might never be compiled separately. */
11411 && (DECL_INLINE (fndecl) || processing_template_decl)
11412 /* Structor return values (if any) are set by the compiler. */
11413 && !DECL_CONSTRUCTOR_P (fndecl)
11414 && !DECL_DESTRUCTOR_P (fndecl))
11415 warning (OPT_Wreturn_type, "no return statement in function returning non-void");
11416
11417 /* Store the end of the function, so that we get good line number
11418 info for the epilogue. */
11419 cfun->function_end_locus = input_location;
11420
11421 /* Genericize before inlining. */
11422 if (!processing_template_decl)
11423 {
11424 struct language_function *f = DECL_SAVED_FUNCTION_DATA (fndecl);
11425 cp_genericize (fndecl);
11426 /* Clear out the bits we don't need. */
11427 f->x_current_class_ptr = NULL;
11428 f->x_current_class_ref = NULL;
11429 f->x_eh_spec_block = NULL;
11430 f->x_in_charge_parm = NULL;
11431 f->x_vtt_parm = NULL;
11432 f->x_return_value = NULL;
11433 f->bindings = NULL;
11434 f->extern_decl_map = NULL;
11435
11436 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
11437 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
11438 }
11439 /* Clear out the bits we don't need. */
11440 local_names = NULL;
11441
11442 /* We're leaving the context of this function, so zap cfun. It's still in
11443 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */
11444 cfun = NULL;
11445 current_function_decl = NULL;
11446
11447 /* If this is an in-class inline definition, we may have to pop the
11448 bindings for the template parameters that we added in
11449 maybe_begin_member_template_processing when start_function was
11450 called. */
11451 if (inclass_inline)
11452 maybe_end_member_template_processing ();
11453
11454 /* Leave the scope of the class. */
11455 if (ctype)
11456 pop_nested_class ();
11457
11458 --function_depth;
11459
11460 /* Clean up. */
11461 if (! nested)
11462 /* Let the error reporting routines know that we're outside a
11463 function. For a nested function, this value is used in
11464 cxx_pop_function_context and then reset via pop_function_context. */
11465 current_function_decl = NULL_TREE;
11466
11467 return fndecl;
11468 }
11469
11470 /* Create the FUNCTION_DECL for a function definition.
11471 DECLSPECS and DECLARATOR are the parts of the declaration;
11472 they describe the return type and the name of the function,
11473 but twisted together in a fashion that parallels the syntax of C.
11474
11475 This function creates a binding context for the function body
11476 as well as setting up the FUNCTION_DECL in current_function_decl.
11477
11478 Returns a FUNCTION_DECL on success.
11479
11480 If the DECLARATOR is not suitable for a function (it defines a datum
11481 instead), we return 0, which tells yyparse to report a parse error.
11482
11483 May return void_type_node indicating that this method is actually
11484 a friend. See grokfield for more details.
11485
11486 Came here with a `.pushlevel' .
11487
11488 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
11489 CHANGES TO CODE IN `grokfield'. */
11490
11491 tree
start_method(cp_decl_specifier_seq * declspecs,const cp_declarator * declarator,tree attrlist)11492 start_method (cp_decl_specifier_seq *declspecs,
11493 const cp_declarator *declarator, tree attrlist)
11494 {
11495 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
11496 &attrlist);
11497
11498 if (fndecl == error_mark_node)
11499 return error_mark_node;
11500
11501 if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
11502 {
11503 error ("invalid member function declaration");
11504 return error_mark_node;
11505 }
11506
11507 if (attrlist)
11508 cplus_decl_attributes (&fndecl, attrlist, 0);
11509
11510 /* Pass friends other than inline friend functions back. */
11511 if (fndecl == void_type_node)
11512 return fndecl;
11513
11514 if (DECL_IN_AGGR_P (fndecl))
11515 {
11516 if (DECL_CONTEXT (fndecl)
11517 && TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
11518 error ("%qD is already defined in class %qT", fndecl,
11519 DECL_CONTEXT (fndecl));
11520 return error_mark_node;
11521 }
11522
11523 check_template_shadow (fndecl);
11524
11525 DECL_DECLARED_INLINE_P (fndecl) = 1;
11526 if (flag_default_inline)
11527 DECL_INLINE (fndecl) = 1;
11528
11529 /* We process method specializations in finish_struct_1. */
11530 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
11531 {
11532 fndecl = push_template_decl (fndecl);
11533 if (fndecl == error_mark_node)
11534 return fndecl;
11535 }
11536
11537 if (! DECL_FRIEND_P (fndecl))
11538 {
11539 if (TREE_CHAIN (fndecl))
11540 {
11541 fndecl = copy_node (fndecl);
11542 TREE_CHAIN (fndecl) = NULL_TREE;
11543 }
11544 }
11545
11546 finish_decl (fndecl, NULL_TREE, NULL_TREE);
11547
11548 /* Make a place for the parms. */
11549 begin_scope (sk_function_parms, fndecl);
11550
11551 DECL_IN_AGGR_P (fndecl) = 1;
11552 return fndecl;
11553 }
11554
11555 /* Go through the motions of finishing a function definition.
11556 We don't compile this method until after the whole class has
11557 been processed.
11558
11559 FINISH_METHOD must return something that looks as though it
11560 came from GROKFIELD (since we are defining a method, after all).
11561
11562 This is called after parsing the body of the function definition.
11563 STMTS is the chain of statements that makes up the function body.
11564
11565 DECL is the ..._DECL that `start_method' provided. */
11566
11567 tree
finish_method(tree decl)11568 finish_method (tree decl)
11569 {
11570 tree fndecl = decl;
11571 tree old_initial;
11572
11573 tree link;
11574
11575 if (decl == void_type_node)
11576 return decl;
11577
11578 old_initial = DECL_INITIAL (fndecl);
11579
11580 /* Undo the level for the parms (from start_method).
11581 This is like poplevel, but it causes nothing to be
11582 saved. Saving information here confuses symbol-table
11583 output routines. Besides, this information will
11584 be correctly output when this method is actually
11585 compiled. */
11586
11587 /* Clear out the meanings of the local variables of this level;
11588 also record in each decl which block it belongs to. */
11589
11590 for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
11591 {
11592 if (DECL_NAME (link) != NULL_TREE)
11593 pop_binding (DECL_NAME (link), link);
11594 gcc_assert (TREE_CODE (link) != FUNCTION_DECL);
11595 DECL_CONTEXT (link) = NULL_TREE;
11596 }
11597
11598 poplevel (0, 0, 0);
11599
11600 DECL_INITIAL (fndecl) = old_initial;
11601
11602 /* We used to check if the context of FNDECL was different from
11603 current_class_type as another way to get inside here. This didn't work
11604 for String.cc in libg++. */
11605 if (DECL_FRIEND_P (fndecl))
11606 {
11607 VEC_safe_push (tree, gc, CLASSTYPE_INLINE_FRIENDS (current_class_type),
11608 fndecl);
11609 decl = void_type_node;
11610 }
11611
11612 return decl;
11613 }
11614
11615
11616 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that
11617 we can lay it out later, when and if its type becomes complete. */
11618
11619 void
maybe_register_incomplete_var(tree var)11620 maybe_register_incomplete_var (tree var)
11621 {
11622 gcc_assert (TREE_CODE (var) == VAR_DECL);
11623
11624 /* Keep track of variables with incomplete types. */
11625 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
11626 && DECL_EXTERNAL (var))
11627 {
11628 tree inner_type = TREE_TYPE (var);
11629
11630 while (TREE_CODE (inner_type) == ARRAY_TYPE)
11631 inner_type = TREE_TYPE (inner_type);
11632 inner_type = TYPE_MAIN_VARIANT (inner_type);
11633
11634 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
11635 /* RTTI TD entries are created while defining the type_info. */
11636 || (TYPE_LANG_SPECIFIC (inner_type)
11637 && TYPE_BEING_DEFINED (inner_type)))
11638 incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
11639 }
11640 }
11641
11642 /* Called when a class type (given by TYPE) is defined. If there are
11643 any existing VAR_DECLs whose type hsa been completed by this
11644 declaration, update them now. */
11645
11646 void
complete_vars(tree type)11647 complete_vars (tree type)
11648 {
11649 tree *list = &incomplete_vars;
11650
11651 gcc_assert (CLASS_TYPE_P (type));
11652 while (*list)
11653 {
11654 if (same_type_p (type, TREE_PURPOSE (*list)))
11655 {
11656 tree var = TREE_VALUE (*list);
11657 tree type = TREE_TYPE (var);
11658 /* Complete the type of the variable. The VAR_DECL itself
11659 will be laid out in expand_expr. */
11660 complete_type (type);
11661 cp_apply_type_quals_to_decl (cp_type_quals (type), var);
11662 /* Remove this entry from the list. */
11663 *list = TREE_CHAIN (*list);
11664 }
11665 else
11666 list = &TREE_CHAIN (*list);
11667 }
11668
11669 /* Check for pending declarations which may have abstract type. */
11670 complete_type_check_abstract (type);
11671 }
11672
11673 /* If DECL is of a type which needs a cleanup, build that cleanup
11674 here. */
11675
11676 tree
cxx_maybe_build_cleanup(tree decl)11677 cxx_maybe_build_cleanup (tree decl)
11678 {
11679 tree type = TREE_TYPE (decl);
11680
11681 if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
11682 {
11683 int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
11684 tree rval;
11685 bool has_vbases = (TREE_CODE (type) == RECORD_TYPE
11686 && CLASSTYPE_VBASECLASSES (type));
11687
11688 if (TREE_CODE (type) == ARRAY_TYPE)
11689 rval = decl;
11690 else
11691 {
11692 cxx_mark_addressable (decl);
11693 rval = build_unary_op (ADDR_EXPR, decl, 0);
11694 }
11695
11696 /* Optimize for space over speed here. */
11697 if (!has_vbases || flag_expensive_optimizations)
11698 flags |= LOOKUP_NONVIRTUAL;
11699
11700 rval = build_delete (TREE_TYPE (rval), rval,
11701 sfk_complete_destructor, flags, 0);
11702
11703 return rval;
11704 }
11705 return NULL_TREE;
11706 }
11707
11708 /* When a stmt has been parsed, this function is called. */
11709
11710 void
finish_stmt(void)11711 finish_stmt (void)
11712 {
11713 }
11714
11715 /* DECL was originally constructed as a non-static member function,
11716 but turned out to be static. Update it accordingly. */
11717
11718 void
revert_static_member_fn(tree decl)11719 revert_static_member_fn (tree decl)
11720 {
11721 tree tmp;
11722 tree function = TREE_TYPE (decl);
11723 tree args = TYPE_ARG_TYPES (function);
11724
11725 if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
11726 != TYPE_UNQUALIFIED)
11727 error ("static member function %q#D declared with type qualifiers", decl);
11728
11729 args = TREE_CHAIN (args);
11730 tmp = build_function_type (TREE_TYPE (function), args);
11731 tmp = build_qualified_type (tmp, cp_type_quals (function));
11732 tmp = build_exception_variant (tmp,
11733 TYPE_RAISES_EXCEPTIONS (function));
11734 TREE_TYPE (decl) = tmp;
11735 if (DECL_ARGUMENTS (decl))
11736 DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
11737 DECL_STATIC_FUNCTION_P (decl) = 1;
11738 }
11739
11740 /* Initialize the variables used during compilation of a C++
11741 function. */
11742
11743 void
cxx_push_function_context(struct function * f)11744 cxx_push_function_context (struct function * f)
11745 {
11746 struct language_function *p = GGC_CNEW (struct language_function);
11747 f->language = p;
11748
11749 /* Whenever we start a new function, we destroy temporaries in the
11750 usual way. */
11751 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
11752
11753 if (f->decl)
11754 {
11755 tree fn = f->decl;
11756
11757 if (DECL_SAVED_FUNCTION_DATA (fn))
11758 {
11759 /* If we already parsed this function, and we're just expanding it
11760 now, restore saved state. */
11761 *cp_function_chain = *DECL_SAVED_FUNCTION_DATA (fn);
11762
11763 /* We don't need the saved data anymore. Unless this is an inline
11764 function; we need the named return value info for
11765 declare_return_variable. */
11766 if (! DECL_INLINE (fn))
11767 DECL_SAVED_FUNCTION_DATA (fn) = NULL;
11768 }
11769 }
11770 }
11771
11772 /* Free the language-specific parts of F, now that we've finished
11773 compiling the function. */
11774
11775 void
cxx_pop_function_context(struct function * f)11776 cxx_pop_function_context (struct function * f)
11777 {
11778 f->language = 0;
11779 }
11780
11781 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
11782 one of the language-independent trees. */
11783
11784 enum cp_tree_node_structure_enum
cp_tree_node_structure(union lang_tree_node * t)11785 cp_tree_node_structure (union lang_tree_node * t)
11786 {
11787 switch (TREE_CODE (&t->generic))
11788 {
11789 case DEFAULT_ARG: return TS_CP_DEFAULT_ARG;
11790 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER;
11791 case OVERLOAD: return TS_CP_OVERLOAD;
11792 case TEMPLATE_PARM_INDEX: return TS_CP_TPI;
11793 case TINST_LEVEL: return TS_CP_TINST_LEVEL;
11794 case PTRMEM_CST: return TS_CP_PTRMEM;
11795 case BASELINK: return TS_CP_BASELINK;
11796 default: return TS_CP_GENERIC;
11797 }
11798 }
11799
11800 /* Build the void_list_node (void_type_node having been created). */
11801 tree
build_void_list_node(void)11802 build_void_list_node (void)
11803 {
11804 tree t = build_tree_list (NULL_TREE, void_type_node);
11805 return t;
11806 }
11807
11808 bool
cp_missing_noreturn_ok_p(tree decl)11809 cp_missing_noreturn_ok_p (tree decl)
11810 {
11811 /* A missing noreturn is ok for the `main' function. */
11812 return DECL_MAIN_P (decl);
11813 }
11814
11815 /* Return the COMDAT group into which DECL should be placed. */
11816
11817 const char *
cxx_comdat_group(tree decl)11818 cxx_comdat_group (tree decl)
11819 {
11820 tree name;
11821
11822 /* Virtual tables, construction virtual tables, and virtual table
11823 tables all go in a single COMDAT group, named after the primary
11824 virtual table. */
11825 if (TREE_CODE (decl) == VAR_DECL && DECL_VTABLE_OR_VTT_P (decl))
11826 name = DECL_ASSEMBLER_NAME (CLASSTYPE_VTABLES (DECL_CONTEXT (decl)));
11827 /* For all other DECLs, the COMDAT group is the mangled name of the
11828 declaration itself. */
11829 else
11830 {
11831 while (DECL_THUNK_P (decl))
11832 {
11833 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk
11834 into the same section as the target function. In that case
11835 we must return target's name. */
11836 tree target = THUNK_TARGET (decl);
11837 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target)
11838 && DECL_SECTION_NAME (target) != NULL
11839 && DECL_ONE_ONLY (target))
11840 decl = target;
11841 else
11842 break;
11843 }
11844 name = DECL_ASSEMBLER_NAME (decl);
11845 }
11846
11847 return IDENTIFIER_POINTER (name);
11848 }
11849
11850 #include "gt-cp-decl.h"
11851