statement.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "explorer/ast/statement.h"
  5. #include "common/check.h"
  6. #include "explorer/ast/declaration.h"
  7. #include "explorer/base/arena.h"
  8. #include "llvm/Support/Casting.h"
  9. namespace Carbon {
  10. using llvm::cast;
  11. Statement::~Statement() = default;
  12. void Statement::PrintDepth(int depth, llvm::raw_ostream& out) const {
  13. if (depth == 0) {
  14. out << " ... ";
  15. return;
  16. }
  17. switch (kind()) {
  18. case StatementKind::Match: {
  19. const auto& match = cast<Match>(*this);
  20. out << "match (" << match.expression() << ") {";
  21. if (depth < 0 || depth > 1) {
  22. out << "\n";
  23. for (const auto& clause : match.clauses()) {
  24. out << "case " << clause.pattern() << " =>\n";
  25. clause.statement().PrintDepth(depth - 1, out);
  26. out << "\n";
  27. }
  28. } else {
  29. out << "...";
  30. }
  31. out << "}";
  32. break;
  33. }
  34. case StatementKind::While: {
  35. const auto& while_stmt = cast<While>(*this);
  36. out << "while (" << while_stmt.condition() << ")\n";
  37. while_stmt.body().PrintDepth(depth - 1, out);
  38. break;
  39. }
  40. case StatementKind::For: {
  41. const auto& for_stmt = cast<For>(*this);
  42. out << "for (" << for_stmt.variable_declaration() << " in "
  43. << for_stmt.loop_target() << ")\n";
  44. for_stmt.body().PrintDepth(depth - 1, out);
  45. break;
  46. }
  47. case StatementKind::Break:
  48. out << "break;";
  49. break;
  50. case StatementKind::Continue:
  51. out << "continue;";
  52. break;
  53. case StatementKind::VariableDefinition: {
  54. const auto& var = cast<VariableDefinition>(*this);
  55. if (var.is_returned()) {
  56. out << "returned ";
  57. }
  58. out << "var " << var.pattern();
  59. if (var.has_init()) {
  60. out << " = " << var.init();
  61. }
  62. out << ";";
  63. break;
  64. }
  65. case StatementKind::ExpressionStatement:
  66. out << cast<ExpressionStatement>(*this).expression() << ";";
  67. break;
  68. case StatementKind::Assign: {
  69. const auto& assign = cast<Assign>(*this);
  70. out << assign.lhs() << " " << AssignOperatorToString(assign.op()) << " "
  71. << assign.rhs() << ";";
  72. break;
  73. }
  74. case StatementKind::IncrementDecrement: {
  75. const auto& inc_dec = cast<IncrementDecrement>(*this);
  76. out << (inc_dec.is_increment() ? "++" : "--") << inc_dec.argument();
  77. break;
  78. }
  79. case StatementKind::If: {
  80. const auto& if_stmt = cast<If>(*this);
  81. out << "if (" << if_stmt.condition() << ")\n";
  82. if_stmt.then_block().PrintDepth(depth - 1, out);
  83. if (if_stmt.else_block()) {
  84. out << "\nelse\n";
  85. (*if_stmt.else_block())->PrintDepth(depth - 1, out);
  86. }
  87. break;
  88. }
  89. case StatementKind::ReturnVar: {
  90. out << "return var;";
  91. break;
  92. }
  93. case StatementKind::ReturnExpression: {
  94. const auto& ret = cast<ReturnExpression>(*this);
  95. if (ret.is_omitted_expression()) {
  96. out << "return;";
  97. } else {
  98. out << "return " << ret.expression() << ";";
  99. }
  100. break;
  101. }
  102. case StatementKind::Block: {
  103. const auto& block = cast<Block>(*this);
  104. out << "{";
  105. if (depth < 0 || depth > 1) {
  106. out << "\n";
  107. }
  108. for (const auto* statement : block.statements()) {
  109. statement->PrintDepth(depth, out);
  110. if (depth < 0 || depth > 1) {
  111. out << "\n";
  112. }
  113. }
  114. out << "}";
  115. if (depth < 0 || depth > 1) {
  116. out << "\n";
  117. }
  118. break;
  119. }
  120. }
  121. }
  122. auto AssignOperatorToString(AssignOperator op) -> std::string_view {
  123. switch (op) {
  124. case AssignOperator::Plain:
  125. return "=";
  126. case AssignOperator::Add:
  127. return "+=";
  128. case AssignOperator::Div:
  129. return "/=";
  130. case AssignOperator::Mul:
  131. return "*=";
  132. case AssignOperator::Mod:
  133. return "%=";
  134. case AssignOperator::Sub:
  135. return "-=";
  136. case AssignOperator::And:
  137. return "&=";
  138. case AssignOperator::Or:
  139. return "|=";
  140. case AssignOperator::Xor:
  141. return "^=";
  142. case AssignOperator::ShiftLeft:
  143. return "<<=";
  144. case AssignOperator::ShiftRight:
  145. return ">>=";
  146. }
  147. }
  148. Return::Return(CloneContext& context, const Return& other)
  149. : Statement(context, other), function_(context.Remap(other.function_)) {}
  150. } // namespace Carbon