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