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