-
Notifications
You must be signed in to change notification settings - Fork 60
/
scope.go
378 lines (322 loc) · 8.86 KB
/
scope.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package meta
import (
"bytes"
"encoding/gob"
"fmt"
"io"
"strings"
"github.com/VKCOM/noverify/src/ir"
"github.com/VKCOM/noverify/src/types"
)
var debugScope = false
type VarFlags uint8
const (
// varNoReplace - do not replace variable upon assignment (used for phpdoc @var declaration)
varNoReplace VarFlags = 1 << iota
VarAlwaysDefined
VarImplicit
)
type ScopeVar struct {
Type types.Map
Flags VarFlags
}
func (flags VarFlags) IsNoReplace() bool { return flags&varNoReplace != 0 }
func (flags VarFlags) IsAlwaysDefined() bool { return flags&VarAlwaysDefined != 0 }
func (flags VarFlags) IsImplicit() bool { return flags&VarImplicit != 0 }
func (flags *VarFlags) SetAlwaysDefined(v bool) {
if v {
*flags |= VarAlwaysDefined
} else {
*flags &^= VarAlwaysDefined
}
}
// Scope contains variables with their types in the respective scope
type Scope struct {
vars map[string]*ScopeVar // variables declared in the scope
inInstanceMethod bool
inClosure bool
}
// NewScope creates new empty scope
func NewScope() *Scope {
return &Scope{vars: make(map[string]*ScopeVar)}
}
func (s *Scope) GobWrite(w io.Writer) error {
encoder := gob.NewEncoder(w)
err := encoder.Encode(s.vars)
if err != nil {
return err
}
err = encoder.Encode(s.inInstanceMethod)
if err != nil {
return err
}
err = encoder.Encode(s.inClosure)
if err != nil {
return err
}
return nil
}
// GobEncode is a custom gob marshaller
func (s *Scope) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
if err := s.GobWrite(w); err != nil {
return nil, err
}
return w.Bytes(), nil
}
// GobDecode is custom gob unmarshaller
func (s *Scope) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)
err := decoder.Decode(&s.vars)
if err != nil {
return err
}
err = decoder.Decode(&s.inInstanceMethod)
if err != nil {
return err
}
return decoder.Decode(&s.inClosure)
}
// GobEncode is a custom gob marshaller
func (s *ScopeVar) GobEncode() ([]byte, error) {
w := new(bytes.Buffer)
encoder := gob.NewEncoder(w)
err := encoder.Encode(s.Type)
if err != nil {
return nil, err
}
err = encoder.Encode(s.Flags)
if err != nil {
return nil, err
}
return w.Bytes(), nil
}
// GobDecode is custom gob unmarshaller
func (s *ScopeVar) GobDecode(buf []byte) error {
r := bytes.NewBuffer(buf)
decoder := gob.NewDecoder(r)
err := decoder.Decode(&s.Type)
if err != nil {
return err
}
return decoder.Decode(&s.Flags)
}
// IsInInstanceMethod returns whether or not this scope exists in instance method (and thus closures must capture $this)
func (s *Scope) IsInInstanceMethod() bool {
return s.inInstanceMethod
}
// IsInClosure returns whether or not this scope is inside a closure and thus $this can be late-bound.
func (s *Scope) IsInClosure() bool {
return s.inClosure
}
// SetInInstanceMethod updates "inInstanceMethod" flag that indicated whether or not scope is located inside instance method
// and that "$this" needs to be captured
func (s *Scope) SetInInstanceMethod(v bool) {
s.inInstanceMethod = v
}
// SetInClosure updates "inClosure" flag that indicates whether or not we are inside a closure
// and thus late $this binding is possible.
func (s *Scope) SetInClosure(v bool) {
s.inClosure = v
}
func (s *Scope) Iterate(cb func(varName string, typ types.Map, flags VarFlags)) {
for varName, v := range s.vars {
cb(varName, v.Type, v.Flags)
}
}
func (s *Scope) Len() int {
return len(s.vars)
}
// AddVar adds variable with specified types to scope
func (s *Scope) AddVar(v ir.Node, typ types.Map, reason string, flags VarFlags) {
name, ok := scopeVarName(v)
if !ok {
return
}
s.AddVarName(name, typ, reason, flags)
}
// AddImplicitVar adds implicit variable with specified types to scope
func (s *Scope) AddImplicitVar(varNode ir.Node, typ types.Map, reason string, flags VarFlags) {
name, ok := scopeVarName(varNode)
if !ok {
return
}
s.addVarName(name, typ, reason, flags|VarImplicit)
}
// ReplaceVar replaces variable with specified types to scope
func (s *Scope) ReplaceVar(v ir.Node, typ types.Map, reason string, flags VarFlags) {
name, ok := scopeVarName(v)
if !ok {
return
}
s.ReplaceVarName(name, typ, reason, flags)
}
// DelVar deletes specified variable from scope
func (s *Scope) DelVar(v ir.Node, reason string) {
name, ok := scopeVarName(v)
if !ok {
return
}
s.DelVarName(name, reason)
}
// DelVarName deletes variable from the scope by it's name
func (s *Scope) DelVarName(name, reason string) {
if debugScope {
fmt.Println("unset $" + name + " - " + reason)
}
delete(s.vars, name)
}
// ReplaceVarName replaces variable with specified types to the scope
func (s *Scope) ReplaceVarName(name string, typ types.Map, reason string, flags VarFlags) {
oldVar, ok := s.vars[name]
if ok && oldVar.Flags.IsNoReplace() {
oldVar.Type = oldVar.Type.Append(typ)
return
}
s.vars[name] = &ScopeVar{
Type: typ,
Flags: flags,
}
}
// AddVarName adds variable with specified types to the scope
func (s *Scope) addVarName(name string, typ types.Map, reason string, flags VarFlags) {
v, ok := s.vars[name]
if !ok {
s.vars[name] = &ScopeVar{
Type: typ,
Flags: flags,
}
return
}
if !v.Flags.IsAlwaysDefined() && flags.IsAlwaysDefined() {
v.Flags |= VarAlwaysDefined
}
if !v.Flags.IsNoReplace() && flags.IsNoReplace() {
v.Flags |= varNoReplace
}
v.Type = v.Type.Append(typ)
s.vars[name] = v
}
// AddVarName adds variable with specified types to the scope
func (s *Scope) AddVarName(name string, typ types.Map, reason string, flags VarFlags) {
s.addVarName(name, typ, reason, flags)
}
// AddVarFromPHPDoc adds variable with specified types to the scope
func (s *Scope) AddVarFromPHPDoc(name string, typ types.Map, reason string) {
s.addVarName(name, typ, reason, varNoReplace|VarAlwaysDefined)
}
// HaveVar checks whether or not specified variable is present in the scope
// and that it is always defined
func (s *Scope) HaveVar(v ir.Node) bool {
name, ok := scopeVarName(v)
if !ok {
return false
}
return s.HaveVarName(name)
}
// HaveImplicitVar checks whether or not specified implicit variable is present
// in the scope and that it is always defined
func (s *Scope) HaveImplicitVar(v ir.Node) bool {
name, ok := scopeVarName(v)
if !ok {
return false
}
return s.HaveImplicitVarName(name)
}
// MaybeHaveVar checks that variable is present in the scope (it may be not always defined)
func (s *Scope) MaybeHaveVar(v ir.Node) bool {
name, ok := scopeVarName(v)
if !ok {
return false
}
return s.MaybeHaveVarName(name)
}
// HaveVarName checks whether or not specified variable is present in the scope and that it is always defined
func (s *Scope) HaveVarName(name string) bool {
v, ok := s.vars[name]
if !ok {
return false
}
return v.Flags.IsAlwaysDefined()
}
// HaveImplicitVarName checks whether or not specified implicit variable is present in the scope and that it is always defined
func (s *Scope) HaveImplicitVarName(name string) bool {
v, ok := s.vars[name]
if !ok {
return false
}
return v.Flags.IsImplicit()
}
// GetVarName returns variable if it exists
func (s *Scope) GetVarName(name string) (m *ScopeVar, ok bool) {
res, ok := s.vars[name]
return res, ok
}
// GetVar returns variable if it exists
func (s *Scope) GetVar(v ir.Node) (m *ScopeVar, ok bool) {
name, ok := scopeVarName(v)
if !ok {
return nil, false
}
return s.GetVarName(name)
}
// GetVarType returns type map for variable if it exists
func (s *Scope) GetVarType(v ir.Node) (m types.Map, ok bool) {
name, ok := scopeVarName(v)
if !ok {
return types.Map{}, false
}
return s.GetVarNameType(name)
}
// GetVarNameType returns type map for variable if it exists
func (s *Scope) GetVarNameType(name string) (m types.Map, ok bool) {
res, ok := s.vars[name]
if !ok {
return types.Map{}, false
}
return res.Type, ok
}
// MaybeHaveVarName checks that variable is present in the scope (it may be not always defined)
func (s *Scope) MaybeHaveVarName(name string) bool {
_, ok := s.vars[name]
return ok
}
// String returns vars contents (for debug purposes)
func (s *Scope) String() string {
var res []string
for name, v := range s.vars {
res = append(res, fmt.Sprintf("%s: alwaysDefined=%v, typ=%s", name, v.Flags.IsAlwaysDefined(), v.Type))
}
return strings.Join(res, "\n")
}
// Clone creates a full scope copy (used in branches)
func (s *Scope) Clone() *Scope {
if s == nil {
return NewScope()
}
res := &Scope{vars: make(map[string]*ScopeVar, len(s.vars))}
for k, v := range s.vars {
res.vars[k] = &ScopeVar{
Type: v.Type.Clone(),
Flags: v.Flags,
}
}
res.inInstanceMethod = s.inInstanceMethod
res.inClosure = s.inClosure
return res
}
func scopeVarName(v ir.Node) (string, bool) {
switch v := v.(type) {
case *ir.SimpleVar:
return v.Name, true
case *ir.Var:
vv, ok := v.Expr.(*ir.SimpleVar)
if !ok {
return "", false // Don't go further than 1 level
}
return "$" + vv.Name, true
default:
return "", false
}
}