value.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include "executable_semantics/interpreter/value.h"
  5. #include <algorithm>
  6. #include "common/check.h"
  7. #include "executable_semantics/common/arena.h"
  8. #include "executable_semantics/common/error.h"
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/Support/Casting.h"
  11. namespace Carbon {
  12. using llvm::cast;
  13. auto FindInVarValues(const std::string& field, const VarValues& inits)
  14. -> const Value* {
  15. for (auto& i : inits) {
  16. if (i.first == field) {
  17. return i.second;
  18. }
  19. }
  20. return nullptr;
  21. }
  22. auto FieldsEqual(const VarValues& ts1, const VarValues& ts2) -> bool {
  23. if (ts1.size() == ts2.size()) {
  24. for (auto& iter1 : ts1) {
  25. auto t2 = FindInVarValues(iter1.first, ts2);
  26. if (t2 == nullptr) {
  27. return false;
  28. }
  29. if (!TypeEqual(iter1.second, t2)) {
  30. return false;
  31. }
  32. }
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }
  38. auto TupleValue::FindField(const std::string& name) const -> const Value* {
  39. for (const TupleElement& element : elements) {
  40. if (element.name == name) {
  41. return element.value;
  42. }
  43. }
  44. return nullptr;
  45. }
  46. namespace {
  47. auto GetMember(const Value* v, const std::string& f, int line_num)
  48. -> const Value* {
  49. switch (v->Tag()) {
  50. case Value::Kind::StructValue: {
  51. const Value* field =
  52. cast<TupleValue>(*cast<StructValue>(*v).Inits()).FindField(f);
  53. if (field == nullptr) {
  54. FATAL_RUNTIME_ERROR(line_num) << "member " << f << " not in " << *v;
  55. }
  56. return field;
  57. }
  58. case Value::Kind::TupleValue: {
  59. const Value* field = cast<TupleValue>(*v).FindField(f);
  60. if (field == nullptr) {
  61. FATAL_RUNTIME_ERROR(line_num) << "field " << f << " not in " << *v;
  62. }
  63. return field;
  64. }
  65. case Value::Kind::ChoiceType: {
  66. const auto& choice = cast<ChoiceType>(*v);
  67. if (FindInVarValues(f, choice.Alternatives()) == nullptr) {
  68. FATAL_RUNTIME_ERROR(line_num)
  69. << "alternative " << f << " not in " << *v;
  70. }
  71. return global_arena->New<AlternativeConstructorValue>(f, choice.Name());
  72. }
  73. default:
  74. FATAL() << "field access not allowed for value " << *v;
  75. }
  76. }
  77. } // namespace
  78. auto Value::GetField(const FieldPath& path, int line_num) const
  79. -> const Value* {
  80. const Value* value = this;
  81. for (const std::string& field : path.components) {
  82. value = GetMember(value, field, line_num);
  83. }
  84. return value;
  85. }
  86. namespace {
  87. auto SetFieldImpl(const Value* value,
  88. std::vector<std::string>::const_iterator path_begin,
  89. std::vector<std::string>::const_iterator path_end,
  90. const Value* field_value, int line_num) -> const Value* {
  91. if (path_begin == path_end) {
  92. return field_value;
  93. }
  94. switch (value->Tag()) {
  95. case Value::Kind::StructValue: {
  96. return SetFieldImpl(cast<StructValue>(*value).Inits(), path_begin,
  97. path_end, field_value, line_num);
  98. }
  99. case Value::Kind::TupleValue: {
  100. std::vector<TupleElement> elements = cast<TupleValue>(*value).Elements();
  101. auto it = std::find_if(elements.begin(), elements.end(),
  102. [path_begin](const TupleElement& element) {
  103. return element.name == *path_begin;
  104. });
  105. if (it == elements.end()) {
  106. FATAL_RUNTIME_ERROR(line_num)
  107. << "field " << *path_begin << " not in " << *value;
  108. }
  109. it->value = SetFieldImpl(it->value, path_begin + 1, path_end, field_value,
  110. line_num);
  111. return global_arena->New<TupleValue>(elements);
  112. }
  113. default:
  114. FATAL() << "field access not allowed for value " << *value;
  115. }
  116. }
  117. } // namespace
  118. auto Value::SetField(const FieldPath& path, const Value* field_value,
  119. int line_num) const -> const Value* {
  120. return SetFieldImpl(this, path.components.begin(), path.components.end(),
  121. field_value, line_num);
  122. }
  123. void Value::Print(llvm::raw_ostream& out) const {
  124. switch (Tag()) {
  125. case Value::Kind::AlternativeConstructorValue: {
  126. const auto& alt = cast<AlternativeConstructorValue>(*this);
  127. out << alt.ChoiceName() << "." << alt.AltName();
  128. break;
  129. }
  130. case Value::Kind::BindingPlaceholderValue: {
  131. const auto& placeholder = cast<BindingPlaceholderValue>(*this);
  132. if (placeholder.Name().has_value()) {
  133. out << *placeholder.Name();
  134. } else {
  135. out << "_";
  136. }
  137. out << ": " << *placeholder.Type();
  138. break;
  139. }
  140. case Value::Kind::AlternativeValue: {
  141. const auto& alt = cast<AlternativeValue>(*this);
  142. out << "alt " << alt.ChoiceName() << "." << alt.AltName() << " "
  143. << *alt.Argument();
  144. break;
  145. }
  146. case Value::Kind::StructValue: {
  147. const auto& s = cast<StructValue>(*this);
  148. out << cast<StructType>(*s.Type()).Name() << *s.Inits();
  149. break;
  150. }
  151. case Value::Kind::TupleValue: {
  152. out << "(";
  153. llvm::ListSeparator sep;
  154. for (const TupleElement& element : cast<TupleValue>(*this).Elements()) {
  155. out << sep << element.name << " = " << *element.value;
  156. }
  157. out << ")";
  158. break;
  159. }
  160. case Value::Kind::IntValue:
  161. out << cast<IntValue>(*this).Val();
  162. break;
  163. case Value::Kind::BoolValue:
  164. out << (cast<BoolValue>(*this).Val() ? "true" : "false");
  165. break;
  166. case Value::Kind::FunctionValue:
  167. out << "fun<" << cast<FunctionValue>(*this).Name() << ">";
  168. break;
  169. case Value::Kind::PointerValue:
  170. out << "ptr<" << cast<PointerValue>(*this).Val() << ">";
  171. break;
  172. case Value::Kind::BoolType:
  173. out << "Bool";
  174. break;
  175. case Value::Kind::IntType:
  176. out << "i32";
  177. break;
  178. case Value::Kind::TypeType:
  179. out << "Type";
  180. break;
  181. case Value::Kind::AutoType:
  182. out << "auto";
  183. break;
  184. case Value::Kind::ContinuationType:
  185. out << "Continuation";
  186. break;
  187. case Value::Kind::PointerType:
  188. out << *cast<PointerType>(*this).Type() << "*";
  189. break;
  190. case Value::Kind::FunctionType: {
  191. const auto& fn_type = cast<FunctionType>(*this);
  192. out << "fn ";
  193. if (fn_type.Deduced().size() > 0) {
  194. out << "[";
  195. unsigned int i = 0;
  196. for (const auto& deduced : fn_type.Deduced()) {
  197. if (i != 0) {
  198. out << ", ";
  199. }
  200. out << deduced.name << ":! " << *deduced.type;
  201. ++i;
  202. }
  203. out << "]";
  204. }
  205. out << *fn_type.Param() << " -> " << *fn_type.Ret();
  206. break;
  207. }
  208. case Value::Kind::StructType:
  209. out << "struct " << cast<StructType>(*this).Name();
  210. break;
  211. case Value::Kind::ChoiceType:
  212. out << "choice " << cast<ChoiceType>(*this).Name();
  213. break;
  214. case Value::Kind::VariableType:
  215. out << cast<VariableType>(*this).Name();
  216. break;
  217. case Value::Kind::ContinuationValue:
  218. out << "continuation";
  219. // TODO: Find a way to print useful information about the continuation
  220. // without creating a dependency cycle.
  221. break;
  222. }
  223. }
  224. auto CopyVal(const Value* val, int line_num) -> const Value* {
  225. switch (val->Tag()) {
  226. case Value::Kind::TupleValue: {
  227. std::vector<TupleElement> elements;
  228. for (const TupleElement& element : cast<TupleValue>(*val).Elements()) {
  229. elements.push_back(
  230. {.name = element.name, .value = CopyVal(element.value, line_num)});
  231. }
  232. return global_arena->New<TupleValue>(std::move(elements));
  233. }
  234. case Value::Kind::AlternativeValue: {
  235. const auto& alt = cast<AlternativeValue>(*val);
  236. const Value* arg = CopyVal(alt.Argument(), line_num);
  237. return global_arena->New<AlternativeValue>(alt.AltName(),
  238. alt.ChoiceName(), arg);
  239. }
  240. case Value::Kind::StructValue: {
  241. const auto& s = cast<StructValue>(*val);
  242. const Value* inits = CopyVal(s.Inits(), line_num);
  243. return global_arena->New<StructValue>(s.Type(), inits);
  244. }
  245. case Value::Kind::IntValue:
  246. return global_arena->New<IntValue>(cast<IntValue>(*val).Val());
  247. case Value::Kind::BoolValue:
  248. return global_arena->New<BoolValue>(cast<BoolValue>(*val).Val());
  249. case Value::Kind::FunctionValue: {
  250. const auto& fn_value = cast<FunctionValue>(*val);
  251. return global_arena->New<FunctionValue>(fn_value.Name(), fn_value.Param(),
  252. fn_value.Body());
  253. }
  254. case Value::Kind::PointerValue:
  255. return global_arena->New<PointerValue>(cast<PointerValue>(*val).Val());
  256. case Value::Kind::ContinuationValue:
  257. // Copying a continuation is "shallow".
  258. return val;
  259. case Value::Kind::FunctionType: {
  260. const auto& fn_type = cast<FunctionType>(*val);
  261. return global_arena->New<FunctionType>(fn_type.Deduced(),
  262. CopyVal(fn_type.Param(), line_num),
  263. CopyVal(fn_type.Ret(), line_num));
  264. }
  265. case Value::Kind::PointerType:
  266. return global_arena->New<PointerType>(
  267. CopyVal(cast<PointerType>(*val).Type(), line_num));
  268. case Value::Kind::IntType:
  269. return global_arena->New<IntType>();
  270. case Value::Kind::BoolType:
  271. return global_arena->New<BoolType>();
  272. case Value::Kind::TypeType:
  273. return global_arena->New<TypeType>();
  274. case Value::Kind::AutoType:
  275. return global_arena->New<AutoType>();
  276. case Value::Kind::ContinuationType:
  277. return global_arena->New<ContinuationType>();
  278. case Value::Kind::VariableType:
  279. case Value::Kind::StructType:
  280. case Value::Kind::ChoiceType:
  281. case Value::Kind::BindingPlaceholderValue:
  282. case Value::Kind::AlternativeConstructorValue:
  283. // TODO: These should be copied so that they don't get destructed.
  284. return val;
  285. }
  286. }
  287. auto TypeEqual(const Value* t1, const Value* t2) -> bool {
  288. if (t1->Tag() != t2->Tag()) {
  289. return false;
  290. }
  291. switch (t1->Tag()) {
  292. case Value::Kind::PointerType:
  293. return TypeEqual(cast<PointerType>(*t1).Type(),
  294. cast<PointerType>(*t2).Type());
  295. case Value::Kind::FunctionType: {
  296. const auto& fn1 = cast<FunctionType>(*t1);
  297. const auto& fn2 = cast<FunctionType>(*t2);
  298. return TypeEqual(fn1.Param(), fn2.Param()) &&
  299. TypeEqual(fn1.Ret(), fn2.Ret());
  300. }
  301. case Value::Kind::StructType:
  302. return cast<StructType>(*t1).Name() == cast<StructType>(*t2).Name();
  303. case Value::Kind::ChoiceType:
  304. return cast<ChoiceType>(*t1).Name() == cast<ChoiceType>(*t2).Name();
  305. case Value::Kind::TupleValue: {
  306. const auto& tup1 = cast<TupleValue>(*t1);
  307. const auto& tup2 = cast<TupleValue>(*t2);
  308. if (tup1.Elements().size() != tup2.Elements().size()) {
  309. return false;
  310. }
  311. for (size_t i = 0; i < tup1.Elements().size(); ++i) {
  312. if (tup1.Elements()[i].name != tup2.Elements()[i].name ||
  313. !TypeEqual(tup1.Elements()[i].value, tup2.Elements()[i].value)) {
  314. return false;
  315. }
  316. }
  317. return true;
  318. }
  319. case Value::Kind::IntType:
  320. case Value::Kind::BoolType:
  321. case Value::Kind::ContinuationType:
  322. case Value::Kind::TypeType:
  323. return true;
  324. case Value::Kind::VariableType:
  325. return cast<VariableType>(*t1).Name() == cast<VariableType>(*t2).Name();
  326. default:
  327. FATAL() << "TypeEqual used to compare non-type values\n"
  328. << *t1 << "\n"
  329. << *t2;
  330. }
  331. }
  332. // Returns true if all the fields of the two tuples contain equal values
  333. // and returns false otherwise.
  334. static auto FieldsValueEqual(const std::vector<TupleElement>& ts1,
  335. const std::vector<TupleElement>& ts2, int line_num)
  336. -> bool {
  337. if (ts1.size() != ts2.size()) {
  338. return false;
  339. }
  340. for (const TupleElement& element : ts1) {
  341. auto iter = std::find_if(
  342. ts2.begin(), ts2.end(),
  343. [&](const TupleElement& e2) { return e2.name == element.name; });
  344. if (iter == ts2.end()) {
  345. return false;
  346. }
  347. if (!ValueEqual(element.value, iter->value, line_num)) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. // Returns true if the two values are equal and returns false otherwise.
  354. //
  355. // This function implements the `==` operator of Carbon.
  356. auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
  357. if (v1->Tag() != v2->Tag()) {
  358. return false;
  359. }
  360. switch (v1->Tag()) {
  361. case Value::Kind::IntValue:
  362. return cast<IntValue>(*v1).Val() == cast<IntValue>(*v2).Val();
  363. case Value::Kind::BoolValue:
  364. return cast<BoolValue>(*v1).Val() == cast<BoolValue>(*v2).Val();
  365. case Value::Kind::PointerValue:
  366. return cast<PointerValue>(*v1).Val() == cast<PointerValue>(*v2).Val();
  367. case Value::Kind::FunctionValue:
  368. return cast<FunctionValue>(*v1).Body() == cast<FunctionValue>(*v2).Body();
  369. case Value::Kind::TupleValue:
  370. return FieldsValueEqual(cast<TupleValue>(*v1).Elements(),
  371. cast<TupleValue>(*v2).Elements(), line_num);
  372. case Value::Kind::IntType:
  373. case Value::Kind::BoolType:
  374. case Value::Kind::TypeType:
  375. case Value::Kind::FunctionType:
  376. case Value::Kind::PointerType:
  377. case Value::Kind::AutoType:
  378. case Value::Kind::StructType:
  379. case Value::Kind::ChoiceType:
  380. case Value::Kind::ContinuationType:
  381. case Value::Kind::VariableType:
  382. return TypeEqual(v1, v2);
  383. case Value::Kind::StructValue:
  384. case Value::Kind::AlternativeValue:
  385. case Value::Kind::BindingPlaceholderValue:
  386. case Value::Kind::AlternativeConstructorValue:
  387. case Value::Kind::ContinuationValue:
  388. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  389. }
  390. }
  391. } // namespace Carbon