value.cpp 22 KB

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