scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit bae05fff3765490d73e5e69945d2ff9a5ffdb2ee
parent 951024c4481a3f63bfab9a83364d6861f16ee5d9
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 11 Mar 2014 17:36:05 +0100

Change type qualfier codification

This form of codificate storage makes trivial all the checks in
declarations.

Diffstat:
Mdecl.c | 41+++++++++++++++++------------------------
Mlex.c | 2+-
Msymbol.h | 9+--------
Mtokens.h | 19++++++++++++-------
Mtypes.c | 31-------------------------------
5 files changed, 31 insertions(+), 71 deletions(-)

diff --git a/decl.c b/decl.c @@ -173,15 +173,18 @@ enumdcl(struct ctype *base) } static bool -specifier(register struct ctype *tp, - char *store, struct qualifier *qlf) +specifier(register struct ctype *tp, char *store, char *qlf) { unsigned char tok; for (;; next()) { switch (yytoken) { case TQUALIFIER: - qlf = qualifier(qlf, yyval->c); + if (*qlf && !options.repeat) + error("duplicated '%s'", yytext); + if (yyval->c == RESTRICT) + error("invalid use of restrict"); + *qlf |= yyval->c; break; case STORAGE: if (*store) @@ -211,8 +214,7 @@ specifier(register struct ctype *tp, check_type: if (!tp->defined) { - if (*store && - !qlf->defined && + if (*store && *qlf && curctx != CTX_OUTER && nested_tags == 0) { return false; @@ -245,16 +247,12 @@ declarator(struct ctype *tp, unsigned char ns, unsigned char isfun) if (yytoken == '*') { for (bp = qlf; n < NR_DECLARATORS ; ++n) { - switch (yytoken) { - case '*': - yytoken = PTR; - case CONST: case VOLATILE: case RESTRICT: - *bp++ = yytoken; - next(); - continue; - default: + if (yytoken == '*') + *bp++ = PTR; + else if (yytoken == TQUALIFIER) + *bp++ = yyval->c; + else goto direct; - } } error("Too much type declarators"); } @@ -288,8 +286,7 @@ initializer(register struct ctype *tp) static struct node * listdcl(struct ctype *base, - char store, - struct qualifier *qlf, + char store, char qlf, unsigned char ns, unsigned char isfun) { struct compound c; @@ -304,7 +301,7 @@ listdcl(struct ctype *base, sym = declarator(base, ns, isfun); sym->store = store; - sym->qlf = *qlf; + sym->qlf = qlf; sym->ctype = *decl_type(base); if (sym->store) { sym->tok = TYPE; @@ -365,11 +362,9 @@ struct node * decl(unsigned char ns) { struct ctype base; - char store = 0; - struct qualifier qlf; + char store = 0, qlf = 0; initctype(&base); - initqlf(&qlf); if (!specifier(&base, &store, &qlf)) return NULL; @@ -382,18 +377,16 @@ decl(unsigned char ns) if (yytoken == ';') return NULL; default: - return listdcl(&base, store, &qlf, ns, 0); + return listdcl(&base, store, qlf, ns, 0); } } void type_name(struct ctype *tp) { - struct qualifier qlf; - char store = 0; + char store = 0, qlf = 0; initctype(tp); - initqlf(&qlf); if (!specifier(tp, &store, &qlf)) return; diff --git a/lex.c b/lex.c @@ -141,7 +141,7 @@ init_keywords(void) {"_Imaginary", TYPE, IMAGINARY}, {"long", TYPE, LONG}, {"register", STORAGE, REGISTER}, - {"restricted", RESTRICT, RESTRICT}, + {"restrict", TQUALIFIER, RESTRICT}, {"return", RETURN, RETURN}, {"short", TYPE, SHORT}, {"signed", TYPE, SIGNED}, diff --git a/symbol.h b/symbol.h @@ -17,11 +17,6 @@ enum { NS_TAG }; -struct qualifier { - bool c_const : 1; - bool c_volatile : 1; - bool defined: 1; -}; struct ctype { unsigned type : 5; @@ -45,7 +40,7 @@ struct ctype { struct symbol { struct ctype ctype; char store; - struct qualifier qlf; + char qlf; unsigned char ctx; unsigned char ns; char *name; @@ -73,11 +68,9 @@ extern void del_ctx(void); extern void freesyms(void); extern struct symbol *lookup(const char *s, signed char ns); extern void insert(struct symbol *sym, unsigned char ctx); -extern struct qualifier *qualifier(register struct qualifier *, unsigned char); extern void delctype(struct ctype *tp); extern unsigned char hash(register const char *s); extern struct ctype *initctype(register struct ctype *tp); -extern struct qualifier * initqlf(struct qualifier *qlf); #ifndef NDEBUG extern void ptype(register struct ctype *t); diff --git a/tokens.h b/tokens.h @@ -13,7 +13,7 @@ enum tokens { LDOUBLE, STRUCT, UNION, ENUM, BOOL, ARY, PTR, FTN, COMPLEX, IMAGINARY, BITFLD, TYPENAME, TYPE, /* type qualifier */ - VOLATILE, CONST, RESTRICT, TQUALIFIER, + TQUALIFIER, /* sign specifier */ UNSIGNED, SIGNED, /* storage specifier */ @@ -29,12 +29,17 @@ enum tokens { CONTINUE, BREAK, RETURN, EOFTOK, NOTOK }; -#define TYPEDEF (1<<0) -#define EXTERN (1<<1) -#define STATIC (1<<2) -#define AUTO (1<<3) -#define REGISTER (1<<4) -#define STORAGE (1<<5) +#define TYPEDEF (1<<0) +#define EXTERN (1<<1) +#define STATIC (1<<2) +#define AUTO (1<<3) +#define REGISTER (1<<4) +#define STORAGE (1<<5) + +#define VOLATILE (1<<0) +#define CONST (1<<1) +#define RESTRICT (1<<2) +#define TQUALIFIER (1<<3) struct symbol; extern struct symbol *yyval; diff --git a/types.c b/types.c @@ -12,13 +12,6 @@ static unsigned char stack[NR_DECLARATORS]; static unsigned char *stackp = stack; -struct qualifier * -initqlf(struct qualifier *qlf) -{ - memset(qlf, 0, sizeof(*qlf)); - return qlf; -} - struct ctype * initctype(register struct ctype *tp) { @@ -203,30 +196,6 @@ two_or_more: error: error(err, yytext); } -struct qualifier * -qualifier(register struct qualifier *qlf, unsigned char mod) -{ - switch (mod) { - case CONST: - if (options.repeat && qlf->c_const) - goto duplicated; - qlf->c_const = 1; - break; - case VOLATILE: - if (options.repeat && qlf->c_volatile) - goto duplicated; - qlf->c_volatile = 1; - break; - default: - assert(0); - } - - qlf->defined = 1; - return qlf; -duplicated: - error("duplicated '%s'", yytext); -} - #ifndef NDEBUG #include <stdio.h>