expression.h 13 KB

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