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