mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Add support for struct types, following p0561. (#856)
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user