aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2021-08-15 21:43:19 +0200
committerMark Wielaard <mark@klomp.org>2021-08-15 21:43:19 +0200
commitbb52e25b2d420325ca885c36a00e064e93a23ef4 (patch)
tree2b8d70692116c461fbecbb832ebdf04b31720f72 /gcc/rust
parent52c1cdc9c63baeb090680daf6762c02362f2c6cd (diff)
downloadgcc-bb52e25b2d420325ca885c36a00e064e93a23ef4.zip
gcc-bb52e25b2d420325ca885c36a00e064e93a23ef4.tar.gz
gcc-bb52e25b2d420325ca885c36a00e064e93a23ef4.tar.bz2
Add support for const bool and const float
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.
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: