expression.h 5.5 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 <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. AutoT,
  20. BoolT,
  21. Boolean,
  22. Call,
  23. FunctionT,
  24. GetField,
  25. Index,
  26. IntT,
  27. ContinuationT, // The type of a continuation value.
  28. Integer,
  29. PatternVariable,
  30. PrimitiveOp,
  31. Tuple,
  32. TypeT,
  33. Variable,
  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 Variable {
  49. static constexpr ExpressionKind Kind = ExpressionKind::Variable;
  50. std::string name;
  51. };
  52. struct FieldAccess {
  53. static constexpr ExpressionKind Kind = ExpressionKind::GetField;
  54. const Expression* aggregate;
  55. std::string field;
  56. };
  57. struct Index {
  58. static constexpr ExpressionKind Kind = ExpressionKind::Index;
  59. const Expression* aggregate;
  60. const Expression* offset;
  61. };
  62. struct PatternVariable {
  63. static constexpr ExpressionKind Kind = ExpressionKind::PatternVariable;
  64. std::string name;
  65. const Expression* type;
  66. };
  67. struct IntLiteral {
  68. static constexpr ExpressionKind Kind = ExpressionKind::Integer;
  69. int value;
  70. };
  71. struct BoolLiteral {
  72. static constexpr ExpressionKind Kind = ExpressionKind::Boolean;
  73. bool value;
  74. };
  75. struct Tuple {
  76. static constexpr ExpressionKind Kind = ExpressionKind::Tuple;
  77. std::vector<FieldInitializer> fields;
  78. };
  79. struct PrimitiveOperator {
  80. static constexpr ExpressionKind Kind = ExpressionKind::PrimitiveOp;
  81. Operator op;
  82. std::vector<const Expression*> arguments;
  83. };
  84. struct Call {
  85. static constexpr ExpressionKind Kind = ExpressionKind::Call;
  86. const Expression* function;
  87. const Expression* argument;
  88. };
  89. struct FunctionType {
  90. static constexpr ExpressionKind Kind = ExpressionKind::FunctionT;
  91. const Expression* parameter;
  92. const Expression* return_type;
  93. };
  94. struct AutoT {
  95. static constexpr ExpressionKind Kind = ExpressionKind::AutoT;
  96. };
  97. struct BoolT {
  98. static constexpr ExpressionKind Kind = ExpressionKind::BoolT;
  99. };
  100. struct IntT {
  101. static constexpr ExpressionKind Kind = ExpressionKind::IntT;
  102. };
  103. struct ContinuationT {
  104. static constexpr ExpressionKind Kind = ExpressionKind::ContinuationT;
  105. };
  106. struct TypeT {
  107. static constexpr ExpressionKind Kind = ExpressionKind::TypeT;
  108. };
  109. struct Expression {
  110. int line_num;
  111. inline auto tag() const -> ExpressionKind;
  112. static auto MakeVar(int line_num, std::string var) -> const Expression*;
  113. static auto MakeVarPat(int line_num, std::string var, const Expression* type)
  114. -> const Expression*;
  115. static auto MakeInt(int line_num, int i) -> const Expression*;
  116. static auto MakeBool(int line_num, bool b) -> const Expression*;
  117. static auto MakeOp(int line_num, Operator op,
  118. std::vector<const Expression*> args) -> const Expression*;
  119. static auto MakeUnOp(int line_num, enum Operator op, const Expression* arg)
  120. -> const Expression*;
  121. static auto MakeBinOp(int line_num, enum Operator op, const Expression* arg1,
  122. const Expression* arg2) -> const Expression*;
  123. static auto MakeCall(int line_num, const Expression* fun,
  124. const Expression* arg) -> const Expression*;
  125. static auto MakeGetField(int line_num, const Expression* exp,
  126. std::string field) -> const Expression*;
  127. static auto MakeTuple(int line_num, std::vector<FieldInitializer> args)
  128. -> const Expression*;
  129. static auto MakeIndex(int line_num, const Expression* exp,
  130. const Expression* i) -> const Expression*;
  131. static auto MakeTypeType(int line_num) -> const Expression*;
  132. static auto MakeIntType(int line_num) -> const Expression*;
  133. static auto MakeBoolType(int line_num) -> const Expression*;
  134. static auto MakeFunType(int line_num, const Expression* param,
  135. const Expression* ret) -> const Expression*;
  136. static auto MakeAutoType(int line_num) -> const Expression*;
  137. static auto MakeContinuationType(int line_num) -> const Expression*;
  138. auto GetVariable() const -> const Variable&;
  139. auto GetFieldAccess() const -> const FieldAccess&;
  140. auto GetIndex() const -> const Index&;
  141. auto GetPatternVariable() const -> const PatternVariable&;
  142. auto GetInteger() const -> int;
  143. auto GetBoolean() const -> bool;
  144. auto GetTuple() const -> const Tuple&;
  145. auto GetPrimitiveOperator() const -> const PrimitiveOperator&;
  146. auto GetCall() const -> const Call&;
  147. auto GetFunctionType() const -> const FunctionType&;
  148. private:
  149. std::variant<Variable, FieldAccess, Index, PatternVariable, IntLiteral,
  150. BoolLiteral, Tuple, PrimitiveOperator, Call, FunctionType, AutoT,
  151. BoolT, IntT, ContinuationT, TypeT>
  152. value;
  153. };
  154. void PrintExp(const Expression* exp);
  155. // Implementation details only beyond this point
  156. struct TagVisitor {
  157. template <typename Alternative>
  158. auto operator()(const Alternative&) -> ExpressionKind {
  159. return Alternative::Kind;
  160. }
  161. };
  162. auto Expression::tag() const -> ExpressionKind {
  163. return std::visit(TagVisitor(), value);
  164. }
  165. } // namespace Carbon
  166. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_