expression.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 -> Nonnull<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 tuple or
  78. // struct field.
  79. class FieldInitializer {
  80. public:
  81. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  82. : name_(std::move(name)), expression_(expression) {}
  83. auto name() const -> const std::string& { return name_; }
  84. auto expression() const -> Nonnull<const Expression*> { return expression_; }
  85. auto expression() -> Nonnull<Expression*> { return expression_; }
  86. private:
  87. // The field name. Cannot be empty.
  88. std::string name_;
  89. // The expression that initializes the field.
  90. Nonnull<Expression*> expression_;
  91. };
  92. enum class Operator {
  93. Add,
  94. And,
  95. Deref,
  96. Eq,
  97. Mul,
  98. Neg,
  99. Not,
  100. Or,
  101. Sub,
  102. Ptr,
  103. };
  104. class IdentifierExpression : public Expression {
  105. public:
  106. explicit IdentifierExpression(SourceLocation source_loc, std::string name)
  107. : Expression(Kind::IdentifierExpression, source_loc),
  108. name(std::move(name)) {}
  109. static auto classof(const Expression* exp) -> bool {
  110. return exp->kind() == Kind::IdentifierExpression;
  111. }
  112. auto Name() const -> const std::string& { return name; }
  113. private:
  114. std::string name;
  115. };
  116. class FieldAccessExpression : public Expression {
  117. public:
  118. explicit FieldAccessExpression(SourceLocation source_loc,
  119. Nonnull<Expression*> aggregate,
  120. std::string field)
  121. : Expression(Kind::FieldAccessExpression, source_loc),
  122. aggregate(aggregate),
  123. field(std::move(field)) {}
  124. static auto classof(const Expression* exp) -> bool {
  125. return exp->kind() == Kind::FieldAccessExpression;
  126. }
  127. auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
  128. auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
  129. auto Field() const -> const std::string& { return field; }
  130. private:
  131. Nonnull<Expression*> aggregate;
  132. std::string field;
  133. };
  134. class IndexExpression : public Expression {
  135. public:
  136. explicit IndexExpression(SourceLocation source_loc,
  137. Nonnull<Expression*> aggregate,
  138. Nonnull<Expression*> offset)
  139. : Expression(Kind::IndexExpression, source_loc),
  140. aggregate(aggregate),
  141. offset(offset) {}
  142. static auto classof(const Expression* exp) -> bool {
  143. return exp->kind() == Kind::IndexExpression;
  144. }
  145. auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
  146. auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
  147. auto Offset() const -> Nonnull<const Expression*> { return offset; }
  148. auto Offset() -> Nonnull<Expression*> { return offset; }
  149. private:
  150. Nonnull<Expression*> aggregate;
  151. Nonnull<Expression*> offset;
  152. };
  153. class IntLiteral : public Expression {
  154. public:
  155. explicit IntLiteral(SourceLocation source_loc, int val)
  156. : Expression(Kind::IntLiteral, source_loc), val(val) {}
  157. static auto classof(const Expression* exp) -> bool {
  158. return exp->kind() == Kind::IntLiteral;
  159. }
  160. auto Val() const -> int { return val; }
  161. private:
  162. int val;
  163. };
  164. class BoolLiteral : public Expression {
  165. public:
  166. explicit BoolLiteral(SourceLocation source_loc, bool val)
  167. : Expression(Kind::BoolLiteral, source_loc), val(val) {}
  168. static auto classof(const Expression* exp) -> bool {
  169. return exp->kind() == Kind::BoolLiteral;
  170. }
  171. auto Val() const -> bool { return val; }
  172. private:
  173. bool val;
  174. };
  175. class StringLiteral : public Expression {
  176. public:
  177. explicit StringLiteral(SourceLocation source_loc, std::string val)
  178. : Expression(Kind::StringLiteral, source_loc), val(std::move(val)) {}
  179. static auto classof(const Expression* exp) -> bool {
  180. return exp->kind() == Kind::StringLiteral;
  181. }
  182. auto Val() const -> const std::string& { return val; }
  183. private:
  184. std::string val;
  185. };
  186. class StringTypeLiteral : public Expression {
  187. public:
  188. explicit StringTypeLiteral(SourceLocation source_loc)
  189. : Expression(Kind::StringTypeLiteral, source_loc) {}
  190. static auto classof(const Expression* exp) -> bool {
  191. return exp->kind() == Kind::StringTypeLiteral;
  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<FieldInitializer> fields)
  200. : Expression(Kind::TupleLiteral, source_loc),
  201. fields_(std::move(fields)) {}
  202. static auto classof(const Expression* exp) -> bool {
  203. return exp->kind() == Kind::TupleLiteral;
  204. }
  205. auto fields() const -> llvm::ArrayRef<FieldInitializer> { return fields_; }
  206. auto fields() -> llvm::MutableArrayRef<FieldInitializer> { return fields_; }
  207. private:
  208. std::vector<FieldInitializer> fields_;
  209. };
  210. // A non-empty literal value of a struct type.
  211. //
  212. // It can't be empty because the syntax `{}` is a struct type literal as well
  213. // as a literal value of that type, so for consistency we always represent it
  214. // as a StructTypeLiteral rather than let it oscillate unpredictably between
  215. // the two.
  216. class StructLiteral : public Expression {
  217. public:
  218. explicit StructLiteral(SourceLocation loc,
  219. std::vector<FieldInitializer> fields)
  220. : Expression(Kind::StructLiteral, loc), fields_(std::move(fields)) {
  221. CHECK(!fields_.empty())
  222. << "`{}` is represented as a StructTypeLiteral, not a StructLiteral.";
  223. }
  224. static auto classof(const Expression* exp) -> bool {
  225. return exp->kind() == Kind::StructLiteral;
  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(Kind::StructTypeLiteral, loc), fields_(std::move(fields)) {}
  242. static auto classof(const Expression* exp) -> bool {
  243. return exp->kind() == Kind::StructTypeLiteral;
  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(Kind::PrimitiveOperatorExpression, source_loc),
  256. op(op),
  257. arguments(std::move(arguments)) {}
  258. static auto classof(const Expression* exp) -> bool {
  259. return exp->kind() == Kind::PrimitiveOperatorExpression;
  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(Kind::CallExpression, source_loc),
  278. function(function),
  279. argument(argument) {}
  280. static auto classof(const Expression* exp) -> bool {
  281. return exp->kind() == Kind::CallExpression;
  282. }
  283. auto Function() const -> Nonnull<const Expression*> { return function; }
  284. auto Function() -> Nonnull<Expression*> { return function; }
  285. auto Argument() const -> Nonnull<const Expression*> { return argument; }
  286. auto Argument() -> Nonnull<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. bool is_omitted_return_type)
  297. : Expression(Kind::FunctionTypeLiteral, source_loc),
  298. parameter(parameter),
  299. return_type(return_type),
  300. is_omitted_return_type(is_omitted_return_type) {}
  301. static auto classof(const Expression* exp) -> bool {
  302. return exp->kind() == Kind::FunctionTypeLiteral;
  303. }
  304. auto Parameter() const -> Nonnull<const Expression*> { return parameter; }
  305. auto Parameter() -> Nonnull<Expression*> { return parameter; }
  306. auto ReturnType() const -> Nonnull<const Expression*> { return return_type; }
  307. auto ReturnType() -> Nonnull<Expression*> { return return_type; }
  308. auto IsOmittedReturnType() const -> bool { return is_omitted_return_type; }
  309. private:
  310. Nonnull<Expression*> parameter;
  311. Nonnull<Expression*> return_type;
  312. bool is_omitted_return_type;
  313. };
  314. class BoolTypeLiteral : public Expression {
  315. public:
  316. explicit BoolTypeLiteral(SourceLocation source_loc)
  317. : Expression(Kind::BoolTypeLiteral, source_loc) {}
  318. static auto classof(const Expression* exp) -> bool {
  319. return exp->kind() == Kind::BoolTypeLiteral;
  320. }
  321. };
  322. class IntTypeLiteral : public Expression {
  323. public:
  324. explicit IntTypeLiteral(SourceLocation source_loc)
  325. : Expression(Kind::IntTypeLiteral, source_loc) {}
  326. static auto classof(const Expression* exp) -> bool {
  327. return exp->kind() == Kind::IntTypeLiteral;
  328. }
  329. };
  330. class ContinuationTypeLiteral : public Expression {
  331. public:
  332. explicit ContinuationTypeLiteral(SourceLocation source_loc)
  333. : Expression(Kind::ContinuationTypeLiteral, source_loc) {}
  334. static auto classof(const Expression* exp) -> bool {
  335. return exp->kind() == Kind::ContinuationTypeLiteral;
  336. }
  337. };
  338. class TypeTypeLiteral : public Expression {
  339. public:
  340. explicit TypeTypeLiteral(SourceLocation source_loc)
  341. : Expression(Kind::TypeTypeLiteral, source_loc) {}
  342. static auto classof(const Expression* exp) -> bool {
  343. return exp->kind() == Kind::TypeTypeLiteral;
  344. }
  345. };
  346. class IntrinsicExpression : public Expression {
  347. public:
  348. enum class IntrinsicKind {
  349. Print,
  350. };
  351. explicit IntrinsicExpression(IntrinsicKind intrinsic)
  352. : Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
  353. intrinsic(intrinsic) {}
  354. static auto classof(const Expression* exp) -> bool {
  355. return exp->kind() == Kind::IntrinsicExpression;
  356. }
  357. auto Intrinsic() const -> IntrinsicKind { return intrinsic; }
  358. private:
  359. IntrinsicKind intrinsic;
  360. };
  361. } // namespace Carbon
  362. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_