value.cpp 27 KB

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