expression.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. const 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. const Expression* aggregate;
  61. const Expression* offset;
  62. };
  63. struct PatternVariable {
  64. static constexpr ExpressionKind Kind = ExpressionKind::PatternVariable;
  65. std::string name;
  66. const 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<const Expression*>* arguments;
  84. };
  85. struct Call {
  86. static constexpr ExpressionKind Kind = ExpressionKind::Call;
  87. const Expression* function;
  88. const Expression* argument;
  89. };
  90. struct FunctionType {
  91. static constexpr ExpressionKind Kind = ExpressionKind::FunctionT;
  92. const Expression* parameter;
  93. const 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, const 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,
  119. std::vector<const Expression*>* args) -> const Expression*;
  120. static auto MakeUnOp(int line_num, enum Operator op, const Expression* arg)
  121. -> const Expression*;
  122. static auto MakeBinOp(int line_num, enum Operator op, const Expression* arg1,
  123. const Expression* arg2) -> const Expression*;
  124. static auto MakeCall(int line_num, const Expression* fun,
  125. const Expression* arg) -> 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 MakeUnit(int line_num) -> const Expression*;
  131. static auto MakeIndex(int line_num, const Expression* exp,
  132. const Expression* i) -> const Expression*;
  133. static auto MakeTypeType(int line_num) -> const Expression*;
  134. static auto MakeIntType(int line_num) -> const Expression*;
  135. static auto MakeBoolType(int line_num) -> const Expression*;
  136. static auto MakeFunType(int line_num, const Expression* param,
  137. const Expression* ret) -> const Expression*;
  138. static auto MakeAutoType(int line_num) -> const Expression*;
  139. static auto MakeContinuationType(int line_num) -> const Expression*;
  140. auto GetVariable() const -> const Variable&;
  141. auto GetFieldAccess() const -> const FieldAccess&;
  142. auto GetIndex() const -> const Index&;
  143. auto GetPatternVariable() const -> const PatternVariable&;
  144. auto GetInteger() const -> int;
  145. auto GetBoolean() const -> bool;
  146. auto GetTuple() const -> const Tuple&;
  147. auto GetPrimitiveOperator() const -> const PrimitiveOperator&;
  148. auto GetCall() const -> const Call&;
  149. auto GetFunctionType() const -> const FunctionType&;
  150. private:
  151. std::variant<Variable, FieldAccess, Index, PatternVariable, IntLiteral,
  152. BoolLiteral, Tuple, PrimitiveOperator, Call, FunctionType, AutoT,
  153. BoolT, IntT, ContinuationT, TypeT>
  154. value;
  155. };
  156. void PrintExp(const Expression* exp);
  157. // Implementation details only beyond this point
  158. struct TagVisitor {
  159. template <typename Alternative>
  160. auto operator()(const Alternative&) -> ExpressionKind {
  161. return Alternative::Kind;
  162. }
  163. };
  164. auto Expression::tag() const -> ExpressionKind {
  165. return std::visit(TagVisitor(), value);
  166. }
  167. } // namespace Carbon
  168. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_