expression.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. Expression* aggregate;
  44. std::string* field;
  45. } get_field;
  46. struct {
  47. Expression* aggregate;
  48. Expression* offset;
  49. } index;
  50. struct {
  51. std::string* name;
  52. Expression* type;
  53. } pattern_variable;
  54. int integer;
  55. bool boolean;
  56. struct {
  57. std::vector<std::pair<std::string, Expression*>>* fields;
  58. } tuple;
  59. struct {
  60. Operator op;
  61. std::vector<Expression*>* arguments;
  62. } primitive_op;
  63. struct {
  64. Expression* function;
  65. Expression* argument;
  66. } call;
  67. struct {
  68. Expression* parameter;
  69. Expression* return_type;
  70. } function_type;
  71. } u;
  72. };
  73. auto MakeVar(int line_num, std::string var) -> Expression*;
  74. auto MakeVarPat(int line_num, std::string var, Expression* type) -> Expression*;
  75. auto MakeInt(int line_num, int i) -> Expression*;
  76. auto MakeBool(int line_num, bool b) -> Expression*;
  77. auto MakeOp(int line_num, Operator op, std::vector<Expression*>* args)
  78. -> Expression*;
  79. auto MakeUnOp(int line_num, enum Operator op, Expression* arg) -> Expression*;
  80. auto MakeBinOp(int line_num, enum Operator op, Expression* arg1,
  81. Expression* arg2) -> Expression*;
  82. auto MakeCall(int line_num, Expression* fun, Expression* arg) -> Expression*;
  83. auto MakeGetField(int line_num, Expression* exp, std::string field)
  84. -> Expression*;
  85. auto MakeTuple(int line_num,
  86. std::vector<std::pair<std::string, Expression*>>* args)
  87. -> Expression*;
  88. // Create an AST node for an empty tuple.
  89. auto MakeUnit(int line_num) -> Expression*;
  90. auto MakeIndex(int line_num, Expression* exp, Expression* i) -> Expression*;
  91. auto MakeTypeType(int line_num) -> Expression*;
  92. auto MakeIntType(int line_num) -> Expression*;
  93. auto MakeBoolType(int line_num) -> Expression*;
  94. auto MakeFunType(int line_num, Expression* param, Expression* ret)
  95. -> Expression*;
  96. auto MakeAutoType(int line_num) -> Expression*;
  97. // Returns a Continuation type AST node at the given source location,
  98. // which is the type of a continuation value.
  99. auto MakeContinuationType(int line_num) -> Expression*;
  100. void PrintExp(const Expression* exp);
  101. } // namespace Carbon
  102. #endif // EXECUTABLE_SEMANTICS_AST_EXPRESSION_H_