value.cpp 22 KB

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