value.cpp 23 KB

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