aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-08-19 08:56:45 +0000
committerGitHub <noreply@github.com>2021-08-19 08:56:45 +0000
commit24838285ad98f9a91ea73ed50652de2b1c0e7e61 (patch)
tree4170aece28531c74eeaff13c2005c62e3c633059 /gcc/rust
parent3a944e1b7a49dae8c0970f9a491121ab84ff2e0b (diff)
parentbb52e25b2d420325ca885c36a00e064e93a23ef4 (diff)
downloadgcc-24838285ad98f9a91ea73ed50652de2b1c0e7e61.zip
gcc-24838285ad98f9a91ea73ed50652de2b1c0e7e61.tar.gz
gcc-24838285ad98f9a91ea73ed50652de2b1c0e7e61.tar.bz2
Merge #637
637: Add support for const bool and const float r=philberty a=dkm From Mark Wielaard : https://gcc.gnu.org/pipermail/gcc-rust/2021-August/000143.html > Handle BOOL and FLOAT in ConstFoldExpr::visit (HIR::LiteralExpr) to > make it possible to create const bool, f32 and f64 constants. Add a > new testcase "primconsts.rs". Not yet handled are const char and &str > types. Co-authored-by: Mark Wielaard <mark@klomp.org>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/typecheck/rust-hir-const-fold.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h
index f6c6616..8efbb18 100644
--- a/gcc/rust/typecheck/rust-hir-const-fold.h
+++ b/gcc/rust/typecheck/rust-hir-const-fold.h
@@ -315,6 +315,36 @@ public:
}
return;
+ case HIR::Literal::BOOL: {
+ bool bval = literal_value->as_string ().compare ("true") == 0;
+ folded = ctx->get_backend ()->boolean_constant_expression (bval);
+ }
+ return;
+
+ case HIR::Literal::FLOAT: {
+ mpfr_t fval;
+ if (mpfr_init_set_str (fval, literal_value->as_string ().c_str (), 10,
+ MPFR_RNDN)
+ != 0)
+ {
+ rust_fatal_error (expr.get_locus (),
+ "bad floating-point number in literal");
+ return;
+ }
+
+ TyTy::BaseType *tyty = nullptr;
+ if (!tyctx->lookup_type (expr.get_mappings ().get_hirid (), &tyty))
+ {
+ rust_fatal_error (expr.get_locus (),
+ "did not resolve type for this literal expr");
+ return;
+ }
+
+ Btype *type = ConstFoldType::fold (tyty, ctx->get_backend ());
+ folded = ctx->get_backend ()->float_constant_expression (type, fval);
+ }
+ return;
+
/* handle other literals */
default: