value.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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::PointerValue:
  172. out << "ptr<" << cast<PointerValue>(*this).address() << ">";
  173. break;
  174. case Value::Kind::LValue:
  175. out << "lval<" << cast<LValue>(*this).address() << ">";
  176. break;
  177. case Value::Kind::BoolType:
  178. out << "Bool";
  179. break;
  180. case Value::Kind::IntType:
  181. out << "i32";
  182. break;
  183. case Value::Kind::TypeType:
  184. out << "Type";
  185. break;
  186. case Value::Kind::AutoType:
  187. out << "auto";
  188. break;
  189. case Value::Kind::ContinuationType:
  190. out << "Continuation";
  191. break;
  192. case Value::Kind::PointerType:
  193. out << cast<PointerType>(*this).type() << "*";
  194. break;
  195. case Value::Kind::FunctionType: {
  196. const auto& fn_type = cast<FunctionType>(*this);
  197. out << "fn ";
  198. if (fn_type.deduced().size() > 0) {
  199. out << "[";
  200. unsigned int i = 0;
  201. for (Nonnull<const GenericBinding*> deduced : fn_type.deduced()) {
  202. if (i != 0) {
  203. out << ", ";
  204. }
  205. out << deduced->name() << ":! " << deduced->type();
  206. ++i;
  207. }
  208. out << "]";
  209. }
  210. out << fn_type.parameters() << " -> " << fn_type.return_type();
  211. break;
  212. }
  213. case Value::Kind::StructType: {
  214. out << "{";
  215. llvm::ListSeparator sep;
  216. for (const auto& [name, type] : cast<StructType>(*this).fields()) {
  217. out << sep << "." << name << ": " << *type;
  218. }
  219. out << "}";
  220. break;
  221. }
  222. case Value::Kind::NominalClassType:
  223. out << "class " << cast<NominalClassType>(*this).name();
  224. break;
  225. case Value::Kind::ChoiceType:
  226. out << "choice " << cast<ChoiceType>(*this).name();
  227. break;
  228. case Value::Kind::VariableType:
  229. out << cast<VariableType>(*this).binding().name();
  230. break;
  231. case Value::Kind::ContinuationValue: {
  232. out << cast<ContinuationValue>(*this).stack();
  233. break;
  234. }
  235. case Value::Kind::StringType:
  236. out << "String";
  237. break;
  238. case Value::Kind::StringValue:
  239. out << "\"";
  240. out.write_escaped(cast<StringValue>(*this).value());
  241. out << "\"";
  242. break;
  243. case Value::Kind::TypeOfClassType:
  244. out << "typeof(" << cast<TypeOfClassType>(*this).class_type().name()
  245. << ")";
  246. break;
  247. case Value::Kind::TypeOfChoiceType:
  248. out << "typeof(" << cast<TypeOfChoiceType>(*this).choice_type().name()
  249. << ")";
  250. break;
  251. }
  252. }
  253. ContinuationValue::StackFragment::~StackFragment() {
  254. CHECK(reversed_todo_.empty())
  255. << "All StackFragments must be empty before the Carbon program ends.";
  256. }
  257. void ContinuationValue::StackFragment::StoreReversed(
  258. std::vector<std::unique_ptr<Action>> reversed_todo) {
  259. CHECK(reversed_todo_.empty());
  260. reversed_todo_ = std::move(reversed_todo);
  261. }
  262. void ContinuationValue::StackFragment::RestoreTo(
  263. Stack<std::unique_ptr<Action>>& todo) {
  264. while (!reversed_todo_.empty()) {
  265. todo.Push(std::move(reversed_todo_.back()));
  266. reversed_todo_.pop_back();
  267. }
  268. }
  269. void ContinuationValue::StackFragment::Clear() {
  270. // We destroy the underlying Actions explicitly to ensure they're
  271. // destroyed in the correct order.
  272. for (auto& action : reversed_todo_) {
  273. action.reset();
  274. }
  275. reversed_todo_.clear();
  276. }
  277. void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
  278. out << "{";
  279. llvm::ListSeparator sep(" :: ");
  280. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  281. out << sep << *action;
  282. }
  283. out << "}";
  284. }
  285. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
  286. if (t1->kind() != t2->kind()) {
  287. return false;
  288. }
  289. switch (t1->kind()) {
  290. case Value::Kind::PointerType:
  291. return TypeEqual(&cast<PointerType>(*t1).type(),
  292. &cast<PointerType>(*t2).type());
  293. case Value::Kind::FunctionType: {
  294. const auto& fn1 = cast<FunctionType>(*t1);
  295. const auto& fn2 = cast<FunctionType>(*t2);
  296. return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
  297. TypeEqual(&fn1.return_type(), &fn2.return_type());
  298. }
  299. case Value::Kind::StructType: {
  300. const auto& struct1 = cast<StructType>(*t1);
  301. const auto& struct2 = cast<StructType>(*t2);
  302. if (struct1.fields().size() != struct2.fields().size()) {
  303. return false;
  304. }
  305. for (size_t i = 0; i < struct1.fields().size(); ++i) {
  306. if (struct1.fields()[i].name != struct2.fields()[i].name ||
  307. !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. case Value::Kind::NominalClassType:
  314. return cast<NominalClassType>(*t1).name() ==
  315. cast<NominalClassType>(*t2).name();
  316. case Value::Kind::ChoiceType:
  317. return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
  318. case Value::Kind::TupleValue: {
  319. const auto& tup1 = cast<TupleValue>(*t1);
  320. const auto& tup2 = cast<TupleValue>(*t2);
  321. if (tup1.elements().size() != tup2.elements().size()) {
  322. return false;
  323. }
  324. for (size_t i = 0; i < tup1.elements().size(); ++i) {
  325. if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
  326. return false;
  327. }
  328. }
  329. return true;
  330. }
  331. case Value::Kind::IntType:
  332. case Value::Kind::BoolType:
  333. case Value::Kind::ContinuationType:
  334. case Value::Kind::TypeType:
  335. case Value::Kind::StringType:
  336. return true;
  337. case Value::Kind::VariableType:
  338. return &cast<VariableType>(*t1).binding() ==
  339. &cast<VariableType>(*t2).binding();
  340. case Value::Kind::TypeOfClassType:
  341. return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
  342. &cast<TypeOfClassType>(*t2).class_type());
  343. case Value::Kind::TypeOfChoiceType:
  344. return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
  345. &cast<TypeOfChoiceType>(*t2).choice_type());
  346. default:
  347. FATAL() << "TypeEqual used to compare non-type values\n"
  348. << *t1 << "\n"
  349. << *t2;
  350. }
  351. }
  352. // Returns true if the two values are equal and returns false otherwise.
  353. //
  354. // This function implements the `==` operator of Carbon.
  355. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
  356. if (v1->kind() != v2->kind()) {
  357. return false;
  358. }
  359. switch (v1->kind()) {
  360. case Value::Kind::IntValue:
  361. return cast<IntValue>(*v1).value() == cast<IntValue>(*v2).value();
  362. case Value::Kind::BoolValue:
  363. return cast<BoolValue>(*v1).value() == cast<BoolValue>(*v2).value();
  364. case Value::Kind::FunctionValue: {
  365. std::optional<Nonnull<const Statement*>> body1 =
  366. cast<FunctionValue>(*v1).declaration().body();
  367. std::optional<Nonnull<const Statement*>> body2 =
  368. cast<FunctionValue>(*v2).declaration().body();
  369. return body1.has_value() == body2.has_value() &&
  370. (!body1.has_value() || *body1 == *body2);
  371. }
  372. case Value::Kind::TupleValue: {
  373. const std::vector<Nonnull<const Value*>>& elements1 =
  374. cast<TupleValue>(*v1).elements();
  375. const std::vector<Nonnull<const Value*>>& elements2 =
  376. cast<TupleValue>(*v2).elements();
  377. if (elements1.size() != elements2.size()) {
  378. return false;
  379. }
  380. for (size_t i = 0; i < elements1.size(); ++i) {
  381. if (!ValueEqual(elements1[i], elements2[i])) {
  382. return false;
  383. }
  384. }
  385. return true;
  386. }
  387. case Value::Kind::StructValue: {
  388. const auto& struct_v1 = cast<StructValue>(*v1);
  389. const auto& struct_v2 = cast<StructValue>(*v2);
  390. CHECK(struct_v1.elements().size() == struct_v2.elements().size());
  391. for (size_t i = 0; i < struct_v1.elements().size(); ++i) {
  392. CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name);
  393. if (!ValueEqual(struct_v1.elements()[i].value,
  394. struct_v2.elements()[i].value)) {
  395. return false;
  396. }
  397. }
  398. return true;
  399. }
  400. case Value::Kind::StringValue:
  401. return cast<StringValue>(*v1).value() == cast<StringValue>(*v2).value();
  402. case Value::Kind::IntType:
  403. case Value::Kind::BoolType:
  404. case Value::Kind::TypeType:
  405. case Value::Kind::FunctionType:
  406. case Value::Kind::PointerType:
  407. case Value::Kind::AutoType:
  408. case Value::Kind::StructType:
  409. case Value::Kind::NominalClassType:
  410. case Value::Kind::ChoiceType:
  411. case Value::Kind::ContinuationType:
  412. case Value::Kind::VariableType:
  413. case Value::Kind::StringType:
  414. case Value::Kind::TypeOfClassType:
  415. case Value::Kind::TypeOfChoiceType:
  416. return TypeEqual(v1, v2);
  417. case Value::Kind::NominalClassValue:
  418. case Value::Kind::AlternativeValue:
  419. case Value::Kind::BindingPlaceholderValue:
  420. case Value::Kind::AlternativeConstructorValue:
  421. case Value::Kind::ContinuationValue:
  422. case Value::Kind::PointerValue:
  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