Skip to content

Commit d3a4cf9

Browse files
bors[bot]philberty
andauthored
Merge #974
974: Add support for ranges and index lang items along with the TyTy::SliceType r=philberty a=philberty This PR contains more code to begin supporting Slices which requires support for more intrinsic, range and index lang items. More work is needed to support slices such as the const_ptr lang item and the offset intrinsic but this is a big PR already and adds support for more lang items along the way. Fixes #975 Addresses #849 Co-authored-by: Philip Herron <[email protected]>
2 parents bf92a10 + 22c6bca commit d3a4cf9

23 files changed

+1252
-270
lines changed

gcc/rust/Make-lang.in

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ GRS_OBJS = \
9595
rust/rust-hir-type-check-type.o \
9696
rust/rust-hir-type-check-struct.o \
9797
rust/rust-hir-type-check-pattern.o \
98+
rust/rust-hir-type-check-expr.o \
9899
rust/rust-hir-dot-operator.o \
99100
rust/rust-autoderef.o \
100101
rust/rust-substitution-mapper.o \

gcc/rust/backend/rust-compile-expr.cc

+168-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
790790

791791
tree
792792
CompileExpr::resolve_operator_overload (
793-
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExpr &expr,
793+
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr,
794794
tree lhs, tree rhs, HIR::Expr *lhs_expr, HIR::Expr *rhs_expr)
795795
{
796796
TyTy::FnType *fntype;
@@ -1377,5 +1377,172 @@ CompileExpr::visit (HIR::IdentifierExpr &expr)
13771377
}
13781378
}
13791379

1380+
void
1381+
CompileExpr::visit (HIR::RangeFromToExpr &expr)
1382+
{
1383+
tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx);
1384+
tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx);
1385+
if (from == error_mark_node || to == error_mark_node)
1386+
{
1387+
translated = error_mark_node;
1388+
return;
1389+
}
1390+
1391+
TyTy::BaseType *tyty = nullptr;
1392+
bool ok
1393+
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
1394+
rust_assert (ok);
1395+
1396+
tree adt = TyTyResolveCompile::compile (ctx, tyty);
1397+
1398+
// make the constructor
1399+
translated
1400+
= ctx->get_backend ()->constructor_expression (adt, false, {from, to}, -1,
1401+
expr.get_locus ());
1402+
}
1403+
1404+
void
1405+
CompileExpr::visit (HIR::RangeFromExpr &expr)
1406+
{
1407+
tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx);
1408+
if (from == error_mark_node)
1409+
{
1410+
translated = error_mark_node;
1411+
return;
1412+
}
1413+
1414+
TyTy::BaseType *tyty = nullptr;
1415+
bool ok
1416+
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
1417+
rust_assert (ok);
1418+
1419+
tree adt = TyTyResolveCompile::compile (ctx, tyty);
1420+
1421+
// make the constructor
1422+
translated
1423+
= ctx->get_backend ()->constructor_expression (adt, false, {from}, -1,
1424+
expr.get_locus ());
1425+
}
1426+
1427+
void
1428+
CompileExpr::visit (HIR::RangeToExpr &expr)
1429+
{
1430+
tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx);
1431+
if (to == error_mark_node)
1432+
{
1433+
translated = error_mark_node;
1434+
return;
1435+
}
1436+
1437+
TyTy::BaseType *tyty = nullptr;
1438+
bool ok
1439+
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
1440+
rust_assert (ok);
1441+
1442+
tree adt = TyTyResolveCompile::compile (ctx, tyty);
1443+
1444+
// make the constructor
1445+
translated
1446+
= ctx->get_backend ()->constructor_expression (adt, false, {to}, -1,
1447+
expr.get_locus ());
1448+
}
1449+
1450+
void
1451+
CompileExpr::visit (HIR::RangeFullExpr &expr)
1452+
{
1453+
TyTy::BaseType *tyty = nullptr;
1454+
bool ok
1455+
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
1456+
rust_assert (ok);
1457+
1458+
tree adt = TyTyResolveCompile::compile (ctx, tyty);
1459+
translated = ctx->get_backend ()->constructor_expression (adt, false, {}, -1,
1460+
expr.get_locus ());
1461+
}
1462+
1463+
void
1464+
CompileExpr::visit (HIR::RangeFromToInclExpr &expr)
1465+
{
1466+
tree from = CompileExpr::Compile (expr.get_from_expr ().get (), ctx);
1467+
tree to = CompileExpr::Compile (expr.get_to_expr ().get (), ctx);
1468+
if (from == error_mark_node || to == error_mark_node)
1469+
{
1470+
translated = error_mark_node;
1471+
return;
1472+
}
1473+
1474+
TyTy::BaseType *tyty = nullptr;
1475+
bool ok
1476+
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (), &tyty);
1477+
rust_assert (ok);
1478+
1479+
tree adt = TyTyResolveCompile::compile (ctx, tyty);
1480+
1481+
// make the constructor
1482+
translated
1483+
= ctx->get_backend ()->constructor_expression (adt, false, {from, to}, -1,
1484+
expr.get_locus ());
1485+
}
1486+
1487+
void
1488+
CompileExpr::visit (HIR::ArrayIndexExpr &expr)
1489+
{
1490+
tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
1491+
tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);
1492+
1493+
// this might be an core::ops::index lang item situation
1494+
TyTy::FnType *fntype;
1495+
bool is_op_overload = ctx->get_tyctx ()->lookup_operator_overload (
1496+
expr.get_mappings ().get_hirid (), &fntype);
1497+
if (is_op_overload)
1498+
{
1499+
auto lang_item_type = Analysis::RustLangItem::ItemType::INDEX;
1500+
tree operator_overload_call
1501+
= resolve_operator_overload (lang_item_type, expr, array_reference,
1502+
index, expr.get_array_expr (),
1503+
expr.get_index_expr ());
1504+
1505+
// lookup the expected type for this expression
1506+
TyTy::BaseType *tyty = nullptr;
1507+
bool ok
1508+
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
1509+
&tyty);
1510+
rust_assert (ok);
1511+
tree expected_type = TyTyResolveCompile::compile (ctx, tyty);
1512+
1513+
// rust deref always returns a reference from this overload then we can
1514+
// actually do the indirection
1515+
translated
1516+
= ctx->get_backend ()->indirect_expression (expected_type,
1517+
operator_overload_call,
1518+
true, expr.get_locus ());
1519+
return;
1520+
}
1521+
1522+
// lets check if the array is a reference type then we can add an
1523+
// indirection if required
1524+
TyTy::BaseType *array_expr_ty = nullptr;
1525+
bool ok = ctx->get_tyctx ()->lookup_type (
1526+
expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty);
1527+
rust_assert (ok);
1528+
1529+
// do we need to add an indirect reference
1530+
if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
1531+
{
1532+
TyTy::ReferenceType *r
1533+
= static_cast<TyTy::ReferenceType *> (array_expr_ty);
1534+
TyTy::BaseType *tuple_type = r->get_base ();
1535+
tree array_tyty = TyTyResolveCompile::compile (ctx, tuple_type);
1536+
1537+
array_reference
1538+
= ctx->get_backend ()->indirect_expression (array_tyty, array_reference,
1539+
true, expr.get_locus ());
1540+
}
1541+
1542+
translated
1543+
= ctx->get_backend ()->array_index_expression (array_reference, index,
1544+
expr.get_locus ());
1545+
}
1546+
13801547
} // namespace Compile
13811548
} // namespace Rust

gcc/rust/backend/rust-compile-expr.h

+12-31
Original file line numberDiff line numberDiff line change
@@ -201,36 +201,7 @@ class CompileExpr : public HIRCompileBase
201201

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

204-
void visit (HIR::ArrayIndexExpr &expr) override
205-
{
206-
tree array_reference = CompileExpr::Compile (expr.get_array_expr (), ctx);
207-
tree index = CompileExpr::Compile (expr.get_index_expr (), ctx);
208-
209-
// lets check if the array is a reference type then we can add an
210-
// indirection if required
211-
TyTy::BaseType *array_expr_ty = nullptr;
212-
bool ok = ctx->get_tyctx ()->lookup_type (
213-
expr.get_array_expr ()->get_mappings ().get_hirid (), &array_expr_ty);
214-
rust_assert (ok);
215-
216-
// do we need to add an indirect reference
217-
if (array_expr_ty->get_kind () == TyTy::TypeKind::REF)
218-
{
219-
TyTy::ReferenceType *r
220-
= static_cast<TyTy::ReferenceType *> (array_expr_ty);
221-
TyTy::BaseType *tuple_type = r->get_base ();
222-
tree array_tyty = TyTyResolveCompile::compile (ctx, tuple_type);
223-
224-
array_reference
225-
= ctx->get_backend ()->indirect_expression (array_tyty,
226-
array_reference, true,
227-
expr.get_locus ());
228-
}
229-
230-
translated
231-
= ctx->get_backend ()->array_index_expression (array_reference, index,
232-
expr.get_locus ());
233-
}
204+
void visit (HIR::ArrayIndexExpr &expr) override;
234205

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

@@ -803,6 +774,16 @@ class CompileExpr : public HIRCompileBase
803774

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

777+
void visit (HIR::RangeFromToExpr &expr) override;
778+
779+
void visit (HIR::RangeFromExpr &expr) override;
780+
781+
void visit (HIR::RangeToExpr &expr) override;
782+
783+
void visit (HIR::RangeFullExpr &expr) override;
784+
785+
void visit (HIR::RangeFromToInclExpr &expr) override;
786+
806787
protected:
807788
tree compile_dyn_dispatch_call (const TyTy::DynamicObjectType *dyn,
808789
TyTy::BaseType *receiver,
@@ -818,7 +799,7 @@ class CompileExpr : public HIRCompileBase
818799

819800
tree
820801
resolve_operator_overload (Analysis::RustLangItem::ItemType lang_item_type,
821-
HIR::OperatorExpr &expr, tree lhs, tree rhs,
802+
HIR::OperatorExprMeta expr, tree lhs, tree rhs,
822803
HIR::Expr *lhs_expr, HIR::Expr *rhs_expr);
823804

824805
tree compile_bool_literal (const HIR::LiteralExpr &expr,

gcc/rust/backend/rust-compile-type.cc

+7
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
344344
= ctx->get_backend ()->array_type (element_type, folded_capacity_expr);
345345
}
346346

347+
void
348+
TyTyResolveCompile::visit (const TyTy::SliceType &type)
349+
{
350+
// TODO
351+
gcc_unreachable ();
352+
}
353+
347354
void
348355
TyTyResolveCompile::visit (const TyTy::BoolType &type)
349356
{

gcc/rust/backend/rust-compile-type.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TyTyResolveCompile : public TyTy::TyConstVisitor
4343
void visit (const TyTy::FnType &) override;
4444
void visit (const TyTy::FnPtr &) override;
4545
void visit (const TyTy::ArrayType &) override;
46+
void visit (const TyTy::SliceType &) override;
4647
void visit (const TyTy::BoolType &) override;
4748
void visit (const TyTy::IntType &) override;
4849
void visit (const TyTy::UintType &) override;

gcc/rust/backend/rust-compile-tyty.h

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class TyTyCompile : public TyTy::TyVisitor
6262

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

65+
void visit (TyTy::SliceType &) override { gcc_unreachable (); }
66+
6567
void visit (TyTy::ReferenceType &) override { gcc_unreachable (); }
6668

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

gcc/rust/hir/rust-ast-lower-expr.h

+79
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,85 @@ class ASTLoweringExpr : public ASTLoweringBase
728728
expr.get_outer_attrs (), expr.get_locus ());
729729
}
730730

731+
void visit (AST::RangeFromToExpr &expr) override
732+
{
733+
auto crate_num = mappings->get_current_crate ();
734+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
735+
mappings->get_next_hir_id (crate_num),
736+
UNKNOWN_LOCAL_DEFID);
737+
738+
HIR::Expr *range_from
739+
= ASTLoweringExpr::translate (expr.get_from_expr ().get ());
740+
HIR::Expr *range_to
741+
= ASTLoweringExpr::translate (expr.get_to_expr ().get ());
742+
743+
translated
744+
= new HIR::RangeFromToExpr (mapping,
745+
std::unique_ptr<HIR::Expr> (range_from),
746+
std::unique_ptr<HIR::Expr> (range_to),
747+
expr.get_locus ());
748+
}
749+
750+
void visit (AST::RangeFromExpr &expr) override
751+
{
752+
auto crate_num = mappings->get_current_crate ();
753+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
754+
mappings->get_next_hir_id (crate_num),
755+
UNKNOWN_LOCAL_DEFID);
756+
757+
HIR::Expr *range_from
758+
= ASTLoweringExpr::translate (expr.get_from_expr ().get ());
759+
760+
translated
761+
= new HIR::RangeFromExpr (mapping,
762+
std::unique_ptr<HIR::Expr> (range_from),
763+
expr.get_locus ());
764+
}
765+
766+
void visit (AST::RangeToExpr &expr) override
767+
{
768+
auto crate_num = mappings->get_current_crate ();
769+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
770+
mappings->get_next_hir_id (crate_num),
771+
UNKNOWN_LOCAL_DEFID);
772+
773+
HIR::Expr *range_to
774+
= ASTLoweringExpr::translate (expr.get_to_expr ().get ());
775+
776+
translated
777+
= new HIR::RangeToExpr (mapping, std::unique_ptr<HIR::Expr> (range_to),
778+
expr.get_locus ());
779+
}
780+
781+
void visit (AST::RangeFullExpr &expr) override
782+
{
783+
auto crate_num = mappings->get_current_crate ();
784+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
785+
mappings->get_next_hir_id (crate_num),
786+
UNKNOWN_LOCAL_DEFID);
787+
788+
translated = new HIR::RangeFullExpr (mapping, expr.get_locus ());
789+
}
790+
791+
void visit (AST::RangeFromToInclExpr &expr) override
792+
{
793+
auto crate_num = mappings->get_current_crate ();
794+
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
795+
mappings->get_next_hir_id (crate_num),
796+
UNKNOWN_LOCAL_DEFID);
797+
798+
HIR::Expr *range_from
799+
= ASTLoweringExpr::translate (expr.get_from_expr ().get ());
800+
HIR::Expr *range_to
801+
= ASTLoweringExpr::translate (expr.get_to_expr ().get ());
802+
803+
translated
804+
= new HIR::RangeFromToInclExpr (mapping,
805+
std::unique_ptr<HIR::Expr> (range_from),
806+
std::unique_ptr<HIR::Expr> (range_to),
807+
expr.get_locus ());
808+
}
809+
731810
private:
732811
ASTLoweringExpr ()
733812
: ASTLoweringBase (), translated (nullptr),

0 commit comments

Comments
 (0)