summaryrefslogtreecommitdiff
path: root/deps/v8/src/torque/scope.h
blob: 21438da8fe6ec5d11987ebb99e17edf8beaa742d (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TORQUE_SCOPE_H_
#define V8_TORQUE_SCOPE_H_

#include <map>
#include <string>

#include "src/torque/ast.h"
#include "src/torque/types.h"
#include "src/torque/utils.h"

namespace v8 {
namespace internal {
namespace torque {

class ScopeChain;
class Variable;
class Declarable;

class Scope {
 public:
  explicit Scope(ScopeChain& scope_chain);

  void Stream(std::ostream& stream) const {
    stream << "scope " << std::to_string(scope_number_) << " {";
    for (auto& c : lookup_) {
      stream << c.first << ",";
    }
    stream << "}";
  }

  int scope_number() const { return scope_number_; }

  ScopeChain& GetScopeChain() const { return scope_chain_; }

  void AddLiveVariables(std::set<const Variable*>& set);

  void Print();

  class Activator;

 private:
  friend class ScopeChain;

  void CheckAlreadyDeclared(SourcePosition pos, const std::string& name,
                            const char* new_type);

  void Declare(const std::string& name, Declarable* d) {
    DCHECK_EQ(lookup_.end(), lookup_.find(name));
    DCHECK(d != nullptr);
    lookup_[name] = d;
  }

  Declarable* Lookup(const std::string& name) {
    auto i = lookup_.find(name);
    if (i == lookup_.end()) {
      return nullptr;
    }
    return i->second;
  }

  ScopeChain& scope_chain_;
  int scope_number_;
  int private_label_number_;
  std::map<std::string, Declarable*> lookup_;
};

class Scope::Activator {
 public:
  explicit Activator(Scope* scope);
  ~Activator();

 private:
  Scope* scope_;
};

class ScopeChain {
 public:
  ScopeChain() : next_scope_number_(0) {}
  Scope* NewScope();

  Scope* TopScope() const { return current_scopes_.back(); }
  void PushScope(Scope* scope) { current_scopes_.push_back(scope); }
  void PopScope() { current_scopes_.pop_back(); }

  std::set<const Variable*> GetLiveVariables() {
    std::set<const Variable*> result;
    for (auto scope : current_scopes_) {
      scope->AddLiveVariables(result);
    }
    return result;
  }

  void Declare(const std::string& name, Declarable* d) {
    TopScope()->Declare(name, d);
  }

  Declarable* Lookup(const std::string& name) {
    auto e = current_scopes_.rend();
    auto c = current_scopes_.rbegin();
    while (c != e) {
      Declarable* result = (*c)->Lookup(name);
      if (result != nullptr) return result;
      ++c;
    }
    return nullptr;
  }

  Declarable* ShallowLookup(const std::string& name) {
    auto& e = current_scopes_.back();
    return e->Lookup(name);
  }

  Declarable* LookupGlobalScope(const std::string& name) {
    auto& e = current_scopes_.front();
    return e->Lookup(name);
  }

  void Print() {
    for (auto s : current_scopes_) {
      s->Print();
    }
  }

  struct Snapshot {
    ScopeChain* chain;
    std::vector<Scope*> current_scopes;
  };

  Snapshot TaskSnapshot() { return {this, current_scopes_}; }

  class ScopedSnapshotRestorer {
   public:
    explicit ScopedSnapshotRestorer(const Snapshot& snapshot)
        : chain_(snapshot.chain) {
      saved_ = chain_->current_scopes_;
      chain_->current_scopes_ = snapshot.current_scopes;
    }
    ~ScopedSnapshotRestorer() { chain_->current_scopes_ = saved_; }

   private:
    ScopeChain* chain_;
    std::vector<Scope*> saved_;
  };

 private:
  friend class Scope;
  friend class ScopedSnapshotRestorer;

  int GetNextScopeNumber() { return next_scope_number_++; }

  int next_scope_number_;
  std::vector<std::unique_ptr<Scope>> scopes_;
  std::vector<Scope*> current_scopes_;
};

inline std::ostream& operator<<(std::ostream& os, const Scope& scope) {
  scope.Stream(os);
  return os;
}

}  // namespace torque
}  // namespace internal
}  // namespace v8

#endif  // V8_TORQUE_SCOPE_H_