value.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. case Value::Kind::StringType:
  223. out << "String";
  224. break;
  225. case Value::Kind::StringValue:
  226. out << "\"";
  227. out.write_escaped(cast<StringValue>(*this).Val());
  228. out << "\"";
  229. break;
  230. }
  231. }
  232. auto CopyVal(const Value* val, int line_num) -> const Value* {
  233. switch (val->Tag()) {
  234. case Value::Kind::TupleValue: {
  235. std::vector<TupleElement> elements;
  236. for (const TupleElement& element : cast<TupleValue>(*val).Elements()) {
  237. elements.push_back(
  238. {.name = element.name, .value = CopyVal(element.value, line_num)});
  239. }
  240. return global_arena->New<TupleValue>(std::move(elements));
  241. }
  242. case Value::Kind::AlternativeValue: {
  243. const auto& alt = cast<AlternativeValue>(*val);
  244. const Value* arg = CopyVal(alt.Argument(), line_num);
  245. return global_arena->New<AlternativeValue>(alt.AltName(),
  246. alt.ChoiceName(), arg);
  247. }
  248. case Value::Kind::StructValue: {
  249. const auto& s = cast<StructValue>(*val);
  250. const Value* inits = CopyVal(s.Inits(), line_num);
  251. return global_arena->New<StructValue>(s.Type(), inits);
  252. }
  253. case Value::Kind::IntValue:
  254. return global_arena->New<IntValue>(cast<IntValue>(*val).Val());
  255. case Value::Kind::BoolValue:
  256. return global_arena->New<BoolValue>(cast<BoolValue>(*val).Val());
  257. case Value::Kind::FunctionValue: {
  258. const auto& fn_value = cast<FunctionValue>(*val);
  259. return global_arena->New<FunctionValue>(fn_value.Name(), fn_value.Param(),
  260. fn_value.Body());
  261. }
  262. case Value::Kind::PointerValue:
  263. return global_arena->New<PointerValue>(cast<PointerValue>(*val).Val());
  264. case Value::Kind::ContinuationValue:
  265. // Copying a continuation is "shallow".
  266. return val;
  267. case Value::Kind::FunctionType: {
  268. const auto& fn_type = cast<FunctionType>(*val);
  269. return global_arena->New<FunctionType>(fn_type.Deduced(),
  270. CopyVal(fn_type.Param(), line_num),
  271. CopyVal(fn_type.Ret(), line_num));
  272. }
  273. case Value::Kind::PointerType:
  274. return global_arena->New<PointerType>(
  275. CopyVal(cast<PointerType>(*val).Type(), line_num));
  276. case Value::Kind::IntType:
  277. return global_arena->New<IntType>();
  278. case Value::Kind::BoolType:
  279. return global_arena->New<BoolType>();
  280. case Value::Kind::TypeType:
  281. return global_arena->New<TypeType>();
  282. case Value::Kind::AutoType:
  283. return global_arena->New<AutoType>();
  284. case Value::Kind::ContinuationType:
  285. return global_arena->New<ContinuationType>();
  286. case Value::Kind::StringType:
  287. return global_arena->New<StringType>();
  288. case Value::Kind::StringValue:
  289. return global_arena->New<StringValue>(cast<StringValue>(*val).Val());
  290. case Value::Kind::VariableType:
  291. case Value::Kind::StructType:
  292. case Value::Kind::ChoiceType:
  293. case Value::Kind::BindingPlaceholderValue:
  294. case Value::Kind::AlternativeConstructorValue:
  295. // TODO: These should be copied so that they don't get destructed.
  296. return val;
  297. }
  298. }
  299. auto TypeEqual(const Value* t1, const Value* t2) -> bool {
  300. if (t1->Tag() != t2->Tag()) {
  301. return false;
  302. }
  303. switch (t1->Tag()) {
  304. case Value::Kind::PointerType:
  305. return TypeEqual(cast<PointerType>(*t1).Type(),
  306. cast<PointerType>(*t2).Type());
  307. case Value::Kind::FunctionType: {
  308. const auto& fn1 = cast<FunctionType>(*t1);
  309. const auto& fn2 = cast<FunctionType>(*t2);
  310. return TypeEqual(fn1.Param(), fn2.Param()) &&
  311. TypeEqual(fn1.Ret(), fn2.Ret());
  312. }
  313. case Value::Kind::StructType:
  314. return cast<StructType>(*t1).Name() == cast<StructType>(*t2).Name();
  315. case Value::Kind::ChoiceType:
  316. return cast<ChoiceType>(*t1).Name() == cast<ChoiceType>(*t2).Name();
  317. case Value::Kind::TupleValue: {
  318. const auto& tup1 = cast<TupleValue>(*t1);
  319. const auto& tup2 = cast<TupleValue>(*t2);
  320. if (tup1.Elements().size() != tup2.Elements().size()) {
  321. return false;
  322. }
  323. for (size_t i = 0; i < tup1.Elements().size(); ++i) {
  324. if (tup1.Elements()[i].name != tup2.Elements()[i].name ||
  325. !TypeEqual(tup1.Elements()[i].value, tup2.Elements()[i].value)) {
  326. return false;
  327. }
  328. }
  329. return true;
  330. }
  331. case Value::Kind::IntType:
  332. case Value::Kind::BoolType:
  333. case Value::Kind::ContinuationType:
  334. case Value::Kind::TypeType:
  335. case Value::Kind::StringType:
  336. return true;
  337. case Value::Kind::VariableType:
  338. return cast<VariableType>(*t1).Name() == cast<VariableType>(*t2).Name();
  339. default:
  340. FATAL() << "TypeEqual used to compare non-type values\n"
  341. << *t1 << "\n"
  342. << *t2;
  343. }
  344. }
  345. // Returns true if all the fields of the two tuples contain equal values
  346. // and returns false otherwise.
  347. static auto FieldsValueEqual(const std::vector<TupleElement>& ts1,
  348. const std::vector<TupleElement>& ts2, int line_num)
  349. -> bool {
  350. if (ts1.size() != ts2.size()) {
  351. return false;
  352. }
  353. for (const TupleElement& element : ts1) {
  354. auto iter = std::find_if(
  355. ts2.begin(), ts2.end(),
  356. [&](const TupleElement& e2) { return e2.name == element.name; });
  357. if (iter == ts2.end()) {
  358. return false;
  359. }
  360. if (!ValueEqual(element.value, iter->value, line_num)) {
  361. return false;
  362. }
  363. }
  364. return true;
  365. }
  366. // Returns true if the two values are equal and returns false otherwise.
  367. //
  368. // This function implements the `==` operator of Carbon.
  369. auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
  370. if (v1->Tag() != v2->Tag()) {
  371. return false;
  372. }
  373. switch (v1->Tag()) {
  374. case Value::Kind::IntValue:
  375. return cast<IntValue>(*v1).Val() == cast<IntValue>(*v2).Val();
  376. case Value::Kind::BoolValue:
  377. return cast<BoolValue>(*v1).Val() == cast<BoolValue>(*v2).Val();
  378. case Value::Kind::PointerValue:
  379. return cast<PointerValue>(*v1).Val() == cast<PointerValue>(*v2).Val();
  380. case Value::Kind::FunctionValue:
  381. return cast<FunctionValue>(*v1).Body() == cast<FunctionValue>(*v2).Body();
  382. case Value::Kind::TupleValue:
  383. return FieldsValueEqual(cast<TupleValue>(*v1).Elements(),
  384. cast<TupleValue>(*v2).Elements(), line_num);
  385. case Value::Kind::StringValue:
  386. return cast<StringValue>(*v1).Val() == cast<StringValue>(*v2).Val();
  387. case Value::Kind::IntType:
  388. case Value::Kind::BoolType:
  389. case Value::Kind::TypeType:
  390. case Value::Kind::FunctionType:
  391. case Value::Kind::PointerType:
  392. case Value::Kind::AutoType:
  393. case Value::Kind::StructType:
  394. case Value::Kind::ChoiceType:
  395. case Value::Kind::ContinuationType:
  396. case Value::Kind::VariableType:
  397. case Value::Kind::StringType:
  398. return TypeEqual(v1, v2);
  399. case Value::Kind::StructValue:
  400. case Value::Kind::AlternativeValue:
  401. case Value::Kind::BindingPlaceholderValue:
  402. case Value::Kind::AlternativeConstructorValue:
  403. case Value::Kind::ContinuationValue:
  404. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  405. }
  406. }
  407. } // namespace Carbon