expression.cpp 10 KB

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