inst.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. template <typename InstT, typename LocT>
  15. auto AddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  16. return AddInst(context, SemIR::LocIdAndInst(loc, inst));
  17. }
  18. // Pushes a parse tree node onto the stack, storing the SemIR::Inst as the
  19. // result.
  20. template <typename InstT>
  21. requires(SemIR::Internal::HasNodeId<InstT>)
  22. auto AddInstAndPush(Context& context,
  23. typename decltype(InstT::Kind)::TypedNodeId node_id,
  24. InstT inst) -> void {
  25. context.node_stack().Push(node_id, AddInst(context, node_id, inst));
  26. }
  27. // Adds an instruction in no block, returning the produced ID. Should be used
  28. // rarely.
  29. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  30. -> SemIR::InstId;
  31. // Convenience for AddInstInNoBlock with typed nodes.
  32. template <typename InstT, typename LocT>
  33. auto AddInstInNoBlock(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  34. return AddInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  35. }
  36. // If the instruction has an implicit location and a constant value, returns
  37. // the constant value's instruction ID. Otherwise, same as AddInst.
  38. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  39. -> SemIR::InstId;
  40. // Convenience for GetOrAddInst with typed nodes.
  41. template <typename InstT, typename LocT>
  42. auto GetOrAddInst(Context& context, LocT loc, InstT inst) -> SemIR::InstId {
  43. return GetOrAddInst(context, SemIR::LocIdAndInst(loc, inst));
  44. }
  45. // Adds an instruction and enqueues it to be added to the eval block of the
  46. // enclosing generic, returning the produced ID. The instruction is expected to
  47. // be a dependent template instantiation action.
  48. auto AddDependentActionInst(Context& context,
  49. SemIR::LocIdAndInst loc_id_and_inst)
  50. -> SemIR::InstId;
  51. // Convenience wrapper for AddDependentActionInst.
  52. template <typename InstT, typename LocT>
  53. auto AddDependentActionInst(Context& context, LocT loc, InstT inst)
  54. -> decltype(AddDependentActionInst(context,
  55. SemIR::LocIdAndInst(loc, inst))) {
  56. return AddDependentActionInst(context, SemIR::LocIdAndInst(loc, inst));
  57. }
  58. // Adds an instruction to the current pattern block, returning the produced
  59. // ID.
  60. // TODO: Is it possible to remove this and pattern_block_stack, now that
  61. // we have BeginSubpattern etc. instead?
  62. auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  63. -> SemIR::InstId;
  64. // Convenience for AddPatternInst with typed nodes.
  65. template <typename InstT>
  66. requires(SemIR::Internal::HasNodeId<InstT>)
  67. auto AddPatternInst(Context& context,
  68. typename decltype(InstT::Kind)::TypedNodeId node_id,
  69. InstT inst) -> SemIR::InstId {
  70. return AddPatternInst(context, SemIR::LocIdAndInst(node_id, inst));
  71. }
  72. // Adds an instruction to the current block, returning the produced ID. The
  73. // instruction is a placeholder that is expected to be replaced by
  74. // `ReplaceInstBeforeConstantUse`.
  75. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  76. -> SemIR::InstId;
  77. // Convenience for AddPlaceholderInst with typed nodes.
  78. template <typename InstT, typename LocT>
  79. auto AddPlaceholderInst(Context& context, LocT loc, InstT inst)
  80. -> SemIR::InstId {
  81. return AddPlaceholderInst(context, SemIR::LocIdAndInst(loc, inst));
  82. }
  83. // Adds an instruction in no block, returning the produced ID. Should be used
  84. // rarely. The instruction is a placeholder that is expected to be replaced by
  85. // `ReplaceInstBeforeConstantUse`.
  86. auto AddPlaceholderInstInNoBlock(Context& context,
  87. SemIR::LocIdAndInst loc_id_and_inst)
  88. -> SemIR::InstId;
  89. // Convenience for AddPlaceholderInstInNoBlock with typed nodes.
  90. template <typename InstT, typename LocT>
  91. auto AddPlaceholderInstInNoBlock(Context& context, LocT loc, InstT inst)
  92. -> SemIR::InstId {
  93. return AddPlaceholderInstInNoBlock(context, SemIR::LocIdAndInst(loc, inst));
  94. }
  95. // Replaces the instruction at `inst_id` with `loc_id_and_inst`. The
  96. // instruction is required to not have been used in any constant evaluation,
  97. // either because it's newly created and entirely unused, or because it's only
  98. // used in a position that constant evaluation ignores, such as a return slot.
  99. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  100. SemIR::InstId inst_id,
  101. SemIR::LocIdAndInst loc_id_and_inst)
  102. -> void;
  103. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  104. // The instruction is required to not have been used in any constant
  105. // evaluation, either because it's newly created and entirely unused, or
  106. // because it's only used in a position that constant evaluation ignores, such
  107. // as a return slot.
  108. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  109. SemIR::Inst inst) -> void;
  110. // Replaces the instruction at `inst_id` with `inst`, not affecting location.
  111. // The instruction is required to not change its constant value.
  112. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  113. SemIR::Inst inst) -> void;
  114. // Sets only the parse node of an instruction. This is only used when setting
  115. // the parse node of an imported namespace. Versus
  116. // ReplaceInstBeforeConstantUse, it is safe to use after the namespace is used
  117. // in constant evaluation. It's exposed this way mainly so that `insts()` can
  118. // remain const.
  119. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  120. Parse::NodeId node_id) -> void;
  121. } // namespace Carbon::Check
  122. #endif // CARBON_TOOLCHAIN_CHECK_INST_H_