diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-01-16 14:24:56 +0000 |
---|---|---|
committer | Philip Herron <herron.philip@googlemail.com> | 2021-01-17 15:58:07 +0000 |
commit | 659ade1611568da5d91a0d6a2d76871cce658ff7 (patch) | |
tree | f860691c1fc461ede8ea6e2334d1f1b9a7f2395f | |
parent | 7066c0eca18f99aca3e88a08b2aaf71d970d0ab6 (diff) | |
download | gcc-659ade1611568da5d91a0d6a2d76871cce658ff7.zip gcc-659ade1611568da5d91a0d6a2d76871cce658ff7.tar.gz gcc-659ade1611568da5d91a0d6a2d76871cce658ff7.tar.bz2 |
Handle forward references in backend compilation
We must ensure the backend can support compilation of things declared
lexically after they are referenced. This scans the toplevel except
functions which are handled as we reference them. To make that generic
enough for all toplevel items will be a move to query based compilation
which still needs planning.
-rw-r--r-- | gcc/rust/backend/rust-compile-item.h | 13 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 7 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/compilable/forward_decl_2.rs | 5 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/compilable/forward_decl_3.rs | 8 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/compilable/forward_decl_4.rs | 8 |
5 files changed, 36 insertions, 5 deletions
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index 8c7eda5..a367ac7 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -31,9 +31,9 @@ namespace Compile { class CompileItem : public HIRCompileBase { public: - static void compile (HIR::Item *item, Context *ctx) + static void compile (HIR::Item *item, Context *ctx, bool compile_fns = true) { - CompileItem compiler (ctx); + CompileItem compiler (ctx, compile_fns); item->accept_vis (compiler); } @@ -116,6 +116,9 @@ public: void visit (HIR::Function &function) { + if (!compile_fns) + return; + // items can be forward compiled which means we may not need to invoke this // code Bfunction *lookup = nullptr; @@ -277,7 +280,11 @@ public: } private: - CompileItem (Context *ctx) : HIRCompileBase (ctx) {} + CompileItem (Context *ctx, bool compile_fns) + : HIRCompileBase (ctx), compile_fns (compile_fns) + {} + + bool compile_fns; }; } // namespace Compile diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index b394289..772d975 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -41,8 +41,11 @@ CompileCrate::Compile (HIR::Crate &crate, Context *ctx) void CompileCrate::go () { - for (auto it = crate.items.begin (); it != crate.items.end (); it++) - CompileItem::compile (it->get (), ctx); + for (auto &item : crate.items) + CompileItem::compile (item.get (), ctx, false); + + for (auto &item : crate.items) + CompileItem::compile (item.get (), ctx, true); } // rust-compile-block.h diff --git a/gcc/testsuite/rust.test/compilable/forward_decl_2.rs b/gcc/testsuite/rust.test/compilable/forward_decl_2.rs new file mode 100644 index 0000000..ba9c963 --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/forward_decl_2.rs @@ -0,0 +1,5 @@ +fn main() { + let y = x + 1; +} + +static x: i32 = 3; diff --git a/gcc/testsuite/rust.test/compilable/forward_decl_3.rs b/gcc/testsuite/rust.test/compilable/forward_decl_3.rs new file mode 100644 index 0000000..257b0f7 --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/forward_decl_3.rs @@ -0,0 +1,8 @@ +fn main() { + let struct_test = Foo { one: 1, two: 2 }; +} + +struct Foo { + one: i32, + two: i32, +} diff --git a/gcc/testsuite/rust.test/compilable/forward_decl_4.rs b/gcc/testsuite/rust.test/compilable/forward_decl_4.rs new file mode 100644 index 0000000..dd8a725 --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/forward_decl_4.rs @@ -0,0 +1,8 @@ +fn main() { + let mut x = TEST_CONST; + x = x + 1; + + let mut y = x + TEST_CONST; +} + +const TEST_CONST: i32 = 10; |