1e4b17023SJohn Marino /* Process declarations and variables for C compiler.
2e4b17023SJohn Marino Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3e4b17023SJohn Marino 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4e4b17023SJohn Marino Free Software Foundation, Inc.
5e4b17023SJohn Marino
6e4b17023SJohn Marino This file is part of GCC.
7e4b17023SJohn Marino
8e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11e4b17023SJohn Marino version.
12e4b17023SJohn Marino
13e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16e4b17023SJohn Marino for more details.
17e4b17023SJohn Marino
18e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
20e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
21e4b17023SJohn Marino
22e4b17023SJohn Marino /* Process declarations and symbol lookup for C front end.
23e4b17023SJohn Marino Also constructs types; the standard scalar types at initialization,
24e4b17023SJohn Marino and structure, union, array and enum types when they are declared. */
25e4b17023SJohn Marino
26e4b17023SJohn Marino /* ??? not all decl nodes are given the most useful possible
27e4b17023SJohn Marino line numbers. For example, the CONST_DECLs for enum values. */
28e4b17023SJohn Marino
29e4b17023SJohn Marino #include "config.h"
30e4b17023SJohn Marino #include "system.h"
31e4b17023SJohn Marino #include "coretypes.h"
32e4b17023SJohn Marino #include "input.h"
33e4b17023SJohn Marino #include "tm.h"
34e4b17023SJohn Marino #include "intl.h"
35e4b17023SJohn Marino #include "tree.h"
36e4b17023SJohn Marino #include "tree-inline.h"
37e4b17023SJohn Marino #include "flags.h"
38e4b17023SJohn Marino #include "function.h"
39e4b17023SJohn Marino #include "output.h"
40e4b17023SJohn Marino #include "c-tree.h"
41e4b17023SJohn Marino #include "toplev.h"
42e4b17023SJohn Marino #include "tm_p.h"
43e4b17023SJohn Marino #include "cpplib.h"
44e4b17023SJohn Marino #include "target.h"
45e4b17023SJohn Marino #include "debug.h"
46e4b17023SJohn Marino #include "opts.h"
47e4b17023SJohn Marino #include "timevar.h"
48e4b17023SJohn Marino #include "c-family/c-common.h"
49e4b17023SJohn Marino #include "c-family/c-objc.h"
50e4b17023SJohn Marino #include "c-family/c-pragma.h"
51e4b17023SJohn Marino #include "c-lang.h"
52e4b17023SJohn Marino #include "langhooks.h"
53e4b17023SJohn Marino #include "tree-iterator.h"
54e4b17023SJohn Marino #include "diagnostic-core.h"
55e4b17023SJohn Marino #include "tree-dump.h"
56e4b17023SJohn Marino #include "cgraph.h"
57e4b17023SJohn Marino #include "hashtab.h"
58e4b17023SJohn Marino #include "langhooks-def.h"
59e4b17023SJohn Marino #include "pointer-set.h"
60e4b17023SJohn Marino #include "plugin.h"
61e4b17023SJohn Marino #include "c-family/c-ada-spec.h"
62e4b17023SJohn Marino
63e4b17023SJohn Marino /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64e4b17023SJohn Marino enum decl_context
65e4b17023SJohn Marino { NORMAL, /* Ordinary declaration */
66e4b17023SJohn Marino FUNCDEF, /* Function definition */
67e4b17023SJohn Marino PARM, /* Declaration of parm before function body */
68e4b17023SJohn Marino FIELD, /* Declaration inside struct or union */
69e4b17023SJohn Marino TYPENAME}; /* Typename (inside cast or sizeof) */
70e4b17023SJohn Marino
71e4b17023SJohn Marino /* States indicating how grokdeclarator() should handle declspecs marked
72e4b17023SJohn Marino with __attribute__((deprecated)). An object declared as
73e4b17023SJohn Marino __attribute__((deprecated)) suppresses warnings of uses of other
74e4b17023SJohn Marino deprecated items. */
75e4b17023SJohn Marino
76e4b17023SJohn Marino enum deprecated_states {
77e4b17023SJohn Marino DEPRECATED_NORMAL,
78e4b17023SJohn Marino DEPRECATED_SUPPRESS
79e4b17023SJohn Marino };
80e4b17023SJohn Marino
81e4b17023SJohn Marino
82e4b17023SJohn Marino /* Nonzero if we have seen an invalid cross reference
83e4b17023SJohn Marino to a struct, union, or enum, but not yet printed the message. */
84e4b17023SJohn Marino tree pending_invalid_xref;
85e4b17023SJohn Marino
86e4b17023SJohn Marino /* File and line to appear in the eventual error message. */
87e4b17023SJohn Marino location_t pending_invalid_xref_location;
88e4b17023SJohn Marino
89e4b17023SJohn Marino /* The file and line that the prototype came from if this is an
90e4b17023SJohn Marino old-style definition; used for diagnostics in
91e4b17023SJohn Marino store_parm_decls_oldstyle. */
92e4b17023SJohn Marino
93e4b17023SJohn Marino static location_t current_function_prototype_locus;
94e4b17023SJohn Marino
95e4b17023SJohn Marino /* Whether this prototype was built-in. */
96e4b17023SJohn Marino
97e4b17023SJohn Marino static bool current_function_prototype_built_in;
98e4b17023SJohn Marino
99e4b17023SJohn Marino /* The argument type information of this prototype. */
100e4b17023SJohn Marino
101e4b17023SJohn Marino static tree current_function_prototype_arg_types;
102e4b17023SJohn Marino
103e4b17023SJohn Marino /* The argument information structure for the function currently being
104e4b17023SJohn Marino defined. */
105e4b17023SJohn Marino
106e4b17023SJohn Marino static struct c_arg_info *current_function_arg_info;
107e4b17023SJohn Marino
108e4b17023SJohn Marino /* The obstack on which parser and related data structures, which are
109e4b17023SJohn Marino not live beyond their top-level declaration or definition, are
110e4b17023SJohn Marino allocated. */
111e4b17023SJohn Marino struct obstack parser_obstack;
112e4b17023SJohn Marino
113e4b17023SJohn Marino /* The current statement tree. */
114e4b17023SJohn Marino
115e4b17023SJohn Marino static GTY(()) struct stmt_tree_s c_stmt_tree;
116e4b17023SJohn Marino
117e4b17023SJohn Marino /* State saving variables. */
118e4b17023SJohn Marino tree c_break_label;
119e4b17023SJohn Marino tree c_cont_label;
120e4b17023SJohn Marino
121e4b17023SJohn Marino /* A list of decls to be made automatically visible in each file scope. */
122e4b17023SJohn Marino static GTY(()) tree visible_builtins;
123e4b17023SJohn Marino
124e4b17023SJohn Marino /* Set to 0 at beginning of a function definition, set to 1 if
125e4b17023SJohn Marino a return statement that specifies a return value is seen. */
126e4b17023SJohn Marino
127e4b17023SJohn Marino int current_function_returns_value;
128e4b17023SJohn Marino
129e4b17023SJohn Marino /* Set to 0 at beginning of a function definition, set to 1 if
130e4b17023SJohn Marino a return statement with no argument is seen. */
131e4b17023SJohn Marino
132e4b17023SJohn Marino int current_function_returns_null;
133e4b17023SJohn Marino
134e4b17023SJohn Marino /* Set to 0 at beginning of a function definition, set to 1 if
135e4b17023SJohn Marino a call to a noreturn function is seen. */
136e4b17023SJohn Marino
137e4b17023SJohn Marino int current_function_returns_abnormally;
138e4b17023SJohn Marino
139e4b17023SJohn Marino /* Set to nonzero by `grokdeclarator' for a function
140e4b17023SJohn Marino whose return type is defaulted, if warnings for this are desired. */
141e4b17023SJohn Marino
142e4b17023SJohn Marino static int warn_about_return_type;
143e4b17023SJohn Marino
144e4b17023SJohn Marino /* Nonzero when the current toplevel function contains a declaration
145e4b17023SJohn Marino of a nested function which is never defined. */
146e4b17023SJohn Marino
147e4b17023SJohn Marino static bool undef_nested_function;
148e4b17023SJohn Marino
149e4b17023SJohn Marino
150e4b17023SJohn Marino /* Each c_binding structure describes one binding of an identifier to
151e4b17023SJohn Marino a decl. All the decls in a scope - irrespective of namespace - are
152e4b17023SJohn Marino chained together by the ->prev field, which (as the name implies)
153e4b17023SJohn Marino runs in reverse order. All the decls in a given namespace bound to
154e4b17023SJohn Marino a given identifier are chained by the ->shadowed field, which runs
155e4b17023SJohn Marino from inner to outer scopes.
156e4b17023SJohn Marino
157e4b17023SJohn Marino The ->decl field usually points to a DECL node, but there are two
158e4b17023SJohn Marino exceptions. In the namespace of type tags, the bound entity is a
159e4b17023SJohn Marino RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
160e4b17023SJohn Marino identifier is encountered, it is bound to error_mark_node to
161e4b17023SJohn Marino suppress further errors about that identifier in the current
162e4b17023SJohn Marino function.
163e4b17023SJohn Marino
164e4b17023SJohn Marino The ->u.type field stores the type of the declaration in this scope;
165e4b17023SJohn Marino if NULL, the type is the type of the ->decl field. This is only of
166e4b17023SJohn Marino relevance for objects with external or internal linkage which may
167e4b17023SJohn Marino be redeclared in inner scopes, forming composite types that only
168e4b17023SJohn Marino persist for the duration of those scopes. In the external scope,
169e4b17023SJohn Marino this stores the composite of all the types declared for this
170e4b17023SJohn Marino object, visible or not. The ->inner_comp field (used only at file
171e4b17023SJohn Marino scope) stores whether an incomplete array type at file scope was
172e4b17023SJohn Marino completed at an inner scope to an array size other than 1.
173e4b17023SJohn Marino
174e4b17023SJohn Marino The ->u.label field is used for labels. It points to a structure
175e4b17023SJohn Marino which stores additional information used for warnings.
176e4b17023SJohn Marino
177e4b17023SJohn Marino The depth field is copied from the scope structure that holds this
178e4b17023SJohn Marino decl. It is used to preserve the proper ordering of the ->shadowed
179e4b17023SJohn Marino field (see bind()) and also for a handful of special-case checks.
180e4b17023SJohn Marino Finally, the invisible bit is true for a decl which should be
181e4b17023SJohn Marino ignored for purposes of normal name lookup, and the nested bit is
182e4b17023SJohn Marino true for a decl that's been bound a second time in an inner scope;
183e4b17023SJohn Marino in all such cases, the binding in the outer scope will have its
184e4b17023SJohn Marino invisible bit true. */
185e4b17023SJohn Marino
186e4b17023SJohn Marino struct GTY((chain_next ("%h.prev"))) c_binding {
187e4b17023SJohn Marino union GTY(()) { /* first so GTY desc can use decl */
188e4b17023SJohn Marino tree GTY((tag ("0"))) type; /* the type in this scope */
189e4b17023SJohn Marino struct c_label_vars * GTY((tag ("1"))) label; /* for warnings */
190e4b17023SJohn Marino } GTY((desc ("TREE_CODE (%0.decl) == LABEL_DECL"))) u;
191e4b17023SJohn Marino tree decl; /* the decl bound */
192e4b17023SJohn Marino tree id; /* the identifier it's bound to */
193e4b17023SJohn Marino struct c_binding *prev; /* the previous decl in this scope */
194e4b17023SJohn Marino struct c_binding *shadowed; /* the innermost decl shadowed by this one */
195e4b17023SJohn Marino unsigned int depth : 28; /* depth of this scope */
196e4b17023SJohn Marino BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
197e4b17023SJohn Marino BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
198e4b17023SJohn Marino BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
199e4b17023SJohn Marino BOOL_BITFIELD in_struct : 1; /* currently defined as struct field */
200e4b17023SJohn Marino location_t locus; /* location for nested bindings */
201e4b17023SJohn Marino };
202e4b17023SJohn Marino #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
203e4b17023SJohn Marino #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
204e4b17023SJohn Marino #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
205e4b17023SJohn Marino #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
206e4b17023SJohn Marino
207e4b17023SJohn Marino #define I_SYMBOL_BINDING(node) \
208e4b17023SJohn Marino (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
209e4b17023SJohn Marino #define I_SYMBOL_DECL(node) \
210e4b17023SJohn Marino (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
211e4b17023SJohn Marino
212e4b17023SJohn Marino #define I_TAG_BINDING(node) \
213e4b17023SJohn Marino (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
214e4b17023SJohn Marino #define I_TAG_DECL(node) \
215e4b17023SJohn Marino (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
216e4b17023SJohn Marino
217e4b17023SJohn Marino #define I_LABEL_BINDING(node) \
218e4b17023SJohn Marino (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
219e4b17023SJohn Marino #define I_LABEL_DECL(node) \
220e4b17023SJohn Marino (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
221e4b17023SJohn Marino
222e4b17023SJohn Marino /* Each C symbol points to three linked lists of c_binding structures.
223e4b17023SJohn Marino These describe the values of the identifier in the three different
224e4b17023SJohn Marino namespaces defined by the language. */
225e4b17023SJohn Marino
226e4b17023SJohn Marino struct GTY(()) lang_identifier {
227e4b17023SJohn Marino struct c_common_identifier common_id;
228e4b17023SJohn Marino struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
229e4b17023SJohn Marino struct c_binding *tag_binding; /* struct/union/enum tags */
230e4b17023SJohn Marino struct c_binding *label_binding; /* labels */
231e4b17023SJohn Marino };
232e4b17023SJohn Marino
233e4b17023SJohn Marino /* Validate c-lang.c's assumptions. */
234e4b17023SJohn Marino extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
235e4b17023SJohn Marino [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
236e4b17023SJohn Marino
237e4b17023SJohn Marino /* The resulting tree type. */
238e4b17023SJohn Marino
239e4b17023SJohn Marino union GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
240e4b17023SJohn Marino chain_next ("(union lang_tree_node *) c_tree_chain_next (&%h.generic)"))) lang_tree_node
241e4b17023SJohn Marino {
242e4b17023SJohn Marino union tree_node GTY ((tag ("0"),
243e4b17023SJohn Marino desc ("tree_node_structure (&%h)")))
244e4b17023SJohn Marino generic;
245e4b17023SJohn Marino struct lang_identifier GTY ((tag ("1"))) identifier;
246e4b17023SJohn Marino };
247e4b17023SJohn Marino
248e4b17023SJohn Marino /* Track bindings and other things that matter for goto warnings. For
249e4b17023SJohn Marino efficiency, we do not gather all the decls at the point of
250e4b17023SJohn Marino definition. Instead, we point into the bindings structure. As
251e4b17023SJohn Marino scopes are popped, we update these structures and gather the decls
252e4b17023SJohn Marino that matter at that time. */
253e4b17023SJohn Marino
254e4b17023SJohn Marino struct GTY(()) c_spot_bindings {
255e4b17023SJohn Marino /* The currently open scope which holds bindings defined when the
256e4b17023SJohn Marino label was defined or the goto statement was found. */
257e4b17023SJohn Marino struct c_scope *scope;
258e4b17023SJohn Marino /* The bindings in the scope field which were defined at the point
259e4b17023SJohn Marino of the label or goto. This lets us look at older or newer
260e4b17023SJohn Marino bindings in the scope, as appropriate. */
261e4b17023SJohn Marino struct c_binding *bindings_in_scope;
262e4b17023SJohn Marino /* The number of statement expressions that have started since this
263e4b17023SJohn Marino label or goto statement was defined. This is zero if we are at
264e4b17023SJohn Marino the same statement expression level. It is positive if we are in
265e4b17023SJohn Marino a statement expression started since this spot. It is negative
266e4b17023SJohn Marino if this spot was in a statement expression and we have left
267e4b17023SJohn Marino it. */
268e4b17023SJohn Marino int stmt_exprs;
269e4b17023SJohn Marino /* Whether we started in a statement expression but are no longer in
270e4b17023SJohn Marino it. This is set to true if stmt_exprs ever goes negative. */
271e4b17023SJohn Marino bool left_stmt_expr;
272e4b17023SJohn Marino };
273e4b17023SJohn Marino
274e4b17023SJohn Marino /* This structure is used to keep track of bindings seen when a goto
275e4b17023SJohn Marino statement is defined. This is only used if we see the goto
276e4b17023SJohn Marino statement before we see the label. */
277e4b17023SJohn Marino
278e4b17023SJohn Marino struct GTY(()) c_goto_bindings {
279e4b17023SJohn Marino /* The location of the goto statement. */
280e4b17023SJohn Marino location_t loc;
281e4b17023SJohn Marino /* The bindings of the goto statement. */
282e4b17023SJohn Marino struct c_spot_bindings goto_bindings;
283e4b17023SJohn Marino };
284e4b17023SJohn Marino
285e4b17023SJohn Marino typedef struct c_goto_bindings *c_goto_bindings_p;
286e4b17023SJohn Marino DEF_VEC_P(c_goto_bindings_p);
287e4b17023SJohn Marino DEF_VEC_ALLOC_P(c_goto_bindings_p,gc);
288e4b17023SJohn Marino
289e4b17023SJohn Marino /* The additional information we keep track of for a label binding.
290e4b17023SJohn Marino These fields are updated as scopes are popped. */
291e4b17023SJohn Marino
292e4b17023SJohn Marino struct GTY(()) c_label_vars {
293e4b17023SJohn Marino /* The shadowed c_label_vars, when one label shadows another (which
294e4b17023SJohn Marino can only happen using a __label__ declaration). */
295e4b17023SJohn Marino struct c_label_vars *shadowed;
296e4b17023SJohn Marino /* The bindings when the label was defined. */
297e4b17023SJohn Marino struct c_spot_bindings label_bindings;
298e4b17023SJohn Marino /* A list of decls that we care about: decls about which we should
299e4b17023SJohn Marino warn if a goto branches to this label from later in the function.
300e4b17023SJohn Marino Decls are added to this list as scopes are popped. We only add
301e4b17023SJohn Marino the decls that matter. */
302e4b17023SJohn Marino VEC(tree,gc) *decls_in_scope;
303e4b17023SJohn Marino /* A list of goto statements to this label. This is only used for
304e4b17023SJohn Marino goto statements seen before the label was defined, so that we can
305e4b17023SJohn Marino issue appropriate warnings for them. */
306e4b17023SJohn Marino VEC(c_goto_bindings_p,gc) *gotos;
307e4b17023SJohn Marino };
308e4b17023SJohn Marino
309e4b17023SJohn Marino /* Each c_scope structure describes the complete contents of one
310e4b17023SJohn Marino scope. Four scopes are distinguished specially: the innermost or
311e4b17023SJohn Marino current scope, the innermost function scope, the file scope (always
312e4b17023SJohn Marino the second to outermost) and the outermost or external scope.
313e4b17023SJohn Marino
314e4b17023SJohn Marino Most declarations are recorded in the current scope.
315e4b17023SJohn Marino
316e4b17023SJohn Marino All normal label declarations are recorded in the innermost
317e4b17023SJohn Marino function scope, as are bindings of undeclared identifiers to
318e4b17023SJohn Marino error_mark_node. (GCC permits nested functions as an extension,
319e4b17023SJohn Marino hence the 'innermost' qualifier.) Explicitly declared labels
320e4b17023SJohn Marino (using the __label__ extension) appear in the current scope.
321e4b17023SJohn Marino
322e4b17023SJohn Marino Being in the file scope (current_scope == file_scope) causes
323e4b17023SJohn Marino special behavior in several places below. Also, under some
324e4b17023SJohn Marino conditions the Objective-C front end records declarations in the
325e4b17023SJohn Marino file scope even though that isn't the current scope.
326e4b17023SJohn Marino
327e4b17023SJohn Marino All declarations with external linkage are recorded in the external
328e4b17023SJohn Marino scope, even if they aren't visible there; this models the fact that
329e4b17023SJohn Marino such declarations are visible to the entire program, and (with a
330e4b17023SJohn Marino bit of cleverness, see pushdecl) allows diagnosis of some violations
331e4b17023SJohn Marino of C99 6.2.2p7 and 6.2.7p2:
332e4b17023SJohn Marino
333e4b17023SJohn Marino If, within the same translation unit, the same identifier appears
334e4b17023SJohn Marino with both internal and external linkage, the behavior is
335e4b17023SJohn Marino undefined.
336e4b17023SJohn Marino
337e4b17023SJohn Marino All declarations that refer to the same object or function shall
338e4b17023SJohn Marino have compatible type; otherwise, the behavior is undefined.
339e4b17023SJohn Marino
340e4b17023SJohn Marino Initially only the built-in declarations, which describe compiler
341e4b17023SJohn Marino intrinsic functions plus a subset of the standard library, are in
342e4b17023SJohn Marino this scope.
343e4b17023SJohn Marino
344e4b17023SJohn Marino The order of the blocks list matters, and it is frequently appended
345e4b17023SJohn Marino to. To avoid having to walk all the way to the end of the list on
346e4b17023SJohn Marino each insertion, or reverse the list later, we maintain a pointer to
347e4b17023SJohn Marino the last list entry. (FIXME: It should be feasible to use a reversed
348e4b17023SJohn Marino list here.)
349e4b17023SJohn Marino
350e4b17023SJohn Marino The bindings list is strictly in reverse order of declarations;
351e4b17023SJohn Marino pop_scope relies on this. */
352e4b17023SJohn Marino
353e4b17023SJohn Marino
354e4b17023SJohn Marino struct GTY((chain_next ("%h.outer"))) c_scope {
355e4b17023SJohn Marino /* The scope containing this one. */
356e4b17023SJohn Marino struct c_scope *outer;
357e4b17023SJohn Marino
358e4b17023SJohn Marino /* The next outermost function scope. */
359e4b17023SJohn Marino struct c_scope *outer_function;
360e4b17023SJohn Marino
361e4b17023SJohn Marino /* All bindings in this scope. */
362e4b17023SJohn Marino struct c_binding *bindings;
363e4b17023SJohn Marino
364e4b17023SJohn Marino /* For each scope (except the global one), a chain of BLOCK nodes
365e4b17023SJohn Marino for all the scopes that were entered and exited one level down. */
366e4b17023SJohn Marino tree blocks;
367e4b17023SJohn Marino tree blocks_last;
368e4b17023SJohn Marino
369e4b17023SJohn Marino /* The depth of this scope. Used to keep the ->shadowed chain of
370e4b17023SJohn Marino bindings sorted innermost to outermost. */
371e4b17023SJohn Marino unsigned int depth : 28;
372e4b17023SJohn Marino
373e4b17023SJohn Marino /* True if we are currently filling this scope with parameter
374e4b17023SJohn Marino declarations. */
375e4b17023SJohn Marino BOOL_BITFIELD parm_flag : 1;
376e4b17023SJohn Marino
377e4b17023SJohn Marino /* True if we saw [*] in this scope. Used to give an error messages
378e4b17023SJohn Marino if these appears in a function definition. */
379e4b17023SJohn Marino BOOL_BITFIELD had_vla_unspec : 1;
380e4b17023SJohn Marino
381e4b17023SJohn Marino /* True if we already complained about forward parameter decls
382e4b17023SJohn Marino in this scope. This prevents double warnings on
383e4b17023SJohn Marino foo (int a; int b; ...) */
384e4b17023SJohn Marino BOOL_BITFIELD warned_forward_parm_decls : 1;
385e4b17023SJohn Marino
386e4b17023SJohn Marino /* True if this is the outermost block scope of a function body.
387e4b17023SJohn Marino This scope contains the parameters, the local variables declared
388e4b17023SJohn Marino in the outermost block, and all the labels (except those in
389e4b17023SJohn Marino nested functions, or declared at block scope with __label__). */
390e4b17023SJohn Marino BOOL_BITFIELD function_body : 1;
391e4b17023SJohn Marino
392e4b17023SJohn Marino /* True means make a BLOCK for this scope no matter what. */
393e4b17023SJohn Marino BOOL_BITFIELD keep : 1;
394e4b17023SJohn Marino
395e4b17023SJohn Marino /* True means that an unsuffixed float constant is _Decimal64. */
396e4b17023SJohn Marino BOOL_BITFIELD float_const_decimal64 : 1;
397e4b17023SJohn Marino
398e4b17023SJohn Marino /* True if this scope has any label bindings. This is used to speed
399e4b17023SJohn Marino up searching for labels when popping scopes, particularly since
400e4b17023SJohn Marino labels are normally only found at function scope. */
401e4b17023SJohn Marino BOOL_BITFIELD has_label_bindings : 1;
402e4b17023SJohn Marino
403e4b17023SJohn Marino /* True if we should issue a warning if a goto statement crosses any
404e4b17023SJohn Marino of the bindings. We still need to check the list of bindings to
405e4b17023SJohn Marino find the specific ones we need to warn about. This is true if
406e4b17023SJohn Marino decl_jump_unsafe would return true for any of the bindings. This
407e4b17023SJohn Marino is used to avoid looping over all the bindings unnecessarily. */
408e4b17023SJohn Marino BOOL_BITFIELD has_jump_unsafe_decl : 1;
409e4b17023SJohn Marino };
410e4b17023SJohn Marino
411e4b17023SJohn Marino /* The scope currently in effect. */
412e4b17023SJohn Marino
413e4b17023SJohn Marino static GTY(()) struct c_scope *current_scope;
414e4b17023SJohn Marino
415e4b17023SJohn Marino /* The innermost function scope. Ordinary (not explicitly declared)
416e4b17023SJohn Marino labels, bindings to error_mark_node, and the lazily-created
417e4b17023SJohn Marino bindings of __func__ and its friends get this scope. */
418e4b17023SJohn Marino
419e4b17023SJohn Marino static GTY(()) struct c_scope *current_function_scope;
420e4b17023SJohn Marino
421e4b17023SJohn Marino /* The C file scope. This is reset for each input translation unit. */
422e4b17023SJohn Marino
423e4b17023SJohn Marino static GTY(()) struct c_scope *file_scope;
424e4b17023SJohn Marino
425e4b17023SJohn Marino /* The outermost scope. This is used for all declarations with
426e4b17023SJohn Marino external linkage, and only these, hence the name. */
427e4b17023SJohn Marino
428e4b17023SJohn Marino static GTY(()) struct c_scope *external_scope;
429e4b17023SJohn Marino
430e4b17023SJohn Marino /* A chain of c_scope structures awaiting reuse. */
431e4b17023SJohn Marino
432e4b17023SJohn Marino static GTY((deletable)) struct c_scope *scope_freelist;
433e4b17023SJohn Marino
434e4b17023SJohn Marino /* A chain of c_binding structures awaiting reuse. */
435e4b17023SJohn Marino
436e4b17023SJohn Marino static GTY((deletable)) struct c_binding *binding_freelist;
437e4b17023SJohn Marino
438e4b17023SJohn Marino /* Append VAR to LIST in scope SCOPE. */
439e4b17023SJohn Marino #define SCOPE_LIST_APPEND(scope, list, decl) do { \
440e4b17023SJohn Marino struct c_scope *s_ = (scope); \
441e4b17023SJohn Marino tree d_ = (decl); \
442e4b17023SJohn Marino if (s_->list##_last) \
443e4b17023SJohn Marino BLOCK_CHAIN (s_->list##_last) = d_; \
444e4b17023SJohn Marino else \
445e4b17023SJohn Marino s_->list = d_; \
446e4b17023SJohn Marino s_->list##_last = d_; \
447e4b17023SJohn Marino } while (0)
448e4b17023SJohn Marino
449e4b17023SJohn Marino /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
450e4b17023SJohn Marino #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
451e4b17023SJohn Marino struct c_scope *t_ = (tscope); \
452e4b17023SJohn Marino struct c_scope *f_ = (fscope); \
453e4b17023SJohn Marino if (t_->to##_last) \
454e4b17023SJohn Marino BLOCK_CHAIN (t_->to##_last) = f_->from; \
455e4b17023SJohn Marino else \
456e4b17023SJohn Marino t_->to = f_->from; \
457e4b17023SJohn Marino t_->to##_last = f_->from##_last; \
458e4b17023SJohn Marino } while (0)
459e4b17023SJohn Marino
460e4b17023SJohn Marino /* A c_inline_static structure stores details of a static identifier
461e4b17023SJohn Marino referenced in a definition of a function that may be an inline
462e4b17023SJohn Marino definition if no subsequent declaration of that function uses
463e4b17023SJohn Marino "extern" or does not use "inline". */
464e4b17023SJohn Marino
465e4b17023SJohn Marino struct GTY((chain_next ("%h.next"))) c_inline_static {
466e4b17023SJohn Marino /* The location for a diagnostic. */
467e4b17023SJohn Marino location_t location;
468e4b17023SJohn Marino
469e4b17023SJohn Marino /* The function that may be an inline definition. */
470e4b17023SJohn Marino tree function;
471e4b17023SJohn Marino
472e4b17023SJohn Marino /* The object or function referenced. */
473e4b17023SJohn Marino tree static_decl;
474e4b17023SJohn Marino
475e4b17023SJohn Marino /* What sort of reference this is. */
476e4b17023SJohn Marino enum c_inline_static_type type;
477e4b17023SJohn Marino
478e4b17023SJohn Marino /* The next such structure or NULL. */
479e4b17023SJohn Marino struct c_inline_static *next;
480e4b17023SJohn Marino };
481e4b17023SJohn Marino
482e4b17023SJohn Marino /* List of static identifiers used or referenced in functions that may
483e4b17023SJohn Marino be inline definitions. */
484e4b17023SJohn Marino static GTY(()) struct c_inline_static *c_inline_statics;
485e4b17023SJohn Marino
486e4b17023SJohn Marino /* True means unconditionally make a BLOCK for the next scope pushed. */
487e4b17023SJohn Marino
488e4b17023SJohn Marino static bool keep_next_level_flag;
489e4b17023SJohn Marino
490e4b17023SJohn Marino /* True means the next call to push_scope will be the outermost scope
491e4b17023SJohn Marino of a function body, so do not push a new scope, merely cease
492e4b17023SJohn Marino expecting parameter decls. */
493e4b17023SJohn Marino
494e4b17023SJohn Marino static bool next_is_function_body;
495e4b17023SJohn Marino
496e4b17023SJohn Marino /* A VEC of pointers to c_binding structures. */
497e4b17023SJohn Marino
498e4b17023SJohn Marino typedef struct c_binding *c_binding_ptr;
499e4b17023SJohn Marino DEF_VEC_P(c_binding_ptr);
500e4b17023SJohn Marino DEF_VEC_ALLOC_P(c_binding_ptr,heap);
501e4b17023SJohn Marino
502e4b17023SJohn Marino /* Information that we keep for a struct or union while it is being
503e4b17023SJohn Marino parsed. */
504e4b17023SJohn Marino
505e4b17023SJohn Marino struct c_struct_parse_info
506e4b17023SJohn Marino {
507e4b17023SJohn Marino /* If warn_cxx_compat, a list of types defined within this
508e4b17023SJohn Marino struct. */
509e4b17023SJohn Marino VEC(tree,heap) *struct_types;
510e4b17023SJohn Marino /* If warn_cxx_compat, a list of field names which have bindings,
511e4b17023SJohn Marino and which are defined in this struct, but which are not defined
512e4b17023SJohn Marino in any enclosing struct. This is used to clear the in_struct
513e4b17023SJohn Marino field of the c_bindings structure. */
514e4b17023SJohn Marino VEC(c_binding_ptr,heap) *fields;
515e4b17023SJohn Marino /* If warn_cxx_compat, a list of typedef names used when defining
516e4b17023SJohn Marino fields in this struct. */
517e4b17023SJohn Marino VEC(tree,heap) *typedefs_seen;
518e4b17023SJohn Marino };
519e4b17023SJohn Marino
520e4b17023SJohn Marino /* Information for the struct or union currently being parsed, or
521e4b17023SJohn Marino NULL if not parsing a struct or union. */
522e4b17023SJohn Marino static struct c_struct_parse_info *struct_parse_info;
523e4b17023SJohn Marino
524e4b17023SJohn Marino /* Forward declarations. */
525e4b17023SJohn Marino static tree lookup_name_in_scope (tree, struct c_scope *);
526e4b17023SJohn Marino static tree c_make_fname_decl (location_t, tree, int);
527e4b17023SJohn Marino static tree grokdeclarator (const struct c_declarator *,
528e4b17023SJohn Marino struct c_declspecs *,
529e4b17023SJohn Marino enum decl_context, bool, tree *, tree *, tree *,
530e4b17023SJohn Marino bool *, enum deprecated_states);
531e4b17023SJohn Marino static tree grokparms (struct c_arg_info *, bool);
532e4b17023SJohn Marino static void layout_array_type (tree);
533e4b17023SJohn Marino
534e4b17023SJohn Marino /* T is a statement. Add it to the statement-tree. This is the
535e4b17023SJohn Marino C/ObjC version--C++ has a slightly different version of this
536e4b17023SJohn Marino function. */
537e4b17023SJohn Marino
538e4b17023SJohn Marino tree
add_stmt(tree t)539e4b17023SJohn Marino add_stmt (tree t)
540e4b17023SJohn Marino {
541e4b17023SJohn Marino enum tree_code code = TREE_CODE (t);
542e4b17023SJohn Marino
543e4b17023SJohn Marino if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
544e4b17023SJohn Marino {
545e4b17023SJohn Marino if (!EXPR_HAS_LOCATION (t))
546e4b17023SJohn Marino SET_EXPR_LOCATION (t, input_location);
547e4b17023SJohn Marino }
548e4b17023SJohn Marino
549e4b17023SJohn Marino if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
550e4b17023SJohn Marino STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
551e4b17023SJohn Marino
552e4b17023SJohn Marino /* Add T to the statement-tree. Non-side-effect statements need to be
553e4b17023SJohn Marino recorded during statement expressions. */
554e4b17023SJohn Marino if (!building_stmt_list_p ())
555e4b17023SJohn Marino push_stmt_list ();
556e4b17023SJohn Marino append_to_statement_list_force (t, &cur_stmt_list);
557e4b17023SJohn Marino
558e4b17023SJohn Marino return t;
559e4b17023SJohn Marino }
560e4b17023SJohn Marino
561e4b17023SJohn Marino /* Return true if we will want to say something if a goto statement
562e4b17023SJohn Marino crosses DECL. */
563e4b17023SJohn Marino
564e4b17023SJohn Marino static bool
decl_jump_unsafe(tree decl)565e4b17023SJohn Marino decl_jump_unsafe (tree decl)
566e4b17023SJohn Marino {
567e4b17023SJohn Marino if (error_operand_p (decl))
568e4b17023SJohn Marino return false;
569e4b17023SJohn Marino
570e4b17023SJohn Marino /* Always warn about crossing variably modified types. */
571e4b17023SJohn Marino if ((TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == TYPE_DECL)
572e4b17023SJohn Marino && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
573e4b17023SJohn Marino return true;
574e4b17023SJohn Marino
575e4b17023SJohn Marino /* Otherwise, only warn if -Wgoto-misses-init and this is an
576e4b17023SJohn Marino initialized automatic decl. */
577e4b17023SJohn Marino if (warn_jump_misses_init
578e4b17023SJohn Marino && TREE_CODE (decl) == VAR_DECL
579e4b17023SJohn Marino && !TREE_STATIC (decl)
580e4b17023SJohn Marino && DECL_INITIAL (decl) != NULL_TREE)
581e4b17023SJohn Marino return true;
582e4b17023SJohn Marino
583e4b17023SJohn Marino return false;
584e4b17023SJohn Marino }
585e4b17023SJohn Marino
586e4b17023SJohn Marino
587e4b17023SJohn Marino void
c_print_identifier(FILE * file,tree node,int indent)588e4b17023SJohn Marino c_print_identifier (FILE *file, tree node, int indent)
589e4b17023SJohn Marino {
590e4b17023SJohn Marino print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
591e4b17023SJohn Marino print_node (file, "tag", I_TAG_DECL (node), indent + 4);
592e4b17023SJohn Marino print_node (file, "label", I_LABEL_DECL (node), indent + 4);
593e4b17023SJohn Marino if (C_IS_RESERVED_WORD (node) && C_RID_CODE (node) != RID_CXX_COMPAT_WARN)
594e4b17023SJohn Marino {
595e4b17023SJohn Marino tree rid = ridpointers[C_RID_CODE (node)];
596e4b17023SJohn Marino indent_to (file, indent + 4);
597e4b17023SJohn Marino fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
598e4b17023SJohn Marino (void *) rid, IDENTIFIER_POINTER (rid));
599e4b17023SJohn Marino }
600e4b17023SJohn Marino }
601e4b17023SJohn Marino
602e4b17023SJohn Marino /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
603e4b17023SJohn Marino which may be any of several kinds of DECL or TYPE or error_mark_node,
604e4b17023SJohn Marino in the scope SCOPE. */
605e4b17023SJohn Marino static void
bind(tree name,tree decl,struct c_scope * scope,bool invisible,bool nested,location_t locus)606e4b17023SJohn Marino bind (tree name, tree decl, struct c_scope *scope, bool invisible,
607e4b17023SJohn Marino bool nested, location_t locus)
608e4b17023SJohn Marino {
609e4b17023SJohn Marino struct c_binding *b, **here;
610e4b17023SJohn Marino
611e4b17023SJohn Marino if (binding_freelist)
612e4b17023SJohn Marino {
613e4b17023SJohn Marino b = binding_freelist;
614e4b17023SJohn Marino binding_freelist = b->prev;
615e4b17023SJohn Marino }
616e4b17023SJohn Marino else
617e4b17023SJohn Marino b = ggc_alloc_c_binding ();
618e4b17023SJohn Marino
619e4b17023SJohn Marino b->shadowed = 0;
620e4b17023SJohn Marino b->decl = decl;
621e4b17023SJohn Marino b->id = name;
622e4b17023SJohn Marino b->depth = scope->depth;
623e4b17023SJohn Marino b->invisible = invisible;
624e4b17023SJohn Marino b->nested = nested;
625e4b17023SJohn Marino b->inner_comp = 0;
626e4b17023SJohn Marino b->in_struct = 0;
627e4b17023SJohn Marino b->locus = locus;
628e4b17023SJohn Marino
629e4b17023SJohn Marino b->u.type = NULL;
630e4b17023SJohn Marino
631e4b17023SJohn Marino b->prev = scope->bindings;
632e4b17023SJohn Marino scope->bindings = b;
633e4b17023SJohn Marino
634e4b17023SJohn Marino if (decl_jump_unsafe (decl))
635e4b17023SJohn Marino scope->has_jump_unsafe_decl = 1;
636e4b17023SJohn Marino
637e4b17023SJohn Marino if (!name)
638e4b17023SJohn Marino return;
639e4b17023SJohn Marino
640e4b17023SJohn Marino switch (TREE_CODE (decl))
641e4b17023SJohn Marino {
642e4b17023SJohn Marino case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
643e4b17023SJohn Marino case ENUMERAL_TYPE:
644e4b17023SJohn Marino case UNION_TYPE:
645e4b17023SJohn Marino case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
646e4b17023SJohn Marino case VAR_DECL:
647e4b17023SJohn Marino case FUNCTION_DECL:
648e4b17023SJohn Marino case TYPE_DECL:
649e4b17023SJohn Marino case CONST_DECL:
650e4b17023SJohn Marino case PARM_DECL:
651e4b17023SJohn Marino case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
652e4b17023SJohn Marino
653e4b17023SJohn Marino default:
654e4b17023SJohn Marino gcc_unreachable ();
655e4b17023SJohn Marino }
656e4b17023SJohn Marino
657e4b17023SJohn Marino /* Locate the appropriate place in the chain of shadowed decls
658e4b17023SJohn Marino to insert this binding. Normally, scope == current_scope and
659e4b17023SJohn Marino this does nothing. */
660e4b17023SJohn Marino while (*here && (*here)->depth > scope->depth)
661e4b17023SJohn Marino here = &(*here)->shadowed;
662e4b17023SJohn Marino
663e4b17023SJohn Marino b->shadowed = *here;
664e4b17023SJohn Marino *here = b;
665e4b17023SJohn Marino }
666e4b17023SJohn Marino
667e4b17023SJohn Marino /* Clear the binding structure B, stick it on the binding_freelist,
668e4b17023SJohn Marino and return the former value of b->prev. This is used by pop_scope
669e4b17023SJohn Marino and get_parm_info to iterate destructively over all the bindings
670e4b17023SJohn Marino from a given scope. */
671e4b17023SJohn Marino static struct c_binding *
free_binding_and_advance(struct c_binding * b)672e4b17023SJohn Marino free_binding_and_advance (struct c_binding *b)
673e4b17023SJohn Marino {
674e4b17023SJohn Marino struct c_binding *prev = b->prev;
675e4b17023SJohn Marino
676e4b17023SJohn Marino memset (b, 0, sizeof (struct c_binding));
677e4b17023SJohn Marino b->prev = binding_freelist;
678e4b17023SJohn Marino binding_freelist = b;
679e4b17023SJohn Marino
680e4b17023SJohn Marino return prev;
681e4b17023SJohn Marino }
682e4b17023SJohn Marino
683e4b17023SJohn Marino /* Bind a label. Like bind, but skip fields which aren't used for
684e4b17023SJohn Marino labels, and add the LABEL_VARS value. */
685e4b17023SJohn Marino static void
bind_label(tree name,tree label,struct c_scope * scope,struct c_label_vars * label_vars)686e4b17023SJohn Marino bind_label (tree name, tree label, struct c_scope *scope,
687e4b17023SJohn Marino struct c_label_vars *label_vars)
688e4b17023SJohn Marino {
689e4b17023SJohn Marino struct c_binding *b;
690e4b17023SJohn Marino
691e4b17023SJohn Marino bind (name, label, scope, /*invisible=*/false, /*nested=*/false,
692e4b17023SJohn Marino UNKNOWN_LOCATION);
693e4b17023SJohn Marino
694e4b17023SJohn Marino scope->has_label_bindings = true;
695e4b17023SJohn Marino
696e4b17023SJohn Marino b = scope->bindings;
697e4b17023SJohn Marino gcc_assert (b->decl == label);
698e4b17023SJohn Marino label_vars->shadowed = b->u.label;
699e4b17023SJohn Marino b->u.label = label_vars;
700e4b17023SJohn Marino }
701e4b17023SJohn Marino
702e4b17023SJohn Marino /* Hook called at end of compilation to assume 1 elt
703e4b17023SJohn Marino for a file-scope tentative array defn that wasn't complete before. */
704e4b17023SJohn Marino
705e4b17023SJohn Marino void
c_finish_incomplete_decl(tree decl)706e4b17023SJohn Marino c_finish_incomplete_decl (tree decl)
707e4b17023SJohn Marino {
708e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL)
709e4b17023SJohn Marino {
710e4b17023SJohn Marino tree type = TREE_TYPE (decl);
711e4b17023SJohn Marino if (type != error_mark_node
712e4b17023SJohn Marino && TREE_CODE (type) == ARRAY_TYPE
713e4b17023SJohn Marino && !DECL_EXTERNAL (decl)
714e4b17023SJohn Marino && TYPE_DOMAIN (type) == 0)
715e4b17023SJohn Marino {
716e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (decl),
717e4b17023SJohn Marino 0, "array %q+D assumed to have one element", decl);
718e4b17023SJohn Marino
719e4b17023SJohn Marino complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
720e4b17023SJohn Marino
721e4b17023SJohn Marino relayout_decl (decl);
722e4b17023SJohn Marino }
723e4b17023SJohn Marino }
724e4b17023SJohn Marino }
725e4b17023SJohn Marino
726e4b17023SJohn Marino /* Record that inline function FUNC contains a reference (location
727e4b17023SJohn Marino LOC) to static DECL (file-scope or function-local according to
728e4b17023SJohn Marino TYPE). */
729e4b17023SJohn Marino
730e4b17023SJohn Marino void
record_inline_static(location_t loc,tree func,tree decl,enum c_inline_static_type type)731e4b17023SJohn Marino record_inline_static (location_t loc, tree func, tree decl,
732e4b17023SJohn Marino enum c_inline_static_type type)
733e4b17023SJohn Marino {
734e4b17023SJohn Marino struct c_inline_static *csi = ggc_alloc_c_inline_static ();
735e4b17023SJohn Marino csi->location = loc;
736e4b17023SJohn Marino csi->function = func;
737e4b17023SJohn Marino csi->static_decl = decl;
738e4b17023SJohn Marino csi->type = type;
739e4b17023SJohn Marino csi->next = c_inline_statics;
740e4b17023SJohn Marino c_inline_statics = csi;
741e4b17023SJohn Marino }
742e4b17023SJohn Marino
743e4b17023SJohn Marino /* Check for references to static declarations in inline functions at
744e4b17023SJohn Marino the end of the translation unit and diagnose them if the functions
745e4b17023SJohn Marino are still inline definitions. */
746e4b17023SJohn Marino
747e4b17023SJohn Marino static void
check_inline_statics(void)748e4b17023SJohn Marino check_inline_statics (void)
749e4b17023SJohn Marino {
750e4b17023SJohn Marino struct c_inline_static *csi;
751e4b17023SJohn Marino for (csi = c_inline_statics; csi; csi = csi->next)
752e4b17023SJohn Marino {
753e4b17023SJohn Marino if (DECL_EXTERNAL (csi->function))
754e4b17023SJohn Marino switch (csi->type)
755e4b17023SJohn Marino {
756e4b17023SJohn Marino case csi_internal:
757e4b17023SJohn Marino pedwarn (csi->location, 0,
758e4b17023SJohn Marino "%qD is static but used in inline function %qD "
759e4b17023SJohn Marino "which is not static", csi->static_decl, csi->function);
760e4b17023SJohn Marino break;
761e4b17023SJohn Marino case csi_modifiable:
762e4b17023SJohn Marino pedwarn (csi->location, 0,
763e4b17023SJohn Marino "%q+D is static but declared in inline function %qD "
764e4b17023SJohn Marino "which is not static", csi->static_decl, csi->function);
765e4b17023SJohn Marino break;
766e4b17023SJohn Marino default:
767e4b17023SJohn Marino gcc_unreachable ();
768e4b17023SJohn Marino }
769e4b17023SJohn Marino }
770e4b17023SJohn Marino c_inline_statics = NULL;
771e4b17023SJohn Marino }
772e4b17023SJohn Marino
773e4b17023SJohn Marino /* Fill in a c_spot_bindings structure. If DEFINING is true, set it
774e4b17023SJohn Marino for the current state, otherwise set it to uninitialized. */
775e4b17023SJohn Marino
776e4b17023SJohn Marino static void
set_spot_bindings(struct c_spot_bindings * p,bool defining)777e4b17023SJohn Marino set_spot_bindings (struct c_spot_bindings *p, bool defining)
778e4b17023SJohn Marino {
779e4b17023SJohn Marino if (defining)
780e4b17023SJohn Marino {
781e4b17023SJohn Marino p->scope = current_scope;
782e4b17023SJohn Marino p->bindings_in_scope = current_scope->bindings;
783e4b17023SJohn Marino }
784e4b17023SJohn Marino else
785e4b17023SJohn Marino {
786e4b17023SJohn Marino p->scope = NULL;
787e4b17023SJohn Marino p->bindings_in_scope = NULL;
788e4b17023SJohn Marino }
789e4b17023SJohn Marino p->stmt_exprs = 0;
790e4b17023SJohn Marino p->left_stmt_expr = false;
791e4b17023SJohn Marino }
792e4b17023SJohn Marino
793e4b17023SJohn Marino /* Update spot bindings P as we pop out of SCOPE. Return true if we
794e4b17023SJohn Marino should push decls for a label. */
795e4b17023SJohn Marino
796e4b17023SJohn Marino static bool
update_spot_bindings(struct c_scope * scope,struct c_spot_bindings * p)797e4b17023SJohn Marino update_spot_bindings (struct c_scope *scope, struct c_spot_bindings *p)
798e4b17023SJohn Marino {
799e4b17023SJohn Marino if (p->scope != scope)
800e4b17023SJohn Marino {
801e4b17023SJohn Marino /* This label or goto is defined in some other scope, or it is a
802e4b17023SJohn Marino label which is not yet defined. There is nothing to
803e4b17023SJohn Marino update. */
804e4b17023SJohn Marino return false;
805e4b17023SJohn Marino }
806e4b17023SJohn Marino
807e4b17023SJohn Marino /* Adjust the spot bindings to refer to the bindings already defined
808e4b17023SJohn Marino in the enclosing scope. */
809e4b17023SJohn Marino p->scope = scope->outer;
810e4b17023SJohn Marino p->bindings_in_scope = p->scope->bindings;
811e4b17023SJohn Marino
812e4b17023SJohn Marino return true;
813e4b17023SJohn Marino }
814e4b17023SJohn Marino
815e4b17023SJohn Marino /* The Objective-C front-end often needs to determine the current scope. */
816e4b17023SJohn Marino
817e4b17023SJohn Marino void *
objc_get_current_scope(void)818e4b17023SJohn Marino objc_get_current_scope (void)
819e4b17023SJohn Marino {
820e4b17023SJohn Marino return current_scope;
821e4b17023SJohn Marino }
822e4b17023SJohn Marino
823e4b17023SJohn Marino /* The following function is used only by Objective-C. It needs to live here
824e4b17023SJohn Marino because it accesses the innards of c_scope. */
825e4b17023SJohn Marino
826e4b17023SJohn Marino void
objc_mark_locals_volatile(void * enclosing_blk)827e4b17023SJohn Marino objc_mark_locals_volatile (void *enclosing_blk)
828e4b17023SJohn Marino {
829e4b17023SJohn Marino struct c_scope *scope;
830e4b17023SJohn Marino struct c_binding *b;
831e4b17023SJohn Marino
832e4b17023SJohn Marino for (scope = current_scope;
833e4b17023SJohn Marino scope && scope != enclosing_blk;
834e4b17023SJohn Marino scope = scope->outer)
835e4b17023SJohn Marino {
836e4b17023SJohn Marino for (b = scope->bindings; b; b = b->prev)
837e4b17023SJohn Marino objc_volatilize_decl (b->decl);
838e4b17023SJohn Marino
839e4b17023SJohn Marino /* Do not climb up past the current function. */
840e4b17023SJohn Marino if (scope->function_body)
841e4b17023SJohn Marino break;
842e4b17023SJohn Marino }
843e4b17023SJohn Marino }
844e4b17023SJohn Marino
845e4b17023SJohn Marino /* Return true if we are in the global binding level. */
846e4b17023SJohn Marino
847e4b17023SJohn Marino bool
global_bindings_p(void)848e4b17023SJohn Marino global_bindings_p (void)
849e4b17023SJohn Marino {
850e4b17023SJohn Marino return current_scope == file_scope;
851e4b17023SJohn Marino }
852e4b17023SJohn Marino
853e4b17023SJohn Marino void
keep_next_level(void)854e4b17023SJohn Marino keep_next_level (void)
855e4b17023SJohn Marino {
856e4b17023SJohn Marino keep_next_level_flag = true;
857e4b17023SJohn Marino }
858e4b17023SJohn Marino
859e4b17023SJohn Marino /* Set the flag for the FLOAT_CONST_DECIMAL64 pragma being ON. */
860e4b17023SJohn Marino
861e4b17023SJohn Marino void
set_float_const_decimal64(void)862e4b17023SJohn Marino set_float_const_decimal64 (void)
863e4b17023SJohn Marino {
864e4b17023SJohn Marino current_scope->float_const_decimal64 = true;
865e4b17023SJohn Marino }
866e4b17023SJohn Marino
867e4b17023SJohn Marino /* Clear the flag for the FLOAT_CONST_DECIMAL64 pragma. */
868e4b17023SJohn Marino
869e4b17023SJohn Marino void
clear_float_const_decimal64(void)870e4b17023SJohn Marino clear_float_const_decimal64 (void)
871e4b17023SJohn Marino {
872e4b17023SJohn Marino current_scope->float_const_decimal64 = false;
873e4b17023SJohn Marino }
874e4b17023SJohn Marino
875e4b17023SJohn Marino /* Return nonzero if an unsuffixed float constant is _Decimal64. */
876e4b17023SJohn Marino
877e4b17023SJohn Marino bool
float_const_decimal64_p(void)878e4b17023SJohn Marino float_const_decimal64_p (void)
879e4b17023SJohn Marino {
880e4b17023SJohn Marino return current_scope->float_const_decimal64;
881e4b17023SJohn Marino }
882e4b17023SJohn Marino
883e4b17023SJohn Marino /* Identify this scope as currently being filled with parameters. */
884e4b17023SJohn Marino
885e4b17023SJohn Marino void
declare_parm_level(void)886e4b17023SJohn Marino declare_parm_level (void)
887e4b17023SJohn Marino {
888e4b17023SJohn Marino current_scope->parm_flag = true;
889e4b17023SJohn Marino }
890e4b17023SJohn Marino
891e4b17023SJohn Marino void
push_scope(void)892e4b17023SJohn Marino push_scope (void)
893e4b17023SJohn Marino {
894e4b17023SJohn Marino if (next_is_function_body)
895e4b17023SJohn Marino {
896e4b17023SJohn Marino /* This is the transition from the parameters to the top level
897e4b17023SJohn Marino of the function body. These are the same scope
898e4b17023SJohn Marino (C99 6.2.1p4,6) so we do not push another scope structure.
899e4b17023SJohn Marino next_is_function_body is set only by store_parm_decls, which
900e4b17023SJohn Marino in turn is called when and only when we are about to
901e4b17023SJohn Marino encounter the opening curly brace for the function body.
902e4b17023SJohn Marino
903e4b17023SJohn Marino The outermost block of a function always gets a BLOCK node,
904e4b17023SJohn Marino because the debugging output routines expect that each
905e4b17023SJohn Marino function has at least one BLOCK. */
906e4b17023SJohn Marino current_scope->parm_flag = false;
907e4b17023SJohn Marino current_scope->function_body = true;
908e4b17023SJohn Marino current_scope->keep = true;
909e4b17023SJohn Marino current_scope->outer_function = current_function_scope;
910e4b17023SJohn Marino current_function_scope = current_scope;
911e4b17023SJohn Marino
912e4b17023SJohn Marino keep_next_level_flag = false;
913e4b17023SJohn Marino next_is_function_body = false;
914e4b17023SJohn Marino
915e4b17023SJohn Marino /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
916e4b17023SJohn Marino if (current_scope->outer)
917e4b17023SJohn Marino current_scope->float_const_decimal64
918e4b17023SJohn Marino = current_scope->outer->float_const_decimal64;
919e4b17023SJohn Marino else
920e4b17023SJohn Marino current_scope->float_const_decimal64 = false;
921e4b17023SJohn Marino }
922e4b17023SJohn Marino else
923e4b17023SJohn Marino {
924e4b17023SJohn Marino struct c_scope *scope;
925e4b17023SJohn Marino if (scope_freelist)
926e4b17023SJohn Marino {
927e4b17023SJohn Marino scope = scope_freelist;
928e4b17023SJohn Marino scope_freelist = scope->outer;
929e4b17023SJohn Marino }
930e4b17023SJohn Marino else
931e4b17023SJohn Marino scope = ggc_alloc_cleared_c_scope ();
932e4b17023SJohn Marino
933e4b17023SJohn Marino /* The FLOAT_CONST_DECIMAL64 pragma applies to nested scopes. */
934e4b17023SJohn Marino if (current_scope)
935e4b17023SJohn Marino scope->float_const_decimal64 = current_scope->float_const_decimal64;
936e4b17023SJohn Marino else
937e4b17023SJohn Marino scope->float_const_decimal64 = false;
938e4b17023SJohn Marino
939e4b17023SJohn Marino scope->keep = keep_next_level_flag;
940e4b17023SJohn Marino scope->outer = current_scope;
941e4b17023SJohn Marino scope->depth = current_scope ? (current_scope->depth + 1) : 0;
942e4b17023SJohn Marino
943e4b17023SJohn Marino /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
944e4b17023SJohn Marino possible. */
945e4b17023SJohn Marino if (current_scope && scope->depth == 0)
946e4b17023SJohn Marino {
947e4b17023SJohn Marino scope->depth--;
948e4b17023SJohn Marino sorry ("GCC supports only %u nested scopes", scope->depth);
949e4b17023SJohn Marino }
950e4b17023SJohn Marino
951e4b17023SJohn Marino current_scope = scope;
952e4b17023SJohn Marino keep_next_level_flag = false;
953e4b17023SJohn Marino }
954e4b17023SJohn Marino }
955e4b17023SJohn Marino
956e4b17023SJohn Marino /* This is called when we are leaving SCOPE. For each label defined
957e4b17023SJohn Marino in SCOPE, add any appropriate decls to its decls_in_scope fields.
958e4b17023SJohn Marino These are the decls whose initialization will be skipped by a goto
959e4b17023SJohn Marino later in the function. */
960e4b17023SJohn Marino
961e4b17023SJohn Marino static void
update_label_decls(struct c_scope * scope)962e4b17023SJohn Marino update_label_decls (struct c_scope *scope)
963e4b17023SJohn Marino {
964e4b17023SJohn Marino struct c_scope *s;
965e4b17023SJohn Marino
966e4b17023SJohn Marino s = scope;
967e4b17023SJohn Marino while (s != NULL)
968e4b17023SJohn Marino {
969e4b17023SJohn Marino if (s->has_label_bindings)
970e4b17023SJohn Marino {
971e4b17023SJohn Marino struct c_binding *b;
972e4b17023SJohn Marino
973e4b17023SJohn Marino for (b = s->bindings; b != NULL; b = b->prev)
974e4b17023SJohn Marino {
975e4b17023SJohn Marino struct c_label_vars *label_vars;
976e4b17023SJohn Marino struct c_binding *b1;
977e4b17023SJohn Marino bool hjud;
978e4b17023SJohn Marino unsigned int ix;
979e4b17023SJohn Marino struct c_goto_bindings *g;
980e4b17023SJohn Marino
981e4b17023SJohn Marino if (TREE_CODE (b->decl) != LABEL_DECL)
982e4b17023SJohn Marino continue;
983e4b17023SJohn Marino label_vars = b->u.label;
984e4b17023SJohn Marino
985e4b17023SJohn Marino b1 = label_vars->label_bindings.bindings_in_scope;
986e4b17023SJohn Marino if (label_vars->label_bindings.scope == NULL)
987e4b17023SJohn Marino hjud = false;
988e4b17023SJohn Marino else
989e4b17023SJohn Marino hjud = label_vars->label_bindings.scope->has_jump_unsafe_decl;
990e4b17023SJohn Marino if (update_spot_bindings (scope, &label_vars->label_bindings))
991e4b17023SJohn Marino {
992e4b17023SJohn Marino /* This label is defined in this scope. */
993e4b17023SJohn Marino if (hjud)
994e4b17023SJohn Marino {
995e4b17023SJohn Marino for (; b1 != NULL; b1 = b1->prev)
996e4b17023SJohn Marino {
997e4b17023SJohn Marino /* A goto from later in the function to this
998e4b17023SJohn Marino label will never see the initialization
999e4b17023SJohn Marino of B1, if any. Save it to issue a
1000e4b17023SJohn Marino warning if needed. */
1001e4b17023SJohn Marino if (decl_jump_unsafe (b1->decl))
1002e4b17023SJohn Marino VEC_safe_push (tree, gc,
1003e4b17023SJohn Marino label_vars->decls_in_scope,
1004e4b17023SJohn Marino b1->decl);
1005e4b17023SJohn Marino }
1006e4b17023SJohn Marino }
1007e4b17023SJohn Marino }
1008e4b17023SJohn Marino
1009e4b17023SJohn Marino /* Update the bindings of any goto statements associated
1010e4b17023SJohn Marino with this label. */
1011e4b17023SJohn Marino FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1012e4b17023SJohn Marino update_spot_bindings (scope, &g->goto_bindings);
1013e4b17023SJohn Marino }
1014e4b17023SJohn Marino }
1015e4b17023SJohn Marino
1016e4b17023SJohn Marino /* Don't search beyond the current function. */
1017e4b17023SJohn Marino if (s == current_function_scope)
1018e4b17023SJohn Marino break;
1019e4b17023SJohn Marino
1020e4b17023SJohn Marino s = s->outer;
1021e4b17023SJohn Marino }
1022e4b17023SJohn Marino }
1023e4b17023SJohn Marino
1024e4b17023SJohn Marino /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
1025e4b17023SJohn Marino
1026e4b17023SJohn Marino static void
set_type_context(tree type,tree context)1027e4b17023SJohn Marino set_type_context (tree type, tree context)
1028e4b17023SJohn Marino {
1029e4b17023SJohn Marino for (type = TYPE_MAIN_VARIANT (type); type;
1030e4b17023SJohn Marino type = TYPE_NEXT_VARIANT (type))
1031e4b17023SJohn Marino TYPE_CONTEXT (type) = context;
1032e4b17023SJohn Marino }
1033e4b17023SJohn Marino
1034e4b17023SJohn Marino /* Exit a scope. Restore the state of the identifier-decl mappings
1035e4b17023SJohn Marino that were in effect when this scope was entered. Return a BLOCK
1036e4b17023SJohn Marino node containing all the DECLs in this scope that are of interest
1037e4b17023SJohn Marino to debug info generation. */
1038e4b17023SJohn Marino
1039e4b17023SJohn Marino tree
pop_scope(void)1040e4b17023SJohn Marino pop_scope (void)
1041e4b17023SJohn Marino {
1042e4b17023SJohn Marino struct c_scope *scope = current_scope;
1043e4b17023SJohn Marino tree block, context, p;
1044e4b17023SJohn Marino struct c_binding *b;
1045e4b17023SJohn Marino
1046e4b17023SJohn Marino bool functionbody = scope->function_body;
1047e4b17023SJohn Marino bool keep = functionbody || scope->keep || scope->bindings;
1048e4b17023SJohn Marino
1049e4b17023SJohn Marino update_label_decls (scope);
1050e4b17023SJohn Marino
1051e4b17023SJohn Marino /* If appropriate, create a BLOCK to record the decls for the life
1052e4b17023SJohn Marino of this function. */
1053e4b17023SJohn Marino block = 0;
1054e4b17023SJohn Marino if (keep)
1055e4b17023SJohn Marino {
1056e4b17023SJohn Marino block = make_node (BLOCK);
1057e4b17023SJohn Marino BLOCK_SUBBLOCKS (block) = scope->blocks;
1058e4b17023SJohn Marino TREE_USED (block) = 1;
1059e4b17023SJohn Marino
1060e4b17023SJohn Marino /* In each subblock, record that this is its superior. */
1061e4b17023SJohn Marino for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
1062e4b17023SJohn Marino BLOCK_SUPERCONTEXT (p) = block;
1063e4b17023SJohn Marino
1064e4b17023SJohn Marino BLOCK_VARS (block) = 0;
1065e4b17023SJohn Marino }
1066e4b17023SJohn Marino
1067e4b17023SJohn Marino /* The TYPE_CONTEXTs for all of the tagged types belonging to this
1068e4b17023SJohn Marino scope must be set so that they point to the appropriate
1069e4b17023SJohn Marino construct, i.e. either to the current FUNCTION_DECL node, or
1070e4b17023SJohn Marino else to the BLOCK node we just constructed.
1071e4b17023SJohn Marino
1072e4b17023SJohn Marino Note that for tagged types whose scope is just the formal
1073e4b17023SJohn Marino parameter list for some function type specification, we can't
1074e4b17023SJohn Marino properly set their TYPE_CONTEXTs here, because we don't have a
1075e4b17023SJohn Marino pointer to the appropriate FUNCTION_TYPE node readily available
1076e4b17023SJohn Marino to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
1077e4b17023SJohn Marino type nodes get set in `grokdeclarator' as soon as we have created
1078e4b17023SJohn Marino the FUNCTION_TYPE node which will represent the "scope" for these
1079e4b17023SJohn Marino "parameter list local" tagged types. */
1080e4b17023SJohn Marino if (scope->function_body)
1081e4b17023SJohn Marino context = current_function_decl;
1082e4b17023SJohn Marino else if (scope == file_scope)
1083e4b17023SJohn Marino {
1084e4b17023SJohn Marino tree file_decl = build_translation_unit_decl (NULL_TREE);
1085e4b17023SJohn Marino context = file_decl;
1086e4b17023SJohn Marino }
1087e4b17023SJohn Marino else
1088e4b17023SJohn Marino context = block;
1089e4b17023SJohn Marino
1090e4b17023SJohn Marino /* Clear all bindings in this scope. */
1091e4b17023SJohn Marino for (b = scope->bindings; b; b = free_binding_and_advance (b))
1092e4b17023SJohn Marino {
1093e4b17023SJohn Marino p = b->decl;
1094e4b17023SJohn Marino switch (TREE_CODE (p))
1095e4b17023SJohn Marino {
1096e4b17023SJohn Marino case LABEL_DECL:
1097e4b17023SJohn Marino /* Warnings for unused labels, errors for undefined labels. */
1098e4b17023SJohn Marino if (TREE_USED (p) && !DECL_INITIAL (p))
1099e4b17023SJohn Marino {
1100e4b17023SJohn Marino error ("label %q+D used but not defined", p);
1101e4b17023SJohn Marino DECL_INITIAL (p) = error_mark_node;
1102e4b17023SJohn Marino }
1103e4b17023SJohn Marino else
1104e4b17023SJohn Marino warn_for_unused_label (p);
1105e4b17023SJohn Marino
1106e4b17023SJohn Marino /* Labels go in BLOCK_VARS. */
1107e4b17023SJohn Marino DECL_CHAIN (p) = BLOCK_VARS (block);
1108e4b17023SJohn Marino BLOCK_VARS (block) = p;
1109e4b17023SJohn Marino gcc_assert (I_LABEL_BINDING (b->id) == b);
1110e4b17023SJohn Marino I_LABEL_BINDING (b->id) = b->shadowed;
1111e4b17023SJohn Marino
1112e4b17023SJohn Marino /* Also pop back to the shadowed label_vars. */
1113e4b17023SJohn Marino release_tree_vector (b->u.label->decls_in_scope);
1114e4b17023SJohn Marino b->u.label = b->u.label->shadowed;
1115e4b17023SJohn Marino break;
1116e4b17023SJohn Marino
1117e4b17023SJohn Marino case ENUMERAL_TYPE:
1118e4b17023SJohn Marino case UNION_TYPE:
1119e4b17023SJohn Marino case RECORD_TYPE:
1120e4b17023SJohn Marino set_type_context (p, context);
1121e4b17023SJohn Marino
1122e4b17023SJohn Marino /* Types may not have tag-names, in which case the type
1123e4b17023SJohn Marino appears in the bindings list with b->id NULL. */
1124e4b17023SJohn Marino if (b->id)
1125e4b17023SJohn Marino {
1126e4b17023SJohn Marino gcc_assert (I_TAG_BINDING (b->id) == b);
1127e4b17023SJohn Marino I_TAG_BINDING (b->id) = b->shadowed;
1128e4b17023SJohn Marino }
1129e4b17023SJohn Marino break;
1130e4b17023SJohn Marino
1131e4b17023SJohn Marino case FUNCTION_DECL:
1132e4b17023SJohn Marino /* Propagate TREE_ADDRESSABLE from nested functions to their
1133e4b17023SJohn Marino containing functions. */
1134e4b17023SJohn Marino if (!TREE_ASM_WRITTEN (p)
1135e4b17023SJohn Marino && DECL_INITIAL (p) != 0
1136e4b17023SJohn Marino && TREE_ADDRESSABLE (p)
1137e4b17023SJohn Marino && DECL_ABSTRACT_ORIGIN (p) != 0
1138e4b17023SJohn Marino && DECL_ABSTRACT_ORIGIN (p) != p)
1139e4b17023SJohn Marino TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
1140e4b17023SJohn Marino if (!DECL_EXTERNAL (p)
1141e4b17023SJohn Marino && !DECL_INITIAL (p)
1142e4b17023SJohn Marino && scope != file_scope
1143e4b17023SJohn Marino && scope != external_scope)
1144e4b17023SJohn Marino {
1145e4b17023SJohn Marino error ("nested function %q+D declared but never defined", p);
1146e4b17023SJohn Marino undef_nested_function = true;
1147e4b17023SJohn Marino }
1148e4b17023SJohn Marino else if (DECL_DECLARED_INLINE_P (p)
1149e4b17023SJohn Marino && TREE_PUBLIC (p)
1150e4b17023SJohn Marino && !DECL_INITIAL (p))
1151e4b17023SJohn Marino {
1152e4b17023SJohn Marino /* C99 6.7.4p6: "a function with external linkage... declared
1153e4b17023SJohn Marino with an inline function specifier ... shall also be defined
1154e4b17023SJohn Marino in the same translation unit." */
1155e4b17023SJohn Marino if (!flag_gnu89_inline)
1156e4b17023SJohn Marino pedwarn (input_location, 0,
1157e4b17023SJohn Marino "inline function %q+D declared but never defined", p);
1158e4b17023SJohn Marino DECL_EXTERNAL (p) = 1;
1159e4b17023SJohn Marino }
1160e4b17023SJohn Marino
1161e4b17023SJohn Marino goto common_symbol;
1162e4b17023SJohn Marino
1163e4b17023SJohn Marino case VAR_DECL:
1164e4b17023SJohn Marino /* Warnings for unused variables. */
1165e4b17023SJohn Marino if ((!TREE_USED (p) || !DECL_READ_P (p))
1166e4b17023SJohn Marino && !TREE_NO_WARNING (p)
1167e4b17023SJohn Marino && !DECL_IN_SYSTEM_HEADER (p)
1168e4b17023SJohn Marino && DECL_NAME (p)
1169e4b17023SJohn Marino && !DECL_ARTIFICIAL (p)
1170e4b17023SJohn Marino && scope != file_scope
1171e4b17023SJohn Marino && scope != external_scope)
1172e4b17023SJohn Marino {
1173e4b17023SJohn Marino if (!TREE_USED (p))
1174e4b17023SJohn Marino warning (OPT_Wunused_variable, "unused variable %q+D", p);
1175e4b17023SJohn Marino else if (DECL_CONTEXT (p) == current_function_decl)
1176e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (p),
1177e4b17023SJohn Marino OPT_Wunused_but_set_variable,
1178e4b17023SJohn Marino "variable %qD set but not used", p);
1179e4b17023SJohn Marino }
1180e4b17023SJohn Marino
1181e4b17023SJohn Marino if (b->inner_comp)
1182e4b17023SJohn Marino {
1183e4b17023SJohn Marino error ("type of array %q+D completed incompatibly with"
1184e4b17023SJohn Marino " implicit initialization", p);
1185e4b17023SJohn Marino }
1186e4b17023SJohn Marino
1187e4b17023SJohn Marino /* Fall through. */
1188e4b17023SJohn Marino case TYPE_DECL:
1189e4b17023SJohn Marino case CONST_DECL:
1190e4b17023SJohn Marino common_symbol:
1191e4b17023SJohn Marino /* All of these go in BLOCK_VARS, but only if this is the
1192e4b17023SJohn Marino binding in the home scope. */
1193e4b17023SJohn Marino if (!b->nested)
1194e4b17023SJohn Marino {
1195e4b17023SJohn Marino DECL_CHAIN (p) = BLOCK_VARS (block);
1196e4b17023SJohn Marino BLOCK_VARS (block) = p;
1197e4b17023SJohn Marino }
1198e4b17023SJohn Marino else if (VAR_OR_FUNCTION_DECL_P (p) && scope != file_scope)
1199e4b17023SJohn Marino {
1200e4b17023SJohn Marino /* For block local externs add a special
1201e4b17023SJohn Marino DECL_EXTERNAL decl for debug info generation. */
1202e4b17023SJohn Marino tree extp = copy_node (p);
1203e4b17023SJohn Marino
1204e4b17023SJohn Marino DECL_EXTERNAL (extp) = 1;
1205e4b17023SJohn Marino TREE_STATIC (extp) = 0;
1206e4b17023SJohn Marino TREE_PUBLIC (extp) = 1;
1207e4b17023SJohn Marino DECL_INITIAL (extp) = NULL_TREE;
1208e4b17023SJohn Marino DECL_LANG_SPECIFIC (extp) = NULL;
1209e4b17023SJohn Marino DECL_CONTEXT (extp) = current_function_decl;
1210e4b17023SJohn Marino if (TREE_CODE (p) == FUNCTION_DECL)
1211e4b17023SJohn Marino {
1212e4b17023SJohn Marino DECL_RESULT (extp) = NULL_TREE;
1213e4b17023SJohn Marino DECL_SAVED_TREE (extp) = NULL_TREE;
1214e4b17023SJohn Marino DECL_STRUCT_FUNCTION (extp) = NULL;
1215e4b17023SJohn Marino }
1216e4b17023SJohn Marino if (b->locus != UNKNOWN_LOCATION)
1217e4b17023SJohn Marino DECL_SOURCE_LOCATION (extp) = b->locus;
1218e4b17023SJohn Marino DECL_CHAIN (extp) = BLOCK_VARS (block);
1219e4b17023SJohn Marino BLOCK_VARS (block) = extp;
1220e4b17023SJohn Marino }
1221e4b17023SJohn Marino /* If this is the file scope set DECL_CONTEXT of each decl to
1222e4b17023SJohn Marino the TRANSLATION_UNIT_DECL. This makes same_translation_unit_p
1223e4b17023SJohn Marino work. */
1224e4b17023SJohn Marino if (scope == file_scope)
1225e4b17023SJohn Marino {
1226e4b17023SJohn Marino DECL_CONTEXT (p) = context;
1227e4b17023SJohn Marino if (TREE_CODE (p) == TYPE_DECL
1228e4b17023SJohn Marino && TREE_TYPE (p) != error_mark_node)
1229e4b17023SJohn Marino set_type_context (TREE_TYPE (p), context);
1230e4b17023SJohn Marino }
1231e4b17023SJohn Marino
1232e4b17023SJohn Marino /* Fall through. */
1233e4b17023SJohn Marino /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
1234e4b17023SJohn Marino already been put there by store_parm_decls. Unused-
1235e4b17023SJohn Marino parameter warnings are handled by function.c.
1236e4b17023SJohn Marino error_mark_node obviously does not go in BLOCK_VARS and
1237e4b17023SJohn Marino does not get unused-variable warnings. */
1238e4b17023SJohn Marino case PARM_DECL:
1239e4b17023SJohn Marino case ERROR_MARK:
1240e4b17023SJohn Marino /* It is possible for a decl not to have a name. We get
1241e4b17023SJohn Marino here with b->id NULL in this case. */
1242e4b17023SJohn Marino if (b->id)
1243e4b17023SJohn Marino {
1244e4b17023SJohn Marino gcc_assert (I_SYMBOL_BINDING (b->id) == b);
1245e4b17023SJohn Marino I_SYMBOL_BINDING (b->id) = b->shadowed;
1246e4b17023SJohn Marino if (b->shadowed && b->shadowed->u.type)
1247e4b17023SJohn Marino TREE_TYPE (b->shadowed->decl) = b->shadowed->u.type;
1248e4b17023SJohn Marino }
1249e4b17023SJohn Marino break;
1250e4b17023SJohn Marino
1251e4b17023SJohn Marino default:
1252e4b17023SJohn Marino gcc_unreachable ();
1253e4b17023SJohn Marino }
1254e4b17023SJohn Marino }
1255e4b17023SJohn Marino
1256e4b17023SJohn Marino
1257e4b17023SJohn Marino /* Dispose of the block that we just made inside some higher level. */
1258e4b17023SJohn Marino if ((scope->function_body || scope == file_scope) && context)
1259e4b17023SJohn Marino {
1260e4b17023SJohn Marino DECL_INITIAL (context) = block;
1261e4b17023SJohn Marino BLOCK_SUPERCONTEXT (block) = context;
1262e4b17023SJohn Marino }
1263e4b17023SJohn Marino else if (scope->outer)
1264e4b17023SJohn Marino {
1265e4b17023SJohn Marino if (block)
1266e4b17023SJohn Marino SCOPE_LIST_APPEND (scope->outer, blocks, block);
1267e4b17023SJohn Marino /* If we did not make a block for the scope just exited, any
1268e4b17023SJohn Marino blocks made for inner scopes must be carried forward so they
1269e4b17023SJohn Marino will later become subblocks of something else. */
1270e4b17023SJohn Marino else if (scope->blocks)
1271e4b17023SJohn Marino SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
1272e4b17023SJohn Marino }
1273e4b17023SJohn Marino
1274e4b17023SJohn Marino /* Pop the current scope, and free the structure for reuse. */
1275e4b17023SJohn Marino current_scope = scope->outer;
1276e4b17023SJohn Marino if (scope->function_body)
1277e4b17023SJohn Marino current_function_scope = scope->outer_function;
1278e4b17023SJohn Marino
1279e4b17023SJohn Marino memset (scope, 0, sizeof (struct c_scope));
1280e4b17023SJohn Marino scope->outer = scope_freelist;
1281e4b17023SJohn Marino scope_freelist = scope;
1282e4b17023SJohn Marino
1283e4b17023SJohn Marino return block;
1284e4b17023SJohn Marino }
1285e4b17023SJohn Marino
1286e4b17023SJohn Marino void
push_file_scope(void)1287e4b17023SJohn Marino push_file_scope (void)
1288e4b17023SJohn Marino {
1289e4b17023SJohn Marino tree decl;
1290e4b17023SJohn Marino
1291e4b17023SJohn Marino if (file_scope)
1292e4b17023SJohn Marino return;
1293e4b17023SJohn Marino
1294e4b17023SJohn Marino push_scope ();
1295e4b17023SJohn Marino file_scope = current_scope;
1296e4b17023SJohn Marino
1297e4b17023SJohn Marino start_fname_decls ();
1298e4b17023SJohn Marino
1299e4b17023SJohn Marino for (decl = visible_builtins; decl; decl = DECL_CHAIN (decl))
1300e4b17023SJohn Marino bind (DECL_NAME (decl), decl, file_scope,
1301e4b17023SJohn Marino /*invisible=*/false, /*nested=*/true, DECL_SOURCE_LOCATION (decl));
1302e4b17023SJohn Marino }
1303e4b17023SJohn Marino
1304e4b17023SJohn Marino void
pop_file_scope(void)1305e4b17023SJohn Marino pop_file_scope (void)
1306e4b17023SJohn Marino {
1307e4b17023SJohn Marino /* In case there were missing closebraces, get us back to the global
1308e4b17023SJohn Marino binding level. */
1309e4b17023SJohn Marino while (current_scope != file_scope)
1310e4b17023SJohn Marino pop_scope ();
1311e4b17023SJohn Marino
1312e4b17023SJohn Marino /* __FUNCTION__ is defined at file scope (""). This
1313e4b17023SJohn Marino call may not be necessary as my tests indicate it
1314e4b17023SJohn Marino still works without it. */
1315e4b17023SJohn Marino finish_fname_decls ();
1316e4b17023SJohn Marino
1317e4b17023SJohn Marino check_inline_statics ();
1318e4b17023SJohn Marino
1319e4b17023SJohn Marino /* This is the point to write out a PCH if we're doing that.
1320e4b17023SJohn Marino In that case we do not want to do anything else. */
1321e4b17023SJohn Marino if (pch_file)
1322e4b17023SJohn Marino {
1323e4b17023SJohn Marino c_common_write_pch ();
1324e4b17023SJohn Marino return;
1325e4b17023SJohn Marino }
1326e4b17023SJohn Marino
1327e4b17023SJohn Marino /* Pop off the file scope and close this translation unit. */
1328e4b17023SJohn Marino pop_scope ();
1329e4b17023SJohn Marino file_scope = 0;
1330e4b17023SJohn Marino
1331e4b17023SJohn Marino maybe_apply_pending_pragma_weaks ();
1332e4b17023SJohn Marino }
1333e4b17023SJohn Marino
1334e4b17023SJohn Marino /* Adjust the bindings for the start of a statement expression. */
1335e4b17023SJohn Marino
1336e4b17023SJohn Marino void
c_bindings_start_stmt_expr(struct c_spot_bindings * switch_bindings)1337e4b17023SJohn Marino c_bindings_start_stmt_expr (struct c_spot_bindings* switch_bindings)
1338e4b17023SJohn Marino {
1339e4b17023SJohn Marino struct c_scope *scope;
1340e4b17023SJohn Marino
1341e4b17023SJohn Marino for (scope = current_scope; scope != NULL; scope = scope->outer)
1342e4b17023SJohn Marino {
1343e4b17023SJohn Marino struct c_binding *b;
1344e4b17023SJohn Marino
1345e4b17023SJohn Marino if (!scope->has_label_bindings)
1346e4b17023SJohn Marino continue;
1347e4b17023SJohn Marino
1348e4b17023SJohn Marino for (b = scope->bindings; b != NULL; b = b->prev)
1349e4b17023SJohn Marino {
1350e4b17023SJohn Marino struct c_label_vars *label_vars;
1351e4b17023SJohn Marino unsigned int ix;
1352e4b17023SJohn Marino struct c_goto_bindings *g;
1353e4b17023SJohn Marino
1354e4b17023SJohn Marino if (TREE_CODE (b->decl) != LABEL_DECL)
1355e4b17023SJohn Marino continue;
1356e4b17023SJohn Marino label_vars = b->u.label;
1357e4b17023SJohn Marino ++label_vars->label_bindings.stmt_exprs;
1358e4b17023SJohn Marino FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1359e4b17023SJohn Marino ++g->goto_bindings.stmt_exprs;
1360e4b17023SJohn Marino }
1361e4b17023SJohn Marino }
1362e4b17023SJohn Marino
1363e4b17023SJohn Marino if (switch_bindings != NULL)
1364e4b17023SJohn Marino ++switch_bindings->stmt_exprs;
1365e4b17023SJohn Marino }
1366e4b17023SJohn Marino
1367e4b17023SJohn Marino /* Adjust the bindings for the end of a statement expression. */
1368e4b17023SJohn Marino
1369e4b17023SJohn Marino void
c_bindings_end_stmt_expr(struct c_spot_bindings * switch_bindings)1370e4b17023SJohn Marino c_bindings_end_stmt_expr (struct c_spot_bindings *switch_bindings)
1371e4b17023SJohn Marino {
1372e4b17023SJohn Marino struct c_scope *scope;
1373e4b17023SJohn Marino
1374e4b17023SJohn Marino for (scope = current_scope; scope != NULL; scope = scope->outer)
1375e4b17023SJohn Marino {
1376e4b17023SJohn Marino struct c_binding *b;
1377e4b17023SJohn Marino
1378e4b17023SJohn Marino if (!scope->has_label_bindings)
1379e4b17023SJohn Marino continue;
1380e4b17023SJohn Marino
1381e4b17023SJohn Marino for (b = scope->bindings; b != NULL; b = b->prev)
1382e4b17023SJohn Marino {
1383e4b17023SJohn Marino struct c_label_vars *label_vars;
1384e4b17023SJohn Marino unsigned int ix;
1385e4b17023SJohn Marino struct c_goto_bindings *g;
1386e4b17023SJohn Marino
1387e4b17023SJohn Marino if (TREE_CODE (b->decl) != LABEL_DECL)
1388e4b17023SJohn Marino continue;
1389e4b17023SJohn Marino label_vars = b->u.label;
1390e4b17023SJohn Marino --label_vars->label_bindings.stmt_exprs;
1391e4b17023SJohn Marino if (label_vars->label_bindings.stmt_exprs < 0)
1392e4b17023SJohn Marino {
1393e4b17023SJohn Marino label_vars->label_bindings.left_stmt_expr = true;
1394e4b17023SJohn Marino label_vars->label_bindings.stmt_exprs = 0;
1395e4b17023SJohn Marino }
1396e4b17023SJohn Marino FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
1397e4b17023SJohn Marino {
1398e4b17023SJohn Marino --g->goto_bindings.stmt_exprs;
1399e4b17023SJohn Marino if (g->goto_bindings.stmt_exprs < 0)
1400e4b17023SJohn Marino {
1401e4b17023SJohn Marino g->goto_bindings.left_stmt_expr = true;
1402e4b17023SJohn Marino g->goto_bindings.stmt_exprs = 0;
1403e4b17023SJohn Marino }
1404e4b17023SJohn Marino }
1405e4b17023SJohn Marino }
1406e4b17023SJohn Marino }
1407e4b17023SJohn Marino
1408e4b17023SJohn Marino if (switch_bindings != NULL)
1409e4b17023SJohn Marino {
1410e4b17023SJohn Marino --switch_bindings->stmt_exprs;
1411e4b17023SJohn Marino gcc_assert (switch_bindings->stmt_exprs >= 0);
1412e4b17023SJohn Marino }
1413e4b17023SJohn Marino }
1414e4b17023SJohn Marino
1415e4b17023SJohn Marino /* Push a definition or a declaration of struct, union or enum tag "name".
1416e4b17023SJohn Marino "type" should be the type node.
1417e4b17023SJohn Marino We assume that the tag "name" is not already defined, and has a location
1418e4b17023SJohn Marino of LOC.
1419e4b17023SJohn Marino
1420e4b17023SJohn Marino Note that the definition may really be just a forward reference.
1421e4b17023SJohn Marino In that case, the TYPE_SIZE will be zero. */
1422e4b17023SJohn Marino
1423e4b17023SJohn Marino static void
pushtag(location_t loc,tree name,tree type)1424e4b17023SJohn Marino pushtag (location_t loc, tree name, tree type)
1425e4b17023SJohn Marino {
1426e4b17023SJohn Marino /* Record the identifier as the type's name if it has none. */
1427e4b17023SJohn Marino if (name && !TYPE_NAME (type))
1428e4b17023SJohn Marino TYPE_NAME (type) = name;
1429e4b17023SJohn Marino bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false, loc);
1430e4b17023SJohn Marino
1431e4b17023SJohn Marino /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
1432e4b17023SJohn Marino tagged type we just added to the current scope. This fake
1433e4b17023SJohn Marino NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
1434e4b17023SJohn Marino to output a representation of a tagged type, and it also gives
1435e4b17023SJohn Marino us a convenient place to record the "scope start" address for the
1436e4b17023SJohn Marino tagged type. */
1437e4b17023SJohn Marino
1438e4b17023SJohn Marino TYPE_STUB_DECL (type) = pushdecl (build_decl (loc,
1439e4b17023SJohn Marino TYPE_DECL, NULL_TREE, type));
1440e4b17023SJohn Marino
1441e4b17023SJohn Marino /* An approximation for now, so we can tell this is a function-scope tag.
1442e4b17023SJohn Marino This will be updated in pop_scope. */
1443e4b17023SJohn Marino TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
1444e4b17023SJohn Marino
1445e4b17023SJohn Marino if (warn_cxx_compat && name != NULL_TREE)
1446e4b17023SJohn Marino {
1447e4b17023SJohn Marino struct c_binding *b = I_SYMBOL_BINDING (name);
1448e4b17023SJohn Marino
1449e4b17023SJohn Marino if (b != NULL
1450e4b17023SJohn Marino && b->decl != NULL_TREE
1451e4b17023SJohn Marino && TREE_CODE (b->decl) == TYPE_DECL
1452e4b17023SJohn Marino && (B_IN_CURRENT_SCOPE (b)
1453e4b17023SJohn Marino || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
1454e4b17023SJohn Marino && (TYPE_MAIN_VARIANT (TREE_TYPE (b->decl))
1455e4b17023SJohn Marino != TYPE_MAIN_VARIANT (type)))
1456e4b17023SJohn Marino {
1457e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
1458e4b17023SJohn Marino ("using %qD as both a typedef and a tag is "
1459e4b17023SJohn Marino "invalid in C++"),
1460e4b17023SJohn Marino b->decl);
1461e4b17023SJohn Marino if (b->locus != UNKNOWN_LOCATION)
1462e4b17023SJohn Marino inform (b->locus, "originally defined here");
1463e4b17023SJohn Marino }
1464e4b17023SJohn Marino }
1465e4b17023SJohn Marino }
1466e4b17023SJohn Marino
1467e4b17023SJohn Marino /* Subroutine of compare_decls. Allow harmless mismatches in return
1468e4b17023SJohn Marino and argument types provided that the type modes match. This function
1469e4b17023SJohn Marino return a unified type given a suitable match, and 0 otherwise. */
1470e4b17023SJohn Marino
1471e4b17023SJohn Marino static tree
match_builtin_function_types(tree newtype,tree oldtype)1472e4b17023SJohn Marino match_builtin_function_types (tree newtype, tree oldtype)
1473e4b17023SJohn Marino {
1474e4b17023SJohn Marino tree newrettype, oldrettype;
1475e4b17023SJohn Marino tree newargs, oldargs;
1476e4b17023SJohn Marino tree trytype, tryargs;
1477e4b17023SJohn Marino
1478e4b17023SJohn Marino /* Accept the return type of the new declaration if same modes. */
1479e4b17023SJohn Marino oldrettype = TREE_TYPE (oldtype);
1480e4b17023SJohn Marino newrettype = TREE_TYPE (newtype);
1481e4b17023SJohn Marino
1482e4b17023SJohn Marino if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
1483e4b17023SJohn Marino return 0;
1484e4b17023SJohn Marino
1485e4b17023SJohn Marino oldargs = TYPE_ARG_TYPES (oldtype);
1486e4b17023SJohn Marino newargs = TYPE_ARG_TYPES (newtype);
1487e4b17023SJohn Marino tryargs = newargs;
1488e4b17023SJohn Marino
1489e4b17023SJohn Marino while (oldargs || newargs)
1490e4b17023SJohn Marino {
1491e4b17023SJohn Marino if (!oldargs
1492e4b17023SJohn Marino || !newargs
1493e4b17023SJohn Marino || !TREE_VALUE (oldargs)
1494e4b17023SJohn Marino || !TREE_VALUE (newargs)
1495e4b17023SJohn Marino || TYPE_MODE (TREE_VALUE (oldargs))
1496e4b17023SJohn Marino != TYPE_MODE (TREE_VALUE (newargs)))
1497e4b17023SJohn Marino return 0;
1498e4b17023SJohn Marino
1499e4b17023SJohn Marino oldargs = TREE_CHAIN (oldargs);
1500e4b17023SJohn Marino newargs = TREE_CHAIN (newargs);
1501e4b17023SJohn Marino }
1502e4b17023SJohn Marino
1503e4b17023SJohn Marino trytype = build_function_type (newrettype, tryargs);
1504e4b17023SJohn Marino return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1505e4b17023SJohn Marino }
1506e4b17023SJohn Marino
1507e4b17023SJohn Marino /* Subroutine of diagnose_mismatched_decls. Check for function type
1508e4b17023SJohn Marino mismatch involving an empty arglist vs a nonempty one and give clearer
1509e4b17023SJohn Marino diagnostics. */
1510e4b17023SJohn Marino static void
diagnose_arglist_conflict(tree newdecl,tree olddecl,tree newtype,tree oldtype)1511e4b17023SJohn Marino diagnose_arglist_conflict (tree newdecl, tree olddecl,
1512e4b17023SJohn Marino tree newtype, tree oldtype)
1513e4b17023SJohn Marino {
1514e4b17023SJohn Marino tree t;
1515e4b17023SJohn Marino
1516e4b17023SJohn Marino if (TREE_CODE (olddecl) != FUNCTION_DECL
1517e4b17023SJohn Marino || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1518e4b17023SJohn Marino || !((!prototype_p (oldtype) && DECL_INITIAL (olddecl) == 0)
1519e4b17023SJohn Marino || (!prototype_p (newtype) && DECL_INITIAL (newdecl) == 0)))
1520e4b17023SJohn Marino return;
1521e4b17023SJohn Marino
1522e4b17023SJohn Marino t = TYPE_ARG_TYPES (oldtype);
1523e4b17023SJohn Marino if (t == 0)
1524e4b17023SJohn Marino t = TYPE_ARG_TYPES (newtype);
1525e4b17023SJohn Marino for (; t; t = TREE_CHAIN (t))
1526e4b17023SJohn Marino {
1527e4b17023SJohn Marino tree type = TREE_VALUE (t);
1528e4b17023SJohn Marino
1529e4b17023SJohn Marino if (TREE_CHAIN (t) == 0
1530e4b17023SJohn Marino && TYPE_MAIN_VARIANT (type) != void_type_node)
1531e4b17023SJohn Marino {
1532e4b17023SJohn Marino inform (input_location, "a parameter list with an ellipsis can%'t match "
1533e4b17023SJohn Marino "an empty parameter name list declaration");
1534e4b17023SJohn Marino break;
1535e4b17023SJohn Marino }
1536e4b17023SJohn Marino
1537e4b17023SJohn Marino if (c_type_promotes_to (type) != type)
1538e4b17023SJohn Marino {
1539e4b17023SJohn Marino inform (input_location, "an argument type that has a default promotion can%'t match "
1540e4b17023SJohn Marino "an empty parameter name list declaration");
1541e4b17023SJohn Marino break;
1542e4b17023SJohn Marino }
1543e4b17023SJohn Marino }
1544e4b17023SJohn Marino }
1545e4b17023SJohn Marino
1546e4b17023SJohn Marino /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1547e4b17023SJohn Marino old-style function definition, NEWDECL is a prototype declaration.
1548e4b17023SJohn Marino Diagnose inconsistencies in the argument list. Returns TRUE if
1549e4b17023SJohn Marino the prototype is compatible, FALSE if not. */
1550e4b17023SJohn Marino static bool
validate_proto_after_old_defn(tree newdecl,tree newtype,tree oldtype)1551e4b17023SJohn Marino validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1552e4b17023SJohn Marino {
1553e4b17023SJohn Marino tree newargs, oldargs;
1554e4b17023SJohn Marino int i;
1555e4b17023SJohn Marino
1556e4b17023SJohn Marino #define END_OF_ARGLIST(t) ((t) == void_type_node)
1557e4b17023SJohn Marino
1558e4b17023SJohn Marino oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1559e4b17023SJohn Marino newargs = TYPE_ARG_TYPES (newtype);
1560e4b17023SJohn Marino i = 1;
1561e4b17023SJohn Marino
1562e4b17023SJohn Marino for (;;)
1563e4b17023SJohn Marino {
1564e4b17023SJohn Marino tree oldargtype = TREE_VALUE (oldargs);
1565e4b17023SJohn Marino tree newargtype = TREE_VALUE (newargs);
1566e4b17023SJohn Marino
1567e4b17023SJohn Marino if (oldargtype == error_mark_node || newargtype == error_mark_node)
1568e4b17023SJohn Marino return false;
1569e4b17023SJohn Marino
1570e4b17023SJohn Marino oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1571e4b17023SJohn Marino newargtype = TYPE_MAIN_VARIANT (newargtype);
1572e4b17023SJohn Marino
1573e4b17023SJohn Marino if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1574e4b17023SJohn Marino break;
1575e4b17023SJohn Marino
1576e4b17023SJohn Marino /* Reaching the end of just one list means the two decls don't
1577e4b17023SJohn Marino agree on the number of arguments. */
1578e4b17023SJohn Marino if (END_OF_ARGLIST (oldargtype))
1579e4b17023SJohn Marino {
1580e4b17023SJohn Marino error ("prototype for %q+D declares more arguments "
1581e4b17023SJohn Marino "than previous old-style definition", newdecl);
1582e4b17023SJohn Marino return false;
1583e4b17023SJohn Marino }
1584e4b17023SJohn Marino else if (END_OF_ARGLIST (newargtype))
1585e4b17023SJohn Marino {
1586e4b17023SJohn Marino error ("prototype for %q+D declares fewer arguments "
1587e4b17023SJohn Marino "than previous old-style definition", newdecl);
1588e4b17023SJohn Marino return false;
1589e4b17023SJohn Marino }
1590e4b17023SJohn Marino
1591e4b17023SJohn Marino /* Type for passing arg must be consistent with that declared
1592e4b17023SJohn Marino for the arg. */
1593e4b17023SJohn Marino else if (!comptypes (oldargtype, newargtype))
1594e4b17023SJohn Marino {
1595e4b17023SJohn Marino error ("prototype for %q+D declares argument %d"
1596e4b17023SJohn Marino " with incompatible type",
1597e4b17023SJohn Marino newdecl, i);
1598e4b17023SJohn Marino return false;
1599e4b17023SJohn Marino }
1600e4b17023SJohn Marino
1601e4b17023SJohn Marino oldargs = TREE_CHAIN (oldargs);
1602e4b17023SJohn Marino newargs = TREE_CHAIN (newargs);
1603e4b17023SJohn Marino i++;
1604e4b17023SJohn Marino }
1605e4b17023SJohn Marino
1606e4b17023SJohn Marino /* If we get here, no errors were found, but do issue a warning
1607e4b17023SJohn Marino for this poor-style construct. */
1608e4b17023SJohn Marino warning (0, "prototype for %q+D follows non-prototype definition",
1609e4b17023SJohn Marino newdecl);
1610e4b17023SJohn Marino return true;
1611e4b17023SJohn Marino #undef END_OF_ARGLIST
1612e4b17023SJohn Marino }
1613e4b17023SJohn Marino
1614e4b17023SJohn Marino /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1615e4b17023SJohn Marino first in a pair of mismatched declarations, using the diagnostic
1616e4b17023SJohn Marino function DIAG. */
1617e4b17023SJohn Marino static void
locate_old_decl(tree decl)1618e4b17023SJohn Marino locate_old_decl (tree decl)
1619e4b17023SJohn Marino {
1620e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1621e4b17023SJohn Marino ;
1622e4b17023SJohn Marino else if (DECL_INITIAL (decl))
1623e4b17023SJohn Marino inform (input_location, "previous definition of %q+D was here", decl);
1624e4b17023SJohn Marino else if (C_DECL_IMPLICIT (decl))
1625e4b17023SJohn Marino inform (input_location, "previous implicit declaration of %q+D was here", decl);
1626e4b17023SJohn Marino else
1627e4b17023SJohn Marino inform (input_location, "previous declaration of %q+D was here", decl);
1628e4b17023SJohn Marino }
1629e4b17023SJohn Marino
1630e4b17023SJohn Marino /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1631e4b17023SJohn Marino Returns true if the caller should proceed to merge the two, false
1632e4b17023SJohn Marino if OLDDECL should simply be discarded. As a side effect, issues
1633e4b17023SJohn Marino all necessary diagnostics for invalid or poor-style combinations.
1634e4b17023SJohn Marino If it returns true, writes the types of NEWDECL and OLDDECL to
1635e4b17023SJohn Marino *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1636e4b17023SJohn Marino TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1637e4b17023SJohn Marino
1638e4b17023SJohn Marino static bool
diagnose_mismatched_decls(tree newdecl,tree olddecl,tree * newtypep,tree * oldtypep)1639e4b17023SJohn Marino diagnose_mismatched_decls (tree newdecl, tree olddecl,
1640e4b17023SJohn Marino tree *newtypep, tree *oldtypep)
1641e4b17023SJohn Marino {
1642e4b17023SJohn Marino tree newtype, oldtype;
1643e4b17023SJohn Marino bool pedwarned = false;
1644e4b17023SJohn Marino bool warned = false;
1645e4b17023SJohn Marino bool retval = true;
1646e4b17023SJohn Marino
1647e4b17023SJohn Marino #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1648e4b17023SJohn Marino && DECL_EXTERNAL (DECL))
1649e4b17023SJohn Marino
1650e4b17023SJohn Marino /* If we have error_mark_node for either decl or type, just discard
1651e4b17023SJohn Marino the previous decl - we're in an error cascade already. */
1652e4b17023SJohn Marino if (olddecl == error_mark_node || newdecl == error_mark_node)
1653e4b17023SJohn Marino return false;
1654e4b17023SJohn Marino *oldtypep = oldtype = TREE_TYPE (olddecl);
1655e4b17023SJohn Marino *newtypep = newtype = TREE_TYPE (newdecl);
1656e4b17023SJohn Marino if (oldtype == error_mark_node || newtype == error_mark_node)
1657e4b17023SJohn Marino return false;
1658e4b17023SJohn Marino
1659e4b17023SJohn Marino /* Two different categories of symbol altogether. This is an error
1660e4b17023SJohn Marino unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1661e4b17023SJohn Marino if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1662e4b17023SJohn Marino {
1663e4b17023SJohn Marino if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1664e4b17023SJohn Marino && DECL_BUILT_IN (olddecl)
1665e4b17023SJohn Marino && !C_DECL_DECLARED_BUILTIN (olddecl)))
1666e4b17023SJohn Marino {
1667e4b17023SJohn Marino error ("%q+D redeclared as different kind of symbol", newdecl);
1668e4b17023SJohn Marino locate_old_decl (olddecl);
1669e4b17023SJohn Marino }
1670e4b17023SJohn Marino else if (TREE_PUBLIC (newdecl))
1671e4b17023SJohn Marino warning (0, "built-in function %q+D declared as non-function",
1672e4b17023SJohn Marino newdecl);
1673e4b17023SJohn Marino else
1674e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows "
1675e4b17023SJohn Marino "a built-in function", newdecl);
1676e4b17023SJohn Marino return false;
1677e4b17023SJohn Marino }
1678e4b17023SJohn Marino
1679e4b17023SJohn Marino /* Enumerators have no linkage, so may only be declared once in a
1680e4b17023SJohn Marino given scope. */
1681e4b17023SJohn Marino if (TREE_CODE (olddecl) == CONST_DECL)
1682e4b17023SJohn Marino {
1683e4b17023SJohn Marino error ("redeclaration of enumerator %q+D", newdecl);
1684e4b17023SJohn Marino locate_old_decl (olddecl);
1685e4b17023SJohn Marino return false;
1686e4b17023SJohn Marino }
1687e4b17023SJohn Marino
1688e4b17023SJohn Marino if (!comptypes (oldtype, newtype))
1689e4b17023SJohn Marino {
1690e4b17023SJohn Marino if (TREE_CODE (olddecl) == FUNCTION_DECL
1691e4b17023SJohn Marino && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1692e4b17023SJohn Marino {
1693e4b17023SJohn Marino /* Accept harmless mismatch in function types.
1694e4b17023SJohn Marino This is for the ffs and fprintf builtins. */
1695e4b17023SJohn Marino tree trytype = match_builtin_function_types (newtype, oldtype);
1696e4b17023SJohn Marino
1697e4b17023SJohn Marino if (trytype && comptypes (newtype, trytype))
1698e4b17023SJohn Marino *oldtypep = oldtype = trytype;
1699e4b17023SJohn Marino else
1700e4b17023SJohn Marino {
1701e4b17023SJohn Marino /* If types don't match for a built-in, throw away the
1702e4b17023SJohn Marino built-in. No point in calling locate_old_decl here, it
1703e4b17023SJohn Marino won't print anything. */
1704e4b17023SJohn Marino warning (0, "conflicting types for built-in function %q+D",
1705e4b17023SJohn Marino newdecl);
1706e4b17023SJohn Marino return false;
1707e4b17023SJohn Marino }
1708e4b17023SJohn Marino }
1709e4b17023SJohn Marino else if (TREE_CODE (olddecl) == FUNCTION_DECL
1710e4b17023SJohn Marino && DECL_IS_BUILTIN (olddecl))
1711e4b17023SJohn Marino {
1712e4b17023SJohn Marino /* A conflicting function declaration for a predeclared
1713e4b17023SJohn Marino function that isn't actually built in. Objective C uses
1714e4b17023SJohn Marino these. The new declaration silently overrides everything
1715e4b17023SJohn Marino but the volatility (i.e. noreturn) indication. See also
1716e4b17023SJohn Marino below. FIXME: Make Objective C use normal builtins. */
1717e4b17023SJohn Marino TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1718e4b17023SJohn Marino return false;
1719e4b17023SJohn Marino }
1720e4b17023SJohn Marino /* Permit void foo (...) to match int foo (...) if the latter is
1721e4b17023SJohn Marino the definition and implicit int was used. See
1722e4b17023SJohn Marino c-torture/compile/920625-2.c. */
1723e4b17023SJohn Marino else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1724e4b17023SJohn Marino && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1725e4b17023SJohn Marino && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1726e4b17023SJohn Marino && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1727e4b17023SJohn Marino {
1728e4b17023SJohn Marino pedwarned = pedwarn (input_location, 0,
1729e4b17023SJohn Marino "conflicting types for %q+D", newdecl);
1730e4b17023SJohn Marino /* Make sure we keep void as the return type. */
1731e4b17023SJohn Marino TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1732e4b17023SJohn Marino C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1733e4b17023SJohn Marino }
1734e4b17023SJohn Marino /* Permit void foo (...) to match an earlier call to foo (...) with
1735e4b17023SJohn Marino no declared type (thus, implicitly int). */
1736e4b17023SJohn Marino else if (TREE_CODE (newdecl) == FUNCTION_DECL
1737e4b17023SJohn Marino && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1738e4b17023SJohn Marino && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1739e4b17023SJohn Marino && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1740e4b17023SJohn Marino {
1741e4b17023SJohn Marino pedwarned = pedwarn (input_location, 0,
1742e4b17023SJohn Marino "conflicting types for %q+D", newdecl);
1743e4b17023SJohn Marino /* Make sure we keep void as the return type. */
1744e4b17023SJohn Marino TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1745e4b17023SJohn Marino }
1746e4b17023SJohn Marino else
1747e4b17023SJohn Marino {
1748e4b17023SJohn Marino int new_quals = TYPE_QUALS (newtype);
1749e4b17023SJohn Marino int old_quals = TYPE_QUALS (oldtype);
1750e4b17023SJohn Marino
1751e4b17023SJohn Marino if (new_quals != old_quals)
1752e4b17023SJohn Marino {
1753e4b17023SJohn Marino addr_space_t new_addr = DECODE_QUAL_ADDR_SPACE (new_quals);
1754e4b17023SJohn Marino addr_space_t old_addr = DECODE_QUAL_ADDR_SPACE (old_quals);
1755e4b17023SJohn Marino if (new_addr != old_addr)
1756e4b17023SJohn Marino {
1757e4b17023SJohn Marino if (ADDR_SPACE_GENERIC_P (new_addr))
1758e4b17023SJohn Marino error ("conflicting named address spaces (generic vs %s) "
1759e4b17023SJohn Marino "for %q+D",
1760e4b17023SJohn Marino c_addr_space_name (old_addr), newdecl);
1761e4b17023SJohn Marino else if (ADDR_SPACE_GENERIC_P (old_addr))
1762e4b17023SJohn Marino error ("conflicting named address spaces (%s vs generic) "
1763e4b17023SJohn Marino "for %q+D",
1764e4b17023SJohn Marino c_addr_space_name (new_addr), newdecl);
1765e4b17023SJohn Marino else
1766e4b17023SJohn Marino error ("conflicting named address spaces (%s vs %s) "
1767e4b17023SJohn Marino "for %q+D",
1768e4b17023SJohn Marino c_addr_space_name (new_addr),
1769e4b17023SJohn Marino c_addr_space_name (old_addr),
1770e4b17023SJohn Marino newdecl);
1771e4b17023SJohn Marino }
1772e4b17023SJohn Marino
1773e4b17023SJohn Marino if (CLEAR_QUAL_ADDR_SPACE (new_quals)
1774e4b17023SJohn Marino != CLEAR_QUAL_ADDR_SPACE (old_quals))
1775e4b17023SJohn Marino error ("conflicting type qualifiers for %q+D", newdecl);
1776e4b17023SJohn Marino }
1777e4b17023SJohn Marino else
1778e4b17023SJohn Marino error ("conflicting types for %q+D", newdecl);
1779e4b17023SJohn Marino diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1780e4b17023SJohn Marino locate_old_decl (olddecl);
1781e4b17023SJohn Marino return false;
1782e4b17023SJohn Marino }
1783e4b17023SJohn Marino }
1784e4b17023SJohn Marino
1785e4b17023SJohn Marino /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1786e4b17023SJohn Marino but silently ignore the redeclaration if either is in a system
1787e4b17023SJohn Marino header. (Conflicting redeclarations were handled above.) This
1788e4b17023SJohn Marino is allowed for C11 if the types are the same, not just
1789e4b17023SJohn Marino compatible. */
1790e4b17023SJohn Marino if (TREE_CODE (newdecl) == TYPE_DECL)
1791e4b17023SJohn Marino {
1792e4b17023SJohn Marino bool types_different = false;
1793e4b17023SJohn Marino int comptypes_result;
1794e4b17023SJohn Marino
1795e4b17023SJohn Marino comptypes_result
1796e4b17023SJohn Marino = comptypes_check_different_types (oldtype, newtype, &types_different);
1797e4b17023SJohn Marino
1798e4b17023SJohn Marino if (comptypes_result != 1 || types_different)
1799e4b17023SJohn Marino {
1800e4b17023SJohn Marino error ("redefinition of typedef %q+D with different type", newdecl);
1801e4b17023SJohn Marino locate_old_decl (olddecl);
1802e4b17023SJohn Marino return false;
1803e4b17023SJohn Marino }
1804e4b17023SJohn Marino
1805e4b17023SJohn Marino if (DECL_IN_SYSTEM_HEADER (newdecl)
1806e4b17023SJohn Marino || DECL_IN_SYSTEM_HEADER (olddecl)
1807e4b17023SJohn Marino || TREE_NO_WARNING (newdecl)
1808e4b17023SJohn Marino || TREE_NO_WARNING (olddecl))
1809e4b17023SJohn Marino return true; /* Allow OLDDECL to continue in use. */
1810e4b17023SJohn Marino
1811e4b17023SJohn Marino if (variably_modified_type_p (newtype, NULL))
1812e4b17023SJohn Marino {
1813e4b17023SJohn Marino error ("redefinition of typedef %q+D with variably modified type",
1814e4b17023SJohn Marino newdecl);
1815e4b17023SJohn Marino locate_old_decl (olddecl);
1816e4b17023SJohn Marino }
1817e4b17023SJohn Marino else if (pedantic && !flag_isoc11)
1818e4b17023SJohn Marino {
1819e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
1820e4b17023SJohn Marino "redefinition of typedef %q+D", newdecl);
1821e4b17023SJohn Marino locate_old_decl (olddecl);
1822e4b17023SJohn Marino }
1823e4b17023SJohn Marino
1824e4b17023SJohn Marino return true;
1825e4b17023SJohn Marino }
1826e4b17023SJohn Marino
1827e4b17023SJohn Marino /* Function declarations can either be 'static' or 'extern' (no
1828e4b17023SJohn Marino qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1829e4b17023SJohn Marino can never conflict with each other on account of linkage
1830e4b17023SJohn Marino (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1831e4b17023SJohn Marino gnu89 mode permits two definitions if one is 'extern inline' and
1832e4b17023SJohn Marino one is not. The non- extern-inline definition supersedes the
1833e4b17023SJohn Marino extern-inline definition. */
1834e4b17023SJohn Marino
1835e4b17023SJohn Marino else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1836e4b17023SJohn Marino {
1837e4b17023SJohn Marino /* If you declare a built-in function name as static, or
1838e4b17023SJohn Marino define the built-in with an old-style definition (so we
1839e4b17023SJohn Marino can't validate the argument list) the built-in definition is
1840e4b17023SJohn Marino overridden, but optionally warn this was a bad choice of name. */
1841e4b17023SJohn Marino if (DECL_BUILT_IN (olddecl)
1842e4b17023SJohn Marino && !C_DECL_DECLARED_BUILTIN (olddecl)
1843e4b17023SJohn Marino && (!TREE_PUBLIC (newdecl)
1844e4b17023SJohn Marino || (DECL_INITIAL (newdecl)
1845e4b17023SJohn Marino && !prototype_p (TREE_TYPE (newdecl)))))
1846e4b17023SJohn Marino {
1847e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows "
1848e4b17023SJohn Marino "a built-in function", newdecl);
1849e4b17023SJohn Marino /* Discard the old built-in function. */
1850e4b17023SJohn Marino return false;
1851e4b17023SJohn Marino }
1852e4b17023SJohn Marino
1853e4b17023SJohn Marino if (DECL_INITIAL (newdecl))
1854e4b17023SJohn Marino {
1855e4b17023SJohn Marino if (DECL_INITIAL (olddecl))
1856e4b17023SJohn Marino {
1857e4b17023SJohn Marino /* If both decls are in the same TU and the new declaration
1858e4b17023SJohn Marino isn't overriding an extern inline reject the new decl.
1859e4b17023SJohn Marino In c99, no overriding is allowed in the same translation
1860e4b17023SJohn Marino unit. */
1861e4b17023SJohn Marino if ((!DECL_EXTERN_INLINE (olddecl)
1862e4b17023SJohn Marino || DECL_EXTERN_INLINE (newdecl)
1863e4b17023SJohn Marino || (!flag_gnu89_inline
1864e4b17023SJohn Marino && (!DECL_DECLARED_INLINE_P (olddecl)
1865e4b17023SJohn Marino || !lookup_attribute ("gnu_inline",
1866e4b17023SJohn Marino DECL_ATTRIBUTES (olddecl)))
1867e4b17023SJohn Marino && (!DECL_DECLARED_INLINE_P (newdecl)
1868e4b17023SJohn Marino || !lookup_attribute ("gnu_inline",
1869e4b17023SJohn Marino DECL_ATTRIBUTES (newdecl))))
1870e4b17023SJohn Marino )
1871e4b17023SJohn Marino && same_translation_unit_p (newdecl, olddecl))
1872e4b17023SJohn Marino {
1873e4b17023SJohn Marino error ("redefinition of %q+D", newdecl);
1874e4b17023SJohn Marino locate_old_decl (olddecl);
1875e4b17023SJohn Marino return false;
1876e4b17023SJohn Marino }
1877e4b17023SJohn Marino }
1878e4b17023SJohn Marino }
1879e4b17023SJohn Marino /* If we have a prototype after an old-style function definition,
1880e4b17023SJohn Marino the argument types must be checked specially. */
1881e4b17023SJohn Marino else if (DECL_INITIAL (olddecl)
1882e4b17023SJohn Marino && !prototype_p (oldtype) && prototype_p (newtype)
1883e4b17023SJohn Marino && TYPE_ACTUAL_ARG_TYPES (oldtype)
1884e4b17023SJohn Marino && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1885e4b17023SJohn Marino {
1886e4b17023SJohn Marino locate_old_decl (olddecl);
1887e4b17023SJohn Marino return false;
1888e4b17023SJohn Marino }
1889e4b17023SJohn Marino /* A non-static declaration (even an "extern") followed by a
1890e4b17023SJohn Marino static declaration is undefined behavior per C99 6.2.2p3-5,7.
1891e4b17023SJohn Marino The same is true for a static forward declaration at block
1892e4b17023SJohn Marino scope followed by a non-static declaration/definition at file
1893e4b17023SJohn Marino scope. Static followed by non-static at the same scope is
1894e4b17023SJohn Marino not undefined behavior, and is the most convenient way to get
1895e4b17023SJohn Marino some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1896e4b17023SJohn Marino the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1897e4b17023SJohn Marino we do diagnose it if -Wtraditional. */
1898e4b17023SJohn Marino if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1899e4b17023SJohn Marino {
1900e4b17023SJohn Marino /* Two exceptions to the rule. If olddecl is an extern
1901e4b17023SJohn Marino inline, or a predeclared function that isn't actually
1902e4b17023SJohn Marino built in, newdecl silently overrides olddecl. The latter
1903e4b17023SJohn Marino occur only in Objective C; see also above. (FIXME: Make
1904e4b17023SJohn Marino Objective C use normal builtins.) */
1905e4b17023SJohn Marino if (!DECL_IS_BUILTIN (olddecl)
1906e4b17023SJohn Marino && !DECL_EXTERN_INLINE (olddecl))
1907e4b17023SJohn Marino {
1908e4b17023SJohn Marino error ("static declaration of %q+D follows "
1909e4b17023SJohn Marino "non-static declaration", newdecl);
1910e4b17023SJohn Marino locate_old_decl (olddecl);
1911e4b17023SJohn Marino }
1912e4b17023SJohn Marino return false;
1913e4b17023SJohn Marino }
1914e4b17023SJohn Marino else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1915e4b17023SJohn Marino {
1916e4b17023SJohn Marino if (DECL_CONTEXT (olddecl))
1917e4b17023SJohn Marino {
1918e4b17023SJohn Marino error ("non-static declaration of %q+D follows "
1919e4b17023SJohn Marino "static declaration", newdecl);
1920e4b17023SJohn Marino locate_old_decl (olddecl);
1921e4b17023SJohn Marino return false;
1922e4b17023SJohn Marino }
1923e4b17023SJohn Marino else if (warn_traditional)
1924e4b17023SJohn Marino {
1925e4b17023SJohn Marino warned |= warning (OPT_Wtraditional,
1926e4b17023SJohn Marino "non-static declaration of %q+D "
1927e4b17023SJohn Marino "follows static declaration", newdecl);
1928e4b17023SJohn Marino }
1929e4b17023SJohn Marino }
1930e4b17023SJohn Marino
1931e4b17023SJohn Marino /* Make sure gnu_inline attribute is either not present, or
1932e4b17023SJohn Marino present on all inline decls. */
1933e4b17023SJohn Marino if (DECL_DECLARED_INLINE_P (olddecl)
1934e4b17023SJohn Marino && DECL_DECLARED_INLINE_P (newdecl))
1935e4b17023SJohn Marino {
1936e4b17023SJohn Marino bool newa = lookup_attribute ("gnu_inline",
1937e4b17023SJohn Marino DECL_ATTRIBUTES (newdecl)) != NULL;
1938e4b17023SJohn Marino bool olda = lookup_attribute ("gnu_inline",
1939e4b17023SJohn Marino DECL_ATTRIBUTES (olddecl)) != NULL;
1940e4b17023SJohn Marino if (newa != olda)
1941e4b17023SJohn Marino {
1942e4b17023SJohn Marino error_at (input_location, "%<gnu_inline%> attribute present on %q+D",
1943e4b17023SJohn Marino newa ? newdecl : olddecl);
1944e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (newa ? olddecl : newdecl),
1945e4b17023SJohn Marino "but not here");
1946e4b17023SJohn Marino }
1947e4b17023SJohn Marino }
1948e4b17023SJohn Marino }
1949e4b17023SJohn Marino else if (TREE_CODE (newdecl) == VAR_DECL)
1950e4b17023SJohn Marino {
1951e4b17023SJohn Marino /* Only variables can be thread-local, and all declarations must
1952e4b17023SJohn Marino agree on this property. */
1953e4b17023SJohn Marino if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1954e4b17023SJohn Marino {
1955e4b17023SJohn Marino /* Nothing to check. Since OLDDECL is marked threadprivate
1956e4b17023SJohn Marino and NEWDECL does not have a thread-local attribute, we
1957e4b17023SJohn Marino will merge the threadprivate attribute into NEWDECL. */
1958e4b17023SJohn Marino ;
1959e4b17023SJohn Marino }
1960e4b17023SJohn Marino else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1961e4b17023SJohn Marino {
1962e4b17023SJohn Marino if (DECL_THREAD_LOCAL_P (newdecl))
1963e4b17023SJohn Marino error ("thread-local declaration of %q+D follows "
1964e4b17023SJohn Marino "non-thread-local declaration", newdecl);
1965e4b17023SJohn Marino else
1966e4b17023SJohn Marino error ("non-thread-local declaration of %q+D follows "
1967e4b17023SJohn Marino "thread-local declaration", newdecl);
1968e4b17023SJohn Marino
1969e4b17023SJohn Marino locate_old_decl (olddecl);
1970e4b17023SJohn Marino return false;
1971e4b17023SJohn Marino }
1972e4b17023SJohn Marino
1973e4b17023SJohn Marino /* Multiple initialized definitions are not allowed (6.9p3,5). */
1974e4b17023SJohn Marino if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1975e4b17023SJohn Marino {
1976e4b17023SJohn Marino error ("redefinition of %q+D", newdecl);
1977e4b17023SJohn Marino locate_old_decl (olddecl);
1978e4b17023SJohn Marino return false;
1979e4b17023SJohn Marino }
1980e4b17023SJohn Marino
1981e4b17023SJohn Marino /* Objects declared at file scope: if the first declaration had
1982e4b17023SJohn Marino external linkage (even if it was an external reference) the
1983e4b17023SJohn Marino second must have external linkage as well, or the behavior is
1984e4b17023SJohn Marino undefined. If the first declaration had internal linkage, then
1985e4b17023SJohn Marino the second must too, or else be an external reference (in which
1986e4b17023SJohn Marino case the composite declaration still has internal linkage).
1987e4b17023SJohn Marino As for function declarations, we warn about the static-then-
1988e4b17023SJohn Marino extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1989e4b17023SJohn Marino if (DECL_FILE_SCOPE_P (newdecl)
1990e4b17023SJohn Marino && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1991e4b17023SJohn Marino {
1992e4b17023SJohn Marino if (DECL_EXTERNAL (newdecl))
1993e4b17023SJohn Marino {
1994e4b17023SJohn Marino if (!DECL_FILE_SCOPE_P (olddecl))
1995e4b17023SJohn Marino {
1996e4b17023SJohn Marino error ("extern declaration of %q+D follows "
1997e4b17023SJohn Marino "declaration with no linkage", newdecl);
1998e4b17023SJohn Marino locate_old_decl (olddecl);
1999e4b17023SJohn Marino return false;
2000e4b17023SJohn Marino }
2001e4b17023SJohn Marino else if (warn_traditional)
2002e4b17023SJohn Marino {
2003e4b17023SJohn Marino warned |= warning (OPT_Wtraditional,
2004e4b17023SJohn Marino "non-static declaration of %q+D "
2005e4b17023SJohn Marino "follows static declaration", newdecl);
2006e4b17023SJohn Marino }
2007e4b17023SJohn Marino }
2008e4b17023SJohn Marino else
2009e4b17023SJohn Marino {
2010e4b17023SJohn Marino if (TREE_PUBLIC (newdecl))
2011e4b17023SJohn Marino error ("non-static declaration of %q+D follows "
2012e4b17023SJohn Marino "static declaration", newdecl);
2013e4b17023SJohn Marino else
2014e4b17023SJohn Marino error ("static declaration of %q+D follows "
2015e4b17023SJohn Marino "non-static declaration", newdecl);
2016e4b17023SJohn Marino
2017e4b17023SJohn Marino locate_old_decl (olddecl);
2018e4b17023SJohn Marino return false;
2019e4b17023SJohn Marino }
2020e4b17023SJohn Marino }
2021e4b17023SJohn Marino /* Two objects with the same name declared at the same block
2022e4b17023SJohn Marino scope must both be external references (6.7p3). */
2023e4b17023SJohn Marino else if (!DECL_FILE_SCOPE_P (newdecl))
2024e4b17023SJohn Marino {
2025e4b17023SJohn Marino if (DECL_EXTERNAL (newdecl))
2026e4b17023SJohn Marino {
2027e4b17023SJohn Marino /* Extern with initializer at block scope, which will
2028e4b17023SJohn Marino already have received an error. */
2029e4b17023SJohn Marino }
2030e4b17023SJohn Marino else if (DECL_EXTERNAL (olddecl))
2031e4b17023SJohn Marino {
2032e4b17023SJohn Marino error ("declaration of %q+D with no linkage follows "
2033e4b17023SJohn Marino "extern declaration", newdecl);
2034e4b17023SJohn Marino locate_old_decl (olddecl);
2035e4b17023SJohn Marino }
2036e4b17023SJohn Marino else
2037e4b17023SJohn Marino {
2038e4b17023SJohn Marino error ("redeclaration of %q+D with no linkage", newdecl);
2039e4b17023SJohn Marino locate_old_decl (olddecl);
2040e4b17023SJohn Marino }
2041e4b17023SJohn Marino
2042e4b17023SJohn Marino return false;
2043e4b17023SJohn Marino }
2044e4b17023SJohn Marino
2045e4b17023SJohn Marino /* C++ does not permit a decl to appear multiple times at file
2046e4b17023SJohn Marino scope. */
2047e4b17023SJohn Marino if (warn_cxx_compat
2048e4b17023SJohn Marino && DECL_FILE_SCOPE_P (newdecl)
2049e4b17023SJohn Marino && !DECL_EXTERNAL (newdecl)
2050e4b17023SJohn Marino && !DECL_EXTERNAL (olddecl))
2051e4b17023SJohn Marino warned |= warning_at (DECL_SOURCE_LOCATION (newdecl),
2052e4b17023SJohn Marino OPT_Wc___compat,
2053e4b17023SJohn Marino ("duplicate declaration of %qD is "
2054e4b17023SJohn Marino "invalid in C++"),
2055e4b17023SJohn Marino newdecl);
2056e4b17023SJohn Marino }
2057e4b17023SJohn Marino
2058e4b17023SJohn Marino /* warnings */
2059e4b17023SJohn Marino /* All decls must agree on a visibility. */
2060e4b17023SJohn Marino if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
2061e4b17023SJohn Marino && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
2062e4b17023SJohn Marino && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2063e4b17023SJohn Marino {
2064e4b17023SJohn Marino warned |= warning (0, "redeclaration of %q+D with different visibility "
2065e4b17023SJohn Marino "(old visibility preserved)", newdecl);
2066e4b17023SJohn Marino }
2067e4b17023SJohn Marino
2068e4b17023SJohn Marino if (TREE_CODE (newdecl) == FUNCTION_DECL)
2069e4b17023SJohn Marino {
2070e4b17023SJohn Marino /* Diagnose inline __attribute__ ((noinline)) which is silly. */
2071e4b17023SJohn Marino if (DECL_DECLARED_INLINE_P (newdecl)
2072e4b17023SJohn Marino && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2073e4b17023SJohn Marino {
2074e4b17023SJohn Marino warned |= warning (OPT_Wattributes,
2075e4b17023SJohn Marino "inline declaration of %qD follows "
2076e4b17023SJohn Marino "declaration with attribute noinline", newdecl);
2077e4b17023SJohn Marino }
2078e4b17023SJohn Marino else if (DECL_DECLARED_INLINE_P (olddecl)
2079e4b17023SJohn Marino && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2080e4b17023SJohn Marino {
2081e4b17023SJohn Marino warned |= warning (OPT_Wattributes,
2082e4b17023SJohn Marino "declaration of %q+D with attribute "
2083e4b17023SJohn Marino "noinline follows inline declaration ", newdecl);
2084e4b17023SJohn Marino }
2085e4b17023SJohn Marino }
2086e4b17023SJohn Marino else /* PARM_DECL, VAR_DECL */
2087e4b17023SJohn Marino {
2088e4b17023SJohn Marino /* Redeclaration of a parameter is a constraint violation (this is
2089e4b17023SJohn Marino not explicitly stated, but follows from C99 6.7p3 [no more than
2090e4b17023SJohn Marino one declaration of the same identifier with no linkage in the
2091e4b17023SJohn Marino same scope, except type tags] and 6.2.2p6 [parameters have no
2092e4b17023SJohn Marino linkage]). We must check for a forward parameter declaration,
2093e4b17023SJohn Marino indicated by TREE_ASM_WRITTEN on the old declaration - this is
2094e4b17023SJohn Marino an extension, the mandatory diagnostic for which is handled by
2095e4b17023SJohn Marino mark_forward_parm_decls. */
2096e4b17023SJohn Marino
2097e4b17023SJohn Marino if (TREE_CODE (newdecl) == PARM_DECL
2098e4b17023SJohn Marino && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
2099e4b17023SJohn Marino {
2100e4b17023SJohn Marino error ("redefinition of parameter %q+D", newdecl);
2101e4b17023SJohn Marino locate_old_decl (olddecl);
2102e4b17023SJohn Marino return false;
2103e4b17023SJohn Marino }
2104e4b17023SJohn Marino }
2105e4b17023SJohn Marino
2106e4b17023SJohn Marino /* Optional warning for completely redundant decls. */
2107e4b17023SJohn Marino if (!warned && !pedwarned
2108e4b17023SJohn Marino && warn_redundant_decls
2109e4b17023SJohn Marino /* Don't warn about a function declaration followed by a
2110e4b17023SJohn Marino definition. */
2111e4b17023SJohn Marino && !(TREE_CODE (newdecl) == FUNCTION_DECL
2112e4b17023SJohn Marino && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
2113e4b17023SJohn Marino /* Don't warn about redundant redeclarations of builtins. */
2114e4b17023SJohn Marino && !(TREE_CODE (newdecl) == FUNCTION_DECL
2115e4b17023SJohn Marino && !DECL_BUILT_IN (newdecl)
2116e4b17023SJohn Marino && DECL_BUILT_IN (olddecl)
2117e4b17023SJohn Marino && !C_DECL_DECLARED_BUILTIN (olddecl))
2118e4b17023SJohn Marino /* Don't warn about an extern followed by a definition. */
2119e4b17023SJohn Marino && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
2120e4b17023SJohn Marino /* Don't warn about forward parameter decls. */
2121e4b17023SJohn Marino && !(TREE_CODE (newdecl) == PARM_DECL
2122e4b17023SJohn Marino && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2123e4b17023SJohn Marino /* Don't warn about a variable definition following a declaration. */
2124e4b17023SJohn Marino && !(TREE_CODE (newdecl) == VAR_DECL
2125e4b17023SJohn Marino && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
2126e4b17023SJohn Marino {
2127e4b17023SJohn Marino warned = warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
2128e4b17023SJohn Marino newdecl);
2129e4b17023SJohn Marino }
2130e4b17023SJohn Marino
2131e4b17023SJohn Marino /* Report location of previous decl/defn. */
2132e4b17023SJohn Marino if (warned || pedwarned)
2133e4b17023SJohn Marino locate_old_decl (olddecl);
2134e4b17023SJohn Marino
2135e4b17023SJohn Marino #undef DECL_EXTERN_INLINE
2136e4b17023SJohn Marino
2137e4b17023SJohn Marino return retval;
2138e4b17023SJohn Marino }
2139e4b17023SJohn Marino
2140e4b17023SJohn Marino /* Subroutine of duplicate_decls. NEWDECL has been found to be
2141e4b17023SJohn Marino consistent with OLDDECL, but carries new information. Merge the
2142e4b17023SJohn Marino new information into OLDDECL. This function issues no
2143e4b17023SJohn Marino diagnostics. */
2144e4b17023SJohn Marino
2145e4b17023SJohn Marino static void
merge_decls(tree newdecl,tree olddecl,tree newtype,tree oldtype)2146e4b17023SJohn Marino merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
2147e4b17023SJohn Marino {
2148e4b17023SJohn Marino bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
2149e4b17023SJohn Marino && DECL_INITIAL (newdecl) != 0);
2150e4b17023SJohn Marino bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
2151e4b17023SJohn Marino && prototype_p (TREE_TYPE (newdecl)));
2152e4b17023SJohn Marino bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
2153e4b17023SJohn Marino && prototype_p (TREE_TYPE (olddecl)));
2154e4b17023SJohn Marino bool extern_changed = false;
2155e4b17023SJohn Marino
2156e4b17023SJohn Marino /* For real parm decl following a forward decl, rechain the old decl
2157e4b17023SJohn Marino in its new location and clear TREE_ASM_WRITTEN (it's not a
2158e4b17023SJohn Marino forward decl anymore). */
2159e4b17023SJohn Marino if (TREE_CODE (newdecl) == PARM_DECL
2160e4b17023SJohn Marino && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
2161e4b17023SJohn Marino {
2162e4b17023SJohn Marino struct c_binding *b, **here;
2163e4b17023SJohn Marino
2164e4b17023SJohn Marino for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
2165e4b17023SJohn Marino if ((*here)->decl == olddecl)
2166e4b17023SJohn Marino goto found;
2167e4b17023SJohn Marino gcc_unreachable ();
2168e4b17023SJohn Marino
2169e4b17023SJohn Marino found:
2170e4b17023SJohn Marino b = *here;
2171e4b17023SJohn Marino *here = b->prev;
2172e4b17023SJohn Marino b->prev = current_scope->bindings;
2173e4b17023SJohn Marino current_scope->bindings = b;
2174e4b17023SJohn Marino
2175e4b17023SJohn Marino TREE_ASM_WRITTEN (olddecl) = 0;
2176e4b17023SJohn Marino }
2177e4b17023SJohn Marino
2178e4b17023SJohn Marino DECL_ATTRIBUTES (newdecl)
2179e4b17023SJohn Marino = targetm.merge_decl_attributes (olddecl, newdecl);
2180e4b17023SJohn Marino
2181e4b17023SJohn Marino /* Merge the data types specified in the two decls. */
2182e4b17023SJohn Marino TREE_TYPE (newdecl)
2183e4b17023SJohn Marino = TREE_TYPE (olddecl)
2184e4b17023SJohn Marino = composite_type (newtype, oldtype);
2185e4b17023SJohn Marino
2186e4b17023SJohn Marino /* Lay the type out, unless already done. */
2187e4b17023SJohn Marino if (!comptypes (oldtype, TREE_TYPE (newdecl)))
2188e4b17023SJohn Marino {
2189e4b17023SJohn Marino if (TREE_TYPE (newdecl) != error_mark_node)
2190e4b17023SJohn Marino layout_type (TREE_TYPE (newdecl));
2191e4b17023SJohn Marino if (TREE_CODE (newdecl) != FUNCTION_DECL
2192e4b17023SJohn Marino && TREE_CODE (newdecl) != TYPE_DECL
2193e4b17023SJohn Marino && TREE_CODE (newdecl) != CONST_DECL)
2194e4b17023SJohn Marino layout_decl (newdecl, 0);
2195e4b17023SJohn Marino }
2196e4b17023SJohn Marino else
2197e4b17023SJohn Marino {
2198e4b17023SJohn Marino /* Since the type is OLDDECL's, make OLDDECL's size go with. */
2199e4b17023SJohn Marino DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
2200e4b17023SJohn Marino DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
2201e4b17023SJohn Marino DECL_MODE (newdecl) = DECL_MODE (olddecl);
2202e4b17023SJohn Marino if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
2203e4b17023SJohn Marino {
2204e4b17023SJohn Marino DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
2205e4b17023SJohn Marino DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
2206e4b17023SJohn Marino }
2207e4b17023SJohn Marino }
2208e4b17023SJohn Marino
2209e4b17023SJohn Marino /* Keep the old rtl since we can safely use it. */
2210e4b17023SJohn Marino if (HAS_RTL_P (olddecl))
2211e4b17023SJohn Marino COPY_DECL_RTL (olddecl, newdecl);
2212e4b17023SJohn Marino
2213e4b17023SJohn Marino /* Merge the type qualifiers. */
2214e4b17023SJohn Marino if (TREE_READONLY (newdecl))
2215e4b17023SJohn Marino TREE_READONLY (olddecl) = 1;
2216e4b17023SJohn Marino
2217e4b17023SJohn Marino if (TREE_THIS_VOLATILE (newdecl))
2218e4b17023SJohn Marino TREE_THIS_VOLATILE (olddecl) = 1;
2219e4b17023SJohn Marino
2220e4b17023SJohn Marino /* Merge deprecatedness. */
2221e4b17023SJohn Marino if (TREE_DEPRECATED (newdecl))
2222e4b17023SJohn Marino TREE_DEPRECATED (olddecl) = 1;
2223e4b17023SJohn Marino
2224e4b17023SJohn Marino /* If a decl is in a system header and the other isn't, keep the one on the
2225e4b17023SJohn Marino system header. Otherwise, keep source location of definition rather than
2226e4b17023SJohn Marino declaration and of prototype rather than non-prototype unless that
2227e4b17023SJohn Marino prototype is built-in. */
2228e4b17023SJohn Marino if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2229e4b17023SJohn Marino && DECL_IN_SYSTEM_HEADER (olddecl)
2230e4b17023SJohn Marino && !DECL_IN_SYSTEM_HEADER (newdecl) )
2231e4b17023SJohn Marino DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2232e4b17023SJohn Marino else if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS)
2233e4b17023SJohn Marino && DECL_IN_SYSTEM_HEADER (newdecl)
2234e4b17023SJohn Marino && !DECL_IN_SYSTEM_HEADER (olddecl))
2235e4b17023SJohn Marino DECL_SOURCE_LOCATION (olddecl) = DECL_SOURCE_LOCATION (newdecl);
2236e4b17023SJohn Marino else if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
2237e4b17023SJohn Marino || (old_is_prototype && !new_is_prototype
2238e4b17023SJohn Marino && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
2239e4b17023SJohn Marino DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
2240e4b17023SJohn Marino
2241e4b17023SJohn Marino /* Merge the initialization information. */
2242e4b17023SJohn Marino if (DECL_INITIAL (newdecl) == 0)
2243e4b17023SJohn Marino DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2244e4b17023SJohn Marino
2245e4b17023SJohn Marino /* Merge the threadprivate attribute. */
2246e4b17023SJohn Marino if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
2247e4b17023SJohn Marino {
2248e4b17023SJohn Marino DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
2249e4b17023SJohn Marino C_DECL_THREADPRIVATE_P (newdecl) = 1;
2250e4b17023SJohn Marino }
2251e4b17023SJohn Marino
2252e4b17023SJohn Marino if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
2253e4b17023SJohn Marino {
2254e4b17023SJohn Marino /* Merge the section attribute.
2255e4b17023SJohn Marino We want to issue an error if the sections conflict but that
2256e4b17023SJohn Marino must be done later in decl_attributes since we are called
2257e4b17023SJohn Marino before attributes are assigned. */
2258e4b17023SJohn Marino if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2259e4b17023SJohn Marino DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2260e4b17023SJohn Marino
2261e4b17023SJohn Marino /* Copy the assembler name.
2262e4b17023SJohn Marino Currently, it can only be defined in the prototype. */
2263e4b17023SJohn Marino COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2264e4b17023SJohn Marino
2265e4b17023SJohn Marino /* Use visibility of whichever declaration had it specified */
2266e4b17023SJohn Marino if (DECL_VISIBILITY_SPECIFIED (olddecl))
2267e4b17023SJohn Marino {
2268e4b17023SJohn Marino DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2269e4b17023SJohn Marino DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2270e4b17023SJohn Marino }
2271e4b17023SJohn Marino
2272e4b17023SJohn Marino if (TREE_CODE (newdecl) == FUNCTION_DECL)
2273e4b17023SJohn Marino {
2274e4b17023SJohn Marino DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
2275e4b17023SJohn Marino DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2276e4b17023SJohn Marino DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
2277e4b17023SJohn Marino DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
2278e4b17023SJohn Marino |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
2279e4b17023SJohn Marino TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
2280e4b17023SJohn Marino DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
2281e4b17023SJohn Marino DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
2282e4b17023SJohn Marino TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
2283e4b17023SJohn Marino DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
2284e4b17023SJohn Marino DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
2285e4b17023SJohn Marino }
2286e4b17023SJohn Marino
2287e4b17023SJohn Marino /* Merge the storage class information. */
2288e4b17023SJohn Marino merge_weak (newdecl, olddecl);
2289e4b17023SJohn Marino
2290e4b17023SJohn Marino /* For functions, static overrides non-static. */
2291e4b17023SJohn Marino if (TREE_CODE (newdecl) == FUNCTION_DECL)
2292e4b17023SJohn Marino {
2293e4b17023SJohn Marino TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
2294e4b17023SJohn Marino /* This is since we don't automatically
2295e4b17023SJohn Marino copy the attributes of NEWDECL into OLDDECL. */
2296e4b17023SJohn Marino TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2297e4b17023SJohn Marino /* If this clears `static', clear it in the identifier too. */
2298e4b17023SJohn Marino if (!TREE_PUBLIC (olddecl))
2299e4b17023SJohn Marino TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
2300e4b17023SJohn Marino }
2301e4b17023SJohn Marino }
2302e4b17023SJohn Marino
2303e4b17023SJohn Marino /* In c99, 'extern' declaration before (or after) 'inline' means this
2304e4b17023SJohn Marino function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
2305e4b17023SJohn Marino is present. */
2306e4b17023SJohn Marino if (TREE_CODE (newdecl) == FUNCTION_DECL
2307e4b17023SJohn Marino && !flag_gnu89_inline
2308e4b17023SJohn Marino && (DECL_DECLARED_INLINE_P (newdecl)
2309e4b17023SJohn Marino || DECL_DECLARED_INLINE_P (olddecl))
2310e4b17023SJohn Marino && (!DECL_DECLARED_INLINE_P (newdecl)
2311e4b17023SJohn Marino || !DECL_DECLARED_INLINE_P (olddecl)
2312e4b17023SJohn Marino || !DECL_EXTERNAL (olddecl))
2313e4b17023SJohn Marino && DECL_EXTERNAL (newdecl)
2314e4b17023SJohn Marino && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl))
2315e4b17023SJohn Marino && !current_function_decl)
2316e4b17023SJohn Marino DECL_EXTERNAL (newdecl) = 0;
2317e4b17023SJohn Marino
2318e4b17023SJohn Marino if (DECL_EXTERNAL (newdecl))
2319e4b17023SJohn Marino {
2320e4b17023SJohn Marino TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
2321e4b17023SJohn Marino DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
2322e4b17023SJohn Marino
2323e4b17023SJohn Marino /* An extern decl does not override previous storage class. */
2324e4b17023SJohn Marino TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2325e4b17023SJohn Marino if (!DECL_EXTERNAL (newdecl))
2326e4b17023SJohn Marino {
2327e4b17023SJohn Marino DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2328e4b17023SJohn Marino DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2329e4b17023SJohn Marino }
2330e4b17023SJohn Marino }
2331e4b17023SJohn Marino else
2332e4b17023SJohn Marino {
2333e4b17023SJohn Marino TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
2334e4b17023SJohn Marino TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
2335e4b17023SJohn Marino }
2336e4b17023SJohn Marino
2337e4b17023SJohn Marino if (TREE_CODE (newdecl) == FUNCTION_DECL)
2338e4b17023SJohn Marino {
2339e4b17023SJohn Marino /* If we're redefining a function previously defined as extern
2340e4b17023SJohn Marino inline, make sure we emit debug info for the inline before we
2341e4b17023SJohn Marino throw it away, in case it was inlined into a function that
2342e4b17023SJohn Marino hasn't been written out yet. */
2343e4b17023SJohn Marino if (new_is_definition && DECL_INITIAL (olddecl))
2344e4b17023SJohn Marino /* The new defn must not be inline. */
2345e4b17023SJohn Marino DECL_UNINLINABLE (newdecl) = 1;
2346e4b17023SJohn Marino else
2347e4b17023SJohn Marino {
2348e4b17023SJohn Marino /* If either decl says `inline', this fn is inline, unless
2349e4b17023SJohn Marino its definition was passed already. */
2350e4b17023SJohn Marino if (DECL_DECLARED_INLINE_P (newdecl)
2351e4b17023SJohn Marino || DECL_DECLARED_INLINE_P (olddecl))
2352e4b17023SJohn Marino DECL_DECLARED_INLINE_P (newdecl) = 1;
2353e4b17023SJohn Marino
2354e4b17023SJohn Marino DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
2355e4b17023SJohn Marino = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
2356e4b17023SJohn Marino
2357e4b17023SJohn Marino DECL_DISREGARD_INLINE_LIMITS (newdecl)
2358e4b17023SJohn Marino = DECL_DISREGARD_INLINE_LIMITS (olddecl)
2359e4b17023SJohn Marino = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
2360e4b17023SJohn Marino || DECL_DISREGARD_INLINE_LIMITS (olddecl));
2361e4b17023SJohn Marino }
2362e4b17023SJohn Marino
2363e4b17023SJohn Marino if (DECL_BUILT_IN (olddecl))
2364e4b17023SJohn Marino {
2365e4b17023SJohn Marino /* If redeclaring a builtin function, it stays built in.
2366e4b17023SJohn Marino But it gets tagged as having been declared. */
2367e4b17023SJohn Marino DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2368e4b17023SJohn Marino DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2369e4b17023SJohn Marino C_DECL_DECLARED_BUILTIN (newdecl) = 1;
2370e4b17023SJohn Marino if (new_is_prototype)
2371e4b17023SJohn Marino {
2372e4b17023SJohn Marino C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
2373e4b17023SJohn Marino if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
2374e4b17023SJohn Marino {
2375e4b17023SJohn Marino enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
2376e4b17023SJohn Marino switch (fncode)
2377e4b17023SJohn Marino {
2378e4b17023SJohn Marino /* If a compatible prototype of these builtin functions
2379e4b17023SJohn Marino is seen, assume the runtime implements it with the
2380e4b17023SJohn Marino expected semantics. */
2381e4b17023SJohn Marino case BUILT_IN_STPCPY:
2382e4b17023SJohn Marino if (builtin_decl_explicit_p (fncode))
2383e4b17023SJohn Marino set_builtin_decl_implicit_p (fncode, true);
2384e4b17023SJohn Marino break;
2385e4b17023SJohn Marino default:
2386e4b17023SJohn Marino break;
2387e4b17023SJohn Marino }
2388e4b17023SJohn Marino }
2389e4b17023SJohn Marino }
2390e4b17023SJohn Marino else
2391e4b17023SJohn Marino C_DECL_BUILTIN_PROTOTYPE (newdecl)
2392e4b17023SJohn Marino = C_DECL_BUILTIN_PROTOTYPE (olddecl);
2393e4b17023SJohn Marino }
2394e4b17023SJohn Marino
2395e4b17023SJohn Marino /* Preserve function specific target and optimization options */
2396e4b17023SJohn Marino if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
2397e4b17023SJohn Marino && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
2398e4b17023SJohn Marino DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
2399e4b17023SJohn Marino = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
2400e4b17023SJohn Marino
2401e4b17023SJohn Marino if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
2402e4b17023SJohn Marino && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
2403e4b17023SJohn Marino DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
2404e4b17023SJohn Marino = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
2405e4b17023SJohn Marino
2406e4b17023SJohn Marino /* Also preserve various other info from the definition. */
2407e4b17023SJohn Marino if (!new_is_definition)
2408e4b17023SJohn Marino {
2409e4b17023SJohn Marino tree t;
2410e4b17023SJohn Marino DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2411e4b17023SJohn Marino DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2412e4b17023SJohn Marino DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
2413e4b17023SJohn Marino DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
2414e4b17023SJohn Marino DECL_ARGUMENTS (newdecl) = copy_list (DECL_ARGUMENTS (olddecl));
2415e4b17023SJohn Marino for (t = DECL_ARGUMENTS (newdecl); t ; t = DECL_CHAIN (t))
2416e4b17023SJohn Marino DECL_CONTEXT (t) = newdecl;
2417e4b17023SJohn Marino
2418e4b17023SJohn Marino /* See if we've got a function to instantiate from. */
2419e4b17023SJohn Marino if (DECL_SAVED_TREE (olddecl))
2420e4b17023SJohn Marino DECL_ABSTRACT_ORIGIN (newdecl)
2421e4b17023SJohn Marino = DECL_ABSTRACT_ORIGIN (olddecl);
2422e4b17023SJohn Marino }
2423e4b17023SJohn Marino }
2424e4b17023SJohn Marino
2425e4b17023SJohn Marino extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
2426e4b17023SJohn Marino
2427e4b17023SJohn Marino /* Merge the USED information. */
2428e4b17023SJohn Marino if (TREE_USED (olddecl))
2429e4b17023SJohn Marino TREE_USED (newdecl) = 1;
2430e4b17023SJohn Marino else if (TREE_USED (newdecl))
2431e4b17023SJohn Marino TREE_USED (olddecl) = 1;
2432e4b17023SJohn Marino if (TREE_CODE (olddecl) == VAR_DECL || TREE_CODE (olddecl) == PARM_DECL)
2433e4b17023SJohn Marino DECL_READ_P (newdecl) |= DECL_READ_P (olddecl);
2434e4b17023SJohn Marino if (DECL_PRESERVE_P (olddecl))
2435e4b17023SJohn Marino DECL_PRESERVE_P (newdecl) = 1;
2436e4b17023SJohn Marino else if (DECL_PRESERVE_P (newdecl))
2437e4b17023SJohn Marino DECL_PRESERVE_P (olddecl) = 1;
2438e4b17023SJohn Marino
2439e4b17023SJohn Marino /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
2440e4b17023SJohn Marino But preserve OLDDECL's DECL_UID, DECL_CONTEXT and
2441e4b17023SJohn Marino DECL_ARGUMENTS (if appropriate). */
2442e4b17023SJohn Marino {
2443e4b17023SJohn Marino unsigned olddecl_uid = DECL_UID (olddecl);
2444e4b17023SJohn Marino tree olddecl_context = DECL_CONTEXT (olddecl);
2445e4b17023SJohn Marino tree olddecl_arguments = NULL;
2446e4b17023SJohn Marino if (TREE_CODE (olddecl) == FUNCTION_DECL)
2447e4b17023SJohn Marino olddecl_arguments = DECL_ARGUMENTS (olddecl);
2448e4b17023SJohn Marino
2449e4b17023SJohn Marino memcpy ((char *) olddecl + sizeof (struct tree_common),
2450e4b17023SJohn Marino (char *) newdecl + sizeof (struct tree_common),
2451e4b17023SJohn Marino sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2452e4b17023SJohn Marino DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
2453e4b17023SJohn Marino switch (TREE_CODE (olddecl))
2454e4b17023SJohn Marino {
2455e4b17023SJohn Marino case FUNCTION_DECL:
2456e4b17023SJohn Marino case FIELD_DECL:
2457e4b17023SJohn Marino case VAR_DECL:
2458e4b17023SJohn Marino case PARM_DECL:
2459e4b17023SJohn Marino case LABEL_DECL:
2460e4b17023SJohn Marino case RESULT_DECL:
2461e4b17023SJohn Marino case CONST_DECL:
2462e4b17023SJohn Marino case TYPE_DECL:
2463e4b17023SJohn Marino memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2464e4b17023SJohn Marino (char *) newdecl + sizeof (struct tree_decl_common),
2465e4b17023SJohn Marino tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
2466e4b17023SJohn Marino break;
2467e4b17023SJohn Marino
2468e4b17023SJohn Marino default:
2469e4b17023SJohn Marino
2470e4b17023SJohn Marino memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2471e4b17023SJohn Marino (char *) newdecl + sizeof (struct tree_decl_common),
2472e4b17023SJohn Marino sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
2473e4b17023SJohn Marino }
2474e4b17023SJohn Marino DECL_UID (olddecl) = olddecl_uid;
2475e4b17023SJohn Marino DECL_CONTEXT (olddecl) = olddecl_context;
2476e4b17023SJohn Marino if (TREE_CODE (olddecl) == FUNCTION_DECL)
2477e4b17023SJohn Marino DECL_ARGUMENTS (olddecl) = olddecl_arguments;
2478e4b17023SJohn Marino }
2479e4b17023SJohn Marino
2480e4b17023SJohn Marino /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2481e4b17023SJohn Marino so that encode_section_info has a chance to look at the new decl
2482e4b17023SJohn Marino flags and attributes. */
2483e4b17023SJohn Marino if (DECL_RTL_SET_P (olddecl)
2484e4b17023SJohn Marino && (TREE_CODE (olddecl) == FUNCTION_DECL
2485e4b17023SJohn Marino || (TREE_CODE (olddecl) == VAR_DECL
2486e4b17023SJohn Marino && TREE_STATIC (olddecl))))
2487e4b17023SJohn Marino make_decl_rtl (olddecl);
2488e4b17023SJohn Marino
2489e4b17023SJohn Marino /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
2490e4b17023SJohn Marino and the definition is coming from the old version, cgraph needs
2491e4b17023SJohn Marino to be called again. */
2492e4b17023SJohn Marino if (extern_changed && !new_is_definition
2493e4b17023SJohn Marino && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
2494e4b17023SJohn Marino cgraph_mark_if_needed (olddecl);
2495e4b17023SJohn Marino }
2496e4b17023SJohn Marino
2497e4b17023SJohn Marino /* Handle when a new declaration NEWDECL has the same name as an old
2498e4b17023SJohn Marino one OLDDECL in the same binding contour. Prints an error message
2499e4b17023SJohn Marino if appropriate.
2500e4b17023SJohn Marino
2501e4b17023SJohn Marino If safely possible, alter OLDDECL to look like NEWDECL, and return
2502e4b17023SJohn Marino true. Otherwise, return false. */
2503e4b17023SJohn Marino
2504e4b17023SJohn Marino static bool
duplicate_decls(tree newdecl,tree olddecl)2505e4b17023SJohn Marino duplicate_decls (tree newdecl, tree olddecl)
2506e4b17023SJohn Marino {
2507e4b17023SJohn Marino tree newtype = NULL, oldtype = NULL;
2508e4b17023SJohn Marino
2509e4b17023SJohn Marino if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
2510e4b17023SJohn Marino {
2511e4b17023SJohn Marino /* Avoid `unused variable' and other warnings for OLDDECL. */
2512e4b17023SJohn Marino TREE_NO_WARNING (olddecl) = 1;
2513e4b17023SJohn Marino return false;
2514e4b17023SJohn Marino }
2515e4b17023SJohn Marino
2516e4b17023SJohn Marino merge_decls (newdecl, olddecl, newtype, oldtype);
2517e4b17023SJohn Marino return true;
2518e4b17023SJohn Marino }
2519e4b17023SJohn Marino
2520e4b17023SJohn Marino
2521e4b17023SJohn Marino /* Check whether decl-node NEW_DECL shadows an existing declaration. */
2522e4b17023SJohn Marino static void
warn_if_shadowing(tree new_decl)2523e4b17023SJohn Marino warn_if_shadowing (tree new_decl)
2524e4b17023SJohn Marino {
2525e4b17023SJohn Marino struct c_binding *b;
2526e4b17023SJohn Marino
2527e4b17023SJohn Marino /* Shadow warnings wanted? */
2528e4b17023SJohn Marino if (!warn_shadow
2529e4b17023SJohn Marino /* No shadow warnings for internally generated vars. */
2530e4b17023SJohn Marino || DECL_IS_BUILTIN (new_decl)
2531e4b17023SJohn Marino /* No shadow warnings for vars made for inlining. */
2532e4b17023SJohn Marino || DECL_FROM_INLINE (new_decl))
2533e4b17023SJohn Marino return;
2534e4b17023SJohn Marino
2535e4b17023SJohn Marino /* Is anything being shadowed? Invisible decls do not count. */
2536e4b17023SJohn Marino for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
2537e4b17023SJohn Marino if (b->decl && b->decl != new_decl && !b->invisible
2538e4b17023SJohn Marino && (b->decl == error_mark_node
2539e4b17023SJohn Marino || diagnostic_report_warnings_p (global_dc,
2540e4b17023SJohn Marino DECL_SOURCE_LOCATION (b->decl))))
2541e4b17023SJohn Marino {
2542e4b17023SJohn Marino tree old_decl = b->decl;
2543e4b17023SJohn Marino
2544e4b17023SJohn Marino if (old_decl == error_mark_node)
2545e4b17023SJohn Marino {
2546e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows previous "
2547e4b17023SJohn Marino "non-variable", new_decl);
2548e4b17023SJohn Marino break;
2549e4b17023SJohn Marino }
2550e4b17023SJohn Marino else if (TREE_CODE (old_decl) == PARM_DECL)
2551e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
2552e4b17023SJohn Marino new_decl);
2553e4b17023SJohn Marino else if (DECL_FILE_SCOPE_P (old_decl))
2554e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows a global "
2555e4b17023SJohn Marino "declaration", new_decl);
2556e4b17023SJohn Marino else if (TREE_CODE (old_decl) == FUNCTION_DECL
2557e4b17023SJohn Marino && DECL_BUILT_IN (old_decl))
2558e4b17023SJohn Marino {
2559e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows "
2560e4b17023SJohn Marino "a built-in function", new_decl);
2561e4b17023SJohn Marino break;
2562e4b17023SJohn Marino }
2563e4b17023SJohn Marino else
2564e4b17023SJohn Marino warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
2565e4b17023SJohn Marino new_decl);
2566e4b17023SJohn Marino
2567e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (old_decl), OPT_Wshadow,
2568e4b17023SJohn Marino "shadowed declaration is here");
2569e4b17023SJohn Marino
2570e4b17023SJohn Marino break;
2571e4b17023SJohn Marino }
2572e4b17023SJohn Marino }
2573e4b17023SJohn Marino
2574e4b17023SJohn Marino /* Record a decl-node X as belonging to the current lexical scope.
2575e4b17023SJohn Marino Check for errors (such as an incompatible declaration for the same
2576e4b17023SJohn Marino name already seen in the same scope).
2577e4b17023SJohn Marino
2578e4b17023SJohn Marino Returns either X or an old decl for the same name.
2579e4b17023SJohn Marino If an old decl is returned, it may have been smashed
2580e4b17023SJohn Marino to agree with what X says. */
2581e4b17023SJohn Marino
2582e4b17023SJohn Marino tree
pushdecl(tree x)2583e4b17023SJohn Marino pushdecl (tree x)
2584e4b17023SJohn Marino {
2585e4b17023SJohn Marino tree name = DECL_NAME (x);
2586e4b17023SJohn Marino struct c_scope *scope = current_scope;
2587e4b17023SJohn Marino struct c_binding *b;
2588e4b17023SJohn Marino bool nested = false;
2589e4b17023SJohn Marino location_t locus = DECL_SOURCE_LOCATION (x);
2590e4b17023SJohn Marino
2591e4b17023SJohn Marino /* Must set DECL_CONTEXT for everything not at file scope or
2592e4b17023SJohn Marino DECL_FILE_SCOPE_P won't work. Local externs don't count
2593e4b17023SJohn Marino unless they have initializers (which generate code). */
2594e4b17023SJohn Marino if (current_function_decl
2595e4b17023SJohn Marino && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2596e4b17023SJohn Marino || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2597e4b17023SJohn Marino DECL_CONTEXT (x) = current_function_decl;
2598e4b17023SJohn Marino
2599e4b17023SJohn Marino /* Anonymous decls are just inserted in the scope. */
2600e4b17023SJohn Marino if (!name)
2601e4b17023SJohn Marino {
2602e4b17023SJohn Marino bind (name, x, scope, /*invisible=*/false, /*nested=*/false,
2603e4b17023SJohn Marino locus);
2604e4b17023SJohn Marino return x;
2605e4b17023SJohn Marino }
2606e4b17023SJohn Marino
2607e4b17023SJohn Marino /* First, see if there is another declaration with the same name in
2608e4b17023SJohn Marino the current scope. If there is, duplicate_decls may do all the
2609e4b17023SJohn Marino work for us. If duplicate_decls returns false, that indicates
2610e4b17023SJohn Marino two incompatible decls in the same scope; we are to silently
2611e4b17023SJohn Marino replace the old one (duplicate_decls has issued all appropriate
2612e4b17023SJohn Marino diagnostics). In particular, we should not consider possible
2613e4b17023SJohn Marino duplicates in the external scope, or shadowing. */
2614e4b17023SJohn Marino b = I_SYMBOL_BINDING (name);
2615e4b17023SJohn Marino if (b && B_IN_SCOPE (b, scope))
2616e4b17023SJohn Marino {
2617e4b17023SJohn Marino struct c_binding *b_ext, *b_use;
2618e4b17023SJohn Marino tree type = TREE_TYPE (x);
2619e4b17023SJohn Marino tree visdecl = b->decl;
2620e4b17023SJohn Marino tree vistype = TREE_TYPE (visdecl);
2621e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2622e4b17023SJohn Marino && COMPLETE_TYPE_P (TREE_TYPE (x)))
2623e4b17023SJohn Marino b->inner_comp = false;
2624e4b17023SJohn Marino b_use = b;
2625e4b17023SJohn Marino b_ext = b;
2626e4b17023SJohn Marino /* If this is an external linkage declaration, we should check
2627e4b17023SJohn Marino for compatibility with the type in the external scope before
2628e4b17023SJohn Marino setting the type at this scope based on the visible
2629e4b17023SJohn Marino information only. */
2630e4b17023SJohn Marino if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2631e4b17023SJohn Marino {
2632e4b17023SJohn Marino while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2633e4b17023SJohn Marino b_ext = b_ext->shadowed;
2634e4b17023SJohn Marino if (b_ext)
2635e4b17023SJohn Marino {
2636e4b17023SJohn Marino b_use = b_ext;
2637e4b17023SJohn Marino if (b_use->u.type)
2638e4b17023SJohn Marino TREE_TYPE (b_use->decl) = b_use->u.type;
2639e4b17023SJohn Marino }
2640e4b17023SJohn Marino }
2641e4b17023SJohn Marino if (duplicate_decls (x, b_use->decl))
2642e4b17023SJohn Marino {
2643e4b17023SJohn Marino if (b_use != b)
2644e4b17023SJohn Marino {
2645e4b17023SJohn Marino /* Save the updated type in the external scope and
2646e4b17023SJohn Marino restore the proper type for this scope. */
2647e4b17023SJohn Marino tree thistype;
2648e4b17023SJohn Marino if (comptypes (vistype, type))
2649e4b17023SJohn Marino thistype = composite_type (vistype, type);
2650e4b17023SJohn Marino else
2651e4b17023SJohn Marino thistype = TREE_TYPE (b_use->decl);
2652e4b17023SJohn Marino b_use->u.type = TREE_TYPE (b_use->decl);
2653e4b17023SJohn Marino if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2654e4b17023SJohn Marino && DECL_BUILT_IN (b_use->decl))
2655e4b17023SJohn Marino thistype
2656e4b17023SJohn Marino = build_type_attribute_variant (thistype,
2657e4b17023SJohn Marino TYPE_ATTRIBUTES
2658e4b17023SJohn Marino (b_use->u.type));
2659e4b17023SJohn Marino TREE_TYPE (b_use->decl) = thistype;
2660e4b17023SJohn Marino }
2661e4b17023SJohn Marino return b_use->decl;
2662e4b17023SJohn Marino }
2663e4b17023SJohn Marino else
2664e4b17023SJohn Marino goto skip_external_and_shadow_checks;
2665e4b17023SJohn Marino }
2666e4b17023SJohn Marino
2667e4b17023SJohn Marino /* All declarations with external linkage, and all external
2668e4b17023SJohn Marino references, go in the external scope, no matter what scope is
2669e4b17023SJohn Marino current. However, the binding in that scope is ignored for
2670e4b17023SJohn Marino purposes of normal name lookup. A separate binding structure is
2671e4b17023SJohn Marino created in the requested scope; this governs the normal
2672e4b17023SJohn Marino visibility of the symbol.
2673e4b17023SJohn Marino
2674e4b17023SJohn Marino The binding in the externals scope is used exclusively for
2675e4b17023SJohn Marino detecting duplicate declarations of the same object, no matter
2676e4b17023SJohn Marino what scope they are in; this is what we do here. (C99 6.2.7p2:
2677e4b17023SJohn Marino All declarations that refer to the same object or function shall
2678e4b17023SJohn Marino have compatible type; otherwise, the behavior is undefined.) */
2679e4b17023SJohn Marino if (DECL_EXTERNAL (x) || scope == file_scope)
2680e4b17023SJohn Marino {
2681e4b17023SJohn Marino tree type = TREE_TYPE (x);
2682e4b17023SJohn Marino tree vistype = 0;
2683e4b17023SJohn Marino tree visdecl = 0;
2684e4b17023SJohn Marino bool type_saved = false;
2685e4b17023SJohn Marino if (b && !B_IN_EXTERNAL_SCOPE (b)
2686e4b17023SJohn Marino && (TREE_CODE (b->decl) == FUNCTION_DECL
2687e4b17023SJohn Marino || TREE_CODE (b->decl) == VAR_DECL)
2688e4b17023SJohn Marino && DECL_FILE_SCOPE_P (b->decl))
2689e4b17023SJohn Marino {
2690e4b17023SJohn Marino visdecl = b->decl;
2691e4b17023SJohn Marino vistype = TREE_TYPE (visdecl);
2692e4b17023SJohn Marino }
2693e4b17023SJohn Marino if (scope != file_scope
2694e4b17023SJohn Marino && !DECL_IN_SYSTEM_HEADER (x))
2695e4b17023SJohn Marino warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2696e4b17023SJohn Marino
2697e4b17023SJohn Marino while (b && !B_IN_EXTERNAL_SCOPE (b))
2698e4b17023SJohn Marino {
2699e4b17023SJohn Marino /* If this decl might be modified, save its type. This is
2700e4b17023SJohn Marino done here rather than when the decl is first bound
2701e4b17023SJohn Marino because the type may change after first binding, through
2702e4b17023SJohn Marino being completed or through attributes being added. If we
2703e4b17023SJohn Marino encounter multiple such decls, only the first should have
2704e4b17023SJohn Marino its type saved; the others will already have had their
2705e4b17023SJohn Marino proper types saved and the types will not have changed as
2706e4b17023SJohn Marino their scopes will not have been re-entered. */
2707e4b17023SJohn Marino if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2708e4b17023SJohn Marino {
2709e4b17023SJohn Marino b->u.type = TREE_TYPE (b->decl);
2710e4b17023SJohn Marino type_saved = true;
2711e4b17023SJohn Marino }
2712e4b17023SJohn Marino if (B_IN_FILE_SCOPE (b)
2713e4b17023SJohn Marino && TREE_CODE (b->decl) == VAR_DECL
2714e4b17023SJohn Marino && TREE_STATIC (b->decl)
2715e4b17023SJohn Marino && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2716e4b17023SJohn Marino && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2717e4b17023SJohn Marino && TREE_CODE (type) == ARRAY_TYPE
2718e4b17023SJohn Marino && TYPE_DOMAIN (type)
2719e4b17023SJohn Marino && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2720e4b17023SJohn Marino && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2721e4b17023SJohn Marino {
2722e4b17023SJohn Marino /* Array type completed in inner scope, which should be
2723e4b17023SJohn Marino diagnosed if the completion does not have size 1 and
2724e4b17023SJohn Marino it does not get completed in the file scope. */
2725e4b17023SJohn Marino b->inner_comp = true;
2726e4b17023SJohn Marino }
2727e4b17023SJohn Marino b = b->shadowed;
2728e4b17023SJohn Marino }
2729e4b17023SJohn Marino
2730e4b17023SJohn Marino /* If a matching external declaration has been found, set its
2731e4b17023SJohn Marino type to the composite of all the types of that declaration.
2732e4b17023SJohn Marino After the consistency checks, it will be reset to the
2733e4b17023SJohn Marino composite of the visible types only. */
2734e4b17023SJohn Marino if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2735e4b17023SJohn Marino && b->u.type)
2736e4b17023SJohn Marino TREE_TYPE (b->decl) = b->u.type;
2737e4b17023SJohn Marino
2738e4b17023SJohn Marino /* The point of the same_translation_unit_p check here is,
2739e4b17023SJohn Marino we want to detect a duplicate decl for a construct like
2740e4b17023SJohn Marino foo() { extern bar(); } ... static bar(); but not if
2741e4b17023SJohn Marino they are in different translation units. In any case,
2742e4b17023SJohn Marino the static does not go in the externals scope. */
2743e4b17023SJohn Marino if (b
2744e4b17023SJohn Marino && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2745e4b17023SJohn Marino && duplicate_decls (x, b->decl))
2746e4b17023SJohn Marino {
2747e4b17023SJohn Marino tree thistype;
2748e4b17023SJohn Marino if (vistype)
2749e4b17023SJohn Marino {
2750e4b17023SJohn Marino if (comptypes (vistype, type))
2751e4b17023SJohn Marino thistype = composite_type (vistype, type);
2752e4b17023SJohn Marino else
2753e4b17023SJohn Marino thistype = TREE_TYPE (b->decl);
2754e4b17023SJohn Marino }
2755e4b17023SJohn Marino else
2756e4b17023SJohn Marino thistype = type;
2757e4b17023SJohn Marino b->u.type = TREE_TYPE (b->decl);
2758e4b17023SJohn Marino if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2759e4b17023SJohn Marino thistype
2760e4b17023SJohn Marino = build_type_attribute_variant (thistype,
2761e4b17023SJohn Marino TYPE_ATTRIBUTES (b->u.type));
2762e4b17023SJohn Marino TREE_TYPE (b->decl) = thistype;
2763e4b17023SJohn Marino bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true,
2764e4b17023SJohn Marino locus);
2765e4b17023SJohn Marino return b->decl;
2766e4b17023SJohn Marino }
2767e4b17023SJohn Marino else if (TREE_PUBLIC (x))
2768e4b17023SJohn Marino {
2769e4b17023SJohn Marino if (visdecl && !b && duplicate_decls (x, visdecl))
2770e4b17023SJohn Marino {
2771e4b17023SJohn Marino /* An external declaration at block scope referring to a
2772e4b17023SJohn Marino visible entity with internal linkage. The composite
2773e4b17023SJohn Marino type will already be correct for this scope, so we
2774e4b17023SJohn Marino just need to fall through to make the declaration in
2775e4b17023SJohn Marino this scope. */
2776e4b17023SJohn Marino nested = true;
2777e4b17023SJohn Marino x = visdecl;
2778e4b17023SJohn Marino }
2779e4b17023SJohn Marino else
2780e4b17023SJohn Marino {
2781e4b17023SJohn Marino bind (name, x, external_scope, /*invisible=*/true,
2782e4b17023SJohn Marino /*nested=*/false, locus);
2783e4b17023SJohn Marino nested = true;
2784e4b17023SJohn Marino }
2785e4b17023SJohn Marino }
2786e4b17023SJohn Marino }
2787e4b17023SJohn Marino
2788e4b17023SJohn Marino if (TREE_CODE (x) != PARM_DECL)
2789e4b17023SJohn Marino warn_if_shadowing (x);
2790e4b17023SJohn Marino
2791e4b17023SJohn Marino skip_external_and_shadow_checks:
2792e4b17023SJohn Marino if (TREE_CODE (x) == TYPE_DECL)
2793e4b17023SJohn Marino {
2794e4b17023SJohn Marino /* So this is a typedef, set its underlying type. */
2795e4b17023SJohn Marino set_underlying_type (x);
2796e4b17023SJohn Marino
2797e4b17023SJohn Marino /* If X is a typedef defined in the current function, record it
2798e4b17023SJohn Marino for the purpose of implementing the -Wunused-local-typedefs
2799e4b17023SJohn Marino warning. */
2800e4b17023SJohn Marino record_locally_defined_typedef (x);
2801e4b17023SJohn Marino }
2802e4b17023SJohn Marino
2803e4b17023SJohn Marino bind (name, x, scope, /*invisible=*/false, nested, locus);
2804e4b17023SJohn Marino
2805e4b17023SJohn Marino /* If x's type is incomplete because it's based on a
2806e4b17023SJohn Marino structure or union which has not yet been fully declared,
2807e4b17023SJohn Marino attach it to that structure or union type, so we can go
2808e4b17023SJohn Marino back and complete the variable declaration later, if the
2809e4b17023SJohn Marino structure or union gets fully declared.
2810e4b17023SJohn Marino
2811e4b17023SJohn Marino If the input is erroneous, we can have error_mark in the type
2812e4b17023SJohn Marino slot (e.g. "f(void a, ...)") - that doesn't count as an
2813e4b17023SJohn Marino incomplete type. */
2814e4b17023SJohn Marino if (TREE_TYPE (x) != error_mark_node
2815e4b17023SJohn Marino && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2816e4b17023SJohn Marino {
2817e4b17023SJohn Marino tree element = TREE_TYPE (x);
2818e4b17023SJohn Marino
2819e4b17023SJohn Marino while (TREE_CODE (element) == ARRAY_TYPE)
2820e4b17023SJohn Marino element = TREE_TYPE (element);
2821e4b17023SJohn Marino element = TYPE_MAIN_VARIANT (element);
2822e4b17023SJohn Marino
2823e4b17023SJohn Marino if ((TREE_CODE (element) == RECORD_TYPE
2824e4b17023SJohn Marino || TREE_CODE (element) == UNION_TYPE)
2825e4b17023SJohn Marino && (TREE_CODE (x) != TYPE_DECL
2826e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2827e4b17023SJohn Marino && !COMPLETE_TYPE_P (element))
2828e4b17023SJohn Marino C_TYPE_INCOMPLETE_VARS (element)
2829e4b17023SJohn Marino = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2830e4b17023SJohn Marino }
2831e4b17023SJohn Marino return x;
2832e4b17023SJohn Marino }
2833e4b17023SJohn Marino
2834e4b17023SJohn Marino /* Record X as belonging to file scope.
2835e4b17023SJohn Marino This is used only internally by the Objective-C front end,
2836e4b17023SJohn Marino and is limited to its needs. duplicate_decls is not called;
2837e4b17023SJohn Marino if there is any preexisting decl for this identifier, it is an ICE. */
2838e4b17023SJohn Marino
2839e4b17023SJohn Marino tree
pushdecl_top_level(tree x)2840e4b17023SJohn Marino pushdecl_top_level (tree x)
2841e4b17023SJohn Marino {
2842e4b17023SJohn Marino tree name;
2843e4b17023SJohn Marino bool nested = false;
2844e4b17023SJohn Marino gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2845e4b17023SJohn Marino
2846e4b17023SJohn Marino name = DECL_NAME (x);
2847e4b17023SJohn Marino
2848e4b17023SJohn Marino gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2849e4b17023SJohn Marino
2850e4b17023SJohn Marino if (TREE_PUBLIC (x))
2851e4b17023SJohn Marino {
2852e4b17023SJohn Marino bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false,
2853e4b17023SJohn Marino UNKNOWN_LOCATION);
2854e4b17023SJohn Marino nested = true;
2855e4b17023SJohn Marino }
2856e4b17023SJohn Marino if (file_scope)
2857e4b17023SJohn Marino bind (name, x, file_scope, /*invisible=*/false, nested, UNKNOWN_LOCATION);
2858e4b17023SJohn Marino
2859e4b17023SJohn Marino return x;
2860e4b17023SJohn Marino }
2861e4b17023SJohn Marino
2862e4b17023SJohn Marino static void
implicit_decl_warning(tree id,tree olddecl)2863e4b17023SJohn Marino implicit_decl_warning (tree id, tree olddecl)
2864e4b17023SJohn Marino {
2865e4b17023SJohn Marino if (warn_implicit_function_declaration)
2866e4b17023SJohn Marino {
2867e4b17023SJohn Marino bool warned;
2868e4b17023SJohn Marino
2869e4b17023SJohn Marino if (flag_isoc99)
2870e4b17023SJohn Marino warned = pedwarn (input_location, OPT_Wimplicit_function_declaration,
2871e4b17023SJohn Marino "implicit declaration of function %qE", id);
2872e4b17023SJohn Marino else
2873e4b17023SJohn Marino warned = warning (OPT_Wimplicit_function_declaration,
2874e4b17023SJohn Marino G_("implicit declaration of function %qE"), id);
2875e4b17023SJohn Marino if (olddecl && warned)
2876e4b17023SJohn Marino locate_old_decl (olddecl);
2877e4b17023SJohn Marino }
2878e4b17023SJohn Marino }
2879e4b17023SJohn Marino
2880e4b17023SJohn Marino /* Generate an implicit declaration for identifier FUNCTIONID at LOC as a
2881e4b17023SJohn Marino function of type int (). */
2882e4b17023SJohn Marino
2883e4b17023SJohn Marino tree
implicitly_declare(location_t loc,tree functionid)2884e4b17023SJohn Marino implicitly_declare (location_t loc, tree functionid)
2885e4b17023SJohn Marino {
2886e4b17023SJohn Marino struct c_binding *b;
2887e4b17023SJohn Marino tree decl = 0;
2888e4b17023SJohn Marino tree asmspec_tree;
2889e4b17023SJohn Marino
2890e4b17023SJohn Marino for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2891e4b17023SJohn Marino {
2892e4b17023SJohn Marino if (B_IN_SCOPE (b, external_scope))
2893e4b17023SJohn Marino {
2894e4b17023SJohn Marino decl = b->decl;
2895e4b17023SJohn Marino break;
2896e4b17023SJohn Marino }
2897e4b17023SJohn Marino }
2898e4b17023SJohn Marino
2899e4b17023SJohn Marino if (decl)
2900e4b17023SJohn Marino {
2901e4b17023SJohn Marino if (decl == error_mark_node)
2902e4b17023SJohn Marino return decl;
2903e4b17023SJohn Marino
2904e4b17023SJohn Marino /* FIXME: Objective-C has weird not-really-builtin functions
2905e4b17023SJohn Marino which are supposed to be visible automatically. They wind up
2906e4b17023SJohn Marino in the external scope because they're pushed before the file
2907e4b17023SJohn Marino scope gets created. Catch this here and rebind them into the
2908e4b17023SJohn Marino file scope. */
2909e4b17023SJohn Marino if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2910e4b17023SJohn Marino {
2911e4b17023SJohn Marino bind (functionid, decl, file_scope,
2912e4b17023SJohn Marino /*invisible=*/false, /*nested=*/true,
2913e4b17023SJohn Marino DECL_SOURCE_LOCATION (decl));
2914e4b17023SJohn Marino return decl;
2915e4b17023SJohn Marino }
2916e4b17023SJohn Marino else
2917e4b17023SJohn Marino {
2918e4b17023SJohn Marino tree newtype = default_function_type;
2919e4b17023SJohn Marino if (b->u.type)
2920e4b17023SJohn Marino TREE_TYPE (decl) = b->u.type;
2921e4b17023SJohn Marino /* Implicit declaration of a function already declared
2922e4b17023SJohn Marino (somehow) in a different scope, or as a built-in.
2923e4b17023SJohn Marino If this is the first time this has happened, warn;
2924e4b17023SJohn Marino then recycle the old declaration but with the new type. */
2925e4b17023SJohn Marino if (!C_DECL_IMPLICIT (decl))
2926e4b17023SJohn Marino {
2927e4b17023SJohn Marino implicit_decl_warning (functionid, decl);
2928e4b17023SJohn Marino C_DECL_IMPLICIT (decl) = 1;
2929e4b17023SJohn Marino }
2930e4b17023SJohn Marino if (DECL_BUILT_IN (decl))
2931e4b17023SJohn Marino {
2932e4b17023SJohn Marino newtype = build_type_attribute_variant (newtype,
2933e4b17023SJohn Marino TYPE_ATTRIBUTES
2934e4b17023SJohn Marino (TREE_TYPE (decl)));
2935e4b17023SJohn Marino if (!comptypes (newtype, TREE_TYPE (decl)))
2936e4b17023SJohn Marino {
2937e4b17023SJohn Marino warning_at (loc, 0, "incompatible implicit declaration of "
2938e4b17023SJohn Marino "built-in function %qD", decl);
2939e4b17023SJohn Marino newtype = TREE_TYPE (decl);
2940e4b17023SJohn Marino }
2941e4b17023SJohn Marino }
2942e4b17023SJohn Marino else
2943e4b17023SJohn Marino {
2944e4b17023SJohn Marino if (!comptypes (newtype, TREE_TYPE (decl)))
2945e4b17023SJohn Marino {
2946e4b17023SJohn Marino error_at (loc, "incompatible implicit declaration of function %qD", decl);
2947e4b17023SJohn Marino locate_old_decl (decl);
2948e4b17023SJohn Marino }
2949e4b17023SJohn Marino }
2950e4b17023SJohn Marino b->u.type = TREE_TYPE (decl);
2951e4b17023SJohn Marino TREE_TYPE (decl) = newtype;
2952e4b17023SJohn Marino bind (functionid, decl, current_scope,
2953e4b17023SJohn Marino /*invisible=*/false, /*nested=*/true,
2954e4b17023SJohn Marino DECL_SOURCE_LOCATION (decl));
2955e4b17023SJohn Marino return decl;
2956e4b17023SJohn Marino }
2957e4b17023SJohn Marino }
2958e4b17023SJohn Marino
2959e4b17023SJohn Marino /* Not seen before. */
2960e4b17023SJohn Marino decl = build_decl (loc, FUNCTION_DECL, functionid, default_function_type);
2961e4b17023SJohn Marino DECL_EXTERNAL (decl) = 1;
2962e4b17023SJohn Marino TREE_PUBLIC (decl) = 1;
2963e4b17023SJohn Marino C_DECL_IMPLICIT (decl) = 1;
2964e4b17023SJohn Marino implicit_decl_warning (functionid, 0);
2965e4b17023SJohn Marino asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2966e4b17023SJohn Marino if (asmspec_tree)
2967e4b17023SJohn Marino set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2968e4b17023SJohn Marino
2969e4b17023SJohn Marino /* C89 says implicit declarations are in the innermost block.
2970e4b17023SJohn Marino So we record the decl in the standard fashion. */
2971e4b17023SJohn Marino decl = pushdecl (decl);
2972e4b17023SJohn Marino
2973e4b17023SJohn Marino /* No need to call objc_check_decl here - it's a function type. */
2974e4b17023SJohn Marino rest_of_decl_compilation (decl, 0, 0);
2975e4b17023SJohn Marino
2976e4b17023SJohn Marino /* Write a record describing this implicit function declaration
2977e4b17023SJohn Marino to the prototypes file (if requested). */
2978e4b17023SJohn Marino gen_aux_info_record (decl, 0, 1, 0);
2979e4b17023SJohn Marino
2980e4b17023SJohn Marino /* Possibly apply some default attributes to this implicit declaration. */
2981e4b17023SJohn Marino decl_attributes (&decl, NULL_TREE, 0);
2982e4b17023SJohn Marino
2983e4b17023SJohn Marino return decl;
2984e4b17023SJohn Marino }
2985e4b17023SJohn Marino
2986e4b17023SJohn Marino /* Issue an error message for a reference to an undeclared variable
2987e4b17023SJohn Marino ID, including a reference to a builtin outside of function-call
2988e4b17023SJohn Marino context. Establish a binding of the identifier to error_mark_node
2989e4b17023SJohn Marino in an appropriate scope, which will suppress further errors for the
2990e4b17023SJohn Marino same identifier. The error message should be given location LOC. */
2991e4b17023SJohn Marino void
undeclared_variable(location_t loc,tree id)2992e4b17023SJohn Marino undeclared_variable (location_t loc, tree id)
2993e4b17023SJohn Marino {
2994e4b17023SJohn Marino static bool already = false;
2995e4b17023SJohn Marino struct c_scope *scope;
2996e4b17023SJohn Marino
2997e4b17023SJohn Marino if (current_function_decl == 0)
2998e4b17023SJohn Marino {
2999e4b17023SJohn Marino error_at (loc, "%qE undeclared here (not in a function)", id);
3000e4b17023SJohn Marino scope = current_scope;
3001e4b17023SJohn Marino }
3002e4b17023SJohn Marino else
3003e4b17023SJohn Marino {
3004e4b17023SJohn Marino if (!objc_diagnose_private_ivar (id))
3005e4b17023SJohn Marino error_at (loc, "%qE undeclared (first use in this function)", id);
3006e4b17023SJohn Marino if (!already)
3007e4b17023SJohn Marino {
3008e4b17023SJohn Marino inform (loc, "each undeclared identifier is reported only"
3009e4b17023SJohn Marino " once for each function it appears in");
3010e4b17023SJohn Marino already = true;
3011e4b17023SJohn Marino }
3012e4b17023SJohn Marino
3013e4b17023SJohn Marino /* If we are parsing old-style parameter decls, current_function_decl
3014e4b17023SJohn Marino will be nonnull but current_function_scope will be null. */
3015e4b17023SJohn Marino scope = current_function_scope ? current_function_scope : current_scope;
3016e4b17023SJohn Marino }
3017e4b17023SJohn Marino bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false,
3018e4b17023SJohn Marino UNKNOWN_LOCATION);
3019e4b17023SJohn Marino }
3020e4b17023SJohn Marino
3021e4b17023SJohn Marino /* Subroutine of lookup_label, declare_label, define_label: construct a
3022e4b17023SJohn Marino LABEL_DECL with all the proper frills. Also create a struct
3023e4b17023SJohn Marino c_label_vars initialized for the current scope. */
3024e4b17023SJohn Marino
3025e4b17023SJohn Marino static tree
make_label(location_t location,tree name,bool defining,struct c_label_vars ** p_label_vars)3026e4b17023SJohn Marino make_label (location_t location, tree name, bool defining,
3027e4b17023SJohn Marino struct c_label_vars **p_label_vars)
3028e4b17023SJohn Marino {
3029e4b17023SJohn Marino tree label = build_decl (location, LABEL_DECL, name, void_type_node);
3030e4b17023SJohn Marino struct c_label_vars *label_vars;
3031e4b17023SJohn Marino
3032e4b17023SJohn Marino DECL_CONTEXT (label) = current_function_decl;
3033e4b17023SJohn Marino DECL_MODE (label) = VOIDmode;
3034e4b17023SJohn Marino
3035e4b17023SJohn Marino label_vars = ggc_alloc_c_label_vars ();
3036e4b17023SJohn Marino label_vars->shadowed = NULL;
3037e4b17023SJohn Marino set_spot_bindings (&label_vars->label_bindings, defining);
3038e4b17023SJohn Marino label_vars->decls_in_scope = make_tree_vector ();
3039e4b17023SJohn Marino label_vars->gotos = VEC_alloc (c_goto_bindings_p, gc, 0);
3040e4b17023SJohn Marino *p_label_vars = label_vars;
3041e4b17023SJohn Marino
3042e4b17023SJohn Marino return label;
3043e4b17023SJohn Marino }
3044e4b17023SJohn Marino
3045e4b17023SJohn Marino /* Get the LABEL_DECL corresponding to identifier NAME as a label.
3046e4b17023SJohn Marino Create one if none exists so far for the current function.
3047e4b17023SJohn Marino This is called when a label is used in a goto expression or
3048e4b17023SJohn Marino has its address taken. */
3049e4b17023SJohn Marino
3050e4b17023SJohn Marino tree
lookup_label(tree name)3051e4b17023SJohn Marino lookup_label (tree name)
3052e4b17023SJohn Marino {
3053e4b17023SJohn Marino tree label;
3054e4b17023SJohn Marino struct c_label_vars *label_vars;
3055e4b17023SJohn Marino
3056e4b17023SJohn Marino if (current_function_scope == 0)
3057e4b17023SJohn Marino {
3058e4b17023SJohn Marino error ("label %qE referenced outside of any function", name);
3059e4b17023SJohn Marino return 0;
3060e4b17023SJohn Marino }
3061e4b17023SJohn Marino
3062e4b17023SJohn Marino /* Use a label already defined or ref'd with this name, but not if
3063e4b17023SJohn Marino it is inherited from a containing function and wasn't declared
3064e4b17023SJohn Marino using __label__. */
3065e4b17023SJohn Marino label = I_LABEL_DECL (name);
3066e4b17023SJohn Marino if (label && (DECL_CONTEXT (label) == current_function_decl
3067e4b17023SJohn Marino || C_DECLARED_LABEL_FLAG (label)))
3068e4b17023SJohn Marino {
3069e4b17023SJohn Marino /* If the label has only been declared, update its apparent
3070e4b17023SJohn Marino location to point here, for better diagnostics if it
3071e4b17023SJohn Marino turns out not to have been defined. */
3072e4b17023SJohn Marino if (DECL_INITIAL (label) == NULL_TREE)
3073e4b17023SJohn Marino DECL_SOURCE_LOCATION (label) = input_location;
3074e4b17023SJohn Marino return label;
3075e4b17023SJohn Marino }
3076e4b17023SJohn Marino
3077e4b17023SJohn Marino /* No label binding for that identifier; make one. */
3078e4b17023SJohn Marino label = make_label (input_location, name, false, &label_vars);
3079e4b17023SJohn Marino
3080e4b17023SJohn Marino /* Ordinary labels go in the current function scope. */
3081e4b17023SJohn Marino bind_label (name, label, current_function_scope, label_vars);
3082e4b17023SJohn Marino
3083e4b17023SJohn Marino return label;
3084e4b17023SJohn Marino }
3085e4b17023SJohn Marino
3086e4b17023SJohn Marino /* Issue a warning about DECL for a goto statement at GOTO_LOC going
3087e4b17023SJohn Marino to LABEL. */
3088e4b17023SJohn Marino
3089e4b17023SJohn Marino static void
warn_about_goto(location_t goto_loc,tree label,tree decl)3090e4b17023SJohn Marino warn_about_goto (location_t goto_loc, tree label, tree decl)
3091e4b17023SJohn Marino {
3092e4b17023SJohn Marino if (variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3093e4b17023SJohn Marino error_at (goto_loc,
3094e4b17023SJohn Marino "jump into scope of identifier with variably modified type");
3095e4b17023SJohn Marino else
3096e4b17023SJohn Marino warning_at (goto_loc, OPT_Wjump_misses_init,
3097e4b17023SJohn Marino "jump skips variable initialization");
3098e4b17023SJohn Marino inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3099e4b17023SJohn Marino inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3100e4b17023SJohn Marino }
3101e4b17023SJohn Marino
3102e4b17023SJohn Marino /* Look up a label because of a goto statement. This is like
3103e4b17023SJohn Marino lookup_label, but also issues any appropriate warnings. */
3104e4b17023SJohn Marino
3105e4b17023SJohn Marino tree
lookup_label_for_goto(location_t loc,tree name)3106e4b17023SJohn Marino lookup_label_for_goto (location_t loc, tree name)
3107e4b17023SJohn Marino {
3108e4b17023SJohn Marino tree label;
3109e4b17023SJohn Marino struct c_label_vars *label_vars;
3110e4b17023SJohn Marino unsigned int ix;
3111e4b17023SJohn Marino tree decl;
3112e4b17023SJohn Marino
3113e4b17023SJohn Marino label = lookup_label (name);
3114e4b17023SJohn Marino if (label == NULL_TREE)
3115e4b17023SJohn Marino return NULL_TREE;
3116e4b17023SJohn Marino
3117e4b17023SJohn Marino /* If we are jumping to a different function, we can't issue any
3118e4b17023SJohn Marino useful warnings. */
3119e4b17023SJohn Marino if (DECL_CONTEXT (label) != current_function_decl)
3120e4b17023SJohn Marino {
3121e4b17023SJohn Marino gcc_assert (C_DECLARED_LABEL_FLAG (label));
3122e4b17023SJohn Marino return label;
3123e4b17023SJohn Marino }
3124e4b17023SJohn Marino
3125e4b17023SJohn Marino label_vars = I_LABEL_BINDING (name)->u.label;
3126e4b17023SJohn Marino
3127e4b17023SJohn Marino /* If the label has not yet been defined, then push this goto on a
3128e4b17023SJohn Marino list for possible later warnings. */
3129e4b17023SJohn Marino if (label_vars->label_bindings.scope == NULL)
3130e4b17023SJohn Marino {
3131e4b17023SJohn Marino struct c_goto_bindings *g;
3132e4b17023SJohn Marino
3133e4b17023SJohn Marino g = ggc_alloc_c_goto_bindings ();
3134e4b17023SJohn Marino g->loc = loc;
3135e4b17023SJohn Marino set_spot_bindings (&g->goto_bindings, true);
3136e4b17023SJohn Marino VEC_safe_push (c_goto_bindings_p, gc, label_vars->gotos, g);
3137e4b17023SJohn Marino return label;
3138e4b17023SJohn Marino }
3139e4b17023SJohn Marino
3140e4b17023SJohn Marino /* If there are any decls in label_vars->decls_in_scope, then this
3141e4b17023SJohn Marino goto has missed the declaration of the decl. This happens for a
3142e4b17023SJohn Marino case like
3143e4b17023SJohn Marino int i = 1;
3144e4b17023SJohn Marino lab:
3145e4b17023SJohn Marino ...
3146e4b17023SJohn Marino goto lab;
3147e4b17023SJohn Marino Issue a warning or error. */
3148e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, label_vars->decls_in_scope, ix, decl)
3149e4b17023SJohn Marino warn_about_goto (loc, label, decl);
3150e4b17023SJohn Marino
3151e4b17023SJohn Marino if (label_vars->label_bindings.left_stmt_expr)
3152e4b17023SJohn Marino {
3153e4b17023SJohn Marino error_at (loc, "jump into statement expression");
3154e4b17023SJohn Marino inform (DECL_SOURCE_LOCATION (label), "label %qD defined here", label);
3155e4b17023SJohn Marino }
3156e4b17023SJohn Marino
3157e4b17023SJohn Marino return label;
3158e4b17023SJohn Marino }
3159e4b17023SJohn Marino
3160e4b17023SJohn Marino /* Make a label named NAME in the current function, shadowing silently
3161e4b17023SJohn Marino any that may be inherited from containing functions or containing
3162e4b17023SJohn Marino scopes. This is called for __label__ declarations. */
3163e4b17023SJohn Marino
3164e4b17023SJohn Marino tree
declare_label(tree name)3165e4b17023SJohn Marino declare_label (tree name)
3166e4b17023SJohn Marino {
3167e4b17023SJohn Marino struct c_binding *b = I_LABEL_BINDING (name);
3168e4b17023SJohn Marino tree label;
3169e4b17023SJohn Marino struct c_label_vars *label_vars;
3170e4b17023SJohn Marino
3171e4b17023SJohn Marino /* Check to make sure that the label hasn't already been declared
3172e4b17023SJohn Marino at this scope */
3173e4b17023SJohn Marino if (b && B_IN_CURRENT_SCOPE (b))
3174e4b17023SJohn Marino {
3175e4b17023SJohn Marino error ("duplicate label declaration %qE", name);
3176e4b17023SJohn Marino locate_old_decl (b->decl);
3177e4b17023SJohn Marino
3178e4b17023SJohn Marino /* Just use the previous declaration. */
3179e4b17023SJohn Marino return b->decl;
3180e4b17023SJohn Marino }
3181e4b17023SJohn Marino
3182e4b17023SJohn Marino label = make_label (input_location, name, false, &label_vars);
3183e4b17023SJohn Marino C_DECLARED_LABEL_FLAG (label) = 1;
3184e4b17023SJohn Marino
3185e4b17023SJohn Marino /* Declared labels go in the current scope. */
3186e4b17023SJohn Marino bind_label (name, label, current_scope, label_vars);
3187e4b17023SJohn Marino
3188e4b17023SJohn Marino return label;
3189e4b17023SJohn Marino }
3190e4b17023SJohn Marino
3191e4b17023SJohn Marino /* When we define a label, issue any appropriate warnings if there are
3192e4b17023SJohn Marino any gotos earlier in the function which jump to this label. */
3193e4b17023SJohn Marino
3194e4b17023SJohn Marino static void
check_earlier_gotos(tree label,struct c_label_vars * label_vars)3195e4b17023SJohn Marino check_earlier_gotos (tree label, struct c_label_vars* label_vars)
3196e4b17023SJohn Marino {
3197e4b17023SJohn Marino unsigned int ix;
3198e4b17023SJohn Marino struct c_goto_bindings *g;
3199e4b17023SJohn Marino
3200e4b17023SJohn Marino FOR_EACH_VEC_ELT (c_goto_bindings_p, label_vars->gotos, ix, g)
3201e4b17023SJohn Marino {
3202e4b17023SJohn Marino struct c_binding *b;
3203e4b17023SJohn Marino struct c_scope *scope;
3204e4b17023SJohn Marino
3205e4b17023SJohn Marino /* We have a goto to this label. The goto is going forward. In
3206e4b17023SJohn Marino g->scope, the goto is going to skip any binding which was
3207e4b17023SJohn Marino defined after g->bindings_in_scope. */
3208e4b17023SJohn Marino if (g->goto_bindings.scope->has_jump_unsafe_decl)
3209e4b17023SJohn Marino {
3210e4b17023SJohn Marino for (b = g->goto_bindings.scope->bindings;
3211e4b17023SJohn Marino b != g->goto_bindings.bindings_in_scope;
3212e4b17023SJohn Marino b = b->prev)
3213e4b17023SJohn Marino {
3214e4b17023SJohn Marino if (decl_jump_unsafe (b->decl))
3215e4b17023SJohn Marino warn_about_goto (g->loc, label, b->decl);
3216e4b17023SJohn Marino }
3217e4b17023SJohn Marino }
3218e4b17023SJohn Marino
3219e4b17023SJohn Marino /* We also need to warn about decls defined in any scopes
3220e4b17023SJohn Marino between the scope of the label and the scope of the goto. */
3221e4b17023SJohn Marino for (scope = label_vars->label_bindings.scope;
3222e4b17023SJohn Marino scope != g->goto_bindings.scope;
3223e4b17023SJohn Marino scope = scope->outer)
3224e4b17023SJohn Marino {
3225e4b17023SJohn Marino gcc_assert (scope != NULL);
3226e4b17023SJohn Marino if (scope->has_jump_unsafe_decl)
3227e4b17023SJohn Marino {
3228e4b17023SJohn Marino if (scope == label_vars->label_bindings.scope)
3229e4b17023SJohn Marino b = label_vars->label_bindings.bindings_in_scope;
3230e4b17023SJohn Marino else
3231e4b17023SJohn Marino b = scope->bindings;
3232e4b17023SJohn Marino for (; b != NULL; b = b->prev)
3233e4b17023SJohn Marino {
3234e4b17023SJohn Marino if (decl_jump_unsafe (b->decl))
3235e4b17023SJohn Marino warn_about_goto (g->loc, label, b->decl);
3236e4b17023SJohn Marino }
3237e4b17023SJohn Marino }
3238e4b17023SJohn Marino }
3239e4b17023SJohn Marino
3240e4b17023SJohn Marino if (g->goto_bindings.stmt_exprs > 0)
3241e4b17023SJohn Marino {
3242e4b17023SJohn Marino error_at (g->loc, "jump into statement expression");
3243e4b17023SJohn Marino inform (DECL_SOURCE_LOCATION (label), "label %qD defined here",
3244e4b17023SJohn Marino label);
3245e4b17023SJohn Marino }
3246e4b17023SJohn Marino }
3247e4b17023SJohn Marino
3248e4b17023SJohn Marino /* Now that the label is defined, we will issue warnings about
3249e4b17023SJohn Marino subsequent gotos to this label when we see them. */
3250e4b17023SJohn Marino VEC_truncate (c_goto_bindings_p, label_vars->gotos, 0);
3251e4b17023SJohn Marino label_vars->gotos = NULL;
3252e4b17023SJohn Marino }
3253e4b17023SJohn Marino
3254e4b17023SJohn Marino /* Define a label, specifying the location in the source file.
3255e4b17023SJohn Marino Return the LABEL_DECL node for the label, if the definition is valid.
3256e4b17023SJohn Marino Otherwise return 0. */
3257e4b17023SJohn Marino
3258e4b17023SJohn Marino tree
define_label(location_t location,tree name)3259e4b17023SJohn Marino define_label (location_t location, tree name)
3260e4b17023SJohn Marino {
3261e4b17023SJohn Marino /* Find any preexisting label with this name. It is an error
3262e4b17023SJohn Marino if that label has already been defined in this function, or
3263e4b17023SJohn Marino if there is a containing function with a declared label with
3264e4b17023SJohn Marino the same name. */
3265e4b17023SJohn Marino tree label = I_LABEL_DECL (name);
3266e4b17023SJohn Marino
3267e4b17023SJohn Marino if (label
3268e4b17023SJohn Marino && ((DECL_CONTEXT (label) == current_function_decl
3269e4b17023SJohn Marino && DECL_INITIAL (label) != 0)
3270e4b17023SJohn Marino || (DECL_CONTEXT (label) != current_function_decl
3271e4b17023SJohn Marino && C_DECLARED_LABEL_FLAG (label))))
3272e4b17023SJohn Marino {
3273e4b17023SJohn Marino error_at (location, "duplicate label %qD", label);
3274e4b17023SJohn Marino locate_old_decl (label);
3275e4b17023SJohn Marino return 0;
3276e4b17023SJohn Marino }
3277e4b17023SJohn Marino else if (label && DECL_CONTEXT (label) == current_function_decl)
3278e4b17023SJohn Marino {
3279e4b17023SJohn Marino struct c_label_vars *label_vars = I_LABEL_BINDING (name)->u.label;
3280e4b17023SJohn Marino
3281e4b17023SJohn Marino /* The label has been used or declared already in this function,
3282e4b17023SJohn Marino but not defined. Update its location to point to this
3283e4b17023SJohn Marino definition. */
3284e4b17023SJohn Marino DECL_SOURCE_LOCATION (label) = location;
3285e4b17023SJohn Marino set_spot_bindings (&label_vars->label_bindings, true);
3286e4b17023SJohn Marino
3287e4b17023SJohn Marino /* Issue warnings as required about any goto statements from
3288e4b17023SJohn Marino earlier in the function. */
3289e4b17023SJohn Marino check_earlier_gotos (label, label_vars);
3290e4b17023SJohn Marino }
3291e4b17023SJohn Marino else
3292e4b17023SJohn Marino {
3293e4b17023SJohn Marino struct c_label_vars *label_vars;
3294e4b17023SJohn Marino
3295e4b17023SJohn Marino /* No label binding for that identifier; make one. */
3296e4b17023SJohn Marino label = make_label (location, name, true, &label_vars);
3297e4b17023SJohn Marino
3298e4b17023SJohn Marino /* Ordinary labels go in the current function scope. */
3299e4b17023SJohn Marino bind_label (name, label, current_function_scope, label_vars);
3300e4b17023SJohn Marino }
3301e4b17023SJohn Marino
3302e4b17023SJohn Marino if (!in_system_header && lookup_name (name))
3303e4b17023SJohn Marino warning_at (location, OPT_Wtraditional,
3304e4b17023SJohn Marino "traditional C lacks a separate namespace "
3305e4b17023SJohn Marino "for labels, identifier %qE conflicts", name);
3306e4b17023SJohn Marino
3307e4b17023SJohn Marino /* Mark label as having been defined. */
3308e4b17023SJohn Marino DECL_INITIAL (label) = error_mark_node;
3309e4b17023SJohn Marino return label;
3310e4b17023SJohn Marino }
3311e4b17023SJohn Marino
3312e4b17023SJohn Marino /* Get the bindings for a new switch statement. This is used to issue
3313e4b17023SJohn Marino warnings as appropriate for jumps from the switch to case or
3314e4b17023SJohn Marino default labels. */
3315e4b17023SJohn Marino
3316e4b17023SJohn Marino struct c_spot_bindings *
c_get_switch_bindings(void)3317e4b17023SJohn Marino c_get_switch_bindings (void)
3318e4b17023SJohn Marino {
3319e4b17023SJohn Marino struct c_spot_bindings *switch_bindings;
3320e4b17023SJohn Marino
3321e4b17023SJohn Marino switch_bindings = XNEW (struct c_spot_bindings);
3322e4b17023SJohn Marino set_spot_bindings (switch_bindings, true);
3323e4b17023SJohn Marino return switch_bindings;
3324e4b17023SJohn Marino }
3325e4b17023SJohn Marino
3326e4b17023SJohn Marino void
c_release_switch_bindings(struct c_spot_bindings * bindings)3327e4b17023SJohn Marino c_release_switch_bindings (struct c_spot_bindings *bindings)
3328e4b17023SJohn Marino {
3329e4b17023SJohn Marino gcc_assert (bindings->stmt_exprs == 0 && !bindings->left_stmt_expr);
3330e4b17023SJohn Marino XDELETE (bindings);
3331e4b17023SJohn Marino }
3332e4b17023SJohn Marino
3333e4b17023SJohn Marino /* This is called at the point of a case or default label to issue
3334e4b17023SJohn Marino warnings about decls as needed. It returns true if it found an
3335e4b17023SJohn Marino error, not just a warning. */
3336e4b17023SJohn Marino
3337e4b17023SJohn Marino bool
c_check_switch_jump_warnings(struct c_spot_bindings * switch_bindings,location_t switch_loc,location_t case_loc)3338e4b17023SJohn Marino c_check_switch_jump_warnings (struct c_spot_bindings *switch_bindings,
3339e4b17023SJohn Marino location_t switch_loc, location_t case_loc)
3340e4b17023SJohn Marino {
3341e4b17023SJohn Marino bool saw_error;
3342e4b17023SJohn Marino struct c_scope *scope;
3343e4b17023SJohn Marino
3344e4b17023SJohn Marino saw_error = false;
3345e4b17023SJohn Marino for (scope = current_scope;
3346e4b17023SJohn Marino scope != switch_bindings->scope;
3347e4b17023SJohn Marino scope = scope->outer)
3348e4b17023SJohn Marino {
3349e4b17023SJohn Marino struct c_binding *b;
3350e4b17023SJohn Marino
3351e4b17023SJohn Marino gcc_assert (scope != NULL);
3352e4b17023SJohn Marino
3353e4b17023SJohn Marino if (!scope->has_jump_unsafe_decl)
3354e4b17023SJohn Marino continue;
3355e4b17023SJohn Marino
3356e4b17023SJohn Marino for (b = scope->bindings; b != NULL; b = b->prev)
3357e4b17023SJohn Marino {
3358e4b17023SJohn Marino if (decl_jump_unsafe (b->decl))
3359e4b17023SJohn Marino {
3360e4b17023SJohn Marino if (variably_modified_type_p (TREE_TYPE (b->decl), NULL_TREE))
3361e4b17023SJohn Marino {
3362e4b17023SJohn Marino saw_error = true;
3363e4b17023SJohn Marino error_at (case_loc,
3364e4b17023SJohn Marino ("switch jumps into scope of identifier with "
3365e4b17023SJohn Marino "variably modified type"));
3366e4b17023SJohn Marino }
3367e4b17023SJohn Marino else
3368e4b17023SJohn Marino warning_at (case_loc, OPT_Wjump_misses_init,
3369e4b17023SJohn Marino "switch jumps over variable initialization");
3370e4b17023SJohn Marino inform (switch_loc, "switch starts here");
3371e4b17023SJohn Marino inform (DECL_SOURCE_LOCATION (b->decl), "%qD declared here",
3372e4b17023SJohn Marino b->decl);
3373e4b17023SJohn Marino }
3374e4b17023SJohn Marino }
3375e4b17023SJohn Marino }
3376e4b17023SJohn Marino
3377e4b17023SJohn Marino if (switch_bindings->stmt_exprs > 0)
3378e4b17023SJohn Marino {
3379e4b17023SJohn Marino saw_error = true;
3380e4b17023SJohn Marino error_at (case_loc, "switch jumps into statement expression");
3381e4b17023SJohn Marino inform (switch_loc, "switch starts here");
3382e4b17023SJohn Marino }
3383e4b17023SJohn Marino
3384e4b17023SJohn Marino return saw_error;
3385e4b17023SJohn Marino }
3386e4b17023SJohn Marino
3387e4b17023SJohn Marino /* Given NAME, an IDENTIFIER_NODE,
3388e4b17023SJohn Marino return the structure (or union or enum) definition for that name.
3389e4b17023SJohn Marino If THISLEVEL_ONLY is nonzero, searches only the current_scope.
3390e4b17023SJohn Marino CODE says which kind of type the caller wants;
3391e4b17023SJohn Marino it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
3392e4b17023SJohn Marino If PLOC is not NULL and this returns non-null, it sets *PLOC to the
3393e4b17023SJohn Marino location where the tag was defined.
3394e4b17023SJohn Marino If the wrong kind of type is found, an error is reported. */
3395e4b17023SJohn Marino
3396e4b17023SJohn Marino static tree
lookup_tag(enum tree_code code,tree name,int thislevel_only,location_t * ploc)3397e4b17023SJohn Marino lookup_tag (enum tree_code code, tree name, int thislevel_only,
3398e4b17023SJohn Marino location_t *ploc)
3399e4b17023SJohn Marino {
3400e4b17023SJohn Marino struct c_binding *b = I_TAG_BINDING (name);
3401e4b17023SJohn Marino int thislevel = 0;
3402e4b17023SJohn Marino
3403e4b17023SJohn Marino if (!b || !b->decl)
3404e4b17023SJohn Marino return 0;
3405e4b17023SJohn Marino
3406e4b17023SJohn Marino /* We only care about whether it's in this level if
3407e4b17023SJohn Marino thislevel_only was set or it might be a type clash. */
3408e4b17023SJohn Marino if (thislevel_only || TREE_CODE (b->decl) != code)
3409e4b17023SJohn Marino {
3410e4b17023SJohn Marino /* For our purposes, a tag in the external scope is the same as
3411e4b17023SJohn Marino a tag in the file scope. (Primarily relevant to Objective-C
3412e4b17023SJohn Marino and its builtin structure tags, which get pushed before the
3413e4b17023SJohn Marino file scope is created.) */
3414e4b17023SJohn Marino if (B_IN_CURRENT_SCOPE (b)
3415e4b17023SJohn Marino || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
3416e4b17023SJohn Marino thislevel = 1;
3417e4b17023SJohn Marino }
3418e4b17023SJohn Marino
3419e4b17023SJohn Marino if (thislevel_only && !thislevel)
3420e4b17023SJohn Marino return 0;
3421e4b17023SJohn Marino
3422e4b17023SJohn Marino if (TREE_CODE (b->decl) != code)
3423e4b17023SJohn Marino {
3424e4b17023SJohn Marino /* Definition isn't the kind we were looking for. */
3425e4b17023SJohn Marino pending_invalid_xref = name;
3426e4b17023SJohn Marino pending_invalid_xref_location = input_location;
3427e4b17023SJohn Marino
3428e4b17023SJohn Marino /* If in the same binding level as a declaration as a tag
3429e4b17023SJohn Marino of a different type, this must not be allowed to
3430e4b17023SJohn Marino shadow that tag, so give the error immediately.
3431e4b17023SJohn Marino (For example, "struct foo; union foo;" is invalid.) */
3432e4b17023SJohn Marino if (thislevel)
3433e4b17023SJohn Marino pending_xref_error ();
3434e4b17023SJohn Marino }
3435e4b17023SJohn Marino
3436e4b17023SJohn Marino if (ploc != NULL)
3437e4b17023SJohn Marino *ploc = b->locus;
3438e4b17023SJohn Marino
3439e4b17023SJohn Marino return b->decl;
3440e4b17023SJohn Marino }
3441e4b17023SJohn Marino
3442e4b17023SJohn Marino /* Print an error message now
3443e4b17023SJohn Marino for a recent invalid struct, union or enum cross reference.
3444e4b17023SJohn Marino We don't print them immediately because they are not invalid
3445e4b17023SJohn Marino when used in the `struct foo;' construct for shadowing. */
3446e4b17023SJohn Marino
3447e4b17023SJohn Marino void
pending_xref_error(void)3448e4b17023SJohn Marino pending_xref_error (void)
3449e4b17023SJohn Marino {
3450e4b17023SJohn Marino if (pending_invalid_xref != 0)
3451e4b17023SJohn Marino error_at (pending_invalid_xref_location, "%qE defined as wrong kind of tag",
3452e4b17023SJohn Marino pending_invalid_xref);
3453e4b17023SJohn Marino pending_invalid_xref = 0;
3454e4b17023SJohn Marino }
3455e4b17023SJohn Marino
3456e4b17023SJohn Marino
3457e4b17023SJohn Marino /* Look up NAME in the current scope and its superiors
3458e4b17023SJohn Marino in the namespace of variables, functions and typedefs.
3459e4b17023SJohn Marino Return a ..._DECL node of some kind representing its definition,
3460e4b17023SJohn Marino or return 0 if it is undefined. */
3461e4b17023SJohn Marino
3462e4b17023SJohn Marino tree
lookup_name(tree name)3463e4b17023SJohn Marino lookup_name (tree name)
3464e4b17023SJohn Marino {
3465e4b17023SJohn Marino struct c_binding *b = I_SYMBOL_BINDING (name);
3466e4b17023SJohn Marino if (b && !b->invisible)
3467e4b17023SJohn Marino {
3468e4b17023SJohn Marino maybe_record_typedef_use (b->decl);
3469e4b17023SJohn Marino return b->decl;
3470e4b17023SJohn Marino }
3471e4b17023SJohn Marino return 0;
3472e4b17023SJohn Marino }
3473e4b17023SJohn Marino
3474e4b17023SJohn Marino /* Similar to `lookup_name' but look only at the indicated scope. */
3475e4b17023SJohn Marino
3476e4b17023SJohn Marino static tree
lookup_name_in_scope(tree name,struct c_scope * scope)3477e4b17023SJohn Marino lookup_name_in_scope (tree name, struct c_scope *scope)
3478e4b17023SJohn Marino {
3479e4b17023SJohn Marino struct c_binding *b;
3480e4b17023SJohn Marino
3481e4b17023SJohn Marino for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
3482e4b17023SJohn Marino if (B_IN_SCOPE (b, scope))
3483e4b17023SJohn Marino return b->decl;
3484e4b17023SJohn Marino return 0;
3485e4b17023SJohn Marino }
3486e4b17023SJohn Marino
3487e4b17023SJohn Marino /* Create the predefined scalar types of C,
3488e4b17023SJohn Marino and some nodes representing standard constants (0, 1, (void *) 0).
3489e4b17023SJohn Marino Initialize the global scope.
3490e4b17023SJohn Marino Make definitions for built-in primitive functions. */
3491e4b17023SJohn Marino
3492e4b17023SJohn Marino void
c_init_decl_processing(void)3493e4b17023SJohn Marino c_init_decl_processing (void)
3494e4b17023SJohn Marino {
3495e4b17023SJohn Marino location_t save_loc = input_location;
3496e4b17023SJohn Marino
3497e4b17023SJohn Marino /* Initialize reserved words for parser. */
3498e4b17023SJohn Marino c_parse_init ();
3499e4b17023SJohn Marino
3500e4b17023SJohn Marino current_function_decl = 0;
3501e4b17023SJohn Marino
3502e4b17023SJohn Marino gcc_obstack_init (&parser_obstack);
3503e4b17023SJohn Marino
3504e4b17023SJohn Marino /* Make the externals scope. */
3505e4b17023SJohn Marino push_scope ();
3506e4b17023SJohn Marino external_scope = current_scope;
3507e4b17023SJohn Marino
3508e4b17023SJohn Marino /* Declarations from c_common_nodes_and_builtins must not be associated
3509e4b17023SJohn Marino with this input file, lest we get differences between using and not
3510e4b17023SJohn Marino using preprocessed headers. */
3511e4b17023SJohn Marino input_location = BUILTINS_LOCATION;
3512e4b17023SJohn Marino
3513e4b17023SJohn Marino c_common_nodes_and_builtins ();
3514e4b17023SJohn Marino
3515e4b17023SJohn Marino /* In C, comparisons and TRUTH_* expressions have type int. */
3516e4b17023SJohn Marino truthvalue_type_node = integer_type_node;
3517e4b17023SJohn Marino truthvalue_true_node = integer_one_node;
3518e4b17023SJohn Marino truthvalue_false_node = integer_zero_node;
3519e4b17023SJohn Marino
3520e4b17023SJohn Marino /* Even in C99, which has a real boolean type. */
3521e4b17023SJohn Marino pushdecl (build_decl (UNKNOWN_LOCATION, TYPE_DECL, get_identifier ("_Bool"),
3522e4b17023SJohn Marino boolean_type_node));
3523e4b17023SJohn Marino
3524e4b17023SJohn Marino input_location = save_loc;
3525e4b17023SJohn Marino
3526e4b17023SJohn Marino pedantic_lvalues = true;
3527e4b17023SJohn Marino
3528e4b17023SJohn Marino make_fname_decl = c_make_fname_decl;
3529e4b17023SJohn Marino start_fname_decls ();
3530e4b17023SJohn Marino }
3531e4b17023SJohn Marino
3532e4b17023SJohn Marino /* Create the VAR_DECL at LOC for __FUNCTION__ etc. ID is the name to
3533e4b17023SJohn Marino give the decl, NAME is the initialization string and TYPE_DEP
3534e4b17023SJohn Marino indicates whether NAME depended on the type of the function. As we
3535e4b17023SJohn Marino don't yet implement delayed emission of static data, we mark the
3536e4b17023SJohn Marino decl as emitted so it is not placed in the output. Anything using
3537e4b17023SJohn Marino it must therefore pull out the STRING_CST initializer directly.
3538e4b17023SJohn Marino FIXME. */
3539e4b17023SJohn Marino
3540e4b17023SJohn Marino static tree
c_make_fname_decl(location_t loc,tree id,int type_dep)3541e4b17023SJohn Marino c_make_fname_decl (location_t loc, tree id, int type_dep)
3542e4b17023SJohn Marino {
3543e4b17023SJohn Marino const char *name = fname_as_string (type_dep);
3544e4b17023SJohn Marino tree decl, type, init;
3545e4b17023SJohn Marino size_t length = strlen (name);
3546e4b17023SJohn Marino
3547e4b17023SJohn Marino type = build_array_type (char_type_node,
3548e4b17023SJohn Marino build_index_type (size_int (length)));
3549e4b17023SJohn Marino type = c_build_qualified_type (type, TYPE_QUAL_CONST);
3550e4b17023SJohn Marino
3551e4b17023SJohn Marino decl = build_decl (loc, VAR_DECL, id, type);
3552e4b17023SJohn Marino
3553e4b17023SJohn Marino TREE_STATIC (decl) = 1;
3554e4b17023SJohn Marino TREE_READONLY (decl) = 1;
3555e4b17023SJohn Marino DECL_ARTIFICIAL (decl) = 1;
3556e4b17023SJohn Marino
3557e4b17023SJohn Marino init = build_string (length + 1, name);
3558e4b17023SJohn Marino free (CONST_CAST (char *, name));
3559e4b17023SJohn Marino TREE_TYPE (init) = type;
3560e4b17023SJohn Marino DECL_INITIAL (decl) = init;
3561e4b17023SJohn Marino
3562e4b17023SJohn Marino TREE_USED (decl) = 1;
3563e4b17023SJohn Marino
3564e4b17023SJohn Marino if (current_function_decl
3565e4b17023SJohn Marino /* For invalid programs like this:
3566e4b17023SJohn Marino
3567e4b17023SJohn Marino void foo()
3568e4b17023SJohn Marino const char* p = __FUNCTION__;
3569e4b17023SJohn Marino
3570e4b17023SJohn Marino the __FUNCTION__ is believed to appear in K&R style function
3571e4b17023SJohn Marino parameter declarator. In that case we still don't have
3572e4b17023SJohn Marino function_scope. */
3573e4b17023SJohn Marino && (!seen_error () || current_function_scope))
3574e4b17023SJohn Marino {
3575e4b17023SJohn Marino DECL_CONTEXT (decl) = current_function_decl;
3576e4b17023SJohn Marino bind (id, decl, current_function_scope,
3577e4b17023SJohn Marino /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
3578e4b17023SJohn Marino }
3579e4b17023SJohn Marino
3580e4b17023SJohn Marino finish_decl (decl, loc, init, NULL_TREE, NULL_TREE);
3581e4b17023SJohn Marino
3582e4b17023SJohn Marino return decl;
3583e4b17023SJohn Marino }
3584e4b17023SJohn Marino
3585e4b17023SJohn Marino tree
c_builtin_function(tree decl)3586e4b17023SJohn Marino c_builtin_function (tree decl)
3587e4b17023SJohn Marino {
3588e4b17023SJohn Marino tree type = TREE_TYPE (decl);
3589e4b17023SJohn Marino tree id = DECL_NAME (decl);
3590e4b17023SJohn Marino
3591e4b17023SJohn Marino const char *name = IDENTIFIER_POINTER (id);
3592e4b17023SJohn Marino C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3593e4b17023SJohn Marino
3594e4b17023SJohn Marino /* Should never be called on a symbol with a preexisting meaning. */
3595e4b17023SJohn Marino gcc_assert (!I_SYMBOL_BINDING (id));
3596e4b17023SJohn Marino
3597e4b17023SJohn Marino bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false,
3598e4b17023SJohn Marino UNKNOWN_LOCATION);
3599e4b17023SJohn Marino
3600e4b17023SJohn Marino /* Builtins in the implementation namespace are made visible without
3601e4b17023SJohn Marino needing to be explicitly declared. See push_file_scope. */
3602e4b17023SJohn Marino if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3603e4b17023SJohn Marino {
3604e4b17023SJohn Marino DECL_CHAIN (decl) = visible_builtins;
3605e4b17023SJohn Marino visible_builtins = decl;
3606e4b17023SJohn Marino }
3607e4b17023SJohn Marino
3608e4b17023SJohn Marino return decl;
3609e4b17023SJohn Marino }
3610e4b17023SJohn Marino
3611e4b17023SJohn Marino tree
c_builtin_function_ext_scope(tree decl)3612e4b17023SJohn Marino c_builtin_function_ext_scope (tree decl)
3613e4b17023SJohn Marino {
3614e4b17023SJohn Marino tree type = TREE_TYPE (decl);
3615e4b17023SJohn Marino tree id = DECL_NAME (decl);
3616e4b17023SJohn Marino
3617e4b17023SJohn Marino const char *name = IDENTIFIER_POINTER (id);
3618e4b17023SJohn Marino C_DECL_BUILTIN_PROTOTYPE (decl) = prototype_p (type);
3619e4b17023SJohn Marino
3620e4b17023SJohn Marino /* Should never be called on a symbol with a preexisting meaning. */
3621e4b17023SJohn Marino gcc_assert (!I_SYMBOL_BINDING (id));
3622e4b17023SJohn Marino
3623e4b17023SJohn Marino bind (id, decl, external_scope, /*invisible=*/false, /*nested=*/false,
3624e4b17023SJohn Marino UNKNOWN_LOCATION);
3625e4b17023SJohn Marino
3626e4b17023SJohn Marino /* Builtins in the implementation namespace are made visible without
3627e4b17023SJohn Marino needing to be explicitly declared. See push_file_scope. */
3628e4b17023SJohn Marino if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
3629e4b17023SJohn Marino {
3630e4b17023SJohn Marino DECL_CHAIN (decl) = visible_builtins;
3631e4b17023SJohn Marino visible_builtins = decl;
3632e4b17023SJohn Marino }
3633e4b17023SJohn Marino
3634e4b17023SJohn Marino return decl;
3635e4b17023SJohn Marino }
3636e4b17023SJohn Marino
3637e4b17023SJohn Marino /* Called when a declaration is seen that contains no names to declare.
3638e4b17023SJohn Marino If its type is a reference to a structure, union or enum inherited
3639e4b17023SJohn Marino from a containing scope, shadow that tag name for the current scope
3640e4b17023SJohn Marino with a forward reference.
3641e4b17023SJohn Marino If its type defines a new named structure or union
3642e4b17023SJohn Marino or defines an enum, it is valid but we need not do anything here.
3643e4b17023SJohn Marino Otherwise, it is an error. */
3644e4b17023SJohn Marino
3645e4b17023SJohn Marino void
shadow_tag(const struct c_declspecs * declspecs)3646e4b17023SJohn Marino shadow_tag (const struct c_declspecs *declspecs)
3647e4b17023SJohn Marino {
3648e4b17023SJohn Marino shadow_tag_warned (declspecs, 0);
3649e4b17023SJohn Marino }
3650e4b17023SJohn Marino
3651e4b17023SJohn Marino /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
3652e4b17023SJohn Marino but no pedwarn. */
3653e4b17023SJohn Marino void
shadow_tag_warned(const struct c_declspecs * declspecs,int warned)3654e4b17023SJohn Marino shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
3655e4b17023SJohn Marino {
3656e4b17023SJohn Marino bool found_tag = false;
3657e4b17023SJohn Marino
3658e4b17023SJohn Marino if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
3659e4b17023SJohn Marino {
3660e4b17023SJohn Marino tree value = declspecs->type;
3661e4b17023SJohn Marino enum tree_code code = TREE_CODE (value);
3662e4b17023SJohn Marino
3663e4b17023SJohn Marino if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
3664e4b17023SJohn Marino /* Used to test also that TYPE_SIZE (value) != 0.
3665e4b17023SJohn Marino That caused warning for `struct foo;' at top level in the file. */
3666e4b17023SJohn Marino {
3667e4b17023SJohn Marino tree name = TYPE_NAME (value);
3668e4b17023SJohn Marino tree t;
3669e4b17023SJohn Marino
3670e4b17023SJohn Marino found_tag = true;
3671e4b17023SJohn Marino
3672e4b17023SJohn Marino if (declspecs->restrict_p)
3673e4b17023SJohn Marino {
3674e4b17023SJohn Marino error ("invalid use of %<restrict%>");
3675e4b17023SJohn Marino warned = 1;
3676e4b17023SJohn Marino }
3677e4b17023SJohn Marino
3678e4b17023SJohn Marino if (name == 0)
3679e4b17023SJohn Marino {
3680e4b17023SJohn Marino if (warned != 1 && code != ENUMERAL_TYPE)
3681e4b17023SJohn Marino /* Empty unnamed enum OK */
3682e4b17023SJohn Marino {
3683e4b17023SJohn Marino pedwarn (input_location, 0,
3684e4b17023SJohn Marino "unnamed struct/union that defines no instances");
3685e4b17023SJohn Marino warned = 1;
3686e4b17023SJohn Marino }
3687e4b17023SJohn Marino }
3688e4b17023SJohn Marino else if (declspecs->typespec_kind != ctsk_tagdef
3689e4b17023SJohn Marino && declspecs->typespec_kind != ctsk_tagfirstref
3690e4b17023SJohn Marino && declspecs->storage_class != csc_none)
3691e4b17023SJohn Marino {
3692e4b17023SJohn Marino if (warned != 1)
3693e4b17023SJohn Marino pedwarn (input_location, 0,
3694e4b17023SJohn Marino "empty declaration with storage class specifier "
3695e4b17023SJohn Marino "does not redeclare tag");
3696e4b17023SJohn Marino warned = 1;
3697e4b17023SJohn Marino pending_xref_error ();
3698e4b17023SJohn Marino }
3699e4b17023SJohn Marino else if (declspecs->typespec_kind != ctsk_tagdef
3700e4b17023SJohn Marino && declspecs->typespec_kind != ctsk_tagfirstref
3701e4b17023SJohn Marino && (declspecs->const_p
3702e4b17023SJohn Marino || declspecs->volatile_p
3703e4b17023SJohn Marino || declspecs->restrict_p
3704e4b17023SJohn Marino || declspecs->address_space))
3705e4b17023SJohn Marino {
3706e4b17023SJohn Marino if (warned != 1)
3707e4b17023SJohn Marino pedwarn (input_location, 0,
3708e4b17023SJohn Marino "empty declaration with type qualifier "
3709e4b17023SJohn Marino "does not redeclare tag");
3710e4b17023SJohn Marino warned = 1;
3711e4b17023SJohn Marino pending_xref_error ();
3712e4b17023SJohn Marino }
3713e4b17023SJohn Marino else if (declspecs->typespec_kind != ctsk_tagdef
3714e4b17023SJohn Marino && declspecs->typespec_kind != ctsk_tagfirstref
3715e4b17023SJohn Marino && declspecs->alignas_p)
3716e4b17023SJohn Marino {
3717e4b17023SJohn Marino if (warned != 1)
3718e4b17023SJohn Marino pedwarn (input_location, 0,
3719e4b17023SJohn Marino "empty declaration with %<_Alignas%> "
3720e4b17023SJohn Marino "does not redeclare tag");
3721e4b17023SJohn Marino warned = 1;
3722e4b17023SJohn Marino pending_xref_error ();
3723e4b17023SJohn Marino }
3724e4b17023SJohn Marino else
3725e4b17023SJohn Marino {
3726e4b17023SJohn Marino pending_invalid_xref = 0;
3727e4b17023SJohn Marino t = lookup_tag (code, name, 1, NULL);
3728e4b17023SJohn Marino
3729e4b17023SJohn Marino if (t == 0)
3730e4b17023SJohn Marino {
3731e4b17023SJohn Marino t = make_node (code);
3732e4b17023SJohn Marino pushtag (input_location, name, t);
3733e4b17023SJohn Marino }
3734e4b17023SJohn Marino }
3735e4b17023SJohn Marino }
3736e4b17023SJohn Marino else
3737e4b17023SJohn Marino {
3738e4b17023SJohn Marino if (warned != 1 && !in_system_header)
3739e4b17023SJohn Marino {
3740e4b17023SJohn Marino pedwarn (input_location, 0,
3741e4b17023SJohn Marino "useless type name in empty declaration");
3742e4b17023SJohn Marino warned = 1;
3743e4b17023SJohn Marino }
3744e4b17023SJohn Marino }
3745e4b17023SJohn Marino }
3746e4b17023SJohn Marino else if (warned != 1 && !in_system_header && declspecs->typedef_p)
3747e4b17023SJohn Marino {
3748e4b17023SJohn Marino pedwarn (input_location, 0, "useless type name in empty declaration");
3749e4b17023SJohn Marino warned = 1;
3750e4b17023SJohn Marino }
3751e4b17023SJohn Marino
3752e4b17023SJohn Marino pending_invalid_xref = 0;
3753e4b17023SJohn Marino
3754e4b17023SJohn Marino if (declspecs->inline_p)
3755e4b17023SJohn Marino {
3756e4b17023SJohn Marino error ("%<inline%> in empty declaration");
3757e4b17023SJohn Marino warned = 1;
3758e4b17023SJohn Marino }
3759e4b17023SJohn Marino
3760e4b17023SJohn Marino if (declspecs->noreturn_p)
3761e4b17023SJohn Marino {
3762e4b17023SJohn Marino error ("%<_Noreturn%> in empty declaration");
3763e4b17023SJohn Marino warned = 1;
3764e4b17023SJohn Marino }
3765e4b17023SJohn Marino
3766e4b17023SJohn Marino if (current_scope == file_scope && declspecs->storage_class == csc_auto)
3767e4b17023SJohn Marino {
3768e4b17023SJohn Marino error ("%<auto%> in file-scope empty declaration");
3769e4b17023SJohn Marino warned = 1;
3770e4b17023SJohn Marino }
3771e4b17023SJohn Marino
3772e4b17023SJohn Marino if (current_scope == file_scope && declspecs->storage_class == csc_register)
3773e4b17023SJohn Marino {
3774e4b17023SJohn Marino error ("%<register%> in file-scope empty declaration");
3775e4b17023SJohn Marino warned = 1;
3776e4b17023SJohn Marino }
3777e4b17023SJohn Marino
3778e4b17023SJohn Marino if (!warned && !in_system_header && declspecs->storage_class != csc_none)
3779e4b17023SJohn Marino {
3780e4b17023SJohn Marino warning (0, "useless storage class specifier in empty declaration");
3781e4b17023SJohn Marino warned = 2;
3782e4b17023SJohn Marino }
3783e4b17023SJohn Marino
3784e4b17023SJohn Marino if (!warned && !in_system_header && declspecs->thread_p)
3785e4b17023SJohn Marino {
3786e4b17023SJohn Marino warning (0, "useless %<__thread%> in empty declaration");
3787e4b17023SJohn Marino warned = 2;
3788e4b17023SJohn Marino }
3789e4b17023SJohn Marino
3790e4b17023SJohn Marino if (!warned && !in_system_header && (declspecs->const_p
3791e4b17023SJohn Marino || declspecs->volatile_p
3792e4b17023SJohn Marino || declspecs->restrict_p
3793e4b17023SJohn Marino || declspecs->address_space))
3794e4b17023SJohn Marino {
3795e4b17023SJohn Marino warning (0, "useless type qualifier in empty declaration");
3796e4b17023SJohn Marino warned = 2;
3797e4b17023SJohn Marino }
3798e4b17023SJohn Marino
3799e4b17023SJohn Marino if (!warned && !in_system_header && declspecs->alignas_p)
3800e4b17023SJohn Marino {
3801e4b17023SJohn Marino warning (0, "useless %<_Alignas%> in empty declaration");
3802e4b17023SJohn Marino warned = 2;
3803e4b17023SJohn Marino }
3804e4b17023SJohn Marino
3805e4b17023SJohn Marino if (warned != 1)
3806e4b17023SJohn Marino {
3807e4b17023SJohn Marino if (!found_tag)
3808e4b17023SJohn Marino pedwarn (input_location, 0, "empty declaration");
3809e4b17023SJohn Marino }
3810e4b17023SJohn Marino }
3811e4b17023SJohn Marino
3812e4b17023SJohn Marino
3813e4b17023SJohn Marino /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
3814e4b17023SJohn Marino bits. SPECS represents declaration specifiers that the grammar
3815e4b17023SJohn Marino only permits to contain type qualifiers and attributes. */
3816e4b17023SJohn Marino
3817e4b17023SJohn Marino int
quals_from_declspecs(const struct c_declspecs * specs)3818e4b17023SJohn Marino quals_from_declspecs (const struct c_declspecs *specs)
3819e4b17023SJohn Marino {
3820e4b17023SJohn Marino int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3821e4b17023SJohn Marino | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3822e4b17023SJohn Marino | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0)
3823e4b17023SJohn Marino | (ENCODE_QUAL_ADDR_SPACE (specs->address_space)));
3824e4b17023SJohn Marino gcc_assert (!specs->type
3825e4b17023SJohn Marino && !specs->decl_attr
3826e4b17023SJohn Marino && specs->typespec_word == cts_none
3827e4b17023SJohn Marino && specs->storage_class == csc_none
3828e4b17023SJohn Marino && !specs->typedef_p
3829e4b17023SJohn Marino && !specs->explicit_signed_p
3830e4b17023SJohn Marino && !specs->deprecated_p
3831e4b17023SJohn Marino && !specs->long_p
3832e4b17023SJohn Marino && !specs->long_long_p
3833e4b17023SJohn Marino && !specs->short_p
3834e4b17023SJohn Marino && !specs->signed_p
3835e4b17023SJohn Marino && !specs->unsigned_p
3836e4b17023SJohn Marino && !specs->complex_p
3837e4b17023SJohn Marino && !specs->inline_p
3838e4b17023SJohn Marino && !specs->noreturn_p
3839e4b17023SJohn Marino && !specs->thread_p);
3840e4b17023SJohn Marino return quals;
3841e4b17023SJohn Marino }
3842e4b17023SJohn Marino
3843e4b17023SJohn Marino /* Construct an array declarator. LOC is the location of the
3844e4b17023SJohn Marino beginning of the array (usually the opening brace). EXPR is the
3845e4b17023SJohn Marino expression inside [], or NULL_TREE. QUALS are the type qualifiers
3846e4b17023SJohn Marino inside the [] (to be applied to the pointer to which a parameter
3847e4b17023SJohn Marino array is converted). STATIC_P is true if "static" is inside the
3848e4b17023SJohn Marino [], false otherwise. VLA_UNSPEC_P is true if the array is [*], a
3849e4b17023SJohn Marino VLA of unspecified length which is nevertheless a complete type,
3850e4b17023SJohn Marino false otherwise. The field for the contained declarator is left to
3851e4b17023SJohn Marino be filled in by set_array_declarator_inner. */
3852e4b17023SJohn Marino
3853e4b17023SJohn Marino struct c_declarator *
build_array_declarator(location_t loc,tree expr,struct c_declspecs * quals,bool static_p,bool vla_unspec_p)3854e4b17023SJohn Marino build_array_declarator (location_t loc,
3855e4b17023SJohn Marino tree expr, struct c_declspecs *quals, bool static_p,
3856e4b17023SJohn Marino bool vla_unspec_p)
3857e4b17023SJohn Marino {
3858e4b17023SJohn Marino struct c_declarator *declarator = XOBNEW (&parser_obstack,
3859e4b17023SJohn Marino struct c_declarator);
3860e4b17023SJohn Marino declarator->id_loc = loc;
3861e4b17023SJohn Marino declarator->kind = cdk_array;
3862e4b17023SJohn Marino declarator->declarator = 0;
3863e4b17023SJohn Marino declarator->u.array.dimen = expr;
3864e4b17023SJohn Marino if (quals)
3865e4b17023SJohn Marino {
3866e4b17023SJohn Marino declarator->u.array.attrs = quals->attrs;
3867e4b17023SJohn Marino declarator->u.array.quals = quals_from_declspecs (quals);
3868e4b17023SJohn Marino }
3869e4b17023SJohn Marino else
3870e4b17023SJohn Marino {
3871e4b17023SJohn Marino declarator->u.array.attrs = NULL_TREE;
3872e4b17023SJohn Marino declarator->u.array.quals = 0;
3873e4b17023SJohn Marino }
3874e4b17023SJohn Marino declarator->u.array.static_p = static_p;
3875e4b17023SJohn Marino declarator->u.array.vla_unspec_p = vla_unspec_p;
3876e4b17023SJohn Marino if (!flag_isoc99)
3877e4b17023SJohn Marino {
3878e4b17023SJohn Marino if (static_p || quals != NULL)
3879e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
3880e4b17023SJohn Marino "ISO C90 does not support %<static%> or type "
3881e4b17023SJohn Marino "qualifiers in parameter array declarators");
3882e4b17023SJohn Marino if (vla_unspec_p)
3883e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
3884e4b17023SJohn Marino "ISO C90 does not support %<[*]%> array declarators");
3885e4b17023SJohn Marino }
3886e4b17023SJohn Marino if (vla_unspec_p)
3887e4b17023SJohn Marino {
3888e4b17023SJohn Marino if (!current_scope->parm_flag)
3889e4b17023SJohn Marino {
3890e4b17023SJohn Marino /* C99 6.7.5.2p4 */
3891e4b17023SJohn Marino error_at (loc, "%<[*]%> not allowed in other than "
3892e4b17023SJohn Marino "function prototype scope");
3893e4b17023SJohn Marino declarator->u.array.vla_unspec_p = false;
3894e4b17023SJohn Marino return NULL;
3895e4b17023SJohn Marino }
3896e4b17023SJohn Marino current_scope->had_vla_unspec = true;
3897e4b17023SJohn Marino }
3898e4b17023SJohn Marino return declarator;
3899e4b17023SJohn Marino }
3900e4b17023SJohn Marino
3901e4b17023SJohn Marino /* Set the contained declarator of an array declarator. DECL is the
3902e4b17023SJohn Marino declarator, as constructed by build_array_declarator; INNER is what
3903e4b17023SJohn Marino appears on the left of the []. */
3904e4b17023SJohn Marino
3905e4b17023SJohn Marino struct c_declarator *
set_array_declarator_inner(struct c_declarator * decl,struct c_declarator * inner)3906e4b17023SJohn Marino set_array_declarator_inner (struct c_declarator *decl,
3907e4b17023SJohn Marino struct c_declarator *inner)
3908e4b17023SJohn Marino {
3909e4b17023SJohn Marino decl->declarator = inner;
3910e4b17023SJohn Marino return decl;
3911e4b17023SJohn Marino }
3912e4b17023SJohn Marino
3913e4b17023SJohn Marino /* INIT is a constructor that forms DECL's initializer. If the final
3914e4b17023SJohn Marino element initializes a flexible array field, add the size of that
3915e4b17023SJohn Marino initializer to DECL's size. */
3916e4b17023SJohn Marino
3917e4b17023SJohn Marino static void
add_flexible_array_elts_to_size(tree decl,tree init)3918e4b17023SJohn Marino add_flexible_array_elts_to_size (tree decl, tree init)
3919e4b17023SJohn Marino {
3920e4b17023SJohn Marino tree elt, type;
3921e4b17023SJohn Marino
3922e4b17023SJohn Marino if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3923e4b17023SJohn Marino return;
3924e4b17023SJohn Marino
3925e4b17023SJohn Marino elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3926e4b17023SJohn Marino type = TREE_TYPE (elt);
3927e4b17023SJohn Marino if (TREE_CODE (type) == ARRAY_TYPE
3928e4b17023SJohn Marino && TYPE_SIZE (type) == NULL_TREE
3929e4b17023SJohn Marino && TYPE_DOMAIN (type) != NULL_TREE
3930e4b17023SJohn Marino && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3931e4b17023SJohn Marino {
3932e4b17023SJohn Marino complete_array_type (&type, elt, false);
3933e4b17023SJohn Marino DECL_SIZE (decl)
3934e4b17023SJohn Marino = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3935e4b17023SJohn Marino DECL_SIZE_UNIT (decl)
3936e4b17023SJohn Marino = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3937e4b17023SJohn Marino }
3938e4b17023SJohn Marino }
3939e4b17023SJohn Marino
3940e4b17023SJohn Marino /* Decode a "typename", such as "int **", returning a ..._TYPE node.
3941e4b17023SJohn Marino Set *EXPR, if EXPR not NULL, to any expression to be evaluated
3942e4b17023SJohn Marino before the type name, and set *EXPR_CONST_OPERANDS, if
3943e4b17023SJohn Marino EXPR_CONST_OPERANDS not NULL, to indicate whether the type name may
3944e4b17023SJohn Marino appear in a constant expression. */
3945e4b17023SJohn Marino
3946e4b17023SJohn Marino tree
groktypename(struct c_type_name * type_name,tree * expr,bool * expr_const_operands)3947e4b17023SJohn Marino groktypename (struct c_type_name *type_name, tree *expr,
3948e4b17023SJohn Marino bool *expr_const_operands)
3949e4b17023SJohn Marino {
3950e4b17023SJohn Marino tree type;
3951e4b17023SJohn Marino tree attrs = type_name->specs->attrs;
3952e4b17023SJohn Marino
3953e4b17023SJohn Marino type_name->specs->attrs = NULL_TREE;
3954e4b17023SJohn Marino
3955e4b17023SJohn Marino type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3956e4b17023SJohn Marino false, NULL, &attrs, expr, expr_const_operands,
3957e4b17023SJohn Marino DEPRECATED_NORMAL);
3958e4b17023SJohn Marino
3959e4b17023SJohn Marino /* Apply attributes. */
3960e4b17023SJohn Marino decl_attributes (&type, attrs, 0);
3961e4b17023SJohn Marino
3962e4b17023SJohn Marino return type;
3963e4b17023SJohn Marino }
3964e4b17023SJohn Marino
3965e4b17023SJohn Marino /* Decode a declarator in an ordinary declaration or data definition.
3966e4b17023SJohn Marino This is called as soon as the type information and variable name
3967e4b17023SJohn Marino have been parsed, before parsing the initializer if any.
3968e4b17023SJohn Marino Here we create the ..._DECL node, fill in its type,
3969e4b17023SJohn Marino and put it on the list of decls for the current context.
3970e4b17023SJohn Marino The ..._DECL node is returned as the value.
3971e4b17023SJohn Marino
3972e4b17023SJohn Marino Exception: for arrays where the length is not specified,
3973e4b17023SJohn Marino the type is left null, to be filled in by `finish_decl'.
3974e4b17023SJohn Marino
3975e4b17023SJohn Marino Function definitions do not come here; they go to start_function
3976e4b17023SJohn Marino instead. However, external and forward declarations of functions
3977e4b17023SJohn Marino do go through here. Structure field declarations are done by
3978e4b17023SJohn Marino grokfield and not through here. */
3979e4b17023SJohn Marino
3980e4b17023SJohn Marino tree
start_decl(struct c_declarator * declarator,struct c_declspecs * declspecs,bool initialized,tree attributes)3981e4b17023SJohn Marino start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3982e4b17023SJohn Marino bool initialized, tree attributes)
3983e4b17023SJohn Marino {
3984e4b17023SJohn Marino tree decl;
3985e4b17023SJohn Marino tree tem;
3986e4b17023SJohn Marino tree expr = NULL_TREE;
3987e4b17023SJohn Marino enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3988e4b17023SJohn Marino
3989e4b17023SJohn Marino /* An object declared as __attribute__((deprecated)) suppresses
3990e4b17023SJohn Marino warnings of uses of other deprecated items. */
3991e4b17023SJohn Marino if (lookup_attribute ("deprecated", attributes))
3992e4b17023SJohn Marino deprecated_state = DEPRECATED_SUPPRESS;
3993e4b17023SJohn Marino
3994e4b17023SJohn Marino decl = grokdeclarator (declarator, declspecs,
3995e4b17023SJohn Marino NORMAL, initialized, NULL, &attributes, &expr, NULL,
3996e4b17023SJohn Marino deprecated_state);
3997e4b17023SJohn Marino if (!decl)
3998e4b17023SJohn Marino return 0;
3999e4b17023SJohn Marino
4000e4b17023SJohn Marino if (expr)
4001e4b17023SJohn Marino add_stmt (fold_convert (void_type_node, expr));
4002e4b17023SJohn Marino
4003e4b17023SJohn Marino if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl)))
4004e4b17023SJohn Marino warning (OPT_Wmain, "%q+D is usually a function", decl);
4005e4b17023SJohn Marino
4006e4b17023SJohn Marino if (initialized)
4007e4b17023SJohn Marino /* Is it valid for this decl to have an initializer at all?
4008e4b17023SJohn Marino If not, set INITIALIZED to zero, which will indirectly
4009e4b17023SJohn Marino tell 'finish_decl' to ignore the initializer once it is parsed. */
4010e4b17023SJohn Marino switch (TREE_CODE (decl))
4011e4b17023SJohn Marino {
4012e4b17023SJohn Marino case TYPE_DECL:
4013e4b17023SJohn Marino error ("typedef %qD is initialized (use __typeof__ instead)", decl);
4014e4b17023SJohn Marino initialized = 0;
4015e4b17023SJohn Marino break;
4016e4b17023SJohn Marino
4017e4b17023SJohn Marino case FUNCTION_DECL:
4018e4b17023SJohn Marino error ("function %qD is initialized like a variable", decl);
4019e4b17023SJohn Marino initialized = 0;
4020e4b17023SJohn Marino break;
4021e4b17023SJohn Marino
4022e4b17023SJohn Marino case PARM_DECL:
4023e4b17023SJohn Marino /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
4024e4b17023SJohn Marino error ("parameter %qD is initialized", decl);
4025e4b17023SJohn Marino initialized = 0;
4026e4b17023SJohn Marino break;
4027e4b17023SJohn Marino
4028e4b17023SJohn Marino default:
4029e4b17023SJohn Marino /* Don't allow initializations for incomplete types except for
4030e4b17023SJohn Marino arrays which might be completed by the initialization. */
4031e4b17023SJohn Marino
4032e4b17023SJohn Marino /* This can happen if the array size is an undefined macro.
4033e4b17023SJohn Marino We already gave a warning, so we don't need another one. */
4034e4b17023SJohn Marino if (TREE_TYPE (decl) == error_mark_node)
4035e4b17023SJohn Marino initialized = 0;
4036e4b17023SJohn Marino else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
4037e4b17023SJohn Marino {
4038e4b17023SJohn Marino /* A complete type is ok if size is fixed. */
4039e4b17023SJohn Marino
4040e4b17023SJohn Marino if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
4041e4b17023SJohn Marino || C_DECL_VARIABLE_SIZE (decl))
4042e4b17023SJohn Marino {
4043e4b17023SJohn Marino error ("variable-sized object may not be initialized");
4044e4b17023SJohn Marino initialized = 0;
4045e4b17023SJohn Marino }
4046e4b17023SJohn Marino }
4047e4b17023SJohn Marino else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
4048e4b17023SJohn Marino {
4049e4b17023SJohn Marino error ("variable %qD has initializer but incomplete type", decl);
4050e4b17023SJohn Marino initialized = 0;
4051e4b17023SJohn Marino }
4052e4b17023SJohn Marino else if (C_DECL_VARIABLE_SIZE (decl))
4053e4b17023SJohn Marino {
4054e4b17023SJohn Marino /* Although C99 is unclear about whether incomplete arrays
4055e4b17023SJohn Marino of VLAs themselves count as VLAs, it does not make
4056e4b17023SJohn Marino sense to permit them to be initialized given that
4057e4b17023SJohn Marino ordinary VLAs may not be initialized. */
4058e4b17023SJohn Marino error ("variable-sized object may not be initialized");
4059e4b17023SJohn Marino initialized = 0;
4060e4b17023SJohn Marino }
4061e4b17023SJohn Marino }
4062e4b17023SJohn Marino
4063e4b17023SJohn Marino if (initialized)
4064e4b17023SJohn Marino {
4065e4b17023SJohn Marino if (current_scope == file_scope)
4066e4b17023SJohn Marino TREE_STATIC (decl) = 1;
4067e4b17023SJohn Marino
4068e4b17023SJohn Marino /* Tell 'pushdecl' this is an initialized decl
4069e4b17023SJohn Marino even though we don't yet have the initializer expression.
4070e4b17023SJohn Marino Also tell 'finish_decl' it may store the real initializer. */
4071e4b17023SJohn Marino DECL_INITIAL (decl) = error_mark_node;
4072e4b17023SJohn Marino }
4073e4b17023SJohn Marino
4074e4b17023SJohn Marino /* If this is a function declaration, write a record describing it to the
4075e4b17023SJohn Marino prototypes file (if requested). */
4076e4b17023SJohn Marino
4077e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL)
4078e4b17023SJohn Marino gen_aux_info_record (decl, 0, 0, prototype_p (TREE_TYPE (decl)));
4079e4b17023SJohn Marino
4080e4b17023SJohn Marino /* ANSI specifies that a tentative definition which is not merged with
4081e4b17023SJohn Marino a non-tentative definition behaves exactly like a definition with an
4082e4b17023SJohn Marino initializer equal to zero. (Section 3.7.2)
4083e4b17023SJohn Marino
4084e4b17023SJohn Marino -fno-common gives strict ANSI behavior, though this tends to break
4085e4b17023SJohn Marino a large body of code that grew up without this rule.
4086e4b17023SJohn Marino
4087e4b17023SJohn Marino Thread-local variables are never common, since there's no entrenched
4088e4b17023SJohn Marino body of code to break, and it allows more efficient variable references
4089e4b17023SJohn Marino in the presence of dynamic linking. */
4090e4b17023SJohn Marino
4091e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL
4092e4b17023SJohn Marino && !initialized
4093e4b17023SJohn Marino && TREE_PUBLIC (decl)
4094e4b17023SJohn Marino && !DECL_THREAD_LOCAL_P (decl)
4095e4b17023SJohn Marino && !flag_no_common)
4096e4b17023SJohn Marino DECL_COMMON (decl) = 1;
4097e4b17023SJohn Marino
4098e4b17023SJohn Marino /* Set attributes here so if duplicate decl, will have proper attributes. */
4099e4b17023SJohn Marino decl_attributes (&decl, attributes, 0);
4100e4b17023SJohn Marino
4101e4b17023SJohn Marino /* Handle gnu_inline attribute. */
4102e4b17023SJohn Marino if (declspecs->inline_p
4103e4b17023SJohn Marino && !flag_gnu89_inline
4104e4b17023SJohn Marino && TREE_CODE (decl) == FUNCTION_DECL
4105e4b17023SJohn Marino && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
4106e4b17023SJohn Marino || current_function_decl))
4107e4b17023SJohn Marino {
4108e4b17023SJohn Marino if (declspecs->storage_class == csc_auto && current_scope != file_scope)
4109e4b17023SJohn Marino ;
4110e4b17023SJohn Marino else if (declspecs->storage_class != csc_static)
4111e4b17023SJohn Marino DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
4112e4b17023SJohn Marino }
4113e4b17023SJohn Marino
4114e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL
4115e4b17023SJohn Marino && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
4116e4b17023SJohn Marino {
4117e4b17023SJohn Marino struct c_declarator *ce = declarator;
4118e4b17023SJohn Marino
4119e4b17023SJohn Marino if (ce->kind == cdk_pointer)
4120e4b17023SJohn Marino ce = declarator->declarator;
4121e4b17023SJohn Marino if (ce->kind == cdk_function)
4122e4b17023SJohn Marino {
4123e4b17023SJohn Marino tree args = ce->u.arg_info->parms;
4124e4b17023SJohn Marino for (; args; args = DECL_CHAIN (args))
4125e4b17023SJohn Marino {
4126e4b17023SJohn Marino tree type = TREE_TYPE (args);
4127e4b17023SJohn Marino if (type && INTEGRAL_TYPE_P (type)
4128e4b17023SJohn Marino && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4129e4b17023SJohn Marino DECL_ARG_TYPE (args) = integer_type_node;
4130e4b17023SJohn Marino }
4131e4b17023SJohn Marino }
4132e4b17023SJohn Marino }
4133e4b17023SJohn Marino
4134e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL
4135e4b17023SJohn Marino && DECL_DECLARED_INLINE_P (decl)
4136e4b17023SJohn Marino && DECL_UNINLINABLE (decl)
4137e4b17023SJohn Marino && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
4138e4b17023SJohn Marino warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
4139e4b17023SJohn Marino decl);
4140e4b17023SJohn Marino
4141e4b17023SJohn Marino /* C99 6.7.4p3: An inline definition of a function with external
4142e4b17023SJohn Marino linkage shall not contain a definition of a modifiable object
4143e4b17023SJohn Marino with static storage duration... */
4144e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL
4145e4b17023SJohn Marino && current_scope != file_scope
4146e4b17023SJohn Marino && TREE_STATIC (decl)
4147e4b17023SJohn Marino && !TREE_READONLY (decl)
4148e4b17023SJohn Marino && DECL_DECLARED_INLINE_P (current_function_decl)
4149e4b17023SJohn Marino && DECL_EXTERNAL (current_function_decl))
4150e4b17023SJohn Marino record_inline_static (input_location, current_function_decl,
4151e4b17023SJohn Marino decl, csi_modifiable);
4152e4b17023SJohn Marino
4153e4b17023SJohn Marino if (c_dialect_objc ()
4154e4b17023SJohn Marino && (TREE_CODE (decl) == VAR_DECL
4155e4b17023SJohn Marino || TREE_CODE (decl) == FUNCTION_DECL))
4156e4b17023SJohn Marino objc_check_global_decl (decl);
4157e4b17023SJohn Marino
4158e4b17023SJohn Marino /* Add this decl to the current scope.
4159e4b17023SJohn Marino TEM may equal DECL or it may be a previous decl of the same name. */
4160e4b17023SJohn Marino tem = pushdecl (decl);
4161e4b17023SJohn Marino
4162e4b17023SJohn Marino if (initialized && DECL_EXTERNAL (tem))
4163e4b17023SJohn Marino {
4164e4b17023SJohn Marino DECL_EXTERNAL (tem) = 0;
4165e4b17023SJohn Marino TREE_STATIC (tem) = 1;
4166e4b17023SJohn Marino }
4167e4b17023SJohn Marino
4168e4b17023SJohn Marino return tem;
4169e4b17023SJohn Marino }
4170e4b17023SJohn Marino
4171e4b17023SJohn Marino /* Subroutine of finish_decl. TYPE is the type of an uninitialized object
4172e4b17023SJohn Marino DECL or the non-array element type if DECL is an uninitialized array.
4173e4b17023SJohn Marino If that type has a const member, diagnose this. */
4174e4b17023SJohn Marino
4175e4b17023SJohn Marino static void
diagnose_uninitialized_cst_member(tree decl,tree type)4176e4b17023SJohn Marino diagnose_uninitialized_cst_member (tree decl, tree type)
4177e4b17023SJohn Marino {
4178e4b17023SJohn Marino tree field;
4179e4b17023SJohn Marino for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4180e4b17023SJohn Marino {
4181e4b17023SJohn Marino tree field_type;
4182e4b17023SJohn Marino if (TREE_CODE (field) != FIELD_DECL)
4183e4b17023SJohn Marino continue;
4184e4b17023SJohn Marino field_type = strip_array_types (TREE_TYPE (field));
4185e4b17023SJohn Marino
4186e4b17023SJohn Marino if (TYPE_QUALS (field_type) & TYPE_QUAL_CONST)
4187e4b17023SJohn Marino {
4188e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4189e4b17023SJohn Marino "uninitialized const member in %qT is invalid in C++",
4190e4b17023SJohn Marino strip_array_types (TREE_TYPE (decl)));
4191e4b17023SJohn Marino inform (DECL_SOURCE_LOCATION (field), "%qD should be initialized", field);
4192e4b17023SJohn Marino }
4193e4b17023SJohn Marino
4194e4b17023SJohn Marino if (TREE_CODE (field_type) == RECORD_TYPE
4195e4b17023SJohn Marino || TREE_CODE (field_type) == UNION_TYPE)
4196e4b17023SJohn Marino diagnose_uninitialized_cst_member (decl, field_type);
4197e4b17023SJohn Marino }
4198e4b17023SJohn Marino }
4199e4b17023SJohn Marino
4200e4b17023SJohn Marino /* Finish processing of a declaration;
4201e4b17023SJohn Marino install its initial value.
4202e4b17023SJohn Marino If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
4203e4b17023SJohn Marino If the length of an array type is not known before,
4204e4b17023SJohn Marino it must be determined now, from the initial value, or it is an error.
4205e4b17023SJohn Marino
4206e4b17023SJohn Marino INIT_LOC is the location of the initial value. */
4207e4b17023SJohn Marino
4208e4b17023SJohn Marino void
finish_decl(tree decl,location_t init_loc,tree init,tree origtype,tree asmspec_tree)4209e4b17023SJohn Marino finish_decl (tree decl, location_t init_loc, tree init,
4210e4b17023SJohn Marino tree origtype, tree asmspec_tree)
4211e4b17023SJohn Marino {
4212e4b17023SJohn Marino tree type;
4213e4b17023SJohn Marino bool was_incomplete = (DECL_SIZE (decl) == 0);
4214e4b17023SJohn Marino const char *asmspec = 0;
4215e4b17023SJohn Marino
4216e4b17023SJohn Marino /* If a name was specified, get the string. */
4217e4b17023SJohn Marino if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
4218e4b17023SJohn Marino && DECL_FILE_SCOPE_P (decl))
4219e4b17023SJohn Marino asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
4220e4b17023SJohn Marino if (asmspec_tree)
4221e4b17023SJohn Marino asmspec = TREE_STRING_POINTER (asmspec_tree);
4222e4b17023SJohn Marino
4223e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL
4224e4b17023SJohn Marino && TREE_STATIC (decl)
4225e4b17023SJohn Marino && global_bindings_p ())
4226e4b17023SJohn Marino /* So decl is a global variable. Record the types it uses
4227e4b17023SJohn Marino so that we can decide later to emit debug info for them. */
4228e4b17023SJohn Marino record_types_used_by_current_var_decl (decl);
4229e4b17023SJohn Marino
4230e4b17023SJohn Marino /* If `start_decl' didn't like having an initialization, ignore it now. */
4231e4b17023SJohn Marino if (init != 0 && DECL_INITIAL (decl) == 0)
4232e4b17023SJohn Marino init = 0;
4233e4b17023SJohn Marino
4234e4b17023SJohn Marino /* Don't crash if parm is initialized. */
4235e4b17023SJohn Marino if (TREE_CODE (decl) == PARM_DECL)
4236e4b17023SJohn Marino init = 0;
4237e4b17023SJohn Marino
4238e4b17023SJohn Marino if (init)
4239e4b17023SJohn Marino store_init_value (init_loc, decl, init, origtype);
4240e4b17023SJohn Marino
4241e4b17023SJohn Marino if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
4242e4b17023SJohn Marino || TREE_CODE (decl) == FUNCTION_DECL
4243e4b17023SJohn Marino || TREE_CODE (decl) == FIELD_DECL))
4244e4b17023SJohn Marino objc_check_decl (decl);
4245e4b17023SJohn Marino
4246e4b17023SJohn Marino type = TREE_TYPE (decl);
4247e4b17023SJohn Marino
4248e4b17023SJohn Marino /* Deduce size of array from initialization, if not already known. */
4249e4b17023SJohn Marino if (TREE_CODE (type) == ARRAY_TYPE
4250e4b17023SJohn Marino && TYPE_DOMAIN (type) == 0
4251e4b17023SJohn Marino && TREE_CODE (decl) != TYPE_DECL)
4252e4b17023SJohn Marino {
4253e4b17023SJohn Marino bool do_default
4254e4b17023SJohn Marino = (TREE_STATIC (decl)
4255e4b17023SJohn Marino /* Even if pedantic, an external linkage array
4256e4b17023SJohn Marino may have incomplete type at first. */
4257e4b17023SJohn Marino ? pedantic && !TREE_PUBLIC (decl)
4258e4b17023SJohn Marino : !DECL_EXTERNAL (decl));
4259e4b17023SJohn Marino int failure
4260e4b17023SJohn Marino = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
4261e4b17023SJohn Marino do_default);
4262e4b17023SJohn Marino
4263e4b17023SJohn Marino /* Get the completed type made by complete_array_type. */
4264e4b17023SJohn Marino type = TREE_TYPE (decl);
4265e4b17023SJohn Marino
4266e4b17023SJohn Marino switch (failure)
4267e4b17023SJohn Marino {
4268e4b17023SJohn Marino case 1:
4269e4b17023SJohn Marino error ("initializer fails to determine size of %q+D", decl);
4270e4b17023SJohn Marino break;
4271e4b17023SJohn Marino
4272e4b17023SJohn Marino case 2:
4273e4b17023SJohn Marino if (do_default)
4274e4b17023SJohn Marino error ("array size missing in %q+D", decl);
4275e4b17023SJohn Marino /* If a `static' var's size isn't known,
4276e4b17023SJohn Marino make it extern as well as static, so it does not get
4277e4b17023SJohn Marino allocated.
4278e4b17023SJohn Marino If it is not `static', then do not mark extern;
4279e4b17023SJohn Marino finish_incomplete_decl will give it a default size
4280e4b17023SJohn Marino and it will get allocated. */
4281e4b17023SJohn Marino else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
4282e4b17023SJohn Marino DECL_EXTERNAL (decl) = 1;
4283e4b17023SJohn Marino break;
4284e4b17023SJohn Marino
4285e4b17023SJohn Marino case 3:
4286e4b17023SJohn Marino error ("zero or negative size array %q+D", decl);
4287e4b17023SJohn Marino break;
4288e4b17023SJohn Marino
4289e4b17023SJohn Marino case 0:
4290e4b17023SJohn Marino /* For global variables, update the copy of the type that
4291e4b17023SJohn Marino exists in the binding. */
4292e4b17023SJohn Marino if (TREE_PUBLIC (decl))
4293e4b17023SJohn Marino {
4294e4b17023SJohn Marino struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
4295e4b17023SJohn Marino while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
4296e4b17023SJohn Marino b_ext = b_ext->shadowed;
4297e4b17023SJohn Marino if (b_ext)
4298e4b17023SJohn Marino {
4299e4b17023SJohn Marino if (b_ext->u.type && comptypes (b_ext->u.type, type))
4300e4b17023SJohn Marino b_ext->u.type = composite_type (b_ext->u.type, type);
4301e4b17023SJohn Marino else
4302e4b17023SJohn Marino b_ext->u.type = type;
4303e4b17023SJohn Marino }
4304e4b17023SJohn Marino }
4305e4b17023SJohn Marino break;
4306e4b17023SJohn Marino
4307e4b17023SJohn Marino default:
4308e4b17023SJohn Marino gcc_unreachable ();
4309e4b17023SJohn Marino }
4310e4b17023SJohn Marino
4311e4b17023SJohn Marino if (DECL_INITIAL (decl))
4312e4b17023SJohn Marino TREE_TYPE (DECL_INITIAL (decl)) = type;
4313e4b17023SJohn Marino
4314e4b17023SJohn Marino relayout_decl (decl);
4315e4b17023SJohn Marino }
4316e4b17023SJohn Marino
4317e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL)
4318e4b17023SJohn Marino {
4319e4b17023SJohn Marino if (init && TREE_CODE (init) == CONSTRUCTOR)
4320e4b17023SJohn Marino add_flexible_array_elts_to_size (decl, init);
4321e4b17023SJohn Marino
4322e4b17023SJohn Marino if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
4323e4b17023SJohn Marino && COMPLETE_TYPE_P (TREE_TYPE (decl)))
4324e4b17023SJohn Marino layout_decl (decl, 0);
4325e4b17023SJohn Marino
4326e4b17023SJohn Marino if (DECL_SIZE (decl) == 0
4327e4b17023SJohn Marino /* Don't give an error if we already gave one earlier. */
4328e4b17023SJohn Marino && TREE_TYPE (decl) != error_mark_node
4329e4b17023SJohn Marino && (TREE_STATIC (decl)
4330e4b17023SJohn Marino /* A static variable with an incomplete type
4331e4b17023SJohn Marino is an error if it is initialized.
4332e4b17023SJohn Marino Also if it is not file scope.
4333e4b17023SJohn Marino Otherwise, let it through, but if it is not `extern'
4334e4b17023SJohn Marino then it may cause an error message later. */
4335e4b17023SJohn Marino ? (DECL_INITIAL (decl) != 0
4336e4b17023SJohn Marino || !DECL_FILE_SCOPE_P (decl))
4337e4b17023SJohn Marino /* An automatic variable with an incomplete type
4338e4b17023SJohn Marino is an error. */
4339e4b17023SJohn Marino : !DECL_EXTERNAL (decl)))
4340e4b17023SJohn Marino {
4341e4b17023SJohn Marino error ("storage size of %q+D isn%'t known", decl);
4342e4b17023SJohn Marino TREE_TYPE (decl) = error_mark_node;
4343e4b17023SJohn Marino }
4344e4b17023SJohn Marino
4345e4b17023SJohn Marino if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
4346e4b17023SJohn Marino && DECL_SIZE (decl) != 0)
4347e4b17023SJohn Marino {
4348e4b17023SJohn Marino if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
4349e4b17023SJohn Marino constant_expression_warning (DECL_SIZE (decl));
4350e4b17023SJohn Marino else
4351e4b17023SJohn Marino {
4352e4b17023SJohn Marino error ("storage size of %q+D isn%'t constant", decl);
4353e4b17023SJohn Marino TREE_TYPE (decl) = error_mark_node;
4354e4b17023SJohn Marino }
4355e4b17023SJohn Marino }
4356e4b17023SJohn Marino
4357e4b17023SJohn Marino if (TREE_USED (type))
4358e4b17023SJohn Marino {
4359e4b17023SJohn Marino TREE_USED (decl) = 1;
4360e4b17023SJohn Marino DECL_READ_P (decl) = 1;
4361e4b17023SJohn Marino }
4362e4b17023SJohn Marino }
4363e4b17023SJohn Marino
4364e4b17023SJohn Marino /* If this is a function and an assembler name is specified, reset DECL_RTL
4365e4b17023SJohn Marino so we can give it its new name. Also, update builtin_decl if it
4366e4b17023SJohn Marino was a normal built-in. */
4367e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
4368e4b17023SJohn Marino {
4369e4b17023SJohn Marino if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4370e4b17023SJohn Marino set_builtin_user_assembler_name (decl, asmspec);
4371e4b17023SJohn Marino set_user_assembler_name (decl, asmspec);
4372e4b17023SJohn Marino }
4373e4b17023SJohn Marino
4374e4b17023SJohn Marino /* If #pragma weak was used, mark the decl weak now. */
4375e4b17023SJohn Marino maybe_apply_pragma_weak (decl);
4376e4b17023SJohn Marino
4377e4b17023SJohn Marino /* Output the assembler code and/or RTL code for variables and functions,
4378e4b17023SJohn Marino unless the type is an undefined structure or union.
4379e4b17023SJohn Marino If not, it will get done when the type is completed. */
4380e4b17023SJohn Marino
4381e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
4382e4b17023SJohn Marino {
4383e4b17023SJohn Marino /* Determine the ELF visibility. */
4384e4b17023SJohn Marino if (TREE_PUBLIC (decl))
4385e4b17023SJohn Marino c_determine_visibility (decl);
4386e4b17023SJohn Marino
4387e4b17023SJohn Marino /* This is a no-op in c-lang.c or something real in objc-act.c. */
4388e4b17023SJohn Marino if (c_dialect_objc ())
4389e4b17023SJohn Marino objc_check_decl (decl);
4390e4b17023SJohn Marino
4391e4b17023SJohn Marino if (asmspec)
4392e4b17023SJohn Marino {
4393e4b17023SJohn Marino /* If this is not a static variable, issue a warning.
4394e4b17023SJohn Marino It doesn't make any sense to give an ASMSPEC for an
4395e4b17023SJohn Marino ordinary, non-register local variable. Historically,
4396e4b17023SJohn Marino GCC has accepted -- but ignored -- the ASMSPEC in
4397e4b17023SJohn Marino this case. */
4398e4b17023SJohn Marino if (!DECL_FILE_SCOPE_P (decl)
4399e4b17023SJohn Marino && TREE_CODE (decl) == VAR_DECL
4400e4b17023SJohn Marino && !C_DECL_REGISTER (decl)
4401e4b17023SJohn Marino && !TREE_STATIC (decl))
4402e4b17023SJohn Marino warning (0, "ignoring asm-specifier for non-static local "
4403e4b17023SJohn Marino "variable %q+D", decl);
4404e4b17023SJohn Marino else
4405e4b17023SJohn Marino set_user_assembler_name (decl, asmspec);
4406e4b17023SJohn Marino }
4407e4b17023SJohn Marino
4408e4b17023SJohn Marino if (DECL_FILE_SCOPE_P (decl))
4409e4b17023SJohn Marino {
4410e4b17023SJohn Marino if (DECL_INITIAL (decl) == NULL_TREE
4411e4b17023SJohn Marino || DECL_INITIAL (decl) == error_mark_node)
4412e4b17023SJohn Marino /* Don't output anything
4413e4b17023SJohn Marino when a tentative file-scope definition is seen.
4414e4b17023SJohn Marino But at end of compilation, do output code for them. */
4415e4b17023SJohn Marino DECL_DEFER_OUTPUT (decl) = 1;
4416e4b17023SJohn Marino if (asmspec && C_DECL_REGISTER (decl))
4417e4b17023SJohn Marino DECL_HARD_REGISTER (decl) = 1;
4418e4b17023SJohn Marino rest_of_decl_compilation (decl, true, 0);
4419e4b17023SJohn Marino }
4420e4b17023SJohn Marino else
4421e4b17023SJohn Marino {
4422e4b17023SJohn Marino /* In conjunction with an ASMSPEC, the `register'
4423e4b17023SJohn Marino keyword indicates that we should place the variable
4424e4b17023SJohn Marino in a particular register. */
4425e4b17023SJohn Marino if (asmspec && C_DECL_REGISTER (decl))
4426e4b17023SJohn Marino {
4427e4b17023SJohn Marino DECL_HARD_REGISTER (decl) = 1;
4428e4b17023SJohn Marino /* This cannot be done for a structure with volatile
4429e4b17023SJohn Marino fields, on which DECL_REGISTER will have been
4430e4b17023SJohn Marino reset. */
4431e4b17023SJohn Marino if (!DECL_REGISTER (decl))
4432e4b17023SJohn Marino error ("cannot put object with volatile field into register");
4433e4b17023SJohn Marino }
4434e4b17023SJohn Marino
4435e4b17023SJohn Marino if (TREE_CODE (decl) != FUNCTION_DECL)
4436e4b17023SJohn Marino {
4437e4b17023SJohn Marino /* If we're building a variable sized type, and we might be
4438e4b17023SJohn Marino reachable other than via the top of the current binding
4439e4b17023SJohn Marino level, then create a new BIND_EXPR so that we deallocate
4440e4b17023SJohn Marino the object at the right time. */
4441e4b17023SJohn Marino /* Note that DECL_SIZE can be null due to errors. */
4442e4b17023SJohn Marino if (DECL_SIZE (decl)
4443e4b17023SJohn Marino && !TREE_CONSTANT (DECL_SIZE (decl))
4444e4b17023SJohn Marino && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
4445e4b17023SJohn Marino {
4446e4b17023SJohn Marino tree bind;
4447e4b17023SJohn Marino bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
4448e4b17023SJohn Marino TREE_SIDE_EFFECTS (bind) = 1;
4449e4b17023SJohn Marino add_stmt (bind);
4450e4b17023SJohn Marino BIND_EXPR_BODY (bind) = push_stmt_list ();
4451e4b17023SJohn Marino }
4452e4b17023SJohn Marino add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl),
4453e4b17023SJohn Marino DECL_EXPR, decl));
4454e4b17023SJohn Marino }
4455e4b17023SJohn Marino }
4456e4b17023SJohn Marino
4457e4b17023SJohn Marino
4458e4b17023SJohn Marino if (!DECL_FILE_SCOPE_P (decl))
4459e4b17023SJohn Marino {
4460e4b17023SJohn Marino /* Recompute the RTL of a local array now
4461e4b17023SJohn Marino if it used to be an incomplete type. */
4462e4b17023SJohn Marino if (was_incomplete
4463e4b17023SJohn Marino && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
4464e4b17023SJohn Marino {
4465e4b17023SJohn Marino /* If we used it already as memory, it must stay in memory. */
4466e4b17023SJohn Marino TREE_ADDRESSABLE (decl) = TREE_USED (decl);
4467e4b17023SJohn Marino /* If it's still incomplete now, no init will save it. */
4468e4b17023SJohn Marino if (DECL_SIZE (decl) == 0)
4469e4b17023SJohn Marino DECL_INITIAL (decl) = 0;
4470e4b17023SJohn Marino }
4471e4b17023SJohn Marino }
4472e4b17023SJohn Marino }
4473e4b17023SJohn Marino
4474e4b17023SJohn Marino if (TREE_CODE (decl) == TYPE_DECL)
4475e4b17023SJohn Marino {
4476e4b17023SJohn Marino if (!DECL_FILE_SCOPE_P (decl)
4477e4b17023SJohn Marino && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
4478e4b17023SJohn Marino add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
4479e4b17023SJohn Marino
4480e4b17023SJohn Marino rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
4481e4b17023SJohn Marino }
4482e4b17023SJohn Marino
4483e4b17023SJohn Marino /* Install a cleanup (aka destructor) if one was given. */
4484e4b17023SJohn Marino if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
4485e4b17023SJohn Marino {
4486e4b17023SJohn Marino tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
4487e4b17023SJohn Marino if (attr)
4488e4b17023SJohn Marino {
4489e4b17023SJohn Marino tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
4490e4b17023SJohn Marino tree cleanup_decl = lookup_name (cleanup_id);
4491e4b17023SJohn Marino tree cleanup;
4492e4b17023SJohn Marino VEC(tree,gc) *vec;
4493e4b17023SJohn Marino
4494e4b17023SJohn Marino /* Build "cleanup(&decl)" for the destructor. */
4495e4b17023SJohn Marino cleanup = build_unary_op (input_location, ADDR_EXPR, decl, 0);
4496e4b17023SJohn Marino vec = VEC_alloc (tree, gc, 1);
4497e4b17023SJohn Marino VEC_quick_push (tree, vec, cleanup);
4498e4b17023SJohn Marino cleanup = build_function_call_vec (DECL_SOURCE_LOCATION (decl),
4499e4b17023SJohn Marino cleanup_decl, vec, NULL);
4500e4b17023SJohn Marino VEC_free (tree, gc, vec);
4501e4b17023SJohn Marino
4502e4b17023SJohn Marino /* Don't warn about decl unused; the cleanup uses it. */
4503e4b17023SJohn Marino TREE_USED (decl) = 1;
4504e4b17023SJohn Marino TREE_USED (cleanup_decl) = 1;
4505e4b17023SJohn Marino DECL_READ_P (decl) = 1;
4506e4b17023SJohn Marino
4507e4b17023SJohn Marino push_cleanup (decl, cleanup, false);
4508e4b17023SJohn Marino }
4509e4b17023SJohn Marino }
4510e4b17023SJohn Marino
4511e4b17023SJohn Marino if (warn_cxx_compat
4512e4b17023SJohn Marino && TREE_CODE (decl) == VAR_DECL
4513e4b17023SJohn Marino && !DECL_EXTERNAL (decl)
4514e4b17023SJohn Marino && DECL_INITIAL (decl) == NULL_TREE)
4515e4b17023SJohn Marino {
4516e4b17023SJohn Marino type = strip_array_types (type);
4517e4b17023SJohn Marino if (TREE_READONLY (decl))
4518e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
4519e4b17023SJohn Marino "uninitialized const %qD is invalid in C++", decl);
4520e4b17023SJohn Marino else if ((TREE_CODE (type) == RECORD_TYPE
4521e4b17023SJohn Marino || TREE_CODE (type) == UNION_TYPE)
4522e4b17023SJohn Marino && C_TYPE_FIELDS_READONLY (type))
4523e4b17023SJohn Marino diagnose_uninitialized_cst_member (decl, type);
4524e4b17023SJohn Marino }
4525e4b17023SJohn Marino
4526e4b17023SJohn Marino invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl);
4527e4b17023SJohn Marino }
4528e4b17023SJohn Marino
4529e4b17023SJohn Marino /* Given a parsed parameter declaration, decode it into a PARM_DECL.
4530e4b17023SJohn Marino EXPR is NULL or a pointer to an expression that needs to be
4531e4b17023SJohn Marino evaluated for the side effects of array size expressions in the
4532e4b17023SJohn Marino parameters. */
4533e4b17023SJohn Marino
4534e4b17023SJohn Marino tree
grokparm(const struct c_parm * parm,tree * expr)4535e4b17023SJohn Marino grokparm (const struct c_parm *parm, tree *expr)
4536e4b17023SJohn Marino {
4537e4b17023SJohn Marino tree attrs = parm->attrs;
4538e4b17023SJohn Marino tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
4539e4b17023SJohn Marino NULL, &attrs, expr, NULL, DEPRECATED_NORMAL);
4540e4b17023SJohn Marino
4541e4b17023SJohn Marino decl_attributes (&decl, attrs, 0);
4542e4b17023SJohn Marino
4543e4b17023SJohn Marino return decl;
4544e4b17023SJohn Marino }
4545e4b17023SJohn Marino
4546e4b17023SJohn Marino /* Given a parsed parameter declaration, decode it into a PARM_DECL
4547e4b17023SJohn Marino and push that on the current scope. EXPR is a pointer to an
4548e4b17023SJohn Marino expression that needs to be evaluated for the side effects of array
4549e4b17023SJohn Marino size expressions in the parameters. */
4550e4b17023SJohn Marino
4551e4b17023SJohn Marino void
push_parm_decl(const struct c_parm * parm,tree * expr)4552e4b17023SJohn Marino push_parm_decl (const struct c_parm *parm, tree *expr)
4553e4b17023SJohn Marino {
4554e4b17023SJohn Marino tree attrs = parm->attrs;
4555e4b17023SJohn Marino tree decl;
4556e4b17023SJohn Marino
4557e4b17023SJohn Marino decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
4558e4b17023SJohn Marino &attrs, expr, NULL, DEPRECATED_NORMAL);
4559e4b17023SJohn Marino decl_attributes (&decl, attrs, 0);
4560e4b17023SJohn Marino
4561e4b17023SJohn Marino decl = pushdecl (decl);
4562e4b17023SJohn Marino
4563e4b17023SJohn Marino finish_decl (decl, input_location, NULL_TREE, NULL_TREE, NULL_TREE);
4564e4b17023SJohn Marino }
4565e4b17023SJohn Marino
4566e4b17023SJohn Marino /* Mark all the parameter declarations to date as forward decls.
4567e4b17023SJohn Marino Also diagnose use of this extension. */
4568e4b17023SJohn Marino
4569e4b17023SJohn Marino void
mark_forward_parm_decls(void)4570e4b17023SJohn Marino mark_forward_parm_decls (void)
4571e4b17023SJohn Marino {
4572e4b17023SJohn Marino struct c_binding *b;
4573e4b17023SJohn Marino
4574e4b17023SJohn Marino if (pedantic && !current_scope->warned_forward_parm_decls)
4575e4b17023SJohn Marino {
4576e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
4577e4b17023SJohn Marino "ISO C forbids forward parameter declarations");
4578e4b17023SJohn Marino current_scope->warned_forward_parm_decls = true;
4579e4b17023SJohn Marino }
4580e4b17023SJohn Marino
4581e4b17023SJohn Marino for (b = current_scope->bindings; b; b = b->prev)
4582e4b17023SJohn Marino if (TREE_CODE (b->decl) == PARM_DECL)
4583e4b17023SJohn Marino TREE_ASM_WRITTEN (b->decl) = 1;
4584e4b17023SJohn Marino }
4585e4b17023SJohn Marino
4586e4b17023SJohn Marino /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
4587e4b17023SJohn Marino literal, which may be an incomplete array type completed by the
4588e4b17023SJohn Marino initializer; INIT is a CONSTRUCTOR at LOC that initializes the compound
4589e4b17023SJohn Marino literal. NON_CONST is true if the initializers contain something
4590e4b17023SJohn Marino that cannot occur in a constant expression. */
4591e4b17023SJohn Marino
4592e4b17023SJohn Marino tree
build_compound_literal(location_t loc,tree type,tree init,bool non_const)4593e4b17023SJohn Marino build_compound_literal (location_t loc, tree type, tree init, bool non_const)
4594e4b17023SJohn Marino {
4595e4b17023SJohn Marino /* We do not use start_decl here because we have a type, not a declarator;
4596e4b17023SJohn Marino and do not use finish_decl because the decl should be stored inside
4597e4b17023SJohn Marino the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
4598e4b17023SJohn Marino tree decl;
4599e4b17023SJohn Marino tree complit;
4600e4b17023SJohn Marino tree stmt;
4601e4b17023SJohn Marino
4602e4b17023SJohn Marino if (type == error_mark_node
4603e4b17023SJohn Marino || init == error_mark_node)
4604e4b17023SJohn Marino return error_mark_node;
4605e4b17023SJohn Marino
4606e4b17023SJohn Marino decl = build_decl (loc, VAR_DECL, NULL_TREE, type);
4607e4b17023SJohn Marino DECL_EXTERNAL (decl) = 0;
4608e4b17023SJohn Marino TREE_PUBLIC (decl) = 0;
4609e4b17023SJohn Marino TREE_STATIC (decl) = (current_scope == file_scope);
4610e4b17023SJohn Marino DECL_CONTEXT (decl) = current_function_decl;
4611e4b17023SJohn Marino TREE_USED (decl) = 1;
4612e4b17023SJohn Marino DECL_READ_P (decl) = 1;
4613e4b17023SJohn Marino TREE_TYPE (decl) = type;
4614e4b17023SJohn Marino TREE_READONLY (decl) = TYPE_READONLY (type);
4615e4b17023SJohn Marino store_init_value (loc, decl, init, NULL_TREE);
4616e4b17023SJohn Marino
4617e4b17023SJohn Marino if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
4618e4b17023SJohn Marino {
4619e4b17023SJohn Marino int failure = complete_array_type (&TREE_TYPE (decl),
4620e4b17023SJohn Marino DECL_INITIAL (decl), true);
4621*95d28233SJohn Marino /* If complete_array_type returns 3, it means that the
4622*95d28233SJohn Marino initial value of the compound literal is empty. Allow it. */
4623*95d28233SJohn Marino gcc_assert (failure == 0 || failure == 3);
4624e4b17023SJohn Marino
4625e4b17023SJohn Marino type = TREE_TYPE (decl);
4626e4b17023SJohn Marino TREE_TYPE (DECL_INITIAL (decl)) = type;
4627e4b17023SJohn Marino }
4628e4b17023SJohn Marino
4629e4b17023SJohn Marino if (type == error_mark_node || !COMPLETE_TYPE_P (type))
4630e4b17023SJohn Marino return error_mark_node;
4631e4b17023SJohn Marino
4632e4b17023SJohn Marino stmt = build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl);
4633e4b17023SJohn Marino complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
4634e4b17023SJohn Marino TREE_SIDE_EFFECTS (complit) = 1;
4635e4b17023SJohn Marino
4636e4b17023SJohn Marino layout_decl (decl, 0);
4637e4b17023SJohn Marino
4638e4b17023SJohn Marino if (TREE_STATIC (decl))
4639e4b17023SJohn Marino {
4640e4b17023SJohn Marino /* This decl needs a name for the assembler output. */
4641e4b17023SJohn Marino set_compound_literal_name (decl);
4642e4b17023SJohn Marino DECL_DEFER_OUTPUT (decl) = 1;
4643e4b17023SJohn Marino DECL_COMDAT (decl) = 1;
4644e4b17023SJohn Marino DECL_ARTIFICIAL (decl) = 1;
4645e4b17023SJohn Marino DECL_IGNORED_P (decl) = 1;
4646e4b17023SJohn Marino pushdecl (decl);
4647e4b17023SJohn Marino rest_of_decl_compilation (decl, 1, 0);
4648e4b17023SJohn Marino }
4649e4b17023SJohn Marino
4650e4b17023SJohn Marino if (non_const)
4651e4b17023SJohn Marino {
4652e4b17023SJohn Marino complit = build2 (C_MAYBE_CONST_EXPR, type, NULL, complit);
4653e4b17023SJohn Marino C_MAYBE_CONST_EXPR_NON_CONST (complit) = 1;
4654e4b17023SJohn Marino }
4655e4b17023SJohn Marino
4656e4b17023SJohn Marino return complit;
4657e4b17023SJohn Marino }
4658e4b17023SJohn Marino
4659e4b17023SJohn Marino /* Check the type of a compound literal. Here we just check that it
4660e4b17023SJohn Marino is valid for C++. */
4661e4b17023SJohn Marino
4662e4b17023SJohn Marino void
check_compound_literal_type(location_t loc,struct c_type_name * type_name)4663e4b17023SJohn Marino check_compound_literal_type (location_t loc, struct c_type_name *type_name)
4664e4b17023SJohn Marino {
4665e4b17023SJohn Marino if (warn_cxx_compat
4666e4b17023SJohn Marino && (type_name->specs->typespec_kind == ctsk_tagdef
4667e4b17023SJohn Marino || type_name->specs->typespec_kind == ctsk_tagfirstref))
4668e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
4669e4b17023SJohn Marino "defining a type in a compound literal is invalid in C++");
4670e4b17023SJohn Marino }
4671e4b17023SJohn Marino
4672e4b17023SJohn Marino /* Determine whether TYPE is a structure with a flexible array member,
4673e4b17023SJohn Marino or a union containing such a structure (possibly recursively). */
4674e4b17023SJohn Marino
4675e4b17023SJohn Marino static bool
flexible_array_type_p(tree type)4676e4b17023SJohn Marino flexible_array_type_p (tree type)
4677e4b17023SJohn Marino {
4678e4b17023SJohn Marino tree x;
4679e4b17023SJohn Marino switch (TREE_CODE (type))
4680e4b17023SJohn Marino {
4681e4b17023SJohn Marino case RECORD_TYPE:
4682e4b17023SJohn Marino x = TYPE_FIELDS (type);
4683e4b17023SJohn Marino if (x == NULL_TREE)
4684e4b17023SJohn Marino return false;
4685e4b17023SJohn Marino while (DECL_CHAIN (x) != NULL_TREE)
4686e4b17023SJohn Marino x = DECL_CHAIN (x);
4687e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4688e4b17023SJohn Marino && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4689e4b17023SJohn Marino && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
4690e4b17023SJohn Marino && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
4691e4b17023SJohn Marino return true;
4692e4b17023SJohn Marino return false;
4693e4b17023SJohn Marino case UNION_TYPE:
4694e4b17023SJohn Marino for (x = TYPE_FIELDS (type); x != NULL_TREE; x = DECL_CHAIN (x))
4695e4b17023SJohn Marino {
4696e4b17023SJohn Marino if (flexible_array_type_p (TREE_TYPE (x)))
4697e4b17023SJohn Marino return true;
4698e4b17023SJohn Marino }
4699e4b17023SJohn Marino return false;
4700e4b17023SJohn Marino default:
4701e4b17023SJohn Marino return false;
4702e4b17023SJohn Marino }
4703e4b17023SJohn Marino }
4704e4b17023SJohn Marino
4705e4b17023SJohn Marino /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
4706e4b17023SJohn Marino replacing with appropriate values if they are invalid. */
4707e4b17023SJohn Marino static void
check_bitfield_type_and_width(tree * type,tree * width,tree orig_name)4708e4b17023SJohn Marino check_bitfield_type_and_width (tree *type, tree *width, tree orig_name)
4709e4b17023SJohn Marino {
4710e4b17023SJohn Marino tree type_mv;
4711e4b17023SJohn Marino unsigned int max_width;
4712e4b17023SJohn Marino unsigned HOST_WIDE_INT w;
4713e4b17023SJohn Marino const char *name = (orig_name
4714e4b17023SJohn Marino ? identifier_to_locale (IDENTIFIER_POINTER (orig_name))
4715e4b17023SJohn Marino : _("<anonymous>"));
4716e4b17023SJohn Marino
4717e4b17023SJohn Marino /* Detect and ignore out of range field width and process valid
4718e4b17023SJohn Marino field widths. */
4719e4b17023SJohn Marino if (!INTEGRAL_TYPE_P (TREE_TYPE (*width)))
4720e4b17023SJohn Marino {
4721e4b17023SJohn Marino error ("bit-field %qs width not an integer constant", name);
4722e4b17023SJohn Marino *width = integer_one_node;
4723e4b17023SJohn Marino }
4724e4b17023SJohn Marino else
4725e4b17023SJohn Marino {
4726e4b17023SJohn Marino if (TREE_CODE (*width) != INTEGER_CST)
4727e4b17023SJohn Marino {
4728e4b17023SJohn Marino *width = c_fully_fold (*width, false, NULL);
4729e4b17023SJohn Marino if (TREE_CODE (*width) == INTEGER_CST)
4730e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
4731e4b17023SJohn Marino "bit-field %qs width not an integer constant expression",
4732e4b17023SJohn Marino name);
4733e4b17023SJohn Marino }
4734e4b17023SJohn Marino if (TREE_CODE (*width) != INTEGER_CST)
4735e4b17023SJohn Marino {
4736e4b17023SJohn Marino error ("bit-field %qs width not an integer constant", name);
4737e4b17023SJohn Marino *width = integer_one_node;
4738e4b17023SJohn Marino }
4739e4b17023SJohn Marino constant_expression_warning (*width);
4740e4b17023SJohn Marino if (tree_int_cst_sgn (*width) < 0)
4741e4b17023SJohn Marino {
4742e4b17023SJohn Marino error ("negative width in bit-field %qs", name);
4743e4b17023SJohn Marino *width = integer_one_node;
4744e4b17023SJohn Marino }
4745e4b17023SJohn Marino else if (integer_zerop (*width) && orig_name)
4746e4b17023SJohn Marino {
4747e4b17023SJohn Marino error ("zero width for bit-field %qs", name);
4748e4b17023SJohn Marino *width = integer_one_node;
4749e4b17023SJohn Marino }
4750e4b17023SJohn Marino }
4751e4b17023SJohn Marino
4752e4b17023SJohn Marino /* Detect invalid bit-field type. */
4753e4b17023SJohn Marino if (TREE_CODE (*type) != INTEGER_TYPE
4754e4b17023SJohn Marino && TREE_CODE (*type) != BOOLEAN_TYPE
4755e4b17023SJohn Marino && TREE_CODE (*type) != ENUMERAL_TYPE)
4756e4b17023SJohn Marino {
4757e4b17023SJohn Marino error ("bit-field %qs has invalid type", name);
4758e4b17023SJohn Marino *type = unsigned_type_node;
4759e4b17023SJohn Marino }
4760e4b17023SJohn Marino
4761e4b17023SJohn Marino type_mv = TYPE_MAIN_VARIANT (*type);
4762e4b17023SJohn Marino if (!in_system_header
4763e4b17023SJohn Marino && type_mv != integer_type_node
4764e4b17023SJohn Marino && type_mv != unsigned_type_node
4765e4b17023SJohn Marino && type_mv != boolean_type_node)
4766e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
4767e4b17023SJohn Marino "type of bit-field %qs is a GCC extension", name);
4768e4b17023SJohn Marino
4769e4b17023SJohn Marino max_width = TYPE_PRECISION (*type);
4770e4b17023SJohn Marino
4771e4b17023SJohn Marino if (0 < compare_tree_int (*width, max_width))
4772e4b17023SJohn Marino {
4773e4b17023SJohn Marino error ("width of %qs exceeds its type", name);
4774e4b17023SJohn Marino w = max_width;
4775e4b17023SJohn Marino *width = build_int_cst (integer_type_node, w);
4776e4b17023SJohn Marino }
4777e4b17023SJohn Marino else
4778e4b17023SJohn Marino w = tree_low_cst (*width, 1);
4779e4b17023SJohn Marino
4780e4b17023SJohn Marino if (TREE_CODE (*type) == ENUMERAL_TYPE)
4781e4b17023SJohn Marino {
4782e4b17023SJohn Marino struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
4783e4b17023SJohn Marino if (!lt
4784e4b17023SJohn Marino || w < tree_int_cst_min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
4785e4b17023SJohn Marino || w < tree_int_cst_min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
4786e4b17023SJohn Marino warning (0, "%qs is narrower than values of its type", name);
4787e4b17023SJohn Marino }
4788e4b17023SJohn Marino }
4789e4b17023SJohn Marino
4790e4b17023SJohn Marino
4791e4b17023SJohn Marino
4792e4b17023SJohn Marino /* Print warning about variable length array if necessary. */
4793e4b17023SJohn Marino
4794e4b17023SJohn Marino static void
warn_variable_length_array(tree name,tree size)4795e4b17023SJohn Marino warn_variable_length_array (tree name, tree size)
4796e4b17023SJohn Marino {
4797e4b17023SJohn Marino int const_size = TREE_CONSTANT (size);
4798e4b17023SJohn Marino
4799e4b17023SJohn Marino if (!flag_isoc99 && pedantic && warn_vla != 0)
4800e4b17023SJohn Marino {
4801e4b17023SJohn Marino if (const_size)
4802e4b17023SJohn Marino {
4803e4b17023SJohn Marino if (name)
4804e4b17023SJohn Marino pedwarn (input_location, OPT_Wvla,
4805e4b17023SJohn Marino "ISO C90 forbids array %qE whose size "
4806e4b17023SJohn Marino "can%'t be evaluated",
4807e4b17023SJohn Marino name);
4808e4b17023SJohn Marino else
4809e4b17023SJohn Marino pedwarn (input_location, OPT_Wvla, "ISO C90 forbids array whose size "
4810e4b17023SJohn Marino "can%'t be evaluated");
4811e4b17023SJohn Marino }
4812e4b17023SJohn Marino else
4813e4b17023SJohn Marino {
4814e4b17023SJohn Marino if (name)
4815e4b17023SJohn Marino pedwarn (input_location, OPT_Wvla,
4816e4b17023SJohn Marino "ISO C90 forbids variable length array %qE",
4817e4b17023SJohn Marino name);
4818e4b17023SJohn Marino else
4819e4b17023SJohn Marino pedwarn (input_location, OPT_Wvla, "ISO C90 forbids variable length array");
4820e4b17023SJohn Marino }
4821e4b17023SJohn Marino }
4822e4b17023SJohn Marino else if (warn_vla > 0)
4823e4b17023SJohn Marino {
4824e4b17023SJohn Marino if (const_size)
4825e4b17023SJohn Marino {
4826e4b17023SJohn Marino if (name)
4827e4b17023SJohn Marino warning (OPT_Wvla,
4828e4b17023SJohn Marino "the size of array %qE can"
4829e4b17023SJohn Marino "%'t be evaluated", name);
4830e4b17023SJohn Marino else
4831e4b17023SJohn Marino warning (OPT_Wvla,
4832e4b17023SJohn Marino "the size of array can %'t be evaluated");
4833e4b17023SJohn Marino }
4834e4b17023SJohn Marino else
4835e4b17023SJohn Marino {
4836e4b17023SJohn Marino if (name)
4837e4b17023SJohn Marino warning (OPT_Wvla,
4838e4b17023SJohn Marino "variable length array %qE is used",
4839e4b17023SJohn Marino name);
4840e4b17023SJohn Marino else
4841e4b17023SJohn Marino warning (OPT_Wvla,
4842e4b17023SJohn Marino "variable length array is used");
4843e4b17023SJohn Marino }
4844e4b17023SJohn Marino }
4845e4b17023SJohn Marino }
4846e4b17023SJohn Marino
4847e4b17023SJohn Marino /* Given declspecs and a declarator,
4848e4b17023SJohn Marino determine the name and type of the object declared
4849e4b17023SJohn Marino and construct a ..._DECL node for it.
4850e4b17023SJohn Marino (In one case we can return a ..._TYPE node instead.
4851e4b17023SJohn Marino For invalid input we sometimes return 0.)
4852e4b17023SJohn Marino
4853e4b17023SJohn Marino DECLSPECS is a c_declspecs structure for the declaration specifiers.
4854e4b17023SJohn Marino
4855e4b17023SJohn Marino DECL_CONTEXT says which syntactic context this declaration is in:
4856e4b17023SJohn Marino NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
4857e4b17023SJohn Marino FUNCDEF for a function definition. Like NORMAL but a few different
4858e4b17023SJohn Marino error messages in each case. Return value may be zero meaning
4859e4b17023SJohn Marino this definition is too screwy to try to parse.
4860e4b17023SJohn Marino PARM for a parameter declaration (either within a function prototype
4861e4b17023SJohn Marino or before a function body). Make a PARM_DECL, or return void_type_node.
4862e4b17023SJohn Marino TYPENAME if for a typename (in a cast or sizeof).
4863e4b17023SJohn Marino Don't make a DECL node; just return the ..._TYPE node.
4864e4b17023SJohn Marino FIELD for a struct or union field; make a FIELD_DECL.
4865e4b17023SJohn Marino INITIALIZED is true if the decl has an initializer.
4866e4b17023SJohn Marino WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
4867e4b17023SJohn Marino representing the width of the bit-field.
4868e4b17023SJohn Marino DECL_ATTRS points to the list of attributes that should be added to this
4869e4b17023SJohn Marino decl. Any nested attributes that belong on the decl itself will be
4870e4b17023SJohn Marino added to this list.
4871e4b17023SJohn Marino If EXPR is not NULL, any expressions that need to be evaluated as
4872e4b17023SJohn Marino part of evaluating variably modified types will be stored in *EXPR.
4873e4b17023SJohn Marino If EXPR_CONST_OPERANDS is not NULL, *EXPR_CONST_OPERANDS will be
4874e4b17023SJohn Marino set to indicate whether operands in *EXPR can be used in constant
4875e4b17023SJohn Marino expressions.
4876e4b17023SJohn Marino DEPRECATED_STATE is a deprecated_states value indicating whether
4877e4b17023SJohn Marino deprecation warnings should be suppressed.
4878e4b17023SJohn Marino
4879e4b17023SJohn Marino In the TYPENAME case, DECLARATOR is really an absolute declarator.
4880e4b17023SJohn Marino It may also be so in the PARM case, for a prototype where the
4881e4b17023SJohn Marino argument type is specified but not the name.
4882e4b17023SJohn Marino
4883e4b17023SJohn Marino This function is where the complicated C meanings of `static'
4884e4b17023SJohn Marino and `extern' are interpreted. */
4885e4b17023SJohn Marino
4886e4b17023SJohn Marino static tree
grokdeclarator(const struct c_declarator * declarator,struct c_declspecs * declspecs,enum decl_context decl_context,bool initialized,tree * width,tree * decl_attrs,tree * expr,bool * expr_const_operands,enum deprecated_states deprecated_state)4887e4b17023SJohn Marino grokdeclarator (const struct c_declarator *declarator,
4888e4b17023SJohn Marino struct c_declspecs *declspecs,
4889e4b17023SJohn Marino enum decl_context decl_context, bool initialized, tree *width,
4890e4b17023SJohn Marino tree *decl_attrs, tree *expr, bool *expr_const_operands,
4891e4b17023SJohn Marino enum deprecated_states deprecated_state)
4892e4b17023SJohn Marino {
4893e4b17023SJohn Marino tree type = declspecs->type;
4894e4b17023SJohn Marino bool threadp = declspecs->thread_p;
4895e4b17023SJohn Marino enum c_storage_class storage_class = declspecs->storage_class;
4896e4b17023SJohn Marino int constp;
4897e4b17023SJohn Marino int restrictp;
4898e4b17023SJohn Marino int volatilep;
4899e4b17023SJohn Marino int type_quals = TYPE_UNQUALIFIED;
4900e4b17023SJohn Marino tree name = NULL_TREE;
4901e4b17023SJohn Marino bool funcdef_flag = false;
4902e4b17023SJohn Marino bool funcdef_syntax = false;
4903e4b17023SJohn Marino bool size_varies = false;
4904e4b17023SJohn Marino tree decl_attr = declspecs->decl_attr;
4905e4b17023SJohn Marino int array_ptr_quals = TYPE_UNQUALIFIED;
4906e4b17023SJohn Marino tree array_ptr_attrs = NULL_TREE;
4907e4b17023SJohn Marino int array_parm_static = 0;
4908e4b17023SJohn Marino bool array_parm_vla_unspec_p = false;
4909e4b17023SJohn Marino tree returned_attrs = NULL_TREE;
4910e4b17023SJohn Marino bool bitfield = width != NULL;
4911e4b17023SJohn Marino tree element_type;
4912e4b17023SJohn Marino struct c_arg_info *arg_info = 0;
4913e4b17023SJohn Marino addr_space_t as1, as2, address_space;
4914e4b17023SJohn Marino location_t loc = UNKNOWN_LOCATION;
4915e4b17023SJohn Marino const char *errmsg;
4916e4b17023SJohn Marino tree expr_dummy;
4917e4b17023SJohn Marino bool expr_const_operands_dummy;
4918e4b17023SJohn Marino enum c_declarator_kind first_non_attr_kind;
4919e4b17023SJohn Marino unsigned int alignas_align = 0;
4920e4b17023SJohn Marino
4921e4b17023SJohn Marino if (TREE_CODE (type) == ERROR_MARK)
4922e4b17023SJohn Marino return error_mark_node;
4923e4b17023SJohn Marino if (expr == NULL)
4924e4b17023SJohn Marino expr = &expr_dummy;
4925e4b17023SJohn Marino if (expr_const_operands == NULL)
4926e4b17023SJohn Marino expr_const_operands = &expr_const_operands_dummy;
4927e4b17023SJohn Marino
4928e4b17023SJohn Marino *expr = declspecs->expr;
4929e4b17023SJohn Marino *expr_const_operands = declspecs->expr_const_operands;
4930e4b17023SJohn Marino
4931e4b17023SJohn Marino if (decl_context == FUNCDEF)
4932e4b17023SJohn Marino funcdef_flag = true, decl_context = NORMAL;
4933e4b17023SJohn Marino
4934e4b17023SJohn Marino /* Look inside a declarator for the name being declared
4935e4b17023SJohn Marino and get it as an IDENTIFIER_NODE, for an error message. */
4936e4b17023SJohn Marino {
4937e4b17023SJohn Marino const struct c_declarator *decl = declarator;
4938e4b17023SJohn Marino
4939e4b17023SJohn Marino first_non_attr_kind = cdk_attrs;
4940e4b17023SJohn Marino while (decl)
4941e4b17023SJohn Marino switch (decl->kind)
4942e4b17023SJohn Marino {
4943e4b17023SJohn Marino case cdk_array:
4944e4b17023SJohn Marino loc = decl->id_loc;
4945e4b17023SJohn Marino /* FALL THRU. */
4946e4b17023SJohn Marino
4947e4b17023SJohn Marino case cdk_function:
4948e4b17023SJohn Marino case cdk_pointer:
4949e4b17023SJohn Marino funcdef_syntax = (decl->kind == cdk_function);
4950e4b17023SJohn Marino decl = decl->declarator;
4951e4b17023SJohn Marino if (first_non_attr_kind == cdk_attrs)
4952e4b17023SJohn Marino first_non_attr_kind = decl->kind;
4953e4b17023SJohn Marino break;
4954e4b17023SJohn Marino
4955e4b17023SJohn Marino case cdk_attrs:
4956e4b17023SJohn Marino decl = decl->declarator;
4957e4b17023SJohn Marino break;
4958e4b17023SJohn Marino
4959e4b17023SJohn Marino case cdk_id:
4960e4b17023SJohn Marino loc = decl->id_loc;
4961e4b17023SJohn Marino if (decl->u.id)
4962e4b17023SJohn Marino name = decl->u.id;
4963e4b17023SJohn Marino if (first_non_attr_kind == cdk_attrs)
4964e4b17023SJohn Marino first_non_attr_kind = decl->kind;
4965e4b17023SJohn Marino decl = 0;
4966e4b17023SJohn Marino break;
4967e4b17023SJohn Marino
4968e4b17023SJohn Marino default:
4969e4b17023SJohn Marino gcc_unreachable ();
4970e4b17023SJohn Marino }
4971e4b17023SJohn Marino if (name == 0)
4972e4b17023SJohn Marino {
4973e4b17023SJohn Marino gcc_assert (decl_context == PARM
4974e4b17023SJohn Marino || decl_context == TYPENAME
4975e4b17023SJohn Marino || (decl_context == FIELD
4976e4b17023SJohn Marino && declarator->kind == cdk_id));
4977e4b17023SJohn Marino gcc_assert (!initialized);
4978e4b17023SJohn Marino }
4979e4b17023SJohn Marino }
4980e4b17023SJohn Marino
4981e4b17023SJohn Marino /* A function definition's declarator must have the form of
4982e4b17023SJohn Marino a function declarator. */
4983e4b17023SJohn Marino
4984e4b17023SJohn Marino if (funcdef_flag && !funcdef_syntax)
4985e4b17023SJohn Marino return 0;
4986e4b17023SJohn Marino
4987e4b17023SJohn Marino /* If this looks like a function definition, make it one,
4988e4b17023SJohn Marino even if it occurs where parms are expected.
4989e4b17023SJohn Marino Then store_parm_decls will reject it and not use it as a parm. */
4990e4b17023SJohn Marino if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4991e4b17023SJohn Marino decl_context = PARM;
4992e4b17023SJohn Marino
4993e4b17023SJohn Marino if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4994e4b17023SJohn Marino warn_deprecated_use (declspecs->type, declspecs->decl_attr);
4995e4b17023SJohn Marino
4996e4b17023SJohn Marino if ((decl_context == NORMAL || decl_context == FIELD)
4997e4b17023SJohn Marino && current_scope == file_scope
4998e4b17023SJohn Marino && variably_modified_type_p (type, NULL_TREE))
4999e4b17023SJohn Marino {
5000e4b17023SJohn Marino if (name)
5001e4b17023SJohn Marino error_at (loc, "variably modified %qE at file scope", name);
5002e4b17023SJohn Marino else
5003e4b17023SJohn Marino error_at (loc, "variably modified field at file scope");
5004e4b17023SJohn Marino type = integer_type_node;
5005e4b17023SJohn Marino }
5006e4b17023SJohn Marino
5007e4b17023SJohn Marino size_varies = C_TYPE_VARIABLE_SIZE (type) != 0;
5008e4b17023SJohn Marino
5009e4b17023SJohn Marino /* Diagnose defaulting to "int". */
5010e4b17023SJohn Marino
5011e4b17023SJohn Marino if (declspecs->default_int_p && !in_system_header)
5012e4b17023SJohn Marino {
5013e4b17023SJohn Marino /* Issue a warning if this is an ISO C 99 program or if
5014e4b17023SJohn Marino -Wreturn-type and this is a function, or if -Wimplicit;
5015e4b17023SJohn Marino prefer the former warning since it is more explicit. */
5016e4b17023SJohn Marino if ((warn_implicit_int || warn_return_type || flag_isoc99)
5017e4b17023SJohn Marino && funcdef_flag)
5018e4b17023SJohn Marino warn_about_return_type = 1;
5019e4b17023SJohn Marino else
5020e4b17023SJohn Marino {
5021e4b17023SJohn Marino if (name)
5022e4b17023SJohn Marino pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5023e4b17023SJohn Marino "type defaults to %<int%> in declaration of %qE",
5024e4b17023SJohn Marino name);
5025e4b17023SJohn Marino else
5026e4b17023SJohn Marino pedwarn_c99 (input_location, flag_isoc99 ? 0 : OPT_Wimplicit_int,
5027e4b17023SJohn Marino "type defaults to %<int%> in type name");
5028e4b17023SJohn Marino }
5029e4b17023SJohn Marino }
5030e4b17023SJohn Marino
5031e4b17023SJohn Marino /* Adjust the type if a bit-field is being declared,
5032e4b17023SJohn Marino -funsigned-bitfields applied and the type is not explicitly
5033e4b17023SJohn Marino "signed". */
5034e4b17023SJohn Marino if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
5035e4b17023SJohn Marino && TREE_CODE (type) == INTEGER_TYPE)
5036e4b17023SJohn Marino type = unsigned_type_for (type);
5037e4b17023SJohn Marino
5038e4b17023SJohn Marino /* Figure out the type qualifiers for the declaration. There are
5039e4b17023SJohn Marino two ways a declaration can become qualified. One is something
5040e4b17023SJohn Marino like `const int i' where the `const' is explicit. Another is
5041e4b17023SJohn Marino something like `typedef const int CI; CI i' where the type of the
5042e4b17023SJohn Marino declaration contains the `const'. A third possibility is that
5043e4b17023SJohn Marino there is a type qualifier on the element type of a typedefed
5044e4b17023SJohn Marino array type, in which case we should extract that qualifier so
5045e4b17023SJohn Marino that c_apply_type_quals_to_decl receives the full list of
5046e4b17023SJohn Marino qualifiers to work with (C90 is not entirely clear about whether
5047e4b17023SJohn Marino duplicate qualifiers should be diagnosed in this case, but it
5048e4b17023SJohn Marino seems most appropriate to do so). */
5049e4b17023SJohn Marino element_type = strip_array_types (type);
5050e4b17023SJohn Marino constp = declspecs->const_p + TYPE_READONLY (element_type);
5051e4b17023SJohn Marino restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
5052e4b17023SJohn Marino volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
5053e4b17023SJohn Marino as1 = declspecs->address_space;
5054e4b17023SJohn Marino as2 = TYPE_ADDR_SPACE (element_type);
5055e4b17023SJohn Marino address_space = ADDR_SPACE_GENERIC_P (as1)? as2 : as1;
5056e4b17023SJohn Marino
5057e4b17023SJohn Marino if (pedantic && !flag_isoc99)
5058e4b17023SJohn Marino {
5059e4b17023SJohn Marino if (constp > 1)
5060e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "duplicate %<const%>");
5061e4b17023SJohn Marino if (restrictp > 1)
5062e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "duplicate %<restrict%>");
5063e4b17023SJohn Marino if (volatilep > 1)
5064e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "duplicate %<volatile%>");
5065e4b17023SJohn Marino }
5066e4b17023SJohn Marino
5067e4b17023SJohn Marino if (!ADDR_SPACE_GENERIC_P (as1) && !ADDR_SPACE_GENERIC_P (as2) && as1 != as2)
5068e4b17023SJohn Marino error_at (loc, "conflicting named address spaces (%s vs %s)",
5069e4b17023SJohn Marino c_addr_space_name (as1), c_addr_space_name (as2));
5070e4b17023SJohn Marino
5071e4b17023SJohn Marino if ((TREE_CODE (type) == ARRAY_TYPE
5072e4b17023SJohn Marino || first_non_attr_kind == cdk_array)
5073e4b17023SJohn Marino && TYPE_QUALS (element_type))
5074e4b17023SJohn Marino type = TYPE_MAIN_VARIANT (type);
5075e4b17023SJohn Marino type_quals = ((constp ? TYPE_QUAL_CONST : 0)
5076e4b17023SJohn Marino | (restrictp ? TYPE_QUAL_RESTRICT : 0)
5077e4b17023SJohn Marino | (volatilep ? TYPE_QUAL_VOLATILE : 0)
5078e4b17023SJohn Marino | ENCODE_QUAL_ADDR_SPACE (address_space));
5079e4b17023SJohn Marino
5080e4b17023SJohn Marino /* Warn about storage classes that are invalid for certain
5081e4b17023SJohn Marino kinds of declarations (parameters, typenames, etc.). */
5082e4b17023SJohn Marino
5083e4b17023SJohn Marino if (funcdef_flag
5084e4b17023SJohn Marino && (threadp
5085e4b17023SJohn Marino || storage_class == csc_auto
5086e4b17023SJohn Marino || storage_class == csc_register
5087e4b17023SJohn Marino || storage_class == csc_typedef))
5088e4b17023SJohn Marino {
5089e4b17023SJohn Marino if (storage_class == csc_auto)
5090e4b17023SJohn Marino pedwarn (loc,
5091e4b17023SJohn Marino (current_scope == file_scope) ? 0 : OPT_pedantic,
5092e4b17023SJohn Marino "function definition declared %<auto%>");
5093e4b17023SJohn Marino if (storage_class == csc_register)
5094e4b17023SJohn Marino error_at (loc, "function definition declared %<register%>");
5095e4b17023SJohn Marino if (storage_class == csc_typedef)
5096e4b17023SJohn Marino error_at (loc, "function definition declared %<typedef%>");
5097e4b17023SJohn Marino if (threadp)
5098e4b17023SJohn Marino error_at (loc, "function definition declared %<__thread%>");
5099e4b17023SJohn Marino threadp = false;
5100e4b17023SJohn Marino if (storage_class == csc_auto
5101e4b17023SJohn Marino || storage_class == csc_register
5102e4b17023SJohn Marino || storage_class == csc_typedef)
5103e4b17023SJohn Marino storage_class = csc_none;
5104e4b17023SJohn Marino }
5105e4b17023SJohn Marino else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
5106e4b17023SJohn Marino {
5107e4b17023SJohn Marino if (decl_context == PARM && storage_class == csc_register)
5108e4b17023SJohn Marino ;
5109e4b17023SJohn Marino else
5110e4b17023SJohn Marino {
5111e4b17023SJohn Marino switch (decl_context)
5112e4b17023SJohn Marino {
5113e4b17023SJohn Marino case FIELD:
5114e4b17023SJohn Marino if (name)
5115e4b17023SJohn Marino error_at (loc, "storage class specified for structure "
5116e4b17023SJohn Marino "field %qE", name);
5117e4b17023SJohn Marino else
5118e4b17023SJohn Marino error_at (loc, "storage class specified for structure field");
5119e4b17023SJohn Marino break;
5120e4b17023SJohn Marino case PARM:
5121e4b17023SJohn Marino if (name)
5122e4b17023SJohn Marino error_at (loc, "storage class specified for parameter %qE",
5123e4b17023SJohn Marino name);
5124e4b17023SJohn Marino else
5125e4b17023SJohn Marino error_at (loc, "storage class specified for unnamed parameter");
5126e4b17023SJohn Marino break;
5127e4b17023SJohn Marino default:
5128e4b17023SJohn Marino error_at (loc, "storage class specified for typename");
5129e4b17023SJohn Marino break;
5130e4b17023SJohn Marino }
5131e4b17023SJohn Marino storage_class = csc_none;
5132e4b17023SJohn Marino threadp = false;
5133e4b17023SJohn Marino }
5134e4b17023SJohn Marino }
5135e4b17023SJohn Marino else if (storage_class == csc_extern
5136e4b17023SJohn Marino && initialized
5137e4b17023SJohn Marino && !funcdef_flag)
5138e4b17023SJohn Marino {
5139e4b17023SJohn Marino /* 'extern' with initialization is invalid if not at file scope. */
5140e4b17023SJohn Marino if (current_scope == file_scope)
5141e4b17023SJohn Marino {
5142e4b17023SJohn Marino /* It is fine to have 'extern const' when compiling at C
5143e4b17023SJohn Marino and C++ intersection. */
5144e4b17023SJohn Marino if (!(warn_cxx_compat && constp))
5145e4b17023SJohn Marino warning_at (loc, 0, "%qE initialized and declared %<extern%>",
5146e4b17023SJohn Marino name);
5147e4b17023SJohn Marino }
5148e4b17023SJohn Marino else
5149e4b17023SJohn Marino error_at (loc, "%qE has both %<extern%> and initializer", name);
5150e4b17023SJohn Marino }
5151e4b17023SJohn Marino else if (current_scope == file_scope)
5152e4b17023SJohn Marino {
5153e4b17023SJohn Marino if (storage_class == csc_auto)
5154e4b17023SJohn Marino error_at (loc, "file-scope declaration of %qE specifies %<auto%>",
5155e4b17023SJohn Marino name);
5156e4b17023SJohn Marino if (pedantic && storage_class == csc_register)
5157e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
5158e4b17023SJohn Marino "file-scope declaration of %qE specifies %<register%>", name);
5159e4b17023SJohn Marino }
5160e4b17023SJohn Marino else
5161e4b17023SJohn Marino {
5162e4b17023SJohn Marino if (storage_class == csc_extern && funcdef_flag)
5163e4b17023SJohn Marino error_at (loc, "nested function %qE declared %<extern%>", name);
5164e4b17023SJohn Marino else if (threadp && storage_class == csc_none)
5165e4b17023SJohn Marino {
5166e4b17023SJohn Marino error_at (loc, "function-scope %qE implicitly auto and declared "
5167e4b17023SJohn Marino "%<__thread%>",
5168e4b17023SJohn Marino name);
5169e4b17023SJohn Marino threadp = false;
5170e4b17023SJohn Marino }
5171e4b17023SJohn Marino }
5172e4b17023SJohn Marino
5173e4b17023SJohn Marino /* Now figure out the structure of the declarator proper.
5174e4b17023SJohn Marino Descend through it, creating more complex types, until we reach
5175e4b17023SJohn Marino the declared identifier (or NULL_TREE, in an absolute declarator).
5176e4b17023SJohn Marino At each stage we maintain an unqualified version of the type
5177e4b17023SJohn Marino together with any qualifiers that should be applied to it with
5178e4b17023SJohn Marino c_build_qualified_type; this way, array types including
5179e4b17023SJohn Marino multidimensional array types are first built up in unqualified
5180e4b17023SJohn Marino form and then the qualified form is created with
5181e4b17023SJohn Marino TYPE_MAIN_VARIANT pointing to the unqualified form. */
5182e4b17023SJohn Marino
5183e4b17023SJohn Marino while (declarator && declarator->kind != cdk_id)
5184e4b17023SJohn Marino {
5185e4b17023SJohn Marino if (type == error_mark_node)
5186e4b17023SJohn Marino {
5187e4b17023SJohn Marino declarator = declarator->declarator;
5188e4b17023SJohn Marino continue;
5189e4b17023SJohn Marino }
5190e4b17023SJohn Marino
5191e4b17023SJohn Marino /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
5192e4b17023SJohn Marino a cdk_pointer (for *...),
5193e4b17023SJohn Marino a cdk_function (for ...(...)),
5194e4b17023SJohn Marino a cdk_attrs (for nested attributes),
5195e4b17023SJohn Marino or a cdk_id (for the name being declared
5196e4b17023SJohn Marino or the place in an absolute declarator
5197e4b17023SJohn Marino where the name was omitted).
5198e4b17023SJohn Marino For the last case, we have just exited the loop.
5199e4b17023SJohn Marino
5200e4b17023SJohn Marino At this point, TYPE is the type of elements of an array,
5201e4b17023SJohn Marino or for a function to return, or for a pointer to point to.
5202e4b17023SJohn Marino After this sequence of ifs, TYPE is the type of the
5203e4b17023SJohn Marino array or function or pointer, and DECLARATOR has had its
5204e4b17023SJohn Marino outermost layer removed. */
5205e4b17023SJohn Marino
5206e4b17023SJohn Marino if (array_ptr_quals != TYPE_UNQUALIFIED
5207e4b17023SJohn Marino || array_ptr_attrs != NULL_TREE
5208e4b17023SJohn Marino || array_parm_static)
5209e4b17023SJohn Marino {
5210e4b17023SJohn Marino /* Only the innermost declarator (making a parameter be of
5211e4b17023SJohn Marino array type which is converted to pointer type)
5212e4b17023SJohn Marino may have static or type qualifiers. */
5213e4b17023SJohn Marino error_at (loc, "static or type qualifiers in non-parameter array declarator");
5214e4b17023SJohn Marino array_ptr_quals = TYPE_UNQUALIFIED;
5215e4b17023SJohn Marino array_ptr_attrs = NULL_TREE;
5216e4b17023SJohn Marino array_parm_static = 0;
5217e4b17023SJohn Marino }
5218e4b17023SJohn Marino
5219e4b17023SJohn Marino switch (declarator->kind)
5220e4b17023SJohn Marino {
5221e4b17023SJohn Marino case cdk_attrs:
5222e4b17023SJohn Marino {
5223e4b17023SJohn Marino /* A declarator with embedded attributes. */
5224e4b17023SJohn Marino tree attrs = declarator->u.attrs;
5225e4b17023SJohn Marino const struct c_declarator *inner_decl;
5226e4b17023SJohn Marino int attr_flags = 0;
5227e4b17023SJohn Marino declarator = declarator->declarator;
5228e4b17023SJohn Marino inner_decl = declarator;
5229e4b17023SJohn Marino while (inner_decl->kind == cdk_attrs)
5230e4b17023SJohn Marino inner_decl = inner_decl->declarator;
5231e4b17023SJohn Marino if (inner_decl->kind == cdk_id)
5232e4b17023SJohn Marino attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
5233e4b17023SJohn Marino else if (inner_decl->kind == cdk_function)
5234e4b17023SJohn Marino attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
5235e4b17023SJohn Marino else if (inner_decl->kind == cdk_array)
5236e4b17023SJohn Marino attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
5237e4b17023SJohn Marino returned_attrs = decl_attributes (&type,
5238e4b17023SJohn Marino chainon (returned_attrs, attrs),
5239e4b17023SJohn Marino attr_flags);
5240e4b17023SJohn Marino break;
5241e4b17023SJohn Marino }
5242e4b17023SJohn Marino case cdk_array:
5243e4b17023SJohn Marino {
5244e4b17023SJohn Marino tree itype = NULL_TREE;
5245e4b17023SJohn Marino tree size = declarator->u.array.dimen;
5246e4b17023SJohn Marino /* The index is a signed object `sizetype' bits wide. */
5247e4b17023SJohn Marino tree index_type = c_common_signed_type (sizetype);
5248e4b17023SJohn Marino
5249e4b17023SJohn Marino array_ptr_quals = declarator->u.array.quals;
5250e4b17023SJohn Marino array_ptr_attrs = declarator->u.array.attrs;
5251e4b17023SJohn Marino array_parm_static = declarator->u.array.static_p;
5252e4b17023SJohn Marino array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
5253e4b17023SJohn Marino
5254e4b17023SJohn Marino declarator = declarator->declarator;
5255e4b17023SJohn Marino
5256e4b17023SJohn Marino /* Check for some types that there cannot be arrays of. */
5257e4b17023SJohn Marino
5258e4b17023SJohn Marino if (VOID_TYPE_P (type))
5259e4b17023SJohn Marino {
5260e4b17023SJohn Marino if (name)
5261e4b17023SJohn Marino error_at (loc, "declaration of %qE as array of voids", name);
5262e4b17023SJohn Marino else
5263e4b17023SJohn Marino error_at (loc, "declaration of type name as array of voids");
5264e4b17023SJohn Marino type = error_mark_node;
5265e4b17023SJohn Marino }
5266e4b17023SJohn Marino
5267e4b17023SJohn Marino if (TREE_CODE (type) == FUNCTION_TYPE)
5268e4b17023SJohn Marino {
5269e4b17023SJohn Marino if (name)
5270e4b17023SJohn Marino error_at (loc, "declaration of %qE as array of functions",
5271e4b17023SJohn Marino name);
5272e4b17023SJohn Marino else
5273e4b17023SJohn Marino error_at (loc, "declaration of type name as array of "
5274e4b17023SJohn Marino "functions");
5275e4b17023SJohn Marino type = error_mark_node;
5276e4b17023SJohn Marino }
5277e4b17023SJohn Marino
5278e4b17023SJohn Marino if (pedantic && !in_system_header && flexible_array_type_p (type))
5279e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5280e4b17023SJohn Marino "invalid use of structure with flexible array member");
5281e4b17023SJohn Marino
5282e4b17023SJohn Marino if (size == error_mark_node)
5283e4b17023SJohn Marino type = error_mark_node;
5284e4b17023SJohn Marino
5285e4b17023SJohn Marino if (type == error_mark_node)
5286e4b17023SJohn Marino continue;
5287e4b17023SJohn Marino
5288e4b17023SJohn Marino /* If size was specified, set ITYPE to a range-type for
5289e4b17023SJohn Marino that size. Otherwise, ITYPE remains null. finish_decl
5290e4b17023SJohn Marino may figure it out from an initial value. */
5291e4b17023SJohn Marino
5292e4b17023SJohn Marino if (size)
5293e4b17023SJohn Marino {
5294e4b17023SJohn Marino bool size_maybe_const = true;
5295e4b17023SJohn Marino bool size_int_const = (TREE_CODE (size) == INTEGER_CST
5296e4b17023SJohn Marino && !TREE_OVERFLOW (size));
5297e4b17023SJohn Marino bool this_size_varies = false;
5298e4b17023SJohn Marino
5299e4b17023SJohn Marino /* Strip NON_LVALUE_EXPRs since we aren't using as an
5300e4b17023SJohn Marino lvalue. */
5301e4b17023SJohn Marino STRIP_TYPE_NOPS (size);
5302e4b17023SJohn Marino
5303e4b17023SJohn Marino if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
5304e4b17023SJohn Marino {
5305e4b17023SJohn Marino if (name)
5306e4b17023SJohn Marino error_at (loc, "size of array %qE has non-integer type",
5307e4b17023SJohn Marino name);
5308e4b17023SJohn Marino else
5309e4b17023SJohn Marino error_at (loc,
5310e4b17023SJohn Marino "size of unnamed array has non-integer type");
5311e4b17023SJohn Marino size = integer_one_node;
5312e4b17023SJohn Marino }
5313e4b17023SJohn Marino
5314e4b17023SJohn Marino size = c_fully_fold (size, false, &size_maybe_const);
5315e4b17023SJohn Marino
5316e4b17023SJohn Marino if (pedantic && size_maybe_const && integer_zerop (size))
5317e4b17023SJohn Marino {
5318e4b17023SJohn Marino if (name)
5319e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5320e4b17023SJohn Marino "ISO C forbids zero-size array %qE", name);
5321e4b17023SJohn Marino else
5322e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5323e4b17023SJohn Marino "ISO C forbids zero-size array");
5324e4b17023SJohn Marino }
5325e4b17023SJohn Marino
5326e4b17023SJohn Marino if (TREE_CODE (size) == INTEGER_CST && size_maybe_const)
5327e4b17023SJohn Marino {
5328e4b17023SJohn Marino constant_expression_warning (size);
5329e4b17023SJohn Marino if (tree_int_cst_sgn (size) < 0)
5330e4b17023SJohn Marino {
5331e4b17023SJohn Marino if (name)
5332e4b17023SJohn Marino error_at (loc, "size of array %qE is negative", name);
5333e4b17023SJohn Marino else
5334e4b17023SJohn Marino error_at (loc, "size of unnamed array is negative");
5335e4b17023SJohn Marino size = integer_one_node;
5336e4b17023SJohn Marino }
5337e4b17023SJohn Marino /* Handle a size folded to an integer constant but
5338e4b17023SJohn Marino not an integer constant expression. */
5339e4b17023SJohn Marino if (!size_int_const)
5340e4b17023SJohn Marino {
5341e4b17023SJohn Marino /* If this is a file scope declaration of an
5342e4b17023SJohn Marino ordinary identifier, this is invalid code;
5343e4b17023SJohn Marino diagnosing it here and not subsequently
5344e4b17023SJohn Marino treating the type as variable-length avoids
5345e4b17023SJohn Marino more confusing diagnostics later. */
5346e4b17023SJohn Marino if ((decl_context == NORMAL || decl_context == FIELD)
5347e4b17023SJohn Marino && current_scope == file_scope)
5348e4b17023SJohn Marino pedwarn (input_location, 0,
5349e4b17023SJohn Marino "variably modified %qE at file scope",
5350e4b17023SJohn Marino name);
5351e4b17023SJohn Marino else
5352e4b17023SJohn Marino this_size_varies = size_varies = true;
5353e4b17023SJohn Marino warn_variable_length_array (name, size);
5354e4b17023SJohn Marino }
5355e4b17023SJohn Marino }
5356e4b17023SJohn Marino else if ((decl_context == NORMAL || decl_context == FIELD)
5357e4b17023SJohn Marino && current_scope == file_scope)
5358e4b17023SJohn Marino {
5359e4b17023SJohn Marino error_at (loc, "variably modified %qE at file scope", name);
5360e4b17023SJohn Marino size = integer_one_node;
5361e4b17023SJohn Marino }
5362e4b17023SJohn Marino else
5363e4b17023SJohn Marino {
5364e4b17023SJohn Marino /* Make sure the array size remains visibly
5365e4b17023SJohn Marino nonconstant even if it is (eg) a const variable
5366e4b17023SJohn Marino with known value. */
5367e4b17023SJohn Marino this_size_varies = size_varies = true;
5368e4b17023SJohn Marino warn_variable_length_array (name, size);
5369e4b17023SJohn Marino }
5370e4b17023SJohn Marino
5371e4b17023SJohn Marino if (integer_zerop (size) && !this_size_varies)
5372e4b17023SJohn Marino {
5373e4b17023SJohn Marino /* A zero-length array cannot be represented with
5374e4b17023SJohn Marino an unsigned index type, which is what we'll
5375e4b17023SJohn Marino get with build_index_type. Create an
5376e4b17023SJohn Marino open-ended range instead. */
5377e4b17023SJohn Marino itype = build_range_type (sizetype, size, NULL_TREE);
5378e4b17023SJohn Marino }
5379e4b17023SJohn Marino else
5380e4b17023SJohn Marino {
5381e4b17023SJohn Marino /* Arrange for the SAVE_EXPR on the inside of the
5382e4b17023SJohn Marino MINUS_EXPR, which allows the -1 to get folded
5383e4b17023SJohn Marino with the +1 that happens when building TYPE_SIZE. */
5384e4b17023SJohn Marino if (size_varies)
5385e4b17023SJohn Marino size = save_expr (size);
5386e4b17023SJohn Marino if (this_size_varies && TREE_CODE (size) == INTEGER_CST)
5387e4b17023SJohn Marino size = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5388e4b17023SJohn Marino integer_zero_node, size);
5389e4b17023SJohn Marino
5390e4b17023SJohn Marino /* Compute the maximum valid index, that is, size
5391e4b17023SJohn Marino - 1. Do the calculation in index_type, so that
5392e4b17023SJohn Marino if it is a variable the computations will be
5393e4b17023SJohn Marino done in the proper mode. */
5394e4b17023SJohn Marino itype = fold_build2_loc (loc, MINUS_EXPR, index_type,
5395e4b17023SJohn Marino convert (index_type, size),
5396e4b17023SJohn Marino convert (index_type,
5397e4b17023SJohn Marino size_one_node));
5398e4b17023SJohn Marino
5399e4b17023SJohn Marino /* The above overflows when size does not fit
5400e4b17023SJohn Marino in index_type.
5401e4b17023SJohn Marino ??? While a size of INT_MAX+1 technically shouldn't
5402e4b17023SJohn Marino cause an overflow (because we subtract 1), handling
5403e4b17023SJohn Marino this case seems like an unnecessary complication. */
5404e4b17023SJohn Marino if (TREE_CODE (size) == INTEGER_CST
5405e4b17023SJohn Marino && !int_fits_type_p (size, index_type))
5406e4b17023SJohn Marino {
5407e4b17023SJohn Marino if (name)
5408e4b17023SJohn Marino error_at (loc, "size of array %qE is too large",
5409e4b17023SJohn Marino name);
5410e4b17023SJohn Marino else
5411e4b17023SJohn Marino error_at (loc, "size of unnamed array is too large");
5412e4b17023SJohn Marino type = error_mark_node;
5413e4b17023SJohn Marino continue;
5414e4b17023SJohn Marino }
5415e4b17023SJohn Marino
5416e4b17023SJohn Marino itype = build_index_type (itype);
5417e4b17023SJohn Marino }
5418e4b17023SJohn Marino if (this_size_varies)
5419e4b17023SJohn Marino {
5420e4b17023SJohn Marino if (*expr)
5421e4b17023SJohn Marino *expr = build2 (COMPOUND_EXPR, TREE_TYPE (size),
5422e4b17023SJohn Marino *expr, size);
5423e4b17023SJohn Marino else
5424e4b17023SJohn Marino *expr = size;
5425e4b17023SJohn Marino *expr_const_operands &= size_maybe_const;
5426e4b17023SJohn Marino }
5427e4b17023SJohn Marino }
5428e4b17023SJohn Marino else if (decl_context == FIELD)
5429e4b17023SJohn Marino {
5430e4b17023SJohn Marino bool flexible_array_member = false;
5431e4b17023SJohn Marino if (array_parm_vla_unspec_p)
5432e4b17023SJohn Marino /* Field names can in fact have function prototype
5433e4b17023SJohn Marino scope so [*] is disallowed here through making
5434e4b17023SJohn Marino the field variably modified, not through being
5435e4b17023SJohn Marino something other than a declaration with function
5436e4b17023SJohn Marino prototype scope. */
5437e4b17023SJohn Marino size_varies = true;
5438e4b17023SJohn Marino else
5439e4b17023SJohn Marino {
5440e4b17023SJohn Marino const struct c_declarator *t = declarator;
5441e4b17023SJohn Marino while (t->kind == cdk_attrs)
5442e4b17023SJohn Marino t = t->declarator;
5443e4b17023SJohn Marino flexible_array_member = (t->kind == cdk_id);
5444e4b17023SJohn Marino }
5445e4b17023SJohn Marino if (flexible_array_member
5446e4b17023SJohn Marino && pedantic && !flag_isoc99 && !in_system_header)
5447e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5448e4b17023SJohn Marino "ISO C90 does not support flexible array members");
5449e4b17023SJohn Marino
5450e4b17023SJohn Marino /* ISO C99 Flexible array members are effectively
5451e4b17023SJohn Marino identical to GCC's zero-length array extension. */
5452e4b17023SJohn Marino if (flexible_array_member || array_parm_vla_unspec_p)
5453e4b17023SJohn Marino itype = build_range_type (sizetype, size_zero_node,
5454e4b17023SJohn Marino NULL_TREE);
5455e4b17023SJohn Marino }
5456e4b17023SJohn Marino else if (decl_context == PARM)
5457e4b17023SJohn Marino {
5458e4b17023SJohn Marino if (array_parm_vla_unspec_p)
5459e4b17023SJohn Marino {
5460e4b17023SJohn Marino itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
5461e4b17023SJohn Marino size_varies = true;
5462e4b17023SJohn Marino }
5463e4b17023SJohn Marino }
5464e4b17023SJohn Marino else if (decl_context == TYPENAME)
5465e4b17023SJohn Marino {
5466e4b17023SJohn Marino if (array_parm_vla_unspec_p)
5467e4b17023SJohn Marino {
5468e4b17023SJohn Marino /* C99 6.7.5.2p4 */
5469e4b17023SJohn Marino warning (0, "%<[*]%> not in a declaration");
5470e4b17023SJohn Marino /* We use this to avoid messing up with incomplete
5471e4b17023SJohn Marino array types of the same type, that would
5472e4b17023SJohn Marino otherwise be modified below. */
5473e4b17023SJohn Marino itype = build_range_type (sizetype, size_zero_node,
5474e4b17023SJohn Marino NULL_TREE);
5475e4b17023SJohn Marino size_varies = true;
5476e4b17023SJohn Marino }
5477e4b17023SJohn Marino }
5478e4b17023SJohn Marino
5479e4b17023SJohn Marino /* Complain about arrays of incomplete types. */
5480e4b17023SJohn Marino if (!COMPLETE_TYPE_P (type))
5481e4b17023SJohn Marino {
5482e4b17023SJohn Marino error_at (loc, "array type has incomplete element type");
5483e4b17023SJohn Marino type = error_mark_node;
5484e4b17023SJohn Marino }
5485e4b17023SJohn Marino else
5486e4b17023SJohn Marino /* When itype is NULL, a shared incomplete array type is
5487e4b17023SJohn Marino returned for all array of a given type. Elsewhere we
5488e4b17023SJohn Marino make sure we don't complete that type before copying
5489e4b17023SJohn Marino it, but here we want to make sure we don't ever
5490e4b17023SJohn Marino modify the shared type, so we gcc_assert (itype)
5491e4b17023SJohn Marino below. */
5492e4b17023SJohn Marino {
5493e4b17023SJohn Marino addr_space_t as = DECODE_QUAL_ADDR_SPACE (type_quals);
5494e4b17023SJohn Marino if (!ADDR_SPACE_GENERIC_P (as) && as != TYPE_ADDR_SPACE (type))
5495e4b17023SJohn Marino type = build_qualified_type (type,
5496e4b17023SJohn Marino ENCODE_QUAL_ADDR_SPACE (as));
5497e4b17023SJohn Marino
5498e4b17023SJohn Marino type = build_array_type (type, itype);
5499e4b17023SJohn Marino }
5500e4b17023SJohn Marino
5501e4b17023SJohn Marino if (type != error_mark_node)
5502e4b17023SJohn Marino {
5503e4b17023SJohn Marino if (size_varies)
5504e4b17023SJohn Marino {
5505e4b17023SJohn Marino /* It is ok to modify type here even if itype is
5506e4b17023SJohn Marino NULL: if size_varies, we're in a
5507e4b17023SJohn Marino multi-dimensional array and the inner type has
5508e4b17023SJohn Marino variable size, so the enclosing shared array type
5509e4b17023SJohn Marino must too. */
5510e4b17023SJohn Marino if (size && TREE_CODE (size) == INTEGER_CST)
5511e4b17023SJohn Marino type
5512e4b17023SJohn Marino = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5513e4b17023SJohn Marino C_TYPE_VARIABLE_SIZE (type) = 1;
5514e4b17023SJohn Marino }
5515e4b17023SJohn Marino
5516e4b17023SJohn Marino /* The GCC extension for zero-length arrays differs from
5517e4b17023SJohn Marino ISO flexible array members in that sizeof yields
5518e4b17023SJohn Marino zero. */
5519e4b17023SJohn Marino if (size && integer_zerop (size))
5520e4b17023SJohn Marino {
5521e4b17023SJohn Marino gcc_assert (itype);
5522e4b17023SJohn Marino type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5523e4b17023SJohn Marino TYPE_SIZE (type) = bitsize_zero_node;
5524e4b17023SJohn Marino TYPE_SIZE_UNIT (type) = size_zero_node;
5525e4b17023SJohn Marino SET_TYPE_STRUCTURAL_EQUALITY (type);
5526e4b17023SJohn Marino }
5527e4b17023SJohn Marino if (array_parm_vla_unspec_p)
5528e4b17023SJohn Marino {
5529e4b17023SJohn Marino gcc_assert (itype);
5530e4b17023SJohn Marino /* The type is complete. C99 6.7.5.2p4 */
5531e4b17023SJohn Marino type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
5532e4b17023SJohn Marino TYPE_SIZE (type) = bitsize_zero_node;
5533e4b17023SJohn Marino TYPE_SIZE_UNIT (type) = size_zero_node;
5534e4b17023SJohn Marino SET_TYPE_STRUCTURAL_EQUALITY (type);
5535e4b17023SJohn Marino }
5536e4b17023SJohn Marino }
5537e4b17023SJohn Marino
5538e4b17023SJohn Marino if (decl_context != PARM
5539e4b17023SJohn Marino && (array_ptr_quals != TYPE_UNQUALIFIED
5540e4b17023SJohn Marino || array_ptr_attrs != NULL_TREE
5541e4b17023SJohn Marino || array_parm_static))
5542e4b17023SJohn Marino {
5543e4b17023SJohn Marino error_at (loc, "static or type qualifiers in non-parameter array declarator");
5544e4b17023SJohn Marino array_ptr_quals = TYPE_UNQUALIFIED;
5545e4b17023SJohn Marino array_ptr_attrs = NULL_TREE;
5546e4b17023SJohn Marino array_parm_static = 0;
5547e4b17023SJohn Marino }
5548e4b17023SJohn Marino break;
5549e4b17023SJohn Marino }
5550e4b17023SJohn Marino case cdk_function:
5551e4b17023SJohn Marino {
5552e4b17023SJohn Marino /* Say it's a definition only for the declarator closest
5553e4b17023SJohn Marino to the identifier, apart possibly from some
5554e4b17023SJohn Marino attributes. */
5555e4b17023SJohn Marino bool really_funcdef = false;
5556e4b17023SJohn Marino tree arg_types;
5557e4b17023SJohn Marino if (funcdef_flag)
5558e4b17023SJohn Marino {
5559e4b17023SJohn Marino const struct c_declarator *t = declarator->declarator;
5560e4b17023SJohn Marino while (t->kind == cdk_attrs)
5561e4b17023SJohn Marino t = t->declarator;
5562e4b17023SJohn Marino really_funcdef = (t->kind == cdk_id);
5563e4b17023SJohn Marino }
5564e4b17023SJohn Marino
5565e4b17023SJohn Marino /* Declaring a function type. Make sure we have a valid
5566e4b17023SJohn Marino type for the function to return. */
5567e4b17023SJohn Marino if (type == error_mark_node)
5568e4b17023SJohn Marino continue;
5569e4b17023SJohn Marino
5570e4b17023SJohn Marino size_varies = false;
5571e4b17023SJohn Marino
5572e4b17023SJohn Marino /* Warn about some types functions can't return. */
5573e4b17023SJohn Marino if (TREE_CODE (type) == FUNCTION_TYPE)
5574e4b17023SJohn Marino {
5575e4b17023SJohn Marino if (name)
5576e4b17023SJohn Marino error_at (loc, "%qE declared as function returning a "
5577e4b17023SJohn Marino "function", name);
5578e4b17023SJohn Marino else
5579e4b17023SJohn Marino error_at (loc, "type name declared as function "
5580e4b17023SJohn Marino "returning a function");
5581e4b17023SJohn Marino type = integer_type_node;
5582e4b17023SJohn Marino }
5583e4b17023SJohn Marino if (TREE_CODE (type) == ARRAY_TYPE)
5584e4b17023SJohn Marino {
5585e4b17023SJohn Marino if (name)
5586e4b17023SJohn Marino error_at (loc, "%qE declared as function returning an array",
5587e4b17023SJohn Marino name);
5588e4b17023SJohn Marino else
5589e4b17023SJohn Marino error_at (loc, "type name declared as function returning "
5590e4b17023SJohn Marino "an array");
5591e4b17023SJohn Marino type = integer_type_node;
5592e4b17023SJohn Marino }
5593e4b17023SJohn Marino errmsg = targetm.invalid_return_type (type);
5594e4b17023SJohn Marino if (errmsg)
5595e4b17023SJohn Marino {
5596e4b17023SJohn Marino error (errmsg);
5597e4b17023SJohn Marino type = integer_type_node;
5598e4b17023SJohn Marino }
5599e4b17023SJohn Marino
5600e4b17023SJohn Marino /* Construct the function type and go to the next
5601e4b17023SJohn Marino inner layer of declarator. */
5602e4b17023SJohn Marino arg_info = declarator->u.arg_info;
5603e4b17023SJohn Marino arg_types = grokparms (arg_info, really_funcdef);
5604e4b17023SJohn Marino
5605e4b17023SJohn Marino /* Type qualifiers before the return type of the function
5606e4b17023SJohn Marino qualify the return type, not the function type. */
5607e4b17023SJohn Marino if (type_quals)
5608e4b17023SJohn Marino {
5609e4b17023SJohn Marino /* Type qualifiers on a function return type are
5610e4b17023SJohn Marino normally permitted by the standard but have no
5611e4b17023SJohn Marino effect, so give a warning at -Wreturn-type.
5612e4b17023SJohn Marino Qualifiers on a void return type are banned on
5613e4b17023SJohn Marino function definitions in ISO C; GCC used to used
5614e4b17023SJohn Marino them for noreturn functions. */
5615e4b17023SJohn Marino if (VOID_TYPE_P (type) && really_funcdef)
5616e4b17023SJohn Marino pedwarn (loc, 0,
5617e4b17023SJohn Marino "function definition has qualified void return type");
5618e4b17023SJohn Marino else
5619e4b17023SJohn Marino warning_at (loc, OPT_Wignored_qualifiers,
5620e4b17023SJohn Marino "type qualifiers ignored on function return type");
5621e4b17023SJohn Marino
5622e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5623e4b17023SJohn Marino }
5624e4b17023SJohn Marino type_quals = TYPE_UNQUALIFIED;
5625e4b17023SJohn Marino
5626e4b17023SJohn Marino type = build_function_type (type, arg_types);
5627e4b17023SJohn Marino declarator = declarator->declarator;
5628e4b17023SJohn Marino
5629e4b17023SJohn Marino /* Set the TYPE_CONTEXTs for each tagged type which is local to
5630e4b17023SJohn Marino the formal parameter list of this FUNCTION_TYPE to point to
5631e4b17023SJohn Marino the FUNCTION_TYPE node itself. */
5632e4b17023SJohn Marino {
5633e4b17023SJohn Marino c_arg_tag *tag;
5634e4b17023SJohn Marino unsigned ix;
5635e4b17023SJohn Marino
5636e4b17023SJohn Marino FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
5637e4b17023SJohn Marino TYPE_CONTEXT (tag->type) = type;
5638e4b17023SJohn Marino }
5639e4b17023SJohn Marino break;
5640e4b17023SJohn Marino }
5641e4b17023SJohn Marino case cdk_pointer:
5642e4b17023SJohn Marino {
5643e4b17023SJohn Marino /* Merge any constancy or volatility into the target type
5644e4b17023SJohn Marino for the pointer. */
5645e4b17023SJohn Marino
5646e4b17023SJohn Marino if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5647e4b17023SJohn Marino && type_quals)
5648e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5649e4b17023SJohn Marino "ISO C forbids qualified function types");
5650e4b17023SJohn Marino if (type_quals)
5651e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5652e4b17023SJohn Marino size_varies = false;
5653e4b17023SJohn Marino
5654e4b17023SJohn Marino /* When the pointed-to type involves components of variable size,
5655e4b17023SJohn Marino care must be taken to ensure that the size evaluation code is
5656e4b17023SJohn Marino emitted early enough to dominate all the possible later uses
5657e4b17023SJohn Marino and late enough for the variables on which it depends to have
5658e4b17023SJohn Marino been assigned.
5659e4b17023SJohn Marino
5660e4b17023SJohn Marino This is expected to happen automatically when the pointed-to
5661e4b17023SJohn Marino type has a name/declaration of it's own, but special attention
5662e4b17023SJohn Marino is required if the type is anonymous.
5663e4b17023SJohn Marino
5664e4b17023SJohn Marino We handle the NORMAL and FIELD contexts here by attaching an
5665e4b17023SJohn Marino artificial TYPE_DECL to such pointed-to type. This forces the
5666e4b17023SJohn Marino sizes evaluation at a safe point and ensures it is not deferred
5667e4b17023SJohn Marino until e.g. within a deeper conditional context.
5668e4b17023SJohn Marino
5669e4b17023SJohn Marino We expect nothing to be needed here for PARM or TYPENAME.
5670e4b17023SJohn Marino Pushing a TYPE_DECL at this point for TYPENAME would actually
5671e4b17023SJohn Marino be incorrect, as we might be in the middle of an expression
5672e4b17023SJohn Marino with side effects on the pointed-to type size "arguments" prior
5673e4b17023SJohn Marino to the pointer declaration point and the fake TYPE_DECL in the
5674e4b17023SJohn Marino enclosing context would force the size evaluation prior to the
5675e4b17023SJohn Marino side effects. */
5676e4b17023SJohn Marino
5677e4b17023SJohn Marino if (!TYPE_NAME (type)
5678e4b17023SJohn Marino && (decl_context == NORMAL || decl_context == FIELD)
5679e4b17023SJohn Marino && variably_modified_type_p (type, NULL_TREE))
5680e4b17023SJohn Marino {
5681e4b17023SJohn Marino tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
5682e4b17023SJohn Marino DECL_ARTIFICIAL (decl) = 1;
5683e4b17023SJohn Marino pushdecl (decl);
5684e4b17023SJohn Marino finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
5685e4b17023SJohn Marino TYPE_NAME (type) = decl;
5686e4b17023SJohn Marino }
5687e4b17023SJohn Marino
5688e4b17023SJohn Marino type = build_pointer_type (type);
5689e4b17023SJohn Marino
5690e4b17023SJohn Marino /* Process type qualifiers (such as const or volatile)
5691e4b17023SJohn Marino that were given inside the `*'. */
5692e4b17023SJohn Marino type_quals = declarator->u.pointer_quals;
5693e4b17023SJohn Marino
5694e4b17023SJohn Marino declarator = declarator->declarator;
5695e4b17023SJohn Marino break;
5696e4b17023SJohn Marino }
5697e4b17023SJohn Marino default:
5698e4b17023SJohn Marino gcc_unreachable ();
5699e4b17023SJohn Marino }
5700e4b17023SJohn Marino }
5701e4b17023SJohn Marino *decl_attrs = chainon (returned_attrs, *decl_attrs);
5702e4b17023SJohn Marino
5703e4b17023SJohn Marino /* Now TYPE has the actual type, apart from any qualifiers in
5704e4b17023SJohn Marino TYPE_QUALS. */
5705e4b17023SJohn Marino
5706e4b17023SJohn Marino /* Warn about address space used for things other than static memory or
5707e4b17023SJohn Marino pointers. */
5708e4b17023SJohn Marino address_space = DECODE_QUAL_ADDR_SPACE (type_quals);
5709e4b17023SJohn Marino if (!ADDR_SPACE_GENERIC_P (address_space))
5710e4b17023SJohn Marino {
5711e4b17023SJohn Marino if (decl_context == NORMAL)
5712e4b17023SJohn Marino {
5713e4b17023SJohn Marino switch (storage_class)
5714e4b17023SJohn Marino {
5715e4b17023SJohn Marino case csc_auto:
5716e4b17023SJohn Marino error ("%qs combined with %<auto%> qualifier for %qE",
5717e4b17023SJohn Marino c_addr_space_name (address_space), name);
5718e4b17023SJohn Marino break;
5719e4b17023SJohn Marino case csc_register:
5720e4b17023SJohn Marino error ("%qs combined with %<register%> qualifier for %qE",
5721e4b17023SJohn Marino c_addr_space_name (address_space), name);
5722e4b17023SJohn Marino break;
5723e4b17023SJohn Marino case csc_none:
5724e4b17023SJohn Marino if (current_function_scope)
5725e4b17023SJohn Marino {
5726e4b17023SJohn Marino error ("%qs specified for auto variable %qE",
5727e4b17023SJohn Marino c_addr_space_name (address_space), name);
5728e4b17023SJohn Marino break;
5729e4b17023SJohn Marino }
5730e4b17023SJohn Marino break;
5731e4b17023SJohn Marino case csc_static:
5732e4b17023SJohn Marino case csc_extern:
5733e4b17023SJohn Marino case csc_typedef:
5734e4b17023SJohn Marino break;
5735e4b17023SJohn Marino default:
5736e4b17023SJohn Marino gcc_unreachable ();
5737e4b17023SJohn Marino }
5738e4b17023SJohn Marino }
5739e4b17023SJohn Marino else if (decl_context == PARM && TREE_CODE (type) != ARRAY_TYPE)
5740e4b17023SJohn Marino {
5741e4b17023SJohn Marino if (name)
5742e4b17023SJohn Marino error ("%qs specified for parameter %qE",
5743e4b17023SJohn Marino c_addr_space_name (address_space), name);
5744e4b17023SJohn Marino else
5745e4b17023SJohn Marino error ("%qs specified for unnamed parameter",
5746e4b17023SJohn Marino c_addr_space_name (address_space));
5747e4b17023SJohn Marino }
5748e4b17023SJohn Marino else if (decl_context == FIELD)
5749e4b17023SJohn Marino {
5750e4b17023SJohn Marino if (name)
5751e4b17023SJohn Marino error ("%qs specified for structure field %qE",
5752e4b17023SJohn Marino c_addr_space_name (address_space), name);
5753e4b17023SJohn Marino else
5754e4b17023SJohn Marino error ("%qs specified for structure field",
5755e4b17023SJohn Marino c_addr_space_name (address_space));
5756e4b17023SJohn Marino }
5757e4b17023SJohn Marino }
5758e4b17023SJohn Marino
5759e4b17023SJohn Marino /* Check the type and width of a bit-field. */
5760e4b17023SJohn Marino if (bitfield)
5761e4b17023SJohn Marino check_bitfield_type_and_width (&type, width, name);
5762e4b17023SJohn Marino
5763e4b17023SJohn Marino /* Reject invalid uses of _Alignas. */
5764e4b17023SJohn Marino if (declspecs->alignas_p)
5765e4b17023SJohn Marino {
5766e4b17023SJohn Marino if (storage_class == csc_typedef)
5767e4b17023SJohn Marino error_at (loc, "alignment specified for typedef %qE", name);
5768e4b17023SJohn Marino else if (storage_class == csc_register)
5769e4b17023SJohn Marino error_at (loc, "alignment specified for %<register%> object %qE",
5770e4b17023SJohn Marino name);
5771e4b17023SJohn Marino else if (decl_context == PARM)
5772e4b17023SJohn Marino {
5773e4b17023SJohn Marino if (name)
5774e4b17023SJohn Marino error_at (loc, "alignment specified for parameter %qE", name);
5775e4b17023SJohn Marino else
5776e4b17023SJohn Marino error_at (loc, "alignment specified for unnamed parameter");
5777e4b17023SJohn Marino }
5778e4b17023SJohn Marino else if (bitfield)
5779e4b17023SJohn Marino {
5780e4b17023SJohn Marino if (name)
5781e4b17023SJohn Marino error_at (loc, "alignment specified for bit-field %qE", name);
5782e4b17023SJohn Marino else
5783e4b17023SJohn Marino error_at (loc, "alignment specified for unnamed bit-field");
5784e4b17023SJohn Marino }
5785e4b17023SJohn Marino else if (TREE_CODE (type) == FUNCTION_TYPE)
5786e4b17023SJohn Marino error_at (loc, "alignment specified for function %qE", name);
5787e4b17023SJohn Marino else if (declspecs->align_log != -1)
5788e4b17023SJohn Marino {
5789e4b17023SJohn Marino alignas_align = 1U << declspecs->align_log;
5790e4b17023SJohn Marino if (alignas_align < TYPE_ALIGN_UNIT (type))
5791e4b17023SJohn Marino {
5792e4b17023SJohn Marino if (name)
5793e4b17023SJohn Marino error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5794e4b17023SJohn Marino "alignment of %qE", name);
5795e4b17023SJohn Marino else
5796e4b17023SJohn Marino error_at (loc, "%<_Alignas%> specifiers cannot reduce "
5797e4b17023SJohn Marino "alignment of unnamed field");
5798e4b17023SJohn Marino alignas_align = 0;
5799e4b17023SJohn Marino }
5800e4b17023SJohn Marino }
5801e4b17023SJohn Marino }
5802e4b17023SJohn Marino
5803e4b17023SJohn Marino /* Did array size calculations overflow? */
5804e4b17023SJohn Marino
5805e4b17023SJohn Marino if (TREE_CODE (type) == ARRAY_TYPE
5806e4b17023SJohn Marino && COMPLETE_TYPE_P (type)
5807e4b17023SJohn Marino && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
5808e4b17023SJohn Marino && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
5809e4b17023SJohn Marino {
5810e4b17023SJohn Marino if (name)
5811e4b17023SJohn Marino error_at (loc, "size of array %qE is too large", name);
5812e4b17023SJohn Marino else
5813e4b17023SJohn Marino error_at (loc, "size of unnamed array is too large");
5814e4b17023SJohn Marino /* If we proceed with the array type as it is, we'll eventually
5815e4b17023SJohn Marino crash in tree_low_cst(). */
5816e4b17023SJohn Marino type = error_mark_node;
5817e4b17023SJohn Marino }
5818e4b17023SJohn Marino
5819e4b17023SJohn Marino /* If this is declaring a typedef name, return a TYPE_DECL. */
5820e4b17023SJohn Marino
5821e4b17023SJohn Marino if (storage_class == csc_typedef)
5822e4b17023SJohn Marino {
5823e4b17023SJohn Marino tree decl;
5824e4b17023SJohn Marino if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5825e4b17023SJohn Marino && type_quals)
5826e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5827e4b17023SJohn Marino "ISO C forbids qualified function types");
5828e4b17023SJohn Marino if (type_quals)
5829e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5830e4b17023SJohn Marino decl = build_decl (declarator->id_loc,
5831e4b17023SJohn Marino TYPE_DECL, declarator->u.id, type);
5832e4b17023SJohn Marino if (declspecs->explicit_signed_p)
5833e4b17023SJohn Marino C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
5834e4b17023SJohn Marino if (declspecs->inline_p)
5835e4b17023SJohn Marino pedwarn (loc, 0,"typedef %q+D declared %<inline%>", decl);
5836e4b17023SJohn Marino if (declspecs->noreturn_p)
5837e4b17023SJohn Marino pedwarn (loc, 0,"typedef %q+D declared %<_Noreturn%>", decl);
5838e4b17023SJohn Marino
5839e4b17023SJohn Marino if (warn_cxx_compat && declarator->u.id != NULL_TREE)
5840e4b17023SJohn Marino {
5841e4b17023SJohn Marino struct c_binding *b = I_TAG_BINDING (declarator->u.id);
5842e4b17023SJohn Marino
5843e4b17023SJohn Marino if (b != NULL
5844e4b17023SJohn Marino && b->decl != NULL_TREE
5845e4b17023SJohn Marino && (B_IN_CURRENT_SCOPE (b)
5846e4b17023SJohn Marino || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
5847e4b17023SJohn Marino && TYPE_MAIN_VARIANT (b->decl) != TYPE_MAIN_VARIANT (type))
5848e4b17023SJohn Marino {
5849e4b17023SJohn Marino warning_at (declarator->id_loc, OPT_Wc___compat,
5850e4b17023SJohn Marino ("using %qD as both a typedef and a tag is "
5851e4b17023SJohn Marino "invalid in C++"),
5852e4b17023SJohn Marino decl);
5853e4b17023SJohn Marino if (b->locus != UNKNOWN_LOCATION)
5854e4b17023SJohn Marino inform (b->locus, "originally defined here");
5855e4b17023SJohn Marino }
5856e4b17023SJohn Marino }
5857e4b17023SJohn Marino
5858e4b17023SJohn Marino return decl;
5859e4b17023SJohn Marino }
5860e4b17023SJohn Marino
5861e4b17023SJohn Marino /* If this is a type name (such as, in a cast or sizeof),
5862e4b17023SJohn Marino compute the type and return it now. */
5863e4b17023SJohn Marino
5864e4b17023SJohn Marino if (decl_context == TYPENAME)
5865e4b17023SJohn Marino {
5866e4b17023SJohn Marino /* Note that the grammar rejects storage classes in typenames
5867e4b17023SJohn Marino and fields. */
5868e4b17023SJohn Marino gcc_assert (storage_class == csc_none && !threadp
5869e4b17023SJohn Marino && !declspecs->inline_p && !declspecs->noreturn_p);
5870e4b17023SJohn Marino if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
5871e4b17023SJohn Marino && type_quals)
5872e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5873e4b17023SJohn Marino "ISO C forbids const or volatile function types");
5874e4b17023SJohn Marino if (type_quals)
5875e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5876e4b17023SJohn Marino return type;
5877e4b17023SJohn Marino }
5878e4b17023SJohn Marino
5879e4b17023SJohn Marino if (pedantic && decl_context == FIELD
5880e4b17023SJohn Marino && variably_modified_type_p (type, NULL_TREE))
5881e4b17023SJohn Marino {
5882e4b17023SJohn Marino /* C99 6.7.2.1p8 */
5883e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "a member of a structure or union cannot "
5884e4b17023SJohn Marino "have a variably modified type");
5885e4b17023SJohn Marino }
5886e4b17023SJohn Marino
5887e4b17023SJohn Marino /* Aside from typedefs and type names (handle above),
5888e4b17023SJohn Marino `void' at top level (not within pointer)
5889e4b17023SJohn Marino is allowed only in public variables.
5890e4b17023SJohn Marino We don't complain about parms either, but that is because
5891e4b17023SJohn Marino a better error message can be made later. */
5892e4b17023SJohn Marino
5893e4b17023SJohn Marino if (VOID_TYPE_P (type) && decl_context != PARM
5894e4b17023SJohn Marino && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
5895e4b17023SJohn Marino && (storage_class == csc_extern
5896e4b17023SJohn Marino || (current_scope == file_scope
5897e4b17023SJohn Marino && !(storage_class == csc_static
5898e4b17023SJohn Marino || storage_class == csc_register)))))
5899e4b17023SJohn Marino {
5900e4b17023SJohn Marino error_at (loc, "variable or field %qE declared void", name);
5901e4b17023SJohn Marino type = integer_type_node;
5902e4b17023SJohn Marino }
5903e4b17023SJohn Marino
5904e4b17023SJohn Marino /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
5905e4b17023SJohn Marino or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
5906e4b17023SJohn Marino
5907e4b17023SJohn Marino {
5908e4b17023SJohn Marino tree decl;
5909e4b17023SJohn Marino
5910e4b17023SJohn Marino if (decl_context == PARM)
5911e4b17023SJohn Marino {
5912e4b17023SJohn Marino tree promoted_type;
5913e4b17023SJohn Marino
5914e4b17023SJohn Marino /* A parameter declared as an array of T is really a pointer to T.
5915e4b17023SJohn Marino One declared as a function is really a pointer to a function. */
5916e4b17023SJohn Marino
5917e4b17023SJohn Marino if (TREE_CODE (type) == ARRAY_TYPE)
5918e4b17023SJohn Marino {
5919e4b17023SJohn Marino /* Transfer const-ness of array into that of type pointed to. */
5920e4b17023SJohn Marino type = TREE_TYPE (type);
5921e4b17023SJohn Marino if (type_quals)
5922e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5923e4b17023SJohn Marino type = build_pointer_type (type);
5924e4b17023SJohn Marino type_quals = array_ptr_quals;
5925e4b17023SJohn Marino if (type_quals)
5926e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5927e4b17023SJohn Marino
5928e4b17023SJohn Marino /* We don't yet implement attributes in this context. */
5929e4b17023SJohn Marino if (array_ptr_attrs != NULL_TREE)
5930e4b17023SJohn Marino warning_at (loc, OPT_Wattributes,
5931e4b17023SJohn Marino "attributes in parameter array declarator ignored");
5932e4b17023SJohn Marino
5933e4b17023SJohn Marino size_varies = false;
5934e4b17023SJohn Marino }
5935e4b17023SJohn Marino else if (TREE_CODE (type) == FUNCTION_TYPE)
5936e4b17023SJohn Marino {
5937e4b17023SJohn Marino if (type_quals)
5938e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
5939e4b17023SJohn Marino "ISO C forbids qualified function types");
5940e4b17023SJohn Marino if (type_quals)
5941e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5942e4b17023SJohn Marino type = build_pointer_type (type);
5943e4b17023SJohn Marino type_quals = TYPE_UNQUALIFIED;
5944e4b17023SJohn Marino }
5945e4b17023SJohn Marino else if (type_quals)
5946e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5947e4b17023SJohn Marino
5948e4b17023SJohn Marino decl = build_decl (declarator->id_loc,
5949e4b17023SJohn Marino PARM_DECL, declarator->u.id, type);
5950e4b17023SJohn Marino if (size_varies)
5951e4b17023SJohn Marino C_DECL_VARIABLE_SIZE (decl) = 1;
5952e4b17023SJohn Marino
5953e4b17023SJohn Marino /* Compute the type actually passed in the parmlist,
5954e4b17023SJohn Marino for the case where there is no prototype.
5955e4b17023SJohn Marino (For example, shorts and chars are passed as ints.)
5956e4b17023SJohn Marino When there is a prototype, this is overridden later. */
5957e4b17023SJohn Marino
5958e4b17023SJohn Marino if (type == error_mark_node)
5959e4b17023SJohn Marino promoted_type = type;
5960e4b17023SJohn Marino else
5961e4b17023SJohn Marino promoted_type = c_type_promotes_to (type);
5962e4b17023SJohn Marino
5963e4b17023SJohn Marino DECL_ARG_TYPE (decl) = promoted_type;
5964e4b17023SJohn Marino if (declspecs->inline_p)
5965e4b17023SJohn Marino pedwarn (loc, 0, "parameter %q+D declared %<inline%>", decl);
5966e4b17023SJohn Marino if (declspecs->noreturn_p)
5967e4b17023SJohn Marino pedwarn (loc, 0, "parameter %q+D declared %<_Noreturn%>", decl);
5968e4b17023SJohn Marino }
5969e4b17023SJohn Marino else if (decl_context == FIELD)
5970e4b17023SJohn Marino {
5971e4b17023SJohn Marino /* Note that the grammar rejects storage classes in typenames
5972e4b17023SJohn Marino and fields. */
5973e4b17023SJohn Marino gcc_assert (storage_class == csc_none && !threadp
5974e4b17023SJohn Marino && !declspecs->inline_p && !declspecs->noreturn_p);
5975e4b17023SJohn Marino
5976e4b17023SJohn Marino /* Structure field. It may not be a function. */
5977e4b17023SJohn Marino
5978e4b17023SJohn Marino if (TREE_CODE (type) == FUNCTION_TYPE)
5979e4b17023SJohn Marino {
5980e4b17023SJohn Marino error_at (loc, "field %qE declared as a function", name);
5981e4b17023SJohn Marino type = build_pointer_type (type);
5982e4b17023SJohn Marino }
5983e4b17023SJohn Marino else if (TREE_CODE (type) != ERROR_MARK
5984e4b17023SJohn Marino && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
5985e4b17023SJohn Marino {
5986e4b17023SJohn Marino if (name)
5987e4b17023SJohn Marino error_at (loc, "field %qE has incomplete type", name);
5988e4b17023SJohn Marino else
5989e4b17023SJohn Marino error_at (loc, "unnamed field has incomplete type");
5990e4b17023SJohn Marino type = error_mark_node;
5991e4b17023SJohn Marino }
5992e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
5993e4b17023SJohn Marino decl = build_decl (declarator->id_loc,
5994e4b17023SJohn Marino FIELD_DECL, declarator->u.id, type);
5995e4b17023SJohn Marino DECL_NONADDRESSABLE_P (decl) = bitfield;
5996e4b17023SJohn Marino if (bitfield && !declarator->u.id)
5997e4b17023SJohn Marino TREE_NO_WARNING (decl) = 1;
5998e4b17023SJohn Marino
5999e4b17023SJohn Marino if (size_varies)
6000e4b17023SJohn Marino C_DECL_VARIABLE_SIZE (decl) = 1;
6001e4b17023SJohn Marino }
6002e4b17023SJohn Marino else if (TREE_CODE (type) == FUNCTION_TYPE)
6003e4b17023SJohn Marino {
6004e4b17023SJohn Marino if (storage_class == csc_register || threadp)
6005e4b17023SJohn Marino {
6006e4b17023SJohn Marino error_at (loc, "invalid storage class for function %qE", name);
6007e4b17023SJohn Marino }
6008e4b17023SJohn Marino else if (current_scope != file_scope)
6009e4b17023SJohn Marino {
6010e4b17023SJohn Marino /* Function declaration not at file scope. Storage
6011e4b17023SJohn Marino classes other than `extern' are not allowed, C99
6012e4b17023SJohn Marino 6.7.1p5, and `extern' makes no difference. However,
6013e4b17023SJohn Marino GCC allows 'auto', perhaps with 'inline', to support
6014e4b17023SJohn Marino nested functions. */
6015e4b17023SJohn Marino if (storage_class == csc_auto)
6016e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
6017e4b17023SJohn Marino "invalid storage class for function %qE", name);
6018e4b17023SJohn Marino else if (storage_class == csc_static)
6019e4b17023SJohn Marino {
6020e4b17023SJohn Marino error_at (loc, "invalid storage class for function %qE", name);
6021e4b17023SJohn Marino if (funcdef_flag)
6022e4b17023SJohn Marino storage_class = declspecs->storage_class = csc_none;
6023e4b17023SJohn Marino else
6024e4b17023SJohn Marino return 0;
6025e4b17023SJohn Marino }
6026e4b17023SJohn Marino }
6027e4b17023SJohn Marino
6028e4b17023SJohn Marino decl = build_decl (declarator->id_loc,
6029e4b17023SJohn Marino FUNCTION_DECL, declarator->u.id, type);
6030e4b17023SJohn Marino decl = build_decl_attribute_variant (decl, decl_attr);
6031e4b17023SJohn Marino
6032e4b17023SJohn Marino if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
6033e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
6034e4b17023SJohn Marino "ISO C forbids qualified function types");
6035e4b17023SJohn Marino
6036e4b17023SJohn Marino /* Every function declaration is an external reference
6037e4b17023SJohn Marino (DECL_EXTERNAL) except for those which are not at file
6038e4b17023SJohn Marino scope and are explicitly declared "auto". This is
6039e4b17023SJohn Marino forbidden by standard C (C99 6.7.1p5) and is interpreted by
6040e4b17023SJohn Marino GCC to signify a forward declaration of a nested function. */
6041e4b17023SJohn Marino if (storage_class == csc_auto && current_scope != file_scope)
6042e4b17023SJohn Marino DECL_EXTERNAL (decl) = 0;
6043e4b17023SJohn Marino /* In C99, a function which is declared 'inline' with 'extern'
6044e4b17023SJohn Marino is not an external reference (which is confusing). It
6045e4b17023SJohn Marino means that the later definition of the function must be output
6046e4b17023SJohn Marino in this file, C99 6.7.4p6. In GNU C89, a function declared
6047e4b17023SJohn Marino 'extern inline' is an external reference. */
6048e4b17023SJohn Marino else if (declspecs->inline_p && storage_class != csc_static)
6049e4b17023SJohn Marino DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
6050e4b17023SJohn Marino == flag_gnu89_inline);
6051e4b17023SJohn Marino else
6052e4b17023SJohn Marino DECL_EXTERNAL (decl) = !initialized;
6053e4b17023SJohn Marino
6054e4b17023SJohn Marino /* Record absence of global scope for `static' or `auto'. */
6055e4b17023SJohn Marino TREE_PUBLIC (decl)
6056e4b17023SJohn Marino = !(storage_class == csc_static || storage_class == csc_auto);
6057e4b17023SJohn Marino
6058e4b17023SJohn Marino /* For a function definition, record the argument information
6059e4b17023SJohn Marino block where store_parm_decls will look for it. */
6060e4b17023SJohn Marino if (funcdef_flag)
6061e4b17023SJohn Marino current_function_arg_info = arg_info;
6062e4b17023SJohn Marino
6063e4b17023SJohn Marino if (declspecs->default_int_p)
6064e4b17023SJohn Marino C_FUNCTION_IMPLICIT_INT (decl) = 1;
6065e4b17023SJohn Marino
6066e4b17023SJohn Marino /* Record presence of `inline' and `_Noreturn', if it is
6067e4b17023SJohn Marino reasonable. */
6068e4b17023SJohn Marino if (flag_hosted && MAIN_NAME_P (declarator->u.id))
6069e4b17023SJohn Marino {
6070e4b17023SJohn Marino if (declspecs->inline_p)
6071e4b17023SJohn Marino pedwarn (loc, 0, "cannot inline function %<main%>");
6072e4b17023SJohn Marino if (declspecs->noreturn_p)
6073e4b17023SJohn Marino pedwarn (loc, 0, "%<main%> declared %<_Noreturn%>");
6074e4b17023SJohn Marino }
6075e4b17023SJohn Marino else
6076e4b17023SJohn Marino {
6077e4b17023SJohn Marino if (declspecs->inline_p)
6078e4b17023SJohn Marino /* Record that the function is declared `inline'. */
6079e4b17023SJohn Marino DECL_DECLARED_INLINE_P (decl) = 1;
6080e4b17023SJohn Marino if (declspecs->noreturn_p)
6081e4b17023SJohn Marino {
6082e4b17023SJohn Marino if (!flag_isoc11)
6083e4b17023SJohn Marino {
6084e4b17023SJohn Marino if (flag_isoc99)
6085e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
6086e4b17023SJohn Marino "ISO C99 does not support %<_Noreturn%>");
6087e4b17023SJohn Marino else
6088e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
6089e4b17023SJohn Marino "ISO C90 does not support %<_Noreturn%>");
6090e4b17023SJohn Marino }
6091e4b17023SJohn Marino TREE_THIS_VOLATILE (decl) = 1;
6092e4b17023SJohn Marino }
6093e4b17023SJohn Marino }
6094e4b17023SJohn Marino }
6095e4b17023SJohn Marino else
6096e4b17023SJohn Marino {
6097e4b17023SJohn Marino /* It's a variable. */
6098e4b17023SJohn Marino /* An uninitialized decl with `extern' is a reference. */
6099e4b17023SJohn Marino int extern_ref = !initialized && storage_class == csc_extern;
6100e4b17023SJohn Marino
6101e4b17023SJohn Marino type = c_build_qualified_type (type, type_quals);
6102e4b17023SJohn Marino
6103e4b17023SJohn Marino /* C99 6.2.2p7: It is invalid (compile-time undefined
6104e4b17023SJohn Marino behavior) to create an 'extern' declaration for a
6105e4b17023SJohn Marino variable if there is a global declaration that is
6106e4b17023SJohn Marino 'static' and the global declaration is not visible.
6107e4b17023SJohn Marino (If the static declaration _is_ currently visible,
6108e4b17023SJohn Marino the 'extern' declaration is taken to refer to that decl.) */
6109e4b17023SJohn Marino if (extern_ref && current_scope != file_scope)
6110e4b17023SJohn Marino {
6111e4b17023SJohn Marino tree global_decl = identifier_global_value (declarator->u.id);
6112e4b17023SJohn Marino tree visible_decl = lookup_name (declarator->u.id);
6113e4b17023SJohn Marino
6114e4b17023SJohn Marino if (global_decl
6115e4b17023SJohn Marino && global_decl != visible_decl
6116e4b17023SJohn Marino && TREE_CODE (global_decl) == VAR_DECL
6117e4b17023SJohn Marino && !TREE_PUBLIC (global_decl))
6118e4b17023SJohn Marino error_at (loc, "variable previously declared %<static%> "
6119e4b17023SJohn Marino "redeclared %<extern%>");
6120e4b17023SJohn Marino }
6121e4b17023SJohn Marino
6122e4b17023SJohn Marino decl = build_decl (declarator->id_loc,
6123e4b17023SJohn Marino VAR_DECL, declarator->u.id, type);
6124e4b17023SJohn Marino if (size_varies)
6125e4b17023SJohn Marino C_DECL_VARIABLE_SIZE (decl) = 1;
6126e4b17023SJohn Marino
6127e4b17023SJohn Marino if (declspecs->inline_p)
6128e4b17023SJohn Marino pedwarn (loc, 0, "variable %q+D declared %<inline%>", decl);
6129e4b17023SJohn Marino if (declspecs->noreturn_p)
6130e4b17023SJohn Marino pedwarn (loc, 0, "variable %q+D declared %<_Noreturn%>", decl);
6131e4b17023SJohn Marino
6132e4b17023SJohn Marino /* At file scope, an initialized extern declaration may follow
6133e4b17023SJohn Marino a static declaration. In that case, DECL_EXTERNAL will be
6134e4b17023SJohn Marino reset later in start_decl. */
6135e4b17023SJohn Marino DECL_EXTERNAL (decl) = (storage_class == csc_extern);
6136e4b17023SJohn Marino
6137e4b17023SJohn Marino /* At file scope, the presence of a `static' or `register' storage
6138e4b17023SJohn Marino class specifier, or the absence of all storage class specifiers
6139e4b17023SJohn Marino makes this declaration a definition (perhaps tentative). Also,
6140e4b17023SJohn Marino the absence of `static' makes it public. */
6141e4b17023SJohn Marino if (current_scope == file_scope)
6142e4b17023SJohn Marino {
6143e4b17023SJohn Marino TREE_PUBLIC (decl) = storage_class != csc_static;
6144e4b17023SJohn Marino TREE_STATIC (decl) = !extern_ref;
6145e4b17023SJohn Marino }
6146e4b17023SJohn Marino /* Not at file scope, only `static' makes a static definition. */
6147e4b17023SJohn Marino else
6148e4b17023SJohn Marino {
6149e4b17023SJohn Marino TREE_STATIC (decl) = (storage_class == csc_static);
6150e4b17023SJohn Marino TREE_PUBLIC (decl) = extern_ref;
6151e4b17023SJohn Marino }
6152e4b17023SJohn Marino
6153e4b17023SJohn Marino if (threadp)
6154e4b17023SJohn Marino DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
6155e4b17023SJohn Marino }
6156e4b17023SJohn Marino
6157e4b17023SJohn Marino if ((storage_class == csc_extern
6158e4b17023SJohn Marino || (storage_class == csc_none
6159e4b17023SJohn Marino && TREE_CODE (type) == FUNCTION_TYPE
6160e4b17023SJohn Marino && !funcdef_flag))
6161e4b17023SJohn Marino && variably_modified_type_p (type, NULL_TREE))
6162e4b17023SJohn Marino {
6163e4b17023SJohn Marino /* C99 6.7.5.2p2 */
6164e4b17023SJohn Marino if (TREE_CODE (type) == FUNCTION_TYPE)
6165e4b17023SJohn Marino error_at (loc, "non-nested function with variably modified type");
6166e4b17023SJohn Marino else
6167e4b17023SJohn Marino error_at (loc, "object with variably modified type must have "
6168e4b17023SJohn Marino "no linkage");
6169e4b17023SJohn Marino }
6170e4b17023SJohn Marino
6171e4b17023SJohn Marino /* Record `register' declaration for warnings on &
6172e4b17023SJohn Marino and in case doing stupid register allocation. */
6173e4b17023SJohn Marino
6174e4b17023SJohn Marino if (storage_class == csc_register)
6175e4b17023SJohn Marino {
6176e4b17023SJohn Marino C_DECL_REGISTER (decl) = 1;
6177e4b17023SJohn Marino DECL_REGISTER (decl) = 1;
6178e4b17023SJohn Marino }
6179e4b17023SJohn Marino
6180e4b17023SJohn Marino /* Record constancy and volatility. */
6181e4b17023SJohn Marino c_apply_type_quals_to_decl (type_quals, decl);
6182e4b17023SJohn Marino
6183e4b17023SJohn Marino /* Apply _Alignas specifiers. */
6184e4b17023SJohn Marino if (alignas_align)
6185e4b17023SJohn Marino {
6186e4b17023SJohn Marino DECL_ALIGN (decl) = alignas_align * BITS_PER_UNIT;
6187e4b17023SJohn Marino DECL_USER_ALIGN (decl) = 1;
6188e4b17023SJohn Marino }
6189e4b17023SJohn Marino
6190e4b17023SJohn Marino /* If a type has volatile components, it should be stored in memory.
6191e4b17023SJohn Marino Otherwise, the fact that those components are volatile
6192e4b17023SJohn Marino will be ignored, and would even crash the compiler.
6193e4b17023SJohn Marino Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
6194e4b17023SJohn Marino if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
6195e4b17023SJohn Marino && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
6196e4b17023SJohn Marino || TREE_CODE (decl) == RESULT_DECL))
6197e4b17023SJohn Marino {
6198e4b17023SJohn Marino /* It is not an error for a structure with volatile fields to
6199e4b17023SJohn Marino be declared register, but reset DECL_REGISTER since it
6200e4b17023SJohn Marino cannot actually go in a register. */
6201e4b17023SJohn Marino int was_reg = C_DECL_REGISTER (decl);
6202e4b17023SJohn Marino C_DECL_REGISTER (decl) = 0;
6203e4b17023SJohn Marino DECL_REGISTER (decl) = 0;
6204e4b17023SJohn Marino c_mark_addressable (decl);
6205e4b17023SJohn Marino C_DECL_REGISTER (decl) = was_reg;
6206e4b17023SJohn Marino }
6207e4b17023SJohn Marino
6208e4b17023SJohn Marino /* This is the earliest point at which we might know the assembler
6209e4b17023SJohn Marino name of a variable. Thus, if it's known before this, die horribly. */
6210e4b17023SJohn Marino gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
6211e4b17023SJohn Marino
6212e4b17023SJohn Marino if (warn_cxx_compat
6213e4b17023SJohn Marino && TREE_CODE (decl) == VAR_DECL
6214e4b17023SJohn Marino && TREE_PUBLIC (decl)
6215e4b17023SJohn Marino && TREE_STATIC (decl)
6216e4b17023SJohn Marino && (TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6217e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6218e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
6219e4b17023SJohn Marino && TYPE_NAME (TREE_TYPE (decl)) == NULL_TREE)
6220e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
6221e4b17023SJohn Marino ("non-local variable %qD with anonymous type is "
6222e4b17023SJohn Marino "questionable in C++"),
6223e4b17023SJohn Marino decl);
6224e4b17023SJohn Marino
6225e4b17023SJohn Marino return decl;
6226e4b17023SJohn Marino }
6227e4b17023SJohn Marino }
6228e4b17023SJohn Marino
6229e4b17023SJohn Marino /* Decode the parameter-list info for a function type or function definition.
6230e4b17023SJohn Marino The argument is the value returned by `get_parm_info' (or made in c-parse.c
6231e4b17023SJohn Marino if there is an identifier list instead of a parameter decl list).
6232e4b17023SJohn Marino These two functions are separate because when a function returns
6233e4b17023SJohn Marino or receives functions then each is called multiple times but the order
6234e4b17023SJohn Marino of calls is different. The last call to `grokparms' is always the one
6235e4b17023SJohn Marino that contains the formal parameter names of a function definition.
6236e4b17023SJohn Marino
6237e4b17023SJohn Marino Return a list of arg types to use in the FUNCTION_TYPE for this function.
6238e4b17023SJohn Marino
6239e4b17023SJohn Marino FUNCDEF_FLAG is true for a function definition, false for
6240e4b17023SJohn Marino a mere declaration. A nonempty identifier-list gets an error message
6241e4b17023SJohn Marino when FUNCDEF_FLAG is false. */
6242e4b17023SJohn Marino
6243e4b17023SJohn Marino static tree
grokparms(struct c_arg_info * arg_info,bool funcdef_flag)6244e4b17023SJohn Marino grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
6245e4b17023SJohn Marino {
6246e4b17023SJohn Marino tree arg_types = arg_info->types;
6247e4b17023SJohn Marino
6248e4b17023SJohn Marino if (funcdef_flag && arg_info->had_vla_unspec)
6249e4b17023SJohn Marino {
6250e4b17023SJohn Marino /* A function definition isn't function prototype scope C99 6.2.1p4. */
6251e4b17023SJohn Marino /* C99 6.7.5.2p4 */
6252e4b17023SJohn Marino error ("%<[*]%> not allowed in other than function prototype scope");
6253e4b17023SJohn Marino }
6254e4b17023SJohn Marino
6255e4b17023SJohn Marino if (arg_types == 0 && !funcdef_flag && !in_system_header)
6256e4b17023SJohn Marino warning (OPT_Wstrict_prototypes,
6257e4b17023SJohn Marino "function declaration isn%'t a prototype");
6258e4b17023SJohn Marino
6259e4b17023SJohn Marino if (arg_types == error_mark_node)
6260e4b17023SJohn Marino return 0; /* don't set TYPE_ARG_TYPES in this case */
6261e4b17023SJohn Marino
6262e4b17023SJohn Marino else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
6263e4b17023SJohn Marino {
6264e4b17023SJohn Marino if (!funcdef_flag)
6265e4b17023SJohn Marino {
6266e4b17023SJohn Marino pedwarn (input_location, 0, "parameter names (without types) in function declaration");
6267e4b17023SJohn Marino arg_info->parms = NULL_TREE;
6268e4b17023SJohn Marino }
6269e4b17023SJohn Marino else
6270e4b17023SJohn Marino arg_info->parms = arg_info->types;
6271e4b17023SJohn Marino
6272e4b17023SJohn Marino arg_info->types = 0;
6273e4b17023SJohn Marino return 0;
6274e4b17023SJohn Marino }
6275e4b17023SJohn Marino else
6276e4b17023SJohn Marino {
6277e4b17023SJohn Marino tree parm, type, typelt;
6278e4b17023SJohn Marino unsigned int parmno;
6279e4b17023SJohn Marino const char *errmsg;
6280e4b17023SJohn Marino
6281e4b17023SJohn Marino /* If there is a parameter of incomplete type in a definition,
6282e4b17023SJohn Marino this is an error. In a declaration this is valid, and a
6283e4b17023SJohn Marino struct or union type may be completed later, before any calls
6284e4b17023SJohn Marino or definition of the function. In the case where the tag was
6285e4b17023SJohn Marino first declared within the parameter list, a warning has
6286e4b17023SJohn Marino already been given. If a parameter has void type, then
6287e4b17023SJohn Marino however the function cannot be defined or called, so
6288e4b17023SJohn Marino warn. */
6289e4b17023SJohn Marino
6290e4b17023SJohn Marino for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
6291e4b17023SJohn Marino parm;
6292e4b17023SJohn Marino parm = DECL_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
6293e4b17023SJohn Marino {
6294e4b17023SJohn Marino type = TREE_VALUE (typelt);
6295e4b17023SJohn Marino if (type == error_mark_node)
6296e4b17023SJohn Marino continue;
6297e4b17023SJohn Marino
6298e4b17023SJohn Marino if (!COMPLETE_TYPE_P (type))
6299e4b17023SJohn Marino {
6300e4b17023SJohn Marino if (funcdef_flag)
6301e4b17023SJohn Marino {
6302e4b17023SJohn Marino if (DECL_NAME (parm))
6303e4b17023SJohn Marino error_at (input_location,
6304e4b17023SJohn Marino "parameter %u (%q+D) has incomplete type",
6305e4b17023SJohn Marino parmno, parm);
6306e4b17023SJohn Marino else
6307e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (parm),
6308e4b17023SJohn Marino "parameter %u has incomplete type",
6309e4b17023SJohn Marino parmno);
6310e4b17023SJohn Marino
6311e4b17023SJohn Marino TREE_VALUE (typelt) = error_mark_node;
6312e4b17023SJohn Marino TREE_TYPE (parm) = error_mark_node;
6313e4b17023SJohn Marino arg_types = NULL_TREE;
6314e4b17023SJohn Marino }
6315e4b17023SJohn Marino else if (VOID_TYPE_P (type))
6316e4b17023SJohn Marino {
6317e4b17023SJohn Marino if (DECL_NAME (parm))
6318e4b17023SJohn Marino warning_at (input_location, 0,
6319e4b17023SJohn Marino "parameter %u (%q+D) has void type",
6320e4b17023SJohn Marino parmno, parm);
6321e4b17023SJohn Marino else
6322e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (parm), 0,
6323e4b17023SJohn Marino "parameter %u has void type",
6324e4b17023SJohn Marino parmno);
6325e4b17023SJohn Marino }
6326e4b17023SJohn Marino }
6327e4b17023SJohn Marino
6328e4b17023SJohn Marino errmsg = targetm.invalid_parameter_type (type);
6329e4b17023SJohn Marino if (errmsg)
6330e4b17023SJohn Marino {
6331e4b17023SJohn Marino error (errmsg);
6332e4b17023SJohn Marino TREE_VALUE (typelt) = error_mark_node;
6333e4b17023SJohn Marino TREE_TYPE (parm) = error_mark_node;
6334e4b17023SJohn Marino arg_types = NULL_TREE;
6335e4b17023SJohn Marino }
6336e4b17023SJohn Marino
6337e4b17023SJohn Marino if (DECL_NAME (parm) && TREE_USED (parm))
6338e4b17023SJohn Marino warn_if_shadowing (parm);
6339e4b17023SJohn Marino }
6340e4b17023SJohn Marino return arg_types;
6341e4b17023SJohn Marino }
6342e4b17023SJohn Marino }
6343e4b17023SJohn Marino
6344e4b17023SJohn Marino /* Allocate and initialize a c_arg_info structure from the parser's
6345e4b17023SJohn Marino obstack. */
6346e4b17023SJohn Marino
6347e4b17023SJohn Marino struct c_arg_info *
build_arg_info(void)6348e4b17023SJohn Marino build_arg_info (void)
6349e4b17023SJohn Marino {
6350e4b17023SJohn Marino struct c_arg_info *ret = XOBNEW (&parser_obstack, struct c_arg_info);
6351e4b17023SJohn Marino ret->parms = NULL_TREE;
6352e4b17023SJohn Marino ret->tags = NULL;
6353e4b17023SJohn Marino ret->types = NULL_TREE;
6354e4b17023SJohn Marino ret->others = NULL_TREE;
6355e4b17023SJohn Marino ret->pending_sizes = NULL;
6356e4b17023SJohn Marino ret->had_vla_unspec = 0;
6357e4b17023SJohn Marino return ret;
6358e4b17023SJohn Marino }
6359e4b17023SJohn Marino
6360e4b17023SJohn Marino /* Take apart the current scope and return a c_arg_info structure with
6361e4b17023SJohn Marino info on a parameter list just parsed.
6362e4b17023SJohn Marino
6363e4b17023SJohn Marino This structure is later fed to 'grokparms' and 'store_parm_decls'.
6364e4b17023SJohn Marino
6365e4b17023SJohn Marino ELLIPSIS being true means the argument list ended in '...' so don't
6366e4b17023SJohn Marino append a sentinel (void_list_node) to the end of the type-list.
6367e4b17023SJohn Marino
6368e4b17023SJohn Marino EXPR is NULL or an expression that needs to be evaluated for the
6369e4b17023SJohn Marino side effects of array size expressions in the parameters. */
6370e4b17023SJohn Marino
6371e4b17023SJohn Marino struct c_arg_info *
get_parm_info(bool ellipsis,tree expr)6372e4b17023SJohn Marino get_parm_info (bool ellipsis, tree expr)
6373e4b17023SJohn Marino {
6374e4b17023SJohn Marino struct c_binding *b = current_scope->bindings;
6375e4b17023SJohn Marino struct c_arg_info *arg_info = build_arg_info ();
6376e4b17023SJohn Marino
6377e4b17023SJohn Marino tree parms = 0;
6378e4b17023SJohn Marino VEC(c_arg_tag,gc) *tags = NULL;
6379e4b17023SJohn Marino tree types = 0;
6380e4b17023SJohn Marino tree others = 0;
6381e4b17023SJohn Marino
6382e4b17023SJohn Marino static bool explained_incomplete_types = false;
6383e4b17023SJohn Marino bool gave_void_only_once_err = false;
6384e4b17023SJohn Marino
6385e4b17023SJohn Marino arg_info->had_vla_unspec = current_scope->had_vla_unspec;
6386e4b17023SJohn Marino
6387e4b17023SJohn Marino /* The bindings in this scope must not get put into a block.
6388e4b17023SJohn Marino We will take care of deleting the binding nodes. */
6389e4b17023SJohn Marino current_scope->bindings = 0;
6390e4b17023SJohn Marino
6391e4b17023SJohn Marino /* This function is only called if there was *something* on the
6392e4b17023SJohn Marino parameter list. */
6393e4b17023SJohn Marino gcc_assert (b);
6394e4b17023SJohn Marino
6395e4b17023SJohn Marino /* A parameter list consisting solely of 'void' indicates that the
6396e4b17023SJohn Marino function takes no arguments. But if the 'void' is qualified
6397e4b17023SJohn Marino (by 'const' or 'volatile'), or has a storage class specifier
6398e4b17023SJohn Marino ('register'), then the behavior is undefined; issue an error.
6399e4b17023SJohn Marino Typedefs for 'void' are OK (see DR#157). */
6400e4b17023SJohn Marino if (b->prev == 0 /* one binding */
6401e4b17023SJohn Marino && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
6402e4b17023SJohn Marino && !DECL_NAME (b->decl) /* anonymous */
6403e4b17023SJohn Marino && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
6404e4b17023SJohn Marino {
6405e4b17023SJohn Marino if (TREE_THIS_VOLATILE (b->decl)
6406e4b17023SJohn Marino || TREE_READONLY (b->decl)
6407e4b17023SJohn Marino || C_DECL_REGISTER (b->decl))
6408e4b17023SJohn Marino error ("%<void%> as only parameter may not be qualified");
6409e4b17023SJohn Marino
6410e4b17023SJohn Marino /* There cannot be an ellipsis. */
6411e4b17023SJohn Marino if (ellipsis)
6412e4b17023SJohn Marino error ("%<void%> must be the only parameter");
6413e4b17023SJohn Marino
6414e4b17023SJohn Marino arg_info->types = void_list_node;
6415e4b17023SJohn Marino return arg_info;
6416e4b17023SJohn Marino }
6417e4b17023SJohn Marino
6418e4b17023SJohn Marino if (!ellipsis)
6419e4b17023SJohn Marino types = void_list_node;
6420e4b17023SJohn Marino
6421e4b17023SJohn Marino /* Break up the bindings list into parms, tags, types, and others;
6422e4b17023SJohn Marino apply sanity checks; purge the name-to-decl bindings. */
6423e4b17023SJohn Marino while (b)
6424e4b17023SJohn Marino {
6425e4b17023SJohn Marino tree decl = b->decl;
6426e4b17023SJohn Marino tree type = TREE_TYPE (decl);
6427e4b17023SJohn Marino c_arg_tag *tag;
6428e4b17023SJohn Marino const char *keyword;
6429e4b17023SJohn Marino
6430e4b17023SJohn Marino switch (TREE_CODE (decl))
6431e4b17023SJohn Marino {
6432e4b17023SJohn Marino case PARM_DECL:
6433e4b17023SJohn Marino if (b->id)
6434e4b17023SJohn Marino {
6435e4b17023SJohn Marino gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6436e4b17023SJohn Marino I_SYMBOL_BINDING (b->id) = b->shadowed;
6437e4b17023SJohn Marino }
6438e4b17023SJohn Marino
6439e4b17023SJohn Marino /* Check for forward decls that never got their actual decl. */
6440e4b17023SJohn Marino if (TREE_ASM_WRITTEN (decl))
6441e4b17023SJohn Marino error ("parameter %q+D has just a forward declaration", decl);
6442e4b17023SJohn Marino /* Check for (..., void, ...) and issue an error. */
6443e4b17023SJohn Marino else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
6444e4b17023SJohn Marino {
6445e4b17023SJohn Marino if (!gave_void_only_once_err)
6446e4b17023SJohn Marino {
6447e4b17023SJohn Marino error ("%<void%> must be the only parameter");
6448e4b17023SJohn Marino gave_void_only_once_err = true;
6449e4b17023SJohn Marino }
6450e4b17023SJohn Marino }
6451e4b17023SJohn Marino else
6452e4b17023SJohn Marino {
6453e4b17023SJohn Marino /* Valid parameter, add it to the list. */
6454e4b17023SJohn Marino DECL_CHAIN (decl) = parms;
6455e4b17023SJohn Marino parms = decl;
6456e4b17023SJohn Marino
6457e4b17023SJohn Marino /* Since there is a prototype, args are passed in their
6458e4b17023SJohn Marino declared types. The back end may override this later. */
6459e4b17023SJohn Marino DECL_ARG_TYPE (decl) = type;
6460e4b17023SJohn Marino types = tree_cons (0, type, types);
6461e4b17023SJohn Marino }
6462e4b17023SJohn Marino break;
6463e4b17023SJohn Marino
6464e4b17023SJohn Marino case ENUMERAL_TYPE: keyword = "enum"; goto tag;
6465e4b17023SJohn Marino case UNION_TYPE: keyword = "union"; goto tag;
6466e4b17023SJohn Marino case RECORD_TYPE: keyword = "struct"; goto tag;
6467e4b17023SJohn Marino tag:
6468e4b17023SJohn Marino /* Types may not have tag-names, in which case the type
6469e4b17023SJohn Marino appears in the bindings list with b->id NULL. */
6470e4b17023SJohn Marino if (b->id)
6471e4b17023SJohn Marino {
6472e4b17023SJohn Marino gcc_assert (I_TAG_BINDING (b->id) == b);
6473e4b17023SJohn Marino I_TAG_BINDING (b->id) = b->shadowed;
6474e4b17023SJohn Marino }
6475e4b17023SJohn Marino
6476e4b17023SJohn Marino /* Warn about any struct, union or enum tags defined in a
6477e4b17023SJohn Marino parameter list. The scope of such types is limited to
6478e4b17023SJohn Marino the parameter list, which is rarely if ever desirable
6479e4b17023SJohn Marino (it's impossible to call such a function with type-
6480e4b17023SJohn Marino correct arguments). An anonymous union parm type is
6481e4b17023SJohn Marino meaningful as a GNU extension, so don't warn for that. */
6482e4b17023SJohn Marino if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
6483e4b17023SJohn Marino {
6484e4b17023SJohn Marino if (b->id)
6485e4b17023SJohn Marino /* The %s will be one of 'struct', 'union', or 'enum'. */
6486e4b17023SJohn Marino warning (0, "%<%s %E%> declared inside parameter list",
6487e4b17023SJohn Marino keyword, b->id);
6488e4b17023SJohn Marino else
6489e4b17023SJohn Marino /* The %s will be one of 'struct', 'union', or 'enum'. */
6490e4b17023SJohn Marino warning (0, "anonymous %s declared inside parameter list",
6491e4b17023SJohn Marino keyword);
6492e4b17023SJohn Marino
6493e4b17023SJohn Marino if (!explained_incomplete_types)
6494e4b17023SJohn Marino {
6495e4b17023SJohn Marino warning (0, "its scope is only this definition or declaration,"
6496e4b17023SJohn Marino " which is probably not what you want");
6497e4b17023SJohn Marino explained_incomplete_types = true;
6498e4b17023SJohn Marino }
6499e4b17023SJohn Marino }
6500e4b17023SJohn Marino
6501e4b17023SJohn Marino tag = VEC_safe_push (c_arg_tag, gc, tags, NULL);
6502e4b17023SJohn Marino tag->id = b->id;
6503e4b17023SJohn Marino tag->type = decl;
6504e4b17023SJohn Marino break;
6505e4b17023SJohn Marino
6506e4b17023SJohn Marino case CONST_DECL:
6507e4b17023SJohn Marino case TYPE_DECL:
6508e4b17023SJohn Marino case FUNCTION_DECL:
6509e4b17023SJohn Marino /* CONST_DECLs appear here when we have an embedded enum,
6510e4b17023SJohn Marino and TYPE_DECLs appear here when we have an embedded struct
6511e4b17023SJohn Marino or union. No warnings for this - we already warned about the
6512e4b17023SJohn Marino type itself. FUNCTION_DECLs appear when there is an implicit
6513e4b17023SJohn Marino function declaration in the parameter list. */
6514e4b17023SJohn Marino
6515e4b17023SJohn Marino /* When we reinsert this decl in the function body, we need
6516e4b17023SJohn Marino to reconstruct whether it was marked as nested. */
6517e4b17023SJohn Marino gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
6518e4b17023SJohn Marino ? b->nested
6519e4b17023SJohn Marino : !b->nested);
6520e4b17023SJohn Marino DECL_CHAIN (decl) = others;
6521e4b17023SJohn Marino others = decl;
6522e4b17023SJohn Marino /* fall through */
6523e4b17023SJohn Marino
6524e4b17023SJohn Marino case ERROR_MARK:
6525e4b17023SJohn Marino /* error_mark_node appears here when we have an undeclared
6526e4b17023SJohn Marino variable. Just throw it away. */
6527e4b17023SJohn Marino if (b->id)
6528e4b17023SJohn Marino {
6529e4b17023SJohn Marino gcc_assert (I_SYMBOL_BINDING (b->id) == b);
6530e4b17023SJohn Marino I_SYMBOL_BINDING (b->id) = b->shadowed;
6531e4b17023SJohn Marino }
6532e4b17023SJohn Marino break;
6533e4b17023SJohn Marino
6534e4b17023SJohn Marino /* Other things that might be encountered. */
6535e4b17023SJohn Marino case LABEL_DECL:
6536e4b17023SJohn Marino case VAR_DECL:
6537e4b17023SJohn Marino default:
6538e4b17023SJohn Marino gcc_unreachable ();
6539e4b17023SJohn Marino }
6540e4b17023SJohn Marino
6541e4b17023SJohn Marino b = free_binding_and_advance (b);
6542e4b17023SJohn Marino }
6543e4b17023SJohn Marino
6544e4b17023SJohn Marino arg_info->parms = parms;
6545e4b17023SJohn Marino arg_info->tags = tags;
6546e4b17023SJohn Marino arg_info->types = types;
6547e4b17023SJohn Marino arg_info->others = others;
6548e4b17023SJohn Marino arg_info->pending_sizes = expr;
6549e4b17023SJohn Marino return arg_info;
6550e4b17023SJohn Marino }
6551e4b17023SJohn Marino
6552e4b17023SJohn Marino /* Get the struct, enum or union (CODE says which) with tag NAME.
6553e4b17023SJohn Marino Define the tag as a forward-reference with location LOC if it is
6554e4b17023SJohn Marino not defined. Return a c_typespec structure for the type
6555e4b17023SJohn Marino specifier. */
6556e4b17023SJohn Marino
6557e4b17023SJohn Marino struct c_typespec
parser_xref_tag(location_t loc,enum tree_code code,tree name)6558e4b17023SJohn Marino parser_xref_tag (location_t loc, enum tree_code code, tree name)
6559e4b17023SJohn Marino {
6560e4b17023SJohn Marino struct c_typespec ret;
6561e4b17023SJohn Marino tree ref;
6562e4b17023SJohn Marino location_t refloc;
6563e4b17023SJohn Marino
6564e4b17023SJohn Marino ret.expr = NULL_TREE;
6565e4b17023SJohn Marino ret.expr_const_operands = true;
6566e4b17023SJohn Marino
6567e4b17023SJohn Marino /* If a cross reference is requested, look up the type
6568e4b17023SJohn Marino already defined for this tag and return it. */
6569e4b17023SJohn Marino
6570e4b17023SJohn Marino ref = lookup_tag (code, name, 0, &refloc);
6571e4b17023SJohn Marino /* If this is the right type of tag, return what we found.
6572e4b17023SJohn Marino (This reference will be shadowed by shadow_tag later if appropriate.)
6573e4b17023SJohn Marino If this is the wrong type of tag, do not return it. If it was the
6574e4b17023SJohn Marino wrong type in the same scope, we will have had an error
6575e4b17023SJohn Marino message already; if in a different scope and declaring
6576e4b17023SJohn Marino a name, pending_xref_error will give an error message; but if in a
6577e4b17023SJohn Marino different scope and not declaring a name, this tag should
6578e4b17023SJohn Marino shadow the previous declaration of a different type of tag, and
6579e4b17023SJohn Marino this would not work properly if we return the reference found.
6580e4b17023SJohn Marino (For example, with "struct foo" in an outer scope, "union foo;"
6581e4b17023SJohn Marino must shadow that tag with a new one of union type.) */
6582e4b17023SJohn Marino ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
6583e4b17023SJohn Marino if (ref && TREE_CODE (ref) == code)
6584e4b17023SJohn Marino {
6585e4b17023SJohn Marino if (C_TYPE_DEFINED_IN_STRUCT (ref)
6586e4b17023SJohn Marino && loc != UNKNOWN_LOCATION
6587e4b17023SJohn Marino && warn_cxx_compat)
6588e4b17023SJohn Marino {
6589e4b17023SJohn Marino switch (code)
6590e4b17023SJohn Marino {
6591e4b17023SJohn Marino case ENUMERAL_TYPE:
6592e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
6593e4b17023SJohn Marino ("enum type defined in struct or union "
6594e4b17023SJohn Marino "is not visible in C++"));
6595e4b17023SJohn Marino inform (refloc, "enum type defined here");
6596e4b17023SJohn Marino break;
6597e4b17023SJohn Marino case RECORD_TYPE:
6598e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
6599e4b17023SJohn Marino ("struct defined in struct or union "
6600e4b17023SJohn Marino "is not visible in C++"));
6601e4b17023SJohn Marino inform (refloc, "struct defined here");
6602e4b17023SJohn Marino break;
6603e4b17023SJohn Marino case UNION_TYPE:
6604e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
6605e4b17023SJohn Marino ("union defined in struct or union "
6606e4b17023SJohn Marino "is not visible in C++"));
6607e4b17023SJohn Marino inform (refloc, "union defined here");
6608e4b17023SJohn Marino break;
6609e4b17023SJohn Marino default:
6610e4b17023SJohn Marino gcc_unreachable();
6611e4b17023SJohn Marino }
6612e4b17023SJohn Marino }
6613e4b17023SJohn Marino
6614e4b17023SJohn Marino ret.spec = ref;
6615e4b17023SJohn Marino return ret;
6616e4b17023SJohn Marino }
6617e4b17023SJohn Marino
6618e4b17023SJohn Marino /* If no such tag is yet defined, create a forward-reference node
6619e4b17023SJohn Marino and record it as the "definition".
6620e4b17023SJohn Marino When a real declaration of this type is found,
6621e4b17023SJohn Marino the forward-reference will be altered into a real type. */
6622e4b17023SJohn Marino
6623e4b17023SJohn Marino ref = make_node (code);
6624e4b17023SJohn Marino if (code == ENUMERAL_TYPE)
6625e4b17023SJohn Marino {
6626e4b17023SJohn Marino /* Give the type a default layout like unsigned int
6627e4b17023SJohn Marino to avoid crashing if it does not get defined. */
6628e4b17023SJohn Marino SET_TYPE_MODE (ref, TYPE_MODE (unsigned_type_node));
6629e4b17023SJohn Marino TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
6630e4b17023SJohn Marino TYPE_USER_ALIGN (ref) = 0;
6631e4b17023SJohn Marino TYPE_UNSIGNED (ref) = 1;
6632e4b17023SJohn Marino TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
6633e4b17023SJohn Marino TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
6634e4b17023SJohn Marino TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
6635e4b17023SJohn Marino }
6636e4b17023SJohn Marino
6637e4b17023SJohn Marino pushtag (loc, name, ref);
6638e4b17023SJohn Marino
6639e4b17023SJohn Marino ret.spec = ref;
6640e4b17023SJohn Marino return ret;
6641e4b17023SJohn Marino }
6642e4b17023SJohn Marino
6643e4b17023SJohn Marino /* Get the struct, enum or union (CODE says which) with tag NAME.
6644e4b17023SJohn Marino Define the tag as a forward-reference if it is not defined.
6645e4b17023SJohn Marino Return a tree for the type. */
6646e4b17023SJohn Marino
6647e4b17023SJohn Marino tree
xref_tag(enum tree_code code,tree name)6648e4b17023SJohn Marino xref_tag (enum tree_code code, tree name)
6649e4b17023SJohn Marino {
6650e4b17023SJohn Marino return parser_xref_tag (input_location, code, name).spec;
6651e4b17023SJohn Marino }
6652e4b17023SJohn Marino
6653e4b17023SJohn Marino /* Make sure that the tag NAME is defined *in the current scope*
6654e4b17023SJohn Marino at least as a forward reference.
6655e4b17023SJohn Marino LOC is the location of the struct's definition.
6656e4b17023SJohn Marino CODE says which kind of tag NAME ought to be.
6657e4b17023SJohn Marino
6658e4b17023SJohn Marino This stores the current value of the file static STRUCT_PARSE_INFO
6659e4b17023SJohn Marino in *ENCLOSING_STRUCT_PARSE_INFO, and points STRUCT_PARSE_INFO at a
6660e4b17023SJohn Marino new c_struct_parse_info structure. The old value of
6661e4b17023SJohn Marino STRUCT_PARSE_INFO is restored in finish_struct. */
6662e4b17023SJohn Marino
6663e4b17023SJohn Marino tree
start_struct(location_t loc,enum tree_code code,tree name,struct c_struct_parse_info ** enclosing_struct_parse_info)6664e4b17023SJohn Marino start_struct (location_t loc, enum tree_code code, tree name,
6665e4b17023SJohn Marino struct c_struct_parse_info **enclosing_struct_parse_info)
6666e4b17023SJohn Marino {
6667e4b17023SJohn Marino /* If there is already a tag defined at this scope
6668e4b17023SJohn Marino (as a forward reference), just return it. */
6669e4b17023SJohn Marino
6670e4b17023SJohn Marino tree ref = NULL_TREE;
6671e4b17023SJohn Marino location_t refloc = UNKNOWN_LOCATION;
6672e4b17023SJohn Marino
6673e4b17023SJohn Marino if (name != NULL_TREE)
6674e4b17023SJohn Marino ref = lookup_tag (code, name, 1, &refloc);
6675e4b17023SJohn Marino if (ref && TREE_CODE (ref) == code)
6676e4b17023SJohn Marino {
6677e4b17023SJohn Marino if (TYPE_SIZE (ref))
6678e4b17023SJohn Marino {
6679e4b17023SJohn Marino if (code == UNION_TYPE)
6680e4b17023SJohn Marino error_at (loc, "redefinition of %<union %E%>", name);
6681e4b17023SJohn Marino else
6682e4b17023SJohn Marino error_at (loc, "redefinition of %<struct %E%>", name);
6683e4b17023SJohn Marino if (refloc != UNKNOWN_LOCATION)
6684e4b17023SJohn Marino inform (refloc, "originally defined here");
6685e4b17023SJohn Marino /* Don't create structures using a name already in use. */
6686e4b17023SJohn Marino ref = NULL_TREE;
6687e4b17023SJohn Marino }
6688e4b17023SJohn Marino else if (C_TYPE_BEING_DEFINED (ref))
6689e4b17023SJohn Marino {
6690e4b17023SJohn Marino if (code == UNION_TYPE)
6691e4b17023SJohn Marino error_at (loc, "nested redefinition of %<union %E%>", name);
6692e4b17023SJohn Marino else
6693e4b17023SJohn Marino error_at (loc, "nested redefinition of %<struct %E%>", name);
6694e4b17023SJohn Marino /* Don't bother to report "originally defined here" for a
6695e4b17023SJohn Marino nested redefinition; the original definition should be
6696e4b17023SJohn Marino obvious. */
6697e4b17023SJohn Marino /* Don't create structures that contain themselves. */
6698e4b17023SJohn Marino ref = NULL_TREE;
6699e4b17023SJohn Marino }
6700e4b17023SJohn Marino }
6701e4b17023SJohn Marino
6702e4b17023SJohn Marino /* Otherwise create a forward-reference just so the tag is in scope. */
6703e4b17023SJohn Marino
6704e4b17023SJohn Marino if (ref == NULL_TREE || TREE_CODE (ref) != code)
6705e4b17023SJohn Marino {
6706e4b17023SJohn Marino ref = make_node (code);
6707e4b17023SJohn Marino pushtag (loc, name, ref);
6708e4b17023SJohn Marino }
6709e4b17023SJohn Marino
6710e4b17023SJohn Marino C_TYPE_BEING_DEFINED (ref) = 1;
6711e4b17023SJohn Marino TYPE_PACKED (ref) = flag_pack_struct;
6712e4b17023SJohn Marino
6713e4b17023SJohn Marino *enclosing_struct_parse_info = struct_parse_info;
6714e4b17023SJohn Marino struct_parse_info = XNEW (struct c_struct_parse_info);
6715e4b17023SJohn Marino struct_parse_info->struct_types = VEC_alloc (tree, heap, 0);
6716e4b17023SJohn Marino struct_parse_info->fields = VEC_alloc (c_binding_ptr, heap, 0);
6717e4b17023SJohn Marino struct_parse_info->typedefs_seen = VEC_alloc (tree, heap, 0);
6718e4b17023SJohn Marino
6719e4b17023SJohn Marino /* FIXME: This will issue a warning for a use of a type defined
6720e4b17023SJohn Marino within a statement expr used within sizeof, et. al. This is not
6721e4b17023SJohn Marino terribly serious as C++ doesn't permit statement exprs within
6722e4b17023SJohn Marino sizeof anyhow. */
6723e4b17023SJohn Marino if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
6724e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
6725e4b17023SJohn Marino "defining type in %qs expression is invalid in C++",
6726e4b17023SJohn Marino (in_sizeof
6727e4b17023SJohn Marino ? "sizeof"
6728e4b17023SJohn Marino : (in_typeof ? "typeof" : "alignof")));
6729e4b17023SJohn Marino
6730e4b17023SJohn Marino return ref;
6731e4b17023SJohn Marino }
6732e4b17023SJohn Marino
6733e4b17023SJohn Marino /* Process the specs, declarator and width (NULL if omitted)
6734e4b17023SJohn Marino of a structure component, returning a FIELD_DECL node.
6735e4b17023SJohn Marino WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
6736e4b17023SJohn Marino DECL_ATTRS is as for grokdeclarator.
6737e4b17023SJohn Marino
6738e4b17023SJohn Marino LOC is the location of the structure component.
6739e4b17023SJohn Marino
6740e4b17023SJohn Marino This is done during the parsing of the struct declaration.
6741e4b17023SJohn Marino The FIELD_DECL nodes are chained together and the lot of them
6742e4b17023SJohn Marino are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
6743e4b17023SJohn Marino
6744e4b17023SJohn Marino tree
grokfield(location_t loc,struct c_declarator * declarator,struct c_declspecs * declspecs,tree width,tree * decl_attrs)6745e4b17023SJohn Marino grokfield (location_t loc,
6746e4b17023SJohn Marino struct c_declarator *declarator, struct c_declspecs *declspecs,
6747e4b17023SJohn Marino tree width, tree *decl_attrs)
6748e4b17023SJohn Marino {
6749e4b17023SJohn Marino tree value;
6750e4b17023SJohn Marino
6751e4b17023SJohn Marino if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
6752e4b17023SJohn Marino && width == NULL_TREE)
6753e4b17023SJohn Marino {
6754e4b17023SJohn Marino /* This is an unnamed decl.
6755e4b17023SJohn Marino
6756e4b17023SJohn Marino If we have something of the form "union { list } ;" then this
6757e4b17023SJohn Marino is the anonymous union extension. Similarly for struct.
6758e4b17023SJohn Marino
6759e4b17023SJohn Marino If this is something of the form "struct foo;", then
6760e4b17023SJohn Marino If MS or Plan 9 extensions are enabled, this is handled as
6761e4b17023SJohn Marino an anonymous struct.
6762e4b17023SJohn Marino Otherwise this is a forward declaration of a structure tag.
6763e4b17023SJohn Marino
6764e4b17023SJohn Marino If this is something of the form "foo;" and foo is a TYPE_DECL, then
6765e4b17023SJohn Marino If foo names a structure or union without a tag, then this
6766e4b17023SJohn Marino is an anonymous struct (this is permitted by C11).
6767e4b17023SJohn Marino If MS or Plan 9 extensions are enabled and foo names a
6768e4b17023SJohn Marino structure, then again this is an anonymous struct.
6769e4b17023SJohn Marino Otherwise this is an error.
6770e4b17023SJohn Marino
6771e4b17023SJohn Marino Oh what a horrid tangled web we weave. I wonder if MS consciously
6772e4b17023SJohn Marino took this from Plan 9 or if it was an accident of implementation
6773e4b17023SJohn Marino that took root before someone noticed the bug... */
6774e4b17023SJohn Marino
6775e4b17023SJohn Marino tree type = declspecs->type;
6776e4b17023SJohn Marino bool type_ok = (TREE_CODE (type) == RECORD_TYPE
6777e4b17023SJohn Marino || TREE_CODE (type) == UNION_TYPE);
6778e4b17023SJohn Marino bool ok = false;
6779e4b17023SJohn Marino
6780e4b17023SJohn Marino if (type_ok
6781e4b17023SJohn Marino && (flag_ms_extensions
6782e4b17023SJohn Marino || flag_plan9_extensions
6783e4b17023SJohn Marino || !declspecs->typedef_p))
6784e4b17023SJohn Marino {
6785e4b17023SJohn Marino if (flag_ms_extensions || flag_plan9_extensions)
6786e4b17023SJohn Marino ok = true;
6787e4b17023SJohn Marino else if (TYPE_NAME (type) == NULL)
6788e4b17023SJohn Marino ok = true;
6789e4b17023SJohn Marino else
6790e4b17023SJohn Marino ok = false;
6791e4b17023SJohn Marino }
6792e4b17023SJohn Marino if (!ok)
6793e4b17023SJohn Marino {
6794e4b17023SJohn Marino pedwarn (loc, 0, "declaration does not declare anything");
6795e4b17023SJohn Marino return NULL_TREE;
6796e4b17023SJohn Marino }
6797e4b17023SJohn Marino if (!flag_isoc11)
6798e4b17023SJohn Marino {
6799e4b17023SJohn Marino if (flag_isoc99)
6800e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
6801e4b17023SJohn Marino "ISO C99 doesn%'t support unnamed structs/unions");
6802e4b17023SJohn Marino else
6803e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
6804e4b17023SJohn Marino "ISO C90 doesn%'t support unnamed structs/unions");
6805e4b17023SJohn Marino }
6806e4b17023SJohn Marino }
6807e4b17023SJohn Marino
6808e4b17023SJohn Marino value = grokdeclarator (declarator, declspecs, FIELD, false,
6809e4b17023SJohn Marino width ? &width : NULL, decl_attrs, NULL, NULL,
6810e4b17023SJohn Marino DEPRECATED_NORMAL);
6811e4b17023SJohn Marino
6812e4b17023SJohn Marino finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
6813e4b17023SJohn Marino DECL_INITIAL (value) = width;
6814e4b17023SJohn Marino
6815e4b17023SJohn Marino if (warn_cxx_compat && DECL_NAME (value) != NULL_TREE)
6816e4b17023SJohn Marino {
6817e4b17023SJohn Marino /* If we currently have a binding for this field, set the
6818e4b17023SJohn Marino in_struct field in the binding, so that we warn about lookups
6819e4b17023SJohn Marino which find it. */
6820e4b17023SJohn Marino struct c_binding *b = I_SYMBOL_BINDING (DECL_NAME (value));
6821e4b17023SJohn Marino if (b != NULL)
6822e4b17023SJohn Marino {
6823e4b17023SJohn Marino /* If the in_struct field is not yet set, push it on a list
6824e4b17023SJohn Marino to be cleared when this struct is finished. */
6825e4b17023SJohn Marino if (!b->in_struct)
6826e4b17023SJohn Marino {
6827e4b17023SJohn Marino VEC_safe_push (c_binding_ptr, heap,
6828e4b17023SJohn Marino struct_parse_info->fields, b);
6829e4b17023SJohn Marino b->in_struct = 1;
6830e4b17023SJohn Marino }
6831e4b17023SJohn Marino }
6832e4b17023SJohn Marino }
6833e4b17023SJohn Marino
6834e4b17023SJohn Marino return value;
6835e4b17023SJohn Marino }
6836e4b17023SJohn Marino
6837e4b17023SJohn Marino /* Subroutine of detect_field_duplicates: return whether X and Y,
6838e4b17023SJohn Marino which are both fields in the same struct, have duplicate field
6839e4b17023SJohn Marino names. */
6840e4b17023SJohn Marino
6841e4b17023SJohn Marino static bool
is_duplicate_field(tree x,tree y)6842e4b17023SJohn Marino is_duplicate_field (tree x, tree y)
6843e4b17023SJohn Marino {
6844e4b17023SJohn Marino if (DECL_NAME (x) != NULL_TREE && DECL_NAME (x) == DECL_NAME (y))
6845e4b17023SJohn Marino return true;
6846e4b17023SJohn Marino
6847e4b17023SJohn Marino /* When using -fplan9-extensions, an anonymous field whose name is a
6848e4b17023SJohn Marino typedef can duplicate a field name. */
6849e4b17023SJohn Marino if (flag_plan9_extensions
6850e4b17023SJohn Marino && (DECL_NAME (x) == NULL_TREE || DECL_NAME (y) == NULL_TREE))
6851e4b17023SJohn Marino {
6852e4b17023SJohn Marino tree xt, xn, yt, yn;
6853e4b17023SJohn Marino
6854e4b17023SJohn Marino xt = TREE_TYPE (x);
6855e4b17023SJohn Marino if (DECL_NAME (x) != NULL_TREE)
6856e4b17023SJohn Marino xn = DECL_NAME (x);
6857e4b17023SJohn Marino else if ((TREE_CODE (xt) == RECORD_TYPE || TREE_CODE (xt) == UNION_TYPE)
6858e4b17023SJohn Marino && TYPE_NAME (xt) != NULL_TREE
6859e4b17023SJohn Marino && TREE_CODE (TYPE_NAME (xt)) == TYPE_DECL)
6860e4b17023SJohn Marino xn = DECL_NAME (TYPE_NAME (xt));
6861e4b17023SJohn Marino else
6862e4b17023SJohn Marino xn = NULL_TREE;
6863e4b17023SJohn Marino
6864e4b17023SJohn Marino yt = TREE_TYPE (y);
6865e4b17023SJohn Marino if (DECL_NAME (y) != NULL_TREE)
6866e4b17023SJohn Marino yn = DECL_NAME (y);
6867e4b17023SJohn Marino else if ((TREE_CODE (yt) == RECORD_TYPE || TREE_CODE (yt) == UNION_TYPE)
6868e4b17023SJohn Marino && TYPE_NAME (yt) != NULL_TREE
6869e4b17023SJohn Marino && TREE_CODE (TYPE_NAME (yt)) == TYPE_DECL)
6870e4b17023SJohn Marino yn = DECL_NAME (TYPE_NAME (yt));
6871e4b17023SJohn Marino else
6872e4b17023SJohn Marino yn = NULL_TREE;
6873e4b17023SJohn Marino
6874e4b17023SJohn Marino if (xn != NULL_TREE && xn == yn)
6875e4b17023SJohn Marino return true;
6876e4b17023SJohn Marino }
6877e4b17023SJohn Marino
6878e4b17023SJohn Marino return false;
6879e4b17023SJohn Marino }
6880e4b17023SJohn Marino
6881e4b17023SJohn Marino /* Subroutine of detect_field_duplicates: add the fields of FIELDLIST
6882e4b17023SJohn Marino to HTAB, giving errors for any duplicates. */
6883e4b17023SJohn Marino
6884e4b17023SJohn Marino static void
detect_field_duplicates_hash(tree fieldlist,htab_t htab)6885e4b17023SJohn Marino detect_field_duplicates_hash (tree fieldlist, htab_t htab)
6886e4b17023SJohn Marino {
6887e4b17023SJohn Marino tree x, y;
6888e4b17023SJohn Marino void **slot;
6889e4b17023SJohn Marino
6890e4b17023SJohn Marino for (x = fieldlist; x ; x = DECL_CHAIN (x))
6891e4b17023SJohn Marino if ((y = DECL_NAME (x)) != 0)
6892e4b17023SJohn Marino {
6893e4b17023SJohn Marino slot = htab_find_slot (htab, y, INSERT);
6894e4b17023SJohn Marino if (*slot)
6895e4b17023SJohn Marino {
6896e4b17023SJohn Marino error ("duplicate member %q+D", x);
6897e4b17023SJohn Marino DECL_NAME (x) = NULL_TREE;
6898e4b17023SJohn Marino }
6899e4b17023SJohn Marino *slot = y;
6900e4b17023SJohn Marino }
6901e4b17023SJohn Marino else if (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6902e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6903e4b17023SJohn Marino {
6904e4b17023SJohn Marino detect_field_duplicates_hash (TYPE_FIELDS (TREE_TYPE (x)), htab);
6905e4b17023SJohn Marino
6906e4b17023SJohn Marino /* When using -fplan9-extensions, an anonymous field whose
6907e4b17023SJohn Marino name is a typedef can duplicate a field name. */
6908e4b17023SJohn Marino if (flag_plan9_extensions
6909e4b17023SJohn Marino && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6910e4b17023SJohn Marino && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL)
6911e4b17023SJohn Marino {
6912e4b17023SJohn Marino tree xn = DECL_NAME (TYPE_NAME (TREE_TYPE (x)));
6913e4b17023SJohn Marino slot = htab_find_slot (htab, xn, INSERT);
6914e4b17023SJohn Marino if (*slot)
6915e4b17023SJohn Marino error ("duplicate member %q+D", TYPE_NAME (TREE_TYPE (x)));
6916e4b17023SJohn Marino *slot = xn;
6917e4b17023SJohn Marino }
6918e4b17023SJohn Marino }
6919e4b17023SJohn Marino }
6920e4b17023SJohn Marino
6921e4b17023SJohn Marino /* Generate an error for any duplicate field names in FIELDLIST. Munge
6922e4b17023SJohn Marino the list such that this does not present a problem later. */
6923e4b17023SJohn Marino
6924e4b17023SJohn Marino static void
detect_field_duplicates(tree fieldlist)6925e4b17023SJohn Marino detect_field_duplicates (tree fieldlist)
6926e4b17023SJohn Marino {
6927e4b17023SJohn Marino tree x, y;
6928e4b17023SJohn Marino int timeout = 10;
6929e4b17023SJohn Marino
6930e4b17023SJohn Marino /* If the struct is the list of instance variables of an Objective-C
6931e4b17023SJohn Marino class, then we need to check all the instance variables of
6932e4b17023SJohn Marino superclasses when checking for duplicates (since you can't have
6933e4b17023SJohn Marino an instance variable in a subclass with the same name as an
6934e4b17023SJohn Marino instance variable in a superclass). We pass on this job to the
6935e4b17023SJohn Marino Objective-C compiler. objc_detect_field_duplicates() will return
6936e4b17023SJohn Marino false if we are not checking the list of instance variables and
6937e4b17023SJohn Marino the C frontend should proceed with the standard field duplicate
6938e4b17023SJohn Marino checks. If we are checking the list of instance variables, the
6939e4b17023SJohn Marino ObjC frontend will do the check, emit the errors if needed, and
6940e4b17023SJohn Marino then return true. */
6941e4b17023SJohn Marino if (c_dialect_objc ())
6942e4b17023SJohn Marino if (objc_detect_field_duplicates (false))
6943e4b17023SJohn Marino return;
6944e4b17023SJohn Marino
6945e4b17023SJohn Marino /* First, see if there are more than "a few" fields.
6946e4b17023SJohn Marino This is trivially true if there are zero or one fields. */
6947e4b17023SJohn Marino if (!fieldlist || !DECL_CHAIN (fieldlist))
6948e4b17023SJohn Marino return;
6949e4b17023SJohn Marino x = fieldlist;
6950e4b17023SJohn Marino do {
6951e4b17023SJohn Marino timeout--;
6952e4b17023SJohn Marino if (DECL_NAME (x) == NULL_TREE
6953e4b17023SJohn Marino && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6954e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
6955e4b17023SJohn Marino timeout = 0;
6956e4b17023SJohn Marino x = DECL_CHAIN (x);
6957e4b17023SJohn Marino } while (timeout > 0 && x);
6958e4b17023SJohn Marino
6959e4b17023SJohn Marino /* If there were "few" fields and no anonymous structures or unions,
6960e4b17023SJohn Marino avoid the overhead of allocating a hash table. Instead just do
6961e4b17023SJohn Marino the nested traversal thing. */
6962e4b17023SJohn Marino if (timeout > 0)
6963e4b17023SJohn Marino {
6964e4b17023SJohn Marino for (x = DECL_CHAIN (fieldlist); x; x = DECL_CHAIN (x))
6965e4b17023SJohn Marino /* When using -fplan9-extensions, we can have duplicates
6966e4b17023SJohn Marino between typedef names and fields. */
6967e4b17023SJohn Marino if (DECL_NAME (x)
6968e4b17023SJohn Marino || (flag_plan9_extensions
6969e4b17023SJohn Marino && DECL_NAME (x) == NULL_TREE
6970e4b17023SJohn Marino && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
6971e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
6972e4b17023SJohn Marino && TYPE_NAME (TREE_TYPE (x)) != NULL_TREE
6973e4b17023SJohn Marino && TREE_CODE (TYPE_NAME (TREE_TYPE (x))) == TYPE_DECL))
6974e4b17023SJohn Marino {
6975e4b17023SJohn Marino for (y = fieldlist; y != x; y = TREE_CHAIN (y))
6976e4b17023SJohn Marino if (is_duplicate_field (y, x))
6977e4b17023SJohn Marino {
6978e4b17023SJohn Marino error ("duplicate member %q+D", x);
6979e4b17023SJohn Marino DECL_NAME (x) = NULL_TREE;
6980e4b17023SJohn Marino }
6981e4b17023SJohn Marino }
6982e4b17023SJohn Marino }
6983e4b17023SJohn Marino else
6984e4b17023SJohn Marino {
6985e4b17023SJohn Marino htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
6986e4b17023SJohn Marino
6987e4b17023SJohn Marino detect_field_duplicates_hash (fieldlist, htab);
6988e4b17023SJohn Marino htab_delete (htab);
6989e4b17023SJohn Marino }
6990e4b17023SJohn Marino }
6991e4b17023SJohn Marino
6992e4b17023SJohn Marino /* Finish up struct info used by -Wc++-compat. */
6993e4b17023SJohn Marino
6994e4b17023SJohn Marino static void
warn_cxx_compat_finish_struct(tree fieldlist)6995e4b17023SJohn Marino warn_cxx_compat_finish_struct (tree fieldlist)
6996e4b17023SJohn Marino {
6997e4b17023SJohn Marino unsigned int ix;
6998e4b17023SJohn Marino tree x;
6999e4b17023SJohn Marino struct c_binding *b;
7000e4b17023SJohn Marino
7001e4b17023SJohn Marino /* Set the C_TYPE_DEFINED_IN_STRUCT flag for each type defined in
7002e4b17023SJohn Marino the current struct. We do this now at the end of the struct
7003e4b17023SJohn Marino because the flag is used to issue visibility warnings, and we
7004e4b17023SJohn Marino only want to issue those warnings if the type is referenced
7005e4b17023SJohn Marino outside of the struct declaration. */
7006e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, struct_parse_info->struct_types, ix, x)
7007e4b17023SJohn Marino C_TYPE_DEFINED_IN_STRUCT (x) = 1;
7008e4b17023SJohn Marino
7009e4b17023SJohn Marino /* The TYPEDEFS_SEEN field of STRUCT_PARSE_INFO is a list of
7010e4b17023SJohn Marino typedefs used when declaring fields in this struct. If the name
7011e4b17023SJohn Marino of any of the fields is also a typedef name then the struct would
7012e4b17023SJohn Marino not parse in C++, because the C++ lookup rules say that the
7013e4b17023SJohn Marino typedef name would be looked up in the context of the struct, and
7014e4b17023SJohn Marino would thus be the field rather than the typedef. */
7015e4b17023SJohn Marino if (!VEC_empty (tree, struct_parse_info->typedefs_seen)
7016e4b17023SJohn Marino && fieldlist != NULL_TREE)
7017e4b17023SJohn Marino {
7018e4b17023SJohn Marino /* Use a pointer_set using the name of the typedef. We can use
7019e4b17023SJohn Marino a pointer_set because identifiers are interned. */
7020e4b17023SJohn Marino struct pointer_set_t *tset = pointer_set_create ();
7021e4b17023SJohn Marino
7022e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, struct_parse_info->typedefs_seen, ix, x)
7023e4b17023SJohn Marino pointer_set_insert (tset, DECL_NAME (x));
7024e4b17023SJohn Marino
7025e4b17023SJohn Marino for (x = fieldlist; x != NULL_TREE; x = DECL_CHAIN (x))
7026e4b17023SJohn Marino {
7027e4b17023SJohn Marino if (DECL_NAME (x) != NULL_TREE
7028e4b17023SJohn Marino && pointer_set_contains (tset, DECL_NAME (x)))
7029e4b17023SJohn Marino {
7030e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (x), OPT_Wc___compat,
7031e4b17023SJohn Marino ("using %qD as both field and typedef name is "
7032e4b17023SJohn Marino "invalid in C++"),
7033e4b17023SJohn Marino x);
7034e4b17023SJohn Marino /* FIXME: It would be nice to report the location where
7035e4b17023SJohn Marino the typedef name is used. */
7036e4b17023SJohn Marino }
7037e4b17023SJohn Marino }
7038e4b17023SJohn Marino
7039e4b17023SJohn Marino pointer_set_destroy (tset);
7040e4b17023SJohn Marino }
7041e4b17023SJohn Marino
7042e4b17023SJohn Marino /* For each field which has a binding and which was not defined in
7043e4b17023SJohn Marino an enclosing struct, clear the in_struct field. */
7044e4b17023SJohn Marino FOR_EACH_VEC_ELT (c_binding_ptr, struct_parse_info->fields, ix, b)
7045e4b17023SJohn Marino b->in_struct = 0;
7046e4b17023SJohn Marino }
7047e4b17023SJohn Marino
7048e4b17023SJohn Marino /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
7049e4b17023SJohn Marino LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
7050e4b17023SJohn Marino FIELDLIST is a chain of FIELD_DECL nodes for the fields.
7051e4b17023SJohn Marino ATTRIBUTES are attributes to be applied to the structure.
7052e4b17023SJohn Marino
7053e4b17023SJohn Marino ENCLOSING_STRUCT_PARSE_INFO is the value of STRUCT_PARSE_INFO when
7054e4b17023SJohn Marino the struct was started. */
7055e4b17023SJohn Marino
7056e4b17023SJohn Marino tree
finish_struct(location_t loc,tree t,tree fieldlist,tree attributes,struct c_struct_parse_info * enclosing_struct_parse_info)7057e4b17023SJohn Marino finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
7058e4b17023SJohn Marino struct c_struct_parse_info *enclosing_struct_parse_info)
7059e4b17023SJohn Marino {
7060e4b17023SJohn Marino tree x;
7061e4b17023SJohn Marino bool toplevel = file_scope == current_scope;
7062e4b17023SJohn Marino int saw_named_field;
7063e4b17023SJohn Marino
7064e4b17023SJohn Marino /* If this type was previously laid out as a forward reference,
7065e4b17023SJohn Marino make sure we lay it out again. */
7066e4b17023SJohn Marino
7067e4b17023SJohn Marino TYPE_SIZE (t) = 0;
7068e4b17023SJohn Marino
7069e4b17023SJohn Marino decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7070e4b17023SJohn Marino
7071e4b17023SJohn Marino if (pedantic)
7072e4b17023SJohn Marino {
7073e4b17023SJohn Marino for (x = fieldlist; x; x = DECL_CHAIN (x))
7074e4b17023SJohn Marino {
7075e4b17023SJohn Marino if (DECL_NAME (x) != 0)
7076e4b17023SJohn Marino break;
7077e4b17023SJohn Marino if (flag_isoc11
7078e4b17023SJohn Marino && (TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7079e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE))
7080e4b17023SJohn Marino break;
7081e4b17023SJohn Marino }
7082e4b17023SJohn Marino
7083e4b17023SJohn Marino if (x == 0)
7084e4b17023SJohn Marino {
7085e4b17023SJohn Marino if (TREE_CODE (t) == UNION_TYPE)
7086e4b17023SJohn Marino {
7087e4b17023SJohn Marino if (fieldlist)
7088e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "union has no named members");
7089e4b17023SJohn Marino else
7090e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "union has no members");
7091e4b17023SJohn Marino }
7092e4b17023SJohn Marino else
7093e4b17023SJohn Marino {
7094e4b17023SJohn Marino if (fieldlist)
7095e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "struct has no named members");
7096e4b17023SJohn Marino else
7097e4b17023SJohn Marino pedwarn (loc, OPT_pedantic, "struct has no members");
7098e4b17023SJohn Marino }
7099e4b17023SJohn Marino }
7100e4b17023SJohn Marino }
7101e4b17023SJohn Marino
7102e4b17023SJohn Marino /* Install struct as DECL_CONTEXT of each field decl.
7103e4b17023SJohn Marino Also process specified field sizes, found in the DECL_INITIAL,
7104e4b17023SJohn Marino storing 0 there after the type has been changed to precision equal
7105e4b17023SJohn Marino to its width, rather than the precision of the specified standard
7106e4b17023SJohn Marino type. (Correct layout requires the original type to have been preserved
7107e4b17023SJohn Marino until now.) */
7108e4b17023SJohn Marino
7109e4b17023SJohn Marino saw_named_field = 0;
7110e4b17023SJohn Marino for (x = fieldlist; x; x = DECL_CHAIN (x))
7111e4b17023SJohn Marino {
7112e4b17023SJohn Marino if (TREE_TYPE (x) == error_mark_node)
7113e4b17023SJohn Marino continue;
7114e4b17023SJohn Marino
7115e4b17023SJohn Marino DECL_CONTEXT (x) = t;
7116e4b17023SJohn Marino
7117e4b17023SJohn Marino /* If any field is const, the structure type is pseudo-const. */
7118e4b17023SJohn Marino if (TREE_READONLY (x))
7119e4b17023SJohn Marino C_TYPE_FIELDS_READONLY (t) = 1;
7120e4b17023SJohn Marino else
7121e4b17023SJohn Marino {
7122e4b17023SJohn Marino /* A field that is pseudo-const makes the structure likewise. */
7123e4b17023SJohn Marino tree t1 = strip_array_types (TREE_TYPE (x));
7124e4b17023SJohn Marino if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
7125e4b17023SJohn Marino && C_TYPE_FIELDS_READONLY (t1))
7126e4b17023SJohn Marino C_TYPE_FIELDS_READONLY (t) = 1;
7127e4b17023SJohn Marino }
7128e4b17023SJohn Marino
7129e4b17023SJohn Marino /* Any field that is volatile means variables of this type must be
7130e4b17023SJohn Marino treated in some ways as volatile. */
7131e4b17023SJohn Marino if (TREE_THIS_VOLATILE (x))
7132e4b17023SJohn Marino C_TYPE_FIELDS_VOLATILE (t) = 1;
7133e4b17023SJohn Marino
7134e4b17023SJohn Marino /* Any field of nominal variable size implies structure is too. */
7135e4b17023SJohn Marino if (C_DECL_VARIABLE_SIZE (x))
7136e4b17023SJohn Marino C_TYPE_VARIABLE_SIZE (t) = 1;
7137e4b17023SJohn Marino
7138e4b17023SJohn Marino if (DECL_INITIAL (x))
7139e4b17023SJohn Marino {
7140e4b17023SJohn Marino unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
7141e4b17023SJohn Marino DECL_SIZE (x) = bitsize_int (width);
7142e4b17023SJohn Marino DECL_BIT_FIELD (x) = 1;
7143e4b17023SJohn Marino SET_DECL_C_BIT_FIELD (x);
7144e4b17023SJohn Marino }
7145e4b17023SJohn Marino
7146e4b17023SJohn Marino if (TYPE_PACKED (t)
7147e4b17023SJohn Marino && (DECL_BIT_FIELD (x)
7148e4b17023SJohn Marino || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT))
7149e4b17023SJohn Marino DECL_PACKED (x) = 1;
7150e4b17023SJohn Marino
7151e4b17023SJohn Marino /* Detect flexible array member in an invalid context. */
7152e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
7153e4b17023SJohn Marino && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
7154e4b17023SJohn Marino && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
7155e4b17023SJohn Marino && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
7156e4b17023SJohn Marino {
7157e4b17023SJohn Marino if (TREE_CODE (t) == UNION_TYPE)
7158e4b17023SJohn Marino {
7159e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (x),
7160e4b17023SJohn Marino "flexible array member in union");
7161e4b17023SJohn Marino TREE_TYPE (x) = error_mark_node;
7162e4b17023SJohn Marino }
7163e4b17023SJohn Marino else if (DECL_CHAIN (x) != NULL_TREE)
7164e4b17023SJohn Marino {
7165e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (x),
7166e4b17023SJohn Marino "flexible array member not at end of struct");
7167e4b17023SJohn Marino TREE_TYPE (x) = error_mark_node;
7168e4b17023SJohn Marino }
7169e4b17023SJohn Marino else if (!saw_named_field)
7170e4b17023SJohn Marino {
7171e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (x),
7172e4b17023SJohn Marino "flexible array member in otherwise empty struct");
7173e4b17023SJohn Marino TREE_TYPE (x) = error_mark_node;
7174e4b17023SJohn Marino }
7175e4b17023SJohn Marino }
7176e4b17023SJohn Marino
7177e4b17023SJohn Marino if (pedantic && TREE_CODE (t) == RECORD_TYPE
7178e4b17023SJohn Marino && flexible_array_type_p (TREE_TYPE (x)))
7179e4b17023SJohn Marino pedwarn (DECL_SOURCE_LOCATION (x), OPT_pedantic,
7180e4b17023SJohn Marino "invalid use of structure with flexible array member");
7181e4b17023SJohn Marino
7182e4b17023SJohn Marino if (DECL_NAME (x)
7183e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == RECORD_TYPE
7184e4b17023SJohn Marino || TREE_CODE (TREE_TYPE (x)) == UNION_TYPE)
7185e4b17023SJohn Marino saw_named_field = 1;
7186e4b17023SJohn Marino }
7187e4b17023SJohn Marino
7188e4b17023SJohn Marino detect_field_duplicates (fieldlist);
7189e4b17023SJohn Marino
7190e4b17023SJohn Marino /* Now we have the nearly final fieldlist. Record it,
7191e4b17023SJohn Marino then lay out the structure or union (including the fields). */
7192e4b17023SJohn Marino
7193e4b17023SJohn Marino TYPE_FIELDS (t) = fieldlist;
7194e4b17023SJohn Marino
7195e4b17023SJohn Marino layout_type (t);
7196e4b17023SJohn Marino
7197e4b17023SJohn Marino /* Give bit-fields their proper types. */
7198e4b17023SJohn Marino {
7199e4b17023SJohn Marino tree *fieldlistp = &fieldlist;
7200e4b17023SJohn Marino while (*fieldlistp)
7201e4b17023SJohn Marino if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
7202e4b17023SJohn Marino && TREE_TYPE (*fieldlistp) != error_mark_node)
7203e4b17023SJohn Marino {
7204e4b17023SJohn Marino unsigned HOST_WIDE_INT width
7205e4b17023SJohn Marino = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
7206e4b17023SJohn Marino tree type = TREE_TYPE (*fieldlistp);
7207e4b17023SJohn Marino if (width != TYPE_PRECISION (type))
7208e4b17023SJohn Marino {
7209e4b17023SJohn Marino TREE_TYPE (*fieldlistp)
7210e4b17023SJohn Marino = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
7211e4b17023SJohn Marino DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
7212e4b17023SJohn Marino }
7213e4b17023SJohn Marino DECL_INITIAL (*fieldlistp) = 0;
7214e4b17023SJohn Marino }
7215e4b17023SJohn Marino else
7216e4b17023SJohn Marino fieldlistp = &DECL_CHAIN (*fieldlistp);
7217e4b17023SJohn Marino }
7218e4b17023SJohn Marino
7219e4b17023SJohn Marino /* Now we have the truly final field list.
7220e4b17023SJohn Marino Store it in this type and in the variants. */
7221e4b17023SJohn Marino
7222e4b17023SJohn Marino TYPE_FIELDS (t) = fieldlist;
7223e4b17023SJohn Marino
7224e4b17023SJohn Marino /* If there are lots of fields, sort so we can look through them fast.
7225e4b17023SJohn Marino We arbitrarily consider 16 or more elts to be "a lot". */
7226e4b17023SJohn Marino
7227e4b17023SJohn Marino {
7228e4b17023SJohn Marino int len = 0;
7229e4b17023SJohn Marino
7230e4b17023SJohn Marino for (x = fieldlist; x; x = DECL_CHAIN (x))
7231e4b17023SJohn Marino {
7232e4b17023SJohn Marino if (len > 15 || DECL_NAME (x) == NULL)
7233e4b17023SJohn Marino break;
7234e4b17023SJohn Marino len += 1;
7235e4b17023SJohn Marino }
7236e4b17023SJohn Marino
7237e4b17023SJohn Marino if (len > 15)
7238e4b17023SJohn Marino {
7239e4b17023SJohn Marino tree *field_array;
7240e4b17023SJohn Marino struct lang_type *space;
7241e4b17023SJohn Marino struct sorted_fields_type *space2;
7242e4b17023SJohn Marino
7243e4b17023SJohn Marino len += list_length (x);
7244e4b17023SJohn Marino
7245e4b17023SJohn Marino /* Use the same allocation policy here that make_node uses, to
7246e4b17023SJohn Marino ensure that this lives as long as the rest of the struct decl.
7247e4b17023SJohn Marino All decls in an inline function need to be saved. */
7248e4b17023SJohn Marino
7249e4b17023SJohn Marino space = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7250e4b17023SJohn Marino space2 = ggc_alloc_sorted_fields_type
7251e4b17023SJohn Marino (sizeof (struct sorted_fields_type) + len * sizeof (tree));
7252e4b17023SJohn Marino
7253e4b17023SJohn Marino len = 0;
7254e4b17023SJohn Marino space->s = space2;
7255e4b17023SJohn Marino field_array = &space2->elts[0];
7256e4b17023SJohn Marino for (x = fieldlist; x; x = DECL_CHAIN (x))
7257e4b17023SJohn Marino {
7258e4b17023SJohn Marino field_array[len++] = x;
7259e4b17023SJohn Marino
7260e4b17023SJohn Marino /* If there is anonymous struct or union, break out of the loop. */
7261e4b17023SJohn Marino if (DECL_NAME (x) == NULL)
7262e4b17023SJohn Marino break;
7263e4b17023SJohn Marino }
7264e4b17023SJohn Marino /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
7265e4b17023SJohn Marino if (x == NULL)
7266e4b17023SJohn Marino {
7267e4b17023SJohn Marino TYPE_LANG_SPECIFIC (t) = space;
7268e4b17023SJohn Marino TYPE_LANG_SPECIFIC (t)->s->len = len;
7269e4b17023SJohn Marino field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
7270e4b17023SJohn Marino qsort (field_array, len, sizeof (tree), field_decl_cmp);
7271e4b17023SJohn Marino }
7272e4b17023SJohn Marino }
7273e4b17023SJohn Marino }
7274e4b17023SJohn Marino
7275e4b17023SJohn Marino for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
7276e4b17023SJohn Marino {
7277e4b17023SJohn Marino TYPE_FIELDS (x) = TYPE_FIELDS (t);
7278e4b17023SJohn Marino TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
7279e4b17023SJohn Marino C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
7280e4b17023SJohn Marino C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
7281e4b17023SJohn Marino C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
7282e4b17023SJohn Marino }
7283e4b17023SJohn Marino
7284e4b17023SJohn Marino /* If this was supposed to be a transparent union, but we can't
7285e4b17023SJohn Marino make it one, warn and turn off the flag. */
7286e4b17023SJohn Marino if (TREE_CODE (t) == UNION_TYPE
7287e4b17023SJohn Marino && TYPE_TRANSPARENT_AGGR (t)
7288e4b17023SJohn Marino && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
7289e4b17023SJohn Marino {
7290e4b17023SJohn Marino TYPE_TRANSPARENT_AGGR (t) = 0;
7291e4b17023SJohn Marino warning_at (loc, 0, "union cannot be made transparent");
7292e4b17023SJohn Marino }
7293e4b17023SJohn Marino
7294e4b17023SJohn Marino /* If this structure or union completes the type of any previous
7295e4b17023SJohn Marino variable declaration, lay it out and output its rtl. */
7296e4b17023SJohn Marino for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
7297e4b17023SJohn Marino x;
7298e4b17023SJohn Marino x = TREE_CHAIN (x))
7299e4b17023SJohn Marino {
7300e4b17023SJohn Marino tree decl = TREE_VALUE (x);
7301e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
7302e4b17023SJohn Marino layout_array_type (TREE_TYPE (decl));
7303e4b17023SJohn Marino if (TREE_CODE (decl) != TYPE_DECL)
7304e4b17023SJohn Marino {
7305e4b17023SJohn Marino layout_decl (decl, 0);
7306e4b17023SJohn Marino if (c_dialect_objc ())
7307e4b17023SJohn Marino objc_check_decl (decl);
7308e4b17023SJohn Marino rest_of_decl_compilation (decl, toplevel, 0);
7309e4b17023SJohn Marino if (!toplevel)
7310e4b17023SJohn Marino expand_decl (decl);
7311e4b17023SJohn Marino }
7312e4b17023SJohn Marino }
7313e4b17023SJohn Marino C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
7314e4b17023SJohn Marino
7315e4b17023SJohn Marino /* Update type location to the one of the definition, instead of e.g.
7316e4b17023SJohn Marino a forward declaration. */
7317e4b17023SJohn Marino if (TYPE_STUB_DECL (t))
7318e4b17023SJohn Marino DECL_SOURCE_LOCATION (TYPE_STUB_DECL (t)) = loc;
7319e4b17023SJohn Marino
7320e4b17023SJohn Marino /* Finish debugging output for this type. */
7321e4b17023SJohn Marino rest_of_type_compilation (t, toplevel);
7322e4b17023SJohn Marino
7323e4b17023SJohn Marino /* If we're inside a function proper, i.e. not file-scope and not still
7324e4b17023SJohn Marino parsing parameters, then arrange for the size of a variable sized type
7325e4b17023SJohn Marino to be bound now. */
7326e4b17023SJohn Marino if (building_stmt_list_p () && variably_modified_type_p (t, NULL_TREE))
7327e4b17023SJohn Marino add_stmt (build_stmt (loc,
7328e4b17023SJohn Marino DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
7329e4b17023SJohn Marino
7330e4b17023SJohn Marino if (warn_cxx_compat)
7331e4b17023SJohn Marino warn_cxx_compat_finish_struct (fieldlist);
7332e4b17023SJohn Marino
7333e4b17023SJohn Marino VEC_free (tree, heap, struct_parse_info->struct_types);
7334e4b17023SJohn Marino VEC_free (c_binding_ptr, heap, struct_parse_info->fields);
7335e4b17023SJohn Marino VEC_free (tree, heap, struct_parse_info->typedefs_seen);
7336e4b17023SJohn Marino XDELETE (struct_parse_info);
7337e4b17023SJohn Marino
7338e4b17023SJohn Marino struct_parse_info = enclosing_struct_parse_info;
7339e4b17023SJohn Marino
7340e4b17023SJohn Marino /* If this struct is defined inside a struct, add it to
7341e4b17023SJohn Marino struct_types. */
7342e4b17023SJohn Marino if (warn_cxx_compat
7343e4b17023SJohn Marino && struct_parse_info != NULL
7344e4b17023SJohn Marino && !in_sizeof && !in_typeof && !in_alignof)
7345e4b17023SJohn Marino VEC_safe_push (tree, heap, struct_parse_info->struct_types, t);
7346e4b17023SJohn Marino
7347e4b17023SJohn Marino return t;
7348e4b17023SJohn Marino }
7349e4b17023SJohn Marino
7350e4b17023SJohn Marino /* Lay out the type T, and its element type, and so on. */
7351e4b17023SJohn Marino
7352e4b17023SJohn Marino static void
layout_array_type(tree t)7353e4b17023SJohn Marino layout_array_type (tree t)
7354e4b17023SJohn Marino {
7355e4b17023SJohn Marino if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7356e4b17023SJohn Marino layout_array_type (TREE_TYPE (t));
7357e4b17023SJohn Marino layout_type (t);
7358e4b17023SJohn Marino }
7359e4b17023SJohn Marino
7360e4b17023SJohn Marino /* Begin compiling the definition of an enumeration type.
7361e4b17023SJohn Marino NAME is its name (or null if anonymous).
7362e4b17023SJohn Marino LOC is the enum's location.
7363e4b17023SJohn Marino Returns the type object, as yet incomplete.
7364e4b17023SJohn Marino Also records info about it so that build_enumerator
7365e4b17023SJohn Marino may be used to declare the individual values as they are read. */
7366e4b17023SJohn Marino
7367e4b17023SJohn Marino tree
start_enum(location_t loc,struct c_enum_contents * the_enum,tree name)7368e4b17023SJohn Marino start_enum (location_t loc, struct c_enum_contents *the_enum, tree name)
7369e4b17023SJohn Marino {
7370e4b17023SJohn Marino tree enumtype = NULL_TREE;
7371e4b17023SJohn Marino location_t enumloc = UNKNOWN_LOCATION;
7372e4b17023SJohn Marino
7373e4b17023SJohn Marino /* If this is the real definition for a previous forward reference,
7374e4b17023SJohn Marino fill in the contents in the same object that used to be the
7375e4b17023SJohn Marino forward reference. */
7376e4b17023SJohn Marino
7377e4b17023SJohn Marino if (name != NULL_TREE)
7378e4b17023SJohn Marino enumtype = lookup_tag (ENUMERAL_TYPE, name, 1, &enumloc);
7379e4b17023SJohn Marino
7380e4b17023SJohn Marino if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
7381e4b17023SJohn Marino {
7382e4b17023SJohn Marino enumtype = make_node (ENUMERAL_TYPE);
7383e4b17023SJohn Marino pushtag (loc, name, enumtype);
7384e4b17023SJohn Marino }
7385e4b17023SJohn Marino
7386e4b17023SJohn Marino if (C_TYPE_BEING_DEFINED (enumtype))
7387e4b17023SJohn Marino error_at (loc, "nested redefinition of %<enum %E%>", name);
7388e4b17023SJohn Marino
7389e4b17023SJohn Marino C_TYPE_BEING_DEFINED (enumtype) = 1;
7390e4b17023SJohn Marino
7391e4b17023SJohn Marino if (TYPE_VALUES (enumtype) != 0)
7392e4b17023SJohn Marino {
7393e4b17023SJohn Marino /* This enum is a named one that has been declared already. */
7394e4b17023SJohn Marino error_at (loc, "redeclaration of %<enum %E%>", name);
7395e4b17023SJohn Marino if (enumloc != UNKNOWN_LOCATION)
7396e4b17023SJohn Marino inform (enumloc, "originally defined here");
7397e4b17023SJohn Marino
7398e4b17023SJohn Marino /* Completely replace its old definition.
7399e4b17023SJohn Marino The old enumerators remain defined, however. */
7400e4b17023SJohn Marino TYPE_VALUES (enumtype) = 0;
7401e4b17023SJohn Marino }
7402e4b17023SJohn Marino
7403e4b17023SJohn Marino the_enum->enum_next_value = integer_zero_node;
7404e4b17023SJohn Marino the_enum->enum_overflow = 0;
7405e4b17023SJohn Marino
7406e4b17023SJohn Marino if (flag_short_enums)
7407e4b17023SJohn Marino TYPE_PACKED (enumtype) = 1;
7408e4b17023SJohn Marino
7409e4b17023SJohn Marino /* FIXME: This will issue a warning for a use of a type defined
7410e4b17023SJohn Marino within sizeof in a statement expr. This is not terribly serious
7411e4b17023SJohn Marino as C++ doesn't permit statement exprs within sizeof anyhow. */
7412e4b17023SJohn Marino if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
7413e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
7414e4b17023SJohn Marino "defining type in %qs expression is invalid in C++",
7415e4b17023SJohn Marino (in_sizeof
7416e4b17023SJohn Marino ? "sizeof"
7417e4b17023SJohn Marino : (in_typeof ? "typeof" : "alignof")));
7418e4b17023SJohn Marino
7419e4b17023SJohn Marino return enumtype;
7420e4b17023SJohn Marino }
7421e4b17023SJohn Marino
7422e4b17023SJohn Marino /* After processing and defining all the values of an enumeration type,
7423e4b17023SJohn Marino install their decls in the enumeration type and finish it off.
7424e4b17023SJohn Marino ENUMTYPE is the type object, VALUES a list of decl-value pairs,
7425e4b17023SJohn Marino and ATTRIBUTES are the specified attributes.
7426e4b17023SJohn Marino Returns ENUMTYPE. */
7427e4b17023SJohn Marino
7428e4b17023SJohn Marino tree
finish_enum(tree enumtype,tree values,tree attributes)7429e4b17023SJohn Marino finish_enum (tree enumtype, tree values, tree attributes)
7430e4b17023SJohn Marino {
7431e4b17023SJohn Marino tree pair, tem;
7432e4b17023SJohn Marino tree minnode = 0, maxnode = 0;
7433e4b17023SJohn Marino int precision, unsign;
7434e4b17023SJohn Marino bool toplevel = (file_scope == current_scope);
7435e4b17023SJohn Marino struct lang_type *lt;
7436e4b17023SJohn Marino
7437e4b17023SJohn Marino decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
7438e4b17023SJohn Marino
7439e4b17023SJohn Marino /* Calculate the maximum value of any enumerator in this type. */
7440e4b17023SJohn Marino
7441e4b17023SJohn Marino if (values == error_mark_node)
7442e4b17023SJohn Marino minnode = maxnode = integer_zero_node;
7443e4b17023SJohn Marino else
7444e4b17023SJohn Marino {
7445e4b17023SJohn Marino minnode = maxnode = TREE_VALUE (values);
7446e4b17023SJohn Marino for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
7447e4b17023SJohn Marino {
7448e4b17023SJohn Marino tree value = TREE_VALUE (pair);
7449e4b17023SJohn Marino if (tree_int_cst_lt (maxnode, value))
7450e4b17023SJohn Marino maxnode = value;
7451e4b17023SJohn Marino if (tree_int_cst_lt (value, minnode))
7452e4b17023SJohn Marino minnode = value;
7453e4b17023SJohn Marino }
7454e4b17023SJohn Marino }
7455e4b17023SJohn Marino
7456e4b17023SJohn Marino /* Construct the final type of this enumeration. It is the same
7457e4b17023SJohn Marino as one of the integral types - the narrowest one that fits, except
7458e4b17023SJohn Marino that normally we only go as narrow as int - and signed iff any of
7459e4b17023SJohn Marino the values are negative. */
7460e4b17023SJohn Marino unsign = (tree_int_cst_sgn (minnode) >= 0);
7461e4b17023SJohn Marino precision = MAX (tree_int_cst_min_precision (minnode, unsign),
7462e4b17023SJohn Marino tree_int_cst_min_precision (maxnode, unsign));
7463e4b17023SJohn Marino
7464e4b17023SJohn Marino if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
7465e4b17023SJohn Marino {
7466e4b17023SJohn Marino tem = c_common_type_for_size (precision, unsign);
7467e4b17023SJohn Marino if (tem == NULL)
7468e4b17023SJohn Marino {
7469e4b17023SJohn Marino warning (0, "enumeration values exceed range of largest integer");
7470e4b17023SJohn Marino tem = long_long_integer_type_node;
7471e4b17023SJohn Marino }
7472e4b17023SJohn Marino }
7473e4b17023SJohn Marino else
7474e4b17023SJohn Marino tem = unsign ? unsigned_type_node : integer_type_node;
7475e4b17023SJohn Marino
7476e4b17023SJohn Marino TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
7477e4b17023SJohn Marino TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
7478e4b17023SJohn Marino TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
7479e4b17023SJohn Marino TYPE_SIZE (enumtype) = 0;
7480e4b17023SJohn Marino
7481e4b17023SJohn Marino /* If the precision of the type was specific with an attribute and it
7482e4b17023SJohn Marino was too small, give an error. Otherwise, use it. */
7483e4b17023SJohn Marino if (TYPE_PRECISION (enumtype))
7484e4b17023SJohn Marino {
7485e4b17023SJohn Marino if (precision > TYPE_PRECISION (enumtype))
7486e4b17023SJohn Marino error ("specified mode too small for enumeral values");
7487e4b17023SJohn Marino }
7488e4b17023SJohn Marino else
7489e4b17023SJohn Marino TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
7490e4b17023SJohn Marino
7491e4b17023SJohn Marino layout_type (enumtype);
7492e4b17023SJohn Marino
7493e4b17023SJohn Marino if (values != error_mark_node)
7494e4b17023SJohn Marino {
7495e4b17023SJohn Marino /* Change the type of the enumerators to be the enum type. We
7496e4b17023SJohn Marino need to do this irrespective of the size of the enum, for
7497e4b17023SJohn Marino proper type checking. Replace the DECL_INITIALs of the
7498e4b17023SJohn Marino enumerators, and the value slots of the list, with copies
7499e4b17023SJohn Marino that have the enum type; they cannot be modified in place
7500e4b17023SJohn Marino because they may be shared (e.g. integer_zero_node) Finally,
7501e4b17023SJohn Marino change the purpose slots to point to the names of the decls. */
7502e4b17023SJohn Marino for (pair = values; pair; pair = TREE_CHAIN (pair))
7503e4b17023SJohn Marino {
7504e4b17023SJohn Marino tree enu = TREE_PURPOSE (pair);
7505e4b17023SJohn Marino tree ini = DECL_INITIAL (enu);
7506e4b17023SJohn Marino
7507e4b17023SJohn Marino TREE_TYPE (enu) = enumtype;
7508e4b17023SJohn Marino
7509e4b17023SJohn Marino /* The ISO C Standard mandates enumerators to have type int,
7510e4b17023SJohn Marino even though the underlying type of an enum type is
7511e4b17023SJohn Marino unspecified. However, GCC allows enumerators of any
7512e4b17023SJohn Marino integer type as an extensions. build_enumerator()
7513e4b17023SJohn Marino converts any enumerators that fit in an int to type int,
7514e4b17023SJohn Marino to avoid promotions to unsigned types when comparing
7515e4b17023SJohn Marino integers with enumerators that fit in the int range.
7516e4b17023SJohn Marino When -pedantic is given, build_enumerator() would have
7517e4b17023SJohn Marino already warned about those that don't fit. Here we
7518e4b17023SJohn Marino convert the rest to the enumerator type. */
7519e4b17023SJohn Marino if (TREE_TYPE (ini) != integer_type_node)
7520e4b17023SJohn Marino ini = convert (enumtype, ini);
7521e4b17023SJohn Marino
7522e4b17023SJohn Marino DECL_INITIAL (enu) = ini;
7523e4b17023SJohn Marino TREE_PURPOSE (pair) = DECL_NAME (enu);
7524e4b17023SJohn Marino TREE_VALUE (pair) = ini;
7525e4b17023SJohn Marino }
7526e4b17023SJohn Marino
7527e4b17023SJohn Marino TYPE_VALUES (enumtype) = values;
7528e4b17023SJohn Marino }
7529e4b17023SJohn Marino
7530e4b17023SJohn Marino /* Record the min/max values so that we can warn about bit-field
7531e4b17023SJohn Marino enumerations that are too small for the values. */
7532e4b17023SJohn Marino lt = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
7533e4b17023SJohn Marino lt->enum_min = minnode;
7534e4b17023SJohn Marino lt->enum_max = maxnode;
7535e4b17023SJohn Marino TYPE_LANG_SPECIFIC (enumtype) = lt;
7536e4b17023SJohn Marino
7537e4b17023SJohn Marino /* Fix up all variant types of this enum type. */
7538e4b17023SJohn Marino for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
7539e4b17023SJohn Marino {
7540e4b17023SJohn Marino if (tem == enumtype)
7541e4b17023SJohn Marino continue;
7542e4b17023SJohn Marino TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
7543e4b17023SJohn Marino TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
7544e4b17023SJohn Marino TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
7545e4b17023SJohn Marino TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
7546e4b17023SJohn Marino TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
7547e4b17023SJohn Marino SET_TYPE_MODE (tem, TYPE_MODE (enumtype));
7548e4b17023SJohn Marino TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
7549e4b17023SJohn Marino TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
7550e4b17023SJohn Marino TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
7551e4b17023SJohn Marino TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
7552e4b17023SJohn Marino TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
7553e4b17023SJohn Marino }
7554e4b17023SJohn Marino
7555e4b17023SJohn Marino /* Finish debugging output for this type. */
7556e4b17023SJohn Marino rest_of_type_compilation (enumtype, toplevel);
7557e4b17023SJohn Marino
7558e4b17023SJohn Marino /* If this enum is defined inside a struct, add it to
7559e4b17023SJohn Marino struct_types. */
7560e4b17023SJohn Marino if (warn_cxx_compat
7561e4b17023SJohn Marino && struct_parse_info != NULL
7562e4b17023SJohn Marino && !in_sizeof && !in_typeof && !in_alignof)
7563e4b17023SJohn Marino VEC_safe_push (tree, heap, struct_parse_info->struct_types, enumtype);
7564e4b17023SJohn Marino
7565e4b17023SJohn Marino return enumtype;
7566e4b17023SJohn Marino }
7567e4b17023SJohn Marino
7568e4b17023SJohn Marino /* Build and install a CONST_DECL for one value of the
7569e4b17023SJohn Marino current enumeration type (one that was begun with start_enum).
7570e4b17023SJohn Marino DECL_LOC is the location of the enumerator.
7571e4b17023SJohn Marino LOC is the location of the '=' operator if any, DECL_LOC otherwise.
7572e4b17023SJohn Marino Return a tree-list containing the CONST_DECL and its value.
7573e4b17023SJohn Marino Assignment of sequential values by default is handled here. */
7574e4b17023SJohn Marino
7575e4b17023SJohn Marino tree
build_enumerator(location_t decl_loc,location_t loc,struct c_enum_contents * the_enum,tree name,tree value)7576e4b17023SJohn Marino build_enumerator (location_t decl_loc, location_t loc,
7577e4b17023SJohn Marino struct c_enum_contents *the_enum, tree name, tree value)
7578e4b17023SJohn Marino {
7579e4b17023SJohn Marino tree decl, type;
7580e4b17023SJohn Marino
7581e4b17023SJohn Marino /* Validate and default VALUE. */
7582e4b17023SJohn Marino
7583e4b17023SJohn Marino if (value != 0)
7584e4b17023SJohn Marino {
7585e4b17023SJohn Marino /* Don't issue more errors for error_mark_node (i.e. an
7586e4b17023SJohn Marino undeclared identifier) - just ignore the value expression. */
7587e4b17023SJohn Marino if (value == error_mark_node)
7588e4b17023SJohn Marino value = 0;
7589e4b17023SJohn Marino else if (!INTEGRAL_TYPE_P (TREE_TYPE (value)))
7590e4b17023SJohn Marino {
7591e4b17023SJohn Marino error_at (loc, "enumerator value for %qE is not an integer constant",
7592e4b17023SJohn Marino name);
7593e4b17023SJohn Marino value = 0;
7594e4b17023SJohn Marino }
7595e4b17023SJohn Marino else
7596e4b17023SJohn Marino {
7597e4b17023SJohn Marino if (TREE_CODE (value) != INTEGER_CST)
7598e4b17023SJohn Marino {
7599e4b17023SJohn Marino value = c_fully_fold (value, false, NULL);
7600e4b17023SJohn Marino if (TREE_CODE (value) == INTEGER_CST)
7601e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
7602e4b17023SJohn Marino "enumerator value for %qE is not an integer "
7603e4b17023SJohn Marino "constant expression", name);
7604e4b17023SJohn Marino }
7605e4b17023SJohn Marino if (TREE_CODE (value) != INTEGER_CST)
7606e4b17023SJohn Marino {
7607e4b17023SJohn Marino error ("enumerator value for %qE is not an integer constant",
7608e4b17023SJohn Marino name);
7609e4b17023SJohn Marino value = 0;
7610e4b17023SJohn Marino }
7611e4b17023SJohn Marino else
7612e4b17023SJohn Marino {
7613e4b17023SJohn Marino value = default_conversion (value);
7614e4b17023SJohn Marino constant_expression_warning (value);
7615e4b17023SJohn Marino }
7616e4b17023SJohn Marino }
7617e4b17023SJohn Marino }
7618e4b17023SJohn Marino
7619e4b17023SJohn Marino /* Default based on previous value. */
7620e4b17023SJohn Marino /* It should no longer be possible to have NON_LVALUE_EXPR
7621e4b17023SJohn Marino in the default. */
7622e4b17023SJohn Marino if (value == 0)
7623e4b17023SJohn Marino {
7624e4b17023SJohn Marino value = the_enum->enum_next_value;
7625e4b17023SJohn Marino if (the_enum->enum_overflow)
7626e4b17023SJohn Marino error_at (loc, "overflow in enumeration values");
7627e4b17023SJohn Marino }
7628e4b17023SJohn Marino /* Even though the underlying type of an enum is unspecified, the
7629e4b17023SJohn Marino type of enumeration constants is explicitly defined as int
7630e4b17023SJohn Marino (6.4.4.3/2 in the C99 Standard). GCC allows any integer type as
7631e4b17023SJohn Marino an extension. */
7632e4b17023SJohn Marino else if (!int_fits_type_p (value, integer_type_node))
7633e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
7634e4b17023SJohn Marino "ISO C restricts enumerator values to range of %<int%>");
7635e4b17023SJohn Marino
7636e4b17023SJohn Marino /* The ISO C Standard mandates enumerators to have type int, even
7637e4b17023SJohn Marino though the underlying type of an enum type is unspecified.
7638e4b17023SJohn Marino However, GCC allows enumerators of any integer type as an
7639e4b17023SJohn Marino extensions. Here we convert any enumerators that fit in an int
7640e4b17023SJohn Marino to type int, to avoid promotions to unsigned types when comparing
7641e4b17023SJohn Marino integers with enumerators that fit in the int range. When
7642e4b17023SJohn Marino -pedantic is given, we would have already warned about those that
7643e4b17023SJohn Marino don't fit. We have to do this here rather than in finish_enum
7644e4b17023SJohn Marino because this value may be used to define more enumerators. */
7645e4b17023SJohn Marino if (int_fits_type_p (value, integer_type_node))
7646e4b17023SJohn Marino value = convert (integer_type_node, value);
7647e4b17023SJohn Marino
7648e4b17023SJohn Marino /* Set basis for default for next value. */
7649e4b17023SJohn Marino the_enum->enum_next_value
7650e4b17023SJohn Marino = build_binary_op (EXPR_LOC_OR_HERE (value),
7651e4b17023SJohn Marino PLUS_EXPR, value, integer_one_node, 0);
7652e4b17023SJohn Marino the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
7653e4b17023SJohn Marino
7654e4b17023SJohn Marino /* Now create a declaration for the enum value name. */
7655e4b17023SJohn Marino
7656e4b17023SJohn Marino type = TREE_TYPE (value);
7657e4b17023SJohn Marino type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
7658e4b17023SJohn Marino TYPE_PRECISION (integer_type_node)),
7659e4b17023SJohn Marino (TYPE_PRECISION (type)
7660e4b17023SJohn Marino >= TYPE_PRECISION (integer_type_node)
7661e4b17023SJohn Marino && TYPE_UNSIGNED (type)));
7662e4b17023SJohn Marino
7663e4b17023SJohn Marino decl = build_decl (decl_loc, CONST_DECL, name, type);
7664e4b17023SJohn Marino DECL_INITIAL (decl) = convert (type, value);
7665e4b17023SJohn Marino pushdecl (decl);
7666e4b17023SJohn Marino
7667e4b17023SJohn Marino return tree_cons (decl, value, NULL_TREE);
7668e4b17023SJohn Marino }
7669e4b17023SJohn Marino
7670e4b17023SJohn Marino
7671e4b17023SJohn Marino /* Create the FUNCTION_DECL for a function definition.
7672e4b17023SJohn Marino DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
7673e4b17023SJohn Marino the declaration; they describe the function's name and the type it returns,
7674e4b17023SJohn Marino but twisted together in a fashion that parallels the syntax of C.
7675e4b17023SJohn Marino
7676e4b17023SJohn Marino This function creates a binding context for the function body
7677e4b17023SJohn Marino as well as setting up the FUNCTION_DECL in current_function_decl.
7678e4b17023SJohn Marino
7679e4b17023SJohn Marino Returns 1 on success. If the DECLARATOR is not suitable for a function
7680e4b17023SJohn Marino (it defines a datum instead), we return 0, which tells
7681e4b17023SJohn Marino yyparse to report a parse error. */
7682e4b17023SJohn Marino
7683e4b17023SJohn Marino int
start_function(struct c_declspecs * declspecs,struct c_declarator * declarator,tree attributes)7684e4b17023SJohn Marino start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
7685e4b17023SJohn Marino tree attributes)
7686e4b17023SJohn Marino {
7687e4b17023SJohn Marino tree decl1, old_decl;
7688e4b17023SJohn Marino tree restype, resdecl;
7689e4b17023SJohn Marino location_t loc;
7690e4b17023SJohn Marino
7691e4b17023SJohn Marino current_function_returns_value = 0; /* Assume, until we see it does. */
7692e4b17023SJohn Marino current_function_returns_null = 0;
7693e4b17023SJohn Marino current_function_returns_abnormally = 0;
7694e4b17023SJohn Marino warn_about_return_type = 0;
7695e4b17023SJohn Marino c_switch_stack = NULL;
7696e4b17023SJohn Marino
7697e4b17023SJohn Marino /* Indicate no valid break/continue context by setting these variables
7698e4b17023SJohn Marino to some non-null, non-label value. We'll notice and emit the proper
7699e4b17023SJohn Marino error message in c_finish_bc_stmt. */
7700e4b17023SJohn Marino c_break_label = c_cont_label = size_zero_node;
7701e4b17023SJohn Marino
7702e4b17023SJohn Marino decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
7703e4b17023SJohn Marino &attributes, NULL, NULL, DEPRECATED_NORMAL);
7704e4b17023SJohn Marino
7705e4b17023SJohn Marino /* If the declarator is not suitable for a function definition,
7706e4b17023SJohn Marino cause a syntax error. */
7707e4b17023SJohn Marino if (decl1 == 0
7708e4b17023SJohn Marino || TREE_CODE (decl1) != FUNCTION_DECL)
7709e4b17023SJohn Marino return 0;
7710e4b17023SJohn Marino
7711e4b17023SJohn Marino loc = DECL_SOURCE_LOCATION (decl1);
7712e4b17023SJohn Marino
7713e4b17023SJohn Marino decl_attributes (&decl1, attributes, 0);
7714e4b17023SJohn Marino
7715e4b17023SJohn Marino if (DECL_DECLARED_INLINE_P (decl1)
7716e4b17023SJohn Marino && DECL_UNINLINABLE (decl1)
7717e4b17023SJohn Marino && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
7718e4b17023SJohn Marino warning_at (loc, OPT_Wattributes,
7719e4b17023SJohn Marino "inline function %qD given attribute noinline",
7720e4b17023SJohn Marino decl1);
7721e4b17023SJohn Marino
7722e4b17023SJohn Marino /* Handle gnu_inline attribute. */
7723e4b17023SJohn Marino if (declspecs->inline_p
7724e4b17023SJohn Marino && !flag_gnu89_inline
7725e4b17023SJohn Marino && TREE_CODE (decl1) == FUNCTION_DECL
7726e4b17023SJohn Marino && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
7727e4b17023SJohn Marino || current_function_decl))
7728e4b17023SJohn Marino {
7729e4b17023SJohn Marino if (declspecs->storage_class != csc_static)
7730e4b17023SJohn Marino DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
7731e4b17023SJohn Marino }
7732e4b17023SJohn Marino
7733e4b17023SJohn Marino announce_function (decl1);
7734e4b17023SJohn Marino
7735e4b17023SJohn Marino if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
7736e4b17023SJohn Marino {
7737e4b17023SJohn Marino error_at (loc, "return type is an incomplete type");
7738e4b17023SJohn Marino /* Make it return void instead. */
7739e4b17023SJohn Marino TREE_TYPE (decl1)
7740e4b17023SJohn Marino = build_function_type (void_type_node,
7741e4b17023SJohn Marino TYPE_ARG_TYPES (TREE_TYPE (decl1)));
7742e4b17023SJohn Marino }
7743e4b17023SJohn Marino
7744e4b17023SJohn Marino if (warn_about_return_type)
7745e4b17023SJohn Marino pedwarn_c99 (loc, flag_isoc99 ? 0
7746e4b17023SJohn Marino : (warn_return_type ? OPT_Wreturn_type : OPT_Wimplicit_int),
7747e4b17023SJohn Marino "return type defaults to %<int%>");
7748e4b17023SJohn Marino
7749e4b17023SJohn Marino /* Make the init_value nonzero so pushdecl knows this is not tentative.
7750e4b17023SJohn Marino error_mark_node is replaced below (in pop_scope) with the BLOCK. */
7751e4b17023SJohn Marino DECL_INITIAL (decl1) = error_mark_node;
7752e4b17023SJohn Marino
7753e4b17023SJohn Marino /* A nested function is not global. */
7754e4b17023SJohn Marino if (current_function_decl != 0)
7755e4b17023SJohn Marino TREE_PUBLIC (decl1) = 0;
7756e4b17023SJohn Marino
7757e4b17023SJohn Marino /* If this definition isn't a prototype and we had a prototype declaration
7758e4b17023SJohn Marino before, copy the arg type info from that prototype. */
7759e4b17023SJohn Marino old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
7760e4b17023SJohn Marino if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
7761e4b17023SJohn Marino old_decl = 0;
7762e4b17023SJohn Marino current_function_prototype_locus = UNKNOWN_LOCATION;
7763e4b17023SJohn Marino current_function_prototype_built_in = false;
7764e4b17023SJohn Marino current_function_prototype_arg_types = NULL_TREE;
7765e4b17023SJohn Marino if (!prototype_p (TREE_TYPE (decl1)))
7766e4b17023SJohn Marino {
7767e4b17023SJohn Marino if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
7768e4b17023SJohn Marino && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7769e4b17023SJohn Marino TREE_TYPE (TREE_TYPE (old_decl))))
7770e4b17023SJohn Marino {
7771e4b17023SJohn Marino TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
7772e4b17023SJohn Marino TREE_TYPE (decl1));
7773e4b17023SJohn Marino current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
7774e4b17023SJohn Marino current_function_prototype_built_in
7775e4b17023SJohn Marino = C_DECL_BUILTIN_PROTOTYPE (old_decl);
7776e4b17023SJohn Marino current_function_prototype_arg_types
7777e4b17023SJohn Marino = TYPE_ARG_TYPES (TREE_TYPE (decl1));
7778e4b17023SJohn Marino }
7779e4b17023SJohn Marino if (TREE_PUBLIC (decl1))
7780e4b17023SJohn Marino {
7781e4b17023SJohn Marino /* If there is an external prototype declaration of this
7782e4b17023SJohn Marino function, record its location but do not copy information
7783e4b17023SJohn Marino to this decl. This may be an invisible declaration
7784e4b17023SJohn Marino (built-in or in a scope which has finished) or simply
7785e4b17023SJohn Marino have more refined argument types than any declaration
7786e4b17023SJohn Marino found above. */
7787e4b17023SJohn Marino struct c_binding *b;
7788e4b17023SJohn Marino for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
7789e4b17023SJohn Marino if (B_IN_SCOPE (b, external_scope))
7790e4b17023SJohn Marino break;
7791e4b17023SJohn Marino if (b)
7792e4b17023SJohn Marino {
7793e4b17023SJohn Marino tree ext_decl, ext_type;
7794e4b17023SJohn Marino ext_decl = b->decl;
7795e4b17023SJohn Marino ext_type = b->u.type ? b->u.type : TREE_TYPE (ext_decl);
7796e4b17023SJohn Marino if (TREE_CODE (ext_type) == FUNCTION_TYPE
7797e4b17023SJohn Marino && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
7798e4b17023SJohn Marino TREE_TYPE (ext_type)))
7799e4b17023SJohn Marino {
7800e4b17023SJohn Marino current_function_prototype_locus
7801e4b17023SJohn Marino = DECL_SOURCE_LOCATION (ext_decl);
7802e4b17023SJohn Marino current_function_prototype_built_in
7803e4b17023SJohn Marino = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
7804e4b17023SJohn Marino current_function_prototype_arg_types
7805e4b17023SJohn Marino = TYPE_ARG_TYPES (ext_type);
7806e4b17023SJohn Marino }
7807e4b17023SJohn Marino }
7808e4b17023SJohn Marino }
7809e4b17023SJohn Marino }
7810e4b17023SJohn Marino
7811e4b17023SJohn Marino /* Optionally warn of old-fashioned def with no previous prototype. */
7812e4b17023SJohn Marino if (warn_strict_prototypes
7813e4b17023SJohn Marino && old_decl != error_mark_node
7814e4b17023SJohn Marino && !prototype_p (TREE_TYPE (decl1))
7815e4b17023SJohn Marino && C_DECL_ISNT_PROTOTYPE (old_decl))
7816e4b17023SJohn Marino warning_at (loc, OPT_Wstrict_prototypes,
7817e4b17023SJohn Marino "function declaration isn%'t a prototype");
7818e4b17023SJohn Marino /* Optionally warn of any global def with no previous prototype. */
7819e4b17023SJohn Marino else if (warn_missing_prototypes
7820e4b17023SJohn Marino && old_decl != error_mark_node
7821e4b17023SJohn Marino && TREE_PUBLIC (decl1)
7822e4b17023SJohn Marino && !MAIN_NAME_P (DECL_NAME (decl1))
7823e4b17023SJohn Marino && C_DECL_ISNT_PROTOTYPE (old_decl))
7824e4b17023SJohn Marino warning_at (loc, OPT_Wmissing_prototypes,
7825e4b17023SJohn Marino "no previous prototype for %qD", decl1);
7826e4b17023SJohn Marino /* Optionally warn of any def with no previous prototype
7827e4b17023SJohn Marino if the function has already been used. */
7828e4b17023SJohn Marino else if (warn_missing_prototypes
7829e4b17023SJohn Marino && old_decl != 0
7830e4b17023SJohn Marino && old_decl != error_mark_node
7831e4b17023SJohn Marino && TREE_USED (old_decl)
7832e4b17023SJohn Marino && !prototype_p (TREE_TYPE (old_decl)))
7833e4b17023SJohn Marino warning_at (loc, OPT_Wmissing_prototypes,
7834e4b17023SJohn Marino "%qD was used with no prototype before its definition", decl1);
7835e4b17023SJohn Marino /* Optionally warn of any global def with no previous declaration. */
7836e4b17023SJohn Marino else if (warn_missing_declarations
7837e4b17023SJohn Marino && TREE_PUBLIC (decl1)
7838e4b17023SJohn Marino && old_decl == 0
7839e4b17023SJohn Marino && !MAIN_NAME_P (DECL_NAME (decl1)))
7840e4b17023SJohn Marino warning_at (loc, OPT_Wmissing_declarations,
7841e4b17023SJohn Marino "no previous declaration for %qD",
7842e4b17023SJohn Marino decl1);
7843e4b17023SJohn Marino /* Optionally warn of any def with no previous declaration
7844e4b17023SJohn Marino if the function has already been used. */
7845e4b17023SJohn Marino else if (warn_missing_declarations
7846e4b17023SJohn Marino && old_decl != 0
7847e4b17023SJohn Marino && old_decl != error_mark_node
7848e4b17023SJohn Marino && TREE_USED (old_decl)
7849e4b17023SJohn Marino && C_DECL_IMPLICIT (old_decl))
7850e4b17023SJohn Marino warning_at (loc, OPT_Wmissing_declarations,
7851e4b17023SJohn Marino "%qD was used with no declaration before its definition", decl1);
7852e4b17023SJohn Marino
7853e4b17023SJohn Marino /* This function exists in static storage.
7854e4b17023SJohn Marino (This does not mean `static' in the C sense!) */
7855e4b17023SJohn Marino TREE_STATIC (decl1) = 1;
7856e4b17023SJohn Marino
7857e4b17023SJohn Marino /* This is the earliest point at which we might know the assembler
7858e4b17023SJohn Marino name of the function. Thus, if it's set before this, die horribly. */
7859e4b17023SJohn Marino gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
7860e4b17023SJohn Marino
7861e4b17023SJohn Marino /* If #pragma weak was used, mark the decl weak now. */
7862e4b17023SJohn Marino if (current_scope == file_scope)
7863e4b17023SJohn Marino maybe_apply_pragma_weak (decl1);
7864e4b17023SJohn Marino
7865e4b17023SJohn Marino /* Warn for unlikely, improbable, or stupid declarations of `main'. */
7866e4b17023SJohn Marino if (warn_main && MAIN_NAME_P (DECL_NAME (decl1)))
7867e4b17023SJohn Marino {
7868e4b17023SJohn Marino if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
7869e4b17023SJohn Marino != integer_type_node)
7870e4b17023SJohn Marino pedwarn (loc, OPT_Wmain, "return type of %qD is not %<int%>", decl1);
7871e4b17023SJohn Marino
7872e4b17023SJohn Marino check_main_parameter_types (decl1);
7873e4b17023SJohn Marino
7874e4b17023SJohn Marino if (!TREE_PUBLIC (decl1))
7875e4b17023SJohn Marino pedwarn (loc, OPT_Wmain,
7876e4b17023SJohn Marino "%qD is normally a non-static function", decl1);
7877e4b17023SJohn Marino }
7878e4b17023SJohn Marino
7879e4b17023SJohn Marino /* Record the decl so that the function name is defined.
7880e4b17023SJohn Marino If we already have a decl for this name, and it is a FUNCTION_DECL,
7881e4b17023SJohn Marino use the old decl. */
7882e4b17023SJohn Marino
7883e4b17023SJohn Marino current_function_decl = pushdecl (decl1);
7884e4b17023SJohn Marino
7885e4b17023SJohn Marino push_scope ();
7886e4b17023SJohn Marino declare_parm_level ();
7887e4b17023SJohn Marino
7888e4b17023SJohn Marino restype = TREE_TYPE (TREE_TYPE (current_function_decl));
7889e4b17023SJohn Marino resdecl = build_decl (loc, RESULT_DECL, NULL_TREE, restype);
7890e4b17023SJohn Marino DECL_ARTIFICIAL (resdecl) = 1;
7891e4b17023SJohn Marino DECL_IGNORED_P (resdecl) = 1;
7892e4b17023SJohn Marino DECL_RESULT (current_function_decl) = resdecl;
7893e4b17023SJohn Marino
7894e4b17023SJohn Marino start_fname_decls ();
7895e4b17023SJohn Marino
7896e4b17023SJohn Marino return 1;
7897e4b17023SJohn Marino }
7898e4b17023SJohn Marino
7899e4b17023SJohn Marino /* Subroutine of store_parm_decls which handles new-style function
7900e4b17023SJohn Marino definitions (prototype format). The parms already have decls, so we
7901e4b17023SJohn Marino need only record them as in effect and complain if any redundant
7902e4b17023SJohn Marino old-style parm decls were written. */
7903e4b17023SJohn Marino static void
store_parm_decls_newstyle(tree fndecl,const struct c_arg_info * arg_info)7904e4b17023SJohn Marino store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
7905e4b17023SJohn Marino {
7906e4b17023SJohn Marino tree decl;
7907e4b17023SJohn Marino c_arg_tag *tag;
7908e4b17023SJohn Marino unsigned ix;
7909e4b17023SJohn Marino
7910e4b17023SJohn Marino if (current_scope->bindings)
7911e4b17023SJohn Marino {
7912e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (fndecl),
7913e4b17023SJohn Marino "old-style parameter declarations in prototyped "
7914e4b17023SJohn Marino "function definition");
7915e4b17023SJohn Marino
7916e4b17023SJohn Marino /* Get rid of the old-style declarations. */
7917e4b17023SJohn Marino pop_scope ();
7918e4b17023SJohn Marino push_scope ();
7919e4b17023SJohn Marino }
7920e4b17023SJohn Marino /* Don't issue this warning for nested functions, and don't issue this
7921e4b17023SJohn Marino warning if we got here because ARG_INFO_TYPES was error_mark_node
7922e4b17023SJohn Marino (this happens when a function definition has just an ellipsis in
7923e4b17023SJohn Marino its parameter list). */
7924e4b17023SJohn Marino else if (!in_system_header && !current_function_scope
7925e4b17023SJohn Marino && arg_info->types != error_mark_node)
7926e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (fndecl), OPT_Wtraditional,
7927e4b17023SJohn Marino "traditional C rejects ISO C style function definitions");
7928e4b17023SJohn Marino
7929e4b17023SJohn Marino /* Now make all the parameter declarations visible in the function body.
7930e4b17023SJohn Marino We can bypass most of the grunt work of pushdecl. */
7931e4b17023SJohn Marino for (decl = arg_info->parms; decl; decl = DECL_CHAIN (decl))
7932e4b17023SJohn Marino {
7933e4b17023SJohn Marino DECL_CONTEXT (decl) = current_function_decl;
7934e4b17023SJohn Marino if (DECL_NAME (decl))
7935e4b17023SJohn Marino {
7936e4b17023SJohn Marino bind (DECL_NAME (decl), decl, current_scope,
7937e4b17023SJohn Marino /*invisible=*/false, /*nested=*/false,
7938e4b17023SJohn Marino UNKNOWN_LOCATION);
7939e4b17023SJohn Marino if (!TREE_USED (decl))
7940e4b17023SJohn Marino warn_if_shadowing (decl);
7941e4b17023SJohn Marino }
7942e4b17023SJohn Marino else
7943e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (decl), "parameter name omitted");
7944e4b17023SJohn Marino }
7945e4b17023SJohn Marino
7946e4b17023SJohn Marino /* Record the parameter list in the function declaration. */
7947e4b17023SJohn Marino DECL_ARGUMENTS (fndecl) = arg_info->parms;
7948e4b17023SJohn Marino
7949e4b17023SJohn Marino /* Now make all the ancillary declarations visible, likewise. */
7950e4b17023SJohn Marino for (decl = arg_info->others; decl; decl = DECL_CHAIN (decl))
7951e4b17023SJohn Marino {
7952e4b17023SJohn Marino DECL_CONTEXT (decl) = current_function_decl;
7953e4b17023SJohn Marino if (DECL_NAME (decl))
7954e4b17023SJohn Marino bind (DECL_NAME (decl), decl, current_scope,
7955e4b17023SJohn Marino /*invisible=*/false,
7956e4b17023SJohn Marino /*nested=*/(TREE_CODE (decl) == FUNCTION_DECL),
7957e4b17023SJohn Marino UNKNOWN_LOCATION);
7958e4b17023SJohn Marino }
7959e4b17023SJohn Marino
7960e4b17023SJohn Marino /* And all the tag declarations. */
7961e4b17023SJohn Marino FOR_EACH_VEC_ELT_REVERSE (c_arg_tag, arg_info->tags, ix, tag)
7962e4b17023SJohn Marino if (tag->id)
7963e4b17023SJohn Marino bind (tag->id, tag->type, current_scope,
7964e4b17023SJohn Marino /*invisible=*/false, /*nested=*/false, UNKNOWN_LOCATION);
7965e4b17023SJohn Marino }
7966e4b17023SJohn Marino
7967e4b17023SJohn Marino /* Subroutine of store_parm_decls which handles old-style function
7968e4b17023SJohn Marino definitions (separate parameter list and declarations). */
7969e4b17023SJohn Marino
7970e4b17023SJohn Marino static void
store_parm_decls_oldstyle(tree fndecl,const struct c_arg_info * arg_info)7971e4b17023SJohn Marino store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
7972e4b17023SJohn Marino {
7973e4b17023SJohn Marino struct c_binding *b;
7974e4b17023SJohn Marino tree parm, decl, last;
7975e4b17023SJohn Marino tree parmids = arg_info->parms;
7976e4b17023SJohn Marino struct pointer_set_t *seen_args = pointer_set_create ();
7977e4b17023SJohn Marino
7978e4b17023SJohn Marino if (!in_system_header)
7979e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (fndecl),
7980e4b17023SJohn Marino OPT_Wold_style_definition, "old-style function definition");
7981e4b17023SJohn Marino
7982e4b17023SJohn Marino /* Match each formal parameter name with its declaration. Save each
7983e4b17023SJohn Marino decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
7984e4b17023SJohn Marino for (parm = parmids; parm; parm = TREE_CHAIN (parm))
7985e4b17023SJohn Marino {
7986e4b17023SJohn Marino if (TREE_VALUE (parm) == 0)
7987e4b17023SJohn Marino {
7988e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (fndecl),
7989e4b17023SJohn Marino "parameter name missing from parameter list");
7990e4b17023SJohn Marino TREE_PURPOSE (parm) = 0;
7991e4b17023SJohn Marino continue;
7992e4b17023SJohn Marino }
7993e4b17023SJohn Marino
7994e4b17023SJohn Marino b = I_SYMBOL_BINDING (TREE_VALUE (parm));
7995e4b17023SJohn Marino if (b && B_IN_CURRENT_SCOPE (b))
7996e4b17023SJohn Marino {
7997e4b17023SJohn Marino decl = b->decl;
7998e4b17023SJohn Marino /* Skip erroneous parameters. */
7999e4b17023SJohn Marino if (decl == error_mark_node)
8000e4b17023SJohn Marino continue;
8001e4b17023SJohn Marino /* If we got something other than a PARM_DECL it is an error. */
8002e4b17023SJohn Marino if (TREE_CODE (decl) != PARM_DECL)
8003e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (decl),
8004e4b17023SJohn Marino "%qD declared as a non-parameter", decl);
8005e4b17023SJohn Marino /* If the declaration is already marked, we have a duplicate
8006e4b17023SJohn Marino name. Complain and ignore the duplicate. */
8007e4b17023SJohn Marino else if (pointer_set_contains (seen_args, decl))
8008e4b17023SJohn Marino {
8009e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (decl),
8010e4b17023SJohn Marino "multiple parameters named %qD", decl);
8011e4b17023SJohn Marino TREE_PURPOSE (parm) = 0;
8012e4b17023SJohn Marino continue;
8013e4b17023SJohn Marino }
8014e4b17023SJohn Marino /* If the declaration says "void", complain and turn it into
8015e4b17023SJohn Marino an int. */
8016e4b17023SJohn Marino else if (VOID_TYPE_P (TREE_TYPE (decl)))
8017e4b17023SJohn Marino {
8018e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (decl),
8019e4b17023SJohn Marino "parameter %qD declared with void type", decl);
8020e4b17023SJohn Marino TREE_TYPE (decl) = integer_type_node;
8021e4b17023SJohn Marino DECL_ARG_TYPE (decl) = integer_type_node;
8022e4b17023SJohn Marino layout_decl (decl, 0);
8023e4b17023SJohn Marino }
8024e4b17023SJohn Marino warn_if_shadowing (decl);
8025e4b17023SJohn Marino }
8026e4b17023SJohn Marino /* If no declaration found, default to int. */
8027e4b17023SJohn Marino else
8028e4b17023SJohn Marino {
8029e4b17023SJohn Marino /* FIXME diagnostics: This should be the location of the argument,
8030e4b17023SJohn Marino not the FNDECL. E.g., for an old-style declaration
8031e4b17023SJohn Marino
8032e4b17023SJohn Marino int f10(v) { blah; }
8033e4b17023SJohn Marino
8034e4b17023SJohn Marino We should use the location of the V, not the F10.
8035e4b17023SJohn Marino Unfortunately, the V is an IDENTIFIER_NODE which has no
8036e4b17023SJohn Marino location. In the future we need locations for c_arg_info
8037e4b17023SJohn Marino entries.
8038e4b17023SJohn Marino
8039e4b17023SJohn Marino See gcc.dg/Wshadow-3.c for an example of this problem. */
8040e4b17023SJohn Marino decl = build_decl (DECL_SOURCE_LOCATION (fndecl),
8041e4b17023SJohn Marino PARM_DECL, TREE_VALUE (parm), integer_type_node);
8042e4b17023SJohn Marino DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
8043e4b17023SJohn Marino pushdecl (decl);
8044e4b17023SJohn Marino warn_if_shadowing (decl);
8045e4b17023SJohn Marino
8046e4b17023SJohn Marino if (flag_isoc99)
8047e4b17023SJohn Marino pedwarn (DECL_SOURCE_LOCATION (decl),
8048e4b17023SJohn Marino 0, "type of %qD defaults to %<int%>", decl);
8049e4b17023SJohn Marino else
8050e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (decl),
8051e4b17023SJohn Marino OPT_Wmissing_parameter_type,
8052e4b17023SJohn Marino "type of %qD defaults to %<int%>", decl);
8053e4b17023SJohn Marino }
8054e4b17023SJohn Marino
8055e4b17023SJohn Marino TREE_PURPOSE (parm) = decl;
8056e4b17023SJohn Marino pointer_set_insert (seen_args, decl);
8057e4b17023SJohn Marino }
8058e4b17023SJohn Marino
8059e4b17023SJohn Marino /* Now examine the parms chain for incomplete declarations
8060e4b17023SJohn Marino and declarations with no corresponding names. */
8061e4b17023SJohn Marino
8062e4b17023SJohn Marino for (b = current_scope->bindings; b; b = b->prev)
8063e4b17023SJohn Marino {
8064e4b17023SJohn Marino parm = b->decl;
8065e4b17023SJohn Marino if (TREE_CODE (parm) != PARM_DECL)
8066e4b17023SJohn Marino continue;
8067e4b17023SJohn Marino
8068e4b17023SJohn Marino if (TREE_TYPE (parm) != error_mark_node
8069e4b17023SJohn Marino && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
8070e4b17023SJohn Marino {
8071e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (parm),
8072e4b17023SJohn Marino "parameter %qD has incomplete type", parm);
8073e4b17023SJohn Marino TREE_TYPE (parm) = error_mark_node;
8074e4b17023SJohn Marino }
8075e4b17023SJohn Marino
8076e4b17023SJohn Marino if (!pointer_set_contains (seen_args, parm))
8077e4b17023SJohn Marino {
8078e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (parm),
8079e4b17023SJohn Marino "declaration for parameter %qD but no such parameter",
8080e4b17023SJohn Marino parm);
8081e4b17023SJohn Marino
8082e4b17023SJohn Marino /* Pretend the parameter was not missing.
8083e4b17023SJohn Marino This gets us to a standard state and minimizes
8084e4b17023SJohn Marino further error messages. */
8085e4b17023SJohn Marino parmids = chainon (parmids, tree_cons (parm, 0, 0));
8086e4b17023SJohn Marino }
8087e4b17023SJohn Marino }
8088e4b17023SJohn Marino
8089e4b17023SJohn Marino /* Chain the declarations together in the order of the list of
8090e4b17023SJohn Marino names. Store that chain in the function decl, replacing the
8091e4b17023SJohn Marino list of names. Update the current scope to match. */
8092e4b17023SJohn Marino DECL_ARGUMENTS (fndecl) = 0;
8093e4b17023SJohn Marino
8094e4b17023SJohn Marino for (parm = parmids; parm; parm = TREE_CHAIN (parm))
8095e4b17023SJohn Marino if (TREE_PURPOSE (parm))
8096e4b17023SJohn Marino break;
8097e4b17023SJohn Marino if (parm && TREE_PURPOSE (parm))
8098e4b17023SJohn Marino {
8099e4b17023SJohn Marino last = TREE_PURPOSE (parm);
8100e4b17023SJohn Marino DECL_ARGUMENTS (fndecl) = last;
8101e4b17023SJohn Marino
8102e4b17023SJohn Marino for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
8103e4b17023SJohn Marino if (TREE_PURPOSE (parm))
8104e4b17023SJohn Marino {
8105e4b17023SJohn Marino DECL_CHAIN (last) = TREE_PURPOSE (parm);
8106e4b17023SJohn Marino last = TREE_PURPOSE (parm);
8107e4b17023SJohn Marino }
8108e4b17023SJohn Marino DECL_CHAIN (last) = 0;
8109e4b17023SJohn Marino }
8110e4b17023SJohn Marino
8111e4b17023SJohn Marino pointer_set_destroy (seen_args);
8112e4b17023SJohn Marino
8113e4b17023SJohn Marino /* If there was a previous prototype,
8114e4b17023SJohn Marino set the DECL_ARG_TYPE of each argument according to
8115e4b17023SJohn Marino the type previously specified, and report any mismatches. */
8116e4b17023SJohn Marino
8117e4b17023SJohn Marino if (current_function_prototype_arg_types)
8118e4b17023SJohn Marino {
8119e4b17023SJohn Marino tree type;
8120e4b17023SJohn Marino for (parm = DECL_ARGUMENTS (fndecl),
8121e4b17023SJohn Marino type = current_function_prototype_arg_types;
8122e4b17023SJohn Marino parm || (type && TREE_VALUE (type) != error_mark_node
8123e4b17023SJohn Marino && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
8124e4b17023SJohn Marino parm = DECL_CHAIN (parm), type = TREE_CHAIN (type))
8125e4b17023SJohn Marino {
8126e4b17023SJohn Marino if (parm == 0 || type == 0
8127e4b17023SJohn Marino || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
8128e4b17023SJohn Marino {
8129e4b17023SJohn Marino if (current_function_prototype_built_in)
8130e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (fndecl),
8131e4b17023SJohn Marino 0, "number of arguments doesn%'t match "
8132e4b17023SJohn Marino "built-in prototype");
8133e4b17023SJohn Marino else
8134e4b17023SJohn Marino {
8135e4b17023SJohn Marino /* FIXME diagnostics: This should be the location of
8136e4b17023SJohn Marino FNDECL, but there is bug when a prototype is
8137e4b17023SJohn Marino declared inside function context, but defined
8138e4b17023SJohn Marino outside of it (e.g., gcc.dg/pr15698-2.c). In
8139e4b17023SJohn Marino which case FNDECL gets the location of the
8140e4b17023SJohn Marino prototype, not the definition. */
8141e4b17023SJohn Marino error_at (input_location,
8142e4b17023SJohn Marino "number of arguments doesn%'t match prototype");
8143e4b17023SJohn Marino
8144e4b17023SJohn Marino error_at (current_function_prototype_locus,
8145e4b17023SJohn Marino "prototype declaration");
8146e4b17023SJohn Marino }
8147e4b17023SJohn Marino break;
8148e4b17023SJohn Marino }
8149e4b17023SJohn Marino /* Type for passing arg must be consistent with that
8150e4b17023SJohn Marino declared for the arg. ISO C says we take the unqualified
8151e4b17023SJohn Marino type for parameters declared with qualified type. */
8152e4b17023SJohn Marino if (TREE_TYPE (parm) != error_mark_node
8153e4b17023SJohn Marino && TREE_TYPE (type) != error_mark_node
8154e4b17023SJohn Marino && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
8155e4b17023SJohn Marino TYPE_MAIN_VARIANT (TREE_VALUE (type))))
8156e4b17023SJohn Marino {
8157e4b17023SJohn Marino if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
8158e4b17023SJohn Marino == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
8159e4b17023SJohn Marino {
8160e4b17023SJohn Marino /* Adjust argument to match prototype. E.g. a previous
8161e4b17023SJohn Marino `int foo(float);' prototype causes
8162e4b17023SJohn Marino `int foo(x) float x; {...}' to be treated like
8163e4b17023SJohn Marino `int foo(float x) {...}'. This is particularly
8164e4b17023SJohn Marino useful for argument types like uid_t. */
8165e4b17023SJohn Marino DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
8166e4b17023SJohn Marino
8167e4b17023SJohn Marino if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
8168e4b17023SJohn Marino && INTEGRAL_TYPE_P (TREE_TYPE (parm))
8169e4b17023SJohn Marino && TYPE_PRECISION (TREE_TYPE (parm))
8170e4b17023SJohn Marino < TYPE_PRECISION (integer_type_node))
8171e4b17023SJohn Marino DECL_ARG_TYPE (parm) = integer_type_node;
8172e4b17023SJohn Marino
8173e4b17023SJohn Marino /* ??? Is it possible to get here with a
8174e4b17023SJohn Marino built-in prototype or will it always have
8175e4b17023SJohn Marino been diagnosed as conflicting with an
8176e4b17023SJohn Marino old-style definition and discarded? */
8177e4b17023SJohn Marino if (current_function_prototype_built_in)
8178e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (parm),
8179e4b17023SJohn Marino OPT_pedantic, "promoted argument %qD "
8180e4b17023SJohn Marino "doesn%'t match built-in prototype", parm);
8181e4b17023SJohn Marino else
8182e4b17023SJohn Marino {
8183e4b17023SJohn Marino pedwarn (DECL_SOURCE_LOCATION (parm),
8184e4b17023SJohn Marino OPT_pedantic, "promoted argument %qD "
8185e4b17023SJohn Marino "doesn%'t match prototype", parm);
8186e4b17023SJohn Marino pedwarn (current_function_prototype_locus, OPT_pedantic,
8187e4b17023SJohn Marino "prototype declaration");
8188e4b17023SJohn Marino }
8189e4b17023SJohn Marino }
8190e4b17023SJohn Marino else
8191e4b17023SJohn Marino {
8192e4b17023SJohn Marino if (current_function_prototype_built_in)
8193e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (parm),
8194e4b17023SJohn Marino 0, "argument %qD doesn%'t match "
8195e4b17023SJohn Marino "built-in prototype", parm);
8196e4b17023SJohn Marino else
8197e4b17023SJohn Marino {
8198e4b17023SJohn Marino error_at (DECL_SOURCE_LOCATION (parm),
8199e4b17023SJohn Marino "argument %qD doesn%'t match prototype", parm);
8200e4b17023SJohn Marino error_at (current_function_prototype_locus,
8201e4b17023SJohn Marino "prototype declaration");
8202e4b17023SJohn Marino }
8203e4b17023SJohn Marino }
8204e4b17023SJohn Marino }
8205e4b17023SJohn Marino }
8206e4b17023SJohn Marino TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
8207e4b17023SJohn Marino }
8208e4b17023SJohn Marino
8209e4b17023SJohn Marino /* Otherwise, create a prototype that would match. */
8210e4b17023SJohn Marino
8211e4b17023SJohn Marino else
8212e4b17023SJohn Marino {
8213e4b17023SJohn Marino tree actual = 0, last = 0, type;
8214e4b17023SJohn Marino
8215e4b17023SJohn Marino for (parm = DECL_ARGUMENTS (fndecl); parm; parm = DECL_CHAIN (parm))
8216e4b17023SJohn Marino {
8217e4b17023SJohn Marino type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
8218e4b17023SJohn Marino if (last)
8219e4b17023SJohn Marino TREE_CHAIN (last) = type;
8220e4b17023SJohn Marino else
8221e4b17023SJohn Marino actual = type;
8222e4b17023SJohn Marino last = type;
8223e4b17023SJohn Marino }
8224e4b17023SJohn Marino type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
8225e4b17023SJohn Marino if (last)
8226e4b17023SJohn Marino TREE_CHAIN (last) = type;
8227e4b17023SJohn Marino else
8228e4b17023SJohn Marino actual = type;
8229e4b17023SJohn Marino
8230e4b17023SJohn Marino /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
8231e4b17023SJohn Marino of the type of this function, but we need to avoid having this
8232e4b17023SJohn Marino affect the types of other similarly-typed functions, so we must
8233e4b17023SJohn Marino first force the generation of an identical (but separate) type
8234e4b17023SJohn Marino node for the relevant function type. The new node we create
8235e4b17023SJohn Marino will be a variant of the main variant of the original function
8236e4b17023SJohn Marino type. */
8237e4b17023SJohn Marino
8238e4b17023SJohn Marino TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
8239e4b17023SJohn Marino
8240e4b17023SJohn Marino TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
8241e4b17023SJohn Marino }
8242e4b17023SJohn Marino }
8243e4b17023SJohn Marino
8244e4b17023SJohn Marino /* Store parameter declarations passed in ARG_INFO into the current
8245e4b17023SJohn Marino function declaration. */
8246e4b17023SJohn Marino
8247e4b17023SJohn Marino void
store_parm_decls_from(struct c_arg_info * arg_info)8248e4b17023SJohn Marino store_parm_decls_from (struct c_arg_info *arg_info)
8249e4b17023SJohn Marino {
8250e4b17023SJohn Marino current_function_arg_info = arg_info;
8251e4b17023SJohn Marino store_parm_decls ();
8252e4b17023SJohn Marino }
8253e4b17023SJohn Marino
8254e4b17023SJohn Marino /* Store the parameter declarations into the current function declaration.
8255e4b17023SJohn Marino This is called after parsing the parameter declarations, before
8256e4b17023SJohn Marino digesting the body of the function.
8257e4b17023SJohn Marino
8258e4b17023SJohn Marino For an old-style definition, construct a prototype out of the old-style
8259e4b17023SJohn Marino parameter declarations and inject it into the function's type. */
8260e4b17023SJohn Marino
8261e4b17023SJohn Marino void
store_parm_decls(void)8262e4b17023SJohn Marino store_parm_decls (void)
8263e4b17023SJohn Marino {
8264e4b17023SJohn Marino tree fndecl = current_function_decl;
8265e4b17023SJohn Marino bool proto;
8266e4b17023SJohn Marino
8267e4b17023SJohn Marino /* The argument information block for FNDECL. */
8268e4b17023SJohn Marino struct c_arg_info *arg_info = current_function_arg_info;
8269e4b17023SJohn Marino current_function_arg_info = 0;
8270e4b17023SJohn Marino
8271e4b17023SJohn Marino /* True if this definition is written with a prototype. Note:
8272e4b17023SJohn Marino despite C99 6.7.5.3p14, we can *not* treat an empty argument
8273e4b17023SJohn Marino list in a function definition as equivalent to (void) -- an
8274e4b17023SJohn Marino empty argument list specifies the function has no parameters,
8275e4b17023SJohn Marino but only (void) sets up a prototype for future calls. */
8276e4b17023SJohn Marino proto = arg_info->types != 0;
8277e4b17023SJohn Marino
8278e4b17023SJohn Marino if (proto)
8279e4b17023SJohn Marino store_parm_decls_newstyle (fndecl, arg_info);
8280e4b17023SJohn Marino else
8281e4b17023SJohn Marino store_parm_decls_oldstyle (fndecl, arg_info);
8282e4b17023SJohn Marino
8283e4b17023SJohn Marino /* The next call to push_scope will be a function body. */
8284e4b17023SJohn Marino
8285e4b17023SJohn Marino next_is_function_body = true;
8286e4b17023SJohn Marino
8287e4b17023SJohn Marino /* Write a record describing this function definition to the prototypes
8288e4b17023SJohn Marino file (if requested). */
8289e4b17023SJohn Marino
8290e4b17023SJohn Marino gen_aux_info_record (fndecl, 1, 0, proto);
8291e4b17023SJohn Marino
8292e4b17023SJohn Marino /* Initialize the RTL code for the function. */
8293e4b17023SJohn Marino allocate_struct_function (fndecl, false);
8294e4b17023SJohn Marino
8295e4b17023SJohn Marino if (warn_unused_local_typedefs)
8296e4b17023SJohn Marino cfun->language = ggc_alloc_cleared_language_function ();
8297e4b17023SJohn Marino
8298e4b17023SJohn Marino /* Begin the statement tree for this function. */
8299e4b17023SJohn Marino DECL_SAVED_TREE (fndecl) = push_stmt_list ();
8300e4b17023SJohn Marino
8301e4b17023SJohn Marino /* ??? Insert the contents of the pending sizes list into the function
8302e4b17023SJohn Marino to be evaluated. The only reason left to have this is
8303e4b17023SJohn Marino void foo(int n, int array[n++])
8304e4b17023SJohn Marino because we throw away the array type in favor of a pointer type, and
8305e4b17023SJohn Marino thus won't naturally see the SAVE_EXPR containing the increment. All
8306e4b17023SJohn Marino other pending sizes would be handled by gimplify_parameters. */
8307e4b17023SJohn Marino if (arg_info->pending_sizes)
8308e4b17023SJohn Marino add_stmt (arg_info->pending_sizes);
8309e4b17023SJohn Marino }
8310e4b17023SJohn Marino
8311e4b17023SJohn Marino
8312e4b17023SJohn Marino /* Finish up a function declaration and compile that function
8313e4b17023SJohn Marino all the way to assembler language output. Then free the storage
8314e4b17023SJohn Marino for the function definition.
8315e4b17023SJohn Marino
8316e4b17023SJohn Marino This is called after parsing the body of the function definition. */
8317e4b17023SJohn Marino
8318e4b17023SJohn Marino void
finish_function(void)8319e4b17023SJohn Marino finish_function (void)
8320e4b17023SJohn Marino {
8321e4b17023SJohn Marino tree fndecl = current_function_decl;
8322e4b17023SJohn Marino
8323e4b17023SJohn Marino if (c_dialect_objc ())
8324e4b17023SJohn Marino objc_finish_function ();
8325e4b17023SJohn Marino
8326e4b17023SJohn Marino if (TREE_CODE (fndecl) == FUNCTION_DECL
8327e4b17023SJohn Marino && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
8328e4b17023SJohn Marino {
8329e4b17023SJohn Marino tree args = DECL_ARGUMENTS (fndecl);
8330e4b17023SJohn Marino for (; args; args = DECL_CHAIN (args))
8331e4b17023SJohn Marino {
8332e4b17023SJohn Marino tree type = TREE_TYPE (args);
8333e4b17023SJohn Marino if (INTEGRAL_TYPE_P (type)
8334e4b17023SJohn Marino && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8335e4b17023SJohn Marino DECL_ARG_TYPE (args) = integer_type_node;
8336e4b17023SJohn Marino }
8337e4b17023SJohn Marino }
8338e4b17023SJohn Marino
8339e4b17023SJohn Marino if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
8340e4b17023SJohn Marino BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
8341e4b17023SJohn Marino
8342e4b17023SJohn Marino /* Must mark the RESULT_DECL as being in this function. */
8343e4b17023SJohn Marino
8344e4b17023SJohn Marino if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
8345e4b17023SJohn Marino DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
8346e4b17023SJohn Marino
8347e4b17023SJohn Marino if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted
8348e4b17023SJohn Marino && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
8349e4b17023SJohn Marino == integer_type_node && flag_isoc99)
8350e4b17023SJohn Marino {
8351e4b17023SJohn Marino /* Hack. We don't want the middle-end to warn that this return
8352e4b17023SJohn Marino is unreachable, so we mark its location as special. Using
8353e4b17023SJohn Marino UNKNOWN_LOCATION has the problem that it gets clobbered in
8354e4b17023SJohn Marino annotate_one_with_locus. A cleaner solution might be to
8355e4b17023SJohn Marino ensure ! should_carry_locus_p (stmt), but that needs a flag.
8356e4b17023SJohn Marino */
8357e4b17023SJohn Marino c_finish_return (BUILTINS_LOCATION, integer_zero_node, NULL_TREE);
8358e4b17023SJohn Marino }
8359e4b17023SJohn Marino
8360e4b17023SJohn Marino /* Tie off the statement tree for this function. */
8361e4b17023SJohn Marino DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
8362e4b17023SJohn Marino
8363e4b17023SJohn Marino finish_fname_decls ();
8364e4b17023SJohn Marino
8365e4b17023SJohn Marino /* Complain if there's just no return statement. */
8366e4b17023SJohn Marino if (warn_return_type
8367e4b17023SJohn Marino && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
8368e4b17023SJohn Marino && !current_function_returns_value && !current_function_returns_null
8369e4b17023SJohn Marino /* Don't complain if we are no-return. */
8370e4b17023SJohn Marino && !current_function_returns_abnormally
8371e4b17023SJohn Marino /* Don't complain if we are declared noreturn. */
8372e4b17023SJohn Marino && !TREE_THIS_VOLATILE (fndecl)
8373e4b17023SJohn Marino /* Don't warn for main(). */
8374e4b17023SJohn Marino && !MAIN_NAME_P (DECL_NAME (fndecl))
8375e4b17023SJohn Marino /* Or if they didn't actually specify a return type. */
8376e4b17023SJohn Marino && !C_FUNCTION_IMPLICIT_INT (fndecl)
8377e4b17023SJohn Marino /* Normally, with -Wreturn-type, flow will complain, but we might
8378e4b17023SJohn Marino optimize out static functions. */
8379e4b17023SJohn Marino && !TREE_PUBLIC (fndecl))
8380e4b17023SJohn Marino {
8381e4b17023SJohn Marino warning (OPT_Wreturn_type,
8382e4b17023SJohn Marino "no return statement in function returning non-void");
8383e4b17023SJohn Marino TREE_NO_WARNING (fndecl) = 1;
8384e4b17023SJohn Marino }
8385e4b17023SJohn Marino
8386e4b17023SJohn Marino /* Complain about parameters that are only set, but never otherwise used. */
8387e4b17023SJohn Marino if (warn_unused_but_set_parameter)
8388e4b17023SJohn Marino {
8389e4b17023SJohn Marino tree decl;
8390e4b17023SJohn Marino
8391e4b17023SJohn Marino for (decl = DECL_ARGUMENTS (fndecl);
8392e4b17023SJohn Marino decl;
8393e4b17023SJohn Marino decl = DECL_CHAIN (decl))
8394e4b17023SJohn Marino if (TREE_USED (decl)
8395e4b17023SJohn Marino && TREE_CODE (decl) == PARM_DECL
8396e4b17023SJohn Marino && !DECL_READ_P (decl)
8397e4b17023SJohn Marino && DECL_NAME (decl)
8398e4b17023SJohn Marino && !DECL_ARTIFICIAL (decl)
8399e4b17023SJohn Marino && !TREE_NO_WARNING (decl))
8400e4b17023SJohn Marino warning_at (DECL_SOURCE_LOCATION (decl),
8401e4b17023SJohn Marino OPT_Wunused_but_set_parameter,
8402e4b17023SJohn Marino "parameter %qD set but not used", decl);
8403e4b17023SJohn Marino }
8404e4b17023SJohn Marino
8405e4b17023SJohn Marino /* Complain about locally defined typedefs that are not used in this
8406e4b17023SJohn Marino function. */
8407e4b17023SJohn Marino maybe_warn_unused_local_typedefs ();
8408e4b17023SJohn Marino
8409e4b17023SJohn Marino /* Store the end of the function, so that we get good line number
8410e4b17023SJohn Marino info for the epilogue. */
8411e4b17023SJohn Marino cfun->function_end_locus = input_location;
8412e4b17023SJohn Marino
8413e4b17023SJohn Marino /* Finalize the ELF visibility for the function. */
8414e4b17023SJohn Marino c_determine_visibility (fndecl);
8415e4b17023SJohn Marino
8416e4b17023SJohn Marino /* For GNU C extern inline functions disregard inline limits. */
8417e4b17023SJohn Marino if (DECL_EXTERNAL (fndecl)
8418e4b17023SJohn Marino && DECL_DECLARED_INLINE_P (fndecl))
8419e4b17023SJohn Marino DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
8420e4b17023SJohn Marino
8421e4b17023SJohn Marino /* Genericize before inlining. Delay genericizing nested functions
8422e4b17023SJohn Marino until their parent function is genericized. Since finalizing
8423e4b17023SJohn Marino requires GENERIC, delay that as well. */
8424e4b17023SJohn Marino
8425e4b17023SJohn Marino if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
8426e4b17023SJohn Marino && !undef_nested_function)
8427e4b17023SJohn Marino {
8428e4b17023SJohn Marino if (!decl_function_context (fndecl))
8429e4b17023SJohn Marino {
8430e4b17023SJohn Marino invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl);
8431e4b17023SJohn Marino c_genericize (fndecl);
8432e4b17023SJohn Marino
8433e4b17023SJohn Marino /* ??? Objc emits functions after finalizing the compilation unit.
8434e4b17023SJohn Marino This should be cleaned up later and this conditional removed. */
8435e4b17023SJohn Marino if (cgraph_global_info_ready)
8436e4b17023SJohn Marino {
8437e4b17023SJohn Marino cgraph_add_new_function (fndecl, false);
8438e4b17023SJohn Marino return;
8439e4b17023SJohn Marino }
8440e4b17023SJohn Marino cgraph_finalize_function (fndecl, false);
8441e4b17023SJohn Marino }
8442e4b17023SJohn Marino else
8443e4b17023SJohn Marino {
8444e4b17023SJohn Marino /* Register this function with cgraph just far enough to get it
8445e4b17023SJohn Marino added to our parent's nested function list. Handy, since the
8446e4b17023SJohn Marino C front end doesn't have such a list. */
8447e4b17023SJohn Marino (void) cgraph_get_create_node (fndecl);
8448e4b17023SJohn Marino }
8449e4b17023SJohn Marino }
8450e4b17023SJohn Marino
8451e4b17023SJohn Marino if (!decl_function_context (fndecl))
8452e4b17023SJohn Marino undef_nested_function = false;
8453e4b17023SJohn Marino
8454e4b17023SJohn Marino if (cfun->language != NULL)
8455e4b17023SJohn Marino {
8456e4b17023SJohn Marino ggc_free (cfun->language);
8457e4b17023SJohn Marino cfun->language = NULL;
8458e4b17023SJohn Marino }
8459e4b17023SJohn Marino
8460e4b17023SJohn Marino /* We're leaving the context of this function, so zap cfun.
8461e4b17023SJohn Marino It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
8462e4b17023SJohn Marino tree_rest_of_compilation. */
8463e4b17023SJohn Marino set_cfun (NULL);
8464e4b17023SJohn Marino current_function_decl = NULL;
8465e4b17023SJohn Marino }
8466e4b17023SJohn Marino
8467e4b17023SJohn Marino /* Check the declarations given in a for-loop for satisfying the C99
8468e4b17023SJohn Marino constraints. If exactly one such decl is found, return it. LOC is
8469e4b17023SJohn Marino the location of the opening parenthesis of the for loop. The last
8470e4b17023SJohn Marino parameter allows you to control the "for loop initial declarations
8471e4b17023SJohn Marino are only allowed in C99 mode". Normally, you should pass
8472e4b17023SJohn Marino flag_isoc99 as that parameter. But in some cases (Objective-C
8473e4b17023SJohn Marino foreach loop, for example) we want to run the checks in this
8474e4b17023SJohn Marino function even if not in C99 mode, so we allow the caller to turn
8475e4b17023SJohn Marino off the error about not being in C99 mode.
8476e4b17023SJohn Marino */
8477e4b17023SJohn Marino
8478e4b17023SJohn Marino tree
check_for_loop_decls(location_t loc,bool turn_off_iso_c99_error)8479e4b17023SJohn Marino check_for_loop_decls (location_t loc, bool turn_off_iso_c99_error)
8480e4b17023SJohn Marino {
8481e4b17023SJohn Marino struct c_binding *b;
8482e4b17023SJohn Marino tree one_decl = NULL_TREE;
8483e4b17023SJohn Marino int n_decls = 0;
8484e4b17023SJohn Marino
8485e4b17023SJohn Marino if (!turn_off_iso_c99_error)
8486e4b17023SJohn Marino {
8487e4b17023SJohn Marino static bool hint = true;
8488e4b17023SJohn Marino /* If we get here, declarations have been used in a for loop without
8489e4b17023SJohn Marino the C99 for loop scope. This doesn't make much sense, so don't
8490e4b17023SJohn Marino allow it. */
8491e4b17023SJohn Marino error_at (loc, "%<for%> loop initial declarations "
8492e4b17023SJohn Marino "are only allowed in C99 mode");
8493e4b17023SJohn Marino if (hint)
8494e4b17023SJohn Marino {
8495e4b17023SJohn Marino inform (loc,
8496e4b17023SJohn Marino "use option -std=c99 or -std=gnu99 to compile your code");
8497e4b17023SJohn Marino hint = false;
8498e4b17023SJohn Marino }
8499e4b17023SJohn Marino return NULL_TREE;
8500e4b17023SJohn Marino }
8501e4b17023SJohn Marino /* C99 subclause 6.8.5 paragraph 3:
8502e4b17023SJohn Marino
8503e4b17023SJohn Marino [#3] The declaration part of a for statement shall only
8504e4b17023SJohn Marino declare identifiers for objects having storage class auto or
8505e4b17023SJohn Marino register.
8506e4b17023SJohn Marino
8507e4b17023SJohn Marino It isn't clear whether, in this sentence, "identifiers" binds to
8508e4b17023SJohn Marino "shall only declare" or to "objects" - that is, whether all identifiers
8509e4b17023SJohn Marino declared must be identifiers for objects, or whether the restriction
8510e4b17023SJohn Marino only applies to those that are. (A question on this in comp.std.c
8511e4b17023SJohn Marino in November 2000 received no answer.) We implement the strictest
8512e4b17023SJohn Marino interpretation, to avoid creating an extension which later causes
8513e4b17023SJohn Marino problems. */
8514e4b17023SJohn Marino
8515e4b17023SJohn Marino for (b = current_scope->bindings; b; b = b->prev)
8516e4b17023SJohn Marino {
8517e4b17023SJohn Marino tree id = b->id;
8518e4b17023SJohn Marino tree decl = b->decl;
8519e4b17023SJohn Marino
8520e4b17023SJohn Marino if (!id)
8521e4b17023SJohn Marino continue;
8522e4b17023SJohn Marino
8523e4b17023SJohn Marino switch (TREE_CODE (decl))
8524e4b17023SJohn Marino {
8525e4b17023SJohn Marino case VAR_DECL:
8526e4b17023SJohn Marino {
8527e4b17023SJohn Marino location_t decl_loc = DECL_SOURCE_LOCATION (decl);
8528e4b17023SJohn Marino if (TREE_STATIC (decl))
8529e4b17023SJohn Marino error_at (decl_loc,
8530e4b17023SJohn Marino "declaration of static variable %qD in %<for%> loop "
8531e4b17023SJohn Marino "initial declaration", decl);
8532e4b17023SJohn Marino else if (DECL_EXTERNAL (decl))
8533e4b17023SJohn Marino error_at (decl_loc,
8534e4b17023SJohn Marino "declaration of %<extern%> variable %qD in %<for%> loop "
8535e4b17023SJohn Marino "initial declaration", decl);
8536e4b17023SJohn Marino }
8537e4b17023SJohn Marino break;
8538e4b17023SJohn Marino
8539e4b17023SJohn Marino case RECORD_TYPE:
8540e4b17023SJohn Marino error_at (loc,
8541e4b17023SJohn Marino "%<struct %E%> declared in %<for%> loop initial "
8542e4b17023SJohn Marino "declaration", id);
8543e4b17023SJohn Marino break;
8544e4b17023SJohn Marino case UNION_TYPE:
8545e4b17023SJohn Marino error_at (loc,
8546e4b17023SJohn Marino "%<union %E%> declared in %<for%> loop initial declaration",
8547e4b17023SJohn Marino id);
8548e4b17023SJohn Marino break;
8549e4b17023SJohn Marino case ENUMERAL_TYPE:
8550e4b17023SJohn Marino error_at (loc, "%<enum %E%> declared in %<for%> loop "
8551e4b17023SJohn Marino "initial declaration", id);
8552e4b17023SJohn Marino break;
8553e4b17023SJohn Marino default:
8554e4b17023SJohn Marino error_at (loc, "declaration of non-variable "
8555e4b17023SJohn Marino "%qD in %<for%> loop initial declaration", decl);
8556e4b17023SJohn Marino }
8557e4b17023SJohn Marino
8558e4b17023SJohn Marino n_decls++;
8559e4b17023SJohn Marino one_decl = decl;
8560e4b17023SJohn Marino }
8561e4b17023SJohn Marino
8562e4b17023SJohn Marino return n_decls == 1 ? one_decl : NULL_TREE;
8563e4b17023SJohn Marino }
8564e4b17023SJohn Marino
8565e4b17023SJohn Marino /* Save and reinitialize the variables
8566e4b17023SJohn Marino used during compilation of a C function. */
8567e4b17023SJohn Marino
8568e4b17023SJohn Marino void
c_push_function_context(void)8569e4b17023SJohn Marino c_push_function_context (void)
8570e4b17023SJohn Marino {
8571e4b17023SJohn Marino struct language_function *p = cfun->language;
8572e4b17023SJohn Marino /* cfun->language might have been already allocated by the use of
8573e4b17023SJohn Marino -Wunused-local-typedefs. In that case, just re-use it. */
8574e4b17023SJohn Marino if (p == NULL)
8575e4b17023SJohn Marino cfun->language = p = ggc_alloc_cleared_language_function ();
8576e4b17023SJohn Marino
8577e4b17023SJohn Marino p->base.x_stmt_tree = c_stmt_tree;
8578e4b17023SJohn Marino c_stmt_tree.x_cur_stmt_list
8579e4b17023SJohn Marino = VEC_copy (tree, gc, c_stmt_tree.x_cur_stmt_list);
8580e4b17023SJohn Marino p->x_break_label = c_break_label;
8581e4b17023SJohn Marino p->x_cont_label = c_cont_label;
8582e4b17023SJohn Marino p->x_switch_stack = c_switch_stack;
8583e4b17023SJohn Marino p->arg_info = current_function_arg_info;
8584e4b17023SJohn Marino p->returns_value = current_function_returns_value;
8585e4b17023SJohn Marino p->returns_null = current_function_returns_null;
8586e4b17023SJohn Marino p->returns_abnormally = current_function_returns_abnormally;
8587e4b17023SJohn Marino p->warn_about_return_type = warn_about_return_type;
8588e4b17023SJohn Marino
8589e4b17023SJohn Marino push_function_context ();
8590e4b17023SJohn Marino }
8591e4b17023SJohn Marino
8592e4b17023SJohn Marino /* Restore the variables used during compilation of a C function. */
8593e4b17023SJohn Marino
8594e4b17023SJohn Marino void
c_pop_function_context(void)8595e4b17023SJohn Marino c_pop_function_context (void)
8596e4b17023SJohn Marino {
8597e4b17023SJohn Marino struct language_function *p;
8598e4b17023SJohn Marino
8599e4b17023SJohn Marino pop_function_context ();
8600e4b17023SJohn Marino p = cfun->language;
8601e4b17023SJohn Marino /* When -Wunused-local-typedefs is in effect, cfun->languages is
8602e4b17023SJohn Marino used to store data throughout the life time of the current cfun,
8603e4b17023SJohn Marino So don't deallocate it. */
8604e4b17023SJohn Marino if (!warn_unused_local_typedefs)
8605e4b17023SJohn Marino cfun->language = NULL;
8606e4b17023SJohn Marino
8607e4b17023SJohn Marino if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
8608e4b17023SJohn Marino && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
8609e4b17023SJohn Marino {
8610e4b17023SJohn Marino /* Stop pointing to the local nodes about to be freed. */
8611e4b17023SJohn Marino /* But DECL_INITIAL must remain nonzero so we know this
8612e4b17023SJohn Marino was an actual function definition. */
8613e4b17023SJohn Marino DECL_INITIAL (current_function_decl) = error_mark_node;
8614e4b17023SJohn Marino DECL_ARGUMENTS (current_function_decl) = 0;
8615e4b17023SJohn Marino }
8616e4b17023SJohn Marino
8617e4b17023SJohn Marino c_stmt_tree = p->base.x_stmt_tree;
8618e4b17023SJohn Marino c_break_label = p->x_break_label;
8619e4b17023SJohn Marino c_cont_label = p->x_cont_label;
8620e4b17023SJohn Marino c_switch_stack = p->x_switch_stack;
8621e4b17023SJohn Marino current_function_arg_info = p->arg_info;
8622e4b17023SJohn Marino current_function_returns_value = p->returns_value;
8623e4b17023SJohn Marino current_function_returns_null = p->returns_null;
8624e4b17023SJohn Marino current_function_returns_abnormally = p->returns_abnormally;
8625e4b17023SJohn Marino warn_about_return_type = p->warn_about_return_type;
8626e4b17023SJohn Marino }
8627e4b17023SJohn Marino
8628e4b17023SJohn Marino /* The functions below are required for functionality of doing
8629e4b17023SJohn Marino function at once processing in the C front end. Currently these
8630e4b17023SJohn Marino functions are not called from anywhere in the C front end, but as
8631e4b17023SJohn Marino these changes continue, that will change. */
8632e4b17023SJohn Marino
8633e4b17023SJohn Marino /* Returns the stmt_tree (if any) to which statements are currently
8634e4b17023SJohn Marino being added. If there is no active statement-tree, NULL is
8635e4b17023SJohn Marino returned. */
8636e4b17023SJohn Marino
8637e4b17023SJohn Marino stmt_tree
current_stmt_tree(void)8638e4b17023SJohn Marino current_stmt_tree (void)
8639e4b17023SJohn Marino {
8640e4b17023SJohn Marino return &c_stmt_tree;
8641e4b17023SJohn Marino }
8642e4b17023SJohn Marino
8643e4b17023SJohn Marino /* Return the global value of T as a symbol. */
8644e4b17023SJohn Marino
8645e4b17023SJohn Marino tree
identifier_global_value(tree t)8646e4b17023SJohn Marino identifier_global_value (tree t)
8647e4b17023SJohn Marino {
8648e4b17023SJohn Marino struct c_binding *b;
8649e4b17023SJohn Marino
8650e4b17023SJohn Marino for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
8651e4b17023SJohn Marino if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
8652e4b17023SJohn Marino return b->decl;
8653e4b17023SJohn Marino
8654e4b17023SJohn Marino return 0;
8655e4b17023SJohn Marino }
8656e4b17023SJohn Marino
8657e4b17023SJohn Marino /* In C, the only C-linkage public declaration is at file scope. */
8658e4b17023SJohn Marino
8659e4b17023SJohn Marino tree
c_linkage_bindings(tree name)8660e4b17023SJohn Marino c_linkage_bindings (tree name)
8661e4b17023SJohn Marino {
8662e4b17023SJohn Marino return identifier_global_value (name);
8663e4b17023SJohn Marino }
8664e4b17023SJohn Marino
8665e4b17023SJohn Marino /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
8666e4b17023SJohn Marino otherwise the name is found in ridpointers from RID_INDEX. */
8667e4b17023SJohn Marino
8668e4b17023SJohn Marino void
record_builtin_type(enum rid rid_index,const char * name,tree type)8669e4b17023SJohn Marino record_builtin_type (enum rid rid_index, const char *name, tree type)
8670e4b17023SJohn Marino {
8671e4b17023SJohn Marino tree id, decl;
8672e4b17023SJohn Marino if (name == 0)
8673e4b17023SJohn Marino id = ridpointers[(int) rid_index];
8674e4b17023SJohn Marino else
8675e4b17023SJohn Marino id = get_identifier (name);
8676e4b17023SJohn Marino decl = build_decl (UNKNOWN_LOCATION, TYPE_DECL, id, type);
8677e4b17023SJohn Marino pushdecl (decl);
8678e4b17023SJohn Marino if (debug_hooks->type_decl)
8679e4b17023SJohn Marino debug_hooks->type_decl (decl, false);
8680e4b17023SJohn Marino }
8681e4b17023SJohn Marino
8682e4b17023SJohn Marino /* Build the void_list_node (void_type_node having been created). */
8683e4b17023SJohn Marino tree
build_void_list_node(void)8684e4b17023SJohn Marino build_void_list_node (void)
8685e4b17023SJohn Marino {
8686e4b17023SJohn Marino tree t = build_tree_list (NULL_TREE, void_type_node);
8687e4b17023SJohn Marino return t;
8688e4b17023SJohn Marino }
8689e4b17023SJohn Marino
8690e4b17023SJohn Marino /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
8691e4b17023SJohn Marino
8692e4b17023SJohn Marino struct c_parm *
build_c_parm(struct c_declspecs * specs,tree attrs,struct c_declarator * declarator)8693e4b17023SJohn Marino build_c_parm (struct c_declspecs *specs, tree attrs,
8694e4b17023SJohn Marino struct c_declarator *declarator)
8695e4b17023SJohn Marino {
8696e4b17023SJohn Marino struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
8697e4b17023SJohn Marino ret->specs = specs;
8698e4b17023SJohn Marino ret->attrs = attrs;
8699e4b17023SJohn Marino ret->declarator = declarator;
8700e4b17023SJohn Marino return ret;
8701e4b17023SJohn Marino }
8702e4b17023SJohn Marino
8703e4b17023SJohn Marino /* Return a declarator with nested attributes. TARGET is the inner
8704e4b17023SJohn Marino declarator to which these attributes apply. ATTRS are the
8705e4b17023SJohn Marino attributes. */
8706e4b17023SJohn Marino
8707e4b17023SJohn Marino struct c_declarator *
build_attrs_declarator(tree attrs,struct c_declarator * target)8708e4b17023SJohn Marino build_attrs_declarator (tree attrs, struct c_declarator *target)
8709e4b17023SJohn Marino {
8710e4b17023SJohn Marino struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8711e4b17023SJohn Marino ret->kind = cdk_attrs;
8712e4b17023SJohn Marino ret->declarator = target;
8713e4b17023SJohn Marino ret->u.attrs = attrs;
8714e4b17023SJohn Marino return ret;
8715e4b17023SJohn Marino }
8716e4b17023SJohn Marino
8717e4b17023SJohn Marino /* Return a declarator for a function with arguments specified by ARGS
8718e4b17023SJohn Marino and return type specified by TARGET. */
8719e4b17023SJohn Marino
8720e4b17023SJohn Marino struct c_declarator *
build_function_declarator(struct c_arg_info * args,struct c_declarator * target)8721e4b17023SJohn Marino build_function_declarator (struct c_arg_info *args,
8722e4b17023SJohn Marino struct c_declarator *target)
8723e4b17023SJohn Marino {
8724e4b17023SJohn Marino struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8725e4b17023SJohn Marino ret->kind = cdk_function;
8726e4b17023SJohn Marino ret->declarator = target;
8727e4b17023SJohn Marino ret->u.arg_info = args;
8728e4b17023SJohn Marino return ret;
8729e4b17023SJohn Marino }
8730e4b17023SJohn Marino
8731e4b17023SJohn Marino /* Return a declarator for the identifier IDENT (which may be
8732e4b17023SJohn Marino NULL_TREE for an abstract declarator). */
8733e4b17023SJohn Marino
8734e4b17023SJohn Marino struct c_declarator *
build_id_declarator(tree ident)8735e4b17023SJohn Marino build_id_declarator (tree ident)
8736e4b17023SJohn Marino {
8737e4b17023SJohn Marino struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8738e4b17023SJohn Marino ret->kind = cdk_id;
8739e4b17023SJohn Marino ret->declarator = 0;
8740e4b17023SJohn Marino ret->u.id = ident;
8741e4b17023SJohn Marino /* Default value - may get reset to a more precise location. */
8742e4b17023SJohn Marino ret->id_loc = input_location;
8743e4b17023SJohn Marino return ret;
8744e4b17023SJohn Marino }
8745e4b17023SJohn Marino
8746e4b17023SJohn Marino /* Return something to represent absolute declarators containing a *.
8747e4b17023SJohn Marino TARGET is the absolute declarator that the * contains.
8748e4b17023SJohn Marino TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
8749e4b17023SJohn Marino to apply to the pointer type. */
8750e4b17023SJohn Marino
8751e4b17023SJohn Marino struct c_declarator *
make_pointer_declarator(struct c_declspecs * type_quals_attrs,struct c_declarator * target)8752e4b17023SJohn Marino make_pointer_declarator (struct c_declspecs *type_quals_attrs,
8753e4b17023SJohn Marino struct c_declarator *target)
8754e4b17023SJohn Marino {
8755e4b17023SJohn Marino tree attrs;
8756e4b17023SJohn Marino int quals = 0;
8757e4b17023SJohn Marino struct c_declarator *itarget = target;
8758e4b17023SJohn Marino struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
8759e4b17023SJohn Marino if (type_quals_attrs)
8760e4b17023SJohn Marino {
8761e4b17023SJohn Marino attrs = type_quals_attrs->attrs;
8762e4b17023SJohn Marino quals = quals_from_declspecs (type_quals_attrs);
8763e4b17023SJohn Marino if (attrs != NULL_TREE)
8764e4b17023SJohn Marino itarget = build_attrs_declarator (attrs, target);
8765e4b17023SJohn Marino }
8766e4b17023SJohn Marino ret->kind = cdk_pointer;
8767e4b17023SJohn Marino ret->declarator = itarget;
8768e4b17023SJohn Marino ret->u.pointer_quals = quals;
8769e4b17023SJohn Marino return ret;
8770e4b17023SJohn Marino }
8771e4b17023SJohn Marino
8772e4b17023SJohn Marino /* Return a pointer to a structure for an empty list of declaration
8773e4b17023SJohn Marino specifiers. */
8774e4b17023SJohn Marino
8775e4b17023SJohn Marino struct c_declspecs *
build_null_declspecs(void)8776e4b17023SJohn Marino build_null_declspecs (void)
8777e4b17023SJohn Marino {
8778e4b17023SJohn Marino struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
8779e4b17023SJohn Marino ret->type = 0;
8780e4b17023SJohn Marino ret->expr = 0;
8781e4b17023SJohn Marino ret->decl_attr = 0;
8782e4b17023SJohn Marino ret->attrs = 0;
8783e4b17023SJohn Marino ret->align_log = -1;
8784e4b17023SJohn Marino ret->typespec_word = cts_none;
8785e4b17023SJohn Marino ret->storage_class = csc_none;
8786e4b17023SJohn Marino ret->expr_const_operands = true;
8787e4b17023SJohn Marino ret->declspecs_seen_p = false;
8788e4b17023SJohn Marino ret->typespec_kind = ctsk_none;
8789e4b17023SJohn Marino ret->non_sc_seen_p = false;
8790e4b17023SJohn Marino ret->typedef_p = false;
8791e4b17023SJohn Marino ret->explicit_signed_p = false;
8792e4b17023SJohn Marino ret->deprecated_p = false;
8793e4b17023SJohn Marino ret->default_int_p = false;
8794e4b17023SJohn Marino ret->long_p = false;
8795e4b17023SJohn Marino ret->long_long_p = false;
8796e4b17023SJohn Marino ret->short_p = false;
8797e4b17023SJohn Marino ret->signed_p = false;
8798e4b17023SJohn Marino ret->unsigned_p = false;
8799e4b17023SJohn Marino ret->complex_p = false;
8800e4b17023SJohn Marino ret->inline_p = false;
8801e4b17023SJohn Marino ret->noreturn_p = false;
8802e4b17023SJohn Marino ret->thread_p = false;
8803e4b17023SJohn Marino ret->const_p = false;
8804e4b17023SJohn Marino ret->volatile_p = false;
8805e4b17023SJohn Marino ret->restrict_p = false;
8806e4b17023SJohn Marino ret->saturating_p = false;
8807e4b17023SJohn Marino ret->alignas_p = false;
8808e4b17023SJohn Marino ret->address_space = ADDR_SPACE_GENERIC;
8809e4b17023SJohn Marino return ret;
8810e4b17023SJohn Marino }
8811e4b17023SJohn Marino
8812e4b17023SJohn Marino /* Add the address space ADDRSPACE to the declaration specifiers
8813e4b17023SJohn Marino SPECS, returning SPECS. */
8814e4b17023SJohn Marino
8815e4b17023SJohn Marino struct c_declspecs *
declspecs_add_addrspace(struct c_declspecs * specs,addr_space_t as)8816e4b17023SJohn Marino declspecs_add_addrspace (struct c_declspecs *specs, addr_space_t as)
8817e4b17023SJohn Marino {
8818e4b17023SJohn Marino specs->non_sc_seen_p = true;
8819e4b17023SJohn Marino specs->declspecs_seen_p = true;
8820e4b17023SJohn Marino
8821e4b17023SJohn Marino if (!ADDR_SPACE_GENERIC_P (specs->address_space)
8822e4b17023SJohn Marino && specs->address_space != as)
8823e4b17023SJohn Marino error ("incompatible address space qualifiers %qs and %qs",
8824e4b17023SJohn Marino c_addr_space_name (as),
8825e4b17023SJohn Marino c_addr_space_name (specs->address_space));
8826e4b17023SJohn Marino else
8827e4b17023SJohn Marino specs->address_space = as;
8828e4b17023SJohn Marino return specs;
8829e4b17023SJohn Marino }
8830e4b17023SJohn Marino
8831e4b17023SJohn Marino /* Add the type qualifier QUAL to the declaration specifiers SPECS,
8832e4b17023SJohn Marino returning SPECS. */
8833e4b17023SJohn Marino
8834e4b17023SJohn Marino struct c_declspecs *
declspecs_add_qual(struct c_declspecs * specs,tree qual)8835e4b17023SJohn Marino declspecs_add_qual (struct c_declspecs *specs, tree qual)
8836e4b17023SJohn Marino {
8837e4b17023SJohn Marino enum rid i;
8838e4b17023SJohn Marino bool dupe = false;
8839e4b17023SJohn Marino specs->non_sc_seen_p = true;
8840e4b17023SJohn Marino specs->declspecs_seen_p = true;
8841e4b17023SJohn Marino gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
8842e4b17023SJohn Marino && C_IS_RESERVED_WORD (qual));
8843e4b17023SJohn Marino i = C_RID_CODE (qual);
8844e4b17023SJohn Marino switch (i)
8845e4b17023SJohn Marino {
8846e4b17023SJohn Marino case RID_CONST:
8847e4b17023SJohn Marino dupe = specs->const_p;
8848e4b17023SJohn Marino specs->const_p = true;
8849e4b17023SJohn Marino break;
8850e4b17023SJohn Marino case RID_VOLATILE:
8851e4b17023SJohn Marino dupe = specs->volatile_p;
8852e4b17023SJohn Marino specs->volatile_p = true;
8853e4b17023SJohn Marino break;
8854e4b17023SJohn Marino case RID_RESTRICT:
8855e4b17023SJohn Marino dupe = specs->restrict_p;
8856e4b17023SJohn Marino specs->restrict_p = true;
8857e4b17023SJohn Marino break;
8858e4b17023SJohn Marino default:
8859e4b17023SJohn Marino gcc_unreachable ();
8860e4b17023SJohn Marino }
8861e4b17023SJohn Marino if (dupe && !flag_isoc99)
8862e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic, "duplicate %qE", qual);
8863e4b17023SJohn Marino return specs;
8864e4b17023SJohn Marino }
8865e4b17023SJohn Marino
8866e4b17023SJohn Marino /* Add the type specifier TYPE to the declaration specifiers SPECS,
8867e4b17023SJohn Marino returning SPECS. */
8868e4b17023SJohn Marino
8869e4b17023SJohn Marino struct c_declspecs *
declspecs_add_type(location_t loc,struct c_declspecs * specs,struct c_typespec spec)8870e4b17023SJohn Marino declspecs_add_type (location_t loc, struct c_declspecs *specs,
8871e4b17023SJohn Marino struct c_typespec spec)
8872e4b17023SJohn Marino {
8873e4b17023SJohn Marino tree type = spec.spec;
8874e4b17023SJohn Marino specs->non_sc_seen_p = true;
8875e4b17023SJohn Marino specs->declspecs_seen_p = true;
8876e4b17023SJohn Marino specs->typespec_kind = spec.kind;
8877e4b17023SJohn Marino if (TREE_DEPRECATED (type))
8878e4b17023SJohn Marino specs->deprecated_p = true;
8879e4b17023SJohn Marino
8880e4b17023SJohn Marino /* Handle type specifier keywords. */
8881e4b17023SJohn Marino if (TREE_CODE (type) == IDENTIFIER_NODE
8882e4b17023SJohn Marino && C_IS_RESERVED_WORD (type)
8883e4b17023SJohn Marino && C_RID_CODE (type) != RID_CXX_COMPAT_WARN)
8884e4b17023SJohn Marino {
8885e4b17023SJohn Marino enum rid i = C_RID_CODE (type);
8886e4b17023SJohn Marino if (specs->type)
8887e4b17023SJohn Marino {
8888e4b17023SJohn Marino error_at (loc, "two or more data types in declaration specifiers");
8889e4b17023SJohn Marino return specs;
8890e4b17023SJohn Marino }
8891e4b17023SJohn Marino if ((int) i <= (int) RID_LAST_MODIFIER)
8892e4b17023SJohn Marino {
8893e4b17023SJohn Marino /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
8894e4b17023SJohn Marino bool dupe = false;
8895e4b17023SJohn Marino switch (i)
8896e4b17023SJohn Marino {
8897e4b17023SJohn Marino case RID_LONG:
8898e4b17023SJohn Marino if (specs->long_long_p)
8899e4b17023SJohn Marino {
8900e4b17023SJohn Marino error_at (loc, "%<long long long%> is too long for GCC");
8901e4b17023SJohn Marino break;
8902e4b17023SJohn Marino }
8903e4b17023SJohn Marino if (specs->long_p)
8904e4b17023SJohn Marino {
8905e4b17023SJohn Marino if (specs->typespec_word == cts_double)
8906e4b17023SJohn Marino {
8907e4b17023SJohn Marino error_at (loc,
8908e4b17023SJohn Marino ("both %<long long%> and %<double%> in "
8909e4b17023SJohn Marino "declaration specifiers"));
8910e4b17023SJohn Marino break;
8911e4b17023SJohn Marino }
8912e4b17023SJohn Marino pedwarn_c90 (loc, OPT_Wlong_long,
8913e4b17023SJohn Marino "ISO C90 does not support %<long long%>");
8914e4b17023SJohn Marino specs->long_long_p = 1;
8915e4b17023SJohn Marino break;
8916e4b17023SJohn Marino }
8917e4b17023SJohn Marino if (specs->short_p)
8918e4b17023SJohn Marino error_at (loc,
8919e4b17023SJohn Marino ("both %<long%> and %<short%> in "
8920e4b17023SJohn Marino "declaration specifiers"));
8921e4b17023SJohn Marino else if (specs->typespec_word == cts_void)
8922e4b17023SJohn Marino error_at (loc,
8923e4b17023SJohn Marino ("both %<long%> and %<void%> in "
8924e4b17023SJohn Marino "declaration specifiers"));
8925e4b17023SJohn Marino else if (specs->typespec_word == cts_int128)
8926e4b17023SJohn Marino error_at (loc,
8927e4b17023SJohn Marino ("both %<long%> and %<__int128%> in "
8928e4b17023SJohn Marino "declaration specifiers"));
8929e4b17023SJohn Marino else if (specs->typespec_word == cts_bool)
8930e4b17023SJohn Marino error_at (loc,
8931e4b17023SJohn Marino ("both %<long%> and %<_Bool%> in "
8932e4b17023SJohn Marino "declaration specifiers"));
8933e4b17023SJohn Marino else if (specs->typespec_word == cts_char)
8934e4b17023SJohn Marino error_at (loc,
8935e4b17023SJohn Marino ("both %<long%> and %<char%> in "
8936e4b17023SJohn Marino "declaration specifiers"));
8937e4b17023SJohn Marino else if (specs->typespec_word == cts_float)
8938e4b17023SJohn Marino error_at (loc,
8939e4b17023SJohn Marino ("both %<long%> and %<float%> in "
8940e4b17023SJohn Marino "declaration specifiers"));
8941e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat32)
8942e4b17023SJohn Marino error_at (loc,
8943e4b17023SJohn Marino ("both %<long%> and %<_Decimal32%> in "
8944e4b17023SJohn Marino "declaration specifiers"));
8945e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
8946e4b17023SJohn Marino error_at (loc,
8947e4b17023SJohn Marino ("both %<long%> and %<_Decimal64%> in "
8948e4b17023SJohn Marino "declaration specifiers"));
8949e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat128)
8950e4b17023SJohn Marino error_at (loc,
8951e4b17023SJohn Marino ("both %<long%> and %<_Decimal128%> in "
8952e4b17023SJohn Marino "declaration specifiers"));
8953e4b17023SJohn Marino else
8954e4b17023SJohn Marino specs->long_p = true;
8955e4b17023SJohn Marino break;
8956e4b17023SJohn Marino case RID_SHORT:
8957e4b17023SJohn Marino dupe = specs->short_p;
8958e4b17023SJohn Marino if (specs->long_p)
8959e4b17023SJohn Marino error_at (loc,
8960e4b17023SJohn Marino ("both %<long%> and %<short%> in "
8961e4b17023SJohn Marino "declaration specifiers"));
8962e4b17023SJohn Marino else if (specs->typespec_word == cts_void)
8963e4b17023SJohn Marino error_at (loc,
8964e4b17023SJohn Marino ("both %<short%> and %<void%> in "
8965e4b17023SJohn Marino "declaration specifiers"));
8966e4b17023SJohn Marino else if (specs->typespec_word == cts_int128)
8967e4b17023SJohn Marino error_at (loc,
8968e4b17023SJohn Marino ("both %<short%> and %<__int128%> in "
8969e4b17023SJohn Marino "declaration specifiers"));
8970e4b17023SJohn Marino else if (specs->typespec_word == cts_bool)
8971e4b17023SJohn Marino error_at (loc,
8972e4b17023SJohn Marino ("both %<short%> and %<_Bool%> in "
8973e4b17023SJohn Marino "declaration specifiers"));
8974e4b17023SJohn Marino else if (specs->typespec_word == cts_char)
8975e4b17023SJohn Marino error_at (loc,
8976e4b17023SJohn Marino ("both %<short%> and %<char%> in "
8977e4b17023SJohn Marino "declaration specifiers"));
8978e4b17023SJohn Marino else if (specs->typespec_word == cts_float)
8979e4b17023SJohn Marino error_at (loc,
8980e4b17023SJohn Marino ("both %<short%> and %<float%> in "
8981e4b17023SJohn Marino "declaration specifiers"));
8982e4b17023SJohn Marino else if (specs->typespec_word == cts_double)
8983e4b17023SJohn Marino error_at (loc,
8984e4b17023SJohn Marino ("both %<short%> and %<double%> in "
8985e4b17023SJohn Marino "declaration specifiers"));
8986e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat32)
8987e4b17023SJohn Marino error_at (loc,
8988e4b17023SJohn Marino ("both %<short%> and %<_Decimal32%> in "
8989e4b17023SJohn Marino "declaration specifiers"));
8990e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
8991e4b17023SJohn Marino error_at (loc,
8992e4b17023SJohn Marino ("both %<short%> and %<_Decimal64%> in "
8993e4b17023SJohn Marino "declaration specifiers"));
8994e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat128)
8995e4b17023SJohn Marino error_at (loc,
8996e4b17023SJohn Marino ("both %<short%> and %<_Decimal128%> in "
8997e4b17023SJohn Marino "declaration specifiers"));
8998e4b17023SJohn Marino else
8999e4b17023SJohn Marino specs->short_p = true;
9000e4b17023SJohn Marino break;
9001e4b17023SJohn Marino case RID_SIGNED:
9002e4b17023SJohn Marino dupe = specs->signed_p;
9003e4b17023SJohn Marino if (specs->unsigned_p)
9004e4b17023SJohn Marino error_at (loc,
9005e4b17023SJohn Marino ("both %<signed%> and %<unsigned%> in "
9006e4b17023SJohn Marino "declaration specifiers"));
9007e4b17023SJohn Marino else if (specs->typespec_word == cts_void)
9008e4b17023SJohn Marino error_at (loc,
9009e4b17023SJohn Marino ("both %<signed%> and %<void%> in "
9010e4b17023SJohn Marino "declaration specifiers"));
9011e4b17023SJohn Marino else if (specs->typespec_word == cts_bool)
9012e4b17023SJohn Marino error_at (loc,
9013e4b17023SJohn Marino ("both %<signed%> and %<_Bool%> in "
9014e4b17023SJohn Marino "declaration specifiers"));
9015e4b17023SJohn Marino else if (specs->typespec_word == cts_float)
9016e4b17023SJohn Marino error_at (loc,
9017e4b17023SJohn Marino ("both %<signed%> and %<float%> in "
9018e4b17023SJohn Marino "declaration specifiers"));
9019e4b17023SJohn Marino else if (specs->typespec_word == cts_double)
9020e4b17023SJohn Marino error_at (loc,
9021e4b17023SJohn Marino ("both %<signed%> and %<double%> in "
9022e4b17023SJohn Marino "declaration specifiers"));
9023e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat32)
9024e4b17023SJohn Marino error_at (loc,
9025e4b17023SJohn Marino ("both %<signed%> and %<_Decimal32%> in "
9026e4b17023SJohn Marino "declaration specifiers"));
9027e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
9028e4b17023SJohn Marino error_at (loc,
9029e4b17023SJohn Marino ("both %<signed%> and %<_Decimal64%> in "
9030e4b17023SJohn Marino "declaration specifiers"));
9031e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat128)
9032e4b17023SJohn Marino error_at (loc,
9033e4b17023SJohn Marino ("both %<signed%> and %<_Decimal128%> in "
9034e4b17023SJohn Marino "declaration specifiers"));
9035e4b17023SJohn Marino else
9036e4b17023SJohn Marino specs->signed_p = true;
9037e4b17023SJohn Marino break;
9038e4b17023SJohn Marino case RID_UNSIGNED:
9039e4b17023SJohn Marino dupe = specs->unsigned_p;
9040e4b17023SJohn Marino if (specs->signed_p)
9041e4b17023SJohn Marino error_at (loc,
9042e4b17023SJohn Marino ("both %<signed%> and %<unsigned%> in "
9043e4b17023SJohn Marino "declaration specifiers"));
9044e4b17023SJohn Marino else if (specs->typespec_word == cts_void)
9045e4b17023SJohn Marino error_at (loc,
9046e4b17023SJohn Marino ("both %<unsigned%> and %<void%> in "
9047e4b17023SJohn Marino "declaration specifiers"));
9048e4b17023SJohn Marino else if (specs->typespec_word == cts_bool)
9049e4b17023SJohn Marino error_at (loc,
9050e4b17023SJohn Marino ("both %<unsigned%> and %<_Bool%> in "
9051e4b17023SJohn Marino "declaration specifiers"));
9052e4b17023SJohn Marino else if (specs->typespec_word == cts_float)
9053e4b17023SJohn Marino error_at (loc,
9054e4b17023SJohn Marino ("both %<unsigned%> and %<float%> in "
9055e4b17023SJohn Marino "declaration specifiers"));
9056e4b17023SJohn Marino else if (specs->typespec_word == cts_double)
9057e4b17023SJohn Marino error_at (loc,
9058e4b17023SJohn Marino ("both %<unsigned%> and %<double%> in "
9059e4b17023SJohn Marino "declaration specifiers"));
9060e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat32)
9061e4b17023SJohn Marino error_at (loc,
9062e4b17023SJohn Marino ("both %<unsigned%> and %<_Decimal32%> in "
9063e4b17023SJohn Marino "declaration specifiers"));
9064e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
9065e4b17023SJohn Marino error_at (loc,
9066e4b17023SJohn Marino ("both %<unsigned%> and %<_Decimal64%> in "
9067e4b17023SJohn Marino "declaration specifiers"));
9068e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat128)
9069e4b17023SJohn Marino error_at (loc,
9070e4b17023SJohn Marino ("both %<unsigned%> and %<_Decimal128%> in "
9071e4b17023SJohn Marino "declaration specifiers"));
9072e4b17023SJohn Marino else
9073e4b17023SJohn Marino specs->unsigned_p = true;
9074e4b17023SJohn Marino break;
9075e4b17023SJohn Marino case RID_COMPLEX:
9076e4b17023SJohn Marino dupe = specs->complex_p;
9077e4b17023SJohn Marino if (!flag_isoc99 && !in_system_header)
9078e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
9079e4b17023SJohn Marino "ISO C90 does not support complex types");
9080e4b17023SJohn Marino if (specs->typespec_word == cts_void)
9081e4b17023SJohn Marino error_at (loc,
9082e4b17023SJohn Marino ("both %<complex%> and %<void%> in "
9083e4b17023SJohn Marino "declaration specifiers"));
9084e4b17023SJohn Marino else if (specs->typespec_word == cts_bool)
9085e4b17023SJohn Marino error_at (loc,
9086e4b17023SJohn Marino ("both %<complex%> and %<_Bool%> in "
9087e4b17023SJohn Marino "declaration specifiers"));
9088e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat32)
9089e4b17023SJohn Marino error_at (loc,
9090e4b17023SJohn Marino ("both %<complex%> and %<_Decimal32%> in "
9091e4b17023SJohn Marino "declaration specifiers"));
9092e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
9093e4b17023SJohn Marino error_at (loc,
9094e4b17023SJohn Marino ("both %<complex%> and %<_Decimal64%> in "
9095e4b17023SJohn Marino "declaration specifiers"));
9096e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat128)
9097e4b17023SJohn Marino error_at (loc,
9098e4b17023SJohn Marino ("both %<complex%> and %<_Decimal128%> in "
9099e4b17023SJohn Marino "declaration specifiers"));
9100e4b17023SJohn Marino else if (specs->typespec_word == cts_fract)
9101e4b17023SJohn Marino error_at (loc,
9102e4b17023SJohn Marino ("both %<complex%> and %<_Fract%> in "
9103e4b17023SJohn Marino "declaration specifiers"));
9104e4b17023SJohn Marino else if (specs->typespec_word == cts_accum)
9105e4b17023SJohn Marino error_at (loc,
9106e4b17023SJohn Marino ("both %<complex%> and %<_Accum%> in "
9107e4b17023SJohn Marino "declaration specifiers"));
9108e4b17023SJohn Marino else if (specs->saturating_p)
9109e4b17023SJohn Marino error_at (loc,
9110e4b17023SJohn Marino ("both %<complex%> and %<_Sat%> in "
9111e4b17023SJohn Marino "declaration specifiers"));
9112e4b17023SJohn Marino else
9113e4b17023SJohn Marino specs->complex_p = true;
9114e4b17023SJohn Marino break;
9115e4b17023SJohn Marino case RID_SAT:
9116e4b17023SJohn Marino dupe = specs->saturating_p;
9117e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
9118e4b17023SJohn Marino "ISO C does not support saturating types");
9119e4b17023SJohn Marino if (specs->typespec_word == cts_int128)
9120e4b17023SJohn Marino {
9121e4b17023SJohn Marino error_at (loc,
9122e4b17023SJohn Marino ("both %<_Sat%> and %<__int128%> in "
9123e4b17023SJohn Marino "declaration specifiers"));
9124e4b17023SJohn Marino }
9125e4b17023SJohn Marino else if (specs->typespec_word == cts_void)
9126e4b17023SJohn Marino error_at (loc,
9127e4b17023SJohn Marino ("both %<_Sat%> and %<void%> in "
9128e4b17023SJohn Marino "declaration specifiers"));
9129e4b17023SJohn Marino else if (specs->typespec_word == cts_bool)
9130e4b17023SJohn Marino error_at (loc,
9131e4b17023SJohn Marino ("both %<_Sat%> and %<_Bool%> in "
9132e4b17023SJohn Marino "declaration specifiers"));
9133e4b17023SJohn Marino else if (specs->typespec_word == cts_char)
9134e4b17023SJohn Marino error_at (loc,
9135e4b17023SJohn Marino ("both %<_Sat%> and %<char%> in "
9136e4b17023SJohn Marino "declaration specifiers"));
9137e4b17023SJohn Marino else if (specs->typespec_word == cts_int)
9138e4b17023SJohn Marino error_at (loc,
9139e4b17023SJohn Marino ("both %<_Sat%> and %<int%> in "
9140e4b17023SJohn Marino "declaration specifiers"));
9141e4b17023SJohn Marino else if (specs->typespec_word == cts_float)
9142e4b17023SJohn Marino error_at (loc,
9143e4b17023SJohn Marino ("both %<_Sat%> and %<float%> in "
9144e4b17023SJohn Marino "declaration specifiers"));
9145e4b17023SJohn Marino else if (specs->typespec_word == cts_double)
9146e4b17023SJohn Marino error_at (loc,
9147e4b17023SJohn Marino ("both %<_Sat%> and %<double%> in "
9148e4b17023SJohn Marino "declaration specifiers"));
9149e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat32)
9150e4b17023SJohn Marino error_at (loc,
9151e4b17023SJohn Marino ("both %<_Sat%> and %<_Decimal32%> in "
9152e4b17023SJohn Marino "declaration specifiers"));
9153e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
9154e4b17023SJohn Marino error_at (loc,
9155e4b17023SJohn Marino ("both %<_Sat%> and %<_Decimal64%> in "
9156e4b17023SJohn Marino "declaration specifiers"));
9157e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat128)
9158e4b17023SJohn Marino error_at (loc,
9159e4b17023SJohn Marino ("both %<_Sat%> and %<_Decimal128%> in "
9160e4b17023SJohn Marino "declaration specifiers"));
9161e4b17023SJohn Marino else if (specs->complex_p)
9162e4b17023SJohn Marino error_at (loc,
9163e4b17023SJohn Marino ("both %<_Sat%> and %<complex%> in "
9164e4b17023SJohn Marino "declaration specifiers"));
9165e4b17023SJohn Marino else
9166e4b17023SJohn Marino specs->saturating_p = true;
9167e4b17023SJohn Marino break;
9168e4b17023SJohn Marino default:
9169e4b17023SJohn Marino gcc_unreachable ();
9170e4b17023SJohn Marino }
9171e4b17023SJohn Marino
9172e4b17023SJohn Marino if (dupe)
9173e4b17023SJohn Marino error_at (loc, "duplicate %qE", type);
9174e4b17023SJohn Marino
9175e4b17023SJohn Marino return specs;
9176e4b17023SJohn Marino }
9177e4b17023SJohn Marino else
9178e4b17023SJohn Marino {
9179e4b17023SJohn Marino /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
9180e4b17023SJohn Marino "__int128", "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
9181e4b17023SJohn Marino if (specs->typespec_word != cts_none)
9182e4b17023SJohn Marino {
9183e4b17023SJohn Marino error_at (loc,
9184e4b17023SJohn Marino "two or more data types in declaration specifiers");
9185e4b17023SJohn Marino return specs;
9186e4b17023SJohn Marino }
9187e4b17023SJohn Marino switch (i)
9188e4b17023SJohn Marino {
9189e4b17023SJohn Marino case RID_INT128:
9190e4b17023SJohn Marino if (int128_integer_type_node == NULL_TREE)
9191e4b17023SJohn Marino {
9192e4b17023SJohn Marino error_at (loc, "%<__int128%> is not supported for this target");
9193e4b17023SJohn Marino return specs;
9194e4b17023SJohn Marino }
9195e4b17023SJohn Marino if (!in_system_header)
9196e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
9197e4b17023SJohn Marino "ISO C does not support %<__int128%> type");
9198e4b17023SJohn Marino
9199e4b17023SJohn Marino if (specs->long_p)
9200e4b17023SJohn Marino error_at (loc,
9201e4b17023SJohn Marino ("both %<__int128%> and %<long%> in "
9202e4b17023SJohn Marino "declaration specifiers"));
9203e4b17023SJohn Marino else if (specs->saturating_p)
9204e4b17023SJohn Marino error_at (loc,
9205e4b17023SJohn Marino ("both %<_Sat%> and %<__int128%> in "
9206e4b17023SJohn Marino "declaration specifiers"));
9207e4b17023SJohn Marino else if (specs->short_p)
9208e4b17023SJohn Marino error_at (loc,
9209e4b17023SJohn Marino ("both %<__int128%> and %<short%> in "
9210e4b17023SJohn Marino "declaration specifiers"));
9211e4b17023SJohn Marino else
9212e4b17023SJohn Marino specs->typespec_word = cts_int128;
9213e4b17023SJohn Marino return specs;
9214e4b17023SJohn Marino case RID_VOID:
9215e4b17023SJohn Marino if (specs->long_p)
9216e4b17023SJohn Marino error_at (loc,
9217e4b17023SJohn Marino ("both %<long%> and %<void%> in "
9218e4b17023SJohn Marino "declaration specifiers"));
9219e4b17023SJohn Marino else if (specs->short_p)
9220e4b17023SJohn Marino error_at (loc,
9221e4b17023SJohn Marino ("both %<short%> and %<void%> in "
9222e4b17023SJohn Marino "declaration specifiers"));
9223e4b17023SJohn Marino else if (specs->signed_p)
9224e4b17023SJohn Marino error_at (loc,
9225e4b17023SJohn Marino ("both %<signed%> and %<void%> in "
9226e4b17023SJohn Marino "declaration specifiers"));
9227e4b17023SJohn Marino else if (specs->unsigned_p)
9228e4b17023SJohn Marino error_at (loc,
9229e4b17023SJohn Marino ("both %<unsigned%> and %<void%> in "
9230e4b17023SJohn Marino "declaration specifiers"));
9231e4b17023SJohn Marino else if (specs->complex_p)
9232e4b17023SJohn Marino error_at (loc,
9233e4b17023SJohn Marino ("both %<complex%> and %<void%> in "
9234e4b17023SJohn Marino "declaration specifiers"));
9235e4b17023SJohn Marino else if (specs->saturating_p)
9236e4b17023SJohn Marino error_at (loc,
9237e4b17023SJohn Marino ("both %<_Sat%> and %<void%> in "
9238e4b17023SJohn Marino "declaration specifiers"));
9239e4b17023SJohn Marino else
9240e4b17023SJohn Marino specs->typespec_word = cts_void;
9241e4b17023SJohn Marino return specs;
9242e4b17023SJohn Marino case RID_BOOL:
9243e4b17023SJohn Marino if (specs->long_p)
9244e4b17023SJohn Marino error_at (loc,
9245e4b17023SJohn Marino ("both %<long%> and %<_Bool%> in "
9246e4b17023SJohn Marino "declaration specifiers"));
9247e4b17023SJohn Marino else if (specs->short_p)
9248e4b17023SJohn Marino error_at (loc,
9249e4b17023SJohn Marino ("both %<short%> and %<_Bool%> in "
9250e4b17023SJohn Marino "declaration specifiers"));
9251e4b17023SJohn Marino else if (specs->signed_p)
9252e4b17023SJohn Marino error_at (loc,
9253e4b17023SJohn Marino ("both %<signed%> and %<_Bool%> in "
9254e4b17023SJohn Marino "declaration specifiers"));
9255e4b17023SJohn Marino else if (specs->unsigned_p)
9256e4b17023SJohn Marino error_at (loc,
9257e4b17023SJohn Marino ("both %<unsigned%> and %<_Bool%> in "
9258e4b17023SJohn Marino "declaration specifiers"));
9259e4b17023SJohn Marino else if (specs->complex_p)
9260e4b17023SJohn Marino error_at (loc,
9261e4b17023SJohn Marino ("both %<complex%> and %<_Bool%> in "
9262e4b17023SJohn Marino "declaration specifiers"));
9263e4b17023SJohn Marino else if (specs->saturating_p)
9264e4b17023SJohn Marino error_at (loc,
9265e4b17023SJohn Marino ("both %<_Sat%> and %<_Bool%> in "
9266e4b17023SJohn Marino "declaration specifiers"));
9267e4b17023SJohn Marino else
9268e4b17023SJohn Marino specs->typespec_word = cts_bool;
9269e4b17023SJohn Marino return specs;
9270e4b17023SJohn Marino case RID_CHAR:
9271e4b17023SJohn Marino if (specs->long_p)
9272e4b17023SJohn Marino error_at (loc,
9273e4b17023SJohn Marino ("both %<long%> and %<char%> in "
9274e4b17023SJohn Marino "declaration specifiers"));
9275e4b17023SJohn Marino else if (specs->short_p)
9276e4b17023SJohn Marino error_at (loc,
9277e4b17023SJohn Marino ("both %<short%> and %<char%> in "
9278e4b17023SJohn Marino "declaration specifiers"));
9279e4b17023SJohn Marino else if (specs->saturating_p)
9280e4b17023SJohn Marino error_at (loc,
9281e4b17023SJohn Marino ("both %<_Sat%> and %<char%> in "
9282e4b17023SJohn Marino "declaration specifiers"));
9283e4b17023SJohn Marino else
9284e4b17023SJohn Marino specs->typespec_word = cts_char;
9285e4b17023SJohn Marino return specs;
9286e4b17023SJohn Marino case RID_INT:
9287e4b17023SJohn Marino if (specs->saturating_p)
9288e4b17023SJohn Marino error_at (loc,
9289e4b17023SJohn Marino ("both %<_Sat%> and %<int%> in "
9290e4b17023SJohn Marino "declaration specifiers"));
9291e4b17023SJohn Marino else
9292e4b17023SJohn Marino specs->typespec_word = cts_int;
9293e4b17023SJohn Marino return specs;
9294e4b17023SJohn Marino case RID_FLOAT:
9295e4b17023SJohn Marino if (specs->long_p)
9296e4b17023SJohn Marino error_at (loc,
9297e4b17023SJohn Marino ("both %<long%> and %<float%> in "
9298e4b17023SJohn Marino "declaration specifiers"));
9299e4b17023SJohn Marino else if (specs->short_p)
9300e4b17023SJohn Marino error_at (loc,
9301e4b17023SJohn Marino ("both %<short%> and %<float%> in "
9302e4b17023SJohn Marino "declaration specifiers"));
9303e4b17023SJohn Marino else if (specs->signed_p)
9304e4b17023SJohn Marino error_at (loc,
9305e4b17023SJohn Marino ("both %<signed%> and %<float%> in "
9306e4b17023SJohn Marino "declaration specifiers"));
9307e4b17023SJohn Marino else if (specs->unsigned_p)
9308e4b17023SJohn Marino error_at (loc,
9309e4b17023SJohn Marino ("both %<unsigned%> and %<float%> in "
9310e4b17023SJohn Marino "declaration specifiers"));
9311e4b17023SJohn Marino else if (specs->saturating_p)
9312e4b17023SJohn Marino error_at (loc,
9313e4b17023SJohn Marino ("both %<_Sat%> and %<float%> in "
9314e4b17023SJohn Marino "declaration specifiers"));
9315e4b17023SJohn Marino else
9316e4b17023SJohn Marino specs->typespec_word = cts_float;
9317e4b17023SJohn Marino return specs;
9318e4b17023SJohn Marino case RID_DOUBLE:
9319e4b17023SJohn Marino if (specs->long_long_p)
9320e4b17023SJohn Marino error_at (loc,
9321e4b17023SJohn Marino ("both %<long long%> and %<double%> in "
9322e4b17023SJohn Marino "declaration specifiers"));
9323e4b17023SJohn Marino else if (specs->short_p)
9324e4b17023SJohn Marino error_at (loc,
9325e4b17023SJohn Marino ("both %<short%> and %<double%> in "
9326e4b17023SJohn Marino "declaration specifiers"));
9327e4b17023SJohn Marino else if (specs->signed_p)
9328e4b17023SJohn Marino error_at (loc,
9329e4b17023SJohn Marino ("both %<signed%> and %<double%> in "
9330e4b17023SJohn Marino "declaration specifiers"));
9331e4b17023SJohn Marino else if (specs->unsigned_p)
9332e4b17023SJohn Marino error_at (loc,
9333e4b17023SJohn Marino ("both %<unsigned%> and %<double%> in "
9334e4b17023SJohn Marino "declaration specifiers"));
9335e4b17023SJohn Marino else if (specs->saturating_p)
9336e4b17023SJohn Marino error_at (loc,
9337e4b17023SJohn Marino ("both %<_Sat%> and %<double%> in "
9338e4b17023SJohn Marino "declaration specifiers"));
9339e4b17023SJohn Marino else
9340e4b17023SJohn Marino specs->typespec_word = cts_double;
9341e4b17023SJohn Marino return specs;
9342e4b17023SJohn Marino case RID_DFLOAT32:
9343e4b17023SJohn Marino case RID_DFLOAT64:
9344e4b17023SJohn Marino case RID_DFLOAT128:
9345e4b17023SJohn Marino {
9346e4b17023SJohn Marino const char *str;
9347e4b17023SJohn Marino if (i == RID_DFLOAT32)
9348e4b17023SJohn Marino str = "_Decimal32";
9349e4b17023SJohn Marino else if (i == RID_DFLOAT64)
9350e4b17023SJohn Marino str = "_Decimal64";
9351e4b17023SJohn Marino else
9352e4b17023SJohn Marino str = "_Decimal128";
9353e4b17023SJohn Marino if (specs->long_long_p)
9354e4b17023SJohn Marino error_at (loc,
9355e4b17023SJohn Marino ("both %<long long%> and %<%s%> in "
9356e4b17023SJohn Marino "declaration specifiers"),
9357e4b17023SJohn Marino str);
9358e4b17023SJohn Marino if (specs->long_p)
9359e4b17023SJohn Marino error_at (loc,
9360e4b17023SJohn Marino ("both %<long%> and %<%s%> in "
9361e4b17023SJohn Marino "declaration specifiers"),
9362e4b17023SJohn Marino str);
9363e4b17023SJohn Marino else if (specs->short_p)
9364e4b17023SJohn Marino error_at (loc,
9365e4b17023SJohn Marino ("both %<short%> and %<%s%> in "
9366e4b17023SJohn Marino "declaration specifiers"),
9367e4b17023SJohn Marino str);
9368e4b17023SJohn Marino else if (specs->signed_p)
9369e4b17023SJohn Marino error_at (loc,
9370e4b17023SJohn Marino ("both %<signed%> and %<%s%> in "
9371e4b17023SJohn Marino "declaration specifiers"),
9372e4b17023SJohn Marino str);
9373e4b17023SJohn Marino else if (specs->unsigned_p)
9374e4b17023SJohn Marino error_at (loc,
9375e4b17023SJohn Marino ("both %<unsigned%> and %<%s%> in "
9376e4b17023SJohn Marino "declaration specifiers"),
9377e4b17023SJohn Marino str);
9378e4b17023SJohn Marino else if (specs->complex_p)
9379e4b17023SJohn Marino error_at (loc,
9380e4b17023SJohn Marino ("both %<complex%> and %<%s%> in "
9381e4b17023SJohn Marino "declaration specifiers"),
9382e4b17023SJohn Marino str);
9383e4b17023SJohn Marino else if (specs->saturating_p)
9384e4b17023SJohn Marino error_at (loc,
9385e4b17023SJohn Marino ("both %<_Sat%> and %<%s%> in "
9386e4b17023SJohn Marino "declaration specifiers"),
9387e4b17023SJohn Marino str);
9388e4b17023SJohn Marino else if (i == RID_DFLOAT32)
9389e4b17023SJohn Marino specs->typespec_word = cts_dfloat32;
9390e4b17023SJohn Marino else if (i == RID_DFLOAT64)
9391e4b17023SJohn Marino specs->typespec_word = cts_dfloat64;
9392e4b17023SJohn Marino else
9393e4b17023SJohn Marino specs->typespec_word = cts_dfloat128;
9394e4b17023SJohn Marino }
9395e4b17023SJohn Marino if (!targetm.decimal_float_supported_p ())
9396e4b17023SJohn Marino error_at (loc,
9397e4b17023SJohn Marino ("decimal floating point not supported "
9398e4b17023SJohn Marino "for this target"));
9399e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
9400e4b17023SJohn Marino "ISO C does not support decimal floating point");
9401e4b17023SJohn Marino return specs;
9402e4b17023SJohn Marino case RID_FRACT:
9403e4b17023SJohn Marino case RID_ACCUM:
9404e4b17023SJohn Marino {
9405e4b17023SJohn Marino const char *str;
9406e4b17023SJohn Marino if (i == RID_FRACT)
9407e4b17023SJohn Marino str = "_Fract";
9408e4b17023SJohn Marino else
9409e4b17023SJohn Marino str = "_Accum";
9410e4b17023SJohn Marino if (specs->complex_p)
9411e4b17023SJohn Marino error_at (loc,
9412e4b17023SJohn Marino ("both %<complex%> and %<%s%> in "
9413e4b17023SJohn Marino "declaration specifiers"),
9414e4b17023SJohn Marino str);
9415e4b17023SJohn Marino else if (i == RID_FRACT)
9416e4b17023SJohn Marino specs->typespec_word = cts_fract;
9417e4b17023SJohn Marino else
9418e4b17023SJohn Marino specs->typespec_word = cts_accum;
9419e4b17023SJohn Marino }
9420e4b17023SJohn Marino if (!targetm.fixed_point_supported_p ())
9421e4b17023SJohn Marino error_at (loc,
9422e4b17023SJohn Marino "fixed-point types not supported for this target");
9423e4b17023SJohn Marino pedwarn (loc, OPT_pedantic,
9424e4b17023SJohn Marino "ISO C does not support fixed-point types");
9425e4b17023SJohn Marino return specs;
9426e4b17023SJohn Marino default:
9427e4b17023SJohn Marino /* ObjC reserved word "id", handled below. */
9428e4b17023SJohn Marino break;
9429e4b17023SJohn Marino }
9430e4b17023SJohn Marino }
9431e4b17023SJohn Marino }
9432e4b17023SJohn Marino
9433e4b17023SJohn Marino /* Now we have a typedef (a TYPE_DECL node), an identifier (some
9434e4b17023SJohn Marino form of ObjC type, cases such as "int" and "long" being handled
9435e4b17023SJohn Marino above), a TYPE (struct, union, enum and typeof specifiers) or an
9436e4b17023SJohn Marino ERROR_MARK. In none of these cases may there have previously
9437e4b17023SJohn Marino been any type specifiers. */
9438e4b17023SJohn Marino if (specs->type || specs->typespec_word != cts_none
9439e4b17023SJohn Marino || specs->long_p || specs->short_p || specs->signed_p
9440e4b17023SJohn Marino || specs->unsigned_p || specs->complex_p)
9441e4b17023SJohn Marino error_at (loc, "two or more data types in declaration specifiers");
9442e4b17023SJohn Marino else if (TREE_CODE (type) == TYPE_DECL)
9443e4b17023SJohn Marino {
9444e4b17023SJohn Marino if (TREE_TYPE (type) == error_mark_node)
9445e4b17023SJohn Marino ; /* Allow the type to default to int to avoid cascading errors. */
9446e4b17023SJohn Marino else
9447e4b17023SJohn Marino {
9448e4b17023SJohn Marino specs->type = TREE_TYPE (type);
9449e4b17023SJohn Marino specs->decl_attr = DECL_ATTRIBUTES (type);
9450e4b17023SJohn Marino specs->typedef_p = true;
9451e4b17023SJohn Marino specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
9452e4b17023SJohn Marino
9453e4b17023SJohn Marino /* If this typedef name is defined in a struct, then a C++
9454e4b17023SJohn Marino lookup would return a different value. */
9455e4b17023SJohn Marino if (warn_cxx_compat
9456e4b17023SJohn Marino && I_SYMBOL_BINDING (DECL_NAME (type))->in_struct)
9457e4b17023SJohn Marino warning_at (loc, OPT_Wc___compat,
9458e4b17023SJohn Marino "C++ lookup of %qD would return a field, not a type",
9459e4b17023SJohn Marino type);
9460e4b17023SJohn Marino
9461e4b17023SJohn Marino /* If we are parsing a struct, record that a struct field
9462e4b17023SJohn Marino used a typedef. */
9463e4b17023SJohn Marino if (warn_cxx_compat && struct_parse_info != NULL)
9464e4b17023SJohn Marino VEC_safe_push (tree, heap, struct_parse_info->typedefs_seen, type);
9465e4b17023SJohn Marino }
9466e4b17023SJohn Marino }
9467e4b17023SJohn Marino else if (TREE_CODE (type) == IDENTIFIER_NODE)
9468e4b17023SJohn Marino {
9469e4b17023SJohn Marino tree t = lookup_name (type);
9470e4b17023SJohn Marino if (!t || TREE_CODE (t) != TYPE_DECL)
9471e4b17023SJohn Marino error_at (loc, "%qE fails to be a typedef or built in type", type);
9472e4b17023SJohn Marino else if (TREE_TYPE (t) == error_mark_node)
9473e4b17023SJohn Marino ;
9474e4b17023SJohn Marino else
9475e4b17023SJohn Marino specs->type = TREE_TYPE (t);
9476e4b17023SJohn Marino }
9477e4b17023SJohn Marino else
9478e4b17023SJohn Marino {
9479e4b17023SJohn Marino if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
9480e4b17023SJohn Marino {
9481e4b17023SJohn Marino specs->typedef_p = true;
9482e4b17023SJohn Marino if (spec.expr)
9483e4b17023SJohn Marino {
9484e4b17023SJohn Marino if (specs->expr)
9485e4b17023SJohn Marino specs->expr = build2 (COMPOUND_EXPR, TREE_TYPE (spec.expr),
9486e4b17023SJohn Marino specs->expr, spec.expr);
9487e4b17023SJohn Marino else
9488e4b17023SJohn Marino specs->expr = spec.expr;
9489e4b17023SJohn Marino specs->expr_const_operands &= spec.expr_const_operands;
9490e4b17023SJohn Marino }
9491e4b17023SJohn Marino }
9492e4b17023SJohn Marino specs->type = type;
9493e4b17023SJohn Marino }
9494e4b17023SJohn Marino
9495e4b17023SJohn Marino return specs;
9496e4b17023SJohn Marino }
9497e4b17023SJohn Marino
9498e4b17023SJohn Marino /* Add the storage class specifier or function specifier SCSPEC to the
9499e4b17023SJohn Marino declaration specifiers SPECS, returning SPECS. */
9500e4b17023SJohn Marino
9501e4b17023SJohn Marino struct c_declspecs *
declspecs_add_scspec(struct c_declspecs * specs,tree scspec)9502e4b17023SJohn Marino declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
9503e4b17023SJohn Marino {
9504e4b17023SJohn Marino enum rid i;
9505e4b17023SJohn Marino enum c_storage_class n = csc_none;
9506e4b17023SJohn Marino bool dupe = false;
9507e4b17023SJohn Marino specs->declspecs_seen_p = true;
9508e4b17023SJohn Marino gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
9509e4b17023SJohn Marino && C_IS_RESERVED_WORD (scspec));
9510e4b17023SJohn Marino i = C_RID_CODE (scspec);
9511e4b17023SJohn Marino if (specs->non_sc_seen_p)
9512e4b17023SJohn Marino warning (OPT_Wold_style_declaration,
9513e4b17023SJohn Marino "%qE is not at beginning of declaration", scspec);
9514e4b17023SJohn Marino switch (i)
9515e4b17023SJohn Marino {
9516e4b17023SJohn Marino case RID_INLINE:
9517e4b17023SJohn Marino /* C99 permits duplicate inline. Although of doubtful utility,
9518e4b17023SJohn Marino it seems simplest to permit it in gnu89 mode as well, as
9519e4b17023SJohn Marino there is also little utility in maintaining this as a
9520e4b17023SJohn Marino difference between gnu89 and C99 inline. */
9521e4b17023SJohn Marino dupe = false;
9522e4b17023SJohn Marino specs->inline_p = true;
9523e4b17023SJohn Marino break;
9524e4b17023SJohn Marino case RID_NORETURN:
9525e4b17023SJohn Marino /* Duplicate _Noreturn is permitted. */
9526e4b17023SJohn Marino dupe = false;
9527e4b17023SJohn Marino specs->noreturn_p = true;
9528e4b17023SJohn Marino break;
9529e4b17023SJohn Marino case RID_THREAD:
9530e4b17023SJohn Marino dupe = specs->thread_p;
9531e4b17023SJohn Marino if (specs->storage_class == csc_auto)
9532e4b17023SJohn Marino error ("%<__thread%> used with %<auto%>");
9533e4b17023SJohn Marino else if (specs->storage_class == csc_register)
9534e4b17023SJohn Marino error ("%<__thread%> used with %<register%>");
9535e4b17023SJohn Marino else if (specs->storage_class == csc_typedef)
9536e4b17023SJohn Marino error ("%<__thread%> used with %<typedef%>");
9537e4b17023SJohn Marino else
9538e4b17023SJohn Marino specs->thread_p = true;
9539e4b17023SJohn Marino break;
9540e4b17023SJohn Marino case RID_AUTO:
9541e4b17023SJohn Marino n = csc_auto;
9542e4b17023SJohn Marino break;
9543e4b17023SJohn Marino case RID_EXTERN:
9544e4b17023SJohn Marino n = csc_extern;
9545e4b17023SJohn Marino /* Diagnose "__thread extern". */
9546e4b17023SJohn Marino if (specs->thread_p)
9547e4b17023SJohn Marino error ("%<__thread%> before %<extern%>");
9548e4b17023SJohn Marino break;
9549e4b17023SJohn Marino case RID_REGISTER:
9550e4b17023SJohn Marino n = csc_register;
9551e4b17023SJohn Marino break;
9552e4b17023SJohn Marino case RID_STATIC:
9553e4b17023SJohn Marino n = csc_static;
9554e4b17023SJohn Marino /* Diagnose "__thread static". */
9555e4b17023SJohn Marino if (specs->thread_p)
9556e4b17023SJohn Marino error ("%<__thread%> before %<static%>");
9557e4b17023SJohn Marino break;
9558e4b17023SJohn Marino case RID_TYPEDEF:
9559e4b17023SJohn Marino n = csc_typedef;
9560e4b17023SJohn Marino break;
9561e4b17023SJohn Marino default:
9562e4b17023SJohn Marino gcc_unreachable ();
9563e4b17023SJohn Marino }
9564e4b17023SJohn Marino if (n != csc_none && n == specs->storage_class)
9565e4b17023SJohn Marino dupe = true;
9566e4b17023SJohn Marino if (dupe)
9567e4b17023SJohn Marino error ("duplicate %qE", scspec);
9568e4b17023SJohn Marino if (n != csc_none)
9569e4b17023SJohn Marino {
9570e4b17023SJohn Marino if (specs->storage_class != csc_none && n != specs->storage_class)
9571e4b17023SJohn Marino {
9572e4b17023SJohn Marino error ("multiple storage classes in declaration specifiers");
9573e4b17023SJohn Marino }
9574e4b17023SJohn Marino else
9575e4b17023SJohn Marino {
9576e4b17023SJohn Marino specs->storage_class = n;
9577e4b17023SJohn Marino if (n != csc_extern && n != csc_static && specs->thread_p)
9578e4b17023SJohn Marino {
9579e4b17023SJohn Marino error ("%<__thread%> used with %qE", scspec);
9580e4b17023SJohn Marino specs->thread_p = false;
9581e4b17023SJohn Marino }
9582e4b17023SJohn Marino }
9583e4b17023SJohn Marino }
9584e4b17023SJohn Marino return specs;
9585e4b17023SJohn Marino }
9586e4b17023SJohn Marino
9587e4b17023SJohn Marino /* Add the attributes ATTRS to the declaration specifiers SPECS,
9588e4b17023SJohn Marino returning SPECS. */
9589e4b17023SJohn Marino
9590e4b17023SJohn Marino struct c_declspecs *
declspecs_add_attrs(struct c_declspecs * specs,tree attrs)9591e4b17023SJohn Marino declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
9592e4b17023SJohn Marino {
9593e4b17023SJohn Marino specs->attrs = chainon (attrs, specs->attrs);
9594e4b17023SJohn Marino specs->declspecs_seen_p = true;
9595e4b17023SJohn Marino return specs;
9596e4b17023SJohn Marino }
9597e4b17023SJohn Marino
9598e4b17023SJohn Marino /* Add an _Alignas specifier (expression ALIGN, or type whose
9599e4b17023SJohn Marino alignment is ALIGN) to the declaration specifiers SPECS, returning
9600e4b17023SJohn Marino SPECS. */
9601e4b17023SJohn Marino struct c_declspecs *
declspecs_add_alignas(struct c_declspecs * specs,tree align)9602e4b17023SJohn Marino declspecs_add_alignas (struct c_declspecs *specs, tree align)
9603e4b17023SJohn Marino {
9604e4b17023SJohn Marino int align_log;
9605e4b17023SJohn Marino specs->alignas_p = true;
9606e4b17023SJohn Marino if (align == error_mark_node)
9607e4b17023SJohn Marino return specs;
9608e4b17023SJohn Marino align_log = check_user_alignment (align, true);
9609e4b17023SJohn Marino if (align_log > specs->align_log)
9610e4b17023SJohn Marino specs->align_log = align_log;
9611e4b17023SJohn Marino return specs;
9612e4b17023SJohn Marino }
9613e4b17023SJohn Marino
9614e4b17023SJohn Marino /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
9615e4b17023SJohn Marino specifiers with any other type specifier to determine the resulting
9616e4b17023SJohn Marino type. This is where ISO C checks on complex types are made, since
9617e4b17023SJohn Marino "_Complex long" is a prefix of the valid ISO C type "_Complex long
9618e4b17023SJohn Marino double". */
9619e4b17023SJohn Marino
9620e4b17023SJohn Marino struct c_declspecs *
finish_declspecs(struct c_declspecs * specs)9621e4b17023SJohn Marino finish_declspecs (struct c_declspecs *specs)
9622e4b17023SJohn Marino {
9623e4b17023SJohn Marino /* If a type was specified as a whole, we have no modifiers and are
9624e4b17023SJohn Marino done. */
9625e4b17023SJohn Marino if (specs->type != NULL_TREE)
9626e4b17023SJohn Marino {
9627e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9628e4b17023SJohn Marino && !specs->signed_p && !specs->unsigned_p
9629e4b17023SJohn Marino && !specs->complex_p);
9630e4b17023SJohn Marino
9631e4b17023SJohn Marino /* Set a dummy type. */
9632e4b17023SJohn Marino if (TREE_CODE (specs->type) == ERROR_MARK)
9633e4b17023SJohn Marino specs->type = integer_type_node;
9634e4b17023SJohn Marino return specs;
9635e4b17023SJohn Marino }
9636e4b17023SJohn Marino
9637e4b17023SJohn Marino /* If none of "void", "_Bool", "char", "int", "float" or "double"
9638e4b17023SJohn Marino has been specified, treat it as "int" unless "_Complex" is
9639e4b17023SJohn Marino present and there are no other specifiers. If we just have
9640e4b17023SJohn Marino "_Complex", it is equivalent to "_Complex double", but e.g.
9641e4b17023SJohn Marino "_Complex short" is equivalent to "_Complex short int". */
9642e4b17023SJohn Marino if (specs->typespec_word == cts_none)
9643e4b17023SJohn Marino {
9644e4b17023SJohn Marino if (specs->saturating_p)
9645e4b17023SJohn Marino {
9646e4b17023SJohn Marino error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
9647e4b17023SJohn Marino if (!targetm.fixed_point_supported_p ())
9648e4b17023SJohn Marino error ("fixed-point types not supported for this target");
9649e4b17023SJohn Marino specs->typespec_word = cts_fract;
9650e4b17023SJohn Marino }
9651e4b17023SJohn Marino else if (specs->long_p || specs->short_p
9652e4b17023SJohn Marino || specs->signed_p || specs->unsigned_p)
9653e4b17023SJohn Marino {
9654e4b17023SJohn Marino specs->typespec_word = cts_int;
9655e4b17023SJohn Marino }
9656e4b17023SJohn Marino else if (specs->complex_p)
9657e4b17023SJohn Marino {
9658e4b17023SJohn Marino specs->typespec_word = cts_double;
9659e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
9660e4b17023SJohn Marino "ISO C does not support plain %<complex%> meaning "
9661e4b17023SJohn Marino "%<double complex%>");
9662e4b17023SJohn Marino }
9663e4b17023SJohn Marino else
9664e4b17023SJohn Marino {
9665e4b17023SJohn Marino specs->typespec_word = cts_int;
9666e4b17023SJohn Marino specs->default_int_p = true;
9667e4b17023SJohn Marino /* We don't diagnose this here because grokdeclarator will
9668e4b17023SJohn Marino give more specific diagnostics according to whether it is
9669e4b17023SJohn Marino a function definition. */
9670e4b17023SJohn Marino }
9671e4b17023SJohn Marino }
9672e4b17023SJohn Marino
9673e4b17023SJohn Marino /* If "signed" was specified, record this to distinguish "int" and
9674e4b17023SJohn Marino "signed int" in the case of a bit-field with
9675e4b17023SJohn Marino -funsigned-bitfields. */
9676e4b17023SJohn Marino specs->explicit_signed_p = specs->signed_p;
9677e4b17023SJohn Marino
9678e4b17023SJohn Marino /* Now compute the actual type. */
9679e4b17023SJohn Marino switch (specs->typespec_word)
9680e4b17023SJohn Marino {
9681e4b17023SJohn Marino case cts_void:
9682e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->short_p
9683e4b17023SJohn Marino && !specs->signed_p && !specs->unsigned_p
9684e4b17023SJohn Marino && !specs->complex_p);
9685e4b17023SJohn Marino specs->type = void_type_node;
9686e4b17023SJohn Marino break;
9687e4b17023SJohn Marino case cts_bool:
9688e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->short_p
9689e4b17023SJohn Marino && !specs->signed_p && !specs->unsigned_p
9690e4b17023SJohn Marino && !specs->complex_p);
9691e4b17023SJohn Marino specs->type = boolean_type_node;
9692e4b17023SJohn Marino break;
9693e4b17023SJohn Marino case cts_char:
9694e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->short_p);
9695e4b17023SJohn Marino gcc_assert (!(specs->signed_p && specs->unsigned_p));
9696e4b17023SJohn Marino if (specs->signed_p)
9697e4b17023SJohn Marino specs->type = signed_char_type_node;
9698e4b17023SJohn Marino else if (specs->unsigned_p)
9699e4b17023SJohn Marino specs->type = unsigned_char_type_node;
9700e4b17023SJohn Marino else
9701e4b17023SJohn Marino specs->type = char_type_node;
9702e4b17023SJohn Marino if (specs->complex_p)
9703e4b17023SJohn Marino {
9704e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
9705e4b17023SJohn Marino "ISO C does not support complex integer types");
9706e4b17023SJohn Marino specs->type = build_complex_type (specs->type);
9707e4b17023SJohn Marino }
9708e4b17023SJohn Marino break;
9709e4b17023SJohn Marino case cts_int128:
9710e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->short_p && !specs->long_long_p);
9711e4b17023SJohn Marino gcc_assert (!(specs->signed_p && specs->unsigned_p));
9712e4b17023SJohn Marino specs->type = (specs->unsigned_p
9713e4b17023SJohn Marino ? int128_unsigned_type_node
9714e4b17023SJohn Marino : int128_integer_type_node);
9715e4b17023SJohn Marino if (specs->complex_p)
9716e4b17023SJohn Marino {
9717e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
9718e4b17023SJohn Marino "ISO C does not support complex integer types");
9719e4b17023SJohn Marino specs->type = build_complex_type (specs->type);
9720e4b17023SJohn Marino }
9721e4b17023SJohn Marino break;
9722e4b17023SJohn Marino case cts_int:
9723e4b17023SJohn Marino gcc_assert (!(specs->long_p && specs->short_p));
9724e4b17023SJohn Marino gcc_assert (!(specs->signed_p && specs->unsigned_p));
9725e4b17023SJohn Marino if (specs->long_long_p)
9726e4b17023SJohn Marino specs->type = (specs->unsigned_p
9727e4b17023SJohn Marino ? long_long_unsigned_type_node
9728e4b17023SJohn Marino : long_long_integer_type_node);
9729e4b17023SJohn Marino else if (specs->long_p)
9730e4b17023SJohn Marino specs->type = (specs->unsigned_p
9731e4b17023SJohn Marino ? long_unsigned_type_node
9732e4b17023SJohn Marino : long_integer_type_node);
9733e4b17023SJohn Marino else if (specs->short_p)
9734e4b17023SJohn Marino specs->type = (specs->unsigned_p
9735e4b17023SJohn Marino ? short_unsigned_type_node
9736e4b17023SJohn Marino : short_integer_type_node);
9737e4b17023SJohn Marino else
9738e4b17023SJohn Marino specs->type = (specs->unsigned_p
9739e4b17023SJohn Marino ? unsigned_type_node
9740e4b17023SJohn Marino : integer_type_node);
9741e4b17023SJohn Marino if (specs->complex_p)
9742e4b17023SJohn Marino {
9743e4b17023SJohn Marino pedwarn (input_location, OPT_pedantic,
9744e4b17023SJohn Marino "ISO C does not support complex integer types");
9745e4b17023SJohn Marino specs->type = build_complex_type (specs->type);
9746e4b17023SJohn Marino }
9747e4b17023SJohn Marino break;
9748e4b17023SJohn Marino case cts_float:
9749e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->short_p
9750e4b17023SJohn Marino && !specs->signed_p && !specs->unsigned_p);
9751e4b17023SJohn Marino specs->type = (specs->complex_p
9752e4b17023SJohn Marino ? complex_float_type_node
9753e4b17023SJohn Marino : float_type_node);
9754e4b17023SJohn Marino break;
9755e4b17023SJohn Marino case cts_double:
9756e4b17023SJohn Marino gcc_assert (!specs->long_long_p && !specs->short_p
9757e4b17023SJohn Marino && !specs->signed_p && !specs->unsigned_p);
9758e4b17023SJohn Marino if (specs->long_p)
9759e4b17023SJohn Marino {
9760e4b17023SJohn Marino specs->type = (specs->complex_p
9761e4b17023SJohn Marino ? complex_long_double_type_node
9762e4b17023SJohn Marino : long_double_type_node);
9763e4b17023SJohn Marino }
9764e4b17023SJohn Marino else
9765e4b17023SJohn Marino {
9766e4b17023SJohn Marino specs->type = (specs->complex_p
9767e4b17023SJohn Marino ? complex_double_type_node
9768e4b17023SJohn Marino : double_type_node);
9769e4b17023SJohn Marino }
9770e4b17023SJohn Marino break;
9771e4b17023SJohn Marino case cts_dfloat32:
9772e4b17023SJohn Marino case cts_dfloat64:
9773e4b17023SJohn Marino case cts_dfloat128:
9774e4b17023SJohn Marino gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
9775e4b17023SJohn Marino && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
9776e4b17023SJohn Marino if (specs->typespec_word == cts_dfloat32)
9777e4b17023SJohn Marino specs->type = dfloat32_type_node;
9778e4b17023SJohn Marino else if (specs->typespec_word == cts_dfloat64)
9779e4b17023SJohn Marino specs->type = dfloat64_type_node;
9780e4b17023SJohn Marino else
9781e4b17023SJohn Marino specs->type = dfloat128_type_node;
9782e4b17023SJohn Marino break;
9783e4b17023SJohn Marino case cts_fract:
9784e4b17023SJohn Marino gcc_assert (!specs->complex_p);
9785e4b17023SJohn Marino if (!targetm.fixed_point_supported_p ())
9786e4b17023SJohn Marino specs->type = integer_type_node;
9787e4b17023SJohn Marino else if (specs->saturating_p)
9788e4b17023SJohn Marino {
9789e4b17023SJohn Marino if (specs->long_long_p)
9790e4b17023SJohn Marino specs->type = specs->unsigned_p
9791e4b17023SJohn Marino ? sat_unsigned_long_long_fract_type_node
9792e4b17023SJohn Marino : sat_long_long_fract_type_node;
9793e4b17023SJohn Marino else if (specs->long_p)
9794e4b17023SJohn Marino specs->type = specs->unsigned_p
9795e4b17023SJohn Marino ? sat_unsigned_long_fract_type_node
9796e4b17023SJohn Marino : sat_long_fract_type_node;
9797e4b17023SJohn Marino else if (specs->short_p)
9798e4b17023SJohn Marino specs->type = specs->unsigned_p
9799e4b17023SJohn Marino ? sat_unsigned_short_fract_type_node
9800e4b17023SJohn Marino : sat_short_fract_type_node;
9801e4b17023SJohn Marino else
9802e4b17023SJohn Marino specs->type = specs->unsigned_p
9803e4b17023SJohn Marino ? sat_unsigned_fract_type_node
9804e4b17023SJohn Marino : sat_fract_type_node;
9805e4b17023SJohn Marino }
9806e4b17023SJohn Marino else
9807e4b17023SJohn Marino {
9808e4b17023SJohn Marino if (specs->long_long_p)
9809e4b17023SJohn Marino specs->type = specs->unsigned_p
9810e4b17023SJohn Marino ? unsigned_long_long_fract_type_node
9811e4b17023SJohn Marino : long_long_fract_type_node;
9812e4b17023SJohn Marino else if (specs->long_p)
9813e4b17023SJohn Marino specs->type = specs->unsigned_p
9814e4b17023SJohn Marino ? unsigned_long_fract_type_node
9815e4b17023SJohn Marino : long_fract_type_node;
9816e4b17023SJohn Marino else if (specs->short_p)
9817e4b17023SJohn Marino specs->type = specs->unsigned_p
9818e4b17023SJohn Marino ? unsigned_short_fract_type_node
9819e4b17023SJohn Marino : short_fract_type_node;
9820e4b17023SJohn Marino else
9821e4b17023SJohn Marino specs->type = specs->unsigned_p
9822e4b17023SJohn Marino ? unsigned_fract_type_node
9823e4b17023SJohn Marino : fract_type_node;
9824e4b17023SJohn Marino }
9825e4b17023SJohn Marino break;
9826e4b17023SJohn Marino case cts_accum:
9827e4b17023SJohn Marino gcc_assert (!specs->complex_p);
9828e4b17023SJohn Marino if (!targetm.fixed_point_supported_p ())
9829e4b17023SJohn Marino specs->type = integer_type_node;
9830e4b17023SJohn Marino else if (specs->saturating_p)
9831e4b17023SJohn Marino {
9832e4b17023SJohn Marino if (specs->long_long_p)
9833e4b17023SJohn Marino specs->type = specs->unsigned_p
9834e4b17023SJohn Marino ? sat_unsigned_long_long_accum_type_node
9835e4b17023SJohn Marino : sat_long_long_accum_type_node;
9836e4b17023SJohn Marino else if (specs->long_p)
9837e4b17023SJohn Marino specs->type = specs->unsigned_p
9838e4b17023SJohn Marino ? sat_unsigned_long_accum_type_node
9839e4b17023SJohn Marino : sat_long_accum_type_node;
9840e4b17023SJohn Marino else if (specs->short_p)
9841e4b17023SJohn Marino specs->type = specs->unsigned_p
9842e4b17023SJohn Marino ? sat_unsigned_short_accum_type_node
9843e4b17023SJohn Marino : sat_short_accum_type_node;
9844e4b17023SJohn Marino else
9845e4b17023SJohn Marino specs->type = specs->unsigned_p
9846e4b17023SJohn Marino ? sat_unsigned_accum_type_node
9847e4b17023SJohn Marino : sat_accum_type_node;
9848e4b17023SJohn Marino }
9849e4b17023SJohn Marino else
9850e4b17023SJohn Marino {
9851e4b17023SJohn Marino if (specs->long_long_p)
9852e4b17023SJohn Marino specs->type = specs->unsigned_p
9853e4b17023SJohn Marino ? unsigned_long_long_accum_type_node
9854e4b17023SJohn Marino : long_long_accum_type_node;
9855e4b17023SJohn Marino else if (specs->long_p)
9856e4b17023SJohn Marino specs->type = specs->unsigned_p
9857e4b17023SJohn Marino ? unsigned_long_accum_type_node
9858e4b17023SJohn Marino : long_accum_type_node;
9859e4b17023SJohn Marino else if (specs->short_p)
9860e4b17023SJohn Marino specs->type = specs->unsigned_p
9861e4b17023SJohn Marino ? unsigned_short_accum_type_node
9862e4b17023SJohn Marino : short_accum_type_node;
9863e4b17023SJohn Marino else
9864e4b17023SJohn Marino specs->type = specs->unsigned_p
9865e4b17023SJohn Marino ? unsigned_accum_type_node
9866e4b17023SJohn Marino : accum_type_node;
9867e4b17023SJohn Marino }
9868e4b17023SJohn Marino break;
9869e4b17023SJohn Marino default:
9870e4b17023SJohn Marino gcc_unreachable ();
9871e4b17023SJohn Marino }
9872e4b17023SJohn Marino
9873e4b17023SJohn Marino return specs;
9874e4b17023SJohn Marino }
9875e4b17023SJohn Marino
9876e4b17023SJohn Marino /* A subroutine of c_write_global_declarations. Perform final processing
9877e4b17023SJohn Marino on one file scope's declarations (or the external scope's declarations),
9878e4b17023SJohn Marino GLOBALS. */
9879e4b17023SJohn Marino
9880e4b17023SJohn Marino static void
c_write_global_declarations_1(tree globals)9881e4b17023SJohn Marino c_write_global_declarations_1 (tree globals)
9882e4b17023SJohn Marino {
9883e4b17023SJohn Marino tree decl;
9884e4b17023SJohn Marino bool reconsider;
9885e4b17023SJohn Marino
9886e4b17023SJohn Marino /* Process the decls in the order they were written. */
9887e4b17023SJohn Marino for (decl = globals; decl; decl = DECL_CHAIN (decl))
9888e4b17023SJohn Marino {
9889e4b17023SJohn Marino /* Check for used but undefined static functions using the C
9890e4b17023SJohn Marino standard's definition of "used", and set TREE_NO_WARNING so
9891e4b17023SJohn Marino that check_global_declarations doesn't repeat the check. */
9892e4b17023SJohn Marino if (TREE_CODE (decl) == FUNCTION_DECL
9893e4b17023SJohn Marino && DECL_INITIAL (decl) == 0
9894e4b17023SJohn Marino && DECL_EXTERNAL (decl)
9895e4b17023SJohn Marino && !TREE_PUBLIC (decl)
9896e4b17023SJohn Marino && C_DECL_USED (decl))
9897e4b17023SJohn Marino {
9898e4b17023SJohn Marino pedwarn (input_location, 0, "%q+F used but never defined", decl);
9899e4b17023SJohn Marino TREE_NO_WARNING (decl) = 1;
9900e4b17023SJohn Marino }
9901e4b17023SJohn Marino
9902e4b17023SJohn Marino wrapup_global_declaration_1 (decl);
9903e4b17023SJohn Marino }
9904e4b17023SJohn Marino
9905e4b17023SJohn Marino do
9906e4b17023SJohn Marino {
9907e4b17023SJohn Marino reconsider = false;
9908e4b17023SJohn Marino for (decl = globals; decl; decl = DECL_CHAIN (decl))
9909e4b17023SJohn Marino reconsider |= wrapup_global_declaration_2 (decl);
9910e4b17023SJohn Marino }
9911e4b17023SJohn Marino while (reconsider);
9912e4b17023SJohn Marino
9913e4b17023SJohn Marino for (decl = globals; decl; decl = DECL_CHAIN (decl))
9914e4b17023SJohn Marino check_global_declaration_1 (decl);
9915e4b17023SJohn Marino }
9916e4b17023SJohn Marino
9917e4b17023SJohn Marino /* A subroutine of c_write_global_declarations Emit debug information for each
9918e4b17023SJohn Marino of the declarations in GLOBALS. */
9919e4b17023SJohn Marino
9920e4b17023SJohn Marino static void
c_write_global_declarations_2(tree globals)9921e4b17023SJohn Marino c_write_global_declarations_2 (tree globals)
9922e4b17023SJohn Marino {
9923e4b17023SJohn Marino tree decl;
9924e4b17023SJohn Marino
9925e4b17023SJohn Marino for (decl = globals; decl ; decl = DECL_CHAIN (decl))
9926e4b17023SJohn Marino debug_hooks->global_decl (decl);
9927e4b17023SJohn Marino }
9928e4b17023SJohn Marino
9929e4b17023SJohn Marino /* Callback to collect a source_ref from a DECL. */
9930e4b17023SJohn Marino
9931e4b17023SJohn Marino static void
collect_source_ref_cb(tree decl)9932e4b17023SJohn Marino collect_source_ref_cb (tree decl)
9933e4b17023SJohn Marino {
9934e4b17023SJohn Marino if (!DECL_IS_BUILTIN (decl))
9935e4b17023SJohn Marino collect_source_ref (LOCATION_FILE (decl_sloc (decl, false)));
9936e4b17023SJohn Marino }
9937e4b17023SJohn Marino
9938e4b17023SJohn Marino /* Preserve the external declarations scope across a garbage collect. */
9939e4b17023SJohn Marino static GTY(()) tree ext_block;
9940e4b17023SJohn Marino
9941e4b17023SJohn Marino /* Collect all references relevant to SOURCE_FILE. */
9942e4b17023SJohn Marino
9943e4b17023SJohn Marino static void
collect_all_refs(const char * source_file)9944e4b17023SJohn Marino collect_all_refs (const char *source_file)
9945e4b17023SJohn Marino {
9946e4b17023SJohn Marino tree t;
9947e4b17023SJohn Marino unsigned i;
9948e4b17023SJohn Marino
9949e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9950e4b17023SJohn Marino collect_ada_nodes (BLOCK_VARS (DECL_INITIAL (t)), source_file);
9951e4b17023SJohn Marino
9952e4b17023SJohn Marino collect_ada_nodes (BLOCK_VARS (ext_block), source_file);
9953e4b17023SJohn Marino }
9954e4b17023SJohn Marino
9955e4b17023SJohn Marino /* Iterate over all global declarations and call CALLBACK. */
9956e4b17023SJohn Marino
9957e4b17023SJohn Marino static void
for_each_global_decl(void (* callback)(tree decl))9958e4b17023SJohn Marino for_each_global_decl (void (*callback) (tree decl))
9959e4b17023SJohn Marino {
9960e4b17023SJohn Marino tree t;
9961e4b17023SJohn Marino tree decls;
9962e4b17023SJohn Marino tree decl;
9963e4b17023SJohn Marino unsigned i;
9964e4b17023SJohn Marino
9965e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
9966e4b17023SJohn Marino {
9967e4b17023SJohn Marino decls = DECL_INITIAL (t);
9968e4b17023SJohn Marino for (decl = BLOCK_VARS (decls); decl; decl = TREE_CHAIN (decl))
9969e4b17023SJohn Marino callback (decl);
9970e4b17023SJohn Marino }
9971e4b17023SJohn Marino
9972e4b17023SJohn Marino for (decl = BLOCK_VARS (ext_block); decl; decl = TREE_CHAIN (decl))
9973e4b17023SJohn Marino callback (decl);
9974e4b17023SJohn Marino }
9975e4b17023SJohn Marino
9976e4b17023SJohn Marino void
c_write_global_declarations(void)9977e4b17023SJohn Marino c_write_global_declarations (void)
9978e4b17023SJohn Marino {
9979e4b17023SJohn Marino tree t;
9980e4b17023SJohn Marino unsigned i;
9981e4b17023SJohn Marino
9982e4b17023SJohn Marino /* We don't want to do this if generating a PCH. */
9983e4b17023SJohn Marino if (pch_file)
9984e4b17023SJohn Marino return;
9985e4b17023SJohn Marino
9986e4b17023SJohn Marino timevar_start (TV_PHASE_DEFERRED);
9987e4b17023SJohn Marino
9988e4b17023SJohn Marino /* Do the Objective-C stuff. This is where all the Objective-C
9989e4b17023SJohn Marino module stuff gets generated (symtab, class/protocol/selector
9990e4b17023SJohn Marino lists etc). */
9991e4b17023SJohn Marino if (c_dialect_objc ())
9992e4b17023SJohn Marino objc_write_global_declarations ();
9993e4b17023SJohn Marino
9994e4b17023SJohn Marino /* Close the external scope. */
9995e4b17023SJohn Marino ext_block = pop_scope ();
9996e4b17023SJohn Marino external_scope = 0;
9997e4b17023SJohn Marino gcc_assert (!current_scope);
9998e4b17023SJohn Marino
9999e4b17023SJohn Marino /* Handle -fdump-ada-spec[-slim]. */
10000e4b17023SJohn Marino if (dump_enabled_p (TDI_ada))
10001e4b17023SJohn Marino {
10002e4b17023SJohn Marino /* Build a table of files to generate specs for */
10003e4b17023SJohn Marino if (get_dump_file_info (TDI_ada)->flags & TDF_SLIM)
10004e4b17023SJohn Marino collect_source_ref (main_input_filename);
10005e4b17023SJohn Marino else
10006e4b17023SJohn Marino for_each_global_decl (collect_source_ref_cb);
10007e4b17023SJohn Marino
10008e4b17023SJohn Marino dump_ada_specs (collect_all_refs, NULL);
10009e4b17023SJohn Marino }
10010e4b17023SJohn Marino
10011e4b17023SJohn Marino if (ext_block)
10012e4b17023SJohn Marino {
10013e4b17023SJohn Marino tree tmp = BLOCK_VARS (ext_block);
10014e4b17023SJohn Marino int flags;
10015e4b17023SJohn Marino FILE * stream = dump_begin (TDI_tu, &flags);
10016e4b17023SJohn Marino if (stream && tmp)
10017e4b17023SJohn Marino {
10018e4b17023SJohn Marino dump_node (tmp, flags & ~TDF_SLIM, stream);
10019e4b17023SJohn Marino dump_end (TDI_tu, stream);
10020e4b17023SJohn Marino }
10021e4b17023SJohn Marino }
10022e4b17023SJohn Marino
10023e4b17023SJohn Marino /* Process all file scopes in this compilation, and the external_scope,
10024e4b17023SJohn Marino through wrapup_global_declarations and check_global_declarations. */
10025e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
10026e4b17023SJohn Marino c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
10027e4b17023SJohn Marino c_write_global_declarations_1 (BLOCK_VARS (ext_block));
10028e4b17023SJohn Marino
10029e4b17023SJohn Marino timevar_stop (TV_PHASE_DEFERRED);
10030e4b17023SJohn Marino timevar_start (TV_PHASE_CGRAPH);
10031e4b17023SJohn Marino
10032e4b17023SJohn Marino /* We're done parsing; proceed to optimize and emit assembly.
10033e4b17023SJohn Marino FIXME: shouldn't be the front end's responsibility to call this. */
10034e4b17023SJohn Marino cgraph_finalize_compilation_unit ();
10035e4b17023SJohn Marino
10036e4b17023SJohn Marino timevar_stop (TV_PHASE_CGRAPH);
10037e4b17023SJohn Marino timevar_start (TV_PHASE_DBGINFO);
10038e4b17023SJohn Marino
10039e4b17023SJohn Marino /* After cgraph has had a chance to emit everything that's going to
10040e4b17023SJohn Marino be emitted, output debug information for globals. */
10041e4b17023SJohn Marino if (!seen_error ())
10042e4b17023SJohn Marino {
10043e4b17023SJohn Marino timevar_push (TV_SYMOUT);
10044e4b17023SJohn Marino FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
10045e4b17023SJohn Marino c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
10046e4b17023SJohn Marino c_write_global_declarations_2 (BLOCK_VARS (ext_block));
10047e4b17023SJohn Marino timevar_pop (TV_SYMOUT);
10048e4b17023SJohn Marino }
10049e4b17023SJohn Marino
10050e4b17023SJohn Marino ext_block = NULL;
10051e4b17023SJohn Marino timevar_stop (TV_PHASE_DBGINFO);
10052e4b17023SJohn Marino }
10053e4b17023SJohn Marino
10054e4b17023SJohn Marino /* Register reserved keyword WORD as qualifier for address space AS. */
10055e4b17023SJohn Marino
10056e4b17023SJohn Marino void
c_register_addr_space(const char * word,addr_space_t as)10057e4b17023SJohn Marino c_register_addr_space (const char *word, addr_space_t as)
10058e4b17023SJohn Marino {
10059e4b17023SJohn Marino int rid = RID_FIRST_ADDR_SPACE + as;
10060e4b17023SJohn Marino tree id;
10061e4b17023SJohn Marino
10062e4b17023SJohn Marino /* Address space qualifiers are only supported
10063e4b17023SJohn Marino in C with GNU extensions enabled. */
10064e4b17023SJohn Marino if (c_dialect_objc () || flag_no_asm)
10065e4b17023SJohn Marino return;
10066e4b17023SJohn Marino
10067e4b17023SJohn Marino id = get_identifier (word);
10068e4b17023SJohn Marino C_SET_RID_CODE (id, rid);
10069e4b17023SJohn Marino C_IS_RESERVED_WORD (id) = 1;
10070e4b17023SJohn Marino ridpointers [rid] = id;
10071e4b17023SJohn Marino }
10072e4b17023SJohn Marino
10073e4b17023SJohn Marino #include "gt-c-decl.h"
10074