Files
carbon-lang/toolchain/check/handle_index.cpp
David Blaikie 309d3d63c5 Use IndirectIndexWith.Ref for subscripting when available (#7975)
When checking `a[i]` for a non-array type, first try
`IndirectIndexWith.Ref`, falling back to `IndexWith.At`, per the
indexing design. This allows subscripting to produce a reference
expression, so that `a[i] = v` works and non-copyable elements can be
accessed.

Extracted from #7536 (the `Core.Slice` proposal).
2026-10-17 00:14:10 +00:00

106 lines
4.7 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 <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/inst.h"
#include "toolchain/check/literal.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/check/operator.h"
#include "toolchain/check/type.h"
#include "toolchain/diagnostics/diagnostic.h"
#include "toolchain/sem_ir/expr_info.h"
#include "toolchain/sem_ir/inst.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
auto HandleParseNode(Context& /*context*/, Parse::IndexExprStartId /*node_id*/)
-> bool {
// Leave the expression on the stack for IndexExpr.
return true;
}
// Performs an index with base expression `operand_inst_id` for types that are
// not an array, using the `op_name` method of the interface `interface_name`.
// On failure, returns an `ErrorInst`, and diagnoses unless `diagnose` is false.
static auto PerformIndexOperator(Context& context, Parse::NodeId node_id,
CoreIdentifier interface_name,
CoreIdentifier op_name,
SemIR::InstId operand_inst_id,
SemIR::InstId index_inst_id, bool diagnose)
-> SemIR::InstId {
SemIR::InstId args[] = {context.types().GetTypeInstId(
context.insts().Get(index_inst_id).type_id())};
Operator op{.interface_name = interface_name,
.interface_args_ref = args,
.op_name = op_name};
return BuildBinaryOperator(context, node_id, op, operand_inst_id,
index_inst_id, diagnose);
}
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_type_id = context.insts().Get(operand_inst_id).type_id();
// `Core.Array(T, N)` is indexed as the primitive array type it adapts.
// `ArrayIndex` accepts an operand whose type adapts an array type directly.
// TODO: Model this as an `IndexWith` / `IndirectIndexWith` impl in the
// prelude once that can produce a durable reference.
if (auto array_type = TryGetAsArrayType(context, node_id, operand_type_id)) {
auto cast_index_id = ConvertToValueOfType(
context, SemIR::LocId(index_inst_id), index_inst_id,
// TODO: Replace this with impl lookup rather than hardcoding `i32`.
MakeIntType(context, node_id, SemIR::IntKind::Signed,
context.ints().Add(32)));
auto array_cat = SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
if (array_cat == SemIR::ExprCategory::Value) {
// If the operand is an array value, convert it to an ephemeral
// reference to an array so we can perform a primitive indexing into it.
operand_inst_id = AddInst<SemIR::ValueAsRef>(
context, node_id,
{.type_id = operand_type_id, .value_id = operand_inst_id});
}
// Constant evaluation will perform a bounds check on this array indexing
// if the index is constant.
auto elem_id = AddInst<SemIR::ArrayIndex>(
context, node_id,
{.type_id = context.types().GetTypeIdForTypeInstId(
array_type->element_type_inst_id),
.array_id = operand_inst_id,
.index_id = cast_index_id});
if (array_cat != SemIR::ExprCategory::DurableRef) {
// Indexing a durable reference gives a durable reference expression.
// Indexing anything else gives a value expression.
// TODO: This should be replaced by a choice between using `IndexWith`
// and `IndirectIndexWith`.
elem_id = ConvertToValueExpr(context, elem_id);
}
context.node_stack().Push(node_id, elem_id);
} else {
// Per the design, prefer `IndirectIndexWith` if the type implements it,
// otherwise fall back to `IndexWith`.
// TODO: Use `IndexWith.Ref` when the operand is a durable reference.
auto elem_id = PerformIndexOperator(
context, node_id, CoreIdentifier::IndirectIndexWith,
CoreIdentifier::Ref, operand_inst_id, index_inst_id,
/*diagnose=*/false);
if (elem_id == SemIR::ErrorInst::InstId) {
elem_id = PerformIndexOperator(
context, node_id, CoreIdentifier::IndexWith, CoreIdentifier::At,
operand_inst_id, index_inst_id, /*diagnose=*/true);
}
context.node_stack().Push(node_id, elem_id);
}
return true;
}
} // namespace Carbon::Check