precedence of shift was wrong
[cparser] / parser.c
index 1678418..af9b0a0 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -132,11 +132,11 @@ static const symbol_t *sym_noalias    = NULL;
 static unsigned char token_anchor_set[T_LAST_TOKEN];
 
 /** The current source position. */
-#define HERE &token.source_position
+#define HERE (&token.source_position)
 
 static type_t *type_valist;
 
-static statement_t *parse_compound_statement(void);
+static statement_t *parse_compound_statement(bool inside_expression_statement);
 static statement_t *parse_statement(void);
 
 static expression_t *parse_sub_expression(unsigned precedence);
@@ -366,6 +366,8 @@ static size_t get_type_struct_size(type_kind_t kind)
 {
        static const size_t sizes[] = {
                [TYPE_ATOMIC]          = sizeof(atomic_type_t),
+               [TYPE_COMPLEX]         = sizeof(complex_type_t),
+               [TYPE_IMAGINARY]       = sizeof(imaginary_type_t),
                [TYPE_BITFIELD]        = sizeof(bitfield_type_t),
                [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
                [TYPE_COMPOUND_UNION]  = sizeof(compound_type_t),
@@ -488,6 +490,18 @@ static void add_anchor_token(int token_type) {
        ++token_anchor_set[token_type];
 }
 
+static int save_and_reset_anchor_state(int token_type) {
+       assert(0 <= token_type && token_type < T_LAST_TOKEN);
+       int count = token_anchor_set[token_type];
+       token_anchor_set[token_type] = 0;
+       return count;
+}
+
+static void restore_anchor_state(int token_type, int count) {
+       assert(0 <= token_type && token_type < T_LAST_TOKEN);
+       token_anchor_set[token_type] = count;
+}
+
 /**
  * Remove a token from the token anchor set (a multi-set).
  */
@@ -567,8 +581,7 @@ static void eat_block(void) {
 }
 
 /**
- * eat all token until a ';' is reached
- * or a stop token is found.
+ * eat all token until a ';' is reached or a stop token is found.
  */
 static void eat_statement(void) {
        eat_until_matching_token(';');
@@ -581,7 +594,11 @@ static void eat_statement(void) {
 /**
  * Report a parse error because an expected token was not found.
  */
-static void parse_error_expected(const char *message, ...)
+static
+#if defined __GNUC__ && __GNUC__ >= 4
+__attribute__((sentinel))
+#endif
+void parse_error_expected(const char *message, ...)
 {
        if(message != NULL) {
                errorf(HERE, "%s", message);
@@ -607,7 +624,8 @@ static void type_error(const char *msg, const source_position_t *source_position
 static void type_error_incompatible(const char *msg,
                const source_position_t *source_position, type_t *type1, type_t *type2)
 {
-       errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
+       errorf(source_position, "%s, incompatible types: '%T' - '%T'",
+              msg, type1, type2);
 }
 
 /**
@@ -615,16 +633,18 @@ static void type_error_incompatible(const char *msg,
  * If not, generate an error, eat the current statement,
  * and goto the end_error label.
  */
-#define expect(expected)                           \
-       do {                                           \
-    if(UNLIKELY(token.type != (expected))) {       \
-        parse_error_expected(NULL, (expected), 0); \
-               add_anchor_token(expected);                \
-        eat_until_anchor();                        \
-               rem_anchor_token(expected);                \
-        goto end_error;                            \
-    }                                              \
-    next_token();                                  \
+#define expect(expected)                              \
+       do {                                              \
+    if(UNLIKELY(token.type != (expected))) {          \
+        parse_error_expected(NULL, (expected), NULL); \
+               add_anchor_token(expected);                   \
+        eat_until_anchor();                           \
+        if (token.type == expected)                   \
+               next_token();                             \
+               rem_anchor_token(expected);                   \
+        goto end_error;                               \
+    }                                                 \
+    next_token();                                     \
        } while(0)
 
 static void set_scope(scope_t *new_scope)
@@ -771,7 +791,7 @@ static void label_pop_to(size_t new_top)
 static int get_rank(const type_t *type)
 {
        assert(!is_typeref(type));
-       /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
+       /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
         * and esp. footnote 108). However we can't fold constants (yet), so we
         * can't decide whether unsigned int is possible, while int always works.
         * (unsigned int would be preferable when possible... for stuff like
@@ -786,7 +806,7 @@ static int get_rank(const type_t *type)
 static type_t *promote_integer(type_t *type)
 {
        if(type->kind == TYPE_BITFIELD)
-               type = type->bitfield.base;
+               type = type->bitfield.base_type;
 
        if(get_rank(type) < ATOMIC_TYPE_INT)
                type = type_int;
@@ -933,7 +953,8 @@ static expression_t *parse_constant_expression(void)
        expression_t *result = parse_sub_expression(7);
 
        if(!is_constant_expression(result)) {
-               errorf(&result->base.source_position, "expression '%E' is not constant\n", result);
+               errorf(&result->base.source_position,
+                      "expression '%E' is not constant\n", result);
        }
 
        return result;
@@ -1041,6 +1062,7 @@ static const char *gnu_attribute_names[GNU_AK_LAST] = {
        [GNU_AK_TLS_MODEL]              = "tls_model",
        [GNU_AK_VISIBILITY]             = "visibility",
        [GNU_AK_REGPARM]                = "regparm",
+       [GNU_AK_MODE]                   = "mode",
        [GNU_AK_MODEL]                  = "model",
        [GNU_AK_TRAP_EXIT]              = "trap_exit",
        [GNU_AK_SP_SWITCH]              = "sp_switch",
@@ -1068,10 +1090,12 @@ static int strcmp_underscore(const char *s1, const char *s2) {
  */
 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
        gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
-       attribute->kind           = kind;
-       attribute->next           = NULL;
-       attribute->invalid        = false;
-       attribute->have_arguments = false;
+       attribute->kind            = kind;
+       attribute->next            = NULL;
+       attribute->invalid         = false;
+       attribute->have_arguments  = false;
+
+       return attribute;
        return attribute;
 }
 
@@ -1115,10 +1139,13 @@ end_error:
 /**
  * parse one string literal argument.
  */
-static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute, string_t *string) {
+static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
+                                           string_t *string)
+{
        add_anchor_token('(');
        if(token.type != T_STRING_LITERAL) {
-               parse_error_expected("while parsing attribute directive", T_STRING_LITERAL, 0);
+               parse_error_expected("while parsing attribute directive",
+                                    T_STRING_LITERAL, NULL);
                goto end_error;
        }
        *string = parse_string_literals();
@@ -1200,6 +1227,21 @@ static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
        attribute->invalid = true;
 }
 
+static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
+{
+       /* TODO: find out what is allowed here... */
+
+       /* at least: byte, word, pointer, list of machine modes
+        * __XXX___ is interpreted as XXX */
+       add_anchor_token(')');
+       expect(T_IDENTIFIER);
+       rem_anchor_token(')');
+       expect(')');
+       return;
+end_error:
+       attribute->invalid = true;
+}
+
 /**
  * parse one interrupt argument.
  */
@@ -1238,7 +1280,7 @@ static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
        int i;
 
        if(token.type != T_IDENTIFIER) {
-               parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, 0);
+               parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
                goto end_error;
        }
        const char *name = token.v.symbol->string;
@@ -1373,7 +1415,7 @@ static void parse_gnu_attribute(gnu_attribute_t **attributes)
                                /* __attribute__((cdecl)), WITH ms mode */
                                name = "cdecl";
                        } else if(token.type != T_IDENTIFIER) {
-                               parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, 0);
+                               parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
                                break;
                        }
                        const symbol_t *sym = token.v.symbol;
@@ -1524,8 +1566,18 @@ static void parse_gnu_attribute(gnu_attribute_t **attributes)
                                        if(!attribute->have_arguments) {
                                                /* should have arguments */
                                                errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
-                                       } else
+                                       } else {
                                                parse_gnu_attribute_model_arg(attribute);
+                                       }
+                                       break;
+                               case GNU_AK_MODE:
+                                       if(!attribute->have_arguments) {
+                                               /* should have arguments */
+                                               errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
+                                       } else {
+                                               parse_gnu_attribute_mode_arg(attribute);
+                                       }
+                                       break;
                                case GNU_AK_INTERRUPT:
                                        /* may have one string argument */
                                        if(attribute->have_arguments)
@@ -1577,7 +1629,7 @@ static void parse_attributes(gnu_attribute_t **attributes)
                        expect('(');
                        if(token.type != T_STRING_LITERAL) {
                                parse_error_expected("while parsing assembler attribute",
-                                                    T_STRING_LITERAL, 0);
+                                                    T_STRING_LITERAL, NULL);
                                eat_until_matching_token('(');
                                break;
                        } else {
@@ -1618,7 +1670,7 @@ static designator_t *parse_designation(void)
                        next_token();
                        if(token.type != T_IDENTIFIER) {
                                parse_error_expected("while parsing designator",
-                                                    T_IDENTIFIER, 0);
+                                                    T_IDENTIFIER, NULL);
                                return NULL;
                        }
                        designator->symbol = token.v.symbol;
@@ -2064,7 +2116,7 @@ static void skip_until(int type) {
 }
 
 /**
- * skip any {...} blocks until a closing braket is reached.
+ * skip any {...} blocks until a closing bracket is reached.
  */
 static void skip_initializers(void)
 {
@@ -2273,14 +2325,14 @@ error_parse_next:
               len * sizeof(initializers[0]));
 
        DEL_ARR_F(initializers);
-       ascend_to(path, top_path_level);
+       ascend_to(path, top_path_level+1);
 
        return result;
 
 end_error:
        skip_initializers();
        DEL_ARR_F(initializers);
-       ascend_to(path, top_path_level);
+       ascend_to(path, top_path_level+1);
        return NULL;
 }
 
@@ -2389,10 +2441,10 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
        } else if(token.type != '{') {
                if(is_struct) {
                        parse_error_expected("while parsing struct type specifier",
-                                            T_IDENTIFIER, '{', 0);
+                                            T_IDENTIFIER, '{', NULL);
                } else {
                        parse_error_expected("while parsing union type specifier",
-                                            T_IDENTIFIER, '{', 0);
+                                            T_IDENTIFIER, '{', NULL);
                }
 
                return NULL;
@@ -2404,7 +2456,7 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
                        (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
                declaration->source_position = token.source_position;
                declaration->symbol          = symbol;
-               declaration->parent_scope  = scope;
+               declaration->parent_scope    = scope;
                if (symbol != NULL) {
                        environment_push(declaration);
                }
@@ -2412,13 +2464,13 @@ static declaration_t *parse_compound_type_specifier(bool is_struct)
        }
 
        if(token.type == '{') {
-               if(declaration->init.is_defined) {
+               if (declaration->init.complete) {
                        assert(symbol != NULL);
                        errorf(HERE, "multiple definitions of '%s %Y'",
                               is_struct ? "struct" : "union", symbol);
                        declaration->scope.declarations = NULL;
                }
-               declaration->init.is_defined = true;
+               declaration->init.complete = true;
 
                parse_compound_type_entries(declaration);
                parse_attributes(&attributes);
@@ -2440,7 +2492,7 @@ static void parse_enum_entries(type_t *const enum_type)
        add_anchor_token('}');
        do {
                if(token.type != T_IDENTIFIER) {
-                       parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
+                       parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
                        eat_block();
                        rem_anchor_token('}');
                        return;
@@ -2491,7 +2543,7 @@ static type_t *parse_enum_specifier(void)
                declaration = get_declaration(symbol, NAMESPACE_ENUM);
        } else if(token.type != '{') {
                parse_error_expected("while parsing enum type specifier",
-                                    T_IDENTIFIER, '{', 0);
+                                    T_IDENTIFIER, '{', NULL);
                return NULL;
        } else {
                declaration = NULL;
@@ -2510,14 +2562,14 @@ static type_t *parse_enum_specifier(void)
        type->enumt.declaration = declaration;
 
        if(token.type == '{') {
-               if(declaration->init.is_defined) {
+               if(declaration->init.complete) {
                        errorf(HERE, "multiple definitions of enum %Y", symbol);
                }
                if (symbol != NULL) {
                        environment_push(declaration);
                }
                append_declaration(declaration);
-               declaration->init.is_defined = 1;
+               declaration->init.complete = true;
 
                parse_enum_entries(type);
                parse_attributes(&attributes);
@@ -2607,10 +2659,8 @@ typedef enum {
        SPECIFIER_INT32     = 1 << 13,
        SPECIFIER_INT64     = 1 << 14,
        SPECIFIER_INT128    = 1 << 15,
-#ifdef PROVIDE_COMPLEX
        SPECIFIER_COMPLEX   = 1 << 16,
        SPECIFIER_IMAGINARY = 1 << 17,
-#endif
 } specifiers_t;
 
 static type_t *create_builtin_type(symbol_t *const symbol,
@@ -2621,7 +2671,7 @@ static type_t *create_builtin_type(symbol_t *const symbol,
        type->builtin.real_type = real_type;
 
        type_t *result = typehash_insert(type);
-       if (type != result) {
+       if(type != result) {
                free_type(type);
        }
 
@@ -2631,8 +2681,8 @@ static type_t *create_builtin_type(symbol_t *const symbol,
 static type_t *get_typedef_type(symbol_t *symbol)
 {
        declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
-       if(declaration == NULL
-                       || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
+       if(declaration == NULL ||
+          declaration->storage_class != STORAGE_CLASS_TYPEDEF)
                return NULL;
 
        type_t *type               = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
@@ -2750,7 +2800,7 @@ static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *spe
                                        }
                                }
                                next_token();
-                           if(token.type == ',') {
+                               if(token.type == ',') {
                                        next_token();
                                        continue;
                                }
@@ -2903,10 +2953,9 @@ static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
                MATCH_SPECIFIER(T__int32,     SPECIFIER_INT32,     "_int32")
                MATCH_SPECIFIER(T__int64,     SPECIFIER_INT64,     "_int64")
                MATCH_SPECIFIER(T__int128,    SPECIFIER_INT128,    "_int128")
-#ifdef PROVIDE_COMPLEX
                MATCH_SPECIFIER(T__Complex,   SPECIFIER_COMPLEX,   "_Complex")
                MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
-#endif
+
                case T__forceinline:
                        /* only in microsoft mode */
                        specifiers->decl_modifiers |= DM_FORCEINLINE;
@@ -3093,26 +3142,18 @@ finish_specifiers:
                case SPECIFIER_BOOL:
                        atomic_type = ATOMIC_TYPE_BOOL;
                        break;
-#ifdef PROVIDE_COMPLEX
                case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
-                       atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
-                       break;
-               case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
-                       atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
-                       break;
-               case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
-                       atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
-                       break;
                case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
-                       atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
+                       atomic_type = ATOMIC_TYPE_FLOAT;
                        break;
+               case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
                case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
-                       atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
+                       atomic_type = ATOMIC_TYPE_DOUBLE;
                        break;
+               case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
                case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
-                       atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
+                       atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
                        break;
-#endif
                default:
                        /* invalid specifier combination, give an error message */
                        if(type_specifiers == 0) {
@@ -3136,9 +3177,19 @@ finish_specifiers:
                        atomic_type = ATOMIC_TYPE_INVALID;
                }
 
-               type               = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
-               type->atomic.akind = atomic_type;
-               newtype            = 1;
+               if(type_specifiers & SPECIFIER_COMPLEX &&
+                  atomic_type != ATOMIC_TYPE_INVALID) {
+                       type                = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
+                       type->complex.akind = atomic_type;
+               } else if(type_specifiers & SPECIFIER_IMAGINARY &&
+                         atomic_type != ATOMIC_TYPE_INVALID) {
+                       type                  = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
+                       type->imaginary.akind = atomic_type;
+               } else {
+                       type               = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
+                       type->atomic.akind = atomic_type;
+               }
+               newtype = 1;
        } else {
                if(type_specifiers != 0) {
                        errorf(HERE, "multiple datatypes in declaration");
@@ -3199,8 +3250,9 @@ static declaration_t *parse_identifier_list(void)
                }
                last_declaration = declaration;
 
-               if(token.type != ',')
+               if (token.type != ',') {
                        break;
+               }
                next_token();
        } while(token.type == T_IDENTIFIER);
 
@@ -3253,24 +3305,30 @@ static declaration_t *parse_parameter(void)
 
 static declaration_t *parse_parameters(function_type_t *type)
 {
+       declaration_t *declarations = NULL;
+
+       eat('(');
+       add_anchor_token(')');
+       int saved_comma_state = save_and_reset_anchor_state(',');
+
        if(token.type == T_IDENTIFIER) {
                symbol_t *symbol = token.v.symbol;
                if(!is_typedef_symbol(symbol)) {
                        type->kr_style_parameters = true;
-                       return parse_identifier_list();
+                       declarations = parse_identifier_list();
+                       goto parameters_finished;
                }
        }
 
        if(token.type == ')') {
                type->unspecified_parameters = 1;
-               return NULL;
+               goto parameters_finished;
        }
        if(token.type == T_void && look_ahead(1)->type == ')') {
                next_token();
-               return NULL;
+               goto parameters_finished;
        }
 
-       declaration_t        *declarations = NULL;
        declaration_t        *declaration;
        declaration_t        *last_declaration = NULL;
        function_parameter_t *parameter;
@@ -3281,7 +3339,7 @@ static declaration_t *parse_parameters(function_type_t *type)
                case T_DOTDOTDOT:
                        next_token();
                        type->variadic = 1;
-                       return declarations;
+                       goto parameters_finished;
 
                case T_IDENTIFIER:
                case T___extension__:
@@ -3304,12 +3362,25 @@ static declaration_t *parse_parameters(function_type_t *type)
                        break;
 
                default:
-                       return declarations;
+                       goto parameters_finished;
+               }
+               if (token.type != ',') {
+                       goto parameters_finished;
                }
-               if(token.type != ',')
-                       return declarations;
                next_token();
        }
+
+
+parameters_finished:
+       rem_anchor_token(')');
+       expect(')');
+
+       restore_anchor_state(',', saved_comma_state);
+       return declarations;
+
+end_error:
+       restore_anchor_state(',', saved_comma_state);
+       return NULL;
 }
 
 typedef enum {
@@ -3404,9 +3475,6 @@ end_error:
 
 static construct_type_t *parse_function_declarator(declaration_t *declaration)
 {
-       eat('(');
-       add_anchor_token(')');
-
        type_t *type;
        if(declaration != NULL) {
                type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
@@ -3425,10 +3493,6 @@ static construct_type_t *parse_function_declarator(declaration_t *declaration)
        construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
        construct_function_type->function_type       = type;
 
-       rem_anchor_token(')');
-       expect(')');
-
-end_error:
        return (construct_type_t*) construct_function_type;
 }
 
@@ -3479,7 +3543,7 @@ static construct_type_t *parse_inner_declarator(declaration_t *declaration,
        default:
                if(may_be_abstract)
                        break;
-               parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
+               parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
                /* avoid a loop in the outermost scope, because eat_statement doesn't
                 * eat '}' */
                if(token.type == '}' && current_function == NULL) {
@@ -3630,7 +3694,7 @@ static declaration_t *parse_declarator(
 {
        declaration_t *const declaration    = allocate_declaration_zero();
        declaration->declared_storage_class = specifiers->declared_storage_class;
-       declaration->modifiers              = specifiers->decl_modifiers;
+       declaration->decl_modifiers         = specifiers->decl_modifiers;
        declaration->deprecated             = specifiers->deprecated;
        declaration->deprecated_string      = specifiers->deprecated_string;
        declaration->get_property_sym       = specifiers->get_property_sym;
@@ -3751,11 +3815,15 @@ 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);
+       declaration_t *previous_declaration = get_declaration(symbol, namespc);
+
        type_t *const orig_type = declaration->type;
        type_t *const type      = skip_typeref(orig_type);
        if (is_type_function(type) &&
                        type->function.unspecified_parameters &&
-                       warning.strict_prototypes) {
+                       warning.strict_prototypes &&
+                       previous_declaration == NULL) {
                warningf(&declaration->source_position,
                         "function declaration '%#T' is not a prototype",
                         orig_type, declaration->symbol);
@@ -3765,9 +3833,6 @@ static declaration_t *internal_record_declaration(
                check_type_of_main(declaration, &type->function);
        }
 
-       assert(declaration->symbol != NULL);
-       declaration_t *previous_declaration = get_declaration(symbol, namespc);
-
        assert(declaration != previous_declaration);
        if (previous_declaration != NULL) {
                if (previous_declaration->parent_scope == scope) {
@@ -3859,6 +3924,9 @@ warn_redundant_declaration:
                                        }
                                }
                        }
+
+                       if (declaration->is_inline)
+                               previous_declaration->is_inline = true;
                        return previous_declaration;
                }
        } else if (is_function_definition) {
@@ -3909,18 +3977,18 @@ static void parser_error_multiple_definition(declaration_t *declaration,
 }
 
 static bool is_declaration_specifier(const token_t *token,
-                                     bool only_type_specifiers)
+                                     bool only_specifiers_qualifiers)
 {
        switch(token->type) {
                TYPE_SPECIFIERS
+               TYPE_QUALIFIERS
                        return true;
                case T_IDENTIFIER:
                        return is_typedef_symbol(token->v.symbol);
 
                case T___extension__:
                STORAGE_CLASSES
-               TYPE_QUALIFIERS
-                       return !only_type_specifiers;
+                       return !only_specifiers_qualifiers;
 
                default:
                        return false;
@@ -3978,7 +4046,7 @@ static void parse_anonymous_declaration_rest(
        declaration->type                   = specifiers->type;
        declaration->declared_storage_class = specifiers->declared_storage_class;
        declaration->source_position        = specifiers->source_position;
-       declaration->modifiers              = specifiers->decl_modifiers;
+       declaration->decl_modifiers         = specifiers->decl_modifiers;
 
        if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
                warningf(&declaration->source_position,
@@ -4091,13 +4159,27 @@ static void parse_declaration(parsed_declaration_func finished_declaration)
        }
 }
 
+static type_t *get_default_promoted_type(type_t *orig_type)
+{
+       type_t *result = orig_type;
+
+       type_t *type = skip_typeref(orig_type);
+       if(is_type_integer(type)) {
+               result = promote_integer(type);
+       } else if(type == type_float) {
+               result = type_double;
+       }
+
+       return result;
+}
+
 static void parse_kr_declaration_list(declaration_t *declaration)
 {
        type_t *type = skip_typeref(declaration->type);
-       if(!is_type_function(type))
+       if (!is_type_function(type))
                return;
 
-       if(!type->function.kr_style_parameters)
+       if (!type->function.kr_style_parameters)
                return;
 
        /* push function parameters */
@@ -4106,14 +4188,14 @@ static void parse_kr_declaration_list(declaration_t *declaration)
        set_scope(&declaration->scope);
 
        declaration_t *parameter = declaration->scope.declarations;
-       for( ; parameter != NULL; parameter = parameter->next) {
+       for ( ; parameter != NULL; parameter = parameter->next) {
                assert(parameter->parent_scope == NULL);
                parameter->parent_scope = scope;
                environment_push(parameter);
        }
 
        /* parse declaration list */
-       while(is_declaration_specifier(&token, false)) {
+       while (is_declaration_specifier(&token, false)) {
                parse_declaration(finished_kr_declaration);
        }
 
@@ -4124,7 +4206,6 @@ static void parse_kr_declaration_list(declaration_t *declaration)
 
        /* update function type */
        type_t *new_type = duplicate_type(type);
-       new_type->function.kr_style_parameters = false;
 
        function_parameter_t *parameters     = NULL;
        function_parameter_t *last_parameter = NULL;
@@ -4150,6 +4231,11 @@ static void parse_kr_declaration_list(declaration_t *declaration)
                semantic_parameter(parameter_declaration);
                parameter_type = parameter_declaration->type;
 
+               /*
+                * we need the default promoted types for the function type
+                */
+               parameter_type = get_default_promoted_type(parameter_type);
+
                function_parameter_t *function_parameter
                        = obstack_alloc(type_obst, sizeof(function_parameter[0]));
                memset(function_parameter, 0, sizeof(function_parameter[0]));
@@ -4162,7 +4248,11 @@ static void parse_kr_declaration_list(declaration_t *declaration)
                }
                last_parameter = function_parameter;
        }
-       new_type->function.parameters = parameters;
+
+       /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
+        * prototype */
+       new_type->function.parameters             = parameters;
+       new_type->function.unspecified_parameters = true;
 
        type = typehash_insert(new_type);
        if(type != new_type) {
@@ -4282,7 +4372,7 @@ static void parse_external_declaration(void)
        parse_kr_declaration_list(ndeclaration);
 
        if(token.type != '{') {
-               parse_error_expected("while parsing function definition", '{', 0);
+               parse_error_expected("while parsing function definition", '{', NULL);
                eat_until_matching_token(';');
                return;
        }
@@ -4302,7 +4392,9 @@ static void parse_external_declaration(void)
 
        /* § 6.7.5.3 (14) a function definition with () means no
         * parameters (and not unspecified parameters) */
-       if(type->function.unspecified_parameters) {
+       if(type->function.unspecified_parameters
+                       && type->function.parameters == NULL
+                       && !type->function.kr_style_parameters) {
                type_t *duplicate = duplicate_type(type);
                duplicate->function.unspecified_parameters = false;
 
@@ -4345,7 +4437,7 @@ static void parse_external_declaration(void)
                declaration_t *old_current_function = current_function;
                current_function                    = declaration;
 
-               declaration->init.statement = parse_compound_statement();
+               declaration->init.statement = parse_compound_statement(false);
                first_err = true;
                check_labels();
                check_declarations();
@@ -4361,12 +4453,13 @@ end_of_parse_external_declaration:
        environment_pop_to(top);
 }
 
-static type_t *make_bitfield_type(type_t *base, expression_t *size,
+static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
                                   source_position_t *source_position)
 {
-       type_t *type        = allocate_type_zero(TYPE_BITFIELD, source_position);
-       type->bitfield.base = base;
-       type->bitfield.size = size;
+       type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
+
+       type->bitfield.base_type = base_type;
+       type->bitfield.size      = size;
 
        return type;
 }
@@ -4430,7 +4523,7 @@ static void parse_compound_declarators(declaration_t *struct_declaration,
                        declaration->declared_storage_class = STORAGE_CLASS_NONE;
                        declaration->storage_class          = STORAGE_CLASS_NONE;
                        declaration->source_position        = source_position;
-                       declaration->modifiers              = specifiers->decl_modifiers;
+                       declaration->decl_modifiers         = specifiers->decl_modifiers;
                        declaration->type                   = type;
                } else {
                        declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
@@ -4709,18 +4802,11 @@ static declaration_t *create_implicit_function(symbol_t *symbol,
        declaration->type                   = type;
        declaration->symbol                 = symbol;
        declaration->source_position        = *source_position;
-       declaration->parent_scope           = global_scope;
 
-       scope_t *old_scope = scope;
-       set_scope(global_scope);
-
-       environment_push(declaration);
-       /* prepends the declaration to the global declarations list */
-       declaration->next   = scope->declarations;
-       scope->declarations = declaration;
-
-       assert(scope == global_scope);
-       set_scope(old_scope);
+       bool strict_prototypes_old = warning.strict_prototypes;
+       warning.strict_prototypes  = false;
+       record_declaration(declaration);
+       warning.strict_prototypes = strict_prototypes_old;
 
        return declaration;
 }
@@ -4751,6 +4837,20 @@ static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
        return result;
 }
 
+static type_t *make_function_0_type(type_t *return_type)
+{
+       type_t *type               = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
+       type->function.return_type = return_type;
+       type->function.parameters  = NULL;
+
+       type_t *result = typehash_insert(type);
+       if(result != type) {
+               free_type(type);
+       }
+
+       return result;
+}
+
 /**
  * Creates a function type for some function like builtins.
  *
@@ -4761,6 +4861,8 @@ static type_t *get_builtin_symbol_type(symbol_t *symbol)
        switch(symbol->ID) {
        case T___builtin_alloca:
                return make_function_1_type(type_void_ptr, type_size_t);
+       case T___builtin_huge_val:
+               return make_function_0_type(type_double);
        case T___builtin_nan:
                return make_function_1_type(type_double, type_char_ptr);
        case T___builtin_nanf:
@@ -4785,7 +4887,7 @@ static type_t *automatic_type_conversion(type_t *orig_type)
        if(is_type_array(type)) {
                array_type_t *array_type   = &type->array;
                type_t       *element_type = array_type->element_type;
-               unsigned      qualifiers   = array_type->type.qualifiers;
+               unsigned      qualifiers   = array_type->base.qualifiers;
 
                return make_pointer_type(element_type, qualifiers);
        }
@@ -4850,9 +4952,9 @@ static expression_t *parse_reference(void)
        expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
 
        reference_expression_t *ref = &expression->reference;
-       ref->symbol = token.v.symbol;
+       symbol_t *const symbol = token.v.symbol;
 
-       declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
+       declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
 
        source_position_t source_position = token.source_position;
        next_token();
@@ -4862,13 +4964,13 @@ static expression_t *parse_reference(void)
                        /* an implicitly defined function */
                        if (warning.implicit_function_declaration) {
                                warningf(HERE, "implicit declaration of function '%Y'",
-                                       ref->symbol);
+                                       symbol);
                        }
 
-                       declaration = create_implicit_function(ref->symbol,
+                       declaration = create_implicit_function(symbol,
                                                               &source_position);
                } else {
-                       errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
+                       errorf(HERE, "unknown symbol '%Y' found.", symbol);
                        return create_invalid_expression();
                }
        }
@@ -4968,7 +5070,7 @@ static expression_t *parse_statement_expression(void)
 {
        expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
 
-       statement_t *statement           = parse_compound_statement();
+       statement_t *statement           = parse_compound_statement(true);
        expression->statement.statement  = statement;
        expression->base.source_position = statement->base.source_position;
 
@@ -5093,7 +5195,7 @@ static designator_t *parse_designator(void)
 
        if(token.type != T_IDENTIFIER) {
                parse_error_expected("while parsing member designator",
-                                    T_IDENTIFIER, 0);
+                                    T_IDENTIFIER, NULL);
                return NULL;
        }
        result->symbol = token.v.symbol;
@@ -5105,7 +5207,7 @@ static designator_t *parse_designator(void)
                        next_token();
                        if(token.type != T_IDENTIFIER) {
                                parse_error_expected("while parsing member designator",
-                                                    T_IDENTIFIER, 0);
+                                                    T_IDENTIFIER, NULL);
                                return NULL;
                        }
                        designator_t *designator    = allocate_ast_zero(sizeof(result[0]));
@@ -5467,6 +5569,7 @@ static expression_t *parse_primary_expression(void)
                case T___builtin_nan:
                case T___builtin_nand:
                case T___builtin_nanf:
+               case T___builtin_huge_val:
                case T___builtin_va_end:         return parse_builtin_symbol();
                case T___builtin_isgreater:
                case T___builtin_isgreaterequal:
@@ -5544,7 +5647,7 @@ static expression_t *parse_array_expression(unsigned precedence,
 
        rem_anchor_token(']');
        if(token.type != ']') {
-               parse_error_expected("Problem while parsing array access", ']', 0);
+               parse_error_expected("Problem while parsing array access", ']', NULL);
                return expression;
        }
        next_token();
@@ -5555,20 +5658,50 @@ static expression_t *parse_array_expression(unsigned precedence,
        return expression;
 }
 
-static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
+static expression_t *parse_typeprop(expression_kind_t const kind,
+                                    source_position_t const pos,
+                                    unsigned const precedence)
 {
        expression_t *tp_expression = allocate_expression_zero(kind);
-       tp_expression->base.type    = type_size_t;
+       tp_expression->base.type            = type_size_t;
+       tp_expression->base.source_position = pos;
+
+       char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
 
-       if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
+       if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
                next_token();
                add_anchor_token(')');
-               tp_expression->typeprop.type = parse_typename();
+               type_t* const orig_type = parse_typename();
+               tp_expression->typeprop.type = orig_type;
+
+               type_t const* const type = skip_typeref(orig_type);
+               char const* const wrong_type =
+                       is_type_incomplete(type)    ? "incomplete"          :
+                       type->kind == TYPE_FUNCTION ? "function designator" :
+                       type->kind == TYPE_BITFIELD ? "bitfield"            :
+                       NULL;
+               if (wrong_type != NULL) {
+                       errorf(&pos, "operand of %s expression must not be %s type '%T'",
+                              what, wrong_type, type);
+               }
+
                rem_anchor_token(')');
                expect(')');
        } else {
                expression_t *expression = parse_sub_expression(precedence);
-               expression->base.type    = revert_automatic_type_conversion(expression);
+
+               type_t* const orig_type = revert_automatic_type_conversion(expression);
+               expression->base.type = orig_type;
+
+               type_t const* const type = skip_typeref(orig_type);
+               char const* const wrong_type =
+                       is_type_incomplete(type)    ? "incomplete"          :
+                       type->kind == TYPE_FUNCTION ? "function designator" :
+                       type->kind == TYPE_BITFIELD ? "bitfield"            :
+                       NULL;
+               if (wrong_type != NULL) {
+                       errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
+               }
 
                tp_expression->typeprop.type          = expression->base.type;
                tp_expression->typeprop.tp_expression = expression;
@@ -5581,14 +5714,16 @@ end_error:
 
 static expression_t *parse_sizeof(unsigned precedence)
 {
+       source_position_t pos = *HERE;
        eat(T_sizeof);
-       return parse_typeprop(EXPR_SIZEOF, precedence);
+       return parse_typeprop(EXPR_SIZEOF, pos, precedence);
 }
 
 static expression_t *parse_alignof(unsigned precedence)
 {
+       source_position_t pos = *HERE;
        eat(T___alignof__);
-       return parse_typeprop(EXPR_SIZEOF, precedence);
+       return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
 }
 
 static expression_t *parse_select_expression(unsigned precedence,
@@ -5603,8 +5738,8 @@ static expression_t *parse_select_expression(unsigned precedence,
        expression_t *select    = allocate_expression_zero(EXPR_SELECT);
        select->select.compound = compound;
 
-       if(token.type != T_IDENTIFIER) {
-               parse_error_expected("while parsing select", T_IDENTIFIER, 0);
+       if (token.type != T_IDENTIFIER) {
+               parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
                return select;
        }
        symbol_t *symbol      = token.v.symbol;
@@ -5615,7 +5750,7 @@ static expression_t *parse_select_expression(unsigned precedence,
        type_t *const type      = skip_typeref(orig_type);
 
        type_t *type_left = type;
-       if(is_pointer) {
+       if (is_pointer) {
                if (!is_type_pointer(type)) {
                        if (is_type_valid(type)) {
                                errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
@@ -5637,14 +5772,14 @@ static expression_t *parse_select_expression(unsigned precedence,
 
        declaration_t *const declaration = type_left->compound.declaration;
 
-       if(!declaration->init.is_defined) {
+       if (!declaration->init.complete) {
                errorf(HERE, "request for member '%Y' of incomplete type '%T'",
                       symbol, type_left);
                return create_invalid_expression();
        }
 
        declaration_t *iter = find_compound_entry(declaration, symbol);
-       if(iter == NULL) {
+       if (iter == NULL) {
                errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
                return create_invalid_expression();
        }
@@ -5656,13 +5791,9 @@ static expression_t *parse_select_expression(unsigned precedence,
        select->select.compound_entry = iter;
        select->base.type             = expression_type;
 
-       if(expression_type->kind == TYPE_BITFIELD) {
-               expression_t *extract
-                       = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
-               extract->unary.value = select;
-               extract->base.type   = expression_type->bitfield.base;
-
-               return extract;
+       type_t *skipped = skip_typeref(iter->type);
+       if (skipped->kind == TYPE_BITFIELD) {
+               select->base.type = skipped->bitfield.base_type;
        }
 
        return select;
@@ -5728,17 +5859,20 @@ static expression_t *parse_call_expression(unsigned precedence,
        rem_anchor_token(')');
        expect(')');
 
-       if(function_type != NULL) {
-               function_parameter_t *parameter = function_type->parameters;
-               call_argument_t      *argument  = call->arguments;
+       if(function_type == NULL)
+               return result;
+
+       function_parameter_t *parameter = function_type->parameters;
+       call_argument_t      *argument  = call->arguments;
+       if (!function_type->unspecified_parameters) {
                for( ; parameter != NULL && argument != NULL;
                                parameter = parameter->next, argument = argument->next) {
                        type_t *expected_type = parameter->type;
                        /* TODO report scope in error messages */
                        expression_t *const arg_expr = argument->expression;
                        type_t       *const res_type = semantic_assign(expected_type, arg_expr,
-                                                                      "function call",
-                                                                      &arg_expr->base.source_position);
+                                                                                                                  "function call",
+                                                                                                                  &arg_expr->base.source_position);
                        if (res_type == NULL) {
                                /* TODO improve error message */
                                errorf(&arg_expr->base.source_position,
@@ -5748,37 +5882,26 @@ static expression_t *parse_call_expression(unsigned precedence,
                                argument->expression = create_implicit_cast(argument->expression, expected_type);
                        }
                }
-               /* too few parameters */
-               if(parameter != NULL) {
+
+               if (parameter != NULL) {
                        errorf(HERE, "too few arguments to function '%E'", expression);
-               } else if(argument != NULL) {
-                       /* too many parameters */
-                       if(!function_type->variadic
-                                       && !function_type->unspecified_parameters) {
-                               errorf(HERE, "too many arguments to function '%E'", expression);
-                       } else {
-                               /* do default promotion */
-                               for( ; argument != NULL; argument = argument->next) {
-                                       type_t *type = argument->expression->base.type;
-
-                                       type = skip_typeref(type);
-                                       if(is_type_integer(type)) {
-                                               type = promote_integer(type);
-                                       } else if(type == type_float) {
-                                               type = type_double;
-                                       }
+               } else if (argument != NULL && !function_type->variadic) {
+                       errorf(HERE, "too many arguments to function '%E'", expression);
+               }
+       }
 
-                                       argument->expression
-                                               = create_implicit_cast(argument->expression, type);
-                               }
+       /* do default promotion */
+       for( ; argument != NULL; argument = argument->next) {
+               type_t *type = argument->expression->base.type;
 
-                               check_format(&result->call);
-                       }
-               } else {
-                       check_format(&result->call);
-               }
+               type = get_default_promoted_type(type);
+
+               argument->expression
+                       = create_implicit_cast(argument->expression, type);
        }
 
+       check_format(&result->call);
+
        return result;
 end_error:
        return create_invalid_expression();
@@ -6259,10 +6382,33 @@ static void semantic_comparison(binary_expression_t *expression)
 
        /* TODO non-arithmetic types */
        if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+               /* test for signed vs unsigned compares */
                if (warning.sign_compare &&
                    (expression->base.kind != EXPR_BINARY_EQUAL &&
                     expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
                    (is_type_signed(type_left) != is_type_signed(type_right))) {
+
+                       /* check if 1 of the operands is a constant, in this case we just
+                        * check wether we can safely represent the resulting constant in
+                        * the type of the other operand. */
+                       expression_t *const_expr = NULL;
+                       expression_t *other_expr = NULL;
+
+                       if(is_constant_expression(left)) {
+                               const_expr = left;
+                               other_expr = right;
+                       } else if(is_constant_expression(right)) {
+                               const_expr = right;
+                               other_expr = left;
+                       }
+
+                       if(const_expr != NULL) {
+                               type_t *other_type = skip_typeref(other_expr->base.type);
+                               long    val        = fold_constant(const_expr);
+                               /* TODO: check if val can be represented by other_type */
+                               (void) other_type;
+                               (void) val;
+                       }
                        warningf(&expression->base.source_position,
                                 "comparison between signed and unsigned");
                }
@@ -6291,6 +6437,66 @@ static void semantic_comparison(binary_expression_t *expression)
        expression->base.type = type_int;
 }
 
+/**
+ * Checks if a compound type has constant fields.
+ */
+static bool has_const_fields(const compound_type_t *type)
+{
+       const scope_t       *scope       = &type->declaration->scope;
+       const declaration_t *declaration = scope->declarations;
+
+       for (; declaration != NULL; declaration = declaration->next) {
+               if (declaration->namespc != NAMESPACE_NORMAL)
+                       continue;
+
+               const type_t *decl_type = skip_typeref(declaration->type);
+               if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
+                       return true;
+       }
+       /* TODO */
+       return false;
+}
+
+static bool is_valid_assignment_lhs(expression_t const* const left)
+{
+       type_t *const orig_type_left = revert_automatic_type_conversion(left);
+       type_t *const type_left      = skip_typeref(orig_type_left);
+
+       switch (left->kind)  {
+               case EXPR_REFERENCE:
+               case EXPR_ARRAY_ACCESS:
+               case EXPR_SELECT:
+               case EXPR_UNARY_DEREFERENCE:
+                       break;
+
+               default:
+                       errorf(HERE, "left hand side '%E' of assignment is not an lvalue", left);
+                       return false;
+       }
+
+       if (is_type_array(type_left)) {
+               errorf(HERE, "cannot assign to arrays ('%E')", left);
+               return false;
+       }
+       if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
+               errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
+                      orig_type_left);
+               return false;
+       }
+       if (is_type_incomplete(type_left)) {
+               errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
+                      left, orig_type_left);
+               return false;
+       }
+       if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
+               errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
+                      left, orig_type_left);
+               return false;
+       }
+
+       return true;
+}
+
 static void semantic_arithmetic_assign(binary_expression_t *expression)
 {
        expression_t *left            = expression->left;
@@ -6298,6 +6504,9 @@ static void semantic_arithmetic_assign(binary_expression_t *expression)
        type_t       *orig_type_left  = left->base.type;
        type_t       *orig_type_right = right->base.type;
 
+       if (!is_valid_assignment_lhs(left))
+               return;
+
        type_t *type_left  = skip_typeref(orig_type_left);
        type_t *type_right = skip_typeref(orig_type_right);
 
@@ -6327,6 +6536,9 @@ static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
        type_t       *const type_left       = skip_typeref(orig_type_left);
        type_t       *const type_right      = skip_typeref(orig_type_right);
 
+       if (!is_valid_assignment_lhs(left))
+               return;
+
        if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
                /* combined instructions are tricky. We can't create an implicit cast on
                 * the left side, because we need the uncasted form for the store.
@@ -6365,26 +6577,6 @@ static void semantic_logical_op(binary_expression_t *expression)
        expression->base.type = type_int;
 }
 
-/**
- * Checks if a compound type has constant fields.
- */
-static bool has_const_fields(const compound_type_t *type)
-{
-       const scope_t       *scope       = &type->declaration->scope;
-       const declaration_t *declaration = scope->declarations;
-
-       for (; declaration != NULL; declaration = declaration->next) {
-               if (declaration->namespc != NAMESPACE_NORMAL)
-                       continue;
-
-               const type_t *decl_type = skip_typeref(declaration->type);
-               if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
-                       return true;
-       }
-       /* TODO */
-       return false;
-}
-
 /**
  * Check the semantic restrictions of a binary assign expression.
  */
@@ -6396,27 +6588,8 @@ static void semantic_binexpr_assign(binary_expression_t *expression)
        type_t *type_left = revert_automatic_type_conversion(left);
        type_left         = skip_typeref(orig_type_left);
 
-       /* must be a modifiable lvalue */
-       if (is_type_array(type_left)) {
-               errorf(HERE, "cannot assign to arrays ('%E')", left);
-               return;
-       }
-       if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
-               errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
-                      orig_type_left);
+       if (!is_valid_assignment_lhs(left))
                return;
-       }
-       if(is_type_incomplete(type_left)) {
-               errorf(HERE,
-                      "left-hand side of assignment '%E' has incomplete type '%T'",
-                      left, orig_type_left);
-               return;
-       }
-       if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
-               errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
-                      left, orig_type_left);
-               return;
-       }
 
        type_t *const res_type = semantic_assign(orig_type_left, expression->right,
                        "assignment", &left->base.source_position);
@@ -6512,7 +6685,6 @@ static bool expression_has_effect(const expression_t *const expr)
 
                case EXPR_UNARY_CAST_IMPLICIT:       return true;
                case EXPR_UNARY_ASSUME:              return true;
-               case EXPR_UNARY_BITFIELD_EXTRACT:    return false;
 
                case EXPR_BINARY_ADD:                return false;
                case EXPR_BINARY_SUB:                return false;
@@ -6753,13 +6925,13 @@ static void init_expression_parsers(void)
        register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
                                                              T_MINUSMINUS,     30);
 
-       register_infix_parser(parse_EXPR_BINARY_MUL,          '*',              16);
-       register_infix_parser(parse_EXPR_BINARY_DIV,          '/',              16);
-       register_infix_parser(parse_EXPR_BINARY_MOD,          '%',              16);
-       register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT,    T_LESSLESS,       16);
-       register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT,   T_GREATERGREATER, 16);
-       register_infix_parser(parse_EXPR_BINARY_ADD,          '+',              15);
-       register_infix_parser(parse_EXPR_BINARY_SUB,          '-',              15);
+       register_infix_parser(parse_EXPR_BINARY_MUL,          '*',              17);
+       register_infix_parser(parse_EXPR_BINARY_DIV,          '/',              17);
+       register_infix_parser(parse_EXPR_BINARY_MOD,          '%',              17);
+       register_infix_parser(parse_EXPR_BINARY_ADD,          '+',              16);
+       register_infix_parser(parse_EXPR_BINARY_SUB,          '-',              16);
+       register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT,    T_LESSLESS,       15);
+       register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT,   T_GREATERGREATER, 15);
        register_infix_parser(parse_EXPR_BINARY_LESS,         '<',              14);
        register_infix_parser(parse_EXPR_BINARY_GREATER,      '>',              14);
        register_infix_parser(parse_EXPR_BINARY_LESSEQUAL,    T_LESSEQUAL,      14);
@@ -6825,7 +6997,7 @@ static asm_constraint_t *parse_asm_constraints(void)
                        eat('[');
                        if(token.type != T_IDENTIFIER) {
                                parse_error_expected("while parsing asm constraint",
-                                                    T_IDENTIFIER, 0);
+                                                    T_IDENTIFIER, NULL);
                                return NULL;
                        }
                        constraint->symbol = token.v.symbol;
@@ -7323,7 +7495,7 @@ static statement_t *parse_goto(void)
        eat(T_goto);
 
        if(token.type != T_IDENTIFIER) {
-               parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
+               parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
                eat_statement();
                return NULL;
        }
@@ -7571,10 +7743,6 @@ static statement_t *parse_expression_statement(void)
        expression_t *const expr         = parse_expression();
        statement->expression.expression = expr;
 
-       if (warning.unused_value && !expression_has_effect(expr)) {
-               warningf(&expr->base.source_position, "statement has no effect");
-       }
-
        expect(';');
 
        return statement;
@@ -7594,7 +7762,7 @@ static statement_t *parse_ms_try_statment(void) {
 
        ms_try_statement_t *rem = current_try;
        current_try = &statement->ms_try;
-       statement->ms_try.try_statement = parse_compound_statement();
+       statement->ms_try.try_statement = parse_compound_statement(false);
        current_try = rem;
 
        if(token.type == T___except) {
@@ -7613,12 +7781,12 @@ static statement_t *parse_ms_try_statment(void) {
                statement->ms_try.except_expression = create_implicit_cast(expr, type);
                rem_anchor_token(')');
                expect(')');
-               statement->ms_try.final_statement = parse_compound_statement();
+               statement->ms_try.final_statement = parse_compound_statement(false);
        } else if(token.type == T__finally) {
                eat(T___finally);
-               statement->ms_try.final_statement = parse_compound_statement();
+               statement->ms_try.final_statement = parse_compound_statement(false);
        } else {
-               parse_error_expected("while parsing __try statement", T___except, T___finally, 0);
+               parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
                return create_invalid_statement();
        }
        return statement;
@@ -7628,10 +7796,12 @@ end_error:
 
 /**
  * Parse a statement.
+ * There's also parse_statement() which additionally checks for
+ * "statement has no effect" warnings
  */
-static statement_t *parse_statement(void)
+static statement_t *intern_parse_statement(void)
 {
-       statement_t   *statement = NULL;
+       statement_t *statement = NULL;
 
        /* declaration or statement */
        add_anchor_token(';');
@@ -7649,7 +7819,7 @@ static statement_t *parse_statement(void)
                break;
 
        case '{':
-               statement = parse_compound_statement();
+               statement = parse_compound_statement(false);
                break;
 
        case T_if:
@@ -7743,10 +7913,31 @@ static statement_t *parse_statement(void)
        return statement;
 }
 
+/**
+ * parse a statement and emits "statement has no effect" warning if needed
+ * (This is really a wrapper around intern_parse_statement with check for 1
+ *  single warning. It is needed, because for statement expressions we have
+ *  to avoid the warning on the last statement)
+ */
+static statement_t *parse_statement(void)
+{
+       statement_t *statement = intern_parse_statement();
+
+       if(statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
+               expression_t *expression = statement->expression.expression;
+               if(!expression_has_effect(expression)) {
+                       warningf(&expression->base.source_position,
+                                       "statement has no effect");
+               }
+       }
+
+       return statement;
+}
+
 /**
  * Parse a compound statement.
  */
-static statement_t *parse_compound_statement(void)
+static statement_t *parse_compound_statement(bool inside_expression_statement)
 {
        statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
 
@@ -7762,7 +7953,7 @@ static statement_t *parse_compound_statement(void)
        statement_t *last_statement = NULL;
 
        while(token.type != '}' && token.type != T_EOF) {
-               statement_t *sub_statement = parse_statement();
+               statement_t *sub_statement = intern_parse_statement();
                if(is_invalid_statement(sub_statement)) {
                        /* an error occurred. if we are at an anchor, return */
                        if(at_anchor())
@@ -7789,6 +7980,25 @@ static statement_t *parse_compound_statement(void)
                       "end of file while looking for closing '}'");
        }
 
+       /* look over all statements again to produce no effect warnings */
+       if(warning.unused_value) {
+               statement_t *sub_statement = statement->compound.statements;
+               for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
+                       if(sub_statement->kind != STATEMENT_EXPRESSION)
+                               continue;
+                       /* don't emit a warning for the last expression in an expression
+                        * statement as it has always an effect */
+                       if(inside_expression_statement && sub_statement->base.next == NULL)
+                               continue;
+
+                       expression_t *expression = sub_statement->expression.expression;
+                       if(!expression_has_effect(expression)) {
+                               warningf(&expression->base.source_position,
+                                        "statement has no effect");
+                       }
+               }
+       }
+
 end_error:
        rem_anchor_token('}');
        assert(scope == &statement->compound.scope);