value.cpp 15 KB

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