-
Notifications
You must be signed in to change notification settings - Fork 12
/
id.go
57 lines (54 loc) · 998 Bytes
/
id.go
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
package ir
func Tag(o Object) {
var nextID int
if pkg, ok := o.(*Package); ok {
for _, v := range pkg.Scope().m {
tag(v, &nextID)
}
} else {
tag(o, &nextID)
}
}
func tag(o Object, nextID *int) {
switch t := o.(type) {
case *Assignment:
tag(t.Rhs, nextID)
case *Binary:
setID(t, nextID)
tag(t.Lhs, nextID)
tag(t.Rhs, nextID)
case *Block:
for _, e := range t.Exprs {
tag(e, nextID)
}
case *Call:
for _, arg := range t.Args {
tag(arg, nextID)
}
case *Declaration:
for _, p := range t.Params {
setID(t.Scope().Lookup(p).(IDer), nextID)
}
tag(t.Body, nextID)
case *If:
setID(t, nextID)
tag(t.Cond, nextID)
tag(t.Then, nextID)
if t.Else != nil {
tag(t.Else, nextID)
}
case *Param:
setID(t.Scope().Lookup(t.Name()).(IDer), nextID)
case *Unary:
tag(t.Rhs, nextID)
case *Variable:
setID(t.Scope().Lookup(t.Name()).(IDer), nextID)
}
return
}
func setID(o IDer, nextID *int) {
if o.ID() == 0 {
o.SetID(*nextID)
*nextID++
}
}