Repair semantics of !: Result type is int, not the operand type.
[cparser] / parser.c
index 7fac835..c26ca18 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -108,6 +108,7 @@ static declaration_t      *last_declaration  = NULL;
 static declaration_t      *current_function  = NULL;
 static switch_statement_t *current_switch    = NULL;
 static statement_t        *current_loop      = NULL;
+static statement_t        *current_parent    = NULL;
 static ms_try_statement_t *current_try       = NULL;
 static goto_statement_t   *goto_first        = NULL;
 static goto_statement_t   *goto_last         = NULL;
@@ -116,6 +117,11 @@ static label_statement_t  *label_last        = NULL;
 static translation_unit_t *unit              = NULL;
 static struct obstack      temp_obst;
 
+#define PUSH_PARENT(stmt)                          \
+       statement_t *const prev_parent = current_parent; \
+       current_parent = (stmt);
+#define POP_PARENT ((void)(current_parent = prev_parent))
+
 static source_position_t null_position = { NULL, 0 };
 
 /* symbols for Microsoft extended-decl-modifier */
@@ -321,7 +327,8 @@ static statement_t *allocate_statement_zero(statement_kind_t kind)
        size_t       size = get_statement_struct_size(kind);
        statement_t *res  = allocate_ast_zero(size);
 
-       res->base.kind = kind;
+       res->base.kind   = kind;
+       res->base.parent = current_parent;
        return res;
 }
 
@@ -1601,7 +1608,6 @@ static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
                                switch(kind) {
                                case GNU_AK_CONST:
                                case GNU_AK_VOLATILE:
-                               case GNU_AK_DEPRECATED:
                                case GNU_AK_NAKED:
                                case GNU_AK_MALLOC:
                                case GNU_AK_WEAK:
@@ -1650,6 +1656,7 @@ static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
                                case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
                                case GNU_AK_CONSTRUCTOR:       modifiers |= DM_CONSTRUCTOR;       goto no_arg;
                                case GNU_AK_DESTRUCTOR:        modifiers |= DM_DESTRUCTOR;        goto no_arg;
+                               case GNU_AK_DEPRECATED:        modifiers |= DM_DEPRECATED;        goto no_arg;
 
                                case GNU_AK_ALIGNED:
                                        /* __align__ may be used without an argument */
@@ -2339,7 +2346,16 @@ static initializer_t *parse_sub_initializer(type_path_t *path,
                designator_t *designator = NULL;
                if (token.type == '.' || token.type == '[') {
                        designator = parse_designation();
+                       goto finish_designator;
+               } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
+                       /* GNU-style designator ("identifier: value") */
+                       designator = allocate_ast_zero(sizeof(designator[0]));
+                       designator->source_position = token.source_position;
+                       designator->symbol          = token.v.symbol;
+                       eat(T_IDENTIFIER);
+                       eat(':');
 
+finish_designator:
                        /* reset path to toplevel, evaluate designator from there */
                        ascend_to(path, top_path_level);
                        if (!walk_designator(path, designator, false)) {
@@ -3147,6 +3163,7 @@ static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                case T__forceinline:
                        /* only in microsoft mode */
                        specifiers->modifiers |= DM_FORCEINLINE;
+                       /* FALLTHROUGH */
 
                case T_inline:
                        next_token();
@@ -3257,17 +3274,24 @@ finish_specifiers:
                case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
                        atomic_type = ATOMIC_TYPE_ULONG;
                        break;
+
                case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
                case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
                case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
                case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
                        | SPECIFIER_INT:
                        atomic_type = ATOMIC_TYPE_LONGLONG;
-                       break;
+                       goto warn_about_long_long;
+
                case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
                case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
                        | SPECIFIER_INT:
                        atomic_type = ATOMIC_TYPE_ULONGLONG;
+warn_about_long_long:
+                       if (warning.long_long) {
+                               warningf(&specifiers->source_position,
+                                        "ISO C90 does not support 'long long'");
+                       }
                        break;
 
                case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
@@ -3767,6 +3791,9 @@ static construct_type_t *parse_inner_declarator(declaration_t *declaration,
                modifiers |= parse_attributes(&attributes);
        }
 
+       if (declaration != NULL)
+               declaration->modifiers |= modifiers;
+
        construct_type_t *inner_types = NULL;
 
        switch(token.type) {
@@ -3974,7 +4001,6 @@ static declaration_t *parse_declarator(
        declaration_t *const declaration    = allocate_declaration_zero();
        declaration->declared_storage_class = specifiers->declared_storage_class;
        declaration->modifiers              = specifiers->modifiers;
-       declaration->deprecated             = specifiers->deprecated;
        declaration->deprecated_string      = specifiers->deprecated_string;
        declaration->get_property_sym       = specifiers->get_property_sym;
        declaration->put_property_sym       = specifiers->put_property_sym;
@@ -4098,7 +4124,7 @@ static declaration_t *internal_record_declaration(
        const symbol_t *const symbol  = declaration->symbol;
        const namespace_t     namespc = (namespace_t)declaration->namespc;
 
-       assert(declaration->symbol != NULL);
+       assert(symbol != NULL);
        declaration_t *previous_declaration = get_declaration(symbol, namespc);
 
        type_t *const orig_type = declaration->type;
@@ -4116,6 +4142,13 @@ static declaration_t *internal_record_declaration(
                check_type_of_main(declaration, &type->function);
        }
 
+       if (warning.nested_externs                             &&
+           declaration->storage_class == STORAGE_CLASS_EXTERN &&
+           scope                      != global_scope) {
+               warningf(&declaration->source_position,
+                        "nested extern declaration of '%#T'", declaration->type, symbol);
+       }
+
        assert(declaration != previous_declaration);
        if (previous_declaration != NULL
                        && previous_declaration->parent_scope == scope) {
@@ -4222,8 +4255,8 @@ warn_redundant_declaration:
                        }
                }
 
-               if (declaration->is_inline)
-                       previous_declaration->is_inline = true;
+               previous_declaration->modifiers |= declaration->modifiers;
+               previous_declaration->is_inline |= declaration->is_inline;
                return previous_declaration;
        } else if (is_type_function(type)) {
                if (is_definition &&
@@ -4633,6 +4666,433 @@ static void check_declarations(void)
        }
 }
 
+static int determine_truth(expression_t const* const cond)
+{
+       return
+               !is_constant_expression(cond) ? 0 :
+               fold_constant(cond) != 0      ? 1 :
+               -1;
+}
+
+static bool noreturn_candidate;
+
+static void check_reachable(statement_t *const stmt)
+{
+       if (stmt->base.reachable)
+               return;
+       if (stmt->kind != STATEMENT_DO_WHILE)
+               stmt->base.reachable = true;
+
+       statement_t *last = stmt;
+       statement_t *next;
+       switch (stmt->kind) {
+               case STATEMENT_INVALID:
+               case STATEMENT_EMPTY:
+               case STATEMENT_DECLARATION:
+               case STATEMENT_ASM:
+                       next = stmt->base.next;
+                       break;
+
+               case STATEMENT_COMPOUND:
+                       next = stmt->compound.statements;
+                       break;
+
+               case STATEMENT_RETURN:
+                       noreturn_candidate = false;
+                       return;
+
+               case STATEMENT_IF: {
+                       if_statement_t const* const ifs = &stmt->ifs;
+                       int            const        val = determine_truth(ifs->condition);
+
+                       if (val >= 0)
+                               check_reachable(ifs->true_statement);
+
+                       if (val > 0)
+                               return;
+
+                       if (ifs->false_statement != NULL) {
+                               check_reachable(ifs->false_statement);
+                               return;
+                       }
+
+                       next = stmt->base.next;
+                       break;
+               }
+
+               case STATEMENT_SWITCH: {
+                       switch_statement_t const *const switchs = &stmt->switchs;
+                       expression_t       const *const expr    = switchs->expression;
+
+                       if (is_constant_expression(expr)) {
+                               long                    const val      = fold_constant(expr);
+                               case_label_statement_t *      defaults = NULL;
+                               for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
+                                       if (i->expression == NULL) {
+                                               defaults = i;
+                                               continue;
+                                       }
+
+                                       expression_t *const case_expr = i->expression;
+                                       if (is_constant_expression(case_expr) &&
+                                           fold_constant(case_expr) == val) {
+                                               check_reachable((statement_t*)i);
+                                               return;
+                                       }
+                               }
+
+                               if (defaults != NULL) {
+                                       check_reachable((statement_t*)defaults);
+                                       return;
+                               }
+                       } else {
+                               bool has_default = false;
+                               for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
+                                       if (i->expression == NULL)
+                                               has_default = true;
+
+                                       check_reachable((statement_t*)i);
+                               }
+
+                               if (has_default)
+                                       return;
+                       }
+
+                       next = stmt->base.next;
+                       break;
+               }
+
+               case STATEMENT_EXPRESSION: {
+                       /* Check for noreturn function call */
+                       expression_t const *const expr = stmt->expression.expression;
+                       if (expr->kind == EXPR_CALL) {
+                               expression_t const *const func = expr->call.function;
+                               if (func->kind == EXPR_REFERENCE) {
+                                       declaration_t const *const decl = func->reference.declaration;
+                                       if (decl != NULL && decl->modifiers & DM_NORETURN) {
+                                               return;
+                                       }
+                               }
+                       }
+
+                       next = stmt->base.next;
+                       break;
+               }
+
+               case STATEMENT_CONTINUE: {
+                       statement_t *parent = stmt;
+                       for (;;) {
+                               parent = parent->base.parent;
+                               if (parent == NULL) /* continue not within loop */
+                                       return;
+
+                               next = parent;
+                               switch (parent->kind) {
+                                       case STATEMENT_WHILE:    goto continue_while;
+                                       case STATEMENT_DO_WHILE: goto continue_do_while;
+                                       case STATEMENT_FOR:      goto continue_for;
+
+                                       default: break;
+                               }
+                       }
+               }
+
+               case STATEMENT_BREAK: {
+                       statement_t *parent = stmt;
+                       for (;;) {
+                               parent = parent->base.parent;
+                               if (parent == NULL) /* break not within loop/switch */
+                                       return;
+
+                               switch (parent->kind) {
+                                       case STATEMENT_SWITCH:
+                                       case STATEMENT_WHILE:
+                                       case STATEMENT_DO_WHILE:
+                                       case STATEMENT_FOR:
+                                               last = parent;
+                                               next = parent->base.next;
+                                               goto found_break_parent;
+
+                                       default: break;
+                               }
+                       }
+found_break_parent:
+                       break;
+               }
+
+               case STATEMENT_GOTO:
+                       next = stmt->gotos.label->init.statement;
+                       if (next == NULL) /* missing label */
+                               return;
+                       break;
+
+               case STATEMENT_LABEL:
+                       next = stmt->label.statement;
+                       break;
+
+               case STATEMENT_CASE_LABEL:
+                       next = stmt->case_label.statement;
+                       break;
+
+               case STATEMENT_WHILE: {
+                       while_statement_t const *const whiles = &stmt->whiles;
+                       int                      const val    = determine_truth(whiles->condition);
+
+                       if (val >= 0)
+                               check_reachable(whiles->body);
+
+                       if (val > 0)
+                               return;
+
+                       next = stmt->base.next;
+                       break;
+               }
+
+               case STATEMENT_DO_WHILE:
+                       next = stmt->do_while.body;
+                       break;
+
+               case STATEMENT_FOR: {
+                       for_statement_t *const fors = &stmt->fors;
+
+                       if (fors->condition_reachable)
+                               return;
+                       fors->condition_reachable = true;
+
+                       expression_t const *const cond = fors->condition;
+                       int          const        val  =
+                               cond == NULL ? 1 : determine_truth(cond);
+
+                       if (val >= 0)
+                               check_reachable(fors->body);
+
+                       if (val > 0)
+                               return;
+
+                       next = stmt->base.next;
+                       break;
+               }
+
+               case STATEMENT_MS_TRY:
+               case STATEMENT_LEAVE:
+                       panic("unimplemented");
+       }
+
+       while (next == NULL) {
+               next = last->base.parent;
+               if (next == NULL) {
+                       noreturn_candidate = false;
+
+                       type_t *const type = current_function->type;
+                       assert(is_type_function(type));
+                       type_t *const ret  = skip_typeref(type->function.return_type);
+                       if (warning.return_type                    &&
+                           !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
+                           !is_sym_main(current_function->symbol)) {
+                               warningf(&stmt->base.source_position,
+                                        "control reaches end of non-void function");
+                       }
+                       return;
+               }
+
+               switch (next->kind) {
+                       case STATEMENT_INVALID:
+                       case STATEMENT_EMPTY:
+                       case STATEMENT_DECLARATION:
+                       case STATEMENT_EXPRESSION:
+                       case STATEMENT_ASM:
+                       case STATEMENT_RETURN:
+                       case STATEMENT_CONTINUE:
+                       case STATEMENT_BREAK:
+                       case STATEMENT_GOTO:
+                       case STATEMENT_LEAVE:
+                               panic("invalid control flow in function");
+
+                       case STATEMENT_COMPOUND:
+                       case STATEMENT_IF:
+                       case STATEMENT_SWITCH:
+                       case STATEMENT_LABEL:
+                       case STATEMENT_CASE_LABEL:
+                               last = next;
+                               next = next->base.next;
+                               break;
+
+                       case STATEMENT_WHILE: {
+continue_while:
+                               if (next->base.reachable)
+                                       return;
+                               next->base.reachable = true;
+
+                               while_statement_t const *const whiles = &next->whiles;
+                               int                      const val    = determine_truth(whiles->condition);
+
+                               if (val >= 0)
+                                       check_reachable(whiles->body);
+
+                               if (val > 0)
+                                       return;
+
+                               last = next;
+                               next = next->base.next;
+                               break;
+                       }
+
+                       case STATEMENT_DO_WHILE: {
+continue_do_while:
+                               if (next->base.reachable)
+                                       return;
+                               next->base.reachable = true;
+
+                               do_while_statement_t const *const dw  = &next->do_while;
+                               int                  const        val = determine_truth(dw->condition);
+
+                               if (val >= 0)
+                                       check_reachable(dw->body);
+
+                               if (val > 0)
+                                       return;
+
+                               last = next;
+                               next = next->base.next;
+                               break;
+                       }
+
+                       case STATEMENT_FOR: {
+continue_for:;
+                               for_statement_t *const fors = &next->fors;
+
+                               fors->step_reachable = true;
+
+                               if (fors->condition_reachable)
+                                       return;
+                               fors->condition_reachable = true;
+
+                               expression_t const *const cond = fors->condition;
+                               int          const        val  =
+                                       cond == NULL ? 1 : determine_truth(cond);
+
+                               if (val >= 0)
+                                       check_reachable(fors->body);
+
+                               if (val > 0)
+                                       return;
+
+                               last = next;
+                               next = next->base.next;
+                               break;
+                       }
+
+                       case STATEMENT_MS_TRY:
+                               panic("unimplemented");
+               }
+       }
+
+       if (next == NULL) {
+               next = stmt->base.parent;
+               if (next == NULL) {
+                       warningf(&stmt->base.source_position,
+                                "control reaches end of non-void function");
+               }
+       }
+
+       check_reachable(next);
+}
+
+static void check_unreachable(statement_t const* const stmt)
+{
+       if (!stmt->base.reachable            &&
+           stmt->kind != STATEMENT_COMPOUND &&
+           stmt->kind != STATEMENT_DO_WHILE &&
+           stmt->kind != STATEMENT_FOR) {
+               warningf(&stmt->base.source_position, "statement is unreachable");
+       }
+
+       switch (stmt->kind) {
+               case STATEMENT_INVALID:
+               case STATEMENT_EMPTY:
+               case STATEMENT_RETURN:
+               case STATEMENT_DECLARATION:
+               case STATEMENT_EXPRESSION:
+               case STATEMENT_CONTINUE:
+               case STATEMENT_BREAK:
+               case STATEMENT_GOTO:
+               case STATEMENT_ASM:
+               case STATEMENT_LEAVE:
+                       break;
+
+               case STATEMENT_COMPOUND:
+                       if (stmt->compound.statements)
+                               check_unreachable(stmt->compound.statements);
+                       break;
+
+               case STATEMENT_IF:
+                       check_unreachable(stmt->ifs.true_statement);
+                       if (stmt->ifs.false_statement != NULL)
+                               check_unreachable(stmt->ifs.false_statement);
+                       break;
+
+               case STATEMENT_SWITCH:
+                       check_unreachable(stmt->switchs.body);
+                       break;
+
+               case STATEMENT_LABEL:
+                       check_unreachable(stmt->label.statement);
+                       break;
+
+               case STATEMENT_CASE_LABEL:
+                       check_unreachable(stmt->case_label.statement);
+                       break;
+
+               case STATEMENT_WHILE:
+                       check_unreachable(stmt->whiles.body);
+                       break;
+
+               case STATEMENT_DO_WHILE:
+                       check_unreachable(stmt->do_while.body);
+                       if (!stmt->base.reachable) {
+                               expression_t const *const cond = stmt->do_while.condition;
+                               if (determine_truth(cond) >= 0) {
+                                       warningf(&cond->base.source_position,
+                                                "condition of do-while-loop is unreachable");
+                               }
+                       }
+                       break;
+
+               case STATEMENT_FOR: {
+                       for_statement_t const* const fors = &stmt->fors;
+
+                       // if init and step are unreachable, cond is unreachable, too
+                       if (!stmt->base.reachable && !fors->step_reachable) {
+                               warningf(&stmt->base.source_position, "statement is unreachable");
+                       } else {
+                               if (!stmt->base.reachable && fors->initialisation != NULL) {
+                                       warningf(&fors->initialisation->base.source_position,
+                                                "initialisation of for-statement is unreachable");
+                               }
+
+                               if (!fors->condition_reachable && fors->condition != NULL) {
+                                       warningf(&fors->condition->base.source_position,
+                                                "condition of for-statement is unreachable");
+                               }
+
+                               if (!fors->step_reachable && fors->step != NULL) {
+                                       warningf(&fors->step->base.source_position,
+                                                "step of for-statement is unreachable");
+                               }
+                       }
+
+                       check_unreachable(stmt->fors.body);
+                       break;
+               }
+
+               case STATEMENT_MS_TRY:
+                       panic("unimplemented");
+       }
+
+       if (stmt->base.next)
+               check_unreachable(stmt->base.next);
+}
+
 static void parse_external_declaration(void)
 {
        /* function-definitions and declarations both start with declaration
@@ -4744,12 +5204,30 @@ static void parse_external_declaration(void)
                int            label_stack_top      = label_top();
                declaration_t *old_current_function = current_function;
                current_function                    = declaration;
+               current_parent                      = NULL;
 
-               declaration->init.statement = parse_compound_statement(false);
+               statement_t *const body = parse_compound_statement(false);
+               declaration->init.statement = body;
                first_err = true;
                check_labels();
                check_declarations();
+               if (warning.return_type      ||
+                   warning.unreachable_code ||
+                   (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
+                       noreturn_candidate = true;
+                       check_reachable(body);
+                       if (warning.unreachable_code)
+                               check_unreachable(body);
+                       if (warning.missing_noreturn &&
+                           noreturn_candidate       &&
+                           !(declaration->modifiers & DM_NORETURN)) {
+                               warningf(&body->base.source_position,
+                                        "function '%#T' is candidate for attribute 'noreturn'",
+                                        type, declaration->symbol);
+                       }
+               }
 
+               assert(current_parent   == NULL);
                assert(current_function == declaration);
                current_function = old_current_function;
                label_pop_to(label_stack_top);
@@ -5327,18 +5805,20 @@ static expression_t *parse_reference(void)
        declaration->used = true;
 
        /* check for deprecated functions */
-       if (declaration->deprecated != 0) {
-               const char *prefix = "";
-               if (is_type_function(declaration->type))
-                       prefix = "function ";
+       if (warning.deprecated_declarations &&
+           declaration->modifiers & DM_DEPRECATED) {
+               char const *const prefix = is_type_function(declaration->type) ?
+                       "function" : "variable";
 
                if (declaration->deprecated_string != NULL) {
                        warningf(&source_position,
-                               "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
+                               "%s '%Y' is deprecated (declared %P): \"%s\"", prefix,
+                               declaration->symbol, &declaration->source_position,
                                declaration->deprecated_string);
                } else {
                        warningf(&source_position,
-                               "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
+                               "%s '%Y' is deprecated (declared %P)", prefix,
+                               declaration->symbol, &declaration->source_position);
                }
        }
 
@@ -6506,7 +6986,7 @@ static void semantic_unexpr_arithmetic(unary_expression_t *expression)
        expression->base.type = orig_type;
 }
 
-static void semantic_unexpr_scalar(unary_expression_t *expression)
+static void semantic_not(unary_expression_t *expression)
 {
        type_t *const orig_type = expression->value->base.type;
        type_t *const type      = skip_typeref(orig_type);
@@ -6517,7 +6997,7 @@ static void semantic_unexpr_scalar(unary_expression_t *expression)
                return;
        }
 
-       expression->base.type = orig_type;
+       expression->base.type = type_int;
 }
 
 static void semantic_unexpr_integer(unary_expression_t *expression)
@@ -6606,7 +7086,7 @@ CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
                                semantic_unexpr_arithmetic)
 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
-                               semantic_unexpr_scalar)
+                               semantic_not)
 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
                                semantic_dereference)
 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
@@ -7650,11 +8130,14 @@ static statement_t *parse_case_statement(void)
 {
        eat(T_case);
 
-       statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
+       statement_t       *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
+       source_position_t *const pos       = &statement->base.source_position;
 
-       statement->base.source_position  = token.source_position;
+       *pos                             = token.source_position;
        statement->case_label.expression = parse_expression();
 
+       PUSH_PARENT(statement);
+
        if (c_mode & _GNUC) {
                if (token.type == T_DOTDOTDOT) {
                        next_token();
@@ -7665,27 +8148,42 @@ static statement_t *parse_case_statement(void)
        expect(':');
 
        if (! is_constant_expression(statement->case_label.expression)) {
-               errorf(&statement->base.source_position,
-                      "case label does not reduce to an integer constant");
-       } else {
-               /* TODO: check if the case label is already known */
-               if (current_switch != NULL) {
-                       /* link all cases into the switch statement */
-                       if (current_switch->last_case == NULL) {
-                               current_switch->first_case =
-                               current_switch->last_case  = &statement->case_label;
-                       } else {
-                               current_switch->last_case->next = &statement->case_label;
-                       }
+               errorf(pos, "case label does not reduce to an integer constant");
+       } else if (current_switch != NULL) {
+               /* Check for duplicate case values */
+               /* FIXME slow */
+               long const val = fold_constant(statement->case_label.expression);
+               for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
+                       expression_t const* const e = l->expression;
+                       if (e == NULL || !is_constant_expression(e) || fold_constant(e) != val)
+                               continue;
+
+                       errorf(pos, "duplicate case value (previously used %P)",
+                              &l->base.source_position);
+                       break;
+               }
+
+               /* link all cases into the switch statement */
+               if (current_switch->last_case == NULL) {
+                       current_switch->first_case      = &statement->case_label;
                } else {
-                       errorf(&statement->base.source_position,
-                              "case label not within a switch statement");
+                       current_switch->last_case->next = &statement->case_label;
                }
+               current_switch->last_case = &statement->case_label;
+       } else {
+               errorf(pos, "case label not within a switch statement");
        }
-       statement->case_label.statement = parse_statement();
 
+       statement_t *const inner_stmt = parse_statement();
+       statement->case_label.statement = inner_stmt;
+       if (inner_stmt->kind == STATEMENT_DECLARATION) {
+               errorf(&inner_stmt->base.source_position, "declaration after case label");
+       }
+
+       POP_PARENT;
        return statement;
 end_error:
+       POP_PARENT;
        return create_invalid_statement();
 }
 
@@ -7711,9 +8209,10 @@ static statement_t *parse_default_statement(void)
        eat(T_default);
 
        statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
-
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement);
+
        expect(':');
        if (current_switch != NULL) {
                const case_label_statement_t *def_label = find_default_label(current_switch);
@@ -7723,20 +8222,27 @@ static statement_t *parse_default_statement(void)
                } else {
                        /* link all cases into the switch statement */
                        if (current_switch->last_case == NULL) {
-                               current_switch->first_case =
-                                       current_switch->last_case  = &statement->case_label;
+                               current_switch->first_case      = &statement->case_label;
                        } else {
                                current_switch->last_case->next = &statement->case_label;
                        }
+                       current_switch->last_case = &statement->case_label;
                }
        } else {
                errorf(&statement->base.source_position,
                        "'default' label not within a switch statement");
        }
-       statement->case_label.statement = parse_statement();
 
+       statement_t *const inner_stmt = parse_statement();
+       statement->case_label.statement = inner_stmt;
+       if (inner_stmt->kind == STATEMENT_DECLARATION) {
+               errorf(&inner_stmt->base.source_position, "declaration after default label");
+       }
+
+       POP_PARENT;
        return statement;
 end_error:
+       POP_PARENT;
        return create_invalid_statement();
 }
 
@@ -7777,6 +8283,12 @@ static statement_t *parse_label_statement(void)
 
        declaration_t *label = get_label(symbol);
 
+       statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
+       statement->base.source_position = token.source_position;
+       statement->label.label          = label;
+
+       PUSH_PARENT(statement);
+
        /* if source position is already set then the label is defined twice,
         * otherwise it was just mentioned in a goto so far */
        if (label->source_position.input_name != NULL) {
@@ -7784,13 +8296,9 @@ static statement_t *parse_label_statement(void)
                       symbol, &label->source_position);
        } else {
                label->source_position = token.source_position;
+               label->init.statement  = statement;
        }
 
-       statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
-
-       statement->base.source_position = token.source_position;
-       statement->label.label          = label;
-
        eat(':');
 
        if (token.type == '}') {
@@ -7809,7 +8317,11 @@ static statement_t *parse_label_statement(void)
                statement->label.statement = create_empty_statement();
                next_token();
        } else {
-               statement->label.statement = parse_statement();
+               statement_t *const inner_stmt = parse_statement();
+               statement->label.statement = inner_stmt;
+               if (inner_stmt->kind == STATEMENT_DECLARATION) {
+                       errorf(&inner_stmt->base.source_position, "declaration after label");
+               }
        }
 
        /* remember the labels in a list for later checking */
@@ -7820,6 +8332,7 @@ static statement_t *parse_label_statement(void)
        }
        label_last = &statement->label;
 
+       POP_PARENT;
        return statement;
 }
 
@@ -7833,6 +8346,8 @@ static statement_t *parse_if(void)
        statement_t *statement          = allocate_statement_zero(STATEMENT_IF);
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement);
+
        expect('(');
        add_anchor_token(')');
        statement->ifs.condition = parse_expression();
@@ -7848,8 +8363,10 @@ static statement_t *parse_if(void)
                statement->ifs.false_statement = parse_statement();
        }
 
+       POP_PARENT;
        return statement;
 end_error:
+       POP_PARENT;
        return create_invalid_statement();
 }
 
@@ -7863,6 +8380,8 @@ static statement_t *parse_switch(void)
        statement_t *statement          = allocate_statement_zero(STATEMENT_SWITCH);
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement);
+
        expect('(');
        expression_t *const expr = parse_expression();
        type_t       *      type = skip_typeref(expr->base.type);
@@ -7886,8 +8405,10 @@ static statement_t *parse_switch(void)
                warningf(&statement->base.source_position, "switch has no default case");
        }
 
+       POP_PARENT;
        return statement;
 end_error:
+       POP_PARENT;
        return create_invalid_statement();
 }
 
@@ -7912,6 +8433,8 @@ static statement_t *parse_while(void)
        statement_t *statement          = allocate_statement_zero(STATEMENT_WHILE);
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement);
+
        expect('(');
        add_anchor_token(')');
        statement->whiles.condition = parse_expression();
@@ -7920,8 +8443,10 @@ static statement_t *parse_while(void)
 
        statement->whiles.body = parse_loop_body(statement);
 
+       POP_PARENT;
        return statement;
 end_error:
+       POP_PARENT;
        return create_invalid_statement();
 }
 
@@ -7933,9 +8458,10 @@ static statement_t *parse_do(void)
        eat(T_do);
 
        statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
-
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement)
+
        add_anchor_token(T_while);
        statement->do_while.body = parse_loop_body(statement);
        rem_anchor_token(T_while);
@@ -7948,8 +8474,10 @@ static statement_t *parse_do(void)
        expect(')');
        expect(';');
 
+       POP_PARENT;
        return statement;
 end_error:
+       POP_PARENT;
        return create_invalid_statement();
 }
 
@@ -7963,6 +8491,8 @@ static statement_t *parse_for(void)
        statement_t *statement          = allocate_statement_zero(STATEMENT_FOR);
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement);
+
        int      top        = environment_top();
        scope_t *last_scope = scope;
        set_scope(&statement->fors.scope);
@@ -8010,9 +8540,11 @@ static statement_t *parse_for(void)
        set_scope(last_scope);
        environment_pop_to(top);
 
+       POP_PARENT;
        return statement;
 
 end_error:
+       POP_PARENT;
        rem_anchor_token(')');
        assert(scope == &statement->fors.scope);
        set_scope(last_scope);
@@ -8424,9 +8956,10 @@ static statement_t *parse_statement(void)
 static statement_t *parse_compound_statement(bool inside_expression_statement)
 {
        statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
-
        statement->base.source_position = token.source_position;
 
+       PUSH_PARENT(statement);
+
        eat('{');
        add_anchor_token('}');
 
@@ -8436,6 +8969,7 @@ static statement_t *parse_compound_statement(bool inside_expression_statement)
 
        statement_t *last_statement = NULL;
 
+       bool only_decls_so_far = true;
        while (token.type != '}' && token.type != T_EOF) {
                statement_t *sub_statement = intern_parse_statement();
                if (is_invalid_statement(sub_statement)) {
@@ -8445,6 +8979,15 @@ static statement_t *parse_compound_statement(bool inside_expression_statement)
                        continue;
                }
 
+               if (warning.declaration_after_statement) {
+                       if (sub_statement->kind != STATEMENT_DECLARATION) {
+                               only_decls_so_far = false;
+                       } else if (!only_decls_so_far) {
+                               warningf(&sub_statement->base.source_position,
+                                        "ISO C90 forbids mixed declarations and code");
+                       }
+               }
+
                if (last_statement != NULL) {
                        last_statement->base.next = sub_statement;
                } else {
@@ -8489,6 +9032,7 @@ end_error:
        set_scope(last_scope);
        environment_pop_to(top);
 
+       POP_PARENT;
        return statement;
 }
 
@@ -8568,22 +9112,32 @@ end_error:;
  */
 static void parse_translation_unit(void)
 {
-       while (token.type != T_EOF) {
-               switch (token.type) {
-                       case ';':
-                               /* TODO error in strict mode */
-                               warningf(HERE, "stray ';' outside of function");
-                               next_token();
-                               break;
+       for (;;) switch (token.type) {
+               DECLARATION_START
+               case T_IDENTIFIER:
+               case T___extension__:
+                       parse_external_declaration();
+                       break;
 
-                       case T_asm:
-                               parse_global_asm();
-                               break;
+               case T_asm:
+                       parse_global_asm();
+                       break;
 
-                       default:
-                               parse_external_declaration();
-                               break;
-               }
+               case T_EOF:
+                       return;
+
+               case ';':
+                       /* TODO error in strict mode */
+                       warningf(HERE, "stray ';' outside of function");
+                       next_token();
+                       break;
+
+               default:
+                       errorf(HERE, "stray %K outside of function", &token);
+                       if (token.type == '(' || token.type == '{' || token.type == '[')
+                               eat_until_matching_token(token.type);
+                       next_token();
+                       break;
        }
 }