expression.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. private:
  108. std::string name_;
  109. std::optional<NamedEntityView> named_entity_;
  110. };
  111. class FieldAccessExpression : public Expression {
  112. public:
  113. explicit FieldAccessExpression(SourceLocation source_loc,
  114. Nonnull<Expression*> aggregate,
  115. std::string field)
  116. : Expression(AstNodeKind::FieldAccessExpression, source_loc),
  117. aggregate_(aggregate),
  118. field_(std::move(field)) {}
  119. static auto classof(const AstNode* node) -> bool {
  120. return InheritsFromFieldAccessExpression(node->kind());
  121. }
  122. auto aggregate() const -> const Expression& { return *aggregate_; }
  123. auto aggregate() -> Expression& { return *aggregate_; }
  124. auto field() const -> const std::string& { return field_; }
  125. private:
  126. Nonnull<Expression*> aggregate_;
  127. std::string field_;
  128. };
  129. class IndexExpression : public Expression {
  130. public:
  131. explicit IndexExpression(SourceLocation source_loc,
  132. Nonnull<Expression*> aggregate,
  133. Nonnull<Expression*> offset)
  134. : Expression(AstNodeKind::IndexExpression, source_loc),
  135. aggregate_(aggregate),
  136. offset_(offset) {}
  137. static auto classof(const AstNode* node) -> bool {
  138. return InheritsFromIndexExpression(node->kind());
  139. }
  140. auto aggregate() const -> const Expression& { return *aggregate_; }
  141. auto aggregate() -> Expression& { return *aggregate_; }
  142. auto offset() const -> const Expression& { return *offset_; }
  143. auto offset() -> Expression& { return *offset_; }
  144. private:
  145. Nonnull<Expression*> aggregate_;
  146. Nonnull<Expression*> offset_;
  147. };
  148. class IntLiteral : public Expression {
  149. public:
  150. explicit IntLiteral(SourceLocation source_loc, int value)
  151. : Expression(AstNodeKind::IntLiteral, source_loc), value_(value) {}
  152. static auto classof(const AstNode* node) -> bool {
  153. return InheritsFromIntLiteral(node->kind());
  154. }
  155. auto value() const -> int { return value_; }
  156. private:
  157. int value_;
  158. };
  159. class BoolLiteral : public Expression {
  160. public:
  161. explicit BoolLiteral(SourceLocation source_loc, bool value)
  162. : Expression(AstNodeKind::BoolLiteral, source_loc), value_(value) {}
  163. static auto classof(const AstNode* node) -> bool {
  164. return InheritsFromBoolLiteral(node->kind());
  165. }
  166. auto value() const -> bool { return value_; }
  167. private:
  168. bool value_;
  169. };
  170. class StringLiteral : public Expression {
  171. public:
  172. explicit StringLiteral(SourceLocation source_loc, std::string value)
  173. : Expression(AstNodeKind::StringLiteral, source_loc),
  174. value_(std::move(value)) {}
  175. static auto classof(const AstNode* node) -> bool {
  176. return InheritsFromStringLiteral(node->kind());
  177. }
  178. auto value() const -> const std::string& { return value_; }
  179. private:
  180. std::string value_;
  181. };
  182. class StringTypeLiteral : public Expression {
  183. public:
  184. explicit StringTypeLiteral(SourceLocation source_loc)
  185. : Expression(AstNodeKind::StringTypeLiteral, source_loc) {}
  186. static auto classof(const AstNode* node) -> bool {
  187. return InheritsFromStringTypeLiteral(node->kind());
  188. }
  189. };
  190. class TupleLiteral : public Expression {
  191. public:
  192. explicit TupleLiteral(SourceLocation source_loc)
  193. : TupleLiteral(source_loc, {}) {}
  194. explicit TupleLiteral(SourceLocation source_loc,
  195. std::vector<Nonnull<Expression*>> fields)
  196. : Expression(AstNodeKind::TupleLiteral, source_loc),
  197. fields_(std::move(fields)) {}
  198. static auto classof(const AstNode* node) -> bool {
  199. return InheritsFromTupleLiteral(node->kind());
  200. }
  201. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  202. return fields_;
  203. }
  204. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  205. private:
  206. std::vector<Nonnull<Expression*>> fields_;
  207. };
  208. // A non-empty literal value of a struct type.
  209. //
  210. // It can't be empty because the syntax `{}` is a struct type literal as well
  211. // as a literal value of that type, so for consistency we always represent it
  212. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  213. // the two.
  214. class StructLiteral : public Expression {
  215. public:
  216. explicit StructLiteral(SourceLocation loc,
  217. std::vector<FieldInitializer> fields)
  218. : Expression(AstNodeKind::StructLiteral, loc),
  219. fields_(std::move(fields)) {
  220. CHECK(!fields_.empty())
  221. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  222. }
  223. static auto classof(const AstNode* node) -> bool {
  224. return InheritsFromStructLiteral(node->kind());
  225. }
  226. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  227. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  228. private:
  229. std::vector<FieldInitializer> fields_;
  230. };
  231. // A literal representing a struct type.
  232. //
  233. // Code that handles this type may sometimes need to have special-case handling
  234. // for `{}`, which is a struct value in addition to being a struct type.
  235. class StructTypeLiteral : public Expression {
  236. public:
  237. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  238. explicit StructTypeLiteral(SourceLocation loc,
  239. std::vector<FieldInitializer> fields)
  240. : Expression(AstNodeKind::StructTypeLiteral, loc),
  241. fields_(std::move(fields)) {}
  242. static auto classof(const AstNode* node) -> bool {
  243. return InheritsFromStructTypeLiteral(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. class PrimitiveOperatorExpression : public Expression {
  251. public:
  252. explicit PrimitiveOperatorExpression(
  253. SourceLocation source_loc, Operator op,
  254. std::vector<Nonnull<Expression*>> arguments)
  255. : Expression(AstNodeKind::PrimitiveOperatorExpression, source_loc),
  256. op_(op),
  257. arguments_(std::move(arguments)) {}
  258. static auto classof(const AstNode* node) -> bool {
  259. return InheritsFromPrimitiveOperatorExpression(node->kind());
  260. }
  261. auto op() const -> Operator { return op_; }
  262. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  263. return arguments_;
  264. }
  265. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  266. return arguments_;
  267. }
  268. private:
  269. Operator op_;
  270. std::vector<Nonnull<Expression*>> arguments_;
  271. };
  272. class CallExpression : public Expression {
  273. public:
  274. explicit CallExpression(SourceLocation source_loc,
  275. Nonnull<Expression*> function,
  276. Nonnull<Expression*> argument)
  277. : Expression(AstNodeKind::CallExpression, source_loc),
  278. function_(function),
  279. argument_(argument) {}
  280. static auto classof(const AstNode* node) -> bool {
  281. return InheritsFromCallExpression(node->kind());
  282. }
  283. auto function() const -> const Expression& { return *function_; }
  284. auto function() -> Expression& { return *function_; }
  285. auto argument() const -> const Expression& { return *argument_; }
  286. auto argument() -> Expression& { return *argument_; }
  287. private:
  288. Nonnull<Expression*> function_;
  289. Nonnull<Expression*> argument_;
  290. };
  291. class FunctionTypeLiteral : public Expression {
  292. public:
  293. explicit FunctionTypeLiteral(SourceLocation source_loc,
  294. Nonnull<Expression*> parameter,
  295. Nonnull<Expression*> return_type)
  296. : Expression(AstNodeKind::FunctionTypeLiteral, source_loc),
  297. parameter_(parameter),
  298. return_type_(return_type) {}
  299. static auto classof(const AstNode* node) -> bool {
  300. return InheritsFromFunctionTypeLiteral(node->kind());
  301. }
  302. auto parameter() const -> const Expression& { return *parameter_; }
  303. auto parameter() -> Expression& { return *parameter_; }
  304. auto return_type() const -> const Expression& { return *return_type_; }
  305. auto return_type() -> Expression& { return *return_type_; }
  306. private:
  307. Nonnull<Expression*> parameter_;
  308. Nonnull<Expression*> return_type_;
  309. };
  310. class BoolTypeLiteral : public Expression {
  311. public:
  312. explicit BoolTypeLiteral(SourceLocation source_loc)
  313. : Expression(AstNodeKind::BoolTypeLiteral, source_loc) {}
  314. static auto classof(const AstNode* node) -> bool {
  315. return InheritsFromBoolTypeLiteral(node->kind());
  316. }
  317. };
  318. class IntTypeLiteral : public Expression {
  319. public:
  320. explicit IntTypeLiteral(SourceLocation source_loc)
  321. : Expression(AstNodeKind::IntTypeLiteral, source_loc) {}
  322. static auto classof(const AstNode* node) -> bool {
  323. return InheritsFromIntTypeLiteral(node->kind());
  324. }
  325. };
  326. class ContinuationTypeLiteral : public Expression {
  327. public:
  328. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  329. : Expression(AstNodeKind::ContinuationTypeLiteral, source_loc) {}
  330. static auto classof(const AstNode* node) -> bool {
  331. return InheritsFromContinuationTypeLiteral(node->kind());
  332. }
  333. };
  334. class TypeTypeLiteral : public Expression {
  335. public:
  336. explicit TypeTypeLiteral(SourceLocation source_loc)
  337. : Expression(AstNodeKind::TypeTypeLiteral, source_loc) {}
  338. static auto classof(const AstNode* node) -> bool {
  339. return InheritsFromTypeTypeLiteral(node->kind());
  340. }
  341. };
  342. class IntrinsicExpression : public Expression {
  343. public:
  344. enum class Intrinsic {
  345. Print,
  346. };
  347. explicit IntrinsicExpression(std::string_view intrinsic_name,
  348. Nonnull<TupleLiteral*> args,
  349. SourceLocation source_loc)
  350. : Expression(AstNodeKind::IntrinsicExpression, source_loc),
  351. intrinsic_(FindIntrinsic(intrinsic_name, source_loc)),
  352. args_(args) {}
  353. static auto classof(const AstNode* node) -> bool {
  354. return InheritsFromIntrinsicExpression(node->kind());
  355. }
  356. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  357. auto args() const -> const TupleLiteral& { return *args_; }
  358. auto args() -> TupleLiteral& { return *args_; }
  359. private:
  360. // Returns the enumerator corresponding to the intrinsic named `name`,
  361. // or raises a fatal compile error if there is no such enumerator.
  362. static auto FindIntrinsic(std::string_view name, SourceLocation source_loc)
  363. -> Intrinsic;
  364. Intrinsic intrinsic_;
  365. Nonnull<TupleLiteral*> args_;
  366. };
  367. // An expression whose semantics have not been implemented. This can be used
  368. // as a placeholder during development, in order to implement and test parsing
  369. // of a new expression syntax without having to implement its semantics.
  370. class UnimplementedExpression : public Expression {
  371. public:
  372. // Constructs an UnimplementedExpression with the given label and the given
  373. // children, which must all be convertible to Nonnull<AstNode*>. The label
  374. // should correspond roughly to the name of the class that will eventually
  375. // replace this usage of UnimplementedExpression.
  376. template <typename... Children>
  377. UnimplementedExpression(SourceLocation source_loc, std::string label,
  378. Children... children)
  379. : Expression(AstNodeKind::UnimplementedExpression, source_loc),
  380. label_(std::move(label)) {
  381. AddChildren(children...);
  382. }
  383. static auto classof(const AstNode* node) -> bool {
  384. return InheritsFromUnimplementedExpression(node->kind());
  385. }
  386. auto label() const -> std::string_view { return label_; }
  387. auto children() const -> llvm::ArrayRef<Nonnull<const AstNode*>> {
  388. return children_;
  389. }
  390. private:
  391. void AddChildren() {}
  392. template <typename... Children>
  393. void AddChildren(Nonnull<AstNode*> child, Children... children) {
  394. children_.push_back(child);
  395. AddChildren(children...);
  396. }
  397. std::string label_;
  398. std::vector<Nonnull<AstNode*>> children_;
  399. };
  400. // Converts paren_contents to an Expression, interpreting the parentheses as
  401. // grouping if their contents permit that interpretation, or as forming a
  402. // tuple otherwise.
  403. auto ExpressionFromParenContents(
  404. Nonnull<Arena*> arena, SourceLocation source_loc,
  405. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  406. // Converts paren_contents to an Expression, interpreting the parentheses as
  407. // forming a tuple.
  408. auto TupleExpressionFromParenContents(
  409. Nonnull<Arena*> arena, SourceLocation source_loc,
  410. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  411. } // namespace Carbon
  412. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_