subst.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Performs any needed substitution into an instruction. The instruction ID
  16. // should be updated as necessary to represent the new instruction. Returns
  17. // true if the resulting instruction ID is fully-substituted, or false if
  18. // substitution may be needed into operands of the instruction.
  19. virtual auto Subst(SemIR::InstId& inst_id) const -> bool = 0;
  20. // Rebuilds the type of an instruction from the substituted type instruction.
  21. // By default this builds the unattached type described by the given type ID.
  22. virtual auto RebuildType(SemIR::TypeInstId type_inst_id) const
  23. -> SemIR::TypeId;
  24. // Rebuilds an instruction whose operands were changed by substitution.
  25. // `orig_inst_id` is the instruction prior to substitution, and `new_inst` is
  26. // the substituted instruction. Returns the new instruction ID to use to refer
  27. // to `new_inst`.
  28. virtual auto Rebuild(SemIR::InstId orig_inst_id, SemIR::Inst new_inst) const
  29. -> SemIR::InstId = 0;
  30. // Performs any work needed when no substitutions were performed into an
  31. // instruction for which `Subst` returned `false`. Provides an opportunity to
  32. // perform any necessary updates to the instruction beyond updating its
  33. // operands. Returns the new instruction ID to use to refer to `orig_inst_id`.
  34. virtual auto ReuseUnchanged(SemIR::InstId orig_inst_id) const
  35. -> SemIR::InstId {
  36. return orig_inst_id;
  37. }
  38. // Builds a new constant by evaluating `new_inst`, and returns its `InstId`.
  39. //
  40. // This can be used to implement `Rebuild` in straightforward cases.
  41. auto RebuildNewInst(SemIR::LocId loc_id, SemIR::Inst new_inst) const
  42. -> SemIR::InstId;
  43. private:
  44. Context* context_;
  45. };
  46. // Performs substitution into `inst_id` and its operands recursively, using
  47. // `callbacks` to process each instruction. For each instruction encountered,
  48. // calls `Subst` to perform substitution on that instruction.
  49. //
  50. // If `Subst` returns false, the instruction is decomposed into its operands,
  51. // which are substituted recursively, and if any of them change then `Rebuild`
  52. // is used to build a new instruction with the substituted operands.
  53. auto SubstInst(Context& context, SemIR::InstId inst_id,
  54. const SubstInstCallbacks& callbacks) -> SemIR::InstId;
  55. auto SubstInst(Context& context, SemIR::TypeInstId inst_id,
  56. const SubstInstCallbacks& callbacks) -> SemIR::TypeInstId;
  57. // A substitution that is being performed.
  58. struct Substitution {
  59. // The index of a `BindSymbolicName` instruction that is being replaced.
  60. SemIR::CompileTimeBindIndex bind_id;
  61. // The replacement constant value to substitute.
  62. SemIR::ConstantId replacement_id;
  63. };
  64. using Substitutions = llvm::ArrayRef<Substitution>;
  65. // Replaces the `BindSymbolicName` instruction `bind_id` with `replacement_id`
  66. // throughout the constant `const_id`, and returns the substituted value.
  67. auto SubstConstant(Context& context, SemIR::LocId loc_id,
  68. SemIR::ConstantId const_id, Substitutions substitutions)
  69. -> SemIR::ConstantId;
  70. } // namespace Carbon::Check
  71. #endif // CARBON_TOOLCHAIN_CHECK_SUBST_H_