use ir_tarval to calculate case values
[cparser] / parser.c
index 1fc7ed3..869cc25 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -41,6 +41,7 @@
 #include "walk.h"
 #include "warning.h"
 #include "printer.h"
+#include "ast2firm.h"
 #include "adt/bitfiddle.h"
 #include "adt/error.h"
 #include "adt/array.h"
@@ -296,7 +297,6 @@ static size_t get_statement_struct_size(statement_kind_t kind)
                [STATEMENT_GOTO]          = sizeof(goto_statement_t),
                [STATEMENT_LABEL]         = sizeof(label_statement_t),
                [STATEMENT_CASE_LABEL]    = sizeof(case_label_statement_t),
-               [STATEMENT_WHILE]         = sizeof(while_statement_t),
                [STATEMENT_DO_WHILE]      = sizeof(do_while_statement_t),
                [STATEMENT_FOR]           = sizeof(for_statement_t),
                [STATEMENT_ASM]           = sizeof(asm_statement_t),
@@ -323,6 +323,7 @@ static size_t get_expression_struct_size(expression_kind_t kind)
                [EXPR_LITERAL_INTEGER]            = sizeof(literal_expression_t),
                [EXPR_LITERAL_FLOATINGPOINT]      = sizeof(literal_expression_t),
                [EXPR_LITERAL_CHARACTER]          = sizeof(string_literal_expression_t),
+               [EXPR_LITERAL_MS_NOOP]            = sizeof(literal_expression_t),
                [EXPR_STRING_LITERAL]             = sizeof(string_literal_expression_t),
                [EXPR_COMPOUND_LITERAL]           = sizeof(compound_literal_expression_t),
                [EXPR_CALL]                       = sizeof(call_expression_t),
@@ -2308,6 +2309,7 @@ static compound_t *parse_compound_type_specifier(bool is_struct)
        }
 
        if (attributes != NULL) {
+               entity->compound.attributes = attributes;
                handle_entity_attributes(attributes, entity);
        }
 
@@ -4571,8 +4573,7 @@ static void check_declarations(void)
                warn_unused_entity(WARN_UNUSED_PARAMETER, scope->entities, NULL);
        }
        if (is_warn_on(WARN_UNUSED_VARIABLE)) {
-               walk_statements(current_function->statement, check_unused_variables,
-                               NULL);
+               walk_statements(current_function->body, check_unused_variables, NULL);
        }
 }
 
@@ -4789,7 +4790,7 @@ static void check_reachable(statement_t *const stmt)
                                return;
 
                        if (is_constant_expression(expr) == EXPR_CLASS_CONSTANT) {
-                               long                    const val      = fold_constant_to_int(expr);
+                               ir_tarval              *const val      = fold_constant_to_tarval(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) {
@@ -4797,7 +4798,9 @@ static void check_reachable(statement_t *const stmt)
                                                continue;
                                        }
 
-                                       if (i->first_case <= val && val <= i->last_case) {
+                                       if (i->first_case == val || i->last_case == val ||
+                                               ((tarval_cmp(i->first_case, val) & ir_relation_less_equal)
+                                           && (tarval_cmp(val, i->last_case) & ir_relation_less_equal))) {
                                                check_reachable((statement_t*)i);
                                                return;
                                        }
@@ -4842,7 +4845,6 @@ static void check_reachable(statement_t *const stmt)
 
                                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;
 
@@ -4858,7 +4860,6 @@ static void check_reachable(statement_t *const stmt)
 
                                switch (parent->kind) {
                                        case STATEMENT_SWITCH:
-                                       case STATEMENT_WHILE:
                                        case STATEMENT_DO_WHILE:
                                        case STATEMENT_FOR:
                                                last = parent;
@@ -4896,25 +4897,6 @@ found_break_parent:
                        next = stmt->case_label.statement;
                        break;
 
-               case STATEMENT_WHILE: {
-                       while_statement_t const *const whiles = &stmt->whiles;
-                       expression_t      const *const cond   = whiles->condition;
-
-                       if (!expression_returns(cond))
-                               return;
-
-                       int const val = determine_truth(cond);
-
-                       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;
@@ -5019,31 +5001,6 @@ found_break_parent:
                                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;
-                               expression_t      const *const cond   = whiles->condition;
-
-                               if (!expression_returns(cond))
-                                       return;
-
-                               int const val = determine_truth(cond);
-
-                               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)
@@ -5313,7 +5270,7 @@ static void parse_external_declaration(void)
                environment_push(parameter);
        }
 
-       if (function->statement != NULL) {
+       if (function->body != NULL) {
                parser_error_multiple_definition(entity, HERE);
                eat_block();
        } else {
@@ -5330,7 +5287,7 @@ static void parse_external_declaration(void)
                label_anchor = &label_first;
 
                statement_t *const body = parse_compound_statement(false);
-               function->statement = body;
+               function->body = body;
                first_err = true;
                check_labels();
                check_declarations();
@@ -7068,7 +7025,6 @@ static void handle_builtin_argument_restrictions(call_expression_t *call)
                                                   "third argument of '%Y' must be a constant expression",
                                                   call->function->reference.entity->base.symbol);
                                }
-                               locality = rw->next;
                        }
                        break;
                default:
@@ -8886,7 +8842,7 @@ static statement_t *parse_case_statement(void)
                }
                statement->case_label.is_bad = true;
        } else {
-               long const val = fold_constant_to_int(expression);
+               ir_tarval *val = fold_constant_to_tarval(expression);
                statement->case_label.first_case = val;
                statement->case_label.last_case  = val;
        }
@@ -8910,10 +8866,11 @@ static statement_t *parse_case_statement(void)
                                }
                                statement->case_label.is_bad = true;
                        } else {
-                               long const val = fold_constant_to_int(end_range);
+                               ir_tarval *val = fold_constant_to_tarval(end_range);
                                statement->case_label.last_case = val;
 
-                               if (val < statement->case_label.first_case) {
+                               if (tarval_cmp(val, statement->case_label.first_case)
+                                   == ir_relation_less) {
                                        statement->case_label.is_empty_range = true;
                                        warningf(WARN_OTHER, pos, "empty range specified");
                                }
@@ -9018,6 +8975,7 @@ static statement_t *parse_label_statement(void)
        } else {
                label->base.source_position = *pos;
                label->statement            = statement;
+               label->n_users             += 1;
        }
 
        eat(':');
@@ -9117,28 +9075,32 @@ static void check_enum_cases(const switch_statement_t *statement)
 {
        if (!is_warn_on(WARN_SWITCH_ENUM))
                return;
-       const type_t *type = skip_typeref(statement->expression->base.type);
+       type_t *type = skip_typeref(statement->expression->base.type);
        if (! is_type_enum(type))
                return;
-       const enum_type_t *enumt = &type->enumt;
+       enum_type_t *enumt = &type->enumt;
 
        /* if we have a default, no warnings */
        if (statement->default_label != NULL)
                return;
 
+       determine_enum_values(enumt);
+
        /* FIXME: calculation of value should be done while parsing */
        /* TODO: quadratic algorithm here. Change to an n log n one */
-       long            last_value = -1;
-       const entity_t *entry      = enumt->enume->base.next;
+       const entity_t *entry = enumt->enume->base.next;
        for (; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
             entry = entry->base.next) {
-               const expression_t *expression = entry->enum_value.value;
-               long                value      = expression != NULL ? fold_constant_to_int(expression) : last_value + 1;
-               bool                found      = false;
-               for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
+               ir_tarval *value = entry->enum_value.tv;
+               bool       found = false;
+               for (const case_label_statement_t *l = statement->first_case; l != NULL;
+                    l = l->next) {
                        if (l->expression == NULL)
                                continue;
-                       if (l->first_case <= value && value <= l->last_case) {
+                       if (l->first_case == l->last_case && l->first_case != value)
+                               continue;
+                       if ((tarval_cmp(l->first_case, value) & ir_relation_less_equal)
+                        && (tarval_cmp(value, l->last_case) & ir_relation_less_equal)) {
                                found = true;
                                break;
                        }
@@ -9147,7 +9109,6 @@ static void check_enum_cases(const switch_statement_t *statement)
                        source_position_t const *const pos = &statement->base.source_position;
                        warningf(WARN_SWITCH_ENUM, pos, "'%N' not handled in switch", entry);
                }
-               last_value = value;
        }
 }
 
@@ -9208,20 +9169,20 @@ static statement_t *parse_loop_body(statement_t *const loop)
  */
 static statement_t *parse_while(void)
 {
-       statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
+       statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
 
        eat(T_while);
 
        PUSH_PARENT(statement);
-       PUSH_SCOPE_STATEMENT(&statement->whiles.scope);
+       PUSH_SCOPE_STATEMENT(&statement->fors.scope);
 
        expression_t *const cond = parse_condition();
-       statement->whiles.condition = cond;
+       statement->fors.condition = cond;
        /* ยง6.8.5:2    The controlling expression of an iteration statement shall
         *             have scalar type. */
        semantic_condition(cond, "condition of 'while'-statement");
 
-       statement->whiles.body = parse_loop_body(statement);
+       statement->fors.body = parse_loop_body(statement);
 
        POP_SCOPE();
        POP_PARENT();
@@ -9354,6 +9315,7 @@ static statement_t *parse_goto(void)
 
                label_t *const label = get_label("while parsing goto");
                if (label) {
+                       label->n_users        += 1;
                        label->used            = true;
                        statement->gotos.label = label;
 
@@ -10041,7 +10003,7 @@ static void check_unused_globals(void)
                                continue;
 
                        why = WARN_UNUSED_FUNCTION;
-                       s   = entity->function.statement != NULL ? "defined" : "declared";
+                       s   = entity->function.body != NULL ? "defined" : "declared";
                } else {
                        why = WARN_UNUSED_VARIABLE;
                        s   = "defined";
@@ -10277,7 +10239,7 @@ static void complete_incomplete_arrays(void)
 
 static void prepare_main_collect2(entity_t *const entity)
 {
-       PUSH_SCOPE(&entity->function.statement->compound.scope);
+       PUSH_SCOPE(&entity->function.body->compound.scope);
 
        // create call to __main
        symbol_t *symbol         = symbol_table_insert("__main");
@@ -10299,9 +10261,9 @@ static void prepare_main_collect2(entity_t *const entity)
        expr_statement->base.source_position  = builtin_source_position;
        expr_statement->expression.expression = call;
 
-       statement_t *statement = entity->function.statement;
-       assert(statement->kind == STATEMENT_COMPOUND);
-       compound_statement_t *compounds = &statement->compound;
+       statement_t *const body = entity->function.body;
+       assert(body->kind == STATEMENT_COMPOUND);
+       compound_statement_t *compounds = &body->compound;
 
        expr_statement->base.next = compounds->statements;
        compounds->statements     = expr_statement;