scan.l revision 15158
1%{ 2/* $NetBSD: scan.l,v 1.8 1995/10/23 13:38:51 jpo Exp $ */ 3 4/* 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#ifndef lint 36static char rcsid[] = "$NetBSD: scan.l,v 1.8 1995/10/23 13:38:51 jpo Exp $"; 37#endif 38 39#include <stdlib.h> 40#include <string.h> 41#include <limits.h> 42#include <float.h> 43#include <ctype.h> 44#include <errno.h> 45#include <err.h> 46 47#include "lint1.h" 48#include "y.tab.h" 49 50#define CHAR_MASK (~(~0 << CHAR_BIT)) 51 52/* XXX declaration of strtouq() is missing in stdlib.h ? */ 53extern u_quad_t strtouq __P((const char *, char **, int)); 54 55/* Current position (its also updated when an included file is parsed) */ 56pos_t curr_pos = { 1, "" }; 57 58/* 59 * Current position in C source (not updated when an included file is 60 * parsed). 61 */ 62pos_t csrc_pos = { 1, "" }; 63 64static void incline __P((void)); 65static void badchar __P((int)); 66static sbuf_t *allocsb __P((void)); 67static void freesb __P((sbuf_t *)); 68static int inpc __P((void)); 69static int hash __P((const char *)); 70static sym_t *search __P((sbuf_t *)); 71static int name __P((void)); 72static int keyw __P((sym_t *)); 73static int icon __P((int)); 74static int fcon __P((void)); 75static int operator __P((int, op_t)); 76static int ccon __P((void)); 77static int wccon __P((void)); 78static int getescc __P((int)); 79static void directive __P((void)); 80static void comment __P((void)); 81static int string __P((void)); 82static int wcstrg __P((void)); 83 84%} 85 86L [_A-Za-z] 87D [0-9] 88NZD [1-9] 89OD [0-7] 90HD [0-9A-Fa-f] 91EX ([eE][+-]?[0-9]+) 92 93%% 94 95{L}({L}|{D})* return (name()); 960{OD}*[lLuU]* return (icon(8)); 97{NZD}{D}*[lLuU]* return (icon(10)); 980[xX]{HD}+[lLuU]* return (icon(16)); 99{D}+\.{D}*{EX}?[fFlL]? | 100{D}+{EX}[fFlL]? | 101\.{D}+{EX}?[fFlL]? return (fcon()); 102"=" return (operator(T_ASSIGN, ASSIGN)); 103"*=" return (operator(T_OPASS, MULASS)); 104"/=" return (operator(T_OPASS, DIVASS)); 105"%=" return (operator(T_OPASS, MODASS)); 106"+=" return (operator(T_OPASS, ADDASS)); 107"-=" return (operator(T_OPASS, SUBASS)); 108"<<=" return (operator(T_OPASS, SHLASS)); 109">>=" return (operator(T_OPASS, SHRASS)); 110"&=" return (operator(T_OPASS, ANDASS)); 111"^=" return (operator(T_OPASS, XORASS)); 112"|=" return (operator(T_OPASS, ORASS)); 113"||" return (operator(T_LOGOR, LOGOR)); 114"&&" return (operator(T_LOGAND, LOGAND)); 115"|" return (operator(T_OR, OR)); 116"&" return (operator(T_AND, AND)); 117"^" return (operator(T_XOR, XOR)); 118"==" return (operator(T_EQOP, EQ)); 119"!=" return (operator(T_EQOP, NE)); 120"<" return (operator(T_RELOP, LT)); 121">" return (operator(T_RELOP, GT)); 122"<=" return (operator(T_RELOP, LE)); 123">=" return (operator(T_RELOP, GE)); 124"<<" return (operator(T_SHFTOP, SHL)); 125">>" return (operator(T_SHFTOP, SHR)); 126"++" return (operator(T_INCDEC, INC)); 127"--" return (operator(T_INCDEC, DEC)); 128"->" return (operator(T_STROP, ARROW)); 129"." return (operator(T_STROP, POINT)); 130"+" return (operator(T_ADDOP, PLUS)); 131"-" return (operator(T_ADDOP, MINUS)); 132"*" return (operator(T_MULT, MULT)); 133"/" return (operator(T_DIVOP, DIV)); 134"%" return (operator(T_DIVOP, MOD)); 135"!" return (operator(T_UNOP, NOT)); 136"~" return (operator(T_UNOP, COMPL)); 137"\"" return (string()); 138"L\"" return (wcstrg()); 139";" return (T_SEMI); 140"{" return (T_LBRACE); 141"}" return (T_RBRACE); 142"," return (T_COMMA); 143":" return (T_COLON); 144"?" return (T_QUEST); 145"[" return (T_LBRACK); 146"]" return (T_RBRACK); 147"(" return (T_LPARN); 148")" return (T_RPARN); 149"..." return (T_ELLIPSE); 150"'" return (ccon()); 151"L'" return (wccon()); 152^#.*$ directive(); 153\n incline(); 154\t|" "|\f|\v ; 155"/*" comment(); 156. badchar(yytext[0]); 157 158%% 159 160static void 161incline() 162{ 163 curr_pos.p_line++; 164 if (curr_pos.p_file == csrc_pos.p_file) 165 csrc_pos.p_line++; 166} 167 168static void 169badchar(c) 170 int c; 171{ 172 /* unknown character \%o */ 173 error(250, c); 174} 175 176/* 177 * Keywords. 178 * During initialisation they are written to the symbol table. 179 */ 180static struct kwtab { 181 const char *kw_name; /* keyword */ 182 int kw_token; /* token returned by yylex() */ 183 scl_t kw_scl; /* storage class if kw_token T_SCLASS */ 184 tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */ 185 tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */ 186 u_int kw_stdc : 1; /* STDC keyword */ 187 u_int kw_gcc : 1; /* GCC keyword */ 188} kwtab[] = { 189 { "asm", T_ASM, 0, 0, 0, 0, 1 }, 190 { "__asm", T_ASM, 0, 0, 0, 0, 0 }, 191 { "__asm__", T_ASM, 0, 0, 0, 0, 0 }, 192 { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 }, 193 { "break", T_BREAK, 0, 0, 0, 0, 0 }, 194 { "case", T_CASE, 0, 0, 0, 0, 0 }, 195 { "char", T_TYPE, 0, CHAR, 0, 0, 0 }, 196 { "const", T_QUAL, 0, 0, CONST, 1, 0 }, 197 { "__const__", T_QUAL, 0, 0, CONST, 0, 0 }, 198 { "__const", T_QUAL, 0, 0, CONST, 0, 0 }, 199 { "continue", T_CONTINUE, 0, 0, 0, 0, 0 }, 200 { "default", T_DEFAULT, 0, 0, 0, 0, 0 }, 201 { "do", T_DO, 0, 0, 0, 0, 0 }, 202 { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 }, 203 { "else", T_ELSE, 0, 0, 0, 0, 0 }, 204 { "enum", T_ENUM, 0, 0, 0, 0, 0 }, 205 { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 }, 206 { "float", T_TYPE, 0, FLOAT, 0, 0, 0 }, 207 { "for", T_FOR, 0, 0, 0, 0, 0 }, 208 { "goto", T_GOTO, 0, 0, 0, 0, 0 }, 209 { "if", T_IF, 0, 0, 0, 0, 0 }, 210 { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 }, 211 { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 }, 212 { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 }, 213 { "int", T_TYPE, 0, INT, 0, 0, 0 }, 214 { "long", T_TYPE, 0, LONG, 0, 0, 0 }, 215 { "register", T_SCLASS, REG, 0, 0, 0, 0 }, 216 { "return", T_RETURN, 0, 0, 0, 0, 0 }, 217 { "short", T_TYPE, 0, SHORT, 0, 0, 0 }, 218 { "signed", T_TYPE, 0, SIGNED, 0, 1, 0 }, 219 { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0 }, 220 { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0 }, 221 { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0 }, 222 { "static", T_SCLASS, STATIC, 0, 0, 0, 0 }, 223 { "struct", T_SOU, 0, STRUCT, 0, 0, 0 }, 224 { "switch", T_SWITCH, 0, 0, 0, 0, 0 }, 225 { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0 }, 226 { "union", T_SOU, 0, UNION, 0, 0, 0 }, 227 { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0 }, 228 { "void", T_TYPE, 0, VOID, 0, 0, 0 }, 229 { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0 }, 230 { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 231 { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 232 { "while", T_WHILE, 0, 0, 0, 0, 0 }, 233 { NULL, 0, 0, 0, 0, 0, 0 } 234}; 235 236/* Symbol table */ 237static sym_t *symtab[HSHSIZ1]; 238 239/* bit i of the entry with index i is set */ 240u_quad_t qbmasks[sizeof(u_quad_t) * CHAR_BIT]; 241 242/* least significant i bits are set in the entry with index i */ 243u_quad_t qlmasks[sizeof(u_quad_t) * CHAR_BIT + 1]; 244 245/* least significant i bits are not set in the entry with index i */ 246u_quad_t qumasks[sizeof(u_quad_t) * CHAR_BIT + 1]; 247 248/* free list for sbuf structures */ 249static sbuf_t *sbfrlst; 250 251/* Typ of next expected symbol */ 252symt_t symtyp; 253 254 255/* 256 * All keywords are written to the symbol table. This saves us looking 257 * in a extra table for each name we found. 258 */ 259void 260initscan() 261{ 262 struct kwtab *kw; 263 sym_t *sym; 264 int h, i; 265 u_quad_t uq; 266 267 for (kw = kwtab; kw->kw_name != NULL; kw++) { 268 if (kw->kw_stdc && tflag) 269 continue; 270 if (kw->kw_gcc && !gflag) 271 continue; 272 sym = getblk(sizeof (sym_t)); 273 sym->s_name = kw->kw_name; 274 sym->s_keyw = 1; 275 sym->s_value.v_quad = kw->kw_token; 276 if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) { 277 sym->s_tspec = kw->kw_tspec; 278 } else if (kw->kw_token == T_SCLASS) { 279 sym->s_scl = kw->kw_scl; 280 } else if (kw->kw_token == T_QUAL) { 281 sym->s_tqual = kw->kw_tqual; 282 } 283 h = hash(sym->s_name); 284 if ((sym->s_link = symtab[h]) != NULL) 285 symtab[h]->s_rlink = &sym->s_link; 286 (symtab[h] = sym)->s_rlink = &symtab[h]; 287 } 288 289 /* initialize bit-masks for quads */ 290 for (i = 0; i < sizeof (u_quad_t) * CHAR_BIT; i++) { 291 qbmasks[i] = (u_quad_t)1 << i; 292 uq = ~(u_quad_t)0 << i; 293 qumasks[i] = uq; 294 qlmasks[i] = ~uq; 295 } 296 qumasks[i] = 0; 297 qlmasks[i] = ~(u_quad_t)0; 298} 299 300/* 301 * Get a free sbuf structure, if possible from the free list 302 */ 303static sbuf_t * 304allocsb() 305{ 306 sbuf_t *sb; 307 308 if ((sb = sbfrlst) != NULL) { 309 sbfrlst = sb->sb_nxt; 310 } else { 311 sb = xmalloc(sizeof (sbuf_t)); 312 } 313 (void)memset(sb, 0, sizeof (sb)); 314 return (sb); 315} 316 317/* 318 * Put a sbuf structure to the free list 319 */ 320static void 321freesb(sb) 322 sbuf_t *sb; 323{ 324 sb->sb_nxt = sbfrlst; 325 sbfrlst = sb; 326} 327 328/* 329 * Read a character and ensure that it is positive (except EOF). 330 * Increment line count(s) if necessary. 331 */ 332static int 333inpc() 334{ 335 int c; 336 337 if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n') 338 incline(); 339 return (c); 340} 341 342static int 343hash(s) 344 const char *s; 345{ 346 u_int v; 347 const u_char *us; 348 349 v = 0; 350 for (us = (const u_char *)s; *us != '\0'; us++) { 351 v = (v << sizeof (v)) + *us; 352 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 353 } 354 return (v % HSHSIZ1); 355} 356 357/* 358 * Lex has found a letter followed by zero or more letters or digits. 359 * It looks for a symbol in the symbol table with the same name. This 360 * symbol must either be a keyword or a symbol of the type required by 361 * symtyp (label, member, tag, ...). 362 * 363 * If it is a keyword, the token is returned. In some cases it is described 364 * more deeply by data written to yylval. 365 * 366 * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct 367 * is stored in yylval. This struct contains the name of the symbol, it's 368 * length and hash value. If there is already a symbol of the same name 369 * and type in the symbol table, the sbuf struct also contains a pointer 370 * to the symbol table entry. 371 */ 372static int 373name() 374{ 375 char *s; 376 sbuf_t *sb; 377 sym_t *sym; 378 int tok; 379 380 sb = allocsb(); 381 sb->sb_name = yytext; 382 sb->sb_len = yyleng; 383 sb->sb_hash = hash(yytext); 384 385 if ((sym = search(sb)) != NULL && sym->s_keyw) { 386 freesb(sb); 387 return (keyw(sym)); 388 } 389 390 sb->sb_sym = sym; 391 392 if (sym != NULL) { 393 if (blklev < sym->s_blklev) 394 lerror("name() 1"); 395 sb->sb_name = sym->s_name; 396 sb->sb_len = strlen(sym->s_name); 397 tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME; 398 } else { 399 s = getblk(yyleng + 1); 400 (void)memcpy(s, yytext, yyleng + 1); 401 sb->sb_name = s; 402 sb->sb_len = yyleng; 403 tok = T_NAME; 404 } 405 406 yylval.y_sb = sb; 407 return (tok); 408} 409 410static sym_t * 411search(sb) 412 sbuf_t *sb; 413{ 414 sym_t *sym; 415 416 for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) { 417 if (strcmp(sym->s_name, sb->sb_name) == 0) { 418 if (sym->s_keyw || sym->s_kind == symtyp) 419 return (sym); 420 } 421 } 422 423 return (NULL); 424} 425 426static int 427keyw(sym) 428 sym_t *sym; 429{ 430 int t; 431 432 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) { 433 yylval.y_scl = sym->s_scl; 434 } else if (t == T_TYPE || t == T_SOU) { 435 yylval.y_tspec = sym->s_tspec; 436 } else if (t == T_QUAL) { 437 yylval.y_tqual = sym->s_tqual; 438 } 439 return (t); 440} 441 442/* 443 * Convert a string representing an integer into internal representation. 444 * The value is returned in yylval. icon() (and yylex()) returns T_CON. 445 */ 446static int 447icon(base) 448 int base; 449{ 450 int l_suffix, u_suffix; 451 int len; 452 const char *cp; 453 char c, *eptr; 454 tspec_t typ; 455 u_long ul; 456 u_quad_t uq; 457 int ansiu; 458 static tspec_t contypes[2][3] = { 459 { INT, LONG, QUAD }, 460 { UINT, ULONG, UQUAD } 461 }; 462 463 cp = yytext; 464 len = yyleng; 465 466 /* skip 0x */ 467 if (base == 16) { 468 cp += 2; 469 len -= 2; 470 } 471 472 /* read suffixes */ 473 l_suffix = u_suffix = 0; 474 for ( ; ; ) { 475 if ((c = cp[len - 1]) == 'l' || c == 'L') { 476 l_suffix++; 477 } else if (c == 'u' || c == 'U') { 478 u_suffix++; 479 } else { 480 break; 481 } 482 len--; 483 } 484 if (l_suffix > 2 || u_suffix > 1) { 485 /* malformed integer constant */ 486 warning(251); 487 if (l_suffix > 2) 488 l_suffix = 2; 489 if (u_suffix > 1) 490 u_suffix = 1; 491 } 492 if (tflag && u_suffix != 0) { 493 /* suffix U is illegal in traditional C */ 494 warning(97); 495 } 496 typ = contypes[u_suffix][l_suffix]; 497 498 errno = 0; 499 if (l_suffix < 2) { 500 ul = strtoul(cp, &eptr, base); 501 } else { 502 uq = strtouq(cp, &eptr, base); 503 } 504 if (eptr != cp + len) 505 lerror("icon() 1"); 506 if (errno != 0) 507 /* integer constant out of range */ 508 warning(252); 509 510 /* 511 * If the value is to big for the current type, we must choose 512 * another type. 513 */ 514 ansiu = 0; 515 switch (typ) { 516 case INT: 517 if (ul <= INT_MAX) { 518 /* ok */ 519 } else if (ul <= (unsigned)UINT_MAX && base != 10) { 520 typ = UINT; 521 } else if (ul <= LONG_MAX) { 522 typ = LONG; 523 } else { 524 typ = ULONG; 525 } 526 if (typ == UINT || typ == ULONG) { 527 if (tflag) { 528 typ = LONG; 529 } else if (!sflag) { 530 /* 531 * Remember that the constant is unsigned 532 * only in ANSI C 533 */ 534 ansiu = 1; 535 } 536 } 537 break; 538 case UINT: 539 if (ul > (u_int)UINT_MAX) 540 typ = ULONG; 541 break; 542 case LONG: 543 if (ul > LONG_MAX && !tflag) { 544 typ = ULONG; 545 if (!sflag) 546 ansiu = 1; 547 } 548 break; 549 case QUAD: 550 if (uq > QUAD_MAX && !tflag) { 551 typ = UQUAD; 552 if (!sflag) 553 ansiu = 1; 554 } 555 break; 556 /* LINTED (enumeration values not handled in switch) */ 557 } 558 559 if (typ != QUAD && typ != UQUAD) { 560 if (isutyp(typ)) { 561 uq = ul; 562 } else { 563 uq = (quad_t)(long)ul; 564 } 565 } 566 567 uq = (u_quad_t)xsign((quad_t)uq, typ, -1); 568 569 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ; 570 yylval.y_val->v_ansiu = ansiu; 571 yylval.y_val->v_quad = (quad_t)uq; 572 573 return (T_CON); 574} 575 576/* 577 * Returns 1 if t is a signed type and the value is negative. 578 * 579 * len is the number of significant bits. If len is -1, len is set 580 * to the width of type t. 581 */ 582int 583sign(q, t, len) 584 quad_t q; 585 tspec_t t; 586 int len; 587{ 588 if (t == PTR || isutyp(t)) 589 return (0); 590 return (msb(q, t, len)); 591} 592 593int 594msb(q, t, len) 595 quad_t q; 596 tspec_t t; 597 int len; 598{ 599 if (len <= 0) 600 len = size(t); 601 return ((q & qbmasks[len - 1]) != 0); 602} 603 604/* 605 * Extends the sign of q. 606 */ 607quad_t 608xsign(q, t, len) 609 quad_t q; 610 tspec_t t; 611 int len; 612{ 613 if (len <= 0) 614 len = size(t); 615 616 if (t == PTR || isutyp(t) || !sign(q, t, len)) { 617 q &= qlmasks[len]; 618 } else { 619 q |= qumasks[len]; 620 } 621 return (q); 622} 623 624/* 625 * Convert a string representing a floating point value into its interal 626 * representation. Type and value are returned in yylval. fcon() 627 * (and yylex()) returns T_CON. 628 * XXX Currently it is not possible to convert constants of type 629 * long double which are greater then DBL_MAX. 630 */ 631static int 632fcon() 633{ 634 const char *cp; 635 int len; 636 tspec_t typ; 637 char c, *eptr; 638 double d; 639 float f; 640 641 cp = yytext; 642 len = yyleng; 643 644 if ((c = cp[len - 1]) == 'f' || c == 'F') { 645 typ = FLOAT; 646 len--; 647 } else if (c == 'l' || c == 'L') { 648 typ = LDOUBLE; 649 len--; 650 } else { 651 typ = DOUBLE; 652 } 653 654 if (tflag && typ != DOUBLE) { 655 /* suffixes F and L are illegal in traditional C */ 656 warning(98); 657 } 658 659 errno = 0; 660 d = strtod(cp, &eptr); 661 if (eptr != cp + len) 662 lerror("fcon() 1"); 663 if (errno != 0) 664 /* floating-point constant out of range */ 665 warning(248); 666 667 if (typ == FLOAT) { 668 f = (float)d; 669 if (isinf(f)) { 670 /* floating-point constant out of range */ 671 warning(248); 672 f = f > 0 ? FLT_MAX : -FLT_MAX; 673 } 674 } 675 676 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ; 677 if (typ == FLOAT) { 678 yylval.y_val->v_ldbl = f; 679 } else { 680 yylval.y_val->v_ldbl = d; 681 } 682 683 return (T_CON); 684} 685 686static int 687operator(t, o) 688 int t; 689 op_t o; 690{ 691 yylval.y_op = o; 692 return (t); 693} 694 695/* 696 * Called if lex found a leading \'. 697 */ 698static int 699ccon() 700{ 701 int n, val, c; 702 char cv; 703 704 n = 0; 705 val = 0; 706 while ((c = getescc('\'')) >= 0) { 707 val = (val << CHAR_BIT) + c; 708 n++; 709 } 710 if (c == -2) { 711 /* unterminated character constant */ 712 error(253); 713 } else { 714 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) { 715 /* too many characters in character constant */ 716 error(71); 717 } else if (n > 1) { 718 /* multi-character character constant */ 719 warning(294); 720 } else if (n == 0) { 721 /* empty character constant */ 722 error(73); 723 } 724 } 725 if (n == 1) { 726 cv = (char)val; 727 val = cv; 728 } 729 730 yylval.y_val = xcalloc(1, sizeof (val_t)); 731 yylval.y_val->v_tspec = INT; 732 yylval.y_val->v_quad = val; 733 734 return (T_CON); 735} 736 737/* 738 * Called if lex found a leading L\' 739 */ 740static int 741wccon() 742{ 743 static char buf[MB_LEN_MAX + 1]; 744 int i, c; 745 wchar_t wc; 746 747 i = 0; 748 while ((c = getescc('\'')) >= 0) { 749 if (i < MB_CUR_MAX) 750 buf[i] = (char)c; 751 i++; 752 } 753 754 wc = 0; 755 756 if (c == -2) { 757 /* unterminated character constant */ 758 error(253); 759 } else if (c == 0) { 760 /* empty character constant */ 761 error(73); 762 } else { 763 if (i > MB_CUR_MAX) { 764 i = MB_CUR_MAX; 765 /* too many characters in character constant */ 766 error(71); 767 } else { 768 buf[i] = '\0'; 769 (void)mbtowc(NULL, NULL, 0); 770 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0) 771 /* invalid multibyte character */ 772 error(291); 773 } 774 } 775 776 yylval.y_val = xcalloc(1, sizeof (val_t)); 777 yylval.y_val->v_tspec = WCHAR; 778 yylval.y_val->v_quad = wc; 779 780 return (T_CON); 781} 782 783/* 784 * Read a character which is part of a character constant or of a string 785 * and handle escapes. 786 * 787 * The Argument is the character which delimits the character constant or 788 * string. 789 * 790 * Returns -1 if the end of the character constant or string is reached, 791 * -2 if the EOF is reached, and the charachter otherwise. 792 */ 793static int 794getescc(d) 795 int d; 796{ 797 static int pbc = -1; 798 int n, c, v; 799 800 if (pbc == -1) { 801 c = inpc(); 802 } else { 803 c = pbc; 804 pbc = -1; 805 } 806 if (c == d) 807 return (-1); 808 switch (c) { 809 case '\n': 810 /* newline in string or char constant */ 811 error(254); 812 return (-2); 813 case EOF: 814 return (-2); 815 case '\\': 816 switch (c = inpc()) { 817 case '"': 818 if (tflag && d == '\'') 819 /* \" inside character constant undef. ... */ 820 warning(262); 821 return ('"'); 822 case '\'': 823 return ('\''); 824 case '?': 825 if (tflag) 826 /* \? undefined in traditional C */ 827 warning(263); 828 return ('?'); 829 case '\\': 830 return ('\\'); 831 case 'a': 832 if (tflag) 833 /* \a undefined in traditional C */ 834 warning(81); 835#ifdef __STDC__ 836 return ('\a'); 837#else 838 return ('\007'); 839#endif 840 case 'b': 841 return ('\b'); 842 case 'f': 843 return ('\f'); 844 case 'n': 845 return ('\n'); 846 case 'r': 847 return ('\r'); 848 case 't': 849 return ('\t'); 850 case 'v': 851 if (tflag) 852 /* \v undefined in traditional C */ 853 warning(264); 854#ifdef __STDC__ 855 return ('\v'); 856#else 857 return ('\013'); 858#endif 859 case '8': case '9': 860 /* bad octal digit %c */ 861 warning(77, c); 862 /* FALLTHROUGH */ 863 case '0': case '1': case '2': case '3': 864 case '4': case '5': case '6': case '7': 865 n = 3; 866 v = 0; 867 do { 868 v = (v << 3) + (c - '0'); 869 c = inpc(); 870 } while (--n && isdigit(c) && (tflag || c <= '7')); 871 if (tflag && n > 0 && isdigit(c)) 872 /* bad octal digit %c */ 873 warning(77, c); 874 pbc = c; 875 if (v > UCHAR_MAX) { 876 /* character escape does not fit in char. */ 877 warning(76); 878 v &= CHAR_MASK; 879 } 880 return (v); 881 case 'x': 882 if (tflag) 883 /* \x undefined in traditional C */ 884 warning(82); 885 v = 0; 886 n = 0; 887 while ((c = inpc()) >= 0 && isxdigit(c)) { 888 c = isdigit(c) ? 889 c - '0' : toupper(c) - 'A' + 10; 890 v = (v << 4) + c; 891 if (n >= 0) { 892 if ((v & ~CHAR_MASK) != 0) { 893 /* overflow in hex escape */ 894 warning(75); 895 n = -1; 896 } else { 897 n++; 898 } 899 } 900 } 901 pbc = c; 902 if (n == 0) { 903 /* no hex digits follow \x */ 904 error(74); 905 } if (n == -1) { 906 v &= CHAR_MASK; 907 } 908 return (v); 909 case '\n': 910 return (getescc(d)); 911 case EOF: 912 return (-2); 913 default: 914 if (isprint(c)) { 915 /* dubious escape \%c */ 916 warning(79, c); 917 } else { 918 /* dubious escape \%o */ 919 warning(80, c); 920 } 921 } 922 } 923 return (c); 924} 925 926/* 927 * Called for preprocessor directives. Currently implemented are: 928 * # lineno 929 * # lineno "filename" 930 */ 931static void 932directive() 933{ 934 const char *cp, *fn; 935 char c, *eptr; 936 size_t fnl; 937 long ln; 938 static int first = 1; 939 940 /* Go to first non-whitespace after # */ 941 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) ; 942 943 if (!isdigit(c)) { 944 error: 945 /* undefined or invalid # directive */ 946 warning(255); 947 return; 948 } 949 ln = strtol(--cp, &eptr, 10); 950 if (cp == eptr) 951 goto error; 952 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0') 953 goto error; 954 while ((c = *cp++) == ' ' || c == '\t') ; 955 if (c != '\0') { 956 if (c != '"') 957 goto error; 958 fn = cp; 959 while ((c = *cp) != '"' && c != '\0') 960 cp++; 961 if (c != '"') 962 goto error; 963 if ((fnl = cp++ - fn) > PATH_MAX) 964 goto error; 965 while ((c = *cp++) == ' ' || c == '\t') ; 966#if 0 967 if (c != '\0') 968 warning("extra character(s) after directive"); 969#endif 970 curr_pos.p_file = fnnalloc(fn, fnl); 971 /* 972 * If this is the first directive, the name is the name 973 * of the C source file as specified at the command line. 974 * It is written to the output file. 975 */ 976 if (first) { 977 csrc_pos.p_file = curr_pos.p_file; 978 outsrc(curr_pos.p_file); 979 first = 0; 980 } 981 } 982 curr_pos.p_line = (int)ln - 1; 983 if (curr_pos.p_file == csrc_pos.p_file) 984 csrc_pos.p_line = (int)ln - 1; 985} 986 987/* 988 * Handle lint comments. Following comments are currently understood: 989 * ARGSUSEDn 990 * CONSTCOND CONSTANTCOND CONSTANTCONDITION 991 * FALLTHRU FALLTHROUGH 992 * LINTLIBRARY 993 * LINTED NOSTRICT 994 * LONGLONG 995 * NOTREACHED 996 * PRINTFLIKEn 997 * PROTOLIB 998 * SCANFLIKEn 999 * VARARGSn 1000 * If one of this comments is recognized, the arguments, if any, are 1001 * parsed and a function which handles this comment is called. 1002 */ 1003static void 1004comment() 1005{ 1006 int c, lc; 1007 static struct { 1008 const char *keywd; 1009 int arg; 1010 void (*func) __P((int)); 1011 } keywtab[] = { 1012 { "ARGSUSED", 1, argsused }, 1013 { "CONSTCOND", 0, constcond }, 1014 { "CONSTANTCOND", 0, constcond }, 1015 { "CONSTANTCONDITION", 0, constcond }, 1016 { "FALLTHRU", 0, fallthru }, 1017 { "FALLTHROUGH", 0, fallthru }, 1018 { "LINTLIBRARY", 0, lintlib }, 1019 { "LINTED", 0, linted }, 1020 { "LONGLONG", 0, longlong }, 1021 { "NOSTRICT", 0, linted }, 1022 { "NOTREACHED", 0, notreach }, 1023 { "PRINTFLIKE", 1, printflike }, 1024 { "PROTOLIB", 1, protolib }, 1025 { "SCANFLIKE", 1, scanflike }, 1026 { "VARARGS", 1, varargs }, 1027 }; 1028 char keywd[32]; 1029 char arg[32]; 1030 int l, i, a; 1031 int eoc; 1032 1033 eoc = 0; 1034 1035 /* Skip white spaces after the start of the comment */ 1036 while ((c = inpc()) != EOF && isspace(c)) ; 1037 1038 /* Read the potential keyword to keywd */ 1039 l = 0; 1040 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) { 1041 keywd[l++] = (char)c; 1042 c = inpc(); 1043 } 1044 keywd[l] = '\0'; 1045 1046 /* look for the keyword */ 1047 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) { 1048 if (strcmp(keywtab[i].keywd, keywd) == 0) 1049 break; 1050 } 1051 if (i == sizeof (keywtab) / sizeof (keywtab[0])) 1052 goto skip_rest; 1053 1054 /* skip white spaces after the keyword */ 1055 while (c != EOF && isspace(c)) 1056 c = inpc(); 1057 1058 /* read the argument, if the keyword accepts one and there is one */ 1059 l = 0; 1060 if (keywtab[i].arg) { 1061 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) { 1062 arg[l++] = (char)c; 1063 c = inpc(); 1064 } 1065 } 1066 arg[l] = '\0'; 1067 a = l != 0 ? atoi(arg) : -1; 1068 1069 /* skip white spaces after the argument */ 1070 while (c != EOF && isspace(c)) 1071 c = inpc(); 1072 1073 if (c != '*' || (c = inpc()) != '/') { 1074 if (keywtab[i].func != linted) 1075 /* extra characters in lint comment */ 1076 warning(257); 1077 } else { 1078 /* 1079 * remember that we have already found the end of the 1080 * comment 1081 */ 1082 eoc = 1; 1083 } 1084 1085 if (keywtab[i].func != NULL) 1086 (*keywtab[i].func)(a); 1087 1088 skip_rest: 1089 while (!eoc) { 1090 lc = c; 1091 if ((c = inpc()) == EOF) { 1092 /* unterminated comment */ 1093 error(256); 1094 break; 1095 } 1096 if (lc == '*' && c == '/') 1097 eoc = 1; 1098 } 1099} 1100 1101/* 1102 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND. 1103 * clrwflgs() is called after function definitions and global and 1104 * local declarations and definitions. It is also called between 1105 * the controlling expression and the body of control statements 1106 * (if, switch, for, while). 1107 */ 1108void 1109clrwflgs() 1110{ 1111 nowarn = 0; 1112 quadflg = 0; 1113 ccflg = 0; 1114} 1115 1116/* 1117 * Strings are stored in a dynamically alloceted buffer and passed 1118 * in yylval.y_xstrg to the parser. The parser or the routines called 1119 * by the parser are responsible for freeing this buffer. 1120 */ 1121static int 1122string() 1123{ 1124 u_char *s; 1125 int c; 1126 size_t len, max; 1127 strg_t *strg; 1128 1129 s = xmalloc(max = 64); 1130 1131 len = 0; 1132 while ((c = getescc('"')) >= 0) { 1133 /* +1 to reserve space for a trailing NUL character */ 1134 if (len + 1 == max) 1135 s = xrealloc(s, max *= 2); 1136 s[len++] = (char)c; 1137 } 1138 s[len] = '\0'; 1139 if (c == -2) 1140 /* unterminated string constant */ 1141 error(258); 1142 1143 strg = xcalloc(1, sizeof (strg_t)); 1144 strg->st_tspec = CHAR; 1145 strg->st_len = len; 1146 strg->st_cp = s; 1147 1148 yylval.y_strg = strg; 1149 return (T_STRING); 1150} 1151 1152static int 1153wcstrg() 1154{ 1155 char *s; 1156 int c, i, n, wi; 1157 size_t len, max, wlen; 1158 wchar_t *ws; 1159 strg_t *strg; 1160 1161 s = xmalloc(max = 64); 1162 len = 0; 1163 while ((c = getescc('"')) >= 0) { 1164 /* +1 to save space for a trailing NUL character */ 1165 if (len + 1 >= max) 1166 s = xrealloc(s, max *= 2); 1167 s[len++] = (char)c; 1168 } 1169 s[len] = '\0'; 1170 if (c == -2) 1171 /* unterminated string constant */ 1172 error(258); 1173 1174 /* get length of wide character string */ 1175 (void)mblen(NULL, 0); 1176 for (i = 0, wlen = 0; i < len; i += n, wlen++) { 1177 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) { 1178 /* invalid multibyte character */ 1179 error(291); 1180 break; 1181 } 1182 if (n == 0) 1183 n = 1; 1184 } 1185 1186 ws = xmalloc((wlen + 1) * sizeof (wchar_t)); 1187 1188 /* convert from multibyte to wide char */ 1189 (void)mbtowc(NULL, NULL, 0); 1190 for (i = 0, wi = 0; i < len; i += n, wi++) { 1191 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1) 1192 break; 1193 if (n == 0) 1194 n = 1; 1195 } 1196 ws[wi] = 0; 1197 free(s); 1198 1199 strg = xcalloc(1, sizeof (strg_t)); 1200 strg->st_tspec = WCHAR; 1201 strg->st_len = wlen; 1202 strg->st_wcp = ws; 1203 1204 yylval.y_strg = strg; 1205 return (T_STRING); 1206} 1207 1208/* 1209 * As noted above the scanner does not create new symbol table entries 1210 * for symbols it cannot find in the symbol table. This is to avoid 1211 * putting undeclared symbols into the symbol table if a syntax error 1212 * occurs. 1213 * 1214 * getsym() is called as soon as it is probably ok to put the symbol to 1215 * the symbol table. This does not mean that it is not possible that 1216 * symbols are put to the symbol table which are than not completely 1217 * declared due to syntax errors. To avoid too many problems in this 1218 * case symbols get type int in getsym(). 1219 * 1220 * XXX calls to getsym() should be delayed until decl1*() is called 1221 */ 1222sym_t * 1223getsym(sb) 1224 sbuf_t *sb; 1225{ 1226 dinfo_t *di; 1227 char *s; 1228 sym_t *sym; 1229 1230 sym = sb->sb_sym; 1231 1232 /* 1233 * During member declaration it is possible that name() looked 1234 * for symbols of type FVFT, although it should have looked for 1235 * symbols of type FTAG. Same can happen for labels. Both cases 1236 * are compensated here. 1237 */ 1238 if (symtyp == FMOS || symtyp == FLAB) { 1239 if (sym == NULL || sym->s_kind == FVFT) 1240 sym = search(sb); 1241 } 1242 1243 if (sym != NULL) { 1244 if (sym->s_kind != symtyp) 1245 lerror("storesym() 1"); 1246 symtyp = FVFT; 1247 freesb(sb); 1248 return (sym); 1249 } 1250 1251 /* create a new symbol table entry */ 1252 1253 /* labels must always be allocated at level 1 (outhermost block) */ 1254 if (symtyp == FLAB) { 1255 sym = getlblk(1, sizeof (sym_t)); 1256 s = getlblk(1, sb->sb_len + 1); 1257 (void)memcpy(s, sb->sb_name, sb->sb_len + 1); 1258 sym->s_name = s; 1259 sym->s_blklev = 1; 1260 di = dcs; 1261 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL) 1262 di = di->d_nxt; 1263 if (di->d_ctx != AUTO) 1264 lerror("storesym() 2"); 1265 } else { 1266 sym = getblk(sizeof (sym_t)); 1267 sym->s_name = sb->sb_name; 1268 sym->s_blklev = blklev; 1269 di = dcs; 1270 } 1271 1272 STRUCT_ASSIGN(sym->s_dpos, curr_pos); 1273 if ((sym->s_kind = symtyp) != FLAB) 1274 sym->s_type = gettyp(INT); 1275 1276 symtyp = FVFT; 1277 1278 if ((sym->s_link = symtab[sb->sb_hash]) != NULL) 1279 symtab[sb->sb_hash]->s_rlink = &sym->s_link; 1280 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash]; 1281 1282 *di->d_ldlsym = sym; 1283 di->d_ldlsym = &sym->s_dlnxt; 1284 1285 freesb(sb); 1286 return (sym); 1287} 1288 1289/* 1290 * Remove a symbol forever from the symbol table. s_blklev 1291 * is set to -1 to avoid that the symbol will later be put 1292 * back to the symbol table. 1293 */ 1294void 1295rmsym(sym) 1296 sym_t *sym; 1297{ 1298 if ((*sym->s_rlink = sym->s_link) != NULL) 1299 sym->s_link->s_rlink = sym->s_rlink; 1300 sym->s_blklev = -1; 1301 sym->s_link = NULL; 1302} 1303 1304/* 1305 * Remove a list of symbols declared at one level from the symbol 1306 * table. 1307 */ 1308void 1309rmsyms(syms) 1310 sym_t *syms; 1311{ 1312 sym_t *sym; 1313 1314 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) { 1315 if (sym->s_blklev != -1) { 1316 if ((*sym->s_rlink = sym->s_link) != NULL) 1317 sym->s_link->s_rlink = sym->s_rlink; 1318 sym->s_link = NULL; 1319 sym->s_rlink = NULL; 1320 } 1321 } 1322} 1323 1324/* 1325 * Put a symbol into the symbol table 1326 */ 1327void 1328inssym(bl, sym) 1329 int bl; 1330 sym_t *sym; 1331{ 1332 int h; 1333 1334 h = hash(sym->s_name); 1335 if ((sym->s_link = symtab[h]) != NULL) 1336 symtab[h]->s_rlink = &sym->s_link; 1337 (symtab[h] = sym)->s_rlink = &symtab[h]; 1338 sym->s_blklev = bl; 1339 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev) 1340 lerror("inssym()"); 1341} 1342 1343/* 1344 * Called at level 0 after syntax errors 1345 * Removes all symbols which are not declared at level 0 from the 1346 * symbol table. Also frees all memory which is not associated with 1347 * level 0. 1348 */ 1349void 1350cleanup() 1351{ 1352 sym_t *sym, *nsym; 1353 int i; 1354 1355 for (i = 0; i < HSHSIZ1; i++) { 1356 for (sym = symtab[i]; sym != NULL; sym = nsym) { 1357 nsym = sym->s_link; 1358 if (sym->s_blklev >= 1) { 1359 if ((*sym->s_rlink = nsym) != NULL) 1360 nsym->s_rlink = sym->s_rlink; 1361 } 1362 } 1363 } 1364 1365 for (i = mblklev; i > 0; i--) 1366 freelblk(i); 1367} 1368 1369/* 1370 * Create a new symbol with the name of an existing symbol. 1371 */ 1372sym_t * 1373pushdown(sym) 1374 sym_t *sym; 1375{ 1376 int h; 1377 sym_t *nsym; 1378 1379 h = hash(sym->s_name); 1380 nsym = getblk(sizeof (sym_t)); 1381 if (sym->s_blklev > blklev) 1382 lerror("pushdown()"); 1383 nsym->s_name = sym->s_name; 1384 STRUCT_ASSIGN(nsym->s_dpos, curr_pos); 1385 nsym->s_kind = sym->s_kind; 1386 nsym->s_blklev = blklev; 1387 1388 if ((nsym->s_link = symtab[h]) != NULL) 1389 symtab[h]->s_rlink = &nsym->s_link; 1390 (symtab[h] = nsym)->s_rlink = &symtab[h]; 1391 1392 *dcs->d_ldlsym = nsym; 1393 dcs->d_ldlsym = &nsym->s_dlnxt; 1394 1395 return (nsym); 1396} 1397 1398/* 1399 * Free any dynamically allocated memory referenced by 1400 * the value stack or yylval. 1401 * The type of information in yylval is described by tok. 1402 */ 1403void 1404freeyyv(sp, tok) 1405 void *sp; 1406 int tok; 1407{ 1408 if (tok == T_NAME || tok == T_TYPENAME) { 1409 sbuf_t *sb = *(sbuf_t **)sp; 1410 freesb(sb); 1411 } else if (tok == T_CON) { 1412 val_t *val = *(val_t **)sp; 1413 free(val); 1414 } else if (tok == T_STRING) { 1415 strg_t *strg = *(strg_t **)sp; 1416 if (strg->st_tspec == CHAR) { 1417 free(strg->st_cp); 1418 } else if (strg->st_tspec == WCHAR) { 1419 free(strg->st_wcp); 1420 } else { 1421 lerror("fryylv() 1"); 1422 } 1423 free(strg); 1424 } 1425} 1426