inst.h 5.2 KB

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