aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-10-29 17:38:29 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-11-01 13:12:47 +0000
commitfaa1a005e92237a0188311b48455be88126e3e68 (patch)
tree40a10c0a05c8b3a10b8d52844cddca31bcf5069d /gcc/rust/backend
parent82f3dd40d2dcc9eb2ea261e42bf7bb365faaf0a0 (diff)
downloadgcc-faa1a005e92237a0188311b48455be88126e3e68.zip
gcc-faa1a005e92237a0188311b48455be88126e3e68.tar.gz
gcc-faa1a005e92237a0188311b48455be88126e3e68.tar.bz2
Refactor ADTType to consist of multiple variants
Algebraic data types represent Structs, Tuple Structs, unit structs and enums in rust. The key difference here is that each of these are an ADT with a single variant and enums are an ADT with multiple variants. It adds indirection to where the fields of an ADT are managed. Co-authored-by: Mark Wielaard <mark@klomp.org> Addresses #79
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-context.h9
-rw-r--r--gcc/rust/backend/rust-compile-expr.h18
-rw-r--r--gcc/rust/backend/rust-compile.cc6
3 files changed, 27 insertions, 6 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 2b2018f..551e041 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -431,10 +431,15 @@ public:
if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type))
return;
+ // we dont support enums yet
+ rust_assert (!type.is_enum ());
+ rust_assert (type.number_of_variants () == 1);
+
+ TyTy::VariantDef &variant = *type.get_variants ().at (0);
std::vector<Backend::Btyped_identifier> fields;
- for (size_t i = 0; i < type.num_fields (); i++)
+ for (size_t i = 0; i < variant.num_fields (); i++)
{
- const TyTy::StructFieldType *field = type.get_field (i);
+ const TyTy::StructFieldType *field = variant.get_field_at_index (i);
Btype *compiled_field_ty
= TyTyResolveCompile::compile (ctx, field->get_field_type ());
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h
index 0512373..f43db50 100644
--- a/gcc/rust/backend/rust-compile-expr.h
+++ b/gcc/rust/backend/rust-compile-expr.h
@@ -698,7 +698,13 @@ public:
if (receiver->get_kind () == TyTy::TypeKind::ADT)
{
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (receiver);
- adt->get_field (expr.get_field_name (), &field_index);
+ rust_assert (!adt->is_enum ());
+ rust_assert (adt->number_of_variants () == 1);
+
+ TyTy::VariantDef *variant = adt->get_variants ().at (0);
+ bool ok = variant->lookup_field (expr.get_field_name (), nullptr,
+ &field_index);
+ rust_assert (ok);
}
else if (receiver->get_kind () == TyTy::TypeKind::REF)
{
@@ -707,9 +713,15 @@ public:
rust_assert (b->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (b);
- adt->get_field (expr.get_field_name (), &field_index);
- Btype *adt_tyty = TyTyResolveCompile::compile (ctx, adt);
+ rust_assert (!adt->is_enum ());
+ rust_assert (adt->number_of_variants () == 1);
+ TyTy::VariantDef *variant = adt->get_variants ().at (0);
+ bool ok = variant->lookup_field (expr.get_field_name (), nullptr,
+ &field_index);
+ rust_assert (ok);
+
+ Btype *adt_tyty = TyTyResolveCompile::compile (ctx, adt);
Bexpression *indirect
= ctx->get_backend ()->indirect_expression (adt_tyty, receiver_ref,
true, expr.get_locus ());
diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc
index c8254c1..a5d32b1 100644
--- a/gcc/rust/backend/rust-compile.cc
+++ b/gcc/rust/backend/rust-compile.cc
@@ -73,6 +73,10 @@ CompileExpr::visit (HIR::CallExpr &expr)
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
Btype *compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);
+ rust_assert (!adt->is_enum ());
+ rust_assert (adt->number_of_variants () == 1);
+ auto variant = adt->get_variants ().at (0);
+
// this assumes all fields are in order from type resolution and if a
// base struct was specified those fields are filed via accesors
std::vector<Bexpression *> vals;
@@ -83,7 +87,7 @@ CompileExpr::visit (HIR::CallExpr &expr)
// assignments are coercion sites so lets convert the rvalue if
// necessary
- auto respective_field = adt->get_field (i);
+ auto respective_field = variant->get_field_at_index (i);
auto expected = respective_field->get_field_type ();
TyTy::BaseType *actual = nullptr;