expression.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. enum class ExpressionKind {
  10. AutoT,
  11. BoolT,
  12. Boolean,
  13. Call,
  14. FunctionT,
  15. GetField,
  16. Index,
  17. IntT,
  18. ContinuationT, // The type of a continuation value.
  19. Integer,
  20. PatternVariable,
  21. PrimitiveOp,
  22. Tuple,
  23. TypeT,
  24. Variable,
  25. };
  26. enum class Operator {
  27. Add,
  28. And,
  29. Eq,
  30. Neg,
  31. Not,
  32. Or,
  33. Sub,
  34. };
  35. struct Expression {
  36. int line_num;
  37. ExpressionKind tag;
  38. union {
  39. struct {
  40. std::string* name;
  41. } variable;
  42. struct {
  43. const Expression* aggregate;
  44. std::string* field;
  45. } get_field;
  46. struct {
  47. const Expression* aggregate;
  48. const Expression* offset;
  49. } index;
  50. struct {
  51. std::string* name;
  52. const Expression* type;
  53. } pattern_variable;
  54. int integer;
  55. bool boolean;
  56. struct {
  57. std::vector<std::pair<std::string, const Expression*>>* fields;
  58. } tuple;
  59. struct {
  60. Operator op;
  61. std::vector<const Expression*>* arguments;
  62. } primitive_op;
  63. struct {
  64. const Expression* function;
  65. const Expression* argument;
  66. } call;
  67. struct {
  68. const Expression* parameter;
  69. const Expression* return_type;
  70. } function_type;
  71. } u;
  72. };
  73. auto MakeVar(int line_num, std::string var) -> const Expression*;
  74. auto MakeVarPat(int line_num, std::string var, const Expression* type)
  75. -> const Expression*;
  76. auto MakeInt(int line_num, int i) -> const Expression*;
  77. auto MakeBool(int line_num, bool b) -> const Expression*;
  78. auto MakeOp(int line_num, Operator op, std::vector<const Expression*>* args)
  79. -> const Expression*;
  80. auto MakeUnOp(int line_num, enum Operator op, const Expression* arg)
  81. -> const Expression*;
  82. auto MakeBinOp(int line_num, enum Operator op, const Expression* arg1,
  83. const Expression* arg2) -> const Expression*;
  84. auto MakeCall(int line_num, const Expression* fun, const Expression* arg)
  85. -> const Expression*;
  86. auto MakeGetField(int line_num, const Expression* exp, std::string field)
  87. -> const Expression*;
  88. auto MakeTuple(int line_num,
  89. std::vector<std::pair<std::string, const Expression*>>* args)
  90. -> const Expression*;
  91. // Create an AST node for an empty tuple.
  92. auto MakeUnit(int line_num) -> const Expression*;
  93. auto MakeIndex(int line_num, const Expression* exp, const Expression* i)
  94. -> const Expression*;
  95. auto MakeTypeType(int line_num) -> const Expression*;
  96. auto MakeIntType(int line_num) -> const Expression*;
  97. auto MakeBoolType(int line_num) -> const Expression*;
  98. auto MakeFunType(int line_num, const Expression* param, const Expression* ret)
  99. -> const Expression*;
  100. auto MakeAutoType(int line_num) -> const Expression*;
  101. // Returns a Continuation type AST node at the given source location,
  102. // which is the type of a continuation value.
  103. auto MakeContinuationType(int line_num) -> const Expression*;
  104. void PrintExp(const Expression* exp);
  105. } // namespace Carbon
  106. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_