value.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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/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 FATAL_COMPILATION_ERROR(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 FATAL_RUNTIME_ERROR(source_loc)
  58. << "member " << f << " not in " << *v;
  59. }
  60. return *field;
  61. }
  62. case Value::Kind::NominalClassValue: {
  63. const auto& object = cast<NominalClassValue>(*v);
  64. // Look for a field
  65. std::optional<Nonnull<const Value*>> field =
  66. cast<StructValue>(object.inits()).FindField(f);
  67. if (field.has_value()) {
  68. return *field;
  69. } else {
  70. // Look for a method in the object's class
  71. const auto& class_type = cast<NominalClassType>(object.type());
  72. std::optional<Nonnull<const FunctionValue*>> func =
  73. class_type.FindFunction(f);
  74. if (func == std::nullopt) {
  75. return FATAL_RUNTIME_ERROR(source_loc)
  76. << "member " << f << " not in " << *v << " or its "
  77. << class_type;
  78. } else if ((*func)->declaration().is_method()) {
  79. // Found a method. Turn it into a bound method.
  80. const FunctionValue& m = cast<FunctionValue>(**func);
  81. return arena->New<BoundMethodValue>(&m.declaration(), &object,
  82. class_type.type_args(),
  83. class_type.witnesses());
  84. } else {
  85. // Found a class function
  86. Nonnull<const FunctionValue*> fun = arena->New<FunctionValue>(
  87. &(*func)->declaration(), class_type.type_args(),
  88. class_type.witnesses());
  89. return fun;
  90. }
  91. }
  92. }
  93. case Value::Kind::ChoiceType: {
  94. const auto& choice = cast<ChoiceType>(*v);
  95. if (!choice.FindAlternative(f)) {
  96. return FATAL_RUNTIME_ERROR(source_loc)
  97. << "alternative " << f << " not in " << *v;
  98. }
  99. return arena->New<AlternativeConstructorValue>(f, choice.name());
  100. }
  101. case Value::Kind::NominalClassType: {
  102. // Access a class function.
  103. const NominalClassType& class_type = cast<NominalClassType>(*v);
  104. std::optional<Nonnull<const FunctionValue*>> fun =
  105. class_type.FindFunction(f);
  106. if (fun == std::nullopt) {
  107. return FATAL_RUNTIME_ERROR(source_loc)
  108. << "class function " << f << " not in " << *v;
  109. }
  110. return arena->New<FunctionValue>(&(*fun)->declaration(),
  111. class_type.type_args(),
  112. class_type.witnesses());
  113. }
  114. default:
  115. FATAL() << "field access not allowed for value " << *v;
  116. }
  117. }
  118. auto Value::GetField(Nonnull<Arena*> arena, const FieldPath& path,
  119. SourceLocation source_loc) const
  120. -> ErrorOr<Nonnull<const Value*>> {
  121. Nonnull<const Value*> value(this);
  122. for (const FieldPath::Component& field : path.components_) {
  123. ASSIGN_OR_RETURN(value, GetMember(arena, value, field, source_loc));
  124. }
  125. return value;
  126. }
  127. static auto SetFieldImpl(
  128. Nonnull<Arena*> arena, Nonnull<const Value*> value,
  129. std::vector<FieldPath::Component>::const_iterator path_begin,
  130. std::vector<FieldPath::Component>::const_iterator path_end,
  131. Nonnull<const Value*> field_value, SourceLocation source_loc)
  132. -> ErrorOr<Nonnull<const Value*>> {
  133. if (path_begin == path_end) {
  134. return field_value;
  135. }
  136. switch (value->kind()) {
  137. case Value::Kind::StructValue: {
  138. std::vector<NamedValue> elements = cast<StructValue>(*value).elements();
  139. auto it = std::find_if(elements.begin(), elements.end(),
  140. [path_begin](const NamedValue& element) {
  141. return element.name == (*path_begin).name();
  142. });
  143. if (it == elements.end()) {
  144. return FATAL_RUNTIME_ERROR(source_loc)
  145. << "field " << (*path_begin).name() << " not in " << *value;
  146. }
  147. ASSIGN_OR_RETURN(it->value,
  148. SetFieldImpl(arena, it->value, path_begin + 1, path_end,
  149. field_value, source_loc));
  150. return arena->New<StructValue>(elements);
  151. }
  152. case Value::Kind::NominalClassValue: {
  153. return SetFieldImpl(arena, &cast<NominalClassValue>(*value).inits(),
  154. path_begin, path_end, field_value, source_loc);
  155. }
  156. case Value::Kind::TupleValue: {
  157. std::vector<Nonnull<const Value*>> elements =
  158. cast<TupleValue>(*value).elements();
  159. // TODO(geoffromer): update FieldPath to hold integers as well as strings.
  160. int index = std::stoi((*path_begin).name());
  161. if (index < 0 || static_cast<size_t>(index) >= elements.size()) {
  162. return FATAL_RUNTIME_ERROR(source_loc)
  163. << "index " << (*path_begin).name() << " out of range in "
  164. << *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("
  353. << cast<TypeOfClassType>(*this).class_type().declaration().name()
  354. << ")";
  355. break;
  356. case Value::Kind::TypeOfInterfaceType:
  357. out << "typeof("
  358. << cast<TypeOfInterfaceType>(*this)
  359. .interface_type()
  360. .declaration()
  361. .name()
  362. << ")";
  363. break;
  364. case Value::Kind::TypeOfChoiceType:
  365. out << "typeof(" << cast<TypeOfChoiceType>(*this).choice_type().name()
  366. << ")";
  367. break;
  368. case Value::Kind::StaticArrayType: {
  369. const auto& array_type = cast<StaticArrayType>(*this);
  370. out << "[" << array_type.element_type() << "; " << array_type.size()
  371. << "]";
  372. break;
  373. }
  374. }
  375. }
  376. ContinuationValue::StackFragment::~StackFragment() {
  377. CHECK(reversed_todo_.empty())
  378. << "All StackFragments must be empty before the Carbon program ends.";
  379. }
  380. void ContinuationValue::StackFragment::StoreReversed(
  381. std::vector<std::unique_ptr<Action>> reversed_todo) {
  382. CHECK(reversed_todo_.empty());
  383. reversed_todo_ = std::move(reversed_todo);
  384. }
  385. void ContinuationValue::StackFragment::RestoreTo(
  386. Stack<std::unique_ptr<Action>>& todo) {
  387. while (!reversed_todo_.empty()) {
  388. todo.Push(std::move(reversed_todo_.back()));
  389. reversed_todo_.pop_back();
  390. }
  391. }
  392. void ContinuationValue::StackFragment::Clear() {
  393. // We destroy the underlying Actions explicitly to ensure they're
  394. // destroyed in the correct order.
  395. for (auto& action : reversed_todo_) {
  396. action.reset();
  397. }
  398. reversed_todo_.clear();
  399. }
  400. void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
  401. out << "{";
  402. llvm::ListSeparator sep(" :: ");
  403. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  404. out << sep << *action;
  405. }
  406. out << "}";
  407. }
  408. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
  409. if (t1->kind() != t2->kind()) {
  410. return false;
  411. }
  412. switch (t1->kind()) {
  413. case Value::Kind::PointerType:
  414. return TypeEqual(&cast<PointerType>(*t1).type(),
  415. &cast<PointerType>(*t2).type());
  416. case Value::Kind::FunctionType: {
  417. const auto& fn1 = cast<FunctionType>(*t1);
  418. const auto& fn2 = cast<FunctionType>(*t2);
  419. return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
  420. TypeEqual(&fn1.return_type(), &fn2.return_type());
  421. }
  422. case Value::Kind::StructType: {
  423. const auto& struct1 = cast<StructType>(*t1);
  424. const auto& struct2 = cast<StructType>(*t2);
  425. if (struct1.fields().size() != struct2.fields().size()) {
  426. return false;
  427. }
  428. for (size_t i = 0; i < struct1.fields().size(); ++i) {
  429. if (struct1.fields()[i].name != struct2.fields()[i].name ||
  430. !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
  431. return false;
  432. }
  433. }
  434. return true;
  435. }
  436. case Value::Kind::NominalClassType:
  437. if (cast<NominalClassType>(*t1).declaration().name() !=
  438. cast<NominalClassType>(*t2).declaration().name()) {
  439. return false;
  440. }
  441. for (const auto& [ty_var1, ty1] :
  442. cast<NominalClassType>(*t1).type_args()) {
  443. if (!TypeEqual(ty1,
  444. cast<NominalClassType>(*t2).type_args().at(ty_var1))) {
  445. return false;
  446. }
  447. }
  448. return true;
  449. case Value::Kind::InterfaceType:
  450. return cast<InterfaceType>(*t1).declaration().name() ==
  451. cast<InterfaceType>(*t2).declaration().name();
  452. case Value::Kind::ChoiceType:
  453. return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
  454. case Value::Kind::TupleValue: {
  455. const auto& tup1 = cast<TupleValue>(*t1);
  456. const auto& tup2 = cast<TupleValue>(*t2);
  457. if (tup1.elements().size() != tup2.elements().size()) {
  458. return false;
  459. }
  460. for (size_t i = 0; i < tup1.elements().size(); ++i) {
  461. if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
  462. return false;
  463. }
  464. }
  465. return true;
  466. }
  467. case Value::Kind::IntType:
  468. case Value::Kind::BoolType:
  469. case Value::Kind::ContinuationType:
  470. case Value::Kind::TypeType:
  471. case Value::Kind::StringType:
  472. return true;
  473. case Value::Kind::VariableType:
  474. return &cast<VariableType>(*t1).binding() ==
  475. &cast<VariableType>(*t2).binding();
  476. case Value::Kind::TypeOfClassType:
  477. return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
  478. &cast<TypeOfClassType>(*t2).class_type());
  479. case Value::Kind::TypeOfInterfaceType:
  480. return TypeEqual(&cast<TypeOfInterfaceType>(*t1).interface_type(),
  481. &cast<TypeOfInterfaceType>(*t2).interface_type());
  482. case Value::Kind::TypeOfChoiceType:
  483. return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
  484. &cast<TypeOfChoiceType>(*t2).choice_type());
  485. case Value::Kind::StaticArrayType: {
  486. const auto& array1 = cast<StaticArrayType>(*t1);
  487. const auto& array2 = cast<StaticArrayType>(*t2);
  488. return TypeEqual(&array1.element_type(), &array2.element_type()) &&
  489. array1.size() == array2.size();
  490. }
  491. case Value::Kind::IntValue:
  492. case Value::Kind::BoolValue:
  493. case Value::Kind::FunctionValue:
  494. case Value::Kind::BoundMethodValue:
  495. case Value::Kind::StructValue:
  496. case Value::Kind::NominalClassValue:
  497. case Value::Kind::AlternativeValue:
  498. case Value::Kind::AlternativeConstructorValue:
  499. case Value::Kind::StringValue:
  500. case Value::Kind::PointerValue:
  501. case Value::Kind::LValue:
  502. case Value::Kind::BindingPlaceholderValue:
  503. case Value::Kind::ContinuationValue:
  504. FATAL() << "TypeEqual used to compare non-type values\n"
  505. << *t1 << "\n"
  506. << *t2;
  507. case Value::Kind::Witness:
  508. FATAL() << "TypeEqual: unexpected Witness";
  509. break;
  510. case Value::Kind::AutoType:
  511. FATAL() << "TypeEqual: unexpected AutoType";
  512. break;
  513. }
  514. }
  515. // Returns true if the two values are equal and returns false otherwise.
  516. //
  517. // This function implements the `==` operator of Carbon.
  518. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
  519. if (v1->kind() != v2->kind()) {
  520. return false;
  521. }
  522. switch (v1->kind()) {
  523. case Value::Kind::IntValue:
  524. return cast<IntValue>(*v1).value() == cast<IntValue>(*v2).value();
  525. case Value::Kind::BoolValue:
  526. return cast<BoolValue>(*v1).value() == cast<BoolValue>(*v2).value();
  527. case Value::Kind::FunctionValue: {
  528. std::optional<Nonnull<const Statement*>> body1 =
  529. cast<FunctionValue>(*v1).declaration().body();
  530. std::optional<Nonnull<const Statement*>> body2 =
  531. cast<FunctionValue>(*v2).declaration().body();
  532. return body1.has_value() == body2.has_value() &&
  533. (!body1.has_value() || *body1 == *body2);
  534. }
  535. case Value::Kind::BoundMethodValue: {
  536. const auto& m1 = cast<BoundMethodValue>(*v1);
  537. const auto& m2 = cast<BoundMethodValue>(*v2);
  538. std::optional<Nonnull<const Statement*>> body1 = m1.declaration().body();
  539. std::optional<Nonnull<const Statement*>> body2 = m2.declaration().body();
  540. return ValueEqual(m1.receiver(), m2.receiver()) &&
  541. body1.has_value() == body2.has_value() &&
  542. (!body1.has_value() || *body1 == *body2);
  543. }
  544. case Value::Kind::TupleValue: {
  545. const std::vector<Nonnull<const Value*>>& elements1 =
  546. cast<TupleValue>(*v1).elements();
  547. const std::vector<Nonnull<const Value*>>& elements2 =
  548. cast<TupleValue>(*v2).elements();
  549. if (elements1.size() != elements2.size()) {
  550. return false;
  551. }
  552. for (size_t i = 0; i < elements1.size(); ++i) {
  553. if (!ValueEqual(elements1[i], elements2[i])) {
  554. return false;
  555. }
  556. }
  557. return true;
  558. }
  559. case Value::Kind::StructValue: {
  560. const auto& struct_v1 = cast<StructValue>(*v1);
  561. const auto& struct_v2 = cast<StructValue>(*v2);
  562. CHECK(struct_v1.elements().size() == struct_v2.elements().size());
  563. for (size_t i = 0; i < struct_v1.elements().size(); ++i) {
  564. CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name);
  565. if (!ValueEqual(struct_v1.elements()[i].value,
  566. struct_v2.elements()[i].value)) {
  567. return false;
  568. }
  569. }
  570. return true;
  571. }
  572. case Value::Kind::StringValue:
  573. return cast<StringValue>(*v1).value() == cast<StringValue>(*v2).value();
  574. case Value::Kind::IntType:
  575. case Value::Kind::BoolType:
  576. case Value::Kind::TypeType:
  577. case Value::Kind::FunctionType:
  578. case Value::Kind::PointerType:
  579. case Value::Kind::AutoType:
  580. case Value::Kind::StructType:
  581. case Value::Kind::NominalClassType:
  582. case Value::Kind::InterfaceType:
  583. case Value::Kind::Witness:
  584. case Value::Kind::ChoiceType:
  585. case Value::Kind::ContinuationType:
  586. case Value::Kind::VariableType:
  587. case Value::Kind::StringType:
  588. case Value::Kind::TypeOfClassType:
  589. case Value::Kind::TypeOfInterfaceType:
  590. case Value::Kind::TypeOfChoiceType:
  591. case Value::Kind::StaticArrayType:
  592. return TypeEqual(v1, v2);
  593. case Value::Kind::NominalClassValue:
  594. case Value::Kind::AlternativeValue:
  595. case Value::Kind::BindingPlaceholderValue:
  596. case Value::Kind::AlternativeConstructorValue:
  597. case Value::Kind::ContinuationValue:
  598. case Value::Kind::PointerValue:
  599. case Value::Kind::LValue:
  600. // TODO: support pointer comparisons once we have a clearer distinction
  601. // between pointers and lvalues.
  602. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  603. }
  604. }
  605. auto ChoiceType::FindAlternative(std::string_view name) const
  606. -> std::optional<Nonnull<const Value*>> {
  607. for (const NamedValue& alternative : alternatives_) {
  608. if (alternative.name == name) {
  609. return alternative.value;
  610. }
  611. }
  612. return std::nullopt;
  613. }
  614. auto NominalClassType::FindFunction(const std::string& name) const
  615. -> std::optional<Nonnull<const FunctionValue*>> {
  616. for (const auto& member : declaration().members()) {
  617. switch (member->kind()) {
  618. case DeclarationKind::FunctionDeclaration: {
  619. const auto& fun = cast<FunctionDeclaration>(*member);
  620. if (fun.name() == name) {
  621. return &cast<FunctionValue>(**fun.constant_value());
  622. }
  623. break;
  624. }
  625. default:
  626. break;
  627. }
  628. }
  629. return std::nullopt;
  630. }
  631. auto FindMember(const std::string& name,
  632. llvm::ArrayRef<Nonnull<Declaration*>> members)
  633. -> std::optional<Nonnull<const Declaration*>> {
  634. for (Nonnull<const Declaration*> member : members) {
  635. if (std::optional<std::string> mem_name = GetName(*member);
  636. mem_name.has_value()) {
  637. if (*mem_name == name) {
  638. return member;
  639. }
  640. }
  641. }
  642. return std::nullopt;
  643. }
  644. void ImplBinding::Print(llvm::raw_ostream& out) const {
  645. out << "impl binding " << *type_var_ << " as " << *iface_;
  646. }
  647. void ImplBinding::PrintID(llvm::raw_ostream& out) const {
  648. out << *type_var_ << " as " << *iface_;
  649. }
  650. } // namespace Carbon