value.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_INTERPRETER_VALUE_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_
  6. #include <list>
  7. #include <vector>
  8. #include "executable_semantics/ast/statement.h"
  9. namespace Carbon {
  10. struct Value;
  11. using Address = unsigned int;
  12. using VarValues = std::list<std::pair<std::string, Value*>>;
  13. auto FindInVarValues(const std::string& field, VarValues* inits) -> Value*;
  14. auto FieldsEqual(VarValues* ts1, VarValues* ts2) -> bool;
  15. enum class ValKind {
  16. IntV,
  17. FunV,
  18. PtrV,
  19. BoolV,
  20. StructV,
  21. AltV,
  22. TupleV,
  23. VarTV,
  24. IntTV,
  25. BoolTV,
  26. TypeTV,
  27. FunctionTV,
  28. PointerTV,
  29. AutoTV,
  30. TupleTV,
  31. StructTV,
  32. ChoiceTV,
  33. VarPatV,
  34. AltConsV
  35. };
  36. struct Value {
  37. ValKind tag;
  38. bool alive;
  39. union {
  40. int integer;
  41. bool boolean;
  42. struct {
  43. std::string* name;
  44. Value* param;
  45. Statement* body;
  46. } fun;
  47. struct {
  48. Value* type;
  49. Value* inits;
  50. } struct_val;
  51. struct {
  52. std::string* alt_name;
  53. std::string* choice_name;
  54. } alt_cons;
  55. struct {
  56. std::string* alt_name;
  57. std::string* choice_name;
  58. Value* arg;
  59. } alt;
  60. struct {
  61. std::vector<std::pair<std::string, Address>>* elts;
  62. } tuple;
  63. Address ptr;
  64. std::string* var_type;
  65. struct {
  66. std::string* name;
  67. Value* type;
  68. } var_pat;
  69. struct {
  70. Value* param;
  71. Value* ret;
  72. } fun_type;
  73. struct {
  74. Value* type;
  75. } ptr_type;
  76. struct {
  77. std::string* name;
  78. VarValues* fields;
  79. VarValues* methods;
  80. } struct_type;
  81. struct {
  82. std::string* name;
  83. VarValues* fields;
  84. } tuple_type;
  85. struct {
  86. std::string* name;
  87. VarValues* alternatives;
  88. } choice_type;
  89. struct {
  90. std::list<std::string*>* params;
  91. Value* type;
  92. } implicit;
  93. } u;
  94. };
  95. auto MakeIntVal(int i) -> Value*;
  96. auto MakeBoolVal(bool b) -> Value*;
  97. auto MakeFunVal(std::string name, Value* param, Statement* body) -> Value*;
  98. auto MakePtrVal(Address addr) -> Value*;
  99. auto MakeStructVal(Value* type, Value* inits) -> Value*;
  100. auto MakeTupleVal(std::vector<std::pair<std::string, Address>>* elts) -> Value*;
  101. auto MakeAltVal(std::string alt_name, std::string choice_name, Value* arg)
  102. -> Value*;
  103. auto MakeAltCons(std::string alt_name, std::string choice_name) -> Value*;
  104. auto MakeVarPatVal(std::string name, Value* type) -> Value*;
  105. auto MakeVarTypeVal(std::string name) -> Value*;
  106. auto MakeIntTypeVal() -> Value*;
  107. auto MakeAutoTypeVal() -> Value*;
  108. auto MakeBoolTypeVal() -> Value*;
  109. auto MakeTypeTypeVal() -> Value*;
  110. auto MakeFunTypeVal(Value* param, Value* ret) -> Value*;
  111. auto MakePtrTypeVal(Value* type) -> Value*;
  112. auto MakeStructTypeVal(std::string name, VarValues* fields, VarValues* methods)
  113. -> Value*;
  114. auto MakeTupleTypeVal(VarValues* fields) -> Value*;
  115. auto MakeVoidTypeVal() -> Value*;
  116. auto MakeChoiceTypeVal(std::string name, VarValues* alts) -> Value*;
  117. void PrintValue(Value* val, std::ostream& out);
  118. auto TypeEqual(Value* t1, Value* t2) -> bool;
  119. auto ValueEqual(Value* v1, Value* v2, int line_num) -> bool;
  120. auto ToInteger(Value* v) -> int;
  121. void CheckAlive(Value* v, int line_num);
  122. } // namespace Carbon
  123. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_