inst.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  77. -> SemIR::ConstantId;
  78. // Convenience for EvalOrAddInst with typed nodes.
  79. //
  80. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  81. template <typename InstT, typename LocT>
  82. requires(!InstT::Kind.has_cleanup() &&
  83. std::convertible_to<LocT, SemIR::LocId>)
  84. auto EvalOrAddInst(Context& context, LocT loc, InstT inst)
  85. -> SemIR::ConstantId {
  86. return EvalOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
  87. }
  88. // Adds an instruction and enqueues it to be added to the eval block of the
  89. // enclosing generic, returning the produced ID. The instruction is expected to
  90. // be a dependent template instantiation action.
  91. auto AddDependentActionInst(Context& context,
  92. SemIR::LocIdAndInst loc_id_and_inst)
  93. -> SemIR::InstId;
  94. // Convenience wrapper for AddDependentActionInst.
  95. template <typename InstT, typename LocT>
  96. requires std::convertible_to<LocT, SemIR::LocId>
  97. auto AddDependentActionInst(Context& context, LocT loc, InstT inst)
  98. -> SemIR::InstId {
  99. return AddDependentActionInst(context, SemIR::LocIdAndInst(loc, inst));
  100. }
  101. // Like AddDependentActionInst, but for instructions with a type_id of
  102. // `TypeType`, which is encoded in the return type of `TypeInstId`.
  103. template <typename InstT, typename LocT>
  104. requires std::convertible_to<LocT, SemIR::LocId>
  105. auto AddDependentActionTypeInst(Context& context, LocT loc, InstT inst)
  106. -> SemIR::TypeInstId {
  107. return context.types().GetAsTypeInstId(
  108. AddDependentActionInst(context, loc, inst));
  109. }
  110. // Adds an instruction to the current pattern block, returning the produced
  111. // ID.
  112. // TODO: Is it possible to remove this and pattern_block_stack, now that
  113. // we have BeginSubpattern etc. instead?
  114. auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  115. -> SemIR::InstId;
  116. // Convenience for AddPatternInst with typed nodes.
  117. //
  118. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  119. template <typename InstT, typename LocT>
  120. requires(!InstT::Kind.has_cleanup() &&
  121. std::convertible_to<LocT, SemIR::LocId>)
  122. auto AddPatternInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  123. return AddPatternInst(context, SemIR::LocIdAndInst(loc, inst));
  124. }
  125. // Adds an instruction to the current block, returning the produced ID. The
  126. // instruction is a placeholder that is expected to be replaced by
  127. // `ReplaceInstBeforeConstantUse`.
  128. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  129. -> SemIR::InstId;
  130. // Convenience for AddPlaceholderInst with typed nodes.
  131. //
  132. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  133. template <typename InstT, typename LocT>
  134. requires(!InstT::Kind.has_cleanup())
  135. auto AddPlaceholderInst(Context& context, LocT loc, InstT inst)
  136. -> SemIR::InstId {
  137. return AddPlaceholderInst(context, SemIR::LocIdAndInst(loc, inst));
  138. }
  139. // Adds an instruction in no block, returning the produced ID. Should be used
  140. // rarely. The instruction is a placeholder that is expected to be replaced by
  141. // `ReplaceInstBeforeConstantUse`.
  142. auto AddPlaceholderInstInNoBlock(Context& context,
  143. SemIR::LocIdAndInst loc_id_and_inst)
  144. -> SemIR::InstId;
  145. // Convenience for AddPlaceholderInstInNoBlock with typed nodes.
  146. //
  147. // As a safety check, prevent use with storage insts (see `AddInstWithCleanup`).
  148. template <typename InstT, typename LocT>
  149. requires(!InstT::Kind.has_cleanup() &&
  150. std::convertible_to<LocT, SemIR::LocId>)
  151. auto AddPlaceholderInstInNoBlock(Context& context, LocT loc, InstT inst)
  152. -> SemIR::InstId {
  153. return AddPlaceholderInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  154. }
  155. // Similar to `AddPlaceholderInstInNoBlock`, but also tracks the instruction as
  156. // an import.
  157. auto AddPlaceholderImportedInstInNoBlock(Context& context,
  158. SemIR::LocIdAndInst loc_id_and_inst)
  159. -> SemIR::InstId;
  160. // Replaces the instruction at `inst_id` with `loc_id_and_inst`. The
  161. // instruction is required to not have been used in any constant evaluation,
  162. // either because it's newly created and entirely unused, or because it's only
  163. // used in a position that constant evaluation ignores, such as a return slot.
  164. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  165. SemIR::InstId inst_id,
  166. SemIR::LocIdAndInst loc_id_and_inst)
  167. -> void;
  168. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  169. // The instruction is required to not have been used in any constant
  170. // evaluation, either because it's newly created and entirely unused, or
  171. // because it's only used in a position that constant evaluation ignores, such
  172. // as a return slot.
  173. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  174. SemIR::Inst inst) -> void;
  175. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  176. // The instruction is required to not change its constant value.
  177. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  178. SemIR::Inst inst) -> void;
  179. // Sets only the parse node of an instruction. This is only used when setting
  180. // the parse node of an imported namespace. Versus
  181. // ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
  182. // in constant evaluation. It's exposed this way mainly so that `insts()` can
  183. // remain const.
  184. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  185. Parse::NodeId node_id) -> void;
  186. } // namespace Carbon::Check
  187. #endif // CARBON_TOOLCHAIN_CHECK_INST_H_