value.cpp 15 KB

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