|
|
@@ -10,6 +10,7 @@
|
|
|
#include "explorer/common/arena.h"
|
|
|
#include "explorer/common/error_builders.h"
|
|
|
#include "explorer/interpreter/action.h"
|
|
|
+#include "llvm/ADT/STLExtras.h"
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
#include "llvm/Support/Error.h"
|
|
|
@@ -18,6 +19,8 @@ namespace Carbon {
|
|
|
|
|
|
using llvm::cast;
|
|
|
using llvm::dyn_cast;
|
|
|
+using llvm::dyn_cast_or_null;
|
|
|
+using llvm::isa;
|
|
|
|
|
|
auto StructValue::FindField(std::string_view name) const
|
|
|
-> std::optional<Nonnull<const Value*>> {
|
|
|
@@ -37,6 +40,16 @@ static auto GetMember(Nonnull<Arena*> arena, Nonnull<const Value*> v,
|
|
|
|
|
|
if (field.witness().has_value()) {
|
|
|
Nonnull<const Witness*> witness = cast<Witness>(*field.witness());
|
|
|
+
|
|
|
+ // Associated constants.
|
|
|
+ if (auto* assoc_const = dyn_cast_or_null<AssociatedConstantDeclaration>(
|
|
|
+ field.member().declaration().value_or(nullptr))) {
|
|
|
+ CARBON_CHECK(field.interface()) << "have witness but no interface";
|
|
|
+ return arena->New<AssociatedConstant>(v, *field.interface(), assoc_const,
|
|
|
+ witness);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Associated functions.
|
|
|
switch (witness->kind()) {
|
|
|
case Value::Kind::ImplWitness: {
|
|
|
auto* impl_witness = cast<ImplWitness>(witness);
|
|
|
@@ -156,10 +169,10 @@ static auto SetFieldImpl(
|
|
|
switch (value->kind()) {
|
|
|
case Value::Kind::StructValue: {
|
|
|
std::vector<NamedValue> elements = cast<StructValue>(*value).elements();
|
|
|
- auto it = std::find_if(elements.begin(), elements.end(),
|
|
|
- [path_begin](const NamedValue& element) {
|
|
|
- return element.name == (*path_begin).name();
|
|
|
- });
|
|
|
+ auto it =
|
|
|
+ llvm::find_if(elements, [path_begin](const NamedValue& element) {
|
|
|
+ return element.name == (*path_begin).name();
|
|
|
+ });
|
|
|
if (it == elements.end()) {
|
|
|
return RuntimeError(source_loc)
|
|
|
<< "field " << (*path_begin).name() << " not in " << *value;
|
|
|
@@ -397,7 +410,7 @@ void Value::Print(llvm::raw_ostream& out) const {
|
|
|
out << combine << *ctx.context;
|
|
|
}
|
|
|
out << " where ";
|
|
|
- llvm::ListSeparator sep;
|
|
|
+ llvm::ListSeparator sep(" and ");
|
|
|
for (const ConstraintType::ImplConstraint& impl :
|
|
|
constraint.impl_constraints()) {
|
|
|
// TODO: Skip cases where `impl.type` is `.Self` and the interface is
|
|
|
@@ -453,6 +466,11 @@ void Value::Print(llvm::raw_ostream& out) const {
|
|
|
case Value::Kind::VariableType:
|
|
|
out << cast<VariableType>(*this).binding();
|
|
|
break;
|
|
|
+ case Value::Kind::AssociatedConstant: {
|
|
|
+ const auto& assoc = cast<AssociatedConstant>(*this);
|
|
|
+ out << "(" << assoc.base() << ")." << assoc.constant().binding().name();
|
|
|
+ break;
|
|
|
+ }
|
|
|
case Value::Kind::ContinuationValue: {
|
|
|
out << cast<ContinuationValue>(*this).stack();
|
|
|
break;
|
|
|
@@ -540,30 +558,36 @@ void ContinuationValue::StackFragment::Print(llvm::raw_ostream& out) const {
|
|
|
|
|
|
// Check whether two binding maps, which are assumed to have the same keys, are
|
|
|
// equal.
|
|
|
-static auto BindingMapEqual(const BindingMap& map1, const BindingMap& map2)
|
|
|
- -> bool {
|
|
|
+static auto BindingMapEqual(
|
|
|
+ const BindingMap& map1, const BindingMap& map2,
|
|
|
+ std::optional<Nonnull<const EqualityContext*>> equality_ctx) -> bool {
|
|
|
CARBON_CHECK(map1.size() == map2.size()) << "maps should have same keys";
|
|
|
for (const auto& [key, value] : map1) {
|
|
|
- if (!ValueEqual(value, map2.at(key))) {
|
|
|
+ if (!ValueEqual(value, map2.at(key), equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
+auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2,
|
|
|
+ std::optional<Nonnull<const EqualityContext*>> equality_ctx)
|
|
|
+ -> bool {
|
|
|
if (t1->kind() != t2->kind()) {
|
|
|
+ if (isa<AssociatedConstant>(t1) || isa<AssociatedConstant>(t2)) {
|
|
|
+ return ValueEqual(t1, t2, equality_ctx);
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
switch (t1->kind()) {
|
|
|
case Value::Kind::PointerType:
|
|
|
return TypeEqual(&cast<PointerType>(*t1).type(),
|
|
|
- &cast<PointerType>(*t2).type());
|
|
|
+ &cast<PointerType>(*t2).type(), equality_ctx);
|
|
|
case Value::Kind::FunctionType: {
|
|
|
const auto& fn1 = cast<FunctionType>(*t1);
|
|
|
const auto& fn2 = cast<FunctionType>(*t2);
|
|
|
- return TypeEqual(&fn1.parameters(), &fn2.parameters()) &&
|
|
|
- TypeEqual(&fn1.return_type(), &fn2.return_type());
|
|
|
+ return TypeEqual(&fn1.parameters(), &fn2.parameters(), equality_ctx) &&
|
|
|
+ TypeEqual(&fn1.return_type(), &fn2.return_type(), equality_ctx);
|
|
|
}
|
|
|
case Value::Kind::StructType: {
|
|
|
const auto& struct1 = cast<StructType>(*t1);
|
|
|
@@ -573,7 +597,8 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
}
|
|
|
for (size_t i = 0; i < struct1.fields().size(); ++i) {
|
|
|
if (struct1.fields()[i].name != struct2.fields()[i].name ||
|
|
|
- !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value)) {
|
|
|
+ !TypeEqual(struct1.fields()[i].value, struct2.fields()[i].value,
|
|
|
+ equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -583,14 +608,18 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
const auto& class1 = cast<NominalClassType>(*t1);
|
|
|
const auto& class2 = cast<NominalClassType>(*t2);
|
|
|
return class1.declaration().name() == class2.declaration().name() &&
|
|
|
- BindingMapEqual(class1.type_args(), class2.type_args());
|
|
|
+ BindingMapEqual(class1.type_args(), class2.type_args(),
|
|
|
+ equality_ctx);
|
|
|
}
|
|
|
case Value::Kind::InterfaceType: {
|
|
|
const auto& iface1 = cast<InterfaceType>(*t1);
|
|
|
const auto& iface2 = cast<InterfaceType>(*t2);
|
|
|
return iface1.declaration().name() == iface2.declaration().name() &&
|
|
|
- BindingMapEqual(iface1.args(), iface2.args());
|
|
|
+ BindingMapEqual(iface1.args(), iface2.args(), equality_ctx);
|
|
|
}
|
|
|
+ case Value::Kind::AssociatedConstant:
|
|
|
+ // Associated constants are sometimes types.
|
|
|
+ return ValueEqual(t1, t2, equality_ctx);
|
|
|
case Value::Kind::ConstraintType: {
|
|
|
const auto& constraint1 = cast<ConstraintType>(*t1);
|
|
|
const auto& constraint2 = cast<ConstraintType>(*t2);
|
|
|
@@ -605,8 +634,8 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
for (size_t i = 0; i < constraint1.impl_constraints().size(); ++i) {
|
|
|
const auto& impl1 = constraint1.impl_constraints()[i];
|
|
|
const auto& impl2 = constraint2.impl_constraints()[i];
|
|
|
- if (!TypeEqual(impl1.type, impl2.type) ||
|
|
|
- !TypeEqual(impl1.interface, impl2.interface)) {
|
|
|
+ if (!TypeEqual(impl1.type, impl2.type, equality_ctx) ||
|
|
|
+ !TypeEqual(impl1.interface, impl2.interface, equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -617,7 +646,8 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
return false;
|
|
|
}
|
|
|
for (size_t j = 0; j < equality1.values.size(); ++j) {
|
|
|
- if (!ValueEqual(equality1.values[i], equality2.values[i])) {
|
|
|
+ if (!ValueEqual(equality1.values[i], equality2.values[i],
|
|
|
+ equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -625,7 +655,7 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
for (size_t i = 0; i < constraint1.lookup_contexts().size(); ++i) {
|
|
|
const auto& context1 = constraint1.lookup_contexts()[i];
|
|
|
const auto& context2 = constraint2.lookup_contexts()[i];
|
|
|
- if (!TypeEqual(context1.context, context2.context)) {
|
|
|
+ if (!TypeEqual(context1.context, context2.context, equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -640,7 +670,7 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
return false;
|
|
|
}
|
|
|
for (size_t i = 0; i < tup1.elements().size(); ++i) {
|
|
|
- if (!TypeEqual(tup1.elements()[i], tup2.elements()[i])) {
|
|
|
+ if (!TypeEqual(tup1.elements()[i], tup2.elements()[i], equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -657,20 +687,24 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
&cast<VariableType>(*t2).binding();
|
|
|
case Value::Kind::TypeOfClassType:
|
|
|
return TypeEqual(&cast<TypeOfClassType>(*t1).class_type(),
|
|
|
- &cast<TypeOfClassType>(*t2).class_type());
|
|
|
+ &cast<TypeOfClassType>(*t2).class_type(), equality_ctx);
|
|
|
case Value::Kind::TypeOfInterfaceType:
|
|
|
return TypeEqual(&cast<TypeOfInterfaceType>(*t1).interface_type(),
|
|
|
- &cast<TypeOfInterfaceType>(*t2).interface_type());
|
|
|
+ &cast<TypeOfInterfaceType>(*t2).interface_type(),
|
|
|
+ equality_ctx);
|
|
|
case Value::Kind::TypeOfConstraintType:
|
|
|
return TypeEqual(&cast<TypeOfConstraintType>(*t1).constraint_type(),
|
|
|
- &cast<TypeOfConstraintType>(*t2).constraint_type());
|
|
|
+ &cast<TypeOfConstraintType>(*t2).constraint_type(),
|
|
|
+ equality_ctx);
|
|
|
case Value::Kind::TypeOfChoiceType:
|
|
|
return TypeEqual(&cast<TypeOfChoiceType>(*t1).choice_type(),
|
|
|
- &cast<TypeOfChoiceType>(*t2).choice_type());
|
|
|
+ &cast<TypeOfChoiceType>(*t2).choice_type(),
|
|
|
+ equality_ctx);
|
|
|
case Value::Kind::StaticArrayType: {
|
|
|
const auto& array1 = cast<StaticArrayType>(*t1);
|
|
|
const auto& array2 = cast<StaticArrayType>(*t2);
|
|
|
- return TypeEqual(&array1.element_type(), &array2.element_type()) &&
|
|
|
+ return TypeEqual(&array1.element_type(), &array2.element_type(),
|
|
|
+ equality_ctx) &&
|
|
|
array1.size() == array2.size();
|
|
|
}
|
|
|
case Value::Kind::IntValue:
|
|
|
@@ -704,10 +738,11 @@ auto TypeEqual(Nonnull<const Value*> t1, Nonnull<const Value*> t2) -> bool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// Returns true if the two values are equal and returns false otherwise.
|
|
|
-//
|
|
|
-// This function implements the `==` operator of Carbon.
|
|
|
-auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
+// Returns true if the two values are known to be equal and are written in the
|
|
|
+// same way at the top level.
|
|
|
+auto ValueStructurallyEqual(
|
|
|
+ Nonnull<const Value*> v1, Nonnull<const Value*> v2,
|
|
|
+ std::optional<Nonnull<const EqualityContext*>> equality_ctx) -> bool {
|
|
|
if (v1->kind() != v2->kind()) {
|
|
|
return false;
|
|
|
}
|
|
|
@@ -729,7 +764,7 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
const auto& m2 = cast<BoundMethodValue>(*v2);
|
|
|
std::optional<Nonnull<const Statement*>> body1 = m1.declaration().body();
|
|
|
std::optional<Nonnull<const Statement*>> body2 = m2.declaration().body();
|
|
|
- return ValueEqual(m1.receiver(), m2.receiver()) &&
|
|
|
+ return ValueEqual(m1.receiver(), m2.receiver(), equality_ctx) &&
|
|
|
body1.has_value() == body2.has_value() &&
|
|
|
(!body1.has_value() || *body1 == *body2);
|
|
|
}
|
|
|
@@ -742,7 +777,7 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
return false;
|
|
|
}
|
|
|
for (size_t i = 0; i < elements1.size(); ++i) {
|
|
|
- if (!ValueEqual(elements1[i], elements2[i])) {
|
|
|
+ if (!ValueEqual(elements1[i], elements2[i], equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -756,7 +791,7 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
CARBON_CHECK(struct_v1.elements()[i].name ==
|
|
|
struct_v2.elements()[i].name);
|
|
|
if (!ValueEqual(struct_v1.elements()[i].value,
|
|
|
- struct_v2.elements()[i].value)) {
|
|
|
+ struct_v2.elements()[i].value, equality_ctx)) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -773,6 +808,14 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
<< "parameterized name refers to unnamed declaration";
|
|
|
return *name1 == *name2;
|
|
|
}
|
|
|
+ case Value::Kind::AssociatedConstant: {
|
|
|
+ // The witness value is not part of determining value equality.
|
|
|
+ const auto& assoc1 = cast<AssociatedConstant>(*v1);
|
|
|
+ const auto& assoc2 = cast<AssociatedConstant>(*v2);
|
|
|
+ return &assoc1.constant() == &assoc2.constant() &&
|
|
|
+ TypeEqual(&assoc1.base(), &assoc2.base(), equality_ctx) &&
|
|
|
+ TypeEqual(&assoc1.interface(), &assoc2.interface(), equality_ctx);
|
|
|
+ }
|
|
|
case Value::Kind::IntType:
|
|
|
case Value::Kind::BoolType:
|
|
|
case Value::Kind::TypeType:
|
|
|
@@ -796,7 +839,7 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
case Value::Kind::TypeOfParameterizedEntityName:
|
|
|
case Value::Kind::TypeOfMemberName:
|
|
|
case Value::Kind::StaticArrayType:
|
|
|
- return TypeEqual(v1, v2);
|
|
|
+ return TypeEqual(v1, v2, equality_ctx);
|
|
|
case Value::Kind::NominalClassValue:
|
|
|
case Value::Kind::AlternativeValue:
|
|
|
case Value::Kind::BindingPlaceholderValue:
|
|
|
@@ -813,6 +856,78 @@ auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2) -> bool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// Returns true if the two values are equal and returns false otherwise.
|
|
|
+//
|
|
|
+// This function implements the `==` operator of Carbon.
|
|
|
+auto ValueEqual(Nonnull<const Value*> v1, Nonnull<const Value*> v2,
|
|
|
+ std::optional<Nonnull<const EqualityContext*>> equality_ctx)
|
|
|
+ -> bool {
|
|
|
+ // If we're given an equality context, check to see if it knows these values
|
|
|
+ // are equal. Only perform the check if one or the other value is an
|
|
|
+ // associated constant; otherwise we should be able to do better by looking
|
|
|
+ // at the structures of the values.
|
|
|
+ if (equality_ctx) {
|
|
|
+ if (isa<AssociatedConstant>(v1)) {
|
|
|
+ auto visitor = [&](Nonnull<const Value*> maybe_v2) {
|
|
|
+ return !ValueStructurallyEqual(v2, maybe_v2, equality_ctx);
|
|
|
+ };
|
|
|
+ if (!(*equality_ctx)->VisitEqualValues(v1, visitor)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (isa<AssociatedConstant>(v2)) {
|
|
|
+ auto visitor = [&](Nonnull<const Value*> maybe_v1) {
|
|
|
+ return !ValueStructurallyEqual(v1, maybe_v1, equality_ctx);
|
|
|
+ };
|
|
|
+ if (!(*equality_ctx)->VisitEqualValues(v2, visitor)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return ValueStructurallyEqual(v1, v2, equality_ctx);
|
|
|
+}
|
|
|
+
|
|
|
+auto EqualityConstraint::VisitEqualValues(
|
|
|
+ Nonnull<const Value*> value,
|
|
|
+ llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
|
|
|
+ // See if the given value is part of this constraint.
|
|
|
+ auto first_equal = llvm::find_if(values, [value](Nonnull<const Value*> val) {
|
|
|
+ return ValueEqual(value, val, std::nullopt);
|
|
|
+ });
|
|
|
+ if (first_equal == values.end()) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // The value is in this group; pass all non-identical values in the group
|
|
|
+ // to the visitor. First visit the values we already compared.
|
|
|
+ for (auto* val : llvm::make_range(values.begin(), first_equal)) {
|
|
|
+ if (!visitor(val)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Then visit any remaining non-identical values, skipping the one we already
|
|
|
+ // found was identical.
|
|
|
+ ++first_equal;
|
|
|
+ for (auto* val : llvm::make_range(first_equal, values.end())) {
|
|
|
+ if (!ValueEqual(value, val, std::nullopt) && !visitor(val)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+auto ConstraintType::VisitEqualValues(
|
|
|
+ Nonnull<const Value*> value,
|
|
|
+ llvm::function_ref<bool(Nonnull<const Value*>)> visitor) const -> bool {
|
|
|
+ for (const auto& eq : equality_constraints()) {
|
|
|
+ if (!eq.VisitEqualValues(value, visitor)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
auto ChoiceType::FindAlternative(std::string_view name) const
|
|
|
-> std::optional<Nonnull<const Value*>> {
|
|
|
for (const NamedValue& alternative : alternatives_) {
|