forked from open-policy-agent/opa
/
bindings.go
259 lines (229 loc) · 5.51 KB
/
bindings.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"fmt"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/util"
)
type undo struct {
k *ast.Term
u *bindings
next *undo
}
func (u *undo) Undo() {
if u == nil {
// Allow call on zero value of Undo for ease-of-use.
return
}
if u.u == nil {
// Call on empty unifier undos a no-op unify operation.
return
}
u.u.delete(u.k)
u.next.Undo()
}
type bindings struct {
id uint64
values *util.HashMap
instr *Instrumentation
}
func newBindings(id uint64, instr *Instrumentation) *bindings {
eq := func(a, b util.T) bool {
v1, ok1 := a.(*ast.Term)
if ok1 {
v2 := b.(*ast.Term)
return v1.Equal(v2)
}
uv1 := a.(*value)
uv2 := b.(*value)
return uv1.equal(uv2)
}
hash := func(x util.T) int {
v := x.(*ast.Term)
return v.Hash()
}
values := util.NewHashMap(eq, hash)
return &bindings{id, values, instr}
}
func (u *bindings) Iter(caller *bindings, iter func(*ast.Term, *ast.Term) error) error {
var err error
u.values.Iter(func(k, v util.T) bool {
if err != nil {
return true
}
term := k.(*ast.Term)
err = iter(term, u.PlugNamespaced(term, caller))
return false
})
return err
}
func (u *bindings) Plug(a *ast.Term) *ast.Term {
return u.PlugNamespaced(a, nil)
}
func (u *bindings) PlugNamespaced(a *ast.Term, caller *bindings) *ast.Term {
if u != nil {
u.instr.startTimer(evalOpPlug)
defer u.instr.stopTimer(evalOpPlug)
}
return u.plugNamespaced(a, caller)
}
func (u *bindings) plugNamespaced(a *ast.Term, caller *bindings) *ast.Term {
switch v := a.Value.(type) {
case ast.Var:
b, next := u.apply(a)
if a != b || u != next {
return next.plugNamespaced(b, caller)
}
if caller != nil && caller != u {
if name, ok := b.Value.(ast.Var); ok {
return ast.NewTerm(ast.Var(string(name) + fmt.Sprint(u.id)))
}
}
return b
case ast.Array:
cpy := *a
arr := make(ast.Array, len(v))
for i := 0; i < len(arr); i++ {
arr[i] = u.plugNamespaced(v[i], caller)
}
cpy.Value = arr
return &cpy
case ast.Object:
cpy := *a
cpy.Value, _ = v.Map(func(k, v *ast.Term) (*ast.Term, *ast.Term, error) {
return u.plugNamespaced(k, caller), u.plugNamespaced(v, caller), nil
})
return &cpy
case ast.Set:
cpy := *a
cpy.Value, _ = v.Map(func(x *ast.Term) (*ast.Term, error) {
return u.plugNamespaced(x, caller), nil
})
return &cpy
case ast.Ref:
cpy := *a
ref := make(ast.Ref, len(v))
ref[0] = v[0]
for i := 1; i < len(ref); i++ {
ref[i] = u.plugNamespaced(v[i], caller)
}
cpy.Value = ref
return &cpy
// NOTE(tsandall): comprehensions are plugged when they're contained in
// partial evaluation results. If comprehensions are partially evaluated at
// some point, then they will not need to be plugged and these branches can
// go away.
case *ast.ArrayComprehension:
ac := *v
ac.Body = u.plugBody(v.Body, caller)
ac.Term = u.plugNamespaced(v.Term, caller)
cpy := *a
cpy.Value = &ac
return &cpy
case *ast.SetComprehension:
sc := *v
sc.Body = u.plugBody(v.Body, caller)
sc.Term = u.plugNamespaced(v.Term, caller)
cpy := *a
cpy.Value = &sc
return &cpy
case *ast.ObjectComprehension:
oc := *v
oc.Body = u.plugBody(v.Body, caller)
oc.Key = u.plugNamespaced(v.Key, caller)
oc.Value = u.plugNamespaced(v.Value, caller)
cpy := *a
cpy.Value = &oc
return &cpy
}
return a
}
// NOTE(tsandall): see note in plugNamespaced about comprehensions.
func (u *bindings) plugBody(body ast.Body, caller *bindings) ast.Body {
cpy := make(ast.Body, len(body))
for i := range body {
cpy[i] = u.plugExpr(body[i], caller)
}
return cpy
}
func (u *bindings) plugExpr(expr *ast.Expr, caller *bindings) *ast.Expr {
cpy := *expr
switch terms := expr.Terms.(type) {
case *ast.Term:
cpy.Terms = u.plugNamespaced(terms, caller)
case []*ast.Term:
sl := make([]*ast.Term, len(terms))
for i := 0; i < len(sl); i++ {
sl[i] = u.plugNamespaced(terms[i], caller)
}
cpy.Terms = sl
}
return &cpy
}
func (u *bindings) bind(a *ast.Term, b *ast.Term, other *bindings) *undo {
// See note in apply about non-var terms.
_, ok := a.Value.(ast.Var)
if !ok {
panic("illegal value")
}
u.values.Put(a, value{
u: other,
v: b,
})
return &undo{a, u, nil}
}
func (u *bindings) apply(a *ast.Term) (*ast.Term, *bindings) {
// Early exit for non-var terms. Only vars are bound in the binding list,
// so the lookup below will always fail for non-var terms. In some cases,
// the lookup may be expensive as it has to hash the term (which for large
// inputs can be costly.)
_, ok := a.Value.(ast.Var)
if !ok {
return a, u
}
val, ok := u.get(a)
if !ok {
return a, u
}
return val.u.apply(val.v)
}
func (u *bindings) delete(v *ast.Term) {
u.values.Delete(v)
}
func (u *bindings) get(v *ast.Term) (value, bool) {
if u == nil {
return value{}, false
}
r, ok := u.values.Get(v)
if !ok {
return value{}, false
}
return r.(value), true
}
func (u *bindings) String() string {
if u == nil {
return "()"
}
var buf []string
u.values.Iter(func(a, b util.T) bool {
buf = append(buf, fmt.Sprintf("%v: %v", a, b))
return false
})
return fmt.Sprintf("({%v}, %v)", strings.Join(buf, ", "), u.id)
}
type value struct {
u *bindings
v *ast.Term
}
func (v value) String() string {
return fmt.Sprintf("(%v, %d)", v.v, v.u.id)
}
func (v value) equal(other *value) bool {
if v.u == other.u {
return v.v.Equal(other.v)
}
return false
}