inst.h 8.4 KB

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