OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.2.2/] [gcc/] [c-decl.c] - Blame information for rev 369

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 julius
/* Process declarations and variables for C compiler.
2
   Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3
   2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify it under
8
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 3, or (at your option) any later
10
version.
11
 
12
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15
for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING3.  If not see
19
<http://www.gnu.org/licenses/>.  */
20
 
21
/* Process declarations and symbol lookup for C front end.
22
   Also constructs types; the standard scalar types at initialization,
23
   and structure, union, array and enum types when they are declared.  */
24
 
25
/* ??? not all decl nodes are given the most useful possible
26
   line numbers.  For example, the CONST_DECLs for enum values.  */
27
 
28
#include "config.h"
29
#include "system.h"
30
#include "coretypes.h"
31
#include "input.h"
32
#include "tm.h"
33
#include "intl.h"
34
#include "tree.h"
35
#include "tree-inline.h"
36
#include "rtl.h"
37
#include "flags.h"
38
#include "function.h"
39
#include "output.h"
40
#include "expr.h"
41
#include "c-tree.h"
42
#include "toplev.h"
43
#include "ggc.h"
44
#include "tm_p.h"
45
#include "cpplib.h"
46
#include "target.h"
47
#include "debug.h"
48
#include "opts.h"
49
#include "timevar.h"
50
#include "c-common.h"
51
#include "c-pragma.h"
52
#include "langhooks.h"
53
#include "tree-mudflap.h"
54
#include "tree-gimple.h"
55
#include "diagnostic.h"
56
#include "tree-dump.h"
57
#include "cgraph.h"
58
#include "hashtab.h"
59
#include "libfuncs.h"
60
#include "except.h"
61
#include "langhooks-def.h"
62
#include "pointer-set.h"
63
 
64
/* In grokdeclarator, distinguish syntactic contexts of declarators.  */
65
enum decl_context
66
{ NORMAL,                       /* Ordinary declaration */
67
  FUNCDEF,                      /* Function definition */
68
  PARM,                         /* Declaration of parm before function body */
69
  FIELD,                        /* Declaration inside struct or union */
70
  TYPENAME};                    /* Typename (inside cast or sizeof)  */
71
 
72
 
73
/* Nonzero if we have seen an invalid cross reference
74
   to a struct, union, or enum, but not yet printed the message.  */
75
tree pending_invalid_xref;
76
 
77
/* File and line to appear in the eventual error message.  */
78
location_t pending_invalid_xref_location;
79
 
80
/* True means we've initialized exception handling.  */
81
bool c_eh_initialized_p;
82
 
83
/* While defining an enum type, this is 1 plus the last enumerator
84
   constant value.  Note that will do not have to save this or `enum_overflow'
85
   around nested function definition since such a definition could only
86
   occur in an enum value expression and we don't use these variables in
87
   that case.  */
88
 
89
static tree enum_next_value;
90
 
91
/* Nonzero means that there was overflow computing enum_next_value.  */
92
 
93
static int enum_overflow;
94
 
95
/* The file and line that the prototype came from if this is an
96
   old-style definition; used for diagnostics in
97
   store_parm_decls_oldstyle.  */
98
 
99
static location_t current_function_prototype_locus;
100
 
101
/* Whether this prototype was built-in.  */
102
 
103
static bool current_function_prototype_built_in;
104
 
105
/* The argument type information of this prototype.  */
106
 
107
static tree current_function_prototype_arg_types;
108
 
109
/* The argument information structure for the function currently being
110
   defined.  */
111
 
112
static struct c_arg_info *current_function_arg_info;
113
 
114
/* The obstack on which parser and related data structures, which are
115
   not live beyond their top-level declaration or definition, are
116
   allocated.  */
117
struct obstack parser_obstack;
118
 
119
/* The current statement tree.  */
120
 
121
static GTY(()) struct stmt_tree_s c_stmt_tree;
122
 
123
/* State saving variables.  */
124
tree c_break_label;
125
tree c_cont_label;
126
 
127
/* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128
   included in this invocation.  Note that the current translation
129
   unit is not included in this list.  */
130
 
131
static GTY(()) tree all_translation_units;
132
 
133
/* A list of decls to be made automatically visible in each file scope.  */
134
static GTY(()) tree visible_builtins;
135
 
136
/* Set to 0 at beginning of a function definition, set to 1 if
137
   a return statement that specifies a return value is seen.  */
138
 
139
int current_function_returns_value;
140
 
141
/* Set to 0 at beginning of a function definition, set to 1 if
142
   a return statement with no argument is seen.  */
143
 
144
int current_function_returns_null;
145
 
146
/* Set to 0 at beginning of a function definition, set to 1 if
147
   a call to a noreturn function is seen.  */
148
 
149
int current_function_returns_abnormally;
150
 
151
/* Set to nonzero by `grokdeclarator' for a function
152
   whose return type is defaulted, if warnings for this are desired.  */
153
 
154
static int warn_about_return_type;
155
 
156
/* Nonzero when starting a function declared `extern inline'.  */
157
 
158
static int current_extern_inline;
159
 
160
/* Nonzero when the current toplevel function contains a declaration
161
   of a nested function which is never defined.  */
162
 
163
static bool undef_nested_function;
164
 
165
/* True means global_bindings_p should return false even if the scope stack
166
   says we are in file scope.  */
167
bool c_override_global_bindings_to_false;
168
 
169
 
170
/* Each c_binding structure describes one binding of an identifier to
171
   a decl.  All the decls in a scope - irrespective of namespace - are
172
   chained together by the ->prev field, which (as the name implies)
173
   runs in reverse order.  All the decls in a given namespace bound to
174
   a given identifier are chained by the ->shadowed field, which runs
175
   from inner to outer scopes.
176
 
177
   The ->decl field usually points to a DECL node, but there are two
178
   exceptions.  In the namespace of type tags, the bound entity is a
179
   RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node.  If an undeclared
180
   identifier is encountered, it is bound to error_mark_node to
181
   suppress further errors about that identifier in the current
182
   function.
183
 
184
   The ->type field stores the type of the declaration in this scope;
185
   if NULL, the type is the type of the ->decl field.  This is only of
186
   relevance for objects with external or internal linkage which may
187
   be redeclared in inner scopes, forming composite types that only
188
   persist for the duration of those scopes.  In the external scope,
189
   this stores the composite of all the types declared for this
190
   object, visible or not.  The ->inner_comp field (used only at file
191
   scope) stores whether an incomplete array type at file scope was
192
   completed at an inner scope to an array size other than 1.
193
 
194
   The depth field is copied from the scope structure that holds this
195
   decl.  It is used to preserve the proper ordering of the ->shadowed
196
   field (see bind()) and also for a handful of special-case checks.
197
   Finally, the invisible bit is true for a decl which should be
198
   ignored for purposes of normal name lookup, and the nested bit is
199
   true for a decl that's been bound a second time in an inner scope;
200
   in all such cases, the binding in the outer scope will have its
201
   invisible bit true.  */
202
 
203
struct c_binding GTY((chain_next ("%h.prev")))
204
{
205
  tree decl;                    /* the decl bound */
206
  tree type;                    /* the type in this scope */
207
  tree id;                      /* the identifier it's bound to */
208
  struct c_binding *prev;       /* the previous decl in this scope */
209
  struct c_binding *shadowed;   /* the innermost decl shadowed by this one */
210
  unsigned int depth : 28;      /* depth of this scope */
211
  BOOL_BITFIELD invisible : 1;  /* normal lookup should ignore this binding */
212
  BOOL_BITFIELD nested : 1;     /* do not set DECL_CONTEXT when popping */
213
  BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
214
  /* one free bit */
215
};
216
#define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217
#define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218
#define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219
#define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
220
 
221
#define I_SYMBOL_BINDING(node) \
222
  (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223
#define I_SYMBOL_DECL(node) \
224
 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
225
 
226
#define I_TAG_BINDING(node) \
227
  (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228
#define I_TAG_DECL(node) \
229
 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
230
 
231
#define I_LABEL_BINDING(node) \
232
  (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233
#define I_LABEL_DECL(node) \
234
 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
235
 
236
/* Each C symbol points to three linked lists of c_binding structures.
237
   These describe the values of the identifier in the three different
238
   namespaces defined by the language.  */
239
 
240
struct lang_identifier GTY(())
241
{
242
  struct c_common_identifier common_id;
243
  struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244
  struct c_binding *tag_binding;    /* struct/union/enum tags */
245
  struct c_binding *label_binding;  /* labels */
246
};
247
 
248
/* Validate c-lang.c's assumptions.  */
249
extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250
[(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
251
 
252
/* The resulting tree type.  */
253
 
254
union lang_tree_node
255
  GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256
       chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
257
{
258
  union tree_node GTY ((tag ("0"),
259
                        desc ("tree_node_structure (&%h)")))
260
    generic;
261
  struct lang_identifier GTY ((tag ("1"))) identifier;
262
};
263
 
264
/* Each c_scope structure describes the complete contents of one
265
   scope.  Four scopes are distinguished specially: the innermost or
266
   current scope, the innermost function scope, the file scope (always
267
   the second to outermost) and the outermost or external scope.
268
 
269
   Most declarations are recorded in the current scope.
270
 
271
   All normal label declarations are recorded in the innermost
272
   function scope, as are bindings of undeclared identifiers to
273
   error_mark_node.  (GCC permits nested functions as an extension,
274
   hence the 'innermost' qualifier.)  Explicitly declared labels
275
   (using the __label__ extension) appear in the current scope.
276
 
277
   Being in the file scope (current_scope == file_scope) causes
278
   special behavior in several places below.  Also, under some
279
   conditions the Objective-C front end records declarations in the
280
   file scope even though that isn't the current scope.
281
 
282
   All declarations with external linkage are recorded in the external
283
   scope, even if they aren't visible there; this models the fact that
284
   such declarations are visible to the entire program, and (with a
285
   bit of cleverness, see pushdecl) allows diagnosis of some violations
286
   of C99 6.2.2p7 and 6.2.7p2:
287
 
288
     If, within the same translation unit, the same identifier appears
289
     with both internal and external linkage, the behavior is
290
     undefined.
291
 
292
     All declarations that refer to the same object or function shall
293
     have compatible type; otherwise, the behavior is undefined.
294
 
295
   Initially only the built-in declarations, which describe compiler
296
   intrinsic functions plus a subset of the standard library, are in
297
   this scope.
298
 
299
   The order of the blocks list matters, and it is frequently appended
300
   to.  To avoid having to walk all the way to the end of the list on
301
   each insertion, or reverse the list later, we maintain a pointer to
302
   the last list entry.  (FIXME: It should be feasible to use a reversed
303
   list here.)
304
 
305
   The bindings list is strictly in reverse order of declarations;
306
   pop_scope relies on this.  */
307
 
308
 
309
struct c_scope GTY((chain_next ("%h.outer")))
310
{
311
  /* The scope containing this one.  */
312
  struct c_scope *outer;
313
 
314
  /* The next outermost function scope.  */
315
  struct c_scope *outer_function;
316
 
317
  /* All bindings in this scope.  */
318
  struct c_binding *bindings;
319
 
320
  /* For each scope (except the global one), a chain of BLOCK nodes
321
     for all the scopes that were entered and exited one level down.  */
322
  tree blocks;
323
  tree blocks_last;
324
 
325
  /* The depth of this scope.  Used to keep the ->shadowed chain of
326
     bindings sorted innermost to outermost.  */
327
  unsigned int depth : 28;
328
 
329
  /* True if we are currently filling this scope with parameter
330
     declarations.  */
331
  BOOL_BITFIELD parm_flag : 1;
332
 
333
  /* True if we saw [*] in this scope.  Used to give an error messages
334
     if these appears in a function definition.  */
335
  BOOL_BITFIELD had_vla_unspec : 1;
336
 
337
  /* True if we already complained about forward parameter decls
338
     in this scope.  This prevents double warnings on
339
     foo (int a; int b; ...)  */
340
  BOOL_BITFIELD warned_forward_parm_decls : 1;
341
 
342
  /* True if this is the outermost block scope of a function body.
343
     This scope contains the parameters, the local variables declared
344
     in the outermost block, and all the labels (except those in
345
     nested functions, or declared at block scope with __label__).  */
346
  BOOL_BITFIELD function_body : 1;
347
 
348
  /* True means make a BLOCK for this scope no matter what.  */
349
  BOOL_BITFIELD keep : 1;
350
};
351
 
352
/* The scope currently in effect.  */
353
 
354
static GTY(()) struct c_scope *current_scope;
355
 
356
/* The innermost function scope.  Ordinary (not explicitly declared)
357
   labels, bindings to error_mark_node, and the lazily-created
358
   bindings of __func__ and its friends get this scope.  */
359
 
360
static GTY(()) struct c_scope *current_function_scope;
361
 
362
/* The C file scope.  This is reset for each input translation unit.  */
363
 
364
static GTY(()) struct c_scope *file_scope;
365
 
366
/* The outermost scope.  This is used for all declarations with
367
   external linkage, and only these, hence the name.  */
368
 
369
static GTY(()) struct c_scope *external_scope;
370
 
371
/* A chain of c_scope structures awaiting reuse.  */
372
 
373
static GTY((deletable)) struct c_scope *scope_freelist;
374
 
375
/* A chain of c_binding structures awaiting reuse.  */
376
 
377
static GTY((deletable)) struct c_binding *binding_freelist;
378
 
379
/* Append VAR to LIST in scope SCOPE.  */
380
#define SCOPE_LIST_APPEND(scope, list, decl) do {       \
381
  struct c_scope *s_ = (scope);                         \
382
  tree d_ = (decl);                                     \
383
  if (s_->list##_last)                                  \
384
    TREE_CHAIN (s_->list##_last) = d_;                  \
385
  else                                                  \
386
    s_->list = d_;                                      \
387
  s_->list##_last = d_;                                 \
388
} while (0)
389
 
390
/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
391
#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {        \
392
  struct c_scope *t_ = (tscope);                                \
393
  struct c_scope *f_ = (fscope);                                \
394
  if (t_->to##_last)                                            \
395
    TREE_CHAIN (t_->to##_last) = f_->from;                      \
396
  else                                                          \
397
    t_->to = f_->from;                                          \
398
  t_->to##_last = f_->from##_last;                              \
399
} while (0)
400
 
401
/* True means unconditionally make a BLOCK for the next scope pushed.  */
402
 
403
static bool keep_next_level_flag;
404
 
405
/* True means the next call to push_scope will be the outermost scope
406
   of a function body, so do not push a new scope, merely cease
407
   expecting parameter decls.  */
408
 
409
static bool next_is_function_body;
410
 
411
/* Functions called automatically at the beginning and end of execution.  */
412
 
413
static GTY(()) tree static_ctors;
414
static GTY(()) tree static_dtors;
415
 
416
/* Forward declarations.  */
417
static tree lookup_name_in_scope (tree, struct c_scope *);
418
static tree c_make_fname_decl (tree, int);
419
static tree grokdeclarator (const struct c_declarator *,
420
                            struct c_declspecs *,
421
                            enum decl_context, bool, tree *);
422
static tree grokparms (struct c_arg_info *, bool);
423
static void layout_array_type (tree);
424
 
425
/* T is a statement.  Add it to the statement-tree.  This is the
426
   C/ObjC version--C++ has a slightly different version of this
427
   function.  */
428
 
429
tree
430
add_stmt (tree t)
431
{
432
  enum tree_code code = TREE_CODE (t);
433
 
434
  if (EXPR_P (t) && code != LABEL_EXPR)
435
    {
436
      if (!EXPR_HAS_LOCATION (t))
437
        SET_EXPR_LOCATION (t, input_location);
438
    }
439
 
440
  if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
441
    STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
442
 
443
  /* Add T to the statement-tree.  Non-side-effect statements need to be
444
     recorded during statement expressions.  */
445
  append_to_statement_list_force (t, &cur_stmt_list);
446
 
447
  return t;
448
}
449
 
450
/* States indicating how grokdeclarator() should handle declspecs marked
451
   with __attribute__((deprecated)).  An object declared as
452
   __attribute__((deprecated)) suppresses warnings of uses of other
453
   deprecated items.  */
454
 
455
enum deprecated_states {
456
  DEPRECATED_NORMAL,
457
  DEPRECATED_SUPPRESS
458
};
459
 
460
static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
461
 
462
void
463
c_print_identifier (FILE *file, tree node, int indent)
464
{
465
  print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
466
  print_node (file, "tag", I_TAG_DECL (node), indent + 4);
467
  print_node (file, "label", I_LABEL_DECL (node), indent + 4);
468
  if (C_IS_RESERVED_WORD (node))
469
    {
470
      tree rid = ridpointers[C_RID_CODE (node)];
471
      indent_to (file, indent + 4);
472
      fprintf (file, "rid %p \"%s\"",
473
               (void *) rid, IDENTIFIER_POINTER (rid));
474
    }
475
}
476
 
477
/* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
478
   which may be any of several kinds of DECL or TYPE or error_mark_node,
479
   in the scope SCOPE.  */
480
static void
481
bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
482
{
483
  struct c_binding *b, **here;
484
 
485
  if (binding_freelist)
486
    {
487
      b = binding_freelist;
488
      binding_freelist = b->prev;
489
    }
490
  else
491
    b = GGC_NEW (struct c_binding);
492
 
493
  b->shadowed = 0;
494
  b->decl = decl;
495
  b->id = name;
496
  b->depth = scope->depth;
497
  b->invisible = invisible;
498
  b->nested = nested;
499
  b->inner_comp = 0;
500
 
501
  b->type = 0;
502
 
503
  b->prev = scope->bindings;
504
  scope->bindings = b;
505
 
506
  if (!name)
507
    return;
508
 
509
  switch (TREE_CODE (decl))
510
    {
511
    case LABEL_DECL:     here = &I_LABEL_BINDING (name);   break;
512
    case ENUMERAL_TYPE:
513
    case UNION_TYPE:
514
    case RECORD_TYPE:    here = &I_TAG_BINDING (name);     break;
515
    case VAR_DECL:
516
    case FUNCTION_DECL:
517
    case TYPE_DECL:
518
    case CONST_DECL:
519
    case PARM_DECL:
520
    case ERROR_MARK:     here = &I_SYMBOL_BINDING (name);  break;
521
 
522
    default:
523
      gcc_unreachable ();
524
    }
525
 
526
  /* Locate the appropriate place in the chain of shadowed decls
527
     to insert this binding.  Normally, scope == current_scope and
528
     this does nothing.  */
529
  while (*here && (*here)->depth > scope->depth)
530
    here = &(*here)->shadowed;
531
 
532
  b->shadowed = *here;
533
  *here = b;
534
}
535
 
536
/* Clear the binding structure B, stick it on the binding_freelist,
537
   and return the former value of b->prev.  This is used by pop_scope
538
   and get_parm_info to iterate destructively over all the bindings
539
   from a given scope.  */
540
static struct c_binding *
541
free_binding_and_advance (struct c_binding *b)
542
{
543
  struct c_binding *prev = b->prev;
544
 
545
  memset (b, 0, sizeof (struct c_binding));
546
  b->prev = binding_freelist;
547
  binding_freelist = b;
548
 
549
  return prev;
550
}
551
 
552
 
553
/* Hook called at end of compilation to assume 1 elt
554
   for a file-scope tentative array defn that wasn't complete before.  */
555
 
556
void
557
c_finish_incomplete_decl (tree decl)
558
{
559
  if (TREE_CODE (decl) == VAR_DECL)
560
    {
561
      tree type = TREE_TYPE (decl);
562
      if (type != error_mark_node
563
          && TREE_CODE (type) == ARRAY_TYPE
564
          && !DECL_EXTERNAL (decl)
565
          && TYPE_DOMAIN (type) == 0)
566
        {
567
          warning (0, "array %q+D assumed to have one element", decl);
568
 
569
          complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
570
 
571
          layout_decl (decl, 0);
572
        }
573
    }
574
}
575
 
576
/* The Objective-C front-end often needs to determine the current scope.  */
577
 
578
void *
579
objc_get_current_scope (void)
580
{
581
  return current_scope;
582
}
583
 
584
/* The following function is used only by Objective-C.  It needs to live here
585
   because it accesses the innards of c_scope.  */
586
 
587
void
588
objc_mark_locals_volatile (void *enclosing_blk)
589
{
590
  struct c_scope *scope;
591
  struct c_binding *b;
592
 
593
  for (scope = current_scope;
594
       scope && scope != enclosing_blk;
595
       scope = scope->outer)
596
    {
597
      for (b = scope->bindings; b; b = b->prev)
598
        objc_volatilize_decl (b->decl);
599
 
600
      /* Do not climb up past the current function.  */
601
      if (scope->function_body)
602
        break;
603
    }
604
}
605
 
606
/* Nonzero if we are currently in file scope.  */
607
 
608
int
609
global_bindings_p (void)
610
{
611
  return current_scope == file_scope && !c_override_global_bindings_to_false;
612
}
613
 
614
void
615
keep_next_level (void)
616
{
617
  keep_next_level_flag = true;
618
}
619
 
620
/* Identify this scope as currently being filled with parameters.  */
621
 
622
void
623
declare_parm_level (void)
624
{
625
  current_scope->parm_flag = true;
626
}
627
 
628
void
629
push_scope (void)
630
{
631
  if (next_is_function_body)
632
    {
633
      /* This is the transition from the parameters to the top level
634
         of the function body.  These are the same scope
635
         (C99 6.2.1p4,6) so we do not push another scope structure.
636
         next_is_function_body is set only by store_parm_decls, which
637
         in turn is called when and only when we are about to
638
         encounter the opening curly brace for the function body.
639
 
640
         The outermost block of a function always gets a BLOCK node,
641
         because the debugging output routines expect that each
642
         function has at least one BLOCK.  */
643
      current_scope->parm_flag         = false;
644
      current_scope->function_body     = true;
645
      current_scope->keep              = true;
646
      current_scope->outer_function    = current_function_scope;
647
      current_function_scope           = current_scope;
648
 
649
      keep_next_level_flag = false;
650
      next_is_function_body = false;
651
    }
652
  else
653
    {
654
      struct c_scope *scope;
655
      if (scope_freelist)
656
        {
657
          scope = scope_freelist;
658
          scope_freelist = scope->outer;
659
        }
660
      else
661
        scope = GGC_CNEW (struct c_scope);
662
 
663
      scope->keep          = keep_next_level_flag;
664
      scope->outer         = current_scope;
665
      scope->depth         = current_scope ? (current_scope->depth + 1) : 0;
666
 
667
      /* Check for scope depth overflow.  Unlikely (2^28 == 268,435,456) but
668
         possible.  */
669
      if (current_scope && scope->depth == 0)
670
        {
671
          scope->depth--;
672
          sorry ("GCC supports only %u nested scopes", scope->depth);
673
        }
674
 
675
      current_scope        = scope;
676
      keep_next_level_flag = false;
677
    }
678
}
679
 
680
/* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT.  */
681
 
682
static void
683
set_type_context (tree type, tree context)
684
{
685
  for (type = TYPE_MAIN_VARIANT (type); type;
686
       type = TYPE_NEXT_VARIANT (type))
687
    TYPE_CONTEXT (type) = context;
688
}
689
 
690
/* Exit a scope.  Restore the state of the identifier-decl mappings
691
   that were in effect when this scope was entered.  Return a BLOCK
692
   node containing all the DECLs in this scope that are of interest
693
   to debug info generation.  */
694
 
695
tree
696
pop_scope (void)
697
{
698
  struct c_scope *scope = current_scope;
699
  tree block, context, p;
700
  struct c_binding *b;
701
 
702
  bool functionbody = scope->function_body;
703
  bool keep = functionbody || scope->keep || scope->bindings;
704
 
705
  c_end_vm_scope (scope->depth);
706
 
707
  /* If appropriate, create a BLOCK to record the decls for the life
708
     of this function.  */
709
  block = 0;
710
  if (keep)
711
    {
712
      block = make_node (BLOCK);
713
      BLOCK_SUBBLOCKS (block) = scope->blocks;
714
      TREE_USED (block) = 1;
715
 
716
      /* In each subblock, record that this is its superior.  */
717
      for (p = scope->blocks; p; p = TREE_CHAIN (p))
718
        BLOCK_SUPERCONTEXT (p) = block;
719
 
720
      BLOCK_VARS (block) = 0;
721
    }
722
 
723
  /* The TYPE_CONTEXTs for all of the tagged types belonging to this
724
     scope must be set so that they point to the appropriate
725
     construct, i.e.  either to the current FUNCTION_DECL node, or
726
     else to the BLOCK node we just constructed.
727
 
728
     Note that for tagged types whose scope is just the formal
729
     parameter list for some function type specification, we can't
730
     properly set their TYPE_CONTEXTs here, because we don't have a
731
     pointer to the appropriate FUNCTION_TYPE node readily available
732
     to us.  For those cases, the TYPE_CONTEXTs of the relevant tagged
733
     type nodes get set in `grokdeclarator' as soon as we have created
734
     the FUNCTION_TYPE node which will represent the "scope" for these
735
     "parameter list local" tagged types.  */
736
  if (scope->function_body)
737
    context = current_function_decl;
738
  else if (scope == file_scope)
739
    {
740
      tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
741
      TREE_CHAIN (file_decl) = all_translation_units;
742
      all_translation_units = file_decl;
743
      context = file_decl;
744
    }
745
  else
746
    context = block;
747
 
748
  /* Clear all bindings in this scope.  */
749
  for (b = scope->bindings; b; b = free_binding_and_advance (b))
750
    {
751
      p = b->decl;
752
      switch (TREE_CODE (p))
753
        {
754
        case LABEL_DECL:
755
          /* Warnings for unused labels, errors for undefined labels.  */
756
          if (TREE_USED (p) && !DECL_INITIAL (p))
757
            {
758
              error ("label %q+D used but not defined", p);
759
              DECL_INITIAL (p) = error_mark_node;
760
            }
761
          else if (!TREE_USED (p) && warn_unused_label)
762
            {
763
              if (DECL_INITIAL (p))
764
                warning (0, "label %q+D defined but not used", p);
765
              else
766
                warning (0, "label %q+D declared but not defined", p);
767
            }
768
          /* Labels go in BLOCK_VARS.  */
769
          TREE_CHAIN (p) = BLOCK_VARS (block);
770
          BLOCK_VARS (block) = p;
771
          gcc_assert (I_LABEL_BINDING (b->id) == b);
772
          I_LABEL_BINDING (b->id) = b->shadowed;
773
          break;
774
 
775
        case ENUMERAL_TYPE:
776
        case UNION_TYPE:
777
        case RECORD_TYPE:
778
          set_type_context (p, context);
779
 
780
          /* Types may not have tag-names, in which case the type
781
             appears in the bindings list with b->id NULL.  */
782
          if (b->id)
783
            {
784
              gcc_assert (I_TAG_BINDING (b->id) == b);
785
              I_TAG_BINDING (b->id) = b->shadowed;
786
            }
787
          break;
788
 
789
        case FUNCTION_DECL:
790
          /* Propagate TREE_ADDRESSABLE from nested functions to their
791
             containing functions.  */
792
          if (!TREE_ASM_WRITTEN (p)
793
              && DECL_INITIAL (p) != 0
794
              && TREE_ADDRESSABLE (p)
795
              && DECL_ABSTRACT_ORIGIN (p) != 0
796
              && DECL_ABSTRACT_ORIGIN (p) != p)
797
            TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
798
          if (!DECL_EXTERNAL (p)
799
              && DECL_INITIAL (p) == 0)
800
            {
801
              error ("nested function %q+D declared but never defined", p);
802
              undef_nested_function = true;
803
            }
804
          goto common_symbol;
805
 
806
        case VAR_DECL:
807
          /* Warnings for unused variables.  */
808
          if (!TREE_USED (p)
809
              && !TREE_NO_WARNING (p)
810
              && !DECL_IN_SYSTEM_HEADER (p)
811
              && DECL_NAME (p)
812
              && !DECL_ARTIFICIAL (p)
813
              && scope != file_scope
814
              && scope != external_scope)
815
            warning (OPT_Wunused_variable, "unused variable %q+D", p);
816
 
817
          if (b->inner_comp)
818
            {
819
              error ("type of array %q+D completed incompatibly with"
820
                     " implicit initialization", p);
821
            }
822
 
823
          /* Fall through.  */
824
        case TYPE_DECL:
825
        case CONST_DECL:
826
        common_symbol:
827
          /* All of these go in BLOCK_VARS, but only if this is the
828
             binding in the home scope.  */
829
          if (!b->nested)
830
            {
831
              TREE_CHAIN (p) = BLOCK_VARS (block);
832
              BLOCK_VARS (block) = p;
833
            }
834
          /* If this is the file scope, and we are processing more
835
             than one translation unit in this compilation, set
836
             DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
837
             This makes same_translation_unit_p work, and causes
838
             static declarations to be given disambiguating suffixes.  */
839
          if (scope == file_scope && num_in_fnames > 1)
840
            {
841
              DECL_CONTEXT (p) = context;
842
              if (TREE_CODE (p) == TYPE_DECL)
843
                set_type_context (TREE_TYPE (p), context);
844
            }
845
 
846
          /* Fall through.  */
847
          /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
848
             already been put there by store_parm_decls.  Unused-
849
             parameter warnings are handled by function.c.
850
             error_mark_node obviously does not go in BLOCK_VARS and
851
             does not get unused-variable warnings.  */
852
        case PARM_DECL:
853
        case ERROR_MARK:
854
          /* It is possible for a decl not to have a name.  We get
855
             here with b->id NULL in this case.  */
856
          if (b->id)
857
            {
858
              gcc_assert (I_SYMBOL_BINDING (b->id) == b);
859
              I_SYMBOL_BINDING (b->id) = b->shadowed;
860
              if (b->shadowed && b->shadowed->type)
861
                TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
862
            }
863
          break;
864
 
865
        default:
866
          gcc_unreachable ();
867
        }
868
    }
869
 
870
 
871
  /* Dispose of the block that we just made inside some higher level.  */
872
  if ((scope->function_body || scope == file_scope) && context)
873
    {
874
      DECL_INITIAL (context) = block;
875
      BLOCK_SUPERCONTEXT (block) = context;
876
    }
877
  else if (scope->outer)
878
    {
879
      if (block)
880
        SCOPE_LIST_APPEND (scope->outer, blocks, block);
881
      /* If we did not make a block for the scope just exited, any
882
         blocks made for inner scopes must be carried forward so they
883
         will later become subblocks of something else.  */
884
      else if (scope->blocks)
885
        SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
886
    }
887
 
888
  /* Pop the current scope, and free the structure for reuse.  */
889
  current_scope = scope->outer;
890
  if (scope->function_body)
891
    current_function_scope = scope->outer_function;
892
 
893
  memset (scope, 0, sizeof (struct c_scope));
894
  scope->outer = scope_freelist;
895
  scope_freelist = scope;
896
 
897
  return block;
898
}
899
 
900
void
901
push_file_scope (void)
902
{
903
  tree decl;
904
 
905
  if (file_scope)
906
    return;
907
 
908
  push_scope ();
909
  file_scope = current_scope;
910
 
911
  start_fname_decls ();
912
 
913
  for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
914
    bind (DECL_NAME (decl), decl, file_scope,
915
          /*invisible=*/false, /*nested=*/true);
916
}
917
 
918
void
919
pop_file_scope (void)
920
{
921
  /* In case there were missing closebraces, get us back to the global
922
     binding level.  */
923
  while (current_scope != file_scope)
924
    pop_scope ();
925
 
926
  /* __FUNCTION__ is defined at file scope ("").  This
927
     call may not be necessary as my tests indicate it
928
     still works without it.  */
929
  finish_fname_decls ();
930
 
931
  /* This is the point to write out a PCH if we're doing that.
932
     In that case we do not want to do anything else.  */
933
  if (pch_file)
934
    {
935
      c_common_write_pch ();
936
      return;
937
    }
938
 
939
  /* Pop off the file scope and close this translation unit.  */
940
  pop_scope ();
941
  file_scope = 0;
942
 
943
  maybe_apply_pending_pragma_weaks ();
944
  cgraph_finalize_compilation_unit ();
945
}
946
 
947
/* Insert BLOCK at the end of the list of subblocks of the current
948
   scope.  This is used when a BIND_EXPR is expanded, to handle the
949
   BLOCK node inside the BIND_EXPR.  */
950
 
951
void
952
insert_block (tree block)
953
{
954
  TREE_USED (block) = 1;
955
  SCOPE_LIST_APPEND (current_scope, blocks, block);
956
}
957
 
958
/* Push a definition or a declaration of struct, union or enum tag "name".
959
   "type" should be the type node.
960
   We assume that the tag "name" is not already defined.
961
 
962
   Note that the definition may really be just a forward reference.
963
   In that case, the TYPE_SIZE will be zero.  */
964
 
965
static void
966
pushtag (tree name, tree type)
967
{
968
  /* Record the identifier as the type's name if it has none.  */
969
  if (name && !TYPE_NAME (type))
970
    TYPE_NAME (type) = name;
971
  bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
972
 
973
  /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
974
     tagged type we just added to the current scope.  This fake
975
     NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
976
     to output a representation of a tagged type, and it also gives
977
     us a convenient place to record the "scope start" address for the
978
     tagged type.  */
979
 
980
  TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
981
 
982
  /* An approximation for now, so we can tell this is a function-scope tag.
983
     This will be updated in pop_scope.  */
984
  TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
985
}
986
 
987
/* Subroutine of compare_decls.  Allow harmless mismatches in return
988
   and argument types provided that the type modes match.  This function
989
   return a unified type given a suitable match, and 0 otherwise.  */
990
 
991
static tree
992
match_builtin_function_types (tree newtype, tree oldtype)
993
{
994
  tree newrettype, oldrettype;
995
  tree newargs, oldargs;
996
  tree trytype, tryargs;
997
 
998
  /* Accept the return type of the new declaration if same modes.  */
999
  oldrettype = TREE_TYPE (oldtype);
1000
  newrettype = TREE_TYPE (newtype);
1001
 
1002
  if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1003
    return 0;
1004
 
1005
  oldargs = TYPE_ARG_TYPES (oldtype);
1006
  newargs = TYPE_ARG_TYPES (newtype);
1007
  tryargs = newargs;
1008
 
1009
  while (oldargs || newargs)
1010
    {
1011
      if (!oldargs
1012
          || !newargs
1013
          || !TREE_VALUE (oldargs)
1014
          || !TREE_VALUE (newargs)
1015
          || TYPE_MODE (TREE_VALUE (oldargs))
1016
             != TYPE_MODE (TREE_VALUE (newargs)))
1017
        return 0;
1018
 
1019
      oldargs = TREE_CHAIN (oldargs);
1020
      newargs = TREE_CHAIN (newargs);
1021
    }
1022
 
1023
  trytype = build_function_type (newrettype, tryargs);
1024
  return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1025
}
1026
 
1027
/* Subroutine of diagnose_mismatched_decls.  Check for function type
1028
   mismatch involving an empty arglist vs a nonempty one and give clearer
1029
   diagnostics.  */
1030
static void
1031
diagnose_arglist_conflict (tree newdecl, tree olddecl,
1032
                           tree newtype, tree oldtype)
1033
{
1034
  tree t;
1035
 
1036
  if (TREE_CODE (olddecl) != FUNCTION_DECL
1037
      || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1038
      || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1039
           ||
1040
           (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1041
    return;
1042
 
1043
  t = TYPE_ARG_TYPES (oldtype);
1044
  if (t == 0)
1045
    t = TYPE_ARG_TYPES (newtype);
1046
  for (; t; t = TREE_CHAIN (t))
1047
    {
1048
      tree type = TREE_VALUE (t);
1049
 
1050
      if (TREE_CHAIN (t) == 0
1051
          && TYPE_MAIN_VARIANT (type) != void_type_node)
1052
        {
1053
          inform ("a parameter list with an ellipsis can%'t match "
1054
                  "an empty parameter name list declaration");
1055
          break;
1056
        }
1057
 
1058
      if (c_type_promotes_to (type) != type)
1059
        {
1060
          inform ("an argument type that has a default promotion can%'t match "
1061
                  "an empty parameter name list declaration");
1062
          break;
1063
        }
1064
    }
1065
}
1066
 
1067
/* Another subroutine of diagnose_mismatched_decls.  OLDDECL is an
1068
   old-style function definition, NEWDECL is a prototype declaration.
1069
   Diagnose inconsistencies in the argument list.  Returns TRUE if
1070
   the prototype is compatible, FALSE if not.  */
1071
static bool
1072
validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1073
{
1074
  tree newargs, oldargs;
1075
  int i;
1076
 
1077
#define END_OF_ARGLIST(t) ((t) == void_type_node)
1078
 
1079
  oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1080
  newargs = TYPE_ARG_TYPES (newtype);
1081
  i = 1;
1082
 
1083
  for (;;)
1084
    {
1085
      tree oldargtype = TREE_VALUE (oldargs);
1086
      tree newargtype = TREE_VALUE (newargs);
1087
 
1088
      if (oldargtype == error_mark_node || newargtype == error_mark_node)
1089
        return false;
1090
 
1091
      oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1092
      newargtype = TYPE_MAIN_VARIANT (newargtype);
1093
 
1094
      if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1095
        break;
1096
 
1097
      /* Reaching the end of just one list means the two decls don't
1098
         agree on the number of arguments.  */
1099
      if (END_OF_ARGLIST (oldargtype))
1100
        {
1101
          error ("prototype for %q+D declares more arguments "
1102
                 "than previous old-style definition", newdecl);
1103
          return false;
1104
        }
1105
      else if (END_OF_ARGLIST (newargtype))
1106
        {
1107
          error ("prototype for %q+D declares fewer arguments "
1108
                 "than previous old-style definition", newdecl);
1109
          return false;
1110
        }
1111
 
1112
      /* Type for passing arg must be consistent with that declared
1113
         for the arg.  */
1114
      else if (!comptypes (oldargtype, newargtype))
1115
        {
1116
          error ("prototype for %q+D declares argument %d"
1117
                 " with incompatible type",
1118
                 newdecl, i);
1119
          return false;
1120
        }
1121
 
1122
      oldargs = TREE_CHAIN (oldargs);
1123
      newargs = TREE_CHAIN (newargs);
1124
      i++;
1125
    }
1126
 
1127
  /* If we get here, no errors were found, but do issue a warning
1128
     for this poor-style construct.  */
1129
  warning (0, "prototype for %q+D follows non-prototype definition",
1130
           newdecl);
1131
  return true;
1132
#undef END_OF_ARGLIST
1133
}
1134
 
1135
/* Subroutine of diagnose_mismatched_decls.  Report the location of DECL,
1136
   first in a pair of mismatched declarations, using the diagnostic
1137
   function DIAG.  */
1138
static void
1139
locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1140
{
1141
  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1142
    ;
1143
  else if (DECL_INITIAL (decl))
1144
    diag (G_("previous definition of %q+D was here"), decl);
1145
  else if (C_DECL_IMPLICIT (decl))
1146
    diag (G_("previous implicit declaration of %q+D was here"), decl);
1147
  else
1148
    diag (G_("previous declaration of %q+D was here"), decl);
1149
}
1150
 
1151
/* Subroutine of duplicate_decls.  Compare NEWDECL to OLDDECL.
1152
   Returns true if the caller should proceed to merge the two, false
1153
   if OLDDECL should simply be discarded.  As a side effect, issues
1154
   all necessary diagnostics for invalid or poor-style combinations.
1155
   If it returns true, writes the types of NEWDECL and OLDDECL to
1156
   *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1157
   TREE_TYPE (NEWDECL, OLDDECL) respectively.  */
1158
 
1159
static bool
1160
diagnose_mismatched_decls (tree newdecl, tree olddecl,
1161
                           tree *newtypep, tree *oldtypep)
1162
{
1163
  tree newtype, oldtype;
1164
  bool pedwarned = false;
1165
  bool warned = false;
1166
  bool retval = true;
1167
 
1168
#define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL)  \
1169
                                  && DECL_EXTERNAL (DECL))
1170
 
1171
  /* If we have error_mark_node for either decl or type, just discard
1172
     the previous decl - we're in an error cascade already.  */
1173
  if (olddecl == error_mark_node || newdecl == error_mark_node)
1174
    return false;
1175
  *oldtypep = oldtype = TREE_TYPE (olddecl);
1176
  *newtypep = newtype = TREE_TYPE (newdecl);
1177
  if (oldtype == error_mark_node || newtype == error_mark_node)
1178
    return false;
1179
 
1180
  /* Two different categories of symbol altogether.  This is an error
1181
     unless OLDDECL is a builtin.  OLDDECL will be discarded in any case.  */
1182
  if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1183
    {
1184
      if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1185
            && DECL_BUILT_IN (olddecl)
1186
            && !C_DECL_DECLARED_BUILTIN (olddecl)))
1187
        {
1188
          error ("%q+D redeclared as different kind of symbol", newdecl);
1189
          locate_old_decl (olddecl, error);
1190
        }
1191
      else if (TREE_PUBLIC (newdecl))
1192
        warning (0, "built-in function %q+D declared as non-function",
1193
                 newdecl);
1194
      else
1195
        warning (OPT_Wshadow, "declaration of %q+D shadows "
1196
                 "a built-in function", newdecl);
1197
      return false;
1198
    }
1199
 
1200
  /* Enumerators have no linkage, so may only be declared once in a
1201
     given scope.  */
1202
  if (TREE_CODE (olddecl) == CONST_DECL)
1203
    {
1204
      error ("redeclaration of enumerator %q+D", newdecl);
1205
      locate_old_decl (olddecl, error);
1206
      return false;
1207
    }
1208
 
1209
  if (!comptypes (oldtype, newtype))
1210
    {
1211
      if (TREE_CODE (olddecl) == FUNCTION_DECL
1212
          && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1213
        {
1214
          /* Accept harmless mismatch in function types.
1215
             This is for the ffs and fprintf builtins.  */
1216
          tree trytype = match_builtin_function_types (newtype, oldtype);
1217
 
1218
          if (trytype && comptypes (newtype, trytype))
1219
            *oldtypep = oldtype = trytype;
1220
          else
1221
            {
1222
              /* If types don't match for a built-in, throw away the
1223
                 built-in.  No point in calling locate_old_decl here, it
1224
                 won't print anything.  */
1225
              warning (0, "conflicting types for built-in function %q+D",
1226
                       newdecl);
1227
              return false;
1228
            }
1229
        }
1230
      else if (TREE_CODE (olddecl) == FUNCTION_DECL
1231
               && DECL_IS_BUILTIN (olddecl))
1232
        {
1233
          /* A conflicting function declaration for a predeclared
1234
             function that isn't actually built in.  Objective C uses
1235
             these.  The new declaration silently overrides everything
1236
             but the volatility (i.e. noreturn) indication.  See also
1237
             below.  FIXME: Make Objective C use normal builtins.  */
1238
          TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1239
          return false;
1240
        }
1241
      /* Permit void foo (...) to match int foo (...) if the latter is
1242
         the definition and implicit int was used.  See
1243
         c-torture/compile/920625-2.c.  */
1244
      else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1245
               && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1246
               && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1247
               && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1248
        {
1249
          pedwarn ("conflicting types for %q+D", newdecl);
1250
          /* Make sure we keep void as the return type.  */
1251
          TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1252
          C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1253
          pedwarned = true;
1254
        }
1255
      /* Permit void foo (...) to match an earlier call to foo (...) with
1256
         no declared type (thus, implicitly int).  */
1257
      else if (TREE_CODE (newdecl) == FUNCTION_DECL
1258
               && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1259
               && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1260
               && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1261
        {
1262
          pedwarn ("conflicting types for %q+D", newdecl);
1263
          /* Make sure we keep void as the return type.  */
1264
          TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1265
          pedwarned = true;
1266
        }
1267
      else
1268
        {
1269
          if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1270
            error ("conflicting type qualifiers for %q+D", newdecl);
1271
          else
1272
            error ("conflicting types for %q+D", newdecl);
1273
          diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1274
          locate_old_decl (olddecl, error);
1275
          return false;
1276
        }
1277
    }
1278
 
1279
  /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1280
     but silently ignore the redeclaration if either is in a system
1281
     header.  (Conflicting redeclarations were handled above.)  */
1282
  if (TREE_CODE (newdecl) == TYPE_DECL)
1283
    {
1284
      if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1285
        return true;  /* Allow OLDDECL to continue in use.  */
1286
 
1287
      error ("redefinition of typedef %q+D", newdecl);
1288
      locate_old_decl (olddecl, error);
1289
      return false;
1290
    }
1291
 
1292
  /* Function declarations can either be 'static' or 'extern' (no
1293
     qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1294
     can never conflict with each other on account of linkage (6.2.2p4).
1295
     Multiple definitions are not allowed (6.9p3,5) but GCC permits
1296
     two definitions if one is 'extern inline' and one is not.  The non-
1297
     extern-inline definition supersedes the extern-inline definition.  */
1298
 
1299
  else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1300
    {
1301
      /* If you declare a built-in function name as static, or
1302
         define the built-in with an old-style definition (so we
1303
         can't validate the argument list) the built-in definition is
1304
         overridden, but optionally warn this was a bad choice of name.  */
1305
      if (DECL_BUILT_IN (olddecl)
1306
          && !C_DECL_DECLARED_BUILTIN (olddecl)
1307
          && (!TREE_PUBLIC (newdecl)
1308
              || (DECL_INITIAL (newdecl)
1309
                  && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1310
        {
1311
          warning (OPT_Wshadow, "declaration of %q+D shadows "
1312
                   "a built-in function", newdecl);
1313
          /* Discard the old built-in function.  */
1314
          return false;
1315
        }
1316
 
1317
      if (DECL_INITIAL (newdecl))
1318
        {
1319
          if (DECL_INITIAL (olddecl))
1320
            {
1321
              /* If both decls are in the same TU and the new declaration
1322
                 isn't overriding an extern inline reject the new decl.
1323
                 When we handle c99 style inline rules we'll want to reject
1324
                 the following:
1325
 
1326
                 DECL_EXTERN_INLINE (olddecl)
1327
                 && !DECL_EXTERN_INLINE (newdecl)
1328
 
1329
                 if they're in the same translation unit. Until we implement
1330
                 the full semantics we accept the construct.  */
1331
              if (!(DECL_EXTERN_INLINE (olddecl)
1332
                    && !DECL_EXTERN_INLINE (newdecl))
1333
                  && same_translation_unit_p (newdecl, olddecl))
1334
                {
1335
                  error ("redefinition of %q+D", newdecl);
1336
                  locate_old_decl (olddecl, error);
1337
                  return false;
1338
                }
1339
            }
1340
        }
1341
      /* If we have a prototype after an old-style function definition,
1342
         the argument types must be checked specially.  */
1343
      else if (DECL_INITIAL (olddecl)
1344
               && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1345
               && TYPE_ACTUAL_ARG_TYPES (oldtype)
1346
               && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1347
        {
1348
          locate_old_decl (olddecl, error);
1349
          return false;
1350
        }
1351
      /* A non-static declaration (even an "extern") followed by a
1352
         static declaration is undefined behavior per C99 6.2.2p3-5,7.
1353
         The same is true for a static forward declaration at block
1354
         scope followed by a non-static declaration/definition at file
1355
         scope.  Static followed by non-static at the same scope is
1356
         not undefined behavior, and is the most convenient way to get
1357
         some effects (see e.g.  what unwind-dw2-fde-glibc.c does to
1358
         the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1359
         we do diagnose it if -Wtraditional.  */
1360
      if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1361
        {
1362
          /* Two exceptions to the rule.  If olddecl is an extern
1363
             inline, or a predeclared function that isn't actually
1364
             built in, newdecl silently overrides olddecl.  The latter
1365
             occur only in Objective C; see also above.  (FIXME: Make
1366
             Objective C use normal builtins.)  */
1367
          if (!DECL_IS_BUILTIN (olddecl)
1368
              && !DECL_EXTERN_INLINE (olddecl))
1369
            {
1370
              error ("static declaration of %q+D follows "
1371
                     "non-static declaration", newdecl);
1372
              locate_old_decl (olddecl, error);
1373
            }
1374
          return false;
1375
        }
1376
      else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1377
        {
1378
          if (DECL_CONTEXT (olddecl))
1379
            {
1380
              error ("non-static declaration of %q+D follows "
1381
                     "static declaration", newdecl);
1382
              locate_old_decl (olddecl, error);
1383
              return false;
1384
            }
1385
          else if (warn_traditional)
1386
            {
1387
              warning (OPT_Wtraditional, "non-static declaration of %q+D "
1388
                       "follows static declaration", newdecl);
1389
              warned = true;
1390
            }
1391
        }
1392
    }
1393
  else if (TREE_CODE (newdecl) == VAR_DECL)
1394
    {
1395
      /* Only variables can be thread-local, and all declarations must
1396
         agree on this property.  */
1397
      if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1398
        {
1399
          /* Nothing to check.  Since OLDDECL is marked threadprivate
1400
             and NEWDECL does not have a thread-local attribute, we
1401
             will merge the threadprivate attribute into NEWDECL.  */
1402
          ;
1403
        }
1404
      else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1405
        {
1406
          if (DECL_THREAD_LOCAL_P (newdecl))
1407
            error ("thread-local declaration of %q+D follows "
1408
                   "non-thread-local declaration", newdecl);
1409
          else
1410
            error ("non-thread-local declaration of %q+D follows "
1411
                   "thread-local declaration", newdecl);
1412
 
1413
          locate_old_decl (olddecl, error);
1414
          return false;
1415
        }
1416
 
1417
      /* Multiple initialized definitions are not allowed (6.9p3,5).  */
1418
      if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1419
        {
1420
          error ("redefinition of %q+D", newdecl);
1421
          locate_old_decl (olddecl, error);
1422
          return false;
1423
        }
1424
 
1425
      /* Objects declared at file scope: if the first declaration had
1426
         external linkage (even if it was an external reference) the
1427
         second must have external linkage as well, or the behavior is
1428
         undefined.  If the first declaration had internal linkage, then
1429
         the second must too, or else be an external reference (in which
1430
         case the composite declaration still has internal linkage).
1431
         As for function declarations, we warn about the static-then-
1432
         extern case only for -Wtraditional.  See generally 6.2.2p3-5,7.  */
1433
      if (DECL_FILE_SCOPE_P (newdecl)
1434
          && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1435
        {
1436
          if (DECL_EXTERNAL (newdecl))
1437
            {
1438
              if (!DECL_FILE_SCOPE_P (olddecl))
1439
                {
1440
                  error ("extern declaration of %q+D follows "
1441
                         "declaration with no linkage", newdecl);
1442
                  locate_old_decl (olddecl, error);
1443
                  return false;
1444
                }
1445
              else if (warn_traditional)
1446
                {
1447
                  warning (OPT_Wtraditional, "non-static declaration of %q+D "
1448
                           "follows static declaration", newdecl);
1449
                  warned = true;
1450
                }
1451
            }
1452
          else
1453
            {
1454
              if (TREE_PUBLIC (newdecl))
1455
                error ("non-static declaration of %q+D follows "
1456
                       "static declaration", newdecl);
1457
              else
1458
                error ("static declaration of %q+D follows "
1459
                       "non-static declaration", newdecl);
1460
 
1461
              locate_old_decl (olddecl, error);
1462
              return false;
1463
            }
1464
        }
1465
      /* Two objects with the same name declared at the same block
1466
         scope must both be external references (6.7p3).  */
1467
      else if (!DECL_FILE_SCOPE_P (newdecl))
1468
        {
1469
          if (DECL_EXTERNAL (newdecl))
1470
            {
1471
              /* Extern with initializer at block scope, which will
1472
                 already have received an error.  */
1473
            }
1474
          else if (DECL_EXTERNAL (olddecl))
1475
            {
1476
              error ("declaration of %q+D with no linkage follows "
1477
                     "extern declaration", newdecl);
1478
              locate_old_decl (olddecl, error);
1479
            }
1480
          else
1481
            {
1482
              error ("redeclaration of %q+D with no linkage", newdecl);
1483
              locate_old_decl (olddecl, error);
1484
            }
1485
 
1486
          return false;
1487
        }
1488
    }
1489
 
1490
  /* warnings */
1491
  /* All decls must agree on a visibility.  */
1492
  if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1493
      && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1494
      && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1495
    {
1496
      warning (0, "redeclaration of %q+D with different visibility "
1497
               "(old visibility preserved)", newdecl);
1498
      warned = true;
1499
    }
1500
 
1501
  if (TREE_CODE (newdecl) == FUNCTION_DECL)
1502
    {
1503
      /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
1504
      if (DECL_DECLARED_INLINE_P (newdecl)
1505
          && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1506
        {
1507
          warning (OPT_Wattributes, "inline declaration of %qD follows "
1508
                   "declaration with attribute noinline", newdecl);
1509
          warned = true;
1510
        }
1511
      else if (DECL_DECLARED_INLINE_P (olddecl)
1512
               && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1513
        {
1514
          warning (OPT_Wattributes, "declaration of %q+D with attribute "
1515
                   "noinline follows inline declaration ", newdecl);
1516
          warned = true;
1517
        }
1518
 
1519
      /* Inline declaration after use or definition.
1520
         ??? Should we still warn about this now we have unit-at-a-time
1521
         mode and can get it right?
1522
         Definitely don't complain if the decls are in different translation
1523
         units.  */
1524
      if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1525
          && same_translation_unit_p (olddecl, newdecl))
1526
        {
1527
          if (TREE_USED (olddecl))
1528
            {
1529
              warning (0, "%q+D declared inline after being called", olddecl);
1530
              warned = true;
1531
            }
1532
          else if (DECL_INITIAL (olddecl))
1533
            {
1534
              warning (0, "%q+D declared inline after its definition", olddecl);
1535
              warned = true;
1536
            }
1537
        }
1538
    }
1539
  else /* PARM_DECL, VAR_DECL */
1540
    {
1541
      /* Redeclaration of a parameter is a constraint violation (this is
1542
         not explicitly stated, but follows from C99 6.7p3 [no more than
1543
         one declaration of the same identifier with no linkage in the
1544
         same scope, except type tags] and 6.2.2p6 [parameters have no
1545
         linkage]).  We must check for a forward parameter declaration,
1546
         indicated by TREE_ASM_WRITTEN on the old declaration - this is
1547
         an extension, the mandatory diagnostic for which is handled by
1548
         mark_forward_parm_decls.  */
1549
 
1550
      if (TREE_CODE (newdecl) == PARM_DECL
1551
          && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1552
        {
1553
          error ("redefinition of parameter %q+D", newdecl);
1554
          locate_old_decl (olddecl, error);
1555
          return false;
1556
        }
1557
    }
1558
 
1559
  /* Optional warning for completely redundant decls.  */
1560
  if (!warned && !pedwarned
1561
      && warn_redundant_decls
1562
      /* Don't warn about a function declaration followed by a
1563
         definition.  */
1564
      && !(TREE_CODE (newdecl) == FUNCTION_DECL
1565
           && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1566
      /* Don't warn about redundant redeclarations of builtins.  */
1567
      && !(TREE_CODE (newdecl) == FUNCTION_DECL
1568
           && !DECL_BUILT_IN (newdecl)
1569
           && DECL_BUILT_IN (olddecl)
1570
           && !C_DECL_DECLARED_BUILTIN (olddecl))
1571
      /* Don't warn about an extern followed by a definition.  */
1572
      && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1573
      /* Don't warn about forward parameter decls.  */
1574
      && !(TREE_CODE (newdecl) == PARM_DECL
1575
           && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1576
      /* Don't warn about a variable definition following a declaration.  */
1577
      && !(TREE_CODE (newdecl) == VAR_DECL
1578
           && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1579
    {
1580
      warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1581
               newdecl);
1582
      warned = true;
1583
    }
1584
 
1585
  /* Report location of previous decl/defn in a consistent manner.  */
1586
  if (warned || pedwarned)
1587
    locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1588
 
1589
#undef DECL_EXTERN_INLINE
1590
 
1591
  return retval;
1592
}
1593
 
1594
/* Subroutine of duplicate_decls.  NEWDECL has been found to be
1595
   consistent with OLDDECL, but carries new information.  Merge the
1596
   new information into OLDDECL.  This function issues no
1597
   diagnostics.  */
1598
 
1599
static void
1600
merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1601
{
1602
  int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1603
                           && DECL_INITIAL (newdecl) != 0);
1604
  int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1605
                          && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1606
  int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1607
                          && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1608
 
1609
  /* For real parm decl following a forward decl, rechain the old decl
1610
     in its new location and clear TREE_ASM_WRITTEN (it's not a
1611
     forward decl anymore).  */
1612
  if (TREE_CODE (newdecl) == PARM_DECL
1613
      && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1614
    {
1615
      struct c_binding *b, **here;
1616
 
1617
      for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1618
        if ((*here)->decl == olddecl)
1619
          goto found;
1620
      gcc_unreachable ();
1621
 
1622
    found:
1623
      b = *here;
1624
      *here = b->prev;
1625
      b->prev = current_scope->bindings;
1626
      current_scope->bindings = b;
1627
 
1628
      TREE_ASM_WRITTEN (olddecl) = 0;
1629
    }
1630
 
1631
  DECL_ATTRIBUTES (newdecl)
1632
    = targetm.merge_decl_attributes (olddecl, newdecl);
1633
 
1634
  /* Merge the data types specified in the two decls.  */
1635
  TREE_TYPE (newdecl)
1636
    = TREE_TYPE (olddecl)
1637
    = composite_type (newtype, oldtype);
1638
 
1639
  /* Lay the type out, unless already done.  */
1640
  if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1641
    {
1642
      if (TREE_TYPE (newdecl) != error_mark_node)
1643
        layout_type (TREE_TYPE (newdecl));
1644
      if (TREE_CODE (newdecl) != FUNCTION_DECL
1645
          && TREE_CODE (newdecl) != TYPE_DECL
1646
          && TREE_CODE (newdecl) != CONST_DECL)
1647
        layout_decl (newdecl, 0);
1648
    }
1649
  else
1650
    {
1651
      /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1652
      DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1653
      DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1654
      DECL_MODE (newdecl) = DECL_MODE (olddecl);
1655
      if (TREE_CODE (olddecl) != FUNCTION_DECL)
1656
        if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1657
          {
1658
            DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1659
            DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1660
          }
1661
    }
1662
 
1663
 
1664
  /* Merge the type qualifiers.  */
1665
  if (TREE_READONLY (newdecl))
1666
    TREE_READONLY (olddecl) = 1;
1667
 
1668
  if (TREE_THIS_VOLATILE (newdecl))
1669
    TREE_THIS_VOLATILE (olddecl) = 1;
1670
 
1671
  /* Merge deprecatedness.  */
1672
  if (TREE_DEPRECATED (newdecl))
1673
    TREE_DEPRECATED (olddecl) = 1;
1674
 
1675
  /* Keep source location of definition rather than declaration and of
1676
     prototype rather than non-prototype unless that prototype is
1677
     built-in.  */
1678
  if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1679
      || (old_is_prototype && !new_is_prototype
1680
          && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1681
    DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1682
 
1683
  /* Merge the initialization information.  */
1684
   if (DECL_INITIAL (newdecl) == 0)
1685
    DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1686
 
1687
  /* Merge the threadprivate attribute.  */
1688
  if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1689
    {
1690
      DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1691
      C_DECL_THREADPRIVATE_P (newdecl) = 1;
1692
    }
1693
 
1694
  if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1695
    {
1696
      /* Merge the unused-warning information.  */
1697
      if (DECL_IN_SYSTEM_HEADER (olddecl))
1698
        DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1699
      else if (DECL_IN_SYSTEM_HEADER (newdecl))
1700
        DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1701
 
1702
      /* Merge the section attribute.
1703
         We want to issue an error if the sections conflict but that
1704
         must be done later in decl_attributes since we are called
1705
         before attributes are assigned.  */
1706
      if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1707
        DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1708
 
1709
      /* Copy the assembler name.
1710
         Currently, it can only be defined in the prototype.  */
1711
      COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1712
 
1713
      /* Use visibility of whichever declaration had it specified */
1714
      if (DECL_VISIBILITY_SPECIFIED (olddecl))
1715
        {
1716
          DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1717
          DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1718
        }
1719
 
1720
      if (TREE_CODE (newdecl) == FUNCTION_DECL)
1721
        {
1722
          DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1723
          DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1724
          DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1725
          DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1726
            |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1727
          TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1728
          TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1729
          DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1730
          DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1731
          DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1732
        }
1733
 
1734
      /* Merge the storage class information.  */
1735
      merge_weak (newdecl, olddecl);
1736
 
1737
      /* For functions, static overrides non-static.  */
1738
      if (TREE_CODE (newdecl) == FUNCTION_DECL)
1739
        {
1740
          TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1741
          /* This is since we don't automatically
1742
             copy the attributes of NEWDECL into OLDDECL.  */
1743
          TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1744
          /* If this clears `static', clear it in the identifier too.  */
1745
          if (!TREE_PUBLIC (olddecl))
1746
            TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1747
        }
1748
    }
1749
 
1750
  if (DECL_EXTERNAL (newdecl))
1751
    {
1752
      TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1753
      DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1754
 
1755
      /* An extern decl does not override previous storage class.  */
1756
      TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1757
      if (!DECL_EXTERNAL (newdecl))
1758
        {
1759
          DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1760
          DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1761
        }
1762
    }
1763
  else
1764
    {
1765
      TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1766
      TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1767
    }
1768
 
1769
  if (TREE_CODE (newdecl) == FUNCTION_DECL)
1770
    {
1771
      /* If we're redefining a function previously defined as extern
1772
         inline, make sure we emit debug info for the inline before we
1773
         throw it away, in case it was inlined into a function that
1774
         hasn't been written out yet.  */
1775
      if (new_is_definition && DECL_INITIAL (olddecl))
1776
        {
1777
          if (TREE_USED (olddecl)
1778
              /* In unit-at-a-time mode we never inline re-defined extern
1779
                 inline functions.  */
1780
              && !flag_unit_at_a_time
1781
              && cgraph_function_possibly_inlined_p (olddecl))
1782
            (*debug_hooks->outlining_inline_function) (olddecl);
1783
 
1784
          /* The new defn must not be inline.  */
1785
          DECL_INLINE (newdecl) = 0;
1786
          DECL_UNINLINABLE (newdecl) = 1;
1787
        }
1788
      else
1789
        {
1790
          /* If either decl says `inline', this fn is inline, unless
1791
             its definition was passed already.  */
1792
          if (DECL_DECLARED_INLINE_P (newdecl)
1793
              || DECL_DECLARED_INLINE_P (olddecl))
1794
            DECL_DECLARED_INLINE_P (newdecl) = 1;
1795
 
1796
          DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1797
            = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1798
        }
1799
 
1800
      if (DECL_BUILT_IN (olddecl))
1801
        {
1802
          /* If redeclaring a builtin function, it stays built in.
1803
             But it gets tagged as having been declared.  */
1804
          DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1805
          DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1806
          C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1807
          if (new_is_prototype)
1808
            C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1809
          else
1810
            C_DECL_BUILTIN_PROTOTYPE (newdecl)
1811
              = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1812
        }
1813
 
1814
      /* Also preserve various other info from the definition.  */
1815
      if (!new_is_definition)
1816
        {
1817
          DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1818
          DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1819
          DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1820
          DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1821
          DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1822
 
1823
          /* Set DECL_INLINE on the declaration if we've got a body
1824
             from which to instantiate.  */
1825
          if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1826
            {
1827
              DECL_INLINE (newdecl) = 1;
1828
              DECL_ABSTRACT_ORIGIN (newdecl)
1829
                = DECL_ABSTRACT_ORIGIN (olddecl);
1830
            }
1831
        }
1832
      else
1833
        {
1834
          /* If a previous declaration said inline, mark the
1835
             definition as inlinable.  */
1836
          if (DECL_DECLARED_INLINE_P (newdecl)
1837
              && !DECL_UNINLINABLE (newdecl))
1838
            DECL_INLINE (newdecl) = 1;
1839
        }
1840
    }
1841
 
1842
  /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1843
     But preserve OLDDECL's DECL_UID and DECL_CONTEXT.  */
1844
  {
1845
    unsigned olddecl_uid = DECL_UID (olddecl);
1846
    tree olddecl_context = DECL_CONTEXT (olddecl);
1847
 
1848
    memcpy ((char *) olddecl + sizeof (struct tree_common),
1849
            (char *) newdecl + sizeof (struct tree_common),
1850
            sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1851
    switch (TREE_CODE (olddecl))
1852
      {
1853
      case FIELD_DECL:
1854
      case VAR_DECL:
1855
      case PARM_DECL:
1856
      case LABEL_DECL:
1857
      case RESULT_DECL:
1858
      case CONST_DECL:
1859
      case TYPE_DECL:
1860
      case FUNCTION_DECL:
1861
        memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1862
                (char *) newdecl + sizeof (struct tree_decl_common),
1863
                tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1864
        break;
1865
 
1866
      default:
1867
 
1868
        memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1869
                (char *) newdecl + sizeof (struct tree_decl_common),
1870
                sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1871
      }
1872
    DECL_UID (olddecl) = olddecl_uid;
1873
    DECL_CONTEXT (olddecl) = olddecl_context;
1874
  }
1875
 
1876
  /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1877
     so that encode_section_info has a chance to look at the new decl
1878
     flags and attributes.  */
1879
  if (DECL_RTL_SET_P (olddecl)
1880
      && (TREE_CODE (olddecl) == FUNCTION_DECL
1881
          || (TREE_CODE (olddecl) == VAR_DECL
1882
              && TREE_STATIC (olddecl))))
1883
    make_decl_rtl (olddecl);
1884
}
1885
 
1886
/* Handle when a new declaration NEWDECL has the same name as an old
1887
   one OLDDECL in the same binding contour.  Prints an error message
1888
   if appropriate.
1889
 
1890
   If safely possible, alter OLDDECL to look like NEWDECL, and return
1891
   true.  Otherwise, return false.  */
1892
 
1893
static bool
1894
duplicate_decls (tree newdecl, tree olddecl)
1895
{
1896
  tree newtype = NULL, oldtype = NULL;
1897
 
1898
  if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1899
    {
1900
      /* Avoid `unused variable' and other warnings warnings for OLDDECL.  */
1901
      TREE_NO_WARNING (olddecl) = 1;
1902
      return false;
1903
    }
1904
 
1905
  merge_decls (newdecl, olddecl, newtype, oldtype);
1906
  return true;
1907
}
1908
 
1909
 
1910
/* Check whether decl-node NEW_DECL shadows an existing declaration.  */
1911
static void
1912
warn_if_shadowing (tree new_decl)
1913
{
1914
  struct c_binding *b;
1915
 
1916
  /* Shadow warnings wanted?  */
1917
  if (!warn_shadow
1918
      /* No shadow warnings for internally generated vars.  */
1919
      || DECL_IS_BUILTIN (new_decl)
1920
      /* No shadow warnings for vars made for inlining.  */
1921
      || DECL_FROM_INLINE (new_decl))
1922
    return;
1923
 
1924
  /* Is anything being shadowed?  Invisible decls do not count.  */
1925
  for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1926
    if (b->decl && b->decl != new_decl && !b->invisible)
1927
      {
1928
        tree old_decl = b->decl;
1929
 
1930
        if (old_decl == error_mark_node)
1931
          {
1932
            warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1933
                     "non-variable", new_decl);
1934
            break;
1935
          }
1936
        else if (TREE_CODE (old_decl) == PARM_DECL)
1937
          warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1938
                   new_decl);
1939
        else if (DECL_FILE_SCOPE_P (old_decl))
1940
          warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1941
                   "declaration", new_decl);
1942
        else if (TREE_CODE (old_decl) == FUNCTION_DECL
1943
                 && DECL_BUILT_IN (old_decl))
1944
          {
1945
            warning (OPT_Wshadow, "declaration of %q+D shadows "
1946
                     "a built-in function", new_decl);
1947
            break;
1948
          }
1949
        else
1950
          warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1951
                   new_decl);
1952
 
1953
        warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1954
 
1955
        break;
1956
      }
1957
}
1958
 
1959
 
1960
/* Subroutine of pushdecl.
1961
 
1962
   X is a TYPE_DECL for a typedef statement.  Create a brand new
1963
   ..._TYPE node (which will be just a variant of the existing
1964
   ..._TYPE node with identical properties) and then install X
1965
   as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1966
 
1967
   The whole point here is to end up with a situation where each
1968
   and every ..._TYPE node the compiler creates will be uniquely
1969
   associated with AT MOST one node representing a typedef name.
1970
   This way, even though the compiler substitutes corresponding
1971
   ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1972
   early on, later parts of the compiler can always do the reverse
1973
   translation and get back the corresponding typedef name.  For
1974
   example, given:
1975
 
1976
        typedef struct S MY_TYPE;
1977
        MY_TYPE object;
1978
 
1979
   Later parts of the compiler might only know that `object' was of
1980
   type `struct S' if it were not for code just below.  With this
1981
   code however, later parts of the compiler see something like:
1982
 
1983
        struct S' == struct S
1984
        typedef struct S' MY_TYPE;
1985
        struct S' object;
1986
 
1987
    And they can then deduce (from the node for type struct S') that
1988
    the original object declaration was:
1989
 
1990
                MY_TYPE object;
1991
 
1992
    Being able to do this is important for proper support of protoize,
1993
    and also for generating precise symbolic debugging information
1994
    which takes full account of the programmer's (typedef) vocabulary.
1995
 
1996
    Obviously, we don't want to generate a duplicate ..._TYPE node if
1997
    the TYPE_DECL node that we are now processing really represents a
1998
    standard built-in type.
1999
 
2000
    Since all standard types are effectively declared at line zero
2001
    in the source file, we can easily check to see if we are working
2002
    on a standard type by checking the current value of lineno.  */
2003
 
2004
static void
2005
clone_underlying_type (tree x)
2006
{
2007
  if (DECL_IS_BUILTIN (x))
2008
    {
2009
      if (TYPE_NAME (TREE_TYPE (x)) == 0)
2010
        TYPE_NAME (TREE_TYPE (x)) = x;
2011
    }
2012
  else if (TREE_TYPE (x) != error_mark_node
2013
           && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2014
    {
2015
      tree tt = TREE_TYPE (x);
2016
      DECL_ORIGINAL_TYPE (x) = tt;
2017
      tt = build_variant_type_copy (tt);
2018
      TYPE_NAME (tt) = x;
2019
      TREE_USED (tt) = TREE_USED (x);
2020
      TREE_TYPE (x) = tt;
2021
    }
2022
}
2023
 
2024
/* Record a decl-node X as belonging to the current lexical scope.
2025
   Check for errors (such as an incompatible declaration for the same
2026
   name already seen in the same scope).
2027
 
2028
   Returns either X or an old decl for the same name.
2029
   If an old decl is returned, it may have been smashed
2030
   to agree with what X says.  */
2031
 
2032
tree
2033
pushdecl (tree x)
2034
{
2035
  tree name = DECL_NAME (x);
2036
  struct c_scope *scope = current_scope;
2037
  struct c_binding *b;
2038
  bool nested = false;
2039
 
2040
  /* Functions need the lang_decl data.  */
2041
  if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2042
    DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2043
 
2044
  /* Must set DECL_CONTEXT for everything not at file scope or
2045
     DECL_FILE_SCOPE_P won't work.  Local externs don't count
2046
     unless they have initializers (which generate code).  */
2047
  if (current_function_decl
2048
      && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2049
          || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2050
    DECL_CONTEXT (x) = current_function_decl;
2051
 
2052
  /* If this is of variably modified type, prevent jumping into its
2053
     scope.  */
2054
  if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2055
      && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2056
    c_begin_vm_scope (scope->depth);
2057
 
2058
  /* Anonymous decls are just inserted in the scope.  */
2059
  if (!name)
2060
    {
2061
      bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2062
      return x;
2063
    }
2064
 
2065
  /* First, see if there is another declaration with the same name in
2066
     the current scope.  If there is, duplicate_decls may do all the
2067
     work for us.  If duplicate_decls returns false, that indicates
2068
     two incompatible decls in the same scope; we are to silently
2069
     replace the old one (duplicate_decls has issued all appropriate
2070
     diagnostics).  In particular, we should not consider possible
2071
     duplicates in the external scope, or shadowing.  */
2072
  b = I_SYMBOL_BINDING (name);
2073
  if (b && B_IN_SCOPE (b, scope))
2074
    {
2075
      struct c_binding *b_ext, *b_use;
2076
      tree type = TREE_TYPE (x);
2077
      tree visdecl = b->decl;
2078
      tree vistype = TREE_TYPE (visdecl);
2079
      if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2080
          && COMPLETE_TYPE_P (TREE_TYPE (x)))
2081
        b->inner_comp = false;
2082
      b_use = b;
2083
      b_ext = b;
2084
      /* If this is an external linkage declaration, we should check
2085
         for compatibility with the type in the external scope before
2086
         setting the type at this scope based on the visible
2087
         information only.  */
2088
      if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2089
        {
2090
          while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2091
            b_ext = b_ext->shadowed;
2092
          if (b_ext)
2093
            {
2094
              b_use = b_ext;
2095
              if (b_use->type)
2096
                TREE_TYPE (b_use->decl) = b_use->type;
2097
            }
2098
        }
2099
      if (duplicate_decls (x, b_use->decl))
2100
        {
2101
          if (b_use != b)
2102
            {
2103
              /* Save the updated type in the external scope and
2104
                 restore the proper type for this scope.  */
2105
              tree thistype;
2106
              if (comptypes (vistype, type))
2107
                thistype = composite_type (vistype, type);
2108
              else
2109
                thistype = TREE_TYPE (b_use->decl);
2110
              b_use->type = TREE_TYPE (b_use->decl);
2111
              if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2112
                  && DECL_BUILT_IN (b_use->decl))
2113
                thistype
2114
                  = build_type_attribute_variant (thistype,
2115
                                                  TYPE_ATTRIBUTES
2116
                                                  (b_use->type));
2117
              TREE_TYPE (b_use->decl) = thistype;
2118
            }
2119
          return b_use->decl;
2120
        }
2121
      else
2122
        goto skip_external_and_shadow_checks;
2123
    }
2124
 
2125
  /* All declarations with external linkage, and all external
2126
     references, go in the external scope, no matter what scope is
2127
     current.  However, the binding in that scope is ignored for
2128
     purposes of normal name lookup.  A separate binding structure is
2129
     created in the requested scope; this governs the normal
2130
     visibility of the symbol.
2131
 
2132
     The binding in the externals scope is used exclusively for
2133
     detecting duplicate declarations of the same object, no matter
2134
     what scope they are in; this is what we do here.  (C99 6.2.7p2:
2135
     All declarations that refer to the same object or function shall
2136
     have compatible type; otherwise, the behavior is undefined.)  */
2137
  if (DECL_EXTERNAL (x) || scope == file_scope)
2138
    {
2139
      tree type = TREE_TYPE (x);
2140
      tree vistype = 0;
2141
      tree visdecl = 0;
2142
      bool type_saved = false;
2143
      if (b && !B_IN_EXTERNAL_SCOPE (b)
2144
          && (TREE_CODE (b->decl) == FUNCTION_DECL
2145
              || TREE_CODE (b->decl) == VAR_DECL)
2146
          && DECL_FILE_SCOPE_P (b->decl))
2147
        {
2148
          visdecl = b->decl;
2149
          vistype = TREE_TYPE (visdecl);
2150
        }
2151
      if (scope != file_scope
2152
          && !DECL_IN_SYSTEM_HEADER (x))
2153
        warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2154
 
2155
      while (b && !B_IN_EXTERNAL_SCOPE (b))
2156
        {
2157
          /* If this decl might be modified, save its type.  This is
2158
             done here rather than when the decl is first bound
2159
             because the type may change after first binding, through
2160
             being completed or through attributes being added.  If we
2161
             encounter multiple such decls, only the first should have
2162
             its type saved; the others will already have had their
2163
             proper types saved and the types will not have changed as
2164
             their scopes will not have been re-entered.  */
2165
          if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2166
            {
2167
              b->type = TREE_TYPE (b->decl);
2168
              type_saved = true;
2169
            }
2170
          if (B_IN_FILE_SCOPE (b)
2171
              && TREE_CODE (b->decl) == VAR_DECL
2172
              && TREE_STATIC (b->decl)
2173
              && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2174
              && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2175
              && TREE_CODE (type) == ARRAY_TYPE
2176
              && TYPE_DOMAIN (type)
2177
              && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2178
              && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2179
            {
2180
              /* Array type completed in inner scope, which should be
2181
                 diagnosed if the completion does not have size 1 and
2182
                 it does not get completed in the file scope.  */
2183
              b->inner_comp = true;
2184
            }
2185
          b = b->shadowed;
2186
        }
2187
 
2188
      /* If a matching external declaration has been found, set its
2189
         type to the composite of all the types of that declaration.
2190
         After the consistency checks, it will be reset to the
2191
         composite of the visible types only.  */
2192
      if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2193
          && b->type)
2194
        TREE_TYPE (b->decl) = b->type;
2195
 
2196
      /* The point of the same_translation_unit_p check here is,
2197
         we want to detect a duplicate decl for a construct like
2198
         foo() { extern bar(); } ... static bar();  but not if
2199
         they are in different translation units.  In any case,
2200
         the static does not go in the externals scope.  */
2201
      if (b
2202
          && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2203
          && duplicate_decls (x, b->decl))
2204
        {
2205
          tree thistype;
2206
          if (vistype)
2207
            {
2208
              if (comptypes (vistype, type))
2209
                thistype = composite_type (vistype, type);
2210
              else
2211
                thistype = TREE_TYPE (b->decl);
2212
            }
2213
          else
2214
            thistype = type;
2215
          b->type = TREE_TYPE (b->decl);
2216
          if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2217
            thistype
2218
              = build_type_attribute_variant (thistype,
2219
                                              TYPE_ATTRIBUTES (b->type));
2220
          TREE_TYPE (b->decl) = thistype;
2221
          bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2222
          return b->decl;
2223
        }
2224
      else if (TREE_PUBLIC (x))
2225
        {
2226
          if (visdecl && !b && duplicate_decls (x, visdecl))
2227
            {
2228
              /* An external declaration at block scope referring to a
2229
                 visible entity with internal linkage.  The composite
2230
                 type will already be correct for this scope, so we
2231
                 just need to fall through to make the declaration in
2232
                 this scope.  */
2233
              nested = true;
2234
              x = visdecl;
2235
            }
2236
          else
2237
            {
2238
              bind (name, x, external_scope, /*invisible=*/true,
2239
                    /*nested=*/false);
2240
              nested = true;
2241
            }
2242
        }
2243
    }
2244
 
2245
  if (TREE_CODE (x) != PARM_DECL)
2246
    warn_if_shadowing (x);
2247
 
2248
 skip_external_and_shadow_checks:
2249
  if (TREE_CODE (x) == TYPE_DECL)
2250
    clone_underlying_type (x);
2251
 
2252
  bind (name, x, scope, /*invisible=*/false, nested);
2253
 
2254
  /* If x's type is incomplete because it's based on a
2255
     structure or union which has not yet been fully declared,
2256
     attach it to that structure or union type, so we can go
2257
     back and complete the variable declaration later, if the
2258
     structure or union gets fully declared.
2259
 
2260
     If the input is erroneous, we can have error_mark in the type
2261
     slot (e.g. "f(void a, ...)") - that doesn't count as an
2262
     incomplete type.  */
2263
  if (TREE_TYPE (x) != error_mark_node
2264
      && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2265
    {
2266
      tree element = TREE_TYPE (x);
2267
 
2268
      while (TREE_CODE (element) == ARRAY_TYPE)
2269
        element = TREE_TYPE (element);
2270
      element = TYPE_MAIN_VARIANT (element);
2271
 
2272
      if ((TREE_CODE (element) == RECORD_TYPE
2273
           || TREE_CODE (element) == UNION_TYPE)
2274
          && (TREE_CODE (x) != TYPE_DECL
2275
              || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2276
          && !COMPLETE_TYPE_P (element))
2277
        C_TYPE_INCOMPLETE_VARS (element)
2278
          = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2279
    }
2280
  return x;
2281
}
2282
 
2283
/* Record X as belonging to file scope.
2284
   This is used only internally by the Objective-C front end,
2285
   and is limited to its needs.  duplicate_decls is not called;
2286
   if there is any preexisting decl for this identifier, it is an ICE.  */
2287
 
2288
tree
2289
pushdecl_top_level (tree x)
2290
{
2291
  tree name;
2292
  bool nested = false;
2293
  gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2294
 
2295
  name = DECL_NAME (x);
2296
 
2297
 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2298
 
2299
  if (TREE_PUBLIC (x))
2300
    {
2301
      bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2302
      nested = true;
2303
    }
2304
  if (file_scope)
2305
    bind (name, x, file_scope, /*invisible=*/false, nested);
2306
 
2307
  return x;
2308
}
2309
 
2310
static void
2311
implicit_decl_warning (tree id, tree olddecl)
2312
{
2313
  void (*diag) (const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2);
2314
  switch (mesg_implicit_function_declaration)
2315
    {
2316
    case 0: return;
2317
    case 1: diag = warning0; break;
2318
    case 2: diag = error;   break;
2319
    default: gcc_unreachable ();
2320
    }
2321
 
2322
  diag (G_("implicit declaration of function %qE"), id);
2323
  if (olddecl)
2324
    locate_old_decl (olddecl, diag);
2325
}
2326
 
2327
/* Generate an implicit declaration for identifier FUNCTIONID as a
2328
   function of type int ().  */
2329
 
2330
tree
2331
implicitly_declare (tree functionid)
2332
{
2333
  struct c_binding *b;
2334
  tree decl = 0;
2335
  tree asmspec_tree;
2336
 
2337
  for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2338
    {
2339
      if (B_IN_SCOPE (b, external_scope))
2340
        {
2341
          decl = b->decl;
2342
          break;
2343
        }
2344
    }
2345
 
2346
  if (decl)
2347
    {
2348
      if (decl == error_mark_node)
2349
        return decl;
2350
 
2351
      /* FIXME: Objective-C has weird not-really-builtin functions
2352
         which are supposed to be visible automatically.  They wind up
2353
         in the external scope because they're pushed before the file
2354
         scope gets created.  Catch this here and rebind them into the
2355
         file scope.  */
2356
      if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2357
        {
2358
          bind (functionid, decl, file_scope,
2359
                /*invisible=*/false, /*nested=*/true);
2360
          return decl;
2361
        }
2362
      else
2363
        {
2364
          tree newtype = default_function_type;
2365
          if (b->type)
2366
            TREE_TYPE (decl) = b->type;
2367
          /* Implicit declaration of a function already declared
2368
             (somehow) in a different scope, or as a built-in.
2369
             If this is the first time this has happened, warn;
2370
             then recycle the old declaration but with the new type.  */
2371
          if (!C_DECL_IMPLICIT (decl))
2372
            {
2373
              implicit_decl_warning (functionid, decl);
2374
              C_DECL_IMPLICIT (decl) = 1;
2375
            }
2376
          if (DECL_BUILT_IN (decl))
2377
            {
2378
              newtype = build_type_attribute_variant (newtype,
2379
                                                      TYPE_ATTRIBUTES
2380
                                                      (TREE_TYPE (decl)));
2381
              if (!comptypes (newtype, TREE_TYPE (decl)))
2382
                {
2383
                  warning (0, "incompatible implicit declaration of built-in"
2384
                           " function %qD", decl);
2385
                  newtype = TREE_TYPE (decl);
2386
                }
2387
            }
2388
          else
2389
            {
2390
              if (!comptypes (newtype, TREE_TYPE (decl)))
2391
                {
2392
                  error ("incompatible implicit declaration of function %qD",
2393
                         decl);
2394
                  locate_old_decl (decl, error);
2395
                }
2396
            }
2397
          b->type = TREE_TYPE (decl);
2398
          TREE_TYPE (decl) = newtype;
2399
          bind (functionid, decl, current_scope,
2400
                /*invisible=*/false, /*nested=*/true);
2401
          return decl;
2402
        }
2403
    }
2404
 
2405
  /* Not seen before.  */
2406
  decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2407
  DECL_EXTERNAL (decl) = 1;
2408
  TREE_PUBLIC (decl) = 1;
2409
  C_DECL_IMPLICIT (decl) = 1;
2410
  implicit_decl_warning (functionid, 0);
2411
  asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2412
  if (asmspec_tree)
2413
    set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2414
 
2415
  /* C89 says implicit declarations are in the innermost block.
2416
     So we record the decl in the standard fashion.  */
2417
  decl = pushdecl (decl);
2418
 
2419
  /* No need to call objc_check_decl here - it's a function type.  */
2420
  rest_of_decl_compilation (decl, 0, 0);
2421
 
2422
  /* Write a record describing this implicit function declaration
2423
     to the prototypes file (if requested).  */
2424
  gen_aux_info_record (decl, 0, 1, 0);
2425
 
2426
  /* Possibly apply some default attributes to this implicit declaration.  */
2427
  decl_attributes (&decl, NULL_TREE, 0);
2428
 
2429
  return decl;
2430
}
2431
 
2432
/* Issue an error message for a reference to an undeclared variable
2433
   ID, including a reference to a builtin outside of function-call
2434
   context.  Establish a binding of the identifier to error_mark_node
2435
   in an appropriate scope, which will suppress further errors for the
2436
   same identifier.  The error message should be given location LOC.  */
2437
void
2438
undeclared_variable (tree id, location_t loc)
2439
{
2440
  static bool already = false;
2441
  struct c_scope *scope;
2442
 
2443
  if (current_function_decl == 0)
2444
    {
2445
      error ("%H%qE undeclared here (not in a function)", &loc, id);
2446
      scope = current_scope;
2447
    }
2448
  else
2449
    {
2450
      error ("%H%qE undeclared (first use in this function)", &loc, id);
2451
 
2452
      if (!already)
2453
        {
2454
          error ("%H(Each undeclared identifier is reported only once", &loc);
2455
          error ("%Hfor each function it appears in.)", &loc);
2456
          already = true;
2457
        }
2458
 
2459
      /* If we are parsing old-style parameter decls, current_function_decl
2460
         will be nonnull but current_function_scope will be null.  */
2461
      scope = current_function_scope ? current_function_scope : current_scope;
2462
    }
2463
  bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2464
}
2465
 
2466
/* Subroutine of lookup_label, declare_label, define_label: construct a
2467
   LABEL_DECL with all the proper frills.  */
2468
 
2469
static tree
2470
make_label (tree name, location_t location)
2471
{
2472
  tree label = build_decl (LABEL_DECL, name, void_type_node);
2473
 
2474
  DECL_CONTEXT (label) = current_function_decl;
2475
  DECL_MODE (label) = VOIDmode;
2476
  DECL_SOURCE_LOCATION (label) = location;
2477
 
2478
  return label;
2479
}
2480
 
2481
/* Get the LABEL_DECL corresponding to identifier NAME as a label.
2482
   Create one if none exists so far for the current function.
2483
   This is called when a label is used in a goto expression or
2484
   has its address taken.  */
2485
 
2486
tree
2487
lookup_label (tree name)
2488
{
2489
  tree label;
2490
 
2491
  if (current_function_decl == 0)
2492
    {
2493
      error ("label %qE referenced outside of any function", name);
2494
      return 0;
2495
    }
2496
 
2497
  /* Use a label already defined or ref'd with this name, but not if
2498
     it is inherited from a containing function and wasn't declared
2499
     using __label__.  */
2500
  label = I_LABEL_DECL (name);
2501
  if (label && (DECL_CONTEXT (label) == current_function_decl
2502
                || C_DECLARED_LABEL_FLAG (label)))
2503
    {
2504
      /* If the label has only been declared, update its apparent
2505
         location to point here, for better diagnostics if it
2506
         turns out not to have been defined.  */
2507
      if (!TREE_USED (label))
2508
        DECL_SOURCE_LOCATION (label) = input_location;
2509
      return label;
2510
    }
2511
 
2512
  /* No label binding for that identifier; make one.  */
2513
  label = make_label (name, input_location);
2514
 
2515
  /* Ordinary labels go in the current function scope.  */
2516
  bind (name, label, current_function_scope,
2517
        /*invisible=*/false, /*nested=*/false);
2518
  return label;
2519
}
2520
 
2521
/* Make a label named NAME in the current function, shadowing silently
2522
   any that may be inherited from containing functions or containing
2523
   scopes.  This is called for __label__ declarations.  */
2524
 
2525
tree
2526
declare_label (tree name)
2527
{
2528
  struct c_binding *b = I_LABEL_BINDING (name);
2529
  tree label;
2530
 
2531
  /* Check to make sure that the label hasn't already been declared
2532
     at this scope */
2533
  if (b && B_IN_CURRENT_SCOPE (b))
2534
    {
2535
      error ("duplicate label declaration %qE", name);
2536
      locate_old_decl (b->decl, error);
2537
 
2538
      /* Just use the previous declaration.  */
2539
      return b->decl;
2540
    }
2541
 
2542
  label = make_label (name, input_location);
2543
  C_DECLARED_LABEL_FLAG (label) = 1;
2544
 
2545
  /* Declared labels go in the current scope.  */
2546
  bind (name, label, current_scope,
2547
        /*invisible=*/false, /*nested=*/false);
2548
  return label;
2549
}
2550
 
2551
/* Define a label, specifying the location in the source file.
2552
   Return the LABEL_DECL node for the label, if the definition is valid.
2553
   Otherwise return 0.  */
2554
 
2555
tree
2556
define_label (location_t location, tree name)
2557
{
2558
  /* Find any preexisting label with this name.  It is an error
2559
     if that label has already been defined in this function, or
2560
     if there is a containing function with a declared label with
2561
     the same name.  */
2562
  tree label = I_LABEL_DECL (name);
2563
  struct c_label_list *nlist_se, *nlist_vm;
2564
 
2565
  if (label
2566
      && ((DECL_CONTEXT (label) == current_function_decl
2567
           && DECL_INITIAL (label) != 0)
2568
          || (DECL_CONTEXT (label) != current_function_decl
2569
              && C_DECLARED_LABEL_FLAG (label))))
2570
    {
2571
      error ("%Hduplicate label %qD", &location, label);
2572
      locate_old_decl (label, error);
2573
      return 0;
2574
    }
2575
  else if (label && DECL_CONTEXT (label) == current_function_decl)
2576
    {
2577
      /* The label has been used or declared already in this function,
2578
         but not defined.  Update its location to point to this
2579
         definition.  */
2580
      if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2581
        error ("%Jjump into statement expression", label);
2582
      if (C_DECL_UNDEFINABLE_VM (label))
2583
        error ("%Jjump into scope of identifier with variably modified type",
2584
               label);
2585
      DECL_SOURCE_LOCATION (label) = location;
2586
    }
2587
  else
2588
    {
2589
      /* No label binding for that identifier; make one.  */
2590
      label = make_label (name, location);
2591
 
2592
      /* Ordinary labels go in the current function scope.  */
2593
      bind (name, label, current_function_scope,
2594
            /*invisible=*/false, /*nested=*/false);
2595
    }
2596
 
2597
  if (!in_system_header && lookup_name (name))
2598
    warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2599
             "for labels, identifier %qE conflicts", &location, name);
2600
 
2601
  nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2602
  nlist_se->next = label_context_stack_se->labels_def;
2603
  nlist_se->label = label;
2604
  label_context_stack_se->labels_def = nlist_se;
2605
 
2606
  nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2607
  nlist_vm->next = label_context_stack_vm->labels_def;
2608
  nlist_vm->label = label;
2609
  label_context_stack_vm->labels_def = nlist_vm;
2610
 
2611
  /* Mark label as having been defined.  */
2612
  DECL_INITIAL (label) = error_mark_node;
2613
  return label;
2614
}
2615
 
2616
/* Given NAME, an IDENTIFIER_NODE,
2617
   return the structure (or union or enum) definition for that name.
2618
   If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2619
   CODE says which kind of type the caller wants;
2620
   it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2621
   If the wrong kind of type is found, an error is reported.  */
2622
 
2623
static tree
2624
lookup_tag (enum tree_code code, tree name, int thislevel_only)
2625
{
2626
  struct c_binding *b = I_TAG_BINDING (name);
2627
  int thislevel = 0;
2628
 
2629
  if (!b || !b->decl)
2630
    return 0;
2631
 
2632
  /* We only care about whether it's in this level if
2633
     thislevel_only was set or it might be a type clash.  */
2634
  if (thislevel_only || TREE_CODE (b->decl) != code)
2635
    {
2636
      /* For our purposes, a tag in the external scope is the same as
2637
         a tag in the file scope.  (Primarily relevant to Objective-C
2638
         and its builtin structure tags, which get pushed before the
2639
         file scope is created.)  */
2640
      if (B_IN_CURRENT_SCOPE (b)
2641
          || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2642
        thislevel = 1;
2643
    }
2644
 
2645
  if (thislevel_only && !thislevel)
2646
    return 0;
2647
 
2648
  if (TREE_CODE (b->decl) != code)
2649
    {
2650
      /* Definition isn't the kind we were looking for.  */
2651
      pending_invalid_xref = name;
2652
      pending_invalid_xref_location = input_location;
2653
 
2654
      /* If in the same binding level as a declaration as a tag
2655
         of a different type, this must not be allowed to
2656
         shadow that tag, so give the error immediately.
2657
         (For example, "struct foo; union foo;" is invalid.)  */
2658
      if (thislevel)
2659
        pending_xref_error ();
2660
    }
2661
  return b->decl;
2662
}
2663
 
2664
/* Print an error message now
2665
   for a recent invalid struct, union or enum cross reference.
2666
   We don't print them immediately because they are not invalid
2667
   when used in the `struct foo;' construct for shadowing.  */
2668
 
2669
void
2670
pending_xref_error (void)
2671
{
2672
  if (pending_invalid_xref != 0)
2673
    error ("%H%qE defined as wrong kind of tag",
2674
           &pending_invalid_xref_location, pending_invalid_xref);
2675
  pending_invalid_xref = 0;
2676
}
2677
 
2678
 
2679
/* Look up NAME in the current scope and its superiors
2680
   in the namespace of variables, functions and typedefs.
2681
   Return a ..._DECL node of some kind representing its definition,
2682
   or return 0 if it is undefined.  */
2683
 
2684
tree
2685
lookup_name (tree name)
2686
{
2687
  struct c_binding *b = I_SYMBOL_BINDING (name);
2688
  if (b && !b->invisible)
2689
    return b->decl;
2690
  return 0;
2691
}
2692
 
2693
/* Similar to `lookup_name' but look only at the indicated scope.  */
2694
 
2695
static tree
2696
lookup_name_in_scope (tree name, struct c_scope *scope)
2697
{
2698
  struct c_binding *b;
2699
 
2700
  for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2701
    if (B_IN_SCOPE (b, scope))
2702
      return b->decl;
2703
  return 0;
2704
}
2705
 
2706
/* Create the predefined scalar types of C,
2707
   and some nodes representing standard constants (0, 1, (void *) 0).
2708
   Initialize the global scope.
2709
   Make definitions for built-in primitive functions.  */
2710
 
2711
void
2712
c_init_decl_processing (void)
2713
{
2714
  location_t save_loc = input_location;
2715
 
2716
  /* Initialize reserved words for parser.  */
2717
  c_parse_init ();
2718
 
2719
  current_function_decl = 0;
2720
 
2721
  gcc_obstack_init (&parser_obstack);
2722
 
2723
  /* Make the externals scope.  */
2724
  push_scope ();
2725
  external_scope = current_scope;
2726
 
2727
  /* Declarations from c_common_nodes_and_builtins must not be associated
2728
     with this input file, lest we get differences between using and not
2729
     using preprocessed headers.  */
2730
#ifdef USE_MAPPED_LOCATION
2731
  input_location = BUILTINS_LOCATION;
2732
#else
2733
  input_location.file = "<built-in>";
2734
  input_location.line = 0;
2735
#endif
2736
 
2737
  build_common_tree_nodes (flag_signed_char, false);
2738
 
2739
  c_common_nodes_and_builtins ();
2740
 
2741
  /* In C, comparisons and TRUTH_* expressions have type int.  */
2742
  truthvalue_type_node = integer_type_node;
2743
  truthvalue_true_node = integer_one_node;
2744
  truthvalue_false_node = integer_zero_node;
2745
 
2746
  /* Even in C99, which has a real boolean type.  */
2747
  pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2748
                        boolean_type_node));
2749
 
2750
  input_location = save_loc;
2751
 
2752
  pedantic_lvalues = true;
2753
 
2754
  make_fname_decl = c_make_fname_decl;
2755
  start_fname_decls ();
2756
}
2757
 
2758
/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2759
   decl, NAME is the initialization string and TYPE_DEP indicates whether
2760
   NAME depended on the type of the function.  As we don't yet implement
2761
   delayed emission of static data, we mark the decl as emitted
2762
   so it is not placed in the output.  Anything using it must therefore pull
2763
   out the STRING_CST initializer directly.  FIXME.  */
2764
 
2765
static tree
2766
c_make_fname_decl (tree id, int type_dep)
2767
{
2768
  const char *name = fname_as_string (type_dep);
2769
  tree decl, type, init;
2770
  size_t length = strlen (name);
2771
 
2772
  type = build_array_type (char_type_node,
2773
                           build_index_type (size_int (length)));
2774
  type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2775
 
2776
  decl = build_decl (VAR_DECL, id, type);
2777
 
2778
  TREE_STATIC (decl) = 1;
2779
  TREE_READONLY (decl) = 1;
2780
  DECL_ARTIFICIAL (decl) = 1;
2781
 
2782
  init = build_string (length + 1, name);
2783
  free ((char *) name);
2784
  TREE_TYPE (init) = type;
2785
  DECL_INITIAL (decl) = init;
2786
 
2787
  TREE_USED (decl) = 1;
2788
 
2789
  if (current_function_decl
2790
      /* For invalid programs like this:
2791
 
2792
         void foo()
2793
         const char* p = __FUNCTION__;
2794
 
2795
         the __FUNCTION__ is believed to appear in K&R style function
2796
         parameter declarator.  In that case we still don't have
2797
         function_scope.  */
2798
      && (!errorcount || current_function_scope))
2799
    {
2800
      DECL_CONTEXT (decl) = current_function_decl;
2801
      bind (id, decl, current_function_scope,
2802
            /*invisible=*/false, /*nested=*/false);
2803
    }
2804
 
2805
  finish_decl (decl, init, NULL_TREE);
2806
 
2807
  return decl;
2808
}
2809
 
2810
/* Return a definition for a builtin function named NAME and whose data type
2811
   is TYPE.  TYPE should be a function type with argument types.
2812
   FUNCTION_CODE tells later passes how to compile calls to this function.
2813
   See tree.h for its possible values.
2814
 
2815
   If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2816
   the name to be called if we can't opencode the function.  If
2817
   ATTRS is nonzero, use that for the function's attribute list.  */
2818
 
2819
tree
2820
builtin_function (const char *name, tree type, int function_code,
2821
                  enum built_in_class cl, const char *library_name,
2822
                  tree attrs)
2823
{
2824
  tree id = get_identifier (name);
2825
  tree decl = build_decl (FUNCTION_DECL, id, type);
2826
  TREE_PUBLIC (decl) = 1;
2827
  DECL_EXTERNAL (decl) = 1;
2828
  DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2829
  DECL_BUILT_IN_CLASS (decl) = cl;
2830
  DECL_FUNCTION_CODE (decl) = function_code;
2831
  C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2832
  if (library_name)
2833
    SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2834
 
2835
  /* Should never be called on a symbol with a preexisting meaning.  */
2836
  gcc_assert (!I_SYMBOL_BINDING (id));
2837
 
2838
  bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2839
 
2840
  /* Builtins in the implementation namespace are made visible without
2841
     needing to be explicitly declared.  See push_file_scope.  */
2842
  if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2843
    {
2844
      TREE_CHAIN (decl) = visible_builtins;
2845
      visible_builtins = decl;
2846
    }
2847
 
2848
  /* Possibly apply some default attributes to this built-in function.  */
2849
  if (attrs)
2850
    decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2851
  else
2852
    decl_attributes (&decl, NULL_TREE, 0);
2853
 
2854
  return decl;
2855
}
2856
 
2857
/* Called when a declaration is seen that contains no names to declare.
2858
   If its type is a reference to a structure, union or enum inherited
2859
   from a containing scope, shadow that tag name for the current scope
2860
   with a forward reference.
2861
   If its type defines a new named structure or union
2862
   or defines an enum, it is valid but we need not do anything here.
2863
   Otherwise, it is an error.  */
2864
 
2865
void
2866
shadow_tag (const struct c_declspecs *declspecs)
2867
{
2868
  shadow_tag_warned (declspecs, 0);
2869
}
2870
 
2871
/* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2872
   but no pedwarn.  */
2873
void
2874
shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2875
{
2876
  bool found_tag = false;
2877
 
2878
  if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2879
    {
2880
      tree value = declspecs->type;
2881
      enum tree_code code = TREE_CODE (value);
2882
 
2883
      if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2884
        /* Used to test also that TYPE_SIZE (value) != 0.
2885
           That caused warning for `struct foo;' at top level in the file.  */
2886
        {
2887
          tree name = TYPE_NAME (value);
2888
          tree t;
2889
 
2890
          found_tag = true;
2891
 
2892
          if (name == 0)
2893
            {
2894
              if (warned != 1 && code != ENUMERAL_TYPE)
2895
                /* Empty unnamed enum OK */
2896
                {
2897
                  pedwarn ("unnamed struct/union that defines no instances");
2898
                  warned = 1;
2899
                }
2900
            }
2901
          else if (!declspecs->tag_defined_p
2902
                   && declspecs->storage_class != csc_none)
2903
            {
2904
              if (warned != 1)
2905
                pedwarn ("empty declaration with storage class specifier "
2906
                         "does not redeclare tag");
2907
              warned = 1;
2908
              pending_xref_error ();
2909
            }
2910
          else if (!declspecs->tag_defined_p
2911
                   && (declspecs->const_p
2912
                       || declspecs->volatile_p
2913
                       || declspecs->restrict_p))
2914
            {
2915
              if (warned != 1)
2916
                pedwarn ("empty declaration with type qualifier "
2917
                         "does not redeclare tag");
2918
              warned = 1;
2919
              pending_xref_error ();
2920
            }
2921
          else
2922
            {
2923
              pending_invalid_xref = 0;
2924
              t = lookup_tag (code, name, 1);
2925
 
2926
              if (t == 0)
2927
                {
2928
                  t = make_node (code);
2929
                  pushtag (name, t);
2930
                }
2931
            }
2932
        }
2933
      else
2934
        {
2935
          if (warned != 1 && !in_system_header)
2936
            {
2937
              pedwarn ("useless type name in empty declaration");
2938
              warned = 1;
2939
            }
2940
        }
2941
    }
2942
  else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2943
    {
2944
      pedwarn ("useless type name in empty declaration");
2945
      warned = 1;
2946
    }
2947
 
2948
  pending_invalid_xref = 0;
2949
 
2950
  if (declspecs->inline_p)
2951
    {
2952
      error ("%<inline%> in empty declaration");
2953
      warned = 1;
2954
    }
2955
 
2956
  if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2957
    {
2958
      error ("%<auto%> in file-scope empty declaration");
2959
      warned = 1;
2960
    }
2961
 
2962
  if (current_scope == file_scope && declspecs->storage_class == csc_register)
2963
    {
2964
      error ("%<register%> in file-scope empty declaration");
2965
      warned = 1;
2966
    }
2967
 
2968
  if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2969
    {
2970
      warning (0, "useless storage class specifier in empty declaration");
2971
      warned = 2;
2972
    }
2973
 
2974
  if (!warned && !in_system_header && declspecs->thread_p)
2975
    {
2976
      warning (0, "useless %<__thread%> in empty declaration");
2977
      warned = 2;
2978
    }
2979
 
2980
  if (!warned && !in_system_header && (declspecs->const_p
2981
                                       || declspecs->volatile_p
2982
                                       || declspecs->restrict_p))
2983
    {
2984
      warning (0, "useless type qualifier in empty declaration");
2985
      warned = 2;
2986
    }
2987
 
2988
  if (warned != 1)
2989
    {
2990
      if (!found_tag)
2991
        pedwarn ("empty declaration");
2992
    }
2993
}
2994
 
2995
 
2996
/* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2997
   bits.  SPECS represents declaration specifiers that the grammar
2998
   only permits to contain type qualifiers and attributes.  */
2999
 
3000
int
3001
quals_from_declspecs (const struct c_declspecs *specs)
3002
{
3003
  int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3004
               | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3005
               | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3006
  gcc_assert (!specs->type
3007
              && !specs->decl_attr
3008
              && specs->typespec_word == cts_none
3009
              && specs->storage_class == csc_none
3010
              && !specs->typedef_p
3011
              && !specs->explicit_signed_p
3012
              && !specs->deprecated_p
3013
              && !specs->long_p
3014
              && !specs->long_long_p
3015
              && !specs->short_p
3016
              && !specs->signed_p
3017
              && !specs->unsigned_p
3018
              && !specs->complex_p
3019
              && !specs->inline_p
3020
              && !specs->thread_p);
3021
  return quals;
3022
}
3023
 
3024
/* Construct an array declarator.  EXPR is the expression inside [],
3025
   or NULL_TREE.  QUALS are the type qualifiers inside the [] (to be
3026
   applied to the pointer to which a parameter array is converted).
3027
   STATIC_P is true if "static" is inside the [], false otherwise.
3028
   VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3029
   length which is nevertheless a complete type, false otherwise.  The
3030
   field for the contained declarator is left to be filled in by
3031
   set_array_declarator_inner.  */
3032
 
3033
struct c_declarator *
3034
build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3035
                        bool vla_unspec_p)
3036
{
3037
  struct c_declarator *declarator = XOBNEW (&parser_obstack,
3038
                                            struct c_declarator);
3039
  declarator->kind = cdk_array;
3040
  declarator->declarator = 0;
3041
  declarator->u.array.dimen = expr;
3042
  if (quals)
3043
    {
3044
      declarator->u.array.attrs = quals->attrs;
3045
      declarator->u.array.quals = quals_from_declspecs (quals);
3046
    }
3047
  else
3048
    {
3049
      declarator->u.array.attrs = NULL_TREE;
3050
      declarator->u.array.quals = 0;
3051
    }
3052
  declarator->u.array.static_p = static_p;
3053
  declarator->u.array.vla_unspec_p = vla_unspec_p;
3054
  if (pedantic && !flag_isoc99)
3055
    {
3056
      if (static_p || quals != NULL)
3057
        pedwarn ("ISO C90 does not support %<static%> or type "
3058
                 "qualifiers in parameter array declarators");
3059
      if (vla_unspec_p)
3060
        pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3061
    }
3062
  if (vla_unspec_p)
3063
    {
3064
      if (!current_scope->parm_flag)
3065
        {
3066
          /* C99 6.7.5.2p4 */
3067
          error ("%<[*]%> not allowed in other than function prototype scope");
3068
          declarator->u.array.vla_unspec_p = false;
3069
          return NULL;
3070
        }
3071
      current_scope->had_vla_unspec = true;
3072
    }
3073
  return declarator;
3074
}
3075
 
3076
/* Set the contained declarator of an array declarator.  DECL is the
3077
   declarator, as constructed by build_array_declarator; INNER is what
3078
   appears on the left of the [].  ABSTRACT_P is true if it is an
3079
   abstract declarator, false otherwise; this is used to reject static
3080
   and type qualifiers in abstract declarators, where they are not in
3081
   the C99 grammar (subject to possible change in DR#289).  */
3082
 
3083
struct c_declarator *
3084
set_array_declarator_inner (struct c_declarator *decl,
3085
                            struct c_declarator *inner, bool abstract_p)
3086
{
3087
  decl->declarator = inner;
3088
  if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3089
                     || decl->u.array.attrs != NULL_TREE
3090
                     || decl->u.array.static_p))
3091
    error ("static or type qualifiers in abstract declarator");
3092
  return decl;
3093
}
3094
 
3095
/* INIT is a constructor that forms DECL's initializer.  If the final
3096
   element initializes a flexible array field, add the size of that
3097
   initializer to DECL's size.  */
3098
 
3099
static void
3100
add_flexible_array_elts_to_size (tree decl, tree init)
3101
{
3102
  tree elt, type;
3103
 
3104
  if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3105
    return;
3106
 
3107
  elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3108
  type = TREE_TYPE (elt);
3109
  if (TREE_CODE (type) == ARRAY_TYPE
3110
      && TYPE_SIZE (type) == NULL_TREE
3111
      && TYPE_DOMAIN (type) != NULL_TREE
3112
      && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3113
    {
3114
      complete_array_type (&type, elt, false);
3115
      DECL_SIZE (decl)
3116
        = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3117
      DECL_SIZE_UNIT (decl)
3118
        = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3119
    }
3120
}
3121
 
3122
/* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
3123
 
3124
tree
3125
groktypename (struct c_type_name *type_name)
3126
{
3127
  tree type;
3128
  tree attrs = type_name->specs->attrs;
3129
 
3130
  type_name->specs->attrs = NULL_TREE;
3131
 
3132
  type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3133
                         false, NULL);
3134
 
3135
  /* Apply attributes.  */
3136
  decl_attributes (&type, attrs, 0);
3137
 
3138
  return type;
3139
}
3140
 
3141
/* Decode a declarator in an ordinary declaration or data definition.
3142
   This is called as soon as the type information and variable name
3143
   have been parsed, before parsing the initializer if any.
3144
   Here we create the ..._DECL node, fill in its type,
3145
   and put it on the list of decls for the current context.
3146
   The ..._DECL node is returned as the value.
3147
 
3148
   Exception: for arrays where the length is not specified,
3149
   the type is left null, to be filled in by `finish_decl'.
3150
 
3151
   Function definitions do not come here; they go to start_function
3152
   instead.  However, external and forward declarations of functions
3153
   do go through here.  Structure field declarations are done by
3154
   grokfield and not through here.  */
3155
 
3156
tree
3157
start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3158
            bool initialized, tree attributes)
3159
{
3160
  tree decl;
3161
  tree tem;
3162
 
3163
  /* An object declared as __attribute__((deprecated)) suppresses
3164
     warnings of uses of other deprecated items.  */
3165
  if (lookup_attribute ("deprecated", attributes))
3166
    deprecated_state = DEPRECATED_SUPPRESS;
3167
 
3168
  decl = grokdeclarator (declarator, declspecs,
3169
                         NORMAL, initialized, NULL);
3170
  if (!decl)
3171
    return 0;
3172
 
3173
  deprecated_state = DEPRECATED_NORMAL;
3174
 
3175
  if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3176
      && MAIN_NAME_P (DECL_NAME (decl)))
3177
    warning (OPT_Wmain, "%q+D is usually a function", decl);
3178
 
3179
  if (initialized)
3180
    /* Is it valid for this decl to have an initializer at all?
3181
       If not, set INITIALIZED to zero, which will indirectly
3182
       tell 'finish_decl' to ignore the initializer once it is parsed.  */
3183
    switch (TREE_CODE (decl))
3184
      {
3185
      case TYPE_DECL:
3186
        error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3187
        initialized = 0;
3188
        break;
3189
 
3190
      case FUNCTION_DECL:
3191
        error ("function %qD is initialized like a variable", decl);
3192
        initialized = 0;
3193
        break;
3194
 
3195
      case PARM_DECL:
3196
        /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
3197
        error ("parameter %qD is initialized", decl);
3198
        initialized = 0;
3199
        break;
3200
 
3201
      default:
3202
        /* Don't allow initializations for incomplete types except for
3203
           arrays which might be completed by the initialization.  */
3204
 
3205
        /* This can happen if the array size is an undefined macro.
3206
           We already gave a warning, so we don't need another one.  */
3207
        if (TREE_TYPE (decl) == error_mark_node)
3208
          initialized = 0;
3209
        else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3210
          {
3211
            /* A complete type is ok if size is fixed.  */
3212
 
3213
            if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3214
                || C_DECL_VARIABLE_SIZE (decl))
3215
              {
3216
                error ("variable-sized object may not be initialized");
3217
                initialized = 0;
3218
              }
3219
          }
3220
        else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3221
          {
3222
            error ("variable %qD has initializer but incomplete type", decl);
3223
            initialized = 0;
3224
          }
3225
        else if (C_DECL_VARIABLE_SIZE (decl))
3226
          {
3227
            /* Although C99 is unclear about whether incomplete arrays
3228
               of VLAs themselves count as VLAs, it does not make
3229
               sense to permit them to be initialized given that
3230
               ordinary VLAs may not be initialized.  */
3231
            error ("variable-sized object may not be initialized");
3232
            initialized = 0;
3233
          }
3234
      }
3235
 
3236
  if (initialized)
3237
    {
3238
      if (current_scope == file_scope)
3239
        TREE_STATIC (decl) = 1;
3240
 
3241
      /* Tell 'pushdecl' this is an initialized decl
3242
         even though we don't yet have the initializer expression.
3243
         Also tell 'finish_decl' it may store the real initializer.  */
3244
      DECL_INITIAL (decl) = error_mark_node;
3245
    }
3246
 
3247
  /* If this is a function declaration, write a record describing it to the
3248
     prototypes file (if requested).  */
3249
 
3250
  if (TREE_CODE (decl) == FUNCTION_DECL)
3251
    gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3252
 
3253
  /* ANSI specifies that a tentative definition which is not merged with
3254
     a non-tentative definition behaves exactly like a definition with an
3255
     initializer equal to zero.  (Section 3.7.2)
3256
 
3257
     -fno-common gives strict ANSI behavior, though this tends to break
3258
     a large body of code that grew up without this rule.
3259
 
3260
     Thread-local variables are never common, since there's no entrenched
3261
     body of code to break, and it allows more efficient variable references
3262
     in the presence of dynamic linking.  */
3263
 
3264
  if (TREE_CODE (decl) == VAR_DECL
3265
      && !initialized
3266
      && TREE_PUBLIC (decl)
3267
      && !DECL_THREAD_LOCAL_P (decl)
3268
      && !flag_no_common)
3269
    DECL_COMMON (decl) = 1;
3270
 
3271
  /* Set attributes here so if duplicate decl, will have proper attributes.  */
3272
  decl_attributes (&decl, attributes, 0);
3273
 
3274
  if (TREE_CODE (decl) == FUNCTION_DECL
3275
      && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3276
    {
3277
      struct c_declarator *ce = declarator;
3278
 
3279
      if (ce->kind == cdk_pointer)
3280
        ce = declarator->declarator;
3281
      if (ce->kind == cdk_function)
3282
        {
3283
          tree args = ce->u.arg_info->parms;
3284
          for (; args; args = TREE_CHAIN (args))
3285
            {
3286
              tree type = TREE_TYPE (args);
3287
              if (type && INTEGRAL_TYPE_P (type)
3288
                  && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3289
                DECL_ARG_TYPE (args) = integer_type_node;
3290
            }
3291
        }
3292
    }
3293
 
3294
  if (TREE_CODE (decl) == FUNCTION_DECL
3295
      && DECL_DECLARED_INLINE_P (decl)
3296
      && DECL_UNINLINABLE (decl)
3297
      && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3298
    warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3299
             decl);
3300
 
3301
  /* Add this decl to the current scope.
3302
     TEM may equal DECL or it may be a previous decl of the same name.  */
3303
  tem = pushdecl (decl);
3304
 
3305
  if (initialized && DECL_EXTERNAL (tem))
3306
    {
3307
      DECL_EXTERNAL (tem) = 0;
3308
      TREE_STATIC (tem) = 1;
3309
    }
3310
 
3311
  return tem;
3312
}
3313
 
3314
/* Initialize EH if not initialized yet and exceptions are enabled.  */
3315
 
3316
void
3317
c_maybe_initialize_eh (void)
3318
{
3319
  if (!flag_exceptions || c_eh_initialized_p)
3320
    return;
3321
 
3322
  c_eh_initialized_p = true;
3323
  eh_personality_libfunc
3324
    = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3325
                        ? "__gcc_personality_sj0"
3326
                        : "__gcc_personality_v0");
3327
  default_init_unwind_resume_libfunc ();
3328
  using_eh_for_cleanups ();
3329
}
3330
 
3331
/* Finish processing of a declaration;
3332
   install its initial value.
3333
   If the length of an array type is not known before,
3334
   it must be determined now, from the initial value, or it is an error.  */
3335
 
3336
void
3337
finish_decl (tree decl, tree init, tree asmspec_tree)
3338
{
3339
  tree type;
3340
  int was_incomplete = (DECL_SIZE (decl) == 0);
3341
  const char *asmspec = 0;
3342
 
3343
  /* If a name was specified, get the string.  */
3344
  if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3345
      && DECL_FILE_SCOPE_P (decl))
3346
    asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3347
  if (asmspec_tree)
3348
    asmspec = TREE_STRING_POINTER (asmspec_tree);
3349
 
3350
  /* If `start_decl' didn't like having an initialization, ignore it now.  */
3351
  if (init != 0 && DECL_INITIAL (decl) == 0)
3352
    init = 0;
3353
 
3354
  /* Don't crash if parm is initialized.  */
3355
  if (TREE_CODE (decl) == PARM_DECL)
3356
    init = 0;
3357
 
3358
  if (init)
3359
    store_init_value (decl, init);
3360
 
3361
  if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3362
                            || TREE_CODE (decl) == FUNCTION_DECL
3363
                            || TREE_CODE (decl) == FIELD_DECL))
3364
    objc_check_decl (decl);
3365
 
3366
  type = TREE_TYPE (decl);
3367
 
3368
  /* Deduce size of array from initialization, if not already known.  */
3369
  if (TREE_CODE (type) == ARRAY_TYPE
3370
      && TYPE_DOMAIN (type) == 0
3371
      && TREE_CODE (decl) != TYPE_DECL)
3372
    {
3373
      bool do_default
3374
        = (TREE_STATIC (decl)
3375
           /* Even if pedantic, an external linkage array
3376
              may have incomplete type at first.  */
3377
           ? pedantic && !TREE_PUBLIC (decl)
3378
           : !DECL_EXTERNAL (decl));
3379
      int failure
3380
        = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3381
                               do_default);
3382
 
3383
      /* Get the completed type made by complete_array_type.  */
3384
      type = TREE_TYPE (decl);
3385
 
3386
      switch (failure)
3387
        {
3388
        case 1:
3389
          error ("initializer fails to determine size of %q+D", decl);
3390
          break;
3391
 
3392
        case 2:
3393
          if (do_default)
3394
            error ("array size missing in %q+D", decl);
3395
          /* If a `static' var's size isn't known,
3396
             make it extern as well as static, so it does not get
3397
             allocated.
3398
             If it is not `static', then do not mark extern;
3399
             finish_incomplete_decl will give it a default size
3400
             and it will get allocated.  */
3401
          else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3402
            DECL_EXTERNAL (decl) = 1;
3403
          break;
3404
 
3405
        case 3:
3406
          error ("zero or negative size array %q+D", decl);
3407
          break;
3408
 
3409
        case 0:
3410
          /* For global variables, update the copy of the type that
3411
             exists in the binding.  */
3412
          if (TREE_PUBLIC (decl))
3413
            {
3414
              struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3415
              while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3416
                b_ext = b_ext->shadowed;
3417
              if (b_ext)
3418
                {
3419
                  if (b_ext->type)
3420
                    b_ext->type = composite_type (b_ext->type, type);
3421
                  else
3422
                    b_ext->type = type;
3423
                }
3424
            }
3425
          break;
3426
 
3427
        default:
3428
          gcc_unreachable ();
3429
        }
3430
 
3431
      if (DECL_INITIAL (decl))
3432
        TREE_TYPE (DECL_INITIAL (decl)) = type;
3433
 
3434
      layout_decl (decl, 0);
3435
    }
3436
 
3437
  if (TREE_CODE (decl) == VAR_DECL)
3438
    {
3439
      if (init && TREE_CODE (init) == CONSTRUCTOR)
3440
        add_flexible_array_elts_to_size (decl, init);
3441
 
3442
      if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3443
          && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3444
        layout_decl (decl, 0);
3445
 
3446
      if (DECL_SIZE (decl) == 0
3447
          /* Don't give an error if we already gave one earlier.  */
3448
          && TREE_TYPE (decl) != error_mark_node
3449
          && (TREE_STATIC (decl)
3450
              /* A static variable with an incomplete type
3451
                 is an error if it is initialized.
3452
                 Also if it is not file scope.
3453
                 Otherwise, let it through, but if it is not `extern'
3454
                 then it may cause an error message later.  */
3455
              ? (DECL_INITIAL (decl) != 0
3456
                 || !DECL_FILE_SCOPE_P (decl))
3457
              /* An automatic variable with an incomplete type
3458
                 is an error.  */
3459
              : !DECL_EXTERNAL (decl)))
3460
         {
3461
           error ("storage size of %q+D isn%'t known", decl);
3462
           TREE_TYPE (decl) = error_mark_node;
3463
         }
3464
 
3465
      if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3466
          && DECL_SIZE (decl) != 0)
3467
        {
3468
          if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3469
            constant_expression_warning (DECL_SIZE (decl));
3470
          else
3471
            error ("storage size of %q+D isn%'t constant", decl);
3472
        }
3473
 
3474
      if (TREE_USED (type))
3475
        TREE_USED (decl) = 1;
3476
    }
3477
 
3478
  /* If this is a function and an assembler name is specified, reset DECL_RTL
3479
     so we can give it its new name.  Also, update built_in_decls if it
3480
     was a normal built-in.  */
3481
  if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3482
    {
3483
      if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3484
        set_builtin_user_assembler_name (decl, asmspec);
3485
      set_user_assembler_name (decl, asmspec);
3486
    }
3487
 
3488
  /* If #pragma weak was used, mark the decl weak now.  */
3489
  maybe_apply_pragma_weak (decl);
3490
 
3491
  /* Output the assembler code and/or RTL code for variables and functions,
3492
     unless the type is an undefined structure or union.
3493
     If not, it will get done when the type is completed.  */
3494
 
3495
  if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3496
    {
3497
      /* Determine the ELF visibility.  */
3498
      if (TREE_PUBLIC (decl))
3499
        c_determine_visibility (decl);
3500
 
3501
      /* This is a no-op in c-lang.c or something real in objc-act.c.  */
3502
      if (c_dialect_objc ())
3503
        objc_check_decl (decl);
3504
 
3505
      if (asmspec)
3506
        {
3507
          /* If this is not a static variable, issue a warning.
3508
             It doesn't make any sense to give an ASMSPEC for an
3509
             ordinary, non-register local variable.  Historically,
3510
             GCC has accepted -- but ignored -- the ASMSPEC in
3511
             this case.  */
3512
          if (!DECL_FILE_SCOPE_P (decl)
3513
              && TREE_CODE (decl) == VAR_DECL
3514
              && !C_DECL_REGISTER (decl)
3515
              && !TREE_STATIC (decl))
3516
            warning (0, "ignoring asm-specifier for non-static local "
3517
                     "variable %q+D", decl);
3518
          else
3519
            set_user_assembler_name (decl, asmspec);
3520
        }
3521
 
3522
      if (DECL_FILE_SCOPE_P (decl))
3523
        {
3524
          if (DECL_INITIAL (decl) == NULL_TREE
3525
              || DECL_INITIAL (decl) == error_mark_node)
3526
            /* Don't output anything
3527
               when a tentative file-scope definition is seen.
3528
               But at end of compilation, do output code for them.  */
3529
            DECL_DEFER_OUTPUT (decl) = 1;
3530
          rest_of_decl_compilation (decl, true, 0);
3531
        }
3532
      else
3533
        {
3534
          /* In conjunction with an ASMSPEC, the `register'
3535
             keyword indicates that we should place the variable
3536
             in a particular register.  */
3537
          if (asmspec && C_DECL_REGISTER (decl))
3538
            {
3539
              DECL_HARD_REGISTER (decl) = 1;
3540
              /* This cannot be done for a structure with volatile
3541
                 fields, on which DECL_REGISTER will have been
3542
                 reset.  */
3543
              if (!DECL_REGISTER (decl))
3544
                error ("cannot put object with volatile field into register");
3545
            }
3546
 
3547
          if (TREE_CODE (decl) != FUNCTION_DECL)
3548
            {
3549
              /* If we're building a variable sized type, and we might be
3550
                 reachable other than via the top of the current binding
3551
                 level, then create a new BIND_EXPR so that we deallocate
3552
                 the object at the right time.  */
3553
              /* Note that DECL_SIZE can be null due to errors.  */
3554
              if (DECL_SIZE (decl)
3555
                  && !TREE_CONSTANT (DECL_SIZE (decl))
3556
                  && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3557
                {
3558
                  tree bind;
3559
                  bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3560
                  TREE_SIDE_EFFECTS (bind) = 1;
3561
                  add_stmt (bind);
3562
                  BIND_EXPR_BODY (bind) = push_stmt_list ();
3563
                }
3564
              add_stmt (build_stmt (DECL_EXPR, decl));
3565
            }
3566
        }
3567
 
3568
 
3569
      if (!DECL_FILE_SCOPE_P (decl))
3570
        {
3571
          /* Recompute the RTL of a local array now
3572
             if it used to be an incomplete type.  */
3573
          if (was_incomplete
3574
              && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3575
            {
3576
              /* If we used it already as memory, it must stay in memory.  */
3577
              TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3578
              /* If it's still incomplete now, no init will save it.  */
3579
              if (DECL_SIZE (decl) == 0)
3580
                DECL_INITIAL (decl) = 0;
3581
            }
3582
        }
3583
    }
3584
 
3585
  /* If this was marked 'used', be sure it will be output.  */
3586
  if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3587
    mark_decl_referenced (decl);
3588
 
3589
  if (TREE_CODE (decl) == TYPE_DECL)
3590
    {
3591
      if (!DECL_FILE_SCOPE_P (decl)
3592
          && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3593
        add_stmt (build_stmt (DECL_EXPR, decl));
3594
 
3595
      rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3596
    }
3597
 
3598
  /* At the end of a declaration, throw away any variable type sizes
3599
     of types defined inside that declaration.  There is no use
3600
     computing them in the following function definition.  */
3601
  if (current_scope == file_scope)
3602
    get_pending_sizes ();
3603
 
3604
  /* Install a cleanup (aka destructor) if one was given.  */
3605
  if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3606
    {
3607
      tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3608
      if (attr)
3609
        {
3610
          tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3611
          tree cleanup_decl = lookup_name (cleanup_id);
3612
          tree cleanup;
3613
 
3614
          /* Build "cleanup(&decl)" for the destructor.  */
3615
          cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3616
          cleanup = build_tree_list (NULL_TREE, cleanup);
3617
          cleanup = build_function_call (cleanup_decl, cleanup);
3618
 
3619
          /* Don't warn about decl unused; the cleanup uses it.  */
3620
          TREE_USED (decl) = 1;
3621
          TREE_USED (cleanup_decl) = 1;
3622
 
3623
          /* Initialize EH, if we've been told to do so.  */
3624
          c_maybe_initialize_eh ();
3625
 
3626
          push_cleanup (decl, cleanup, false);
3627
        }
3628
    }
3629
}
3630
 
3631
/* Given a parsed parameter declaration, decode it into a PARM_DECL.  */
3632
 
3633
tree
3634
grokparm (const struct c_parm *parm)
3635
{
3636
  tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3637
                              NULL);
3638
 
3639
  decl_attributes (&decl, parm->attrs, 0);
3640
 
3641
  return decl;
3642
}
3643
 
3644
/* Given a parsed parameter declaration, decode it into a PARM_DECL
3645
   and push that on the current scope.  */
3646
 
3647
void
3648
push_parm_decl (const struct c_parm *parm)
3649
{
3650
  tree decl;
3651
 
3652
  decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3653
  decl_attributes (&decl, parm->attrs, 0);
3654
 
3655
  decl = pushdecl (decl);
3656
 
3657
  finish_decl (decl, NULL_TREE, NULL_TREE);
3658
}
3659
 
3660
/* Mark all the parameter declarations to date as forward decls.
3661
   Also diagnose use of this extension.  */
3662
 
3663
void
3664
mark_forward_parm_decls (void)
3665
{
3666
  struct c_binding *b;
3667
 
3668
  if (pedantic && !current_scope->warned_forward_parm_decls)
3669
    {
3670
      pedwarn ("ISO C forbids forward parameter declarations");
3671
      current_scope->warned_forward_parm_decls = true;
3672
    }
3673
 
3674
  for (b = current_scope->bindings; b; b = b->prev)
3675
    if (TREE_CODE (b->decl) == PARM_DECL)
3676
      TREE_ASM_WRITTEN (b->decl) = 1;
3677
}
3678
 
3679
/* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
3680
   literal, which may be an incomplete array type completed by the
3681
   initializer; INIT is a CONSTRUCTOR that initializes the compound
3682
   literal.  */
3683
 
3684
tree
3685
build_compound_literal (tree type, tree init)
3686
{
3687
  /* We do not use start_decl here because we have a type, not a declarator;
3688
     and do not use finish_decl because the decl should be stored inside
3689
     the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR.  */
3690
  tree decl;
3691
  tree complit;
3692
  tree stmt;
3693
 
3694
  if (type == error_mark_node)
3695
    return error_mark_node;
3696
 
3697
  decl = build_decl (VAR_DECL, NULL_TREE, type);
3698
  DECL_EXTERNAL (decl) = 0;
3699
  TREE_PUBLIC (decl) = 0;
3700
  TREE_STATIC (decl) = (current_scope == file_scope);
3701
  DECL_CONTEXT (decl) = current_function_decl;
3702
  TREE_USED (decl) = 1;
3703
  TREE_TYPE (decl) = type;
3704
  TREE_READONLY (decl) = TYPE_READONLY (type);
3705
  store_init_value (decl, init);
3706
 
3707
  if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3708
    {
3709
      int failure = complete_array_type (&TREE_TYPE (decl),
3710
                                         DECL_INITIAL (decl), true);
3711
      gcc_assert (!failure);
3712
 
3713
      type = TREE_TYPE (decl);
3714
      TREE_TYPE (DECL_INITIAL (decl)) = type;
3715
    }
3716
 
3717
  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3718
    return error_mark_node;
3719
 
3720
  stmt = build_stmt (DECL_EXPR, decl);
3721
  complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3722
  TREE_SIDE_EFFECTS (complit) = 1;
3723
 
3724
  layout_decl (decl, 0);
3725
 
3726
  if (TREE_STATIC (decl))
3727
    {
3728
      /* This decl needs a name for the assembler output.  */
3729
      set_compound_literal_name (decl);
3730
      DECL_DEFER_OUTPUT (decl) = 1;
3731
      DECL_COMDAT (decl) = 1;
3732
      DECL_ARTIFICIAL (decl) = 1;
3733
      DECL_IGNORED_P (decl) = 1;
3734
      pushdecl (decl);
3735
      rest_of_decl_compilation (decl, 1, 0);
3736
    }
3737
 
3738
  return complit;
3739
}
3740
 
3741
/* Determine whether TYPE is a structure with a flexible array member,
3742
   or a union containing such a structure (possibly recursively).  */
3743
 
3744
static bool
3745
flexible_array_type_p (tree type)
3746
{
3747
  tree x;
3748
  switch (TREE_CODE (type))
3749
    {
3750
    case RECORD_TYPE:
3751
      x = TYPE_FIELDS (type);
3752
      if (x == NULL_TREE)
3753
        return false;
3754
      while (TREE_CHAIN (x) != NULL_TREE)
3755
        x = TREE_CHAIN (x);
3756
      if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3757
          && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3758
          && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3759
          && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3760
        return true;
3761
      return false;
3762
    case UNION_TYPE:
3763
      for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3764
        {
3765
          if (flexible_array_type_p (TREE_TYPE (x)))
3766
            return true;
3767
        }
3768
      return false;
3769
    default:
3770
    return false;
3771
  }
3772
}
3773
 
3774
/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3775
   replacing with appropriate values if they are invalid.  */
3776
static void
3777
check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3778
{
3779
  tree type_mv;
3780
  unsigned int max_width;
3781
  unsigned HOST_WIDE_INT w;
3782
  const char *name = orig_name ? orig_name: _("<anonymous>");
3783
 
3784
  /* Detect and ignore out of range field width and process valid
3785
     field widths.  */
3786
  if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3787
      || TREE_CODE (*width) != INTEGER_CST)
3788
    {
3789
      error ("bit-field %qs width not an integer constant", name);
3790
      *width = integer_one_node;
3791
    }
3792
  else
3793
    {
3794
      constant_expression_warning (*width);
3795
      if (tree_int_cst_sgn (*width) < 0)
3796
        {
3797
          error ("negative width in bit-field %qs", name);
3798
          *width = integer_one_node;
3799
        }
3800
      else if (integer_zerop (*width) && orig_name)
3801
        {
3802
          error ("zero width for bit-field %qs", name);
3803
          *width = integer_one_node;
3804
        }
3805
    }
3806
 
3807
  /* Detect invalid bit-field type.  */
3808
  if (TREE_CODE (*type) != INTEGER_TYPE
3809
      && TREE_CODE (*type) != BOOLEAN_TYPE
3810
      && TREE_CODE (*type) != ENUMERAL_TYPE)
3811
    {
3812
      error ("bit-field %qs has invalid type", name);
3813
      *type = unsigned_type_node;
3814
    }
3815
 
3816
  type_mv = TYPE_MAIN_VARIANT (*type);
3817
  if (pedantic
3818
      && !in_system_header
3819
      && type_mv != integer_type_node
3820
      && type_mv != unsigned_type_node
3821
      && type_mv != boolean_type_node)
3822
    pedwarn ("type of bit-field %qs is a GCC extension", name);
3823
 
3824
  if (type_mv == boolean_type_node)
3825
    max_width = CHAR_TYPE_SIZE;
3826
  else
3827
    max_width = TYPE_PRECISION (*type);
3828
 
3829
  if (0 < compare_tree_int (*width, max_width))
3830
    {
3831
      error ("width of %qs exceeds its type", name);
3832
      w = max_width;
3833
      *width = build_int_cst (NULL_TREE, w);
3834
    }
3835
  else
3836
    w = tree_low_cst (*width, 1);
3837
 
3838
  if (TREE_CODE (*type) == ENUMERAL_TYPE)
3839
    {
3840
      struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3841
      if (!lt
3842
          || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3843
          || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3844
        warning (0, "%qs is narrower than values of its type", name);
3845
    }
3846
}
3847
 
3848
 
3849
/* Given declspecs and a declarator,
3850
   determine the name and type of the object declared
3851
   and construct a ..._DECL node for it.
3852
   (In one case we can return a ..._TYPE node instead.
3853
    For invalid input we sometimes return 0.)
3854
 
3855
   DECLSPECS is a c_declspecs structure for the declaration specifiers.
3856
 
3857
   DECL_CONTEXT says which syntactic context this declaration is in:
3858
     NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3859
     FUNCDEF for a function definition.  Like NORMAL but a few different
3860
      error messages in each case.  Return value may be zero meaning
3861
      this definition is too screwy to try to parse.
3862
     PARM for a parameter declaration (either within a function prototype
3863
      or before a function body).  Make a PARM_DECL, or return void_type_node.
3864
     TYPENAME if for a typename (in a cast or sizeof).
3865
      Don't make a DECL node; just return the ..._TYPE node.
3866
     FIELD for a struct or union field; make a FIELD_DECL.
3867
   INITIALIZED is true if the decl has an initializer.
3868
   WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3869
   representing the width of the bit-field.
3870
 
3871
   In the TYPENAME case, DECLARATOR is really an absolute declarator.
3872
   It may also be so in the PARM case, for a prototype where the
3873
   argument type is specified but not the name.
3874
 
3875
   This function is where the complicated C meanings of `static'
3876
   and `extern' are interpreted.  */
3877
 
3878
static tree
3879
grokdeclarator (const struct c_declarator *declarator,
3880
                struct c_declspecs *declspecs,
3881
                enum decl_context decl_context, bool initialized, tree *width)
3882
{
3883
  tree type = declspecs->type;
3884
  bool threadp = declspecs->thread_p;
3885
  enum c_storage_class storage_class = declspecs->storage_class;
3886
  int constp;
3887
  int restrictp;
3888
  int volatilep;
3889
  int type_quals = TYPE_UNQUALIFIED;
3890
  const char *name, *orig_name;
3891
  tree typedef_type = 0;
3892
  bool funcdef_flag = false;
3893
  bool funcdef_syntax = false;
3894
  int size_varies = 0;
3895
  tree decl_attr = declspecs->decl_attr;
3896
  int array_ptr_quals = TYPE_UNQUALIFIED;
3897
  tree array_ptr_attrs = NULL_TREE;
3898
  int array_parm_static = 0;
3899
  bool array_parm_vla_unspec_p = false;
3900
  tree returned_attrs = NULL_TREE;
3901
  bool bitfield = width != NULL;
3902
  tree element_type;
3903
  struct c_arg_info *arg_info = 0;
3904
 
3905
  if (decl_context == FUNCDEF)
3906
    funcdef_flag = true, decl_context = NORMAL;
3907
 
3908
  /* Look inside a declarator for the name being declared
3909
     and get it as a string, for an error message.  */
3910
  {
3911
    const struct c_declarator *decl = declarator;
3912
    name = 0;
3913
 
3914
    while (decl)
3915
      switch (decl->kind)
3916
        {
3917
        case cdk_function:
3918
        case cdk_array:
3919
        case cdk_pointer:
3920
          funcdef_syntax = (decl->kind == cdk_function);
3921
          decl = decl->declarator;
3922
          break;
3923
 
3924
        case cdk_attrs:
3925
          decl = decl->declarator;
3926
          break;
3927
 
3928
        case cdk_id:
3929
          if (decl->u.id)
3930
            name = IDENTIFIER_POINTER (decl->u.id);
3931
          decl = 0;
3932
          break;
3933
 
3934
        default:
3935
          gcc_unreachable ();
3936
        }
3937
    orig_name = name;
3938
    if (name == 0)
3939
      name = "type name";
3940
  }
3941
 
3942
  /* A function definition's declarator must have the form of
3943
     a function declarator.  */
3944
 
3945
  if (funcdef_flag && !funcdef_syntax)
3946
    return 0;
3947
 
3948
  /* If this looks like a function definition, make it one,
3949
     even if it occurs where parms are expected.
3950
     Then store_parm_decls will reject it and not use it as a parm.  */
3951
  if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3952
    decl_context = PARM;
3953
 
3954
  if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3955
    warn_deprecated_use (declspecs->type);
3956
 
3957
  if ((decl_context == NORMAL || decl_context == FIELD)
3958
      && current_scope == file_scope
3959
      && variably_modified_type_p (type, NULL_TREE))
3960
    {
3961
      error ("variably modified %qs at file scope", name);
3962
      type = integer_type_node;
3963
    }
3964
 
3965
  typedef_type = type;
3966
  size_varies = C_TYPE_VARIABLE_SIZE (type);
3967
 
3968
  /* Diagnose defaulting to "int".  */
3969
 
3970
  if (declspecs->default_int_p && !in_system_header)
3971
    {
3972
      /* Issue a warning if this is an ISO C 99 program or if
3973
         -Wreturn-type and this is a function, or if -Wimplicit;
3974
         prefer the former warning since it is more explicit.  */
3975
      if ((warn_implicit_int || warn_return_type || flag_isoc99)
3976
          && funcdef_flag)
3977
        warn_about_return_type = 1;
3978
      else if (warn_implicit_int || flag_isoc99)
3979
        pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3980
    }
3981
 
3982
  /* Adjust the type if a bit-field is being declared,
3983
     -funsigned-bitfields applied and the type is not explicitly
3984
     "signed".  */
3985
  if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3986
      && TREE_CODE (type) == INTEGER_TYPE)
3987
    type = c_common_unsigned_type (type);
3988
 
3989
  /* Figure out the type qualifiers for the declaration.  There are
3990
     two ways a declaration can become qualified.  One is something
3991
     like `const int i' where the `const' is explicit.  Another is
3992
     something like `typedef const int CI; CI i' where the type of the
3993
     declaration contains the `const'.  A third possibility is that
3994
     there is a type qualifier on the element type of a typedefed
3995
     array type, in which case we should extract that qualifier so
3996
     that c_apply_type_quals_to_decls receives the full list of
3997
     qualifiers to work with (C90 is not entirely clear about whether
3998
     duplicate qualifiers should be diagnosed in this case, but it
3999
     seems most appropriate to do so).  */
4000
  element_type = strip_array_types (type);
4001
  constp = declspecs->const_p + TYPE_READONLY (element_type);
4002
  restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4003
  volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4004
  if (pedantic && !flag_isoc99)
4005
    {
4006
      if (constp > 1)
4007
        pedwarn ("duplicate %<const%>");
4008
      if (restrictp > 1)
4009
        pedwarn ("duplicate %<restrict%>");
4010
      if (volatilep > 1)
4011
        pedwarn ("duplicate %<volatile%>");
4012
    }
4013
  if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4014
    type = TYPE_MAIN_VARIANT (type);
4015
  type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4016
                | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4017
                | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4018
 
4019
  /* Warn about storage classes that are invalid for certain
4020
     kinds of declarations (parameters, typenames, etc.).  */
4021
 
4022
  if (funcdef_flag
4023
      && (threadp
4024
          || storage_class == csc_auto
4025
          || storage_class == csc_register
4026
          || storage_class == csc_typedef))
4027
    {
4028
      if (storage_class == csc_auto
4029
          && (pedantic || current_scope == file_scope))
4030
        pedwarn ("function definition declared %<auto%>");
4031
      if (storage_class == csc_register)
4032
        error ("function definition declared %<register%>");
4033
      if (storage_class == csc_typedef)
4034
        error ("function definition declared %<typedef%>");
4035
      if (threadp)
4036
        error ("function definition declared %<__thread%>");
4037
      threadp = false;
4038
      if (storage_class == csc_auto
4039
          || storage_class == csc_register
4040
          || storage_class == csc_typedef)
4041
        storage_class = csc_none;
4042
    }
4043
  else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4044
    {
4045
      if (decl_context == PARM && storage_class == csc_register)
4046
        ;
4047
      else
4048
        {
4049
          switch (decl_context)
4050
            {
4051
            case FIELD:
4052
              error ("storage class specified for structure field %qs",
4053
                     name);
4054
              break;
4055
            case PARM:
4056
              error ("storage class specified for parameter %qs", name);
4057
              break;
4058
            default:
4059
              error ("storage class specified for typename");
4060
              break;
4061
            }
4062
          storage_class = csc_none;
4063
          threadp = false;
4064
        }
4065
    }
4066
  else if (storage_class == csc_extern
4067
           && initialized
4068
           && !funcdef_flag)
4069
    {
4070
      /* 'extern' with initialization is invalid if not at file scope.  */
4071
       if (current_scope == file_scope)
4072
         {
4073
           /* It is fine to have 'extern const' when compiling at C
4074
              and C++ intersection.  */
4075
           if (!(warn_cxx_compat && constp))
4076
             warning (0, "%qs initialized and declared %<extern%>", name);
4077
         }
4078
      else
4079
        error ("%qs has both %<extern%> and initializer", name);
4080
    }
4081
  else if (current_scope == file_scope)
4082
    {
4083
      if (storage_class == csc_auto)
4084
        error ("file-scope declaration of %qs specifies %<auto%>", name);
4085
      if (pedantic && storage_class == csc_register)
4086
        pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4087
    }
4088
  else
4089
    {
4090
      if (storage_class == csc_extern && funcdef_flag)
4091
        error ("nested function %qs declared %<extern%>", name);
4092
      else if (threadp && storage_class == csc_none)
4093
        {
4094
          error ("function-scope %qs implicitly auto and declared "
4095
                 "%<__thread%>",
4096
                 name);
4097
          threadp = false;
4098
        }
4099
    }
4100
 
4101
  /* Now figure out the structure of the declarator proper.
4102
     Descend through it, creating more complex types, until we reach
4103
     the declared identifier (or NULL_TREE, in an absolute declarator).
4104
     At each stage we maintain an unqualified version of the type
4105
     together with any qualifiers that should be applied to it with
4106
     c_build_qualified_type; this way, array types including
4107
     multidimensional array types are first built up in unqualified
4108
     form and then the qualified form is created with
4109
     TYPE_MAIN_VARIANT pointing to the unqualified form.  */
4110
 
4111
  while (declarator && declarator->kind != cdk_id)
4112
    {
4113
      if (type == error_mark_node)
4114
        {
4115
          declarator = declarator->declarator;
4116
          continue;
4117
        }
4118
 
4119
      /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4120
         a cdk_pointer (for *...),
4121
         a cdk_function (for ...(...)),
4122
         a cdk_attrs (for nested attributes),
4123
         or a cdk_id (for the name being declared
4124
         or the place in an absolute declarator
4125
         where the name was omitted).
4126
         For the last case, we have just exited the loop.
4127
 
4128
         At this point, TYPE is the type of elements of an array,
4129
         or for a function to return, or for a pointer to point to.
4130
         After this sequence of ifs, TYPE is the type of the
4131
         array or function or pointer, and DECLARATOR has had its
4132
         outermost layer removed.  */
4133
 
4134
      if (array_ptr_quals != TYPE_UNQUALIFIED
4135
          || array_ptr_attrs != NULL_TREE
4136
          || array_parm_static)
4137
        {
4138
          /* Only the innermost declarator (making a parameter be of
4139
             array type which is converted to pointer type)
4140
             may have static or type qualifiers.  */
4141
          error ("static or type qualifiers in non-parameter array declarator");
4142
          array_ptr_quals = TYPE_UNQUALIFIED;
4143
          array_ptr_attrs = NULL_TREE;
4144
          array_parm_static = 0;
4145
        }
4146
 
4147
      switch (declarator->kind)
4148
        {
4149
        case cdk_attrs:
4150
          {
4151
            /* A declarator with embedded attributes.  */
4152
            tree attrs = declarator->u.attrs;
4153
            const struct c_declarator *inner_decl;
4154
            int attr_flags = 0;
4155
            declarator = declarator->declarator;
4156
            inner_decl = declarator;
4157
            while (inner_decl->kind == cdk_attrs)
4158
              inner_decl = inner_decl->declarator;
4159
            if (inner_decl->kind == cdk_id)
4160
              attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4161
            else if (inner_decl->kind == cdk_function)
4162
              attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4163
            else if (inner_decl->kind == cdk_array)
4164
              attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4165
            returned_attrs = decl_attributes (&type,
4166
                                              chainon (returned_attrs, attrs),
4167
                                              attr_flags);
4168
            break;
4169
          }
4170
        case cdk_array:
4171
          {
4172
            tree itype = NULL_TREE;
4173
            tree size = declarator->u.array.dimen;
4174
            /* The index is a signed object `sizetype' bits wide.  */
4175
            tree index_type = c_common_signed_type (sizetype);
4176
 
4177
            array_ptr_quals = declarator->u.array.quals;
4178
            array_ptr_attrs = declarator->u.array.attrs;
4179
            array_parm_static = declarator->u.array.static_p;
4180
            array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4181
 
4182
            declarator = declarator->declarator;
4183
 
4184
            /* Check for some types that there cannot be arrays of.  */
4185
 
4186
            if (VOID_TYPE_P (type))
4187
              {
4188
                error ("declaration of %qs as array of voids", name);
4189
                type = error_mark_node;
4190
              }
4191
 
4192
            if (TREE_CODE (type) == FUNCTION_TYPE)
4193
              {
4194
                error ("declaration of %qs as array of functions", name);
4195
                type = error_mark_node;
4196
              }
4197
 
4198
            if (pedantic && !in_system_header && flexible_array_type_p (type))
4199
              pedwarn ("invalid use of structure with flexible array member");
4200
 
4201
            if (size == error_mark_node)
4202
              type = error_mark_node;
4203
 
4204
            if (type == error_mark_node)
4205
              continue;
4206
 
4207
            /* If size was specified, set ITYPE to a range-type for
4208
               that size.  Otherwise, ITYPE remains null.  finish_decl
4209
               may figure it out from an initial value.  */
4210
 
4211
            if (size)
4212
              {
4213
                /* Strip NON_LVALUE_EXPRs since we aren't using as an
4214
                   lvalue.  */
4215
                STRIP_TYPE_NOPS (size);
4216
 
4217
                if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4218
                  {
4219
                    error ("size of array %qs has non-integer type", name);
4220
                    size = integer_one_node;
4221
                  }
4222
 
4223
                if (pedantic && integer_zerop (size))
4224
                  pedwarn ("ISO C forbids zero-size array %qs", name);
4225
 
4226
                if (TREE_CODE (size) == INTEGER_CST)
4227
                  {
4228
                    constant_expression_warning (size);
4229
                    if (tree_int_cst_sgn (size) < 0)
4230
                      {
4231
                        error ("size of array %qs is negative", name);
4232
                        size = integer_one_node;
4233
                      }
4234
                  }
4235
                else if ((decl_context == NORMAL || decl_context == FIELD)
4236
                         && current_scope == file_scope)
4237
                  {
4238
                    error ("variably modified %qs at file scope", name);
4239
                    size = integer_one_node;
4240
                  }
4241
                else
4242
                  {
4243
                    /* Make sure the array size remains visibly
4244
                       nonconstant even if it is (eg) a const variable
4245
                       with known value.  */
4246
                    size_varies = 1;
4247
 
4248
                    if (!flag_isoc99 && pedantic)
4249
                      {
4250
                        if (TREE_CONSTANT (size))
4251
                          pedwarn ("ISO C90 forbids array %qs whose size "
4252
                                   "can%'t be evaluated",
4253
                                   name);
4254
                        else
4255
                          pedwarn ("ISO C90 forbids variable-size array %qs",
4256
                                   name);
4257
                      }
4258
                  }
4259
 
4260
                if (integer_zerop (size))
4261
                  {
4262
                    /* A zero-length array cannot be represented with
4263
                       an unsigned index type, which is what we'll
4264
                       get with build_index_type.  Create an
4265
                       open-ended range instead.  */
4266
                    itype = build_range_type (sizetype, size, NULL_TREE);
4267
                  }
4268
                else
4269
                  {
4270
                    /* Arrange for the SAVE_EXPR on the inside of the
4271
                       MINUS_EXPR, which allows the -1 to get folded
4272
                       with the +1 that happens when building TYPE_SIZE.  */
4273
                    if (size_varies)
4274
                      size = variable_size (size);
4275
 
4276
                    /* Compute the maximum valid index, that is, size
4277
                       - 1.  Do the calculation in index_type, so that
4278
                       if it is a variable the computations will be
4279
                       done in the proper mode.  */
4280
                    itype = fold_build2 (MINUS_EXPR, index_type,
4281
                                         convert (index_type, size),
4282
                                         convert (index_type,
4283
                                                  size_one_node));
4284
 
4285
                    /* If that overflowed, the array is too big.  ???
4286
                       While a size of INT_MAX+1 technically shouldn't
4287
                       cause an overflow (because we subtract 1), the
4288
                       overflow is recorded during the conversion to
4289
                       index_type, before the subtraction.  Handling
4290
                       this case seems like an unnecessary
4291
                       complication.  */
4292
                    if (TREE_CODE (itype) == INTEGER_CST
4293
                        && TREE_OVERFLOW (itype))
4294
                      {
4295
                        error ("size of array %qs is too large", name);
4296
                        type = error_mark_node;
4297
                        continue;
4298
                      }
4299
 
4300
                    itype = build_index_type (itype);
4301
                  }
4302
              }
4303
            else if (decl_context == FIELD)
4304
              {
4305
                if (pedantic && !flag_isoc99 && !in_system_header)
4306
                  pedwarn ("ISO C90 does not support flexible array members");
4307
 
4308
                /* ISO C99 Flexible array members are effectively
4309
                   identical to GCC's zero-length array extension.  */
4310
                itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4311
              }
4312
            else if (decl_context == PARM)
4313
              {
4314
                if (array_parm_vla_unspec_p)
4315
                  {
4316
                    if (! orig_name)
4317
                      {
4318
                        /* C99 6.7.5.2p4 */
4319
                        error ("%<[*]%> not allowed in other than a declaration");
4320
                      }
4321
 
4322
                    itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4323
                    size_varies = 1;
4324
                  }
4325
              }
4326
            else if (decl_context == TYPENAME)
4327
              {
4328
                if (array_parm_vla_unspec_p)
4329
                  {
4330
                    /* The error is printed elsewhere.  We use this to
4331
                       avoid messing up with incomplete array types of
4332
                       the same type, that would otherwise be modified
4333
                       below.  */
4334
                    itype = build_range_type (sizetype, size_zero_node,
4335
                                              NULL_TREE);
4336
                  }
4337
              }
4338
 
4339
             /* Complain about arrays of incomplete types.  */
4340
            if (!COMPLETE_TYPE_P (type))
4341
              {
4342
                error ("array type has incomplete element type");
4343
                type = error_mark_node;
4344
              }
4345
            else
4346
            /* When itype is NULL, a shared incomplete array type is
4347
               returned for all array of a given type.  Elsewhere we
4348
               make sure we don't complete that type before copying
4349
               it, but here we want to make sure we don't ever
4350
               modify the shared type, so we gcc_assert (itype)
4351
               below.  */
4352
              type = build_array_type (type, itype);
4353
 
4354
            if (type != error_mark_node)
4355
              {
4356
                if (size_varies)
4357
                  {
4358
                    /* It is ok to modify type here even if itype is
4359
                       NULL: if size_varies, we're in a
4360
                       multi-dimensional array and the inner type has
4361
                       variable size, so the enclosing shared array type
4362
                       must too.  */
4363
                    if (size && TREE_CODE (size) == INTEGER_CST)
4364
                      type
4365
                        = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4366
                    C_TYPE_VARIABLE_SIZE (type) = 1;
4367
                  }
4368
 
4369
                /* The GCC extension for zero-length arrays differs from
4370
                   ISO flexible array members in that sizeof yields
4371
                   zero.  */
4372
                if (size && integer_zerop (size))
4373
                  {
4374
                    gcc_assert (itype);
4375
                    TYPE_SIZE (type) = bitsize_zero_node;
4376
                    TYPE_SIZE_UNIT (type) = size_zero_node;
4377
                  }
4378
                if (array_parm_vla_unspec_p)
4379
                  {
4380
                    gcc_assert (itype);
4381
                    /* The type is complete.  C99 6.7.5.2p4  */
4382
                    TYPE_SIZE (type) = bitsize_zero_node;
4383
                    TYPE_SIZE_UNIT (type) = size_zero_node;
4384
                  }
4385
              }
4386
 
4387
            if (decl_context != PARM
4388
                && (array_ptr_quals != TYPE_UNQUALIFIED
4389
                    || array_ptr_attrs != NULL_TREE
4390
                    || array_parm_static))
4391
              {
4392
                error ("static or type qualifiers in non-parameter array declarator");
4393
                array_ptr_quals = TYPE_UNQUALIFIED;
4394
                array_ptr_attrs = NULL_TREE;
4395
                array_parm_static = 0;
4396
              }
4397
            break;
4398
          }
4399
        case cdk_function:
4400
          {
4401
            /* Say it's a definition only for the declarator closest
4402
               to the identifier, apart possibly from some
4403
               attributes.  */
4404
            bool really_funcdef = false;
4405
            tree arg_types;
4406
            if (funcdef_flag)
4407
              {
4408
                const struct c_declarator *t = declarator->declarator;
4409
                while (t->kind == cdk_attrs)
4410
                  t = t->declarator;
4411
                really_funcdef = (t->kind == cdk_id);
4412
              }
4413
 
4414
            /* Declaring a function type.  Make sure we have a valid
4415
               type for the function to return.  */
4416
            if (type == error_mark_node)
4417
              continue;
4418
 
4419
            size_varies = 0;
4420
 
4421
            /* Warn about some types functions can't return.  */
4422
            if (TREE_CODE (type) == FUNCTION_TYPE)
4423
              {
4424
                error ("%qs declared as function returning a function", name);
4425
                type = integer_type_node;
4426
              }
4427
            if (TREE_CODE (type) == ARRAY_TYPE)
4428
              {
4429
                error ("%qs declared as function returning an array", name);
4430
                type = integer_type_node;
4431
              }
4432
 
4433
            /* Construct the function type and go to the next
4434
               inner layer of declarator.  */
4435
            arg_info = declarator->u.arg_info;
4436
            arg_types = grokparms (arg_info, really_funcdef);
4437
            if (really_funcdef)
4438
              put_pending_sizes (arg_info->pending_sizes);
4439
 
4440
            /* Type qualifiers before the return type of the function
4441
               qualify the return type, not the function type.  */
4442
            if (type_quals)
4443
              {
4444
                /* Type qualifiers on a function return type are
4445
                   normally permitted by the standard but have no
4446
                   effect, so give a warning at -Wreturn-type.
4447
                   Qualifiers on a void return type are banned on
4448
                   function definitions in ISO C; GCC used to used
4449
                   them for noreturn functions.  */
4450
                if (VOID_TYPE_P (type) && really_funcdef)
4451
                  pedwarn ("function definition has qualified void return type");
4452
                else
4453
                  warning (OPT_Wreturn_type,
4454
                           "type qualifiers ignored on function return type");
4455
 
4456
                type = c_build_qualified_type (type, type_quals);
4457
              }
4458
            type_quals = TYPE_UNQUALIFIED;
4459
 
4460
            type = build_function_type (type, arg_types);
4461
            declarator = declarator->declarator;
4462
 
4463
            /* Set the TYPE_CONTEXTs for each tagged type which is local to
4464
               the formal parameter list of this FUNCTION_TYPE to point to
4465
               the FUNCTION_TYPE node itself.  */
4466
            {
4467
              tree link;
4468
 
4469
              for (link = arg_info->tags;
4470
                   link;
4471
                   link = TREE_CHAIN (link))
4472
                TYPE_CONTEXT (TREE_VALUE (link)) = type;
4473
            }
4474
            break;
4475
          }
4476
        case cdk_pointer:
4477
          {
4478
            /* Merge any constancy or volatility into the target type
4479
               for the pointer.  */
4480
 
4481
            if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4482
                && type_quals)
4483
              pedwarn ("ISO C forbids qualified function types");
4484
            if (type_quals)
4485
              type = c_build_qualified_type (type, type_quals);
4486
            size_varies = 0;
4487
 
4488
            /* When the pointed-to type involves components of variable size,
4489
               care must be taken to ensure that the size evaluation code is
4490
               emitted early enough to dominate all the possible later uses
4491
               and late enough for the variables on which it depends to have
4492
               been assigned.
4493
 
4494
               This is expected to happen automatically when the pointed-to
4495
               type has a name/declaration of it's own, but special attention
4496
               is required if the type is anonymous.
4497
 
4498
               We handle the NORMAL and FIELD contexts here by attaching an
4499
               artificial TYPE_DECL to such pointed-to type.  This forces the
4500
               sizes evaluation at a safe point and ensures it is not deferred
4501
               until e.g. within a deeper conditional context.
4502
 
4503
               We expect nothing to be needed here for PARM or TYPENAME.
4504
               Pushing a TYPE_DECL at this point for TYPENAME would actually
4505
               be incorrect, as we might be in the middle of an expression
4506
               with side effects on the pointed-to type size "arguments" prior
4507
               to the pointer declaration point and the fake TYPE_DECL in the
4508
               enclosing context would force the size evaluation prior to the
4509
               side effects.  */
4510
 
4511
            if (!TYPE_NAME (type)
4512
                && (decl_context == NORMAL || decl_context == FIELD)
4513
                && variably_modified_type_p (type, NULL_TREE))
4514
              {
4515
                tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4516
                DECL_ARTIFICIAL (decl) = 1;
4517
                pushdecl (decl);
4518
                finish_decl (decl, NULL_TREE, NULL_TREE);
4519
                TYPE_NAME (type) = decl;
4520
              }
4521
 
4522
            type = build_pointer_type (type);
4523
 
4524
            /* Process type qualifiers (such as const or volatile)
4525
               that were given inside the `*'.  */
4526
            type_quals = declarator->u.pointer_quals;
4527
 
4528
            declarator = declarator->declarator;
4529
            break;
4530
          }
4531
        default:
4532
          gcc_unreachable ();
4533
        }
4534
    }
4535
 
4536
  /* Now TYPE has the actual type, apart from any qualifiers in
4537
     TYPE_QUALS.  */
4538
 
4539
  /* Check the type and width of a bit-field.  */
4540
  if (bitfield)
4541
    check_bitfield_type_and_width (&type, width, orig_name);
4542
 
4543
  /* Did array size calculations overflow?  */
4544
 
4545
  if (TREE_CODE (type) == ARRAY_TYPE
4546
      && COMPLETE_TYPE_P (type)
4547
      && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4548
      && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4549
    {
4550
      error ("size of array %qs is too large", name);
4551
      /* If we proceed with the array type as it is, we'll eventually
4552
         crash in tree_low_cst().  */
4553
      type = error_mark_node;
4554
    }
4555
 
4556
  /* If this is declaring a typedef name, return a TYPE_DECL.  */
4557
 
4558
  if (storage_class == csc_typedef)
4559
    {
4560
      tree decl;
4561
      if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4562
          && type_quals)
4563
        pedwarn ("ISO C forbids qualified function types");
4564
      if (type_quals)
4565
        type = c_build_qualified_type (type, type_quals);
4566
      decl = build_decl (TYPE_DECL, declarator->u.id, type);
4567
      if (declspecs->explicit_signed_p)
4568
        C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4569
      decl_attributes (&decl, returned_attrs, 0);
4570
      if (declspecs->inline_p)
4571
        pedwarn ("typedef %q+D declared %<inline%>", decl);
4572
      return decl;
4573
    }
4574
 
4575
  /* If this is a type name (such as, in a cast or sizeof),
4576
     compute the type and return it now.  */
4577
 
4578
  if (decl_context == TYPENAME)
4579
    {
4580
      /* Note that the grammar rejects storage classes in typenames
4581
         and fields.  */
4582
      gcc_assert (storage_class == csc_none && !threadp
4583
                  && !declspecs->inline_p);
4584
      if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4585
          && type_quals)
4586
        pedwarn ("ISO C forbids const or volatile function types");
4587
      if (type_quals)
4588
        type = c_build_qualified_type (type, type_quals);
4589
      decl_attributes (&type, returned_attrs, 0);
4590
      return type;
4591
    }
4592
 
4593
  if (pedantic && decl_context == FIELD
4594
      && variably_modified_type_p (type, NULL_TREE))
4595
    {
4596
      /* C99 6.7.2.1p8 */
4597
      pedwarn ("a member of a structure or union cannot have a variably modified type");
4598
    }
4599
 
4600
  /* Aside from typedefs and type names (handle above),
4601
     `void' at top level (not within pointer)
4602
     is allowed only in public variables.
4603
     We don't complain about parms either, but that is because
4604
     a better error message can be made later.  */
4605
 
4606
  if (VOID_TYPE_P (type) && decl_context != PARM
4607
      && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4608
            && (storage_class == csc_extern
4609
                || (current_scope == file_scope
4610
                    && !(storage_class == csc_static
4611
                         || storage_class == csc_register)))))
4612
    {
4613
      error ("variable or field %qs declared void", name);
4614
      type = integer_type_node;
4615
    }
4616
 
4617
  /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4618
     or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
4619
 
4620
  {
4621
    tree decl;
4622
 
4623
    if (decl_context == PARM)
4624
      {
4625
        tree type_as_written;
4626
        tree promoted_type;
4627
 
4628
        /* A parameter declared as an array of T is really a pointer to T.
4629
           One declared as a function is really a pointer to a function.  */
4630
 
4631
        if (TREE_CODE (type) == ARRAY_TYPE)
4632
          {
4633
            /* Transfer const-ness of array into that of type pointed to.  */
4634
            type = TREE_TYPE (type);
4635
            if (type_quals)
4636
              type = c_build_qualified_type (type, type_quals);
4637
            type = build_pointer_type (type);
4638
            type_quals = array_ptr_quals;
4639
 
4640
            /* We don't yet implement attributes in this context.  */
4641
            if (array_ptr_attrs != NULL_TREE)
4642
              warning (OPT_Wattributes,
4643
                       "attributes in parameter array declarator ignored");
4644
 
4645
            size_varies = 0;
4646
          }
4647
        else if (TREE_CODE (type) == FUNCTION_TYPE)
4648
          {
4649
            if (pedantic && type_quals)
4650
              pedwarn ("ISO C forbids qualified function types");
4651
            if (type_quals)
4652
              type = c_build_qualified_type (type, type_quals);
4653
            type = build_pointer_type (type);
4654
            type_quals = TYPE_UNQUALIFIED;
4655
          }
4656
        else if (type_quals)
4657
          type = c_build_qualified_type (type, type_quals);
4658
 
4659
        type_as_written = type;
4660
 
4661
        decl = build_decl (PARM_DECL, declarator->u.id, type);
4662
        if (size_varies)
4663
          C_DECL_VARIABLE_SIZE (decl) = 1;
4664
 
4665
        /* Compute the type actually passed in the parmlist,
4666
           for the case where there is no prototype.
4667
           (For example, shorts and chars are passed as ints.)
4668
           When there is a prototype, this is overridden later.  */
4669
 
4670
        if (type == error_mark_node)
4671
          promoted_type = type;
4672
        else
4673
          promoted_type = c_type_promotes_to (type);
4674
 
4675
        DECL_ARG_TYPE (decl) = promoted_type;
4676
        if (declspecs->inline_p)
4677
          pedwarn ("parameter %q+D declared %<inline%>", decl);
4678
      }
4679
    else if (decl_context == FIELD)
4680
      {
4681
        /* Note that the grammar rejects storage classes in typenames
4682
           and fields.  */
4683
        gcc_assert (storage_class == csc_none && !threadp
4684
                    && !declspecs->inline_p);
4685
 
4686
        /* Structure field.  It may not be a function.  */
4687
 
4688
        if (TREE_CODE (type) == FUNCTION_TYPE)
4689
          {
4690
            error ("field %qs declared as a function", name);
4691
            type = build_pointer_type (type);
4692
          }
4693
        else if (TREE_CODE (type) != ERROR_MARK
4694
                 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4695
          {
4696
            error ("field %qs has incomplete type", name);
4697
            type = error_mark_node;
4698
          }
4699
        type = c_build_qualified_type (type, type_quals);
4700
        decl = build_decl (FIELD_DECL, declarator->u.id, type);
4701
        DECL_NONADDRESSABLE_P (decl) = bitfield;
4702
 
4703
        if (size_varies)
4704
          C_DECL_VARIABLE_SIZE (decl) = 1;
4705
      }
4706
    else if (TREE_CODE (type) == FUNCTION_TYPE)
4707
      {
4708
        if (storage_class == csc_register || threadp)
4709
          {
4710
            error ("invalid storage class for function %qs", name);
4711
           }
4712
        else if (current_scope != file_scope)
4713
          {
4714
            /* Function declaration not at file scope.  Storage
4715
               classes other than `extern' are not allowed, C99
4716
               6.7.1p5, and `extern' makes no difference.  However,
4717
               GCC allows 'auto', perhaps with 'inline', to support
4718
               nested functions.  */
4719
            if (storage_class == csc_auto)
4720
              {
4721
                if (pedantic)
4722
                  pedwarn ("invalid storage class for function %qs", name);
4723
              }
4724
            else if (storage_class == csc_static)
4725
              {
4726
                error ("invalid storage class for function %qs", name);
4727
                if (funcdef_flag)
4728
                  storage_class = declspecs->storage_class = csc_none;
4729
                else
4730
                  return 0;
4731
              }
4732
          }
4733
 
4734
        decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4735
        decl = build_decl_attribute_variant (decl, decl_attr);
4736
 
4737
        DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4738
 
4739
        if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4740
          pedwarn ("ISO C forbids qualified function types");
4741
 
4742
        /* GNU C interprets a volatile-qualified function type to indicate
4743
           that the function does not return.  */
4744
        if ((type_quals & TYPE_QUAL_VOLATILE)
4745
            && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4746
          warning (0, "%<noreturn%> function returns non-void value");
4747
 
4748
        /* Every function declaration is an external reference
4749
           (DECL_EXTERNAL) except for those which are not at file
4750
           scope and are explicitly declared "auto".  This is
4751
           forbidden by standard C (C99 6.7.1p5) and is interpreted by
4752
           GCC to signify a forward declaration of a nested function.  */
4753
        if (storage_class == csc_auto && current_scope != file_scope)
4754
          DECL_EXTERNAL (decl) = 0;
4755
        else
4756
          DECL_EXTERNAL (decl) = 1;
4757
 
4758
        /* Record absence of global scope for `static' or `auto'.  */
4759
        TREE_PUBLIC (decl)
4760
          = !(storage_class == csc_static || storage_class == csc_auto);
4761
 
4762
        /* For a function definition, record the argument information
4763
           block where store_parm_decls will look for it.  */
4764
        if (funcdef_flag)
4765
          current_function_arg_info = arg_info;
4766
 
4767
        if (declspecs->default_int_p)
4768
          C_FUNCTION_IMPLICIT_INT (decl) = 1;
4769
 
4770
        /* Record presence of `inline', if it is reasonable.  */
4771
        if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4772
          {
4773
            if (declspecs->inline_p)
4774
              pedwarn ("cannot inline function %<main%>");
4775
          }
4776
        else if (declspecs->inline_p)
4777
          {
4778
            /* Record that the function is declared `inline'.  */
4779
            DECL_DECLARED_INLINE_P (decl) = 1;
4780
 
4781
            /* Do not mark bare declarations as DECL_INLINE.  Doing so
4782
               in the presence of multiple declarations can result in
4783
               the abstract origin pointing between the declarations,
4784
               which will confuse dwarf2out.  */
4785
            if (initialized)
4786
              {
4787
                DECL_INLINE (decl) = 1;
4788
                if (storage_class == csc_extern)
4789
                  current_extern_inline = 1;
4790
              }
4791
          }
4792
        /* If -finline-functions, assume it can be inlined.  This does
4793
           two things: let the function be deferred until it is actually
4794
           needed, and let dwarf2 know that the function is inlinable.  */
4795
        else if (flag_inline_trees == 2 && initialized)
4796
          DECL_INLINE (decl) = 1;
4797
      }
4798
    else
4799
      {
4800
        /* It's a variable.  */
4801
        /* An uninitialized decl with `extern' is a reference.  */
4802
        int extern_ref = !initialized && storage_class == csc_extern;
4803
 
4804
        type = c_build_qualified_type (type, type_quals);
4805
 
4806
        /* C99 6.2.2p7: It is invalid (compile-time undefined
4807
           behavior) to create an 'extern' declaration for a
4808
           variable if there is a global declaration that is
4809
           'static' and the global declaration is not visible.
4810
           (If the static declaration _is_ currently visible,
4811
           the 'extern' declaration is taken to refer to that decl.) */
4812
        if (extern_ref && current_scope != file_scope)
4813
          {
4814
            tree global_decl  = identifier_global_value (declarator->u.id);
4815
            tree visible_decl = lookup_name (declarator->u.id);
4816
 
4817
            if (global_decl
4818
                && global_decl != visible_decl
4819
                && TREE_CODE (global_decl) == VAR_DECL
4820
                && !TREE_PUBLIC (global_decl))
4821
              error ("variable previously declared %<static%> redeclared "
4822
                     "%<extern%>");
4823
          }
4824
 
4825
        decl = build_decl (VAR_DECL, declarator->u.id, type);
4826
        DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4827
        if (size_varies)
4828
          C_DECL_VARIABLE_SIZE (decl) = 1;
4829
 
4830
        if (declspecs->inline_p)
4831
          pedwarn ("variable %q+D declared %<inline%>", decl);
4832
 
4833
        /* At file scope, an initialized extern declaration may follow
4834
           a static declaration.  In that case, DECL_EXTERNAL will be
4835
           reset later in start_decl.  */
4836
        DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4837
 
4838
        /* At file scope, the presence of a `static' or `register' storage
4839
           class specifier, or the absence of all storage class specifiers
4840
           makes this declaration a definition (perhaps tentative).  Also,
4841
           the absence of `static' makes it public.  */
4842
        if (current_scope == file_scope)
4843
          {
4844
            TREE_PUBLIC (decl) = storage_class != csc_static;
4845
            TREE_STATIC (decl) = !extern_ref;
4846
          }
4847
        /* Not at file scope, only `static' makes a static definition.  */
4848
        else
4849
          {
4850
            TREE_STATIC (decl) = (storage_class == csc_static);
4851
            TREE_PUBLIC (decl) = extern_ref;
4852
          }
4853
 
4854
        if (threadp)
4855
          {
4856
            if (targetm.have_tls)
4857
              DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4858
            else
4859
              /* A mere warning is sure to result in improper semantics
4860
                 at runtime.  Don't bother to allow this to compile.  */
4861
              error ("thread-local storage not supported for this target");
4862
          }
4863
      }
4864
 
4865
    if (storage_class == csc_extern
4866
        && variably_modified_type_p (type, NULL_TREE))
4867
      {
4868
        /* C99 6.7.5.2p2 */
4869
        error ("object with variably modified type must have no linkage");
4870
      }
4871
 
4872
    /* Record `register' declaration for warnings on &
4873
       and in case doing stupid register allocation.  */
4874
 
4875
    if (storage_class == csc_register)
4876
      {
4877
        C_DECL_REGISTER (decl) = 1;
4878
        DECL_REGISTER (decl) = 1;
4879
      }
4880
 
4881
    /* Record constancy and volatility.  */
4882
    c_apply_type_quals_to_decl (type_quals, decl);
4883
 
4884
    /* If a type has volatile components, it should be stored in memory.
4885
       Otherwise, the fact that those components are volatile
4886
       will be ignored, and would even crash the compiler.
4887
       Of course, this only makes sense on  VAR,PARM, and RESULT decl's.   */
4888
    if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4889
        && (TREE_CODE (decl) == VAR_DECL ||  TREE_CODE (decl) == PARM_DECL
4890
          || TREE_CODE (decl) == RESULT_DECL))
4891
      {
4892
        /* It is not an error for a structure with volatile fields to
4893
           be declared register, but reset DECL_REGISTER since it
4894
           cannot actually go in a register.  */
4895
        int was_reg = C_DECL_REGISTER (decl);
4896
        C_DECL_REGISTER (decl) = 0;
4897
        DECL_REGISTER (decl) = 0;
4898
        c_mark_addressable (decl);
4899
        C_DECL_REGISTER (decl) = was_reg;
4900
      }
4901
 
4902
  /* This is the earliest point at which we might know the assembler
4903
     name of a variable.  Thus, if it's known before this, die horribly.  */
4904
    gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4905
 
4906
    decl_attributes (&decl, returned_attrs, 0);
4907
 
4908
    return decl;
4909
  }
4910
}
4911
 
4912
/* Decode the parameter-list info for a function type or function definition.
4913
   The argument is the value returned by `get_parm_info' (or made in c-parse.c
4914
   if there is an identifier list instead of a parameter decl list).
4915
   These two functions are separate because when a function returns
4916
   or receives functions then each is called multiple times but the order
4917
   of calls is different.  The last call to `grokparms' is always the one
4918
   that contains the formal parameter names of a function definition.
4919
 
4920
   Return a list of arg types to use in the FUNCTION_TYPE for this function.
4921
 
4922
   FUNCDEF_FLAG is true for a function definition, false for
4923
   a mere declaration.  A nonempty identifier-list gets an error message
4924
   when FUNCDEF_FLAG is false.  */
4925
 
4926
static tree
4927
grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4928
{
4929
  tree arg_types = arg_info->types;
4930
 
4931
  if (funcdef_flag && arg_info->had_vla_unspec)
4932
    {
4933
      /* A function definition isn't function prototype scope C99 6.2.1p4.  */
4934
      /* C99 6.7.5.2p4 */
4935
      error ("%<[*]%> not allowed in other than function prototype scope");
4936
    }
4937
 
4938
  if (arg_types == 0 && !funcdef_flag && !in_system_header)
4939
    warning (OPT_Wstrict_prototypes,
4940
             "function declaration isn%'t a prototype");
4941
 
4942
  if (arg_types == error_mark_node)
4943
    return 0;  /* don't set TYPE_ARG_TYPES in this case */
4944
 
4945
  else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4946
    {
4947
      if (!funcdef_flag)
4948
        pedwarn ("parameter names (without types) in function declaration");
4949
 
4950
      arg_info->parms = arg_info->types;
4951
      arg_info->types = 0;
4952
      return 0;
4953
    }
4954
  else
4955
    {
4956
      tree parm, type, typelt;
4957
      unsigned int parmno;
4958
 
4959
      /* If there is a parameter of incomplete type in a definition,
4960
         this is an error.  In a declaration this is valid, and a
4961
         struct or union type may be completed later, before any calls
4962
         or definition of the function.  In the case where the tag was
4963
         first declared within the parameter list, a warning has
4964
         already been given.  If a parameter has void type, then
4965
         however the function cannot be defined or called, so
4966
         warn.  */
4967
 
4968
      for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4969
           parm;
4970
           parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4971
        {
4972
          type = TREE_VALUE (typelt);
4973
          if (type == error_mark_node)
4974
            continue;
4975
 
4976
          if (!COMPLETE_TYPE_P (type))
4977
            {
4978
              if (funcdef_flag)
4979
                {
4980
                  if (DECL_NAME (parm))
4981
                    error ("parameter %u (%q+D) has incomplete type",
4982
                           parmno, parm);
4983
                  else
4984
                    error ("%Jparameter %u has incomplete type",
4985
                           parm, parmno);
4986
 
4987
                  TREE_VALUE (typelt) = error_mark_node;
4988
                  TREE_TYPE (parm) = error_mark_node;
4989
                }
4990
              else if (VOID_TYPE_P (type))
4991
                {
4992
                  if (DECL_NAME (parm))
4993
                    warning (0, "parameter %u (%q+D) has void type",
4994
                             parmno, parm);
4995
                  else
4996
                    warning (0, "%Jparameter %u has void type",
4997
                             parm, parmno);
4998
                }
4999
            }
5000
 
5001
          if (DECL_NAME (parm) && TREE_USED (parm))
5002
            warn_if_shadowing (parm);
5003
        }
5004
      return arg_types;
5005
    }
5006
}
5007
 
5008
/* Take apart the current scope and return a c_arg_info structure with
5009
   info on a parameter list just parsed.
5010
 
5011
   This structure is later fed to 'grokparms' and 'store_parm_decls'.
5012
 
5013
   ELLIPSIS being true means the argument list ended in '...' so don't
5014
   append a sentinel (void_list_node) to the end of the type-list.  */
5015
 
5016
struct c_arg_info *
5017
get_parm_info (bool ellipsis)
5018
{
5019
  struct c_binding *b = current_scope->bindings;
5020
  struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5021
                                        struct c_arg_info);
5022
  tree parms    = 0;
5023
  tree tags     = 0;
5024
  tree types    = 0;
5025
  tree others   = 0;
5026
 
5027
  static bool explained_incomplete_types = false;
5028
  bool gave_void_only_once_err = false;
5029
 
5030
  arg_info->parms = 0;
5031
  arg_info->tags = 0;
5032
  arg_info->types = 0;
5033
  arg_info->others = 0;
5034
  arg_info->pending_sizes = 0;
5035
  arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5036
 
5037
  /* The bindings in this scope must not get put into a block.
5038
     We will take care of deleting the binding nodes.  */
5039
  current_scope->bindings = 0;
5040
 
5041
  /* This function is only called if there was *something* on the
5042
     parameter list.  */
5043
  gcc_assert (b);
5044
 
5045
  /* A parameter list consisting solely of 'void' indicates that the
5046
     function takes no arguments.  But if the 'void' is qualified
5047
     (by 'const' or 'volatile'), or has a storage class specifier
5048
     ('register'), then the behavior is undefined; issue an error.
5049
     Typedefs for 'void' are OK (see DR#157).  */
5050
  if (b->prev == 0                           /* one binding */
5051
      && TREE_CODE (b->decl) == PARM_DECL   /* which is a parameter */
5052
      && !DECL_NAME (b->decl)               /* anonymous */
5053
      && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5054
    {
5055
      if (TREE_THIS_VOLATILE (b->decl)
5056
          || TREE_READONLY (b->decl)
5057
          || C_DECL_REGISTER (b->decl))
5058
        error ("%<void%> as only parameter may not be qualified");
5059
 
5060
      /* There cannot be an ellipsis.  */
5061
      if (ellipsis)
5062
        error ("%<void%> must be the only parameter");
5063
 
5064
      arg_info->types = void_list_node;
5065
      return arg_info;
5066
    }
5067
 
5068
  if (!ellipsis)
5069
    types = void_list_node;
5070
 
5071
  /* Break up the bindings list into parms, tags, types, and others;
5072
     apply sanity checks; purge the name-to-decl bindings.  */
5073
  while (b)
5074
    {
5075
      tree decl = b->decl;
5076
      tree type = TREE_TYPE (decl);
5077
      const char *keyword;
5078
 
5079
      switch (TREE_CODE (decl))
5080
        {
5081
        case PARM_DECL:
5082
          if (b->id)
5083
            {
5084
              gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5085
              I_SYMBOL_BINDING (b->id) = b->shadowed;
5086
            }
5087
 
5088
          /* Check for forward decls that never got their actual decl.  */
5089
          if (TREE_ASM_WRITTEN (decl))
5090
            error ("parameter %q+D has just a forward declaration", decl);
5091
          /* Check for (..., void, ...) and issue an error.  */
5092
          else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5093
            {
5094
              if (!gave_void_only_once_err)
5095
                {
5096
                  error ("%<void%> must be the only parameter");
5097
                  gave_void_only_once_err = true;
5098
                }
5099
            }
5100
          else
5101
            {
5102
              /* Valid parameter, add it to the list.  */
5103
              TREE_CHAIN (decl) = parms;
5104
              parms = decl;
5105
 
5106
              /* Since there is a prototype, args are passed in their
5107
                 declared types.  The back end may override this later.  */
5108
              DECL_ARG_TYPE (decl) = type;
5109
              types = tree_cons (0, type, types);
5110
            }
5111
          break;
5112
 
5113
        case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5114
        case UNION_TYPE:    keyword = "union"; goto tag;
5115
        case RECORD_TYPE:   keyword = "struct"; goto tag;
5116
        tag:
5117
          /* Types may not have tag-names, in which case the type
5118
             appears in the bindings list with b->id NULL.  */
5119
          if (b->id)
5120
            {
5121
              gcc_assert (I_TAG_BINDING (b->id) == b);
5122
              I_TAG_BINDING (b->id) = b->shadowed;
5123
            }
5124
 
5125
          /* Warn about any struct, union or enum tags defined in a
5126
             parameter list.  The scope of such types is limited to
5127
             the parameter list, which is rarely if ever desirable
5128
             (it's impossible to call such a function with type-
5129
             correct arguments).  An anonymous union parm type is
5130
             meaningful as a GNU extension, so don't warn for that.  */
5131
          if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5132
            {
5133
              if (b->id)
5134
                /* The %s will be one of 'struct', 'union', or 'enum'.  */
5135
                warning (0, "%<%s %E%> declared inside parameter list",
5136
                         keyword, b->id);
5137
              else
5138
                /* The %s will be one of 'struct', 'union', or 'enum'.  */
5139
                warning (0, "anonymous %s declared inside parameter list",
5140
                         keyword);
5141
 
5142
              if (!explained_incomplete_types)
5143
                {
5144
                  warning (0, "its scope is only this definition or declaration,"
5145
                           " which is probably not what you want");
5146
                  explained_incomplete_types = true;
5147
                }
5148
            }
5149
 
5150
          tags = tree_cons (b->id, decl, tags);
5151
          break;
5152
 
5153
        case CONST_DECL:
5154
        case TYPE_DECL:
5155
        case FUNCTION_DECL:
5156
          /* CONST_DECLs appear here when we have an embedded enum,
5157
             and TYPE_DECLs appear here when we have an embedded struct
5158
             or union.  No warnings for this - we already warned about the
5159
             type itself.  FUNCTION_DECLs appear when there is an implicit
5160
             function declaration in the parameter list.  */
5161
 
5162
          TREE_CHAIN (decl) = others;
5163
          others = decl;
5164
          /* fall through */
5165
 
5166
        case ERROR_MARK:
5167
          /* error_mark_node appears here when we have an undeclared
5168
             variable.  Just throw it away.  */
5169
          if (b->id)
5170
            {
5171
              gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5172
              I_SYMBOL_BINDING (b->id) = b->shadowed;
5173
            }
5174
          break;
5175
 
5176
          /* Other things that might be encountered.  */
5177
        case LABEL_DECL:
5178
        case VAR_DECL:
5179
        default:
5180
          gcc_unreachable ();
5181
        }
5182
 
5183
      b = free_binding_and_advance (b);
5184
    }
5185
 
5186
  arg_info->parms = parms;
5187
  arg_info->tags = tags;
5188
  arg_info->types = types;
5189
  arg_info->others = others;
5190
  arg_info->pending_sizes = get_pending_sizes ();
5191
  return arg_info;
5192
}
5193
 
5194
/* Get the struct, enum or union (CODE says which) with tag NAME.
5195
   Define the tag as a forward-reference if it is not defined.
5196
   Return a c_typespec structure for the type specifier.  */
5197
 
5198
struct c_typespec
5199
parser_xref_tag (enum tree_code code, tree name)
5200
{
5201
  struct c_typespec ret;
5202
  /* If a cross reference is requested, look up the type
5203
     already defined for this tag and return it.  */
5204
 
5205
  tree ref = lookup_tag (code, name, 0);
5206
  /* If this is the right type of tag, return what we found.
5207
     (This reference will be shadowed by shadow_tag later if appropriate.)
5208
     If this is the wrong type of tag, do not return it.  If it was the
5209
     wrong type in the same scope, we will have had an error
5210
     message already; if in a different scope and declaring
5211
     a name, pending_xref_error will give an error message; but if in a
5212
     different scope and not declaring a name, this tag should
5213
     shadow the previous declaration of a different type of tag, and
5214
     this would not work properly if we return the reference found.
5215
     (For example, with "struct foo" in an outer scope, "union foo;"
5216
     must shadow that tag with a new one of union type.)  */
5217
  ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5218
  if (ref && TREE_CODE (ref) == code)
5219
    {
5220
      ret.spec = ref;
5221
      return ret;
5222
    }
5223
 
5224
  /* If no such tag is yet defined, create a forward-reference node
5225
     and record it as the "definition".
5226
     When a real declaration of this type is found,
5227
     the forward-reference will be altered into a real type.  */
5228
 
5229
  ref = make_node (code);
5230
  if (code == ENUMERAL_TYPE)
5231
    {
5232
      /* Give the type a default layout like unsigned int
5233
         to avoid crashing if it does not get defined.  */
5234
      TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5235
      TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5236
      TYPE_USER_ALIGN (ref) = 0;
5237
      TYPE_UNSIGNED (ref) = 1;
5238
      TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5239
      TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5240
      TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5241
    }
5242
 
5243
  pushtag (name, ref);
5244
 
5245
  ret.spec = ref;
5246
  return ret;
5247
}
5248
 
5249
/* Get the struct, enum or union (CODE says which) with tag NAME.
5250
   Define the tag as a forward-reference if it is not defined.
5251
   Return a tree for the type.  */
5252
 
5253
tree
5254
xref_tag (enum tree_code code, tree name)
5255
{
5256
  return parser_xref_tag (code, name).spec;
5257
}
5258
 
5259
/* Make sure that the tag NAME is defined *in the current scope*
5260
   at least as a forward reference.
5261
   CODE says which kind of tag NAME ought to be.  */
5262
 
5263
tree
5264
start_struct (enum tree_code code, tree name)
5265
{
5266
  /* If there is already a tag defined at this scope
5267
     (as a forward reference), just return it.  */
5268
 
5269
  tree ref = 0;
5270
 
5271
  if (name != 0)
5272
    ref = lookup_tag (code, name, 1);
5273
  if (ref && TREE_CODE (ref) == code)
5274
    {
5275
      if (TYPE_SIZE (ref))
5276
        {
5277
          if (code == UNION_TYPE)
5278
            error ("redefinition of %<union %E%>", name);
5279
          else
5280
            error ("redefinition of %<struct %E%>", name);
5281
        }
5282
      else if (C_TYPE_BEING_DEFINED (ref))
5283
        {
5284
          if (code == UNION_TYPE)
5285
            error ("nested redefinition of %<union %E%>", name);
5286
          else
5287
            error ("nested redefinition of %<struct %E%>", name);
5288
        }
5289
    }
5290
  else
5291
    {
5292
      /* Otherwise create a forward-reference just so the tag is in scope.  */
5293
 
5294
      ref = make_node (code);
5295
      pushtag (name, ref);
5296
    }
5297
 
5298
  C_TYPE_BEING_DEFINED (ref) = 1;
5299
  TYPE_PACKED (ref) = flag_pack_struct;
5300
  return ref;
5301
}
5302
 
5303
/* Process the specs, declarator and width (NULL if omitted)
5304
   of a structure component, returning a FIELD_DECL node.
5305
   WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5306
 
5307
   This is done during the parsing of the struct declaration.
5308
   The FIELD_DECL nodes are chained together and the lot of them
5309
   are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
5310
 
5311
tree
5312
grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5313
           tree width)
5314
{
5315
  tree value;
5316
 
5317
  if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5318
      && width == NULL_TREE)
5319
    {
5320
      /* This is an unnamed decl.
5321
 
5322
         If we have something of the form "union { list } ;" then this
5323
         is the anonymous union extension.  Similarly for struct.
5324
 
5325
         If this is something of the form "struct foo;", then
5326
           If MS extensions are enabled, this is handled as an
5327
             anonymous struct.
5328
           Otherwise this is a forward declaration of a structure tag.
5329
 
5330
         If this is something of the form "foo;" and foo is a TYPE_DECL, then
5331
           If MS extensions are enabled and foo names a structure, then
5332
             again this is an anonymous struct.
5333
           Otherwise this is an error.
5334
 
5335
         Oh what a horrid tangled web we weave.  I wonder if MS consciously
5336
         took this from Plan 9 or if it was an accident of implementation
5337
         that took root before someone noticed the bug...  */
5338
 
5339
      tree type = declspecs->type;
5340
      bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5341
                      || TREE_CODE (type) == UNION_TYPE);
5342
      bool ok = false;
5343
 
5344
      if (type_ok
5345
          && (flag_ms_extensions || !declspecs->typedef_p))
5346
        {
5347
          if (flag_ms_extensions)
5348
            ok = true;
5349
          else if (flag_iso)
5350
            ok = false;
5351
          else if (TYPE_NAME (type) == NULL)
5352
            ok = true;
5353
          else
5354
            ok = false;
5355
        }
5356
      if (!ok)
5357
        {
5358
          pedwarn ("declaration does not declare anything");
5359
          return NULL_TREE;
5360
        }
5361
      if (pedantic)
5362
        pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5363
    }
5364
 
5365
  value = grokdeclarator (declarator, declspecs, FIELD, false,
5366
                          width ? &width : NULL);
5367
 
5368
  finish_decl (value, NULL_TREE, NULL_TREE);
5369
  DECL_INITIAL (value) = width;
5370
 
5371
  return value;
5372
}
5373
 
5374
/* Generate an error for any duplicate field names in FIELDLIST.  Munge
5375
   the list such that this does not present a problem later.  */
5376
 
5377
static void
5378
detect_field_duplicates (tree fieldlist)
5379
{
5380
  tree x, y;
5381
  int timeout = 10;
5382
 
5383
  /* First, see if there are more than "a few" fields.
5384
     This is trivially true if there are zero or one fields.  */
5385
  if (!fieldlist)
5386
    return;
5387
  x = TREE_CHAIN (fieldlist);
5388
  if (!x)
5389
    return;
5390
  do {
5391
    timeout--;
5392
    x = TREE_CHAIN (x);
5393
  } while (timeout > 0 && x);
5394
 
5395
  /* If there were "few" fields, avoid the overhead of allocating
5396
     a hash table.  Instead just do the nested traversal thing.  */
5397
  if (timeout > 0)
5398
    {
5399
      for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5400
        if (DECL_NAME (x))
5401
          {
5402
            for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5403
              if (DECL_NAME (y) == DECL_NAME (x))
5404
                {
5405
                  error ("duplicate member %q+D", x);
5406
                  DECL_NAME (x) = NULL_TREE;
5407
                }
5408
          }
5409
    }
5410
  else
5411
    {
5412
      htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5413
      void **slot;
5414
 
5415
      for (x = fieldlist; x ; x = TREE_CHAIN (x))
5416
        if ((y = DECL_NAME (x)) != 0)
5417
          {
5418
            slot = htab_find_slot (htab, y, INSERT);
5419
            if (*slot)
5420
              {
5421
                error ("duplicate member %q+D", x);
5422
                DECL_NAME (x) = NULL_TREE;
5423
              }
5424
            *slot = y;
5425
          }
5426
 
5427
      htab_delete (htab);
5428
    }
5429
}
5430
 
5431
/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5432
   FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5433
   ATTRIBUTES are attributes to be applied to the structure.  */
5434
 
5435
tree
5436
finish_struct (tree t, tree fieldlist, tree attributes)
5437
{
5438
  tree x;
5439
  bool toplevel = file_scope == current_scope;
5440
  int saw_named_field;
5441
 
5442
  /* If this type was previously laid out as a forward reference,
5443
     make sure we lay it out again.  */
5444
 
5445
  TYPE_SIZE (t) = 0;
5446
 
5447
  decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5448
 
5449
  if (pedantic)
5450
    {
5451
      for (x = fieldlist; x; x = TREE_CHAIN (x))
5452
        if (DECL_NAME (x) != 0)
5453
          break;
5454
 
5455
      if (x == 0)
5456
        {
5457
          if (TREE_CODE (t) == UNION_TYPE)
5458
            {
5459
              if (fieldlist)
5460
                pedwarn ("union has no named members");
5461
              else
5462
                pedwarn ("union has no members");
5463
            }
5464
          else
5465
            {
5466
              if (fieldlist)
5467
                pedwarn ("struct has no named members");
5468
              else
5469
                pedwarn ("struct has no members");
5470
            }
5471
        }
5472
    }
5473
 
5474
  /* Install struct as DECL_CONTEXT of each field decl.
5475
     Also process specified field sizes, found in the DECL_INITIAL,
5476
     storing 0 there after the type has been changed to precision equal
5477
     to its width, rather than the precision of the specified standard
5478
     type.  (Correct layout requires the original type to have been preserved
5479
     until now.)  */
5480
 
5481
  saw_named_field = 0;
5482
  for (x = fieldlist; x; x = TREE_CHAIN (x))
5483
    {
5484
      if (TREE_TYPE (x) == error_mark_node)
5485
        continue;
5486
 
5487
      DECL_CONTEXT (x) = t;
5488
 
5489
      if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5490
        DECL_PACKED (x) = 1;
5491
 
5492
      /* If any field is const, the structure type is pseudo-const.  */
5493
      if (TREE_READONLY (x))
5494
        C_TYPE_FIELDS_READONLY (t) = 1;
5495
      else
5496
        {
5497
          /* A field that is pseudo-const makes the structure likewise.  */
5498
          tree t1 = TREE_TYPE (x);
5499
          while (TREE_CODE (t1) == ARRAY_TYPE)
5500
            t1 = TREE_TYPE (t1);
5501
          if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5502
              && C_TYPE_FIELDS_READONLY (t1))
5503
            C_TYPE_FIELDS_READONLY (t) = 1;
5504
        }
5505
 
5506
      /* Any field that is volatile means variables of this type must be
5507
         treated in some ways as volatile.  */
5508
      if (TREE_THIS_VOLATILE (x))
5509
        C_TYPE_FIELDS_VOLATILE (t) = 1;
5510
 
5511
      /* Any field of nominal variable size implies structure is too.  */
5512
      if (C_DECL_VARIABLE_SIZE (x))
5513
        C_TYPE_VARIABLE_SIZE (t) = 1;
5514
 
5515
      if (DECL_INITIAL (x))
5516
        {
5517
          unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5518
          DECL_SIZE (x) = bitsize_int (width);
5519
          DECL_BIT_FIELD (x) = 1;
5520
          SET_DECL_C_BIT_FIELD (x);
5521
        }
5522
 
5523
      /* Detect flexible array member in an invalid context.  */
5524
      if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5525
          && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5526
          && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5527
          && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5528
        {
5529
          if (TREE_CODE (t) == UNION_TYPE)
5530
            {
5531
              error ("%Jflexible array member in union", x);
5532
              TREE_TYPE (x) = error_mark_node;
5533
            }
5534
          else if (TREE_CHAIN (x) != NULL_TREE)
5535
            {
5536
              error ("%Jflexible array member not at end of struct", x);
5537
              TREE_TYPE (x) = error_mark_node;
5538
            }
5539
          else if (!saw_named_field)
5540
            {
5541
              error ("%Jflexible array member in otherwise empty struct", x);
5542
              TREE_TYPE (x) = error_mark_node;
5543
            }
5544
        }
5545
 
5546
      if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5547
          && flexible_array_type_p (TREE_TYPE (x)))
5548
        pedwarn ("%Jinvalid use of structure with flexible array member", x);
5549
 
5550
      if (DECL_NAME (x))
5551
        saw_named_field = 1;
5552
    }
5553
 
5554
  detect_field_duplicates (fieldlist);
5555
 
5556
  /* Now we have the nearly final fieldlist.  Record it,
5557
     then lay out the structure or union (including the fields).  */
5558
 
5559
  TYPE_FIELDS (t) = fieldlist;
5560
 
5561
  layout_type (t);
5562
 
5563
  /* Give bit-fields their proper types.  */
5564
  {
5565
    tree *fieldlistp = &fieldlist;
5566
    while (*fieldlistp)
5567
      if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5568
          && TREE_TYPE (*fieldlistp) != error_mark_node)
5569
        {
5570
          unsigned HOST_WIDE_INT width
5571
            = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5572
          tree type = TREE_TYPE (*fieldlistp);
5573
          if (width != TYPE_PRECISION (type))
5574
            {
5575
              TREE_TYPE (*fieldlistp)
5576
                = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5577
              DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5578
            }
5579
          DECL_INITIAL (*fieldlistp) = 0;
5580
        }
5581
      else
5582
        fieldlistp = &TREE_CHAIN (*fieldlistp);
5583
  }
5584
 
5585
  /* Now we have the truly final field list.
5586
     Store it in this type and in the variants.  */
5587
 
5588
  TYPE_FIELDS (t) = fieldlist;
5589
 
5590
  /* If there are lots of fields, sort so we can look through them fast.
5591
     We arbitrarily consider 16 or more elts to be "a lot".  */
5592
 
5593
  {
5594
    int len = 0;
5595
 
5596
    for (x = fieldlist; x; x = TREE_CHAIN (x))
5597
      {
5598
        if (len > 15 || DECL_NAME (x) == NULL)
5599
          break;
5600
        len += 1;
5601
      }
5602
 
5603
    if (len > 15)
5604
      {
5605
        tree *field_array;
5606
        struct lang_type *space;
5607
        struct sorted_fields_type *space2;
5608
 
5609
        len += list_length (x);
5610
 
5611
        /* Use the same allocation policy here that make_node uses, to
5612
          ensure that this lives as long as the rest of the struct decl.
5613
          All decls in an inline function need to be saved.  */
5614
 
5615
        space = GGC_CNEW (struct lang_type);
5616
        space2 = GGC_NEWVAR (struct sorted_fields_type,
5617
                             sizeof (struct sorted_fields_type) + len * sizeof (tree));
5618
 
5619
        len = 0;
5620
        space->s = space2;
5621
        field_array = &space2->elts[0];
5622
        for (x = fieldlist; x; x = TREE_CHAIN (x))
5623
          {
5624
            field_array[len++] = x;
5625
 
5626
            /* If there is anonymous struct or union, break out of the loop.  */
5627
            if (DECL_NAME (x) == NULL)
5628
              break;
5629
          }
5630
        /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
5631
        if (x == NULL)
5632
          {
5633
            TYPE_LANG_SPECIFIC (t) = space;
5634
            TYPE_LANG_SPECIFIC (t)->s->len = len;
5635
            field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5636
            qsort (field_array, len, sizeof (tree), field_decl_cmp);
5637
          }
5638
      }
5639
  }
5640
 
5641
  for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5642
    {
5643
      TYPE_FIELDS (x) = TYPE_FIELDS (t);
5644
      TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5645
      C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5646
      C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5647
      C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5648
    }
5649
 
5650
  /* If this was supposed to be a transparent union, but we can't
5651
     make it one, warn and turn off the flag.  */
5652
  if (TREE_CODE (t) == UNION_TYPE
5653
      && TYPE_TRANSPARENT_UNION (t)
5654
      && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5655
    {
5656
      TYPE_TRANSPARENT_UNION (t) = 0;
5657
      warning (0, "union cannot be made transparent");
5658
    }
5659
 
5660
  /* If this structure or union completes the type of any previous
5661
     variable declaration, lay it out and output its rtl.  */
5662
  for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5663
       x;
5664
       x = TREE_CHAIN (x))
5665
    {
5666
      tree decl = TREE_VALUE (x);
5667
      if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5668
        layout_array_type (TREE_TYPE (decl));
5669
      if (TREE_CODE (decl) != TYPE_DECL)
5670
        {
5671
          layout_decl (decl, 0);
5672
          if (c_dialect_objc ())
5673
            objc_check_decl (decl);
5674
          rest_of_decl_compilation (decl, toplevel, 0);
5675
          if (!toplevel)
5676
            expand_decl (decl);
5677
        }
5678
    }
5679
  C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5680
 
5681
  /* Finish debugging output for this type.  */
5682
  rest_of_type_compilation (t, toplevel);
5683
 
5684
  /* If we're inside a function proper, i.e. not file-scope and not still
5685
     parsing parameters, then arrange for the size of a variable sized type
5686
     to be bound now.  */
5687
  if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5688
    add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5689
 
5690
  return t;
5691
}
5692
 
5693
/* Lay out the type T, and its element type, and so on.  */
5694
 
5695
static void
5696
layout_array_type (tree t)
5697
{
5698
  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5699
    layout_array_type (TREE_TYPE (t));
5700
  layout_type (t);
5701
}
5702
 
5703
/* Begin compiling the definition of an enumeration type.
5704
   NAME is its name (or null if anonymous).
5705
   Returns the type object, as yet incomplete.
5706
   Also records info about it so that build_enumerator
5707
   may be used to declare the individual values as they are read.  */
5708
 
5709
tree
5710
start_enum (tree name)
5711
{
5712
  tree enumtype = 0;
5713
 
5714
  /* If this is the real definition for a previous forward reference,
5715
     fill in the contents in the same object that used to be the
5716
     forward reference.  */
5717
 
5718
  if (name != 0)
5719
    enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5720
 
5721
  if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5722
    {
5723
      enumtype = make_node (ENUMERAL_TYPE);
5724
      pushtag (name, enumtype);
5725
    }
5726
 
5727
  if (C_TYPE_BEING_DEFINED (enumtype))
5728
    error ("nested redefinition of %<enum %E%>", name);
5729
 
5730
  C_TYPE_BEING_DEFINED (enumtype) = 1;
5731
 
5732
  if (TYPE_VALUES (enumtype) != 0)
5733
    {
5734
      /* This enum is a named one that has been declared already.  */
5735
      error ("redeclaration of %<enum %E%>", name);
5736
 
5737
      /* Completely replace its old definition.
5738
         The old enumerators remain defined, however.  */
5739
      TYPE_VALUES (enumtype) = 0;
5740
    }
5741
 
5742
  enum_next_value = integer_zero_node;
5743
  enum_overflow = 0;
5744
 
5745
  if (flag_short_enums)
5746
    TYPE_PACKED (enumtype) = 1;
5747
 
5748
  return enumtype;
5749
}
5750
 
5751
/* After processing and defining all the values of an enumeration type,
5752
   install their decls in the enumeration type and finish it off.
5753
   ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5754
   and ATTRIBUTES are the specified attributes.
5755
   Returns ENUMTYPE.  */
5756
 
5757
tree
5758
finish_enum (tree enumtype, tree values, tree attributes)
5759
{
5760
  tree pair, tem;
5761
  tree minnode = 0, maxnode = 0;
5762
  int precision, unsign;
5763
  bool toplevel = (file_scope == current_scope);
5764
  struct lang_type *lt;
5765
 
5766
  decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5767
 
5768
  /* Calculate the maximum value of any enumerator in this type.  */
5769
 
5770
  if (values == error_mark_node)
5771
    minnode = maxnode = integer_zero_node;
5772
  else
5773
    {
5774
      minnode = maxnode = TREE_VALUE (values);
5775
      for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5776
        {
5777
          tree value = TREE_VALUE (pair);
5778
          if (tree_int_cst_lt (maxnode, value))
5779
            maxnode = value;
5780
          if (tree_int_cst_lt (value, minnode))
5781
            minnode = value;
5782
        }
5783
    }
5784
 
5785
  /* Construct the final type of this enumeration.  It is the same
5786
     as one of the integral types - the narrowest one that fits, except
5787
     that normally we only go as narrow as int - and signed iff any of
5788
     the values are negative.  */
5789
  unsign = (tree_int_cst_sgn (minnode) >= 0);
5790
  precision = MAX (min_precision (minnode, unsign),
5791
                   min_precision (maxnode, unsign));
5792
 
5793
  if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5794
    {
5795
      tem = c_common_type_for_size (precision, unsign);
5796
      if (tem == NULL)
5797
        {
5798
          warning (0, "enumeration values exceed range of largest integer");
5799
          tem = long_long_integer_type_node;
5800
        }
5801
    }
5802
  else
5803
    tem = unsign ? unsigned_type_node : integer_type_node;
5804
 
5805
  TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5806
  TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5807
  TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5808
  TYPE_SIZE (enumtype) = 0;
5809
 
5810
  /* If the precision of the type was specific with an attribute and it
5811
     was too small, give an error.  Otherwise, use it.  */
5812
  if (TYPE_PRECISION (enumtype))
5813
    {
5814
      if (precision > TYPE_PRECISION (enumtype))
5815
        error ("specified mode too small for enumeral values");
5816
    }
5817
  else
5818
    TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5819
 
5820
  layout_type (enumtype);
5821
 
5822
  if (values != error_mark_node)
5823
    {
5824
      /* Change the type of the enumerators to be the enum type.  We
5825
         need to do this irrespective of the size of the enum, for
5826
         proper type checking.  Replace the DECL_INITIALs of the
5827
         enumerators, and the value slots of the list, with copies
5828
         that have the enum type; they cannot be modified in place
5829
         because they may be shared (e.g.  integer_zero_node) Finally,
5830
         change the purpose slots to point to the names of the decls.  */
5831
      for (pair = values; pair; pair = TREE_CHAIN (pair))
5832
        {
5833
          tree enu = TREE_PURPOSE (pair);
5834
          tree ini = DECL_INITIAL (enu);
5835
 
5836
          TREE_TYPE (enu) = enumtype;
5837
 
5838
          /* The ISO C Standard mandates enumerators to have type int,
5839
             even though the underlying type of an enum type is
5840
             unspecified.  Here we convert any enumerators that fit in
5841
             an int to type int, to avoid promotions to unsigned types
5842
             when comparing integers with enumerators that fit in the
5843
             int range.  When -pedantic is given, build_enumerator()
5844
             would have already taken care of those that don't fit.  */
5845
          if (int_fits_type_p (ini, integer_type_node))
5846
            tem = integer_type_node;
5847
          else
5848
            tem = enumtype;
5849
          ini = convert (tem, ini);
5850
 
5851
          DECL_INITIAL (enu) = ini;
5852
          TREE_PURPOSE (pair) = DECL_NAME (enu);
5853
          TREE_VALUE (pair) = ini;
5854
        }
5855
 
5856
      TYPE_VALUES (enumtype) = values;
5857
    }
5858
 
5859
  /* Record the min/max values so that we can warn about bit-field
5860
     enumerations that are too small for the values.  */
5861
  lt = GGC_CNEW (struct lang_type);
5862
  lt->enum_min = minnode;
5863
  lt->enum_max = maxnode;
5864
  TYPE_LANG_SPECIFIC (enumtype) = lt;
5865
 
5866
  /* Fix up all variant types of this enum type.  */
5867
  for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5868
    {
5869
      if (tem == enumtype)
5870
        continue;
5871
      TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5872
      TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5873
      TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5874
      TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5875
      TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5876
      TYPE_MODE (tem) = TYPE_MODE (enumtype);
5877
      TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5878
      TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5879
      TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5880
      TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5881
      TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5882
    }
5883
 
5884
  /* Finish debugging output for this type.  */
5885
  rest_of_type_compilation (enumtype, toplevel);
5886
 
5887
  return enumtype;
5888
}
5889
 
5890
/* Build and install a CONST_DECL for one value of the
5891
   current enumeration type (one that was begun with start_enum).
5892
   Return a tree-list containing the CONST_DECL and its value.
5893
   Assignment of sequential values by default is handled here.  */
5894
 
5895
tree
5896
build_enumerator (tree name, tree value)
5897
{
5898
  tree decl, type;
5899
 
5900
  /* Validate and default VALUE.  */
5901
 
5902
  if (value != 0)
5903
    {
5904
      /* Don't issue more errors for error_mark_node (i.e. an
5905
         undeclared identifier) - just ignore the value expression.  */
5906
      if (value == error_mark_node)
5907
        value = 0;
5908
      else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5909
               || TREE_CODE (value) != INTEGER_CST)
5910
        {
5911
          error ("enumerator value for %qE is not an integer constant", name);
5912
          value = 0;
5913
        }
5914
      else
5915
        {
5916
          value = default_conversion (value);
5917
          constant_expression_warning (value);
5918
        }
5919
    }
5920
 
5921
  /* Default based on previous value.  */
5922
  /* It should no longer be possible to have NON_LVALUE_EXPR
5923
     in the default.  */
5924
  if (value == 0)
5925
    {
5926
      value = enum_next_value;
5927
      if (enum_overflow)
5928
        error ("overflow in enumeration values");
5929
    }
5930
 
5931
  if (pedantic && !int_fits_type_p (value, integer_type_node))
5932
    {
5933
      pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5934
      /* XXX This causes -pedantic to change the meaning of the program.
5935
         Remove?  -zw 2004-03-15  */
5936
      value = convert (integer_type_node, value);
5937
    }
5938
 
5939
  /* Set basis for default for next value.  */
5940
  enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5941
  enum_overflow = tree_int_cst_lt (enum_next_value, value);
5942
 
5943
  /* Now create a declaration for the enum value name.  */
5944
 
5945
  type = TREE_TYPE (value);
5946
  type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5947
                                      TYPE_PRECISION (integer_type_node)),
5948
                                 (TYPE_PRECISION (type)
5949
                                  >= TYPE_PRECISION (integer_type_node)
5950
                                  && TYPE_UNSIGNED (type)));
5951
 
5952
  decl = build_decl (CONST_DECL, name, type);
5953
  DECL_INITIAL (decl) = convert (type, value);
5954
  pushdecl (decl);
5955
 
5956
  return tree_cons (decl, value, NULL_TREE);
5957
}
5958
 
5959
 
5960
/* Create the FUNCTION_DECL for a function definition.
5961
   DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5962
   the declaration; they describe the function's name and the type it returns,
5963
   but twisted together in a fashion that parallels the syntax of C.
5964
 
5965
   This function creates a binding context for the function body
5966
   as well as setting up the FUNCTION_DECL in current_function_decl.
5967
 
5968
   Returns 1 on success.  If the DECLARATOR is not suitable for a function
5969
   (it defines a datum instead), we return 0, which tells
5970
   yyparse to report a parse error.  */
5971
 
5972
int
5973
start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5974
                tree attributes)
5975
{
5976
  tree decl1, old_decl;
5977
  tree restype, resdecl;
5978
  struct c_label_context_se *nstack_se;
5979
  struct c_label_context_vm *nstack_vm;
5980
 
5981
  current_function_returns_value = 0;  /* Assume, until we see it does.  */
5982
  current_function_returns_null = 0;
5983
  current_function_returns_abnormally = 0;
5984
  warn_about_return_type = 0;
5985
  current_extern_inline = 0;
5986
  c_switch_stack = NULL;
5987
 
5988
  nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5989
  nstack_se->labels_def = NULL;
5990
  nstack_se->labels_used = NULL;
5991
  nstack_se->next = label_context_stack_se;
5992
  label_context_stack_se = nstack_se;
5993
 
5994
  nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5995
  nstack_vm->labels_def = NULL;
5996
  nstack_vm->labels_used = NULL;
5997
  nstack_vm->scope = 0;
5998
  nstack_vm->next = label_context_stack_vm;
5999
  label_context_stack_vm = nstack_vm;
6000
 
6001
  /* Indicate no valid break/continue context by setting these variables
6002
     to some non-null, non-label value.  We'll notice and emit the proper
6003
     error message in c_finish_bc_stmt.  */
6004
  c_break_label = c_cont_label = size_zero_node;
6005
 
6006
  decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
6007
 
6008
  /* If the declarator is not suitable for a function definition,
6009
     cause a syntax error.  */
6010
  if (decl1 == 0)
6011
    {
6012
      label_context_stack_se = label_context_stack_se->next;
6013
      label_context_stack_vm = label_context_stack_vm->next;
6014
      return 0;
6015
    }
6016
 
6017
  decl_attributes (&decl1, attributes, 0);
6018
 
6019
  if (DECL_DECLARED_INLINE_P (decl1)
6020
      && DECL_UNINLINABLE (decl1)
6021
      && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6022
    warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6023
             decl1);
6024
 
6025
  announce_function (decl1);
6026
 
6027
  if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6028
    {
6029
      error ("return type is an incomplete type");
6030
      /* Make it return void instead.  */
6031
      TREE_TYPE (decl1)
6032
        = build_function_type (void_type_node,
6033
                               TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6034
    }
6035
 
6036
  if (warn_about_return_type)
6037
    pedwarn_c99 ("return type defaults to %<int%>");
6038
 
6039
  /* Make the init_value nonzero so pushdecl knows this is not tentative.
6040
     error_mark_node is replaced below (in pop_scope) with the BLOCK.  */
6041
  DECL_INITIAL (decl1) = error_mark_node;
6042
 
6043
  /* If this definition isn't a prototype and we had a prototype declaration
6044
     before, copy the arg type info from that prototype.  */
6045
  old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6046
  if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6047
    old_decl = 0;
6048
  current_function_prototype_locus = UNKNOWN_LOCATION;
6049
  current_function_prototype_built_in = false;
6050
  current_function_prototype_arg_types = NULL_TREE;
6051
  if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6052
    {
6053
      if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6054
          && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6055
                        TREE_TYPE (TREE_TYPE (old_decl))))
6056
        {
6057
          TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6058
                                              TREE_TYPE (decl1));
6059
          current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6060
          current_function_prototype_built_in
6061
            = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6062
          current_function_prototype_arg_types
6063
            = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6064
        }
6065
      if (TREE_PUBLIC (decl1))
6066
        {
6067
          /* If there is an external prototype declaration of this
6068
             function, record its location but do not copy information
6069
             to this decl.  This may be an invisible declaration
6070
             (built-in or in a scope which has finished) or simply
6071
             have more refined argument types than any declaration
6072
             found above.  */
6073
          struct c_binding *b;
6074
          for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6075
            if (B_IN_SCOPE (b, external_scope))
6076
              break;
6077
          if (b)
6078
            {
6079
              tree ext_decl, ext_type;
6080
              ext_decl = b->decl;
6081
              ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6082
              if (TREE_CODE (ext_type) == FUNCTION_TYPE
6083
                  && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6084
                                TREE_TYPE (ext_type)))
6085
                {
6086
                  current_function_prototype_locus
6087
                    = DECL_SOURCE_LOCATION (ext_decl);
6088
                  current_function_prototype_built_in
6089
                    = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6090
                  current_function_prototype_arg_types
6091
                    = TYPE_ARG_TYPES (ext_type);
6092
                }
6093
            }
6094
        }
6095
    }
6096
 
6097
  /* Optionally warn of old-fashioned def with no previous prototype.  */
6098
  if (warn_strict_prototypes
6099
      && old_decl != error_mark_node
6100
      && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6101
      && C_DECL_ISNT_PROTOTYPE (old_decl))
6102
    warning (OPT_Wstrict_prototypes,
6103
             "function declaration isn%'t a prototype");
6104
  /* Optionally warn of any global def with no previous prototype.  */
6105
  else if (warn_missing_prototypes
6106
           && old_decl != error_mark_node
6107
           && TREE_PUBLIC (decl1)
6108
           && !MAIN_NAME_P (DECL_NAME (decl1))
6109
           && C_DECL_ISNT_PROTOTYPE (old_decl))
6110
    warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6111
  /* Optionally warn of any def with no previous prototype
6112
     if the function has already been used.  */
6113
  else if (warn_missing_prototypes
6114
           && old_decl != 0
6115
           && old_decl != error_mark_node
6116
           && TREE_USED (old_decl)
6117
           && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6118
    warning (OPT_Wmissing_prototypes,
6119
             "%q+D was used with no prototype before its definition", decl1);
6120
  /* Optionally warn of any global def with no previous declaration.  */
6121
  else if (warn_missing_declarations
6122
           && TREE_PUBLIC (decl1)
6123
           && old_decl == 0
6124
           && !MAIN_NAME_P (DECL_NAME (decl1)))
6125
    warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6126
             decl1);
6127
  /* Optionally warn of any def with no previous declaration
6128
     if the function has already been used.  */
6129
  else if (warn_missing_declarations
6130
           && old_decl != 0
6131
           && old_decl != error_mark_node
6132
           && TREE_USED (old_decl)
6133
           && C_DECL_IMPLICIT (old_decl))
6134
    warning (OPT_Wmissing_declarations,
6135
             "%q+D was used with no declaration before its definition", decl1);
6136
 
6137
  /* This is a definition, not a reference.
6138
     So normally clear DECL_EXTERNAL.
6139
     However, `extern inline' acts like a declaration
6140
     except for defining how to inline.  So set DECL_EXTERNAL in that case.  */
6141
  DECL_EXTERNAL (decl1) = current_extern_inline;
6142
 
6143
  /* C99 specified different behaviour for non-static inline
6144
     functions, compared with the traditional GNU behaviour.  We don't
6145
     support the C99 behaviour, but we do warn about non-static inline
6146
     functions here.  The warning can be disabled via an explicit use
6147
     of -fgnu89-inline, or by using the gnu_inline attribute.  */
6148
  if (DECL_DECLARED_INLINE_P (decl1)
6149
      && TREE_PUBLIC (decl1)
6150
      && flag_isoc99
6151
      && flag_gnu89_inline != 1
6152
      && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6153
      && diagnostic_report_warnings_p ())
6154
    {
6155
      static bool info = false;
6156
 
6157
      warning (0, "C99 inline functions are not supported; using GNU89");
6158
      if (!info)
6159
        {
6160
          warning (0,
6161
                   "to disable this warning use -fgnu89-inline or "
6162
                   "the gnu_inline function attribute");
6163
          info = true;
6164
        }
6165
    }
6166
 
6167
  /* This function exists in static storage.
6168
     (This does not mean `static' in the C sense!)  */
6169
  TREE_STATIC (decl1) = 1;
6170
 
6171
  /* A nested function is not global.  */
6172
  if (current_function_decl != 0)
6173
    TREE_PUBLIC (decl1) = 0;
6174
 
6175
  /* This is the earliest point at which we might know the assembler
6176
     name of the function.  Thus, if it's set before this, die horribly.  */
6177
  gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6178
 
6179
  /* If #pragma weak was used, mark the decl weak now.  */
6180
  if (current_scope == file_scope)
6181
    maybe_apply_pragma_weak (decl1);
6182
 
6183
  /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
6184
  if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6185
    {
6186
      tree args;
6187
      int argct = 0;
6188
 
6189
      if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6190
          != integer_type_node)
6191
        pedwarn ("return type of %q+D is not %<int%>", decl1);
6192
 
6193
      for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6194
           args = TREE_CHAIN (args))
6195
        {
6196
          tree type = args ? TREE_VALUE (args) : 0;
6197
 
6198
          if (type == void_type_node)
6199
            break;
6200
 
6201
          ++argct;
6202
          switch (argct)
6203
            {
6204
            case 1:
6205
              if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6206
                pedwarn ("first argument of %q+D should be %<int%>", decl1);
6207
              break;
6208
 
6209
            case 2:
6210
              if (TREE_CODE (type) != POINTER_TYPE
6211
                  || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6212
                  || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6213
                      != char_type_node))
6214
                pedwarn ("second argument of %q+D should be %<char **%>",
6215
                         decl1);
6216
              break;
6217
 
6218
            case 3:
6219
              if (TREE_CODE (type) != POINTER_TYPE
6220
                  || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6221
                  || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6222
                      != char_type_node))
6223
                pedwarn ("third argument of %q+D should probably be "
6224
                         "%<char **%>", decl1);
6225
              break;
6226
            }
6227
        }
6228
 
6229
      /* It is intentional that this message does not mention the third
6230
         argument because it's only mentioned in an appendix of the
6231
         standard.  */
6232
      if (argct > 0 && (argct < 2 || argct > 3))
6233
        pedwarn ("%q+D takes only zero or two arguments", decl1);
6234
 
6235
      if (!TREE_PUBLIC (decl1))
6236
        pedwarn ("%q+D is normally a non-static function", decl1);
6237
    }
6238
 
6239
  /* Record the decl so that the function name is defined.
6240
     If we already have a decl for this name, and it is a FUNCTION_DECL,
6241
     use the old decl.  */
6242
 
6243
  current_function_decl = pushdecl (decl1);
6244
 
6245
  push_scope ();
6246
  declare_parm_level ();
6247
 
6248
  restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6249
  /* Promote the value to int before returning it.  */
6250
  if (c_promoting_integer_type_p (restype))
6251
    {
6252
      /* It retains unsignedness if not really getting wider.  */
6253
      if (TYPE_UNSIGNED (restype)
6254
          && (TYPE_PRECISION (restype)
6255
                  == TYPE_PRECISION (integer_type_node)))
6256
        restype = unsigned_type_node;
6257
      else
6258
        restype = integer_type_node;
6259
    }
6260
 
6261
  resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6262
  DECL_ARTIFICIAL (resdecl) = 1;
6263
  DECL_IGNORED_P (resdecl) = 1;
6264
  DECL_RESULT (current_function_decl) = resdecl;
6265
 
6266
  start_fname_decls ();
6267
 
6268
  return 1;
6269
}
6270
 
6271
/* Subroutine of store_parm_decls which handles new-style function
6272
   definitions (prototype format). The parms already have decls, so we
6273
   need only record them as in effect and complain if any redundant
6274
   old-style parm decls were written.  */
6275
static void
6276
store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6277
{
6278
  tree decl;
6279
 
6280
  if (current_scope->bindings)
6281
    {
6282
      error ("%Jold-style parameter declarations in prototyped "
6283
             "function definition", fndecl);
6284
 
6285
      /* Get rid of the old-style declarations.  */
6286
      pop_scope ();
6287
      push_scope ();
6288
    }
6289
  /* Don't issue this warning for nested functions, and don't issue this
6290
     warning if we got here because ARG_INFO_TYPES was error_mark_node
6291
     (this happens when a function definition has just an ellipsis in
6292
     its parameter list).  */
6293
  else if (!in_system_header && !current_function_scope
6294
           && arg_info->types != error_mark_node)
6295
    warning (OPT_Wtraditional,
6296
             "%Jtraditional C rejects ISO C style function definitions",
6297
             fndecl);
6298
 
6299
  /* Now make all the parameter declarations visible in the function body.
6300
     We can bypass most of the grunt work of pushdecl.  */
6301
  for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6302
    {
6303
      DECL_CONTEXT (decl) = current_function_decl;
6304
      if (DECL_NAME (decl))
6305
        {
6306
          bind (DECL_NAME (decl), decl, current_scope,
6307
                /*invisible=*/false, /*nested=*/false);
6308
          if (!TREE_USED (decl))
6309
            warn_if_shadowing (decl);
6310
        }
6311
      else
6312
        error ("%Jparameter name omitted", decl);
6313
    }
6314
 
6315
  /* Record the parameter list in the function declaration.  */
6316
  DECL_ARGUMENTS (fndecl) = arg_info->parms;
6317
 
6318
  /* Now make all the ancillary declarations visible, likewise.  */
6319
  for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6320
    {
6321
      DECL_CONTEXT (decl) = current_function_decl;
6322
      if (DECL_NAME (decl))
6323
        bind (DECL_NAME (decl), decl, current_scope,
6324
              /*invisible=*/false, /*nested=*/false);
6325
    }
6326
 
6327
  /* And all the tag declarations.  */
6328
  for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6329
    if (TREE_PURPOSE (decl))
6330
      bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6331
            /*invisible=*/false, /*nested=*/false);
6332
}
6333
 
6334
/* Subroutine of store_parm_decls which handles old-style function
6335
   definitions (separate parameter list and declarations).  */
6336
 
6337
static void
6338
store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6339
{
6340
  struct c_binding *b;
6341
  tree parm, decl, last;
6342
  tree parmids = arg_info->parms;
6343
  struct pointer_set_t *seen_args = pointer_set_create ();
6344
 
6345
  if (!in_system_header)
6346
    warning (OPT_Wold_style_definition, "%Jold-style function definition",
6347
             fndecl);
6348
 
6349
  /* Match each formal parameter name with its declaration.  Save each
6350
     decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
6351
  for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6352
    {
6353
      if (TREE_VALUE (parm) == 0)
6354
        {
6355
          error ("%Jparameter name missing from parameter list", fndecl);
6356
          TREE_PURPOSE (parm) = 0;
6357
          continue;
6358
        }
6359
 
6360
      b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6361
      if (b && B_IN_CURRENT_SCOPE (b))
6362
        {
6363
          decl = b->decl;
6364
          /* If we got something other than a PARM_DECL it is an error.  */
6365
          if (TREE_CODE (decl) != PARM_DECL)
6366
            error ("%q+D declared as a non-parameter", decl);
6367
          /* If the declaration is already marked, we have a duplicate
6368
             name.  Complain and ignore the duplicate.  */
6369
          else if (pointer_set_contains (seen_args, decl))
6370
            {
6371
              error ("multiple parameters named %q+D", decl);
6372
              TREE_PURPOSE (parm) = 0;
6373
              continue;
6374
            }
6375
          /* If the declaration says "void", complain and turn it into
6376
             an int.  */
6377
          else if (VOID_TYPE_P (TREE_TYPE (decl)))
6378
            {
6379
              error ("parameter %q+D declared with void type", decl);
6380
              TREE_TYPE (decl) = integer_type_node;
6381
              DECL_ARG_TYPE (decl) = integer_type_node;
6382
              layout_decl (decl, 0);
6383
            }
6384
          warn_if_shadowing (decl);
6385
        }
6386
      /* If no declaration found, default to int.  */
6387
      else
6388
        {
6389
          decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6390
          DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6391
          DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6392
          pushdecl (decl);
6393
          warn_if_shadowing (decl);
6394
 
6395
          if (flag_isoc99)
6396
            pedwarn ("type of %q+D defaults to %<int%>", decl);
6397
          else if (extra_warnings)
6398
            warning (OPT_Wextra, "type of %q+D defaults to %<int%>", decl);
6399
        }
6400
 
6401
      TREE_PURPOSE (parm) = decl;
6402
      pointer_set_insert (seen_args, decl);
6403
    }
6404
 
6405
  /* Now examine the parms chain for incomplete declarations
6406
     and declarations with no corresponding names.  */
6407
 
6408
  for (b = current_scope->bindings; b; b = b->prev)
6409
    {
6410
      parm = b->decl;
6411
      if (TREE_CODE (parm) != PARM_DECL)
6412
        continue;
6413
 
6414
      if (TREE_TYPE (parm) != error_mark_node
6415
          && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6416
        {
6417
          error ("parameter %q+D has incomplete type", parm);
6418
          TREE_TYPE (parm) = error_mark_node;
6419
        }
6420
 
6421
      if (!pointer_set_contains (seen_args, parm))
6422
        {
6423
          error ("declaration for parameter %q+D but no such parameter", parm);
6424
 
6425
          /* Pretend the parameter was not missing.
6426
             This gets us to a standard state and minimizes
6427
             further error messages.  */
6428
          parmids = chainon (parmids, tree_cons (parm, 0, 0));
6429
        }
6430
    }
6431
 
6432
  /* Chain the declarations together in the order of the list of
6433
     names.  Store that chain in the function decl, replacing the
6434
     list of names.  Update the current scope to match.  */
6435
  DECL_ARGUMENTS (fndecl) = 0;
6436
 
6437
  for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6438
    if (TREE_PURPOSE (parm))
6439
      break;
6440
  if (parm && TREE_PURPOSE (parm))
6441
    {
6442
      last = TREE_PURPOSE (parm);
6443
      DECL_ARGUMENTS (fndecl) = last;
6444
 
6445
      for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6446
        if (TREE_PURPOSE (parm))
6447
          {
6448
            TREE_CHAIN (last) = TREE_PURPOSE (parm);
6449
            last = TREE_PURPOSE (parm);
6450
          }
6451
      TREE_CHAIN (last) = 0;
6452
    }
6453
 
6454
  pointer_set_destroy (seen_args);
6455
 
6456
  /* If there was a previous prototype,
6457
     set the DECL_ARG_TYPE of each argument according to
6458
     the type previously specified, and report any mismatches.  */
6459
 
6460
  if (current_function_prototype_arg_types)
6461
    {
6462
      tree type;
6463
      for (parm = DECL_ARGUMENTS (fndecl),
6464
             type = current_function_prototype_arg_types;
6465
           parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6466
                             != void_type_node));
6467
           parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6468
        {
6469
          if (parm == 0 || type == 0
6470
              || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6471
            {
6472
              if (current_function_prototype_built_in)
6473
                warning (0, "number of arguments doesn%'t match "
6474
                         "built-in prototype");
6475
              else
6476
                {
6477
                  error ("number of arguments doesn%'t match prototype");
6478
                  error ("%Hprototype declaration",
6479
                         &current_function_prototype_locus);
6480
                }
6481
              break;
6482
            }
6483
          /* Type for passing arg must be consistent with that
6484
             declared for the arg.  ISO C says we take the unqualified
6485
             type for parameters declared with qualified type.  */
6486
          if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6487
                          TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6488
            {
6489
              if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6490
                  == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6491
                {
6492
                  /* Adjust argument to match prototype.  E.g. a previous
6493
                     `int foo(float);' prototype causes
6494
                     `int foo(x) float x; {...}' to be treated like
6495
                     `int foo(float x) {...}'.  This is particularly
6496
                     useful for argument types like uid_t.  */
6497
                  DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6498
 
6499
                  if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6500
                      && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6501
                      && TYPE_PRECISION (TREE_TYPE (parm))
6502
                      < TYPE_PRECISION (integer_type_node))
6503
                    DECL_ARG_TYPE (parm) = integer_type_node;
6504
 
6505
                  if (pedantic)
6506
                    {
6507
                      /* ??? Is it possible to get here with a
6508
                         built-in prototype or will it always have
6509
                         been diagnosed as conflicting with an
6510
                         old-style definition and discarded?  */
6511
                      if (current_function_prototype_built_in)
6512
                        warning (0, "promoted argument %qD "
6513
                                 "doesn%'t match built-in prototype", parm);
6514
                      else
6515
                        {
6516
                          pedwarn ("promoted argument %qD "
6517
                                   "doesn%'t match prototype", parm);
6518
                          pedwarn ("%Hprototype declaration",
6519
                                   &current_function_prototype_locus);
6520
                        }
6521
                    }
6522
                }
6523
              else
6524
                {
6525
                  if (current_function_prototype_built_in)
6526
                    warning (0, "argument %qD doesn%'t match "
6527
                             "built-in prototype", parm);
6528
                  else
6529
                    {
6530
                      error ("argument %qD doesn%'t match prototype", parm);
6531
                      error ("%Hprototype declaration",
6532
                             &current_function_prototype_locus);
6533
                    }
6534
                }
6535
            }
6536
        }
6537
      TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6538
    }
6539
 
6540
  /* Otherwise, create a prototype that would match.  */
6541
 
6542
  else
6543
    {
6544
      tree actual = 0, last = 0, type;
6545
 
6546
      for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6547
        {
6548
          type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6549
          if (last)
6550
            TREE_CHAIN (last) = type;
6551
          else
6552
            actual = type;
6553
          last = type;
6554
        }
6555
      type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6556
      if (last)
6557
        TREE_CHAIN (last) = type;
6558
      else
6559
        actual = type;
6560
 
6561
      /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6562
         of the type of this function, but we need to avoid having this
6563
         affect the types of other similarly-typed functions, so we must
6564
         first force the generation of an identical (but separate) type
6565
         node for the relevant function type.  The new node we create
6566
         will be a variant of the main variant of the original function
6567
         type.  */
6568
 
6569
      TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6570
 
6571
      TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6572
    }
6573
}
6574
 
6575
/* Store parameter declarations passed in ARG_INFO into the current
6576
   function declaration.  */
6577
 
6578
void
6579
store_parm_decls_from (struct c_arg_info *arg_info)
6580
{
6581
  current_function_arg_info = arg_info;
6582
  store_parm_decls ();
6583
}
6584
 
6585
/* Store the parameter declarations into the current function declaration.
6586
   This is called after parsing the parameter declarations, before
6587
   digesting the body of the function.
6588
 
6589
   For an old-style definition, construct a prototype out of the old-style
6590
   parameter declarations and inject it into the function's type.  */
6591
 
6592
void
6593
store_parm_decls (void)
6594
{
6595
  tree fndecl = current_function_decl;
6596
  bool proto;
6597
 
6598
  /* The argument information block for FNDECL.  */
6599
  struct c_arg_info *arg_info = current_function_arg_info;
6600
  current_function_arg_info = 0;
6601
 
6602
  /* True if this definition is written with a prototype.  Note:
6603
     despite C99 6.7.5.3p14, we can *not* treat an empty argument
6604
     list in a function definition as equivalent to (void) -- an
6605
     empty argument list specifies the function has no parameters,
6606
     but only (void) sets up a prototype for future calls.  */
6607
  proto = arg_info->types != 0;
6608
 
6609
  if (proto)
6610
    store_parm_decls_newstyle (fndecl, arg_info);
6611
  else
6612
    store_parm_decls_oldstyle (fndecl, arg_info);
6613
 
6614
  /* The next call to push_scope will be a function body.  */
6615
 
6616
  next_is_function_body = true;
6617
 
6618
  /* Write a record describing this function definition to the prototypes
6619
     file (if requested).  */
6620
 
6621
  gen_aux_info_record (fndecl, 1, 0, proto);
6622
 
6623
  /* Initialize the RTL code for the function.  */
6624
  allocate_struct_function (fndecl);
6625
 
6626
  /* Begin the statement tree for this function.  */
6627
  DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6628
 
6629
  /* ??? Insert the contents of the pending sizes list into the function
6630
     to be evaluated.  The only reason left to have this is
6631
        void foo(int n, int array[n++])
6632
     because we throw away the array type in favor of a pointer type, and
6633
     thus won't naturally see the SAVE_EXPR containing the increment.  All
6634
     other pending sizes would be handled by gimplify_parameters.  */
6635
  {
6636
    tree t;
6637
    for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6638
      add_stmt (TREE_VALUE (t));
6639
  }
6640
 
6641
  /* Even though we're inside a function body, we still don't want to
6642
     call expand_expr to calculate the size of a variable-sized array.
6643
     We haven't necessarily assigned RTL to all variables yet, so it's
6644
     not safe to try to expand expressions involving them.  */
6645
  cfun->x_dont_save_pending_sizes_p = 1;
6646
}
6647
 
6648
/* Emit diagnostics that require gimple input for detection.  Operate on
6649
   FNDECL and all its nested functions.  */
6650
 
6651
static void
6652
c_gimple_diagnostics_recursively (tree fndecl)
6653
{
6654
  struct cgraph_node *cgn;
6655
 
6656
  /* Handle attribute((warn_unused_result)).  Relies on gimple input.  */
6657
  c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6658
 
6659
  /* Notice when OpenMP structured block constraints are violated.  */
6660
  if (flag_openmp)
6661
    diagnose_omp_structured_block_errors (fndecl);
6662
 
6663
  /* Finalize all nested functions now.  */
6664
  cgn = cgraph_node (fndecl);
6665
  for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6666
    c_gimple_diagnostics_recursively (cgn->decl);
6667
}
6668
 
6669
/* Finish up a function declaration and compile that function
6670
   all the way to assembler language output.  The free the storage
6671
   for the function definition.
6672
 
6673
   This is called after parsing the body of the function definition.  */
6674
 
6675
void
6676
finish_function (void)
6677
{
6678
  tree fndecl = current_function_decl;
6679
 
6680
  label_context_stack_se = label_context_stack_se->next;
6681
  label_context_stack_vm = label_context_stack_vm->next;
6682
 
6683
  if (TREE_CODE (fndecl) == FUNCTION_DECL
6684
      && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6685
    {
6686
      tree args = DECL_ARGUMENTS (fndecl);
6687
      for (; args; args = TREE_CHAIN (args))
6688
        {
6689
          tree type = TREE_TYPE (args);
6690
          if (INTEGRAL_TYPE_P (type)
6691
              && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6692
            DECL_ARG_TYPE (args) = integer_type_node;
6693
        }
6694
    }
6695
 
6696
  if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6697
    BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6698
 
6699
  /* Must mark the RESULT_DECL as being in this function.  */
6700
 
6701
  if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6702
    DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6703
 
6704
  if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6705
    {
6706
      if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6707
          != integer_type_node)
6708
        {
6709
          /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6710
             If warn_main is -1 (-Wno-main) we don't want to be warned.  */
6711
          if (!warn_main)
6712
            pedwarn ("return type of %q+D is not %<int%>", fndecl);
6713
        }
6714
      else
6715
        {
6716
          if (flag_isoc99)
6717
            {
6718
              tree stmt = c_finish_return (integer_zero_node);
6719
#ifdef USE_MAPPED_LOCATION
6720
              /* Hack.  We don't want the middle-end to warn that this return
6721
                 is unreachable, so we mark its location as special.  Using
6722
                 UNKNOWN_LOCATION has the problem that it gets clobbered in
6723
                 annotate_one_with_locus.  A cleaner solution might be to
6724
                 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6725
              */
6726
              SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6727
#else
6728
              /* Hack.  We don't want the middle-end to warn that this
6729
                 return is unreachable, so put the statement on the
6730
                 special line 0.  */
6731
              annotate_with_file_line (stmt, input_filename, 0);
6732
#endif
6733
            }
6734
        }
6735
    }
6736
 
6737
  /* Tie off the statement tree for this function.  */
6738
  DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6739
 
6740
  finish_fname_decls ();
6741
 
6742
  /* Complain if there's just no return statement.  */
6743
  if (warn_return_type
6744
      && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6745
      && !current_function_returns_value && !current_function_returns_null
6746
      /* Don't complain if we are no-return.  */
6747
      && !current_function_returns_abnormally
6748
      /* Don't warn for main().  */
6749
      && !MAIN_NAME_P (DECL_NAME (fndecl))
6750
      /* Or if they didn't actually specify a return type.  */
6751
      && !C_FUNCTION_IMPLICIT_INT (fndecl)
6752
      /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
6753
         inline function, as we might never be compiled separately.  */
6754
      && DECL_INLINE (fndecl))
6755
    {
6756
      warning (OPT_Wreturn_type,
6757
               "no return statement in function returning non-void");
6758
      TREE_NO_WARNING (fndecl) = 1;
6759
    }
6760
 
6761
  /* With just -Wextra, complain only if function returns both with
6762
     and without a value.  */
6763
  if (extra_warnings
6764
      && current_function_returns_value
6765
      && current_function_returns_null)
6766
    warning (OPT_Wextra, "this function may return with or without a value");
6767
 
6768
  /* Store the end of the function, so that we get good line number
6769
     info for the epilogue.  */
6770
  cfun->function_end_locus = input_location;
6771
 
6772
  /* If we don't have ctors/dtors sections, and this is a static
6773
     constructor or destructor, it must be recorded now.  */
6774
  if (DECL_STATIC_CONSTRUCTOR (fndecl)
6775
      && !targetm.have_ctors_dtors)
6776
    static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6777
  if (DECL_STATIC_DESTRUCTOR (fndecl)
6778
      && !targetm.have_ctors_dtors)
6779
    static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6780
 
6781
  /* Finalize the ELF visibility for the function.  */
6782
  c_determine_visibility (fndecl);
6783
 
6784
  /* Genericize before inlining.  Delay genericizing nested functions
6785
     until their parent function is genericized.  Since finalizing
6786
     requires GENERIC, delay that as well.  */
6787
 
6788
  if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6789
      && !undef_nested_function)
6790
    {
6791
      if (!decl_function_context (fndecl))
6792
        {
6793
          c_genericize (fndecl);
6794
          c_gimple_diagnostics_recursively (fndecl);
6795
 
6796
          /* ??? Objc emits functions after finalizing the compilation unit.
6797
             This should be cleaned up later and this conditional removed.  */
6798
          if (cgraph_global_info_ready)
6799
            {
6800
              c_expand_body (fndecl);
6801
              return;
6802
            }
6803
 
6804
          cgraph_finalize_function (fndecl, false);
6805
        }
6806
      else
6807
        {
6808
          /* Register this function with cgraph just far enough to get it
6809
            added to our parent's nested function list.  Handy, since the
6810
            C front end doesn't have such a list.  */
6811
          (void) cgraph_node (fndecl);
6812
        }
6813
    }
6814
 
6815
  if (!decl_function_context (fndecl))
6816
    undef_nested_function = false;
6817
 
6818
  /* We're leaving the context of this function, so zap cfun.
6819
     It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6820
     tree_rest_of_compilation.  */
6821
  cfun = NULL;
6822
  current_function_decl = NULL;
6823
}
6824
 
6825
/* Generate the RTL for the body of FNDECL.  */
6826
 
6827
void
6828
c_expand_body (tree fndecl)
6829
{
6830
 
6831
  if (!DECL_INITIAL (fndecl)
6832
      || DECL_INITIAL (fndecl) == error_mark_node)
6833
    return;
6834
 
6835
  tree_rest_of_compilation (fndecl);
6836
 
6837
  if (DECL_STATIC_CONSTRUCTOR (fndecl)
6838
      && targetm.have_ctors_dtors)
6839
    targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6840
                                 DEFAULT_INIT_PRIORITY);
6841
  if (DECL_STATIC_DESTRUCTOR (fndecl)
6842
      && targetm.have_ctors_dtors)
6843
    targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6844
                                DEFAULT_INIT_PRIORITY);
6845
}
6846
 
6847
/* Check the declarations given in a for-loop for satisfying the C99
6848
   constraints.  If exactly one such decl is found, return it.  */
6849
 
6850
tree
6851
check_for_loop_decls (void)
6852
{
6853
  struct c_binding *b;
6854
  tree one_decl = NULL_TREE;
6855
  int n_decls = 0;
6856
 
6857
 
6858
  if (!flag_isoc99)
6859
    {
6860
      /* If we get here, declarations have been used in a for loop without
6861
         the C99 for loop scope.  This doesn't make much sense, so don't
6862
         allow it.  */
6863
      error ("%<for%> loop initial declaration used outside C99 mode");
6864
      return NULL_TREE;
6865
    }
6866
  /* C99 subclause 6.8.5 paragraph 3:
6867
 
6868
       [#3]  The  declaration  part  of  a for statement shall only
6869
       declare identifiers for objects having storage class auto or
6870
       register.
6871
 
6872
     It isn't clear whether, in this sentence, "identifiers" binds to
6873
     "shall only declare" or to "objects" - that is, whether all identifiers
6874
     declared must be identifiers for objects, or whether the restriction
6875
     only applies to those that are.  (A question on this in comp.std.c
6876
     in November 2000 received no answer.)  We implement the strictest
6877
     interpretation, to avoid creating an extension which later causes
6878
     problems.  */
6879
 
6880
  for (b = current_scope->bindings; b; b = b->prev)
6881
    {
6882
      tree id = b->id;
6883
      tree decl = b->decl;
6884
 
6885
      if (!id)
6886
        continue;
6887
 
6888
      switch (TREE_CODE (decl))
6889
        {
6890
        case VAR_DECL:
6891
          if (TREE_STATIC (decl))
6892
            error ("declaration of static variable %q+D in %<for%> loop "
6893
                   "initial declaration", decl);
6894
          else if (DECL_EXTERNAL (decl))
6895
            error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6896
                   "initial declaration", decl);
6897
          break;
6898
 
6899
        case RECORD_TYPE:
6900
          error ("%<struct %E%> declared in %<for%> loop initial declaration",
6901
                 id);
6902
          break;
6903
        case UNION_TYPE:
6904
          error ("%<union %E%> declared in %<for%> loop initial declaration",
6905
                 id);
6906
          break;
6907
        case ENUMERAL_TYPE:
6908
          error ("%<enum %E%> declared in %<for%> loop initial declaration",
6909
                 id);
6910
          break;
6911
        default:
6912
          error ("declaration of non-variable %q+D in %<for%> loop "
6913
                 "initial declaration", decl);
6914
        }
6915
 
6916
      n_decls++;
6917
      one_decl = decl;
6918
    }
6919
 
6920
  return n_decls == 1 ? one_decl : NULL_TREE;
6921
}
6922
 
6923
/* Save and reinitialize the variables
6924
   used during compilation of a C function.  */
6925
 
6926
void
6927
c_push_function_context (struct function *f)
6928
{
6929
  struct language_function *p;
6930
  p = GGC_NEW (struct language_function);
6931
  f->language = p;
6932
 
6933
  p->base.x_stmt_tree = c_stmt_tree;
6934
  p->x_break_label = c_break_label;
6935
  p->x_cont_label = c_cont_label;
6936
  p->x_switch_stack = c_switch_stack;
6937
  p->arg_info = current_function_arg_info;
6938
  p->returns_value = current_function_returns_value;
6939
  p->returns_null = current_function_returns_null;
6940
  p->returns_abnormally = current_function_returns_abnormally;
6941
  p->warn_about_return_type = warn_about_return_type;
6942
  p->extern_inline = current_extern_inline;
6943
}
6944
 
6945
/* Restore the variables used during compilation of a C function.  */
6946
 
6947
void
6948
c_pop_function_context (struct function *f)
6949
{
6950
  struct language_function *p = f->language;
6951
 
6952
  if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6953
      && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6954
    {
6955
      /* Stop pointing to the local nodes about to be freed.  */
6956
      /* But DECL_INITIAL must remain nonzero so we know this
6957
         was an actual function definition.  */
6958
      DECL_INITIAL (current_function_decl) = error_mark_node;
6959
      DECL_ARGUMENTS (current_function_decl) = 0;
6960
    }
6961
 
6962
  c_stmt_tree = p->base.x_stmt_tree;
6963
  c_break_label = p->x_break_label;
6964
  c_cont_label = p->x_cont_label;
6965
  c_switch_stack = p->x_switch_stack;
6966
  current_function_arg_info = p->arg_info;
6967
  current_function_returns_value = p->returns_value;
6968
  current_function_returns_null = p->returns_null;
6969
  current_function_returns_abnormally = p->returns_abnormally;
6970
  warn_about_return_type = p->warn_about_return_type;
6971
  current_extern_inline = p->extern_inline;
6972
 
6973
  f->language = NULL;
6974
}
6975
 
6976
/* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
6977
 
6978
void
6979
c_dup_lang_specific_decl (tree decl)
6980
{
6981
  struct lang_decl *ld;
6982
 
6983
  if (!DECL_LANG_SPECIFIC (decl))
6984
    return;
6985
 
6986
  ld = GGC_NEW (struct lang_decl);
6987
  memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6988
  DECL_LANG_SPECIFIC (decl) = ld;
6989
}
6990
 
6991
/* The functions below are required for functionality of doing
6992
   function at once processing in the C front end. Currently these
6993
   functions are not called from anywhere in the C front end, but as
6994
   these changes continue, that will change.  */
6995
 
6996
/* Returns the stmt_tree (if any) to which statements are currently
6997
   being added.  If there is no active statement-tree, NULL is
6998
   returned.  */
6999
 
7000
stmt_tree
7001
current_stmt_tree (void)
7002
{
7003
  return &c_stmt_tree;
7004
}
7005
 
7006
/* Nonzero if TYPE is an anonymous union or struct type.  Always 0 in
7007
   C.  */
7008
 
7009
int
7010
anon_aggr_type_p (tree ARG_UNUSED (node))
7011
{
7012
  return 0;
7013
}
7014
 
7015
/* Return the global value of T as a symbol.  */
7016
 
7017
tree
7018
identifier_global_value (tree t)
7019
{
7020
  struct c_binding *b;
7021
 
7022
  for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
7023
    if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
7024
      return b->decl;
7025
 
7026
  return 0;
7027
}
7028
 
7029
/* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
7030
   otherwise the name is found in ridpointers from RID_INDEX.  */
7031
 
7032
void
7033
record_builtin_type (enum rid rid_index, const char *name, tree type)
7034
{
7035
  tree id, decl;
7036
  if (name == 0)
7037
    id = ridpointers[(int) rid_index];
7038
  else
7039
    id = get_identifier (name);
7040
  decl = build_decl (TYPE_DECL, id, type);
7041
  pushdecl (decl);
7042
  if (debug_hooks->type_decl)
7043
    debug_hooks->type_decl (decl, false);
7044
}
7045
 
7046
/* Build the void_list_node (void_type_node having been created).  */
7047
tree
7048
build_void_list_node (void)
7049
{
7050
  tree t = build_tree_list (NULL_TREE, void_type_node);
7051
  return t;
7052
}
7053
 
7054
/* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR.  */
7055
 
7056
struct c_parm *
7057
build_c_parm (struct c_declspecs *specs, tree attrs,
7058
              struct c_declarator *declarator)
7059
{
7060
  struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7061
  ret->specs = specs;
7062
  ret->attrs = attrs;
7063
  ret->declarator = declarator;
7064
  return ret;
7065
}
7066
 
7067
/* Return a declarator with nested attributes.  TARGET is the inner
7068
   declarator to which these attributes apply.  ATTRS are the
7069
   attributes.  */
7070
 
7071
struct c_declarator *
7072
build_attrs_declarator (tree attrs, struct c_declarator *target)
7073
{
7074
  struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7075
  ret->kind = cdk_attrs;
7076
  ret->declarator = target;
7077
  ret->u.attrs = attrs;
7078
  return ret;
7079
}
7080
 
7081
/* Return a declarator for a function with arguments specified by ARGS
7082
   and return type specified by TARGET.  */
7083
 
7084
struct c_declarator *
7085
build_function_declarator (struct c_arg_info *args,
7086
                           struct c_declarator *target)
7087
{
7088
  struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7089
  ret->kind = cdk_function;
7090
  ret->declarator = target;
7091
  ret->u.arg_info = args;
7092
  return ret;
7093
}
7094
 
7095
/* Return a declarator for the identifier IDENT (which may be
7096
   NULL_TREE for an abstract declarator).  */
7097
 
7098
struct c_declarator *
7099
build_id_declarator (tree ident)
7100
{
7101
  struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7102
  ret->kind = cdk_id;
7103
  ret->declarator = 0;
7104
  ret->u.id = ident;
7105
  /* Default value - may get reset to a more precise location. */
7106
  ret->id_loc = input_location;
7107
  return ret;
7108
}
7109
 
7110
/* Return something to represent absolute declarators containing a *.
7111
   TARGET is the absolute declarator that the * contains.
7112
   TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7113
   to apply to the pointer type.  */
7114
 
7115
struct c_declarator *
7116
make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7117
                         struct c_declarator *target)
7118
{
7119
  tree attrs;
7120
  int quals = 0;
7121
  struct c_declarator *itarget = target;
7122
  struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7123
  if (type_quals_attrs)
7124
    {
7125
      attrs = type_quals_attrs->attrs;
7126
      quals = quals_from_declspecs (type_quals_attrs);
7127
      if (attrs != NULL_TREE)
7128
        itarget = build_attrs_declarator (attrs, target);
7129
    }
7130
  ret->kind = cdk_pointer;
7131
  ret->declarator = itarget;
7132
  ret->u.pointer_quals = quals;
7133
  return ret;
7134
}
7135
 
7136
/* Return a pointer to a structure for an empty list of declaration
7137
   specifiers.  */
7138
 
7139
struct c_declspecs *
7140
build_null_declspecs (void)
7141
{
7142
  struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7143
  ret->type = 0;
7144
  ret->decl_attr = 0;
7145
  ret->attrs = 0;
7146
  ret->typespec_word = cts_none;
7147
  ret->storage_class = csc_none;
7148
  ret->declspecs_seen_p = false;
7149
  ret->type_seen_p = false;
7150
  ret->non_sc_seen_p = false;
7151
  ret->typedef_p = false;
7152
  ret->tag_defined_p = false;
7153
  ret->explicit_signed_p = false;
7154
  ret->deprecated_p = false;
7155
  ret->default_int_p = false;
7156
  ret->long_p = false;
7157
  ret->long_long_p = false;
7158
  ret->short_p = false;
7159
  ret->signed_p = false;
7160
  ret->unsigned_p = false;
7161
  ret->complex_p = false;
7162
  ret->inline_p = false;
7163
  ret->thread_p = false;
7164
  ret->const_p = false;
7165
  ret->volatile_p = false;
7166
  ret->restrict_p = false;
7167
  return ret;
7168
}
7169
 
7170
/* Add the type qualifier QUAL to the declaration specifiers SPECS,
7171
   returning SPECS.  */
7172
 
7173
struct c_declspecs *
7174
declspecs_add_qual (struct c_declspecs *specs, tree qual)
7175
{
7176
  enum rid i;
7177
  bool dupe = false;
7178
  specs->non_sc_seen_p = true;
7179
  specs->declspecs_seen_p = true;
7180
  gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7181
              && C_IS_RESERVED_WORD (qual));
7182
  i = C_RID_CODE (qual);
7183
  switch (i)
7184
    {
7185
    case RID_CONST:
7186
      dupe = specs->const_p;
7187
      specs->const_p = true;
7188
      break;
7189
    case RID_VOLATILE:
7190
      dupe = specs->volatile_p;
7191
      specs->volatile_p = true;
7192
      break;
7193
    case RID_RESTRICT:
7194
      dupe = specs->restrict_p;
7195
      specs->restrict_p = true;
7196
      break;
7197
    default:
7198
      gcc_unreachable ();
7199
    }
7200
  if (dupe && pedantic && !flag_isoc99)
7201
    pedwarn ("duplicate %qE", qual);
7202
  return specs;
7203
}
7204
 
7205
/* Add the type specifier TYPE to the declaration specifiers SPECS,
7206
   returning SPECS.  */
7207
 
7208
struct c_declspecs *
7209
declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7210
{
7211
  tree type = spec.spec;
7212
  specs->non_sc_seen_p = true;
7213
  specs->declspecs_seen_p = true;
7214
  specs->type_seen_p = true;
7215
  if (TREE_DEPRECATED (type))
7216
    specs->deprecated_p = true;
7217
 
7218
  /* Handle type specifier keywords.  */
7219
  if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7220
    {
7221
      enum rid i = C_RID_CODE (type);
7222
      if (specs->type)
7223
        {
7224
          error ("two or more data types in declaration specifiers");
7225
          return specs;
7226
        }
7227
      if ((int) i <= (int) RID_LAST_MODIFIER)
7228
        {
7229
          /* "long", "short", "signed", "unsigned" or "_Complex".  */
7230
          bool dupe = false;
7231
          switch (i)
7232
            {
7233
            case RID_LONG:
7234
              if (specs->long_long_p)
7235
                {
7236
                  error ("%<long long long%> is too long for GCC");
7237
                  break;
7238
                }
7239
              if (specs->long_p)
7240
                {
7241
                  if (specs->typespec_word == cts_double)
7242
                    {
7243
                      error ("both %<long long%> and %<double%> in "
7244
                             "declaration specifiers");
7245
                      break;
7246
                    }
7247
                  if (pedantic && !flag_isoc99 && !in_system_header
7248
                      && warn_long_long)
7249
                    pedwarn ("ISO C90 does not support %<long long%>");
7250
                  specs->long_long_p = 1;
7251
                  break;
7252
                }
7253
              if (specs->short_p)
7254
                error ("both %<long%> and %<short%> in "
7255
                       "declaration specifiers");
7256
              else if (specs->typespec_word == cts_void)
7257
                error ("both %<long%> and %<void%> in "
7258
                       "declaration specifiers");
7259
              else if (specs->typespec_word == cts_bool)
7260
                error ("both %<long%> and %<_Bool%> in "
7261
                       "declaration specifiers");
7262
              else if (specs->typespec_word == cts_char)
7263
                error ("both %<long%> and %<char%> in "
7264
                       "declaration specifiers");
7265
              else if (specs->typespec_word == cts_float)
7266
                error ("both %<long%> and %<float%> in "
7267
                       "declaration specifiers");
7268
              else if (specs->typespec_word == cts_dfloat32)
7269
                error ("both %<long%> and %<_Decimal32%> in "
7270
                       "declaration specifiers");
7271
              else if (specs->typespec_word == cts_dfloat64)
7272
                error ("both %<long%> and %<_Decimal64%> in "
7273
                       "declaration specifiers");
7274
              else if (specs->typespec_word == cts_dfloat128)
7275
                error ("both %<long%> and %<_Decimal128%> in "
7276
                       "declaration specifiers");
7277
              else
7278
                specs->long_p = true;
7279
              break;
7280
            case RID_SHORT:
7281
              dupe = specs->short_p;
7282
              if (specs->long_p)
7283
                error ("both %<long%> and %<short%> in "
7284
                       "declaration specifiers");
7285
              else if (specs->typespec_word == cts_void)
7286
                error ("both %<short%> and %<void%> in "
7287
                       "declaration specifiers");
7288
              else if (specs->typespec_word == cts_bool)
7289
                error ("both %<short%> and %<_Bool%> in "
7290
                       "declaration specifiers");
7291
              else if (specs->typespec_word == cts_char)
7292
                error ("both %<short%> and %<char%> in "
7293
                       "declaration specifiers");
7294
              else if (specs->typespec_word == cts_float)
7295
                error ("both %<short%> and %<float%> in "
7296
                       "declaration specifiers");
7297
              else if (specs->typespec_word == cts_double)
7298
                error ("both %<short%> and %<double%> in "
7299
                       "declaration specifiers");
7300
              else if (specs->typespec_word == cts_dfloat32)
7301
                error ("both %<short%> and %<_Decimal32%> in "
7302
                       "declaration specifiers");
7303
              else if (specs->typespec_word == cts_dfloat64)
7304
                error ("both %<short%> and %<_Decimal64%> in "
7305
                                        "declaration specifiers");
7306
              else if (specs->typespec_word == cts_dfloat128)
7307
                error ("both %<short%> and %<_Decimal128%> in "
7308
                       "declaration specifiers");
7309
              else
7310
                specs->short_p = true;
7311
              break;
7312
            case RID_SIGNED:
7313
              dupe = specs->signed_p;
7314
              if (specs->unsigned_p)
7315
                error ("both %<signed%> and %<unsigned%> in "
7316
                       "declaration specifiers");
7317
              else if (specs->typespec_word == cts_void)
7318
                error ("both %<signed%> and %<void%> in "
7319
                       "declaration specifiers");
7320
              else if (specs->typespec_word == cts_bool)
7321
                error ("both %<signed%> and %<_Bool%> in "
7322
                       "declaration specifiers");
7323
              else if (specs->typespec_word == cts_float)
7324
                error ("both %<signed%> and %<float%> in "
7325
                       "declaration specifiers");
7326
              else if (specs->typespec_word == cts_double)
7327
                error ("both %<signed%> and %<double%> in "
7328
                       "declaration specifiers");
7329
              else if (specs->typespec_word == cts_dfloat32)
7330
                error ("both %<signed%> and %<_Decimal32%> in "
7331
                       "declaration specifiers");
7332
              else if (specs->typespec_word == cts_dfloat64)
7333
                error ("both %<signed%> and %<_Decimal64%> in "
7334
                       "declaration specifiers");
7335
              else if (specs->typespec_word == cts_dfloat128)
7336
                error ("both %<signed%> and %<_Decimal128%> in "
7337
                       "declaration specifiers");
7338
              else
7339
                specs->signed_p = true;
7340
              break;
7341
            case RID_UNSIGNED:
7342
              dupe = specs->unsigned_p;
7343
              if (specs->signed_p)
7344
                error ("both %<signed%> and %<unsigned%> in "
7345
                       "declaration specifiers");
7346
              else if (specs->typespec_word == cts_void)
7347
                error ("both %<unsigned%> and %<void%> in "
7348
                       "declaration specifiers");
7349
              else if (specs->typespec_word == cts_bool)
7350
                error ("both %<unsigned%> and %<_Bool%> in "
7351
                       "declaration specifiers");
7352
              else if (specs->typespec_word == cts_float)
7353
                error ("both %<unsigned%> and %<float%> in "
7354
                       "declaration specifiers");
7355
              else if (specs->typespec_word == cts_double)
7356
                error ("both %<unsigned%> and %<double%> in "
7357
                       "declaration specifiers");
7358
              else if (specs->typespec_word == cts_dfloat32)
7359
                error ("both %<unsigned%> and %<_Decimal32%> in "
7360
                       "declaration specifiers");
7361
              else if (specs->typespec_word == cts_dfloat64)
7362
                error ("both %<unsigned%> and %<_Decimal64%> in "
7363
                       "declaration specifiers");
7364
              else if (specs->typespec_word == cts_dfloat128)
7365
                error ("both %<unsigned%> and %<_Decimal128%> in "
7366
                       "declaration specifiers");
7367
              else
7368
                specs->unsigned_p = true;
7369
              break;
7370
            case RID_COMPLEX:
7371
              dupe = specs->complex_p;
7372
              if (pedantic && !flag_isoc99 && !in_system_header)
7373
                pedwarn ("ISO C90 does not support complex types");
7374
              if (specs->typespec_word == cts_void)
7375
                error ("both %<complex%> and %<void%> in "
7376
                       "declaration specifiers");
7377
              else if (specs->typespec_word == cts_bool)
7378
                error ("both %<complex%> and %<_Bool%> in "
7379
                       "declaration specifiers");
7380
              else if (specs->typespec_word == cts_dfloat32)
7381
                error ("both %<complex%> and %<_Decimal32%> in "
7382
                       "declaration specifiers");
7383
              else if (specs->typespec_word == cts_dfloat64)
7384
                error ("both %<complex%> and %<_Decimal64%> in "
7385
                       "declaration specifiers");
7386
              else if (specs->typespec_word == cts_dfloat128)
7387
                error ("both %<complex%> and %<_Decimal128%> in "
7388
                       "declaration specifiers");
7389
              else
7390
                specs->complex_p = true;
7391
              break;
7392
            default:
7393
              gcc_unreachable ();
7394
            }
7395
 
7396
          if (dupe)
7397
            error ("duplicate %qE", type);
7398
 
7399
          return specs;
7400
        }
7401
      else
7402
        {
7403
          /* "void", "_Bool", "char", "int", "float" or "double".  */
7404
          if (specs->typespec_word != cts_none)
7405
            {
7406
              error ("two or more data types in declaration specifiers");
7407
              return specs;
7408
            }
7409
          switch (i)
7410
            {
7411
            case RID_VOID:
7412
              if (specs->long_p)
7413
                error ("both %<long%> and %<void%> in "
7414
                       "declaration specifiers");
7415
              else if (specs->short_p)
7416
                error ("both %<short%> and %<void%> in "
7417
                       "declaration specifiers");
7418
              else if (specs->signed_p)
7419
                error ("both %<signed%> and %<void%> in "
7420
                       "declaration specifiers");
7421
              else if (specs->unsigned_p)
7422
                error ("both %<unsigned%> and %<void%> in "
7423
                       "declaration specifiers");
7424
              else if (specs->complex_p)
7425
                error ("both %<complex%> and %<void%> in "
7426
                       "declaration specifiers");
7427
              else
7428
                specs->typespec_word = cts_void;
7429
              return specs;
7430
            case RID_BOOL:
7431
              if (specs->long_p)
7432
                error ("both %<long%> and %<_Bool%> in "
7433
                       "declaration specifiers");
7434
              else if (specs->short_p)
7435
                error ("both %<short%> and %<_Bool%> in "
7436
                       "declaration specifiers");
7437
              else if (specs->signed_p)
7438
                error ("both %<signed%> and %<_Bool%> in "
7439
                       "declaration specifiers");
7440
              else if (specs->unsigned_p)
7441
                error ("both %<unsigned%> and %<_Bool%> in "
7442
                       "declaration specifiers");
7443
              else if (specs->complex_p)
7444
                error ("both %<complex%> and %<_Bool%> in "
7445
                       "declaration specifiers");
7446
              else
7447
                specs->typespec_word = cts_bool;
7448
              return specs;
7449
            case RID_CHAR:
7450
              if (specs->long_p)
7451
                error ("both %<long%> and %<char%> in "
7452
                       "declaration specifiers");
7453
              else if (specs->short_p)
7454
                error ("both %<short%> and %<char%> in "
7455
                       "declaration specifiers");
7456
              else
7457
                specs->typespec_word = cts_char;
7458
              return specs;
7459
            case RID_INT:
7460
              specs->typespec_word = cts_int;
7461
              return specs;
7462
            case RID_FLOAT:
7463
              if (specs->long_p)
7464
                error ("both %<long%> and %<float%> in "
7465
                       "declaration specifiers");
7466
              else if (specs->short_p)
7467
                error ("both %<short%> and %<float%> in "
7468
                       "declaration specifiers");
7469
              else if (specs->signed_p)
7470
                error ("both %<signed%> and %<float%> in "
7471
                       "declaration specifiers");
7472
              else if (specs->unsigned_p)
7473
                error ("both %<unsigned%> and %<float%> in "
7474
                       "declaration specifiers");
7475
              else
7476
                specs->typespec_word = cts_float;
7477
              return specs;
7478
            case RID_DOUBLE:
7479
              if (specs->long_long_p)
7480
                error ("both %<long long%> and %<double%> in "
7481
                       "declaration specifiers");
7482
              else if (specs->short_p)
7483
                error ("both %<short%> and %<double%> in "
7484
                       "declaration specifiers");
7485
              else if (specs->signed_p)
7486
                error ("both %<signed%> and %<double%> in "
7487
                       "declaration specifiers");
7488
              else if (specs->unsigned_p)
7489
                error ("both %<unsigned%> and %<double%> in "
7490
                       "declaration specifiers");
7491
              else
7492
                specs->typespec_word = cts_double;
7493
              return specs;
7494
            case RID_DFLOAT32:
7495
            case RID_DFLOAT64:
7496
            case RID_DFLOAT128:
7497
              {
7498
                const char *str;
7499
                if (i == RID_DFLOAT32)
7500
                  str = "_Decimal32";
7501
                else if (i == RID_DFLOAT64)
7502
                  str = "_Decimal64";
7503
                else
7504
                  str = "_Decimal128";
7505
                if (specs->long_long_p)
7506
                  error ("both %<long long%> and %<%s%> in "
7507
                         "declaration specifiers", str);
7508
                if (specs->long_p)
7509
                  error ("both %<long%> and %<%s%> in "
7510
                         "declaration specifiers", str);
7511
                else if (specs->short_p)
7512
                  error ("both %<short%> and %<%s%> in "
7513
                         "declaration specifiers", str);
7514
                else if (specs->signed_p)
7515
                  error ("both %<signed%> and %<%s%> in "
7516
                         "declaration specifiers", str);
7517
                else if (specs->unsigned_p)
7518
                  error ("both %<unsigned%> and %<%s%> in "
7519
                         "declaration specifiers", str);
7520
                else if (specs->complex_p)
7521
                  error ("both %<complex%> and %<%s%> in "
7522
                         "declaration specifiers", str);
7523
                else if (i == RID_DFLOAT32)
7524
                  specs->typespec_word = cts_dfloat32;
7525
                else if (i == RID_DFLOAT64)
7526
                  specs->typespec_word = cts_dfloat64;
7527
                else
7528
                  specs->typespec_word = cts_dfloat128;
7529
              }
7530
              if (!targetm.decimal_float_supported_p ())
7531
                error ("decimal floating point not supported for this target");
7532
              if (pedantic)
7533
                pedwarn ("ISO C does not support decimal floating point");
7534
              return specs;
7535
            default:
7536
              /* ObjC reserved word "id", handled below.  */
7537
              break;
7538
            }
7539
        }
7540
    }
7541
 
7542
  /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7543
     form of ObjC type, cases such as "int" and "long" being handled
7544
     above), a TYPE (struct, union, enum and typeof specifiers) or an
7545
     ERROR_MARK.  In none of these cases may there have previously
7546
     been any type specifiers.  */
7547
  if (specs->type || specs->typespec_word != cts_none
7548
      || specs->long_p || specs->short_p || specs->signed_p
7549
      || specs->unsigned_p || specs->complex_p)
7550
    error ("two or more data types in declaration specifiers");
7551
  else if (TREE_CODE (type) == TYPE_DECL)
7552
    {
7553
      if (TREE_TYPE (type) == error_mark_node)
7554
        ; /* Allow the type to default to int to avoid cascading errors.  */
7555
      else
7556
        {
7557
          specs->type = TREE_TYPE (type);
7558
          specs->decl_attr = DECL_ATTRIBUTES (type);
7559
          specs->typedef_p = true;
7560
          specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7561
        }
7562
    }
7563
  else if (TREE_CODE (type) == IDENTIFIER_NODE)
7564
    {
7565
      tree t = lookup_name (type);
7566
      if (!t || TREE_CODE (t) != TYPE_DECL)
7567
        error ("%qE fails to be a typedef or built in type", type);
7568
      else if (TREE_TYPE (t) == error_mark_node)
7569
        ;
7570
      else
7571
        specs->type = TREE_TYPE (t);
7572
    }
7573
  else if (TREE_CODE (type) != ERROR_MARK)
7574
    {
7575
      if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7576
        specs->tag_defined_p = true;
7577
      if (spec.kind == ctsk_typeof)
7578
        specs->typedef_p = true;
7579
      specs->type = type;
7580
    }
7581
 
7582
  return specs;
7583
}
7584
 
7585
/* Add the storage class specifier or function specifier SCSPEC to the
7586
   declaration specifiers SPECS, returning SPECS.  */
7587
 
7588
struct c_declspecs *
7589
declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7590
{
7591
  enum rid i;
7592
  enum c_storage_class n = csc_none;
7593
  bool dupe = false;
7594
  specs->declspecs_seen_p = true;
7595
  gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7596
              && C_IS_RESERVED_WORD (scspec));
7597
  i = C_RID_CODE (scspec);
7598
  if (extra_warnings && specs->non_sc_seen_p)
7599
    warning (OPT_Wextra, "%qE is not at beginning of declaration", scspec);
7600
  switch (i)
7601
    {
7602
    case RID_INLINE:
7603
      /* C99 permits duplicate inline.  Although of doubtful utility,
7604
         it seems simplest to permit it in gnu89 mode as well, as
7605
         there is also little utility in maintaining this as a
7606
         difference between gnu89 and C99 inline.  */
7607
      dupe = false;
7608
      specs->inline_p = true;
7609
      break;
7610
    case RID_THREAD:
7611
      dupe = specs->thread_p;
7612
      if (specs->storage_class == csc_auto)
7613
        error ("%<__thread%> used with %<auto%>");
7614
      else if (specs->storage_class == csc_register)
7615
        error ("%<__thread%> used with %<register%>");
7616
      else if (specs->storage_class == csc_typedef)
7617
        error ("%<__thread%> used with %<typedef%>");
7618
      else
7619
        specs->thread_p = true;
7620
      break;
7621
    case RID_AUTO:
7622
      n = csc_auto;
7623
      break;
7624
    case RID_EXTERN:
7625
      n = csc_extern;
7626
      /* Diagnose "__thread extern".  */
7627
      if (specs->thread_p)
7628
        error ("%<__thread%> before %<extern%>");
7629
      break;
7630
    case RID_REGISTER:
7631
      n = csc_register;
7632
      break;
7633
    case RID_STATIC:
7634
      n = csc_static;
7635
      /* Diagnose "__thread static".  */
7636
      if (specs->thread_p)
7637
        error ("%<__thread%> before %<static%>");
7638
      break;
7639
    case RID_TYPEDEF:
7640
      n = csc_typedef;
7641
      break;
7642
    default:
7643
      gcc_unreachable ();
7644
    }
7645
  if (n != csc_none && n == specs->storage_class)
7646
    dupe = true;
7647
  if (dupe)
7648
    error ("duplicate %qE", scspec);
7649
  if (n != csc_none)
7650
    {
7651
      if (specs->storage_class != csc_none && n != specs->storage_class)
7652
        {
7653
          error ("multiple storage classes in declaration specifiers");
7654
        }
7655
      else
7656
        {
7657
          specs->storage_class = n;
7658
          if (n != csc_extern && n != csc_static && specs->thread_p)
7659
            {
7660
              error ("%<__thread%> used with %qE", scspec);
7661
              specs->thread_p = false;
7662
            }
7663
        }
7664
    }
7665
  return specs;
7666
}
7667
 
7668
/* Add the attributes ATTRS to the declaration specifiers SPECS,
7669
   returning SPECS.  */
7670
 
7671
struct c_declspecs *
7672
declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7673
{
7674
  specs->attrs = chainon (attrs, specs->attrs);
7675
  specs->declspecs_seen_p = true;
7676
  return specs;
7677
}
7678
 
7679
/* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7680
   specifiers with any other type specifier to determine the resulting
7681
   type.  This is where ISO C checks on complex types are made, since
7682
   "_Complex long" is a prefix of the valid ISO C type "_Complex long
7683
   double".  */
7684
 
7685
struct c_declspecs *
7686
finish_declspecs (struct c_declspecs *specs)
7687
{
7688
  /* If a type was specified as a whole, we have no modifiers and are
7689
     done.  */
7690
  if (specs->type != NULL_TREE)
7691
    {
7692
      gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7693
                  && !specs->signed_p && !specs->unsigned_p
7694
                  && !specs->complex_p);
7695
      return specs;
7696
    }
7697
 
7698
  /* If none of "void", "_Bool", "char", "int", "float" or "double"
7699
     has been specified, treat it as "int" unless "_Complex" is
7700
     present and there are no other specifiers.  If we just have
7701
     "_Complex", it is equivalent to "_Complex double", but e.g.
7702
     "_Complex short" is equivalent to "_Complex short int".  */
7703
  if (specs->typespec_word == cts_none)
7704
    {
7705
      if (specs->long_p || specs->short_p
7706
          || specs->signed_p || specs->unsigned_p)
7707
        {
7708
          specs->typespec_word = cts_int;
7709
        }
7710
      else if (specs->complex_p)
7711
        {
7712
          specs->typespec_word = cts_double;
7713
          if (pedantic)
7714
            pedwarn ("ISO C does not support plain %<complex%> meaning "
7715
                     "%<double complex%>");
7716
        }
7717
      else
7718
        {
7719
          specs->typespec_word = cts_int;
7720
          specs->default_int_p = true;
7721
          /* We don't diagnose this here because grokdeclarator will
7722
             give more specific diagnostics according to whether it is
7723
             a function definition.  */
7724
        }
7725
    }
7726
 
7727
  /* If "signed" was specified, record this to distinguish "int" and
7728
     "signed int" in the case of a bit-field with
7729
     -funsigned-bitfields.  */
7730
  specs->explicit_signed_p = specs->signed_p;
7731
 
7732
  /* Now compute the actual type.  */
7733
  switch (specs->typespec_word)
7734
    {
7735
    case cts_void:
7736
      gcc_assert (!specs->long_p && !specs->short_p
7737
                  && !specs->signed_p && !specs->unsigned_p
7738
                  && !specs->complex_p);
7739
      specs->type = void_type_node;
7740
      break;
7741
    case cts_bool:
7742
      gcc_assert (!specs->long_p && !specs->short_p
7743
                  && !specs->signed_p && !specs->unsigned_p
7744
                  && !specs->complex_p);
7745
      specs->type = boolean_type_node;
7746
      break;
7747
    case cts_char:
7748
      gcc_assert (!specs->long_p && !specs->short_p);
7749
      gcc_assert (!(specs->signed_p && specs->unsigned_p));
7750
      if (specs->signed_p)
7751
        specs->type = signed_char_type_node;
7752
      else if (specs->unsigned_p)
7753
        specs->type = unsigned_char_type_node;
7754
      else
7755
        specs->type = char_type_node;
7756
      if (specs->complex_p)
7757
        {
7758
          if (pedantic)
7759
            pedwarn ("ISO C does not support complex integer types");
7760
          specs->type = build_complex_type (specs->type);
7761
        }
7762
      break;
7763
    case cts_int:
7764
      gcc_assert (!(specs->long_p && specs->short_p));
7765
      gcc_assert (!(specs->signed_p && specs->unsigned_p));
7766
      if (specs->long_long_p)
7767
        specs->type = (specs->unsigned_p
7768
                       ? long_long_unsigned_type_node
7769
                       : long_long_integer_type_node);
7770
      else if (specs->long_p)
7771
        specs->type = (specs->unsigned_p
7772
                       ? long_unsigned_type_node
7773
                       : long_integer_type_node);
7774
      else if (specs->short_p)
7775
        specs->type = (specs->unsigned_p
7776
                       ? short_unsigned_type_node
7777
                       : short_integer_type_node);
7778
      else
7779
        specs->type = (specs->unsigned_p
7780
                       ? unsigned_type_node
7781
                       : integer_type_node);
7782
      if (specs->complex_p)
7783
        {
7784
          if (pedantic)
7785
            pedwarn ("ISO C does not support complex integer types");
7786
          specs->type = build_complex_type (specs->type);
7787
        }
7788
      break;
7789
    case cts_float:
7790
      gcc_assert (!specs->long_p && !specs->short_p
7791
                  && !specs->signed_p && !specs->unsigned_p);
7792
      specs->type = (specs->complex_p
7793
                     ? complex_float_type_node
7794
                     : float_type_node);
7795
      break;
7796
    case cts_double:
7797
      gcc_assert (!specs->long_long_p && !specs->short_p
7798
                  && !specs->signed_p && !specs->unsigned_p);
7799
      if (specs->long_p)
7800
        {
7801
          specs->type = (specs->complex_p
7802
                         ? complex_long_double_type_node
7803
                         : long_double_type_node);
7804
        }
7805
      else
7806
        {
7807
          specs->type = (specs->complex_p
7808
                         ? complex_double_type_node
7809
                         : double_type_node);
7810
        }
7811
      break;
7812
    case cts_dfloat32:
7813
    case cts_dfloat64:
7814
    case cts_dfloat128:
7815
      gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7816
                  && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7817
      if (specs->typespec_word == cts_dfloat32)
7818
        specs->type = dfloat32_type_node;
7819
      else if (specs->typespec_word == cts_dfloat64)
7820
        specs->type = dfloat64_type_node;
7821
      else
7822
        specs->type = dfloat128_type_node;
7823
      break;
7824
    default:
7825
      gcc_unreachable ();
7826
    }
7827
 
7828
  return specs;
7829
}
7830
 
7831
/* Synthesize a function which calls all the global ctors or global
7832
   dtors in this file.  This is only used for targets which do not
7833
   support .ctors/.dtors sections.  FIXME: Migrate into cgraph.  */
7834
static void
7835
build_cdtor (int method_type, tree cdtors)
7836
{
7837
  tree body = 0;
7838
 
7839
  if (!cdtors)
7840
    return;
7841
 
7842
  for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7843
    append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7844
                              &body);
7845
 
7846
  cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7847
}
7848
 
7849
/* A subroutine of c_write_global_declarations.  Perform final processing
7850
   on one file scope's declarations (or the external scope's declarations),
7851
   GLOBALS.  */
7852
 
7853
static void
7854
c_write_global_declarations_1 (tree globals)
7855
{
7856
  tree decl;
7857
  bool reconsider;
7858
 
7859
  /* Process the decls in the order they were written.  */
7860
  for (decl = globals; decl; decl = TREE_CHAIN (decl))
7861
    {
7862
      /* Check for used but undefined static functions using the C
7863
         standard's definition of "used", and set TREE_NO_WARNING so
7864
         that check_global_declarations doesn't repeat the check.  */
7865
      if (TREE_CODE (decl) == FUNCTION_DECL
7866
          && DECL_INITIAL (decl) == 0
7867
          && DECL_EXTERNAL (decl)
7868
          && !TREE_PUBLIC (decl)
7869
          && C_DECL_USED (decl))
7870
        {
7871
          pedwarn ("%q+F used but never defined", decl);
7872
          TREE_NO_WARNING (decl) = 1;
7873
        }
7874
 
7875
      wrapup_global_declaration_1 (decl);
7876
    }
7877
 
7878
  do
7879
    {
7880
      reconsider = false;
7881
      for (decl = globals; decl; decl = TREE_CHAIN (decl))
7882
        reconsider |= wrapup_global_declaration_2 (decl);
7883
    }
7884
  while (reconsider);
7885
 
7886
  for (decl = globals; decl; decl = TREE_CHAIN (decl))
7887
    check_global_declaration_1 (decl);
7888
}
7889
 
7890
/* A subroutine of c_write_global_declarations Emit debug information for each
7891
   of the declarations in GLOBALS.  */
7892
 
7893
static void
7894
c_write_global_declarations_2 (tree globals)
7895
{
7896
  tree decl;
7897
 
7898
  for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7899
    debug_hooks->global_decl (decl);
7900
}
7901
 
7902
/* Preserve the external declarations scope across a garbage collect.  */
7903
static GTY(()) tree ext_block;
7904
 
7905
void
7906
c_write_global_declarations (void)
7907
{
7908
  tree t;
7909
 
7910
  /* We don't want to do this if generating a PCH.  */
7911
  if (pch_file)
7912
    return;
7913
 
7914
  /* Don't waste time on further processing if -fsyntax-only or we've
7915
     encountered errors.  */
7916
  if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7917
    return;
7918
 
7919
  /* Close the external scope.  */
7920
  ext_block = pop_scope ();
7921
  external_scope = 0;
7922
  gcc_assert (!current_scope);
7923
 
7924
  if (ext_block)
7925
    {
7926
      tree tmp = BLOCK_VARS (ext_block);
7927
      int flags;
7928
      FILE * stream = dump_begin (TDI_tu, &flags);
7929
      if (stream && tmp)
7930
        {
7931
          dump_node (tmp, flags & ~TDF_SLIM, stream);
7932
          dump_end (TDI_tu, stream);
7933
        }
7934
    }
7935
 
7936
  /* Process all file scopes in this compilation, and the external_scope,
7937
     through wrapup_global_declarations and check_global_declarations.  */
7938
  for (t = all_translation_units; t; t = TREE_CHAIN (t))
7939
    c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7940
  c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7941
 
7942
  /* Generate functions to call static constructors and destructors
7943
     for targets that do not support .ctors/.dtors sections.  These
7944
     functions have magic names which are detected by collect2.  */
7945
  build_cdtor ('I', static_ctors); static_ctors = 0;
7946
  build_cdtor ('D', static_dtors); static_dtors = 0;
7947
 
7948
  /* We're done parsing; proceed to optimize and emit assembly.
7949
     FIXME: shouldn't be the front end's responsibility to call this.  */
7950
  cgraph_optimize ();
7951
 
7952
  /* After cgraph has had a chance to emit everything that's going to
7953
     be emitted, output debug information for globals.  */
7954
  if (errorcount == 0 && sorrycount == 0)
7955
    {
7956
      timevar_push (TV_SYMOUT);
7957
      for (t = all_translation_units; t; t = TREE_CHAIN (t))
7958
        c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7959
      c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7960
      timevar_pop (TV_SYMOUT);
7961
    }
7962
 
7963
  ext_block = NULL;
7964
}
7965
 
7966
#include "gt-c-decl.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.