value.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. IntV,
  31. FunV,
  32. PtrV,
  33. BoolV,
  34. StructV,
  35. AltV,
  36. TupleV,
  37. VarTV,
  38. IntTV,
  39. BoolTV,
  40. TypeTV,
  41. FunctionTV,
  42. PointerTV,
  43. AutoTV,
  44. StructTV,
  45. ChoiceTV,
  46. ContinuationTV, // The type of a continuation.
  47. VarPatV,
  48. AltConsV,
  49. ContinuationV // A first-class continuation value.
  50. };
  51. struct Frame; // used by continuation
  52. struct Function {
  53. std::string* name;
  54. const Value* param;
  55. const Statement* body;
  56. };
  57. struct StructConstructor {
  58. const Value* type;
  59. const Value* inits;
  60. };
  61. struct AlternativeConstructor {
  62. std::string* alt_name;
  63. std::string* choice_name;
  64. };
  65. struct Alternative {
  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 VariablePatternValue {
  74. std::string* name;
  75. const Value* type;
  76. };
  77. struct FunctionTypeValue {
  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 MakeContinuation(std::vector<Frame*> stack) -> Value*;
  102. static auto MakeIntVal(int i) -> const Value*;
  103. static auto MakeBoolVal(bool b) -> const Value*;
  104. static auto MakeFunVal(std::string name, const Value* param,
  105. const Statement* body) -> const Value*;
  106. static auto MakePtrVal(Address addr) -> const Value*;
  107. static auto MakeStructVal(const Value* type, const Value* inits)
  108. -> const Value*;
  109. static auto MakeTupleVal(std::vector<TupleElement>* elts) -> const Value*;
  110. static auto MakeAltVal(std::string alt_name, std::string choice_name,
  111. Address argument) -> const Value*;
  112. static auto MakeAltCons(std::string alt_name, std::string choice_name)
  113. -> const Value*;
  114. static auto MakeVarPatVal(std::string name, const Value* type)
  115. -> const Value*;
  116. static auto MakeVarTypeVal(std::string name) -> const Value*;
  117. static auto MakeIntTypeVal() -> const Value*;
  118. static auto MakeContinuationTypeVal() -> const Value*;
  119. static auto MakeAutoTypeVal() -> const Value*;
  120. static auto MakeBoolTypeVal() -> const Value*;
  121. static auto MakeTypeTypeVal() -> const Value*;
  122. static auto MakeFunTypeVal(const Value* param, const Value* ret)
  123. -> const Value*;
  124. static auto MakePtrTypeVal(const Value* type) -> const Value*;
  125. static auto MakeStructTypeVal(std::string name, VarValues* fields,
  126. VarValues* methods) -> const Value*;
  127. static auto MakeVoidTypeVal() -> const Value*;
  128. static auto MakeChoiceTypeVal(std::string name, VarValues* alts)
  129. -> const Value*;
  130. // Access to alternatives
  131. int GetInteger() const;
  132. bool GetBoolean() const;
  133. Function GetFunction() const;
  134. StructConstructor GetStruct() const;
  135. AlternativeConstructor GetAlternativeConstructor() const;
  136. Alternative GetAlternative() const;
  137. TupleValue GetTuple() const;
  138. Address GetPointer() const;
  139. std::string* GetVariableType() const;
  140. VariablePatternValue GetVariablePattern() const;
  141. FunctionTypeValue GetFunctionType() const;
  142. PointerType GetPointerType() const;
  143. StructType GetStructType() const;
  144. ChoiceType GetChoiceType() const;
  145. ContinuationValue GetContinuation() const;
  146. private:
  147. union {
  148. int integer;
  149. bool boolean;
  150. Function fun;
  151. StructConstructor struct_val;
  152. AlternativeConstructor alt_cons;
  153. Alternative alt;
  154. TupleValue tuple;
  155. Address ptr;
  156. std::string* var_type;
  157. VariablePatternValue var_pat;
  158. FunctionTypeValue fun_type;
  159. PointerType ptr_type;
  160. StructType struct_type;
  161. ChoiceType choice_type;
  162. ContinuationValue continuation;
  163. } u;
  164. };
  165. void PrintValue(const Value* val, std::ostream& out);
  166. auto TypeEqual(const Value* t1, const Value* t2) -> bool;
  167. auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool;
  168. auto ToInteger(const Value* v) -> int;
  169. } // namespace Carbon
  170. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_