expression.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 MakeUnit(int line_num) -> const Expression*;
  130. static auto MakeIndex(int line_num, const Expression* exp,
  131. const Expression* i) -> const Expression*;
  132. static auto MakeTypeType(int line_num) -> const Expression*;
  133. static auto MakeIntType(int line_num) -> const Expression*;
  134. static auto MakeBoolType(int line_num) -> const Expression*;
  135. static auto MakeFunType(int line_num, const Expression* param,
  136. const Expression* ret) -> const Expression*;
  137. static auto MakeAutoType(int line_num) -> const Expression*;
  138. static auto MakeContinuationType(int line_num) -> const Expression*;
  139. auto GetVariable() const -> const Variable&;
  140. auto GetFieldAccess() const -> const FieldAccess&;
  141. auto GetIndex() const -> const Index&;
  142. auto GetPatternVariable() const -> const PatternVariable&;
  143. auto GetInteger() const -> int;
  144. auto GetBoolean() const -> bool;
  145. auto GetTuple() const -> const Tuple&;
  146. auto GetPrimitiveOperator() const -> const PrimitiveOperator&;
  147. auto GetCall() const -> const Call&;
  148. auto GetFunctionType() const -> const FunctionType&;
  149. private:
  150. std::variant<Variable, FieldAccess, Index, PatternVariable, IntLiteral,
  151. BoolLiteral, Tuple, PrimitiveOperator, Call, FunctionType, AutoT,
  152. BoolT, IntT, ContinuationT, TypeT>
  153. value;
  154. };
  155. void PrintExp(const Expression* exp);
  156. // Implementation details only beyond this point
  157. struct TagVisitor {
  158. template <typename Alternative>
  159. auto operator()(const Alternative&) -> ExpressionKind {
  160. return Alternative::Kind;
  161. }
  162. };
  163. auto Expression::tag() const -> ExpressionKind {
  164. return std::visit(TagVisitor(), value);
  165. }
  166. } // namespace Carbon
  167. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_