semantics_ir.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/nodes/expression_statement.h"
  9. namespace Carbon {
  10. void SemanticsIR::Print(llvm::raw_ostream& out, ParseTree::Node node) const {
  11. out << parse_tree_->GetNodeText(node);
  12. }
  13. void SemanticsIR::Print(llvm::raw_ostream& out,
  14. Semantics::Declaration decl) const {
  15. switch (decl.kind()) {
  16. case Semantics::DeclarationKind::Function:
  17. Print(out, declarations_.Get<Semantics::Function>(decl));
  18. return;
  19. case Semantics::DeclarationKind::Invalid:
  20. CARBON_FATAL() << "Invalid declaration type";
  21. }
  22. }
  23. void SemanticsIR::Print(llvm::raw_ostream& out,
  24. Semantics::Expression expr) const {
  25. switch (expr.kind()) {
  26. case Semantics::ExpressionKind::InfixOperator:
  27. Print(out, expressions_.Get<Semantics::InfixOperator>(expr));
  28. return;
  29. case Semantics::ExpressionKind::Literal:
  30. Print(out, expressions_.Get<Semantics::Literal>(expr));
  31. return;
  32. case Semantics::ExpressionKind::Invalid:
  33. CARBON_FATAL() << "Invalid expression type";
  34. }
  35. }
  36. void SemanticsIR::Print(llvm::raw_ostream& out,
  37. Semantics::Statement stmt) const {
  38. switch (stmt.kind()) {
  39. case Semantics::StatementKind::ExpressionStatement:
  40. Print(out, statements_.Get<Semantics::ExpressionStatement>(stmt));
  41. return;
  42. case Semantics::StatementKind::Return:
  43. Print(out, statements_.Get<Semantics::Return>(stmt));
  44. return;
  45. case Semantics::StatementKind::Invalid:
  46. CARBON_FATAL() << "Invalid expression type";
  47. }
  48. out << ";";
  49. }
  50. void SemanticsIR::Print(llvm::raw_ostream& out,
  51. const Semantics::DeclaredName& name) const {
  52. Print(out, name.node());
  53. }
  54. void SemanticsIR::Print(llvm::raw_ostream& out,
  55. const Semantics::ExpressionStatement& expr) const {
  56. Print(out, expr.expression());
  57. }
  58. void SemanticsIR::Print(llvm::raw_ostream& out,
  59. const Semantics::Function& function) const {
  60. out << "fn ";
  61. Print(out, function.name());
  62. out << "(";
  63. llvm::ListSeparator sep;
  64. for (const auto& param : function.params()) {
  65. out << sep;
  66. Print(out, param);
  67. }
  68. out << ")";
  69. if (function.return_expr()) {
  70. out << " -> ";
  71. Print(out, *function.return_expr());
  72. }
  73. Print(out, function.body());
  74. }
  75. void SemanticsIR::Print(llvm::raw_ostream& out,
  76. const Semantics::InfixOperator& op) const {
  77. Print(out, op.lhs());
  78. out << " ";
  79. Print(out, op.node());
  80. out << " ";
  81. Print(out, op.rhs());
  82. }
  83. void SemanticsIR::Print(llvm::raw_ostream& out,
  84. const Semantics::Literal& literal) const {
  85. Print(out, literal.node());
  86. }
  87. void SemanticsIR::Print(llvm::raw_ostream& out,
  88. const Semantics::PatternBinding& binding) const {
  89. Print(out, binding.name());
  90. out << ": ";
  91. Print(out, binding.type());
  92. }
  93. void SemanticsIR::Print(llvm::raw_ostream& out,
  94. const Semantics::Return& ret) const {
  95. out << "return";
  96. if (ret.expression()) {
  97. out << " ";
  98. Print(out, *ret.expression());
  99. }
  100. }
  101. void SemanticsIR::Print(llvm::raw_ostream& out,
  102. const Semantics::StatementBlock& block) const {
  103. out << " { ";
  104. for (const auto& statement : block.nodes()) {
  105. Print(out, statement);
  106. out << "; ";
  107. }
  108. out << "}";
  109. }
  110. } // namespace Carbon