Consistently use PUSH_PARENT()/POP_PARENT().
[cparser] / parser.c
index be280a7..2bbdbf4 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -114,7 +114,7 @@ static elf_visibility_tag_t default_visibility = ELF_VISIBILITY_DEFAULT;
 #define PUSH_PARENT(stmt)                          \
        statement_t *const prev_parent = current_parent; \
        ((void)(current_parent = (stmt)))
-#define POP_PARENT ((void)(current_parent = prev_parent))
+#define POP_PARENT() ((void)(current_parent = prev_parent))
 
 /** special symbol used for anonymous entities. */
 static symbol_t *sym_anonymous = NULL;
@@ -655,9 +655,9 @@ static void type_error_incompatible(const char *msg,
                        parse_error_expected(NULL, (expected), NULL); \
                        add_anchor_token(expected);                   \
                        eat_until_anchor();                           \
-                       next_if((expected));                          \
                        rem_anchor_token(expected);                   \
-                       goto error_label;                             \
+                       if (token.type != (expected))                 \
+                         goto error_label;                           \
                }                                                 \
                next_token();                                     \
        } while (0)
@@ -5491,7 +5491,7 @@ static void parse_external_declaration(void)
                entity_t   *old_current_entity   = current_entity;
                current_function                 = function;
                current_entity                   = entity;
-               current_parent                   = NULL;
+               PUSH_PARENT(NULL);
 
                goto_first   = NULL;
                goto_anchor  = &goto_first;
@@ -5517,7 +5517,7 @@ static void parse_external_declaration(void)
                        }
                }
 
-               assert(current_parent   == NULL);
+               POP_PARENT();
                assert(current_function == function);
                assert(current_entity   == entity);
                current_entity   = old_current_entity;
@@ -5773,20 +5773,25 @@ static void parse_compound_type_entries(compound_t *compound)
        eat('{');
        add_anchor_token('}');
 
-       while (token.type != '}') {
-               if (token.type == T_EOF) {
-                       errorf(HERE, "EOF while parsing struct");
-                       break;
+       for (;;) {
+               switch (token.type) {
+                       DECLARATION_START
+                       case T_IDENTIFIER: {
+                               declaration_specifiers_t specifiers;
+                               parse_declaration_specifiers(&specifiers);
+                               parse_compound_declarators(compound, &specifiers);
+                               break;
+                       }
+
+                       default:
+                               rem_anchor_token('}');
+                               expect('}', end_error);
+end_error:
+                               /* §6.7.2.1:7 */
+                               compound->complete = true;
+                               return;
                }
-               declaration_specifiers_t specifiers;
-               parse_declaration_specifiers(&specifiers);
-               parse_compound_declarators(compound, &specifiers);
        }
-       rem_anchor_token('}');
-       next_token();
-
-       /* §6.7.2.1:7 */
-       compound->complete = true;
 }
 
 static type_t *parse_typename(void)
@@ -5883,10 +5888,9 @@ static expression_t *parse_string_literal(void)
 static expression_t *parse_boolean_literal(bool value)
 {
        expression_t *literal = allocate_expression_zero(EXPR_LITERAL_BOOLEAN);
-       literal->base.source_position = token.source_position;
-       literal->base.type            = type_bool;
-       literal->literal.value.begin  = value ? "true" : "false";
-       literal->literal.value.size   = value ? 4 : 5;
+       literal->base.type           = type_bool;
+       literal->literal.value.begin = value ? "true" : "false";
+       literal->literal.value.size  = value ? 4 : 5;
 
        next_token();
        return literal;
@@ -5998,10 +6002,9 @@ static expression_t *parse_number_literal(void)
        }
 
        expression_t *literal = allocate_expression_zero(kind);
-       literal->base.source_position = token.source_position;
-       literal->base.type            = type;
-       literal->literal.value        = token.literal;
-       literal->literal.suffix       = token.symbol;
+       literal->base.type      = type;
+       literal->literal.value  = token.literal;
+       literal->literal.suffix = token.symbol;
        next_token();
 
        /* integer type depends on the size of the number and the size
@@ -6018,9 +6021,8 @@ static expression_t *parse_number_literal(void)
 static expression_t *parse_character_constant(void)
 {
        expression_t *literal = allocate_expression_zero(EXPR_LITERAL_CHARACTER);
-       literal->base.source_position = token.source_position;
-       literal->base.type            = c_mode & _CXX ? type_char : type_int;
-       literal->literal.value        = token.literal;
+       literal->base.type     = c_mode & _CXX ? type_char : type_int;
+       literal->literal.value = token.literal;
 
        size_t len = literal->literal.value.size;
        if (len > 1) {
@@ -6042,9 +6044,8 @@ static expression_t *parse_character_constant(void)
 static expression_t *parse_wide_character_constant(void)
 {
        expression_t *literal = allocate_expression_zero(EXPR_LITERAL_WIDE_CHARACTER);
-       literal->base.source_position = token.source_position;
-       literal->base.type            = type_int;
-       literal->literal.value        = token.literal;
+       literal->base.type     = type_int;
+       literal->literal.value = token.literal;
 
        size_t len = wstrlen(&literal->literal.value);
        if (len > 1) {
@@ -6343,9 +6344,10 @@ static bool semantic_cast(expression_t *cast)
        return true;
 }
 
-static expression_t *parse_compound_literal(type_t *type)
+static expression_t *parse_compound_literal(source_position_t const *const pos, type_t *type)
 {
        expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
+       expression->base.source_position = *pos;
 
        parse_initializer_env_t env;
        env.type             = type;
@@ -6366,7 +6368,7 @@ static expression_t *parse_compound_literal(type_t *type)
  */
 static expression_t *parse_cast(void)
 {
-       source_position_t source_position = token.source_position;
+       source_position_t const pos = *HERE;
 
        eat('(');
        add_anchor_token(')');
@@ -6377,11 +6379,11 @@ static expression_t *parse_cast(void)
        expect(')', end_error);
 
        if (token.type == '{') {
-               return parse_compound_literal(type);
+               return parse_compound_literal(&pos, type);
        }
 
        expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
-       cast->base.source_position = source_position;
+       cast->base.source_position = pos;
 
        expression_t *value = parse_subexpression(PREC_CAST);
        cast->base.type   = type;
@@ -6888,10 +6890,9 @@ static expression_t *parse_noop_expression(void)
 {
        /* the result is a (int)0 */
        expression_t *literal = allocate_expression_zero(EXPR_LITERAL_MS_NOOP);
-       literal->base.type            = type_int;
-       literal->base.source_position = token.source_position;
-       literal->literal.value.begin  = "__noop";
-       literal->literal.value.size   = 6;
+       literal->base.type           = type_int;
+       literal->literal.value.begin = "__noop";
+       literal->literal.value.size  = 6;
 
        eat(T___noop);
 
@@ -7052,6 +7053,7 @@ static expression_t *parse_typeprop(expression_kind_t const kind)
        type_t       *orig_type;
        expression_t *expression;
        if (token.type == '(' && is_declaration_specifier(look_ahead(1))) {
+               source_position_t const pos = *HERE;
                next_token();
                add_anchor_token(')');
                orig_type = parse_typename();
@@ -7061,7 +7063,7 @@ static expression_t *parse_typeprop(expression_kind_t const kind)
                if (token.type == '{') {
                        /* It was not sizeof(type) after all.  It is sizeof of an expression
                         * starting with a compound literal */
-                       expression = parse_compound_literal(orig_type);
+                       expression = parse_compound_literal(&pos, orig_type);
                        goto typeprop_expression;
                }
        } else {
@@ -9188,7 +9190,7 @@ end_error:
 
        statement->case_label.statement = parse_label_inner_statement(statement, "case label");
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 }
 
@@ -9228,7 +9230,7 @@ end_error:
 
        statement->case_label.statement = parse_label_inner_statement(statement, "default label");
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 }
 
@@ -9262,7 +9264,7 @@ static statement_t *parse_label_statement(void)
        *label_anchor = &statement->label;
        label_anchor  = &statement->label.next;
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 }
 
@@ -9306,7 +9308,7 @@ end_error:
                warningf(WARN_PARENTHESES, pos, "suggest explicit braces to avoid ambiguous 'else'");
        }
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 }
 
@@ -9393,10 +9395,10 @@ static statement_t *parse_switch(void)
        }
        check_enum_cases(&statement->switchs);
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 end_error:
-       POP_PARENT;
+       POP_PARENT();
        return create_invalid_statement();
 }
 
@@ -9435,10 +9437,10 @@ static statement_t *parse_while(void)
 
        statement->whiles.body = parse_loop_body(statement);
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 end_error:
-       POP_PARENT;
+       POP_PARENT();
        return create_invalid_statement();
 }
 
@@ -9470,10 +9472,10 @@ static statement_t *parse_do(void)
        expect(')', end_error);
        expect(';', end_error);
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 end_error:
-       POP_PARENT;
+       POP_PARENT();
        return create_invalid_statement();
 }
 
@@ -9542,11 +9544,11 @@ static statement_t *parse_for(void)
        scope_pop(old_scope);
        environment_pop_to(top);
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 
 end_error2:
-       POP_PARENT;
+       POP_PARENT();
        rem_anchor_token(')');
        assert(current_scope == &statement->fors.scope);
        scope_pop(old_scope);
@@ -9839,7 +9841,7 @@ static statement_t *parse_ms_try_statment(void)
        statement->ms_try.try_statement = parse_compound_statement(false);
        current_try = rem;
 
-       POP_PARENT;
+       POP_PARENT();
 
        if (next_if(T___except)) {
                expect('(', end_error);
@@ -10331,7 +10333,7 @@ end_error:
        scope_pop(old_scope);
        environment_pop_to(top);
 
-       POP_PARENT;
+       POP_PARENT();
        return statement;
 }