aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile-expr.cc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-05-25 11:48:54 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-05-25 13:38:17 +0100
commit4cfd6942c07021db05beae40e1128901e37109e6 (patch)
tree0c7a22bebb3e15b7ffbc54850225aaa1732b2fa6 /gcc/rust/backend/rust-compile-expr.cc
parent79d977e759b073a0a4d422b9a1032427ddc05295 (diff)
downloadgcc-4cfd6942c07021db05beae40e1128901e37109e6.zip
gcc-4cfd6942c07021db05beae40e1128901e37109e6.tar.gz
gcc-4cfd6942c07021db05beae40e1128901e37109e6.tar.bz2
Str's have the same layout as [T]
Raw strings have a very specific type layout which maps over to Slices. It also has very specific type checking rules so for example: let a:&str = "TEST 1"; let b:&str = &"TEST 2"; Are both the same type this is likely to be for all DST's but lets do one rule at a time. Fixes #1023 #1271
Diffstat (limited to 'gcc/rust/backend/rust-compile-expr.cc')
-rw-r--r--gcc/rust/backend/rust-compile-expr.cc19
1 files changed, 17 insertions, 2 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc
index b176ed2..4168bb8 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -1014,13 +1014,28 @@ tree
CompileExpr::compile_string_literal (const HIR::LiteralExpr &expr,
const TyTy::BaseType *tyty)
{
+ tree fat_pointer = TyTyResolveCompile::compile (ctx, tyty);
+
rust_assert (expr.get_lit_type () == HIR::Literal::STRING);
const auto literal_value = expr.get_literal ();
auto base = ctx->get_backend ()->string_constant_expression (
literal_value.as_string ());
- return address_expression (base, build_pointer_type (TREE_TYPE (base)),
- expr.get_locus ());
+ tree data = address_expression (base, build_pointer_type (TREE_TYPE (base)),
+ expr.get_locus ());
+
+ TyTy::BaseType *usize = nullptr;
+ bool ok = ctx->get_tyctx ()->lookup_builtin ("usize", &usize);
+ rust_assert (ok);
+ tree type = TyTyResolveCompile::compile (ctx, usize);
+
+ mpz_t ival;
+ mpz_init_set_ui (ival, literal_value.as_string ().size ());
+ tree size = double_int_to_tree (type, mpz_get_double_int (type, ival, true));
+
+ return ctx->get_backend ()->constructor_expression (fat_pointer, false,
+ {data, size}, -1,
+ expr.get_locus ());
}
tree