expression.h 17 KB

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