|
1 | 1 | package staticconfig |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "go/token" |
5 | | - "iter" |
6 | | - |
7 | 4 | "github.com/dogmatiq/enginekit/config" |
8 | 5 | "github.com/dogmatiq/enginekit/config/internal/configbuilder" |
9 | 6 | "github.com/dogmatiq/enginekit/config/staticconfig/internal/ssax" |
10 | 7 | "golang.org/x/tools/go/ssa" |
11 | 8 | ) |
12 | 9 |
|
13 | | -func findAllocation(v ssa.Value) (*ssa.Alloc, bool) { |
14 | | - switch v := v.(type) { |
15 | | - case *ssa.Alloc: |
16 | | - return v, true |
17 | | - |
18 | | - case *ssa.Slice: |
19 | | - return findAllocation(v.X) |
20 | | - |
21 | | - case *ssa.UnOp: |
22 | | - if v.Op == token.MUL { // pointer de-reference |
23 | | - return findAllocation(v.X) |
24 | | - } |
25 | | - return nil, false |
26 | | - |
27 | | - default: |
28 | | - return nil, false |
29 | | - } |
30 | | -} |
31 | | - |
32 | | -func isIndexOfArray( |
33 | | - array *ssa.Alloc, |
34 | | - v ssa.Value, |
35 | | -) (int, bool) { |
36 | | - switch v := v.(type) { |
37 | | - case *ssa.IndexAddr: |
38 | | - if v.X != array { |
39 | | - return 0, false |
40 | | - } |
41 | | - return ssax.AsInt(v.Index).TryGet() |
42 | | - } |
43 | | - return 0, false |
44 | | -} |
45 | | - |
46 | | -func resolveVariadic[ |
| 10 | +func analyzeVariadicArguments[ |
47 | 11 | T config.Entity, |
48 | 12 | E any, |
49 | 13 | B configbuilder.EntityBuilder[T, E], |
| 14 | + TChild config.Component, |
| 15 | + BChild configbuilder.ComponentBuilder[TChild], |
50 | 16 | ]( |
51 | | - b B, |
52 | | - inst ssa.CallInstruction, |
53 | | -) iter.Seq[ssa.Value] { |
54 | | - return func(yield func(ssa.Value) bool) { |
55 | | - call := inst.Common() |
| 17 | + ctx *configurerCallContext[T, E, B], |
| 18 | + child func(func(BChild)), |
| 19 | + analyze func(*context, BChild, ssa.Value), |
| 20 | +) { |
| 21 | + // The variadic slice parameter is always the last argument. |
| 22 | + varargs := ctx.Args[len(ctx.Args)-1] |
56 | 23 |
|
57 | | - variadics := call.Args[len(call.Args)-1] |
58 | | - if ssax.IsZeroValue(variadics) { |
59 | | - return |
60 | | - } |
| 24 | + if ssax.IsZeroValue(varargs) { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + array, ok := findSliceArrayAllocation(varargs) |
| 29 | + if !ok { |
| 30 | + ctx.Builder.Partial() |
| 31 | + return |
| 32 | + } |
61 | 33 |
|
62 | | - array, ok := findAllocation(variadics) |
63 | | - if !ok { |
64 | | - b.Partial() |
65 | | - return |
| 34 | + buildersByIndex := map[int][]BChild{} |
| 35 | + |
| 36 | + for block := range ssax.WalkBlock(array.Block()) { |
| 37 | + // If there's no path from this block to the call instruction, we can |
| 38 | + // safely ignore it, even if it modifies the underlying array. |
| 39 | + if !ssax.PathExists(block, ctx.Instruction.Block()) { |
| 40 | + continue |
66 | 41 | } |
67 | 42 |
|
68 | | - for b := range ssax.WalkBlock(array.Block()) { |
69 | | - if !ssax.PathExists(b, inst.Block()) { |
70 | | - continue |
71 | | - } |
| 43 | + for inst := range ssax.InstructionsBefore(block, ctx.Instruction) { |
| 44 | + switch inst := inst.(type) { |
| 45 | + case *ssa.Store: |
| 46 | + if addr, ok := inst.Addr.(*ssa.IndexAddr); ok && addr.X == array { |
| 47 | + child(func(b BChild) { |
| 48 | + if index, ok := ssax.AsInt(addr.Index).TryGet(); ok { |
| 49 | + // If there are multiple writes to the same index, |
| 50 | + // we mark them all as speculative. |
| 51 | + // |
| 52 | + // TODO: Could we handle this more intelligently by |
| 53 | + // using the value of the store instruction closest |
| 54 | + // to the call instruction? |
| 55 | + conflicting := buildersByIndex[index] |
| 56 | + if len(conflicting) == 1 { |
| 57 | + conflicting[0].Speculative() |
| 58 | + } |
| 59 | + if len(conflicting) != 0 { |
| 60 | + b.Speculative() |
| 61 | + } |
| 62 | + buildersByIndex[index] = append(conflicting, b) |
| 63 | + } else { |
| 64 | + // If we can't resolve the index we assume the child |
| 65 | + // is speculative because we can't tell if it is |
| 66 | + // ever overwritten with a different value. |
| 67 | + b.Speculative() |
| 68 | + } |
72 | 69 |
|
73 | | - for inst := range ssax.InstructionsBefore(b, inst) { |
74 | | - switch inst := inst.(type) { |
75 | | - case *ssa.Store: |
76 | | - if _, ok := isIndexOfArray(array, inst.Addr); ok { |
77 | | - if !yield(inst.Val) { |
78 | | - return |
| 70 | + if ctx.IsSpeculative { |
| 71 | + b.Speculative() |
79 | 72 | } |
80 | | - } |
| 73 | + |
| 74 | + analyze(ctx.context, b, inst.Val) |
| 75 | + }) |
81 | 76 | } |
82 | 77 | } |
83 | 78 | } |
84 | 79 | } |
85 | 80 | } |
| 81 | + |
| 82 | +// findSliceArrayAllocation returns the underlying array allocation of a slice. |
| 83 | +func findSliceArrayAllocation(v ssa.Value) (*ssa.Alloc, bool) { |
| 84 | + switch v := v.(type) { |
| 85 | + case *ssa.Alloc: |
| 86 | + return v, true |
| 87 | + case *ssa.Slice: |
| 88 | + return findSliceArrayAllocation(v.X) |
| 89 | + default: |
| 90 | + return nil, false |
| 91 | + } |
| 92 | +} |
0 commit comments