| 1 | /*- |
| 2 | * This code is derived from software copyrighted by the Free Software |
| 3 | * Foundation. |
| 4 | * |
| 5 | * Modified 1991 by Donn Seeley at UUNET Technologies, Inc. |
| 6 | */ |
| 7 | |
| 8 | #ifndef lint |
| 9 | static char sccsid[] = "@(#)c-decl.c 6.3 (Berkeley) 5/8/91"; |
| 10 | #endif /* not lint */ |
| 11 | |
| 12 | /* Process declarations and variables for C compiler. |
| 13 | Copyright (C) 1988 Free Software Foundation, Inc. |
| 14 | |
| 15 | This file is part of GNU CC. |
| 16 | |
| 17 | GNU CC is free software; you can redistribute it and/or modify |
| 18 | it under the terms of the GNU General Public License as published by |
| 19 | the Free Software Foundation; either version 1, or (at your option) |
| 20 | any later version. |
| 21 | |
| 22 | GNU CC is distributed in the hope that it will be useful, |
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | GNU General Public License for more details. |
| 26 | |
| 27 | You should have received a copy of the GNU General Public License |
| 28 | along with GNU CC; see the file COPYING. If not, write to |
| 29 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 30 | |
| 31 | |
| 32 | /* Process declarations and symbol lookup for C front end. |
| 33 | Also constructs types; the standard scalar types at initialization, |
| 34 | and structure, union, array and enum types when they are declared. */ |
| 35 | |
| 36 | /* ??? not all decl nodes are given the most useful possible |
| 37 | line numbers. For example, the CONST_DECLs for enum values. */ |
| 38 | |
| 39 | #include "config.h" |
| 40 | #include "tree.h" |
| 41 | #include "flags.h" |
| 42 | #include "c-tree.h" |
| 43 | #include "c-parse.h" |
| 44 | |
| 45 | #include <stdio.h> |
| 46 | |
| 47 | /* In grokdeclarator, distinguish syntactic contexts of declarators. */ |
| 48 | enum decl_context |
| 49 | { NORMAL, /* Ordinary declaration */ |
| 50 | FUNCDEF, /* Function definition */ |
| 51 | PARM, /* Declaration of parm before function body */ |
| 52 | FIELD, /* Declaration inside struct or union */ |
| 53 | TYPENAME}; /* Typename (inside cast or sizeof) */ |
| 54 | |
| 55 | #define NULL 0 |
| 56 | #define MIN(X,Y) ((X) < (Y) ? (X) : (Y)) |
| 57 | #define MAX(X,Y) ((X) > (Y) ? (X) : (Y)) |
| 58 | |
| 59 | #ifndef CHAR_TYPE_SIZE |
| 60 | #define CHAR_TYPE_SIZE BITS_PER_UNIT |
| 61 | #endif |
| 62 | |
| 63 | #ifndef SHORT_TYPE_SIZE |
| 64 | #define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2)) |
| 65 | #endif |
| 66 | |
| 67 | #ifndef INT_TYPE_SIZE |
| 68 | #define INT_TYPE_SIZE BITS_PER_WORD |
| 69 | #endif |
| 70 | |
| 71 | #ifndef LONG_TYPE_SIZE |
| 72 | #define LONG_TYPE_SIZE BITS_PER_WORD |
| 73 | #endif |
| 74 | |
| 75 | #ifndef LONG_LONG_TYPE_SIZE |
| 76 | #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2) |
| 77 | #endif |
| 78 | |
| 79 | #ifndef FLOAT_TYPE_SIZE |
| 80 | #define FLOAT_TYPE_SIZE BITS_PER_WORD |
| 81 | #endif |
| 82 | |
| 83 | #ifndef DOUBLE_TYPE_SIZE |
| 84 | #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2) |
| 85 | #endif |
| 86 | |
| 87 | #ifndef LONG_DOUBLE_TYPE_SIZE |
| 88 | #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2) |
| 89 | #endif |
| 90 | \f |
| 91 | /* a node which has tree code ERROR_MARK, and whose type is itself. |
| 92 | All erroneous expressions are replaced with this node. All functions |
| 93 | that accept nodes as arguments should avoid generating error messages |
| 94 | if this node is one of the arguments, since it is undesirable to get |
| 95 | multiple error messages from one error in the input. */ |
| 96 | |
| 97 | tree error_mark_node; |
| 98 | |
| 99 | /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */ |
| 100 | |
| 101 | tree short_integer_type_node; |
| 102 | tree integer_type_node; |
| 103 | tree long_integer_type_node; |
| 104 | tree long_long_integer_type_node; |
| 105 | |
| 106 | tree short_unsigned_type_node; |
| 107 | tree unsigned_type_node; |
| 108 | tree long_unsigned_type_node; |
| 109 | tree long_long_unsigned_type_node; |
| 110 | |
| 111 | tree unsigned_char_type_node; |
| 112 | tree signed_char_type_node; |
| 113 | tree char_type_node; |
| 114 | |
| 115 | tree float_type_node; |
| 116 | tree double_type_node; |
| 117 | tree long_double_type_node; |
| 118 | |
| 119 | /* a VOID_TYPE node. */ |
| 120 | |
| 121 | tree void_type_node; |
| 122 | |
| 123 | /* A node for type `void *'. */ |
| 124 | |
| 125 | tree ptr_type_node; |
| 126 | |
| 127 | /* A node for type `char *'. */ |
| 128 | |
| 129 | tree string_type_node; |
| 130 | |
| 131 | /* Type `char[256]' or something like it. |
| 132 | Used when an array of char is needed and the size is irrelevant. */ |
| 133 | |
| 134 | tree char_array_type_node; |
| 135 | |
| 136 | /* Type `int[256]' or something like it. |
| 137 | Used when an array of int needed and the size is irrelevant. */ |
| 138 | |
| 139 | tree int_array_type_node; |
| 140 | |
| 141 | /* type `int ()' -- used for implicit declaration of functions. */ |
| 142 | |
| 143 | tree default_function_type; |
| 144 | |
| 145 | /* function types `double (double)' and `double (double, double)', etc. */ |
| 146 | |
| 147 | tree double_ftype_double, double_ftype_double_double; |
| 148 | tree int_ftype_int, long_ftype_long; |
| 149 | |
| 150 | /* Function type `void (void *, void *, int)' and similar ones */ |
| 151 | |
| 152 | tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int; |
| 153 | |
| 154 | /* Two expressions that are constants with value zero. |
| 155 | The first is of type `int', the second of type `void *'. */ |
| 156 | |
| 157 | tree integer_zero_node; |
| 158 | tree null_pointer_node; |
| 159 | |
| 160 | /* A node for the integer constant 1. */ |
| 161 | |
| 162 | tree integer_one_node; |
| 163 | |
| 164 | /* An identifier whose name is <value>. This is used as the "name" |
| 165 | of the RESULT_DECLs for values of functions. */ |
| 166 | |
| 167 | tree value_identifier; |
| 168 | |
| 169 | /* While defining an enum type, this is 1 plus the last enumerator |
| 170 | constant value. */ |
| 171 | |
| 172 | static tree enum_next_value; |
| 173 | |
| 174 | /* Parsing a function declarator leaves a list of parameter names |
| 175 | or a chain or parameter decls here. */ |
| 176 | |
| 177 | static tree last_function_parms; |
| 178 | |
| 179 | /* Parsing a function declarator leaves here a chain of structure |
| 180 | and enum types declared in the parmlist. */ |
| 181 | |
| 182 | static tree last_function_parm_tags; |
| 183 | |
| 184 | /* After parsing the declarator that starts a function definition, |
| 185 | `start_function' puts here the list of parameter names or chain of decls. |
| 186 | `store_parm_decls' finds it here. */ |
| 187 | |
| 188 | static tree current_function_parms; |
| 189 | |
| 190 | /* Similar, for last_function_parm_tags. */ |
| 191 | static tree current_function_parm_tags; |
| 192 | |
| 193 | /* A list (chain of TREE_LIST nodes) of all LABEL_STMTs in the function |
| 194 | that have names. Here so we can clear out their names' definitions |
| 195 | at the end of the function. */ |
| 196 | |
| 197 | static tree named_labels; |
| 198 | |
| 199 | /* The FUNCTION_DECL for the function currently being compiled, |
| 200 | or 0 if between functions. */ |
| 201 | tree current_function_decl; |
| 202 | |
| 203 | /* Set to 0 at beginning of a function definition, set to 1 if |
| 204 | a return statement that specifies a return value is seen. */ |
| 205 | |
| 206 | int current_function_returns_value; |
| 207 | |
| 208 | /* Set to 0 at beginning of a function definition, set to 1 if |
| 209 | a return statement with no argument is seen. */ |
| 210 | |
| 211 | int current_function_returns_null; |
| 212 | |
| 213 | /* Set to nonzero by `grokdeclarator' for a function |
| 214 | whose return type is defaulted, if warnings for this are desired. */ |
| 215 | |
| 216 | static int warn_about_return_type; |
| 217 | |
| 218 | /* Nonzero when starting a function delcared `extern inline'. */ |
| 219 | |
| 220 | static int current_extern_inline; |
| 221 | \f |
| 222 | /* For each binding contour we allocate a binding_level structure |
| 223 | * which records the names defined in that contour. |
| 224 | * Contours include: |
| 225 | * 0) the global one |
| 226 | * 1) one for each function definition, |
| 227 | * where internal declarations of the parameters appear. |
| 228 | * 2) one for each compound statement, |
| 229 | * to record its declarations. |
| 230 | * |
| 231 | * The current meaning of a name can be found by searching the levels from |
| 232 | * the current one out to the global one. |
| 233 | */ |
| 234 | |
| 235 | /* Note that the information in the `names' component of the global contour |
| 236 | is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */ |
| 237 | |
| 238 | struct binding_level |
| 239 | { |
| 240 | /* A chain of _DECL nodes for all variables, constants, functions, |
| 241 | and typedef types. These are in the reverse of the order supplied. |
| 242 | */ |
| 243 | tree names; |
| 244 | |
| 245 | /* A list of structure, union and enum definitions, |
| 246 | * for looking up tag names. |
| 247 | * It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name, |
| 248 | * or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE, |
| 249 | * or ENUMERAL_TYPE node. |
| 250 | */ |
| 251 | tree tags; |
| 252 | |
| 253 | /* For each level, a list of shadowed outer-level local definitions |
| 254 | to be restored when this level is popped. |
| 255 | Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and |
| 256 | whose TREE_VALUE is its old definition (a kind of ..._DECL node). */ |
| 257 | tree shadowed; |
| 258 | |
| 259 | /* For each level (except not the global one), |
| 260 | a chain of LET_STMT nodes for all the levels |
| 261 | that were entered and exited one level down. */ |
| 262 | tree blocks; |
| 263 | |
| 264 | /* The binding level which this one is contained in (inherits from). */ |
| 265 | struct binding_level *level_chain; |
| 266 | |
| 267 | /* Nonzero for the level that holds the parameters of a function. */ |
| 268 | char parm_flag; |
| 269 | |
| 270 | /* Nonzero if this level "doesn't exist" for tags. */ |
| 271 | char tag_transparent; |
| 272 | |
| 273 | /* Nonzero means make a LET_STMT for this level regardless of all else. */ |
| 274 | char keep; |
| 275 | |
| 276 | /* Nonzero means make a LET_STMT if this level has any subblocks. */ |
| 277 | char keep_if_subblocks; |
| 278 | |
| 279 | /* Number of decls in `names' that have incomplete |
| 280 | structure or union types. */ |
| 281 | int n_incomplete; |
| 282 | }; |
| 283 | |
| 284 | #define NULL_BINDING_LEVEL (struct binding_level *) NULL |
| 285 | |
| 286 | /* The binding level currently in effect. */ |
| 287 | |
| 288 | static struct binding_level *current_binding_level; |
| 289 | |
| 290 | /* A chain of binding_level structures awaiting reuse. */ |
| 291 | |
| 292 | static struct binding_level *free_binding_level; |
| 293 | |
| 294 | /* The outermost binding level, for names of file scope. |
| 295 | This is created when the compiler is started and exists |
| 296 | through the entire run. */ |
| 297 | |
| 298 | static struct binding_level *global_binding_level; |
| 299 | |
| 300 | /* Binding level structures are initialized by copying this one. */ |
| 301 | |
| 302 | static struct binding_level clear_binding_level |
| 303 | = {NULL, NULL, NULL, NULL, NULL, 0, 0, 0}; |
| 304 | |
| 305 | /* Nonzero means unconditionally make a LET_STMT for the next level pushed. */ |
| 306 | |
| 307 | static int keep_next_level_flag; |
| 308 | |
| 309 | /* Nonzero means make a LET_STMT for the next level pushed |
| 310 | if it has subblocks. */ |
| 311 | |
| 312 | static int keep_next_if_subblocks; |
| 313 | |
| 314 | /* Forward declarations. */ |
| 315 | |
| 316 | static tree grokparms (), grokdeclarator (); |
| 317 | tree pushdecl (); |
| 318 | static void builtin_function (); |
| 319 | |
| 320 | static tree lookup_tag (); |
| 321 | static tree lookup_tag_reverse (); |
| 322 | static tree lookup_name_current_level (); |
| 323 | static char *redeclaration_error_message (); |
| 324 | static void layout_array_type (); |
| 325 | \f |
| 326 | /* C-specific option variables. */ |
| 327 | |
| 328 | /* Nonzero means allow type mismatches in conditional expressions; |
| 329 | just make their values `void'. */ |
| 330 | |
| 331 | int flag_cond_mismatch; |
| 332 | |
| 333 | /* Nonzero means don't recognize the keyword `asm'. */ |
| 334 | |
| 335 | int flag_no_asm; |
| 336 | |
| 337 | /* Nonzero means do some things the same way PCC does. */ |
| 338 | |
| 339 | int flag_traditional; |
| 340 | |
| 341 | /* Nonzero means warn about implicit declarations. */ |
| 342 | |
| 343 | int warn_implicit; |
| 344 | |
| 345 | /* Nonzero means warn about function definitions that default the return type |
| 346 | or that use a null return and have a return-type other than void. */ |
| 347 | |
| 348 | int warn_return_type; |
| 349 | |
| 350 | /* Nonzero means give string constants the type `const char *' |
| 351 | to get extra warnings from them. These warnings will be too numerous |
| 352 | to be useful, except in thoroughly ANSIfied programs. */ |
| 353 | |
| 354 | int warn_write_strings; |
| 355 | |
| 356 | /* Nonzero means warn about pointer casts that can drop a type qualifier |
| 357 | from the pointer target type. */ |
| 358 | |
| 359 | int warn_cast_qual; |
| 360 | |
| 361 | /* Nonzero means warn about sizeof(function) or addition/subtraction |
| 362 | of function pointers. */ |
| 363 | |
| 364 | int warn_pointer_arith; |
| 365 | |
| 366 | /* Nonzero means warn for all old-style non-prototype function decls. */ |
| 367 | |
| 368 | int warn_strict_prototypes; |
| 369 | |
| 370 | /* Nonzero means `$' can be in an identifier. |
| 371 | See cccp.c for reasons why this breaks some obscure ANSI C programs. */ |
| 372 | |
| 373 | #ifndef DOLLARS_IN_IDENTIFIERS |
| 374 | #define DOLLARS_IN_IDENTIFIERS 0 |
| 375 | #endif |
| 376 | int dollars_in_ident = DOLLARS_IN_IDENTIFIERS; |
| 377 | |
| 378 | char *language_string = "GNU C"; |
| 379 | |
| 380 | /* Decode the string P as a language-specific option. |
| 381 | Return 1 if it is recognized (and handle it); |
| 382 | return 0 if not recognized. */ |
| 383 | |
| 384 | int |
| 385 | lang_decode_option (p) |
| 386 | char *p; |
| 387 | { |
| 388 | if (!strcmp (p, "-ftraditional") || !strcmp (p, "-traditional")) |
| 389 | flag_traditional = 1, dollars_in_ident = 1, flag_writable_strings = 1; |
| 390 | else if (!strcmp (p, "-fsigned-char")) |
| 391 | flag_signed_char = 1; |
| 392 | else if (!strcmp (p, "-funsigned-char")) |
| 393 | flag_signed_char = 0; |
| 394 | else if (!strcmp (p, "-fno-signed-char")) |
| 395 | flag_signed_char = 0; |
| 396 | else if (!strcmp (p, "-fno-unsigned-char")) |
| 397 | flag_signed_char = 1; |
| 398 | else if (!strcmp (p, "-fshort-enums")) |
| 399 | flag_short_enums = 1; |
| 400 | else if (!strcmp (p, "-fno-short-enums")) |
| 401 | flag_short_enums = 0; |
| 402 | else if (!strcmp (p, "-fcond-mismatch")) |
| 403 | flag_cond_mismatch = 1; |
| 404 | else if (!strcmp (p, "-fno-cond-mismatch")) |
| 405 | flag_cond_mismatch = 0; |
| 406 | else if (!strcmp (p, "-fasm")) |
| 407 | flag_no_asm = 0; |
| 408 | else if (!strcmp (p, "-fno-asm")) |
| 409 | flag_no_asm = 1; |
| 410 | else if (!strcmp (p, "-ansi")) |
| 411 | flag_no_asm = 1, dollars_in_ident = 0; |
| 412 | else if (!strcmp (p, "-Wimplicit")) |
| 413 | warn_implicit = 1; |
| 414 | else if (!strcmp (p, "-Wreturn-type")) |
| 415 | warn_return_type = 1; |
| 416 | else if (!strcmp (p, "-Wwrite-strings")) |
| 417 | warn_write_strings = 1; |
| 418 | else if (!strcmp (p, "-Wcast-qual")) |
| 419 | warn_cast_qual = 1; |
| 420 | else if (!strcmp (p, "-Wpointer-arith")) |
| 421 | warn_pointer_arith = 1; |
| 422 | else if (!strcmp (p, "-Wstrict-prototypes")) |
| 423 | warn_strict_prototypes = 1; |
| 424 | else if (!strcmp (p, "-Wcomment")) |
| 425 | ; /* cpp handles this one. */ |
| 426 | else if (!strcmp (p, "-Wcomments")) |
| 427 | ; /* cpp handles this one. */ |
| 428 | else if (!strcmp (p, "-Wtrigraphs")) |
| 429 | ; /* cpp handles this one. */ |
| 430 | else if (!strcmp (p, "-Wall")) |
| 431 | { |
| 432 | extra_warnings = 1; |
| 433 | warn_implicit = 1; |
| 434 | warn_return_type = 1; |
| 435 | warn_unused = 1; |
| 436 | warn_switch = 1; |
| 437 | } |
| 438 | else |
| 439 | return 0; |
| 440 | |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | void |
| 445 | print_lang_identifier (file, node, indent) |
| 446 | FILE *file; |
| 447 | tree node; |
| 448 | int indent; |
| 449 | { |
| 450 | print_node (file, "global", IDENTIFIER_GLOBAL_VALUE (node), indent + 4); |
| 451 | print_node (file, "local", IDENTIFIER_LOCAL_VALUE (node), indent + 4); |
| 452 | print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4); |
| 453 | print_node (file, "implicit", IDENTIFIER_IMPLICIT_DECL (node), indent + 4); |
| 454 | print_node (file, "error locus", IDENTIFIER_ERROR_LOCUS (node), indent + 4); |
| 455 | } |
| 456 | \f |
| 457 | /* Create a new `struct binding_level'. */ |
| 458 | |
| 459 | static |
| 460 | struct binding_level * |
| 461 | make_binding_level () |
| 462 | { |
| 463 | /* NOSTRICT */ |
| 464 | return (struct binding_level *) xmalloc (sizeof (struct binding_level)); |
| 465 | } |
| 466 | |
| 467 | /* Nonzero if we are currently in the global binding level. */ |
| 468 | |
| 469 | int |
| 470 | global_bindings_p () |
| 471 | { |
| 472 | return current_binding_level == global_binding_level; |
| 473 | } |
| 474 | |
| 475 | void |
| 476 | keep_next_level () |
| 477 | { |
| 478 | keep_next_level_flag = 1; |
| 479 | } |
| 480 | |
| 481 | /* Nonzero if the current level needs to have a LET_STMT made. */ |
| 482 | |
| 483 | int |
| 484 | kept_level_p () |
| 485 | { |
| 486 | return ((current_binding_level->keep_if_subblocks |
| 487 | && current_binding_level->blocks != 0) |
| 488 | || current_binding_level->keep |
| 489 | || current_binding_level->names != 0); |
| 490 | } |
| 491 | |
| 492 | /* Identify this binding level as a level of parameters. */ |
| 493 | |
| 494 | void |
| 495 | declare_parm_level () |
| 496 | { |
| 497 | current_binding_level->parm_flag = 1; |
| 498 | } |
| 499 | |
| 500 | /* Nonzero if currently making parm declarations. */ |
| 501 | |
| 502 | in_parm_level_p () |
| 503 | { |
| 504 | return current_binding_level->parm_flag; |
| 505 | } |
| 506 | |
| 507 | /* Enter a new binding level. |
| 508 | If TAG_TRANSPARENT is nonzero, do so only for the name space of variables, |
| 509 | not for that of tags. */ |
| 510 | |
| 511 | void |
| 512 | pushlevel (tag_transparent) |
| 513 | int tag_transparent; |
| 514 | { |
| 515 | register struct binding_level *newlevel = NULL_BINDING_LEVEL; |
| 516 | |
| 517 | /* If this is the top level of a function, |
| 518 | just make sure that NAMED_LABELS is 0. |
| 519 | They should have been set to 0 at the end of the previous function. */ |
| 520 | |
| 521 | if (current_binding_level == global_binding_level) |
| 522 | { |
| 523 | if (named_labels) |
| 524 | abort (); |
| 525 | } |
| 526 | |
| 527 | /* Reuse or create a struct for this binding level. */ |
| 528 | |
| 529 | if (free_binding_level) |
| 530 | { |
| 531 | newlevel = free_binding_level; |
| 532 | free_binding_level = free_binding_level->level_chain; |
| 533 | } |
| 534 | else |
| 535 | { |
| 536 | newlevel = make_binding_level (); |
| 537 | } |
| 538 | |
| 539 | /* Add this level to the front of the chain (stack) of levels that |
| 540 | are active. */ |
| 541 | |
| 542 | *newlevel = clear_binding_level; |
| 543 | newlevel->level_chain = current_binding_level; |
| 544 | current_binding_level = newlevel; |
| 545 | newlevel->tag_transparent = tag_transparent; |
| 546 | newlevel->keep = keep_next_level_flag; |
| 547 | keep_next_level_flag = 0; |
| 548 | newlevel->keep_if_subblocks = keep_next_if_subblocks; |
| 549 | keep_next_if_subblocks = 0; |
| 550 | } |
| 551 | |
| 552 | /* Exit a binding level. |
| 553 | Pop the level off, and restore the state of the identifier-decl mappings |
| 554 | that were in effect when this level was entered. |
| 555 | |
| 556 | If KEEP is nonzero, this level had explicit declarations, so |
| 557 | and create a "block" (a LET_STMT node) for the level |
| 558 | to record its declarations and subblocks for symbol table output. |
| 559 | |
| 560 | If FUNCTIONBODY is nonzero, this level is the body of a function, |
| 561 | so create a block as if KEEP were set and also clear out all |
| 562 | label names. |
| 563 | |
| 564 | If REVERSE is nonzero, reverse the order of decls before putting |
| 565 | them into the LET_STMT. */ |
| 566 | |
| 567 | tree |
| 568 | poplevel (keep, reverse, functionbody) |
| 569 | int keep; |
| 570 | int reverse; |
| 571 | int functionbody; |
| 572 | { |
| 573 | register tree link; |
| 574 | /* The chain of decls was accumulated in reverse order. |
| 575 | Put it into forward order, just for cleanliness. */ |
| 576 | tree decls; |
| 577 | tree tags = current_binding_level->tags; |
| 578 | tree subblocks = current_binding_level->blocks; |
| 579 | tree block = 0; |
| 580 | |
| 581 | keep |= current_binding_level->keep; |
| 582 | |
| 583 | /* This warning is turned off because it causes warnings for |
| 584 | declarations like `extern struct foo *x'. */ |
| 585 | #if 0 |
| 586 | /* Warn about incomplete structure types in this level. */ |
| 587 | for (link = tags; link; link = TREE_CHAIN (link)) |
| 588 | if (TYPE_SIZE (TREE_VALUE (link)) == 0) |
| 589 | { |
| 590 | tree type = TREE_VALUE (link); |
| 591 | char *errmsg; |
| 592 | switch (TREE_CODE (type)) |
| 593 | { |
| 594 | case RECORD_TYPE: |
| 595 | errmsg = "`struct %s' incomplete in scope ending here"; |
| 596 | break; |
| 597 | case UNION_TYPE: |
| 598 | errmsg = "`union %s' incomplete in scope ending here"; |
| 599 | break; |
| 600 | case ENUMERAL_TYPE: |
| 601 | errmsg = "`enum %s' incomplete in scope ending here"; |
| 602 | break; |
| 603 | } |
| 604 | if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE) |
| 605 | error (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type))); |
| 606 | else |
| 607 | /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */ |
| 608 | error (errmsg, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))); |
| 609 | } |
| 610 | #endif /* 0 */ |
| 611 | |
| 612 | /* Get the decls in the order they were written. |
| 613 | Usually current_binding_level->names is in reverse order. |
| 614 | But parameter decls were previously put in forward order. */ |
| 615 | |
| 616 | if (reverse) |
| 617 | current_binding_level->names |
| 618 | = decls = nreverse (current_binding_level->names); |
| 619 | else |
| 620 | decls = current_binding_level->names; |
| 621 | |
| 622 | /* If there were any declarations or structure tags in that level, |
| 623 | or if this level is a function body, |
| 624 | create a LET_STMT to record them for the life of this function. */ |
| 625 | |
| 626 | if (keep || functionbody |
| 627 | || (current_binding_level->keep_if_subblocks && subblocks != 0)) |
| 628 | block = build_let (0, 0, keep ? decls : 0, |
| 629 | subblocks, 0, keep ? tags : 0); |
| 630 | |
| 631 | /* In each subblock, record that this is its superior. */ |
| 632 | |
| 633 | for (link = subblocks; link; link = TREE_CHAIN (link)) |
| 634 | STMT_SUPERCONTEXT (link) = block; |
| 635 | |
| 636 | /* Clear out the meanings of the local variables of this level; |
| 637 | also record in each decl which block it belongs to. */ |
| 638 | |
| 639 | for (link = decls; link; link = TREE_CHAIN (link)) |
| 640 | { |
| 641 | if (DECL_NAME (link) != 0) |
| 642 | { |
| 643 | /* If the ident. was used via a local extern decl, |
| 644 | don't forget that fact. */ |
| 645 | if (TREE_USED (link) && TREE_EXTERNAL (link)) |
| 646 | TREE_USED (DECL_NAME (link)) = 1; |
| 647 | IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0; |
| 648 | } |
| 649 | DECL_CONTEXT (link) = block; |
| 650 | } |
| 651 | |
| 652 | /* Restore all name-meanings of the outer levels |
| 653 | that were shadowed by this level. */ |
| 654 | |
| 655 | for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link)) |
| 656 | IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link); |
| 657 | |
| 658 | /* If the level being exited is the top level of a function, |
| 659 | check over all the labels. */ |
| 660 | |
| 661 | if (functionbody) |
| 662 | { |
| 663 | /* Clear out the definitions of all label names, |
| 664 | since their scopes end here. */ |
| 665 | |
| 666 | for (link = named_labels; link; link = TREE_CHAIN (link)) |
| 667 | { |
| 668 | if (DECL_SOURCE_LINE (TREE_VALUE (link)) == 0) |
| 669 | { |
| 670 | error ("label `%s' used somewhere above but not defined", |
| 671 | IDENTIFIER_POINTER (DECL_NAME (TREE_VALUE (link)))); |
| 672 | /* Avoid crashing later. */ |
| 673 | define_label (input_filename, 1, DECL_NAME (TREE_VALUE (link))); |
| 674 | } |
| 675 | else if (warn_unused && !TREE_USED (TREE_VALUE (link))) |
| 676 | warning_with_decl (TREE_VALUE (link), |
| 677 | "label `%s' defined but not used"); |
| 678 | IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link))) = 0; |
| 679 | } |
| 680 | |
| 681 | named_labels = 0; |
| 682 | } |
| 683 | |
| 684 | /* Pop the current level, and free the structure for reuse. */ |
| 685 | |
| 686 | { |
| 687 | register struct binding_level *level = current_binding_level; |
| 688 | current_binding_level = current_binding_level->level_chain; |
| 689 | |
| 690 | level->level_chain = free_binding_level; |
| 691 | free_binding_level = level; |
| 692 | } |
| 693 | |
| 694 | if (functionbody) |
| 695 | { |
| 696 | DECL_INITIAL (current_function_decl) = block; |
| 697 | /* If this is the top level block of a function, |
| 698 | the vars are the function's parameters. |
| 699 | Don't leave them in the LET_STMT because they are |
| 700 | found in the FUNCTION_DECL instead. */ |
| 701 | STMT_VARS (block) = 0; |
| 702 | } |
| 703 | else if (block) |
| 704 | current_binding_level->blocks |
| 705 | = chainon (current_binding_level->blocks, block); |
| 706 | /* If we did not make a block for the level just exited, |
| 707 | any blocks made for inner levels |
| 708 | (since they cannot be recorded as subblocks in that level) |
| 709 | must be carried forward so they will later become subblocks |
| 710 | of something else. */ |
| 711 | else if (subblocks) |
| 712 | current_binding_level->blocks |
| 713 | = chainon (current_binding_level->blocks, subblocks); |
| 714 | |
| 715 | if (block) |
| 716 | TREE_USED (block) = 1; |
| 717 | return block; |
| 718 | } |
| 719 | \f |
| 720 | /* Push a definition of struct, union or enum tag "name". |
| 721 | "type" should be the type node. |
| 722 | We assume that the tag "name" is not already defined. |
| 723 | |
| 724 | Note that the definition may really be just a forward reference. |
| 725 | In that case, the TYPE_SIZE will be zero. */ |
| 726 | |
| 727 | void |
| 728 | pushtag (name, type) |
| 729 | tree name, type; |
| 730 | { |
| 731 | register struct binding_level *b = current_binding_level; |
| 732 | while (b->tag_transparent) b = b->level_chain; |
| 733 | |
| 734 | if (name) |
| 735 | { |
| 736 | /* Record the identifier as the type's name if it has none. */ |
| 737 | |
| 738 | if (TYPE_NAME (type) == 0) |
| 739 | TYPE_NAME (type) = name; |
| 740 | |
| 741 | if (b == global_binding_level) |
| 742 | b->tags = perm_tree_cons (name, type, b->tags); |
| 743 | else |
| 744 | b->tags = saveable_tree_cons (name, type, b->tags); |
| 745 | } |
| 746 | } |
| 747 | \f |
| 748 | /* Handle when a new declaration NEWDECL |
| 749 | has the same name as an old one OLDDECL |
| 750 | in the same binding contour. |
| 751 | Prints an error message if appropriate. |
| 752 | |
| 753 | If safely possible, alter OLDDECL to look like NEWDECL, and return 1. |
| 754 | Otherwise, return 0. */ |
| 755 | |
| 756 | static int |
| 757 | duplicate_decls (newdecl, olddecl) |
| 758 | register tree newdecl, olddecl; |
| 759 | { |
| 760 | int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
| 761 | |
| 762 | if (TREE_CODE (TREE_TYPE (newdecl)) == ERROR_MARK |
| 763 | || TREE_CODE (TREE_TYPE (olddecl)) == ERROR_MARK) |
| 764 | types_match = 0; |
| 765 | |
| 766 | /* If this decl has linkage, and the old one does too, maybe no error. */ |
| 767 | if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) |
| 768 | { |
| 769 | error_with_decl (newdecl, "`%s' redeclared as different kind of symbol"); |
| 770 | error_with_decl (olddecl, "previous declaration of `%s'"); |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | if (flag_traditional && TREE_CODE (newdecl) == FUNCTION_DECL |
| 775 | && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (newdecl)) == olddecl |
| 776 | && DECL_INITIAL (olddecl) == 0) |
| 777 | /* If -traditional, avoid error for redeclaring fcn |
| 778 | after implicit decl. */ |
| 779 | ; |
| 780 | else if (TREE_CODE (olddecl) == FUNCTION_DECL |
| 781 | && DECL_FUNCTION_CODE (olddecl) != NOT_BUILT_IN) |
| 782 | { |
| 783 | if (!types_match) |
| 784 | error_with_decl (newdecl, "conflicting types for built-in function `%s'"); |
| 785 | else if (extra_warnings) |
| 786 | warning_with_decl (newdecl, "built-in function `%s' redeclared"); |
| 787 | } |
| 788 | else if (!types_match) |
| 789 | { |
| 790 | error_with_decl (newdecl, "conflicting types for `%s'"); |
| 791 | /* Check for function type mismatch |
| 792 | involving an empty arglist vs a nonempty one. */ |
| 793 | if (TREE_CODE (olddecl) == FUNCTION_DECL |
| 794 | && comptypes (TREE_TYPE (TREE_TYPE (olddecl)), |
| 795 | TREE_TYPE (TREE_TYPE (newdecl))) |
| 796 | && ((TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == 0 |
| 797 | && DECL_INITIAL (olddecl) == 0) |
| 798 | || |
| 799 | (TYPE_ARG_TYPES (TREE_TYPE (newdecl)) == 0 |
| 800 | && DECL_INITIAL (newdecl) == 0))) |
| 801 | { |
| 802 | /* Classify the problem further. */ |
| 803 | register tree t = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); |
| 804 | if (t == 0) |
| 805 | t = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); |
| 806 | for (; t; t = TREE_CHAIN (t)) |
| 807 | { |
| 808 | register tree type = TREE_VALUE (t); |
| 809 | |
| 810 | if (TREE_CHAIN (t) == 0 && type != void_type_node) |
| 811 | { |
| 812 | error ("A parameter list with an ellipsis can't match"); |
| 813 | error ("an empty parameter name list declaration."); |
| 814 | break; |
| 815 | } |
| 816 | |
| 817 | if (type == float_type_node |
| 818 | || (TREE_CODE (type) == INTEGER_TYPE |
| 819 | && (TYPE_PRECISION (type) |
| 820 | < TYPE_PRECISION (integer_type_node)))) |
| 821 | { |
| 822 | error ("An argument type that has a default promotion"); |
| 823 | error ("can't match an empty parameter name list declaration."); |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | error_with_decl (olddecl, "previous declaration of `%s'"); |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | char *errmsg = redeclaration_error_message (newdecl, olddecl); |
| 833 | if (errmsg) |
| 834 | { |
| 835 | error_with_decl (newdecl, errmsg); |
| 836 | error_with_decl (olddecl, |
| 837 | "here is the previous declaration of `%s'"); |
| 838 | } |
| 839 | else if (TREE_CODE (olddecl) == FUNCTION_DECL |
| 840 | && DECL_INITIAL (olddecl) != 0 |
| 841 | && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == 0 |
| 842 | && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0) |
| 843 | { |
| 844 | /* Prototype decl follows defn w/o prototype. */ |
| 845 | warning_with_decl (newdecl, "prototype for `%s'"); |
| 846 | warning_with_decl (olddecl, |
| 847 | "follows non-prototype definition here"); |
| 848 | } |
| 849 | |
| 850 | /* These bits are logically part of the type. */ |
| 851 | if (pedantic |
| 852 | && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl) |
| 853 | || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))) |
| 854 | warning_with_decl (newdecl, "type qualifiers for `%s' conflict with previous decl"); |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if (TREE_CODE (olddecl) == TREE_CODE (newdecl)) |
| 859 | { |
| 860 | int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL |
| 861 | && DECL_INITIAL (newdecl) != 0); |
| 862 | |
| 863 | /* Copy all the DECL_... slots specified in the new decl |
| 864 | except for any that we copy here from the old type. */ |
| 865 | |
| 866 | if (types_match) |
| 867 | { |
| 868 | tree oldtype = TREE_TYPE (olddecl); |
| 869 | /* Merge the data types specified in the two decls. */ |
| 870 | TREE_TYPE (newdecl) |
| 871 | = TREE_TYPE (olddecl) |
| 872 | = commontype (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); |
| 873 | |
| 874 | /* Lay the type out, unless already done. */ |
| 875 | if (oldtype != TREE_TYPE (newdecl)) |
| 876 | { |
| 877 | if (TREE_TYPE (newdecl) != error_mark_node) |
| 878 | layout_type (TREE_TYPE (newdecl)); |
| 879 | if (TREE_CODE (newdecl) != FUNCTION_DECL |
| 880 | && TREE_CODE (newdecl) != TYPE_DECL |
| 881 | && TREE_CODE (newdecl) != CONST_DECL) |
| 882 | layout_decl (newdecl, 0); |
| 883 | } |
| 884 | else |
| 885 | { |
| 886 | /* Since the type is OLDDECL's, make OLDDECL's size go with. */ |
| 887 | DECL_SIZE (newdecl) = DECL_SIZE (olddecl); |
| 888 | DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl); |
| 889 | if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) |
| 890 | DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl); |
| 891 | } |
| 892 | |
| 893 | /* Merge the type qualifiers. */ |
| 894 | if (TREE_READONLY (newdecl)) |
| 895 | TREE_READONLY (olddecl) = 1; |
| 896 | if (TREE_THIS_VOLATILE (newdecl)) |
| 897 | TREE_THIS_VOLATILE (olddecl) = 1; |
| 898 | |
| 899 | /* Merge the initialization information. */ |
| 900 | if (DECL_INITIAL (newdecl) == 0) |
| 901 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); |
| 902 | /* Keep the old rtl since we can safely use it. */ |
| 903 | DECL_RTL (newdecl) = DECL_RTL (olddecl); |
| 904 | } |
| 905 | /* If cannot merge, then use the new type and qualifiers, |
| 906 | and don't preserve the old rtl. */ |
| 907 | else |
| 908 | { |
| 909 | TREE_TYPE (olddecl) = TREE_TYPE (newdecl); |
| 910 | TREE_READONLY (olddecl) = TREE_READONLY (newdecl); |
| 911 | TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); |
| 912 | TREE_VOLATILE (olddecl) = TREE_VOLATILE (newdecl); |
| 913 | } |
| 914 | |
| 915 | /* Merge the storage class information. */ |
| 916 | if (TREE_EXTERNAL (newdecl)) |
| 917 | { |
| 918 | TREE_STATIC (newdecl) = TREE_STATIC (olddecl); |
| 919 | TREE_EXTERNAL (newdecl) = TREE_EXTERNAL (olddecl); |
| 920 | |
| 921 | /* For functions, static overrides non-static. */ |
| 922 | if (TREE_CODE (newdecl) == FUNCTION_DECL) |
| 923 | { |
| 924 | TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl); |
| 925 | /* This is since we don't automatically |
| 926 | copy the attributes of NEWDECL into OLDDECL. */ |
| 927 | TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); |
| 928 | /* If this clears `static', clear it in the identifier too. */ |
| 929 | if (! TREE_PUBLIC (olddecl)) |
| 930 | TREE_PUBLIC (DECL_NAME (olddecl)) = 0; |
| 931 | } |
| 932 | else |
| 933 | TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); |
| 934 | } |
| 935 | else |
| 936 | { |
| 937 | TREE_STATIC (olddecl) = TREE_STATIC (newdecl); |
| 938 | TREE_EXTERNAL (olddecl) = 0; |
| 939 | TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl); |
| 940 | } |
| 941 | |
| 942 | /* If either decl says `inline', this fn is inline, |
| 943 | unless its definition was passed already. */ |
| 944 | if (TREE_INLINE (newdecl) && DECL_INITIAL (olddecl) == 0) |
| 945 | TREE_INLINE (olddecl) = 1; |
| 946 | |
| 947 | /* If redeclaring a builtin function, and not a definition, |
| 948 | it stays built in. |
| 949 | Also preserve various other info from the definition. */ |
| 950 | if (TREE_CODE (newdecl) == FUNCTION_DECL && !new_is_definition) |
| 951 | { |
| 952 | DECL_SET_FUNCTION_CODE (newdecl, DECL_FUNCTION_CODE (olddecl)); |
| 953 | DECL_RESULT (newdecl) = DECL_RESULT (olddecl); |
| 954 | DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); |
| 955 | DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl); |
| 956 | DECL_RESULT_TYPE (newdecl) = DECL_RESULT_TYPE (olddecl); |
| 957 | DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); |
| 958 | DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl); |
| 959 | } |
| 960 | |
| 961 | /* Don't lose track of having output OLDDECL as GDB symbol. */ |
| 962 | DECL_BLOCK_SYMTAB_ADDRESS (newdecl) |
| 963 | = DECL_BLOCK_SYMTAB_ADDRESS (olddecl); |
| 964 | |
| 965 | bcopy ((char *) newdecl + sizeof (struct tree_common), |
| 966 | (char *) olddecl + sizeof (struct tree_common), |
| 967 | sizeof (struct tree_decl) - sizeof (struct tree_common)); |
| 968 | |
| 969 | return 1; |
| 970 | } |
| 971 | |
| 972 | /* New decl is completely inconsistent with the old one => |
| 973 | tell caller to replace the old one. */ |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | /* Record a decl-node X as belonging to the current lexical scope. |
| 978 | Check for errors (such as an incompatible declaration for the same |
| 979 | name already seen in the same scope). |
| 980 | |
| 981 | Returns either X or an old decl for the same name. |
| 982 | If an old decl is returned, it may have been smashed |
| 983 | to agree with what X says. */ |
| 984 | |
| 985 | tree |
| 986 | pushdecl (x) |
| 987 | tree x; |
| 988 | { |
| 989 | register tree t; |
| 990 | register tree name = DECL_NAME (x); |
| 991 | register struct binding_level *b = current_binding_level; |
| 992 | |
| 993 | if (name) |
| 994 | { |
| 995 | char *file; |
| 996 | int line; |
| 997 | |
| 998 | t = lookup_name_current_level (name); |
| 999 | if (t != 0 && t == error_mark_node) |
| 1000 | /* error_mark_node is 0 for a while during initialization! */ |
| 1001 | { |
| 1002 | t = 0; |
| 1003 | error_with_decl (x, "`%s' used prior to declaration"); |
| 1004 | } |
| 1005 | |
| 1006 | if (t != 0) |
| 1007 | { |
| 1008 | file = DECL_SOURCE_FILE (t); |
| 1009 | line = DECL_SOURCE_LINE (t); |
| 1010 | } |
| 1011 | |
| 1012 | if (t != 0 && duplicate_decls (x, t)) |
| 1013 | { |
| 1014 | /* If this decl is `static' and an implicit decl was seen previously, |
| 1015 | warn. But don't complain if -traditional, |
| 1016 | since traditional compilers don't complain. */ |
| 1017 | if (!flag_traditional && TREE_PUBLIC (name) |
| 1018 | && ! TREE_PUBLIC (x) && ! TREE_EXTERNAL (x) |
| 1019 | /* We used to warn also for explicit extern followed by static, |
| 1020 | but sometimes you need to do it that way. */ |
| 1021 | && IDENTIFIER_IMPLICIT_DECL (name) != 0) |
| 1022 | { |
| 1023 | warning ("`%s' was declared implicitly `extern' and later `static'", |
| 1024 | IDENTIFIER_POINTER (name)); |
| 1025 | warning_with_file_and_line (file, line, |
| 1026 | "previous declaration of `%s'", |
| 1027 | IDENTIFIER_POINTER (name)); |
| 1028 | } |
| 1029 | return t; |
| 1030 | } |
| 1031 | |
| 1032 | /* If declaring a type as a typedef, and the type has no known |
| 1033 | typedef name, install this TYPE_DECL as its typedef name. */ |
| 1034 | if (TREE_CODE (x) == TYPE_DECL) |
| 1035 | if (TYPE_NAME (TREE_TYPE (x)) == 0) |
| 1036 | TYPE_NAME (TREE_TYPE (x)) = x; |
| 1037 | |
| 1038 | /* Multiple external decls of the same identifier ought to match. */ |
| 1039 | |
| 1040 | if (TREE_EXTERNAL (x) && IDENTIFIER_GLOBAL_VALUE (name) != 0 |
| 1041 | && (TREE_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name)) |
| 1042 | || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name)))) |
| 1043 | { |
| 1044 | if (! comptypes (TREE_TYPE (x), |
| 1045 | TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name)))) |
| 1046 | { |
| 1047 | warning_with_decl (x, |
| 1048 | "type mismatch with previous external decl"); |
| 1049 | warning_with_decl (IDENTIFIER_GLOBAL_VALUE (name), |
| 1050 | "previous external decl of `%s'"); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | /* In PCC-compatibility mode, extern decls of vars with no current decl |
| 1055 | take effect at top level no matter where they are. */ |
| 1056 | if (flag_traditional && TREE_EXTERNAL (x) |
| 1057 | && lookup_name (name) == 0) |
| 1058 | b = global_binding_level; |
| 1059 | |
| 1060 | /* This name is new in its binding level. |
| 1061 | Install the new declaration and return it. */ |
| 1062 | if (b == global_binding_level) |
| 1063 | { |
| 1064 | /* Install a global value. */ |
| 1065 | |
| 1066 | /* If the first global decl has external linkage, |
| 1067 | warn if we later see static one. */ |
| 1068 | if (IDENTIFIER_GLOBAL_VALUE (name) == 0 && TREE_PUBLIC (x)) |
| 1069 | TREE_PUBLIC (name) = 1; |
| 1070 | |
| 1071 | IDENTIFIER_GLOBAL_VALUE (name) = x; |
| 1072 | |
| 1073 | /* Don't forget if the function was used via an implicit decl. */ |
| 1074 | if (IDENTIFIER_IMPLICIT_DECL (name) |
| 1075 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) |
| 1076 | TREE_USED (x) = 1, TREE_USED (name) = 1; |
| 1077 | |
| 1078 | /* Don't forget if its address was taken in that way. */ |
| 1079 | if (IDENTIFIER_IMPLICIT_DECL (name) |
| 1080 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) |
| 1081 | TREE_ADDRESSABLE (x) = 1; |
| 1082 | |
| 1083 | /* Warn about mismatches against previous implicit decl. */ |
| 1084 | if (IDENTIFIER_IMPLICIT_DECL (name) != 0 |
| 1085 | /* If this real decl matches the implicit, don't complain. */ |
| 1086 | && ! (TREE_CODE (x) == FUNCTION_DECL |
| 1087 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) |
| 1088 | warning ("`%s' was previously implicitly declared to return `int'", |
| 1089 | IDENTIFIER_POINTER (name)); |
| 1090 | |
| 1091 | /* If this decl is `static' and an `extern' was seen previously, |
| 1092 | that is erroneous. */ |
| 1093 | if (TREE_PUBLIC (name) |
| 1094 | && ! TREE_PUBLIC (x) && ! TREE_EXTERNAL (x)) |
| 1095 | { |
| 1096 | if (IDENTIFIER_IMPLICIT_DECL (name)) |
| 1097 | warning ("`%s' was declared implicitly `extern' and later `static'", |
| 1098 | IDENTIFIER_POINTER (name)); |
| 1099 | else |
| 1100 | warning ("`%s' was declared `extern' and later `static'", |
| 1101 | IDENTIFIER_POINTER (name)); |
| 1102 | } |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | /* Here to install a non-global value. */ |
| 1107 | tree oldlocal = IDENTIFIER_LOCAL_VALUE (name); |
| 1108 | tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name); |
| 1109 | IDENTIFIER_LOCAL_VALUE (name) = x; |
| 1110 | |
| 1111 | /* If this is an extern function declaration, see if we |
| 1112 | have a global definition for the function. */ |
| 1113 | if (oldlocal == 0 |
| 1114 | && oldglobal != 0 |
| 1115 | && TREE_CODE (x) == FUNCTION_DECL |
| 1116 | && TREE_CODE (oldglobal) == FUNCTION_DECL) |
| 1117 | { |
| 1118 | /* We have one. Their types must agree. */ |
| 1119 | if (! comptypes (TREE_TYPE (x), |
| 1120 | TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name)))) |
| 1121 | warning_with_decl (x, "local declaration of `%s' doesn't match global one"); |
| 1122 | /* If the global one is inline, make the local one inline. */ |
| 1123 | else if (TREE_INLINE (oldglobal) |
| 1124 | || DECL_FUNCTION_CODE (oldglobal) != NOT_BUILT_IN |
| 1125 | || (TYPE_ARG_TYPES (TREE_TYPE (oldglobal)) != 0 |
| 1126 | && TYPE_ARG_TYPES (TREE_TYPE (x)) == 0)) |
| 1127 | IDENTIFIER_LOCAL_VALUE (name) = oldglobal; |
| 1128 | } |
| 1129 | /* If we have a local external declaration, |
| 1130 | and no file-scope declaration has yet been seen, |
| 1131 | then if we later have a file-scope decl it must not be static. */ |
| 1132 | if (oldlocal == 0 |
| 1133 | && oldglobal == 0 |
| 1134 | && TREE_EXTERNAL (x) |
| 1135 | && TREE_PUBLIC (x)) |
| 1136 | { |
| 1137 | TREE_PUBLIC (name) = 1; |
| 1138 | } |
| 1139 | |
| 1140 | /* Warn if shadowing an argument at the top level of the body. */ |
| 1141 | if (oldlocal != 0 && !TREE_EXTERNAL (x) |
| 1142 | && TREE_CODE (oldlocal) == PARM_DECL |
| 1143 | && TREE_CODE (x) != PARM_DECL |
| 1144 | && current_binding_level->level_chain->parm_flag) |
| 1145 | warning ("declaration of `%s' shadows a parameter", |
| 1146 | IDENTIFIER_POINTER (name)); |
| 1147 | |
| 1148 | /* Maybe warn if shadowing something else. */ |
| 1149 | else if (warn_shadow && !TREE_EXTERNAL (x) |
| 1150 | /* No shadow warnings for vars made for inlining. */ |
| 1151 | && !TREE_INLINE (x)) |
| 1152 | { |
| 1153 | char *warnstring = 0; |
| 1154 | |
| 1155 | if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL) |
| 1156 | warnstring = "declaration of `%s' shadows a parameter"; |
| 1157 | else if (oldlocal != 0) |
| 1158 | warnstring = "declaration of `%s' shadows previous local"; |
| 1159 | else if (IDENTIFIER_GLOBAL_VALUE (name) != 0) |
| 1160 | warnstring = "declaration of `%s' shadows global declaration"; |
| 1161 | |
| 1162 | if (warnstring) |
| 1163 | warning (warnstring, IDENTIFIER_POINTER (name)); |
| 1164 | } |
| 1165 | |
| 1166 | /* If storing a local value, there may already be one (inherited). |
| 1167 | If so, record it for restoration when this binding level ends. */ |
| 1168 | if (oldlocal != 0) |
| 1169 | b->shadowed = tree_cons (name, oldlocal, b->shadowed); |
| 1170 | } |
| 1171 | |
| 1172 | /* Keep count of variables in this level with incomplete type. */ |
| 1173 | if (TYPE_SIZE (TREE_TYPE (x)) == 0) |
| 1174 | ++b->n_incomplete; |
| 1175 | } |
| 1176 | |
| 1177 | /* Put decls on list in reverse order. |
| 1178 | We will reverse them later if necessary. */ |
| 1179 | TREE_CHAIN (x) = b->names; |
| 1180 | b->names = x; |
| 1181 | |
| 1182 | return x; |
| 1183 | } |
| 1184 | \f |
| 1185 | /* Generate an implicit declaration for identifier FUNCTIONID |
| 1186 | as a function of type int (). Print a warning if appropriate. */ |
| 1187 | |
| 1188 | tree |
| 1189 | implicitly_declare (functionid) |
| 1190 | tree functionid; |
| 1191 | { |
| 1192 | register tree decl; |
| 1193 | |
| 1194 | /* Save the decl permanently so we can warn if definition follows. */ |
| 1195 | #if 0 /* A temporary implicit decl causes a crash in pushdecl. |
| 1196 | In 1.38, fix pushdecl. */ |
| 1197 | if (flag_traditional || !warn_implicit |
| 1198 | || current_binding_level == global_binding_level) |
| 1199 | #endif |
| 1200 | end_temporary_allocation (); |
| 1201 | |
| 1202 | /* We used to reuse an old implicit decl here, |
| 1203 | but this loses with inline functions because it can clobber |
| 1204 | the saved decl chains. */ |
| 1205 | /* if (IDENTIFIER_IMPLICIT_DECL (functionid) != 0) |
| 1206 | decl = IDENTIFIER_IMPLICIT_DECL (functionid); |
| 1207 | else */ |
| 1208 | decl = build_decl (FUNCTION_DECL, functionid, default_function_type); |
| 1209 | |
| 1210 | TREE_EXTERNAL (decl) = 1; |
| 1211 | TREE_PUBLIC (decl) = 1; |
| 1212 | |
| 1213 | /* ANSI standard says implicit declarations are in the innermost block. |
| 1214 | So we record the decl in the standard fashion. |
| 1215 | If flag_traditional is set, pushdecl does it top-level. */ |
| 1216 | pushdecl (decl); |
| 1217 | rest_of_decl_compilation (decl, 0, 0, 0); |
| 1218 | |
| 1219 | if (warn_implicit |
| 1220 | /* Only one warning per identifier. */ |
| 1221 | && IDENTIFIER_IMPLICIT_DECL (functionid) == 0) |
| 1222 | warning ("implicit declaration of function `%s'", |
| 1223 | IDENTIFIER_POINTER (functionid)); |
| 1224 | |
| 1225 | IDENTIFIER_IMPLICIT_DECL (functionid) = decl; |
| 1226 | |
| 1227 | #if 0 |
| 1228 | if (flag_traditional || ! warn_implicit |
| 1229 | || current_binding_level == global_binding_level) |
| 1230 | #endif |
| 1231 | resume_temporary_allocation (); |
| 1232 | |
| 1233 | return decl; |
| 1234 | } |
| 1235 | |
| 1236 | /* Return zero if the declaration NEWDECL is valid |
| 1237 | when the declaration OLDDECL (assumed to be for the same name) |
| 1238 | has already been seen. |
| 1239 | Otherwise return an error message format string with a %s |
| 1240 | where the identifier should go. */ |
| 1241 | |
| 1242 | static char * |
| 1243 | redeclaration_error_message (newdecl, olddecl) |
| 1244 | tree newdecl, olddecl; |
| 1245 | { |
| 1246 | if (TREE_CODE (newdecl) == TYPE_DECL) |
| 1247 | { |
| 1248 | if (flag_traditional && TREE_TYPE (newdecl) == TREE_TYPE (olddecl)) |
| 1249 | return 0; |
| 1250 | return "redefinition of `%s'"; |
| 1251 | } |
| 1252 | else if (TREE_CODE (newdecl) == FUNCTION_DECL) |
| 1253 | { |
| 1254 | /* Declarations of functions can insist on internal linkage |
| 1255 | but they can't be inconsistent with internal linkage, |
| 1256 | so there can be no error on that account. |
| 1257 | However defining the same name twice is no good. */ |
| 1258 | if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0 |
| 1259 | /* However, defining once as extern inline and a second |
| 1260 | time in another way is ok. */ |
| 1261 | && !(TREE_INLINE (olddecl) && TREE_EXTERNAL (olddecl) |
| 1262 | && !(TREE_INLINE (newdecl) && TREE_EXTERNAL (newdecl)))) |
| 1263 | return "redefinition of `%s'"; |
| 1264 | return 0; |
| 1265 | } |
| 1266 | else if (current_binding_level == global_binding_level) |
| 1267 | { |
| 1268 | /* Objects declared at top level: */ |
| 1269 | /* If at least one is a reference, it's ok. */ |
| 1270 | if (TREE_EXTERNAL (newdecl) || TREE_EXTERNAL (olddecl)) |
| 1271 | return 0; |
| 1272 | /* Reject two definitions. */ |
| 1273 | if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0) |
| 1274 | return "redefinition of `%s'"; |
| 1275 | /* Now we have two tentative defs, or one tentative and one real def. */ |
| 1276 | /* Insist that the linkage match. */ |
| 1277 | if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl)) |
| 1278 | return "conflicting declarations of `%s'"; |
| 1279 | return 0; |
| 1280 | } |
| 1281 | else |
| 1282 | { |
| 1283 | /* Objects declared with block scope: */ |
| 1284 | /* Reject two definitions, and reject a definition |
| 1285 | together with an external reference. */ |
| 1286 | if (!(TREE_EXTERNAL (newdecl) && TREE_EXTERNAL (olddecl))) |
| 1287 | return "redeclaration of `%s'"; |
| 1288 | return 0; |
| 1289 | } |
| 1290 | } |
| 1291 | \f |
| 1292 | /* Get the LABEL_DECL corresponding to identifier ID as a label. |
| 1293 | Create one if none exists so far for the current function. |
| 1294 | This function is called for both label definitions and label references. */ |
| 1295 | |
| 1296 | tree |
| 1297 | lookup_label (id) |
| 1298 | tree id; |
| 1299 | { |
| 1300 | register tree decl = IDENTIFIER_LABEL_VALUE (id); |
| 1301 | |
| 1302 | if (decl != 0) |
| 1303 | return decl; |
| 1304 | |
| 1305 | decl = build_decl (LABEL_DECL, id, NULL_TREE); |
| 1306 | DECL_MODE (decl) = VOIDmode; |
| 1307 | /* Mark that the label's definition has not been seen. */ |
| 1308 | DECL_SOURCE_LINE (decl) = 0; |
| 1309 | |
| 1310 | IDENTIFIER_LABEL_VALUE (id) = decl; |
| 1311 | |
| 1312 | named_labels |
| 1313 | = tree_cons (NULL_TREE, decl, named_labels); |
| 1314 | |
| 1315 | return decl; |
| 1316 | } |
| 1317 | |
| 1318 | /* Define a label, specifying the location in the source file. |
| 1319 | Return the LABEL_DECL node for the label, if the definition is valid. |
| 1320 | Otherwise return 0. */ |
| 1321 | |
| 1322 | tree |
| 1323 | define_label (filename, line, name) |
| 1324 | char *filename; |
| 1325 | int line; |
| 1326 | tree name; |
| 1327 | { |
| 1328 | tree decl = lookup_label (name); |
| 1329 | if (DECL_SOURCE_LINE (decl) != 0) |
| 1330 | { |
| 1331 | error_with_decl (decl, "duplicate label `%s'"); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | else |
| 1335 | { |
| 1336 | /* Mark label as having been defined. */ |
| 1337 | DECL_SOURCE_FILE (decl) = filename; |
| 1338 | DECL_SOURCE_LINE (decl) = line; |
| 1339 | return decl; |
| 1340 | } |
| 1341 | } |
| 1342 | \f |
| 1343 | /* Return the list of declarations of the current level. |
| 1344 | Note that this list is in reverse order unless/until |
| 1345 | you nreverse it; and when you do nreverse it, you must |
| 1346 | store the result back using `storedecls' or you will lose. */ |
| 1347 | |
| 1348 | tree |
| 1349 | getdecls () |
| 1350 | { |
| 1351 | return current_binding_level->names; |
| 1352 | } |
| 1353 | |
| 1354 | /* Return the list of type-tags (for structs, etc) of the current level. */ |
| 1355 | |
| 1356 | tree |
| 1357 | gettags () |
| 1358 | { |
| 1359 | return current_binding_level->tags; |
| 1360 | } |
| 1361 | |
| 1362 | /* Store the list of declarations of the current level. |
| 1363 | This is done for the parameter declarations of a function being defined, |
| 1364 | after they are modified in the light of any missing parameters. */ |
| 1365 | |
| 1366 | static void |
| 1367 | storedecls (decls) |
| 1368 | tree decls; |
| 1369 | { |
| 1370 | current_binding_level->names = decls; |
| 1371 | } |
| 1372 | |
| 1373 | /* Similarly, store the list of tags of the current level. */ |
| 1374 | |
| 1375 | static void |
| 1376 | storetags (tags) |
| 1377 | tree tags; |
| 1378 | { |
| 1379 | current_binding_level->tags = tags; |
| 1380 | } |
| 1381 | \f |
| 1382 | /* Given NAME, an IDENTIFIER_NODE, |
| 1383 | return the structure (or union or enum) definition for that name. |
| 1384 | Searches binding levels from BINDING_LEVEL up to the global level. |
| 1385 | If THISLEVEL_ONLY is nonzero, searches only the specified context |
| 1386 | (but skips any tag-transparent contexts to find one that is |
| 1387 | meaningful for tags). |
| 1388 | FORM says which kind of type the caller wants; |
| 1389 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. |
| 1390 | If the wrong kind of type is found, an error is reported. */ |
| 1391 | |
| 1392 | static tree |
| 1393 | lookup_tag (form, name, binding_level, thislevel_only) |
| 1394 | enum tree_code form; |
| 1395 | struct binding_level *binding_level; |
| 1396 | tree name; |
| 1397 | int thislevel_only; |
| 1398 | { |
| 1399 | register struct binding_level *level; |
| 1400 | |
| 1401 | for (level = binding_level; level; level = level->level_chain) |
| 1402 | { |
| 1403 | register tree tail; |
| 1404 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) |
| 1405 | { |
| 1406 | if (TREE_PURPOSE (tail) == name) |
| 1407 | { |
| 1408 | if (TREE_CODE (TREE_VALUE (tail)) != form) |
| 1409 | { |
| 1410 | /* Definition isn't the kind we were looking for. */ |
| 1411 | error ("`%s' defined as wrong kind of tag", |
| 1412 | IDENTIFIER_POINTER (name)); |
| 1413 | } |
| 1414 | return TREE_VALUE (tail); |
| 1415 | } |
| 1416 | } |
| 1417 | if (thislevel_only && ! level->tag_transparent) |
| 1418 | return NULL_TREE; |
| 1419 | } |
| 1420 | return NULL_TREE; |
| 1421 | } |
| 1422 | |
| 1423 | /* Given a type, find the tag that was defined for it and return the tag name. |
| 1424 | Otherwise return 0. However, the value can never be 0 |
| 1425 | in the cases in which this is used. */ |
| 1426 | |
| 1427 | static tree |
| 1428 | lookup_tag_reverse (type) |
| 1429 | tree type; |
| 1430 | { |
| 1431 | register struct binding_level *level; |
| 1432 | |
| 1433 | for (level = current_binding_level; level; level = level->level_chain) |
| 1434 | { |
| 1435 | register tree tail; |
| 1436 | for (tail = level->tags; tail; tail = TREE_CHAIN (tail)) |
| 1437 | { |
| 1438 | if (TREE_VALUE (tail) == type) |
| 1439 | return TREE_PURPOSE (tail); |
| 1440 | } |
| 1441 | } |
| 1442 | return NULL_TREE; |
| 1443 | } |
| 1444 | \f |
| 1445 | /* Look up NAME in the current binding level and its superiors |
| 1446 | in the namespace of variables, functions and typedefs. |
| 1447 | Return a ..._DECL node of some kind representing its definition, |
| 1448 | or return 0 if it is undefined. */ |
| 1449 | |
| 1450 | tree |
| 1451 | lookup_name (name) |
| 1452 | tree name; |
| 1453 | { |
| 1454 | register tree val; |
| 1455 | if (current_binding_level != global_binding_level |
| 1456 | && IDENTIFIER_LOCAL_VALUE (name)) |
| 1457 | val = IDENTIFIER_LOCAL_VALUE (name); |
| 1458 | else |
| 1459 | val = IDENTIFIER_GLOBAL_VALUE (name); |
| 1460 | if (val && TREE_TYPE (val) == error_mark_node) |
| 1461 | return error_mark_node; |
| 1462 | return val; |
| 1463 | } |
| 1464 | |
| 1465 | /* Similar to `lookup_name' but look only at current binding level. */ |
| 1466 | |
| 1467 | static tree |
| 1468 | lookup_name_current_level (name) |
| 1469 | tree name; |
| 1470 | { |
| 1471 | register tree t; |
| 1472 | |
| 1473 | if (current_binding_level == global_binding_level) |
| 1474 | return IDENTIFIER_GLOBAL_VALUE (name); |
| 1475 | |
| 1476 | if (IDENTIFIER_LOCAL_VALUE (name) == 0) |
| 1477 | return 0; |
| 1478 | |
| 1479 | for (t = current_binding_level->names; t; t = TREE_CHAIN (t)) |
| 1480 | if (DECL_NAME (t) == name) |
| 1481 | break; |
| 1482 | |
| 1483 | return t; |
| 1484 | } |
| 1485 | \f |
| 1486 | /* Create the predefined scalar types of C, |
| 1487 | and some nodes representing standard constants (0, 1, (void *)0). |
| 1488 | Initialize the global binding level. |
| 1489 | Make definitions for built-in primitive functions. */ |
| 1490 | |
| 1491 | void |
| 1492 | init_decl_processing () |
| 1493 | { |
| 1494 | register tree endlink; |
| 1495 | |
| 1496 | /* Make identifier nodes long enough for the language-specific slots. */ |
| 1497 | set_identifier_size (sizeof (struct lang_identifier)); |
| 1498 | |
| 1499 | current_function_decl = NULL; |
| 1500 | named_labels = NULL; |
| 1501 | current_binding_level = NULL_BINDING_LEVEL; |
| 1502 | free_binding_level = NULL_BINDING_LEVEL; |
| 1503 | pushlevel (0); /* make the binding_level structure for global names */ |
| 1504 | global_binding_level = current_binding_level; |
| 1505 | |
| 1506 | value_identifier = get_identifier ("<value>"); |
| 1507 | |
| 1508 | /* Define `int' and `char' first so that dbx will output them first. */ |
| 1509 | |
| 1510 | integer_type_node = make_signed_type (INT_TYPE_SIZE); |
| 1511 | pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT], |
| 1512 | integer_type_node)); |
| 1513 | |
| 1514 | /* Define `char', which is like either `signed char' or `unsigned char' |
| 1515 | but not the same as either. */ |
| 1516 | |
| 1517 | char_type_node = |
| 1518 | (flag_signed_char |
| 1519 | ? make_signed_type (CHAR_TYPE_SIZE) |
| 1520 | : make_unsigned_type (CHAR_TYPE_SIZE)); |
| 1521 | pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), |
| 1522 | char_type_node)); |
| 1523 | |
| 1524 | long_integer_type_node = make_signed_type (LONG_TYPE_SIZE); |
| 1525 | pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"), |
| 1526 | long_integer_type_node)); |
| 1527 | |
| 1528 | unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE); |
| 1529 | pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"), |
| 1530 | unsigned_type_node)); |
| 1531 | |
| 1532 | long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE); |
| 1533 | pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"), |
| 1534 | long_unsigned_type_node)); |
| 1535 | |
| 1536 | /* `unsigned long' or `unsigned int' is the standard type for sizeof. |
| 1537 | Traditionally, use a signed type. */ |
| 1538 | if (INT_TYPE_SIZE != LONG_TYPE_SIZE) |
| 1539 | sizetype = flag_traditional ? long_integer_type_node : long_unsigned_type_node; |
| 1540 | else |
| 1541 | sizetype = flag_traditional ? integer_type_node : unsigned_type_node; |
| 1542 | |
| 1543 | TREE_TYPE (TYPE_SIZE (integer_type_node)) = sizetype; |
| 1544 | TREE_TYPE (TYPE_SIZE (char_type_node)) = sizetype; |
| 1545 | TREE_TYPE (TYPE_SIZE (unsigned_type_node)) = sizetype; |
| 1546 | TREE_TYPE (TYPE_SIZE (long_unsigned_type_node)) = sizetype; |
| 1547 | TREE_TYPE (TYPE_SIZE (long_integer_type_node)) = sizetype; |
| 1548 | |
| 1549 | error_mark_node = make_node (ERROR_MARK); |
| 1550 | TREE_TYPE (error_mark_node) = error_mark_node; |
| 1551 | |
| 1552 | short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE); |
| 1553 | pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"), |
| 1554 | short_integer_type_node)); |
| 1555 | |
| 1556 | long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE); |
| 1557 | pushdecl (build_decl (TYPE_DECL, get_identifier ("long long int"), |
| 1558 | long_long_integer_type_node)); |
| 1559 | |
| 1560 | short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE); |
| 1561 | pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"), |
| 1562 | short_unsigned_type_node)); |
| 1563 | |
| 1564 | long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE); |
| 1565 | pushdecl (build_decl (TYPE_DECL, get_identifier ("long long unsigned int"), |
| 1566 | long_long_unsigned_type_node)); |
| 1567 | |
| 1568 | /* Define both `signed char' and `unsigned char'. */ |
| 1569 | signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE); |
| 1570 | pushdecl (build_decl (TYPE_DECL, get_identifier ("signed char"), |
| 1571 | signed_char_type_node)); |
| 1572 | |
| 1573 | unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE); |
| 1574 | pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"), |
| 1575 | unsigned_char_type_node)); |
| 1576 | |
| 1577 | float_type_node = make_node (REAL_TYPE); |
| 1578 | TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE; |
| 1579 | pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT], |
| 1580 | float_type_node)); |
| 1581 | layout_type (float_type_node); |
| 1582 | |
| 1583 | double_type_node = make_node (REAL_TYPE); |
| 1584 | TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE; |
| 1585 | pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE], |
| 1586 | double_type_node)); |
| 1587 | layout_type (double_type_node); |
| 1588 | |
| 1589 | long_double_type_node = make_node (REAL_TYPE); |
| 1590 | TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE; |
| 1591 | pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"), |
| 1592 | long_double_type_node)); |
| 1593 | layout_type (long_double_type_node); |
| 1594 | |
| 1595 | integer_zero_node = build_int_2 (0, 0); |
| 1596 | TREE_TYPE (integer_zero_node) = integer_type_node; |
| 1597 | integer_one_node = build_int_2 (1, 0); |
| 1598 | TREE_TYPE (integer_one_node) = integer_type_node; |
| 1599 | |
| 1600 | size_zero_node = build_int_2 (0, 0); |
| 1601 | TREE_TYPE (size_zero_node) = sizetype; |
| 1602 | size_one_node = build_int_2 (1, 0); |
| 1603 | TREE_TYPE (size_one_node) = sizetype; |
| 1604 | |
| 1605 | void_type_node = make_node (VOID_TYPE); |
| 1606 | pushdecl (build_decl (TYPE_DECL, |
| 1607 | ridpointers[(int) RID_VOID], void_type_node)); |
| 1608 | layout_type (void_type_node); /* Uses integer_zero_node */ |
| 1609 | |
| 1610 | null_pointer_node = build_int_2 (0, 0); |
| 1611 | TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node); |
| 1612 | layout_type (TREE_TYPE (null_pointer_node)); |
| 1613 | |
| 1614 | string_type_node = build_pointer_type (char_type_node); |
| 1615 | |
| 1616 | /* make a type for arrays of 256 characters. |
| 1617 | 256 is picked randomly because we have a type for integers from 0 to 255. |
| 1618 | With luck nothing will ever really depend on the length of this |
| 1619 | array type. */ |
| 1620 | char_array_type_node |
| 1621 | = build_array_type (char_type_node, unsigned_char_type_node); |
| 1622 | /* Likewise for arrays of ints. */ |
| 1623 | int_array_type_node |
| 1624 | = build_array_type (integer_type_node, unsigned_char_type_node); |
| 1625 | |
| 1626 | default_function_type |
| 1627 | = build_function_type (integer_type_node, NULL_TREE); |
| 1628 | |
| 1629 | ptr_type_node = build_pointer_type (void_type_node); |
| 1630 | endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE); |
| 1631 | |
| 1632 | double_ftype_double |
| 1633 | = build_function_type (double_type_node, |
| 1634 | tree_cons (NULL_TREE, double_type_node, endlink)); |
| 1635 | |
| 1636 | double_ftype_double_double |
| 1637 | = build_function_type (double_type_node, |
| 1638 | tree_cons (NULL_TREE, double_type_node, |
| 1639 | tree_cons (NULL_TREE, |
| 1640 | double_type_node, endlink))); |
| 1641 | |
| 1642 | int_ftype_int |
| 1643 | = build_function_type (integer_type_node, |
| 1644 | tree_cons (NULL_TREE, integer_type_node, endlink)); |
| 1645 | |
| 1646 | long_ftype_long |
| 1647 | = build_function_type (long_integer_type_node, |
| 1648 | tree_cons (NULL_TREE, |
| 1649 | long_integer_type_node, endlink)); |
| 1650 | |
| 1651 | void_ftype_ptr_ptr_int |
| 1652 | = build_function_type (void_type_node, |
| 1653 | tree_cons (NULL_TREE, ptr_type_node, |
| 1654 | tree_cons (NULL_TREE, ptr_type_node, |
| 1655 | tree_cons (NULL_TREE, |
| 1656 | integer_type_node, |
| 1657 | endlink)))); |
| 1658 | |
| 1659 | int_ftype_ptr_ptr_int |
| 1660 | = build_function_type (integer_type_node, |
| 1661 | tree_cons (NULL_TREE, ptr_type_node, |
| 1662 | tree_cons (NULL_TREE, ptr_type_node, |
| 1663 | tree_cons (NULL_TREE, |
| 1664 | integer_type_node, |
| 1665 | endlink)))); |
| 1666 | |
| 1667 | void_ftype_ptr_int_int |
| 1668 | = build_function_type (void_type_node, |
| 1669 | tree_cons (NULL_TREE, ptr_type_node, |
| 1670 | tree_cons (NULL_TREE, integer_type_node, |
| 1671 | tree_cons (NULL_TREE, |
| 1672 | integer_type_node, |
| 1673 | endlink)))); |
| 1674 | |
| 1675 | builtin_function ("__builtin_alloca", |
| 1676 | build_function_type (ptr_type_node, |
| 1677 | tree_cons (NULL_TREE, |
| 1678 | integer_type_node, |
| 1679 | endlink)), |
| 1680 | BUILT_IN_ALLOCA); |
| 1681 | |
| 1682 | builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS); |
| 1683 | builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS); |
| 1684 | builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS); |
| 1685 | builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS); |
| 1686 | builtin_function ("__builtin_saveregs", default_function_type, |
| 1687 | BUILT_IN_SAVEREGS); |
| 1688 | builtin_function ("__builtin_classify_type", default_function_type, |
| 1689 | BUILT_IN_CLASSIFY_TYPE); |
| 1690 | builtin_function ("__builtin_next_arg", |
| 1691 | build_function_type (ptr_type_node, endlink), |
| 1692 | BUILT_IN_NEXT_ARG); |
| 1693 | #if 0 |
| 1694 | /* Support for these has not been written in either expand_builtin |
| 1695 | or build_function_call. */ |
| 1696 | builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV); |
| 1697 | builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV); |
| 1698 | builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR); |
| 1699 | builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL); |
| 1700 | builtin_function ("__builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD); |
| 1701 | builtin_function ("__builtin_frem", double_ftype_double_double, BUILT_IN_FREM); |
| 1702 | builtin_function ("__builtin_memcpy", void_ftype_ptr_ptr_int, BUILT_IN_MEMCPY); |
| 1703 | builtin_function ("__builtin_memcmp", int_ftype_ptr_ptr_int, BUILT_IN_MEMCMP); |
| 1704 | builtin_function ("__builtin_memset", void_ftype_ptr_int_int, BUILT_IN_MEMSET); |
| 1705 | builtin_function ("__builtin_fsqrt", double_ftype_double, BUILT_IN_FSQRT); |
| 1706 | builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP); |
| 1707 | builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN); |
| 1708 | #endif |
| 1709 | |
| 1710 | start_identifier_warnings (); |
| 1711 | } |
| 1712 | |
| 1713 | /* Make a definition for a builtin function named NAME and whose data type |
| 1714 | is TYPE. TYPE should be a function type with argument types. |
| 1715 | FUNCTION_CODE tells later passes how to compile calls to this function. |
| 1716 | See tree.h for its possible values. */ |
| 1717 | |
| 1718 | static void |
| 1719 | builtin_function (name, type, function_code) |
| 1720 | char *name; |
| 1721 | tree type; |
| 1722 | enum built_in_function function_code; |
| 1723 | { |
| 1724 | tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type); |
| 1725 | TREE_EXTERNAL (decl) = 1; |
| 1726 | TREE_PUBLIC (decl) = 1; |
| 1727 | make_decl_rtl (decl, 0, 1); |
| 1728 | pushdecl (decl); |
| 1729 | DECL_SET_FUNCTION_CODE (decl, function_code); |
| 1730 | } |
| 1731 | \f |
| 1732 | /* Called when a declaration is seen that contains no names to declare. |
| 1733 | If its type is a reference to a structure, union or enum inherited |
| 1734 | from a containing scope, shadow that tag name for the current scope |
| 1735 | with a forward reference. |
| 1736 | If its type defines a new named structure or union |
| 1737 | or defines an enum, it is valid but we need not do anything here. |
| 1738 | Otherwise, it is an error. */ |
| 1739 | |
| 1740 | void |
| 1741 | shadow_tag (declspecs) |
| 1742 | tree declspecs; |
| 1743 | { |
| 1744 | int found_tag = 0; |
| 1745 | int warned = 0; |
| 1746 | register tree link; |
| 1747 | |
| 1748 | for (link = declspecs; link; link = TREE_CHAIN (link)) |
| 1749 | { |
| 1750 | register tree value = TREE_VALUE (link); |
| 1751 | register enum tree_code code = TREE_CODE (value); |
| 1752 | int ok = 0; |
| 1753 | |
| 1754 | if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE) |
| 1755 | /* Used to test also that TYPE_SIZE (value) != 0. |
| 1756 | That caused warning for `struct foo;' at top level in the file. */ |
| 1757 | { |
| 1758 | register tree name = lookup_tag_reverse (value); |
| 1759 | register tree t = lookup_tag (code, name, current_binding_level, 1); |
| 1760 | |
| 1761 | if (t == 0) |
| 1762 | { |
| 1763 | t = make_node (code); |
| 1764 | pushtag (name, t); |
| 1765 | ok = 1; |
| 1766 | } |
| 1767 | else if (name != 0 || code == ENUMERAL_TYPE) |
| 1768 | ok = 1; |
| 1769 | } |
| 1770 | |
| 1771 | if (ok) |
| 1772 | found_tag++; |
| 1773 | else |
| 1774 | { |
| 1775 | if (!warned) |
| 1776 | warning ("useless keyword or type name in declaration"); |
| 1777 | warned = 1; |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | if (!warned) |
| 1782 | { |
| 1783 | if (found_tag > 1) |
| 1784 | warning ("multiple types in one declaration"); |
| 1785 | if (found_tag == 0) |
| 1786 | warning ("empty declaration"); |
| 1787 | } |
| 1788 | } |
| 1789 | \f |
| 1790 | /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ |
| 1791 | |
| 1792 | tree |
| 1793 | groktypename (typename) |
| 1794 | tree typename; |
| 1795 | { |
| 1796 | if (TREE_CODE (typename) != TREE_LIST) |
| 1797 | return typename; |
| 1798 | return grokdeclarator (TREE_VALUE (typename), |
| 1799 | TREE_PURPOSE (typename), |
| 1800 | TYPENAME, 0); |
| 1801 | } |
| 1802 | |
| 1803 | /* Decode a declarator in an ordinary declaration or data definition. |
| 1804 | This is called as soon as the type information and variable name |
| 1805 | have been parsed, before parsing the initializer if any. |
| 1806 | Here we create the ..._DECL node, fill in its type, |
| 1807 | and put it on the list of decls for the current context. |
| 1808 | The ..._DECL node is returned as the value. |
| 1809 | |
| 1810 | Exception: for arrays where the length is not specified, |
| 1811 | the type is left null, to be filled in by `finish_decl'. |
| 1812 | |
| 1813 | Function definitions do not come here; they go to start_function |
| 1814 | instead. However, external and forward declarations of functions |
| 1815 | do go through here. Structure field declarations are done by |
| 1816 | grokfield and not through here. */ |
| 1817 | |
| 1818 | /* Set this to zero to debug not using the temporary obstack |
| 1819 | to parse initializers. */ |
| 1820 | int debug_temp_inits = 1; |
| 1821 | |
| 1822 | tree |
| 1823 | start_decl (declarator, declspecs, initialized) |
| 1824 | tree declspecs, declarator; |
| 1825 | int initialized; |
| 1826 | { |
| 1827 | register tree decl = grokdeclarator (declarator, declspecs, |
| 1828 | NORMAL, initialized); |
| 1829 | register tree tem; |
| 1830 | int init_written = initialized; |
| 1831 | |
| 1832 | if (initialized) |
| 1833 | /* Is it valid for this decl to have an initializer at all? |
| 1834 | If not, set INITIALIZED to zero, which will indirectly |
| 1835 | tell `finish_decl' to ignore the initializer once it is parsed. */ |
| 1836 | switch (TREE_CODE (decl)) |
| 1837 | { |
| 1838 | case TYPE_DECL: |
| 1839 | /* typedef foo = bar means give foo the same type as bar. |
| 1840 | We haven't parsed bar yet, so `finish_decl' will fix that up. |
| 1841 | Any other case of an initialization in a TYPE_DECL is an error. */ |
| 1842 | if (pedantic || list_length (declspecs) > 1) |
| 1843 | { |
| 1844 | error ("typedef `%s' is initialized", |
| 1845 | IDENTIFIER_POINTER (DECL_NAME (decl))); |
| 1846 | initialized = 0; |
| 1847 | } |
| 1848 | break; |
| 1849 | |
| 1850 | case FUNCTION_DECL: |
| 1851 | error ("function `%s' is initialized like a variable", |
| 1852 | IDENTIFIER_POINTER (DECL_NAME (decl))); |
| 1853 | initialized = 0; |
| 1854 | break; |
| 1855 | |
| 1856 | default: |
| 1857 | /* Don't allow initializations for incomplete types |
| 1858 | except for arrays which might be completed by the initialization. */ |
| 1859 | if (TYPE_SIZE (TREE_TYPE (decl)) != 0) |
| 1860 | ; /* A complete type is ok. */ |
| 1861 | else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE) |
| 1862 | { |
| 1863 | error ("variable `%s' has initializer but incomplete type", |
| 1864 | IDENTIFIER_POINTER (DECL_NAME (decl))); |
| 1865 | initialized = 0; |
| 1866 | } |
| 1867 | else if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))) == 0) |
| 1868 | { |
| 1869 | error ("elements of array `%s' have incomplete type", |
| 1870 | IDENTIFIER_POINTER (DECL_NAME (decl))); |
| 1871 | initialized = 0; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | if (initialized) |
| 1876 | { |
| 1877 | #if 0 /* Seems redundant. */ |
| 1878 | if (current_binding_level != global_binding_level |
| 1879 | && TREE_EXTERNAL (decl) |
| 1880 | && TREE_CODE (decl) != FUNCTION_DECL) |
| 1881 | warning ("declaration of `%s' has `extern' and is initialized", |
| 1882 | IDENTIFIER_POINTER (DECL_NAME (decl))); |
| 1883 | #endif |
| 1884 | TREE_EXTERNAL (decl) = 0; |
| 1885 | if (current_binding_level == global_binding_level) |
| 1886 | TREE_STATIC (decl) = 1; |
| 1887 | |
| 1888 | /* Tell `pushdecl' this is an initialized decl |
| 1889 | even though we don't yet have the initializer expression. |
| 1890 | Also tell `finish_decl' it may store the real initializer. */ |
| 1891 | DECL_INITIAL (decl) = error_mark_node; |
| 1892 | } |
| 1893 | |
| 1894 | /* Add this decl to the current binding level. |
| 1895 | TEM may equal DECL or it may be a previous decl of the same name. */ |
| 1896 | tem = pushdecl (decl); |
| 1897 | |
| 1898 | /* For a local variable, define the RTL now. */ |
| 1899 | if (current_binding_level != global_binding_level |
| 1900 | /* But not if this is a duplicate decl |
| 1901 | and we preserved the rtl from the previous one |
| 1902 | (which may or may not happen). */ |
| 1903 | && DECL_RTL (tem) == 0) |
| 1904 | { |
| 1905 | if (TYPE_SIZE (TREE_TYPE (tem)) != 0) |
| 1906 | expand_decl (tem, NULL_TREE); |
| 1907 | else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE |
| 1908 | && DECL_INITIAL (tem) != 0) |
| 1909 | expand_decl (tem, NULL_TREE); |
| 1910 | } |
| 1911 | |
| 1912 | if (init_written) |
| 1913 | { |
| 1914 | /* When parsing and digesting the initializer, |
| 1915 | use temporary storage. Do this even if we will ignore the value. */ |
| 1916 | if (current_binding_level == global_binding_level && debug_temp_inits) |
| 1917 | temporary_allocation (); |
| 1918 | } |
| 1919 | |
| 1920 | return tem; |
| 1921 | } |
| 1922 | |
| 1923 | /* Finish processing of a declaration; |
| 1924 | install its line number and initial value. |
| 1925 | If the length of an array type is not known before, |
| 1926 | it must be determined now, from the initial value, or it is an error. */ |
| 1927 | |
| 1928 | void |
| 1929 | finish_decl (decl, init, asmspec_tree) |
| 1930 | tree decl, init; |
| 1931 | tree asmspec_tree; |
| 1932 | { |
| 1933 | register tree type = TREE_TYPE (decl); |
| 1934 | int was_incomplete = (DECL_SIZE (decl) == 0); |
| 1935 | int temporary = allocation_temporary_p (); |
| 1936 | char *asmspec = 0; |
| 1937 | |
| 1938 | if (asmspec_tree) |
| 1939 | asmspec = TREE_STRING_POINTER (asmspec_tree); |
| 1940 | |
| 1941 | /* If `start_decl' didn't like having an initialization, ignore it now. */ |
| 1942 | |
| 1943 | if (init != 0 && DECL_INITIAL (decl) == 0) |
| 1944 | init = 0; |
| 1945 | |
| 1946 | if (init) |
| 1947 | { |
| 1948 | if (TREE_CODE (decl) != TYPE_DECL) |
| 1949 | store_init_value (decl, init); |
| 1950 | else |
| 1951 | { |
| 1952 | /* typedef foo = bar; store the type of bar as the type of foo. */ |
| 1953 | TREE_TYPE (decl) = TREE_TYPE (init); |
| 1954 | DECL_INITIAL (decl) = init = 0; |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | /* For top-level declaration, the initial value was read in |
| 1959 | the temporary obstack. MAXINDEX, rtl, etc. to be made below |
| 1960 | must go in the permanent obstack; but don't discard the |
| 1961 | temporary data yet. */ |
| 1962 | |
| 1963 | if (current_binding_level == global_binding_level && temporary) |
| 1964 | end_temporary_allocation (); |
| 1965 | |
| 1966 | /* Deduce size of array from initialization, if not already known */ |
| 1967 | |
| 1968 | if (TREE_CODE (type) == ARRAY_TYPE |
| 1969 | && TYPE_DOMAIN (type) == 0 |
| 1970 | && TREE_CODE (decl) != TYPE_DECL) |
| 1971 | { |
| 1972 | #if 0 |
| 1973 | int do_default = ! ((!pedantic && TREE_STATIC (decl)) |
| 1974 | || TREE_EXTERNAL (decl)); |
| 1975 | #endif |
| 1976 | int do_default |
| 1977 | = (TREE_STATIC (decl) |
| 1978 | /* Even if pedantic, an external linkage array |
| 1979 | may have incomplete type at first. */ |
| 1980 | ? pedantic && !TREE_PUBLIC (decl) |
| 1981 | : !TREE_EXTERNAL (decl)); |
| 1982 | int failure |
| 1983 | = complete_array_type (type, DECL_INITIAL (decl), do_default); |
| 1984 | |
| 1985 | if (failure == 1) |
| 1986 | error_with_decl (decl, "initializer fails to determine size of `%s'"); |
| 1987 | |
| 1988 | if (failure == 2) |
| 1989 | { |
| 1990 | if (do_default) |
| 1991 | { |
| 1992 | if (! TREE_PUBLIC (decl)) |
| 1993 | error_with_decl (decl, "array size missing in `%s'"); |
| 1994 | } |
| 1995 | else if (!pedantic && TREE_STATIC (decl)) |
| 1996 | TREE_EXTERNAL (decl) = 1; |
| 1997 | } |
| 1998 | |
| 1999 | if (pedantic && TYPE_DOMAIN (type) != 0 |
| 2000 | && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), |
| 2001 | integer_zero_node)) |
| 2002 | error_with_decl (decl, "zero-size array `%s'"); |
| 2003 | |
| 2004 | layout_decl (decl, 0); |
| 2005 | } |
| 2006 | |
| 2007 | if (TREE_CODE (decl) == VAR_DECL) |
| 2008 | { |
| 2009 | if (TREE_STATIC (decl) && DECL_SIZE (decl) == 0) |
| 2010 | { |
| 2011 | /* A static variable with an incomplete type: |
| 2012 | that is an error if it is initialized or `static'. |
| 2013 | Otherwise, let it through, but if it is not `extern' |
| 2014 | then it may cause an error message later. */ |
| 2015 | if (! (TREE_PUBLIC (decl) && DECL_INITIAL (decl) == 0)) |
| 2016 | error_with_decl (decl, "storage size of `%s' isn't known"); |
| 2017 | } |
| 2018 | else if (!TREE_EXTERNAL (decl) && DECL_SIZE (decl) == 0) |
| 2019 | { |
| 2020 | /* An automatic variable with an incomplete type: |
| 2021 | that is an error. */ |
| 2022 | error_with_decl (decl, "storage size of `%s' isn't known"); |
| 2023 | TREE_TYPE (decl) = error_mark_node; |
| 2024 | } |
| 2025 | |
| 2026 | if ((TREE_EXTERNAL (decl) || TREE_STATIC (decl)) |
| 2027 | && DECL_SIZE (decl) != 0 && ! TREE_LITERAL (DECL_SIZE (decl))) |
| 2028 | error_with_decl (decl, "storage size of `%s' isn't constant"); |
| 2029 | } |
| 2030 | |
| 2031 | /* Output the assembler code and/or RTL code for variables and functions, |
| 2032 | unless the type is an undefined structure or union. |
| 2033 | If not, it will get done when the type is completed. */ |
| 2034 | |
| 2035 | if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL) |
| 2036 | { |
| 2037 | if (flag_traditional && allocation_temporary_p ()) |
| 2038 | { |
| 2039 | end_temporary_allocation (); |
| 2040 | rest_of_decl_compilation (decl, asmspec, |
| 2041 | current_binding_level == global_binding_level, |
| 2042 | 0); |
| 2043 | resume_temporary_allocation (); |
| 2044 | } |
| 2045 | else |
| 2046 | rest_of_decl_compilation (decl, asmspec, |
| 2047 | current_binding_level == global_binding_level, |
| 2048 | 0); |
| 2049 | if (current_binding_level != global_binding_level) |
| 2050 | { |
| 2051 | /* Recompute the RTL of a local array now |
| 2052 | if it used to be an incomplete type. */ |
| 2053 | if (was_incomplete |
| 2054 | && ! TREE_STATIC (decl) && ! TREE_EXTERNAL (decl)) |
| 2055 | { |
| 2056 | /* If we used it already as memory, it must stay in memory. */ |
| 2057 | TREE_ADDRESSABLE (decl) = TREE_USED (decl); |
| 2058 | /* If it's still incomplete now, no init will save it. */ |
| 2059 | if (DECL_SIZE (decl) == 0) |
| 2060 | DECL_INITIAL (decl) = 0; |
| 2061 | expand_decl (decl, NULL_TREE); |
| 2062 | } |
| 2063 | /* Compute and store the initial value. */ |
| 2064 | expand_decl_init (decl); |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | if (TREE_CODE (decl) == TYPE_DECL) |
| 2069 | rest_of_decl_compilation (decl, 0, |
| 2070 | current_binding_level == global_binding_level, |
| 2071 | 0); |
| 2072 | |
| 2073 | /* Resume permanent allocation, if not within a function. */ |
| 2074 | if (temporary && current_binding_level == global_binding_level) |
| 2075 | { |
| 2076 | permanent_allocation (); |
| 2077 | /* We need to remember that this array HAD an initialization, |
| 2078 | but discard the actual nodes, since they are temporary anyway. */ |
| 2079 | if (DECL_INITIAL (decl) != 0) |
| 2080 | DECL_INITIAL (decl) = error_mark_node; |
| 2081 | } |
| 2082 | |
| 2083 | /* At the end of a declaration, throw away any variable type sizes |
| 2084 | of types defined inside that declaration. There is no use |
| 2085 | computing them in the following function definition. */ |
| 2086 | if (current_binding_level == global_binding_level) |
| 2087 | get_pending_sizes (); |
| 2088 | } |
| 2089 | |
| 2090 | /* Given a parsed parameter declaration, |
| 2091 | decode it into a PARM_DECL and push that on the current binding level. */ |
| 2092 | |
| 2093 | void |
| 2094 | push_parm_decl (parm) |
| 2095 | tree parm; |
| 2096 | { |
| 2097 | register tree decl = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm), |
| 2098 | PARM, 0); |
| 2099 | |
| 2100 | /* Add this decl to the current binding level. */ |
| 2101 | finish_decl (pushdecl (decl), NULL_TREE, NULL_TREE); |
| 2102 | } |
| 2103 | \f |
| 2104 | /* Make TYPE a complete type based on INITIAL_VALUE. |
| 2105 | Return 0 if successful, 1 if INITIAL_VALUE can't be decyphered, |
| 2106 | 2 if there was no information (in which case assume 1 if DO_DEFAULT). */ |
| 2107 | |
| 2108 | int |
| 2109 | complete_array_type (type, initial_value, do_default) |
| 2110 | tree type; |
| 2111 | tree initial_value; |
| 2112 | int do_default; |
| 2113 | { |
| 2114 | register tree maxindex = NULL_TREE; |
| 2115 | int value = 0; |
| 2116 | int temporary = (TREE_PERMANENT (type) && allocation_temporary_p ()); |
| 2117 | |
| 2118 | /* Don't put temporary nodes in permanent type. */ |
| 2119 | if (temporary) |
| 2120 | end_temporary_allocation (); |
| 2121 | |
| 2122 | if (initial_value) |
| 2123 | { |
| 2124 | /* Note MAXINDEX is really the maximum index, |
| 2125 | one less than the size. */ |
| 2126 | if (TREE_CODE (initial_value) == STRING_CST) |
| 2127 | { |
| 2128 | int eltsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (initial_value)))); |
| 2129 | maxindex = build_int_2 (TREE_STRING_LENGTH (initial_value) / eltsize - 1, 0); |
| 2130 | } |
| 2131 | else if (TREE_CODE (initial_value) == CONSTRUCTOR) |
| 2132 | { |
| 2133 | register int nelts |
| 2134 | = list_length (CONSTRUCTOR_ELTS (initial_value)); |
| 2135 | maxindex = build_int_2 (nelts - 1, 0); |
| 2136 | } |
| 2137 | else |
| 2138 | { |
| 2139 | /* Make an error message unless that happened already. */ |
| 2140 | if (initial_value != error_mark_node) |
| 2141 | value = 1; |
| 2142 | |
| 2143 | /* Prevent further error messages. */ |
| 2144 | maxindex = build_int_2 (1, 0); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | if (!maxindex) |
| 2149 | { |
| 2150 | if (do_default) |
| 2151 | maxindex = build_int_2 (1, 0); |
| 2152 | value = 2; |
| 2153 | } |
| 2154 | |
| 2155 | if (maxindex) |
| 2156 | { |
| 2157 | TYPE_DOMAIN (type) = build_index_type (maxindex); |
| 2158 | if (!TREE_TYPE (maxindex)) |
| 2159 | TREE_TYPE (maxindex) = TYPE_DOMAIN (type); |
| 2160 | } |
| 2161 | |
| 2162 | /* Lay out the type now that we can get the real answer. */ |
| 2163 | |
| 2164 | layout_type (type); |
| 2165 | |
| 2166 | if (temporary) |
| 2167 | resume_temporary_allocation (); |
| 2168 | |
| 2169 | return value; |
| 2170 | } |
| 2171 | \f |
| 2172 | /* Given declspecs and a declarator, |
| 2173 | determine the name and type of the object declared |
| 2174 | and construct a ..._DECL node for it. |
| 2175 | (In one case we can return a ..._TYPE node instead. |
| 2176 | For invalid input we sometimes return 0.) |
| 2177 | |
| 2178 | DECLSPECS is a chain of tree_list nodes whose value fields |
| 2179 | are the storage classes and type specifiers. |
| 2180 | |
| 2181 | DECL_CONTEXT says which syntactic context this declaration is in: |
| 2182 | NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. |
| 2183 | FUNCDEF for a function definition. Like NORMAL but a few different |
| 2184 | error messages in each case. Return value may be zero meaning |
| 2185 | this definition is too screwy to try to parse. |
| 2186 | PARM for a parameter declaration (either within a function prototype |
| 2187 | or before a function body). Make a PARM_DECL, or return void_type_node. |
| 2188 | TYPENAME if for a typename (in a cast or sizeof). |
| 2189 | Don't make a DECL node; just return the ..._TYPE node. |
| 2190 | FIELD for a struct or union field; make a FIELD_DECL. |
| 2191 | INITIALIZED is 1 if the decl has an initializer. |
| 2192 | |
| 2193 | In the TYPENAME case, DECLARATOR is really an absolute declarator. |
| 2194 | It may also be so in the PARM case, for a prototype where the |
| 2195 | argument type is specified but not the name. |
| 2196 | |
| 2197 | This function is where the complicated C meanings of `static' |
| 2198 | and `extern' are intrepreted. */ |
| 2199 | |
| 2200 | static tree |
| 2201 | grokdeclarator (declarator, declspecs, decl_context, initialized) |
| 2202 | tree declspecs; |
| 2203 | tree declarator; |
| 2204 | enum decl_context decl_context; |
| 2205 | int initialized; |
| 2206 | { |
| 2207 | int specbits = 0; |
| 2208 | tree spec; |
| 2209 | tree type = NULL_TREE; |
| 2210 | int longlong = 0; |
| 2211 | int constp; |
| 2212 | int volatilep; |
| 2213 | int inlinep; |
| 2214 | int explicit_int = 0; |
| 2215 | int explicit_char = 0; |
| 2216 | char *name; |
| 2217 | tree typedef_type = 0; |
| 2218 | int funcdef_flag = 0; |
| 2219 | int resume_temporary = 0; |
| 2220 | enum tree_code innermost_code = ERROR_MARK; |
| 2221 | |
| 2222 | if (decl_context == FUNCDEF) |
| 2223 | funcdef_flag = 1, decl_context = NORMAL; |
| 2224 | |
| 2225 | if (flag_traditional && allocation_temporary_p ()) |
| 2226 | { |
| 2227 | resume_temporary = 1; |
| 2228 | end_temporary_allocation (); |
| 2229 | } |
| 2230 | |
| 2231 | /* Look inside a declarator for the name being declared |
| 2232 | and get it as a string, for an error message. */ |
| 2233 | { |
| 2234 | register tree decl = declarator; |
| 2235 | name = 0; |
| 2236 | |
| 2237 | while (decl) |
| 2238 | switch (TREE_CODE (decl)) |
| 2239 | { |
| 2240 | case ARRAY_REF: |
| 2241 | case INDIRECT_REF: |
| 2242 | case CALL_EXPR: |
| 2243 | innermost_code = TREE_CODE (decl); |
| 2244 | decl = TREE_OPERAND (decl, 0); |
| 2245 | break; |
| 2246 | |
| 2247 | case IDENTIFIER_NODE: |
| 2248 | name = IDENTIFIER_POINTER (decl); |
| 2249 | decl = 0; |
| 2250 | break; |
| 2251 | |
| 2252 | default: |
| 2253 | abort (); |
| 2254 | } |
| 2255 | if (name == 0) |
| 2256 | name = "type name"; |
| 2257 | } |
| 2258 | |
| 2259 | /* A function definition's declarator must have the form of |
| 2260 | a function declarator. */ |
| 2261 | |
| 2262 | if (funcdef_flag && innermost_code != CALL_EXPR) |
| 2263 | return 0; |
| 2264 | |
| 2265 | /* Anything declared one level down from the top level |
| 2266 | must be one of the parameters of a function |
| 2267 | (because the body is at least two levels down). */ |
| 2268 | |
| 2269 | if (decl_context == NORMAL |
| 2270 | && current_binding_level->level_chain == global_binding_level) |
| 2271 | decl_context = PARM; |
| 2272 | |
| 2273 | /* Look through the decl specs and record which ones appear. |
| 2274 | Some typespecs are defined as built-in typenames. |
| 2275 | Others, the ones that are modifiers of other types, |
| 2276 | are represented by bits in SPECBITS: set the bits for |
| 2277 | the modifiers that appear. Storage class keywords are also in SPECBITS. |
| 2278 | |
| 2279 | If there is a typedef name or a type, store the type in TYPE. |
| 2280 | This includes builtin typedefs such as `int'. |
| 2281 | |
| 2282 | Set EXPLICIT_INT if the type is `int' or `char' and did not |
| 2283 | come from a user typedef. |
| 2284 | |
| 2285 | Set LONGLONG if `long' is mentioned twice. */ |
| 2286 | |
| 2287 | for (spec = declspecs; spec; spec = TREE_CHAIN (spec)) |
| 2288 | { |
| 2289 | register int i; |
| 2290 | register tree id = TREE_VALUE (spec); |
| 2291 | |
| 2292 | if (id == ridpointers[(int) RID_INT]) |
| 2293 | explicit_int = 1; |
| 2294 | if (id == ridpointers[(int) RID_CHAR]) |
| 2295 | explicit_char = 1; |
| 2296 | |
| 2297 | if (TREE_CODE (id) == IDENTIFIER_NODE) |
| 2298 | for (i = (int) RID_FIRST_MODIFIER; i < (int) RID_MAX; i++) |
| 2299 | { |
| 2300 | if (ridpointers[i] == id) |
| 2301 | { |
| 2302 | if (i == (int) RID_LONG && specbits & (1<<i)) |
| 2303 | { |
| 2304 | if (pedantic) |
| 2305 | warning ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
| 2306 | else if (longlong) |
| 2307 | warning ("`long long long' is too long for GCC"); |
| 2308 | else |
| 2309 | longlong = 1; |
| 2310 | } |
| 2311 | else if (specbits & (1 << i)) |
| 2312 | warning ("duplicate `%s'", IDENTIFIER_POINTER (id)); |
| 2313 | specbits |= 1 << i; |
| 2314 | goto found; |
| 2315 | } |
| 2316 | } |
| 2317 | if (type) |
| 2318 | error ("two or more data types in declaration of `%s'", name); |
| 2319 | else if (TREE_CODE (id) == IDENTIFIER_NODE) |
| 2320 | { |
| 2321 | register tree t = lookup_name (id); |
| 2322 | if (!t || TREE_CODE (t) != TYPE_DECL) |
| 2323 | error ("`%s' fails to be a typedef or built in type", |
| 2324 | IDENTIFIER_POINTER (id)); |
| 2325 | else type = TREE_TYPE (t); |
| 2326 | } |
| 2327 | else if (TREE_CODE (id) != ERROR_MARK) |
| 2328 | type = id; |
| 2329 | |
| 2330 | found: {} |
| 2331 | } |
| 2332 | |
| 2333 | typedef_type = type; |
| 2334 | |
| 2335 | /* No type at all: default to `int', and set EXPLICIT_INT |
| 2336 | because it was not a user-defined typedef. */ |
| 2337 | |
| 2338 | if (type == 0) |
| 2339 | { |
| 2340 | if (funcdef_flag && warn_return_type |
| 2341 | && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) |
| 2342 | | (1 << (int) RID_SIGNED) | (1 << (int) RID_UNSIGNED)))) |
| 2343 | warn_about_return_type = 1; |
| 2344 | explicit_int = 1; |
| 2345 | type = integer_type_node; |
| 2346 | } |
| 2347 | |
| 2348 | /* Now process the modifiers that were specified |
| 2349 | and check for invalid combinations. */ |
| 2350 | |
| 2351 | /* Long double is a special combination. */ |
| 2352 | |
| 2353 | if ((specbits & 1 << (int) RID_LONG) && type == double_type_node) |
| 2354 | { |
| 2355 | specbits &= ~ (1 << (int) RID_LONG); |
| 2356 | type = long_double_type_node; |
| 2357 | } |
| 2358 | |
| 2359 | /* Check all other uses of type modifiers. */ |
| 2360 | |
| 2361 | if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT) |
| 2362 | | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED))) |
| 2363 | { |
| 2364 | if (!explicit_int && !explicit_char && !pedantic) |
| 2365 | error ("long, short, signed or unsigned used invalidly for `%s'", name); |
| 2366 | else if ((specbits & 1 << (int) RID_LONG) && (specbits & 1 << (int) RID_SHORT)) |
| 2367 | error ("long and short specified together for `%s'", name); |
| 2368 | else if (((specbits & 1 << (int) RID_LONG) || (specbits & 1 << (int) RID_SHORT)) |
| 2369 | && explicit_char) |
| 2370 | error ("long or short specified with char for `%s'", name); |
| 2371 | else if ((specbits & 1 << (int) RID_SIGNED) && (specbits & 1 << (int) RID_UNSIGNED)) |
| 2372 | error ("signed and unsigned given together for `%s'", name); |
| 2373 | else |
| 2374 | { |
| 2375 | if (specbits & 1 << (int) RID_UNSIGNED) |
| 2376 | { |
| 2377 | if (longlong) |
| 2378 | type = long_long_unsigned_type_node; |
| 2379 | else if (specbits & 1 << (int) RID_LONG) |
| 2380 | type = long_unsigned_type_node; |
| 2381 | else if (specbits & 1 << (int) RID_SHORT) |
| 2382 | type = short_unsigned_type_node; |
| 2383 | else if (type == char_type_node) |
| 2384 | type = unsigned_char_type_node; |
| 2385 | else |
| 2386 | type = unsigned_type_node; |
| 2387 | } |
| 2388 | else if ((specbits & 1 << (int) RID_SIGNED) |
| 2389 | && type == char_type_node) |
| 2390 | type = signed_char_type_node; |
| 2391 | else if (longlong) |
| 2392 | type = long_long_integer_type_node; |
| 2393 | else if (specbits & 1 << (int) RID_LONG) |
| 2394 | type = long_integer_type_node; |
| 2395 | else if (specbits & 1 << (int) RID_SHORT) |
| 2396 | type = short_integer_type_node; |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | /* Set CONSTP if this declaration is `const', whether by |
| 2401 | explicit specification or via a typedef. |
| 2402 | Likewise for VOLATILEP. */ |
| 2403 | |
| 2404 | constp = !! (specbits & 1 << (int) RID_CONST) + TREE_READONLY (type); |
| 2405 | volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TREE_VOLATILE (type); |
| 2406 | inlinep = !! (specbits & (1 << (int) RID_INLINE)); |
| 2407 | if (constp > 1) |
| 2408 | warning ("duplicate `const'"); |
| 2409 | if (volatilep > 1) |
| 2410 | warning ("duplicate `volatile'"); |
| 2411 | type = TYPE_MAIN_VARIANT (type); |
| 2412 | |
| 2413 | /* Warn if two storage classes are given. Default to `auto'. */ |
| 2414 | |
| 2415 | { |
| 2416 | int nclasses = 0; |
| 2417 | |
| 2418 | if (specbits & 1 << (int) RID_AUTO) nclasses++; |
| 2419 | if (specbits & 1 << (int) RID_STATIC) nclasses++; |
| 2420 | if (specbits & 1 << (int) RID_EXTERN) nclasses++; |
| 2421 | if (specbits & 1 << (int) RID_REGISTER) nclasses++; |
| 2422 | if (specbits & 1 << (int) RID_TYPEDEF) nclasses++; |
| 2423 | |
| 2424 | /* Warn about storage classes that are invalid for certain |
| 2425 | kinds of declarations (parameters, typenames, etc.). */ |
| 2426 | |
| 2427 | if (nclasses > 1) |
| 2428 | error ("multiple storage classes in declaration of `%s'", name); |
| 2429 | else if (funcdef_flag |
| 2430 | && (specbits |
| 2431 | & ((1 << (int) RID_REGISTER) |
| 2432 | | (1 << (int) RID_AUTO) |
| 2433 | | (1 << (int) RID_TYPEDEF)))) |
| 2434 | { |
| 2435 | if (specbits & 1 << (int) RID_AUTO) |
| 2436 | error ("function definition declared `auto'"); |
| 2437 | if (specbits & 1 << (int) RID_REGISTER) |
| 2438 | error ("function definition declared `auto'"); |
| 2439 | if (specbits & 1 << (int) RID_TYPEDEF) |
| 2440 | error ("function definition declared `typedef'"); |
| 2441 | specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) |
| 2442 | | (1 << (int) RID_AUTO)); |
| 2443 | } |
| 2444 | else if (decl_context != NORMAL && nclasses > 0) |
| 2445 | { |
| 2446 | if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER) |
| 2447 | ; |
| 2448 | else |
| 2449 | { |
| 2450 | error ((decl_context == FIELD |
| 2451 | ? "storage class specified for structure field `%s'" |
| 2452 | : (decl_context == PARM |
| 2453 | ? "storage class specified for parameter `%s'" |
| 2454 | : "storage class specified for typename")), |
| 2455 | name); |
| 2456 | specbits &= ~ ((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER) |
| 2457 | | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC) |
| 2458 | | (1 << (int) RID_EXTERN)); |
| 2459 | } |
| 2460 | } |
| 2461 | else if (specbits & 1 << (int) RID_EXTERN && initialized |
| 2462 | && ! funcdef_flag) |
| 2463 | warning ("`%s' initialized and declared `extern'", name); |
| 2464 | else if (current_binding_level == global_binding_level |
| 2465 | && specbits & (1 << (int) RID_AUTO)) |
| 2466 | error ("top-level declaration of `%s' specifies `auto'", name); |
| 2467 | } |
| 2468 | |
| 2469 | /* Now figure out the structure of the declarator proper. |
| 2470 | Descend through it, creating more complex types, until we reach |
| 2471 | the declared identifier (or NULL_TREE, in an absolute declarator). */ |
| 2472 | |
| 2473 | while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE) |
| 2474 | { |
| 2475 | if (type == error_mark_node) |
| 2476 | { |
| 2477 | declarator = TREE_OPERAND (declarator, 0); |
| 2478 | continue; |
| 2479 | } |
| 2480 | |
| 2481 | /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]), |
| 2482 | an INDIRECT_REF (for *...), |
| 2483 | a CALL_EXPR (for ...(...)), |
| 2484 | an identifier (for the name being declared) |
| 2485 | or a null pointer (for the place in an absolute declarator |
| 2486 | where the name was omitted). |
| 2487 | For the last two cases, we have just exited the loop. |
| 2488 | |
| 2489 | At this point, TYPE is the type of elements of an array, |
| 2490 | or for a function to return, or for a pointer to point to. |
| 2491 | After this sequence of ifs, TYPE is the type of the |
| 2492 | array or function or pointer, and DECLARATOR has had its |
| 2493 | outermost layer removed. */ |
| 2494 | |
| 2495 | if (TREE_CODE (declarator) == ARRAY_REF) |
| 2496 | { |
| 2497 | register tree itype = NULL_TREE; |
| 2498 | register tree size = TREE_OPERAND (declarator, 1); |
| 2499 | |
| 2500 | declarator = TREE_OPERAND (declarator, 0); |
| 2501 | |
| 2502 | /* Check for some types that there cannot be arrays of. */ |
| 2503 | |
| 2504 | if (type == void_type_node) |
| 2505 | { |
| 2506 | error ("declaration of `%s' as array of voids", name); |
| 2507 | type = error_mark_node; |
| 2508 | } |
| 2509 | |
| 2510 | if (TREE_CODE (type) == FUNCTION_TYPE) |
| 2511 | { |
| 2512 | error ("declaration of `%s' as array of functions", name); |
| 2513 | type = error_mark_node; |
| 2514 | } |
| 2515 | |
| 2516 | if (size == error_mark_node) |
| 2517 | type = error_mark_node; |
| 2518 | |
| 2519 | if (type == error_mark_node) |
| 2520 | continue; |
| 2521 | |
| 2522 | /* If size was specified, set ITYPE to a range-type for that size. |
| 2523 | Otherwise, ITYPE remains null. finish_decl may figure it out |
| 2524 | from an initial value. */ |
| 2525 | |
| 2526 | if (size) |
| 2527 | { |
| 2528 | /* might be a cast */ |
| 2529 | if (TREE_CODE (size) == NOP_EXPR |
| 2530 | && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0))) |
| 2531 | size = TREE_OPERAND (size, 0); |
| 2532 | |
| 2533 | if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE |
| 2534 | && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE) |
| 2535 | { |
| 2536 | error ("size of array `%s' has non-integer type", name); |
| 2537 | size = integer_one_node; |
| 2538 | } |
| 2539 | if (pedantic && integer_zerop (size)) |
| 2540 | warning ("ANSI C forbids zero-size array `%s'", name); |
| 2541 | if (TREE_CODE (size) == INTEGER_CST) |
| 2542 | { |
| 2543 | if (INT_CST_LT (size, integer_zero_node)) |
| 2544 | { |
| 2545 | error ("size of array `%s' is negative", name); |
| 2546 | size = integer_one_node; |
| 2547 | } |
| 2548 | itype = build_index_type (build_int_2 (TREE_INT_CST_LOW (size) - 1, 0)); |
| 2549 | } |
| 2550 | else |
| 2551 | { |
| 2552 | if (pedantic) |
| 2553 | warning ("ANSI C forbids variable-size array `%s'", name); |
| 2554 | itype = build_binary_op (MINUS_EXPR, size, integer_one_node); |
| 2555 | itype = build_index_type (itype); |
| 2556 | } |
| 2557 | } |
| 2558 | |
| 2559 | /* Complain about arrays of incomplete types, except in typedefs. */ |
| 2560 | |
| 2561 | if (TYPE_SIZE (type) == 0 |
| 2562 | && !(specbits & (1 << (int) RID_TYPEDEF))) |
| 2563 | warning ("array type has incomplete element type"); |
| 2564 | |
| 2565 | /* Build the array type itself. |
| 2566 | Merge any constancy or volatility into the target type. */ |
| 2567 | |
| 2568 | if (constp || volatilep) |
| 2569 | type = c_build_type_variant (type, constp, volatilep); |
| 2570 | |
| 2571 | #if 0 /* don't clear these; leave them set so that the array type |
| 2572 | or the variable is itself const or volatile. */ |
| 2573 | constp = 0; |
| 2574 | volatilep = 0; |
| 2575 | #endif |
| 2576 | |
| 2577 | type = build_array_type (type, itype); |
| 2578 | } |
| 2579 | else if (TREE_CODE (declarator) == CALL_EXPR) |
| 2580 | { |
| 2581 | tree arg_types; |
| 2582 | |
| 2583 | /* Declaring a function type. |
| 2584 | Make sure we have a valid type for the function to return. */ |
| 2585 | if (type == error_mark_node) |
| 2586 | continue; |
| 2587 | |
| 2588 | if (pedantic && (constp || volatilep)) |
| 2589 | warning ("function declared to return const or volatile result"); |
| 2590 | |
| 2591 | /* Warn about some types functions can't return. */ |
| 2592 | |
| 2593 | if (TREE_CODE (type) == FUNCTION_TYPE) |
| 2594 | { |
| 2595 | error ("`%s' declared as function returning a function", name); |
| 2596 | type = integer_type_node; |
| 2597 | } |
| 2598 | if (TREE_CODE (type) == ARRAY_TYPE) |
| 2599 | { |
| 2600 | error ("`%s' declared as function returning an array", name); |
| 2601 | type = integer_type_node; |
| 2602 | } |
| 2603 | |
| 2604 | /* Traditionally, declaring return type float means double. */ |
| 2605 | |
| 2606 | if (flag_traditional && type == float_type_node) |
| 2607 | type = double_type_node; |
| 2608 | |
| 2609 | /* Construct the function type and go to the next |
| 2610 | inner layer of declarator. */ |
| 2611 | |
| 2612 | arg_types = grokparms (TREE_OPERAND (declarator, 1), |
| 2613 | funcdef_flag |
| 2614 | /* Say it's a definition |
| 2615 | only for the CALL_EXPR |
| 2616 | closest to the identifier. */ |
| 2617 | && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE); |
| 2618 | #if 0 /* This seems to be false. We turn off temporary allocation |
| 2619 | above in this function if -traditional. |
| 2620 | And this code caused inconsistent results with prototypes: |
| 2621 | callers would ignore them, and pass arguments wrong. */ |
| 2622 | |
| 2623 | /* Omit the arg types if -traditional, since the arg types |
| 2624 | and the list links might not be permanent. */ |
| 2625 | type = build_function_type (type, flag_traditional ? 0 : arg_types); |
| 2626 | #endif |
| 2627 | type = build_function_type (type, arg_types); |
| 2628 | declarator = TREE_OPERAND (declarator, 0); |
| 2629 | } |
| 2630 | else if (TREE_CODE (declarator) == INDIRECT_REF) |
| 2631 | { |
| 2632 | /* Merge any constancy or volatility into the target type |
| 2633 | for the pointer. */ |
| 2634 | |
| 2635 | if (constp || volatilep) |
| 2636 | type = c_build_type_variant (type, constp, volatilep); |
| 2637 | constp = 0; |
| 2638 | volatilep = 0; |
| 2639 | |
| 2640 | type = build_pointer_type (type); |
| 2641 | |
| 2642 | /* Process a list of type modifier keywords |
| 2643 | (such as const or volatile) that were given inside the `*'. */ |
| 2644 | |
| 2645 | if (TREE_TYPE (declarator)) |
| 2646 | { |
| 2647 | register tree typemodlist; |
| 2648 | int erred = 0; |
| 2649 | for (typemodlist = TREE_TYPE (declarator); typemodlist; |
| 2650 | typemodlist = TREE_CHAIN (typemodlist)) |
| 2651 | { |
| 2652 | if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_CONST]) |
| 2653 | constp++; |
| 2654 | else if (TREE_VALUE (typemodlist) == ridpointers[(int) RID_VOLATILE]) |
| 2655 | volatilep++; |
| 2656 | else if (!erred) |
| 2657 | { |
| 2658 | erred = 1; |
| 2659 | error ("invalid type modifier within pointer declarator"); |
| 2660 | } |
| 2661 | } |
| 2662 | if (constp > 1) |
| 2663 | warning ("duplicate `const'"); |
| 2664 | if (volatilep > 1) |
| 2665 | warning ("duplicate `volatile'"); |
| 2666 | } |
| 2667 | |
| 2668 | declarator = TREE_OPERAND (declarator, 0); |
| 2669 | } |
| 2670 | else |
| 2671 | abort (); |
| 2672 | |
| 2673 | /* layout_type (type); */ |
| 2674 | |
| 2675 | /* @@ Should perhaps replace the following code by changes in |
| 2676 | * @@ stor_layout.c. */ |
| 2677 | if (TREE_CODE (type) == FUNCTION_DECL) |
| 2678 | { |
| 2679 | /* A function variable in C should be Pmode rather than EPmode |
| 2680 | because it has just the address of a function, no static chain.*/ |
| 2681 | TYPE_MODE (type) = Pmode; |
| 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | /* Now TYPE has the actual type. */ |
| 2686 | |
| 2687 | /* If this is declaring a typedef name, return a TYPE_DECL. */ |
| 2688 | |
| 2689 | if (specbits & (1 << (int) RID_TYPEDEF)) |
| 2690 | { |
| 2691 | /* Note that the grammar rejects storage classes |
| 2692 | in typenames, fields or parameters */ |
| 2693 | if (constp || volatilep) |
| 2694 | type = c_build_type_variant (type, constp, volatilep); |
| 2695 | if (resume_temporary) |
| 2696 | resume_temporary_allocation (); |
| 2697 | return build_decl (TYPE_DECL, declarator, type); |
| 2698 | } |
| 2699 | |
| 2700 | /* Detect the case of an array type of unspecified size |
| 2701 | which came, as such, direct from a typedef name. |
| 2702 | We must copy the type, so that each identifier gets |
| 2703 | a distinct type, so that each identifier's size can be |
| 2704 | controlled separately by its own initializer. */ |
| 2705 | |
| 2706 | if (type != 0 && typedef_type != 0 |
| 2707 | && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type) |
| 2708 | && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0) |
| 2709 | type = build_array_type (TREE_TYPE (type), 0); |
| 2710 | |
| 2711 | /* If this is a type name (such as, in a cast or sizeof), |
| 2712 | compute the type and return it now. */ |
| 2713 | |
| 2714 | if (decl_context == TYPENAME) |
| 2715 | { |
| 2716 | /* Note that the grammar rejects storage classes |
| 2717 | in typenames, fields or parameters */ |
| 2718 | if (constp || volatilep) |
| 2719 | type = c_build_type_variant (type, constp, volatilep); |
| 2720 | if (resume_temporary) |
| 2721 | resume_temporary_allocation (); |
| 2722 | return type; |
| 2723 | } |
| 2724 | |
| 2725 | /* `void' at top level (not within pointer) |
| 2726 | is allowed only in typedefs or type names. |
| 2727 | We don't complain about parms either, but that is because |
| 2728 | a better error message can be made later. */ |
| 2729 | |
| 2730 | if (type == void_type_node && decl_context != PARM) |
| 2731 | { |
| 2732 | error ("variable or field `%s' declared void", |
| 2733 | IDENTIFIER_POINTER (declarator)); |
| 2734 | type = integer_type_node; |
| 2735 | } |
| 2736 | |
| 2737 | /* Now create the decl, which may be a VAR_DECL, a PARM_DECL |
| 2738 | or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ |
| 2739 | |
| 2740 | { |
| 2741 | register tree decl; |
| 2742 | |
| 2743 | if (decl_context == PARM) |
| 2744 | { |
| 2745 | /* A parameter declared as an array of T is really a pointer to T. |
| 2746 | One declared as a function is really a pointer to a function. */ |
| 2747 | |
| 2748 | if (TREE_CODE (type) == ARRAY_TYPE) |
| 2749 | { |
| 2750 | /* Transfer const-ness of array into that of type pointed to. */ |
| 2751 | type = build_pointer_type |
| 2752 | (c_build_type_variant (TREE_TYPE (type), constp, volatilep)); |
| 2753 | volatilep = constp = 0; |
| 2754 | } |
| 2755 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
| 2756 | { |
| 2757 | if (pedantic && (constp || volatilep)) |
| 2758 | warning ("ANSI C forbids const or volatile function types"); |
| 2759 | type = build_pointer_type (c_build_type_variant (type, constp, volatilep)); |
| 2760 | volatilep = constp = 0; |
| 2761 | } |
| 2762 | |
| 2763 | if (initialized) |
| 2764 | error ("parameter `%s' is initialized", name); |
| 2765 | |
| 2766 | decl = build_decl (PARM_DECL, declarator, type); |
| 2767 | |
| 2768 | /* Compute the type actually passed in the parmlist, |
| 2769 | for the case where there is no prototype. |
| 2770 | (For example, shorts and chars are passed as ints.) |
| 2771 | When there is a prototype, this is overridden later. */ |
| 2772 | |
| 2773 | DECL_ARG_TYPE (decl) = type; |
| 2774 | if (type == float_type_node) |
| 2775 | DECL_ARG_TYPE (decl) = double_type_node; |
| 2776 | else if (TREE_CODE (type) == INTEGER_TYPE |
| 2777 | /* ANSI C says short and char are promoted to int |
| 2778 | or unsigned int, even if that is not wider. */ |
| 2779 | && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node) |
| 2780 | || type == short_integer_type_node |
| 2781 | || type == short_unsigned_type_node)) |
| 2782 | { |
| 2783 | if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node) |
| 2784 | && TREE_UNSIGNED (type)) |
| 2785 | DECL_ARG_TYPE (decl) = unsigned_type_node; |
| 2786 | else |
| 2787 | DECL_ARG_TYPE (decl) = integer_type_node; |
| 2788 | } |
| 2789 | } |
| 2790 | else if (decl_context == FIELD) |
| 2791 | { |
| 2792 | /* Structure field. It may not be a function. */ |
| 2793 | |
| 2794 | if (TREE_CODE (type) == FUNCTION_TYPE) |
| 2795 | { |
| 2796 | error ("field `%s' declared as a function", |
| 2797 | IDENTIFIER_POINTER (declarator)); |
| 2798 | type = build_pointer_type (type); |
| 2799 | } |
| 2800 | else if (TREE_CODE (type) != ERROR_MARK && TYPE_SIZE (type) == 0) |
| 2801 | { |
| 2802 | error ("field `%s' has incomplete type", |
| 2803 | IDENTIFIER_POINTER (declarator)); |
| 2804 | type = error_mark_node; |
| 2805 | } |
| 2806 | /* Move type qualifiers down to element of an array. */ |
| 2807 | if (TREE_CODE (type) == ARRAY_TYPE && (constp || volatilep)) |
| 2808 | { |
| 2809 | type = c_build_type_variant (type, constp, volatilep); |
| 2810 | constp = volatilep = 0; |
| 2811 | } |
| 2812 | /* Note that the grammar rejects storage classes |
| 2813 | in typenames, fields or parameters */ |
| 2814 | decl = build_decl (FIELD_DECL, declarator, type); |
| 2815 | } |
| 2816 | else if (TREE_CODE (type) == FUNCTION_TYPE) |
| 2817 | { |
| 2818 | if (specbits & ((1 << (int) RID_AUTO) | (1 << (int) RID_REGISTER))) |
| 2819 | error ("invalid storage class for function `%s'", |
| 2820 | IDENTIFIER_POINTER (declarator)); |
| 2821 | /* Function declaration not at top level. |
| 2822 | Storage classes other than `extern' are not allowed |
| 2823 | and `extern' makes no difference. */ |
| 2824 | if (current_binding_level != global_binding_level |
| 2825 | && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE))) |
| 2826 | && pedantic) |
| 2827 | warning ("invalid storage class for function `%s'", |
| 2828 | IDENTIFIER_POINTER (declarator)); |
| 2829 | decl = build_decl (FUNCTION_DECL, declarator, type); |
| 2830 | |
| 2831 | TREE_EXTERNAL (decl) = 1; |
| 2832 | /* Record presence of `static'. */ |
| 2833 | TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC)); |
| 2834 | /* Record presence of `inline', if it is reasonable. */ |
| 2835 | if (inlinep) |
| 2836 | { |
| 2837 | tree last = tree_last (TYPE_ARG_TYPES (type)); |
| 2838 | |
| 2839 | if (! strcmp (IDENTIFIER_POINTER (declarator), "main")) |
| 2840 | warning ("cannot inline function `main'"); |
| 2841 | else if (last && TREE_VALUE (last) != void_type_node) |
| 2842 | warning ("inline declaration ignored for function with `...'"); |
| 2843 | else |
| 2844 | /* Assume that otherwise the function can be inlined. */ |
| 2845 | TREE_INLINE (decl) = 1; |
| 2846 | |
| 2847 | if (specbits & (1 << (int) RID_EXTERN)) |
| 2848 | current_extern_inline = 1; |
| 2849 | } |
| 2850 | } |
| 2851 | else |
| 2852 | { |
| 2853 | /* It's a variable. */ |
| 2854 | |
| 2855 | /* Move type qualifiers down to element of an array. */ |
| 2856 | if (TREE_CODE (type) == ARRAY_TYPE && (constp || volatilep)) |
| 2857 | { |
| 2858 | type = c_build_type_variant (type, constp, volatilep); |
| 2859 | #if 0 /* but a variable whose type is const should still have TREE_READONLY. */ |
| 2860 | constp = volatilep = 0; |
| 2861 | #endif |
| 2862 | } |
| 2863 | |
| 2864 | decl = build_decl (VAR_DECL, declarator, type); |
| 2865 | |
| 2866 | if (inlinep) |
| 2867 | warning_with_decl (decl, "variable `%s' declared `inline'"); |
| 2868 | |
| 2869 | /* An uninitialized decl with `extern' is a reference. */ |
| 2870 | TREE_EXTERNAL (decl) |
| 2871 | = !initialized && (specbits & (1 << (int) RID_EXTERN)); |
| 2872 | /* At top level, either `static' or no s.c. makes a definition |
| 2873 | (perhaps tentative), and absence of `static' makes it public. */ |
| 2874 | if (current_binding_level == global_binding_level) |
| 2875 | { |
| 2876 | TREE_PUBLIC (decl) = !(specbits & (1 << (int) RID_STATIC)); |
| 2877 | TREE_STATIC (decl) = ! TREE_EXTERNAL (decl); |
| 2878 | } |
| 2879 | /* Not at top level, only `static' makes a static definition. */ |
| 2880 | else |
| 2881 | { |
| 2882 | TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0; |
| 2883 | TREE_PUBLIC (decl) = TREE_EXTERNAL (decl); |
| 2884 | /* `extern' with initialization is invalid if not at top level. */ |
| 2885 | if ((specbits & (1 << (int) RID_EXTERN)) && initialized) |
| 2886 | error ("`%s' has both `extern' and initializer", name); |
| 2887 | } |
| 2888 | } |
| 2889 | |
| 2890 | /* Record `register' declaration for warnings on & |
| 2891 | and in case doing stupid register allocation. */ |
| 2892 | |
| 2893 | if (specbits & (1 << (int) RID_REGISTER)) |
| 2894 | TREE_REGDECL (decl) = 1; |
| 2895 | |
| 2896 | /* Record constancy and volatility. */ |
| 2897 | |
| 2898 | if (constp) |
| 2899 | TREE_READONLY (decl) = 1; |
| 2900 | if (volatilep) |
| 2901 | { |
| 2902 | TREE_VOLATILE (decl) = 1; |
| 2903 | TREE_THIS_VOLATILE (decl) = 1; |
| 2904 | } |
| 2905 | |
| 2906 | if (resume_temporary) |
| 2907 | resume_temporary_allocation (); |
| 2908 | |
| 2909 | return decl; |
| 2910 | } |
| 2911 | } |
| 2912 | \f |
| 2913 | /* Make a variant type in the proper way for C, propagating qualifiers |
| 2914 | down to the element type of an array. */ |
| 2915 | |
| 2916 | tree |
| 2917 | c_build_type_variant (type, constp, volatilep) |
| 2918 | tree type; |
| 2919 | int constp, volatilep; |
| 2920 | { |
| 2921 | if (TREE_CODE (type) != ARRAY_TYPE) |
| 2922 | return build_type_variant (type, constp, volatilep); |
| 2923 | |
| 2924 | return build_array_type (c_build_type_variant (TREE_TYPE (type), |
| 2925 | constp, volatilep), |
| 2926 | TYPE_DOMAIN (type)); |
| 2927 | } |
| 2928 | \f |
| 2929 | /* Decode the parameter-list info for a function type or function definition. |
| 2930 | The argument is the value returned by `get_parm_info' (or made in parse.y |
| 2931 | if there is an identifier list instead of a parameter decl list). |
| 2932 | These two functions are separate because when a function returns |
| 2933 | or receives functions then each is called multiple times but the order |
| 2934 | of calls is different. The last call to `grokparms' is always the one |
| 2935 | that contains the formal parameter names of a function definition. |
| 2936 | |
| 2937 | Store in `last_function_parms' a chain of the decls of parms. |
| 2938 | Also store in `last_function_parm_tags' a chain of the struct and union |
| 2939 | tags declared among the parms. |
| 2940 | |
| 2941 | Return a list of arg types to use in the FUNCTION_TYPE for this function. |
| 2942 | |
| 2943 | FUNCDEF_FLAG is nonzero for a function definition, 0 for |
| 2944 | a mere declaration. A nonempty identifier-list gets an error message |
| 2945 | when FUNCDEF_FLAG is zero. */ |
| 2946 | |
| 2947 | static tree |
| 2948 | grokparms (parms_info, funcdef_flag) |
| 2949 | tree parms_info; |
| 2950 | int funcdef_flag; |
| 2951 | { |
| 2952 | tree first_parm = TREE_CHAIN (parms_info); |
| 2953 | |
| 2954 | last_function_parms = TREE_PURPOSE (parms_info); |
| 2955 | last_function_parm_tags = TREE_VALUE (parms_info); |
| 2956 | |
| 2957 | if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag) |
| 2958 | warning ("function declaration isn't a prototype"); |
| 2959 | |
| 2960 | if (first_parm != 0 |
| 2961 | && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE) |
| 2962 | { |
| 2963 | if (! funcdef_flag) |
| 2964 | warning ("parameter names (without types) in function declaration"); |
| 2965 | |
| 2966 | last_function_parms = first_parm; |
| 2967 | return 0; |
| 2968 | } |
| 2969 | else |
| 2970 | { |
| 2971 | tree parm; |
| 2972 | tree typelt; |
| 2973 | /* We no longer test FUNCDEF_FLAG. |
| 2974 | If the arg types are incomplete in a declaration, |
| 2975 | they must include undefined tags. |
| 2976 | These tags can never be defined in the scope of the declaration, |
| 2977 | so the types can never be completed, |
| 2978 | and no call can be compiled successfully. */ |
| 2979 | #if 0 |
| 2980 | /* In a fcn definition, arg types must be complete. */ |
| 2981 | if (funcdef_flag) |
| 2982 | #endif |
| 2983 | for (parm = last_function_parms, typelt = first_parm; |
| 2984 | parm; |
| 2985 | parm = TREE_CHAIN (parm)) |
| 2986 | /* Skip over any enumeration constants declared here. */ |
| 2987 | if (TREE_CODE (parm) == PARM_DECL) |
| 2988 | { |
| 2989 | /* Barf if the parameter itself has an incomplete type. */ |
| 2990 | tree type = TREE_VALUE (typelt); |
| 2991 | if (TYPE_SIZE (type) == 0) |
| 2992 | { |
| 2993 | if (funcdef_flag && DECL_NAME (parm) != 0) |
| 2994 | error ("parameter `%s' has incomplete type", |
| 2995 | IDENTIFIER_POINTER (DECL_NAME (parm))); |
| 2996 | else |
| 2997 | warning ("parameter has incomplete type"); |
| 2998 | if (funcdef_flag) |
| 2999 | { |
| 3000 | TREE_VALUE (typelt) = error_mark_node; |
| 3001 | TREE_TYPE (parm) = error_mark_node; |
| 3002 | } |
| 3003 | } |
| 3004 | #if 0 /* This has been replaced by parm_tags_warning |
| 3005 | which uses a more accurate criterion for what to warn about. */ |
| 3006 | else |
| 3007 | { |
| 3008 | /* Now warn if is a pointer to an incomplete type. */ |
| 3009 | while (TREE_CODE (type) == POINTER_TYPE |
| 3010 | || TREE_CODE (type) == REFERENCE_TYPE) |
| 3011 | type = TREE_TYPE (type); |
| 3012 | type = TYPE_MAIN_VARIANT (type); |
| 3013 | if (TYPE_SIZE (type) == 0) |
| 3014 | { |
| 3015 | if (DECL_NAME (parm) != 0) |
| 3016 | warning ("parameter `%s' points to incomplete type", |
| 3017 | IDENTIFIER_POINTER (DECL_NAME (parm))); |
| 3018 | else |
| 3019 | warning ("parameter points to incomplete type"); |
| 3020 | } |
| 3021 | } |
| 3022 | #endif |
| 3023 | typelt = TREE_CHAIN (typelt); |
| 3024 | } |
| 3025 | |
| 3026 | return first_parm; |
| 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | |
| 3031 | /* Return a tree_list node with info on a parameter list just parsed. |
| 3032 | The TREE_PURPOSE is a chain of decls of those parms. |
| 3033 | The TREE_VALUE is a list of structure, union and enum tags defined. |
| 3034 | The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE. |
| 3035 | This tree_list node is later fed to `grokparms'. |
| 3036 | |
| 3037 | VOID_AT_END nonzero means append `void' to the end of the type-list. |
| 3038 | Zero means the parmlist ended with an ellipsis so don't append `void'. */ |
| 3039 | |
| 3040 | tree |
| 3041 | get_parm_info (void_at_end) |
| 3042 | int void_at_end; |
| 3043 | { |
| 3044 | register tree decl; |
| 3045 | register tree types = 0; |
| 3046 | int erred = 0; |
| 3047 | tree tags = gettags (); |
| 3048 | tree parms = nreverse (getdecls ()); |
| 3049 | |
| 3050 | /* Just `void' (and no ellipsis) is special. There are really no parms. */ |
| 3051 | if (void_at_end && parms != 0 |
| 3052 | && TREE_CHAIN (parms) == 0 |
| 3053 | && TREE_TYPE (parms) == void_type_node |
| 3054 | && DECL_NAME (parms) == 0) |
| 3055 | { |
| 3056 | parms = NULL_TREE; |
| 3057 | storedecls (NULL_TREE); |
| 3058 | return saveable_tree_cons (NULL_TREE, NULL_TREE, |
| 3059 | saveable_tree_cons (NULL_TREE, void_type_node, NULL_TREE)); |
| 3060 | } |
| 3061 | |
| 3062 | storedecls (parms); |
| 3063 | |
| 3064 | for (decl = parms; decl; decl = TREE_CHAIN (decl)) |
| 3065 | /* There may also be declarations for enumerators if an enumeration |
| 3066 | type is declared among the parms. Ignore them here. */ |
| 3067 | if (TREE_CODE (decl) == PARM_DECL) |
| 3068 | { |
| 3069 | /* Since there is a prototype, |
| 3070 | args are passed in their declared types. */ |
| 3071 | tree type = TREE_TYPE (decl); |
| 3072 | DECL_ARG_TYPE (decl) = type; |
| 3073 | #ifdef PROMOTE_PROTOTYPES |
| 3074 | if (TREE_CODE (type) == INTEGER_TYPE |
| 3075 | && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)) |
| 3076 | DECL_ARG_TYPE (decl) = integer_type_node; |
| 3077 | #endif |
| 3078 | |
| 3079 | types = saveable_tree_cons (NULL_TREE, TREE_TYPE (decl), types); |
| 3080 | if (TREE_VALUE (types) == void_type_node && ! erred |
| 3081 | && DECL_NAME (decl) == 0) |
| 3082 | { |
| 3083 | error ("`void' in parameter list must be the entire list"); |
| 3084 | erred = 1; |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | if (void_at_end) |
| 3089 | return saveable_tree_cons (parms, tags, |
| 3090 | nreverse (saveable_tree_cons (NULL_TREE, void_type_node, types))); |
| 3091 | |
| 3092 | return saveable_tree_cons (parms, tags, nreverse (types)); |
| 3093 | } |
| 3094 | |
| 3095 | /* At end of parameter list, warn about any struct, union or enum tags |
| 3096 | defined within. Do so because these types cannot ever become complete. */ |
| 3097 | |
| 3098 | void |
| 3099 | parmlist_tags_warning () |
| 3100 | { |
| 3101 | tree elt; |
| 3102 | static int already; |
| 3103 | |
| 3104 | for (elt = current_binding_level->tags; elt; elt = TREE_CHAIN (elt)) |
| 3105 | { |
| 3106 | enum tree_code code = TREE_CODE (TREE_VALUE (elt)); |
| 3107 | warning ("`%s %s' declared inside parameter list", |
| 3108 | (code == RECORD_TYPE ? "struct" |
| 3109 | : code == UNION_TYPE ? "union" |
| 3110 | : "enum"), |
| 3111 | IDENTIFIER_POINTER (TREE_PURPOSE (elt))); |
| 3112 | if (! already) |
| 3113 | { |
| 3114 | warning ("its scope is only this definition or declaration,"); |
| 3115 | warning ("which is probably not what you want."); |
| 3116 | already = 1; |
| 3117 | } |
| 3118 | } |
| 3119 | } |
| 3120 | \f |
| 3121 | /* Get the struct, enum or union (CODE says which) with tag NAME. |
| 3122 | Define the tag as a forward-reference if it is not defined. */ |
| 3123 | |
| 3124 | tree |
| 3125 | xref_tag (code, name) |
| 3126 | enum tree_code code; |
| 3127 | tree name; |
| 3128 | { |
| 3129 | int temporary = allocation_temporary_p (); |
| 3130 | |
| 3131 | /* If a cross reference is requested, look up the type |
| 3132 | already defined for this tag and return it. */ |
| 3133 | |
| 3134 | register tree ref = lookup_tag (code, name, current_binding_level, 0); |
| 3135 | if (ref) return ref; |
| 3136 | |
| 3137 | if (current_binding_level == global_binding_level && temporary) |
| 3138 | end_temporary_allocation (); |
| 3139 | |
| 3140 | /* If no such tag is yet defined, create a forward-reference node |
| 3141 | and record it as the "definition". |
| 3142 | When a real declaration of this type is found, |
| 3143 | the forward-reference will be altered into a real type. */ |
| 3144 | |
| 3145 | ref = make_node (code); |
| 3146 | if (code == ENUMERAL_TYPE) |
| 3147 | { |
| 3148 | /* (In ANSI, Enums can be referred to only if already defined.) */ |
| 3149 | if (pedantic) |
| 3150 | warning ("ANSI C forbids forward references to `enum' types"); |
| 3151 | /* Give the type a default layout like unsigned int |
| 3152 | to avoid crashing if it does not get defined. */ |
| 3153 | TYPE_MODE (ref) = SImode; |
| 3154 | TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node); |
| 3155 | TREE_UNSIGNED (ref) = 1; |
| 3156 | TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node); |
| 3157 | TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node); |
| 3158 | TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node); |
| 3159 | } |
| 3160 | |
| 3161 | pushtag (name, ref); |
| 3162 | |
| 3163 | if (current_binding_level == global_binding_level && temporary) |
| 3164 | resume_temporary_allocation (); |
| 3165 | |
| 3166 | return ref; |
| 3167 | } |
| 3168 | \f |
| 3169 | /* Make sure that the tag NAME is defined *in the current binding level* |
| 3170 | at least as a forward reference. |
| 3171 | CODE says which kind of tag NAME ought to be. */ |
| 3172 | |
| 3173 | tree |
| 3174 | start_struct (code, name) |
| 3175 | enum tree_code code; |
| 3176 | tree name; |
| 3177 | { |
| 3178 | /* If there is already a tag defined at this binding level |
| 3179 | (as a forward reference), just return it. */ |
| 3180 | |
| 3181 | register tree ref = 0; |
| 3182 | |
| 3183 | if (name != 0) |
| 3184 | ref = lookup_tag (code, name, current_binding_level, 1); |
| 3185 | if (ref && TREE_CODE (ref) == code) |
| 3186 | { |
| 3187 | if (TYPE_FIELDS (ref)) |
| 3188 | error ((code == UNION_TYPE ? "redefinition of `union %s'" |
| 3189 | : "redefinition of `struct %s'"), |
| 3190 | IDENTIFIER_POINTER (name)); |
| 3191 | |
| 3192 | return ref; |
| 3193 | } |
| 3194 | |
| 3195 | /* Otherwise create a forward-reference just so the tag is in scope. */ |
| 3196 | |
| 3197 | ref = make_node (code); |
| 3198 | pushtag (name, ref); |
| 3199 | return ref; |
| 3200 | } |
| 3201 | |
| 3202 | /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted) |
| 3203 | of a structure component, returning a FIELD_DECL node. |
| 3204 | WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node. |
| 3205 | |
| 3206 | This is done during the parsing of the struct declaration. |
| 3207 | The FIELD_DECL nodes are chained together and the lot of them |
| 3208 | are ultimately passed to `build_struct' to make the RECORD_TYPE node. */ |
| 3209 | |
| 3210 | tree |
| 3211 | grokfield (filename, line, declarator, declspecs, width) |
| 3212 | char *filename; |
| 3213 | int line; |
| 3214 | tree declarator, declspecs, width; |
| 3215 | { |
| 3216 | register tree value = grokdeclarator (declarator, declspecs, FIELD, 0); |
| 3217 | |
| 3218 | finish_decl (value, NULL, NULL); |
| 3219 | DECL_INITIAL (value) = width; |
| 3220 | |
| 3221 | return value; |
| 3222 | } |
| 3223 | \f |
| 3224 | /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T. |
| 3225 | FIELDLIST is a chain of FIELD_DECL nodes for the fields. */ |
| 3226 | |
| 3227 | tree |
| 3228 | finish_struct (t, fieldlist) |
| 3229 | register tree t, fieldlist; |
| 3230 | { |
| 3231 | register tree x; |
| 3232 | int old_momentary; |
| 3233 | int round_up_size = 1; |
| 3234 | |
| 3235 | /* If this type was previously laid out as a forward reference, |
| 3236 | make sure we lay it out again. */ |
| 3237 | |
| 3238 | TYPE_SIZE (t) = 0; |
| 3239 | |
| 3240 | if (extra_warnings && in_parm_level_p ()) |
| 3241 | warning ((TREE_CODE (t) == UNION_TYPE ? "union defined inside parms" |
| 3242 | : "structure defined inside parms")); |
| 3243 | |
| 3244 | old_momentary = suspend_momentary (); |
| 3245 | |
| 3246 | if (fieldlist == 0 && pedantic) |
| 3247 | warning ((TREE_CODE (t) == UNION_TYPE ? "union has no members" |
| 3248 | : "structure has no members")); |
| 3249 | |
| 3250 | /* Install struct as DECL_CONTEXT of each field decl. |
| 3251 | Also process specified field sizes. |
| 3252 | Set DECL_SIZE_UNIT to the specified size, or 0 if none specified. |
| 3253 | The specified size is found in the DECL_INITIAL. |
| 3254 | Store 0 there, except for ": 0" fields (so we can find them |
| 3255 | and delete them, below). */ |
| 3256 | |
| 3257 | for (x = fieldlist; x; x = TREE_CHAIN (x)) |
| 3258 | { |
| 3259 | DECL_CONTEXT (x) = t; |
| 3260 | DECL_SIZE_UNIT (x) = 0; |
| 3261 | |
| 3262 | /* If any field is const, the structure type is pseudo-const. */ |
| 3263 | if (TREE_READONLY (x)) |
| 3264 | C_TYPE_FIELDS_READONLY (t) = 1; |
| 3265 | else |
| 3266 | { |
| 3267 | /* A field that is pseudo-const makes the structure likewise. */ |
| 3268 | tree t1 = TREE_TYPE (x); |
| 3269 | while (TREE_CODE (t1) == ARRAY_TYPE) |
| 3270 | t1 = TREE_TYPE (t1); |
| 3271 | if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE) |
| 3272 | && C_TYPE_FIELDS_READONLY (t1)) |
| 3273 | C_TYPE_FIELDS_READONLY (t) = 1; |
| 3274 | } |
| 3275 | |
| 3276 | /* Detect invalid bit-field size. */ |
| 3277 | if (DECL_INITIAL (x) && TREE_CODE (DECL_INITIAL (x)) != INTEGER_CST) |
| 3278 | { |
| 3279 | error_with_decl (x, "bit-field `%s' width not an integer constant"); |
| 3280 | DECL_INITIAL (x) = NULL; |
| 3281 | } |
| 3282 | |
| 3283 | /* Detect invalid bit-field type. */ |
| 3284 | if (DECL_INITIAL (x) |
| 3285 | && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE |
| 3286 | && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE) |
| 3287 | { |
| 3288 | error_with_decl (x, "bit-field `%s' has invalid type"); |
| 3289 | DECL_INITIAL (x) = NULL; |
| 3290 | } |
| 3291 | if (DECL_INITIAL (x) && pedantic |
| 3292 | && TREE_TYPE (x) != integer_type_node |
| 3293 | && TREE_TYPE (x) != unsigned_type_node) |
| 3294 | warning_with_decl (x, "bit-field `%s' type invalid in ANSI C"); |
| 3295 | |
| 3296 | /* Detect and ignore out of range field width. */ |
| 3297 | if (DECL_INITIAL (x)) |
| 3298 | { |
| 3299 | register int width = TREE_INT_CST_LOW (DECL_INITIAL (x)); |
| 3300 | |
| 3301 | if (width < 0) |
| 3302 | { |
| 3303 | DECL_INITIAL (x) = NULL; |
| 3304 | warning_with_decl (x, "negative width in bit-field `%s'"); |
| 3305 | } |
| 3306 | else if (width == 0 && DECL_NAME (x) != 0) |
| 3307 | { |
| 3308 | error_with_decl (x, "zero width for bit-field `%s'"); |
| 3309 | DECL_INITIAL (x) = NULL; |
| 3310 | } |
| 3311 | else if (width > TYPE_PRECISION (TREE_TYPE (x))) |
| 3312 | { |
| 3313 | DECL_INITIAL (x) = NULL; |
| 3314 | warning_with_decl (x, "width of `%s' exceeds its type"); |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | /* Process valid field width. */ |
| 3319 | if (DECL_INITIAL (x)) |
| 3320 | { |
| 3321 | register int width = TREE_INT_CST_LOW (DECL_INITIAL (x)); |
| 3322 | |
| 3323 | if (width == 0) |
| 3324 | { |
| 3325 | /* field size 0 => mark following field as "aligned" */ |
| 3326 | if (TREE_CHAIN (x)) |
| 3327 | DECL_ALIGN (TREE_CHAIN (x)) |
| 3328 | = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY); |
| 3329 | /* field of size 0 at the end => round up the size. */ |
| 3330 | else |
| 3331 | round_up_size = EMPTY_FIELD_BOUNDARY; |
| 3332 | } |
| 3333 | else |
| 3334 | { |
| 3335 | DECL_INITIAL (x) = NULL; |
| 3336 | DECL_SIZE_UNIT (x) = width; |
| 3337 | TREE_PACKED (x) = 1; |
| 3338 | /* Traditionally a bit field is unsigned |
| 3339 | even if declared signed. */ |
| 3340 | if (flag_traditional |
| 3341 | && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE) |
| 3342 | TREE_TYPE (x) = unsigned_type_node; |
| 3343 | } |
| 3344 | } |
| 3345 | else |
| 3346 | /* Non-bit-fields are aligned for their type. */ |
| 3347 | DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x))); |
| 3348 | } |
| 3349 | |
| 3350 | /* Now DECL_INITIAL is null on all members except for zero-width bit-fields. |
| 3351 | And they have already done their work. */ |
| 3352 | |
| 3353 | /* Delete all zero-width bit-fields from the front of the fieldlist */ |
| 3354 | while (fieldlist |
| 3355 | && DECL_INITIAL (fieldlist)) |
| 3356 | fieldlist = TREE_CHAIN (fieldlist); |
| 3357 | /* Delete all such members from the rest of the fieldlist */ |
| 3358 | for (x = fieldlist; x;) |
| 3359 | { |
| 3360 | if (TREE_CHAIN (x) && DECL_INITIAL (TREE_CHAIN (x))) |
| 3361 | TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x)); |
| 3362 | else x = TREE_CHAIN (x); |
| 3363 | } |
| 3364 | |
| 3365 | /* Delete all duplicate fields from the fieldlist */ |
| 3366 | for (x = fieldlist; x && TREE_CHAIN (x);) |
| 3367 | /* Anonymous fields aren't duplicates. */ |
| 3368 | if (DECL_NAME (TREE_CHAIN (x)) == 0) |
| 3369 | x = TREE_CHAIN (x); |
| 3370 | else |
| 3371 | { |
| 3372 | register tree y = fieldlist; |
| 3373 | |
| 3374 | while (1) |
| 3375 | { |
| 3376 | if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x))) |
| 3377 | break; |
| 3378 | if (y == x) |
| 3379 | break; |
| 3380 | y = TREE_CHAIN (y); |
| 3381 | } |
| 3382 | if (DECL_NAME (y) == DECL_NAME (TREE_CHAIN (x))) |
| 3383 | { |
| 3384 | error_with_decl (TREE_CHAIN (x), "duplicate member `%s'"); |
| 3385 | TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x)); |
| 3386 | } |
| 3387 | else x = TREE_CHAIN (x); |
| 3388 | } |
| 3389 | |
| 3390 | /* Now we have the final fieldlist. Record it, |
| 3391 | then lay out the structure or union (including the fields). */ |
| 3392 | |
| 3393 | TYPE_FIELDS (t) = fieldlist; |
| 3394 | |
| 3395 | /* If there's a :0 field at the end, round the size to the |
| 3396 | EMPTY_FIELD_BOUNDARY. */ |
| 3397 | TYPE_ALIGN (t) = round_up_size; |
| 3398 | |
| 3399 | for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) |
| 3400 | { |
| 3401 | TYPE_FIELDS (x) = TYPE_FIELDS (t); |
| 3402 | TYPE_ALIGN (x) = TYPE_ALIGN (t); |
| 3403 | } |
| 3404 | |
| 3405 | layout_type (t); |
| 3406 | |
| 3407 | /* Promote each bit-field's type to int if it is narrower than that. */ |
| 3408 | for (x = fieldlist; x; x = TREE_CHAIN (x)) |
| 3409 | if (TREE_PACKED (x) |
| 3410 | && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE |
| 3411 | && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x) |
| 3412 | < TYPE_PRECISION (integer_type_node))) |
| 3413 | TREE_TYPE (x) = integer_type_node; |
| 3414 | |
| 3415 | /* If this structure or union completes the type of any previous |
| 3416 | variable declaration, lay it out and output its rtl. */ |
| 3417 | |
| 3418 | if (current_binding_level->n_incomplete != 0) |
| 3419 | { |
| 3420 | tree decl; |
| 3421 | for (decl = current_binding_level->names; decl; decl = TREE_CHAIN (decl)) |
| 3422 | { |
| 3423 | if (TREE_TYPE (decl) == t |
| 3424 | && TREE_CODE (decl) != TYPE_DECL) |
| 3425 | { |
| 3426 | int toplevel = global_binding_level == current_binding_level; |
| 3427 | layout_decl (decl, 0); |
| 3428 | rest_of_decl_compilation (decl, 0, toplevel, 0); |
| 3429 | if (! toplevel) |
| 3430 | expand_decl (decl, NULL_TREE); |
| 3431 | --current_binding_level->n_incomplete; |
| 3432 | } |
| 3433 | else if (TYPE_SIZE (TREE_TYPE (decl)) == 0 |
| 3434 | && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) |
| 3435 | { |
| 3436 | tree element = TREE_TYPE (decl); |
| 3437 | while (TREE_CODE (element) == ARRAY_TYPE) |
| 3438 | element = TREE_TYPE (element); |
| 3439 | if (element == t) |
| 3440 | layout_array_type (TREE_TYPE (decl)); |
| 3441 | } |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | resume_momentary (old_momentary); |
| 3446 | |
| 3447 | return t; |
| 3448 | } |
| 3449 | |
| 3450 | /* Lay out the type T, and its element type, and so on. */ |
| 3451 | |
| 3452 | static void |
| 3453 | layout_array_type (t) |
| 3454 | tree t; |
| 3455 | { |
| 3456 | if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) |
| 3457 | layout_array_type (TREE_TYPE (t)); |
| 3458 | layout_type (t); |
| 3459 | } |
| 3460 | \f |
| 3461 | /* Begin compiling the definition of an enumeration type. |
| 3462 | NAME is its name (or null if anonymous). |
| 3463 | Returns the type object, as yet incomplete. |
| 3464 | Also records info about it so that build_enumerator |
| 3465 | may be used to declare the individual values as they are read. */ |
| 3466 | |
| 3467 | tree |
| 3468 | start_enum (name) |
| 3469 | tree name; |
| 3470 | { |
| 3471 | register tree enumtype = 0; |
| 3472 | |
| 3473 | /* If this is the real definition for a previous forward reference, |
| 3474 | fill in the contents in the same object that used to be the |
| 3475 | forward reference. */ |
| 3476 | |
| 3477 | if (name != 0) |
| 3478 | enumtype = lookup_tag (ENUMERAL_TYPE, name, current_binding_level, 1); |
| 3479 | |
| 3480 | if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE) |
| 3481 | { |
| 3482 | enumtype = make_node (ENUMERAL_TYPE); |
| 3483 | pushtag (name, enumtype); |
| 3484 | } |
| 3485 | |
| 3486 | if (TYPE_VALUES (enumtype) != 0) |
| 3487 | { |
| 3488 | /* This enum is a named one that has been declared already. */ |
| 3489 | error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name)); |
| 3490 | |
| 3491 | /* Completely replace its old definition. |
| 3492 | The old enumerators remain defined, however. */ |
| 3493 | TYPE_VALUES (enumtype) = 0; |
| 3494 | } |
| 3495 | |
| 3496 | /* Initially, set up this enum as like `int' |
| 3497 | so that we can create the enumerators' declarations and values. |
| 3498 | Later on, the precision of the type may be changed and |
| 3499 | it may be laid out again. */ |
| 3500 | |
| 3501 | TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node); |
| 3502 | TYPE_SIZE (enumtype) = 0; |
| 3503 | fixup_unsigned_type (enumtype); |
| 3504 | |
| 3505 | enum_next_value = integer_zero_node; |
| 3506 | |
| 3507 | return enumtype; |
| 3508 | } |
| 3509 | |
| 3510 | /* After processing and defining all the values of an enumeration type, |
| 3511 | install their decls in the enumeration type and finish it off. |
| 3512 | ENUMTYPE is the type object and VALUES a list of name-value pairs. |
| 3513 | Returns ENUMTYPE. */ |
| 3514 | |
| 3515 | tree |
| 3516 | finish_enum (enumtype, values) |
| 3517 | register tree enumtype, values; |
| 3518 | { |
| 3519 | register tree pair; |
| 3520 | register long maxvalue = 0; |
| 3521 | register long minvalue = 0; |
| 3522 | register int i; |
| 3523 | |
| 3524 | if (in_parm_level_p ()) |
| 3525 | warning ("enum defined inside parms"); |
| 3526 | |
| 3527 | TYPE_VALUES (enumtype) = values; |
| 3528 | |
| 3529 | /* Calculate the maximum value of any enumerator in this type. */ |
| 3530 | |
| 3531 | for (pair = values; pair; pair = TREE_CHAIN (pair)) |
| 3532 | { |
| 3533 | int value = TREE_INT_CST_LOW (TREE_VALUE (pair)); |
| 3534 | if (pair == values) |
| 3535 | minvalue = maxvalue = value; |
| 3536 | else |
| 3537 | { |
| 3538 | if (value > maxvalue) |
| 3539 | maxvalue = value; |
| 3540 | if (value < minvalue) |
| 3541 | minvalue = value; |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | if (flag_short_enums) |
| 3546 | { |
| 3547 | /* Determine the precision this type needs, lay it out, and define it. */ |
| 3548 | |
| 3549 | for (i = maxvalue; i; i >>= 1) |
| 3550 | TYPE_PRECISION (enumtype)++; |
| 3551 | |
| 3552 | if (!TYPE_PRECISION (enumtype)) |
| 3553 | TYPE_PRECISION (enumtype) = 1; |
| 3554 | |
| 3555 | /* Cancel the laying out previously done for the enum type, |
| 3556 | so that fixup_unsigned_type will do it over. */ |
| 3557 | TYPE_SIZE (enumtype) = 0; |
| 3558 | |
| 3559 | fixup_unsigned_type (enumtype); |
| 3560 | } |
| 3561 | |
| 3562 | TREE_INT_CST_LOW (TYPE_MAX_VALUE (enumtype)) = maxvalue; |
| 3563 | |
| 3564 | /* An enum can have some negative values; then it is signed. */ |
| 3565 | if (minvalue < 0) |
| 3566 | { |
| 3567 | TREE_INT_CST_LOW (TYPE_MIN_VALUE (enumtype)) = minvalue; |
| 3568 | TREE_INT_CST_HIGH (TYPE_MIN_VALUE (enumtype)) = -1; |
| 3569 | TREE_UNSIGNED (enumtype) = 0; |
| 3570 | } |
| 3571 | return enumtype; |
| 3572 | } |
| 3573 | |
| 3574 | /* Build and install a CONST_DECL for one value of the |
| 3575 | current enumeration type (one that was begun with start_enum). |
| 3576 | Return a tree-list containing the name and its value. |
| 3577 | Assignment of sequential values by default is handled here. */ |
| 3578 | |
| 3579 | tree |
| 3580 | build_enumerator (name, value) |
| 3581 | tree name, value; |
| 3582 | { |
| 3583 | register tree decl; |
| 3584 | |
| 3585 | /* Validate and default VALUE. */ |
| 3586 | |
| 3587 | /* Remove no-op casts from the value. */ |
| 3588 | while (value != 0 && TREE_CODE (value) == NOP_EXPR) |
| 3589 | value = TREE_OPERAND (value, 0); |
| 3590 | |
| 3591 | if (value != 0 && TREE_CODE (value) != INTEGER_CST) |
| 3592 | { |
| 3593 | error ("enumerator value for `%s' not integer constant", |
| 3594 | IDENTIFIER_POINTER (name)); |
| 3595 | value = 0; |
| 3596 | } |
| 3597 | |
| 3598 | /* Default based on previous value. */ |
| 3599 | if (value == 0) |
| 3600 | value = enum_next_value; |
| 3601 | |
| 3602 | /* Might as well enforce the ANSI restriction, since |
| 3603 | values outside this range don't work in version 1. */ |
| 3604 | if (! int_fits_type_p (value, integer_type_node)) |
| 3605 | { |
| 3606 | error ("enumerator value outside range of `int'"); |
| 3607 | value = integer_zero_node; |
| 3608 | } |
| 3609 | |
| 3610 | /* Set basis for default for next value. */ |
| 3611 | enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value, |
| 3612 | integer_one_node, PLUS_EXPR); |
| 3613 | |
| 3614 | /* Now create a declaration for the enum value name. */ |
| 3615 | |
| 3616 | decl = build_decl (CONST_DECL, name, integer_type_node); |
| 3617 | DECL_INITIAL (decl) = value; |
| 3618 | TREE_TYPE (value) = integer_type_node; |
| 3619 | pushdecl (decl); |
| 3620 | |
| 3621 | return saveable_tree_cons (name, value, NULL); |
| 3622 | } |
| 3623 | \f |
| 3624 | /* Create the FUNCTION_DECL for a function definition. |
| 3625 | DECLSPECS and DECLARATOR are the parts of the declaration; |
| 3626 | they describe the function's name and the type it returns, |
| 3627 | but twisted together in a fashion that parallels the syntax of C. |
| 3628 | |
| 3629 | This function creates a binding context for the function body |
| 3630 | as well as setting up the FUNCTION_DECL in current_function_decl. |
| 3631 | |
| 3632 | Returns 1 on success. If the DECLARATOR is not suitable for a function |
| 3633 | (it defines a datum instead), we return 0, which tells |
| 3634 | yyparse to report a parse error. */ |
| 3635 | |
| 3636 | int |
| 3637 | start_function (declspecs, declarator) |
| 3638 | tree declarator, declspecs; |
| 3639 | { |
| 3640 | tree decl1, old_decl; |
| 3641 | tree restype; |
| 3642 | |
| 3643 | current_function_returns_value = 0; /* Assume, until we see it does. */ |
| 3644 | current_function_returns_null = 0; |
| 3645 | warn_about_return_type = 0; |
| 3646 | current_extern_inline = 0; |
| 3647 | |
| 3648 | decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1); |
| 3649 | |
| 3650 | /* If the declarator is not suitable for a function definition, |
| 3651 | cause a syntax error. */ |
| 3652 | if (decl1 == 0) |
| 3653 | return 0; |
| 3654 | |
| 3655 | current_function_decl = decl1; |
| 3656 | |
| 3657 | announce_function (current_function_decl); |
| 3658 | |
| 3659 | if (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl1))) == 0) |
| 3660 | { |
| 3661 | error ("return-type is an incomplete type"); |
| 3662 | /* Make it return void instead. */ |
| 3663 | TREE_TYPE (decl1) |
| 3664 | = build_function_type (void_type_node, |
| 3665 | TYPE_ARG_TYPES (TREE_TYPE (decl1))); |
| 3666 | } |
| 3667 | |
| 3668 | if (warn_about_return_type) |
| 3669 | warning ("return-type defaults to `int'"); |
| 3670 | |
| 3671 | /* Save the parm names or decls from this function's declarator |
| 3672 | where store_parm_decls will find them. */ |
| 3673 | current_function_parms = last_function_parms; |
| 3674 | current_function_parm_tags = last_function_parm_tags; |
| 3675 | |
| 3676 | /* Make the init_value nonzero so pushdecl knows this is not tentative. |
| 3677 | error_mark_node is replaced below (in poplevel) with the LET_STMT. */ |
| 3678 | DECL_INITIAL (current_function_decl) = error_mark_node; |
| 3679 | |
| 3680 | /* If this definition isn't a prototype and we had a prototype declaration |
| 3681 | before, copy the arg type info from that prototype. */ |
| 3682 | old_decl = lookup_name_current_level (DECL_NAME (current_function_decl)); |
| 3683 | if (old_decl != 0 |
| 3684 | && TREE_TYPE (TREE_TYPE (current_function_decl)) == TREE_TYPE (TREE_TYPE (old_decl)) |
| 3685 | && TYPE_ARG_TYPES (TREE_TYPE (current_function_decl)) == 0) |
| 3686 | TREE_TYPE (current_function_decl) = TREE_TYPE (old_decl); |
| 3687 | |
| 3688 | /* This is a definition, not a reference. |
| 3689 | So normally clear TREE_EXTERNAL. |
| 3690 | However, `extern inline' acts like a declaration |
| 3691 | except for defining how to inline. So set TREE_EXTERNAL in that case. */ |
| 3692 | TREE_EXTERNAL (current_function_decl) = current_extern_inline; |
| 3693 | |
| 3694 | /* This function exists in static storage. |
| 3695 | (This does not mean `static' in the C sense!) */ |
| 3696 | TREE_STATIC (current_function_decl) = 1; |
| 3697 | |
| 3698 | /* Record the decl so that the function name is defined. |
| 3699 | If we already have a decl for this name, and it is a FUNCTION_DECL, |
| 3700 | use the old decl. */ |
| 3701 | |
| 3702 | current_function_decl = pushdecl (current_function_decl); |
| 3703 | |
| 3704 | pushlevel (0); |
| 3705 | declare_parm_level (); |
| 3706 | |
| 3707 | make_function_rtl (current_function_decl); |
| 3708 | |
| 3709 | restype = TREE_TYPE (TREE_TYPE (current_function_decl)); |
| 3710 | /* Promote the value to int before returning it. */ |
| 3711 | if (TREE_CODE (restype) == INTEGER_TYPE |
| 3712 | && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node)) |
| 3713 | restype = integer_type_node; |
| 3714 | DECL_RESULT_TYPE (current_function_decl) = restype; |
| 3715 | DECL_RESULT (current_function_decl) |
| 3716 | = build_decl (RESULT_DECL, value_identifier, restype); |
| 3717 | |
| 3718 | /* Allocate further tree nodes temporarily during compilation |
| 3719 | of this function only. */ |
| 3720 | temporary_allocation (); |
| 3721 | |
| 3722 | /* If this fcn was already referenced via a block-scope `extern' decl |
| 3723 | (or an implicit decl), propagate certain information about the usage. */ |
| 3724 | if (TREE_ADDRESSABLE (DECL_NAME (current_function_decl))) |
| 3725 | TREE_ADDRESSABLE (current_function_decl) = 1; |
| 3726 | |
| 3727 | return 1; |
| 3728 | } |
| 3729 | \f |
| 3730 | /* Store the parameter declarations into the current function declaration. |
| 3731 | This is called after parsing the parameter declarations, before |
| 3732 | digesting the body of the function. */ |
| 3733 | |
| 3734 | void |
| 3735 | store_parm_decls () |
| 3736 | { |
| 3737 | register tree fndecl = current_function_decl; |
| 3738 | register tree parm; |
| 3739 | |
| 3740 | /* This is either a chain of PARM_DECLs (if a prototype was used) |
| 3741 | or a list of IDENTIFIER_NODEs (for an old-fashioned C definition). */ |
| 3742 | tree specparms = current_function_parms; |
| 3743 | |
| 3744 | /* This is a list of types declared among parms in a prototype. */ |
| 3745 | tree parmtags = current_function_parm_tags; |
| 3746 | |
| 3747 | /* This is a chain of PARM_DECLs from old-style parm declarations. */ |
| 3748 | register tree parmdecls = getdecls (); |
| 3749 | |
| 3750 | /* This is a chain of any other decls that came in among the parm |
| 3751 | declarations. If a parm is declared with enum {foo, bar} x; |
| 3752 | then CONST_DECLs for foo and bar are put here. */ |
| 3753 | tree nonparms = 0; |
| 3754 | |
| 3755 | if (specparms != 0 && TREE_CODE (specparms) != TREE_LIST) |
| 3756 | { |
| 3757 | /* This case is when the function was defined with an ANSI prototype. |
| 3758 | The parms already have decls, so we need not do anything here |
| 3759 | except record them as in effect |
| 3760 | and complain if any redundant old-style parm decls were written. */ |
| 3761 | |
| 3762 | register tree next; |
| 3763 | tree others = 0; |
| 3764 | |
| 3765 | if (parmdecls != 0) |
| 3766 | error_with_decl (fndecl, |
| 3767 | "parm types given both in parmlist and separately"); |
| 3768 | |
| 3769 | specparms = nreverse (specparms); |
| 3770 | for (parm = specparms; parm; parm = next) |
| 3771 | { |
| 3772 | next = TREE_CHAIN (parm); |
| 3773 | if (DECL_NAME (parm) == 0) |
| 3774 | error_with_decl (parm, "parameter name omitted"); |
| 3775 | else if (TREE_TYPE (parm) == void_type_node) |
| 3776 | error_with_decl (parm, "parameter `%s' declared void"); |
| 3777 | else if (TREE_CODE (parm) == PARM_DECL) |
| 3778 | pushdecl (parm); |
| 3779 | else |
| 3780 | { |
| 3781 | /* If we find an enum constant, put it aside for the moment. */ |
| 3782 | TREE_CHAIN (parm) = 0; |
| 3783 | others = chainon (others, parm); |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | /* Get the decls in their original chain order |
| 3788 | and record in the function. */ |
| 3789 | DECL_ARGUMENTS (fndecl) = getdecls (); |
| 3790 | |
| 3791 | /* Now pushdecl the enum constants. */ |
| 3792 | for (parm = others; parm; parm = next) |
| 3793 | { |
| 3794 | next = TREE_CHAIN (parm); |
| 3795 | if (DECL_NAME (parm) == 0) |
| 3796 | ; |
| 3797 | else if (TREE_TYPE (parm) == void_type_node) |
| 3798 | ; |
| 3799 | else if (TREE_CODE (parm) != PARM_DECL) |
| 3800 | pushdecl (parm); |
| 3801 | } |
| 3802 | |
| 3803 | storetags (chainon (parmtags, gettags ())); |
| 3804 | } |
| 3805 | else |
| 3806 | { |
| 3807 | /* SPECPARMS is an identifier list--a chain of TREE_LIST nodes |
| 3808 | each with a parm name as the TREE_VALUE. |
| 3809 | |
| 3810 | PARMDECLS is a list of declarations for parameters. |
| 3811 | Warning! It can also contain CONST_DECLs which are not parameters |
| 3812 | but are names of enumerators of any enum types |
| 3813 | declared among the parameters. |
| 3814 | |
| 3815 | First match each formal parameter name with its declaration. |
| 3816 | Associate decls with the names and store the decls |
| 3817 | into the TREE_PURPOSE slots. */ |
| 3818 | |
| 3819 | for (parm = specparms; parm; parm = TREE_CHAIN (parm)) |
| 3820 | { |
| 3821 | register tree tail, found = NULL; |
| 3822 | |
| 3823 | if (TREE_VALUE (parm) == 0) |
| 3824 | { |
| 3825 | error_with_decl (fndecl, "parameter name missing from parameter list"); |
| 3826 | TREE_PURPOSE (parm) = 0; |
| 3827 | continue; |
| 3828 | } |
| 3829 | |
| 3830 | /* See if any of the parmdecls specifies this parm by name. |
| 3831 | Ignore any enumerator decls. */ |
| 3832 | for (tail = parmdecls; tail; tail = TREE_CHAIN (tail)) |
| 3833 | if (DECL_NAME (tail) == TREE_VALUE (parm) |
| 3834 | && TREE_CODE (tail) == PARM_DECL) |
| 3835 | { |
| 3836 | found = tail; |
| 3837 | break; |
| 3838 | } |
| 3839 | |
| 3840 | /* If declaration already marked, we have a duplicate name. |
| 3841 | Complain, and don't use this decl twice. */ |
| 3842 | if (found && DECL_CONTEXT (found) != 0) |
| 3843 | { |
| 3844 | error_with_decl (found, "multiple parameters named `%s'"); |
| 3845 | found = 0; |
| 3846 | } |
| 3847 | |
| 3848 | /* If the declaration says "void", complain and ignore it. */ |
| 3849 | if (found && TREE_TYPE (found) == void_type_node) |
| 3850 | { |
| 3851 | error_with_decl (found, "parameter `%s' declared void"); |
| 3852 | TREE_TYPE (found) = integer_type_node; |
| 3853 | DECL_ARG_TYPE (found) = integer_type_node; |
| 3854 | layout_decl (found, 0); |
| 3855 | } |
| 3856 | |
| 3857 | /* If no declaration found, default to int. */ |
| 3858 | if (!found) |
| 3859 | { |
| 3860 | found = build_decl (PARM_DECL, TREE_VALUE (parm), |
| 3861 | integer_type_node); |
| 3862 | DECL_ARG_TYPE (found) = TREE_TYPE (found); |
| 3863 | DECL_SOURCE_LINE (found) = DECL_SOURCE_LINE (fndecl); |
| 3864 | DECL_SOURCE_FILE (found) = DECL_SOURCE_FILE (fndecl); |
| 3865 | if (extra_warnings) |
| 3866 | warning_with_decl (found, "type of `%s' defaults to `int'"); |
| 3867 | pushdecl (found); |
| 3868 | } |
| 3869 | |
| 3870 | TREE_PURPOSE (parm) = found; |
| 3871 | |
| 3872 | /* Mark this decl as "already found" -- see test, above. |
| 3873 | It is safe to clobber DECL_CONTEXT temporarily |
| 3874 | because the final values are not stored until |
| 3875 | the `poplevel' in `finish_function'. */ |
| 3876 | DECL_CONTEXT (found) = error_mark_node; |
| 3877 | } |
| 3878 | |
| 3879 | /* Complain about declarations not matched with any names. |
| 3880 | Put any enumerator constants onto the list NONPARMS. */ |
| 3881 | |
| 3882 | nonparms = 0; |
| 3883 | for (parm = parmdecls; parm; ) |
| 3884 | { |
| 3885 | tree next = TREE_CHAIN (parm); |
| 3886 | TREE_CHAIN (parm) = 0; |
| 3887 | |
| 3888 | /* Complain about args with incomplete types. */ |
| 3889 | if (TYPE_SIZE (TREE_TYPE (parm)) == 0) |
| 3890 | { |
| 3891 | error_with_decl (parm, "parameter `%s' has incomplete type"); |
| 3892 | TREE_TYPE (parm) = error_mark_node; |
| 3893 | } |
| 3894 | |
| 3895 | if (TREE_CODE (parm) != PARM_DECL) |
| 3896 | nonparms = chainon (nonparms, parm); |
| 3897 | |
| 3898 | else if (DECL_CONTEXT (parm) == 0) |
| 3899 | { |
| 3900 | error_with_decl (parm, |
| 3901 | "declaration for parameter `%s' but no such parameter"); |
| 3902 | /* Pretend the parameter was not missing. |
| 3903 | This gets us to a standard state and minimizes |
| 3904 | further error messages. */ |
| 3905 | specparms |
| 3906 | = chainon (specparms, |
| 3907 | tree_cons (parm, NULL_TREE, NULL_TREE)); |
| 3908 | } |
| 3909 | |
| 3910 | parm = next; |
| 3911 | } |
| 3912 | |
| 3913 | /* Chain the declarations together in the order of the list of names. */ |
| 3914 | /* Store that chain in the function decl, replacing the list of names. */ |
| 3915 | parm = specparms; |
| 3916 | DECL_ARGUMENTS (fndecl) = 0; |
| 3917 | { |
| 3918 | register tree last; |
| 3919 | for (last = 0; parm; parm = TREE_CHAIN (parm)) |
| 3920 | if (TREE_PURPOSE (parm)) |
| 3921 | { |
| 3922 | if (last == 0) |
| 3923 | DECL_ARGUMENTS (fndecl) = TREE_PURPOSE (parm); |
| 3924 | else |
| 3925 | TREE_CHAIN (last) = TREE_PURPOSE (parm); |
| 3926 | last = TREE_PURPOSE (parm); |
| 3927 | TREE_CHAIN (last) = 0; |
| 3928 | DECL_CONTEXT (last) = 0; |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | /* If there was a previous prototype, |
| 3933 | set the DECL_ARG_TYPE of each argument according to |
| 3934 | the type previously specified, and report any mismatches. */ |
| 3935 | |
| 3936 | if (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) |
| 3937 | { |
| 3938 | register tree type; |
| 3939 | for (parm = DECL_ARGUMENTS (fndecl), |
| 3940 | type = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); |
| 3941 | parm || (type && TREE_VALUE (type) != void_type_node); |
| 3942 | parm = TREE_CHAIN (parm), type = TREE_CHAIN (type)) |
| 3943 | { |
| 3944 | if (parm == 0 || type == 0 |
| 3945 | || TREE_VALUE (type) == void_type_node) |
| 3946 | { |
| 3947 | error ("number of arguments doesn't match prototype"); |
| 3948 | break; |
| 3949 | } |
| 3950 | /* Type for passing arg must be consistent |
| 3951 | with that declared for the arg. */ |
| 3952 | if (! comptypes (DECL_ARG_TYPE (parm), TREE_VALUE (type))) |
| 3953 | { |
| 3954 | error ("argument `%s' doesn't match function prototype", |
| 3955 | IDENTIFIER_POINTER (DECL_NAME (parm))); |
| 3956 | if (DECL_ARG_TYPE (parm) == integer_type_node |
| 3957 | && TREE_VALUE (type) == TREE_TYPE (parm)) |
| 3958 | { |
| 3959 | error ("a formal parameter type that promotes to `int'"); |
| 3960 | error ("can match only `int' in the prototype"); |
| 3961 | } |
| 3962 | if (DECL_ARG_TYPE (parm) == double_type_node |
| 3963 | && TREE_VALUE (type) == TREE_TYPE (parm)) |
| 3964 | { |
| 3965 | error ("a formal parameter type that promotes to `double'"); |
| 3966 | error ("can match only `double' in the prototype"); |
| 3967 | } |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | /* Now store the final chain of decls for the arguments |
| 3973 | as the decl-chain of the current lexical scope. |
| 3974 | Put the enumerators in as well, at the front so that |
| 3975 | DECL_ARGUMENTS is not modified. */ |
| 3976 | |
| 3977 | storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl))); |
| 3978 | } |
| 3979 | |
| 3980 | /* Make sure the binding level for the top of the function body |
| 3981 | gets a LET_STMT if there are any in the function. |
| 3982 | Otherwise, the dbx output is wrong. */ |
| 3983 | |
| 3984 | keep_next_if_subblocks = 1; |
| 3985 | |
| 3986 | /* Initialize the RTL code for the function. */ |
| 3987 | |
| 3988 | init_function_start (fndecl, input_filename, lineno); |
| 3989 | |
| 3990 | /* Set up parameters and prepare for return, for the function. */ |
| 3991 | |
| 3992 | expand_function_start (fndecl, 0); |
| 3993 | } |
| 3994 | \f |
| 3995 | /* Finish up a function declaration and compile that function |
| 3996 | all the way to assembler language output. The free the storage |
| 3997 | for the function definition. |
| 3998 | |
| 3999 | This is called after parsing the body of the function definition. |
| 4000 | LINENO is the current line number. */ |
| 4001 | |
| 4002 | void |
| 4003 | finish_function (lineno) |
| 4004 | int lineno; |
| 4005 | { |
| 4006 | register tree fndecl = current_function_decl; |
| 4007 | |
| 4008 | /* TREE_READONLY (fndecl) = 1; |
| 4009 | This caused &foo to be of type ptr-to-const-function |
| 4010 | which then got a warning when stored in a ptr-to-function variable. */ |
| 4011 | |
| 4012 | poplevel (1, 0, 1); |
| 4013 | |
| 4014 | /* Must mark the RESULT_DECL as being in this function. */ |
| 4015 | |
| 4016 | DECL_CONTEXT (DECL_RESULT (fndecl)) = DECL_INITIAL (fndecl); |
| 4017 | |
| 4018 | /* Obey `register' declarations if `setjmp' is called in this fn. */ |
| 4019 | if (flag_traditional && current_function_calls_setjmp) |
| 4020 | setjmp_protect (DECL_INITIAL (fndecl)); |
| 4021 | |
| 4022 | /* Generate rtl for function exit. */ |
| 4023 | expand_function_end (input_filename, lineno); |
| 4024 | |
| 4025 | /* So we can tell if jump_optimize sets it to 1. */ |
| 4026 | current_function_returns_null = 0; |
| 4027 | |
| 4028 | /* Run the optimizers and output the assembler code for this function. */ |
| 4029 | rest_of_compilation (fndecl); |
| 4030 | |
| 4031 | if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null) |
| 4032 | warning ("`volatile' function does return"); |
| 4033 | else if (warn_return_type && current_function_returns_null |
| 4034 | && TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node) |
| 4035 | /* If this function returns non-void and control can drop through, |
| 4036 | complain. */ |
| 4037 | warning ("control reaches end of non-void function"); |
| 4038 | /* With just -W, complain only if function returns both with |
| 4039 | and without a value. */ |
| 4040 | else if (extra_warnings |
| 4041 | && current_function_returns_value && current_function_returns_null) |
| 4042 | warning ("this function may return with or without a value"); |
| 4043 | |
| 4044 | /* Free all the tree nodes making up this function. */ |
| 4045 | /* Switch back to allocating nodes permanently |
| 4046 | until we start another function. */ |
| 4047 | permanent_allocation (); |
| 4048 | |
| 4049 | if (DECL_SAVED_INSNS (fndecl) == 0) |
| 4050 | { |
| 4051 | /* Stop pointing to the local nodes about to be freed. */ |
| 4052 | /* But DECL_INITIAL must remain nonzero so we know this |
| 4053 | was an actual function definition. */ |
| 4054 | DECL_INITIAL (fndecl) = error_mark_node; |
| 4055 | DECL_ARGUMENTS (fndecl) = 0; |
| 4056 | } |
| 4057 | |
| 4058 | /* Let the error reporting routines know that we're outside a function. */ |
| 4059 | current_function_decl = NULL; |
| 4060 | } |