Skip to content

Commit

Permalink
Refactor short case statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcgr committed Apr 29, 2020
1 parent d12c536 commit ab1ad31
Show file tree
Hide file tree
Showing 32 changed files with 390 additions and 873 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
Expand Down
39 changes: 13 additions & 26 deletions src/AggregateOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,21 @@ enum class AggregateOp {

inline std::ostream& operator<<(std::ostream& os, AggregateOp op) {
switch (op) {
case AggregateOp::COUNT:
return os << "count";
case AggregateOp::COUNT: return os << "count";

case AggregateOp::MEAN:
return os << "mean";
case AggregateOp::MEAN: return os << "mean";

case AggregateOp::MAX:
case AggregateOp::UMAX:
case AggregateOp::FMAX:
return os << "max";
case AggregateOp::FMAX: return os << "max";

case AggregateOp::MIN:
case AggregateOp::UMIN:
case AggregateOp::FMIN:
return os << "min";
case AggregateOp::FMIN: return os << "min";

case AggregateOp::SUM:
case AggregateOp::USUM:
case AggregateOp::FSUM:
return os << "sum";
case AggregateOp::FSUM: return os << "sum";
}

UNREACHABLE_BAD_CASE_ANALYSIS
Expand All @@ -69,8 +64,7 @@ inline std::ostream& operator<<(std::ostream& os, AggregateOp op) {
// `[min, max]` # of arguments for each function
inline std::pair<uint8_t, uint8_t> aggregateArity(AggregateOp op) {
switch (op) {
case AggregateOp::COUNT:
return {0, 0};
case AggregateOp::COUNT: return {0, 0};

case AggregateOp::FMAX:
case AggregateOp::FMIN:
Expand All @@ -81,8 +75,7 @@ inline std::pair<uint8_t, uint8_t> aggregateArity(AggregateOp op) {
case AggregateOp::SUM:
case AggregateOp::UMAX:
case AggregateOp::UMIN:
case AggregateOp::USUM:
return {1, 1};
case AggregateOp::USUM: return {1, 1};
}

UNREACHABLE_BAD_CASE_ANALYSIS
Expand All @@ -96,19 +89,16 @@ inline TypeAttribute getTypeAttributeAggregate(const AggregateOp op) {
case AggregateOp::COUNT:
case AggregateOp::MAX:
case AggregateOp::MIN:
case AggregateOp::SUM:
return TypeAttribute::Signed;
case AggregateOp::SUM: return TypeAttribute::Signed;

case AggregateOp::MEAN:
case AggregateOp::FMAX:
case AggregateOp::FMIN:
case AggregateOp::FSUM:
return TypeAttribute::Float;
case AggregateOp::FSUM: return TypeAttribute::Float;

case AggregateOp::UMAX:
case AggregateOp::UMIN:
case AggregateOp::USUM:
return TypeAttribute::Unsigned;
case AggregateOp::USUM: return TypeAttribute::Unsigned;
}

UNREACHABLE_BAD_CASE_ANALYSIS
Expand All @@ -118,15 +108,12 @@ inline bool isOverloadedAggregator(const AggregateOp op) {
switch (op) {
case AggregateOp::MAX:
case AggregateOp::MIN:
case AggregateOp::SUM:
return true;
case AggregateOp::SUM: return true;

case AggregateOp::MEAN:
case AggregateOp::COUNT:
return false;
case AggregateOp::COUNT: return false;

default:
fatal("likely mistaken use of overloaded aggregator op");
default: fatal("likely mistaken use of overloaded aggregator op");
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/AstFunctorDeclaration.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,11 @@ class AstFunctorDeclaration : public AstNode {
void print(std::ostream& out) const override {
auto convert = [&](TypeAttribute type) {
switch (type) {
case TypeAttribute::Signed:
return "number";
case TypeAttribute::Symbol:
return "symbol";
case TypeAttribute::Float:
return "float";
case TypeAttribute::Unsigned:
return "unsigned";
case TypeAttribute::Record:
fatal("unhandled `TypeAttribute`");
case TypeAttribute::Signed: return "number";
case TypeAttribute::Symbol: return "symbol";
case TypeAttribute::Float: return "float";
case TypeAttribute::Unsigned: return "unsigned";
case TypeAttribute::Record: fatal("unhandled `TypeAttribute`");
}

UNREACHABLE_BAD_CASE_ANALYSIS
Expand Down
9 changes: 3 additions & 6 deletions src/AstIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ enum class AstIoType { input, output, printsize };
// FIXME: I'm going crazy defining these. There has to be a library that does this boilerplate for us.
inline std::ostream& operator<<(std::ostream& os, AstIoType e) {
switch (e) {
case AstIoType::input:
return os << "input";
case AstIoType::output:
return os << "output";
case AstIoType::printsize:
return os << "printsize";
case AstIoType::input: return os << "input";
case AstIoType::output: return os << "output";
case AstIoType::printsize: return os << "printsize";
}

UNREACHABLE_BAD_CASE_ANALYSIS
Expand Down
8 changes: 2 additions & 6 deletions src/AstIOTypeAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ void IOType::run(const AstTranslationUnit& translationUnit) {
return;
}
switch (io.getType()) {
case AstIoType::input:
inputRelations.insert(relation);
break;
case AstIoType::output:
outputRelations.insert(relation);
break;
case AstIoType::input: inputRelations.insert(relation); break;
case AstIoType::output: outputRelations.insert(relation); break;
case AstIoType::printsize:
printSizeRelations.insert(relation);
outputRelations.insert(relation);
Expand Down
45 changes: 12 additions & 33 deletions src/AstSemanticChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ AstSemanticCheckerImpl::AstSemanticCheckerImpl(AstTranslationUnit& tu) : tu(tu)
case TypeAttribute::Symbol:
report.addError("Non-symbolic use for symbolic functor", fun.getSrcLoc());
break;
case TypeAttribute::Record:
fatal("Invalid return type");
case TypeAttribute::Record: fatal("Invalid return type");
}
}

Expand All @@ -329,8 +328,7 @@ AstSemanticCheckerImpl::AstSemanticCheckerImpl(AstTranslationUnit& tu) : tu(tu)
case TypeAttribute::Float:
report.addError("Non-float argument for functor", arg->getSrcLoc());
break;
case TypeAttribute::Record:
fatal("Invalid argument type");
case TypeAttribute::Record: fatal("Invalid argument type");
}
}
++i;
Expand Down Expand Up @@ -361,21 +359,11 @@ AstSemanticCheckerImpl::AstSemanticCheckerImpl(AstTranslationUnit& tu) : tu(tu)
ss << "Constraint requires an operand of type "
<< join(opRamTypes, " or ", [&](auto& out, auto& ramTy) {
switch (ramTy) {
case TypeAttribute::Signed:
out << "`number`";
break;
case TypeAttribute::Symbol:
out << "`symbol`";
break;
case TypeAttribute::Unsigned:
out << "`unsigned`";
break;
case TypeAttribute::Float:
out << "`float`";
break;
case TypeAttribute::Record:
out << "a record";
break;
case TypeAttribute::Signed: out << "`number`"; break;
case TypeAttribute::Symbol: out << "`symbol`"; break;
case TypeAttribute::Unsigned: out << "`unsigned`"; break;
case TypeAttribute::Float: out << "`float`"; break;
case TypeAttribute::Record: out << "a record"; break;
}
});
report.addError(ss.str(), side.getSrcLoc());
Expand Down Expand Up @@ -844,20 +832,11 @@ void AstSemanticCheckerImpl::checkUnionType(const AstUnionType& type) {
std::stringstream errorMessage;
auto toPrimitiveTypeName = [](std::ostream& out, TypeAttribute type) {
switch (type) {
case TypeAttribute::Signed:
out << "number";
break;
case TypeAttribute::Unsigned:
out << "unsigned";
break;
case TypeAttribute::Float:
out << "float";
break;
case TypeAttribute::Symbol:
out << "symbol";
break;
case TypeAttribute::Record:
fatal("Invalid type");
case TypeAttribute::Signed: out << "number"; break;
case TypeAttribute::Unsigned: out << "unsigned"; break;
case TypeAttribute::Float: out << "float"; break;
case TypeAttribute::Symbol: out << "symbol"; break;
case TypeAttribute::Record: fatal("Invalid type");
}
};
errorMessage << "Union type " << name << " is defined over {"
Expand Down
24 changes: 8 additions & 16 deletions src/AstTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,16 +865,11 @@ std::unique_ptr<RamStatement> AstTranslator::ClauseTranslator::translateClause(

auto func_op = [&]() -> RamNestedIntrinsicOp {
switch (func->getFunction()) {
case FunctorOp::RANGE:
return RamNestedIntrinsicOp::RANGE;
case FunctorOp::URANGE:
return RamNestedIntrinsicOp::URANGE;
case FunctorOp::FRANGE:
return RamNestedIntrinsicOp::FRANGE;

default:
assert(isFunctorMultiResult(func->getFunction()));
abort();
case FunctorOp::RANGE: return RamNestedIntrinsicOp::RANGE;
case FunctorOp::URANGE: return RamNestedIntrinsicOp::URANGE;
case FunctorOp::FRANGE: return RamNestedIntrinsicOp::FRANGE;

default: assert(isFunctorMultiResult(func->getFunction())); abort();
}
};

Expand Down Expand Up @@ -967,12 +962,9 @@ std::unique_ptr<RamExpression> AstTranslator::translateConstant(AstConstant cons

if (auto* const c_num = dynamic_cast<const AstNumericConstant*>(&c)) {
switch (*c_num->getType()) {
case AstNumericConstant::Type::Int:
return std::make_unique<RamSignedConstant>(rawConstant);
case AstNumericConstant::Type::Uint:
return std::make_unique<RamUnsignedConstant>(rawConstant);
case AstNumericConstant::Type::Float:
return std::make_unique<RamFloatConstant>(rawConstant);
case AstNumericConstant::Type::Int: return std::make_unique<RamSignedConstant>(rawConstant);
case AstNumericConstant::Type::Uint: return std::make_unique<RamUnsignedConstant>(rawConstant);
case AstNumericConstant::Type::Float: return std::make_unique<RamFloatConstant>(rawConstant);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/AstTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ class AstTranslator {
return RamSignedFromString(numConstant->getConstant(), nullptr, 0);
case AstNumericConstant::Type::Uint:
return RamUnsignedFromString(numConstant->getConstant(), nullptr, 0);
case AstNumericConstant::Type::Float:
return RamFloatFromString(numConstant->getConstant());
case AstNumericConstant::Type::Float: return RamFloatFromString(numConstant->getConstant());
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/AstType.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,11 @@ class AstSubsetType : public AstType {
void print(std::ostream& os) const override {
os << ".type " << getQualifiedName() << " <: ";
switch (type) {
case TypeAttribute::Signed:
os << "number";
break;
case TypeAttribute::Unsigned:
os << "unsigned";
break;
case TypeAttribute::Float:
os << "float";
break;
case TypeAttribute::Symbol:
os << "symbol";
break;
case TypeAttribute::Record:
fatal("Invalid type");
case TypeAttribute::Signed: os << "number"; break;
case TypeAttribute::Unsigned: os << "unsigned"; break;
case TypeAttribute::Float: os << "float"; break;
case TypeAttribute::Symbol: os << "symbol"; break;
case TypeAttribute::Record: fatal("Invalid type");
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/AstTypeEnvironmentAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ Graph<AstQualifiedName> TypeEnvironmentAnalysis::createTypeDependencyGraph(
case TypeAttribute::Symbol:
typeDependencyGraph.insert(type->getQualifiedName(), "symbol");
break;
case TypeAttribute::Record:
fatal("invalid type");
case TypeAttribute::Record: fatal("invalid type");
}
} else if (dynamic_cast<const AstRecordType*>(astType) != nullptr) {
// do nothing
Expand Down
Loading

0 comments on commit ab1ad31

Please sign in to comment.