mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 20:58:15 +00:00
Factor out a Pattern sum type from Expression (#685)
`Pattern` is intended to pilot some changes I would like to apply to all our sum types: - The alternatives are expressed as derived classes rather than members of a `std::variant`. - The alternatives are classes in the [style guide sense](https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes), meaning they can have invariants, but can't have public data members. - Creating an object is expressed using a constructor rather than a factory function. - Accessing an alternative is expressed as a cast (using LLVM's RTTI system) rather than `std::get` or a `Get` method. Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
This commit is contained in:
1 parent
b08f6bb0f1
commit
6ac3adfa53
31 files changed
+1302
-575
No files matched your search
@@ -29,6 +29,12 @@ auto Action::MakeExpressionAction(const Expression* e) -> Action* {
|
||||
return act;
|
||||
}
|
||||
|
||||
auto Action::MakePatternAction(const Pattern* p) -> Action* {
|
||||
auto* act = new Action();
|
||||
act->value = PatternAction({.pattern = p});
|
||||
return act;
|
||||
}
|
||||
|
||||
auto Action::MakeStatementAction(const Statement* s) -> Action* {
|
||||
auto* act = new Action();
|
||||
act->value = StatementAction({.stmt = s});
|
||||
@@ -49,6 +55,10 @@ auto Action::GetExpressionAction() const -> const ExpressionAction& {
|
||||
return std::get<ExpressionAction>(value);
|
||||
}
|
||||
|
||||
auto Action::GetPatternAction() const -> const PatternAction& {
|
||||
return std::get<PatternAction>(value);
|
||||
}
|
||||
|
||||
auto Action::GetStatementAction() const -> const StatementAction& {
|
||||
return std::get<StatementAction>(value);
|
||||
}
|
||||
@@ -65,6 +75,9 @@ void Action::Print(llvm::raw_ostream& out) const {
|
||||
case ActionKind::ExpressionAction:
|
||||
out << *GetExpressionAction().exp;
|
||||
break;
|
||||
case ActionKind::PatternAction:
|
||||
out << *GetPatternAction().pattern;
|
||||
break;
|
||||
case ActionKind::StatementAction:
|
||||
GetStatementAction().stmt->PrintDepth(1, out);
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user