Switch Action to use inheritance+cast (#711)

This commit is contained in:
Jon Meow
2021-08-06 15:14:29 -07:00
committed by GitHub
parent c1148caf7d
commit 4b2346cfcd
3 changed files with 376 additions and 354 deletions
+114 -58
View File
@@ -17,60 +17,18 @@
namespace Carbon {
enum class ActionKind {
LValAction,
ExpressionAction,
PatternAction,
StatementAction,
ValAction,
};
class Action {
public:
enum class Kind {
LValAction,
ExpressionAction,
PatternAction,
StatementAction,
ValAction,
};
struct LValAction {
static constexpr ActionKind Kind = ActionKind::LValAction;
const Expression* exp;
};
struct ExpressionAction {
static constexpr ActionKind Kind = ActionKind::ExpressionAction;
const Expression* exp;
};
struct PatternAction {
static constexpr ActionKind Kind = ActionKind::PatternAction;
const Pattern* pattern;
};
struct StatementAction {
static constexpr ActionKind Kind = ActionKind::StatementAction;
const Statement* stmt;
};
struct ValAction {
static constexpr ActionKind Kind = ActionKind::ValAction;
const Value* val;
};
struct Action {
static auto MakeLValAction(const Expression* e) -> Action*;
static auto MakeExpressionAction(const Expression* e) -> Action*;
static auto MakePatternAction(const Pattern* p) -> Action*;
static auto MakeStatementAction(const Statement* s) -> Action*;
static auto MakeValAction(const Value* v) -> Action*;
static void PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out);
auto GetLValAction() const -> const LValAction&;
auto GetExpressionAction() const -> const ExpressionAction&;
auto GetPatternAction() const -> const PatternAction&;
auto GetStatementAction() const -> const StatementAction&;
auto GetValAction() const -> const ValAction&;
void Print(llvm::raw_ostream& out) const;
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
inline auto tag() const -> ActionKind {
return std::visit([](const auto& t) { return t.Kind; }, value);
}
Action(const Value&) = delete;
Action& operator=(const Value&) = delete;
// The position or state of the action. Starts at 0 and goes up to the number
// of subexpressions.
@@ -78,15 +36,113 @@ struct Action {
// pos indicates how many of the entries in the following `results` vector
// will be filled in the next time this action is active.
// For each i < pos, results[i] contains a pointer to a Value.
int pos = 0;
auto Pos() const -> int { return pos; }
// Results from a subexpression.
std::vector<const Value*> results;
auto Results() const -> const std::vector<const Value*>& { return results; }
void IncrementPos() { ++pos; }
void AddResult(const Value* result) { results.push_back(result); }
void Clear() {
pos = 0;
results.clear();
}
// Returns the enumerator corresponding to the most-derived type of this
// object.
auto Tag() const -> Kind { return tag; }
static void PrintList(const Stack<Action*>& ls, llvm::raw_ostream& out);
void Print(llvm::raw_ostream& out) const;
LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
protected:
// Constructs an Action. `tag` must be the enumerator corresponding to the
// most-derived type being constructed.
explicit Action(Kind tag) : tag(tag) {}
private:
std::variant<LValAction, ExpressionAction, PatternAction, StatementAction,
ValAction>
value;
int pos = 0;
std::vector<const Value*> results;
const Kind tag;
};
class LValAction : public Action {
public:
explicit LValAction(const Expression* exp)
: Action(Kind::LValAction), exp(exp) {}
static auto classof(const Action* action) -> bool {
return action->Tag() == Kind::LValAction;
}
auto Exp() const -> const Expression* { return exp; }
private:
const Expression* exp;
};
class ExpressionAction : public Action {
public:
explicit ExpressionAction(const Expression* exp)
: Action(Kind::ExpressionAction), exp(exp) {}
static auto classof(const Action* action) -> bool {
return action->Tag() == Kind::ExpressionAction;
}
auto Exp() const -> const Expression* { return exp; }
private:
const Expression* exp;
};
class PatternAction : public Action {
public:
explicit PatternAction(const Pattern* pat)
: Action(Kind::PatternAction), pat(pat) {}
static auto classof(const Action* action) -> bool {
return action->Tag() == Kind::PatternAction;
}
auto Pat() const -> const Pattern* { return pat; }
private:
const Pattern* pat;
};
class StatementAction : public Action {
public:
explicit StatementAction(const Statement* stmt)
: Action(Kind::StatementAction), stmt(stmt) {}
static auto classof(const Action* action) -> bool {
return action->Tag() == Kind::StatementAction;
}
auto Stmt() const -> const Statement* { return stmt; }
private:
const Statement* stmt;
};
class ValAction : public Action {
public:
explicit ValAction(const Value* val) : Action(Kind::ValAction), val(val) {}
static auto classof(const Action* action) -> bool {
return action->Tag() == Kind::ValAction;
}
auto Val() const -> const Value* { return val; }
private:
const Value* val;
};
} // namespace Carbon