diff options
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 43 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.h | 44 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/torture/byte_str.rs | 4 |
3 files changed, 84 insertions, 7 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index d0c0b74..eb245dc 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -304,8 +304,7 @@ public: } return; - case HIR::Literal::STRING: - case HIR::Literal::BYTE_STRING: { + case HIR::Literal::STRING: { auto base = ctx->get_backend ()->string_constant_expression ( literal_value->as_string ()); translated @@ -313,6 +312,46 @@ public: } return; + case HIR::Literal::BYTE_STRING: { + TyTy::BaseType *tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + expr.get_mappings ().get_hirid (), &tyty)) + { + rust_fatal_error (expr.get_locus (), + "did not resolve type for this array expr"); + return; + } + + // the type here is &[ty; capacity] + rust_assert (tyty->get_kind () == TyTy::TypeKind::REF); + auto ref_tyty = static_cast<TyTy::ReferenceType *> (tyty); + auto base_tyty = ref_tyty->get_base (); + rust_assert (base_tyty->get_kind () == TyTy::TypeKind::ARRAY); + auto array_tyty = static_cast<TyTy::ArrayType *> (base_tyty); + + std::string value_str = expr.get_literal ()->as_string (); + std::vector<Bexpression *> vals; + std::vector<unsigned long> indexes; + for (size_t i = 0; i < value_str.size (); i++) + { + char b = value_str.at (i); + Bexpression *bb + = ctx->get_backend ()->char_constant_expression (b); + vals.push_back (bb); + indexes.push_back (i); + } + + Btype *array_type = TyTyResolveCompile::compile (ctx, array_tyty); + Bexpression *constructed + = ctx->get_backend ()->array_constructor_expression ( + array_type, indexes, vals, expr.get_locus ()); + + translated + = ctx->get_backend ()->address_expression (constructed, + expr.get_locus ()); + } + return; + default: rust_fatal_error (expr.get_locus (), "unknown literal"); return; diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index fe8973a..28b9851 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -609,15 +609,49 @@ public: break; case HIR::Literal::LitType::BYTE_STRING: { - /* We just treat this as a string, but it really is an arraytype of - u8. It isn't in UTF-8, but really just a byte array. */ - TyTy::BaseType *base = nullptr; - auto ok = context->lookup_builtin ("str", &base); + /* This is an arraytype of u8 reference (&[u8;size]). It isn't in + UTF-8, but really just a byte array. Code to construct the array + reference copied from ArrayElemsValues and ArrayType. */ + TyTy::BaseType *u8; + auto ok = context->lookup_builtin ("u8", &u8); rust_assert (ok); + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping capacity_mapping (crate_num, UNKNOWN_NODEID, + mappings->get_next_hir_id ( + crate_num), + UNKNOWN_LOCAL_DEFID); + + /* Capacity is the size of the string (number of chars). + It is a constant, but for fold it to get a Bexpression. */ + std::string capacity_str + = std::to_string (expr.get_literal ()->as_string ().size ()); + HIR::LiteralExpr literal_capacity (capacity_mapping, capacity_str, + HIR::Literal::LitType::INT, + PrimitiveCoreType::CORETYPE_USIZE, + expr.get_locus ()); + + // mark the type for this implicit node + context->insert_type (capacity_mapping, + new TyTy::USizeType ( + capacity_mapping.get_hirid ())); + + Bexpression *capacity + = ConstFold::ConstFoldExpr::fold (&literal_capacity); + + Analysis::NodeMapping array_mapping (crate_num, UNKNOWN_NODEID, + mappings->get_next_hir_id ( + crate_num), + UNKNOWN_LOCAL_DEFID); + + TyTy::ArrayType *array + = new TyTy::ArrayType (array_mapping.get_hirid (), capacity, + TyTy::TyVar (u8->get_ref ())); + context->insert_type (array_mapping, array); + infered = new TyTy::ReferenceType (expr.get_mappings ().get_hirid (), - TyTy::TyVar (base->get_ref ()), false); + TyTy::TyVar (array->get_ref ()), false); } break; diff --git a/gcc/testsuite/rust/compile/torture/byte_str.rs b/gcc/testsuite/rust/compile/torture/byte_str.rs new file mode 100644 index 0000000..28934d2 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/byte_str.rs @@ -0,0 +1,4 @@ +pub fn main() { + let a: &[u8; 4]; + a = b"test"; +} |