aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/rust-gcc.cc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-04-20 15:19:33 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-04-20 18:56:53 +0100
commit393b783549e1247dfed5b86f098fb10d1eaf6659 (patch)
tree628921f4b47bc83ab588fa3f7109ddfd7702bfd6 /gcc/rust/rust-gcc.cc
parentee5afc03f94e8a76c1b1b9306849cd932d5d9090 (diff)
downloadgcc-393b783549e1247dfed5b86f098fb10d1eaf6659.zip
gcc-393b783549e1247dfed5b86f098fb10d1eaf6659.tar.gz
gcc-393b783549e1247dfed5b86f098fb10d1eaf6659.tar.bz2
Reuse fold-const.c from GCC to enforce const array capacities
Rust allows for constant eval for cases like array capacity, the initial version of this code forced the programmer to only use literals which was not correct but got the type system off the ground. This now takes advantage of the GCC backend object to offer 3 helpers. 1. check for constant Bexpression* are equal 2. extract a size_t constant value from the Bexpression* 3. to string the Bexpression constant We can get away with the extraction of the value here because we know its going to be a usize for array capacity but some thought is needed if these methods are to be reused in other cases. There is a new ConstFold namespace which should be extensible for const functions later on and const generics. Fixes: #296
Diffstat (limited to 'gcc/rust/rust-gcc.cc')
-rw-r--r--gcc/rust/rust-gcc.cc46
1 files changed, 45 insertions, 1 deletions
diff --git a/gcc/rust/rust-gcc.cc b/gcc/rust/rust-gcc.cc
index b23d141..ed89c94 100644
--- a/gcc/rust/rust-gcc.cc
+++ b/gcc/rust/rust-gcc.cc
@@ -167,7 +167,6 @@ public:
void debug (Blabel *t) { debug_tree (t->get_tree ()); };
// Types.
-
Btype *error_type () { return this->make_type (error_mark_node); }
Btype *void_type () { return this->make_type (void_type_node); }
@@ -176,6 +175,46 @@ public:
Btype *char_type () { return this->make_type (char_type_node); }
+ bool const_size_cast (Bexpression *expr, size_t *result)
+ {
+ rust_assert (TREE_CONSTANT (expr->get_tree ()));
+
+ unsigned char buf[sizeof (size_t) + 1];
+ memset (buf, 0, sizeof (buf));
+
+ int ret = native_encode_expr (expr->get_tree (), buf, sizeof (buf), 0);
+ if (ret <= 0)
+ return false;
+
+ size_t *tmp = (size_t *) buf;
+ *result = *tmp;
+ return true;
+ }
+
+ std::string const_size_val_to_string (Bexpression *expr)
+ {
+ rust_assert (TREE_CONSTANT (expr->get_tree ()));
+
+ unsigned char buf[sizeof (size_t) + 1];
+ memset (buf, 0, sizeof (buf));
+
+ int ret = native_encode_expr (expr->get_tree (), buf, sizeof (buf), 0);
+ rust_assert (ret > 0);
+
+ size_t *ptr = (size_t *) buf;
+ return std::to_string (*ptr);
+ }
+
+ bool const_values_equal (Bexpression *a, Bexpression *b)
+ {
+ return operand_equal_p (a->get_tree (), b->get_tree (),
+ OEP_ONLY_CONST | OEP_PURE_SAME);
+ // printf ("comparing!\n");
+ // debug_tree (a->get_tree ());
+ // debug_tree (b->get_tree ());
+ // printf ("ok = %s\n", ok ? "true" : "false");
+ }
+
Btype *wchar_type ()
{
// i think this is meant to be 32 bit from
@@ -250,6 +289,11 @@ public:
return this->make_expression (error_mark_node);
}
+ bool is_error_expression (Bexpression *expr)
+ {
+ return expr->get_tree () == error_mark_node;
+ }
+
Bexpression *nil_pointer_expression ()
{
return this->make_expression (null_pointer_node);