Basic support for .Self within :! bindings and where expressions. (#1311)

`.Self` is modeled as a new kind of expression, `DotSelfExpression`. Name resolution associates each occurrence of a `DotSelfExpression` with a generic binding, much like for an `IdentifierExpression`.

Both `:!` bindings and `where` expressions bring `.Self` into scope. The tentative intent is that if there are multiple `.Self`s in scope and they refer to different bindings, the result of using a `.Self` expression is an ambiguity error, but that is not implemented in this patch. Instead, like for `IdentifierExpression`s, we find the innermost enclosing definition of `.Self`.

`.Foo` expressions are rewritten to `.Self.Foo`, but this isn't enough to make them do anything useful, because associated constants aren't supported in general yet.
This commit is contained in:
Richard Smith authored and GitHub committed 2022-06-14 21:49:01 -07:00
1 parent b83f5c3fc7
commit 2fef76e58c
26 files changed
+418 -67

No files matched your search

+16 -1
View File
@@ -13,6 +13,7 @@
namespace Carbon {
using ::llvm::cast;
using ::llvm::isa;
static auto ExpressionToProto(const Expression& expression)
-> Fuzzing::Expression;
@@ -108,6 +109,13 @@ static auto ExpressionToProto(const Expression& expression)
case ExpressionKind::SimpleMemberAccessExpression: {
const auto& simple_member_access =
cast<SimpleMemberAccessExpression>(expression);
if (isa<DotSelfExpression>(simple_member_access.object())) {
// The parser rewrites `.Foo` into `.Self.Foo`. Undo this
// transformation.
auto* designator_proto = expression_proto.mutable_designator();
designator_proto->set_name(simple_member_access.member());
break;
}
auto* simple_member_access_proto =
expression_proto.mutable_simple_member_access();
simple_member_access_proto->set_field(simple_member_access.member());
@@ -182,7 +190,8 @@ static auto ExpressionToProto(const Expression& expression)
case ExpressionKind::WhereExpression: {
const auto& where = cast<WhereExpression>(expression);
auto* where_proto = expression_proto.mutable_where();
*where_proto->mutable_base() = ExpressionToProto(where.base());
*where_proto->mutable_base() =
ExpressionToProto(where.self_binding().type());
for (const WhereClause* where : where.clauses()) {
Fuzzing::WhereClause clause_proto;
switch (where->kind()) {
@@ -208,6 +217,12 @@ static auto ExpressionToProto(const Expression& expression)
break;
}
case ExpressionKind::DotSelfExpression: {
auto* designator_proto = expression_proto.mutable_designator();
designator_proto->set_name("Self");
break;
}
case ExpressionKind::IntrinsicExpression: {
const auto& intrinsic = cast<IntrinsicExpression>(expression);
auto* intrinsic_proto = expression_proto.mutable_intrinsic();