expression.cpp 9.1 KB

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