mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 12:01:05 +01:00
Perform implicit conversions at run time (#903)
This resolves the "cheating" in #870.
This commit is contained in:
@@ -161,7 +161,8 @@ void Interpreter::InitEnv(const Declaration& d, Env* env) {
|
||||
const auto& var = cast<VariableDeclaration>(d);
|
||||
// Adds an entry in `globals` mapping the variable's name to the
|
||||
// result of evaluating the initializer.
|
||||
auto v = InterpExp(*env, &var.initializer());
|
||||
Nonnull<const Value*> v =
|
||||
Convert(InterpExp(*env, &var.initializer()), &var.static_type());
|
||||
Address a = heap_.AllocateValue(v);
|
||||
env->Set(*var.binding().name(), a);
|
||||
break;
|
||||
@@ -455,6 +456,73 @@ auto Interpreter::StepLvalue() -> Transition {
|
||||
}
|
||||
}
|
||||
|
||||
auto Interpreter::Convert(Nonnull<const Value*> value,
|
||||
Nonnull<const Value*> destination_type) const
|
||||
-> Nonnull<const Value*> {
|
||||
switch (value->kind()) {
|
||||
case Value::Kind::IntValue:
|
||||
case Value::Kind::FunctionValue:
|
||||
case Value::Kind::PointerValue:
|
||||
case Value::Kind::BoolValue:
|
||||
case Value::Kind::NominalClassValue:
|
||||
case Value::Kind::AlternativeValue:
|
||||
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::NominalClassType:
|
||||
case Value::Kind::ChoiceType:
|
||||
case Value::Kind::ContinuationType:
|
||||
case Value::Kind::VariableType:
|
||||
case Value::Kind::BindingPlaceholderValue:
|
||||
case Value::Kind::AlternativeConstructorValue:
|
||||
case Value::Kind::ContinuationValue:
|
||||
case Value::Kind::StringType:
|
||||
case Value::Kind::StringValue:
|
||||
// TODO: add `CHECK(TypeEqual(type, value->dynamic_type()))`, once we
|
||||
// have Value::dynamic_type.
|
||||
return value;
|
||||
case Value::Kind::StructValue: {
|
||||
const auto& struct_val = cast<StructValue>(*value);
|
||||
switch (destination_type->kind()) {
|
||||
case Value::Kind::StructType: {
|
||||
const auto& destination_struct_type =
|
||||
cast<StructType>(*destination_type);
|
||||
std::vector<StructElement> new_elements;
|
||||
for (const auto& [field_name, field_type] :
|
||||
destination_struct_type.fields()) {
|
||||
std::optional<Nonnull<const Value*>> old_value =
|
||||
struct_val.FindField(field_name);
|
||||
new_elements.push_back(
|
||||
{.name = field_name, .value = Convert(*old_value, field_type)});
|
||||
}
|
||||
return arena_->New<StructValue>(std::move(new_elements));
|
||||
}
|
||||
case Value::Kind::NominalClassType:
|
||||
return arena_->New<NominalClassValue>(destination_type, value);
|
||||
default:
|
||||
FATAL() << "Can't convert value " << *value << " to type "
|
||||
<< *destination_type;
|
||||
}
|
||||
}
|
||||
case Value::Kind::TupleValue: {
|
||||
const auto& tuple = cast<TupleValue>(value);
|
||||
const auto& destination_tuple_type = cast<TupleValue>(destination_type);
|
||||
CHECK(tuple->elements().size() ==
|
||||
destination_tuple_type->elements().size());
|
||||
std::vector<Nonnull<const Value*>> new_elements;
|
||||
for (size_t i = 0; i < tuple->elements().size(); ++i) {
|
||||
new_elements.push_back(Convert(tuple->elements()[i],
|
||||
destination_tuple_type->elements()[i]));
|
||||
}
|
||||
return arena_->New<TupleValue>(std::move(new_elements));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto Interpreter::StepExp() -> Transition {
|
||||
Nonnull<Action*> act = stack_.Top()->todo.Top();
|
||||
const Expression& exp = cast<ExpressionAction>(*act).expression();
|
||||
@@ -758,8 +826,10 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
return Done{};
|
||||
}
|
||||
auto c = match_stmt.clauses()[clause_num];
|
||||
std::optional<Env> matches = PatternMatch(
|
||||
&c.pattern().value(), act->results()[0], stmt.source_loc());
|
||||
std::optional<Env> matches =
|
||||
PatternMatch(&c.pattern().value(),
|
||||
Convert(act->results()[0], &c.pattern().static_type()),
|
||||
stmt.source_loc());
|
||||
if (matches) { // We have a match, start the body.
|
||||
// Ensure we don't process any more clauses.
|
||||
act->set_pos(match_stmt.clauses().size() + 1);
|
||||
@@ -781,14 +851,18 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
act->Clear();
|
||||
return Spawn{
|
||||
arena_->New<ExpressionAction>(&cast<While>(stmt).condition())};
|
||||
} else if (cast<BoolValue>(*act->results().back()).value()) {
|
||||
// { {true :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
// -> { { s :: (while (e) s) :: C, E, F } :: S, H}
|
||||
return Spawn{arena_->New<StatementAction>(&cast<While>(stmt).body())};
|
||||
} else {
|
||||
// { {false :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
// -> { { C, E, F } :: S, H}
|
||||
return Done{};
|
||||
Nonnull<const Value*> condition =
|
||||
Convert(act->results().back(), arena_->New<BoolType>());
|
||||
if (cast<BoolValue>(*condition).value()) {
|
||||
// { {true :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
// -> { { s :: (while (e) s) :: C, E, F } :: S, H}
|
||||
return Spawn{arena_->New<StatementAction>(&cast<While>(stmt).body())};
|
||||
} else {
|
||||
// { {false :: (while ([]) s) :: C, E, F} :: S, H}
|
||||
// -> { { C, E, F } :: S, H}
|
||||
return Done{};
|
||||
}
|
||||
}
|
||||
case Statement::Kind::Break: {
|
||||
CHECK(act->pos() == 0);
|
||||
@@ -831,16 +905,17 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
return Done{};
|
||||
}
|
||||
}
|
||||
case Statement::Kind::VariableDefinition:
|
||||
case Statement::Kind::VariableDefinition: {
|
||||
const auto& definition = cast<VariableDefinition>(stmt);
|
||||
if (act->pos() == 0) {
|
||||
// { {(var x = e) :: C, E, F} :: S, H}
|
||||
// -> { {e :: (var x = []) :: C, E, F} :: S, H}
|
||||
return Spawn{arena_->New<ExpressionAction>(
|
||||
&cast<VariableDefinition>(stmt).init())};
|
||||
return Spawn{arena_->New<ExpressionAction>(&definition.init())};
|
||||
} else {
|
||||
// { { v :: (x = []) :: C, E, F} :: S, H}
|
||||
// -> { { C, E(x := a), F} :: S, H(a := copy(v))}
|
||||
Nonnull<const Value*> v = act->results()[0];
|
||||
Nonnull<const Value*> v =
|
||||
Convert(act->results()[0], &definition.pattern().static_type());
|
||||
Nonnull<const Value*> p =
|
||||
&cast<VariableDefinition>(stmt).pattern().value();
|
||||
|
||||
@@ -854,6 +929,7 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
}
|
||||
return Done{};
|
||||
}
|
||||
}
|
||||
case Statement::Kind::ExpressionStatement:
|
||||
if (act->pos() == 0) {
|
||||
// { {e :: C, E, F} :: S, H}
|
||||
@@ -863,43 +939,49 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
} else {
|
||||
return Done{};
|
||||
}
|
||||
case Statement::Kind::Assign:
|
||||
case Statement::Kind::Assign: {
|
||||
const auto& assign = cast<Assign>(stmt);
|
||||
if (act->pos() == 0) {
|
||||
// { {(lv = e) :: C, E, F} :: S, H}
|
||||
// -> { {lv :: ([] = e) :: C, E, F} :: S, H}
|
||||
return Spawn{arena_->New<LValAction>(&cast<Assign>(stmt).lhs())};
|
||||
return Spawn{arena_->New<LValAction>(&assign.lhs())};
|
||||
} else if (act->pos() == 1) {
|
||||
// { { a :: ([] = e) :: C, E, F} :: S, H}
|
||||
// -> { { e :: (a = []) :: C, E, F} :: S, H}
|
||||
return Spawn{arena_->New<ExpressionAction>(&cast<Assign>(stmt).rhs())};
|
||||
return Spawn{arena_->New<ExpressionAction>(&assign.rhs())};
|
||||
} else {
|
||||
// { { v :: (a = []) :: C, E, F} :: S, H}
|
||||
// -> { { C, E, F} :: S, H(a := v)}
|
||||
auto pat = act->results()[0];
|
||||
auto val = act->results()[1];
|
||||
auto val = Convert(act->results()[1], &assign.lhs().static_type());
|
||||
PatternAssignment(pat, val, stmt.source_loc());
|
||||
return Done{};
|
||||
}
|
||||
}
|
||||
case Statement::Kind::If:
|
||||
if (act->pos() == 0) {
|
||||
// { {(if (e) then_stmt else else_stmt) :: C, E, F} :: S, H}
|
||||
// -> { { e :: (if ([]) then_stmt else else_stmt) :: C, E, F} :: S, H}
|
||||
return Spawn{
|
||||
arena_->New<ExpressionAction>(&cast<If>(stmt).condition())};
|
||||
} else if (cast<BoolValue>(*act->results()[0]).value()) {
|
||||
// { {true :: if ([]) then_stmt else else_stmt :: C, E, F} ::
|
||||
// S, H}
|
||||
// -> { { then_stmt :: C, E, F } :: S, H}
|
||||
return Delegate{
|
||||
arena_->New<StatementAction>(&cast<If>(stmt).then_statement())};
|
||||
} else if (cast<If>(stmt).else_statement()) {
|
||||
// { {false :: if ([]) then_stmt else else_stmt :: C, E, F} ::
|
||||
// S, H}
|
||||
// -> { { else_stmt :: C, E, F } :: S, H}
|
||||
return Delegate{
|
||||
arena_->New<StatementAction>(*cast<If>(stmt).else_statement())};
|
||||
} else {
|
||||
return Done{};
|
||||
Nonnull<const Value*> condition =
|
||||
Convert(act->results()[0], arena_->New<BoolType>());
|
||||
if (cast<BoolValue>(*condition).value()) {
|
||||
// { {true :: if ([]) then_stmt else else_stmt :: C, E, F} ::
|
||||
// S, H}
|
||||
// -> { { then_stmt :: C, E, F } :: S, H}
|
||||
return Delegate{
|
||||
arena_->New<StatementAction>(&cast<If>(stmt).then_statement())};
|
||||
} else if (cast<If>(stmt).else_statement()) {
|
||||
// { {false :: if ([]) then_stmt else else_stmt :: C, E, F} ::
|
||||
// S, H}
|
||||
// -> { { else_stmt :: C, E, F } :: S, H}
|
||||
return Delegate{
|
||||
arena_->New<StatementAction>(*cast<If>(stmt).else_statement())};
|
||||
} else {
|
||||
return Done{};
|
||||
}
|
||||
}
|
||||
case Statement::Kind::Return:
|
||||
if (act->pos() == 0) {
|
||||
@@ -910,6 +992,8 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
} else {
|
||||
// { {v :: return [] :: C, E, F} :: {C', E', F'} :: S, H}
|
||||
// -> { {v :: C', E', F'} :: S, H}
|
||||
// TODO(geoffromer): convert the result to the function's return type,
|
||||
// once #880 gives us a way to find that type.
|
||||
return UnwindFunctionCall{act->results()[0]};
|
||||
}
|
||||
case Statement::Kind::Sequence: {
|
||||
@@ -967,8 +1051,10 @@ auto Interpreter::StepStmt() -> Transition {
|
||||
arena_->New<TupleLiteral>(stmt.source_loc())));
|
||||
frame->todo.Push(ignore_result);
|
||||
// Push the continuation onto the current stack_.
|
||||
Nonnull<const Value*> arg =
|
||||
Convert(act->results()[0], arena_->New<ContinuationType>());
|
||||
std::vector<Nonnull<Frame*>>& continuation_vector =
|
||||
cast<ContinuationValue>(*act->results()[0]).stack();
|
||||
cast<ContinuationValue>(*arg).stack();
|
||||
while (!continuation_vector.empty()) {
|
||||
stack_.Push(continuation_vector.back());
|
||||
continuation_vector.pop_back();
|
||||
@@ -1054,8 +1140,11 @@ class Interpreter::DoTransition {
|
||||
|
||||
void operator()(const CallFunction& call) {
|
||||
interpreter->stack_.Top()->todo.Pop();
|
||||
std::optional<Env> matches = interpreter->PatternMatch(
|
||||
&call.function->param_pattern().value(), call.args, call.source_loc);
|
||||
Nonnull<const Value*> converted_args = interpreter->Convert(
|
||||
call.args, &call.function->param_pattern().static_type());
|
||||
std::optional<Env> matches =
|
||||
interpreter->PatternMatch(&call.function->param_pattern().value(),
|
||||
converted_args, call.source_loc);
|
||||
CHECK(matches.has_value())
|
||||
<< "internal error in call_function, pattern match failed";
|
||||
// Create the new frame and push it on the stack
|
||||
|
||||
Reference in New Issue
Block a user