mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Support is added for all overloaded operator interfaces in the current design apart from `Assign`, which is going to require some more work to properly handle, given that primitive assignment currently has a special implementation for quite a few builtin types. As we don't have support for generics yet -- in particular, generic interfaces -- there is no support for `*With` interfaces, but homogenous interfaces such as `Add` are supported instead. Factor out building of call expressions so that overloaded operators can generate calls. Switch a few places from using specific kinds of NodeId to a general NodeId. Because overloaded operators and other things like implicit conversions can result in member access and function calls, those operations can't require a specific kind of NodeId. Add import support for associated entities, and fix import support for interfaces and symbolic bindings. We now import interfaces in two steps, first importing a forward declaration then a definition, just like we do for classes. For symbolic bindings, we ensure that each BindSymbolicName is imported only once, because its ID is used as its symbolic identity. This is necessary because we (only) support operator interfaces that are defined in an imported Carbon package for now. The entire contents of `check/operator.cpp` should probably be rethought. In particular, doing a lot of name lookups on each operator is likely to be bad for performance. But this gets us to the point where overloaded operators are basically working, which seems like a good place to iterate from. For now, the tests that the individual operators map to the right interfaces are mostly generated by a script, but that's just because I'm expecting a fair bit of churn in how we define the prelude and the `impl`s -- in particular, when we add support for `AddWith`, we'll need to update all the tests. The plan is to remove the script once things settle down. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com> Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
149 lines
5.2 KiB
C++
149 lines
5.2 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 "toolchain/check/operator.h"
|
|
|
|
#include "toolchain/check/call.h"
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/member_access.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Returns the scope of the Core package, or Invalid if it's not found.
|
|
//
|
|
// TODO: Consider tracking the Core package in SemIR so we don't need to use
|
|
// name lookup to find it.
|
|
static auto GetCorePackage(Context& context, Parse::AnyExprId node_id)
|
|
-> SemIR::NameScopeId {
|
|
// TODO: If the current package is the `Core` package, return
|
|
// `SemIR::InstId::Package`.
|
|
|
|
auto ident_id = context.identifiers().Lookup("Core");
|
|
if (!ident_id.is_valid()) {
|
|
return SemIR::NameScopeId::Invalid;
|
|
}
|
|
auto name_id = SemIR::NameId::ForIdentifier(ident_id);
|
|
|
|
// Look up `package.Core`.
|
|
auto package_id = context.LookupQualifiedName(
|
|
node_id, name_id, SemIR::NameScopeId::Package, /*required=*/false);
|
|
if (!package_id.is_valid()) {
|
|
return SemIR::NameScopeId::Invalid;
|
|
}
|
|
|
|
// Look through import_refs and aliases.
|
|
package_id = context.constant_values().Get(package_id).inst_id();
|
|
|
|
// We expect it to be a package, and fail if not.
|
|
if (auto package_inst =
|
|
context.insts().TryGetAs<SemIR::Namespace>(package_id)) {
|
|
auto& name_scope = context.name_scopes().Get(package_inst->name_scope_id);
|
|
// Check that this is really the `Core` package and not an alias.
|
|
if (name_scope.is_closed_import && name_scope.name_id == name_id &&
|
|
name_scope.enclosing_scope_id == SemIR::NameScopeId::Package) {
|
|
return package_inst->name_scope_id;
|
|
}
|
|
}
|
|
return SemIR::NameScopeId::Invalid;
|
|
}
|
|
|
|
// Returns the name scope of the operator interface for the specified operator
|
|
// from the Core package.
|
|
static auto GetOperatorInterface(Context& context, Parse::AnyExprId node_id,
|
|
Operator op) -> SemIR::NameScopeId {
|
|
auto carbon_package_id = GetCorePackage(context, node_id);
|
|
if (!carbon_package_id.is_valid()) {
|
|
return SemIR::NameScopeId::Invalid;
|
|
}
|
|
|
|
// Lookup `Core.InterfaceName`.
|
|
auto interface_ident_id = context.identifiers().Add(op.interface_name);
|
|
auto interface_id = context.LookupQualifiedName(
|
|
node_id, SemIR::NameId::ForIdentifier(interface_ident_id),
|
|
carbon_package_id, /*required=*/false);
|
|
if (!interface_id.is_valid()) {
|
|
return SemIR::NameScopeId::Invalid;
|
|
}
|
|
|
|
// Look through import_refs and aliases.
|
|
interface_id = context.constant_values().Get(interface_id).inst_id();
|
|
|
|
// We expect it to be an interface.
|
|
if (auto interface_inst =
|
|
context.insts().TryGetAs<SemIR::InterfaceType>(interface_id)) {
|
|
return context.interfaces().Get(interface_inst->interface_id).scope_id;
|
|
}
|
|
return SemIR::NameScopeId::Invalid;
|
|
}
|
|
|
|
// Returns the `Op` function for the specified operator.
|
|
static auto GetOperatorOpFunction(Context& context, Parse::AnyExprId node_id,
|
|
Operator op) -> SemIR::InstId {
|
|
auto interface_scope_id = GetOperatorInterface(context, node_id, op);
|
|
if (!interface_scope_id.is_valid()) {
|
|
return SemIR::InstId::Invalid;
|
|
}
|
|
|
|
// Lookup `Interface.Op`.
|
|
auto op_ident_id = context.identifiers().Add(op.op_name);
|
|
auto op_id = context.LookupQualifiedName(
|
|
node_id, SemIR::NameId::ForIdentifier(op_ident_id), interface_scope_id,
|
|
/*required=*/false);
|
|
if (!op_id.is_valid()) {
|
|
return SemIR::InstId::Invalid;
|
|
}
|
|
|
|
// Look through import_refs and aliases.
|
|
op_id = context.constant_values().Get(op_id).inst_id();
|
|
|
|
// We expect it to be an associated function.
|
|
if (context.insts().Is<SemIR::AssociatedEntity>(op_id)) {
|
|
return op_id;
|
|
}
|
|
return SemIR::InstId::Invalid;
|
|
}
|
|
|
|
auto BuildUnaryOperator(Context& context, Parse::AnyExprId node_id, Operator op,
|
|
SemIR::InstId operand_id) -> SemIR::InstId {
|
|
auto op_fn = GetOperatorOpFunction(context, node_id, op);
|
|
if (!op_fn.is_valid()) {
|
|
context.TODO(node_id, "missing or invalid operator interface");
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
// Form `operand.(Op)`.
|
|
auto bound_op_id =
|
|
PerformCompoundMemberAccess(context, node_id, operand_id, op_fn);
|
|
if (bound_op_id == SemIR::InstId::BuiltinError) {
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
// Form `bound_op()`.
|
|
return PerformCall(context, node_id, bound_op_id, {});
|
|
}
|
|
|
|
auto BuildBinaryOperator(Context& context, Parse::AnyExprId node_id,
|
|
Operator op, SemIR::InstId lhs_id,
|
|
SemIR::InstId rhs_id) -> SemIR::InstId {
|
|
auto op_fn = GetOperatorOpFunction(context, node_id, op);
|
|
if (!op_fn.is_valid()) {
|
|
context.TODO(node_id, "missing or invalid operator interface");
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
// Form `lhs.(Op)`.
|
|
auto bound_op_id =
|
|
PerformCompoundMemberAccess(context, node_id, lhs_id, op_fn);
|
|
if (bound_op_id == SemIR::InstId::BuiltinError) {
|
|
return SemIR::InstId::BuiltinError;
|
|
}
|
|
|
|
// Form `bound_op(rhs)`.
|
|
return PerformCall(context, node_id, bound_op_id, {rhs_id});
|
|
}
|
|
|
|
} // namespace Carbon::Check
|