value.cpp 15 KB

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