Add support for struct types, following p0561. (#856)

This commit is contained in:
Geoff Romer
2021-09-29 16:14:27 -07:00
committed by GitHub
parent d157d96338
commit c4e40aaa86
20 changed files with 674 additions and 44 deletions
+53
View File
@@ -35,6 +35,8 @@ class Expression {
StringLiteral,
StringTypeLiteral,
TupleLiteral,
StructLiteral,
StructTypeLiteral,
TypeTypeLiteral,
IdentifierExpression,
IntrinsicExpression,
@@ -230,6 +232,57 @@ class TupleLiteral : public Expression {
std::vector<FieldInitializer> fields;
};
// A non-empty literal value of a struct type.
//
// It can't be empty because the syntax `{}` is a struct type literal as well
// as a literal value of that type, so for consistency we always represent it
// as a StructTypeLiteral rather than let it oscillate unpredictably between
// the two.
class StructLiteral : public Expression {
public:
explicit StructLiteral(SourceLocation loc,
std::vector<FieldInitializer> fields)
: Expression(Kind::StructLiteral, loc), fields_(std::move(fields)) {
CHECK(!fields_.empty())
<< "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
}
static auto classof(const Expression* exp) -> bool {
return exp->Tag() == Kind::StructLiteral;
}
auto fields() const -> const std::vector<FieldInitializer>& {
return fields_;
}
private:
std::vector<FieldInitializer> fields_;
};
// A literal representing a struct type.
//
// Code that handles this type may sometimes need to have special-case handling
// for `{}`, which is a struct value in addition to being a struct type.
class StructTypeLiteral : public Expression {
public:
explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
explicit StructTypeLiteral(SourceLocation loc,
std::vector<FieldInitializer> fields)
: Expression(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {}
static auto classof(const Expression* exp) -> bool {
return exp->Tag() == Kind::StructTypeLiteral;
}
auto fields() const -> const std::vector<FieldInitializer>& {
return fields_;
}
private:
std::vector<FieldInitializer> fields_;
};
class PrimitiveOperatorExpression : public Expression {
public:
explicit PrimitiveOperatorExpression(