Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split TypeCheckExp into recursive and non-recursive steps #1277

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 101 additions & 23 deletions explorer/interpreter/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,99 @@ auto TypeChecker::SatisfyImpls(
return Success();
}

auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
const ImplScope& impl_scope)
auto TypeChecker::TypeCheckExpOperands(Nonnull<Expression*> e,
const ImplScope& impl_scope)
-> ErrorOr<Success> {
switch (e->kind()) {
case ExpressionKind::IndexExpression: {
auto& index = cast<IndexExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&index.aggregate(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&index.offset(), impl_scope));
break;
}
case ExpressionKind::TupleLiteral:
for (auto* arg : cast<TupleLiteral>(*e).fields()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(arg, impl_scope));
}
break;
case ExpressionKind::StructLiteral:
for (auto& arg : cast<StructLiteral>(*e).fields()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope));
}
break;
case ExpressionKind::StructTypeLiteral:
for (auto& arg : cast<StructTypeLiteral>(*e).fields()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope));
}
break;
case ExpressionKind::FieldAccessExpression:
CARBON_RETURN_IF_ERROR(TypeCheckExp(
&cast<FieldAccessExpression>(*e).aggregate(), impl_scope));
break;
case ExpressionKind::CompoundFieldAccessExpression: {
auto& access = cast<CompoundFieldAccessExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&access.object(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&access.path(), impl_scope));
break;
}
case ExpressionKind::PrimitiveOperatorExpression:
for (Nonnull<Expression*> argument :
cast<PrimitiveOperatorExpression>(*e).arguments()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(argument, impl_scope));
}
break;
case ExpressionKind::CallExpression: {
auto& call = cast<CallExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&call.function(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&call.argument(), impl_scope));
break;
}
case ExpressionKind::FunctionTypeLiteral: {
auto& fn = cast<FunctionTypeLiteral>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&fn.parameter(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&fn.return_type(), impl_scope));
break;
}
case ExpressionKind::IntrinsicExpression:
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&cast<IntrinsicExpression>(*e).args(), impl_scope));
break;
case ExpressionKind::IfExpression: {
auto& if_expr = cast<IfExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&if_expr.condition(), impl_scope));
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&if_expr.then_expression(), impl_scope));
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&if_expr.else_expression(), impl_scope));
break;
}
case ExpressionKind::ArrayTypeLiteral: {
auto& array_literal = cast<ArrayTypeLiteral>(*e);
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&array_literal.element_type_expression(), impl_scope));
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&array_literal.size_expression(), impl_scope));
break;
}
case ExpressionKind::InstantiateImpl:
case ExpressionKind::IdentifierExpression:
case ExpressionKind::IntLiteral:
case ExpressionKind::BoolLiteral:
case ExpressionKind::StringLiteral:
case ExpressionKind::IntTypeLiteral:
case ExpressionKind::BoolTypeLiteral:
case ExpressionKind::StringTypeLiteral:
case ExpressionKind::TypeTypeLiteral:
case ExpressionKind::ContinuationTypeLiteral:
case ExpressionKind::UnimplementedExpression:
break;
}

return Success();
}

auto TypeChecker::TypeCheckOneExp(Nonnull<Expression*> e,
const ImplScope& impl_scope)
-> ErrorOr<Success> {
if (trace_stream_) {
**trace_stream_ << "checking expression " << *e;
Expand All @@ -787,8 +878,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
}
case ExpressionKind::IndexExpression: {
auto& index = cast<IndexExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&index.aggregate(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&index.offset(), impl_scope));
const Value& aggregate_type = index.aggregate().static_type();
switch (aggregate_type.kind()) {
case Value::Kind::TupleValue: {
Expand Down Expand Up @@ -825,7 +914,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
case ExpressionKind::TupleLiteral: {
std::vector<Nonnull<const Value*>> arg_types;
for (auto* arg : cast<TupleLiteral>(*e).fields()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(arg, impl_scope));
CARBON_RETURN_IF_ERROR(
ExpectIsConcreteType(arg->source_loc(), &arg->static_type()));
arg_types.push_back(&arg->static_type());
Expand All @@ -837,7 +925,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
case ExpressionKind::StructLiteral: {
std::vector<NamedValue> arg_types;
for (auto& arg : cast<StructLiteral>(*e).fields()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope));
CARBON_RETURN_IF_ERROR(ExpectIsConcreteType(
arg.expression().source_loc(), &arg.expression().static_type()));
arg_types.push_back({arg.name(), &arg.expression().static_type()});
Expand All @@ -849,7 +936,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
case ExpressionKind::StructTypeLiteral: {
auto& struct_type = cast<StructTypeLiteral>(*e);
for (auto& arg : struct_type.fields()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(&arg.expression(), impl_scope));
CARBON_ASSIGN_OR_RETURN(
auto value, InterpExp(&arg.expression(), arena_, trace_stream_));
CARBON_RETURN_IF_ERROR(
Expand All @@ -869,7 +955,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
}
case ExpressionKind::FieldAccessExpression: {
auto& access = cast<FieldAccessExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&access.aggregate(), impl_scope));
const Value& aggregate_type = access.aggregate().static_type();
switch (aggregate_type.kind()) {
case Value::Kind::StructType: {
Expand Down Expand Up @@ -1097,8 +1182,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
}
case ExpressionKind::CompoundFieldAccessExpression: {
auto& access = cast<CompoundFieldAccessExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&access.object(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&access.path(), impl_scope));
if (!isa<TypeOfMemberName>(access.path().static_type())) {
return CompilationError(e->source_loc())
<< "expected name of instance member or interface member in "
Expand Down Expand Up @@ -1223,7 +1306,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
auto& op = cast<PrimitiveOperatorExpression>(*e);
std::vector<Nonnull<const Value*>> ts;
for (Nonnull<Expression*> argument : op.arguments()) {
CARBON_RETURN_IF_ERROR(TypeCheckExp(argument, impl_scope));
ts.push_back(&argument->static_type());
}
switch (op.op()) {
Expand Down Expand Up @@ -1315,8 +1397,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
}
case ExpressionKind::CallExpression: {
auto& call = cast<CallExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&call.function(), impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckExp(&call.argument(), impl_scope));
switch (call.function().static_type().kind()) {
case Value::Kind::FunctionType: {
const auto& fun_t = cast<FunctionType>(call.function().static_type());
Expand Down Expand Up @@ -1484,7 +1564,6 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
return Success();
case ExpressionKind::IntrinsicExpression: {
auto& intrinsic_exp = cast<IntrinsicExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&intrinsic_exp.args(), impl_scope));
switch (cast<IntrinsicExpression>(*e).intrinsic()) {
case IntrinsicExpression::Intrinsic::Print:
if (intrinsic_exp.args().fields().size() != 1) {
Expand All @@ -1510,16 +1589,11 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
return Success();
case ExpressionKind::IfExpression: {
auto& if_expr = cast<IfExpression>(*e);
CARBON_RETURN_IF_ERROR(TypeCheckExp(&if_expr.condition(), impl_scope));
CARBON_RETURN_IF_ERROR(ExpectType(
if_expr.source_loc(), "condition of `if`", arena_->New<BoolType>(),
&if_expr.condition().static_type()));

// TODO: Compute the common type and convert both operands to it.
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&if_expr.then_expression(), impl_scope));
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&if_expr.else_expression(), impl_scope));
CARBON_RETURN_IF_ERROR(
ExpectExactType(e->source_loc(), "expression of `if` expression",
&if_expr.then_expression().static_type(),
Expand All @@ -1532,17 +1606,13 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
CARBON_FATAL() << "Unimplemented: " << *e;
case ExpressionKind::ArrayTypeLiteral: {
auto& array_literal = cast<ArrayTypeLiteral>(*e);
CARBON_RETURN_IF_ERROR(
TypeCheckExp(&array_literal.element_type_expression(), impl_scope));
CARBON_ASSIGN_OR_RETURN(
Nonnull<const Value*> element_type,
InterpExp(&array_literal.element_type_expression(), arena_,
trace_stream_));
CARBON_RETURN_IF_ERROR(ExpectIsConcreteType(
array_literal.element_type_expression().source_loc(), element_type));

CARBON_RETURN_IF_ERROR(
TypeCheckExp(&array_literal.size_expression(), impl_scope));
CARBON_RETURN_IF_ERROR(
ExpectExactType(array_literal.size_expression().source_loc(),
"array size", arena_->New<IntType>(),
Expand All @@ -1561,6 +1631,14 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
}
}

auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
const ImplScope& impl_scope)
-> ErrorOr<Success> {
CARBON_RETURN_IF_ERROR(TypeCheckExpOperands(e, impl_scope));
CARBON_RETURN_IF_ERROR(TypeCheckOneExp(e, impl_scope));
return Success();
}

void TypeChecker::CollectGenericBindingsInPattern(
Nonnull<const Pattern*> p,
std::vector<Nonnull<const GenericBinding*>>& generic_bindings) {
Expand Down
16 changes: 13 additions & 3 deletions explorer/interpreter/type_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,21 @@ class TypeChecker {
-> std::optional<Nonnull<Expression*>>;

private:
// Type-check the immediate operands of `e`, populating their `static_type`
// and `value_category`. Does not perform type-checking for `e` itself.
// Type-checking the operands of `e` will recursively type-check the AST
// rooted at `e`, except for `e` itself.
auto TypeCheckExpOperands(Nonnull<Expression*> e, const ImplScope& impl_scope)
-> ErrorOr<Success>;

// Type-check the expression `e`, by ensuring it follows Carbon's typing
// rules and computing its `static_type` and `value_category`. Assumes the
// operands of `e` have already been type-checked.
auto TypeCheckOneExp(Nonnull<Expression*> e, const ImplScope& impl_scope)
-> ErrorOr<Success>;

// Traverses the AST rooted at `e`, populating the static_type() of all nodes
// and ensuring they follow Carbon's typing rules.
//
// `values` maps variable names to their compile-time values. It is not
// directly used in this function but is passed to InterpExp.
auto TypeCheckExp(Nonnull<Expression*> e, const ImplScope& impl_scope)
-> ErrorOr<Success>;

Expand Down