expression.h 6.1 KB

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