value.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. if (placeholder.name().has_value()) {
  128. out << *placeholder.name();
  129. } else {
  130. out << "_";
  131. }
  132. out << ": " << placeholder.type();
  133. break;
  134. }
  135. case Value::Kind::AlternativeValue: {
  136. const auto& alt = cast<AlternativeValue>(*this);
  137. out << "alt " << alt.choice_name() << "." << alt.alt_name() << " "
  138. << alt.argument();
  139. break;
  140. }
  141. case Value::Kind::StructValue: {
  142. const auto& struct_val = cast<StructValue>(*this);
  143. out << "{";
  144. llvm::ListSeparator sep;
  145. for (const NamedValue& element : struct_val.elements()) {
  146. out << sep << "." << element.name << " = " << *element.value;
  147. }
  148. out << "}";
  149. break;
  150. }
  151. case Value::Kind::NominalClassValue: {
  152. const auto& s = cast<NominalClassValue>(*this);
  153. out << cast<NominalClassType>(s.type()).name() << s.inits();
  154. break;
  155. }
  156. case Value::Kind::TupleValue: {
  157. out << "(";
  158. llvm::ListSeparator sep;
  159. for (Nonnull<const Value*> element : cast<TupleValue>(*this).elements()) {
  160. out << sep << *element;
  161. }
  162. out << ")";
  163. break;
  164. }
  165. case Value::Kind::IntValue:
  166. out << cast<IntValue>(*this).value();
  167. break;
  168. case Value::Kind::BoolValue:
  169. out << (cast<BoolValue>(*this).value() ? "true" : "false");
  170. break;
  171. case Value::Kind::FunctionValue:
  172. out << "fun<" << cast<FunctionValue>(*this).declaration().name() << ">";
  173. break;
  174. case Value::Kind::LValue:
  175. out << "ptr<" << 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. }
  244. }
  245. ContinuationValue::StackFragment::~StackFragment() {
  246. CHECK(reversed_todo_.empty())
  247. << "All StackFragments must be empty before the Carbon program ends.";
  248. }
  249. void ContinuationValue::StackFragment::StoreReversed(
  250. std::vector<std::unique_ptr<Action>> reversed_todo) {
  251. CHECK(reversed_todo_.empty());
  252. reversed_todo_ = std::move(reversed_todo);
  253. }
  254. void ContinuationValue::StackFragment::RestoreTo(
  255. Stack<std::unique_ptr<Action>>& todo) {
  256. while (!reversed_todo_.empty()) {
  257. todo.Push(std::move(reversed_todo_.back()));
  258. reversed_todo_.pop_back();
  259. }
  260. }
  261. void ContinuationValue::StackFragment::Clear() {
  262. // We destroy the underlying Actions explicitly to ensure they're
  263. // destroyed in the correct order.
  264. for (auto& action : reversed_todo_) {
  265. action.reset();
  266. }
  267. reversed_todo_.clear();
  268. }
  269. void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
  270. out << "{";
  271. llvm::ListSeparator sep(" :: ");
  272. for (const std::unique_ptr<Action>& action : reversed_todo_) {
  273. out << sep << *action;
  274. }
  275. out << "}";
  276. }
  277. auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
  278. if (t1->kind() != t2->kind()) {
  279. return false;
  280. }
  281. switch (t1->kind()) {
  282. case Value::Kind::PointerType:
  283. return TypeEqual(&cast<PointerType>(*t1).type(),
  284. &cast<PointerType>(*t2).type());
  285. case Value::Kind::FunctionType: {
  286. const auto& fn1 = cast<FunctionType>(*t1);
  287. const auto& fn2 = cast<FunctionType>(*t2);
  288. return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
  289. TypeEqual(&fn1.return_type(), &fn2.return_type());
  290. }
  291. case Value::Kind::StructType: {
  292. const auto& struct1 = cast<StructType>(*t1);
  293. const auto& struct2 = cast<StructType>(*t2);
  294. if (struct1.fields().size() != struct2.fields().size()) {
  295. return false;
  296. }
  297. for (size_t i = 0; i < struct1.fields().size(); ++i) {
  298. if (struct1.fields()[i].name != struct2.fields()[i].name ||
  299. !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
  300. return false;
  301. }
  302. }
  303. return true;
  304. }
  305. case Value::Kind::NominalClassType:
  306. return cast<NominalClassType>(*t1).name() ==
  307. cast<NominalClassType>(*t2).name();
  308. case Value::Kind::ChoiceType:
  309. return cast<ChoiceType>(*t1).name() == cast<ChoiceType>(*t2).name();
  310. case Value::Kind::TupleValue: {
  311. const auto& tup1 = cast<TupleValue>(*t1);
  312. const auto& tup2 = cast<TupleValue>(*t2);
  313. if (tup1.elements().size() != tup2.elements().size()) {
  314. return false;
  315. }
  316. for (size_t i = 0; i < tup1.elements().size(); ++i) {
  317. if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
  318. return false;
  319. }
  320. }
  321. return true;
  322. }
  323. case Value::Kind::IntType:
  324. case Value::Kind::BoolType:
  325. case Value::Kind::ContinuationType:
  326. case Value::Kind::TypeType:
  327. case Value::Kind::StringType:
  328. return true;
  329. case Value::Kind::VariableType:
  330. return &cast<VariableType>(*t1).binding() ==
  331. &cast<VariableType>(*t2).binding();
  332. default:
  333. FATAL() << "TypeEqual used to compare non-type values\n"
  334. << *t1 << "\n"
  335. << *t2;
  336. }
  337. }
  338. // Returns true if the two values are equal and returns false otherwise.
  339. //
  340. // This function implements the `==` operator of Carbon.
  341. auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
  342. SourceLocation source_loc) -> bool {
  343. if (v1->kind() != v2->kind()) {
  344. return false;
  345. }
  346. switch (v1->kind()) {
  347. case Value::Kind::IntValue:
  348. return cast<IntValue>(*v1).value() == cast<IntValue>(*v2).value();
  349. case Value::Kind::BoolValue:
  350. return cast<BoolValue>(*v1).value() == cast<BoolValue>(*v2).value();
  351. case Value::Kind::FunctionValue: {
  352. std::optional<Nonnull<const Statement*>> body1 =
  353. cast<FunctionValue>(*v1).declaration().body();
  354. std::optional<Nonnull<const Statement*>> body2 =
  355. cast<FunctionValue>(*v2).declaration().body();
  356. return body1.has_value() == body2.has_value() &&
  357. (!body1.has_value() || *body1 == *body2);
  358. }
  359. case Value::Kind::TupleValue: {
  360. const std::vector<Nonnull<const Value*>>& elements1 =
  361. cast<TupleValue>(*v1).elements();
  362. const std::vector<Nonnull<const Value*>>& elements2 =
  363. cast<TupleValue>(*v2).elements();
  364. if (elements1.size() != elements2.size()) {
  365. return false;
  366. }
  367. for (size_t i = 0; i < elements1.size(); ++i) {
  368. if (!ValueEqual(elements1[i], elements2[i], source_loc)) {
  369. return false;
  370. }
  371. }
  372. return true;
  373. }
  374. case Value::Kind::StructValue: {
  375. const auto& struct_v1 = cast<StructValue>(*v1);
  376. const auto& struct_v2 = cast<StructValue>(*v2);
  377. CHECK(struct_v1.elements().size() == struct_v2.elements().size());
  378. for (size_t i = 0; i < struct_v1.elements().size(); ++i) {
  379. CHECK(struct_v1.elements()[i].name == struct_v2.elements()[i].name);
  380. if (!ValueEqual(struct_v1.elements()[i].value,
  381. struct_v2.elements()[i].value, source_loc)) {
  382. return false;
  383. }
  384. }
  385. return true;
  386. }
  387. case Value::Kind::StringValue:
  388. return cast<StringValue>(*v1).value() == cast<StringValue>(*v2).value();
  389. case Value::Kind::IntType:
  390. case Value::Kind::BoolType:
  391. case Value::Kind::TypeType:
  392. case Value::Kind::FunctionType:
  393. case Value::Kind::PointerType:
  394. case Value::Kind::AutoType:
  395. case Value::Kind::StructType:
  396. case Value::Kind::NominalClassType:
  397. case Value::Kind::ChoiceType:
  398. case Value::Kind::ContinuationType:
  399. case Value::Kind::VariableType:
  400. case Value::Kind::StringType:
  401. return TypeEqual(v1, v2);
  402. case Value::Kind::NominalClassValue:
  403. case Value::Kind::AlternativeValue:
  404. case Value::Kind::BindingPlaceholderValue:
  405. case Value::Kind::AlternativeConstructorValue:
  406. case Value::Kind::ContinuationValue:
  407. case Value::Kind::LValue:
  408. // TODO: support pointer comparisons once we have a clearer distinction
  409. // between pointers and lvalues.
  410. FATAL() << "ValueEqual does not support this kind of value: " << *v1;
  411. }
  412. }
  413. auto ChoiceType::FindAlternative(std::string_view name) const
  414. -> std::optional<Nonnull<const Value*>> {
  415. for (const NamedValue& alternative : alternatives_) {
  416. if (alternative.name == name) {
  417. return alternative.value;
  418. }
  419. }
  420. return std::nullopt;
  421. }
  422. } // namespace Carbon