/
CSharpParseOptions.cs
245 lines (207 loc) · 8.69 KB
/
CSharpParseOptions.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp
{
/// <summary>
/// This class stores several source parsing related options and offers access to their values.
/// </summary>
public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOptions>
{
/// <summary>
/// The default parse options.
/// </summary>
public static CSharpParseOptions Default { get; } = new CSharpParseOptions();
private ImmutableDictionary<string, string> _features;
/// <summary>
/// Gets the effective language version, which the compiler uses to select the
/// language rules to apply to the program.
/// </summary>
public LanguageVersion LanguageVersion { get; private set; }
/// <summary>
/// Gets the specified language version, which is the value that was specified in
/// the call to the constructor, or modified using the <see cref="WithLanguageVersion"/> method,
/// or provided on the command line.
/// </summary>
public LanguageVersion SpecifiedLanguageVersion { get; private set; }
internal ImmutableArray<string> PreprocessorSymbols { get; private set; }
/// <summary>
/// Gets the names of defined preprocessor symbols.
/// </summary>
public override IEnumerable<string> PreprocessorSymbolNames
{
get { return PreprocessorSymbols; }
}
public CSharpParseOptions(
LanguageVersion languageVersion = LanguageVersion.Default,
DocumentationMode documentationMode = DocumentationMode.Parse,
SourceCodeKind kind = SourceCodeKind.Regular,
IEnumerable<string>? preprocessorSymbols = null)
: this(languageVersion,
documentationMode,
kind,
preprocessorSymbols.ToImmutableArrayOrEmpty(),
ImmutableDictionary<string, string>.Empty)
{
}
internal CSharpParseOptions(
LanguageVersion languageVersion,
DocumentationMode documentationMode,
SourceCodeKind kind,
ImmutableArray<string> preprocessorSymbols,
IReadOnlyDictionary<string, string>? features)
: base(kind, documentationMode)
{
this.SpecifiedLanguageVersion = languageVersion;
this.LanguageVersion = languageVersion.MapSpecifiedToEffectiveVersion();
this.PreprocessorSymbols = preprocessorSymbols.ToImmutableArrayOrEmpty();
_features = features?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty;
}
private CSharpParseOptions(CSharpParseOptions other) : this(
languageVersion: other.SpecifiedLanguageVersion,
documentationMode: other.DocumentationMode,
kind: other.Kind,
preprocessorSymbols: other.PreprocessorSymbols,
features: other.Features)
{
}
public override string Language => LanguageNames.CSharp;
public new CSharpParseOptions WithKind(SourceCodeKind kind)
{
if (kind == this.SpecifiedKind)
{
return this;
}
var effectiveKind = kind.MapSpecifiedToEffectiveKind();
return new CSharpParseOptions(this) { SpecifiedKind = kind, Kind = effectiveKind };
}
public CSharpParseOptions WithLanguageVersion(LanguageVersion version)
{
if (version == this.SpecifiedLanguageVersion)
{
return this;
}
var effectiveLanguageVersion = version.MapSpecifiedToEffectiveVersion();
return new CSharpParseOptions(this) { SpecifiedLanguageVersion = version, LanguageVersion = effectiveLanguageVersion };
}
public CSharpParseOptions WithPreprocessorSymbols(IEnumerable<string>? preprocessorSymbols)
{
return WithPreprocessorSymbols(preprocessorSymbols.AsImmutableOrNull());
}
public CSharpParseOptions WithPreprocessorSymbols(params string[]? preprocessorSymbols)
{
return WithPreprocessorSymbols(preprocessorSymbols.AsImmutableOrNull());
}
public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray<string> symbols)
{
if (symbols.IsDefault)
{
symbols = ImmutableArray<string>.Empty;
}
if (symbols.Equals(this.PreprocessorSymbols))
{
return this;
}
return new CSharpParseOptions(this) { PreprocessorSymbols = symbols };
}
public new CSharpParseOptions WithDocumentationMode(DocumentationMode documentationMode)
{
if (documentationMode == this.DocumentationMode)
{
return this;
}
return new CSharpParseOptions(this) { DocumentationMode = documentationMode };
}
public override ParseOptions CommonWithKind(SourceCodeKind kind)
{
return WithKind(kind);
}
protected override ParseOptions CommonWithDocumentationMode(DocumentationMode documentationMode)
{
return WithDocumentationMode(documentationMode);
}
protected override ParseOptions CommonWithFeatures(IEnumerable<KeyValuePair<string, string>>? features)
{
return WithFeatures(features);
}
/// <summary>
/// Enable some experimental language features for testing.
/// </summary>
public new CSharpParseOptions WithFeatures(IEnumerable<KeyValuePair<string, string>>? features)
{
ImmutableDictionary<string, string> dictionary =
features?.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase)
?? ImmutableDictionary<string, string>.Empty;
return new CSharpParseOptions(this) { _features = dictionary };
}
public override IReadOnlyDictionary<string, string> Features
{
get
{
return _features;
}
}
internal override void ValidateOptions(ArrayBuilder<Diagnostic> builder)
{
ValidateOptions(builder, MessageProvider.Instance);
// Validate LanguageVersion not SpecifiedLanguageVersion, after Latest/Default has been converted:
if (!LanguageVersion.IsValid())
{
builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_BadLanguageVersion, LanguageVersion.ToString()));
}
if (!PreprocessorSymbols.IsDefaultOrEmpty)
{
foreach (var symbol in PreprocessorSymbols)
{
if (symbol == null)
{
builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_InvalidPreprocessingSymbol, "null"));
}
else if (!SyntaxFacts.IsValidIdentifier(symbol))
{
builder.Add(Diagnostic.Create(MessageProvider.Instance, (int)ErrorCode.ERR_InvalidPreprocessingSymbol, symbol));
}
}
}
}
internal bool IsFeatureEnabled(MessageID feature)
{
string? featureFlag = feature.RequiredFeature();
if (featureFlag != null)
{
return Features.ContainsKey(featureFlag);
}
LanguageVersion availableVersion = LanguageVersion;
LanguageVersion requiredVersion = feature.RequiredVersion();
return availableVersion >= requiredVersion;
}
public override bool Equals(object? obj)
{
return this.Equals(obj as CSharpParseOptions);
}
public bool Equals(CSharpParseOptions? other)
{
if (object.ReferenceEquals(this, other))
{
return true;
}
if (!base.EqualsHelper(other))
{
return false;
}
return this.SpecifiedLanguageVersion == other.SpecifiedLanguageVersion;
}
public override int GetHashCode()
{
return
Hash.Combine(base.GetHashCodeHelper(),
Hash.Combine((int)this.SpecifiedLanguageVersion, 0));
}
}
}