expression.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 <map>
  7. #include <optional>
  8. #include <string>
  9. #include <variant>
  10. #include <vector>
  11. #include "common/ostream.h"
  12. #include "executable_semantics/ast/ast_node.h"
  13. #include "executable_semantics/ast/paren_contents.h"
  14. #include "executable_semantics/ast/static_scope.h"
  15. #include "executable_semantics/ast/value_category.h"
  16. #include "executable_semantics/common/arena.h"
  17. #include "executable_semantics/common/source_location.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. #include "llvm/Support/Compiler.h"
  20. namespace Carbon {
  21. class Value;
  22. class VariableType;
  23. class ImplBinding;
  24. class Expression : public AstNode {
  25. public:
  26. ~Expression() override = 0;
  27. void Print(llvm::raw_ostream& out) const override;
  28. void PrintID(llvm::raw_ostream& out) const override;
  29. static auto classof(const AstNode* node) {
  30. return InheritsFromExpression(node->kind());
  31. }
  32. // Returns the enumerator corresponding to the most-derived type of this
  33. // object.
  34. auto kind() const -> ExpressionKind {
  35. return static_cast<ExpressionKind>(root_kind());
  36. }
  37. // The static type of this expression. Cannot be called before typechecking.
  38. auto static_type() const -> const Value& {
  39. CHECK(static_type_.has_value());
  40. return **static_type_;
  41. }
  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) {
  45. CHECK(!static_type_.has_value());
  46. static_type_ = type;
  47. }
  48. // The value category of this expression. Cannot be called before
  49. // typechecking.
  50. auto value_category() const -> ValueCategory { return *value_category_; }
  51. // Sets the value category of this expression. Can be called multiple times,
  52. // but the argument must have the same value each time.
  53. void set_value_category(ValueCategory value_category) {
  54. CHECK(!value_category_.has_value() || value_category == *value_category_);
  55. value_category_ = value_category;
  56. }
  57. protected:
  58. // Constructs an Expression representing syntax at the given line number.
  59. // `kind` must be the enumerator corresponding to the most-derived type being
  60. // constructed.
  61. Expression(AstNodeKind kind, SourceLocation source_loc)
  62. : AstNode(kind, source_loc) {}
  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. AddressOf,
  84. And,
  85. Deref,
  86. Eq,
  87. Mul,
  88. Neg,
  89. Not,
  90. Or,
  91. Sub,
  92. Ptr,
  93. };
  94. // Returns the lexical representation of `op`, such as "+" for `Add`.
  95. auto ToString(Operator op) -> std::string_view;
  96. class IdentifierExpression : public Expression {
  97. public:
  98. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  99. : Expression(AstNodeKind::IdentifierExpression, source_loc),
  100. name_(std::move(name)) {}
  101. static auto classof(const AstNode* node) -> bool {
  102. return InheritsFromIdentifierExpression(node->kind());
  103. }
  104. auto name() const -> const std::string& { return name_; }
  105. // Returns the ValueNodeView this identifier refers to. Cannot be called
  106. // before name resolution.
  107. auto value_node() const -> const ValueNodeView& { return *value_node_; }
  108. // Sets the value returned by value_node. Can be called only once,
  109. // during name resolution.
  110. void set_value_node(ValueNodeView value_node) {
  111. CHECK(!value_node_.has_value());
  112. value_node_ = std::move(value_node);
  113. }
  114. private:
  115. std::string name_;
  116. std::optional<ValueNodeView> value_node_;
  117. };
  118. class FieldAccessExpression : public Expression {
  119. public:
  120. explicit FieldAccessExpression(SourceLocation source_loc,
  121. Nonnull<Expression*> aggregate,
  122. std::string field)
  123. : Expression(AstNodeKind::FieldAccessExpression, source_loc),
  124. aggregate_(aggregate),
  125. field_(std::move(field)) {}
  126. static auto classof(const AstNode* node) -> bool {
  127. return InheritsFromFieldAccessExpression(node->kind());
  128. }
  129. auto aggregate() const -> const Expression& { return *aggregate_; }
  130. auto aggregate() -> Expression& { return *aggregate_; }
  131. auto field() const -> const std::string& { return field_; }
  132. // If `aggregate` has a generic type, returns the `ImplBinding` that
  133. // identifies its witness table. Otherwise, returns `std::nullopt`. Should not
  134. // be called before typechecking.
  135. auto impl() const -> std::optional<Nonnull<const ImplBinding*>> {
  136. return impl_;
  137. }
  138. // Can only be called once, during typechecking.
  139. void set_impl(Nonnull<const ImplBinding*> impl) {
  140. CHECK(!impl_.has_value());
  141. impl_ = impl;
  142. }
  143. private:
  144. Nonnull<Expression*> aggregate_;
  145. std::string field_;
  146. std::optional<Nonnull<const ImplBinding*>> impl_;
  147. };
  148. class IndexExpression : public Expression {
  149. public:
  150. explicit IndexExpression(SourceLocation source_loc,
  151. Nonnull<Expression*> aggregate,
  152. Nonnull<Expression*> offset)
  153. : Expression(AstNodeKind::IndexExpression, source_loc),
  154. aggregate_(aggregate),
  155. offset_(offset) {}
  156. static auto classof(const AstNode* node) -> bool {
  157. return InheritsFromIndexExpression(node->kind());
  158. }
  159. auto aggregate() const -> const Expression& { return *aggregate_; }
  160. auto aggregate() -> Expression& { return *aggregate_; }
  161. auto offset() const -> const Expression& { return *offset_; }
  162. auto offset() -> Expression& { return *offset_; }
  163. private:
  164. Nonnull<Expression*> aggregate_;
  165. Nonnull<Expression*> offset_;
  166. };
  167. class IntLiteral : public Expression {
  168. public:
  169. explicit IntLiteral(SourceLocation source_loc, int value)
  170. : Expression(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  171. static auto classof(const AstNode* node) -> bool {
  172. return InheritsFromIntLiteral(node->kind());
  173. }
  174. auto value() const -> int { return value_; }
  175. private:
  176. int value_;
  177. };
  178. class BoolLiteral : public Expression {
  179. public:
  180. explicit BoolLiteral(SourceLocation source_loc, bool value)
  181. : Expression(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  182. static auto classof(const AstNode* node) -> bool {
  183. return InheritsFromBoolLiteral(node->kind());
  184. }
  185. auto value() const -> bool { return value_; }
  186. private:
  187. bool value_;
  188. };
  189. class StringLiteral : public Expression {
  190. public:
  191. explicit StringLiteral(SourceLocation source_loc, std::string value)
  192. : Expression(AstNodeKind::StringLiteral, source_loc),
  193. value_(std::move(value)) {}
  194. static auto classof(const AstNode* node) -> bool {
  195. return InheritsFromStringLiteral(node->kind());
  196. }
  197. auto value() const -> const std::string& { return value_; }
  198. private:
  199. std::string value_;
  200. };
  201. class StringTypeLiteral : public Expression {
  202. public:
  203. explicit StringTypeLiteral(SourceLocation source_loc)
  204. : Expression(AstNodeKind::StringTypeLiteral, source_loc) {}
  205. static auto classof(const AstNode* node) -> bool {
  206. return InheritsFromStringTypeLiteral(node->kind());
  207. }
  208. };
  209. class TupleLiteral : public Expression {
  210. public:
  211. explicit TupleLiteral(SourceLocation source_loc)
  212. : TupleLiteral(source_loc, {}) {}
  213. explicit TupleLiteral(SourceLocation source_loc,
  214. std::vector<Nonnull<Expression*>> fields)
  215. : Expression(AstNodeKind::TupleLiteral, source_loc),
  216. fields_(std::move(fields)) {}
  217. static auto classof(const AstNode* node) -> bool {
  218. return InheritsFromTupleLiteral(node->kind());
  219. }
  220. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  221. return fields_;
  222. }
  223. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  224. private:
  225. std::vector<Nonnull<Expression*>> fields_;
  226. };
  227. // A non-empty literal value of a struct type.
  228. //
  229. // It can't be empty because the syntax `{}` is a struct type literal as well
  230. // as a literal value of that type, so for consistency we always represent it
  231. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  232. // the two.
  233. class StructLiteral : public Expression {
  234. public:
  235. explicit StructLiteral(SourceLocation loc,
  236. std::vector<FieldInitializer> fields)
  237. : Expression(AstNodeKind::StructLiteral, loc),
  238. fields_(std::move(fields)) {
  239. CHECK(!fields_.empty())
  240. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  241. }
  242. static auto classof(const AstNode* node) -> bool {
  243. return InheritsFromStructLiteral(node->kind());
  244. }
  245. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  246. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  247. private:
  248. std::vector<FieldInitializer> fields_;
  249. };
  250. // A literal representing a struct type.
  251. //
  252. // Code that handles this type may sometimes need to have special-case handling
  253. // for `{}`, which is a struct value in addition to being a struct type.
  254. class StructTypeLiteral : public Expression {
  255. public:
  256. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  257. explicit StructTypeLiteral(SourceLocation loc,
  258. std::vector<FieldInitializer> fields)
  259. : Expression(AstNodeKind::StructTypeLiteral, loc),
  260. fields_(std::move(fields)) {}
  261. static auto classof(const AstNode* node) -> bool {
  262. return InheritsFromStructTypeLiteral(node->kind());
  263. }
  264. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  265. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  266. private:
  267. std::vector<FieldInitializer> fields_;
  268. };
  269. class PrimitiveOperatorExpression : public Expression {
  270. public:
  271. explicit PrimitiveOperatorExpression(
  272. SourceLocation source_loc, Operator op,
  273. std::vector<Nonnull<Expression*>> arguments)
  274. : Expression(AstNodeKind::PrimitiveOperatorExpression, source_loc),
  275. op_(op),
  276. arguments_(std::move(arguments)) {}
  277. static auto classof(const AstNode* node) -> bool {
  278. return InheritsFromPrimitiveOperatorExpression(node->kind());
  279. }
  280. auto op() const -> Operator { return op_; }
  281. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  282. return arguments_;
  283. }
  284. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  285. return arguments_;
  286. }
  287. private:
  288. Operator op_;
  289. std::vector<Nonnull<Expression*>> arguments_;
  290. };
  291. class GenericBinding;
  292. using BindingMap =
  293. std::map<Nonnull<const GenericBinding*>, Nonnull<const Value*>>;
  294. class CallExpression : public Expression {
  295. public:
  296. explicit CallExpression(SourceLocation source_loc,
  297. Nonnull<Expression*> function,
  298. Nonnull<Expression*> argument)
  299. : Expression(AstNodeKind::CallExpression, source_loc),
  300. function_(function),
  301. argument_(argument) {}
  302. static auto classof(const AstNode* node) -> bool {
  303. return InheritsFromCallExpression(node->kind());
  304. }
  305. auto function() const -> const Expression& { return *function_; }
  306. auto function() -> Expression& { return *function_; }
  307. auto argument() const -> const Expression& { return *argument_; }
  308. auto argument() -> Expression& { return *argument_; }
  309. // Maps each of `function`'s generic parameters to the AST node
  310. // that identifies the witness table for the corresponding argument.
  311. // Should not be called before typechecking, or if `function` is not
  312. // a generic function.
  313. auto impls() const
  314. -> const std::map<Nonnull<const ImplBinding*>, ValueNodeView>& {
  315. return impls_;
  316. }
  317. // Can only be called once, during typechecking.
  318. void set_impls(
  319. const std::map<Nonnull<const ImplBinding*>, ValueNodeView>& impls) {
  320. CHECK(impls_.empty());
  321. impls_ = impls;
  322. }
  323. auto deduced_args() const -> const BindingMap& { return deduced_args_; }
  324. void set_deduced_args(const BindingMap& deduced_args) {
  325. deduced_args_ = deduced_args;
  326. }
  327. private:
  328. Nonnull<Expression*> function_;
  329. Nonnull<Expression*> argument_;
  330. std::map<Nonnull<const ImplBinding*>, ValueNodeView> impls_;
  331. BindingMap deduced_args_;
  332. };
  333. class FunctionTypeLiteral : public Expression {
  334. public:
  335. explicit FunctionTypeLiteral(SourceLocation source_loc,
  336. Nonnull<Expression*> parameter,
  337. Nonnull<Expression*> return_type)
  338. : Expression(AstNodeKind::FunctionTypeLiteral, source_loc),
  339. parameter_(parameter),
  340. return_type_(return_type) {}
  341. static auto classof(const AstNode* node) -> bool {
  342. return InheritsFromFunctionTypeLiteral(node->kind());
  343. }
  344. auto parameter() const -> const Expression& { return *parameter_; }
  345. auto parameter() -> Expression& { return *parameter_; }
  346. auto return_type() const -> const Expression& { return *return_type_; }
  347. auto return_type() -> Expression& { return *return_type_; }
  348. private:
  349. Nonnull<Expression*> parameter_;
  350. Nonnull<Expression*> return_type_;
  351. };
  352. class BoolTypeLiteral : public Expression {
  353. public:
  354. explicit BoolTypeLiteral(SourceLocation source_loc)
  355. : Expression(AstNodeKind::BoolTypeLiteral, source_loc) {}
  356. static auto classof(const AstNode* node) -> bool {
  357. return InheritsFromBoolTypeLiteral(node->kind());
  358. }
  359. };
  360. class IntTypeLiteral : public Expression {
  361. public:
  362. explicit IntTypeLiteral(SourceLocation source_loc)
  363. : Expression(AstNodeKind::IntTypeLiteral, source_loc) {}
  364. static auto classof(const AstNode* node) -> bool {
  365. return InheritsFromIntTypeLiteral(node->kind());
  366. }
  367. };
  368. class ContinuationTypeLiteral : public Expression {
  369. public:
  370. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  371. : Expression(AstNodeKind::ContinuationTypeLiteral, source_loc) {}
  372. static auto classof(const AstNode* node) -> bool {
  373. return InheritsFromContinuationTypeLiteral(node->kind());
  374. }
  375. };
  376. class TypeTypeLiteral : public Expression {
  377. public:
  378. explicit TypeTypeLiteral(SourceLocation source_loc)
  379. : Expression(AstNodeKind::TypeTypeLiteral, source_loc) {}
  380. static auto classof(const AstNode* node) -> bool {
  381. return InheritsFromTypeTypeLiteral(node->kind());
  382. }
  383. };
  384. class IntrinsicExpression : public Expression {
  385. public:
  386. enum class Intrinsic {
  387. Print,
  388. };
  389. // Returns the enumerator corresponding to the intrinsic named `name`,
  390. // or raises a fatal compile error if there is no such enumerator.
  391. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  392. -> ErrorOr<Intrinsic>;
  393. explicit IntrinsicExpression(Intrinsic intrinsic, Nonnull<TupleLiteral*> args,
  394. SourceLocation source_loc)
  395. : Expression(AstNodeKind::IntrinsicExpression, source_loc),
  396. intrinsic_(intrinsic),
  397. args_(args) {}
  398. static auto classof(const AstNode* node) -> bool {
  399. return InheritsFromIntrinsicExpression(node->kind());
  400. }
  401. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  402. auto args() const -> const TupleLiteral& { return *args_; }
  403. auto args() -> TupleLiteral& { return *args_; }
  404. private:
  405. Intrinsic intrinsic_;
  406. Nonnull<TupleLiteral*> args_;
  407. };
  408. class IfExpression : public Expression {
  409. public:
  410. explicit IfExpression(SourceLocation source_loc,
  411. Nonnull<Expression*> condition,
  412. Nonnull<Expression*> then_expression,
  413. Nonnull<Expression*> else_expression)
  414. : Expression(AstNodeKind::IfExpression, source_loc),
  415. condition_(condition),
  416. then_expression_(then_expression),
  417. else_expression_(else_expression) {}
  418. static auto classof(const AstNode* node) -> bool {
  419. return InheritsFromIfExpression(node->kind());
  420. }
  421. auto condition() const -> const Expression& { return *condition_; }
  422. auto condition() -> Expression& { return *condition_; }
  423. auto then_expression() const -> const Expression& {
  424. return *then_expression_;
  425. }
  426. auto then_expression() -> Expression& { return *then_expression_; }
  427. auto else_expression() const -> const Expression& {
  428. return *else_expression_;
  429. }
  430. auto else_expression() -> Expression& { return *else_expression_; }
  431. private:
  432. Nonnull<Expression*> condition_;
  433. Nonnull<Expression*> then_expression_;
  434. Nonnull<Expression*> else_expression_;
  435. };
  436. // An expression whose semantics have not been implemented. This can be used
  437. // as a placeholder during development, in order to implement and test parsing
  438. // of a new expression syntax without having to implement its semantics.
  439. class UnimplementedExpression : public Expression {
  440. public:
  441. // Constructs an UnimplementedExpression with the given label and the given
  442. // children, which must all be convertible to Nonnull<AstNode*>. The label
  443. // should correspond roughly to the name of the class that will eventually
  444. // replace this usage of UnimplementedExpression.
  445. template <typename... Children>
  446. UnimplementedExpression(SourceLocation source_loc, std::string label,
  447. Children... children)
  448. : Expression(AstNodeKind::UnimplementedExpression, source_loc),
  449. label_(std::move(label)) {
  450. AddChildren(children...);
  451. }
  452. static auto classof(const AstNode* node) -> bool {
  453. return InheritsFromUnimplementedExpression(node->kind());
  454. }
  455. auto label() const -> std::string_view { return label_; }
  456. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  457. return children_;
  458. }
  459. private:
  460. void AddChildren() {}
  461. template <typename... Children>
  462. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  463. children_.push_back(child);
  464. AddChildren(children...);
  465. }
  466. std::string label_;
  467. std::vector<Nonnull<AstNode*>> children_;
  468. };
  469. // A literal representing a statically-sized array type.
  470. class ArrayTypeLiteral : public Expression {
  471. public:
  472. // Constructs an array type literal which uses the given expressions to
  473. // represent the element type and size.
  474. ArrayTypeLiteral(SourceLocation source_loc,
  475. Nonnull<Expression*> element_type_expression,
  476. Nonnull<Expression*> size_expression)
  477. : Expression(AstNodeKind::ArrayTypeLiteral, source_loc),
  478. element_type_expression_(element_type_expression),
  479. size_expression_(size_expression) {}
  480. static auto classof(const AstNode* node) -> bool {
  481. return InheritsFromArrayTypeLiteral(node->kind());
  482. }
  483. auto element_type_expression() const -> const Expression& {
  484. return *element_type_expression_;
  485. }
  486. auto element_type_expression() -> Expression& {
  487. return *element_type_expression_;
  488. }
  489. auto size_expression() const -> const Expression& {
  490. return *size_expression_;
  491. }
  492. auto size_expression() -> Expression& { return *size_expression_; }
  493. private:
  494. Nonnull<Expression*> element_type_expression_;
  495. Nonnull<Expression*> size_expression_;
  496. };
  497. // Converts paren_contents to an Expression, interpreting the parentheses as
  498. // grouping if their contents permit that interpretation, or as forming a
  499. // tuple otherwise.
  500. auto ExpressionFromParenContents(
  501. Nonnull<Arena*> arena, SourceLocation source_loc,
  502. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  503. // Converts paren_contents to an Expression, interpreting the parentheses as
  504. // forming a tuple.
  505. auto TupleExpressionFromParenContents(
  506. Nonnull<Arena*> arena, SourceLocation source_loc,
  507. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  508. } // namespace Carbon
  509. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_