expression.cpp 8.8 KB

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