-
Notifications
You must be signed in to change notification settings - Fork 762
/
Copy pathSymbolValidator.cs
157 lines (135 loc) · 8 KB
/
SymbolValidator.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Bicep.Core.Diagnostics;
using Bicep.Core.Extensions;
using Bicep.Core.Parsing;
using Bicep.Core.Syntax;
using Bicep.Core.Text;
using Bicep.Core.TypeSystem;
namespace Bicep.Core.Semantics
{
public static class SymbolValidator
{
private delegate IEnumerable<string> GetNameSuggestions();
private delegate ErrorDiagnostic GetMissingNameError(DiagnosticBuilder.DiagnosticBuilderInternal builder, string? suggestedName);
public static Symbol ResolveNamespaceQualifiedFunction(FunctionFlags allowedFlags, Symbol? foundSymbol, IdentifierSyntax identifierSyntax, NamespaceType namespaceType)
=> ResolveSymbolInternal(
allowedFlags,
foundSymbol,
identifierSyntax,
getNameSuggestions: () =>
{
var knowFunctionNames = namespaceType.MethodResolver.GetKnownFunctions().Keys;
return allowedFlags.HasAnyDecoratorFlag()
? knowFunctionNames.Concat(namespaceType.DecoratorResolver.GetKnownDecoratorFunctions().Keys)
: knowFunctionNames;
},
getMissingNameError: (builder, suggestedName) => suggestedName switch {
null => builder.FunctionDoesNotExistInNamespace(namespaceType, identifierSyntax.IdentifierName),
_ => builder.FunctionDoesNotExistInNamespaceWithSuggestion(namespaceType, identifierSyntax.IdentifierName, suggestedName),
});
public static Symbol ResolveObjectQualifiedFunction(Symbol? foundSymbol, IdentifierSyntax identifierSyntax, ObjectType objectType)
{
if (objectType is NamespaceType namespaceType)
{
return ResolveNamespaceQualifiedFunction(FunctionFlags.Default, foundSymbol, identifierSyntax, namespaceType);
}
return ResolveSymbolInternal(
FunctionFlags.Default,
foundSymbol,
identifierSyntax,
getNameSuggestions: () => objectType.MethodResolver.GetKnownFunctions().Keys,
getMissingNameError: (builder, suggestedName) => suggestedName switch {
null => builder.FunctionDoesNotExistOnObject(objectType, identifierSyntax.IdentifierName),
_ => builder.FunctionDoesNotExistOnObjectWithSuggestion(objectType, identifierSyntax.IdentifierName, suggestedName),
});
}
public static Symbol ResolveUnqualifiedFunction(FunctionFlags allowedFlags, Symbol? foundSymbol, IdentifierSyntax identifierSyntax, IEnumerable<NamespaceSymbol> namespaces)
=> ResolveSymbolInternal(
allowedFlags,
foundSymbol,
identifierSyntax,
getNameSuggestions: () => namespaces.SelectMany(x =>
{
var knowFunctionNames = x.Type.MethodResolver.GetKnownFunctions().Keys;
return allowedFlags.HasAnyDecoratorFlag()
? knowFunctionNames.Concat(x.Type.DecoratorResolver.GetKnownDecoratorFunctions().Keys)
: knowFunctionNames;
}),
getMissingNameError: (builder, suggestedName) => suggestedName switch {
null => builder.SymbolicNameDoesNotExist(identifierSyntax.IdentifierName),
_ => builder.SymbolicNameDoesNotExistWithSuggestion(identifierSyntax.IdentifierName, suggestedName),
});
public static Symbol ResolveUnqualifiedSymbol(Symbol? foundSymbol, IdentifierSyntax identifierSyntax, IEnumerable<NamespaceSymbol> namespaces, IEnumerable<string> declarations)
=> ResolveSymbolInternal(
FunctionFlags.Default,
foundSymbol,
identifierSyntax,
getNameSuggestions: () => namespaces.SelectMany(x => x.Type.Properties.Keys).Concat(declarations),
getMissingNameError: (builder, suggestedName) => suggestedName switch {
null => builder.SymbolicNameDoesNotExist(identifierSyntax.IdentifierName),
_ => builder.SymbolicNameDoesNotExistWithSuggestion(identifierSyntax.IdentifierName, suggestedName),
});
private static Symbol ResolveSymbolInternal(FunctionFlags allowedFlags, Symbol? foundSymbol, IdentifierSyntax identifierSyntax, GetNameSuggestions getNameSuggestions, GetMissingNameError getMissingNameError)
{
if (foundSymbol == null)
{
var nameCandidates = getNameSuggestions().ToImmutableSortedSet(StringComparer.OrdinalIgnoreCase);
var suggestedName = SpellChecker.GetSpellingSuggestion(identifierSyntax.IdentifierName, nameCandidates);
return new ErrorSymbol(getMissingNameError(DiagnosticBuilder.ForPosition(identifierSyntax), suggestedName));
}
switch (foundSymbol)
{
case FunctionSymbol functionSymbol:
return ResolveFunctionFlags(allowedFlags, functionSymbol, identifierSyntax);
default:
return foundSymbol;
}
}
private static Symbol ResolveFunctionFlags(FunctionFlags allowedFlags, FunctionSymbol functionSymbol, IPositionable span)
{
var functionFlags = functionSymbol.Overloads.Select(overload => overload.Flags).Aggregate((x, y) => x | y);
if (functionFlags.HasFlag(FunctionFlags.ParamDefaultsOnly) && !allowedFlags.HasFlag(FunctionFlags.ParamDefaultsOnly))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).FunctionOnlyValidInParameterDefaults(functionSymbol.Name));
}
if (functionFlags.HasFlag(FunctionFlags.RequiresInlining) && !allowedFlags.HasFlag(FunctionFlags.RequiresInlining))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).FunctionOnlyValidInResourceBody(functionSymbol.Name));
}
if (functionFlags.HasAnyDecoratorFlag() && allowedFlags.HasAllDecoratorFlags())
{
return functionSymbol;
}
if (!functionFlags.HasAnyDecoratorFlag() && allowedFlags.HasAnyDecoratorFlag())
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).CannotUseFunctionAsDecorator(functionSymbol.Name));
}
if (!functionFlags.HasFlag(FunctionFlags.ParameterDecorator) && allowedFlags.HasFlag(FunctionFlags.ParameterDecorator))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).CannotUseFunctionAsParameterDecorator(functionSymbol.Name));
}
if (!functionFlags.HasFlag(FunctionFlags.VariableDecorator) && allowedFlags.HasFlag(FunctionFlags.VariableDecorator))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).CannotUseFunctionAsVariableDecorator(functionSymbol.Name));
}
if (!functionFlags.HasFlag(FunctionFlags.ResourceDecorator) && allowedFlags.HasFlag(FunctionFlags.ResourceDecorator))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).CannotUseFunctionAsResourceDecorator(functionSymbol.Name));
}
if (!functionFlags.HasFlag(FunctionFlags.ModuleDecorator) && allowedFlags.HasFlag(FunctionFlags.ModuleDecorator))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).CannotUseFunctionAsModuleDecorator(functionSymbol.Name));
}
if (!functionFlags.HasFlag(FunctionFlags.OutputDecorator) && allowedFlags.HasFlag(FunctionFlags.OutputDecorator))
{
return new ErrorSymbol(DiagnosticBuilder.ForPosition(span).CannotUseFunctionAsOutputDecorator(functionSymbol.Name));
}
return functionSymbol;
}
}
}