Files
carbon-lang/toolchain/check/inst.h
T
Richard Smith d4c7ca4221 Support for dependent initialization in templates (#7801)
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
2026-09-24 23:10:18 +00:00

222 lines
9.5 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_INST_H_
#define CARBON_TOOLCHAIN_CHECK_INST_H_
#include <concepts>
#include "toolchain/check/context.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
namespace Carbon::Check {
// Adds an instruction to the current block, returning the produced ID.
auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Convenience for AddInst with typed nodes.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup() &&
std::convertible_to<LocT, SemIR::LocId>)
auto AddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
return AddInst(context, SemIR::LocIdAndInst(loc, inst));
}
// Like AddInst, but for instructions with a type_id of `TypeType`, which is
// encoded in the return type of `TypeInstId`.
inline auto AddTypeInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::TypeInstId {
return context.types().GetAsTypeInstId(AddInst(context, loc_id_and_inst));
}
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup() &&
std::convertible_to<LocT, SemIR::LocId>)
auto AddTypeInst(Context& context, LocT loc, InstT inst) -> SemIR::TypeInstId {
return AddTypeInst(context, SemIR::LocIdAndInst(loc, inst));
}
// Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
// result.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT>
requires(SemIR::Internal::HasNodeId<InstT> && !InstT::Kind.has_cleanup())
auto AddInstAndPush(Context& context,
typename decltype(InstT::Kind)::TypedNodeId node_id,
InstT inst) -> void {
context.node_stack().Push(node_id, AddInst(context, node_id, inst));
}
// Adds an instruction in no block, returning the produced ID. Should be used
// rarely.
auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Convenience for AddInstInNoBlock with typed nodes.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup() &&
std::convertible_to<LocT, SemIR::LocId>)
auto AddInstInNoBlock(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
return AddInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
}
// If the instruction has a desugared location and a constant value, returns
// the constant value's instruction ID. Otherwise, same as AddInst.
auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Convenience for GetOrAddInst with typed nodes.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup() &&
std::convertible_to<LocT, SemIR::LocId>)
auto GetOrAddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
return GetOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
}
// Evaluate the given instruction, and returns the corresponding constant value.
// Adds the instruction to the current block if it might be referenced by its
// constant value; otherwise, does not add the instruction to an instruction
// block.
//
// The resulting ConstantId should be used immediately, or its canonical
// instruction can be inserted into some other instruction (though it won't have
// location information, so GetOrAddInst is typically better unless a canonical
// instruction is required). The constant value can't be modified by specifics
// unless it is done so as part of some other instruction.
auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::ConstantId;
// Convenience for EvalOrAddInst with typed nodes.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup() &&
std::convertible_to<LocT, SemIR::LocId>)
auto EvalOrAddInst(Context& context, LocT loc, InstT inst)
-> SemIR::ConstantId {
return EvalOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
}
// Adds an instruction and enqueues it to be added to the eval block of the
// enclosing generic, returning the produced ID. The instruction is expected to
// have a template constant value.
auto AddTemplateConstantInstToEvalBlock(Context& context,
SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Convenience wrapper for AddTemplateConstantInstToEvalBlock.
template <typename InstT, typename LocT>
requires std::convertible_to<LocT, SemIR::LocId>
auto AddTemplateConstantInstToEvalBlock(Context& context, LocT loc, InstT inst)
-> SemIR::InstId {
return AddTemplateConstantInstToEvalBlock(context,
SemIR::LocIdAndInst(loc, inst));
}
// Adds an instruction to the current block, returning the produced ID. The
// instruction is a placeholder that is expected to be replaced by
// `ReplaceInstBeforeConstantUse`.
auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Convenience for AddPlaceholderInst with typed nodes.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup())
auto AddPlaceholderInst(Context& context, LocT loc, InstT inst)
-> SemIR::InstId {
return AddPlaceholderInst(context, SemIR::LocIdAndInst(loc, inst));
}
// Adds an instruction in no block, returning the produced ID. Should be used
// rarely. The instruction is a placeholder that is expected to be replaced by
// `ReplaceInstBeforeConstantUse`.
auto AddPlaceholderInstInNoBlock(Context& context,
SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Convenience for AddPlaceholderInstInNoBlock with typed nodes.
//
// As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
template <typename InstT, typename LocT>
requires(!InstT::Kind.has_cleanup() &&
std::convertible_to<LocT, SemIR::LocId>)
auto AddPlaceholderInstInNoBlock(Context& context, LocT loc, InstT inst)
-> SemIR::InstId {
return AddPlaceholderInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
}
// Similar to `AddPlaceholderInstInNoBlock`, but also tracks the instruction as
// an import.
auto AddPlaceholderImportedInstInNoBlock(Context& context,
SemIR::LocIdAndInst loc_id_and_inst)
-> SemIR::InstId;
// Replaces the instruction at `inst_id` with `loc_id_and_inst`. The
// instruction is required to not have been used in any constant evaluation,
// either because it's newly created and entirely unused, or because it's only
// used in a position that constant evaluation ignores, such as a return slot.
auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
SemIR::InstId inst_id,
SemIR::LocIdAndInst loc_id_and_inst)
-> void;
// Replaces the instruction at `inst_id` with `inst`, not affecting location.
// The instruction is required to not have been used in any constant
// evaluation, either because it's newly created and entirely unused, or
// because it's only used in a position that constant evaluation ignores, such
// as a return slot.
auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
SemIR::Inst inst) -> void;
// Replaces the instruction at `inst_id` with `inst`, not affecting location.
// The instruction is required to not change its constant value.
auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
SemIR::Inst inst) -> void;
// Sets only the parse node of an instruction. This is only used when setting
// the parse node of an imported namespace. Versus
// ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
// in constant evaluation. It's exposed this way mainly so that `insts()` can
// remain const.
auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
Parse::NodeId node_id) -> void;
// Result type for `WrapInstForSpecific`.
struct WrapInstForSpecificResult {
SemIR::InstId inst_id;
SemIR::TypeId type_id;
};
// Given a potentially generic instruction `inst_id`, this returns an inst ID
// whose constant value is the same as the substituted constant value of
// `inst_id` in `specific_id`. If the result is not `inst_id` itself, it will be
// a newly created inst whose location is `loc_id`. If `specific_id` is `None`,
// it is treated as the self-specific, so `inst_id` is passed through unchanged.
//
// This also returns the type ID of the result, as a convenience for the caller.
auto WrapInstForSpecific(Context& context, SemIR::LocId loc_id,
SemIR::InstId inst_id, SemIR::SpecificId specific_id)
-> WrapInstForSpecificResult;
// Builds a reference to the given name, which has already been resolved to
// `inst_id` within `specific_id`.
auto BuildNameRef(Context& context, SemIR::LocId loc_id, SemIR::NameId name_id,
SemIR::InstId inst_id, SemIR::SpecificId specific_id)
-> SemIR::InstId;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_INST_H_