]>
Commit | Line | Data |
---|---|---|
51e29401 | 1 | /* Process declarations and variables for C compiler. |
21c7361e | 2 | Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
85b58ca5 | 3 | 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
51e29401 | 4 | |
1322177d | 5 | This file is part of GCC. |
51e29401 | 6 | |
1322177d LB |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
51e29401 | 11 | |
1322177d LB |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
51e29401 RS |
16 | |
17 | You should have received a copy of the GNU General Public License | |
1322177d LB |
18 | along with GCC; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
51e29401 | 21 | |
51e29401 RS |
22 | /* Process declarations and symbol lookup for C front end. |
23 | Also constructs types; the standard scalar types at initialization, | |
24 | and structure, union, array and enum types when they are declared. */ | |
25 | ||
26 | /* ??? not all decl nodes are given the most useful possible | |
27 | line numbers. For example, the CONST_DECLs for enum values. */ | |
28 | ||
29 | #include "config.h" | |
670ee920 | 30 | #include "system.h" |
4977bab6 ZW |
31 | #include "coretypes.h" |
32 | #include "tm.h" | |
993c790e | 33 | #include "intl.h" |
51e29401 | 34 | #include "tree.h" |
4838c5ee | 35 | #include "tree-inline.h" |
051c57da | 36 | #include "rtl.h" |
51e29401 | 37 | #include "flags.h" |
49ad7cfa | 38 | #include "function.h" |
e14417fa | 39 | #include "output.h" |
051c57da | 40 | #include "expr.h" |
51e29401 | 41 | #include "c-tree.h" |
5f6da302 | 42 | #include "toplev.h" |
1526a060 | 43 | #include "ggc.h" |
809d4ef1 | 44 | #include "tm_p.h" |
a0d85b75 | 45 | #include "cpplib.h" |
672a6f42 | 46 | #include "target.h" |
e1772ac0 | 47 | #include "debug.h" |
40e941af | 48 | #include "opts.h" |
b932f770 | 49 | #include "timevar.h" |
26f943fd | 50 | #include "c-common.h" |
ecb0eece | 51 | #include "c-pragma.h" |
6de9cd9a DN |
52 | #include "langhooks.h" |
53 | #include "tree-mudflap.h" | |
eadf906f | 54 | #include "tree-gimple.h" |
6de9cd9a DN |
55 | #include "diagnostic.h" |
56 | #include "tree-dump.h" | |
1c4a429a | 57 | #include "cgraph.h" |
66ea6f4c | 58 | #include "hashtab.h" |
0bfa5f65 RH |
59 | #include "libfuncs.h" |
60 | #include "except.h" | |
48873ed2 | 61 | #include "langhooks-def.h" |
a0d85b75 | 62 | |
51e29401 RS |
63 | /* In grokdeclarator, distinguish syntactic contexts of declarators. */ |
64 | enum decl_context | |
65 | { NORMAL, /* Ordinary declaration */ | |
66 | FUNCDEF, /* Function definition */ | |
67 | PARM, /* Declaration of parm before function body */ | |
68 | FIELD, /* Declaration inside struct or union */ | |
51e29401 RS |
69 | TYPENAME}; /* Typename (inside cast or sizeof) */ |
70 | ||
51e29401 | 71 | \f |
51e29401 RS |
72 | /* Nonzero if we have seen an invalid cross reference |
73 | to a struct, union, or enum, but not yet printed the message. */ | |
51e29401 | 74 | tree pending_invalid_xref; |
093c7153 | 75 | |
51e29401 | 76 | /* File and line to appear in the eventual error message. */ |
070588f0 | 77 | location_t pending_invalid_xref_location; |
51e29401 | 78 | |
093c7153 RH |
79 | /* True means we've initialized exception handling. */ |
80 | bool c_eh_initialized_p; | |
81 | ||
51e29401 | 82 | /* While defining an enum type, this is 1 plus the last enumerator |
18192b41 RK |
83 | constant value. Note that will do not have to save this or `enum_overflow' |
84 | around nested function definition since such a definition could only | |
85 | occur in an enum value expression and we don't use these variables in | |
86 | that case. */ | |
51e29401 RS |
87 | |
88 | static tree enum_next_value; | |
89 | ||
93e3ba4f RS |
90 | /* Nonzero means that there was overflow computing enum_next_value. */ |
91 | ||
92 | static int enum_overflow; | |
93 | ||
77dbdb57 ZW |
94 | /* These #defines are for clarity in working with the information block |
95 | returned by get_parm_info. */ | |
96 | #define ARG_INFO_PARMS(args) TREE_PURPOSE(args) | |
97 | #define ARG_INFO_TAGS(args) TREE_VALUE(args) | |
98 | #define ARG_INFO_TYPES(args) TREE_CHAIN(args) | |
99 | #define ARG_INFO_OTHERS(args) TREE_TYPE(args) | |
51e29401 | 100 | |
77dbdb57 ZW |
101 | /* The file and line that the prototype came from if this is an |
102 | old-style definition; used for diagnostics in | |
103 | store_parm_decls_oldstyle. */ | |
eb1dfbb2 | 104 | |
95035b6f | 105 | static location_t current_function_prototype_locus; |
50a9145c | 106 | |
8f17b5c5 MM |
107 | /* The current statement tree. */ |
108 | ||
e2500fed | 109 | static GTY(()) struct stmt_tree_s c_stmt_tree; |
8f17b5c5 | 110 | |
6614fd40 | 111 | /* State saving variables. */ |
506e2710 RH |
112 | tree c_break_label; |
113 | tree c_cont_label; | |
e13e48e7 | 114 | |
eecec698 ZW |
115 | /* Linked list of TRANSLATION_UNIT_DECLS for the translation units |
116 | included in this invocation. Note that the current translation | |
117 | unit is not included in this list. */ | |
d1bd0ded | 118 | |
eecec698 | 119 | static GTY(()) tree all_translation_units; |
d1bd0ded | 120 | |
f75fbaf7 ZW |
121 | /* A list of decls to be made automatically visible in each file scope. */ |
122 | static GTY(()) tree visible_builtins; | |
123 | ||
51e29401 RS |
124 | /* Set to 0 at beginning of a function definition, set to 1 if |
125 | a return statement that specifies a return value is seen. */ | |
126 | ||
127 | int current_function_returns_value; | |
128 | ||
129 | /* Set to 0 at beginning of a function definition, set to 1 if | |
130 | a return statement with no argument is seen. */ | |
131 | ||
132 | int current_function_returns_null; | |
133 | ||
5ce89b2e JM |
134 | /* Set to 0 at beginning of a function definition, set to 1 if |
135 | a call to a noreturn function is seen. */ | |
136 | ||
137 | int current_function_returns_abnormally; | |
138 | ||
51e29401 RS |
139 | /* Set to nonzero by `grokdeclarator' for a function |
140 | whose return type is defaulted, if warnings for this are desired. */ | |
141 | ||
142 | static int warn_about_return_type; | |
143 | ||
929f3671 | 144 | /* Nonzero when starting a function declared `extern inline'. */ |
51e29401 RS |
145 | |
146 | static int current_extern_inline; | |
f75fbaf7 ZW |
147 | |
148 | /* True means global_bindings_p should return false even if the scope stack | |
149 | says we are in file scope. */ | |
150 | bool c_override_global_bindings_to_false; | |
151 | ||
51e29401 | 152 | \f |
f75fbaf7 ZW |
153 | /* Each c_binding structure describes one binding of an identifier to |
154 | a decl. All the decls in a scope - irrespective of namespace - are | |
155 | chained together by the ->prev field, which (as the name implies) | |
156 | runs in reverse order. All the decls in a given namespace bound to | |
157 | a given identifier are chained by the ->shadowed field, which runs | |
9aaabf8a | 158 | from inner to outer scopes. |
f75fbaf7 ZW |
159 | |
160 | The ->decl field usually points to a DECL node, but there are two | |
161 | exceptions. In the namespace of type tags, the bound entity is a | |
162 | RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared | |
163 | identifier is encountered, it is bound to error_mark_node to | |
164 | suppress further errors about that identifier in the current | |
9aaabf8a ZW |
165 | function. |
166 | ||
167 | The depth field is copied from the scope structure that holds this | |
168 | decl. It is used to preserve the proper ordering of the ->shadowed | |
169 | field (see bind()) and also for a handful of special-case checks. | |
170 | Finally, the invisible bit is true for a decl which should be | |
171 | ignored for purposes of normal name lookup, and the nested bit is | |
172 | true for a decl that's been bound a second time in an inner scope; | |
173 | in all such cases, the binding in the outer scope will have its | |
174 | invisible bit true. */ | |
f75fbaf7 | 175 | |
18d5f982 | 176 | struct c_binding GTY((chain_next ("%h.prev"))) |
f75fbaf7 ZW |
177 | { |
178 | tree decl; /* the decl bound */ | |
179 | tree id; /* the identifier it's bound to */ | |
180 | struct c_binding *prev; /* the previous decl in this scope */ | |
181 | struct c_binding *shadowed; /* the innermost decl shadowed by this one */ | |
9aaabf8a ZW |
182 | unsigned int depth : 28; /* depth of this scope */ |
183 | BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */ | |
184 | BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */ | |
185 | /* two free bits */ | |
f75fbaf7 | 186 | }; |
9aaabf8a ZW |
187 | #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth) |
188 | #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth) | |
189 | #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/) | |
190 | #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/) | |
f75fbaf7 ZW |
191 | |
192 | #define I_SYMBOL_BINDING(node) \ | |
193 | (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding) | |
194 | #define I_SYMBOL_DECL(node) \ | |
195 | (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0) | |
196 | ||
197 | #define I_TAG_BINDING(node) \ | |
198 | (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding) | |
199 | #define I_TAG_DECL(node) \ | |
200 | (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0) | |
201 | ||
202 | #define I_LABEL_BINDING(node) \ | |
203 | (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding) | |
204 | #define I_LABEL_DECL(node) \ | |
205 | (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0) | |
206 | ||
18d5f982 ZW |
207 | /* Each C symbol points to three linked lists of c_binding structures. |
208 | These describe the values of the identifier in the three different | |
209 | namespaces defined by the language. */ | |
210 | ||
211 | struct lang_identifier GTY(()) | |
212 | { | |
213 | struct c_common_identifier common_id; | |
214 | struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */ | |
215 | struct c_binding *tag_binding; /* struct/union/enum tags */ | |
216 | struct c_binding *label_binding; /* labels */ | |
217 | }; | |
218 | ||
219 | /* Validate c-lang.c's assumptions. */ | |
220 | extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate | |
221 | [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1]; | |
222 | ||
223 | /* The resulting tree type. */ | |
224 | ||
225 | union lang_tree_node | |
226 | GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"), | |
227 | chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *)TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *)TREE_CHAIN (&%h.generic)"))) | |
228 | { | |
229 | union tree_node GTY ((tag ("0"), | |
230 | desc ("tree_node_structure (&%h)"))) | |
231 | generic; | |
232 | struct lang_identifier GTY ((tag ("1"))) identifier; | |
233 | }; | |
234 | ||
f75fbaf7 ZW |
235 | /* Each c_scope structure describes the complete contents of one |
236 | scope. Four scopes are distinguished specially: the innermost or | |
237 | current scope, the innermost function scope, the file scope (always | |
238 | the second to outermost) and the outermost or external scope. | |
f91f41b2 ZW |
239 | |
240 | Most declarations are recorded in the current scope. | |
241 | ||
242 | All normal label declarations are recorded in the innermost | |
243 | function scope, as are bindings of undeclared identifiers to | |
244 | error_mark_node. (GCC permits nested functions as an extension, | |
245 | hence the 'innermost' qualifier.) Explicitly declared labels | |
246 | (using the __label__ extension) appear in the current scope. | |
247 | ||
f75fbaf7 | 248 | Being in the file scope (current_scope == file_scope) causes |
f91f41b2 ZW |
249 | special behavior in several places below. Also, under some |
250 | conditions the Objective-C front end records declarations in the | |
f75fbaf7 ZW |
251 | file scope even though that isn't the current scope. |
252 | ||
253 | All declarations with external linkage are recorded in the external | |
254 | scope, even if they aren't visible there; this models the fact that | |
255 | such declarations are visible to the entire program, and (with a | |
256 | bit of cleverness, see pushdecl) allows diagnosis of some violations | |
257 | of C99 6.2.2p7 and 6.2.7p2: | |
258 | ||
259 | If, within the same translation unit, the same identifier appears | |
260 | with both internal and external linkage, the behavior is | |
261 | undefined. | |
f91f41b2 | 262 | |
f75fbaf7 ZW |
263 | All declarations that refer to the same object or function shall |
264 | have compatible type; otherwise, the behavior is undefined. | |
265 | ||
266 | Initially only the built-in declarations, which describe compiler | |
267 | intrinsic functions plus a subset of the standard library, are in | |
268 | this scope. | |
269 | ||
270 | The order of the blocks list matters, and it is frequently appended | |
271 | to. To avoid having to walk all the way to the end of the list on | |
272 | each insertion, or reverse the list later, we maintain a pointer to | |
273 | the last list entry. (FIXME: It should be feasible to use a reversed | |
274 | list here.) | |
275 | ||
276 | The bindings list is strictly in reverse order of declarations; | |
277 | pop_scope relies on this. */ | |
f91f41b2 | 278 | |
51e29401 | 279 | |
18d5f982 | 280 | struct c_scope GTY((chain_next ("%h.outer"))) |
14077d68 ZW |
281 | { |
282 | /* The scope containing this one. */ | |
283 | struct c_scope *outer; | |
284 | ||
285 | /* The next outermost function scope. */ | |
286 | struct c_scope *outer_function; | |
287 | ||
f75fbaf7 ZW |
288 | /* All bindings in this scope. */ |
289 | struct c_binding *bindings; | |
14077d68 ZW |
290 | |
291 | /* For each scope (except the global one), a chain of BLOCK nodes | |
292 | for all the scopes that were entered and exited one level down. */ | |
293 | tree blocks; | |
f91f41b2 | 294 | tree blocks_last; |
14077d68 | 295 | |
f75fbaf7 ZW |
296 | /* The depth of this scope. Used to keep the ->shadowed chain of |
297 | bindings sorted innermost to outermost. */ | |
298 | unsigned int depth : 28; | |
299 | ||
14077d68 ZW |
300 | /* True if we are currently filling this scope with parameter |
301 | declarations. */ | |
1c62e7b2 | 302 | BOOL_BITFIELD parm_flag : 1; |
14077d68 | 303 | |
55d54003 ZW |
304 | /* True if we already complained about forward parameter decls |
305 | in this scope. This prevents double warnings on | |
306 | foo (int a; int b; ...) */ | |
1c62e7b2 | 307 | BOOL_BITFIELD warned_forward_parm_decls : 1; |
55d54003 | 308 | |
14077d68 ZW |
309 | /* True if this is the outermost block scope of a function body. |
310 | This scope contains the parameters, the local variables declared | |
311 | in the outermost block, and all the labels (except those in | |
312 | nested functions, or declared at block scope with __label__). */ | |
1c62e7b2 | 313 | BOOL_BITFIELD function_body : 1; |
14077d68 ZW |
314 | |
315 | /* True means make a BLOCK for this scope no matter what. */ | |
1c62e7b2 | 316 | BOOL_BITFIELD keep : 1; |
14077d68 | 317 | }; |
51e29401 | 318 | |
f8521984 | 319 | /* The scope currently in effect. */ |
6645c3fa | 320 | |
f8521984 | 321 | static GTY(()) struct c_scope *current_scope; |
51e29401 | 322 | |
14e33ee8 ZW |
323 | /* The innermost function scope. Ordinary (not explicitly declared) |
324 | labels, bindings to error_mark_node, and the lazily-created | |
325 | bindings of __func__ and its friends get this scope. */ | |
14077d68 | 326 | |
f8521984 | 327 | static GTY(()) struct c_scope *current_function_scope; |
14e33ee8 | 328 | |
f75fbaf7 ZW |
329 | /* The C file scope. This is reset for each input translation unit. */ |
330 | ||
331 | static GTY(()) struct c_scope *file_scope; | |
332 | ||
333 | /* The outermost scope. This is used for all declarations with | |
334 | external linkage, and only these, hence the name. */ | |
335 | ||
336 | static GTY(()) struct c_scope *external_scope; | |
337 | ||
338 | /* A chain of c_scope structures awaiting reuse. */ | |
339 | ||
1431042e | 340 | static GTY((deletable)) struct c_scope *scope_freelist; |
f75fbaf7 ZW |
341 | |
342 | /* A chain of c_binding structures awaiting reuse. */ | |
51e29401 | 343 | |
1431042e | 344 | static GTY((deletable)) struct c_binding *binding_freelist; |
51e29401 | 345 | |
3d93cdfa | 346 | /* Append VAR to LIST in scope SCOPE. */ |
f91f41b2 ZW |
347 | #define SCOPE_LIST_APPEND(scope, list, decl) do { \ |
348 | struct c_scope *s_ = (scope); \ | |
349 | tree d_ = (decl); \ | |
350 | if (s_->list##_last) \ | |
351 | TREE_CHAIN (s_->list##_last) = d_; \ | |
352 | else \ | |
353 | s_->list = d_; \ | |
354 | s_->list##_last = d_; \ | |
355 | } while (0) | |
356 | ||
357 | /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */ | |
358 | #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \ | |
359 | struct c_scope *t_ = (tscope); \ | |
360 | struct c_scope *f_ = (fscope); \ | |
361 | if (t_->to##_last) \ | |
362 | TREE_CHAIN (t_->to##_last) = f_->from; \ | |
363 | else \ | |
364 | t_->to = f_->from; \ | |
365 | t_->to##_last = f_->from##_last; \ | |
366 | } while (0) | |
367 | ||
f8521984 | 368 | /* True means unconditionally make a BLOCK for the next scope pushed. */ |
51e29401 | 369 | |
14e33ee8 | 370 | static bool keep_next_level_flag; |
51e29401 | 371 | |
f75fbaf7 | 372 | /* True means the next call to push_scope will be the outermost scope |
a8ccdffe ZW |
373 | of a function body, so do not push a new scope, merely cease |
374 | expecting parameter decls. */ | |
6645c3fa | 375 | |
a8ccdffe | 376 | static bool next_is_function_body; |
51e29401 | 377 | |
2c5f4139 JM |
378 | /* Functions called automatically at the beginning and end of execution. */ |
379 | ||
380 | tree static_ctors, static_dtors; | |
381 | ||
51e29401 | 382 | /* Forward declarations. */ |
f75fbaf7 ZW |
383 | static tree lookup_name_in_scope (tree, struct c_scope *); |
384 | static tree c_make_fname_decl (tree, int); | |
2ff7cce4 | 385 | static tree grokdeclarator (tree, tree, enum decl_context, int, tree *); |
35b1a6fa AJ |
386 | static tree grokparms (tree, int); |
387 | static void layout_array_type (tree); | |
51e29401 | 388 | \f |
e23bd218 IR |
389 | /* States indicating how grokdeclarator() should handle declspecs marked |
390 | with __attribute__((deprecated)). An object declared as | |
391 | __attribute__((deprecated)) suppresses warnings of uses of other | |
392 | deprecated items. */ | |
35b1a6fa | 393 | |
e23bd218 IR |
394 | enum deprecated_states { |
395 | DEPRECATED_NORMAL, | |
396 | DEPRECATED_SUPPRESS | |
397 | }; | |
398 | ||
399 | static enum deprecated_states deprecated_state = DEPRECATED_NORMAL; | |
400 | ||
51e29401 | 401 | void |
35b1a6fa | 402 | c_print_identifier (FILE *file, tree node, int indent) |
51e29401 | 403 | { |
f75fbaf7 ZW |
404 | print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4); |
405 | print_node (file, "tag", I_TAG_DECL (node), indent + 4); | |
406 | print_node (file, "label", I_LABEL_DECL (node), indent + 4); | |
0e5921e8 ZW |
407 | if (C_IS_RESERVED_WORD (node)) |
408 | { | |
409 | tree rid = ridpointers[C_RID_CODE (node)]; | |
410 | indent_to (file, indent + 4); | |
75b6f3fd KG |
411 | fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"", |
412 | (void *) rid, IDENTIFIER_POINTER (rid)); | |
0e5921e8 | 413 | } |
51e29401 | 414 | } |
f75fbaf7 ZW |
415 | |
416 | /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL, | |
417 | which may be any of several kinds of DECL or TYPE or error_mark_node, | |
418 | in the scope SCOPE. */ | |
419 | static void | |
9aaabf8a | 420 | bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested) |
f75fbaf7 ZW |
421 | { |
422 | struct c_binding *b, **here; | |
423 | ||
424 | if (binding_freelist) | |
425 | { | |
426 | b = binding_freelist; | |
427 | binding_freelist = b->prev; | |
428 | } | |
429 | else | |
430 | b = ggc_alloc (sizeof (struct c_binding)); | |
431 | ||
432 | b->shadowed = 0; | |
433 | b->decl = decl; | |
434 | b->id = name; | |
9aaabf8a ZW |
435 | b->depth = scope->depth; |
436 | b->invisible = invisible; | |
437 | b->nested = nested; | |
f75fbaf7 ZW |
438 | |
439 | b->prev = scope->bindings; | |
440 | scope->bindings = b; | |
441 | ||
442 | if (!name) | |
443 | return; | |
444 | ||
445 | switch (TREE_CODE (decl)) | |
446 | { | |
447 | case LABEL_DECL: here = &I_LABEL_BINDING (name); break; | |
448 | case ENUMERAL_TYPE: | |
449 | case UNION_TYPE: | |
450 | case RECORD_TYPE: here = &I_TAG_BINDING (name); break; | |
451 | case VAR_DECL: | |
452 | case FUNCTION_DECL: | |
453 | case TYPE_DECL: | |
454 | case CONST_DECL: | |
455 | case PARM_DECL: | |
456 | case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break; | |
457 | ||
458 | default: | |
459 | abort (); | |
460 | } | |
461 | ||
462 | /* Locate the appropriate place in the chain of shadowed decls | |
463 | to insert this binding. Normally, scope == current_scope and | |
464 | this does nothing. */ | |
9aaabf8a | 465 | while (*here && (*here)->depth > scope->depth) |
f75fbaf7 ZW |
466 | here = &(*here)->shadowed; |
467 | ||
468 | b->shadowed = *here; | |
469 | *here = b; | |
470 | } | |
471 | ||
472 | /* Clear the binding structure B, stick it on the binding_freelist, | |
473 | and return the former value of b->prev. This is used by pop_scope | |
474 | and get_parm_info to iterate destructively over all the bindings | |
475 | from a given scope. */ | |
476 | static struct c_binding * | |
477 | free_binding_and_advance (struct c_binding *b) | |
478 | { | |
479 | struct c_binding *prev = b->prev; | |
480 | ||
9aaabf8a | 481 | memset (b, 0, sizeof (struct c_binding)); |
f75fbaf7 ZW |
482 | b->prev = binding_freelist; |
483 | binding_freelist = b; | |
484 | ||
485 | return prev; | |
486 | } | |
487 | ||
51e29401 | 488 | \f |
b4892310 | 489 | /* Hook called at end of compilation to assume 1 elt |
f8521984 | 490 | for a file-scope tentative array defn that wasn't complete before. */ |
6645c3fa | 491 | |
b4892310 | 492 | void |
35b1a6fa | 493 | c_finish_incomplete_decl (tree decl) |
b4892310 | 494 | { |
5cfac96e | 495 | if (TREE_CODE (decl) == VAR_DECL) |
b4892310 RS |
496 | { |
497 | tree type = TREE_TYPE (decl); | |
5cfac96e RK |
498 | if (type != error_mark_node |
499 | && TREE_CODE (type) == ARRAY_TYPE | |
53c5b5d7 | 500 | && ! DECL_EXTERNAL (decl) |
5cfac96e | 501 | && TYPE_DOMAIN (type) == 0) |
b4892310 | 502 | { |
ddd2d57e | 503 | warning ("%Jarray '%D' assumed to have one element", decl, decl); |
5cfac96e | 504 | |
b4892310 RS |
505 | complete_array_type (type, NULL_TREE, 1); |
506 | ||
507 | layout_decl (decl, 0); | |
508 | } | |
509 | } | |
510 | } | |
511 | \f | |
264fa2db ZL |
512 | /* The Objective-C front-end often needs to determine the current scope. */ |
513 | ||
514 | void * | |
515 | get_current_scope (void) | |
516 | { | |
517 | return current_scope; | |
518 | } | |
519 | ||
520 | /* The following function is used only by Objective-C. It needs to live here | |
521 | because it accesses the innards of c_scope. */ | |
522 | ||
523 | void | |
524 | objc_mark_locals_volatile (void *enclosing_blk) | |
525 | { | |
526 | struct c_scope *scope; | |
f75fbaf7 | 527 | struct c_binding *b; |
e13e48e7 EC |
528 | |
529 | for (scope = current_scope; | |
264fa2db ZL |
530 | scope && scope != enclosing_blk; |
531 | scope = scope->outer) | |
532 | { | |
f75fbaf7 | 533 | for (b = scope->bindings; b; b = b->prev) |
264fa2db | 534 | { |
f75fbaf7 ZW |
535 | if (TREE_CODE (b->decl) == VAR_DECL |
536 | || TREE_CODE (b->decl) == PARM_DECL) | |
537 | { | |
5baeaac0 | 538 | C_DECL_REGISTER (b->decl) = 0; |
f75fbaf7 ZW |
539 | DECL_REGISTER (b->decl) = 0; |
540 | TREE_THIS_VOLATILE (b->decl) = 1; | |
541 | } | |
264fa2db | 542 | } |
f75fbaf7 | 543 | |
264fa2db ZL |
544 | /* Do not climb up past the current function. */ |
545 | if (scope->function_body) | |
546 | break; | |
e13e48e7 EC |
547 | } |
548 | } | |
549 | ||
f75fbaf7 | 550 | /* Nonzero if we are currently in file scope. */ |
51e29401 RS |
551 | |
552 | int | |
35b1a6fa | 553 | global_bindings_p (void) |
51e29401 | 554 | { |
f75fbaf7 | 555 | return current_scope == file_scope && !c_override_global_bindings_to_false; |
51e29401 RS |
556 | } |
557 | ||
558 | void | |
35b1a6fa | 559 | keep_next_level (void) |
51e29401 | 560 | { |
14e33ee8 | 561 | keep_next_level_flag = true; |
51e29401 RS |
562 | } |
563 | ||
f8521984 | 564 | /* Identify this scope as currently being filled with parameters. */ |
51e29401 RS |
565 | |
566 | void | |
eb1dfbb2 | 567 | declare_parm_level (void) |
51e29401 | 568 | { |
f8521984 | 569 | current_scope->parm_flag = true; |
51e29401 RS |
570 | } |
571 | ||
51e29401 | 572 | void |
f75fbaf7 | 573 | push_scope (void) |
51e29401 | 574 | { |
a8ccdffe | 575 | if (next_is_function_body) |
51e29401 | 576 | { |
339a28b9 ZW |
577 | /* This is the transition from the parameters to the top level |
578 | of the function body. These are the same scope | |
f8521984 | 579 | (C99 6.2.1p4,6) so we do not push another scope structure. |
a8ccdffe ZW |
580 | next_is_function_body is set only by store_parm_decls, which |
581 | in turn is called when and only when we are about to | |
582 | encounter the opening curly brace for the function body. | |
339a28b9 | 583 | |
a8ccdffe ZW |
584 | The outermost block of a function always gets a BLOCK node, |
585 | because the debugging output routines expect that each | |
59e4e217 | 586 | function has at least one BLOCK. */ |
f8521984 ZW |
587 | current_scope->parm_flag = false; |
588 | current_scope->function_body = true; | |
a8ccdffe | 589 | current_scope->keep = true; |
f8521984 ZW |
590 | current_scope->outer_function = current_function_scope; |
591 | current_function_scope = current_scope; | |
14e33ee8 ZW |
592 | |
593 | keep_next_level_flag = false; | |
a8ccdffe | 594 | next_is_function_body = false; |
51e29401 | 595 | } |
339a28b9 ZW |
596 | else |
597 | { | |
f75fbaf7 ZW |
598 | struct c_scope *scope; |
599 | if (scope_freelist) | |
600 | { | |
601 | scope = scope_freelist; | |
602 | scope_freelist = scope->outer; | |
603 | } | |
604 | else | |
605 | scope = ggc_alloc_cleared (sizeof (struct c_scope)); | |
51e29401 | 606 | |
f8521984 ZW |
607 | scope->keep = keep_next_level_flag; |
608 | scope->outer = current_scope; | |
f75fbaf7 ZW |
609 | scope->depth = current_scope ? (current_scope->depth + 1) : 0; |
610 | ||
611 | /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but | |
612 | possible. */ | |
613 | if (current_scope && scope->depth == 0) | |
614 | { | |
615 | scope->depth--; | |
616 | sorry ("GCC supports only %u nested scopes\n", scope->depth); | |
617 | } | |
118a3a8b | 618 | |
f8521984 ZW |
619 | current_scope = scope; |
620 | keep_next_level_flag = false; | |
339a28b9 | 621 | } |
0500d6f9 | 622 | } |
6645c3fa | 623 | |
f8521984 | 624 | /* Exit a scope. Restore the state of the identifier-decl mappings |
f75fbaf7 ZW |
625 | that were in effect when this scope was entered. Return a BLOCK |
626 | node containing all the DECLs in this scope that are of interest | |
627 | to debug info generation. */ | |
51e29401 RS |
628 | |
629 | tree | |
f75fbaf7 | 630 | pop_scope (void) |
51e29401 | 631 | { |
f91f41b2 | 632 | struct c_scope *scope = current_scope; |
f75fbaf7 ZW |
633 | tree block, context, p; |
634 | struct c_binding *b; | |
9cd51ef6 | 635 | |
f75fbaf7 ZW |
636 | bool functionbody = scope->function_body; |
637 | bool keep = functionbody || scope->keep || scope->bindings; | |
f91f41b2 ZW |
638 | |
639 | /* If appropriate, create a BLOCK to record the decls for the life | |
640 | of this function. */ | |
641 | block = 0; | |
642 | if (keep) | |
643 | { | |
644 | block = make_node (BLOCK); | |
f91f41b2 ZW |
645 | BLOCK_SUBBLOCKS (block) = scope->blocks; |
646 | TREE_USED (block) = 1; | |
f91f41b2 | 647 | |
f75fbaf7 ZW |
648 | /* In each subblock, record that this is its superior. */ |
649 | for (p = scope->blocks; p; p = TREE_CHAIN (p)) | |
650 | BLOCK_SUPERCONTEXT (p) = block; | |
f91f41b2 | 651 | |
f75fbaf7 ZW |
652 | BLOCK_VARS (block) = 0; |
653 | } | |
339a28b9 | 654 | |
f75fbaf7 ZW |
655 | /* The TYPE_CONTEXTs for all of the tagged types belonging to this |
656 | scope must be set so that they point to the appropriate | |
657 | construct, i.e. either to the current FUNCTION_DECL node, or | |
658 | else to the BLOCK node we just constructed. | |
f91f41b2 | 659 | |
f75fbaf7 ZW |
660 | Note that for tagged types whose scope is just the formal |
661 | parameter list for some function type specification, we can't | |
662 | properly set their TYPE_CONTEXTs here, because we don't have a | |
663 | pointer to the appropriate FUNCTION_TYPE node readily available | |
664 | to us. For those cases, the TYPE_CONTEXTs of the relevant tagged | |
665 | type nodes get set in `grokdeclarator' as soon as we have created | |
666 | the FUNCTION_TYPE node which will represent the "scope" for these | |
667 | "parameter list local" tagged types. */ | |
668 | if (scope->function_body) | |
669 | context = current_function_decl; | |
670 | else if (scope == file_scope) | |
eecec698 ZW |
671 | { |
672 | tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0); | |
673 | TREE_CHAIN (file_decl) = all_translation_units; | |
674 | all_translation_units = file_decl; | |
675 | context = file_decl; | |
676 | } | |
f75fbaf7 ZW |
677 | else |
678 | context = block; | |
f91f41b2 | 679 | |
f75fbaf7 ZW |
680 | /* Clear all bindings in this scope. */ |
681 | for (b = scope->bindings; b; b = free_binding_and_advance (b)) | |
339a28b9 | 682 | { |
f75fbaf7 | 683 | p = b->decl; |
f91f41b2 | 684 | switch (TREE_CODE (p)) |
14e33ee8 | 685 | { |
f91f41b2 | 686 | case LABEL_DECL: |
f75fbaf7 | 687 | /* Warnings for unused labels, errors for undefined labels. */ |
f91f41b2 | 688 | if (TREE_USED (p) && !DECL_INITIAL (p)) |
14e33ee8 | 689 | { |
ddd2d57e | 690 | error ("%Jlabel `%D' used but not defined", p, p); |
f91f41b2 | 691 | DECL_INITIAL (p) = error_mark_node; |
14e33ee8 | 692 | } |
f91f41b2 | 693 | else if (!TREE_USED (p) && warn_unused_label) |
14e33ee8 | 694 | { |
f91f41b2 | 695 | if (DECL_INITIAL (p)) |
ddd2d57e | 696 | warning ("%Jlabel `%D' defined but not used", p, p); |
14e33ee8 | 697 | else |
ddd2d57e | 698 | warning ("%Jlabel `%D' declared but not defined", p, p); |
14e33ee8 | 699 | } |
f75fbaf7 ZW |
700 | /* Labels go in BLOCK_VARS. */ |
701 | TREE_CHAIN (p) = BLOCK_VARS (block); | |
702 | BLOCK_VARS (block) = p; | |
118a3a8b | 703 | |
f75fbaf7 ZW |
704 | #ifdef ENABLE_CHECKING |
705 | if (I_LABEL_BINDING (b->id) != b) abort (); | |
706 | #endif | |
707 | I_LABEL_BINDING (b->id) = b->shadowed; | |
708 | break; | |
339a28b9 | 709 | |
f75fbaf7 ZW |
710 | case ENUMERAL_TYPE: |
711 | case UNION_TYPE: | |
712 | case RECORD_TYPE: | |
713 | TYPE_CONTEXT (p) = context; | |
714 | ||
715 | /* Types may not have tag-names, in which case the type | |
716 | appears in the bindings list with b->id NULL. */ | |
717 | if (b->id) | |
718 | { | |
719 | #ifdef ENABLE_CHECKING | |
720 | if (I_TAG_BINDING (b->id) != b) abort (); | |
721 | #endif | |
722 | I_TAG_BINDING (b->id) = b->shadowed; | |
723 | } | |
724 | break; | |
339a28b9 | 725 | |
f91f41b2 | 726 | case FUNCTION_DECL: |
f75fbaf7 ZW |
727 | /* Propagate TREE_ADDRESSABLE from nested functions to their |
728 | containing functions. */ | |
f91f41b2 ZW |
729 | if (! TREE_ASM_WRITTEN (p) |
730 | && DECL_INITIAL (p) != 0 | |
731 | && TREE_ADDRESSABLE (p) | |
732 | && DECL_ABSTRACT_ORIGIN (p) != 0 | |
733 | && DECL_ABSTRACT_ORIGIN (p) != p) | |
734 | TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1; | |
f75fbaf7 | 735 | goto common_symbol; |
f91f41b2 ZW |
736 | |
737 | case VAR_DECL: | |
f75fbaf7 ZW |
738 | /* Warnings for unused variables. Keep this in sync with |
739 | stmt.c:warn_about_unused_variables, which we cannot use | |
740 | since it expects a different data structure. */ | |
741 | if (warn_unused_variable | |
f91f41b2 ZW |
742 | && !TREE_USED (p) |
743 | && !DECL_IN_SYSTEM_HEADER (p) | |
744 | && DECL_NAME (p) | |
f75fbaf7 ZW |
745 | && !DECL_ARTIFICIAL (p) |
746 | && (scope != file_scope | |
747 | || (TREE_STATIC (p) && !TREE_PUBLIC (p) | |
748 | && !TREE_THIS_VOLATILE (p))) | |
749 | && scope != external_scope) | |
ddd2d57e | 750 | warning ("%Junused variable `%D'", p, p); |
f75fbaf7 | 751 | |
a1105617 | 752 | /* Fall through. */ |
f75fbaf7 ZW |
753 | case TYPE_DECL: |
754 | case CONST_DECL: | |
755 | common_symbol: | |
756 | /* All of these go in BLOCK_VARS, but only if this is the | |
757 | binding in the home scope. */ | |
9aaabf8a | 758 | if (!b->nested) |
f75fbaf7 ZW |
759 | { |
760 | TREE_CHAIN (p) = BLOCK_VARS (block); | |
761 | BLOCK_VARS (block) = p; | |
762 | } | |
9aaabf8a ZW |
763 | /* If this is the file scope, must set DECL_CONTEXT on |
764 | these. Do so even for externals, so that | |
765 | same_translation_unit_p works. */ | |
766 | if (scope == file_scope) | |
eecec698 | 767 | DECL_CONTEXT (p) = context; |
55d54003 | 768 | |
f75fbaf7 ZW |
769 | /* Fall through. */ |
770 | /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have | |
771 | already been put there by store_parm_decls. Unused- | |
772 | parameter warnings are handled by function.c. | |
773 | error_mark_node obviously does not go in BLOCK_VARS and | |
774 | does not get unused-variable warnings. */ | |
775 | case PARM_DECL: | |
776 | case ERROR_MARK: | |
777 | /* It is possible for a decl not to have a name. We get | |
778 | here with b->id NULL in this case. */ | |
779 | if (b->id) | |
f91f41b2 | 780 | { |
f75fbaf7 ZW |
781 | #ifdef ENABLE_CHECKING |
782 | if (I_SYMBOL_BINDING (b->id) != b) abort (); | |
783 | #endif | |
784 | I_SYMBOL_BINDING (b->id) = b->shadowed; | |
f91f41b2 ZW |
785 | } |
786 | break; | |
f75fbaf7 ZW |
787 | |
788 | default: | |
789 | abort (); | |
f91f41b2 ZW |
790 | } |
791 | } | |
339a28b9 | 792 | |
118a3a8b | 793 | |
f75fbaf7 ZW |
794 | /* Dispose of the block that we just made inside some higher level. */ |
795 | if ((scope->function_body || scope == file_scope) && context) | |
339a28b9 | 796 | { |
f75fbaf7 ZW |
797 | DECL_INITIAL (context) = block; |
798 | BLOCK_SUPERCONTEXT (block) = context; | |
339a28b9 | 799 | } |
f91f41b2 | 800 | else if (scope->outer) |
3bf40d18 | 801 | { |
f91f41b2 ZW |
802 | if (block) |
803 | SCOPE_LIST_APPEND (scope->outer, blocks, block); | |
804 | /* If we did not make a block for the scope just exited, any | |
805 | blocks made for inner scopes must be carried forward so they | |
806 | will later become subblocks of something else. */ | |
807 | else if (scope->blocks) | |
808 | SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks); | |
3bf40d18 | 809 | } |
51e29401 | 810 | |
f8521984 | 811 | /* Pop the current scope, and free the structure for reuse. */ |
f75fbaf7 ZW |
812 | current_scope = scope->outer; |
813 | if (scope->function_body) | |
814 | current_function_scope = scope->outer_function; | |
815 | ||
816 | memset (scope, 0, sizeof (struct c_scope)); | |
817 | scope->outer = scope_freelist; | |
818 | scope_freelist = scope; | |
51e29401 | 819 | |
51e29401 RS |
820 | return block; |
821 | } | |
3bf40d18 | 822 | |
f75fbaf7 ZW |
823 | void |
824 | push_file_scope (void) | |
825 | { | |
826 | tree decl; | |
f75fbaf7 | 827 | |
f6bf7bb6 AP |
828 | if (file_scope) |
829 | return; | |
830 | ||
f75fbaf7 ZW |
831 | push_scope (); |
832 | file_scope = current_scope; | |
833 | ||
834 | start_fname_decls (); | |
835 | ||
836 | for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl)) | |
9aaabf8a ZW |
837 | bind (DECL_NAME (decl), decl, file_scope, |
838 | /*invisible=*/false, /*nested=*/true); | |
f75fbaf7 ZW |
839 | } |
840 | ||
841 | void | |
842 | pop_file_scope (void) | |
843 | { | |
844 | /* In case there were missing closebraces, get us back to the global | |
845 | binding level. */ | |
846 | while (current_scope != file_scope) | |
847 | pop_scope (); | |
848 | ||
849 | /* __FUNCTION__ is defined at file scope (""). This | |
850 | call may not be necessary as my tests indicate it | |
851 | still works without it. */ | |
852 | finish_fname_decls (); | |
853 | ||
854 | /* Kludge: don't actually pop the file scope if generating a | |
855 | precompiled header, so that macros and local symbols are still | |
856 | visible to the PCH generator. */ | |
857 | if (pch_file) | |
858 | return; | |
859 | ||
860 | /* And pop off the file scope. */ | |
861 | pop_scope (); | |
862 | file_scope = 0; | |
863 | ||
864 | cpp_undef_all (parse_in); | |
865 | } | |
866 | ||
f8521984 ZW |
867 | /* Insert BLOCK at the end of the list of subblocks of the current |
868 | scope. This is used when a BIND_EXPR is expanded, to handle the | |
869 | BLOCK node inside the BIND_EXPR. */ | |
3bf40d18 RS |
870 | |
871 | void | |
35b1a6fa | 872 | insert_block (tree block) |
3bf40d18 RS |
873 | { |
874 | TREE_USED (block) = 1; | |
f91f41b2 | 875 | SCOPE_LIST_APPEND (current_scope, blocks, block); |
3bf40d18 | 876 | } |
51e29401 | 877 | \f |
51e29401 RS |
878 | /* Push a definition or a declaration of struct, union or enum tag "name". |
879 | "type" should be the type node. | |
880 | We assume that the tag "name" is not already defined. | |
881 | ||
882 | Note that the definition may really be just a forward reference. | |
883 | In that case, the TYPE_SIZE will be zero. */ | |
884 | ||
f75fbaf7 | 885 | static void |
35b1a6fa | 886 | pushtag (tree name, tree type) |
51e29401 | 887 | { |
14077d68 | 888 | /* Record the identifier as the type's name if it has none. */ |
f75fbaf7 ZW |
889 | if (name && !TYPE_NAME (type)) |
890 | TYPE_NAME (type) = name; | |
9aaabf8a | 891 | bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false); |
c138f328 | 892 | |
51e29401 | 893 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the |
f8521984 | 894 | tagged type we just added to the current scope. This fake |
51e29401 | 895 | NULL-named TYPE_DECL node helps dwarfout.c to know when it needs |
858a47b1 | 896 | to output a representation of a tagged type, and it also gives |
51e29401 RS |
897 | us a convenient place to record the "scope start" address for the |
898 | tagged type. */ | |
899 | ||
8d9bfdc5 | 900 | TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type)); |
88dad228 JM |
901 | |
902 | /* An approximation for now, so we can tell this is a function-scope tag. | |
f75fbaf7 | 903 | This will be updated in pop_scope. */ |
88dad228 | 904 | TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type)); |
51e29401 RS |
905 | } |
906 | \f | |
3c6e6fbf | 907 | /* Subroutine of compare_decls. Allow harmless mismatches in return |
6907ddd3 RS |
908 | and argument types provided that the type modes match. This function |
909 | return a unified type given a suitable match, and 0 otherwise. */ | |
910 | ||
911 | static tree | |
3c6e6fbf | 912 | match_builtin_function_types (tree newtype, tree oldtype) |
6907ddd3 RS |
913 | { |
914 | tree newrettype, oldrettype; | |
915 | tree newargs, oldargs; | |
916 | tree trytype, tryargs; | |
917 | ||
918 | /* Accept the return type of the new declaration if same modes. */ | |
919 | oldrettype = TREE_TYPE (oldtype); | |
920 | newrettype = TREE_TYPE (newtype); | |
921 | ||
922 | if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype)) | |
923 | return 0; | |
924 | ||
925 | oldargs = TYPE_ARG_TYPES (oldtype); | |
926 | newargs = TYPE_ARG_TYPES (newtype); | |
927 | tryargs = newargs; | |
928 | ||
929 | while (oldargs || newargs) | |
930 | { | |
931 | if (! oldargs | |
932 | || ! newargs | |
933 | || ! TREE_VALUE (oldargs) | |
934 | || ! TREE_VALUE (newargs) | |
935 | || TYPE_MODE (TREE_VALUE (oldargs)) | |
936 | != TYPE_MODE (TREE_VALUE (newargs))) | |
937 | return 0; | |
938 | ||
939 | oldargs = TREE_CHAIN (oldargs); | |
940 | newargs = TREE_CHAIN (newargs); | |
941 | } | |
942 | ||
943 | trytype = build_function_type (newrettype, tryargs); | |
944 | return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype)); | |
945 | } | |
946 | ||
1f52178b | 947 | /* Subroutine of diagnose_mismatched_decls. Check for function type |
3c6e6fbf | 948 | mismatch involving an empty arglist vs a nonempty one and give clearer |
9ac97460 | 949 | diagnostics. */ |
3c6e6fbf ZW |
950 | static void |
951 | diagnose_arglist_conflict (tree newdecl, tree olddecl, | |
952 | tree newtype, tree oldtype) | |
953 | { | |
954 | tree t; | |
51e29401 | 955 | |
3c6e6fbf | 956 | if (TREE_CODE (olddecl) != FUNCTION_DECL |
132da1a5 | 957 | || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype)) |
3c6e6fbf ZW |
958 | || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0) |
959 | || | |
960 | (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0))) | |
961 | return; | |
bf44f7de | 962 | |
3c6e6fbf ZW |
963 | t = TYPE_ARG_TYPES (oldtype); |
964 | if (t == 0) | |
965 | t = TYPE_ARG_TYPES (newtype); | |
966 | for (; t; t = TREE_CHAIN (t)) | |
967 | { | |
968 | tree type = TREE_VALUE (t); | |
51e29401 | 969 | |
3c6e6fbf ZW |
970 | if (TREE_CHAIN (t) == 0 |
971 | && TYPE_MAIN_VARIANT (type) != void_type_node) | |
972 | { | |
d4968a11 | 973 | inform ("a parameter list with an ellipsis can't match " |
3c6e6fbf ZW |
974 | "an empty parameter name list declaration"); |
975 | break; | |
976 | } | |
977 | ||
978 | if (c_type_promotes_to (type) != type) | |
979 | { | |
d4968a11 | 980 | inform ("an argument type that has a default promotion can't match " |
3c6e6fbf ZW |
981 | "an empty parameter name list declaration"); |
982 | break; | |
983 | } | |
984 | } | |
985 | } | |
51e29401 | 986 | |
3c6e6fbf ZW |
987 | /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an |
988 | old-style function definition, NEWDECL is a prototype declaration. | |
989 | Diagnose inconsistencies in the argument list. Returns TRUE if | |
990 | the prototype is compatible, FALSE if not. */ | |
991 | static bool | |
992 | validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype) | |
993 | { | |
bc298aa7 ZW |
994 | tree newargs, oldargs; |
995 | int i; | |
996 | ||
997 | /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */ | |
998 | #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node) | |
999 | ||
1000 | oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype); | |
1001 | newargs = TYPE_ARG_TYPES (newtype); | |
1002 | i = 1; | |
1003 | ||
1004 | for (;;) | |
9162542e | 1005 | { |
bc298aa7 ZW |
1006 | tree oldargtype = TREE_VALUE (oldargs); |
1007 | tree newargtype = TREE_VALUE (newargs); | |
1008 | ||
1009 | if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype)) | |
1010 | break; | |
1011 | ||
1012 | /* Reaching the end of just one list means the two decls don't | |
1013 | agree on the number of arguments. */ | |
1014 | if (END_OF_ARGLIST (oldargtype)) | |
9162542e | 1015 | { |
bc298aa7 ZW |
1016 | error ("%Jprototype for '%D' declares more arguments " |
1017 | "than previous old-style definition", newdecl, newdecl); | |
1018 | return false; | |
9162542e | 1019 | } |
bc298aa7 | 1020 | else if (END_OF_ARGLIST (newargtype)) |
3c6e6fbf | 1021 | { |
bc298aa7 ZW |
1022 | error ("%Jprototype for '%D' declares fewer arguments " |
1023 | "than previous old-style definition", newdecl, newdecl); | |
3c6e6fbf ZW |
1024 | return false; |
1025 | } | |
bc298aa7 ZW |
1026 | |
1027 | /* Type for passing arg must be consistent with that declared | |
1028 | for the arg. */ | |
132da1a5 | 1029 | else if (! comptypes (oldargtype, newargtype)) |
3c6e6fbf | 1030 | { |
bc298aa7 ZW |
1031 | error ("%Jprototype for '%D' declares arg %d with incompatible type", |
1032 | newdecl, newdecl, i); | |
3c6e6fbf ZW |
1033 | return false; |
1034 | } | |
bc298aa7 ZW |
1035 | |
1036 | oldargs = TREE_CHAIN (oldargs); | |
1037 | newargs = TREE_CHAIN (newargs); | |
1038 | i++; | |
9162542e | 1039 | } |
bc298aa7 ZW |
1040 | |
1041 | /* If we get here, no errors were found, but do issue a warning | |
1042 | for this poor-style construct. */ | |
1043 | warning ("%Jprototype for '%D' follows non-prototype definition", | |
1044 | newdecl, newdecl); | |
1045 | return true; | |
1046 | #undef END_OF_ARGLIST | |
3c6e6fbf | 1047 | } |
4b4e3407 | 1048 | |
3c6e6fbf ZW |
1049 | /* Subroutine of diagnose_mismatched_decls. Report the location of DECL, |
1050 | first in a pair of mismatched declarations, using the diagnostic | |
1051 | function DIAG. */ | |
1052 | static void | |
1053 | locate_old_decl (tree decl, void (*diag)(const char *, ...)) | |
1054 | { | |
1055 | if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)) | |
1056 | ; | |
1057 | else if (DECL_INITIAL (decl)) | |
1058 | diag (N_("%Jprevious definition of '%D' was here"), decl, decl); | |
1059 | else if (C_DECL_IMPLICIT (decl)) | |
1060 | diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl); | |
1061 | else | |
1062 | diag (N_("%Jprevious declaration of '%D' was here"), decl, decl); | |
1063 | } | |
51e29401 | 1064 | |
3c6e6fbf ZW |
1065 | /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL. |
1066 | Returns true if the caller should proceed to merge the two, false | |
1067 | if OLDDECL should simply be discarded. As a side effect, issues | |
1068 | all necessary diagnostics for invalid or poor-style combinations. | |
1069 | If it returns true, writes the types of NEWDECL and OLDDECL to | |
1070 | *NEWTYPEP and *OLDTYPEP - these may have been adjusted from | |
1071 | TREE_TYPE (NEWDECL, OLDDECL) respectively. */ | |
1072 | ||
1073 | static bool | |
1074 | diagnose_mismatched_decls (tree newdecl, tree olddecl, | |
1075 | tree *newtypep, tree *oldtypep) | |
1076 | { | |
1077 | tree newtype, oldtype; | |
1078 | bool pedwarned = false; | |
1079 | bool warned = false; | |
1080 | ||
1081 | /* If we have error_mark_node for either decl or type, just discard | |
1082 | the previous decl - we're in an error cascade already. */ | |
1083 | if (olddecl == error_mark_node || newdecl == error_mark_node) | |
1084 | return false; | |
bc298aa7 ZW |
1085 | *oldtypep = oldtype = TREE_TYPE (olddecl); |
1086 | *newtypep = newtype = TREE_TYPE (newdecl); | |
3c6e6fbf ZW |
1087 | if (oldtype == error_mark_node || newtype == error_mark_node) |
1088 | return false; | |
1089 | ||
1090 | /* Two different categories of symbol altogether. This is an error | |
1091 | unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */ | |
51e29401 RS |
1092 | if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) |
1093 | { | |
f75fbaf7 ZW |
1094 | if (!(TREE_CODE (olddecl) == FUNCTION_DECL |
1095 | && DECL_BUILT_IN (olddecl) | |
1096 | && !C_DECL_DECLARED_BUILTIN (olddecl))) | |
51e29401 | 1097 | { |
3c6e6fbf ZW |
1098 | error ("%J'%D' redeclared as different kind of symbol", |
1099 | newdecl, newdecl); | |
1100 | locate_old_decl (olddecl, error); | |
1101 | } | |
1102 | else if (TREE_PUBLIC (newdecl)) | |
1103 | warning ("%Jbuilt-in function '%D' declared as non-function", | |
1104 | newdecl, newdecl); | |
1105 | else if (warn_shadow) | |
f75fbaf7 | 1106 | warning ("%Jdeclaration of '%D' shadows a built-in function", |
3c6e6fbf ZW |
1107 | newdecl, newdecl); |
1108 | return false; | |
1109 | } | |
1110 | ||
132da1a5 | 1111 | if (!comptypes (oldtype, newtype)) |
3c6e6fbf | 1112 | { |
a6f78652 | 1113 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
f75fbaf7 | 1114 | && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl)) |
3c6e6fbf ZW |
1115 | { |
1116 | /* Accept harmless mismatch in function types. | |
1117 | This is for the ffs and fprintf builtins. */ | |
1118 | tree trytype = match_builtin_function_types (newtype, oldtype); | |
1119 | ||
132da1a5 | 1120 | if (trytype && comptypes (newtype, trytype)) |
bc298aa7 | 1121 | *oldtypep = oldtype = trytype; |
3c6e6fbf | 1122 | else |
51e29401 | 1123 | { |
3c6e6fbf ZW |
1124 | /* If types don't match for a built-in, throw away the |
1125 | built-in. No point in calling locate_old_decl here, it | |
9ac97460 | 1126 | won't print anything. */ |
3c6e6fbf ZW |
1127 | warning ("%Jconflicting types for built-in function '%D'", |
1128 | newdecl, newdecl); | |
1129 | return false; | |
51e29401 | 1130 | } |
3c6e6fbf ZW |
1131 | } |
1132 | else if (TREE_CODE (olddecl) == FUNCTION_DECL | |
3c20847b | 1133 | && DECL_IS_BUILTIN (olddecl)) |
3c6e6fbf ZW |
1134 | { |
1135 | /* A conflicting function declaration for a predeclared | |
1136 | function that isn't actually built in. Objective C uses | |
1137 | these. The new declaration silently overrides everything | |
1138 | but the volatility (i.e. noreturn) indication. See also | |
1139 | below. FIXME: Make Objective C use normal builtins. */ | |
1140 | TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); | |
1141 | return false; | |
1142 | } | |
1143 | /* Permit void foo (...) to match int foo (...) if the latter is | |
1144 | the definition and implicit int was used. See | |
1145 | c-torture/compile/920625-2.c. */ | |
1146 | else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) | |
1147 | && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node | |
1148 | && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node | |
1149 | && C_FUNCTION_IMPLICIT_INT (newdecl)) | |
1150 | { | |
1151 | pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl); | |
1152 | /* Make sure we keep void as the return type. */ | |
bc298aa7 | 1153 | TREE_TYPE (newdecl) = *newtypep = newtype = oldtype; |
3c6e6fbf ZW |
1154 | C_FUNCTION_IMPLICIT_INT (newdecl) = 0; |
1155 | pedwarned = true; | |
51e29401 RS |
1156 | } |
1157 | else | |
1158 | { | |
118a3a8b EC |
1159 | if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype)) |
1160 | error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl); | |
1161 | else | |
1162 | error ("%Jconflicting types for '%D'", newdecl, newdecl); | |
3c6e6fbf ZW |
1163 | diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype); |
1164 | locate_old_decl (olddecl, error); | |
1165 | return false; | |
51e29401 | 1166 | } |
51e29401 RS |
1167 | } |
1168 | ||
3c6e6fbf ZW |
1169 | /* Redeclaration of a type is a constraint violation (6.7.2.3p1), |
1170 | but silently ignore the redeclaration if either is in a system | |
1171 | header. (Conflicting redeclarations were handled above.) */ | |
1172 | if (TREE_CODE (newdecl) == TYPE_DECL) | |
339a28b9 | 1173 | { |
3c6e6fbf | 1174 | if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl)) |
a1105617 | 1175 | return true; /* Allow OLDDECL to continue in use. */ |
118a3a8b | 1176 | |
3c6e6fbf ZW |
1177 | error ("%Jredefinition of typedef '%D'", newdecl, newdecl); |
1178 | locate_old_decl (olddecl, error); | |
1179 | return false; | |
339a28b9 | 1180 | } |
51e29401 | 1181 | |
3c6e6fbf ZW |
1182 | /* Function declarations can either be 'static' or 'extern' (no |
1183 | qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore | |
1184 | can never conflict with each other on account of linkage (6.2.2p4). | |
1185 | Multiple definitions are not allowed (6.9p3,5) but GCC permits | |
1186 | two definitions if one is 'extern inline' and one is not. The non- | |
1187 | extern-inline definition supersedes the extern-inline definition. */ | |
1188 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
51e29401 | 1189 | { |
04b8f97f ZW |
1190 | /* If you declare a built-in function name as static, or |
1191 | define the built-in with an old-style definition (so we | |
1192 | can't validate the argument list) the built-in definition is | |
1193 | overridden, but optionally warn this was a bad choice of name. */ | |
1194 | if (DECL_BUILT_IN (olddecl) | |
f75fbaf7 | 1195 | && !C_DECL_DECLARED_BUILTIN (olddecl) |
04b8f97f ZW |
1196 | && (!TREE_PUBLIC (newdecl) |
1197 | || (DECL_INITIAL (newdecl) | |
1198 | && !TYPE_ARG_TYPES (TREE_TYPE (newdecl))))) | |
51e29401 | 1199 | { |
51e29401 | 1200 | if (warn_shadow) |
f75fbaf7 ZW |
1201 | warning ("%Jdeclaration of '%D' shadows a built-in function", |
1202 | newdecl, newdecl); | |
51e29401 | 1203 | /* Discard the old built-in function. */ |
3c6e6fbf | 1204 | return false; |
51e29401 | 1205 | } |
118a3a8b | 1206 | |
3c6e6fbf | 1207 | if (DECL_INITIAL (newdecl)) |
4c41bbfa | 1208 | { |
3c6e6fbf ZW |
1209 | if (DECL_INITIAL (olddecl) |
1210 | && !(DECL_DECLARED_INLINE_P (olddecl) | |
1211 | && DECL_EXTERNAL (olddecl) | |
1212 | && !(DECL_DECLARED_INLINE_P (newdecl) | |
1213 | && DECL_EXTERNAL (newdecl)))) | |
6907ddd3 | 1214 | { |
3c6e6fbf ZW |
1215 | error ("%Jredefinition of '%D'", newdecl, newdecl); |
1216 | locate_old_decl (olddecl, error); | |
1217 | return false; | |
4c41bbfa RS |
1218 | } |
1219 | } | |
3c6e6fbf ZW |
1220 | /* If we have a prototype after an old-style function definition, |
1221 | the argument types must be checked specially. */ | |
1222 | else if (DECL_INITIAL (olddecl) | |
1223 | && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype) | |
1224 | && TYPE_ACTUAL_ARG_TYPES (oldtype) | |
1225 | && !validate_proto_after_old_defn (newdecl, newtype, oldtype)) | |
52b6a22f | 1226 | { |
3c6e6fbf ZW |
1227 | locate_old_decl (olddecl, error); |
1228 | return false; | |
52b6a22f | 1229 | } |
9aaabf8a ZW |
1230 | /* A non-static declaration (even an "extern") followed by a |
1231 | static declaration is undefined behavior per C99 6.2.2p3-5,7. | |
1232 | The same is true for a static forward declaration at block | |
1233 | scope followed by a non-static declaration/definition at file | |
1234 | scope. Static followed by non-static at the same scope is | |
1235 | not undefined behavior, and is the most convenient way to get | |
1236 | some effects (see e.g. what unwind-dw2-fde-glibc.c does to | |
1237 | the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but | |
1238 | we do diagnose it if -Wtraditional. */ | |
3c6e6fbf | 1239 | if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl)) |
51e29401 | 1240 | { |
9aaabf8a ZW |
1241 | /* Two exceptions to the rule. If olddecl is an extern |
1242 | inline, or a predeclared function that isn't actually | |
1243 | built in, newdecl silently overrides olddecl. The latter | |
1244 | occur only in Objective C; see also above. (FIXME: Make | |
1245 | Objective C use normal builtins.) */ | |
1246 | if (!DECL_IS_BUILTIN (olddecl) | |
1247 | && !(DECL_EXTERNAL (olddecl) | |
1248 | && DECL_DECLARED_INLINE_P (olddecl))) | |
3c6e6fbf | 1249 | { |
9aaabf8a ZW |
1250 | error ("%Jstatic declaration of '%D' follows " |
1251 | "non-static declaration", newdecl, newdecl); | |
1252 | locate_old_decl (olddecl, error); | |
3c6e6fbf | 1253 | } |
9aaabf8a | 1254 | return false; |
51e29401 | 1255 | } |
9aaabf8a | 1256 | else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)) |
e841f997 | 1257 | { |
9aaabf8a ZW |
1258 | if (DECL_CONTEXT (olddecl)) |
1259 | { | |
1260 | error ("%Jnon-static declaration of '%D' follows " | |
1261 | "static declaration", newdecl, newdecl); | |
1262 | locate_old_decl (olddecl, error); | |
1263 | return false; | |
1264 | } | |
1265 | else if (warn_traditional) | |
1266 | { | |
1267 | warning ("%Jnon-static declaration of '%D' follows " | |
1268 | "static declaration", newdecl, newdecl); | |
1269 | warned = true; | |
1270 | } | |
e841f997 | 1271 | } |
51e29401 | 1272 | } |
3c6e6fbf | 1273 | else if (TREE_CODE (newdecl) == VAR_DECL) |
339a28b9 | 1274 | { |
3c6e6fbf ZW |
1275 | /* Only variables can be thread-local, and all declarations must |
1276 | agree on this property. */ | |
1277 | if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl)) | |
51e29401 | 1278 | { |
3c6e6fbf ZW |
1279 | if (DECL_THREAD_LOCAL (newdecl)) |
1280 | error ("%Jthread-local declaration of '%D' follows " | |
1281 | "non-thread-local declaration", newdecl, newdecl); | |
1282 | else | |
1283 | error ("%Jnon-thread-local declaration of '%D' follows " | |
1284 | "thread-local declaration", newdecl, newdecl); | |
51e29401 | 1285 | |
3c6e6fbf ZW |
1286 | locate_old_decl (olddecl, error); |
1287 | return false; | |
1288 | } | |
51e29401 | 1289 | |
3c6e6fbf ZW |
1290 | /* Multiple initialized definitions are not allowed (6.9p3,5). */ |
1291 | if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl)) | |
1292 | { | |
1293 | error ("%Jredefinition of '%D'", newdecl, newdecl); | |
1294 | locate_old_decl (olddecl, error); | |
1295 | return false; | |
1296 | } | |
1297 | ||
9aaabf8a ZW |
1298 | /* Objects declared at file scope: if the first declaration had |
1299 | external linkage (even if it was an external reference) the | |
1300 | second must have external linkage as well, or the behavior is | |
1301 | undefined. If the first declaration had internal linkage, then | |
1302 | the second must too, or else be an external reference (in which | |
1303 | case the composite declaration still has internal linkage). | |
1304 | As for function declarations, we warn about the static-then- | |
1305 | extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */ | |
1306 | if (DECL_FILE_SCOPE_P (newdecl) | |
1307 | && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl)) | |
3c6e6fbf | 1308 | { |
9aaabf8a ZW |
1309 | if (DECL_EXTERNAL (newdecl)) |
1310 | { | |
1311 | if (warn_traditional) | |
1312 | { | |
1313 | warning ("%Jnon-static declaration of '%D' follows " | |
1314 | "static declaration", newdecl, newdecl); | |
1315 | warned = true; | |
1316 | } | |
1317 | } | |
1318 | else | |
3c6e6fbf ZW |
1319 | { |
1320 | if (TREE_PUBLIC (newdecl)) | |
1321 | error ("%Jnon-static declaration of '%D' follows " | |
1322 | "static declaration", newdecl, newdecl); | |
1323 | else | |
1324 | error ("%Jstatic declaration of '%D' follows " | |
1325 | "non-static declaration", newdecl, newdecl); | |
1326 | ||
1327 | locate_old_decl (olddecl, error); | |
1328 | return false; | |
51e29401 RS |
1329 | } |
1330 | } | |
3c6e6fbf ZW |
1331 | /* Two objects with the same name declared at the same block |
1332 | scope must both be external references (6.7p3). */ | |
9aaabf8a ZW |
1333 | else if (!DECL_FILE_SCOPE_P (newdecl) |
1334 | && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl) | |
3c6e6fbf ZW |
1335 | && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl))) |
1336 | { | |
1337 | if (DECL_EXTERNAL (newdecl)) | |
1338 | error ("%Jextern declaration of '%D' follows " | |
1339 | "declaration with no linkage", newdecl, newdecl); | |
1340 | else if (DECL_EXTERNAL (olddecl)) | |
1341 | error ("%Jdeclaration of '%D' with no linkage follows " | |
1342 | "extern declaration", newdecl, newdecl); | |
1343 | else | |
1344 | error ("%Jredeclaration of '%D' with no linkage", | |
1345 | newdecl, newdecl); | |
ecf92f82 | 1346 | |
3c6e6fbf ZW |
1347 | locate_old_decl (olddecl, error); |
1348 | return false; | |
1349 | } | |
1ae0ccb6 | 1350 | } |
3c6e6fbf ZW |
1351 | |
1352 | /* warnings */ | |
1353 | /* All decls must agree on a non-default visibility. */ | |
1354 | if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT | |
1355 | && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT | |
1356 | && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) | |
1ae0ccb6 | 1357 | { |
3c6e6fbf ZW |
1358 | warning ("%Jredeclaration of '%D' with different visibility " |
1359 | "(old visibility preserved)", newdecl, newdecl); | |
1360 | warned = true; | |
1ae0ccb6 | 1361 | } |
3c6e6fbf ZW |
1362 | |
1363 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
51e29401 | 1364 | { |
3c6e6fbf ZW |
1365 | /* Diagnose inline __attribute__ ((noinline)) which is silly. */ |
1366 | if (DECL_DECLARED_INLINE_P (newdecl) | |
1367 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl))) | |
51e29401 | 1368 | { |
3c6e6fbf ZW |
1369 | warning ("%Jinline declaration of '%D' follows " |
1370 | "declaration with attribute noinline", newdecl, newdecl); | |
1371 | warned = true; | |
51e29401 | 1372 | } |
3c6e6fbf ZW |
1373 | else if (DECL_DECLARED_INLINE_P (olddecl) |
1374 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))) | |
4b4e3407 | 1375 | { |
3c6e6fbf ZW |
1376 | warning ("%Jdeclaration of '%D' with attribute noinline follows " |
1377 | "inline declaration ", newdecl, newdecl); | |
1378 | warned = true; | |
4b4e3407 | 1379 | } |
3c6e6fbf ZW |
1380 | |
1381 | /* Inline declaration after use or definition. | |
1382 | ??? Should we still warn about this now we have unit-at-a-time | |
1383 | mode and can get it right? */ | |
1384 | if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)) | |
51e29401 | 1385 | { |
3c6e6fbf | 1386 | if (TREE_USED (olddecl)) |
51e29401 | 1387 | { |
0bb03c11 JH |
1388 | warning ("%J'%D' declared inline after being called", |
1389 | olddecl, olddecl); | |
3c6e6fbf ZW |
1390 | warned = true; |
1391 | } | |
1392 | else if (DECL_INITIAL (olddecl)) | |
1393 | { | |
0bb03c11 JH |
1394 | warning ("%J'%D' declared inline after its definition", |
1395 | olddecl, olddecl); | |
3c6e6fbf | 1396 | warned = true; |
51e29401 | 1397 | } |
51e29401 | 1398 | } |
3c6e6fbf | 1399 | } |
3205a71e | 1400 | else /* PARM_DECL, VAR_DECL */ |
3c6e6fbf | 1401 | { |
f75fbaf7 ZW |
1402 | /* Redeclaration of a parameter is a constraint violation (this is |
1403 | not explicitly stated, but follows from C99 6.7p3 [no more than | |
1404 | one declaration of the same identifier with no linkage in the | |
1405 | same scope, except type tags] and 6.2.2p6 [parameters have no | |
1406 | linkage]). We must check for a forward parameter declaration, | |
1407 | indicated by TREE_ASM_WRITTEN on the old declaration - this is | |
1408 | an extension, the mandatory diagnostic for which is handled by | |
1409 | mark_forward_parm_decls. */ | |
1410 | ||
3205a71e ZW |
1411 | if (TREE_CODE (newdecl) == PARM_DECL |
1412 | && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl))) | |
1413 | { | |
1414 | error ("%Jredefinition of parameter '%D'", newdecl, newdecl); | |
1415 | locate_old_decl (olddecl, error); | |
1416 | return false; | |
1417 | } | |
51e29401 RS |
1418 | } |
1419 | ||
3c6e6fbf ZW |
1420 | /* Optional warning for completely redundant decls. */ |
1421 | if (!warned && !pedwarned | |
1422 | && warn_redundant_decls | |
1423 | /* Don't warn about a function declaration followed by a | |
1424 | definition. */ | |
3205a71e ZW |
1425 | && !(TREE_CODE (newdecl) == FUNCTION_DECL |
1426 | && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)) | |
1427 | /* Don't warn about an extern followed by a definition. */ | |
1428 | && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)) | |
1429 | /* Don't warn about forward parameter decls. */ | |
1430 | && !(TREE_CODE (newdecl) == PARM_DECL | |
1431 | && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))) | |
27f427f8 | 1432 | { |
3c6e6fbf ZW |
1433 | warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl); |
1434 | warned = true; | |
27f427f8 RS |
1435 | } |
1436 | ||
3c6e6fbf ZW |
1437 | /* Report location of previous decl/defn in a consistent manner. */ |
1438 | if (warned || pedwarned) | |
1439 | locate_old_decl (olddecl, pedwarned ? pedwarn : warning); | |
664b4b1e | 1440 | |
3c6e6fbf ZW |
1441 | return true; |
1442 | } | |
51e29401 | 1443 | |
3c6e6fbf ZW |
1444 | /* Subroutine of duplicate_decls. NEWDECL has been found to be |
1445 | consistent with OLDDECL, but carries new information. Merge the | |
1ef82ef2 | 1446 | new information into OLDDECL. This function issues no |
3c6e6fbf ZW |
1447 | diagnostics. */ |
1448 | ||
1449 | static void | |
1ef82ef2 | 1450 | merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype) |
3c6e6fbf ZW |
1451 | { |
1452 | int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL | |
1453 | && DECL_INITIAL (newdecl) != 0); | |
1454 | ||
f75fbaf7 ZW |
1455 | /* For real parm decl following a forward decl, rechain the old decl |
1456 | in its new location and clear TREE_ASM_WRITTEN (it's not a | |
1457 | forward decl anymore). */ | |
3c6e6fbf ZW |
1458 | if (TREE_CODE (newdecl) == PARM_DECL |
1459 | && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl)) | |
51e29401 | 1460 | { |
f75fbaf7 ZW |
1461 | struct c_binding *b, **here; |
1462 | ||
1463 | for (here = ¤t_scope->bindings; *here; here = &(*here)->prev) | |
1464 | if ((*here)->decl == olddecl) | |
1465 | goto found; | |
1466 | abort (); | |
1467 | ||
1468 | found: | |
1469 | b = *here; | |
1470 | *here = b->prev; | |
1471 | b->prev = current_scope->bindings; | |
1472 | current_scope->bindings = b; | |
1473 | ||
3c6e6fbf | 1474 | TREE_ASM_WRITTEN (olddecl) = 0; |
3c6e6fbf ZW |
1475 | } |
1476 | ||
1477 | DECL_ATTRIBUTES (newdecl) | |
5fd9b178 | 1478 | = targetm.merge_decl_attributes (olddecl, newdecl); |
bf44f7de | 1479 | |
3c6e6fbf | 1480 | /* Merge the data types specified in the two decls. */ |
1ef82ef2 ZW |
1481 | TREE_TYPE (newdecl) |
1482 | = TREE_TYPE (olddecl) | |
10bc1b1b | 1483 | = composite_type (newtype, oldtype); |
51e29401 | 1484 | |
3c6e6fbf ZW |
1485 | /* Lay the type out, unless already done. */ |
1486 | if (oldtype != TREE_TYPE (newdecl)) | |
1487 | { | |
1488 | if (TREE_TYPE (newdecl) != error_mark_node) | |
1489 | layout_type (TREE_TYPE (newdecl)); | |
1490 | if (TREE_CODE (newdecl) != FUNCTION_DECL | |
1491 | && TREE_CODE (newdecl) != TYPE_DECL | |
1492 | && TREE_CODE (newdecl) != CONST_DECL) | |
1493 | layout_decl (newdecl, 0); | |
1494 | } | |
1495 | else | |
1496 | { | |
1497 | /* Since the type is OLDDECL's, make OLDDECL's size go with. */ | |
1498 | DECL_SIZE (newdecl) = DECL_SIZE (olddecl); | |
1499 | DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); | |
1500 | DECL_MODE (newdecl) = DECL_MODE (olddecl); | |
1501 | if (TREE_CODE (olddecl) != FUNCTION_DECL) | |
1502 | if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) | |
1503 | { | |
1504 | DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); | |
1505 | DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl); | |
1506 | } | |
1507 | } | |
fc542d3c | 1508 | |
3c6e6fbf ZW |
1509 | /* Keep the old rtl since we can safely use it. */ |
1510 | COPY_DECL_RTL (olddecl, newdecl); | |
17aec3eb | 1511 | |
3c6e6fbf ZW |
1512 | /* Merge the type qualifiers. */ |
1513 | if (TREE_READONLY (newdecl)) | |
1ef82ef2 | 1514 | TREE_READONLY (olddecl) = 1; |
facef326 | 1515 | |
3c6e6fbf ZW |
1516 | if (TREE_THIS_VOLATILE (newdecl)) |
1517 | { | |
1ef82ef2 | 1518 | TREE_THIS_VOLATILE (olddecl) = 1; |
3c6e6fbf ZW |
1519 | if (TREE_CODE (newdecl) == VAR_DECL) |
1520 | make_var_volatile (newdecl); | |
51e29401 | 1521 | } |
3c6e6fbf ZW |
1522 | |
1523 | /* Keep source location of definition rather than declaration. */ | |
1ef82ef2 | 1524 | if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0) |
3c6e6fbf ZW |
1525 | DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); |
1526 | ||
1527 | /* Merge the unused-warning information. */ | |
1528 | if (DECL_IN_SYSTEM_HEADER (olddecl)) | |
1529 | DECL_IN_SYSTEM_HEADER (newdecl) = 1; | |
1530 | else if (DECL_IN_SYSTEM_HEADER (newdecl)) | |
1ef82ef2 | 1531 | DECL_IN_SYSTEM_HEADER (olddecl) = 1; |
3c6e6fbf ZW |
1532 | |
1533 | /* Merge the initialization information. */ | |
1ef82ef2 | 1534 | if (DECL_INITIAL (newdecl) == 0) |
3c6e6fbf ZW |
1535 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); |
1536 | ||
1537 | /* Merge the section attribute. | |
1538 | We want to issue an error if the sections conflict but that must be | |
1539 | done later in decl_attributes since we are called before attributes | |
1540 | are assigned. */ | |
1541 | if (DECL_SECTION_NAME (newdecl) == NULL_TREE) | |
1542 | DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl); | |
1543 | ||
1544 | /* Copy the assembler name. | |
1545 | Currently, it can only be defined in the prototype. */ | |
1546 | COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); | |
1547 | ||
1548 | /* If either declaration has a nondefault visibility, use it. */ | |
1549 | if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT) | |
1550 | DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); | |
1551 | ||
1552 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
51e29401 | 1553 | { |
3c6e6fbf ZW |
1554 | DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl); |
1555 | DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); | |
1556 | DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); | |
1557 | DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) | |
1558 | |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); | |
1559 | TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); | |
1560 | TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); | |
1561 | DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); | |
1562 | DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl); | |
51e29401 RS |
1563 | } |
1564 | ||
1565 | /* Merge the storage class information. */ | |
45806a3f FS |
1566 | merge_weak (newdecl, olddecl); |
1567 | ||
51e29401 RS |
1568 | /* For functions, static overrides non-static. */ |
1569 | if (TREE_CODE (newdecl) == FUNCTION_DECL) | |
1570 | { | |
1571 | TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); | |
1572 | /* This is since we don't automatically | |
1573 | copy the attributes of NEWDECL into OLDDECL. */ | |
1574 | TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); | |
1575 | /* If this clears `static', clear it in the identifier too. */ | |
1576 | if (! TREE_PUBLIC (olddecl)) | |
1577 | TREE_PUBLIC (DECL_NAME (olddecl)) = 0; | |
1578 | } | |
1394aabd | 1579 | if (DECL_EXTERNAL (newdecl)) |
51e29401 | 1580 | { |
1ef82ef2 ZW |
1581 | TREE_STATIC (newdecl) = TREE_STATIC (olddecl); |
1582 | DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl); | |
1583 | ||
51e29401 RS |
1584 | /* An extern decl does not override previous storage class. */ |
1585 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); | |
0ecef3cf | 1586 | if (! DECL_EXTERNAL (newdecl)) |
d1bd0ded GK |
1587 | { |
1588 | DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); | |
b68a40b2 | 1589 | DECL_COMMON (newdecl) = DECL_COMMON (olddecl); |
d1bd0ded | 1590 | } |
51e29401 RS |
1591 | } |
1592 | else | |
1593 | { | |
1594 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl); | |
51e29401 RS |
1595 | TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); |
1596 | } | |
850cba29 | 1597 | |
bf44f7de | 1598 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
51e29401 | 1599 | { |
5daf7c0a JM |
1600 | /* If we're redefining a function previously defined as extern |
1601 | inline, make sure we emit debug info for the inline before we | |
1602 | throw it away, in case it was inlined into a function that hasn't | |
1603 | been written out yet. */ | |
02a362d9 | 1604 | if (new_is_definition && DECL_INITIAL (olddecl)) |
5daf7c0a | 1605 | { |
1bb17c21 JH |
1606 | if (TREE_USED (olddecl) |
1607 | /* In unit-at-a-time mode we never inline re-defined extern | |
6614fd40 | 1608 | inline functions. */ |
b684a3df JH |
1609 | && !flag_unit_at_a_time |
1610 | && cgraph_function_possibly_inlined_p (olddecl)) | |
02a362d9 | 1611 | (*debug_hooks->outlining_inline_function) (olddecl); |
17aec3eb | 1612 | |
5daf7c0a JM |
1613 | /* The new defn must not be inline. */ |
1614 | DECL_INLINE (newdecl) = 0; | |
1615 | DECL_UNINLINABLE (newdecl) = 1; | |
1616 | } | |
1617 | else | |
1618 | { | |
1619 | /* If either decl says `inline', this fn is inline, | |
1620 | unless its definition was passed already. */ | |
31ed8fea | 1621 | if (DECL_DECLARED_INLINE_P (newdecl) |
b79d5213 RH |
1622 | || DECL_DECLARED_INLINE_P (olddecl)) |
1623 | DECL_DECLARED_INLINE_P (newdecl) = 1; | |
9162542e AO |
1624 | |
1625 | DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) | |
1626 | = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); | |
5daf7c0a | 1627 | } |
17aec3eb | 1628 | |
51e29401 RS |
1629 | if (DECL_BUILT_IN (olddecl)) |
1630 | { | |
f75fbaf7 ZW |
1631 | /* If redeclaring a builtin function, it stays built in. |
1632 | But it gets tagged as having been declared. */ | |
a6f78652 ZW |
1633 | DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl); |
1634 | DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl); | |
f75fbaf7 | 1635 | C_DECL_DECLARED_BUILTIN (newdecl) = 1; |
51e29401 | 1636 | } |
b79d5213 | 1637 | |
bf44f7de | 1638 | /* Also preserve various other info from the definition. */ |
bf44f7de JW |
1639 | if (! new_is_definition) |
1640 | { | |
1641 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); | |
1ef82ef2 | 1642 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); |
1da326c3 | 1643 | DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); |
23700f65 | 1644 | DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); |
bf44f7de | 1645 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); |
b79d5213 RH |
1646 | |
1647 | /* Set DECL_INLINE on the declaration if we've got a body | |
1648 | from which to instantiate. */ | |
1649 | if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl)) | |
1650 | { | |
1651 | DECL_INLINE (newdecl) = 1; | |
1652 | DECL_ABSTRACT_ORIGIN (newdecl) | |
1ef82ef2 | 1653 | = DECL_ABSTRACT_ORIGIN (olddecl); |
b79d5213 RH |
1654 | } |
1655 | } | |
1656 | else | |
1657 | { | |
1658 | /* If a previous declaration said inline, mark the | |
1659 | definition as inlinable. */ | |
1660 | if (DECL_DECLARED_INLINE_P (newdecl) | |
1661 | && ! DECL_UNINLINABLE (newdecl)) | |
1662 | DECL_INLINE (newdecl) = 1; | |
bf44f7de JW |
1663 | } |
1664 | } | |
51e29401 | 1665 | |
850cba29 | 1666 | /* Copy most of the decl-specific fields of NEWDECL into OLDDECL. |
e5e809f4 | 1667 | But preserve OLDDECL's DECL_UID. */ |
850cba29 | 1668 | { |
b3694847 | 1669 | unsigned olddecl_uid = DECL_UID (olddecl); |
850cba29 | 1670 | |
da61dec9 JM |
1671 | memcpy ((char *) olddecl + sizeof (struct tree_common), |
1672 | (char *) newdecl + sizeof (struct tree_common), | |
1673 | sizeof (struct tree_decl) - sizeof (struct tree_common)); | |
850cba29 RS |
1674 | DECL_UID (olddecl) = olddecl_uid; |
1675 | } | |
51e29401 | 1676 | |
2306d91c RH |
1677 | /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl |
1678 | so that encode_section_info has a chance to look at the new decl | |
1679 | flags and attributes. */ | |
1680 | if (DECL_RTL_SET_P (olddecl) | |
1681 | && (TREE_CODE (olddecl) == FUNCTION_DECL | |
1682 | || (TREE_CODE (olddecl) == VAR_DECL | |
1683 | && TREE_STATIC (olddecl)))) | |
1684 | make_decl_rtl (olddecl, NULL); | |
51e29401 RS |
1685 | } |
1686 | ||
3c6e6fbf ZW |
1687 | /* Handle when a new declaration NEWDECL has the same name as an old |
1688 | one OLDDECL in the same binding contour. Prints an error message | |
1689 | if appropriate. | |
1690 | ||
1691 | If safely possible, alter OLDDECL to look like NEWDECL, and return | |
1ef82ef2 | 1692 | true. Otherwise, return false. */ |
3c6e6fbf ZW |
1693 | |
1694 | static bool | |
1ef82ef2 | 1695 | duplicate_decls (tree newdecl, tree olddecl) |
3c6e6fbf | 1696 | { |
6de9cd9a | 1697 | tree newtype = NULL, oldtype = NULL; |
3c6e6fbf ZW |
1698 | |
1699 | if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype)) | |
1700 | return false; | |
1701 | ||
1ef82ef2 ZW |
1702 | merge_decls (newdecl, olddecl, newtype, oldtype); |
1703 | return true; | |
3c6e6fbf | 1704 | } |
118a3a8b | 1705 | |
3c6e6fbf | 1706 | \f |
f75fbaf7 | 1707 | /* Check whether decl-node NEW shadows an existing declaration. */ |
339a28b9 | 1708 | static void |
f75fbaf7 | 1709 | warn_if_shadowing (tree new) |
339a28b9 | 1710 | { |
f75fbaf7 | 1711 | struct c_binding *b; |
339a28b9 | 1712 | |
f75fbaf7 ZW |
1713 | /* Shadow warnings wanted? */ |
1714 | if (!warn_shadow | |
339a28b9 | 1715 | /* No shadow warnings for internally generated vars. */ |
3c20847b | 1716 | || DECL_IS_BUILTIN (new) |
339a28b9 | 1717 | /* No shadow warnings for vars made for inlining. */ |
f75fbaf7 | 1718 | || DECL_FROM_INLINE (new) |
339a28b9 | 1719 | /* Don't warn about the parm names in function declarator |
f75fbaf7 ZW |
1720 | within a function declarator. It would be nice to avoid |
1721 | warning in any function declarator in a declaration, as | |
1722 | opposed to a definition, but there is no way to tell | |
1723 | it's not a definition at this point. */ | |
1724 | || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag)) | |
26f943fd NB |
1725 | return; |
1726 | ||
9aaabf8a | 1727 | /* Is anything being shadowed? Invisible decls do not count. */ |
f75fbaf7 | 1728 | for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed) |
9aaabf8a | 1729 | if (b->decl && b->decl != new && !b->invisible) |
f75fbaf7 ZW |
1730 | { |
1731 | tree old = b->decl; | |
1732 | ||
1733 | if (TREE_CODE (old) == PARM_DECL) | |
1734 | warning ("%Jdeclaration of '%D' shadows a parameter", new, new); | |
1735 | else if (DECL_FILE_SCOPE_P (old)) | |
1736 | warning ("%Jdeclaration of '%D' shadows a global declaration", | |
1737 | new, new); | |
1738 | else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old)) | |
1739 | warning ("%Jdeclaration of '%D' shadows a built-in function", | |
1740 | new, new); | |
1741 | else | |
1742 | warning ("%Jdeclaration of '%D' shadows a previous local", new, new); | |
1743 | ||
1744 | if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old)) | |
1745 | warning ("%Jshadowed declaration is here", old); | |
a6f78652 | 1746 | |
f75fbaf7 ZW |
1747 | break; |
1748 | } | |
339a28b9 ZW |
1749 | } |
1750 | ||
1751 | ||
1752 | /* Subroutine of pushdecl. | |
1753 | ||
1754 | X is a TYPE_DECL for a typedef statement. Create a brand new | |
1755 | ..._TYPE node (which will be just a variant of the existing | |
1756 | ..._TYPE node with identical properties) and then install X | |
1757 | as the TYPE_NAME of this brand new (duplicate) ..._TYPE node. | |
1758 | ||
1759 | The whole point here is to end up with a situation where each | |
1760 | and every ..._TYPE node the compiler creates will be uniquely | |
1761 | associated with AT MOST one node representing a typedef name. | |
1762 | This way, even though the compiler substitutes corresponding | |
1763 | ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very | |
1764 | early on, later parts of the compiler can always do the reverse | |
1765 | translation and get back the corresponding typedef name. For | |
1766 | example, given: | |
1767 | ||
1768 | typedef struct S MY_TYPE; | |
1769 | MY_TYPE object; | |
1770 | ||
1771 | Later parts of the compiler might only know that `object' was of | |
1772 | type `struct S' if it were not for code just below. With this | |
1773 | code however, later parts of the compiler see something like: | |
1774 | ||
1775 | struct S' == struct S | |
1776 | typedef struct S' MY_TYPE; | |
1777 | struct S' object; | |
1778 | ||
1779 | And they can then deduce (from the node for type struct S') that | |
1780 | the original object declaration was: | |
1781 | ||
1782 | MY_TYPE object; | |
1783 | ||
1784 | Being able to do this is important for proper support of protoize, | |
1785 | and also for generating precise symbolic debugging information | |
1786 | which takes full account of the programmer's (typedef) vocabulary. | |
1787 | ||
1788 | Obviously, we don't want to generate a duplicate ..._TYPE node if | |
1789 | the TYPE_DECL node that we are now processing really represents a | |
1790 | standard built-in type. | |
1791 | ||
1792 | Since all standard types are effectively declared at line zero | |
1793 | in the source file, we can easily check to see if we are working | |
1794 | on a standard type by checking the current value of lineno. */ | |
1795 | ||
1796 | static void | |
35b1a6fa | 1797 | clone_underlying_type (tree x) |
339a28b9 | 1798 | { |
3c20847b | 1799 | if (DECL_IS_BUILTIN (x)) |
26f943fd | 1800 | { |
339a28b9 ZW |
1801 | if (TYPE_NAME (TREE_TYPE (x)) == 0) |
1802 | TYPE_NAME (TREE_TYPE (x)) = x; | |
1803 | } | |
1804 | else if (TREE_TYPE (x) != error_mark_node | |
1805 | && DECL_ORIGINAL_TYPE (x) == NULL_TREE) | |
1806 | { | |
1807 | tree tt = TREE_TYPE (x); | |
1808 | DECL_ORIGINAL_TYPE (x) = tt; | |
1809 | tt = build_type_copy (tt); | |
1810 | TYPE_NAME (tt) = x; | |
1811 | TREE_USED (tt) = TREE_USED (x); | |
1812 | TREE_TYPE (x) = tt; | |
26f943fd NB |
1813 | } |
1814 | } | |
1815 | ||
51e29401 RS |
1816 | /* Record a decl-node X as belonging to the current lexical scope. |
1817 | Check for errors (such as an incompatible declaration for the same | |
1818 | name already seen in the same scope). | |
1819 | ||
1820 | Returns either X or an old decl for the same name. | |
1821 | If an old decl is returned, it may have been smashed | |
1822 | to agree with what X says. */ | |
1823 | ||
1824 | tree | |
35b1a6fa | 1825 | pushdecl (tree x) |
51e29401 | 1826 | { |
b3694847 | 1827 | tree name = DECL_NAME (x); |
f8521984 | 1828 | struct c_scope *scope = current_scope; |
f75fbaf7 | 1829 | struct c_binding *b; |
9aaabf8a | 1830 | bool nested = false; |
51e29401 | 1831 | |
31ed8fea RH |
1832 | /* Functions need the lang_decl data. */ |
1833 | if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x)) | |
703ad42b | 1834 | DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl)); |
31ed8fea | 1835 | |
eecec698 ZW |
1836 | /* Must set DECL_CONTEXT for everything not at file scope or |
1837 | DECL_FILE_SCOPE_P won't work. Local externs don't count | |
1838 | unless they have initializers (which generate code). */ | |
1839 | if (current_function_decl | |
1840 | && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL) | |
1841 | || DECL_INITIAL (x) || !DECL_EXTERNAL (x))) | |
339a28b9 | 1842 | DECL_CONTEXT (x) = current_function_decl; |
e13e48e7 | 1843 | |
f75fbaf7 ZW |
1844 | /* Anonymous decls are just inserted in the scope. */ |
1845 | if (!name) | |
51e29401 | 1846 | { |
9aaabf8a | 1847 | bind (name, x, scope, /*invisible=*/false, /*nested=*/false); |
f75fbaf7 ZW |
1848 | return x; |
1849 | } | |
1850 | ||
1851 | /* First, see if there is another declaration with the same name in | |
1852 | the current scope. If there is, duplicate_decls may do all the | |
1853 | work for us. If duplicate_decls returns false, that indicates | |
1854 | two incompatible decls in the same scope; we are to silently | |
1855 | replace the old one (duplicate_decls has issued all appropriate | |
1856 | diagnostics). In particular, we should not consider possible | |
1857 | duplicates in the external scope, or shadowing. */ | |
1858 | b = I_SYMBOL_BINDING (name); | |
9aaabf8a | 1859 | if (b && B_IN_SCOPE (b, scope)) |
f75fbaf7 ZW |
1860 | { |
1861 | if (duplicate_decls (x, b->decl)) | |
1862 | return b->decl; | |
1863 | else | |
1864 | goto skip_external_and_shadow_checks; | |
1865 | } | |
1866 | ||
1867 | /* All declarations with external linkage, and all external | |
1868 | references, go in the external scope, no matter what scope is | |
1869 | current. However, the binding in that scope is ignored for | |
1870 | purposes of normal name lookup. A separate binding structure is | |
1871 | created in the requested scope; this governs the normal | |
1872 | visibility of the symbol. | |
51e29401 | 1873 | |
f75fbaf7 ZW |
1874 | The binding in the externals scope is used exclusively for |
1875 | detecting duplicate declarations of the same object, no matter | |
1876 | what scope they are in; this is what we do here. (C99 6.2.7p2: | |
1877 | All declarations that refer to the same object or function shall | |
1878 | have compatible type; otherwise, the behavior is undefined.) */ | |
1879 | if (DECL_EXTERNAL (x) || scope == file_scope) | |
1880 | { | |
234f46ae | 1881 | if (warn_nested_externs |
f75fbaf7 | 1882 | && scope != file_scope |
234f46ae | 1883 | && !DECL_IN_SYSTEM_HEADER (x)) |
9aaabf8a | 1884 | warning ("nested extern declaration of '%D'", x); |
234f46ae | 1885 | |
9aaabf8a | 1886 | while (b && !B_IN_EXTERNAL_SCOPE (b)) |
f75fbaf7 ZW |
1887 | b = b->shadowed; |
1888 | ||
1889 | /* The point of the same_translation_unit_p check here is, | |
1890 | we want to detect a duplicate decl for a construct like | |
1891 | foo() { extern bar(); } ... static bar(); but not if | |
1892 | they are in different translation units. In any case, | |
1893 | the static does not go in the externals scope. */ | |
1894 | if (b | |
1895 | && (DECL_EXTERNAL (x) || TREE_PUBLIC (x) | |
1896 | || same_translation_unit_p (x, b->decl)) | |
1897 | && duplicate_decls (x, b->decl)) | |
55d54003 | 1898 | { |
9aaabf8a | 1899 | bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); |
f75fbaf7 | 1900 | return b->decl; |
55d54003 | 1901 | } |
f75fbaf7 | 1902 | else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x)) |
6645c3fa | 1903 | { |
9aaabf8a ZW |
1904 | bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); |
1905 | nested = true; | |
51e29401 | 1906 | } |
f75fbaf7 | 1907 | } |
9aaabf8a ZW |
1908 | /* Similarly, a declaration of a function with static linkage at |
1909 | block scope must be checked against any existing declaration | |
1910 | of that function at file scope. */ | |
1911 | else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope | |
1912 | && !TREE_PUBLIC (x) && !DECL_INITIAL (x)) | |
1913 | { | |
1914 | if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x)) | |
1915 | warning ("nested static declaration of '%D'", x); | |
1916 | ||
1917 | while (b && !B_IN_FILE_SCOPE (b)) | |
1918 | b = b->shadowed; | |
1919 | ||
1920 | if (b && same_translation_unit_p (x, b->decl) | |
1921 | && duplicate_decls (x, b->decl)) | |
1922 | { | |
1923 | bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true); | |
1924 | return b->decl; | |
1925 | } | |
1926 | else | |
1927 | { | |
1928 | bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false); | |
1929 | nested = true; | |
1930 | } | |
1931 | } | |
35b1a6fa | 1932 | |
f75fbaf7 | 1933 | warn_if_shadowing (x); |
51e29401 | 1934 | |
f75fbaf7 ZW |
1935 | skip_external_and_shadow_checks: |
1936 | if (TREE_CODE (x) == TYPE_DECL) | |
1937 | clone_underlying_type (x); | |
51e29401 | 1938 | |
9aaabf8a | 1939 | bind (name, x, scope, /*invisible=*/false, nested); |
f75fbaf7 ZW |
1940 | |
1941 | /* If x's type is incomplete because it's based on a | |
1942 | structure or union which has not yet been fully declared, | |
1943 | attach it to that structure or union type, so we can go | |
1944 | back and complete the variable declaration later, if the | |
1945 | structure or union gets fully declared. | |
51e29401 | 1946 | |
f75fbaf7 ZW |
1947 | If the input is erroneous, we can have error_mark in the type |
1948 | slot (e.g. "f(void a, ...)") - that doesn't count as an | |
1949 | incomplete type. */ | |
1950 | if (TREE_TYPE (x) != error_mark_node | |
1951 | && !COMPLETE_TYPE_P (TREE_TYPE (x))) | |
1952 | { | |
1953 | tree element = TREE_TYPE (x); | |
1954 | ||
1955 | while (TREE_CODE (element) == ARRAY_TYPE) | |
1956 | element = TREE_TYPE (element); | |
1957 | element = TYPE_MAIN_VARIANT (element); | |
1958 | ||
1959 | if ((TREE_CODE (element) == RECORD_TYPE | |
1960 | || TREE_CODE (element) == UNION_TYPE) | |
1961 | && (TREE_CODE (x) != TYPE_DECL | |
1962 | || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE) | |
1963 | && !COMPLETE_TYPE_P (element)) | |
1964 | C_TYPE_INCOMPLETE_VARS (element) | |
1965 | = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element)); | |
1966 | } | |
51e29401 RS |
1967 | return x; |
1968 | } | |
1969 | ||
f75fbaf7 | 1970 | /* Record X as belonging to file scope. |
6356f892 | 1971 | This is used only internally by the Objective-C front end, |
f91f41b2 ZW |
1972 | and is limited to its needs. duplicate_decls is not called; |
1973 | if there is any preexisting decl for this identifier, it is an ICE. */ | |
1974 | ||
339a28b9 | 1975 | tree |
35b1a6fa | 1976 | pushdecl_top_level (tree x) |
6970c06a | 1977 | { |
f91f41b2 | 1978 | tree name; |
9aaabf8a | 1979 | bool nested = false; |
339a28b9 ZW |
1980 | |
1981 | if (TREE_CODE (x) != VAR_DECL) | |
1982 | abort (); | |
1983 | ||
1984 | name = DECL_NAME (x); | |
339a28b9 | 1985 | |
f75fbaf7 | 1986 | if (I_SYMBOL_BINDING (name)) |
f91f41b2 | 1987 | abort (); |
6970c06a | 1988 | |
f75fbaf7 ZW |
1989 | if (DECL_EXTERNAL (x) || TREE_PUBLIC (x)) |
1990 | { | |
9aaabf8a ZW |
1991 | bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false); |
1992 | nested = true; | |
f75fbaf7 ZW |
1993 | } |
1994 | if (file_scope) | |
9aaabf8a | 1995 | bind (name, x, file_scope, /*invisible=*/false, nested); |
51e29401 | 1996 | |
f91f41b2 | 1997 | return x; |
51e29401 RS |
1998 | } |
1999 | \f | |
f75fbaf7 ZW |
2000 | static void |
2001 | implicit_decl_warning (tree id, tree olddecl) | |
2002 | { | |
2003 | void (*diag) (const char *, ...); | |
2004 | switch (mesg_implicit_function_declaration) | |
2005 | { | |
2006 | case 0: return; | |
2007 | case 1: diag = warning; break; | |
2008 | case 2: diag = error; break; | |
2009 | default: abort (); | |
2010 | } | |
2011 | ||
2012 | diag (N_("implicit declaration of function '%E'"), id); | |
2013 | if (olddecl) | |
2014 | locate_old_decl (olddecl, diag); | |
2015 | } | |
2016 | ||
339a28b9 ZW |
2017 | /* Generate an implicit declaration for identifier FUNCTIONID as a |
2018 | function of type int (). */ | |
51e29401 RS |
2019 | |
2020 | tree | |
35b1a6fa | 2021 | implicitly_declare (tree functionid) |
51e29401 | 2022 | { |
f75fbaf7 | 2023 | tree decl = lookup_name_in_scope (functionid, external_scope); |
51e29401 | 2024 | |
f91f41b2 | 2025 | if (decl) |
339a28b9 | 2026 | { |
f75fbaf7 ZW |
2027 | /* FIXME: Objective-C has weird not-really-builtin functions |
2028 | which are supposed to be visible automatically. They wind up | |
2029 | in the external scope because they're pushed before the file | |
2030 | scope gets created. Catch this here and rebind them into the | |
2031 | file scope. */ | |
3c20847b | 2032 | if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl)) |
f75fbaf7 | 2033 | { |
9aaabf8a ZW |
2034 | bind (functionid, decl, file_scope, |
2035 | /*invisible=*/false, /*nested=*/true); | |
f75fbaf7 ZW |
2036 | return decl; |
2037 | } | |
2038 | else | |
339a28b9 | 2039 | { |
f75fbaf7 ZW |
2040 | /* Implicit declaration of a function already declared |
2041 | (somehow) in a different scope, or as a built-in. | |
2042 | If this is the first time this has happened, warn; | |
2043 | then recycle the old declaration. */ | |
2044 | if (!C_DECL_IMPLICIT (decl)) | |
2045 | { | |
2046 | implicit_decl_warning (functionid, decl); | |
2047 | C_DECL_IMPLICIT (decl) = 1; | |
2048 | } | |
9aaabf8a ZW |
2049 | bind (functionid, decl, current_scope, |
2050 | /*invisible=*/false, /*nested=*/true); | |
f75fbaf7 | 2051 | return decl; |
339a28b9 | 2052 | } |
339a28b9 | 2053 | } |
51e29401 | 2054 | |
339a28b9 ZW |
2055 | /* Not seen before. */ |
2056 | decl = build_decl (FUNCTION_DECL, functionid, default_function_type); | |
1394aabd | 2057 | DECL_EXTERNAL (decl) = 1; |
51e29401 | 2058 | TREE_PUBLIC (decl) = 1; |
339a28b9 | 2059 | C_DECL_IMPLICIT (decl) = 1; |
f75fbaf7 | 2060 | implicit_decl_warning (functionid, 0); |
51e29401 | 2061 | |
14077d68 | 2062 | /* C89 says implicit declarations are in the innermost block. |
f458d1d5 | 2063 | So we record the decl in the standard fashion. */ |
339a28b9 | 2064 | decl = pushdecl (decl); |
51e29401 | 2065 | |
339a28b9 | 2066 | /* No need to call objc_check_decl here - it's a function type. */ |
6496a589 | 2067 | rest_of_decl_compilation (decl, NULL, 0, 0); |
51e29401 | 2068 | |
14077d68 ZW |
2069 | /* Write a record describing this implicit function declaration |
2070 | to the prototypes file (if requested). */ | |
51e29401 RS |
2071 | gen_aux_info_record (decl, 0, 1, 0); |
2072 | ||
6431177a JM |
2073 | /* Possibly apply some default attributes to this implicit declaration. */ |
2074 | decl_attributes (&decl, NULL_TREE, 0); | |
2075 | ||
51e29401 RS |
2076 | return decl; |
2077 | } | |
2078 | ||
9cd51ef6 ZW |
2079 | /* Issue an error message for a reference to an undeclared variable |
2080 | ID, including a reference to a builtin outside of function-call | |
2081 | context. Establish a binding of the identifier to error_mark_node | |
2082 | in an appropriate scope, which will suppress further errors for the | |
2083 | same identifier. */ | |
2084 | void | |
2085 | undeclared_variable (tree id) | |
2086 | { | |
2087 | static bool already = false; | |
f91f41b2 | 2088 | struct c_scope *scope; |
9cd51ef6 ZW |
2089 | |
2090 | if (current_function_decl == 0) | |
2091 | { | |
f75fbaf7 | 2092 | error ("'%E' undeclared here (not in a function)", id); |
f91f41b2 | 2093 | scope = current_scope; |
9cd51ef6 ZW |
2094 | } |
2095 | else | |
2096 | { | |
f75fbaf7 | 2097 | error ("'%E' undeclared (first use in this function)", id); |
9cd51ef6 ZW |
2098 | |
2099 | if (! already) | |
2100 | { | |
2101 | error ("(Each undeclared identifier is reported only once"); | |
2102 | error ("for each function it appears in.)"); | |
2103 | already = true; | |
2104 | } | |
2105 | ||
f75fbaf7 ZW |
2106 | /* If we are parsing old-style parameter decls, current_function_decl |
2107 | will be nonnull but current_function_scope will be null. */ | |
2108 | scope = current_function_scope ? current_function_scope : current_scope; | |
9cd51ef6 | 2109 | } |
9aaabf8a | 2110 | bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false); |
9cd51ef6 | 2111 | } |
51e29401 | 2112 | \f |
14e33ee8 ZW |
2113 | /* Subroutine of lookup_label, declare_label, define_label: construct a |
2114 | LABEL_DECL with all the proper frills. */ | |
2115 | ||
2116 | static tree | |
2117 | make_label (tree name, location_t location) | |
2118 | { | |
2119 | tree label = build_decl (LABEL_DECL, name, void_type_node); | |
2120 | ||
2121 | DECL_CONTEXT (label) = current_function_decl; | |
2122 | DECL_MODE (label) = VOIDmode; | |
f31686a3 | 2123 | DECL_SOURCE_LOCATION (label) = location; |
14e33ee8 ZW |
2124 | |
2125 | return label; | |
2126 | } | |
2127 | ||
14e33ee8 | 2128 | /* Get the LABEL_DECL corresponding to identifier NAME as a label. |
51e29401 | 2129 | Create one if none exists so far for the current function. |
14e33ee8 ZW |
2130 | This is called when a label is used in a goto expression or |
2131 | has its address taken. */ | |
51e29401 RS |
2132 | |
2133 | tree | |
14e33ee8 | 2134 | lookup_label (tree name) |
51e29401 | 2135 | { |
14e33ee8 | 2136 | tree label; |
51e29401 | 2137 | |
7d9795e5 RS |
2138 | if (current_function_decl == 0) |
2139 | { | |
2140 | error ("label %s referenced outside of any function", | |
14e33ee8 | 2141 | IDENTIFIER_POINTER (name)); |
7d9795e5 RS |
2142 | return 0; |
2143 | } | |
2144 | ||
14e33ee8 ZW |
2145 | /* Use a label already defined or ref'd with this name, but not if |
2146 | it is inherited from a containing function and wasn't declared | |
2147 | using __label__. */ | |
f75fbaf7 | 2148 | label = I_LABEL_DECL (name); |
14e33ee8 ZW |
2149 | if (label && (DECL_CONTEXT (label) == current_function_decl |
2150 | || C_DECLARED_LABEL_FLAG (label))) | |
51e29401 | 2151 | { |
14e33ee8 ZW |
2152 | /* If the label has only been declared, update its apparent |
2153 | location to point here, for better diagnostics if it | |
2154 | turns out not to have been defined. */ | |
2155 | if (!TREE_USED (label)) | |
f31686a3 | 2156 | DECL_SOURCE_LOCATION (label) = input_location; |
14e33ee8 | 2157 | return label; |
51e29401 RS |
2158 | } |
2159 | ||
14e33ee8 ZW |
2160 | /* No label binding for that identifier; make one. */ |
2161 | label = make_label (name, input_location); | |
51e29401 | 2162 | |
14077d68 | 2163 | /* Ordinary labels go in the current function scope. */ |
9aaabf8a ZW |
2164 | bind (name, label, current_function_scope, |
2165 | /*invisible=*/false, /*nested=*/false); | |
14e33ee8 | 2166 | return label; |
51e29401 RS |
2167 | } |
2168 | ||
14e33ee8 ZW |
2169 | /* Make a label named NAME in the current function, shadowing silently |
2170 | any that may be inherited from containing functions or containing | |
2171 | scopes. This is called for __label__ declarations. */ | |
51e29401 | 2172 | |
51e29401 | 2173 | tree |
14e33ee8 | 2174 | declare_label (tree name) |
51e29401 | 2175 | { |
f75fbaf7 ZW |
2176 | struct c_binding *b = I_LABEL_BINDING (name); |
2177 | tree label; | |
51e29401 | 2178 | |
14e33ee8 ZW |
2179 | /* Check to make sure that the label hasn't already been declared |
2180 | at this scope */ | |
9aaabf8a | 2181 | if (b && B_IN_CURRENT_SCOPE (b)) |
f75fbaf7 ZW |
2182 | { |
2183 | error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name)); | |
2184 | locate_old_decl (b->decl, error); | |
a784882b | 2185 | |
f75fbaf7 ZW |
2186 | /* Just use the previous declaration. */ |
2187 | return b->decl; | |
2188 | } | |
a784882b | 2189 | |
14e33ee8 ZW |
2190 | label = make_label (name, input_location); |
2191 | C_DECLARED_LABEL_FLAG (label) = 1; | |
51e29401 | 2192 | |
14e33ee8 | 2193 | /* Declared labels go in the current scope. */ |
9aaabf8a ZW |
2194 | bind (name, label, current_scope, |
2195 | /*invisible=*/false, /*nested=*/false); | |
14e33ee8 | 2196 | return label; |
51e29401 RS |
2197 | } |
2198 | ||
2199 | /* Define a label, specifying the location in the source file. | |
2200 | Return the LABEL_DECL node for the label, if the definition is valid. | |
2201 | Otherwise return 0. */ | |
2202 | ||
2203 | tree | |
5b030314 | 2204 | define_label (location_t location, tree name) |
51e29401 | 2205 | { |
14e33ee8 ZW |
2206 | /* Find any preexisting label with this name. It is an error |
2207 | if that label has already been defined in this function, or | |
2208 | if there is a containing function with a declared label with | |
2209 | the same name. */ | |
f75fbaf7 | 2210 | tree label = I_LABEL_DECL (name); |
14e33ee8 ZW |
2211 | |
2212 | if (label | |
2213 | && ((DECL_CONTEXT (label) == current_function_decl | |
2214 | && DECL_INITIAL (label) != 0) | |
2215 | || (DECL_CONTEXT (label) != current_function_decl | |
2216 | && C_DECLARED_LABEL_FLAG (label)))) | |
51e29401 | 2217 | { |
14e33ee8 | 2218 | error ("%Hduplicate label `%D'", &location, label); |
f75fbaf7 | 2219 | locate_old_decl (label, error); |
14e33ee8 | 2220 | return 0; |
51e29401 | 2221 | } |
14e33ee8 | 2222 | else if (label && DECL_CONTEXT (label) == current_function_decl) |
51e29401 | 2223 | { |
14e33ee8 ZW |
2224 | /* The label has been used or declared already in this function, |
2225 | but not defined. Update its location to point to this | |
2226 | definition. */ | |
f31686a3 | 2227 | DECL_SOURCE_LOCATION (label) = location; |
51e29401 RS |
2228 | } |
2229 | else | |
2230 | { | |
14e33ee8 ZW |
2231 | /* No label binding for that identifier; make one. */ |
2232 | label = make_label (name, location); | |
2233 | ||
14077d68 | 2234 | /* Ordinary labels go in the current function scope. */ |
9aaabf8a ZW |
2235 | bind (name, label, current_function_scope, |
2236 | /*invisible=*/false, /*nested=*/false); | |
51e29401 | 2237 | } |
14e33ee8 ZW |
2238 | |
2239 | if (warn_traditional && !in_system_header && lookup_name (name)) | |
2240 | warning ("%Htraditional C lacks a separate namespace for labels, " | |
2241 | "identifier `%s' conflicts", &location, | |
2242 | IDENTIFIER_POINTER (name)); | |
2243 | ||
2244 | /* Mark label as having been defined. */ | |
2245 | DECL_INITIAL (label) = error_mark_node; | |
2246 | return label; | |
51e29401 | 2247 | } |
51e29401 RS |
2248 | \f |
2249 | /* Given NAME, an IDENTIFIER_NODE, | |
2250 | return the structure (or union or enum) definition for that name. | |
f8521984 | 2251 | If THISLEVEL_ONLY is nonzero, searches only the current_scope. |
51e29401 RS |
2252 | CODE says which kind of type the caller wants; |
2253 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
2254 | If the wrong kind of type is found, an error is reported. */ | |
2255 | ||
2256 | static tree | |
35b1a6fa | 2257 | lookup_tag (enum tree_code code, tree name, int thislevel_only) |
51e29401 | 2258 | { |
f75fbaf7 | 2259 | struct c_binding *b = I_TAG_BINDING (name); |
339a28b9 | 2260 | int thislevel = 0; |
51e29401 | 2261 | |
f75fbaf7 | 2262 | if (!b || !b->decl) |
339a28b9 ZW |
2263 | return 0; |
2264 | ||
2265 | /* We only care about whether it's in this level if | |
2266 | thislevel_only was set or it might be a type clash. */ | |
f75fbaf7 ZW |
2267 | if (thislevel_only || TREE_CODE (b->decl) != code) |
2268 | { | |
2269 | /* For our purposes, a tag in the external scope is the same as | |
2270 | a tag in the file scope. (Primarily relevant to Objective-C | |
2271 | and its builtin structure tags, which get pushed before the | |
118a3a8b | 2272 | file scope is created.) */ |
9aaabf8a ZW |
2273 | if (B_IN_CURRENT_SCOPE (b) |
2274 | || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b))) | |
339a28b9 | 2275 | thislevel = 1; |
51e29401 | 2276 | } |
339a28b9 ZW |
2277 | |
2278 | if (thislevel_only && !thislevel) | |
2279 | return 0; | |
2280 | ||
f75fbaf7 | 2281 | if (TREE_CODE (b->decl) != code) |
339a28b9 ZW |
2282 | { |
2283 | /* Definition isn't the kind we were looking for. */ | |
2284 | pending_invalid_xref = name; | |
070588f0 | 2285 | pending_invalid_xref_location = input_location; |
339a28b9 ZW |
2286 | |
2287 | /* If in the same binding level as a declaration as a tag | |
2288 | of a different type, this must not be allowed to | |
2289 | shadow that tag, so give the error immediately. | |
2290 | (For example, "struct foo; union foo;" is invalid.) */ | |
2291 | if (thislevel) | |
2292 | pending_xref_error (); | |
2293 | } | |
f75fbaf7 | 2294 | return b->decl; |
51e29401 RS |
2295 | } |
2296 | ||
2297 | /* Print an error message now | |
2298 | for a recent invalid struct, union or enum cross reference. | |
2299 | We don't print them immediately because they are not invalid | |
2300 | when used in the `struct foo;' construct for shadowing. */ | |
2301 | ||
2302 | void | |
35b1a6fa | 2303 | pending_xref_error (void) |
51e29401 RS |
2304 | { |
2305 | if (pending_invalid_xref != 0) | |
95035b6f GDR |
2306 | error ("%H`%s' defined as wrong kind of tag", |
2307 | &pending_invalid_xref_location, | |
2308 | IDENTIFIER_POINTER (pending_invalid_xref)); | |
51e29401 RS |
2309 | pending_invalid_xref = 0; |
2310 | } | |
2311 | ||
51e29401 | 2312 | \f |
f8521984 | 2313 | /* Look up NAME in the current scope and its superiors |
51e29401 RS |
2314 | in the namespace of variables, functions and typedefs. |
2315 | Return a ..._DECL node of some kind representing its definition, | |
2316 | or return 0 if it is undefined. */ | |
2317 | ||
2318 | tree | |
35b1a6fa | 2319 | lookup_name (tree name) |
51e29401 | 2320 | { |
f75fbaf7 | 2321 | struct c_binding *b = I_SYMBOL_BINDING (name); |
9aaabf8a | 2322 | if (b && !b->invisible) |
f75fbaf7 ZW |
2323 | return b->decl; |
2324 | return 0; | |
51e29401 RS |
2325 | } |
2326 | ||
f75fbaf7 | 2327 | /* Similar to `lookup_name' but look only at the indicated scope. */ |
51e29401 | 2328 | |
339a28b9 | 2329 | static tree |
f75fbaf7 | 2330 | lookup_name_in_scope (tree name, struct c_scope *scope) |
51e29401 | 2331 | { |
f75fbaf7 | 2332 | struct c_binding *b; |
339a28b9 | 2333 | |
f75fbaf7 | 2334 | for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed) |
9aaabf8a | 2335 | if (B_IN_SCOPE (b, scope)) |
f75fbaf7 | 2336 | return b->decl; |
339a28b9 | 2337 | return 0; |
51e29401 RS |
2338 | } |
2339 | \f | |
2340 | /* Create the predefined scalar types of C, | |
0f41302f | 2341 | and some nodes representing standard constants (0, 1, (void *) 0). |
f8521984 | 2342 | Initialize the global scope. |
51e29401 RS |
2343 | Make definitions for built-in primitive functions. */ |
2344 | ||
2345 | void | |
35b1a6fa | 2346 | c_init_decl_processing (void) |
51e29401 | 2347 | { |
b3694847 | 2348 | tree endlink; |
7f4edbcb | 2349 | tree ptr_ftype_void, ptr_ftype_ptr; |
4714db5a | 2350 | location_t save_loc = input_location; |
e13e48e7 | 2351 | |
f5e99456 NB |
2352 | /* Adds some ggc roots, and reserved words for c-parse.in. */ |
2353 | c_parse_init (); | |
2354 | ||
14077d68 | 2355 | current_function_decl = 0; |
6645c3fa | 2356 | |
f75fbaf7 ZW |
2357 | /* Make the externals scope. */ |
2358 | push_scope (); | |
2359 | external_scope = current_scope; | |
14077d68 | 2360 | |
e3091a5f R |
2361 | /* Declarations from c_common_nodes_and_builtins must not be associated |
2362 | with this input file, lest we get differences between using and not | |
2363 | using preprocessed headers. */ | |
3c20847b PB |
2364 | #ifdef USE_MAPPED_LOCATION |
2365 | input_location = BUILTINS_LOCATION; | |
2366 | #else | |
2367 | input_location.file = "<built-in>"; | |
4714db5a | 2368 | input_location.line = 0; |
3c20847b | 2369 | #endif |
51e29401 | 2370 | |
81b3411c | 2371 | build_common_tree_nodes (flag_signed_char); |
51e29401 | 2372 | |
eaa7c03f | 2373 | c_common_nodes_and_builtins (); |
51e29401 | 2374 | |
de7df9eb JM |
2375 | /* In C, comparisons and TRUTH_* expressions have type int. */ |
2376 | truthvalue_type_node = integer_type_node; | |
2377 | truthvalue_true_node = integer_one_node; | |
2378 | truthvalue_false_node = integer_zero_node; | |
2379 | ||
2380 | /* Even in C99, which has a real boolean type. */ | |
19552aa5 | 2381 | pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"), |
de7df9eb | 2382 | boolean_type_node)); |
19552aa5 | 2383 | |
7f4edbcb | 2384 | endlink = void_list_node; |
0021b564 JM |
2385 | ptr_ftype_void = build_function_type (ptr_type_node, endlink); |
2386 | ptr_ftype_ptr | |
2387 | = build_function_type (ptr_type_node, | |
2388 | tree_cons (NULL_TREE, ptr_type_node, endlink)); | |
2389 | ||
4714db5a | 2390 | input_location = save_loc; |
e3091a5f | 2391 | |
53cd18ec | 2392 | pedantic_lvalues = true; |
f444e553 | 2393 | |
2ce07e2d | 2394 | make_fname_decl = c_make_fname_decl; |
0ba8a114 | 2395 | start_fname_decls (); |
51e29401 RS |
2396 | } |
2397 | ||
2ce07e2d NS |
2398 | /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the |
2399 | decl, NAME is the initialization string and TYPE_DEP indicates whether | |
2400 | NAME depended on the type of the function. As we don't yet implement | |
2401 | delayed emission of static data, we mark the decl as emitted | |
2402 | so it is not placed in the output. Anything using it must therefore pull | |
f91f41b2 | 2403 | out the STRING_CST initializer directly. FIXME. */ |
2ce07e2d NS |
2404 | |
2405 | static tree | |
35b1a6fa | 2406 | c_make_fname_decl (tree id, int type_dep) |
2ce07e2d | 2407 | { |
0ba8a114 | 2408 | const char *name = fname_as_string (type_dep); |
2ce07e2d NS |
2409 | tree decl, type, init; |
2410 | size_t length = strlen (name); | |
2411 | ||
2412 | type = build_array_type | |
2413 | (build_qualified_type (char_type_node, TYPE_QUAL_CONST), | |
0ba8a114 | 2414 | build_index_type (size_int (length))); |
2ce07e2d NS |
2415 | |
2416 | decl = build_decl (VAR_DECL, id, type); | |
35b1a6fa | 2417 | |
2ce07e2d NS |
2418 | TREE_STATIC (decl) = 1; |
2419 | TREE_READONLY (decl) = 1; | |
2ce07e2d | 2420 | DECL_ARTIFICIAL (decl) = 1; |
35b1a6fa | 2421 | |
2ce07e2d | 2422 | init = build_string (length + 1, name); |
0d0bc036 | 2423 | free ((char *) name); |
2ce07e2d NS |
2424 | TREE_TYPE (init) = type; |
2425 | DECL_INITIAL (decl) = init; | |
0ba8a114 NS |
2426 | |
2427 | TREE_USED (decl) = 1; | |
6cce57b0 JM |
2428 | |
2429 | if (current_function_decl) | |
f91f41b2 ZW |
2430 | { |
2431 | DECL_CONTEXT (decl) = current_function_decl; | |
9aaabf8a ZW |
2432 | bind (id, decl, current_function_scope, |
2433 | /*invisible=*/false, /*nested=*/false); | |
f91f41b2 | 2434 | } |
339a28b9 | 2435 | |
0ba8a114 | 2436 | finish_decl (decl, init, NULL_TREE); |
6645c3fa | 2437 | |
2ce07e2d NS |
2438 | return decl; |
2439 | } | |
2440 | ||
51e29401 RS |
2441 | /* Return a definition for a builtin function named NAME and whose data type |
2442 | is TYPE. TYPE should be a function type with argument types. | |
2443 | FUNCTION_CODE tells later passes how to compile calls to this function. | |
2444 | See tree.h for its possible values. | |
2445 | ||
2446 | If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME, | |
6a2dd09a RS |
2447 | the name to be called if we can't opencode the function. If |
2448 | ATTRS is nonzero, use that for the function's attribute list. */ | |
51e29401 | 2449 | |
929f3671 | 2450 | tree |
35b1a6fa AJ |
2451 | builtin_function (const char *name, tree type, int function_code, |
2452 | enum built_in_class class, const char *library_name, | |
2453 | tree attrs) | |
51e29401 | 2454 | { |
f75fbaf7 ZW |
2455 | tree id = get_identifier (name); |
2456 | tree decl = build_decl (FUNCTION_DECL, id, type); | |
51e29401 | 2457 | TREE_PUBLIC (decl) = 1; |
f75fbaf7 ZW |
2458 | DECL_EXTERNAL (decl) = 1; |
2459 | DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl)); | |
2460 | DECL_BUILT_IN_CLASS (decl) = class; | |
2461 | DECL_FUNCTION_CODE (decl) = function_code; | |
51e29401 | 2462 | if (library_name) |
92643fea | 2463 | SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name)); |
26db82d8 | 2464 | |
f75fbaf7 ZW |
2465 | /* Should never be called on a symbol with a preexisting meaning. */ |
2466 | if (I_SYMBOL_BINDING (id)) | |
2467 | abort (); | |
2468 | ||
9aaabf8a | 2469 | bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false); |
f75fbaf7 ZW |
2470 | |
2471 | /* Builtins in the implementation namespace are made visible without | |
2472 | needing to be explicitly declared. See push_file_scope. */ | |
2473 | if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1]))) | |
2474 | { | |
2475 | TREE_CHAIN (decl) = visible_builtins; | |
2476 | visible_builtins = decl; | |
2477 | } | |
51e29401 | 2478 | |
6431177a | 2479 | /* Possibly apply some default attributes to this built-in function. */ |
6a2dd09a RS |
2480 | if (attrs) |
2481 | decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN); | |
2482 | else | |
2483 | decl_attributes (&decl, NULL_TREE, 0); | |
6431177a | 2484 | |
51e29401 RS |
2485 | return decl; |
2486 | } | |
2487 | \f | |
2488 | /* Called when a declaration is seen that contains no names to declare. | |
2489 | If its type is a reference to a structure, union or enum inherited | |
2490 | from a containing scope, shadow that tag name for the current scope | |
2491 | with a forward reference. | |
2492 | If its type defines a new named structure or union | |
2493 | or defines an enum, it is valid but we need not do anything here. | |
2494 | Otherwise, it is an error. */ | |
2495 | ||
2496 | void | |
35b1a6fa | 2497 | shadow_tag (tree declspecs) |
9282f2f9 RS |
2498 | { |
2499 | shadow_tag_warned (declspecs, 0); | |
2500 | } | |
2501 | ||
2502 | void | |
35b1a6fa AJ |
2503 | shadow_tag_warned (tree declspecs, int warned) |
2504 | ||
2505 | ||
773edaef RK |
2506 | /* 1 => we have done a pedwarn. 2 => we have done a warning, but |
2507 | no pedwarn. */ | |
51e29401 RS |
2508 | { |
2509 | int found_tag = 0; | |
b3694847 | 2510 | tree link; |
d9525bec | 2511 | tree specs, attrs; |
51e29401 RS |
2512 | |
2513 | pending_invalid_xref = 0; | |
2514 | ||
d9525bec BK |
2515 | /* Remove the attributes from declspecs, since they will confuse the |
2516 | following code. */ | |
2517 | split_specs_attrs (declspecs, &specs, &attrs); | |
2518 | ||
3bd89472 | 2519 | for (link = specs; link; link = TREE_CHAIN (link)) |
51e29401 | 2520 | { |
b3694847 SS |
2521 | tree value = TREE_VALUE (link); |
2522 | enum tree_code code = TREE_CODE (value); | |
51e29401 RS |
2523 | |
2524 | if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) | |
2525 | /* Used to test also that TYPE_SIZE (value) != 0. | |
2526 | That caused warning for `struct foo;' at top level in the file. */ | |
2527 | { | |
339a28b9 | 2528 | tree name = TYPE_NAME (value); |
b3694847 | 2529 | tree t; |
51e29401 RS |
2530 | |
2531 | found_tag++; | |
2532 | ||
2533 | if (name == 0) | |
2534 | { | |
773edaef RK |
2535 | if (warned != 1 && code != ENUMERAL_TYPE) |
2536 | /* Empty unnamed enum OK */ | |
51e29401 RS |
2537 | { |
2538 | pedwarn ("unnamed struct/union that defines no instances"); | |
2539 | warned = 1; | |
2540 | } | |
2541 | } | |
2542 | else | |
2543 | { | |
339a28b9 | 2544 | t = lookup_tag (code, name, 1); |
51e29401 RS |
2545 | |
2546 | if (t == 0) | |
2547 | { | |
2548 | t = make_node (code); | |
2549 | pushtag (name, t); | |
2550 | } | |
2551 | } | |
2552 | } | |
2553 | else | |
2554 | { | |
efffbf71 | 2555 | if (!warned && ! in_system_header) |
773edaef RK |
2556 | { |
2557 | warning ("useless keyword or type name in empty declaration"); | |
2558 | warned = 2; | |
2559 | } | |
51e29401 RS |
2560 | } |
2561 | } | |
2562 | ||
773edaef RK |
2563 | if (found_tag > 1) |
2564 | error ("two types specified in one empty declaration"); | |
2565 | ||
2566 | if (warned != 1) | |
51e29401 | 2567 | { |
51e29401 RS |
2568 | if (found_tag == 0) |
2569 | pedwarn ("empty declaration"); | |
2570 | } | |
2571 | } | |
2572 | \f | |
0e03329a JM |
2573 | /* Construct an array declarator. EXPR is the expression inside [], or |
2574 | NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied | |
2575 | to the pointer to which a parameter array is converted). STATIC_P is | |
da7d8304 KH |
2576 | nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P |
2577 | is nonzero is the array is [*], a VLA of unspecified length which is | |
0e03329a JM |
2578 | nevertheless a complete type (not currently implemented by GCC), |
2579 | zero otherwise. The declarator is constructed as an ARRAY_REF | |
2580 | (to be decoded by grokdeclarator), whose operand 0 is what's on the | |
2581 | left of the [] (filled by in set_array_declarator_type) and operand 1 | |
2582 | is the expression inside; whose TREE_TYPE is the type qualifiers and | |
2583 | which has TREE_STATIC set if "static" is used. */ | |
2584 | ||
2585 | tree | |
35b1a6fa | 2586 | build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p) |
0e03329a JM |
2587 | { |
2588 | tree decl; | |
44de5aeb | 2589 | decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE); |
0e03329a JM |
2590 | TREE_TYPE (decl) = quals; |
2591 | TREE_STATIC (decl) = (static_p ? 1 : 0); | |
2592 | if (pedantic && !flag_isoc99) | |
2593 | { | |
2594 | if (static_p || quals != NULL_TREE) | |
56508306 | 2595 | pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators"); |
0e03329a | 2596 | if (vla_unspec_p) |
56508306 | 2597 | pedwarn ("ISO C90 does not support `[*]' array declarators"); |
0e03329a JM |
2598 | } |
2599 | if (vla_unspec_p) | |
2600 | warning ("GCC does not yet properly implement `[*]' array declarators"); | |
2601 | return decl; | |
2602 | } | |
2603 | ||
2604 | /* Set the type of an array declarator. DECL is the declarator, as | |
2605 | constructed by build_array_declarator; TYPE is what appears on the left | |
da7d8304 | 2606 | of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an |
0e03329a JM |
2607 | abstract declarator, zero otherwise; this is used to reject static and |
2608 | type qualifiers in abstract declarators, where they are not in the | |
2609 | C99 grammar. */ | |
2610 | ||
2611 | tree | |
35b1a6fa | 2612 | set_array_declarator_type (tree decl, tree type, int abstract_p) |
0e03329a JM |
2613 | { |
2614 | TREE_OPERAND (decl, 0) = type; | |
2615 | if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl))) | |
2616 | error ("static or type qualifiers in abstract declarator"); | |
2617 | return decl; | |
2618 | } | |
2619 | \f | |
51e29401 RS |
2620 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ |
2621 | ||
2622 | tree | |
35b1a6fa | 2623 | groktypename (tree typename) |
51e29401 | 2624 | { |
d3b4cd6f AH |
2625 | tree specs, attrs; |
2626 | ||
51e29401 RS |
2627 | if (TREE_CODE (typename) != TREE_LIST) |
2628 | return typename; | |
d3b4cd6f AH |
2629 | |
2630 | split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs); | |
2631 | ||
2ff7cce4 JM |
2632 | typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0, |
2633 | NULL); | |
d3b4cd6f AH |
2634 | |
2635 | /* Apply attributes. */ | |
2636 | decl_attributes (&typename, attrs, 0); | |
2637 | ||
2638 | return typename; | |
51e29401 RS |
2639 | } |
2640 | ||
2641 | /* Return a PARM_DECL node for a given pair of specs and declarator. */ | |
2642 | ||
2643 | tree | |
35b1a6fa | 2644 | groktypename_in_parm_context (tree typename) |
51e29401 RS |
2645 | { |
2646 | if (TREE_CODE (typename) != TREE_LIST) | |
2647 | return typename; | |
2648 | return grokdeclarator (TREE_VALUE (typename), | |
2649 | TREE_PURPOSE (typename), | |
2ff7cce4 | 2650 | PARM, 0, NULL); |
51e29401 RS |
2651 | } |
2652 | ||
2653 | /* Decode a declarator in an ordinary declaration or data definition. | |
2654 | This is called as soon as the type information and variable name | |
2655 | have been parsed, before parsing the initializer if any. | |
2656 | Here we create the ..._DECL node, fill in its type, | |
2657 | and put it on the list of decls for the current context. | |
2658 | The ..._DECL node is returned as the value. | |
2659 | ||
2660 | Exception: for arrays where the length is not specified, | |
2661 | the type is left null, to be filled in by `finish_decl'. | |
2662 | ||
2663 | Function definitions do not come here; they go to start_function | |
2664 | instead. However, external and forward declarations of functions | |
2665 | do go through here. Structure field declarations are done by | |
2666 | grokfield and not through here. */ | |
2667 | ||
51e29401 | 2668 | tree |
35b1a6fa | 2669 | start_decl (tree declarator, tree declspecs, int initialized, tree attributes) |
51e29401 | 2670 | { |
e23bd218 | 2671 | tree decl; |
b3694847 | 2672 | tree tem; |
35b1a6fa | 2673 | |
e23bd218 IR |
2674 | /* An object declared as __attribute__((deprecated)) suppresses |
2675 | warnings of uses of other deprecated items. */ | |
2676 | if (lookup_attribute ("deprecated", attributes)) | |
2677 | deprecated_state = DEPRECATED_SUPPRESS; | |
2678 | ||
2679 | decl = grokdeclarator (declarator, declspecs, | |
2ff7cce4 | 2680 | NORMAL, initialized, NULL); |
35b1a6fa | 2681 | |
e23bd218 | 2682 | deprecated_state = DEPRECATED_NORMAL; |
51e29401 | 2683 | |
6645c3fa | 2684 | if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL |
5b47282c | 2685 | && MAIN_NAME_P (DECL_NAME (decl))) |
ddd2d57e | 2686 | warning ("%J'%D' is usually a function", decl, decl); |
b8705e61 | 2687 | |
51e29401 RS |
2688 | if (initialized) |
2689 | /* Is it valid for this decl to have an initializer at all? | |
2690 | If not, set INITIALIZED to zero, which will indirectly | |
f75fbaf7 | 2691 | tell 'finish_decl' to ignore the initializer once it is parsed. */ |
51e29401 RS |
2692 | switch (TREE_CODE (decl)) |
2693 | { | |
2694 | case TYPE_DECL: | |
f75fbaf7 | 2695 | error ("typedef '%D' is initialized (use __typeof__ instead)", decl); |
95f79357 | 2696 | initialized = 0; |
51e29401 RS |
2697 | break; |
2698 | ||
2699 | case FUNCTION_DECL: | |
f75fbaf7 | 2700 | error ("function '%D' is initialized like a variable", decl); |
51e29401 RS |
2701 | initialized = 0; |
2702 | break; | |
2703 | ||
2704 | case PARM_DECL: | |
2705 | /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */ | |
f75fbaf7 | 2706 | error ("parameter '%D' is initialized", decl); |
51e29401 RS |
2707 | initialized = 0; |
2708 | break; | |
2709 | ||
2710 | default: | |
f75fbaf7 ZW |
2711 | /* Don't allow initializations for incomplete types except for |
2712 | arrays which might be completed by the initialization. */ | |
f4fce7ed | 2713 | |
f75fbaf7 ZW |
2714 | /* This can happen if the array size is an undefined macro. |
2715 | We already gave a warning, so we don't need another one. */ | |
f4fce7ed JW |
2716 | if (TREE_TYPE (decl) == error_mark_node) |
2717 | initialized = 0; | |
2718 | else if (COMPLETE_TYPE_P (TREE_TYPE (decl))) | |
51e29401 RS |
2719 | { |
2720 | /* A complete type is ok if size is fixed. */ | |
2721 | ||
2722 | if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST | |
2723 | || C_DECL_VARIABLE_SIZE (decl)) | |
2724 | { | |
2725 | error ("variable-sized object may not be initialized"); | |
2726 | initialized = 0; | |
2727 | } | |
2728 | } | |
2729 | else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) | |
2730 | { | |
f75fbaf7 | 2731 | error ("variable '%D' has initializer but incomplete type", decl); |
51e29401 RS |
2732 | initialized = 0; |
2733 | } | |
d0f062fb | 2734 | else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) |
51e29401 | 2735 | { |
f75fbaf7 | 2736 | error ("elements of array '%D' have incomplete type", decl); |
51e29401 RS |
2737 | initialized = 0; |
2738 | } | |
2739 | } | |
2740 | ||
2741 | if (initialized) | |
2742 | { | |
1394aabd | 2743 | DECL_EXTERNAL (decl) = 0; |
f75fbaf7 | 2744 | if (current_scope == file_scope) |
51e29401 RS |
2745 | TREE_STATIC (decl) = 1; |
2746 | ||
f75fbaf7 | 2747 | /* Tell 'pushdecl' this is an initialized decl |
51e29401 | 2748 | even though we don't yet have the initializer expression. |
f75fbaf7 | 2749 | Also tell 'finish_decl' it may store the real initializer. */ |
51e29401 RS |
2750 | DECL_INITIAL (decl) = error_mark_node; |
2751 | } | |
2752 | ||
2753 | /* If this is a function declaration, write a record describing it to the | |
2754 | prototypes file (if requested). */ | |
2755 | ||
2756 | if (TREE_CODE (decl) == FUNCTION_DECL) | |
2757 | gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0); | |
2758 | ||
2786cbad JM |
2759 | /* ANSI specifies that a tentative definition which is not merged with |
2760 | a non-tentative definition behaves exactly like a definition with an | |
2761 | initializer equal to zero. (Section 3.7.2) | |
3d78f2e9 RH |
2762 | |
2763 | -fno-common gives strict ANSI behavior, though this tends to break | |
2764 | a large body of code that grew up without this rule. | |
2765 | ||
2766 | Thread-local variables are never common, since there's no entrenched | |
2767 | body of code to break, and it allows more efficient variable references | |
95bd1dd7 | 2768 | in the presence of dynamic linking. */ |
3d78f2e9 RH |
2769 | |
2770 | if (TREE_CODE (decl) == VAR_DECL | |
2771 | && !initialized | |
2772 | && TREE_PUBLIC (decl) | |
2773 | && !DECL_THREAD_LOCAL (decl) | |
2774 | && !flag_no_common) | |
2786cbad | 2775 | DECL_COMMON (decl) = 1; |
8615176a | 2776 | |
daa6d5ff | 2777 | /* Set attributes here so if duplicate decl, will have proper attributes. */ |
59387d2e | 2778 | decl_attributes (&decl, attributes, 0); |
daa6d5ff | 2779 | |
61f71b34 DD |
2780 | if (TREE_CODE (decl) == FUNCTION_DECL |
2781 | && targetm.calls.promote_prototypes (TREE_TYPE (decl))) | |
2782 | { | |
2783 | tree ce = declarator; | |
2784 | ||
2785 | if (TREE_CODE (ce) == INDIRECT_REF) | |
2786 | ce = TREE_OPERAND (declarator, 0); | |
2787 | if (TREE_CODE (ce) == CALL_EXPR) | |
2788 | { | |
2789 | tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1)); | |
2790 | for (; args; args = TREE_CHAIN (args)) | |
2791 | { | |
2792 | tree type = TREE_TYPE (args); | |
1f4f60fc | 2793 | if (type && INTEGRAL_TYPE_P (type) |
61f71b34 DD |
2794 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
2795 | DECL_ARG_TYPE (args) = integer_type_node; | |
2796 | } | |
2797 | } | |
2798 | } | |
2799 | ||
9162542e AO |
2800 | if (TREE_CODE (decl) == FUNCTION_DECL |
2801 | && DECL_DECLARED_INLINE_P (decl) | |
2802 | && DECL_UNINLINABLE (decl) | |
2803 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) | |
ddd2d57e | 2804 | warning ("%Jinline function '%D' given attribute noinline", decl, decl); |
9162542e | 2805 | |
f8521984 | 2806 | /* Add this decl to the current scope. |
51e29401 RS |
2807 | TEM may equal DECL or it may be a previous decl of the same name. */ |
2808 | tem = pushdecl (decl); | |
2809 | ||
51e29401 RS |
2810 | return tem; |
2811 | } | |
2812 | ||
2813 | /* Finish processing of a declaration; | |
2814 | install its initial value. | |
2815 | If the length of an array type is not known before, | |
2816 | it must be determined now, from the initial value, or it is an error. */ | |
2817 | ||
2818 | void | |
35b1a6fa | 2819 | finish_decl (tree decl, tree init, tree asmspec_tree) |
51e29401 | 2820 | { |
b3694847 | 2821 | tree type = TREE_TYPE (decl); |
51e29401 | 2822 | int was_incomplete = (DECL_SIZE (decl) == 0); |
520a57c8 | 2823 | const char *asmspec = 0; |
51e29401 | 2824 | |
6d2f8887 | 2825 | /* If a name was specified, get the string. */ |
f75fbaf7 | 2826 | if (current_scope == file_scope) |
41c64394 | 2827 | asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); |
51e29401 | 2828 | if (asmspec_tree) |
72f5a12b | 2829 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
51e29401 RS |
2830 | |
2831 | /* If `start_decl' didn't like having an initialization, ignore it now. */ | |
51e29401 RS |
2832 | if (init != 0 && DECL_INITIAL (decl) == 0) |
2833 | init = 0; | |
35b1a6fa | 2834 | |
51e29401 RS |
2835 | /* Don't crash if parm is initialized. */ |
2836 | if (TREE_CODE (decl) == PARM_DECL) | |
2837 | init = 0; | |
2838 | ||
2839 | if (init) | |
95f79357 | 2840 | store_init_value (decl, init); |
51e29401 | 2841 | |
264fa2db ZL |
2842 | if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL |
2843 | || TREE_CODE (decl) == FUNCTION_DECL | |
2844 | || TREE_CODE (decl) == FIELD_DECL)) | |
2845 | objc_check_decl (decl); | |
2846 | ||
6614fd40 | 2847 | /* Deduce size of array from initialization, if not already known. */ |
51e29401 RS |
2848 | if (TREE_CODE (type) == ARRAY_TYPE |
2849 | && TYPE_DOMAIN (type) == 0 | |
2850 | && TREE_CODE (decl) != TYPE_DECL) | |
2851 | { | |
2852 | int do_default | |
2853 | = (TREE_STATIC (decl) | |
2854 | /* Even if pedantic, an external linkage array | |
2855 | may have incomplete type at first. */ | |
2856 | ? pedantic && !TREE_PUBLIC (decl) | |
1394aabd | 2857 | : !DECL_EXTERNAL (decl)); |
51e29401 RS |
2858 | int failure |
2859 | = complete_array_type (type, DECL_INITIAL (decl), do_default); | |
2860 | ||
2861 | /* Get the completed type made by complete_array_type. */ | |
2862 | type = TREE_TYPE (decl); | |
2863 | ||
2864 | if (failure == 1) | |
ddd2d57e | 2865 | error ("%Jinitializer fails to determine size of '%D'", decl, decl); |
51e29401 | 2866 | |
5fa7c8ce | 2867 | else if (failure == 2) |
51e29401 RS |
2868 | { |
2869 | if (do_default) | |
ddd2d57e | 2870 | error ("%Jarray size missing in '%D'", decl, decl); |
b4892310 RS |
2871 | /* If a `static' var's size isn't known, |
2872 | make it extern as well as static, so it does not get | |
2873 | allocated. | |
2874 | If it is not `static', then do not mark extern; | |
2875 | finish_incomplete_decl will give it a default size | |
2876 | and it will get allocated. */ | |
2877 | else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl)) | |
1394aabd | 2878 | DECL_EXTERNAL (decl) = 1; |
51e29401 RS |
2879 | } |
2880 | ||
7e44eda6 JW |
2881 | /* TYPE_MAX_VALUE is always one less than the number of elements |
2882 | in the array, because we start counting at zero. Therefore, | |
2883 | warn only if the value is less than zero. */ | |
5fa7c8ce RK |
2884 | else if (pedantic && TYPE_DOMAIN (type) != 0 |
2885 | && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0) | |
ddd2d57e | 2886 | error ("%Jzero or negative size array '%D'", decl, decl); |
51e29401 RS |
2887 | |
2888 | layout_decl (decl, 0); | |
2889 | } | |
2890 | ||
2891 | if (TREE_CODE (decl) == VAR_DECL) | |
2892 | { | |
f4fce7ed JW |
2893 | if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node |
2894 | && COMPLETE_TYPE_P (TREE_TYPE (decl))) | |
d575f110 RS |
2895 | layout_decl (decl, 0); |
2896 | ||
a7f64d52 | 2897 | if (DECL_SIZE (decl) == 0 |
f4fce7ed JW |
2898 | /* Don't give an error if we already gave one earlier. */ |
2899 | && TREE_TYPE (decl) != error_mark_node | |
a7f64d52 RS |
2900 | && (TREE_STATIC (decl) |
2901 | ? | |
2902 | /* A static variable with an incomplete type | |
f3b4fb6e | 2903 | is an error if it is initialized. |
70efc776 | 2904 | Also if it is not file scope. |
a7f64d52 RS |
2905 | Otherwise, let it through, but if it is not `extern' |
2906 | then it may cause an error message later. */ | |
e3b776dc | 2907 | (DECL_INITIAL (decl) != 0 |
4b1e44be | 2908 | || !DECL_FILE_SCOPE_P (decl)) |
a7f64d52 RS |
2909 | : |
2910 | /* An automatic variable with an incomplete type | |
2911 | is an error. */ | |
70038ec9 | 2912 | !DECL_EXTERNAL (decl))) |
51e29401 | 2913 | { |
ddd2d57e | 2914 | error ("%Jstorage size of '%D' isn't known", decl, decl); |
51e29401 RS |
2915 | TREE_TYPE (decl) = error_mark_node; |
2916 | } | |
2917 | ||
1394aabd | 2918 | if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) |
90374cc2 | 2919 | && DECL_SIZE (decl) != 0) |
e681c5a1 RS |
2920 | { |
2921 | if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST) | |
2922 | constant_expression_warning (DECL_SIZE (decl)); | |
2923 | else | |
ddd2d57e | 2924 | error ("%Jstorage size of '%D' isn't constant", decl, decl); |
e681c5a1 | 2925 | } |
e9a25f70 | 2926 | |
6645c3fa | 2927 | if (TREE_USED (type)) |
e9a25f70 | 2928 | TREE_USED (decl) = 1; |
51e29401 RS |
2929 | } |
2930 | ||
9661b15f JJ |
2931 | /* If this is a function and an assembler name is specified, reset DECL_RTL |
2932 | so we can give it its new name. Also, update built_in_decls if it | |
2933 | was a normal built-in. */ | |
3c4afaa5 | 2934 | if (TREE_CODE (decl) == FUNCTION_DECL && asmspec) |
6645c3fa | 2935 | { |
52a8b7b8 R |
2936 | /* ASMSPEC is given, and not the name of a register. Mark the |
2937 | name with a star so assemble_name won't munge it. */ | |
2938 | char *starred = alloca (strlen (asmspec) + 2); | |
2939 | starred[0] = '*'; | |
2940 | strcpy (starred + 1, asmspec); | |
2941 | ||
9661b15f JJ |
2942 | if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL) |
2943 | { | |
2944 | tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)]; | |
2945 | SET_DECL_RTL (builtin, NULL_RTX); | |
0b395e19 | 2946 | change_decl_assembler_name (builtin, get_identifier (starred)); |
9661b15f | 2947 | if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY) |
52a8b7b8 | 2948 | init_block_move_fn (starred); |
9661b15f | 2949 | else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET) |
52a8b7b8 | 2950 | init_block_clear_fn (starred); |
9661b15f | 2951 | } |
19e7881c | 2952 | SET_DECL_RTL (decl, NULL_RTX); |
fccc4eb2 | 2953 | change_decl_assembler_name (decl, get_identifier (starred)); |
6645c3fa | 2954 | } |
3c4afaa5 | 2955 | |
d05cc98e | 2956 | /* If #pragma weak was used, mark the decl weak now. */ |
f75fbaf7 | 2957 | if (current_scope == file_scope) |
d05cc98e GK |
2958 | maybe_apply_pragma_weak (decl); |
2959 | ||
51e29401 RS |
2960 | /* Output the assembler code and/or RTL code for variables and functions, |
2961 | unless the type is an undefined structure or union. | |
2962 | If not, it will get done when the type is completed. */ | |
2963 | ||
2964 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) | |
2965 | { | |
0f7866e7 | 2966 | /* This is a no-op in c-lang.c or something real in objc-act.c. */ |
37fa72e9 | 2967 | if (c_dialect_objc ()) |
0f7866e7 | 2968 | objc_check_decl (decl); |
8f17b5c5 | 2969 | |
4b1e44be | 2970 | if (DECL_FILE_SCOPE_P (decl)) |
8cf8d8a2 JM |
2971 | { |
2972 | if (DECL_INITIAL (decl) == NULL_TREE | |
2973 | || DECL_INITIAL (decl) == error_mark_node) | |
2974 | /* Don't output anything | |
2975 | when a tentative file-scope definition is seen. | |
2976 | But at end of compilation, do output code for them. */ | |
2977 | DECL_DEFER_OUTPUT (decl) = 1; | |
d1bd0ded | 2978 | rest_of_decl_compilation (decl, asmspec, true, 0); |
8cf8d8a2 | 2979 | } |
8f17b5c5 MM |
2980 | else |
2981 | { | |
0adc3c19 MM |
2982 | /* This is a local variable. If there is an ASMSPEC, the |
2983 | user has requested that we handle it specially. */ | |
8f17b5c5 | 2984 | if (asmspec) |
3645c4dc | 2985 | { |
0adc3c19 MM |
2986 | /* In conjunction with an ASMSPEC, the `register' |
2987 | keyword indicates that we should place the variable | |
2988 | in a particular register. */ | |
5baeaac0 JM |
2989 | if (C_DECL_REGISTER (decl)) |
2990 | { | |
6de9cd9a | 2991 | DECL_HARD_REGISTER (decl) = 1; |
5baeaac0 JM |
2992 | /* This cannot be done for a structure with volatile |
2993 | fields, on which DECL_REGISTER will have been | |
2994 | reset. */ | |
2995 | if (!DECL_REGISTER (decl)) | |
2996 | error ("cannot put object with volatile field into register"); | |
2997 | } | |
0adc3c19 MM |
2998 | |
2999 | /* If this is not a static variable, issue a warning. | |
3000 | It doesn't make any sense to give an ASMSPEC for an | |
3001 | ordinary, non-register local variable. Historically, | |
3002 | GCC has accepted -- but ignored -- the ASMSPEC in | |
3003 | this case. */ | |
35b1a6fa | 3004 | if (TREE_CODE (decl) == VAR_DECL |
5baeaac0 | 3005 | && !C_DECL_REGISTER (decl) |
0adc3c19 | 3006 | && !TREE_STATIC (decl)) |
ddd2d57e RH |
3007 | warning ("%Jignoring asm-specifier for non-static local " |
3008 | "variable '%D'", decl, decl); | |
0adc3c19 | 3009 | else |
fccc4eb2 | 3010 | change_decl_assembler_name (decl, get_identifier (asmspec)); |
3645c4dc | 3011 | } |
0adc3c19 | 3012 | |
c0a4369a | 3013 | if (TREE_CODE (decl) != FUNCTION_DECL) |
350fae66 | 3014 | add_stmt (build_stmt (DECL_EXPR, decl)); |
8f17b5c5 | 3015 | } |
4dd7201e | 3016 | |
4b1e44be | 3017 | if (!DECL_FILE_SCOPE_P (decl)) |
51e29401 RS |
3018 | { |
3019 | /* Recompute the RTL of a local array now | |
3020 | if it used to be an incomplete type. */ | |
3021 | if (was_incomplete | |
1394aabd | 3022 | && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl)) |
51e29401 RS |
3023 | { |
3024 | /* If we used it already as memory, it must stay in memory. */ | |
3025 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); | |
3026 | /* If it's still incomplete now, no init will save it. */ | |
3027 | if (DECL_SIZE (decl) == 0) | |
3028 | DECL_INITIAL (decl) = 0; | |
51e29401 | 3029 | } |
51e29401 RS |
3030 | } |
3031 | } | |
3032 | ||
d1bd0ded GK |
3033 | /* If this was marked 'used', be sure it will be output. */ |
3034 | if (lookup_attribute ("used", DECL_ATTRIBUTES (decl))) | |
bb9a388d | 3035 | mark_decl_referenced (decl); |
d1bd0ded | 3036 | |
51e29401 | 3037 | if (TREE_CODE (decl) == TYPE_DECL) |
1c9766da RK |
3038 | { |
3039 | if (!DECL_FILE_SCOPE_P (decl) | |
3040 | && variably_modified_type_p (TREE_TYPE (decl))) | |
350fae66 | 3041 | add_stmt (build_stmt (DECL_EXPR, decl)); |
1c9766da RK |
3042 | |
3043 | rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0); | |
3044 | } | |
51e29401 | 3045 | |
51e29401 RS |
3046 | /* At the end of a declaration, throw away any variable type sizes |
3047 | of types defined inside that declaration. There is no use | |
3048 | computing them in the following function definition. */ | |
f75fbaf7 | 3049 | if (current_scope == file_scope) |
51e29401 | 3050 | get_pending_sizes (); |
0bfa5f65 RH |
3051 | |
3052 | /* Install a cleanup (aka destructor) if one was given. */ | |
3053 | if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl)) | |
3054 | { | |
3055 | tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); | |
3056 | if (attr) | |
3057 | { | |
0bfa5f65 RH |
3058 | tree cleanup_id = TREE_VALUE (TREE_VALUE (attr)); |
3059 | tree cleanup_decl = lookup_name (cleanup_id); | |
3060 | tree cleanup; | |
3061 | ||
3062 | /* Build "cleanup(&decl)" for the destructor. */ | |
3063 | cleanup = build_unary_op (ADDR_EXPR, decl, 0); | |
3064 | cleanup = build_tree_list (NULL_TREE, cleanup); | |
3065 | cleanup = build_function_call (cleanup_decl, cleanup); | |
3066 | ||
3067 | /* Don't warn about decl unused; the cleanup uses it. */ | |
3068 | TREE_USED (decl) = 1; | |
325c3691 | 3069 | TREE_USED (cleanup_decl) = 1; |
0bfa5f65 RH |
3070 | |
3071 | /* Initialize EH, if we've been told to do so. */ | |
093c7153 | 3072 | if (flag_exceptions && !c_eh_initialized_p) |
0bfa5f65 | 3073 | { |
093c7153 | 3074 | c_eh_initialized_p = true; |
0bfa5f65 RH |
3075 | eh_personality_libfunc |
3076 | = init_one_libfunc (USING_SJLJ_EXCEPTIONS | |
3077 | ? "__gcc_personality_sj0" | |
3078 | : "__gcc_personality_v0"); | |
3079 | using_eh_for_cleanups (); | |
3080 | } | |
3081 | ||
325c3691 | 3082 | push_cleanup (decl, cleanup, false); |
0bfa5f65 RH |
3083 | } |
3084 | } | |
51e29401 RS |
3085 | } |
3086 | ||
55d54003 ZW |
3087 | /* Given a parsed parameter declaration, decode it into a PARM_DECL |
3088 | and push that on the current scope. */ | |
51e29401 RS |
3089 | |
3090 | void | |
35b1a6fa | 3091 | push_parm_decl (tree parm) |
51e29401 | 3092 | { |
6cc902a1 | 3093 | tree decl; |
14077d68 | 3094 | |
005979f2 | 3095 | decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)), |
2ff7cce4 JM |
3096 | TREE_PURPOSE (TREE_PURPOSE (parm)), |
3097 | PARM, 0, NULL); | |
59387d2e | 3098 | decl_attributes (&decl, TREE_VALUE (parm), 0); |
6a5ed5bf | 3099 | |
51e29401 RS |
3100 | decl = pushdecl (decl); |
3101 | ||
51e29401 RS |
3102 | finish_decl (decl, NULL_TREE, NULL_TREE); |
3103 | } | |
3104 | ||
f75fbaf7 | 3105 | /* Mark all the parameter declarations to date as forward decls. |
f91f41b2 | 3106 | Also diagnose use of this extension. */ |
51e29401 RS |
3107 | |
3108 | void | |
55d54003 | 3109 | mark_forward_parm_decls (void) |
51e29401 | 3110 | { |
f75fbaf7 | 3111 | struct c_binding *b; |
55d54003 ZW |
3112 | |
3113 | if (pedantic && !current_scope->warned_forward_parm_decls) | |
3114 | { | |
3115 | pedwarn ("ISO C forbids forward parameter declarations"); | |
3116 | current_scope->warned_forward_parm_decls = true; | |
3117 | } | |
3118 | ||
f75fbaf7 ZW |
3119 | for (b = current_scope->bindings; b; b = b->prev) |
3120 | if (TREE_CODE (b->decl) == PARM_DECL) | |
3121 | TREE_ASM_WRITTEN (b->decl) = 1; | |
51e29401 RS |
3122 | } |
3123 | \f | |
17211ab5 GK |
3124 | static GTY(()) int compound_literal_number; |
3125 | ||
db3acfa5 JM |
3126 | /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound |
3127 | literal, which may be an incomplete array type completed by the | |
3128 | initializer; INIT is a CONSTRUCTOR that initializes the compound | |
3129 | literal. */ | |
3130 | ||
3131 | tree | |
35b1a6fa | 3132 | build_compound_literal (tree type, tree init) |
db3acfa5 JM |
3133 | { |
3134 | /* We do not use start_decl here because we have a type, not a declarator; | |
3135 | and do not use finish_decl because the decl should be stored inside | |
350fae66 | 3136 | the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */ |
db3acfa5 JM |
3137 | tree decl = build_decl (VAR_DECL, NULL_TREE, type); |
3138 | tree complit; | |
8d37a5c0 | 3139 | tree stmt; |
db3acfa5 JM |
3140 | DECL_EXTERNAL (decl) = 0; |
3141 | TREE_PUBLIC (decl) = 0; | |
f75fbaf7 | 3142 | TREE_STATIC (decl) = (current_scope == file_scope); |
db3acfa5 JM |
3143 | DECL_CONTEXT (decl) = current_function_decl; |
3144 | TREE_USED (decl) = 1; | |
3145 | TREE_TYPE (decl) = type; | |
4f976745 | 3146 | TREE_READONLY (decl) = TYPE_READONLY (type); |
db3acfa5 JM |
3147 | store_init_value (decl, init); |
3148 | ||
3149 | if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type)) | |
3150 | { | |
3151 | int failure = complete_array_type (type, DECL_INITIAL (decl), 1); | |
3152 | if (failure) | |
3153 | abort (); | |
3154 | } | |
3155 | ||
3156 | type = TREE_TYPE (decl); | |
3157 | if (type == error_mark_node || !COMPLETE_TYPE_P (type)) | |
3158 | return error_mark_node; | |
3159 | ||
350fae66 | 3160 | stmt = build_stmt (DECL_EXPR, decl); |
8d37a5c0 | 3161 | complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt); |
db3acfa5 JM |
3162 | TREE_SIDE_EFFECTS (complit) = 1; |
3163 | ||
3164 | layout_decl (decl, 0); | |
3165 | ||
3166 | if (TREE_STATIC (decl)) | |
3167 | { | |
3168 | /* This decl needs a name for the assembler output. We also need | |
cf3c4f56 JJ |
3169 | a unique suffix to be added to the name. */ |
3170 | char *name; | |
cf3c4f56 | 3171 | |
35b1a6fa | 3172 | ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal", |
17211ab5 GK |
3173 | compound_literal_number); |
3174 | compound_literal_number++; | |
cf3c4f56 JJ |
3175 | DECL_NAME (decl) = get_identifier (name); |
3176 | DECL_DEFER_OUTPUT (decl) = 1; | |
3177 | DECL_COMDAT (decl) = 1; | |
3178 | DECL_ARTIFICIAL (decl) = 1; | |
3179 | pushdecl (decl); | |
db3acfa5 | 3180 | rest_of_decl_compilation (decl, NULL, 1, 0); |
db3acfa5 JM |
3181 | } |
3182 | ||
3183 | return complit; | |
3184 | } | |
3185 | \f | |
51e29401 | 3186 | /* Make TYPE a complete type based on INITIAL_VALUE. |
929f3671 | 3187 | Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, |
51e29401 RS |
3188 | 2 if there was no information (in which case assume 1 if DO_DEFAULT). */ |
3189 | ||
3190 | int | |
35b1a6fa | 3191 | complete_array_type (tree type, tree initial_value, int do_default) |
51e29401 | 3192 | { |
b3694847 | 3193 | tree maxindex = NULL_TREE; |
51e29401 RS |
3194 | int value = 0; |
3195 | ||
3196 | if (initial_value) | |
3197 | { | |
3198 | /* Note MAXINDEX is really the maximum index, | |
3199 | one less than the size. */ | |
3200 | if (TREE_CODE (initial_value) == STRING_CST) | |
3201 | { | |
3202 | int eltsize | |
3203 | = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value))); | |
20bf3fac RS |
3204 | maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value) |
3205 | / eltsize) - 1, 0); | |
51e29401 RS |
3206 | } |
3207 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) | |
3208 | { | |
ecd4cee0 | 3209 | tree elts = CONSTRUCTOR_ELTS (initial_value); |
fed3cef0 | 3210 | maxindex = build_int_2 (-1, -1); |
ecd4cee0 RS |
3211 | for (; elts; elts = TREE_CHAIN (elts)) |
3212 | { | |
3213 | if (TREE_PURPOSE (elts)) | |
3214 | maxindex = TREE_PURPOSE (elts); | |
3215 | else | |
fed3cef0 RK |
3216 | maxindex = fold (build (PLUS_EXPR, integer_type_node, |
3217 | maxindex, integer_one_node)); | |
ecd4cee0 RS |
3218 | } |
3219 | maxindex = copy_node (maxindex); | |
51e29401 RS |
3220 | } |
3221 | else | |
3222 | { | |
3223 | /* Make an error message unless that happened already. */ | |
3224 | if (initial_value != error_mark_node) | |
3225 | value = 1; | |
3226 | ||
3227 | /* Prevent further error messages. */ | |
b4892310 | 3228 | maxindex = build_int_2 (0, 0); |
51e29401 RS |
3229 | } |
3230 | } | |
3231 | ||
3232 | if (!maxindex) | |
3233 | { | |
3234 | if (do_default) | |
b4892310 | 3235 | maxindex = build_int_2 (0, 0); |
51e29401 RS |
3236 | value = 2; |
3237 | } | |
3238 | ||
3239 | if (maxindex) | |
3240 | { | |
3241 | TYPE_DOMAIN (type) = build_index_type (maxindex); | |
3242 | if (!TREE_TYPE (maxindex)) | |
3243 | TREE_TYPE (maxindex) = TYPE_DOMAIN (type); | |
3244 | } | |
3245 | ||
3246 | /* Lay out the type now that we can get the real answer. */ | |
3247 | ||
3248 | layout_type (type); | |
3249 | ||
3250 | return value; | |
3251 | } | |
3252 | \f | |
2984fe64 JM |
3253 | /* Determine whether TYPE is a structure with a flexible array member, |
3254 | or a union containing such a structure (possibly recursively). */ | |
3255 | ||
3256 | static bool | |
35b1a6fa | 3257 | flexible_array_type_p (tree type) |
2984fe64 JM |
3258 | { |
3259 | tree x; | |
3260 | switch (TREE_CODE (type)) | |
3261 | { | |
3262 | case RECORD_TYPE: | |
3263 | x = TYPE_FIELDS (type); | |
3264 | if (x == NULL_TREE) | |
3265 | return false; | |
3266 | while (TREE_CHAIN (x) != NULL_TREE) | |
3267 | x = TREE_CHAIN (x); | |
3268 | if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE | |
3269 | && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE | |
3270 | && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE | |
3271 | && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) | |
3272 | return true; | |
3273 | return false; | |
3274 | case UNION_TYPE: | |
3275 | for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x)) | |
3276 | { | |
3277 | if (flexible_array_type_p (TREE_TYPE (x))) | |
3278 | return true; | |
3279 | } | |
3280 | return false; | |
3281 | default: | |
3282 | return false; | |
3283 | } | |
3284 | } | |
3285 | \f | |
2ff7cce4 JM |
3286 | /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME, |
3287 | replacing with appropriate values if they are invalid. */ | |
3288 | static void | |
3289 | check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name) | |
3290 | { | |
3291 | tree type_mv; | |
3292 | unsigned int max_width; | |
3293 | unsigned HOST_WIDE_INT w; | |
3294 | const char *name = orig_name ? orig_name: _("<anonymous>"); | |
3295 | ||
3296 | /* Necessary? */ | |
3297 | STRIP_NOPS (*width); | |
3298 | ||
3299 | /* Detect and ignore out of range field width and process valid | |
3300 | field widths. */ | |
3301 | if (TREE_CODE (*width) != INTEGER_CST) | |
3302 | { | |
3303 | error ("bit-field `%s' width not an integer constant", name); | |
3304 | *width = integer_one_node; | |
3305 | } | |
3306 | else | |
3307 | { | |
3308 | constant_expression_warning (*width); | |
3309 | if (tree_int_cst_sgn (*width) < 0) | |
3310 | { | |
3311 | error ("negative width in bit-field `%s'", name); | |
3312 | *width = integer_one_node; | |
3313 | } | |
3314 | else if (integer_zerop (*width) && orig_name) | |
3315 | { | |
3316 | error ("zero width for bit-field `%s'", name); | |
3317 | *width = integer_one_node; | |
3318 | } | |
3319 | } | |
3320 | ||
3321 | /* Detect invalid bit-field type. */ | |
3322 | if (TREE_CODE (*type) != INTEGER_TYPE | |
3323 | && TREE_CODE (*type) != BOOLEAN_TYPE | |
3324 | && TREE_CODE (*type) != ENUMERAL_TYPE) | |
3325 | { | |
3326 | error ("bit-field `%s' has invalid type", name); | |
3327 | *type = unsigned_type_node; | |
3328 | } | |
3329 | ||
3330 | type_mv = TYPE_MAIN_VARIANT (*type); | |
3331 | if (pedantic | |
3332 | && type_mv != integer_type_node | |
3333 | && type_mv != unsigned_type_node | |
a3bf324c | 3334 | && type_mv != boolean_type_node) |
2ff7cce4 JM |
3335 | pedwarn ("type of bit-field `%s' is a GCC extension", name); |
3336 | ||
3337 | if (type_mv == boolean_type_node) | |
3338 | max_width = CHAR_TYPE_SIZE; | |
3339 | else | |
3340 | max_width = TYPE_PRECISION (*type); | |
3341 | ||
3342 | if (0 < compare_tree_int (*width, max_width)) | |
3343 | { | |
3344 | error ("width of `%s' exceeds its type", name); | |
3345 | w = max_width; | |
3346 | *width = build_int_2 (w, 0); | |
3347 | } | |
3348 | else | |
3349 | w = tree_low_cst (*width, 1); | |
3350 | ||
6de9cd9a DN |
3351 | if (TREE_CODE (*type) == ENUMERAL_TYPE) |
3352 | { | |
3353 | struct lang_type *lt = TYPE_LANG_SPECIFIC (*type); | |
118a3a8b | 3354 | if (!lt |
6de9cd9a DN |
3355 | || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type)) |
3356 | || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type))) | |
3357 | warning ("`%s' is narrower than values of its type", name); | |
3358 | } | |
2ff7cce4 JM |
3359 | } |
3360 | \f | |
51e29401 RS |
3361 | /* Given declspecs and a declarator, |
3362 | determine the name and type of the object declared | |
3363 | and construct a ..._DECL node for it. | |
3364 | (In one case we can return a ..._TYPE node instead. | |
3365 | For invalid input we sometimes return 0.) | |
3366 | ||
3367 | DECLSPECS is a chain of tree_list nodes whose value fields | |
3368 | are the storage classes and type specifiers. | |
3369 | ||
3370 | DECL_CONTEXT says which syntactic context this declaration is in: | |
3371 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. | |
3372 | FUNCDEF for a function definition. Like NORMAL but a few different | |
3373 | error messages in each case. Return value may be zero meaning | |
3374 | this definition is too screwy to try to parse. | |
3375 | PARM for a parameter declaration (either within a function prototype | |
3376 | or before a function body). Make a PARM_DECL, or return void_type_node. | |
3377 | TYPENAME if for a typename (in a cast or sizeof). | |
3378 | Don't make a DECL node; just return the ..._TYPE node. | |
3379 | FIELD for a struct or union field; make a FIELD_DECL. | |
51e29401 | 3380 | INITIALIZED is 1 if the decl has an initializer. |
2ff7cce4 JM |
3381 | WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node |
3382 | representing the width of the bit-field. | |
51e29401 RS |
3383 | |
3384 | In the TYPENAME case, DECLARATOR is really an absolute declarator. | |
3385 | It may also be so in the PARM case, for a prototype where the | |
3386 | argument type is specified but not the name. | |
3387 | ||
3388 | This function is where the complicated C meanings of `static' | |
929f3671 | 3389 | and `extern' are interpreted. */ |
51e29401 RS |
3390 | |
3391 | static tree | |
35b1a6fa | 3392 | grokdeclarator (tree declarator, tree declspecs, |
2ff7cce4 | 3393 | enum decl_context decl_context, int initialized, tree *width) |
51e29401 RS |
3394 | { |
3395 | int specbits = 0; | |
3396 | tree spec; | |
3397 | tree type = NULL_TREE; | |
3398 | int longlong = 0; | |
3399 | int constp; | |
3932261a | 3400 | int restrictp; |
51e29401 | 3401 | int volatilep; |
3932261a | 3402 | int type_quals = TYPE_UNQUALIFIED; |
51e29401 RS |
3403 | int inlinep; |
3404 | int explicit_int = 0; | |
3405 | int explicit_char = 0; | |
5ab10c42 | 3406 | int defaulted_int = 0; |
51e29401 | 3407 | tree typedef_decl = 0; |
2ff7cce4 | 3408 | const char *name, *orig_name; |
51e29401 RS |
3409 | tree typedef_type = 0; |
3410 | int funcdef_flag = 0; | |
3411 | enum tree_code innermost_code = ERROR_MARK; | |
929f3671 | 3412 | int size_varies = 0; |
91d231cb | 3413 | tree decl_attr = NULL_TREE; |
0e03329a JM |
3414 | tree array_ptr_quals = NULL_TREE; |
3415 | int array_parm_static = 0; | |
91d231cb | 3416 | tree returned_attrs = NULL_TREE; |
2ff7cce4 | 3417 | bool bitfield = width != NULL; |
85b58ca5 | 3418 | tree element_type; |
77dbdb57 | 3419 | tree arg_info = NULL_TREE; |
51e29401 RS |
3420 | |
3421 | if (decl_context == FUNCDEF) | |
3422 | funcdef_flag = 1, decl_context = NORMAL; | |
3423 | ||
51e29401 RS |
3424 | /* Look inside a declarator for the name being declared |
3425 | and get it as a string, for an error message. */ | |
3426 | { | |
b3694847 | 3427 | tree decl = declarator; |
51e29401 RS |
3428 | name = 0; |
3429 | ||
3430 | while (decl) | |
3431 | switch (TREE_CODE (decl)) | |
3432 | { | |
3433 | case ARRAY_REF: | |
3434 | case INDIRECT_REF: | |
3435 | case CALL_EXPR: | |
3436 | innermost_code = TREE_CODE (decl); | |
3437 | decl = TREE_OPERAND (decl, 0); | |
3438 | break; | |
3439 | ||
91d231cb JM |
3440 | case TREE_LIST: |
3441 | decl = TREE_VALUE (decl); | |
3442 | break; | |
3443 | ||
51e29401 RS |
3444 | case IDENTIFIER_NODE: |
3445 | name = IDENTIFIER_POINTER (decl); | |
3446 | decl = 0; | |
3447 | break; | |
3448 | ||
3449 | default: | |
3450 | abort (); | |
3451 | } | |
2ff7cce4 | 3452 | orig_name = name; |
51e29401 RS |
3453 | if (name == 0) |
3454 | name = "type name"; | |
3455 | } | |
3456 | ||
3457 | /* A function definition's declarator must have the form of | |
3458 | a function declarator. */ | |
3459 | ||
3460 | if (funcdef_flag && innermost_code != CALL_EXPR) | |
3461 | return 0; | |
3462 | ||
51e29401 RS |
3463 | /* If this looks like a function definition, make it one, |
3464 | even if it occurs where parms are expected. | |
3465 | Then store_parm_decls will reject it and not use it as a parm. */ | |
f75fbaf7 | 3466 | if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag) |
51e29401 RS |
3467 | decl_context = PARM; |
3468 | ||
3469 | /* Look through the decl specs and record which ones appear. | |
3470 | Some typespecs are defined as built-in typenames. | |
3471 | Others, the ones that are modifiers of other types, | |
3472 | are represented by bits in SPECBITS: set the bits for | |
3473 | the modifiers that appear. Storage class keywords are also in SPECBITS. | |
3474 | ||
3475 | If there is a typedef name or a type, store the type in TYPE. | |
3476 | This includes builtin typedefs such as `int'. | |
3477 | ||
3478 | Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char' | |
3479 | and did not come from a user typedef. | |
3480 | ||
3481 | Set LONGLONG if `long' is mentioned twice. */ | |
3482 | ||
3483 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) | |
3484 | { | |
b3694847 | 3485 | tree id = TREE_VALUE (spec); |
51e29401 | 3486 | |
e23bd218 IR |
3487 | /* If the entire declaration is itself tagged as deprecated then |
3488 | suppress reports of deprecated items. */ | |
3489 | if (id && TREE_DEPRECATED (id)) | |
3490 | { | |
3491 | if (deprecated_state != DEPRECATED_SUPPRESS) | |
3492 | warn_deprecated_use (id); | |
3493 | } | |
3494 | ||
51e29401 RS |
3495 | if (id == ridpointers[(int) RID_INT]) |
3496 | explicit_int = 1; | |
3497 | if (id == ridpointers[(int) RID_CHAR]) | |
3498 | explicit_char = 1; | |
3499 | ||
0e5921e8 ZW |
3500 | if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id)) |
3501 | { | |
3502 | enum rid i = C_RID_CODE (id); | |
dbbbbf3b | 3503 | if ((int) i <= (int) RID_LAST_MODIFIER) |
0e5921e8 | 3504 | { |
3d78f2e9 | 3505 | if (i == RID_LONG && (specbits & (1 << (int) RID_LONG))) |
0e5921e8 ZW |
3506 | { |
3507 | if (longlong) | |
3508 | error ("`long long long' is too long for GCC"); | |
3509 | else | |
3510 | { | |
3511 | if (pedantic && !flag_isoc99 && ! in_system_header | |
3512 | && warn_long_long) | |
56508306 | 3513 | pedwarn ("ISO C90 does not support `long long'"); |
0e5921e8 ZW |
3514 | longlong = 1; |
3515 | } | |
3516 | } | |
dbbbbf3b | 3517 | else if (specbits & (1 << (int) i)) |
04e6db94 JM |
3518 | { |
3519 | if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT) | |
3520 | { | |
28fca7e4 | 3521 | if (pedantic && !flag_isoc99) |
04e6db94 JM |
3522 | pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
3523 | } | |
3524 | else | |
3525 | error ("duplicate `%s'", IDENTIFIER_POINTER (id)); | |
3526 | } | |
3d78f2e9 RH |
3527 | |
3528 | /* Diagnose "__thread extern". Recall that this list | |
3529 | is in the reverse order seen in the text. */ | |
3530 | if (i == RID_THREAD | |
3531 | && (specbits & (1 << (int) RID_EXTERN | |
3532 | | 1 << (int) RID_STATIC))) | |
3533 | { | |
3534 | if (specbits & 1 << (int) RID_EXTERN) | |
3535 | error ("`__thread' before `extern'"); | |
3536 | else | |
3537 | error ("`__thread' before `static'"); | |
3538 | } | |
3539 | ||
dbbbbf3b | 3540 | specbits |= 1 << (int) i; |
0e5921e8 ZW |
3541 | goto found; |
3542 | } | |
3543 | } | |
51e29401 RS |
3544 | if (type) |
3545 | error ("two or more data types in declaration of `%s'", name); | |
3546 | /* Actual typedefs come to us as TYPE_DECL nodes. */ | |
3547 | else if (TREE_CODE (id) == TYPE_DECL) | |
3548 | { | |
6169e5fd JM |
3549 | if (TREE_TYPE (id) == error_mark_node) |
3550 | ; /* Allow the type to default to int to avoid cascading errors. */ | |
3551 | else | |
3552 | { | |
3553 | type = TREE_TYPE (id); | |
3554 | decl_attr = DECL_ATTRIBUTES (id); | |
3555 | typedef_decl = id; | |
3556 | } | |
51e29401 RS |
3557 | } |
3558 | /* Built-in types come as identifiers. */ | |
3559 | else if (TREE_CODE (id) == IDENTIFIER_NODE) | |
3560 | { | |
b3694847 | 3561 | tree t = lookup_name (id); |
f75fbaf7 | 3562 | if (!t || TREE_CODE (t) != TYPE_DECL) |
51e29401 RS |
3563 | error ("`%s' fails to be a typedef or built in type", |
3564 | IDENTIFIER_POINTER (id)); | |
f75fbaf7 ZW |
3565 | else if (TREE_TYPE (t) == error_mark_node) |
3566 | ; | |
51e29401 RS |
3567 | else |
3568 | { | |
3569 | type = TREE_TYPE (t); | |
3570 | typedef_decl = t; | |
3571 | } | |
3572 | } | |
3573 | else if (TREE_CODE (id) != ERROR_MARK) | |
3574 | type = id; | |
3575 | ||
6645c3fa KH |
3576 | found: |
3577 | ; | |
51e29401 RS |
3578 | } |
3579 | ||
3580 | typedef_type = type; | |
3581 | if (type) | |
929f3671 | 3582 | size_varies = C_TYPE_VARIABLE_SIZE (type); |
51e29401 | 3583 | |
5ab10c42 | 3584 | /* No type at all: default to `int', and set DEFAULTED_INT |
51e29401 RS |
3585 | because it was not a user-defined typedef. */ |
3586 | ||
3587 | if (type == 0) | |
3588 | { | |
f5963e61 JL |
3589 | if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) |
3590 | | (1 << (int) RID_SIGNED) | |
60e9d01c JM |
3591 | | (1 << (int) RID_UNSIGNED) |
3592 | | (1 << (int) RID_COMPLEX)))) | |
03369c93 AS |
3593 | /* Don't warn about typedef foo = bar. */ |
3594 | && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized) | |
4dd7201e | 3595 | && ! in_system_header) |
720283f2 | 3596 | { |
916269ab | 3597 | /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type |
6f4d7222 UD |
3598 | and this is a function, or if -Wimplicit; prefer the former |
3599 | warning since it is more explicit. */ | |
f43b2795 JM |
3600 | if ((warn_implicit_int || warn_return_type || flag_isoc99) |
3601 | && funcdef_flag) | |
720283f2 | 3602 | warn_about_return_type = 1; |
916269ab | 3603 | else if (warn_implicit_int || flag_isoc99) |
6645c3fa KH |
3604 | pedwarn_c99 ("type defaults to `int' in declaration of `%s'", |
3605 | name); | |
720283f2 RK |
3606 | } |
3607 | ||
5ab10c42 | 3608 | defaulted_int = 1; |
51e29401 RS |
3609 | type = integer_type_node; |
3610 | } | |
3611 | ||
3612 | /* Now process the modifiers that were specified | |
3613 | and check for invalid combinations. */ | |
3614 | ||
3615 | /* Long double is a special combination. */ | |
3616 | ||
861bb6c1 | 3617 | if ((specbits & 1 << (int) RID_LONG) && ! longlong |
90d56da8 | 3618 | && TYPE_MAIN_VARIANT (type) == double_type_node) |
51e29401 | 3619 | { |
6645c3fa | 3620 | specbits &= ~(1 << (int) RID_LONG); |
51e29401 RS |
3621 | type = long_double_type_node; |
3622 | } | |
3623 | ||
3624 | /* Check all other uses of type modifiers. */ | |
3625 | ||
3626 | if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) | |
3627 | | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED))) | |
3628 | { | |
3629 | int ok = 0; | |
3630 | ||
861bb6c1 JL |
3631 | if ((specbits & 1 << (int) RID_LONG) |
3632 | && (specbits & 1 << (int) RID_SHORT)) | |
3633 | error ("both long and short specified for `%s'", name); | |
51e29401 RS |
3634 | else if (((specbits & 1 << (int) RID_LONG) |
3635 | || (specbits & 1 << (int) RID_SHORT)) | |
3636 | && explicit_char) | |
3637 | error ("long or short specified with char for `%s'", name); | |
3638 | else if (((specbits & 1 << (int) RID_LONG) | |
3639 | || (specbits & 1 << (int) RID_SHORT)) | |
3640 | && TREE_CODE (type) == REAL_TYPE) | |
861bb6c1 JL |
3641 | { |
3642 | static int already = 0; | |
3643 | ||
3644 | error ("long or short specified with floating type for `%s'", name); | |
3645 | if (! already && ! pedantic) | |
3646 | { | |
3647 | error ("the only valid combination is `long double'"); | |
3648 | already = 1; | |
3649 | } | |
3650 | } | |
51e29401 RS |
3651 | else if ((specbits & 1 << (int) RID_SIGNED) |
3652 | && (specbits & 1 << (int) RID_UNSIGNED)) | |
861bb6c1 JL |
3653 | error ("both signed and unsigned specified for `%s'", name); |
3654 | else if (TREE_CODE (type) != INTEGER_TYPE) | |
3655 | error ("long, short, signed or unsigned invalid for `%s'", name); | |
51e29401 RS |
3656 | else |
3657 | { | |
3658 | ok = 1; | |
b462d4ab | 3659 | if (!explicit_int && !defaulted_int && !explicit_char) |
51e29401 | 3660 | { |
b462d4ab JM |
3661 | error ("long, short, signed or unsigned used invalidly for `%s'", |
3662 | name); | |
3663 | ok = 0; | |
51e29401 RS |
3664 | } |
3665 | } | |
3666 | ||
3667 | /* Discard the type modifiers if they are invalid. */ | |
3668 | if (! ok) | |
3669 | { | |
3670 | specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) | |
3671 | | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)); | |
3672 | longlong = 0; | |
3673 | } | |
3674 | } | |
3675 | ||
c470260b RK |
3676 | if ((specbits & (1 << (int) RID_COMPLEX)) |
3677 | && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) | |
3678 | { | |
3679 | error ("complex invalid for `%s'", name); | |
6645c3fa | 3680 | specbits &= ~(1 << (int) RID_COMPLEX); |
c470260b RK |
3681 | } |
3682 | ||
51e29401 | 3683 | /* Decide whether an integer type is signed or not. |
2ff7cce4 | 3684 | Optionally treat bit-fields as signed by default. */ |
51e29401 | 3685 | if (specbits & 1 << (int) RID_UNSIGNED |
51e29401 | 3686 | || (bitfield && ! flag_signed_bitfields |
5ab10c42 | 3687 | && (explicit_int || defaulted_int || explicit_char |
51e29401 RS |
3688 | /* A typedef for plain `int' without `signed' |
3689 | can be controlled just like plain `int'. */ | |
3690 | || ! (typedef_decl != 0 | |
3691 | && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
3692 | && TREE_CODE (type) != ENUMERAL_TYPE | |
3693 | && !(specbits & 1 << (int) RID_SIGNED))) | |
3694 | { | |
3695 | if (longlong) | |
3696 | type = long_long_unsigned_type_node; | |
3697 | else if (specbits & 1 << (int) RID_LONG) | |
3698 | type = long_unsigned_type_node; | |
3699 | else if (specbits & 1 << (int) RID_SHORT) | |
3700 | type = short_unsigned_type_node; | |
3701 | else if (type == char_type_node) | |
3702 | type = unsigned_char_type_node; | |
3703 | else if (typedef_decl) | |
ceef8ce4 | 3704 | type = c_common_unsigned_type (type); |
51e29401 RS |
3705 | else |
3706 | type = unsigned_type_node; | |
3707 | } | |
3708 | else if ((specbits & 1 << (int) RID_SIGNED) | |
3709 | && type == char_type_node) | |
3710 | type = signed_char_type_node; | |
3711 | else if (longlong) | |
3712 | type = long_long_integer_type_node; | |
3713 | else if (specbits & 1 << (int) RID_LONG) | |
3714 | type = long_integer_type_node; | |
3715 | else if (specbits & 1 << (int) RID_SHORT) | |
3716 | type = short_integer_type_node; | |
c470260b RK |
3717 | |
3718 | if (specbits & 1 << (int) RID_COMPLEX) | |
5ab10c42 | 3719 | { |
60e9d01c | 3720 | if (pedantic && !flag_isoc99) |
56508306 | 3721 | pedwarn ("ISO C90 does not support complex types"); |
c470260b RK |
3722 | /* If we just have "complex", it is equivalent to |
3723 | "complex double", but if any modifiers at all are specified it is | |
3724 | the complex form of TYPE. E.g, "complex short" is | |
3725 | "complex short int". */ | |
3726 | ||
3727 | if (defaulted_int && ! longlong | |
3728 | && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) | |
3729 | | (1 << (int) RID_SIGNED) | |
3730 | | (1 << (int) RID_UNSIGNED)))) | |
60e9d01c JM |
3731 | { |
3732 | if (pedantic) | |
3733 | pedwarn ("ISO C does not support plain `complex' meaning `double complex'"); | |
3734 | type = complex_double_type_node; | |
3735 | } | |
5ab10c42 | 3736 | else if (type == integer_type_node) |
60e9d01c JM |
3737 | { |
3738 | if (pedantic) | |
3739 | pedwarn ("ISO C does not support complex integer types"); | |
3740 | type = complex_integer_type_node; | |
3741 | } | |
5ab10c42 RS |
3742 | else if (type == float_type_node) |
3743 | type = complex_float_type_node; | |
3744 | else if (type == double_type_node) | |
3745 | type = complex_double_type_node; | |
3746 | else if (type == long_double_type_node) | |
3747 | type = complex_long_double_type_node; | |
3748 | else | |
60e9d01c JM |
3749 | { |
3750 | if (pedantic) | |
3751 | pedwarn ("ISO C does not support complex integer types"); | |
3752 | type = build_complex_type (type); | |
3753 | } | |
5ab10c42 | 3754 | } |
51e29401 | 3755 | |
2ff7cce4 JM |
3756 | /* Check the type and width of a bit-field. */ |
3757 | if (bitfield) | |
3758 | check_bitfield_type_and_width (&type, width, orig_name); | |
3759 | ||
3932261a MM |
3760 | /* Figure out the type qualifiers for the declaration. There are |
3761 | two ways a declaration can become qualified. One is something | |
3762 | like `const int i' where the `const' is explicit. Another is | |
3763 | something like `typedef const int CI; CI i' where the type of the | |
85b58ca5 JM |
3764 | declaration contains the `const'. A third possibility is that |
3765 | there is a type qualifier on the element type of a typedefed | |
3766 | array type, in which case we should extract that qualifier so | |
3767 | that c_apply_type_quals_to_decls receives the full list of | |
3768 | qualifiers to work with (C90 is not entirely clear about whether | |
3769 | duplicate qualifiers should be diagnosed in this case, but it | |
3770 | seems most appropriate to do so). */ | |
3771 | element_type = strip_array_types (type); | |
3772 | constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type); | |
3773 | restrictp | |
3774 | = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type); | |
3775 | volatilep | |
3776 | = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type); | |
51e29401 | 3777 | inlinep = !! (specbits & (1 << (int) RID_INLINE)); |
28fca7e4 RH |
3778 | if (pedantic && !flag_isoc99) |
3779 | { | |
3780 | if (constp > 1) | |
3781 | pedwarn ("duplicate `const'"); | |
3782 | if (restrictp > 1) | |
3783 | pedwarn ("duplicate `restrict'"); | |
3784 | if (volatilep > 1) | |
3785 | pedwarn ("duplicate `volatile'"); | |
3786 | } | |
3932261a | 3787 | if (! flag_gen_aux_info && (TYPE_QUALS (type))) |
51e29401 | 3788 | type = TYPE_MAIN_VARIANT (type); |
3932261a MM |
3789 | type_quals = ((constp ? TYPE_QUAL_CONST : 0) |
3790 | | (restrictp ? TYPE_QUAL_RESTRICT : 0) | |
3791 | | (volatilep ? TYPE_QUAL_VOLATILE : 0)); | |
51e29401 RS |
3792 | |
3793 | /* Warn if two storage classes are given. Default to `auto'. */ | |
3794 | ||
3795 | { | |
3796 | int nclasses = 0; | |
3797 | ||
3798 | if (specbits & 1 << (int) RID_AUTO) nclasses++; | |
3799 | if (specbits & 1 << (int) RID_STATIC) nclasses++; | |
3800 | if (specbits & 1 << (int) RID_EXTERN) nclasses++; | |
3801 | if (specbits & 1 << (int) RID_REGISTER) nclasses++; | |
3802 | if (specbits & 1 << (int) RID_TYPEDEF) nclasses++; | |
3803 | ||
3d78f2e9 RH |
3804 | /* "static __thread" and "extern __thread" are allowed. */ |
3805 | if ((specbits & (1 << (int) RID_THREAD | |
3806 | | 1 << (int) RID_STATIC | |
3807 | | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD)) | |
3808 | nclasses++; | |
3809 | ||
51e29401 RS |
3810 | /* Warn about storage classes that are invalid for certain |
3811 | kinds of declarations (parameters, typenames, etc.). */ | |
3812 | ||
3813 | if (nclasses > 1) | |
3814 | error ("multiple storage classes in declaration of `%s'", name); | |
3815 | else if (funcdef_flag | |
3816 | && (specbits | |
3817 | & ((1 << (int) RID_REGISTER) | |
3818 | | (1 << (int) RID_AUTO) | |
3d78f2e9 RH |
3819 | | (1 << (int) RID_TYPEDEF) |
3820 | | (1 << (int) RID_THREAD)))) | |
51e29401 RS |
3821 | { |
3822 | if (specbits & 1 << (int) RID_AUTO | |
f75fbaf7 | 3823 | && (pedantic || current_scope == file_scope)) |
51e29401 RS |
3824 | pedwarn ("function definition declared `auto'"); |
3825 | if (specbits & 1 << (int) RID_REGISTER) | |
3826 | error ("function definition declared `register'"); | |
3827 | if (specbits & 1 << (int) RID_TYPEDEF) | |
3828 | error ("function definition declared `typedef'"); | |
3d78f2e9 RH |
3829 | if (specbits & 1 << (int) RID_THREAD) |
3830 | error ("function definition declared `__thread'"); | |
6645c3fa | 3831 | specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) |
3d78f2e9 | 3832 | | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD)); |
51e29401 RS |
3833 | } |
3834 | else if (decl_context != NORMAL && nclasses > 0) | |
3835 | { | |
3836 | if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER) | |
3837 | ; | |
3838 | else | |
3839 | { | |
913d0833 KG |
3840 | switch (decl_context) |
3841 | { | |
3842 | case FIELD: | |
6645c3fa KH |
3843 | error ("storage class specified for structure field `%s'", |
3844 | name); | |
913d0833 KG |
3845 | break; |
3846 | case PARM: | |
3847 | error ("storage class specified for parameter `%s'", name); | |
3848 | break; | |
3849 | default: | |
3850 | error ("storage class specified for typename"); | |
3851 | break; | |
3852 | } | |
6645c3fa KH |
3853 | specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) |
3854 | | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC) | |
3d78f2e9 | 3855 | | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD)); |
51e29401 RS |
3856 | } |
3857 | } | |
3858 | else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag) | |
3859 | { | |
f8521984 | 3860 | /* `extern' with initialization is invalid if not at file scope. */ |
f75fbaf7 | 3861 | if (current_scope == file_scope) |
51e29401 RS |
3862 | warning ("`%s' initialized and declared `extern'", name); |
3863 | else | |
3864 | error ("`%s' has both `extern' and initializer", name); | |
3865 | } | |
f75fbaf7 | 3866 | else if (current_scope == file_scope) |
3d78f2e9 RH |
3867 | { |
3868 | if (specbits & 1 << (int) RID_AUTO) | |
f8521984 | 3869 | error ("file-scope declaration of `%s' specifies `auto'", name); |
3d78f2e9 RH |
3870 | } |
3871 | else | |
3872 | { | |
3873 | if (specbits & 1 << (int) RID_EXTERN && funcdef_flag) | |
3874 | error ("nested function `%s' declared `extern'", name); | |
3875 | else if ((specbits & (1 << (int) RID_THREAD | |
3876 | | 1 << (int) RID_EXTERN | |
3877 | | 1 << (int) RID_STATIC)) | |
3878 | == (1 << (int) RID_THREAD)) | |
3879 | { | |
3880 | error ("function-scope `%s' implicitly auto and declared `__thread'", | |
3881 | name); | |
3882 | specbits &= ~(1 << (int) RID_THREAD); | |
3883 | } | |
3884 | } | |
51e29401 RS |
3885 | } |
3886 | ||
3887 | /* Now figure out the structure of the declarator proper. | |
3888 | Descend through it, creating more complex types, until we reach | |
3889 | the declared identifier (or NULL_TREE, in an absolute declarator). */ | |
3890 | ||
3891 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE) | |
3892 | { | |
3893 | if (type == error_mark_node) | |
3894 | { | |
3895 | declarator = TREE_OPERAND (declarator, 0); | |
3896 | continue; | |
3897 | } | |
3898 | ||
3899 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), | |
3900 | an INDIRECT_REF (for *...), | |
3901 | a CALL_EXPR (for ...(...)), | |
91d231cb | 3902 | a TREE_LIST (for nested attributes), |
51e29401 RS |
3903 | an identifier (for the name being declared) |
3904 | or a null pointer (for the place in an absolute declarator | |
3905 | where the name was omitted). | |
3906 | For the last two cases, we have just exited the loop. | |
3907 | ||
3908 | At this point, TYPE is the type of elements of an array, | |
3909 | or for a function to return, or for a pointer to point to. | |
3910 | After this sequence of ifs, TYPE is the type of the | |
3911 | array or function or pointer, and DECLARATOR has had its | |
3912 | outermost layer removed. */ | |
3913 | ||
0e03329a JM |
3914 | if (array_ptr_quals != NULL_TREE || array_parm_static) |
3915 | { | |
3916 | /* Only the innermost declarator (making a parameter be of | |
3917 | array type which is converted to pointer type) | |
3918 | may have static or type qualifiers. */ | |
3919 | error ("static or type qualifiers in non-parameter array declarator"); | |
3920 | array_ptr_quals = NULL_TREE; | |
3921 | array_parm_static = 0; | |
3922 | } | |
3923 | ||
91d231cb JM |
3924 | if (TREE_CODE (declarator) == TREE_LIST) |
3925 | { | |
3926 | /* We encode a declarator with embedded attributes using | |
3927 | a TREE_LIST. */ | |
3928 | tree attrs = TREE_PURPOSE (declarator); | |
3929 | tree inner_decl; | |
3930 | int attr_flags = 0; | |
3931 | declarator = TREE_VALUE (declarator); | |
3932 | inner_decl = declarator; | |
3933 | while (inner_decl != NULL_TREE | |
3934 | && TREE_CODE (inner_decl) == TREE_LIST) | |
3935 | inner_decl = TREE_VALUE (inner_decl); | |
3936 | if (inner_decl == NULL_TREE | |
3937 | || TREE_CODE (inner_decl) == IDENTIFIER_NODE) | |
3938 | attr_flags |= (int) ATTR_FLAG_DECL_NEXT; | |
ee96ce90 | 3939 | else if (TREE_CODE (inner_decl) == CALL_EXPR) |
91d231cb | 3940 | attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; |
ee96ce90 | 3941 | else if (TREE_CODE (inner_decl) == ARRAY_REF) |
91d231cb JM |
3942 | attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; |
3943 | returned_attrs = decl_attributes (&type, | |
3944 | chainon (returned_attrs, attrs), | |
3945 | attr_flags); | |
3946 | } | |
3947 | else if (TREE_CODE (declarator) == ARRAY_REF) | |
51e29401 | 3948 | { |
b3694847 SS |
3949 | tree itype = NULL_TREE; |
3950 | tree size = TREE_OPERAND (declarator, 1); | |
1ff1a2d2 | 3951 | /* The index is a signed object `sizetype' bits wide. */ |
ceef8ce4 | 3952 | tree index_type = c_common_signed_type (sizetype); |
51e29401 | 3953 | |
0e03329a JM |
3954 | array_ptr_quals = TREE_TYPE (declarator); |
3955 | array_parm_static = TREE_STATIC (declarator); | |
3956 | ||
51e29401 RS |
3957 | declarator = TREE_OPERAND (declarator, 0); |
3958 | ||
3959 | /* Check for some types that there cannot be arrays of. */ | |
3960 | ||
71653180 | 3961 | if (VOID_TYPE_P (type)) |
51e29401 RS |
3962 | { |
3963 | error ("declaration of `%s' as array of voids", name); | |
3964 | type = error_mark_node; | |
3965 | } | |
3966 | ||
3967 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
3968 | { | |
3969 | error ("declaration of `%s' as array of functions", name); | |
3970 | type = error_mark_node; | |
3971 | } | |
3972 | ||
ec3e6835 | 3973 | if (pedantic && !in_system_header && flexible_array_type_p (type)) |
2984fe64 JM |
3974 | pedwarn ("invalid use of structure with flexible array member"); |
3975 | ||
51e29401 RS |
3976 | if (size == error_mark_node) |
3977 | type = error_mark_node; | |
3978 | ||
3979 | if (type == error_mark_node) | |
3980 | continue; | |
3981 | ||
3982 | /* If size was specified, set ITYPE to a range-type for that size. | |
3983 | Otherwise, ITYPE remains null. finish_decl may figure it out | |
3984 | from an initial value. */ | |
3985 | ||
3986 | if (size) | |
3987 | { | |
3988 | /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */ | |
874a7be1 | 3989 | STRIP_TYPE_NOPS (size); |
51e29401 | 3990 | |
2c1a2421 | 3991 | if (! INTEGRAL_TYPE_P (TREE_TYPE (size))) |
51e29401 RS |
3992 | { |
3993 | error ("size of array `%s' has non-integer type", name); | |
3994 | size = integer_one_node; | |
3995 | } | |
0a43d680 | 3996 | |
51e29401 | 3997 | if (pedantic && integer_zerop (size)) |
89abf8d1 | 3998 | pedwarn ("ISO C forbids zero-size array `%s'", name); |
0a43d680 | 3999 | |
51e29401 RS |
4000 | if (TREE_CODE (size) == INTEGER_CST) |
4001 | { | |
90374cc2 | 4002 | constant_expression_warning (size); |
0a43d680 | 4003 | if (tree_int_cst_sgn (size) < 0) |
51e29401 RS |
4004 | { |
4005 | error ("size of array `%s' is negative", name); | |
4006 | size = integer_one_node; | |
4007 | } | |
51e29401 RS |
4008 | } |
4009 | else | |
4010 | { | |
0a43d680 RK |
4011 | /* Make sure the array size remains visibly nonconstant |
4012 | even if it is (eg) a const variable with known value. */ | |
4013 | size_varies = 1; | |
4014 | ||
2d1b5436 | 4015 | if (!flag_isoc99 && pedantic) |
1a6603da RS |
4016 | { |
4017 | if (TREE_CONSTANT (size)) | |
56508306 | 4018 | pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated", |
6645c3fa | 4019 | name); |
1a6603da | 4020 | else |
56508306 | 4021 | pedwarn ("ISO C90 forbids variable-size array `%s'", |
6645c3fa | 4022 | name); |
1a6603da | 4023 | } |
51e29401 | 4024 | } |
0a43d680 | 4025 | |
967e627a | 4026 | if (integer_zerop (size)) |
e9a25f70 | 4027 | { |
967e627a RH |
4028 | /* A zero-length array cannot be represented with an |
4029 | unsigned index type, which is what we'll get with | |
a25f1211 RH |
4030 | build_index_type. Create an open-ended range instead. */ |
4031 | itype = build_range_type (sizetype, size, NULL_TREE); | |
e9a25f70 | 4032 | } |
967e627a RH |
4033 | else |
4034 | { | |
4035 | /* Compute the maximum valid index, that is, size - 1. | |
4036 | Do the calculation in index_type, so that if it is | |
4037 | a variable the computations will be done in the | |
4038 | proper mode. */ | |
4039 | itype = fold (build (MINUS_EXPR, index_type, | |
4040 | convert (index_type, size), | |
4041 | convert (index_type, size_one_node))); | |
4042 | ||
4043 | /* If that overflowed, the array is too big. | |
4044 | ??? While a size of INT_MAX+1 technically shouldn't | |
4045 | cause an overflow (because we subtract 1), the overflow | |
4046 | is recorded during the conversion to index_type, before | |
4047 | the subtraction. Handling this case seems like an | |
4048 | unnecessary complication. */ | |
4049 | if (TREE_OVERFLOW (itype)) | |
4050 | { | |
4051 | error ("size of array `%s' is too large", name); | |
4052 | type = error_mark_node; | |
4053 | continue; | |
4054 | } | |
e9a25f70 | 4055 | |
967e627a | 4056 | if (size_varies) |
82c82743 | 4057 | itype = variable_size (itype); |
967e627a RH |
4058 | itype = build_index_type (itype); |
4059 | } | |
51e29401 | 4060 | } |
a25f1211 RH |
4061 | else if (decl_context == FIELD) |
4062 | { | |
a25f1211 | 4063 | if (pedantic && !flag_isoc99 && !in_system_header) |
56508306 | 4064 | pedwarn ("ISO C90 does not support flexible array members"); |
a25f1211 RH |
4065 | |
4066 | /* ISO C99 Flexible array members are effectively identical | |
4067 | to GCC's zero-length array extension. */ | |
4068 | itype = build_range_type (sizetype, size_zero_node, NULL_TREE); | |
4069 | } | |
51e29401 | 4070 | |
c7b82833 JM |
4071 | /* If pedantic, complain about arrays of incomplete types. */ |
4072 | ||
4073 | if (pedantic && !COMPLETE_TYPE_P (type)) | |
4074 | pedwarn ("array type has incomplete element type"); | |
51e29401 | 4075 | |
50e65854 JW |
4076 | /* Build the array type itself, then merge any constancy or |
4077 | volatility into the target type. We must do it in this order | |
4078 | to ensure that the TYPE_MAIN_VARIANT field of the array type | |
4079 | is set correctly. */ | |
4080 | ||
4081 | type = build_array_type (type, itype); | |
3932261a MM |
4082 | if (type_quals) |
4083 | type = c_build_qualified_type (type, type_quals); | |
51e29401 | 4084 | |
929f3671 | 4085 | if (size_varies) |
51e29401 | 4086 | C_TYPE_VARIABLE_SIZE (type) = 1; |
584ef5fe RH |
4087 | |
4088 | /* The GCC extension for zero-length arrays differs from | |
4089 | ISO flexible array members in that sizeof yields zero. */ | |
4090 | if (size && integer_zerop (size)) | |
4091 | { | |
4092 | layout_type (type); | |
4093 | TYPE_SIZE (type) = bitsize_zero_node; | |
4094 | TYPE_SIZE_UNIT (type) = size_zero_node; | |
4095 | } | |
6de9cd9a DN |
4096 | else if (declarator && TREE_CODE (declarator) == INDIRECT_REF) |
4097 | /* We can never complete an array type which is the target of a | |
4098 | pointer, so go ahead and lay it out. */ | |
4099 | layout_type (type); | |
4100 | ||
0e03329a JM |
4101 | if (decl_context != PARM |
4102 | && (array_ptr_quals != NULL_TREE || array_parm_static)) | |
4103 | { | |
4104 | error ("static or type qualifiers in non-parameter array declarator"); | |
4105 | array_ptr_quals = NULL_TREE; | |
4106 | array_parm_static = 0; | |
4107 | } | |
51e29401 RS |
4108 | } |
4109 | else if (TREE_CODE (declarator) == CALL_EXPR) | |
4110 | { | |
f75fbaf7 ZW |
4111 | /* Say it's a definition only for the CALL_EXPR closest to |
4112 | the identifier. */ | |
77dbdb57 ZW |
4113 | bool really_funcdef = (funcdef_flag |
4114 | && (TREE_CODE (TREE_OPERAND (declarator, 0)) | |
4115 | == IDENTIFIER_NODE)); | |
51e29401 RS |
4116 | tree arg_types; |
4117 | ||
f75fbaf7 ZW |
4118 | /* Declaring a function type. |
4119 | Make sure we have a valid type for the function to return. */ | |
51e29401 RS |
4120 | if (type == error_mark_node) |
4121 | continue; | |
4122 | ||
929f3671 | 4123 | size_varies = 0; |
51e29401 RS |
4124 | |
4125 | /* Warn about some types functions can't return. */ | |
4126 | ||
4127 | if (TREE_CODE (type) == FUNCTION_TYPE) | |
4128 | { | |
4129 | error ("`%s' declared as function returning a function", name); | |
4130 | type = integer_type_node; | |
4131 | } | |
4132 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4133 | { | |
4134 | error ("`%s' declared as function returning an array", name); | |
4135 | type = integer_type_node; | |
4136 | } | |
4137 | ||
51e29401 RS |
4138 | /* Construct the function type and go to the next |
4139 | inner layer of declarator. */ | |
77dbdb57 ZW |
4140 | arg_info = TREE_OPERAND (declarator, 1); |
4141 | arg_types = grokparms (arg_info, really_funcdef); | |
51e29401 | 4142 | |
3932261a MM |
4143 | /* Type qualifiers before the return type of the function |
4144 | qualify the return type, not the function type. */ | |
4145 | if (type_quals) | |
e0c9fbb7 JM |
4146 | { |
4147 | /* Type qualifiers on a function return type are normally | |
4148 | permitted by the standard but have no effect, so give a | |
65ca2d60 | 4149 | warning at -Wextra. Qualifiers on a void return type have |
e0c9fbb7 JM |
4150 | meaning as a GNU extension, and are banned on function |
4151 | definitions in ISO C. FIXME: strictly we shouldn't | |
4152 | pedwarn for qualified void return types except on function | |
4153 | definitions, but not doing so could lead to the undesirable | |
4154 | state of a "volatile void" function return type not being | |
4155 | warned about, and a use of the function being compiled | |
4156 | with GNU semantics, with no diagnostics under -pedantic. */ | |
4157 | if (VOID_TYPE_P (type) && pedantic && !in_system_header) | |
4158 | pedwarn ("ISO C forbids qualified void function return type"); | |
4159 | else if (extra_warnings | |
4160 | && !(VOID_TYPE_P (type) | |
4161 | && type_quals == TYPE_QUAL_VOLATILE)) | |
4162 | warning ("type qualifiers ignored on function return type"); | |
4163 | ||
4164 | type = c_build_qualified_type (type, type_quals); | |
4165 | } | |
3932261a | 4166 | type_quals = TYPE_UNQUALIFIED; |
61df2ee2 | 4167 | |
51e29401 RS |
4168 | type = build_function_type (type, arg_types); |
4169 | declarator = TREE_OPERAND (declarator, 0); | |
4170 | ||
4171 | /* Set the TYPE_CONTEXTs for each tagged type which is local to | |
4172 | the formal parameter list of this FUNCTION_TYPE to point to | |
4173 | the FUNCTION_TYPE node itself. */ | |
4174 | ||
4175 | { | |
b3694847 | 4176 | tree link; |
51e29401 | 4177 | |
77dbdb57 | 4178 | for (link = ARG_INFO_TAGS (arg_info); |
51e29401 RS |
4179 | link; |
4180 | link = TREE_CHAIN (link)) | |
4181 | TYPE_CONTEXT (TREE_VALUE (link)) = type; | |
4182 | } | |
4183 | } | |
4184 | else if (TREE_CODE (declarator) == INDIRECT_REF) | |
4185 | { | |
4186 | /* Merge any constancy or volatility into the target type | |
4187 | for the pointer. */ | |
4188 | ||
4189 | if (pedantic && TREE_CODE (type) == FUNCTION_TYPE | |
3932261a | 4190 | && type_quals) |
89abf8d1 | 4191 | pedwarn ("ISO C forbids qualified function types"); |
3932261a MM |
4192 | if (type_quals) |
4193 | type = c_build_qualified_type (type, type_quals); | |
4194 | type_quals = TYPE_UNQUALIFIED; | |
929f3671 | 4195 | size_varies = 0; |
51e29401 RS |
4196 | |
4197 | type = build_pointer_type (type); | |
4198 | ||
4199 | /* Process a list of type modifier keywords | |
4200 | (such as const or volatile) that were given inside the `*'. */ | |
4201 | ||
4202 | if (TREE_TYPE (declarator)) | |
4203 | { | |
b3694847 | 4204 | tree typemodlist; |
51e29401 | 4205 | int erred = 0; |
3932261a MM |
4206 | |
4207 | constp = 0; | |
4208 | volatilep = 0; | |
4209 | restrictp = 0; | |
51e29401 RS |
4210 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
4211 | typemodlist = TREE_CHAIN (typemodlist)) | |
4212 | { | |
3932261a MM |
4213 | tree qualifier = TREE_VALUE (typemodlist); |
4214 | ||
0e5921e8 | 4215 | if (C_IS_RESERVED_WORD (qualifier)) |
51e29401 | 4216 | { |
0e5921e8 ZW |
4217 | if (C_RID_CODE (qualifier) == RID_CONST) |
4218 | constp++; | |
4219 | else if (C_RID_CODE (qualifier) == RID_VOLATILE) | |
4220 | volatilep++; | |
4221 | else if (C_RID_CODE (qualifier) == RID_RESTRICT) | |
4222 | restrictp++; | |
4223 | else | |
4224 | erred++; | |
51e29401 | 4225 | } |
0e5921e8 ZW |
4226 | else |
4227 | erred++; | |
51e29401 | 4228 | } |
0e5921e8 ZW |
4229 | |
4230 | if (erred) | |
4231 | error ("invalid type modifier within pointer declarator"); | |
28fca7e4 RH |
4232 | if (pedantic && !flag_isoc99) |
4233 | { | |
4234 | if (constp > 1) | |
4235 | pedwarn ("duplicate `const'"); | |
4236 | if (volatilep > 1) | |
4237 | pedwarn ("duplicate `volatile'"); | |
4238 | if (restrictp > 1) | |
4239 | pedwarn ("duplicate `restrict'"); | |
4240 | } | |
3932261a MM |
4241 | |
4242 | type_quals = ((constp ? TYPE_QUAL_CONST : 0) | |
4243 | | (restrictp ? TYPE_QUAL_RESTRICT : 0) | |
4244 | | (volatilep ? TYPE_QUAL_VOLATILE : 0)); | |
51e29401 RS |
4245 | } |
4246 | ||
4247 | declarator = TREE_OPERAND (declarator, 0); | |
4248 | } | |
4249 | else | |
4250 | abort (); | |
4251 | ||
4252 | } | |
4253 | ||
4254 | /* Now TYPE has the actual type. */ | |
4255 | ||
e9a25f70 JL |
4256 | /* Did array size calculations overflow? */ |
4257 | ||
4258 | if (TREE_CODE (type) == ARRAY_TYPE | |
d0f062fb | 4259 | && COMPLETE_TYPE_P (type) |
e9a25f70 | 4260 | && TREE_OVERFLOW (TYPE_SIZE (type))) |
68940175 AO |
4261 | { |
4262 | error ("size of array `%s' is too large", name); | |
684d9f3b | 4263 | /* If we proceed with the array type as it is, we'll eventually |
68940175 AO |
4264 | crash in tree_low_cst(). */ |
4265 | type = error_mark_node; | |
4266 | } | |
e9a25f70 | 4267 | |
51e29401 RS |
4268 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
4269 | ||
4270 | if (specbits & (1 << (int) RID_TYPEDEF)) | |
4271 | { | |
4272 | tree decl; | |
4273 | /* Note that the grammar rejects storage classes | |
4274 | in typenames, fields or parameters */ | |
4275 | if (pedantic && TREE_CODE (type) == FUNCTION_TYPE | |
3932261a | 4276 | && type_quals) |
89abf8d1 | 4277 | pedwarn ("ISO C forbids qualified function types"); |
3932261a MM |
4278 | if (type_quals) |
4279 | type = c_build_qualified_type (type, type_quals); | |
51e29401 RS |
4280 | decl = build_decl (TYPE_DECL, declarator, type); |
4281 | if ((specbits & (1 << (int) RID_SIGNED)) | |
4282 | || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) | |
4283 | C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; | |
91d231cb | 4284 | decl_attributes (&decl, returned_attrs, 0); |
51e29401 RS |
4285 | return decl; |
4286 | } | |
4287 | ||
4288 | /* Detect the case of an array type of unspecified size | |
4289 | which came, as such, direct from a typedef name. | |
4290 | We must copy the type, so that each identifier gets | |
4291 | a distinct type, so that each identifier's size can be | |
4292 | controlled separately by its own initializer. */ | |
4293 | ||
4294 | if (type != 0 && typedef_type != 0 | |
a52fb89b RK |
4295 | && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0 |
4296 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) | |
51e29401 RS |
4297 | { |
4298 | type = build_array_type (TREE_TYPE (type), 0); | |
929f3671 | 4299 | if (size_varies) |
51e29401 RS |
4300 | C_TYPE_VARIABLE_SIZE (type) = 1; |
4301 | } | |
4302 | ||
4303 | /* If this is a type name (such as, in a cast or sizeof), | |
4304 | compute the type and return it now. */ | |
4305 | ||
4306 | if (decl_context == TYPENAME) | |
4307 | { | |
4308 | /* Note that the grammar rejects storage classes | |
4309 | in typenames, fields or parameters */ | |
4310 | if (pedantic && TREE_CODE (type) == FUNCTION_TYPE | |
3932261a | 4311 | && type_quals) |
89abf8d1 | 4312 | pedwarn ("ISO C forbids const or volatile function types"); |
3932261a MM |
4313 | if (type_quals) |
4314 | type = c_build_qualified_type (type, type_quals); | |
91d231cb | 4315 | decl_attributes (&type, returned_attrs, 0); |
51e29401 RS |
4316 | return type; |
4317 | } | |
4318 | ||
61df2ee2 RS |
4319 | /* Aside from typedefs and type names (handle above), |
4320 | `void' at top level (not within pointer) | |
4321 | is allowed only in public variables. | |
51e29401 RS |
4322 | We don't complain about parms either, but that is because |
4323 | a better error message can be made later. */ | |
4324 | ||
71653180 | 4325 | if (VOID_TYPE_P (type) && decl_context != PARM |
61df2ee2 RS |
4326 | && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE) |
4327 | && ((specbits & (1 << (int) RID_EXTERN)) | |
f75fbaf7 | 4328 | || (current_scope == file_scope |
61df2ee2 RS |
4329 | && !(specbits |
4330 | & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER))))))) | |
51e29401 | 4331 | { |
c40f7b33 | 4332 | error ("variable or field `%s' declared void", name); |
51e29401 RS |
4333 | type = integer_type_node; |
4334 | } | |
4335 | ||
4336 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL | |
4337 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ | |
4338 | ||
4339 | { | |
91d231cb | 4340 | tree decl; |
51e29401 RS |
4341 | |
4342 | if (decl_context == PARM) | |
4343 | { | |
0f38b811 | 4344 | tree type_as_written; |
c530479e | 4345 | tree promoted_type; |
51e29401 RS |
4346 | |
4347 | /* A parameter declared as an array of T is really a pointer to T. | |
4348 | One declared as a function is really a pointer to a function. */ | |
4349 | ||
4350 | if (TREE_CODE (type) == ARRAY_TYPE) | |
4351 | { | |
4352 | /* Transfer const-ness of array into that of type pointed to. */ | |
eaf2e788 | 4353 | type = TREE_TYPE (type); |
3932261a MM |
4354 | if (type_quals) |
4355 | type = c_build_qualified_type (type, type_quals); | |
eaf2e788 | 4356 | type = build_pointer_type (type); |
3932261a | 4357 | type_quals = TYPE_UNQUALIFIED; |
0e03329a JM |
4358 | if (array_ptr_quals) |
4359 | { | |
4360 | tree new_ptr_quals, new_ptr_attrs; | |
4361 | int erred = 0; | |
4362 | split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs); | |
4363 | /* We don't yet implement attributes in this context. */ | |
4364 | if (new_ptr_attrs != NULL_TREE) | |
4365 | warning ("attributes in parameter array declarator ignored"); | |
4366 | ||
4367 | constp = 0; | |
4368 | volatilep = 0; | |
4369 | restrictp = 0; | |
4370 | for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals)) | |
4371 | { | |
4372 | tree qualifier = TREE_VALUE (new_ptr_quals); | |
4373 | ||
4374 | if (C_IS_RESERVED_WORD (qualifier)) | |
4375 | { | |
4376 | if (C_RID_CODE (qualifier) == RID_CONST) | |
4377 | constp++; | |
4378 | else if (C_RID_CODE (qualifier) == RID_VOLATILE) | |
4379 | volatilep++; | |
4380 | else if (C_RID_CODE (qualifier) == RID_RESTRICT) | |
4381 | restrictp++; | |
4382 | else | |
4383 | erred++; | |
4384 | } | |
4385 | else | |
4386 | erred++; | |
4387 | } | |
4388 | ||
4389 | if (erred) | |
4390 | error ("invalid type modifier within array declarator"); | |
4391 | ||
4392 | type_quals = ((constp ? TYPE_QUAL_CONST : 0) | |
4393 | | (restrictp ? TYPE_QUAL_RESTRICT : 0) | |
4394 | | (volatilep ? TYPE_QUAL_VOLATILE : 0)); | |
4395 | } | |
929f3671 | 4396 | size_varies = 0; |
51e29401 RS |
4397 | } |
4398 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
4399 | { | |
3932261a | 4400 | if (pedantic && type_quals) |
89abf8d1 | 4401 | pedwarn ("ISO C forbids qualified function types"); |
3932261a MM |
4402 | if (type_quals) |
4403 | type = c_build_qualified_type (type, type_quals); | |
eaf2e788 | 4404 | type = build_pointer_type (type); |
3932261a | 4405 | type_quals = TYPE_UNQUALIFIED; |
51e29401 | 4406 | } |
0f38b811 MM |
4407 | else if (type_quals) |
4408 | type = c_build_qualified_type (type, type_quals); | |
35b1a6fa | 4409 | |
0f38b811 | 4410 | type_as_written = type; |
51e29401 | 4411 | |
51e29401 | 4412 | decl = build_decl (PARM_DECL, declarator, type); |
929f3671 | 4413 | if (size_varies) |
51e29401 RS |
4414 | C_DECL_VARIABLE_SIZE (decl) = 1; |
4415 | ||
4416 | /* Compute the type actually passed in the parmlist, | |
4417 | for the case where there is no prototype. | |
4418 | (For example, shorts and chars are passed as ints.) | |
4419 | When there is a prototype, this is overridden later. */ | |
4420 | ||
c530479e RH |
4421 | if (type == error_mark_node) |
4422 | promoted_type = type; | |
4423 | else | |
ab393bf1 | 4424 | promoted_type = c_type_promotes_to (type); |
51e29401 | 4425 | |
c530479e | 4426 | DECL_ARG_TYPE (decl) = promoted_type; |
51e29401 RS |
4427 | DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written; |
4428 | } | |
4429 | else if (decl_context == FIELD) | |
4430 | { | |
4431 | /* Structure field. It may not be a function. */ | |
dfd48d76 | 4432 | |
51e29401 RS |
4433 | if (TREE_CODE (type) == FUNCTION_TYPE) |
4434 | { | |
c40f7b33 | 4435 | error ("field `%s' declared as a function", name); |
51e29401 RS |
4436 | type = build_pointer_type (type); |
4437 | } | |
d0f062fb NS |
4438 | else if (TREE_CODE (type) != ERROR_MARK |
4439 | && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type)) | |
51e29401 | 4440 | { |
c40f7b33 | 4441 | error ("field `%s' has incomplete type", name); |
51e29401 RS |
4442 | type = error_mark_node; |
4443 | } | |
4444 | /* Move type qualifiers down to element of an array. */ | |
3932261a | 4445 | if (TREE_CODE (type) == ARRAY_TYPE && type_quals) |
14077d68 ZW |
4446 | type = build_array_type (c_build_qualified_type (TREE_TYPE (type), |
4447 | type_quals), | |
4448 | TYPE_DOMAIN (type)); | |
51e29401 | 4449 | decl = build_decl (FIELD_DECL, declarator, type); |
2bf105ab RK |
4450 | DECL_NONADDRESSABLE_P (decl) = bitfield; |
4451 | ||
929f3671 | 4452 | if (size_varies) |
51e29401 RS |
4453 | C_DECL_VARIABLE_SIZE (decl) = 1; |
4454 | } | |
4455 | else if (TREE_CODE (type) == FUNCTION_TYPE) | |
4456 | { | |
fd0b8fce JW |
4457 | /* Every function declaration is "external" |
4458 | except for those which are inside a function body | |
4459 | in which `auto' is used. | |
4460 | That is a case not specified by ANSI C, | |
4461 | and we use it for forward declarations for nested functions. */ | |
4462 | int extern_ref = (!(specbits & (1 << (int) RID_AUTO)) | |
f75fbaf7 | 4463 | || current_scope == file_scope); |
fd0b8fce | 4464 | |
51e29401 | 4465 | if (specbits & (1 << (int) RID_AUTO) |
f75fbaf7 | 4466 | && (pedantic || current_scope == file_scope)) |
c40f7b33 | 4467 | pedwarn ("invalid storage class for function `%s'", name); |
51e29401 | 4468 | if (specbits & (1 << (int) RID_REGISTER)) |
c40f7b33 | 4469 | error ("invalid storage class for function `%s'", name); |
3d78f2e9 RH |
4470 | if (specbits & (1 << (int) RID_THREAD)) |
4471 | error ("invalid storage class for function `%s'", name); | |
f8521984 | 4472 | /* Function declaration not at file scope. |
51e29401 RS |
4473 | Storage classes other than `extern' are not allowed |
4474 | and `extern' makes no difference. */ | |
f75fbaf7 | 4475 | if (current_scope != file_scope |
51e29401 RS |
4476 | && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE))) |
4477 | && pedantic) | |
c40f7b33 | 4478 | pedwarn ("invalid storage class for function `%s'", name); |
fd0b8fce | 4479 | |
51e29401 | 4480 | decl = build_decl (FUNCTION_DECL, declarator, type); |
91d231cb | 4481 | decl = build_decl_attribute_variant (decl, decl_attr); |
51e29401 | 4482 | |
703ad42b KG |
4483 | DECL_LANG_SPECIFIC (decl) |
4484 | = ggc_alloc_cleared (sizeof (struct lang_decl)); | |
31ed8fea | 4485 | |
3932261a | 4486 | if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl)) |
89abf8d1 | 4487 | pedwarn ("ISO C forbids qualified function types"); |
51e29401 | 4488 | |
3932261a MM |
4489 | /* GNU C interprets a `volatile void' return type to indicate |
4490 | that the function does not return. */ | |
4491 | if ((type_quals & TYPE_QUAL_VOLATILE) | |
71653180 | 4492 | && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl)))) |
11433f42 | 4493 | warning ("`noreturn' function returns non-void value"); |
61df2ee2 | 4494 | |
fd0b8fce | 4495 | if (extern_ref) |
1394aabd | 4496 | DECL_EXTERNAL (decl) = 1; |
51e29401 RS |
4497 | /* Record absence of global scope for `static' or `auto'. */ |
4498 | TREE_PUBLIC (decl) | |
4499 | = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO))); | |
c40f7b33 | 4500 | |
77dbdb57 ZW |
4501 | /* For a function definition, record the argument information |
4502 | block in DECL_ARGUMENTS where store_parm_decls will look | |
4503 | for it. */ | |
4504 | if (funcdef_flag) | |
4505 | DECL_ARGUMENTS (decl) = arg_info; | |
4506 | ||
5ce89b2e JM |
4507 | if (defaulted_int) |
4508 | C_FUNCTION_IMPLICIT_INT (decl) = 1; | |
4509 | ||
51e29401 | 4510 | /* Record presence of `inline', if it is reasonable. */ |
31ed8fea | 4511 | if (MAIN_NAME_P (declarator)) |
51e29401 | 4512 | { |
31ed8fea | 4513 | if (inlinep) |
51e29401 | 4514 | warning ("cannot inline function `main'"); |
31ed8fea RH |
4515 | } |
4516 | else if (inlinep) | |
4517 | { | |
b3c3af2f | 4518 | /* Record that the function is declared `inline'. */ |
31ed8fea | 4519 | DECL_DECLARED_INLINE_P (decl) = 1; |
51e29401 | 4520 | |
b79d5213 RH |
4521 | /* Do not mark bare declarations as DECL_INLINE. Doing so |
4522 | in the presence of multiple declarations can result in | |
4523 | the abstract origin pointing between the declarations, | |
4524 | which will confuse dwarf2out. */ | |
4525 | if (initialized) | |
4526 | { | |
4527 | DECL_INLINE (decl) = 1; | |
4528 | if (specbits & (1 << (int) RID_EXTERN)) | |
4529 | current_extern_inline = 1; | |
4530 | } | |
51e29401 | 4531 | } |
31ed8fea RH |
4532 | /* If -finline-functions, assume it can be inlined. This does |
4533 | two things: let the function be deferred until it is actually | |
4534 | needed, and let dwarf2 know that the function is inlinable. */ | |
b79d5213 | 4535 | else if (flag_inline_trees == 2 && initialized) |
b3c3af2f | 4536 | DECL_INLINE (decl) = 1; |
51e29401 RS |
4537 | } |
4538 | else | |
4539 | { | |
4540 | /* It's a variable. */ | |
fd0b8fce JW |
4541 | /* An uninitialized decl with `extern' is a reference. */ |
4542 | int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN)); | |
51e29401 RS |
4543 | |
4544 | /* Move type qualifiers down to element of an array. */ | |
3932261a | 4545 | if (TREE_CODE (type) == ARRAY_TYPE && type_quals) |
51e29401 | 4546 | { |
54d7f9aa | 4547 | int saved_align = TYPE_ALIGN(type); |
3932261a MM |
4548 | type = build_array_type (c_build_qualified_type (TREE_TYPE (type), |
4549 | type_quals), | |
51e29401 | 4550 | TYPE_DOMAIN (type)); |
54d7f9aa | 4551 | TYPE_ALIGN (type) = saved_align; |
51e29401 | 4552 | } |
0f38b811 MM |
4553 | else if (type_quals) |
4554 | type = c_build_qualified_type (type, type_quals); | |
f4e92987 | 4555 | |
f75fbaf7 ZW |
4556 | /* C99 6.2.2p7: It is invalid (compile-time undefined |
4557 | behavior) to create an 'extern' declaration for a | |
f4e92987 | 4558 | variable if there is a global declaration that is |
f75fbaf7 ZW |
4559 | 'static' and the global declaration is not visible. |
4560 | (If the static declaration _is_ currently visible, | |
4561 | the 'extern' declaration is taken to refer to that decl.) */ | |
4562 | if (extern_ref && current_scope != file_scope) | |
f4e92987 | 4563 | { |
f75fbaf7 ZW |
4564 | tree global_decl = identifier_global_value (declarator); |
4565 | tree visible_decl = lookup_name (declarator); | |
f4e92987 | 4566 | |
35b1a6fa | 4567 | if (global_decl |
f75fbaf7 | 4568 | && global_decl != visible_decl |
f4e92987 MM |
4569 | && TREE_CODE (global_decl) == VAR_DECL |
4570 | && !TREE_PUBLIC (global_decl)) | |
f75fbaf7 ZW |
4571 | error ("variable previously declared 'static' redeclared " |
4572 | "'extern'"); | |
f4e92987 MM |
4573 | } |
4574 | ||
51e29401 | 4575 | decl = build_decl (VAR_DECL, declarator, type); |
929f3671 | 4576 | if (size_varies) |
51e29401 RS |
4577 | C_DECL_VARIABLE_SIZE (decl) = 1; |
4578 | ||
4579 | if (inlinep) | |
ddd2d57e | 4580 | pedwarn ("%Jvariable '%D' declared `inline'", decl, decl); |
51e29401 | 4581 | |
fd0b8fce | 4582 | DECL_EXTERNAL (decl) = extern_ref; |
3d78f2e9 | 4583 | |
f8521984 | 4584 | /* At file scope, the presence of a `static' or `register' storage |
ee534ebf RS |
4585 | class specifier, or the absence of all storage class specifiers |
4586 | makes this declaration a definition (perhaps tentative). Also, | |
4587 | the absence of both `static' and `register' makes it public. */ | |
f75fbaf7 | 4588 | if (current_scope == file_scope) |
51e29401 | 4589 | { |
3d78f2e9 RH |
4590 | TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC) |
4591 | | (1 << (int) RID_REGISTER))); | |
4592 | TREE_STATIC (decl) = !extern_ref; | |
51e29401 | 4593 | } |
f8521984 | 4594 | /* Not at file scope, only `static' makes a static definition. */ |
51e29401 RS |
4595 | else |
4596 | { | |
4597 | TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0; | |
3d78f2e9 RH |
4598 | TREE_PUBLIC (decl) = extern_ref; |
4599 | } | |
4600 | ||
4601 | if (specbits & 1 << (int) RID_THREAD) | |
4602 | { | |
4603 | if (targetm.have_tls) | |
4604 | DECL_THREAD_LOCAL (decl) = 1; | |
4605 | else | |
4606 | /* A mere warning is sure to result in improper semantics | |
4607 | at runtime. Don't bother to allow this to compile. */ | |
4608 | error ("thread-local storage not supported for this target"); | |
51e29401 RS |
4609 | } |
4610 | } | |
4611 | ||
4612 | /* Record `register' declaration for warnings on & | |
4613 | and in case doing stupid register allocation. */ | |
4614 | ||
4615 | if (specbits & (1 << (int) RID_REGISTER)) | |
5baeaac0 JM |
4616 | { |
4617 | C_DECL_REGISTER (decl) = 1; | |
4618 | DECL_REGISTER (decl) = 1; | |
4619 | } | |
51e29401 RS |
4620 | |
4621 | /* Record constancy and volatility. */ | |
3932261a | 4622 | c_apply_type_quals_to_decl (type_quals, decl); |
51e29401 | 4623 | |
51e29401 RS |
4624 | /* If a type has volatile components, it should be stored in memory. |
4625 | Otherwise, the fact that those components are volatile | |
4626 | will be ignored, and would even crash the compiler. */ | |
4627 | if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))) | |
5baeaac0 JM |
4628 | { |
4629 | /* It is not an error for a structure with volatile fields to | |
4630 | be declared register, but reset DECL_REGISTER since it | |
4631 | cannot actually go in a register. */ | |
4632 | int was_reg = C_DECL_REGISTER (decl); | |
4633 | C_DECL_REGISTER (decl) = 0; | |
4634 | DECL_REGISTER (decl) = 0; | |
4635 | c_mark_addressable (decl); | |
4636 | C_DECL_REGISTER (decl) = was_reg; | |
4637 | } | |
51e29401 | 4638 | |
d05cc98e GK |
4639 | #ifdef ENABLE_CHECKING |
4640 | /* This is the earliest point at which we might know the assembler | |
4641 | name of a variable. Thus, if it's known before this, die horribly. */ | |
4642 | if (DECL_ASSEMBLER_NAME_SET_P (decl)) | |
4643 | abort (); | |
4644 | #endif | |
4645 | ||
91d231cb JM |
4646 | decl_attributes (&decl, returned_attrs, 0); |
4647 | ||
51e29401 RS |
4648 | return decl; |
4649 | } | |
4650 | } | |
4651 | \f | |
51e29401 RS |
4652 | /* Decode the parameter-list info for a function type or function definition. |
4653 | The argument is the value returned by `get_parm_info' (or made in parse.y | |
4654 | if there is an identifier list instead of a parameter decl list). | |
4655 | These two functions are separate because when a function returns | |
4656 | or receives functions then each is called multiple times but the order | |
4657 | of calls is different. The last call to `grokparms' is always the one | |
4658 | that contains the formal parameter names of a function definition. | |
4659 | ||
51e29401 RS |
4660 | Return a list of arg types to use in the FUNCTION_TYPE for this function. |
4661 | ||
4662 | FUNCDEF_FLAG is nonzero for a function definition, 0 for | |
4663 | a mere declaration. A nonempty identifier-list gets an error message | |
4664 | when FUNCDEF_FLAG is zero. */ | |
4665 | ||
4666 | static tree | |
77dbdb57 | 4667 | grokparms (tree arg_info, int funcdef_flag) |
51e29401 | 4668 | { |
77dbdb57 | 4669 | tree arg_types = ARG_INFO_TYPES (arg_info); |
51e29401 | 4670 | |
77dbdb57 | 4671 | if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag |
27f427f8 | 4672 | && !in_system_header) |
51e29401 RS |
4673 | warning ("function declaration isn't a prototype"); |
4674 | ||
f75fbaf7 ZW |
4675 | if (arg_types == error_mark_node) |
4676 | return 0; /* don't set TYPE_ARG_TYPES in this case */ | |
4677 | ||
4678 | else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE) | |
51e29401 RS |
4679 | { |
4680 | if (! funcdef_flag) | |
4681 | pedwarn ("parameter names (without types) in function declaration"); | |
4682 | ||
77dbdb57 ZW |
4683 | ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info); |
4684 | ARG_INFO_TYPES (arg_info) = 0; | |
51e29401 RS |
4685 | return 0; |
4686 | } | |
4687 | else | |
4688 | { | |
77dbdb57 ZW |
4689 | tree parm, type, typelt; |
4690 | unsigned int parmno; | |
4691 | ||
4692 | /* If the arg types are incomplete in a declaration, they must | |
4693 | include undefined tags. These tags can never be defined in | |
4694 | the scope of the declaration, so the types can never be | |
4695 | completed, and no call can be compiled successfully. */ | |
4696 | ||
4697 | for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1; | |
eb1dfbb2 | 4698 | parm; |
77dbdb57 ZW |
4699 | parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++) |
4700 | { | |
4701 | type = TREE_VALUE (typelt); | |
4702 | if (type == error_mark_node) | |
4703 | continue; | |
4704 | ||
4705 | if (!COMPLETE_TYPE_P (type)) | |
4706 | { | |
4707 | if (funcdef_flag) | |
4708 | { | |
4709 | if (DECL_NAME (parm)) | |
4710 | error ("%Jparameter %u ('%D') has incomplete type", | |
4711 | parm, parmno, parm); | |
4712 | else | |
4713 | error ("%Jparameter %u has incomplete type", | |
4714 | parm, parmno); | |
51e29401 | 4715 | |
77dbdb57 ZW |
4716 | TREE_VALUE (typelt) = error_mark_node; |
4717 | TREE_TYPE (parm) = error_mark_node; | |
4718 | } | |
4719 | else | |
4720 | { | |
4721 | if (DECL_NAME (parm)) | |
4722 | warning ("%Jparameter %u ('%D') has incomplete type", | |
4723 | parm, parmno, parm); | |
4724 | else | |
4725 | warning ("%Jparameter %u has incomplete type", | |
4726 | parm, parmno); | |
4727 | } | |
4728 | } | |
4729 | } | |
4730 | return arg_types; | |
51e29401 RS |
4731 | } |
4732 | } | |
4733 | ||
f75fbaf7 ZW |
4734 | /* Take apart the current scope and return a tree_list node with info |
4735 | on a parameter list just parsed. This tree_list node should be | |
4736 | examined using the ARG_INFO_* macros, defined above: | |
4737 | ||
77dbdb57 ZW |
4738 | ARG_INFO_PARMS: a list of parameter decls. |
4739 | ARG_INFO_TAGS: a list of structure, union and enum tags defined. | |
4740 | ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE. | |
4741 | ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration | |
f75fbaf7 | 4742 | constants) defined with the parameters. |
77dbdb57 ZW |
4743 | |
4744 | This tree_list node is later fed to 'grokparms' and 'store_parm_decls'. | |
51e29401 | 4745 | |
f75fbaf7 ZW |
4746 | ELLIPSIS being true means the argument list ended in '...' so don't |
4747 | append a sentinel (void_list_node) to the end of the type-list. */ | |
51e29401 RS |
4748 | |
4749 | tree | |
f75fbaf7 | 4750 | get_parm_info (bool ellipsis) |
51e29401 | 4751 | { |
f75fbaf7 ZW |
4752 | struct c_binding *b = current_scope->bindings; |
4753 | tree arg_info = make_node (TREE_LIST); | |
4754 | tree parms = 0; | |
4755 | tree tags = 0; | |
4756 | tree types = 0; | |
4757 | tree others = 0; | |
4758 | ||
55d54003 ZW |
4759 | static bool explained_incomplete_types = false; |
4760 | bool gave_void_only_once_err = false; | |
4761 | ||
f75fbaf7 ZW |
4762 | /* The bindings in this scope must not get put into a block. |
4763 | We will take care of deleting the binding nodes. */ | |
4764 | current_scope->bindings = 0; | |
51e29401 | 4765 | |
f75fbaf7 ZW |
4766 | /* This function is only called if there was *something* on the |
4767 | parameter list. */ | |
77dbdb57 | 4768 | #ifdef ENABLE_CHECKING |
f75fbaf7 ZW |
4769 | if (b == 0) |
4770 | abort (); | |
77dbdb57 | 4771 | #endif |
55d54003 | 4772 | |
f75fbaf7 ZW |
4773 | /* A parameter list consisting solely of 'void' indicates that the |
4774 | function takes no arguments. But if the 'void' is qualified | |
4775 | (by 'const' or 'volatile'), or has a storage class specifier | |
4776 | ('register'), then the behavior is undefined; issue an error. | |
4777 | Typedefs for 'void' are OK (see DR#157). */ | |
4778 | if (b->prev == 0 /* one binding */ | |
4779 | && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */ | |
4780 | && !DECL_NAME (b->decl) /* anonymous */ | |
4781 | && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */ | |
4782 | { | |
4783 | if (TREE_THIS_VOLATILE (b->decl) | |
4784 | || TREE_READONLY (b->decl) | |
5baeaac0 | 4785 | || C_DECL_REGISTER (b->decl)) |
f75fbaf7 | 4786 | error ("'void' as only parameter may not be qualified"); |
55d54003 | 4787 | |
f75fbaf7 ZW |
4788 | /* There cannot be an ellipsis. */ |
4789 | if (ellipsis) | |
4790 | error ("'void' must be the only parameter"); | |
51e29401 | 4791 | |
f75fbaf7 ZW |
4792 | ARG_INFO_TYPES (arg_info) = void_list_node; |
4793 | return arg_info; | |
51e29401 RS |
4794 | } |
4795 | ||
f75fbaf7 ZW |
4796 | if (!ellipsis) |
4797 | types = void_list_node; | |
51e29401 | 4798 | |
f75fbaf7 ZW |
4799 | /* Break up the bindings list into parms, tags, types, and others; |
4800 | apply sanity checks; purge the name-to-decl bindings. */ | |
4801 | while (b) | |
51e29401 | 4802 | { |
f75fbaf7 ZW |
4803 | tree decl = b->decl; |
4804 | tree type = TREE_TYPE (decl); | |
55d54003 | 4805 | const char *keyword; |
55d54003 | 4806 | |
f75fbaf7 | 4807 | switch (TREE_CODE (decl)) |
6645c3fa | 4808 | { |
f75fbaf7 ZW |
4809 | case PARM_DECL: |
4810 | if (b->id) | |
4811 | { | |
4812 | #ifdef ENABLE_CHECKING | |
4813 | if (I_SYMBOL_BINDING (b->id) != b) abort (); | |
4814 | #endif | |
4815 | I_SYMBOL_BINDING (b->id) = b->shadowed; | |
4816 | } | |
55d54003 | 4817 | |
f75fbaf7 ZW |
4818 | /* Check for forward decls that never got their actual decl. */ |
4819 | if (TREE_ASM_WRITTEN (decl)) | |
4820 | error ("%Jparameter '%D' has just a forward declaration", | |
4821 | decl, decl); | |
4822 | /* Check for (..., void, ...) and issue an error. */ | |
4823 | else if (VOID_TYPE_P (type) && !DECL_NAME (decl)) | |
4824 | { | |
4825 | if (!gave_void_only_once_err) | |
4826 | { | |
4827 | error ("'void' must be the only parameter"); | |
4828 | gave_void_only_once_err = true; | |
4829 | } | |
4830 | } | |
4831 | else | |
4832 | { | |
4833 | /* Valid parameter, add it to the list. */ | |
4834 | TREE_CHAIN (decl) = parms; | |
4835 | parms = decl; | |
4836 | ||
4837 | /* Since there is a prototype, args are passed in their | |
4838 | declared types. The back end may override this later. */ | |
4839 | DECL_ARG_TYPE (decl) = type; | |
4840 | types = tree_cons (0, type, types); | |
4841 | } | |
4842 | break; | |
55d54003 | 4843 | |
56141a6a | 4844 | case ENUMERAL_TYPE: keyword = "enum"; goto tag; |
f75fbaf7 | 4845 | case UNION_TYPE: keyword = "union"; goto tag; |
56141a6a | 4846 | case RECORD_TYPE: keyword = "struct"; goto tag; |
f75fbaf7 ZW |
4847 | tag: |
4848 | /* Types may not have tag-names, in which case the type | |
4849 | appears in the bindings list with b->id NULL. */ | |
4850 | if (b->id) | |
4851 | { | |
4852 | #ifdef ENABLE_CHECKING | |
4853 | if (I_TAG_BINDING (b->id) != b) abort (); | |
4854 | #endif | |
4855 | I_TAG_BINDING (b->id) = b->shadowed; | |
4856 | } | |
55d54003 | 4857 | |
f75fbaf7 ZW |
4858 | /* Warn about any struct, union or enum tags defined in a |
4859 | parameter list. The scope of such types is limited to | |
4860 | the parameter list, which is rarely if ever desirable | |
4861 | (it's impossible to call such a function with type- | |
4862 | correct arguments). An anonymous union parm type is | |
4863 | meaningful as a GNU extension, so don't warn for that. */ | |
4864 | if (TREE_CODE (decl) != UNION_TYPE || b->id != 0) | |
4865 | { | |
4866 | if (b->id) | |
4867 | /* The %s will be one of 'struct', 'union', or 'enum'. */ | |
4868 | warning ("'%s %E' declared inside parameter list", | |
4869 | keyword, b->id); | |
4870 | else | |
4871 | /* The %s will be one of 'struct', 'union', or 'enum'. */ | |
4872 | warning ("anonymous %s declared inside parameter list", | |
4873 | keyword); | |
118a3a8b | 4874 | |
f75fbaf7 ZW |
4875 | if (! explained_incomplete_types) |
4876 | { | |
4877 | warning ("its scope is only this definition or declaration," | |
4878 | " which is probably not what you want"); | |
4879 | explained_incomplete_types = true; | |
4880 | } | |
4881 | } | |
55d54003 | 4882 | |
f75fbaf7 ZW |
4883 | tags = tree_cons (b->id, decl, tags); |
4884 | break; | |
4885 | ||
4886 | case CONST_DECL: | |
4887 | case TYPE_DECL: | |
4888 | /* CONST_DECLs appear here when we have an embedded enum, | |
4889 | and TYPE_DECLs appear here when we have an embedded struct | |
4890 | or union. No warnings for this - we already warned about the | |
4891 | type itself. */ | |
18d5f982 ZW |
4892 | TREE_CHAIN (decl) = others; |
4893 | others = decl; | |
4894 | /* fall through */ | |
4895 | ||
4896 | case ERROR_MARK: | |
4897 | /* error_mark_node appears here when we have an undeclared | |
4898 | variable. Just throw it away. */ | |
f75fbaf7 ZW |
4899 | if (b->id) |
4900 | { | |
4901 | #ifdef ENABLE_CHECKING | |
4902 | if (I_SYMBOL_BINDING (b->id) != b) abort (); | |
4903 | #endif | |
4904 | I_SYMBOL_BINDING (b->id) = b->shadowed; | |
4905 | } | |
f75fbaf7 ZW |
4906 | break; |
4907 | ||
4908 | /* Other things that might be encountered. */ | |
4909 | case LABEL_DECL: | |
4910 | case FUNCTION_DECL: | |
4911 | case VAR_DECL: | |
f75fbaf7 ZW |
4912 | default: |
4913 | abort (); | |
4914 | } | |
4915 | ||
4916 | b = free_binding_and_advance (b); | |
55d54003 ZW |
4917 | } |
4918 | ||
f75fbaf7 ZW |
4919 | ARG_INFO_PARMS (arg_info) = parms; |
4920 | ARG_INFO_TAGS (arg_info) = tags; | |
4921 | ARG_INFO_TYPES (arg_info) = types; | |
4922 | ARG_INFO_OTHERS (arg_info) = others; | |
4923 | return arg_info; | |
51e29401 RS |
4924 | } |
4925 | \f | |
4926 | /* Get the struct, enum or union (CODE says which) with tag NAME. | |
4927 | Define the tag as a forward-reference if it is not defined. */ | |
4928 | ||
4929 | tree | |
35b1a6fa | 4930 | xref_tag (enum tree_code code, tree name) |
51e29401 | 4931 | { |
51e29401 RS |
4932 | /* If a cross reference is requested, look up the type |
4933 | already defined for this tag and return it. */ | |
4934 | ||
339a28b9 | 4935 | tree ref = lookup_tag (code, name, 0); |
f18b70f5 JM |
4936 | /* If this is the right type of tag, return what we found. |
4937 | (This reference will be shadowed by shadow_tag later if appropriate.) | |
4938 | If this is the wrong type of tag, do not return it. If it was the | |
f8521984 ZW |
4939 | wrong type in the same scope, we will have had an error |
4940 | message already; if in a different scope and declaring | |
f18b70f5 | 4941 | a name, pending_xref_error will give an error message; but if in a |
f8521984 | 4942 | different scope and not declaring a name, this tag should |
f18b70f5 JM |
4943 | shadow the previous declaration of a different type of tag, and |
4944 | this would not work properly if we return the reference found. | |
4945 | (For example, with "struct foo" in an outer scope, "union foo;" | |
4946 | must shadow that tag with a new one of union type.) */ | |
4947 | if (ref && TREE_CODE (ref) == code) | |
51e29401 RS |
4948 | return ref; |
4949 | ||
51e29401 RS |
4950 | /* If no such tag is yet defined, create a forward-reference node |
4951 | and record it as the "definition". | |
4952 | When a real declaration of this type is found, | |
4953 | the forward-reference will be altered into a real type. */ | |
4954 | ||
4955 | ref = make_node (code); | |
4956 | if (code == ENUMERAL_TYPE) | |
4957 | { | |
51e29401 RS |
4958 | /* Give the type a default layout like unsigned int |
4959 | to avoid crashing if it does not get defined. */ | |
4960 | TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node); | |
4961 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); | |
11cf4d18 | 4962 | TYPE_USER_ALIGN (ref) = 0; |
8df83eae | 4963 | TYPE_UNSIGNED (ref) = 1; |
51e29401 RS |
4964 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); |
4965 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); | |
4966 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); | |
4967 | } | |
4968 | ||
4969 | pushtag (name, ref); | |
4970 | ||
51e29401 RS |
4971 | return ref; |
4972 | } | |
4973 | \f | |
f8521984 | 4974 | /* Make sure that the tag NAME is defined *in the current scope* |
51e29401 | 4975 | at least as a forward reference. |
4dd7201e | 4976 | CODE says which kind of tag NAME ought to be. */ |
51e29401 RS |
4977 | |
4978 | tree | |
35b1a6fa | 4979 | start_struct (enum tree_code code, tree name) |
51e29401 | 4980 | { |
f8521984 | 4981 | /* If there is already a tag defined at this scope |
51e29401 RS |
4982 | (as a forward reference), just return it. */ |
4983 | ||
b3694847 | 4984 | tree ref = 0; |
51e29401 RS |
4985 | |
4986 | if (name != 0) | |
339a28b9 | 4987 | ref = lookup_tag (code, name, 1); |
51e29401 RS |
4988 | if (ref && TREE_CODE (ref) == code) |
4989 | { | |
51e29401 | 4990 | if (TYPE_FIELDS (ref)) |
53fcdc76 PB |
4991 | { |
4992 | if (code == UNION_TYPE) | |
5326cd3d | 4993 | error ("redefinition of `union %s'", IDENTIFIER_POINTER (name)); |
53fcdc76 | 4994 | else |
5326cd3d | 4995 | error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name)); |
35b1a6fa | 4996 | } |
51e29401 | 4997 | } |
5326cd3d NS |
4998 | else |
4999 | { | |
5000 | /* Otherwise create a forward-reference just so the tag is in scope. */ | |
51e29401 | 5001 | |
5326cd3d NS |
5002 | ref = make_node (code); |
5003 | pushtag (name, ref); | |
5004 | } | |
35b1a6fa | 5005 | |
51e29401 | 5006 | C_TYPE_BEING_DEFINED (ref) = 1; |
02eb6e90 | 5007 | TYPE_PACKED (ref) = flag_pack_struct; |
51e29401 RS |
5008 | return ref; |
5009 | } | |
5010 | ||
5011 | /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted) | |
5012 | of a structure component, returning a FIELD_DECL node. | |
2ff7cce4 | 5013 | WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node. |
51e29401 RS |
5014 | |
5015 | This is done during the parsing of the struct declaration. | |
5016 | The FIELD_DECL nodes are chained together and the lot of them | |
5017 | are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ | |
5018 | ||
5019 | tree | |
bc4721b8 | 5020 | grokfield (tree declarator, tree declspecs, tree width) |
51e29401 RS |
5021 | { |
5022 | tree value; | |
5023 | ||
3e96a2fd DD |
5024 | if (declarator == NULL_TREE && width == NULL_TREE) |
5025 | { | |
750491fc RH |
5026 | /* This is an unnamed decl. |
5027 | ||
5028 | If we have something of the form "union { list } ;" then this | |
5029 | is the anonymous union extension. Similarly for struct. | |
5030 | ||
5031 | If this is something of the form "struct foo;", then | |
5032 | If MS extensions are enabled, this is handled as an | |
5033 | anonymous struct. | |
5034 | Otherwise this is a forward declaration of a structure tag. | |
5035 | ||
5036 | If this is something of the form "foo;" and foo is a TYPE_DECL, then | |
5037 | If MS extensions are enabled and foo names a structure, then | |
5038 | again this is an anonymous struct. | |
5039 | Otherwise this is an error. | |
5040 | ||
95bd1dd7 | 5041 | Oh what a horrid tangled web we weave. I wonder if MS consciously |
750491fc RH |
5042 | took this from Plan 9 or if it was an accident of implementation |
5043 | that took root before someone noticed the bug... */ | |
5044 | ||
85d49058 JJ |
5045 | tree type = TREE_VALUE (declspecs); |
5046 | ||
750491fc | 5047 | if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL) |
85d49058 | 5048 | type = TREE_TYPE (type); |
750491fc RH |
5049 | if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE) |
5050 | { | |
5051 | if (flag_ms_extensions) | |
5052 | ; /* ok */ | |
5053 | else if (flag_iso) | |
5054 | goto warn_unnamed_field; | |
5055 | else if (TYPE_NAME (type) == NULL) | |
5056 | ; /* ok */ | |
5057 | else | |
5058 | goto warn_unnamed_field; | |
5059 | } | |
5060 | else | |
3e96a2fd | 5061 | { |
750491fc RH |
5062 | warn_unnamed_field: |
5063 | warning ("declaration does not declare anything"); | |
3e96a2fd DD |
5064 | return NULL_TREE; |
5065 | } | |
5066 | } | |
5067 | ||
2ff7cce4 JM |
5068 | value = grokdeclarator (declarator, declspecs, FIELD, 0, |
5069 | width ? &width : NULL); | |
51e29401 | 5070 | |
8d9bfdc5 | 5071 | finish_decl (value, NULL_TREE, NULL_TREE); |
dfd48d76 | 5072 | DECL_INITIAL (value) = width; |
51e29401 RS |
5073 | |
5074 | return value; | |
5075 | } | |
5076 | \f | |
66ea6f4c RH |
5077 | /* Generate an error for any duplicate field names in FIELDLIST. Munge |
5078 | the list such that this does not present a problem later. */ | |
5079 | ||
5080 | static void | |
5081 | detect_field_duplicates (tree fieldlist) | |
5082 | { | |
5083 | tree x, y; | |
5084 | int timeout = 10; | |
5085 | ||
5086 | /* First, see if there are more than "a few" fields. | |
5087 | This is trivially true if there are zero or one fields. */ | |
5088 | if (!fieldlist) | |
5089 | return; | |
5090 | x = TREE_CHAIN (fieldlist); | |
5091 | if (!x) | |
5092 | return; | |
5093 | do { | |
5094 | timeout--; | |
5095 | x = TREE_CHAIN (x); | |
5096 | } while (timeout > 0 && x); | |
5097 | ||
5098 | /* If there were "few" fields, avoid the overhead of allocating | |
5099 | a hash table. Instead just do the nested traversal thing. */ | |
5100 | if (timeout > 0) | |
5101 | { | |
5102 | for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x)) | |
5103 | if (DECL_NAME (x)) | |
5104 | { | |
5105 | for (y = fieldlist; y != x; y = TREE_CHAIN (y)) | |
5106 | if (DECL_NAME (y) == DECL_NAME (x)) | |
5107 | { | |
ddd2d57e | 5108 | error ("%Jduplicate member '%D'", x, x); |
66ea6f4c RH |
5109 | DECL_NAME (x) = NULL_TREE; |
5110 | } | |
5111 | } | |
5112 | } | |
5113 | else | |
5114 | { | |
5115 | htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL); | |
5116 | void **slot; | |
5117 | ||
5118 | for (x = fieldlist; x ; x = TREE_CHAIN (x)) | |
5119 | if ((y = DECL_NAME (x)) != 0) | |
5120 | { | |
5121 | slot = htab_find_slot (htab, y, INSERT); | |
5122 | if (*slot) | |
5123 | { | |
ddd2d57e | 5124 | error ("%Jduplicate member '%D'", x, x); |
66ea6f4c RH |
5125 | DECL_NAME (x) = NULL_TREE; |
5126 | } | |
5127 | *slot = y; | |
5128 | } | |
5129 | ||
5130 | htab_delete (htab); | |
5131 | } | |
5132 | } | |
5133 | ||
51e29401 | 5134 | /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. |
7a0347ff | 5135 | FIELDLIST is a chain of FIELD_DECL nodes for the fields. |
4dd7201e | 5136 | ATTRIBUTES are attributes to be applied to the structure. */ |
51e29401 RS |
5137 | |
5138 | tree | |
35b1a6fa | 5139 | finish_struct (tree t, tree fieldlist, tree attributes) |
51e29401 | 5140 | { |
b3694847 | 5141 | tree x; |
f75fbaf7 | 5142 | bool toplevel = file_scope == current_scope; |
ffc5c6a9 | 5143 | int saw_named_field; |
51e29401 RS |
5144 | |
5145 | /* If this type was previously laid out as a forward reference, | |
5146 | make sure we lay it out again. */ | |
5147 | ||
5148 | TYPE_SIZE (t) = 0; | |
5149 | ||
91d231cb | 5150 | decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); |
10861e9a | 5151 | |
9590fa72 RK |
5152 | if (pedantic) |
5153 | { | |
5154 | for (x = fieldlist; x; x = TREE_CHAIN (x)) | |
5155 | if (DECL_NAME (x) != 0) | |
5156 | break; | |
5157 | ||
5158 | if (x == 0) | |
5e4adfba PT |
5159 | pedwarn ("%s has no %s", |
5160 | TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"), | |
5161 | fieldlist ? _("named members") : _("members")); | |
9590fa72 | 5162 | } |
51e29401 | 5163 | |
dfd48d76 NB |
5164 | /* Install struct as DECL_CONTEXT of each field decl. |
5165 | Also process specified field sizes,m which is found in the DECL_INITIAL. | |
5166 | Store 0 there, except for ": 0" fields (so we can find them | |
5167 | and delete them, below). */ | |
51e29401 | 5168 | |
ffc5c6a9 | 5169 | saw_named_field = 0; |
51e29401 RS |
5170 | for (x = fieldlist; x; x = TREE_CHAIN (x)) |
5171 | { | |
5172 | DECL_CONTEXT (x) = t; | |
5be1df77 | 5173 | DECL_PACKED (x) |= TYPE_PACKED (t); |
51e29401 RS |
5174 | |
5175 | /* If any field is const, the structure type is pseudo-const. */ | |
5176 | if (TREE_READONLY (x)) | |
5177 | C_TYPE_FIELDS_READONLY (t) = 1; | |
5178 | else | |
5179 | { | |
5180 | /* A field that is pseudo-const makes the structure likewise. */ | |
5181 | tree t1 = TREE_TYPE (x); | |
5182 | while (TREE_CODE (t1) == ARRAY_TYPE) | |
5183 | t1 = TREE_TYPE (t1); | |
5184 | if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) | |
5185 | && C_TYPE_FIELDS_READONLY (t1)) | |
5186 | C_TYPE_FIELDS_READONLY (t) = 1; | |
5187 | } | |
5188 | ||
5189 | /* Any field that is volatile means variables of this type must be | |
5190 | treated in some ways as volatile. */ | |
5191 | if (TREE_THIS_VOLATILE (x)) | |
5192 | C_TYPE_FIELDS_VOLATILE (t) = 1; | |
5193 | ||
5194 | /* Any field of nominal variable size implies structure is too. */ | |
5195 | if (C_DECL_VARIABLE_SIZE (x)) | |
5196 | C_TYPE_VARIABLE_SIZE (t) = 1; | |
5197 | ||
8d7bbe5f RS |
5198 | /* Detect invalid nested redefinition. */ |
5199 | if (TREE_TYPE (x) == t) | |
5200 | error ("nested redefinition of `%s'", | |
5201 | IDENTIFIER_POINTER (TYPE_NAME (t))); | |
5202 | ||
dfd48d76 NB |
5203 | if (DECL_INITIAL (x)) |
5204 | { | |
2ff7cce4 JM |
5205 | unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1); |
5206 | DECL_SIZE (x) = bitsize_int (width); | |
5207 | DECL_BIT_FIELD (x) = 1; | |
5208 | SET_DECL_C_BIT_FIELD (x); | |
dfd48d76 NB |
5209 | } |
5210 | ||
dfd48d76 NB |
5211 | DECL_INITIAL (x) = 0; |
5212 | ||
ffc5c6a9 RH |
5213 | /* Detect flexible array member in an invalid context. */ |
5214 | if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE | |
5215 | && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE | |
5216 | && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE | |
5217 | && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE) | |
5218 | { | |
5219 | if (TREE_CODE (t) == UNION_TYPE) | |
d915eec0 JJ |
5220 | { |
5221 | error ("%Jflexible array member in union", x); | |
5222 | TREE_TYPE (x) = error_mark_node; | |
5223 | } | |
ffc5c6a9 | 5224 | else if (TREE_CHAIN (x) != NULL_TREE) |
d915eec0 JJ |
5225 | { |
5226 | error ("%Jflexible array member not at end of struct", x); | |
5227 | TREE_TYPE (x) = error_mark_node; | |
5228 | } | |
ffc5c6a9 | 5229 | else if (! saw_named_field) |
d915eec0 JJ |
5230 | { |
5231 | error ("%Jflexible array member in otherwise empty struct", x); | |
5232 | TREE_TYPE (x) = error_mark_node; | |
5233 | } | |
ffc5c6a9 | 5234 | } |
2984fe64 | 5235 | |
ec3e6835 | 5236 | if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE |
2984fe64 | 5237 | && flexible_array_type_p (TREE_TYPE (x))) |
ddd2d57e | 5238 | pedwarn ("%Jinvalid use of structure with flexible array member", x); |
2984fe64 | 5239 | |
ffc5c6a9 RH |
5240 | if (DECL_NAME (x)) |
5241 | saw_named_field = 1; | |
05bccae2 | 5242 | } |
51e29401 | 5243 | |
66ea6f4c | 5244 | detect_field_duplicates (fieldlist); |
51e29401 RS |
5245 | |
5246 | /* Now we have the nearly final fieldlist. Record it, | |
5247 | then lay out the structure or union (including the fields). */ | |
5248 | ||
5249 | TYPE_FIELDS (t) = fieldlist; | |
5250 | ||
5251 | layout_type (t); | |
5252 | ||
6614fd40 | 5253 | /* Delete all zero-width bit-fields from the fieldlist. */ |
6fbfac92 JM |
5254 | { |
5255 | tree *fieldlistp = &fieldlist; | |
07b983cd JM |
5256 | while (*fieldlistp) |
5257 | if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)) | |
6fbfac92 JM |
5258 | *fieldlistp = TREE_CHAIN (*fieldlistp); |
5259 | else | |
5260 | fieldlistp = &TREE_CHAIN (*fieldlistp); | |
5261 | } | |
51e29401 | 5262 | |
ffc5c6a9 RH |
5263 | /* Now we have the truly final field list. |
5264 | Store it in this type and in the variants. */ | |
51e29401 RS |
5265 | |
5266 | TYPE_FIELDS (t) = fieldlist; | |
5267 | ||
d07605f5 | 5268 | /* If there are lots of fields, sort so we can look through them fast. |
6614fd40 | 5269 | We arbitrarily consider 16 or more elts to be "a lot". */ |
d07605f5 AP |
5270 | |
5271 | { | |
5272 | int len = 0; | |
5273 | ||
5274 | for (x = fieldlist; x; x = TREE_CHAIN (x)) | |
5275 | { | |
5276 | if (len > 15 || DECL_NAME (x) == NULL) | |
5277 | break; | |
5278 | len += 1; | |
5279 | } | |
5280 | ||
5281 | if (len > 15) | |
5282 | { | |
5283 | tree *field_array; | |
5284 | struct lang_type *space; | |
5285 | struct sorted_fields_type *space2; | |
e13e48e7 | 5286 | |
d07605f5 | 5287 | len += list_length (x); |
e13e48e7 | 5288 | |
d07605f5 AP |
5289 | /* Use the same allocation policy here that make_node uses, to |
5290 | ensure that this lives as long as the rest of the struct decl. | |
5291 | All decls in an inline function need to be saved. */ | |
e13e48e7 | 5292 | |
6de9cd9a | 5293 | space = ggc_alloc_cleared (sizeof (struct lang_type)); |
d07605f5 | 5294 | space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree)); |
e13e48e7 | 5295 | |
d07605f5 AP |
5296 | len = 0; |
5297 | space->s = space2; | |
5298 | field_array = &space2->elts[0]; | |
5299 | for (x = fieldlist; x; x = TREE_CHAIN (x)) | |
5300 | { | |
5301 | field_array[len++] = x; | |
e13e48e7 | 5302 | |
938d968e | 5303 | /* If there is anonymous struct or union, break out of the loop. */ |
d07605f5 AP |
5304 | if (DECL_NAME (x) == NULL) |
5305 | break; | |
5306 | } | |
938d968e | 5307 | /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */ |
d07605f5 AP |
5308 | if (x == NULL) |
5309 | { | |
5310 | TYPE_LANG_SPECIFIC (t) = space; | |
5311 | TYPE_LANG_SPECIFIC (t)->s->len = len; | |
5312 | field_array = TYPE_LANG_SPECIFIC (t)->s->elts; | |
5313 | qsort (field_array, len, sizeof (tree), field_decl_cmp); | |
5314 | } | |
5315 | } | |
5316 | } | |
e13e48e7 | 5317 | |
51e29401 RS |
5318 | for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) |
5319 | { | |
5320 | TYPE_FIELDS (x) = TYPE_FIELDS (t); | |
5321 | TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t); | |
5322 | TYPE_ALIGN (x) = TYPE_ALIGN (t); | |
11cf4d18 | 5323 | TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t); |
51e29401 RS |
5324 | } |
5325 | ||
1604422c RK |
5326 | /* If this was supposed to be a transparent union, but we can't |
5327 | make it one, warn and turn off the flag. */ | |
5328 | if (TREE_CODE (t) == UNION_TYPE | |
5329 | && TYPE_TRANSPARENT_UNION (t) | |
5330 | && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))) | |
5331 | { | |
5332 | TYPE_TRANSPARENT_UNION (t) = 0; | |
d787aad9 | 5333 | warning ("union cannot be made transparent"); |
1604422c RK |
5334 | } |
5335 | ||
51e29401 RS |
5336 | /* If this structure or union completes the type of any previous |
5337 | variable declaration, lay it out and output its rtl. */ | |
bf7a697f ZW |
5338 | for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)); |
5339 | x; | |
5340 | x = TREE_CHAIN (x)) | |
51e29401 | 5341 | { |
bf7a697f ZW |
5342 | tree decl = TREE_VALUE (x); |
5343 | if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) | |
5344 | layout_array_type (TREE_TYPE (decl)); | |
5345 | if (TREE_CODE (decl) != TYPE_DECL) | |
5346 | { | |
5347 | layout_decl (decl, 0); | |
5348 | if (c_dialect_objc ()) | |
5349 | objc_check_decl (decl); | |
5350 | rest_of_decl_compilation (decl, NULL, toplevel, 0); | |
5351 | if (! toplevel) | |
5352 | expand_decl (decl); | |
51e29401 RS |
5353 | } |
5354 | } | |
bf7a697f | 5355 | C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0; |
51e29401 | 5356 | |
51e29401 RS |
5357 | /* Finish debugging output for this type. */ |
5358 | rest_of_type_compilation (t, toplevel); | |
5359 | ||
5360 | return t; | |
5361 | } | |
5362 | ||
5363 | /* Lay out the type T, and its element type, and so on. */ | |
5364 | ||
5365 | static void | |
35b1a6fa | 5366 | layout_array_type (tree t) |
51e29401 RS |
5367 | { |
5368 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) | |
5369 | layout_array_type (TREE_TYPE (t)); | |
5370 | layout_type (t); | |
5371 | } | |
5372 | \f | |
5373 | /* Begin compiling the definition of an enumeration type. | |
5374 | NAME is its name (or null if anonymous). | |
5375 | Returns the type object, as yet incomplete. | |
5376 | Also records info about it so that build_enumerator | |
5377 | may be used to declare the individual values as they are read. */ | |
5378 | ||
5379 | tree | |
35b1a6fa | 5380 | start_enum (tree name) |
51e29401 | 5381 | { |
b3694847 | 5382 | tree enumtype = 0; |
51e29401 RS |
5383 | |
5384 | /* If this is the real definition for a previous forward reference, | |
5385 | fill in the contents in the same object that used to be the | |
5386 | forward reference. */ | |
5387 | ||
5388 | if (name != 0) | |
339a28b9 | 5389 | enumtype = lookup_tag (ENUMERAL_TYPE, name, 1); |
51e29401 RS |
5390 | |
5391 | if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) | |
5392 | { | |
5393 | enumtype = make_node (ENUMERAL_TYPE); | |
5394 | pushtag (name, enumtype); | |
5395 | } | |
5396 | ||
5397 | C_TYPE_BEING_DEFINED (enumtype) = 1; | |
5398 | ||
5399 | if (TYPE_VALUES (enumtype) != 0) | |
5400 | { | |
5401 | /* This enum is a named one that has been declared already. */ | |
5402 | error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name)); | |
5403 | ||
5404 | /* Completely replace its old definition. | |
5405 | The old enumerators remain defined, however. */ | |
5406 | TYPE_VALUES (enumtype) = 0; | |
5407 | } | |
5408 | ||
5409 | enum_next_value = integer_zero_node; | |
93e3ba4f | 5410 | enum_overflow = 0; |
51e29401 | 5411 | |
02eb6e90 RK |
5412 | if (flag_short_enums) |
5413 | TYPE_PACKED (enumtype) = 1; | |
5414 | ||
51e29401 RS |
5415 | return enumtype; |
5416 | } | |
5417 | ||
5418 | /* After processing and defining all the values of an enumeration type, | |
5419 | install their decls in the enumeration type and finish it off. | |
10861e9a RK |
5420 | ENUMTYPE is the type object, VALUES a list of decl-value pairs, |
5421 | and ATTRIBUTES are the specified attributes. | |
51e29401 RS |
5422 | Returns ENUMTYPE. */ |
5423 | ||
5424 | tree | |
35b1a6fa | 5425 | finish_enum (tree enumtype, tree values, tree attributes) |
51e29401 | 5426 | { |
b3694847 | 5427 | tree pair, tem; |
6de9cd9a | 5428 | tree minnode = 0, maxnode = 0; |
cb3ca04e | 5429 | int precision, unsign; |
f75fbaf7 | 5430 | bool toplevel = (file_scope == current_scope); |
6de9cd9a | 5431 | struct lang_type *lt; |
51e29401 | 5432 | |
91d231cb | 5433 | decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); |
10861e9a | 5434 | |
51e29401 RS |
5435 | /* Calculate the maximum value of any enumerator in this type. */ |
5436 | ||
59116212 RK |
5437 | if (values == error_mark_node) |
5438 | minnode = maxnode = integer_zero_node; | |
5439 | else | |
f500253d | 5440 | { |
cb3ca04e ZW |
5441 | minnode = maxnode = TREE_VALUE (values); |
5442 | for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair)) | |
f500253d | 5443 | { |
cb3ca04e ZW |
5444 | tree value = TREE_VALUE (pair); |
5445 | if (tree_int_cst_lt (maxnode, value)) | |
5446 | maxnode = value; | |
5447 | if (tree_int_cst_lt (value, minnode)) | |
5448 | minnode = value; | |
f500253d | 5449 | } |
cb3ca04e | 5450 | } |
f500253d | 5451 | |
cb3ca04e ZW |
5452 | /* Construct the final type of this enumeration. It is the same |
5453 | as one of the integral types - the narrowest one that fits, except | |
5454 | that normally we only go as narrow as int - and signed iff any of | |
5455 | the values are negative. */ | |
5456 | unsign = (tree_int_cst_sgn (minnode) >= 0); | |
5457 | precision = MAX (min_precision (minnode, unsign), | |
5458 | min_precision (maxnode, unsign)); | |
1ada4cd0 | 5459 | if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node)) |
cb3ca04e | 5460 | { |
6de9cd9a DN |
5461 | tem = c_common_type_for_size (precision, unsign); |
5462 | if (tem == NULL) | |
1ada4cd0 JJ |
5463 | { |
5464 | warning ("enumeration values exceed range of largest integer"); | |
6de9cd9a | 5465 | tem = long_long_integer_type_node; |
1ada4cd0 | 5466 | } |
f500253d | 5467 | } |
1ada4cd0 | 5468 | else |
6de9cd9a | 5469 | tem = unsign ? unsigned_type_node : integer_type_node; |
736f85c7 | 5470 | |
6de9cd9a DN |
5471 | TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem); |
5472 | TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem); | |
5473 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem); | |
5474 | TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem); | |
51e29401 RS |
5475 | TYPE_SIZE (enumtype) = 0; |
5476 | layout_type (enumtype); | |
5477 | ||
59116212 | 5478 | if (values != error_mark_node) |
75b46437 | 5479 | { |
cb3ca04e ZW |
5480 | /* Change the type of the enumerators to be the enum type. We |
5481 | need to do this irrespective of the size of the enum, for | |
5482 | proper type checking. Replace the DECL_INITIALs of the | |
5483 | enumerators, and the value slots of the list, with copies | |
5484 | that have the enum type; they cannot be modified in place | |
5485 | because they may be shared (e.g. integer_zero_node) Finally, | |
5486 | change the purpose slots to point to the names of the decls. */ | |
59116212 RK |
5487 | for (pair = values; pair; pair = TREE_CHAIN (pair)) |
5488 | { | |
cb3ca04e | 5489 | tree enu = TREE_PURPOSE (pair); |
6de9cd9a | 5490 | tree ini = DECL_INITIAL (enu); |
51e29401 | 5491 | |
cb3ca04e | 5492 | TREE_TYPE (enu) = enumtype; |
736f85c7 AO |
5493 | |
5494 | /* The ISO C Standard mandates enumerators to have type int, | |
5495 | even though the underlying type of an enum type is | |
5496 | unspecified. Here we convert any enumerators that fit in | |
5497 | an int to type int, to avoid promotions to unsigned types | |
5498 | when comparing integers with enumerators that fit in the | |
5499 | int range. When -pedantic is given, build_enumerator() | |
5500 | would have already taken care of those that don't fit. */ | |
6de9cd9a DN |
5501 | if (int_fits_type_p (ini, integer_type_node)) |
5502 | tem = integer_type_node; | |
736f85c7 | 5503 | else |
6de9cd9a DN |
5504 | tem = enumtype; |
5505 | ini = convert (tem, ini); | |
cb3ca04e | 5506 | |
6de9cd9a | 5507 | DECL_INITIAL (enu) = ini; |
cb3ca04e | 5508 | TREE_PURPOSE (pair) = DECL_NAME (enu); |
6de9cd9a | 5509 | TREE_VALUE (pair) = ini; |
cb3ca04e | 5510 | } |
51e29401 | 5511 | |
59116212 RK |
5512 | TYPE_VALUES (enumtype) = values; |
5513 | } | |
51e29401 | 5514 | |
6de9cd9a DN |
5515 | /* Record the min/max values so that we can warn about bit-field |
5516 | enumerations that are too small for the values. */ | |
5517 | lt = ggc_alloc_cleared (sizeof (struct lang_type)); | |
5518 | lt->enum_min = minnode; | |
5519 | lt->enum_max = maxnode; | |
5520 | TYPE_LANG_SPECIFIC (enumtype) = lt; | |
5521 | ||
fbe23ee7 RS |
5522 | /* Fix up all variant types of this enum type. */ |
5523 | for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem)) | |
5524 | { | |
cb3ca04e ZW |
5525 | if (tem == enumtype) |
5526 | continue; | |
fbe23ee7 RS |
5527 | TYPE_VALUES (tem) = TYPE_VALUES (enumtype); |
5528 | TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype); | |
5529 | TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype); | |
5530 | TYPE_SIZE (tem) = TYPE_SIZE (enumtype); | |
def9b006 | 5531 | TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype); |
fbe23ee7 RS |
5532 | TYPE_MODE (tem) = TYPE_MODE (enumtype); |
5533 | TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype); | |
5534 | TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype); | |
11cf4d18 | 5535 | TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype); |
8df83eae | 5536 | TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype); |
6de9cd9a | 5537 | TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype); |
fbe23ee7 RS |
5538 | } |
5539 | ||
51e29401 RS |
5540 | /* Finish debugging output for this type. */ |
5541 | rest_of_type_compilation (enumtype, toplevel); | |
5542 | ||
5543 | return enumtype; | |
5544 | } | |
5545 | ||
5546 | /* Build and install a CONST_DECL for one value of the | |
5547 | current enumeration type (one that was begun with start_enum). | |
5548 | Return a tree-list containing the CONST_DECL and its value. | |
5549 | Assignment of sequential values by default is handled here. */ | |
5550 | ||
5551 | tree | |
35b1a6fa | 5552 | build_enumerator (tree name, tree value) |
51e29401 | 5553 | { |
b3694847 | 5554 | tree decl, type; |
51e29401 RS |
5555 | |
5556 | /* Validate and default VALUE. */ | |
5557 | ||
5558 | /* Remove no-op casts from the value. */ | |
cd7a1451 | 5559 | if (value) |
874a7be1 | 5560 | STRIP_TYPE_NOPS (value); |
51e29401 | 5561 | |
90374cc2 | 5562 | if (value != 0) |
e681c5a1 | 5563 | { |
f75fbaf7 ZW |
5564 | /* Don't issue more errors for error_mark_node (i.e. an |
5565 | undeclared identifier) - just ignore the value expression. */ | |
5566 | if (value == error_mark_node) | |
5567 | value = 0; | |
5568 | else if (TREE_CODE (value) != INTEGER_CST) | |
25a1019f | 5569 | { |
f75fbaf7 ZW |
5570 | error ("enumerator value for '%E' is not an integer constant", name); |
5571 | value = 0; | |
25a1019f | 5572 | } |
e681c5a1 RS |
5573 | else |
5574 | { | |
f75fbaf7 ZW |
5575 | value = default_conversion (value); |
5576 | constant_expression_warning (value); | |
e681c5a1 RS |
5577 | } |
5578 | } | |
51e29401 RS |
5579 | |
5580 | /* Default based on previous value. */ | |
5581 | /* It should no longer be possible to have NON_LVALUE_EXPR | |
5582 | in the default. */ | |
5583 | if (value == 0) | |
93e3ba4f RS |
5584 | { |
5585 | value = enum_next_value; | |
5586 | if (enum_overflow) | |
5587 | error ("overflow in enumeration values"); | |
5588 | } | |
51e29401 RS |
5589 | |
5590 | if (pedantic && ! int_fits_type_p (value, integer_type_node)) | |
5591 | { | |
89abf8d1 | 5592 | pedwarn ("ISO C restricts enumerator values to range of `int'"); |
f75fbaf7 ZW |
5593 | /* XXX This causes -pedantic to change the meaning of the program. |
5594 | Remove? -zw 2004-03-15 */ | |
736f85c7 | 5595 | value = convert (integer_type_node, value); |
51e29401 RS |
5596 | } |
5597 | ||
5598 | /* Set basis for default for next value. */ | |
5599 | enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0); | |
93e3ba4f | 5600 | enum_overflow = tree_int_cst_lt (enum_next_value, value); |
51e29401 RS |
5601 | |
5602 | /* Now create a declaration for the enum value name. */ | |
5603 | ||
75b46437 | 5604 | type = TREE_TYPE (value); |
b0c48229 NB |
5605 | type = c_common_type_for_size (MAX (TYPE_PRECISION (type), |
5606 | TYPE_PRECISION (integer_type_node)), | |
5607 | (TYPE_PRECISION (type) | |
5608 | >= TYPE_PRECISION (integer_type_node) | |
8df83eae | 5609 | && TYPE_UNSIGNED (type))); |
75b46437 RS |
5610 | |
5611 | decl = build_decl (CONST_DECL, name, type); | |
0543d026 | 5612 | DECL_INITIAL (decl) = convert (type, value); |
51e29401 RS |
5613 | pushdecl (decl); |
5614 | ||
4dd7201e | 5615 | return tree_cons (decl, value, NULL_TREE); |
51e29401 | 5616 | } |
8f17b5c5 | 5617 | |
51e29401 RS |
5618 | \f |
5619 | /* Create the FUNCTION_DECL for a function definition. | |
f7a4cec0 | 5620 | DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of |
5730cf69 | 5621 | the declaration; they describe the function's name and the type it returns, |
51e29401 RS |
5622 | but twisted together in a fashion that parallels the syntax of C. |
5623 | ||
5624 | This function creates a binding context for the function body | |
5625 | as well as setting up the FUNCTION_DECL in current_function_decl. | |
5626 | ||
5627 | Returns 1 on success. If the DECLARATOR is not suitable for a function | |
5628 | (it defines a datum instead), we return 0, which tells | |
4dd7201e | 5629 | yyparse to report a parse error. */ |
51e29401 RS |
5630 | |
5631 | int | |
35b1a6fa | 5632 | start_function (tree declspecs, tree declarator, tree attributes) |
51e29401 RS |
5633 | { |
5634 | tree decl1, old_decl; | |
5635 | tree restype; | |
5636 | ||
0f41302f | 5637 | current_function_returns_value = 0; /* Assume, until we see it does. */ |
51e29401 | 5638 | current_function_returns_null = 0; |
5ce89b2e | 5639 | current_function_returns_abnormally = 0; |
51e29401 RS |
5640 | warn_about_return_type = 0; |
5641 | current_extern_inline = 0; | |
506e2710 RH |
5642 | c_switch_stack = NULL; |
5643 | ||
5644 | /* Indicate no valid break/continue context by setting these variables | |
5645 | to some non-null, non-label value. We'll notice and emit the proper | |
5646 | error message in c_finish_bc_stmt. */ | |
5647 | c_break_label = c_cont_label = size_zero_node; | |
51e29401 | 5648 | |
2ff7cce4 | 5649 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL); |
51e29401 RS |
5650 | |
5651 | /* If the declarator is not suitable for a function definition, | |
5652 | cause a syntax error. */ | |
5653 | if (decl1 == 0) | |
6a0bec2c | 5654 | return 0; |
51e29401 | 5655 | |
59387d2e | 5656 | decl_attributes (&decl1, attributes, 0); |
7665dfd4 | 5657 | |
9162542e AO |
5658 | if (DECL_DECLARED_INLINE_P (decl1) |
5659 | && DECL_UNINLINABLE (decl1) | |
5660 | && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1))) | |
ddd2d57e | 5661 | warning ("%Jinline function '%D' given attribute noinline", decl1, decl1); |
9162542e | 5662 | |
51e29401 RS |
5663 | announce_function (decl1); |
5664 | ||
d0f062fb | 5665 | if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1)))) |
51e29401 | 5666 | { |
903f51d9 | 5667 | error ("return type is an incomplete type"); |
51e29401 RS |
5668 | /* Make it return void instead. */ |
5669 | TREE_TYPE (decl1) | |
5670 | = build_function_type (void_type_node, | |
5671 | TYPE_ARG_TYPES (TREE_TYPE (decl1))); | |
5672 | } | |
5673 | ||
5674 | if (warn_about_return_type) | |
903f51d9 | 5675 | pedwarn_c99 ("return type defaults to `int'"); |
51e29401 | 5676 | |
51e29401 | 5677 | /* Make the init_value nonzero so pushdecl knows this is not tentative. |
f75fbaf7 | 5678 | error_mark_node is replaced below (in pop_scope) with the BLOCK. */ |
51e29401 RS |
5679 | DECL_INITIAL (decl1) = error_mark_node; |
5680 | ||
5681 | /* If this definition isn't a prototype and we had a prototype declaration | |
5682 | before, copy the arg type info from that prototype. | |
5683 | But not if what we had before was a builtin function. */ | |
f75fbaf7 | 5684 | old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope); |
51e29401 RS |
5685 | if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE |
5686 | && !DECL_BUILT_IN (old_decl) | |
fa7d8b92 RK |
5687 | && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) |
5688 | == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl)))) | |
51e29401 | 5689 | && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0) |
50a9145c JW |
5690 | { |
5691 | TREE_TYPE (decl1) = TREE_TYPE (old_decl); | |
f31686a3 | 5692 | current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl); |
50a9145c | 5693 | } |
51e29401 RS |
5694 | |
5695 | /* Optionally warn of old-fashioned def with no previous prototype. */ | |
5696 | if (warn_strict_prototypes | |
5697 | && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0 | |
339a28b9 | 5698 | && C_DECL_ISNT_PROTOTYPE (old_decl)) |
51e29401 RS |
5699 | warning ("function declaration isn't a prototype"); |
5700 | /* Optionally warn of any global def with no previous prototype. */ | |
5701 | else if (warn_missing_prototypes | |
5702 | && TREE_PUBLIC (decl1) | |
339a28b9 ZW |
5703 | && ! MAIN_NAME_P (DECL_NAME (decl1)) |
5704 | && C_DECL_ISNT_PROTOTYPE (old_decl)) | |
ddd2d57e | 5705 | warning ("%Jno previous prototype for '%D'", decl1, decl1); |
51e29401 RS |
5706 | /* Optionally warn of any def with no previous prototype |
5707 | if the function has already been used. */ | |
5708 | else if (warn_missing_prototypes | |
5709 | && old_decl != 0 && TREE_USED (old_decl) | |
6fc7c517 | 5710 | && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0) |
ddd2d57e RH |
5711 | warning ("%J'%D' was used with no prototype before its definition", |
5712 | decl1, decl1); | |
1474fe46 RK |
5713 | /* Optionally warn of any global def with no previous declaration. */ |
5714 | else if (warn_missing_declarations | |
5715 | && TREE_PUBLIC (decl1) | |
5716 | && old_decl == 0 | |
5b47282c | 5717 | && ! MAIN_NAME_P (DECL_NAME (decl1))) |
ddd2d57e | 5718 | warning ("%Jno previous declaration for '%D'", decl1, decl1); |
1474fe46 RK |
5719 | /* Optionally warn of any def with no previous declaration |
5720 | if the function has already been used. */ | |
5721 | else if (warn_missing_declarations | |
6fc7c517 | 5722 | && old_decl != 0 && TREE_USED (old_decl) |
339a28b9 | 5723 | && C_DECL_IMPLICIT (old_decl)) |
ddd2d57e RH |
5724 | warning ("%J`%D' was used with no declaration before its definition", |
5725 | decl1, decl1); | |
51e29401 RS |
5726 | |
5727 | /* This is a definition, not a reference. | |
1394aabd | 5728 | So normally clear DECL_EXTERNAL. |
51e29401 | 5729 | However, `extern inline' acts like a declaration |
1394aabd RS |
5730 | except for defining how to inline. So set DECL_EXTERNAL in that case. */ |
5731 | DECL_EXTERNAL (decl1) = current_extern_inline; | |
51e29401 RS |
5732 | |
5733 | /* This function exists in static storage. | |
5734 | (This does not mean `static' in the C sense!) */ | |
5735 | TREE_STATIC (decl1) = 1; | |
5736 | ||
5737 | /* A nested function is not global. */ | |
5738 | if (current_function_decl != 0) | |
5739 | TREE_PUBLIC (decl1) = 0; | |
5740 | ||
d05cc98e GK |
5741 | #ifdef ENABLE_CHECKING |
5742 | /* This is the earliest point at which we might know the assembler | |
5743 | name of the function. Thus, if it's set before this, die horribly. */ | |
5744 | if (DECL_ASSEMBLER_NAME_SET_P (decl1)) | |
5745 | abort (); | |
5746 | #endif | |
5747 | ||
5748 | /* If #pragma weak was used, mark the decl weak now. */ | |
f75fbaf7 | 5749 | if (current_scope == file_scope) |
d05cc98e GK |
5750 | maybe_apply_pragma_weak (decl1); |
5751 | ||
6645c3fa | 5752 | /* Warn for unlikely, improbable, or stupid declarations of `main'. */ |
5b47282c | 5753 | if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1))) |
b8705e61 RK |
5754 | { |
5755 | tree args; | |
5756 | int argct = 0; | |
5757 | ||
5758 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1))) | |
6645c3fa | 5759 | != integer_type_node) |
ddd2d57e | 5760 | pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1); |
b8705e61 RK |
5761 | |
5762 | for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args; | |
5763 | args = TREE_CHAIN (args)) | |
5764 | { | |
5765 | tree type = args ? TREE_VALUE (args) : 0; | |
5766 | ||
5767 | if (type == void_type_node) | |
5768 | break; | |
5769 | ||
5770 | ++argct; | |
5771 | switch (argct) | |
5772 | { | |
5773 | case 1: | |
5774 | if (TYPE_MAIN_VARIANT (type) != integer_type_node) | |
ddd2d57e RH |
5775 | pedwarn ("%Jfirst argument of '%D' should be `int'", |
5776 | decl1, decl1); | |
b8705e61 RK |
5777 | break; |
5778 | ||
5779 | case 2: | |
5780 | if (TREE_CODE (type) != POINTER_TYPE | |
5781 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
5782 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
5783 | != char_type_node)) | |
ddd2d57e RH |
5784 | pedwarn ("%Jsecond argument of '%D' should be 'char **'", |
5785 | decl1, decl1); | |
b8705e61 RK |
5786 | break; |
5787 | ||
5788 | case 3: | |
5789 | if (TREE_CODE (type) != POINTER_TYPE | |
5790 | || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE | |
5791 | || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type))) | |
5792 | != char_type_node)) | |
ddd2d57e RH |
5793 | pedwarn ("%Jthird argument of '%D' should probably be " |
5794 | "'char **'", decl1, decl1); | |
b8705e61 RK |
5795 | break; |
5796 | } | |
d71f83ca | 5797 | } |
b8705e61 | 5798 | |
d71f83ca | 5799 | /* It is intentional that this message does not mention the third |
6d1c15cc RK |
5800 | argument because it's only mentioned in an appendix of the |
5801 | standard. */ | |
d71f83ca | 5802 | if (argct > 0 && (argct < 2 || argct > 3)) |
ddd2d57e | 5803 | pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1); |
b8705e61 | 5804 | |
b8705e61 | 5805 | if (! TREE_PUBLIC (decl1)) |
ddd2d57e | 5806 | pedwarn ("%J'%D' is normally a non-static function", decl1, decl1); |
b8705e61 RK |
5807 | } |
5808 | ||
51e29401 RS |
5809 | /* Record the decl so that the function name is defined. |
5810 | If we already have a decl for this name, and it is a FUNCTION_DECL, | |
5811 | use the old decl. */ | |
5812 | ||
5813 | current_function_decl = pushdecl (decl1); | |
5814 | ||
f75fbaf7 | 5815 | push_scope (); |
eb1dfbb2 | 5816 | declare_parm_level (); |
51e29401 | 5817 | |
51e29401 RS |
5818 | restype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
5819 | /* Promote the value to int before returning it. */ | |
d72040f5 | 5820 | if (c_promoting_integer_type_p (restype)) |
42dfa47f | 5821 | { |
f458d1d5 | 5822 | /* It retains unsignedness if not really getting wider. */ |
8df83eae | 5823 | if (TYPE_UNSIGNED (restype) |
f458d1d5 ZW |
5824 | && (TYPE_PRECISION (restype) |
5825 | == TYPE_PRECISION (integer_type_node))) | |
42dfa47f RS |
5826 | restype = unsigned_type_node; |
5827 | else | |
5828 | restype = integer_type_node; | |
5829 | } | |
8d9bfdc5 RK |
5830 | DECL_RESULT (current_function_decl) |
5831 | = build_decl (RESULT_DECL, NULL_TREE, restype); | |
51e29401 | 5832 | |
0ba8a114 | 5833 | start_fname_decls (); |
35b1a6fa | 5834 | |
51e29401 RS |
5835 | return 1; |
5836 | } | |
51e29401 | 5837 | \f |
66db6b62 ZW |
5838 | /* Subroutine of store_parm_decls which handles new-style function |
5839 | definitions (prototype format). The parms already have decls, so we | |
5840 | need only record them as in effect and complain if any redundant | |
5841 | old-style parm decls were written. */ | |
5842 | static void | |
77dbdb57 | 5843 | store_parm_decls_newstyle (tree fndecl, tree arg_info) |
51e29401 | 5844 | { |
f75fbaf7 ZW |
5845 | tree decl; |
5846 | tree parms = ARG_INFO_PARMS (arg_info); | |
5847 | tree tags = ARG_INFO_TAGS (arg_info); | |
77dbdb57 | 5848 | tree others = ARG_INFO_OTHERS (arg_info); |
51e29401 | 5849 | |
f75fbaf7 | 5850 | if (current_scope->bindings) |
66db6b62 | 5851 | { |
ddd2d57e RH |
5852 | error ("%Jold-style parameter declarations in prototyped " |
5853 | "function definition", fndecl); | |
51e29401 | 5854 | |
66db6b62 | 5855 | /* Get rid of the old-style declarations. */ |
f75fbaf7 ZW |
5856 | pop_scope (); |
5857 | push_scope (); | |
5858 | } | |
5859 | /* Don't issue this warning for nested functions, and don't issue this | |
5860 | warning if we got here because ARG_INFO_TYPES was error_mark_node | |
5861 | (this happens when a function definition has just an ellipsis in | |
5862 | its parameter list). */ | |
3358cae0 | 5863 | else if (warn_traditional && !in_system_header && !current_function_scope |
f75fbaf7 ZW |
5864 | && ARG_INFO_TYPES (arg_info) != error_mark_node) |
5865 | warning ("%Jtraditional C rejects ISO C style function definitions", | |
5866 | fndecl); | |
51e29401 | 5867 | |
55d54003 ZW |
5868 | /* Now make all the parameter declarations visible in the function body. |
5869 | We can bypass most of the grunt work of pushdecl. */ | |
f75fbaf7 | 5870 | for (decl = parms; decl; decl = TREE_CHAIN (decl)) |
66db6b62 | 5871 | { |
55d54003 | 5872 | DECL_CONTEXT (decl) = current_function_decl; |
f75fbaf7 | 5873 | if (DECL_NAME (decl)) |
9aaabf8a ZW |
5874 | bind (DECL_NAME (decl), decl, current_scope, |
5875 | /*invisible=*/false, /*nested=*/false); | |
66db6b62 | 5876 | else |
f75fbaf7 | 5877 | error ("%Jparameter name omitted", decl); |
66db6b62 | 5878 | } |
26f943fd | 5879 | |
66db6b62 | 5880 | /* Record the parameter list in the function declaration. */ |
55d54003 | 5881 | DECL_ARGUMENTS (fndecl) = parms; |
51e29401 | 5882 | |
66db6b62 | 5883 | /* Now make all the ancillary declarations visible, likewise. */ |
f75fbaf7 | 5884 | for (decl = others; decl; decl = TREE_CHAIN (decl)) |
55d54003 ZW |
5885 | { |
5886 | DECL_CONTEXT (decl) = current_function_decl; | |
f75fbaf7 | 5887 | if (DECL_NAME (decl)) |
9aaabf8a ZW |
5888 | bind (DECL_NAME (decl), decl, current_scope, |
5889 | /*invisible=*/false, /*nested=*/false); | |
55d54003 | 5890 | } |
26f943fd | 5891 | |
66db6b62 | 5892 | /* And all the tag declarations. */ |
55d54003 ZW |
5893 | for (decl = tags; decl; decl = TREE_CHAIN (decl)) |
5894 | if (TREE_PURPOSE (decl)) | |
9aaabf8a ZW |
5895 | bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope, |
5896 | /*invisible=*/false, /*nested=*/false); | |
66db6b62 | 5897 | } |
1f731749 | 5898 | |
66db6b62 ZW |
5899 | /* Subroutine of store_parm_decls which handles old-style function |
5900 | definitions (separate parameter list and declarations). */ | |
51e29401 | 5901 | |
66db6b62 | 5902 | static void |
77dbdb57 | 5903 | store_parm_decls_oldstyle (tree fndecl, tree arg_info) |
66db6b62 | 5904 | { |
f75fbaf7 | 5905 | struct c_binding *b; |
55d54003 | 5906 | tree parm, decl, last; |
77dbdb57 | 5907 | tree parmids = ARG_INFO_PARMS (arg_info); |
51e29401 | 5908 | |
66db6b62 | 5909 | /* We use DECL_WEAK as a flag to show which parameters have been |
f91f41b2 ZW |
5910 | seen already, since it is not used on PARM_DECL. */ |
5911 | #ifdef ENABLE_CHECKING | |
f75fbaf7 ZW |
5912 | for (b = current_scope->bindings; b; b = b->prev) |
5913 | if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl)) | |
f91f41b2 ZW |
5914 | abort (); |
5915 | #endif | |
66db6b62 | 5916 | |
f75fbaf7 ZW |
5917 | if (warn_old_style_definition && !in_system_header) |
5918 | warning ("%Jold-style function definition", fndecl); | |
5919 | ||
66db6b62 ZW |
5920 | /* Match each formal parameter name with its declaration. Save each |
5921 | decl in the appropriate TREE_PURPOSE slot of the parmids chain. */ | |
5922 | for (parm = parmids; parm; parm = TREE_CHAIN (parm)) | |
5923 | { | |
5924 | if (TREE_VALUE (parm) == 0) | |
7a0347ff | 5925 | { |
ddd2d57e | 5926 | error ("%Jparameter name missing from parameter list", fndecl); |
66db6b62 ZW |
5927 | TREE_PURPOSE (parm) = 0; |
5928 | continue; | |
7a0347ff | 5929 | } |
51e29401 | 5930 | |
f75fbaf7 | 5931 | b = I_SYMBOL_BINDING (TREE_VALUE (parm)); |
9aaabf8a | 5932 | if (b && B_IN_CURRENT_SCOPE (b)) |
51e29401 | 5933 | { |
f75fbaf7 | 5934 | decl = b->decl; |
66db6b62 ZW |
5935 | /* If we got something other than a PARM_DECL it is an error. */ |
5936 | if (TREE_CODE (decl) != PARM_DECL) | |
f75fbaf7 | 5937 | error ("%J'%D' declared as a non-parameter", decl, decl); |
66db6b62 ZW |
5938 | /* If the declaration is already marked, we have a duplicate |
5939 | name. Complain and ignore the duplicate. */ | |
5940 | else if (DECL_WEAK (decl)) | |
51e29401 | 5941 | { |
f75fbaf7 | 5942 | error ("%Jmultiple parameters named '%D'", decl, decl); |
66db6b62 ZW |
5943 | TREE_PURPOSE (parm) = 0; |
5944 | continue; | |
51e29401 | 5945 | } |
66db6b62 ZW |
5946 | /* If the declaration says "void", complain and turn it into |
5947 | an int. */ | |
5948 | else if (VOID_TYPE_P (TREE_TYPE (decl))) | |
51e29401 | 5949 | { |
f75fbaf7 | 5950 | error ("%Jparameter '%D' declared with void type", decl, decl); |
66db6b62 ZW |
5951 | TREE_TYPE (decl) = integer_type_node; |
5952 | DECL_ARG_TYPE (decl) = integer_type_node; | |
5953 | layout_decl (decl, 0); | |
51e29401 RS |
5954 | } |
5955 | } | |
66db6b62 ZW |
5956 | /* If no declaration found, default to int. */ |
5957 | else | |
51e29401 | 5958 | { |
66db6b62 ZW |
5959 | decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node); |
5960 | DECL_ARG_TYPE (decl) = TREE_TYPE (decl); | |
f31686a3 | 5961 | DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl); |
66db6b62 ZW |
5962 | pushdecl (decl); |
5963 | ||
5964 | if (flag_isoc99) | |
f75fbaf7 | 5965 | pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl); |
66db6b62 | 5966 | else if (extra_warnings) |
f75fbaf7 | 5967 | warning ("%Jtype of '%D' defaults to 'int'", decl, decl); |
51e29401 RS |
5968 | } |
5969 | ||
66db6b62 ZW |
5970 | TREE_PURPOSE (parm) = decl; |
5971 | DECL_WEAK (decl) = 1; | |
51e29401 | 5972 | } |
51e29401 | 5973 | |
55d54003 ZW |
5974 | /* Now examine the parms chain for incomplete declarations |
5975 | and declarations with no corresponding names. */ | |
51e29401 | 5976 | |
f75fbaf7 | 5977 | for (b = current_scope->bindings; b; b = b->prev) |
66db6b62 | 5978 | { |
f75fbaf7 ZW |
5979 | parm = b->decl; |
5980 | if (TREE_CODE (parm) != PARM_DECL) | |
5981 | continue; | |
5982 | ||
66db6b62 | 5983 | if (!COMPLETE_TYPE_P (TREE_TYPE (parm))) |
51e29401 | 5984 | { |
f75fbaf7 | 5985 | error ("%Jparameter '%D' has incomplete type", parm, parm); |
66db6b62 ZW |
5986 | TREE_TYPE (parm) = error_mark_node; |
5987 | } | |
51e29401 | 5988 | |
66db6b62 ZW |
5989 | if (! DECL_WEAK (parm)) |
5990 | { | |
f75fbaf7 | 5991 | error ("%Jdeclaration for parameter '%D' but no such parameter", |
ddd2d57e | 5992 | parm, parm); |
51e29401 | 5993 | |
66db6b62 ZW |
5994 | /* Pretend the parameter was not missing. |
5995 | This gets us to a standard state and minimizes | |
5996 | further error messages. */ | |
5997 | parmids = chainon (parmids, tree_cons (parm, 0, 0)); | |
51e29401 | 5998 | } |
66db6b62 | 5999 | } |
51e29401 | 6000 | |
66db6b62 ZW |
6001 | /* Chain the declarations together in the order of the list of |
6002 | names. Store that chain in the function decl, replacing the | |
55d54003 | 6003 | list of names. Update the current scope to match. */ |
66db6b62 | 6004 | DECL_ARGUMENTS (fndecl) = 0; |
66db6b62 | 6005 | |
55d54003 ZW |
6006 | for (parm = parmids; parm; parm = TREE_CHAIN (parm)) |
6007 | if (TREE_PURPOSE (parm)) | |
6008 | break; | |
6009 | if (parm && TREE_PURPOSE (parm)) | |
6010 | { | |
6011 | last = TREE_PURPOSE (parm); | |
6012 | DECL_ARGUMENTS (fndecl) = last; | |
55d54003 ZW |
6013 | DECL_WEAK (last) = 0; |
6014 | ||
6015 | for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm)) | |
6016 | if (TREE_PURPOSE (parm)) | |
6017 | { | |
6018 | TREE_CHAIN (last) = TREE_PURPOSE (parm); | |
6019 | last = TREE_PURPOSE (parm); | |
6020 | DECL_WEAK (last) = 0; | |
6021 | } | |
6022 | TREE_CHAIN (last) = 0; | |
6023 | } | |
51e29401 | 6024 | |
66db6b62 ZW |
6025 | /* If there was a previous prototype, |
6026 | set the DECL_ARG_TYPE of each argument according to | |
6027 | the type previously specified, and report any mismatches. */ | |
51e29401 | 6028 | |
66db6b62 ZW |
6029 | if (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) |
6030 | { | |
6031 | tree type; | |
6032 | for (parm = DECL_ARGUMENTS (fndecl), | |
6033 | type = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); | |
6034 | parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) | |
6035 | != void_type_node)); | |
6036 | parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) | |
51e29401 | 6037 | { |
66db6b62 ZW |
6038 | if (parm == 0 || type == 0 |
6039 | || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node) | |
51e29401 | 6040 | { |
66db6b62 ZW |
6041 | error ("number of arguments doesn't match prototype"); |
6042 | error ("%Hprototype declaration", | |
6043 | ¤t_function_prototype_locus); | |
6044 | break; | |
6045 | } | |
6046 | /* Type for passing arg must be consistent with that | |
6047 | declared for the arg. ISO C says we take the unqualified | |
6048 | type for parameters declared with qualified type. */ | |
6049 | if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)), | |
132da1a5 | 6050 | TYPE_MAIN_VARIANT (TREE_VALUE (type)))) |
66db6b62 ZW |
6051 | { |
6052 | if (TYPE_MAIN_VARIANT (TREE_TYPE (parm)) | |
6053 | == TYPE_MAIN_VARIANT (TREE_VALUE (type))) | |
51e29401 | 6054 | { |
66db6b62 ZW |
6055 | /* Adjust argument to match prototype. E.g. a previous |
6056 | `int foo(float);' prototype causes | |
6057 | `int foo(x) float x; {...}' to be treated like | |
6058 | `int foo(float x) {...}'. This is particularly | |
6059 | useful for argument types like uid_t. */ | |
6060 | DECL_ARG_TYPE (parm) = TREE_TYPE (parm); | |
6061 | ||
61f71b34 | 6062 | if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl)) |
66db6b62 ZW |
6063 | && INTEGRAL_TYPE_P (TREE_TYPE (parm)) |
6064 | && TYPE_PRECISION (TREE_TYPE (parm)) | |
6065 | < TYPE_PRECISION (integer_type_node)) | |
6066 | DECL_ARG_TYPE (parm) = integer_type_node; | |
6067 | ||
6068 | if (pedantic) | |
50a9145c | 6069 | { |
f75fbaf7 | 6070 | pedwarn ("promoted argument '%D' " |
66db6b62 ZW |
6071 | "doesn't match prototype", parm); |
6072 | pedwarn ("%Hprototype declaration", | |
6073 | ¤t_function_prototype_locus); | |
50a9145c | 6074 | } |
51e29401 | 6075 | } |
66db6b62 ZW |
6076 | else |
6077 | { | |
f75fbaf7 | 6078 | error ("argument '%D' doesn't match prototype", parm); |
66db6b62 ZW |
6079 | error ("%Hprototype declaration", |
6080 | ¤t_function_prototype_locus); | |
6081 | } | |
51e29401 | 6082 | } |
51e29401 | 6083 | } |
66db6b62 ZW |
6084 | TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0; |
6085 | } | |
51e29401 | 6086 | |
66db6b62 | 6087 | /* Otherwise, create a prototype that would match. */ |
51e29401 | 6088 | |
66db6b62 ZW |
6089 | else |
6090 | { | |
6091 | tree actual = 0, last = 0, type; | |
51e29401 | 6092 | |
66db6b62 ZW |
6093 | for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) |
6094 | { | |
6095 | type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE); | |
51e29401 RS |
6096 | if (last) |
6097 | TREE_CHAIN (last) = type; | |
6098 | else | |
6099 | actual = type; | |
66db6b62 ZW |
6100 | last = type; |
6101 | } | |
6102 | type = tree_cons (NULL_TREE, void_type_node, NULL_TREE); | |
6103 | if (last) | |
6104 | TREE_CHAIN (last) = type; | |
6105 | else | |
6106 | actual = type; | |
51e29401 | 6107 | |
66db6b62 ZW |
6108 | /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES |
6109 | of the type of this function, but we need to avoid having this | |
6110 | affect the types of other similarly-typed functions, so we must | |
6111 | first force the generation of an identical (but separate) type | |
6112 | node for the relevant function type. The new node we create | |
6113 | will be a variant of the main variant of the original function | |
6114 | type. */ | |
c138f328 | 6115 | |
66db6b62 | 6116 | TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl)); |
c138f328 | 6117 | |
66db6b62 ZW |
6118 | TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual; |
6119 | } | |
66db6b62 ZW |
6120 | } |
6121 | ||
6122 | /* Store the parameter declarations into the current function declaration. | |
6123 | This is called after parsing the parameter declarations, before | |
6124 | digesting the body of the function. | |
6125 | ||
6126 | For an old-style definition, construct a prototype out of the old-style | |
6127 | parameter declarations and inject it into the function's type. */ | |
6128 | ||
6129 | void | |
6130 | store_parm_decls (void) | |
6131 | { | |
6132 | tree fndecl = current_function_decl; | |
6133 | ||
77dbdb57 ZW |
6134 | /* The argument information block for FNDECL. */ |
6135 | tree arg_info = DECL_ARGUMENTS (fndecl); | |
6136 | ||
f75fbaf7 ZW |
6137 | /* True if this definition is written with a prototype. Note: |
6138 | despite C99 6.7.5.3p14, we can *not* treat an empty argument | |
6139 | list in a function definition as equivalent to (void) -- an | |
6140 | empty argument list specifies the function has no parameters, | |
6141 | but only (void) sets up a prototype for future calls. */ | |
6142 | bool proto = ARG_INFO_TYPES (arg_info) != 0; | |
66db6b62 | 6143 | |
f75fbaf7 | 6144 | if (proto) |
77dbdb57 | 6145 | store_parm_decls_newstyle (fndecl, arg_info); |
66db6b62 | 6146 | else |
77dbdb57 | 6147 | store_parm_decls_oldstyle (fndecl, arg_info); |
51e29401 | 6148 | |
f75fbaf7 | 6149 | /* The next call to push_scope will be a function body. */ |
51e29401 | 6150 | |
a8ccdffe | 6151 | next_is_function_body = true; |
51e29401 | 6152 | |
51e29401 RS |
6153 | /* Write a record describing this function definition to the prototypes |
6154 | file (if requested). */ | |
6155 | ||
f75fbaf7 | 6156 | gen_aux_info_record (fndecl, 1, 0, proto); |
51e29401 RS |
6157 | |
6158 | /* Initialize the RTL code for the function. */ | |
4985cde3 | 6159 | allocate_struct_function (fndecl); |
51e29401 | 6160 | |
8f17b5c5 | 6161 | /* Begin the statement tree for this function. */ |
325c3691 | 6162 | DECL_SAVED_TREE (fndecl) = push_stmt_list (); |
51e29401 | 6163 | |
82c82743 RH |
6164 | /* ??? Insert the contents of the pending sizes list into the function |
6165 | to be evaluated. This just changes mis-behaviour until assign_parms | |
6166 | phase ordering problems are resolved. */ | |
6167 | { | |
6168 | tree t; | |
6169 | for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t)) | |
6170 | add_stmt (TREE_VALUE (t)); | |
6171 | } | |
1f731749 | 6172 | |
8f17b5c5 MM |
6173 | /* Even though we're inside a function body, we still don't want to |
6174 | call expand_expr to calculate the size of a variable-sized array. | |
6175 | We haven't necessarily assigned RTL to all variables yet, so it's | |
6176 | not safe to try to expand expressions involving them. */ | |
8f17b5c5 | 6177 | cfun->x_dont_save_pending_sizes_p = 1; |
51e29401 RS |
6178 | } |
6179 | \f | |
6de9cd9a DN |
6180 | /* Give FNDECL and all its nested functions to cgraph for compilation. */ |
6181 | ||
6182 | static void | |
6183 | c_finalize (tree fndecl) | |
6184 | { | |
6185 | struct cgraph_node *cgn; | |
6186 | ||
6187 | /* Handle attribute((warn_unused_result)). Relies on gimple input. */ | |
6188 | c_warn_unused_result (&DECL_SAVED_TREE (fndecl)); | |
6189 | ||
6190 | /* ??? Objc emits functions after finalizing the compilation unit. | |
6191 | This should be cleaned up later and this conditional removed. */ | |
6192 | if (cgraph_global_info_ready) | |
6193 | { | |
6194 | c_expand_body (fndecl); | |
6195 | return; | |
6196 | } | |
6197 | ||
6198 | /* Finalize all nested functions now. */ | |
6199 | cgn = cgraph_node (fndecl); | |
6200 | for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested) | |
6201 | c_finalize (cgn->decl); | |
6202 | ||
6203 | cgraph_finalize_function (fndecl, false); | |
6204 | } | |
6205 | ||
51e29401 RS |
6206 | /* Finish up a function declaration and compile that function |
6207 | all the way to assembler language output. The free the storage | |
6208 | for the function definition. | |
6209 | ||
4a46cbfb | 6210 | This is called after parsing the body of the function definition. */ |
51e29401 RS |
6211 | |
6212 | void | |
edaf3e03 | 6213 | finish_function (void) |
51e29401 | 6214 | { |
b3694847 | 6215 | tree fndecl = current_function_decl; |
51e29401 | 6216 | |
61f71b34 DD |
6217 | if (TREE_CODE (fndecl) == FUNCTION_DECL |
6218 | && targetm.calls.promote_prototypes (TREE_TYPE (fndecl))) | |
6219 | { | |
6220 | tree args = DECL_ARGUMENTS (fndecl); | |
6221 | for (; args; args = TREE_CHAIN (args)) | |
6222 | { | |
6223 | tree type = TREE_TYPE (args); | |
6224 | if (INTEGRAL_TYPE_P (type) | |
6225 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) | |
6226 | DECL_ARG_TYPE (args) = integer_type_node; | |
6227 | } | |
6228 | } | |
6229 | ||
67b28863 | 6230 | if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) |
af3fbed1 | 6231 | BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; |
51e29401 RS |
6232 | |
6233 | /* Must mark the RESULT_DECL as being in this function. */ | |
6234 | ||
67b28863 | 6235 | if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) |
af3fbed1 | 6236 | DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; |
51e29401 | 6237 | |
5b47282c | 6238 | if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted) |
51e29401 | 6239 | { |
90d56da8 RS |
6240 | if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) |
6241 | != integer_type_node) | |
b8705e61 | 6242 | { |
007aaed0 | 6243 | /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned. |
6645c3fa | 6244 | If warn_main is -1 (-Wno-main) we don't want to be warned. */ |
435ab236 | 6245 | if (!warn_main) |
ddd2d57e | 6246 | pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl); |
b8705e61 | 6247 | } |
8e077183 RS |
6248 | else |
6249 | { | |
98be7846 | 6250 | if (flag_isoc99) |
5088b058 | 6251 | c_finish_return (integer_zero_node); |
8e077183 | 6252 | } |
51e29401 | 6253 | } |
35b1a6fa | 6254 | |
8f17b5c5 | 6255 | /* Tie off the statement tree for this function. */ |
325c3691 RH |
6256 | DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); |
6257 | ||
6258 | finish_fname_decls (); | |
5ce89b2e JM |
6259 | |
6260 | /* Complain if there's just no return statement. */ | |
46cfb101 JM |
6261 | if (warn_return_type |
6262 | && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE | |
5ce89b2e JM |
6263 | && !current_function_returns_value && !current_function_returns_null |
6264 | /* Don't complain if we abort. */ | |
6265 | && !current_function_returns_abnormally | |
6266 | /* Don't warn for main(). */ | |
6267 | && !MAIN_NAME_P (DECL_NAME (fndecl)) | |
6268 | /* Or if they didn't actually specify a return type. */ | |
6269 | && !C_FUNCTION_IMPLICIT_INT (fndecl) | |
46cfb101 | 6270 | /* Normally, with -Wreturn-type, flow will complain. Unless we're an |
5ce89b2e | 6271 | inline function, as we might never be compiled separately. */ |
46cfb101 | 6272 | && DECL_INLINE (fndecl)) |
5ce89b2e JM |
6273 | warning ("no return statement in function returning non-void"); |
6274 | ||
03845b47 SB |
6275 | /* With just -Wextra, complain only if function returns both with |
6276 | and without a value. */ | |
6277 | if (extra_warnings | |
6278 | && current_function_returns_value | |
6279 | && current_function_returns_null) | |
6280 | warning ("this function may return with or without a value"); | |
6281 | ||
6de9cd9a DN |
6282 | /* Store the end of the function, so that we get good line number |
6283 | info for the epilogue. */ | |
6284 | cfun->function_end_locus = input_location; | |
6285 | ||
6286 | /* If we don't have ctors/dtors sections, and this is a static | |
6287 | constructor or destructor, it must be recorded now. */ | |
6288 | if (DECL_STATIC_CONSTRUCTOR (fndecl) | |
6289 | && !targetm.have_ctors_dtors) | |
6290 | static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors); | |
6291 | if (DECL_STATIC_DESTRUCTOR (fndecl) | |
6292 | && !targetm.have_ctors_dtors) | |
6293 | static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors); | |
6294 | ||
6295 | /* Genericize before inlining. Delay genericizing nested functions | |
6296 | until their parent function is genericized. Since finalizing | |
6297 | requires GENERIC, delay that as well. */ | |
118a3a8b | 6298 | |
6de9cd9a DN |
6299 | if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node) |
6300 | { | |
6301 | if (!decl_function_context (fndecl)) | |
6302 | { | |
6303 | c_genericize (fndecl); | |
6304 | lower_nested_functions (fndecl); | |
6305 | c_finalize (fndecl); | |
6306 | } | |
6307 | else | |
6308 | { | |
6309 | /* Register this function with cgraph just far enough to get it | |
6310 | added to our parent's nested function list. Handy, since the | |
6311 | C front end doesn't have such a list. */ | |
6312 | (void) cgraph_node (fndecl); | |
6313 | } | |
6314 | } | |
6315 | ||
1da326c3 | 6316 | /* We're leaving the context of this function, so zap cfun. |
6de9cd9a | 6317 | It's still in DECL_STRUCT_FUNCTION, and we'll restore it in |
1da326c3 | 6318 | tree_rest_of_compilation. */ |
8f17b5c5 | 6319 | cfun = NULL; |
4a46cbfb | 6320 | current_function_decl = NULL; |
8f17b5c5 MM |
6321 | } |
6322 | ||
3c79fa86 | 6323 | /* Generate the RTL for the body of FNDECL. */ |
8f17b5c5 | 6324 | |
3c79fa86 AP |
6325 | void |
6326 | c_expand_body (tree fndecl) | |
8f17b5c5 | 6327 | { |
c1f927e8 | 6328 | |
3c79fa86 AP |
6329 | if (!DECL_INITIAL (fndecl) |
6330 | || DECL_INITIAL (fndecl) == error_mark_node) | |
6331 | return; | |
c1f927e8 | 6332 | |
3c79fa86 | 6333 | tree_rest_of_compilation (fndecl, false); |
f4e5c65b | 6334 | |
6de9cd9a DN |
6335 | if (DECL_STATIC_CONSTRUCTOR (fndecl) |
6336 | && targetm.have_ctors_dtors) | |
6337 | targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0), | |
3c79fa86 | 6338 | DEFAULT_INIT_PRIORITY); |
6de9cd9a DN |
6339 | if (DECL_STATIC_DESTRUCTOR (fndecl) |
6340 | && targetm.have_ctors_dtors) | |
6341 | targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0), | |
3c79fa86 | 6342 | DEFAULT_INIT_PRIORITY); |
e72fcfe8 | 6343 | } |
51e29401 | 6344 | \f |
77c4d6c0 JM |
6345 | /* Check the declarations given in a for-loop for satisfying the C99 |
6346 | constraints. */ | |
6347 | void | |
35b1a6fa | 6348 | check_for_loop_decls (void) |
77c4d6c0 | 6349 | { |
f75fbaf7 | 6350 | struct c_binding *b; |
77c4d6c0 JM |
6351 | |
6352 | if (!flag_isoc99) | |
6353 | { | |
6354 | /* If we get here, declarations have been used in a for loop without | |
6355 | the C99 for loop scope. This doesn't make much sense, so don't | |
6356 | allow it. */ | |
f91f41b2 | 6357 | error ("'for' loop initial declaration used outside C99 mode"); |
77c4d6c0 JM |
6358 | return; |
6359 | } | |
6360 | /* C99 subclause 6.8.5 paragraph 3: | |
6361 | ||
6362 | [#3] The declaration part of a for statement shall only | |
6363 | declare identifiers for objects having storage class auto or | |
6364 | register. | |
6365 | ||
6366 | It isn't clear whether, in this sentence, "identifiers" binds to | |
6367 | "shall only declare" or to "objects" - that is, whether all identifiers | |
6368 | declared must be identifiers for objects, or whether the restriction | |
6369 | only applies to those that are. (A question on this in comp.std.c | |
6370 | in November 2000 received no answer.) We implement the strictest | |
6371 | interpretation, to avoid creating an extension which later causes | |
6372 | problems. */ | |
6373 | ||
f75fbaf7 | 6374 | for (b = current_scope->bindings; b; b = b->prev) |
77c4d6c0 | 6375 | { |
f75fbaf7 ZW |
6376 | tree id = b->id; |
6377 | tree decl = b->decl; | |
53fcdc76 | 6378 | |
f75fbaf7 ZW |
6379 | if (!id) |
6380 | continue; | |
6381 | ||
6382 | switch (TREE_CODE (decl)) | |
6383 | { | |
6384 | case VAR_DECL: | |
6385 | if (TREE_STATIC (decl)) | |
6386 | error ("%Jdeclaration of static variable '%D' in 'for' loop " | |
6387 | "initial declaration", decl, decl); | |
6388 | else if (DECL_EXTERNAL (decl)) | |
6389 | error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop " | |
6390 | "initial declaration", decl, decl); | |
6391 | break; | |
6392 | ||
6393 | case RECORD_TYPE: | |
6394 | error ("'struct %E' declared in 'for' loop initial declaration", id); | |
6395 | break; | |
6396 | case UNION_TYPE: | |
6397 | error ("'union %E' declared in 'for' loop initial declaration", id); | |
6398 | break; | |
6399 | case ENUMERAL_TYPE: | |
6400 | error ("'enum %E' declared in 'for' loop initial declaration", id); | |
6401 | break; | |
6402 | default: | |
6403 | error ("%Jdeclaration of non-variable '%D' in 'for' loop " | |
6404 | "initial declaration", decl, decl); | |
6405 | } | |
77c4d6c0 JM |
6406 | } |
6407 | } | |
6408 | \f | |
51e29401 RS |
6409 | /* Save and reinitialize the variables |
6410 | used during compilation of a C function. */ | |
6411 | ||
6412 | void | |
35b1a6fa | 6413 | c_push_function_context (struct function *f) |
51e29401 | 6414 | { |
e2500fed | 6415 | struct language_function *p; |
703ad42b | 6416 | p = ggc_alloc (sizeof (struct language_function)); |
e2500fed | 6417 | f->language = p; |
51e29401 | 6418 | |
8f17b5c5 | 6419 | p->base.x_stmt_tree = c_stmt_tree; |
506e2710 RH |
6420 | p->x_break_label = c_break_label; |
6421 | p->x_cont_label = c_cont_label; | |
6422 | p->x_switch_stack = c_switch_stack; | |
51e29401 RS |
6423 | p->returns_value = current_function_returns_value; |
6424 | p->returns_null = current_function_returns_null; | |
5ce89b2e | 6425 | p->returns_abnormally = current_function_returns_abnormally; |
51e29401 RS |
6426 | p->warn_about_return_type = warn_about_return_type; |
6427 | p->extern_inline = current_extern_inline; | |
51e29401 RS |
6428 | } |
6429 | ||
6430 | /* Restore the variables used during compilation of a C function. */ | |
6431 | ||
6432 | void | |
35b1a6fa | 6433 | c_pop_function_context (struct function *f) |
51e29401 | 6434 | { |
e2500fed | 6435 | struct language_function *p = f->language; |
51e29401 | 6436 | |
1da326c3 | 6437 | if (DECL_STRUCT_FUNCTION (current_function_decl) == 0 |
8f17b5c5 | 6438 | && DECL_SAVED_TREE (current_function_decl) == NULL_TREE) |
51e29401 RS |
6439 | { |
6440 | /* Stop pointing to the local nodes about to be freed. */ | |
6441 | /* But DECL_INITIAL must remain nonzero so we know this | |
6442 | was an actual function definition. */ | |
6443 | DECL_INITIAL (current_function_decl) = error_mark_node; | |
6444 | DECL_ARGUMENTS (current_function_decl) = 0; | |
6445 | } | |
6446 | ||
8f17b5c5 | 6447 | c_stmt_tree = p->base.x_stmt_tree; |
506e2710 RH |
6448 | c_break_label = p->x_break_label; |
6449 | c_cont_label = p->x_cont_label; | |
6450 | c_switch_stack = p->x_switch_stack; | |
51e29401 RS |
6451 | current_function_returns_value = p->returns_value; |
6452 | current_function_returns_null = p->returns_null; | |
5ce89b2e | 6453 | current_function_returns_abnormally = p->returns_abnormally; |
51e29401 RS |
6454 | warn_about_return_type = p->warn_about_return_type; |
6455 | current_extern_inline = p->extern_inline; | |
51e29401 | 6456 | |
e2500fed | 6457 | f->language = NULL; |
1526a060 BS |
6458 | } |
6459 | ||
63e1b1c4 | 6460 | /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */ |
37105beb JM |
6461 | |
6462 | void | |
35b1a6fa | 6463 | c_dup_lang_specific_decl (tree decl) |
37105beb | 6464 | { |
8f17b5c5 MM |
6465 | struct lang_decl *ld; |
6466 | ||
6467 | if (!DECL_LANG_SPECIFIC (decl)) | |
6468 | return; | |
6469 | ||
703ad42b KG |
6470 | ld = ggc_alloc (sizeof (struct lang_decl)); |
6471 | memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl)); | |
8f17b5c5 | 6472 | DECL_LANG_SPECIFIC (decl) = ld; |
37105beb | 6473 | } |
1526a060 | 6474 | |
f2c5f623 BC |
6475 | /* The functions below are required for functionality of doing |
6476 | function at once processing in the C front end. Currently these | |
6477 | functions are not called from anywhere in the C front end, but as | |
6645c3fa | 6478 | these changes continue, that will change. */ |
f2c5f623 | 6479 | |
da7d8304 | 6480 | /* Returns nonzero if the current statement is a full expression, |
f2c5f623 BC |
6481 | i.e. temporaries created during that statement should be destroyed |
6482 | at the end of the statement. */ | |
6483 | ||
6484 | int | |
35b1a6fa | 6485 | stmts_are_full_exprs_p (void) |
f2c5f623 BC |
6486 | { |
6487 | return 0; | |
6488 | } | |
6489 | ||
ae499cce MM |
6490 | /* Returns the stmt_tree (if any) to which statements are currently |
6491 | being added. If there is no active statement-tree, NULL is | |
6492 | returned. */ | |
6493 | ||
6494 | stmt_tree | |
35b1a6fa | 6495 | current_stmt_tree (void) |
ae499cce | 6496 | { |
8f17b5c5 MM |
6497 | return &c_stmt_tree; |
6498 | } | |
6499 | ||
f2c5f623 | 6500 | /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in |
6645c3fa | 6501 | C. */ |
f2c5f623 | 6502 | |
6645c3fa | 6503 | int |
35b1a6fa | 6504 | anon_aggr_type_p (tree node ATTRIBUTE_UNUSED) |
f2c5f623 BC |
6505 | { |
6506 | return 0; | |
6507 | } | |
6508 | ||
8f17b5c5 | 6509 | /* Dummy function in place of callback used by C++. */ |
f2c5f623 | 6510 | |
8f17b5c5 | 6511 | void |
35b1a6fa | 6512 | extract_interface_info (void) |
f2c5f623 | 6513 | { |
f2c5f623 BC |
6514 | } |
6515 | ||
339a28b9 | 6516 | /* Return the global value of T as a symbol. */ |
5fd8e536 JM |
6517 | |
6518 | tree | |
35b1a6fa | 6519 | identifier_global_value (tree t) |
5fd8e536 | 6520 | { |
f75fbaf7 | 6521 | struct c_binding *b; |
339a28b9 | 6522 | |
f75fbaf7 | 6523 | for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed) |
9aaabf8a | 6524 | if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b)) |
f75fbaf7 | 6525 | return b->decl; |
339a28b9 | 6526 | |
339a28b9 | 6527 | return 0; |
5fd8e536 | 6528 | } |
eaa7c03f JM |
6529 | |
6530 | /* Record a builtin type for C. If NAME is non-NULL, it is the name used; | |
6531 | otherwise the name is found in ridpointers from RID_INDEX. */ | |
6532 | ||
6533 | void | |
35b1a6fa | 6534 | record_builtin_type (enum rid rid_index, const char *name, tree type) |
eaa7c03f JM |
6535 | { |
6536 | tree id; | |
6537 | if (name == 0) | |
6538 | id = ridpointers[(int) rid_index]; | |
6539 | else | |
6540 | id = get_identifier (name); | |
f75fbaf7 | 6541 | pushdecl (build_decl (TYPE_DECL, id, type)); |
eaa7c03f JM |
6542 | } |
6543 | ||
6544 | /* Build the void_list_node (void_type_node having been created). */ | |
6545 | tree | |
35b1a6fa | 6546 | build_void_list_node (void) |
eaa7c03f JM |
6547 | { |
6548 | tree t = build_tree_list (NULL_TREE, void_type_node); | |
6549 | return t; | |
6550 | } | |
81a75f0f NB |
6551 | |
6552 | /* Return something to represent absolute declarators containing a *. | |
6553 | TARGET is the absolute declarator that the * contains. | |
6554 | TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile | |
6555 | to apply to the pointer type, represented as identifiers, possible mixed | |
6556 | with attributes. | |
6557 | ||
6558 | We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST, | |
6559 | if attributes are present) and whose type is the modifier list. */ | |
6560 | ||
6561 | tree | |
35b1a6fa | 6562 | make_pointer_declarator (tree type_quals_attrs, tree target) |
81a75f0f NB |
6563 | { |
6564 | tree quals, attrs; | |
6565 | tree itarget = target; | |
6566 | split_specs_attrs (type_quals_attrs, &quals, &attrs); | |
6567 | if (attrs != NULL_TREE) | |
6568 | itarget = tree_cons (attrs, target, NULL_TREE); | |
6569 | return build1 (INDIRECT_REF, quals, itarget); | |
6570 | } | |
e2500fed | 6571 | |
f75fbaf7 ZW |
6572 | /* Perform final processing on file-scope data. */ |
6573 | static void | |
6574 | c_write_global_declarations_1 (tree globals) | |
d1bd0ded | 6575 | { |
f75fbaf7 ZW |
6576 | size_t len = list_length (globals); |
6577 | tree *vec = xmalloc (sizeof (tree) * len); | |
6578 | size_t i; | |
d1bd0ded | 6579 | tree decl; |
118a3a8b | 6580 | |
f75fbaf7 ZW |
6581 | /* Process the decls in the order they were written. */ |
6582 | for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl)) | |
6583 | vec[i] = decl; | |
d1bd0ded | 6584 | |
f75fbaf7 ZW |
6585 | wrapup_global_declarations (vec, len); |
6586 | check_global_declarations (vec, len); | |
118a3a8b | 6587 | |
f75fbaf7 | 6588 | free (vec); |
d1bd0ded GK |
6589 | } |
6590 | ||
d1bd0ded | 6591 | void |
f75fbaf7 | 6592 | c_write_global_declarations (void) |
d1bd0ded | 6593 | { |
f75fbaf7 | 6594 | tree t; |
e13e48e7 | 6595 | |
f75fbaf7 ZW |
6596 | /* We don't want to do this if generating a PCH. */ |
6597 | if (pch_file) | |
6598 | return; | |
d1bd0ded | 6599 | |
f75fbaf7 | 6600 | /* Process all file scopes in this compilation. */ |
eecec698 | 6601 | for (t = all_translation_units; t; t = TREE_CHAIN (t)) |
f75fbaf7 | 6602 | c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t))); |
d1bd0ded | 6603 | |
f75fbaf7 ZW |
6604 | /* Now do the same for the externals scope. */ |
6605 | t = pop_scope (); | |
6606 | if (t) | |
6607 | c_write_global_declarations_1 (BLOCK_VARS (t)); | |
d1bd0ded GK |
6608 | } |
6609 | ||
e2500fed | 6610 | #include "gt-c-decl.h" |