scan.l revision 108532
112099Sjoerg%{ 291592Smarkm/* $NetBSD: scan.l,v 1.26 2002/01/31 22:30:21 tv Exp $ */ 312099Sjoerg 412099Sjoerg/* 591592Smarkm * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 612099Sjoerg * Copyright (c) 1994, 1995 Jochen Pohl 712099Sjoerg * All Rights Reserved. 812099Sjoerg * 912099Sjoerg * Redistribution and use in source and binary forms, with or without 1012099Sjoerg * modification, are permitted provided that the following conditions 1112099Sjoerg * are met: 1212099Sjoerg * 1. Redistributions of source code must retain the above copyright 1312099Sjoerg * notice, this list of conditions and the following disclaimer. 1412099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1512099Sjoerg * notice, this list of conditions and the following disclaimer in the 1612099Sjoerg * documentation and/or other materials provided with the distribution. 1712099Sjoerg * 3. All advertising materials mentioning features or use of this software 1812099Sjoerg * must display the following acknowledgement: 1912099Sjoerg * This product includes software developed by Jochen Pohl for 2012099Sjoerg * The NetBSD Project. 2112099Sjoerg * 4. The name of the author may not be used to endorse or promote products 2212099Sjoerg * derived from this software without specific prior written permission. 2312099Sjoerg * 2412099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2512099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2612099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2712099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2812099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2912099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3012099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3112099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3212099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3312099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3412099Sjoerg */ 3512099Sjoerg 3691592Smarkm#include <sys/cdefs.h> 3791592Smarkm#if defined(__RCSID) && !defined(lint) 3891592Smarkm__RCSID("$NetBSD: scan.l,v 1.26 2002/01/31 22:30:21 tv Exp $"); 3912099Sjoerg#endif 4091592Smarkm__FBSDID("$FreeBSD: head/usr.bin/xlint/lint1/scan.l 108532 2003-01-01 18:13:45Z schweikh $"); 4112099Sjoerg 4212099Sjoerg#include <stdlib.h> 4312099Sjoerg#include <string.h> 4412099Sjoerg#include <limits.h> 4512099Sjoerg#include <float.h> 4612099Sjoerg#include <ctype.h> 4712099Sjoerg#include <errno.h> 4812099Sjoerg#include <err.h> 4917142Sjkh#include <math.h> 5012099Sjoerg 5112099Sjoerg#include "lint1.h" 5291592Smarkm#include "cgram.h" 5312099Sjoerg 5412099Sjoerg#define CHAR_MASK (~(~0 << CHAR_BIT)) 5591592Smarkm#define YY_NO_UNPUT 5612099Sjoerg 5712099Sjoerg/* Current position (its also updated when an included file is parsed) */ 5891592Smarkmpos_t curr_pos = { 1, "", 0 }; 5912099Sjoerg 6012099Sjoerg/* 6112099Sjoerg * Current position in C source (not updated when an included file is 6212099Sjoerg * parsed). 6312099Sjoerg */ 6491592Smarkmpos_t csrc_pos = { 1, "", 0 }; 6512099Sjoerg 6691592Smarkmstatic void incline(void); 6791592Smarkmstatic void badchar(int); 6891592Smarkmstatic sbuf_t *allocsb(void); 6991592Smarkmstatic void freesb(sbuf_t *); 7091592Smarkmstatic int inpc(void); 7191592Smarkmstatic int hash(const char *); 7291592Smarkmstatic sym_t *search(sbuf_t *); 7391592Smarkmstatic int name(void); 7491592Smarkmstatic int keyw(sym_t *); 7591592Smarkmstatic int icon(int); 7691592Smarkmstatic int fcon(void); 7791592Smarkmstatic int operator(int, op_t); 7891592Smarkmstatic int ccon(void); 7991592Smarkmstatic int wccon(void); 8091592Smarkmstatic int getescc(int); 8191592Smarkmstatic void directive(void); 8291592Smarkmstatic void comment(void); 8391592Smarkmstatic void slashslashcomment(void); 8491592Smarkmstatic int string(void); 8591592Smarkmstatic int wcstrg(void); 8612099Sjoerg 8712099Sjoerg%} 8812099Sjoerg 8912099SjoergL [_A-Za-z] 9012099SjoergD [0-9] 9112099SjoergNZD [1-9] 9212099SjoergOD [0-7] 9312099SjoergHD [0-9A-Fa-f] 9412099SjoergEX ([eE][+-]?[0-9]+) 9512099Sjoerg 9612099Sjoerg%% 9712099Sjoerg 9812099Sjoerg{L}({L}|{D})* return (name()); 9912099Sjoerg0{OD}*[lLuU]* return (icon(8)); 10012099Sjoerg{NZD}{D}*[lLuU]* return (icon(10)); 10112099Sjoerg0[xX]{HD}+[lLuU]* return (icon(16)); 10212099Sjoerg{D}+\.{D}*{EX}?[fFlL]? | 10312099Sjoerg{D}+{EX}[fFlL]? | 10412099Sjoerg\.{D}+{EX}?[fFlL]? return (fcon()); 10512099Sjoerg"=" return (operator(T_ASSIGN, ASSIGN)); 10612099Sjoerg"*=" return (operator(T_OPASS, MULASS)); 10712099Sjoerg"/=" return (operator(T_OPASS, DIVASS)); 10812099Sjoerg"%=" return (operator(T_OPASS, MODASS)); 10912099Sjoerg"+=" return (operator(T_OPASS, ADDASS)); 11012099Sjoerg"-=" return (operator(T_OPASS, SUBASS)); 11112099Sjoerg"<<=" return (operator(T_OPASS, SHLASS)); 11212099Sjoerg">>=" return (operator(T_OPASS, SHRASS)); 11312099Sjoerg"&=" return (operator(T_OPASS, ANDASS)); 11412099Sjoerg"^=" return (operator(T_OPASS, XORASS)); 11512099Sjoerg"|=" return (operator(T_OPASS, ORASS)); 11612099Sjoerg"||" return (operator(T_LOGOR, LOGOR)); 11712099Sjoerg"&&" return (operator(T_LOGAND, LOGAND)); 11812099Sjoerg"|" return (operator(T_OR, OR)); 11912099Sjoerg"&" return (operator(T_AND, AND)); 12012099Sjoerg"^" return (operator(T_XOR, XOR)); 12112099Sjoerg"==" return (operator(T_EQOP, EQ)); 12212099Sjoerg"!=" return (operator(T_EQOP, NE)); 12312099Sjoerg"<" return (operator(T_RELOP, LT)); 12412099Sjoerg">" return (operator(T_RELOP, GT)); 12512099Sjoerg"<=" return (operator(T_RELOP, LE)); 12612099Sjoerg">=" return (operator(T_RELOP, GE)); 12712099Sjoerg"<<" return (operator(T_SHFTOP, SHL)); 12812099Sjoerg">>" return (operator(T_SHFTOP, SHR)); 12912099Sjoerg"++" return (operator(T_INCDEC, INC)); 13012099Sjoerg"--" return (operator(T_INCDEC, DEC)); 13112099Sjoerg"->" return (operator(T_STROP, ARROW)); 13212099Sjoerg"." return (operator(T_STROP, POINT)); 13312099Sjoerg"+" return (operator(T_ADDOP, PLUS)); 13412099Sjoerg"-" return (operator(T_ADDOP, MINUS)); 13512099Sjoerg"*" return (operator(T_MULT, MULT)); 13612099Sjoerg"/" return (operator(T_DIVOP, DIV)); 13712099Sjoerg"%" return (operator(T_DIVOP, MOD)); 13812099Sjoerg"!" return (operator(T_UNOP, NOT)); 13912099Sjoerg"~" return (operator(T_UNOP, COMPL)); 14012099Sjoerg"\"" return (string()); 14112099Sjoerg"L\"" return (wcstrg()); 14212099Sjoerg";" return (T_SEMI); 14312099Sjoerg"{" return (T_LBRACE); 14412099Sjoerg"}" return (T_RBRACE); 14512099Sjoerg"," return (T_COMMA); 14612099Sjoerg":" return (T_COLON); 14712099Sjoerg"?" return (T_QUEST); 14812099Sjoerg"[" return (T_LBRACK); 14912099Sjoerg"]" return (T_RBRACK); 15012099Sjoerg"(" return (T_LPARN); 15112099Sjoerg")" return (T_RPARN); 15212099Sjoerg"..." return (T_ELLIPSE); 15312099Sjoerg"'" return (ccon()); 15412099Sjoerg"L'" return (wccon()); 15512099Sjoerg^#.*$ directive(); 15612099Sjoerg\n incline(); 15712099Sjoerg\t|" "|\f|\v ; 15812099Sjoerg"/*" comment(); 15991592Smarkm"//" slashslashcomment(); 16012099Sjoerg. badchar(yytext[0]); 16112099Sjoerg 16212099Sjoerg%% 16312099Sjoerg 16412099Sjoergstatic void 16591592Smarkmincline(void) 16612099Sjoerg{ 16712099Sjoerg curr_pos.p_line++; 16891592Smarkm curr_pos.p_uniq = 0; 16991592Smarkm if (curr_pos.p_file == csrc_pos.p_file) { 17012099Sjoerg csrc_pos.p_line++; 17191592Smarkm csrc_pos.p_uniq = 0; 17291592Smarkm } 17312099Sjoerg} 17412099Sjoerg 17512099Sjoergstatic void 17691592Smarkmbadchar(int c) 17712099Sjoerg{ 17891592Smarkm 17912099Sjoerg /* unknown character \%o */ 18012099Sjoerg error(250, c); 18112099Sjoerg} 18212099Sjoerg 18312099Sjoerg/* 18412099Sjoerg * Keywords. 18512099Sjoerg * During initialisation they are written to the symbol table. 18612099Sjoerg */ 18712099Sjoergstatic struct kwtab { 18812099Sjoerg const char *kw_name; /* keyword */ 18912099Sjoerg int kw_token; /* token returned by yylex() */ 19012099Sjoerg scl_t kw_scl; /* storage class if kw_token T_SCLASS */ 19112099Sjoerg tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */ 19212099Sjoerg tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */ 19312099Sjoerg u_int kw_stdc : 1; /* STDC keyword */ 19412099Sjoerg u_int kw_gcc : 1; /* GCC keyword */ 19512099Sjoerg} kwtab[] = { 19612099Sjoerg { "asm", T_ASM, 0, 0, 0, 0, 1 }, 19712099Sjoerg { "__asm", T_ASM, 0, 0, 0, 0, 0 }, 19812099Sjoerg { "__asm__", T_ASM, 0, 0, 0, 0, 0 }, 19912099Sjoerg { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 }, 20012099Sjoerg { "break", T_BREAK, 0, 0, 0, 0, 0 }, 20112099Sjoerg { "case", T_CASE, 0, 0, 0, 0, 0 }, 20212099Sjoerg { "char", T_TYPE, 0, CHAR, 0, 0, 0 }, 20312099Sjoerg { "const", T_QUAL, 0, 0, CONST, 1, 0 }, 20412099Sjoerg { "__const__", T_QUAL, 0, 0, CONST, 0, 0 }, 20512099Sjoerg { "__const", T_QUAL, 0, 0, CONST, 0, 0 }, 20612099Sjoerg { "continue", T_CONTINUE, 0, 0, 0, 0, 0 }, 20712099Sjoerg { "default", T_DEFAULT, 0, 0, 0, 0, 0 }, 20812099Sjoerg { "do", T_DO, 0, 0, 0, 0, 0 }, 20912099Sjoerg { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 }, 21012099Sjoerg { "else", T_ELSE, 0, 0, 0, 0, 0 }, 21112099Sjoerg { "enum", T_ENUM, 0, 0, 0, 0, 0 }, 21212099Sjoerg { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 }, 21312099Sjoerg { "float", T_TYPE, 0, FLOAT, 0, 0, 0 }, 21412099Sjoerg { "for", T_FOR, 0, 0, 0, 0, 0 }, 21512099Sjoerg { "goto", T_GOTO, 0, 0, 0, 0, 0 }, 21612099Sjoerg { "if", T_IF, 0, 0, 0, 0, 0 }, 21712099Sjoerg { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 }, 21812099Sjoerg { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 }, 21912099Sjoerg { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 }, 22012099Sjoerg { "int", T_TYPE, 0, INT, 0, 0, 0 }, 22191592Smarkm { "__symbolrename", T_SYMBOLRENAME, 0, 0, 0, 0, 0 }, 22212099Sjoerg { "long", T_TYPE, 0, LONG, 0, 0, 0 }, 22312099Sjoerg { "register", T_SCLASS, REG, 0, 0, 0, 0 }, 22412099Sjoerg { "return", T_RETURN, 0, 0, 0, 0, 0 }, 22512099Sjoerg { "short", T_TYPE, 0, SHORT, 0, 0, 0 }, 22612099Sjoerg { "signed", T_TYPE, 0, SIGNED, 0, 1, 0 }, 22712099Sjoerg { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0 }, 22812099Sjoerg { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0 }, 22912099Sjoerg { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0 }, 23012099Sjoerg { "static", T_SCLASS, STATIC, 0, 0, 0, 0 }, 23112099Sjoerg { "struct", T_SOU, 0, STRUCT, 0, 0, 0 }, 23212099Sjoerg { "switch", T_SWITCH, 0, 0, 0, 0, 0 }, 23312099Sjoerg { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0 }, 23412099Sjoerg { "union", T_SOU, 0, UNION, 0, 0, 0 }, 23512099Sjoerg { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0 }, 23612099Sjoerg { "void", T_TYPE, 0, VOID, 0, 0, 0 }, 23712099Sjoerg { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0 }, 23812099Sjoerg { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 23912099Sjoerg { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 24012099Sjoerg { "while", T_WHILE, 0, 0, 0, 0, 0 }, 24112099Sjoerg { NULL, 0, 0, 0, 0, 0, 0 } 24212099Sjoerg}; 24312099Sjoerg 24412099Sjoerg/* Symbol table */ 24512099Sjoergstatic sym_t *symtab[HSHSIZ1]; 24612099Sjoerg 24712099Sjoerg/* bit i of the entry with index i is set */ 24891592Smarkmuint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT]; 24912099Sjoerg 25012099Sjoerg/* least significant i bits are set in the entry with index i */ 25191592Smarkmuint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1]; 25212099Sjoerg 25312099Sjoerg/* least significant i bits are not set in the entry with index i */ 25491592Smarkmuint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1]; 25512099Sjoerg 25612099Sjoerg/* free list for sbuf structures */ 25712099Sjoergstatic sbuf_t *sbfrlst; 25812099Sjoerg 25912099Sjoerg/* Typ of next expected symbol */ 26012099Sjoergsymt_t symtyp; 26112099Sjoerg 26212099Sjoerg 26312099Sjoerg/* 26412099Sjoerg * All keywords are written to the symbol table. This saves us looking 265108532Sschweikh * in an extra table for each name we found. 26612099Sjoerg */ 26712099Sjoergvoid 26891592Smarkminitscan(void) 26912099Sjoerg{ 27012099Sjoerg struct kwtab *kw; 27112099Sjoerg sym_t *sym; 27212099Sjoerg int h, i; 27391592Smarkm uint64_t uq; 27412099Sjoerg 27512099Sjoerg for (kw = kwtab; kw->kw_name != NULL; kw++) { 27612099Sjoerg if (kw->kw_stdc && tflag) 27712099Sjoerg continue; 27812099Sjoerg if (kw->kw_gcc && !gflag) 27912099Sjoerg continue; 28012099Sjoerg sym = getblk(sizeof (sym_t)); 28112099Sjoerg sym->s_name = kw->kw_name; 28212099Sjoerg sym->s_keyw = 1; 28312099Sjoerg sym->s_value.v_quad = kw->kw_token; 28412099Sjoerg if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) { 28512099Sjoerg sym->s_tspec = kw->kw_tspec; 28612099Sjoerg } else if (kw->kw_token == T_SCLASS) { 28712099Sjoerg sym->s_scl = kw->kw_scl; 28812099Sjoerg } else if (kw->kw_token == T_QUAL) { 28912099Sjoerg sym->s_tqual = kw->kw_tqual; 29012099Sjoerg } 29112099Sjoerg h = hash(sym->s_name); 29212099Sjoerg if ((sym->s_link = symtab[h]) != NULL) 29312099Sjoerg symtab[h]->s_rlink = &sym->s_link; 29412099Sjoerg (symtab[h] = sym)->s_rlink = &symtab[h]; 29512099Sjoerg } 29612099Sjoerg 29712099Sjoerg /* initialize bit-masks for quads */ 29891592Smarkm for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) { 29991592Smarkm qbmasks[i] = (uint64_t)1 << i; 30091592Smarkm uq = ~(uint64_t)0 << i; 30112099Sjoerg qumasks[i] = uq; 30212099Sjoerg qlmasks[i] = ~uq; 30312099Sjoerg } 30412099Sjoerg qumasks[i] = 0; 30591592Smarkm qlmasks[i] = ~(uint64_t)0; 30612099Sjoerg} 30712099Sjoerg 30812099Sjoerg/* 30912099Sjoerg * Get a free sbuf structure, if possible from the free list 31012099Sjoerg */ 31112099Sjoergstatic sbuf_t * 31291592Smarkmallocsb(void) 31312099Sjoerg{ 31412099Sjoerg sbuf_t *sb; 31512099Sjoerg 31612099Sjoerg if ((sb = sbfrlst) != NULL) { 31712099Sjoerg sbfrlst = sb->sb_nxt; 31812099Sjoerg } else { 31980284Sobrien if ((sb = malloc(sizeof (sbuf_t))) == NULL) 32080284Sobrien nomem(); 32112099Sjoerg } 32212099Sjoerg (void)memset(sb, 0, sizeof (sb)); 32312099Sjoerg return (sb); 32412099Sjoerg} 32512099Sjoerg 32612099Sjoerg/* 32712099Sjoerg * Put a sbuf structure to the free list 32812099Sjoerg */ 32912099Sjoergstatic void 33091592Smarkmfreesb(sbuf_t *sb) 33112099Sjoerg{ 33291592Smarkm 33312099Sjoerg sb->sb_nxt = sbfrlst; 33412099Sjoerg sbfrlst = sb; 33512099Sjoerg} 33612099Sjoerg 33712099Sjoerg/* 33812099Sjoerg * Read a character and ensure that it is positive (except EOF). 33912099Sjoerg * Increment line count(s) if necessary. 34012099Sjoerg */ 34112099Sjoergstatic int 34291592Smarkminpc(void) 34312099Sjoerg{ 34412099Sjoerg int c; 34512099Sjoerg 34612099Sjoerg if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n') 34712099Sjoerg incline(); 34812099Sjoerg return (c); 34912099Sjoerg} 35012099Sjoerg 35112099Sjoergstatic int 35291592Smarkmhash(const char *s) 35312099Sjoerg{ 35412099Sjoerg u_int v; 35512099Sjoerg const u_char *us; 35612099Sjoerg 35712099Sjoerg v = 0; 35812099Sjoerg for (us = (const u_char *)s; *us != '\0'; us++) { 35912099Sjoerg v = (v << sizeof (v)) + *us; 36012099Sjoerg v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 36112099Sjoerg } 36212099Sjoerg return (v % HSHSIZ1); 36312099Sjoerg} 36412099Sjoerg 36512099Sjoerg/* 36612099Sjoerg * Lex has found a letter followed by zero or more letters or digits. 36712099Sjoerg * It looks for a symbol in the symbol table with the same name. This 36812099Sjoerg * symbol must either be a keyword or a symbol of the type required by 36912099Sjoerg * symtyp (label, member, tag, ...). 37012099Sjoerg * 37112099Sjoerg * If it is a keyword, the token is returned. In some cases it is described 37212099Sjoerg * more deeply by data written to yylval. 37312099Sjoerg * 37412099Sjoerg * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct 37512099Sjoerg * is stored in yylval. This struct contains the name of the symbol, it's 37612099Sjoerg * length and hash value. If there is already a symbol of the same name 37712099Sjoerg * and type in the symbol table, the sbuf struct also contains a pointer 37812099Sjoerg * to the symbol table entry. 37912099Sjoerg */ 38012099Sjoergstatic int 38191592Smarkmname(void) 38212099Sjoerg{ 38312099Sjoerg char *s; 38412099Sjoerg sbuf_t *sb; 38512099Sjoerg sym_t *sym; 38612099Sjoerg int tok; 38712099Sjoerg 38812099Sjoerg sb = allocsb(); 38912099Sjoerg sb->sb_name = yytext; 39012099Sjoerg sb->sb_len = yyleng; 39112099Sjoerg sb->sb_hash = hash(yytext); 39212099Sjoerg 39312099Sjoerg if ((sym = search(sb)) != NULL && sym->s_keyw) { 39412099Sjoerg freesb(sb); 39512099Sjoerg return (keyw(sym)); 39612099Sjoerg } 39712099Sjoerg 39812099Sjoerg sb->sb_sym = sym; 39912099Sjoerg 40012099Sjoerg if (sym != NULL) { 40112099Sjoerg if (blklev < sym->s_blklev) 40212099Sjoerg lerror("name() 1"); 40312099Sjoerg sb->sb_name = sym->s_name; 40412099Sjoerg sb->sb_len = strlen(sym->s_name); 40512099Sjoerg tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME; 40612099Sjoerg } else { 40712099Sjoerg s = getblk(yyleng + 1); 40812099Sjoerg (void)memcpy(s, yytext, yyleng + 1); 40912099Sjoerg sb->sb_name = s; 41012099Sjoerg sb->sb_len = yyleng; 41112099Sjoerg tok = T_NAME; 41212099Sjoerg } 41312099Sjoerg 41412099Sjoerg yylval.y_sb = sb; 41512099Sjoerg return (tok); 41612099Sjoerg} 41712099Sjoerg 41812099Sjoergstatic sym_t * 41991592Smarkmsearch(sbuf_t *sb) 42012099Sjoerg{ 42112099Sjoerg sym_t *sym; 42212099Sjoerg 42312099Sjoerg for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) { 42412099Sjoerg if (strcmp(sym->s_name, sb->sb_name) == 0) { 42512099Sjoerg if (sym->s_keyw || sym->s_kind == symtyp) 42612099Sjoerg return (sym); 42712099Sjoerg } 42812099Sjoerg } 42912099Sjoerg 43012099Sjoerg return (NULL); 43112099Sjoerg} 43291592Smarkm 43312099Sjoergstatic int 43491592Smarkmkeyw(sym_t *sym) 43512099Sjoerg{ 43612099Sjoerg int t; 43712099Sjoerg 43812099Sjoerg if ((t = (int)sym->s_value.v_quad) == T_SCLASS) { 43912099Sjoerg yylval.y_scl = sym->s_scl; 44012099Sjoerg } else if (t == T_TYPE || t == T_SOU) { 44112099Sjoerg yylval.y_tspec = sym->s_tspec; 44212099Sjoerg } else if (t == T_QUAL) { 44312099Sjoerg yylval.y_tqual = sym->s_tqual; 44412099Sjoerg } 44512099Sjoerg return (t); 44612099Sjoerg} 44712099Sjoerg 44812099Sjoerg/* 44912099Sjoerg * Convert a string representing an integer into internal representation. 45012099Sjoerg * The value is returned in yylval. icon() (and yylex()) returns T_CON. 45112099Sjoerg */ 45212099Sjoergstatic int 45391592Smarkmicon(int base) 45412099Sjoerg{ 45512099Sjoerg int l_suffix, u_suffix; 45612099Sjoerg int len; 45712099Sjoerg const char *cp; 45812099Sjoerg char c, *eptr; 45912099Sjoerg tspec_t typ; 46091592Smarkm u_long ul = 0; 46191592Smarkm uint64_t uq = 0; 46212099Sjoerg int ansiu; 46312099Sjoerg static tspec_t contypes[2][3] = { 46412099Sjoerg { INT, LONG, QUAD }, 46512099Sjoerg { UINT, ULONG, UQUAD } 46612099Sjoerg }; 46712099Sjoerg 46812099Sjoerg cp = yytext; 46912099Sjoerg len = yyleng; 47012099Sjoerg 47112099Sjoerg /* skip 0x */ 47212099Sjoerg if (base == 16) { 47312099Sjoerg cp += 2; 47412099Sjoerg len -= 2; 47512099Sjoerg } 47612099Sjoerg 47712099Sjoerg /* read suffixes */ 47812099Sjoerg l_suffix = u_suffix = 0; 47912099Sjoerg for ( ; ; ) { 48012099Sjoerg if ((c = cp[len - 1]) == 'l' || c == 'L') { 48112099Sjoerg l_suffix++; 48212099Sjoerg } else if (c == 'u' || c == 'U') { 48312099Sjoerg u_suffix++; 48412099Sjoerg } else { 48512099Sjoerg break; 48612099Sjoerg } 48712099Sjoerg len--; 48812099Sjoerg } 48912099Sjoerg if (l_suffix > 2 || u_suffix > 1) { 49012099Sjoerg /* malformed integer constant */ 49112099Sjoerg warning(251); 49212099Sjoerg if (l_suffix > 2) 49312099Sjoerg l_suffix = 2; 49412099Sjoerg if (u_suffix > 1) 49512099Sjoerg u_suffix = 1; 49612099Sjoerg } 49712099Sjoerg if (tflag && u_suffix != 0) { 49812099Sjoerg /* suffix U is illegal in traditional C */ 49912099Sjoerg warning(97); 50012099Sjoerg } 50112099Sjoerg typ = contypes[u_suffix][l_suffix]; 50212099Sjoerg 50312099Sjoerg errno = 0; 50412099Sjoerg if (l_suffix < 2) { 50512099Sjoerg ul = strtoul(cp, &eptr, base); 50612099Sjoerg } else { 50712099Sjoerg uq = strtouq(cp, &eptr, base); 50812099Sjoerg } 50912099Sjoerg if (eptr != cp + len) 51012099Sjoerg lerror("icon() 1"); 51112099Sjoerg if (errno != 0) 51212099Sjoerg /* integer constant out of range */ 51312099Sjoerg warning(252); 51412099Sjoerg 51512099Sjoerg /* 51691592Smarkm * If the value is to big for the current type, we must choose 51712099Sjoerg * another type. 51812099Sjoerg */ 51912099Sjoerg ansiu = 0; 52012099Sjoerg switch (typ) { 52112099Sjoerg case INT: 52212099Sjoerg if (ul <= INT_MAX) { 52312099Sjoerg /* ok */ 52412099Sjoerg } else if (ul <= (unsigned)UINT_MAX && base != 10) { 52512099Sjoerg typ = UINT; 52612099Sjoerg } else if (ul <= LONG_MAX) { 52712099Sjoerg typ = LONG; 52812099Sjoerg } else { 52912099Sjoerg typ = ULONG; 53012099Sjoerg } 53112099Sjoerg if (typ == UINT || typ == ULONG) { 53212099Sjoerg if (tflag) { 53312099Sjoerg typ = LONG; 53412099Sjoerg } else if (!sflag) { 53512099Sjoerg /* 53612099Sjoerg * Remember that the constant is unsigned 53712099Sjoerg * only in ANSI C 53812099Sjoerg */ 53912099Sjoerg ansiu = 1; 54012099Sjoerg } 54112099Sjoerg } 54212099Sjoerg break; 54312099Sjoerg case UINT: 54412099Sjoerg if (ul > (u_int)UINT_MAX) 54512099Sjoerg typ = ULONG; 54612099Sjoerg break; 54712099Sjoerg case LONG: 54812099Sjoerg if (ul > LONG_MAX && !tflag) { 54912099Sjoerg typ = ULONG; 55012099Sjoerg if (!sflag) 55112099Sjoerg ansiu = 1; 55212099Sjoerg } 55312099Sjoerg break; 55412099Sjoerg case QUAD: 55512099Sjoerg if (uq > QUAD_MAX && !tflag) { 55612099Sjoerg typ = UQUAD; 55712099Sjoerg if (!sflag) 55812099Sjoerg ansiu = 1; 55912099Sjoerg } 56012099Sjoerg break; 56112099Sjoerg /* LINTED (enumeration values not handled in switch) */ 56291592Smarkm case STRUCT: 56391592Smarkm case VOID: 56491592Smarkm case LDOUBLE: 56591592Smarkm case FUNC: 56691592Smarkm case ARRAY: 56791592Smarkm case PTR: 56891592Smarkm case ENUM: 56991592Smarkm case UNION: 57091592Smarkm case SIGNED: 57191592Smarkm case NOTSPEC: 57291592Smarkm case DOUBLE: 57391592Smarkm case FLOAT: 57491592Smarkm case UQUAD: 57591592Smarkm case ULONG: 57691592Smarkm case USHORT: 57791592Smarkm case SHORT: 57891592Smarkm case UCHAR: 57991592Smarkm case SCHAR: 58091592Smarkm case CHAR: 58191592Smarkm case UNSIGN: 58291592Smarkm break; 58391592Smarkm 58491592Smarkm case NTSPEC: /* this value unused */ 58591592Smarkm break; 58612099Sjoerg } 58712099Sjoerg 58812099Sjoerg if (typ != QUAD && typ != UQUAD) { 58912099Sjoerg if (isutyp(typ)) { 59012099Sjoerg uq = ul; 59112099Sjoerg } else { 59291592Smarkm uq = (int64_t)(long)ul; 59312099Sjoerg } 59412099Sjoerg } 59512099Sjoerg 59691592Smarkm uq = (uint64_t)xsign((int64_t)uq, typ, -1); 59712099Sjoerg 59880284Sobrien if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL) 59980284Sobrien nomem(); 60080284Sobrien yylval.y_val->v_tspec = typ; 60112099Sjoerg yylval.y_val->v_ansiu = ansiu; 60291592Smarkm yylval.y_val->v_quad = (int64_t)uq; 60312099Sjoerg 60412099Sjoerg return (T_CON); 60512099Sjoerg} 60612099Sjoerg 60712099Sjoerg/* 60812099Sjoerg * Returns 1 if t is a signed type and the value is negative. 60912099Sjoerg * 61012099Sjoerg * len is the number of significant bits. If len is -1, len is set 61112099Sjoerg * to the width of type t. 61212099Sjoerg */ 61312099Sjoergint 61491592Smarkmsign(int64_t q, tspec_t t, int len) 61512099Sjoerg{ 61691592Smarkm 61712099Sjoerg if (t == PTR || isutyp(t)) 61812099Sjoerg return (0); 61912099Sjoerg return (msb(q, t, len)); 62012099Sjoerg} 62112099Sjoerg 62212099Sjoergint 62391592Smarkmmsb(int64_t q, tspec_t t, int len) 62412099Sjoerg{ 62591592Smarkm 62612099Sjoerg if (len <= 0) 62712099Sjoerg len = size(t); 62812099Sjoerg return ((q & qbmasks[len - 1]) != 0); 62912099Sjoerg} 63012099Sjoerg 63112099Sjoerg/* 63212099Sjoerg * Extends the sign of q. 63312099Sjoerg */ 63491592Smarkmint64_t 63591592Smarkmxsign(int64_t q, tspec_t t, int len) 63612099Sjoerg{ 63791592Smarkm 63812099Sjoerg if (len <= 0) 63912099Sjoerg len = size(t); 64012099Sjoerg 64112099Sjoerg if (t == PTR || isutyp(t) || !sign(q, t, len)) { 64212099Sjoerg q &= qlmasks[len]; 64312099Sjoerg } else { 64412099Sjoerg q |= qumasks[len]; 64512099Sjoerg } 64612099Sjoerg return (q); 64712099Sjoerg} 64812099Sjoerg 64912099Sjoerg/* 65012099Sjoerg * Convert a string representing a floating point value into its interal 65112099Sjoerg * representation. Type and value are returned in yylval. fcon() 65212099Sjoerg * (and yylex()) returns T_CON. 65312099Sjoerg * XXX Currently it is not possible to convert constants of type 65491592Smarkm * long double which are greater than DBL_MAX. 65512099Sjoerg */ 65612099Sjoergstatic int 65791592Smarkmfcon(void) 65812099Sjoerg{ 65912099Sjoerg const char *cp; 66012099Sjoerg int len; 66112099Sjoerg tspec_t typ; 66212099Sjoerg char c, *eptr; 66312099Sjoerg double d; 66491592Smarkm float f = 0; 66512099Sjoerg 66612099Sjoerg cp = yytext; 66712099Sjoerg len = yyleng; 66812099Sjoerg 66912099Sjoerg if ((c = cp[len - 1]) == 'f' || c == 'F') { 67012099Sjoerg typ = FLOAT; 67112099Sjoerg len--; 67212099Sjoerg } else if (c == 'l' || c == 'L') { 67312099Sjoerg typ = LDOUBLE; 67412099Sjoerg len--; 67512099Sjoerg } else { 67612099Sjoerg typ = DOUBLE; 67712099Sjoerg } 67812099Sjoerg 67912099Sjoerg if (tflag && typ != DOUBLE) { 68012099Sjoerg /* suffixes F and L are illegal in traditional C */ 68112099Sjoerg warning(98); 68212099Sjoerg } 68312099Sjoerg 68412099Sjoerg errno = 0; 68512099Sjoerg d = strtod(cp, &eptr); 68612099Sjoerg if (eptr != cp + len) 68712099Sjoerg lerror("fcon() 1"); 68812099Sjoerg if (errno != 0) 68912099Sjoerg /* floating-point constant out of range */ 69012099Sjoerg warning(248); 69112099Sjoerg 69212099Sjoerg if (typ == FLOAT) { 69312099Sjoerg f = (float)d; 69491592Smarkm if (!finite(f)) { 69512099Sjoerg /* floating-point constant out of range */ 69612099Sjoerg warning(248); 69712099Sjoerg f = f > 0 ? FLT_MAX : -FLT_MAX; 69812099Sjoerg } 69912099Sjoerg } 70012099Sjoerg 70180284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 70280284Sobrien nomem(); 70380284Sobrien yylval.y_val->v_tspec = typ; 70412099Sjoerg if (typ == FLOAT) { 70512099Sjoerg yylval.y_val->v_ldbl = f; 70612099Sjoerg } else { 70712099Sjoerg yylval.y_val->v_ldbl = d; 70812099Sjoerg } 70912099Sjoerg 71012099Sjoerg return (T_CON); 71112099Sjoerg} 71212099Sjoerg 71312099Sjoergstatic int 71491592Smarkmoperator(int t, op_t o) 71512099Sjoerg{ 71691592Smarkm 71712099Sjoerg yylval.y_op = o; 71812099Sjoerg return (t); 71912099Sjoerg} 72012099Sjoerg 72112099Sjoerg/* 72212099Sjoerg * Called if lex found a leading \'. 72312099Sjoerg */ 72412099Sjoergstatic int 72591592Smarkmccon(void) 72612099Sjoerg{ 72712099Sjoerg int n, val, c; 72812099Sjoerg char cv; 72912099Sjoerg 73012099Sjoerg n = 0; 73112099Sjoerg val = 0; 73212099Sjoerg while ((c = getescc('\'')) >= 0) { 73312099Sjoerg val = (val << CHAR_BIT) + c; 73412099Sjoerg n++; 73512099Sjoerg } 73612099Sjoerg if (c == -2) { 73712099Sjoerg /* unterminated character constant */ 73812099Sjoerg error(253); 73912099Sjoerg } else { 74012099Sjoerg if (n > sizeof (int) || (n > 1 && (pflag || hflag))) { 74112099Sjoerg /* too many characters in character constant */ 74212099Sjoerg error(71); 74312099Sjoerg } else if (n > 1) { 74412099Sjoerg /* multi-character character constant */ 74512099Sjoerg warning(294); 74612099Sjoerg } else if (n == 0) { 74712099Sjoerg /* empty character constant */ 74812099Sjoerg error(73); 74912099Sjoerg } 75012099Sjoerg } 75112099Sjoerg if (n == 1) { 75212099Sjoerg cv = (char)val; 75312099Sjoerg val = cv; 75412099Sjoerg } 75591592Smarkm 75691592Smarkm yylval.y_val = xcalloc(1, sizeof (val_t)); 75712099Sjoerg yylval.y_val->v_tspec = INT; 75812099Sjoerg yylval.y_val->v_quad = val; 75912099Sjoerg 76012099Sjoerg return (T_CON); 76112099Sjoerg} 76212099Sjoerg 76312099Sjoerg/* 76412099Sjoerg * Called if lex found a leading L\' 76512099Sjoerg */ 76612099Sjoergstatic int 76791592Smarkmwccon(void) 76812099Sjoerg{ 76912099Sjoerg static char buf[MB_LEN_MAX + 1]; 77012099Sjoerg int i, c; 77112099Sjoerg wchar_t wc; 77212099Sjoerg 77312099Sjoerg i = 0; 77412099Sjoerg while ((c = getescc('\'')) >= 0) { 77512099Sjoerg if (i < MB_CUR_MAX) 77612099Sjoerg buf[i] = (char)c; 77712099Sjoerg i++; 77812099Sjoerg } 77912099Sjoerg 78012099Sjoerg wc = 0; 78112099Sjoerg 78212099Sjoerg if (c == -2) { 78312099Sjoerg /* unterminated character constant */ 78412099Sjoerg error(253); 78512099Sjoerg } else if (c == 0) { 78612099Sjoerg /* empty character constant */ 78712099Sjoerg error(73); 78812099Sjoerg } else { 78912099Sjoerg if (i > MB_CUR_MAX) { 79012099Sjoerg i = MB_CUR_MAX; 79112099Sjoerg /* too many characters in character constant */ 79212099Sjoerg error(71); 79312099Sjoerg } else { 79412099Sjoerg buf[i] = '\0'; 79512099Sjoerg (void)mbtowc(NULL, NULL, 0); 79612099Sjoerg if (mbtowc(&wc, buf, MB_CUR_MAX) < 0) 79712099Sjoerg /* invalid multibyte character */ 79812099Sjoerg error(291); 79912099Sjoerg } 80012099Sjoerg } 80112099Sjoerg 80280284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 80380284Sobrien nomem(); 80412099Sjoerg yylval.y_val->v_tspec = WCHAR; 80512099Sjoerg yylval.y_val->v_quad = wc; 80612099Sjoerg 80712099Sjoerg return (T_CON); 80812099Sjoerg} 80912099Sjoerg 81012099Sjoerg/* 81112099Sjoerg * Read a character which is part of a character constant or of a string 81212099Sjoerg * and handle escapes. 81312099Sjoerg * 81412099Sjoerg * The Argument is the character which delimits the character constant or 81512099Sjoerg * string. 81612099Sjoerg * 81712099Sjoerg * Returns -1 if the end of the character constant or string is reached, 81891592Smarkm * -2 if the EOF is reached, and the character otherwise. 81912099Sjoerg */ 82012099Sjoergstatic int 82191592Smarkmgetescc(int d) 82212099Sjoerg{ 82312099Sjoerg static int pbc = -1; 82412099Sjoerg int n, c, v; 82512099Sjoerg 82612099Sjoerg if (pbc == -1) { 82712099Sjoerg c = inpc(); 82812099Sjoerg } else { 82912099Sjoerg c = pbc; 83012099Sjoerg pbc = -1; 83112099Sjoerg } 83212099Sjoerg if (c == d) 83312099Sjoerg return (-1); 83412099Sjoerg switch (c) { 83512099Sjoerg case '\n': 83691592Smarkm if (tflag) { 83791592Smarkm /* newline in string or char constant */ 83891592Smarkm error(254); 83991592Smarkm return (-2); 84091592Smarkm } 84191592Smarkm return (c); 84212099Sjoerg case EOF: 84312099Sjoerg return (-2); 84412099Sjoerg case '\\': 84512099Sjoerg switch (c = inpc()) { 84612099Sjoerg case '"': 84712099Sjoerg if (tflag && d == '\'') 84812099Sjoerg /* \" inside character constant undef. ... */ 84912099Sjoerg warning(262); 85012099Sjoerg return ('"'); 85112099Sjoerg case '\'': 85212099Sjoerg return ('\''); 85312099Sjoerg case '?': 85412099Sjoerg if (tflag) 85512099Sjoerg /* \? undefined in traditional C */ 85612099Sjoerg warning(263); 85712099Sjoerg return ('?'); 85812099Sjoerg case '\\': 85912099Sjoerg return ('\\'); 86012099Sjoerg case 'a': 86112099Sjoerg if (tflag) 86212099Sjoerg /* \a undefined in traditional C */ 86312099Sjoerg warning(81); 86412099Sjoerg return ('\a'); 86512099Sjoerg case 'b': 86612099Sjoerg return ('\b'); 86712099Sjoerg case 'f': 86812099Sjoerg return ('\f'); 86912099Sjoerg case 'n': 87012099Sjoerg return ('\n'); 87112099Sjoerg case 'r': 87212099Sjoerg return ('\r'); 87312099Sjoerg case 't': 87412099Sjoerg return ('\t'); 87512099Sjoerg case 'v': 87612099Sjoerg if (tflag) 87712099Sjoerg /* \v undefined in traditional C */ 87812099Sjoerg warning(264); 87912099Sjoerg return ('\v'); 88012099Sjoerg case '8': case '9': 88112099Sjoerg /* bad octal digit %c */ 88212099Sjoerg warning(77, c); 88312099Sjoerg /* FALLTHROUGH */ 88412099Sjoerg case '0': case '1': case '2': case '3': 88512099Sjoerg case '4': case '5': case '6': case '7': 88612099Sjoerg n = 3; 88712099Sjoerg v = 0; 88812099Sjoerg do { 88912099Sjoerg v = (v << 3) + (c - '0'); 89012099Sjoerg c = inpc(); 89112099Sjoerg } while (--n && isdigit(c) && (tflag || c <= '7')); 89212099Sjoerg if (tflag && n > 0 && isdigit(c)) 89312099Sjoerg /* bad octal digit %c */ 89412099Sjoerg warning(77, c); 89512099Sjoerg pbc = c; 89612099Sjoerg if (v > UCHAR_MAX) { 89712099Sjoerg /* character escape does not fit in char. */ 89812099Sjoerg warning(76); 89912099Sjoerg v &= CHAR_MASK; 90012099Sjoerg } 90112099Sjoerg return (v); 90212099Sjoerg case 'x': 90312099Sjoerg if (tflag) 90412099Sjoerg /* \x undefined in traditional C */ 90512099Sjoerg warning(82); 90612099Sjoerg v = 0; 90712099Sjoerg n = 0; 90812099Sjoerg while ((c = inpc()) >= 0 && isxdigit(c)) { 90912099Sjoerg c = isdigit(c) ? 91012099Sjoerg c - '0' : toupper(c) - 'A' + 10; 91112099Sjoerg v = (v << 4) + c; 91212099Sjoerg if (n >= 0) { 91312099Sjoerg if ((v & ~CHAR_MASK) != 0) { 91412099Sjoerg /* overflow in hex escape */ 91512099Sjoerg warning(75); 91612099Sjoerg n = -1; 91712099Sjoerg } else { 91812099Sjoerg n++; 91912099Sjoerg } 92012099Sjoerg } 92112099Sjoerg } 92212099Sjoerg pbc = c; 92312099Sjoerg if (n == 0) { 92412099Sjoerg /* no hex digits follow \x */ 92512099Sjoerg error(74); 92612099Sjoerg } if (n == -1) { 92712099Sjoerg v &= CHAR_MASK; 92812099Sjoerg } 92912099Sjoerg return (v); 93012099Sjoerg case '\n': 93112099Sjoerg return (getescc(d)); 93212099Sjoerg case EOF: 93312099Sjoerg return (-2); 93412099Sjoerg default: 93512099Sjoerg if (isprint(c)) { 93612099Sjoerg /* dubious escape \%c */ 93712099Sjoerg warning(79, c); 93812099Sjoerg } else { 93912099Sjoerg /* dubious escape \%o */ 94012099Sjoerg warning(80, c); 94112099Sjoerg } 94212099Sjoerg } 94312099Sjoerg } 94412099Sjoerg return (c); 94512099Sjoerg} 94612099Sjoerg 94712099Sjoerg/* 94812099Sjoerg * Called for preprocessor directives. Currently implemented are: 94912099Sjoerg * # lineno 95012099Sjoerg * # lineno "filename" 95112099Sjoerg */ 95212099Sjoergstatic void 95391592Smarkmdirective(void) 95412099Sjoerg{ 95512099Sjoerg const char *cp, *fn; 95612099Sjoerg char c, *eptr; 95712099Sjoerg size_t fnl; 95812099Sjoerg long ln; 95912099Sjoerg static int first = 1; 96012099Sjoerg 96112099Sjoerg /* Go to first non-whitespace after # */ 96291592Smarkm for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) 96391592Smarkm continue; 96412099Sjoerg 96591592Smarkm if (!isdigit((unsigned char)c)) { 96612099Sjoerg error: 96712099Sjoerg /* undefined or invalid # directive */ 96812099Sjoerg warning(255); 96912099Sjoerg return; 97012099Sjoerg } 97112099Sjoerg ln = strtol(--cp, &eptr, 10); 97212099Sjoerg if (cp == eptr) 97312099Sjoerg goto error; 97412099Sjoerg if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0') 97512099Sjoerg goto error; 97691592Smarkm while ((c = *cp++) == ' ' || c == '\t') 97791592Smarkm continue; 97812099Sjoerg if (c != '\0') { 97912099Sjoerg if (c != '"') 98012099Sjoerg goto error; 98112099Sjoerg fn = cp; 98212099Sjoerg while ((c = *cp) != '"' && c != '\0') 98312099Sjoerg cp++; 98412099Sjoerg if (c != '"') 98512099Sjoerg goto error; 98612099Sjoerg if ((fnl = cp++ - fn) > PATH_MAX) 98712099Sjoerg goto error; 98891592Smarkm while ((c = *cp++) == ' ' || c == '\t') 98991592Smarkm continue; 99012099Sjoerg#if 0 99112099Sjoerg if (c != '\0') 99212099Sjoerg warning("extra character(s) after directive"); 99312099Sjoerg#endif 99491592Smarkm 99591592Smarkm /* empty string means stdin */ 99691592Smarkm if (fnl == 0) { 99791592Smarkm fn = "{standard input}"; 99891592Smarkm fnl = 16; /* strlen (fn) */ 99991592Smarkm } 100012099Sjoerg curr_pos.p_file = fnnalloc(fn, fnl); 100112099Sjoerg /* 100212099Sjoerg * If this is the first directive, the name is the name 100312099Sjoerg * of the C source file as specified at the command line. 100412099Sjoerg * It is written to the output file. 100512099Sjoerg */ 100612099Sjoerg if (first) { 100712099Sjoerg csrc_pos.p_file = curr_pos.p_file; 100812099Sjoerg outsrc(curr_pos.p_file); 100912099Sjoerg first = 0; 101012099Sjoerg } 101112099Sjoerg } 101212099Sjoerg curr_pos.p_line = (int)ln - 1; 101391592Smarkm curr_pos.p_uniq = 0; 101491592Smarkm if (curr_pos.p_file == csrc_pos.p_file) { 101512099Sjoerg csrc_pos.p_line = (int)ln - 1; 101691592Smarkm csrc_pos.p_uniq = 0; 101791592Smarkm } 101812099Sjoerg} 101912099Sjoerg 102012099Sjoerg/* 102112099Sjoerg * Handle lint comments. Following comments are currently understood: 102212099Sjoerg * ARGSUSEDn 102391592Smarkm * BITFIELDTYPE 102412099Sjoerg * CONSTCOND CONSTANTCOND CONSTANTCONDITION 102512099Sjoerg * FALLTHRU FALLTHROUGH 102612099Sjoerg * LINTLIBRARY 102712099Sjoerg * LINTED NOSTRICT 102812099Sjoerg * LONGLONG 102912099Sjoerg * NOTREACHED 103012099Sjoerg * PRINTFLIKEn 103112099Sjoerg * PROTOLIB 103212099Sjoerg * SCANFLIKEn 103312099Sjoerg * VARARGSn 103412099Sjoerg * If one of this comments is recognized, the arguments, if any, are 103512099Sjoerg * parsed and a function which handles this comment is called. 103612099Sjoerg */ 103712099Sjoergstatic void 103891592Smarkmcomment(void) 103912099Sjoerg{ 104012099Sjoerg int c, lc; 104112099Sjoerg static struct { 104212099Sjoerg const char *keywd; 104312099Sjoerg int arg; 104491592Smarkm void (*func)(int); 104512099Sjoerg } keywtab[] = { 104612099Sjoerg { "ARGSUSED", 1, argsused }, 104791592Smarkm { "BITFIELDTYPE", 0, bitfieldtype }, 104812099Sjoerg { "CONSTCOND", 0, constcond }, 104912099Sjoerg { "CONSTANTCOND", 0, constcond }, 105012099Sjoerg { "CONSTANTCONDITION", 0, constcond }, 105112099Sjoerg { "FALLTHRU", 0, fallthru }, 105212099Sjoerg { "FALLTHROUGH", 0, fallthru }, 105312099Sjoerg { "LINTLIBRARY", 0, lintlib }, 105412099Sjoerg { "LINTED", 0, linted }, 105512099Sjoerg { "LONGLONG", 0, longlong }, 105612099Sjoerg { "NOSTRICT", 0, linted }, 105712099Sjoerg { "NOTREACHED", 0, notreach }, 105812099Sjoerg { "PRINTFLIKE", 1, printflike }, 105912099Sjoerg { "PROTOLIB", 1, protolib }, 106012099Sjoerg { "SCANFLIKE", 1, scanflike }, 106112099Sjoerg { "VARARGS", 1, varargs }, 106212099Sjoerg }; 106312099Sjoerg char keywd[32]; 106412099Sjoerg char arg[32]; 106512099Sjoerg int l, i, a; 106612099Sjoerg int eoc; 106712099Sjoerg 106812099Sjoerg eoc = 0; 106912099Sjoerg 107012099Sjoerg /* Skip white spaces after the start of the comment */ 107191592Smarkm while ((c = inpc()) != EOF && isspace(c)) 107291592Smarkm continue; 107312099Sjoerg 107412099Sjoerg /* Read the potential keyword to keywd */ 107512099Sjoerg l = 0; 107612099Sjoerg while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) { 107712099Sjoerg keywd[l++] = (char)c; 107812099Sjoerg c = inpc(); 107912099Sjoerg } 108012099Sjoerg keywd[l] = '\0'; 108112099Sjoerg 108212099Sjoerg /* look for the keyword */ 108312099Sjoerg for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) { 108412099Sjoerg if (strcmp(keywtab[i].keywd, keywd) == 0) 108512099Sjoerg break; 108612099Sjoerg } 108712099Sjoerg if (i == sizeof (keywtab) / sizeof (keywtab[0])) 108812099Sjoerg goto skip_rest; 108912099Sjoerg 109012099Sjoerg /* skip white spaces after the keyword */ 109112099Sjoerg while (c != EOF && isspace(c)) 109212099Sjoerg c = inpc(); 109312099Sjoerg 109412099Sjoerg /* read the argument, if the keyword accepts one and there is one */ 109512099Sjoerg l = 0; 109612099Sjoerg if (keywtab[i].arg) { 109712099Sjoerg while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) { 109812099Sjoerg arg[l++] = (char)c; 109912099Sjoerg c = inpc(); 110012099Sjoerg } 110112099Sjoerg } 110212099Sjoerg arg[l] = '\0'; 110312099Sjoerg a = l != 0 ? atoi(arg) : -1; 110412099Sjoerg 110512099Sjoerg /* skip white spaces after the argument */ 110612099Sjoerg while (c != EOF && isspace(c)) 110712099Sjoerg c = inpc(); 110812099Sjoerg 110912099Sjoerg if (c != '*' || (c = inpc()) != '/') { 111012099Sjoerg if (keywtab[i].func != linted) 111112099Sjoerg /* extra characters in lint comment */ 111212099Sjoerg warning(257); 111312099Sjoerg } else { 111412099Sjoerg /* 111512099Sjoerg * remember that we have already found the end of the 111612099Sjoerg * comment 111712099Sjoerg */ 111812099Sjoerg eoc = 1; 111912099Sjoerg } 112012099Sjoerg 112112099Sjoerg if (keywtab[i].func != NULL) 112212099Sjoerg (*keywtab[i].func)(a); 112312099Sjoerg 112412099Sjoerg skip_rest: 112512099Sjoerg while (!eoc) { 112612099Sjoerg lc = c; 112712099Sjoerg if ((c = inpc()) == EOF) { 112812099Sjoerg /* unterminated comment */ 112912099Sjoerg error(256); 113012099Sjoerg break; 113112099Sjoerg } 113212099Sjoerg if (lc == '*' && c == '/') 113312099Sjoerg eoc = 1; 113412099Sjoerg } 113512099Sjoerg} 113612099Sjoerg 113712099Sjoerg/* 113891592Smarkm * Handle // style comments 113991592Smarkm */ 114091592Smarkmstatic void 114191592Smarkmslashslashcomment(void) 114291592Smarkm{ 114391592Smarkm int c; 114491592Smarkm 114591592Smarkm if (sflag < 2 && !gflag) 114691592Smarkm /* // comments only supported in C99 */ 114791592Smarkm (void)gnuism(312, tflag ? "traditional" : "ANSI"); 114891592Smarkm 114991592Smarkm while ((c = inpc()) != EOF && c != '\n') 115091592Smarkm continue; 115191592Smarkm} 115291592Smarkm 115391592Smarkm/* 115412099Sjoerg * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND. 115512099Sjoerg * clrwflgs() is called after function definitions and global and 115612099Sjoerg * local declarations and definitions. It is also called between 115712099Sjoerg * the controlling expression and the body of control statements 115812099Sjoerg * (if, switch, for, while). 115912099Sjoerg */ 116012099Sjoergvoid 116191592Smarkmclrwflgs(void) 116212099Sjoerg{ 116391592Smarkm 116412099Sjoerg nowarn = 0; 116512099Sjoerg quadflg = 0; 116612099Sjoerg ccflg = 0; 116712099Sjoerg} 116812099Sjoerg 116912099Sjoerg/* 117012099Sjoerg * Strings are stored in a dynamically alloceted buffer and passed 117112099Sjoerg * in yylval.y_xstrg to the parser. The parser or the routines called 117212099Sjoerg * by the parser are responsible for freeing this buffer. 117312099Sjoerg */ 117412099Sjoergstatic int 117591592Smarkmstring(void) 117612099Sjoerg{ 117712099Sjoerg u_char *s; 117812099Sjoerg int c; 117912099Sjoerg size_t len, max; 118012099Sjoerg strg_t *strg; 118112099Sjoerg 118280284Sobrien if ((s = malloc(max = 64)) == NULL) 118380284Sobrien nomem(); 118412099Sjoerg 118512099Sjoerg len = 0; 118612099Sjoerg while ((c = getescc('"')) >= 0) { 118712099Sjoerg /* +1 to reserve space for a trailing NUL character */ 118812099Sjoerg if (len + 1 == max) 118980284Sobrien if ((s = realloc(s, max *= 2)) == NULL) 119080284Sobrien nomem(); 119112099Sjoerg s[len++] = (char)c; 119212099Sjoerg } 119312099Sjoerg s[len] = '\0'; 119412099Sjoerg if (c == -2) 119512099Sjoerg /* unterminated string constant */ 119612099Sjoerg error(258); 119712099Sjoerg 119880284Sobrien if ((strg = calloc(1, sizeof (strg_t))) == NULL) 119980284Sobrien nomem(); 120012099Sjoerg strg->st_tspec = CHAR; 120112099Sjoerg strg->st_len = len; 120212099Sjoerg strg->st_cp = s; 120312099Sjoerg 120412099Sjoerg yylval.y_strg = strg; 120512099Sjoerg return (T_STRING); 120612099Sjoerg} 120712099Sjoerg 120812099Sjoergstatic int 120991592Smarkmwcstrg(void) 121012099Sjoerg{ 121112099Sjoerg char *s; 121212099Sjoerg int c, i, n, wi; 121312099Sjoerg size_t len, max, wlen; 121412099Sjoerg wchar_t *ws; 121512099Sjoerg strg_t *strg; 121612099Sjoerg 121780284Sobrien if ((s = malloc(max = 64)) == NULL) 121880284Sobrien nomem(); 121912099Sjoerg len = 0; 122012099Sjoerg while ((c = getescc('"')) >= 0) { 122112099Sjoerg /* +1 to save space for a trailing NUL character */ 122212099Sjoerg if (len + 1 >= max) 122380284Sobrien if ((s = realloc(s, max *= 2)) == NULL) 122480284Sobrien nomem(); 122512099Sjoerg s[len++] = (char)c; 122612099Sjoerg } 122712099Sjoerg s[len] = '\0'; 122812099Sjoerg if (c == -2) 122912099Sjoerg /* unterminated string constant */ 123012099Sjoerg error(258); 123112099Sjoerg 123212099Sjoerg /* get length of wide character string */ 123312099Sjoerg (void)mblen(NULL, 0); 123412099Sjoerg for (i = 0, wlen = 0; i < len; i += n, wlen++) { 123512099Sjoerg if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) { 123612099Sjoerg /* invalid multibyte character */ 123712099Sjoerg error(291); 123812099Sjoerg break; 123912099Sjoerg } 124012099Sjoerg if (n == 0) 124112099Sjoerg n = 1; 124212099Sjoerg } 124312099Sjoerg 124480284Sobrien if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL) 124580284Sobrien nomem(); 124612099Sjoerg 124712099Sjoerg /* convert from multibyte to wide char */ 124812099Sjoerg (void)mbtowc(NULL, NULL, 0); 124912099Sjoerg for (i = 0, wi = 0; i < len; i += n, wi++) { 125012099Sjoerg if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1) 125112099Sjoerg break; 125212099Sjoerg if (n == 0) 125312099Sjoerg n = 1; 125412099Sjoerg } 125512099Sjoerg ws[wi] = 0; 125612099Sjoerg free(s); 125712099Sjoerg 125880284Sobrien if ((strg = calloc(1, sizeof (strg_t))) == NULL) 125980284Sobrien nomem(); 126012099Sjoerg strg->st_tspec = WCHAR; 126112099Sjoerg strg->st_len = wlen; 126212099Sjoerg strg->st_wcp = ws; 126312099Sjoerg 126412099Sjoerg yylval.y_strg = strg; 126512099Sjoerg return (T_STRING); 126612099Sjoerg} 126712099Sjoerg 126812099Sjoerg/* 126912099Sjoerg * As noted above the scanner does not create new symbol table entries 127012099Sjoerg * for symbols it cannot find in the symbol table. This is to avoid 127112099Sjoerg * putting undeclared symbols into the symbol table if a syntax error 127212099Sjoerg * occurs. 127312099Sjoerg * 127412099Sjoerg * getsym() is called as soon as it is probably ok to put the symbol to 127512099Sjoerg * the symbol table. This does not mean that it is not possible that 127612099Sjoerg * symbols are put to the symbol table which are than not completely 127712099Sjoerg * declared due to syntax errors. To avoid too many problems in this 127812099Sjoerg * case symbols get type int in getsym(). 127912099Sjoerg * 128012099Sjoerg * XXX calls to getsym() should be delayed until decl1*() is called 128112099Sjoerg */ 128212099Sjoergsym_t * 128391592Smarkmgetsym(sbuf_t *sb) 128412099Sjoerg{ 128512099Sjoerg dinfo_t *di; 128612099Sjoerg char *s; 128712099Sjoerg sym_t *sym; 128812099Sjoerg 128912099Sjoerg sym = sb->sb_sym; 129012099Sjoerg 129112099Sjoerg /* 129212099Sjoerg * During member declaration it is possible that name() looked 129312099Sjoerg * for symbols of type FVFT, although it should have looked for 129412099Sjoerg * symbols of type FTAG. Same can happen for labels. Both cases 129512099Sjoerg * are compensated here. 129612099Sjoerg */ 129712099Sjoerg if (symtyp == FMOS || symtyp == FLAB) { 129812099Sjoerg if (sym == NULL || sym->s_kind == FVFT) 129912099Sjoerg sym = search(sb); 130012099Sjoerg } 130112099Sjoerg 130212099Sjoerg if (sym != NULL) { 130312099Sjoerg if (sym->s_kind != symtyp) 130412099Sjoerg lerror("storesym() 1"); 130512099Sjoerg symtyp = FVFT; 130612099Sjoerg freesb(sb); 130712099Sjoerg return (sym); 130812099Sjoerg } 130912099Sjoerg 131012099Sjoerg /* create a new symbol table entry */ 131112099Sjoerg 131212099Sjoerg /* labels must always be allocated at level 1 (outhermost block) */ 131312099Sjoerg if (symtyp == FLAB) { 131412099Sjoerg sym = getlblk(1, sizeof (sym_t)); 131512099Sjoerg s = getlblk(1, sb->sb_len + 1); 131612099Sjoerg (void)memcpy(s, sb->sb_name, sb->sb_len + 1); 131712099Sjoerg sym->s_name = s; 131812099Sjoerg sym->s_blklev = 1; 131912099Sjoerg di = dcs; 132012099Sjoerg while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL) 132112099Sjoerg di = di->d_nxt; 132212099Sjoerg if (di->d_ctx != AUTO) 132312099Sjoerg lerror("storesym() 2"); 132412099Sjoerg } else { 132512099Sjoerg sym = getblk(sizeof (sym_t)); 132612099Sjoerg sym->s_name = sb->sb_name; 132712099Sjoerg sym->s_blklev = blklev; 132812099Sjoerg di = dcs; 132912099Sjoerg } 133012099Sjoerg 133191592Smarkm UNIQUE_CURR_POS(sym->s_dpos); 133212099Sjoerg if ((sym->s_kind = symtyp) != FLAB) 133312099Sjoerg sym->s_type = gettyp(INT); 133412099Sjoerg 133512099Sjoerg symtyp = FVFT; 133612099Sjoerg 133712099Sjoerg if ((sym->s_link = symtab[sb->sb_hash]) != NULL) 133812099Sjoerg symtab[sb->sb_hash]->s_rlink = &sym->s_link; 133912099Sjoerg (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash]; 134012099Sjoerg 134112099Sjoerg *di->d_ldlsym = sym; 134212099Sjoerg di->d_ldlsym = &sym->s_dlnxt; 134312099Sjoerg 134412099Sjoerg freesb(sb); 134512099Sjoerg return (sym); 134612099Sjoerg} 134712099Sjoerg 134812099Sjoerg/* 134912099Sjoerg * Remove a symbol forever from the symbol table. s_blklev 135012099Sjoerg * is set to -1 to avoid that the symbol will later be put 135112099Sjoerg * back to the symbol table. 135212099Sjoerg */ 135312099Sjoergvoid 135491592Smarkmrmsym(sym_t *sym) 135512099Sjoerg{ 135691592Smarkm 135712099Sjoerg if ((*sym->s_rlink = sym->s_link) != NULL) 135812099Sjoerg sym->s_link->s_rlink = sym->s_rlink; 135912099Sjoerg sym->s_blklev = -1; 136012099Sjoerg sym->s_link = NULL; 136112099Sjoerg} 136212099Sjoerg 136312099Sjoerg/* 136412099Sjoerg * Remove a list of symbols declared at one level from the symbol 136512099Sjoerg * table. 136612099Sjoerg */ 136712099Sjoergvoid 136891592Smarkmrmsyms(sym_t *syms) 136912099Sjoerg{ 137012099Sjoerg sym_t *sym; 137112099Sjoerg 137212099Sjoerg for (sym = syms; sym != NULL; sym = sym->s_dlnxt) { 137312099Sjoerg if (sym->s_blklev != -1) { 137412099Sjoerg if ((*sym->s_rlink = sym->s_link) != NULL) 137512099Sjoerg sym->s_link->s_rlink = sym->s_rlink; 137612099Sjoerg sym->s_link = NULL; 137712099Sjoerg sym->s_rlink = NULL; 137812099Sjoerg } 137912099Sjoerg } 138012099Sjoerg} 138112099Sjoerg 138212099Sjoerg/* 138312099Sjoerg * Put a symbol into the symbol table 138412099Sjoerg */ 138512099Sjoergvoid 138691592Smarkminssym(int bl, sym_t *sym) 138712099Sjoerg{ 138812099Sjoerg int h; 138912099Sjoerg 139012099Sjoerg h = hash(sym->s_name); 139112099Sjoerg if ((sym->s_link = symtab[h]) != NULL) 139212099Sjoerg symtab[h]->s_rlink = &sym->s_link; 139312099Sjoerg (symtab[h] = sym)->s_rlink = &symtab[h]; 139412099Sjoerg sym->s_blklev = bl; 139512099Sjoerg if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev) 139612099Sjoerg lerror("inssym()"); 139712099Sjoerg} 139812099Sjoerg 139912099Sjoerg/* 140012099Sjoerg * Called at level 0 after syntax errors 140112099Sjoerg * Removes all symbols which are not declared at level 0 from the 140212099Sjoerg * symbol table. Also frees all memory which is not associated with 140312099Sjoerg * level 0. 140412099Sjoerg */ 140512099Sjoergvoid 140691592Smarkmcleanup(void) 140712099Sjoerg{ 140812099Sjoerg sym_t *sym, *nsym; 140912099Sjoerg int i; 141012099Sjoerg 141112099Sjoerg for (i = 0; i < HSHSIZ1; i++) { 141212099Sjoerg for (sym = symtab[i]; sym != NULL; sym = nsym) { 141312099Sjoerg nsym = sym->s_link; 141412099Sjoerg if (sym->s_blklev >= 1) { 141512099Sjoerg if ((*sym->s_rlink = nsym) != NULL) 141612099Sjoerg nsym->s_rlink = sym->s_rlink; 141712099Sjoerg } 141812099Sjoerg } 141912099Sjoerg } 142012099Sjoerg 142112099Sjoerg for (i = mblklev; i > 0; i--) 142212099Sjoerg freelblk(i); 142312099Sjoerg} 142412099Sjoerg 142512099Sjoerg/* 142612099Sjoerg * Create a new symbol with the name of an existing symbol. 142712099Sjoerg */ 142812099Sjoergsym_t * 142991592Smarkmpushdown(sym_t *sym) 143012099Sjoerg{ 143112099Sjoerg int h; 143212099Sjoerg sym_t *nsym; 143312099Sjoerg 143412099Sjoerg h = hash(sym->s_name); 143512099Sjoerg nsym = getblk(sizeof (sym_t)); 143612099Sjoerg if (sym->s_blklev > blklev) 143712099Sjoerg lerror("pushdown()"); 143812099Sjoerg nsym->s_name = sym->s_name; 143991592Smarkm UNIQUE_CURR_POS(nsym->s_dpos); 144012099Sjoerg nsym->s_kind = sym->s_kind; 144112099Sjoerg nsym->s_blklev = blklev; 144212099Sjoerg 144312099Sjoerg if ((nsym->s_link = symtab[h]) != NULL) 144412099Sjoerg symtab[h]->s_rlink = &nsym->s_link; 144512099Sjoerg (symtab[h] = nsym)->s_rlink = &symtab[h]; 144612099Sjoerg 144712099Sjoerg *dcs->d_ldlsym = nsym; 144812099Sjoerg dcs->d_ldlsym = &nsym->s_dlnxt; 144912099Sjoerg 145012099Sjoerg return (nsym); 145112099Sjoerg} 145212099Sjoerg 145312099Sjoerg/* 145412099Sjoerg * Free any dynamically allocated memory referenced by 145512099Sjoerg * the value stack or yylval. 145612099Sjoerg * The type of information in yylval is described by tok. 145712099Sjoerg */ 145812099Sjoergvoid 145991592Smarkmfreeyyv(void *sp, int tok) 146012099Sjoerg{ 146112099Sjoerg if (tok == T_NAME || tok == T_TYPENAME) { 146212099Sjoerg sbuf_t *sb = *(sbuf_t **)sp; 146312099Sjoerg freesb(sb); 146412099Sjoerg } else if (tok == T_CON) { 146512099Sjoerg val_t *val = *(val_t **)sp; 146612099Sjoerg free(val); 146712099Sjoerg } else if (tok == T_STRING) { 146812099Sjoerg strg_t *strg = *(strg_t **)sp; 146912099Sjoerg if (strg->st_tspec == CHAR) { 147012099Sjoerg free(strg->st_cp); 147112099Sjoerg } else if (strg->st_tspec == WCHAR) { 147212099Sjoerg free(strg->st_wcp); 147312099Sjoerg } else { 147412099Sjoerg lerror("fryylv() 1"); 147512099Sjoerg } 147612099Sjoerg free(strg); 147791592Smarkm } 147812099Sjoerg} 1479