/
static_check_data.go
147 lines (121 loc) · 3.85 KB
/
static_check_data.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
package core
import (
"sync/atomic"
"github.com/inoxlang/inox/internal/parse"
"github.com/inoxlang/inox/internal/utils"
)
// A StaticCheckData is the immutable data produced by statically checking a module.
type StaticCheckData struct {
errors []*StaticCheckError
warnings []*StaticCheckWarning
fnData map[*parse.FunctionExpression]*FunctionStaticData
mappingData map[*parse.MappingExpression]*MappingStaticData
//.errors property accessible from scripts
errorsPropSet atomic.Bool
errorsProp *Tuple
//.warnings property accessible from scripts
warningsPropSet atomic.Bool
warningsProp *Tuple
}
// Errors returns all errors in the code after a static check, the result should not be modified.
func (d *StaticCheckData) Errors() []*StaticCheckError {
return d.errors
}
func (d *StaticCheckData) ErrorTuple() *Tuple {
if d.errorsPropSet.CompareAndSwap(false, true) {
errors := make([]Serializable, len(d.errors))
for i, err := range d.errors {
errors[i] = err.Err()
}
d.errorsProp = NewTuple(errors)
}
return d.errorsProp
}
// Warnings returns all warnings in the code after a static check, the result should not be modified.
func (d *StaticCheckData) Warnings() []*StaticCheckWarning {
return d.warnings
}
func (d *StaticCheckData) WarningTuple() *Tuple {
if d.warningsPropSet.CompareAndSwap(false, true) {
warnings := make([]Serializable, len(d.warnings))
for i, warning := range d.warnings {
warnings[i] = String(warning.LocatedMessage)
}
d.warningsProp = NewTuple(warnings)
}
return d.warningsProp
}
func (d *StaticCheckData) GetGoMethod(name string) (*GoFunction, bool) {
return nil, false
}
func (d *StaticCheckData) Prop(ctx *Context, name string) Value {
switch name {
case "errors":
return d.ErrorTuple()
case "warnings":
return d.WarningTuple()
}
method, ok := d.GetGoMethod(name)
if !ok {
panic(FormatErrPropertyDoesNotExist(name, d))
}
return method
}
func (*StaticCheckData) SetProp(ctx *Context, name string, value Value) error {
return ErrCannotSetProp
}
func (*StaticCheckData) PropertyNames(ctx *Context) []string {
return STATIC_CHECK_DATA_PROP_NAMES
}
type FunctionStaticData struct {
capturedGlobals []string
assignGlobal bool
}
type MappingStaticData struct {
referencedGlobals []string
}
func (data *StaticCheckData) addFnCapturedGlobal(fnExpr *parse.FunctionExpression, name string, optionalInfo *globalVarInfo) {
fnData := data.fnData[fnExpr]
if fnData == nil {
fnData = &FunctionStaticData{}
data.fnData[fnExpr] = fnData
}
if !utils.SliceContains(fnData.capturedGlobals, name) {
fnData.capturedGlobals = append(fnData.capturedGlobals, name)
}
if optionalInfo != nil && optionalInfo.fnExpr != nil {
capturedGlobalFnData := data.GetFnData(optionalInfo.fnExpr)
if capturedGlobalFnData != nil {
for _, name := range capturedGlobalFnData.capturedGlobals {
if utils.SliceContains(fnData.capturedGlobals, name) {
continue
}
fnData.capturedGlobals = append(fnData.capturedGlobals, name)
}
}
}
}
func (data *StaticCheckData) addMappingCapturedGlobal(expr *parse.MappingExpression, name string) {
mappingData := data.mappingData[expr]
if mappingData == nil {
mappingData = &MappingStaticData{}
data.mappingData[expr] = mappingData
}
if !utils.SliceContains(mappingData.referencedGlobals, name) {
mappingData.referencedGlobals = append(mappingData.referencedGlobals, name)
}
}
func (data *StaticCheckData) addFnAssigningGlobal(fnExpr *parse.FunctionExpression) {
fnData := data.fnData[fnExpr]
if fnData == nil {
fnData = &FunctionStaticData{}
data.fnData[fnExpr] = fnData
}
fnData.assignGlobal = true
}
func (data *StaticCheckData) GetFnData(fnExpr *parse.FunctionExpression) *FunctionStaticData {
return data.fnData[fnExpr]
}
func (data *StaticCheckData) GetMappingData(expr *parse.MappingExpression) *MappingStaticData {
return data.mappingData[expr]
}