expression.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. PatternVariableExpression,
  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 PatternVariableExpression {
  63. static constexpr ExpressionKind Kind =
  64. ExpressionKind::PatternVariableExpression;
  65. std::string name;
  66. const Expression* type;
  67. };
  68. struct IntLiteral {
  69. static constexpr ExpressionKind Kind = ExpressionKind::IntLiteral;
  70. int value;
  71. };
  72. struct BoolLiteral {
  73. static constexpr ExpressionKind Kind = ExpressionKind::BoolLiteral;
  74. bool value;
  75. };
  76. struct TupleLiteral {
  77. static constexpr ExpressionKind Kind = ExpressionKind::TupleLiteral;
  78. std::vector<FieldInitializer> fields;
  79. };
  80. struct PrimitiveOperatorExpression {
  81. static constexpr ExpressionKind Kind =
  82. ExpressionKind::PrimitiveOperatorExpression;
  83. Operator op;
  84. std::vector<const Expression*> arguments;
  85. };
  86. struct CallExpression {
  87. static constexpr ExpressionKind Kind = ExpressionKind::CallExpression;
  88. const Expression* function;
  89. const Expression* argument;
  90. };
  91. struct FunctionTypeLiteral {
  92. static constexpr ExpressionKind Kind = ExpressionKind::FunctionTypeLiteral;
  93. const Expression* parameter;
  94. const Expression* return_type;
  95. };
  96. struct AutoTypeLiteral {
  97. static constexpr ExpressionKind Kind = ExpressionKind::AutoTypeLiteral;
  98. };
  99. struct BoolTypeLiteral {
  100. static constexpr ExpressionKind Kind = ExpressionKind::BoolTypeLiteral;
  101. };
  102. struct IntTypeLiteral {
  103. static constexpr ExpressionKind Kind = ExpressionKind::IntTypeLiteral;
  104. };
  105. struct ContinuationTypeLiteral {
  106. static constexpr ExpressionKind Kind =
  107. ExpressionKind::ContinuationTypeLiteral;
  108. };
  109. struct TypeTypeLiteral {
  110. static constexpr ExpressionKind Kind = ExpressionKind::TypeTypeLiteral;
  111. };
  112. struct Expression {
  113. int line_num;
  114. inline auto tag() const -> ExpressionKind;
  115. static auto MakeIdentifierExpression(int line_num, std::string var)
  116. -> const Expression*;
  117. static auto MakePatternVariableExpression(int line_num, std::string var,
  118. const Expression* type)
  119. -> const Expression*;
  120. static auto MakeIntLiteral(int line_num, int i) -> const Expression*;
  121. static auto MakeBoolLiteral(int line_num, bool b) -> const Expression*;
  122. static auto MakePrimitiveOperatorExpression(
  123. int line_num, Operator op, std::vector<const Expression*> args)
  124. -> const Expression*;
  125. static auto MakeCallExpression(int line_num, const Expression* fun,
  126. const Expression* arg) -> const Expression*;
  127. static auto MakeFieldAccessExpression(int line_num, const Expression* exp,
  128. std::string field) -> const Expression*;
  129. static auto MakeTupleLiteral(int line_num, std::vector<FieldInitializer> args)
  130. -> const Expression*;
  131. static auto MakeIndexExpression(int line_num, const Expression* exp,
  132. const Expression* i) -> const Expression*;
  133. static auto MakeTypeTypeLiteral(int line_num) -> const Expression*;
  134. static auto MakeIntTypeLiteral(int line_num) -> const Expression*;
  135. static auto MakeBoolTypeLiteral(int line_num) -> const Expression*;
  136. static auto MakeFunctionTypeLiteral(int line_num, const Expression* param,
  137. const Expression* ret)
  138. -> const Expression*;
  139. static auto MakeAutoTypeLiteral(int line_num) -> const Expression*;
  140. static auto MakeContinuationTypeLiteral(int line_num) -> const Expression*;
  141. auto GetIdentifierExpression() const -> const IdentifierExpression&;
  142. auto GetFieldAccessExpression() const -> const FieldAccessExpression&;
  143. auto GetIndexExpression() const -> const IndexExpression&;
  144. auto GetPatternVariableExpression() const -> const PatternVariableExpression&;
  145. auto GetIntLiteral() const -> int;
  146. auto GetBoolLiteral() const -> bool;
  147. auto GetTupleLiteral() const -> const TupleLiteral&;
  148. auto GetPrimitiveOperatorExpression() const
  149. -> const PrimitiveOperatorExpression&;
  150. auto GetCallExpression() const -> const CallExpression&;
  151. auto GetFunctionTypeLiteral() const -> const FunctionTypeLiteral&;
  152. private:
  153. std::variant<IdentifierExpression, FieldAccessExpression, IndexExpression,
  154. PatternVariableExpression, IntLiteral, BoolLiteral, TupleLiteral,
  155. PrimitiveOperatorExpression, CallExpression, FunctionTypeLiteral,
  156. AutoTypeLiteral, BoolTypeLiteral, IntTypeLiteral,
  157. ContinuationTypeLiteral, TypeTypeLiteral>
  158. value;
  159. };
  160. void PrintExp(const Expression* exp);
  161. // Implementation details only beyond this point
  162. struct TagVisitor {
  163. template <typename Alternative>
  164. auto operator()(const Alternative&) -> ExpressionKind {
  165. return Alternative::Kind;
  166. }
  167. };
  168. auto Expression::tag() const -> ExpressionKind {
  169. return std::visit(TagVisitor(), value);
  170. }
  171. } // namespace Carbon
  172. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_