formatter.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // A callback that indicates whether a specific entity, identified by its
  16. // declaration, should be included in the output.
  17. using ShouldFormatEntityFn =
  18. llvm::function_ref<auto(InstId decl_inst_id)->bool>;
  19. explicit Formatter(
  20. const File* sem_ir,
  21. ShouldFormatEntityFn should_format_entity = [](InstId) { return true; });
  22. ~Formatter();
  23. // Prints the full IR.
  24. auto Print(llvm::raw_ostream& out) -> void;
  25. private:
  26. const File* sem_ir_;
  27. ShouldFormatEntityFn should_format_entity_;
  28. // Caches naming between Print calls.
  29. InstNamer inst_namer_;
  30. };
  31. } // namespace Carbon::SemIR
  32. #endif // CARBON_TOOLCHAIN_SEM_IR_FORMATTER_H_