expression.h 10 KB

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