value.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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_builders.h"
  9. #include "executable_semantics/interpreter/action.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/Support/Casting.h"
  12. #include "llvm/Support/Error.h"
  13. namespace Carbon {
  14. using llvm::cast;
  15. auto StructValue::FindField(const std::string& name) const
  16. -> std::optional<Nonnull<const Value*>> {
  17. for (const NamedValue& element : elements_) {
  18. if (element.name == name) {
  19. return element.value;
  20. }
  21. }
  22. return std::nullopt;
  23. }
  24. static auto GetMember(Nonnull<Arena*> arena, Nonnull<const Value*> v,
  25. const FieldPath::Component& field,
  26. SourceLocation source_loc)
  27. -> ErrorOr<Nonnull<const Value*>> {
  28. const std::string& f = field.name();
  29. if (field.witness().has_value()) {
  30. Nonnull<const Witness*> witness = *field.witness();
  31. switch (witness->kind()) {
  32. case Value::Kind::Witness: {
  33. if (std::optional<Nonnull<const Declaration*>> mem_decl =
  34. FindMember(f, witness->declaration().members());
  35. mem_decl.has_value()) {
  36. const auto& fun_decl = cast<FunctionDeclaration>(**mem_decl);
  37. if (fun_decl.is_method()) {
  38. return arena->New<BoundMethodValue>(
  39. &fun_decl, v, witness->type_args(), witness->witnesses());
  40. } else {
  41. // Class function.
  42. auto fun = cast<FunctionValue>(*fun_decl.constant_value());
  43. return arena->New<FunctionValue>(&fun->declaration(),
  44. witness->type_args(),
  45. witness->witnesses());
  46. }
  47. } else {
  48. return CompilationError(source_loc)
  49. << "member " << f << " not in " << *witness;
  50. }
  51. }
  52. default:
  53. FATAL() << "expected Witness, not " << *witness;
  54. }
  55. }
  56. switch (v->kind()) {
  57. case Value::Kind::StructValue: {
  58. std::optional<Nonnull<const Value*>> field =
  59. cast<StructValue>(*v).FindField(f);
  60. if (field == std::nullopt) {
  61. return RuntimeError(source_loc) << "member " << f << " not in " << *v;
  62. }
  63. return *field;
  64. }
  65. case Value::Kind::NominalClassValue: {
  66. const auto& object = cast<NominalClassValue>(*v);
  67. // Look for a field
  68. std::optional<Nonnull<const Value*>> field =
  69. cast<StructValue>(object.inits()).FindField(f);
  70. if (field.has_value()) {
  71. return *field;
  72. } else {
  73. // Look for a method in the object's class
  74. const auto& class_type = cast<NominalClassType>(object.type());
  75. std::optional<Nonnull<const FunctionValue*>> func =
  76. class_type.FindFunction(f);
  77. if (func == std::nullopt) {
  78. return RuntimeError(source_loc) << "member " << f << " not in " << *v
  79. << " or its " << class_type;
  80. } else if ((*func)->declaration().is_method()) {
  81. // Found a method. Turn it into a bound method.
  82. const FunctionValue& m = cast<FunctionValue>(**func);
  83. return arena->New<BoundMethodValue>(&m.declaration(), &object,
  84. class_type.type_args(),
  85. class_type.witnesses());
  86. } else {
  87. // Found a class function
  88. return arena->New<FunctionValue>(&(*func)->declaration(),
  89. class_type.type_args(),
  90. class_type.witnesses());
  91. }
  92. }
  93. }
  94. case Value::Kind::ChoiceType: {
  95. const auto& choice = cast<ChoiceType>(*v);
  96. if (!choice.FindAlternative(f)) {
  97. return RuntimeError(source_loc)
  98. << "alternative " << f << " not in " << *v;
  99. }
  100. return arena->New<AlternativeConstructorValue>(f, choice.name());
  101. }
  102. case Value::Kind::NominalClassType: {
  103. // Access a class function.
  104. const NominalClassType& class_type = cast<NominalClassType>(*v);
  105. std::optional<Nonnull<const FunctionValue*>> fun =
  106. class_type.FindFunction(f);
  107. if (fun == std::nullopt) {
  108. return RuntimeError(source_loc)
  109. << "class function " << f << " not in " << *v;
  110. }
  111. return arena->New<FunctionValue>(&(*fun)->declaration(),
  112. class_type.type_args(),
  113. class_type.witnesses());
  114. }
  115. default:
  116. FATAL() << "field access not allowed for value " << *v;
  117. }
  118. }
  119. auto Value::GetField(Nonnull<Arena*> arena, const FieldPath& path,
  120. SourceLocation source_loc) const
  121. -> ErrorOr<Nonnull<const Value*>> {
  122. Nonnull<const Value*> value(this);
  123. for (const FieldPath::Component& field : path.components_) {
  124. ASSIGN_OR_RETURN(value, GetMember(arena, value, field, source_loc));
  125. }
  126. return value;
  127. }
  128. static auto SetFieldImpl(
  129. Nonnull<Arena*> arena, Nonnull<const Value*> value,
  130. std::vector<FieldPath::Component>::const_iterator path_begin,
  131. std::vector<FieldPath::Component>::const_iterator path_end,
  132. Nonnull<const Value*> field_value, SourceLocation source_loc)
  133. -> ErrorOr<Nonnull<const Value*>> {
  134. if (path_begin == path_end) {
  135. return field_value;
  136. }
  137. switch (value->kind()) {
  138. case Value::Kind::StructValue: {
  139. std::vector<NamedValue> elements = cast<StructValue>(*value).elements();
  140. auto it = std::find_if(elements.begin(), elements.end(),
  141. [path_begin](const NamedValue& element) {
  142. return element.name == (*path_begin).name();
  143. });
  144. if (it == elements.end()) {
  145. return RuntimeError(source_loc)
  146. << "field " << (*path_begin).name() << " not in " << *value;
  147. }
  148. ASSIGN_OR_RETURN(it->value,
  149. SetFieldImpl(arena, it->value, path_begin + 1, path_end,
  150. field_value, source_loc));
  151. return arena->New<StructValue>(elements);
  152. }
  153. case Value::Kind::NominalClassValue: {
  154. return SetFieldImpl(arena, &cast<NominalClassValue>(*value).inits(),
  155. path_begin, path_end, field_value, source_loc);
  156. }
  157. case Value::Kind::TupleValue: {
  158. std::vector<Nonnull<const Value*>> elements =
  159. cast<TupleValue>(*value).elements();
  160. // TODO(geoffromer): update FieldPath to hold integers as well as strings.
  161. int index = std::stoi((*path_begin).name());
  162. if (index < 0 || static_cast<size_t>(index) >= elements.size()) {
  163. return RuntimeError(source_loc) << "index " << (*path_begin).name()
  164. << " out of range in " << *value;
  165. }
  166. ASSIGN_OR_RETURN(elements[index],
  167. SetFieldImpl(arena, elements[index], path_begin + 1,
  168. path_end, field_value, source_loc));
  169. return arena->New<TupleValue>(elements);
  170. }
  171. default:
  172. FATAL() << "field access not allowed for value " << *value;
  173. }
  174. }
  175. auto Value::SetField(Nonnull<Arena*> arena, const FieldPath& path,
  176. Nonnull<const Value*> field_value,
  177. SourceLocation source_loc) const
  178. -> ErrorOr<Nonnull<const Value*>> {
  179. return SetFieldImpl(arena, Nonnull<const Value*>(this),
  180. path.components_.begin(), path.components_.end(),
  181. field_value, source_loc);
  182. }
  183. void Value::Print(llvm::raw_ostream& out) const {
  184. switch (kind()) {
  185. case Value::Kind::AlternativeConstructorValue: {
  186. const auto& alt = cast<AlternativeConstructorValue>(*this);
  187. out << alt.choice_name() << "." << alt.alt_name();
  188. break;
  189. }
  190. case Value::Kind::BindingPlaceholderValue: {
  191. const auto& placeholder = cast<BindingPlaceholderValue>(*this);
  192. out << "Placeholder<";
  193. if (placeholder.value_node().has_value()) {
  194. out << (*placeholder.value_node());
  195. } else {
  196. out << "_";
  197. }
  198. out << ">";
  199. break;
  200. }
  201. case Value::Kind::AlternativeValue: {
  202. const auto& alt = cast<AlternativeValue>(*this);
  203. out << "alt " << alt.choice_name() << "." << alt.alt_name() << " "
  204. << alt.argument();
  205. break;
  206. }
  207. case Value::Kind::StructValue: {
  208. const auto& struct_val = cast<StructValue>(*this);
  209. out << "{";
  210. llvm::ListSeparator sep;
  211. for (const NamedValue& element : struct_val.elements()) {
  212. out << sep << "." << element.name << " = " << *element.value;
  213. }
  214. out << "}";
  215. break;
  216. }
  217. case Value::Kind::NominalClassValue: {
  218. const auto& s = cast<NominalClassValue>(*this);
  219. out << cast<NominalClassType>(s.type()).declaration().name() << s.inits();
  220. break;
  221. }
  222. case Value::Kind::TupleValue: {
  223. out << "(";
  224. llvm::ListSeparator sep;
  225. for (Nonnull<const Value*> element : cast<TupleValue>(*this).elements()) {
  226. out << sep << *element;
  227. }
  228. out << ")";
  229. break;
  230. }
  231. case Value::Kind::IntValue:
  232. out << cast<IntValue>(*this).value();
  233. break;
  234. case Value::Kind::BoolValue:
  235. out << (cast<BoolValue>(*this).value() ? "true" : "false");
  236. break;
  237. case Value::Kind::FunctionValue:
  238. out << "fun<" << cast<FunctionValue>(*this).declaration().name() << ">";
  239. break;
  240. case Value::Kind::BoundMethodValue:
  241. out << "bound_method<"
  242. << cast<BoundMethodValue>(*this).declaration().name() << ">";
  243. break;
  244. case Value::Kind::PointerValue:
  245. out << "ptr<" << cast<PointerValue>(*this).address() << ">";
  246. break;
  247. case Value::Kind::LValue:
  248. out << "lval<" << cast<LValue>(*this).address() << ">";
  249. break;
  250. case Value::Kind::BoolType:
  251. out << "Bool";
  252. break;
  253. case Value::Kind::IntType:
  254. out << "i32";
  255. break;
  256. case Value::Kind::TypeType:
  257. out << "Type";
  258. break;
  259. case Value::Kind::AutoType:
  260. out << "auto";
  261. break;
  262. case Value::Kind::ContinuationType:
  263. out << "Continuation";
  264. break;
  265. case Value::Kind::PointerType:
  266. out << cast<PointerType>(*this).type() << "*";
  267. break;
  268. case Value::Kind::FunctionType: {
  269. const auto& fn_type = cast<FunctionType>(*this);
  270. out << "fn ";
  271. if (!fn_type.deduced().empty()) {
  272. out << "[";
  273. unsigned int i = 0;
  274. for (Nonnull<const GenericBinding*> deduced : fn_type.deduced()) {
  275. if (i != 0) {
  276. out << ", ";
  277. }
  278. out << deduced->name() << ":! " << deduced->type();
  279. ++i;
  280. }
  281. out << "]";
  282. }
  283. out << fn_type.parameters() << " -> " << fn_type.return_type();
  284. break;
  285. }
  286. case Value::Kind::StructType: {
  287. out << "{";
  288. llvm::ListSeparator sep;
  289. for (const auto& [name, type] : cast<StructType>(*this).fields()) {
  290. out << sep << "." << name << ": " << *type;
  291. }
  292. out << "}";
  293. break;
  294. }
  295. case Value::Kind::NominalClassType: {
  296. const auto& class_type = cast<NominalClassType>(*this);
  297. out << "class " << class_type.declaration().name();
  298. if (!class_type.type_args().empty()) {
  299. out << "(";
  300. llvm::ListSeparator sep;
  301. for (const auto& [bind, val] : class_type.type_args()) {
  302. out << sep << bind->name() << " = " << *val;
  303. }
  304. out << ")";
  305. }
  306. if (!class_type.impls().empty()) {
  307. out << " impls ";
  308. llvm::ListSeparator sep;
  309. for (const auto& [impl_bind, impl] : class_type.impls()) {
  310. out << sep << *impl;
  311. }
  312. }
  313. if (!class_type.witnesses().empty()) {
  314. out << " witnesses ";
  315. llvm::ListSeparator sep;
  316. for (const auto& [impl_bind, witness] : class_type.witnesses()) {
  317. out << sep << *witness;
  318. }
  319. }
  320. break;
  321. }
  322. case Value::Kind::InterfaceType: {
  323. const auto& iface_type = cast<InterfaceType>(*this);
  324. out << "interface " << iface_type.declaration().name();
  325. if (!iface_type.args().empty()) {
  326. out << "(";
  327. llvm::ListSeparator sep;
  328. for (const auto& [bind, val] : iface_type.args()) {
  329. out << sep << bind->name() << " = " << *val;
  330. }
  331. out << ")";
  332. }
  333. break;
  334. }
  335. case Value::Kind::Witness: {
  336. const auto& witness = cast<Witness>(*this);
  337. out << "witness " << *witness.declaration().impl_type() << " as "
  338. << witness.declaration().interface();
  339. break;
  340. }
  341. case Value::Kind::ChoiceType:
  342. out << "choice " << cast<ChoiceType>(*this).name();
  343. break;
  344. case Value::Kind::VariableType:
  345. out << cast<VariableType>(*this).binding();
  346. break;
  347. case Value::Kind::ContinuationValue: {
  348. out << cast<ContinuationValue>(*this).stack();
  349. break;
  350. }
  351. case Value::Kind::StringType:
  352. out << "String";
  353. break;
  354. case Value::Kind::StringValue:
  355. out << "\"";
  356. out.write_escaped(cast<StringValue>(*this).value());
  357. out << "\"";
  358. break;
  359. case Value::Kind::TypeOfClassType:
  360. out << "typeof(" << cast<TypeOfClassType>(*this).class_type() << ")";
  361. break;
  362. case Value::Kind::TypeOfInterfaceType:
  363. out << "typeof("
  364. << cast<TypeOfInterfaceType>(*this)
  365. .interface_type()
  366. .declaration()
  367. .name()
  368. << ")";
  369. break;
  370. case Value::Kind::TypeOfChoiceType:
  371. out << "typeof(" << cast<TypeOfChoiceType>(*this).choice_type().name()
  372. << ")";
  373. break;
  374. case Value::Kind::StaticArrayType: {
  375. const auto& array_type = cast<StaticArrayType>(*this);
  376. out << "[" << array_type.element_type() << "; " << array_type.size()
  377. << "]";
  378. break;
  379. }
  380. }
  381. }
  382. ContinuationValue::StackFragment::~StackFragment() {
  383. CHECK(reversed_todo_.empty())
  384. << "All StackFragments must be empty before the Carbon program ends.";
  385. }
  386. void ContinuationValue::StackFragment::StoreReversed(
  387. std::vector<std::unique_ptr<Action>> reversed_todo) {
  388. CHECK(reversed_todo_.empty());
  389. reversed_todo_ = std::move(reversed_todo);
  390. }
  391. void ContinuationValue::StackFragment::RestoreTo(
  392. Stack<std::unique_ptr<Action>>& todo) {
  393. while (!reversed_todo_.empty()) {
  394. todo.Push(std::move(reversed_todo_.back()));
  395. reversed_todo_.pop_back();
  396. }
  397. }
  398. void ContinuationValue::StackFragment::Clear() {
  399. // We destroy the underlying Actions explicitly to ensure they're
  400. // destroyed in the correct order.
  401. for (auto& action : reversed_todo_) {
  402. action.reset();
  403. }
  404. reversed_todo_.clear();
  405. }
  406. void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
  407. out << "{";
  408. llvm::ListSeparator sep(" :: ");
  409. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  410. out << sep << *action;
  411. }
  412. out << "}";
  413. }
  414. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
  415. if (t1->kind() != t2->kind()) {
  416. return false;
  417. }
  418. switch (t1->kind()) {
  419. case Value::Kind::PointerType:
  420. return TypeEqual(&cast<PointerType>(*t1).type(),
  421. &cast<PointerType>(*t2).type());
  422. case Value::Kind::FunctionType: {
  423. const auto& fn1 = cast<FunctionType>(*t1);
  424. const auto& fn2 = cast<FunctionType>(*t2);
  425. return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
  426. TypeEqual(&fn1.return_type(), &fn2.return_type());
  427. }
  428. case Value::Kind::StructType: {
  429. const auto& struct1 = cast<StructType>(*t1);
  430. const auto& struct2 = cast<StructType>(*t2);
  431. if (struct1.fields().size() != struct2.fields().size()) {
  432. return false;
  433. }
  434. for (size_t i = 0; i < struct1.fields().size(); ++i) {
  435. if (struct1.fields()[i].name != struct2.fields()[i].name ||
  436. !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
  437. return false;
  438. }
  439. }
  440. return true;
  441. }
  442. case Value::Kind::NominalClassType:
  443. if (cast<NominalClassType>(*t1).declaration().name() !=
  444. cast<NominalClassType>(*t2).declaration().name()) {
  445. return false;
  446. }
  447. for (const auto& [ty_var1, ty1] :
  448. cast<NominalClassType>(*t1).type_args()) {
  449. if (!ValueEqual(ty1,
  450. cast<NominalClassType>(*t2).type_args().at(ty_var1))) {
  451. return false;
  452. }
  453. }
  454. return true;
  455. case Value::Kind::InterfaceType:
  456. if (cast<InterfaceType>(*t1).declaration().name() !=
  457. cast<InterfaceType>(*t2).declaration().name()) {
  458. return false;
  459. }
  460. for (const auto& [ty_var1, ty1] : cast<InterfaceType>(*t1).args()) {
  461. if (!ValueEqual(ty1, cast<InterfaceType>(*t2).args().at(ty_var1))) {
  462. return false;
  463. }
  464. }
  465. return true;
  466. case Value::Kind::ChoiceType:
  467. return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
  468. case Value::Kind::TupleValue: {
  469. const auto& tup1 = cast<TupleValue>(*t1);
  470. const auto& tup2 = cast<TupleValue>(*t2);
  471. if (tup1.elements().size() != tup2.elements().size()) {
  472. return false;
  473. }
  474. for (size_t i = 0; i < tup1.elements().size(); ++i) {
  475. if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
  476. return false;
  477. }
  478. }
  479. return true;
  480. }
  481. case Value::Kind::IntType:
  482. case Value::Kind::BoolType:
  483. case Value::Kind::ContinuationType:
  484. case Value::Kind::TypeType:
  485. case Value::Kind::StringType:
  486. return true;
  487. case Value::Kind::VariableType:
  488. return &cast<VariableType>(*t1).binding() ==
  489. &cast<VariableType>(*t2).binding();
  490. case Value::Kind::TypeOfClassType:
  491. return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
  492. &cast<TypeOfClassType>(*t2).class_type());
  493. case Value::Kind::TypeOfInterfaceType:
  494. return TypeEqual(&cast<TypeOfInterfaceType>(*t1).interface_type(),
  495. &cast<TypeOfInterfaceType>(*t2).interface_type());
  496. case Value::Kind::TypeOfChoiceType:
  497. return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
  498. &cast<TypeOfChoiceType>(*t2).choice_type());
  499. case Value::Kind::StaticArrayType: {
  500. const auto& array1 = cast<StaticArrayType>(*t1);
  501. const auto& array2 = cast<StaticArrayType>(*t2);
  502. return TypeEqual(&array1.element_type(), &array2.element_type()) &&
  503. array1.size() == array2.size();
  504. }
  505. case Value::Kind::IntValue:
  506. case Value::Kind::BoolValue:
  507. case Value::Kind::FunctionValue:
  508. case Value::Kind::BoundMethodValue:
  509. case Value::Kind::StructValue:
  510. case Value::Kind::NominalClassValue:
  511. case Value::Kind::AlternativeValue:
  512. case Value::Kind::AlternativeConstructorValue:
  513. case Value::Kind::StringValue:
  514. case Value::Kind::PointerValue:
  515. case Value::Kind::LValue:
  516. case Value::Kind::BindingPlaceholderValue:
  517. case Value::Kind::ContinuationValue:
  518. FATAL() << "TypeEqual used to compare non-type values\n"
  519. << *t1 << "\n"
  520. << *t2;
  521. case Value::Kind::Witness:
  522. FATAL() << "TypeEqual: unexpected Witness";
  523. break;
  524. case Value::Kind::AutoType:
  525. FATAL() << "TypeEqual: unexpected AutoType";
  526. break;
  527. }
  528. }
  529. // Returns true if the two values are equal and returns false otherwise.
  530. //
  531. // This function implements the `==` operator of Carbon.
  532. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
  533. if (v1->kind() != v2->kind()) {
  534. return false;
  535. }
  536. switch (v1->kind()) {
  537. case Value::Kind::IntValue:
  538. return cast<IntValue>(*v1).value() == cast<IntValue>(*v2).value();
  539. case Value::Kind::BoolValue:
  540. return cast<BoolValue>(*v1).value() == cast<BoolValue>(*v2).value();
  541. case Value::Kind::FunctionValue: {
  542. std::optional<Nonnull<const Statement*>> body1 =
  543. cast<FunctionValue>(*v1).declaration().body();
  544. std::optional<Nonnull<const Statement*>> body2 =
  545. cast<FunctionValue>(*v2).declaration().body();
  546. return body1.has_value() == body2.has_value() &&
  547. (!body1.has_value() || *body1 == *body2);
  548. }
  549. case Value::Kind::BoundMethodValue: {
  550. const auto& m1 = cast<BoundMethodValue>(*v1);
  551. const auto& m2 = cast<BoundMethodValue>(*v2);
  552. std::optional<Nonnull<const Statement*>> body1 = m1.declaration().body();
  553. std::optional<Nonnull<const Statement*>> body2 = m2.declaration().body();
  554. return ValueEqual(m1.receiver(), m2.receiver()) &&
  555. body1.has_value() == body2.has_value() &&
  556. (!body1.has_value() || *body1 == *body2);
  557. }
  558. case Value::Kind::TupleValue: {
  559. const std::vector<Nonnull<const Value*>>& elements1 =
  560. cast<TupleValue>(*v1).elements();
  561. const std::vector<Nonnull<const Value*>>& elements2 =
  562. cast<TupleValue>(*v2).elements();
  563. if (elements1.size() != elements2.size()) {
  564. return false;
  565. }
  566. for (size_t i = 0; i < elements1.size(); ++i) {
  567. if (!ValueEqual(elements1[i], elements2[i])) {
  568. return false;
  569. }
  570. }
  571. return true;
  572. }
  573. case Value::Kind::StructValue: {
  574. const auto& struct_v1 = cast<StructValue>(*v1);
  575. const auto& struct_v2 = cast<StructValue>(*v2);
  576. CHECK(struct_v1.elements().size() == struct_v2.elements().size());
  577. for (size_t i = 0; i < struct_v1.elements().size(); ++i) {
  578. CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name);
  579. if (!ValueEqual(struct_v1.elements()[i].value,
  580. struct_v2.elements()[i].value)) {
  581. return false;
  582. }
  583. }
  584. return true;
  585. }
  586. case Value::Kind::StringValue:
  587. return cast<StringValue>(*v1).value() == cast<StringValue>(*v2).value();
  588. case Value::Kind::IntType:
  589. case Value::Kind::BoolType:
  590. case Value::Kind::TypeType:
  591. case Value::Kind::FunctionType:
  592. case Value::Kind::PointerType:
  593. case Value::Kind::AutoType:
  594. case Value::Kind::StructType:
  595. case Value::Kind::NominalClassType:
  596. case Value::Kind::InterfaceType:
  597. case Value::Kind::Witness:
  598. case Value::Kind::ChoiceType:
  599. case Value::Kind::ContinuationType:
  600. case Value::Kind::VariableType:
  601. case Value::Kind::StringType:
  602. case Value::Kind::TypeOfClassType:
  603. case Value::Kind::TypeOfInterfaceType:
  604. case Value::Kind::TypeOfChoiceType:
  605. case Value::Kind::StaticArrayType:
  606. return TypeEqual(v1, v2);
  607. case Value::Kind::NominalClassValue:
  608. case Value::Kind::AlternativeValue:
  609. case Value::Kind::BindingPlaceholderValue:
  610. case Value::Kind::AlternativeConstructorValue:
  611. case Value::Kind::ContinuationValue:
  612. case Value::Kind::PointerValue:
  613. case Value::Kind::LValue:
  614. // TODO: support pointer comparisons once we have a clearer distinction
  615. // between pointers and lvalues.
  616. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  617. }
  618. }
  619. auto ChoiceType::FindAlternative(std::string_view name) const
  620. -> std::optional<Nonnull<const Value*>> {
  621. for (const NamedValue& alternative : alternatives_) {
  622. if (alternative.name == name) {
  623. return alternative.value;
  624. }
  625. }
  626. return std::nullopt;
  627. }
  628. auto NominalClassType::FindFunction(const std::string& name) const
  629. -> std::optional<Nonnull<const FunctionValue*>> {
  630. for (const auto& member : declaration().members()) {
  631. switch (member->kind()) {
  632. case DeclarationKind::FunctionDeclaration: {
  633. const auto& fun = cast<FunctionDeclaration>(*member);
  634. if (fun.name() == name) {
  635. return &cast<FunctionValue>(**fun.constant_value());
  636. }
  637. break;
  638. }
  639. default:
  640. break;
  641. }
  642. }
  643. return std::nullopt;
  644. }
  645. auto FindMember(const std::string& name,
  646. llvm::ArrayRef<Nonnull<Declaration*>> members)
  647. -> std::optional<Nonnull<const Declaration*>> {
  648. for (Nonnull<const Declaration*> member : members) {
  649. if (std::optional<std::string> mem_name = GetName(*member);
  650. mem_name.has_value()) {
  651. if (*mem_name == name) {
  652. return member;
  653. }
  654. }
  655. }
  656. return std::nullopt;
  657. }
  658. void ImplBinding::Print(llvm::raw_ostream& out) const {
  659. out << "impl binding " << *type_var_ << " as " << *iface_;
  660. }
  661. void ImplBinding::PrintID(llvm::raw_ostream& out) const {
  662. out << *type_var_ << " as " << *iface_;
  663. }
  664. } // namespace Carbon