semantics_ir.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include "toolchain/semantics/semantics_ir.h"
  5. #include "common/check.h"
  6. #include "llvm/Support/FormatVariadic.h"
  7. #include "toolchain/lexer/tokenized_buffer.h"
  8. #include "toolchain/semantics/semantics_node.h"
  9. namespace Carbon {
  10. auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
  11. out << "identifiers = {\n";
  12. for (int32_t i = 0; i < static_cast<int32_t>(identifiers_.size()); ++i) {
  13. out.indent(2);
  14. out << SemanticsIdentifierId(i) << " = \"" << identifiers_[i] << "\";\n";
  15. }
  16. out << "},\n";
  17. out << "integer_literals = {\n";
  18. for (int32_t i = 0; i < static_cast<int32_t>(integer_literals_.size()); ++i) {
  19. out.indent(2);
  20. out << SemanticsIntegerLiteralId(i) << " = " << integer_literals_[i]
  21. << ";\n";
  22. }
  23. out << "},\n";
  24. out << "nodes = {\n";
  25. int indent = 2;
  26. for (int32_t i = 0; i < static_cast<int32_t>(nodes_.size()); ++i) {
  27. SemanticsNode node = nodes_[i];
  28. // Adjust indent for block contents.
  29. switch (node.kind()) {
  30. case SemanticsNodeKind::CodeBlockStart():
  31. case SemanticsNodeKind::FunctionDefinitionStart():
  32. out.indent(indent);
  33. indent += 2;
  34. break;
  35. case SemanticsNodeKind::CodeBlockEnd():
  36. case SemanticsNodeKind::FunctionDefinitionEnd():
  37. indent -= 2;
  38. out.indent(indent);
  39. break;
  40. default:
  41. // No indentation change.
  42. out.indent(indent);
  43. break;
  44. }
  45. out << SemanticsNodeId(i) << " = " << node << ";\n";
  46. }
  47. out << "}\n";
  48. }
  49. } // namespace Carbon