expression.h 17 KB

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