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. namespace Carbon {
  6. auto Expression::GetIdentifierExpression() const
  7. -> const IdentifierExpression& {
  8. return std::get<IdentifierExpression>(value);
  9. }
  10. auto Expression::GetFieldAccessExpression() const
  11. -> const FieldAccessExpression& {
  12. return std::get<FieldAccessExpression>(value);
  13. }
  14. auto Expression::GetIndexExpression() const -> const IndexExpression& {
  15. return std::get<IndexExpression>(value);
  16. }
  17. auto Expression::GetBindingExpression() const -> const BindingExpression& {
  18. return std::get<BindingExpression>(value);
  19. }
  20. auto Expression::GetIntLiteral() const -> int {
  21. return std::get<IntLiteral>(value).value;
  22. }
  23. auto Expression::GetBoolLiteral() const -> bool {
  24. return std::get<BoolLiteral>(value).value;
  25. }
  26. auto Expression::GetTupleLiteral() const -> const TupleLiteral& {
  27. return std::get<TupleLiteral>(value);
  28. }
  29. auto Expression::GetPrimitiveOperatorExpression() const
  30. -> const PrimitiveOperatorExpression& {
  31. return std::get<PrimitiveOperatorExpression>(value);
  32. }
  33. auto Expression::GetCallExpression() const -> const CallExpression& {
  34. return std::get<CallExpression>(value);
  35. }
  36. auto Expression::GetFunctionTypeLiteral() const -> const FunctionTypeLiteral& {
  37. return std::get<FunctionTypeLiteral>(value);
  38. }
  39. auto Expression::MakeTypeTypeLiteral(int line_num) -> const Expression* {
  40. auto* t = new Expression();
  41. t->line_num = line_num;
  42. t->value = TypeTypeLiteral();
  43. return t;
  44. }
  45. auto Expression::MakeIntTypeLiteral(int line_num) -> const Expression* {
  46. auto* t = new Expression();
  47. t->line_num = line_num;
  48. t->value = IntTypeLiteral();
  49. return t;
  50. }
  51. auto Expression::MakeBoolTypeLiteral(int line_num) -> const Expression* {
  52. auto* t = new Expression();
  53. t->line_num = line_num;
  54. t->value = BoolTypeLiteral();
  55. return t;
  56. }
  57. auto Expression::MakeAutoTypeLiteral(int line_num) -> const Expression* {
  58. auto* t = new Expression();
  59. t->line_num = line_num;
  60. t->value = AutoTypeLiteral();
  61. return t;
  62. }
  63. // Returns a Continuation type AST node at the given source location.
  64. auto Expression::MakeContinuationTypeLiteral(int line_num)
  65. -> const Expression* {
  66. auto* type = new Expression();
  67. type->line_num = line_num;
  68. type->value = ContinuationTypeLiteral();
  69. return type;
  70. }
  71. auto Expression::MakeFunctionTypeLiteral(int line_num, const Expression* param,
  72. const Expression* ret)
  73. -> const Expression* {
  74. auto* t = new Expression();
  75. t->line_num = line_num;
  76. t->value = FunctionTypeLiteral({.parameter = param, .return_type = ret});
  77. return t;
  78. }
  79. auto Expression::MakeIdentifierExpression(int line_num, std::string var)
  80. -> const Expression* {
  81. auto* v = new Expression();
  82. v->line_num = line_num;
  83. v->value = IdentifierExpression({.name = std::move(var)});
  84. return v;
  85. }
  86. auto Expression::MakeBindingExpression(int line_num,
  87. std::optional<std::string> var,
  88. const Expression* type)
  89. -> const Expression* {
  90. auto* v = new Expression();
  91. v->line_num = line_num;
  92. v->value = BindingExpression({.name = std::move(var), .type = type});
  93. return v;
  94. }
  95. auto Expression::MakeIntLiteral(int line_num, int i) -> const Expression* {
  96. auto* e = new Expression();
  97. e->line_num = line_num;
  98. e->value = IntLiteral({.value = i});
  99. return e;
  100. }
  101. auto Expression::MakeBoolLiteral(int line_num, bool b) -> const Expression* {
  102. auto* e = new Expression();
  103. e->line_num = line_num;
  104. e->value = BoolLiteral({.value = b});
  105. return e;
  106. }
  107. auto Expression::MakePrimitiveOperatorExpression(
  108. int line_num, enum Operator op, std::vector<const Expression*> args)
  109. -> const Expression* {
  110. auto* e = new Expression();
  111. e->line_num = line_num;
  112. e->value =
  113. PrimitiveOperatorExpression({.op = op, .arguments = std::move(args)});
  114. return e;
  115. }
  116. auto Expression::MakeCallExpression(int line_num, const Expression* fun,
  117. const Expression* arg)
  118. -> const Expression* {
  119. auto* e = new Expression();
  120. e->line_num = line_num;
  121. e->value = CallExpression({.function = fun, .argument = arg});
  122. return e;
  123. }
  124. auto Expression::MakeFieldAccessExpression(int line_num, const Expression* exp,
  125. std::string field)
  126. -> const Expression* {
  127. auto* e = new Expression();
  128. e->line_num = line_num;
  129. e->value =
  130. FieldAccessExpression({.aggregate = exp, .field = std::move(field)});
  131. return e;
  132. }
  133. auto Expression::MakeTupleLiteral(int line_num,
  134. std::vector<FieldInitializer> args)
  135. -> const Expression* {
  136. auto* e = new Expression();
  137. e->line_num = line_num;
  138. int i = 0;
  139. bool seen_named_member = false;
  140. for (auto& arg : args) {
  141. if (arg.name == "") {
  142. if (seen_named_member) {
  143. llvm::errs() << line_num
  144. << ": positional members must come before named members\n";
  145. exit(-1);
  146. }
  147. arg.name = std::to_string(i);
  148. ++i;
  149. } else {
  150. seen_named_member = true;
  151. }
  152. }
  153. e->value = TupleLiteral({.fields = args});
  154. return e;
  155. }
  156. auto Expression::MakeIndexExpression(int line_num, const Expression* exp,
  157. const Expression* i) -> const Expression* {
  158. auto* e = new Expression();
  159. e->line_num = line_num;
  160. e->value = IndexExpression({.aggregate = exp, .offset = i});
  161. return e;
  162. }
  163. static void PrintOp(llvm::raw_ostream& out, Operator op) {
  164. switch (op) {
  165. case Operator::Add:
  166. out << "+";
  167. break;
  168. case Operator::Neg:
  169. case Operator::Sub:
  170. out << "-";
  171. break;
  172. case Operator::Mul:
  173. case Operator::Deref:
  174. case Operator::Ptr:
  175. out << "*";
  176. break;
  177. case Operator::Not:
  178. out << "not";
  179. break;
  180. case Operator::And:
  181. out << "and";
  182. break;
  183. case Operator::Or:
  184. out << "or";
  185. break;
  186. case Operator::Eq:
  187. out << "==";
  188. break;
  189. }
  190. }
  191. static void PrintFields(llvm::raw_ostream& out,
  192. const std::vector<FieldInitializer>& fields) {
  193. int i = 0;
  194. for (auto iter = fields.begin(); iter != fields.end(); ++iter, ++i) {
  195. if (i != 0) {
  196. out << ", ";
  197. }
  198. out << iter->name << " = " << *iter->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