expression.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. class IdentifierExpression : public Expression {
  76. public:
  77. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  78. : AstNode(AstNodeKind::IdentifierExpression, source_loc),
  79. name_(std::move(name)) {}
  80. static auto classof(const AstNode* node) -> bool {
  81. return InheritsFromIdentifierExpression(node->kind());
  82. }
  83. auto name() const -> const std::string& { return name_; }
  84. private:
  85. std::string name_;
  86. };
  87. class FieldAccessExpression : public Expression {
  88. public:
  89. explicit FieldAccessExpression(SourceLocation source_loc,
  90. Nonnull<Expression*> aggregate,
  91. std::string field)
  92. : AstNode(AstNodeKind::FieldAccessExpression, source_loc),
  93. aggregate_(aggregate),
  94. field_(std::move(field)) {}
  95. static auto classof(const AstNode* node) -> bool {
  96. return InheritsFromFieldAccessExpression(node->kind());
  97. }
  98. auto aggregate() const -> const Expression& { return *aggregate_; }
  99. auto aggregate() -> Expression& { return *aggregate_; }
  100. auto field() const -> const std::string& { return field_; }
  101. private:
  102. Nonnull<Expression*> aggregate_;
  103. std::string field_;
  104. };
  105. class IndexExpression : public Expression {
  106. public:
  107. explicit IndexExpression(SourceLocation source_loc,
  108. Nonnull<Expression*> aggregate,
  109. Nonnull<Expression*> offset)
  110. : AstNode(AstNodeKind::IndexExpression, source_loc),
  111. aggregate_(aggregate),
  112. offset_(offset) {}
  113. static auto classof(const AstNode* node) -> bool {
  114. return InheritsFromIndexExpression(node->kind());
  115. }
  116. auto aggregate() const -> const Expression& { return *aggregate_; }
  117. auto aggregate() -> Expression& { return *aggregate_; }
  118. auto offset() const -> const Expression& { return *offset_; }
  119. auto offset() -> Expression& { return *offset_; }
  120. private:
  121. Nonnull<Expression*> aggregate_;
  122. Nonnull<Expression*> offset_;
  123. };
  124. class IntLiteral : public Expression {
  125. public:
  126. explicit IntLiteral(SourceLocation source_loc, int value)
  127. : AstNode(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  128. static auto classof(const AstNode* node) -> bool {
  129. return InheritsFromIntLiteral(node->kind());
  130. }
  131. auto value() const -> int { return value_; }
  132. private:
  133. int value_;
  134. };
  135. class BoolLiteral : public Expression {
  136. public:
  137. explicit BoolLiteral(SourceLocation source_loc, bool value)
  138. : AstNode(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  139. static auto classof(const AstNode* node) -> bool {
  140. return InheritsFromBoolLiteral(node->kind());
  141. }
  142. auto value() const -> bool { return value_; }
  143. private:
  144. bool value_;
  145. };
  146. class StringLiteral : public Expression {
  147. public:
  148. explicit StringLiteral(SourceLocation source_loc, std::string value)
  149. : AstNode(AstNodeKind::StringLiteral, source_loc),
  150. value_(std::move(value)) {}
  151. static auto classof(const AstNode* node) -> bool {
  152. return InheritsFromStringLiteral(node->kind());
  153. }
  154. auto value() const -> const std::string& { return value_; }
  155. private:
  156. std::string value_;
  157. };
  158. class StringTypeLiteral : public Expression {
  159. public:
  160. explicit StringTypeLiteral(SourceLocation source_loc)
  161. : AstNode(AstNodeKind::StringTypeLiteral, source_loc) {}
  162. static auto classof(const AstNode* node) -> bool {
  163. return InheritsFromStringTypeLiteral(node->kind());
  164. }
  165. };
  166. class TupleLiteral : public Expression {
  167. public:
  168. explicit TupleLiteral(SourceLocation source_loc)
  169. : TupleLiteral(source_loc, {}) {}
  170. explicit TupleLiteral(SourceLocation source_loc,
  171. std::vector<Nonnull<Expression*>> fields)
  172. : AstNode(AstNodeKind::TupleLiteral, source_loc),
  173. fields_(std::move(fields)) {}
  174. static auto classof(const AstNode* node) -> bool {
  175. return InheritsFromTupleLiteral(node->kind());
  176. }
  177. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  178. return fields_;
  179. }
  180. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  181. private:
  182. std::vector<Nonnull<Expression*>> fields_;
  183. };
  184. // A non-empty literal value of a struct type.
  185. //
  186. // It can't be empty because the syntax `{}` is a struct type literal as well
  187. // as a literal value of that type, so for consistency we always represent it
  188. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  189. // the two.
  190. class StructLiteral : public Expression {
  191. public:
  192. explicit StructLiteral(SourceLocation loc,
  193. std::vector<FieldInitializer> fields)
  194. : AstNode(AstNodeKind::StructLiteral, loc), fields_(std::move(fields)) {
  195. CHECK(!fields_.empty())
  196. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  197. }
  198. static auto classof(const AstNode* node) -> bool {
  199. return InheritsFromStructLiteral(node->kind());
  200. }
  201. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  202. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  203. private:
  204. std::vector<FieldInitializer> fields_;
  205. };
  206. // A literal representing a struct type.
  207. //
  208. // Code that handles this type may sometimes need to have special-case handling
  209. // for `{}`, which is a struct value in addition to being a struct type.
  210. class StructTypeLiteral : public Expression {
  211. public:
  212. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  213. explicit StructTypeLiteral(SourceLocation loc,
  214. std::vector<FieldInitializer> fields)
  215. : AstNode(AstNodeKind::StructTypeLiteral, loc),
  216. fields_(std::move(fields)) {}
  217. static auto classof(const AstNode* node) -> bool {
  218. return InheritsFromStructTypeLiteral(node->kind());
  219. }
  220. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  221. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  222. private:
  223. std::vector<FieldInitializer> fields_;
  224. };
  225. class PrimitiveOperatorExpression : public Expression {
  226. public:
  227. explicit PrimitiveOperatorExpression(
  228. SourceLocation source_loc, Operator op,
  229. std::vector<Nonnull<Expression*>> arguments)
  230. : AstNode(AstNodeKind::PrimitiveOperatorExpression, source_loc),
  231. op_(op),
  232. arguments_(std::move(arguments)) {}
  233. static auto classof(const AstNode* node) -> bool {
  234. return InheritsFromPrimitiveOperatorExpression(node->kind());
  235. }
  236. auto op() const -> Operator { return op_; }
  237. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  238. return arguments_;
  239. }
  240. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  241. return arguments_;
  242. }
  243. private:
  244. Operator op_;
  245. std::vector<Nonnull<Expression*>> arguments_;
  246. };
  247. class CallExpression : public Expression {
  248. public:
  249. explicit CallExpression(SourceLocation source_loc,
  250. Nonnull<Expression*> function,
  251. Nonnull<Expression*> argument)
  252. : AstNode(AstNodeKind::CallExpression, source_loc),
  253. function_(function),
  254. argument_(argument) {}
  255. static auto classof(const AstNode* node) -> bool {
  256. return InheritsFromCallExpression(node->kind());
  257. }
  258. auto function() const -> const Expression& { return *function_; }
  259. auto function() -> Expression& { return *function_; }
  260. auto argument() const -> const Expression& { return *argument_; }
  261. auto argument() -> Expression& { return *argument_; }
  262. private:
  263. Nonnull<Expression*> function_;
  264. Nonnull<Expression*> argument_;
  265. };
  266. class FunctionTypeLiteral : public Expression {
  267. public:
  268. explicit FunctionTypeLiteral(SourceLocation source_loc,
  269. Nonnull<Expression*> parameter,
  270. Nonnull<Expression*> return_type)
  271. : AstNode(AstNodeKind::FunctionTypeLiteral, source_loc),
  272. parameter_(parameter),
  273. return_type_(return_type) {}
  274. static auto classof(const AstNode* node) -> bool {
  275. return InheritsFromFunctionTypeLiteral(node->kind());
  276. }
  277. auto parameter() const -> const Expression& { return *parameter_; }
  278. auto parameter() -> Expression& { return *parameter_; }
  279. auto return_type() const -> const Expression& { return *return_type_; }
  280. auto return_type() -> Expression& { return *return_type_; }
  281. private:
  282. Nonnull<Expression*> parameter_;
  283. Nonnull<Expression*> return_type_;
  284. };
  285. class BoolTypeLiteral : public Expression {
  286. public:
  287. explicit BoolTypeLiteral(SourceLocation source_loc)
  288. : AstNode(AstNodeKind::BoolTypeLiteral, source_loc) {}
  289. static auto classof(const AstNode* node) -> bool {
  290. return InheritsFromBoolTypeLiteral(node->kind());
  291. }
  292. };
  293. class IntTypeLiteral : public Expression {
  294. public:
  295. explicit IntTypeLiteral(SourceLocation source_loc)
  296. : AstNode(AstNodeKind::IntTypeLiteral, source_loc) {}
  297. static auto classof(const AstNode* node) -> bool {
  298. return InheritsFromIntTypeLiteral(node->kind());
  299. }
  300. };
  301. class ContinuationTypeLiteral : public Expression {
  302. public:
  303. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  304. : AstNode(AstNodeKind::ContinuationTypeLiteral, source_loc) {}
  305. static auto classof(const AstNode* node) -> bool {
  306. return InheritsFromContinuationTypeLiteral(node->kind());
  307. }
  308. };
  309. class TypeTypeLiteral : public Expression {
  310. public:
  311. explicit TypeTypeLiteral(SourceLocation source_loc)
  312. : AstNode(AstNodeKind::TypeTypeLiteral, source_loc) {}
  313. static auto classof(const AstNode* node) -> bool {
  314. return InheritsFromTypeTypeLiteral(node->kind());
  315. }
  316. };
  317. class IntrinsicExpression : public Expression {
  318. public:
  319. enum class Intrinsic {
  320. Print,
  321. };
  322. explicit IntrinsicExpression(Intrinsic intrinsic)
  323. : AstNode(AstNodeKind::IntrinsicExpression,
  324. SourceLocation("<intrinsic>", 0)),
  325. intrinsic_(intrinsic) {}
  326. static auto classof(const AstNode* node) -> bool {
  327. return InheritsFromIntrinsicExpression(node->kind());
  328. }
  329. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  330. private:
  331. Intrinsic intrinsic_;
  332. };
  333. // Converts paren_contents to an Expression, interpreting the parentheses as
  334. // grouping if their contents permit that interpretation, or as forming a
  335. // tuple otherwise.
  336. auto ExpressionFromParenContents(
  337. Nonnull<Arena*> arena, SourceLocation source_loc,
  338. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  339. // Converts paren_contents to an Expression, interpreting the parentheses as
  340. // forming a tuple.
  341. auto TupleExpressionFromParenContents(
  342. Nonnull<Arena*> arena, SourceLocation source_loc,
  343. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  344. } // namespace Carbon
  345. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_