expression.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 Expression {
  18. public:
  19. enum class Kind {
  20. BoolTypeLiteral,
  21. BoolLiteral,
  22. CallExpression,
  23. FunctionTypeLiteral,
  24. FieldAccessExpression,
  25. IndexExpression,
  26. IntTypeLiteral,
  27. ContinuationTypeLiteral, // The type of a continuation value.
  28. IntLiteral,
  29. PrimitiveOperatorExpression,
  30. StringLiteral,
  31. StringTypeLiteral,
  32. TupleLiteral,
  33. TypeTypeLiteral,
  34. IdentifierExpression,
  35. IntrinsicExpression,
  36. };
  37. // Returns the enumerator corresponding to the most-derived type of this
  38. // object.
  39. auto Tag() const -> Kind { return tag; }
  40. auto SourceLoc() const -> SourceLocation { return loc; }
  41. void Print(llvm::raw_ostream& out) const;
  42. LLVM_DUMP_METHOD void Dump() const { Print(llvm::errs()); }
  43. protected:
  44. // Constructs an Expression representing syntax at the given line number.
  45. // `tag` must be the enumerator corresponding to the most-derived type being
  46. // constructed.
  47. Expression(Kind tag, SourceLocation loc) : tag(tag), loc(loc) {}
  48. private:
  49. const Kind tag;
  50. SourceLocation loc;
  51. };
  52. // Converts paren_contents to an Expression, interpreting the parentheses as
  53. // grouping if their contents permit that interpretation, or as forming a
  54. // tuple otherwise.
  55. auto ExpressionFromParenContents(
  56. Nonnull<Arena*> arena, SourceLocation loc,
  57. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  58. // Converts paren_contents to an Expression, interpreting the parentheses as
  59. // forming a tuple.
  60. auto TupleExpressionFromParenContents(
  61. Nonnull<Arena*> arena, SourceLocation loc,
  62. const ParenContents<Expression>& paren_contents) -> Nonnull<Expression*>;
  63. // A FieldInitializer represents the initialization of a single tuple field.
  64. struct FieldInitializer {
  65. FieldInitializer(std::string name, Nonnull<Expression*> expression)
  66. : name(std::move(name)), expression(expression) {}
  67. // The field name. Cannot be empty.
  68. std::string name;
  69. // The expression that initializes the field.
  70. Nonnull<Expression*> expression;
  71. };
  72. enum class Operator {
  73. Add,
  74. And,
  75. Deref,
  76. Eq,
  77. Mul,
  78. Neg,
  79. Not,
  80. Or,
  81. Sub,
  82. Ptr,
  83. };
  84. class IdentifierExpression : public Expression {
  85. public:
  86. explicit IdentifierExpression(SourceLocation loc, std::string name)
  87. : Expression(Kind::IdentifierExpression, loc), name(std::move(name)) {}
  88. static auto classof(const Expression* exp) -> bool {
  89. return exp->Tag() == Kind::IdentifierExpression;
  90. }
  91. auto Name() const -> const std::string& { return name; }
  92. private:
  93. std::string name;
  94. };
  95. class FieldAccessExpression : public Expression {
  96. public:
  97. explicit FieldAccessExpression(SourceLocation loc,
  98. Nonnull<Expression*> aggregate,
  99. std::string field)
  100. : Expression(Kind::FieldAccessExpression, loc),
  101. aggregate(aggregate),
  102. field(std::move(field)) {}
  103. static auto classof(const Expression* exp) -> bool {
  104. return exp->Tag() == Kind::FieldAccessExpression;
  105. }
  106. auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
  107. auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
  108. auto Field() const -> const std::string& { return field; }
  109. private:
  110. Nonnull<Expression*> aggregate;
  111. std::string field;
  112. };
  113. class IndexExpression : public Expression {
  114. public:
  115. explicit IndexExpression(SourceLocation loc, Nonnull<Expression*> aggregate,
  116. Nonnull<Expression*> offset)
  117. : Expression(Kind::IndexExpression, loc),
  118. aggregate(aggregate),
  119. offset(offset) {}
  120. static auto classof(const Expression* exp) -> bool {
  121. return exp->Tag() == Kind::IndexExpression;
  122. }
  123. auto Aggregate() const -> Nonnull<const Expression*> { return aggregate; }
  124. auto Aggregate() -> Nonnull<Expression*> { return aggregate; }
  125. auto Offset() const -> Nonnull<const Expression*> { return offset; }
  126. auto Offset() -> Nonnull<Expression*> { return offset; }
  127. private:
  128. Nonnull<Expression*> aggregate;
  129. Nonnull<Expression*> offset;
  130. };
  131. class IntLiteral : public Expression {
  132. public:
  133. explicit IntLiteral(SourceLocation loc, int val)
  134. : Expression(Kind::IntLiteral, loc), val(val) {}
  135. static auto classof(const Expression* exp) -> bool {
  136. return exp->Tag() == Kind::IntLiteral;
  137. }
  138. auto Val() const -> int { return val; }
  139. private:
  140. int val;
  141. };
  142. class BoolLiteral : public Expression {
  143. public:
  144. explicit BoolLiteral(SourceLocation loc, bool val)
  145. : Expression(Kind::BoolLiteral, loc), val(val) {}
  146. static auto classof(const Expression* exp) -> bool {
  147. return exp->Tag() == Kind::BoolLiteral;
  148. }
  149. auto Val() const -> bool { return val; }
  150. private:
  151. bool val;
  152. };
  153. class StringLiteral : public Expression {
  154. public:
  155. explicit StringLiteral(SourceLocation loc, std::string val)
  156. : Expression(Kind::StringLiteral, loc), val(std::move(val)) {}
  157. static auto classof(const Expression* exp) -> bool {
  158. return exp->Tag() == Kind::StringLiteral;
  159. }
  160. auto Val() const -> const std::string& { return val; }
  161. private:
  162. std::string val;
  163. };
  164. class StringTypeLiteral : public Expression {
  165. public:
  166. explicit StringTypeLiteral(SourceLocation loc)
  167. : Expression(Kind::StringTypeLiteral, loc) {}
  168. static auto classof(const Expression* exp) -> bool {
  169. return exp->Tag() == Kind::StringTypeLiteral;
  170. }
  171. };
  172. class TupleLiteral : public Expression {
  173. public:
  174. explicit TupleLiteral(SourceLocation loc) : TupleLiteral(loc, {}) {}
  175. explicit TupleLiteral(SourceLocation loc,
  176. std::vector<FieldInitializer> fields)
  177. : Expression(Kind::TupleLiteral, loc), fields(std::move(fields)) {}
  178. static auto classof(const Expression* exp) -> bool {
  179. return exp->Tag() == Kind::TupleLiteral;
  180. }
  181. auto Fields() const -> const std::vector<FieldInitializer>& { return fields; }
  182. private:
  183. std::vector<FieldInitializer> fields;
  184. };
  185. class PrimitiveOperatorExpression : public Expression {
  186. public:
  187. explicit PrimitiveOperatorExpression(
  188. SourceLocation loc, Operator op,
  189. std::vector<Nonnull<Expression*>> arguments)
  190. : Expression(Kind::PrimitiveOperatorExpression, loc),
  191. op(op),
  192. arguments(std::move(arguments)) {}
  193. static auto classof(const Expression* exp) -> bool {
  194. return exp->Tag() == Kind::PrimitiveOperatorExpression;
  195. }
  196. auto Op() const -> Operator { return op; }
  197. auto Arguments() const -> llvm::ArrayRef<Nonnull<Expression*>> {
  198. return arguments;
  199. }
  200. auto Arguments() -> llvm::MutableArrayRef<Nonnull<Expression*>> {
  201. return arguments;
  202. }
  203. private:
  204. Operator op;
  205. std::vector<Nonnull<Expression*>> arguments;
  206. };
  207. class CallExpression : public Expression {
  208. public:
  209. explicit CallExpression(SourceLocation loc, Nonnull<Expression*> function,
  210. Nonnull<Expression*> argument)
  211. : Expression(Kind::CallExpression, loc),
  212. function(function),
  213. argument(argument) {}
  214. static auto classof(const Expression* exp) -> bool {
  215. return exp->Tag() == Kind::CallExpression;
  216. }
  217. auto Function() const -> Nonnull<const Expression*> { return function; }
  218. auto Function() -> Nonnull<Expression*> { return function; }
  219. auto Argument() const -> Nonnull<const Expression*> { return argument; }
  220. auto Argument() -> Nonnull<Expression*> { return argument; }
  221. private:
  222. Nonnull<Expression*> function;
  223. Nonnull<Expression*> argument;
  224. };
  225. class FunctionTypeLiteral : public Expression {
  226. public:
  227. explicit FunctionTypeLiteral(SourceLocation loc,
  228. Nonnull<Expression*> parameter,
  229. Nonnull<Expression*> return_type,
  230. bool is_omitted_return_type)
  231. : Expression(Kind::FunctionTypeLiteral, loc),
  232. parameter(parameter),
  233. return_type(return_type),
  234. is_omitted_return_type(is_omitted_return_type) {}
  235. static auto classof(const Expression* exp) -> bool {
  236. return exp->Tag() == Kind::FunctionTypeLiteral;
  237. }
  238. auto Parameter() const -> Nonnull<const Expression*> { return parameter; }
  239. auto Parameter() -> Nonnull<Expression*> { return parameter; }
  240. auto ReturnType() const -> Nonnull<const Expression*> { return return_type; }
  241. auto ReturnType() -> Nonnull<Expression*> { return return_type; }
  242. auto IsOmittedReturnType() const -> bool { return is_omitted_return_type; }
  243. private:
  244. Nonnull<Expression*> parameter;
  245. Nonnull<Expression*> return_type;
  246. bool is_omitted_return_type;
  247. };
  248. class BoolTypeLiteral : public Expression {
  249. public:
  250. explicit BoolTypeLiteral(SourceLocation loc)
  251. : Expression(Kind::BoolTypeLiteral, loc) {}
  252. static auto classof(const Expression* exp) -> bool {
  253. return exp->Tag() == Kind::BoolTypeLiteral;
  254. }
  255. };
  256. class IntTypeLiteral : public Expression {
  257. public:
  258. explicit IntTypeLiteral(SourceLocation loc)
  259. : Expression(Kind::IntTypeLiteral, loc) {}
  260. static auto classof(const Expression* exp) -> bool {
  261. return exp->Tag() == Kind::IntTypeLiteral;
  262. }
  263. };
  264. class ContinuationTypeLiteral : public Expression {
  265. public:
  266. explicit ContinuationTypeLiteral(SourceLocation loc)
  267. : Expression(Kind::ContinuationTypeLiteral, loc) {}
  268. static auto classof(const Expression* exp) -> bool {
  269. return exp->Tag() == Kind::ContinuationTypeLiteral;
  270. }
  271. };
  272. class TypeTypeLiteral : public Expression {
  273. public:
  274. explicit TypeTypeLiteral(SourceLocation loc)
  275. : Expression(Kind::TypeTypeLiteral, loc) {}
  276. static auto classof(const Expression* exp) -> bool {
  277. return exp->Tag() == Kind::TypeTypeLiteral;
  278. }
  279. };
  280. class IntrinsicExpression : public Expression {
  281. public:
  282. enum class IntrinsicKind {
  283. Print,
  284. };
  285. explicit IntrinsicExpression(IntrinsicKind intrinsic)
  286. : Expression(Kind::IntrinsicExpression, SourceLocation("<intrinsic>", 0)),
  287. intrinsic(intrinsic) {}
  288. static auto classof(const Expression* exp) -> bool {
  289. return exp->Tag() == Kind::IntrinsicExpression;
  290. }
  291. auto Intrinsic() const -> IntrinsicKind { return intrinsic; }
  292. private:
  293. IntrinsicKind intrinsic;
  294. };
  295. } // namespace Carbon
  296. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_