inst.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_CHECK_INST_H_
  5. #define CARBON_TOOLCHAIN_CHECK_INST_H_
  6. #include <concepts>
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/inst.h"
  10. namespace Carbon::Check {
  11. // Adds an instruction to the current block, returning the produced ID.
  12. auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  13. -> SemIR::InstId;
  14. // Convenience for AddInst with typed nodes.
  15. //
  16. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  17. template <typename InstT, typename LocT>
  18. requires(!InstT::Kind.has_cleanup() &&
  19. std::convertible_to<LocT, SemIR::LocId>)
  20. auto AddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  21. return AddInst(context, SemIR::LocIdAndInst(loc, inst));
  22. }
  23. // Like AddInst, but for instructions with a type_id of `TypeType`, which is
  24. // encoded in the return type of `TypeInstId`.
  25. inline auto AddTypeInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  26. -> SemIR::TypeInstId {
  27. return context.types().GetAsTypeInstId(AddInst(context, loc_id_and_inst));
  28. }
  29. template <typename InstT, typename LocT>
  30. requires(!InstT::Kind.has_cleanup() &&
  31. std::convertible_to<LocT, SemIR::LocId>)
  32. auto AddTypeInst(Context& context, LocT loc, InstT inst) -> SemIR::TypeInstId {
  33. return AddTypeInst(context, SemIR::LocIdAndInst(loc, inst));
  34. }
  35. // Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
  36. // result.
  37. //
  38. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  39. template <typename InstT>
  40. requires(SemIR::Internal::HasNodeId<InstT> && !InstT::Kind.has_cleanup())
  41. auto AddInstAndPush(Context& context,
  42. typename decltype(InstT::Kind)::TypedNodeId node_id,
  43. InstT inst) -> void {
  44. context.node_stack().Push(node_id, AddInst(context, node_id, inst));
  45. }
  46. // Adds an instruction in no block, returning the produced ID. Should be used
  47. // rarely.
  48. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  49. -> SemIR::InstId;
  50. // Convenience for AddInstInNoBlock with typed nodes.
  51. //
  52. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  53. template <typename InstT, typename LocT>
  54. requires(!InstT::Kind.has_cleanup() &&
  55. std::convertible_to<LocT, SemIR::LocId>)
  56. auto AddInstInNoBlock(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  57. return AddInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  58. }
  59. // If the instruction has a desugared location and a constant value, returns
  60. // the constant value's instruction ID. Otherwise, same as AddInst.
  61. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  62. -> SemIR::InstId;
  63. // Convenience for GetOrAddInst with typed nodes.
  64. //
  65. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  66. template <typename InstT, typename LocT>
  67. requires(!InstT::Kind.has_cleanup() &&
  68. std::convertible_to<LocT, SemIR::LocId>)
  69. auto GetOrAddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  70. return GetOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
  71. }
  72. // Evaluate the given instruction, and returns the corresponding constant value.
  73. // Adds the instruction to the current block if it might be referenced by its
  74. // constant value; otherwise, does not add the instruction to an instruction
  75. // block.
  76. //
  77. // The resulting ConstantId should be used immediately, or its canonical
  78. // instruction can be inserted into some other instruction (though it won't have
  79. // location information, so GetOrAddInst is typically better unless a canonical
  80. // instruction is required). The constant value can't be modified by specifics
  81. // unless it is done so as part of some other instruction.
  82. auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  83. -> SemIR::ConstantId;
  84. // Convenience for EvalOrAddInst with typed nodes.
  85. //
  86. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  87. template <typename InstT, typename LocT>
  88. requires(!InstT::Kind.has_cleanup() &&
  89. std::convertible_to<LocT, SemIR::LocId>)
  90. auto EvalOrAddInst(Context& context, LocT loc, InstT inst)
  91. -> SemIR::ConstantId {
  92. return EvalOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
  93. }
  94. // Adds an instruction and enqueues it to be added to the eval block of the
  95. // enclosing generic, returning the produced ID. The instruction is expected to
  96. // be a dependent template instantiation action.
  97. auto AddDependentActionInst(Context& context,
  98. SemIR::LocIdAndInst loc_id_and_inst)
  99. -> SemIR::InstId;
  100. // Convenience wrapper for AddDependentActionInst.
  101. template <typename InstT, typename LocT>
  102. requires std::convertible_to<LocT, SemIR::LocId>
  103. auto AddDependentActionInst(Context& context, LocT loc, InstT inst)
  104. -> SemIR::InstId {
  105. return AddDependentActionInst(context, SemIR::LocIdAndInst(loc, inst));
  106. }
  107. // Like AddDependentActionInst, but for instructions with a type_id of
  108. // `TypeType`, which is encoded in the return type of `TypeInstId`.
  109. template <typename InstT, typename LocT>
  110. requires std::convertible_to<LocT, SemIR::LocId>
  111. auto AddDependentActionTypeInst(Context& context, LocT loc, InstT inst)
  112. -> SemIR::TypeInstId {
  113. return context.types().GetAsTypeInstId(
  114. AddDependentActionInst(context, loc, inst));
  115. }
  116. // Adds an instruction to the current block, returning the produced ID. The
  117. // instruction is a placeholder that is expected to be replaced by
  118. // `ReplaceInstBeforeConstantUse`.
  119. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  120. -> SemIR::InstId;
  121. // Convenience for AddPlaceholderInst with typed nodes.
  122. //
  123. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  124. template <typename InstT, typename LocT>
  125. requires(!InstT::Kind.has_cleanup())
  126. auto AddPlaceholderInst(Context& context, LocT loc, InstT inst)
  127. -> SemIR::InstId {
  128. return AddPlaceholderInst(context, SemIR::LocIdAndInst(loc, inst));
  129. }
  130. // Adds an instruction in no block, returning the produced ID. Should be used
  131. // rarely. The instruction is a placeholder that is expected to be replaced by
  132. // `ReplaceInstBeforeConstantUse`.
  133. auto AddPlaceholderInstInNoBlock(Context& context,
  134. SemIR::LocIdAndInst loc_id_and_inst)
  135. -> SemIR::InstId;
  136. // Convenience for AddPlaceholderInstInNoBlock with typed nodes.
  137. //
  138. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  139. template <typename InstT, typename LocT>
  140. requires(!InstT::Kind.has_cleanup() &&
  141. std::convertible_to<LocT, SemIR::LocId>)
  142. auto AddPlaceholderInstInNoBlock(Context& context, LocT loc, InstT inst)
  143. -> SemIR::InstId {
  144. return AddPlaceholderInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  145. }
  146. // Similar to `AddPlaceholderInstInNoBlock`, but also tracks the instruction as
  147. // an import.
  148. auto AddPlaceholderImportedInstInNoBlock(Context& context,
  149. SemIR::LocIdAndInst loc_id_and_inst)
  150. -> SemIR::InstId;
  151. // Replaces the instruction at `inst_id` with `loc_id_and_inst`. The
  152. // instruction is required to not have been used in any constant evaluation,
  153. // either because it's newly created and entirely unused, or because it's only
  154. // used in a position that constant evaluation ignores, such as a return slot.
  155. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  156. SemIR::InstId inst_id,
  157. SemIR::LocIdAndInst loc_id_and_inst)
  158. -> void;
  159. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  160. // The instruction is required to not have been used in any constant
  161. // evaluation, either because it's newly created and entirely unused, or
  162. // because it's only used in a position that constant evaluation ignores, such
  163. // as a return slot.
  164. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  165. SemIR::Inst inst) -> void;
  166. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  167. // The instruction is required to not change its constant value.
  168. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  169. SemIR::Inst inst) -> void;
  170. // Sets only the parse node of an instruction. This is only used when setting
  171. // the parse node of an imported namespace. Versus
  172. // ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
  173. // in constant evaluation. It's exposed this way mainly so that `insts()` can
  174. // remain const.
  175. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  176. Parse::NodeId node_id) -> void;
  177. } // namespace Carbon::Check
  178. #endif // CARBON_TOOLCHAIN_CHECK_INST_H_