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