scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 4292e620e8f4305403a0ef59a7b41b48354314be
parent bc40f94fdda9ad39a8cc71a9b5a4c80eebe210a8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 29 Jun 2012 10:43:48 +0200

Join types.h and symbol.h

These headers has too much dependencies between them, until the point of you
can not use one without the other, so the best solution is join both. This
solution also allows us to move ctype to type.c, which is a better place for it.

Diffstat:
code.c | 1+
decl.c | 1-
keyword.c | 1-
lex.c | 1-
symbol.c | 64----------------------------------------------------------------
symbol.h | 48++++++++++++++++++++++++++++++++++++++++++++++--
types.c | 66+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
types.h | 62--------------------------------------------------------------
8 files changed, 110 insertions(+), 134 deletions(-)

diff --git a/code.c b/code.c @@ -1,3 +1,4 @@ +#include <stddef.h> #include "cc.h" #include "code.h" diff --git a/decl.c b/decl.c @@ -5,7 +5,6 @@ #include "sizes.h" #include "cc.h" #include "tokens.h" -#include "types.h" #include "syntax.h" #include "symbol.h" diff --git a/keyword.c b/keyword.c @@ -1,7 +1,6 @@ #include <stddef.h> #include "tokens.h" -#include "types.h" #include "symbol.h" diff --git a/lex.c b/lex.c @@ -7,7 +7,6 @@ #include "cc.h" #include "tokens.h" #include "symbol.h" -#include "types.h" static FILE *yyin; diff --git a/symbol.c b/symbol.c @@ -4,7 +4,6 @@ #include "cc.h" #include "symbol.h" -#include "tokens.h" #define xmalloc malloc #define xstrdup strdup @@ -102,66 +101,3 @@ unsigned char hashfun(register const char *s) /* nothing */; return h; } - -void ctype(struct ctype *cp, unsigned char mod) -{ - extern unsigned char nested_level; - - switch (mod) { - case TYPEDEF: - if (cp->c_type) - goto duplicated; - if (cp->c_extrn | cp->c_auto | cp->c_reg | cp->c_static) - goto two_storage; - cp->c_type = 1; - return; - case EXTERN: - if (cp->c_extrn) - goto duplicated; - if (cp->c_type | cp->c_auto | cp->c_reg | cp->c_static) - goto two_storage; - cp->c_extrn = 1; - return; - case STATIC: - if (cp->c_static) - goto duplicated; - if (cp->c_type | cp->c_extrn | cp->c_auto | cp->c_reg) - goto two_storage; - cp->c_static = 1; - return; - case AUTO: - if (nested_level != 0) - goto bad_file_scope_storage; - if (cp->c_type | cp->c_extrn | cp->c_static | cp->c_reg) - goto two_storage; - if (cp->c_auto) - goto duplicated; - cp->c_static = 1; - return; - case REGISTER: - if (nested_level != 0) - goto bad_file_scope_storage; - if (cp->c_type | cp->c_extrn | cp->c_auto | cp->c_static) - goto two_storage; - if (cp->c_reg) - goto duplicated; - cp->c_reg = 1; - return; - case CONST: - if (user_opt.typeqlf_repeat && cp->c_reg) - goto duplicated; - cp->c_const = 1; - return; - case VOLATILE: - if (user_opt.typeqlf_repeat && cp->c_vol) - goto duplicated; - cp->c_vol = 1; - return; - } -bad_file_scope_storage: - error("file-scope declaration specifies ā€˜%sā€™", yytext); -two_storage: - error("Two or more storage specifier"); -duplicated: - error("duplicated '%s'", yytext); -} diff --git a/symbol.h b/symbol.h @@ -7,6 +7,21 @@ # include <stdbool.h> #endif +#define T_CHAR (&tschar) +#define T_SHORT (&tshort) +#define T_INT (&tint) +#define T_FLOAT (&tfloat) +#define T_DOUBLE (&tdouble) +#define T_LDOUBLE (&tdouble) +#define T_LONG (&tlong) +#define T_LLONG (&tllong) +#define T_VOID (&tvoid) +#define T_BOOL (&tbool) + +#define ARY 1 +#define PTR 2 +#define FTN 3 + enum namespace { NS_IDEN, NS_KEYWORD, @@ -15,8 +30,6 @@ enum namespace { NS_TYPEDEF }; -struct type; - struct ctype { bool c_type : 1; bool c_extrn : 1; @@ -30,6 +43,24 @@ struct ctype { char *iden; }; +struct type { + unsigned op : 5; + struct type *base; + struct type *ary; /* array */ + struct type *ptr; /* pointer */ + struct type *ftn; /* function */ + struct type *cnst; /* const */ + struct type *vltl; /* volatile */ + struct type *rstr; /* restricted */ + + union { + struct { + unsigned btype : 4; + }; + size_t nelem; /* size of array */ + }; +}; + struct symbol { struct ctype ctype; unsigned char ns; @@ -52,6 +83,14 @@ struct symctx { struct symctx *next; }; +extern struct type tchar, tshort, tint, tulong, tllong, tvoid, tkeyword; +extern struct type tfloat, tdouble, tldouble, tlong; + + +extern struct type *mktype(register struct type *base, unsigned char op); +extern struct type *decl_type(struct type *t); +extern void pushtype(unsigned char mod); +extern struct type *btype(struct type *tp, unsigned char tok); extern void new_ctx(struct symctx *ctx); extern void del_ctx(void); extern struct symbol *install(const char *s, unsigned char key); @@ -59,5 +98,10 @@ extern struct symbol *lookup(char *s, unsigned char key); extern unsigned char hashfun(register const char *s); extern void ctype(struct ctype *cp, unsigned char mod); +#ifndef NDEBUG +extern void ptype(register struct type *t); +#else +# define ptype(t) +#endif #endif diff --git a/types.c b/types.c @@ -5,7 +5,7 @@ #include "sizes.h" #include "cc.h" #include "tokens.h" -#include "types.h" +#include "symbol.h" /* TODO: create wrapper file */ #define xcalloc calloc @@ -129,8 +129,68 @@ struct type *mktype(register struct type *base, unsigned char op) return nt; } - - +void ctype(struct ctype *cp, unsigned char mod) +{ + extern unsigned char nested_level; + + switch (mod) { + case TYPEDEF: + if (cp->c_type) + goto duplicated; + if (cp->c_extrn | cp->c_auto | cp->c_reg | cp->c_static) + goto two_storage; + cp->c_type = 1; + return; + case EXTERN: + if (cp->c_extrn) + goto duplicated; + if (cp->c_type | cp->c_auto | cp->c_reg | cp->c_static) + goto two_storage; + cp->c_extrn = 1; + return; + case STATIC: + if (cp->c_static) + goto duplicated; + if (cp->c_type | cp->c_extrn | cp->c_auto | cp->c_reg) + goto two_storage; + cp->c_static = 1; + return; + case AUTO: + if (nested_level != 0) + goto bad_file_scope_storage; + if (cp->c_type | cp->c_extrn | cp->c_static | cp->c_reg) + goto two_storage; + if (cp->c_auto) + goto duplicated; + cp->c_static = 1; + return; + case REGISTER: + if (nested_level != 0) + goto bad_file_scope_storage; + if (cp->c_type | cp->c_extrn | cp->c_auto | cp->c_static) + goto two_storage; + if (cp->c_reg) + goto duplicated; + cp->c_reg = 1; + return; + case CONST: + if (user_opt.typeqlf_repeat && cp->c_reg) + goto duplicated; + cp->c_const = 1; + return; + case VOLATILE: + if (user_opt.typeqlf_repeat && cp->c_vol) + goto duplicated; + cp->c_vol = 1; + return; + } +bad_file_scope_storage: + error("file-scope declaration specifies ā€˜%sā€™", yytext); +two_storage: + error("Two or more storage specifier"); +duplicated: + error("duplicated '%s'", yytext); +} #ifndef NDEBUG #include <stdio.h> diff --git a/types.h b/types.h @@ -1,62 +0,0 @@ -#ifndef TYPES_H_ -#define TYPES_H_ - -#ifndef __bool_true_false_are_defined -# include <stdbool.h> -#endif - -struct ctype; - -struct type { - unsigned op : 5; - struct type *base; - struct type *ary; /* array */ - struct type *ptr; /* pointer */ - struct type *ftn; /* function */ - struct type *cnst; /* const */ - struct type *vltl; /* volatile */ - struct type *rstr; /* restricted */ - - union { - struct { - unsigned btype : 4; - }; - size_t nelem; /* size of array */ - }; -}; - - -extern struct type tchar, tshort, tint, tulong, tllong, tvoid, tkeyword; -extern struct type tfloat, tdouble, tldouble, tlong; - -#define T_CHAR (&tschar) -#define T_SHORT (&tshort) -#define T_INT (&tint) -#define T_FLOAT (&tfloat) -#define T_DOUBLE (&tdouble) -#define T_LDOUBLE (&tdouble) -#define T_LONG (&tlong) -#define T_LLONG (&tllong) -#define T_VOID (&tvoid) -#define T_BOOL (&tbool) - - -#define ARY 1 -#define PTR 2 -#define FTN 3 - -struct type *mktype(register struct type *base, unsigned char op); - -extern struct type *decl_type(struct type *t); -void pushtype(unsigned char mod); - -#ifndef NDEBUG -extern void ptype(register struct type *t); -#else -# define ptype(t) -#endif - - -extern struct type *btype(struct type *tp, unsigned char tok); - -#endif