Skip to content

Commit d9c17b4

Browse files
committed
Merge branch 'cast-value'
* small improvements to cast_value()
2 parents 56afb50 + 187285b commit d9c17b4

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

‎expand.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ static long long get_longlong(struct expression *expr)
9494
return (value & andmask) | ormask;
9595
}
9696

97-
void cast_value(struct expression *expr, struct symbol *newtype,
98-
struct expression *old, struct symbol *oldtype)
97+
void cast_value(struct expression *expr, struct symbol *newtype, struct expression *old)
9998
{
99+
struct symbol *oldtype = old->ctype;
100100
int old_size = oldtype->bit_size;
101101
int new_size = newtype->bit_size;
102102
long long value, mask, signmask;
@@ -110,11 +110,13 @@ void cast_value(struct expression *expr, struct symbol *newtype,
110110
expr->taint = old->taint;
111111
if (old_size == new_size) {
112112
expr->value = old->value;
113+
expr->ctype = newtype;
113114
return;
114115
}
115116

116117
// expand it to the full "long long" value
117118
value = get_longlong(old);
119+
expr->ctype = newtype;
118120

119121
Int:
120122
// _Bool requires a zero test rather than truncation.
@@ -153,6 +155,7 @@ void cast_value(struct expression *expr, struct symbol *newtype,
153155
value = (long long)old->fvalue;
154156
expr->type = EXPR_VALUE;
155157
expr->taint = 0;
158+
expr->ctype = newtype;
156159
goto Int;
157160
}
158161

@@ -168,6 +171,7 @@ void cast_value(struct expression *expr, struct symbol *newtype,
168171
expr->fvalue = (float)expr->fvalue;
169172
}
170173
expr->type = EXPR_FVALUE;
174+
expr->ctype = newtype;
171175
}
172176

173177
/* Return true if constant shift size is valid */
@@ -872,7 +876,7 @@ static int expand_cast(struct expression *expr)
872876

873877
/* Simplify normal integer casts.. */
874878
if (target->type == EXPR_VALUE || target->type == EXPR_FVALUE) {
875-
cast_value(expr, expr->ctype, target, target->ctype);
879+
cast_value(expr, expr->ctype, target);
876880
return 0;
877881
}
878882
return cost + 1;

‎expression.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ struct token *primary_expression(struct token *token, struct expression **tree)
432432
// TODO: handle 'u8', 'u' & 'U' prefixes.
433433
if (token_type(token) < TOKEN_WIDE_CHAR) {
434434
expr->ctype = &char_ctype;
435-
cast_value(expr, &int_ctype, expr, expr->ctype);
436-
expr->ctype = &int_ctype;
435+
cast_value(expr, &int_ctype, expr);
437436
} else {
438437
expr->ctype = wchar_ctype;
439438
}

‎expression.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ struct token *compound_statement(struct token *, struct statement *);
337337
#define constant_expression(token,tree) conditional_expression(token, tree)
338338

339339
/* Cast folding of constant values.. */
340-
void cast_value(struct expression *expr, struct symbol *newtype,
341-
struct expression *old, struct symbol *oldtype);
340+
void cast_value(struct expression *expr, struct symbol *newtype, struct expression *old);
342341

343342
#endif

‎parse.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,7 @@ static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
903903
expr->ctype = &int_ctype;
904904
continue;
905905
}
906-
cast_value(expr, base_type, expr, ctype);
907-
expr->ctype = base_type;
906+
cast_value(expr, base_type, expr);
908907
} END_FOR_EACH_PTR(sym);
909908
}
910909

0 commit comments

Comments
 (0)