forked from Azure/bicep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceDependencyVisitor.cs
299 lines (251 loc) · 12.9 KB
/
ResourceDependencyVisitor.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Bicep.Core.DataFlow;
using Bicep.Core.Extensions;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Metadata;
using Bicep.Core.Syntax;
namespace Bicep.Core.Emit
{
public class ResourceDependencyVisitor : AstVisitor
{
private readonly SemanticModel model;
private Options? options;
private readonly IDictionary<DeclaredSymbol, HashSet<ResourceDependency>> resourceDependencies;
private DeclaredSymbol? currentDeclaration;
public struct Options
{
// If true, only inferred dependencies will be returned, not those declared explicitly by dependsOn entries
public bool? IgnoreExplicitDependsOn;
}
/// <summary>
/// Determines resource dependencies between all resources, returning it as a map of resource -> dependencies.
/// Consider usage in expressions, parent/child relationships and (by default) dependsOn entries
/// </summary>
/// <returns></returns>
public static ImmutableDictionary<DeclaredSymbol, ImmutableHashSet<ResourceDependency>> GetResourceDependencies(SemanticModel model, Options? options = null)
{
var visitor = new ResourceDependencyVisitor(model, options);
visitor.Visit(model.Root.Syntax);
var output = new Dictionary<DeclaredSymbol, ImmutableHashSet<ResourceDependency>>();
foreach (var kvp in visitor.resourceDependencies)
{
if (kvp.Key is ResourceSymbol || kvp.Key is ModuleSymbol)
{
output[kvp.Key] = OptimizeDependencies(kvp.Value);
}
}
return output.ToImmutableDictionary();
}
private static ImmutableHashSet<ResourceDependency> OptimizeDependencies(HashSet<ResourceDependency> dependencies) =>
dependencies
.GroupBy(dep => dep.Resource)
.SelectMany(group => @group.FirstOrDefault(dep => dep.IndexExpression == null) is { } dependencyWithoutIndex
? dependencyWithoutIndex.AsEnumerable()
: @group)
.ToImmutableHashSet();
private ResourceDependencyVisitor(SemanticModel model, Options? options)
{
this.model = model;
this.options = options;
this.resourceDependencies = new Dictionary<DeclaredSymbol, HashSet<ResourceDependency>>();
this.currentDeclaration = null;
}
public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
{
static int GetIndexOfLastNonExistingAncestor(ImmutableArray<ResourceAncestorGraph.ResourceAncestor> ancestors)
{
for (int i = ancestors.Length - 1; i >= 0; i--)
{
if (!ancestors[i].Resource.IsExistingResource)
{
// we found the non-existing resource - we're done
return i;
}
}
// no non-existing resources are found in the ancestors list
return -1;
}
if (model.ResourceMetadata.TryLookup(syntax) is not DeclaredResourceMetadata resource)
{
// When invoked by BicepDeploymentGraphHandler, it's possible that the declaration is unbound.
return;
}
// Resource ancestors are always dependencies.
var ancestors = this.model.ResourceAncestors.GetAncestors(resource);
var lastNonExistingAncestorIndex = GetIndexOfLastNonExistingAncestor(ancestors);
// save previous declaration as we may call this recursively
var prevDeclaration = this.currentDeclaration;
this.currentDeclaration = resource.Symbol;
this.resourceDependencies[resource.Symbol] = new HashSet<ResourceDependency>(ancestors.Select((a, i) => new ResourceDependency(a.Resource.Symbol, a.IndexExpression, i == lastNonExistingAncestorIndex ? ResourceDependencyKind.Primary : ResourceDependencyKind.Transitive)));
base.VisitResourceDeclarationSyntax(syntax);
// restore previous declaration
this.currentDeclaration = prevDeclaration;
}
public override void VisitModuleDeclarationSyntax(ModuleDeclarationSyntax syntax)
{
if (this.model.GetSymbolInfo(syntax) is not ModuleSymbol moduleSymbol)
{
return;
}
// save previous declaration as we may call this recursively
var prevDeclaration = this.currentDeclaration;
this.currentDeclaration = moduleSymbol;
this.resourceDependencies[moduleSymbol] = new HashSet<ResourceDependency>();
base.VisitModuleDeclarationSyntax(syntax);
// restore previous declaration
this.currentDeclaration = prevDeclaration;
}
public override void VisitVariableDeclarationSyntax(VariableDeclarationSyntax syntax)
{
if (this.model.GetSymbolInfo(syntax) is not VariableSymbol variableSymbol)
{
return;
}
// save previous declaration as we may call this recursively
var prevDeclaration = this.currentDeclaration;
this.currentDeclaration = variableSymbol;
this.resourceDependencies[variableSymbol] = new HashSet<ResourceDependency>();
base.VisitVariableDeclarationSyntax(syntax);
// restore previous declaration
this.currentDeclaration = prevDeclaration;
}
private IEnumerable<ResourceDependency> GetResourceDependencies(DeclaredSymbol declaredSymbol)
{
if (!resourceDependencies.TryGetValue(declaredSymbol, out var dependencies))
{
// recursively visit dependent variables
this.Visit(declaredSymbol.DeclaringSyntax);
if (!resourceDependencies.TryGetValue(declaredSymbol, out dependencies))
{
return Enumerable.Empty<ResourceDependency>();
}
}
return dependencies;
}
public override void VisitVariableAccessSyntax(VariableAccessSyntax syntax)
{
if (currentDeclaration is null)
{
return;
}
if (!this.resourceDependencies.TryGetValue(currentDeclaration, out HashSet<ResourceDependency>? currentResourceDependencies))
{
Debug.Fail("currentDeclaration should be guaranteed to be contained in this.resourceDependencies in VisitResourceDeclarationSyntax");
return;
}
switch (model.GetSymbolInfo(syntax))
{
case VariableSymbol variableSymbol:
var varDependencies = GetResourceDependencies(variableSymbol);
currentResourceDependencies.UnionWith(varDependencies);
return;
case ResourceSymbol resourceSymbol:
if (resourceSymbol.DeclaringResource.IsExistingResource())
{
var existingDependencies = GetResourceDependencies(resourceSymbol);
currentResourceDependencies.UnionWith(existingDependencies);
return;
}
currentResourceDependencies.Add(new ResourceDependency(resourceSymbol, GetIndexExpression(syntax, resourceSymbol.IsCollection), ResourceDependencyKind.Primary));
return;
case ModuleSymbol moduleSymbol:
currentResourceDependencies.Add(new ResourceDependency(moduleSymbol, GetIndexExpression(syntax, moduleSymbol.IsCollection), ResourceDependencyKind.Primary));
return;
}
}
public override void VisitResourceAccessSyntax(ResourceAccessSyntax syntax)
{
if (currentDeclaration is null)
{
return;
}
if (!this.resourceDependencies.TryGetValue(currentDeclaration, out HashSet<ResourceDependency>? currentResourceDependencies))
{
Debug.Fail("currentDeclaration should be guaranteed to be in this.resourceDependencies in VisitResourceDeclarationSyntax");
return;
}
switch (model.GetSymbolInfo(syntax))
{
case ResourceSymbol resourceSymbol:
if (resourceSymbol.DeclaringResource.IsExistingResource())
{
var existingDependencies = GetResourceDependencies(resourceSymbol);
currentResourceDependencies.UnionWith(existingDependencies);
return;
}
currentResourceDependencies.Add(new ResourceDependency(resourceSymbol, GetIndexExpression(syntax, resourceSymbol.IsCollection), ResourceDependencyKind.Primary));
return;
case ModuleSymbol moduleSymbol:
currentResourceDependencies.Add(new ResourceDependency(moduleSymbol, GetIndexExpression(syntax, moduleSymbol.IsCollection), ResourceDependencyKind.Primary));
return;
}
}
private SyntaxBase? GetIndexExpression(SyntaxBase syntax, bool isCollection)
{
SyntaxBase? candidateIndexExpression = isCollection && this.model.Binder.GetParent(syntax) is ArrayAccessSyntax arrayAccess && ReferenceEquals(arrayAccess.BaseExpression, syntax)
? arrayAccess.IndexExpression
: null;
if (candidateIndexExpression is null)
{
// there is no index expression
// depend on the entire collection instead
return null;
}
// the index expression we just obtained could be in the scope of a property loop
// when dependsOn properties are generated, this would mean that a local would be taken outside of its expected scope
// which would result in runtime errors
// to avoid this we will run data flow analysis to determine if such locals are present in the index expression
var dfa = new DataFlowAnalyzer(this.model);
var context = this.currentDeclaration switch
{
ResourceSymbol resourceSymbol => resourceSymbol.DeclaringResource.GetBody(),
ModuleSymbol moduleSymbol => moduleSymbol.DeclaringModule.GetBody(),
VariableSymbol variableSymbol => variableSymbol.DeclaringVariable.Value,
_ => throw new NotImplementedException($"Unexpected current declaration type '{this.currentDeclaration?.GetType().Name}'.")
};
// using the resource/module body as the context to allow indexed dependencies relying on the resource/module loop index to work as expected
var inaccessibleLocals = dfa.GetInaccessibleLocalsAfterSyntaxMove(candidateIndexExpression, context);
if (inaccessibleLocals.Any())
{
// some local will become inaccessible
// depend on the entire collection instead
return null;
}
return candidateIndexExpression;
}
public override void VisitObjectPropertySyntax(ObjectPropertySyntax propertySyntax)
{
if (options?.IgnoreExplicitDependsOn == true)
{
// Is it a property named "dependsOn"?
if (propertySyntax.Key is IdentifierSyntax key && key.NameEquals(LanguageConstants.ResourceDependsOnPropertyName))
{
// ... that is the a top-level resource or module property?
if (this.IsTopLevelPropertyOfCurrentDeclaration(propertySyntax))
{
// Yes - don't include dependencies from this property value
return;
}
}
}
base.VisitObjectPropertySyntax(propertySyntax);
}
private bool IsTopLevelPropertyOfCurrentDeclaration(ObjectPropertySyntax propertySyntax)
{
SyntaxBase? declaringSyntax = this.currentDeclaration switch
{
ResourceSymbol resourceSymbol => (resourceSymbol.DeclaringSyntax as ResourceDeclarationSyntax)?.TryGetBody(),
ModuleSymbol moduleSymbol => (moduleSymbol.DeclaringSyntax as ModuleDeclarationSyntax)?.TryGetBody(),
_ => null
};
IEnumerable<ObjectPropertySyntax>? currentDeclarationProperties = (declaringSyntax as ObjectSyntax)?.Properties;
return currentDeclarationProperties?.Contains(propertySyntax) ?? false;
}
}
}