1 /* $NetBSD: lex.c,v 1.241 2026/03/28 14:17:06 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1994, 1995 Jochen Pohl 6 * All Rights Reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Jochen Pohl for 19 * The NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #if HAVE_NBTOOL_CONFIG_H 36 #include "nbtool_config.h" 37 #endif 38 39 #include <sys/cdefs.h> 40 #if defined(__RCSID) 41 __RCSID("$NetBSD: lex.c,v 1.241 2026/03/28 14:17:06 rillig Exp $"); 42 #endif 43 44 #include <ctype.h> 45 #include <errno.h> 46 #include <float.h> 47 #include <limits.h> 48 #include <math.h> 49 #include <stdlib.h> 50 #include <string.h> 51 52 #include "lint1.h" 53 #include "cgram.h" 54 55 #define CHAR_MASK ((1U << CHAR_SIZE) - 1) 56 57 58 /* Current position (it's also updated when an included file is parsed) */ 59 pos_t curr_pos = { "", 1, 0 }; 60 61 /* 62 * Current position in C source (not updated when an included file is 63 * parsed). 64 */ 65 pos_t csrc_pos = { "", 1, 0 }; 66 67 bool in_gcc_attribute; 68 bool in_system_header; 69 70 /* 71 * Define a keyword that cannot be overridden by identifiers. 72 * 73 * Valid values for 'since' are 78, 90, 99, 11, 23. 74 * 75 * The C11 keywords are all taken from the reserved namespace. They are added 76 * in C99 mode as well, to make the parse error messages more useful. For 77 * example, if the keyword '_Generic' were not defined, it would be interpreted 78 * as an implicit function call, leading to a parse error. 79 * 80 * The C23 keywords are not made available in earlier modes, as they may 81 * conflict with user-defined identifiers. 82 */ 83 #define kwdef(name, token, detail, since, gcc, deco) \ 84 { \ 85 name, token, detail, \ 86 (since) == 90, \ 87 (since) == 99 || (since) == 11, \ 88 (since) == 23, \ 89 (gcc) > 0, \ 90 ((deco) & 1) != 0, ((deco) & 2) != 0, ((deco) & 4) != 0, \ 91 } 92 #define kwdef_token(name, token, since, gcc, deco) \ 93 kwdef(name, token, {false}, since, gcc, deco) 94 #define kwdef_sclass(name, sclass, since, gcc, deco) \ 95 kwdef(name, T_SCLASS, .u.kw_scl = (sclass), since, gcc, deco) 96 #define kwdef_type(name, tspec, since) \ 97 kwdef(name, T_TYPE, .u.kw_tspec = (tspec), since, 0, 1) 98 #define kwdef_tqual(name, tqual, since, gcc, deco) \ 99 kwdef(name, T_QUAL, .u.kw_tqual = {.tqual = true}, since, gcc, deco) 100 #define kwdef_const(name, named_constant, since, gcc, deco) \ 101 kwdef(name, T_NAMED_CONSTANT, \ 102 .u.kw_named_constant = (named_constant), since, gcc, deco) 103 #define kwdef_keyword(name, token) \ 104 kwdef(name, token, {false}, 78, 0, 1) 105 106 /* During initialization, these keywords are written to the symbol table. */ 107 static const struct keyword { 108 const char kw_name[20]; 109 int kw_token; /* token to be returned by yylex() */ 110 union { 111 bool kw_dummy; 112 scl_t kw_scl; /* if kw_token is T_SCLASS */ 113 tspec_t kw_tspec; /* if kw_token is T_TYPE or 114 * T_STRUCT_OR_UNION */ 115 type_qualifiers kw_tqual; /* if kw_token is T_QUAL */ 116 function_specifier kw_fs; /* if kw_token is 117 * T_FUNCTION_SPECIFIER */ 118 named_constant kw_named_constant; 119 } u; 120 bool kw_added_in_c90:1; 121 bool kw_added_in_c99_or_c11:1; 122 bool kw_added_in_c23:1; 123 bool kw_gcc:1; /* available in GCC mode */ 124 bool kw_plain:1; /* 'name' */ 125 bool kw_leading:1; /* '__name' */ 126 bool kw_both:1; /* '__name__' */ 127 } keywords[] = { 128 // TODO: _Alignas is not available in C99. 129 kwdef_keyword( "_Alignas", T_ALIGNAS), 130 // TODO: _Alignof is not available in C99. 131 kwdef_keyword( "_Alignof", T_ALIGNOF), 132 // TODO: alignof is not available in C99. 133 kwdef_token( "alignof", T_ALIGNOF, 78,0,6), 134 kwdef_token( "asm", T_ASM, 78,1,7), 135 kwdef_token( "_Atomic", T_ATOMIC, 11,0,1), 136 kwdef("__auto_type", T_TYPE, .u.kw_tspec = AUTO_TYPE, 99,1,1), 137 kwdef_token( "attribute", T_ATTRIBUTE, 78,1,6), 138 kwdef_sclass( "auto", AUTO, 78,0,1), 139 kwdef_type( "_Bool", BOOL, 99), 140 kwdef_type( "bool", BOOL, 23), 141 kwdef_keyword( "break", T_BREAK), 142 kwdef_token( "__builtin_offsetof", T_BUILTIN_OFFSETOF, 78,1,1), 143 kwdef_keyword( "case", T_CASE), 144 kwdef_type( "char", CHAR, 78), 145 kwdef_type( "_Complex", COMPLEX, 99), 146 kwdef_tqual( "const", tq_const, 90,0,7), 147 kwdef_keyword( "continue", T_CONTINUE), 148 kwdef_keyword( "default", T_DEFAULT), 149 kwdef_keyword( "do", T_DO), 150 kwdef_type( "double", DOUBLE, 78), 151 kwdef_keyword( "else", T_ELSE), 152 // XXX: enum requires C90 or later. 153 kwdef_keyword( "enum", T_ENUM), 154 kwdef_token( "__extension__",T_EXTENSION, 78,1,1), 155 kwdef_sclass( "extern", EXTERN, 78,0,1), 156 kwdef_const( "false", NC_FALSE, 23,0,1), 157 kwdef_type( "float", FLOAT, 78), 158 kwdef_keyword( "for", T_FOR), 159 kwdef_token( "_Generic", T_GENERIC, 11,0,1), 160 kwdef_keyword( "goto", T_GOTO), 161 kwdef_keyword( "if", T_IF), 162 kwdef_token( "__imag__", T_IMAG, 78,1,1), 163 kwdef("inline", T_FUNCTION_SPECIFIER, .u.kw_fs = FS_INLINE, 99,0,7), 164 kwdef_type( "int", INT, 78), 165 kwdef_type( "__int128_t", INT128, 99), 166 kwdef_type( "long", LONG, 78), 167 kwdef("_Noreturn", T_FUNCTION_SPECIFIER, .u.kw_fs = FS_NORETURN, 11,0,1), 168 kwdef_const( "nullptr", NC_NULLPTR, 23,0,1), 169 // XXX: __packed is GCC-specific. 170 kwdef_token( "__packed", T_PACKED, 78,0,1), 171 kwdef_token( "__real__", T_REAL, 78,1,1), 172 kwdef_sclass( "register", REG, 78,0,1), 173 kwdef_tqual( "restrict", tq_restrict, 99,0,7), 174 kwdef_keyword( "return", T_RETURN), 175 kwdef_type( "short", SHORT, 78), 176 kwdef( "signed", T_TYPE, .u.kw_tspec = SIGNED, 90,0,3), 177 kwdef_keyword( "sizeof", T_SIZEOF), 178 kwdef_sclass( "static", STATIC, 78,0,1), 179 // XXX: _Static_assert was added in C11. 180 kwdef_keyword( "_Static_assert", T_STATIC_ASSERT), 181 kwdef("struct", T_STRUCT_OR_UNION, .u.kw_tspec = STRUCT, 78,0,1), 182 kwdef_keyword( "switch", T_SWITCH), 183 kwdef_token( "__symbolrename", T_SYMBOLRENAME, 78,0,1), 184 kwdef_sclass( "__thread", THREAD_LOCAL, 78,1,1), 185 kwdef_sclass( "_Thread_local", THREAD_LOCAL, 11,0,1), 186 kwdef_sclass( "thread_local", THREAD_LOCAL, 23,0,1), 187 kwdef_const( "true", NC_TRUE, 23,0,1), 188 kwdef_sclass( "typedef", TYPEDEF, 78,0,1), 189 kwdef_token( "typeof", T_TYPEOF, 78,1,7), 190 kwdef_type( "__uint128_t", UINT128, 99), 191 kwdef("union", T_STRUCT_OR_UNION, .u.kw_tspec = UNION, 78,0,1), 192 kwdef_type( "unsigned", UNSIGN, 78), 193 // XXX: void requires C90 or later. 194 kwdef_type( "void", VOID, 78), 195 kwdef_tqual( "volatile", tq_volatile, 90,0,7), 196 kwdef_keyword( "while", T_WHILE), 197 #undef kwdef 198 #undef kwdef_token 199 #undef kwdef_sclass 200 #undef kwdef_type 201 #undef kwdef_tqual 202 #undef kwdef_keyword 203 }; 204 205 /* 206 * The symbol table containing all keywords, identifiers and labels. The hash 207 * entries are linked via sym_t.s_symtab_next. 208 */ 209 static sym_t *symtab[503]; 210 211 /* 212 * The kind of the next expected symbol, to distinguish the namespaces of 213 * members, labels, type tags and other identifiers. 214 */ 215 symbol_kind sym_kind; 216 217 218 static unsigned int 219 hash(const char *s) 220 { 221 unsigned int v = 0; 222 for (const char *p = s; *p != '\0'; p++) { 223 v = (v << 4) + (unsigned char)*p; 224 v ^= v >> 28; 225 } 226 return v % (sizeof(symtab) / sizeof(symtab[0])); 227 } 228 229 static void 230 symtab_add(sym_t *sym) 231 { 232 unsigned int h = hash(sym->s_name); 233 if ((sym->s_symtab_next = symtab[h]) != NULL) 234 symtab[h]->s_symtab_ref = &sym->s_symtab_next; 235 sym->s_symtab_ref = &symtab[h]; 236 symtab[h] = sym; 237 } 238 239 static sym_t * 240 symtab_search(const char *name) 241 { 242 243 unsigned int h = hash(name); 244 for (sym_t *sym = symtab[h]; sym != NULL; sym = sym->s_symtab_next) { 245 if (strcmp(sym->s_name, name) != 0) 246 continue; 247 if (sym->s_keyword != NULL || 248 sym->s_kind == sym_kind || 249 in_gcc_attribute) 250 return sym; 251 } 252 253 return NULL; 254 } 255 256 static void 257 symtab_remove(sym_t *sym) 258 { 259 260 if ((*sym->s_symtab_ref = sym->s_symtab_next) != NULL) 261 sym->s_symtab_next->s_symtab_ref = sym->s_symtab_ref; 262 sym->s_symtab_next = NULL; 263 } 264 265 static void 266 symtab_remove_locals(void) 267 { 268 269 for (size_t i = 0; i < sizeof(symtab) / sizeof(symtab[0]); i++) { 270 for (sym_t *sym = symtab[i]; sym != NULL; ) { 271 sym_t *next = sym->s_symtab_next; 272 if (sym->s_block_level >= 1) 273 symtab_remove(sym); 274 sym = next; 275 } 276 } 277 } 278 279 #ifdef DEBUG 280 static int 281 sym_by_name(const void *va, const void *vb) 282 { 283 const sym_t *a = *(const sym_t *const *)va; 284 const sym_t *b = *(const sym_t *const *)vb; 285 286 return strcmp(a->s_name, b->s_name); 287 } 288 289 struct syms { 290 const sym_t **items; 291 size_t len; 292 size_t cap; 293 }; 294 295 static void 296 syms_add(struct syms *syms, const sym_t *sym) 297 { 298 if (syms->len >= syms->cap) { 299 syms->cap *= 2; 300 syms->items = xrealloc(syms->items, 301 syms->cap * sizeof(syms->items[0])); 302 } 303 syms->items[syms->len++] = sym; 304 } 305 306 void 307 debug_symtab(void) 308 { 309 struct syms syms = { xcalloc(64, sizeof(syms.items[0])), 0, 64 }; 310 311 debug_enter(); 312 for (int level = -1;; level++) { 313 bool more = false; 314 size_t n = sizeof(symtab) / sizeof(symtab[0]); 315 316 syms.len = 0; 317 for (size_t i = 0; i < n; i++) { 318 for (sym_t *sym = symtab[i]; sym != NULL;) { 319 if (sym->s_block_level == level && 320 sym->s_keyword == NULL) 321 syms_add(&syms, sym); 322 if (sym->s_block_level > level) 323 more = true; 324 sym = sym->s_symtab_next; 325 } 326 } 327 328 if (syms.len > 0) { 329 debug_step("symbol table level %d", level); 330 debug_indent_inc(); 331 qsort(syms.items, syms.len, sizeof(syms.items[0]), 332 sym_by_name); 333 for (size_t i = 0; i < syms.len; i++) 334 debug_sym("", syms.items[i], "\n"); 335 debug_indent_dec(); 336 337 lint_assert(level != -1); 338 } 339 340 if (!more) 341 break; 342 } 343 debug_leave(); 344 345 free(syms.items); 346 } 347 #endif 348 349 static void 350 register_keyword(const struct keyword *kw, bool leading, bool trailing) 351 { 352 353 const char *name; 354 if (!leading && !trailing) { 355 name = kw->kw_name; 356 } else { 357 char buf[256]; 358 (void)snprintf(buf, sizeof(buf), "%s%s%s", 359 leading ? "__" : "", kw->kw_name, trailing ? "__" : ""); 360 name = xstrdup(buf); 361 } 362 363 sym_t *sym = block_zero_alloc(sizeof(*sym), "sym"); 364 sym->s_name = name; 365 sym->s_keyword = kw; 366 int tok = kw->kw_token; 367 sym->u.s_keyword.sk_token = tok; 368 if (tok == T_TYPE || tok == T_STRUCT_OR_UNION) 369 sym->u.s_keyword.u.sk_tspec = kw->u.kw_tspec; 370 if (tok == T_SCLASS) 371 sym->s_scl = kw->u.kw_scl; 372 if (tok == T_QUAL) 373 sym->u.s_keyword.u.sk_type_qualifier = kw->u.kw_tqual; 374 if (tok == T_FUNCTION_SPECIFIER) 375 sym->u.s_keyword.u.function_specifier = kw->u.kw_fs; 376 if (tok == T_NAMED_CONSTANT) 377 sym->u.s_keyword.u.named_constant = kw->u.kw_named_constant; 378 379 symtab_add(sym); 380 } 381 382 static bool 383 is_keyword_known(const struct keyword *kw) 384 { 385 386 if (kw->kw_added_in_c23 && !allow_c23) 387 return false; 388 if ((kw->kw_added_in_c90 || kw->kw_added_in_c99_or_c11) && !allow_c90) 389 return false; 390 391 /* 392 * In the 1990s, GCC defined several keywords that were later 393 * incorporated into C99, therefore in GCC mode, all C99 keywords are 394 * made available. The C11 keywords are made available as well, but 395 * there are so few that they don't matter practically. 396 */ 397 if (allow_gcc) 398 return true; 399 if (kw->kw_gcc) 400 return false; 401 402 if (kw->kw_added_in_c99_or_c11 && !allow_c99) 403 return false; 404 return true; 405 } 406 407 /* Write all keywords to the symbol table. */ 408 void 409 init_lex(void) 410 { 411 412 size_t n = sizeof(keywords) / sizeof(keywords[0]); 413 for (size_t i = 0; i < n; i++) { 414 const struct keyword *kw = keywords + i; 415 if (!is_keyword_known(kw)) 416 continue; 417 if (kw->kw_plain) 418 register_keyword(kw, false, false); 419 if (kw->kw_leading) 420 register_keyword(kw, true, false); 421 if (kw->kw_both) 422 register_keyword(kw, true, true); 423 } 424 } 425 426 /* 427 * When scanning the remainder of a long token (see lex_input), read a byte 428 * and return it as an unsigned char or as EOF. 429 * 430 * Increment the line counts if necessary. 431 */ 432 static int 433 read_byte(void) 434 { 435 int c = lex_input(); 436 437 if (c == '\n') 438 lex_next_line(); 439 return c == '\0' ? EOF : c; /* lex returns 0 on EOF. */ 440 } 441 442 static int 443 lex_keyword(sym_t *sym) 444 { 445 int tok = sym->u.s_keyword.sk_token; 446 447 if (tok == T_SCLASS) 448 yylval.y_scl = sym->s_scl; 449 if (tok == T_TYPE || tok == T_STRUCT_OR_UNION) 450 yylval.y_tspec = sym->u.s_keyword.u.sk_tspec; 451 if (tok == T_QUAL) 452 yylval.y_type_qualifiers = 453 sym->u.s_keyword.u.sk_type_qualifier; 454 if (tok == T_FUNCTION_SPECIFIER) 455 yylval.y_function_specifier = 456 sym->u.s_keyword.u.function_specifier; 457 if (tok == T_NAMED_CONSTANT) 458 yylval.y_named_constant = sym->u.s_keyword.u.named_constant; 459 return tok; 460 } 461 462 /* 463 * Look up the definition of a name in the symbol table. This symbol must 464 * either be a keyword or a symbol of the type required by sym_kind (label, 465 * member, tag, ...). 466 */ 467 extern int 468 lex_name(const char *text, size_t len) 469 { 470 471 sym_t *sym = symtab_search(text); 472 if (sym != NULL && sym->s_keyword != NULL) 473 return lex_keyword(sym); 474 475 sbuf_t *sb = xmalloc(sizeof(*sb)); 476 sb->sb_len = len; 477 sb->sb_sym = sym; 478 yylval.y_name = sb; 479 480 if (sym != NULL) { 481 lint_assert(block_level >= sym->s_block_level); 482 sb->sb_name = sym->s_name; 483 return sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME; 484 } 485 486 char *name = block_zero_alloc(len + 1, "string"); 487 (void)memcpy(name, text, len + 1); 488 sb->sb_name = name; 489 return T_NAME; 490 } 491 492 static tspec_t 493 integer_constant_type_signed(unsigned ls, uint64_t ui, int base, bool warned) 494 { 495 if (ls == 0 && ui <= TARG_INT_MAX) 496 return INT; 497 if (ls == 0 && ui <= TARG_UINT_MAX && base != 10 && allow_c90) 498 return UINT; 499 if (ls == 0 && ui <= TARG_LONG_MAX) 500 return LONG; 501 502 if (ls <= 1 && ui <= TARG_LONG_MAX) 503 return LONG; 504 if (ls <= 1 && ui <= TARG_ULONG_MAX && base != 10) 505 return allow_c90 ? ULONG : LONG; 506 if (ls <= 1 && !allow_c99) { 507 if (!warned) 508 /* integer constant out of range */ 509 warning(252); 510 return allow_c90 ? ULONG : LONG; 511 } 512 513 if (ui <= TARG_LLONG_MAX) 514 return LLONG; 515 if (ui <= TARG_ULLONG_MAX && base != 10) 516 return allow_c90 ? ULLONG : LLONG; 517 if (!warned) 518 /* integer constant out of range */ 519 warning(252); 520 return allow_c90 ? ULLONG : LLONG; 521 } 522 523 static tspec_t 524 integer_constant_type_unsigned(unsigned l, uint64_t ui, bool warned) 525 { 526 if (l == 0 && ui <= TARG_UINT_MAX) 527 return UINT; 528 529 if (l <= 1 && ui <= TARG_ULONG_MAX) 530 return ULONG; 531 if (l <= 1 && !allow_c99) { 532 if (!warned) 533 /* integer constant out of range */ 534 warning(252); 535 return ULONG; 536 } 537 538 if (ui <= TARG_ULLONG_MAX) 539 return ULLONG; 540 if (!warned) 541 /* integer constant out of range */ 542 warning(252); 543 return ULLONG; 544 } 545 546 int 547 lex_integer_constant(const char *text, size_t len, int base) 548 { 549 const char *cp = text; 550 551 /* skip 0[xX] or 0[bB] */ 552 if (base == 16 || base == 2) { 553 cp += 2; 554 len -= 2; 555 } 556 557 /* read suffixes */ 558 unsigned l_suffix = 0, u_suffix = 0; 559 for (;; len--) { 560 char c = cp[len - 1]; 561 if (c == 'l' || c == 'L') 562 l_suffix++; 563 else if (c == 'u' || c == 'U') 564 u_suffix++; 565 else 566 break; 567 } 568 if (l_suffix > 2 || u_suffix > 1) { 569 /* malformed integer constant */ 570 warning(251); 571 if (l_suffix > 2) 572 l_suffix = 2; 573 if (u_suffix > 1) 574 u_suffix = 1; 575 } 576 if (!allow_c90 && u_suffix > 0) 577 /* suffix 'U' requires C90 or later */ 578 warning(97); 579 580 bool warned = false; 581 errno = 0; 582 char *eptr; 583 uint64_t ui = (uint64_t)strtoull(cp, &eptr, base); 584 lint_assert(eptr == cp + len); 585 if (errno != 0) { 586 /* integer constant out of range */ 587 warning(252); 588 warned = true; 589 } 590 591 if (base == 8 && len > 1) 592 /* octal number '%.*s' */ 593 query_message(8, (int)len, cp); 594 595 bool unsigned_since_c90 = allow_trad && allow_c90 && u_suffix == 0 596 && ui > TARG_INT_MAX 597 && ((l_suffix == 0 && base != 10 && ui <= TARG_UINT_MAX) 598 || (l_suffix <= 1 && ui > TARG_LONG_MAX)); 599 600 tspec_t t = u_suffix > 0 601 ? integer_constant_type_unsigned(l_suffix, ui, warned) 602 : integer_constant_type_signed(l_suffix, ui, base, warned); 603 ui = (uint64_t)convert_integer((int64_t)ui, t, size_in_bits(t)); 604 605 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val)); 606 yylval.y_val->v_tspec = t; 607 yylval.y_val->v_unsigned_since_c90 = unsigned_since_c90; 608 yylval.y_val->u.integer = (int64_t)ui; 609 610 return T_CON; 611 } 612 613 /* Extend or truncate si to match t. If t is signed, sign-extend. */ 614 int64_t 615 convert_integer(int64_t si, tspec_t t, unsigned int bits) 616 { 617 618 uint64_t vbits = value_bits(bits); 619 uint64_t ui = (uint64_t)si; 620 return t == PTR || is_uinteger(t) || (ui & bit(bits - 1)) == 0 621 ? (int64_t)(ui & vbits) 622 : (int64_t)(ui | ~vbits); 623 } 624 625 int 626 lex_floating_constant(const char *text, size_t len) 627 { 628 const char *cp = text; 629 630 bool imaginary = cp[len - 1] == 'i'; 631 if (imaginary) 632 len--; 633 634 char c = cp[len - 1]; 635 tspec_t t; 636 if (c == 'f' || c == 'F') { 637 t = imaginary ? FCOMPLEX : FLOAT; 638 len--; 639 } else if (c == 'l' || c == 'L') { 640 t = imaginary ? LCOMPLEX : LDOUBLE; 641 len--; 642 } else 643 t = imaginary ? DCOMPLEX : DOUBLE; 644 645 if (!allow_c90 && t != DOUBLE) 646 /* suffixes 'F' or 'L' require C90 or later */ 647 warning(98); 648 649 errno = 0; 650 char *eptr; 651 long double ld = strtold(cp, &eptr); 652 lint_assert(eptr == cp + len); 653 if (errno != 0) 654 /* floating-point constant out of range */ 655 warning(248); 656 else if (t == FLOAT) { 657 ld = (float)ld; 658 if (isfinite(ld) == 0) { 659 /* floating-point constant out of range */ 660 warning(248); 661 ld = ld > 0 ? FLT_MAX : -FLT_MAX; 662 } 663 } else if (t == DOUBLE || LDOUBLE_SIZE == DOUBLE_SIZE) { 664 ld = (double)ld; 665 if (isfinite(ld) == 0) { 666 /* floating-point constant out of range */ 667 warning(248); 668 ld = ld > 0 ? DBL_MAX : -DBL_MAX; 669 } 670 } 671 672 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val)); 673 yylval.y_val->v_tspec = t; 674 yylval.y_val->u.floating = ld; 675 676 return T_CON; 677 } 678 679 int 680 lex_operator(int t, op_t o) 681 { 682 683 yylval.y_op = o; 684 return t; 685 } 686 687 static buffer 688 read_quoted(bool *complete, char delim, bool wide) 689 { 690 buffer buf; 691 buf_init(&buf); 692 if (wide) 693 buf_add_char(&buf, 'L'); 694 buf_add_char(&buf, delim); 695 696 for (;;) { 697 int c = read_byte(); 698 if (c <= 0) 699 break; 700 buf_add_char(&buf, (char)c); 701 if (c == '\n') 702 break; 703 if (c == delim) { 704 *complete = true; 705 return buf; 706 } 707 if (c == '\\') { 708 c = read_byte(); 709 buf_add_char(&buf, (char)(c <= 0 ? ' ' : c)); 710 if (c <= 0) 711 break; 712 } 713 } 714 *complete = false; 715 buf_add_char(&buf, delim); 716 return buf; 717 } 718 719 /* 720 * Analyze the lexical representation of the next character in the string 721 * literal list. At the end, only update the position information. 722 */ 723 bool 724 quoted_next(const buffer *lit, quoted_iterator *it) 725 { 726 const char *s = lit->data; 727 728 *it = (quoted_iterator){ .start = it->end }; 729 730 char delim = s[s[0] == 'L' ? 1 : 0]; 731 732 bool in_the_middle = it->start > 0; 733 if (!in_the_middle) { 734 it->start = s[0] == 'L' ? 2 : 1; 735 it->end = it->start; 736 } 737 738 while (s[it->start] == delim) { 739 if (it->start + 1 == lit->len) { 740 it->end = it->start; 741 return false; 742 } 743 it->next_literal = in_the_middle; 744 it->start += 2; 745 } 746 it->end = it->start; 747 748 again: 749 switch (s[it->end]) { 750 case '\\': 751 it->end++; 752 goto backslash; 753 case '\n': 754 it->unescaped_newline = true; 755 return false; 756 default: 757 it->value = (unsigned char)s[it->end++]; 758 return true; 759 } 760 761 backslash: 762 it->escaped = true; 763 if ('0' <= s[it->end] && s[it->end] <= '7') 764 goto octal_escape; 765 switch (s[it->end++]) { 766 case '\n': 767 goto again; 768 case 'a': 769 it->named_escape = true; 770 it->value = '\a'; 771 it->invalid_escape = !allow_c90; 772 return true; 773 case 'b': 774 it->named_escape = true; 775 it->value = '\b'; 776 return true; 777 case 'e': 778 it->named_escape = true; 779 it->value = '\033'; 780 it->invalid_escape = !allow_gcc; 781 return true; 782 case 'f': 783 it->named_escape = true; 784 it->value = '\f'; 785 return true; 786 case 'n': 787 it->named_escape = true; 788 it->value = '\n'; 789 return true; 790 case 'r': 791 it->named_escape = true; 792 it->value = '\r'; 793 return true; 794 case 't': 795 it->named_escape = true; 796 it->value = '\t'; 797 return true; 798 case 'v': 799 it->named_escape = true; 800 it->value = '\v'; 801 it->invalid_escape = !allow_c90; 802 return true; 803 case 'x': 804 goto hex_escape; 805 case '"': 806 it->literal_escape = true; 807 it->value = '"'; 808 it->invalid_escape = !allow_c90 && delim == '\''; 809 return true; 810 case '?': 811 it->literal_escape = true; 812 it->value = '?'; 813 it->invalid_escape = !allow_c90; 814 return true; 815 default: 816 it->invalid_escape = true; 817 /* FALLTHROUGH */ 818 case '\'': 819 case '\\': 820 it->literal_escape = true; 821 it->value = (unsigned char)s[it->end - 1]; 822 return true; 823 } 824 825 octal_escape: 826 it->octal_digits++; 827 it->value = s[it->end++] - '0'; 828 if ('0' <= s[it->end] && s[it->end] <= '7') { 829 it->octal_digits++; 830 it->value = 8 * it->value + (s[it->end++] - '0'); 831 if ('0' <= s[it->end] && s[it->end] <= '7') { 832 it->octal_digits++; 833 it->value = 8 * it->value + (s[it->end++] - '0'); 834 it->overflow = it->value > TARG_UCHAR_MAX 835 && s[0] != 'L'; 836 } 837 } 838 return true; 839 840 hex_escape: 841 for (;;) { 842 char ch = s[it->end]; 843 unsigned digit_value; 844 if ('0' <= ch && ch <= '9') 845 digit_value = ch - '0'; 846 else if ('A' <= ch && ch <= 'F') 847 digit_value = 10 + (ch - 'A'); 848 else if ('a' <= ch && ch <= 'f') 849 digit_value = 10 + (ch - 'a'); 850 else 851 break; 852 853 it->end++; 854 it->value = 16 * it->value + digit_value; 855 uint64_t limit = s[0] == 'L' ? TARG_UINT_MAX : TARG_UCHAR_MAX; 856 if (it->value > limit) 857 it->overflow = true; 858 if (it->hex_digits < 3) 859 it->hex_digits++; 860 } 861 it->missing_hex_digits = it->hex_digits == 0; 862 return true; 863 } 864 865 static void 866 check_quoted(const buffer *buf, bool complete, char delim) 867 { 868 quoted_iterator it = { .end = 0 }, prev = it; 869 for (; quoted_next(buf, &it); prev = it) { 870 if (it.missing_hex_digits) 871 /* no hex digits follow \x */ 872 error(74); 873 if (it.hex_digits > 0 && !allow_c90) 874 /* \x requires C90 or later */ 875 warning(82); 876 else if (!it.invalid_escape) 877 ; 878 else if (it.value == '8' || it.value == '9') 879 /* bad octal digit '%c' */ 880 warning(77, (int)it.value); 881 else if (it.literal_escape && it.value == '?') 882 /* \? requires C90 or later */ 883 warning(263); 884 else if (it.literal_escape && it.value == '"') 885 /* \" inside a character constant requires C90 ... */ 886 warning(262); 887 else if (it.named_escape && it.value == '\a') 888 /* \a requires C90 or later */ 889 warning(81); 890 else if (it.named_escape && it.value == '\v') 891 /* \v requires C90 or later */ 892 warning(264); 893 else { 894 unsigned char ch = buf->data[it.end - 1]; 895 if (ch_isprint(ch)) 896 /* dubious escape \%c */ 897 warning(79, ch); 898 else 899 /* dubious escape \%o */ 900 warning(80, ch); 901 } 902 if (it.overflow && it.hex_digits > 0) 903 /* overflow in hex escape */ 904 warning(75); 905 if (it.overflow && it.octal_digits > 0) 906 /* character escape does not fit in character */ 907 warning(76); 908 if (it.value < ' ' && !it.escaped && complete) 909 /* invisible character U+%04X in %s */ 910 query_message(17, (unsigned)it.value, delim == '"' 911 ? "string literal" : "character constant"); 912 if (prev.octal_digits > 0 && prev.octal_digits < 3 913 && !it.escaped && it.value >= '8' && it.value <= '9') 914 /* short octal escape '%.*s' followed by digit '%c' */ 915 warning(356, (int)(prev.end - prev.start), 916 buf->data + prev.start, buf->data[it.start]); 917 } 918 if (it.unescaped_newline) 919 /* newline in string or char constant */ 920 error(254); 921 if (!complete && delim == '"') 922 /* unterminated string constant */ 923 error(258); 924 if (!complete && delim == '\'') 925 /* unterminated character constant */ 926 error(253); 927 } 928 929 static buffer 930 lex_quoted(char delim, bool wide) 931 { 932 bool complete; 933 buffer buf = read_quoted(&complete, delim, wide); 934 check_quoted(&buf, complete, delim); 935 return buf; 936 } 937 938 /* Called if lex found a leading "'". */ 939 int 940 lex_character_constant(void) 941 { 942 buffer buf = lex_quoted('\'', false); 943 944 size_t n = 0; 945 uint64_t val = 0; 946 quoted_iterator it = { .end = 0 }; 947 while (quoted_next(&buf, &it)) { 948 val = (val << CHAR_SIZE) + it.value; 949 n++; 950 } 951 if (n > sizeof(int) || (n > 1 && (pflag || hflag))) { 952 /* 953 * XXX: ^^ should rather be sizeof(TARG_INT). Luckily, 954 * sizeof(int) is the same on all supported platforms. 955 */ 956 /* too many characters in character constant */ 957 error(71); 958 } else if (n > 1) 959 /* multi-character character constant */ 960 warning(294); 961 else if (n == 0 && !it.unescaped_newline) 962 /* empty character constant */ 963 error(73); 964 965 int64_t cval = n == 1 966 ? convert_integer((int64_t)val, CHAR, CHAR_SIZE) 967 : (int64_t)val; 968 969 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val)); 970 yylval.y_val->v_tspec = INT; 971 yylval.y_val->v_char_constant = true; 972 yylval.y_val->u.integer = cval; 973 974 return T_CON; 975 } 976 977 /* Called if lex found a leading "L'". */ 978 int 979 lex_wide_character_constant(void) 980 { 981 buffer buf = lex_quoted('\'', true); 982 983 static char wbuf[MB_LEN_MAX + 1]; 984 size_t n = 0, nmax = MB_CUR_MAX; 985 986 quoted_iterator it = { .end = 0 }; 987 while (quoted_next(&buf, &it)) { 988 if (n < nmax) 989 wbuf[n] = (char)it.value; 990 n++; 991 } 992 993 wchar_t wc = 0; 994 if (n == 0) 995 /* empty character constant */ 996 error(73); 997 else if (n > nmax) { 998 n = nmax; 999 /* too many characters in character constant */ 1000 error(71); 1001 } else { 1002 wbuf[n] = '\0'; 1003 (void)mbtowc(NULL, NULL, 0); 1004 if (mbtowc(&wc, wbuf, nmax) < 0) 1005 /* invalid multibyte character */ 1006 error(291); 1007 } 1008 1009 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val)); 1010 yylval.y_val->v_tspec = WCHAR_TSPEC; 1011 yylval.y_val->v_char_constant = true; 1012 yylval.y_val->u.integer = wc; 1013 1014 return T_CON; 1015 } 1016 1017 /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */ 1018 static void 1019 parse_line_directive_flags(const char *p, 1020 bool *is_begin, bool *is_end, bool *is_system) 1021 { 1022 1023 *is_begin = false; 1024 *is_end = false; 1025 *is_system = false; 1026 1027 while (*p != '\0') { 1028 while (ch_isspace(*p)) 1029 p++; 1030 1031 const char *word = p; 1032 while (*p != '\0' && !ch_isspace(*p)) 1033 p++; 1034 size_t len = (size_t)(p - word); 1035 1036 if (len == 1 && word[0] == '1') 1037 *is_begin = true; 1038 if (len == 1 && word[0] == '2') 1039 *is_end = true; 1040 if (len == 1 && word[0] == '3') 1041 *is_system = true; 1042 /* Flag '4' is only interesting for C++. */ 1043 } 1044 } 1045 1046 /* 1047 * The first directive of the preprocessed translation unit provides the name 1048 * of the C source file as specified at the command line. 1049 */ 1050 static void 1051 set_csrc_pos(void) 1052 { 1053 static bool done; 1054 1055 if (done) 1056 return; 1057 done = true; 1058 csrc_pos.p_file = curr_pos.p_file; 1059 outsrc(transform_filename(curr_pos.p_file, strlen(curr_pos.p_file))); 1060 } 1061 1062 /* # lineno ["filename" [GCC-flag...]] */ 1063 static void 1064 set_location(const char *p) 1065 { 1066 char *end; 1067 long ln = strtol(--p, &end, 10); 1068 if (end == p) 1069 goto error; 1070 p = end; 1071 1072 if (*p != ' ' && *p != '\0') 1073 goto error; 1074 while (*p == ' ') 1075 p++; 1076 1077 if (*p != '\0') { 1078 if (*p != '"') 1079 goto error; 1080 const char *fn = ++p; 1081 while (*p != '"' && *p != '\0') 1082 p++; 1083 if (*p != '"') 1084 goto error; 1085 size_t fn_len = p++ - fn; 1086 if (fn_len > PATH_MAX) 1087 goto error; 1088 if (fn_len == 0) { 1089 fn = "{standard input}"; 1090 fn_len = strlen(fn); 1091 } 1092 curr_pos.p_file = record_filename(fn, fn_len); 1093 set_csrc_pos(); 1094 1095 bool is_begin, is_end, is_system; 1096 parse_line_directive_flags(p, &is_begin, &is_end, &is_system); 1097 update_location(curr_pos.p_file, (int)ln, is_begin, is_end); 1098 in_system_header = is_system; 1099 } 1100 curr_pos.p_line = (int)ln - 1; 1101 curr_pos.p_uniq = 0; 1102 if (curr_pos.p_file == csrc_pos.p_file) { 1103 csrc_pos.p_line = (int)ln - 1; 1104 csrc_pos.p_uniq = 0; 1105 } 1106 return; 1107 1108 error: 1109 /* undefined or invalid '#' directive */ 1110 warning(255); 1111 } 1112 1113 static void 1114 check_stmt_macro(const char *text) 1115 { 1116 const char *p = text; 1117 while (*p == ' ') 1118 p++; 1119 1120 const char *name_start = p; 1121 while (ch_isalnum(*p) || *p == '_') 1122 p++; 1123 const char *name_end = p; 1124 1125 if (*p == '(') { 1126 while (*p != '\0' && *p != ')') 1127 p++; 1128 if (*p == ')') 1129 p++; 1130 } 1131 1132 while (*p == ' ') 1133 p++; 1134 1135 if (strncmp(p, "do", 2) == 0 && !ch_isalnum(p[2]) && p[2] != '_') 1136 /* do-while macro '%.*s' ends with semicolon */ 1137 warning(385, (int)(name_end - name_start), name_start); 1138 } 1139 1140 // Between lex_pp_begin and lex_pp_end, the current preprocessing line, 1141 // with comments and whitespace converted to a single space. 1142 static buffer pp_line; 1143 1144 void 1145 lex_pp_begin(void) 1146 { 1147 if (pp_line.data == NULL) 1148 buf_init(&pp_line); 1149 debug_step("%s", __func__); 1150 lint_assert(pp_line.len == 0); 1151 } 1152 1153 void 1154 lex_pp_identifier(const char *text) 1155 { 1156 debug_step("%s '%s'", __func__, text); 1157 buf_add(&pp_line, text); 1158 } 1159 1160 void 1161 lex_pp_number(const char *text) 1162 { 1163 debug_step("%s '%s'", __func__, text); 1164 buf_add(&pp_line, text); 1165 } 1166 1167 void 1168 lex_pp_character_constant(void) 1169 { 1170 buffer buf = lex_quoted('\'', false); 1171 debug_step("%s '%s'", __func__, buf.data); 1172 buf_add(&pp_line, buf.data); 1173 free(buf.data); 1174 } 1175 1176 void 1177 lex_pp_string_literal(void) 1178 { 1179 buffer buf = lex_quoted('"', false); 1180 debug_step("%s '%s'", __func__, buf.data); 1181 buf_add(&pp_line, buf.data); 1182 free(buf.data); 1183 } 1184 1185 void 1186 lex_pp_punctuator(const char *text) 1187 { 1188 debug_step("%s '%s'", __func__, text); 1189 buf_add(&pp_line, text); 1190 } 1191 1192 void 1193 lex_pp_comment(void) 1194 { 1195 int lc = -1, c; 1196 1197 for (;;) { 1198 if ((c = read_byte()) == EOF) { 1199 /* unterminated comment */ 1200 error(256); 1201 return; 1202 } 1203 if (lc == '*' && c == '/') 1204 break; 1205 lc = c; 1206 } 1207 1208 buf_add_char(&pp_line, ' '); 1209 } 1210 1211 void 1212 lex_pp_whitespace(void) 1213 { 1214 buf_add_char(&pp_line, ' '); 1215 } 1216 1217 void 1218 lex_pp_end(void) 1219 { 1220 const char *text = pp_line.data; 1221 size_t len = pp_line.len; 1222 while (len > 0 && text[len - 1] == ' ') 1223 len--; 1224 debug_step("%s '%.*s'", __func__, (int)len, text); 1225 1226 const char *p = text; 1227 while (*p == ' ') 1228 p++; 1229 1230 if (ch_isdigit(*p)) 1231 set_location(p); 1232 else if (strncmp(p, "pragma ", 7) == 0) 1233 goto done; 1234 else if (strncmp(p, "define ", 7) == 0) { 1235 if (text[len - 1] == ';') 1236 check_stmt_macro(p + 7); 1237 } else if (strncmp(p, "undef ", 6) == 0) 1238 goto done; 1239 else 1240 /* undefined or invalid '#' directive */ 1241 warning(255); 1242 1243 done: 1244 pp_line.len = 0; 1245 pp_line.data[0] = '\0'; 1246 } 1247 1248 /* Handle lint comments such as ARGSUSED. */ 1249 void 1250 lex_comment(void) 1251 { 1252 int c; 1253 static const struct { 1254 const char name[13]; 1255 bool arg; 1256 lint_comment comment; 1257 } keywtab[] = { 1258 { "ARGSUSED", true, LC_ARGSUSED }, 1259 { "BITFIELDTYPE", false, LC_BITFIELDTYPE }, 1260 { "FALLTHRU", false, LC_FALLTHROUGH }, 1261 { "FALLTHROUGH", false, LC_FALLTHROUGH }, 1262 { "FALL THROUGH", false, LC_FALLTHROUGH }, 1263 { "fallthrough", false, LC_FALLTHROUGH }, 1264 { "LINTLIBRARY", false, LC_LINTLIBRARY }, 1265 { "LINTED", true, LC_LINTED }, 1266 { "LONGLONG", false, LC_LONGLONG }, 1267 { "NOSTRICT", true, LC_LINTED }, 1268 { "NOTREACHED", false, LC_NOTREACHED }, 1269 { "PRINTFLIKE", true, LC_PRINTFLIKE }, 1270 { "PROTOLIB", true, LC_PROTOLIB }, 1271 { "SCANFLIKE", true, LC_SCANFLIKE }, 1272 { "VARARGS", true, LC_VARARGS }, 1273 }; 1274 char keywd[32]; 1275 1276 bool seen_end_of_comment = false; 1277 1278 while (c = read_byte(), isspace(c) != 0) 1279 continue; 1280 1281 /* Read the potential keyword to keywd */ 1282 size_t l = 0; 1283 while (c != EOF && l < sizeof(keywd) - 1 && 1284 (isalpha(c) != 0 || isspace(c) != 0)) { 1285 if (islower(c) != 0 && l > 0 && ch_isupper(keywd[0])) 1286 break; 1287 keywd[l++] = (char)c; 1288 c = read_byte(); 1289 } 1290 while (l > 0 && ch_isspace(keywd[l - 1])) 1291 l--; 1292 keywd[l] = '\0'; 1293 1294 /* look for the keyword */ 1295 size_t i; 1296 for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++) 1297 if (strcmp(keywtab[i].name, keywd) == 0) 1298 goto found_keyword; 1299 goto skip_rest; 1300 1301 found_keyword: 1302 while (isspace(c) != 0) 1303 c = read_byte(); 1304 1305 /* read the argument, if the keyword accepts one and there is one */ 1306 char arg[32]; 1307 l = 0; 1308 if (keywtab[i].arg) { 1309 while (isdigit(c) != 0 && l < sizeof(arg) - 1) { 1310 arg[l++] = (char)c; 1311 c = read_byte(); 1312 } 1313 } 1314 arg[l] = '\0'; 1315 int a = l != 0 ? atoi(arg) : -1; 1316 1317 while (isspace(c) != 0) 1318 c = read_byte(); 1319 1320 seen_end_of_comment = c == '*' && (c = read_byte()) == '/'; 1321 if (!seen_end_of_comment && keywtab[i].comment != LC_LINTED) 1322 /* extra characters in lint comment */ 1323 warning(257); 1324 1325 handle_lint_comment(keywtab[i].comment, a); 1326 1327 skip_rest: 1328 while (!seen_end_of_comment) { 1329 int lc = c; 1330 if ((c = read_byte()) == EOF) { 1331 /* unterminated comment */ 1332 error(256); 1333 break; 1334 } 1335 if (lc == '*' && c == '/') 1336 seen_end_of_comment = true; 1337 } 1338 } 1339 1340 void 1341 lex_slash_slash_comment(void) 1342 { 1343 1344 if (!allow_c99 && !allow_gcc) 1345 /* %s does not support '//' comments */ 1346 gnuism(312, allow_c90 ? "C90" : "traditional C"); 1347 1348 for (int c; c = read_byte(), c != EOF && c != '\n';) 1349 continue; 1350 } 1351 1352 void 1353 reset_suppressions(void) 1354 { 1355 1356 lwarn = LWARN_ALL; 1357 suppress_longlong = false; 1358 } 1359 1360 int 1361 lex_string(void) 1362 { 1363 buffer *buf = xmalloc(sizeof(*buf)); 1364 *buf = lex_quoted('"', false); 1365 yylval.y_string = buf; 1366 return T_STRING; 1367 } 1368 1369 static size_t 1370 wide_length(const buffer *buf) 1371 { 1372 1373 (void)mblen(NULL, 0); 1374 size_t len = 0, i = 0; 1375 while (i < buf->len) { 1376 int n = mblen(buf->data + i, MB_CUR_MAX); 1377 if (n == -1) { 1378 /* invalid multibyte character */ 1379 error(291); 1380 break; 1381 } 1382 i += n > 1 ? n : 1; 1383 len++; 1384 } 1385 return len; 1386 } 1387 1388 int 1389 lex_wide_string(void) 1390 { 1391 buffer buf = lex_quoted('"', true); 1392 1393 buffer str; 1394 buf_init(&str); 1395 quoted_iterator it = { .end = 0 }; 1396 while (quoted_next(&buf, &it)) 1397 buf_add_char(&str, (char)it.value); 1398 1399 free(buf.data); 1400 1401 buffer *len_buf = xcalloc(1, sizeof(*len_buf)); 1402 len_buf->len = wide_length(&str); 1403 yylval.y_string = len_buf; 1404 return T_STRING; 1405 } 1406 1407 void 1408 lex_next_line(void) 1409 { 1410 curr_pos.p_line++; 1411 curr_pos.p_uniq = 0; 1412 debug_skip_indent(); 1413 debug_printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line); 1414 if (curr_pos.p_file == csrc_pos.p_file) { 1415 csrc_pos.p_line++; 1416 csrc_pos.p_uniq = 0; 1417 } 1418 } 1419 1420 void 1421 lex_unknown_character(int c) 1422 { 1423 1424 /* unknown character \%o */ 1425 error(250, c); 1426 } 1427 1428 /* 1429 * The scanner does not create new symbol table entries for symbols it cannot 1430 * find in the symbol table. This is to avoid putting undeclared symbols into 1431 * the symbol table if a syntax error occurs. 1432 * 1433 * getsym is called as soon as it is probably ok to put the symbol in the 1434 * symbol table. It is still possible that symbols are put in the symbol 1435 * table that are not completely declared due to syntax errors. To avoid too 1436 * many problems in this case, symbols get type 'int' in getsym. 1437 * 1438 * XXX calls to getsym should be delayed until declare_1_* is called. 1439 */ 1440 sym_t * 1441 getsym(sbuf_t *sb) 1442 { 1443 1444 sym_t *sym = sb->sb_sym; 1445 1446 /* 1447 * During member declaration it is possible that name() looked for 1448 * symbols of type SK_VCFT, although it should have looked for symbols 1449 * of type SK_TAG. Same can happen for labels. Both cases are 1450 * compensated here. 1451 */ 1452 if (sym_kind == SK_MEMBER || sym_kind == SK_LABEL) { 1453 if (sym == NULL || sym->s_kind == SK_VCFT) 1454 sym = symtab_search(sb->sb_name); 1455 } 1456 1457 if (sym != NULL) { 1458 lint_assert(sym->s_kind == sym_kind); 1459 set_sym_kind(SK_VCFT); 1460 free(sb); 1461 return sym; 1462 } 1463 1464 /* create a new symbol table entry */ 1465 1466 decl_level *dl; 1467 if (sym_kind == SK_LABEL) { 1468 sym = level_zero_alloc(1, sizeof(*sym), "sym"); 1469 char *s = level_zero_alloc(1, sb->sb_len + 1, "string"); 1470 (void)memcpy(s, sb->sb_name, sb->sb_len + 1); 1471 sym->s_name = s; 1472 sym->s_block_level = 1; 1473 dl = dcs; 1474 while (dl->d_enclosing != NULL && 1475 dl->d_enclosing->d_enclosing != NULL) 1476 dl = dl->d_enclosing; 1477 lint_assert(dl->d_kind == DLK_AUTO); 1478 } else { 1479 sym = block_zero_alloc(sizeof(*sym), "sym"); 1480 sym->s_name = sb->sb_name; 1481 sym->s_block_level = block_level; 1482 dl = dcs; 1483 } 1484 1485 sym->s_def_pos = unique_curr_pos(); 1486 if ((sym->s_kind = sym_kind) != SK_LABEL) 1487 sym->s_type = gettyp(INT); 1488 1489 set_sym_kind(SK_VCFT); 1490 1491 if (!in_gcc_attribute) { 1492 debug_printf("%s: symtab_add ", __func__); 1493 debug_sym("", sym, "\n"); 1494 symtab_add(sym); 1495 1496 *dl->d_last_dlsym = sym; 1497 dl->d_last_dlsym = &sym->s_level_next; 1498 } 1499 1500 free(sb); 1501 return sym; 1502 } 1503 1504 /* 1505 * Construct a temporary symbol. The symbol name starts with a digit to avoid 1506 * name clashes with other identifiers. 1507 */ 1508 sym_t * 1509 mktempsym(type_t *tp) 1510 { 1511 static unsigned n = 0; 1512 char *s = level_zero_alloc((size_t)block_level, 64, "string"); 1513 sym_t *sym = block_zero_alloc(sizeof(*sym), "sym"); 1514 scl_t scl; 1515 1516 (void)snprintf(s, 64, "%.8u_tmp", n++); 1517 1518 scl = dcs->d_scl; 1519 if (scl == NO_SCL) 1520 scl = block_level > 0 ? AUTO : EXTERN; 1521 1522 sym->s_name = s; 1523 sym->s_type = tp; 1524 sym->s_block_level = block_level; 1525 sym->s_scl = scl; 1526 sym->s_kind = SK_VCFT; 1527 sym->s_used = true; 1528 sym->s_set = true; 1529 1530 symtab_add(sym); 1531 1532 *dcs->d_last_dlsym = sym; 1533 dcs->d_last_dlsym = &sym->s_level_next; 1534 1535 return sym; 1536 } 1537 1538 void 1539 symtab_remove_forever(sym_t *sym) 1540 { 1541 1542 debug_step("%s '%s' %s '%s'", __func__, 1543 sym->s_name, symbol_kind_name(sym->s_kind), 1544 type_name(sym->s_type)); 1545 symtab_remove(sym); 1546 1547 /* avoid that the symbol will later be put back to the symbol table */ 1548 sym->s_block_level = -1; 1549 } 1550 1551 /* 1552 * Remove all symbols from the symbol table that have the same level as the 1553 * given symbol. 1554 */ 1555 void 1556 symtab_remove_level(sym_t *syms) 1557 { 1558 1559 if (syms != NULL) 1560 debug_step("%s %d", __func__, syms->s_block_level); 1561 1562 /* Note the use of s_level_next instead of s_symtab_next. */ 1563 for (sym_t *sym = syms; sym != NULL; sym = sym->s_level_next) { 1564 if (sym->s_block_level != -1) { 1565 debug_step("%s '%s' %s '%s' %d", __func__, 1566 sym->s_name, symbol_kind_name(sym->s_kind), 1567 type_name(sym->s_type), sym->s_block_level); 1568 symtab_remove(sym); 1569 sym->s_symtab_ref = NULL; 1570 } 1571 } 1572 } 1573 1574 /* Put a symbol into the symbol table. */ 1575 void 1576 inssym(int level, sym_t *sym) 1577 { 1578 1579 debug_step("%s '%s' %s '%s' %d", __func__, 1580 sym->s_name, symbol_kind_name(sym->s_kind), 1581 type_name(sym->s_type), level); 1582 sym->s_block_level = level; 1583 symtab_add(sym); 1584 1585 const sym_t *next = sym->s_symtab_next; 1586 if (next != NULL) 1587 lint_assert(sym->s_block_level >= next->s_block_level); 1588 } 1589 1590 /* Called at level 0 after syntax errors. */ 1591 void 1592 clean_up_after_error(void) 1593 { 1594 1595 symtab_remove_locals(); 1596 1597 while (mem_block_level > 0) 1598 level_free_all(mem_block_level--); 1599 } 1600 1601 /* Create a new symbol with the same name as an existing symbol. */ 1602 sym_t * 1603 pushdown(const sym_t *sym) 1604 { 1605 1606 debug_step("pushdown '%s' %s '%s'", 1607 sym->s_name, symbol_kind_name(sym->s_kind), 1608 type_name(sym->s_type)); 1609 1610 sym_t *nsym = block_zero_alloc(sizeof(*nsym), "sym"); 1611 lint_assert(sym->s_block_level <= block_level); 1612 nsym->s_name = sym->s_name; 1613 nsym->s_def_pos = unique_curr_pos(); 1614 nsym->s_kind = sym->s_kind; 1615 nsym->s_block_level = block_level; 1616 1617 symtab_add(nsym); 1618 1619 *dcs->d_last_dlsym = nsym; 1620 dcs->d_last_dlsym = &nsym->s_level_next; 1621 1622 return nsym; 1623 } 1624 1625 static void 1626 fill_token(int tk, const char *text, token *tok) 1627 { 1628 switch (tk) { 1629 case T_NAME: 1630 case T_TYPENAME: 1631 tok->kind = TK_IDENTIFIER; 1632 tok->u.identifier = xstrdup(yylval.y_name->sb_name); 1633 break; 1634 case T_CON: 1635 tok->kind = TK_CONSTANT; 1636 tok->u.constant = *yylval.y_val; 1637 break; 1638 case T_NAMED_CONSTANT: 1639 tok->kind = TK_IDENTIFIER; 1640 tok->u.identifier = xstrdup(text); 1641 break; 1642 case T_STRING:; 1643 tok->kind = TK_STRING_LITERALS; 1644 tok->u.string_literals.len = yylval.y_string->len; 1645 tok->u.string_literals.cap = yylval.y_string->cap; 1646 tok->u.string_literals.data = xstrdup(yylval.y_string->data); 1647 break; 1648 default: 1649 tok->kind = TK_PUNCTUATOR; 1650 tok->u.punctuator = xstrdup(text); 1651 } 1652 } 1653 1654 static void 1655 seq_reserve(balanced_token_sequence *seq) 1656 { 1657 if (seq->len >= seq->cap) { 1658 seq->cap = 16 + 2 * seq->cap; 1659 const balanced_token *old_tokens = seq->tokens; 1660 balanced_token *new_tokens = block_zero_alloc( 1661 seq->cap * sizeof(*seq->tokens), "balanced_token[]"); 1662 if (seq->len > 0) 1663 memcpy(new_tokens, old_tokens, 1664 seq->len * sizeof(*seq->tokens)); 1665 seq->tokens = new_tokens; 1666 } 1667 } 1668 1669 static balanced_token_sequence 1670 read_balanced(int opening) 1671 { 1672 int closing = opening == T_LPAREN ? T_RPAREN 1673 : opening == T_LBRACK ? T_RBRACK : T_RBRACE; 1674 balanced_token_sequence seq = { NULL, 0, 0 }; 1675 1676 int tok; 1677 while (tok = yylex(), tok > 0 && tok != closing) { 1678 seq_reserve(&seq); 1679 if (tok == T_LPAREN || tok == T_LBRACK || tok == T_LBRACE) { 1680 seq.tokens[seq.len].kind = tok == T_LPAREN ? '(' 1681 : tok == T_LBRACK ? '[' : '{'; 1682 seq.tokens[seq.len].u.tokens = read_balanced(tok); 1683 } else { 1684 fill_token(tok, yytext, &seq.tokens[seq.len].u.token); 1685 freeyyv(&yylval, tok); 1686 } 1687 seq.len++; 1688 } 1689 return seq; 1690 } 1691 1692 balanced_token_sequence 1693 lex_balanced(void) 1694 { 1695 return read_balanced(T_LPAREN); 1696 } 1697 1698 /* 1699 * Free any dynamically allocated memory referenced by 1700 * the value stack or yylval. 1701 * The type of information in yylval is described by tok. 1702 */ 1703 void 1704 freeyyv(void *sp, int tok) 1705 { 1706 if (tok == T_NAME || tok == T_TYPENAME) { 1707 sbuf_t *sb = *(sbuf_t **)sp; 1708 free(sb); 1709 } else if (tok == T_CON) { 1710 val_t *val = *(val_t **)sp; 1711 free(val); 1712 } else if (tok == T_STRING) { 1713 buffer *str = *(buffer **)sp; 1714 free(str->data); 1715 free(str); 1716 } 1717 } 1718