aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-expr.h
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/rust-compile-expr.h
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/rust-compile-expr.h')
-rw-r--r--gcc/rust/backend/rust-compile-expr.h18
1 files changed, 15 insertions, 3 deletions
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 ());