inst.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. template <typename InstT, typename LocT>
  26. requires(!InstT::Kind.has_cleanup() &&
  27. std::convertible_to<LocT, SemIR::LocId>)
  28. auto AddTypeInst(Context& context, LocT loc, InstT inst) -> SemIR::TypeInstId {
  29. return context.types().GetAsTypeInstId(
  30. AddInst(context, SemIR::LocIdAndInst(loc, inst)));
  31. }
  32. // Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
  33. // result.
  34. //
  35. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  36. template <typename InstT>
  37. requires(SemIR::Internal::HasNodeId<InstT> && !InstT::Kind.has_cleanup())
  38. auto AddInstAndPush(Context& context,
  39. typename decltype(InstT::Kind)::TypedNodeId node_id,
  40. InstT inst) -> void {
  41. context.node_stack().Push(node_id, AddInst(context, node_id, inst));
  42. }
  43. // Adds an instruction in no block, returning the produced ID. Should be used
  44. // rarely.
  45. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  46. -> SemIR::InstId;
  47. // Convenience for AddInstInNoBlock with typed nodes.
  48. //
  49. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  50. template <typename InstT, typename LocT>
  51. requires(!InstT::Kind.has_cleanup() &&
  52. std::convertible_to<LocT, SemIR::LocId>)
  53. auto AddInstInNoBlock(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  54. return AddInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  55. }
  56. // If the instruction has an implicit location and a constant value, returns
  57. // the constant value's instruction ID. Otherwise, same as AddInst.
  58. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  59. -> SemIR::InstId;
  60. // Convenience for GetOrAddInst with typed nodes.
  61. //
  62. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  63. template <typename InstT, typename LocT>
  64. requires(!InstT::Kind.has_cleanup() &&
  65. std::convertible_to<LocT, SemIR::LocId>)
  66. auto GetOrAddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  67. return GetOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
  68. }
  69. // Evaluate the given instruction, and returns the corresponding constant value.
  70. // Adds the instruction to the current block if it might be referenced by its
  71. // constant value; otherwise, does not add the instruction to an instruction
  72. // block.
  73. auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  74. -> SemIR::ConstantId;
  75. // Convenience for EvalOrAddInst with typed nodes.
  76. //
  77. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  78. template <typename InstT, typename LocT>
  79. requires(!InstT::Kind.has_cleanup() &&
  80. std::convertible_to<LocT, SemIR::LocId>)
  81. auto EvalOrAddInst(Context& context, LocT loc, InstT inst)
  82. -> SemIR::ConstantId {
  83. return EvalOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
  84. }
  85. // Adds an instruction and enqueues it to be added to the eval block of the
  86. // enclosing generic, returning the produced ID. The instruction is expected to
  87. // be a dependent template instantiation action.
  88. auto AddDependentActionInst(Context& context,
  89. SemIR::LocIdAndInst loc_id_and_inst)
  90. -> SemIR::InstId;
  91. // Convenience wrapper for AddDependentActionInst.
  92. template <typename InstT, typename LocT>
  93. requires std::convertible_to<LocT, SemIR::LocId>
  94. auto AddDependentActionInst(Context& context, LocT loc, InstT inst)
  95. -> SemIR::InstId {
  96. return AddDependentActionInst(context, SemIR::LocIdAndInst(loc, inst));
  97. }
  98. // Like AddDependentActionInst, but for instructions with a type_id of
  99. // `TypeType`, which is encoded in the return type of `TypeInstId`.
  100. template <typename InstT, typename LocT>
  101. requires std::convertible_to<LocT, SemIR::LocId>
  102. auto AddDependentActionTypeInst(Context& context, LocT loc, InstT inst)
  103. -> SemIR::TypeInstId {
  104. return context.types().GetAsTypeInstId(
  105. AddDependentActionInst(context, loc, inst));
  106. }
  107. // Adds an instruction to the current pattern block, returning the produced
  108. // ID.
  109. // TODO: Is it possible to remove this and pattern_block_stack, now that
  110. // we have BeginSubpattern etc. instead?
  111. auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  112. -> SemIR::InstId;
  113. // Convenience for AddPatternInst with typed nodes.
  114. //
  115. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  116. template <typename InstT, typename LocT>
  117. requires(!InstT::Kind.has_cleanup() &&
  118. std::convertible_to<LocT, SemIR::LocId>)
  119. auto AddPatternInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  120. return AddPatternInst(context, SemIR::LocIdAndInst(loc, inst));
  121. }
  122. // Adds an instruction to the current block, returning the produced ID. The
  123. // instruction is a placeholder that is expected to be replaced by
  124. // `ReplaceInstBeforeConstantUse`.
  125. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  126. -> SemIR::InstId;
  127. // Convenience for AddPlaceholderInst with typed nodes.
  128. //
  129. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  130. template <typename InstT, typename LocT>
  131. requires(!InstT::Kind.has_cleanup())
  132. auto AddPlaceholderInst(Context& context, LocT loc, InstT inst)
  133. -> SemIR::InstId {
  134. return AddPlaceholderInst(context, SemIR::LocIdAndInst(loc, inst));
  135. }
  136. // Adds an instruction in no block, returning the produced ID. Should be used
  137. // rarely. The instruction is a placeholder that is expected to be replaced by
  138. // `ReplaceInstBeforeConstantUse`.
  139. auto AddPlaceholderInstInNoBlock(Context& context,
  140. SemIR::LocIdAndInst loc_id_and_inst)
  141. -> SemIR::InstId;
  142. // Convenience for AddPlaceholderInstInNoBlock with typed nodes.
  143. //
  144. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  145. template <typename InstT, typename LocT>
  146. requires(!InstT::Kind.has_cleanup() &&
  147. std::convertible_to<LocT, SemIR::LocId>)
  148. auto AddPlaceholderInstInNoBlock(Context& context, LocT loc, InstT inst)
  149. -> SemIR::InstId {
  150. return AddPlaceholderInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  151. }
  152. // Replaces the instruction at `inst_id` with `loc_id_and_inst`. The
  153. // instruction is required to not have been used in any constant evaluation,
  154. // either because it's newly created and entirely unused, or because it's only
  155. // used in a position that constant evaluation ignores, such as a return slot.
  156. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  157. SemIR::InstId inst_id,
  158. SemIR::LocIdAndInst loc_id_and_inst)
  159. -> void;
  160. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  161. // The instruction is required to not have been used in any constant
  162. // evaluation, either because it's newly created and entirely unused, or
  163. // because it's only used in a position that constant evaluation ignores, such
  164. // as a return slot.
  165. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  166. SemIR::Inst inst) -> void;
  167. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  168. // The instruction is required to not change its constant value.
  169. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  170. SemIR::Inst inst) -> void;
  171. // Sets only the parse node of an instruction. This is only used when setting
  172. // the parse node of an imported namespace. Versus
  173. // ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
  174. // in constant evaluation. It's exposed this way mainly so that `insts()` can
  175. // remain const.
  176. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  177. Parse::NodeId node_id) -> void;
  178. } // namespace Carbon::Check
  179. #endif // CARBON_TOOLCHAIN_CHECK_INST_H_