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