aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-01-25 17:03:36 +0000
committerPhilip Herron <herron.philip@googlemail.com>2021-01-27 10:42:25 +0000
commit8578c61be5061fab91fe679a15fd68ab5fad987c (patch)
treef09524d5f8bf17fab00618ef7e8e98fd905a557e /gcc/rust/backend
parent854aad3b58e747cad3e46b522c9ef765bdfadca4 (diff)
downloadgcc-8578c61be5061fab91fe679a15fd68ab5fad987c.zip
gcc-8578c61be5061fab91fe679a15fd68ab5fad987c.tar.gz
gcc-8578c61be5061fab91fe679a15fd68ab5fad987c.tar.bz2
Add mutablity checks and left hand size assignee checker
In order to assign to a name we must ensure the LHS is a valid expression to assign to. This leads onto actually checking if this is a mutable declaration or not. Once these checks pass the name resolver we can in GIMPLE create immutable types for these declarations to help with optimization. Fixes #77
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r--gcc/rust/backend/rust-compile-fnparam.h67
-rw-r--r--gcc/rust/backend/rust-compile-item.h18
-rw-r--r--gcc/rust/backend/rust-compile-var-decl.h3
3 files changed, 81 insertions, 7 deletions
diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h
new file mode 100644
index 0000000..cf6e6f7
--- /dev/null
+++ b/gcc/rust/backend/rust-compile-fnparam.h
@@ -0,0 +1,67 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_COMPILE_FNPARAM
+#define RUST_COMPILE_FNPARAM
+
+#include "rust-compile-base.h"
+
+namespace Rust {
+namespace Compile {
+
+class CompileFnParam : public HIRCompileBase
+{
+public:
+ static Bvariable *compile (Context *ctx, Bfunction *fndecl,
+ HIR::FunctionParam *param, Btype *decl_type,
+ Location locus)
+ {
+ CompileFnParam compiler (ctx, fndecl, decl_type, locus);
+ param->get_param_name ()->accept_vis (compiler);
+ return compiler.translated;
+ }
+
+ void visit (HIR::IdentifierPattern &pattern)
+ {
+ if (!pattern.is_mut)
+ decl_type = ctx->get_backend ()->immutable_type (decl_type);
+
+ translated
+ = ctx->get_backend ()->parameter_variable (fndecl, pattern.variable_ident,
+ decl_type,
+ false /* address_taken */,
+ locus);
+ }
+
+private:
+ CompileFnParam (Context *ctx, ::Bfunction *fndecl, ::Btype *decl_type,
+ Location locus)
+ : HIRCompileBase (ctx), fndecl (fndecl), decl_type (decl_type),
+ locus (locus), translated (nullptr)
+ {}
+
+ ::Bfunction *fndecl;
+ ::Btype *decl_type;
+ Location locus;
+ ::Bvariable *translated;
+};
+
+} // namespace Compile
+} // namespace Rust
+
+#endif // RUST_COMPILE_FNPARAM
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index bf899d8..1bb7c91 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -24,6 +24,7 @@
#include "rust-compile-var-decl.h"
#include "rust-compile-stmt.h"
#include "rust-compile-expr.h"
+#include "rust-compile-fnparam.h"
namespace Rust {
namespace Compile {
@@ -174,17 +175,20 @@ public:
for (auto &it : fntype->get_params ())
{
HIR::FunctionParam &referenced_param = function.function_params.at (i);
- auto param_pattern = it.first;
auto param_tyty = it.second;
-
auto compiled_param_type
= TyTyResolveCompile::compile (ctx, param_tyty);
- bool tree_addressable = false;
- auto compiled_param_var = ctx->get_backend ()->parameter_variable (
- fndecl, param_pattern->as_string (), compiled_param_type,
- tree_addressable,
- ctx->get_mappings ()->lookup_location (param_tyty->get_ref ()));
+ Location param_locus
+ = ctx->get_mappings ()->lookup_location (param_tyty->get_ref ());
+ Bvariable *compiled_param_var
+ = CompileFnParam::compile (ctx, fndecl, &referenced_param,
+ compiled_param_type, param_locus);
+ if (compiled_param_var == nullptr)
+ {
+ rust_error_at (param_locus, "failed to compile parameter variable");
+ return;
+ }
param_vars.push_back (compiled_param_var);
diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h
index cf73820..06ea5a9 100644
--- a/gcc/rust/backend/rust-compile-var-decl.h
+++ b/gcc/rust/backend/rust-compile-var-decl.h
@@ -54,6 +54,9 @@ public:
void visit (HIR::IdentifierPattern &pattern)
{
+ if (!pattern.is_mut)
+ translated_type = ctx->get_backend ()->immutable_type (translated_type);
+
translated
= ctx->get_backend ()->local_variable (fndecl, pattern.variable_ident,
translated_type, NULL /*decl_var*/,