-
Notifications
You must be signed in to change notification settings - Fork 42
/
type_inference.go
202 lines (178 loc) · 4.25 KB
/
type_inference.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
package ssa4analyze
import (
"github.com/yaklang/yaklang/common/utils"
"github.com/yaklang/yaklang/common/yak/ssa"
)
const TITAG = "TypeInference"
type TypeInference struct {
Finish map[ssa.Value]struct{}
DeleteInst []ssa.Instruction
}
func NewTypeInference(config) Analyzer {
return &TypeInference{
Finish: make(map[ssa.Value]struct{}),
}
}
func (t *TypeInference) Run(prog *ssa.Program) {
prog.EachFunction(func(f *ssa.Function) {
t.RunOnFunction(f)
})
}
func (t *TypeInference) RunOnFunction(fun *ssa.Function) {
t.DeleteInst = make([]ssa.Instruction, 0)
for _, b := range fun.Blocks {
for _, inst := range b.Insts {
t.InferenceOnInstruction(inst)
}
}
for _, inst := range t.DeleteInst {
ssa.DeleteInst(inst)
}
}
func (t *TypeInference) InferenceOnInstruction(inst ssa.Instruction) {
if iv, ok := inst.(ssa.Value); ok {
t := iv.GetType()
if utils.IsNil(t) {
iv.SetType(ssa.BasicTypes[ssa.NullTypeKind])
}
}
switch inst := inst.(type) {
case *ssa.Phi:
// return t.TypeInferencePhi(inst)
case *ssa.UnOp:
case *ssa.BinOp:
t.TypeInferenceBinOp(inst)
case *ssa.Call:
t.TypeInferenceCall(inst)
}
}
func collectTypeFromValues(values []ssa.Value, skip func(int, ssa.Value) bool) []ssa.Type {
typMap := make(map[ssa.Type]struct{})
typs := make([]ssa.Type, 0, len(values))
for index, v := range values {
// skip function
if skip(index, v) {
continue
}
// uniq typ
typ := v.GetType()
if _, ok := typMap[typ]; !ok {
typMap[typ] = struct{}{}
typs = append(typs, typ)
}
}
return typs
}
// if all finish, return false
func (t *TypeInference) checkValuesNotFinish(vs []ssa.Value) bool {
for _, v := range vs {
if _, ok := t.Finish[v]; !ok {
return true
}
}
return false
}
/*
if v.Type !match typ return true
if v.Type match typ return false
*/
func checkType(v ssa.Value, typ ssa.Type) bool {
if v.GetType() == nil {
v.SetType(typ)
return false
}
v.SetType(typ)
return true
}
func (t *TypeInference) TypeInferencePhi(phi *ssa.Phi) {
// check
// TODO: handler Acyclic graph
if t.checkValuesNotFinish(phi.Edge) {
return
}
// set type
typs := collectTypeFromValues(
phi.Edge,
// // skip unreachable block
func(index int, value ssa.Value) bool {
block := phi.GetBlock().Preds[index]
return block.Reachable() == -1
},
)
// only first set type, phi will change
phi.SetType(typs[0])
}
func (t *TypeInference) TypeInferenceBinOp(bin *ssa.BinOp) {
if bin == nil || bin.X == nil || bin.Y == nil {
return
}
XTyps := bin.X.GetType()
YTyps := bin.Y.GetType()
handlerBinOpType := func(x, y ssa.Type) ssa.Type {
if x == nil {
return y
}
if x.GetTypeKind() == y.GetTypeKind() {
return x
}
if x.GetTypeKind() == ssa.AnyTypeKind {
return y
}
if y.GetTypeKind() == ssa.AnyTypeKind {
return x
}
// if y.GetTypeKind() == ssa.Null {
if bin.Op >= ssa.OpGt && bin.Op <= ssa.OpNotEq {
return ssa.BasicTypes[ssa.BooleanTypeKind]
}
// }
return nil
}
retTyp := handlerBinOpType(XTyps, YTyps)
if retTyp == nil {
// bin.NewError(ssa.Error, TITAG, "this expression type error: x[%s] %s y[%s]", XTyps, ssa.BinaryOpcodeName[bin.Op], YTyps)
return
}
// typ := handler
if bin.Op >= ssa.OpGt && bin.Op <= ssa.OpNotEq {
bin.SetType(ssa.BasicTypes[ssa.BooleanTypeKind])
return
} else {
bin.SetType(retTyp)
return
}
}
func (t *TypeInference) TypeInferenceCall(c *ssa.Call) {
// get function type
funcTyp, ok := ssa.ToFunctionType(c.Method.GetType())
if !ok {
return
}
// handle FreeValue
if len(funcTyp.FreeValue) != 0 {
c.HandleFreeValue(funcTyp.FreeValue)
}
// handle ellipsis, unpack argument
if c.IsEllipsis {
// getField := func(object ssa.User, key ssa.Value) *ssa.Field {
// var f *ssa.Field
// if f = ssa.GetField(object, key); f == nil {
// f = ssa.NewFieldOnly(key, object, c.Block)
// ssa.EmitBefore(c, f)
// }
// return f
// }
// obj := c.Args[len(c.Args)-1].(ssa.User)
// num := len(ssa.GetFields(obj))
// if t, ok := obj.GetType().(*ssa.ObjectType); ok {
// if t.Kind == ssa.Slice {
// num = len(t.Key)
// }
// }
// // fields := ssa.GetFields(obj)
// c.Args[len(c.Args)-1] = getField(obj, ssa.NewConst(0))
// for i := 1; i < num; i++ {
// c.Args = append(c.Args, getField(obj, ssa.NewConst(i)))
// }
}
}