subst.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_SUBST_H_
  5. #define CARBON_TOOLCHAIN_CHECK_SUBST_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Callbacks used by SubstInst to recursively substitute into and rebuild an
  10. // instruction.
  11. class SubstInstCallbacks {
  12. public:
  13. explicit SubstInstCallbacks(Context* context) : context_(context) {}
  14. auto context() const -> Context& { return *context_; }
  15. // How further substitution should or should not be applied to the instruction
  16. // after Subst is done.
  17. //
  18. // Rebuild or ReuseUnchaged will always be called when SubstAgain or
  19. // SubstOperands is returned, after processing anything inside the instruction
  20. // after Subst.
  21. enum SubstResult {
  22. // Don't substitute into the operands of the instruction.
  23. FullySubstituted,
  24. // Attempt to substitute into the operands of the instruction.
  25. SubstOperands,
  26. // Attempt to substitute again on the resulting instruction, acting like
  27. // recursion on the instruction itself.
  28. SubstAgain,
  29. // Attempt to substitute into the operands of the instruction. If the InstId
  30. // returned from Rebuild or ReuseUnchanged differs from the input (typically
  31. // because some operand in the instruction changed), then the new
  32. // instruction will be given to `Subst` again afterward. This allows for the
  33. // uncommon case of substituting from the inside out.
  34. SubstOperandsAndRetry,
  35. };
  36. // Performs any needed substitution into an instruction. The instruction ID
  37. // should be updated as necessary to represent the new instruction.
  38. //
  39. // Return FullySubstituted if the resulting instruction ID is
  40. // fully-substituted. Return SubstOperands if substitution may be needed into
  41. // operands of the instruction, or SubstAgain if the replaced instruction
  42. // itself should have substitution applied to it again. Return
  43. // SubstOperandsAndRetry to recurse on the instructions operands and then
  44. // substitute the resulting instruction afterward, if the instruction is
  45. // replaced by a new one (typically due to Rebuild when the operands changed).
  46. //
  47. // When SubstOperands, SubstAgain, or SubstOperandsAndRetry is returned, it
  48. // results in a call back to Rebuild or ReuseUnchanged when that instruction's
  49. // substitution step is complete.
  50. virtual auto Subst(SemIR::InstId& inst_id) -> SubstResult = 0;
  51. // Rebuilds the type of an instruction from the substituted type instruction.
  52. // By default this builds the unattached type described by the given type ID.
  53. virtual auto RebuildType(SemIR::TypeInstId type_inst_id) const
  54. -> SemIR::TypeId;
  55. // Rebuilds an instruction whose operands were changed by substitution.
  56. // `orig_inst_id` is the instruction prior to substitution, and `new_inst` is
  57. // the substituted instruction. Returns the new instruction ID to use to refer
  58. // to `new_inst`.
  59. virtual auto Rebuild(SemIR::InstId orig_inst_id, SemIR::Inst new_inst)
  60. -> SemIR::InstId = 0;
  61. // Performs any work needed when no substitutions were performed into an
  62. // instruction for which `Subst` returned `false`. Provides an opportunity to
  63. // perform any necessary updates to the instruction beyond updating its
  64. // operands. Returns the new instruction ID to use to refer to `orig_inst_id`.
  65. virtual auto ReuseUnchanged(SemIR::InstId orig_inst_id) -> SemIR::InstId {
  66. return orig_inst_id;
  67. }
  68. // Builds a new constant by evaluating `new_inst`, and returns its `InstId`.
  69. //
  70. // This can be used to implement `Rebuild` in straightforward cases.
  71. auto RebuildNewInst(SemIR::LocId loc_id, SemIR::Inst new_inst) const
  72. -> SemIR::InstId;
  73. template <typename InstT>
  74. auto RebuildNewInst(SemIR::LocId loc_id, InstT new_inst) const
  75. -> SemIR::InstId {
  76. return RebuildNewInst(loc_id, static_cast<SemIR::Inst>(new_inst));
  77. }
  78. private:
  79. Context* context_;
  80. };
  81. // Performs substitution into `inst_id` and its operands recursively, using
  82. // `callbacks` to process each instruction. For each instruction encountered,
  83. // calls `Subst` to perform substitution on that instruction.
  84. auto SubstInst(Context& context, SemIR::InstId inst_id,
  85. SubstInstCallbacks& callbacks) -> SemIR::InstId;
  86. auto SubstInst(Context& context, SemIR::TypeInstId inst_id,
  87. SubstInstCallbacks& callbacks) -> SemIR::TypeInstId;
  88. // A substitution that is being performed.
  89. struct Substitution {
  90. // The index of a `SymbolicBinding` instruction that is being replaced.
  91. SemIR::CompileTimeBindIndex bind_id;
  92. // The replacement constant value to substitute.
  93. SemIR::ConstantId replacement_id;
  94. };
  95. using Substitutions = llvm::ArrayRef<Substitution>;
  96. // Replaces the `SymbolicBinding` instruction `bind_id` with `replacement_id`
  97. // throughout the constant `const_id`, and returns the substituted value.
  98. auto SubstConstant(Context& context, SemIR::LocId loc_id,
  99. SemIR::ConstantId const_id, Substitutions substitutions)
  100. -> SemIR::ConstantId;
  101. } // namespace Carbon::Check
  102. #endif // CARBON_TOOLCHAIN_CHECK_SUBST_H_