expression.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. // The value category of a Carbon expression indicates whether it evaluates
  22. // to a variable or a value. A variable can be mutated, and can have its
  23. // address taken, whereas a value cannot.
  24. enum class ValueCategory {
  25. // A variable. This roughly corresponds to a C/C++ lvalue.
  26. Var,
  27. // A value. This roughly corresponds to a C/C++ rvalue.
  28. Let,
  29. };
  30. ~Expression() override = 0;
  31. void Print(llvm::raw_ostream& out) const override;
  32. static auto classof(const AstNode* node) {
  33. return InheritsFromExpression(node->kind());
  34. }
  35. // Returns the enumerator corresponding to the most-derived type of this
  36. // object.
  37. auto kind() const -> ExpressionKind {
  38. return static_cast<ExpressionKind>(root_kind());
  39. }
  40. // The static type of this expression. Cannot be called before typechecking.
  41. auto static_type() const -> const Value& { return **static_type_; }
  42. // Sets the static type of this expression. Can only be called once, during
  43. // typechecking.
  44. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  45. // Returns whether the static type has been set. Should only be called
  46. // during typechecking: before typechecking it's guaranteed to be false,
  47. // and after typechecking it's guaranteed to be true.
  48. auto has_static_type() const -> bool { return static_type_.has_value(); }
  49. // The value category of this expression. Cannot be called before
  50. // typechecking.
  51. auto value_category() const -> ValueCategory { return *value_category_; }
  52. // Sets the value category of this expression. Can be called multiple times,
  53. // but the argument must have the same value each time.
  54. void set_value_category(ValueCategory value_category) {
  55. CHECK(!value_category_.has_value() || value_category == *value_category_);
  56. value_category_ = value_category;
  57. }
  58. protected:
  59. // Constructs an Expression representing syntax at the given line number.
  60. // `kind` must be the enumerator corresponding to the most-derived type being
  61. // constructed.
  62. Expression() = default;
  63. private:
  64. std::optional<Nonnull<const Value*>> static_type_;
  65. std::optional<ValueCategory> value_category_;
  66. };
  67. // A FieldInitializer represents the initialization of a single 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 -> const Expression& { return *expression_; }
  74. auto expression() -> 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. // Returns the lexical representation of `op`, such as "+" for `Add`.
  94. auto ToString(Operator op) -> std::string_view;
  95. class IdentifierExpression : public Expression {
  96. public:
  97. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  98. : AstNode(AstNodeKind::IdentifierExpression, source_loc),
  99. name_(std::move(name)) {}
  100. static auto classof(const AstNode* node) -> bool {
  101. return InheritsFromIdentifierExpression(node->kind());
  102. }
  103. auto name() const -> const std::string& { return name_; }
  104. private:
  105. std::string name_;
  106. };
  107. class FieldAccessExpression : public Expression {
  108. public:
  109. explicit FieldAccessExpression(SourceLocation source_loc,
  110. Nonnull<Expression*> aggregate,
  111. std::string field)
  112. : AstNode(AstNodeKind::FieldAccessExpression, source_loc),
  113. aggregate_(aggregate),
  114. field_(std::move(field)) {}
  115. static auto classof(const AstNode* node) -> bool {
  116. return InheritsFromFieldAccessExpression(node->kind());
  117. }
  118. auto aggregate() const -> const Expression& { return *aggregate_; }
  119. auto aggregate() -> Expression& { return *aggregate_; }
  120. auto field() const -> const std::string& { return field_; }
  121. private:
  122. Nonnull<Expression*> aggregate_;
  123. std::string field_;
  124. };
  125. class IndexExpression : public Expression {
  126. public:
  127. explicit IndexExpression(SourceLocation source_loc,
  128. Nonnull<Expression*> aggregate,
  129. Nonnull<Expression*> offset)
  130. : AstNode(AstNodeKind::IndexExpression, source_loc),
  131. aggregate_(aggregate),
  132. offset_(offset) {}
  133. static auto classof(const AstNode* node) -> bool {
  134. return InheritsFromIndexExpression(node->kind());
  135. }
  136. auto aggregate() const -> const Expression& { return *aggregate_; }
  137. auto aggregate() -> Expression& { return *aggregate_; }
  138. auto offset() const -> const Expression& { return *offset_; }
  139. auto offset() -> Expression& { return *offset_; }
  140. private:
  141. Nonnull<Expression*> aggregate_;
  142. Nonnull<Expression*> offset_;
  143. };
  144. class IntLiteral : public Expression {
  145. public:
  146. explicit IntLiteral(SourceLocation source_loc, int value)
  147. : AstNode(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  148. static auto classof(const AstNode* node) -> bool {
  149. return InheritsFromIntLiteral(node->kind());
  150. }
  151. auto value() const -> int { return value_; }
  152. private:
  153. int value_;
  154. };
  155. class BoolLiteral : public Expression {
  156. public:
  157. explicit BoolLiteral(SourceLocation source_loc, bool value)
  158. : AstNode(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  159. static auto classof(const AstNode* node) -> bool {
  160. return InheritsFromBoolLiteral(node->kind());
  161. }
  162. auto value() const -> bool { return value_; }
  163. private:
  164. bool value_;
  165. };
  166. class StringLiteral : public Expression {
  167. public:
  168. explicit StringLiteral(SourceLocation source_loc, std::string value)
  169. : AstNode(AstNodeKind::StringLiteral, source_loc),
  170. value_(std::move(value)) {}
  171. static auto classof(const AstNode* node) -> bool {
  172. return InheritsFromStringLiteral(node->kind());
  173. }
  174. auto value() const -> const std::string& { return value_; }
  175. private:
  176. std::string value_;
  177. };
  178. class StringTypeLiteral : public Expression {
  179. public:
  180. explicit StringTypeLiteral(SourceLocation source_loc)
  181. : AstNode(AstNodeKind::StringTypeLiteral, source_loc) {}
  182. static auto classof(const AstNode* node) -> bool {
  183. return InheritsFromStringTypeLiteral(node->kind());
  184. }
  185. };
  186. class TupleLiteral : public Expression {
  187. public:
  188. explicit TupleLiteral(SourceLocation source_loc)
  189. : TupleLiteral(source_loc, {}) {}
  190. explicit TupleLiteral(SourceLocation source_loc,
  191. std::vector<Nonnull<Expression*>> fields)
  192. : AstNode(AstNodeKind::TupleLiteral, source_loc),
  193. fields_(std::move(fields)) {}
  194. static auto classof(const AstNode* node) -> bool {
  195. return InheritsFromTupleLiteral(node->kind());
  196. }
  197. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  198. return fields_;
  199. }
  200. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  201. private:
  202. std::vector<Nonnull<Expression*>> fields_;
  203. };
  204. // A non-empty literal value of a struct type.
  205. //
  206. // It can't be empty because the syntax `{}` is a struct type literal as well
  207. // as a literal value of that type, so for consistency we always represent it
  208. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  209. // the two.
  210. class StructLiteral : public Expression {
  211. public:
  212. explicit StructLiteral(SourceLocation loc,
  213. std::vector<FieldInitializer> fields)
  214. : AstNode(AstNodeKind::StructLiteral, loc), fields_(std::move(fields)) {
  215. CHECK(!fields_.empty())
  216. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  217. }
  218. static auto classof(const AstNode* node) -> bool {
  219. return InheritsFromStructLiteral(node->kind());
  220. }
  221. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  222. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  223. private:
  224. std::vector<FieldInitializer> fields_;
  225. };
  226. // A literal representing a struct type.
  227. //
  228. // Code that handles this type may sometimes need to have special-case handling
  229. // for `{}`, which is a struct value in addition to being a struct type.
  230. class StructTypeLiteral : public Expression {
  231. public:
  232. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  233. explicit StructTypeLiteral(SourceLocation loc,
  234. std::vector<FieldInitializer> fields)
  235. : AstNode(AstNodeKind::StructTypeLiteral, loc),
  236. fields_(std::move(fields)) {}
  237. static auto classof(const AstNode* node) -> bool {
  238. return InheritsFromStructTypeLiteral(node->kind());
  239. }
  240. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  241. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  242. private:
  243. std::vector<FieldInitializer> fields_;
  244. };
  245. class PrimitiveOperatorExpression : public Expression {
  246. public:
  247. explicit PrimitiveOperatorExpression(
  248. SourceLocation source_loc, Operator op,
  249. std::vector<Nonnull<Expression*>> arguments)
  250. : AstNode(AstNodeKind::PrimitiveOperatorExpression, source_loc),
  251. op_(op),
  252. arguments_(std::move(arguments)) {}
  253. static auto classof(const AstNode* node) -> bool {
  254. return InheritsFromPrimitiveOperatorExpression(node->kind());
  255. }
  256. auto op() const -> Operator { return op_; }
  257. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  258. return arguments_;
  259. }
  260. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  261. return arguments_;
  262. }
  263. private:
  264. Operator op_;
  265. std::vector<Nonnull<Expression*>> arguments_;
  266. };
  267. class CallExpression : public Expression {
  268. public:
  269. explicit CallExpression(SourceLocation source_loc,
  270. Nonnull<Expression*> function,
  271. Nonnull<Expression*> argument)
  272. : AstNode(AstNodeKind::CallExpression, source_loc),
  273. function_(function),
  274. argument_(argument) {}
  275. static auto classof(const AstNode* node) -> bool {
  276. return InheritsFromCallExpression(node->kind());
  277. }
  278. auto function() const -> const Expression& { return *function_; }
  279. auto function() -> Expression& { return *function_; }
  280. auto argument() const -> const Expression& { return *argument_; }
  281. auto argument() -> Expression& { return *argument_; }
  282. private:
  283. Nonnull<Expression*> function_;
  284. Nonnull<Expression*> argument_;
  285. };
  286. class FunctionTypeLiteral : public Expression {
  287. public:
  288. explicit FunctionTypeLiteral(SourceLocation source_loc,
  289. Nonnull<Expression*> parameter,
  290. Nonnull<Expression*> return_type)
  291. : AstNode(AstNodeKind::FunctionTypeLiteral, source_loc),
  292. parameter_(parameter),
  293. return_type_(return_type) {}
  294. static auto classof(const AstNode* node) -> bool {
  295. return InheritsFromFunctionTypeLiteral(node->kind());
  296. }
  297. auto parameter() const -> const Expression& { return *parameter_; }
  298. auto parameter() -> Expression& { return *parameter_; }
  299. auto return_type() const -> const Expression& { return *return_type_; }
  300. auto return_type() -> Expression& { return *return_type_; }
  301. private:
  302. Nonnull<Expression*> parameter_;
  303. Nonnull<Expression*> return_type_;
  304. };
  305. class BoolTypeLiteral : public Expression {
  306. public:
  307. explicit BoolTypeLiteral(SourceLocation source_loc)
  308. : AstNode(AstNodeKind::BoolTypeLiteral, source_loc) {}
  309. static auto classof(const AstNode* node) -> bool {
  310. return InheritsFromBoolTypeLiteral(node->kind());
  311. }
  312. };
  313. class IntTypeLiteral : public Expression {
  314. public:
  315. explicit IntTypeLiteral(SourceLocation source_loc)
  316. : AstNode(AstNodeKind::IntTypeLiteral, source_loc) {}
  317. static auto classof(const AstNode* node) -> bool {
  318. return InheritsFromIntTypeLiteral(node->kind());
  319. }
  320. };
  321. class ContinuationTypeLiteral : public Expression {
  322. public:
  323. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  324. : AstNode(AstNodeKind::ContinuationTypeLiteral, source_loc) {}
  325. static auto classof(const AstNode* node) -> bool {
  326. return InheritsFromContinuationTypeLiteral(node->kind());
  327. }
  328. };
  329. class TypeTypeLiteral : public Expression {
  330. public:
  331. explicit TypeTypeLiteral(SourceLocation source_loc)
  332. : AstNode(AstNodeKind::TypeTypeLiteral, source_loc) {}
  333. static auto classof(const AstNode* node) -> bool {
  334. return InheritsFromTypeTypeLiteral(node->kind());
  335. }
  336. };
  337. class IntrinsicExpression : public Expression {
  338. public:
  339. enum class Intrinsic {
  340. Print,
  341. };
  342. explicit IntrinsicExpression(std::string_view intrinsic_name,
  343. Nonnull<TupleLiteral*> args,
  344. SourceLocation source_loc)
  345. : AstNode(AstNodeKind::IntrinsicExpression, source_loc),
  346. intrinsic_(FindIntrinsic(intrinsic_name, source_loc)),
  347. args_(args) {}
  348. static auto classof(const AstNode* node) -> bool {
  349. return InheritsFromIntrinsicExpression(node->kind());
  350. }
  351. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  352. auto args() const -> const TupleLiteral& { return *args_; }
  353. auto args() -> TupleLiteral& { return *args_; }
  354. private:
  355. // Returns the enumerator corresponding to the intrinsic named `name`,
  356. // or raises a fatal compile error if there is no such enumerator.
  357. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  358. -> Intrinsic;
  359. Intrinsic intrinsic_;
  360. Nonnull<TupleLiteral*> args_;
  361. };
  362. // An expression whose semantics have not been implemented. This can be used
  363. // as a placeholder during development, in order to implement and test parsing
  364. // of a new expression syntax without having to implement its semantics.
  365. class UnimplementedExpression : public Expression {
  366. public:
  367. // Constructs an UnimplementedExpression with the given label and the given
  368. // children, which must all be convertible to Nonnull<AstNode*>. The label
  369. // should correspond roughly to the name of the class that will eventually
  370. // replace this usage of UnimplementedExpression.
  371. template <typename... Children>
  372. UnimplementedExpression(SourceLocation source_loc, std::string label,
  373. Children... children)
  374. : AstNode(AstNodeKind::UnimplementedExpression, source_loc),
  375. label_(std::move(label)) {
  376. AddChildren(children...);
  377. }
  378. static auto classof(const AstNode* node) -> bool {
  379. return InheritsFromUnimplementedExpression(node->kind());
  380. }
  381. auto label() const -> std::string_view { return label_; }
  382. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  383. return children_;
  384. }
  385. private:
  386. void AddChildren() {}
  387. template <typename... Children>
  388. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  389. children_.push_back(child);
  390. AddChildren(children...);
  391. }
  392. std::string label_;
  393. std::vector<Nonnull<AstNode*>> children_;
  394. };
  395. // Converts paren_contents to an Expression, interpreting the parentheses as
  396. // grouping if their contents permit that interpretation, or as forming a
  397. // tuple otherwise.
  398. auto ExpressionFromParenContents(
  399. Nonnull<Arena*> arena, SourceLocation source_loc,
  400. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  401. // Converts paren_contents to an Expression, interpreting the parentheses as
  402. // forming a tuple.
  403. auto TupleExpressionFromParenContents(
  404. Nonnull<Arena*> arena, SourceLocation source_loc,
  405. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  406. } // namespace Carbon
  407. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_