bugfixes for context/symbol-declaration mapping
authorMatthias Braun <matze@braunis.de>
Sat, 21 Jul 2007 12:24:41 +0000 (12:24 +0000)
committerMatthias Braun <matze@braunis.de>
Sat, 21 Jul 2007 12:24:41 +0000 (12:24 +0000)
[r18335]

ast.c
ast.h
ast_t.h
lexer.c
parser.c
type_t.h

diff --git a/ast.c b/ast.c
index 3e5bf78..1599986 100644 (file)
--- a/ast.c
+++ b/ast.c
@@ -121,8 +121,8 @@ void print_expression(FILE *out, const expression_t *expression)
 }
 
 static
-void print_block_statement(FILE *out, int indent,
-                           const block_statement_t *block)
+void print_compound_statement(FILE *out, int indent,
+                              const compound_statement_t *block)
 {
        statement_t *statement = block->first_statement;
        while(statement != NULL) {
@@ -194,9 +194,9 @@ void print_statement(FILE *out, int indent, const statement_t *statement)
                fprintf(out, "\t");
 
        switch(statement->type) {
-       case STATEMENT_BLOCK:
-               print_block_statement(out, indent,
-                                     (const block_statement_t*) statement);
+       case STATEMENT_COMPOUND:
+               print_compound_statement(out, indent,
+                                        (const compound_statement_t*) statement);
                break;
        case STATEMENT_RETURN:
                print_return_statement(out, (const return_statement_t*) statement);
diff --git a/ast.h b/ast.h
index 44ec234..fafa2b3 100644 (file)
--- a/ast.h
+++ b/ast.h
@@ -23,7 +23,7 @@ typedef struct comma_expression_t         comma_expression_t;
 typedef struct declaration_t              declaration_t;
 
 typedef struct statement_t                statement_t;
-typedef struct block_statement_t          block_statement_t;
+typedef struct compound_statement_t       compound_statement_t;
 typedef struct return_statement_t         return_statement_t;
 typedef struct if_statement_t             if_statement_t;
 typedef struct declaration_statement_t    declaration_statement_t;
diff --git a/ast_t.h b/ast_t.h
index 61c6c78..82c91b3 100644 (file)
--- a/ast_t.h
+++ b/ast_t.h
@@ -179,7 +179,7 @@ struct declaration_t {
 
 typedef enum {
        STATEMENT_INVALID,
-       STATEMENT_BLOCK,
+       STATEMENT_COMPOUND,
        STATEMENT_RETURN,
        STATEMENT_DECLARATION,
        STATEMENT_IF,
@@ -199,7 +199,7 @@ struct return_statement_t {
        expression_t *return_value;
 };
 
-struct block_statement_t {
+struct compound_statement_t {
        statement_t  statement;
        statement_t *first_statement;
 };
diff --git a/lexer.c b/lexer.c
index c4e63a4..ce964fa 100644 (file)
--- a/lexer.c
+++ b/lexer.c
@@ -1014,6 +1014,7 @@ void exit_lexer(void)
 static __attribute__((unused))
 void dbg_pos(const source_position_t source_position)
 {
-       fprintf(stdout, "%s:%d\n", source_position.input_name, source_position.linenr);
+       fprintf(stdout, "%s:%d\n", source_position.input_name,
+               source_position.linenr);
        fflush(stdout);
 }
index 914f3a6..35c734b 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -28,7 +28,7 @@ static int                   lookahead_bufpos;
 static struct obstack        environment_obstack;
 static environment_entry_t **environment_stack = NULL;
 static translation_unit_t   *translation_unit  = NULL;
-static block_statement_t    *context           = NULL;
+static const void           *context           = NULL;
 
 static
 statement_t *parse_compound_statement(void);
@@ -190,11 +190,13 @@ void environment_push(declaration_t *declaration, const void *context)
        ARR_RESIZE(environment_stack, top + 1);
        environment_stack[top] = entry;
 
+       assert(declaration->source_position.input_name != NULL);
+
        symbol_t *symbol = declaration->symbol;
        assert(declaration != symbol->declaration);
 
        if(symbol->context == context) {
-               if(context != NULL) {
+               if(symbol->declaration != NULL) {
                        assert(symbol->declaration != NULL);
                        parser_print_error_prefix_pos(declaration->source_position);
                        fprintf(stderr, "multiple definitions for symbol '%s'.\n",
@@ -204,6 +206,8 @@ void environment_push(declaration_t *declaration, const void *context)
                }
        }
 
+       fprintf(stderr, "Set '%s' to %p\n", symbol->string, (void*) declaration);
+
        entry->old_declaration = symbol->declaration;
        entry->old_context     = symbol->context;
        entry->symbol          = symbol;
@@ -258,6 +262,7 @@ static expression_t *parse_assignment_expression(void)
 static compound_entry_t *parse_compound_type_entries(void);
 static void parse_declarator(declaration_t *declaration,
                              storage_class_t storage_class, type_t *type);
+static void maybe_push_declaration(declaration_t *declaration);
 
 typedef struct declaration_specifiers_t  declaration_specifiers_t;
 struct declaration_specifiers_t {
@@ -273,20 +278,31 @@ static type_t *parse_struct_specifier(void)
        struct_type->type.type       = TYPE_COMPOUND_STRUCT;
        struct_type->source_position = token.source_position;
 
+       fprintf(stderr, "New struct %p\n", (void*) struct_type);
+
+       int         top          = environment_top();
+       const void *last_context = context;
+       context                  = struct_type;
+
        if(token.type == T_IDENTIFIER) {
-               /* TODO */
                next_token();
                if(token.type == '{') {
                        parse_compound_type_entries();
                }
+               fprintf(stderr, "Finished struct %p\n",(void*)  struct_type);
        } else if(token.type == '{') {
                parse_compound_type_entries();
+               fprintf(stderr, "Finished struct %p\n", (void*) struct_type);
        } else {
                parse_error_expected("problem while parsing struct type specifiers",
                                     T_IDENTIFIER, '{', 0);
-               return NULL;
+               struct_type = NULL;
        }
 
+       assert(context == struct_type);
+       context = last_context;
+       environment_pop_to(top);
+
        return (type_t*) struct_type;
 }
 
@@ -298,6 +314,10 @@ static type_t *parse_union_specifier(void)
        union_type->type.type       = TYPE_COMPOUND_UNION;
        union_type->source_position = token.source_position;
 
+       int         top          = environment_top();
+       const void *last_context = context;
+       context                  = union_type;
+
        if(token.type == T_IDENTIFIER) {
                union_type->symbol = token.v.symbol;
                next_token();
@@ -309,8 +329,13 @@ static type_t *parse_union_specifier(void)
        } else {
                parse_error_expected("problem while parsing union type specifiers",
                                     T_IDENTIFIER, '{');
+               union_type = NULL;
        }
 
+       assert(context == union_type);
+       context = last_context;
+       environment_pop_to(top);
+
        return (type_t*) union_type;
 }
 
@@ -433,7 +458,6 @@ typedef enum {
        case T_enum:            \
        COMPLEX_SPECIFIERS      \
        IMAGINARY_SPECIFIERS
-/* TODO: T_IDENTIFIER && typename */
 
 #define DECLARATION_START   \
        STORAGE_CLASSES         \
@@ -462,6 +486,7 @@ void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
        type_t        *type            = NULL;
        unsigned       type_qualifiers = 0;
        unsigned       type_specifiers = 0;
+       int            newtype         = 0;
 
        while(1) {
                switch(token.type) {
@@ -682,6 +707,7 @@ finish_specifiers:
                atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
                atype->type.type     = TYPE_ATOMIC;
                atype->atype         = atomic_type;
+               newtype              = 1;
 
                type = (type_t*) atype;
        } else {
@@ -693,7 +719,7 @@ finish_specifiers:
        type->qualifiers = type_qualifiers;
 
        type_t *result = typehash_insert(type);
-       if(result != (type_t*) type) {
+       if(newtype && result != (type_t*) type) {
                obstack_free(type_obst, type);
        }
 
@@ -779,10 +805,10 @@ void parse_parameter()
        specifiers.type = parse_pointer(specifiers.type);
 
        if(token.type == '(' || token.type == T_IDENTIFIER) {
-               declaration_t declaration;
-               memset(&declaration, 0, sizeof(declaration));
-               parse_declarator(&declaration, specifiers.storage_class,
+               declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
+               parse_declarator(declaration, specifiers.storage_class,
                                 specifiers.type);
+               maybe_push_declaration(declaration);
        }
 }
 
@@ -790,13 +816,19 @@ static
 void parse_parameters()
 {
        if(token.type == T_IDENTIFIER) {
-               parse_identifier_list();
-               return;
+               symbol_t      *symbol      = token.v.symbol;
+               declaration_t *declaration = symbol->declaration;
+               if(declaration == NULL
+                               || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
+                       parse_identifier_list();
+                       return;
+               }
        }
 
        while(1) {
                switch(token.type) {
                case T_DOTDOTDOT:
+               case T_IDENTIFIER:
                DECLARATION_START
                        parse_parameter();
                        break;
@@ -848,7 +880,8 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
 
        switch(token.type) {
        case T_IDENTIFIER:
-               declaration->symbol = token.v.symbol;
+               declaration->symbol          = token.v.symbol;
+               declaration->source_position = token.source_position;
                next_token();
                break;
        case '(':
@@ -866,8 +899,16 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                case '(':
                        next_token();
 
+                       int         top          = environment_top();
+                       const void *last_context = context;
+                       context                  = NULL;
+
                        parse_parameters();
 
+                       assert(context == NULL);
+                       context = last_context;
+                       environment_pop_to(top);
+
                        expect_void(')');
                        break;
                case '[':
@@ -884,7 +925,7 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
                                }
                        }
 
-                       if(token.type == '*' /* TODO: && lookahead == ']' */) {
+                       if(token.type == '*' && la(1)->type == ']') {
                                next_token();
                        } else if(token.type != ']') {
                                parse_assignment_expression();
@@ -899,9 +940,14 @@ void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
 
 declarator_finished:
        parse_attributes();
+}
 
-       fprintf(stderr, "Declarator type: ");
-       print_type(stderr, type);
+static
+void maybe_push_declaration(declaration_t *declaration)
+{
+       fprintf(stderr, "Declarator '%s' type: ",
+                       declaration->symbol ? declaration->symbol->string : "");
+       print_type(stderr, declaration->type);
        fprintf(stderr, "\n");
 
        symbol_t *symbol = declaration->symbol;
@@ -919,6 +965,7 @@ void parse_init_declarators(const declaration_specifiers_t *specifiers)
 
                parse_declarator(declaration, specifiers->storage_class,
                                 specifiers->type);
+               maybe_push_declaration(declaration);
                if(token.type == '=') {
                        next_token();
                        if(token.type == '{') {
@@ -943,16 +990,19 @@ static
 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
 {
        while(1) {
-               declaration_t declaration;
+               declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
+
                compound_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
+               entry->declaration = declaration;
 
                if(token.type == ':') {
                        next_token();
                        parse_constant_expression();
                        /* TODO */
                } else {
-                       parse_declarator(&declaration, specifiers->storage_class,
+                       parse_declarator(declaration, specifiers->storage_class,
                                         specifiers->type);
+                       maybe_push_declaration(declaration);
 
                        if(token.type == ':') {
                                next_token();
@@ -1682,12 +1732,20 @@ statement_t *parse_compound_statement(void)
 {
        eat('{');
 
-       int top = environment_top();
+       compound_statement_t *compound_statement
+               = allocate_ast_zero(sizeof(compound_statement[0]));
+       compound_statement->statement.type = STATEMENT_COMPOUND;
+
+       int         top          = environment_top();
+       const void *last_context = context;
+       context                  = compound_statement;
 
        while(token.type != '}') {
                parse_statement();
        }
 
+       assert(context == compound_statement);
+       context = last_context;
        environment_pop_to(top);
 
        next_token();
index 738fcba..020f87d 100644 (file)
--- a/type_t.h
+++ b/type_t.h
@@ -96,8 +96,7 @@ struct method_type_t {
 };
 
 struct compound_entry_t {
-       type_t            *type;
-       symbol_t          *symbol;
+       declaration_t     *declaration;
        compound_entry_t  *next;
        source_position_t  source_position;
 };