value.cpp 25 KB

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