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. // Converts paren_contents to an Expression, interpreting the parentheses as
  67. // grouping if their contents permit that interpretation, or as forming a
  68. // tuple otherwise.
  69. auto ExpressionFromParenContents(
  70. Nonnull<Arena*> arena, SourceLocation source_loc,
  71. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  72. // Converts paren_contents to an Expression, interpreting the parentheses as
  73. // forming a tuple.
  74. auto TupleExpressionFromParenContents(
  75. Nonnull<Arena*> arena, SourceLocation source_loc,
  76. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  77. // A FieldInitializer represents the initialization of a single struct field.
  78. class FieldInitializer {
  79. public:
  80. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  81. : name_(std::move(name)), expression_(expression) {}
  82. auto name() const -> const std::string& { return name_; }
  83. auto expression() const -> const Expression& { return *expression_; }
  84. auto expression() -> Expression& { return *expression_; }
  85. private:
  86. // The field name. Cannot be empty.
  87. std::string name_;
  88. // The expression that initializes the field.
  89. Nonnull<Expression*> expression_;
  90. };
  91. enum class Operator {
  92. Add,
  93. And,
  94. Deref,
  95. Eq,
  96. Mul,
  97. Neg,
  98. Not,
  99. Or,
  100. Sub,
  101. Ptr,
  102. };
  103. class IdentifierExpression : public Expression {
  104. public:
  105. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  106. : Expression(Kind::IdentifierExpression, source_loc),
  107. name_(std::move(name)) {}
  108. static auto classof(const Expression* exp) -> bool {
  109. return exp->kind() == Kind::IdentifierExpression;
  110. }
  111. auto name() const -> const std::string& { return name_; }
  112. private:
  113. std::string name_;
  114. };
  115. class FieldAccessExpression : public Expression {
  116. public:
  117. explicit FieldAccessExpression(SourceLocation source_loc,
  118. Nonnull<Expression*> aggregate,
  119. std::string field)
  120. : Expression(Kind::FieldAccessExpression, source_loc),
  121. aggregate_(aggregate),
  122. field_(std::move(field)) {}
  123. static auto classof(const Expression* exp) -> bool {
  124. return exp->kind() == Kind::FieldAccessExpression;
  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(Kind::IndexExpression, source_loc),
  139. aggregate_(aggregate),
  140. offset_(offset) {}
  141. static auto classof(const Expression* exp) -> bool {
  142. return exp->kind() == Kind::IndexExpression;
  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(Kind::IntLiteral, source_loc), value_(value) {}
  156. static auto classof(const Expression* exp) -> bool {
  157. return exp->kind() == Kind::IntLiteral;
  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(Kind::BoolLiteral, source_loc), value_(value) {}
  167. static auto classof(const Expression* exp) -> bool {
  168. return exp->kind() == Kind::BoolLiteral;
  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(Kind::StringLiteral, source_loc), value_(std::move(value)) {}
  178. static auto classof(const Expression* exp) -> bool {
  179. return exp->kind() == Kind::StringLiteral;
  180. }
  181. auto value() const -> const std::string& { return value_; }
  182. private:
  183. std::string value_;
  184. };
  185. class StringTypeLiteral : public Expression {
  186. public:
  187. explicit StringTypeLiteral(SourceLocation source_loc)
  188. : Expression(Kind::StringTypeLiteral, source_loc) {}
  189. static auto classof(const Expression* exp) -> bool {
  190. return exp->kind() == Kind::StringTypeLiteral;
  191. }
  192. };
  193. class TupleLiteral : public Expression {
  194. public:
  195. explicit TupleLiteral(SourceLocation source_loc)
  196. : TupleLiteral(source_loc, {}) {}
  197. explicit TupleLiteral(SourceLocation source_loc,
  198. std::vector<Nonnull<Expression*>> fields)
  199. : Expression(Kind::TupleLiteral, source_loc),
  200. fields_(std::move(fields)) {}
  201. static auto classof(const Expression* exp) -> bool {
  202. return exp->kind() == Kind::TupleLiteral;
  203. }
  204. auto fields() const -> llvm::ArrayRef<Nonnull<const Expression*>> {
  205. return fields_;
  206. }
  207. auto fields() -> llvm::ArrayRef<Nonnull<Expression*>> { return fields_; }
  208. private:
  209. std::vector<Nonnull<Expression*>> fields_;
  210. };
  211. // A non-empty literal value of a struct type.
  212. //
  213. // It can't be empty because the syntax `{}` is a struct type literal as well
  214. // as a literal value of that type, so for consistency we always represent it
  215. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  216. // the two.
  217. class StructLiteral : public Expression {
  218. public:
  219. explicit StructLiteral(SourceLocation loc,
  220. std::vector<FieldInitializer> fields)
  221. : Expression(Kind::StructLiteral, loc), fields_(std::move(fields)) {
  222. CHECK(!fields_.empty())
  223. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  224. }
  225. static auto classof(const Expression* exp) -> bool {
  226. return exp->kind() == Kind::StructLiteral;
  227. }
  228. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  229. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  230. private:
  231. std::vector<FieldInitializer> fields_;
  232. };
  233. // A literal representing a struct type.
  234. //
  235. // Code that handles this type may sometimes need to have special-case handling
  236. // for `{}`, which is a struct value in addition to being a struct type.
  237. class StructTypeLiteral : public Expression {
  238. public:
  239. explicit StructTypeLiteral(SourceLocation loc) : StructTypeLiteral(loc, {}) {}
  240. explicit StructTypeLiteral(SourceLocation loc,
  241. std::vector<FieldInitializer> fields)
  242. : Expression(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {}
  243. static auto classof(const Expression* exp) -> bool {
  244. return exp->kind() == Kind::StructTypeLiteral;
  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(Kind::PrimitiveOperatorExpression, source_loc),
  257. op_(op),
  258. arguments_(std::move(arguments)) {}
  259. static auto classof(const Expression* exp) -> bool {
  260. return exp->kind() == Kind::PrimitiveOperatorExpression;
  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(Kind::CallExpression, source_loc),
  279. function_(function),
  280. argument_(argument) {}
  281. static auto classof(const Expression* exp) -> bool {
  282. return exp->kind() == Kind::CallExpression;
  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. bool is_omitted_return_type)
  298. : Expression(Kind::FunctionTypeLiteral, source_loc),
  299. parameter_(parameter),
  300. return_type_(return_type),
  301. is_omitted_return_type_(is_omitted_return_type) {}
  302. static auto classof(const Expression* exp) -> bool {
  303. return exp->kind() == Kind::FunctionTypeLiteral;
  304. }
  305. auto parameter() const -> const Expression& { return *parameter_; }
  306. auto parameter() -> Expression& { return *parameter_; }
  307. auto return_type() const -> const Expression& { return *return_type_; }
  308. auto return_type() -> Expression& { return *return_type_; }
  309. auto is_omitted_return_type() const -> bool {
  310. return is_omitted_return_type_;
  311. }
  312. private:
  313. Nonnull<Expression*> parameter_;
  314. Nonnull<Expression*> return_type_;
  315. bool is_omitted_return_type_;
  316. };
  317. class BoolTypeLiteral : public Expression {
  318. public:
  319. explicit BoolTypeLiteral(SourceLocation source_loc)
  320. : Expression(Kind::BoolTypeLiteral, source_loc) {}
  321. static auto classof(const Expression* exp) -> bool {
  322. return exp->kind() == Kind::BoolTypeLiteral;
  323. }
  324. };
  325. class IntTypeLiteral : public Expression {
  326. public:
  327. explicit IntTypeLiteral(SourceLocation source_loc)
  328. : Expression(Kind::IntTypeLiteral, source_loc) {}
  329. static auto classof(const Expression* exp) -> bool {
  330. return exp->kind() == Kind::IntTypeLiteral;
  331. }
  332. };
  333. class ContinuationTypeLiteral : public Expression {
  334. public:
  335. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  336. : Expression(Kind::ContinuationTypeLiteral, source_loc) {}
  337. static auto classof(const Expression* exp) -> bool {
  338. return exp->kind() == Kind::ContinuationTypeLiteral;
  339. }
  340. };
  341. class TypeTypeLiteral : public Expression {
  342. public:
  343. explicit TypeTypeLiteral(SourceLocation source_loc)
  344. : Expression(Kind::TypeTypeLiteral, source_loc) {}
  345. static auto classof(const Expression* exp) -> bool {
  346. return exp->kind() == Kind::TypeTypeLiteral;
  347. }
  348. };
  349. class IntrinsicExpression : public Expression {
  350. public:
  351. enum class Intrinsic {
  352. Print,
  353. };
  354. explicit IntrinsicExpression(Intrinsic intrinsic)
  355. : Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
  356. intrinsic_(intrinsic) {}
  357. static auto classof(const Expression* exp) -> bool {
  358. return exp->kind() == Kind::IntrinsicExpression;
  359. }
  360. auto intrinsic() const -> Intrinsic { return intrinsic_; }
  361. private:
  362. Intrinsic intrinsic_;
  363. };
  364. } // namespace Carbon
  365. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_