From e367f7db025eabd57d0c150e1072cb2ead724f5c Mon Sep 17 00:00:00 2001 From: Teddy Date: Sat, 12 Apr 2014 23:21:00 +0800 Subject: remove redundant code in typedef handling --- ast.h | 2 +- cibic.y | 34 +++++++++++++++++----------------- semantics.c | 19 ++++++++----------- semantics.h | 16 +++++++--------- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/ast.h b/ast.h index a886c2e..11fdf04 100644 --- a/ast.h +++ b/ast.h @@ -60,7 +60,7 @@ typedef struct CNode { CNode *cnode_add_loc(CNode *node, YYLTYPE loc); CNode *cnode_create_ast(CNode *wrapped); -CNode *cnode_create_nop(); +CNode *cnode_create_nop(void); CNode *cnode_create_general(int type, int subtype, int pnum, va_list ap); CNode *cnode_list_append(CNode *list, CNode *tail); CNode *cnode_list_wrap(int type, CNode *list); diff --git a/cibic.y b/cibic.y index 66337c9..6e25724 100644 --- a/cibic.y +++ b/cibic.y @@ -36,25 +36,25 @@ declaration $$ = cnode_add_loc(cnode_create_typedef( $2, cnode_add_loc(cnode_list_wrap(DECLRS, $4), @4)), @$); - exit_declr(); + clear_state(); } | type_specifier ';' { $$ = cnode_add_loc(cnode_create_decl( $1, cnode_list_wrap(INIT_DECLRS, cnode_create_nop())), @$); - exit_declr(); + clear_state(); } | type_specifier init_declarators ';' { $$ = cnode_add_loc(cnode_create_decl( $1, cnode_add_loc(cnode_list_wrap(INIT_DECLRS, $2), @2)), @$); - exit_declr(); + clear_state(); } function_definition : type_specifier declarator compound_statement { $$ = cnode_add_loc(cnode_create_func($1, $2, $3), @$); - exit_declr(); + clear_state(); } parameters @@ -84,22 +84,22 @@ array_initializer $$ = cnode_list_append(cnode_add_loc($1, @1), $3); } type_specifier - : KW_VOID { $$ = cnode_add_loc(cnode_create_type_spec(KW_VOID, 0), @$); enter_declr(); } - | KW_CHAR { $$ = cnode_add_loc(cnode_create_type_spec(KW_CHAR, 0), @$); enter_declr(); } - | KW_INT { $$ = cnode_add_loc(cnode_create_type_spec(KW_INT, 0), @$); enter_declr(); } + : KW_VOID { $$ = cnode_add_loc(cnode_create_type_spec(KW_VOID, 0), @$); force_id(); } + | KW_CHAR { $$ = cnode_add_loc(cnode_create_type_spec(KW_CHAR, 0), @$); force_id(); } + | KW_INT { $$ = cnode_add_loc(cnode_create_type_spec(KW_INT, 0), @$); force_id(); } | struct_or_union identifier '{' struct_fields '}' { $$ = cnode_add_loc(cnode_create_type_spec($1, 2, $2, cnode_add_loc(cnode_list_wrap(FIELDS, $4), @4)), @$); - enter_declr(); + force_id(); } | struct_or_union '{' struct_fields '}' { $$ = cnode_add_loc(cnode_create_type_spec($1, 2, cnode_create_nop(), cnode_add_loc(cnode_list_wrap(FIELDS, $3), @3)), @$); - enter_declr(); + force_id(); } | struct_or_union identifier { $$ = cnode_add_loc(cnode_create_type_spec($1, 2, $2, cnode_create_nop()), @$); - enter_declr(); + force_id(); } - | user_type { $$ = cnode_add_loc(cnode_create_type_spec(USER_TYPE, 1, $1), @$); enter_declr(); } + | user_type { $$ = cnode_add_loc(cnode_create_type_spec(USER_TYPE, 1, $1), @$); force_id(); } user_type : USER_TYPE { $$ = cnode_add_loc(cnode_create_identifier($1), @$); } @@ -113,17 +113,17 @@ struct_field cnode_create_struct_field( $1, cnode_add_loc(cnode_list_wrap(DECLRS, $2), @2)), @$); - exit_declr(); + clear_state(); } struct_or_union - : KW_STRUCT { $$ = KW_STRUCT; enter_struct(); } - | KW_UNION { $$ = KW_UNION; enter_struct(); } + : KW_STRUCT { $$ = KW_STRUCT; force_id(); } + | KW_UNION { $$ = KW_UNION; force_id(); } plain_declaration : type_specifier declarator { $$ = cnode_add_loc(cnode_create_plain_decl($1, $2), @$); - exit_declr(); + clear_state(); } direct_declarator @@ -155,7 +155,7 @@ expression_statement | expression ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_EXP, 1, $1), @$); } compound_statement - : { exit_declr(); enter_block(); } '{' comp_decls comp_stmts '}' { + : { clear_state(); enter_block(); } '{' comp_decls comp_stmts '}' { $$ = cnode_add_loc( cnode_create_stmt(STMT_COMP, 2, cnode_add_loc(cnode_list_wrap(COMP_DECLS, $3), @3), cnode_add_loc(cnode_list_wrap(COMP_STMTS, $4), @4)), @$); @@ -314,7 +314,7 @@ cast_expression type_name : type_specifier abstract_declarator_opt { $$ = cnode_add_loc(cnode_create_declr(0, 2, $1, $2), @$); - exit_declr(); + clear_state(); } abstract_declarator_opt diff --git a/semantics.c b/semantics.c index d507e6a..da2a42b 100644 --- a/semantics.c +++ b/semantics.c @@ -1643,9 +1643,8 @@ void semantics_check(CNode *p) { static CScope_t typedef_scope; static enum { NONE, - TYPEDEF_DECLR, - OTHER_DECLR, - STRUCT_TAG + FORCE_ID, + IN_TYPEDEF } typedef_state; void cibic_init() { @@ -1656,11 +1655,10 @@ void cibic_init() { int is_identifier(const char *name) { CSymbol_t lu; /* struct tag */ - if (typedef_state == STRUCT_TAG) return 1; /* the parser is reading declarators */ - if (typedef_state == OTHER_DECLR) return 1; + if (typedef_state == FORCE_ID) return 1; /* the parser is reading typedef */ - if (typedef_state == TYPEDEF_DECLR) return 1; + if (typedef_state == IN_TYPEDEF) return 1; /* no info about name, assume it to be an id by default */ lu = cscope_lookup(typedef_scope, name, NS_ID); if (!lu) return 1; @@ -1668,7 +1666,7 @@ int is_identifier(const char *name) { } void push(const char *name) { - if (typedef_state == TYPEDEF_DECLR) + if (typedef_state == IN_TYPEDEF) cscope_push_type(typedef_scope, ctype_create(name, 0, NULL), NS_ID); else cscope_push_var(typedef_scope, cvar_create(name, NULL, NULL), NS_ID); @@ -1684,7 +1682,6 @@ CDef_t cdef_create(const char *name, CType_t type, CNode *ast) { void enter_block() { cscope_enter(typedef_scope); } void exit_block() { cscope_exit(typedef_scope); } -void enter_typedef() { typedef_state = TYPEDEF_DECLR; } -void enter_declr() { typedef_state = OTHER_DECLR; } -void enter_struct() { typedef_state = STRUCT_TAG; } -void exit_declr() { typedef_state = NONE; } +void force_id() { typedef_state = FORCE_ID; } +void enter_typedef() { typedef_state = IN_TYPEDEF; } +void clear_state() { typedef_state = NONE; } diff --git a/semantics.h b/semantics.h index 84dc9dc..b836ed2 100644 --- a/semantics.h +++ b/semantics.h @@ -142,7 +142,7 @@ struct CDef { }; CDef_t cdef_create(const char *name, CType_t type, CNode *ast); -CScope_t cscope_create(); +CScope_t cscope_create(void); CSymbol_t cscope_lookup(CScope_t cs, const char *name, int nspace); int cscope_push_var(CScope_t cs, CVar_t var, int nspace); int cscope_push_def(CScope_t cs, CDef_t def, int nspace); @@ -156,13 +156,11 @@ const char *ctable_cvar_print(void *var); void semantics_check(CNode *ast); int is_identifier(const char *name); -void declr_begin(); -void declr_end(); void push(const char *name); -void cibic_init(); -void enter_block(); -void exit_block(); -void enter_typedef(); -void exit(); -void enter_declr(); +void cibic_init(void); +void enter_block(void); +void exit_block(void); +void force_id(void); +void enter_typedef(void); +void clear_state(void); #endif -- cgit v1.2.3