semantics_ir.cpp 1.9 KB

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