expression.cpp 8.8 KB

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