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:
Geoff Romer
2021-07-30 12:24:12 -07:00
committed by GitHub
co-authored by Jon Meow
parent b08f6bb0f1
commit 6ac3adfa53
31 changed files with 1302 additions and 575 deletions
+22 -23
View File
@@ -11,15 +11,33 @@
#include <vector>
#include "common/ostream.h"
#include "executable_semantics/syntax/paren_contents.h"
#include "llvm/Support/Compiler.h"
namespace Carbon {
struct Expression;
class Pattern;
// Converts paren_contents to an Expression, interpreting the parentheses as
// grouping if their contents permit that interpretation, or as forming a
// tuple otherwise.
auto ExpressionFromParenContents(
int line_num, const ParenContents<Expression>& paren_contents)
-> const Expression*;
// Converts paren_contents to an Expression, interpreting the parentheses as
// forming a tuple.
auto TupleExpressionFromParenContents(
int line_num, const ParenContents<Expression>& paren_contents)
-> const Expression*;
// A FieldInitializer represents the initialization of a single tuple field.
struct FieldInitializer {
// The field name. For a positional field, this may be empty.
FieldInitializer(std::string name, const Expression* expression)
: name(std::move(name)), expression(expression) {}
// The field name. Cannot be empty.
std::string name;
// The expression that initializes the field.
@@ -27,7 +45,6 @@ struct FieldInitializer {
};
enum class ExpressionKind {
AutoTypeLiteral,
BoolTypeLiteral,
BoolLiteral,
CallExpression,
@@ -37,7 +54,6 @@ enum class ExpressionKind {
IntTypeLiteral,
ContinuationTypeLiteral, // The type of a continuation value.
IntLiteral,
BindingExpression,
PrimitiveOperatorExpression,
TupleLiteral,
TypeTypeLiteral,
@@ -76,13 +92,6 @@ struct IndexExpression {
const Expression* offset;
};
struct BindingExpression {
static constexpr ExpressionKind Kind = ExpressionKind::BindingExpression;
// nullopt represents the `_` placeholder.
std::optional<std::string> name;
const Expression* type;
};
struct IntLiteral {
static constexpr ExpressionKind Kind = ExpressionKind::IntLiteral;
int value;
@@ -117,10 +126,6 @@ struct FunctionTypeLiteral {
const Expression* return_type;
};
struct AutoTypeLiteral {
static constexpr ExpressionKind Kind = ExpressionKind::AutoTypeLiteral;
};
struct BoolTypeLiteral {
static constexpr ExpressionKind Kind = ExpressionKind::BoolTypeLiteral;
};
@@ -141,10 +146,6 @@ struct TypeTypeLiteral {
struct Expression {
static auto MakeIdentifierExpression(int line_num, std::string var)
-> const Expression*;
static auto MakeBindingExpression(int line_num,
std::optional<std::string> var,
const Expression* type)
-> const Expression*;
static auto MakeIntLiteral(int line_num, int i) -> const Expression*;
static auto MakeBoolLiteral(int line_num, bool b) -> const Expression*;
static auto MakePrimitiveOperatorExpression(
@@ -164,13 +165,11 @@ struct Expression {
static auto MakeFunctionTypeLiteral(int line_num, const Expression* param,
const Expression* ret)
-> const Expression*;
static auto MakeAutoTypeLiteral(int line_num) -> const Expression*;
static auto MakeContinuationTypeLiteral(int line_num) -> const Expression*;
auto GetIdentifierExpression() const -> const IdentifierExpression&;
auto GetFieldAccessExpression() const -> const FieldAccessExpression&;
auto GetIndexExpression() const -> const IndexExpression&;
auto GetBindingExpression() const -> const BindingExpression&;
auto GetIntLiteral() const -> int;
auto GetBoolLiteral() const -> bool;
auto GetTupleLiteral() const -> const TupleLiteral&;
@@ -190,10 +189,10 @@ struct Expression {
private:
std::variant<IdentifierExpression, FieldAccessExpression, IndexExpression,
BindingExpression, IntLiteral, BoolLiteral, TupleLiteral,
IntLiteral, BoolLiteral, TupleLiteral,
PrimitiveOperatorExpression, CallExpression, FunctionTypeLiteral,
AutoTypeLiteral, BoolTypeLiteral, IntTypeLiteral,
ContinuationTypeLiteral, TypeTypeLiteral>
BoolTypeLiteral, IntTypeLiteral, ContinuationTypeLiteral,
TypeTypeLiteral>
value;
};