value.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. break;
  326. }
  327. case Value::Kind::Witness: {
  328. const auto& witness = cast<Witness>(*this);
  329. out << "witness " << *witness.declaration().impl_type() << " as "
  330. << witness.declaration().interface();
  331. break;
  332. }
  333. case Value::Kind::ChoiceType:
  334. out << "choice " << cast<ChoiceType>(*this).name();
  335. break;
  336. case Value::Kind::VariableType:
  337. out << cast<VariableType>(*this).binding();
  338. break;
  339. case Value::Kind::ContinuationValue: {
  340. out << cast<ContinuationValue>(*this).stack();
  341. break;
  342. }
  343. case Value::Kind::StringType:
  344. out << "String";
  345. break;
  346. case Value::Kind::StringValue:
  347. out << "\"";
  348. out.write_escaped(cast<StringValue>(*this).value());
  349. out << "\"";
  350. break;
  351. case Value::Kind::TypeOfClassType:
  352. out << "typeof(" << cast<TypeOfClassType>(*this).class_type() << ")";
  353. break;
  354. case Value::Kind::TypeOfInterfaceType:
  355. out << "typeof("
  356. << cast<TypeOfInterfaceType>(*this)
  357. .interface_type()
  358. .declaration()
  359. .name()
  360. << ")";
  361. break;
  362. case Value::Kind::TypeOfChoiceType:
  363. out << "typeof(" << cast<TypeOfChoiceType>(*this).choice_type().name()
  364. << ")";
  365. break;
  366. case Value::Kind::StaticArrayType: {
  367. const auto& array_type = cast<StaticArrayType>(*this);
  368. out << "[" << array_type.element_type() << "; " << array_type.size()
  369. << "]";
  370. break;
  371. }
  372. }
  373. }
  374. ContinuationValue::StackFragment::~StackFragment() {
  375. CHECK(reversed_todo_.empty())
  376. << "All StackFragments must be empty before the Carbon program ends.";
  377. }
  378. void ContinuationValue::StackFragment::StoreReversed(
  379. std::vector<std::unique_ptr<Action>> reversed_todo) {
  380. CHECK(reversed_todo_.empty());
  381. reversed_todo_ = std::move(reversed_todo);
  382. }
  383. void ContinuationValue::StackFragment::RestoreTo(
  384. Stack<std::unique_ptr<Action>>& todo) {
  385. while (!reversed_todo_.empty()) {
  386. todo.Push(std::move(reversed_todo_.back()));
  387. reversed_todo_.pop_back();
  388. }
  389. }
  390. void ContinuationValue::StackFragment::Clear() {
  391. // We destroy the underlying Actions explicitly to ensure they're
  392. // destroyed in the correct order.
  393. for (auto& action : reversed_todo_) {
  394. action.reset();
  395. }
  396. reversed_todo_.clear();
  397. }
  398. void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
  399. out << "{";
  400. llvm::ListSeparator sep(" :: ");
  401. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  402. out << sep << *action;
  403. }
  404. out << "}";
  405. }
  406. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
  407. if (t1->kind() != t2->kind()) {
  408. return false;
  409. }
  410. switch (t1->kind()) {
  411. case Value::Kind::PointerType:
  412. return TypeEqual(&cast<PointerType>(*t1).type(),
  413. &cast<PointerType>(*t2).type());
  414. case Value::Kind::FunctionType: {
  415. const auto& fn1 = cast<FunctionType>(*t1);
  416. const auto& fn2 = cast<FunctionType>(*t2);
  417. return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
  418. TypeEqual(&fn1.return_type(), &fn2.return_type());
  419. }
  420. case Value::Kind::StructType: {
  421. const auto& struct1 = cast<StructType>(*t1);
  422. const auto& struct2 = cast<StructType>(*t2);
  423. if (struct1.fields().size() != struct2.fields().size()) {
  424. return false;
  425. }
  426. for (size_t i = 0; i < struct1.fields().size(); ++i) {
  427. if (struct1.fields()[i].name != struct2.fields()[i].name ||
  428. !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
  429. return false;
  430. }
  431. }
  432. return true;
  433. }
  434. case Value::Kind::NominalClassType:
  435. if (cast<NominalClassType>(*t1).declaration().name() !=
  436. cast<NominalClassType>(*t2).declaration().name()) {
  437. return false;
  438. }
  439. for (const auto& [ty_var1, ty1] :
  440. cast<NominalClassType>(*t1).type_args()) {
  441. if (!TypeEqual(ty1,
  442. cast<NominalClassType>(*t2).type_args().at(ty_var1))) {
  443. return false;
  444. }
  445. }
  446. return true;
  447. case Value::Kind::InterfaceType:
  448. return cast<InterfaceType>(*t1).declaration().name() ==
  449. cast<InterfaceType>(*t2).declaration().name();
  450. case Value::Kind::ChoiceType:
  451. return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
  452. case Value::Kind::TupleValue: {
  453. const auto& tup1 = cast<TupleValue>(*t1);
  454. const auto& tup2 = cast<TupleValue>(*t2);
  455. if (tup1.elements().size() != tup2.elements().size()) {
  456. return false;
  457. }
  458. for (size_t i = 0; i < tup1.elements().size(); ++i) {
  459. if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
  460. return false;
  461. }
  462. }
  463. return true;
  464. }
  465. case Value::Kind::IntType:
  466. case Value::Kind::BoolType:
  467. case Value::Kind::ContinuationType:
  468. case Value::Kind::TypeType:
  469. case Value::Kind::StringType:
  470. return true;
  471. case Value::Kind::VariableType:
  472. return &cast<VariableType>(*t1).binding() ==
  473. &cast<VariableType>(*t2).binding();
  474. case Value::Kind::TypeOfClassType:
  475. return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
  476. &cast<TypeOfClassType>(*t2).class_type());
  477. case Value::Kind::TypeOfInterfaceType:
  478. return TypeEqual(&cast<TypeOfInterfaceType>(*t1).interface_type(),
  479. &cast<TypeOfInterfaceType>(*t2).interface_type());
  480. case Value::Kind::TypeOfChoiceType:
  481. return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
  482. &cast<TypeOfChoiceType>(*t2).choice_type());
  483. case Value::Kind::StaticArrayType: {
  484. const auto& array1 = cast<StaticArrayType>(*t1);
  485. const auto& array2 = cast<StaticArrayType>(*t2);
  486. return TypeEqual(&array1.element_type(), &array2.element_type()) &&
  487. array1.size() == array2.size();
  488. }
  489. case Value::Kind::IntValue:
  490. case Value::Kind::BoolValue:
  491. case Value::Kind::FunctionValue:
  492. case Value::Kind::BoundMethodValue:
  493. case Value::Kind::StructValue:
  494. case Value::Kind::NominalClassValue:
  495. case Value::Kind::AlternativeValue:
  496. case Value::Kind::AlternativeConstructorValue:
  497. case Value::Kind::StringValue:
  498. case Value::Kind::PointerValue:
  499. case Value::Kind::LValue:
  500. case Value::Kind::BindingPlaceholderValue:
  501. case Value::Kind::ContinuationValue:
  502. FATAL() << "TypeEqual used to compare non-type values\n"
  503. << *t1 << "\n"
  504. << *t2;
  505. case Value::Kind::Witness:
  506. FATAL() << "TypeEqual: unexpected Witness";
  507. break;
  508. case Value::Kind::AutoType:
  509. FATAL() << "TypeEqual: unexpected AutoType";
  510. break;
  511. }
  512. }
  513. // Returns true if the two values are equal and returns false otherwise.
  514. //
  515. // This function implements the `==` operator of Carbon.
  516. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
  517. if (v1->kind() != v2->kind()) {
  518. return false;
  519. }
  520. switch (v1->kind()) {
  521. case Value::Kind::IntValue:
  522. return cast<IntValue>(*v1).value() == cast<IntValue>(*v2).value();
  523. case Value::Kind::BoolValue:
  524. return cast<BoolValue>(*v1).value() == cast<BoolValue>(*v2).value();
  525. case Value::Kind::FunctionValue: {
  526. std::optional<Nonnull<const Statement*>> body1 =
  527. cast<FunctionValue>(*v1).declaration().body();
  528. std::optional<Nonnull<const Statement*>> body2 =
  529. cast<FunctionValue>(*v2).declaration().body();
  530. return body1.has_value() == body2.has_value() &&
  531. (!body1.has_value() || *body1 == *body2);
  532. }
  533. case Value::Kind::BoundMethodValue: {
  534. const auto& m1 = cast<BoundMethodValue>(*v1);
  535. const auto& m2 = cast<BoundMethodValue>(*v2);
  536. std::optional<Nonnull<const Statement*>> body1 = m1.declaration().body();
  537. std::optional<Nonnull<const Statement*>> body2 = m2.declaration().body();
  538. return ValueEqual(m1.receiver(), m2.receiver()) &&
  539. body1.has_value() == body2.has_value() &&
  540. (!body1.has_value() || *body1 == *body2);
  541. }
  542. case Value::Kind::TupleValue: {
  543. const std::vector<Nonnull<const Value*>>& elements1 =
  544. cast<TupleValue>(*v1).elements();
  545. const std::vector<Nonnull<const Value*>>& elements2 =
  546. cast<TupleValue>(*v2).elements();
  547. if (elements1.size() != elements2.size()) {
  548. return false;
  549. }
  550. for (size_t i = 0; i < elements1.size(); ++i) {
  551. if (!ValueEqual(elements1[i], elements2[i])) {
  552. return false;
  553. }
  554. }
  555. return true;
  556. }
  557. case Value::Kind::StructValue: {
  558. const auto& struct_v1 = cast<StructValue>(*v1);
  559. const auto& struct_v2 = cast<StructValue>(*v2);
  560. CHECK(struct_v1.elements().size() == struct_v2.elements().size());
  561. for (size_t i = 0; i < struct_v1.elements().size(); ++i) {
  562. CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name);
  563. if (!ValueEqual(struct_v1.elements()[i].value,
  564. struct_v2.elements()[i].value)) {
  565. return false;
  566. }
  567. }
  568. return true;
  569. }
  570. case Value::Kind::StringValue:
  571. return cast<StringValue>(*v1).value() == cast<StringValue>(*v2).value();
  572. case Value::Kind::IntType:
  573. case Value::Kind::BoolType:
  574. case Value::Kind::TypeType:
  575. case Value::Kind::FunctionType:
  576. case Value::Kind::PointerType:
  577. case Value::Kind::AutoType:
  578. case Value::Kind::StructType:
  579. case Value::Kind::NominalClassType:
  580. case Value::Kind::InterfaceType:
  581. case Value::Kind::Witness:
  582. case Value::Kind::ChoiceType:
  583. case Value::Kind::ContinuationType:
  584. case Value::Kind::VariableType:
  585. case Value::Kind::StringType:
  586. case Value::Kind::TypeOfClassType:
  587. case Value::Kind::TypeOfInterfaceType:
  588. case Value::Kind::TypeOfChoiceType:
  589. case Value::Kind::StaticArrayType:
  590. return TypeEqual(v1, v2);
  591. case Value::Kind::NominalClassValue:
  592. case Value::Kind::AlternativeValue:
  593. case Value::Kind::BindingPlaceholderValue:
  594. case Value::Kind::AlternativeConstructorValue:
  595. case Value::Kind::ContinuationValue:
  596. case Value::Kind::PointerValue:
  597. case Value::Kind::LValue:
  598. // TODO: support pointer comparisons once we have a clearer distinction
  599. // between pointers and lvalues.
  600. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  601. }
  602. }
  603. auto ChoiceType::FindAlternative(std::string_view name) const
  604. -> std::optional<Nonnull<const Value*>> {
  605. for (const NamedValue& alternative : alternatives_) {
  606. if (alternative.name == name) {
  607. return alternative.value;
  608. }
  609. }
  610. return std::nullopt;
  611. }
  612. auto NominalClassType::FindFunction(const std::string& name) const
  613. -> std::optional<Nonnull<const FunctionValue*>> {
  614. for (const auto& member : declaration().members()) {
  615. switch (member->kind()) {
  616. case DeclarationKind::FunctionDeclaration: {
  617. const auto& fun = cast<FunctionDeclaration>(*member);
  618. if (fun.name() == name) {
  619. return &cast<FunctionValue>(**fun.constant_value());
  620. }
  621. break;
  622. }
  623. default:
  624. break;
  625. }
  626. }
  627. return std::nullopt;
  628. }
  629. auto FindMember(const std::string& name,
  630. llvm::ArrayRef<Nonnull<Declaration*>> members)
  631. -> std::optional<Nonnull<const Declaration*>> {
  632. for (Nonnull<const Declaration*> member : members) {
  633. if (std::optional<std::string> mem_name = GetName(*member);
  634. mem_name.has_value()) {
  635. if (*mem_name == name) {
  636. return member;
  637. }
  638. }
  639. }
  640. return std::nullopt;
  641. }
  642. void ImplBinding::Print(llvm::raw_ostream& out) const {
  643. out << "impl binding " << *type_var_ << " as " << *iface_;
  644. }
  645. void ImplBinding::PrintID(llvm::raw_ostream& out) const {
  646. out << *type_var_ << " as " << *iface_;
  647. }
  648. } // namespace Carbon