formatter.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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_SEM_IR_FORMATTER_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_FORMATTER_H_
  6. #include "llvm/Support/raw_ostream.h"
  7. #include "toolchain/lex/tokenized_buffer.h"
  8. #include "toolchain/parse/tree.h"
  9. #include "toolchain/sem_ir/file.h"
  10. #include "toolchain/sem_ir/inst_namer.h"
  11. namespace Carbon::SemIR {
  12. // Formatter for printing textual Semantics IR.
  13. class Formatter {
  14. public:
  15. explicit Formatter(const Lex::TokenizedBuffer& tokenized_buffer,
  16. const Parse::Tree& parse_tree, const File& sem_ir);
  17. ~Formatter();
  18. // Prints the full IR.
  19. auto Print(llvm::raw_ostream& out) -> void;
  20. // Prints a single code block. Only prints the last several instructions of
  21. // large blocks.
  22. auto PrintPartialTrailingCodeBlock(llvm::ArrayRef<SemIR::InstId> block,
  23. int indent, llvm::raw_ostream& out)
  24. -> void;
  25. // Prints a single instruction.
  26. auto PrintInst(SemIR::InstId inst_id, int indent, llvm::raw_ostream& out)
  27. -> void;
  28. private:
  29. const File& sem_ir_;
  30. // Caches naming between Print calls.
  31. InstNamer inst_namer_;
  32. };
  33. } // namespace Carbon::SemIR
  34. #endif // CARBON_TOOLCHAIN_SEM_IR_FORMATTER_H_