mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Switch Value to use the inherit/cast model (#703)
This commit is contained in:
@@ -10,70 +10,11 @@
|
||||
#include "executable_semantics/common/arena.h"
|
||||
#include "executable_semantics/common/error.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/Casting.h"
|
||||
|
||||
namespace Carbon {
|
||||
|
||||
auto Value::GetIntValue() const -> int {
|
||||
return std::get<IntValue>(value).value;
|
||||
}
|
||||
|
||||
auto Value::GetBoolValue() const -> bool {
|
||||
return std::get<BoolValue>(value).value;
|
||||
}
|
||||
|
||||
auto Value::GetFunctionValue() const -> const FunctionValue& {
|
||||
return std::get<FunctionValue>(value);
|
||||
}
|
||||
|
||||
auto Value::GetStructValue() const -> const StructValue& {
|
||||
return std::get<StructValue>(value);
|
||||
}
|
||||
|
||||
auto Value::GetAlternativeConstructorValue() const
|
||||
-> const AlternativeConstructorValue& {
|
||||
return std::get<AlternativeConstructorValue>(value);
|
||||
}
|
||||
|
||||
auto Value::GetAlternativeValue() const -> const AlternativeValue& {
|
||||
return std::get<AlternativeValue>(value);
|
||||
}
|
||||
|
||||
auto Value::GetTupleValue() const -> const TupleValue& {
|
||||
return std::get<TupleValue>(value);
|
||||
}
|
||||
|
||||
auto Value::GetPointerValue() const -> Address {
|
||||
return std::get<PointerValue>(value).value;
|
||||
}
|
||||
|
||||
auto Value::GetBindingPlaceholderValue() const
|
||||
-> const BindingPlaceholderValue& {
|
||||
return std::get<BindingPlaceholderValue>(value);
|
||||
}
|
||||
|
||||
auto Value::GetFunctionType() const -> const FunctionType& {
|
||||
return std::get<FunctionType>(value);
|
||||
}
|
||||
|
||||
auto Value::GetPointerType() const -> const PointerType& {
|
||||
return std::get<PointerType>(value);
|
||||
}
|
||||
|
||||
auto Value::GetStructType() const -> const StructType& {
|
||||
return std::get<StructType>(value);
|
||||
}
|
||||
|
||||
auto Value::GetChoiceType() const -> const ChoiceType& {
|
||||
return std::get<ChoiceType>(value);
|
||||
}
|
||||
|
||||
auto Value::GetVariableType() const -> const VariableType& {
|
||||
return std::get<VariableType>(value);
|
||||
}
|
||||
|
||||
auto Value::GetContinuationValue() const -> const ContinuationValue& {
|
||||
return std::get<ContinuationValue>(value);
|
||||
}
|
||||
using llvm::cast;
|
||||
|
||||
auto FindInVarValues(const std::string& field, const VarValues& inits)
|
||||
-> const Value* {
|
||||
@@ -111,178 +52,33 @@ auto TupleValue::FindField(const std::string& name) const -> const Value* {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto Value::MakeIntValue(int i) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = IntValue({.value = i});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeBoolValue(bool b) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = BoolValue({.value = b});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeFunctionValue(std::string name, const Value* param,
|
||||
const Statement* body) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value =
|
||||
FunctionValue({.name = std::move(name), .param = param, .body = body});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakePointerValue(Address addr) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = PointerValue({.value = addr});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeStructValue(const Value* type, const Value* inits)
|
||||
-> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = StructValue({.type = type, .inits = inits});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeTupleValue(std::vector<TupleElement> elements) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = TupleValue({.elements = std::move(elements)});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeAlternativeValue(std::string alt_name, std::string choice_name,
|
||||
const Value* argument) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = AlternativeValue({.alt_name = std::move(alt_name),
|
||||
.choice_name = std::move(choice_name),
|
||||
.argument = argument});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeAlternativeConstructorValue(std::string alt_name,
|
||||
std::string choice_name)
|
||||
-> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = AlternativeConstructorValue(
|
||||
{.alt_name = std::move(alt_name), .choice_name = std::move(choice_name)});
|
||||
return v;
|
||||
}
|
||||
|
||||
// Return a first-class continuation represented a fragment
|
||||
// of the stack.
|
||||
auto Value::MakeContinuationValue(std::vector<Frame*> stack) -> Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = ContinuationValue({.stack = std::move(stack)});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeBindingPlaceholderValue(std::optional<std::string> name,
|
||||
const Value* type) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = BindingPlaceholderValue({.name = std::move(name), .type = type});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeIntType() -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = IntType();
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeBoolType() -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = BoolType();
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeTypeType() -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = TypeType();
|
||||
return v;
|
||||
}
|
||||
|
||||
// Return a Continuation type.
|
||||
auto Value::MakeContinuationType() -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = ContinuationType();
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeAutoType() -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = AutoType();
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeFunctionType(std::vector<GenericBinding> deduced_params,
|
||||
const Value* param, const Value* ret)
|
||||
-> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = FunctionType(
|
||||
{.deduced = std::move(deduced_params), .param = param, .ret = ret});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakePointerType(const Value* type) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = PointerType({.type = type});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeStructType(std::string name, VarValues fields,
|
||||
VarValues methods) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = StructType({.name = std::move(name),
|
||||
.fields = std::move(fields),
|
||||
.methods = std::move(methods)});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeUnitTypeVal() -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = TupleValue({.elements = {}});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeChoiceType(std::string name, VarValues alts) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value =
|
||||
ChoiceType({.name = std::move(name), .alternatives = std::move(alts)});
|
||||
return v;
|
||||
}
|
||||
|
||||
auto Value::MakeVariableType(std::string name) -> const Value* {
|
||||
auto* v = global_arena->New<Value>();
|
||||
v->value = VariableType({.name = std::move(name)});
|
||||
return v;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
auto GetMember(const Value* v, const std::string& f, int line_num)
|
||||
-> const Value* {
|
||||
switch (v->tag()) {
|
||||
case ValKind::StructValue: {
|
||||
switch (v->Tag()) {
|
||||
case Value::Kind::StructValue: {
|
||||
const Value* field =
|
||||
v->GetStructValue().inits->GetTupleValue().FindField(f);
|
||||
cast<TupleValue>(*cast<StructValue>(*v).Inits()).FindField(f);
|
||||
if (field == nullptr) {
|
||||
FATAL_RUNTIME_ERROR(line_num) << "member " << f << " not in " << *v;
|
||||
}
|
||||
return field;
|
||||
}
|
||||
case ValKind::TupleValue: {
|
||||
const Value* field = v->GetTupleValue().FindField(f);
|
||||
case Value::Kind::TupleValue: {
|
||||
const Value* field = cast<TupleValue>(*v).FindField(f);
|
||||
if (field == nullptr) {
|
||||
FATAL_RUNTIME_ERROR(line_num) << "field " << f << " not in " << *v;
|
||||
}
|
||||
return field;
|
||||
}
|
||||
case ValKind::ChoiceType: {
|
||||
if (FindInVarValues(f, v->GetChoiceType().alternatives) == nullptr) {
|
||||
case Value::Kind::ChoiceType: {
|
||||
const auto& choice = cast<ChoiceType>(*v);
|
||||
if (FindInVarValues(f, choice.Alternatives()) == nullptr) {
|
||||
FATAL_RUNTIME_ERROR(line_num)
|
||||
<< "alternative " << f << " not in " << *v;
|
||||
}
|
||||
return Value::MakeAlternativeConstructorValue(f, v->GetChoiceType().name);
|
||||
return global_arena->New<AlternativeConstructorValue>(f, choice.Name());
|
||||
}
|
||||
default:
|
||||
llvm::errs() << "field access not allowed for value " << *v << "\n";
|
||||
@@ -310,13 +106,13 @@ auto SetFieldImpl(const Value* value,
|
||||
if (path_begin == path_end) {
|
||||
return field_value;
|
||||
}
|
||||
switch (value->tag()) {
|
||||
case ValKind::StructValue: {
|
||||
return SetFieldImpl(value->GetStructValue().inits, path_begin, path_end,
|
||||
field_value, line_num);
|
||||
switch (value->Tag()) {
|
||||
case Value::Kind::StructValue: {
|
||||
return SetFieldImpl(cast<StructValue>(*value).Inits(), path_begin,
|
||||
path_end, field_value, line_num);
|
||||
}
|
||||
case ValKind::TupleValue: {
|
||||
std::vector<TupleElement> elements = value->GetTupleValue().elements;
|
||||
case Value::Kind::TupleValue: {
|
||||
std::vector<TupleElement> elements = cast<TupleValue>(*value).Elements();
|
||||
auto it = std::find_if(elements.begin(), elements.end(),
|
||||
[path_begin](const TupleElement& element) {
|
||||
return element.name == *path_begin;
|
||||
@@ -327,7 +123,7 @@ auto SetFieldImpl(const Value* value,
|
||||
}
|
||||
it->value = SetFieldImpl(it->value, path_begin + 1, path_end, field_value,
|
||||
line_num);
|
||||
return Value::MakeTupleValue(elements);
|
||||
return global_arena->New<TupleValue>(elements);
|
||||
}
|
||||
default:
|
||||
llvm::errs() << "field access not allowed for value " << *value << "\n";
|
||||
@@ -344,78 +140,79 @@ auto Value::SetField(const FieldPath& path, const Value* field_value,
|
||||
}
|
||||
|
||||
void Value::Print(llvm::raw_ostream& out) const {
|
||||
switch (tag()) {
|
||||
case ValKind::AlternativeConstructorValue: {
|
||||
out << GetAlternativeConstructorValue().choice_name << "."
|
||||
<< GetAlternativeConstructorValue().alt_name;
|
||||
switch (Tag()) {
|
||||
case Value::Kind::AlternativeConstructorValue: {
|
||||
const auto& alt = cast<AlternativeConstructorValue>(*this);
|
||||
out << alt.ChoiceName() << "." << alt.AltName();
|
||||
break;
|
||||
}
|
||||
case ValKind::BindingPlaceholderValue: {
|
||||
const BindingPlaceholderValue& placeholder = GetBindingPlaceholderValue();
|
||||
if (placeholder.name.has_value()) {
|
||||
out << *placeholder.name;
|
||||
case Value::Kind::BindingPlaceholderValue: {
|
||||
const auto& placeholder = cast<BindingPlaceholderValue>(*this);
|
||||
if (placeholder.Name().has_value()) {
|
||||
out << *placeholder.Name();
|
||||
} else {
|
||||
out << "_";
|
||||
}
|
||||
out << ": " << *placeholder.type;
|
||||
out << ": " << *placeholder.Type();
|
||||
break;
|
||||
}
|
||||
case ValKind::AlternativeValue: {
|
||||
out << "alt " << GetAlternativeValue().choice_name << "."
|
||||
<< GetAlternativeValue().alt_name << " "
|
||||
<< *GetAlternativeValue().argument;
|
||||
case Value::Kind::AlternativeValue: {
|
||||
const auto& alt = cast<AlternativeValue>(*this);
|
||||
out << "alt " << alt.ChoiceName() << "." << alt.AltName() << " "
|
||||
<< *alt.Argument();
|
||||
break;
|
||||
}
|
||||
case ValKind::StructValue: {
|
||||
out << GetStructValue().type->GetStructType().name
|
||||
<< *GetStructValue().inits;
|
||||
case Value::Kind::StructValue: {
|
||||
const auto& s = cast<StructValue>(*this);
|
||||
out << cast<StructType>(*s.Type()).Name() << *s.Inits();
|
||||
break;
|
||||
}
|
||||
case ValKind::TupleValue: {
|
||||
case Value::Kind::TupleValue: {
|
||||
out << "(";
|
||||
llvm::ListSeparator sep;
|
||||
for (const TupleElement& element : GetTupleValue().elements) {
|
||||
for (const TupleElement& element : cast<TupleValue>(*this).Elements()) {
|
||||
out << sep << element.name << " = " << *element.value;
|
||||
}
|
||||
out << ")";
|
||||
break;
|
||||
}
|
||||
case ValKind::IntValue:
|
||||
out << GetIntValue();
|
||||
case Value::Kind::IntValue:
|
||||
out << cast<IntValue>(*this).Val();
|
||||
break;
|
||||
case ValKind::BoolValue:
|
||||
out << (GetBoolValue() ? "true" : "false");
|
||||
case Value::Kind::BoolValue:
|
||||
out << (cast<BoolValue>(*this).Val() ? "true" : "false");
|
||||
break;
|
||||
case ValKind::FunctionValue:
|
||||
out << "fun<" << GetFunctionValue().name << ">";
|
||||
case Value::Kind::FunctionValue:
|
||||
out << "fun<" << cast<FunctionValue>(*this).Name() << ">";
|
||||
break;
|
||||
case ValKind::PointerValue:
|
||||
out << "ptr<" << GetPointerValue() << ">";
|
||||
case Value::Kind::PointerValue:
|
||||
out << "ptr<" << cast<PointerValue>(*this).Val() << ">";
|
||||
break;
|
||||
case ValKind::BoolType:
|
||||
case Value::Kind::BoolType:
|
||||
out << "Bool";
|
||||
break;
|
||||
case ValKind::IntType:
|
||||
case Value::Kind::IntType:
|
||||
out << "Int";
|
||||
break;
|
||||
case ValKind::TypeType:
|
||||
case Value::Kind::TypeType:
|
||||
out << "Type";
|
||||
break;
|
||||
case ValKind::AutoType:
|
||||
case Value::Kind::AutoType:
|
||||
out << "auto";
|
||||
break;
|
||||
case ValKind::ContinuationType:
|
||||
case Value::Kind::ContinuationType:
|
||||
out << "Continuation";
|
||||
break;
|
||||
case ValKind::PointerType:
|
||||
out << *GetPointerType().type << "*";
|
||||
case Value::Kind::PointerType:
|
||||
out << *cast<PointerType>(*this).Type() << "*";
|
||||
break;
|
||||
case ValKind::FunctionType:
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*this);
|
||||
out << "fn ";
|
||||
if (GetFunctionType().deduced.size() > 0) {
|
||||
if (fn_type.Deduced().size() > 0) {
|
||||
out << "[";
|
||||
unsigned int i = 0;
|
||||
for (const auto& deduced : GetFunctionType().deduced) {
|
||||
for (const auto& deduced : fn_type.Deduced()) {
|
||||
if (i != 0) {
|
||||
out << ", ";
|
||||
}
|
||||
@@ -424,18 +221,19 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
out << "]";
|
||||
}
|
||||
out << *GetFunctionType().param << " -> " << *GetFunctionType().ret;
|
||||
out << *fn_type.Param() << " -> " << *fn_type.Ret();
|
||||
break;
|
||||
case ValKind::StructType:
|
||||
out << "struct " << GetStructType().name;
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
out << "struct " << cast<StructType>(*this).Name();
|
||||
break;
|
||||
case ValKind::ChoiceType:
|
||||
out << "choice " << GetChoiceType().name;
|
||||
case Value::Kind::ChoiceType:
|
||||
out << "choice " << cast<ChoiceType>(*this).Name();
|
||||
break;
|
||||
case ValKind::VariableType:
|
||||
out << GetVariableType().name;
|
||||
case Value::Kind::VariableType:
|
||||
out << cast<VariableType>(*this).Name();
|
||||
break;
|
||||
case ValKind::ContinuationValue:
|
||||
case Value::Kind::ContinuationValue:
|
||||
out << "continuation";
|
||||
// TODO: Find a way to print useful information about the continuation
|
||||
// without creating a dependency cycle.
|
||||
@@ -444,106 +242,108 @@ void Value::Print(llvm::raw_ostream& out) const {
|
||||
}
|
||||
|
||||
auto CopyVal(const Value* val, int line_num) -> const Value* {
|
||||
switch (val->tag()) {
|
||||
case ValKind::TupleValue: {
|
||||
switch (val->Tag()) {
|
||||
case Value::Kind::TupleValue: {
|
||||
std::vector<TupleElement> elements;
|
||||
for (const TupleElement& element : val->GetTupleValue().elements) {
|
||||
for (const TupleElement& element : cast<TupleValue>(*val).Elements()) {
|
||||
elements.push_back(
|
||||
{.name = element.name, .value = CopyVal(element.value, line_num)});
|
||||
}
|
||||
return Value::MakeTupleValue(std::move(elements));
|
||||
return global_arena->New<TupleValue>(std::move(elements));
|
||||
}
|
||||
case ValKind::AlternativeValue: {
|
||||
const Value* arg = CopyVal(val->GetAlternativeValue().argument, line_num);
|
||||
return Value::MakeAlternativeValue(val->GetAlternativeValue().alt_name,
|
||||
val->GetAlternativeValue().choice_name,
|
||||
arg);
|
||||
case Value::Kind::AlternativeValue: {
|
||||
const auto& alt = cast<AlternativeValue>(*val);
|
||||
const Value* arg = CopyVal(alt.Argument(), line_num);
|
||||
return global_arena->New<AlternativeValue>(alt.AltName(),
|
||||
alt.ChoiceName(), arg);
|
||||
}
|
||||
case ValKind::StructValue: {
|
||||
const Value* inits = CopyVal(val->GetStructValue().inits, line_num);
|
||||
return Value::MakeStructValue(val->GetStructValue().type, inits);
|
||||
case Value::Kind::StructValue: {
|
||||
const auto& s = cast<StructValue>(*val);
|
||||
const Value* inits = CopyVal(s.Inits(), line_num);
|
||||
return global_arena->New<StructValue>(s.Type(), inits);
|
||||
}
|
||||
case ValKind::IntValue:
|
||||
return Value::MakeIntValue(val->GetIntValue());
|
||||
case ValKind::BoolValue:
|
||||
return Value::MakeBoolValue(val->GetBoolValue());
|
||||
case ValKind::FunctionValue:
|
||||
return Value::MakeFunctionValue(val->GetFunctionValue().name,
|
||||
val->GetFunctionValue().param,
|
||||
val->GetFunctionValue().body);
|
||||
case ValKind::PointerValue:
|
||||
return Value::MakePointerValue(val->GetPointerValue());
|
||||
case ValKind::ContinuationValue:
|
||||
case Value::Kind::IntValue:
|
||||
return global_arena->New<IntValue>(cast<IntValue>(*val).Val());
|
||||
case Value::Kind::BoolValue:
|
||||
return global_arena->New<BoolValue>(cast<BoolValue>(*val).Val());
|
||||
case Value::Kind::FunctionValue: {
|
||||
const auto& fn_value = cast<FunctionValue>(*val);
|
||||
return global_arena->New<FunctionValue>(fn_value.Name(), fn_value.Param(),
|
||||
fn_value.Body());
|
||||
}
|
||||
case Value::Kind::PointerValue:
|
||||
return global_arena->New<PointerValue>(cast<PointerValue>(*val).Val());
|
||||
case Value::Kind::ContinuationValue:
|
||||
// Copying a continuation is "shallow".
|
||||
return val;
|
||||
case ValKind::FunctionType:
|
||||
return Value::MakeFunctionType(
|
||||
val->GetFunctionType().deduced,
|
||||
CopyVal(val->GetFunctionType().param, line_num),
|
||||
CopyVal(val->GetFunctionType().ret, line_num));
|
||||
|
||||
case ValKind::PointerType:
|
||||
return Value::MakePointerType(
|
||||
CopyVal(val->GetPointerType().type, line_num));
|
||||
case ValKind::IntType:
|
||||
return Value::MakeIntType();
|
||||
case ValKind::BoolType:
|
||||
return Value::MakeBoolType();
|
||||
case ValKind::TypeType:
|
||||
return Value::MakeTypeType();
|
||||
case ValKind::AutoType:
|
||||
return Value::MakeAutoType();
|
||||
case ValKind::ContinuationType:
|
||||
return Value::MakeContinuationType();
|
||||
case ValKind::VariableType:
|
||||
case ValKind::StructType:
|
||||
case ValKind::ChoiceType:
|
||||
case ValKind::BindingPlaceholderValue:
|
||||
case ValKind::AlternativeConstructorValue:
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn_type = cast<FunctionType>(*val);
|
||||
return global_arena->New<FunctionType>(fn_type.Deduced(),
|
||||
CopyVal(fn_type.Param(), line_num),
|
||||
CopyVal(fn_type.Ret(), line_num));
|
||||
}
|
||||
case Value::Kind::PointerType:
|
||||
return global_arena->New<PointerType>(
|
||||
CopyVal(cast<PointerType>(*val).Type(), line_num));
|
||||
case Value::Kind::IntType:
|
||||
return global_arena->New<IntType>();
|
||||
case Value::Kind::BoolType:
|
||||
return global_arena->New<BoolType>();
|
||||
case Value::Kind::TypeType:
|
||||
return global_arena->New<TypeType>();
|
||||
case Value::Kind::AutoType:
|
||||
return global_arena->New<AutoType>();
|
||||
case Value::Kind::ContinuationType:
|
||||
return global_arena->New<ContinuationType>();
|
||||
case Value::Kind::VariableType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::BindingPlaceholderValue:
|
||||
case Value::Kind::AlternativeConstructorValue:
|
||||
// TODO: These should be copied so that they don't get destructed.
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
auto TypeEqual(const Value* t1, const Value* t2) -> bool {
|
||||
if (t1->tag() != t2->tag()) {
|
||||
if (t1->Tag() != t2->Tag()) {
|
||||
return false;
|
||||
}
|
||||
switch (t1->tag()) {
|
||||
case ValKind::PointerType:
|
||||
return TypeEqual(t1->GetPointerType().type, t2->GetPointerType().type);
|
||||
case ValKind::FunctionType:
|
||||
return TypeEqual(t1->GetFunctionType().param,
|
||||
t2->GetFunctionType().param) &&
|
||||
TypeEqual(t1->GetFunctionType().ret, t2->GetFunctionType().ret);
|
||||
case ValKind::StructType:
|
||||
return t1->GetStructType().name == t2->GetStructType().name;
|
||||
case ValKind::ChoiceType:
|
||||
return t1->GetChoiceType().name == t2->GetChoiceType().name;
|
||||
case ValKind::TupleValue: {
|
||||
if (t1->GetTupleValue().elements.size() !=
|
||||
t2->GetTupleValue().elements.size()) {
|
||||
switch (t1->Tag()) {
|
||||
case Value::Kind::PointerType:
|
||||
return TypeEqual(cast<PointerType>(*t1).Type(),
|
||||
cast<PointerType>(*t2).Type());
|
||||
case Value::Kind::FunctionType: {
|
||||
const auto& fn1 = cast<FunctionType>(*t1);
|
||||
const auto& fn2 = cast<FunctionType>(*t2);
|
||||
return TypeEqual(fn1.Param(), fn2.Param()) &&
|
||||
TypeEqual(fn1.Ret(), fn2.Ret());
|
||||
}
|
||||
case Value::Kind::StructType:
|
||||
return cast<StructType>(*t1).Name() == cast<StructType>(*t2).Name();
|
||||
case Value::Kind::ChoiceType:
|
||||
return cast<ChoiceType>(*t1).Name() == cast<ChoiceType>(*t2).Name();
|
||||
case Value::Kind::TupleValue: {
|
||||
const auto& tup1 = cast<TupleValue>(*t1);
|
||||
const auto& tup2 = cast<TupleValue>(*t2);
|
||||
if (tup1.Elements().size() != tup2.Elements().size()) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < t1->GetTupleValue().elements.size(); ++i) {
|
||||
if (t1->GetTupleValue().elements[i].name !=
|
||||
t2->GetTupleValue().elements[i].name) {
|
||||
return false;
|
||||
}
|
||||
if (!TypeEqual(t1->GetTupleValue().elements[i].value,
|
||||
t2->GetTupleValue().elements[i].value)) {
|
||||
for (size_t i = 0; i < tup1.Elements().size(); ++i) {
|
||||
if (tup1.Elements()[i].name != tup2.Elements()[i].name ||
|
||||
!TypeEqual(tup1.Elements()[i].value, tup2.Elements()[i].value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case ValKind::IntType:
|
||||
case ValKind::BoolType:
|
||||
case ValKind::ContinuationType:
|
||||
case ValKind::TypeType:
|
||||
case Value::Kind::IntType:
|
||||
case Value::Kind::BoolType:
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::TypeType:
|
||||
return true;
|
||||
case ValKind::VariableType:
|
||||
return t1->GetVariableType().name == t2->GetVariableType().name;
|
||||
case Value::Kind::VariableType:
|
||||
return cast<VariableType>(*t1).Name() == cast<VariableType>(*t2).Name();
|
||||
default:
|
||||
llvm::errs() << "TypeEqual used to compare non-type values\n"
|
||||
<< *t1 << "\n"
|
||||
@@ -578,37 +378,37 @@ static auto FieldsValueEqual(const std::vector<TupleElement>& ts1,
|
||||
//
|
||||
// This function implements the `==` operator of Carbon.
|
||||
auto ValueEqual(const Value* v1, const Value* v2, int line_num) -> bool {
|
||||
if (v1->tag() != v2->tag()) {
|
||||
if (v1->Tag() != v2->Tag()) {
|
||||
return false;
|
||||
}
|
||||
switch (v1->tag()) {
|
||||
case ValKind::IntValue:
|
||||
return v1->GetIntValue() == v2->GetIntValue();
|
||||
case ValKind::BoolValue:
|
||||
return v1->GetBoolValue() == v2->GetBoolValue();
|
||||
case ValKind::PointerValue:
|
||||
return v1->GetPointerValue() == v2->GetPointerValue();
|
||||
case ValKind::FunctionValue:
|
||||
return v1->GetFunctionValue().body == v2->GetFunctionValue().body;
|
||||
case ValKind::TupleValue:
|
||||
return FieldsValueEqual(v1->GetTupleValue().elements,
|
||||
v2->GetTupleValue().elements, line_num);
|
||||
switch (v1->Tag()) {
|
||||
case Value::Kind::IntValue:
|
||||
return cast<IntValue>(*v1).Val() == cast<IntValue>(*v2).Val();
|
||||
case Value::Kind::BoolValue:
|
||||
return cast<BoolValue>(*v1).Val() == cast<BoolValue>(*v2).Val();
|
||||
case Value::Kind::PointerValue:
|
||||
return cast<PointerValue>(*v1).Val() == cast<PointerValue>(*v2).Val();
|
||||
case Value::Kind::FunctionValue:
|
||||
return cast<FunctionValue>(*v1).Body() == cast<FunctionValue>(*v2).Body();
|
||||
case Value::Kind::TupleValue:
|
||||
return FieldsValueEqual(cast<TupleValue>(*v1).Elements(),
|
||||
cast<TupleValue>(*v2).Elements(), line_num);
|
||||
default:
|
||||
case ValKind::IntType:
|
||||
case ValKind::BoolType:
|
||||
case ValKind::TypeType:
|
||||
case ValKind::FunctionType:
|
||||
case ValKind::PointerType:
|
||||
case ValKind::AutoType:
|
||||
case ValKind::StructType:
|
||||
case ValKind::ChoiceType:
|
||||
case ValKind::ContinuationType:
|
||||
case Value::Kind::IntType:
|
||||
case Value::Kind::BoolType:
|
||||
case Value::Kind::TypeType:
|
||||
case Value::Kind::FunctionType:
|
||||
case Value::Kind::PointerType:
|
||||
case Value::Kind::AutoType:
|
||||
case Value::Kind::StructType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::ContinuationType:
|
||||
return TypeEqual(v1, v2);
|
||||
case ValKind::StructValue:
|
||||
case ValKind::AlternativeValue:
|
||||
case ValKind::BindingPlaceholderValue:
|
||||
case ValKind::AlternativeConstructorValue:
|
||||
case ValKind::ContinuationValue:
|
||||
case Value::Kind::StructValue:
|
||||
case Value::Kind::AlternativeValue:
|
||||
case Value::Kind::BindingPlaceholderValue:
|
||||
case Value::Kind::AlternativeConstructorValue:
|
||||
case Value::Kind::ContinuationValue:
|
||||
llvm::errs() << "ValueEqual does not support this kind of value.\n";
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user