aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorPhilip Herron <herron.philip@googlemail.com>2020-05-21 22:37:18 +0100
committerPhilip Herron <philip.herron@embecosm.com>2020-11-28 21:13:15 +0000
commit0840906b7475bdacbcd0655bb1aa9055925aedeb (patch)
tree59ca39744ccef22521b0d42a47d47fbbf82ade78 /gcc/rust
parentb26c1f617c02b9a25f65a34abed58bfded990501 (diff)
downloadgcc-0840906b7475bdacbcd0655bb1aa9055925aedeb.zip
gcc-0840906b7475bdacbcd0655bb1aa9055925aedeb.tar.gz
gcc-0840906b7475bdacbcd0655bb1aa9055925aedeb.tar.bz2
Add a compilation unit scope to unitify how scoping is done in GIMPLE conversion
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/backend/cscope.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/gcc/rust/backend/cscope.h b/gcc/rust/backend/cscope.h
new file mode 100644
index 0000000..0cc0694
--- /dev/null
+++ b/gcc/rust/backend/cscope.h
@@ -0,0 +1,105 @@
+#pragma once
+
+#include "rust-system.h"
+#include "rust-backend.h"
+#include "scope.h"
+
+namespace Rust {
+namespace Compile {
+
+class Scope
+{
+public:
+ Scope (Backend *backend) : backend (backend) {}
+
+ ~Scope () {}
+
+ void Push ()
+ {
+ fndecls.Push ();
+ vars.Push ();
+ types.Push ();
+ }
+
+ void Pop ()
+ {
+ fndecls.Pop ();
+ vars.Pop ();
+ types.Pop ();
+ }
+
+ void PushCurrentFunction (std::string name, Bfunction *fn)
+ {
+ InsertFunction (name, fn);
+ fns.push_back (fn);
+ }
+
+ Bfunction *PopCurrentFunction ()
+ {
+ auto ret = fns.back ();
+ fns.pop_back ();
+ return ret;
+ }
+
+ Bfunction *GetCurrentFndecl () { return fns.back (); }
+
+ void PushBlock (Bblock *block)
+ {
+ blocks.push_back (block);
+ std::vector<Bstatement *> empty;
+ context.push_back (empty);
+ }
+
+ Bblock *PopBlock ()
+ {
+ auto ret = blocks.back ();
+ blocks.pop_back ();
+
+ auto stmts = context.back ();
+ context.pop_back ();
+
+ backend->block_add_statements (ret, stmts);
+
+ return ret;
+ }
+
+ void AddStatement (Bstatement *stmt) { context.back ().push_back (stmt); }
+
+ void InsertFunction (std::string name, Bfunction *fn)
+ {
+ fndecls.Insert (name, fn);
+ }
+
+ bool LookupFunction (std::string name, Bfunction **fn)
+ {
+ return fndecls.Lookup (name, fn);
+ }
+
+ void InsertType (std::string name, Btype *type) { types.Insert (name, type); }
+
+ bool LookupType (std::string name, Btype **type)
+ {
+ return types.Lookup (name, type);
+ }
+
+ void InsertVar (std::string name, Bvariable *var) { vars.Insert (name, var); }
+
+ bool LookupVar (std::string name, Bvariable **var)
+ {
+ return vars.Lookup (name, var);
+ }
+
+private:
+ Backend *backend;
+
+ ::std::vector<Bfunction *> fns;
+ ::std::vector<Bblock *> blocks;
+ ::std::vector< ::std::vector<Bstatement *> > context;
+
+ Analysis::Scope<Bfunction *> fndecls;
+ Analysis::Scope<Bvariable *> vars;
+ Analysis::Scope<Btype *> types;
+};
+
+} // namespace Compile
+} // namespace Rust