expression.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <string>
  7. #include <variant>
  8. #include <vector>
  9. namespace Carbon {
  10. struct Expression;
  11. // A FieldInitializer represents the initialization of a single tuple field.
  12. struct FieldInitializer {
  13. // The field name. For a positional field, this may be empty.
  14. std::string name;
  15. // The expression that initializes the field.
  16. const Expression* expression;
  17. };
  18. enum class ExpressionKind {
  19. AutoTypeLiteral,
  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. BindingExpression,
  30. PrimitiveOperatorExpression,
  31. TupleLiteral,
  32. TypeTypeLiteral,
  33. IdentifierExpression,
  34. };
  35. enum class Operator {
  36. Add,
  37. And,
  38. Deref,
  39. Eq,
  40. Mul,
  41. Neg,
  42. Not,
  43. Or,
  44. Sub,
  45. Ptr,
  46. };
  47. struct Expression;
  48. struct IdentifierExpression {
  49. static constexpr ExpressionKind Kind = ExpressionKind::IdentifierExpression;
  50. std::string name;
  51. };
  52. struct FieldAccessExpression {
  53. static constexpr ExpressionKind Kind = ExpressionKind::FieldAccessExpression;
  54. const Expression* aggregate;
  55. std::string field;
  56. };
  57. struct IndexExpression {
  58. static constexpr ExpressionKind Kind = ExpressionKind::IndexExpression;
  59. const Expression* aggregate;
  60. const Expression* offset;
  61. };
  62. struct BindingExpression {
  63. static constexpr ExpressionKind Kind = ExpressionKind::BindingExpression;
  64. std::string name;
  65. const Expression* type;
  66. };
  67. struct IntLiteral {
  68. static constexpr ExpressionKind Kind = ExpressionKind::IntLiteral;
  69. int value;
  70. };
  71. struct BoolLiteral {
  72. static constexpr ExpressionKind Kind = ExpressionKind::BoolLiteral;
  73. bool value;
  74. };
  75. struct TupleLiteral {
  76. static constexpr ExpressionKind Kind = ExpressionKind::TupleLiteral;
  77. std::vector<FieldInitializer> fields;
  78. };
  79. struct PrimitiveOperatorExpression {
  80. static constexpr ExpressionKind Kind =
  81. ExpressionKind::PrimitiveOperatorExpression;
  82. Operator op;
  83. std::vector<const Expression*> arguments;
  84. };
  85. struct CallExpression {
  86. static constexpr ExpressionKind Kind = ExpressionKind::CallExpression;
  87. const Expression* function;
  88. const Expression* argument;
  89. };
  90. struct FunctionTypeLiteral {
  91. static constexpr ExpressionKind Kind = ExpressionKind::FunctionTypeLiteral;
  92. const Expression* parameter;
  93. const Expression* return_type;
  94. };
  95. struct AutoTypeLiteral {
  96. static constexpr ExpressionKind Kind = ExpressionKind::AutoTypeLiteral;
  97. };
  98. struct BoolTypeLiteral {
  99. static constexpr ExpressionKind Kind = ExpressionKind::BoolTypeLiteral;
  100. };
  101. struct IntTypeLiteral {
  102. static constexpr ExpressionKind Kind = ExpressionKind::IntTypeLiteral;
  103. };
  104. struct ContinuationTypeLiteral {
  105. static constexpr ExpressionKind Kind =
  106. ExpressionKind::ContinuationTypeLiteral;
  107. };
  108. struct TypeTypeLiteral {
  109. static constexpr ExpressionKind Kind = ExpressionKind::TypeTypeLiteral;
  110. };
  111. struct Expression {
  112. int line_num;
  113. inline auto tag() const -> ExpressionKind;
  114. static auto MakeIdentifierExpression(int line_num, std::string var)
  115. -> const Expression*;
  116. static auto MakeBindingExpression(int line_num, std::string var,
  117. const Expression* type)
  118. -> const Expression*;
  119. static auto MakeIntLiteral(int line_num, int i) -> const Expression*;
  120. static auto MakeBoolLiteral(int line_num, bool b) -> const Expression*;
  121. static auto MakePrimitiveOperatorExpression(
  122. int line_num, Operator op, std::vector<const Expression*> args)
  123. -> const Expression*;
  124. static auto MakeCallExpression(int line_num, const Expression* fun,
  125. const Expression* arg) -> const Expression*;
  126. static auto MakeFieldAccessExpression(int line_num, const Expression* exp,
  127. std::string field) -> const Expression*;
  128. static auto MakeTupleLiteral(int line_num, std::vector<FieldInitializer> args)
  129. -> const Expression*;
  130. static auto MakeIndexExpression(int line_num, const Expression* exp,
  131. const Expression* i) -> const Expression*;
  132. static auto MakeTypeTypeLiteral(int line_num) -> const Expression*;
  133. static auto MakeIntTypeLiteral(int line_num) -> const Expression*;
  134. static auto MakeBoolTypeLiteral(int line_num) -> const Expression*;
  135. static auto MakeFunctionTypeLiteral(int line_num, const Expression* param,
  136. const Expression* ret)
  137. -> const Expression*;
  138. static auto MakeAutoTypeLiteral(int line_num) -> const Expression*;
  139. static auto MakeContinuationTypeLiteral(int line_num) -> const Expression*;
  140. auto GetIdentifierExpression() const -> const IdentifierExpression&;
  141. auto GetFieldAccessExpression() const -> const FieldAccessExpression&;
  142. auto GetIndexExpression() const -> const IndexExpression&;
  143. auto GetBindingExpression() const -> const BindingExpression&;
  144. auto GetIntLiteral() const -> int;
  145. auto GetBoolLiteral() const -> bool;
  146. auto GetTupleLiteral() const -> const TupleLiteral&;
  147. auto GetPrimitiveOperatorExpression() const
  148. -> const PrimitiveOperatorExpression&;
  149. auto GetCallExpression() const -> const CallExpression&;
  150. auto GetFunctionTypeLiteral() const -> const FunctionTypeLiteral&;
  151. private:
  152. std::variant<IdentifierExpression, FieldAccessExpression, IndexExpression,
  153. BindingExpression, IntLiteral, BoolLiteral, TupleLiteral,
  154. PrimitiveOperatorExpression, CallExpression, FunctionTypeLiteral,
  155. AutoTypeLiteral, BoolTypeLiteral, IntTypeLiteral,
  156. ContinuationTypeLiteral, TypeTypeLiteral>
  157. value;
  158. };
  159. void PrintExp(const Expression* exp);
  160. // Implementation details only beyond this point
  161. struct TagVisitor {
  162. template <typename Alternative>
  163. auto operator()(const Alternative&) -> ExpressionKind {
  164. return Alternative::Kind;
  165. }
  166. };
  167. auto Expression::tag() const -> ExpressionKind {
  168. return std::visit(TagVisitor(), value);
  169. }
  170. } // namespace Carbon
  171. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_