expression.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/paren_contents.h"
  12. #include "executable_semantics/ast/source_location.h"
  13. #include "executable_semantics/common/arena.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/Support/Compiler.h"
  16. namespace Carbon {
  17. class Value;
  18. class Expression {
  19. public:
  20. enum class Kind {
  21. BoolTypeLiteral,
  22. BoolLiteral,
  23. CallExpression,
  24. FunctionTypeLiteral,
  25. FieldAccessExpression,
  26. IndexExpression,
  27. IntTypeLiteral,
  28. ContinuationTypeLiteral, // The type of a continuation value.
  29. IntLiteral,
  30. PrimitiveOperatorExpression,
  31. StringLiteral,
  32. StringTypeLiteral,
  33. TupleLiteral,
  34. StructLiteral,
  35. StructTypeLiteral,
  36. TypeTypeLiteral,
  37. IdentifierExpression,
  38. IntrinsicExpression,
  39. };
  40. void Print(llvm::raw_ostream& out) const;
  41. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  42. // Returns the enumerator corresponding to the most-derived type of this
  43. // object.
  44. auto kind() const -> Kind { return kind_; }
  45. auto source_loc() const -> SourceLocation { return source_loc_; }
  46. // The static type of this expression. Cannot be called before typechecking.
  47. auto static_type() const -> const Value& { return **static_type_; }
  48. // Sets the static type of this expression. Can only be called once, during
  49. // typechecking.
  50. void set_static_type(Nonnull<const Value*> type) { static_type_ = type; }
  51. // Returns whether the static type has been set. Should only be called
  52. // during typechecking: before typechecking it's guaranteed to be false,
  53. // and after typechecking it's guaranteed to be true.
  54. auto has_static_type() const -> bool { return static_type_.has_value(); }
  55. protected:
  56. // Constructs an Expression representing syntax at the given line number.
  57. // `kind` must be the enumerator corresponding to the most-derived type being
  58. // constructed.
  59. Expression(Kind kind, SourceLocation source_loc)
  60. : kind_(kind), source_loc_(source_loc) {}
  61. private:
  62. const Kind kind_;
  63. SourceLocation source_loc_;
  64. std::optional<Nonnull<const Value*>> static_type_;
  65. };
  66. // A FieldInitializer represents the initialization of a single struct field.
  67. class FieldInitializer {
  68. public:
  69. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  70. : name_(std::move(name)), expression_(expression) {}
  71. auto name() const -> const std::string& { return name_; }
  72. auto expression() const -> const Expression& { return *expression_; }
  73. auto expression() -> Expression& { return *expression_; }
  74. private:
  75. // The field name. Cannot be empty.
  76. std::string name_;
  77. // The expression that initializes the field.
  78. Nonnull<Expression*> expression_;
  79. };
  80. enum class Operator {
  81. Add,
  82. And,
  83. Deref,
  84. Eq,
  85. Mul,
  86. Neg,
  87. Not,
  88. Or,
  89. Sub,
  90. Ptr,
  91. };
  92. class IdentifierExpression : public Expression {
  93. public:
  94. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  95. : Expression(Kind::IdentifierExpression, source_loc),
  96. name_(std::move(name)) {}
  97. static auto classof(const Expression* exp) -> bool {
  98. return exp->kind() == Kind::IdentifierExpression;
  99. }
  100. auto name() const -> const std::string& { return name_; }
  101. private:
  102. std::string name_;
  103. };
  104. class FieldAccessExpression : public Expression {
  105. public:
  106. explicit FieldAccessExpression(SourceLocation source_loc,
  107. Nonnull<Expression*> aggregate,
  108. std::string field)
  109. : Expression(Kind::FieldAccessExpression, source_loc),
  110. aggregate_(aggregate),
  111. field_(std::move(field)) {}
  112. static auto classof(const Expression* exp) -> bool {
  113. return exp->kind() == Kind::FieldAccessExpression;
  114. }
  115. auto aggregate() const -> const Expression& { return *aggregate_; }
  116. auto aggregate() -> Expression& { return *aggregate_; }
  117. auto field() const -> const std::string& { return field_; }
  118. private:
  119. Nonnull<Expression*> aggregate_;
  120. std::string field_;
  121. };
  122. class IndexExpression : public Expression {
  123. public:
  124. explicit IndexExpression(SourceLocation source_loc,
  125. Nonnull<Expression*> aggregate,
  126. Nonnull<Expression*> offset)
  127. : Expression(Kind::IndexExpression, source_loc),
  128. aggregate_(aggregate),
  129. offset_(offset) {}
  130. static auto classof(const Expression* exp) -> bool {
  131. return exp->kind() == Kind::IndexExpression;
  132. }
  133. auto aggregate() const -> const Expression& { return *aggregate_; }
  134. auto aggregate() -> Expression& { return *aggregate_; }
  135. auto offset() const -> const Expression& { return *offset_; }
  136. auto offset() -> Expression& { return *offset_; }
  137. private:
  138. Nonnull<Expression*> aggregate_;
  139. Nonnull<Expression*> offset_;
  140. };
  141. class IntLiteral : public Expression {
  142. public:
  143. explicit IntLiteral(SourceLocation source_loc, int value)
  144. : Expression(Kind::IntLiteral, source_loc), value_(value) {}
  145. static auto classof(const Expression* exp) -> bool {
  146. return exp->kind() == Kind::IntLiteral;
  147. }
  148. auto value() const -> int { return value_; }
  149. private:
  150. int value_;
  151. };
  152. class BoolLiteral : public Expression {
  153. public:
  154. explicit BoolLiteral(SourceLocation source_loc, bool value)
  155. : Expression(Kind::BoolLiteral, source_loc), value_(value) {}
  156. static auto classof(const Expression* exp) -> bool {
  157. return exp->kind() == Kind::BoolLiteral;
  158. }
  159. auto value() const -> bool { return value_; }
  160. private:
  161. bool value_;
  162. };
  163. class StringLiteral : public Expression {
  164. public:
  165. explicit StringLiteral(SourceLocation source_loc, std::string value)
  166. : Expression(Kind::StringLiteral, source_loc), value_(std::move(value)) {}
  167. static auto classof(const Expression* exp) -> bool {
  168. return exp->kind() == Kind::StringLiteral;
  169. }
  170. auto value() const -> const std::string& { return value_; }
  171. private:
  172. std::string value_;
  173. };
  174. class StringTypeLiteral : public Expression {
  175. public:
  176. explicit StringTypeLiteral(SourceLocation source_loc)
  177. : Expression(Kind::StringTypeLiteral, source_loc) {}
  178. static auto classof(const Expression* exp) -> bool {
  179. return exp->kind() == Kind::StringTypeLiteral;
  180. }
  181. };
  182. class TupleLiteral : public Expression {
  183. public:
  184. explicit TupleLiteral(SourceLocation source_loc)
  185. : TupleLiteral(source_loc, {}) {}
  186. explicit TupleLiteral(SourceLocation source_loc,
  187. std::vector<Nonnull<Expression*>> fields)
  188. : Expression(Kind::TupleLiteral, source_loc),
  189. fields_(std::move(fields)) {}
  190. static auto classof(const Expression* exp) -> bool {
  191. return exp->kind() == Kind::TupleLiteral;
  192. }
  193. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  194. return fields_;
  195. }
  196. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  197. private:
  198. std::vector<Nonnull<Expression*>> fields_;
  199. };
  200. // A non-empty literal value of a struct type.
  201. //
  202. // It can't be empty because the syntax `{}` is a struct type literal as well
  203. // as a literal value of that type, so for consistency we always represent it
  204. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  205. // the two.
  206. class StructLiteral : public Expression {
  207. public:
  208. explicit StructLiteral(SourceLocation loc,
  209. std::vector<FieldInitializer> fields)
  210. : Expression(Kind::StructLiteral, loc), fields_(std::move(fields)) {
  211. CHECK(!fields_.empty())
  212. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  213. }
  214. static auto classof(const Expression* exp) -> bool {
  215. return exp->kind() == Kind::StructLiteral;
  216. }
  217. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  218. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  219. private:
  220. std::vector<FieldInitializer> fields_;
  221. };
  222. // A literal representing a struct type.
  223. //
  224. // Code that handles this type may sometimes need to have special-case handling
  225. // for `{}`, which is a struct value in addition to being a struct type.
  226. class StructTypeLiteral : public Expression {
  227. public:
  228. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  229. explicit StructTypeLiteral(SourceLocation loc,
  230. std::vector<FieldInitializer> fields)
  231. : Expression(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {}
  232. static auto classof(const Expression* exp) -> bool {
  233. return exp->kind() == Kind::StructTypeLiteral;
  234. }
  235. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  236. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  237. private:
  238. std::vector<FieldInitializer> fields_;
  239. };
  240. class PrimitiveOperatorExpression : public Expression {
  241. public:
  242. explicit PrimitiveOperatorExpression(
  243. SourceLocation source_loc, Operator op,
  244. std::vector<Nonnull<Expression*>> arguments)
  245. : Expression(Kind::PrimitiveOperatorExpression, source_loc),
  246. op_(op),
  247. arguments_(std::move(arguments)) {}
  248. static auto classof(const Expression* exp) -> bool {
  249. return exp->kind() == Kind::PrimitiveOperatorExpression;
  250. }
  251. auto op() const -> Operator { return op_; }
  252. auto arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  253. return arguments_;
  254. }
  255. auto arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  256. return arguments_;
  257. }
  258. private:
  259. Operator op_;
  260. std::vector<Nonnull<Expression*>> arguments_;
  261. };
  262. class CallExpression : public Expression {
  263. public:
  264. explicit CallExpression(SourceLocation source_loc,
  265. Nonnull<Expression*> function,
  266. Nonnull<Expression*> argument)
  267. : Expression(Kind::CallExpression, source_loc),
  268. function_(function),
  269. argument_(argument) {}
  270. static auto classof(const Expression* exp) -> bool {
  271. return exp->kind() == Kind::CallExpression;
  272. }
  273. auto function() const -> const Expression& { return *function_; }
  274. auto function() -> Expression& { return *function_; }
  275. auto argument() const -> const Expression& { return *argument_; }
  276. auto argument() -> Expression& { return *argument_; }
  277. private:
  278. Nonnull<Expression*> function_;
  279. Nonnull<Expression*> argument_;
  280. };
  281. class FunctionTypeLiteral : public Expression {
  282. public:
  283. explicit FunctionTypeLiteral(SourceLocation source_loc,
  284. Nonnull<Expression*> parameter,
  285. Nonnull<Expression*> return_type,
  286. bool is_omitted_return_type)
  287. : Expression(Kind::FunctionTypeLiteral, source_loc),
  288. parameter_(parameter),
  289. return_type_(return_type),
  290. is_omitted_return_type_(is_omitted_return_type) {}
  291. static auto classof(const Expression* exp) -> bool {
  292. return exp->kind() == Kind::FunctionTypeLiteral;
  293. }
  294. auto parameter() const -> const Expression& { return *parameter_; }
  295. auto parameter() -> Expression& { return *parameter_; }
  296. auto return_type() const -> const Expression& { return *return_type_; }
  297. auto return_type() -> Expression& { return *return_type_; }
  298. auto is_omitted_return_type() const -> bool {
  299. return is_omitted_return_type_;
  300. }
  301. private:
  302. Nonnull<Expression*> parameter_;
  303. Nonnull<Expression*> return_type_;
  304. bool is_omitted_return_type_;
  305. };
  306. class BoolTypeLiteral : public Expression {
  307. public:
  308. explicit BoolTypeLiteral(SourceLocation source_loc)
  309. : Expression(Kind::BoolTypeLiteral, source_loc) {}
  310. static auto classof(const Expression* exp) -> bool {
  311. return exp->kind() == Kind::BoolTypeLiteral;
  312. }
  313. };
  314. class IntTypeLiteral : public Expression {
  315. public:
  316. explicit IntTypeLiteral(SourceLocation source_loc)
  317. : Expression(Kind::IntTypeLiteral, source_loc) {}
  318. static auto classof(const Expression* exp) -> bool {
  319. return exp->kind() == Kind::IntTypeLiteral;
  320. }
  321. };
  322. class ContinuationTypeLiteral : public Expression {
  323. public:
  324. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  325. : Expression(Kind::ContinuationTypeLiteral, source_loc) {}
  326. static auto classof(const Expression* exp) -> bool {
  327. return exp->kind() == Kind::ContinuationTypeLiteral;
  328. }
  329. };
  330. class TypeTypeLiteral : public Expression {
  331. public:
  332. explicit TypeTypeLiteral(SourceLocation source_loc)
  333. : Expression(Kind::TypeTypeLiteral, source_loc) {}
  334. static auto classof(const Expression* exp) -> bool {
  335. return exp->kind() == Kind::TypeTypeLiteral;
  336. }
  337. };
  338. class IntrinsicExpression : public Expression {
  339. public:
  340. enum class Intrinsic {
  341. Print,
  342. };
  343. explicit IntrinsicExpression(Intrinsic intrinsic)
  344. : Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
  345. intrinsic_(intrinsic) {}
  346. static auto classof(const Expression* exp) -> bool {
  347. return exp->kind() == Kind::IntrinsicExpression;
  348. }
  349. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  350. private:
  351. Intrinsic intrinsic_;
  352. };
  353. // Converts paren_contents to an Expression, interpreting the parentheses as
  354. // grouping if their contents permit that interpretation, or as forming a
  355. // tuple otherwise.
  356. auto ExpressionFromParenContents(
  357. Nonnull<Arena*> arena, SourceLocation source_loc,
  358. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  359. // Converts paren_contents to an Expression, interpreting the parentheses as
  360. // forming a tuple.
  361. auto TupleExpressionFromParenContents(
  362. Nonnull<Arena*> arena, SourceLocation source_loc,
  363. const ParenContents<Expression>& paren_contents) -> Nonnull<TupleLiteral*>;
  364. } // namespace Carbon
  365. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_