expression.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "explorer/ast/expression.h"
  5. #include <map>
  6. #include <optional>
  7. #include "explorer/ast/pattern.h"
  8. #include "explorer/common/arena.h"
  9. #include "explorer/common/error_builders.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/Support/Casting.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. namespace Carbon {
  14. using llvm::cast;
  15. using llvm::isa;
  16. auto IntrinsicExpression::FindIntrinsic(std::string_view name,
  17. SourceLocation source_loc)
  18. -> ErrorOr<Intrinsic> {
  19. // TODO: Remove Print special casing once we have variadics or overloads.
  20. if (name == "Print") {
  21. return Intrinsic::Print;
  22. }
  23. static const auto& intrinsic_map = *new std::map<std::string_view, Intrinsic>(
  24. {{"print", Intrinsic::Print},
  25. {"new", Intrinsic::Alloc},
  26. {"delete", Intrinsic::Dealloc},
  27. {"rand", Intrinsic::Rand},
  28. {"int_eq", Intrinsic::IntEq},
  29. {"str_eq", Intrinsic::StrEq}});
  30. name.remove_prefix(std::strlen("__intrinsic_"));
  31. auto it = intrinsic_map.find(name);
  32. if (it == intrinsic_map.end()) {
  33. return CompilationError(source_loc) << "Unknown intrinsic '" << name << "'";
  34. }
  35. return it->second;
  36. }
  37. auto ExpressionFromParenContents(
  38. Nonnull<Arena*> arena, SourceLocation source_loc,
  39. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*> {
  40. std::optional<Nonnull<Expression*>> single_term = paren_contents.SingleTerm();
  41. if (single_term.has_value()) {
  42. return *single_term;
  43. } else {
  44. return TupleExpressionFromParenContents(arena, source_loc, paren_contents);
  45. }
  46. }
  47. auto TupleExpressionFromParenContents(
  48. Nonnull<Arena*> arena, SourceLocation source_loc,
  49. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*> {
  50. return arena->New<TupleLiteral>(source_loc, paren_contents.elements);
  51. }
  52. Expression::~Expression() = default;
  53. auto ToString(Operator op) -> std::string_view {
  54. switch (op) {
  55. case Operator::Add:
  56. return "+";
  57. case Operator::As:
  58. return "as";
  59. case Operator::AddressOf:
  60. case Operator::Combine:
  61. return "&";
  62. case Operator::Neg:
  63. case Operator::Sub:
  64. return "-";
  65. case Operator::Mul:
  66. case Operator::Deref:
  67. case Operator::Ptr:
  68. return "*";
  69. case Operator::Not:
  70. return "not";
  71. case Operator::And:
  72. return "and";
  73. case Operator::Or:
  74. return "or";
  75. case Operator::Eq:
  76. return "==";
  77. case Operator::Mod:
  78. return "%";
  79. }
  80. }
  81. static void PrintFields(llvm::raw_ostream& out,
  82. const std::vector<FieldInitializer>& fields,
  83. std::string_view separator) {
  84. llvm::ListSeparator sep;
  85. for (const auto& field : fields) {
  86. out << sep << "." << field.name() << separator << field.expression();
  87. }
  88. }
  89. void Expression::Print(llvm::raw_ostream& out) const {
  90. switch (kind()) {
  91. case ExpressionKind::IndexExpression: {
  92. const auto& index = cast<IndexExpression>(*this);
  93. out << index.object() << "[" << index.offset() << "]";
  94. break;
  95. }
  96. case ExpressionKind::SimpleMemberAccessExpression: {
  97. const auto& access = cast<SimpleMemberAccessExpression>(*this);
  98. out << access.object() << "." << access.member_name();
  99. break;
  100. }
  101. case ExpressionKind::CompoundMemberAccessExpression: {
  102. const auto& access = cast<CompoundMemberAccessExpression>(*this);
  103. out << access.object() << ".(" << access.path() << ")";
  104. break;
  105. }
  106. case ExpressionKind::TupleLiteral: {
  107. out << "(";
  108. llvm::ListSeparator sep;
  109. for (Nonnull<const Expression*> field :
  110. cast<TupleLiteral>(*this).fields()) {
  111. out << sep << *field;
  112. }
  113. out << ")";
  114. break;
  115. }
  116. case ExpressionKind::StructLiteral:
  117. out << "{";
  118. PrintFields(out, cast<StructLiteral>(*this).fields(), " = ");
  119. out << "}";
  120. break;
  121. case ExpressionKind::StructTypeLiteral:
  122. out << "{";
  123. PrintFields(out, cast<StructTypeLiteral>(*this).fields(), ": ");
  124. out << "}";
  125. break;
  126. case ExpressionKind::OperatorExpression: {
  127. out << "(";
  128. const auto& op = cast<OperatorExpression>(*this);
  129. switch (op.arguments().size()) {
  130. case 0:
  131. out << ToString(op.op());
  132. break;
  133. case 1:
  134. out << ToString(op.op()) << " " << *op.arguments()[0];
  135. break;
  136. case 2:
  137. out << *op.arguments()[0] << " " << ToString(op.op()) << " "
  138. << *op.arguments()[1];
  139. break;
  140. default:
  141. CARBON_FATAL() << "Unexpected argument count: "
  142. << op.arguments().size();
  143. }
  144. out << ")";
  145. break;
  146. }
  147. case ExpressionKind::CallExpression: {
  148. const auto& call = cast<CallExpression>(*this);
  149. out << call.function();
  150. if (isa<TupleLiteral>(call.argument())) {
  151. out << call.argument();
  152. } else {
  153. out << "(" << call.argument() << ")";
  154. }
  155. break;
  156. }
  157. case ExpressionKind::FunctionTypeLiteral: {
  158. const auto& fn = cast<FunctionTypeLiteral>(*this);
  159. out << "fn " << fn.parameter() << " -> " << fn.return_type();
  160. break;
  161. }
  162. case ExpressionKind::IntrinsicExpression: {
  163. const auto& iexp = cast<IntrinsicExpression>(*this);
  164. // TODO: Remove Print special casing once we have variadics or overloads.
  165. if (iexp.intrinsic() == IntrinsicExpression::Intrinsic::Print) {
  166. out << "Print" << iexp.args();
  167. break;
  168. }
  169. out << "intrinsic_";
  170. switch (iexp.intrinsic()) {
  171. case IntrinsicExpression::Intrinsic::Print:
  172. out << "print";
  173. break;
  174. case IntrinsicExpression::Intrinsic::Alloc:
  175. out << "new";
  176. break;
  177. case IntrinsicExpression::Intrinsic::Dealloc:
  178. out << "delete";
  179. break;
  180. case IntrinsicExpression::Intrinsic::Rand:
  181. out << "rand";
  182. break;
  183. case IntrinsicExpression::Intrinsic::IntEq:
  184. out << "int_eq";
  185. break;
  186. case IntrinsicExpression::Intrinsic::StrEq:
  187. out << "str_eq";
  188. }
  189. out << iexp.args();
  190. break;
  191. }
  192. case ExpressionKind::IfExpression: {
  193. const auto& if_expr = cast<IfExpression>(*this);
  194. out << "if " << if_expr.condition() << " then "
  195. << if_expr.then_expression() << " else " << if_expr.else_expression();
  196. break;
  197. }
  198. case ExpressionKind::WhereExpression: {
  199. const auto& where = cast<WhereExpression>(*this);
  200. out << where.self_binding().type() << " where ";
  201. llvm::ListSeparator sep(" and ");
  202. for (const WhereClause* clause : where.clauses()) {
  203. out << sep << *clause;
  204. }
  205. break;
  206. }
  207. case ExpressionKind::InstantiateImpl: {
  208. const auto& inst_impl = cast<InstantiateImpl>(*this);
  209. out << "instantiate " << *inst_impl.generic_impl();
  210. break;
  211. }
  212. case ExpressionKind::UnimplementedExpression: {
  213. const auto& unimplemented = cast<UnimplementedExpression>(*this);
  214. out << "UnimplementedExpression<" << unimplemented.label() << ">(";
  215. llvm::ListSeparator sep;
  216. for (Nonnull<const AstNode*> child : unimplemented.children()) {
  217. out << sep << *child;
  218. }
  219. out << ")";
  220. break;
  221. }
  222. case ExpressionKind::ArrayTypeLiteral: {
  223. const auto& array_literal = cast<ArrayTypeLiteral>(*this);
  224. out << "[" << array_literal.element_type_expression() << "; "
  225. << array_literal.size_expression() << "]";
  226. break;
  227. }
  228. case ExpressionKind::IdentifierExpression:
  229. case ExpressionKind::DotSelfExpression:
  230. case ExpressionKind::IntLiteral:
  231. case ExpressionKind::BoolLiteral:
  232. case ExpressionKind::BoolTypeLiteral:
  233. case ExpressionKind::IntTypeLiteral:
  234. case ExpressionKind::StringLiteral:
  235. case ExpressionKind::StringTypeLiteral:
  236. case ExpressionKind::TypeTypeLiteral:
  237. case ExpressionKind::ContinuationTypeLiteral:
  238. case ExpressionKind::ValueLiteral:
  239. PrintID(out);
  240. break;
  241. }
  242. }
  243. void Expression::PrintID(llvm::raw_ostream& out) const {
  244. switch (kind()) {
  245. case ExpressionKind::IdentifierExpression:
  246. out << cast<IdentifierExpression>(*this).name();
  247. break;
  248. case ExpressionKind::DotSelfExpression:
  249. out << ".Self";
  250. break;
  251. case ExpressionKind::IntLiteral:
  252. out << cast<IntLiteral>(*this).value();
  253. break;
  254. case ExpressionKind::BoolLiteral:
  255. out << (cast<BoolLiteral>(*this).value() ? "true" : "false");
  256. break;
  257. case ExpressionKind::BoolTypeLiteral:
  258. out << "Bool";
  259. break;
  260. case ExpressionKind::IntTypeLiteral:
  261. out << "i32";
  262. break;
  263. case ExpressionKind::StringLiteral:
  264. out << "\"";
  265. out.write_escaped(cast<StringLiteral>(*this).value());
  266. out << "\"";
  267. break;
  268. case ExpressionKind::StringTypeLiteral:
  269. out << "String";
  270. break;
  271. case ExpressionKind::TypeTypeLiteral:
  272. out << "Type";
  273. break;
  274. case ExpressionKind::ContinuationTypeLiteral:
  275. out << "Continuation";
  276. break;
  277. case ExpressionKind::ValueLiteral:
  278. // TODO: For layering reasons, we can't print out the value from here.
  279. out << "ValueLiteral";
  280. break;
  281. case ExpressionKind::IndexExpression:
  282. case ExpressionKind::SimpleMemberAccessExpression:
  283. case ExpressionKind::CompoundMemberAccessExpression:
  284. case ExpressionKind::IfExpression:
  285. case ExpressionKind::WhereExpression:
  286. case ExpressionKind::TupleLiteral:
  287. case ExpressionKind::StructLiteral:
  288. case ExpressionKind::StructTypeLiteral:
  289. case ExpressionKind::CallExpression:
  290. case ExpressionKind::OperatorExpression:
  291. case ExpressionKind::IntrinsicExpression:
  292. case ExpressionKind::UnimplementedExpression:
  293. case ExpressionKind::FunctionTypeLiteral:
  294. case ExpressionKind::ArrayTypeLiteral:
  295. case ExpressionKind::InstantiateImpl:
  296. out << "...";
  297. break;
  298. }
  299. }
  300. WhereClause::~WhereClause() = default;
  301. void WhereClause::Print(llvm::raw_ostream& out) const {
  302. switch (kind()) {
  303. case WhereClauseKind::IsWhereClause: {
  304. auto& clause = cast<IsWhereClause>(*this);
  305. out << clause.type() << " is " << clause.constraint();
  306. break;
  307. }
  308. case WhereClauseKind::EqualsWhereClause: {
  309. auto& clause = cast<EqualsWhereClause>(*this);
  310. out << clause.lhs() << " == " << clause.rhs();
  311. break;
  312. }
  313. }
  314. }
  315. void WhereClause::PrintID(llvm::raw_ostream& out) const { out << "..."; }
  316. } // namespace Carbon