mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 14:51:03 +01:00
Add support for struct types, following p0561. (#856)
This commit is contained in:
@@ -64,10 +64,11 @@ static void PrintOp(llvm::raw_ostream& out, Operator op) {
|
||||
}
|
||||
|
||||
static void PrintFields(llvm::raw_ostream& out,
|
||||
const std::vector<FieldInitializer>& fields) {
|
||||
const std::vector<FieldInitializer>& fields,
|
||||
std::string_view separator) {
|
||||
llvm::ListSeparator sep;
|
||||
for (const auto& field : fields) {
|
||||
out << sep << field.name << " = " << *field.expression;
|
||||
out << sep << "." << field.name << separator << *field.expression;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,9 +86,19 @@ void Expression::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
case Expression::Kind::TupleLiteral:
|
||||
out << "(";
|
||||
PrintFields(out, cast<TupleLiteral>(*this).Fields());
|
||||
PrintFields(out, cast<TupleLiteral>(*this).Fields(), " = ");
|
||||
out << ")";
|
||||
break;
|
||||
case Expression::Kind::StructLiteral:
|
||||
out << "{";
|
||||
PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
|
||||
out << "}";
|
||||
break;
|
||||
case Expression::Kind::StructTypeLiteral:
|
||||
out << "{";
|
||||
PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
|
||||
out << "}";
|
||||
break;
|
||||
case Expression::Kind::IntLiteral:
|
||||
out << cast<IntLiteral>(*this).Val();
|
||||
break;
|
||||
|
||||
@@ -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