-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathidentifier.h
77 lines (63 loc) · 1.59 KB
/
identifier.h
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
#pragma once
// (c) Robert Muth - see LICENSE for more info
#include <map>
#include "FE/cwast_gen.h"
#include "Util/assert.h"
namespace cwerg::fe {
class IdGen {
private:
std::map<uint32_t, uint32_t> last_used_seq_;
void RegistereStrAndSeq(const StrAndSeq& ss) {
if (ss.seq > last_used_seq_[ss.name]) {
last_used_seq_[ss.name] = ss.seq;
}
}
public:
Name NameNewNext(uint32_t offset) {
auto it = last_used_seq_.find(offset);
ASSERT(it != last_used_seq_.end(), "");
++it->second;
return NameNew(offset, it->second);
}
Name NameNewNext(Name name) {
const auto& ss = NameStrAndSeq(name);
return NameNewNext(ss.name);
}
Name NameNewNext(std::string_view name) {
uint32_t offset = gNamePool.Intern(name, 1);
return NameNewNext(offset);
}
IdGen() {}
IdGen(Node fun, const SymTab* symtab) {
for (const auto& kv : *symtab) {
RegistereStrAndSeq(kv.first);
}
auto visitor = [this, symtab](Node node, Node parent) {
switch (Node_kind(node)) {
case NT::DefVar:
case NT::Id:
RegistereStrAndSeq(NameStrAndSeq(Node_name(node)));
break;
default:
break;
}
};
VisitNodesRecursivelyPost(fun, visitor, kNodeInvalid);
}
};
class IdGenCache {
private:
std::map<Node, IdGen*> cache_;
public:
IdGenCache() {}
IdGen* Get(Node fun, const SymTab* symtab) {
auto it = cache_.find(fun);
if (it != cache_.end()) {
return it->second;
}
IdGen* id_gen = new IdGen(fun, symtab);
cache_[fun] = id_gen;
return id_gen;
}
};
} // namespace cwerg::fe