value.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, const Value*>>;
  13. auto FindInVarValues(const std::string& field, VarValues* inits)
  14. -> const Value*;
  15. auto FieldsEqual(VarValues* ts1, VarValues* ts2) -> bool;
  16. enum class ValKind {
  17. IntV,
  18. FunV,
  19. PtrV,
  20. BoolV,
  21. StructV,
  22. AltV,
  23. TupleV,
  24. VarTV,
  25. IntTV,
  26. BoolTV,
  27. TypeTV,
  28. FunctionTV,
  29. PointerTV,
  30. AutoTV,
  31. TupleTV,
  32. StructTV,
  33. ChoiceTV,
  34. VarPatV,
  35. AltConsV
  36. };
  37. struct Value {
  38. ValKind tag;
  39. union {
  40. int integer;
  41. bool boolean;
  42. struct {
  43. std::string* name;
  44. const Value* param;
  45. Statement* body;
  46. } fun;
  47. struct {
  48. const Value* type;
  49. const 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. Address argument;
  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. const Value* type;
  68. } var_pat;
  69. struct {
  70. const Value* param;
  71. const Value* ret;
  72. } fun_type;
  73. struct {
  74. const 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. const Value* type;
  92. } implicit;
  93. } u;
  94. };
  95. auto MakeIntVal(int i) -> const Value*;
  96. auto MakeBoolVal(bool b) -> const Value*;
  97. auto MakeFunVal(std::string name, const Value* param, Statement* body)
  98. -> const Value*;
  99. auto MakePtrVal(Address addr) -> const Value*;
  100. auto MakeStructVal(const Value* type, const Value* inits) -> const Value*;
  101. auto MakeTupleVal(std::vector<std::pair<std::string, Address>>* elts)
  102. -> const Value*;
  103. auto MakeAltVal(std::string alt_name, std::string choice_name, Address argument)
  104. -> const Value*;
  105. auto MakeAltCons(std::string alt_name, std::string choice_name) -> const Value*;
  106. auto MakeVarPatVal(std::string name, const Value* type) -> const Value*;
  107. auto MakeVarTypeVal(std::string name) -> const Value*;
  108. auto MakeIntTypeVal() -> const Value*;
  109. auto MakeAutoTypeVal() -> const Value*;
  110. auto MakeBoolTypeVal() -> const Value*;
  111. auto MakeTypeTypeVal() -> const Value*;
  112. auto MakeFunTypeVal(const Value* param, const Value* ret) -> const Value*;
  113. auto MakePtrTypeVal(const Value* type) -> const Value*;
  114. auto MakeStructTypeVal(std::string name, VarValues* fields, VarValues* methods)
  115. -> const Value*;
  116. auto MakeTupleTypeVal(VarValues* fields) -> const Value*;
  117. auto MakeVoidTypeVal() -> const Value*;
  118. auto MakeChoiceTypeVal(std::string name, VarValues* alts) -> const Value*;
  119. void PrintValue(const Value* val, std::ostream& out);
  120. auto TypeEqual(const Value* t1, const Value* t2) -> bool;
  121. auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool;
  122. auto ToInteger(const Value* v) -> int;
  123. void CheckAlive(Address a, int line_num);
  124. } // namespace Carbon
  125. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_VALUE_H_