Files
carbon-lang/toolchain/check/eval.cpp
T
Dana Jansens 6f1ae86ce4 Disable -Wunused-template in clangd-tidy (#7750)
This is firing on some of our _used_ templates in eval.cpp in clang-tidy
24

It was coming to `-Wall` for clang as well
(https://github.com/llvm/llvm-project/issues/202945) but was reverted
due to issues like false positives
(https://github.com/llvm/llvm-project/pull/218638). Some fixes were
applied to try enable in `-Wall` in clang 23
(https://github.com/llvm/llvm-project/pull/222336) but it still remains
disabled by default for clang.
2026-09-10 19:28:25 +00:00

3871 lines
159 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/eval.h"
#include <algorithm>
#include <array>
#include <optional>
#include <utility>
#include "common/raw_string_ostream.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/Support/ConvertUTF.h"
#include "toolchain/base/canonical_value_store.h"
#include "toolchain/base/int.h"
#include "toolchain/base/kind_switch.h"
#include "toolchain/base/value_ids.h"
#include "toolchain/check/action.h"
#include "toolchain/check/context.h"
#include "toolchain/check/cpp/constant.h"
#include "toolchain/check/diagnostic_helpers.h"
#include "toolchain/check/eval_inst.h"
#include "toolchain/check/facet_type.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/import_ref.h"
#include "toolchain/check/inst.h"
#include "toolchain/check/name_lookup.h"
#include "toolchain/check/period_self.h"
#include "toolchain/check/type.h"
#include "toolchain/check/type_completion.h"
#include "toolchain/diagnostics/diagnostic.h"
#include "toolchain/diagnostics/emitter.h"
#include "toolchain/diagnostics/format_providers.h"
#include "toolchain/lex/token_kind.h"
#include "toolchain/sem_ir/builtin_function_kind.h"
#include "toolchain/sem_ir/constant.h"
#include "toolchain/sem_ir/declared_facet_type.h"
#include "toolchain/sem_ir/function.h"
#include "toolchain/sem_ir/generic.h"
#include "toolchain/sem_ir/id_kind.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/impl.h"
#include "toolchain/sem_ir/inst_categories.h"
#include "toolchain/sem_ir/inst_kind.h"
#include "toolchain/sem_ir/specific_named_constraint.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::Check {
namespace {
// Information about an eval block of a specific that we are currently building.
struct SpecificEvalInfo {
// The region within the specific whose eval block we are building.
SemIR::GenericInstIndex::Region region;
// The work-in-progress contents of the eval block.
llvm::ArrayRef<SemIR::InstId> values;
};
// Information about a local scope that we're currently evaluating, such as a
// call to an `eval fn`. In this scope, instructions with runtime phase may
// locally have constant values, for example values that are computed from the
// arguments to the call. These values are specific to the current evaluation
// and not global properties of the instruction.
struct LocalEvalInfo {
// A mapping from instructions with runtime phase within the local scope to
// the values that they have in the current evaluation. This is populated as
// the local scope is evaluated, and due to control flow, the same instruction
// may have its value set multiple times. This map tracks the most recent
// value that the instruction had, which is the one that a reference to it in
// well-formed SemIR should refer to.
Map<SemIR::InstId, SemIR::ConstantId>* locals;
};
// Information about the context within which we are performing evaluation.
// `context` must not be null.
class EvalContext {
public:
explicit EvalContext(Context* context, SemIR::LocId fallback_loc_id,
SemIR::SpecificId specific_id = SemIR::SpecificId::None)
: context_(context),
fallback_loc_id_(fallback_loc_id),
specific_id_(specific_id) {}
EvalContext(const EvalContext&) = delete;
auto operator=(const EvalContext&) -> EvalContext& = delete;
// Gets the location to use for diagnostics if a better location is
// unavailable.
// TODO: This is also sometimes unavailable.
auto fallback_loc_id() const -> SemIR::LocId { return fallback_loc_id_; }
// Returns a location to use to point at an instruction in a diagnostic, given
// a list of instructions that might have an attached location. This is the
// location of the first instruction in the list that has a location if there
// is one, and otherwise the fallback location.
auto GetDiagnosticLoc(llvm::ArrayRef<SemIR::InstId> inst_ids)
-> SemIR::LocId {
for (auto inst_id : inst_ids) {
if (inst_id.has_value()) {
auto loc_id = context_->insts().GetCanonicalLocId(inst_id);
if (loc_id.has_value()) {
return loc_id;
}
}
}
return fallback_loc_id_;
}
// Gets the value of the specified compile-time binding in this context.
// Returns `None` if the value is not fixed in this context.
auto GetCompileTimeBindValue(SemIR::SymbolicBinding binding)
-> SemIR::ConstantId {
// If we know which specific we're evaluating within and this is an argument
// of that specific, its constant value is the corresponding argument value.
const auto& binding_name = entity_names().Get(binding.entity_name_id);
if (!binding_name.bind_index().has_value() || !specific_id_.has_value()) {
return SemIR::ConstantId::None;
}
auto binding_index = binding_name.bind_index().index;
const auto& specific = specifics().Get(specific_id_);
auto args = inst_blocks().Get(specific.args_id);
// Bindings past the ones with known arguments can appear as local
// bindings of entities declared within this generic.
if (static_cast<size_t>(binding_index) >= args.size()) {
return SemIR::ConstantId::None;
}
// Check that the binding belongs to the current generic.
{
auto generic = generics().Get(specific.generic_id);
auto generic_bindings = inst_blocks().Get(generic.bindings_id);
CARBON_CHECK(static_cast<size_t>(binding_index) <
generic_bindings.size());
auto generic_binding_const_id =
constant_values().Get(generic_bindings[binding_index]);
auto generic_binding =
constant_values().GetInstAs<SemIR::SymbolicBinding>(
generic_binding_const_id);
auto generic_binding_name =
entity_names().Get(generic_binding.entity_name_id);
// TODO: consider checking more fields of EntityName. But note that even
// if we check all of them, there will still be false negatives when
// different bindings happen to have the same EntityName representation.
CARBON_CHECK(generic_binding.type_id == binding.type_id &&
generic_binding_name.name_id == binding_name.name_id,
"Binding {0} does not belong to generic {1}", binding_name,
generic);
}
return constant_values().Get(args[binding_index]);
}
// Gets the constant value of the specified instruction in this context.
auto GetConstantValue(SemIR::InstId inst_id) -> SemIR::ConstantId {
auto const_id = constant_values().GetAttached(inst_id);
// While evaluating a function, map from local non-constant instructions to
// their earlier-evaluated values.
if (!const_id.is_constant()) {
if (local_eval_info_) {
if (auto local = local_eval_info_->locals->Lookup(inst_id)) {
return local.value();
}
}
return const_id;
}
return GetConstantValueInSpecific(sem_ir(), specific_id_, inst_id);
}
// Gets the type of the specified instruction in this context.
auto GetTypeOfInst(SemIR::InstId inst_id) -> SemIR::TypeId {
return GetTypeOfInstInSpecific(sem_ir(), specific_id_, inst_id);
}
auto ints() -> SharedValueStores::IntStore& { return sem_ir().ints(); }
auto floats() -> SharedValueStores::FloatStore& { return sem_ir().floats(); }
auto entity_names() -> SemIR::EntityNameStore& {
return sem_ir().entity_names();
}
auto functions() -> const SemIR::FunctionStore& {
return sem_ir().functions();
}
auto classes() -> const SemIR::ClassStore& { return sem_ir().classes(); }
auto interfaces() -> const SemIR::InterfaceStore& {
return sem_ir().interfaces();
}
auto specific_interfaces() -> SemIR::SpecificInterfaceStore& {
return sem_ir().specific_interfaces();
}
auto declared_facet_types() -> SemIR::DeclaredFacetTypeStore& {
return sem_ir().declared_facet_types();
}
auto generics() -> const SemIR::GenericStore& { return sem_ir().generics(); }
auto specifics() -> const SemIR::SpecificStore& {
return sem_ir().specifics();
}
auto insts() -> const SemIR::InstStore& { return sem_ir().insts(); }
auto inst_blocks() -> SemIR::InstBlockStore& {
return sem_ir().inst_blocks();
}
// Gets the constant value store. Note that this does not provide the constant
// values that should be used from this evaluation context, and so should be
// used with caution.
auto constant_values() -> const SemIR::ConstantValueStore& {
return sem_ir().constant_values();
}
// Gets the types store. Note that this does not provide the type values that
// should be used from this evaluation context, and so should be used with
// caution.
auto types() -> const SemIR::TypeStore& { return sem_ir().types(); }
auto context() -> Context& { return *context_; }
auto sem_ir() -> SemIR::File& { return context().sem_ir(); }
auto specific_id() -> SemIR::SpecificId { return specific_id_; }
auto emitter() -> DiagnosticEmitterBase& { return context().emitter(); }
protected:
explicit EvalContext(Context* context, SemIR::LocId fallback_loc_id,
SemIR::SpecificId specific_id,
std::optional<LocalEvalInfo> local_eval_info)
: context_(context),
fallback_loc_id_(fallback_loc_id),
specific_id_(specific_id),
local_eval_info_(local_eval_info) {}
// Returns the current locals map, which is assumed to exist.
auto locals() -> Map<SemIR::InstId, SemIR::ConstantId>& {
return *local_eval_info_->locals;
}
private:
// The type-checking context in which we're performing evaluation.
Context* context_;
// The location to use for diagnostics when a better location isn't available.
SemIR::LocId fallback_loc_id_;
// The specific that we are evaluating within.
SemIR::SpecificId specific_id_;
// If we are currently evaluating within a local scope, values of local
// instructions that have already been evaluated. This is here rather than in
// `FunctionEvalContext` so we can reference it from `GetConstantValue`.
std::optional<LocalEvalInfo> local_eval_info_;
};
} // namespace
namespace {
// The evaluation phase for an expression, computed by evaluation. These are
// ordered so that the phase of an expression is the numerically highest phase
// of its constituent evaluations. Note that an expression with any runtime
// component is known to have Runtime phase even if it involves an evaluation
// with UnknownDueToError phase.
enum class Phase : uint8_t {
// Value could be entirely and concretely computed.
Concrete,
// Evaluation phase is symbolic because the expression involves specifically a
// reference to `.Self`.
PeriodSelfSymbolic,
// Evaluation phase is symbolic because the expression involves a reference to
// a non-template symbolic binding other than `.Self`.
CheckedSymbolic,
// Evaluation phase is symbolic because the expression involves a reference to
// a template parameter, or otherwise depends on something template dependent.
// The expression might also reference non-template symbolic bindings.
TemplateSymbolic,
// The evaluation phase is unknown because evaluation encountered an
// already-diagnosed semantic or syntax error. This is treated as being
// potentially constant, but with an unknown phase.
UnknownDueToError,
// The expression has runtime phase because of a non-constant subexpression.
Runtime,
};
} // namespace
static auto IsConstantOrError(Phase phase) -> bool {
return phase != Phase::Runtime;
}
// Gets the phase in which the value of a constant will become available.
static auto GetPhase(const SemIR::ConstantValueStore& constant_values,
SemIR::ConstantId constant_id) -> Phase {
if (!constant_id.is_constant()) {
return Phase::Runtime;
} else if (constant_id == SemIR::ErrorInst::ConstantId) {
return Phase::UnknownDueToError;
}
switch (constant_values.GetDependence(constant_id)) {
case SemIR::ConstantDependence::None:
return Phase::Concrete;
case SemIR::ConstantDependence::PeriodSelf:
return Phase::PeriodSelfSymbolic;
case SemIR::ConstantDependence::Checked:
return Phase::CheckedSymbolic;
case SemIR::ConstantDependence::Template:
return Phase::TemplateSymbolic;
}
}
// Returns the later of two phases.
static auto LatestPhase(Phase a, Phase b) -> Phase {
return static_cast<Phase>(
std::max(static_cast<uint8_t>(a), static_cast<uint8_t>(b)));
}
// Forms a `constant_id` describing a given evaluation result.
static auto MakeConstantResult(Context& context, SemIR::Inst inst, Phase phase)
-> SemIR::ConstantId {
switch (phase) {
case Phase::Concrete:
return context.constants().GetOrAdd(inst,
SemIR::ConstantDependence::None);
case Phase::PeriodSelfSymbolic:
return context.constants().GetOrAdd(
inst, SemIR::ConstantDependence::PeriodSelf);
case Phase::CheckedSymbolic:
return context.constants().GetOrAdd(inst,
SemIR::ConstantDependence::Checked);
case Phase::TemplateSymbolic:
return context.constants().GetOrAdd(inst,
SemIR::ConstantDependence::Template);
case Phase::UnknownDueToError:
return SemIR::ErrorInst::ConstantId;
case Phase::Runtime:
return SemIR::ConstantId::NotConstant;
}
}
// Forms a `constant_id` describing why an evaluation was not constant.
static auto MakeNonConstantResult(Phase phase) -> SemIR::ConstantId {
return phase == Phase::UnknownDueToError ? SemIR::ErrorInst::ConstantId
: SemIR::ConstantId::NotConstant;
}
// Forms a constant for an empty tuple value.
static auto MakeEmptyTupleResult(EvalContext& eval_context)
-> SemIR::ConstantId {
auto type_id = GetTupleType(eval_context.context(), {});
return MakeConstantResult(
eval_context.context(),
SemIR::TupleValue{.type_id = type_id,
.elements_id = SemIR::InstBlockId::Empty},
Phase::Concrete);
}
// Converts a bool value into a ConstantId.
static auto MakeBoolResult(Context& context, SemIR::TypeId bool_type_id,
bool result) -> SemIR::ConstantId {
return MakeConstantResult(
context,
SemIR::BoolLiteral{.type_id = bool_type_id,
.value = SemIR::BoolValue::From(result)},
Phase::Concrete);
}
// Converts an IntId value into a ConstantId.
static auto MakeIntResult(Context& context, SemIR::TypeId type_id, IntId int_id)
-> SemIR::ConstantId {
return MakeConstantResult(
context, SemIR::IntValue{.type_id = type_id, .int_id = int_id},
Phase::Concrete);
}
// Converts an APInt value into a ConstantId.
static auto MakeIntResult(Context& context, SemIR::TypeId type_id,
bool is_signed, llvm::APInt value)
-> SemIR::ConstantId {
CARBON_CHECK(is_signed == context.types().IsSignedInt(type_id));
auto result = is_signed ? context.ints().AddSigned(std::move(value))
: context.ints().AddUnsigned(std::move(value));
return MakeIntResult(context, type_id, result);
}
// Converts a Real into a ConstantId.
static auto MakeFloatLiteralResult(Context& context, Real real)
-> SemIR::ConstantId {
auto real_id = context.reals().Add(real);
return MakeConstantResult(
context,
SemIR::FloatLiteralValue{
.type_id =
GetSingletonType(context, SemIR::FloatLiteralType::TypeInstId),
.real_id = real_id},
Phase::Concrete);
}
// Converts an APFloat value into a ConstantId.
static auto MakeFloatResult(Context& context, SemIR::TypeId type_id,
llvm::APFloat value) -> SemIR::ConstantId {
auto result = context.floats().Add(std::move(value));
return MakeConstantResult(
context, SemIR::FloatValue{.type_id = type_id, .float_id = result},
Phase::Concrete);
}
// Creates a FacetType constant.
static auto MakeFacetTypeResult(
Context& context, const SemIR::DeclaredFacetType& declared_facet_type,
Phase phase) -> SemIR::ConstantId {
SemIR::DeclaredFacetTypeId declared_facet_type_id =
context.declared_facet_types().Add(declared_facet_type);
return MakeConstantResult(
context,
SemIR::FacetType{.type_id = SemIR::TypeType::TypeId,
.declared_facet_type_id = declared_facet_type_id},
phase);
}
// `GetConstantValue` checks to see whether the provided ID describes a value
// with constant phase, and if so, returns the corresponding constant value.
// Overloads are provided for different kinds of ID. `RequireConstantValue` does
// the same, but produces an error diagnostic if the input is not constant.
// AbsoluteInstId can not have its values substituted, so this overload is
// deleted. This prevents conversion to InstId.
static auto GetConstantValue(EvalContext& eval_context,
SemIR::AbsoluteInstId inst_id, Phase* phase)
-> SemIR::InstId = delete;
// If the given instruction is constant, returns its constant value.
static auto GetConstantValue(EvalContext& eval_context, SemIR::InstId inst_id,
Phase* phase) -> SemIR::InstId {
if (!inst_id.has_value()) {
return SemIR::InstId::None;
}
auto const_id = eval_context.GetConstantValue(inst_id);
*phase =
LatestPhase(*phase, GetPhase(eval_context.constant_values(), const_id));
return eval_context.constant_values().GetInstId(const_id);
}
// Issue a suitable diagnostic for an instruction that evaluated to a
// non-constant value but was required to evaluate to a constant.
static auto DiagnoseNonConstantValue(Context& context, SemIR::LocId loc_id)
-> void {
CARBON_DIAGNOSTIC(EvalRequiresConstantValue, Error,
"expression is runtime; expected constant");
context.emitter().Emit(loc_id, EvalRequiresConstantValue);
}
// Gets a constant value for an `inst_id`, diagnosing when the input is not a
// constant value.
static auto RequireConstantValue(EvalContext& eval_context,
SemIR::InstId inst_id, Phase* phase)
-> SemIR::InstId {
if (!inst_id.has_value()) {
return SemIR::InstId::None;
}
if (inst_id == SemIR::ErrorInst::InstId) {
*phase = Phase::UnknownDueToError;
return SemIR::ErrorInst::InstId;
}
auto const_id = eval_context.GetConstantValue(inst_id);
*phase =
LatestPhase(*phase, GetPhase(eval_context.constant_values(), const_id));
if (const_id.is_constant()) {
return eval_context.constant_values().GetInstId(const_id);
}
DiagnoseNonConstantValue(eval_context.context(),
eval_context.GetDiagnosticLoc({inst_id}));
*phase = Phase::UnknownDueToError;
return SemIR::ErrorInst::InstId;
}
// Gets a constant value for an `inst_id`, diagnosing when the input is not
// constant, and CHECKing that it is concrete. Should only be used in contexts
// where non-concrete constants cannot appear.
static auto CheckConcreteValue(EvalContext& eval_context, SemIR::InstId inst_id)
-> SemIR::InstId {
auto phase = Phase::Concrete;
auto value_inst_id = RequireConstantValue(eval_context, inst_id, &phase);
if (phase == Phase::UnknownDueToError) {
return SemIR::ErrorInst::InstId;
}
CARBON_CHECK(phase == Phase::Concrete,
"expression evaluates to symbolic value {0}",
eval_context.insts().Get(value_inst_id));
return value_inst_id;
}
// Find the instruction that the given instruction instantiates to, and return
// that.
static auto GetConstantValue(EvalContext& eval_context,
SemIR::MetaInstId inst_id, Phase* phase)
-> SemIR::MetaInstId {
Phase inner_phase = Phase::Concrete;
if (auto const_inst_id =
GetConstantValue(eval_context, SemIR::InstId(inst_id), &inner_phase);
const_inst_id.has_value()) {
// The instruction has a constant value. Use that as the operand of the
// action.
*phase = LatestPhase(*phase, inner_phase);
return const_inst_id;
}
// If this instruction is splicing in an action result, that action result is
// our operand.
if (auto splice = eval_context.insts().TryGetAs<SemIR::SpliceInst>(inst_id)) {
if (auto spliced_inst_id =
GetConstantValue(eval_context, splice->inst_id, phase);
spliced_inst_id.has_value()) {
if (auto inst_value_id = eval_context.insts().TryGetAs<SemIR::InstValue>(
spliced_inst_id)) {
return inst_value_id->inst_id;
}
}
}
// Otherwise, this is a normal instruction.
if (OperandDependence(eval_context.context(), inst_id) ==
SemIR::ConstantDependence::Template) {
*phase = LatestPhase(*phase, Phase::TemplateSymbolic);
}
return inst_id;
}
static auto GetConstantValue(EvalContext& eval_context,
SemIR::MetaInstBlockId inst_block_id, Phase* phase)
-> SemIR::MetaInstBlockId {
auto inst_ids = eval_context.inst_blocks().Get(inst_block_id);
llvm::SmallVector<SemIR::InstId> new_inst_ids;
for (auto inst_id : inst_ids) {
new_inst_ids.push_back(
GetConstantValue(eval_context, SemIR::MetaInstId{inst_id}, phase));
}
return eval_context.inst_blocks().Add(new_inst_ids);
}
static auto GetConstantValue(EvalContext& eval_context,
SemIR::TypeInstId inst_id, Phase* phase)
-> SemIR::TypeInstId {
// The input instruction is a TypeInstId, and eval does not change concrete
// types (like TypeType which TypeInstId implies), so the result is also a
// valid TypeInstId.
return SemIR::TypeInstId::UnsafeMake(GetConstantValue(
eval_context, static_cast<SemIR::InstId>(inst_id), phase));
}
// Explicitly discard a `DestInstId`, because we should not be using the
// destination as part of evaluation.
static auto GetConstantValue(EvalContext& /*eval_context*/,
SemIR::DestInstId /*inst_id*/, Phase* /*phase*/)
-> SemIR::DestInstId {
return SemIR::InstId::None;
}
// Given an instruction whose type may refer to a generic parameter, returns the
// corresponding type in the evaluation context.
//
// If the `InstId` is not provided, the instruction is assumed to be new and
// therefore unattached, and the type of the given instruction is returned
// unchanged, but the phase is still updated.
static auto GetTypeOfInst(EvalContext& eval_context, SemIR::InstId inst_id,
SemIR::Inst inst, Phase* phase) -> SemIR::TypeId {
auto type_id = inst_id.has_value() ? eval_context.GetTypeOfInst(inst_id)
: inst.type_id();
*phase = LatestPhase(*phase,
GetPhase(eval_context.constant_values(),
eval_context.types().GetConstantId(type_id)));
return type_id;
}
// AbsoluteInstBlockId can not have its values substituted, so this overload is
// deleted. This prevents conversion to InstBlockId.
static auto GetConstantValue(EvalContext& eval_context,
SemIR::AbsoluteInstBlockId inst_block_id,
Phase* phase) -> SemIR::InstBlockId = delete;
// If the given instruction block contains only constants, returns a
// corresponding block of those values. Ignores the instructions in the
// specified range of indexes, replacing those elements with `None`.
static auto GetConstantBlockValueIgnoringIndexRange(
EvalContext& eval_context, SemIR::InstBlockId inst_block_id, Phase* phase,
std::pair<int, int> ignored_range) -> SemIR::InstBlockId {
if (!inst_block_id.has_value()) {
return SemIR::InstBlockId::None;
}
auto insts = eval_context.inst_blocks().Get(inst_block_id);
llvm::SmallVector<SemIR::InstId> const_insts;
for (auto inst_id : insts) {
auto const_inst_id = SemIR::InstId::None;
if (static_cast<int>(const_insts.size()) < ignored_range.first ||
static_cast<int>(const_insts.size()) >= ignored_range.second) {
const_inst_id = GetConstantValue(eval_context, inst_id, phase);
if (!const_inst_id.has_value()) {
return SemIR::InstBlockId::None;
}
}
// Once we leave the small buffer, we know the first few elements are all
// constant, so it's likely that the entire block is constant. Resize to
// the target size given that we're going to allocate memory now anyway.
if (const_insts.size() == const_insts.capacity()) {
const_insts.reserve(insts.size());
}
const_insts.push_back(const_inst_id);
}
// TODO: If the new block is identical to the original block, and we know the
// old ID was canonical, return the original ID.
return eval_context.inst_blocks().AddCanonical(const_insts);
}
// If the given instruction block contains only constants, returns a
// corresponding block of those values.
static auto GetConstantValue(EvalContext& eval_context,
SemIR::InstBlockId inst_block_id, Phase* phase)
-> SemIR::InstBlockId {
return GetConstantBlockValueIgnoringIndexRange(eval_context, inst_block_id,
phase, {0, 0});
}
// Compute the constant value of a type block. This may be different from the
// input type block if we have known generic arguments.
static auto GetConstantValue(EvalContext& eval_context,
SemIR::StructTypeFieldsId fields_id, Phase* phase)
-> SemIR::StructTypeFieldsId {
if (!fields_id.has_value()) {
return SemIR::StructTypeFieldsId::None;
}
auto fields = eval_context.context().struct_type_fields().Get(fields_id);
llvm::SmallVector<SemIR::StructTypeField> new_fields;
for (auto field : fields) {
auto new_type_inst_id =
GetConstantValue(eval_context, field.type_inst_id, phase);
if (!new_type_inst_id.has_value()) {
return SemIR::StructTypeFieldsId::None;
}
// Once we leave the small buffer, we know the first few elements are all
// constant, so it's likely that the entire block is constant. Resize to the
// target size given that we're going to allocate memory now anyway.
if (new_fields.size() == new_fields.capacity()) {
new_fields.reserve(fields.size());
}
new_fields.push_back(
{.name_id = field.name_id, .type_inst_id = new_type_inst_id});
}
// TODO: If the new block is identical to the original block, and we know the
// old ID was canonical, return the original ID.
return eval_context.context().struct_type_fields().AddCanonical(new_fields);
}
// The constant value of a specific is the specific with the corresponding
// constant values for its arguments.
static auto GetConstantValue(EvalContext& eval_context,
SemIR::SpecificId specific_id, Phase* phase)
-> SemIR::SpecificId {
if (!specific_id.has_value()) {
return SemIR::SpecificId::None;
}
const auto& specific = eval_context.specifics().Get(specific_id);
auto args_id = GetConstantValue(eval_context, specific.args_id, phase);
if (!args_id.has_value()) {
return SemIR::SpecificId::None;
}
// Generally, when making a new specific, it's done through MakeSpecific(),
// which will ensure the declaration is resolved.
//
// However, the SpecificId returned here is intentionally left without its
// declaration resolved. Imported instructions with SpecificIds should not
// have the specific's declaration resolved, but other instructions which
// include a new SpecificId should.
//
// The resolving of the specific's declaration will be ensured later when
// evaluating the instruction containing the SpecificId.
if (args_id == specific.args_id) {
return specific_id;
}
return eval_context.context().specifics().GetOrAdd(specific.generic_id,
args_id);
}
static auto GetConstantValue(EvalContext& eval_context,
SemIR::SpecificInterfaceId specific_interface_id,
Phase* phase) -> SemIR::SpecificInterfaceId {
const auto& interface =
eval_context.specific_interfaces().Get(specific_interface_id);
if (!interface.specific_id.has_value()) {
return specific_interface_id;
}
return eval_context.specific_interfaces().Add(
{.interface_id = interface.interface_id,
.specific_id =
GetConstantValue(eval_context, interface.specific_id, phase)});
}
// Like `GetConstantValue` but for a `DeclaredFacetType`.
static auto GetConstantDeclaredFacetType(EvalContext& eval_context,
SemIR::LocId loc_id,
const SemIR::DeclaredFacetType& orig,
Phase* phase)
-> SemIR::DeclaredFacetType {
SemIR::DeclaredFacetType declared_facet_type = {};
// Phase of constraints whose `.Self` refers to the type constrained by this
// facet type.
Phase self_phase = Phase::Concrete;
declared_facet_type.extend_constraints.reserve(
orig.extend_constraints.size());
for (const auto& extend : orig.extend_constraints) {
// TODO: Add GetConstantValue for SpecificInterface.
declared_facet_type.extend_constraints.push_back(
{.interface_id = extend.interface_id,
.specific_id =
GetConstantValue(eval_context, extend.specific_id, phase)});
}
declared_facet_type.self_impls_constraints.reserve(
orig.self_impls_constraints.size());
for (const auto& self_impls : orig.self_impls_constraints) {
// TODO: Add GetConstantValue for SpecificInterface.
declared_facet_type.self_impls_constraints.push_back(
{.interface_id = self_impls.interface_id,
.specific_id = GetConstantValue(eval_context, self_impls.specific_id,
&self_phase)});
}
declared_facet_type.extend_named_constraints.reserve(
orig.extend_named_constraints.size());
for (const auto& extend : orig.extend_named_constraints) {
// TODO: Add GetConstantValue for SpecificNamedConstraint.
declared_facet_type.extend_named_constraints.push_back(
{.named_constraint_id = extend.named_constraint_id,
.specific_id =
GetConstantValue(eval_context, extend.specific_id, phase)});
}
declared_facet_type.self_impls_named_constraints.reserve(
orig.self_impls_named_constraints.size());
for (const auto& self_impls : orig.self_impls_named_constraints) {
// TODO: Add GetConstantValue for SpecificNamedConstraint.
declared_facet_type.self_impls_named_constraints.push_back(
{.named_constraint_id = self_impls.named_constraint_id,
.specific_id = GetConstantValue(eval_context, self_impls.specific_id,
&self_phase)});
}
declared_facet_type.type_impls_interfaces.reserve(
orig.type_impls_interfaces.size());
for (const auto& type_impls : orig.type_impls_interfaces) {
declared_facet_type.type_impls_interfaces.push_back(
{.self_type =
GetConstantValue(eval_context, type_impls.self_type, &self_phase),
// TODO: Add GetConstantValue for SpecificInterface.
.specific_interface = {
.interface_id = type_impls.specific_interface.interface_id,
.specific_id = GetConstantValue(
eval_context, type_impls.specific_interface.specific_id,
&self_phase)}});
}
declared_facet_type.type_impls_named_constraints.reserve(
orig.type_impls_named_constraints.size());
for (const auto& type_impls : orig.type_impls_named_constraints) {
declared_facet_type.type_impls_named_constraints.push_back(
{.self_type =
GetConstantValue(eval_context, type_impls.self_type, &self_phase),
// TODO: Add GetConstantValue for SpecificNamedConstraint.
.specific_named_constraint = {
.named_constraint_id =
type_impls.specific_named_constraint.named_constraint_id,
.specific_id = GetConstantValue(
eval_context, type_impls.specific_named_constraint.specific_id,
&self_phase)}});
}
// Rewrite constraints are resolved first before replacing them with their
// canonical instruction, so that in a `WhereExpr` we can work with the
// `ImplWitnessAccess` references to `.Self` on the LHS of the constraints
// rather than the value of the associated constant they reference.
//
// This also implies that we may find `ImplWitnessAccessSubstituted`
// instructions in the LHS and RHS of these constraints, which are preserved
// to maintain them as an unresolved reference to an associated constant, but
// which must be handled gracefully during resolution. They will be replaced
// with the constant value of the `ImplWitnessAccess` below when they are
// substituted with a constant value.
declared_facet_type.rewrite_constraints = orig.rewrite_constraints;
if (!ResolveFacetTypeRewriteConstraints(
eval_context.context(), loc_id,
declared_facet_type.rewrite_constraints)) {
*phase = Phase::UnknownDueToError;
}
for (auto& rewrite : declared_facet_type.rewrite_constraints) {
auto lhs_id =
RequireConstantValue(eval_context, rewrite.lhs_id, &self_phase);
auto rhs_id =
RequireConstantValue(eval_context, rewrite.rhs_id, &self_phase);
rewrite = {.lhs_id = lhs_id, .rhs_id = rhs_id};
}
// Update phase, ignoring `.Self` dependence from constraints whose `.Self` is
// scoped to this facet type.
if (self_phase > Phase::PeriodSelfSymbolic) {
*phase = LatestPhase(*phase, self_phase);
}
// TODO: Process other requirements.
declared_facet_type.other_requirements = orig.other_requirements;
declared_facet_type.Canonicalize();
return declared_facet_type;
}
static auto GetConstantValue(EvalContext& eval_context,
SemIR::DeclaredFacetTypeId declared_facet_type_id,
Phase* phase) -> SemIR::DeclaredFacetTypeId {
SemIR::DeclaredFacetType declared_facet_type = GetConstantDeclaredFacetType(
eval_context, SemIR::LocId::None,
eval_context.declared_facet_types().Get(declared_facet_type_id), phase);
return eval_context.declared_facet_types().Add(declared_facet_type);
}
static auto GetConstantValue(EvalContext& eval_context,
SemIR::EntityNameId entity_name_id, Phase* phase)
-> SemIR::EntityNameId {
const auto& bind_name = eval_context.entity_names().Get(entity_name_id);
Phase name_phase;
if (bind_name.name_id == SemIR::NameId::PeriodSelf) {
name_phase = Phase::PeriodSelfSymbolic;
} else if (!bind_name.bind_index().has_value()) {
name_phase = Phase::Concrete;
} else if (bind_name.is_template) {
name_phase = Phase::TemplateSymbolic;
} else {
name_phase = Phase::CheckedSymbolic;
}
*phase = LatestPhase(*phase, name_phase);
return eval_context.entity_names().MakeCanonical(entity_name_id);
}
// Returns the constant value of `id` if it has a `GetConstantValue` overload,
// and otherwise returns `id` itself.
template <typename IdT>
static auto GetConstantValueOrPassThrough(EvalContext& eval_context, IdT id,
Phase* phase) -> IdT;
template <typename BundleT>
static auto GetConstantValue(EvalContext& eval_context,
SemIR::BundleId<BundleT> bundle_id, Phase* phase)
-> SemIR::BundleId<BundleT> {
return eval_context.context().bundles().AddCanonical(std::apply(
[&]<typename... Ids>(Ids... ids) -> BundleT {
return {GetConstantValueOrPassThrough(eval_context, ids, phase)...};
},
eval_context.context().bundles().GetAsTuple(bundle_id)));
}
// Replaces the specified field of the given typed instruction with its constant
// value, if it has constant phase. Returns true on success, false if the value
// has runtime phase.
template <typename InstT, typename FieldIdT>
static auto ReplaceFieldWithConstantValue(EvalContext& eval_context,
InstT* inst, FieldIdT InstT::* field,
Phase* phase) -> bool {
auto unwrapped = GetConstantValue(eval_context, inst->*field, phase);
if (!unwrapped.has_value() && (inst->*field).has_value()) {
return false;
}
inst->*field = unwrapped;
return IsConstantOrError(*phase);
}
// Function template that can be called with an argument of type `T`. Used below
// to detect which overloads of `GetConstantValue` exist.
template <typename T>
static auto Accept(T /*arg*/) -> void {}
// Determines whether a `GetConstantValue` overload exists for a given ID type.
// Note that we do not check whether `GetConstantValue` is *callable* with a
// given ID type, because that would use the `InstId` overload for
// `AbsoluteInstId` and similar wrapper types, which should be left alone.
template <typename IdT>
static constexpr bool HasGetConstantValueOverload = requires {
Accept<auto (*)(EvalContext&, IdT, Phase*)->IdT>(GetConstantValue);
};
template <typename IdT>
static auto GetConstantValueOrPassThrough(EvalContext& eval_context, IdT id,
Phase* phase) -> IdT {
if constexpr (HasGetConstantValueOverload<IdT>) {
return GetConstantValue(eval_context, id, phase);
} else {
return id;
}
}
// Given the stored value `arg` of an instruction field and its corresponding
// kind `kind`, returns the constant value to use for that field, if it has a
// constant phase. `*phase` is updated to include the new constant value. If
// the resulting phase is not constant, the returned value is not useful and
// will typically be `NoneIndex`.
static auto GetConstantValueForArg(EvalContext& eval_context,
SemIR::IdAndKind arg_and_kind, Phase* phase)
-> int32_t {
return arg_and_kind.Dispatch<int32_t>([&]<typename IdT>(IdT id) -> int32_t {
return SemIR::ToRaw(GetConstantValueOrPassThrough(eval_context, id, phase));
});
}
// Given an instruction, replaces its operands with their constant values from
// the specified evaluation context. `*phase` is updated to describe the
// constant phase of the result. Returns whether `*phase` is a constant phase;
// if not, `inst` may not be fully updated and should not be used.
static auto ReplaceAllFieldsWithConstantValues(EvalContext& eval_context,
SemIR::Inst* inst, Phase* phase)
-> bool {
auto arg0 =
GetConstantValueForArg(eval_context, inst->arg0_and_kind(), phase);
if (!IsConstantOrError(*phase)) {
return false;
}
auto arg1 =
GetConstantValueForArg(eval_context, inst->arg1_and_kind(), phase);
if (!IsConstantOrError(*phase)) {
return false;
}
inst->SetArgs(arg0, arg1);
return true;
}
// Given an instruction and its ID, replaces its type with the corresponding
// value in this evaluation context. Updates `*phase` to describe the phase of
// the result, and returns whether `*phase` is a constant phase.
static auto ReplaceTypeWithConstantValue(EvalContext& eval_context,
SemIR::InstId inst_id,
SemIR::Inst* inst, Phase* phase)
-> bool {
inst->SetType(GetTypeOfInst(eval_context, inst_id, *inst, phase));
return IsConstantOrError(*phase);
}
template <typename InstT>
static auto ReplaceTypeWithConstantValue(EvalContext& eval_context,
SemIR::InstId inst_id, InstT* inst,
Phase* phase) -> bool {
inst->type_id = GetTypeOfInst(eval_context, inst_id, *inst, phase);
return IsConstantOrError(*phase);
}
static auto ResolveSpecificDeclForSpecificId(EvalContext& eval_context,
SemIR::SpecificId specific_id)
-> void {
if (!specific_id.has_value()) {
return;
}
const auto& specific = eval_context.specifics().Get(specific_id);
const auto& generic = eval_context.generics().Get(specific.generic_id);
if (specific_id == generic.self_specific_id) {
// Impl witness table construction happens before its generic decl is
// finish, in order to make the table's instructions dependent
// instructions of the Impl's generic. But those instructions can refer to
// the generic's self specific. We can not resolve the specific
// declaration for the self specific until the generic is finished, but it
// is explicitly resolved at that time in `FinishGenericDecl()`.
return;
}
ResolveSpecificDecl(eval_context.context(), eval_context.fallback_loc_id(),
specific_id);
}
static auto ResolveSpecificDeclForArg(
EvalContext& eval_context,
SemIR::DeclaredFacetTypeId declared_facet_type_id) -> void {
const auto& declared_facet_type =
eval_context.context().declared_facet_types().Get(declared_facet_type_id);
for (const auto& interface : declared_facet_type.extend_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, interface.specific_id);
}
for (const auto& interface : declared_facet_type.self_impls_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, interface.specific_id);
}
for (const auto& constraint : declared_facet_type.extend_named_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, constraint.specific_id);
}
for (const auto& constraint :
declared_facet_type.self_impls_named_constraints) {
ResolveSpecificDeclForSpecificId(eval_context, constraint.specific_id);
}
for (const auto& type_impls : declared_facet_type.type_impls_interfaces) {
ResolveSpecificDeclForSpecificId(eval_context,
type_impls.specific_interface.specific_id);
}
for (const auto& type_impls :
declared_facet_type.type_impls_named_constraints) {
ResolveSpecificDeclForSpecificId(
eval_context, type_impls.specific_named_constraint.specific_id);
}
}
static auto ResolveSpecificDeclForArg(EvalContext& eval_context,
SemIR::SpecificId specific_id) -> void {
ResolveSpecificDeclForSpecificId(eval_context, specific_id);
}
static auto ResolveSpecificDeclForArg(
EvalContext& eval_context, SemIR::SpecificInterfaceId specific_interface_id)
-> void {
ResolveSpecificDeclForSpecificId(eval_context,
eval_context.specific_interfaces()
.Get(specific_interface_id)
.specific_id);
}
template <typename IdT>
requires SemIR::Internal::IsIdKindType<IdT> &&
SameAsOneOf<IdT, SemIR::IdAndKind::NoneType, SemIR::DestInstId,
SemIR::EntityNameId, SemIR::InstBlockId, SemIR::InstId,
SemIR::MetaInstId, SemIR::MetaInstBlockId,
SemIR::StructTypeFieldsId, SemIR::TypeInstId>
static auto ResolveSpecificDeclForArg(EvalContext& /*eval_context*/, IdT /*id*/)
-> void {
// These id types have a GetConstantValue() overload but that overload
// does not canonicalize any SpecificId in the value type.
}
template <typename IdT>
requires SemIR::Internal::IsIdKindType<IdT>
static auto ResolveSpecificDeclForArg(EvalContext& /*eval_context*/, IdT /*id*/)
-> void {
if constexpr (HasGetConstantValueOverload<IdT>) {
CARBON_FATAL("Missing case for {0} which has a GetConstantValue() overload",
IdT::Label);
}
}
template <typename BundleT>
static auto ResolveSpecificDeclForArg(EvalContext& eval_context,
SemIR::BundleId<BundleT> bundle_id)
-> void {
std::apply(
[&](auto... ids) -> void {
(..., ResolveSpecificDeclForArg(eval_context, ids));
},
eval_context.context().bundles().GetAsTuple(bundle_id));
}
// Resolves the specific declarations for a specific id in any field of the
// `inst` instruction.
static auto ResolveSpecificDeclForInst(EvalContext& eval_context,
const SemIR::Inst& inst) -> void {
for (auto arg_and_kind : {inst.arg0_and_kind(), inst.arg1_and_kind()}) {
arg_and_kind.Dispatch<void>(
[&](auto id) { ResolveSpecificDeclForArg(eval_context, id); });
}
}
auto AddImportedConstant(Context& context, SemIR::Inst inst)
-> SemIR::ConstantId {
EvalContext eval_context(&context, SemIR::LocId::None);
CARBON_CHECK(inst.kind().has_type(), "Can't import untyped instructions: {0}",
inst.kind());
Phase phase = GetPhase(context.constant_values(),
context.types().GetConstantId(inst.type_id()));
// We ignore the return value of ReplaceAllFieldsWithConstantValues and just
// propagate runtime and error constant values into the resulting ConstantId.
ReplaceAllFieldsWithConstantValues(eval_context, &inst, &phase);
return MakeConstantResult(context, inst, phase);
}
// Performs an index into a homogeneous aggregate, retrieving the specified
// element.
static auto PerformArrayIndex(EvalContext& eval_context, SemIR::ArrayIndex inst)
-> SemIR::ConstantId {
Phase phase = Phase::Concrete;
auto index_id = GetConstantValue(eval_context, inst.index_id, &phase);
if (!index_id.has_value()) {
return MakeNonConstantResult(phase);
}
auto index = eval_context.insts().TryGetAs<SemIR::IntValue>(index_id);
if (!index) {
CARBON_CHECK(phase != Phase::Concrete,
"Concrete constant integer should be a literal");
return MakeNonConstantResult(phase);
}
// Array indexing is invalid if the index is constant and out of range,
// regardless of whether the array itself is constant.
const auto& index_val = eval_context.ints().Get(index->int_id);
auto aggregate_type_id = eval_context.GetTypeOfInst(inst.array_id);
if (auto array_type =
eval_context.types().TryGetAs<SemIR::ArrayType>(aggregate_type_id)) {
if (auto bound = eval_context.insts().TryGetAs<SemIR::IntValue>(
array_type->bound_id)) {
// This awkward call to `getZExtValue` is a workaround for APInt not
// supporting comparisons between integers of different bit widths.
if (index_val.getActiveBits() > 64 ||
eval_context.ints()
.Get(bound->int_id)
.ule(index_val.getZExtValue())) {
CARBON_DIAGNOSTIC(ArrayIndexOutOfBounds, Error,
"array index `{0}` is past the end of type {1}",
TypedInt, SemIR::TypeId);
eval_context.emitter().Emit(
eval_context.GetDiagnosticLoc(inst.index_id), ArrayIndexOutOfBounds,
{.type = index->type_id, .value = index_val}, aggregate_type_id);
return SemIR::ErrorInst::ConstantId;
}
}
}
auto aggregate_id = GetConstantValue(eval_context, inst.array_id, &phase);
if (!aggregate_id.has_value()) {
return MakeNonConstantResult(phase);
}
auto aggregate =
eval_context.insts().TryGetAs<SemIR::AnyAggregateValue>(aggregate_id);
if (!aggregate) {
// TODO: Consider forming a symbolic constant or reference constant array
// index in this case.
return MakeNonConstantResult(phase);
}
auto elements = eval_context.inst_blocks().Get(aggregate->elements_id);
return eval_context.GetConstantValue(elements[index_val.getZExtValue()]);
}
// Diagnoses that a character value is too large for the destination type.
static auto DiagnoseCharTooLargeForType(Context& context, SemIR::LocId loc_id,
SemIR::CharId char_id,
SemIR::TypeId dest_type_id) -> void {
CARBON_DIAGNOSTIC(CharTooLargeForType, Error,
"character value {0} too large for type {1}", SemIR::CharId,
SemIR::TypeId);
context.emitter().Emit(loc_id, CharTooLargeForType, char_id, dest_type_id);
}
// Performs a conversion between character types, diagnosing if the value
// doesn't fit in the destination type.
static auto PerformCharLiteralConvertChar(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
auto arg = context.insts().GetAs<SemIR::CharLiteralValue>(arg_id);
// Values over 0x80 require multiple code units in UTF-8.
if (arg.value.index >= 0x80) {
DiagnoseCharTooLargeForType(context, loc_id, arg.value, dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
llvm::APInt int_val(8, arg.value.index, /*isSigned=*/false);
return MakeIntResult(context, dest_type_id, /*is_signed=*/false,
std::move(int_val));
}
// Converts a CharLiteral to an integer type.
static auto PerformCharLiteralConvertInt(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
auto arg = context.insts().GetAs<SemIR::CharLiteralValue>(arg_id);
auto [is_signed, bit_width_id] =
context.sem_ir().types().GetIntTypeInfo(dest_type_id);
CARBON_CHECK(bit_width_id.has_value());
unsigned int width = context.ints().Get(bit_width_id).getZExtValue();
int32_t code_point = arg.value.index;
// Determine the bit width of the code point value. Log2_32(0) is -1, so this
// correctly computes a width of 0 for U+0000.
unsigned code_point_bit_width = llvm::Log2_32(code_point) + 1;
if (code_point_bit_width > width - is_signed) {
DiagnoseCharTooLargeForType(context, loc_id, arg.value, dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
llvm::APInt int_val(width, code_point, /*isSigned=*/false);
return MakeIntResult(context, dest_type_id, is_signed, std::move(int_val));
}
// Converts an integer to a CharLiteral, checking if the value is a valid
// Unicode code point.
static auto PerformIntConvertCharLiteral(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
auto arg = context.insts().GetAs<SemIR::IntValue>(arg_id);
llvm::APInt arg_val = context.ints().Get(arg.int_id);
auto char_id = SemIR::CharId::ForCodePoint(arg_val);
if (!char_id) {
CARBON_DIAGNOSTIC(InvalidCharLiteralValue, Error,
"integer value {0} is not a valid Unicode code point",
TypedInt);
context.emitter().Emit(loc_id, InvalidCharLiteralValue,
{.type = arg.type_id, .value = arg_val});
return SemIR::ErrorInst::ConstantId;
}
return MakeConstantResult(
context,
SemIR::CharLiteralValue{.type_id = dest_type_id, .value = *char_id},
Phase::Concrete);
}
// Performs a comparison between two CharLiteral values.
static auto PerformCharLiteralComparison(
Context& context, SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id, SemIR::InstId rhs_id, SemIR::TypeId bool_type_id)
-> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::CharLiteralValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::CharLiteralValue>(rhs_id);
bool result;
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::CharLiteralEq:
result = (lhs.value.index == rhs.value.index);
break;
case SemIR::BuiltinFunctionKind::CharLiteralNeq:
result = (lhs.value.index != rhs.value.index);
break;
case SemIR::BuiltinFunctionKind::CharLiteralLess:
result = (lhs.value.index < rhs.value.index);
break;
case SemIR::BuiltinFunctionKind::CharLiteralLessEq:
result = (lhs.value.index <= rhs.value.index);
break;
case SemIR::BuiltinFunctionKind::CharLiteralGreater:
result = (lhs.value.index > rhs.value.index);
break;
case SemIR::BuiltinFunctionKind::CharLiteralGreaterEq:
result = (lhs.value.index >= rhs.value.index);
break;
default:
CARBON_FATAL("Unexpected operation kind.");
}
return MakeBoolResult(context, bool_type_id, result);
}
// Performs subtraction/addition of CharLiteral and Int.
static auto PerformCharLiteralArithmetic(
Context& context, SemIR::LocId loc_id,
SemIR::BuiltinFunctionKind builtin_kind, SemIR::InstId lhs_id,
SemIR::InstId rhs_id, SemIR::TypeId dest_type_id) -> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::CharLiteralValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
const llvm::APInt& rhs_val = context.ints().Get(rhs.int_id);
// The code point value fits in 21 bits, so 32 bits is plenty for the
// CharLiteral. Active bits + 2 gives us room for a sign bit and a carry,
// while avoiding an APInt heap allocation except on the error path.
auto val = rhs_val.sextOrTrunc(std::max(32U, rhs_val.getActiveBits() + 2));
if (builtin_kind == SemIR::BuiltinFunctionKind::CharLiteralSubInt) {
val.negate();
}
val += lhs.value.index;
auto char_id = SemIR::CharId::ForCodePoint(val);
if (!char_id) {
CARBON_DIAGNOSTIC(InvalidCharLiteralArithmeticResult, Error,
"character arithmetic result {0} is not a valid "
"Unicode code point",
llvm::APSInt);
context.emitter().Emit(loc_id, InvalidCharLiteralArithmeticResult,
llvm::APSInt(val, /*isUnsigned=*/false));
return SemIR::ErrorInst::ConstantId;
}
return MakeConstantResult(
context,
SemIR::CharLiteralValue{.type_id = dest_type_id, .value = *char_id},
Phase::Concrete);
}
// Performs subtraction of two CharLiteral values.
static auto PerformCharLiteralSubChar(Context& context, SemIR::InstId lhs_id,
SemIR::InstId rhs_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::CharLiteralValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::CharLiteralValue>(rhs_id);
int32_t result = lhs.value.index - rhs.value.index;
return MakeIntResult(context, dest_type_id, /*is_signed=*/true,
llvm::APInt(32, result, /*isSigned=*/true));
}
// Forms a constant int type as an evaluation result. Requires that width_id is
// constant.
static auto MakeIntTypeResult(Context& context, SemIR::LocId loc_id,
SemIR::IntKind int_kind, SemIR::InstId width_id,
Phase phase) -> SemIR::ConstantId {
auto result = SemIR::IntType{.type_id = SemIR::TypeType::TypeId,
.int_kind = int_kind,
.bit_width_id = width_id};
if (!ValidateIntType(context, loc_id, result)) {
return SemIR::ErrorInst::ConstantId;
}
return MakeConstantResult(context, result, phase);
}
// Forms a constant float type as an evaluation result. Requires that width_id
// is constant.
static auto MakeFloatTypeResult(Context& context, SemIR::LocId loc_id,
SemIR::InstId width_id, Phase phase)
-> SemIR::ConstantId {
auto result = SemIR::FloatType{.type_id = SemIR::TypeType::TypeId,
.bit_width_id = width_id,
.float_kind = SemIR::FloatKind::None};
if (!ValidateFloatTypeAndSetKind(context, loc_id, result)) {
return SemIR::ErrorInst::ConstantId;
}
return MakeConstantResult(context, result, phase);
}
// Get an integer at a suitable bit-width: either `bit_width_id` if it has a
// value, or the canonical width from the value store if not.
static auto GetIntAtSuitableWidth(Context& context, IntId int_id,
IntId bit_width_id) -> llvm::APInt {
return bit_width_id.has_value()
? context.ints().GetAtWidth(int_id, bit_width_id)
: context.ints().Get(int_id);
}
// Performs a conversion between integer types, truncating if the value doesn't
// fit in the destination type.
static auto PerformIntConvert(Context& context, SemIR::InstId arg_id,
SemIR::TypeId dest_type_id) -> SemIR::ConstantId {
auto arg_val =
context.ints().Get(context.insts().GetAs<SemIR::IntValue>(arg_id).int_id);
auto dest_int_info = context.sem_ir().types().TryGetIntTypeInfo(dest_type_id);
if (!dest_int_info) {
// The destination is not a valid integer type, such as when its bit width
// was diagnosed as invalid. The error was already diagnosed when forming
// the type, so just produce an error value.
return SemIR::ErrorInst::ConstantId;
}
auto [dest_is_signed, bit_width_id] = *dest_int_info;
if (bit_width_id.has_value()) {
// TODO: If the value fits in the destination type, reuse the existing
// int_id rather than recomputing it. This is probably the most common case.
bool src_is_signed = context.sem_ir().types().IsSignedInt(
context.insts().Get(arg_id).type_id());
unsigned width = context.ints().Get(bit_width_id).getZExtValue();
arg_val =
src_is_signed ? arg_val.sextOrTrunc(width) : arg_val.zextOrTrunc(width);
}
return MakeIntResult(context, dest_type_id, dest_is_signed,
std::move(arg_val));
}
// Performs a conversion between integer types, diagnosing if the value doesn't
// fit in the destination type.
static auto PerformCheckedIntConvert(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
auto arg = context.insts().GetAs<SemIR::IntValue>(arg_id);
auto arg_val = context.ints().Get(arg.int_id);
auto dest_int_info = context.sem_ir().types().TryGetIntTypeInfo(dest_type_id);
if (!dest_int_info) {
// The destination is not a valid integer type; see PerformIntConvert.
return SemIR::ErrorInst::ConstantId;
}
auto [is_signed, bit_width_id] = *dest_int_info;
auto width = bit_width_id.has_value()
? context.ints().Get(bit_width_id).getZExtValue()
: arg_val.getBitWidth();
if (!is_signed && arg_val.isNegative()) {
CARBON_DIAGNOSTIC(
NegativeIntInUnsignedType, Error,
"negative integer value {0} converted to unsigned type {1}", TypedInt,
SemIR::TypeId);
context.emitter().Emit(loc_id, NegativeIntInUnsignedType,
{.type = arg.type_id, .value = arg_val},
dest_type_id);
}
unsigned arg_non_sign_bits = arg_val.getSignificantBits() - 1;
if (arg_non_sign_bits + is_signed > width) {
CARBON_DIAGNOSTIC(IntTooLargeForType, Error,
"integer value {0} too large for type {1}", TypedInt,
SemIR::TypeId);
context.emitter().Emit(loc_id, IntTooLargeForType,
{.type = arg.type_id, .value = arg_val},
dest_type_id);
}
return MakeIntResult(context, dest_type_id, arg.int_id);
}
// Convert a real value to an APFloat.
static auto RealToAPFloat(Context& context, RealId real_id,
const llvm::fltSemantics& semantics,
llvm::APFloat::opStatus* status = nullptr)
-> llvm::APFloat {
auto real_value = context.sem_ir().reals().Get(real_id);
// Convert the real value to a string.
llvm::SmallString<64> str;
real_value.mantissa.toString(str, real_value.is_decimal ? 10 : 16,
/*signed=*/true, /*formatAsCLiteral=*/true);
str += real_value.is_decimal ? "e" : "p";
real_value.exponent.toStringSigned(str);
// Convert the string to an APFloat.
// TODO: The implementation of this conversion effectively converts back to
// APInts, but unfortunately the conversion from integer mantissa and
// exponent in IEEEFloat::roundSignificandWithExponent is not part of the
// public API.
llvm::APFloat result(semantics);
auto res_status =
result.convertFromString(str, llvm::APFloat::rmNearestTiesToEven);
if (auto error = res_status.takeError()) {
// The literal we create should always successfully parse.
CARBON_FATAL("Float literal parsing failed: {0}",
toString(std::move(error)));
}
if (status) {
*status = res_status.get();
}
return result;
}
// Performs a conversion between floating-point types, diagnosing if the value
// doesn't fit in the destination type when check_overflow is true.
static auto PerformFloatConvert(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id, bool check_overflow)
-> SemIR::ConstantId {
auto dest_type_object_rep_id = context.types().GetObjectRepr(dest_type_id);
CARBON_CHECK(dest_type_object_rep_id.has_value(),
"Conversion to incomplete type");
auto dest_float_type =
context.types().TryGetAs<SemIR::FloatType>(dest_type_object_rep_id);
CARBON_CHECK(dest_float_type || context.types().Is<SemIR::FloatLiteralType>(
dest_type_object_rep_id));
if (auto literal =
context.insts().TryGetAs<SemIR::FloatLiteralValue>(arg_id)) {
if (!dest_float_type) {
return MakeConstantResult(
context,
SemIR::FloatLiteralValue{.type_id = dest_type_id,
.real_id = literal->real_id},
Phase::Concrete);
}
// Convert the real literal to an llvm::APFloat and add it to the floats
// ValueStore. In the future this would use an arbitrary precision Rational
// type.
//
// TODO: Implement Carbon's actual implicit conversion rules for
// floating-point constants, as per the design
// docs/design/expressions/implicit_conversions.md
llvm::APFloat::opStatus status;
llvm::APFloat result =
RealToAPFloat(context, literal->real_id,
dest_float_type->float_kind.Semantics(), &status);
if (check_overflow && (status & llvm::APFloat::opOverflow)) {
CARBON_DIAGNOSTIC(FloatLiteralTooLargeForType, Error,
"value {0} too large for floating-point type {1}",
RealId, SemIR::TypeId);
context.emitter().Emit(loc_id, FloatLiteralTooLargeForType,
literal->real_id, dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
return MakeFloatResult(context, dest_type_id, std::move(result));
}
if (!dest_float_type) {
context.TODO(loc_id, "conversion from float to float literal");
return SemIR::ErrorInst::ConstantId;
}
// Convert to the destination float semantics.
auto arg = context.insts().GetAs<SemIR::FloatValue>(arg_id);
llvm::APFloat result = context.floats().Get(arg.float_id);
bool loses_info;
auto status = result.convert(dest_float_type->float_kind.Semantics(),
llvm::APFloat::rmNearestTiesToEven, &loses_info);
if (check_overflow && (status & llvm::APFloat::opOverflow)) {
CARBON_DIAGNOSTIC(FloatTooLargeForType, Error,
"value {0} too large for floating-point type {1}",
llvm::APFloat, SemIR::TypeId);
context.emitter().Emit(loc_id, FloatTooLargeForType,
context.floats().Get(arg.float_id), dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
return MakeFloatResult(context, dest_type_id, std::move(result));
}
// Performs a conversion from integer type to a floating-point type, optionally
// checking for exactness.
static auto PerformIntToFloatConvert(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id,
bool require_exact) -> SemIR::ConstantId {
auto arg = context.insts().GetAs<SemIR::IntValue>(arg_id);
auto [src_is_signed, bit_width_id] =
context.sem_ir().types().GetIntTypeInfo(arg.type_id);
llvm::APInt op_val = GetIntAtSuitableWidth(context, arg.int_id, bit_width_id);
auto dest_type_object_rep_id = context.types().GetObjectRepr(dest_type_id);
CARBON_CHECK(dest_type_object_rep_id.has_value(),
"Conversion to incomplete type");
auto dest_float_type =
context.types().TryGetAs<SemIR::FloatType>(dest_type_object_rep_id);
if (!dest_float_type) {
// Target is Core.FloatLiteral, which is always exact.
llvm::APInt mantissa = op_val;
return MakeFloatLiteralResult(
context, Real{.mantissa = mantissa,
.exponent = llvm::APInt(32, 0, /*isSigned=*/true),
.is_decimal = true});
}
llvm::APFloat ap_float(dest_float_type->float_kind.Semantics());
auto status = ap_float.convertFromAPInt(op_val, src_is_signed,
llvm::APFloat::rmNearestTiesToEven);
if (status & llvm::APFloat::opOverflow) {
CARBON_DIAGNOSTIC(IntTooLargeForFloatType, Error,
"integer value {0} too large for floating-point type {1}",
TypedInt, SemIR::TypeId);
context.emitter().Emit(loc_id, IntTooLargeForFloatType,
{.type = arg.type_id, .value = op_val},
dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
if (require_exact && (status & llvm::APFloat::opInexact)) {
CARBON_DIAGNOSTIC(IntLossyConversionToFloat, Error,
"integer value {0} cannot be represented exactly in "
"floating-point type {1}",
TypedInt, SemIR::TypeId);
context.emitter().Emit(loc_id, IntLossyConversionToFloat,
{.type = arg.type_id, .value = op_val},
dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
return MakeFloatResult(context, dest_type_id, std::move(ap_float));
}
// Diagnoses that the real literal is too large for the destination type.
static auto DiagnoseRealLiteralTooLarge(Context& context, SemIR::LocId loc_id,
RealId real_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
CARBON_DIAGNOSTIC(RealLiteralTooLargeForIntType, Error,
"floating-point value {0} too large for integer type {1}",
RealId, SemIR::TypeId);
context.emitter().Emit(loc_id, RealLiteralTooLargeForIntType, real_id,
dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
namespace {
struct BitWidthBounds {
uint64_t lower_bound;
uint64_t upper_bound;
};
} // namespace
// Computes a strict lower bound and upper bound on the bit-width of a real
// literal value when converted to an integer, rounding towards zero.
static auto EstimateRealLiteralBitWidth(const Real& real_val)
-> BitWidthBounds {
const llvm::APInt& mantissa = real_val.mantissa;
const llvm::APInt& exponent = real_val.exponent;
if (mantissa.isZero() ||
(exponent.isNegative() && exponent.abs().getActiveBits() > 64)) {
// If the result is definitely less than one, it rounds to zero.
return {.lower_bound = 0, .upper_bound = 0};
}
// Check if the exponent is extremely large, indicating an immediate overflow.
// We return the maximum possible bit width since the mathematically evaluated
// value has at least 2^64 bits.
if (!exponent.isNegative() && exponent.getActiveBits() > 64) {
return {.lower_bound = static_cast<uint64_t>(-1),
.upper_bound = static_cast<uint64_t>(-1)};
}
uint64_t abs_exponent = exponent.abs().getZExtValue();
uint64_t scale_min = abs_exponent;
uint64_t scale_max = abs_exponent;
if (real_val.is_decimal) {
// 10^4 = 10000 > 8192 = 2^13, so each decimal digit changes the size by
// at least 13/4 bits (safe lower-bound scaling).
scale_min = (abs_exponent * 13) / 4;
// 10^3 = 1000 < 1024 = 2^10, so each decimal digit changes the size by
// less than 10/3 bits (safe upper-bound scaling).
scale_max = (abs_exponent * 10 + 2) / 3;
}
uint64_t lower_bound = mantissa.getActiveBits();
uint64_t upper_bound = mantissa.getActiveBits();
if (exponent.isNegative()) {
// A negative exponent decreases the result size.
lower_bound = (lower_bound > scale_max) ? (lower_bound - scale_max) : 0;
upper_bound = (upper_bound > scale_min) ? (upper_bound - scale_min) : 0;
} else {
// A positive exponent increases the result size.
lower_bound += scale_min;
upper_bound += scale_max;
}
return {.lower_bound = lower_bound, .upper_bound = upper_bound};
}
// Converts an unsized RealId (floating-point literal) to an integer.
static auto ConvertRealLiteralToInt(Context& context, SemIR::LocId loc_id,
RealId real_id, SemIR::TypeId dest_type_id,
bool dest_is_signed, IntId bit_width_id)
-> SemIR::ConstantId {
const auto& real_val = context.reals().Get(real_id);
const llvm::APInt& mantissa = real_val.mantissa;
const llvm::APInt& exponent = real_val.exponent;
auto bounds = EstimateRealLiteralBitWidth(real_val);
if (bounds.upper_bound == 0) {
// The result fits in 0 bits, so must be 0.
return MakeIntResult(context, dest_type_id, context.ints().Add(0));
}
// Sized bounds check: prevent constructing an APInt with a huge number of
// digits if it's way larger than the destination type.
if (bit_width_id.has_value()) {
if (context.ints().Get(bit_width_id).ult(bounds.lower_bound)) {
return DiagnoseRealLiteralTooLarge(context, loc_id, real_id,
dest_type_id);
}
} else if (bounds.lower_bound > IntStore::MaxIntWidth) {
CARBON_DIAGNOSTIC(
RealLiteralTooLargeForUnsizedInt, Error,
"floating-point value {0} too large to convert: result would be an "
"integer whose width is greater than the maximum supported width of "
"{1}",
RealId, int);
context.emitter().Emit(loc_id, RealLiteralTooLargeForUnsizedInt, real_id,
IntStore::MaxIntWidth);
return SemIR::ErrorInst::ConstantId;
}
// Compute an upper bound on the bit width of base^exponent.
unsigned abs_exponent = exponent.abs().getZExtValue();
unsigned exponent_upper_bound =
real_val.is_decimal ? ((abs_exponent * 10 + 2) / 3) : abs_exponent;
// If the exponent is positive, base^exponent cannot be larger than the result
// size. If it's negative, base^exponent can't be *much* larger than the
// mantissa or we'd have computed a lower bound of 0 bits and bailed out.
CARBON_CHECK(exponent_upper_bound <=
std::max<unsigned>(mantissa.getSignificantBits() * 2,
bounds.upper_bound));
// Compute a bit-width in which we can safely compute the result. We need
// enough space to store the mantissa, base^exponent, the result and a sign
// bit, and the number 10 (4 bits).
unsigned calc_width =
std::max({mantissa.getSignificantBits(), exponent_upper_bound,
static_cast<unsigned>(bounds.upper_bound + 1), 4U});
// Compute the integer result.
llvm::APInt integer_val = mantissa.sextOrTrunc(calc_width);
if (!real_val.is_decimal) {
// Binary exponent (mantissa * 2^exponent).
if (!exponent.isNegative()) {
integer_val <<= abs_exponent;
} else {
integer_val.lshrInPlace(abs_exponent);
}
} else {
// Decimal exponent (mantissa * 10^exponent).
llvm::APInt ten(calc_width, 10);
llvm::APInt ten_pow = llvm::APIntOps::pow(ten, abs_exponent);
if (!exponent.isNegative()) {
integer_val *= ten_pow;
} else {
integer_val = integer_val.udiv(ten_pow);
}
}
// If the target type is sized, check the final value fits in the type.
if (bit_width_id.has_value()) {
unsigned dest_width = context.ints().Get(bit_width_id).getZExtValue();
if (integer_val.getActiveBits() > dest_width - dest_is_signed) {
return DiagnoseRealLiteralTooLarge(context, loc_id, real_id,
dest_type_id);
}
}
return MakeIntResult(context, dest_type_id, dest_is_signed,
std::move(integer_val));
}
// Converts a sized FloatId to an integer.
static auto ConvertFloatValueToInt(Context& context, SemIR::LocId loc_id,
FloatId float_id, SemIR::TypeId dest_type_id,
bool dest_is_signed, IntId bit_width_id)
-> SemIR::ConstantId {
llvm::APFloat float_val = context.floats().Get(float_id);
if (float_val.isNaN()) {
CARBON_DIAGNOSTIC(FloatNaNConvertedToInt, Error,
"cannot convert NaN to integer type {0}", SemIR::TypeId);
context.emitter().Emit(loc_id, FloatNaNConvertedToInt, dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
if (float_val.isInfinity()) {
CARBON_DIAGNOSTIC(FloatInfinityConvertedToInt, Error,
"cannot convert infinity to integer type {0}",
SemIR::TypeId);
context.emitter().Emit(loc_id, FloatInfinityConvertedToInt, dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
int exp = float_val.isZero() ? 0 : llvm::ilogb(float_val);
unsigned target_width = bit_width_id.has_value()
? context.ints().Get(bit_width_id).getZExtValue()
: std::max(64, exp + 2);
llvm::APSInt result(target_width, !dest_is_signed);
bool is_exact;
auto status = float_val.convertToInteger(result, llvm::APFloat::rmTowardZero,
&is_exact);
if (status & (llvm::APFloat::opOverflow | llvm::APFloat::opInvalidOp)) {
CARBON_DIAGNOSTIC(FloatTooLargeForIntType, Error,
"floating-point value {0} too large for integer type {1}",
llvm::APFloat, SemIR::TypeId);
context.emitter().Emit(loc_id, FloatTooLargeForIntType, float_val,
dest_type_id);
return SemIR::ErrorInst::ConstantId;
}
return MakeIntResult(context, dest_type_id, dest_is_signed,
std::move(result));
}
// Performs a conversion from a floating-point type to an integer type.
static auto PerformFloatToIntConvert(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id,
SemIR::TypeId dest_type_id)
-> SemIR::ConstantId {
auto [dest_is_signed, bit_width_id] =
context.sem_ir().types().GetIntTypeInfo(dest_type_id);
if (auto literal =
context.insts().TryGetAs<SemIR::FloatLiteralValue>(arg_id)) {
return ConvertRealLiteralToInt(context, loc_id, literal->real_id,
dest_type_id, dest_is_signed, bit_width_id);
}
auto arg = context.insts().GetAs<SemIR::FloatValue>(arg_id);
return ConvertFloatValueToInt(context, loc_id, arg.float_id, dest_type_id,
dest_is_signed, bit_width_id);
}
// Issues a diagnostic for a compile-time division by zero.
static auto DiagnoseDivisionByZero(Context& context, SemIR::LocId loc_id)
-> void {
CARBON_DIAGNOSTIC(CompileTimeDivisionByZero, Error, "division by zero");
context.emitter().Emit(loc_id, CompileTimeDivisionByZero);
}
// Performs a builtin unary integer -> integer operation.
static auto PerformBuiltinUnaryIntOp(Context& context, SemIR::LocId loc_id,
SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId arg_id)
-> SemIR::ConstantId {
auto op = context.insts().GetAs<SemIR::IntValue>(arg_id);
auto [is_signed, bit_width_id] =
context.sem_ir().types().GetIntTypeInfo(op.type_id);
llvm::APInt op_val = GetIntAtSuitableWidth(context, op.int_id, bit_width_id);
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::IntSNegate:
if (op_val.isMinSignedValue()) {
if (bit_width_id.has_value()) {
CARBON_DIAGNOSTIC(CompileTimeIntegerNegateOverflow, Error,
"integer overflow in negation of {0}", TypedInt);
context.emitter().Emit(loc_id, CompileTimeIntegerNegateOverflow,
{.type = op.type_id, .value = op_val});
} else {
// Widen the integer so we don't overflow into the sign bit.
op_val = op_val.sext(op_val.getBitWidth() +
llvm::APInt::APINT_BITS_PER_WORD);
}
}
op_val.negate();
break;
case SemIR::BuiltinFunctionKind::IntUNegate:
CARBON_CHECK(bit_width_id.has_value(), "Unsigned negate on unsized int");
op_val.negate();
break;
case SemIR::BuiltinFunctionKind::IntComplement:
// TODO: Should we have separate builtins for signed and unsigned
// complement? Like with signed/unsigned negate, these operations do
// different things to the integer value, even though they do the same
// thing to the bits. We treat IntLiteral complement as signed complement,
// given that the result of unsigned complement depends on the bit width.
op_val.flipAllBits();
break;
default:
CARBON_FATAL("Unexpected builtin kind");
}
return MakeIntResult(context, op.type_id, is_signed, std::move(op_val));
}
namespace {
// A pair of APInts that are the operands of a binary operator. We use an
// aggregate rather than `std::pair` to allow RVO of the individual ints.
struct APIntBinaryOperands {
llvm::APInt lhs;
llvm::APInt rhs;
};
} // namespace
// Get a pair of integers at the same suitable bit-width: either their actual
// width if they have a fixed width, or the smallest canonical width in which
// they both fit otherwise.
static auto GetIntsAtSuitableWidth(Context& context, IntId lhs_id, IntId rhs_id,
IntId bit_width_id) -> APIntBinaryOperands {
// Unsized operands: take the wider of the bit widths.
if (!bit_width_id.has_value()) {
APIntBinaryOperands result = {.lhs = context.ints().Get(lhs_id),
.rhs = context.ints().Get(rhs_id)};
if (result.lhs.getBitWidth() != result.rhs.getBitWidth()) {
if (result.lhs.getBitWidth() > result.rhs.getBitWidth()) {
result.rhs = result.rhs.sext(result.lhs.getBitWidth());
} else {
result.lhs = result.lhs.sext(result.rhs.getBitWidth());
}
}
return result;
}
return {.lhs = context.ints().GetAtWidth(lhs_id, bit_width_id),
.rhs = context.ints().GetAtWidth(rhs_id, bit_width_id)};
}
namespace {
// The result of performing a binary int operation.
struct BinaryIntOpResult {
llvm::APInt result_val;
bool overflow;
Lex::TokenKind op_token;
};
} // namespace
// Computes the result of a homogeneous binary (int, int) -> int operation.
static auto ComputeBinaryIntOpResult(SemIR::BuiltinFunctionKind builtin_kind,
const llvm::APInt& lhs_val,
const llvm::APInt& rhs_val)
-> BinaryIntOpResult {
llvm::APInt result_val;
bool overflow = false;
Lex::TokenKind op_token = Lex::TokenKind::Not;
switch (builtin_kind) {
// Arithmetic.
case SemIR::BuiltinFunctionKind::IntSAdd:
result_val = lhs_val.sadd_ov(rhs_val, overflow);
op_token = Lex::TokenKind::Plus;
break;
case SemIR::BuiltinFunctionKind::IntSSub:
result_val = lhs_val.ssub_ov(rhs_val, overflow);
op_token = Lex::TokenKind::Minus;
break;
case SemIR::BuiltinFunctionKind::IntSMul:
result_val = lhs_val.smul_ov(rhs_val, overflow);
op_token = Lex::TokenKind::Star;
break;
case SemIR::BuiltinFunctionKind::IntSDiv:
result_val = lhs_val.sdiv_ov(rhs_val, overflow);
op_token = Lex::TokenKind::Slash;
break;
case SemIR::BuiltinFunctionKind::IntSMod:
result_val = lhs_val.srem(rhs_val);
// LLVM weirdly lacks `srem_ov`, so we work it out for ourselves:
// <signed min> % -1 overflows because <signed min> / -1 overflows.
overflow = lhs_val.isMinSignedValue() && rhs_val.isAllOnes();
op_token = Lex::TokenKind::Percent;
break;
case SemIR::BuiltinFunctionKind::IntUAdd:
result_val = lhs_val + rhs_val;
op_token = Lex::TokenKind::Plus;
break;
case SemIR::BuiltinFunctionKind::IntUSub:
result_val = lhs_val - rhs_val;
op_token = Lex::TokenKind::Minus;
break;
case SemIR::BuiltinFunctionKind::IntUMul:
result_val = lhs_val * rhs_val;
op_token = Lex::TokenKind::Star;
break;
case SemIR::BuiltinFunctionKind::IntUDiv:
result_val = lhs_val.udiv(rhs_val);
op_token = Lex::TokenKind::Slash;
break;
case SemIR::BuiltinFunctionKind::IntUMod:
result_val = lhs_val.urem(rhs_val);
op_token = Lex::TokenKind::Percent;
break;
// Bitwise.
case SemIR::BuiltinFunctionKind::IntAnd:
result_val = lhs_val & rhs_val;
op_token = Lex::TokenKind::And;
break;
case SemIR::BuiltinFunctionKind::IntOr:
result_val = lhs_val | rhs_val;
op_token = Lex::TokenKind::Pipe;
break;
case SemIR::BuiltinFunctionKind::IntXor:
result_val = lhs_val ^ rhs_val;
op_token = Lex::TokenKind::Caret;
break;
case SemIR::BuiltinFunctionKind::IntLeftShift:
case SemIR::BuiltinFunctionKind::IntRightShift:
CARBON_FATAL("Non-homogeneous operation handled separately.");
default:
CARBON_FATAL("Unexpected operation kind.");
}
return {.result_val = std::move(result_val),
.overflow = overflow,
.op_token = op_token};
}
// Performs a builtin integer bit shift operation.
static auto PerformBuiltinIntShiftOp(Context& context, SemIR::LocId loc_id,
SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id, SemIR::InstId rhs_id)
-> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::IntValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
auto [lhs_is_signed, lhs_bit_width_id] =
context.sem_ir().types().GetIntTypeInfo(lhs.type_id);
llvm::APInt lhs_val =
GetIntAtSuitableWidth(context, lhs.int_id, lhs_bit_width_id);
const auto& rhs_orig_val = context.ints().Get(rhs.int_id);
if (lhs_bit_width_id.has_value() && rhs_orig_val.uge(lhs_val.getBitWidth())) {
CARBON_DIAGNOSTIC(
CompileTimeShiftOutOfRange, Error,
"shift distance >= type width of {0} in `{1} {2:<<|>>} {3}`", unsigned,
TypedInt, Diagnostics::BoolAsSelect, TypedInt);
context.emitter().Emit(
loc_id, CompileTimeShiftOutOfRange, lhs_val.getBitWidth(),
{.type = lhs.type_id, .value = lhs_val},
builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift,
{.type = rhs.type_id, .value = rhs_orig_val});
// TODO: Is it useful to recover by returning 0 or -1?
return SemIR::ErrorInst::ConstantId;
}
if (rhs_orig_val.isNegative() &&
context.sem_ir().types().IsSignedInt(rhs.type_id)) {
CARBON_DIAGNOSTIC(CompileTimeShiftNegative, Error,
"shift distance negative in `{0} {1:<<|>>} {2}`",
TypedInt, Diagnostics::BoolAsSelect, TypedInt);
context.emitter().Emit(
loc_id, CompileTimeShiftNegative,
{.type = lhs.type_id, .value = lhs_val},
builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift,
{.type = rhs.type_id, .value = rhs_orig_val});
// TODO: Is it useful to recover by returning 0 or -1?
return SemIR::ErrorInst::ConstantId;
}
llvm::APInt result_val;
if (builtin_kind == SemIR::BuiltinFunctionKind::IntLeftShift) {
if (!lhs_bit_width_id.has_value() && !lhs_val.isZero()) {
// Ensure we don't generate a ridiculously large integer through a bit
// shift.
auto width = rhs_orig_val.trySExtValue();
if (!width ||
*width > IntStore::MaxIntWidth - lhs_val.getSignificantBits()) {
CARBON_DIAGNOSTIC(CompileTimeUnsizedShiftOutOfRange, Error,
"shift distance of {0} would result in an "
"integer whose width is greater than the "
"maximum supported width of {1}",
TypedInt, int);
context.emitter().Emit(loc_id, CompileTimeUnsizedShiftOutOfRange,
{.type = rhs.type_id, .value = rhs_orig_val},
IntStore::MaxIntWidth);
return SemIR::ErrorInst::ConstantId;
}
lhs_val = lhs_val.sext(
IntStore::CanonicalBitWidth(lhs_val.getSignificantBits() + *width));
}
result_val =
lhs_val.shl(rhs_orig_val.getLimitedValue(lhs_val.getBitWidth()));
} else if (lhs_is_signed) {
result_val =
lhs_val.ashr(rhs_orig_val.getLimitedValue(lhs_val.getBitWidth()));
} else {
CARBON_CHECK(lhs_bit_width_id.has_value(), "Logical shift on unsized int");
result_val =
lhs_val.lshr(rhs_orig_val.getLimitedValue(lhs_val.getBitWidth()));
}
return MakeIntResult(context, lhs.type_id, lhs_is_signed,
std::move(result_val));
}
// Performs a homogeneous builtin binary integer -> integer operation.
static auto PerformBuiltinBinaryIntOp(Context& context, SemIR::LocId loc_id,
SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id,
SemIR::InstId rhs_id)
-> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::IntValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
CARBON_CHECK(rhs.type_id == lhs.type_id, "Heterogeneous builtin integer op!");
auto type_id = lhs.type_id;
auto [is_signed, bit_width_id] =
context.sem_ir().types().GetIntTypeInfo(type_id);
auto [lhs_val, rhs_val] =
GetIntsAtSuitableWidth(context, lhs.int_id, rhs.int_id, bit_width_id);
// Check for division by zero.
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::IntSDiv:
case SemIR::BuiltinFunctionKind::IntSMod:
case SemIR::BuiltinFunctionKind::IntUDiv:
case SemIR::BuiltinFunctionKind::IntUMod:
if (rhs_val.isZero()) {
DiagnoseDivisionByZero(context, loc_id);
return SemIR::ErrorInst::ConstantId;
}
break;
default:
break;
}
BinaryIntOpResult result =
ComputeBinaryIntOpResult(builtin_kind, lhs_val, rhs_val);
if (result.overflow && !bit_width_id.has_value()) {
// Retry with a larger bit width. Most operations can only overflow by one
// bit, but signed n-bit multiplication can overflow to 2n-1 bits. We don't
// need to handle unsigned multiplication here because it's not permitted
// for unsized integers.
//
// Note that we speculatively first perform the calculation in the width of
// the wider operand: smaller operations are faster and overflow to a wider
// integer is unlikely to be needed, especially given that the width will
// have been rounded up to a multiple of 64 bits by the int store.
CARBON_CHECK(builtin_kind != SemIR::BuiltinFunctionKind::IntUMul,
"Unsigned arithmetic requires a fixed bitwidth");
int new_width =
builtin_kind == SemIR::BuiltinFunctionKind::IntSMul
? lhs_val.getBitWidth() * 2
: IntStore::CanonicalBitWidth(lhs_val.getBitWidth() + 1);
new_width = std::min(new_width, IntStore::MaxIntWidth);
lhs_val = context.ints().GetAtWidth(lhs.int_id, new_width);
rhs_val = context.ints().GetAtWidth(rhs.int_id, new_width);
// Note that this can in theory still overflow if we limited `new_width` to
// `MaxIntWidth`. In that case we fall through to the signed overflow
// diagnostic below.
result = ComputeBinaryIntOpResult(builtin_kind, lhs_val, rhs_val);
CARBON_CHECK(!result.overflow || new_width == IntStore::MaxIntWidth);
}
if (result.overflow) {
CARBON_DIAGNOSTIC(CompileTimeIntegerOverflow, Error,
"integer overflow in calculation `{0} {1} {2}`", TypedInt,
Lex::TokenKind, TypedInt);
context.emitter().Emit(loc_id, CompileTimeIntegerOverflow,
{.type = type_id, .value = lhs_val}, result.op_token,
{.type = type_id, .value = rhs_val});
}
return MakeIntResult(context, type_id, is_signed,
std::move(result.result_val));
}
// Performs a builtin integer comparison.
static auto PerformBuiltinIntComparison(Context& context,
SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id,
SemIR::InstId rhs_id,
SemIR::TypeId bool_type_id)
-> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::IntValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::IntValue>(rhs_id);
llvm::APInt lhs_val = context.ints().Get(lhs.int_id);
llvm::APInt rhs_val = context.ints().Get(rhs.int_id);
bool result;
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::IntEq:
result = (lhs_val == rhs_val);
break;
case SemIR::BuiltinFunctionKind::IntNeq:
result = (lhs_val != rhs_val);
break;
case SemIR::BuiltinFunctionKind::IntLess:
result = lhs_val.slt(rhs_val);
break;
case SemIR::BuiltinFunctionKind::IntLessEq:
result = lhs_val.sle(rhs_val);
break;
case SemIR::BuiltinFunctionKind::IntGreater:
result = lhs_val.sgt(rhs_val);
break;
case SemIR::BuiltinFunctionKind::IntGreaterEq:
result = lhs_val.sge(rhs_val);
break;
default:
CARBON_FATAL("Unexpected operation kind.");
}
return MakeBoolResult(context, bool_type_id, result);
}
static auto NegateRealLiteral(Real val) -> Real {
// Check if negation would overflow.
if (val.mantissa.isMinSignedValue()) {
val.mantissa = val.mantissa.sext(val.mantissa.getBitWidth() + 1);
}
val.mantissa.negate();
return val;
}
// Performs a builtin unary float -> float operation.
static auto PerformBuiltinUnaryFloatOp(Context& context,
SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId arg_id)
-> SemIR::ConstantId {
CARBON_CHECK(builtin_kind == SemIR::BuiltinFunctionKind::FloatNegate,
"Unexpected builtin kind");
if (auto literal =
context.insts().TryGetAs<SemIR::FloatLiteralValue>(arg_id)) {
auto real_val = context.reals().Get(literal->real_id);
return MakeFloatLiteralResult(context, NegateRealLiteral(real_val));
}
auto op = context.insts().GetAs<SemIR::FloatValue>(arg_id);
auto op_val = context.floats().Get(op.float_id);
op_val.changeSign();
return MakeFloatResult(context, op.type_id, std::move(op_val));
}
// Adds two APInts handling overflow by growing result.
// Assumes lhs and rhs have same bit width.
static auto OverflowAdd(const llvm::APInt& lhs, const llvm::APInt& rhs)
-> llvm::APInt {
CARBON_CHECK(lhs.getBitWidth() == rhs.getBitWidth());
bool is_negative = lhs.isNegative();
bool overflow = false;
llvm::APInt result = lhs.sadd_ov(rhs, overflow);
if (overflow) {
unsigned old_width = lhs.getBitWidth();
unsigned new_width = old_width + 1;
result = result.zext(new_width);
if (is_negative) {
// For positive value overflow, zero-extension is sufficient, for negative
// overflow we need to re-apply the dropped sign bits.
result.setBits(old_width, new_width);
}
}
return result;
}
struct FactoredExponent {
int twos = 0;
int fives = 0;
};
// Decomposes Real's exponents into form 2^a * 5^b where a and b are 32-bit
// ints. Returns nullopt if exponent is too large.
static auto TryGetFactoredExponent(const Real& real)
-> std::optional<FactoredExponent> {
if (real.exponent.getSignificantBits() > 32) {
// Reject evaluation if we can't fit exponent into int.
return std::nullopt;
}
auto exponent = static_cast<int>(real.exponent.getZExtValue());
return FactoredExponent{
.twos = exponent,
.fives = real.is_decimal ? exponent : 0,
};
}
// Constructs a Real from mantissa and factored exponent.
static auto RecombineFactoredExponent(llvm::APInt mantissa,
const FactoredExponent& exponent)
-> Real {
CARBON_CHECK(exponent.fives == exponent.twos || exponent.fives == 0,
"exponent must by dyadic or decadic");
return {
.mantissa = mantissa,
.exponent = llvm::APInt(32, exponent.twos, /*isSigned=*/true),
.is_decimal = exponent.fives == exponent.twos,
};
}
// Finds a dyadic or decadic exponent that both lhs and rhs can be converted to.
static auto FindCommonExponent(FactoredExponent lhs, FactoredExponent rhs)
-> FactoredExponent {
// In general common exponent of x^a and x^b is x^min(a,b) because we can
// always subtract from exponent (and increase mantisssa by factor) but we
// can't add to exponent unless factor divides the mantisssa.
FactoredExponent min_factors = {
.twos = std::min(lhs.twos, rhs.twos),
.fives = std::min(lhs.fives, rhs.fives),
};
// If both lhs and rhs have positive 5^n factor, we can convert to dyadic or
// decadic real. Choose the one that minimizes mantissa size.
// Assume x * 5^n requires 3n bits and x * 2^n requires n bits.
int factor_diff = std::abs(min_factors.fives - min_factors.twos);
int decadic_bits =
min_factors.fives > min_factors.twos ? 3 * factor_diff : factor_diff;
if (min_factors.fives >= 0) {
int dyadic_bits = 3 * min_factors.fives;
if (dyadic_bits < decadic_bits) {
// Result will be (likely) smaller if stored as dyadic real.
return {
.twos = min_factors.twos,
.fives = 0,
};
}
}
int min_exponent = std::min(min_factors.fives, min_factors.twos);
return {
.twos = min_exponent,
.fives = min_exponent,
};
}
// Finds difference between two exponents.
static auto ComputeExponentDelta(const FactoredExponent& lhs,
const FactoredExponent& rhs)
-> FactoredExponent {
return {
.twos = lhs.twos - rhs.twos,
.fives = lhs.fives - rhs.fives,
};
}
// Estimates additional bits required to apply exponent to an APInt.
static auto EstimateBitsForExponent(const FactoredExponent& exponent) -> int {
CARBON_CHECK(exponent.twos >= 0 && exponent.fives >= 0);
return static_cast<int>(std::ceil(std::log2f(5) * exponent.fives)) +
exponent.twos;
}
// Multiplies factored exponent with provided APInt sign, result is sign
// extended to `bit_width`.
static auto ApplyFactoredExponent(const FactoredExponent& exponent,
const llvm::APInt& mantissa,
unsigned bit_width) -> llvm::APInt {
CARBON_CHECK(exponent.twos >= 0 && exponent.fives >= 0);
auto result = mantissa.sextOrTrunc(bit_width);
if (exponent.twos > 0) {
result <<= exponent.twos;
}
if (exponent.fives > 0) {
result *= llvm::APIntOps::pow(llvm::APInt(bit_width, 5), exponent.fives);
}
return result;
}
// Adds or subtracts two Real literals.
// Returns std::nullopt if value would be too large to compute without losing
// precision.
static auto TryAddRealLiterals(const Real& lhs, const Real& rhs)
-> std::optional<Real> {
auto lhs_exponent = TryGetFactoredExponent(lhs);
if (!lhs_exponent) {
return std::nullopt;
}
auto rhs_exponent = TryGetFactoredExponent(rhs);
if (!rhs_exponent) {
return std::nullopt;
}
// Find an exponent we can convert both lhs and rhs to.
auto common_exponent = FindCommonExponent(*lhs_exponent, *rhs_exponent);
// Find change to mantisssa (in form of factored exponents) so that lhs and
// rhs have desired exponent.
auto lhs_exponent_delta =
ComputeExponentDelta(*lhs_exponent, common_exponent);
auto rhs_exponent_delta =
ComputeExponentDelta(*rhs_exponent, common_exponent);
// Assume no overflow during addition, OverflowAdd will grow final result if
// necessary.
auto bit_width = std::max({
lhs.mantissa.getSignificantBits() +
EstimateBitsForExponent(lhs_exponent_delta),
rhs.mantissa.getSignificantBits() +
EstimateBitsForExponent(rhs_exponent_delta),
static_cast<unsigned>(IntStore::MinAPWidth),
});
if (bit_width > IntStore::MaxIntWidth) {
// Mantissa is too big.
return std::nullopt;
}
auto lhs_mantissa =
ApplyFactoredExponent(lhs_exponent_delta, lhs.mantissa, bit_width);
auto rhs_mantissa =
ApplyFactoredExponent(rhs_exponent_delta, rhs.mantissa, bit_width);
return RecombineFactoredExponent(OverflowAdd(lhs_mantissa, rhs_mantissa),
common_exponent);
}
// Performs a builtin binary real -> real operation.
static auto PerformBuiltinBinaryFloatLiteralOp(
Context& context, SemIR::LocId loc_id,
SemIR::BuiltinFunctionKind builtin_kind, RealId lhs_id, RealId rhs_id)
-> SemIR::ConstantId {
auto lhs_val = context.reals().Get(lhs_id);
auto rhs_val = context.reals().Get(rhs_id);
Lex::TokenKind op_token;
std::optional<Real> result;
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::FloatAdd:
result = TryAddRealLiterals(lhs_val, rhs_val);
op_token = Lex::TokenKind::Plus;
break;
case SemIR::BuiltinFunctionKind::FloatSub:
result = TryAddRealLiterals(lhs_val, NegateRealLiteral(rhs_val));
op_token = Lex::TokenKind::Minus;
break;
default:
CARBON_FATAL("Unexpected operation kind.");
}
if (!result) {
CARBON_DIAGNOSTIC(
CompileTimeFloatLiteralBinaryOperationOutOfRange, Error,
"binary calculation `{0} {1} {2}` would exceed the maximum "
"supported integer width of {3}",
Real, Lex::TokenKind, Real, int);
context.emitter().Emit(loc_id,
CompileTimeFloatLiteralBinaryOperationOutOfRange,
lhs_val, op_token, rhs_val, IntStore::MaxIntWidth);
return SemIR::ErrorInst::ConstantId;
}
return MakeFloatLiteralResult(context, std::move(*result));
}
// Performs a builtin binary float -> float operation.
static auto PerformBuiltinBinaryFloatOp(Context& context, SemIR::LocId loc_id,
SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id,
SemIR::InstId rhs_id)
-> SemIR::ConstantId {
if (context.insts().Is<SemIR::FloatLiteralValue>(lhs_id)) {
auto literal_lhs = context.insts().GetAs<SemIR::FloatLiteralValue>(lhs_id);
auto literal_rhs = context.insts().GetAs<SemIR::FloatLiteralValue>(rhs_id);
return PerformBuiltinBinaryFloatLiteralOp(context, loc_id, builtin_kind,
literal_lhs.real_id,
literal_rhs.real_id);
}
auto lhs = context.insts().GetAs<SemIR::FloatValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::FloatValue>(rhs_id);
auto lhs_val = context.floats().Get(lhs.float_id);
auto rhs_val = context.floats().Get(rhs.float_id);
llvm::APFloat result_val(lhs_val.getSemantics());
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::FloatAdd:
result_val = lhs_val + rhs_val;
break;
case SemIR::BuiltinFunctionKind::FloatSub:
result_val = lhs_val - rhs_val;
break;
case SemIR::BuiltinFunctionKind::FloatMul:
result_val = lhs_val * rhs_val;
break;
case SemIR::BuiltinFunctionKind::FloatDiv:
result_val = lhs_val / rhs_val;
break;
default:
CARBON_FATAL("Unexpected operation kind.");
}
return MakeFloatResult(context, lhs.type_id, std::move(result_val));
}
// Performs a builtin float comparison.
static auto PerformBuiltinFloatComparison(
Context& context, SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id, SemIR::InstId rhs_id, SemIR::TypeId bool_type_id)
-> SemIR::ConstantId {
auto lhs = context.insts().GetAs<SemIR::FloatValue>(lhs_id);
auto rhs = context.insts().GetAs<SemIR::FloatValue>(rhs_id);
const auto& lhs_val = context.floats().Get(lhs.float_id);
const auto& rhs_val = context.floats().Get(rhs.float_id);
bool result;
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::FloatEq:
result = (lhs_val == rhs_val);
break;
case SemIR::BuiltinFunctionKind::FloatNeq:
result = (lhs_val != rhs_val);
break;
case SemIR::BuiltinFunctionKind::FloatLess:
result = lhs_val < rhs_val;
break;
case SemIR::BuiltinFunctionKind::FloatLessEq:
result = lhs_val <= rhs_val;
break;
case SemIR::BuiltinFunctionKind::FloatGreater:
result = lhs_val > rhs_val;
break;
case SemIR::BuiltinFunctionKind::FloatGreaterEq:
result = lhs_val >= rhs_val;
break;
default:
CARBON_FATAL("Unexpected operation kind.");
}
return MakeBoolResult(context, bool_type_id, result);
}
// Performs a builtin boolean comparison.
static auto PerformBuiltinBoolComparison(
Context& context, SemIR::BuiltinFunctionKind builtin_kind,
SemIR::InstId lhs_id, SemIR::InstId rhs_id, SemIR::TypeId bool_type_id) {
bool lhs = context.insts().GetAs<SemIR::BoolLiteral>(lhs_id).value.ToBool();
bool rhs = context.insts().GetAs<SemIR::BoolLiteral>(rhs_id).value.ToBool();
return MakeBoolResult(context, bool_type_id,
builtin_kind == SemIR::BuiltinFunctionKind::BoolEq
? lhs == rhs
: lhs != rhs);
}
// Converts a call argument to a DeclaredFacetTypeId.
static auto ArgToFacetTypeId(Context& context, SemIR::LocId loc_id,
SemIR::InstId arg_id)
-> SemIR::DeclaredFacetTypeId {
auto type_arg_id = context.types().GetAsTypeInstId(arg_id);
if (auto facet_type =
context.insts().TryGetAs<SemIR::FacetType>(type_arg_id)) {
return facet_type->declared_facet_type_id;
}
CARBON_DIAGNOSTIC(FacetTypeRequiredForTypeAndOperator, Error,
"non-facet type {0} combined with `&` operator",
SemIR::TypeId);
// TODO: Find a location for the lhs or rhs specifically, instead of
// the whole thing. If that's not possible we can change the text to
// say if it's referring to the left or the right side for the error.
// The `arg_id` instruction has no location in it for some reason.
context.emitter().Emit(loc_id, FacetTypeRequiredForTypeAndOperator,
context.types().GetTypeIdForTypeInstId(type_arg_id));
return SemIR::DeclaredFacetTypeId::None;
}
// Returns a constant for a call to a builtin function.
static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
SemIR::LocId loc_id, SemIR::Call call,
SemIR::BuiltinFunctionKind builtin_kind,
llvm::ArrayRef<SemIR::InstId> arg_ids,
Phase phase) -> SemIR::ConstantId {
auto& context = eval_context.context();
switch (builtin_kind) {
case SemIR::BuiltinFunctionKind::None:
CARBON_FATAL("Not a builtin function.");
case SemIR::BuiltinFunctionKind::NoOp: {
return MakeEmptyTupleResult(eval_context);
}
case SemIR::BuiltinFunctionKind::PrimitiveCopy: {
return context.constant_values().Get(arg_ids[0]);
}
case SemIR::BuiltinFunctionKind::StringAt: {
Phase phase = Phase::Concrete;
auto str_id = GetConstantValue(eval_context, arg_ids[0], &phase);
auto index_id = GetConstantValue(eval_context, arg_ids[1], &phase);
if (phase != Phase::Concrete) {
return MakeNonConstantResult(phase);
}
auto str_struct = eval_context.insts().GetAs<SemIR::StructValue>(str_id);
auto elements = eval_context.inst_blocks().Get(str_struct.elements_id);
// String struct has two fields: a pointer to the string data and the
// length.
CARBON_CHECK(elements.size() == 2, "String struct should have 2 fields.");
auto string_literal = eval_context.insts().GetAs<SemIR::StringLiteral>(
eval_context.constant_values().GetConstantInstId(elements[0]));
const auto& string_value =
eval_context.sem_ir().string_literal_values().Get(
string_literal.string_literal_id);
auto index_inst = eval_context.insts().GetAs<SemIR::IntValue>(index_id);
const auto& index_val = eval_context.ints().Get(index_inst.int_id);
if (index_val.isNegative()) {
CARBON_DIAGNOSTIC(StringAtIndexNegative, Error,
"index `{0}` is negative.", TypedInt);
context.emitter().Emit(
loc_id, StringAtIndexNegative,
{.type = eval_context.insts().Get(index_id).type_id(),
.value = index_val});
return SemIR::ConstantId::NotConstant;
}
if (index_val.getZExtValue() >= string_value.size()) {
CARBON_DIAGNOSTIC(
StringAtIndexOutOfBounds, Error,
"string index `{0}` is out of bounds; string has length {1}.",
TypedInt, size_t);
context.emitter().Emit(
loc_id, StringAtIndexOutOfBounds,
{.type = eval_context.insts().Get(index_id).type_id(),
.value = index_val},
string_value.size());
return SemIR::ConstantId::NotConstant;
}
auto char_value =
static_cast<uint8_t>(string_value[index_val.getZExtValue()]);
return MakeIntResult(eval_context.context(), call.type_id,
/*is_signed=*/false, llvm::APInt(32, char_value));
}
case SemIR::BuiltinFunctionKind::MakeUninitialized:
case SemIR::BuiltinFunctionKind::PrintChar:
case SemIR::BuiltinFunctionKind::PrintInt:
case SemIR::BuiltinFunctionKind::ReadChar:
case SemIR::BuiltinFunctionKind::FloatAddAssign:
case SemIR::BuiltinFunctionKind::FloatSubAssign:
case SemIR::BuiltinFunctionKind::FloatMulAssign:
case SemIR::BuiltinFunctionKind::FloatDivAssign:
case SemIR::BuiltinFunctionKind::IntSAddAssign:
case SemIR::BuiltinFunctionKind::IntSSubAssign:
case SemIR::BuiltinFunctionKind::IntSMulAssign:
case SemIR::BuiltinFunctionKind::IntSDivAssign:
case SemIR::BuiltinFunctionKind::IntSModAssign:
case SemIR::BuiltinFunctionKind::IntUAddAssign:
case SemIR::BuiltinFunctionKind::IntUSubAssign:
case SemIR::BuiltinFunctionKind::IntUMulAssign:
case SemIR::BuiltinFunctionKind::IntUDivAssign:
case SemIR::BuiltinFunctionKind::IntUModAssign:
case SemIR::BuiltinFunctionKind::IntAndAssign:
case SemIR::BuiltinFunctionKind::IntOrAssign:
case SemIR::BuiltinFunctionKind::IntXorAssign:
case SemIR::BuiltinFunctionKind::IntLeftShiftAssign:
case SemIR::BuiltinFunctionKind::IntRightShiftAssign:
case SemIR::BuiltinFunctionKind::PointerMakeNull:
case SemIR::BuiltinFunctionKind::PointerIsNull:
case SemIR::BuiltinFunctionKind::PointerUnsafeConvert:
case SemIR::BuiltinFunctionKind::CppStdInitializerListMake: {
// These are runtime-only builtins.
// TODO: Consider tracking this on the `BuiltinFunctionKind`.
return SemIR::ConstantId::NotConstant;
}
case SemIR::BuiltinFunctionKind::TypeAnd: {
CARBON_CHECK(arg_ids.size() == 2);
auto lhs_declared_facet_type_id =
ArgToFacetTypeId(context, loc_id, arg_ids[0]);
auto rhs_declared_facet_type_id =
ArgToFacetTypeId(context, loc_id, arg_ids[1]);
// Allow errors to be diagnosed for both sides of the operator before
// returning here if any error occurred on either side.
if (!lhs_declared_facet_type_id.has_value() ||
!rhs_declared_facet_type_id.has_value()) {
return SemIR::ErrorInst::ConstantId;
}
// Reuse one of the argument instructions if nothing has changed.
if (lhs_declared_facet_type_id == rhs_declared_facet_type_id) {
return context.types().GetConstantId(
context.types().GetTypeIdForTypeInstId(arg_ids[0]));
}
auto combined_declared_facet_type = SemIR::DeclaredFacetType::Combine(
context.declared_facet_types().Get(lhs_declared_facet_type_id),
context.declared_facet_types().Get(rhs_declared_facet_type_id));
if (!ResolveFacetTypeRewriteConstraints(
eval_context.context(), loc_id,
combined_declared_facet_type.rewrite_constraints)) {
phase = Phase::UnknownDueToError;
}
combined_declared_facet_type.Canonicalize();
return MakeFacetTypeResult(eval_context.context(),
combined_declared_facet_type, phase);
}
case SemIR::BuiltinFunctionKind::CharLiteralMakeType: {
return context.constant_values().Get(SemIR::CharLiteralType::TypeInstId);
}
case SemIR::BuiltinFunctionKind::FloatLiteralMakeType: {
return context.constant_values().Get(SemIR::FloatLiteralType::TypeInstId);
}
case SemIR::BuiltinFunctionKind::IntLiteralMakeType: {
return context.constant_values().Get(SemIR::IntLiteralType::TypeInstId);
}
case SemIR::BuiltinFunctionKind::IntMakeTypeSigned: {
return MakeIntTypeResult(context, loc_id, SemIR::IntKind::Signed,
arg_ids[0], phase);
}
case SemIR::BuiltinFunctionKind::IntMakeTypeUnsigned: {
return MakeIntTypeResult(context, loc_id, SemIR::IntKind::Unsigned,
arg_ids[0], phase);
}
case SemIR::BuiltinFunctionKind::FloatMakeType: {
return MakeFloatTypeResult(context, loc_id, arg_ids[0], phase);
}
case SemIR::BuiltinFunctionKind::BoolMakeType: {
return context.constant_values().Get(SemIR::BoolType::TypeInstId);
}
case SemIR::BuiltinFunctionKind::MaybeUnformedMakeType: {
return MakeConstantResult(
context,
SemIR::MaybeUnformedType{
.type_id = SemIR::TypeType::TypeId,
.inner_id = context.types().GetAsTypeInstId(arg_ids[0])},
phase);
}
case SemIR::BuiltinFunctionKind::FormMakeType: {
return context.constant_values().Get(SemIR::FormType::TypeInstId);
}
case SemIR::BuiltinFunctionKind::CharLiteralAdd:
case SemIR::BuiltinFunctionKind::CharLiteralSubInt: {
if (phase != Phase::Concrete) {
break;
}
return PerformCharLiteralArithmetic(context, loc_id, builtin_kind,
arg_ids[0], arg_ids[1], call.type_id);
}
case SemIR::BuiltinFunctionKind::CharLiteralConvertChar: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformCharLiteralConvertChar(context, loc_id, arg_ids[0],
call.type_id);
}
case SemIR::BuiltinFunctionKind::CharLiteralConvertInt: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformCharLiteralConvertInt(context, loc_id, arg_ids[0],
call.type_id);
}
case SemIR::BuiltinFunctionKind::CharLiteralEq:
case SemIR::BuiltinFunctionKind::CharLiteralGreater:
case SemIR::BuiltinFunctionKind::CharLiteralGreaterEq:
case SemIR::BuiltinFunctionKind::CharLiteralLess:
case SemIR::BuiltinFunctionKind::CharLiteralLessEq:
case SemIR::BuiltinFunctionKind::CharLiteralNeq: {
if (phase != Phase::Concrete) {
break;
}
return PerformCharLiteralComparison(context, builtin_kind, arg_ids[0],
arg_ids[1], call.type_id);
}
case SemIR::BuiltinFunctionKind::CharLiteralSubChar: {
if (phase != Phase::Concrete) {
break;
}
return PerformCharLiteralSubChar(context, arg_ids[0], arg_ids[1],
call.type_id);
}
case SemIR::BuiltinFunctionKind::IntAddCharLiteral: {
if (phase != Phase::Concrete) {
break;
}
return PerformCharLiteralArithmetic(
context, loc_id, SemIR::BuiltinFunctionKind::CharLiteralAdd,
arg_ids[1], arg_ids[0], call.type_id);
}
case SemIR::BuiltinFunctionKind::IntConvertCharLiteral: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformIntConvertCharLiteral(context, loc_id, arg_ids[0],
call.type_id);
}
// Integer conversions.
case SemIR::BuiltinFunctionKind::IntConvertChar: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformIntConvert(context, arg_ids[0], call.type_id);
}
case SemIR::BuiltinFunctionKind::IntConvert: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformIntConvert(context, arg_ids[0], call.type_id);
}
case SemIR::BuiltinFunctionKind::IntConvertChecked: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformCheckedIntConvert(context, loc_id, arg_ids[0],
call.type_id);
}
case SemIR::BuiltinFunctionKind::IntConvertFloat: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformIntToFloatConvert(context, loc_id, arg_ids[0], call.type_id,
/*require_exact=*/false);
}
case SemIR::BuiltinFunctionKind::IntConvertFloatChecked: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformIntToFloatConvert(context, loc_id, arg_ids[0], call.type_id,
/*require_exact=*/true);
}
// Unary integer -> integer operations.
case SemIR::BuiltinFunctionKind::IntSNegate:
case SemIR::BuiltinFunctionKind::IntUNegate:
case SemIR::BuiltinFunctionKind::IntComplement: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinUnaryIntOp(context, loc_id, builtin_kind,
arg_ids[0]);
}
// Homogeneous binary integer -> integer operations.
case SemIR::BuiltinFunctionKind::IntSAdd:
case SemIR::BuiltinFunctionKind::IntSSub:
case SemIR::BuiltinFunctionKind::IntSMul:
case SemIR::BuiltinFunctionKind::IntSDiv:
case SemIR::BuiltinFunctionKind::IntSMod:
case SemIR::BuiltinFunctionKind::IntUAdd:
case SemIR::BuiltinFunctionKind::IntUSub:
case SemIR::BuiltinFunctionKind::IntUMul:
case SemIR::BuiltinFunctionKind::IntUDiv:
case SemIR::BuiltinFunctionKind::IntUMod:
case SemIR::BuiltinFunctionKind::IntAnd:
case SemIR::BuiltinFunctionKind::IntOr:
case SemIR::BuiltinFunctionKind::IntXor: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinBinaryIntOp(context, loc_id, builtin_kind,
arg_ids[0], arg_ids[1]);
}
// Bit shift operations.
case SemIR::BuiltinFunctionKind::IntLeftShift:
case SemIR::BuiltinFunctionKind::IntRightShift: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinIntShiftOp(context, loc_id, builtin_kind, arg_ids[0],
arg_ids[1]);
}
// Integer comparisons.
case SemIR::BuiltinFunctionKind::IntEq:
case SemIR::BuiltinFunctionKind::IntNeq:
case SemIR::BuiltinFunctionKind::IntLess:
case SemIR::BuiltinFunctionKind::IntLessEq:
case SemIR::BuiltinFunctionKind::IntGreater:
case SemIR::BuiltinFunctionKind::IntGreaterEq: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinIntComparison(context, builtin_kind, arg_ids[0],
arg_ids[1], call.type_id);
}
// Floating-point conversions.
case SemIR::BuiltinFunctionKind::FloatConvert: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformFloatConvert(context, loc_id, arg_ids[0], call.type_id,
/*check_overflow=*/false);
}
case SemIR::BuiltinFunctionKind::FloatConvertChecked: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformFloatConvert(context, loc_id, arg_ids[0], call.type_id,
/*check_overflow=*/true);
}
case SemIR::BuiltinFunctionKind::FloatConvertInt: {
if (phase != Phase::Concrete) {
return MakeConstantResult(context, call, phase);
}
return PerformFloatToIntConvert(context, loc_id, arg_ids[0],
call.type_id);
}
// Unary float -> float operations.
case SemIR::BuiltinFunctionKind::FloatNegate: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinUnaryFloatOp(context, builtin_kind, arg_ids[0]);
}
// Binary float -> float operations.
case SemIR::BuiltinFunctionKind::FloatAdd:
case SemIR::BuiltinFunctionKind::FloatSub:
case SemIR::BuiltinFunctionKind::FloatMul:
case SemIR::BuiltinFunctionKind::FloatDiv: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinBinaryFloatOp(context, loc_id, builtin_kind,
arg_ids[0], arg_ids[1]);
}
// Float comparisons.
case SemIR::BuiltinFunctionKind::FloatEq:
case SemIR::BuiltinFunctionKind::FloatNeq:
case SemIR::BuiltinFunctionKind::FloatLess:
case SemIR::BuiltinFunctionKind::FloatLessEq:
case SemIR::BuiltinFunctionKind::FloatGreater:
case SemIR::BuiltinFunctionKind::FloatGreaterEq: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinFloatComparison(context, builtin_kind, arg_ids[0],
arg_ids[1], call.type_id);
}
// Bool comparisons.
case SemIR::BuiltinFunctionKind::BoolEq:
case SemIR::BuiltinFunctionKind::BoolNeq: {
if (phase != Phase::Concrete) {
break;
}
return PerformBuiltinBoolComparison(context, builtin_kind, arg_ids[0],
arg_ids[1], call.type_id);
}
}
return SemIR::ConstantId::NotConstant;
}
static auto TryEvalCall(EvalContext& outer_eval_context, SemIR::LocId loc_id,
const SemIR::Function& function,
SemIR::SpecificId specific_id,
SemIR::InstBlockId args_id) -> SemIR::ConstantId;
// Returns the range of parameter indexes that contain the return storage for
// this function call.
static auto GetReturnStorageParamIndexRange(EvalContext& eval_context,
const SemIR::Callee& callee)
-> std::pair<int, int> {
if (const auto* callee_function =
std::get_if<SemIR::CalleeFunction>(&callee)) {
const auto& function =
eval_context.functions().Get(callee_function->function_id);
return {function.call_param_ranges.return_begin().index,
function.call_param_ranges.return_end().index};
}
return {0, 0};
}
// Replace the `args_id` field of a call with its constant value. The return
// storage argument, if any, is instead replaced with `None`.
static auto ReplaceCallArgsFieldWithConstantValue(EvalContext& eval_context,
const SemIR::Callee& callee,
SemIR::Call* call,
Phase* phase) -> bool {
auto return_storage_param_index_range =
GetReturnStorageParamIndexRange(eval_context, callee);
auto args_id = GetConstantBlockValueIgnoringIndexRange(
eval_context, call->args_id, phase, return_storage_param_index_range);
if (!args_id.has_value() && call->args_id.has_value()) {
return false;
}
call->args_id = args_id;
return IsConstantOrError(*phase);
}
// Makes a constant for a call instruction.
static auto MakeConstantForCall(EvalContext& eval_context,
SemIR::InstId inst_id, SemIR::Call call)
-> SemIR::ConstantId {
Phase phase = Phase::Concrete;
// A call with an invalid argument list is used to represent an erroneous
// call.
//
// TODO: Use a better representation for this.
if (call.args_id == SemIR::InstBlockId::None) {
return SemIR::ErrorInst::ConstantId;
}
// If the callee is a C++ thunk, modify the `call` to directly call
// the thunk's callee.
MaybeModifyCppThunkCallForConstEval(eval_context.context(), &call);
// Find the constant value of the callee.
bool has_constant_callee = ReplaceFieldWithConstantValue(
eval_context, &call, &SemIR::Call::callee_id, &phase);
auto callee = SemIR::GetCallee(eval_context.sem_ir(), call.callee_id);
const SemIR::Function* function = nullptr;
auto builtin_kind = SemIR::BuiltinFunctionKind::None;
auto evaluation_mode = SemIR::Function::EvaluationMode::None;
if (auto* callee_function = std::get_if<SemIR::CalleeFunction>(&callee)) {
function = &eval_context.functions().Get(callee_function->function_id);
builtin_kind = function->builtin_function_kind();
evaluation_mode = function->evaluation_mode;
// Calls to builtins and to `eval` or `musteval` functions might be
// constant.
if (builtin_kind == SemIR::BuiltinFunctionKind::None &&
evaluation_mode == SemIR::Function::EvaluationMode::None) {
return SemIR::ConstantId::NotConstant;
}
} else {
// Calls to non-functions, such as calls to generic entity names, might be
// constant.
}
// Find the argument values and the return type.
bool has_constant_operands =
has_constant_callee &&
ReplaceTypeWithConstantValue(eval_context, inst_id, &call, &phase) &&
ReplaceCallArgsFieldWithConstantValue(eval_context, callee, &call,
&phase);
if (phase == Phase::UnknownDueToError) {
return SemIR::ErrorInst::ConstantId;
}
// If any operand of the call is non-constant, the call is non-constant.
// TODO: Some builtin calls might allow some operands to be non-constant.
if (!has_constant_operands) {
if (builtin_kind.IsCompTimeOnly(
eval_context.sem_ir(), eval_context.inst_blocks().Get(call.args_id),
call.type_id) ||
evaluation_mode == SemIR::Function::EvaluationMode::MustEval) {
CARBON_DIAGNOSTIC(NonConstantCallToCompTimeOnlyFunction, Error,
"non-constant call to compile-time-only function");
CARBON_DIAGNOSTIC(CompTimeOnlyFunctionHere, Note,
"compile-time-only function declared here");
const auto& function = eval_context.functions().Get(
std::get<SemIR::CalleeFunction>(callee).function_id);
eval_context.emitter()
.Build(inst_id, NonConstantCallToCompTimeOnlyFunction)
.Note(function.latest_decl_id(), CompTimeOnlyFunctionHere)
.Emit();
}
return SemIR::ConstantId::NotConstant;
}
// Handle calls to builtins.
if (builtin_kind != SemIR::BuiltinFunctionKind::None) {
return MakeConstantForBuiltinCall(
eval_context, SemIR::LocId(inst_id), call, builtin_kind,
eval_context.inst_blocks().Get(call.args_id), phase);
}
// Handle calls to `eval` and `musteval` functions.
if (evaluation_mode != SemIR::Function::EvaluationMode::None) {
// A non-concrete call to `eval` or `musteval` is a template symbolic
// constant, regardless of the phase of the arguments.
if (phase != Phase::Concrete) {
CARBON_CHECK(phase <= Phase::TemplateSymbolic);
return MakeConstantResult(eval_context.context(), call,
Phase::TemplateSymbolic);
}
// TODO: Instead of performing the call immediately, add it to a work queue
// and do it non-recursively.
return TryEvalCall(
eval_context, SemIR::LocId(inst_id), *function,
std::get<SemIR::CalleeFunction>(callee).resolved_specific_id,
call.args_id);
}
return SemIR::ConstantId::NotConstant;
}
// Given an instruction, compute its phase based on its operands.
static auto ComputeInstPhase(Context& context, SemIR::Inst inst) -> Phase {
EvalContext eval_context(&context, SemIR::LocId::None);
auto phase = GetPhase(context.constant_values(),
context.types().GetConstantId(inst.type_id()));
GetConstantValueForArg(eval_context, inst.arg0_and_kind(), &phase);
GetConstantValueForArg(eval_context, inst.arg1_and_kind(), &phase);
CARBON_CHECK(IsConstantOrError(phase));
return phase;
}
// Convert a ConstantEvalResult to a ConstantId. Factored out of
// TryEvalTypedInst to avoid repeated instantiation of common code.
static auto ConvertEvalResultToConstantId(Context& context,
ConstantEvalResult result,
SemIR::InstKind orig_inst_kind,
Phase orig_phase)
-> SemIR::ConstantId {
if (result.is_new()) {
auto is_symbolic_only =
orig_inst_kind.constant_kind() == SemIR::InstConstantKind::SymbolicOnly;
auto is_template_only =
orig_inst_kind.constant_kind() == SemIR::InstConstantKind::TemplateOnly;
auto new_phase = result.same_phase_as_inst()
? orig_phase
: ComputeInstPhase(context, result.new_inst());
CARBON_CHECK(!is_symbolic_only || new_phase > Phase::Concrete ||
result.new_inst().kind() != orig_inst_kind,
"SymbolicOnly instruction `{0}` has a concrete value",
orig_inst_kind);
CARBON_CHECK(!is_template_only || new_phase > Phase::Concrete ||
result.new_inst().kind() != orig_inst_kind,
"TemplateOnly instruction `{0}` has a concrete value",
orig_inst_kind);
if (is_template_only && new_phase < Phase::TemplateSymbolic) {
new_phase = Phase::TemplateSymbolic;
}
return MakeConstantResult(context, result.new_inst(), new_phase);
}
return result.existing();
}
// Evaluates an instruction of a known type in an evaluation context. The
// default behavior of this function depends on the constant kind of the
// instruction:
//
// - InstConstantKind::Never: returns ConstantId::NotConstant.
// - InstConstantKind::Indirect, SymbolicOnly, SymbolicOrReference,
// Conditional: evaluates all the operands of the instruction, and calls
// `EvalConstantInst` to evaluate the resulting constant instruction.
// - InstConstantKind::WheneverPossible, Always: evaluates all the operands of
// the instruction, and produces the resulting constant instruction as the
// result.
// - InstConstantKind::Unique: returns the `inst_id` as the resulting
// constant.
//
// Returns an error constant ID if any of the nested evaluations fail, and
// returns NotConstant if any of the nested evaluations is non-constant.
//
// This template is explicitly specialized for instructions that need special
// handling.
template <typename InstT>
static auto TryEvalTypedInst(EvalContext& eval_context, SemIR::InstId inst_id,
SemIR::Inst inst) -> SemIR::ConstantId {
constexpr auto ConstantKind = InstT::Kind.constant_kind();
if constexpr (ConstantKind == SemIR::InstConstantKind::Never) {
return SemIR::ConstantId::NotConstant;
} else if constexpr (ConstantKind == SemIR::InstConstantKind::AlwaysUnique) {
CARBON_CHECK(inst_id.has_value());
return SemIR::ConstantId::ForConcreteConstant(inst_id);
} else {
// Build a constant instruction by replacing each non-constant operand with
// its constant value.
Phase phase = Phase::Concrete;
if ((SemIR::Internal::HasTypeIdMember<InstT> &&
!ReplaceTypeWithConstantValue(eval_context, inst_id, &inst, &phase)) ||
!ReplaceAllFieldsWithConstantValues(eval_context, &inst, &phase)) {
if constexpr (ConstantKind == SemIR::InstConstantKind::Always) {
CARBON_FATAL("{0} should always be constant", InstT::Kind);
}
return SemIR::ConstantId::NotConstant;
}
// If any operand of the instruction has an error in it, the instruction
// itself evaluates to an error.
if (phase == Phase::UnknownDueToError) {
return SemIR::ErrorInst::ConstantId;
}
// When canonicalizing a SpecificId, we defer resolving the specific's
// declaration until here, to avoid resolving declarations from imported
// specifics. (Imported instructions are not evaluated.)
ResolveSpecificDeclForInst(eval_context, inst);
if constexpr (ConstantKind == SemIR::InstConstantKind::Always ||
ConstantKind == SemIR::InstConstantKind::WheneverPossible) {
return MakeConstantResult(eval_context.context(), inst, phase);
} else if constexpr (ConstantKind == SemIR::InstConstantKind::InstAction) {
auto result_inst_id = PerformDelayedAction(
eval_context.context(), eval_context.specific_id(),
SemIR::LocId(inst_id), inst.As<InstT>());
if (result_inst_id.has_value()) {
// The result is an instruction.
return MakeConstantResult(
eval_context.context(),
SemIR::InstValue{
.type_id = GetSingletonType(eval_context.context(),
SemIR::InstType::TypeInstId),
.inst_id = result_inst_id},
Phase::Concrete);
}
// Couldn't perform the action because it's still dependent.
return MakeConstantResult(eval_context.context(), inst,
Phase::TemplateSymbolic);
} else if constexpr (InstT::Kind.constant_needs_inst_id() !=
SemIR::InstConstantNeedsInstIdKind::No) {
CARBON_CHECK(inst_id.has_value());
return ConvertEvalResultToConstantId(
eval_context.context(),
EvalConstantInst(eval_context.context(), inst_id, inst.As<InstT>()),
InstT::Kind, phase);
} else {
return ConvertEvalResultToConstantId(
eval_context.context(),
EvalConstantInst(eval_context.context(), inst.As<InstT>()),
InstT::Kind, phase);
}
}
}
// Specialize evaluation for array indexing because we want to check the index
// expression even if the array expression is non-constant.
template <>
auto TryEvalTypedInst<SemIR::ArrayIndex>(EvalContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst inst)
-> SemIR::ConstantId {
return PerformArrayIndex(eval_context, inst.As<SemIR::ArrayIndex>());
}
// Specialize evaluation for function calls because we want to check the callee
// expression even if an argument expression is non-constant, and because we
// will eventually want to perform control flow handling here.
template <>
auto TryEvalTypedInst<SemIR::Call>(EvalContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
return MakeConstantForCall(eval_context, inst_id, inst.As<SemIR::Call>());
}
// ImportRefLoaded can have a constant value, but it's owned and maintained by
// `import_ref.cpp`, not by us.
// TODO: Rearrange how `ImportRefLoaded` instructions are created so we never
// call this.
template <>
auto TryEvalTypedInst<SemIR::ImportRefLoaded>(EvalContext& /*eval_context*/,
SemIR::InstId /*inst_id*/,
SemIR::Inst /*inst*/)
-> SemIR::ConstantId {
return SemIR::ConstantId::NotConstant;
}
// Symbolic bindings are a special case because they can reach into the eval
// context and produce a context-specific value.
template <>
auto TryEvalTypedInst<SemIR::SymbolicBinding>(EvalContext& eval_context,
SemIR::InstId inst_id,
SemIR::Inst inst)
-> SemIR::ConstantId {
auto bind = inst.As<SemIR::SymbolicBinding>();
// If we know which specific we're evaluating within and this is an argument
// of that specific, its constant value is the corresponding argument value.
if (auto value = eval_context.GetCompileTimeBindValue(bind);
value.has_value()) {
return value;
}
// The constant form of a symbolic binding is an idealized form of the
// original, with no equivalent value.
Phase phase = Phase::Concrete;
bind.value_id = SemIR::InstId::None;
if (!ReplaceTypeWithConstantValue(eval_context, inst_id, &bind, &phase) ||
!ReplaceFieldWithConstantValue(eval_context, &bind,
&SemIR::SymbolicBinding::entity_name_id,
&phase)) {
return SemIR::ConstantId::NotConstant;
}
// This correctly handles `Phase::UnknownDueToError`.
return MakeConstantResult(eval_context.context(), bind, phase);
}
template <>
auto TryEvalTypedInst<SemIR::Temporary>(EvalContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
auto temporary = inst.As<SemIR::Temporary>();
temporary.storage_id = SemIR::InstId::None;
Phase phase = Phase::Concrete;
if (!ReplaceTypeWithConstantValue(eval_context, inst_id, &temporary,
&phase) ||
!ReplaceFieldWithConstantValue(eval_context, &temporary,
&SemIR::Temporary::init_id, &phase)) {
return SemIR::ConstantId::NotConstant;
}
return MakeConstantResult(eval_context.context(), temporary, phase);
}
static auto AddRequirementBase(Context& context,
SemIR::RequirementBaseFacetType base,
SemIR::DeclaredFacetType* declared_facet_type,
Phase* phase) -> void {
auto base_type_inst_id =
context.constant_values().GetConstantTypeInstId(base.base_type_inst_id);
if (base_type_inst_id == SemIR::ErrorInst::TypeInstId) {
*phase = Phase::UnknownDueToError;
return;
}
if (auto base_facet_type =
context.insts().TryGetAs<SemIR::FacetType>(base_type_inst_id)) {
const auto& base_declared_facet_type = context.declared_facet_types().Get(
base_facet_type->declared_facet_type_id);
declared_facet_type->extend_constraints.append(
base_declared_facet_type.extend_constraints);
declared_facet_type->extend_named_constraints.append(
base_declared_facet_type.extend_named_constraints);
declared_facet_type->self_impls_constraints.append(
base_declared_facet_type.self_impls_constraints);
declared_facet_type->self_impls_named_constraints.append(
base_declared_facet_type.self_impls_named_constraints);
declared_facet_type->type_impls_interfaces.append(
base_declared_facet_type.type_impls_interfaces);
declared_facet_type->type_impls_named_constraints.append(
base_declared_facet_type.type_impls_named_constraints);
declared_facet_type->rewrite_constraints.append(
base_declared_facet_type.rewrite_constraints);
declared_facet_type->other_requirements |=
base_declared_facet_type.other_requirements;
}
}
static auto AddRequirementRewrite(Context& context,
SemIR::RequirementRewrite rewrite,
SemIR::DeclaredFacetType* declared_facet_type,
Phase* phase) -> void {
auto lhs_id = context.constant_values().GetConstantInstId(rewrite.lhs_id);
auto rhs_id = context.constant_values().GetConstantInstId(rewrite.rhs_id);
if (lhs_id == SemIR::ErrorInst::InstId ||
rhs_id == SemIR::ErrorInst::InstId) {
*phase = Phase::UnknownDueToError;
return;
}
if (!rhs_id.has_value()) {
// The RHS may be an arbitrary expression, which means it could have a
// runtime value, which we reject since we can't evaluate that.
DiagnoseNonConstantValue(context, SemIR::LocId(rewrite.rhs_id));
*phase = Phase::UnknownDueToError;
return;
}
// The DeclaredFacetType must hold canonical IDs for constant comparison, yet
// here we must insert the non-canonical IDs:
// * Rewrite constraints are resolved once the DeclaredFacetType is fully
// constructed in order to produce the constant value of the facet type.
// That resolution step needs the non-canonical insts to do its job
// correctly. For instance, the LHS may be a `ImplWitnessAccessSubstituted`
// instruction which preserves which element in the witness is being
// assigned to but evaluates to the RHS of some other rewrite. So the
// constant value would be incorrect to use.
// * We use the id of the non-canonical RHS instruction as a hint to order
// diagnostics in the resolution of rewrites, so that they can usually refer
// to the rewrites in the same order as they are written in the code. Using
// the constant value of the RHS reorders the diagnostics in a worse way.
// * The final step of constructing the facet type from the WhereExpr
// canonicalizes all the instructions, so we don't need to store canonical
// values here. We only need to use canonical values if we need to observe
// the constant value, such as to determine in the RHS has a runtime value
// above.
declared_facet_type->rewrite_constraints.push_back(
{.lhs_id = rewrite.lhs_id, .rhs_id = rewrite.rhs_id});
}
static auto AddRequirementImpls(Context& context, SemIR::RequirementImpls impls,
SemIR::DeclaredFacetType* declared_facet_type,
Phase* phase) -> void {
auto lhs_id = context.constant_values().GetConstantInstId(impls.lhs_id);
auto rhs_id = context.constant_values().GetConstantInstId(impls.rhs_id);
if (lhs_id == SemIR::ErrorInst::InstId ||
rhs_id == SemIR::ErrorInst::InstId) {
*phase = Phase::UnknownDueToError;
return;
}
if (rhs_id == SemIR::TypeType::TypeInstId) {
// `<type> impls type` -> nothing to do.
return;
}
auto facet_type = context.insts().GetAs<SemIR::FacetType>(rhs_id);
const auto& rhs =
context.declared_facet_types().Get(facet_type.declared_facet_type_id);
// We forbid `where` on the RHS of another `where`, so non-extend constraints
// can't be part of a facet type on the RHS of `where ... impls`.
CARBON_CHECK(rhs.self_impls_constraints.empty());
CARBON_CHECK(rhs.self_impls_named_constraints.empty());
CARBON_CHECK(rhs.type_impls_interfaces.empty());
CARBON_CHECK(rhs.type_impls_named_constraints.empty());
CARBON_CHECK(rhs.rewrite_constraints.empty());
CARBON_CHECK(!rhs.other_requirements);
if (IsPeriodSelf(context, lhs_id)) {
// A facet type with `.Self impls <RHS facet type>`. Whatever the RHS facet
// type constrains for `.Self` gets forwarded to the output facet type to
// also constrain `.Self`. Nothing on the RHS of `impls` can extend the
// resulting facet type.
llvm::append_range(declared_facet_type->self_impls_constraints,
rhs.extend_constraints);
llvm::append_range(declared_facet_type->self_impls_named_constraints,
rhs.extend_named_constraints);
} else {
auto lhs_facet_or_type = GetCanonicalFacetOrTypeValue(context, lhs_id);
auto extends_interface = [=](SemIR::SpecificInterface si)
-> SemIR::DeclaredFacetType::TypeImplsInterface {
return {lhs_facet_or_type, si};
};
auto extends_constraint = [=](SemIR::SpecificNamedConstraint sc)
-> SemIR::DeclaredFacetType::TypeImplsNamedConstraint {
return {lhs_facet_or_type, sc};
};
// Extend constraints are copied over without replacing anything, but are
// converted to type impls constraints so they apply to the LHS type.
llvm::append_range(
declared_facet_type->type_impls_interfaces,
llvm::map_range(rhs.extend_constraints, extends_interface));
llvm::append_range(
declared_facet_type->type_impls_named_constraints,
llvm::map_range(rhs.extend_named_constraints, extends_constraint));
}
}
static auto AddRequirementEquivalent(
Context& context, SemIR::RequirementEquivalent equiv,
SemIR::DeclaredFacetType* declared_facet_type, Phase* phase) -> void {
auto lhs_id = context.constant_values().GetConstantInstId(equiv.lhs_id);
auto rhs_id = context.constant_values().GetConstantInstId(equiv.rhs_id);
if (lhs_id == SemIR::ErrorInst::InstId ||
rhs_id == SemIR::ErrorInst::InstId) {
*phase = Phase::UnknownDueToError;
return;
}
// TODO: Handle equality requirements.
declared_facet_type->other_requirements = true;
}
// Add the constraints from the WhereExpr instruction into a DeclaredFacetType
// in order to construct a FacetType constant value.
//
// TODO: Convert this to an EvalConstantInst function. This will require
// providing a `GetConstantValue` overload for a requirement block.
template <>
auto TryEvalTypedInst<SemIR::WhereExpr>(EvalContext& eval_context,
SemIR::InstId where_inst_id,
SemIR::Inst inst) -> SemIR::ConstantId {
auto typed_inst = inst.As<SemIR::WhereExpr>();
Phase phase = Phase::Concrete;
SemIR::DeclaredFacetType declared_facet_type;
if (inst.type_id() == SemIR::ErrorInst::TypeId) {
return SemIR::ErrorInst::ConstantId;
}
// Note that these requirement instructions don't have a constant value. That
// means we have to look for errors inside them, we can't just look to see if
// their constant value is an error.
for (auto inst_id :
eval_context.inst_blocks().GetOrEmpty(typed_inst.requirements_id)) {
if (phase == Phase::UnknownDueToError) {
// Abandon ship to save work once we've encountered an error.
return SemIR::ErrorInst::ConstantId;
}
auto inst = eval_context.insts().Get(inst_id);
CARBON_KIND_SWITCH(inst) {
case CARBON_KIND(SemIR::RequirementBaseFacetType base): {
AddRequirementBase(eval_context.context(), base, &declared_facet_type,
&phase);
break;
}
case CARBON_KIND(SemIR::RequirementRewrite rewrite): {
AddRequirementRewrite(eval_context.context(), rewrite,
&declared_facet_type, &phase);
break;
}
case CARBON_KIND(SemIR::RequirementImpls impls): {
AddRequirementImpls(eval_context.context(), impls, &declared_facet_type,
&phase);
break;
}
case CARBON_KIND(SemIR::RequirementEquivalent equiv): {
AddRequirementEquivalent(eval_context.context(), equiv,
&declared_facet_type, &phase);
break;
}
default:
CARBON_FATAL("unexpected inst {0} in WhereExpr requirements block",
inst);
}
}
auto const_declared_facet_type = GetConstantDeclaredFacetType(
eval_context, SemIR::LocId(where_inst_id), declared_facet_type, &phase);
return MakeFacetTypeResult(eval_context.context(), const_declared_facet_type,
phase);
}
// Implementation for `TryEvalInst`, wrapping `Context` with `EvalContext`.
static auto TryEvalInstInContext(EvalContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
using EvalInstFn =
auto(EvalContext & eval_context, SemIR::InstId inst_id, SemIR::Inst inst)
->SemIR::ConstantId;
static constexpr EvalInstFn* EvalInstFns[] = {
#define CARBON_SEM_IR_INST_KIND(Kind) &TryEvalTypedInst<SemIR::Kind>,
#include "toolchain/sem_ir/inst_kind.def"
};
[[clang::musttail]] return EvalInstFns[inst.kind().AsInt()](eval_context,
inst_id, inst);
}
auto TryEvalInstUnsafe(Context& context, SemIR::InstId inst_id,
SemIR::Inst inst) -> SemIR::ConstantId {
EvalContext eval_context(&context, SemIR::LocId(inst_id));
return TryEvalInstInContext(eval_context, inst_id, inst);
}
auto TryEvalBlockForSpecific(Context& context, SemIR::LocId loc_id,
SemIR::SpecificId specific_id,
SemIR::GenericInstIndex::Region region) -> void {
auto generic_id = context.specifics().Get(specific_id).generic_id;
auto eval_block_id = context.generics().Get(generic_id).GetEvalBlock(region);
auto eval_block = context.inst_blocks().Get(eval_block_id);
// Allocate the value block and store it back onto the specific, so that our
// in-progress results are visible.
auto& specific = context.specifics().Get(specific_id);
auto value_block_id =
context.inst_blocks().AddUninitialized(eval_block.size());
auto value_block = context.inst_blocks().GetMutable(value_block_id);
for (auto& inst_id : value_block) {
inst_id = SemIR::InstId::None;
}
specific.SetValueBlock(region, value_block_id);
EvalContext eval_context(&context, loc_id, specific_id);
Diagnostics::ContextScope diagnostic_context(
&context.emitter(), [&](auto& builder) {
CARBON_DIAGNOSTIC(ResolvingSpecificHere, SoftContext,
"unable to monomorphize specific {0}",
SemIR::SpecificId);
builder.Context(loc_id, ResolvingSpecificHere, specific_id);
});
for (auto [i, inst_id, result_id] :
llvm::enumerate(eval_block, value_block)) {
auto const_id = TryEvalInstInContext(eval_context, inst_id,
context.insts().Get(inst_id));
CARBON_CHECK(const_id.has_value(), "Failed to evaluate {0} in eval block",
context.insts().Get(inst_id));
if (const_id == SemIR::ErrorInst::ConstantId) {
specific.SetHasError(region);
}
result_id = context.constant_values().GetInstId(const_id);
}
}
// Information about the function call we are currently executing. Unlike
// evaluation, execution sequentially interprets instructions, and can handle
// control flow and (eventually) side effects and mutable state.
class FunctionExecContext : public EvalContext {
public:
// A block argument passed to `BranchWithArg`.
struct BlockArgValue {
SemIR::InstBlockId block_id = SemIR::InstBlockId::None;
SemIR::ConstantId arg_id = SemIR::ConstantId::None;
};
FunctionExecContext(Context* context, SemIR::LocId loc_id,
SemIR::SpecificId specific_id,
Map<SemIR::InstId, SemIR::ConstantId>* locals,
SemIR::InstBlockId args_id)
: EvalContext(context, loc_id, specific_id,
LocalEvalInfo{.locals = locals}),
args_(context->inst_blocks().Get(args_id)) {}
// Returns the argument values supplied in the call to the function.
auto args() const -> llvm::ArrayRef<SemIR::InstId> { return args_; }
using EvalContext::locals;
// Branch control flow to the given block. This replaces the innermost block
// in the block stack, but doesn't affect any enclosing blocks.
auto BranchTo(SemIR::InstBlockId block_id) -> void {
blocks_.back() = inst_blocks().Get(block_id);
}
// Push a new block to be executed immediately. After the block finishes,
// control will resume after the current instruction.
auto PushBlock(SemIR::InstBlockId block_id) -> void {
blocks_.push_back(inst_blocks().Get(block_id));
}
// Pops and returns the next instruction to be executed.
auto PopNextInstId() -> SemIR::InstId {
while (blocks_.back().empty()) {
blocks_.pop_back();
CARBON_CHECK(!blocks_.empty(), "Fell off end of function");
}
return blocks_.back().consume_front();
}
// Sets the most recent block argument value provided by a `BranchWithArg`.
// This can later be retrieved by a `BlockArg`.
auto SetCurrentBlockArgValue(BlockArgValue arg) -> void {
current_block_arg_value_ = arg;
}
// Returns the most recent block argument value provided by a `BranchWithArg`.
auto current_block_arg_value() const -> BlockArgValue {
return current_block_arg_value_;
}
private:
// The stack of code blocks that we are currently evaluating. This is kept as
// a stack so that we can schedule the function body to execute after the decl
// block and so that we can handle `SpliceBlock`s. When the innermost block is
// complete, it will be popped and the next outer block will execute.
llvm::SmallVector<llvm::ArrayRef<SemIR::InstId>, 4> blocks_;
// The arguments in the function call.
llvm::ArrayRef<SemIR::InstId> args_;
// The block argument provided by the most recently executed `BranchWithArg`.
// We assume that we only need to track one of these, as the branch target
// will invoke `BlockArg` before the next `BranchWithArg` happens. We will
// need to track more than one of these if that ever changes.
BlockArgValue current_block_arg_value_;
};
// Handles the result of executing an instruction in a function. Returns an
// error the result is not a constant, and otherwise updates the locals map to
// track the result as an input to later evaluations in this function and
// returns None.
static auto HandleExecResult(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::ConstantId const_id)
-> SemIR::ConstantId {
if (const_id == SemIR::ErrorInst::ConstantId) {
return const_id;
}
if (!const_id.has_value() || !const_id.is_constant()) {
DiagnoseNonConstantValue(eval_context.context(),
eval_context.GetDiagnosticLoc(inst_id));
return SemIR::ErrorInst::ConstantId;
}
eval_context.locals().Update(inst_id, const_id);
return SemIR::ConstantId::None;
}
// Executes an instruction for TryEvalCall. By default, performs normal
// evaluation of the instruction within a context that supplies the values
// produced by executing prior instructions in this function execution. This is
// specialized for instructions that have special handling in function
// execution, such as those that access parameters or perform flow control. If
// execution should continue, returns `SemIR::ConstantId::None`, otherwise
// returns the result to produce for the enclosing function call, which should
// be either the returned value or an error.
template <typename InstT>
static auto TryExecTypedInst(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
if constexpr (InstT::Kind.expr_category() ==
SemIR::InstExprCategory(SemIR::ExprCategory::NotExpr)) {
// Instructions in this category are assumed to not have a runtime effect.
// This includes some kinds of declaration.
return SemIR::ConstantId::None;
}
if constexpr (InstT::Kind.constant_kind() != SemIR::InstConstantKind::Never) {
if (eval_context.constant_values().Get(inst_id).is_concrete()) {
// Instruction has a concrete constant value that doesn't depend on the
// context. We don't need to evaluate it again.
return SemIR::ConstantId::None;
}
}
// Evaluate the instruction in the current context.
auto const_id = TryEvalTypedInst<InstT>(eval_context, inst_id, inst);
return HandleExecResult(eval_context, inst_id, const_id);
}
template <>
auto TryExecTypedInst<SemIR::BlockArg>(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
auto block_arg = inst.As<SemIR::BlockArg>();
CARBON_CHECK(
block_arg.block_id == eval_context.current_block_arg_value().block_id,
"BlockArg does not refer to most recent BranchWithArg");
eval_context.locals().Update(inst_id,
eval_context.current_block_arg_value().arg_id);
return SemIR::ConstantId::None;
}
template <>
auto TryExecTypedInst<SemIR::Branch>(FunctionExecContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst inst) -> SemIR::ConstantId {
auto branch = inst.As<SemIR::Branch>();
eval_context.BranchTo(branch.target_id);
return SemIR::ConstantId::None;
}
template <>
auto TryExecTypedInst<SemIR::BranchIf>(FunctionExecContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst inst) -> SemIR::ConstantId {
auto branch_if = inst.As<SemIR::BranchIf>();
auto cond_id = CheckConcreteValue(eval_context, branch_if.cond_id);
if (cond_id == SemIR::ErrorInst::InstId) {
return SemIR::ErrorInst::ConstantId;
}
auto cond = eval_context.insts().GetAs<SemIR::BoolLiteral>(cond_id);
if (cond.value == SemIR::BoolValue::True) {
eval_context.BranchTo(branch_if.target_id);
}
return SemIR::ConstantId::None;
}
template <>
auto TryExecTypedInst<SemIR::BranchWithArg>(FunctionExecContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst inst)
-> SemIR::ConstantId {
auto branch = inst.As<SemIR::BranchWithArg>();
eval_context.SetCurrentBlockArgValue(
{.block_id = branch.target_id,
.arg_id = eval_context.GetConstantValue(branch.arg_id)});
eval_context.BranchTo(branch.target_id);
return SemIR::ConstantId::None;
}
template <>
auto TryExecTypedInst<SemIR::Return>(FunctionExecContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst /*inst*/)
-> SemIR::ConstantId {
return MakeEmptyTupleResult(eval_context);
}
template <>
auto TryExecTypedInst<SemIR::ReturnExpr>(FunctionExecContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst inst)
-> SemIR::ConstantId {
auto return_expr = inst.As<SemIR::ReturnExpr>();
return eval_context.GetConstantValue(return_expr.expr_id);
}
template <>
auto TryExecTypedInst<SemIR::ReturnSlot>(FunctionExecContext& eval_context,
SemIR::InstId inst_id,
SemIR::Inst inst)
-> SemIR::ConstantId {
auto return_slot = inst.As<SemIR::ReturnSlot>();
// In the case where the function's return type is not in-place, the return
// slot will refer to an out parameter that doesn't have an argument. In that
// case, we don't have a constant value for storage_id. To handle this, copy
// the value directly from the locals map rather than using GetConstantValue.
//
// TODO: Remove this and use a normal call to `GetConstantValue` if we stop
// adding out parameters with no corresponding argument.
eval_context.locals().Insert(
inst_id, eval_context.locals().Lookup(return_slot.storage_id).value());
return SemIR::ConstantId::None;
}
template <>
auto TryExecTypedInst<SemIR::SpliceBlock>(FunctionExecContext& eval_context,
SemIR::InstId /*inst_id*/,
SemIR::Inst inst)
-> SemIR::ConstantId {
auto splice_block = inst.As<SemIR::SpliceBlock>();
eval_context.PushBlock(splice_block.block_id);
// TODO: Copy the values from the result_id instruction to the result of
// the splice_block instruction once the spliced block finishes.
return SemIR::ConstantId::None;
}
// Executes the introduction of a parameter into the local scope. Copies the
// argument supplied by the caller for the parameter into the locals map.
static auto TryExecTypedParam(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
auto param = inst.As<SemIR::AnyParam>();
CARBON_CHECK(static_cast<size_t>(param.index.index) <
eval_context.args().size());
eval_context.locals().Insert(inst_id,
eval_context.constant_values().Get(
eval_context.args()[param.index.index]));
return SemIR::ConstantId::None;
}
template <>
auto TryExecTypedInst<SemIR::OutParam>(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
auto param = inst.As<SemIR::OutParam>();
if (static_cast<size_t>(param.index.index) >= eval_context.args().size()) {
// For return values that have a copy initializing representation, the SemIR
// has an OutParam with an index that has no corresponding argument. In that
// case, we do not have a constant value for the parameter, but this doesn't
// prevent the call from being constant.
//
// TODO: Remove this once we stop adding out parameters with no
// corresponding argument.
eval_context.locals().Insert(inst_id, SemIR::ConstantId::None);
return SemIR::ConstantId::None;
}
if (!eval_context.args()[param.index.index].has_value()) {
// The argument will be `None` for an index corresponding to a return
// storage argument for return values that have an in-place initializing
// representation. Produce an opaque "out parameter" variable for now, so
// that references to it can still successfully evaluate.
//
// TODO: Create and track mutable storage for the return value here. This is
// necessary to support things like `returned var`.
eval_context.locals().Insert(
inst_id,
MakeConstantResult(
eval_context.context(),
SemIR::VarStorage{.type_id = inst.type_id(),
.pattern_id = SemIR::AbsoluteInstId::None},
Phase::Concrete));
return SemIR::ConstantId::None;
}
return TryExecTypedParam(eval_context, inst_id, inst);
}
template <>
auto TryExecTypedInst<SemIR::RefParam>(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
return TryExecTypedParam(eval_context, inst_id, inst);
}
template <>
auto TryExecTypedInst<SemIR::ValueParam>(FunctionExecContext& eval_context,
SemIR::InstId inst_id,
SemIR::Inst inst)
-> SemIR::ConstantId {
return TryExecTypedParam(eval_context, inst_id, inst);
}
template <>
auto TryExecTypedInst<SemIR::WrapperBinding>(FunctionExecContext& eval_context,
SemIR::InstId inst_id,
SemIR::Inst inst)
-> SemIR::ConstantId {
auto value_binding = inst.As<SemIR::WrapperBinding>();
auto local_value_id = eval_context.GetConstantValue(value_binding.value_id);
eval_context.locals().Insert(inst_id, local_value_id);
return SemIR::ConstantId::None;
}
static auto TryExecInst(FunctionExecContext& eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
-> SemIR::ConstantId {
using ExecInstFn = auto(FunctionExecContext & eval_context,
SemIR::InstId inst_id, SemIR::Inst inst)
->SemIR::ConstantId;
static constexpr ExecInstFn* ExecInstFns[] = {
#define CARBON_SEM_IR_INST_KIND(Kind) &TryExecTypedInst<SemIR::Kind>,
#include "toolchain/sem_ir/inst_kind.def"
};
[[clang::musttail]] return ExecInstFns[inst.kind().AsInt()](eval_context,
inst_id, inst);
}
// Evaluates a call to an `eval` or `musteval` function by executing the
// function body.
static auto TryEvalCall(EvalContext& outer_eval_context, SemIR::LocId loc_id,
const SemIR::Function& function,
SemIR::SpecificId specific_id,
SemIR::InstBlockId args_id) -> SemIR::ConstantId {
const auto* clang_decl = outer_eval_context.sem_ir().clang_decls().Lookup(
function.first_decl_id());
if (clang_decl && clang_decl->is_imported) {
return EvalCppCall(outer_eval_context.context(), loc_id, *clang_decl,
args_id);
} else if (function.body_block_ids.empty()) {
// TODO: Diagnose this.
return SemIR::ConstantId::NotConstant;
}
if (specific_id.has_value()) {
ResolveSpecificDefinition(outer_eval_context.context(), loc_id,
specific_id);
}
// TODO: Consider tracking the lowest and highest inst_id in the function and
// using an array instead of a map. We would still need a map for instantiated
// portions of a function template.
Map<SemIR::InstId, SemIR::ConstantId> locals;
FunctionExecContext eval_context(&outer_eval_context.context(), loc_id,
specific_id, &locals, args_id);
Diagnostics::AnnotationScope annotate_diagnostics(
&eval_context.emitter(), [&](auto& builder) {
CARBON_DIAGNOSTIC(InCallToEvalFn, Note, "in call to {0} here",
SemIR::NameId);
builder.Note(loc_id, InCallToEvalFn, function.name_id);
});
// Execute the function decl block followed by the body.
eval_context.PushBlock(function.body_block_ids.front());
eval_context.PushBlock(eval_context.insts()
.GetAs<SemIR::FunctionDecl>(function.definition_id)
.decl_block_id);
// Execute the blocks. This is mostly expression evaluation, with special
// handling for control flow and parameters.
while (true) {
auto inst_id = eval_context.PopNextInstId();
auto inst = eval_context.context().insts().Get(inst_id);
if (auto result = TryExecInst(eval_context, inst_id, inst);
result.has_value()) {
return result;
}
}
}
} // namespace Carbon::Check