expression.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "common/indirect_value.h"
  10. namespace Carbon {
  11. struct Expression;
  12. // A FieldInitializer represents the initialization of a single tuple field.
  13. struct FieldInitializer {
  14. // The field name. For a positional field, this may be empty.
  15. std::string name;
  16. // The expression that initializes the field.
  17. IndirectValue<Expression> expression;
  18. };
  19. enum class ExpressionKind {
  20. AutoT,
  21. BoolT,
  22. Boolean,
  23. Call,
  24. FunctionT,
  25. GetField,
  26. Index,
  27. IntT,
  28. ContinuationT, // The type of a continuation value.
  29. Integer,
  30. PatternVariable,
  31. PrimitiveOp,
  32. Tuple,
  33. TypeT,
  34. Variable,
  35. };
  36. enum class Operator {
  37. Add,
  38. And,
  39. Deref,
  40. Eq,
  41. Mul,
  42. Neg,
  43. Not,
  44. Or,
  45. Sub,
  46. Ptr,
  47. };
  48. struct Expression;
  49. struct Variable {
  50. static constexpr ExpressionKind Kind = ExpressionKind::Variable;
  51. std::string name;
  52. };
  53. struct FieldAccess {
  54. static constexpr ExpressionKind Kind = ExpressionKind::GetField;
  55. IndirectValue<Expression> aggregate;
  56. std::string field;
  57. };
  58. struct Index {
  59. static constexpr ExpressionKind Kind = ExpressionKind::Index;
  60. IndirectValue<Expression> aggregate;
  61. IndirectValue<Expression> offset;
  62. };
  63. struct PatternVariable {
  64. static constexpr ExpressionKind Kind = ExpressionKind::PatternVariable;
  65. std::string name;
  66. IndirectValue<Expression> type;
  67. };
  68. struct IntLiteral {
  69. static constexpr ExpressionKind Kind = ExpressionKind::Integer;
  70. int value;
  71. };
  72. struct BoolLiteral {
  73. static constexpr ExpressionKind Kind = ExpressionKind::Boolean;
  74. bool value;
  75. };
  76. struct Tuple {
  77. static constexpr ExpressionKind Kind = ExpressionKind::Tuple;
  78. std::vector<FieldInitializer> fields;
  79. };
  80. struct PrimitiveOperator {
  81. static constexpr ExpressionKind Kind = ExpressionKind::PrimitiveOp;
  82. Operator op;
  83. std::vector<Expression> arguments;
  84. };
  85. struct Call {
  86. static constexpr ExpressionKind Kind = ExpressionKind::Call;
  87. IndirectValue<Expression> function;
  88. IndirectValue<Expression> argument;
  89. };
  90. struct FunctionType {
  91. static constexpr ExpressionKind Kind = ExpressionKind::FunctionT;
  92. IndirectValue<Expression> parameter;
  93. IndirectValue<Expression> return_type;
  94. };
  95. struct AutoT {
  96. static constexpr ExpressionKind Kind = ExpressionKind::AutoT;
  97. };
  98. struct BoolT {
  99. static constexpr ExpressionKind Kind = ExpressionKind::BoolT;
  100. };
  101. struct IntT {
  102. static constexpr ExpressionKind Kind = ExpressionKind::IntT;
  103. };
  104. struct ContinuationT {
  105. static constexpr ExpressionKind Kind = ExpressionKind::ContinuationT;
  106. };
  107. struct TypeT {
  108. static constexpr ExpressionKind Kind = ExpressionKind::TypeT;
  109. };
  110. struct Expression {
  111. int line_num;
  112. inline auto tag() const -> ExpressionKind;
  113. static auto MakeVar(int line_num, std::string var) -> const Expression*;
  114. static auto MakeVarPat(int line_num, std::string var, Expression type)
  115. -> const Expression*;
  116. static auto MakeInt(int line_num, int i) -> const Expression*;
  117. static auto MakeBool(int line_num, bool b) -> const Expression*;
  118. static auto MakeOp(int line_num, Operator op, std::vector<Expression> args)
  119. -> const Expression*;
  120. static auto MakeUnOp(int line_num, enum Operator op, Expression arg)
  121. -> const Expression*;
  122. static auto MakeBinOp(int line_num, enum Operator op, Expression arg1,
  123. Expression arg2) -> const Expression*;
  124. static auto MakeCall(int line_num, Expression fun, Expression arg)
  125. -> const Expression*;
  126. static auto MakeGetField(int line_num, const Expression* exp,
  127. std::string field) -> const Expression*;
  128. static auto MakeTuple(int line_num, std::vector<FieldInitializer> args)
  129. -> const Expression*;
  130. static auto MakeIndex(int line_num, Expression exp, Expression i)
  131. -> 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, Expression param, Expression ret)
  136. -> 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_