aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/analysis/scope.h
blob: 41bcee61737b5ea5b56eec982c4d8106a61c82a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once

#include "rust-system.h"
#include "rust-ast-full.h"

namespace Rust {
namespace Analysis {

template <class T> class Scope
{
public:
  Scope () : scopeStack () {}

  ~Scope () {}

  bool Insert (std::string key, T val)
  {
    if (scopeStack.back ().find (key) != scopeStack.back ().end ())
      {
	return false;
      }

    scopeStack.back ().insert (std::make_pair (key, std::move (val)));
    return true;
  }

  bool Lookup (std::string key, T *result)
  {
    for (auto it = scopeStack.rbegin (); it != scopeStack.rend (); ++it)
      {
	auto lookup = it->find (key);
	if (lookup != it->end ())
	  {
	    *result = lookup->second;
	    return true;
	  }
      }
    return false;
  }

  void Push () { scopeStack.push_back ({}); }

  std ::map<std::string, T> Pop ()
  {
    auto toplevel = scopeStack.back ();
    scopeStack.pop_back ();
    return toplevel;
  }

  std ::map<std::string, T> Peek () { return scopeStack.back (); }

private:
  std::vector<std::map<std::string, T> > scopeStack;
};

} // namespace Analysis
} // namespace Rust