112099Sjoerg%{
2280387Spfg/* $NetBSD: scan.l,v 1.37 2007/02/06 00:08:31 he Exp $ */
312099Sjoerg
412099Sjoerg/*
591592Smarkm * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
612099Sjoerg * Copyright (c) 1994, 1995 Jochen Pohl
712099Sjoerg * All Rights Reserved.
812099Sjoerg *
912099Sjoerg * Redistribution and use in source and binary forms, with or without
1012099Sjoerg * modification, are permitted provided that the following conditions
1112099Sjoerg * are met:
1212099Sjoerg * 1. Redistributions of source code must retain the above copyright
1312099Sjoerg *    notice, this list of conditions and the following disclaimer.
1412099Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1512099Sjoerg *    notice, this list of conditions and the following disclaimer in the
1612099Sjoerg *    documentation and/or other materials provided with the distribution.
1712099Sjoerg * 3. All advertising materials mentioning features or use of this software
1812099Sjoerg *    must display the following acknowledgement:
1912099Sjoerg *      This product includes software developed by Jochen Pohl for
2012099Sjoerg *      The NetBSD Project.
2112099Sjoerg * 4. The name of the author may not be used to endorse or promote products
2212099Sjoerg *    derived from this software without specific prior written permission.
2312099Sjoerg *
2412099Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2512099Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2612099Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2712099Sjoerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2812099Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2912099Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3012099Sjoerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3112099Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3212099Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3312099Sjoerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3412099Sjoerg */
3512099Sjoerg
3691592Smarkm#include <sys/cdefs.h>
3791592Smarkm#if defined(__RCSID) && !defined(lint)
38280387Spfg__RCSID("$NetBSD: scan.l,v 1.37 2007/02/06 00:08:31 he Exp $");
3912099Sjoerg#endif
4091592Smarkm__FBSDID("$FreeBSD$");
4112099Sjoerg
4212099Sjoerg#include <stdlib.h>
4312099Sjoerg#include <string.h>
4412099Sjoerg#include <limits.h>
4512099Sjoerg#include <float.h>
4612099Sjoerg#include <ctype.h>
4712099Sjoerg#include <errno.h>
4812099Sjoerg#include <err.h>
4917142Sjkh#include <math.h>
5012099Sjoerg
5112099Sjoerg#include "lint1.h"
5291592Smarkm#include "cgram.h"
5312099Sjoerg
5412099Sjoerg#define CHAR_MASK	(~(~0 << CHAR_BIT))
5512099Sjoerg
5612099Sjoerg/* Current position (its also updated when an included file is parsed) */
5791592Smarkmpos_t	curr_pos = { 1, "", 0 };
5812099Sjoerg
5912099Sjoerg/*
6012099Sjoerg * Current position in C source (not updated when an included file is
6112099Sjoerg * parsed).
6212099Sjoerg */
6391592Smarkmpos_t	csrc_pos = { 1, "", 0 };
6412099Sjoerg
6591592Smarkmstatic	void	incline(void);
6691592Smarkmstatic	void	badchar(int);
6791592Smarkmstatic	sbuf_t	*allocsb(void);
6891592Smarkmstatic	void	freesb(sbuf_t *);
6991592Smarkmstatic	int	inpc(void);
7091592Smarkmstatic	int	hash(const char *);
7191592Smarkmstatic	sym_t	*search(sbuf_t *);
7291592Smarkmstatic	int	name(void);
7391592Smarkmstatic	int	keyw(sym_t *);
7491592Smarkmstatic	int	icon(int);
7591592Smarkmstatic	int	fcon(void);
7691592Smarkmstatic	int	operator(int, op_t);
7791592Smarkmstatic	int	ccon(void);
7891592Smarkmstatic	int	wccon(void);
7991592Smarkmstatic	int	getescc(int);
8091592Smarkmstatic	void	directive(void);
8191592Smarkmstatic	void	comment(void);
8291592Smarkmstatic	void	slashslashcomment(void);
8391592Smarkmstatic	int	string(void);
8491592Smarkmstatic	int	wcstrg(void);
8512099Sjoerg
8612099Sjoerg%}
8712099Sjoerg
88250227Sjkim%option nounput
89250227Sjkim
9012099SjoergL	[_A-Za-z]
9112099SjoergD	[0-9]
9212099SjoergNZD	[1-9]
9312099SjoergOD	[0-7]
9412099SjoergHD	[0-9A-Fa-f]
9512099SjoergEX	([eE][+-]?[0-9]+)
9612099Sjoerg
9712099Sjoerg%%
9812099Sjoerg
99280387Spfg{L}({L}|{D})*			return (name());
10012099Sjoerg0{OD}*[lLuU]*			return (icon(8));
10112099Sjoerg{NZD}{D}*[lLuU]*		return (icon(10));
10212099Sjoerg0[xX]{HD}+[lLuU]*		return (icon(16));
10312099Sjoerg{D}+\.{D}*{EX}?[fFlL]?		|
10412099Sjoerg{D}+{EX}[fFlL]?			|
105280387Spfg0[xX]{HD}+p{HD}+[fFlL]?		|
10612099Sjoerg\.{D}+{EX}?[fFlL]?		return (fcon());
10712099Sjoerg"="				return (operator(T_ASSIGN, ASSIGN));
10812099Sjoerg"*="				return (operator(T_OPASS, MULASS));
10912099Sjoerg"/="				return (operator(T_OPASS, DIVASS));
11012099Sjoerg"%="				return (operator(T_OPASS, MODASS));
11112099Sjoerg"+="				return (operator(T_OPASS, ADDASS));
11212099Sjoerg"-="				return (operator(T_OPASS, SUBASS));
11312099Sjoerg"<<="				return (operator(T_OPASS, SHLASS));
11412099Sjoerg">>="				return (operator(T_OPASS, SHRASS));
11512099Sjoerg"&="				return (operator(T_OPASS, ANDASS));
11612099Sjoerg"^="				return (operator(T_OPASS, XORASS));
11712099Sjoerg"|="				return (operator(T_OPASS, ORASS));
11812099Sjoerg"||"				return (operator(T_LOGOR, LOGOR));
11912099Sjoerg"&&"				return (operator(T_LOGAND, LOGAND));
12012099Sjoerg"|"				return (operator(T_OR, OR));
12112099Sjoerg"&"				return (operator(T_AND, AND));
12212099Sjoerg"^"				return (operator(T_XOR, XOR));
12312099Sjoerg"=="				return (operator(T_EQOP, EQ));
12412099Sjoerg"!="				return (operator(T_EQOP, NE));
12512099Sjoerg"<"				return (operator(T_RELOP, LT));
12612099Sjoerg">"				return (operator(T_RELOP, GT));
12712099Sjoerg"<="				return (operator(T_RELOP, LE));
12812099Sjoerg">="				return (operator(T_RELOP, GE));
12912099Sjoerg"<<"				return (operator(T_SHFTOP, SHL));
13012099Sjoerg">>"				return (operator(T_SHFTOP, SHR));
13112099Sjoerg"++"				return (operator(T_INCDEC, INC));
13212099Sjoerg"--"				return (operator(T_INCDEC, DEC));
13312099Sjoerg"->"				return (operator(T_STROP, ARROW));
13412099Sjoerg"."				return (operator(T_STROP, POINT));
13512099Sjoerg"+"				return (operator(T_ADDOP, PLUS));
13612099Sjoerg"-"				return (operator(T_ADDOP, MINUS));
13712099Sjoerg"*"				return (operator(T_MULT, MULT));
13812099Sjoerg"/"				return (operator(T_DIVOP, DIV));
13912099Sjoerg"%"				return (operator(T_DIVOP, MOD));
14012099Sjoerg"!"				return (operator(T_UNOP, NOT));
14112099Sjoerg"~"				return (operator(T_UNOP, COMPL));
14212099Sjoerg"\""				return (string());
14312099Sjoerg"L\""				return (wcstrg());
14412099Sjoerg";"				return (T_SEMI);
14512099Sjoerg"{"				return (T_LBRACE);
14612099Sjoerg"}"				return (T_RBRACE);
14712099Sjoerg","				return (T_COMMA);
14812099Sjoerg":"				return (T_COLON);
14912099Sjoerg"?"				return (T_QUEST);
15012099Sjoerg"["				return (T_LBRACK);
15112099Sjoerg"]"				return (T_RBRACK);
15212099Sjoerg"("				return (T_LPARN);
15312099Sjoerg")"				return (T_RPARN);
15412099Sjoerg"..."				return (T_ELLIPSE);
15512099Sjoerg"'"				return (ccon());
15612099Sjoerg"L'"				return (wccon());
15712099Sjoerg^#.*$				directive();
15812099Sjoerg\n				incline();
15912099Sjoerg\t|" "|\f|\v			;
16012099Sjoerg"/*"				comment();
16191592Smarkm"//"				slashslashcomment();
16212099Sjoerg.				badchar(yytext[0]);
16312099Sjoerg
16412099Sjoerg%%
16512099Sjoerg
16612099Sjoergstatic void
16791592Smarkmincline(void)
16812099Sjoerg{
16912099Sjoerg	curr_pos.p_line++;
17091592Smarkm	curr_pos.p_uniq = 0;
17191592Smarkm	if (curr_pos.p_file == csrc_pos.p_file) {
17212099Sjoerg		csrc_pos.p_line++;
17391592Smarkm		csrc_pos.p_uniq = 0;
17491592Smarkm	}
17512099Sjoerg}
17612099Sjoerg
17712099Sjoergstatic void
17891592Smarkmbadchar(int c)
17912099Sjoerg{
18091592Smarkm
18112099Sjoerg	/* unknown character \%o */
18212099Sjoerg	error(250, c);
18312099Sjoerg}
18412099Sjoerg
18512099Sjoerg/*
18612099Sjoerg * Keywords.
18712099Sjoerg * During initialisation they are written to the symbol table.
18812099Sjoerg */
18912099Sjoergstatic	struct	kwtab {
19012099Sjoerg	const	char *kw_name;	/* keyword */
19112099Sjoerg	int	kw_token;	/* token returned by yylex() */
19212099Sjoerg	scl_t	kw_scl;		/* storage class if kw_token T_SCLASS */
19312099Sjoerg	tspec_t	kw_tspec;	/* type spec. if kw_token T_TYPE or T_SOU */
19412099Sjoerg	tqual_t	kw_tqual;	/* type qual. fi kw_token T_QUAL */
195280387Spfg	u_int	kw_c89;		/* c89 keyword */
196280387Spfg	u_int	kw_c99;		/* c99 keyword */
197280387Spfg	u_int	kw_gcc;		/* GCC keyword */
19812099Sjoerg} kwtab[] = {
199280387Spfg	{ "asm",	T_ASM,		0,	0,	0,	  0, 0, 1 },
200280387Spfg	{ "__asm",	T_ASM,		0,	0,	0,	  0, 0, 0 },
201280387Spfg	{ "__asm__",	T_ASM,		0,	0,	0,	  0, 0, 0 },
202280387Spfg	{ "auto",	T_SCLASS,	AUTO,	0,	0,	  0, 0, 0 },
203280387Spfg	{ "break",	T_BREAK,	0,	0,	0,	  0, 0, 0 },
204280387Spfg	{ "case",	T_CASE,		0,	0,	0,	  0, 0, 0 },
205280387Spfg	{ "char",	T_TYPE,		0,	CHAR,	0,	  0, 0, 0 },
206280387Spfg	{ "const",	T_QUAL,		0,	0,	CONST,	  1, 0, 0 },
207280387Spfg	{ "__const__",	T_QUAL,		0,	0,	CONST,	  0, 0, 0 },
208280387Spfg	{ "__const",	T_QUAL,		0,	0,	CONST,	  0, 0, 0 },
209280387Spfg	{ "continue",	T_CONTINUE,	0,	0,	0,	  0, 0, 0 },
210280387Spfg	{ "default",	T_DEFAULT,	0,	0,	0,	  0, 0, 0 },
211280387Spfg	{ "do",		T_DO,		0,	0,	0,	  0, 0, 0 },
212280387Spfg	{ "double",	T_TYPE,		0,	DOUBLE,	0,	  0, 0, 0 },
213280387Spfg	{ "else",	T_ELSE,		0,	0,	0,	  0, 0, 0 },
214280387Spfg	{ "enum",	T_ENUM,		0,	0,	0,	  0, 0, 0 },
215280387Spfg	{ "extern",	T_SCLASS,	EXTERN,	0,	0,	  0, 0, 0 },
216280387Spfg	{ "float",	T_TYPE,		0,	FLOAT,	0,	  0, 0, 0 },
217280387Spfg	{ "for",	T_FOR,		0,	0,	0,	  0, 0, 0 },
218280387Spfg	{ "goto",	T_GOTO,		0,	0,	0,	  0, 0, 0 },
219280387Spfg	{ "if",		T_IF,		0,	0,	0,	  0, 0, 0 },
220280387Spfg	{ "inline",	T_SCLASS,	INLINE,	0,	0,	  0, 1, 0 },
221280387Spfg	{ "__inline__",	T_SCLASS,	INLINE,	0,	0,	  0, 0, 0 },
222280387Spfg	{ "__inline",	T_SCLASS,	INLINE,	0,	0,	  0, 0, 0 },
223280387Spfg	{ "int",	T_TYPE,		0,	INT,	0,	  0, 0, 0 },
224280387Spfg	{ "__symbolrename", T_SYMBOLRENAME, 0,	0,	0,	  0, 0, 0 },
225280387Spfg	{ "long",	T_TYPE,		0,	LONG,	0,	  0, 0, 0 },
226280387Spfg	{ "register",	T_SCLASS,	REG,	0,	0,	  0, 0, 0 },
227280387Spfg	{ "return",	T_RETURN,	0,	0,	0,	  0, 0, 0 },
228280387Spfg	{ "short",	T_TYPE,		0,	SHORT,	0,	  0, 0, 0 },
229280387Spfg	{ "signed",	T_TYPE,		0,	SIGNED,	0,	  1, 0, 0 },
230280387Spfg	{ "__signed__",	T_TYPE,		0,	SIGNED,	0,	  0, 0, 0 },
231280387Spfg	{ "__signed",	T_TYPE,		0,	SIGNED,	0,	  0, 0, 0 },
232280387Spfg	{ "sizeof",	T_SIZEOF,	0,	0,	0,	  0, 0, 0 },
233280387Spfg	{ "static",	T_SCLASS,	STATIC,	0,	0,	  0, 0, 0 },
234280387Spfg	{ "struct",	T_SOU,		0,	STRUCT,	0,	  0, 0, 0 },
235280387Spfg	{ "switch",	T_SWITCH,	0,	0,	0,	  0, 0, 0 },
236280387Spfg	{ "typedef",	T_SCLASS,	TYPEDEF, 0,	0,	  0, 0, 0 },
237280387Spfg	{ "union",	T_SOU,		0,	UNION,	0,	  0, 0, 0 },
238280387Spfg	{ "unsigned",	T_TYPE,		0,	UNSIGN,	0,	  0, 0, 0 },
239280387Spfg	{ "void",	T_TYPE,		0,	VOID,	0,	  0, 0, 0 },
240280387Spfg	{ "volatile",	T_QUAL,		0,	0,	VOLATILE, 1, 0, 0 },
241280387Spfg	{ "__volatile__", T_QUAL,	0,	0,	VOLATILE, 0, 0, 0 },
242280387Spfg	{ "__volatile",	T_QUAL,		0,	0,	VOLATILE, 0, 0, 0 },
243280387Spfg	{ "while",	T_WHILE,	0,	0,	0,	  0, 0, 0 },
244280387Spfg	{ NULL,		0,		0,	0,	0,	  0, 0, 0 }
24512099Sjoerg};
24612099Sjoerg
24712099Sjoerg/* Symbol table */
24812099Sjoergstatic	sym_t	*symtab[HSHSIZ1];
24912099Sjoerg
25012099Sjoerg/* bit i of the entry with index i is set */
25191592Smarkmuint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT];
25212099Sjoerg
25312099Sjoerg/* least significant i bits are set in the entry with index i */
25491592Smarkmuint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1];
25512099Sjoerg
25612099Sjoerg/* least significant i bits are not set in the entry with index i */
25791592Smarkmuint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1];
25812099Sjoerg
25912099Sjoerg/* free list for sbuf structures */
26012099Sjoergstatic	sbuf_t	 *sbfrlst;
26112099Sjoerg
262286614Spfg/* type of next expected symbol */
26312099Sjoergsymt_t	symtyp;
26412099Sjoerg
26512099Sjoerg
26612099Sjoerg/*
26712099Sjoerg * All keywords are written to the symbol table. This saves us looking
268108532Sschweikh * in an extra table for each name we found.
26912099Sjoerg */
27012099Sjoergvoid
27191592Smarkminitscan(void)
27212099Sjoerg{
27312099Sjoerg	struct	kwtab *kw;
27412099Sjoerg	sym_t	*sym;
27512099Sjoerg	int	h, i;
27691592Smarkm	uint64_t uq;
27712099Sjoerg
27812099Sjoerg	for (kw = kwtab; kw->kw_name != NULL; kw++) {
279280387Spfg		if ((kw->kw_c89 || kw->kw_c99) && tflag)
28012099Sjoerg			continue;
281280387Spfg		if (kw->kw_c99 && !(Sflag || gflag))
282280387Spfg			continue;
28312099Sjoerg		if (kw->kw_gcc && !gflag)
28412099Sjoerg			continue;
28512099Sjoerg		sym = getblk(sizeof (sym_t));
28612099Sjoerg		sym->s_name = kw->kw_name;
28712099Sjoerg		sym->s_keyw = 1;
28812099Sjoerg		sym->s_value.v_quad = kw->kw_token;
28912099Sjoerg		if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) {
29012099Sjoerg			sym->s_tspec = kw->kw_tspec;
29112099Sjoerg		} else if (kw->kw_token == T_SCLASS) {
29212099Sjoerg			sym->s_scl = kw->kw_scl;
29312099Sjoerg		} else if (kw->kw_token == T_QUAL) {
29412099Sjoerg			sym->s_tqual = kw->kw_tqual;
29512099Sjoerg		}
29612099Sjoerg		h = hash(sym->s_name);
29712099Sjoerg		if ((sym->s_link = symtab[h]) != NULL)
29812099Sjoerg			symtab[h]->s_rlink = &sym->s_link;
29912099Sjoerg		(symtab[h] = sym)->s_rlink = &symtab[h];
30012099Sjoerg	}
30112099Sjoerg
30212099Sjoerg	/* initialize bit-masks for quads */
30391592Smarkm	for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) {
30491592Smarkm		qbmasks[i] = (uint64_t)1 << i;
30591592Smarkm		uq = ~(uint64_t)0 << i;
30612099Sjoerg		qumasks[i] = uq;
30712099Sjoerg		qlmasks[i] = ~uq;
30812099Sjoerg	}
30912099Sjoerg	qumasks[i] = 0;
31091592Smarkm	qlmasks[i] = ~(uint64_t)0;
31112099Sjoerg}
31212099Sjoerg
31312099Sjoerg/*
31412099Sjoerg * Get a free sbuf structure, if possible from the free list
31512099Sjoerg */
31612099Sjoergstatic sbuf_t *
31791592Smarkmallocsb(void)
31812099Sjoerg{
31912099Sjoerg	sbuf_t	*sb;
32012099Sjoerg
32112099Sjoerg	if ((sb = sbfrlst) != NULL) {
32212099Sjoerg		sbfrlst = sb->sb_nxt;
32312099Sjoerg	} else {
32480284Sobrien		if ((sb = malloc(sizeof (sbuf_t))) == NULL)
32580284Sobrien			nomem();
32612099Sjoerg	}
327204872Sjh	(void)memset(sb, 0, sizeof (*sb));
32812099Sjoerg	return (sb);
32912099Sjoerg}
33012099Sjoerg
33112099Sjoerg/*
33212099Sjoerg * Put a sbuf structure to the free list
33312099Sjoerg */
33412099Sjoergstatic void
33591592Smarkmfreesb(sbuf_t *sb)
33612099Sjoerg{
33791592Smarkm
33812099Sjoerg	sb->sb_nxt = sbfrlst;
33912099Sjoerg	sbfrlst = sb;
34012099Sjoerg}
34112099Sjoerg
34212099Sjoerg/*
34312099Sjoerg * Read a character and ensure that it is positive (except EOF).
34412099Sjoerg * Increment line count(s) if necessary.
34512099Sjoerg */
34612099Sjoergstatic int
34791592Smarkminpc(void)
34812099Sjoerg{
34912099Sjoerg	int	c;
35012099Sjoerg
35112099Sjoerg	if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
35212099Sjoerg		incline();
35312099Sjoerg	return (c);
35412099Sjoerg}
35512099Sjoerg
35612099Sjoergstatic int
35791592Smarkmhash(const char *s)
35812099Sjoerg{
35912099Sjoerg	u_int	v;
36012099Sjoerg	const	u_char *us;
36112099Sjoerg
36212099Sjoerg	v = 0;
36312099Sjoerg	for (us = (const u_char *)s; *us != '\0'; us++) {
36412099Sjoerg		v = (v << sizeof (v)) + *us;
36512099Sjoerg		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
36612099Sjoerg	}
36712099Sjoerg	return (v % HSHSIZ1);
36812099Sjoerg}
36912099Sjoerg
37012099Sjoerg/*
37112099Sjoerg * Lex has found a letter followed by zero or more letters or digits.
37212099Sjoerg * It looks for a symbol in the symbol table with the same name. This
37312099Sjoerg * symbol must either be a keyword or a symbol of the type required by
37412099Sjoerg * symtyp (label, member, tag, ...).
37512099Sjoerg *
37612099Sjoerg * If it is a keyword, the token is returned. In some cases it is described
37712099Sjoerg * more deeply by data written to yylval.
37812099Sjoerg *
37912099Sjoerg * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
38012099Sjoerg * is stored in yylval. This struct contains the name of the symbol, it's
38112099Sjoerg * length and hash value. If there is already a symbol of the same name
38212099Sjoerg * and type in the symbol table, the sbuf struct also contains a pointer
38312099Sjoerg * to the symbol table entry.
38412099Sjoerg */
38512099Sjoergstatic int
38691592Smarkmname(void)
38712099Sjoerg{
38812099Sjoerg	char	*s;
38912099Sjoerg	sbuf_t	*sb;
39012099Sjoerg	sym_t	*sym;
39112099Sjoerg	int	tok;
39212099Sjoerg
39312099Sjoerg	sb = allocsb();
39412099Sjoerg	sb->sb_name = yytext;
39512099Sjoerg	sb->sb_len = yyleng;
39612099Sjoerg	sb->sb_hash = hash(yytext);
39712099Sjoerg
39812099Sjoerg	if ((sym = search(sb)) != NULL && sym->s_keyw) {
39912099Sjoerg		freesb(sb);
40012099Sjoerg		return (keyw(sym));
40112099Sjoerg	}
40212099Sjoerg
40312099Sjoerg	sb->sb_sym = sym;
40412099Sjoerg
40512099Sjoerg	if (sym != NULL) {
40612099Sjoerg		if (blklev < sym->s_blklev)
407280387Spfg			LERROR("name()");
40812099Sjoerg		sb->sb_name = sym->s_name;
40912099Sjoerg		sb->sb_len = strlen(sym->s_name);
41012099Sjoerg		tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
41112099Sjoerg	} else {
41212099Sjoerg		s = getblk(yyleng + 1);
41312099Sjoerg		(void)memcpy(s, yytext, yyleng + 1);
41412099Sjoerg		sb->sb_name = s;
41512099Sjoerg		sb->sb_len = yyleng;
41612099Sjoerg		tok = T_NAME;
41712099Sjoerg	}
41812099Sjoerg
41912099Sjoerg	yylval.y_sb = sb;
42012099Sjoerg	return (tok);
42112099Sjoerg}
42212099Sjoerg
42312099Sjoergstatic sym_t *
42491592Smarkmsearch(sbuf_t *sb)
42512099Sjoerg{
42612099Sjoerg	sym_t	*sym;
42712099Sjoerg
42812099Sjoerg	for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
42912099Sjoerg		if (strcmp(sym->s_name, sb->sb_name) == 0) {
43012099Sjoerg			if (sym->s_keyw || sym->s_kind == symtyp)
43112099Sjoerg				return (sym);
43212099Sjoerg		}
43312099Sjoerg	}
43412099Sjoerg
43512099Sjoerg	return (NULL);
43612099Sjoerg}
43791592Smarkm
43812099Sjoergstatic int
43991592Smarkmkeyw(sym_t *sym)
44012099Sjoerg{
44112099Sjoerg	int	t;
44212099Sjoerg
44312099Sjoerg	if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
44412099Sjoerg		yylval.y_scl = sym->s_scl;
44512099Sjoerg	} else if (t == T_TYPE || t == T_SOU) {
44612099Sjoerg		yylval.y_tspec = sym->s_tspec;
44712099Sjoerg	} else if (t == T_QUAL) {
44812099Sjoerg		yylval.y_tqual = sym->s_tqual;
44912099Sjoerg	}
45012099Sjoerg	return (t);
45112099Sjoerg}
45212099Sjoerg
45312099Sjoerg/*
45412099Sjoerg * Convert a string representing an integer into internal representation.
45512099Sjoerg * The value is returned in yylval. icon() (and yylex()) returns T_CON.
45612099Sjoerg */
45712099Sjoergstatic int
45891592Smarkmicon(int base)
45912099Sjoerg{
46012099Sjoerg	int	l_suffix, u_suffix;
46112099Sjoerg	int	len;
46212099Sjoerg	const	char *cp;
46312099Sjoerg	char	c, *eptr;
46412099Sjoerg	tspec_t	typ;
46591592Smarkm	u_long	ul = 0;
46691592Smarkm	uint64_t uq = 0;
46712099Sjoerg	int	ansiu;
46812099Sjoerg	static	tspec_t contypes[2][3] = {
46912099Sjoerg		{ INT,  LONG,  QUAD },
47012099Sjoerg		{ UINT, ULONG, UQUAD }
47112099Sjoerg	};
47212099Sjoerg
47312099Sjoerg	cp = yytext;
47412099Sjoerg	len = yyleng;
47512099Sjoerg
47612099Sjoerg	/* skip 0x */
47712099Sjoerg	if (base == 16) {
47812099Sjoerg		cp += 2;
47912099Sjoerg		len -= 2;
48012099Sjoerg	}
48112099Sjoerg
48212099Sjoerg	/* read suffixes */
48312099Sjoerg	l_suffix = u_suffix = 0;
48412099Sjoerg	for ( ; ; ) {
48512099Sjoerg		if ((c = cp[len - 1]) == 'l' || c == 'L') {
48612099Sjoerg			l_suffix++;
48712099Sjoerg		} else if (c == 'u' || c == 'U') {
48812099Sjoerg			u_suffix++;
48912099Sjoerg		} else {
49012099Sjoerg			break;
49112099Sjoerg		}
49212099Sjoerg		len--;
49312099Sjoerg	}
49412099Sjoerg	if (l_suffix > 2 || u_suffix > 1) {
49512099Sjoerg		/* malformed integer constant */
49612099Sjoerg		warning(251);
49712099Sjoerg		if (l_suffix > 2)
49812099Sjoerg			l_suffix = 2;
49912099Sjoerg		if (u_suffix > 1)
50012099Sjoerg			u_suffix = 1;
50112099Sjoerg	}
50212099Sjoerg	if (tflag && u_suffix != 0) {
50312099Sjoerg		/* suffix U is illegal in traditional C */
50412099Sjoerg		warning(97);
50512099Sjoerg	}
50612099Sjoerg	typ = contypes[u_suffix][l_suffix];
50712099Sjoerg
50812099Sjoerg	errno = 0;
50912099Sjoerg	if (l_suffix < 2) {
51012099Sjoerg		ul = strtoul(cp, &eptr, base);
51112099Sjoerg	} else {
51212099Sjoerg		uq = strtouq(cp, &eptr, base);
51312099Sjoerg	}
51412099Sjoerg	if (eptr != cp + len)
515280387Spfg		LERROR("icon()");
51612099Sjoerg	if (errno != 0)
51712099Sjoerg		/* integer constant out of range */
51812099Sjoerg		warning(252);
51912099Sjoerg
52012099Sjoerg	/*
521286614Spfg	 * If the value is too big for the current type, we must choose
52212099Sjoerg	 * another type.
52312099Sjoerg	 */
52412099Sjoerg	ansiu = 0;
52512099Sjoerg	switch (typ) {
52612099Sjoerg	case INT:
52712099Sjoerg		if (ul <= INT_MAX) {
52812099Sjoerg			/* ok */
52912099Sjoerg		} else if (ul <= (unsigned)UINT_MAX && base != 10) {
53012099Sjoerg			typ = UINT;
531280387Spfg#if INT_MAX != LONG_MAX
53212099Sjoerg		} else if (ul <= LONG_MAX) {
53312099Sjoerg			typ = LONG;
534280387Spfg#endif
53512099Sjoerg		} else {
53612099Sjoerg			typ = ULONG;
53712099Sjoerg		}
53812099Sjoerg		if (typ == UINT || typ == ULONG) {
53912099Sjoerg			if (tflag) {
54012099Sjoerg				typ = LONG;
54112099Sjoerg			} else if (!sflag) {
54212099Sjoerg				/*
54312099Sjoerg				 * Remember that the constant is unsigned
54412099Sjoerg				 * only in ANSI C
54512099Sjoerg				 */
54612099Sjoerg				ansiu = 1;
54712099Sjoerg			}
54812099Sjoerg		}
54912099Sjoerg		break;
55012099Sjoerg	case UINT:
55112099Sjoerg		if (ul > (u_int)UINT_MAX)
55212099Sjoerg			typ = ULONG;
55312099Sjoerg		break;
55412099Sjoerg	case LONG:
55512099Sjoerg		if (ul > LONG_MAX && !tflag) {
55612099Sjoerg			typ = ULONG;
55712099Sjoerg			if (!sflag)
55812099Sjoerg				ansiu = 1;
55912099Sjoerg		}
56012099Sjoerg		break;
56112099Sjoerg	case QUAD:
56212099Sjoerg		if (uq > QUAD_MAX && !tflag) {
56312099Sjoerg			typ = UQUAD;
56412099Sjoerg			if (!sflag)
56512099Sjoerg				ansiu = 1;
56612099Sjoerg		}
56712099Sjoerg		break;
56812099Sjoerg		/* LINTED (enumeration values not handled in switch) */
56991592Smarkm	case STRUCT:
57091592Smarkm	case VOID:
57191592Smarkm	case LDOUBLE:
57291592Smarkm	case FUNC:
57391592Smarkm	case ARRAY:
57491592Smarkm	case PTR:
57591592Smarkm	case ENUM:
57691592Smarkm	case UNION:
57791592Smarkm	case SIGNED:
57891592Smarkm	case NOTSPEC:
57991592Smarkm	case DOUBLE:
58091592Smarkm	case FLOAT:
58191592Smarkm	case UQUAD:
58291592Smarkm	case ULONG:
58391592Smarkm	case USHORT:
58491592Smarkm	case SHORT:
58591592Smarkm	case UCHAR:
58691592Smarkm	case SCHAR:
58791592Smarkm	case CHAR:
58891592Smarkm	case UNSIGN:
58991592Smarkm		break;
59012099Sjoerg	}
59112099Sjoerg
59212099Sjoerg	if (typ != QUAD && typ != UQUAD) {
59312099Sjoerg		if (isutyp(typ)) {
59412099Sjoerg			uq = ul;
59512099Sjoerg		} else {
59691592Smarkm			uq = (int64_t)(long)ul;
59712099Sjoerg		}
59812099Sjoerg	}
59912099Sjoerg
60091592Smarkm	uq = (uint64_t)xsign((int64_t)uq, typ, -1);
60112099Sjoerg
60280284Sobrien	if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL)
60380284Sobrien		nomem();
60480284Sobrien	yylval.y_val->v_tspec = typ;
60512099Sjoerg	yylval.y_val->v_ansiu = ansiu;
60691592Smarkm	yylval.y_val->v_quad = (int64_t)uq;
60712099Sjoerg
60812099Sjoerg	return (T_CON);
60912099Sjoerg}
61012099Sjoerg
61112099Sjoerg/*
61212099Sjoerg * Returns 1 if t is a signed type and the value is negative.
61312099Sjoerg *
61412099Sjoerg * len is the number of significant bits. If len is -1, len is set
61512099Sjoerg * to the width of type t.
61612099Sjoerg */
61712099Sjoergint
61891592Smarkmsign(int64_t q, tspec_t t, int len)
61912099Sjoerg{
62091592Smarkm
62112099Sjoerg	if (t == PTR || isutyp(t))
62212099Sjoerg		return (0);
62312099Sjoerg	return (msb(q, t, len));
62412099Sjoerg}
62512099Sjoerg
62612099Sjoergint
62791592Smarkmmsb(int64_t q, tspec_t t, int len)
62812099Sjoerg{
62991592Smarkm
63012099Sjoerg	if (len <= 0)
63112099Sjoerg		len = size(t);
63212099Sjoerg	return ((q & qbmasks[len - 1]) != 0);
63312099Sjoerg}
63412099Sjoerg
63512099Sjoerg/*
63612099Sjoerg * Extends the sign of q.
63712099Sjoerg */
63891592Smarkmint64_t
63991592Smarkmxsign(int64_t q, tspec_t t, int len)
64012099Sjoerg{
64191592Smarkm
64212099Sjoerg	if (len <= 0)
64312099Sjoerg		len = size(t);
64412099Sjoerg
64512099Sjoerg	if (t == PTR || isutyp(t) || !sign(q, t, len)) {
64612099Sjoerg		q &= qlmasks[len];
64712099Sjoerg	} else {
64812099Sjoerg		q |= qumasks[len];
64912099Sjoerg	}
65012099Sjoerg	return (q);
65112099Sjoerg}
65212099Sjoerg
65312099Sjoerg/*
65412099Sjoerg * Convert a string representing a floating point value into its interal
65512099Sjoerg * representation. Type and value are returned in yylval. fcon()
65612099Sjoerg * (and yylex()) returns T_CON.
65712099Sjoerg * XXX Currently it is not possible to convert constants of type
65891592Smarkm * long double which are greater than DBL_MAX.
65912099Sjoerg */
66012099Sjoergstatic int
66191592Smarkmfcon(void)
66212099Sjoerg{
66312099Sjoerg	const	char *cp;
66412099Sjoerg	int	len;
66512099Sjoerg	tspec_t typ;
66612099Sjoerg	char	c, *eptr;
66712099Sjoerg	double	d;
66891592Smarkm	float	f = 0;
66912099Sjoerg
67012099Sjoerg	cp = yytext;
67112099Sjoerg	len = yyleng;
67212099Sjoerg
67312099Sjoerg	if ((c = cp[len - 1]) == 'f' || c == 'F') {
67412099Sjoerg		typ = FLOAT;
67512099Sjoerg		len--;
67612099Sjoerg	} else if (c == 'l' || c == 'L') {
67712099Sjoerg		typ = LDOUBLE;
67812099Sjoerg		len--;
67912099Sjoerg	} else {
68012099Sjoerg		typ = DOUBLE;
68112099Sjoerg	}
68212099Sjoerg
68312099Sjoerg	if (tflag && typ != DOUBLE) {
68412099Sjoerg		/* suffixes F and L are illegal in traditional C */
68512099Sjoerg		warning(98);
68612099Sjoerg	}
68712099Sjoerg
68812099Sjoerg	errno = 0;
68912099Sjoerg	d = strtod(cp, &eptr);
690280387Spfg	if (eptr != cp + len) {
691280387Spfg		switch (*eptr) {
692280387Spfg		/*
693280387Spfg		 * XXX: non-native non-current strtod() may not handle hex
694280387Spfg		 * floats, ignore the rest if we find traces of hex float
695280387Spfg		 * syntax...
696280387Spfg		 */
697280387Spfg		case 'p':
698280387Spfg		case 'P':
699280387Spfg		case 'x':
700280387Spfg		case 'X':
701280387Spfg			d = 0;
702280387Spfg			errno = 0;
703280387Spfg			break;
704280387Spfg		default:
705280387Spfg			LERROR("fcon()");
706280387Spfg		}
707280387Spfg	}
70812099Sjoerg	if (errno != 0)
70912099Sjoerg		/* floating-point constant out of range */
71012099Sjoerg		warning(248);
71112099Sjoerg
71212099Sjoerg	if (typ == FLOAT) {
71312099Sjoerg		f = (float)d;
71491592Smarkm		if (!finite(f)) {
71512099Sjoerg			/* floating-point constant out of range */
71612099Sjoerg			warning(248);
71712099Sjoerg			f = f > 0 ? FLT_MAX : -FLT_MAX;
71812099Sjoerg		}
71912099Sjoerg	}
72012099Sjoerg
72180284Sobrien	if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
72280284Sobrien		nomem();
72380284Sobrien	yylval.y_val->v_tspec = typ;
72412099Sjoerg	if (typ == FLOAT) {
72512099Sjoerg		yylval.y_val->v_ldbl = f;
72612099Sjoerg	} else {
72712099Sjoerg		yylval.y_val->v_ldbl = d;
72812099Sjoerg	}
72912099Sjoerg
73012099Sjoerg	return (T_CON);
73112099Sjoerg}
73212099Sjoerg
73312099Sjoergstatic int
73491592Smarkmoperator(int t, op_t o)
73512099Sjoerg{
73691592Smarkm
73712099Sjoerg	yylval.y_op = o;
73812099Sjoerg	return (t);
73912099Sjoerg}
74012099Sjoerg
74112099Sjoerg/*
74212099Sjoerg * Called if lex found a leading \'.
74312099Sjoerg */
74412099Sjoergstatic int
74591592Smarkmccon(void)
74612099Sjoerg{
74712099Sjoerg	int	n, val, c;
74812099Sjoerg	char	cv;
74912099Sjoerg
75012099Sjoerg	n = 0;
75112099Sjoerg	val = 0;
75212099Sjoerg	while ((c = getescc('\'')) >= 0) {
75312099Sjoerg		val = (val << CHAR_BIT) + c;
75412099Sjoerg		n++;
75512099Sjoerg	}
75612099Sjoerg	if (c == -2) {
75712099Sjoerg		/* unterminated character constant */
75812099Sjoerg		error(253);
75912099Sjoerg	} else {
76012099Sjoerg		if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
76112099Sjoerg			/* too many characters in character constant */
76212099Sjoerg			error(71);
76312099Sjoerg		} else if (n > 1) {
76412099Sjoerg			/* multi-character character constant */
76512099Sjoerg			warning(294);
76612099Sjoerg		} else if (n == 0) {
76712099Sjoerg			/* empty character constant */
76812099Sjoerg			error(73);
76912099Sjoerg		}
77012099Sjoerg	}
77112099Sjoerg	if (n == 1) {
77212099Sjoerg		cv = (char)val;
77312099Sjoerg		val = cv;
77412099Sjoerg	}
77591592Smarkm
77691592Smarkm	yylval.y_val = xcalloc(1, sizeof (val_t));
77712099Sjoerg	yylval.y_val->v_tspec = INT;
77812099Sjoerg	yylval.y_val->v_quad = val;
77912099Sjoerg
78012099Sjoerg	return (T_CON);
78112099Sjoerg}
78212099Sjoerg
78312099Sjoerg/*
78412099Sjoerg * Called if lex found a leading L\'
78512099Sjoerg */
78612099Sjoergstatic int
78791592Smarkmwccon(void)
78812099Sjoerg{
78912099Sjoerg	static	char buf[MB_LEN_MAX + 1];
79012099Sjoerg	int	i, c;
79112099Sjoerg	wchar_t	wc;
79212099Sjoerg
79312099Sjoerg	i = 0;
79412099Sjoerg	while ((c = getescc('\'')) >= 0) {
79512099Sjoerg		if (i < MB_CUR_MAX)
79612099Sjoerg			buf[i] = (char)c;
79712099Sjoerg		i++;
79812099Sjoerg	}
79912099Sjoerg
80012099Sjoerg	wc = 0;
80112099Sjoerg
80212099Sjoerg	if (c == -2) {
80312099Sjoerg		/* unterminated character constant */
80412099Sjoerg		error(253);
80512099Sjoerg	} else if (c == 0) {
80612099Sjoerg		/* empty character constant */
80712099Sjoerg		error(73);
80812099Sjoerg	} else {
80912099Sjoerg		if (i > MB_CUR_MAX) {
81012099Sjoerg			i = MB_CUR_MAX;
81112099Sjoerg			/* too many characters in character constant */
81212099Sjoerg			error(71);
81312099Sjoerg		} else {
81412099Sjoerg			buf[i] = '\0';
81512099Sjoerg			(void)mbtowc(NULL, NULL, 0);
81612099Sjoerg			if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
81712099Sjoerg				/* invalid multibyte character */
81812099Sjoerg				error(291);
81912099Sjoerg		}
82012099Sjoerg	}
82112099Sjoerg
82280284Sobrien	if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
82380284Sobrien		nomem();
82412099Sjoerg	yylval.y_val->v_tspec = WCHAR;
82512099Sjoerg	yylval.y_val->v_quad = wc;
82612099Sjoerg
82712099Sjoerg	return (T_CON);
82812099Sjoerg}
82912099Sjoerg
83012099Sjoerg/*
83112099Sjoerg * Read a character which is part of a character constant or of a string
83212099Sjoerg * and handle escapes.
83312099Sjoerg *
83412099Sjoerg * The Argument is the character which delimits the character constant or
83512099Sjoerg * string.
83612099Sjoerg *
83712099Sjoerg * Returns -1 if the end of the character constant or string is reached,
83891592Smarkm * -2 if the EOF is reached, and the character otherwise.
83912099Sjoerg */
84012099Sjoergstatic int
84191592Smarkmgetescc(int d)
84212099Sjoerg{
84312099Sjoerg	static	int pbc = -1;
84412099Sjoerg	int	n, c, v;
84512099Sjoerg
84612099Sjoerg	if (pbc == -1) {
84712099Sjoerg		c = inpc();
84812099Sjoerg	} else {
84912099Sjoerg		c = pbc;
85012099Sjoerg		pbc = -1;
85112099Sjoerg	}
85212099Sjoerg	if (c == d)
85312099Sjoerg		return (-1);
85412099Sjoerg	switch (c) {
85512099Sjoerg	case '\n':
85691592Smarkm		if (tflag) {
85791592Smarkm			/* newline in string or char constant */
85891592Smarkm			error(254);
85991592Smarkm			return (-2);
86091592Smarkm		}
86191592Smarkm		return (c);
86212099Sjoerg	case EOF:
86312099Sjoerg		return (-2);
86412099Sjoerg	case '\\':
86512099Sjoerg		switch (c = inpc()) {
86612099Sjoerg		case '"':
86712099Sjoerg			if (tflag && d == '\'')
86812099Sjoerg				/* \" inside character constant undef. ... */
86912099Sjoerg				warning(262);
87012099Sjoerg			return ('"');
87112099Sjoerg		case '\'':
87212099Sjoerg			return ('\'');
87312099Sjoerg		case '?':
87412099Sjoerg			if (tflag)
87512099Sjoerg				/* \? undefined in traditional C */
87612099Sjoerg				warning(263);
87712099Sjoerg			return ('?');
87812099Sjoerg		case '\\':
87912099Sjoerg			return ('\\');
88012099Sjoerg		case 'a':
88112099Sjoerg			if (tflag)
88212099Sjoerg				/* \a undefined in traditional C */
88312099Sjoerg				warning(81);
88412099Sjoerg			return ('\a');
88512099Sjoerg		case 'b':
88612099Sjoerg			return ('\b');
88712099Sjoerg		case 'f':
88812099Sjoerg			return ('\f');
88912099Sjoerg		case 'n':
89012099Sjoerg			return ('\n');
89112099Sjoerg		case 'r':
89212099Sjoerg			return ('\r');
89312099Sjoerg		case 't':
89412099Sjoerg			return ('\t');
89512099Sjoerg		case 'v':
89612099Sjoerg			if (tflag)
89712099Sjoerg				/* \v undefined in traditional C */
89812099Sjoerg				warning(264);
89912099Sjoerg			return ('\v');
90012099Sjoerg		case '8': case '9':
90112099Sjoerg			/* bad octal digit %c */
90212099Sjoerg			warning(77, c);
90312099Sjoerg			/* FALLTHROUGH */
90412099Sjoerg		case '0': case '1': case '2': case '3':
90512099Sjoerg		case '4': case '5': case '6': case '7':
90612099Sjoerg			n = 3;
90712099Sjoerg			v = 0;
90812099Sjoerg			do {
90912099Sjoerg				v = (v << 3) + (c - '0');
91012099Sjoerg				c = inpc();
91112099Sjoerg			} while (--n && isdigit(c) && (tflag || c <= '7'));
91212099Sjoerg			if (tflag && n > 0 && isdigit(c))
91312099Sjoerg				/* bad octal digit %c */
91412099Sjoerg				warning(77, c);
91512099Sjoerg			pbc = c;
91612099Sjoerg			if (v > UCHAR_MAX) {
91712099Sjoerg				/* character escape does not fit in char. */
91812099Sjoerg				warning(76);
91912099Sjoerg				v &= CHAR_MASK;
92012099Sjoerg			}
92112099Sjoerg			return (v);
92212099Sjoerg		case 'x':
92312099Sjoerg			if (tflag)
92412099Sjoerg				/* \x undefined in traditional C */
92512099Sjoerg				warning(82);
92612099Sjoerg			v = 0;
92712099Sjoerg			n = 0;
92812099Sjoerg			while ((c = inpc()) >= 0 && isxdigit(c)) {
92912099Sjoerg				c = isdigit(c) ?
93012099Sjoerg					c - '0' : toupper(c) - 'A' + 10;
93112099Sjoerg				v = (v << 4) + c;
93212099Sjoerg				if (n >= 0) {
93312099Sjoerg					if ((v & ~CHAR_MASK) != 0) {
93412099Sjoerg						/* overflow in hex escape */
93512099Sjoerg						warning(75);
93612099Sjoerg						n = -1;
93712099Sjoerg					} else {
93812099Sjoerg						n++;
93912099Sjoerg					}
94012099Sjoerg				}
94112099Sjoerg			}
94212099Sjoerg			pbc = c;
94312099Sjoerg			if (n == 0) {
94412099Sjoerg				/* no hex digits follow \x */
94512099Sjoerg				error(74);
94612099Sjoerg			} if (n == -1) {
94712099Sjoerg				v &= CHAR_MASK;
94812099Sjoerg			}
94912099Sjoerg			return (v);
95012099Sjoerg		case '\n':
95112099Sjoerg			return (getescc(d));
95212099Sjoerg		case EOF:
95312099Sjoerg			return (-2);
95412099Sjoerg		default:
95512099Sjoerg			if (isprint(c)) {
95612099Sjoerg				/* dubious escape \%c */
95712099Sjoerg				warning(79, c);
95812099Sjoerg			} else {
95912099Sjoerg				/* dubious escape \%o */
96012099Sjoerg				warning(80, c);
96112099Sjoerg			}
96212099Sjoerg		}
96312099Sjoerg	}
96412099Sjoerg	return (c);
96512099Sjoerg}
96612099Sjoerg
96712099Sjoerg/*
96812099Sjoerg * Called for preprocessor directives. Currently implemented are:
96912099Sjoerg *	# lineno
97012099Sjoerg *	# lineno "filename"
97112099Sjoerg */
97212099Sjoergstatic void
97391592Smarkmdirective(void)
97412099Sjoerg{
97512099Sjoerg	const	char *cp, *fn;
97612099Sjoerg	char	c, *eptr;
97712099Sjoerg	size_t	fnl;
97812099Sjoerg	long	ln;
97912099Sjoerg	static	int first = 1;
98012099Sjoerg
98112099Sjoerg	/* Go to first non-whitespace after # */
98291592Smarkm	for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
98391592Smarkm		continue;
98412099Sjoerg
98591592Smarkm	if (!isdigit((unsigned char)c)) {
98612099Sjoerg	error:
98712099Sjoerg		/* undefined or invalid # directive */
98812099Sjoerg		warning(255);
98912099Sjoerg		return;
99012099Sjoerg	}
99112099Sjoerg	ln = strtol(--cp, &eptr, 10);
99212099Sjoerg	if (cp == eptr)
99312099Sjoerg		goto error;
99412099Sjoerg	if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
99512099Sjoerg		goto error;
99691592Smarkm	while ((c = *cp++) == ' ' || c == '\t')
99791592Smarkm		continue;
99812099Sjoerg	if (c != '\0') {
99912099Sjoerg		if (c != '"')
100012099Sjoerg			goto error;
100112099Sjoerg		fn = cp;
100212099Sjoerg		while ((c = *cp) != '"' && c != '\0')
100312099Sjoerg			cp++;
100412099Sjoerg		if (c != '"')
100512099Sjoerg			goto error;
100612099Sjoerg		if ((fnl = cp++ - fn) > PATH_MAX)
100712099Sjoerg			goto error;
100891592Smarkm		while ((c = *cp++) == ' ' || c == '\t')
100991592Smarkm			continue;
101012099Sjoerg#if 0
101112099Sjoerg		if (c != '\0')
101212099Sjoerg			warning("extra character(s) after directive");
101312099Sjoerg#endif
101491592Smarkm
101591592Smarkm		/* empty string means stdin */
101691592Smarkm		if (fnl == 0) {
101791592Smarkm			fn = "{standard input}";
101891592Smarkm			fnl = 16;			/* strlen (fn) */
101991592Smarkm		}
102012099Sjoerg		curr_pos.p_file = fnnalloc(fn, fnl);
102112099Sjoerg		/*
102212099Sjoerg		 * If this is the first directive, the name is the name
102312099Sjoerg		 * of the C source file as specified at the command line.
102412099Sjoerg		 * It is written to the output file.
102512099Sjoerg		 */
102612099Sjoerg		if (first) {
102712099Sjoerg			csrc_pos.p_file = curr_pos.p_file;
102812099Sjoerg			outsrc(curr_pos.p_file);
102912099Sjoerg			first = 0;
103012099Sjoerg		}
103112099Sjoerg	}
103212099Sjoerg	curr_pos.p_line = (int)ln - 1;
103391592Smarkm	curr_pos.p_uniq = 0;
103491592Smarkm	if (curr_pos.p_file == csrc_pos.p_file) {
103512099Sjoerg		csrc_pos.p_line = (int)ln - 1;
103691592Smarkm		csrc_pos.p_uniq = 0;
103791592Smarkm	}
103812099Sjoerg}
103912099Sjoerg
104012099Sjoerg/*
104112099Sjoerg * Handle lint comments. Following comments are currently understood:
104212099Sjoerg *	ARGSUSEDn
104391592Smarkm *	BITFIELDTYPE
104412099Sjoerg *	CONSTCOND CONSTANTCOND CONSTANTCONDITION
104512099Sjoerg *	FALLTHRU FALLTHROUGH
104612099Sjoerg *	LINTLIBRARY
104712099Sjoerg *	LINTED NOSTRICT
104812099Sjoerg *	LONGLONG
104912099Sjoerg *	NOTREACHED
105012099Sjoerg *	PRINTFLIKEn
105112099Sjoerg *	PROTOLIB
105212099Sjoerg *	SCANFLIKEn
105312099Sjoerg *	VARARGSn
105412099Sjoerg * If one of this comments is recognized, the arguments, if any, are
105512099Sjoerg * parsed and a function which handles this comment is called.
105612099Sjoerg */
105712099Sjoergstatic void
105891592Smarkmcomment(void)
105912099Sjoerg{
106012099Sjoerg	int	c, lc;
106112099Sjoerg	static struct {
106212099Sjoerg		const	char *keywd;
106312099Sjoerg		int	arg;
106491592Smarkm		void	(*func)(int);
106512099Sjoerg	} keywtab[] = {
106612099Sjoerg		{ "ARGSUSED",		1,	argsused	},
106791592Smarkm		{ "BITFIELDTYPE",	0,	bitfieldtype	},
106812099Sjoerg		{ "CONSTCOND",		0,	constcond	},
106912099Sjoerg		{ "CONSTANTCOND",	0,	constcond	},
107012099Sjoerg		{ "CONSTANTCONDITION",	0,	constcond	},
107112099Sjoerg		{ "FALLTHRU",		0,	fallthru	},
107212099Sjoerg		{ "FALLTHROUGH",	0,	fallthru	},
107312099Sjoerg		{ "LINTLIBRARY",	0,	lintlib		},
107412099Sjoerg		{ "LINTED",		0,	linted		},
107512099Sjoerg		{ "LONGLONG",		0,	longlong	},
107612099Sjoerg		{ "NOSTRICT",		0,	linted		},
107712099Sjoerg		{ "NOTREACHED",		0,	notreach	},
107812099Sjoerg		{ "PRINTFLIKE",		1,	printflike	},
107912099Sjoerg		{ "PROTOLIB",		1,	protolib	},
108012099Sjoerg		{ "SCANFLIKE",		1,	scanflike	},
108112099Sjoerg		{ "VARARGS",		1,	varargs		},
108212099Sjoerg	};
108312099Sjoerg	char	keywd[32];
108412099Sjoerg	char	arg[32];
108512099Sjoerg	int	l, i, a;
108612099Sjoerg	int	eoc;
108712099Sjoerg
108812099Sjoerg	eoc = 0;
108912099Sjoerg
109012099Sjoerg	/* Skip white spaces after the start of the comment */
109191592Smarkm	while ((c = inpc()) != EOF && isspace(c))
109291592Smarkm		continue;
109312099Sjoerg
109412099Sjoerg	/* Read the potential keyword to keywd */
109512099Sjoerg	l = 0;
109612099Sjoerg	while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
109712099Sjoerg		keywd[l++] = (char)c;
109812099Sjoerg		c = inpc();
109912099Sjoerg	}
110012099Sjoerg	keywd[l] = '\0';
110112099Sjoerg
110212099Sjoerg	/* look for the keyword */
110312099Sjoerg	for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
110412099Sjoerg		if (strcmp(keywtab[i].keywd, keywd) == 0)
110512099Sjoerg			break;
110612099Sjoerg	}
110712099Sjoerg	if (i == sizeof (keywtab) / sizeof (keywtab[0]))
110812099Sjoerg		goto skip_rest;
110912099Sjoerg
111012099Sjoerg	/* skip white spaces after the keyword */
111112099Sjoerg	while (c != EOF && isspace(c))
111212099Sjoerg		c = inpc();
111312099Sjoerg
111412099Sjoerg	/* read the argument, if the keyword accepts one and there is one */
111512099Sjoerg	l = 0;
111612099Sjoerg	if (keywtab[i].arg) {
111712099Sjoerg		while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
111812099Sjoerg			arg[l++] = (char)c;
111912099Sjoerg			c = inpc();
112012099Sjoerg		}
112112099Sjoerg	}
112212099Sjoerg	arg[l] = '\0';
112312099Sjoerg	a = l != 0 ? atoi(arg) : -1;
112412099Sjoerg
112512099Sjoerg	/* skip white spaces after the argument */
112612099Sjoerg	while (c != EOF && isspace(c))
112712099Sjoerg		c = inpc();
112812099Sjoerg
112912099Sjoerg	if (c != '*' || (c = inpc()) != '/') {
113012099Sjoerg		if (keywtab[i].func != linted)
113112099Sjoerg			/* extra characters in lint comment */
113212099Sjoerg			warning(257);
113312099Sjoerg	} else {
113412099Sjoerg		/*
113512099Sjoerg		 * remember that we have already found the end of the
113612099Sjoerg		 * comment
113712099Sjoerg		 */
113812099Sjoerg		eoc = 1;
113912099Sjoerg	}
114012099Sjoerg
114112099Sjoerg	if (keywtab[i].func != NULL)
114212099Sjoerg		(*keywtab[i].func)(a);
114312099Sjoerg
114412099Sjoerg skip_rest:
114512099Sjoerg	while (!eoc) {
114612099Sjoerg		lc = c;
114712099Sjoerg		if ((c = inpc()) == EOF) {
114812099Sjoerg			/* unterminated comment */
114912099Sjoerg			error(256);
115012099Sjoerg			break;
115112099Sjoerg		}
115212099Sjoerg		if (lc == '*' && c == '/')
115312099Sjoerg			eoc = 1;
115412099Sjoerg	}
115512099Sjoerg}
115612099Sjoerg
115712099Sjoerg/*
115891592Smarkm * Handle // style comments
115991592Smarkm */
116091592Smarkmstatic void
116191592Smarkmslashslashcomment(void)
116291592Smarkm{
116391592Smarkm	int c;
116491592Smarkm
1165280387Spfg	if (!Sflag && !gflag)
116691592Smarkm		/* // comments only supported in C99 */
116791592Smarkm		(void)gnuism(312, tflag ? "traditional" : "ANSI");
116891592Smarkm
116991592Smarkm	while ((c = inpc()) != EOF && c != '\n')
117091592Smarkm		continue;
117191592Smarkm}
117291592Smarkm
117391592Smarkm/*
117412099Sjoerg * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
117512099Sjoerg * clrwflgs() is called after function definitions and global and
117612099Sjoerg * local declarations and definitions. It is also called between
117712099Sjoerg * the controlling expression and the body of control statements
117812099Sjoerg * (if, switch, for, while).
117912099Sjoerg */
118012099Sjoergvoid
118191592Smarkmclrwflgs(void)
118212099Sjoerg{
118391592Smarkm
118412099Sjoerg	nowarn = 0;
118512099Sjoerg	quadflg = 0;
118612099Sjoerg	ccflg = 0;
118712099Sjoerg}
118812099Sjoerg
118912099Sjoerg/*
119012099Sjoerg * Strings are stored in a dynamically alloceted buffer and passed
119112099Sjoerg * in yylval.y_xstrg to the parser. The parser or the routines called
119212099Sjoerg * by the parser are responsible for freeing this buffer.
119312099Sjoerg */
119412099Sjoergstatic int
119591592Smarkmstring(void)
119612099Sjoerg{
119712099Sjoerg	u_char	*s;
119812099Sjoerg	int	c;
119912099Sjoerg	size_t	len, max;
120012099Sjoerg	strg_t	*strg;
120112099Sjoerg
120280284Sobrien	if ((s = malloc(max = 64)) == NULL)
120380284Sobrien		nomem();
120412099Sjoerg
120512099Sjoerg	len = 0;
120612099Sjoerg	while ((c = getescc('"')) >= 0) {
120712099Sjoerg		/* +1 to reserve space for a trailing NUL character */
120812099Sjoerg		if (len + 1 == max)
120980284Sobrien			if ((s = realloc(s, max *= 2)) == NULL)
121080284Sobrien				nomem();
121112099Sjoerg		s[len++] = (char)c;
121212099Sjoerg	}
121312099Sjoerg	s[len] = '\0';
121412099Sjoerg	if (c == -2)
121512099Sjoerg		/* unterminated string constant */
121612099Sjoerg		error(258);
121712099Sjoerg
121880284Sobrien	if ((strg = calloc(1, sizeof (strg_t))) == NULL)
121980284Sobrien		nomem();
122012099Sjoerg	strg->st_tspec = CHAR;
122112099Sjoerg	strg->st_len = len;
122212099Sjoerg	strg->st_cp = s;
122312099Sjoerg
122412099Sjoerg	yylval.y_strg = strg;
122512099Sjoerg	return (T_STRING);
122612099Sjoerg}
122712099Sjoerg
122812099Sjoergstatic int
122991592Smarkmwcstrg(void)
123012099Sjoerg{
123112099Sjoerg	char	*s;
123212099Sjoerg	int	c, i, n, wi;
123312099Sjoerg	size_t	len, max, wlen;
123412099Sjoerg	wchar_t	*ws;
123512099Sjoerg	strg_t	*strg;
123612099Sjoerg
123780284Sobrien	if ((s = malloc(max = 64)) == NULL)
123880284Sobrien		nomem();
123912099Sjoerg	len = 0;
124012099Sjoerg	while ((c = getescc('"')) >= 0) {
124112099Sjoerg		/* +1 to save space for a trailing NUL character */
124212099Sjoerg		if (len + 1 >= max)
124380284Sobrien			if ((s = realloc(s, max *= 2)) == NULL)
124480284Sobrien				nomem();
124512099Sjoerg		s[len++] = (char)c;
124612099Sjoerg	}
124712099Sjoerg	s[len] = '\0';
124812099Sjoerg	if (c == -2)
124912099Sjoerg		/* unterminated string constant */
125012099Sjoerg		error(258);
125112099Sjoerg
125212099Sjoerg	/* get length of wide character string */
125312099Sjoerg	(void)mblen(NULL, 0);
125412099Sjoerg	for (i = 0, wlen = 0; i < len; i += n, wlen++) {
125512099Sjoerg		if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
125612099Sjoerg			/* invalid multibyte character */
125712099Sjoerg			error(291);
125812099Sjoerg			break;
125912099Sjoerg		}
126012099Sjoerg		if (n == 0)
126112099Sjoerg			n = 1;
126212099Sjoerg	}
126312099Sjoerg
126480284Sobrien	if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL)
126580284Sobrien		nomem();
126612099Sjoerg
126712099Sjoerg	/* convert from multibyte to wide char */
126812099Sjoerg	(void)mbtowc(NULL, NULL, 0);
126912099Sjoerg	for (i = 0, wi = 0; i < len; i += n, wi++) {
127012099Sjoerg		if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
127112099Sjoerg			break;
127212099Sjoerg		if (n == 0)
127312099Sjoerg			n = 1;
127412099Sjoerg	}
127512099Sjoerg	ws[wi] = 0;
127612099Sjoerg	free(s);
127712099Sjoerg
127880284Sobrien	if ((strg = calloc(1, sizeof (strg_t))) == NULL)
127980284Sobrien		nomem();
128012099Sjoerg	strg->st_tspec = WCHAR;
128112099Sjoerg	strg->st_len = wlen;
128212099Sjoerg	strg->st_wcp = ws;
128312099Sjoerg
128412099Sjoerg	yylval.y_strg = strg;
128512099Sjoerg	return (T_STRING);
128612099Sjoerg}
128712099Sjoerg
128812099Sjoerg/*
128912099Sjoerg * As noted above the scanner does not create new symbol table entries
129012099Sjoerg * for symbols it cannot find in the symbol table. This is to avoid
129112099Sjoerg * putting undeclared symbols into the symbol table if a syntax error
129212099Sjoerg * occurs.
129312099Sjoerg *
129412099Sjoerg * getsym() is called as soon as it is probably ok to put the symbol to
129512099Sjoerg * the symbol table. This does not mean that it is not possible that
129612099Sjoerg * symbols are put to the symbol table which are than not completely
129712099Sjoerg * declared due to syntax errors. To avoid too many problems in this
129812099Sjoerg * case symbols get type int in getsym().
129912099Sjoerg *
130012099Sjoerg * XXX calls to getsym() should be delayed until decl1*() is called
130112099Sjoerg */
130212099Sjoergsym_t *
130391592Smarkmgetsym(sbuf_t *sb)
130412099Sjoerg{
130512099Sjoerg	dinfo_t	*di;
130612099Sjoerg	char	*s;
130712099Sjoerg	sym_t	*sym;
130812099Sjoerg
130912099Sjoerg	sym = sb->sb_sym;
131012099Sjoerg
131112099Sjoerg	/*
131212099Sjoerg	 * During member declaration it is possible that name() looked
131312099Sjoerg	 * for symbols of type FVFT, although it should have looked for
131412099Sjoerg	 * symbols of type FTAG. Same can happen for labels. Both cases
131512099Sjoerg	 * are compensated here.
131612099Sjoerg	 */
131712099Sjoerg	if (symtyp == FMOS || symtyp == FLAB) {
131812099Sjoerg		if (sym == NULL || sym->s_kind == FVFT)
131912099Sjoerg			sym = search(sb);
132012099Sjoerg	}
132112099Sjoerg
132212099Sjoerg	if (sym != NULL) {
132312099Sjoerg		if (sym->s_kind != symtyp)
1324280387Spfg			LERROR("storesym()");
132512099Sjoerg		symtyp = FVFT;
132612099Sjoerg		freesb(sb);
132712099Sjoerg		return (sym);
132812099Sjoerg	}
132912099Sjoerg
133012099Sjoerg	/* create a new symbol table entry */
133112099Sjoerg
133212099Sjoerg	/* labels must always be allocated at level 1 (outhermost block) */
133312099Sjoerg	if (symtyp == FLAB) {
133412099Sjoerg		sym = getlblk(1, sizeof (sym_t));
133512099Sjoerg		s = getlblk(1, sb->sb_len + 1);
133612099Sjoerg		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
133712099Sjoerg		sym->s_name = s;
133812099Sjoerg		sym->s_blklev = 1;
133912099Sjoerg		di = dcs;
134012099Sjoerg		while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
134112099Sjoerg			di = di->d_nxt;
134212099Sjoerg		if (di->d_ctx != AUTO)
1343280387Spfg			LERROR("storesym()");
134412099Sjoerg	} else {
134512099Sjoerg		sym = getblk(sizeof (sym_t));
134612099Sjoerg		sym->s_name = sb->sb_name;
134712099Sjoerg		sym->s_blklev = blklev;
134812099Sjoerg		di = dcs;
134912099Sjoerg	}
135012099Sjoerg
135191592Smarkm	UNIQUE_CURR_POS(sym->s_dpos);
135212099Sjoerg	if ((sym->s_kind = symtyp) != FLAB)
135312099Sjoerg		sym->s_type = gettyp(INT);
135412099Sjoerg
135512099Sjoerg	symtyp = FVFT;
135612099Sjoerg
135712099Sjoerg	if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
135812099Sjoerg		symtab[sb->sb_hash]->s_rlink = &sym->s_link;
135912099Sjoerg	(symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
136012099Sjoerg
136112099Sjoerg	*di->d_ldlsym = sym;
136212099Sjoerg	di->d_ldlsym = &sym->s_dlnxt;
136312099Sjoerg
136412099Sjoerg	freesb(sb);
136512099Sjoerg	return (sym);
136612099Sjoerg}
136712099Sjoerg
136812099Sjoerg/*
1369280387Spfg * Construct a temporary symbol. The symbol starts with a digit, so that
1370280387Spfg * it is illegal.
1371280387Spfg */
1372280387Spfgsym_t *
1373280387Spfgmktempsym(type_t *t)
1374280387Spfg{
1375280387Spfg	static int n = 0;
1376280387Spfg	int h;
1377280387Spfg	char *s = getlblk(blklev, 64);
1378280387Spfg	sym_t *sym = getblk(sizeof (sym_t));
1379280387Spfg
1380280387Spfg	(void)snprintf(s, 64, "%.8d_tmp", n++);
1381280387Spfg	h = hash(s);
1382280387Spfg
1383280387Spfg	sym->s_name = s;
1384280387Spfg	sym->s_type = t;
1385280387Spfg	sym->s_blklev = blklev;
1386280387Spfg	sym->s_scl = AUTO;
1387280387Spfg	sym->s_kind = FVFT;
1388280387Spfg	sym->s_used = 1;
1389280387Spfg	sym->s_set = 1;
1390280387Spfg
1391280387Spfg	if ((sym->s_link = symtab[h]) != NULL)
1392280387Spfg		symtab[h]->s_rlink = &sym->s_link;
1393280387Spfg	(symtab[h] = sym)->s_rlink = &symtab[h];
1394280387Spfg
1395280387Spfg	*dcs->d_ldlsym = sym;
1396280387Spfg	dcs->d_ldlsym = &sym->s_dlnxt;
1397280387Spfg
1398280387Spfg	return sym;
1399280387Spfg}
1400280387Spfg
1401280387Spfg/*
140212099Sjoerg * Remove a symbol forever from the symbol table. s_blklev
140312099Sjoerg * is set to -1 to avoid that the symbol will later be put
140412099Sjoerg * back to the symbol table.
140512099Sjoerg */
140612099Sjoergvoid
140791592Smarkmrmsym(sym_t *sym)
140812099Sjoerg{
140991592Smarkm
141012099Sjoerg	if ((*sym->s_rlink = sym->s_link) != NULL)
141112099Sjoerg		sym->s_link->s_rlink = sym->s_rlink;
141212099Sjoerg	sym->s_blklev = -1;
141312099Sjoerg	sym->s_link = NULL;
141412099Sjoerg}
141512099Sjoerg
141612099Sjoerg/*
141712099Sjoerg * Remove a list of symbols declared at one level from the symbol
141812099Sjoerg * table.
141912099Sjoerg */
142012099Sjoergvoid
142191592Smarkmrmsyms(sym_t *syms)
142212099Sjoerg{
142312099Sjoerg	sym_t	*sym;
142412099Sjoerg
142512099Sjoerg	for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
142612099Sjoerg		if (sym->s_blklev != -1) {
142712099Sjoerg			if ((*sym->s_rlink = sym->s_link) != NULL)
142812099Sjoerg				sym->s_link->s_rlink = sym->s_rlink;
142912099Sjoerg			sym->s_link = NULL;
143012099Sjoerg			sym->s_rlink = NULL;
143112099Sjoerg		}
143212099Sjoerg	}
143312099Sjoerg}
143412099Sjoerg
143512099Sjoerg/*
143612099Sjoerg * Put a symbol into the symbol table
143712099Sjoerg */
143812099Sjoergvoid
143991592Smarkminssym(int bl, sym_t *sym)
144012099Sjoerg{
144112099Sjoerg	int	h;
144212099Sjoerg
144312099Sjoerg	h = hash(sym->s_name);
144412099Sjoerg	if ((sym->s_link = symtab[h]) != NULL)
144512099Sjoerg		symtab[h]->s_rlink = &sym->s_link;
144612099Sjoerg	(symtab[h] = sym)->s_rlink = &symtab[h];
144712099Sjoerg	sym->s_blklev = bl;
144812099Sjoerg	if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1449280387Spfg		LERROR("inssym()");
145012099Sjoerg}
145112099Sjoerg
145212099Sjoerg/*
145312099Sjoerg * Called at level 0 after syntax errors
145412099Sjoerg * Removes all symbols which are not declared at level 0 from the
145512099Sjoerg * symbol table. Also frees all memory which is not associated with
145612099Sjoerg * level 0.
145712099Sjoerg */
145812099Sjoergvoid
145991592Smarkmcleanup(void)
146012099Sjoerg{
146112099Sjoerg	sym_t	*sym, *nsym;
146212099Sjoerg	int	i;
146312099Sjoerg
146412099Sjoerg	for (i = 0; i < HSHSIZ1; i++) {
146512099Sjoerg		for (sym = symtab[i]; sym != NULL; sym = nsym) {
146612099Sjoerg			nsym = sym->s_link;
146712099Sjoerg			if (sym->s_blklev >= 1) {
146812099Sjoerg				if ((*sym->s_rlink = nsym) != NULL)
146912099Sjoerg					nsym->s_rlink = sym->s_rlink;
147012099Sjoerg			}
147112099Sjoerg		}
147212099Sjoerg	}
147312099Sjoerg
147412099Sjoerg	for (i = mblklev; i > 0; i--)
147512099Sjoerg		freelblk(i);
147612099Sjoerg}
147712099Sjoerg
147812099Sjoerg/*
147912099Sjoerg * Create a new symbol with the name of an existing symbol.
148012099Sjoerg */
148112099Sjoergsym_t *
148291592Smarkmpushdown(sym_t *sym)
148312099Sjoerg{
148412099Sjoerg	int	h;
148512099Sjoerg	sym_t	*nsym;
148612099Sjoerg
148712099Sjoerg	h = hash(sym->s_name);
148812099Sjoerg	nsym = getblk(sizeof (sym_t));
148912099Sjoerg	if (sym->s_blklev > blklev)
1490280387Spfg		LERROR("pushdown()");
149112099Sjoerg	nsym->s_name = sym->s_name;
149291592Smarkm	UNIQUE_CURR_POS(nsym->s_dpos);
149312099Sjoerg	nsym->s_kind = sym->s_kind;
149412099Sjoerg	nsym->s_blklev = blklev;
149512099Sjoerg
149612099Sjoerg	if ((nsym->s_link = symtab[h]) != NULL)
149712099Sjoerg		symtab[h]->s_rlink = &nsym->s_link;
149812099Sjoerg	(symtab[h] = nsym)->s_rlink = &symtab[h];
149912099Sjoerg
150012099Sjoerg	*dcs->d_ldlsym = nsym;
150112099Sjoerg	dcs->d_ldlsym = &nsym->s_dlnxt;
150212099Sjoerg
150312099Sjoerg	return (nsym);
150412099Sjoerg}
150512099Sjoerg
150612099Sjoerg/*
150712099Sjoerg * Free any dynamically allocated memory referenced by
150812099Sjoerg * the value stack or yylval.
150912099Sjoerg * The type of information in yylval is described by tok.
151012099Sjoerg */
151112099Sjoergvoid
151291592Smarkmfreeyyv(void *sp, int tok)
151312099Sjoerg{
151412099Sjoerg	if (tok == T_NAME || tok == T_TYPENAME) {
151512099Sjoerg		sbuf_t *sb = *(sbuf_t **)sp;
151612099Sjoerg		freesb(sb);
151712099Sjoerg	} else if (tok == T_CON) {
151812099Sjoerg		val_t *val = *(val_t **)sp;
151912099Sjoerg		free(val);
152012099Sjoerg	} else if (tok == T_STRING) {
152112099Sjoerg		strg_t *strg = *(strg_t **)sp;
152212099Sjoerg		if (strg->st_tspec == CHAR) {
152312099Sjoerg			free(strg->st_cp);
152412099Sjoerg		} else if (strg->st_tspec == WCHAR) {
152512099Sjoerg			free(strg->st_wcp);
152612099Sjoerg		} else {
1527280387Spfg			LERROR("fryylv()");
152812099Sjoerg		}
152912099Sjoerg		free(strg);
153091592Smarkm	}
153112099Sjoerg}
1532