This change is mostly for brevity's sake; but it also paves the way for the `Policy` enum to be reuseable for other situations that require the same three-way semantics.
Reviewed By: Peiming
Differential Revision: https://reviews.llvm.org/D155532
// Our variation on `AffineParser::{parseBareIdExpr,parseIdentifierDefinition}`
OptionalParseResult DimLvlMapParser::parseVar(VarKind vk, bool isOptional,
- CreationPolicy creationPolicy,
+ Policy creationPolicy,
VarInfo::ID &varID,
bool &didCreate) {
// Save the current location so that we can have error messages point to
// TODO(wrengr): these error messages make sense for our intended usage,
// but not in general; but it's unclear how best to factor that part out.
switch (creationPolicy) {
- case CreationPolicy::MustNot:
+ case Policy::MustNot:
return parser.emitError(loc, "use of undeclared identifier '" + name + "'");
- case CreationPolicy::May:
- llvm_unreachable("got nullopt for CreationPolicy::May");
- case CreationPolicy::Must:
+ case Policy::May:
+ llvm_unreachable("got nullopt for Policy::May");
+ case Policy::Must:
return parser.emitError(loc, "redefinition of identifier '" + name + "'");
}
- llvm_unreachable("unknown CreationPolicy");
+ llvm_unreachable("unknown Policy");
}
FailureOr<VarInfo::ID> DimLvlMapParser::parseVarUsage(VarKind vk) {
VarInfo::ID varID;
bool didCreate;
- const auto res = parseVar(vk, /*isOptional=*/false, CreationPolicy::MustNot,
- varID, didCreate);
+ const auto res =
+ parseVar(vk, /*isOptional=*/false, Policy::MustNot, varID, didCreate);
if (!res.has_value() || failed(*res))
return failure();
return varID;
DimLvlMapParser::parseVarBinding(VarKind vk, bool isOptional) {
VarInfo::ID id;
bool didCreate;
- const auto res =
- parseVar(vk, isOptional, CreationPolicy::Must, id, didCreate);
+ const auto res = parseVar(vk, isOptional, Policy::Must, id, didCreate);
if (res.has_value()) {
FAILURE_IF_FAILED(*res)
return std::make_pair(env.bindVar(id), true);
private:
OptionalParseResult parseVar(VarKind vk, bool isOptional,
- CreationPolicy creationPolicy, VarInfo::ID &id,
+ Policy creationPolicy, VarInfo::ID &id,
bool &didCreate);
FailureOr<VarInfo::ID> parseVarUsage(VarKind vk);
FailureOr<std::pair<Var, bool>> parseVarBinding(VarKind vk, bool isOptional);
}
std::optional<std::pair<VarInfo::ID, bool>>
-VarEnv::lookupOrCreate(CreationPolicy policy, StringRef name, llvm::SMLoc loc,
+VarEnv::lookupOrCreate(Policy creationPolicy, StringRef name, llvm::SMLoc loc,
VarKind vk) {
- switch (policy) {
- case CreationPolicy::MustNot: {
+ switch (creationPolicy) {
+ case Policy::MustNot: {
const auto oid = lookup(name);
if (!oid)
return std::nullopt; // Doesn't exist, but must not create.
#endif // NDEBUG
return std::make_pair(*oid, false);
}
- case CreationPolicy::May:
+ case Policy::May:
return create(name, loc, vk, /*verifyUsage=*/true);
- case CreationPolicy::Must: {
+ case Policy::Must: {
const auto res = create(name, loc, vk, /*verifyUsage=*/false);
// const auto id = res.first;
const auto didCreate = res.second;
return res;
}
}
- llvm_unreachable("unknown CreationPolicy");
+ llvm_unreachable("unknown Policy");
}
Var VarEnv::bindUnusedVar(VarKind vk) { return Var(vk, nextNum[vk]++); }
// static_assert(IsZeroCostAbstraction<VarInfo>);
//===----------------------------------------------------------------------===//
-enum class CreationPolicy { MustNot, May, Must };
+enum class Policy { MustNot, May, Must };
+//===----------------------------------------------------------------------===//
class VarEnv final {
/// Map from `VarKind` to the next free `Var::Num`; used by `bindVar`.
VarKindArray<Var::Num> nextNum;
VarKind vk, bool verifyUsage = false);
/// Attempts to lookup or create a variable according to the given
- /// `CreationPolicy`. Returns nullopt in one of two circumstances:
+ /// `Policy`. Returns nullopt in one of two circumstances:
/// (1) the policy says we `Must` create, yet the variable already exists;
/// (2) the policy says we `MustNot` create, yet no such variable exists.
/// Otherwise, if the variable already exists then it is validated against
// TODO(wrengr): Prolly want to rename this to `create` and move the
// current method of that name to being a private `createImpl`.
std::optional<std::pair<VarInfo::ID, bool>>
- lookupOrCreate(CreationPolicy policy, StringRef name, llvm::SMLoc loc,
+ lookupOrCreate(Policy creationPolicy, StringRef name, llvm::SMLoc loc,
VarKind vk);
/// Binds the given variable to the next free `Var::Num` for its `VarKind`.