c-decl.c revision 132730
118334Speter/* Process declarations and variables for C compiler.
290075Sobrien   Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3132730Skan   2001, 2002, 2003, 2004 Free Software Foundation, Inc.
418334Speter
590075SobrienThis file is part of GCC.
618334Speter
790075SobrienGCC is free software; you can redistribute it and/or modify it under
890075Sobrienthe terms of the GNU General Public License as published by the Free
990075SobrienSoftware Foundation; either version 2, or (at your option) any later
1090075Sobrienversion.
1118334Speter
1290075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1390075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1490075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1590075Sobrienfor more details.
1618334Speter
1718334SpeterYou should have received a copy of the GNU General Public License
1890075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
1990075SobrienSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA
2090075Sobrien02111-1307, USA.  */
2118334Speter
2296549Sobrien/* $FreeBSD: head/contrib/gcc/c-decl.c 132730 2004-07-28 03:53:20Z kan $ */
2396549Sobrien
2418334Speter/* Process declarations and symbol lookup for C front end.
2518334Speter   Also constructs types; the standard scalar types at initialization,
2618334Speter   and structure, union, array and enum types when they are declared.  */
2718334Speter
2818334Speter/* ??? not all decl nodes are given the most useful possible
2918334Speter   line numbers.  For example, the CONST_DECLs for enum values.  */
3018334Speter
3118334Speter#include "config.h"
3250397Sobrien#include "system.h"
33132730Skan#include "coretypes.h"
34132730Skan#include "tm.h"
3590075Sobrien#include "intl.h"
3618334Speter#include "tree.h"
3790075Sobrien#include "tree-inline.h"
3890075Sobrien#include "rtl.h"
3918334Speter#include "flags.h"
4090075Sobrien#include "function.h"
4118334Speter#include "output.h"
4290075Sobrien#include "expr.h"
4318334Speter#include "c-tree.h"
4450397Sobrien#include "toplev.h"
4590075Sobrien#include "ggc.h"
4690075Sobrien#include "tm_p.h"
4750397Sobrien#include "cpplib.h"
4890075Sobrien#include "target.h"
4990075Sobrien#include "debug.h"
50132730Skan#include "opts.h"
5190075Sobrien#include "timevar.h"
5290075Sobrien#include "c-common.h"
5396263Sobrien#include "c-pragma.h"
54132730Skan#include "cgraph.h"
55132730Skan#include "hashtab.h"
56117421Skan#include "libfuncs.h"
57117421Skan#include "except.h"
58132730Skan#include "langhooks-def.h"
5950397Sobrien
6018334Speter/* In grokdeclarator, distinguish syntactic contexts of declarators.  */
6118334Speterenum decl_context
6218334Speter{ NORMAL,			/* Ordinary declaration */
6318334Speter  FUNCDEF,			/* Function definition */
6418334Speter  PARM,				/* Declaration of parm before function body */
6518334Speter  FIELD,			/* Declaration inside struct or union */
6618334Speter  TYPENAME};			/* Typename (inside cast or sizeof)  */
6718334Speter
6818334Speter
6918334Speter/* Nonzero if we have seen an invalid cross reference
7018334Speter   to a struct, union, or enum, but not yet printed the message.  */
7118334Speter
7218334Spetertree pending_invalid_xref;
7318334Speter/* File and line to appear in the eventual error message.  */
74132730Skanlocation_t pending_invalid_xref_location;
7518334Speter
7618334Speter/* While defining an enum type, this is 1 plus the last enumerator
7718334Speter   constant value.  Note that will do not have to save this or `enum_overflow'
7818334Speter   around nested function definition since such a definition could only
7918334Speter   occur in an enum value expression and we don't use these variables in
8018334Speter   that case.  */
8118334Speter
8218334Speterstatic tree enum_next_value;
8318334Speter
8418334Speter/* Nonzero means that there was overflow computing enum_next_value.  */
8518334Speter
8618334Speterstatic int enum_overflow;
8718334Speter
8818334Speter/* Parsing a function declarator leaves a list of parameter names
89132730Skan   or a chain of parameter decls here.  */
9018334Speter
9118334Speterstatic tree last_function_parms;
9218334Speter
93132730Skan/* ... and a chain of structure and enum types declared in the
94132730Skan   parmlist here.  */
9518334Speter
9618334Speterstatic tree last_function_parm_tags;
9718334Speter
98132730Skan/* ... and a chain of all non-parameter declarations (such as
99132730Skan   CONST_DECLs from enumerations) here.  */
100132730Skan
101132730Skanstatic tree last_function_parm_others;
102132730Skan
10318334Speter/* After parsing the declarator that starts a function definition,
104132730Skan   `start_function' puts the list of parameter names or chain of decls here
105132730Skan   for `store_parm_decls' to find.  */
10618334Speter
10718334Speterstatic tree current_function_parms;
10818334Speter
10918334Speter/* Similar, for last_function_parm_tags.  */
110132730Skan
11118334Speterstatic tree current_function_parm_tags;
11218334Speter
113132730Skan/* And for last_function_parm_others.  */
114132730Skan
115132730Skanstatic tree current_function_parm_others;
116132730Skan
11718334Speter/* Similar, for the file and line that the prototype came from if this is
11818334Speter   an old-style definition.  */
11918334Speter
120132730Skanstatic location_t current_function_prototype_locus;
121132730Skan
12290075Sobrien/* The current statement tree.  */
12390075Sobrien
124117421Skanstatic GTY(()) struct stmt_tree_s c_stmt_tree;
12590075Sobrien
12690075Sobrien/* The current scope statement stack.  */
12790075Sobrien
128117421Skanstatic GTY(()) tree c_scope_stmt_stack;
12990075Sobrien
130132730Skan/* State saving variables.  */
131132730Skanint c_in_iteration_stmt;
132132730Skanint c_in_case_stmt;
13318334Speter
134132730Skan/* A list of external DECLs that appeared at block scope when there was
135132730Skan   some other global meaning for that identifier.  */
136132730Skanstatic GTY(()) tree truly_local_externals;
13718334Speter
138132730Skan/* All the builtins; this is a subset of the entries of global_scope.  */
13918334Speter
140132730Skanstatic GTY(()) tree first_builtin_decl;
141132730Skanstatic GTY(()) tree last_builtin_decl;
14218334Speter
143132730Skan/* A DECL for the current file-scope context.  */
144132730Skan
145132730Skanstatic GTY(()) tree current_file_decl;
146132730Skan
14718334Speter/* Set to 0 at beginning of a function definition, set to 1 if
14818334Speter   a return statement that specifies a return value is seen.  */
14918334Speter
15018334Speterint current_function_returns_value;
15118334Speter
15218334Speter/* Set to 0 at beginning of a function definition, set to 1 if
15318334Speter   a return statement with no argument is seen.  */
15418334Speter
15518334Speterint current_function_returns_null;
15618334Speter
15796263Sobrien/* Set to 0 at beginning of a function definition, set to 1 if
15896263Sobrien   a call to a noreturn function is seen.  */
15996263Sobrien
16096263Sobrienint current_function_returns_abnormally;
16196263Sobrien
16218334Speter/* Set to nonzero by `grokdeclarator' for a function
16318334Speter   whose return type is defaulted, if warnings for this are desired.  */
16418334Speter
16518334Speterstatic int warn_about_return_type;
16618334Speter
16718334Speter/* Nonzero when starting a function declared `extern inline'.  */
16818334Speter
16918334Speterstatic int current_extern_inline;
17018334Speter
171132730Skan/* Each c_scope structure describes the complete contents of one scope.
172132730Skan   Three scopes are distinguished specially: the innermost or current
173132730Skan   scope, the innermost function scope, and the outermost or file scope.
17418334Speter
175132730Skan   Most declarations are recorded in the current scope.
17618334Speter
177132730Skan   All normal label declarations are recorded in the innermost
178132730Skan   function scope, as are bindings of undeclared identifiers to
179132730Skan   error_mark_node.  (GCC permits nested functions as an extension,
180132730Skan   hence the 'innermost' qualifier.)  Explicitly declared labels
181132730Skan   (using the __label__ extension) appear in the current scope.
18218334Speter
183132730Skan   Being in the global scope (current_scope == global_scope) causes
184132730Skan   special behavior in several places below.  Also, under some
185132730Skan   conditions the Objective-C front end records declarations in the
186132730Skan   global scope even though that isn't the current scope.
18718334Speter
188132730Skan   The order of the names, parms, and blocks lists matters, and they
189132730Skan   are frequently appended to.  To avoid having to walk all the way to
190132730Skan   the end of the list on each insertion, or reverse the lists later,
191132730Skan   we maintain a pointer to the last list entry for each of the lists.
19218334Speter
193132730Skan   The order of the tags, shadowed, and shadowed_tags
194132730Skan   lists does not matter, so we just prepend to these lists.  */
19518334Speter
196132730Skanstruct c_scope GTY(())
197132730Skan{
198132730Skan  /* The scope containing this one.  */
199132730Skan  struct c_scope *outer;
20018334Speter
201132730Skan  /* The next outermost function scope.  */
202132730Skan  struct c_scope *outer_function;
20318334Speter
204132730Skan  /* All variables, constants, functions, labels, and typedef names.  */
205132730Skan  tree names;
206132730Skan  tree names_last;
20718334Speter
208132730Skan  /* All parameter declarations.  Used only in the outermost scope of
209132730Skan     a function.  */
210132730Skan  tree parms;
211132730Skan  tree parms_last;
21218334Speter
213132730Skan  /* All structure, union, and enum type tags.  */
214132730Skan  tree tags;
21518334Speter
216132730Skan  /* For each scope, a list of shadowed outer-scope definitions
217132730Skan     to be restored when this scope is popped.
218132730Skan     Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
219132730Skan     whose TREE_VALUE is its old definition (a kind of ..._DECL node).  */
220132730Skan  tree shadowed;
22118334Speter
222132730Skan  /* For each scope, a list of shadowed outer-scope tag definitions
223132730Skan     to be restored when this scope is popped.
224132730Skan     Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
225132730Skan     whose TREE_VALUE is its old definition (a kind of ..._TYPE node).  */
226132730Skan  tree shadowed_tags;
22718334Speter
228132730Skan  /* For each scope (except the global one), a chain of BLOCK nodes
229132730Skan     for all the scopes that were entered and exited one level down.  */
230132730Skan  tree blocks;
231132730Skan  tree blocks_last;
23218334Speter
233132730Skan  /* True if we are currently filling this scope with parameter
234132730Skan     declarations.  */
235132730Skan  BOOL_BITFIELD parm_flag : 1;
23618334Speter
237132730Skan  /* True if we already complained about forward parameter decls
238132730Skan     in this scope.  This prevents double warnings on
239132730Skan     foo (int a; int b; ...)  */
240132730Skan  BOOL_BITFIELD warned_forward_parm_decls : 1;
24190075Sobrien
242132730Skan  /* True if this is the outermost block scope of a function body.
243132730Skan     This scope contains the parameters, the local variables declared
244132730Skan     in the outermost block, and all the labels (except those in
245132730Skan     nested functions, or declared at block scope with __label__).  */
246132730Skan  BOOL_BITFIELD function_body : 1;
24718334Speter
248132730Skan  /* True means make a BLOCK for this scope no matter what.  */
249132730Skan  BOOL_BITFIELD keep : 1;
250132730Skan};
25118334Speter
252132730Skan/* The scope currently in effect.  */
25318334Speter
254132730Skanstatic GTY(()) struct c_scope *current_scope;
25518334Speter
256132730Skan/* A chain of c_scope structures awaiting reuse.  */
25718334Speter
258132730Skanstatic GTY((deletable (""))) struct c_scope *scope_freelist;
25918334Speter
260132730Skan/* The innermost function scope.  Ordinary (not explicitly declared)
261132730Skan   labels, bindings to error_mark_node, and the lazily-created
262132730Skan   bindings of __func__ and its friends get this scope.  */
26318334Speter
264132730Skanstatic GTY(()) struct c_scope *current_function_scope;
26518334Speter
266132730Skan/* The outermost scope, corresponding to the C "file scope".  This is
267132730Skan   created when the compiler is started and exists through the entire run.  */
26818334Speter
269132730Skanstatic GTY(()) struct c_scope *global_scope;
27018334Speter
271132730Skan/* Append VAR to LIST in scope SCOPE.  */
272132730Skan#define SCOPE_LIST_APPEND(scope, list, decl) do {	\
273132730Skan  struct c_scope *s_ = (scope);				\
274132730Skan  tree d_ = (decl);					\
275132730Skan  if (s_->list##_last)					\
276132730Skan    TREE_CHAIN (s_->list##_last) = d_;			\
277132730Skan  else							\
278132730Skan    s_->list = d_;					\
279132730Skan  s_->list##_last = d_;					\
280132730Skan} while (0)
28118334Speter
282132730Skan/* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE.  */
283132730Skan#define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do {	\
284132730Skan  struct c_scope *t_ = (tscope);				\
285132730Skan  struct c_scope *f_ = (fscope);				\
286132730Skan  if (t_->to##_last)						\
287132730Skan    TREE_CHAIN (t_->to##_last) = f_->from;			\
288132730Skan  else								\
289132730Skan    t_->to = f_->from;						\
290132730Skan  t_->to##_last = f_->from##_last;				\
291132730Skan} while (0)
29290075Sobrien
293132730Skan/* True means unconditionally make a BLOCK for the next scope pushed.  */
29418334Speter
295132730Skanstatic bool keep_next_level_flag;
29618334Speter
297132730Skan/* True means the next call to pushlevel will be the outermost scope
298132730Skan   of a function body, so do not push a new scope, merely cease
299132730Skan   expecting parameter decls.  */
300132730Skan
301132730Skanstatic bool next_is_function_body;
302132730Skan
30318334Speter/* Functions called automatically at the beginning and end of execution.  */
30418334Speter
30518334Spetertree static_ctors, static_dtors;
30618334Speter
30718334Speter/* Forward declarations.  */
30818334Speter
309132730Skanstatic struct c_scope *make_scope (void);
310132730Skanstatic void pop_scope (void);
311132730Skanstatic tree make_label (tree, location_t);
312132730Skanstatic void bind_label (tree, tree, struct c_scope *);
313132730Skanstatic void implicit_decl_warning (tree);
314132730Skanstatic tree lookup_tag (enum tree_code, tree, int);
315132730Skanstatic tree lookup_name_current_level (tree);
316132730Skanstatic tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
317132730Skanstatic tree grokparms (tree, int);
318132730Skanstatic void layout_array_type (tree);
319132730Skanstatic void store_parm_decls_newstyle (void);
320132730Skanstatic void store_parm_decls_oldstyle (void);
321132730Skanstatic tree c_make_fname_decl (tree, int);
322132730Skanstatic void c_expand_body_1 (tree, int);
323132730Skanstatic tree any_external_decl (tree);
324132730Skanstatic void record_external_decl (tree);
325132730Skanstatic void warn_if_shadowing (tree, tree);
326132730Skanstatic void check_bitfield_type_and_width (tree *, tree *, const char *);
327132730Skanstatic void clone_underlying_type (tree);
328132730Skanstatic bool flexible_array_type_p (tree);
329132730Skanstatic hashval_t link_hash_hash	(const void *);
330132730Skanstatic int link_hash_eq (const void *, const void *);
33118334Speter
33290075Sobrien/* States indicating how grokdeclarator() should handle declspecs marked
33390075Sobrien   with __attribute__((deprecated)).  An object declared as
33490075Sobrien   __attribute__((deprecated)) suppresses warnings of uses of other
33590075Sobrien   deprecated items.  */
336132730Skan
33790075Sobrienenum deprecated_states {
33890075Sobrien  DEPRECATED_NORMAL,
33990075Sobrien  DEPRECATED_SUPPRESS
34090075Sobrien};
34190075Sobrien
34290075Sobrienstatic enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
34390075Sobrien
34418334Spetervoid
345132730Skanc_print_identifier (FILE *file, tree node, int indent)
34618334Speter{
347132730Skan  print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
348132730Skan  print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
34918334Speter  print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
35090075Sobrien  if (C_IS_RESERVED_WORD (node))
35190075Sobrien    {
35290075Sobrien      tree rid = ridpointers[C_RID_CODE (node)];
35390075Sobrien      indent_to (file, indent + 4);
354132730Skan      fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
355132730Skan	       (void *) rid, IDENTIFIER_POINTER (rid));
35690075Sobrien    }
35718334Speter}
35818334Speter
35918334Speter/* Hook called at end of compilation to assume 1 elt
360132730Skan   for a file-scope tentative array defn that wasn't complete before.  */
36190075Sobrien
36218334Spetervoid
363132730Skanc_finish_incomplete_decl (tree decl)
36418334Speter{
36550397Sobrien  if (TREE_CODE (decl) == VAR_DECL)
36618334Speter    {
36718334Speter      tree type = TREE_TYPE (decl);
36850397Sobrien      if (type != error_mark_node
36950397Sobrien	  && TREE_CODE (type) == ARRAY_TYPE
37090075Sobrien	  && ! DECL_EXTERNAL (decl)
37150397Sobrien	  && TYPE_DOMAIN (type) == 0)
37218334Speter	{
373132730Skan	  warning ("%Jarray '%D' assumed to have one element", decl, decl);
37450397Sobrien
37518334Speter	  complete_array_type (type, NULL_TREE, 1);
37618334Speter
37718334Speter	  layout_decl (decl, 0);
37818334Speter	}
37918334Speter    }
38018334Speter}
38118334Speter
382132730Skan/* Reuse or create a struct for this scope.  */
38318334Speter
384132730Skanstatic struct c_scope *
385132730Skanmake_scope (void)
38618334Speter{
387132730Skan  struct c_scope *result;
388132730Skan  if (scope_freelist)
389117421Skan    {
390132730Skan      result = scope_freelist;
391132730Skan      scope_freelist = result->outer;
392117421Skan    }
393117421Skan  else
394132730Skan    result = ggc_alloc_cleared (sizeof (struct c_scope));
395132730Skan
396132730Skan  return result;
39718334Speter}
39818334Speter
399132730Skan/* Remove the topmost scope from the stack and add it to the
400132730Skan   free list, updating current_function_scope if necessary.  */
401117421Skan
402117421Skanstatic void
403132730Skanpop_scope (void)
404117421Skan{
405132730Skan  struct c_scope *scope = current_scope;
406132730Skan
407132730Skan  current_scope = scope->outer;
408132730Skan  if (scope->function_body)
409132730Skan    current_function_scope = scope->outer_function;
410132730Skan
411132730Skan  memset (scope, 0, sizeof (struct c_scope));
412132730Skan  scope->outer = scope_freelist;
413132730Skan  scope_freelist = scope;
414117421Skan}
415117421Skan
416132730Skan/* The Objective-C front-end often needs to determine the current scope.  */
41718334Speter
418132730Skanvoid *
419132730Skanget_current_scope (void)
42018334Speter{
421132730Skan  return current_scope;
42218334Speter}
42318334Speter
424132730Skan/* The following function is used only by Objective-C.  It needs to live here
425132730Skan   because it accesses the innards of c_scope.  */
426132730Skan
42718334Spetervoid
428132730Skanobjc_mark_locals_volatile (void *enclosing_blk)
42918334Speter{
430132730Skan  struct c_scope *scope;
431132730Skan
432132730Skan  for (scope = current_scope;
433132730Skan       scope && scope != enclosing_blk;
434132730Skan       scope = scope->outer)
435132730Skan    {
436132730Skan      tree decl;
437132730Skan
438132730Skan      for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
439132730Skan	{
440132730Skan	  DECL_REGISTER (decl) = 0;
441132730Skan	  TREE_THIS_VOLATILE (decl) = 1;
442132730Skan	}
443132730Skan      /* Do not climb up past the current function.  */
444132730Skan      if (scope->function_body)
445132730Skan	break;
446132730Skan    }
44718334Speter}
44818334Speter
449132730Skan/* Nonzero if we are currently in the global scope.  */
45018334Speter
45118334Speterint
452132730Skanglobal_bindings_p (void)
45318334Speter{
454132730Skan  return current_scope == global_scope;
45518334Speter}
45618334Speter
457132730Skanvoid
458132730Skankeep_next_level (void)
459132730Skan{
460132730Skan  keep_next_level_flag = true;
461132730Skan}
46218334Speter
463132730Skan/* Identify this scope as currently being filled with parameters.  */
464132730Skan
46518334Spetervoid
466132730Skandeclare_parm_level (void)
46718334Speter{
468132730Skan  current_scope->parm_flag = true;
46918334Speter}
47018334Speter
47118334Speter/* Nonzero if currently making parm declarations.  */
47218334Speter
47318334Speterint
474132730Skanin_parm_level_p (void)
47518334Speter{
476132730Skan  return current_scope->parm_flag;
47718334Speter}
47818334Speter
479132730Skan/* Enter a new scope.  The dummy parameter is for signature
480132730Skan   compatibility with lang_hooks.decls.pushlevel.  */
48118334Speter
48218334Spetervoid
483132730Skanpushlevel (int dummy ATTRIBUTE_UNUSED)
48418334Speter{
485132730Skan  if (next_is_function_body)
486132730Skan    {
487132730Skan      /* This is the transition from the parameters to the top level
488132730Skan	 of the function body.  These are the same scope
489132730Skan	 (C99 6.2.1p4,6) so we do not push another scope structure.
490132730Skan	 next_is_function_body is set only by store_parm_decls, which
491132730Skan	 in turn is called when and only when we are about to
492132730Skan	 encounter the opening curly brace for the function body.
49318334Speter
494132730Skan	 The outermost block of a function always gets a BLOCK node,
495132730Skan	 because the debugging output routines expect that each
496132730Skan	 function has at least one BLOCK.  */
497132730Skan      current_scope->parm_flag         = false;
498132730Skan      current_scope->function_body     = true;
499132730Skan      current_scope->keep              = true;
500132730Skan      current_scope->outer_function    = current_function_scope;
501132730Skan      current_function_scope           = current_scope;
50218334Speter
503132730Skan      keep_next_level_flag = false;
504132730Skan      next_is_function_body = false;
505132730Skan    }
506132730Skan  else
50718334Speter    {
508132730Skan      struct c_scope *scope = make_scope ();
509132730Skan
510132730Skan      scope->keep          = keep_next_level_flag;
511132730Skan      scope->outer         = current_scope;
512132730Skan      current_scope        = scope;
513132730Skan      keep_next_level_flag = false;
51418334Speter    }
51518334Speter}
51618334Speter
517132730Skan/* Exit a scope.  Restore the state of the identifier-decl mappings
518132730Skan   that were in effect when this scope was entered.
51950397Sobrien
520132730Skan   If KEEP is KEEP_YES (1), this scope had explicit declarations, so
521132730Skan   create a BLOCK node to record its declarations and subblocks for
522132730Skan   debugging output.  If KEEP is KEEP_MAYBE, do so only if the names
523132730Skan   or tags lists are nonempty.
52450397Sobrien
525132730Skan   The second parameter is ignored; it is present only for
526132730Skan   signature compatibility with lang_hooks.decls.poplevel.
52750397Sobrien
52818334Speter   If FUNCTIONBODY is nonzero, this level is the body of a function,
529132730Skan   even if current_scope->function_body is not set.  This is used
530132730Skan   by language-independent code that generates synthetic functions,
531132730Skan   and cannot set current_scope->function_body.
53218334Speter
533132730Skan   FIXME: Eliminate the need for all arguments.  */
53418334Speter
53518334Spetertree
536132730Skanpoplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
53718334Speter{
538132730Skan  struct c_scope *scope = current_scope;
539132730Skan  tree block;
54018334Speter  tree decl;
541132730Skan  tree p;
54218334Speter
543132730Skan  /* The following line does not use |= due to a bug in HP's C compiler.  */
544132730Skan  scope->function_body = scope->function_body | functionbody;
54518334Speter
546132730Skan  if (keep == KEEP_MAYBE)
547132730Skan    keep = (scope->names || scope->tags);
54818334Speter
549132730Skan  keep |= scope->keep;
550132730Skan  keep |= scope->function_body;
55118334Speter
552132730Skan  /* If appropriate, create a BLOCK to record the decls for the life
553132730Skan     of this function.  */
55418334Speter  block = 0;
555132730Skan  if (keep)
55618334Speter    {
557132730Skan      block = make_node (BLOCK);
558132730Skan      BLOCK_VARS (block) = scope->names;
559132730Skan      BLOCK_SUBBLOCKS (block) = scope->blocks;
560132730Skan      TREE_USED (block) = 1;
56118334Speter    }
56218334Speter
56318334Speter  /* In each subblock, record that this is its superior.  */
564132730Skan  for (p = scope->blocks; p; p = TREE_CHAIN (p))
565132730Skan    BLOCK_SUPERCONTEXT (p) = block;
56618334Speter
567132730Skan  /* Clear out the variable bindings in this scope.
56818334Speter
569132730Skan     Propagate TREE_ADDRESSABLE from nested functions to their
570132730Skan     containing functions.
57118334Speter
572132730Skan     Issue warnings for unused variables and labels, and errors for
573132730Skan     undefined labels, if there are any.  */
574132730Skan
575132730Skan  for (p = scope->names; p; p = TREE_CHAIN (p))
57618334Speter    {
577132730Skan      switch (TREE_CODE (p))
57818334Speter	{
579132730Skan	case LABEL_DECL:
580132730Skan	  if (TREE_USED (p) && !DECL_INITIAL (p))
58118334Speter	    {
582132730Skan	      error ("%Jlabel `%D' used but not defined", p, p);
583132730Skan	      DECL_INITIAL (p) = error_mark_node;
58418334Speter	    }
585132730Skan	  else if (!TREE_USED (p) && warn_unused_label)
586132730Skan	    {
587132730Skan	      if (DECL_INITIAL (p))
588132730Skan		warning ("%Jlabel `%D' defined but not used", p, p);
589132730Skan	      else
590132730Skan		warning ("%Jlabel `%D' declared but not defined", p, p);
591132730Skan	    }
59218334Speter
593132730Skan	  IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
594132730Skan	  break;
59518334Speter
596132730Skan	case FUNCTION_DECL:
597132730Skan	  if (! TREE_ASM_WRITTEN (p)
598132730Skan	      && DECL_INITIAL (p) != 0
599132730Skan	      && TREE_ADDRESSABLE (p)
600132730Skan	      && DECL_ABSTRACT_ORIGIN (p) != 0
601132730Skan	      && DECL_ABSTRACT_ORIGIN (p) != p)
602132730Skan	    TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
603132730Skan	  goto normal;
60418334Speter
605132730Skan	case VAR_DECL:
606132730Skan	  /* Keep this in sync with stmt.c:warn_about_unused_variables.
607132730Skan	     No warnings when the global scope is popped because the
608132730Skan	     global scope isn't popped for the last translation unit,
609132730Skan	     so the warnings are done in c_write_global_declaration.  */
610132730Skan	  if (warn_unused_variable && scope != global_scope
611132730Skan	      && !TREE_USED (p)
612132730Skan	      && !DECL_IN_SYSTEM_HEADER (p)
613132730Skan	      && DECL_NAME (p)
614132730Skan	      && !DECL_ARTIFICIAL (p))
615132730Skan	    warning ("%Junused variable `%D'", p, p);
616132730Skan	  /* fall through */
61718334Speter
618132730Skan	default:
619132730Skan	normal:
620132730Skan	  if (DECL_NAME (p))
621132730Skan	    {
622132730Skan	      if (DECL_EXTERNAL (p) && scope != global_scope)
623132730Skan		/* External decls stay in the symbol-value slot but are
624132730Skan		   inaccessible.  */
625132730Skan		C_DECL_INVISIBLE (p) = 1;
626132730Skan	      else
627132730Skan		IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
628132730Skan	    }
629132730Skan	  break;
630132730Skan	}
631132730Skan    }
63250397Sobrien
633132730Skan  /* Clear out the parameter bindings in this scope, if any.
634132730Skan     Unused-parameter warnings are handled by function.c.  */
635132730Skan  for (p = scope->parms; p; p = TREE_CHAIN (p))
636132730Skan    if (DECL_NAME (p))
637132730Skan      IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
63818334Speter
639132730Skan  /* Clear out the tag-meanings declared in this scope.
64018334Speter
641132730Skan     Set the TYPE_CONTEXTs for all of the tagged types belonging to
642132730Skan     this scope so that they point to the appropriate construct, i.e.
643132730Skan     either to the current FUNCTION_DECL node, or else to the BLOCK
644132730Skan     node we just constructed.
64518334Speter
646132730Skan     Note that for tagged types whose scope is just the formal
647132730Skan     parameter list for some function type specification, we can't
648132730Skan     properly set their TYPE_CONTEXTs here, because we don't have a
649132730Skan     pointer to the appropriate FUNCTION_TYPE node readily available
650132730Skan     to us.  For those cases, the TYPE_CONTEXTs of the relevant tagged
651132730Skan     type nodes get set in `grokdeclarator' as soon as we have created
652132730Skan     the FUNCTION_TYPE node which will represent the "scope" for these
653132730Skan     "parameter list local" tagged types.  */
65418334Speter
655132730Skan  decl = scope->function_body ? current_function_decl : block;
656132730Skan  for (p = scope->tags; p; p = TREE_CHAIN (p))
657132730Skan    {
658132730Skan      if (TREE_PURPOSE (p))
659132730Skan	IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
660132730Skan      if (decl)
661132730Skan	TYPE_CONTEXT (TREE_VALUE (p)) = decl;
66218334Speter    }
66318334Speter
664132730Skan  /* Restore all name- and label-meanings from outer scopes that were
665132730Skan     shadowed by this scope.  */
666132730Skan  for (p = scope->shadowed; p; p = TREE_CHAIN (p))
667132730Skan    if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
668132730Skan      IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
669132730Skan    else
670132730Skan      IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
67118334Speter
672132730Skan  /* Restore all tag-meanings from outer scopes that were shadowed by
673132730Skan     this scope.  */
674132730Skan  for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
675132730Skan    IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
67618334Speter
67718334Speter  /* Dispose of the block that we just made inside some higher level.  */
678132730Skan  if (scope->function_body && current_function_decl)
67918334Speter    DECL_INITIAL (current_function_decl) = block;
680132730Skan  else if (scope->outer)
68118334Speter    {
682132730Skan      if (block)
683132730Skan	SCOPE_LIST_APPEND (scope->outer, blocks, block);
684132730Skan      /* If we did not make a block for the scope just exited, any
685132730Skan	 blocks made for inner scopes must be carried forward so they
686132730Skan	 will later become subblocks of something else.  */
687132730Skan      else if (scope->blocks)
688132730Skan	SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
68918334Speter    }
69018334Speter
691132730Skan  /* Pop the current scope, and free the structure for reuse.  */
692132730Skan  pop_scope ();
69318334Speter
69418334Speter  return block;
69518334Speter}
69618334Speter
697132730Skan/* Insert BLOCK at the end of the list of subblocks of the current
698132730Skan   scope.  This is used when a BIND_EXPR is expanded, to handle the
699132730Skan   BLOCK node inside the BIND_EXPR.  */
70018334Speter
70118334Spetervoid
702132730Skaninsert_block (tree block)
70318334Speter{
70418334Speter  TREE_USED (block) = 1;
705132730Skan  SCOPE_LIST_APPEND (current_scope, blocks, block);
70618334Speter}
70718334Speter
708132730Skan/* Set the BLOCK node for the innermost scope (the one we are
709132730Skan   currently in).  The RTL expansion machinery requires us to provide
710132730Skan   this hook, but it is not useful in function-at-a-time mode.  */
71118334Speter
71218334Spetervoid
713132730Skanset_block (tree block ATTRIBUTE_UNUSED)
71418334Speter{
71518334Speter}
71618334Speter
71718334Speter/* Push a definition or a declaration of struct, union or enum tag "name".
71818334Speter   "type" should be the type node.
71918334Speter   We assume that the tag "name" is not already defined.
72018334Speter
72118334Speter   Note that the definition may really be just a forward reference.
72218334Speter   In that case, the TYPE_SIZE will be zero.  */
72318334Speter
72418334Spetervoid
725132730Skanpushtag (tree name, tree type)
72618334Speter{
727132730Skan  struct c_scope *b = current_scope;
72818334Speter
729132730Skan  /* Record the identifier as the type's name if it has none.  */
73018334Speter  if (name)
73118334Speter    {
73218334Speter      if (TYPE_NAME (type) == 0)
73318334Speter	TYPE_NAME (type) = name;
734132730Skan
735132730Skan      if (IDENTIFIER_TAG_VALUE (name))
736132730Skan	b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
737132730Skan				      b->shadowed_tags);
738132730Skan      IDENTIFIER_TAG_VALUE (name) = type;
73918334Speter    }
74018334Speter
74190075Sobrien  b->tags = tree_cons (name, type, b->tags);
74218334Speter
74318334Speter  /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
744132730Skan     tagged type we just added to the current scope.  This fake
74518334Speter     NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
74618334Speter     to output a representation of a tagged type, and it also gives
74718334Speter     us a convenient place to record the "scope start" address for the
74818334Speter     tagged type.  */
74918334Speter
75018334Speter  TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
75150397Sobrien
75250397Sobrien  /* An approximation for now, so we can tell this is a function-scope tag.
75350397Sobrien     This will be updated in poplevel.  */
75450397Sobrien  TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
75518334Speter}
75618334Speter
757132730Skan/* Subroutine of compare_decls.  Allow harmless mismatches in return
758132730Skan   and argument types provided that the type modes match.  This function
759132730Skan   return a unified type given a suitable match, and 0 otherwise.  */
76018334Speter
761132730Skanstatic tree
762132730Skanmatch_builtin_function_types (tree newtype, tree oldtype)
763132730Skan{
764132730Skan  tree newrettype, oldrettype;
765132730Skan  tree newargs, oldargs;
766132730Skan  tree trytype, tryargs;
76718334Speter
768132730Skan  /* Accept the return type of the new declaration if same modes.  */
769132730Skan  oldrettype = TREE_TYPE (oldtype);
770132730Skan  newrettype = TREE_TYPE (newtype);
77150397Sobrien
772132730Skan  if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
773132730Skan    return 0;
774132730Skan
775132730Skan  oldargs = TYPE_ARG_TYPES (oldtype);
776132730Skan  newargs = TYPE_ARG_TYPES (newtype);
777132730Skan  tryargs = newargs;
778132730Skan
779132730Skan  while (oldargs || newargs)
780132730Skan    {
781132730Skan      if (! oldargs
782132730Skan	  || ! newargs
783132730Skan	  || ! TREE_VALUE (oldargs)
784132730Skan	  || ! TREE_VALUE (newargs)
785132730Skan	  || TYPE_MODE (TREE_VALUE (oldargs))
786132730Skan	     != TYPE_MODE (TREE_VALUE (newargs)))
787132730Skan	return 0;
788132730Skan
789132730Skan      oldargs = TREE_CHAIN (oldargs);
790132730Skan      newargs = TREE_CHAIN (newargs);
791132730Skan    }
792132730Skan
793132730Skan  trytype = build_function_type (newrettype, tryargs);
794132730Skan  return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
795132730Skan}
796132730Skan
797132730Skan/* Subroutine of diagnose_mismathed_decls.  Check for function type
798132730Skan   mismatch involving an empty arglist vs a nonempty one and give clearer
799132730Skan   diagnostics. */
800132730Skanstatic void
801132730Skandiagnose_arglist_conflict (tree newdecl, tree olddecl,
802132730Skan			   tree newtype, tree oldtype)
80318334Speter{
804132730Skan  tree t;
80518334Speter
806132730Skan  if (TREE_CODE (olddecl) != FUNCTION_DECL
807132730Skan      || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
808132730Skan      || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
809132730Skan	   ||
810132730Skan	   (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
811132730Skan    return;
812132730Skan
813132730Skan  t = TYPE_ARG_TYPES (oldtype);
814132730Skan  if (t == 0)
815132730Skan    t = TYPE_ARG_TYPES (newtype);
816132730Skan  for (; t; t = TREE_CHAIN (t))
81790075Sobrien    {
818132730Skan      tree type = TREE_VALUE (t);
819132730Skan
820132730Skan      if (TREE_CHAIN (t) == 0
821132730Skan	  && TYPE_MAIN_VARIANT (type) != void_type_node)
82290075Sobrien	{
823132730Skan	  inform ("a parameter list with an ellipsis can't match "
824132730Skan		  "an empty parameter name list declaration");
825132730Skan	  break;
82690075Sobrien	}
82718334Speter
828132730Skan      if (c_type_promotes_to (type) != type)
829132730Skan	{
830132730Skan	  inform ("an argument type that has a default promotion can't match "
831132730Skan		  "an empty parameter name list declaration");
832132730Skan	  break;
833132730Skan	}
83490075Sobrien    }
835132730Skan}
83690075Sobrien
837132730Skan/* Another subroutine of diagnose_mismatched_decls.  OLDDECL is an
838132730Skan   old-style function definition, NEWDECL is a prototype declaration.
839132730Skan   Diagnose inconsistencies in the argument list.  Returns TRUE if
840132730Skan   the prototype is compatible, FALSE if not.  */
841132730Skanstatic bool
842132730Skanvalidate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
843132730Skan{
844132730Skan  tree newargs, oldargs;
845132730Skan  int i;
84618334Speter
847132730Skan  /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context.  */
848132730Skan#define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
849132730Skan
850132730Skan  oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
851132730Skan  newargs = TYPE_ARG_TYPES (newtype);
852132730Skan  i = 1;
853132730Skan
854132730Skan  for (;;)
855132730Skan    {
856132730Skan      tree oldargtype = TREE_VALUE (oldargs);
857132730Skan      tree newargtype = TREE_VALUE (newargs);
858132730Skan
859132730Skan      if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
860132730Skan	break;
861132730Skan
862132730Skan      /* Reaching the end of just one list means the two decls don't
863132730Skan	 agree on the number of arguments.  */
864132730Skan      if (END_OF_ARGLIST (oldargtype))
865132730Skan	{
866132730Skan	  error ("%Jprototype for '%D' declares more arguments "
867132730Skan		 "than previous old-style definition", newdecl, newdecl);
868132730Skan	  return false;
869132730Skan	}
870132730Skan      else if (END_OF_ARGLIST (newargtype))
871132730Skan	{
872132730Skan	  error ("%Jprototype for '%D' declares fewer arguments "
873132730Skan		 "than previous old-style definition", newdecl, newdecl);
874132730Skan	  return false;
875132730Skan	}
876132730Skan
877132730Skan      /* Type for passing arg must be consistent with that declared
878132730Skan	 for the arg.  */
879132730Skan      else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
880132730Skan	{
881132730Skan	  error ("%Jprototype for '%D' declares arg %d with incompatible type",
882132730Skan		 newdecl, newdecl, i);
883132730Skan	  return false;
884132730Skan	}
885132730Skan
886132730Skan      oldargs = TREE_CHAIN (oldargs);
887132730Skan      newargs = TREE_CHAIN (newargs);
888132730Skan      i++;
889132730Skan    }
890132730Skan
891132730Skan  /* If we get here, no errors were found, but do issue a warning
892132730Skan     for this poor-style construct.  */
893132730Skan  warning ("%Jprototype for '%D' follows non-prototype definition",
894132730Skan	   newdecl, newdecl);
895132730Skan  return true;
896132730Skan#undef END_OF_ARGLIST
897132730Skan}
898132730Skan
899132730Skan/* Subroutine of diagnose_mismatched_decls.  Report the location of DECL,
900132730Skan   first in a pair of mismatched declarations, using the diagnostic
901132730Skan   function DIAG.  */
902132730Skanstatic void
903132730Skanlocate_old_decl (tree decl, void (*diag)(const char *, ...))
904132730Skan{
905132730Skan  if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
906132730Skan    ;
907132730Skan  else if (DECL_INITIAL (decl))
908132730Skan    diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
909132730Skan  else if (C_DECL_IMPLICIT (decl))
910132730Skan    diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
911132730Skan  else
912132730Skan    diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
913132730Skan}
914132730Skan
915132730Skan/* Subroutine of duplicate_decls.  Compare NEWDECL to OLDDECL.
916132730Skan   Returns true if the caller should proceed to merge the two, false
917132730Skan   if OLDDECL should simply be discarded.  As a side effect, issues
918132730Skan   all necessary diagnostics for invalid or poor-style combinations.
919132730Skan   If it returns true, writes the types of NEWDECL and OLDDECL to
920132730Skan   *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
921132730Skan   TREE_TYPE (NEWDECL, OLDDECL) respectively.  */
922132730Skan
923132730Skanstatic bool
924132730Skandiagnose_mismatched_decls (tree newdecl, tree olddecl,
925132730Skan			   tree *newtypep, tree *oldtypep)
926132730Skan{
927132730Skan  tree newtype, oldtype;
928132730Skan  bool pedwarned = false;
929132730Skan  bool warned = false;
930132730Skan
931132730Skan  /* If we have error_mark_node for either decl or type, just discard
932132730Skan     the previous decl - we're in an error cascade already.  */
933132730Skan  if (olddecl == error_mark_node || newdecl == error_mark_node)
934132730Skan    return false;
935132730Skan  *oldtypep = oldtype = TREE_TYPE (olddecl);
936132730Skan  *newtypep = newtype = TREE_TYPE (newdecl);
937132730Skan  if (oldtype == error_mark_node || newtype == error_mark_node)
938132730Skan    return false;
939132730Skan
940132730Skan  /* Two different categories of symbol altogether.  This is an error
941132730Skan     unless OLDDECL is a builtin.  OLDDECL will be discarded in any case.  */
94218334Speter  if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
94318334Speter    {
944132730Skan      if (TREE_CODE (olddecl) != FUNCTION_DECL
945132730Skan          || !DECL_BUILT_IN (olddecl) || !C_DECL_INVISIBLE (olddecl))
946132730Skan	{
947132730Skan	  error ("%J'%D' redeclared as different kind of symbol",
948132730Skan		 newdecl, newdecl);
949132730Skan	  locate_old_decl (olddecl, error);
950132730Skan	}
951132730Skan      else if (TREE_PUBLIC (newdecl))
952132730Skan	warning ("%Jbuilt-in function '%D' declared as non-function",
953132730Skan		 newdecl, newdecl);
954132730Skan      else if (warn_shadow)
955132730Skan	warning ("%Jshadowing built-in function '%D'",
956132730Skan		 newdecl, newdecl);
957132730Skan      return false;
958132730Skan    }
959132730Skan
960132730Skan  if (!comptypes (oldtype, newtype, COMPARE_STRICT))
961132730Skan    {
96218334Speter      if (TREE_CODE (olddecl) == FUNCTION_DECL
963132730Skan	  && DECL_BUILT_IN (olddecl) && C_DECL_INVISIBLE (olddecl))
96418334Speter	{
965132730Skan	  /* Accept harmless mismatch in function types.
966132730Skan	     This is for the ffs and fprintf builtins.  */
967132730Skan	  tree trytype = match_builtin_function_types (newtype, oldtype);
968132730Skan
969132730Skan	  if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
970132730Skan	    *oldtypep = oldtype = trytype;
971132730Skan	  else
97218334Speter	    {
973132730Skan	      /* If types don't match for a built-in, throw away the
974132730Skan		 built-in.  No point in calling locate_old_decl here, it
975132730Skan		 won't print anything. */
976132730Skan	      warning ("%Jconflicting types for built-in function '%D'",
977132730Skan		       newdecl, newdecl);
978132730Skan	      return false;
97918334Speter	    }
98018334Speter	}
981132730Skan      else if (TREE_CODE (olddecl) == FUNCTION_DECL
982132730Skan	       && DECL_SOURCE_LINE (olddecl) == 0)
983132730Skan	{
984132730Skan	  /* A conflicting function declaration for a predeclared
985132730Skan	     function that isn't actually built in.  Objective C uses
986132730Skan	     these.  The new declaration silently overrides everything
987132730Skan	     but the volatility (i.e. noreturn) indication.  See also
988132730Skan	     below.  FIXME: Make Objective C use normal builtins.  */
989132730Skan	  TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
990132730Skan	  return false;
991132730Skan	}
992132730Skan      /* Permit void foo (...) to match int foo (...) if the latter is
993132730Skan	 the definition and implicit int was used.  See
994132730Skan	 c-torture/compile/920625-2.c.  */
995132730Skan      else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
996132730Skan	       && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
997132730Skan	       && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
998132730Skan	       && C_FUNCTION_IMPLICIT_INT (newdecl))
999132730Skan	{
1000132730Skan	  pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1001132730Skan	  /* Make sure we keep void as the return type.  */
1002132730Skan	  TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1003132730Skan	  C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1004132730Skan	  pedwarned = true;
1005132730Skan	}
100618334Speter      else
100718334Speter	{
1008132730Skan	  error ("%Jconflicting types for '%D'", newdecl, newdecl);
1009132730Skan	  diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1010132730Skan	  locate_old_decl (olddecl, error);
1011132730Skan	  return false;
101218334Speter	}
1013132730Skan    }
101418334Speter
1015132730Skan  /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1016132730Skan     but silently ignore the redeclaration if either is in a system
1017132730Skan     header.  (Conflicting redeclarations were handled above.)  */
1018132730Skan  if (TREE_CODE (newdecl) == TYPE_DECL)
1019132730Skan    {
1020132730Skan      if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1021132730Skan	return true;  /* allow OLDDECL to continue in use */
1022132730Skan
1023132730Skan      error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1024132730Skan      locate_old_decl (olddecl, error);
1025132730Skan      return false;
102618334Speter    }
102718334Speter
1028132730Skan  /* Function declarations can either be 'static' or 'extern' (no
1029132730Skan     qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1030132730Skan     can never conflict with each other on account of linkage (6.2.2p4).
1031132730Skan     Multiple definitions are not allowed (6.9p3,5) but GCC permits
1032132730Skan     two definitions if one is 'extern inline' and one is not.  The non-
1033132730Skan     extern-inline definition supersedes the extern-inline definition.  */
1034132730Skan  else if (TREE_CODE (newdecl) == FUNCTION_DECL)
103518334Speter    {
1036132730Skan      /* If you declare a built-in function name as static, or
1037132730Skan	 define the built-in with an old-style definition (so we
1038132730Skan	 can't validate the argument list) the built-in definition is
1039132730Skan	 overridden, but optionally warn this was a bad choice of name.  */
1040132730Skan      if (DECL_BUILT_IN (olddecl)
1041132730Skan	  && C_DECL_INVISIBLE (olddecl)
1042132730Skan	  && (!TREE_PUBLIC (newdecl)
1043132730Skan	      || (DECL_INITIAL (newdecl)
1044132730Skan		  && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
104518334Speter	{
104618334Speter	  if (warn_shadow)
1047132730Skan	    warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
104818334Speter	  /* Discard the old built-in function.  */
1049132730Skan	  return false;
105018334Speter	}
1051132730Skan
1052132730Skan      if (DECL_INITIAL (newdecl))
105318334Speter	{
1054132730Skan	  if (DECL_INITIAL (olddecl)
1055132730Skan	      && !(DECL_DECLARED_INLINE_P (olddecl)
1056132730Skan		   && DECL_EXTERNAL (olddecl)
1057132730Skan		   && !(DECL_DECLARED_INLINE_P (newdecl)
1058132730Skan			&& DECL_EXTERNAL (newdecl))))
105918334Speter	    {
1060132730Skan	      error ("%Jredefinition of '%D'", newdecl, newdecl);
1061132730Skan	      locate_old_decl (olddecl, error);
1062132730Skan	      return false;
106318334Speter	    }
1064132730Skan	}
1065132730Skan      /* If we have a prototype after an old-style function definition,
1066132730Skan	 the argument types must be checked specially.  */
1067132730Skan      else if (DECL_INITIAL (olddecl)
1068132730Skan	       && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1069132730Skan	       && TYPE_ACTUAL_ARG_TYPES (oldtype)
1070132730Skan	       && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1071132730Skan	{
1072132730Skan	  locate_old_decl (olddecl, error);
1073132730Skan	  return false;
1074132730Skan	}
1075132730Skan      /* Mismatched non-static and static is considered poor style.
1076132730Skan         We only diagnose static then non-static if -Wtraditional,
1077132730Skan	 because it is the most convenient way to get some effects
1078132730Skan	 (see e.g.  what unwind-dw2-fde-glibc.c does to the definition
1079132730Skan	 of _Unwind_Find_FDE in unwind-dw2-fde.c).  Revisit?  */
1080132730Skan      if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1081132730Skan	{
1082132730Skan	  /* A static function declaration for a predeclared function
1083132730Skan	     that isn't actually built in, silently overrides the
1084132730Skan	     default.  Objective C uses these.  See also above.
1085132730Skan	     FIXME: Make Objective C use normal builtins.  */
1086132730Skan	  if (TREE_CODE (olddecl) == FUNCTION_DECL
1087132730Skan	      && DECL_SOURCE_LINE (olddecl) == 0)
1088132730Skan	    return false;
1089132730Skan	  else
109018334Speter	    {
1091132730Skan	      warning ("%Jstatic declaration of '%D' follows "
1092132730Skan		       "non-static declaration", newdecl, newdecl);
1093132730Skan	      warned = true;
109418334Speter	    }
109518334Speter	}
1096132730Skan      else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1097132730Skan	       && warn_traditional)
109896263Sobrien	{
1099132730Skan	  warning ("%Jnon-static declaration of '%D' follows "
1100132730Skan		   "static declaration", newdecl, newdecl);
1101132730Skan	  warned = true;
110296263Sobrien	}
110318334Speter    }
1104132730Skan  else if (TREE_CODE (newdecl) == VAR_DECL)
110518334Speter    {
1106132730Skan      /* Only variables can be thread-local, and all declarations must
1107132730Skan	 agree on this property.  */
1108132730Skan      if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
110918334Speter	{
1110132730Skan	  if (DECL_THREAD_LOCAL (newdecl))
1111132730Skan	    error ("%Jthread-local declaration of '%D' follows "
1112132730Skan		   "non-thread-local declaration", newdecl, newdecl);
1113132730Skan	  else
1114132730Skan	    error ("%Jnon-thread-local declaration of '%D' follows "
1115132730Skan		   "thread-local declaration", newdecl, newdecl);
1116132730Skan
1117132730Skan	  locate_old_decl (olddecl, error);
1118132730Skan	  return false;
111918334Speter	}
1120132730Skan
1121132730Skan      /* Multiple initialized definitions are not allowed (6.9p3,5).  */
1122132730Skan      if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
112318334Speter	{
1124132730Skan	  error ("%Jredefinition of '%D'", newdecl, newdecl);
1125132730Skan	  locate_old_decl (olddecl, error);
1126132730Skan	  return false;
112718334Speter	}
112818334Speter
1129132730Skan      /* Objects declared at file scope: if at least one is 'extern',
1130132730Skan	 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7).  */
1131132730Skan      if (DECL_FILE_SCOPE_P (newdecl))
113218334Speter	{
1133132730Skan	  if (!DECL_EXTERNAL (newdecl)
1134132730Skan	      && !DECL_EXTERNAL (olddecl)
1135132730Skan	      && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
113618334Speter	    {
1137132730Skan	      if (TREE_PUBLIC (newdecl))
1138132730Skan		error ("%Jnon-static declaration of '%D' follows "
1139132730Skan		       "static declaration", newdecl, newdecl);
1140132730Skan	      else
1141132730Skan		error ("%Jstatic declaration of '%D' follows "
1142132730Skan		       "non-static declaration", newdecl, newdecl);
114318334Speter
1144132730Skan	      locate_old_decl (olddecl, error);
1145132730Skan	      return false;
114618334Speter	    }
114718334Speter	}
1148132730Skan      /* Two objects with the same name declared at the same block
1149132730Skan	 scope must both be external references (6.7p3).  */
1150132730Skan      else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1151132730Skan	       && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1152132730Skan	{
1153132730Skan	  if (DECL_EXTERNAL (newdecl))
1154132730Skan	    error ("%Jextern declaration of '%D' follows "
1155132730Skan		   "declaration with no linkage", newdecl, newdecl);
1156132730Skan	  else if (DECL_EXTERNAL (olddecl))
1157132730Skan	    error ("%Jdeclaration of '%D' with no linkage follows "
1158132730Skan		   "extern declaration", newdecl, newdecl);
1159132730Skan	  else
1160132730Skan	    error ("%Jredeclaration of '%D' with no linkage",
1161132730Skan		   newdecl, newdecl);
1162117421Skan
1163132730Skan	  locate_old_decl (olddecl, error);
1164132730Skan	  return false;
1165132730Skan	}
116618334Speter    }
1167132730Skan
1168132730Skan  /* warnings */
1169132730Skan  /* All decls must agree on a non-default visibility.  */
1170132730Skan  if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1171132730Skan      && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1172132730Skan      && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1173117421Skan    {
1174132730Skan      warning ("%Jredeclaration of '%D' with different visibility "
1175132730Skan	       "(old visibility preserved)", newdecl, newdecl);
1176132730Skan      warned = true;
1177117421Skan    }
1178132730Skan
1179132730Skan  if (TREE_CODE (newdecl) == FUNCTION_DECL)
1180117421Skan    {
1181132730Skan      /* Diagnose inline __attribute__ ((noinline)) which is silly.  */
1182132730Skan      if (DECL_DECLARED_INLINE_P (newdecl)
1183132730Skan	  && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
118418334Speter	{
1185132730Skan	  warning ("%Jinline declaration of '%D' follows "
1186132730Skan		   "declaration with attribute noinline", newdecl, newdecl);
1187132730Skan	  warned = true;
118818334Speter	}
1189132730Skan      else if (DECL_DECLARED_INLINE_P (olddecl)
1190132730Skan	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
119118334Speter	{
1192132730Skan	  warning ("%Jdeclaration of '%D' with attribute noinline follows "
1193132730Skan		   "inline declaration ", newdecl, newdecl);
1194132730Skan	  warned = true;
119518334Speter	}
1196132730Skan
1197132730Skan      /* Inline declaration after use or definition.
1198132730Skan	 ??? Should we still warn about this now we have unit-at-a-time
1199132730Skan	 mode and can get it right?  */
1200132730Skan      if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
120118334Speter	{
1202132730Skan	  if (TREE_USED (olddecl))
120318334Speter	    {
1204132730Skan	      warning ("%J'%D' declared inline after being called",
1205132730Skan		       olddecl, olddecl);
1206132730Skan	      warned = true;
120718334Speter	    }
1208132730Skan	  else if (DECL_INITIAL (olddecl))
1209132730Skan	    {
1210132730Skan	      warning ("%J'%D' declared inline after its definition",
1211132730Skan		       olddecl, olddecl);
1212132730Skan	      warned = true;
1213132730Skan	    }
121418334Speter	}
1215132730Skan    }
1216132730Skan  else /* PARM_DECL, VAR_DECL */
1217132730Skan    {
1218132730Skan      /* Redeclaration of a PARM_DECL is invalid unless this is the
1219132730Skan	 real position of a forward-declared parameter (GCC extension).  */
1220132730Skan      if (TREE_CODE (newdecl) == PARM_DECL
1221132730Skan	  && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
122218334Speter	{
1223132730Skan	  error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1224132730Skan	  locate_old_decl (olddecl, error);
1225132730Skan	  return false;
1226132730Skan	}
122718334Speter
1228132730Skan      /* These bits are only type qualifiers when applied to objects.  */
1229132730Skan      if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1230132730Skan	{
1231132730Skan	  if (TREE_THIS_VOLATILE (newdecl))
1232132730Skan	    pedwarn ("%Jvolatile declaration of '%D' follows "
1233132730Skan		     "non-volatile declaration", newdecl, newdecl);
1234132730Skan	  else
1235132730Skan	    pedwarn ("%Jnon-volatile declaration of '%D' follows "
1236132730Skan		     "volatile declaration", newdecl, newdecl);
1237132730Skan	  pedwarned = true;
123818334Speter	}
1239132730Skan      if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1240132730Skan	{
1241132730Skan	  if (TREE_READONLY (newdecl))
1242132730Skan	    pedwarn ("%Jconst declaration of '%D' follows "
1243132730Skan		     "non-const declaration", newdecl, newdecl);
1244132730Skan	  else
1245132730Skan	    pedwarn ("%Jnon-const declaration of '%D' follows "
1246132730Skan		     "const declaration", newdecl, newdecl);
1247132730Skan	  pedwarned = true;
1248132730Skan	}
124918334Speter    }
125018334Speter
1251132730Skan  /* Optional warning for completely redundant decls.  */
1252132730Skan  if (!warned && !pedwarned
1253132730Skan      && warn_redundant_decls
1254132730Skan      /* Don't warn about a function declaration followed by a
1255132730Skan	 definition.  */
1256132730Skan      && !(TREE_CODE (newdecl) == FUNCTION_DECL
1257132730Skan	   && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1258132730Skan      /* Don't warn about redundant redeclarations of builtins. */
1259132730Skan      && !(TREE_CODE (newdecl) == FUNCTION_DECL
1260132730Skan	   && !DECL_BUILT_IN (newdecl)
1261132730Skan	   && DECL_BUILT_IN (olddecl)
1262132730Skan	   && C_DECL_INVISIBLE (olddecl))
1263132730Skan      /* Don't warn about an extern followed by a definition.  */
1264132730Skan      && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1265132730Skan      /* Don't warn about forward parameter decls.  */
1266132730Skan      && !(TREE_CODE (newdecl) == PARM_DECL
1267132730Skan	   && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
126818334Speter    {
1269132730Skan      warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1270132730Skan      warned = true;
127118334Speter    }
127218334Speter
1273132730Skan  /* Report location of previous decl/defn in a consistent manner.  */
1274132730Skan  if (warned || pedwarned)
1275132730Skan    locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
127618334Speter
1277132730Skan  return true;
1278132730Skan}
127918334Speter
1280132730Skan/* Subroutine of duplicate_decls.  NEWDECL has been found to be
1281132730Skan   consistent with OLDDECL, but carries new information.  Merge the
1282132730Skan   new information into OLDDECL.  This function issues no
1283132730Skan   diagnostics.  */
1284132730Skan
1285132730Skanstatic void
1286132730Skanmerge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1287132730Skan{
1288132730Skan  int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1289132730Skan			   && DECL_INITIAL (newdecl) != 0);
1290132730Skan
1291132730Skan  /* For real parm decl following a forward decl, return 1 so old decl
1292132730Skan     will be reused.  Only allow this to happen once.  */
1293132730Skan  if (TREE_CODE (newdecl) == PARM_DECL
1294132730Skan      && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
129518334Speter    {
1296132730Skan      TREE_ASM_WRITTEN (olddecl) = 0;
1297132730Skan      return;
1298132730Skan    }
129950397Sobrien
1300132730Skan  DECL_ATTRIBUTES (newdecl)
1301132730Skan    = (*targetm.merge_decl_attributes) (olddecl, newdecl);
130218334Speter
1303132730Skan  /* Merge the data types specified in the two decls.  */
1304132730Skan  TREE_TYPE (newdecl)
1305132730Skan    = TREE_TYPE (olddecl)
1306132730Skan    = common_type (newtype, oldtype);
130718334Speter
1308132730Skan  /* Lay the type out, unless already done.  */
1309132730Skan  if (oldtype != TREE_TYPE (newdecl))
1310132730Skan    {
1311132730Skan      if (TREE_TYPE (newdecl) != error_mark_node)
1312132730Skan	layout_type (TREE_TYPE (newdecl));
1313132730Skan      if (TREE_CODE (newdecl) != FUNCTION_DECL
1314132730Skan	  && TREE_CODE (newdecl) != TYPE_DECL
1315132730Skan	  && TREE_CODE (newdecl) != CONST_DECL)
1316132730Skan	layout_decl (newdecl, 0);
1317132730Skan    }
1318132730Skan  else
1319132730Skan    {
1320132730Skan      /* Since the type is OLDDECL's, make OLDDECL's size go with.  */
1321132730Skan      DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1322132730Skan      DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1323132730Skan      DECL_MODE (newdecl) = DECL_MODE (olddecl);
1324132730Skan      if (TREE_CODE (olddecl) != FUNCTION_DECL)
1325132730Skan	if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1326132730Skan	  {
1327132730Skan	    DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1328132730Skan	    DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1329132730Skan	  }
1330132730Skan    }
133118334Speter
1332132730Skan  /* Keep the old rtl since we can safely use it.  */
1333132730Skan  COPY_DECL_RTL (olddecl, newdecl);
133490075Sobrien
1335132730Skan  /* Merge the type qualifiers.  */
1336132730Skan  if (TREE_READONLY (newdecl))
1337132730Skan    TREE_READONLY (olddecl) = 1;
133818334Speter
1339132730Skan  if (TREE_THIS_VOLATILE (newdecl))
1340132730Skan    {
1341132730Skan      TREE_THIS_VOLATILE (olddecl) = 1;
1342132730Skan      if (TREE_CODE (newdecl) == VAR_DECL)
1343132730Skan	make_var_volatile (newdecl);
1344132730Skan    }
134518334Speter
1346132730Skan  /* Keep source location of definition rather than declaration.  */
1347132730Skan  if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1348132730Skan    DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
134918334Speter
1350132730Skan  /* Merge the unused-warning information.  */
1351132730Skan  if (DECL_IN_SYSTEM_HEADER (olddecl))
1352132730Skan    DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1353132730Skan  else if (DECL_IN_SYSTEM_HEADER (newdecl))
1354132730Skan    DECL_IN_SYSTEM_HEADER (olddecl) = 1;
135518334Speter
1356132730Skan  /* Merge the initialization information.  */
1357132730Skan   if (DECL_INITIAL (newdecl) == 0)
1358132730Skan    DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
135918334Speter
1360132730Skan  /* Merge the section attribute.
1361132730Skan     We want to issue an error if the sections conflict but that must be
1362132730Skan     done later in decl_attributes since we are called before attributes
1363132730Skan     are assigned.  */
1364132730Skan  if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1365132730Skan    DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
136690075Sobrien
1367132730Skan  /* Copy the assembler name.
1368132730Skan     Currently, it can only be defined in the prototype.  */
1369132730Skan  COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1370132730Skan
1371132730Skan  /* If either declaration has a nondefault visibility, use it.  */
1372132730Skan  if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1373132730Skan    DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1374132730Skan
1375132730Skan  if (TREE_CODE (newdecl) == FUNCTION_DECL)
137618334Speter    {
1377132730Skan      DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1378132730Skan      DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1379132730Skan      DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1380132730Skan      DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1381132730Skan	|= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1382132730Skan      TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1383132730Skan      TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1384132730Skan      DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1385132730Skan      DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
138618334Speter    }
138718334Speter
138818334Speter  /* Merge the storage class information.  */
138996263Sobrien  merge_weak (newdecl, olddecl);
139096263Sobrien
139118334Speter  /* For functions, static overrides non-static.  */
139218334Speter  if (TREE_CODE (newdecl) == FUNCTION_DECL)
139318334Speter    {
139418334Speter      TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
139518334Speter      /* This is since we don't automatically
139618334Speter	 copy the attributes of NEWDECL into OLDDECL.  */
139718334Speter      TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
139818334Speter      /* If this clears `static', clear it in the identifier too.  */
139918334Speter      if (! TREE_PUBLIC (olddecl))
140018334Speter	TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
140118334Speter    }
140218334Speter  if (DECL_EXTERNAL (newdecl))
140318334Speter    {
1404132730Skan      TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1405132730Skan      DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1406132730Skan
140718334Speter      /* An extern decl does not override previous storage class.  */
140818334Speter      TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
140950397Sobrien      if (! DECL_EXTERNAL (newdecl))
1410132730Skan	{
1411132730Skan	  DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1412132730Skan	  DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1413132730Skan	}
141418334Speter    }
141518334Speter  else
141618334Speter    {
141718334Speter      TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
141818334Speter      TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
141918334Speter    }
142018334Speter
142150397Sobrien  if (TREE_CODE (newdecl) == FUNCTION_DECL)
142218334Speter    {
142390075Sobrien      /* If we're redefining a function previously defined as extern
142490075Sobrien	 inline, make sure we emit debug info for the inline before we
142590075Sobrien	 throw it away, in case it was inlined into a function that hasn't
142690075Sobrien	 been written out yet.  */
1427110623Skan      if (new_is_definition && DECL_INITIAL (olddecl))
142890075Sobrien	{
1429132730Skan	  if (TREE_USED (olddecl)
1430132730Skan	      /* In unit-at-a-time mode we never inline re-defined extern
1431132730Skan	         inline functions.  */
1432132730Skan	      && !flag_unit_at_a_time
1433132730Skan	      && cgraph_function_possibly_inlined_p (olddecl))
1434110623Skan	    (*debug_hooks->outlining_inline_function) (olddecl);
143590075Sobrien
143690075Sobrien	  /* The new defn must not be inline.  */
143790075Sobrien	  DECL_INLINE (newdecl) = 0;
143890075Sobrien	  DECL_UNINLINABLE (newdecl) = 1;
143990075Sobrien	}
144090075Sobrien      else
144190075Sobrien	{
144290075Sobrien	  /* If either decl says `inline', this fn is inline,
144390075Sobrien	     unless its definition was passed already.  */
144490075Sobrien	  if (DECL_DECLARED_INLINE_P (newdecl)
144590075Sobrien	      || DECL_DECLARED_INLINE_P (olddecl))
144690075Sobrien	    DECL_DECLARED_INLINE_P (newdecl) = 1;
144790075Sobrien
144890075Sobrien	  DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
144990075Sobrien	    = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
145090075Sobrien	}
145190075Sobrien
145218334Speter      if (DECL_BUILT_IN (olddecl))
145318334Speter	{
1454132730Skan	  /* If redeclaring a builtin function, it stays built in.  */
1455132730Skan	  DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1456132730Skan	  DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
145718334Speter	}
145890075Sobrien
145950397Sobrien      /* Also preserve various other info from the definition.  */
146050397Sobrien      if (! new_is_definition)
146150397Sobrien	{
146250397Sobrien	  DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1463132730Skan	  DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
146450397Sobrien	  DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
146590075Sobrien	  DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
146650397Sobrien	  DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
146790075Sobrien
146890075Sobrien	  /* Set DECL_INLINE on the declaration if we've got a body
146990075Sobrien	     from which to instantiate.  */
147090075Sobrien	  if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
147190075Sobrien	    {
147290075Sobrien	      DECL_INLINE (newdecl) = 1;
147390075Sobrien	      DECL_ABSTRACT_ORIGIN (newdecl)
1474132730Skan		= DECL_ABSTRACT_ORIGIN (olddecl);
147590075Sobrien	    }
147650397Sobrien	}
147790075Sobrien      else
147890075Sobrien	{
147990075Sobrien	  /* If a previous declaration said inline, mark the
148090075Sobrien	     definition as inlinable.  */
148190075Sobrien	  if (DECL_DECLARED_INLINE_P (newdecl)
148290075Sobrien	      && ! DECL_UNINLINABLE (newdecl))
148390075Sobrien	    DECL_INLINE (newdecl) = 1;
148490075Sobrien	}
148550397Sobrien    }
148618334Speter
148718334Speter  /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1488132730Skan     But preserve OLDDECL's DECL_UID and C_DECL_INVISIBLE.  */
148918334Speter  {
149090075Sobrien    unsigned olddecl_uid = DECL_UID (olddecl);
1491132730Skan    unsigned olddecl_invisible = C_DECL_INVISIBLE (olddecl);
149218334Speter
149390075Sobrien    memcpy ((char *) olddecl + sizeof (struct tree_common),
149490075Sobrien	    (char *) newdecl + sizeof (struct tree_common),
149590075Sobrien	    sizeof (struct tree_decl) - sizeof (struct tree_common));
149618334Speter    DECL_UID (olddecl) = olddecl_uid;
1497132730Skan    C_DECL_INVISIBLE (olddecl) = olddecl_invisible;
149818334Speter  }
149918334Speter
1500132730Skan  /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1501119268Skan     so that encode_section_info has a chance to look at the new decl
1502119268Skan     flags and attributes.  */
1503119268Skan  if (DECL_RTL_SET_P (olddecl)
1504119268Skan      && (TREE_CODE (olddecl) == FUNCTION_DECL
1505119268Skan	  || (TREE_CODE (olddecl) == VAR_DECL
1506119268Skan	      && TREE_STATIC (olddecl))))
1507119268Skan    make_decl_rtl (olddecl, NULL);
1508132730Skan}
1509119268Skan
1510132730Skan/* Handle when a new declaration NEWDECL has the same name as an old
1511132730Skan   one OLDDECL in the same binding contour.  Prints an error message
1512132730Skan   if appropriate.
1513132730Skan
1514132730Skan   If safely possible, alter OLDDECL to look like NEWDECL, and return
1515132730Skan   true.  Otherwise, return false.  */
1516132730Skan
1517132730Skanstatic bool
1518132730Skanduplicate_decls (tree newdecl, tree olddecl)
1519132730Skan{
1520132730Skan  tree newtype, oldtype;
1521132730Skan
1522132730Skan  if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1523132730Skan    return false;
1524132730Skan
1525132730Skan  merge_decls (newdecl, olddecl, newtype, oldtype);
1526132730Skan  return true;
152718334Speter}
1528132730Skan
1529132730Skan
1530132730Skan/* Return any external DECL associated with ID, whether or not it is
1531132730Skan   currently in scope.  */
153218334Speter
1533132730Skanstatic tree
1534132730Skanany_external_decl (tree id)
1535132730Skan{
1536132730Skan  tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1537132730Skan  tree t;
1538132730Skan
1539132730Skan  if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1540132730Skan    return 0;
1541132730Skan  else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1542132730Skan    return decl;
1543132730Skan
1544132730Skan  t = purpose_member (id, truly_local_externals);
1545132730Skan  if (t)
1546132730Skan    return TREE_VALUE (t);
1547132730Skan
1548132730Skan  return 0;
1549132730Skan}
1550132730Skan
1551132730Skan/* Record an external decl DECL.  This only does something if a
1552132730Skan   shadowing decl already exists.  */
1553132730Skanstatic void
1554132730Skanrecord_external_decl (tree decl)
1555132730Skan{
1556132730Skan  tree name = DECL_NAME (decl);
1557132730Skan  if (!IDENTIFIER_SYMBOL_VALUE (name))
1558132730Skan    return;
1559132730Skan
1560132730Skan  truly_local_externals = tree_cons (name, decl, truly_local_externals);
1561132730Skan}
1562132730Skan
156390075Sobrien/* Check whether decl-node X shadows an existing declaration.
1564132730Skan   OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
156590075Sobrien   which might be a NULL_TREE.  */
156690075Sobrienstatic void
1567132730Skanwarn_if_shadowing (tree x, tree old)
156890075Sobrien{
1569132730Skan  /* Nothing to shadow?  */
1570132730Skan  if (old == 0
1571132730Skan      /* Shadow warnings not wanted?  */
1572132730Skan      || !warn_shadow
1573132730Skan      /* No shadow warnings for internally generated vars.  */
1574132730Skan      || DECL_SOURCE_LINE (x) == 0
1575132730Skan      /* No shadow warnings for vars made for inlining.  */
1576132730Skan      || DECL_FROM_INLINE (x)
1577132730Skan      /* Don't warn about the parm names in function declarator
1578132730Skan	 within a function declarator.
1579132730Skan	 It would be nice to avoid warning in any function
1580132730Skan	 declarator in a declaration, as opposed to a definition,
1581132730Skan	 but there is no way to tell it's not a definition.  */
1582132730Skan      || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag)
1583132730Skan      /* Shadow warnings only apply to local variables and parameters.  */
1584132730Skan      || (TREE_CODE (x) != PARM_DECL && DECL_FILE_SCOPE_P (x)))
158590075Sobrien    return;
158690075Sobrien
1587132730Skan  if (TREE_CODE (old) == PARM_DECL)
1588132730Skan    warning ("%Jdeclaration of '%D' shadows a parameter", x, x);
1589132730Skan  else if (DECL_FILE_SCOPE_P (old))
1590132730Skan    warning ("%Jdeclaration of '%D' shadows a global declaration", x, x);
1591132730Skan  else
1592132730Skan    warning ("%Jdeclaration of '%D' shadows a previous local", x, x);
159390075Sobrien
1594132730Skan  warning ("%Jshadowed declaration is here", old);
1595132730Skan}
1596132730Skan
1597132730Skan
1598132730Skan/* Subroutine of pushdecl.
1599132730Skan
1600132730Skan   X is a TYPE_DECL for a typedef statement.  Create a brand new
1601132730Skan   ..._TYPE node (which will be just a variant of the existing
1602132730Skan   ..._TYPE node with identical properties) and then install X
1603132730Skan   as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1604132730Skan
1605132730Skan   The whole point here is to end up with a situation where each
1606132730Skan   and every ..._TYPE node the compiler creates will be uniquely
1607132730Skan   associated with AT MOST one node representing a typedef name.
1608132730Skan   This way, even though the compiler substitutes corresponding
1609132730Skan   ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1610132730Skan   early on, later parts of the compiler can always do the reverse
1611132730Skan   translation and get back the corresponding typedef name.  For
1612132730Skan   example, given:
1613132730Skan
1614132730Skan        typedef struct S MY_TYPE;
1615132730Skan	MY_TYPE object;
1616132730Skan
1617132730Skan   Later parts of the compiler might only know that `object' was of
1618132730Skan   type `struct S' if it were not for code just below.  With this
1619132730Skan   code however, later parts of the compiler see something like:
1620132730Skan
1621132730Skan	struct S' == struct S
1622132730Skan	typedef struct S' MY_TYPE;
1623132730Skan	struct S' object;
1624132730Skan
1625132730Skan    And they can then deduce (from the node for type struct S') that
1626132730Skan    the original object declaration was:
1627132730Skan
1628132730Skan		MY_TYPE object;
1629132730Skan
1630132730Skan    Being able to do this is important for proper support of protoize,
1631132730Skan    and also for generating precise symbolic debugging information
1632132730Skan    which takes full account of the programmer's (typedef) vocabulary.
1633132730Skan
1634132730Skan    Obviously, we don't want to generate a duplicate ..._TYPE node if
1635132730Skan    the TYPE_DECL node that we are now processing really represents a
1636132730Skan    standard built-in type.
1637132730Skan
1638132730Skan    Since all standard types are effectively declared at line zero
1639132730Skan    in the source file, we can easily check to see if we are working
1640132730Skan    on a standard type by checking the current value of lineno.  */
1641132730Skan
1642132730Skanstatic void
1643132730Skanclone_underlying_type (tree x)
1644132730Skan{
1645132730Skan  if (DECL_SOURCE_LINE (x) == 0)
164690075Sobrien    {
1647132730Skan      if (TYPE_NAME (TREE_TYPE (x)) == 0)
1648132730Skan	TYPE_NAME (TREE_TYPE (x)) = x;
164990075Sobrien    }
1650132730Skan  else if (TREE_TYPE (x) != error_mark_node
1651132730Skan	   && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
165290075Sobrien    {
1653132730Skan      tree tt = TREE_TYPE (x);
1654132730Skan      DECL_ORIGINAL_TYPE (x) = tt;
1655132730Skan      tt = build_type_copy (tt);
1656132730Skan      TYPE_NAME (tt) = x;
1657132730Skan      TREE_USED (tt) = TREE_USED (x);
1658132730Skan      TREE_TYPE (x) = tt;
165990075Sobrien    }
166090075Sobrien}
166190075Sobrien
166218334Speter/* Record a decl-node X as belonging to the current lexical scope.
166318334Speter   Check for errors (such as an incompatible declaration for the same
166418334Speter   name already seen in the same scope).
166518334Speter
166618334Speter   Returns either X or an old decl for the same name.
166718334Speter   If an old decl is returned, it may have been smashed
166818334Speter   to agree with what X says.  */
166918334Speter
167018334Spetertree
1671132730Skanpushdecl (tree x)
167218334Speter{
167390075Sobrien  tree name = DECL_NAME (x);
1674132730Skan  struct c_scope *scope = current_scope;
167518334Speter
1676132730Skan#ifdef ENABLE_CHECKING
1677132730Skan  if (error_mark_node == 0)
1678132730Skan    /* Called too early.  */
1679132730Skan    abort ();
1680132730Skan#endif
1681132730Skan
168290075Sobrien  /* Functions need the lang_decl data.  */
168390075Sobrien  if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1684132730Skan    DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
168590075Sobrien
168618334Speter  /* A local extern declaration for a function doesn't constitute nesting.
168718334Speter     A local auto declaration does, since it's a forward decl
168818334Speter     for a nested function coming later.  */
1689132730Skan  if (current_function_decl == NULL
1690132730Skan      || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1691132730Skan	  && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1692132730Skan    DECL_CONTEXT (x) = current_file_decl;
1693132730Skan  else
1694132730Skan    DECL_CONTEXT (x) = current_function_decl;
169518334Speter
169618334Speter  if (name)
169718334Speter    {
1698132730Skan      tree old;
169918334Speter
170090075Sobrien      if (warn_nested_externs
1701132730Skan	  && scope != global_scope
170290075Sobrien	  && DECL_EXTERNAL (x)
170390075Sobrien	  && !DECL_IN_SYSTEM_HEADER (x))
170490075Sobrien	warning ("nested extern declaration of `%s'",
170590075Sobrien		 IDENTIFIER_POINTER (name));
170690075Sobrien
1707132730Skan      old = lookup_name_current_level (name);
1708132730Skan      if (old && duplicate_decls (x, old))
170950397Sobrien	{
1710132730Skan	  /* For PARM_DECLs, old may be a forward declaration.
1711132730Skan	     If so, we want to remove it from its old location
1712132730Skan	     (in the variables chain) and rechain it in the
1713132730Skan	     location given by the new declaration.  */
1714132730Skan	  if (TREE_CODE (x) == PARM_DECL)
171518334Speter	    {
1716132730Skan	      tree *p;
1717132730Skan	      for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1718132730Skan		if (*p == old)
1719132730Skan		  {
1720132730Skan		    *p = TREE_CHAIN (old);
1721132730Skan		    SCOPE_LIST_APPEND (scope, parms, old);
1722132730Skan		    break;
1723132730Skan		  }
172418334Speter	    }
1725132730Skan	  return old;
172618334Speter	}
1727132730Skan      if (DECL_EXTERNAL (x) || scope == global_scope)
172890075Sobrien	{
1729132730Skan	  /* Find and check against a previous, not-in-scope, external
1730132730Skan	     decl for this identifier.  (C99 6.2.7p2: All declarations
1731132730Skan	     that refer to the same object or function shall have
1732132730Skan	     compatible type; otherwise, the behavior is undefined.)  */
1733132730Skan 	  tree ext = any_external_decl (name);
1734132730Skan	  if (ext)
173590075Sobrien	    {
1736132730Skan	      if (duplicate_decls (x, ext))
1737132730Skan		x = copy_node (ext);
173890075Sobrien	    }
173918334Speter	  else
1740132730Skan	    record_external_decl (x);
174118334Speter	}
174218334Speter
1743132730Skan      if (TREE_CODE (x) == TYPE_DECL)
1744132730Skan	clone_underlying_type (x);
174518334Speter
1746132730Skan      /* If storing a local value, there may already be one
1747132730Skan	 (inherited).  If so, record it for restoration when this
1748132730Skan	 scope ends.  Take care not to do this if we are replacing an
1749132730Skan	 older decl in the same scope (i.e.  duplicate_decls returned
1750132730Skan	 false, above).  */
1751132730Skan      if (scope != global_scope)
175218334Speter	{
1753132730Skan	  tree inherited_decl = lookup_name (name);
1754132730Skan	  if (inherited_decl && inherited_decl != old)
175518334Speter	    {
1756132730Skan	      warn_if_shadowing (x, inherited_decl);
1757132730Skan	      scope->shadowed = tree_cons (name, inherited_decl,
1758132730Skan					   scope->shadowed);
175918334Speter	    }
176018334Speter	}
176190075Sobrien
1762132730Skan      /* Install the new declaration in the requested scope.  */
1763132730Skan      IDENTIFIER_SYMBOL_VALUE (name) = x;
1764132730Skan      C_DECL_INVISIBLE (x) = 0;
176518334Speter
1766132730Skan      /* If x's type is incomplete because it's based on a
1767132730Skan	 structure or union which has not yet been fully declared,
1768132730Skan	 attach it to that structure or union type, so we can go
1769132730Skan	 back and complete the variable declaration later, if the
1770132730Skan	 structure or union gets fully declared.
177118334Speter
177290075Sobrien	 If the input is erroneous, we can have error_mark in the type
177390075Sobrien	 slot (e.g. "f(void a, ...)") - that doesn't count as an
177490075Sobrien	 incomplete type.  */
177590075Sobrien      if (TREE_TYPE (x) != error_mark_node
177690075Sobrien	  && !COMPLETE_TYPE_P (TREE_TYPE (x)))
177790075Sobrien	{
177890075Sobrien	  tree element = TREE_TYPE (x);
177990075Sobrien
178090075Sobrien	  while (TREE_CODE (element) == ARRAY_TYPE)
178190075Sobrien	    element = TREE_TYPE (element);
1782132730Skan	  element = TYPE_MAIN_VARIANT (element);
1783132730Skan
1784122196Skan	  if ((TREE_CODE (element) == RECORD_TYPE
1785122196Skan	       || TREE_CODE (element) == UNION_TYPE)
1786122196Skan	      && (TREE_CODE (x) != TYPE_DECL
1787132730Skan		  || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1788132730Skan	      && !COMPLETE_TYPE_P (element))
1789132730Skan	    C_TYPE_INCOMPLETE_VARS (element)
1790132730Skan	      = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
179190075Sobrien	}
179218334Speter    }
179318334Speter
1794132730Skan  if (TREE_CODE (x) == PARM_DECL)
1795132730Skan    SCOPE_LIST_APPEND (scope, parms, x);
1796132730Skan  else
1797132730Skan    SCOPE_LIST_APPEND (scope, names, x);
179818334Speter
179918334Speter  return x;
180018334Speter}
180118334Speter
1802132730Skan/* Record X as belonging to the global scope (C99 "file scope").
1803132730Skan   This is used only internally by the Objective-C front end,
1804132730Skan   and is limited to its needs.  duplicate_decls is not called;
1805132730Skan   if there is any preexisting decl for this identifier, it is an ICE.  */
180618334Speter
180718334Spetertree
1808132730Skanpushdecl_top_level (tree x)
180918334Speter{
1810132730Skan  tree name;
181118334Speter
1812132730Skan  if (TREE_CODE (x) != VAR_DECL)
1813132730Skan    abort ();
1814132730Skan
1815132730Skan  name = DECL_NAME (x);
1816132730Skan
1817132730Skan  if (IDENTIFIER_SYMBOL_VALUE (name))
1818132730Skan    abort ();
1819132730Skan
1820132730Skan  DECL_CONTEXT (x) = current_file_decl;
1821132730Skan  IDENTIFIER_SYMBOL_VALUE (name) = x;
1822132730Skan
1823132730Skan  SCOPE_LIST_APPEND (global_scope, names, x);
1824132730Skan  return x;
182518334Speter}
182618334Speter
1827132730Skan/* Generate an implicit declaration for identifier FUNCTIONID as a
1828132730Skan   function of type int ().  */
182918334Speter
183018334Spetertree
1831132730Skanimplicitly_declare (tree functionid)
183218334Speter{
1833132730Skan  tree decl = any_external_decl (functionid);
183418334Speter
1835132730Skan  if (decl)
1836132730Skan    {
1837132730Skan      /* Implicit declaration of a function already declared
1838132730Skan	 (somehow) in a different scope, or as a built-in.
1839132730Skan	 If this is the first time this has happened, warn;
1840132730Skan	 then recycle the old declaration.  */
1841132730Skan      if (!C_DECL_IMPLICIT (decl))
1842132730Skan	{
1843132730Skan	  implicit_decl_warning (DECL_NAME (decl));
1844132730Skan	  if (! DECL_FILE_SCOPE_P (decl))
1845132730Skan	    warning ("%Jprevious declaration of '%D'", decl, decl);
1846132730Skan	  C_DECL_IMPLICIT (decl) = 1;
1847132730Skan	}
1848132730Skan      /* If this function is global, then it must already be in the
1849132730Skan	 global scope, so there's no need to push it again.  */
1850132730Skan      if (current_scope == global_scope)
1851132730Skan	return decl;
1852132730Skan      /* If this is a local declaration, make a copy; we can't have
1853132730Skan	 the same DECL listed in two different scopes.  */
1854132730Skan      return pushdecl (copy_node (decl));
1855132730Skan    }
185618334Speter
1857132730Skan  /* Not seen before.  */
1858132730Skan  decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
185918334Speter  DECL_EXTERNAL (decl) = 1;
186018334Speter  TREE_PUBLIC (decl) = 1;
1861132730Skan  C_DECL_IMPLICIT (decl) = 1;
1862132730Skan  implicit_decl_warning (functionid);
186318334Speter
1864132730Skan  /* C89 says implicit declarations are in the innermost block.
1865117421Skan     So we record the decl in the standard fashion.  */
1866132730Skan  decl = pushdecl (decl);
186718334Speter
1868132730Skan  /* No need to call objc_check_decl here - it's a function type.  */
186990075Sobrien  rest_of_decl_compilation (decl, NULL, 0, 0);
187018334Speter
1871132730Skan  /* Write a record describing this implicit function declaration
1872132730Skan     to the prototypes file (if requested).  */
187318334Speter  gen_aux_info_record (decl, 0, 1, 0);
187418334Speter
187590075Sobrien  /* Possibly apply some default attributes to this implicit declaration.  */
187690075Sobrien  decl_attributes (&decl, NULL_TREE, 0);
187718334Speter
187818334Speter  return decl;
187918334Speter}
188018334Speter
1881132730Skanstatic void
1882132730Skanimplicit_decl_warning (tree id)
188390075Sobrien{
188490075Sobrien  const char *name = IDENTIFIER_POINTER (id);
188590075Sobrien  if (mesg_implicit_function_declaration == 2)
188690075Sobrien    error ("implicit declaration of function `%s'", name);
188790075Sobrien  else if (mesg_implicit_function_declaration == 1)
188890075Sobrien    warning ("implicit declaration of function `%s'", name);
188990075Sobrien}
189090075Sobrien
1891132730Skan/* Issue an error message for a reference to an undeclared variable
1892132730Skan   ID, including a reference to a builtin outside of function-call
1893132730Skan   context.  Establish a binding of the identifier to error_mark_node
1894132730Skan   in an appropriate scope, which will suppress further errors for the
1895132730Skan   same identifier.  */
1896132730Skanvoid
1897132730Skanundeclared_variable (tree id)
1898132730Skan{
1899132730Skan  static bool already = false;
1900132730Skan  struct c_scope *scope;
190118334Speter
1902132730Skan  if (current_function_decl == 0)
190318334Speter    {
1904132730Skan      error ("`%s' undeclared here (not in a function)",
1905132730Skan	     IDENTIFIER_POINTER (id));
1906132730Skan      scope = current_scope;
190718334Speter    }
190818334Speter  else
190918334Speter    {
1910132730Skan      error ("`%s' undeclared (first use in this function)",
1911132730Skan	     IDENTIFIER_POINTER (id));
1912132730Skan
1913132730Skan      if (! already)
1914132730Skan	{
1915132730Skan	  error ("(Each undeclared identifier is reported only once");
1916132730Skan	  error ("for each function it appears in.)");
1917132730Skan	  already = true;
1918132730Skan	}
1919132730Skan
1920132730Skan      scope = current_function_scope;
192118334Speter    }
1922132730Skan
1923132730Skan  scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1924132730Skan			       scope->shadowed);
1925132730Skan  IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
192618334Speter}
192718334Speter
1928132730Skan/* Subroutine of lookup_label, declare_label, define_label: construct a
1929132730Skan   LABEL_DECL with all the proper frills.  */
1930132730Skan
1931132730Skanstatic tree
1932132730Skanmake_label (tree name, location_t location)
1933132730Skan{
1934132730Skan  tree label = build_decl (LABEL_DECL, name, void_type_node);
1935132730Skan
1936132730Skan  DECL_CONTEXT (label) = current_function_decl;
1937132730Skan  DECL_MODE (label) = VOIDmode;
1938132730Skan  DECL_SOURCE_LOCATION (label) = location;
1939132730Skan
1940132730Skan  return label;
1941132730Skan}
1942132730Skan
1943132730Skan/* Another subroutine of lookup_label, declare_label, define_label:
1944132730Skan   set up the binding of name to LABEL_DECL in the given SCOPE.  */
1945132730Skan
1946132730Skanstatic void
1947132730Skanbind_label (tree name, tree label, struct c_scope *scope)
1948132730Skan{
1949132730Skan  if (IDENTIFIER_LABEL_VALUE (name))
1950132730Skan    scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1951132730Skan				 scope->shadowed);
1952132730Skan  IDENTIFIER_LABEL_VALUE (name) = label;
1953132730Skan
1954132730Skan  SCOPE_LIST_APPEND (scope, names, label);
1955132730Skan}
1956132730Skan
1957132730Skan/* Get the LABEL_DECL corresponding to identifier NAME as a label.
195818334Speter   Create one if none exists so far for the current function.
1959132730Skan   This is called when a label is used in a goto expression or
1960132730Skan   has its address taken.  */
196118334Speter
196218334Spetertree
1963132730Skanlookup_label (tree name)
196418334Speter{
1965132730Skan  tree label;
196618334Speter
196718334Speter  if (current_function_decl == 0)
196818334Speter    {
196918334Speter      error ("label %s referenced outside of any function",
1970132730Skan	     IDENTIFIER_POINTER (name));
197118334Speter      return 0;
197218334Speter    }
197318334Speter
1974132730Skan  /* Use a label already defined or ref'd with this name, but not if
1975132730Skan     it is inherited from a containing function and wasn't declared
1976132730Skan     using __label__.  */
1977132730Skan  label = IDENTIFIER_LABEL_VALUE (name);
1978132730Skan  if (label && (DECL_CONTEXT (label) == current_function_decl
1979132730Skan		|| C_DECLARED_LABEL_FLAG (label)))
198018334Speter    {
1981132730Skan      /* If the label has only been declared, update its apparent
1982132730Skan	 location to point here, for better diagnostics if it
1983132730Skan	 turns out not to have been defined.  */
1984132730Skan      if (!TREE_USED (label))
1985132730Skan	DECL_SOURCE_LOCATION (label) = input_location;
1986132730Skan      return label;
198718334Speter    }
198818334Speter
1989132730Skan  /* No label binding for that identifier; make one.  */
1990132730Skan  label = make_label (name, input_location);
199118334Speter
1992132730Skan  /* Ordinary labels go in the current function scope.  */
1993132730Skan  bind_label (name, label, current_function_scope);
1994132730Skan  return label;
199518334Speter}
199618334Speter
1997132730Skan/* Make a label named NAME in the current function, shadowing silently
1998132730Skan   any that may be inherited from containing functions or containing
1999132730Skan   scopes.  This is called for __label__ declarations.  */
200018334Speter
2001132730Skan/* Note that valid use, if the label being shadowed comes from another
2002132730Skan   scope in the same function, requires calling declare_nonlocal_label
2003132730Skan   right away.  (Is this still true?  -zw 2003-07-17)  */
200418334Speter
200518334Spetertree
2006132730Skandeclare_label (tree name)
200718334Speter{
2008132730Skan  tree label = IDENTIFIER_LABEL_VALUE (name);
2009132730Skan  tree dup;
201018334Speter
2011132730Skan  /* Check to make sure that the label hasn't already been declared
2012132730Skan     at this scope */
2013132730Skan  for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2014132730Skan    if (dup == label)
2015132730Skan      {
2016132730Skan	error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2017132730Skan	error ("%Jthis is a previous declaration", dup);
201818334Speter
2019132730Skan	/* Just use the previous declaration.  */
2020132730Skan	return dup;
2021132730Skan      }
202218334Speter
2023132730Skan  label = make_label (name, input_location);
2024132730Skan  C_DECLARED_LABEL_FLAG (label) = 1;
202518334Speter
2026132730Skan  /* Declared labels go in the current scope.  */
2027132730Skan  bind_label (name, label, current_scope);
2028132730Skan  return label;
202918334Speter}
203018334Speter
203118334Speter/* Define a label, specifying the location in the source file.
203218334Speter   Return the LABEL_DECL node for the label, if the definition is valid.
203318334Speter   Otherwise return 0.  */
203418334Speter
203518334Spetertree
2036132730Skandefine_label (location_t location, tree name)
203718334Speter{
2038132730Skan  tree label;
203918334Speter
2040132730Skan  /* Find any preexisting label with this name.  It is an error
2041132730Skan     if that label has already been defined in this function, or
2042132730Skan     if there is a containing function with a declared label with
2043132730Skan     the same name.  */
2044132730Skan  label = IDENTIFIER_LABEL_VALUE (name);
204518334Speter
2046132730Skan  if (label
2047132730Skan      && ((DECL_CONTEXT (label) == current_function_decl
2048132730Skan	   && DECL_INITIAL (label) != 0)
2049132730Skan	  || (DECL_CONTEXT (label) != current_function_decl
2050132730Skan	      && C_DECLARED_LABEL_FLAG (label))))
205118334Speter    {
2052132730Skan      error ("%Hduplicate label `%D'", &location, label);
2053132730Skan      if (DECL_INITIAL (label))
2054132730Skan	error ("%J`%D' previously defined here", label, label);
2055132730Skan      else
2056132730Skan	error ("%J`%D' previously declared here", label, label);
205718334Speter      return 0;
205818334Speter    }
2059132730Skan  else if (label && DECL_CONTEXT (label) == current_function_decl)
2060132730Skan    {
2061132730Skan      /* The label has been used or declared already in this function,
2062132730Skan	 but not defined.  Update its location to point to this
2063132730Skan	 definition.  */
2064132730Skan      DECL_SOURCE_LOCATION (label) = location;
2065132730Skan    }
206618334Speter  else
206718334Speter    {
2068132730Skan      /* No label binding for that identifier; make one.  */
2069132730Skan      label = make_label (name, location);
2070132730Skan
2071132730Skan      /* Ordinary labels go in the current function scope.  */
2072132730Skan      bind_label (name, label, current_function_scope);
207318334Speter    }
2074132730Skan
2075132730Skan  if (warn_traditional && !in_system_header && lookup_name (name))
2076132730Skan    warning ("%Htraditional C lacks a separate namespace for labels, "
2077132730Skan             "identifier `%s' conflicts", &location,
2078132730Skan	     IDENTIFIER_POINTER (name));
2079132730Skan
2080132730Skan  /* Mark label as having been defined.  */
2081132730Skan  DECL_INITIAL (label) = error_mark_node;
2082132730Skan  return label;
208318334Speter}
208418334Speter
2085132730Skan/* Return the list of declarations of the current scope.  */
208618334Speter
208718334Spetertree
2088132730Skangetdecls (void)
208918334Speter{
2090132730Skan  return current_scope->names;
209118334Speter}
209218334Speter
209318334Speter
209418334Speter/* Given NAME, an IDENTIFIER_NODE,
209518334Speter   return the structure (or union or enum) definition for that name.
2096132730Skan   If THISLEVEL_ONLY is nonzero, searches only the current_scope.
209718334Speter   CODE says which kind of type the caller wants;
209818334Speter   it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
209918334Speter   If the wrong kind of type is found, an error is reported.  */
210018334Speter
210118334Speterstatic tree
2102132730Skanlookup_tag (enum tree_code code, tree name, int thislevel_only)
210318334Speter{
2104132730Skan  tree tag = IDENTIFIER_TAG_VALUE (name);
2105132730Skan  int thislevel = 0;
210618334Speter
2107132730Skan  if (!tag)
2108132730Skan    return 0;
2109132730Skan
2110132730Skan  /* We only care about whether it's in this level if
2111132730Skan     thislevel_only was set or it might be a type clash.  */
2112132730Skan  if (thislevel_only || TREE_CODE (tag) != code)
211318334Speter    {
2114132730Skan      if (current_scope == global_scope
2115132730Skan	  || purpose_member (name, current_scope->tags))
2116132730Skan	thislevel = 1;
211718334Speter    }
2118132730Skan
2119132730Skan  if (thislevel_only && !thislevel)
2120132730Skan    return 0;
2121132730Skan
2122132730Skan  if (TREE_CODE (tag) != code)
2123132730Skan    {
2124132730Skan      /* Definition isn't the kind we were looking for.  */
2125132730Skan      pending_invalid_xref = name;
2126132730Skan      pending_invalid_xref_location = input_location;
2127132730Skan
2128132730Skan      /* If in the same binding level as a declaration as a tag
2129132730Skan	 of a different type, this must not be allowed to
2130132730Skan	 shadow that tag, so give the error immediately.
2131132730Skan	 (For example, "struct foo; union foo;" is invalid.)  */
2132132730Skan      if (thislevel)
2133132730Skan	pending_xref_error ();
2134132730Skan    }
2135132730Skan  return tag;
213618334Speter}
213718334Speter
213818334Speter/* Print an error message now
213918334Speter   for a recent invalid struct, union or enum cross reference.
214018334Speter   We don't print them immediately because they are not invalid
214118334Speter   when used in the `struct foo;' construct for shadowing.  */
214218334Speter
214318334Spetervoid
2144132730Skanpending_xref_error (void)
214518334Speter{
214618334Speter  if (pending_invalid_xref != 0)
2147132730Skan    error ("%H`%s' defined as wrong kind of tag",
2148132730Skan           &pending_invalid_xref_location,
2149132730Skan           IDENTIFIER_POINTER (pending_invalid_xref));
215018334Speter  pending_invalid_xref = 0;
215118334Speter}
215218334Speter
215318334Speter
2154132730Skan/* Look up NAME in the current scope and its superiors
215518334Speter   in the namespace of variables, functions and typedefs.
215618334Speter   Return a ..._DECL node of some kind representing its definition,
215718334Speter   or return 0 if it is undefined.  */
215818334Speter
215918334Spetertree
2160132730Skanlookup_name (tree name)
216118334Speter{
2162132730Skan  tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2163132730Skan  if (decl == 0 || decl == error_mark_node)
2164132730Skan    return decl;
2165132730Skan  if (C_DECL_INVISIBLE (decl))
2166132730Skan    return 0;
2167132730Skan  return decl;
216818334Speter}
216918334Speter
2170132730Skan/* Similar to `lookup_name' but look only at the current scope.  */
217118334Speter
2172132730Skanstatic tree
2173132730Skanlookup_name_current_level (tree name)
217418334Speter{
2175132730Skan  tree decl = IDENTIFIER_SYMBOL_VALUE (name);
217618334Speter
2177132730Skan  if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
217818334Speter    return 0;
217918334Speter
2180132730Skan  if (current_scope == global_scope)
2181132730Skan    return decl;
218218334Speter
2183132730Skan  /* Scan the current scope for a decl with name NAME.
2184132730Skan     For PARM_DECLs, we have to look at both ->parms and ->names, since
2185132730Skan     forward parameter declarations wind up on the ->names list.  */
2186132730Skan  if (TREE_CODE (decl) == PARM_DECL
2187132730Skan      && chain_member (decl, current_scope->parms))
2188132730Skan    return decl;
2189132730Skan  if (chain_member (decl, current_scope->names))
2190132730Skan    return decl;
2191132730Skan
2192132730Skan  return 0;
219318334Speter}
219418334Speter
219518334Speter/* Create the predefined scalar types of C,
219650397Sobrien   and some nodes representing standard constants (0, 1, (void *) 0).
2197132730Skan   Initialize the global scope.
219818334Speter   Make definitions for built-in primitive functions.  */
219918334Speter
220018334Spetervoid
2201132730Skanc_init_decl_processing (void)
220218334Speter{
220390075Sobrien  tree endlink;
220490075Sobrien  tree ptr_ftype_void, ptr_ftype_ptr;
2205132730Skan  location_t save_loc = input_location;
220618334Speter
220790075Sobrien  /* Adds some ggc roots, and reserved words for c-parse.in.  */
220890075Sobrien  c_parse_init ();
220990075Sobrien
2210132730Skan  current_function_decl = 0;
221190075Sobrien
2212132730Skan  /* Make the c_scope structure for global names.  */
221390075Sobrien  pushlevel (0);
2214132730Skan  global_scope = current_scope;
221518334Speter
2216132730Skan  /* Declarations from c_common_nodes_and_builtins must not be associated
2217132730Skan     with this input file, lest we get differences between using and not
2218132730Skan     using preprocessed headers.  */
2219132730Skan  input_location.file = "<internal>";
2220132730Skan  input_location.line = 0;
2221132730Skan
2222132730Skan  /* Make the DECL for the toplevel file scope.  */
2223132730Skan  current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2224132730Skan
222590075Sobrien  build_common_tree_nodes (flag_signed_char);
222618334Speter
222790075Sobrien  c_common_nodes_and_builtins ();
222818334Speter
2229132730Skan  /* In C, comparisons and TRUTH_* expressions have type int.  */
2230132730Skan  truthvalue_type_node = integer_type_node;
2231132730Skan  truthvalue_true_node = integer_one_node;
2232132730Skan  truthvalue_false_node = integer_zero_node;
223318334Speter
2234132730Skan  /* Even in C99, which has a real boolean type.  */
223590075Sobrien  pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2236132730Skan			boolean_type_node));
223718334Speter
223890075Sobrien  endlink = void_list_node;
223950397Sobrien  ptr_ftype_void = build_function_type (ptr_type_node, endlink);
224050397Sobrien  ptr_ftype_ptr
224150397Sobrien    = build_function_type (ptr_type_node,
224250397Sobrien			   tree_cons (NULL_TREE, ptr_type_node, endlink));
224350397Sobrien
2244132730Skan  input_location = save_loc;
2245132730Skan
224690075Sobrien  pedantic_lvalues = pedantic;
224718334Speter
224890075Sobrien  make_fname_decl = c_make_fname_decl;
224990075Sobrien  start_fname_decls ();
2250132730Skan
2251132730Skan  first_builtin_decl = global_scope->names;
2252132730Skan  last_builtin_decl = global_scope->names_last;
225390075Sobrien}
225418334Speter
225590075Sobrien/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
225690075Sobrien   decl, NAME is the initialization string and TYPE_DEP indicates whether
225790075Sobrien   NAME depended on the type of the function.  As we don't yet implement
225890075Sobrien   delayed emission of static data, we mark the decl as emitted
225990075Sobrien   so it is not placed in the output.  Anything using it must therefore pull
2260132730Skan   out the STRING_CST initializer directly.  FIXME.  */
226118334Speter
226290075Sobrienstatic tree
2263132730Skanc_make_fname_decl (tree id, int type_dep)
226490075Sobrien{
226590075Sobrien  const char *name = fname_as_string (type_dep);
226690075Sobrien  tree decl, type, init;
226790075Sobrien  size_t length = strlen (name);
226818334Speter
226990075Sobrien  type =  build_array_type
227090075Sobrien          (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
227190075Sobrien	   build_index_type (size_int (length)));
227218334Speter
227390075Sobrien  decl = build_decl (VAR_DECL, id, type);
2274132730Skan
227590075Sobrien  TREE_STATIC (decl) = 1;
227690075Sobrien  TREE_READONLY (decl) = 1;
227790075Sobrien  DECL_ARTIFICIAL (decl) = 1;
2278132730Skan
227990075Sobrien  init = build_string (length + 1, name);
228090075Sobrien  TREE_TYPE (init) = type;
228190075Sobrien  DECL_INITIAL (decl) = init;
228218334Speter
228390075Sobrien  TREE_USED (decl) = 1;
2284132730Skan
2285132730Skan  if (current_function_decl)
2286132730Skan    {
2287132730Skan      DECL_CONTEXT (decl) = current_function_decl;
2288132730Skan      IDENTIFIER_SYMBOL_VALUE (id) = decl;
2289132730Skan      SCOPE_LIST_APPEND (current_function_scope, names, decl);
2290132730Skan    }
2291132730Skan
229290075Sobrien  finish_decl (decl, init, NULL_TREE);
229318334Speter
229490075Sobrien  return decl;
229518334Speter}
229618334Speter
229718334Speter/* Return a definition for a builtin function named NAME and whose data type
229818334Speter   is TYPE.  TYPE should be a function type with argument types.
229918334Speter   FUNCTION_CODE tells later passes how to compile calls to this function.
230018334Speter   See tree.h for its possible values.
230118334Speter
230218334Speter   If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2303117421Skan   the name to be called if we can't opencode the function.  If
2304117421Skan   ATTRS is nonzero, use that for the function's attribute list.  */
230518334Speter
230618334Spetertree
2307132730Skanbuiltin_function (const char *name, tree type, int function_code,
2308132730Skan		  enum built_in_class class, const char *library_name,
2309132730Skan		  tree attrs)
231018334Speter{
231118334Speter  tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
231218334Speter  DECL_EXTERNAL (decl) = 1;
231318334Speter  TREE_PUBLIC (decl) = 1;
231418334Speter  if (library_name)
231590075Sobrien    SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
231690075Sobrien  make_decl_rtl (decl, NULL);
231718334Speter  pushdecl (decl);
231890075Sobrien  DECL_BUILT_IN_CLASS (decl) = class;
231990075Sobrien  DECL_FUNCTION_CODE (decl) = function_code;
232090075Sobrien
232118334Speter  /* Warn if a function in the namespace for users
232218334Speter     is used without an occasion to consider it declared.  */
232318334Speter  if (name[0] != '_' || name[1] != '_')
2324132730Skan    C_DECL_INVISIBLE (decl) = 1;
232518334Speter
232690075Sobrien  /* Possibly apply some default attributes to this built-in function.  */
2327117421Skan  if (attrs)
2328117421Skan    decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2329117421Skan  else
2330117421Skan    decl_attributes (&decl, NULL_TREE, 0);
233190075Sobrien
233218334Speter  return decl;
233318334Speter}
233418334Speter
233518334Speter/* Called when a declaration is seen that contains no names to declare.
233618334Speter   If its type is a reference to a structure, union or enum inherited
233718334Speter   from a containing scope, shadow that tag name for the current scope
233818334Speter   with a forward reference.
233918334Speter   If its type defines a new named structure or union
234018334Speter   or defines an enum, it is valid but we need not do anything here.
234118334Speter   Otherwise, it is an error.  */
234218334Speter
234318334Spetervoid
2344132730Skanshadow_tag (tree declspecs)
234518334Speter{
234618334Speter  shadow_tag_warned (declspecs, 0);
234718334Speter}
234818334Speter
234918334Spetervoid
2350132730Skanshadow_tag_warned (tree declspecs, int warned)
2351132730Skan
2352132730Skan
235318334Speter     /* 1 => we have done a pedwarn.  2 => we have done a warning, but
235418334Speter	no pedwarn.  */
235518334Speter{
235618334Speter  int found_tag = 0;
235790075Sobrien  tree link;
235850397Sobrien  tree specs, attrs;
235918334Speter
236018334Speter  pending_invalid_xref = 0;
236118334Speter
236250397Sobrien  /* Remove the attributes from declspecs, since they will confuse the
236350397Sobrien     following code.  */
236450397Sobrien  split_specs_attrs (declspecs, &specs, &attrs);
236550397Sobrien
236650397Sobrien  for (link = specs; link; link = TREE_CHAIN (link))
236718334Speter    {
236890075Sobrien      tree value = TREE_VALUE (link);
236990075Sobrien      enum tree_code code = TREE_CODE (value);
237018334Speter
237118334Speter      if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
237218334Speter	/* Used to test also that TYPE_SIZE (value) != 0.
237318334Speter	   That caused warning for `struct foo;' at top level in the file.  */
237418334Speter	{
2375132730Skan	  tree name = TYPE_NAME (value);
237690075Sobrien	  tree t;
237718334Speter
237818334Speter	  found_tag++;
237918334Speter
238018334Speter	  if (name == 0)
238118334Speter	    {
238218334Speter	      if (warned != 1 && code != ENUMERAL_TYPE)
238318334Speter		/* Empty unnamed enum OK */
238418334Speter		{
238518334Speter		  pedwarn ("unnamed struct/union that defines no instances");
238618334Speter		  warned = 1;
238718334Speter		}
238818334Speter	    }
238918334Speter	  else
239018334Speter	    {
2391132730Skan	      t = lookup_tag (code, name, 1);
239218334Speter
239318334Speter	      if (t == 0)
239418334Speter		{
239518334Speter		  t = make_node (code);
239618334Speter		  pushtag (name, t);
239718334Speter		}
239818334Speter	    }
239918334Speter	}
240018334Speter      else
240118334Speter	{
240218334Speter	  if (!warned && ! in_system_header)
240318334Speter	    {
240418334Speter	      warning ("useless keyword or type name in empty declaration");
240518334Speter	      warned = 2;
240618334Speter	    }
240718334Speter	}
240818334Speter    }
240918334Speter
241018334Speter  if (found_tag > 1)
241118334Speter    error ("two types specified in one empty declaration");
241218334Speter
241318334Speter  if (warned != 1)
241418334Speter    {
241518334Speter      if (found_tag == 0)
241618334Speter	pedwarn ("empty declaration");
241718334Speter    }
241818334Speter}
241918334Speter
242090075Sobrien/* Construct an array declarator.  EXPR is the expression inside [], or
242190075Sobrien   NULL_TREE.  QUALS are the type qualifiers inside the [] (to be applied
242290075Sobrien   to the pointer to which a parameter array is converted).  STATIC_P is
2423117421Skan   nonzero if "static" is inside the [], zero otherwise.  VLA_UNSPEC_P
2424117421Skan   is nonzero is the array is [*], a VLA of unspecified length which is
242590075Sobrien   nevertheless a complete type (not currently implemented by GCC),
242690075Sobrien   zero otherwise.  The declarator is constructed as an ARRAY_REF
242790075Sobrien   (to be decoded by grokdeclarator), whose operand 0 is what's on the
242890075Sobrien   left of the [] (filled by in set_array_declarator_type) and operand 1
242990075Sobrien   is the expression inside; whose TREE_TYPE is the type qualifiers and
243090075Sobrien   which has TREE_STATIC set if "static" is used.  */
243190075Sobrien
243290075Sobrientree
2433132730Skanbuild_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
243490075Sobrien{
243590075Sobrien  tree decl;
243690075Sobrien  decl = build_nt (ARRAY_REF, NULL_TREE, expr);
243790075Sobrien  TREE_TYPE (decl) = quals;
243890075Sobrien  TREE_STATIC (decl) = (static_p ? 1 : 0);
243990075Sobrien  if (pedantic && !flag_isoc99)
244090075Sobrien    {
244190075Sobrien      if (static_p || quals != NULL_TREE)
2442117421Skan	pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
244390075Sobrien      if (vla_unspec_p)
2444117421Skan	pedwarn ("ISO C90 does not support `[*]' array declarators");
244590075Sobrien    }
244690075Sobrien  if (vla_unspec_p)
244790075Sobrien    warning ("GCC does not yet properly implement `[*]' array declarators");
244890075Sobrien  return decl;
244990075Sobrien}
245090075Sobrien
245190075Sobrien/* Set the type of an array declarator.  DECL is the declarator, as
245290075Sobrien   constructed by build_array_declarator; TYPE is what appears on the left
2453117421Skan   of the [] and goes in operand 0.  ABSTRACT_P is nonzero if it is an
245490075Sobrien   abstract declarator, zero otherwise; this is used to reject static and
245590075Sobrien   type qualifiers in abstract declarators, where they are not in the
245690075Sobrien   C99 grammar.  */
245790075Sobrien
245890075Sobrientree
2459132730Skanset_array_declarator_type (tree decl, tree type, int abstract_p)
246090075Sobrien{
246190075Sobrien  TREE_OPERAND (decl, 0) = type;
246290075Sobrien  if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
246390075Sobrien    error ("static or type qualifiers in abstract declarator");
246490075Sobrien  return decl;
246590075Sobrien}
246690075Sobrien
246718334Speter/* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
246818334Speter
246918334Spetertree
2470132730Skangroktypename (tree typename)
247118334Speter{
247290075Sobrien  tree specs, attrs;
247390075Sobrien
247418334Speter  if (TREE_CODE (typename) != TREE_LIST)
247518334Speter    return typename;
247690075Sobrien
247790075Sobrien  split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
247890075Sobrien
2479132730Skan  typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2480132730Skan			     NULL);
248190075Sobrien
248290075Sobrien  /* Apply attributes.  */
248390075Sobrien  decl_attributes (&typename, attrs, 0);
248490075Sobrien
248590075Sobrien  return typename;
248618334Speter}
248718334Speter
248818334Speter/* Return a PARM_DECL node for a given pair of specs and declarator.  */
248918334Speter
249018334Spetertree
2491132730Skangroktypename_in_parm_context (tree typename)
249218334Speter{
249318334Speter  if (TREE_CODE (typename) != TREE_LIST)
249418334Speter    return typename;
249518334Speter  return grokdeclarator (TREE_VALUE (typename),
249618334Speter			 TREE_PURPOSE (typename),
2497132730Skan			 PARM, 0, NULL);
249818334Speter}
249918334Speter
250018334Speter/* Decode a declarator in an ordinary declaration or data definition.
250118334Speter   This is called as soon as the type information and variable name
250218334Speter   have been parsed, before parsing the initializer if any.
250318334Speter   Here we create the ..._DECL node, fill in its type,
250418334Speter   and put it on the list of decls for the current context.
250518334Speter   The ..._DECL node is returned as the value.
250618334Speter
250718334Speter   Exception: for arrays where the length is not specified,
250818334Speter   the type is left null, to be filled in by `finish_decl'.
250918334Speter
251018334Speter   Function definitions do not come here; they go to start_function
251118334Speter   instead.  However, external and forward declarations of functions
251218334Speter   do go through here.  Structure field declarations are done by
251318334Speter   grokfield and not through here.  */
251418334Speter
251518334Spetertree
2516132730Skanstart_decl (tree declarator, tree declspecs, int initialized, tree attributes)
251718334Speter{
251890075Sobrien  tree decl;
251990075Sobrien  tree tem;
2520132730Skan
252190075Sobrien  /* An object declared as __attribute__((deprecated)) suppresses
252290075Sobrien     warnings of uses of other deprecated items.  */
252390075Sobrien  if (lookup_attribute ("deprecated", attributes))
252490075Sobrien    deprecated_state = DEPRECATED_SUPPRESS;
252518334Speter
252690075Sobrien  decl = grokdeclarator (declarator, declspecs,
2527132730Skan			 NORMAL, initialized, NULL);
2528132730Skan
252990075Sobrien  deprecated_state = DEPRECATED_NORMAL;
253018334Speter
253190075Sobrien  if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
253290075Sobrien      && MAIN_NAME_P (DECL_NAME (decl)))
2533132730Skan    warning ("%J'%D' is usually a function", decl, decl);
253450397Sobrien
253518334Speter  if (initialized)
253618334Speter    /* Is it valid for this decl to have an initializer at all?
253718334Speter       If not, set INITIALIZED to zero, which will indirectly
253818334Speter       tell `finish_decl' to ignore the initializer once it is parsed.  */
253918334Speter    switch (TREE_CODE (decl))
254018334Speter      {
254118334Speter      case TYPE_DECL:
2542107593Sobrien	error ("typedef `%s' is initialized (use __typeof__ instead)",
2543104761Skan	       IDENTIFIER_POINTER (DECL_NAME (decl)));
2544104761Skan	initialized = 0;
254518334Speter	break;
254618334Speter
254718334Speter      case FUNCTION_DECL:
254818334Speter	error ("function `%s' is initialized like a variable",
254918334Speter	       IDENTIFIER_POINTER (DECL_NAME (decl)));
255018334Speter	initialized = 0;
255118334Speter	break;
255218334Speter
255318334Speter      case PARM_DECL:
255418334Speter	/* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE.  */
255518334Speter	error ("parameter `%s' is initialized",
255618334Speter	       IDENTIFIER_POINTER (DECL_NAME (decl)));
255718334Speter	initialized = 0;
255818334Speter	break;
255918334Speter
256018334Speter      default:
256118334Speter	/* Don't allow initializations for incomplete types
256218334Speter	   except for arrays which might be completed by the initialization.  */
256390075Sobrien
256490075Sobrien	/* This can happen if the array size is an undefined macro.  We already
256590075Sobrien	   gave a warning, so we don't need another one.  */
256690075Sobrien	if (TREE_TYPE (decl) == error_mark_node)
256790075Sobrien	  initialized = 0;
256890075Sobrien	else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
256918334Speter	  {
257018334Speter	    /* A complete type is ok if size is fixed.  */
257118334Speter
257218334Speter	    if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
257318334Speter		|| C_DECL_VARIABLE_SIZE (decl))
257418334Speter	      {
257518334Speter		error ("variable-sized object may not be initialized");
257618334Speter		initialized = 0;
257718334Speter	      }
257818334Speter	  }
257918334Speter	else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
258018334Speter	  {
258118334Speter	    error ("variable `%s' has initializer but incomplete type",
258218334Speter		   IDENTIFIER_POINTER (DECL_NAME (decl)));
258318334Speter	    initialized = 0;
258418334Speter	  }
258590075Sobrien	else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
258618334Speter	  {
258718334Speter	    error ("elements of array `%s' have incomplete type",
258818334Speter		   IDENTIFIER_POINTER (DECL_NAME (decl)));
258918334Speter	    initialized = 0;
259018334Speter	  }
259118334Speter      }
259218334Speter
259318334Speter  if (initialized)
259418334Speter    {
259518334Speter      DECL_EXTERNAL (decl) = 0;
2596132730Skan      if (current_scope == global_scope)
259718334Speter	TREE_STATIC (decl) = 1;
259818334Speter
259918334Speter      /* Tell `pushdecl' this is an initialized decl
260018334Speter	 even though we don't yet have the initializer expression.
260118334Speter	 Also tell `finish_decl' it may store the real initializer.  */
260218334Speter      DECL_INITIAL (decl) = error_mark_node;
260318334Speter    }
260418334Speter
260518334Speter  /* If this is a function declaration, write a record describing it to the
260618334Speter     prototypes file (if requested).  */
260718334Speter
260818334Speter  if (TREE_CODE (decl) == FUNCTION_DECL)
260918334Speter    gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
261018334Speter
261150397Sobrien  /* ANSI specifies that a tentative definition which is not merged with
261250397Sobrien     a non-tentative definition behaves exactly like a definition with an
261350397Sobrien     initializer equal to zero.  (Section 3.7.2)
2614117421Skan
2615117421Skan     -fno-common gives strict ANSI behavior, though this tends to break
2616117421Skan     a large body of code that grew up without this rule.
2617117421Skan
2618117421Skan     Thread-local variables are never common, since there's no entrenched
2619117421Skan     body of code to break, and it allows more efficient variable references
2620132730Skan     in the presence of dynamic linking.  */
2621117421Skan
2622117421Skan  if (TREE_CODE (decl) == VAR_DECL
2623117421Skan      && !initialized
2624117421Skan      && TREE_PUBLIC (decl)
2625117421Skan      && !DECL_THREAD_LOCAL (decl)
2626117421Skan      && !flag_no_common)
262750397Sobrien    DECL_COMMON (decl) = 1;
262818334Speter
262918334Speter  /* Set attributes here so if duplicate decl, will have proper attributes.  */
263090075Sobrien  decl_attributes (&decl, attributes, 0);
263118334Speter
2632132730Skan  if (TREE_CODE (decl) == FUNCTION_DECL
2633132730Skan      && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2634132730Skan    {
2635132730Skan      tree ce = declarator;
263696263Sobrien
2637132730Skan      if (TREE_CODE (ce) == INDIRECT_REF)
2638132730Skan	ce = TREE_OPERAND (declarator, 0);
2639132730Skan      if (TREE_CODE (ce) == CALL_EXPR)
2640132730Skan	{
2641132730Skan	  tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2642132730Skan	  for (; args; args = TREE_CHAIN (args))
2643132730Skan	    {
2644132730Skan	      tree type = TREE_TYPE (args);
2645132730Skan	      if (INTEGRAL_TYPE_P (type)
2646132730Skan		  && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2647132730Skan		DECL_ARG_TYPE (args) = integer_type_node;
2648132730Skan	    }
2649132730Skan	}
2650132730Skan    }
2651132730Skan
265290075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL
265390075Sobrien      && DECL_DECLARED_INLINE_P (decl)
265490075Sobrien      && DECL_UNINLINABLE (decl)
265590075Sobrien      && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2656132730Skan    warning ("%Jinline function '%D' given attribute noinline", decl, decl);
265790075Sobrien
2658132730Skan  /* Add this decl to the current scope.
265918334Speter     TEM may equal DECL or it may be a previous decl of the same name.  */
266018334Speter  tem = pushdecl (decl);
266118334Speter
266218334Speter  /* For a local variable, define the RTL now.  */
2663132730Skan  if (current_scope != global_scope
266418334Speter      /* But not if this is a duplicate decl
266518334Speter	 and we preserved the rtl from the previous one
266618334Speter	 (which may or may not happen).  */
266790075Sobrien      && !DECL_RTL_SET_P (tem)
2668132730Skan      && DECL_FILE_SCOPE_P (tem))
266918334Speter    {
267090075Sobrien      if (TREE_TYPE (tem) != error_mark_node
2671132730Skan	  && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2672132730Skan	      || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2673132730Skan		  && DECL_INITIAL (tem) != 0)))
267418334Speter	expand_decl (tem);
267518334Speter    }
267618334Speter
267718334Speter  return tem;
267818334Speter}
267918334Speter
268018334Speter/* Finish processing of a declaration;
268118334Speter   install its initial value.
268218334Speter   If the length of an array type is not known before,
268318334Speter   it must be determined now, from the initial value, or it is an error.  */
268418334Speter
268518334Spetervoid
2686132730Skanfinish_decl (tree decl, tree init, tree asmspec_tree)
268718334Speter{
268890075Sobrien  tree type = TREE_TYPE (decl);
268918334Speter  int was_incomplete = (DECL_SIZE (decl) == 0);
269090075Sobrien  const char *asmspec = 0;
269118334Speter
269290075Sobrien  /* If a name was specified, get the string.  */
2693132730Skan  if (current_scope == global_scope)
269496263Sobrien    asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
269518334Speter  if (asmspec_tree)
269618334Speter    asmspec = TREE_STRING_POINTER (asmspec_tree);
269718334Speter
269818334Speter  /* If `start_decl' didn't like having an initialization, ignore it now.  */
269918334Speter  if (init != 0 && DECL_INITIAL (decl) == 0)
270018334Speter    init = 0;
2701132730Skan
270218334Speter  /* Don't crash if parm is initialized.  */
270318334Speter  if (TREE_CODE (decl) == PARM_DECL)
270418334Speter    init = 0;
270518334Speter
270618334Speter  if (init)
2707104761Skan    store_init_value (decl, init);
270818334Speter
2709132730Skan  if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2710132730Skan		    || TREE_CODE (decl) == FUNCTION_DECL
2711132730Skan		    || TREE_CODE (decl) == FIELD_DECL))
2712132730Skan    objc_check_decl (decl);
2713132730Skan
2714132730Skan  /* Deduce size of array from initialization, if not already known.  */
271518334Speter  if (TREE_CODE (type) == ARRAY_TYPE
271618334Speter      && TYPE_DOMAIN (type) == 0
271718334Speter      && TREE_CODE (decl) != TYPE_DECL)
271818334Speter    {
271918334Speter      int do_default
272018334Speter	= (TREE_STATIC (decl)
272118334Speter	   /* Even if pedantic, an external linkage array
272218334Speter	      may have incomplete type at first.  */
272318334Speter	   ? pedantic && !TREE_PUBLIC (decl)
272418334Speter	   : !DECL_EXTERNAL (decl));
272518334Speter      int failure
272618334Speter	= complete_array_type (type, DECL_INITIAL (decl), do_default);
272718334Speter
272818334Speter      /* Get the completed type made by complete_array_type.  */
272918334Speter      type = TREE_TYPE (decl);
273018334Speter
273118334Speter      if (failure == 1)
2732132730Skan	error ("%Jinitializer fails to determine size of '%D'", decl, decl);
273318334Speter
273490075Sobrien      else if (failure == 2)
273518334Speter	{
273618334Speter	  if (do_default)
2737132730Skan	    error ("%Jarray size missing in '%D'", decl, decl);
273818334Speter	  /* If a `static' var's size isn't known,
273918334Speter	     make it extern as well as static, so it does not get
274018334Speter	     allocated.
274118334Speter	     If it is not `static', then do not mark extern;
274218334Speter	     finish_incomplete_decl will give it a default size
274318334Speter	     and it will get allocated.  */
274418334Speter	  else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
274518334Speter	    DECL_EXTERNAL (decl) = 1;
274618334Speter	}
274718334Speter
274818334Speter      /* TYPE_MAX_VALUE is always one less than the number of elements
274918334Speter	 in the array, because we start counting at zero.  Therefore,
275018334Speter	 warn only if the value is less than zero.  */
275190075Sobrien      else if (pedantic && TYPE_DOMAIN (type) != 0
275290075Sobrien	      && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2753132730Skan	error ("%Jzero or negative size array '%D'", decl, decl);
275418334Speter
275518334Speter      layout_decl (decl, 0);
275618334Speter    }
275718334Speter
275818334Speter  if (TREE_CODE (decl) == VAR_DECL)
275918334Speter    {
276090075Sobrien      if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
276190075Sobrien	  && COMPLETE_TYPE_P (TREE_TYPE (decl)))
276218334Speter	layout_decl (decl, 0);
276318334Speter
276418334Speter      if (DECL_SIZE (decl) == 0
276590075Sobrien	  /* Don't give an error if we already gave one earlier.  */
276690075Sobrien	  && TREE_TYPE (decl) != error_mark_node
276718334Speter	  && (TREE_STATIC (decl)
276818334Speter	      ?
276918334Speter		/* A static variable with an incomplete type
277018334Speter		   is an error if it is initialized.
277118334Speter		   Also if it is not file scope.
277218334Speter		   Otherwise, let it through, but if it is not `extern'
277318334Speter		   then it may cause an error message later.  */
277450397Sobrien		(DECL_INITIAL (decl) != 0
2775132730Skan		 || !DECL_FILE_SCOPE_P (decl))
277618334Speter	      :
277718334Speter		/* An automatic variable with an incomplete type
277818334Speter		   is an error.  */
277918334Speter		!DECL_EXTERNAL (decl)))
278018334Speter	{
2781132730Skan	  error ("%Jstorage size of '%D' isn't known", decl, decl);
278218334Speter	  TREE_TYPE (decl) = error_mark_node;
278318334Speter	}
278418334Speter
278518334Speter      if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
278618334Speter	  && DECL_SIZE (decl) != 0)
278718334Speter	{
278818334Speter	  if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
278918334Speter	    constant_expression_warning (DECL_SIZE (decl));
279018334Speter	  else
2791132730Skan	    error ("%Jstorage size of '%D' isn't constant", decl, decl);
279218334Speter	}
279350397Sobrien
279490075Sobrien      if (TREE_USED (type))
279550397Sobrien	TREE_USED (decl) = 1;
279618334Speter    }
279718334Speter
2798132730Skan  /* If this is a function and an assembler name is specified, reset DECL_RTL
2799132730Skan     so we can give it its new name.  Also, update built_in_decls if it
2800132730Skan     was a normal built-in.  */
280118334Speter  if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
280290075Sobrien    {
2803132730Skan      /* ASMSPEC is given, and not the name of a register.  Mark the
2804132730Skan      name with a star so assemble_name won't munge it.  */
2805132730Skan      char *starred = alloca (strlen (asmspec) + 2);
2806132730Skan      starred[0] = '*';
2807132730Skan      strcpy (starred + 1, asmspec);
2808132730Skan
2809132730Skan      if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2810132730Skan	{
2811132730Skan	  tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2812132730Skan	  SET_DECL_RTL (builtin, NULL_RTX);
2813132730Skan	  SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2814132730Skan#ifdef TARGET_MEM_FUNCTIONS
2815132730Skan	  if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2816132730Skan	    init_block_move_fn (starred);
2817132730Skan	  else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2818132730Skan	    init_block_clear_fn (starred);
2819132730Skan#else
2820132730Skan	  if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2821132730Skan	    init_block_move_fn (starred);
2822132730Skan	  else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2823132730Skan	    init_block_clear_fn (starred);
2824132730Skan#endif
2825132730Skan	}
282690075Sobrien      SET_DECL_RTL (decl, NULL_RTX);
2827132730Skan      change_decl_assembler_name (decl, get_identifier (starred));
282890075Sobrien    }
282918334Speter
2830132730Skan  /* If #pragma weak was used, mark the decl weak now.  */
2831132730Skan  if (current_scope == global_scope)
2832132730Skan    maybe_apply_pragma_weak (decl);
2833132730Skan
283418334Speter  /* Output the assembler code and/or RTL code for variables and functions,
283518334Speter     unless the type is an undefined structure or union.
283618334Speter     If not, it will get done when the type is completed.  */
283718334Speter
283818334Speter  if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
283918334Speter    {
2840117421Skan      /* This is a no-op in c-lang.c or something real in objc-act.c.  */
2841132730Skan      if (c_dialect_objc ())
2842117421Skan	objc_check_decl (decl);
284390075Sobrien
2844132730Skan      if (DECL_FILE_SCOPE_P (decl))
284518334Speter	{
284690075Sobrien	  if (DECL_INITIAL (decl) == NULL_TREE
284790075Sobrien	      || DECL_INITIAL (decl) == error_mark_node)
284890075Sobrien	    /* Don't output anything
284990075Sobrien	       when a tentative file-scope definition is seen.
285090075Sobrien	       But at end of compilation, do output code for them.  */
285190075Sobrien	    DECL_DEFER_OUTPUT (decl) = 1;
2852132730Skan	  rest_of_decl_compilation (decl, asmspec, true, 0);
285318334Speter	}
285418334Speter      else
285518334Speter	{
285690075Sobrien	  /* This is a local variable.  If there is an ASMSPEC, the
285790075Sobrien	     user has requested that we handle it specially.  */
285890075Sobrien	  if (asmspec)
285990075Sobrien	    {
286090075Sobrien	      /* In conjunction with an ASMSPEC, the `register'
286190075Sobrien		 keyword indicates that we should place the variable
286290075Sobrien		 in a particular register.  */
286390075Sobrien	      if (DECL_REGISTER (decl))
286490075Sobrien		DECL_C_HARD_REGISTER (decl) = 1;
286590075Sobrien
286690075Sobrien	      /* If this is not a static variable, issue a warning.
286790075Sobrien		 It doesn't make any sense to give an ASMSPEC for an
286890075Sobrien		 ordinary, non-register local variable.  Historically,
286990075Sobrien		 GCC has accepted -- but ignored -- the ASMSPEC in
287090075Sobrien		 this case.  */
2871132730Skan	      if (TREE_CODE (decl) == VAR_DECL
287290075Sobrien		  && !DECL_REGISTER (decl)
287390075Sobrien		  && !TREE_STATIC (decl))
2874132730Skan		warning ("%Jignoring asm-specifier for non-static local "
2875132730Skan                         "variable '%D'", decl, decl);
287690075Sobrien	      else
2877132730Skan		change_decl_assembler_name (decl, get_identifier (asmspec));
287890075Sobrien	    }
287990075Sobrien
288090075Sobrien	  if (TREE_CODE (decl) != FUNCTION_DECL)
288190075Sobrien	    add_decl_stmt (decl);
288218334Speter	}
288390075Sobrien
2884132730Skan      if (!DECL_FILE_SCOPE_P (decl))
288518334Speter	{
288618334Speter	  /* Recompute the RTL of a local array now
288718334Speter	     if it used to be an incomplete type.  */
288818334Speter	  if (was_incomplete
288918334Speter	      && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
289018334Speter	    {
289118334Speter	      /* If we used it already as memory, it must stay in memory.  */
289218334Speter	      TREE_ADDRESSABLE (decl) = TREE_USED (decl);
289318334Speter	      /* If it's still incomplete now, no init will save it.  */
289418334Speter	      if (DECL_SIZE (decl) == 0)
289518334Speter		DECL_INITIAL (decl) = 0;
289618334Speter	    }
289718334Speter	}
289818334Speter    }
289918334Speter
2900132730Skan  /* If this was marked 'used', be sure it will be output.  */
2901132730Skan  if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2902132730Skan    mark_referenced (DECL_ASSEMBLER_NAME (decl));
2903132730Skan
290418334Speter  if (TREE_CODE (decl) == TYPE_DECL)
2905132730Skan    rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
290618334Speter
290718334Speter  /* At the end of a declaration, throw away any variable type sizes
290818334Speter     of types defined inside that declaration.  There is no use
290918334Speter     computing them in the following function definition.  */
2910132730Skan  if (current_scope == global_scope)
291118334Speter    get_pending_sizes ();
291218334Speter
2913117421Skan  /* Install a cleanup (aka destructor) if one was given.  */
2914117421Skan  if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2915117421Skan    {
2916117421Skan      tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2917117421Skan      if (attr)
2918117421Skan	{
2919117421Skan	  static bool eh_initialized_p;
292018334Speter
2921117421Skan	  tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2922117421Skan	  tree cleanup_decl = lookup_name (cleanup_id);
2923117421Skan	  tree cleanup;
2924117421Skan
2925117421Skan	  /* Build "cleanup(&decl)" for the destructor.  */
2926117421Skan	  cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2927117421Skan	  cleanup = build_tree_list (NULL_TREE, cleanup);
2928117421Skan	  cleanup = build_function_call (cleanup_decl, cleanup);
2929117421Skan
2930117421Skan	  /* Don't warn about decl unused; the cleanup uses it.  */
2931117421Skan	  TREE_USED (decl) = 1;
2932117421Skan
2933117421Skan	  /* Initialize EH, if we've been told to do so.  */
2934117421Skan	  if (flag_exceptions && !eh_initialized_p)
2935117421Skan	    {
2936117421Skan	      eh_initialized_p = true;
2937117421Skan	      eh_personality_libfunc
2938117421Skan		= init_one_libfunc (USING_SJLJ_EXCEPTIONS
2939117421Skan				    ? "__gcc_personality_sj0"
2940117421Skan				    : "__gcc_personality_v0");
2941117421Skan	      using_eh_for_cleanups ();
2942117421Skan	    }
2943117421Skan
2944117421Skan	  add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2945117421Skan	}
2946117421Skan    }
294718334Speter}
294818334Speter
2949132730Skan/* Given a parsed parameter declaration, decode it into a PARM_DECL
2950132730Skan   and push that on the current scope.  */
295118334Speter
295218334Spetervoid
2953132730Skanpush_parm_decl (tree parm)
295418334Speter{
295518334Speter  tree decl;
2956132730Skan
2957132730Skan  /* Don't attempt to expand sizes while parsing this decl.
2958132730Skan     (We can get here with i_s_e 1 somehow from Objective-C.)  */
2959132730Skan  int save_immediate_size_expand = immediate_size_expand;
296018334Speter  immediate_size_expand = 0;
296118334Speter
296218334Speter  decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2963132730Skan			 TREE_PURPOSE (TREE_PURPOSE (parm)),
2964132730Skan			 PARM, 0, NULL);
296590075Sobrien  decl_attributes (&decl, TREE_VALUE (parm), 0);
296618334Speter
296718334Speter  decl = pushdecl (decl);
296818334Speter
2969132730Skan  finish_decl (decl, NULL_TREE, NULL_TREE);
297018334Speter
2971132730Skan  immediate_size_expand = save_immediate_size_expand;
297218334Speter}
297318334Speter
2974132730Skan/* Mark all the parameter declarations to date as forward decls,
2975132730Skan   shift them to the variables list, and reset the parameters list.
2976132730Skan   Also diagnose use of this extension.  */
297718334Speter
297818334Spetervoid
2979132730Skanmark_forward_parm_decls (void)
298018334Speter{
2981132730Skan  tree parm;
2982132730Skan
2983132730Skan  if (pedantic && !current_scope->warned_forward_parm_decls)
2984132730Skan    {
2985132730Skan      pedwarn ("ISO C forbids forward parameter declarations");
2986132730Skan      current_scope->warned_forward_parm_decls = true;
2987132730Skan    }
2988132730Skan
2989132730Skan  for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
2990132730Skan    TREE_ASM_WRITTEN (parm) = 1;
2991132730Skan
2992132730Skan  SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
2993132730Skan  current_scope->parms = 0;
2994132730Skan  current_scope->parms_last = 0;
299518334Speter}
299618334Speter
2997132730Skanstatic GTY(()) int compound_literal_number;
2998132730Skan
299990075Sobrien/* Build a COMPOUND_LITERAL_EXPR.  TYPE is the type given in the compound
300090075Sobrien   literal, which may be an incomplete array type completed by the
300190075Sobrien   initializer; INIT is a CONSTRUCTOR that initializes the compound
300290075Sobrien   literal.  */
300390075Sobrien
300490075Sobrientree
3005132730Skanbuild_compound_literal (tree type, tree init)
300690075Sobrien{
300790075Sobrien  /* We do not use start_decl here because we have a type, not a declarator;
300890075Sobrien     and do not use finish_decl because the decl should be stored inside
300990075Sobrien     the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT.  */
301090075Sobrien  tree decl = build_decl (VAR_DECL, NULL_TREE, type);
301190075Sobrien  tree complit;
301290075Sobrien  tree stmt;
301390075Sobrien  DECL_EXTERNAL (decl) = 0;
301490075Sobrien  TREE_PUBLIC (decl) = 0;
3015132730Skan  TREE_STATIC (decl) = (current_scope == global_scope);
301690075Sobrien  DECL_CONTEXT (decl) = current_function_decl;
301790075Sobrien  TREE_USED (decl) = 1;
301890075Sobrien  TREE_TYPE (decl) = type;
3019102790Skan  TREE_READONLY (decl) = TREE_READONLY (type);
302090075Sobrien  store_init_value (decl, init);
302190075Sobrien
302290075Sobrien  if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
302390075Sobrien    {
302490075Sobrien      int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
302590075Sobrien      if (failure)
302690075Sobrien	abort ();
302790075Sobrien    }
302890075Sobrien
302990075Sobrien  type = TREE_TYPE (decl);
303090075Sobrien  if (type == error_mark_node || !COMPLETE_TYPE_P (type))
303190075Sobrien    return error_mark_node;
303290075Sobrien
303390075Sobrien  stmt = build_stmt (DECL_STMT, decl);
303490075Sobrien  complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
303590075Sobrien  TREE_SIDE_EFFECTS (complit) = 1;
303690075Sobrien
303790075Sobrien  layout_decl (decl, 0);
303890075Sobrien
303990075Sobrien  if (TREE_STATIC (decl))
304090075Sobrien    {
304190075Sobrien      /* This decl needs a name for the assembler output.  We also need
3042102790Skan	 a unique suffix to be added to the name.  */
3043102790Skan      char *name;
3044102790Skan
3045132730Skan      ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3046132730Skan			       compound_literal_number);
3047132730Skan      compound_literal_number++;
3048102790Skan      DECL_NAME (decl) = get_identifier (name);
3049102790Skan      DECL_DEFER_OUTPUT (decl) = 1;
3050102790Skan      DECL_COMDAT (decl) = 1;
3051102790Skan      DECL_ARTIFICIAL (decl) = 1;
3052102790Skan      pushdecl (decl);
305390075Sobrien      rest_of_decl_compilation (decl, NULL, 1, 0);
305490075Sobrien    }
305590075Sobrien
305690075Sobrien  return complit;
305790075Sobrien}
305890075Sobrien
305918334Speter/* Make TYPE a complete type based on INITIAL_VALUE.
306018334Speter   Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
306118334Speter   2 if there was no information (in which case assume 1 if DO_DEFAULT).  */
306218334Speter
306318334Speterint
3064132730Skancomplete_array_type (tree type, tree initial_value, int do_default)
306518334Speter{
306690075Sobrien  tree maxindex = NULL_TREE;
306718334Speter  int value = 0;
306818334Speter
306918334Speter  if (initial_value)
307018334Speter    {
307118334Speter      /* Note MAXINDEX  is really the maximum index,
307218334Speter	 one less than the size.  */
307318334Speter      if (TREE_CODE (initial_value) == STRING_CST)
307418334Speter	{
307518334Speter	  int eltsize
307618334Speter	    = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
307718334Speter	  maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
307818334Speter				   / eltsize) - 1, 0);
307918334Speter	}
308018334Speter      else if (TREE_CODE (initial_value) == CONSTRUCTOR)
308118334Speter	{
308218334Speter	  tree elts = CONSTRUCTOR_ELTS (initial_value);
308390075Sobrien	  maxindex = build_int_2 (-1, -1);
308418334Speter	  for (; elts; elts = TREE_CHAIN (elts))
308518334Speter	    {
308618334Speter	      if (TREE_PURPOSE (elts))
308718334Speter		maxindex = TREE_PURPOSE (elts);
308818334Speter	      else
308990075Sobrien		maxindex = fold (build (PLUS_EXPR, integer_type_node,
309090075Sobrien					maxindex, integer_one_node));
309118334Speter	    }
309218334Speter	  maxindex = copy_node (maxindex);
309318334Speter	}
309418334Speter      else
309518334Speter	{
309618334Speter	  /* Make an error message unless that happened already.  */
309718334Speter	  if (initial_value != error_mark_node)
309818334Speter	    value = 1;
309918334Speter
310018334Speter	  /* Prevent further error messages.  */
310118334Speter	  maxindex = build_int_2 (0, 0);
310218334Speter	}
310318334Speter    }
310418334Speter
310518334Speter  if (!maxindex)
310618334Speter    {
310718334Speter      if (do_default)
310818334Speter	maxindex = build_int_2 (0, 0);
310918334Speter      value = 2;
311018334Speter    }
311118334Speter
311218334Speter  if (maxindex)
311318334Speter    {
311418334Speter      TYPE_DOMAIN (type) = build_index_type (maxindex);
311518334Speter      if (!TREE_TYPE (maxindex))
311618334Speter	TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
311718334Speter    }
311818334Speter
311918334Speter  /* Lay out the type now that we can get the real answer.  */
312018334Speter
312118334Speter  layout_type (type);
312218334Speter
312318334Speter  return value;
312418334Speter}
312518334Speter
3126117421Skan/* Determine whether TYPE is a structure with a flexible array member,
3127117421Skan   or a union containing such a structure (possibly recursively).  */
3128117421Skan
3129117421Skanstatic bool
3130132730Skanflexible_array_type_p (tree type)
3131117421Skan{
3132117421Skan  tree x;
3133117421Skan  switch (TREE_CODE (type))
3134117421Skan    {
3135117421Skan    case RECORD_TYPE:
3136117421Skan      x = TYPE_FIELDS (type);
3137117421Skan      if (x == NULL_TREE)
3138117421Skan	return false;
3139117421Skan      while (TREE_CHAIN (x) != NULL_TREE)
3140117421Skan	x = TREE_CHAIN (x);
3141117421Skan      if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3142117421Skan	  && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3143117421Skan	  && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3144117421Skan	  && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3145117421Skan	return true;
3146117421Skan      return false;
3147117421Skan    case UNION_TYPE:
3148117421Skan      for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3149117421Skan	{
3150117421Skan	  if (flexible_array_type_p (TREE_TYPE (x)))
3151117421Skan	    return true;
3152117421Skan	}
3153117421Skan      return false;
3154117421Skan    default:
3155117421Skan    return false;
3156117421Skan  }
3157117421Skan}
3158117421Skan
3159132730Skan/* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3160132730Skan   replacing with appropriate values if they are invalid.  */
3161132730Skanstatic void
3162132730Skancheck_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3163132730Skan{
3164132730Skan  tree type_mv;
3165132730Skan  unsigned int max_width;
3166132730Skan  unsigned HOST_WIDE_INT w;
3167132730Skan  const char *name = orig_name ? orig_name: _("<anonymous>");
3168132730Skan
3169132730Skan  /* Necessary?  */
3170132730Skan  STRIP_NOPS (*width);
3171132730Skan
3172132730Skan  /* Detect and ignore out of range field width and process valid
3173132730Skan     field widths.  */
3174132730Skan  if (TREE_CODE (*width) != INTEGER_CST)
3175132730Skan    {
3176132730Skan      error ("bit-field `%s' width not an integer constant", name);
3177132730Skan      *width = integer_one_node;
3178132730Skan    }
3179132730Skan  else
3180132730Skan    {
3181132730Skan      constant_expression_warning (*width);
3182132730Skan      if (tree_int_cst_sgn (*width) < 0)
3183132730Skan	{
3184132730Skan	  error ("negative width in bit-field `%s'", name);
3185132730Skan	  *width = integer_one_node;
3186132730Skan	}
3187132730Skan      else if (integer_zerop (*width) && orig_name)
3188132730Skan	{
3189132730Skan	  error ("zero width for bit-field `%s'", name);
3190132730Skan	  *width = integer_one_node;
3191132730Skan	}
3192132730Skan    }
3193132730Skan
3194132730Skan  /* Detect invalid bit-field type.  */
3195132730Skan  if (TREE_CODE (*type) != INTEGER_TYPE
3196132730Skan      && TREE_CODE (*type) != BOOLEAN_TYPE
3197132730Skan      && TREE_CODE (*type) != ENUMERAL_TYPE)
3198132730Skan    {
3199132730Skan      error ("bit-field `%s' has invalid type", name);
3200132730Skan      *type = unsigned_type_node;
3201132730Skan    }
3202132730Skan
3203132730Skan  type_mv = TYPE_MAIN_VARIANT (*type);
3204132730Skan  if (pedantic
3205132730Skan      && type_mv != integer_type_node
3206132730Skan      && type_mv != unsigned_type_node
3207132730Skan      && type_mv != boolean_type_node)
3208132730Skan    pedwarn ("type of bit-field `%s' is a GCC extension", name);
3209132730Skan
3210132730Skan  if (type_mv == boolean_type_node)
3211132730Skan    max_width = CHAR_TYPE_SIZE;
3212132730Skan  else
3213132730Skan    max_width = TYPE_PRECISION (*type);
3214132730Skan
3215132730Skan  if (0 < compare_tree_int (*width, max_width))
3216132730Skan    {
3217132730Skan      error ("width of `%s' exceeds its type", name);
3218132730Skan      w = max_width;
3219132730Skan      *width = build_int_2 (w, 0);
3220132730Skan    }
3221132730Skan  else
3222132730Skan    w = tree_low_cst (*width, 1);
3223132730Skan
3224132730Skan  if (TREE_CODE (*type) == ENUMERAL_TYPE
3225132730Skan      && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3226132730Skan	  || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3227132730Skan    warning ("`%s' is narrower than values of its type", name);
3228132730Skan}
3229132730Skan
323018334Speter/* Given declspecs and a declarator,
323118334Speter   determine the name and type of the object declared
323218334Speter   and construct a ..._DECL node for it.
323318334Speter   (In one case we can return a ..._TYPE node instead.
323418334Speter    For invalid input we sometimes return 0.)
323518334Speter
323618334Speter   DECLSPECS is a chain of tree_list nodes whose value fields
323718334Speter    are the storage classes and type specifiers.
323818334Speter
323918334Speter   DECL_CONTEXT says which syntactic context this declaration is in:
324018334Speter     NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
324118334Speter     FUNCDEF for a function definition.  Like NORMAL but a few different
324218334Speter      error messages in each case.  Return value may be zero meaning
324318334Speter      this definition is too screwy to try to parse.
324418334Speter     PARM for a parameter declaration (either within a function prototype
324518334Speter      or before a function body).  Make a PARM_DECL, or return void_type_node.
324618334Speter     TYPENAME if for a typename (in a cast or sizeof).
324718334Speter      Don't make a DECL node; just return the ..._TYPE node.
324818334Speter     FIELD for a struct or union field; make a FIELD_DECL.
324918334Speter   INITIALIZED is 1 if the decl has an initializer.
3250132730Skan   WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3251132730Skan   representing the width of the bit-field.
325218334Speter
325318334Speter   In the TYPENAME case, DECLARATOR is really an absolute declarator.
325418334Speter   It may also be so in the PARM case, for a prototype where the
325518334Speter   argument type is specified but not the name.
325618334Speter
325718334Speter   This function is where the complicated C meanings of `static'
325818334Speter   and `extern' are interpreted.  */
325918334Speter
326018334Speterstatic tree
3261132730Skangrokdeclarator (tree declarator, tree declspecs,
3262132730Skan		enum decl_context decl_context, int initialized, tree *width)
326318334Speter{
326418334Speter  int specbits = 0;
326518334Speter  tree spec;
326618334Speter  tree type = NULL_TREE;
326718334Speter  int longlong = 0;
326818334Speter  int constp;
326952284Sobrien  int restrictp;
327018334Speter  int volatilep;
327152284Sobrien  int type_quals = TYPE_UNQUALIFIED;
327218334Speter  int inlinep;
327318334Speter  int explicit_int = 0;
327418334Speter  int explicit_char = 0;
327518334Speter  int defaulted_int = 0;
327618334Speter  tree typedef_decl = 0;
3277132730Skan  const char *name, *orig_name;
327818334Speter  tree typedef_type = 0;
327918334Speter  int funcdef_flag = 0;
328018334Speter  enum tree_code innermost_code = ERROR_MARK;
328118334Speter  int size_varies = 0;
328290075Sobrien  tree decl_attr = NULL_TREE;
328390075Sobrien  tree array_ptr_quals = NULL_TREE;
328490075Sobrien  int array_parm_static = 0;
328590075Sobrien  tree returned_attrs = NULL_TREE;
3286132730Skan  bool bitfield = width != NULL;
3287132730Skan  tree element_type;
328818334Speter
328918334Speter  if (decl_context == FUNCDEF)
329018334Speter    funcdef_flag = 1, decl_context = NORMAL;
329118334Speter
329218334Speter  /* Look inside a declarator for the name being declared
329318334Speter     and get it as a string, for an error message.  */
329418334Speter  {
329590075Sobrien    tree decl = declarator;
329618334Speter    name = 0;
329718334Speter
329818334Speter    while (decl)
329918334Speter      switch (TREE_CODE (decl))
330018334Speter	{
330118334Speter	case ARRAY_REF:
330218334Speter	case INDIRECT_REF:
330318334Speter	case CALL_EXPR:
330418334Speter	  innermost_code = TREE_CODE (decl);
330518334Speter	  decl = TREE_OPERAND (decl, 0);
330618334Speter	  break;
330718334Speter
330890075Sobrien	case TREE_LIST:
330990075Sobrien	  decl = TREE_VALUE (decl);
331090075Sobrien	  break;
331190075Sobrien
331218334Speter	case IDENTIFIER_NODE:
331318334Speter	  name = IDENTIFIER_POINTER (decl);
331418334Speter	  decl = 0;
331518334Speter	  break;
331618334Speter
331718334Speter	default:
331818334Speter	  abort ();
331918334Speter	}
3320132730Skan    orig_name = name;
332118334Speter    if (name == 0)
332218334Speter      name = "type name";
332318334Speter  }
332418334Speter
332518334Speter  /* A function definition's declarator must have the form of
332618334Speter     a function declarator.  */
332718334Speter
332818334Speter  if (funcdef_flag && innermost_code != CALL_EXPR)
332918334Speter    return 0;
333018334Speter
333118334Speter  /* If this looks like a function definition, make it one,
333218334Speter     even if it occurs where parms are expected.
333318334Speter     Then store_parm_decls will reject it and not use it as a parm.  */
333418334Speter  if (decl_context == NORMAL && !funcdef_flag
3335132730Skan      && current_scope->parm_flag)
333618334Speter    decl_context = PARM;
333718334Speter
333818334Speter  /* Look through the decl specs and record which ones appear.
333918334Speter     Some typespecs are defined as built-in typenames.
334018334Speter     Others, the ones that are modifiers of other types,
334118334Speter     are represented by bits in SPECBITS: set the bits for
334218334Speter     the modifiers that appear.  Storage class keywords are also in SPECBITS.
334318334Speter
334418334Speter     If there is a typedef name or a type, store the type in TYPE.
334518334Speter     This includes builtin typedefs such as `int'.
334618334Speter
334718334Speter     Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
334818334Speter     and did not come from a user typedef.
334918334Speter
335018334Speter     Set LONGLONG if `long' is mentioned twice.  */
335118334Speter
335218334Speter  for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
335318334Speter    {
335490075Sobrien      tree id = TREE_VALUE (spec);
335518334Speter
335690075Sobrien      /* If the entire declaration is itself tagged as deprecated then
335790075Sobrien         suppress reports of deprecated items.  */
335890075Sobrien      if (id && TREE_DEPRECATED (id))
335990075Sobrien        {
336090075Sobrien	  if (deprecated_state != DEPRECATED_SUPPRESS)
336190075Sobrien	    warn_deprecated_use (id);
336290075Sobrien        }
336390075Sobrien
336418334Speter      if (id == ridpointers[(int) RID_INT])
336518334Speter	explicit_int = 1;
336618334Speter      if (id == ridpointers[(int) RID_CHAR])
336718334Speter	explicit_char = 1;
336818334Speter
336990075Sobrien      if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
337090075Sobrien	{
337190075Sobrien	  enum rid i = C_RID_CODE (id);
337290075Sobrien	  if ((int) i <= (int) RID_LAST_MODIFIER)
337390075Sobrien	    {
3374117421Skan	      if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
337590075Sobrien		{
337690075Sobrien		  if (longlong)
337790075Sobrien		    error ("`long long long' is too long for GCC");
337890075Sobrien		  else
337990075Sobrien		    {
338090075Sobrien		      if (pedantic && !flag_isoc99 && ! in_system_header
338190075Sobrien			  && warn_long_long)
3382117421Skan			pedwarn ("ISO C90 does not support `long long'");
338390075Sobrien		      longlong = 1;
338490075Sobrien		    }
338590075Sobrien		}
338690075Sobrien	      else if (specbits & (1 << (int) i))
3387117421Skan		{
3388117421Skan		  if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3389117421Skan		    {
3390132730Skan		      if (pedantic && !flag_isoc99)
3391117421Skan			pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3392117421Skan		    }
3393117421Skan		  else
3394117421Skan		    error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3395117421Skan		}
3396117421Skan
3397117421Skan	      /* Diagnose "__thread extern".  Recall that this list
3398117421Skan		 is in the reverse order seen in the text.  */
3399117421Skan	      if (i == RID_THREAD
3400117421Skan		  && (specbits & (1 << (int) RID_EXTERN
3401117421Skan				  | 1 << (int) RID_STATIC)))
3402117421Skan		{
3403117421Skan		  if (specbits & 1 << (int) RID_EXTERN)
3404117421Skan		    error ("`__thread' before `extern'");
3405117421Skan		  else
3406117421Skan		    error ("`__thread' before `static'");
3407117421Skan		}
3408117421Skan
340990075Sobrien	      specbits |= 1 << (int) i;
341090075Sobrien	      goto found;
341190075Sobrien	    }
341290075Sobrien	}
341318334Speter      if (type)
341418334Speter	error ("two or more data types in declaration of `%s'", name);
341518334Speter      /* Actual typedefs come to us as TYPE_DECL nodes.  */
341618334Speter      else if (TREE_CODE (id) == TYPE_DECL)
341718334Speter	{
341890075Sobrien	  if (TREE_TYPE (id) == error_mark_node)
341990075Sobrien	    ; /* Allow the type to default to int to avoid cascading errors.  */
342090075Sobrien	  else
342190075Sobrien	    {
342290075Sobrien	      type = TREE_TYPE (id);
342390075Sobrien	      decl_attr = DECL_ATTRIBUTES (id);
342490075Sobrien	      typedef_decl = id;
342590075Sobrien	    }
342618334Speter	}
342718334Speter      /* Built-in types come as identifiers.  */
342818334Speter      else if (TREE_CODE (id) == IDENTIFIER_NODE)
342918334Speter	{
343090075Sobrien	  tree t = lookup_name (id);
343118334Speter	  if (TREE_TYPE (t) == error_mark_node)
343218334Speter	    ;
343318334Speter	  else if (!t || TREE_CODE (t) != TYPE_DECL)
343418334Speter	    error ("`%s' fails to be a typedef or built in type",
343518334Speter		   IDENTIFIER_POINTER (id));
343618334Speter	  else
343718334Speter	    {
343818334Speter	      type = TREE_TYPE (t);
343918334Speter	      typedef_decl = t;
344018334Speter	    }
344118334Speter	}
344218334Speter      else if (TREE_CODE (id) != ERROR_MARK)
344318334Speter	type = id;
344418334Speter
344590075Sobrien    found:
344690075Sobrien      ;
344718334Speter    }
344818334Speter
344918334Speter  typedef_type = type;
345018334Speter  if (type)
345118334Speter    size_varies = C_TYPE_VARIABLE_SIZE (type);
345218334Speter
345318334Speter  /* No type at all: default to `int', and set DEFAULTED_INT
345418334Speter     because it was not a user-defined typedef.  */
345518334Speter
345618334Speter  if (type == 0)
345718334Speter    {
345850397Sobrien      if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
345950397Sobrien			  | (1 << (int) RID_SIGNED)
346090075Sobrien			  | (1 << (int) RID_UNSIGNED)
346190075Sobrien			  | (1 << (int) RID_COMPLEX))))
346250397Sobrien	  /* Don't warn about typedef foo = bar.  */
346350397Sobrien	  && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
346490075Sobrien	  && ! in_system_header)
346550397Sobrien	{
346690075Sobrien	  /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
346752284Sobrien	     and this is a function, or if -Wimplicit; prefer the former
346852284Sobrien	     warning since it is more explicit.  */
346990075Sobrien	  if ((warn_implicit_int || warn_return_type || flag_isoc99)
347090075Sobrien	      && funcdef_flag)
347150397Sobrien	    warn_about_return_type = 1;
347290075Sobrien	  else if (warn_implicit_int || flag_isoc99)
347390075Sobrien	    pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
347490075Sobrien			 name);
347550397Sobrien	}
347650397Sobrien
347718334Speter      defaulted_int = 1;
347818334Speter      type = integer_type_node;
347918334Speter    }
348018334Speter
348118334Speter  /* Now process the modifiers that were specified
348218334Speter     and check for invalid combinations.  */
348318334Speter
348418334Speter  /* Long double is a special combination.  */
348518334Speter
348650397Sobrien  if ((specbits & 1 << (int) RID_LONG) && ! longlong
348718334Speter      && TYPE_MAIN_VARIANT (type) == double_type_node)
348818334Speter    {
348990075Sobrien      specbits &= ~(1 << (int) RID_LONG);
349018334Speter      type = long_double_type_node;
349118334Speter    }
349218334Speter
349318334Speter  /* Check all other uses of type modifiers.  */
349418334Speter
349518334Speter  if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
349618334Speter		  | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
349718334Speter    {
349818334Speter      int ok = 0;
349918334Speter
350050397Sobrien      if ((specbits & 1 << (int) RID_LONG)
350150397Sobrien	  && (specbits & 1 << (int) RID_SHORT))
350250397Sobrien	error ("both long and short specified for `%s'", name);
350318334Speter      else if (((specbits & 1 << (int) RID_LONG)
350418334Speter		|| (specbits & 1 << (int) RID_SHORT))
350518334Speter	       && explicit_char)
350618334Speter	error ("long or short specified with char for `%s'", name);
350718334Speter      else if (((specbits & 1 << (int) RID_LONG)
350818334Speter		|| (specbits & 1 << (int) RID_SHORT))
350918334Speter	       && TREE_CODE (type) == REAL_TYPE)
351050397Sobrien	{
351150397Sobrien	  static int already = 0;
351250397Sobrien
351350397Sobrien	  error ("long or short specified with floating type for `%s'", name);
351450397Sobrien	  if (! already && ! pedantic)
351550397Sobrien	    {
351650397Sobrien	      error ("the only valid combination is `long double'");
351750397Sobrien	      already = 1;
351850397Sobrien	    }
351950397Sobrien	}
352018334Speter      else if ((specbits & 1 << (int) RID_SIGNED)
352118334Speter	       && (specbits & 1 << (int) RID_UNSIGNED))
352250397Sobrien	error ("both signed and unsigned specified for `%s'", name);
352350397Sobrien      else if (TREE_CODE (type) != INTEGER_TYPE)
352450397Sobrien	error ("long, short, signed or unsigned invalid for `%s'", name);
352518334Speter      else
352618334Speter	{
352718334Speter	  ok = 1;
3528117421Skan	  if (!explicit_int && !defaulted_int && !explicit_char)
352918334Speter	    {
3530117421Skan	      error ("long, short, signed or unsigned used invalidly for `%s'",
3531117421Skan		     name);
3532117421Skan	      ok = 0;
353318334Speter	    }
353418334Speter	}
353518334Speter
353618334Speter      /* Discard the type modifiers if they are invalid.  */
353718334Speter      if (! ok)
353818334Speter	{
353918334Speter	  specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
354018334Speter			| (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
354118334Speter	  longlong = 0;
354218334Speter	}
354318334Speter    }
354418334Speter
354518334Speter  if ((specbits & (1 << (int) RID_COMPLEX))
354618334Speter      && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
354718334Speter    {
354818334Speter      error ("complex invalid for `%s'", name);
354990075Sobrien      specbits &= ~(1 << (int) RID_COMPLEX);
355018334Speter    }
355118334Speter
355218334Speter  /* Decide whether an integer type is signed or not.
3553132730Skan     Optionally treat bit-fields as signed by default.  */
355418334Speter  if (specbits & 1 << (int) RID_UNSIGNED
355518334Speter      || (bitfield && ! flag_signed_bitfields
355618334Speter	  && (explicit_int || defaulted_int || explicit_char
355718334Speter	      /* A typedef for plain `int' without `signed'
355818334Speter		 can be controlled just like plain `int'.  */
355918334Speter	      || ! (typedef_decl != 0
356018334Speter		    && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
356118334Speter	  && TREE_CODE (type) != ENUMERAL_TYPE
356218334Speter	  && !(specbits & 1 << (int) RID_SIGNED)))
356318334Speter    {
356418334Speter      if (longlong)
356518334Speter	type = long_long_unsigned_type_node;
356618334Speter      else if (specbits & 1 << (int) RID_LONG)
356718334Speter	type = long_unsigned_type_node;
356818334Speter      else if (specbits & 1 << (int) RID_SHORT)
356918334Speter	type = short_unsigned_type_node;
357018334Speter      else if (type == char_type_node)
357118334Speter	type = unsigned_char_type_node;
357218334Speter      else if (typedef_decl)
3573117421Skan	type = c_common_unsigned_type (type);
357418334Speter      else
357518334Speter	type = unsigned_type_node;
357618334Speter    }
357718334Speter  else if ((specbits & 1 << (int) RID_SIGNED)
357818334Speter	   && type == char_type_node)
357918334Speter    type = signed_char_type_node;
358018334Speter  else if (longlong)
358118334Speter    type = long_long_integer_type_node;
358218334Speter  else if (specbits & 1 << (int) RID_LONG)
358318334Speter    type = long_integer_type_node;
358418334Speter  else if (specbits & 1 << (int) RID_SHORT)
358518334Speter    type = short_integer_type_node;
358618334Speter
358718334Speter  if (specbits & 1 << (int) RID_COMPLEX)
358818334Speter    {
358990075Sobrien      if (pedantic && !flag_isoc99)
3590117421Skan	pedwarn ("ISO C90 does not support complex types");
359118334Speter      /* If we just have "complex", it is equivalent to
359218334Speter	 "complex double", but if any modifiers at all are specified it is
359318334Speter	 the complex form of TYPE.  E.g, "complex short" is
359418334Speter	 "complex short int".  */
359518334Speter
359618334Speter      if (defaulted_int && ! longlong
359718334Speter	  && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
359818334Speter			    | (1 << (int) RID_SIGNED)
359918334Speter			    | (1 << (int) RID_UNSIGNED))))
360090075Sobrien	{
360190075Sobrien	  if (pedantic)
360290075Sobrien	    pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
360390075Sobrien	  type = complex_double_type_node;
360490075Sobrien	}
360518334Speter      else if (type == integer_type_node)
360690075Sobrien	{
360790075Sobrien	  if (pedantic)
360890075Sobrien	    pedwarn ("ISO C does not support complex integer types");
360990075Sobrien	  type = complex_integer_type_node;
361090075Sobrien	}
361118334Speter      else if (type == float_type_node)
361218334Speter	type = complex_float_type_node;
361318334Speter      else if (type == double_type_node)
361418334Speter	type = complex_double_type_node;
361518334Speter      else if (type == long_double_type_node)
361618334Speter	type = complex_long_double_type_node;
361718334Speter      else
361890075Sobrien	{
361990075Sobrien	  if (pedantic)
362090075Sobrien	    pedwarn ("ISO C does not support complex integer types");
362190075Sobrien	  type = build_complex_type (type);
362290075Sobrien	}
362318334Speter    }
362418334Speter
3625132730Skan  /* Check the type and width of a bit-field.  */
3626132730Skan  if (bitfield)
3627132730Skan    check_bitfield_type_and_width (&type, width, orig_name);
3628132730Skan
362952284Sobrien  /* Figure out the type qualifiers for the declaration.  There are
363052284Sobrien     two ways a declaration can become qualified.  One is something
363152284Sobrien     like `const int i' where the `const' is explicit.  Another is
363252284Sobrien     something like `typedef const int CI; CI i' where the type of the
3633132730Skan     declaration contains the `const'.  A third possibility is that
3634132730Skan     there is a type qualifier on the element type of a typedefed
3635132730Skan     array type, in which case we should extract that qualifier so
3636132730Skan     that c_apply_type_quals_to_decls receives the full list of
3637132730Skan     qualifiers to work with (C90 is not entirely clear about whether
3638132730Skan     duplicate qualifiers should be diagnosed in this case, but it
3639132730Skan     seems most appropriate to do so).  */
3640132730Skan  element_type = strip_array_types (type);
3641132730Skan  constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3642132730Skan  restrictp
3643132730Skan    = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3644132730Skan  volatilep
3645132730Skan    = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
364618334Speter  inlinep = !! (specbits & (1 << (int) RID_INLINE));
3647132730Skan  if (pedantic && !flag_isoc99)
3648132730Skan    {
3649132730Skan      if (constp > 1)
3650132730Skan	pedwarn ("duplicate `const'");
3651132730Skan      if (restrictp > 1)
3652132730Skan	pedwarn ("duplicate `restrict'");
3653132730Skan      if (volatilep > 1)
3654132730Skan	pedwarn ("duplicate `volatile'");
3655132730Skan    }
365652284Sobrien  if (! flag_gen_aux_info && (TYPE_QUALS (type)))
365718334Speter    type = TYPE_MAIN_VARIANT (type);
365852284Sobrien  type_quals = ((constp ? TYPE_QUAL_CONST : 0)
365952284Sobrien		| (restrictp ? TYPE_QUAL_RESTRICT : 0)
366052284Sobrien		| (volatilep ? TYPE_QUAL_VOLATILE : 0));
366118334Speter
366218334Speter  /* Warn if two storage classes are given. Default to `auto'.  */
366318334Speter
366418334Speter  {
366518334Speter    int nclasses = 0;
366618334Speter
366718334Speter    if (specbits & 1 << (int) RID_AUTO) nclasses++;
366818334Speter    if (specbits & 1 << (int) RID_STATIC) nclasses++;
366918334Speter    if (specbits & 1 << (int) RID_EXTERN) nclasses++;
367018334Speter    if (specbits & 1 << (int) RID_REGISTER) nclasses++;
367118334Speter    if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
367218334Speter
3673117421Skan    /* "static __thread" and "extern __thread" are allowed.  */
3674117421Skan    if ((specbits & (1 << (int) RID_THREAD
3675117421Skan		     | 1 << (int) RID_STATIC
3676117421Skan		     | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3677117421Skan      nclasses++;
3678117421Skan
367918334Speter    /* Warn about storage classes that are invalid for certain
368018334Speter       kinds of declarations (parameters, typenames, etc.).  */
368118334Speter
368218334Speter    if (nclasses > 1)
368318334Speter      error ("multiple storage classes in declaration of `%s'", name);
368418334Speter    else if (funcdef_flag
368518334Speter	     && (specbits
368618334Speter		 & ((1 << (int) RID_REGISTER)
368718334Speter		    | (1 << (int) RID_AUTO)
3688117421Skan		    | (1 << (int) RID_TYPEDEF)
3689117421Skan		    | (1 << (int) RID_THREAD))))
369018334Speter      {
369118334Speter	if (specbits & 1 << (int) RID_AUTO
3692132730Skan	    && (pedantic || current_scope == global_scope))
369318334Speter	  pedwarn ("function definition declared `auto'");
369418334Speter	if (specbits & 1 << (int) RID_REGISTER)
369518334Speter	  error ("function definition declared `register'");
369618334Speter	if (specbits & 1 << (int) RID_TYPEDEF)
369718334Speter	  error ("function definition declared `typedef'");
3698117421Skan	if (specbits & 1 << (int) RID_THREAD)
3699117421Skan	  error ("function definition declared `__thread'");
370090075Sobrien	specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3701117421Skan		      | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
370218334Speter      }
370318334Speter    else if (decl_context != NORMAL && nclasses > 0)
370418334Speter      {
370518334Speter	if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
370618334Speter	  ;
370718334Speter	else
370818334Speter	  {
370990075Sobrien	    switch (decl_context)
371090075Sobrien	      {
371190075Sobrien	      case FIELD:
371290075Sobrien		error ("storage class specified for structure field `%s'",
371390075Sobrien		       name);
371490075Sobrien		break;
371590075Sobrien	      case PARM:
371690075Sobrien		error ("storage class specified for parameter `%s'", name);
371790075Sobrien		break;
371890075Sobrien	      default:
371990075Sobrien		error ("storage class specified for typename");
372090075Sobrien		break;
372190075Sobrien	      }
372290075Sobrien	    specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
372390075Sobrien			  | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3724117421Skan			  | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
372518334Speter	  }
372618334Speter      }
372718334Speter    else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
372818334Speter      {
3729132730Skan	/* `extern' with initialization is invalid if not at file scope.  */
3730132730Skan	if (current_scope == global_scope)
373118334Speter	  warning ("`%s' initialized and declared `extern'", name);
373218334Speter	else
373318334Speter	  error ("`%s' has both `extern' and initializer", name);
373418334Speter      }
3735132730Skan    else if (current_scope == global_scope)
3736117421Skan      {
3737117421Skan	if (specbits & 1 << (int) RID_AUTO)
3738132730Skan	  error ("file-scope declaration of `%s' specifies `auto'", name);
3739117421Skan      }
3740117421Skan    else
3741117421Skan      {
3742117421Skan	if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3743117421Skan	  error ("nested function `%s' declared `extern'", name);
3744117421Skan	else if ((specbits & (1 << (int) RID_THREAD
3745117421Skan			       | 1 << (int) RID_EXTERN
3746117421Skan			       | 1 << (int) RID_STATIC))
3747117421Skan		 == (1 << (int) RID_THREAD))
3748117421Skan	  {
3749117421Skan	    error ("function-scope `%s' implicitly auto and declared `__thread'",
3750117421Skan		   name);
3751117421Skan	    specbits &= ~(1 << (int) RID_THREAD);
3752117421Skan	  }
3753117421Skan      }
375418334Speter  }
375518334Speter
375618334Speter  /* Now figure out the structure of the declarator proper.
375718334Speter     Descend through it, creating more complex types, until we reach
375818334Speter     the declared identifier (or NULL_TREE, in an absolute declarator).  */
375918334Speter
376018334Speter  while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
376118334Speter    {
376218334Speter      if (type == error_mark_node)
376318334Speter	{
376418334Speter	  declarator = TREE_OPERAND (declarator, 0);
376518334Speter	  continue;
376618334Speter	}
376718334Speter
376818334Speter      /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
376918334Speter	 an INDIRECT_REF (for *...),
377018334Speter	 a CALL_EXPR (for ...(...)),
377190075Sobrien	 a TREE_LIST (for nested attributes),
377218334Speter	 an identifier (for the name being declared)
377318334Speter	 or a null pointer (for the place in an absolute declarator
377418334Speter	 where the name was omitted).
377518334Speter	 For the last two cases, we have just exited the loop.
377618334Speter
377718334Speter	 At this point, TYPE is the type of elements of an array,
377818334Speter	 or for a function to return, or for a pointer to point to.
377918334Speter	 After this sequence of ifs, TYPE is the type of the
378018334Speter	 array or function or pointer, and DECLARATOR has had its
378118334Speter	 outermost layer removed.  */
378218334Speter
378390075Sobrien      if (array_ptr_quals != NULL_TREE || array_parm_static)
378418334Speter	{
378590075Sobrien	  /* Only the innermost declarator (making a parameter be of
378690075Sobrien	     array type which is converted to pointer type)
378790075Sobrien	     may have static or type qualifiers.  */
378890075Sobrien	  error ("static or type qualifiers in non-parameter array declarator");
378990075Sobrien	  array_ptr_quals = NULL_TREE;
379090075Sobrien	  array_parm_static = 0;
379190075Sobrien	}
379290075Sobrien
379390075Sobrien      if (TREE_CODE (declarator) == TREE_LIST)
379490075Sobrien	{
379590075Sobrien	  /* We encode a declarator with embedded attributes using
379690075Sobrien	     a TREE_LIST.  */
379790075Sobrien	  tree attrs = TREE_PURPOSE (declarator);
379890075Sobrien	  tree inner_decl;
379990075Sobrien	  int attr_flags = 0;
380090075Sobrien	  declarator = TREE_VALUE (declarator);
380190075Sobrien	  inner_decl = declarator;
380290075Sobrien	  while (inner_decl != NULL_TREE
380390075Sobrien		 && TREE_CODE (inner_decl) == TREE_LIST)
380490075Sobrien	    inner_decl = TREE_VALUE (inner_decl);
380590075Sobrien	  if (inner_decl == NULL_TREE
380690075Sobrien	      || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
380790075Sobrien	    attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
380896263Sobrien	  else if (TREE_CODE (inner_decl) == CALL_EXPR)
380990075Sobrien	    attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
381096263Sobrien	  else if (TREE_CODE (inner_decl) == ARRAY_REF)
381190075Sobrien	    attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
381290075Sobrien	  returned_attrs = decl_attributes (&type,
381390075Sobrien					    chainon (returned_attrs, attrs),
381490075Sobrien					    attr_flags);
381590075Sobrien	}
381690075Sobrien      else if (TREE_CODE (declarator) == ARRAY_REF)
381790075Sobrien	{
381890075Sobrien	  tree itype = NULL_TREE;
381990075Sobrien	  tree size = TREE_OPERAND (declarator, 1);
382018334Speter	  /* The index is a signed object `sizetype' bits wide.  */
3821117421Skan	  tree index_type = c_common_signed_type (sizetype);
382218334Speter
382390075Sobrien	  array_ptr_quals = TREE_TYPE (declarator);
382490075Sobrien	  array_parm_static = TREE_STATIC (declarator);
382590075Sobrien
382618334Speter	  declarator = TREE_OPERAND (declarator, 0);
382718334Speter
382818334Speter	  /* Check for some types that there cannot be arrays of.  */
382918334Speter
383090075Sobrien	  if (VOID_TYPE_P (type))
383118334Speter	    {
383218334Speter	      error ("declaration of `%s' as array of voids", name);
383318334Speter	      type = error_mark_node;
383418334Speter	    }
383518334Speter
383618334Speter	  if (TREE_CODE (type) == FUNCTION_TYPE)
383718334Speter	    {
383818334Speter	      error ("declaration of `%s' as array of functions", name);
383918334Speter	      type = error_mark_node;
384018334Speter	    }
384118334Speter
3842132730Skan	  if (pedantic && !in_system_header && flexible_array_type_p (type))
3843117421Skan	    pedwarn ("invalid use of structure with flexible array member");
3844117421Skan
384518334Speter	  if (size == error_mark_node)
384618334Speter	    type = error_mark_node;
384718334Speter
384818334Speter	  if (type == error_mark_node)
384918334Speter	    continue;
385018334Speter
385118334Speter	  /* If size was specified, set ITYPE to a range-type for that size.
385218334Speter	     Otherwise, ITYPE remains null.  finish_decl may figure it out
385318334Speter	     from an initial value.  */
385418334Speter
385518334Speter	  if (size)
385618334Speter	    {
385718334Speter	      /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
385818334Speter	      STRIP_TYPE_NOPS (size);
385918334Speter
386090075Sobrien	      if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
386118334Speter		{
386218334Speter		  error ("size of array `%s' has non-integer type", name);
386318334Speter		  size = integer_one_node;
386418334Speter		}
386518334Speter
386618334Speter	      if (pedantic && integer_zerop (size))
386790075Sobrien		pedwarn ("ISO C forbids zero-size array `%s'", name);
386818334Speter
386918334Speter	      if (TREE_CODE (size) == INTEGER_CST)
387018334Speter		{
387118334Speter		  constant_expression_warning (size);
387218334Speter		  if (tree_int_cst_sgn (size) < 0)
387318334Speter		    {
387418334Speter		      error ("size of array `%s' is negative", name);
387518334Speter		      size = integer_one_node;
387618334Speter		    }
387718334Speter		}
387818334Speter	      else
387918334Speter		{
388018334Speter		  /* Make sure the array size remains visibly nonconstant
388118334Speter		     even if it is (eg) a const variable with known value.  */
388218334Speter		  size_varies = 1;
388318334Speter
3884102790Skan		  if (!flag_isoc99 && pedantic)
388518334Speter		    {
388618334Speter		      if (TREE_CONSTANT (size))
3887117421Skan			pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
388890075Sobrien				 name);
388918334Speter		      else
3890117421Skan			pedwarn ("ISO C90 forbids variable-size array `%s'",
389190075Sobrien				 name);
389218334Speter		    }
389318334Speter		}
389418334Speter
389590075Sobrien	      if (integer_zerop (size))
389650397Sobrien		{
389790075Sobrien		  /* A zero-length array cannot be represented with an
389890075Sobrien		     unsigned index type, which is what we'll get with
389990075Sobrien		     build_index_type.  Create an open-ended range instead.  */
390090075Sobrien		  itype = build_range_type (sizetype, size, NULL_TREE);
390150397Sobrien		}
390290075Sobrien	      else
390390075Sobrien		{
390490075Sobrien		  /* Compute the maximum valid index, that is, size - 1.
390590075Sobrien		     Do the calculation in index_type, so that if it is
390690075Sobrien		     a variable the computations will be done in the
390790075Sobrien		     proper mode.  */
390890075Sobrien	          itype = fold (build (MINUS_EXPR, index_type,
390990075Sobrien				       convert (index_type, size),
391090075Sobrien				       convert (index_type, size_one_node)));
391150397Sobrien
391290075Sobrien	          /* If that overflowed, the array is too big.
391390075Sobrien		     ??? While a size of INT_MAX+1 technically shouldn't
391490075Sobrien		     cause an overflow (because we subtract 1), the overflow
391590075Sobrien		     is recorded during the conversion to index_type, before
391690075Sobrien		     the subtraction.  Handling this case seems like an
391790075Sobrien		     unnecessary complication.  */
391890075Sobrien		  if (TREE_OVERFLOW (itype))
391990075Sobrien		    {
392090075Sobrien		      error ("size of array `%s' is too large", name);
392190075Sobrien		      type = error_mark_node;
392290075Sobrien		      continue;
392390075Sobrien		    }
392490075Sobrien
392590075Sobrien		  if (size_varies)
3926117421Skan		    {
3927117421Skan		      /* We must be able to distinguish the
3928117421Skan			 SAVE_EXPR_CONTEXT for the variably-sized type
3929117421Skan			 so that we can set it correctly in
3930117421Skan			 set_save_expr_context.  The convention is
3931117421Skan			 that all SAVE_EXPRs that need to be reset
3932117421Skan			 have NULL_TREE for their SAVE_EXPR_CONTEXT.  */
3933117421Skan		      tree cfd = current_function_decl;
3934117421Skan		      if (decl_context == PARM)
3935117421Skan			current_function_decl = NULL_TREE;
3936117421Skan		      itype = variable_size (itype);
3937117421Skan		      if (decl_context == PARM)
3938117421Skan			current_function_decl = cfd;
3939117421Skan		    }
394090075Sobrien		  itype = build_index_type (itype);
394190075Sobrien		}
394218334Speter	    }
394390075Sobrien	  else if (decl_context == FIELD)
394490075Sobrien	    {
394590075Sobrien	      if (pedantic && !flag_isoc99 && !in_system_header)
3946117421Skan		pedwarn ("ISO C90 does not support flexible array members");
394718334Speter
394890075Sobrien	      /* ISO C99 Flexible array members are effectively identical
394990075Sobrien		 to GCC's zero-length array extension.  */
395090075Sobrien	      itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
395190075Sobrien	    }
395218334Speter
395390075Sobrien	  /* If pedantic, complain about arrays of incomplete types.  */
395490075Sobrien
395590075Sobrien	  if (pedantic && !COMPLETE_TYPE_P (type))
395690075Sobrien	    pedwarn ("array type has incomplete element type");
395790075Sobrien
395818334Speter	  /* Build the array type itself, then merge any constancy or
395918334Speter	     volatility into the target type.  We must do it in this order
396018334Speter	     to ensure that the TYPE_MAIN_VARIANT field of the array type
396118334Speter	     is set correctly.  */
396218334Speter
396318334Speter	  type = build_array_type (type, itype);
396452284Sobrien	  if (type_quals)
396552284Sobrien	    type = c_build_qualified_type (type, type_quals);
396618334Speter
396718334Speter	  if (size_varies)
396818334Speter	    C_TYPE_VARIABLE_SIZE (type) = 1;
396990075Sobrien
397090075Sobrien	  /* The GCC extension for zero-length arrays differs from
397190075Sobrien	     ISO flexible array members in that sizeof yields zero.  */
397290075Sobrien	  if (size && integer_zerop (size))
397390075Sobrien	    {
397490075Sobrien	      layout_type (type);
397590075Sobrien	      TYPE_SIZE (type) = bitsize_zero_node;
397690075Sobrien	      TYPE_SIZE_UNIT (type) = size_zero_node;
397790075Sobrien	    }
397890075Sobrien	  if (decl_context != PARM
397990075Sobrien	      && (array_ptr_quals != NULL_TREE || array_parm_static))
398090075Sobrien	    {
398190075Sobrien	      error ("static or type qualifiers in non-parameter array declarator");
398290075Sobrien	      array_ptr_quals = NULL_TREE;
398390075Sobrien	      array_parm_static = 0;
398490075Sobrien	    }
398518334Speter	}
398618334Speter      else if (TREE_CODE (declarator) == CALL_EXPR)
398718334Speter	{
398818334Speter	  tree arg_types;
398918334Speter
399018334Speter	  /* Declaring a function type.
399118334Speter	     Make sure we have a valid type for the function to return.  */
399218334Speter	  if (type == error_mark_node)
399318334Speter	    continue;
399418334Speter
399518334Speter	  size_varies = 0;
399618334Speter
399718334Speter	  /* Warn about some types functions can't return.  */
399818334Speter
399918334Speter	  if (TREE_CODE (type) == FUNCTION_TYPE)
400018334Speter	    {
400118334Speter	      error ("`%s' declared as function returning a function", name);
400218334Speter	      type = integer_type_node;
400318334Speter	    }
400418334Speter	  if (TREE_CODE (type) == ARRAY_TYPE)
400518334Speter	    {
400618334Speter	      error ("`%s' declared as function returning an array", name);
400718334Speter	      type = integer_type_node;
400818334Speter	    }
400918334Speter
401018334Speter	  /* Construct the function type and go to the next
401118334Speter	     inner layer of declarator.  */
401218334Speter
401318334Speter	  arg_types = grokparms (TREE_OPERAND (declarator, 1),
401418334Speter				 funcdef_flag
401518334Speter				 /* Say it's a definition
401618334Speter				    only for the CALL_EXPR
401718334Speter				    closest to the identifier.  */
401818334Speter				 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
401952284Sobrien	  /* Type qualifiers before the return type of the function
402052284Sobrien	     qualify the return type, not the function type.  */
402152284Sobrien	  if (type_quals)
402290075Sobrien	    {
402390075Sobrien	      /* Type qualifiers on a function return type are normally
402490075Sobrien		 permitted by the standard but have no effect, so give a
4025132730Skan		 warning at -Wextra.  Qualifiers on a void return type have
402690075Sobrien		 meaning as a GNU extension, and are banned on function
402790075Sobrien		 definitions in ISO C.  FIXME: strictly we shouldn't
402890075Sobrien		 pedwarn for qualified void return types except on function
402990075Sobrien		 definitions, but not doing so could lead to the undesirable
403090075Sobrien		 state of a "volatile void" function return type not being
403190075Sobrien		 warned about, and a use of the function being compiled
403290075Sobrien		 with GNU semantics, with no diagnostics under -pedantic.  */
403390075Sobrien	      if (VOID_TYPE_P (type) && pedantic && !in_system_header)
403490075Sobrien		pedwarn ("ISO C forbids qualified void function return type");
403590075Sobrien	      else if (extra_warnings
403690075Sobrien		       && !(VOID_TYPE_P (type)
403790075Sobrien			    && type_quals == TYPE_QUAL_VOLATILE))
403890075Sobrien		warning ("type qualifiers ignored on function return type");
403990075Sobrien
404090075Sobrien	      type = c_build_qualified_type (type, type_quals);
404190075Sobrien	    }
404252284Sobrien	  type_quals = TYPE_UNQUALIFIED;
404318334Speter
404418334Speter	  type = build_function_type (type, arg_types);
404518334Speter	  declarator = TREE_OPERAND (declarator, 0);
404618334Speter
404718334Speter	  /* Set the TYPE_CONTEXTs for each tagged type which is local to
404818334Speter	     the formal parameter list of this FUNCTION_TYPE to point to
404918334Speter	     the FUNCTION_TYPE node itself.  */
405018334Speter
405118334Speter	  {
405290075Sobrien	    tree link;
405318334Speter
405450397Sobrien	    for (link = last_function_parm_tags;
405518334Speter		 link;
405618334Speter		 link = TREE_CHAIN (link))
405718334Speter	      TYPE_CONTEXT (TREE_VALUE (link)) = type;
405818334Speter	  }
405918334Speter	}
406018334Speter      else if (TREE_CODE (declarator) == INDIRECT_REF)
406118334Speter	{
406218334Speter	  /* Merge any constancy or volatility into the target type
406318334Speter	     for the pointer.  */
406418334Speter
406518334Speter	  if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
406652284Sobrien	      && type_quals)
406790075Sobrien	    pedwarn ("ISO C forbids qualified function types");
406852284Sobrien	  if (type_quals)
406952284Sobrien	    type = c_build_qualified_type (type, type_quals);
407052284Sobrien	  type_quals = TYPE_UNQUALIFIED;
407118334Speter	  size_varies = 0;
407218334Speter
407318334Speter	  type = build_pointer_type (type);
407418334Speter
407518334Speter	  /* Process a list of type modifier keywords
407618334Speter	     (such as const or volatile) that were given inside the `*'.  */
407718334Speter
407818334Speter	  if (TREE_TYPE (declarator))
407918334Speter	    {
408090075Sobrien	      tree typemodlist;
408118334Speter	      int erred = 0;
408252284Sobrien
408352284Sobrien	      constp = 0;
408452284Sobrien	      volatilep = 0;
408552284Sobrien	      restrictp = 0;
408618334Speter	      for (typemodlist = TREE_TYPE (declarator); typemodlist;
408718334Speter		   typemodlist = TREE_CHAIN (typemodlist))
408818334Speter		{
408952284Sobrien		  tree qualifier = TREE_VALUE (typemodlist);
409052284Sobrien
409190075Sobrien		  if (C_IS_RESERVED_WORD (qualifier))
409218334Speter		    {
409390075Sobrien		      if (C_RID_CODE (qualifier) == RID_CONST)
409490075Sobrien			constp++;
409590075Sobrien		      else if (C_RID_CODE (qualifier) == RID_VOLATILE)
409690075Sobrien			volatilep++;
409790075Sobrien		      else if (C_RID_CODE (qualifier) == RID_RESTRICT)
409890075Sobrien			restrictp++;
409990075Sobrien		      else
410090075Sobrien			erred++;
410118334Speter		    }
410290075Sobrien		  else
410390075Sobrien		    erred++;
410418334Speter		}
410590075Sobrien
410690075Sobrien	      if (erred)
410790075Sobrien		error ("invalid type modifier within pointer declarator");
4108132730Skan	      if (pedantic && !flag_isoc99)
4109132730Skan		{
4110132730Skan		  if (constp > 1)
4111132730Skan		    pedwarn ("duplicate `const'");
4112132730Skan		  if (volatilep > 1)
4113132730Skan		    pedwarn ("duplicate `volatile'");
4114132730Skan		  if (restrictp > 1)
4115132730Skan		    pedwarn ("duplicate `restrict'");
4116132730Skan		}
411752284Sobrien
411852284Sobrien	      type_quals = ((constp ? TYPE_QUAL_CONST : 0)
411952284Sobrien			    | (restrictp ? TYPE_QUAL_RESTRICT : 0)
412052284Sobrien			    | (volatilep ? TYPE_QUAL_VOLATILE : 0));
412118334Speter	    }
412218334Speter
412318334Speter	  declarator = TREE_OPERAND (declarator, 0);
412418334Speter	}
412518334Speter      else
412618334Speter	abort ();
412718334Speter
412818334Speter    }
412918334Speter
413018334Speter  /* Now TYPE has the actual type.  */
413118334Speter
413250397Sobrien  /* Did array size calculations overflow?  */
413350397Sobrien
413450397Sobrien  if (TREE_CODE (type) == ARRAY_TYPE
413590075Sobrien      && COMPLETE_TYPE_P (type)
413650397Sobrien      && TREE_OVERFLOW (TYPE_SIZE (type)))
413790075Sobrien    {
413890075Sobrien      error ("size of array `%s' is too large", name);
413990075Sobrien      /* If we proceed with the array type as it is, we'll eventually
414090075Sobrien	 crash in tree_low_cst().  */
414190075Sobrien      type = error_mark_node;
414290075Sobrien    }
414350397Sobrien
414418334Speter  /* If this is declaring a typedef name, return a TYPE_DECL.  */
414518334Speter
414618334Speter  if (specbits & (1 << (int) RID_TYPEDEF))
414718334Speter    {
414818334Speter      tree decl;
414918334Speter      /* Note that the grammar rejects storage classes
415018334Speter	 in typenames, fields or parameters */
415118334Speter      if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
415252284Sobrien	  && type_quals)
415390075Sobrien	pedwarn ("ISO C forbids qualified function types");
415452284Sobrien      if (type_quals)
415552284Sobrien	type = c_build_qualified_type (type, type_quals);
415618334Speter      decl = build_decl (TYPE_DECL, declarator, type);
415718334Speter      if ((specbits & (1 << (int) RID_SIGNED))
415818334Speter	  || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
415918334Speter	C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
416090075Sobrien      decl_attributes (&decl, returned_attrs, 0);
416118334Speter      return decl;
416218334Speter    }
416318334Speter
416418334Speter  /* Detect the case of an array type of unspecified size
416518334Speter     which came, as such, direct from a typedef name.
416618334Speter     We must copy the type, so that each identifier gets
416718334Speter     a distinct type, so that each identifier's size can be
416818334Speter     controlled separately by its own initializer.  */
416918334Speter
417018334Speter  if (type != 0 && typedef_type != 0
417190075Sobrien      && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
417290075Sobrien      && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
417318334Speter    {
417418334Speter      type = build_array_type (TREE_TYPE (type), 0);
417518334Speter      if (size_varies)
417618334Speter	C_TYPE_VARIABLE_SIZE (type) = 1;
417718334Speter    }
417818334Speter
417918334Speter  /* If this is a type name (such as, in a cast or sizeof),
418018334Speter     compute the type and return it now.  */
418118334Speter
418218334Speter  if (decl_context == TYPENAME)
418318334Speter    {
418418334Speter      /* Note that the grammar rejects storage classes
418518334Speter	 in typenames, fields or parameters */
418618334Speter      if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
418752284Sobrien	  && type_quals)
418890075Sobrien	pedwarn ("ISO C forbids const or volatile function types");
418952284Sobrien      if (type_quals)
419052284Sobrien	type = c_build_qualified_type (type, type_quals);
419190075Sobrien      decl_attributes (&type, returned_attrs, 0);
419218334Speter      return type;
419318334Speter    }
419418334Speter
419518334Speter  /* Aside from typedefs and type names (handle above),
419618334Speter     `void' at top level (not within pointer)
419718334Speter     is allowed only in public variables.
419818334Speter     We don't complain about parms either, but that is because
419918334Speter     a better error message can be made later.  */
420018334Speter
420190075Sobrien  if (VOID_TYPE_P (type) && decl_context != PARM
420218334Speter      && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
420318334Speter	    && ((specbits & (1 << (int) RID_EXTERN))
4204132730Skan		|| (current_scope == global_scope
420518334Speter		    && !(specbits
420618334Speter			 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
420718334Speter    {
420818334Speter      error ("variable or field `%s' declared void", name);
420918334Speter      type = integer_type_node;
421018334Speter    }
421118334Speter
421218334Speter  /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
421318334Speter     or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
421418334Speter
421518334Speter  {
421690075Sobrien    tree decl;
421718334Speter
421818334Speter    if (decl_context == PARM)
421918334Speter      {
422090075Sobrien	tree type_as_written;
422190075Sobrien	tree promoted_type;
422218334Speter
422318334Speter	/* A parameter declared as an array of T is really a pointer to T.
422418334Speter	   One declared as a function is really a pointer to a function.  */
422518334Speter
422618334Speter	if (TREE_CODE (type) == ARRAY_TYPE)
422718334Speter	  {
422818334Speter	    /* Transfer const-ness of array into that of type pointed to.  */
422918334Speter	    type = TREE_TYPE (type);
423052284Sobrien	    if (type_quals)
423152284Sobrien	      type = c_build_qualified_type (type, type_quals);
423218334Speter	    type = build_pointer_type (type);
423352284Sobrien	    type_quals = TYPE_UNQUALIFIED;
423490075Sobrien	    if (array_ptr_quals)
423590075Sobrien	      {
423690075Sobrien		tree new_ptr_quals, new_ptr_attrs;
423790075Sobrien		int erred = 0;
423890075Sobrien		split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
423990075Sobrien		/* We don't yet implement attributes in this context.  */
424090075Sobrien		if (new_ptr_attrs != NULL_TREE)
424190075Sobrien		  warning ("attributes in parameter array declarator ignored");
424290075Sobrien
424390075Sobrien		constp = 0;
424490075Sobrien		volatilep = 0;
424590075Sobrien		restrictp = 0;
424690075Sobrien		for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
424790075Sobrien		  {
424890075Sobrien		    tree qualifier = TREE_VALUE (new_ptr_quals);
424990075Sobrien
425090075Sobrien		    if (C_IS_RESERVED_WORD (qualifier))
425190075Sobrien		      {
425290075Sobrien			if (C_RID_CODE (qualifier) == RID_CONST)
425390075Sobrien			  constp++;
425490075Sobrien			else if (C_RID_CODE (qualifier) == RID_VOLATILE)
425590075Sobrien			  volatilep++;
425690075Sobrien			else if (C_RID_CODE (qualifier) == RID_RESTRICT)
425790075Sobrien			  restrictp++;
425890075Sobrien			else
425990075Sobrien			  erred++;
426090075Sobrien		      }
426190075Sobrien		    else
426290075Sobrien		      erred++;
426390075Sobrien		  }
426490075Sobrien
426590075Sobrien		if (erred)
426690075Sobrien		  error ("invalid type modifier within array declarator");
426790075Sobrien
426890075Sobrien		type_quals = ((constp ? TYPE_QUAL_CONST : 0)
426990075Sobrien			      | (restrictp ? TYPE_QUAL_RESTRICT : 0)
427090075Sobrien			      | (volatilep ? TYPE_QUAL_VOLATILE : 0));
427190075Sobrien	      }
427218334Speter	    size_varies = 0;
427318334Speter	  }
427418334Speter	else if (TREE_CODE (type) == FUNCTION_TYPE)
427518334Speter	  {
427652284Sobrien	    if (pedantic && type_quals)
427790075Sobrien	      pedwarn ("ISO C forbids qualified function types");
427852284Sobrien	    if (type_quals)
427952284Sobrien	      type = c_build_qualified_type (type, type_quals);
428018334Speter	    type = build_pointer_type (type);
428152284Sobrien	    type_quals = TYPE_UNQUALIFIED;
428218334Speter	  }
428390075Sobrien	else if (type_quals)
428490075Sobrien	  type = c_build_qualified_type (type, type_quals);
4285132730Skan
428690075Sobrien	type_as_written = type;
428718334Speter
428818334Speter	decl = build_decl (PARM_DECL, declarator, type);
428918334Speter	if (size_varies)
429018334Speter	  C_DECL_VARIABLE_SIZE (decl) = 1;
429118334Speter
429218334Speter	/* Compute the type actually passed in the parmlist,
429318334Speter	   for the case where there is no prototype.
429418334Speter	   (For example, shorts and chars are passed as ints.)
429518334Speter	   When there is a prototype, this is overridden later.  */
429618334Speter
429790075Sobrien	if (type == error_mark_node)
429890075Sobrien	  promoted_type = type;
429990075Sobrien	else
4300117421Skan	  promoted_type = c_type_promotes_to (type);
430118334Speter
430290075Sobrien	DECL_ARG_TYPE (decl) = promoted_type;
430318334Speter	DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
430418334Speter      }
430518334Speter    else if (decl_context == FIELD)
430618334Speter      {
430718334Speter	/* Structure field.  It may not be a function.  */
430896263Sobrien
430918334Speter	if (TREE_CODE (type) == FUNCTION_TYPE)
431018334Speter	  {
431118334Speter	    error ("field `%s' declared as a function", name);
431218334Speter	    type = build_pointer_type (type);
431318334Speter	  }
431490075Sobrien	else if (TREE_CODE (type) != ERROR_MARK
431590075Sobrien	         && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
431618334Speter	  {
431718334Speter	    error ("field `%s' has incomplete type", name);
431818334Speter	    type = error_mark_node;
431918334Speter	  }
432018334Speter	/* Move type qualifiers down to element of an array.  */
432152284Sobrien	if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4322132730Skan	  type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4323132730Skan							   type_quals),
4324132730Skan				   TYPE_DOMAIN (type));
432518334Speter	decl = build_decl (FIELD_DECL, declarator, type);
432690075Sobrien	DECL_NONADDRESSABLE_P (decl) = bitfield;
432790075Sobrien
432818334Speter	if (size_varies)
432918334Speter	  C_DECL_VARIABLE_SIZE (decl) = 1;
433018334Speter      }
433118334Speter    else if (TREE_CODE (type) == FUNCTION_TYPE)
433218334Speter      {
433318334Speter	/* Every function declaration is "external"
433418334Speter	   except for those which are inside a function body
433518334Speter	   in which `auto' is used.
433618334Speter	   That is a case not specified by ANSI C,
433718334Speter	   and we use it for forward declarations for nested functions.  */
433818334Speter	int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4339132730Skan			  || current_scope == global_scope);
434018334Speter
434118334Speter	if (specbits & (1 << (int) RID_AUTO)
4342132730Skan	    && (pedantic || current_scope == global_scope))
434318334Speter	  pedwarn ("invalid storage class for function `%s'", name);
434418334Speter	if (specbits & (1 << (int) RID_REGISTER))
434518334Speter	  error ("invalid storage class for function `%s'", name);
4346117421Skan	if (specbits & (1 << (int) RID_THREAD))
4347117421Skan	  error ("invalid storage class for function `%s'", name);
4348132730Skan	/* Function declaration not at file scope.
434918334Speter	   Storage classes other than `extern' are not allowed
435018334Speter	   and `extern' makes no difference.  */
4351132730Skan	if (current_scope != global_scope
435218334Speter	    && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
435318334Speter	    && pedantic)
435418334Speter	  pedwarn ("invalid storage class for function `%s'", name);
435518334Speter
435618334Speter	decl = build_decl (FUNCTION_DECL, declarator, type);
435790075Sobrien	decl = build_decl_attribute_variant (decl, decl_attr);
435818334Speter
4359132730Skan	DECL_LANG_SPECIFIC (decl)
4360132730Skan	  = ggc_alloc_cleared (sizeof (struct lang_decl));
436190075Sobrien
436252284Sobrien	if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
436390075Sobrien	  pedwarn ("ISO C forbids qualified function types");
436418334Speter
436552284Sobrien	/* GNU C interprets a `volatile void' return type to indicate
436652284Sobrien	   that the function does not return.  */
436752284Sobrien	if ((type_quals & TYPE_QUAL_VOLATILE)
436890075Sobrien	    && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
436918334Speter	  warning ("`noreturn' function returns non-void value");
437018334Speter
437118334Speter	if (extern_ref)
437218334Speter	  DECL_EXTERNAL (decl) = 1;
437318334Speter	/* Record absence of global scope for `static' or `auto'.  */
437418334Speter	TREE_PUBLIC (decl)
437518334Speter	  = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
437618334Speter
437796263Sobrien	if (defaulted_int)
437896263Sobrien	  C_FUNCTION_IMPLICIT_INT (decl) = 1;
437996263Sobrien
438018334Speter	/* Record presence of `inline', if it is reasonable.  */
438190075Sobrien	if (MAIN_NAME_P (declarator))
438218334Speter	  {
438390075Sobrien	    if (inlinep)
438418334Speter	      warning ("cannot inline function `main'");
438590075Sobrien	  }
438690075Sobrien	else if (inlinep)
438790075Sobrien	  {
4388132730Skan	    /* Record that the function is declared `inline'.  */
438990075Sobrien	    DECL_DECLARED_INLINE_P (decl) = 1;
439018334Speter
439190075Sobrien	    /* Do not mark bare declarations as DECL_INLINE.  Doing so
439290075Sobrien	       in the presence of multiple declarations can result in
439390075Sobrien	       the abstract origin pointing between the declarations,
439490075Sobrien	       which will confuse dwarf2out.  */
439590075Sobrien	    if (initialized)
439690075Sobrien	      {
439790075Sobrien		DECL_INLINE (decl) = 1;
439890075Sobrien		if (specbits & (1 << (int) RID_EXTERN))
439990075Sobrien		  current_extern_inline = 1;
440090075Sobrien	      }
440118334Speter	  }
440290075Sobrien	/* If -finline-functions, assume it can be inlined.  This does
440390075Sobrien	   two things: let the function be deferred until it is actually
440490075Sobrien	   needed, and let dwarf2 know that the function is inlinable.  */
440590075Sobrien	else if (flag_inline_trees == 2 && initialized)
4406132730Skan	  DECL_INLINE (decl) = 1;
440718334Speter      }
440818334Speter    else
440918334Speter      {
441018334Speter	/* It's a variable.  */
441118334Speter	/* An uninitialized decl with `extern' is a reference.  */
441218334Speter	int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
441318334Speter
441418334Speter	/* Move type qualifiers down to element of an array.  */
441552284Sobrien	if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
441618334Speter	  {
441790075Sobrien	    int saved_align = TYPE_ALIGN(type);
441852284Sobrien	    type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
441952284Sobrien							     type_quals),
442018334Speter				     TYPE_DOMAIN (type));
442190075Sobrien	    TYPE_ALIGN (type) = saved_align;
442218334Speter	  }
442390075Sobrien	else if (type_quals)
442490075Sobrien	  type = c_build_qualified_type (type, type_quals);
4425132730Skan
4426132730Skan	/* It is invalid to create an `extern' declaration for a
4427132730Skan	   variable if there is a global declaration that is
4428132730Skan	   `static' and the global declaration is not visible.  */
4429132730Skan	if (extern_ref && current_scope != global_scope)
4430132730Skan	  {
4431132730Skan	    tree global_decl;
4432132730Skan
4433132730Skan	    global_decl = identifier_global_value (declarator);
4434132730Skan	    if (global_decl
4435132730Skan		&& TREE_CODE (global_decl) == VAR_DECL
4436132730Skan		&& lookup_name (declarator) != global_decl
4437132730Skan		&& !TREE_PUBLIC (global_decl))
4438132730Skan	      error ("variable previously declared `static' redeclared "
4439132730Skan		     "`extern'");
4440132730Skan	  }
4441132730Skan
444218334Speter	decl = build_decl (VAR_DECL, declarator, type);
444318334Speter	if (size_varies)
444418334Speter	  C_DECL_VARIABLE_SIZE (decl) = 1;
444518334Speter
444618334Speter	if (inlinep)
4447132730Skan	  pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
444818334Speter
444918334Speter	DECL_EXTERNAL (decl) = extern_ref;
4450117421Skan
4451132730Skan	/* At file scope, the presence of a `static' or `register' storage
445218334Speter	   class specifier, or the absence of all storage class specifiers
445318334Speter	   makes this declaration a definition (perhaps tentative).  Also,
445418334Speter	   the absence of both `static' and `register' makes it public.  */
4455132730Skan	if (current_scope == global_scope)
445618334Speter	  {
4457117421Skan	    TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4458117421Skan					       | (1 << (int) RID_REGISTER)));
4459117421Skan	    TREE_STATIC (decl) = !extern_ref;
446018334Speter	  }
4461132730Skan	/* Not at file scope, only `static' makes a static definition.  */
446218334Speter	else
446318334Speter	  {
446418334Speter	    TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4465117421Skan	    TREE_PUBLIC (decl) = extern_ref;
446618334Speter	  }
4467117421Skan
4468117421Skan	if (specbits & 1 << (int) RID_THREAD)
4469117421Skan	  {
4470117421Skan	    if (targetm.have_tls)
4471117421Skan	      DECL_THREAD_LOCAL (decl) = 1;
4472117421Skan	    else
4473117421Skan	      /* A mere warning is sure to result in improper semantics
4474117421Skan		 at runtime.  Don't bother to allow this to compile.  */
4475117421Skan	      error ("thread-local storage not supported for this target");
4476117421Skan	  }
447718334Speter      }
447818334Speter
447918334Speter    /* Record `register' declaration for warnings on &
448018334Speter       and in case doing stupid register allocation.  */
448118334Speter
448218334Speter    if (specbits & (1 << (int) RID_REGISTER))
448318334Speter      DECL_REGISTER (decl) = 1;
448418334Speter
448518334Speter    /* Record constancy and volatility.  */
448652284Sobrien    c_apply_type_quals_to_decl (type_quals, decl);
448718334Speter
448818334Speter    /* If a type has volatile components, it should be stored in memory.
448918334Speter       Otherwise, the fact that those components are volatile
449018334Speter       will be ignored, and would even crash the compiler.  */
449118334Speter    if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4492117421Skan      c_mark_addressable (decl);
449318334Speter
4494132730Skan#ifdef ENABLE_CHECKING
4495132730Skan  /* This is the earliest point at which we might know the assembler
4496132730Skan     name of a variable.  Thus, if it's known before this, die horribly.  */
4497132730Skan  if (DECL_ASSEMBLER_NAME_SET_P (decl))
4498132730Skan    abort ();
4499132730Skan#endif
4500132730Skan
450190075Sobrien    decl_attributes (&decl, returned_attrs, 0);
450218334Speter
450318334Speter    return decl;
450418334Speter  }
450518334Speter}
450618334Speter
450718334Speter/* Decode the parameter-list info for a function type or function definition.
450818334Speter   The argument is the value returned by `get_parm_info' (or made in parse.y
450918334Speter   if there is an identifier list instead of a parameter decl list).
451018334Speter   These two functions are separate because when a function returns
451118334Speter   or receives functions then each is called multiple times but the order
451218334Speter   of calls is different.  The last call to `grokparms' is always the one
451318334Speter   that contains the formal parameter names of a function definition.
451418334Speter
451518334Speter   Store in `last_function_parms' a chain of the decls of parms.
451618334Speter   Also store in `last_function_parm_tags' a chain of the struct, union,
451718334Speter   and enum tags declared among the parms.
451818334Speter
451918334Speter   Return a list of arg types to use in the FUNCTION_TYPE for this function.
452018334Speter
452118334Speter   FUNCDEF_FLAG is nonzero for a function definition, 0 for
452218334Speter   a mere declaration.  A nonempty identifier-list gets an error message
452318334Speter   when FUNCDEF_FLAG is zero.  */
452418334Speter
452518334Speterstatic tree
4526132730Skangrokparms (tree parms_info, int funcdef_flag)
452718334Speter{
452818334Speter  tree first_parm = TREE_CHAIN (parms_info);
452918334Speter
453018334Speter  last_function_parms = TREE_PURPOSE (parms_info);
453118334Speter  last_function_parm_tags = TREE_VALUE (parms_info);
4532132730Skan  last_function_parm_others = TREE_TYPE (parms_info);
453318334Speter
453418334Speter  if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
453518334Speter      && !in_system_header)
453618334Speter    warning ("function declaration isn't a prototype");
453718334Speter
453818334Speter  if (first_parm != 0
453918334Speter      && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
454018334Speter    {
454118334Speter      if (! funcdef_flag)
454218334Speter	pedwarn ("parameter names (without types) in function declaration");
454318334Speter
454418334Speter      last_function_parms = first_parm;
454518334Speter      return 0;
454618334Speter    }
454718334Speter  else
454818334Speter    {
454918334Speter      tree parm;
455018334Speter      tree typelt;
4551132730Skan      /* If the arg types are incomplete in a declaration,
455218334Speter	 they must include undefined tags.
455318334Speter	 These tags can never be defined in the scope of the declaration,
455418334Speter	 so the types can never be completed,
455518334Speter	 and no call can be compiled successfully.  */
455618334Speter
4557132730Skan      for (parm = last_function_parms, typelt = first_parm;
4558132730Skan	   parm;
4559132730Skan	   parm = TREE_CHAIN (parm))
4560132730Skan	/* Skip over any enumeration constants declared here.  */
4561132730Skan	if (TREE_CODE (parm) == PARM_DECL)
4562132730Skan	  {
4563132730Skan	    /* Barf if the parameter itself has an incomplete type.  */
4564132730Skan	    tree type = TREE_VALUE (typelt);
4565132730Skan	    if (type == error_mark_node)
4566132730Skan	      continue;
4567132730Skan	    if (!COMPLETE_TYPE_P (type))
4568132730Skan	      {
4569132730Skan		if (funcdef_flag && DECL_NAME (parm) != 0)
4570132730Skan		  error ("parameter `%s' has incomplete type",
4571132730Skan			 IDENTIFIER_POINTER (DECL_NAME (parm)));
4572132730Skan		else
4573132730Skan		  warning ("parameter has incomplete type");
4574132730Skan		if (funcdef_flag)
4575132730Skan		  {
4576132730Skan		    TREE_VALUE (typelt) = error_mark_node;
4577132730Skan		    TREE_TYPE (parm) = error_mark_node;
4578132730Skan		  }
4579132730Skan	      }
4580132730Skan	    typelt = TREE_CHAIN (typelt);
4581132730Skan	  }
4582132730Skan
458390075Sobrien      return first_parm;
458418334Speter    }
458518334Speter}
458618334Speter
458718334Speter/* Return a tree_list node with info on a parameter list just parsed.
4588132730Skan   The TREE_PURPOSE is a list of decls of those parms.
458918334Speter   The TREE_VALUE is a list of structure, union and enum tags defined.
459018334Speter   The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4591132730Skan   The TREE_TYPE is a list of non-parameter decls which appeared with the
4592132730Skan   parameters.
459318334Speter   This tree_list node is later fed to `grokparms'.
459418334Speter
459518334Speter   VOID_AT_END nonzero means append `void' to the end of the type-list.
459618334Speter   Zero means the parmlist ended with an ellipsis so don't append `void'.  */
459718334Speter
459818334Spetertree
4599132730Skanget_parm_info (int void_at_end)
460018334Speter{
4601132730Skan  tree decl, type, list;
460290075Sobrien  tree types = 0;
4603132730Skan  tree *last_type = &types;
4604132730Skan  tree tags = current_scope->tags;
4605132730Skan  tree parms = current_scope->parms;
4606132730Skan  tree others = current_scope->names;
4607132730Skan  static bool explained_incomplete_types = false;
4608132730Skan  bool gave_void_only_once_err = false;
460918334Speter
4610132730Skan  /* Just "void" (and no ellipsis) is special.  There are really no parms.
4611132730Skan     But if the "void" is qualified (by "const" or "volatile"), or has a
4612132730Skan     storage class specifier ("register"), then the behavior is undefined;
4613132730Skan     issue an error.  Typedefs for "void" are OK (see DR#157).  */
461418334Speter  if (void_at_end && parms != 0
461518334Speter      && TREE_CHAIN (parms) == 0
461690075Sobrien      && VOID_TYPE_P (TREE_TYPE (parms))
4617132730Skan      && !DECL_NAME (parms))
461818334Speter    {
4619132730Skan      if (TREE_THIS_VOLATILE (parms)
4620132730Skan	  || TREE_READONLY (parms)
4621132730Skan	  || DECL_REGISTER (parms))
4622132730Skan	error ("\"void\" as only parameter may not be qualified");
4623132730Skan
4624132730Skan      return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
462518334Speter    }
462618334Speter
4627132730Skan  /* Sanity check all of the parameter declarations.  */
4628132730Skan  for (decl = parms; decl; decl = TREE_CHAIN (decl))
462918334Speter    {
4630132730Skan      if (TREE_CODE (decl) != PARM_DECL)
4631132730Skan	abort ();
4632132730Skan      if (TREE_ASM_WRITTEN (decl))
4633132730Skan	abort ();
463418334Speter
4635132730Skan      /* Since there is a prototype, args are passed in their
4636132730Skan	 declared types.  The back end may override this.  */
4637132730Skan      type = TREE_TYPE (decl);
4638132730Skan      DECL_ARG_TYPE (decl) = type;
4639132730Skan
4640132730Skan      /* Check for (..., void, ...) and issue an error.  */
4641132730Skan      if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
464218334Speter	{
4643132730Skan	  error ("\"void\" must be the only parameter");
4644132730Skan	  gave_void_only_once_err = true;
464518334Speter	}
464618334Speter
4647132730Skan      type = build_tree_list (0, type);
4648132730Skan      *last_type = type;
4649132730Skan      last_type = &TREE_CHAIN (type);
465018334Speter    }
465118334Speter
4652132730Skan  /* Check the list of non-parameter decls for any forward parm decls
4653132730Skan     that never got real decls.  */
4654132730Skan  for (decl = others; decl; decl = TREE_CHAIN (decl))
465518334Speter    if (TREE_CODE (decl) == PARM_DECL)
465618334Speter      {
4657132730Skan	if (!TREE_ASM_WRITTEN (decl))
4658132730Skan	  abort ();
465918334Speter
4660132730Skan	  error ("%Jparameter \"%D\" has just a forward declaration",
4661132730Skan		 decl, decl);
466218334Speter      }
466318334Speter
4664132730Skan  /* Warn about any struct, union or enum tags defined within this
4665132730Skan     list.  The scope of such types is limited to this declaration,
4666132730Skan     which is rarely if ever desirable (it's impossible to call such
4667132730Skan     a function with type-correct arguments).  */
4668132730Skan  for (decl = tags; decl; decl = TREE_CHAIN (decl))
466918334Speter    {
4670132730Skan      enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4671132730Skan      const char *keyword;
467218334Speter      /* An anonymous union parm type is meaningful as a GNU extension.
467318334Speter	 So don't warn for that.  */
4674132730Skan      if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
467518334Speter	continue;
4676132730Skan
4677132730Skan      /* The keyword should not be translated.  */
4678132730Skan      switch (code)
467990075Sobrien	{
4680132730Skan	case RECORD_TYPE:   keyword = "struct"; break;
4681132730Skan	case UNION_TYPE:    keyword = "union";  break;
4682132730Skan	case ENUMERAL_TYPE: keyword = "enum";   break;
4683132730Skan	default: abort ();
468490075Sobrien	}
4685132730Skan
4686132730Skan      if (TREE_PURPOSE (decl))
4687132730Skan	/* The first %s will be one of 'struct', 'union', or 'enum'.  */
4688132730Skan	warning ("\"%s %s\" declared inside parameter list",
4689132730Skan		 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4690132730Skan      else
4691132730Skan	/* The %s will be one of 'struct', 'union', or 'enum'.  */
4692132730Skan	warning ("anonymous %s declared inside parameter list", keyword);
4693132730Skan
4694132730Skan      if (! explained_incomplete_types)
469518334Speter	{
4696132730Skan	  warning ("its scope is only this definition or declaration,"
4697132730Skan		   " which is probably not what you want");
4698132730Skan	  explained_incomplete_types = true;
469918334Speter	}
470018334Speter    }
4701132730Skan
4702132730Skan
4703132730Skan  if (void_at_end)
4704132730Skan    {
4705132730Skan      type = build_tree_list (0, void_type_node);
4706132730Skan      *last_type = type;
4707132730Skan    }
4708132730Skan
4709132730Skan  list = tree_cons (parms, tags, types);
4710132730Skan  TREE_TYPE (list) = others;
4711132730Skan  return list;
471218334Speter}
471318334Speter
471418334Speter/* Get the struct, enum or union (CODE says which) with tag NAME.
471518334Speter   Define the tag as a forward-reference if it is not defined.  */
471618334Speter
471718334Spetertree
4718132730Skanxref_tag (enum tree_code code, tree name)
471918334Speter{
472018334Speter  /* If a cross reference is requested, look up the type
472118334Speter     already defined for this tag and return it.  */
472218334Speter
4723132730Skan  tree ref = lookup_tag (code, name, 0);
472490075Sobrien  /* If this is the right type of tag, return what we found.
472590075Sobrien     (This reference will be shadowed by shadow_tag later if appropriate.)
472690075Sobrien     If this is the wrong type of tag, do not return it.  If it was the
4727132730Skan     wrong type in the same scope, we will have had an error
4728132730Skan     message already; if in a different scope and declaring
472990075Sobrien     a name, pending_xref_error will give an error message; but if in a
4730132730Skan     different scope and not declaring a name, this tag should
473190075Sobrien     shadow the previous declaration of a different type of tag, and
473290075Sobrien     this would not work properly if we return the reference found.
473390075Sobrien     (For example, with "struct foo" in an outer scope, "union foo;"
473490075Sobrien     must shadow that tag with a new one of union type.)  */
473590075Sobrien  if (ref && TREE_CODE (ref) == code)
473618334Speter    return ref;
473718334Speter
473818334Speter  /* If no such tag is yet defined, create a forward-reference node
473918334Speter     and record it as the "definition".
474018334Speter     When a real declaration of this type is found,
474118334Speter     the forward-reference will be altered into a real type.  */
474218334Speter
474318334Speter  ref = make_node (code);
474418334Speter  if (code == ENUMERAL_TYPE)
474518334Speter    {
474618334Speter      /* Give the type a default layout like unsigned int
474718334Speter	 to avoid crashing if it does not get defined.  */
474818334Speter      TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
474918334Speter      TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
475090075Sobrien      TYPE_USER_ALIGN (ref) = 0;
475118334Speter      TREE_UNSIGNED (ref) = 1;
475218334Speter      TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
475318334Speter      TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
475418334Speter      TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
475518334Speter    }
475618334Speter
475718334Speter  pushtag (name, ref);
475818334Speter
475918334Speter  return ref;
476018334Speter}
476118334Speter
4762132730Skan/* Make sure that the tag NAME is defined *in the current scope*
476318334Speter   at least as a forward reference.
476490075Sobrien   CODE says which kind of tag NAME ought to be.  */
476518334Speter
476618334Spetertree
4767132730Skanstart_struct (enum tree_code code, tree name)
476818334Speter{
4769132730Skan  /* If there is already a tag defined at this scope
477018334Speter     (as a forward reference), just return it.  */
477118334Speter
477290075Sobrien  tree ref = 0;
477318334Speter
477418334Speter  if (name != 0)
4775132730Skan    ref = lookup_tag (code, name, 1);
477618334Speter  if (ref && TREE_CODE (ref) == code)
477718334Speter    {
477818334Speter      if (TYPE_FIELDS (ref))
477990075Sobrien        {
478090075Sobrien	  if (code == UNION_TYPE)
4781117421Skan	    error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
478290075Sobrien          else
4783117421Skan	    error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4784132730Skan	}
4785117421Skan    }
4786117421Skan  else
4787117421Skan    {
4788117421Skan      /* Otherwise create a forward-reference just so the tag is in scope.  */
478918334Speter
4790117421Skan      ref = make_node (code);
4791117421Skan      pushtag (name, ref);
479218334Speter    }
4793132730Skan
479418334Speter  C_TYPE_BEING_DEFINED (ref) = 1;
479550397Sobrien  TYPE_PACKED (ref) = flag_pack_struct;
479618334Speter  return ref;
479718334Speter}
479818334Speter
479918334Speter/* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
480018334Speter   of a structure component, returning a FIELD_DECL node.
4801132730Skan   WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
480218334Speter
480318334Speter   This is done during the parsing of the struct declaration.
480418334Speter   The FIELD_DECL nodes are chained together and the lot of them
480518334Speter   are ultimately passed to `build_struct' to make the RECORD_TYPE node.  */
480618334Speter
480718334Spetertree
4808132730Skangrokfield (tree declarator, tree declspecs, tree width)
480918334Speter{
481018334Speter  tree value;
481118334Speter
481290075Sobrien  if (declarator == NULL_TREE && width == NULL_TREE)
481390075Sobrien    {
4814117421Skan      /* This is an unnamed decl.
4815117421Skan
4816117421Skan	 If we have something of the form "union { list } ;" then this
4817117421Skan	 is the anonymous union extension.  Similarly for struct.
4818117421Skan
4819117421Skan	 If this is something of the form "struct foo;", then
4820117421Skan	   If MS extensions are enabled, this is handled as an
4821117421Skan	     anonymous struct.
4822117421Skan	   Otherwise this is a forward declaration of a structure tag.
4823117421Skan
4824117421Skan	 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4825117421Skan	   If MS extensions are enabled and foo names a structure, then
4826117421Skan	     again this is an anonymous struct.
4827117421Skan	   Otherwise this is an error.
4828117421Skan
4829132730Skan	 Oh what a horrid tangled web we weave.  I wonder if MS consciously
4830117421Skan	 took this from Plan 9 or if it was an accident of implementation
4831117421Skan	 that took root before someone noticed the bug...  */
4832117421Skan
4833102790Skan      tree type = TREE_VALUE (declspecs);
4834102790Skan
4835117421Skan      if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4836102790Skan	type = TREE_TYPE (type);
4837117421Skan      if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
483890075Sobrien	{
4839117421Skan	  if (flag_ms_extensions)
4840117421Skan	    ; /* ok */
4841117421Skan	  else if (flag_iso)
4842117421Skan	    goto warn_unnamed_field;
4843117421Skan	  else if (TYPE_NAME (type) == NULL)
4844117421Skan	    ; /* ok */
4845117421Skan	  else
4846117421Skan	    goto warn_unnamed_field;
4847117421Skan	}
4848117421Skan      else
4849117421Skan	{
4850117421Skan	warn_unnamed_field:
4851117421Skan	  warning ("declaration does not declare anything");
485290075Sobrien	  return NULL_TREE;
485390075Sobrien	}
485490075Sobrien    }
485518334Speter
4856132730Skan  value = grokdeclarator (declarator, declspecs, FIELD, 0,
4857132730Skan			  width ? &width : NULL);
485818334Speter
485918334Speter  finish_decl (value, NULL_TREE, NULL_TREE);
486096263Sobrien  DECL_INITIAL (value) = width;
486118334Speter
486218334Speter  return value;
486318334Speter}
486418334Speter
4865132730Skan/* Generate an error for any duplicate field names in FIELDLIST.  Munge
4866132730Skan   the list such that this does not present a problem later.  */
4867122196Skan
4868132730Skanstatic void
4869132730Skandetect_field_duplicates (tree fieldlist)
4870132730Skan{
4871132730Skan  tree x, y;
4872132730Skan  int timeout = 10;
4873122196Skan
4874132730Skan  /* First, see if there are more than "a few" fields.
4875132730Skan     This is trivially true if there are zero or one fields.  */
4876132730Skan  if (!fieldlist)
4877132730Skan    return;
4878132730Skan  x = TREE_CHAIN (fieldlist);
4879132730Skan  if (!x)
4880132730Skan    return;
4881132730Skan  do {
4882132730Skan    timeout--;
4883132730Skan    x = TREE_CHAIN (x);
4884132730Skan  } while (timeout > 0 && x);
4885132730Skan
4886132730Skan  /* If there were "few" fields, avoid the overhead of allocating
4887132730Skan     a hash table.  Instead just do the nested traversal thing.  */
4888132730Skan  if (timeout > 0)
4889132730Skan    {
4890132730Skan      for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4891132730Skan	if (DECL_NAME (x))
4892132730Skan	  {
4893132730Skan	    for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4894132730Skan	      if (DECL_NAME (y) == DECL_NAME (x))
4895132730Skan		{
4896132730Skan		  error ("%Jduplicate member '%D'", x, x);
4897132730Skan		  DECL_NAME (x) = NULL_TREE;
4898132730Skan		}
4899132730Skan	  }
4900132730Skan    }
4901132730Skan  else
4902132730Skan    {
4903132730Skan      htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4904132730Skan      void **slot;
4905132730Skan
4906132730Skan      for (x = fieldlist; x ; x = TREE_CHAIN (x))
4907132730Skan	if ((y = DECL_NAME (x)) != 0)
4908132730Skan	  {
4909132730Skan	    slot = htab_find_slot (htab, y, INSERT);
4910132730Skan	    if (*slot)
4911132730Skan	      {
4912132730Skan		error ("%Jduplicate member '%D'", x, x);
4913132730Skan		DECL_NAME (x) = NULL_TREE;
4914132730Skan	      }
4915132730Skan	    *slot = y;
4916132730Skan	  }
4917132730Skan
4918132730Skan      htab_delete (htab);
4919132730Skan    }
4920122196Skan}
4921132730Skan
492218334Speter/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
492318334Speter   FIELDLIST is a chain of FIELD_DECL nodes for the fields.
492490075Sobrien   ATTRIBUTES are attributes to be applied to the structure.  */
492518334Speter
492618334Spetertree
4927132730Skanfinish_struct (tree t, tree fieldlist, tree attributes)
492818334Speter{
492990075Sobrien  tree x;
4930132730Skan  int toplevel = global_scope == current_scope;
493190075Sobrien  int saw_named_field;
493218334Speter
493318334Speter  /* If this type was previously laid out as a forward reference,
493418334Speter     make sure we lay it out again.  */
493518334Speter
493618334Speter  TYPE_SIZE (t) = 0;
493718334Speter
493890075Sobrien  decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
493918334Speter
494018334Speter  /* Nameless union parm types are useful as GCC extension.  */
494118334Speter  if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
494218334Speter    /* Otherwise, warn about any struct or union def. in parmlist.  */
494318334Speter    if (in_parm_level_p ())
494418334Speter      {
494518334Speter	if (pedantic)
494690075Sobrien	  pedwarn ("%s defined inside parms",
494790075Sobrien		   TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4948117421Skan	else
494990075Sobrien	  warning ("%s defined inside parms",
495090075Sobrien		   TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
495118334Speter      }
495218334Speter
495350397Sobrien  if (pedantic)
495450397Sobrien    {
495550397Sobrien      for (x = fieldlist; x; x = TREE_CHAIN (x))
495650397Sobrien	if (DECL_NAME (x) != 0)
495750397Sobrien	  break;
495818334Speter
495950397Sobrien      if (x == 0)
496090075Sobrien	pedwarn ("%s has no %s",
496190075Sobrien		 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
496290075Sobrien		 fieldlist ? _("named members") : _("members"));
496350397Sobrien    }
496450397Sobrien
496596263Sobrien  /* Install struct as DECL_CONTEXT of each field decl.
496696263Sobrien     Also process specified field sizes,m which is found in the DECL_INITIAL.
496796263Sobrien     Store 0 there, except for ": 0" fields (so we can find them
496896263Sobrien     and delete them, below).  */
496918334Speter
497090075Sobrien  saw_named_field = 0;
497118334Speter  for (x = fieldlist; x; x = TREE_CHAIN (x))
497218334Speter    {
497318334Speter      DECL_CONTEXT (x) = t;
497418334Speter      DECL_PACKED (x) |= TYPE_PACKED (t);
497518334Speter
497618334Speter      /* If any field is const, the structure type is pseudo-const.  */
497718334Speter      if (TREE_READONLY (x))
497818334Speter	C_TYPE_FIELDS_READONLY (t) = 1;
497918334Speter      else
498018334Speter	{
498118334Speter	  /* A field that is pseudo-const makes the structure likewise.  */
498218334Speter	  tree t1 = TREE_TYPE (x);
498318334Speter	  while (TREE_CODE (t1) == ARRAY_TYPE)
498418334Speter	    t1 = TREE_TYPE (t1);
498518334Speter	  if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
498618334Speter	      && C_TYPE_FIELDS_READONLY (t1))
498718334Speter	    C_TYPE_FIELDS_READONLY (t) = 1;
498818334Speter	}
498918334Speter
499018334Speter      /* Any field that is volatile means variables of this type must be
499118334Speter	 treated in some ways as volatile.  */
499218334Speter      if (TREE_THIS_VOLATILE (x))
499318334Speter	C_TYPE_FIELDS_VOLATILE (t) = 1;
499418334Speter
499518334Speter      /* Any field of nominal variable size implies structure is too.  */
499618334Speter      if (C_DECL_VARIABLE_SIZE (x))
499718334Speter	C_TYPE_VARIABLE_SIZE (t) = 1;
499818334Speter
499918334Speter      /* Detect invalid nested redefinition.  */
500018334Speter      if (TREE_TYPE (x) == t)
500118334Speter	error ("nested redefinition of `%s'",
500218334Speter	       IDENTIFIER_POINTER (TYPE_NAME (t)));
500318334Speter
500496263Sobrien      if (DECL_INITIAL (x))
500518334Speter	{
5006132730Skan	  unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5007132730Skan	  DECL_SIZE (x) = bitsize_int (width);
5008132730Skan	  DECL_BIT_FIELD (x) = 1;
5009132730Skan	  SET_DECL_C_BIT_FIELD (x);
501096263Sobrien	}
501196263Sobrien
501296263Sobrien      DECL_INITIAL (x) = 0;
501396263Sobrien
501490075Sobrien      /* Detect flexible array member in an invalid context.  */
501590075Sobrien      if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
501690075Sobrien	  && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
501790075Sobrien	  && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
501890075Sobrien	  && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
501918334Speter	{
502090075Sobrien	  if (TREE_CODE (t) == UNION_TYPE)
5021132730Skan	    {
5022132730Skan	      error ("%Jflexible array member in union", x);
5023132730Skan	      TREE_TYPE (x) = error_mark_node;
5024132730Skan	    }
502590075Sobrien	  else if (TREE_CHAIN (x) != NULL_TREE)
5026132730Skan	    {
5027132730Skan	      error ("%Jflexible array member not at end of struct", x);
5028132730Skan	      TREE_TYPE (x) = error_mark_node;
5029132730Skan	    }
503090075Sobrien	  else if (! saw_named_field)
5031132730Skan	    {
5032132730Skan	      error ("%Jflexible array member in otherwise empty struct", x);
5033132730Skan	      TREE_TYPE (x) = error_mark_node;
5034132730Skan	    }
503518334Speter	}
5036117421Skan
5037132730Skan      if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5038117421Skan	  && flexible_array_type_p (TREE_TYPE (x)))
5039132730Skan	pedwarn ("%Jinvalid use of structure with flexible array member", x);
5040117421Skan
504190075Sobrien      if (DECL_NAME (x))
504290075Sobrien	saw_named_field = 1;
504318334Speter    }
504418334Speter
5045132730Skan  detect_field_duplicates (fieldlist);
504690075Sobrien
504718334Speter  /* Now we have the nearly final fieldlist.  Record it,
504818334Speter     then lay out the structure or union (including the fields).  */
504918334Speter
505018334Speter  TYPE_FIELDS (t) = fieldlist;
505118334Speter
505218334Speter  layout_type (t);
505318334Speter
5054132730Skan  /* Delete all zero-width bit-fields from the fieldlist.  */
505590075Sobrien  {
505690075Sobrien    tree *fieldlistp = &fieldlist;
505790075Sobrien    while (*fieldlistp)
505890075Sobrien      if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
505990075Sobrien	*fieldlistp = TREE_CHAIN (*fieldlistp);
506090075Sobrien      else
506190075Sobrien	fieldlistp = &TREE_CHAIN (*fieldlistp);
506290075Sobrien  }
506318334Speter
506490075Sobrien  /* Now we have the truly final field list.
506590075Sobrien     Store it in this type and in the variants.  */
506618334Speter
506718334Speter  TYPE_FIELDS (t) = fieldlist;
506818334Speter
5069122196Skan  /* If there are lots of fields, sort so we can look through them fast.
5070132730Skan     We arbitrarily consider 16 or more elts to be "a lot".  */
5071122196Skan
5072122196Skan  {
5073122196Skan    int len = 0;
5074122196Skan
5075122196Skan    for (x = fieldlist; x; x = TREE_CHAIN (x))
5076122196Skan      {
5077122196Skan        if (len > 15 || DECL_NAME (x) == NULL)
5078122196Skan          break;
5079122196Skan        len += 1;
5080122196Skan      }
5081122196Skan
5082122196Skan    if (len > 15)
5083122196Skan      {
5084122196Skan        tree *field_array;
5085132730Skan        struct lang_type *space;
5086132730Skan        struct sorted_fields_type *space2;
5087132730Skan
5088122196Skan        len += list_length (x);
5089132730Skan
5090122196Skan        /* Use the same allocation policy here that make_node uses, to
5091122196Skan          ensure that this lives as long as the rest of the struct decl.
5092122196Skan          All decls in an inline function need to be saved.  */
5093132730Skan
5094132730Skan        space = ggc_alloc (sizeof (struct lang_type));
5095132730Skan        space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5096132730Skan
5097122196Skan        len = 0;
5098132730Skan	space->s = space2;
5099132730Skan	field_array = &space2->elts[0];
5100122196Skan        for (x = fieldlist; x; x = TREE_CHAIN (x))
5101122196Skan          {
5102122196Skan            field_array[len++] = x;
5103132730Skan
5104132730Skan            /* If there is anonymous struct or union, break out of the loop.  */
5105122196Skan            if (DECL_NAME (x) == NULL)
5106122196Skan              break;
5107122196Skan          }
5108132730Skan        /* Found no anonymous struct/union.  Add the TYPE_LANG_SPECIFIC.  */
5109122196Skan        if (x == NULL)
5110122196Skan          {
5111132730Skan            TYPE_LANG_SPECIFIC (t) = space;
5112132730Skan            TYPE_LANG_SPECIFIC (t)->s->len = len;
5113132730Skan            field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5114122196Skan            qsort (field_array, len, sizeof (tree), field_decl_cmp);
5115122196Skan          }
5116122196Skan      }
5117122196Skan  }
5118132730Skan
511918334Speter  for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
512018334Speter    {
512118334Speter      TYPE_FIELDS (x) = TYPE_FIELDS (t);
512218334Speter      TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
512318334Speter      TYPE_ALIGN (x) = TYPE_ALIGN (t);
512490075Sobrien      TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
512518334Speter    }
512618334Speter
512718334Speter  /* If this was supposed to be a transparent union, but we can't
512818334Speter     make it one, warn and turn off the flag.  */
512918334Speter  if (TREE_CODE (t) == UNION_TYPE
513018334Speter      && TYPE_TRANSPARENT_UNION (t)
513118334Speter      && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
513218334Speter    {
513318334Speter      TYPE_TRANSPARENT_UNION (t) = 0;
513450397Sobrien      warning ("union cannot be made transparent");
513518334Speter    }
513618334Speter
513718334Speter  /* If this structure or union completes the type of any previous
513818334Speter     variable declaration, lay it out and output its rtl.  */
5139132730Skan  for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5140132730Skan       x;
5141132730Skan       x = TREE_CHAIN (x))
514218334Speter    {
5143132730Skan      tree decl = TREE_VALUE (x);
5144132730Skan      if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5145132730Skan	layout_array_type (TREE_TYPE (decl));
5146132730Skan      if (TREE_CODE (decl) != TYPE_DECL)
5147132730Skan	{
5148132730Skan	  layout_decl (decl, 0);
5149132730Skan	  if (c_dialect_objc ())
5150132730Skan	    objc_check_decl (decl);
5151132730Skan	  rest_of_decl_compilation (decl, NULL, toplevel, 0);
5152132730Skan	  if (! toplevel)
5153132730Skan	    expand_decl (decl);
515418334Speter	}
515518334Speter    }
5156132730Skan  C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
515718334Speter
515818334Speter  /* Finish debugging output for this type.  */
515918334Speter  rest_of_type_compilation (t, toplevel);
516018334Speter
516118334Speter  return t;
516218334Speter}
516318334Speter
516418334Speter/* Lay out the type T, and its element type, and so on.  */
516518334Speter
516618334Speterstatic void
5167132730Skanlayout_array_type (tree t)
516818334Speter{
516918334Speter  if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
517018334Speter    layout_array_type (TREE_TYPE (t));
517118334Speter  layout_type (t);
517218334Speter}
517318334Speter
517418334Speter/* Begin compiling the definition of an enumeration type.
517518334Speter   NAME is its name (or null if anonymous).
517618334Speter   Returns the type object, as yet incomplete.
517718334Speter   Also records info about it so that build_enumerator
517818334Speter   may be used to declare the individual values as they are read.  */
517918334Speter
518018334Spetertree
5181132730Skanstart_enum (tree name)
518218334Speter{
518390075Sobrien  tree enumtype = 0;
518418334Speter
518518334Speter  /* If this is the real definition for a previous forward reference,
518618334Speter     fill in the contents in the same object that used to be the
518718334Speter     forward reference.  */
518818334Speter
518918334Speter  if (name != 0)
5190132730Skan    enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
519118334Speter
519218334Speter  if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
519318334Speter    {
519418334Speter      enumtype = make_node (ENUMERAL_TYPE);
519518334Speter      pushtag (name, enumtype);
519618334Speter    }
519718334Speter
519818334Speter  C_TYPE_BEING_DEFINED (enumtype) = 1;
519918334Speter
520018334Speter  if (TYPE_VALUES (enumtype) != 0)
520118334Speter    {
520218334Speter      /* This enum is a named one that has been declared already.  */
520318334Speter      error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
520418334Speter
520518334Speter      /* Completely replace its old definition.
520618334Speter	 The old enumerators remain defined, however.  */
520718334Speter      TYPE_VALUES (enumtype) = 0;
520818334Speter    }
520918334Speter
521018334Speter  enum_next_value = integer_zero_node;
521118334Speter  enum_overflow = 0;
521218334Speter
521350397Sobrien  if (flag_short_enums)
521450397Sobrien    TYPE_PACKED (enumtype) = 1;
521550397Sobrien
521618334Speter  return enumtype;
521718334Speter}
521818334Speter
521918334Speter/* After processing and defining all the values of an enumeration type,
522018334Speter   install their decls in the enumeration type and finish it off.
522118334Speter   ENUMTYPE is the type object, VALUES a list of decl-value pairs,
522218334Speter   and ATTRIBUTES are the specified attributes.
522318334Speter   Returns ENUMTYPE.  */
522418334Speter
522518334Spetertree
5226132730Skanfinish_enum (tree enumtype, tree values, tree attributes)
522718334Speter{
522890075Sobrien  tree pair, tem;
522990075Sobrien  tree minnode = 0, maxnode = 0, enum_value_type;
523090075Sobrien  int precision, unsign;
5231132730Skan  int toplevel = (global_scope == current_scope);
523218334Speter
523318334Speter  if (in_parm_level_p ())
523418334Speter    warning ("enum defined inside parms");
523518334Speter
523690075Sobrien  decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
523718334Speter
523818334Speter  /* Calculate the maximum value of any enumerator in this type.  */
523918334Speter
524018334Speter  if (values == error_mark_node)
524118334Speter    minnode = maxnode = integer_zero_node;
524218334Speter  else
524390075Sobrien    {
524490075Sobrien      minnode = maxnode = TREE_VALUE (values);
524590075Sobrien      for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
524690075Sobrien	{
524790075Sobrien	  tree value = TREE_VALUE (pair);
524890075Sobrien	  if (tree_int_cst_lt (maxnode, value))
524990075Sobrien	    maxnode = value;
525090075Sobrien	  if (tree_int_cst_lt (value, minnode))
525190075Sobrien	    minnode = value;
525290075Sobrien	}
525390075Sobrien    }
525418334Speter
525590075Sobrien  /* Construct the final type of this enumeration.  It is the same
525690075Sobrien     as one of the integral types - the narrowest one that fits, except
525790075Sobrien     that normally we only go as narrow as int - and signed iff any of
525890075Sobrien     the values are negative.  */
525990075Sobrien  unsign = (tree_int_cst_sgn (minnode) >= 0);
526090075Sobrien  precision = MAX (min_precision (minnode, unsign),
526190075Sobrien		   min_precision (maxnode, unsign));
526250397Sobrien  if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
526350397Sobrien    {
5264117421Skan      tree narrowest = c_common_type_for_size (precision, unsign);
526550397Sobrien      if (narrowest == 0)
526650397Sobrien	{
526750397Sobrien	  warning ("enumeration values exceed range of largest integer");
526850397Sobrien	  narrowest = long_long_integer_type_node;
526950397Sobrien	}
527050397Sobrien
527190075Sobrien      precision = TYPE_PRECISION (narrowest);
527250397Sobrien    }
527318334Speter  else
527490075Sobrien    precision = TYPE_PRECISION (integer_type_node);
527518334Speter
527690075Sobrien  if (precision == TYPE_PRECISION (integer_type_node))
5277117421Skan    enum_value_type = c_common_type_for_size (precision, 0);
527890075Sobrien  else
527990075Sobrien    enum_value_type = enumtype;
528090075Sobrien
528190075Sobrien  TYPE_MIN_VALUE (enumtype) = minnode;
528290075Sobrien  TYPE_MAX_VALUE (enumtype) = maxnode;
528390075Sobrien  TYPE_PRECISION (enumtype) = precision;
528490075Sobrien  TREE_UNSIGNED (enumtype) = unsign;
528518334Speter  TYPE_SIZE (enumtype) = 0;
528618334Speter  layout_type (enumtype);
528718334Speter
528818334Speter  if (values != error_mark_node)
528918334Speter    {
529090075Sobrien      /* Change the type of the enumerators to be the enum type.  We
529190075Sobrien	 need to do this irrespective of the size of the enum, for
529290075Sobrien	 proper type checking.  Replace the DECL_INITIALs of the
529390075Sobrien	 enumerators, and the value slots of the list, with copies
529490075Sobrien	 that have the enum type; they cannot be modified in place
529590075Sobrien	 because they may be shared (e.g.  integer_zero_node) Finally,
529690075Sobrien	 change the purpose slots to point to the names of the decls.  */
529718334Speter      for (pair = values; pair; pair = TREE_CHAIN (pair))
529818334Speter	{
529990075Sobrien	  tree enu = TREE_PURPOSE (pair);
530090075Sobrien
530190075Sobrien	  TREE_TYPE (enu) = enumtype;
530290075Sobrien
530390075Sobrien	  /* The ISO C Standard mandates enumerators to have type int,
530490075Sobrien	     even though the underlying type of an enum type is
530590075Sobrien	     unspecified.  Here we convert any enumerators that fit in
530690075Sobrien	     an int to type int, to avoid promotions to unsigned types
530790075Sobrien	     when comparing integers with enumerators that fit in the
530890075Sobrien	     int range.  When -pedantic is given, build_enumerator()
530990075Sobrien	     would have already taken care of those that don't fit.  */
531090075Sobrien	  if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
531190075Sobrien	    DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
531290075Sobrien	  else
531390075Sobrien	    DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
531490075Sobrien
531590075Sobrien	  TREE_PURPOSE (pair) = DECL_NAME (enu);
531690075Sobrien	  TREE_VALUE (pair) = DECL_INITIAL (enu);
531718334Speter	}
531818334Speter
531918334Speter      TYPE_VALUES (enumtype) = values;
532018334Speter    }
532118334Speter
532218334Speter  /* Fix up all variant types of this enum type.  */
532318334Speter  for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
532418334Speter    {
532590075Sobrien      if (tem == enumtype)
532690075Sobrien	continue;
532718334Speter      TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
532818334Speter      TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
532918334Speter      TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
533018334Speter      TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
533150397Sobrien      TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
533218334Speter      TYPE_MODE (tem) = TYPE_MODE (enumtype);
533318334Speter      TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
533418334Speter      TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
533590075Sobrien      TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
533618334Speter      TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
533718334Speter    }
533818334Speter
533918334Speter  /* Finish debugging output for this type.  */
534018334Speter  rest_of_type_compilation (enumtype, toplevel);
534118334Speter
534218334Speter  return enumtype;
534318334Speter}
534418334Speter
534518334Speter/* Build and install a CONST_DECL for one value of the
534618334Speter   current enumeration type (one that was begun with start_enum).
534718334Speter   Return a tree-list containing the CONST_DECL and its value.
534818334Speter   Assignment of sequential values by default is handled here.  */
534918334Speter
535018334Spetertree
5351132730Skanbuild_enumerator (tree name, tree value)
535218334Speter{
535390075Sobrien  tree decl, type;
535418334Speter
535518334Speter  /* Validate and default VALUE.  */
535618334Speter
535718334Speter  /* Remove no-op casts from the value.  */
535818334Speter  if (value)
535918334Speter    STRIP_TYPE_NOPS (value);
536018334Speter
536118334Speter  if (value != 0)
536218334Speter    {
536318334Speter      if (TREE_CODE (value) == INTEGER_CST)
536418334Speter	{
536518334Speter	  value = default_conversion (value);
536618334Speter	  constant_expression_warning (value);
536718334Speter	}
536818334Speter      else
536918334Speter	{
537018334Speter	  error ("enumerator value for `%s' not integer constant",
537118334Speter		 IDENTIFIER_POINTER (name));
537218334Speter	  value = 0;
537318334Speter	}
537418334Speter    }
537518334Speter
537618334Speter  /* Default based on previous value.  */
537718334Speter  /* It should no longer be possible to have NON_LVALUE_EXPR
537818334Speter     in the default.  */
537918334Speter  if (value == 0)
538018334Speter    {
538118334Speter      value = enum_next_value;
538218334Speter      if (enum_overflow)
538318334Speter	error ("overflow in enumeration values");
538418334Speter    }
538518334Speter
538618334Speter  if (pedantic && ! int_fits_type_p (value, integer_type_node))
538718334Speter    {
538890075Sobrien      pedwarn ("ISO C restricts enumerator values to range of `int'");
538990075Sobrien      value = convert (integer_type_node, value);
539018334Speter    }
539118334Speter
539218334Speter  /* Set basis for default for next value.  */
539318334Speter  enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
539418334Speter  enum_overflow = tree_int_cst_lt (enum_next_value, value);
539518334Speter
539618334Speter  /* Now create a declaration for the enum value name.  */
539718334Speter
539818334Speter  type = TREE_TYPE (value);
5399117421Skan  type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5400117421Skan				      TYPE_PRECISION (integer_type_node)),
5401117421Skan				 (TYPE_PRECISION (type)
5402117421Skan				  >= TYPE_PRECISION (integer_type_node)
5403117421Skan				  && TREE_UNSIGNED (type)));
540418334Speter
540518334Speter  decl = build_decl (CONST_DECL, name, type);
540690075Sobrien  DECL_INITIAL (decl) = convert (type, value);
540718334Speter  pushdecl (decl);
540818334Speter
540990075Sobrien  return tree_cons (decl, value, NULL_TREE);
541018334Speter}
541190075Sobrien
541218334Speter
541318334Speter/* Create the FUNCTION_DECL for a function definition.
541490075Sobrien   DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
541518334Speter   the declaration; they describe the function's name and the type it returns,
541618334Speter   but twisted together in a fashion that parallels the syntax of C.
541718334Speter
541818334Speter   This function creates a binding context for the function body
541918334Speter   as well as setting up the FUNCTION_DECL in current_function_decl.
542018334Speter
542118334Speter   Returns 1 on success.  If the DECLARATOR is not suitable for a function
542218334Speter   (it defines a datum instead), we return 0, which tells
542390075Sobrien   yyparse to report a parse error.  */
542418334Speter
542518334Speterint
5426132730Skanstart_function (tree declspecs, tree declarator, tree attributes)
542718334Speter{
542818334Speter  tree decl1, old_decl;
542918334Speter  tree restype;
543018334Speter  int old_immediate_size_expand = immediate_size_expand;
543118334Speter
543250397Sobrien  current_function_returns_value = 0;  /* Assume, until we see it does.  */
543318334Speter  current_function_returns_null = 0;
543496263Sobrien  current_function_returns_abnormally = 0;
543518334Speter  warn_about_return_type = 0;
543618334Speter  current_extern_inline = 0;
5437132730Skan  c_in_iteration_stmt = 0;
5438132730Skan  c_in_case_stmt = 0;
543918334Speter
544018334Speter  /* Don't expand any sizes in the return type of the function.  */
544118334Speter  immediate_size_expand = 0;
544218334Speter
5443132730Skan  decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
544418334Speter
544518334Speter  /* If the declarator is not suitable for a function definition,
544618334Speter     cause a syntax error.  */
544718334Speter  if (decl1 == 0)
544850397Sobrien    {
544950397Sobrien      immediate_size_expand = old_immediate_size_expand;
545050397Sobrien      return 0;
545150397Sobrien    }
545218334Speter
545390075Sobrien  decl_attributes (&decl1, attributes, 0);
545418334Speter
545590075Sobrien  if (DECL_DECLARED_INLINE_P (decl1)
545690075Sobrien      && DECL_UNINLINABLE (decl1)
545790075Sobrien      && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5458132730Skan    warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
545990075Sobrien
546018334Speter  announce_function (decl1);
546118334Speter
546290075Sobrien  if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
546318334Speter    {
546490075Sobrien      error ("return type is an incomplete type");
546518334Speter      /* Make it return void instead.  */
546618334Speter      TREE_TYPE (decl1)
546718334Speter	= build_function_type (void_type_node,
546818334Speter			       TYPE_ARG_TYPES (TREE_TYPE (decl1)));
546918334Speter    }
547018334Speter
547118334Speter  if (warn_about_return_type)
547290075Sobrien    pedwarn_c99 ("return type defaults to `int'");
547318334Speter
547418334Speter  /* Save the parm names or decls from this function's declarator
547518334Speter     where store_parm_decls will find them.  */
547618334Speter  current_function_parms = last_function_parms;
547718334Speter  current_function_parm_tags = last_function_parm_tags;
5478132730Skan  current_function_parm_others = last_function_parm_others;
547918334Speter
548018334Speter  /* Make the init_value nonzero so pushdecl knows this is not tentative.
548118334Speter     error_mark_node is replaced below (in poplevel) with the BLOCK.  */
548218334Speter  DECL_INITIAL (decl1) = error_mark_node;
548318334Speter
548418334Speter  /* If this definition isn't a prototype and we had a prototype declaration
548518334Speter     before, copy the arg type info from that prototype.
548618334Speter     But not if what we had before was a builtin function.  */
548718334Speter  old_decl = lookup_name_current_level (DECL_NAME (decl1));
548818334Speter  if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
548918334Speter      && !DECL_BUILT_IN (old_decl)
549018334Speter      && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
549118334Speter	  == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
549218334Speter      && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
549318334Speter    {
549418334Speter      TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5495132730Skan      current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
549618334Speter    }
549718334Speter
549818334Speter  /* Optionally warn of old-fashioned def with no previous prototype.  */
549918334Speter  if (warn_strict_prototypes
550018334Speter      && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5501132730Skan      && C_DECL_ISNT_PROTOTYPE (old_decl))
550218334Speter    warning ("function declaration isn't a prototype");
550318334Speter  /* Optionally warn of any global def with no previous prototype.  */
550418334Speter  else if (warn_missing_prototypes
550518334Speter	   && TREE_PUBLIC (decl1)
5506132730Skan	   && ! MAIN_NAME_P (DECL_NAME (decl1))
5507132730Skan	   && C_DECL_ISNT_PROTOTYPE (old_decl))
5508132730Skan    warning ("%Jno previous prototype for '%D'", decl1, decl1);
550918334Speter  /* Optionally warn of any def with no previous prototype
551018334Speter     if the function has already been used.  */
551118334Speter  else if (warn_missing_prototypes
551218334Speter	   && old_decl != 0 && TREE_USED (old_decl)
551318334Speter	   && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5514132730Skan    warning ("%J'%D' was used with no prototype before its definition",
5515132730Skan	     decl1, decl1);
551618334Speter  /* Optionally warn of any global def with no previous declaration.  */
551718334Speter  else if (warn_missing_declarations
551818334Speter	   && TREE_PUBLIC (decl1)
551918334Speter	   && old_decl == 0
552090075Sobrien	   && ! MAIN_NAME_P (DECL_NAME (decl1)))
5521132730Skan    warning ("%Jno previous declaration for '%D'", decl1, decl1);
552218334Speter  /* Optionally warn of any def with no previous declaration
552318334Speter     if the function has already been used.  */
552418334Speter  else if (warn_missing_declarations
552518334Speter	   && old_decl != 0 && TREE_USED (old_decl)
5526132730Skan	   && C_DECL_IMPLICIT (old_decl))
5527132730Skan    warning ("%J`%D' was used with no declaration before its definition",
5528132730Skan	     decl1, decl1);
552918334Speter
553018334Speter  /* This is a definition, not a reference.
553118334Speter     So normally clear DECL_EXTERNAL.
553218334Speter     However, `extern inline' acts like a declaration
553318334Speter     except for defining how to inline.  So set DECL_EXTERNAL in that case.  */
553418334Speter  DECL_EXTERNAL (decl1) = current_extern_inline;
553518334Speter
553618334Speter  /* This function exists in static storage.
553718334Speter     (This does not mean `static' in the C sense!)  */
553818334Speter  TREE_STATIC (decl1) = 1;
553918334Speter
554018334Speter  /* A nested function is not global.  */
554118334Speter  if (current_function_decl != 0)
554218334Speter    TREE_PUBLIC (decl1) = 0;
554318334Speter
5544132730Skan#ifdef ENABLE_CHECKING
5545132730Skan  /* This is the earliest point at which we might know the assembler
5546132730Skan     name of the function.  Thus, if it's set before this, die horribly.  */
5547132730Skan  if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5548132730Skan    abort ();
5549132730Skan#endif
5550132730Skan
5551132730Skan  /* If #pragma weak was used, mark the decl weak now.  */
5552132730Skan  if (current_scope == global_scope)
5553132730Skan    maybe_apply_pragma_weak (decl1);
5554132730Skan
555590075Sobrien  /* Warn for unlikely, improbable, or stupid declarations of `main'.  */
555690075Sobrien  if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
555750397Sobrien    {
555850397Sobrien      tree args;
555950397Sobrien      int argct = 0;
556050397Sobrien
556150397Sobrien      if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
556290075Sobrien	  != integer_type_node)
5563132730Skan	pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
556450397Sobrien
556550397Sobrien      for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
556650397Sobrien	   args = TREE_CHAIN (args))
556750397Sobrien	{
556850397Sobrien	  tree type = args ? TREE_VALUE (args) : 0;
556950397Sobrien
557050397Sobrien	  if (type == void_type_node)
557150397Sobrien	    break;
557250397Sobrien
557350397Sobrien	  ++argct;
557450397Sobrien	  switch (argct)
557550397Sobrien	    {
557650397Sobrien	    case 1:
557750397Sobrien	      if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5578132730Skan		pedwarn ("%Jfirst argument of '%D' should be `int'",
5579132730Skan			 decl1, decl1);
558050397Sobrien	      break;
558150397Sobrien
558250397Sobrien	    case 2:
558350397Sobrien	      if (TREE_CODE (type) != POINTER_TYPE
558450397Sobrien		  || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
558550397Sobrien		  || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
558650397Sobrien		      != char_type_node))
5587132730Skan		pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5588132730Skan                         decl1, decl1);
558950397Sobrien	      break;
559050397Sobrien
559150397Sobrien	    case 3:
559250397Sobrien	      if (TREE_CODE (type) != POINTER_TYPE
559350397Sobrien		  || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
559450397Sobrien		  || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
559550397Sobrien		      != char_type_node))
5596132730Skan		pedwarn ("%Jthird argument of '%D' should probably be "
5597132730Skan                         "'char **'", decl1, decl1);
559850397Sobrien	      break;
559950397Sobrien	    }
560050397Sobrien	}
560150397Sobrien
560250397Sobrien      /* It is intentional that this message does not mention the third
560390075Sobrien	 argument because it's only mentioned in an appendix of the
560490075Sobrien	 standard.  */
560550397Sobrien      if (argct > 0 && (argct < 2 || argct > 3))
5606132730Skan	pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
560750397Sobrien
560850397Sobrien      if (! TREE_PUBLIC (decl1))
5609132730Skan	pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
561050397Sobrien    }
561150397Sobrien
561218334Speter  /* Record the decl so that the function name is defined.
561318334Speter     If we already have a decl for this name, and it is a FUNCTION_DECL,
561418334Speter     use the old decl.  */
561518334Speter
561618334Speter  current_function_decl = pushdecl (decl1);
561718334Speter
561818334Speter  pushlevel (0);
5619132730Skan  declare_parm_level ();
562018334Speter
562190075Sobrien  make_decl_rtl (current_function_decl, NULL);
562218334Speter
562318334Speter  restype = TREE_TYPE (TREE_TYPE (current_function_decl));
562418334Speter  /* Promote the value to int before returning it.  */
562590075Sobrien  if (c_promoting_integer_type_p (restype))
562618334Speter    {
5627117421Skan      /* It retains unsignedness if not really getting wider.  */
562818334Speter      if (TREE_UNSIGNED (restype)
5629117421Skan	  && (TYPE_PRECISION (restype)
5630117421Skan		  == TYPE_PRECISION (integer_type_node)))
563118334Speter	restype = unsigned_type_node;
563218334Speter      else
563318334Speter	restype = integer_type_node;
563418334Speter    }
563518334Speter  DECL_RESULT (current_function_decl)
563618334Speter    = build_decl (RESULT_DECL, NULL_TREE, restype);
563718334Speter
563818334Speter  /* If this fcn was already referenced via a block-scope `extern' decl
563918334Speter     (or an implicit decl), propagate certain information about the usage.  */
564018334Speter  if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
564118334Speter    TREE_ADDRESSABLE (current_function_decl) = 1;
564218334Speter
564318334Speter  immediate_size_expand = old_immediate_size_expand;
564418334Speter
564590075Sobrien  start_fname_decls ();
5646132730Skan
564718334Speter  return 1;
564818334Speter}
564918334Speter
5650132730Skan/* Subroutine of store_parm_decls which handles new-style function
5651132730Skan   definitions (prototype format). The parms already have decls, so we
5652132730Skan   need only record them as in effect and complain if any redundant
5653132730Skan   old-style parm decls were written.  */
5654132730Skanstatic void
5655132730Skanstore_parm_decls_newstyle (void)
565618334Speter{
5657132730Skan  tree decl, last;
565890075Sobrien  tree fndecl = current_function_decl;
5659132730Skan  tree parms = current_function_parms;
5660132730Skan  tree tags = current_function_parm_tags;
5661132730Skan  tree others = current_function_parm_others;
566218334Speter
5663132730Skan  if (current_scope->parms || current_scope->names || current_scope->tags)
5664132730Skan    {
5665132730Skan      error ("%Jold-style parameter declarations in prototyped "
5666132730Skan	     "function definition", fndecl);
566718334Speter
5668132730Skan      /* Get rid of the old-style declarations.  */
5669132730Skan      poplevel (0, 0, 0);
5670132730Skan      pushlevel (0);
5671132730Skan    }
567218334Speter
5673132730Skan  /* Now make all the parameter declarations visible in the function body.
5674132730Skan     We can bypass most of the grunt work of pushdecl.  */
5675132730Skan  for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5676132730Skan    {
5677132730Skan      DECL_CONTEXT (decl) = current_function_decl;
5678132730Skan      if (DECL_NAME (decl) == 0)
5679132730Skan	error ("%Jparameter name omitted", decl);
5680132730Skan      else
5681132730Skan	{
5682132730Skan	  if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5683132730Skan	    current_scope->shadowed
5684132730Skan	      = tree_cons (DECL_NAME (decl),
5685132730Skan			   IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5686132730Skan			   current_scope->shadowed);
5687132730Skan	  IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5688132730Skan	}
5689132730Skan    }
5690132730Skan  current_scope->parms = parms;
5691132730Skan  current_scope->parms_last = last;
569218334Speter
5693132730Skan  /* Record the parameter list in the function declaration.  */
5694132730Skan  DECL_ARGUMENTS (fndecl) = parms;
569518334Speter
5696132730Skan  /* Now make all the ancillary declarations visible, likewise.  */
5697132730Skan  for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5698132730Skan    {
5699132730Skan      DECL_CONTEXT (decl) = current_function_decl;
5700132730Skan      if (DECL_NAME (decl)
5701132730Skan	  && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5702132730Skan	{
5703132730Skan	  if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5704132730Skan	    current_scope->shadowed
5705132730Skan	      = tree_cons (DECL_NAME (decl),
5706132730Skan			   IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5707132730Skan			   current_scope->shadowed);
5708132730Skan	  IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5709132730Skan	}
5710132730Skan    }
5711132730Skan  current_scope->names = others;
5712132730Skan  current_scope->names_last = last;
571390075Sobrien
5714132730Skan  /* And all the tag declarations.  */
5715132730Skan  for (decl = tags; decl; decl = TREE_CHAIN (decl))
5716132730Skan    if (TREE_PURPOSE (decl))
5717132730Skan      {
5718132730Skan	if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5719132730Skan	  current_scope->shadowed_tags
5720132730Skan	    = tree_cons (TREE_PURPOSE (decl),
5721132730Skan			 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5722132730Skan			 current_scope->shadowed_tags);
5723132730Skan	IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5724132730Skan      }
5725132730Skan  current_scope->tags = tags;
5726132730Skan}
572718334Speter
5728132730Skan/* Subroutine of store_parm_decls which handles old-style function
5729132730Skan   definitions (separate parameter list and declarations).  */
573090075Sobrien
5731132730Skanstatic void
5732132730Skanstore_parm_decls_oldstyle (void)
5733132730Skan{
5734132730Skan  tree parm, decl, last;
5735132730Skan  tree fndecl = current_function_decl;
573690075Sobrien
5737132730Skan  /* This is the identifier list from the function declarator.  */
5738132730Skan  tree parmids = current_function_parms;
573918334Speter
5740132730Skan  /* We use DECL_WEAK as a flag to show which parameters have been
5741132730Skan     seen already, since it is not used on PARM_DECL.  */
5742132730Skan#ifdef ENABLE_CHECKING
5743132730Skan  for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5744132730Skan    if (DECL_WEAK (parm))
5745132730Skan      abort ();
5746132730Skan#endif
574718334Speter
5748132730Skan  /* Match each formal parameter name with its declaration.  Save each
5749132730Skan     decl in the appropriate TREE_PURPOSE slot of the parmids chain.  */
5750132730Skan  for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5751132730Skan    {
5752132730Skan      if (TREE_VALUE (parm) == 0)
575318334Speter	{
5754132730Skan	  error ("%Jparameter name missing from parameter list", fndecl);
5755132730Skan	  TREE_PURPOSE (parm) = 0;
5756132730Skan	  continue;
575718334Speter	}
575818334Speter
5759132730Skan      decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5760132730Skan      if (decl && DECL_CONTEXT (decl) == fndecl)
576118334Speter	{
5762132730Skan	  /* If we got something other than a PARM_DECL it is an error.  */
5763132730Skan	  if (TREE_CODE (decl) != PARM_DECL)
5764132730Skan	    error ("%J\"%D\" declared as a non-parameter", decl, decl);
5765132730Skan	  /* If the declaration is already marked, we have a duplicate
5766132730Skan	     name.  Complain and ignore the duplicate.  */
5767132730Skan	  else if (DECL_WEAK (decl))
576818334Speter	    {
5769132730Skan	      error ("%Jmultiple parameters named \"%D\"", decl, decl);
5770132730Skan	      TREE_PURPOSE (parm) = 0;
5771132730Skan	      continue;
577218334Speter	    }
5773132730Skan	  /* If the declaration says "void", complain and turn it into
5774132730Skan	     an int.  */
5775132730Skan	  else if (VOID_TYPE_P (TREE_TYPE (decl)))
577618334Speter	    {
5777132730Skan	      error ("%Jparameter \"%D\" declared void", decl, decl);
5778132730Skan	      TREE_TYPE (decl) = integer_type_node;
5779132730Skan	      DECL_ARG_TYPE (decl) = integer_type_node;
5780132730Skan	      layout_decl (decl, 0);
578118334Speter	    }
578218334Speter	}
5783132730Skan      /* If no declaration found, default to int.  */
5784132730Skan      else
578518334Speter	{
5786132730Skan	  decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5787132730Skan	  DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5788132730Skan	  DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5789132730Skan	  pushdecl (decl);
579018334Speter
5791132730Skan	  if (flag_isoc99)
5792132730Skan	    pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5793132730Skan	  else if (extra_warnings)
5794132730Skan	    warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
579518334Speter	}
579618334Speter
5797132730Skan      TREE_PURPOSE (parm) = decl;
5798132730Skan      DECL_WEAK (decl) = 1;
579918334Speter    }
580018334Speter
5801132730Skan  /* Now examine the parms chain for incomplete declarations
5802132730Skan     and declarations with no corresponding names.  */
580318334Speter
5804132730Skan  for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5805132730Skan    {
5806132730Skan      if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5807132730Skan	{
5808132730Skan	  error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5809132730Skan	  TREE_TYPE (parm) = error_mark_node;
5810132730Skan	}
581118334Speter
5812132730Skan      if (! DECL_WEAK (parm))
581318334Speter	{
5814132730Skan	  error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5815132730Skan		 parm, parm);
581618334Speter
5817132730Skan	  /* Pretend the parameter was not missing.
5818132730Skan	     This gets us to a standard state and minimizes
5819132730Skan	     further error messages.  */
5820132730Skan	  parmids = chainon (parmids, tree_cons (parm, 0, 0));
5821132730Skan	}
5822132730Skan    }
582318334Speter
5824132730Skan  /* Chain the declarations together in the order of the list of
5825132730Skan     names.  Store that chain in the function decl, replacing the
5826132730Skan     list of names.  Update the current scope to match.  */
5827132730Skan  DECL_ARGUMENTS (fndecl) = 0;
582818334Speter
5829132730Skan  for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5830132730Skan    if (TREE_PURPOSE (parm))
5831132730Skan      break;
5832132730Skan  if (parm && TREE_PURPOSE (parm))
5833132730Skan    {
5834132730Skan      last = TREE_PURPOSE (parm);
5835132730Skan      DECL_ARGUMENTS (fndecl) = last;
5836132730Skan      current_scope->parms = last;
5837132730Skan      DECL_WEAK (last) = 0;
583818334Speter
5839132730Skan      for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5840132730Skan	if (TREE_PURPOSE (parm))
5841132730Skan	  {
5842132730Skan	    TREE_CHAIN (last) = TREE_PURPOSE (parm);
5843132730Skan	    last = TREE_PURPOSE (parm);
5844132730Skan	    DECL_WEAK (last) = 0;
5845132730Skan	  }
5846132730Skan      current_scope->parms_last = last;
5847132730Skan      TREE_CHAIN (last) = 0;
5848132730Skan    }
584918334Speter
5850132730Skan  /* If there was a previous prototype,
5851132730Skan     set the DECL_ARG_TYPE of each argument according to
5852132730Skan     the type previously specified, and report any mismatches.  */
585318334Speter
5854132730Skan  if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5855132730Skan    {
5856132730Skan      tree type;
5857132730Skan      for (parm = DECL_ARGUMENTS (fndecl),
5858132730Skan	     type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5859132730Skan	   parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5860132730Skan			     != void_type_node));
5861132730Skan	   parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
586218334Speter	{
5863132730Skan	  if (parm == 0 || type == 0
5864132730Skan	      || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
586518334Speter	    {
5866132730Skan	      error ("number of arguments doesn't match prototype");
5867132730Skan	      error ("%Hprototype declaration",
5868132730Skan		     &current_function_prototype_locus);
5869132730Skan	      break;
587018334Speter	    }
5871132730Skan	  /* Type for passing arg must be consistent with that
5872132730Skan	     declared for the arg.  ISO C says we take the unqualified
5873132730Skan	     type for parameters declared with qualified type.  */
5874132730Skan	  if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5875132730Skan			   TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5876132730Skan			   COMPARE_STRICT))
587718334Speter	    {
5878132730Skan	      if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5879132730Skan		  == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
588018334Speter		{
5881132730Skan		  /* Adjust argument to match prototype.  E.g. a previous
5882132730Skan		     `int foo(float);' prototype causes
5883132730Skan		     `int foo(x) float x; {...}' to be treated like
5884132730Skan		     `int foo(float x) {...}'.  This is particularly
5885132730Skan		     useful for argument types like uid_t.  */
5886132730Skan		  DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
588790075Sobrien
5888132730Skan		  if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5889132730Skan		      && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5890132730Skan		      && TYPE_PRECISION (TREE_TYPE (parm))
5891132730Skan		      < TYPE_PRECISION (integer_type_node))
5892132730Skan		    DECL_ARG_TYPE (parm) = integer_type_node;
589390075Sobrien
5894132730Skan		  if (pedantic)
589518334Speter		    {
5896132730Skan		      pedwarn ("promoted argument \"%D\" "
5897132730Skan			       "doesn't match prototype", parm);
5898132730Skan		      pedwarn ("%Hprototype declaration",
5899132730Skan			       &current_function_prototype_locus);
590018334Speter		    }
590118334Speter		}
5902132730Skan	      else
5903132730Skan		{
5904132730Skan		  error ("argument \"%D\" doesn't match prototype", parm);
5905132730Skan		  error ("%Hprototype declaration",
5906132730Skan			 &current_function_prototype_locus);
5907132730Skan		}
590818334Speter	    }
590918334Speter	}
5910132730Skan      TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5911132730Skan    }
591218334Speter
5913132730Skan  /* Otherwise, create a prototype that would match.  */
591418334Speter
5915132730Skan  else
5916132730Skan    {
5917132730Skan      tree actual = 0, last = 0, type;
5918132730Skan
5919132730Skan      for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
592018334Speter	{
5921132730Skan	  type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
592218334Speter	  if (last)
592318334Speter	    TREE_CHAIN (last) = type;
592418334Speter	  else
592518334Speter	    actual = type;
5926132730Skan	  last = type;
5927132730Skan	}
5928132730Skan      type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5929132730Skan      if (last)
5930132730Skan	TREE_CHAIN (last) = type;
5931132730Skan      else
5932132730Skan	actual = type;
593318334Speter
5934132730Skan      /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5935132730Skan	 of the type of this function, but we need to avoid having this
5936132730Skan	 affect the types of other similarly-typed functions, so we must
5937132730Skan	 first force the generation of an identical (but separate) type
5938132730Skan	 node for the relevant function type.  The new node we create
5939132730Skan	 will be a variant of the main variant of the original function
5940132730Skan	 type.  */
594118334Speter
5942132730Skan      TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
594318334Speter
5944132730Skan      TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5945132730Skan    }
5946132730Skan}
594718334Speter
5948132730Skan/* Store the parameter declarations into the current function declaration.
5949132730Skan   This is called after parsing the parameter declarations, before
5950132730Skan   digesting the body of the function.
595118334Speter
5952132730Skan   For an old-style definition, construct a prototype out of the old-style
5953132730Skan   parameter declarations and inject it into the function's type.  */
595418334Speter
5955132730Skanvoid
5956132730Skanstore_parm_decls (void)
5957132730Skan{
5958132730Skan  tree fndecl = current_function_decl;
595918334Speter
5960132730Skan  /* The function containing FNDECL, if any.  */
5961132730Skan  tree context = decl_function_context (fndecl);
596218334Speter
5963132730Skan  /* True if this definition is written with a prototype.  */
5964132730Skan  bool prototype = (current_function_parms
5965132730Skan		    && TREE_CODE (current_function_parms) != TREE_LIST);
596618334Speter
5967132730Skan  if (prototype)
5968132730Skan    store_parm_decls_newstyle ();
5969132730Skan  else
5970132730Skan    store_parm_decls_oldstyle ();
5971132730Skan
5972132730Skan  /* The next call to pushlevel will be a function body.  */
5973132730Skan
5974132730Skan  next_is_function_body = true;
5975132730Skan
597618334Speter  /* Write a record describing this function definition to the prototypes
597718334Speter     file (if requested).  */
597818334Speter
597918334Speter  gen_aux_info_record (fndecl, 1, 0, prototype);
598018334Speter
598118334Speter  /* Initialize the RTL code for the function.  */
5982132730Skan  allocate_struct_function (fndecl);
598318334Speter
598490075Sobrien  /* Begin the statement tree for this function.  */
5985132730Skan  begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
598618334Speter
598790075Sobrien  /* If this is a nested function, save away the sizes of any
598890075Sobrien     variable-size types so that we can expand them when generating
598990075Sobrien     RTL.  */
599090075Sobrien  if (context)
599118334Speter    {
599290075Sobrien      tree t;
599318334Speter
5994132730Skan      DECL_LANG_SPECIFIC (fndecl)->pending_sizes
599590075Sobrien	= nreverse (get_pending_sizes ());
599690075Sobrien      for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
599790075Sobrien	   t;
599890075Sobrien	   t = TREE_CHAIN (t))
599990075Sobrien	SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
600018334Speter    }
600118334Speter
600290075Sobrien  /* This function is being processed in whole-function mode.  */
600390075Sobrien  cfun->x_whole_function_mode_p = 1;
600418334Speter
600590075Sobrien  /* Even though we're inside a function body, we still don't want to
600690075Sobrien     call expand_expr to calculate the size of a variable-sized array.
600790075Sobrien     We haven't necessarily assigned RTL to all variables yet, so it's
600890075Sobrien     not safe to try to expand expressions involving them.  */
600990075Sobrien  immediate_size_expand = 0;
601090075Sobrien  cfun->x_dont_save_pending_sizes_p = 1;
601118334Speter}
601218334Speter
601318334Speter/* Finish up a function declaration and compile that function
601418334Speter   all the way to assembler language output.  The free the storage
601518334Speter   for the function definition.
601618334Speter
6017132730Skan   This is called after parsing the body of the function definition.  */
601818334Speter
601918334Spetervoid
6020132730Skanfinish_function (void)
602118334Speter{
602290075Sobrien  tree fndecl = current_function_decl;
602318334Speter
6024132730Skan  /* When a function declaration is totally empty, e.g.
6025132730Skan        void foo(void) { }
6026132730Skan     (the argument list is irrelevant) the compstmt rule will not
6027132730Skan     bother calling pushlevel/poplevel, which means we get here with
6028132730Skan     the scope stack out of sync.  Detect this situation by noticing
6029132730Skan     that current_scope is still as store_parm_decls left it, and do
6030132730Skan     a dummy push/pop to get back to consistency.
6031132730Skan     Note that the call to pushlevel does not actually push another
6032132730Skan     scope - see there for details.  */
603318334Speter
6034132730Skan  if (current_scope->parm_flag && next_is_function_body)
6035132730Skan    {
6036132730Skan      pushlevel (0);
6037132730Skan      poplevel (0, 0, 0);
6038132730Skan    }
603918334Speter
6040132730Skan  if (TREE_CODE (fndecl) == FUNCTION_DECL
6041132730Skan      && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6042132730Skan    {
6043132730Skan      tree args = DECL_ARGUMENTS (fndecl);
6044132730Skan      for (; args; args = TREE_CHAIN (args))
6045132730Skan 	{
6046132730Skan 	  tree type = TREE_TYPE (args);
6047132730Skan 	  if (INTEGRAL_TYPE_P (type)
6048132730Skan 	      && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6049132730Skan 	    DECL_ARG_TYPE (args) = integer_type_node;
6050132730Skan 	}
6051132730Skan    }
6052132730Skan
6053132730Skan  if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6054132730Skan    BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6055132730Skan
605618334Speter  /* Must mark the RESULT_DECL as being in this function.  */
605718334Speter
6058132730Skan  if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6059132730Skan    DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
606018334Speter
606190075Sobrien  if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
606218334Speter    {
606318334Speter      if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
606418334Speter	  != integer_type_node)
606550397Sobrien	{
606652284Sobrien	  /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
606790075Sobrien	     If warn_main is -1 (-Wno-main) we don't want to be warned.  */
6068132730Skan	  if (!warn_main)
6069132730Skan	    pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
607050397Sobrien	}
607118334Speter      else
607218334Speter	{
607350397Sobrien#ifdef DEFAULT_MAIN_RETURN
607418334Speter	  /* Make it so that `main' always returns success by default.  */
607518334Speter	  DEFAULT_MAIN_RETURN;
607690075Sobrien#else
607790075Sobrien	  if (flag_isoc99)
607890075Sobrien	    c_expand_return (integer_zero_node);
607950397Sobrien#endif
608018334Speter	}
608118334Speter    }
6082132730Skan
608390075Sobrien  finish_fname_decls ();
608418334Speter
608590075Sobrien  /* Tie off the statement tree for this function.  */
608690075Sobrien  finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
608796263Sobrien
608896263Sobrien  /* Complain if there's just no return statement.  */
608996263Sobrien  if (warn_return_type
609096263Sobrien      && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
609196263Sobrien      && !current_function_returns_value && !current_function_returns_null
609296263Sobrien      /* Don't complain if we abort.  */
609396263Sobrien      && !current_function_returns_abnormally
609496263Sobrien      /* Don't warn for main().  */
609596263Sobrien      && !MAIN_NAME_P (DECL_NAME (fndecl))
609696263Sobrien      /* Or if they didn't actually specify a return type.  */
609796263Sobrien      && !C_FUNCTION_IMPLICIT_INT (fndecl)
609896263Sobrien      /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
609996263Sobrien	 inline function, as we might never be compiled separately.  */
610096263Sobrien      && DECL_INLINE (fndecl))
610196263Sobrien    warning ("no return statement in function returning non-void");
610296263Sobrien
6103132730Skan  /* With just -Wextra, complain only if function returns both with
6104122196Skan     and without a value.  */
6105122196Skan  if (extra_warnings
6106122196Skan      && current_function_returns_value
6107122196Skan      && current_function_returns_null)
6108122196Skan    warning ("this function may return with or without a value");
6109122196Skan
6110132730Skan  /* We're leaving the context of this function, so zap cfun.  It's still in
6111132730Skan     DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation.  */
611290075Sobrien  cfun = NULL;
611390075Sobrien
6114132730Skan  /* ??? Objc emits functions after finalizing the compilation unit.
6115132730Skan     This should be cleaned up later and this conditional removed.  */
6116132730Skan  if (!cgraph_global_info_ready)
6117132730Skan    cgraph_finalize_function (fndecl, false);
6118132730Skan  else
6119132730Skan    c_expand_body (fndecl);
6120132730Skan  current_function_decl = NULL;
612190075Sobrien}
612290075Sobrien
612390075Sobrien/* Generate the RTL for a deferred function FNDECL.  */
612490075Sobrien
612590075Sobrienvoid
6126132730Skanc_expand_deferred_function (tree fndecl)
612790075Sobrien{
612890075Sobrien  /* DECL_INLINE or DECL_RESULT might got cleared after the inline
612990075Sobrien     function was deferred, e.g. in duplicate_decls.  */
613090075Sobrien  if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
613190075Sobrien    {
6132132730Skan      if (flag_inline_trees)
6133132730Skan	{
6134132730Skan	  timevar_push (TV_INTEGRATION);
6135132730Skan	  optimize_inline_calls (fndecl);
6136132730Skan	  timevar_pop (TV_INTEGRATION);
6137132730Skan	}
6138132730Skan      c_expand_body (fndecl);
613990075Sobrien      current_function_decl = NULL;
614090075Sobrien    }
614190075Sobrien}
614290075Sobrien
6143117421Skan/* Generate the RTL for the body of FNDECL.  If NESTED_P is nonzero,
614490075Sobrien   then we are already in the process of generating RTL for another
6145132730Skan   function.  */
614690075Sobrien
614790075Sobrienstatic void
6148132730Skanc_expand_body_1 (tree fndecl, int nested_p)
614990075Sobrien{
615090075Sobrien  if (nested_p)
615190075Sobrien    {
615290075Sobrien      /* Make sure that we will evaluate variable-sized types involved
615390075Sobrien	 in our function's type.  */
615490075Sobrien      expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6155132730Skan
615690075Sobrien      /* Squirrel away our current state.  */
615790075Sobrien      push_function_context ();
615890075Sobrien    }
6159132730Skan
6160132730Skan  tree_rest_of_compilation (fndecl, nested_p);
616190075Sobrien
616290075Sobrien  if (nested_p)
6163132730Skan    /* Return to the enclosing function.  */
6164132730Skan    pop_function_context ();
616518334Speter
616618334Speter  if (DECL_STATIC_CONSTRUCTOR (fndecl))
616718334Speter    {
616890075Sobrien      if (targetm.have_ctors_dtors)
616990075Sobrien	(* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
617090075Sobrien				         DEFAULT_INIT_PRIORITY);
617118334Speter      else
617290075Sobrien	static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
617318334Speter    }
617490075Sobrien
617518334Speter  if (DECL_STATIC_DESTRUCTOR (fndecl))
617618334Speter    {
617790075Sobrien      if (targetm.have_ctors_dtors)
617890075Sobrien	(* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
617990075Sobrien				        DEFAULT_INIT_PRIORITY);
618018334Speter      else
618190075Sobrien	static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
618218334Speter    }
6183132730Skan}
618418334Speter
6185132730Skan/* Like c_expand_body_1 but only for unnested functions.  */
6186117421Skan
6187132730Skanvoid
6188132730Skanc_expand_body (tree fndecl)
6189132730Skan{
6190132730Skan
6191132730Skan  if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6192132730Skan    c_expand_body_1 (fndecl, 0);
619390075Sobrien}
619490075Sobrien
619590075Sobrien/* Check the declarations given in a for-loop for satisfying the C99
619690075Sobrien   constraints.  */
619790075Sobrienvoid
6198132730Skancheck_for_loop_decls (void)
619990075Sobrien{
620090075Sobrien  tree t;
620190075Sobrien
620290075Sobrien  if (!flag_isoc99)
620318334Speter    {
620490075Sobrien      /* If we get here, declarations have been used in a for loop without
620590075Sobrien	 the C99 for loop scope.  This doesn't make much sense, so don't
620690075Sobrien	 allow it.  */
6207132730Skan      error ("'for' loop initial declaration used outside C99 mode");
620890075Sobrien      return;
620918334Speter    }
621090075Sobrien  /* C99 subclause 6.8.5 paragraph 3:
621190075Sobrien
621290075Sobrien       [#3]  The  declaration  part  of  a for statement shall only
621390075Sobrien       declare identifiers for objects having storage class auto or
621490075Sobrien       register.
621590075Sobrien
621690075Sobrien     It isn't clear whether, in this sentence, "identifiers" binds to
621790075Sobrien     "shall only declare" or to "objects" - that is, whether all identifiers
621890075Sobrien     declared must be identifiers for objects, or whether the restriction
621990075Sobrien     only applies to those that are.  (A question on this in comp.std.c
622090075Sobrien     in November 2000 received no answer.)  We implement the strictest
622190075Sobrien     interpretation, to avoid creating an extension which later causes
622290075Sobrien     problems.  */
622390075Sobrien
6224132730Skan  for (t = current_scope->tags; t; t = TREE_CHAIN (t))
622590075Sobrien    {
622690075Sobrien      if (TREE_PURPOSE (t) != 0)
622790075Sobrien        {
622890075Sobrien          enum tree_code code = TREE_CODE (TREE_VALUE (t));
6229132730Skan
623090075Sobrien          if (code == RECORD_TYPE)
6231132730Skan            error ("'struct %s' declared in 'for' loop initial declaration",
623290075Sobrien                   IDENTIFIER_POINTER (TREE_PURPOSE (t)));
623390075Sobrien          else if (code == UNION_TYPE)
6234132730Skan            error ("'union %s' declared in 'for' loop initial declaration",
623590075Sobrien                   IDENTIFIER_POINTER (TREE_PURPOSE (t)));
623690075Sobrien          else
6237132730Skan            error ("'enum %s' declared in 'for' loop initial declaration",
623890075Sobrien                   IDENTIFIER_POINTER (TREE_PURPOSE (t)));
623990075Sobrien        }
624090075Sobrien    }
624190075Sobrien
624290075Sobrien  for (t = getdecls (); t; t = TREE_CHAIN (t))
624390075Sobrien    {
624490075Sobrien      if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6245132730Skan	error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6246132730Skan               "initial declaration", t, t);
624790075Sobrien      else if (TREE_STATIC (t))
6248132730Skan	error ("%Jdeclaration of static variable '%D' in 'for' loop "
6249132730Skan	       "initial declaration", t, t);
625090075Sobrien      else if (DECL_EXTERNAL (t))
6251132730Skan	error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6252132730Skan               "initial declaration", t, t);
625390075Sobrien    }
625418334Speter}
625518334Speter
625618334Speter/* Save and reinitialize the variables
625718334Speter   used during compilation of a C function.  */
625818334Speter
625918334Spetervoid
6260132730Skanc_push_function_context (struct function *f)
626118334Speter{
6262117421Skan  struct language_function *p;
6263132730Skan  p = ggc_alloc (sizeof (struct language_function));
6264117421Skan  f->language = p;
626518334Speter
626690075Sobrien  p->base.x_stmt_tree = c_stmt_tree;
626790075Sobrien  p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6268132730Skan  p->x_in_iteration_stmt = c_in_iteration_stmt;
6269132730Skan  p->x_in_case_stmt = c_in_case_stmt;
627018334Speter  p->returns_value = current_function_returns_value;
627118334Speter  p->returns_null = current_function_returns_null;
627296263Sobrien  p->returns_abnormally = current_function_returns_abnormally;
627318334Speter  p->warn_about_return_type = warn_about_return_type;
627418334Speter  p->extern_inline = current_extern_inline;
627518334Speter}
627618334Speter
627718334Speter/* Restore the variables used during compilation of a C function.  */
627818334Speter
627918334Spetervoid
6280132730Skanc_pop_function_context (struct function *f)
628118334Speter{
6282117421Skan  struct language_function *p = f->language;
628318334Speter
628490075Sobrien  if (DECL_SAVED_INSNS (current_function_decl) == 0
628590075Sobrien      && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
628618334Speter    {
628718334Speter      /* Stop pointing to the local nodes about to be freed.  */
628818334Speter      /* But DECL_INITIAL must remain nonzero so we know this
628918334Speter	 was an actual function definition.  */
629018334Speter      DECL_INITIAL (current_function_decl) = error_mark_node;
629118334Speter      DECL_ARGUMENTS (current_function_decl) = 0;
629218334Speter    }
629318334Speter
629490075Sobrien  c_stmt_tree = p->base.x_stmt_tree;
629590075Sobrien  c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6296132730Skan  c_in_iteration_stmt = p->x_in_iteration_stmt;
6297132730Skan  c_in_case_stmt = p->x_in_case_stmt;
629818334Speter  current_function_returns_value = p->returns_value;
629918334Speter  current_function_returns_null = p->returns_null;
630096263Sobrien  current_function_returns_abnormally = p->returns_abnormally;
630118334Speter  warn_about_return_type = p->warn_about_return_type;
630218334Speter  current_extern_inline = p->extern_inline;
630318334Speter
6304117421Skan  f->language = NULL;
630518334Speter}
630618334Speter
6307117421Skan/* Copy the DECL_LANG_SPECIFIC data associated with DECL.  */
630818334Speter
630918334Spetervoid
6310132730Skanc_dup_lang_specific_decl (tree decl)
631190075Sobrien{
631290075Sobrien  struct lang_decl *ld;
631390075Sobrien
631490075Sobrien  if (!DECL_LANG_SPECIFIC (decl))
631590075Sobrien    return;
631690075Sobrien
6317132730Skan  ld = ggc_alloc (sizeof (struct lang_decl));
6318132730Skan  memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
631990075Sobrien  DECL_LANG_SPECIFIC (decl) = ld;
632090075Sobrien}
632190075Sobrien
632290075Sobrien/* The functions below are required for functionality of doing
632390075Sobrien   function at once processing in the C front end. Currently these
632490075Sobrien   functions are not called from anywhere in the C front end, but as
632590075Sobrien   these changes continue, that will change.  */
632690075Sobrien
6327117421Skan/* Returns nonzero if the current statement is a full expression,
632890075Sobrien   i.e. temporaries created during that statement should be destroyed
632990075Sobrien   at the end of the statement.  */
633090075Sobrien
633190075Sobrienint
6332132730Skanstmts_are_full_exprs_p (void)
633390075Sobrien{
633490075Sobrien  return 0;
633590075Sobrien}
633690075Sobrien
633790075Sobrien/* Returns the stmt_tree (if any) to which statements are currently
633890075Sobrien   being added.  If there is no active statement-tree, NULL is
633990075Sobrien   returned.  */
634090075Sobrien
634190075Sobrienstmt_tree
6342132730Skancurrent_stmt_tree (void)
634390075Sobrien{
634490075Sobrien  return &c_stmt_tree;
634590075Sobrien}
634690075Sobrien
634790075Sobrien/* Returns the stack of SCOPE_STMTs for the current function.  */
634890075Sobrien
634990075Sobrientree *
6350132730Skancurrent_scope_stmt_stack (void)
635190075Sobrien{
635290075Sobrien  return &c_scope_stmt_stack;
635390075Sobrien}
635490075Sobrien
635590075Sobrien/* Nonzero if TYPE is an anonymous union or struct type.  Always 0 in
635690075Sobrien   C.  */
635790075Sobrien
635890075Sobrienint
6359132730Skananon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
636018334Speter{
636190075Sobrien  return 0;
636218334Speter}
636390075Sobrien
636490075Sobrien/* Dummy function in place of callback used by C++.  */
636590075Sobrien
636690075Sobrienvoid
6367132730Skanextract_interface_info (void)
636890075Sobrien{
636990075Sobrien}
637090075Sobrien
637190075Sobrien/* Return a new COMPOUND_STMT, after adding it to the current
637290075Sobrien   statement tree.  */
637390075Sobrien
637490075Sobrientree
6375132730Skanc_begin_compound_stmt (void)
637690075Sobrien{
637790075Sobrien  tree stmt;
637890075Sobrien
637990075Sobrien  /* Create the COMPOUND_STMT.  */
638090075Sobrien  stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
638190075Sobrien
638290075Sobrien  return stmt;
638390075Sobrien}
638490075Sobrien
638590075Sobrien/* Expand T (a DECL_STMT) if it declares an entity not handled by the
638690075Sobrien   common code.  */
638790075Sobrien
638890075Sobrienvoid
6389132730Skanc_expand_decl_stmt (tree t)
639090075Sobrien{
639190075Sobrien  tree decl = DECL_STMT_DECL (t);
639290075Sobrien
639390075Sobrien  /* Expand nested functions.  */
639490075Sobrien  if (TREE_CODE (decl) == FUNCTION_DECL
639590075Sobrien      && DECL_CONTEXT (decl) == current_function_decl
639690075Sobrien      && DECL_SAVED_TREE (decl))
6397132730Skan    c_expand_body_1 (decl, 1);
639890075Sobrien}
639990075Sobrien
6400132730Skan/* Return the global value of T as a symbol.  */
640190075Sobrien
640290075Sobrientree
6403132730Skanidentifier_global_value	(tree t)
640490075Sobrien{
6405132730Skan  tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6406132730Skan  if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6407132730Skan    return decl;
6408132730Skan
6409132730Skan  /* Shadowed by something else; find the true global value.  */
6410132730Skan  for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6411132730Skan    if (DECL_NAME (decl) == t)
6412132730Skan      return decl;
6413132730Skan
6414132730Skan  /* Only local values for this decl.  */
6415132730Skan  return 0;
641690075Sobrien}
641790075Sobrien
641890075Sobrien/* Record a builtin type for C.  If NAME is non-NULL, it is the name used;
641990075Sobrien   otherwise the name is found in ridpointers from RID_INDEX.  */
642090075Sobrien
642190075Sobrienvoid
6422132730Skanrecord_builtin_type (enum rid rid_index, const char *name, tree type)
642390075Sobrien{
642490075Sobrien  tree id;
642590075Sobrien  if (name == 0)
642690075Sobrien    id = ridpointers[(int) rid_index];
642790075Sobrien  else
642890075Sobrien    id = get_identifier (name);
642990075Sobrien  pushdecl (build_decl (TYPE_DECL, id, type));
643090075Sobrien}
643190075Sobrien
643290075Sobrien/* Build the void_list_node (void_type_node having been created).  */
643390075Sobrientree
6434132730Skanbuild_void_list_node (void)
643590075Sobrien{
643690075Sobrien  tree t = build_tree_list (NULL_TREE, void_type_node);
643790075Sobrien  return t;
643890075Sobrien}
6439117421Skan
6440117421Skan/* Return something to represent absolute declarators containing a *.
6441117421Skan   TARGET is the absolute declarator that the * contains.
6442117421Skan   TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6443117421Skan   to apply to the pointer type, represented as identifiers, possible mixed
6444117421Skan   with attributes.
6445117421Skan
6446117421Skan   We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6447117421Skan   if attributes are present) and whose type is the modifier list.  */
6448117421Skan
6449117421Skantree
6450132730Skanmake_pointer_declarator (tree type_quals_attrs, tree target)
6451117421Skan{
6452117421Skan  tree quals, attrs;
6453117421Skan  tree itarget = target;
6454117421Skan  split_specs_attrs (type_quals_attrs, &quals, &attrs);
6455117421Skan  if (attrs != NULL_TREE)
6456117421Skan    itarget = tree_cons (attrs, target, NULL_TREE);
6457117421Skan  return build1 (INDIRECT_REF, quals, itarget);
6458117421Skan}
6459117421Skan
6460132730Skan/* A wrapper around lhd_set_decl_assembler_name that gives static
6461132730Skan   variables their C names if they are at file scope and only one
6462132730Skan   translation unit is being compiled, for backwards compatibility
6463132730Skan   with certain bizarre assembler hacks (like crtstuff.c).  */
6464132730Skan
6465132730Skanvoid
6466132730Skanc_static_assembler_name (tree decl)
6467132730Skan{
6468132730Skan  if (num_in_fnames == 1
6469132730Skan      && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6470132730Skan      && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6471132730Skan    SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6472132730Skan  else
6473132730Skan    lhd_set_decl_assembler_name (decl);
6474132730Skan}
6475132730Skan
6476132730Skan/* Hash and equality functions for link_hash_table: key off
6477132730Skan   DECL_ASSEMBLER_NAME.  */
6478132730Skan
6479132730Skanstatic hashval_t
6480132730Skanlink_hash_hash (const void *x_p)
6481132730Skan{
6482132730Skan  tree x = (tree)x_p;
6483132730Skan  return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6484132730Skan}
6485132730Skan
6486132730Skanstatic int
6487132730Skanlink_hash_eq (const void *x1_p, const void *x2_p)
6488132730Skan{
6489132730Skan  tree x1 = (tree)x1_p;
6490132730Skan  tree x2 = (tree)x2_p;
6491132730Skan  return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6492132730Skan}
6493132730Skan
6494132730Skan/* Propagate information between definitions and uses between multiple
6495132730Skan   translation units in TU_LIST based on linkage rules.  */
6496132730Skan
6497132730Skanvoid
6498132730Skanmerge_translation_unit_decls (void)
6499132730Skan{
6500132730Skan  const tree tu_list = current_file_decl;
6501132730Skan  tree tu;
6502132730Skan  tree decl;
6503132730Skan  htab_t link_hash_table;
6504132730Skan  tree block;
6505132730Skan
6506132730Skan  /* Create the BLOCK that poplevel would have created, but don't
6507132730Skan     actually call poplevel since that's expensive.  */
6508132730Skan  block = make_node (BLOCK);
6509132730Skan  BLOCK_VARS (block) = current_scope->names;
6510132730Skan  TREE_USED (block) = 1;
6511132730Skan  DECL_INITIAL (current_file_decl) = block;
6512132730Skan
6513132730Skan  /* If only one translation unit seen, no copying necessary.  */
6514132730Skan  if (TREE_CHAIN (tu_list) == NULL_TREE)
6515132730Skan    return;
6516132730Skan
6517132730Skan  link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6518132730Skan
6519132730Skan  /* Enter any actual definitions into the hash table.  */
6520132730Skan  for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6521132730Skan    for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6522132730Skan      if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6523132730Skan	{
6524132730Skan	  PTR *slot;
6525132730Skan	  slot = htab_find_slot (link_hash_table, decl, INSERT);
6526132730Skan
6527132730Skan	  /* If we've already got a definition, work out which one is
6528132730Skan	     the real one, put it into the hash table, and make the
6529132730Skan	     other one DECL_EXTERNAL.  This is important to avoid
6530132730Skan	     putting out two definitions of the same symbol in the
6531132730Skan	     assembly output.  */
6532132730Skan	  if (*slot != NULL)
6533132730Skan	    {
6534132730Skan	      tree old_decl = (tree) *slot;
6535132730Skan
6536132730Skan	      /* If this is weak or common or whatever, suppress it
6537132730Skan		 in favor of the other definition.  */
6538132730Skan	      if (DECL_WEAK (decl))
6539132730Skan		DECL_EXTERNAL (decl) = 1;
6540132730Skan	      else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6541132730Skan		DECL_EXTERNAL (old_decl) = 1;
6542132730Skan	      else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6543132730Skan		DECL_EXTERNAL (decl) = 1;
6544132730Skan	      else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6545132730Skan		DECL_EXTERNAL (old_decl) = 1;
6546132730Skan
6547132730Skan	      if (DECL_EXTERNAL (decl))
6548132730Skan		{
6549132730Skan		  DECL_INITIAL (decl) = NULL_TREE;
6550132730Skan		  DECL_COMMON (decl) = 0;
6551132730Skan		  DECL_ONE_ONLY (decl) = 0;
6552132730Skan		  DECL_WEAK (decl) = 0;
6553132730Skan		}
6554132730Skan	      else if (DECL_EXTERNAL (old_decl))
6555132730Skan		{
6556132730Skan		  DECL_INITIAL (old_decl) = NULL_TREE;
6557132730Skan		  DECL_COMMON (old_decl) = 0;
6558132730Skan		  DECL_ONE_ONLY (old_decl) = 0;
6559132730Skan		  DECL_WEAK (old_decl) = 0;
6560132730Skan		  *slot = decl;
6561132730Skan		}
6562132730Skan	      else
6563132730Skan		{
6564132730Skan		  error ("%Jredefinition of global '%D'", decl, decl);
6565132730Skan		  error ("%J'%D' previously defined here", old_decl, old_decl);
6566132730Skan		}
6567132730Skan	    }
6568132730Skan	  else
6569132730Skan	    *slot = decl;
6570132730Skan	}
6571132730Skan
6572132730Skan  /* Now insert the desired information from all the definitions
6573132730Skan     into any plain declarations.  */
6574132730Skan  for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6575132730Skan    for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6576132730Skan      if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6577132730Skan	{
6578132730Skan	  tree global_decl;
6579132730Skan	  global_decl = htab_find (link_hash_table, decl);
6580132730Skan
6581132730Skan	  if (! global_decl)
6582132730Skan	    continue;
6583132730Skan
6584132730Skan	  /* Print any appropriate error messages, and partially merge
6585132730Skan	     the decls.  */
6586132730Skan	  (void) duplicate_decls (decl, global_decl);
6587132730Skan	}
6588132730Skan
6589132730Skan  htab_delete (link_hash_table);
6590132730Skan}
6591132730Skan
6592132730Skan/* Perform final processing on file-scope data.  */
6593132730Skan
6594132730Skanvoid
6595132730Skanc_write_global_declarations(void)
6596132730Skan{
6597132730Skan  tree link;
6598132730Skan
6599132730Skan  for (link = current_file_decl; link; link = TREE_CHAIN (link))
6600132730Skan    {
6601132730Skan      tree globals = BLOCK_VARS (DECL_INITIAL (link));
6602132730Skan      int len = list_length (globals);
6603132730Skan      tree *vec = xmalloc (sizeof (tree) * len);
6604132730Skan      int i;
6605132730Skan      tree decl;
6606132730Skan
6607132730Skan      /* Process the decls in the order they were written.  */
6608132730Skan
6609132730Skan      for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6610132730Skan	vec[i] = decl;
6611132730Skan
6612132730Skan      wrapup_global_declarations (vec, len);
6613132730Skan
6614132730Skan      check_global_declarations (vec, len);
6615132730Skan
6616132730Skan      /* Clean up.  */
6617132730Skan      free (vec);
6618132730Skan    }
6619132730Skan}
6620132730Skan
6621132730Skan/* Reset the parser's state in preparation for a new file.  */
6622132730Skan
6623132730Skanvoid
6624132730Skanc_reset_state (void)
6625132730Skan{
6626132730Skan  tree link;
6627132730Skan  tree file_scope_decl;
6628132730Skan
6629132730Skan  /* Pop the global scope.  */
6630132730Skan  if (current_scope != global_scope)
6631132730Skan      current_scope = global_scope;
6632132730Skan  file_scope_decl = current_file_decl;
6633132730Skan  DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6634132730Skan  BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6635132730Skan  truly_local_externals = NULL_TREE;
6636132730Skan
6637132730Skan  /* Start a new global binding level.  */
6638132730Skan  pushlevel (0);
6639132730Skan  global_scope = current_scope;
6640132730Skan  current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6641132730Skan  TREE_CHAIN (current_file_decl) = file_scope_decl;
6642132730Skan
6643132730Skan  /* Reintroduce the builtin declarations.  */
6644132730Skan  for (link = first_builtin_decl;
6645132730Skan       link != TREE_CHAIN (last_builtin_decl);
6646132730Skan       link = TREE_CHAIN (link))
6647132730Skan    pushdecl (copy_node (link));
6648132730Skan}
6649132730Skan
6650117421Skan#include "gt-c-decl.h"
6651