Skip to content
Merged
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
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ GRS_OBJS = \
rust/rust-hir-type-check-type.o \
rust/rust-hir-type-check-struct.o \
rust/rust-hir-type-check-pattern.o \
rust/rust-hir-type-check-expr.o \
rust/rust-hir-dot-operator.o \
rust/rust-autoderef.o \
rust/rust-substitution-mapper.o \
Expand Down
169 changes: 168 additions & 1 deletion gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,

tree
CompileExpr::resolve_operator_overload (
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExpr &expr,
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr,
tree lhs, tree rhs, HIR::Expr *lhs_expr, HIR::Expr *rhs_expr)
{
TyTy::FnType *fntype;
Expand Down Expand Up @@ -1377,5 +1377,172 @@ CompileExpr::visit (HIR::IdentifierExpr &expr)
}
}

void
CompileExpr::visit (HIR::RangeFromToExpr &expr)
{
tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx);
tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx);
if (from == error_mark_node || to == error_mark_node)
{
translated = error_mark_node;
return;
}

TyTy::BaseType *tyty = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
rust_assert (ok);

tree adt = TyTyResolveCompile::compile (ctx, tyty);

// make the constructor
translated
= ctx->get_backend ()->constructor_expression (adt, false, {from, to}, -1,
expr.get_locus ());
}

void
CompileExpr::visit (HIR::RangeFromExpr &expr)
{
tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx);
if (from == error_mark_node)
{
translated = error_mark_node;
return;
}

TyTy::BaseType *tyty = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
rust_assert (ok);

tree adt = TyTyResolveCompile::compile (ctx, tyty);

// make the constructor
translated
= ctx->get_backend ()->constructor_expression (adt, false, {from}, -1,
expr.get_locus ());
}

void
CompileExpr::visit (HIR::RangeToExpr &expr)
{
tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx);
if (to == error_mark_node)
{
translated = error_mark_node;
return;
}

TyTy::BaseType *tyty = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
rust_assert (ok);

tree adt = TyTyResolveCompile::compile (ctx, tyty);

// make the constructor
translated
= ctx->get_backend ()->constructor_expression (adt, false, {to}, -1,
expr.get_locus ());
}

void
CompileExpr::visit (HIR::RangeFullExpr &expr)
{
TyTy::BaseType *tyty = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
rust_assert (ok);

tree adt = TyTyResolveCompile::compile (ctx, tyty);
translated = ctx->get_backend ()->constructor_expression (adt, false, {}, -1,
expr.get_locus ());
}

void
CompileExpr::visit (HIR::RangeFromToInclExpr &expr)
{
tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx);
tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx);
if (from == error_mark_node || to == error_mark_node)
{
translated = error_mark_node;
return;
}

TyTy::BaseType *tyty = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
rust_assert (ok);

tree adt = TyTyResolveCompile::compile (ctx, tyty);

// make the constructor
translated
= ctx->get_backend ()->constructor_expression (adt, false, {from, to}, -1,
expr.get_locus ());
}

void
CompileExpr::visit (HIR::ArrayIndexExpr &expr)
{
tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);

// this might be an core::ops::index lang item situation
TyTy::FnType *fntype;
bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
expr.get_mappings ().get_hirid (), &fntype);
if (is_op_overload)
{
auto lang_item_type = Analysis::RustLangItem::ItemType::INDEX;
tree operator_overload_call
= resolve_operator_overload (lang_item_type, expr, array_reference,
index, expr.get_array_expr (),
expr.get_index_expr ());

// lookup the expected type for this expression
TyTy::BaseType *tyty = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
&tyty);
rust_assert (ok);
tree expected_type = TyTyResolveCompile::compile (ctx, tyty);

// rust deref always returns a reference from this overload then we can
// actually do the indirection
translated
= ctx->get_backend ()->indirect_expression (expected_type,
operator_overload_call,
true, expr.get_locus ());
return;
}

// lets check if the array is a reference type then we can add an
// indirection if required
TyTy::BaseType *array_expr_ty = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty);
rust_assert (ok);

// do we need to add an indirect reference
if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
{
TyTy::ReferenceType *r
= static_cast<TyTy::ReferenceType *> (array_expr_ty);
TyTy::BaseType *tuple_type = r->get_base ();
tree array_tyty = TyTyResolveCompile::compile (ctx, tuple_type);

array_reference
= ctx->get_backend ()->indirect_expression (array_tyty, array_reference,
true, expr.get_locus ());
}

translated
= ctx->get_backend ()->array_index_expression (array_reference, index,
expr.get_locus ());
}

} // namespace Compile
} // namespace Rust
43 changes: 12 additions & 31 deletions gcc/rust/backend/rust-compile-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,36 +201,7 @@ class CompileExpr : public HIRCompileBase

void visit (HIR::CompoundAssignmentExpr &expr) override;

void visit (HIR::ArrayIndexExpr &expr) override
{
tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);

// lets check if the array is a reference type then we can add an
// indirection if required
TyTy::BaseType *array_expr_ty = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty);
rust_assert (ok);

// do we need to add an indirect reference
if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
{
TyTy::ReferenceType *r
= static_cast<TyTy::ReferenceType *> (array_expr_ty);
TyTy::BaseType *tuple_type = r->get_base ();
tree array_tyty = TyTyResolveCompile::compile (ctx, tuple_type);

array_reference
= ctx->get_backend ()->indirect_expression (array_tyty,
array_reference, true,
expr.get_locus ());
}

translated
= ctx->get_backend ()->array_index_expression (array_reference, index,
expr.get_locus ());
}
void visit (HIR::ArrayIndexExpr &expr) override;

void visit (HIR::ArrayExpr &expr) override;

Expand Down Expand Up @@ -803,6 +774,16 @@ class CompileExpr : public HIRCompileBase

void visit (HIR::MatchExpr &expr) override;

void visit (HIR::RangeFromToExpr &expr) override;

void visit (HIR::RangeFromExpr &expr) override;

void visit (HIR::RangeToExpr &expr) override;

void visit (HIR::RangeFullExpr &expr) override;

void visit (HIR::RangeFromToInclExpr &expr) override;

protected:
tree compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn,
TyTy::BaseType *receiver,
Expand All @@ -818,7 +799,7 @@ class CompileExpr : public HIRCompileBase

tree
resolve_operator_overload (Analysis::RustLangItem::ItemType lang_item_type,
HIR::OperatorExpr &expr, tree lhs, tree rhs,
HIR::OperatorExprMeta expr, tree lhs, tree rhs,
HIR::Expr *lhs_expr, HIR::Expr *rhs_expr);

tree compile_bool_literal (const HIR::LiteralExpr &expr,
Expand Down
7 changes: 7 additions & 0 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
= ctx->get_backend ()->array_type (element_type, folded_capacity_expr);
}

void
TyTyResolveCompile::visit (const TyTy::SliceType &type)
{
// TODO
gcc_unreachable ();
}

void
TyTyResolveCompile::visit (const TyTy::BoolType &type)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/backend/rust-compile-type.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TyTyResolveCompile : public TyTy::TyConstVisitor
void visit (const TyTy::FnType &) override;
void visit (const TyTy::FnPtr &) override;
void visit (const TyTy::ArrayType &) override;
void visit (const TyTy::SliceType &) override;
void visit (const TyTy::BoolType &) override;
void visit (const TyTy::IntType &) override;
void visit (const TyTy::UintType &) override;
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/backend/rust-compile-tyty.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class TyTyCompile : public TyTy::TyVisitor

void visit (TyTy::ArrayType &) override { gcc_unreachable (); }

void visit (TyTy::SliceType &) override { gcc_unreachable (); }

void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }

void visit (TyTy::PointerType &) override { gcc_unreachable (); }
Expand Down
79 changes: 79 additions & 0 deletions gcc/rust/hir/rust-ast-lower-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,85 @@ class ASTLoweringExpr : public ASTLoweringBase
expr.get_outer_attrs (), expr.get_locus ());
}

void visit (AST::RangeFromToExpr &expr) override
{
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

HIR::Expr *range_from
= ASTLoweringExpr::translate (expr.get_from_expr ().get ());
HIR::Expr *range_to
= ASTLoweringExpr::translate (expr.get_to_expr ().get ());

translated
= new HIR::RangeFromToExpr (mapping,
std::unique_ptr<HIR::Expr> (range_from),
std::unique_ptr<HIR::Expr> (range_to),
expr.get_locus ());
}

void visit (AST::RangeFromExpr &expr) override
{
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

HIR::Expr *range_from
= ASTLoweringExpr::translate (expr.get_from_expr ().get ());

translated
= new HIR::RangeFromExpr (mapping,
std::unique_ptr<HIR::Expr> (range_from),
expr.get_locus ());
}

void visit (AST::RangeToExpr &expr) override
{
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

HIR::Expr *range_to
= ASTLoweringExpr::translate (expr.get_to_expr ().get ());

translated
= new HIR::RangeToExpr (mapping, std::unique_ptr<HIR::Expr> (range_to),
expr.get_locus ());
}

void visit (AST::RangeFullExpr &expr) override
{
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

translated = new HIR::RangeFullExpr (mapping, expr.get_locus ());
}

void visit (AST::RangeFromToInclExpr &expr) override
{
auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings->get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

HIR::Expr *range_from
= ASTLoweringExpr::translate (expr.get_from_expr ().get ());
HIR::Expr *range_to
= ASTLoweringExpr::translate (expr.get_to_expr ().get ());

translated
= new HIR::RangeFromToInclExpr (mapping,
std::unique_ptr<HIR::Expr> (range_from),
std::unique_ptr<HIR::Expr> (range_to),
expr.get_locus ());
}

private:
ASTLoweringExpr ()
: ASTLoweringBase (), translated (nullptr),
Expand Down
Loading