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