value.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. // A TupleElement represents the value of a single tuple field.
  23. struct TupleElement {
  24. // The field name.
  25. std::string name;
  26. // Location of the field's value.
  27. Address address;
  28. };
  29. enum class ValKind {
  30. IntValue,
  31. FunctionValue,
  32. PointerValue,
  33. BoolValue,
  34. StructValue,
  35. AlternativeValue,
  36. TupleValue,
  37. VarTV,
  38. IntType,
  39. BoolType,
  40. TypeType,
  41. FunctionType,
  42. PointerType,
  43. AutoType,
  44. StructType,
  45. ChoiceType,
  46. ContinuationType, // The type of a continuation.
  47. BindingPlaceholderValue,
  48. AlternativeConstructorValue,
  49. ContinuationValue // A first-class continuation value.
  50. };
  51. struct Frame; // used by continuation
  52. struct FunctionValue {
  53. std::string* name;
  54. const Value* param;
  55. const Statement* body;
  56. };
  57. struct StructValue {
  58. const Value* type;
  59. const Value* inits;
  60. };
  61. struct AlternativeConstructorValue {
  62. std::string* alt_name;
  63. std::string* choice_name;
  64. };
  65. struct AlternativeValue {
  66. std::string* alt_name;
  67. std::string* choice_name;
  68. Address argument;
  69. };
  70. struct TupleValue {
  71. std::vector<TupleElement>* elements;
  72. };
  73. struct BindingPlaceholderValue {
  74. std::string* name;
  75. const Value* type;
  76. };
  77. struct FunctionType {
  78. const Value* param;
  79. const Value* ret;
  80. };
  81. struct PointerType {
  82. const Value* type;
  83. };
  84. struct StructType {
  85. std::string* name;
  86. VarValues* fields;
  87. VarValues* methods;
  88. };
  89. struct ChoiceType {
  90. std::string* name;
  91. VarValues* alternatives;
  92. };
  93. struct ContinuationValue {
  94. std::vector<Frame*>* stack;
  95. };
  96. struct Value {
  97. ValKind tag;
  98. // Constructors
  99. // Return a first-class continuation represented by the
  100. // given stack, down to the nearest enclosing `__continuation`.
  101. static auto MakeContinuationValue(std::vector<Frame*> stack) -> Value*;
  102. static auto MakeIntValue(int i) -> const Value*;
  103. static auto MakeBoolValue(bool b) -> const Value*;
  104. static auto MakeFunctionValue(std::string name, const Value* param,
  105. const Statement* body) -> const Value*;
  106. static auto MakePointerValue(Address addr) -> const Value*;
  107. static auto MakeStructValue(const Value* type, const Value* inits)
  108. -> const Value*;
  109. static auto MakeTupleValue(std::vector<TupleElement>* elts) -> const Value*;
  110. static auto MakeAlternativeValue(std::string alt_name,
  111. std::string choice_name, Address argument)
  112. -> const Value*;
  113. static auto MakeAlternativeConstructorValue(std::string alt_name,
  114. std::string choice_name)
  115. -> const Value*;
  116. static auto MakeBindingPlaceholderValue(std::string name, const Value* type)
  117. -> const Value*;
  118. static auto MakeVarTypeVal(std::string name) -> const Value*;
  119. static auto MakeIntType() -> const Value*;
  120. static auto MakeContinuationType() -> const Value*;
  121. static auto MakeAutoType() -> const Value*;
  122. static auto MakeBoolType() -> const Value*;
  123. static auto MakeTypeType() -> const Value*;
  124. static auto MakeFunctionType(const Value* param, const Value* ret)
  125. -> const Value*;
  126. static auto MakePointerType(const Value* type) -> const Value*;
  127. static auto MakeStructType(std::string name, VarValues* fields,
  128. VarValues* methods) -> const Value*;
  129. static auto MakeUnitTypeVal() -> const Value*;
  130. static auto MakeChoiceType(std::string name, VarValues* alts) -> const Value*;
  131. // Access to alternatives
  132. int GetIntValue() const;
  133. bool GetBoolValue() const;
  134. FunctionValue GetFunctionValue() const;
  135. StructValue GetStructValue() const;
  136. AlternativeConstructorValue GetAlternativeConstructorValue() const;
  137. AlternativeValue GetAlternativeValue() const;
  138. TupleValue GetTupleValue() const;
  139. Address GetPointerValue() const;
  140. std::string* GetVariableType() const;
  141. BindingPlaceholderValue GetBindingPlaceholderValue() const;
  142. FunctionType GetFunctionType() const;
  143. PointerType GetPointerType() const;
  144. StructType GetStructType() const;
  145. ChoiceType GetChoiceType() const;
  146. ContinuationValue GetContinuationValue() const;
  147. private:
  148. union {
  149. int integer;
  150. bool boolean;
  151. FunctionValue fun;
  152. StructValue struct_val;
  153. AlternativeConstructorValue alt_cons;
  154. AlternativeValue alt;
  155. TupleValue tuple;
  156. Address ptr;
  157. std::string* var_type;
  158. BindingPlaceholderValue var_pat;
  159. FunctionType fun_type;
  160. PointerType ptr_type;
  161. StructType struct_type;
  162. ChoiceType choice_type;
  163. ContinuationValue continuation;
  164. } u;
  165. };
  166. void PrintValue(const Value* val, std::ostream& out);
  167. auto TypeEqual(const Value* t1, const Value* t2) -> bool;
  168. auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool;
  169. auto ToInteger(const Value* v) -> int;
  170. } // namespace Carbon
  171. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_