Skip to content

Commit 99c4403

Browse files
committed
Update typed converter to use new when-statement representation
Also update with reviewer feedback: - do not use 'qualType()' - consistent usage of 'TypedConverter*' Signed-off-by: Ben Harshbarger <[email protected]>
1 parent e1d9bd1 commit 99c4403

File tree

1 file changed

+41
-56
lines changed

1 file changed

+41
-56
lines changed

Diff for: compiler/passes/convert-typed-uast.cpp

+41-56
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,6 @@ struct TConverter final : UastConverter {
752752

753753
// Create a new temporary. The type used for it must be supplied.
754754
Symbol* makeNewTemp(const types::QualifiedType& qt, bool insertDef=true);
755-
Symbol* makeNewTemp(::Qualifier qual, Type* t, bool insertDef=true);
756755

757756
// Store 'e' in a temporary if it does not already refer to one. The type
758757
// for the temporary must be provided and cannot easily be retrieved from
@@ -2809,22 +2808,17 @@ TConverter::defaultValueForType(const types::Type* t,
28092808
}
28102809

28112810
Symbol*
2812-
TConverter::makeNewTemp(::Qualifier qual, Type* t, bool insertDef) {
2811+
TConverter::makeNewTemp(const types::QualifiedType& qt, bool insertDef) {
28132812
auto ret = newTemp();
28142813
ret->addFlag(FLAG_EXPR_TEMP);
2815-
ret->qual = qual;
2816-
ret->type = t;
2814+
ret->qual = convertQualifier(qt.kind());
2815+
ret->type = convertType(qt.type());
28172816

28182817
if (insertDef) insertStmt(new DefExpr(ret));
28192818

28202819
return ret;
28212820
}
28222821

2823-
Symbol*
2824-
TConverter::makeNewTemp(const types::QualifiedType& qt, bool insertDef) {
2825-
return makeNewTemp(convertQualifier(qt.kind()), convertType(qt.type()), insertDef);
2826-
}
2827-
28282822
SymExpr*
28292823
TConverter::storeInTempIfNeeded(Expr* e, const types::QualifiedType& qt) {
28302824
if (SymExpr* se = toSymExpr(e)) {
@@ -2833,16 +2827,12 @@ TConverter::storeInTempIfNeeded(Expr* e, const types::QualifiedType& qt) {
28332827

28342828
Symbol* t = nullptr;
28352829

2836-
if (qt.isUnknown()) {
2837-
t = makeNewTemp(e->qualType().getQual(), e->typeInfo());
2838-
} else {
2839-
// Prevent default-constructed 'QualifiedType' from slipping in.
2840-
INT_ASSERT(qt.hasTypePtr());
2841-
2842-
// otherwise, store the value in a temp
2843-
t = makeNewTemp(qt);
2844-
}
2845-
2830+
// Prevent default-constructed 'QualifiedType' from slipping in.
2831+
// Note: Can't use, e.g., ``e->qualType()`` here because ``qualType`` may
2832+
// attempt to invoke production's resolution in some cases.
2833+
INT_ASSERT(qt.hasTypePtr());
2834+
// otherwise, store the value in a temp
2835+
t = makeNewTemp(qt);
28462836
insertStmt(new CallExpr(PRIM_MOVE, t, e));
28472837
return new SymExpr(t);
28482838
}
@@ -4068,8 +4058,9 @@ void TConverter::ActualConverter::convertActual(const FormalActual& fa) {
40684058
INT_ASSERT(astActual);
40694059

40704060
// Convert the actual and leave.
4071-
auto actualExpr = tc_->convertExpr(astActual, rv_);
4072-
std::get<Expr*>(slot) = tc_->storeInTempIfNeeded(actualExpr, {});
4061+
types::QualifiedType actualType;
4062+
auto actualExpr = tc_->convertExpr(astActual, rv_, &actualType);
4063+
std::get<Expr*>(slot) = tc_->storeInTempIfNeeded(actualExpr, actualType);
40734064

40744065
return;
40754066
}
@@ -5090,50 +5081,41 @@ bool TConverter::enter(const ExternBlock* node, RV& rv) {
50905081

50915082
void TConverter::exit(const ExternBlock* node, RV& rv) {}
50925083

5093-
static SymExpr* makeCaseCond(TConverter& tc, TConverter::RV& rv,
5084+
static SymExpr* makeCaseCond(TConverter* tc, TConverter::RV& rv,
50945085
const uast::When* when,
50955086
SymExpr* selectExpr,
50965087
const uast::AstNode* cs) {
50975088
auto re = rv.byAst(cs);
50985089

50995090
// Grab the '==' ResolvedFunction
5100-
const AssociatedAction* action = nullptr;
5101-
for (const auto& a : re.associatedActions()) {
5102-
if (a.action() == AssociatedAction::COMPARE) {
5103-
action = &a;
5104-
break;
5105-
} else {
5106-
INT_ASSERT(false);
5107-
}
5108-
}
5091+
auto action = re.getAction(AssociatedAction::COMPARE);
5092+
INT_ASSERT(action);
51095093
auto cmp = action->fn();
51105094

51115095
// TODO: create a wrapper for this kind of thing
51125096
const ResolvedFunction* rf = nullptr;
5113-
if (tc.paramElideCallOrNull(cmp, re.poiScope(), &rf)) {
5097+
if (tc->paramElideCallOrNull(cmp, re.poiScope(), &rf)) {
51145098
INT_ASSERT(false && "Should not have param elided initializer call!");
51155099
} else if (!rf) {
5116-
return toSymExpr(TC_PLACEHOLDER((&tc)));
5100+
return toSymExpr(TC_PLACEHOLDER(tc));
51175101
}
51185102

5119-
auto fn = tc.findOrConvertFunction(rf);
5103+
auto fn = tc->findOrConvertFunction(rf);
51205104
INT_ASSERT(fn);
51215105

5122-
// TODO: is there a better way to represent the return type of '=='? As-is,
5123-
// it's a bit confusing for the case-expression's type in 'rv' to always be
5124-
// a bool.
5125-
Expr* caseExpr = tc.convertExpr(cs, rv);
5126-
caseExpr = tc.storeInTempIfNeeded(caseExpr, {});
5106+
types::QualifiedType caseType;
5107+
Expr* caseExpr = tc->convertExpr(cs, rv, &caseType);
5108+
caseExpr = tc->storeInTempIfNeeded(caseExpr, caseType);
51275109

51285110
// TODO: handle case where passing to '==' has an associated action
51295111
auto call = new CallExpr(fn, selectExpr->copy(), caseExpr);
51305112

5131-
auto cond = tc.storeInTempIfNeeded(call, re.type());
5113+
auto cond = tc->storeInTempIfNeeded(call, re.type());
51325114

51335115
return cond;
51345116
}
51355117

5136-
static SymExpr* getWhenCond(TConverter& tc, TConverter::RV& rv,
5118+
static SymExpr* getWhenCond(TConverter* tc, TConverter::RV& rv,
51375119
const uast::When* when,
51385120
SymExpr* selectExpr) {
51395121
if (when->numCaseExprs() == 1) {
@@ -5145,14 +5127,16 @@ static SymExpr* getWhenCond(TConverter& tc, TConverter::RV& rv,
51455127
// not match.
51465128

51475129
VarSymbol* agg = new VarSymbol("_case_cond_agg", dtBool);
5148-
tc.insertStmt(new CallExpr(PRIM_MOVE, agg, gFalse));
5149-
tc.insertStmt(new DefExpr(agg));
5130+
tc->insertStmt(new CallExpr(PRIM_MOVE, agg, gFalse));
5131+
tc->insertStmt(new DefExpr(agg));
51505132

51515133
int count = 0;
51525134
for (auto cs : when->caseExprs()) {
5153-
// If it's param-true, 'getWhenCond' would not have been called
5154-
// If it's param-false, there's no need to generate any comparisons
5155-
if (rv.byAst(cs).type().isParam()) continue;
5135+
if (auto action = rv.byAst(cs).getAction(AssociatedAction::COMPARE)) {
5136+
// If it's param-true, 'getWhenCond' would not have been called
5137+
// If it's param-false, there's no need to generate any comparisons
5138+
if (action->type().isParam()) continue;
5139+
}
51565140

51575141
auto cond = makeCaseCond(tc, rv, when, selectExpr, cs);
51585142

@@ -5161,16 +5145,16 @@ static SymExpr* getWhenCond(TConverter& tc, TConverter::RV& rv,
51615145

51625146
auto elseBlock = new BlockStmt();
51635147
auto branch = new CondStmt(cond, thenBlock, elseBlock);
5164-
tc.insertStmt(branch);
5148+
tc->insertStmt(branch);
51655149

51665150
// Push the else branch so that the next case check is inserted there
5167-
tc.pushBlock(elseBlock);
5151+
tc->pushBlock(elseBlock);
51685152
count += 1;
51695153
}
51705154

51715155
// pop the blocks we pushed, to get back to the when-stmt level
51725156
for (int i = 0; i < count; i++) {
5173-
tc.popBlock();
5157+
tc->popBlock();
51745158
}
51755159

51765160
return new SymExpr(agg);
@@ -5196,13 +5180,14 @@ bool TConverter::enter(const Select* node, RV& rv) {
51965180
bool anyParamTrue = false;
51975181
bool allParamFalse = true;
51985182
for(auto cs : when->caseExprs()) {
5199-
auto qt = rv.byAst(cs).type();
5200-
if (!qt.isParamFalse()) {
5201-
allParamFalse = false;
5202-
}
5183+
if (auto action = rv.byAst(cs).getAction(AssociatedAction::COMPARE)) {
5184+
if (!action->type().isParamFalse()) {
5185+
allParamFalse = false;
5186+
}
52035187

5204-
if (qt.isParamTrue()) {
5205-
anyParamTrue = true;
5188+
if (action->type().isParamTrue()) {
5189+
anyParamTrue = true;
5190+
}
52065191
}
52075192
}
52085193

@@ -5214,7 +5199,7 @@ bool TConverter::enter(const Select* node, RV& rv) {
52145199
// Nothing else can match after this, so we're done
52155200
break;
52165201
} else if (!allParamFalse) {
5217-
auto cond = getWhenCond(*this, rv, when, selectSym);
5202+
auto cond = getWhenCond(this, rv, when, selectSym);
52185203

52195204
auto thenBlock = new BlockStmt();
52205205
pushBlock(thenBlock);

0 commit comments

Comments
 (0)