mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 20:18:11 +00:00
Add support for deferring initialization as a template action, and performing the deferred initialization during template instantiation. This is substantially more complex than other conversion actions, for two primary reasons: * The initializer in the generic may have storage arguments as inputs. We model an initializing expression as having a "slot" where initialization writes the location that should be initialized by that initializing expression, and that needs to be an output of the initialization action. * Initialization from a tuple or struct literal needs to recurse into that literal, and the literal will have been spelled in the generic, meaning we don't have an `InstId` that can be used to name the specific version of the initializer as input for nested conversions. These issues are addressed by introducing two new features to the action machinery: In addition to `InstAction`, we now have `MultiInstAction`, which is an action that produces a tuple of instruction values instead of a single instruction value. Initialization actions produce one instruction for the final result, which is spliced at the point of initialization, plus one instruction for each storage argument, which are spliced into the storage argument slots in the original generic. During initialization, if we find one of those splices in the storage argument of an initializing expression, we return the new storage argument back to the initialization action to be included in the specific, instead of overwriting the storage argument in the generic. Actions whose `PerforrmAction` takes a `SpecificId` as input no longer perform automatic refinement of their operands to specific instructions. Instead, the action is given control over when and where it performs that refinement. In `InitializeAction`, we use this freedom to form a `SpecificInst` for the initializer in the primary output block, and form a `SpecificInst` for the target in the target block. When detecting whether we are initializing from a tuple or struct literal, we step over the `SpecificInst` and track its `SpecificId`, and if necessary create a new `SpecificInst` wrapping the sub-initializer when we recurse into the nested element conversion. Assisted-by: Claude and Gemini via Antigravity
277 lines
13 KiB
C++
277 lines
13 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_ACTION_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_ACTION_H_
|
|
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/check/inst.h"
|
|
#include "toolchain/check/pending_block.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
#include "toolchain/sem_ir/inst_kind.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
namespace Internal {
|
|
// Computes the `SpecificId` parameters to use for PerformAction for InstT.
|
|
template <typename InstT>
|
|
using SpecificParamsForPerformAction =
|
|
std::conditional_t<InstT::Kind.action_needs_specific_id(),
|
|
auto(SemIR::SpecificId specific_id)->void, auto()->void>;
|
|
|
|
template <typename InstT>
|
|
// Computes the return type to use for PerformAction for InstT.
|
|
using ReturnTypeForPerformAction =
|
|
std::conditional_t<InstT::Kind.constant_kind() ==
|
|
SemIR::InstConstantKind::MultiInstAction,
|
|
llvm::SmallVector<SemIR::InstId>, SemIR::InstId>;
|
|
|
|
// Computes the function type to use for PerformAction for InstT.
|
|
template <typename InstT,
|
|
typename SpecificParams = SpecificParamsForPerformAction<InstT>>
|
|
struct FunctionTypeForPerformActionImpl {
|
|
// By default, no PerformAction function.
|
|
using Type = auto() -> void;
|
|
};
|
|
template <typename InstT, typename... SpecificParams>
|
|
requires(InstT::Kind.is_action())
|
|
struct FunctionTypeForPerformActionImpl<InstT, auto(SpecificParams...)->void> {
|
|
using Type = auto(Context& context, SpecificParams..., SemIR::LocId loc_id,
|
|
InstT inst) -> ReturnTypeForPerformAction<InstT>;
|
|
};
|
|
template <typename InstT>
|
|
using FunctionTypeForPerformAction =
|
|
FunctionTypeForPerformActionImpl<InstT>::Type;
|
|
} // namespace Internal
|
|
|
|
// Explicitly delete the overload generated for non-action instructions. These
|
|
// all produce the same signature, so we only need to delete it once.
|
|
auto PerformAction() -> void = delete;
|
|
|
|
// Performs an action. Each PerformAction implementation lives with the code
|
|
// that creates and defines the action. For an instruction whose constant kind
|
|
// is InstAction, an overload should be provided with the signature:
|
|
//
|
|
// auto PerformAction(Context& context, SemIR::LocId loc_id, InstT inst)
|
|
// -> SemIR::InstId;
|
|
//
|
|
// or if action_needs_specific_id = true is specified when defining the
|
|
// instruction kind, the signature:
|
|
//
|
|
// auto PerformAction(Context& context, SemIR::SpecificId specific_id,
|
|
// SemIR::LocId loc_id, InstT inst)
|
|
// -> SemIR::InstId;
|
|
//
|
|
// that returns the value that should be used as the result of evaluating the
|
|
// instructions produced by the action. Any instructions generated during
|
|
// `PerformAction` will be spliced into the code at the point where the action
|
|
// was created.
|
|
//
|
|
// For an instruction whose constant kind is MultiInstAction, the overload is
|
|
// the same but should return a `SmallVector<SemIR::InstId>` instead, providing
|
|
// multiple instructions to be spliced into different places in the generic;
|
|
// these instructions become the elements of the tuple of instruction values
|
|
// produced by the action. The first element is assumed to be spliced at the
|
|
// current location, and is treated specially: the instruction value in the
|
|
// result of the action will be a splice block containing any instructions
|
|
// generated by `PerformAction`, and the result value of that splice block is
|
|
// the first element in the returned vector.
|
|
#define CARBON_SEM_IR_INST_KIND(Name) \
|
|
Internal::FunctionTypeForPerformAction<SemIR::Name> PerformAction;
|
|
#include "toolchain/sem_ir/inst_kind.def"
|
|
|
|
// Determines whether the given action can be performed immediately (i.e.
|
|
// whether it is non-template-dependent).
|
|
auto ActionIsPerformable(Context& context, SemIR::Inst action_inst,
|
|
SemIR::SpecificId specific_id) -> bool;
|
|
|
|
// Returns the constant-dependence of `inst_id` (i.e. the maximum of the
|
|
// constant-dependences of its type and its value).
|
|
auto OperandDependence(Context& context, SemIR::InstId inst_id)
|
|
-> SemIR::ConstantDependence;
|
|
auto OperandDependence(Context& context, SemIR::MetaInstId inst_id)
|
|
-> SemIR::ConstantDependence;
|
|
auto OperandDependence(Context& context, SemIR::TypeInstId inst_id)
|
|
-> SemIR::ConstantDependence;
|
|
|
|
// Returns the constant-dependence of `type_id` (i.e. the constant-dependence
|
|
// of the corresponding type constant).
|
|
auto OperandDependence(Context& context, SemIR::TypeId type_id)
|
|
-> SemIR::ConstantDependence;
|
|
|
|
// Adds an instruction to the current block to splice in the given instruction
|
|
// value. `result_type_inst_id` specifies the type of the instruction, if known.
|
|
auto AddSpliceInst(Context& context, SemIR::InstId inst_value_id,
|
|
SemIR::TypeInstId result_type_inst_id) -> SemIR::InstId;
|
|
|
|
// Returns an instruction with the same meaning as `inst_id`, which is an
|
|
// instruction within a generic, but with the type and constant value that it
|
|
// has within `specific_id`. If `inst_id` is not template-dependent within the
|
|
// generic, it is returned unchanged; otherwise a `SpecificInst` is added to the
|
|
// current block.
|
|
auto AddSpecificInst(Context& context, SemIR::InstId inst_id,
|
|
SemIR::SpecificId specific_id) -> SemIR::InstId;
|
|
|
|
// Like the above, but any `SpecificInst` is added to `block` instead of to the
|
|
// current block.
|
|
auto AddSpecificInstToPendingBlock(PendingBlock& block, SemIR::InstId inst_id,
|
|
SemIR::SpecificId specific_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Adds an action instruction to the eval block to perform a dependent action.
|
|
// The result is not spliced into the current block. This should be used when
|
|
// the action instruction produces multiple instruction values, so its result
|
|
// cannot be spliced directly.
|
|
auto AddDependentActionInst(Context& context, SemIR::LocIdAndInst action)
|
|
-> SemIR::InstId;
|
|
|
|
// Convenience wrapper for `AddDependentActionInst`.
|
|
template <typename InstT, typename LocT>
|
|
auto AddDependentActionInst(Context& context, LocT loc, InstT inst)
|
|
-> SemIR::InstId {
|
|
return AddDependentActionInst(context, SemIR::LocIdAndInst(loc, inst));
|
|
}
|
|
|
|
// Adds an instruction to the current block to splice in the result of
|
|
// performing a dependent action.
|
|
auto AddDependentActionSplice(Context& context, SemIR::LocIdAndInst action,
|
|
SemIR::TypeInstId result_type_inst_id)
|
|
-> SemIR::InstId;
|
|
|
|
// Convenience wrapper for `AddDependentActionSplice`.
|
|
template <typename InstT, typename LocT>
|
|
auto AddDependentActionSplice(Context& context, LocT loc, InstT inst,
|
|
SemIR::TypeInstId result_type_inst_id)
|
|
-> SemIR::InstId {
|
|
return AddDependentActionSplice(context, SemIR::LocIdAndInst(loc, inst),
|
|
result_type_inst_id);
|
|
}
|
|
|
|
// Handles a new action if necessary. If the action is not dependent, returns
|
|
// InstId::None. Otherwise, adds the action to the enclosing template's eval
|
|
// block and creates an instruction to splice in the result of the action.
|
|
// `result_type_inst_id` is the type of inst produced by the action. If not
|
|
// known, it can be set to `None`, and a `TypeOfInst` instruction will be added
|
|
// to act as the type of the splice.
|
|
template <typename ActionT, typename LocIdT>
|
|
auto AddActionSpliceIfDependent(Context& context, LocIdT loc_id,
|
|
SemIR::TypeInstId expected_result_type_inst_id,
|
|
ActionT action_inst) -> SemIR::InstId {
|
|
CARBON_CHECK(action_inst.type_id == SemIR::InstType::TypeId);
|
|
if (ActionIsPerformable(context, action_inst, SemIR::SpecificId::None)) {
|
|
return SemIR::InstId::None;
|
|
}
|
|
return AddDependentActionSplice(context,
|
|
SemIR::LocIdAndInst::RuntimeVerified(
|
|
context.sem_ir(), loc_id, action_inst),
|
|
expected_result_type_inst_id);
|
|
}
|
|
|
|
// Handles a new action. If the action is not dependent, it is performed
|
|
// immediately. Otherwise, adds the action to the enclosing template's eval
|
|
// block and creates an instruction to splice in the result of the action.
|
|
// `result_type_inst_id` is the type of inst produced by the action. If not
|
|
// known, it can be set to `None`, and a `TypeOfInst` instruction will be added
|
|
// to act as the type of the splice.
|
|
template <typename ActionT, typename LocIdT>
|
|
// TODO: There is a cycle through pattern matching with an action.
|
|
// NOLINTNEXTLINE(misc-no-recursion)
|
|
auto HandleAction(Context& context, LocIdT loc_id,
|
|
SemIR::TypeInstId expected_result_type_inst_id,
|
|
ActionT action_inst) -> SemIR::InstId {
|
|
if (auto splice_inst_id = AddActionSpliceIfDependent(
|
|
context, loc_id, expected_result_type_inst_id, action_inst);
|
|
splice_inst_id.has_value()) {
|
|
return splice_inst_id;
|
|
}
|
|
|
|
auto expected_result_type_id =
|
|
expected_result_type_inst_id.has_value()
|
|
? context.types().GetTypeIdForTypeInstId(expected_result_type_inst_id)
|
|
: SemIR::TypeId::None;
|
|
auto result_id = PerformAction(context, loc_id, action_inst);
|
|
auto result_type_id = context.insts().Get(result_id).type_id();
|
|
CARBON_CHECK(expected_result_type_id == SemIR::TypeId::None ||
|
|
result_type_id == SemIR::ErrorInst::TypeId ||
|
|
result_type_id == expected_result_type_id);
|
|
return result_id;
|
|
}
|
|
|
|
namespace Internal {
|
|
// Performs setup steps for performing a delayed action. This is an
|
|
// implementation detail of PerformDelayedAction and should not be called
|
|
// directly.
|
|
auto BeginPerformDelayedAction(Context& context) -> void;
|
|
|
|
// Refines the operands of an action that is about to be performed within
|
|
// `specific_id`, so that they refer to instructions in the specific rather
|
|
// than in the generic. Creates new `specific_inst` instructions in the current
|
|
// instruction block as necessary to do this. This is an implementation detail
|
|
// of PerformDelayedAction and should not be called directly.
|
|
auto RefineOperandsInSpecific(Context& context, SemIR::SpecificId specific_id,
|
|
SemIR::Inst action) -> SemIR::Inst;
|
|
|
|
// Calls the PerformAction function for the given action, passing in the
|
|
// relevant arguments.
|
|
template <typename ActionT>
|
|
auto CallPerformAction(Context& context, SemIR::SpecificId specific_id,
|
|
SemIR::LocId loc_id, ActionT action_inst)
|
|
-> ReturnTypeForPerformAction<ActionT> {
|
|
if constexpr (ActionT::Kind.action_needs_specific_id()) {
|
|
// If the action takes a `SpecificId`, it's assumed to do whatever
|
|
// refinement of its operands is necessary itself.
|
|
return PerformAction(context, specific_id, loc_id, action_inst);
|
|
} else {
|
|
// Refine the operands of the action so that they refer to instructions in
|
|
// the specific rather than in the generic. Any instructions this creates
|
|
// are included in the block of instructions produced by the action.
|
|
SemIR::Inst refined_action =
|
|
Internal::RefineOperandsInSpecific(context, specific_id, action_inst);
|
|
return PerformAction(context, loc_id, refined_action.As<ActionT>());
|
|
}
|
|
}
|
|
|
|
// Performs cleanup steps for performing a delayed action. This is an
|
|
// implementation detail of PerformDelayedAction and should not be called
|
|
// directly.
|
|
auto EndPerformDelayedAction(Context& context, SemIR::InstId result_id)
|
|
-> SemIR::InstId;
|
|
} // namespace Internal
|
|
|
|
// Performs an action as a result of evaluation of a template's eval block.
|
|
template <typename ActionT>
|
|
requires(ActionT::Kind.constant_kind() == SemIR::InstConstantKind::InstAction)
|
|
auto PerformDelayedAction(Context& context, SemIR::SpecificId specific_id,
|
|
SemIR::LocId loc_id, ActionT action_inst)
|
|
-> SemIR::InstId {
|
|
if (!ActionIsPerformable(context, action_inst, specific_id)) {
|
|
return SemIR::InstId::None;
|
|
}
|
|
Internal::BeginPerformDelayedAction(context);
|
|
auto inst_id =
|
|
Internal::CallPerformAction(context, specific_id, loc_id, action_inst);
|
|
return Internal::EndPerformDelayedAction(context, inst_id);
|
|
}
|
|
template <typename ActionT>
|
|
requires(ActionT::Kind.constant_kind() ==
|
|
SemIR::InstConstantKind::MultiInstAction)
|
|
auto PerformDelayedAction(Context& context, SemIR::SpecificId specific_id,
|
|
SemIR::LocId loc_id, ActionT action_inst)
|
|
-> llvm::SmallVector<SemIR::InstId> {
|
|
if (!ActionIsPerformable(context, action_inst, specific_id)) {
|
|
return {};
|
|
}
|
|
Internal::BeginPerformDelayedAction(context);
|
|
auto inst_ids =
|
|
Internal::CallPerformAction(context, specific_id, loc_id, action_inst);
|
|
inst_ids[0] = Internal::EndPerformDelayedAction(context, inst_ids[0]);
|
|
return inst_ids;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_ACTION_H_
|