subst.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. private:
  39. Context* context_;
  40. };
  41. // Performs substitution into `inst_id` and its operands recursively, using
  42. // `callbacks` to process each instruction. For each instruction encountered,
  43. // calls `Subst` to perform substitution on that instruction.
  44. //
  45. // If `Subst` returns false, the instruction is decomposed into its operands,
  46. // which are substituted recursively, and if any of them change then `Rebuild`
  47. // is used to build a new instruction with the substituted operands.
  48. auto SubstInst(Context& context, SemIR::InstId inst_id,
  49. const SubstInstCallbacks& callbacks) -> SemIR::InstId;
  50. auto SubstInst(Context& context, SemIR::TypeInstId inst_id,
  51. const SubstInstCallbacks& callbacks) -> SemIR::TypeInstId;
  52. // A substitution that is being performed.
  53. struct Substitution {
  54. // The index of a `BindSymbolicName` instruction that is being replaced.
  55. SemIR::CompileTimeBindIndex bind_id;
  56. // The replacement constant value to substitute.
  57. SemIR::ConstantId replacement_id;
  58. };
  59. using Substitutions = llvm::ArrayRef<Substitution>;
  60. // Replaces the `BindSymbolicName` instruction `bind_id` with `replacement_id`
  61. // throughout the constant `const_id`, and returns the substituted value.
  62. auto SubstConstant(Context& context, SemIR::ConstantId const_id,
  63. Substitutions substitutions) -> SemIR::ConstantId;
  64. } // namespace Carbon::Check
  65. #endif // CARBON_TOOLCHAIN_CHECK_SUBST_H_