expression.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "executable_semantics/ast/expression.h"
  5. #include <optional>
  6. #include "executable_semantics/common/arena.h"
  7. #include "executable_semantics/common/error.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. namespace Carbon {
  11. auto ExpressionFromParenContents(
  12. int line_num, const ParenContents<Expression>& paren_contents)
  13. -> const Expression* {
  14. std::optional<const Expression*> single_term = paren_contents.SingleTerm();
  15. if (single_term.has_value()) {
  16. return *single_term;
  17. } else {
  18. return TupleExpressionFromParenContents(line_num, paren_contents);
  19. }
  20. }
  21. auto TupleExpressionFromParenContents(
  22. int line_num, const ParenContents<Expression>& paren_contents)
  23. -> const Expression* {
  24. return Expression::MakeTupleLiteral(
  25. line_num, paren_contents.TupleElements<FieldInitializer>(line_num));
  26. }
  27. auto Expression::GetIdentifierExpression() const
  28. -> const IdentifierExpression& {
  29. return std::get<IdentifierExpression>(value);
  30. }
  31. auto Expression::GetFieldAccessExpression() const
  32. -> const FieldAccessExpression& {
  33. return std::get<FieldAccessExpression>(value);
  34. }
  35. auto Expression::GetIndexExpression() const -> const IndexExpression& {
  36. return std::get<IndexExpression>(value);
  37. }
  38. auto Expression::GetIntLiteral() const -> int {
  39. return std::get<IntLiteral>(value).value;
  40. }
  41. auto Expression::GetBoolLiteral() const -> bool {
  42. return std::get<BoolLiteral>(value).value;
  43. }
  44. auto Expression::GetTupleLiteral() const -> const TupleLiteral& {
  45. return std::get<TupleLiteral>(value);
  46. }
  47. auto Expression::GetPrimitiveOperatorExpression() const
  48. -> const PrimitiveOperatorExpression& {
  49. return std::get<PrimitiveOperatorExpression>(value);
  50. }
  51. auto Expression::GetCallExpression() const -> const CallExpression& {
  52. return std::get<CallExpression>(value);
  53. }
  54. auto Expression::GetFunctionTypeLiteral() const -> const FunctionTypeLiteral& {
  55. return std::get<FunctionTypeLiteral>(value);
  56. }
  57. auto Expression::MakeTypeTypeLiteral(int line_num) -> const Expression* {
  58. auto* t = global_arena->New<Expression>();
  59. t->line_num = line_num;
  60. t->value = TypeTypeLiteral();
  61. return t;
  62. }
  63. auto Expression::MakeIntTypeLiteral(int line_num) -> const Expression* {
  64. auto* t = global_arena->New<Expression>();
  65. t->line_num = line_num;
  66. t->value = IntTypeLiteral();
  67. return t;
  68. }
  69. auto Expression::MakeBoolTypeLiteral(int line_num) -> const Expression* {
  70. auto* t = global_arena->New<Expression>();
  71. t->line_num = line_num;
  72. t->value = BoolTypeLiteral();
  73. return t;
  74. }
  75. // Returns a Continuation type AST node at the given source location.
  76. auto Expression::MakeContinuationTypeLiteral(int line_num)
  77. -> const Expression* {
  78. auto* type = global_arena->New<Expression>();
  79. type->line_num = line_num;
  80. type->value = ContinuationTypeLiteral();
  81. return type;
  82. }
  83. auto Expression::MakeFunctionTypeLiteral(int line_num,
  84. const Expression* parameter,
  85. const Expression* return_type,
  86. bool is_omitted_return_type)
  87. -> const Expression* {
  88. auto* t = global_arena->New<Expression>();
  89. t->line_num = line_num;
  90. t->value =
  91. FunctionTypeLiteral({.parameter = parameter,
  92. .return_type = return_type,
  93. .is_omitted_return_type = is_omitted_return_type});
  94. return t;
  95. }
  96. auto Expression::MakeIdentifierExpression(int line_num, std::string var)
  97. -> const Expression* {
  98. auto* v = global_arena->New<Expression>();
  99. v->line_num = line_num;
  100. v->value = IdentifierExpression({.name = std::move(var)});
  101. return v;
  102. }
  103. auto Expression::MakeIntLiteral(int line_num, int i) -> const Expression* {
  104. auto* e = global_arena->New<Expression>();
  105. e->line_num = line_num;
  106. e->value = IntLiteral({.value = i});
  107. return e;
  108. }
  109. auto Expression::MakeBoolLiteral(int line_num, bool b) -> const Expression* {
  110. auto* e = global_arena->New<Expression>();
  111. e->line_num = line_num;
  112. e->value = BoolLiteral({.value = b});
  113. return e;
  114. }
  115. auto Expression::MakePrimitiveOperatorExpression(
  116. int line_num, enum Operator op, std::vector<const Expression*> args)
  117. -> const Expression* {
  118. auto* e = global_arena->New<Expression>();
  119. e->line_num = line_num;
  120. e->value =
  121. PrimitiveOperatorExpression({.op = op, .arguments = std::move(args)});
  122. return e;
  123. }
  124. auto Expression::MakeCallExpression(int line_num, const Expression* fun,
  125. const Expression* arg)
  126. -> const Expression* {
  127. auto* e = global_arena->New<Expression>();
  128. e->line_num = line_num;
  129. e->value = CallExpression({.function = fun, .argument = arg});
  130. return e;
  131. }
  132. auto Expression::MakeFieldAccessExpression(int line_num, const Expression* exp,
  133. std::string field)
  134. -> const Expression* {
  135. auto* e = global_arena->New<Expression>();
  136. e->line_num = line_num;
  137. e->value =
  138. FieldAccessExpression({.aggregate = exp, .field = std::move(field)});
  139. return e;
  140. }
  141. auto Expression::MakeTupleLiteral(int line_num,
  142. std::vector<FieldInitializer> args)
  143. -> const Expression* {
  144. auto* e = global_arena->New<Expression>();
  145. e->line_num = line_num;
  146. e->value = TupleLiteral({.fields = std::move(args)});
  147. return e;
  148. }
  149. auto Expression::MakeIndexExpression(int line_num, const Expression* exp,
  150. const Expression* i) -> const Expression* {
  151. auto* e = global_arena->New<Expression>();
  152. e->line_num = line_num;
  153. e->value = IndexExpression({.aggregate = exp, .offset = i});
  154. return e;
  155. }
  156. static void PrintOp(llvm::raw_ostream& out, Operator op) {
  157. switch (op) {
  158. case Operator::Add:
  159. out << "+";
  160. break;
  161. case Operator::Neg:
  162. case Operator::Sub:
  163. out << "-";
  164. break;
  165. case Operator::Mul:
  166. case Operator::Deref:
  167. case Operator::Ptr:
  168. out << "*";
  169. break;
  170. case Operator::Not:
  171. out << "not";
  172. break;
  173. case Operator::And:
  174. out << "and";
  175. break;
  176. case Operator::Or:
  177. out << "or";
  178. break;
  179. case Operator::Eq:
  180. out << "==";
  181. break;
  182. }
  183. }
  184. static void PrintFields(llvm::raw_ostream& out,
  185. const std::vector<FieldInitializer>& fields) {
  186. llvm::ListSeparator sep;
  187. for (const auto& field : fields) {
  188. out << sep << field.name << " = " << field.expression;
  189. }
  190. }
  191. void Expression::Print(llvm::raw_ostream& out) const {
  192. switch (tag()) {
  193. case ExpressionKind::IndexExpression:
  194. out << *GetIndexExpression().aggregate << "["
  195. << *GetIndexExpression().offset << "]";
  196. break;
  197. case ExpressionKind::FieldAccessExpression:
  198. out << *GetFieldAccessExpression().aggregate << "."
  199. << GetFieldAccessExpression().field;
  200. break;
  201. case ExpressionKind::TupleLiteral:
  202. out << "(";
  203. PrintFields(out, GetTupleLiteral().fields);
  204. out << ")";
  205. break;
  206. case ExpressionKind::IntLiteral:
  207. out << GetIntLiteral();
  208. break;
  209. case ExpressionKind::BoolLiteral:
  210. out << (GetBoolLiteral() ? "true" : "false");
  211. break;
  212. case ExpressionKind::PrimitiveOperatorExpression: {
  213. out << "(";
  214. PrimitiveOperatorExpression op = GetPrimitiveOperatorExpression();
  215. if (op.arguments.size() == 0) {
  216. PrintOp(out, op.op);
  217. } else if (op.arguments.size() == 1) {
  218. PrintOp(out, op.op);
  219. out << " " << *op.arguments[0];
  220. } else if (op.arguments.size() == 2) {
  221. out << *op.arguments[0] << " ";
  222. PrintOp(out, op.op);
  223. out << " " << *op.arguments[1];
  224. }
  225. out << ")";
  226. break;
  227. }
  228. case ExpressionKind::IdentifierExpression:
  229. out << GetIdentifierExpression().name;
  230. break;
  231. case ExpressionKind::CallExpression:
  232. out << *GetCallExpression().function;
  233. if (GetCallExpression().argument->tag() == ExpressionKind::TupleLiteral) {
  234. out << *GetCallExpression().argument;
  235. } else {
  236. out << "(" << *GetCallExpression().argument << ")";
  237. }
  238. break;
  239. case ExpressionKind::BoolTypeLiteral:
  240. out << "Bool";
  241. break;
  242. case ExpressionKind::IntTypeLiteral:
  243. out << "i32";
  244. break;
  245. case ExpressionKind::TypeTypeLiteral:
  246. out << "Type";
  247. break;
  248. case ExpressionKind::ContinuationTypeLiteral:
  249. out << "Continuation";
  250. break;
  251. case ExpressionKind::FunctionTypeLiteral:
  252. out << "fn " << *GetFunctionTypeLiteral().parameter << " -> "
  253. << *GetFunctionTypeLiteral().return_type;
  254. break;
  255. }
  256. }
  257. } // namespace Carbon