value.cpp 16 KB

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