Initial support for where expressions. (#1310)

Build constraint types from `where` expressions. This is very bare-bones; there's no support for constraining `.Self` or associated types yet, but degenerate cases not involving self-reference work.

`==` constraints are syntactically supported but not semantically verified yet, and they aren't used for anything.
This commit is contained in:
Richard Smith
2022-06-09 10:31:04 -07:00
committed by GitHub
parent 36b9d997b7
commit 9007b3952d
20 changed files with 677 additions and 78 deletions
+29
View File
@@ -171,6 +171,15 @@ void Expression::Print(llvm::raw_ostream& out) const {
<< if_expr.then_expression() << " else " << if_expr.else_expression();
break;
}
case ExpressionKind::WhereExpression: {
const auto& where = cast<WhereExpression>(*this);
out << where.base() << " where ";
llvm::ListSeparator sep(" and ");
for (const WhereClause* clause : where.clauses()) {
out << sep << *clause;
}
break;
}
case ExpressionKind::InstantiateImpl: {
const auto& inst_impl = cast<InstantiateImpl>(*this);
out << "instantiate " << *inst_impl.generic_impl();
@@ -246,6 +255,7 @@ void Expression::PrintID(llvm::raw_ostream& out) const {
case ExpressionKind::SimpleMemberAccessExpression:
case ExpressionKind::CompoundMemberAccessExpression:
case ExpressionKind::IfExpression:
case ExpressionKind::WhereExpression:
case ExpressionKind::TupleLiteral:
case ExpressionKind::StructLiteral:
case ExpressionKind::StructTypeLiteral:
@@ -261,4 +271,23 @@ void Expression::PrintID(llvm::raw_ostream& out) const {
}
}
WhereClause::~WhereClause() = default;
void WhereClause::Print(llvm::raw_ostream& out) const {
switch (kind()) {
case WhereClauseKind::IsWhereClause: {
auto& clause = cast<IsWhereClause>(*this);
out << clause.type() << " is " << clause.constraint();
break;
}
case WhereClauseKind::EqualsWhereClause: {
auto& clause = cast<EqualsWhereClause>(*this);
out << clause.lhs() << " == " << clause.rhs();
break;
}
}
}
void WhereClause::PrintID(llvm::raw_ostream& out) const { out << "..."; }
} // namespace Carbon