expression.h 4.0 KB

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