expression.h 16 KB

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