subst.h 3.0 KB

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