|
|
1.1 root 1: /* Process declarations and variables for C compiler.
1.1.1.2 root 2: Copyright (C) 1988 Free Software Foundation, Inc.
1.1 root 3:
4: This file is part of GNU CC.
5:
6: GNU CC is distributed in the hope that it will be useful,
7: but WITHOUT ANY WARRANTY. No author or distributor
8: accepts responsibility to anyone for the consequences of using it
9: or for whether it serves any particular purpose or works at all,
10: unless he says so in writing. Refer to the GNU CC General Public
11: License for full details.
12:
13: Everyone is granted permission to copy, modify and redistribute
14: GNU CC, but only under the conditions described in the
15: GNU CC General Public License. A copy of this license is
16: supposed to have been given to you along with GNU CC so you
17: can know your rights and responsibilities. It should be in a
18: file named COPYING. Among other things, the copyright notice
19: and this notice must be preserved on all copies. */
20:
21:
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"
30: #include "tree.h"
1.1.1.2 root 31: #include "flags.h"
1.1 root 32: #include "c-tree.h"
1.1.1.2 root 33: #include "parse.h"
1.1 root 34:
35: /* In grokdeclarator, distinguish syntactic contexts of declarators. */
36: enum decl_context
37: { NORMAL, /* Ordinary declaration */
1.1.1.2 root 38: FUNCDEF, /* Function definition */
1.1 root 39: PARM, /* Declaration of parm before function body */
40: FIELD, /* Declaration inside struct or union */
41: TYPENAME}; /* Typename (inside cast or sizeof) */
42:
43: #define NULL 0
1.1.1.3 root 44: #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
45: #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
1.1 root 46:
47: static tree grokparms (), grokdeclarator ();
1.1.1.2 root 48: tree pushdecl ();
49: tree make_index_type ();
1.1 root 50: static void builtin_function ();
51:
52: static tree lookup_tag ();
1.1.1.2 root 53: static tree lookup_tag_reverse ();
1.1 root 54: static tree lookup_name_current_level ();
1.1.1.2 root 55: static char *redeclaration_error_message ();
1.1 root 56:
57: /* a node which has tree code ERROR_MARK, and whose type is itself.
58: All erroneous expressions are replaced with this node. All functions
59: that accept nodes as arguments should avoid generating error messages
60: if this node is one of the arguments, since it is undesirable to get
61: multiple error messages from one error in the input. */
62:
63: tree error_mark_node;
64:
65: /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
66:
67: tree short_integer_type_node;
68: tree integer_type_node;
69: tree long_integer_type_node;
1.1.1.5 ! root 70: tree long_long_integer_type_node;
1.1 root 71:
72: tree short_unsigned_type_node;
73: tree unsigned_type_node;
74: tree long_unsigned_type_node;
1.1.1.5 ! root 75: tree long_long_unsigned_type_node;
1.1 root 76:
77: tree unsigned_char_type_node;
1.1.1.2 root 78: tree signed_char_type_node;
1.1 root 79: tree char_type_node;
80:
81: tree float_type_node;
82: tree double_type_node;
83: tree long_double_type_node;
84:
85: /* a VOID_TYPE node. */
86:
87: tree void_type_node;
88:
89: /* A node for type `void *'. */
90:
91: tree ptr_type_node;
92:
93: /* A node for type `char *'. */
94:
95: tree string_type_node;
96:
97: /* Type `char[256]' or something like it.
98: Used when an array of char is needed and the size is irrelevant. */
99:
100: tree char_array_type_node;
101:
1.1.1.2 root 102: /* Type `int[256]' or something like it.
103: Used when an array of int needed and the size is irrelevant. */
104:
105: tree int_array_type_node;
106:
1.1 root 107: /* type `int ()' -- used for implicit declaration of functions. */
108:
109: tree default_function_type;
110:
111: /* function types `double (double)' and `double (double, double)', etc. */
112:
113: tree double_ftype_double, double_ftype_double_double;
114: tree int_ftype_int, long_ftype_long;
115:
116: /* Function type `void (void *, void *, int)' and similar ones */
117:
118: tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
119:
120: /* Two expressions that are constants with value zero.
121: The first is of type `int', the second of type `void *'. */
122:
123: tree integer_zero_node;
124: tree null_pointer_node;
125:
126: /* A node for the integer constant 1. */
127:
128: tree integer_one_node;
129:
130: /* An identifier whose name is <value>. This is used as the "name"
131: of the RESULT_DECLs for values of functions. */
132:
133: tree value_identifier;
134:
1.1.1.2 root 135: /* While defining an enum type, this is 1 plus the last enumerator
136: constant value. */
1.1 root 137:
1.1.1.2 root 138: static tree enum_next_value;
1.1 root 139:
1.1.1.2 root 140: /* Parsing a function declarator leaves a list of parameter names
141: or a chain or parameter decls here. */
1.1 root 142:
1.1.1.2 root 143: static tree last_function_parms;
1.1 root 144:
1.1.1.2 root 145: /* Parsing a function declarator leaves here a chain of structure
146: and enum types declared in the parmlist. */
1.1 root 147:
1.1.1.2 root 148: static tree last_function_parm_tags;
149:
150: /* After parsing the declarator that starts a function definition,
151: `start_function' puts here the list of parameter names or chain of decls.
152: `store_parm_decls' finds it here. */
153:
154: static tree current_function_parms;
155:
156: /* Similar, for last_function_parm_tags. */
157: static tree current_function_parm_tags;
1.1 root 158:
159: /* A list (chain of TREE_LIST nodes) of all LABEL_STMTs in the function
160: that have names. Here so we can clear out their names' definitions
161: at the end of the function. */
162:
163: static tree named_labels;
164:
1.1.1.2 root 165: /* The FUNCTION_DECL for the function currently being compiled,
166: or 0 if between functions. */
167: tree current_function_decl;
1.1 root 168:
169: /* Set to 0 at beginning of a function definition, set to 1 if
170: a return statement that specifies a return value is seen. */
171:
172: int current_function_returns_value;
1.1.1.2 root 173:
174: /* Set to 0 at beginning of a function definition, set to 1 if
175: a return statement with no argument is seen. */
176:
177: int current_function_returns_null;
178:
179: /* Set to nonzero by `grokdeclarator' for a function
180: whose return type is defaulted, if warnings for this are desired. */
181:
182: static int warn_about_return_type;
1.1 root 183:
184: /* For each binding contour we allocate a binding_level structure
185: * which records the names defined in that contour.
186: * Contours include:
187: * 0) the global one
188: * 1) one for each function definition,
189: * where internal declarations of the parameters appear.
190: * 2) one for each compound statement,
191: * to record its declarations.
192: *
193: * The current meaning of a name can be found by searching the levels from
194: * the current one out to the global one.
195: */
196:
197: /* Note that the information in the `names' component of the global contour
198: is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
199:
1.1.1.2 root 200: struct binding_level
1.1 root 201: {
202: /* A chain of _DECL nodes for all variables, constants, functions,
203: and typedef types. These are in the reverse of the order supplied.
204: */
205: tree names;
206:
207: /* A list of structure, union and enum definitions,
208: * for looking up tag names.
209: * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
210: * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
211: * or ENUMERAL_TYPE node.
212: */
213: tree tags;
214:
215: /* For each level, a list of shadowed outer-level local definitions
216: to be restored when this level is popped.
217: Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
218: whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
219: tree shadowed;
220:
1.1.1.2 root 221: /* For each level (except not the global one),
222: a chain of LET_STMT nodes for all the levels
223: that were entered and exited one level down. */
224: tree blocks;
225:
1.1 root 226: /* The binding level which this one is contained in (inherits from). */
227: struct binding_level *level_chain;
1.1.1.2 root 228:
229: /* Nonzero for the level that holds the parameters of a function. */
230: char parm_flag;
231:
232: /* Nonzero if this level "doesn't exist" for tags. */
233: char tag_transparent;
234:
235: /* Number of decls in `names' that have incomplete
236: structure or union types. */
237: int n_incomplete;
1.1 root 238: };
239:
240: #define NULL_BINDING_LEVEL (struct binding_level *) NULL
241:
242: /* The binding level currently in effect. */
243:
244: static struct binding_level *current_binding_level;
245:
246: /* A chain of binding_level structures awaiting reuse. */
247:
248: static struct binding_level *free_binding_level;
249:
250: /* The outermost binding level, for names of file scope.
251: This is created when the compiler is started and exists
252: through the entire run. */
253:
254: static struct binding_level *global_binding_level;
255:
256: /* Binding level structures are initialized by copying this one. */
257:
1.1.1.2 root 258: static struct binding_level clear_binding_level
259: = {NULL, NULL, NULL, NULL, NULL, 0, 0, 0};
1.1 root 260:
261: /* Create a new `struct binding_level'. */
262:
263: static
264: struct binding_level *
265: make_binding_level ()
266: {
267: /* NOSTRICT */
268: return (struct binding_level *) xmalloc (sizeof (struct binding_level));
269: }
270:
1.1.1.4 root 271: /* Nonzero if we are currently in the global binding level. */
272:
273: int
274: global_bindings_p ()
275: {
276: return current_binding_level == global_binding_level;
277: }
278:
1.1.1.2 root 279: /* Enter a new binding level.
280: If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
281: not for that of tags. */
1.1 root 282:
283: void
1.1.1.2 root 284: pushlevel (tag_transparent)
285: int tag_transparent;
1.1 root 286: {
287: register struct binding_level *newlevel = NULL_BINDING_LEVEL;
288:
289: /* If this is the top level of a function,
1.1.1.2 root 290: just make sure that NAMED_LABELS is 0.
1.1 root 291: They should have been set to 0 at the end of the previous function. */
292:
293: if (current_binding_level == global_binding_level)
294: {
1.1.1.2 root 295: if (named_labels)
1.1 root 296: abort ();
297: }
298:
299: /* Reuse or create a struct for this binding level. */
300:
301: if (free_binding_level)
302: {
303: newlevel = free_binding_level;
304: free_binding_level = free_binding_level->level_chain;
305: }
306: else
307: {
308: newlevel = make_binding_level ();
309: }
310:
311: /* Add this level to the front of the chain (stack) of levels that
312: are active. */
313:
314: *newlevel = clear_binding_level;
315: newlevel->level_chain = current_binding_level;
316: current_binding_level = newlevel;
1.1.1.2 root 317: newlevel->tag_transparent = tag_transparent;
1.1 root 318: }
319:
1.1.1.2 root 320: /* Exit a binding level.
321: Pop the level off, and restore the state of the identifier-decl mappings
322: that were in effect when this level was entered.
323:
324: If KEEP is nonzero, this level had explicit declarations, so
325: and create a "block" (a LET_STMT node) for the level
326: to record its declarations and subblocks for symbol table output.
327:
328: If FUNCTIONBODY is nonzero, this level is the body of a function,
329: so create a block as if KEEP were set and also clear out all
330: label names.
331:
332: If REVERSE is nonzero, reverse the order of decls before putting
333: them into the LET_STMT. */
1.1 root 334:
335: void
1.1.1.2 root 336: poplevel (keep, reverse, functionbody)
337: int keep;
338: int reverse;
339: int functionbody;
1.1 root 340: {
341: register tree link;
1.1.1.2 root 342: /* The chain of decls was accumulated in reverse order.
343: Put it into forward order, just for cleanliness. */
344: tree decls;
345: tree tags = current_binding_level->tags;
346: tree subblocks = current_binding_level->blocks;
347: tree block = 0;
348:
349: /* This warning is turned off because it causes warnings for
350: declarations like `extern struct foo *x'. */
351: #if 0
352: /* Warn about incomplete structure types in this level. */
353: for (link = tags; link; link = TREE_CHAIN (link))
354: if (TYPE_SIZE (TREE_VALUE (link)) == 0)
355: {
356: tree type = TREE_VALUE (link);
357: char *errmsg;
358: switch (TREE_CODE (type))
359: {
360: case RECORD_TYPE:
361: errmsg = "`struct %s' incomplete in scope ending here";
362: break;
363: case UNION_TYPE:
364: errmsg = "`union %s' incomplete in scope ending here";
365: break;
366: case ENUMERAL_TYPE:
367: errmsg = "`enum %s' incomplete in scope ending here";
368: break;
369: }
370: if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
371: error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
372: else
373: /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
374: error (errmsg, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
375: }
376: #endif /* 0 */
377:
378: /* Get the decls in the order they were written.
379: Usually current_binding_level->names is in reverse order.
380: But parameter decls were previously put in forward order. */
381:
382: if (reverse)
383: current_binding_level->names
384: = decls = nreverse (current_binding_level->names);
385: else
386: decls = current_binding_level->names;
387:
388: /* If there were any declarations or structure tags in that level,
389: or if this level is a function body,
390: create a LET_STMT to record them for the life of this function. */
1.1 root 391:
1.1.1.2 root 392: if (keep || functionbody)
393: block = build_let (0, 0, keep ? decls : 0,
394: subblocks, 0, keep ? tags : 0);
1.1 root 395:
1.1.1.2 root 396: /* In each subblock, record that this is its superior. */
397:
398: for (link = subblocks; link; link = TREE_CHAIN (link))
399: STMT_SUPERCONTEXT (link) = block;
400:
401: /* Clear out the meanings of the local variables of this level;
402: also record in each decl which block it belongs to. */
403:
404: for (link = decls; link; link = TREE_CHAIN (link))
405: {
406: if (DECL_NAME (link) != 0)
407: IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
408: DECL_CONTEXT (link) = block;
409: }
1.1 root 410:
411: /* Restore all name-meanings of the outer levels
412: that were shadowed by this level. */
413:
414: for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
415: IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
416:
417: /* If the level being exited is the top level of a function,
1.1.1.2 root 418: check over all the labels. */
1.1 root 419:
1.1.1.2 root 420: if (functionbody)
1.1 root 421: {
1.1.1.2 root 422: /* Clear out the definitions of all label names,
423: since their scopes end here. */
1.1 root 424:
1.1.1.2 root 425: for (link = named_labels; link; link = TREE_CHAIN (link))
1.1 root 426: {
1.1.1.2 root 427: if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0)
1.1 root 428: {
1.1.1.2 root 429: error ("label `%s' used somewhere above but not defined",
430: IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (link))));
431: /* Avoid crashing later. */
432: define_label (input_filename, 1, DECL_NAME (TREE_VALUE (link)));
1.1 root 433: }
1.1.1.2 root 434: IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0;
1.1 root 435: }
436:
437: named_labels = 0;
438: }
439:
440: /* Pop the current level, and free the structure for reuse. */
441:
442: {
443: register struct binding_level *level = current_binding_level;
444: current_binding_level = current_binding_level->level_chain;
445:
446: level->level_chain = free_binding_level;
447: free_binding_level = level;
448: }
1.1.1.2 root 449:
450: if (functionbody)
451: {
452: DECL_INITIAL (current_function_decl) = block;
453: /* If this is the top level block of a function,
454: the vars are the function's parameters.
455: Don't leave them in the LET_STMT because they are
456: found in the FUNCTION_DECL instead. */
457: STMT_VARS (block) = 0;
458: }
459: else if (block)
460: current_binding_level->blocks
461: = chainon (current_binding_level->blocks, block);
462: /* If we did not make a block for the level just exited,
463: any blocks made for inner levels
464: (since they cannot be recorded as subblocks in that level)
465: must be carried forward so they will later become subblocks
466: of something else. */
467: else if (subblocks)
468: current_binding_level->blocks
469: = chainon (current_binding_level->blocks, subblocks);
470:
1.1 root 471: }
472:
473: /* Push a definition of struct, union or enum tag "name".
474: "type" should be the type node.
1.1.1.2 root 475: We assume that the tag "name" is not already defined.
476:
1.1 root 477: Note that the definition may really be just a forward reference.
478: In that case, the TYPE_SIZE will be zero. */
479:
480: void
481: pushtag (name, type)
482: tree name, type;
483: {
1.1.1.2 root 484: register struct binding_level *b = current_binding_level;
485: while (b->tag_transparent) b = b->level_chain;
1.1 root 486:
487: if (name)
488: {
489: /* Record the identifier as the type's name if it has none. */
490:
491: if (TYPE_NAME (type) == 0)
492: TYPE_NAME (type) = name;
493:
1.1.1.2 root 494: b->tags = tree_cons (name, type, b->tags);
495: }
1.1 root 496: }
1.1.1.2 root 497:
498: /* Handle when a new declaration NEW has the same name as an old one OLD
499: in the same binding contour. Prints an error message if appropriate.
500:
501: If safely possible, alter OLD to look like NEW, and return 1.
502: Otherwise, return 0. */
503:
504: static int
505: duplicate_decls (new, old)
506: register tree new, old;
507: {
508: int types_match = comptypes (TREE_TYPE (new), TREE_TYPE (old));
509:
510: /* If this decl has linkage, and the old one does too, maybe no error. */
511: if (TREE_CODE (old) != TREE_CODE (new))
1.1.1.4 root 512: {
513: error_with_decl (new, "`%s' redeclared as different kind of symbol");
514: error_with_decl (old, "previous declaration of `%s'");
515: }
1.1.1.2 root 516: else
517: {
518: if (!types_match)
1.1.1.4 root 519: {
520: error_with_decl (new, "conflicting types for `%s'");
521: error_with_decl (old, "previous declaration of `%s'");
522: }
1.1.1.2 root 523: else
524: {
525: char *errmsg = redeclaration_error_message (new, old);
526: if (errmsg)
1.1.1.4 root 527: {
528: error_with_decl (new, errmsg);
529: error_with_decl (old, "previous declaration of `%s'");
530: }
1.1.1.2 root 531: }
532: }
1.1 root 533:
1.1.1.2 root 534: if (TREE_CODE (old) == TREE_CODE (new))
535: {
536: /* Copy all the DECL_... slots specified in the new decl
537: except for any that we copy here from the old type. */
1.1 root 538:
1.1.1.2 root 539: if (types_match)
540: {
541: tree oldtype = TREE_TYPE (old);
542: /* Merge the data types specified in the two decls. */
543: TREE_TYPE (new)
544: = TREE_TYPE (old) = commontype (TREE_TYPE (new), TREE_TYPE (old));
1.1 root 545:
1.1.1.2 root 546: /* Lay the type out, unless already done. */
547: if (oldtype != TREE_TYPE (new))
548: {
549: if (TREE_TYPE (new) != error_mark_node)
550: layout_type (TREE_TYPE (new));
551: if (TREE_CODE (new) != FUNCTION_DECL
552: && TREE_CODE (new) != TYPE_DECL
553: && TREE_CODE (new) != CONST_DECL)
554: layout_decl (new);
555: }
556: else
557: {
558: /* Since the type is OLD's, make OLD's size go with. */
559: DECL_SIZE (new) = DECL_SIZE (old);
560: DECL_SIZE_UNIT (new) = DECL_SIZE_UNIT (old);
561: }
1.1 root 562:
1.1.1.2 root 563: /* Merge the initialization information. */
564: if (DECL_INITIAL (new) == 0)
565: DECL_INITIAL (new) = DECL_INITIAL (old);
566: /* Keep the old rtl since we can safely use it. */
567: DECL_RTL (new) = DECL_RTL (old);
568: }
569: /* If cannot merge, then use the new type
570: and discard the old rtl. */
571: else
572: TREE_TYPE (old) = TREE_TYPE (new);
573:
574: /* Merge the storage class information. */
575: if (TREE_EXTERNAL (new))
576: {
577: TREE_STATIC (new) = TREE_STATIC (old);
578: TREE_PUBLIC (new) = TREE_PUBLIC (old);
579: TREE_EXTERNAL (new) = TREE_EXTERNAL (old);
580: }
581: else
582: {
583: TREE_STATIC (old) = TREE_STATIC (new);
584: TREE_EXTERNAL (old) = 0;
585: TREE_PUBLIC (old) = TREE_PUBLIC (new);
586: }
587: /* If either decl says `inline', this fn is inline,
588: unless its definition was passed already. */
589: if (TREE_INLINE (new) && DECL_INITIAL (old) == 0)
590: TREE_INLINE (old) = 1;
591:
592: bcopy ((char *) new + sizeof (struct tree_common),
593: (char *) old + sizeof (struct tree_common),
594: sizeof (struct tree_decl) - sizeof (struct tree_common));
595: return 1;
1.1 root 596: }
1.1.1.2 root 597:
598: /* New decl is completely inconsistent with the old one =>
599: tell caller to replace the old one. */
600: return 0;
1.1 root 601: }
602:
603: /* Record a decl-node X as belonging to the current lexical scope.
604: Check for errors (such as an incompatible declaration for the same
605: name already seen in the same scope).
606:
607: Returns either X or an old decl for the same name.
1.1.1.2 root 608: If an old decl is returned, it may have been smashed
1.1 root 609: to agree with what X says. */
610:
611: tree
612: pushdecl (x)
613: tree x;
614: {
615: register tree t;
616: register tree name = DECL_NAME (x);
617:
618: if (name)
619: {
620: t = lookup_name_current_level (name);
1.1.1.2 root 621: if (t && duplicate_decls (x, t))
622: return t;
1.1 root 623:
624: /* If declaring a type as a typedef, and the type has no known
625: typedef name, install this TYPE_DECL as its typedef name. */
626: if (TREE_CODE (x) == TYPE_DECL)
1.1.1.3 root 627: if (TYPE_NAME (TREE_TYPE (x)) == 0)
1.1 root 628: TYPE_NAME (TREE_TYPE (x)) = x;
629:
1.1.1.2 root 630: /* This name is new in its binding level.
631: Install the new declaration and return it. */
632: if (current_binding_level == global_binding_level
633: /* In PCC-compatibility mode, extern decls
634: take effect at top level no matter where they are. */
635: || (flag_traditional && TREE_EXTERNAL (x)
636: && lookup_name (name) == 0))
637: {
638: /* Install a global value. */
639:
640: IDENTIFIER_GLOBAL_VALUE (name) = x;
641:
642: if (IDENTIFIER_IMPLICIT_DECL (name) != 0
643: /* If this real decl matches the implicit, don't complain. */
644: && ! (TREE_CODE (x) == FUNCTION_DECL
645: && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
646: warning ("`%s' was previously implicitly declared to return `int'",
647: IDENTIFIER_POINTER (name));
1.1.1.4 root 648:
649: /* If this decl is `static' and an `extern' was seen previously,
650: that is erroneous. */
651: if (TREE_PUBLIC (name)
652: && ! TREE_PUBLIC (x) && ! TREE_EXTERNAL (x))
653: warning ("`%s' was declared `extern' and later `static'",
654: IDENTIFIER_POINTER (name));
1.1.1.2 root 655: }
656: else
657: {
1.1.1.3 root 658: /* Here to install a non-global value. */
659: tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
660: IDENTIFIER_LOCAL_VALUE (name) = x;
661:
662: /* If this is an extern function declaration, see if we
663: have a global definition for the function. */
664: if (oldlocal == 0
665: && IDENTIFIER_GLOBAL_VALUE (name)
666: && TREE_CODE (x) == FUNCTION_DECL
667: && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL
668: && TREE_INLINE (IDENTIFIER_GLOBAL_VALUE (name)))
669: {
670: /* We have one. Their types must agree. */
671: if (! comptypes (TREE_TYPE (x),
672: TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name))))
673: warning_with_decl (x, "local declaration of `%s' doesn't match global one");
674: /* If the global one is inline, make the local one inline. */
675: else if (TREE_INLINE (IDENTIFIER_GLOBAL_VALUE (name)))
676: IDENTIFIER_LOCAL_VALUE (name) = IDENTIFIER_GLOBAL_VALUE (name);
677: }
1.1.1.4 root 678: /* If we have a local external declaration,
679: and no file-scope declaration has yet been seen,
680: then if we later have a file-scope decl it may not be static. */
681: if (oldlocal == 0
682: && IDENTIFIER_GLOBAL_VALUE (name) == 0
683: && TREE_EXTERNAL (x))
684: {
685: TREE_PUBLIC (name) = 1;
686: }
1.1.1.3 root 687: /* Warn if shadowing an argument. */
688: if (oldlocal != 0
689: && TREE_CODE (oldlocal) == PARM_DECL
1.1.1.2 root 690: && current_binding_level->level_chain->parm_flag)
691: warning ("shadowing parameter `%s' with a local variable",
692: IDENTIFIER_POINTER (name));
693: /* If storing a local value, there may already be one (inherited).
694: If so, record it for restoration when this binding level ends. */
1.1.1.3 root 695: if (oldlocal != 0)
1.1.1.2 root 696: current_binding_level->shadowed
1.1.1.3 root 697: = tree_cons (name, oldlocal,
1.1.1.2 root 698: current_binding_level->shadowed);
699: }
700:
701: /* Keep count of variables in this level with incomplete type. */
702: if (TYPE_SIZE (TREE_TYPE (x)) == 0)
703: ++current_binding_level->n_incomplete;
1.1 root 704: }
705:
706: /* Put decls on list in reverse order.
707: We will reverse them later if necessary. */
1.1.1.2 root 708: TREE_CHAIN (x) = current_binding_level->names;
709: current_binding_level->names = x;
1.1 root 710:
711: return x;
712: }
1.1.1.2 root 713:
714: /* Generate an implicit declaration for identifier FUNCTIONID
715: as a function of type int (). Print a warning if appropriate. */
1.1 root 716:
1.1.1.2 root 717: tree
718: implicitly_declare (functionid)
719: tree functionid;
1.1 root 720: {
1.1.1.2 root 721: register tree decl;
1.1 root 722:
1.1.1.4 root 723: /* Save the decl permanently so we can warn if definition follows. */
724: end_temporary_allocation ();
1.1.1.2 root 725:
1.1.1.4 root 726: if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0)
727: decl = IDENTIFIER_IMPLICIT_DECL (functionid);
728: else
729: decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1.1.1.2 root 730:
731: TREE_EXTERNAL (decl) = 1;
732: TREE_PUBLIC (decl) = 1;
733:
734: /* ANSI standard says implicit declarations are in the innermost block.
735: So we record the decl in the standard fashion.
736: If flag_traditional is set, pushdecl does it top-level. */
737: pushdecl (decl);
738: rest_of_decl_compilation (decl, 0, 0, 0);
739:
740: if (warn_implicit
741: /* Only one warning per identifier. */
742: && IDENTIFIER_IMPLICIT_DECL (functionid) == 0)
743: warning ("implicit declaration of function `%s'",
744: IDENTIFIER_POINTER (functionid));
745:
746: IDENTIFIER_IMPLICIT_DECL (functionid) = decl;
747:
1.1.1.4 root 748: resume_temporary_allocation ();
1.1.1.2 root 749:
750: return decl;
751: }
752:
753: /* Return zero if the declaration NEW is valid
754: when the declaration OLD (assumed to be for the same name)
755: has already been seen.
756: Otherwise return an error message format string with a %s
757: where the identifier should go. */
1.1 root 758:
1.1.1.2 root 759: static char *
760: redeclaration_error_message (new, old)
761: tree new, old;
762: {
763: if (TREE_CODE (new) == TYPE_DECL)
764: return "redefinition of `%s'";
765: else if (TREE_CODE (new) == FUNCTION_DECL)
766: {
767: /* Declarations of functions can insist on internal linkage
768: but they can't be inconsistent with internal linkage,
769: so there can be no error on that account.
770: However defining the same name twice is no good. */
771: if (DECL_INITIAL (old) != 0 && DECL_INITIAL (new) != 0)
772: return "redefinition of `%s'";
773: return 0;
774: }
775: else if (current_binding_level == global_binding_level)
776: {
777: /* Objects declared at top level: */
778: /* If at least one is a reference, it's ok. */
779: if (TREE_EXTERNAL (new) || TREE_EXTERNAL (old))
780: return 0;
781: /* Reject two definitions. */
782: if (DECL_INITIAL (old) != 0 && DECL_INITIAL (new) != 0)
783: return "redefinition of `%s'";
784: /* Now we have two tentative defs, or one tentative and one real def. */
785: /* Insist that the linkage match. */
786: if (TREE_PUBLIC (old) != TREE_PUBLIC (new))
787: return "conflicting declarations of `%s'";
788: return 0;
789: }
1.1 root 790: else
1.1.1.2 root 791: {
792: /* Objects declared with block scope: */
793: /* Reject two definitions, and reject a definition
794: together with an external reference. */
795: if (!(TREE_EXTERNAL (new) && TREE_EXTERNAL (old)))
796: return "redeclaration of `%s'";
797: return 0;
798: }
799: }
800:
801: /* Get the LABEL_DECL corresponding to identifier ID as a label.
802: Create one if none exists so far for the current function.
803: This function is called for both label definitions and label references. */
804:
805: tree
806: lookup_label (id)
807: tree id;
808: {
809: register tree decl = IDENTIFIER_LABEL_VALUE (id);
810:
811: if (decl != 0)
812: return decl;
813:
814: decl = build_decl (LABEL_DECL, id, NULL_TREE);
815: DECL_MODE (decl) = VOIDmode;
816: /* Mark that the label's definition has not been seen. */
817: DECL_SOURCE_LINE (decl) = 0;
818:
819: IDENTIFIER_LABEL_VALUE (id) = decl;
1.1 root 820:
821: named_labels
1.1.1.2 root 822: = tree_cons (NULL_TREE, decl, named_labels);
823:
824: return decl;
1.1 root 825: }
826:
1.1.1.2 root 827: /* Define a label, specifying the location in the source file.
828: Return the LABEL_DECL node for the label, if the definition is valid.
829: Otherwise return 0. */
1.1 root 830:
1.1.1.2 root 831: tree
832: define_label (filename, line, name)
833: char *filename;
834: int line;
835: tree name;
1.1 root 836: {
1.1.1.2 root 837: tree decl = lookup_label (name);
838: if (DECL_SOURCE_LINE (decl) != 0)
839: {
840: error_with_decl (decl, "duplicate label `%s'");
841: return 0;
842: }
843: else
844: {
845: /* Mark label as having been defined. */
846: DECL_SOURCE_FILE (decl) = filename;
847: DECL_SOURCE_LINE (decl) = line;
848: return decl;
849: }
1.1 root 850: }
851:
852: /* Return the list of declarations of the current level.
1.1.1.2 root 853: Note that this list is in reverse order unless/until
854: you nreverse it; and when you do nreverse it, you must
855: store the result back using `storedecls' or you will lose. */
1.1 root 856:
857: tree
858: getdecls ()
859: {
1.1.1.2 root 860: return current_binding_level->names;
1.1 root 861: }
862:
863: /* Return the list of type-tags (for structs, etc) of the current level. */
864:
865: tree
866: gettags ()
867: {
868: return current_binding_level->tags;
869: }
870:
871: /* Store the list of declarations of the current level.
872: This is done for the parameter declarations of a function being defined,
873: after they are modified in the light of any missing parameters. */
874:
1.1.1.2 root 875: static void
1.1 root 876: storedecls (decls)
877: tree decls;
878: {
879: current_binding_level->names = decls;
880: }
1.1.1.2 root 881:
882: /* Similarly, store the list of tags of the current level. */
883:
884: static void
885: storetags (tags)
886: tree tags;
887: {
888: current_binding_level->tags = tags;
889: }
1.1 root 890:
891: /* Given NAME, an IDENTIFIER_NODE,
892: return the structure (or union or enum) definition for that name.
893: Searches binding levels from BINDING_LEVEL up to the global level.
1.1.1.2 root 894: If THISLEVEL_ONLY is nonzero, searches only the specified context
895: (but skips any tag-transparent contexts to find one that is
896: meaningful for tags).
1.1 root 897: FORM says which kind of type the caller wants;
898: it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
899: If the wrong kind of type is found, an error is reported. */
900:
901: static tree
902: lookup_tag (form, name, binding_level, thislevel_only)
903: enum tree_code form;
904: struct binding_level *binding_level;
905: tree name;
906: int thislevel_only;
907: {
908: register struct binding_level *level;
909:
910: for (level = binding_level; level; level = level->level_chain)
911: {
912: register tree tail;
913: for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
914: {
915: if (TREE_PURPOSE (tail) == name)
916: {
917: if (TREE_CODE (TREE_VALUE (tail)) != form)
918: {
919: /* Definition isn't the kind we were looking for. */
1.1.1.2 root 920: error ("`%s' defined as wrong kind of tag",
921: IDENTIFIER_POINTER (name));
1.1 root 922: }
923: return TREE_VALUE (tail);
924: }
925: }
1.1.1.2 root 926: if (thislevel_only && ! level->tag_transparent)
1.1 root 927: return NULL_TREE;
928: }
929: return NULL_TREE;
930: }
931:
1.1.1.2 root 932: /* Given a type, find the tag that was defined for it and return the tag name.
933: Otherwise return 0. However, the value can never be 0
934: in the cases in which this is used. */
935:
936: static tree
937: lookup_tag_reverse (type)
938: tree type;
939: {
940: register struct binding_level *level;
941:
942: for (level = current_binding_level; level; level = level->level_chain)
943: {
944: register tree tail;
945: for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
946: {
947: if (TREE_VALUE (tail) == type)
948: return TREE_PURPOSE (tail);
949: }
950: }
951: return NULL_TREE;
952: }
953:
1.1 root 954: /* Look up NAME in the current binding level and its superiors
955: in the namespace of variables, functions and typedefs.
956: Return a ..._DECL node of some kind representing its definition,
957: or return 0 if it is undefined. */
958:
959: tree
960: lookup_name (name)
961: tree name;
962: {
1.1.1.2 root 963: register tree val;
1.1 root 964: if (current_binding_level != global_binding_level
965: && IDENTIFIER_LOCAL_VALUE (name))
1.1.1.2 root 966: val = IDENTIFIER_LOCAL_VALUE (name);
967: else
968: val = IDENTIFIER_GLOBAL_VALUE (name);
969: if (val && TREE_TYPE (val) == error_mark_node)
970: return error_mark_node;
971: return val;
1.1 root 972: }
973:
974: /* Similar to `lookup_name' but look only at current binding level. */
975:
976: static tree
977: lookup_name_current_level (name)
978: tree name;
979: {
980: register tree t;
981:
982: if (current_binding_level == global_binding_level)
983: return IDENTIFIER_GLOBAL_VALUE (name);
984:
985: if (IDENTIFIER_LOCAL_VALUE (name) == 0)
986: return 0;
987:
988: for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
989: if (DECL_NAME (t) == name)
990: break;
991:
992: return t;
993: }
994:
995: /* Create the predefined scalar types of C,
996: and some nodes representing standard constants (0, 1, (void *)0).
997: Initialize the global binding level.
998: Make definitions for built-in primitive functions. */
999:
1000: void
1001: init_decl_processing ()
1002: {
1003: register tree endlink;
1004:
1.1.1.2 root 1005: current_function_decl = NULL;
1.1 root 1006: named_labels = NULL;
1007: current_binding_level = NULL_BINDING_LEVEL;
1008: free_binding_level = NULL_BINDING_LEVEL;
1.1.1.2 root 1009: pushlevel (0); /* make the binding_level structure for global names */
1.1 root 1010: global_binding_level = current_binding_level;
1011:
1012: value_identifier = get_identifier ("<value>");
1013:
1.1.1.2 root 1014: /* Define `int' and `char' first so that dbx will output them first. */
1.1 root 1015:
1.1.1.2 root 1016: #ifdef INT_TYPE_SIZE
1017: integer_type_node = make_signed_type (INT_TYPE_SIZE);
1018: #else
1.1 root 1019: integer_type_node = make_signed_type (BITS_PER_WORD);
1.1.1.2 root 1020: #endif
1.1 root 1021: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
1.1.1.2 root 1022: integer_type_node));
1023:
1024: /* Define `char', which is like either `signed char' or `unsigned char'
1025: but not the same as either. */
1026:
1027: char_type_node =
1028: (flag_signed_char
1029: ? make_signed_type (BITS_PER_UNIT)
1030: : make_unsigned_type (BITS_PER_UNIT));
1031: pushdecl (build_decl (TYPE_DECL, get_identifier ("char"),
1032: char_type_node));
1033:
1034: #ifdef INT_TYPE_SIZE
1035: unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
1036: #else
1037: unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
1038: #endif
1039: pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
1040: unsigned_type_node));
1041:
1.1.1.3 root 1042: long_unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
1043: pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
1044: long_unsigned_type_node));
1045:
1046: /* `unsigned long' or `unsigned int' is the type for sizeof. */
1047: #ifdef INT_TYPE_SIZE
1048: if (INT_TYPE_SIZE != BITS_PER_WORD)
1049: sizetype = long_unsigned_type_node;
1050: else
1051: sizetype = unsigned_type_node;
1052: #else
1.1.1.2 root 1053: sizetype = unsigned_type_node;
1.1.1.3 root 1054: #endif
1055:
1.1.1.2 root 1056: TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype;
1057: TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype;
1058: TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype;
1.1.1.3 root 1059: TREE_TYPE (TYPE_SIZE (long_unsigned_type_node)) = sizetype;
1.1 root 1060:
1061: error_mark_node = make_node (ERROR_MARK);
1062: TREE_TYPE (error_mark_node) = error_mark_node;
1063:
1.1.1.3 root 1064: short_integer_type_node = make_signed_type (BITS_PER_UNIT * MIN (UNITS_PER_WORD / 2, 2));
1.1 root 1065: pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
1.1.1.2 root 1066: short_integer_type_node));
1.1 root 1067:
1068: long_integer_type_node = make_signed_type (BITS_PER_WORD);
1069: pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
1.1.1.2 root 1070: long_integer_type_node));
1.1 root 1071:
1.1.1.5 ! root 1072: long_long_integer_type_node = make_signed_type (2 * BITS_PER_WORD);
! 1073: pushdecl (build_decl (TYPE_DECL, get_identifier ("long long int"),
! 1074: long_long_integer_type_node));
! 1075:
1.1.1.3 root 1076: short_unsigned_type_node = make_unsigned_type (BITS_PER_UNIT * MIN (UNITS_PER_WORD / 2, 2));
1.1 root 1077: pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
1.1.1.2 root 1078: short_unsigned_type_node));
1.1 root 1079:
1.1.1.5 ! root 1080: long_long_unsigned_type_node = make_unsigned_type (2 * BITS_PER_WORD);
! 1081: pushdecl (build_decl (TYPE_DECL, get_identifier ("long long unsigned int"),
! 1082: long_long_unsigned_type_node));
! 1083:
1.1.1.2 root 1084: /* Define both `signed char' and `unsigned char'. */
1085: signed_char_type_node = make_signed_type (BITS_PER_UNIT);
1086: pushdecl (build_decl (TYPE_DECL, get_identifier ("signed char"),
1087: signed_char_type_node));
1.1 root 1088:
1089: unsigned_char_type_node = make_unsigned_type (BITS_PER_UNIT);
1090: pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
1.1.1.2 root 1091: unsigned_char_type_node));
1.1 root 1092:
1093: float_type_node = make_node (REAL_TYPE);
1094: TYPE_PRECISION (float_type_node) = BITS_PER_WORD;
1095: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
1.1.1.2 root 1096: float_type_node));
1.1 root 1097: layout_type (float_type_node);
1098:
1099: double_type_node = make_node (REAL_TYPE);
1100: TYPE_PRECISION (double_type_node) = 2 * BITS_PER_WORD;
1101: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
1.1.1.2 root 1102: double_type_node));
1.1 root 1103: layout_type (double_type_node);
1104:
1105: long_double_type_node = make_node (REAL_TYPE);
1106: TYPE_PRECISION (long_double_type_node) = 2 * BITS_PER_WORD;
1107: pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
1.1.1.2 root 1108: long_double_type_node));
1.1 root 1109: layout_type (long_double_type_node);
1110:
1111: integer_zero_node = build_int_2 (0, 0);
1112: TREE_TYPE (integer_zero_node) = integer_type_node;
1113: integer_one_node = build_int_2 (1, 0);
1114: TREE_TYPE (integer_one_node) = integer_type_node;
1115:
1.1.1.3 root 1116: size_zero_node = build_int_2 (0, 0);
1117: TREE_TYPE (size_zero_node) = sizetype;
1118: size_one_node = build_int_2 (1, 0);
1119: TREE_TYPE (size_one_node) = sizetype;
1120:
1.1 root 1121: void_type_node = make_node (VOID_TYPE);
1122: pushdecl (build_decl (TYPE_DECL,
1.1.1.2 root 1123: ridpointers[(int) RID_VOID], void_type_node));
1.1 root 1124: layout_type (void_type_node); /* Uses integer_zero_node */
1125:
1126: null_pointer_node = build_int_2 (0, 0);
1127: TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
1128: layout_type (TREE_TYPE (null_pointer_node));
1129:
1130: string_type_node = build_pointer_type (char_type_node);
1131:
1132: /* make a type for arrays of 256 characters.
1133: 256 is picked randomly because we have a type for integers from 0 to 255.
1134: With luck nothing will ever really depend on the length of this
1135: array type. */
1136: char_array_type_node
1137: = build_array_type (char_type_node, unsigned_char_type_node);
1.1.1.2 root 1138: /* Likewise for arrays of ints. */
1139: int_array_type_node
1140: = build_array_type (integer_type_node, unsigned_char_type_node);
1.1 root 1141:
1142: default_function_type
1143: = build_function_type (integer_type_node, NULL_TREE);
1144:
1145: ptr_type_node = build_pointer_type (void_type_node);
1146: endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
1147:
1148: double_ftype_double
1149: = build_function_type (double_type_node,
1150: tree_cons (NULL_TREE, double_type_node, endlink));
1151:
1152: double_ftype_double_double
1153: = build_function_type (double_type_node,
1154: tree_cons (NULL_TREE, double_type_node,
1155: tree_cons (NULL_TREE,
1156: double_type_node, endlink)));
1157:
1158: int_ftype_int
1159: = build_function_type (integer_type_node,
1160: tree_cons (NULL_TREE, integer_type_node, endlink));
1161:
1162: long_ftype_long
1163: = build_function_type (long_integer_type_node,
1164: tree_cons (NULL_TREE,
1165: long_integer_type_node, endlink));
1166:
1167: void_ftype_ptr_ptr_int
1168: = build_function_type (void_type_node,
1169: tree_cons (NULL_TREE, ptr_type_node,
1170: tree_cons (NULL_TREE, ptr_type_node,
1171: tree_cons (NULL_TREE,
1172: integer_type_node,
1173: endlink))));
1174:
1175: int_ftype_ptr_ptr_int
1176: = build_function_type (integer_type_node,
1177: tree_cons (NULL_TREE, ptr_type_node,
1178: tree_cons (NULL_TREE, ptr_type_node,
1179: tree_cons (NULL_TREE,
1180: integer_type_node,
1181: endlink))));
1182:
1183: void_ftype_ptr_int_int
1184: = build_function_type (void_type_node,
1185: tree_cons (NULL_TREE, ptr_type_node,
1186: tree_cons (NULL_TREE, integer_type_node,
1187: tree_cons (NULL_TREE,
1188: integer_type_node,
1189: endlink))));
1190:
1.1.1.2 root 1191: builtin_function ("__builtin_alloca",
1.1 root 1192: build_function_type (ptr_type_node,
1193: tree_cons (NULL_TREE,
1194: integer_type_node,
1195: endlink)),
1196: BUILT_IN_ALLOCA);
1197:
1.1.1.2 root 1198: builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS);
1199: builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS);
1200: builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS);
1201: builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS);
1202: /* builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV);
1203: builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV); */
1204: builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR);
1205: builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL);
1206: builtin_function ("__builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD);
1207: builtin_function ("__builtin_frem", double_ftype_double_double, BUILT_IN_FREM);
1208: builtin_function ("__builtin_memcpy", void_ftype_ptr_ptr_int, BUILT_IN_MEMCPY);
1209: builtin_function ("__builtin_memcmp", int_ftype_ptr_ptr_int, BUILT_IN_MEMCMP);
1210: builtin_function ("__builtin_memset", void_ftype_ptr_int_int, BUILT_IN_MEMSET);
1211: builtin_function ("__builtin_fsqrt", double_ftype_double, BUILT_IN_FSQRT);
1212: builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP);
1213: builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN);
1.1 root 1214: }
1215:
1216: /* Make a definition for a builtin function named NAME and whose data type
1217: is TYPE. TYPE should be a function type with argument types.
1218: FUNCTION_CODE tells later passes how to compile calls to this function.
1219: See tree.h for its possible values. */
1220:
1221: static void
1222: builtin_function (name, type, function_code)
1223: char *name;
1224: tree type;
1225: enum built_in_function function_code;
1226: {
1.1.1.2 root 1227: tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
1228: TREE_EXTERNAL (decl) = 1;
1229: TREE_PUBLIC (decl) = 1;
1.1 root 1230: make_function_rtl (decl);
1231: pushdecl (decl);
1232: DECL_SET_FUNCTION_CODE (decl, function_code);
1233: }
1234:
1235: /* Called when a declaration is seen that contains no names to declare.
1236: If its type is a reference to a structure, union or enum inherited
1237: from a containing scope, shadow that tag name for the current scope
1238: with a forward reference.
1239: If its type defines a new named structure or union
1240: or defines an enum, it is valid but we need not do anything here.
1241: Otherwise, it is an error. */
1242:
1243: void
1244: shadow_tag (declspecs)
1245: tree declspecs;
1246: {
1247: register tree link;
1248:
1249: for (link = declspecs; link; link = TREE_CHAIN (link))
1250: {
1251: register tree value = TREE_VALUE (link);
1252: register enum tree_code code = TREE_CODE (value);
1253: if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
1254: && TYPE_SIZE (value) != 0)
1255: {
1.1.1.2 root 1256: register tree name = lookup_tag_reverse (value);
1.1 root 1257: register tree t = lookup_tag (code, name, current_binding_level, 1);
1258: if (t == 0)
1259: {
1260: t = make_node (code);
1261: pushtag (name, t);
1262: return;
1263: }
1264: if (name != 0 || code == ENUMERAL_TYPE)
1265: return;
1266: }
1267: }
1268: warning ("empty declaration");
1269: }
1270:
1271: /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
1272:
1273: tree
1274: groktypename (typename)
1275: tree typename;
1276: {
1.1.1.2 root 1277: if (TREE_CODE (typename) != TREE_LIST)
1278: return typename;
1.1 root 1279: return grokdeclarator (TREE_VALUE (typename),
1280: TREE_PURPOSE (typename),
1.1.1.2 root 1281: TYPENAME, 0);
1.1 root 1282: }
1283:
1284: /* Decode a declarator in an ordinary declaration or data definition.
1285: This is called as soon as the type information and variable name
1286: have been parsed, before parsing the initializer if any.
1287: Here we create the ..._DECL node, fill in its type,
1288: and put it on the list of decls for the current context.
1289: The ..._DECL node is returned as the value.
1290:
1291: Exception: for arrays where the length is not specified,
1292: the type is left null, to be filled in by `finish_decl'.
1293:
1294: Function definitions do not come here; they go to start_function
1295: instead. However, external and forward declarations of functions
1296: do go through here. Structure field declarations are done by
1297: grokfield and not through here. */
1298:
1.1.1.2 root 1299: /* Set this nonzero to debug not using the temporary obstack
1300: to parse initializers. */
1301: int debug_no_temp_inits = 1;
1302:
1.1 root 1303: tree
1304: start_decl (declarator, declspecs, initialized)
1305: tree declspecs, declarator;
1306: int initialized;
1307: {
1.1.1.2 root 1308: register tree decl = grokdeclarator (declarator, declspecs,
1309: NORMAL, initialized);
1310: register tree tem;
1311: int init_written = initialized;
1312:
1313: if (initialized)
1314: /* Is it valid for this decl to have an initializer at all?
1315: If not, set INITIALIZED to zero, which will indirectly
1316: tell `finish_decl' to ignore the initializer once it is parsed. */
1317: switch (TREE_CODE (decl))
1318: {
1319: case TYPE_DECL:
1320: /* typedef foo = bar means give foo the same type as bar.
1321: We haven't parsed bar yet, so `finish_decl' will fix that up.
1322: Any other case of an initialization in a TYPE_DECL is an error. */
1323: if (pedantic || list_length (declspecs) > 1)
1324: {
1325: error ("typedef `%s' is initialized",
1326: IDENTIFIER_POINTER (DECL_NAME (decl)));
1327: initialized = 0;
1328: }
1329: break;
1330:
1331: case FUNCTION_DECL:
1332: error ("function `%s' is initialized like a variable",
1333: IDENTIFIER_POINTER (DECL_NAME (decl)));
1334: initialized = 0;
1335: break;
1336:
1337: default:
1338: /* Don't allow initializations for incomplete types
1339: except for arrays which might be completed by the initialization. */
1340: if (TYPE_SIZE (TREE_TYPE (decl)) != 0)
1341: ; /* A complete type is ok. */
1342: else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
1343: {
1344: error ("variable `%s' has initializer but incomplete type",
1345: IDENTIFIER_POINTER (DECL_NAME (decl)));
1346: initialized = 0;
1347: }
1348: else if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))) == 0)
1349: {
1350: error ("elements of array `%s' have incomplete type",
1351: IDENTIFIER_POINTER (DECL_NAME (decl)));
1352: initialized = 0;
1353: }
1354: }
1355:
1356: if (initialized)
1357: {
1358: if (current_binding_level != global_binding_level
1359: && TREE_EXTERNAL (decl))
1360: warning ("declaration of `%s' has `extern' and is initialized",
1361: IDENTIFIER_POINTER (DECL_NAME (decl)));
1362: TREE_EXTERNAL (decl) = 0;
1363: if (current_binding_level == global_binding_level)
1364: TREE_STATIC (decl) = 1;
1365:
1366: /* Tell `pushdecl' this is an initialized decl
1367: even though we don't yet have the initializer expression.
1368: Also tell `finish_decl' it may store the real initializer. */
1369: DECL_INITIAL (decl) = error_mark_node;
1370: }
1371:
1372: /* Add this decl to the current binding level. */
1373: tem = pushdecl (decl);
1374:
1375: if (init_written)
1376: {
1377: /* When parsing and digesting the initializer,
1378: use temporary storage. Do this even if we will ignore the value. */
1379: if (current_binding_level == global_binding_level
1380: && debug_no_temp_inits)
1381: temporary_allocation ();
1382: }
1383:
1384: return tem;
1.1 root 1385: }
1386:
1387: /* Finish processing of a declaration;
1388: install its line number and initial value.
1389: If the length of an array type is not known before,
1390: it must be determined now, from the initial value, or it is an error. */
1391:
1392: void
1.1.1.2 root 1393: finish_decl (decl, init, asmspec)
1.1 root 1394: tree decl, init;
1.1.1.2 root 1395: tree asmspec;
1.1 root 1396: {
1397: register tree type = TREE_TYPE (decl);
1.1.1.2 root 1398: int init_written = init != 0;
1.1 root 1399:
1.1.1.2 root 1400: /* If `start_decl' didn't like having an initialization, ignore it now. */
1.1 root 1401:
1.1.1.2 root 1402: if (init != 0 && DECL_INITIAL (decl) == 0)
1403: init = 0;
1404:
1405: if (init)
1406: {
1407: if (TREE_CODE (decl) != TYPE_DECL)
1408: store_init_value (decl, init);
1409: else
1410: {
1411: /* typedef foo = bar; store the type of bar as the type of foo. */
1412: TREE_TYPE (decl) = TREE_TYPE (init);
1413: DECL_INITIAL (decl) = init = 0;
1414: }
1415: }
1416:
1417: /* For top-level declaration, the initial value was read in
1418: the temporary obstack. MAXINDEX, rtl, etc. to be made below
1419: must go in the permanent obstack; but don't discard the
1420: temporary data yet. */
1421:
1422: if (debug_no_temp_inits && init_written
1423: && current_binding_level == global_binding_level)
1424: end_temporary_allocation ();
1425:
1426: /* Deduce size of array from initialization, if not already known */
1.1 root 1427:
1428: if (TREE_CODE (type) == ARRAY_TYPE
1.1.1.2 root 1429: && TYPE_DOMAIN (type) == 0
1430: && TREE_CODE (decl) != TYPE_DECL)
1.1 root 1431: {
1.1.1.2 root 1432: int do_default = ! ((!pedantic && TREE_STATIC (decl))
1433: || TREE_EXTERNAL (decl));
1434: int failure
1435: = complete_array_type (type, DECL_INITIAL (decl), do_default);
1436:
1437: if (failure == 1)
1438: error_with_decl (decl, "initializer fails to determine size of `%s'");
1439:
1440: if (failure == 2)
1441: {
1442: if (do_default)
1443: error_with_decl (decl, "array size missing in `%s'");
1444: else if (!pedantic && TREE_STATIC (decl))
1445: TREE_EXTERNAL (decl) = 1;
1446: }
1447:
1448: if (pedantic && TYPE_DOMAIN (type) != 0
1.1.1.4 root 1449: && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
1450: integer_zero_node))
1.1.1.2 root 1451: error_with_decl (decl, "zero-size array `%s'");
1.1 root 1452:
1.1.1.2 root 1453: if (TREE_CODE (decl) != TYPE_DECL)
1454: layout_decl (decl, 0);
1455: }
1456:
1457: if (TREE_CODE (decl) == VAR_DECL)
1458: {
1459: if (TREE_STATIC (decl) && DECL_SIZE (decl) == 0)
1.1 root 1460: {
1.1.1.2 root 1461: /* A static variable with an incomplete type:
1462: that is an error if it is initialized or `static'.
1463: Otherwise, let it through, but if it is not `extern'
1464: then it may cause an error message later. */
1465: if (! (TREE_PUBLIC (decl) && DECL_INITIAL (decl) == 0))
1466: error_with_decl (decl, "storage size of `%s' isn't known");
1.1 root 1467: }
1.1.1.2 root 1468: else if (!TREE_EXTERNAL (decl) && DECL_SIZE (decl) == 0)
1.1 root 1469: {
1.1.1.2 root 1470: /* An automatic variable with an incomplete type:
1471: that is an error. */
1472: error_with_decl (decl, "storage size of `%s' isn't known");
1473: TREE_TYPE (decl) = error_mark_node;
1.1 root 1474: }
1475:
1.1.1.2 root 1476: if ((TREE_EXTERNAL (decl) || TREE_STATIC (decl))
1477: && DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl)))
1478: error_with_decl (decl, "storage size of `%s' isn't constant");
1479: }
1480:
1481: /* Output the assembler code and/or RTL code for variables and functions,
1482: unless the type is an undefined structure or union.
1483: If not, it will get done when the type is completed. */
1484:
1485: if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
1486: {
1.1.1.5 ! root 1487: if (flag_traditional && allocation_temporary_p ())
! 1488: {
! 1489: end_temporary_allocation ();
! 1490: rest_of_decl_compilation (decl, asmspec,
! 1491: current_binding_level == global_binding_level,
! 1492: 0);
! 1493: resume_temporary_allocation ();
! 1494: }
! 1495: else
! 1496: rest_of_decl_compilation (decl, asmspec,
! 1497: current_binding_level == global_binding_level,
! 1498: 0);
1.1.1.2 root 1499: if (TYPE_SIZE (TREE_TYPE (decl)) != 0
1500: || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1501: if (current_binding_level != global_binding_level)
1502: expand_decl (decl);
1503: }
1504:
1.1.1.3 root 1505: if (TREE_CODE (decl) == TYPE_DECL)
1506: rest_of_decl_compilation (decl, 0,
1507: current_binding_level == global_binding_level,
1508: 0);
1509:
1.1.1.2 root 1510: /* Resume permanent allocation, if not within a function. */
1511: if (debug_no_temp_inits && init_written
1512: && current_binding_level == global_binding_level)
1513: permanent_allocation ();
1514: }
1515:
1516: /* Given a parsed parameter declaration,
1517: decode it into a PARM_DECL and push that on the current binding level. */
1518:
1519: void
1520: push_parm_decl (parm)
1521: tree parm;
1522: {
1523: register tree decl = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1524: PARM, 0);
1525:
1526: /* Add this decl to the current binding level. */
1527: finish_decl (pushdecl (decl), NULL_TREE, NULL_TREE);
1528: }
1529:
1530: /* Make TYPE a complete type based on INITIAL_VALUE.
1531: Return 0 if successful, 1 if INITIAL_VALUE can't be decyphered,
1532: 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
1533:
1534: int
1535: complete_array_type (type, initial_value, do_default)
1536: tree type;
1537: tree initial_value;
1538: int do_default;
1539: {
1540: register tree maxindex = NULL_TREE;
1541: int value = 0;
1542:
1543: if (initial_value)
1544: {
1545: /* Note MAXINDEX is really the maximum index,
1546: one less than the size. */
1547: if (TREE_CODE (initial_value) == STRING_CST)
1548: maxindex = build_int_2 (TREE_STRING_LENGTH (initial_value) - 1, 0);
1549: else if (TREE_CODE (initial_value) == CONSTRUCTOR)
1550: {
1551: register int nelts
1552: = list_length (CONSTRUCTOR_ELTS (initial_value));
1553: maxindex = build_int_2 (nelts - 1, 0);
1554: }
1555: else
1.1 root 1556: {
1.1.1.2 root 1557: /* Make an error message unless that happened already. */
1558: if (initial_value != error_mark_node)
1559: value = 1;
1560:
1561: /* Prevent further error messages. */
1562: maxindex = build_int_2 (1, 0);
1.1 root 1563: }
1.1.1.2 root 1564: }
1.1 root 1565:
1.1.1.2 root 1566: if (!maxindex)
1567: {
1568: if (do_default)
1569: maxindex = build_int_2 (1, 0);
1570: value = 2;
1571: }
1.1 root 1572:
1.1.1.2 root 1573: if (maxindex)
1574: {
1575: TYPE_DOMAIN (type) = make_index_type (maxindex);
1576: if (!TREE_TYPE (maxindex))
1577: TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
1.1 root 1578: }
1579:
1.1.1.2 root 1580: /* Lay out the type now that we can get the real answer. */
1581:
1582: layout_type (type);
1.1 root 1583:
1.1.1.2 root 1584: return value;
1.1 root 1585: }
1586:
1587: /* Given declspecs and a declarator,
1588: determine the name and type of the object declared.
1589: DECLSPECS is a chain of tree_list nodes whose value fields
1590: are the storage classes and type specifiers.
1591:
1592: DECL_CONTEXT says which syntactic context this declaration is in:
1.1.1.2 root 1593: NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
1594: FUNCDEF for a function definition. Like NORMAL but a few different
1595: error messages in each case. Return value may be zero meaning
1596: this definition is too screwy to try to parse.
1597: PARM for a parameter declaration (either within a function prototype
1598: or before a function body). Make a PARM_DECL, or return void_type_node.
1599: TYPENAME if for a typename (in a cast or sizeof).
1600: Don't make a DECL node; just return the type.
1601: FIELD for a struct or union field; make a FIELD_DECL.
1602: INITIALIZED is 1 if the decl has an initializer.
1.1 root 1603:
1604: In the TYPENAME case, DECLARATOR is really an absolute declarator.
1605: It may also be so in the PARM case, for a prototype where the
1.1.1.2 root 1606: argument type is specified but not the name.
1607:
1608: This function is where the complicated C meanings of `static'
1609: and `extern' are intrepreted. */
1.1 root 1610:
1611: static tree
1.1.1.2 root 1612: grokdeclarator (declarator, declspecs, decl_context, initialized)
1.1 root 1613: tree declspecs;
1614: tree declarator;
1615: enum decl_context decl_context;
1.1.1.2 root 1616: int initialized;
1.1 root 1617: {
1618: int specbits = 0;
1619: tree spec;
1620: tree type = NULL_TREE;
1621: int longlong = 0;
1622: int constp;
1623: int volatilep;
1.1.1.2 root 1624: int inlinep;
1.1 root 1625: int explicit_int = 0;
1.1.1.2 root 1626: int explicit_char = 0;
1627: char *name;
1628: tree typedef_type = 0;
1629: int funcdef_flag = 0;
1.1.1.5 ! root 1630: int resume_temporary = 0;
1.1.1.2 root 1631: enum tree_code innermost_code = ERROR_MARK;
1632:
1633: if (decl_context == FUNCDEF)
1634: funcdef_flag = 1, decl_context = NORMAL;
1635:
1.1.1.5 ! root 1636: if (flag_traditional && allocation_temporary_p ())
! 1637: {
! 1638: resume_temporary = 1;
! 1639: end_temporary_allocation ();
! 1640: }
! 1641:
1.1.1.2 root 1642: /* Look inside a declarator for the name being declared
1643: and get it as a string, for an error message. */
1644: {
1645: register tree decl = declarator;
1646: name = 0;
1647:
1648: while (decl)
1649: switch (TREE_CODE (decl))
1650: {
1651: case ARRAY_REF:
1652: case INDIRECT_REF:
1653: case CALL_EXPR:
1654: innermost_code = TREE_CODE (decl);
1655: decl = TREE_OPERAND (decl, 0);
1656: break;
1657:
1658: case IDENTIFIER_NODE:
1659: name = IDENTIFIER_POINTER (decl);
1660: decl = 0;
1661: break;
1662:
1663: default:
1664: abort ();
1665: }
1666: if (name == 0)
1667: name = "type name";
1668: }
1669:
1670: /* A function definition's declarator must have the form of
1671: a function declarator. */
1672:
1673: if (funcdef_flag && innermost_code != CALL_EXPR)
1674: return 0;
1.1 root 1675:
1676: /* Anything declared one level down from the top level
1677: must be one of the parameters of a function
1678: (because the body is at least two levels down). */
1679:
1680: if (decl_context == NORMAL
1681: && current_binding_level->level_chain == global_binding_level)
1682: decl_context = PARM;
1683:
1684: /* Look through the decl specs and record which ones appear.
1685: Some typespecs are defined as built-in typenames.
1686: Others, the ones that are modifiers of other types,
1687: are represented by bits in SPECBITS: set the bits for
1688: the modifiers that appear. Storage class keywords are also in SPECBITS.
1689:
1690: If there is a typedef name or a type, store the type in TYPE.
1691: This includes builtin typedefs such as `int'.
1692:
1693: Set EXPLICIT_INT if the type is `int' or `char' and did not
1694: come from a user typedef.
1695:
1696: Set LONGLONG if `long' is mentioned twice. */
1697:
1698: for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
1699: {
1700: register int i;
1701: register tree id = TREE_VALUE (spec);
1702:
1.1.1.2 root 1703: if (id == ridpointers[(int) RID_INT])
1.1 root 1704: explicit_int = 1;
1.1.1.2 root 1705: if (id == ridpointers[(int) RID_CHAR])
1706: explicit_char = 1;
1.1 root 1707:
1.1.1.2 root 1708: if (TREE_CODE (id) == IDENTIFIER_NODE)
1709: for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++)
1710: {
1711: if (ridpointers[i] == id)
1712: {
1713: if (i == (int) RID_LONG && specbits & (1<<i))
1.1.1.5 ! root 1714: {
! 1715: if (pedantic)
! 1716: warning ("duplicate `%s'", IDENTIFIER_POINTER (id));
! 1717: else
! 1718: longlong = 1;
! 1719: }
! 1720: else if (specbits & (1 << i))
1.1.1.2 root 1721: warning ("duplicate `%s'", IDENTIFIER_POINTER (id));
1722: specbits |= 1 << i;
1723: goto found;
1724: }
1725: }
1726: if (type)
1727: error ("two or more data types in declaration of `%s'", name);
1728: else if (TREE_CODE (id) == IDENTIFIER_NODE)
1.1 root 1729: {
1730: register tree t = lookup_name (id);
1731: if (!t || TREE_CODE (t) != TYPE_DECL)
1.1.1.2 root 1732: error ("`%s' fails to be a typedef or built in type",
1733: IDENTIFIER_POINTER (id));
1.1 root 1734: else type = TREE_TYPE (t);
1735: }
1.1.1.2 root 1736: else if (TREE_CODE (id) != ERROR_MARK)
1737: type = id;
1.1 root 1738:
1.1.1.2 root 1739: found: {}
1.1 root 1740: }
1741:
1.1.1.2 root 1742: typedef_type = type;
1743:
1.1 root 1744: /* No type at all: default to `int', and set EXPLICIT_INT
1745: because it was not a user-defined typedef. */
1746:
1747: if (type == 0)
1748: {
1.1.1.2 root 1749: if (funcdef_flag && warn_return_type
1750: && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
1751: | (1 << (int) RID_SIGNED) | (1 << (int) RID_UNSIGNED))))
1752: warn_about_return_type = 1;
1.1 root 1753: explicit_int = 1;
1754: type = integer_type_node;
1755: }
1756:
1757: /* Now process the modifiers that were specified
1758: and check for invalid combinations. */
1759:
1760: /* Long double is a special combination. */
1761:
1.1.1.2 root 1762: if ((specbits & 1 << (int) RID_LONG) && type == double_type_node)
1.1 root 1763: {
1.1.1.2 root 1764: specbits &= ~ (1 << (int) RID_LONG);
1.1 root 1765: type = long_double_type_node;
1766: }
1767:
1768: /* Check all other uses of type modifiers. */
1769:
1.1.1.2 root 1770: if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
1771: | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
1.1 root 1772: {
1.1.1.2 root 1773: if (!explicit_int && !explicit_char && !pedantic)
1774: error ("long, short, signed or unsigned used invalidly for `%s'", name);
1775: else if ((specbits & 1 << (int) RID_LONG) && (specbits & 1 << (int) RID_SHORT))
1776: error ("long and short specified together for `%s'", name);
1777: else if (((specbits & 1 << (int) RID_LONG) || (specbits & 1 << (int) RID_SHORT))
1778: && explicit_char)
1779: error ("long or short specified with char for `%s'", name);
1780: else if ((specbits & 1 << (int) RID_SIGNED) && (specbits & 1 << (int) RID_UNSIGNED))
1781: error ("signed and unsigned given together for `%s'", name);
1.1 root 1782: else
1783: {
1.1.1.2 root 1784: if (specbits & 1 << (int) RID_UNSIGNED)
1.1 root 1785: {
1.1.1.5 ! root 1786: if (longlong)
! 1787: type = long_long_unsigned_type_node;
! 1788: else if (specbits & 1 << (int) RID_LONG)
1.1 root 1789: type = long_unsigned_type_node;
1.1.1.2 root 1790: else if (specbits & 1 << (int) RID_SHORT)
1.1 root 1791: type = short_unsigned_type_node;
1792: else if (type == char_type_node)
1793: type = unsigned_char_type_node;
1794: else
1795: type = unsigned_type_node;
1796: }
1.1.1.2 root 1797: else if ((specbits & 1 << (int) RID_SIGNED)
1798: && type == char_type_node)
1799: type = signed_char_type_node;
1.1.1.5 ! root 1800: else if (longlong)
! 1801: type = long_long_integer_type_node;
1.1.1.2 root 1802: else if (specbits & 1 << (int) RID_LONG)
1.1 root 1803: type = long_integer_type_node;
1.1.1.2 root 1804: else if (specbits & 1 << (int) RID_SHORT)
1.1 root 1805: type = short_integer_type_node;
1806: }
1807: }
1.1.1.2 root 1808:
1.1 root 1809: /* Set CONSTP if this declaration is `const', whether by
1810: explicit specification or via a typedef.
1811: Likewise for VOLATILEP. */
1812:
1.1.1.2 root 1813: constp = !! (specbits & 1 << (int) RID_CONST) + TREE_READONLY (type);
1814: volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TREE_VOLATILE (type);
1815: inlinep = !! (specbits & (1 << (int) RID_INLINE));
1816: if (constp > 1)
1817: warning ("duplicate `const'");
1818: if (volatilep > 1)
1819: warning ("duplicate `volatile'");
1.1 root 1820: type = TYPE_MAIN_VARIANT (type);
1821:
1822: /* Warn if two storage classes are given. Default to `auto'. */
1823:
1824: {
1825: int nclasses = 0;
1826:
1.1.1.2 root 1827: if (specbits & 1 << (int) RID_AUTO) nclasses++;
1828: if (specbits & 1 << (int) RID_STATIC) nclasses++;
1829: if (specbits & 1 << (int) RID_EXTERN) nclasses++;
1830: if (specbits & 1 << (int) RID_REGISTER) nclasses++;
1831: if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
1.1 root 1832:
1833: /* Warn about storage classes that are invalid for certain
1834: kinds of declarations (parameters, typenames, etc.). */
1835:
1836: if (nclasses > 1)
1.1.1.2 root 1837: error ("multiple storage classes in declaration of `%s'", name);
1.1 root 1838: else if (decl_context != NORMAL && nclasses > 0)
1839: {
1.1.1.2 root 1840: if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
1.1 root 1841: ;
1.1.1.2 root 1842: else
1843: {
1844: error ((decl_context == FIELD
1845: ? "storage class specified for structure field `%s'"
1846: : (decl_context == PARM
1847: ? "storage class specified for parameter `%s'"
1848: : "storage class specified for typename")),
1849: name);
1850: specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
1851: | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
1852: | (1 << (int) RID_EXTERN));
1853: }
1.1 root 1854: }
1.1.1.2 root 1855: else if (current_binding_level == global_binding_level)
1.1 root 1856: {
1.1.1.2 root 1857: if (specbits & (1 << (int) RID_AUTO))
1858: error ("top-level declaration of `%s' specifies `auto'", name);
1859: if (specbits & (1 << (int) RID_REGISTER))
1860: error ("top-level declaration of `%s' specifies `register'", name);
1.1 root 1861: }
1862: }
1.1.1.2 root 1863:
1.1 root 1864: /* Now figure out the structure of the declarator proper.
1865: Descend through it, creating more complex types, until we reach
1866: the declared identifier (or NULL_TREE, in an absolute declarator). */
1867:
1868: while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
1869: {
1.1.1.2 root 1870: if (type == error_mark_node)
1871: {
1872: declarator = TREE_OPERAND (declarator, 0);
1873: continue;
1874: }
1875:
1.1 root 1876: /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
1877: an INDIRECT_REF (for *...),
1878: a CALL_EXPR (for ...(...)),
1879: an identifier (for the name being declared)
1880: or a null pointer (for the place in an absolute declarator
1881: where the name was omitted).
1882: For the last two cases, we have just exited the loop.
1883:
1884: At this point, TYPE is the type of elements of an array,
1885: or for a function to return, or for a pointer to point to.
1886: After this sequence of ifs, TYPE is the type of the
1887: array or function or pointer, and DECLARATOR has had its
1888: outermost layer removed. */
1889:
1890: if (TREE_CODE (declarator) == ARRAY_REF)
1891: {
1892: register tree itype = NULL_TREE;
1893: register tree size = TREE_OPERAND (declarator, 1);
1894:
1.1.1.2 root 1895: declarator = TREE_OPERAND (declarator, 0);
1.1 root 1896:
1897: /* Check for some types that there cannot be arrays of. */
1898:
1899: if (type == void_type_node)
1900: {
1.1.1.2 root 1901: error ("declaration of `%s' as array of voids", name);
1902: type = error_mark_node;
1.1 root 1903: }
1904:
1905: if (TREE_CODE (type) == FUNCTION_TYPE)
1906: {
1.1.1.2 root 1907: error ("declaration of `%s' as array of functions", name);
1908: type = error_mark_node;
1.1 root 1909: }
1910:
1.1.1.2 root 1911: if (size == error_mark_node)
1912: type = error_mark_node;
1913:
1914: if (type == error_mark_node)
1915: continue;
1916:
1.1 root 1917: /* If size was specified, set ITYPE to a range-type for that size.
1918: Otherwise, ITYPE remains null. finish_decl may figure it out
1919: from an initial value. */
1920:
1921: if (size)
1922: {
1.1.1.2 root 1923: if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
1924: && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE)
1925: {
1926: error ("size of array `%s' has non-integer type", name);
1927: size = integer_one_node;
1928: }
1929: if (pedantic && integer_zerop (size))
1930: warning ("ANSI C forbids zero-size array `%s'", name);
1931: if (INT_CST_LT (size, integer_zero_node))
1932: {
1933: error ("size of array `%s' is negative", name);
1934: size = integer_one_node;
1935: }
1.1 root 1936: if (TREE_LITERAL (size))
1937: itype = make_index_type (build_int_2 (TREE_INT_CST_LOW (size) - 1, 0));
1938: else
1.1.1.2 root 1939: {
1940: if (pedantic)
1941: warning ("ANSI C forbids variable-size array `%s'", name);
1942: itype = build_binary_op (MINUS_EXPR, size, integer_one_node);
1943: itype = make_index_type (itype);
1944: }
1.1 root 1945: }
1946:
1947: /* Build the array type itself. */
1948:
1949: type = build_array_type (type, itype);
1950: }
1951: else if (TREE_CODE (declarator) == CALL_EXPR)
1952: {
1953: /* Declaring a function type.
1954: Make sure we have a valid type for the function to return. */
1.1.1.2 root 1955: if (type == error_mark_node)
1956: continue;
1.1 root 1957:
1958: /* Is this an error? Should they be merged into TYPE here? */
1.1.1.2 root 1959: if (constp || volatilep)
1960: warning ("function declared to return const or volatile result");
1.1 root 1961: constp = 0;
1962: volatilep = 0;
1963:
1964: /* Warn about some types functions can't return. */
1965:
1966: if (TREE_CODE (type) == FUNCTION_TYPE)
1967: {
1.1.1.2 root 1968: error ("`%s' declared as function returning a function", name);
1.1 root 1969: type = integer_type_node;
1970: }
1971: if (TREE_CODE (type) == ARRAY_TYPE)
1972: {
1.1.1.2 root 1973: error ("`%s' declared as function returning an array", name);
1.1 root 1974: type = integer_type_node;
1975: }
1976:
1977: /* Construct the function type and go to the next
1978: inner layer of declarator. */
1979:
1980: type =
1981: build_function_type (type,
1.1.1.2 root 1982: grokparms (TREE_OPERAND (declarator, 1),
1.1.1.3 root 1983: funcdef_flag
1984: /* Say it's a definition
1985: only for the CALL_EXPR
1986: closest to the identifier. */
1987: && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE));
1.1 root 1988: declarator = TREE_OPERAND (declarator, 0);
1989: }
1990: else if (TREE_CODE (declarator) == INDIRECT_REF)
1991: {
1992: /* Merge any constancy or volatility into the target type
1993: for the pointer. */
1994:
1995: if (constp || volatilep)
1996: type = build_type_variant (type, constp, volatilep);
1997: constp = 0;
1998: volatilep = 0;
1999:
2000: type = build_pointer_type (type);
2001:
2002: /* Process a list of type modifier keywords
2003: (such as const or volatile) that were given inside the `*'. */
2004:
2005: if (TREE_TYPE (declarator))
2006: {
2007: register tree typemodlist;
1.1.1.2 root 2008: int erred = 0;
1.1 root 2009: for (typemodlist = TREE_TYPE (declarator); typemodlist;
2010: typemodlist = TREE_CHAIN (typemodlist))
2011: {
2012: if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST])
1.1.1.2 root 2013: constp++;
2014: else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE])
2015: volatilep++;
2016: else if (!erred)
2017: {
2018: erred = 1;
2019: error ("invalid type modifier within pointer declarator");
2020: }
1.1 root 2021: }
1.1.1.2 root 2022: if (constp > 1)
2023: warning ("duplicate `const'");
2024: if (volatilep > 1)
2025: warning ("duplicate `volatile'");
1.1 root 2026: }
2027:
2028: declarator = TREE_OPERAND (declarator, 0);
2029: }
2030: else
2031: abort ();
2032:
1.1.1.2 root 2033: /* layout_type (type); */
1.1 root 2034:
2035: /* @@ Should perhaps replace the following code by changes in
2036: * @@ stor_layout.c. */
2037: if (TREE_CODE (type) == FUNCTION_DECL)
2038: {
2039: /* A function variable in C should be Pmode rather than EPmode
2040: because it has just the address of a function, no static chain.*/
2041: TYPE_MODE (type) = Pmode;
2042: }
2043: }
2044:
1.1.1.2 root 2045: /* Now TYPE has the actual type. */
1.1 root 2046:
2047: /* If this is declaring a typedef name, return a TYPE_DECL. */
2048:
1.1.1.2 root 2049: if (specbits & (1 << (int) RID_TYPEDEF))
1.1 root 2050: {
2051: /* Note that the grammar rejects storage classes
2052: in typenames, fields or parameters */
2053: if (constp || volatilep)
2054: type = build_type_variant (type, constp, volatilep);
1.1.1.5 ! root 2055: if (resume_temporary)
! 2056: resume_temporary_allocation ();
1.1.1.2 root 2057: return build_decl (TYPE_DECL, declarator, type);
1.1 root 2058: }
2059:
1.1.1.2 root 2060: /* Detect the case of an array type of unspecified size
2061: which came, as such, direct from a typedef name.
2062: We must copy the type, so that each identifier gets
2063: a distinct type, so that each identifier's size can be
2064: controlled separately by its own initializer. */
1.1 root 2065:
1.1.1.2 root 2066: if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
2067: && TYPE_DOMAIN (type) == 0)
2068: {
2069: type = build_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
2070: }
1.1 root 2071:
2072: /* If this is a type name (such as, in a cast or sizeof),
2073: compute the type and return it now. */
2074:
2075: if (decl_context == TYPENAME)
2076: {
2077: /* Note that the grammar rejects storage classes
2078: in typenames, fields or parameters */
2079: if (constp || volatilep)
2080: type = build_type_variant (type, constp, volatilep);
1.1.1.5 ! root 2081: if (resume_temporary)
! 2082: resume_temporary_allocation ();
1.1 root 2083: return type;
2084: }
2085:
2086: /* `void' at top level (not within pointer)
1.1.1.2 root 2087: is allowed only in typedefs or type names.
2088: We don't complain about parms either, but that is because
2089: a better error message can be made later. */
1.1 root 2090:
1.1.1.2 root 2091: if (type == void_type_node && decl_context != PARM)
1.1 root 2092: {
1.1.1.2 root 2093: error ("variable or field `%s' declared void",
2094: IDENTIFIER_POINTER (declarator));
1.1 root 2095: type = integer_type_node;
2096: }
2097:
2098: /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
2099: or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
2100:
2101: {
2102: register tree decl;
2103:
2104: if (decl_context == PARM)
2105: {
2106: /* A parameter declared as an array of T is really a pointer to T.
2107: One declared as a function is really a pointer to a function. */
2108:
2109: if (TREE_CODE (type) == ARRAY_TYPE)
2110: {
1.1.1.2 root 2111: /* Transfer const-ness of array into that of type pointed to. */
2112: type = build_pointer_type
2113: (build_type_variant (TREE_TYPE (type), constp, volatilep));
2114: volatilep = constp = 0;
2115: }
2116: else if (TREE_CODE (type) == FUNCTION_TYPE)
2117: type = build_pointer_type (type);
1.1 root 2118:
1.1.1.2 root 2119: decl = build_decl (PARM_DECL, declarator, type);
2120:
2121: /* Compute the type actually passed in the parmlist,
2122: for the case where there is no prototype.
2123: (For example, shorts and chars are passed as ints.)
2124: When there is a prototype, this is overridden later. */
2125:
1.1 root 2126: DECL_ARG_TYPE (decl) = type;
2127: if (type == float_type_node)
2128: DECL_ARG_TYPE (decl) = double_type_node;
2129: else if (TREE_CODE (type) == INTEGER_TYPE
2130: && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2131: DECL_ARG_TYPE (decl) = integer_type_node;
2132: }
2133: else if (decl_context == FIELD)
2134: {
2135: /* Structure field. It may not be a function. */
2136:
2137: if (TREE_CODE (type) == FUNCTION_TYPE)
2138: {
1.1.1.2 root 2139: error ("field `%s' declared as a function",
2140: IDENTIFIER_POINTER (declarator));
1.1 root 2141: type = build_pointer_type (type);
2142: }
1.1.1.2 root 2143: else if (TYPE_SIZE (type) == 0)
2144: {
2145: error ("field `%s' has incomplete type",
2146: IDENTIFIER_POINTER (declarator));
2147: type = error_mark_node;
2148: }
2149: decl = build_decl (FIELD_DECL, declarator, type);
1.1 root 2150: }
1.1.1.2 root 2151: else if (TREE_CODE (type) == FUNCTION_TYPE)
1.1 root 2152: {
1.1.1.2 root 2153: if (specbits & ((1 << (int) RID_AUTO) | (1 << (int) RID_REGISTER)))
2154: error ("invalid storage class for function `%s'",
2155: IDENTIFIER_POINTER (declarator));
2156: /* Function declaration not at top level.
2157: Storage classes other than `extern' are not allowed
2158: and `extern' makes no difference. */
2159: if (current_binding_level != global_binding_level
2160: && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
2161: && pedantic)
2162: warning ("invalid storage class for function `%s'",
2163: IDENTIFIER_POINTER (declarator));
2164: decl = build_decl (FUNCTION_DECL, declarator, type);
1.1.1.5 ! root 2165:
1.1.1.2 root 2166: TREE_EXTERNAL (decl) = 1;
2167: /* Record presence of `static'. */
2168: TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC));
2169: /* Record presence of `inline', if it is reasonable. */
2170: if (inlinep)
2171: {
2172: tree last = tree_last (TYPE_ARG_TYPES (type));
1.1 root 2173:
1.1.1.2 root 2174: if (! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2175: warning ("cannot inline function `main'");
2176: else if (last && TREE_VALUE (last) != void_type_node)
2177: error ("inline declaration ignored for function with `...'");
2178: else
2179: /* Assume that otherwise the function can be inlined. */
2180: TREE_INLINE (decl) = 1;
2181: }
2182: }
2183: else
2184: {
2185: /* It's a variable. */
1.1 root 2186:
1.1.1.2 root 2187: decl = build_decl (VAR_DECL, declarator, type);
1.1 root 2188:
1.1.1.2 root 2189: if (inlinep)
2190: warning_with_decl (decl, "variable `%s' declared `inline'");
1.1 root 2191:
1.1.1.2 root 2192: /* An uninitialized decl with `extern' is a reference. */
2193: TREE_EXTERNAL (decl)
2194: = !initialized && (specbits & (1 << (int) RID_EXTERN));
2195: /* At top level, either `static' or no s.c. makes a definition
2196: (perhaps tentative), and absence of `static' makes it public. */
2197: if (current_binding_level == global_binding_level)
2198: {
2199: TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC));
2200: TREE_STATIC (decl) = ! TREE_EXTERNAL (decl);
2201: }
2202: /* Not at top level, only `static' makes a static definition. */
2203: else
2204: {
2205: TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
2206: /* `extern' with initialization is invalid if not at top level. */
2207: if ((specbits & (1 << (int) RID_EXTERN)) && initialized)
2208: error ("`%s' has both `extern' and initializer", name);
2209: }
1.1 root 2210: }
2211:
2212: /* Record `register' declaration for warnings on &
2213: and in case doing stupid register allocation. */
2214:
1.1.1.2 root 2215: if (specbits & (1 << (int) RID_REGISTER))
1.1 root 2216: TREE_REGDECL (decl) = 1;
2217:
2218: /* Record constancy and volatility. */
2219:
2220: if (constp)
2221: TREE_READONLY (decl) = 1;
2222: if (volatilep)
2223: {
2224: TREE_VOLATILE (decl) = 1;
2225: TREE_THIS_VOLATILE (decl) = 1;
2226: }
2227:
1.1.1.5 ! root 2228: if (resume_temporary)
! 2229: resume_temporary_allocation ();
! 2230:
1.1 root 2231: return decl;
2232: }
2233: }
2234:
2235: /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
2236: MAXVAL should be the maximum value in the domain
2237: (one less than the length of the array). */
2238:
2239: tree
2240: make_index_type (maxval)
2241: tree maxval;
2242: {
2243: register tree itype = make_node (INTEGER_TYPE);
1.1.1.2 root 2244: int maxint = TREE_INT_CST_LOW (maxval);
1.1 root 2245: TYPE_PRECISION (itype) = BITS_PER_WORD;
2246: TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
2247: TREE_TYPE (TYPE_MIN_VALUE (itype)) = itype;
2248: TYPE_MAX_VALUE (itype) = maxval;
2249: TREE_TYPE (maxval) = itype;
2250: TYPE_MODE (itype) = SImode;
1.1.1.2 root 2251: TYPE_SIZE (itype) = TYPE_SIZE (long_integer_type_node);
2252: TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (long_integer_type_node);
2253: TYPE_ALIGN (itype) = TYPE_ALIGN (long_integer_type_node);
2254: return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
1.1 root 2255: }
2256:
1.1.1.3 root 2257: /* Decode the parameter-list info for a function type or function definition.
2258: The argument is the value returned by `get_parm_info' (or made in parse.y
2259: if there is an identifier list instead of a parameter decl list).
2260: These two functions are separate because when a function returns
2261: or receives functions then each is called multiple times but the order
2262: of calls is different. The last call to `grokparms' is always the one
2263: that contains the formal parameter names of a function definition.
2264:
2265: Store in `last_function_parms' a chain of the decls of parms.
2266: Also store in `last_function_parm_tags' a chain of the struct and union
2267: tags declared among the parms.
2268:
2269: Return a list of arg types to use in the FUNCTION_TYPE for this function.
1.1.1.2 root 2270:
2271: FUNCDEF_FLAG is nonzero for a function definition, 0 for
2272: a mere declaration. A nonempty identifier-list gets an error message
2273: when FUNCDEF_FLAG is zero. */
1.1 root 2274:
1.1.1.2 root 2275: static tree
1.1.1.3 root 2276: grokparms (parms_info, funcdef_flag)
2277: tree parms_info;
1.1.1.2 root 2278: int funcdef_flag;
1.1 root 2279: {
1.1.1.3 root 2280: tree first_parm = TREE_CHAIN (parms_info);
2281:
2282: last_function_parms = TREE_PURPOSE (parms_info);
2283: last_function_parm_tags = TREE_VALUE (parms_info);
2284:
1.1.1.2 root 2285: if (first_parm != 0
2286: && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
2287: {
2288: if (! funcdef_flag)
2289: warning ("parameter names (without types) in function declaration");
1.1 root 2290:
1.1.1.2 root 2291: last_function_parms = first_parm;
2292: return 0;
2293: }
2294: else
2295: {
1.1.1.3 root 2296: tree t;
2297: /* In a fcn definition, arg types must be complete. */
2298: if (funcdef_flag)
2299: for (t = last_function_parms; t; t = TREE_CHAIN (t))
2300: {
2301: tree type = TREE_TYPE (t);
2302: if (TYPE_SIZE (type) == 0)
2303: {
2304: error ("parameter `%s' has incomplete type",
2305: IDENTIFIER_POINTER (DECL_NAME (t)));
2306: TREE_TYPE (t) = error_mark_node;
2307: }
2308: }
2309:
1.1.1.2 root 2310: return first_parm;
2311: }
2312: }
2313:
2314:
1.1.1.3 root 2315: /* Return a tree_list node with info on a parameter list just parsed.
2316: The TREE_PURPOSE is a chain of decls of those parms.
2317: The TREE_VALUE is a list of structure, union and enum tags defined.
2318: The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
2319: This tree_list node is later fed to `grokparms'.
1.1.1.2 root 2320:
1.1.1.3 root 2321: VOID_AT_END nonzero means append `void' to the end of the type-list.
1.1.1.2 root 2322: Zero means the parmlist ended with an ellipsis so don't append `void'. */
2323:
2324: tree
1.1.1.3 root 2325: get_parm_info (void_at_end)
1.1.1.2 root 2326: int void_at_end;
2327: {
2328: register tree decl;
2329: register tree types = 0;
2330: tree link;
2331: int erred = 0;
1.1.1.3 root 2332: tree tags = gettags ();
2333: tree parms = nreverse (getdecls ());
1.1.1.2 root 2334:
2335: /* Just `void' (and no ellipsis) is special. There are really no parms. */
1.1.1.3 root 2336: if (void_at_end && parms != 0
2337: && TREE_CHAIN (parms) == 0
2338: && TREE_TYPE (parms) == void_type_node)
1.1.1.2 root 2339: {
1.1.1.3 root 2340: parms = NULL_TREE;
1.1.1.2 root 2341: storedecls (NULL_TREE);
1.1.1.3 root 2342: return tree_cons (NULL_TREE, NULL_TREE,
2343: tree_cons (NULL_TREE, void_type_node, NULL_TREE));
1.1.1.2 root 2344: }
2345:
1.1.1.3 root 2346: storedecls (parms);
1.1.1.2 root 2347:
1.1.1.3 root 2348: for (decl = parms; decl; decl = TREE_CHAIN (decl))
1.1.1.2 root 2349: {
2350: /* Since there is a prototype,
2351: args are passed in their declared types. */
2352: DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
2353:
2354: types = tree_cons (NULL_TREE, TREE_TYPE (decl), types);
2355: if (TREE_VALUE (types) == void_type_node && ! erred)
1.1 root 2356: {
1.1.1.2 root 2357: error ("`void' in parameter list must be the entire list");
2358: erred = 1;
1.1 root 2359: }
2360: }
2361:
1.1.1.2 root 2362: if (void_at_end)
1.1.1.3 root 2363: return tree_cons (parms, tags,
2364: nreverse (tree_cons (NULL_TREE, void_type_node, types)));
1.1.1.2 root 2365:
1.1.1.3 root 2366: return tree_cons (parms, tags, nreverse (types));
1.1.1.2 root 2367: }
2368:
2369: /* Get the struct, enum or union (CODE says which) with tag NAME.
2370: Define the tag as a forward-reference if it is not defined. */
2371:
2372: tree
2373: xref_tag (code, name)
2374: tree name;
2375: {
2376: /* If a cross reference is requested, look up the type
2377: already defined for this tag and return it. */
2378:
2379: register tree ref = lookup_tag (code, name, current_binding_level, 0);
2380: if (ref) return ref;
2381:
2382: /* If no such tag is yet defined, create a forward-reference node
2383: and record it as the "definition".
2384: When a real declaration of this type is found,
2385: the forward-reference will be altered into a real type. */
2386:
2387: ref = make_node (code);
2388: pushtag (name, ref);
2389: return ref;
1.1 root 2390: }
2391:
1.1.1.2 root 2392: /* Make sure that the tag NAME is defined *in the current binding level*
2393: at least as a forward reference.
2394: CODE says which kind of tag NAME ought to be. */
2395:
2396: tree
2397: start_struct (code, name)
2398: enum tree_code code;
2399: tree name;
2400: {
2401: /* If there is already a tag defined at this binding level
2402: (as a forward reference), just return it. */
2403:
2404: register tree ref = 0;
2405:
2406: if (name != 0)
2407: ref = lookup_tag (code, name, current_binding_level, 1);
2408: if (ref && TREE_CODE (ref) == code)
2409: {
2410: if (TYPE_FIELDS (ref))
2411: error ((code == UNION_TYPE ? "redefinition of `union %s'"
2412: : "redefinition of `struct %s'"),
2413: IDENTIFIER_POINTER (name));
2414:
2415: return ref;
2416: }
2417:
2418: /* Otherwise create a forward-reference just so the tag is in scope. */
2419:
2420: ref = make_node (code);
2421: pushtag (name, ref);
2422: return ref;
2423: }
2424:
1.1 root 2425: /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
2426: of a structure component, returning a FIELD_DECL node.
2427: WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
2428:
2429: This is done during the parsing of the struct declaration.
2430: The FIELD_DECL nodes are chained together and the lot of them
2431: are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
2432:
2433: tree
2434: grokfield (filename, line, declarator, declspecs, width)
2435: char *filename;
2436: int line;
2437: tree declarator, declspecs, width;
2438: {
1.1.1.2 root 2439: register tree value = grokdeclarator (declarator, declspecs, FIELD, 0);
1.1 root 2440:
1.1.1.2 root 2441: finish_decl (value, NULL, NULL);
1.1 root 2442: DECL_INITIAL (value) = width;
1.1.1.2 root 2443:
1.1 root 2444: return value;
2445: }
2446:
1.1.1.2 root 2447: /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
2448: FIELDLIST is a chain of FIELD_DECL nodes for the fields. */
1.1 root 2449:
2450: tree
1.1.1.2 root 2451: finish_struct (t, fieldlist)
2452: register tree t, fieldlist;
1.1 root 2453: {
2454: register tree x;
1.1.1.2 root 2455: int old;
2456: int round_up_size = 1;
1.1 root 2457:
1.1.1.2 root 2458: /* If this type was previously laid out as a forward reference,
2459: make sure we lay it out again. */
1.1 root 2460:
1.1.1.2 root 2461: TYPE_SIZE (t) = 0;
1.1 root 2462:
1.1.1.2 root 2463: old = suspend_momentary ();
1.1 root 2464:
1.1.1.2 root 2465: if (fieldlist == 0 && pedantic)
2466: warning ((TREE_CODE (t) == UNION_TYPE ? "union has no members"
2467: : "structure has no members"));
1.1 root 2468:
2469: /* Install struct as DECL_CONTEXT of each field decl.
2470: Also process specified field sizes.
2471: Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
2472: The specified size is found in the DECL_INITIAL.
2473: Store 0 there, except for ": 0" fields (so we can find them
2474: and delete them, below). */
2475:
2476: for (x = fieldlist; x; x = TREE_CHAIN (x))
2477: {
2478: DECL_CONTEXT (x) = t;
2479: DECL_SIZE_UNIT (x) = 0;
1.1.1.2 root 2480:
2481: /* If any field is const, the structure type is pseudo-const. */
2482: if (TREE_READONLY (x))
2483: C_TYPE_FIELDS_READONLY (t) = 1;
2484: else
2485: {
2486: /* A field that is pseudo-const makes the structure likewise. */
2487: tree t1 = TREE_TYPE (x);
2488: while (TREE_CODE (t1) == ARRAY_TYPE)
2489: t1 = TREE_TYPE (t1);
2490: if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
2491: && C_TYPE_FIELDS_READONLY (t1))
2492: C_TYPE_FIELDS_READONLY (t) = 1;
2493: }
2494:
2495: /* Detect invalid bit-field size. */
1.1 root 2496: if (DECL_INITIAL (x) && TREE_CODE (DECL_INITIAL (x)) != INTEGER_CST)
2497: {
1.1.1.2 root 2498: error_with_decl (x, "bit-field `%s' width not an integer constant");
1.1 root 2499: DECL_INITIAL (x) = NULL;
2500: }
1.1.1.2 root 2501:
2502: /* Detect invalid bit-field type. */
2503: if (DECL_INITIAL (x)
2504: && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
2505: && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
2506: {
2507: error_with_decl (x, "bit-field `%s' has invalid type");
2508: DECL_INITIAL (x) = NULL;
2509: }
2510: if (DECL_INITIAL (x) && pedantic
2511: && TREE_TYPE (x) != integer_type_node
2512: && TREE_TYPE (x) != unsigned_type_node)
2513: warning_with_decl (x, "bit-field `%s' type invalid in ANSI C");
2514:
2515: /* Detect and ignore out of range field width. */
1.1 root 2516: if (DECL_INITIAL (x))
2517: {
1.1.1.2 root 2518: register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
1.1 root 2519:
1.1.1.2 root 2520: if (width < 0)
2521: {
2522: DECL_INITIAL (x) = NULL;
2523: warning_with_decl (x, "negative width in bit-field `%s'");
2524: }
2525: else if (width > TYPE_PRECISION (TREE_TYPE (x)))
2526: {
2527: DECL_INITIAL (x) = NULL;
2528: warning_with_decl (x, "width of `%s' exceeds its type");
2529: }
2530: }
2531:
2532: /* Process valid field width. */
2533: if (DECL_INITIAL (x))
2534: {
2535: register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
2536:
2537: if (width == 0)
1.1 root 2538: {
2539: /* field size 0 => mark following field as "aligned" */
2540: if (TREE_CHAIN (x))
1.1.1.3 root 2541: DECL_ALIGN (TREE_CHAIN (x))
2542: = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
1.1.1.2 root 2543: /* field of size 0 at the end => round up the size. */
2544: else
2545: round_up_size = EMPTY_FIELD_BOUNDARY;
1.1 root 2546: }
2547: else
2548: {
2549: DECL_INITIAL (x) = NULL;
1.1.1.2 root 2550: DECL_SIZE_UNIT (x) = width;
1.1 root 2551: TREE_PACKED (x) = 1;
1.1.1.2 root 2552: /* Traditionally a bit field is unsigned
2553: even if declared signed. */
2554: if (flag_traditional
2555: && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
2556: TREE_TYPE (x) = unsigned_type_node;
1.1 root 2557: }
2558: }
1.1.1.2 root 2559: else
2560: /* Non-bit-fields are aligned for their type. */
1.1.1.3 root 2561: DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
1.1 root 2562: }
1.1.1.2 root 2563:
2564: /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
2565: And they have already done their work. */
2566:
2567: /* Delete all zero-width bit-fields from the front of the fieldlist */
1.1 root 2568: while (fieldlist
2569: && DECL_INITIAL (fieldlist))
2570: fieldlist = TREE_CHAIN (fieldlist);
1.1.1.2 root 2571: /* Delete all such members from the rest of the fieldlist */
1.1 root 2572: for (x = fieldlist; x;)
2573: {
2574: if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x)))
2575: TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
2576: else x = TREE_CHAIN (x);
2577: }
2578:
1.1.1.2 root 2579: /* Delete all duplicate fields from the fieldlist */
2580: for (x = fieldlist; x && TREE_CHAIN (x);)
2581: /* Anonymous fields aren't duplicates. */
2582: if (DECL_NAME (TREE_CHAIN (x)) == 0)
2583: x = TREE_CHAIN (x);
2584: else
2585: {
2586: register tree y = fieldlist;
2587:
2588: while (1)
2589: {
2590: if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
2591: break;
2592: if (y == x)
2593: break;
2594: y = TREE_CHAIN (y);
2595: }
2596: if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x)))
2597: {
2598: error_with_decl (TREE_CHAIN (x), "duplicate member `%s'");
2599: TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
2600: }
2601: else x = TREE_CHAIN (x);
2602: }
2603:
2604: /* Now we have the final fieldlist. Record it,
2605: then lay out the structure or union (including the fields). */
2606:
1.1 root 2607: TYPE_FIELDS (t) = fieldlist;
1.1.1.2 root 2608:
2609: /* If there's a :0 field at the end, round the size to the
2610: EMPTY_FIELD_BOUNDARY. */
2611: TYPE_ALIGN (t) = round_up_size;
1.1 root 2612:
2613: layout_type (t);
2614:
1.1.1.2 root 2615: /* Promote each bit-field's type to int if it is narrower than that. */
2616: for (x = fieldlist; x; x = TREE_CHAIN (x))
2617: if (TREE_PACKED (x)
2618: && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
2619: && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x)
2620: < TYPE_PRECISION (integer_type_node)))
2621: TREE_TYPE (x) = integer_type_node;
2622:
2623: /* If this structure or union completes the type of any previous
2624: variable declaration, lay it out and output its rtl. */
2625:
2626: if (current_binding_level->n_incomplete != 0)
2627: {
2628: tree decl;
2629: for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl))
2630: if (TREE_TYPE (decl) == t
2631: && TREE_CODE (decl) != TYPE_DECL)
2632: {
2633: int toplevel = global_binding_level == current_binding_level;
2634: layout_decl (decl);
2635: rest_of_decl_compilation (decl, 0, toplevel, 0);
2636: if (! toplevel)
2637: expand_decl (decl);
2638: --current_binding_level->n_incomplete;
2639: }
2640: }
2641:
2642: resume_momentary (old);
1.1 root 2643:
2644: return t;
2645: }
2646:
2647: /* Begin compiling the definition of an enumeration type.
2648: NAME is its name (or null if anonymous).
2649: Returns the type object, as yet incomplete.
2650: Also records info about it so that build_enumerator
2651: may be used to declare the individual values as they are read. */
2652:
2653: tree
2654: start_enum (name)
2655: tree name;
2656: {
2657: register tree enumtype = 0;
2658:
2659: /* If this is the real definition for a previous forward reference,
2660: fill in the contents in the same object that used to be the
2661: forward reference. */
2662:
2663: if (name != 0)
2664: enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1);
2665:
1.1.1.2 root 2666: if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
1.1 root 2667: {
2668: enumtype = make_node (ENUMERAL_TYPE);
2669: pushtag (name, enumtype);
2670: }
2671:
2672: if (TYPE_VALUES (enumtype) != 0)
2673: {
2674: /* This enum is a named one that has been declared already. */
1.1.1.2 root 2675: error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
1.1 root 2676:
2677: /* Completely replace its old definition.
2678: The old enumerators remain defined, however. */
2679: TYPE_VALUES (enumtype) = 0;
2680: }
2681:
2682: /* Initially, set up this enum as like `int'
2683: so that we can create the enumerators' declarations and values.
2684: Later on, the precision of the type may be changed and
2685: it may be layed out again. */
2686:
2687: TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
2688: TYPE_SIZE (enumtype) = 0;
2689: fixup_unsigned_type (enumtype);
2690:
1.1.1.2 root 2691: enum_next_value = integer_zero_node;
1.1 root 2692:
2693: return enumtype;
2694: }
2695:
2696: /* After processing and defining all the values of an enumeration type,
2697: install their decls in the enumeration type and finish it off.
2698: ENUMTYPE is the type object and VALUES a list of name-value pairs.
2699: Returns ENUMTYPE. */
2700:
2701: tree
2702: finish_enum (enumtype, values)
2703: register tree enumtype, values;
2704: {
2705: register tree pair = values;
2706: register long maxvalue = 0;
1.1.1.4 root 2707: register long minvalue = 0;
1.1 root 2708: register int i;
2709:
2710: TYPE_VALUES (enumtype) = values;
2711:
2712: /* Calculate the maximum value of any enumerator in this type. */
1.1.1.2 root 2713:
1.1 root 2714: for (pair = values; pair; pair = TREE_CHAIN (pair))
2715: {
2716: int value = TREE_INT_CST_LOW (TREE_VALUE (pair));
1.1.1.4 root 2717: if (pair == values)
2718: minvalue = maxvalue = value;
2719: else
2720: {
2721: if (value > maxvalue)
2722: maxvalue = value;
2723: if (value < minvalue)
2724: minvalue = value;
2725: }
1.1 root 2726: }
2727:
2728: #if 0
2729: /* Determine the precision this type needs, lay it out, and define it. */
2730:
2731: for (i = maxvalue; i; i >>= 1)
2732: TYPE_PRECISION (enumtype)++;
2733:
2734: if (!TYPE_PRECISION (enumtype))
2735: TYPE_PRECISION (enumtype) = 1;
2736:
2737: /* Cancel the laying out previously done for the enum type,
2738: so that fixup_unsigned_type will do it over. */
2739: TYPE_SIZE (enumtype) = 0;
2740:
2741: fixup_unsigned_type (enumtype);
1.1.1.2 root 2742: #endif
1.1 root 2743:
1.1.1.4 root 2744: TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue;
1.1 root 2745:
1.1.1.4 root 2746: /* An enum can have some negative values; then it is signed. */
2747: if (minvalue < 0)
2748: {
2749: TREE_INT_CST_LOW (TYPE_MIN_VALUE (enumtype)) = minvalue;
2750: TREE_INT_CST_HIGH (TYPE_MIN_VALUE (enumtype)) = -1;
2751: TREE_UNSIGNED (enumtype) = 0;
2752: }
1.1 root 2753: return enumtype;
2754: }
2755:
2756: /* Build and install a CONST_DECL for one value of the
2757: current enumeration type (one that was begun with start_enum).
2758: Return a tree-list containing the name and its value.
2759: Assignment of sequential values by default is handled here. */
1.1.1.2 root 2760:
1.1 root 2761: tree
2762: build_enumerator (name, value)
2763: tree name, value;
2764: {
2765: register tree decl;
2766:
2767: /* Validate and default VALUE. */
2768:
2769: if (value != 0 && TREE_CODE (value) != INTEGER_CST)
2770: {
1.1.1.2 root 2771: error ("enumerator value for `%s' not integer constant",
2772: IDENTIFIER_POINTER (name));
1.1 root 2773: value = 0;
2774: }
2775:
1.1.1.2 root 2776: /* Default based on previous value. */
1.1 root 2777: if (value == 0)
1.1.1.2 root 2778: value = enum_next_value;
1.1 root 2779:
1.1.1.2 root 2780: /* Set basis for default for next value. */
2781: enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
2782: integer_one_node);
1.1 root 2783:
2784: /* Now create a declaration for the enum value name. */
2785:
1.1.1.2 root 2786: decl = build_decl (CONST_DECL, name, integer_type_node);
1.1 root 2787: DECL_INITIAL (decl) = value;
1.1.1.2 root 2788: TREE_TYPE (value) = integer_type_node;
1.1 root 2789: pushdecl (decl);
2790:
2791: return build_tree_list (name, value);
2792: }
2793:
2794: /* Create the FUNCTION_DECL for a function definition.
2795: LINE1 is the line number that the definition absolutely begins on.
2796: LINE2 is the line number that the name of the function appears on.
2797: DECLSPECS and DECLARATOR are the parts of the declaration;
2798: they describe the function's name and the type it returns,
2799: but twisted together in a fashion that parallels the syntax of C.
2800:
2801: This function creates a binding context for the function body
2802: as well as setting up the FUNCTION_DECL in current_function_decl.
2803:
2804: Returns 1 on success. If the DECLARATOR is not suitable for a function
2805: (it defines a datum instead), we return 0, which tells
2806: yyparse to report a parse error. */
2807:
2808: int
2809: start_function (declspecs, declarator)
2810: tree declarator, declspecs;
2811: {
1.1.1.2 root 2812: tree decl1, old_decl;
1.1.1.3 root 2813: tree restype;
1.1.1.2 root 2814:
2815: current_function_returns_value = 0; /* Assume, until we see it does. */
2816: current_function_returns_null = 0;
2817: warn_about_return_type = 0;
1.1 root 2818:
1.1.1.2 root 2819: decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
1.1 root 2820:
1.1.1.2 root 2821: /* If the declarator is not suitable for a function definition,
2822: cause a syntax error. */
2823: if (decl1 == 0)
1.1 root 2824: return 0;
2825:
2826: current_function_decl = decl1;
2827:
2828: announce_function (current_function_decl);
2829:
1.1.1.3 root 2830: if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl1))) == 0)
1.1.1.2 root 2831: {
2832: error ("return-type is an incomplete type");
2833: /* Make it return void instead. */
2834: TREE_TYPE (decl1)
2835: = build_function_type (void_type_node,
2836: TYPE_ARG_TYPES (TREE_TYPE (decl1)));
2837: }
2838:
2839: if (warn_about_return_type)
2840: warning ("return-type defaults to `int'");
2841:
2842: /* Save the parm names or decls from this function's declarator
2843: where store_parm_decls will find them. */
2844: current_function_parms = last_function_parms;
2845: current_function_parm_tags = last_function_parm_tags;
2846:
1.1 root 2847: /* Make the init_value nonzero so pushdecl knows this is not tentative.
1.1.1.2 root 2848: error_mark_node is replaced below (in poplevel) with the LET_STMT. */
2849: DECL_INITIAL (current_function_decl) = error_mark_node;
2850:
2851: /* If this definition isn't a prototype and we had a prototype declaration
2852: before, copy the arg type info from that prototype. */
2853: old_decl = lookup_name_current_level (DECL_NAME (current_function_decl));
2854: if (old_decl != 0
2855: && TREE_TYPE (TREE_TYPE (current_function_decl)) == TREE_TYPE (TREE_TYPE (old_decl))
2856: && TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)) == 0)
2857: TREE_TYPE (current_function_decl) = TREE_TYPE (old_decl);
2858:
2859: /* This is a definition, not a reference. */
2860: TREE_EXTERNAL (current_function_decl) = 0;
2861: /* This function exists in static storage.
2862: (This does not mean `static' in the C sense!) */
2863: TREE_STATIC (current_function_decl) = 1;
2864:
2865: /* Record the decl so that the function name is defined.
2866: If we already have a decl for this name, and it is a FUNCTION_DECL,
2867: use the old decl. */
1.1 root 2868:
1.1.1.2 root 2869: current_function_decl = pushdecl (current_function_decl);
1.1 root 2870:
1.1.1.2 root 2871: pushlevel (0);
2872: current_binding_level->parm_flag = 1;
1.1 root 2873:
2874: make_function_rtl (current_function_decl);
2875:
2876: /* Allocate further tree nodes temporarily during compilation
2877: of this function only. */
2878: temporary_allocation ();
2879:
1.1.1.3 root 2880: restype = TREE_TYPE (TREE_TYPE (current_function_decl));
2881: /* Promote the value to int before returning it. */
2882: if (TREE_CODE (restype) == INTEGER_TYPE
2883: && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
2884: restype = integer_type_node;
1.1 root 2885: DECL_RESULT (current_function_decl)
1.1.1.3 root 2886: = build_decl (RESULT_DECL, value_identifier, restype);
1.1 root 2887:
2888: /* Make the FUNCTION_DECL's contents appear in a local tree dump
2889: and make the FUNCTION_DECL itself not appear in the permanent dump. */
2890:
2891: TREE_PERMANENT (current_function_decl) = 0;
2892:
1.1.1.4 root 2893: /* If this fcn was already referenced via a block-scope `extern' decl
2894: (or an implicit decl), propagate certain information about the usage. */
2895: if (TREE_ADDRESSABLE (DECL_NAME (current_function_decl)))
2896: TREE_ADDRESSABLE (current_function_decl) = 1;
2897:
1.1 root 2898: return 1;
2899: }
2900:
2901: /* Store the parameter declarations into the current function declaration.
2902: This is called after parsing the parameter declarations, before
2903: digesting the body of the function. */
2904:
2905: void
2906: store_parm_decls ()
2907: {
2908: register tree fndecl = current_function_decl;
2909: register tree parm;
2910:
1.1.1.2 root 2911: /* This is either a chain of PARM_DECLs (if a prototype was used)
2912: or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */
2913: tree specparms = current_function_parms;
1.1 root 2914:
1.1.1.2 root 2915: /* This is a list of types declared among parms in a prototype. */
2916: tree parmtags = current_function_parm_tags;
1.1 root 2917:
1.1.1.2 root 2918: /* This is a chain of PARM_DECLs from old-style parm declarations. */
2919: register tree parmdecls = getdecls ();
1.1 root 2920:
1.1.1.2 root 2921: /* This is a chain of any other decls that came in among the parm
2922: declarations. If a parm is declared with enum {foo, bar} x;
2923: then CONST_DECLs for foo and bar are put here. */
2924: tree nonparms = 0;
2925:
2926: if (specparms != 0 && TREE_CODE (specparms) == PARM_DECL)
2927: {
2928: /* This case is when the function was defined with an ANSI prototype.
2929: The parms already have decls, so we need not do anything here
2930: except record them as in effect
2931: and complain if any redundant old-style parm decls were written. */
2932:
2933: register tree next;
2934:
2935: if (parmdecls != 0)
2936: error_with_decl (fndecl,
2937: "parm types given both in parmlist and separately");
2938:
2939: for (parm = nreverse (specparms); parm; parm = next)
2940: {
2941: next = TREE_CHAIN (parm);
2942: if (DECL_NAME (parm) == 0)
2943: error_with_decl (parm, "parameter name omitted");
2944: else if (TREE_TYPE (parm) == void_type_node)
2945: error_with_decl (parm, "parameter `%s' declared void");
2946: else
2947: pushdecl (parm);
1.1 root 2948: }
2949:
1.1.1.2 root 2950: /* Get the decls in their original chain order
2951: and record in the function. */
2952: DECL_ARGUMENTS (fndecl) = getdecls ();
1.1 root 2953:
1.1.1.2 root 2954: storetags (chainon (parmtags, gettags ()));
2955: }
2956: else
2957: {
2958: /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes
2959: each with a parm name as the TREE_VALUE.
2960:
2961: PARMDECLS is a list of declarations for parameters.
2962: Warning! It can also contain CONST_DECLs which are not parameters
2963: but are names of enumerators of any enum types
2964: declared among the parameters.
2965:
2966: First match each formal parameter name with its declaration.
2967: Associate decls with the names and store the decls
2968: into the TREE_PURPOSE slots. */
1.1 root 2969:
1.1.1.2 root 2970: for (parm = specparms; parm; parm = TREE_CHAIN (parm))
1.1 root 2971: {
1.1.1.2 root 2972: register tree tail, found = NULL;
2973:
2974: if (TREE_VALUE (parm) == 0)
2975: {
2976: error_with_decl (fndecl, "parameter name missing from parameter list");
2977: TREE_PURPOSE (parm) = 0;
2978: continue;
2979: }
1.1 root 2980:
1.1.1.2 root 2981: /* See if any of the parmdecls specifies this parm by name.
2982: Ignore any enumerator decls. */
2983: for (tail = parmdecls; tail; tail = TREE_CHAIN (tail))
2984: if (DECL_NAME (tail) == TREE_VALUE (parm)
2985: && TREE_CODE (tail) == PARM_DECL)
2986: {
2987: found = tail;
2988: break;
2989: }
2990:
2991: /* If declaration already marked, we have a duplicate name.
2992: Complain, and don't use this decl twice. */
2993: if (found && DECL_CONTEXT (found) != 0)
2994: {
2995: error_with_decl (found, "multiple parameters named `%s'");
2996: found = 0;
2997: }
1.1 root 2998:
1.1.1.2 root 2999: /* If the declaration says "void", complain and ignore it. */
3000: if (found && TREE_TYPE (found) == void_type_node)
3001: {
3002: error_with_decl (found, "parameter `%s' declared void");
3003: TREE_TYPE (found) = integer_type_node;
3004: DECL_ARG_TYPE (found) = integer_type_node;
3005: layout_decl (found, 0);
3006: }
1.1 root 3007:
1.1.1.2 root 3008: /* If no declaration found, default to int. */
3009: if (!found)
3010: {
3011: found = build_decl (PARM_DECL, TREE_VALUE (parm),
3012: integer_type_node);
3013: DECL_ARG_TYPE (found) = TREE_TYPE (found);
3014: DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl);
3015: DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl);
3016: pushdecl (found);
3017: }
1.1 root 3018:
1.1.1.2 root 3019: TREE_PURPOSE (parm) = found;
1.1 root 3020:
1.1.1.2 root 3021: /* Mark this decl as "already found" -- see test, above.
3022: It is safe to clobber DECL_CONTEXT temporarily
3023: because the final values are not stored until
3024: the `poplevel' in `finish_function'. */
3025: DECL_CONTEXT (found) = error_mark_node;
3026: }
1.1 root 3027:
1.1.1.2 root 3028: /* Complain about declarations not matched with any names.
3029: Put any enumerator constants onto the list NONPARMS. */
3030:
3031: nonparms = 0;
3032: for (parm = parmdecls; parm; )
1.1 root 3033: {
1.1.1.2 root 3034: tree next = TREE_CHAIN (parm);
3035: TREE_CHAIN (parm) = 0;
3036:
1.1.1.3 root 3037: /* Complain about args with incomplete types. */
3038: if (TYPE_SIZE (TREE_TYPE (parm)) == 0)
3039: {
3040: error_with_decl (parm, "parameter `%s' has incomplete type");
3041: TREE_TYPE (parm) = error_mark_node;
3042: }
3043:
1.1.1.2 root 3044: if (TREE_CODE (parm) != PARM_DECL)
3045: nonparms = chainon (nonparms, parm);
3046:
3047: else if (DECL_CONTEXT (parm) == 0)
3048: {
3049: error_with_decl (parm,
3050: "declaration for parameter `%s' but no such parameter");
3051: /* Pretend the parameter was not missing.
3052: This gets us to a standard state and minimizes
3053: further error messages. */
3054: specparms
3055: = chainon (specparms,
3056: tree_cons (parm, NULL_TREE, NULL_TREE));
3057: }
3058:
3059: parm = next;
1.1 root 3060: }
1.1.1.2 root 3061:
3062: /* Chain the declarations together in the order of the list of names. */
3063: /* Store that chain in the function decl, replacing the list of names. */
3064: parm = specparms;
3065: DECL_ARGUMENTS (fndecl) = 0;
3066: {
3067: register tree last;
3068: for (last = 0; parm; parm = TREE_CHAIN (parm))
3069: if (TREE_PURPOSE (parm))
3070: {
3071: if (last == 0)
3072: DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm);
3073: else
3074: TREE_CHAIN (last) = TREE_PURPOSE (parm);
3075: last = TREE_PURPOSE (parm);
3076: TREE_CHAIN (last) = 0;
3077: DECL_CONTEXT (last) = 0;
3078: }
3079: }
3080:
3081: /* If there was a previous prototype,
3082: set the DECL_ARG_TYPE of each argument according to
3083: the type previously specified, and report any mismatches. */
3084:
3085: if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
3086: {
3087: register tree type;
3088: for (parm = DECL_ARGUMENTS (fndecl),
3089: type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
3090: parm || (type && TREE_VALUE (type) != void_type_node);
3091: parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
3092: {
3093: if (parm == 0 || type == 0
3094: || TREE_VALUE (type) == void_type_node)
3095: {
3096: error ("argument decls of `%s' don't match prototype",
3097: IDENTIFIER_POINTER (DECL_NAME (fndecl)));
3098: break;
3099: }
3100: /* Type for passing arg must be consistent
3101: with that declared for the arg. */
3102: if (! comptypes (DECL_ARG_TYPE (parm), TREE_VALUE (type)))
3103: error ("argument `%s' doesn't match function prototype",
3104: IDENTIFIER_POINTER (DECL_NAME (parm)));
3105: }
3106: }
3107: }
1.1 root 3108:
3109: /* Now store the final chain of decls for the arguments
1.1.1.2 root 3110: as the decl-chain of the current lexical scope.
3111: Put the enumerators in as well, at the front so that
3112: DECL_ARGUMENTS is not modified. */
1.1 root 3113:
1.1.1.2 root 3114: storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
1.1 root 3115:
1.1.1.2 root 3116: /* Initialize the RTL code for the function. */
1.1 root 3117:
1.1.1.2 root 3118: expand_function_start (fndecl);
1.1 root 3119: }
3120:
3121: /* Finish up a function declaration and compile that function
3122: all the way to assembler language output. The free the storage
3123: for the function definition.
3124:
3125: This is called after parsing the body of the function definition.
3126: STMTS is the chain of statements that makes up the function body. */
3127:
3128: void
1.1.1.3 root 3129: finish_function ()
1.1 root 3130: {
3131: register tree fndecl = current_function_decl;
3132:
3133: /* TREE_READONLY (fndecl) = 1;
3134: This caused &foo to be of type ptr-to-const-function
3135: which then got a warning when stored in a ptr-to-function variable. */
3136:
1.1.1.2 root 3137: poplevel (1, 0, 1);
1.1 root 3138:
1.1.1.2 root 3139: /* Must mark the RESULT_DECL as being in this function. */
1.1 root 3140:
1.1.1.2 root 3141: DECL_CONTEXT (DECL_RESULT (fndecl)) = DECL_INITIAL (fndecl);
1.1 root 3142:
1.1.1.2 root 3143: /* Generate rtl for function exit. */
3144: expand_function_end ();
1.1 root 3145:
1.1.1.2 root 3146: if (warn_return_type)
3147: /* So we can tell if jump_optimize sets it to 1. */
3148: current_function_returns_null = 0;
3149:
3150: /* Run the optimizers and output the assembler code for this function. */
3151: rest_of_compilation (fndecl);
3152:
3153: if (warn_return_type && current_function_returns_null
3154: && TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node)
3155: /* If this function returns non-void and control can drop through,
3156: complain. */
3157: warning ("control reaches end of non-void function");
3158: /* With just -W, complain only if function returns both with
3159: and without a value. */
3160: else if (extra_warnings
3161: && current_function_returns_value && current_function_returns_null)
3162: warning ("this function may return with or without a value");
1.1 root 3163:
3164: /* Free all the tree nodes making up this function. */
3165: /* Switch back to allocating nodes permanently
3166: until we start another function. */
3167: permanent_allocation ();
3168:
1.1.1.2 root 3169: if (DECL_SAVED_INSNS (fndecl) == 0)
1.1 root 3170: {
1.1.1.2 root 3171: /* Stop pointing to the local nodes about to be freed. */
3172: /* But DECL_INITIAL must remain nonzero so we know this
3173: was an actual function definition. */
3174: DECL_INITIAL (fndecl) = error_mark_node;
3175: DECL_ARGUMENTS (fndecl) = 0;
3176: DECL_RESULT (fndecl) = 0;
1.1 root 3177: }
1.1.1.2 root 3178:
3179: /* Let the error reporting routines know that we're outside a function. */
3180: current_function_decl = NULL;
1.1 root 3181: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.