expression.h 17 KB

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