-
Notifications
You must be signed in to change notification settings - Fork 2
/
trace_build.go
423 lines (369 loc) · 9.54 KB
/
trace_build.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"go/ast"
"go/token"
"strconv"
"strings"
)
func (p *ExecTrace) addTransition(su *StateUpdate) {
mt := MethodTransition{}
if conds := p.nearestCond(); conds != nil {
mt.Condition = conds.buildCondition()
}
if p.migration != nil {
mt.Migration = p.getInlineFuncExpr(p.migration, Execution)
} else {
mt.InheritMigration = true
}
switch p.md.MType {
case DeclarationInit, Construction:
mt.Transition = su.fullName()
case 0:
fallthrough
default:
if !p.addContextOpTransition(su, &mt) {
if mt.Transition == "" {
return
}
mt.Transition = "<unknown>"
}
}
p.md.AddTransition(mt)
}
func (p *ExecTrace) addContextOpTransition(su *StateUpdate, mt *MethodTransition) bool {
switch su.name {
case "CallSubroutine":
if len(su.args) != 3 {
return false
}
// CallSubroutine(SubroutineStateMachine, MigrateFunc, SubroutineExitFunc) StateUpdate
//
// this step -> SubroutineSM -> SubroutineExitFunc
mt.Operation = "CallSubroutine"
if mh := p.getInlineFuncExpr(su.args[1], Migration); mh != "" {
mt.Migration = mh
mt.InheritMigration = false
}
mt.Transition = p.md.Name + `.` + p.getInlineFuncExpr(su.args[0], 0) + `.` + strconv.Itoa(len(p.md.SubSteps)+1)
mds := p.buildSubStep(mt.Transition, nil, 0)
mds.AddMigration(mt.Migration)
mds.IsSubroutine = true
exitStep := p.getInlineFuncExpr(su.args[2], Execution) // net exactly an execution, but ok
mds.AddTransition(MethodTransition{
Transition: exitStep,
})
mt.Migration = "" // Migration from CallSubroutine is not applied to the caller
mt.HiddenPropagate = exitStep // all settings applied to the next step after SM return
return true
case "Error", "Errorf":
mt.Operation = "Error"
mt.Transition = "<stop>"
mt.InheritMigration = false
return true
case "Stay":
mt.Operation = ""
return false
case "Stop":
mt.Transition = "<stop>"
mt.InheritMigration = false
return true
case "Replace":
mt.Operation = "Replace"
mt.Transition = p.getInlineFuncExpr(su.args[0], Construction)
mt.InheritMigration = false
return mt.Transition != ""
case "ReplaceWith":
mt.Operation = "Replace"
mt.Transition = p.getInlineFuncExpr(su.args[0], 0)
mt.InheritMigration = false
return mt.Transition != ""
case "ThenRepeatOrElse":
// unsupported - to be removed
mt.Operation, mt.DelayedStart = p.buildOperation(su.parent)
mt.Transition = "<ThenRepeatOrElse>"
return false
case "ThenRepeatOrJump":
mt.Operation, mt.DelayedStart = p.buildOperation(su.parent)
if len(su.args) == 0 {
return false
}
p.md.AddTransition(*mt) // adds a repeat transition, because mt.Transition is empty
mt.Transition = p.getInlineFuncExpr(su.args[0], Execution)
return mt.Transition != ""
case "ThenRepeatOrJumpExt":
mt.Operation, mt.DelayedStart = p.buildOperation(su.parent)
p.md.AddTransition(*mt) // adds a repeat transition, because mt.Transition is empty
case "Repeat":
if len(su.args) == 0 {
return false
}
mt.Operation = `Repeat(` + p.shortenArgs(su.args, maxArgLen) + `)`
return true
case "RestoreStep":
mt.WaitTransition = true
mt.Operation = su.name
default:
if strings.HasPrefix(su.name, "Then") {
switch op := su.parent.name; {
case op == "Sleep":
mt.WaitTransition = true
case strings.HasPrefix(op, "Wait"):
mt.WaitTransition = true
}
mt.Operation, mt.DelayedStart = p.buildOperation(su.parent)
}
if strings.HasSuffix(su.name, "Ext") {
break
}
if len(su.args) == 0 { // repeat and similar ops
return true
}
mt.Transition = p.getInlineFuncExpr(su.args[0], Execution)
return mt.Transition != ""
}
if len(su.args) == 0 {
return false
}
mh := ""
mt.Transition, mh = p.getSlotStepExpr(su.args[0])
if mh != "" {
mt.Migration = mh
mt.InheritMigration = false
}
return mt.Transition != ""
}
func (p *ExecTrace) getSlotStepExpr(expr ast.Expr) (transition, migration string) {
switch op := expr.(type) {
case *ast.CompositeLit:
if _, sel := getSelectorOfExpr(op.Type); sel != "SlotStep" {
break
}
for _, el := range op.Elts {
if kve, ok := el.(*ast.KeyValueExpr); ok {
switch xkey, key := getSelectorOfExpr(kve.Key); {
case xkey != "":
case key == "Transition":
transition = p.getInlineFuncExpr(kve.Value, Execution)
case key == "Migration":
migration = p.getInlineFuncExpr(kve.Value, Migration)
}
}
}
return
}
_, sel := getSelectorOfExpr(expr)
return "DYNAMIC " + sel, ""
}
func (p *ExecTrace) buildSubStep(name string, args *ast.FieldList, mType MethodType) *MethodDecl {
md := &MethodDecl{
SM: p.md.SM,
RType: p.md.RType,
RName: p.md.RName,
Name: name,
MType: mType,
}
if mType.HasContextArg() && args != nil {
if mt, argName := p.fs.findContextArg(args.List); mt == mType {
md.CtxArg = argName
}
}
if mType.HasStateUpdate() {
md.UpdateIdx = 1
}
p.md.SubSteps = append(p.md.SubSteps, md)
return md
}
func (p *ExecTrace) getInlineFuncExpr(expr ast.Expr, mType MethodType) string {
switch op := expr.(type) {
case *ast.UnaryExpr:
if mType.HasStateUpdate() {
break
}
if op.Op == token.AND {
return p.getInlineFuncExpr(op.X, mType)
}
case *ast.CompositeLit:
if mType != 0 {
break
}
x, sel := getSelectorOfExpr(op.Type)
if x != "" {
sel = x + `.` + sel + `{}`
} else {
sel += `{}`
}
mds := p.buildSubStep(sel, nil, mType)
mds.IsSubroutine = true
return sel
case *ast.FuncLit:
funcName := p.md.Name + `.` + strconv.Itoa(len(p.md.SubSteps)+1)
mds := p.buildSubStep(funcName, op.Type.Params, mType)
mds.parseFuncBody(op.Body, p.fs)
return funcName
}
switch x, sel := getSelectorOfExpr(expr); {
case x != "":
return x + `.` + sel
case sel != "nil":
return sel
}
return ""
}
func (p *ExecTrace) buildCondition() string {
s := ""
maxLen := maxCondLen
if len(p.conds) == 1 {
s = p.shortenCond(p.conds[0], maxLen)
} else {
b := strings.Builder{}
b.WriteString(p.shortenCond(p.conds[0], maxLen))
for _, c := range p.conds[1:] {
if max := maxLen - b.Len(); max > 3 {
cs := p.shortenCond(c, max)
if len(cs) < max+3 {
b.WriteString(cs)
continue
}
}
b.WriteString("...")
break
}
s = b.String()
}
s = strconv.Quote(s)
s = `[` + s[1:len(s)-1] + `]`
if p.inverted {
return `!` + s
}
return s
}
func (p *ExecTrace) shortenArgs(args []ast.Expr, maxLen int) string {
if len(args) == 0 {
return ""
}
return p.shortenCond(args[0], maxLen)
}
func (p *ExecTrace) shortenCond(cond ast.Expr, maxLen int) string {
s := p._shortenCond(cond, maxLen)
if s != "" {
return s
}
return p.fs.Excerpt(cond.Pos(), cond.End(), maxLen)
}
func (p *ExecTrace) _shortenCond(cond ast.Expr, maxLen int) string {
switch op := cond.(type) {
case *ast.SelectorExpr:
if op.Sel == nil {
return p._shortenCond(op.X, maxLen)
}
s := op.Sel.Name
if len(s) >= maxLen {
if op.X != nil {
return `(...).` + s
}
return s
}
return p._shortenCond(op.X, maxLen-len(s)-1) + `.` + s
case *ast.Ident:
return op.Name
case *ast.CallExpr:
return p._shortenCond(op.Fun, maxLen-2) + `()`
case *ast.BasicLit:
return op.Value
case *ast.FuncLit:
return "func(){}"
case *ast.CompositeLit:
return p._shortenCond(op.Type, maxLen-2) + `{}`
case *ast.ParenExpr:
return `(` + p._shortenCond(op.X, maxLen-2) + `)`
case *ast.IndexExpr:
return p._shortenCond(op.X, maxLen-2) + `[]`
case *ast.SliceExpr:
return p._shortenCond(op.X, maxLen-2) + `[]`
case *ast.TypeAssertExpr:
return p._shortenCond(op.X, maxLen)
case *ast.StarExpr:
return p._shortenCond(op.X, maxLen)
case *ast.UnaryExpr:
switch op.Op {
case token.AND:
return p._shortenCond(op.X, maxLen)
case token.XOR:
return `^` + p._shortenCond(op.X, maxLen-1)
case token.NOT:
return `!` + p._shortenCond(op.X, maxLen-1)
default:
return p._shortenCond(op.X, maxLen)
}
case *ast.BinaryExpr:
tok := op.Op.String()
s := tok + p._shortenCond(op.Y, maxLen-1-len(tok))
if len(s) >= maxLen {
return `...` + s
}
return p._shortenCond(op.X, maxLen-len(s)) + s
default:
return "(...)"
}
}
func (p *ExecTrace) formatUpdateName(su *StateUpdate) string {
if len(su.args) == 0 && !su.isCall {
return su.name
}
return su.name + `(` + p.shortenArgs(su.args, maxArgLen) + `)`
}
func (p *ExecTrace) buildOperation(su *StateUpdate) (op, adapter string) {
if !su.HasName() {
return "", ""
}
if su.isContext {
s := su.name
for su = su.parent; su.HasName(); su = su.parent {
if len(su.args) == 0 {
s = `.` + su.name
} else {
s = `.` + p.formatUpdateName(su)
}
}
return s, ""
}
op = `DelayedStart`
switch {
case su.name == op:
op = ""
case su.parent != nil && su.parent.name == op && len(su.args) == 0:
op = su.name
su = su.parent
default:
return "", ""
}
if prep, adapt := p._extractAdapterCall(su); prep != nil {
prepName := ""
prepName, adapter = p.getAdapterCallNames(prep, adapt)
p.md.AddAdapter(adapter)
return prepName + `.` + op, adapter
}
return "", ""
}
func (p *ExecTrace) buildCallChain(su *StateUpdate) string {
if su == nil {
return ""
}
s := p.formatUpdateName(su)
for su = su.parent; su.HasName(); su = su.parent {
s = p.formatUpdateName(su) + `.` + s
}
return s
}
func (p *ExecTrace) getAdapterCallNames(start, prep *StateUpdate) (prepName, adapter string) {
adapter = p.buildCallChain(prep.parent)
a := prep.parent
prep.parent = nil
prepName = p.buildCallChain(start.parent)
prep.parent = a
return prepName, adapter
}
func (p *ExecTrace) addAdapterCall(start, prep *StateUpdate) {
prepName, adapter := p.getAdapterCallNames(start, prep)
p.md.AddAdapterCall(start.name, prepName, adapter)
}