value.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. namespace {
  24. auto GetMember(Nonnull<Arena*> arena, Nonnull<const Value*> v,
  25. const std::string& f, SourceLocation source_loc)
  26. -> Nonnull<const Value*> {
  27. switch (v->kind()) {
  28. case Value::Kind::StructValue: {
  29. std::optional<Nonnull<const Value*>> field =
  30. cast<StructValue>(*v).FindField(f);
  31. if (field == std::nullopt) {
  32. FATAL_RUNTIME_ERROR(source_loc) << "member " << f << " not in " << *v;
  33. }
  34. return *field;
  35. }
  36. case Value::Kind::NominalClassValue: {
  37. std::optional<Nonnull<const Value*>> field =
  38. cast<StructValue>(cast<NominalClassValue>(*v).inits()).FindField(f);
  39. if (field == std::nullopt) {
  40. FATAL_RUNTIME_ERROR(source_loc) << "member " << f << " not in " << *v;
  41. }
  42. return *field;
  43. }
  44. case Value::Kind::ChoiceType: {
  45. const auto& choice = cast<ChoiceType>(*v);
  46. if (!choice.FindAlternative(f)) {
  47. FATAL_RUNTIME_ERROR(source_loc)
  48. << "alternative " << f << " not in " << *v;
  49. }
  50. return arena->New<AlternativeConstructorValue>(f, choice.name());
  51. }
  52. default:
  53. FATAL() << "field access not allowed for value " << *v;
  54. }
  55. }
  56. } // namespace
  57. auto Value::GetField(Nonnull<Arena*> arena, const FieldPath& path,
  58. SourceLocation source_loc) const -> Nonnull<const Value*> {
  59. Nonnull<const Value*> value(this);
  60. for (const std::string& field : path.components_) {
  61. value = GetMember(arena, value, field, source_loc);
  62. }
  63. return value;
  64. }
  65. namespace {
  66. auto SetFieldImpl(Nonnull<Arena*> arena, Nonnull<const Value*> value,
  67. std::vector<std::string>::const_iterator path_begin,
  68. std::vector<std::string>::const_iterator path_end,
  69. Nonnull<const Value*> field_value, SourceLocation source_loc)
  70. -> Nonnull<const Value*> {
  71. if (path_begin == path_end) {
  72. return field_value;
  73. }
  74. switch (value->kind()) {
  75. case Value::Kind::StructValue: {
  76. std::vector<NamedValue> elements = cast<StructValue>(*value).elements();
  77. auto it = std::find_if(elements.begin(), elements.end(),
  78. [path_begin](const NamedValue& element) {
  79. return element.name == *path_begin;
  80. });
  81. if (it == elements.end()) {
  82. FATAL_RUNTIME_ERROR(source_loc)
  83. << "field " << *path_begin << " not in " << *value;
  84. }
  85. it->value = SetFieldImpl(arena, it->value, path_begin + 1, path_end,
  86. field_value, source_loc);
  87. return arena->New<StructValue>(elements);
  88. }
  89. case Value::Kind::NominalClassValue: {
  90. return SetFieldImpl(arena, &cast<NominalClassValue>(*value).inits(),
  91. path_begin, path_end, field_value, source_loc);
  92. }
  93. case Value::Kind::TupleValue: {
  94. std::vector<Nonnull<const Value*>> elements =
  95. cast<TupleValue>(*value).elements();
  96. // TODO(geoffromer): update FieldPath to hold integers as well as strings.
  97. int index = std::stoi(*path_begin);
  98. if (index < 0 || static_cast<size_t>(index) >= elements.size()) {
  99. FATAL_RUNTIME_ERROR(source_loc)
  100. << "index " << *path_begin << " out of range in " << *value;
  101. }
  102. elements[index] = SetFieldImpl(arena, elements[index], path_begin + 1,
  103. path_end, field_value, source_loc);
  104. return arena->New<TupleValue>(elements);
  105. }
  106. default:
  107. FATAL() << "field access not allowed for value " << *value;
  108. }
  109. }
  110. } // namespace
  111. auto Value::SetField(Nonnull<Arena*> arena, const FieldPath& path,
  112. Nonnull<const Value*> field_value,
  113. SourceLocation source_loc) const -> Nonnull<const Value*> {
  114. return SetFieldImpl(arena, Nonnull<const Value*>(this),
  115. path.components_.begin(), path.components_.end(),
  116. field_value, source_loc);
  117. }
  118. void Value::Print(llvm::raw_ostream& out) const {
  119. switch (kind()) {
  120. case Value::Kind::AlternativeConstructorValue: {
  121. const auto& alt = cast<AlternativeConstructorValue>(*this);
  122. out << alt.choice_name() << "." << alt.alt_name();
  123. break;
  124. }
  125. case Value::Kind::BindingPlaceholderValue: {
  126. const auto& placeholder = cast<BindingPlaceholderValue>(*this);
  127. out << "Placeholder<";
  128. if (placeholder.named_entity().has_value()) {
  129. out << (*placeholder.named_entity()).name();
  130. } else {
  131. out << "_";
  132. }
  133. out << ">";
  134. break;
  135. }
  136. case Value::Kind::AlternativeValue: {
  137. const auto& alt = cast<AlternativeValue>(*this);
  138. out << "alt " << alt.choice_name() << "." << alt.alt_name() << " "
  139. << alt.argument();
  140. break;
  141. }
  142. case Value::Kind::StructValue: {
  143. const auto& struct_val = cast<StructValue>(*this);
  144. out << "{";
  145. llvm::ListSeparator sep;
  146. for (const NamedValue& element : struct_val.elements()) {
  147. out << sep << "." << element.name << " = " << *element.value;
  148. }
  149. out << "}";
  150. break;
  151. }
  152. case Value::Kind::NominalClassValue: {
  153. const auto& s = cast<NominalClassValue>(*this);
  154. out << cast<NominalClassType>(s.type()).name() << s.inits();
  155. break;
  156. }
  157. case Value::Kind::TupleValue: {
  158. out << "(";
  159. llvm::ListSeparator sep;
  160. for (Nonnull<const Value*> element : cast<TupleValue>(*this).elements()) {
  161. out << sep << *element;
  162. }
  163. out << ")";
  164. break;
  165. }
  166. case Value::Kind::IntValue:
  167. out << cast<IntValue>(*this).value();
  168. break;
  169. case Value::Kind::BoolValue:
  170. out << (cast<BoolValue>(*this).value() ? "true" : "false");
  171. break;
  172. case Value::Kind::FunctionValue:
  173. out << "fun<" << cast<FunctionValue>(*this).declaration().name() << ">";
  174. break;
  175. case Value::Kind::LValue:
  176. out << "ptr<" << cast<LValue>(*this).address() << ">";
  177. break;
  178. case Value::Kind::BoolType:
  179. out << "Bool";
  180. break;
  181. case Value::Kind::IntType:
  182. out << "i32";
  183. break;
  184. case Value::Kind::TypeType:
  185. out << "Type";
  186. break;
  187. case Value::Kind::AutoType:
  188. out << "auto";
  189. break;
  190. case Value::Kind::ContinuationType:
  191. out << "Continuation";
  192. break;
  193. case Value::Kind::PointerType:
  194. out << cast<PointerType>(*this).type() << "*";
  195. break;
  196. case Value::Kind::FunctionType: {
  197. const auto& fn_type = cast<FunctionType>(*this);
  198. out << "fn ";
  199. if (fn_type.deduced().size() > 0) {
  200. out << "[";
  201. unsigned int i = 0;
  202. for (Nonnull<const GenericBinding*> deduced : fn_type.deduced()) {
  203. if (i != 0) {
  204. out << ", ";
  205. }
  206. out << deduced->name() << ":! " << deduced->type();
  207. ++i;
  208. }
  209. out << "]";
  210. }
  211. out << fn_type.parameters() << " -> " << fn_type.return_type();
  212. break;
  213. }
  214. case Value::Kind::StructType: {
  215. out << "{";
  216. llvm::ListSeparator sep;
  217. for (const auto& [name, type] : cast<StructType>(*this).fields()) {
  218. out << sep << "." << name << ": " << *type;
  219. }
  220. out << "}";
  221. break;
  222. }
  223. case Value::Kind::NominalClassType:
  224. out << "class " << cast<NominalClassType>(*this).name();
  225. break;
  226. case Value::Kind::ChoiceType:
  227. out << "choice " << cast<ChoiceType>(*this).name();
  228. break;
  229. case Value::Kind::VariableType:
  230. out << cast<VariableType>(*this).binding().name();
  231. break;
  232. case Value::Kind::ContinuationValue: {
  233. out << cast<ContinuationValue>(*this).stack();
  234. break;
  235. }
  236. case Value::Kind::StringType:
  237. out << "String";
  238. break;
  239. case Value::Kind::StringValue:
  240. out << "\"";
  241. out.write_escaped(cast<StringValue>(*this).value());
  242. out << "\"";
  243. break;
  244. case Value::Kind::TypeOfClassType:
  245. out << "typeof(" << cast<TypeOfClassType>(*this).class_type().name()
  246. << ")";
  247. break;
  248. case Value::Kind::TypeOfChoiceType:
  249. out << "typeof(" << cast<TypeOfChoiceType>(*this).choice_type().name()
  250. << ")";
  251. break;
  252. }
  253. }
  254. ContinuationValue::StackFragment::~StackFragment() {
  255. CHECK(reversed_todo_.empty())
  256. << "All StackFragments must be empty before the Carbon program ends.";
  257. }
  258. void ContinuationValue::StackFragment::StoreReversed(
  259. std::vector<std::unique_ptr<Action>> reversed_todo) {
  260. CHECK(reversed_todo_.empty());
  261. reversed_todo_ = std::move(reversed_todo);
  262. }
  263. void ContinuationValue::StackFragment::RestoreTo(
  264. Stack<std::unique_ptr<Action>>& todo) {
  265. while (!reversed_todo_.empty()) {
  266. todo.Push(std::move(reversed_todo_.back()));
  267. reversed_todo_.pop_back();
  268. }
  269. }
  270. void ContinuationValue::StackFragment::Clear() {
  271. // We destroy the underlying Actions explicitly to ensure they're
  272. // destroyed in the correct order.
  273. for (auto& action : reversed_todo_) {
  274. action.reset();
  275. }
  276. reversed_todo_.clear();
  277. }
  278. void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
  279. out << "{";
  280. llvm::ListSeparator sep(" :: ");
  281. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  282. out << sep << *action;
  283. }
  284. out << "}";
  285. }
  286. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
  287. if (t1->kind() != t2->kind()) {
  288. return false;
  289. }
  290. switch (t1->kind()) {
  291. case Value::Kind::PointerType:
  292. return TypeEqual(&cast<PointerType>(*t1).type(),
  293. &cast<PointerType>(*t2).type());
  294. case Value::Kind::FunctionType: {
  295. const auto& fn1 = cast<FunctionType>(*t1);
  296. const auto& fn2 = cast<FunctionType>(*t2);
  297. return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
  298. TypeEqual(&fn1.return_type(), &fn2.return_type());
  299. }
  300. case Value::Kind::StructType: {
  301. const auto& struct1 = cast<StructType>(*t1);
  302. const auto& struct2 = cast<StructType>(*t2);
  303. if (struct1.fields().size() != struct2.fields().size()) {
  304. return false;
  305. }
  306. for (size_t i = 0; i < struct1.fields().size(); ++i) {
  307. if (struct1.fields()[i].name != struct2.fields()[i].name ||
  308. !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
  309. return false;
  310. }
  311. }
  312. return true;
  313. }
  314. case Value::Kind::NominalClassType:
  315. return cast<NominalClassType>(*t1).name() ==
  316. cast<NominalClassType>(*t2).name();
  317. case Value::Kind::ChoiceType:
  318. return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
  319. case Value::Kind::TupleValue: {
  320. const auto& tup1 = cast<TupleValue>(*t1);
  321. const auto& tup2 = cast<TupleValue>(*t2);
  322. if (tup1.elements().size() != tup2.elements().size()) {
  323. return false;
  324. }
  325. for (size_t i = 0; i < tup1.elements().size(); ++i) {
  326. if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
  327. return false;
  328. }
  329. }
  330. return true;
  331. }
  332. case Value::Kind::IntType:
  333. case Value::Kind::BoolType:
  334. case Value::Kind::ContinuationType:
  335. case Value::Kind::TypeType:
  336. case Value::Kind::StringType:
  337. return true;
  338. case Value::Kind::VariableType:
  339. return &cast<VariableType>(*t1).binding() ==
  340. &cast<VariableType>(*t2).binding();
  341. case Value::Kind::TypeOfClassType:
  342. return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
  343. &cast<TypeOfClassType>(*t2).class_type());
  344. case Value::Kind::TypeOfChoiceType:
  345. return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
  346. &cast<TypeOfChoiceType>(*t2).choice_type());
  347. default:
  348. FATAL() << "TypeEqual used to compare non-type values\n"
  349. << *t1 << "\n"
  350. << *t2;
  351. }
  352. }
  353. // Returns true if the two values are equal and returns false otherwise.
  354. //
  355. // This function implements the `==` operator of Carbon.
  356. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
  357. if (v1->kind() != v2->kind()) {
  358. return false;
  359. }
  360. switch (v1->kind()) {
  361. case Value::Kind::IntValue:
  362. return cast<IntValue>(*v1).value() == cast<IntValue>(*v2).value();
  363. case Value::Kind::BoolValue:
  364. return cast<BoolValue>(*v1).value() == cast<BoolValue>(*v2).value();
  365. case Value::Kind::FunctionValue: {
  366. std::optional<Nonnull<const Statement*>> body1 =
  367. cast<FunctionValue>(*v1).declaration().body();
  368. std::optional<Nonnull<const Statement*>> body2 =
  369. cast<FunctionValue>(*v2).declaration().body();
  370. return body1.has_value() == body2.has_value() &&
  371. (!body1.has_value() || *body1 == *body2);
  372. }
  373. case Value::Kind::TupleValue: {
  374. const std::vector<Nonnull<const Value*>>& elements1 =
  375. cast<TupleValue>(*v1).elements();
  376. const std::vector<Nonnull<const Value*>>& elements2 =
  377. cast<TupleValue>(*v2).elements();
  378. if (elements1.size() != elements2.size()) {
  379. return false;
  380. }
  381. for (size_t i = 0; i < elements1.size(); ++i) {
  382. if (!ValueEqual(elements1[i], elements2[i])) {
  383. return false;
  384. }
  385. }
  386. return true;
  387. }
  388. case Value::Kind::StructValue: {
  389. const auto& struct_v1 = cast<StructValue>(*v1);
  390. const auto& struct_v2 = cast<StructValue>(*v2);
  391. CHECK(struct_v1.elements().size() == struct_v2.elements().size());
  392. for (size_t i = 0; i < struct_v1.elements().size(); ++i) {
  393. CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name);
  394. if (!ValueEqual(struct_v1.elements()[i].value,
  395. struct_v2.elements()[i].value)) {
  396. return false;
  397. }
  398. }
  399. return true;
  400. }
  401. case Value::Kind::StringValue:
  402. return cast<StringValue>(*v1).value() == cast<StringValue>(*v2).value();
  403. case Value::Kind::IntType:
  404. case Value::Kind::BoolType:
  405. case Value::Kind::TypeType:
  406. case Value::Kind::FunctionType:
  407. case Value::Kind::PointerType:
  408. case Value::Kind::AutoType:
  409. case Value::Kind::StructType:
  410. case Value::Kind::NominalClassType:
  411. case Value::Kind::ChoiceType:
  412. case Value::Kind::ContinuationType:
  413. case Value::Kind::VariableType:
  414. case Value::Kind::StringType:
  415. case Value::Kind::TypeOfClassType:
  416. case Value::Kind::TypeOfChoiceType:
  417. return TypeEqual(v1, v2);
  418. case Value::Kind::NominalClassValue:
  419. case Value::Kind::AlternativeValue:
  420. case Value::Kind::BindingPlaceholderValue:
  421. case Value::Kind::AlternativeConstructorValue:
  422. case Value::Kind::ContinuationValue:
  423. case Value::Kind::LValue:
  424. // TODO: support pointer comparisons once we have a clearer distinction
  425. // between pointers and lvalues.
  426. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  427. }
  428. }
  429. auto ChoiceType::FindAlternative(std::string_view name) const
  430. -> std::optional<Nonnull<const Value*>> {
  431. for (const NamedValue& alternative : alternatives_) {
  432. if (alternative.name == name) {
  433. return alternative.value;
  434. }
  435. }
  436. return std::nullopt;
  437. }
  438. } // namespace Carbon