value.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <optional>
  8. #include <vector>
  9. #include "executable_semantics/ast/statement.h"
  10. #include "executable_semantics/interpreter/stack.h"
  11. namespace Carbon {
  12. struct Value;
  13. using Address = unsigned int;
  14. using VarValues = std::list<std::pair<std::string, const Value*>>;
  15. using VarAddresses = std::vector<std::pair<std::string, Address>>;
  16. auto FindInVarValues(const std::string& field, VarValues* inits)
  17. -> const Value*;
  18. auto FieldsEqual(VarValues* ts1, VarValues* ts2) -> bool;
  19. // Finds the field in `*tuple` named `name`, and returns its address, or
  20. // nullopt if there is no such field. `*tuple` must be a tuple value.
  21. auto FindTupleField(const std::string& name, const Value* tuple)
  22. -> std::optional<Address>;
  23. enum class ValKind {
  24. IntV,
  25. FunV,
  26. PtrV,
  27. BoolV,
  28. StructV,
  29. AltV,
  30. TupleV,
  31. VarTV,
  32. IntTV,
  33. BoolTV,
  34. TypeTV,
  35. FunctionTV,
  36. PointerTV,
  37. AutoTV,
  38. StructTV,
  39. ChoiceTV,
  40. ContinuationTV, // The type of a continuation.
  41. VarPatV,
  42. AltConsV,
  43. ContinuationV // A first-class continuation value.
  44. };
  45. struct Frame; // used by continuation
  46. struct Value {
  47. ValKind tag;
  48. union {
  49. int integer;
  50. bool boolean;
  51. struct {
  52. std::string* name;
  53. const Value* param;
  54. const Statement* body;
  55. } fun;
  56. struct {
  57. const Value* type;
  58. const Value* inits;
  59. } struct_val;
  60. struct {
  61. std::string* alt_name;
  62. std::string* choice_name;
  63. } alt_cons;
  64. struct {
  65. std::string* alt_name;
  66. std::string* choice_name;
  67. Address argument;
  68. } alt;
  69. struct {
  70. VarAddresses* elts;
  71. } tuple;
  72. Address ptr;
  73. std::string* var_type;
  74. struct {
  75. std::string* name;
  76. const Value* type;
  77. } var_pat;
  78. struct {
  79. const Value* param;
  80. const Value* ret;
  81. } fun_type;
  82. struct {
  83. const Value* type;
  84. } ptr_type;
  85. struct {
  86. std::string* name;
  87. VarValues* fields;
  88. VarValues* methods;
  89. } struct_type;
  90. struct {
  91. std::string* name;
  92. VarValues* alternatives;
  93. } choice_type;
  94. struct {
  95. std::list<std::string*>* params;
  96. const Value* type;
  97. } implicit;
  98. struct {
  99. std::vector<Frame*>* stack;
  100. } continuation;
  101. } u;
  102. };
  103. // Return a first-class continuation represented by the
  104. // given stack, down to the nearest enclosing `__continuation`.
  105. auto MakeContinuation(std::vector<Frame*> stack) -> Value*;
  106. auto MakeIntVal(int i) -> const Value*;
  107. auto MakeBoolVal(bool b) -> const Value*;
  108. auto MakeFunVal(std::string name, const Value* param, const Statement* body)
  109. -> const Value*;
  110. auto MakePtrVal(Address addr) -> const Value*;
  111. auto MakeStructVal(const Value* type, const Value* inits) -> const Value*;
  112. auto MakeTupleVal(std::vector<std::pair<std::string, Address>>* elts)
  113. -> const Value*;
  114. auto MakeAltVal(std::string alt_name, std::string choice_name, Address argument)
  115. -> const Value*;
  116. auto MakeAltCons(std::string alt_name, std::string choice_name) -> const Value*;
  117. auto MakeVarPatVal(std::string name, const Value* type) -> const Value*;
  118. auto MakeVarTypeVal(std::string name) -> const Value*;
  119. auto MakeIntTypeVal() -> const Value*;
  120. auto MakeContinuationTypeVal() -> const Value*;
  121. auto MakeAutoTypeVal() -> const Value*;
  122. auto MakeBoolTypeVal() -> const Value*;
  123. auto MakeTypeTypeVal() -> const Value*;
  124. auto MakeFunTypeVal(const Value* param, const Value* ret) -> const Value*;
  125. auto MakePtrTypeVal(const Value* type) -> const Value*;
  126. auto MakeStructTypeVal(std::string name, VarValues* fields, VarValues* methods)
  127. -> const Value*;
  128. auto MakeVoidTypeVal() -> const Value*;
  129. auto MakeChoiceTypeVal(std::string name, VarValues* alts) -> const Value*;
  130. void PrintValue(const Value* val, std::ostream& out);
  131. auto TypeEqual(const Value* t1, const Value* t2) -> bool;
  132. auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool;
  133. auto ToInteger(const Value* v) -> int;
  134. } // namespace Carbon
  135. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_