-
Notifications
You must be signed in to change notification settings - Fork 4k
/
LanguageVersion.cs
409 lines (361 loc) · 14.2 KB
/
LanguageVersion.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
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
// 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.
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp
{
/// <summary>
/// Specifies the language version.
/// </summary>
public enum LanguageVersion
{
/// <summary>
/// C# language version 1
/// </summary>
CSharp1 = 1,
/// <summary>
/// C# language version 2
/// </summary>
CSharp2 = 2,
/// <summary>
/// C# language version 3
/// </summary>
/// <remarks>
/// Features: LINQ.
/// </remarks>
CSharp3 = 3,
/// <summary>
/// C# language version 4
/// </summary>
/// <remarks>
/// Features: dynamic.
/// </remarks>
CSharp4 = 4,
/// <summary>
/// C# language version 5
/// </summary>
/// <remarks>
/// Features: async, caller info attributes.
/// </remarks>
CSharp5 = 5,
/// <summary>
/// C# language version 6
/// </summary>
/// <remarks>
/// <para>Features:</para>
/// <list type="bullet">
/// <item><description>Using of a static class</description></item>
/// <item><description>Exception filters</description></item>
/// <item><description>Await in catch/finally blocks</description></item>
/// <item><description>Auto-property initializers</description></item>
/// <item><description>Expression-bodied methods and properties</description></item>
/// <item><description>Null-propagating operator ?.</description></item>
/// <item><description>String interpolation</description></item>
/// <item><description>nameof operator</description></item>
/// <item><description>Dictionary initializer</description></item>
/// </list>
/// </remarks>
CSharp6 = 6,
/// <summary>
/// C# language version 7.0
/// </summary>
/// <remarks>
/// <para>Features:</para>
/// <list type="bullet">
/// <item><description>Out variables</description></item>
/// <item><description>Pattern-matching</description></item>
/// <item><description>Tuples</description></item>
/// <item><description>Deconstruction</description></item>
/// <item><description>Discards</description></item>
/// <item><description>Local functions</description></item>
/// <item><description>Digit separators</description></item>
/// <item><description>Ref returns and locals</description></item>
/// <item><description>Generalized async return types</description></item>
/// <item><description>More expression-bodied members</description></item>
/// <item><description>Throw expressions</description></item>
/// </list>
/// </remarks>
CSharp7 = 7,
/// <summary>
/// C# language version 7.1
/// </summary>
/// <remarks>
/// <para>Features:</para>
/// <list type="bullet">
/// <item><description>Async Main</description></item>
/// <item><description>Default literal</description></item>
/// <item><description>Inferred tuple element names</description></item>
/// <item><description>Pattern-matching with generics</description></item>
/// </list>
/// </remarks>
CSharp7_1 = 701,
/// <summary>
/// C# language version 7.2
/// </summary>
/// <remarks>
/// <para>Features:</para>
/// <list type="bullet">
/// <item><description>Ref readonly</description></item>
/// <item><description>Ref and readonly structs</description></item>
/// <item><description>Ref extensions</description></item>
/// <item><description>Conditional ref operator</description></item>
/// <item><description>Private protected</description></item>
/// <item><description>Digit separators after base specifier</description></item>
/// <item><description>Non-trailing named arguments</description></item>
/// </list>
/// </remarks>
CSharp7_2 = 702,
/// <summary>
/// C# language version 7.3
/// </summary>
CSharp7_3 = 703,
/// <summary>
/// C# language version 8.0
/// </summary>
CSharp8 = 800,
// When this value is available in the released NuGet package, update LanguageVersionExtensions in the IDE layer to point to it.
// https://github.com/dotnet/roslyn/issues/43348
//
/// <summary>
/// C# language version 9.0
/// </summary>
CSharp9 = 900,
/// <summary>
/// The latest major supported version.
/// </summary>
LatestMajor = int.MaxValue - 2,
/// <summary>
/// Preview of the next language version.
/// </summary>
Preview = int.MaxValue - 1,
/// <summary>
/// The latest supported version of the language.
/// </summary>
Latest = int.MaxValue,
/// <summary>
/// The default language version, which is the latest supported version.
/// </summary>
Default = 0,
}
internal static class LanguageVersionExtensionsInternal
{
internal static bool IsValid(this LanguageVersion value)
{
switch (value)
{
case LanguageVersion.CSharp1:
case LanguageVersion.CSharp2:
case LanguageVersion.CSharp3:
case LanguageVersion.CSharp4:
case LanguageVersion.CSharp5:
case LanguageVersion.CSharp6:
case LanguageVersion.CSharp7:
case LanguageVersion.CSharp7_1:
case LanguageVersion.CSharp7_2:
case LanguageVersion.CSharp7_3:
case LanguageVersion.CSharp8:
case LanguageVersion.CSharp9:
case LanguageVersion.Preview:
return true;
}
return false;
}
internal static ErrorCode GetErrorCode(this LanguageVersion version)
{
switch (version)
{
case LanguageVersion.CSharp1:
return ErrorCode.ERR_FeatureNotAvailableInVersion1;
case LanguageVersion.CSharp2:
return ErrorCode.ERR_FeatureNotAvailableInVersion2;
case LanguageVersion.CSharp3:
return ErrorCode.ERR_FeatureNotAvailableInVersion3;
case LanguageVersion.CSharp4:
return ErrorCode.ERR_FeatureNotAvailableInVersion4;
case LanguageVersion.CSharp5:
return ErrorCode.ERR_FeatureNotAvailableInVersion5;
case LanguageVersion.CSharp6:
return ErrorCode.ERR_FeatureNotAvailableInVersion6;
case LanguageVersion.CSharp7:
return ErrorCode.ERR_FeatureNotAvailableInVersion7;
case LanguageVersion.CSharp7_1:
return ErrorCode.ERR_FeatureNotAvailableInVersion7_1;
case LanguageVersion.CSharp7_2:
return ErrorCode.ERR_FeatureNotAvailableInVersion7_2;
case LanguageVersion.CSharp7_3:
return ErrorCode.ERR_FeatureNotAvailableInVersion7_3;
case LanguageVersion.CSharp8:
return ErrorCode.ERR_FeatureNotAvailableInVersion8;
case LanguageVersion.CSharp9:
return ErrorCode.ERR_FeatureNotAvailableInVersion9;
default:
throw ExceptionUtilities.UnexpectedValue(version);
}
}
}
internal class CSharpRequiredLanguageVersion : RequiredLanguageVersion
{
internal LanguageVersion Version { get; }
internal CSharpRequiredLanguageVersion(LanguageVersion version)
{
Version = (version == LanguageVersion.Preview.MapSpecifiedToEffectiveVersion()) ? LanguageVersion.Preview : version;
}
public override string ToString() => Version.ToDisplayString();
}
public static class LanguageVersionFacts
{
/// <summary>
/// Displays the version number in the format expected on the command-line (/langver flag).
/// For instance, "6", "7.0", "7.1", "latest".
/// </summary>
public static string ToDisplayString(this LanguageVersion version)
{
switch (version)
{
case LanguageVersion.CSharp1:
return "1";
case LanguageVersion.CSharp2:
return "2";
case LanguageVersion.CSharp3:
return "3";
case LanguageVersion.CSharp4:
return "4";
case LanguageVersion.CSharp5:
return "5";
case LanguageVersion.CSharp6:
return "6";
case LanguageVersion.CSharp7:
return "7.0";
case LanguageVersion.CSharp7_1:
return "7.1";
case LanguageVersion.CSharp7_2:
return "7.2";
case LanguageVersion.CSharp7_3:
return "7.3";
case LanguageVersion.CSharp8:
return "8.0";
case LanguageVersion.CSharp9:
return "9.0";
case LanguageVersion.Default:
return "default";
case LanguageVersion.Latest:
return "latest";
case LanguageVersion.LatestMajor:
return "latestmajor";
case LanguageVersion.Preview:
return "preview";
default:
throw ExceptionUtilities.UnexpectedValue(version);
}
}
/// <summary>
/// Try parse a <see cref="LanguageVersion"/> from a string input, returning default if input was null.
/// </summary>
public static bool TryParse(string? version, out LanguageVersion result)
{
if (version == null)
{
result = LanguageVersion.Default;
return true;
}
switch (CaseInsensitiveComparison.ToLower(version))
{
case "default":
result = LanguageVersion.Default;
return true;
case "latest":
result = LanguageVersion.Latest;
return true;
case "latestmajor":
result = LanguageVersion.LatestMajor;
return true;
case "preview":
result = LanguageVersion.Preview;
return true;
case "1":
case "1.0":
case "iso-1":
result = LanguageVersion.CSharp1;
return true;
case "2":
case "2.0":
case "iso-2":
result = LanguageVersion.CSharp2;
return true;
case "3":
case "3.0":
result = LanguageVersion.CSharp3;
return true;
case "4":
case "4.0":
result = LanguageVersion.CSharp4;
return true;
case "5":
case "5.0":
result = LanguageVersion.CSharp5;
return true;
case "6":
case "6.0":
result = LanguageVersion.CSharp6;
return true;
case "7":
case "7.0":
result = LanguageVersion.CSharp7;
return true;
case "7.1":
result = LanguageVersion.CSharp7_1;
return true;
case "7.2":
result = LanguageVersion.CSharp7_2;
return true;
case "7.3":
result = LanguageVersion.CSharp7_3;
return true;
case "8":
case "8.0":
result = LanguageVersion.CSharp8;
return true;
case "9":
case "9.0":
result = LanguageVersion.CSharp9;
return true;
default:
result = LanguageVersion.Default;
return false;
}
}
/// <summary>
/// Map a language version (such as Default, Latest, or CSharpN) to a specific version (CSharpM).
/// </summary>
public static LanguageVersion MapSpecifiedToEffectiveVersion(this LanguageVersion version)
{
switch (version)
{
case LanguageVersion.Latest:
case LanguageVersion.Default:
case LanguageVersion.LatestMajor:
return LanguageVersion.CSharp9;
default:
return version;
}
}
internal static LanguageVersion CurrentVersion => LanguageVersion.CSharp9;
/// <summary>Inference of tuple element names was added in C# 7.1</summary>
internal static bool DisallowInferredTupleElementNames(this LanguageVersion self)
{
return self < MessageID.IDS_FeatureInferredTupleNames.RequiredVersion();
}
internal static bool AllowNonTrailingNamedArguments(this LanguageVersion self)
{
return self >= MessageID.IDS_FeatureNonTrailingNamedArguments.RequiredVersion();
}
internal static bool AllowAttributesOnBackingFields(this LanguageVersion self)
{
return self >= MessageID.IDS_FeatureAttributesOnBackingFields.RequiredVersion();
}
internal static bool AllowImprovedOverloadCandidates(this LanguageVersion self)
{
return self >= MessageID.IDS_FeatureImprovedOverloadCandidates.RequiredVersion();
}
}
}