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