Skip to content

Commit 3eb5d02

Browse files
committed
Analyser: simplify constant value analyser
* rename `ctv_analyser.check` as `ctv_analyser.checkRange` * call `ast.evalExpr()` instead of `ctv_analyser.get_value()` * remove `ctv_analyser.get_value` * move analyser_utils/ctv_analyser.c2 to analyser/ctv_analyser.c2
1 parent a653207 commit 3eb5d02

19 files changed

+34
-51
lines changed

analyser/conversion_checker.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public fn bool Checker.check(Checker* c, QualType lhs, QualType rhs, Expr** e_pt
139139
// TODO if bool, always allow, except in init?
140140
if (t1.isBool()) return true;
141141

142-
return ctv_analyser.check(c.diags, lhs, *e_ptr);
142+
return ctv_analyser.checkRange(c.diags, lhs, *e_ptr);
143143
}
144144

145145
const Type* lcanon = t1.getTypeOrNil();
@@ -434,7 +434,7 @@ fn bool Checker.checkEnum2Int(Checker* c, const Type* lcanon, const Type* rcanon
434434

435435
// NOTE: EnumConstDecls are CTV, but variables of the type are not!
436436
if ((*c.expr_ptr).isCtv()) {
437-
return ctv_analyser.check(c.diags, c.lhs, *c.expr_ptr);
437+
return ctv_analyser.checkRange(c.diags, c.lhs, *c.expr_ptr);
438438
} else {
439439
// compare impl type
440440
const EnumType* et = (EnumType*)rcanon;

analyser/conversion_checker_expr.c2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
module conversion_checker;
1717

1818
import ast local;
19-
import ctv_analyser;
2019

2120
type ExprWidth struct {
2221
u8 width; // in bits
@@ -47,7 +46,7 @@ fn ExprWidth getExprWidth(const Expr* e) {
4746
ExprWidth result = {}
4847

4948
if (e.isCtv()) {
50-
Value v = ctv_analyser.get_value(e);
49+
Value v = ast.evalExpr(e);
5150
result.width = v.getWidth();
5251
result.is_signed = v.isNegative();
5352
return result;
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ fn void Limit.init(Limit* l, u32 width, bool is_signed) {
3535
}
3636
}
3737

38-
public fn Value get_value(const Expr* e) {
39-
return ast.evalExpr(e);
40-
}
41-
42-
public fn bool check(diagnostics.Diags* diags, QualType qt, const Expr* e) {
38+
public fn bool checkRange(diagnostics.Diags* diags, QualType qt, const Expr* e) {
4339
QualType canon = qt.getCanonicalType();
4440
if (!canon.isBuiltin()) return true; // TODO find out cases
4541

4642
assert(e.isCtv());
4743

48-
Value value = ctv_analyser.get_value((e));
44+
Value value = ast.evalExpr((e));
4945

5046
if (value.kind == ValueKind.Error) {
5147
diags.errorRange(e.getLoc(), e.getRange(), "%s", value.error_msg);
@@ -55,7 +51,7 @@ public fn bool check(diagnostics.Diags* diags, QualType qt, const Expr* e) {
5551
}
5652

5753
public fn bool checkBitfield(diagnostics.Diags* diags, u8 bitfield_width, bool bitfield_signed, const Expr* e) {
58-
Value value = ctv_analyser.get_value((e));
54+
Value value = ast.evalExpr((e));
5955
if (value.kind == ValueKind.Error) {
6056
diags.errorRange(e.getLoc(), e.getRange(), "%s", value.error_msg);
6157
return false;

analyser/module_analyser.c2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515

1616
module module_analyser;
1717

18+
import ast local;
1819
import ast_context;
1920
import ast_builder;
20-
import ast local;
2121
import attr;
22-
import ctv_analyser;
2322
import conversion_checker as conv;
2423
import diagnostics;
2524
import file_utils;
@@ -504,8 +503,8 @@ fn void Analyser.handleStaticAssert(void* arg, StaticAssert* d) {
504503

505504
if (error) return;
506505

507-
Value val1 = ctv_analyser.get_value(lhs);
508-
Value val2 = ctv_analyser.get_value(rhs);
506+
Value val1 = ast.evalExpr(lhs);
507+
Value val2 = ast.evalExpr(rhs);
509508

510509
if (!val1.is_equal(&val2)) {
511510
ma.errorRange(rhs.getStartLoc(), rhs.getRange(), "static_assert failed, expected %s, got %s", val1.str(), val2.str());

analyser/module_analyser_binop.c2

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import ast local;
1919
import c_prec local;
2020
import c2_prec local;
2121
import conversion_checker;
22-
import ctv_analyser;
2322

2423
fn bool validBinOpKind(QualType t) {
2524
t = t.getCanonicalType();
@@ -650,7 +649,7 @@ fn bool Analyser.checkShiftArgs(Analyser* ma, Expr* lhs, Expr* rhs) {
650649
// Do not reduce width of signed types to allow `i32 a = 1 << 31;`
651650

652651
if (lhs.isCtv()) {
653-
Value val = ctv_analyser.get_value(lhs);
652+
Value val = ast.evalExpr(lhs);
654653
if (val.isNegative()) {
655654
ma.error(lhs.getLoc(), "shifting a negative signed value is undefined");
656655
return false;
@@ -659,7 +658,7 @@ fn bool Analyser.checkShiftArgs(Analyser* ma, Expr* lhs, Expr* rhs) {
659658

660659
// check rhs
661660
if (rhs.isCtv()) {
662-
Value val = ctv_analyser.get_value(rhs);
661+
Value val = ast.evalExpr(rhs);
663662
if (val.isNegative()) {
664663
ma.error(rhs.getLoc(), "shift count is negative");
665664
return false;
@@ -678,7 +677,7 @@ fn bool Analyser.checkShiftArgs(Analyser* ma, Expr* lhs, Expr* rhs) {
678677
fn bool Analyser.checkZero(Analyser* ma, Expr* e, const char* operation) {
679678
if (!e.isCtv()) return true;
680679

681-
Value val = ctv_analyser.get_value(e);
680+
Value val = ast.evalExpr(e);
682681
if (val.isDecimal()) {
683682
if (val.isZero()) {
684683
ma.error(e.getLoc(), "%s by zero is undefined", operation);

analyser/module_analyser_expr.c2

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module module_analyser;
1818
import ast local;
1919
import ast_builder;
2020
import conversion_checker;
21-
import ctv_analyser;
2221
import src_loc local;
2322

2423
fn QualType Analyser.analyseExpr(Analyser* ma, Expr** e_ptr, bool need_rvalue, u32 side) {
@@ -437,7 +436,7 @@ fn QualType Analyser.analyseArraySubscriptExpr(Analyser* ma, Expr** e_ptr, u32 s
437436
// check range
438437
u32 size = at.getSize();
439438
if (size != 0) { // dont give error if arrays has size 0 (allowed for struct members)
440-
Value val = ctv_analyser.get_value(index);
439+
Value val = ast.evalExpr(index);
441440
u64 idx = val.as_u64();
442441
if (idx >= size) {
443442
ma.error(index.getLoc(), "array out-of-bounds access [%d] in array of [%d]", idx, size);
@@ -511,7 +510,7 @@ fn bool Analyser.analyseBitOffsetIndex(Analyser* ma, Expr** e_ptr, QualType base
511510
// TODO only allow CTV expressions
512511
if (!e.isCtv()) return false;
513512

514-
Value val = ctv_analyser.get_value(e);
513+
Value val = ast.evalExpr(e);
515514
if (val.isNegative()) {
516515
ma.errorRange(e.getLoc(), e.getRange(), "bitoffset index value '%s' is negative", val.str());
517516
return false;

analyser/module_analyser_init.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn bool Analyser.analyseInitExpr(Analyser* ma, Expr** e_ptr, QualType expectedTy
9494
if (expectedType.isBuiltin() && !res.isPointer()) {
9595
// TODO should insert `BooleanCast` implicit cast
9696
if (expectedType.isBool()) return true;
97-
if (!ctv_analyser.check(ma.diags, expectedType, e)) return false;
97+
if (!ctv_analyser.checkRange(ma.diags, expectedType, e)) return false;
9898
if (res.getType() != expectedType.getType()) {
9999
ma.builder.insertImplicitCast(ImplicitCastKind.IntegralCast, e_ptr, expectedType);
100100
}
@@ -266,7 +266,7 @@ fn bool Analyser.checkArrayDesignators(Analyser* ma, InitListExpr* ile, bool has
266266
ArrayDesignatedInitExpr* ad = (ArrayDesignatedInitExpr*)value;
267267
Expr* desig = ad.getDesignator();
268268
loc = desig.getLoc();
269-
Value idx = ctv_analyser.get_value(desig);
269+
Value idx = ast.evalExpr(desig);
270270
if (!idx.isDecimal()) {
271271
ma.error(loc, "array designator expression must have integer type");
272272
return false;

analyser/module_analyser_stmt.c2

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
module module_analyser;
1717

1818
import ast local;
19-
import ctv_analyser;
2019
import label_vector local;
2120
import scope;
2221

@@ -269,7 +268,7 @@ fn FlowBits Analyser.analyseForStmt(Analyser* ma, Stmt* s) {
269268
if (qt.isInvalid()) goto done;
270269
ma.checker.check(builtins[BuiltinKind.Bool], qt, cond, (*cond).getLoc());
271270
if ((*cond).isCtv()) {
272-
Value v = ctv_analyser.get_value((*cond));
271+
Value v = ast.evalExpr((*cond));
273272
if (v.isZero()) {
274273
// TODO: body is never reached
275274
} else {
@@ -304,7 +303,7 @@ fn FlowBits Analyser.analyseWhileStmt(Analyser* ma, Stmt* s) {
304303

305304
Expr* cond = getCondExpr(w.getCond());
306305
if (cond.isCtv()) {
307-
Value v = ctv_analyser.get_value(cond);
306+
Value v = ast.evalExpr(cond);
308307
if (v.isZero()) {
309308
// TODO: body is never reached
310309
} else {

analyser/module_analyser_struct.c2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
module module_analyser;
1717

1818
import ast local;
19-
import ctv_analyser;
2019
import name_vector local;
2120
import size_analyser;
2221

@@ -86,7 +85,7 @@ fn void Analyser.analyseStructMembers(Analyser* ma, StructTypeDecl* d) {
8685
ma.errorRange(bitfield.getLoc(), bitfield.getRange(), "bit-field size is not a compile-time value");
8786
return;
8887
}
89-
Value value = ctv_analyser.get_value(bitfield);
88+
Value value = ast.evalExpr(bitfield);
9089
if (value.isZero()) {
9190
if (name)
9291
ma.errorRange(bitfield.getLoc(), bitfield.getRange(), "zero width for bit-field '%s'", name);

analyser/module_analyser_switch.c2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
module module_analyser;
1717

1818
import ast local;
19-
import ctv_analyser;
2019
import init_checker;
2120
import src_loc local;
2221
import scope;
@@ -330,7 +329,7 @@ fn bool Analyser.analyseCaseExpr(Analyser* ma,
330329
ma.error(cond.getLoc(), "case condition is not compile-time constant");
331330
return false;
332331
}
333-
Value v = ctv_analyser.get_value(cond);
332+
Value v = ast.evalExpr(cond);
334333
index = v.as_u32();
335334
*name_idxp = 0;
336335
if (loc) {

0 commit comments

Comments
 (0)