Skip to content

Add code generation for the slice type #1015

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

Merged
merged 1 commit into from
Mar 11, 2022
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
34 changes: 32 additions & 2 deletions gcc/rust/backend/rust-compile-type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,38 @@ TyTyResolveCompile::visit (const TyTy::ArrayType &type)
void
TyTyResolveCompile::visit (const TyTy::SliceType &type)
{
// TODO
gcc_unreachable ();
if (ctx->lookup_compiled_types (type.get_ty_ref (), &translated, &type))
return;

std::vector<Backend::typed_identifier> fields;

tree element_type
= TyTyResolveCompile::compile (ctx, type.get_element_type ());
tree data_field_ty = build_pointer_type (element_type);
Backend::typed_identifier data_field ("data", data_field_ty, Location ());
fields.push_back (std::move (data_field));

// lookup usize
TyTy::BaseType *usize = nullptr;
bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
rust_assert (ok);

tree len_field_ty = TyTyResolveCompile::compile (ctx, usize);
Backend::typed_identifier len_field ("len", len_field_ty, Location ());
fields.push_back (std::move (len_field));

tree type_record = ctx->get_backend ()->struct_type (fields);

std::string named_struct_str
= std::string ("[") + type.get_element_type ()->get_name () + "]";
tree named_struct
= ctx->get_backend ()->named_type (named_struct_str, type_record,
type.get_ident ().locus);

ctx->push_type (named_struct);
translated = named_struct;

ctx->insert_compiled_type (type.get_ty_ref (), named_struct, &type);
}

void
Expand Down
27 changes: 27 additions & 0 deletions gcc/testsuite/rust/execute/torture/slice1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// { dg-additional-options "-w" }
struct FatPtr<T> {
data: *const T,
len: usize,
}

union Repr<T> {
rust: *const [T],
rust_mut: *mut [T],
raw: FatPtr<T>,
}

const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
unsafe {
let a = FatPtr { data, len };
let b = Repr { raw: a };
b.rust
}
}

fn main() -> i32 {
let a = 123;
let b: *const i32 = &a;
let c = slice_from_raw_parts(b, 1);

0
}