expression.cpp 9.5 KB

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