|
|
1.1 ! root 1: /* Process declarations and variables for C compiler. ! 2: Copyright (C) 1987 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 "parse.h" ! 31: #include "tree.h" ! 32: #include "c-tree.h" ! 33: ! 34: /* In grokdeclarator, distinguish syntactic contexts of declarators. */ ! 35: enum decl_context ! 36: { NORMAL, /* Ordinary declaration */ ! 37: PARM, /* Declaration of parm before function body */ ! 38: FIELD, /* Declaration inside struct or union */ ! 39: TYPENAME}; /* Typename (inside cast or sizeof) */ ! 40: ! 41: #define NULL 0 ! 42: #define min(X,Y) ((X) < (Y) ? (X) : (Y)) ! 43: ! 44: static tree grokparms (), grokdeclarator (); ! 45: static tree make_index_type (); ! 46: static void builtin_function (); ! 47: ! 48: static tree lookup_labelname (); ! 49: static tree lookup_tag (); ! 50: static tree lookup_name_current_level (); ! 51: ! 52: /* a node which has tree code ERROR_MARK, and whose type is itself. ! 53: All erroneous expressions are replaced with this node. All functions ! 54: that accept nodes as arguments should avoid generating error messages ! 55: if this node is one of the arguments, since it is undesirable to get ! 56: multiple error messages from one error in the input. */ ! 57: ! 58: tree error_mark_node; ! 59: ! 60: /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */ ! 61: ! 62: tree short_integer_type_node; ! 63: tree integer_type_node; ! 64: tree long_integer_type_node; ! 65: ! 66: tree short_unsigned_type_node; ! 67: tree unsigned_type_node; ! 68: tree long_unsigned_type_node; ! 69: ! 70: tree unsigned_char_type_node; ! 71: tree char_type_node; ! 72: ! 73: tree float_type_node; ! 74: tree double_type_node; ! 75: tree long_double_type_node; ! 76: ! 77: /* a VOID_TYPE node. */ ! 78: ! 79: tree void_type_node; ! 80: ! 81: /* A node for type `void *'. */ ! 82: ! 83: tree ptr_type_node; ! 84: ! 85: /* A node for type `char *'. */ ! 86: ! 87: tree string_type_node; ! 88: ! 89: /* Type `char[256]' or something like it. ! 90: Used when an array of char is needed and the size is irrelevant. */ ! 91: ! 92: tree char_array_type_node; ! 93: ! 94: /* type `int ()' -- used for implicit declaration of functions. */ ! 95: ! 96: tree default_function_type; ! 97: ! 98: /* function types `double (double)' and `double (double, double)', etc. */ ! 99: ! 100: tree double_ftype_double, double_ftype_double_double; ! 101: tree int_ftype_int, long_ftype_long; ! 102: ! 103: /* Function type `void (void *, void *, int)' and similar ones */ ! 104: ! 105: tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int; ! 106: ! 107: /* Two expressions that are constants with value zero. ! 108: The first is of type `int', the second of type `void *'. */ ! 109: ! 110: tree integer_zero_node; ! 111: tree null_pointer_node; ! 112: ! 113: /* A node for the integer constant 1. */ ! 114: ! 115: tree integer_one_node; ! 116: ! 117: /* An identifier whose name is <value>. This is used as the "name" ! 118: of the RESULT_DECLs for values of functions. */ ! 119: ! 120: tree value_identifier; ! 121: ! 122: /* Enumeration type currently being built, or 0 if not doing that now. */ ! 123: ! 124: tree current_enum_type; ! 125: ! 126: /* Default value for next enumerator of enumeration type currently being read, ! 127: or undefined at other times. */ ! 128: ! 129: int enum_next_value; ! 130: ! 131: /* Parsing a function declarator leaves a list of parameter names here. ! 132: If the declarator is the beginning of a function definition, ! 133: the names are stored into the function declaration when it is created. */ ! 134: ! 135: static tree last_function_parm_names; ! 136: ! 137: /* A list (chain of TREE_LIST nodes) of all LABEL_STMTs in the function ! 138: that have names. Here so we can clear out their names' definitions ! 139: at the end of the function. */ ! 140: ! 141: static tree named_labels; ! 142: ! 143: /* A list of all GOTO_STMT nodes in the current function, ! 144: so we can fill in the LABEL_STMTs they go to once all are defined. */ ! 145: ! 146: static tree all_gotos; ! 147: ! 148: /* Set to 0 at beginning of a function definition, set to 1 if ! 149: a return statement that specifies a return value is seen. */ ! 150: ! 151: int current_function_returns_value; ! 152: ! 153: /* For each binding contour we allocate a binding_level structure ! 154: * which records the names defined in that contour. ! 155: * Contours include: ! 156: * 0) the global one ! 157: * 1) one for each function definition, ! 158: * where internal declarations of the parameters appear. ! 159: * 2) one for each compound statement, ! 160: * to record its declarations. ! 161: * ! 162: * The current meaning of a name can be found by searching the levels from ! 163: * the current one out to the global one. ! 164: */ ! 165: ! 166: /* Note that the information in the `names' component of the global contour ! 167: is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ ! 168: ! 169: static struct binding_level ! 170: { ! 171: /* A chain of _DECL nodes for all variables, constants, functions, ! 172: and typedef types. These are in the reverse of the order supplied. ! 173: */ ! 174: tree names; ! 175: ! 176: /* A list of structure, union and enum definitions, ! 177: * for looking up tag names. ! 178: * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name, ! 179: * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE, ! 180: * or ENUMERAL_TYPE node. ! 181: */ ! 182: tree tags; ! 183: ! 184: /* For each level, a list of shadowed outer-level local definitions ! 185: to be restored when this level is popped. ! 186: Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and ! 187: whose TREE_VALUE is its old definition (a kind of ..._DECL node). */ ! 188: tree shadowed; ! 189: ! 190: /* The binding level which this one is contained in (inherits from). */ ! 191: struct binding_level *level_chain; ! 192: }; ! 193: ! 194: #define NULL_BINDING_LEVEL (struct binding_level *) NULL ! 195: ! 196: /* The binding level currently in effect. */ ! 197: ! 198: static struct binding_level *current_binding_level; ! 199: ! 200: /* A chain of binding_level structures awaiting reuse. */ ! 201: ! 202: static struct binding_level *free_binding_level; ! 203: ! 204: /* The outermost binding level, for names of file scope. ! 205: This is created when the compiler is started and exists ! 206: through the entire run. */ ! 207: ! 208: static struct binding_level *global_binding_level; ! 209: ! 210: /* Binding level structures are initialized by copying this one. */ ! 211: ! 212: static struct binding_level clear_binding_level = ! 213: {NULL, NULL, NULL, NULL}; ! 214: ! 215: /* Create a new `struct binding_level'. */ ! 216: ! 217: static ! 218: struct binding_level * ! 219: make_binding_level () ! 220: { ! 221: /* NOSTRICT */ ! 222: return (struct binding_level *) xmalloc (sizeof (struct binding_level)); ! 223: } ! 224: ! 225: /* Enter a new binding level. */ ! 226: ! 227: void ! 228: pushlevel () ! 229: { ! 230: register struct binding_level *newlevel = NULL_BINDING_LEVEL; ! 231: ! 232: /* If this is the top level of a function, ! 233: just make sure that ALL_GOTOS and NAMED_LABELS are 0. ! 234: They should have been set to 0 at the end of the previous function. */ ! 235: ! 236: if (current_binding_level == global_binding_level) ! 237: { ! 238: if (all_gotos || named_labels) ! 239: abort (); ! 240: } ! 241: ! 242: /* Reuse or create a struct for this binding level. */ ! 243: ! 244: if (free_binding_level) ! 245: { ! 246: newlevel = free_binding_level; ! 247: free_binding_level = free_binding_level->level_chain; ! 248: } ! 249: else ! 250: { ! 251: newlevel = make_binding_level (); ! 252: } ! 253: ! 254: /* Add this level to the front of the chain (stack) of levels that ! 255: are active. */ ! 256: ! 257: *newlevel = clear_binding_level; ! 258: newlevel->level_chain = current_binding_level; ! 259: current_binding_level = newlevel; ! 260: } ! 261: ! 262: /* Exit a binding level. */ ! 263: ! 264: void ! 265: poplevel () ! 266: { ! 267: register tree link; ! 268: ! 269: /* Clear out the meanings of the local variables of this level. */ ! 270: ! 271: for (link = current_binding_level->names; link; link = TREE_CHAIN (link)) ! 272: IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0; ! 273: ! 274: /* Restore all name-meanings of the outer levels ! 275: that were shadowed by this level. */ ! 276: ! 277: for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link)) ! 278: IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link); ! 279: ! 280: /* If the level being exited is the top level of a function, ! 281: match all goto statements with their labels. */ ! 282: ! 283: if (current_binding_level->level_chain == global_binding_level) ! 284: { ! 285: /* Search for labels for any unresolved gotos. */ ! 286: ! 287: for (link = all_gotos; link; link = TREE_CHAIN (link)) ! 288: { ! 289: register tree stmt = TREE_VALUE (link); ! 290: register tree label = lookup_labelname (STMT_BODY (stmt)); ! 291: ! 292: if (label) ! 293: STMT_BODY (stmt) = label; ! 294: else ! 295: { ! 296: yylineerror (STMT_SOURCE_LINE (stmt), ! 297: "no label %s visible for goto", ! 298: IDENTIFIER_POINTER (STMT_BODY (stmt))); ! 299: STMT_BODY (stmt) = NULL_TREE; ! 300: } ! 301: } ! 302: ! 303: /* Then clear out the definitions of all label names, ! 304: since their scopes end here. */ ! 305: ! 306: for (link = named_labels; link; link = TREE_CHAIN (link)) ! 307: IDENTIFIER_LABEL_VALUE (DECL_NAME (STMT_BODY (TREE_VALUE (link)))) = 0; ! 308: ! 309: named_labels = 0; ! 310: all_gotos = 0; ! 311: } ! 312: ! 313: /* Pop the current level, and free the structure for reuse. */ ! 314: ! 315: { ! 316: register struct binding_level *level = current_binding_level; ! 317: current_binding_level = current_binding_level->level_chain; ! 318: ! 319: level->level_chain = free_binding_level; ! 320: free_binding_level = level; ! 321: } ! 322: } ! 323: ! 324: /* Push a definition of struct, union or enum tag "name". ! 325: "type" should be the type node. ! 326: Note that the definition may really be just a forward reference. ! 327: In that case, the TYPE_SIZE will be zero. */ ! 328: ! 329: void ! 330: pushtag (name, type) ! 331: tree name, type; ! 332: { ! 333: register tree t; ! 334: ! 335: /* If the type has a name, check for duplicate definitions of the name. */ ! 336: ! 337: if (name) ! 338: { ! 339: for (t = current_binding_level->tags; t; t = TREE_CHAIN (t)) ! 340: if (TREE_PURPOSE (t) == name) ! 341: { ! 342: yyerror ("redeclaration of struct, union or enum tag %s", ! 343: IDENTIFIER_POINTER (name)); ! 344: return; ! 345: } ! 346: ! 347: /* Record the identifier as the type's name if it has none. */ ! 348: ! 349: if (TYPE_NAME (type) == 0) ! 350: TYPE_NAME (type) = name; ! 351: } ! 352: ! 353: current_binding_level->tags ! 354: = tree_cons (name, type, current_binding_level->tags); ! 355: } ! 356: ! 357: ! 358: /* Handle when a new declaration X has the same name as an old one T ! 359: in the same binding contour. ! 360: May alter the old decl to say what the new one says. ! 361: ! 362: Returns the old decl T if the two are more or less compatible; ! 363: returns the new one X if they are thoroughly alien. */ ! 364: ! 365: static tree ! 366: duplicate_decls (x, t) ! 367: register tree x, t; ! 368: { ! 369: /* At top level in file, if the old definition is "tentative" and ! 370: this one is close enough, no error. */ ! 371: if (! allowed_redeclaration (x, t, ! 372: current_binding_level == global_binding_level)) ! 373: yylineerror (DECL_SOURCE_LINE (x), "redeclaration of %s", ! 374: IDENTIFIER_POINTER (DECL_NAME (x))); ! 375: ! 376: /* Install latest semantics. */ ! 377: if (TREE_CODE (t) == TREE_CODE (x)) ! 378: { ! 379: bcopy ((char *) x + sizeof (struct tree_shared), ! 380: (char *) t + sizeof (struct tree_shared), ! 381: sizeof (struct tree_decl) - sizeof (struct tree_shared)); ! 382: TREE_TYPE (t) = TREE_TYPE (x); ! 383: DECL_ARGUMENTS (t) = DECL_ARGUMENTS (x); ! 384: DECL_RESULT (t) = DECL_RESULT (x); ! 385: DECL_SOURCE_FILE (t) = DECL_SOURCE_FILE (x); ! 386: DECL_SOURCE_LINE (t) = DECL_SOURCE_LINE (x); ! 387: TREE_STATIC (t) = TREE_STATIC (x); ! 388: TREE_EXTERNAL (t) = TREE_EXTERNAL (x); ! 389: TREE_PUBLIC (t) = TREE_PUBLIC (x); ! 390: if (DECL_INITIAL (x)) ! 391: DECL_INITIAL (t) = DECL_INITIAL (x); ! 392: return t; ! 393: } ! 394: return x; ! 395: } ! 396: ! 397: /* Record a decl-node X as belonging to the current lexical scope. ! 398: Check for errors (such as an incompatible declaration for the same ! 399: name already seen in the same scope). ! 400: ! 401: Returns either X or an old decl for the same name. ! 402: If an old decl is returned, it has been smashed ! 403: to agree with what X says. */ ! 404: ! 405: tree ! 406: pushdecl (x) ! 407: tree x; ! 408: { ! 409: register tree t; ! 410: register tree name = DECL_NAME (x); ! 411: ! 412: if (name) ! 413: { ! 414: t = lookup_name_current_level (name); ! 415: if (t) ! 416: return duplicate_decls (x, t); ! 417: ! 418: /* If declaring a type as a typedef, and the type has no known ! 419: typedef name, install this TYPE_DECL as its typedef name. */ ! 420: if (TREE_CODE (x) == TYPE_DECL) ! 421: if (TYPE_NAME (TREE_TYPE (x)) == 0 ! 422: || TREE_CODE (TYPE_NAME (TREE_TYPE (x))) != TYPE_DECL) ! 423: TYPE_NAME (TREE_TYPE (x)) = x; ! 424: } ! 425: ! 426: /* This name is new. ! 427: Install the new declaration and return it. */ ! 428: if (current_binding_level == global_binding_level) ! 429: { ! 430: IDENTIFIER_GLOBAL_VALUE (name) = x; ! 431: } ! 432: else ! 433: { ! 434: /* If storing a local value, there may already be one (inherited). ! 435: If so, record it for restoration when this binding level ends. */ ! 436: if (IDENTIFIER_LOCAL_VALUE (name)) ! 437: current_binding_level->shadowed ! 438: = tree_cons (name, IDENTIFIER_LOCAL_VALUE (name), ! 439: current_binding_level->shadowed); ! 440: IDENTIFIER_LOCAL_VALUE (name) = x; ! 441: } ! 442: ! 443: /* Put decls on list in reverse order. ! 444: We will reverse them later if necessary. */ ! 445: current_binding_level->names = chainon (x, current_binding_level->names); ! 446: ! 447: return x; ! 448: } ! 449: ! 450: /* Record a C label name. X is a LABEL_STMT and its STMT_BODY is a LABEL_DECL. ! 451: That is where we find the name. */ ! 452: ! 453: void ! 454: pushlabel (x) ! 455: tree x; ! 456: { ! 457: register tree decl = STMT_BODY (x); ! 458: ! 459: if (0 == DECL_NAME (decl)) ! 460: return; ! 461: ! 462: if (IDENTIFIER_LABEL_VALUE (DECL_NAME (decl))) ! 463: yyerror ("duplicate label %s", ! 464: IDENTIFIER_POINTER (DECL_NAME (decl))); ! 465: else ! 466: IDENTIFIER_LABEL_VALUE (DECL_NAME (decl)) = decl; ! 467: ! 468: named_labels ! 469: = tree_cons (NULL_TREE, x, named_labels); ! 470: } ! 471: ! 472: /* Record the goto statement X on the list of all gotos in the ! 473: current function. The list is used to find them all at ! 474: the end of the function so that their target labels can be found then. */ ! 475: ! 476: void ! 477: pushgoto (x) ! 478: tree x; ! 479: { ! 480: all_gotos = tree_cons (NULL_TREE, x, all_gotos); ! 481: } ! 482: ! 483: /* Return the list of declarations of the current level. ! 484: They are pushed on the list in reverse order since that is easiest. ! 485: We reverse them to the correct order here. */ ! 486: ! 487: tree ! 488: getdecls () ! 489: { ! 490: return ! 491: current_binding_level->names = nreverse (current_binding_level->names); ! 492: } ! 493: ! 494: /* Return the list of type-tags (for structs, etc) of the current level. */ ! 495: ! 496: tree ! 497: gettags () ! 498: { ! 499: return current_binding_level->tags; ! 500: } ! 501: ! 502: /* Store the list of declarations of the current level. ! 503: This is done for the parameter declarations of a function being defined, ! 504: after they are modified in the light of any missing parameters. */ ! 505: ! 506: void ! 507: storedecls (decls) ! 508: tree decls; ! 509: { ! 510: current_binding_level->names = decls; ! 511: } ! 512: ! 513: /* Given NAME, an IDENTIFIER_NODE, ! 514: return the structure (or union or enum) definition for that name. ! 515: Searches binding levels from BINDING_LEVEL up to the global level. ! 516: If THISLEVEL_ONLY is nonzero, searches only the specified context. ! 517: FORM says which kind of type the caller wants; ! 518: it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. ! 519: If the wrong kind of type is found, an error is reported. */ ! 520: ! 521: static tree ! 522: lookup_tag (form, name, binding_level, thislevel_only) ! 523: enum tree_code form; ! 524: struct binding_level *binding_level; ! 525: tree name; ! 526: int thislevel_only; ! 527: { ! 528: register struct binding_level *level; ! 529: ! 530: for (level = binding_level; level; level = level->level_chain) ! 531: { ! 532: register tree tail; ! 533: for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) ! 534: { ! 535: if (TREE_PURPOSE (tail) == name) ! 536: { ! 537: if (TREE_CODE (TREE_VALUE (tail)) != form) ! 538: { ! 539: /* Definition isn't the kind we were looking for. */ ! 540: yyerror ("%s defined as wrong kind of tag", ! 541: IDENTIFIER_POINTER (name)); ! 542: } ! 543: return TREE_VALUE (tail); ! 544: } ! 545: } ! 546: if (thislevel_only) ! 547: return NULL_TREE; ! 548: } ! 549: return NULL_TREE; ! 550: } ! 551: ! 552: /* Look up NAME in the current binding level and its superiors ! 553: in the namespace of variables, functions and typedefs. ! 554: Return a ..._DECL node of some kind representing its definition, ! 555: or return 0 if it is undefined. */ ! 556: ! 557: tree ! 558: lookup_name (name) ! 559: tree name; ! 560: { ! 561: if (current_binding_level != global_binding_level ! 562: && IDENTIFIER_LOCAL_VALUE (name)) ! 563: return IDENTIFIER_LOCAL_VALUE (name); ! 564: return IDENTIFIER_GLOBAL_VALUE (name); ! 565: } ! 566: ! 567: /* Similar to `lookup_name' but look only at current binding level. */ ! 568: ! 569: static tree ! 570: lookup_name_current_level (name) ! 571: tree name; ! 572: { ! 573: register tree t; ! 574: ! 575: if (current_binding_level == global_binding_level) ! 576: return IDENTIFIER_GLOBAL_VALUE (name); ! 577: ! 578: if (IDENTIFIER_LOCAL_VALUE (name) == 0) ! 579: return 0; ! 580: ! 581: for (t = current_binding_level->names; t; t = TREE_CHAIN (t)) ! 582: if (DECL_NAME (t) == name) ! 583: break; ! 584: ! 585: return t; ! 586: } ! 587: ! 588: /* Return the definition of NAME as a label (a LABEL-DECL node), ! 589: or 0 if it has no definition as a label. */ ! 590: ! 591: static ! 592: tree ! 593: lookup_labelname (name) ! 594: tree name; ! 595: { ! 596: return IDENTIFIER_LABEL_VALUE (name); ! 597: } ! 598: ! 599: /* Create a DECL_... node of code CODE, name NAME and data type TYPE. ! 600: STATICP nonzero means this is declared `static' in the C sense; ! 601: EXTERNP means it is declared `extern' in the C sense. ! 602: The name's definition is *not* entered in the symbol table. ! 603: ! 604: The source file and line number are left 0. ! 605: layout_decl is used to set up the decl's storage layout. ! 606: are initialized to 0 or null pointers. */ ! 607: ! 608: tree ! 609: build_decl (code, name, type, staticp, externp) ! 610: enum tree_code code; ! 611: tree name, type; ! 612: int staticp, externp; ! 613: { ! 614: register tree t; ! 615: ! 616: t = make_node (code); ! 617: ! 618: /* if (type == error_mark_node) ! 619: type = integer_type_node; */ ! 620: /* That is not done, deliberately, so that having error_mark_node ! 621: as the type can suppress useless errors in the use of this variable. */ ! 622: ! 623: DECL_NAME (t) = name; ! 624: TREE_TYPE (t) = type; ! 625: DECL_ARGUMENTS (t) = NULL_TREE; ! 626: DECL_INITIAL (t) = NULL_TREE; ! 627: if (externp) ! 628: TREE_EXTERNAL (t) = 1; ! 629: if (staticp) ! 630: TREE_STATIC (t) = 1; ! 631: ! 632: if (current_binding_level == global_binding_level) ! 633: { ! 634: if (!TREE_STATIC (t)) ! 635: TREE_PUBLIC (t) = 1; ! 636: TREE_STATIC (t) = (code != FUNCTION_DECL); ! 637: } ! 638: ! 639: if (TREE_EXTERNAL (t)) ! 640: TREE_STATIC (t) = 0; ! 641: ! 642: if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL) ! 643: layout_decl (t, 0); ! 644: else if (code == FUNCTION_DECL) ! 645: /* all functions considered external unless def in this file */ ! 646: { ! 647: TREE_EXTERNAL (t) = 1; ! 648: DECL_MODE (t) = FUNCTION_MODE; ! 649: } ! 650: return t; ! 651: } ! 652: ! 653: /* Create a LABEL_STMT statement node containing a LABEL_DECL named NAME. ! 654: NAME may be a null pointer. ! 655: FILE ane LINENUM say where in the source the label is. ! 656: CONTEXT is the LET_STMT node which is the context of this label name. ! 657: The label name definition is entered in the symbol table. */ ! 658: ! 659: tree ! 660: build_label (filename, line, name, context) ! 661: char *filename; ! 662: int line; ! 663: tree name; ! 664: tree context; ! 665: { ! 666: register tree t = make_node (LABEL_STMT); ! 667: register tree decl = build_decl (LABEL_DECL, name, NULL_TREE, 1, 0); ! 668: ! 669: STMT_SOURCE_FILE (t) = filename; ! 670: STMT_SOURCE_LINE (t) = line; ! 671: STMT_BODY (t) = decl; ! 672: DECL_SOURCE_FILE (decl) = filename; ! 673: DECL_SOURCE_LINE (decl) = line; ! 674: DECL_MODE (decl) = VOIDmode; ! 675: DECL_CONTEXT (decl) = context; ! 676: pushlabel (t); ! 677: return t; ! 678: } ! 679: ! 680: /* Store the data into a LET_STMT node. ! 681: Each C braced grouping with declarations is represented ! 682: by a LET_STMT node. The node is created when the open-brace is read, ! 683: but the contents to put in it are not known until the close-brace. ! 684: This function is called at the time of the close-brace ! 685: to install the proper contents. ! 686: ! 687: BLOCK is the LET_STMT node itself. ! 688: DCLS is the chain of declarations within the grouping. ! 689: TAGS is the chain of struct, union and enum tags defined within it. ! 690: STMTS is the chain of statements making up the inside of the grouping. */ ! 691: ! 692: finish_block (block, dcls, tags, stmts) ! 693: tree block, dcls, tags, stmts; ! 694: { ! 695: register tree tem; ! 696: ! 697: /* In each decl, record the block it belongs to. */ ! 698: for (tem = dcls; tem; tem = TREE_CHAIN (tem)) ! 699: DECL_CONTEXT (tem) = block; ! 700: ! 701: STMT_VARS (block) = dcls; ! 702: STMT_BODY (block) = stmts; ! 703: STMT_TYPE_TAGS (block) = tags; ! 704: } ! 705: ! 706: finish_tree () ! 707: { ! 708: } ! 709: ! 710: /* Create the predefined scalar types of C, ! 711: and some nodes representing standard constants (0, 1, (void *)0). ! 712: Initialize the global binding level. ! 713: Make definitions for built-in primitive functions. */ ! 714: ! 715: void ! 716: init_decl_processing () ! 717: { ! 718: register tree endlink; ! 719: ! 720: named_labels = NULL; ! 721: all_gotos = NULL; ! 722: current_binding_level = NULL_BINDING_LEVEL; ! 723: free_binding_level = NULL_BINDING_LEVEL; ! 724: pushlevel (); /* make the binding_level structure for global names */ ! 725: global_binding_level = current_binding_level; ! 726: ! 727: value_identifier = get_identifier ("<value>"); ! 728: ! 729: /* This must be the first type made and laid out, ! 730: so that it will get used as the type for expressions ! 731: for the sizes of types. */ ! 732: ! 733: integer_type_node = make_signed_type (BITS_PER_WORD); ! 734: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT], ! 735: integer_type_node, 1, 0)); ! 736: ! 737: error_mark_node = make_node (ERROR_MARK); ! 738: TREE_TYPE (error_mark_node) = error_mark_node; ! 739: ! 740: short_integer_type_node = make_signed_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2)); ! 741: pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"), ! 742: short_integer_type_node, 1, 0)); ! 743: ! 744: long_integer_type_node = make_signed_type (BITS_PER_WORD); ! 745: pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"), ! 746: long_integer_type_node, 1, 0)); ! 747: ! 748: short_unsigned_type_node = make_unsigned_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2)); ! 749: pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"), ! 750: short_unsigned_type_node, 1, 0)); ! 751: ! 752: unsigned_type_node = make_unsigned_type (BITS_PER_WORD); ! 753: pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"), ! 754: unsigned_type_node, 1, 0)); ! 755: ! 756: long_unsigned_type_node = make_unsigned_type (BITS_PER_WORD); ! 757: pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"), ! 758: long_unsigned_type_node, 1, 0)); ! 759: ! 760: char_type_node = make_signed_type (BITS_PER_UNIT); ! 761: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_CHAR], ! 762: char_type_node, 1, 0)); ! 763: ! 764: unsigned_char_type_node = make_unsigned_type (BITS_PER_UNIT); ! 765: pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"), ! 766: unsigned_char_type_node, 1, 0)); ! 767: ! 768: float_type_node = make_node (REAL_TYPE); ! 769: TYPE_PRECISION (float_type_node) = BITS_PER_WORD; ! 770: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT], ! 771: float_type_node, 1, 0)); ! 772: layout_type (float_type_node); ! 773: ! 774: double_type_node = make_node (REAL_TYPE); ! 775: TYPE_PRECISION (double_type_node) = 2 * BITS_PER_WORD; ! 776: pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE], ! 777: double_type_node, 1, 0)); ! 778: layout_type (double_type_node); ! 779: ! 780: long_double_type_node = make_node (REAL_TYPE); ! 781: TYPE_PRECISION (long_double_type_node) = 2 * BITS_PER_WORD; ! 782: pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"), ! 783: long_double_type_node, 1, 0)); ! 784: layout_type (long_double_type_node); ! 785: ! 786: integer_zero_node = build_int_2 (0, 0); ! 787: TREE_TYPE (integer_zero_node) = integer_type_node; ! 788: integer_one_node = build_int_2 (1, 0); ! 789: TREE_TYPE (integer_one_node) = integer_type_node; ! 790: ! 791: void_type_node = make_node (VOID_TYPE); ! 792: pushdecl (build_decl (TYPE_DECL, ! 793: ridpointers[(int) RID_VOID], void_type_node, ! 794: 1, 0)); ! 795: layout_type (void_type_node); /* Uses integer_zero_node */ ! 796: ! 797: null_pointer_node = build_int_2 (0, 0); ! 798: TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node); ! 799: layout_type (TREE_TYPE (null_pointer_node)); ! 800: ! 801: string_type_node = build_pointer_type (char_type_node); ! 802: layout_type (string_type_node); ! 803: ! 804: /* make a type for arrays of 256 characters. ! 805: 256 is picked randomly because we have a type for integers from 0 to 255. ! 806: With luck nothing will ever really depend on the length of this ! 807: array type. */ ! 808: char_array_type_node ! 809: = build_array_type (char_type_node, unsigned_char_type_node); ! 810: layout_type (char_array_type_node); ! 811: ! 812: default_function_type ! 813: = build_function_type (integer_type_node, NULL_TREE); ! 814: layout_type (default_function_type); ! 815: ! 816: ptr_type_node = build_pointer_type (void_type_node); ! 817: endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE); ! 818: ! 819: double_ftype_double ! 820: = build_function_type (double_type_node, ! 821: tree_cons (NULL_TREE, double_type_node, endlink)); ! 822: ! 823: double_ftype_double_double ! 824: = build_function_type (double_type_node, ! 825: tree_cons (NULL_TREE, double_type_node, ! 826: tree_cons (NULL_TREE, ! 827: double_type_node, endlink))); ! 828: ! 829: int_ftype_int ! 830: = build_function_type (integer_type_node, ! 831: tree_cons (NULL_TREE, integer_type_node, endlink)); ! 832: ! 833: long_ftype_long ! 834: = build_function_type (long_integer_type_node, ! 835: tree_cons (NULL_TREE, ! 836: long_integer_type_node, endlink)); ! 837: ! 838: void_ftype_ptr_ptr_int ! 839: = build_function_type (void_type_node, ! 840: tree_cons (NULL_TREE, ptr_type_node, ! 841: tree_cons (NULL_TREE, ptr_type_node, ! 842: tree_cons (NULL_TREE, ! 843: integer_type_node, ! 844: endlink)))); ! 845: ! 846: int_ftype_ptr_ptr_int ! 847: = build_function_type (integer_type_node, ! 848: tree_cons (NULL_TREE, ptr_type_node, ! 849: tree_cons (NULL_TREE, ptr_type_node, ! 850: tree_cons (NULL_TREE, ! 851: integer_type_node, ! 852: endlink)))); ! 853: ! 854: void_ftype_ptr_int_int ! 855: = build_function_type (void_type_node, ! 856: tree_cons (NULL_TREE, ptr_type_node, ! 857: tree_cons (NULL_TREE, integer_type_node, ! 858: tree_cons (NULL_TREE, ! 859: integer_type_node, ! 860: endlink)))); ! 861: ! 862: builtin_function ("_builtin_alloca", ! 863: build_function_type (ptr_type_node, ! 864: tree_cons (NULL_TREE, ! 865: integer_type_node, ! 866: endlink)), ! 867: BUILT_IN_ALLOCA); ! 868: ! 869: builtin_function ("_builtin_abs", int_ftype_int, BUILT_IN_ABS); ! 870: builtin_function ("_builtin_fabs", double_ftype_double, BUILT_IN_FABS); ! 871: builtin_function ("_builtin_labs", long_ftype_long, BUILT_IN_LABS); ! 872: /* builtin_function ("_builtin_div", default_ftype, BUILT_IN_DIV); ! 873: builtin_function ("_builtin_ldiv", default_ftype, BUILT_IN_LDIV); */ ! 874: builtin_function ("_builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR); ! 875: builtin_function ("_builtin_fceil", double_ftype_double, BUILT_IN_FCEIL); ! 876: builtin_function ("_builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD); ! 877: builtin_function ("_builtin_frem", double_ftype_double_double, BUILT_IN_FREM); ! 878: builtin_function ("_builtin_memcpy", void_ftype_ptr_ptr_int, BUILT_IN_MEMCPY); ! 879: builtin_function ("_builtin_memcmp", int_ftype_ptr_ptr_int, BUILT_IN_MEMCMP); ! 880: builtin_function ("_builtin_memset", void_ftype_ptr_int_int, BUILT_IN_MEMSET); ! 881: builtin_function ("_builtin_fsqrt", double_ftype_double, BUILT_IN_FSQRT); ! 882: builtin_function ("_builtin_getexp", double_ftype_double, BUILT_IN_GETEXP); ! 883: builtin_function ("_builtin_getman", double_ftype_double, BUILT_IN_GETMAN); ! 884: } ! 885: ! 886: /* Make a definition for a builtin function named NAME and whose data type ! 887: is TYPE. TYPE should be a function type with argument types. ! 888: FUNCTION_CODE tells later passes how to compile calls to this function. ! 889: See tree.h for its possible values. */ ! 890: ! 891: static void ! 892: builtin_function (name, type, function_code) ! 893: char *name; ! 894: tree type; ! 895: enum built_in_function function_code; ! 896: { ! 897: tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type, 0, 1); ! 898: make_function_rtl (decl); ! 899: pushdecl (decl); ! 900: DECL_SET_FUNCTION_CODE (decl, function_code); ! 901: } ! 902: ! 903: /* Validate a structure, union or enum type: make sure it ! 904: is not just a forward reference. ! 905: If it is valid, return it. Otherwise, return error_mark_node. ! 906: Callers may want to use the returned value instead of the original type. */ ! 907: ! 908: tree ! 909: resolve_tags (type) ! 910: tree type; ! 911: { ! 912: register char *errmsg = NULL; ! 913: ! 914: switch (TREE_CODE (type)) ! 915: { ! 916: case RECORD_TYPE: ! 917: errmsg = "undefined struct tag %s"; ! 918: break; ! 919: ! 920: case UNION_TYPE: ! 921: errmsg = "undefined union tag %s"; ! 922: break; ! 923: ! 924: case ENUMERAL_TYPE: ! 925: errmsg = "undefined enum tag %s"; ! 926: break; ! 927: ! 928: default: ! 929: return type; ! 930: } ! 931: ! 932: if (TYPE_SIZE (type) != 0) ! 933: return type; ! 934: ! 935: yyerror (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type))); ! 936: return error_mark_node; ! 937: } ! 938: ! 939: /* Called when a declaration is seen that contains no names to declare. ! 940: If its type is a reference to a structure, union or enum inherited ! 941: from a containing scope, shadow that tag name for the current scope ! 942: with a forward reference. ! 943: If its type defines a new named structure or union ! 944: or defines an enum, it is valid but we need not do anything here. ! 945: Otherwise, it is an error. */ ! 946: ! 947: void ! 948: shadow_tag (declspecs) ! 949: tree declspecs; ! 950: { ! 951: register tree link; ! 952: ! 953: for (link = declspecs; link; link = TREE_CHAIN (link)) ! 954: { ! 955: register tree value = TREE_VALUE (link); ! 956: register enum tree_code code = TREE_CODE (value); ! 957: if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) ! 958: && TYPE_SIZE (value) != 0) ! 959: { ! 960: register tree name = TYPE_NAME (value); ! 961: register tree t = lookup_tag (code, name, current_binding_level, 1); ! 962: if (t == 0) ! 963: { ! 964: t = make_node (code); ! 965: pushtag (name, t); ! 966: return; ! 967: } ! 968: if (name != 0 || code == ENUMERAL_TYPE) ! 969: return; ! 970: } ! 971: } ! 972: warning ("empty declaration"); ! 973: } ! 974: ! 975: /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ ! 976: ! 977: tree ! 978: groktypename (typename) ! 979: tree typename; ! 980: { ! 981: return grokdeclarator (TREE_VALUE (typename), ! 982: TREE_PURPOSE (typename), ! 983: TYPENAME); ! 984: } ! 985: ! 986: /* Decode a declarator in an ordinary declaration or data definition. ! 987: This is called as soon as the type information and variable name ! 988: have been parsed, before parsing the initializer if any. ! 989: Here we create the ..._DECL node, fill in its type, ! 990: and put it on the list of decls for the current context. ! 991: The ..._DECL node is returned as the value. ! 992: ! 993: Exception: for arrays where the length is not specified, ! 994: the type is left null, to be filled in by `finish_decl'. ! 995: ! 996: Function definitions do not come here; they go to start_function ! 997: instead. However, external and forward declarations of functions ! 998: do go through here. Structure field declarations are done by ! 999: grokfield and not through here. */ ! 1000: ! 1001: tree ! 1002: start_decl (declarator, declspecs, initialized) ! 1003: tree declspecs, declarator; ! 1004: int initialized; ! 1005: { ! 1006: register tree decl = grokdeclarator (declarator, declspecs, NORMAL); ! 1007: if (initialized) ! 1008: TREE_EXTERNAL (decl) = 0; ! 1009: return pushdecl (decl); ! 1010: } ! 1011: ! 1012: /* Finish processing of a declaration; ! 1013: install its line number and initial value. ! 1014: If the length of an array type is not known before, ! 1015: it must be determined now, from the initial value, or it is an error. */ ! 1016: ! 1017: void ! 1018: finish_decl (filename, line, decl, init) ! 1019: char *filename; ! 1020: int line; ! 1021: tree decl, init; ! 1022: { ! 1023: register tree type = TREE_TYPE (decl); ! 1024: ! 1025: if (init) store_init_value (decl, init); ! 1026: ! 1027: /* deduce size of array from initialization, if not already known */ ! 1028: ! 1029: if (TREE_CODE (type) == ARRAY_TYPE ! 1030: && TYPE_DOMAIN (type) == 0) ! 1031: { ! 1032: register tree maxindex = NULL_TREE; ! 1033: ! 1034: if (init) ! 1035: { ! 1036: /* Note MAXINDEX is really the maximum index, ! 1037: one less than the size. */ ! 1038: if (TREE_CODE (init) == ADDR_EXPR ! 1039: && TREE_CODE (TREE_OPERAND (init, 0)) == STRING_CST) ! 1040: maxindex = build_int_2 (TREE_STRING_LENGTH (TREE_OPERAND (init, 0)) - 1, 0); ! 1041: else if (TREE_CODE (init) == CONSTRUCTOR) ! 1042: { ! 1043: int nelts = list_length (TREE_OPERAND (DECL_INITIAL (decl), 0)); ! 1044: maxindex = build_int_2 (nelts - 1, 0); ! 1045: } ! 1046: } ! 1047: ! 1048: if (!maxindex) ! 1049: { ! 1050: if (!pedantic) ! 1051: TREE_EXTERNAL (decl) = 1; ! 1052: else if (!TREE_EXTERNAL (decl)) ! 1053: { ! 1054: yylineerror (line, "size missing in array declaration"); ! 1055: maxindex = build_int_2 (0, 0); ! 1056: } ! 1057: } ! 1058: ! 1059: if (maxindex) ! 1060: { ! 1061: if (pedantic && integer_zerop (maxindex)) ! 1062: yylineerror (line, "zero-size array"); ! 1063: TYPE_DOMAIN (type) = make_index_type (maxindex); ! 1064: if (!TREE_TYPE (maxindex)) ! 1065: TREE_TYPE (maxindex) = TYPE_DOMAIN (type); ! 1066: } ! 1067: ! 1068: layout_type (type); ! 1069: ! 1070: layout_decl (decl, 0); ! 1071: } ! 1072: ! 1073: /* Output the assembler code for the variable. */ ! 1074: ! 1075: rest_of_compilation (decl, current_binding_level == global_binding_level); ! 1076: } ! 1077: ! 1078: /* Given declspecs and a declarator, ! 1079: determine the name and type of the object declared. ! 1080: DECLSPECS is a chain of tree_list nodes whose value fields ! 1081: are the storage classes and type specifiers. ! 1082: ! 1083: DECL_CONTEXT says which syntactic context this declaration is in: ! 1084: NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. ! 1085: PARM for a paramater declaration (either within a function prototype ! 1086: or before a function body). Make a PARM_DECL. ! 1087: TYPENAME if for a typename (in a cast or sizeof). ! 1088: Don't make a DECL node; just return the type. ! 1089: FIELD for a struct or union field; make a FIELD_DECL. ! 1090: ! 1091: In the TYPENAME case, DECLARATOR is really an absolute declarator. ! 1092: It may also be so in the PARM case, for a prototype where the ! 1093: argument type is specified but not the name. */ ! 1094: ! 1095: static tree ! 1096: grokdeclarator (declarator, declspecs, decl_context) ! 1097: tree declspecs; ! 1098: tree declarator; ! 1099: enum decl_context decl_context; ! 1100: { ! 1101: int specbits = 0; ! 1102: tree spec; ! 1103: tree type = NULL_TREE; ! 1104: int longlong = 0; ! 1105: int constp; ! 1106: int volatilep; ! 1107: int explicit_int = 0; ! 1108: ! 1109: /* Anything declared one level down from the top level ! 1110: must be one of the parameters of a function ! 1111: (because the body is at least two levels down). */ ! 1112: ! 1113: if (decl_context == NORMAL ! 1114: && current_binding_level->level_chain == global_binding_level) ! 1115: decl_context = PARM; ! 1116: ! 1117: /* Look through the decl specs and record which ones appear. ! 1118: Some typespecs are defined as built-in typenames. ! 1119: Others, the ones that are modifiers of other types, ! 1120: are represented by bits in SPECBITS: set the bits for ! 1121: the modifiers that appear. Storage class keywords are also in SPECBITS. ! 1122: ! 1123: If there is a typedef name or a type, store the type in TYPE. ! 1124: This includes builtin typedefs such as `int'. ! 1125: ! 1126: Set EXPLICIT_INT if the type is `int' or `char' and did not ! 1127: come from a user typedef. ! 1128: ! 1129: Set LONGLONG if `long' is mentioned twice. */ ! 1130: ! 1131: for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) ! 1132: { ! 1133: register int i; ! 1134: register tree id = TREE_VALUE (spec); ! 1135: ! 1136: if (id == ridpointers[(int) RID_INT] ! 1137: || id == ridpointers[(int) RID_CHAR]) ! 1138: explicit_int = 1; ! 1139: ! 1140: for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++) ! 1141: { ! 1142: if (ridpointers[i] == id) ! 1143: { ! 1144: if (i == (int) RID_LONG && specbits & (1<<i)) ! 1145: longlong = 1; ! 1146: specbits |= 1 << i; ! 1147: goto found; ! 1148: } ! 1149: } ! 1150: if (type) yyerror ("two or more data types in declaration"); ! 1151: else if (TREE_CODE (id) == RECORD_TYPE || TREE_CODE (id) == UNION_TYPE || TREE_CODE (id) == ENUMERAL_TYPE) ! 1152: type = id; ! 1153: else ! 1154: { ! 1155: register tree t = lookup_name (id); ! 1156: if (!t || TREE_CODE (t) != TYPE_DECL) ! 1157: yyerror ("%s fails to be a typedef or built in type", ! 1158: IDENTIFIER_POINTER (id)); ! 1159: else type = TREE_TYPE (t); ! 1160: } ! 1161: ! 1162: found: {} ! 1163: } ! 1164: ! 1165: /* No type at all: default to `int', and set EXPLICIT_INT ! 1166: because it was not a user-defined typedef. */ ! 1167: ! 1168: if (type == 0) ! 1169: { ! 1170: explicit_int = 1; ! 1171: type = integer_type_node; ! 1172: } ! 1173: ! 1174: /* Now process the modifiers that were specified ! 1175: and check for invalid combinations. */ ! 1176: ! 1177: /* Long double is a special combination. */ ! 1178: ! 1179: if ((specbits & 1 << RID_LONG) && type == double_type_node) ! 1180: { ! 1181: specbits &= ~ (1 << RID_LONG); ! 1182: type = long_double_type_node; ! 1183: } ! 1184: ! 1185: /* Check all other uses of type modifiers. */ ! 1186: ! 1187: if (specbits & ((1 << RID_LONG) | (1 << RID_SHORT) ! 1188: | (1 << RID_UNSIGNED) | (1 << RID_SIGNED))) ! 1189: { ! 1190: if (!explicit_int) ! 1191: yyerror ("long, short, signed or unsigned used invalidly"); ! 1192: else if ((specbits & 1 << RID_LONG) && (specbits & 1 << RID_SHORT)) ! 1193: yyerror ("long and short specified together"); ! 1194: else if ((specbits & 1 << RID_SIGNED) && (specbits & 1 << RID_UNSIGNED)) ! 1195: yyerror ("signed and unsigned specified together"); ! 1196: else ! 1197: { ! 1198: if (specbits & 1 << RID_UNSIGNED) ! 1199: { ! 1200: if (specbits & 1 << RID_LONG) ! 1201: type = long_unsigned_type_node; ! 1202: else if (specbits & 1 << RID_SHORT) ! 1203: type = short_unsigned_type_node; ! 1204: else if (type == char_type_node) ! 1205: type = unsigned_char_type_node; ! 1206: else ! 1207: type = unsigned_type_node; ! 1208: } ! 1209: else if (specbits & 1 << RID_LONG) ! 1210: type = long_integer_type_node; ! 1211: else if (specbits & 1 << RID_SHORT) ! 1212: type = short_integer_type_node; ! 1213: } ! 1214: } ! 1215: ! 1216: /* Set CONSTP if this declaration is `const', whether by ! 1217: explicit specification or via a typedef. ! 1218: Likewise for VOLATILEP. */ ! 1219: ! 1220: constp = (specbits & 1 << RID_CONST) | TREE_READONLY (type); ! 1221: volatilep = (specbits & 1 << RID_VOLATILE) | TREE_VOLATILE (type); ! 1222: type = TYPE_MAIN_VARIANT (type); ! 1223: ! 1224: /* Warn if two storage classes are given. Default to `auto'. */ ! 1225: ! 1226: { ! 1227: int nclasses = 0; ! 1228: ! 1229: if (specbits & 1 << RID_AUTO) nclasses++; ! 1230: if (specbits & 1 << RID_STATIC) nclasses++; ! 1231: if (specbits & 1 << RID_EXTERN) nclasses++; ! 1232: if (specbits & 1 << RID_REGISTER) nclasses++; ! 1233: if (specbits & 1 << RID_TYPEDEF) nclasses++; ! 1234: ! 1235: if (nclasses == 0) specbits |= 1 << RID_AUTO; ! 1236: ! 1237: /* Warn about storage classes that are invalid for certain ! 1238: kinds of declarations (parameters, typenames, etc.). */ ! 1239: ! 1240: if (nclasses > 1) ! 1241: yyerror ("two or more storage classes in declaration"); ! 1242: else if (decl_context != NORMAL && nclasses > 0) ! 1243: { ! 1244: if (decl_context == PARM && specbits & 1 << RID_REGISTER) ! 1245: ; ! 1246: else if (decl_context == FIELD) ! 1247: yyerror ("storage class specified in structure field"); ! 1248: else yyerror (decl_context == PARM ! 1249: ? "storage class specified in parameter list" ! 1250: : "storage class specified in typename"); ! 1251: } ! 1252: else if (current_binding_level == global_binding_level ! 1253: && nclasses > 0) ! 1254: { ! 1255: if (specbits & (1 << RID_AUTO)) ! 1256: yyerror ("auto specified in external declaration"); ! 1257: if (specbits & (1 << RID_REGISTER)) ! 1258: yyerror ("auto specified in external declaration"); ! 1259: } ! 1260: } ! 1261: ! 1262: /* Now figure out the structure of the declarator proper. ! 1263: Descend through it, creating more complex types, until we reach ! 1264: the declared identifier (or NULL_TREE, in an absolute declarator). */ ! 1265: ! 1266: while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE) ! 1267: { ! 1268: /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), ! 1269: an INDIRECT_REF (for *...), ! 1270: a CALL_EXPR (for ...(...)), ! 1271: an identifier (for the name being declared) ! 1272: or a null pointer (for the place in an absolute declarator ! 1273: where the name was omitted). ! 1274: For the last two cases, we have just exited the loop. ! 1275: ! 1276: At this point, TYPE is the type of elements of an array, ! 1277: or for a function to return, or for a pointer to point to. ! 1278: After this sequence of ifs, TYPE is the type of the ! 1279: array or function or pointer, and DECLARATOR has had its ! 1280: outermost layer removed. */ ! 1281: ! 1282: if (TREE_CODE (declarator) == ARRAY_REF) ! 1283: { ! 1284: register tree itype = NULL_TREE; ! 1285: register tree size = TREE_OPERAND (declarator, 1); ! 1286: ! 1287: /* Make sure we have a valid type for the array elements. */ ! 1288: type = resolve_tags (type); ! 1289: /* Make a variant that is const or volatile as needed. */ ! 1290: if (constp || volatilep) ! 1291: type = build_type_variant (type, constp, volatilep); ! 1292: /* Record that any const-ness or volatility are taken care of. */ ! 1293: constp = 0; ! 1294: volatilep = 0; ! 1295: ! 1296: /* Check for some types that there cannot be arrays of. */ ! 1297: ! 1298: if (type == void_type_node) ! 1299: { ! 1300: yyerror ("array of voids declared"); ! 1301: type = integer_type_node; ! 1302: } ! 1303: ! 1304: if (TREE_CODE (type) == FUNCTION_TYPE) ! 1305: { ! 1306: yyerror ("array of functions declared"); ! 1307: type = integer_type_node; ! 1308: } ! 1309: ! 1310: /* If size was specified, set ITYPE to a range-type for that size. ! 1311: Otherwise, ITYPE remains null. finish_decl may figure it out ! 1312: from an initial value. */ ! 1313: ! 1314: if (size) ! 1315: { ! 1316: if (TREE_LITERAL (size)) ! 1317: itype = make_index_type (build_int_2 (TREE_INT_CST_LOW (size) - 1, 0)); ! 1318: else ! 1319: itype = make_index_type (build_binary_op (MINUS_EXPR, size, integer_one_node)); ! 1320: } ! 1321: ! 1322: /* Build the array type itself. */ ! 1323: ! 1324: type = build_array_type (type, itype); ! 1325: ! 1326: /* Make sure there is a valid pointer type ! 1327: for automatic coercion of array to pointer. */ ! 1328: ! 1329: layout_type (TYPE_POINTER_TO (TREE_TYPE (type))); ! 1330: ! 1331: declarator = TREE_OPERAND (declarator, 0); ! 1332: ! 1333: /* if size unknown, don't lay it out until finish_decl */ ! 1334: if (!itype) goto nolayout; ! 1335: } ! 1336: else if (TREE_CODE (declarator) == CALL_EXPR) ! 1337: { ! 1338: /* Declaring a function type. ! 1339: Make sure we have a valid type for the function to return. */ ! 1340: type = resolve_tags (type); ! 1341: ! 1342: /* Is this an error? Should they be merged into TYPE here? */ ! 1343: constp = 0; ! 1344: volatilep = 0; ! 1345: ! 1346: /* Warn about some types functions can't return. */ ! 1347: ! 1348: if (TREE_CODE (type) == FUNCTION_TYPE) ! 1349: { ! 1350: yyerror ("function returning a function declared"); ! 1351: type = integer_type_node; ! 1352: } ! 1353: if (TREE_CODE (type) == ARRAY_TYPE) ! 1354: { ! 1355: yyerror ("function returning an array declared"); ! 1356: type = integer_type_node; ! 1357: } ! 1358: ! 1359: /* Construct the function type and go to the next ! 1360: inner layer of declarator. */ ! 1361: ! 1362: type = ! 1363: build_function_type (type, ! 1364: grokparms (TREE_OPERAND (declarator, 1))); ! 1365: declarator = TREE_OPERAND (declarator, 0); ! 1366: } ! 1367: else if (TREE_CODE (declarator) == INDIRECT_REF) ! 1368: { ! 1369: /* Merge any constancy or volatility into the target type ! 1370: for the pointer. */ ! 1371: ! 1372: if (constp || volatilep) ! 1373: type = build_type_variant (type, constp, volatilep); ! 1374: constp = 0; ! 1375: volatilep = 0; ! 1376: ! 1377: type = build_pointer_type (type); ! 1378: ! 1379: /* Process a list of type modifier keywords ! 1380: (such as const or volatile) that were given inside the `*'. */ ! 1381: ! 1382: if (TREE_TYPE (declarator)) ! 1383: { ! 1384: register tree typemodlist; ! 1385: for (typemodlist = TREE_TYPE (declarator); typemodlist; ! 1386: typemodlist = TREE_CHAIN (typemodlist)) ! 1387: { ! 1388: if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST]) ! 1389: constp = 1; ! 1390: if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE]) ! 1391: volatilep = 1; ! 1392: } ! 1393: } ! 1394: ! 1395: declarator = TREE_OPERAND (declarator, 0); ! 1396: } ! 1397: else ! 1398: abort (); ! 1399: ! 1400: layout_type (type); ! 1401: ! 1402: /* @@ Should perhaps replace the following code by changes in ! 1403: * @@ stor_layout.c. */ ! 1404: if (TREE_CODE (type) == FUNCTION_DECL) ! 1405: { ! 1406: /* A function variable in C should be Pmode rather than EPmode ! 1407: because it has just the address of a function, no static chain.*/ ! 1408: TYPE_MODE (type) = Pmode; ! 1409: } ! 1410: ! 1411: nolayout: ; ! 1412: } ! 1413: ! 1414: /* Now TYPE has the actual type; but if it is an ARRAY_TYPE ! 1415: then it has not been laid out of its size wasn't specified. */ ! 1416: ! 1417: /* If this is declaring a typedef name, return a TYPE_DECL. */ ! 1418: ! 1419: if (specbits & 1 << RID_TYPEDEF) ! 1420: { ! 1421: /* Note that the grammar rejects storage classes ! 1422: in typenames, fields or parameters */ ! 1423: if (constp || volatilep) ! 1424: type = build_type_variant (type, constp, volatilep); ! 1425: return build_decl (TYPE_DECL, declarator, type, 0, 0); ! 1426: } ! 1427: ! 1428: /* It is ok to typedef a forward reference to a structure tag, ! 1429: but using it for other purposes requires it to be defined ! 1430: (except when it's inside a pointer; but in that case, what we ! 1431: get here would be a pointer type and `resolve_tags' would do nothing. */ ! 1432: ! 1433: type = resolve_tags (type); ! 1434: ! 1435: /* If this is a type name (such as, in a cast or sizeof), ! 1436: compute the type and return it now. */ ! 1437: ! 1438: if (decl_context == TYPENAME) ! 1439: { ! 1440: /* Note that the grammar rejects storage classes ! 1441: in typenames, fields or parameters */ ! 1442: if (constp || volatilep) ! 1443: type = build_type_variant (type, constp, volatilep); ! 1444: return type; ! 1445: } ! 1446: ! 1447: /* `void' at top level (not within pointer) ! 1448: is allowed only in typedefs or type names. */ ! 1449: ! 1450: if (type == void_type_node) ! 1451: { ! 1452: yyerror ("variable or field %s declared void", ! 1453: IDENTIFIER_POINTER (declarator)); ! 1454: type = integer_type_node; ! 1455: } ! 1456: ! 1457: /* Now create the decl, which may be a VAR_DECL, a PARM_DECL ! 1458: or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ ! 1459: ! 1460: { ! 1461: register tree decl; ! 1462: ! 1463: if (decl_context == PARM) ! 1464: { ! 1465: /* A parameter declared as an array of T is really a pointer to T. ! 1466: One declared as a function is really a pointer to a function. */ ! 1467: ! 1468: if (TREE_CODE (type) == ARRAY_TYPE) ! 1469: type = build_pointer_type (TREE_TYPE (type)); ! 1470: if (TREE_CODE (type) == FUNCTION_TYPE) ! 1471: { ! 1472: type = build_pointer_type (type); ! 1473: layout_type (type); ! 1474: } ! 1475: ! 1476: decl = build_decl (PARM_DECL, declarator, type, 0, 0); ! 1477: DECL_ARG_TYPE (decl) = type; ! 1478: if (type == float_type_node) ! 1479: DECL_ARG_TYPE (decl) = double_type_node; ! 1480: else if (TREE_CODE (type) == INTEGER_TYPE ! 1481: && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) ! 1482: DECL_ARG_TYPE (decl) = integer_type_node; ! 1483: } ! 1484: else if (decl_context == FIELD) ! 1485: { ! 1486: /* Structure field. It may not be a function. */ ! 1487: ! 1488: if (TREE_CODE (type) == FUNCTION_TYPE) ! 1489: { ! 1490: yyerror ("field %s declared as a function", ! 1491: IDENTIFIER_POINTER (declarator)); ! 1492: type = build_pointer_type (type); ! 1493: layout_type (type); ! 1494: } ! 1495: decl = build_decl (FIELD_DECL, declarator, type, 0, 0); ! 1496: } ! 1497: else ! 1498: { ! 1499: /* Declaration in ordinary context ! 1500: is either a variable or a function depending on TYPE. */ ! 1501: ! 1502: decl = build_decl (((TREE_CODE (type) == FUNCTION_TYPE) ! 1503: ? FUNCTION_DECL : VAR_DECL), ! 1504: declarator, type, ! 1505: specbits & 1 << RID_STATIC, ! 1506: specbits & 1 << RID_EXTERN); ! 1507: ! 1508: ! 1509: /* For function declaration, ! 1510: store list of parameter names in the parameters field. ! 1511: finish_function will replace it with ! 1512: chained declarations of them. */ ! 1513: ! 1514: if (TREE_CODE (type) == FUNCTION_TYPE) ! 1515: DECL_ARGUMENTS (decl) = last_function_parm_names; ! 1516: } ! 1517: ! 1518: /* Record `register' declaration for warnings on & ! 1519: and in case doing stupid register allocation. */ ! 1520: ! 1521: if (specbits & 1 << RID_REGISTER) ! 1522: TREE_REGDECL (decl) = 1; ! 1523: ! 1524: /* Record constancy and volatility. */ ! 1525: ! 1526: if (constp) ! 1527: TREE_READONLY (decl) = 1; ! 1528: if (volatilep) ! 1529: { ! 1530: TREE_VOLATILE (decl) = 1; ! 1531: TREE_THIS_VOLATILE (decl) = 1; ! 1532: } ! 1533: ! 1534: return decl; ! 1535: } ! 1536: } ! 1537: ! 1538: /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE. ! 1539: MAXVAL should be the maximum value in the domain ! 1540: (one less than the length of the array). */ ! 1541: ! 1542: static ! 1543: tree ! 1544: make_index_type (maxval) ! 1545: tree maxval; ! 1546: { ! 1547: register tree itype = make_node (INTEGER_TYPE); ! 1548: TYPE_PRECISION (itype) = BITS_PER_WORD; ! 1549: TYPE_MIN_VALUE (itype) = build_int_2 (0, 0); ! 1550: TREE_TYPE (TYPE_MIN_VALUE (itype)) = itype; ! 1551: TYPE_MAX_VALUE (itype) = maxval; ! 1552: TREE_TYPE (maxval) = itype; ! 1553: TYPE_MODE (itype) = SImode; ! 1554: TYPE_SIZE (itype) = TYPE_SIZE (integer_type_node); ! 1555: TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (integer_type_node); ! 1556: TYPE_ALIGN (itype) = TYPE_ALIGN (integer_type_node); ! 1557: return itype; ! 1558: } ! 1559: ! 1560: /* Decode the list of parameter types for a function type. ! 1561: Given the list of things declared inside the parens, ! 1562: return a list of types. ! 1563: ! 1564: The list we receive can have three kinds of elements: ! 1565: an IDENTIFIER_NODE for names given without types, ! 1566: a TREE_LIST node for arguments given as typespecs or names with typespecs, ! 1567: or void_type_node, to mark the end of an argument list ! 1568: when additional arguments are not permitted (... was not used). ! 1569: ! 1570: If all elements of the input list contain types, ! 1571: we return a list of the types. ! 1572: If all elements contain no type (except perhaps a void_type_node ! 1573: at the end), we return a null list. ! 1574: If some have types and some do not, it is an error, and we ! 1575: return a null list. ! 1576: ! 1577: Also stores a list of names (IDENTIFIER_NODEs) ! 1578: in last_function_parm_names. The list links have the names ! 1579: as the TREE_VALUE and their types (if specified) as the TREE_PURPOSE. */ ! 1580: ! 1581: static ! 1582: tree ! 1583: grokparms (first_parm) ! 1584: tree first_parm; ! 1585: { ! 1586: register tree parm; ! 1587: tree result = NULL_TREE; ! 1588: tree names = NULL_TREE; ! 1589: int notfirst = 0; ! 1590: int erring = 0; ! 1591: ! 1592: for (parm = first_parm; ! 1593: parm != NULL_TREE; ! 1594: parm = TREE_CHAIN (parm)) ! 1595: { ! 1596: register tree parm_node = TREE_VALUE (parm); ! 1597: register tree name, type; ! 1598: if (TREE_CODE (parm_node) == VOID_TYPE) ! 1599: { ! 1600: name = 0; ! 1601: if (result != 0) ! 1602: result = chainon (result, build_tree_list (0, parm_node)); ! 1603: break; ! 1604: } ! 1605: else if (TREE_CODE (parm_node) == IDENTIFIER_NODE) ! 1606: { ! 1607: name = parm_node; ! 1608: type = 0; ! 1609: } ! 1610: else ! 1611: { ! 1612: name = TREE_VALUE (parm_node); ! 1613: type = TREE_TYPE (grokdeclarator (TREE_VALUE (parm_node), ! 1614: TREE_PURPOSE (parm_node), ! 1615: PARM)); ! 1616: } ! 1617: ! 1618: if (notfirst && !erring && (type != 0) != (result != 0)) ! 1619: { ! 1620: yyerror ("types sometimes given and sometimes omitted in parameter list"); ! 1621: erring = 1; ! 1622: } ! 1623: notfirst = 1; ! 1624: ! 1625: names = chainon (names, build_tree_list (type, name)); ! 1626: if (type != 0) ! 1627: result = chainon (result, build_tree_list (0, type)); ! 1628: } ! 1629: ! 1630: last_function_parm_names = names; ! 1631: if (erring) ! 1632: return 0; ! 1633: return result; ! 1634: } ! 1635: ! 1636: /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted) ! 1637: of a structure component, returning a FIELD_DECL node. ! 1638: WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. ! 1639: ! 1640: This is done during the parsing of the struct declaration. ! 1641: The FIELD_DECL nodes are chained together and the lot of them ! 1642: are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ ! 1643: ! 1644: tree ! 1645: grokfield (filename, line, declarator, declspecs, width) ! 1646: char *filename; ! 1647: int line; ! 1648: tree declarator, declspecs, width; ! 1649: { ! 1650: register tree value = grokdeclarator (declarator, declspecs, FIELD); ! 1651: register tree semantics = TREE_TYPE (value); ! 1652: ! 1653: finish_decl (filename, line, value, NULL); ! 1654: DECL_INITIAL (value) = width; ! 1655: DECL_ALIGN (value) = TYPE_ALIGN (semantics); ! 1656: return value; ! 1657: } ! 1658: ! 1659: /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration. ! 1660: CODE says which one; it is RECORD_TYPE or UNION_TYPE. ! 1661: FILENAME and LINE say where the declaration is located in the source. ! 1662: NAME is the name of the struct or union tag, or 0 if there is none. ! 1663: FIELDLIST is a chain of FIELD_DECL nodes for the fields. ! 1664: XREF is nonzero to make a cross reference to a struct or union ! 1665: defined elsewhere; this is how `struct foo' with no members ! 1666: is handled. */ ! 1667: ! 1668: tree ! 1669: build_struct (code, filename, line, name, fieldlist, xref) ! 1670: enum tree_code code; ! 1671: char *filename; ! 1672: int line; ! 1673: tree name, fieldlist; ! 1674: int xref; ! 1675: { ! 1676: register tree t; ! 1677: register tree x; ! 1678: ! 1679: if (xref) ! 1680: { ! 1681: /* If a cross reference is requested, look up the type ! 1682: already defined for this tag and return it. */ ! 1683: ! 1684: register tree ref = lookup_tag (code, name, current_binding_level, 0); ! 1685: if (ref) return ref; ! 1686: ! 1687: /* If no such tag is yet defined, create a forward-reference node ! 1688: and record it as the "definition". ! 1689: When a real declaration of this type is found, ! 1690: the forward-reference will be altered into a real type. */ ! 1691: ! 1692: t = make_node (code); ! 1693: pushtag (name, t); ! 1694: return t; ! 1695: } ! 1696: ! 1697: /* If we have previously made forward reference to this type, ! 1698: fill in the contents in the same object that used to be the ! 1699: forward reference. */ ! 1700: ! 1701: if (name) ! 1702: t = lookup_tag (code, name, current_binding_level, 1); ! 1703: else ! 1704: t = 0; ! 1705: ! 1706: /* Otherwise, create a new node for this type. */ ! 1707: ! 1708: if (t == 0) ! 1709: { ! 1710: t = make_node (code); ! 1711: pushtag (name, t); ! 1712: } ! 1713: ! 1714: /* Install struct as DECL_CONTEXT of each field decl. ! 1715: Also process specified field sizes. ! 1716: Set DECL_SIZE_UNIT to the specified size, or 0 if none specified. ! 1717: The specified size is found in the DECL_INITIAL. ! 1718: Store 0 there, except for ": 0" fields (so we can find them ! 1719: and delete them, below). */ ! 1720: ! 1721: for (x = fieldlist; x; x = TREE_CHAIN (x)) ! 1722: { ! 1723: DECL_CONTEXT (x) = t; ! 1724: DECL_SIZE_UNIT (x) = 0; ! 1725: /* detect invalid field size */ ! 1726: if (DECL_INITIAL (x) && TREE_CODE (DECL_INITIAL (x)) != INTEGER_CST) ! 1727: { ! 1728: yylineerror (DECL_SOURCE_LINE (x), ! 1729: "structure field %s width not an integer constant", ! 1730: IDENTIFIER_POINTER (DECL_NAME (x))); ! 1731: DECL_INITIAL (x) = NULL; ! 1732: } ! 1733: /* process valid field size */ ! 1734: if (DECL_INITIAL (x)) ! 1735: { ! 1736: register int prec = TREE_INT_CST_LOW (DECL_INITIAL (x)); ! 1737: ! 1738: if (prec == 0) ! 1739: { ! 1740: /* field size 0 => mark following field as "aligned" */ ! 1741: if (TREE_CHAIN (x)) ! 1742: DECL_ALIGN (TREE_CHAIN (x)) = BITS_PER_WORD; ! 1743: } ! 1744: else ! 1745: { ! 1746: DECL_INITIAL (x) = NULL; ! 1747: DECL_SIZE_UNIT (x) = prec > 0 ? prec : - prec; ! 1748: TREE_PACKED (x) = 1; ! 1749: } ! 1750: } ! 1751: } ! 1752: /* delete all ": 0" fields from the front of the fieldlist */ ! 1753: while (fieldlist ! 1754: && DECL_INITIAL (fieldlist)) ! 1755: fieldlist = TREE_CHAIN (fieldlist); ! 1756: /* delete all such fields from the rest of the fieldlist */ ! 1757: for (x = fieldlist; x;) ! 1758: { ! 1759: if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x))) ! 1760: TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x)); ! 1761: else x = TREE_CHAIN (x); ! 1762: } ! 1763: ! 1764: TYPE_FIELDS (t) = fieldlist; ! 1765: ! 1766: layout_type (t); ! 1767: ! 1768: /* Round the size up to be a multiple of the required alignment */ ! 1769: TYPE_SIZE (t) ! 1770: = convert_units (TYPE_SIZE (t), TYPE_SIZE_UNIT (t), TYPE_ALIGN (t)); ! 1771: TYPE_SIZE_UNIT (t) = TYPE_ALIGN (t); ! 1772: ! 1773: return t; ! 1774: } ! 1775: ! 1776: /* Begin compiling the definition of an enumeration type. ! 1777: NAME is its name (or null if anonymous). ! 1778: Returns the type object, as yet incomplete. ! 1779: Also records info about it so that build_enumerator ! 1780: may be used to declare the individual values as they are read. */ ! 1781: ! 1782: tree ! 1783: start_enum (name) ! 1784: tree name; ! 1785: { ! 1786: register tree enumtype = 0; ! 1787: ! 1788: /* If this is the real definition for a previous forward reference, ! 1789: fill in the contents in the same object that used to be the ! 1790: forward reference. */ ! 1791: ! 1792: if (name != 0) ! 1793: enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1); ! 1794: ! 1795: if (enumtype == 0) ! 1796: { ! 1797: enumtype = make_node (ENUMERAL_TYPE); ! 1798: pushtag (name, enumtype); ! 1799: } ! 1800: ! 1801: if (TYPE_VALUES (enumtype) != 0) ! 1802: { ! 1803: /* This enum is a named one that has been declared already. */ ! 1804: yyerror ("redeclaration of enum %s", IDENTIFIER_POINTER (name)); ! 1805: ! 1806: /* Completely replace its old definition. ! 1807: The old enumerators remain defined, however. */ ! 1808: TYPE_VALUES (enumtype) = 0; ! 1809: } ! 1810: ! 1811: /* Initially, set up this enum as like `int' ! 1812: so that we can create the enumerators' declarations and values. ! 1813: Later on, the precision of the type may be changed and ! 1814: it may be layed out again. */ ! 1815: ! 1816: TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); ! 1817: TYPE_SIZE (enumtype) = 0; ! 1818: fixup_unsigned_type (enumtype); ! 1819: ! 1820: enum_next_value = 0; ! 1821: current_enum_type = enumtype; ! 1822: ! 1823: return enumtype; ! 1824: } ! 1825: ! 1826: /* Return the enumeration type tagged with NAME ! 1827: or create and return a forward reference to such a type. */ ! 1828: ! 1829: tree ! 1830: xref_enum (name) ! 1831: tree name; ! 1832: { ! 1833: register tree ref = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 0); ! 1834: /* If we find an already-defined enum type, or a previous ! 1835: forward reference, return it. */ ! 1836: if (ref) return ref; ! 1837: ! 1838: /* If this is a forward reference for the first time, ! 1839: record it as the "definition". */ ! 1840: ref = make_node (ENUMERAL_TYPE); ! 1841: pushtag (name, ref); ! 1842: return ref; ! 1843: } ! 1844: ! 1845: /* After processing and defining all the values of an enumeration type, ! 1846: install their decls in the enumeration type and finish it off. ! 1847: ENUMTYPE is the type object and VALUES a list of name-value pairs. ! 1848: Returns ENUMTYPE. */ ! 1849: ! 1850: tree ! 1851: finish_enum (enumtype, values) ! 1852: register tree enumtype, values; ! 1853: { ! 1854: register tree pair = values; ! 1855: register long maxvalue = 0; ! 1856: register int i; ! 1857: ! 1858: TYPE_VALUES (enumtype) = values; ! 1859: ! 1860: /* Calculate the maximum value of any enumerator in this type. */ ! 1861: ! 1862: for (pair = values; pair; pair = TREE_CHAIN (pair)) ! 1863: { ! 1864: int value = TREE_INT_CST_LOW (TREE_VALUE (pair)); ! 1865: if (value > maxvalue) ! 1866: maxvalue = value; ! 1867: } ! 1868: ! 1869: #if 0 ! 1870: /* Determine the precision this type needs, lay it out, and define it. */ ! 1871: ! 1872: for (i = maxvalue; i; i >>= 1) ! 1873: TYPE_PRECISION (enumtype)++; ! 1874: ! 1875: if (!TYPE_PRECISION (enumtype)) ! 1876: TYPE_PRECISION (enumtype) = 1; ! 1877: #endif ! 1878: ! 1879: /* Cancel the laying out previously done for the enum type, ! 1880: so that fixup_unsigned_type will do it over. */ ! 1881: TYPE_SIZE (enumtype) = 0; ! 1882: ! 1883: fixup_unsigned_type (enumtype); ! 1884: TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue; ! 1885: ! 1886: current_enum_type = 0; ! 1887: ! 1888: return enumtype; ! 1889: } ! 1890: ! 1891: /* Build and install a CONST_DECL for one value of the ! 1892: current enumeration type (one that was begun with start_enum). ! 1893: Return a tree-list containing the name and its value. ! 1894: Assignment of sequential values by default is handled here. */ ! 1895: ! 1896: tree ! 1897: build_enumerator (name, value) ! 1898: tree name, value; ! 1899: { ! 1900: register tree decl; ! 1901: ! 1902: /* Validate and default VALUE. */ ! 1903: ! 1904: if (value != 0 && TREE_CODE (value) != INTEGER_CST) ! 1905: { ! 1906: yyerror ("enumerator value for %s not integer constant", ! 1907: IDENTIFIER_POINTER (name)); ! 1908: value = 0; ! 1909: } ! 1910: ! 1911: if (value == 0) ! 1912: value = build_int_2 (enum_next_value, 0); ! 1913: ! 1914: /* Set default for following value. */ ! 1915: ! 1916: enum_next_value = TREE_INT_CST_LOW (value) + 1; ! 1917: ! 1918: /* Now create a declaration for the enum value name. */ ! 1919: ! 1920: decl = build_decl (CONST_DECL, name, ! 1921: current_enum_type, 0, 0); ! 1922: DECL_INITIAL (decl) = value; ! 1923: TREE_TYPE (value) = current_enum_type; ! 1924: pushdecl (decl); ! 1925: ! 1926: return build_tree_list (name, value); ! 1927: } ! 1928: ! 1929: /* Create the FUNCTION_DECL for a function definition. ! 1930: LINE1 is the line number that the definition absolutely begins on. ! 1931: LINE2 is the line number that the name of the function appears on. ! 1932: DECLSPECS and DECLARATOR are the parts of the declaration; ! 1933: they describe the function's name and the type it returns, ! 1934: but twisted together in a fashion that parallels the syntax of C. ! 1935: ! 1936: This function creates a binding context for the function body ! 1937: as well as setting up the FUNCTION_DECL in current_function_decl. ! 1938: ! 1939: Returns 1 on success. If the DECLARATOR is not suitable for a function ! 1940: (it defines a datum instead), we return 0, which tells ! 1941: yyparse to report a parse error. */ ! 1942: ! 1943: int ! 1944: start_function (declspecs, declarator) ! 1945: tree declarator, declspecs; ! 1946: { ! 1947: tree pushed_decl, decl1; ! 1948: ! 1949: current_function_returns_value = 0; /* assume it doesn't until we see it does. */ ! 1950: ! 1951: decl1 = grokdeclarator (declarator, declspecs, NORMAL); ! 1952: if (TREE_CODE (decl1) != FUNCTION_DECL) ! 1953: return 0; ! 1954: ! 1955: current_function_decl = decl1; ! 1956: ! 1957: announce_function (current_function_decl); ! 1958: ! 1959: /* Make the init_value nonzero so pushdecl knows this is not tentative. ! 1960: 1 is not a legitimate value, ! 1961: but it is replaced below with the LET_STMT. */ ! 1962: DECL_INITIAL (current_function_decl) = (tree)1; ! 1963: pushed_decl = pushdecl (current_function_decl); ! 1964: ! 1965: /* If this is an erroneous redeclaration of something not a function, ! 1966: return the original declaration (that nobody else can see) ! 1967: to avoid bombing out reading in the body of the function. */ ! 1968: ! 1969: if (TREE_CODE (pushed_decl) == FUNCTION_DECL) ! 1970: current_function_decl = pushed_decl; ! 1971: ! 1972: pushlevel (); ! 1973: ! 1974: make_function_rtl (current_function_decl); ! 1975: ! 1976: /* Allocate further tree nodes temporarily during compilation ! 1977: of this function only. */ ! 1978: temporary_allocation (); ! 1979: ! 1980: current_block = build_let (NULL, NULL, NULL, NULL, NULL, NULL); ! 1981: DECL_INITIAL (current_function_decl) = current_block; ! 1982: DECL_RESULT (current_function_decl) ! 1983: = build_decl (RESULT_DECL, value_identifier, ! 1984: TREE_TYPE (TREE_TYPE (current_function_decl)), 0, 0); ! 1985: ! 1986: /* Make the FUNCTION_DECL's contents appear in a local tree dump ! 1987: and make the FUNCTION_DECL itself not appear in the permanent dump. */ ! 1988: ! 1989: TREE_PERMANENT (current_function_decl) = 0; ! 1990: ! 1991: /* Must mark the RESULT_DECL as being in this function. */ ! 1992: ! 1993: DECL_CONTEXT (DECL_RESULT (current_function_decl)) = current_block; ! 1994: ! 1995: return 1; ! 1996: } ! 1997: ! 1998: /* Store the parameter declarations into the current function declaration. ! 1999: This is called after parsing the parameter declarations, before ! 2000: digesting the body of the function. */ ! 2001: ! 2002: void ! 2003: store_parm_decls () ! 2004: { ! 2005: register tree parmdecls = getdecls (); ! 2006: register tree fndecl = current_function_decl; ! 2007: register tree block = DECL_INITIAL (fndecl); ! 2008: register tree parm; ! 2009: ! 2010: /* First match each formal parameter name with its declaration. ! 2011: The DECL_ARGUMENTS is a chain of TREE_LIST nodes ! 2012: each with a parm name as the TREE_VALUE. ! 2013: (A declared type may be in the TREE_PURPOSE.) ! 2014: ! 2015: Associate decls with the names and store the decls ! 2016: into the TREE_PURPOSE slots. */ ! 2017: ! 2018: for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm)) ! 2019: { ! 2020: register tree tail, found = NULL; ! 2021: ! 2022: if (TREE_VALUE (parm) == 0) ! 2023: { ! 2024: yylineerror (DECL_SOURCE_LINE (fndecl), ! 2025: "parameter name missing from parameter list"); ! 2026: TREE_PURPOSE (parm) = 0; ! 2027: continue; ! 2028: } ! 2029: ! 2030: /* See if any of the parmdecls specifies this parm by name. */ ! 2031: for (tail = parmdecls; tail; tail = TREE_CHAIN (tail)) ! 2032: if (DECL_NAME (tail) == TREE_VALUE (parm)) ! 2033: { ! 2034: found = tail; ! 2035: break; ! 2036: } ! 2037: ! 2038: /* If declaration already marked, we have a duplicate name. ! 2039: Complain, and don't use this decl twice. */ ! 2040: if (found && DECL_CONTEXT (found) != 0) ! 2041: { ! 2042: yylineerror (DECL_SOURCE_LINE (fndecl), ! 2043: "multiple parameters named %s", ! 2044: IDENTIFIER_POINTER (TREE_VALUE (parm))); ! 2045: found = 0; ! 2046: } ! 2047: ! 2048: /* See if the type was given in the arglist. ! 2049: If so, that overrides any parmdecl. */ ! 2050: if (TREE_PURPOSE (parm) != 0) ! 2051: { ! 2052: /* Error to have type in the arglist and have a parmdecl. */ ! 2053: if (found) ! 2054: yylineerror (DECL_SOURCE_LINE (fndecl), ! 2055: "type for parameter %s given twice", ! 2056: IDENTIFIER_POINTER (TREE_VALUE (parm))); ! 2057: ! 2058: found = build_decl (PARM_DECL, TREE_VALUE (parm), ! 2059: TREE_PURPOSE (parm), 0, 0); ! 2060: DECL_ARG_TYPE (found) == TREE_TYPE (found); ! 2061: ! 2062: DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl); ! 2063: DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl); ! 2064: /* Note that this pushdecl is needed despite the storedecls below ! 2065: in order to make the name know about its decl. */ ! 2066: pushdecl (found); ! 2067: } ! 2068: ! 2069: /* If no declaration found, default to int. */ ! 2070: if (!found) ! 2071: { ! 2072: found = build_decl (PARM_DECL, TREE_VALUE (parm), ! 2073: integer_type_node, 0, 0); ! 2074: DECL_ARG_TYPE (found) = TREE_TYPE (found); ! 2075: DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl); ! 2076: DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl); ! 2077: pushdecl (found); ! 2078: } ! 2079: ! 2080: TREE_PURPOSE (parm) = found; ! 2081: ! 2082: /* Mark this declaration as belonging to this function. */ ! 2083: ! 2084: DECL_CONTEXT (found) = block; ! 2085: } ! 2086: ! 2087: /* Complain about declarations not matched with any names. */ ! 2088: ! 2089: for (parm = parmdecls; parm; parm = TREE_CHAIN (parm)) ! 2090: { ! 2091: if (DECL_CONTEXT (parm) == 0) ! 2092: yylineerror (DECL_SOURCE_LINE (parm), ! 2093: "declaration for parameter %s but no such parameter", ! 2094: IDENTIFIER_POINTER (DECL_NAME (parm))); ! 2095: } ! 2096: ! 2097: /* Chain the declarations together in the order of the list of names. */ ! 2098: /* Store that chain in the function decl, replacing the list of names. */ ! 2099: parm = DECL_ARGUMENTS (fndecl); ! 2100: DECL_ARGUMENTS (fndecl) = 0; ! 2101: { ! 2102: register tree last; ! 2103: for (last = 0; parm; parm = TREE_CHAIN (parm)) ! 2104: if (TREE_PURPOSE (parm)) ! 2105: { ! 2106: if (last == 0) ! 2107: DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm); ! 2108: else ! 2109: TREE_CHAIN (last) = TREE_PURPOSE (parm); ! 2110: last = TREE_PURPOSE (parm); ! 2111: TREE_CHAIN (last) = 0; ! 2112: } ! 2113: } ! 2114: ! 2115: /* Now store the final chain of decls for the arguments ! 2116: as the decl-chain of the current lexical scope. */ ! 2117: ! 2118: storedecls (DECL_ARGUMENTS (fndecl)); ! 2119: ! 2120: /* Compute the offset of each parameter wrt the entire arglist ! 2121: and store it in the parameter's decl node. */ ! 2122: ! 2123: layout_parms (fndecl); ! 2124: } ! 2125: ! 2126: /* Finish up a function declaration and compile that function ! 2127: all the way to assembler language output. The free the storage ! 2128: for the function definition. ! 2129: ! 2130: This is called after parsing the body of the function definition. ! 2131: STMTS is the chain of statements that makes up the function body. */ ! 2132: ! 2133: void ! 2134: finish_function (filename, line, stmts) ! 2135: char *filename; ! 2136: int line; ! 2137: tree stmts; ! 2138: { ! 2139: register tree fndecl = current_function_decl; ! 2140: register tree block = DECL_INITIAL (fndecl); ! 2141: int old_uid; ! 2142: ! 2143: register tree link; ! 2144: ! 2145: /* TREE_READONLY (fndecl) = 1; ! 2146: This caused &foo to be of type ptr-to-const-function ! 2147: which then got a warning when stored in a ptr-to-function variable. */ ! 2148: ! 2149: TREE_EXTERNAL (fndecl) = 0; ! 2150: TREE_STATIC (fndecl) = 1; ! 2151: ! 2152: finish_block (block, NULL_TREE, NULL, stmts); ! 2153: ! 2154: DECL_SOURCE_FILE (block) = filename; ! 2155: DECL_SOURCE_LINE (block) = line; ! 2156: ! 2157: poplevel (); ! 2158: current_switch_stmt = NULL; ! 2159: current_block = NULL; ! 2160: ! 2161: rest_of_compilation (fndecl, 1); ! 2162: ! 2163: /* Free all the tree nodes making up this function. */ ! 2164: /* Switch back to allocating nodes permanently ! 2165: until we start another function. */ ! 2166: permanent_allocation (); ! 2167: ! 2168: /* Stop pointing to the local nodes about to be freed. */ ! 2169: /* But DECL_INITIAL must remain nonzero so we know this ! 2170: was an actual function definition. */ ! 2171: DECL_INITIAL (fndecl) = (tree) 1; ! 2172: DECL_ARGUMENTS (fndecl) = 0; ! 2173: DECL_RESULT (fndecl) = 0; ! 2174: } ! 2175: ! 2176: tree ! 2177: implicitly_declare (functionid) ! 2178: tree functionid; ! 2179: { ! 2180: register tree decl ! 2181: = build_decl (FUNCTION_DECL, functionid, ! 2182: default_function_type, 0, 1); ! 2183: ! 2184: /* ANSI standard says implicit declarations are in the innermost block */ ! 2185: pushdecl (decl); ! 2186: return decl; ! 2187: } ! 2188: ! 2189: /* Return nonzero if the declaration new is legal ! 2190: when the declaration old (assumed to be for the same name) ! 2191: has already been seen. */ ! 2192: ! 2193: int ! 2194: allowed_redeclaration (new, old, global) ! 2195: tree new, old; ! 2196: int global; ! 2197: { ! 2198: if (!comptypes (TREE_TYPE (new), TREE_TYPE (old))) ! 2199: return 0; ! 2200: ! 2201: if (global) ! 2202: { ! 2203: /* Reject two definitions. */ ! 2204: if (DECL_INITIAL (old) != 0 && DECL_INITIAL (new) != 0) ! 2205: return 0; ! 2206: /* Reject two tentative definitions with different linkage. */ ! 2207: if (!TREE_EXTERNAL (new) && !TREE_EXTERNAL (old) ! 2208: && TREE_STATIC (old) != TREE_STATIC (new)) ! 2209: return 0; ! 2210: return 1; ! 2211: } ! 2212: else if (TREE_CODE (TREE_TYPE (new)) == FUNCTION_TYPE) ! 2213: { ! 2214: /* Declarations of functions inside blocks ! 2215: are just references, and do not determine linkage. */ ! 2216: return 1; ! 2217: } ! 2218: else ! 2219: { ! 2220: /* Reject two definitions, and reject a definition ! 2221: together with an external reference. */ ! 2222: return TREE_EXTERNAL (new) && TREE_EXTERNAL (old); ! 2223: } ! 2224: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.