|
|
@@ -2,11 +2,17 @@
|
|
|
// Exceptions. See /LICENSE for license information.
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
|
+#include <optional>
|
|
|
+
|
|
|
#include "toolchain/base/kind_switch.h"
|
|
|
#include "toolchain/check/context.h"
|
|
|
#include "toolchain/check/convert.h"
|
|
|
#include "toolchain/check/handle.h"
|
|
|
+#include "toolchain/check/operator.h"
|
|
|
+#include "toolchain/diagnostics/diagnostic.h"
|
|
|
+#include "toolchain/sem_ir/builtin_inst_kind.h"
|
|
|
#include "toolchain/sem_ir/inst.h"
|
|
|
+#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
|
|
namespace Carbon::Check {
|
|
|
|
|
|
@@ -16,17 +22,113 @@ auto HandleParseNode(Context& /*context*/, Parse::IndexExprStartId /*node_id*/)
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+// Returns the argument values of the `IndexWith` interface. Arguments
|
|
|
+// correspond to the `SubscriptType` and the `ElementType`. If no arguments are
|
|
|
+// used to define `IndexWith`, this returns an empty array reference. If the
|
|
|
+// class does not implement the said interface, this returns a `std::nullopt`.
|
|
|
+// TODO: Switch to using an associated type instead of a parameter for the
|
|
|
+// `ElementType`.
|
|
|
+static auto GetIndexWithArgs(Context& context, Parse::NodeId node_id,
|
|
|
+ SemIR::TypeId self_id)
|
|
|
+ -> std::optional<llvm::ArrayRef<SemIR::InstId>> {
|
|
|
+ auto index_with_inst_id = context.LookupNameInCore(node_id, "IndexWith");
|
|
|
+ // If the `IndexWith` interface doesn't have generic arguments then return an
|
|
|
+ // empty reference.
|
|
|
+ if (context.insts().Is<SemIR::InterfaceType>(index_with_inst_id)) {
|
|
|
+ return llvm::ArrayRef<SemIR::InstId>();
|
|
|
+ }
|
|
|
+
|
|
|
+ auto index_with_inst =
|
|
|
+ context.insts().TryGetAsIfValid<SemIR::StructValue>(index_with_inst_id);
|
|
|
+ if (!index_with_inst) {
|
|
|
+ return std::nullopt;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto index_with_interface =
|
|
|
+ context.types().TryGetAs<SemIR::GenericInterfaceType>(
|
|
|
+ index_with_inst->type_id);
|
|
|
+ if (!index_with_interface) {
|
|
|
+ return std::nullopt;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (const auto& impl : context.impls().array_ref()) {
|
|
|
+ auto impl_self_type_id = context.GetTypeIdForTypeInst(impl.self_id);
|
|
|
+ auto impl_constraint_type_id =
|
|
|
+ context.GetTypeIdForTypeInst(impl.constraint_id);
|
|
|
+
|
|
|
+ if (impl_self_type_id != self_id) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ auto interface_type =
|
|
|
+ context.types().TryGetAs<SemIR::InterfaceType>(impl_constraint_type_id);
|
|
|
+ if (!interface_type) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (index_with_interface->interface_id != interface_type->interface_id) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ return context.inst_blocks().GetOrEmpty(
|
|
|
+ context.specifics().Get(interface_type->specific_id).args_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ return std::nullopt;
|
|
|
+}
|
|
|
+
|
|
|
+// Performs an index with base expression `operand_inst_id` and
|
|
|
+// `operand_type_id` for types that are not an array. This checks if
|
|
|
+// the base expression implements the `IndexWith` interface; if so, uses the
|
|
|
+// `At` associative method, otherwise prints a diagnostic.
|
|
|
+static auto PerformIndexWith(Context& context, Parse::NodeId node_id,
|
|
|
+ SemIR::InstId operand_inst_id,
|
|
|
+ SemIR::TypeId operand_type_id,
|
|
|
+ SemIR::InstId index_inst_id) -> SemIR::InstId {
|
|
|
+ auto args = GetIndexWithArgs(context, node_id, operand_type_id);
|
|
|
+
|
|
|
+ // If the type does not implement the `IndexWith` interface, then return
|
|
|
+ // an error.
|
|
|
+ if (!args) {
|
|
|
+ CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
|
|
|
+ "type {0} does not support indexing", SemIR::TypeId);
|
|
|
+ context.emitter().Emit(node_id, TypeNotIndexable, operand_type_id);
|
|
|
+ return SemIR::InstId::BuiltinError;
|
|
|
+ }
|
|
|
+
|
|
|
+ Operator op{
|
|
|
+ .interface_name = "IndexWith",
|
|
|
+ .interface_args_ref = *args,
|
|
|
+ .op_name = "At",
|
|
|
+ };
|
|
|
+
|
|
|
+ // IndexWith is defined without generic arguments.
|
|
|
+ if (args->empty()) {
|
|
|
+ return BuildBinaryOperator(context, node_id, op, operand_inst_id,
|
|
|
+ index_inst_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ // The first argument of the `IndexWith` interface corresponds to the
|
|
|
+ // `SubscriptType`, so first cast `index_inst_id` to that type.
|
|
|
+ auto subscript_type_id = context.GetTypeIdForTypeInst((*args)[0]);
|
|
|
+ auto cast_index_id =
|
|
|
+ ConvertToValueOfType(context, node_id, index_inst_id, subscript_type_id);
|
|
|
+
|
|
|
+ return BuildBinaryOperator(context, node_id, op, operand_inst_id,
|
|
|
+ cast_index_id);
|
|
|
+}
|
|
|
+
|
|
|
auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
|
|
|
auto index_inst_id = context.node_stack().PopExpr();
|
|
|
auto operand_inst_id = context.node_stack().PopExpr();
|
|
|
operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
|
|
|
auto operand_inst = context.insts().Get(operand_inst_id);
|
|
|
auto operand_type_id = operand_inst.type_id();
|
|
|
+
|
|
|
CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
|
|
|
case CARBON_KIND(SemIR::ArrayType array_type): {
|
|
|
- auto index_node_id = context.insts().GetLocId(index_inst_id);
|
|
|
+ auto index_loc_id = context.insts().GetLocId(index_inst_id);
|
|
|
auto cast_index_id = ConvertToValueOfType(
|
|
|
- context, index_node_id, index_inst_id,
|
|
|
+ context, index_loc_id, index_inst_id,
|
|
|
context.GetBuiltinType(SemIR::BuiltinInstKind::IntType));
|
|
|
auto array_cat =
|
|
|
SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
|
|
|
@@ -52,13 +154,14 @@ auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
|
|
|
context.node_stack().Push(node_id, elem_id);
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
default: {
|
|
|
+ auto elem_id = SemIR::InstId::BuiltinError;
|
|
|
if (operand_type_id != SemIR::TypeId::Error) {
|
|
|
- CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
|
|
|
- "type {0} does not support indexing", TypeOfInstId);
|
|
|
- context.emitter().Emit(node_id, TypeNotIndexable, operand_inst_id);
|
|
|
+ elem_id = PerformIndexWith(context, node_id, operand_inst_id,
|
|
|
+ operand_type_id, index_inst_id);
|
|
|
}
|
|
|
- context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
|
|
|
+ context.node_stack().Push(node_id, elem_id);
|
|
|
return true;
|
|
|
}
|
|
|
}
|