expression.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #ifndef EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
  5. #define EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_
  6. #include <optional>
  7. #include <string>
  8. #include <variant>
  9. #include <vector>
  10. #include "common/ostream.h"
  11. #include "executable_semantics/ast/paren_contents.h"
  12. #include "executable_semantics/ast/source_location.h"
  13. #include "executable_semantics/common/arena.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace Carbon {
  17. class Expression {
  18. public:
  19. enum class Kind {
  20. BoolTypeLiteral,
  21. BoolLiteral,
  22. CallExpression,
  23. FunctionTypeLiteral,
  24. FieldAccessExpression,
  25. IndexExpression,
  26. IntTypeLiteral,
  27. ContinuationTypeLiteral, // The type of a continuation value.
  28. IntLiteral,
  29. PrimitiveOperatorExpression,
  30. StringLiteral,
  31. StringTypeLiteral,
  32. TupleLiteral,
  33. StructLiteral,
  34. StructTypeLiteral,
  35. TypeTypeLiteral,
  36. IdentifierExpression,
  37. IntrinsicExpression,
  38. };
  39. void Print(llvm::raw_ostream& out) const;
  40. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  41. // Returns the enumerator corresponding to the most-derived type of this
  42. // object.
  43. auto kind() const -> Kind { return kind_; }
  44. auto source_loc() const -> SourceLocation { return source_loc_; }
  45. protected:
  46. // Constructs an Expression representing syntax at the given line number.
  47. // `kind` must be the enumerator corresponding to the most-derived type being
  48. // constructed.
  49. Expression(Kind kind, SourceLocation source_loc)
  50. : kind_(kind), source_loc_(source_loc) {}
  51. private:
  52. const Kind kind_;
  53. SourceLocation source_loc_;
  54. };
  55. // Converts paren_contents to an Expression, interpreting the parentheses as
  56. // grouping if their contents permit that interpretation, or as forming a
  57. // tuple otherwise.
  58. auto ExpressionFromParenContents(
  59. Nonnull<Arena*> arena, SourceLocation source_loc,
  60. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  61. // Converts paren_contents to an Expression, interpreting the parentheses as
  62. // forming a tuple.
  63. auto TupleExpressionFromParenContents(
  64. Nonnull<Arena*> arena, SourceLocation source_loc,
  65. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  66. // A FieldInitializer represents the initialization of a single tuple or
  67. // struct field.
  68. class FieldInitializer {
  69. public:
  70. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  71. : name_(std::move(name)), expression_(expression) {}
  72. auto name() const -> const std::string& { return name_; }
  73. auto expression() const -> Nonnull<const Expression*> { return expression_; }
  74. auto expression() -> Nonnull<Expression*> { return expression_; }
  75. private:
  76. // The field name. Cannot be empty.
  77. std::string name_;
  78. // The expression that initializes the field.
  79. Nonnull<Expression*> expression_;
  80. };
  81. enum class Operator {
  82. Add,
  83. And,
  84. Deref,
  85. Eq,
  86. Mul,
  87. Neg,
  88. Not,
  89. Or,
  90. Sub,
  91. Ptr,
  92. };
  93. class IdentifierExpression : public Expression {
  94. public:
  95. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  96. : Expression(Kind::IdentifierExpression, source_loc),
  97. name(std::move(name)) {}
  98. static auto classof(const Expression* exp) -> bool {
  99. return exp->kind() == Kind::IdentifierExpression;
  100. }
  101. auto Name() const -> const std::string& { return name; }
  102. private:
  103. std::string name;
  104. };
  105. class FieldAccessExpression : public Expression {
  106. public:
  107. explicit FieldAccessExpression(SourceLocation source_loc,
  108. Nonnull<Expression*> aggregate,
  109. std::string field)
  110. : Expression(Kind::FieldAccessExpression, source_loc),
  111. aggregate(aggregate),
  112. field(std::move(field)) {}
  113. static auto classof(const Expression* exp) -> bool {
  114. return exp->kind() == Kind::FieldAccessExpression;
  115. }
  116. auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
  117. auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
  118. auto Field() const -> const std::string& { return field; }
  119. private:
  120. Nonnull<Expression*> aggregate;
  121. std::string field;
  122. };
  123. class IndexExpression : public Expression {
  124. public:
  125. explicit IndexExpression(SourceLocation source_loc,
  126. Nonnull<Expression*> aggregate,
  127. Nonnull<Expression*> offset)
  128. : Expression(Kind::IndexExpression, source_loc),
  129. aggregate(aggregate),
  130. offset(offset) {}
  131. static auto classof(const Expression* exp) -> bool {
  132. return exp->kind() == Kind::IndexExpression;
  133. }
  134. auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
  135. auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
  136. auto Offset() const -> Nonnull<const Expression*> { return offset; }
  137. auto Offset() -> Nonnull<Expression*> { return offset; }
  138. private:
  139. Nonnull<Expression*> aggregate;
  140. Nonnull<Expression*> offset;
  141. };
  142. class IntLiteral : public Expression {
  143. public:
  144. explicit IntLiteral(SourceLocation source_loc, int val)
  145. : Expression(Kind::IntLiteral, source_loc), val(val) {}
  146. static auto classof(const Expression* exp) -> bool {
  147. return exp->kind() == Kind::IntLiteral;
  148. }
  149. auto Val() const -> int { return val; }
  150. private:
  151. int val;
  152. };
  153. class BoolLiteral : public Expression {
  154. public:
  155. explicit BoolLiteral(SourceLocation source_loc, bool val)
  156. : Expression(Kind::BoolLiteral, source_loc), val(val) {}
  157. static auto classof(const Expression* exp) -> bool {
  158. return exp->kind() == Kind::BoolLiteral;
  159. }
  160. auto Val() const -> bool { return val; }
  161. private:
  162. bool val;
  163. };
  164. class StringLiteral : public Expression {
  165. public:
  166. explicit StringLiteral(SourceLocation source_loc, std::string val)
  167. : Expression(Kind::StringLiteral, source_loc), val(std::move(val)) {}
  168. static auto classof(const Expression* exp) -> bool {
  169. return exp->kind() == Kind::StringLiteral;
  170. }
  171. auto Val() const -> const std::string& { return val; }
  172. private:
  173. std::string val;
  174. };
  175. class StringTypeLiteral : public Expression {
  176. public:
  177. explicit StringTypeLiteral(SourceLocation source_loc)
  178. : Expression(Kind::StringTypeLiteral, source_loc) {}
  179. static auto classof(const Expression* exp) -> bool {
  180. return exp->kind() == Kind::StringTypeLiteral;
  181. }
  182. };
  183. class TupleLiteral : public Expression {
  184. public:
  185. explicit TupleLiteral(SourceLocation source_loc)
  186. : TupleLiteral(source_loc, {}) {}
  187. explicit TupleLiteral(SourceLocation source_loc,
  188. std::vector<FieldInitializer> fields)
  189. : Expression(Kind::TupleLiteral, source_loc),
  190. fields_(std::move(fields)) {}
  191. static auto classof(const Expression* exp) -> bool {
  192. return exp->kind() == Kind::TupleLiteral;
  193. }
  194. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  195. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  196. private:
  197. std::vector<FieldInitializer> fields_;
  198. };
  199. // A non-empty literal value of a struct type.
  200. //
  201. // It can't be empty because the syntax `{}` is a struct type literal as well
  202. // as a literal value of that type, so for consistency we always represent it
  203. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  204. // the two.
  205. class StructLiteral : public Expression {
  206. public:
  207. explicit StructLiteral(SourceLocation loc,
  208. std::vector<FieldInitializer> fields)
  209. : Expression(Kind::StructLiteral, loc), fields_(std::move(fields)) {
  210. CHECK(!fields_.empty())
  211. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  212. }
  213. static auto classof(const Expression* exp) -> bool {
  214. return exp->kind() == Kind::StructLiteral;
  215. }
  216. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  217. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  218. private:
  219. std::vector<FieldInitializer> fields_;
  220. };
  221. // A literal representing a struct type.
  222. //
  223. // Code that handles this type may sometimes need to have special-case handling
  224. // for `{}`, which is a struct value in addition to being a struct type.
  225. class StructTypeLiteral : public Expression {
  226. public:
  227. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  228. explicit StructTypeLiteral(SourceLocation loc,
  229. std::vector<FieldInitializer> fields)
  230. : Expression(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {}
  231. static auto classof(const Expression* exp) -> bool {
  232. return exp->kind() == Kind::StructTypeLiteral;
  233. }
  234. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  235. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  236. private:
  237. std::vector<FieldInitializer> fields_;
  238. };
  239. class PrimitiveOperatorExpression : public Expression {
  240. public:
  241. explicit PrimitiveOperatorExpression(
  242. SourceLocation source_loc, Operator op,
  243. std::vector<Nonnull<Expression*>> arguments)
  244. : Expression(Kind::PrimitiveOperatorExpression, source_loc),
  245. op(op),
  246. arguments(std::move(arguments)) {}
  247. static auto classof(const Expression* exp) -> bool {
  248. return exp->kind() == Kind::PrimitiveOperatorExpression;
  249. }
  250. auto Op() const -> Operator { return op; }
  251. auto Arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  252. return arguments;
  253. }
  254. auto Arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  255. return arguments;
  256. }
  257. private:
  258. Operator op;
  259. std::vector<Nonnull<Expression*>> arguments;
  260. };
  261. class CallExpression : public Expression {
  262. public:
  263. explicit CallExpression(SourceLocation source_loc,
  264. Nonnull<Expression*> function,
  265. Nonnull<Expression*> argument)
  266. : Expression(Kind::CallExpression, source_loc),
  267. function(function),
  268. argument(argument) {}
  269. static auto classof(const Expression* exp) -> bool {
  270. return exp->kind() == Kind::CallExpression;
  271. }
  272. auto Function() const -> Nonnull<const Expression*> { return function; }
  273. auto Function() -> Nonnull<Expression*> { return function; }
  274. auto Argument() const -> Nonnull<const Expression*> { return argument; }
  275. auto Argument() -> Nonnull<Expression*> { return argument; }
  276. private:
  277. Nonnull<Expression*> function;
  278. Nonnull<Expression*> argument;
  279. };
  280. class FunctionTypeLiteral : public Expression {
  281. public:
  282. explicit FunctionTypeLiteral(SourceLocation source_loc,
  283. Nonnull<Expression*> parameter,
  284. Nonnull<Expression*> return_type,
  285. bool is_omitted_return_type)
  286. : Expression(Kind::FunctionTypeLiteral, source_loc),
  287. parameter(parameter),
  288. return_type(return_type),
  289. is_omitted_return_type(is_omitted_return_type) {}
  290. static auto classof(const Expression* exp) -> bool {
  291. return exp->kind() == Kind::FunctionTypeLiteral;
  292. }
  293. auto Parameter() const -> Nonnull<const Expression*> { return parameter; }
  294. auto Parameter() -> Nonnull<Expression*> { return parameter; }
  295. auto ReturnType() const -> Nonnull<const Expression*> { return return_type; }
  296. auto ReturnType() -> Nonnull<Expression*> { return return_type; }
  297. auto IsOmittedReturnType() const -> bool { return is_omitted_return_type; }
  298. private:
  299. Nonnull<Expression*> parameter;
  300. Nonnull<Expression*> return_type;
  301. bool is_omitted_return_type;
  302. };
  303. class BoolTypeLiteral : public Expression {
  304. public:
  305. explicit BoolTypeLiteral(SourceLocation source_loc)
  306. : Expression(Kind::BoolTypeLiteral, source_loc) {}
  307. static auto classof(const Expression* exp) -> bool {
  308. return exp->kind() == Kind::BoolTypeLiteral;
  309. }
  310. };
  311. class IntTypeLiteral : public Expression {
  312. public:
  313. explicit IntTypeLiteral(SourceLocation source_loc)
  314. : Expression(Kind::IntTypeLiteral, source_loc) {}
  315. static auto classof(const Expression* exp) -> bool {
  316. return exp->kind() == Kind::IntTypeLiteral;
  317. }
  318. };
  319. class ContinuationTypeLiteral : public Expression {
  320. public:
  321. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  322. : Expression(Kind::ContinuationTypeLiteral, source_loc) {}
  323. static auto classof(const Expression* exp) -> bool {
  324. return exp->kind() == Kind::ContinuationTypeLiteral;
  325. }
  326. };
  327. class TypeTypeLiteral : public Expression {
  328. public:
  329. explicit TypeTypeLiteral(SourceLocation source_loc)
  330. : Expression(Kind::TypeTypeLiteral, source_loc) {}
  331. static auto classof(const Expression* exp) -> bool {
  332. return exp->kind() == Kind::TypeTypeLiteral;
  333. }
  334. };
  335. class IntrinsicExpression : public Expression {
  336. public:
  337. enum class IntrinsicKind {
  338. Print,
  339. };
  340. explicit IntrinsicExpression(IntrinsicKind intrinsic)
  341. : Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
  342. intrinsic(intrinsic) {}
  343. static auto classof(const Expression* exp) -> bool {
  344. return exp->kind() == Kind::IntrinsicExpression;
  345. }
  346. auto Intrinsic() const -> IntrinsicKind { return intrinsic; }
  347. private:
  348. IntrinsicKind intrinsic;
  349. };
  350. } // namespace Carbon
  351. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_