scan.l revision 80284
112099Sjoerg%{ 275831Sasmodai/* $NetBSD: scan.l,v 1.8 1995/10/23 13:38:51 jpo Exp $ */ 312099Sjoerg 412099Sjoerg/* 512099Sjoerg * Copyright (c) 1994, 1995 Jochen Pohl 612099Sjoerg * All Rights Reserved. 712099Sjoerg * 812099Sjoerg * Redistribution and use in source and binary forms, with or without 912099Sjoerg * modification, are permitted provided that the following conditions 1012099Sjoerg * are met: 1112099Sjoerg * 1. Redistributions of source code must retain the above copyright 1212099Sjoerg * notice, this list of conditions and the following disclaimer. 1312099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright 1412099Sjoerg * notice, this list of conditions and the following disclaimer in the 1512099Sjoerg * documentation and/or other materials provided with the distribution. 1612099Sjoerg * 3. All advertising materials mentioning features or use of this software 1712099Sjoerg * must display the following acknowledgement: 1812099Sjoerg * This product includes software developed by Jochen Pohl for 1912099Sjoerg * The NetBSD Project. 2012099Sjoerg * 4. The name of the author may not be used to endorse or promote products 2112099Sjoerg * derived from this software without specific prior written permission. 2212099Sjoerg * 2312099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2412099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2512099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2612099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2712099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2812099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2912099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3012099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3112099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3212099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3312099Sjoerg */ 3412099Sjoerg 3512099Sjoerg#ifndef lint 3675730Sasmodaistatic char rcsid[] = "$FreeBSD: head/usr.bin/xlint/lint1/scan.l 80284 2001-07-24 14:02:07Z obrien $"; 3712099Sjoerg#endif 3812099Sjoerg 3912099Sjoerg#include <stdlib.h> 4012099Sjoerg#include <string.h> 4112099Sjoerg#include <limits.h> 4212099Sjoerg#include <float.h> 4312099Sjoerg#include <ctype.h> 4412099Sjoerg#include <errno.h> 4512099Sjoerg#include <err.h> 4617142Sjkh#include <math.h> 4712099Sjoerg 4812099Sjoerg#include "lint1.h" 4916073Sphk#include "y.tab.h" 5012099Sjoerg 5112099Sjoerg#define CHAR_MASK (~(~0 << CHAR_BIT)) 5212099Sjoerg 5312099Sjoerg/* Current position (its also updated when an included file is parsed) */ 5412099Sjoergpos_t curr_pos = { 1, "" }; 5512099Sjoerg 5612099Sjoerg/* 5712099Sjoerg * Current position in C source (not updated when an included file is 5812099Sjoerg * parsed). 5912099Sjoerg */ 6012099Sjoergpos_t csrc_pos = { 1, "" }; 6112099Sjoerg 6212099Sjoergstatic void incline __P((void)); 6312099Sjoergstatic void badchar __P((int)); 6412099Sjoergstatic sbuf_t *allocsb __P((void)); 6512099Sjoergstatic void freesb __P((sbuf_t *)); 6612099Sjoergstatic int inpc __P((void)); 6712099Sjoergstatic int hash __P((const char *)); 6812099Sjoergstatic sym_t *search __P((sbuf_t *)); 6912099Sjoergstatic int name __P((void)); 7012099Sjoergstatic int keyw __P((sym_t *)); 7112099Sjoergstatic int icon __P((int)); 7212099Sjoergstatic int fcon __P((void)); 7312099Sjoergstatic int operator __P((int, op_t)); 7412099Sjoergstatic int ccon __P((void)); 7512099Sjoergstatic int wccon __P((void)); 7612099Sjoergstatic int getescc __P((int)); 7712099Sjoergstatic void directive __P((void)); 7812099Sjoergstatic void comment __P((void)); 7912099Sjoergstatic int string __P((void)); 8012099Sjoergstatic int wcstrg __P((void)); 8112099Sjoerg 8212099Sjoerg%} 8312099Sjoerg 8412099SjoergL [_A-Za-z] 8512099SjoergD [0-9] 8612099SjoergNZD [1-9] 8712099SjoergOD [0-7] 8812099SjoergHD [0-9A-Fa-f] 8912099SjoergEX ([eE][+-]?[0-9]+) 9012099Sjoerg 9112099Sjoerg%% 9212099Sjoerg 9312099Sjoerg{L}({L}|{D})* return (name()); 9412099Sjoerg0{OD}*[lLuU]* return (icon(8)); 9512099Sjoerg{NZD}{D}*[lLuU]* return (icon(10)); 9612099Sjoerg0[xX]{HD}+[lLuU]* return (icon(16)); 9712099Sjoerg{D}+\.{D}*{EX}?[fFlL]? | 9812099Sjoerg{D}+{EX}[fFlL]? | 9912099Sjoerg\.{D}+{EX}?[fFlL]? return (fcon()); 10012099Sjoerg"=" return (operator(T_ASSIGN, ASSIGN)); 10112099Sjoerg"*=" return (operator(T_OPASS, MULASS)); 10212099Sjoerg"/=" return (operator(T_OPASS, DIVASS)); 10312099Sjoerg"%=" return (operator(T_OPASS, MODASS)); 10412099Sjoerg"+=" return (operator(T_OPASS, ADDASS)); 10512099Sjoerg"-=" return (operator(T_OPASS, SUBASS)); 10612099Sjoerg"<<=" return (operator(T_OPASS, SHLASS)); 10712099Sjoerg">>=" return (operator(T_OPASS, SHRASS)); 10812099Sjoerg"&=" return (operator(T_OPASS, ANDASS)); 10912099Sjoerg"^=" return (operator(T_OPASS, XORASS)); 11012099Sjoerg"|=" return (operator(T_OPASS, ORASS)); 11112099Sjoerg"||" return (operator(T_LOGOR, LOGOR)); 11212099Sjoerg"&&" return (operator(T_LOGAND, LOGAND)); 11312099Sjoerg"|" return (operator(T_OR, OR)); 11412099Sjoerg"&" return (operator(T_AND, AND)); 11512099Sjoerg"^" return (operator(T_XOR, XOR)); 11612099Sjoerg"==" return (operator(T_EQOP, EQ)); 11712099Sjoerg"!=" return (operator(T_EQOP, NE)); 11812099Sjoerg"<" return (operator(T_RELOP, LT)); 11912099Sjoerg">" return (operator(T_RELOP, GT)); 12012099Sjoerg"<=" return (operator(T_RELOP, LE)); 12112099Sjoerg">=" return (operator(T_RELOP, GE)); 12212099Sjoerg"<<" return (operator(T_SHFTOP, SHL)); 12312099Sjoerg">>" return (operator(T_SHFTOP, SHR)); 12412099Sjoerg"++" return (operator(T_INCDEC, INC)); 12512099Sjoerg"--" return (operator(T_INCDEC, DEC)); 12612099Sjoerg"->" return (operator(T_STROP, ARROW)); 12712099Sjoerg"." return (operator(T_STROP, POINT)); 12812099Sjoerg"+" return (operator(T_ADDOP, PLUS)); 12912099Sjoerg"-" return (operator(T_ADDOP, MINUS)); 13012099Sjoerg"*" return (operator(T_MULT, MULT)); 13112099Sjoerg"/" return (operator(T_DIVOP, DIV)); 13212099Sjoerg"%" return (operator(T_DIVOP, MOD)); 13312099Sjoerg"!" return (operator(T_UNOP, NOT)); 13412099Sjoerg"~" return (operator(T_UNOP, COMPL)); 13512099Sjoerg"\"" return (string()); 13612099Sjoerg"L\"" return (wcstrg()); 13712099Sjoerg";" return (T_SEMI); 13812099Sjoerg"{" return (T_LBRACE); 13912099Sjoerg"}" return (T_RBRACE); 14012099Sjoerg"," return (T_COMMA); 14112099Sjoerg":" return (T_COLON); 14212099Sjoerg"?" return (T_QUEST); 14312099Sjoerg"[" return (T_LBRACK); 14412099Sjoerg"]" return (T_RBRACK); 14512099Sjoerg"(" return (T_LPARN); 14612099Sjoerg")" return (T_RPARN); 14712099Sjoerg"..." return (T_ELLIPSE); 14812099Sjoerg"'" return (ccon()); 14912099Sjoerg"L'" return (wccon()); 15012099Sjoerg^#.*$ directive(); 15112099Sjoerg\n incline(); 15212099Sjoerg\t|" "|\f|\v ; 15312099Sjoerg"/*" comment(); 15412099Sjoerg. badchar(yytext[0]); 15512099Sjoerg 15612099Sjoerg%% 15712099Sjoerg 15812099Sjoergstatic void 15912099Sjoergincline() 16012099Sjoerg{ 16112099Sjoerg curr_pos.p_line++; 16212099Sjoerg if (curr_pos.p_file == csrc_pos.p_file) 16312099Sjoerg csrc_pos.p_line++; 16412099Sjoerg} 16512099Sjoerg 16612099Sjoergstatic void 16712099Sjoergbadchar(c) 16812099Sjoerg int c; 16912099Sjoerg{ 17012099Sjoerg /* unknown character \%o */ 17112099Sjoerg error(250, c); 17212099Sjoerg} 17312099Sjoerg 17412099Sjoerg/* 17512099Sjoerg * Keywords. 17612099Sjoerg * During initialisation they are written to the symbol table. 17712099Sjoerg */ 17812099Sjoergstatic struct kwtab { 17912099Sjoerg const char *kw_name; /* keyword */ 18012099Sjoerg int kw_token; /* token returned by yylex() */ 18112099Sjoerg scl_t kw_scl; /* storage class if kw_token T_SCLASS */ 18212099Sjoerg tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */ 18312099Sjoerg tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */ 18412099Sjoerg u_int kw_stdc : 1; /* STDC keyword */ 18512099Sjoerg u_int kw_gcc : 1; /* GCC keyword */ 18612099Sjoerg} kwtab[] = { 18712099Sjoerg { "asm", T_ASM, 0, 0, 0, 0, 1 }, 18812099Sjoerg { "__asm", T_ASM, 0, 0, 0, 0, 0 }, 18912099Sjoerg { "__asm__", T_ASM, 0, 0, 0, 0, 0 }, 19012099Sjoerg { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 }, 19112099Sjoerg { "break", T_BREAK, 0, 0, 0, 0, 0 }, 19212099Sjoerg { "case", T_CASE, 0, 0, 0, 0, 0 }, 19312099Sjoerg { "char", T_TYPE, 0, CHAR, 0, 0, 0 }, 19412099Sjoerg { "const", T_QUAL, 0, 0, CONST, 1, 0 }, 19512099Sjoerg { "__const__", T_QUAL, 0, 0, CONST, 0, 0 }, 19612099Sjoerg { "__const", T_QUAL, 0, 0, CONST, 0, 0 }, 19712099Sjoerg { "continue", T_CONTINUE, 0, 0, 0, 0, 0 }, 19812099Sjoerg { "default", T_DEFAULT, 0, 0, 0, 0, 0 }, 19912099Sjoerg { "do", T_DO, 0, 0, 0, 0, 0 }, 20012099Sjoerg { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 }, 20112099Sjoerg { "else", T_ELSE, 0, 0, 0, 0, 0 }, 20212099Sjoerg { "enum", T_ENUM, 0, 0, 0, 0, 0 }, 20312099Sjoerg { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 }, 20412099Sjoerg { "float", T_TYPE, 0, FLOAT, 0, 0, 0 }, 20512099Sjoerg { "for", T_FOR, 0, 0, 0, 0, 0 }, 20612099Sjoerg { "goto", T_GOTO, 0, 0, 0, 0, 0 }, 20712099Sjoerg { "if", T_IF, 0, 0, 0, 0, 0 }, 20812099Sjoerg { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 }, 20912099Sjoerg { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 }, 21012099Sjoerg { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 }, 21112099Sjoerg { "int", T_TYPE, 0, INT, 0, 0, 0 }, 21212099Sjoerg { "long", T_TYPE, 0, LONG, 0, 0, 0 }, 21312099Sjoerg { "register", T_SCLASS, REG, 0, 0, 0, 0 }, 21412099Sjoerg { "return", T_RETURN, 0, 0, 0, 0, 0 }, 21512099Sjoerg { "short", T_TYPE, 0, SHORT, 0, 0, 0 }, 21612099Sjoerg { "signed", T_TYPE, 0, SIGNED, 0, 1, 0 }, 21712099Sjoerg { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0 }, 21812099Sjoerg { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0 }, 21912099Sjoerg { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0 }, 22012099Sjoerg { "static", T_SCLASS, STATIC, 0, 0, 0, 0 }, 22112099Sjoerg { "struct", T_SOU, 0, STRUCT, 0, 0, 0 }, 22212099Sjoerg { "switch", T_SWITCH, 0, 0, 0, 0, 0 }, 22312099Sjoerg { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0 }, 22412099Sjoerg { "union", T_SOU, 0, UNION, 0, 0, 0 }, 22512099Sjoerg { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0 }, 22612099Sjoerg { "void", T_TYPE, 0, VOID, 0, 0, 0 }, 22712099Sjoerg { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0 }, 22812099Sjoerg { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 22912099Sjoerg { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0 }, 23012099Sjoerg { "while", T_WHILE, 0, 0, 0, 0, 0 }, 23112099Sjoerg { NULL, 0, 0, 0, 0, 0, 0 } 23212099Sjoerg}; 23312099Sjoerg 23412099Sjoerg/* Symbol table */ 23512099Sjoergstatic sym_t *symtab[HSHSIZ1]; 23612099Sjoerg 23712099Sjoerg/* bit i of the entry with index i is set */ 23812099Sjoergu_quad_t qbmasks[sizeof(u_quad_t) * CHAR_BIT]; 23912099Sjoerg 24012099Sjoerg/* least significant i bits are set in the entry with index i */ 24112099Sjoergu_quad_t qlmasks[sizeof(u_quad_t) * CHAR_BIT + 1]; 24212099Sjoerg 24312099Sjoerg/* least significant i bits are not set in the entry with index i */ 24412099Sjoergu_quad_t qumasks[sizeof(u_quad_t) * CHAR_BIT + 1]; 24512099Sjoerg 24612099Sjoerg/* free list for sbuf structures */ 24712099Sjoergstatic sbuf_t *sbfrlst; 24812099Sjoerg 24912099Sjoerg/* Typ of next expected symbol */ 25012099Sjoergsymt_t symtyp; 25112099Sjoerg 25212099Sjoerg 25312099Sjoerg/* 25412099Sjoerg * All keywords are written to the symbol table. This saves us looking 25512099Sjoerg * in a extra table for each name we found. 25612099Sjoerg */ 25712099Sjoergvoid 25812099Sjoerginitscan() 25912099Sjoerg{ 26012099Sjoerg struct kwtab *kw; 26112099Sjoerg sym_t *sym; 26212099Sjoerg int h, i; 26312099Sjoerg u_quad_t uq; 26412099Sjoerg 26512099Sjoerg for (kw = kwtab; kw->kw_name != NULL; kw++) { 26612099Sjoerg if (kw->kw_stdc && tflag) 26712099Sjoerg continue; 26812099Sjoerg if (kw->kw_gcc && !gflag) 26912099Sjoerg continue; 27012099Sjoerg sym = getblk(sizeof (sym_t)); 27112099Sjoerg sym->s_name = kw->kw_name; 27212099Sjoerg sym->s_keyw = 1; 27312099Sjoerg sym->s_value.v_quad = kw->kw_token; 27412099Sjoerg if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) { 27512099Sjoerg sym->s_tspec = kw->kw_tspec; 27612099Sjoerg } else if (kw->kw_token == T_SCLASS) { 27712099Sjoerg sym->s_scl = kw->kw_scl; 27812099Sjoerg } else if (kw->kw_token == T_QUAL) { 27912099Sjoerg sym->s_tqual = kw->kw_tqual; 28012099Sjoerg } 28112099Sjoerg h = hash(sym->s_name); 28212099Sjoerg if ((sym->s_link = symtab[h]) != NULL) 28312099Sjoerg symtab[h]->s_rlink = &sym->s_link; 28412099Sjoerg (symtab[h] = sym)->s_rlink = &symtab[h]; 28512099Sjoerg } 28612099Sjoerg 28712099Sjoerg /* initialize bit-masks for quads */ 28812099Sjoerg for (i = 0; i < sizeof (u_quad_t) * CHAR_BIT; i++) { 28912099Sjoerg qbmasks[i] = (u_quad_t)1 << i; 29012099Sjoerg uq = ~(u_quad_t)0 << i; 29112099Sjoerg qumasks[i] = uq; 29212099Sjoerg qlmasks[i] = ~uq; 29312099Sjoerg } 29412099Sjoerg qumasks[i] = 0; 29512099Sjoerg qlmasks[i] = ~(u_quad_t)0; 29612099Sjoerg} 29712099Sjoerg 29812099Sjoerg/* 29912099Sjoerg * Get a free sbuf structure, if possible from the free list 30012099Sjoerg */ 30112099Sjoergstatic sbuf_t * 30212099Sjoergallocsb() 30312099Sjoerg{ 30412099Sjoerg sbuf_t *sb; 30512099Sjoerg 30612099Sjoerg if ((sb = sbfrlst) != NULL) { 30712099Sjoerg sbfrlst = sb->sb_nxt; 30812099Sjoerg } else { 30980284Sobrien if ((sb = malloc(sizeof (sbuf_t))) == NULL) 31080284Sobrien nomem(); 31112099Sjoerg } 31212099Sjoerg (void)memset(sb, 0, sizeof (sb)); 31312099Sjoerg return (sb); 31412099Sjoerg} 31512099Sjoerg 31612099Sjoerg/* 31712099Sjoerg * Put a sbuf structure to the free list 31812099Sjoerg */ 31912099Sjoergstatic void 32012099Sjoergfreesb(sb) 32112099Sjoerg sbuf_t *sb; 32212099Sjoerg{ 32312099Sjoerg sb->sb_nxt = sbfrlst; 32412099Sjoerg sbfrlst = sb; 32512099Sjoerg} 32612099Sjoerg 32712099Sjoerg/* 32812099Sjoerg * Read a character and ensure that it is positive (except EOF). 32912099Sjoerg * Increment line count(s) if necessary. 33012099Sjoerg */ 33112099Sjoergstatic int 33212099Sjoerginpc() 33312099Sjoerg{ 33412099Sjoerg int c; 33512099Sjoerg 33612099Sjoerg if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n') 33712099Sjoerg incline(); 33812099Sjoerg return (c); 33912099Sjoerg} 34012099Sjoerg 34112099Sjoergstatic int 34212099Sjoerghash(s) 34312099Sjoerg const char *s; 34412099Sjoerg{ 34512099Sjoerg u_int v; 34612099Sjoerg const u_char *us; 34712099Sjoerg 34812099Sjoerg v = 0; 34912099Sjoerg for (us = (const u_char *)s; *us != '\0'; us++) { 35012099Sjoerg v = (v << sizeof (v)) + *us; 35112099Sjoerg v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v)); 35212099Sjoerg } 35312099Sjoerg return (v % HSHSIZ1); 35412099Sjoerg} 35512099Sjoerg 35612099Sjoerg/* 35712099Sjoerg * Lex has found a letter followed by zero or more letters or digits. 35812099Sjoerg * It looks for a symbol in the symbol table with the same name. This 35912099Sjoerg * symbol must either be a keyword or a symbol of the type required by 36012099Sjoerg * symtyp (label, member, tag, ...). 36112099Sjoerg * 36212099Sjoerg * If it is a keyword, the token is returned. In some cases it is described 36312099Sjoerg * more deeply by data written to yylval. 36412099Sjoerg * 36512099Sjoerg * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct 36612099Sjoerg * is stored in yylval. This struct contains the name of the symbol, it's 36712099Sjoerg * length and hash value. If there is already a symbol of the same name 36812099Sjoerg * and type in the symbol table, the sbuf struct also contains a pointer 36912099Sjoerg * to the symbol table entry. 37012099Sjoerg */ 37112099Sjoergstatic int 37212099Sjoergname() 37312099Sjoerg{ 37412099Sjoerg char *s; 37512099Sjoerg sbuf_t *sb; 37612099Sjoerg sym_t *sym; 37712099Sjoerg int tok; 37812099Sjoerg 37912099Sjoerg sb = allocsb(); 38012099Sjoerg sb->sb_name = yytext; 38112099Sjoerg sb->sb_len = yyleng; 38212099Sjoerg sb->sb_hash = hash(yytext); 38312099Sjoerg 38412099Sjoerg if ((sym = search(sb)) != NULL && sym->s_keyw) { 38512099Sjoerg freesb(sb); 38612099Sjoerg return (keyw(sym)); 38712099Sjoerg } 38812099Sjoerg 38912099Sjoerg sb->sb_sym = sym; 39012099Sjoerg 39112099Sjoerg if (sym != NULL) { 39212099Sjoerg if (blklev < sym->s_blklev) 39312099Sjoerg lerror("name() 1"); 39412099Sjoerg sb->sb_name = sym->s_name; 39512099Sjoerg sb->sb_len = strlen(sym->s_name); 39612099Sjoerg tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME; 39712099Sjoerg } else { 39812099Sjoerg s = getblk(yyleng + 1); 39912099Sjoerg (void)memcpy(s, yytext, yyleng + 1); 40012099Sjoerg sb->sb_name = s; 40112099Sjoerg sb->sb_len = yyleng; 40212099Sjoerg tok = T_NAME; 40312099Sjoerg } 40412099Sjoerg 40512099Sjoerg yylval.y_sb = sb; 40612099Sjoerg return (tok); 40712099Sjoerg} 40812099Sjoerg 40912099Sjoergstatic sym_t * 41012099Sjoergsearch(sb) 41112099Sjoerg sbuf_t *sb; 41212099Sjoerg{ 41312099Sjoerg sym_t *sym; 41412099Sjoerg 41512099Sjoerg for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) { 41612099Sjoerg if (strcmp(sym->s_name, sb->sb_name) == 0) { 41712099Sjoerg if (sym->s_keyw || sym->s_kind == symtyp) 41812099Sjoerg return (sym); 41912099Sjoerg } 42012099Sjoerg } 42112099Sjoerg 42212099Sjoerg return (NULL); 42312099Sjoerg} 42412099Sjoerg 42512099Sjoergstatic int 42612099Sjoergkeyw(sym) 42712099Sjoerg sym_t *sym; 42812099Sjoerg{ 42912099Sjoerg int t; 43012099Sjoerg 43112099Sjoerg if ((t = (int)sym->s_value.v_quad) == T_SCLASS) { 43212099Sjoerg yylval.y_scl = sym->s_scl; 43312099Sjoerg } else if (t == T_TYPE || t == T_SOU) { 43412099Sjoerg yylval.y_tspec = sym->s_tspec; 43512099Sjoerg } else if (t == T_QUAL) { 43612099Sjoerg yylval.y_tqual = sym->s_tqual; 43712099Sjoerg } 43812099Sjoerg return (t); 43912099Sjoerg} 44012099Sjoerg 44112099Sjoerg/* 44212099Sjoerg * Convert a string representing an integer into internal representation. 44312099Sjoerg * The value is returned in yylval. icon() (and yylex()) returns T_CON. 44412099Sjoerg */ 44512099Sjoergstatic int 44612099Sjoergicon(base) 44712099Sjoerg int base; 44812099Sjoerg{ 44912099Sjoerg int l_suffix, u_suffix; 45012099Sjoerg int len; 45112099Sjoerg const char *cp; 45212099Sjoerg char c, *eptr; 45312099Sjoerg tspec_t typ; 45412099Sjoerg u_long ul; 45512099Sjoerg u_quad_t uq; 45612099Sjoerg int ansiu; 45712099Sjoerg static tspec_t contypes[2][3] = { 45812099Sjoerg { INT, LONG, QUAD }, 45912099Sjoerg { UINT, ULONG, UQUAD } 46012099Sjoerg }; 46112099Sjoerg 46212099Sjoerg cp = yytext; 46312099Sjoerg len = yyleng; 46412099Sjoerg 46512099Sjoerg /* skip 0x */ 46612099Sjoerg if (base == 16) { 46712099Sjoerg cp += 2; 46812099Sjoerg len -= 2; 46912099Sjoerg } 47012099Sjoerg 47112099Sjoerg /* read suffixes */ 47212099Sjoerg l_suffix = u_suffix = 0; 47312099Sjoerg for ( ; ; ) { 47412099Sjoerg if ((c = cp[len - 1]) == 'l' || c == 'L') { 47512099Sjoerg l_suffix++; 47612099Sjoerg } else if (c == 'u' || c == 'U') { 47712099Sjoerg u_suffix++; 47812099Sjoerg } else { 47912099Sjoerg break; 48012099Sjoerg } 48112099Sjoerg len--; 48212099Sjoerg } 48312099Sjoerg if (l_suffix > 2 || u_suffix > 1) { 48412099Sjoerg /* malformed integer constant */ 48512099Sjoerg warning(251); 48612099Sjoerg if (l_suffix > 2) 48712099Sjoerg l_suffix = 2; 48812099Sjoerg if (u_suffix > 1) 48912099Sjoerg u_suffix = 1; 49012099Sjoerg } 49112099Sjoerg if (tflag && u_suffix != 0) { 49212099Sjoerg /* suffix U is illegal in traditional C */ 49312099Sjoerg warning(97); 49412099Sjoerg } 49512099Sjoerg typ = contypes[u_suffix][l_suffix]; 49612099Sjoerg 49712099Sjoerg errno = 0; 49812099Sjoerg if (l_suffix < 2) { 49912099Sjoerg ul = strtoul(cp, &eptr, base); 50012099Sjoerg } else { 50112099Sjoerg uq = strtouq(cp, &eptr, base); 50212099Sjoerg } 50312099Sjoerg if (eptr != cp + len) 50412099Sjoerg lerror("icon() 1"); 50512099Sjoerg if (errno != 0) 50612099Sjoerg /* integer constant out of range */ 50712099Sjoerg warning(252); 50812099Sjoerg 50912099Sjoerg /* 51012099Sjoerg * If the value is to big for the current type, we must choose 51112099Sjoerg * another type. 51212099Sjoerg */ 51312099Sjoerg ansiu = 0; 51412099Sjoerg switch (typ) { 51512099Sjoerg case INT: 51612099Sjoerg if (ul <= INT_MAX) { 51712099Sjoerg /* ok */ 51812099Sjoerg } else if (ul <= (unsigned)UINT_MAX && base != 10) { 51912099Sjoerg typ = UINT; 52012099Sjoerg } else if (ul <= LONG_MAX) { 52112099Sjoerg typ = LONG; 52212099Sjoerg } else { 52312099Sjoerg typ = ULONG; 52412099Sjoerg } 52512099Sjoerg if (typ == UINT || typ == ULONG) { 52612099Sjoerg if (tflag) { 52712099Sjoerg typ = LONG; 52812099Sjoerg } else if (!sflag) { 52912099Sjoerg /* 53012099Sjoerg * Remember that the constant is unsigned 53112099Sjoerg * only in ANSI C 53212099Sjoerg */ 53312099Sjoerg ansiu = 1; 53412099Sjoerg } 53512099Sjoerg } 53612099Sjoerg break; 53712099Sjoerg case UINT: 53812099Sjoerg if (ul > (u_int)UINT_MAX) 53912099Sjoerg typ = ULONG; 54012099Sjoerg break; 54112099Sjoerg case LONG: 54212099Sjoerg if (ul > LONG_MAX && !tflag) { 54312099Sjoerg typ = ULONG; 54412099Sjoerg if (!sflag) 54512099Sjoerg ansiu = 1; 54612099Sjoerg } 54712099Sjoerg break; 54812099Sjoerg case QUAD: 54912099Sjoerg if (uq > QUAD_MAX && !tflag) { 55012099Sjoerg typ = UQUAD; 55112099Sjoerg if (!sflag) 55212099Sjoerg ansiu = 1; 55312099Sjoerg } 55412099Sjoerg break; 55512099Sjoerg /* LINTED (enumeration values not handled in switch) */ 55617142Sjkh default: 55712099Sjoerg } 55812099Sjoerg 55912099Sjoerg if (typ != QUAD && typ != UQUAD) { 56012099Sjoerg if (isutyp(typ)) { 56112099Sjoerg uq = ul; 56212099Sjoerg } else { 56312099Sjoerg uq = (quad_t)(long)ul; 56412099Sjoerg } 56512099Sjoerg } 56612099Sjoerg 56712099Sjoerg uq = (u_quad_t)xsign((quad_t)uq, typ, -1); 56812099Sjoerg 56980284Sobrien if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL) 57080284Sobrien nomem(); 57180284Sobrien yylval.y_val->v_tspec = typ; 57212099Sjoerg yylval.y_val->v_ansiu = ansiu; 57312099Sjoerg yylval.y_val->v_quad = (quad_t)uq; 57412099Sjoerg 57512099Sjoerg return (T_CON); 57612099Sjoerg} 57712099Sjoerg 57812099Sjoerg/* 57912099Sjoerg * Returns 1 if t is a signed type and the value is negative. 58012099Sjoerg * 58112099Sjoerg * len is the number of significant bits. If len is -1, len is set 58212099Sjoerg * to the width of type t. 58312099Sjoerg */ 58412099Sjoergint 58512099Sjoergsign(q, t, len) 58612099Sjoerg quad_t q; 58712099Sjoerg tspec_t t; 58812099Sjoerg int len; 58912099Sjoerg{ 59012099Sjoerg if (t == PTR || isutyp(t)) 59112099Sjoerg return (0); 59212099Sjoerg return (msb(q, t, len)); 59312099Sjoerg} 59412099Sjoerg 59512099Sjoergint 59612099Sjoergmsb(q, t, len) 59712099Sjoerg quad_t q; 59812099Sjoerg tspec_t t; 59912099Sjoerg int len; 60012099Sjoerg{ 60112099Sjoerg if (len <= 0) 60212099Sjoerg len = size(t); 60312099Sjoerg return ((q & qbmasks[len - 1]) != 0); 60412099Sjoerg} 60512099Sjoerg 60612099Sjoerg/* 60712099Sjoerg * Extends the sign of q. 60812099Sjoerg */ 60912099Sjoergquad_t 61012099Sjoergxsign(q, t, len) 61112099Sjoerg quad_t q; 61212099Sjoerg tspec_t t; 61312099Sjoerg int len; 61412099Sjoerg{ 61512099Sjoerg if (len <= 0) 61612099Sjoerg len = size(t); 61712099Sjoerg 61812099Sjoerg if (t == PTR || isutyp(t) || !sign(q, t, len)) { 61912099Sjoerg q &= qlmasks[len]; 62012099Sjoerg } else { 62112099Sjoerg q |= qumasks[len]; 62212099Sjoerg } 62312099Sjoerg return (q); 62412099Sjoerg} 62512099Sjoerg 62612099Sjoerg/* 62712099Sjoerg * Convert a string representing a floating point value into its interal 62812099Sjoerg * representation. Type and value are returned in yylval. fcon() 62912099Sjoerg * (and yylex()) returns T_CON. 63012099Sjoerg * XXX Currently it is not possible to convert constants of type 63112099Sjoerg * long double which are greater then DBL_MAX. 63212099Sjoerg */ 63312099Sjoergstatic int 63412099Sjoergfcon() 63512099Sjoerg{ 63612099Sjoerg const char *cp; 63712099Sjoerg int len; 63812099Sjoerg tspec_t typ; 63912099Sjoerg char c, *eptr; 64012099Sjoerg double d; 64112099Sjoerg float f; 64212099Sjoerg 64312099Sjoerg cp = yytext; 64412099Sjoerg len = yyleng; 64512099Sjoerg 64612099Sjoerg if ((c = cp[len - 1]) == 'f' || c == 'F') { 64712099Sjoerg typ = FLOAT; 64812099Sjoerg len--; 64912099Sjoerg } else if (c == 'l' || c == 'L') { 65012099Sjoerg typ = LDOUBLE; 65112099Sjoerg len--; 65212099Sjoerg } else { 65312099Sjoerg typ = DOUBLE; 65412099Sjoerg } 65512099Sjoerg 65612099Sjoerg if (tflag && typ != DOUBLE) { 65712099Sjoerg /* suffixes F and L are illegal in traditional C */ 65812099Sjoerg warning(98); 65912099Sjoerg } 66012099Sjoerg 66112099Sjoerg errno = 0; 66212099Sjoerg d = strtod(cp, &eptr); 66312099Sjoerg if (eptr != cp + len) 66412099Sjoerg lerror("fcon() 1"); 66512099Sjoerg if (errno != 0) 66612099Sjoerg /* floating-point constant out of range */ 66712099Sjoerg warning(248); 66812099Sjoerg 66912099Sjoerg if (typ == FLOAT) { 67012099Sjoerg f = (float)d; 67112099Sjoerg if (isinf(f)) { 67212099Sjoerg /* floating-point constant out of range */ 67312099Sjoerg warning(248); 67412099Sjoerg f = f > 0 ? FLT_MAX : -FLT_MAX; 67512099Sjoerg } 67612099Sjoerg } 67712099Sjoerg 67880284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 67980284Sobrien nomem(); 68080284Sobrien yylval.y_val->v_tspec = typ; 68112099Sjoerg if (typ == FLOAT) { 68212099Sjoerg yylval.y_val->v_ldbl = f; 68312099Sjoerg } else { 68412099Sjoerg yylval.y_val->v_ldbl = d; 68512099Sjoerg } 68612099Sjoerg 68712099Sjoerg return (T_CON); 68812099Sjoerg} 68912099Sjoerg 69012099Sjoergstatic int 69112099Sjoergoperator(t, o) 69212099Sjoerg int t; 69312099Sjoerg op_t o; 69412099Sjoerg{ 69512099Sjoerg yylval.y_op = o; 69612099Sjoerg return (t); 69712099Sjoerg} 69812099Sjoerg 69912099Sjoerg/* 70012099Sjoerg * Called if lex found a leading \'. 70112099Sjoerg */ 70212099Sjoergstatic int 70312099Sjoergccon() 70412099Sjoerg{ 70512099Sjoerg int n, val, c; 70612099Sjoerg char cv; 70712099Sjoerg 70812099Sjoerg n = 0; 70912099Sjoerg val = 0; 71012099Sjoerg while ((c = getescc('\'')) >= 0) { 71112099Sjoerg val = (val << CHAR_BIT) + c; 71212099Sjoerg n++; 71312099Sjoerg } 71412099Sjoerg if (c == -2) { 71512099Sjoerg /* unterminated character constant */ 71612099Sjoerg error(253); 71712099Sjoerg } else { 71812099Sjoerg if (n > sizeof (int) || (n > 1 && (pflag || hflag))) { 71912099Sjoerg /* too many characters in character constant */ 72012099Sjoerg error(71); 72112099Sjoerg } else if (n > 1) { 72212099Sjoerg /* multi-character character constant */ 72312099Sjoerg warning(294); 72412099Sjoerg } else if (n == 0) { 72512099Sjoerg /* empty character constant */ 72612099Sjoerg error(73); 72712099Sjoerg } 72812099Sjoerg } 72912099Sjoerg if (n == 1) { 73012099Sjoerg cv = (char)val; 73112099Sjoerg val = cv; 73212099Sjoerg } 73312099Sjoerg 73480284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 73580284Sobrien nomem(); 73612099Sjoerg yylval.y_val->v_tspec = INT; 73712099Sjoerg yylval.y_val->v_quad = val; 73812099Sjoerg 73912099Sjoerg return (T_CON); 74012099Sjoerg} 74112099Sjoerg 74212099Sjoerg/* 74312099Sjoerg * Called if lex found a leading L\' 74412099Sjoerg */ 74512099Sjoergstatic int 74612099Sjoergwccon() 74712099Sjoerg{ 74812099Sjoerg static char buf[MB_LEN_MAX + 1]; 74912099Sjoerg int i, c; 75012099Sjoerg wchar_t wc; 75112099Sjoerg 75212099Sjoerg i = 0; 75312099Sjoerg while ((c = getescc('\'')) >= 0) { 75412099Sjoerg if (i < MB_CUR_MAX) 75512099Sjoerg buf[i] = (char)c; 75612099Sjoerg i++; 75712099Sjoerg } 75812099Sjoerg 75912099Sjoerg wc = 0; 76012099Sjoerg 76112099Sjoerg if (c == -2) { 76212099Sjoerg /* unterminated character constant */ 76312099Sjoerg error(253); 76412099Sjoerg } else if (c == 0) { 76512099Sjoerg /* empty character constant */ 76612099Sjoerg error(73); 76712099Sjoerg } else { 76812099Sjoerg if (i > MB_CUR_MAX) { 76912099Sjoerg i = MB_CUR_MAX; 77012099Sjoerg /* too many characters in character constant */ 77112099Sjoerg error(71); 77212099Sjoerg } else { 77312099Sjoerg buf[i] = '\0'; 77412099Sjoerg (void)mbtowc(NULL, NULL, 0); 77512099Sjoerg if (mbtowc(&wc, buf, MB_CUR_MAX) < 0) 77612099Sjoerg /* invalid multibyte character */ 77712099Sjoerg error(291); 77812099Sjoerg } 77912099Sjoerg } 78012099Sjoerg 78180284Sobrien if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL) 78280284Sobrien nomem(); 78312099Sjoerg yylval.y_val->v_tspec = WCHAR; 78412099Sjoerg yylval.y_val->v_quad = wc; 78512099Sjoerg 78612099Sjoerg return (T_CON); 78712099Sjoerg} 78812099Sjoerg 78912099Sjoerg/* 79012099Sjoerg * Read a character which is part of a character constant or of a string 79112099Sjoerg * and handle escapes. 79212099Sjoerg * 79312099Sjoerg * The Argument is the character which delimits the character constant or 79412099Sjoerg * string. 79512099Sjoerg * 79612099Sjoerg * Returns -1 if the end of the character constant or string is reached, 79712099Sjoerg * -2 if the EOF is reached, and the charachter otherwise. 79812099Sjoerg */ 79912099Sjoergstatic int 80012099Sjoerggetescc(d) 80112099Sjoerg int d; 80212099Sjoerg{ 80312099Sjoerg static int pbc = -1; 80412099Sjoerg int n, c, v; 80512099Sjoerg 80612099Sjoerg if (pbc == -1) { 80712099Sjoerg c = inpc(); 80812099Sjoerg } else { 80912099Sjoerg c = pbc; 81012099Sjoerg pbc = -1; 81112099Sjoerg } 81212099Sjoerg if (c == d) 81312099Sjoerg return (-1); 81412099Sjoerg switch (c) { 81512099Sjoerg case '\n': 81612099Sjoerg /* newline in string or char constant */ 81712099Sjoerg error(254); 81812099Sjoerg return (-2); 81912099Sjoerg case EOF: 82012099Sjoerg return (-2); 82112099Sjoerg case '\\': 82212099Sjoerg switch (c = inpc()) { 82312099Sjoerg case '"': 82412099Sjoerg if (tflag && d == '\'') 82512099Sjoerg /* \" inside character constant undef. ... */ 82612099Sjoerg warning(262); 82712099Sjoerg return ('"'); 82812099Sjoerg case '\'': 82912099Sjoerg return ('\''); 83012099Sjoerg case '?': 83112099Sjoerg if (tflag) 83212099Sjoerg /* \? undefined in traditional C */ 83312099Sjoerg warning(263); 83412099Sjoerg return ('?'); 83512099Sjoerg case '\\': 83612099Sjoerg return ('\\'); 83712099Sjoerg case 'a': 83812099Sjoerg if (tflag) 83912099Sjoerg /* \a undefined in traditional C */ 84012099Sjoerg warning(81); 84112099Sjoerg#ifdef __STDC__ 84212099Sjoerg return ('\a'); 84312099Sjoerg#else 84412099Sjoerg return ('\007'); 84512099Sjoerg#endif 84612099Sjoerg case 'b': 84712099Sjoerg return ('\b'); 84812099Sjoerg case 'f': 84912099Sjoerg return ('\f'); 85012099Sjoerg case 'n': 85112099Sjoerg return ('\n'); 85212099Sjoerg case 'r': 85312099Sjoerg return ('\r'); 85412099Sjoerg case 't': 85512099Sjoerg return ('\t'); 85612099Sjoerg case 'v': 85712099Sjoerg if (tflag) 85812099Sjoerg /* \v undefined in traditional C */ 85912099Sjoerg warning(264); 86012099Sjoerg#ifdef __STDC__ 86112099Sjoerg return ('\v'); 86212099Sjoerg#else 86312099Sjoerg return ('\013'); 86412099Sjoerg#endif 86512099Sjoerg case '8': case '9': 86612099Sjoerg /* bad octal digit %c */ 86712099Sjoerg warning(77, c); 86812099Sjoerg /* FALLTHROUGH */ 86912099Sjoerg case '0': case '1': case '2': case '3': 87012099Sjoerg case '4': case '5': case '6': case '7': 87112099Sjoerg n = 3; 87212099Sjoerg v = 0; 87312099Sjoerg do { 87412099Sjoerg v = (v << 3) + (c - '0'); 87512099Sjoerg c = inpc(); 87612099Sjoerg } while (--n && isdigit(c) && (tflag || c <= '7')); 87712099Sjoerg if (tflag && n > 0 && isdigit(c)) 87812099Sjoerg /* bad octal digit %c */ 87912099Sjoerg warning(77, c); 88012099Sjoerg pbc = c; 88112099Sjoerg if (v > UCHAR_MAX) { 88212099Sjoerg /* character escape does not fit in char. */ 88312099Sjoerg warning(76); 88412099Sjoerg v &= CHAR_MASK; 88512099Sjoerg } 88612099Sjoerg return (v); 88712099Sjoerg case 'x': 88812099Sjoerg if (tflag) 88912099Sjoerg /* \x undefined in traditional C */ 89012099Sjoerg warning(82); 89112099Sjoerg v = 0; 89212099Sjoerg n = 0; 89312099Sjoerg while ((c = inpc()) >= 0 && isxdigit(c)) { 89412099Sjoerg c = isdigit(c) ? 89512099Sjoerg c - '0' : toupper(c) - 'A' + 10; 89612099Sjoerg v = (v << 4) + c; 89712099Sjoerg if (n >= 0) { 89812099Sjoerg if ((v & ~CHAR_MASK) != 0) { 89912099Sjoerg /* overflow in hex escape */ 90012099Sjoerg warning(75); 90112099Sjoerg n = -1; 90212099Sjoerg } else { 90312099Sjoerg n++; 90412099Sjoerg } 90512099Sjoerg } 90612099Sjoerg } 90712099Sjoerg pbc = c; 90812099Sjoerg if (n == 0) { 90912099Sjoerg /* no hex digits follow \x */ 91012099Sjoerg error(74); 91112099Sjoerg } if (n == -1) { 91212099Sjoerg v &= CHAR_MASK; 91312099Sjoerg } 91412099Sjoerg return (v); 91512099Sjoerg case '\n': 91612099Sjoerg return (getescc(d)); 91712099Sjoerg case EOF: 91812099Sjoerg return (-2); 91912099Sjoerg default: 92012099Sjoerg if (isprint(c)) { 92112099Sjoerg /* dubious escape \%c */ 92212099Sjoerg warning(79, c); 92312099Sjoerg } else { 92412099Sjoerg /* dubious escape \%o */ 92512099Sjoerg warning(80, c); 92612099Sjoerg } 92712099Sjoerg } 92812099Sjoerg } 92912099Sjoerg return (c); 93012099Sjoerg} 93112099Sjoerg 93212099Sjoerg/* 93312099Sjoerg * Called for preprocessor directives. Currently implemented are: 93412099Sjoerg * # lineno 93512099Sjoerg * # lineno "filename" 93612099Sjoerg */ 93712099Sjoergstatic void 93812099Sjoergdirective() 93912099Sjoerg{ 94012099Sjoerg const char *cp, *fn; 94112099Sjoerg char c, *eptr; 94212099Sjoerg size_t fnl; 94312099Sjoerg long ln; 94412099Sjoerg static int first = 1; 94512099Sjoerg 94612099Sjoerg /* Go to first non-whitespace after # */ 94712099Sjoerg for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) ; 94812099Sjoerg 94912099Sjoerg if (!isdigit(c)) { 95012099Sjoerg error: 95112099Sjoerg /* undefined or invalid # directive */ 95212099Sjoerg warning(255); 95312099Sjoerg return; 95412099Sjoerg } 95512099Sjoerg ln = strtol(--cp, &eptr, 10); 95612099Sjoerg if (cp == eptr) 95712099Sjoerg goto error; 95812099Sjoerg if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0') 95912099Sjoerg goto error; 96012099Sjoerg while ((c = *cp++) == ' ' || c == '\t') ; 96112099Sjoerg if (c != '\0') { 96212099Sjoerg if (c != '"') 96312099Sjoerg goto error; 96412099Sjoerg fn = cp; 96512099Sjoerg while ((c = *cp) != '"' && c != '\0') 96612099Sjoerg cp++; 96712099Sjoerg if (c != '"') 96812099Sjoerg goto error; 96912099Sjoerg if ((fnl = cp++ - fn) > PATH_MAX) 97012099Sjoerg goto error; 97112099Sjoerg while ((c = *cp++) == ' ' || c == '\t') ; 97212099Sjoerg#if 0 97312099Sjoerg if (c != '\0') 97412099Sjoerg warning("extra character(s) after directive"); 97512099Sjoerg#endif 97612099Sjoerg curr_pos.p_file = fnnalloc(fn, fnl); 97712099Sjoerg /* 97812099Sjoerg * If this is the first directive, the name is the name 97912099Sjoerg * of the C source file as specified at the command line. 98012099Sjoerg * It is written to the output file. 98112099Sjoerg */ 98212099Sjoerg if (first) { 98312099Sjoerg csrc_pos.p_file = curr_pos.p_file; 98412099Sjoerg outsrc(curr_pos.p_file); 98512099Sjoerg first = 0; 98612099Sjoerg } 98712099Sjoerg } 98812099Sjoerg curr_pos.p_line = (int)ln - 1; 98912099Sjoerg if (curr_pos.p_file == csrc_pos.p_file) 99012099Sjoerg csrc_pos.p_line = (int)ln - 1; 99112099Sjoerg} 99212099Sjoerg 99312099Sjoerg/* 99412099Sjoerg * Handle lint comments. Following comments are currently understood: 99512099Sjoerg * ARGSUSEDn 99612099Sjoerg * CONSTCOND CONSTANTCOND CONSTANTCONDITION 99712099Sjoerg * FALLTHRU FALLTHROUGH 99812099Sjoerg * LINTLIBRARY 99912099Sjoerg * LINTED NOSTRICT 100012099Sjoerg * LONGLONG 100112099Sjoerg * NOTREACHED 100212099Sjoerg * PRINTFLIKEn 100312099Sjoerg * PROTOLIB 100412099Sjoerg * SCANFLIKEn 100512099Sjoerg * VARARGSn 100612099Sjoerg * If one of this comments is recognized, the arguments, if any, are 100712099Sjoerg * parsed and a function which handles this comment is called. 100812099Sjoerg */ 100912099Sjoergstatic void 101012099Sjoergcomment() 101112099Sjoerg{ 101212099Sjoerg int c, lc; 101312099Sjoerg static struct { 101412099Sjoerg const char *keywd; 101512099Sjoerg int arg; 101612099Sjoerg void (*func) __P((int)); 101712099Sjoerg } keywtab[] = { 101812099Sjoerg { "ARGSUSED", 1, argsused }, 101912099Sjoerg { "CONSTCOND", 0, constcond }, 102012099Sjoerg { "CONSTANTCOND", 0, constcond }, 102112099Sjoerg { "CONSTANTCONDITION", 0, constcond }, 102212099Sjoerg { "FALLTHRU", 0, fallthru }, 102312099Sjoerg { "FALLTHROUGH", 0, fallthru }, 102412099Sjoerg { "LINTLIBRARY", 0, lintlib }, 102512099Sjoerg { "LINTED", 0, linted }, 102612099Sjoerg { "LONGLONG", 0, longlong }, 102712099Sjoerg { "NOSTRICT", 0, linted }, 102812099Sjoerg { "NOTREACHED", 0, notreach }, 102912099Sjoerg { "PRINTFLIKE", 1, printflike }, 103012099Sjoerg { "PROTOLIB", 1, protolib }, 103112099Sjoerg { "SCANFLIKE", 1, scanflike }, 103212099Sjoerg { "VARARGS", 1, varargs }, 103312099Sjoerg }; 103412099Sjoerg char keywd[32]; 103512099Sjoerg char arg[32]; 103612099Sjoerg int l, i, a; 103712099Sjoerg int eoc; 103812099Sjoerg 103912099Sjoerg eoc = 0; 104012099Sjoerg 104112099Sjoerg /* Skip white spaces after the start of the comment */ 104212099Sjoerg while ((c = inpc()) != EOF && isspace(c)) ; 104312099Sjoerg 104412099Sjoerg /* Read the potential keyword to keywd */ 104512099Sjoerg l = 0; 104612099Sjoerg while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) { 104712099Sjoerg keywd[l++] = (char)c; 104812099Sjoerg c = inpc(); 104912099Sjoerg } 105012099Sjoerg keywd[l] = '\0'; 105112099Sjoerg 105212099Sjoerg /* look for the keyword */ 105312099Sjoerg for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) { 105412099Sjoerg if (strcmp(keywtab[i].keywd, keywd) == 0) 105512099Sjoerg break; 105612099Sjoerg } 105712099Sjoerg if (i == sizeof (keywtab) / sizeof (keywtab[0])) 105812099Sjoerg goto skip_rest; 105912099Sjoerg 106012099Sjoerg /* skip white spaces after the keyword */ 106112099Sjoerg while (c != EOF && isspace(c)) 106212099Sjoerg c = inpc(); 106312099Sjoerg 106412099Sjoerg /* read the argument, if the keyword accepts one and there is one */ 106512099Sjoerg l = 0; 106612099Sjoerg if (keywtab[i].arg) { 106712099Sjoerg while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) { 106812099Sjoerg arg[l++] = (char)c; 106912099Sjoerg c = inpc(); 107012099Sjoerg } 107112099Sjoerg } 107212099Sjoerg arg[l] = '\0'; 107312099Sjoerg a = l != 0 ? atoi(arg) : -1; 107412099Sjoerg 107512099Sjoerg /* skip white spaces after the argument */ 107612099Sjoerg while (c != EOF && isspace(c)) 107712099Sjoerg c = inpc(); 107812099Sjoerg 107912099Sjoerg if (c != '*' || (c = inpc()) != '/') { 108012099Sjoerg if (keywtab[i].func != linted) 108112099Sjoerg /* extra characters in lint comment */ 108212099Sjoerg warning(257); 108312099Sjoerg } else { 108412099Sjoerg /* 108512099Sjoerg * remember that we have already found the end of the 108612099Sjoerg * comment 108712099Sjoerg */ 108812099Sjoerg eoc = 1; 108912099Sjoerg } 109012099Sjoerg 109112099Sjoerg if (keywtab[i].func != NULL) 109212099Sjoerg (*keywtab[i].func)(a); 109312099Sjoerg 109412099Sjoerg skip_rest: 109512099Sjoerg while (!eoc) { 109612099Sjoerg lc = c; 109712099Sjoerg if ((c = inpc()) == EOF) { 109812099Sjoerg /* unterminated comment */ 109912099Sjoerg error(256); 110012099Sjoerg break; 110112099Sjoerg } 110212099Sjoerg if (lc == '*' && c == '/') 110312099Sjoerg eoc = 1; 110412099Sjoerg } 110512099Sjoerg} 110612099Sjoerg 110712099Sjoerg/* 110812099Sjoerg * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND. 110912099Sjoerg * clrwflgs() is called after function definitions and global and 111012099Sjoerg * local declarations and definitions. It is also called between 111112099Sjoerg * the controlling expression and the body of control statements 111212099Sjoerg * (if, switch, for, while). 111312099Sjoerg */ 111412099Sjoergvoid 111512099Sjoergclrwflgs() 111612099Sjoerg{ 111712099Sjoerg nowarn = 0; 111812099Sjoerg quadflg = 0; 111912099Sjoerg ccflg = 0; 112012099Sjoerg} 112112099Sjoerg 112212099Sjoerg/* 112312099Sjoerg * Strings are stored in a dynamically alloceted buffer and passed 112412099Sjoerg * in yylval.y_xstrg to the parser. The parser or the routines called 112512099Sjoerg * by the parser are responsible for freeing this buffer. 112612099Sjoerg */ 112712099Sjoergstatic int 112812099Sjoergstring() 112912099Sjoerg{ 113012099Sjoerg u_char *s; 113112099Sjoerg int c; 113212099Sjoerg size_t len, max; 113312099Sjoerg strg_t *strg; 113412099Sjoerg 113580284Sobrien if ((s = malloc(max = 64)) == NULL) 113680284Sobrien nomem(); 113712099Sjoerg 113812099Sjoerg len = 0; 113912099Sjoerg while ((c = getescc('"')) >= 0) { 114012099Sjoerg /* +1 to reserve space for a trailing NUL character */ 114112099Sjoerg if (len + 1 == max) 114280284Sobrien if ((s = realloc(s, max *= 2)) == NULL) 114380284Sobrien nomem(); 114412099Sjoerg s[len++] = (char)c; 114512099Sjoerg } 114612099Sjoerg s[len] = '\0'; 114712099Sjoerg if (c == -2) 114812099Sjoerg /* unterminated string constant */ 114912099Sjoerg error(258); 115012099Sjoerg 115180284Sobrien if ((strg = calloc(1, sizeof (strg_t))) == NULL) 115280284Sobrien nomem(); 115312099Sjoerg strg->st_tspec = CHAR; 115412099Sjoerg strg->st_len = len; 115512099Sjoerg strg->st_cp = s; 115612099Sjoerg 115712099Sjoerg yylval.y_strg = strg; 115812099Sjoerg return (T_STRING); 115912099Sjoerg} 116012099Sjoerg 116112099Sjoergstatic int 116212099Sjoergwcstrg() 116312099Sjoerg{ 116412099Sjoerg char *s; 116512099Sjoerg int c, i, n, wi; 116612099Sjoerg size_t len, max, wlen; 116712099Sjoerg wchar_t *ws; 116812099Sjoerg strg_t *strg; 116912099Sjoerg 117080284Sobrien if ((s = malloc(max = 64)) == NULL) 117180284Sobrien nomem(); 117212099Sjoerg len = 0; 117312099Sjoerg while ((c = getescc('"')) >= 0) { 117412099Sjoerg /* +1 to save space for a trailing NUL character */ 117512099Sjoerg if (len + 1 >= max) 117680284Sobrien if ((s = realloc(s, max *= 2)) == NULL) 117780284Sobrien nomem(); 117812099Sjoerg s[len++] = (char)c; 117912099Sjoerg } 118012099Sjoerg s[len] = '\0'; 118112099Sjoerg if (c == -2) 118212099Sjoerg /* unterminated string constant */ 118312099Sjoerg error(258); 118412099Sjoerg 118512099Sjoerg /* get length of wide character string */ 118612099Sjoerg (void)mblen(NULL, 0); 118712099Sjoerg for (i = 0, wlen = 0; i < len; i += n, wlen++) { 118812099Sjoerg if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) { 118912099Sjoerg /* invalid multibyte character */ 119012099Sjoerg error(291); 119112099Sjoerg break; 119212099Sjoerg } 119312099Sjoerg if (n == 0) 119412099Sjoerg n = 1; 119512099Sjoerg } 119612099Sjoerg 119780284Sobrien if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL) 119880284Sobrien nomem(); 119912099Sjoerg 120012099Sjoerg /* convert from multibyte to wide char */ 120112099Sjoerg (void)mbtowc(NULL, NULL, 0); 120212099Sjoerg for (i = 0, wi = 0; i < len; i += n, wi++) { 120312099Sjoerg if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1) 120412099Sjoerg break; 120512099Sjoerg if (n == 0) 120612099Sjoerg n = 1; 120712099Sjoerg } 120812099Sjoerg ws[wi] = 0; 120912099Sjoerg free(s); 121012099Sjoerg 121180284Sobrien if ((strg = calloc(1, sizeof (strg_t))) == NULL) 121280284Sobrien nomem(); 121312099Sjoerg strg->st_tspec = WCHAR; 121412099Sjoerg strg->st_len = wlen; 121512099Sjoerg strg->st_wcp = ws; 121612099Sjoerg 121712099Sjoerg yylval.y_strg = strg; 121812099Sjoerg return (T_STRING); 121912099Sjoerg} 122012099Sjoerg 122112099Sjoerg/* 122212099Sjoerg * As noted above the scanner does not create new symbol table entries 122312099Sjoerg * for symbols it cannot find in the symbol table. This is to avoid 122412099Sjoerg * putting undeclared symbols into the symbol table if a syntax error 122512099Sjoerg * occurs. 122612099Sjoerg * 122712099Sjoerg * getsym() is called as soon as it is probably ok to put the symbol to 122812099Sjoerg * the symbol table. This does not mean that it is not possible that 122912099Sjoerg * symbols are put to the symbol table which are than not completely 123012099Sjoerg * declared due to syntax errors. To avoid too many problems in this 123112099Sjoerg * case symbols get type int in getsym(). 123212099Sjoerg * 123312099Sjoerg * XXX calls to getsym() should be delayed until decl1*() is called 123412099Sjoerg */ 123512099Sjoergsym_t * 123612099Sjoerggetsym(sb) 123712099Sjoerg sbuf_t *sb; 123812099Sjoerg{ 123912099Sjoerg dinfo_t *di; 124012099Sjoerg char *s; 124112099Sjoerg sym_t *sym; 124212099Sjoerg 124312099Sjoerg sym = sb->sb_sym; 124412099Sjoerg 124512099Sjoerg /* 124612099Sjoerg * During member declaration it is possible that name() looked 124712099Sjoerg * for symbols of type FVFT, although it should have looked for 124812099Sjoerg * symbols of type FTAG. Same can happen for labels. Both cases 124912099Sjoerg * are compensated here. 125012099Sjoerg */ 125112099Sjoerg if (symtyp == FMOS || symtyp == FLAB) { 125212099Sjoerg if (sym == NULL || sym->s_kind == FVFT) 125312099Sjoerg sym = search(sb); 125412099Sjoerg } 125512099Sjoerg 125612099Sjoerg if (sym != NULL) { 125712099Sjoerg if (sym->s_kind != symtyp) 125812099Sjoerg lerror("storesym() 1"); 125912099Sjoerg symtyp = FVFT; 126012099Sjoerg freesb(sb); 126112099Sjoerg return (sym); 126212099Sjoerg } 126312099Sjoerg 126412099Sjoerg /* create a new symbol table entry */ 126512099Sjoerg 126612099Sjoerg /* labels must always be allocated at level 1 (outhermost block) */ 126712099Sjoerg if (symtyp == FLAB) { 126812099Sjoerg sym = getlblk(1, sizeof (sym_t)); 126912099Sjoerg s = getlblk(1, sb->sb_len + 1); 127012099Sjoerg (void)memcpy(s, sb->sb_name, sb->sb_len + 1); 127112099Sjoerg sym->s_name = s; 127212099Sjoerg sym->s_blklev = 1; 127312099Sjoerg di = dcs; 127412099Sjoerg while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL) 127512099Sjoerg di = di->d_nxt; 127612099Sjoerg if (di->d_ctx != AUTO) 127712099Sjoerg lerror("storesym() 2"); 127812099Sjoerg } else { 127912099Sjoerg sym = getblk(sizeof (sym_t)); 128012099Sjoerg sym->s_name = sb->sb_name; 128112099Sjoerg sym->s_blklev = blklev; 128212099Sjoerg di = dcs; 128312099Sjoerg } 128412099Sjoerg 128512099Sjoerg STRUCT_ASSIGN(sym->s_dpos, curr_pos); 128612099Sjoerg if ((sym->s_kind = symtyp) != FLAB) 128712099Sjoerg sym->s_type = gettyp(INT); 128812099Sjoerg 128912099Sjoerg symtyp = FVFT; 129012099Sjoerg 129112099Sjoerg if ((sym->s_link = symtab[sb->sb_hash]) != NULL) 129212099Sjoerg symtab[sb->sb_hash]->s_rlink = &sym->s_link; 129312099Sjoerg (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash]; 129412099Sjoerg 129512099Sjoerg *di->d_ldlsym = sym; 129612099Sjoerg di->d_ldlsym = &sym->s_dlnxt; 129712099Sjoerg 129812099Sjoerg freesb(sb); 129912099Sjoerg return (sym); 130012099Sjoerg} 130112099Sjoerg 130212099Sjoerg/* 130312099Sjoerg * Remove a symbol forever from the symbol table. s_blklev 130412099Sjoerg * is set to -1 to avoid that the symbol will later be put 130512099Sjoerg * back to the symbol table. 130612099Sjoerg */ 130712099Sjoergvoid 130812099Sjoergrmsym(sym) 130912099Sjoerg sym_t *sym; 131012099Sjoerg{ 131112099Sjoerg if ((*sym->s_rlink = sym->s_link) != NULL) 131212099Sjoerg sym->s_link->s_rlink = sym->s_rlink; 131312099Sjoerg sym->s_blklev = -1; 131412099Sjoerg sym->s_link = NULL; 131512099Sjoerg} 131612099Sjoerg 131712099Sjoerg/* 131812099Sjoerg * Remove a list of symbols declared at one level from the symbol 131912099Sjoerg * table. 132012099Sjoerg */ 132112099Sjoergvoid 132212099Sjoergrmsyms(syms) 132312099Sjoerg sym_t *syms; 132412099Sjoerg{ 132512099Sjoerg sym_t *sym; 132612099Sjoerg 132712099Sjoerg for (sym = syms; sym != NULL; sym = sym->s_dlnxt) { 132812099Sjoerg if (sym->s_blklev != -1) { 132912099Sjoerg if ((*sym->s_rlink = sym->s_link) != NULL) 133012099Sjoerg sym->s_link->s_rlink = sym->s_rlink; 133112099Sjoerg sym->s_link = NULL; 133212099Sjoerg sym->s_rlink = NULL; 133312099Sjoerg } 133412099Sjoerg } 133512099Sjoerg} 133612099Sjoerg 133712099Sjoerg/* 133812099Sjoerg * Put a symbol into the symbol table 133912099Sjoerg */ 134012099Sjoergvoid 134112099Sjoerginssym(bl, sym) 134212099Sjoerg int bl; 134312099Sjoerg sym_t *sym; 134412099Sjoerg{ 134512099Sjoerg int h; 134612099Sjoerg 134712099Sjoerg h = hash(sym->s_name); 134812099Sjoerg if ((sym->s_link = symtab[h]) != NULL) 134912099Sjoerg symtab[h]->s_rlink = &sym->s_link; 135012099Sjoerg (symtab[h] = sym)->s_rlink = &symtab[h]; 135112099Sjoerg sym->s_blklev = bl; 135212099Sjoerg if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev) 135312099Sjoerg lerror("inssym()"); 135412099Sjoerg} 135512099Sjoerg 135612099Sjoerg/* 135712099Sjoerg * Called at level 0 after syntax errors 135812099Sjoerg * Removes all symbols which are not declared at level 0 from the 135912099Sjoerg * symbol table. Also frees all memory which is not associated with 136012099Sjoerg * level 0. 136112099Sjoerg */ 136212099Sjoergvoid 136312099Sjoergcleanup() 136412099Sjoerg{ 136512099Sjoerg sym_t *sym, *nsym; 136612099Sjoerg int i; 136712099Sjoerg 136812099Sjoerg for (i = 0; i < HSHSIZ1; i++) { 136912099Sjoerg for (sym = symtab[i]; sym != NULL; sym = nsym) { 137012099Sjoerg nsym = sym->s_link; 137112099Sjoerg if (sym->s_blklev >= 1) { 137212099Sjoerg if ((*sym->s_rlink = nsym) != NULL) 137312099Sjoerg nsym->s_rlink = sym->s_rlink; 137412099Sjoerg } 137512099Sjoerg } 137612099Sjoerg } 137712099Sjoerg 137812099Sjoerg for (i = mblklev; i > 0; i--) 137912099Sjoerg freelblk(i); 138012099Sjoerg} 138112099Sjoerg 138212099Sjoerg/* 138312099Sjoerg * Create a new symbol with the name of an existing symbol. 138412099Sjoerg */ 138512099Sjoergsym_t * 138612099Sjoergpushdown(sym) 138712099Sjoerg sym_t *sym; 138812099Sjoerg{ 138912099Sjoerg int h; 139012099Sjoerg sym_t *nsym; 139112099Sjoerg 139212099Sjoerg h = hash(sym->s_name); 139312099Sjoerg nsym = getblk(sizeof (sym_t)); 139412099Sjoerg if (sym->s_blklev > blklev) 139512099Sjoerg lerror("pushdown()"); 139612099Sjoerg nsym->s_name = sym->s_name; 139712099Sjoerg STRUCT_ASSIGN(nsym->s_dpos, curr_pos); 139812099Sjoerg nsym->s_kind = sym->s_kind; 139912099Sjoerg nsym->s_blklev = blklev; 140012099Sjoerg 140112099Sjoerg if ((nsym->s_link = symtab[h]) != NULL) 140212099Sjoerg symtab[h]->s_rlink = &nsym->s_link; 140312099Sjoerg (symtab[h] = nsym)->s_rlink = &symtab[h]; 140412099Sjoerg 140512099Sjoerg *dcs->d_ldlsym = nsym; 140612099Sjoerg dcs->d_ldlsym = &nsym->s_dlnxt; 140712099Sjoerg 140812099Sjoerg return (nsym); 140912099Sjoerg} 141012099Sjoerg 141112099Sjoerg/* 141212099Sjoerg * Free any dynamically allocated memory referenced by 141312099Sjoerg * the value stack or yylval. 141412099Sjoerg * The type of information in yylval is described by tok. 141512099Sjoerg */ 141612099Sjoergvoid 141712099Sjoergfreeyyv(sp, tok) 141812099Sjoerg void *sp; 141912099Sjoerg int tok; 142012099Sjoerg{ 142112099Sjoerg if (tok == T_NAME || tok == T_TYPENAME) { 142212099Sjoerg sbuf_t *sb = *(sbuf_t **)sp; 142312099Sjoerg freesb(sb); 142412099Sjoerg } else if (tok == T_CON) { 142512099Sjoerg val_t *val = *(val_t **)sp; 142612099Sjoerg free(val); 142712099Sjoerg } else if (tok == T_STRING) { 142812099Sjoerg strg_t *strg = *(strg_t **)sp; 142912099Sjoerg if (strg->st_tspec == CHAR) { 143012099Sjoerg free(strg->st_cp); 143112099Sjoerg } else if (strg->st_tspec == WCHAR) { 143212099Sjoerg free(strg->st_wcp); 143312099Sjoerg } else { 143412099Sjoerg lerror("fryylv() 1"); 143512099Sjoerg } 143612099Sjoerg free(strg); 143712099Sjoerg } 143812099Sjoerg} 1439