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
2022-06-14 21:49:01 -07:00
committed by GitHub
parent b83f5c3fc7
commit 2fef76e58c
26 changed files with 418 additions and 67 deletions
-1
View File
@@ -81,7 +81,6 @@ cc_library(
"//explorer/ast:declaration",
"//explorer/ast:expression",
"//explorer/ast:paren_contents",
"//explorer/ast:pattern",
"//explorer/ast:value_category",
"//explorer/common:arena",
"//explorer/common:error_builders",
+14 -1
View File
@@ -301,6 +301,15 @@ api_or_impl:
primary_expression:
identifier
{ $$ = arena->New<IdentifierExpression>(context.source_loc(), $1); }
| designator
{
// `.Foo` is rewritten to `.Self.Foo`.
$$ = arena->New<SimpleMemberAccessExpression>(
context.source_loc(),
arena->New<DotSelfExpression>(context.source_loc()), $1);
}
| PERIOD SELF
{ $$ = arena->New<DotSelfExpression>(context.source_loc()); }
| integer_literal
{ $$ = arena->New<IntLiteral>(context.source_loc(), $1); }
| string_literal
@@ -544,7 +553,11 @@ where_clause_list:
;
where_expression:
type_expression WHERE where_clause_list
{ $$ = arena->New<WhereExpression>(context.source_loc(), $1, $3); }
{
auto* self =
arena -> New<GenericBinding>(context.source_loc(), ".Self", $1);
$$ = arena->New<WhereExpression>(context.source_loc(), self, $3);
}
;
statement_expression:
ref_deref_expression