Files
carbon-lang/toolchain/check/operator.cpp
T
Dana JansensandJon Ross-Perkins 315e206ff1 Construct LocId from InstId directly (explicitly) instead of doing lookups when possible (#5355)
Remove calls to `InstStore::GetLocId()` to build a LocId from an InstId
now that they can be constructed directly from the InstId. Most uses of
LocId are just plumbing, so this does not affect them. However places
that want to look inside the LocId do not want to work with the InstId
form. In these places, introduce `InstStore::GetResolvedLocId()` which
converts a LocId (or an InstId as an optimization) into a LocId which is
not backed by an InstId. These locations can be printed (they have a
line and column when they are a NodeId), they can have flags added to
them (`ToImplicit`, `ToTokenOnly`), they can be converted to an
underlying ImportIRInstId, or they may be `None`.

`Dump()` is made to print a resolved location instead of printing the
InstId in the location, since (at least in my experience) the resolved
location is what is interesting in debugging, and this saves manual
`MakeInstId` steps in the debugger every time a location is of interest.

The LocId constructor from InstId is made `explicit` to add clarity to
function calls passing an `inst_id` now directly instead of calling
`context.insts().GetLocId(inst_id)`. To avoid needing to construct
`SemIR::LocId(...)` explicitly in all cases though, the diagnostics code
in Check uses `DiagnosticLocId` as its template parameter which accepts
InstId as well and does the construction of LocId from it.

Because LocId now requires an explicit construction from InstId, any
callers to `AddInst()` functions will have to explicitly convert to
LocId if they had an InstId, but not if they pass a NodeId. To make this
difference clear to callers, we `requires` that the input type can be
converted to LocId. This ensures that passing an InstId results in an
error at the callsite where the InstId is passed, instead of generating
a compiler error when trying to construct `LocIdAndInst` inside
`AddInst()`, which is less clear about what went wrong and doesn't seem
entirely intentional.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-04-28 19:06:24 +00:00

74 lines
2.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 "toolchain/check/operator.h"
#include "toolchain/check/call.h"
#include "toolchain/check/context.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/member_access.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
// Returns the `Op` function for the specified operator.
static auto GetOperatorOpFunction(Context& context, SemIR::LocId loc_id,
Operator op) -> SemIR::InstId {
auto implicit_loc_id = context.insts().GetCanonicalLocId(loc_id).ToImplicit();
// Look up the interface, and pass it any generic arguments.
auto interface_id =
LookupNameInCore(context, implicit_loc_id, op.interface_name);
if (!op.interface_args_ref.empty()) {
interface_id = PerformCall(context, implicit_loc_id, interface_id,
op.interface_args_ref);
}
// Look up the interface member.
auto op_name_id =
SemIR::NameId::ForIdentifier(context.identifiers().Add(op.op_name));
return PerformMemberAccess(context, implicit_loc_id, interface_id,
op_name_id);
}
auto BuildUnaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
SemIR::InstId operand_id,
MakeDiagnosticBuilderFn missing_impl_diagnoser)
-> SemIR::InstId {
// Look up the operator function.
auto op_fn = GetOperatorOpFunction(context, loc_id, op);
// Form `operand.(Op)`.
auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, operand_id,
op_fn, missing_impl_diagnoser);
if (bound_op_id == SemIR::ErrorInst::InstId) {
return SemIR::ErrorInst::InstId;
}
// Form `bound_op()`.
return PerformCall(context, loc_id, bound_op_id, {});
}
auto BuildBinaryOperator(Context& context, SemIR::LocId loc_id, Operator op,
SemIR::InstId lhs_id, SemIR::InstId rhs_id,
MakeDiagnosticBuilderFn missing_impl_diagnoser)
-> SemIR::InstId {
// Look up the operator function.
auto op_fn = GetOperatorOpFunction(context, loc_id, op);
// Form `lhs.(Op)`.
auto bound_op_id = PerformCompoundMemberAccess(context, loc_id, lhs_id, op_fn,
missing_impl_diagnoser);
if (bound_op_id == SemIR::ErrorInst::InstId) {
return SemIR::ErrorInst::InstId;
}
// Form `bound_op(rhs)`.
return PerformCall(context, loc_id, bound_op_id, {rhs_id});
}
} // namespace Carbon::Check