mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 06:48:11 +00:00
Implement initial support for `A.(B)` syntax, per #989. Specifically, this supports: * `object.(Type.member)` for instance members, * `object.(Interface.member)` for instance and non-instance members, * `object.(Type.(Interface.member))` for instance members, * `Type.(Interface.member)` for non-instance members. Three new AST nodes are introduced: * `CompoundFieldAccessExpression` represents the `A.(B)` syntax. * `MemberName` is a `Value` that represents the result of evaluating an expression such as `Type.member` or `Interface.member` or `Type.(Interface.member)`. * `TypeOfMemberName` is the type of a `MemberName` value. In order to handle members of classes and interfaces which have corresponding declarations and may need substitution into their types, and members of structs which don't have declarations but also don't need substitution, a class `Member` is introduced that can refer to either of these kinds of member. Co-authored-by: Geoff Romer <gromer@google.com> Co-authored-by: Jon Meow <jperkins@google.com>
465 lines
18 KiB
C++
465 lines
18 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "explorer/interpreter/resolve_names.h"
|
|
|
|
#include <set>
|
|
|
|
#include "explorer/ast/declaration.h"
|
|
#include "explorer/ast/expression.h"
|
|
#include "explorer/ast/pattern.h"
|
|
#include "explorer/ast/statement.h"
|
|
#include "explorer/ast/static_scope.h"
|
|
#include "llvm/Support/Casting.h"
|
|
#include "llvm/Support/Error.h"
|
|
|
|
using llvm::cast;
|
|
|
|
namespace Carbon {
|
|
|
|
// Adds the names exposed by the given AST node to enclosing_scope.
|
|
static auto AddExposedNames(const Declaration& declaration,
|
|
StaticScope& enclosing_scope) -> ErrorOr<Success>;
|
|
|
|
static auto AddExposedNames(const Declaration& declaration,
|
|
StaticScope& enclosing_scope) -> ErrorOr<Success> {
|
|
switch (declaration.kind()) {
|
|
case DeclarationKind::InterfaceDeclaration: {
|
|
auto& iface_decl = cast<InterfaceDeclaration>(declaration);
|
|
CARBON_RETURN_IF_ERROR(
|
|
enclosing_scope.Add(iface_decl.name(), &iface_decl));
|
|
break;
|
|
}
|
|
case DeclarationKind::ImplDeclaration: {
|
|
// Nothing to do here
|
|
break;
|
|
}
|
|
case DeclarationKind::FunctionDeclaration: {
|
|
auto& func = cast<FunctionDeclaration>(declaration);
|
|
CARBON_RETURN_IF_ERROR(enclosing_scope.Add(func.name(), &func));
|
|
break;
|
|
}
|
|
case DeclarationKind::ClassDeclaration: {
|
|
auto& class_decl = cast<ClassDeclaration>(declaration);
|
|
CARBON_RETURN_IF_ERROR(
|
|
enclosing_scope.Add(class_decl.name(), &class_decl));
|
|
break;
|
|
}
|
|
case DeclarationKind::ChoiceDeclaration: {
|
|
// Choice name is added to the scope after the choice's alternatives.
|
|
// See https://github.com/carbon-language/carbon-lang/issues/1248.
|
|
break;
|
|
}
|
|
case DeclarationKind::VariableDeclaration: {
|
|
auto& var = cast<VariableDeclaration>(declaration);
|
|
if (var.binding().name() != AnonymousName) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
enclosing_scope.Add(var.binding().name(), &var.binding()));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::SelfDeclaration: {
|
|
auto& self = cast<SelfDeclaration>(declaration);
|
|
CARBON_RETURN_IF_ERROR(enclosing_scope.Add("Self", &self));
|
|
break;
|
|
}
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
// Traverses the sub-AST rooted at the given node, resolving all names within
|
|
// it using enclosing_scope, and updating enclosing_scope to add names to
|
|
// it as they become available. In scopes where names are only visible below
|
|
// their point of declaration (such as block scopes in C++), this is implemented
|
|
// as a single pass, recursively calling ResolveNames on the elements of the
|
|
// scope in order. In scopes where names are also visible above their point of
|
|
// declaration (such as class scopes in C++), this requires two passes: first
|
|
// calling AddExposedNames on each element of the scope to populate a
|
|
// StaticScope, and then calling ResolveNames on each element, passing it the
|
|
// already-populated StaticScope.
|
|
static auto ResolveNames(Expression& expression,
|
|
const StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success>;
|
|
static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success>;
|
|
static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success>;
|
|
static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success>;
|
|
|
|
static auto ResolveNames(Expression& expression,
|
|
const StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success> {
|
|
switch (expression.kind()) {
|
|
case ExpressionKind::CallExpression: {
|
|
auto& call = cast<CallExpression>(expression);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(call.function(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(call.argument(), enclosing_scope));
|
|
break;
|
|
}
|
|
case ExpressionKind::FunctionTypeLiteral: {
|
|
auto& fun_type = cast<FunctionTypeLiteral>(expression);
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(fun_type.parameter(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(fun_type.return_type(), enclosing_scope));
|
|
break;
|
|
}
|
|
case ExpressionKind::FieldAccessExpression:
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(cast<FieldAccessExpression>(expression).aggregate(),
|
|
enclosing_scope));
|
|
break;
|
|
case ExpressionKind::CompoundFieldAccessExpression: {
|
|
auto& access = cast<CompoundFieldAccessExpression>(expression);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(access.object(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(access.path(), enclosing_scope));
|
|
break;
|
|
}
|
|
case ExpressionKind::IndexExpression: {
|
|
auto& index = cast<IndexExpression>(expression);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(index.aggregate(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(index.offset(), enclosing_scope));
|
|
break;
|
|
}
|
|
case ExpressionKind::PrimitiveOperatorExpression:
|
|
for (Nonnull<Expression*> operand :
|
|
cast<PrimitiveOperatorExpression>(expression).arguments()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*operand, enclosing_scope));
|
|
}
|
|
break;
|
|
case ExpressionKind::TupleLiteral:
|
|
for (Nonnull<Expression*> field :
|
|
cast<TupleLiteral>(expression).fields()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
|
|
}
|
|
break;
|
|
case ExpressionKind::StructLiteral:
|
|
for (FieldInitializer& init : cast<StructLiteral>(expression).fields()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(init.expression(), enclosing_scope));
|
|
}
|
|
break;
|
|
case ExpressionKind::StructTypeLiteral:
|
|
for (FieldInitializer& init :
|
|
cast<StructTypeLiteral>(expression).fields()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(init.expression(), enclosing_scope));
|
|
}
|
|
break;
|
|
case ExpressionKind::IdentifierExpression: {
|
|
auto& identifier = cast<IdentifierExpression>(expression);
|
|
CARBON_ASSIGN_OR_RETURN(
|
|
const auto value_node,
|
|
enclosing_scope.Resolve(identifier.name(), identifier.source_loc()));
|
|
identifier.set_value_node(value_node);
|
|
break;
|
|
}
|
|
case ExpressionKind::IntrinsicExpression:
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(
|
|
cast<IntrinsicExpression>(expression).args(), enclosing_scope));
|
|
break;
|
|
case ExpressionKind::IfExpression: {
|
|
auto& if_expr = cast<IfExpression>(expression);
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(if_expr.condition(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(if_expr.then_expression(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(if_expr.else_expression(), enclosing_scope));
|
|
break;
|
|
}
|
|
case ExpressionKind::ArrayTypeLiteral: {
|
|
auto& array_literal = cast<ArrayTypeLiteral>(expression);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(
|
|
array_literal.element_type_expression(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(array_literal.size_expression(), enclosing_scope));
|
|
break;
|
|
}
|
|
case ExpressionKind::BoolTypeLiteral:
|
|
case ExpressionKind::BoolLiteral:
|
|
case ExpressionKind::IntTypeLiteral:
|
|
case ExpressionKind::ContinuationTypeLiteral:
|
|
case ExpressionKind::IntLiteral:
|
|
case ExpressionKind::StringLiteral:
|
|
case ExpressionKind::StringTypeLiteral:
|
|
case ExpressionKind::TypeTypeLiteral:
|
|
break;
|
|
case ExpressionKind::InstantiateImpl: // created after name resolution
|
|
case ExpressionKind::UnimplementedExpression:
|
|
return CompilationError(expression.source_loc()) << "Unimplemented";
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
static auto ResolveNames(Pattern& pattern, StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success> {
|
|
switch (pattern.kind()) {
|
|
case PatternKind::BindingPattern: {
|
|
auto& binding = cast<BindingPattern>(pattern);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
|
|
if (binding.name() != AnonymousName) {
|
|
CARBON_RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
|
|
}
|
|
break;
|
|
}
|
|
case PatternKind::GenericBinding: {
|
|
auto& binding = cast<GenericBinding>(pattern);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(binding.type(), enclosing_scope));
|
|
if (binding.name() != AnonymousName) {
|
|
CARBON_RETURN_IF_ERROR(enclosing_scope.Add(binding.name(), &binding));
|
|
}
|
|
break;
|
|
}
|
|
case PatternKind::TuplePattern:
|
|
for (Nonnull<Pattern*> field : cast<TuplePattern>(pattern).fields()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*field, enclosing_scope));
|
|
}
|
|
break;
|
|
case PatternKind::AlternativePattern: {
|
|
auto& alternative = cast<AlternativePattern>(pattern);
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(alternative.choice_type(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(alternative.arguments(), enclosing_scope));
|
|
break;
|
|
}
|
|
case PatternKind::ExpressionPattern:
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(
|
|
cast<ExpressionPattern>(pattern).expression(), enclosing_scope));
|
|
break;
|
|
case PatternKind::AutoPattern:
|
|
break;
|
|
case PatternKind::VarPattern:
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(cast<VarPattern>(pattern).pattern(), enclosing_scope));
|
|
break;
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success> {
|
|
switch (statement.kind()) {
|
|
case StatementKind::ExpressionStatement:
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(
|
|
cast<ExpressionStatement>(statement).expression(), enclosing_scope));
|
|
break;
|
|
case StatementKind::Assign: {
|
|
auto& assign = cast<Assign>(statement);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(assign.lhs(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(assign.rhs(), enclosing_scope));
|
|
break;
|
|
}
|
|
case StatementKind::VariableDefinition: {
|
|
auto& def = cast<VariableDefinition>(statement);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(def.init(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(def.pattern(), enclosing_scope));
|
|
break;
|
|
}
|
|
case StatementKind::If: {
|
|
auto& if_stmt = cast<If>(statement);
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(if_stmt.condition(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(if_stmt.then_block(), enclosing_scope));
|
|
if (if_stmt.else_block().has_value()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(**if_stmt.else_block(), enclosing_scope));
|
|
}
|
|
break;
|
|
}
|
|
case StatementKind::Return:
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(cast<Return>(statement).expression(), enclosing_scope));
|
|
break;
|
|
case StatementKind::Block: {
|
|
auto& block = cast<Block>(statement);
|
|
StaticScope block_scope;
|
|
block_scope.AddParent(&enclosing_scope);
|
|
for (Nonnull<Statement*> sub_statement : block.statements()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*sub_statement, block_scope));
|
|
}
|
|
break;
|
|
}
|
|
case StatementKind::While: {
|
|
auto& while_stmt = cast<While>(statement);
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(while_stmt.condition(), enclosing_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(while_stmt.body(), enclosing_scope));
|
|
break;
|
|
}
|
|
case StatementKind::Match: {
|
|
auto& match = cast<Match>(statement);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(match.expression(), enclosing_scope));
|
|
for (Match::Clause& clause : match.clauses()) {
|
|
StaticScope clause_scope;
|
|
clause_scope.AddParent(&enclosing_scope);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(clause.pattern(), clause_scope));
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(clause.statement(), clause_scope));
|
|
}
|
|
break;
|
|
}
|
|
case StatementKind::Continuation: {
|
|
auto& continuation = cast<Continuation>(statement);
|
|
CARBON_RETURN_IF_ERROR(
|
|
enclosing_scope.Add(continuation.name(), &continuation));
|
|
StaticScope continuation_scope;
|
|
continuation_scope.AddParent(&enclosing_scope);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(cast<Continuation>(statement).body(),
|
|
continuation_scope));
|
|
break;
|
|
}
|
|
case StatementKind::Run:
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(cast<Run>(statement).argument(), enclosing_scope));
|
|
break;
|
|
case StatementKind::Await:
|
|
case StatementKind::Break:
|
|
case StatementKind::Continue:
|
|
break;
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope)
|
|
-> ErrorOr<Success> {
|
|
switch (declaration.kind()) {
|
|
case DeclarationKind::InterfaceDeclaration: {
|
|
auto& iface = cast<InterfaceDeclaration>(declaration);
|
|
StaticScope iface_scope;
|
|
iface_scope.AddParent(&enclosing_scope);
|
|
if (iface.params().has_value()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(**iface.params(), iface_scope));
|
|
}
|
|
CARBON_RETURN_IF_ERROR(iface_scope.Add("Self", iface.self()));
|
|
for (Nonnull<Declaration*> member : iface.members()) {
|
|
CARBON_RETURN_IF_ERROR(AddExposedNames(*member, iface_scope));
|
|
}
|
|
for (Nonnull<Declaration*> member : iface.members()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*member, iface_scope));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ImplDeclaration: {
|
|
auto& impl = cast<ImplDeclaration>(declaration);
|
|
StaticScope impl_scope;
|
|
impl_scope.AddParent(&enclosing_scope);
|
|
for (Nonnull<GenericBinding*> binding : impl.deduced_parameters()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(binding->type(), impl_scope));
|
|
CARBON_RETURN_IF_ERROR(impl_scope.Add(binding->name(), binding));
|
|
}
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*impl.impl_type(), impl_scope));
|
|
// Only add `Self` to the impl_scope if it is not already in the enclosing
|
|
// scope. Add `Self` after we resolve names for the impl_type, so you
|
|
// can't write something like `impl Vector(Self) as ...`. Add `Self`
|
|
// before resolving names in the interface, so you can write something
|
|
// like `impl VeryLongTypeName as AddWith(Self)`
|
|
if (!enclosing_scope.Resolve("Self", impl.source_loc()).ok()) {
|
|
CARBON_RETURN_IF_ERROR(AddExposedNames(*impl.self(), impl_scope));
|
|
}
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(impl.interface(), impl_scope));
|
|
for (Nonnull<Declaration*> member : impl.members()) {
|
|
CARBON_RETURN_IF_ERROR(AddExposedNames(*member, impl_scope));
|
|
}
|
|
for (Nonnull<Declaration*> member : impl.members()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*member, impl_scope));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::FunctionDeclaration: {
|
|
auto& function = cast<FunctionDeclaration>(declaration);
|
|
StaticScope function_scope;
|
|
function_scope.AddParent(&enclosing_scope);
|
|
for (Nonnull<GenericBinding*> binding : function.deduced_parameters()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(binding->type(), function_scope));
|
|
CARBON_RETURN_IF_ERROR(function_scope.Add(binding->name(), binding));
|
|
}
|
|
if (function.is_method()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(function.me_pattern(), function_scope));
|
|
}
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(function.param_pattern(), function_scope));
|
|
if (function.return_term().type_expression().has_value()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(
|
|
**function.return_term().type_expression(), function_scope));
|
|
}
|
|
if (function.body().has_value()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(**function.body(), function_scope));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ClassDeclaration: {
|
|
auto& class_decl = cast<ClassDeclaration>(declaration);
|
|
StaticScope class_scope;
|
|
class_scope.AddParent(&enclosing_scope);
|
|
CARBON_RETURN_IF_ERROR(class_scope.Add(class_decl.name(), &class_decl));
|
|
CARBON_RETURN_IF_ERROR(AddExposedNames(*class_decl.self(), class_scope));
|
|
if (class_decl.type_params().has_value()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(**class_decl.type_params(), class_scope));
|
|
}
|
|
|
|
// TODO: Disable unqualified access of members by other members for now.
|
|
// Put it back later, but in a way that turns unqualified accesses
|
|
// into qualified ones, so that generic classes and impls
|
|
// behave the in the right way. -Jeremy
|
|
// for (Nonnull<Declaration*> member : class_decl.members()) {
|
|
// AddExposedNames(*member, class_scope);
|
|
// }
|
|
for (Nonnull<Declaration*> member : class_decl.members()) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*member, class_scope));
|
|
}
|
|
break;
|
|
}
|
|
case DeclarationKind::ChoiceDeclaration: {
|
|
auto& choice = cast<ChoiceDeclaration>(declaration);
|
|
// Alternative names are never used unqualified, so we don't need to
|
|
// add the alternatives to a scope, or introduce a new scope; we only
|
|
// need to check for duplicates.
|
|
std::set<std::string_view> alternative_names;
|
|
for (Nonnull<AlternativeSignature*> alternative : choice.alternatives()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(alternative->signature(), enclosing_scope));
|
|
if (!alternative_names.insert(alternative->name()).second) {
|
|
return CompilationError(alternative->source_loc())
|
|
<< "Duplicate name `" << alternative->name()
|
|
<< "` in choice type";
|
|
}
|
|
}
|
|
CARBON_RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice));
|
|
break;
|
|
}
|
|
case DeclarationKind::VariableDeclaration: {
|
|
auto& var = cast<VariableDeclaration>(declaration);
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(var.binding(), enclosing_scope));
|
|
if (var.has_initializer()) {
|
|
CARBON_RETURN_IF_ERROR(
|
|
ResolveNames(var.initializer(), enclosing_scope));
|
|
}
|
|
break;
|
|
}
|
|
|
|
case DeclarationKind::SelfDeclaration: {
|
|
CARBON_FATAL() << "Unreachable: resolving names for `Self` declaration";
|
|
}
|
|
}
|
|
return Success();
|
|
}
|
|
|
|
auto ResolveNames(AST& ast) -> ErrorOr<Success> {
|
|
StaticScope file_scope;
|
|
for (auto declaration : ast.declarations) {
|
|
CARBON_RETURN_IF_ERROR(AddExposedNames(*declaration, file_scope));
|
|
}
|
|
for (auto declaration : ast.declarations) {
|
|
CARBON_RETURN_IF_ERROR(ResolveNames(*declaration, file_scope));
|
|
}
|
|
return ResolveNames(**ast.main_call, file_scope);
|
|
}
|
|
|
|
} // namespace Carbon
|