提交 042823a3 编写于 作者: G gafter 提交者: Neal Gafter

Add a "default" language version

and SpecifiedLanguageVersion to the parse options.
上级 9356acce
...@@ -21,10 +21,15 @@ public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOpt ...@@ -21,10 +21,15 @@ public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOpt
private ImmutableDictionary<string, string> _features; private ImmutableDictionary<string, string> _features;
/// <summary> /// <summary>
/// Gets the language version. /// Gets the effective language version.
/// </summary> /// </summary>
public LanguageVersion LanguageVersion { get; private set; } public LanguageVersion LanguageVersion { get; private set; }
/// <summary>
/// Gets the specified language version.
/// </summary>
public LanguageVersion SpecifiedLanguageVersion { get; private set; }
internal ImmutableArray<string> PreprocessorSymbols { get; private set; } internal ImmutableArray<string> PreprocessorSymbols { get; private set; }
/// <summary> /// <summary>
...@@ -36,11 +41,11 @@ public override IEnumerable<string> PreprocessorSymbolNames ...@@ -36,11 +41,11 @@ public override IEnumerable<string> PreprocessorSymbolNames
} }
public CSharpParseOptions( public CSharpParseOptions(
LanguageVersion languageVersion = LanguageVersion.Latest, LanguageVersion languageVersion = LanguageVersion.Default,
DocumentationMode documentationMode = DocumentationMode.Parse, DocumentationMode documentationMode = DocumentationMode.Parse,
SourceCodeKind kind = SourceCodeKind.Regular, SourceCodeKind kind = SourceCodeKind.Regular,
IEnumerable<string> preprocessorSymbols = null) IEnumerable<string> preprocessorSymbols = null)
: this(languageVersion.MapLatestToVersion(), documentationMode, kind, preprocessorSymbols.ToImmutableArrayOrEmpty()) : this(languageVersion, documentationMode, kind, preprocessorSymbols.ToImmutableArrayOrEmpty())
{ {
// We test the mapped value, LanguageVersion, rather than the parameter, languageVersion, // We test the mapped value, LanguageVersion, rather than the parameter, languageVersion,
// which has not had "Latest" mapped to the latest version yet. // which has not had "Latest" mapped to the latest version yet.
...@@ -83,7 +88,7 @@ public override IEnumerable<string> PreprocessorSymbolNames ...@@ -83,7 +88,7 @@ public override IEnumerable<string> PreprocessorSymbolNames
} }
private CSharpParseOptions(CSharpParseOptions other) : this( private CSharpParseOptions(CSharpParseOptions other) : this(
languageVersion: other.LanguageVersion, languageVersion: other.SpecifiedLanguageVersion,
documentationMode: other.DocumentationMode, documentationMode: other.DocumentationMode,
kind: other.Kind, kind: other.Kind,
preprocessorSymbols: other.PreprocessorSymbols, preprocessorSymbols: other.PreprocessorSymbols,
...@@ -100,7 +105,8 @@ public override IEnumerable<string> PreprocessorSymbolNames ...@@ -100,7 +105,8 @@ public override IEnumerable<string> PreprocessorSymbolNames
: base(kind, documentationMode) : base(kind, documentationMode)
{ {
Debug.Assert(!preprocessorSymbols.IsDefault); Debug.Assert(!preprocessorSymbols.IsDefault);
this.LanguageVersion = languageVersion; this.SpecifiedLanguageVersion = languageVersion;
this.LanguageVersion = languageVersion.MapSpecifiedToEffectiveVersion();
this.PreprocessorSymbols = preprocessorSymbols; this.PreprocessorSymbols = preprocessorSymbols;
_features = ImmutableDictionary<string, string>.Empty; _features = ImmutableDictionary<string, string>.Empty;
} }
...@@ -122,19 +128,18 @@ public new CSharpParseOptions WithKind(SourceCodeKind kind) ...@@ -122,19 +128,18 @@ public new CSharpParseOptions WithKind(SourceCodeKind kind)
public CSharpParseOptions WithLanguageVersion(LanguageVersion version) public CSharpParseOptions WithLanguageVersion(LanguageVersion version)
{ {
version = version.MapLatestToVersion(); if (version == this.SpecifiedLanguageVersion)
if (version == this.LanguageVersion)
{ {
return this; return this;
} }
if (!version.IsValid()) var effectiveLanguageVersion = version.MapSpecifiedToEffectiveVersion();
if (!effectiveLanguageVersion.IsValid())
{ {
throw new ArgumentOutOfRangeException(nameof(version)); throw new ArgumentOutOfRangeException(nameof(version));
} }
return new CSharpParseOptions(this) { LanguageVersion = version }; return new CSharpParseOptions(this) { SpecifiedLanguageVersion = version, LanguageVersion = effectiveLanguageVersion };
} }
public CSharpParseOptions WithPreprocessorSymbols(IEnumerable<string> preprocessorSymbols) public CSharpParseOptions WithPreprocessorSymbols(IEnumerable<string> preprocessorSymbols)
...@@ -242,14 +247,14 @@ public bool Equals(CSharpParseOptions other) ...@@ -242,14 +247,14 @@ public bool Equals(CSharpParseOptions other)
return false; return false;
} }
return this.LanguageVersion == other.LanguageVersion; return this.SpecifiedLanguageVersion == other.SpecifiedLanguageVersion;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return return
Hash.Combine(base.GetHashCodeHelper(), Hash.Combine(base.GetHashCodeHelper(),
Hash.Combine((int)this.LanguageVersion, 0)); Hash.Combine((int)this.SpecifiedLanguageVersion, 0));
} }
} }
} }
...@@ -4450,7 +4450,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ ...@@ -4450,7 +4450,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
/define:&lt;symbol list&gt; Define conditional compilation symbol(s) (Short /define:&lt;symbol list&gt; Define conditional compilation symbol(s) (Short
form: /d) form: /d)
/langversion:&lt;string&gt; Specify language version mode: ISO-1, ISO-2, 3, /langversion:&lt;string&gt; Specify language version mode: ISO-1, ISO-2, 3,
4, 5, 6, or Default 4, 5, 6, Default, or Latest
- SECURITY - - SECURITY -
/delaysign[+|-] Delay-sign the assembly using only the public /delaysign[+|-] Delay-sign the assembly using only the public
......
...@@ -70,7 +70,7 @@ public new CSharpCommandLineArguments Parse(IEnumerable<string> args, string bas ...@@ -70,7 +70,7 @@ public new CSharpCommandLineArguments Parse(IEnumerable<string> args, string bas
bool utf8output = false; bool utf8output = false;
OutputKind outputKind = OutputKind.ConsoleApplication; OutputKind outputKind = OutputKind.ConsoleApplication;
SubsystemVersion subsystemVersion = SubsystemVersion.None; SubsystemVersion subsystemVersion = SubsystemVersion.None;
LanguageVersion languageVersion = CSharpParseOptions.Default.LanguageVersion; LanguageVersion languageVersion = LanguageVersion.Default;
string mainTypeName = null; string mainTypeName = null;
string win32ManifestFile = null; string win32ManifestFile = null;
string win32ResourceFile = null; string win32ResourceFile = null;
...@@ -1695,11 +1695,9 @@ private static void ValidateWin32Settings(string win32ResourceFile, string win32 ...@@ -1695,11 +1695,9 @@ private static void ValidateWin32Settings(string win32ResourceFile, string win32
private static bool TryParseLanguageVersion(string str, out LanguageVersion version) private static bool TryParseLanguageVersion(string str, out LanguageVersion version)
{ {
var defaultVersion = LanguageVersion.Latest.MapLatestToVersion();
if (str == null) if (str == null)
{ {
version = defaultVersion; version = LanguageVersion.Default;
return true; return true;
} }
...@@ -1718,7 +1716,11 @@ private static bool TryParseLanguageVersion(string str, out LanguageVersion vers ...@@ -1718,7 +1716,11 @@ private static bool TryParseLanguageVersion(string str, out LanguageVersion vers
return true; return true;
case "default": case "default":
version = defaultVersion; version = LanguageVersion.Default;
return true;
case "latest":
version = LanguageVersion.Latest;
return true; return true;
default: default:
...@@ -1735,7 +1737,8 @@ private static bool TryParseLanguageVersion(string str, out LanguageVersion vers ...@@ -1735,7 +1737,8 @@ private static bool TryParseLanguageVersion(string str, out LanguageVersion vers
version = (LanguageVersion)versionNumber; version = (LanguageVersion)versionNumber;
return true; return true;
} }
version = defaultVersion;
version = LanguageVersion.Default;
return false; return false;
} }
} }
......
...@@ -356,7 +356,7 @@ private static LanguageVersion CommonLanguageVersion(ImmutableArray<SyntaxTree> ...@@ -356,7 +356,7 @@ private static LanguageVersion CommonLanguageVersion(ImmutableArray<SyntaxTree>
} }
} }
return result ?? CSharpParseOptions.Default.LanguageVersion; return result ?? LanguageVersion.Default.MapSpecifiedToEffectiveVersion();
} }
/// <summary> /// <summary>
......
...@@ -9,6 +9,11 @@ namespace Microsoft.CodeAnalysis.CSharp ...@@ -9,6 +9,11 @@ namespace Microsoft.CodeAnalysis.CSharp
/// </summary> /// </summary>
public enum LanguageVersion public enum LanguageVersion
{ {
/// <summary>
/// The default language version, which is the latest major supported version.
/// </summary>
Default = 0,
/// <summary> /// <summary>
/// C# language version 1.0. /// C# language version 1.0.
/// </summary> /// </summary>
...@@ -71,9 +76,16 @@ public enum LanguageVersion ...@@ -71,9 +76,16 @@ public enum LanguageVersion
internal static partial class LanguageVersionExtensions internal static partial class LanguageVersionExtensions
{ {
internal static LanguageVersion MapLatestToVersion(this LanguageVersion version) internal static LanguageVersion MapSpecifiedToEffectiveVersion(this LanguageVersion version)
{ {
return (version == LanguageVersion.Latest) ? LanguageVersion.CSharp7 : version; switch (version)
{
case LanguageVersion.Latest:
case LanguageVersion.Default:
return LanguageVersion.CSharp7;
default:
return version;
}
} }
internal static bool IsValid(this LanguageVersion value) internal static bool IsValid(this LanguageVersion value)
......
Microsoft.CodeAnalysis.CSharp.CSharpParseOptions.CSharpParseOptions(Microsoft.CodeAnalysis.CSharp.LanguageVersion languageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest, Microsoft.CodeAnalysis.DocumentationMode documentationMode = Microsoft.CodeAnalysis.DocumentationMode.Parse, Microsoft.CodeAnalysis.SourceCodeKind kind = Microsoft.CodeAnalysis.SourceCodeKind.Regular, System.Collections.Generic.IEnumerable<string> preprocessorSymbols = null) -> void Microsoft.CodeAnalysis.CSharp.CSharpParseOptions.CSharpParseOptions(Microsoft.CodeAnalysis.CSharp.LanguageVersion languageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion.Default, Microsoft.CodeAnalysis.DocumentationMode documentationMode = Microsoft.CodeAnalysis.DocumentationMode.Parse, Microsoft.CodeAnalysis.SourceCodeKind kind = Microsoft.CodeAnalysis.SourceCodeKind.Regular, System.Collections.Generic.IEnumerable<string> preprocessorSymbols = null) -> void
Microsoft.CodeAnalysis.CSharp.CSharpParseOptions.SpecifiedLanguageVersion.get -> Microsoft.CodeAnalysis.CSharp.LanguageVersion
Microsoft.CodeAnalysis.CSharp.Conversion.IsTupleConversion.get -> bool Microsoft.CodeAnalysis.CSharp.Conversion.IsTupleConversion.get -> bool
Microsoft.CodeAnalysis.CSharp.Conversion.IsTupleLiteralConversion.get -> bool Microsoft.CodeAnalysis.CSharp.Conversion.IsTupleLiteralConversion.get -> bool
Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp7 = 7 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp7 = 7 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion
Microsoft.CodeAnalysis.CSharp.LanguageVersion.Default = 0 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion
Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest = 2147483647 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion Microsoft.CodeAnalysis.CSharp.LanguageVersion.Latest = 2147483647 -> Microsoft.CodeAnalysis.CSharp.LanguageVersion
Microsoft.CodeAnalysis.CSharp.Syntax.CasePatternSwitchLabelSyntax Microsoft.CodeAnalysis.CSharp.Syntax.CasePatternSwitchLabelSyntax
Microsoft.CodeAnalysis.CSharp.Syntax.CasePatternSwitchLabelSyntax.Pattern.get -> Microsoft.CodeAnalysis.CSharp.Syntax.PatternSyntax Microsoft.CodeAnalysis.CSharp.Syntax.CasePatternSwitchLabelSyntax.Pattern.get -> Microsoft.CodeAnalysis.CSharp.Syntax.PatternSyntax
......
...@@ -1171,7 +1171,7 @@ public void ArgumentParsing() ...@@ -1171,7 +1171,7 @@ public void ArgumentParsing()
[Fact] [Fact]
public void LangVersion() public void LangVersion()
{ {
LanguageVersion defaultVersion = LanguageVersion.Latest.MapLatestToVersion(); LanguageVersion defaultVersion = LanguageVersion.Default.MapSpecifiedToEffectiveVersion();
var parsedArgs = DefaultParse(new[] { "/langversion:1", "a.cs" }, _baseDirectory); var parsedArgs = DefaultParse(new[] { "/langversion:1", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(); parsedArgs.Errors.Verify();
...@@ -1215,13 +1215,12 @@ public void LangVersion() ...@@ -1215,13 +1215,12 @@ public void LangVersion()
parsedArgs = DefaultParse(new[] { "/langversion:default", "a.cs" }, _baseDirectory); parsedArgs = DefaultParse(new[] { "/langversion:default", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify(); parsedArgs.Errors.Verify();
Assert.Equal(LanguageVersion.Default, parsedArgs.ParseOptions.SpecifiedLanguageVersion);
Assert.Equal(defaultVersion, parsedArgs.ParseOptions.LanguageVersion); Assert.Equal(defaultVersion, parsedArgs.ParseOptions.LanguageVersion);
parsedArgs = DefaultParse(new[] { "/langversion:latest", "a.cs" }, _baseDirectory); parsedArgs = DefaultParse(new[] { "/langversion:latest", "a.cs" }, _baseDirectory);
parsedArgs.Errors.Verify( parsedArgs.Errors.Verify();
// error CS1617: Invalid option 'latest' for /langversion; must be ISO-1, ISO-2, Default or an integer in range 1 to 6. Assert.Equal(LanguageVersion.Latest, parsedArgs.ParseOptions.SpecifiedLanguageVersion);
Diagnostic(ErrorCode.ERR_BadCompatMode).WithArguments("latest").WithLocation(1, 1)
);
Assert.Equal(defaultVersion, parsedArgs.ParseOptions.LanguageVersion); Assert.Equal(defaultVersion, parsedArgs.ParseOptions.LanguageVersion);
parsedArgs = DefaultParse(new[] { "/langversion:iso-1", "a.cs" }, _baseDirectory); parsedArgs = DefaultParse(new[] { "/langversion:iso-1", "a.cs" }, _baseDirectory);
......
...@@ -69,7 +69,8 @@ public void TestFieldsForEqualsAndGetHashCode() ...@@ -69,7 +69,8 @@ public void TestFieldsForEqualsAndGetHashCode()
"Features", "Features",
"LanguageVersion", "LanguageVersion",
"PreprocessorSymbolNames", "PreprocessorSymbolNames",
"PreprocessorSymbols"); "PreprocessorSymbols",
"SpecifiedLanguageVersion");
} }
} }
} }
...@@ -100,7 +100,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -100,7 +100,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim parseDocumentationComments As Boolean = False ' Don't just null check documentationFileName because we want to do this even if the file name is invalid. Dim parseDocumentationComments As Boolean = False ' Don't just null check documentationFileName because we want to do this even if the file name is invalid.
Dim outputKind As OutputKind = OutputKind.ConsoleApplication Dim outputKind As OutputKind = OutputKind.ConsoleApplication
Dim ssVersion As SubsystemVersion = SubsystemVersion.None Dim ssVersion As SubsystemVersion = SubsystemVersion.None
Dim languageVersion As LanguageVersion = LanguageVersion.Latest.MapLatestToVersion() Dim languageVersion As LanguageVersion = LanguageVersion.Default
Dim mainTypeName As String = Nothing Dim mainTypeName As String = Nothing
Dim win32ManifestFile As String = Nothing Dim win32ManifestFile As String = Nothing
Dim win32ResourceFile As String = Nothing Dim win32ResourceFile As String = Nothing
...@@ -804,7 +804,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -804,7 +804,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Case "15", "15.0" Case "15", "15.0"
languageVersion = LanguageVersion.VisualBasic15 languageVersion = LanguageVersion.VisualBasic15
Case "default" Case "default"
languageVersion = LanguageVersion.Latest.MapLatestToVersion() languageVersion = LanguageVersion.Default
Case "latest"
languageVersion = LanguageVersion.Latest
Case Else Case Else
AddDiagnostic(diagnostics, ERRID.ERR_InvalidSwitchValue, "langversion", value) AddDiagnostic(diagnostics, ERRID.ERR_InvalidSwitchValue, "langversion", value)
End Select End Select
......
...@@ -252,7 +252,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -252,7 +252,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
' The My template regularly makes use of more recent language features. Care is ' The My template regularly makes use of more recent language features. Care is
' taken to ensure these are compatible with 2.0 runtimes so there is no danger ' taken to ensure these are compatible with 2.0 runtimes so there is no danger
' with allowing the newer syntax here. ' with allowing the newer syntax here.
Dim options = parseOptions.WithLanguageVersion(LanguageVersion.Latest) Dim options = parseOptions.WithLanguageVersion(LanguageVersion.Default)
tree = VisualBasicSyntaxTree.ParseText(text, options:=options, isMyTemplate:=True) tree = VisualBasicSyntaxTree.ParseText(text, options:=options, isMyTemplate:=True)
If tree.GetDiagnostics().Any() Then If tree.GetDiagnostics().Any() Then
...@@ -481,7 +481,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -481,7 +481,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End If End If
Next Next
Return If(result, VisualBasicParseOptions.Default.LanguageVersion) Return If(result, VisualBasicParseOptions.Default.LanguageVersion.MapSpecifiedToEffectiveVersion)
End Function End Function
''' <summary> ''' <summary>
......
...@@ -7,6 +7,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -7,6 +7,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' Supported Visual Basic language versions. ''' Supported Visual Basic language versions.
''' </summary> ''' </summary>
Public Enum LanguageVersion Public Enum LanguageVersion
[Default] = 0
VisualBasic9 = 9 VisualBasic9 = 9
VisualBasic10 = 10 VisualBasic10 = 10
VisualBasic11 = 11 VisualBasic11 = 11
...@@ -57,8 +58,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -57,8 +58,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Function End Function
<Extension> <Extension>
Friend Function MapLatestToVersion(version As LanguageVersion) As LanguageVersion Friend Function MapSpecifiedToEffectiveVersion(version As LanguageVersion) As LanguageVersion
Return If(version = LanguageVersion.Latest, LanguageVersion.VisualBasic15, version) Select Case version
Case LanguageVersion.Latest, LanguageVersion.Default
Return LanguageVersion.VisualBasic15
Case Else
Return version
End Select
End Function End Function
End Module End Module
......
Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Default = 0 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion
Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest = 2147483647 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest = 2147483647 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion
Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic15 = 15 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic15 = 15 -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion
Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions.New(languageVersion As Microsoft.CodeAnalysis.VisualBasic.LanguageVersion = Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Latest, documentationMode As Microsoft.CodeAnalysis.DocumentationMode = Microsoft.CodeAnalysis.DocumentationMode.Parse, kind As Microsoft.CodeAnalysis.SourceCodeKind = Microsoft.CodeAnalysis.SourceCodeKind.Regular, preprocessorSymbols As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, Object)) = Nothing) -> Void Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions.New(languageVersion As Microsoft.CodeAnalysis.VisualBasic.LanguageVersion = Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.Default, documentationMode As Microsoft.CodeAnalysis.DocumentationMode = Microsoft.CodeAnalysis.DocumentationMode.Parse, kind As Microsoft.CodeAnalysis.SourceCodeKind = Microsoft.CodeAnalysis.SourceCodeKind.Regular, preprocessorSymbols As System.Collections.Generic.IEnumerable(Of System.Collections.Generic.KeyValuePair(Of String, Object)) = Nothing) -> Void
Microsoft.CodeAnalysis.VisualBasic.VisualBasicParseOptions.SpecifiedLanguageVersion() -> Microsoft.CodeAnalysis.VisualBasic.LanguageVersion
\ No newline at end of file
...@@ -5081,7 +5081,8 @@ ...@@ -5081,7 +5081,8 @@
referenced metadata files. referenced metadata files.
import_list:namespace,... import_list:namespace,...
/langversion:&lt;number&gt; Specify language version: /langversion:&lt;number&gt; Specify language version:
9|9.0|10|10.0|11|11.0|12|12.0|14|14.0|15|15.0|default 9|9.0|10|10.0|11|11.0|12|12.0|14|14.0|15|
15.0|default|latest
/optionexplicit[+|-] Require explicit declaration of variables. /optionexplicit[+|-] Require explicit declaration of variables.
/optioninfer[+|-] Allow type inference of variables. /optioninfer[+|-] Allow type inference of variables.
/rootnamespace:&lt;string&gt; Specifies the root Namespace for all type /rootnamespace:&lt;string&gt; Specifies the root Namespace for all type
......
...@@ -19,6 +19,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -19,6 +19,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private _features As ImmutableDictionary(Of String, String) Private _features As ImmutableDictionary(Of String, String)
Private _preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) Private _preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object))
Private _specifiedLanguageVersion As LanguageVersion
Private _languageVersion As LanguageVersion Private _languageVersion As LanguageVersion
''' <summary> ''' <summary>
...@@ -29,19 +30,19 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -29,19 +30,19 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' <param name="kind">The kind of source code.<see cref="SourceCodeKind"/></param> ''' <param name="kind">The kind of source code.<see cref="SourceCodeKind"/></param>
''' <param name="preprocessorSymbols">An enumerable sequence of KeyValuePair representing preprocessor symbols.</param> ''' <param name="preprocessorSymbols">An enumerable sequence of KeyValuePair representing preprocessor symbols.</param>
Public Sub New( Public Sub New(
Optional languageVersion As LanguageVersion = LanguageVersion.Latest, Optional languageVersion As LanguageVersion = LanguageVersion.Default,
Optional documentationMode As DocumentationMode = DocumentationMode.Parse, Optional documentationMode As DocumentationMode = DocumentationMode.Parse,
Optional kind As SourceCodeKind = SourceCodeKind.Regular, Optional kind As SourceCodeKind = SourceCodeKind.Regular,
Optional preprocessorSymbols As IEnumerable(Of KeyValuePair(Of String, Object)) = Nothing) Optional preprocessorSymbols As IEnumerable(Of KeyValuePair(Of String, Object)) = Nothing)
MyClass.New(languageVersion.MapLatestToVersion(), MyClass.New(languageVersion,
documentationMode, documentationMode,
kind, kind,
If(preprocessorSymbols Is Nothing, DefaultPreprocessorSymbols, ImmutableArray.CreateRange(preprocessorSymbols)), If(preprocessorSymbols Is Nothing, DefaultPreprocessorSymbols, ImmutableArray.CreateRange(preprocessorSymbols)),
ImmutableDictionary(Of String, String).Empty) ImmutableDictionary(Of String, String).Empty)
' We test the mapped value, _languageVersion, rather than the parameter, languageVersion, ' We test the mapped value, _languageVersion, rather than the parameter, languageVersion,
' which has Not had "Latest" mapped to the latest version yet. ' which has not had "Latest" mapped to the latest version yet.
If Not _languageVersion.IsValid Then If Not _languageVersion.IsValid Then
Throw New ArgumentOutOfRangeException(NameOf(languageVersion)) Throw New ArgumentOutOfRangeException(NameOf(languageVersion))
End If End If
...@@ -66,7 +67,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -66,7 +67,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
If(preprocessorSymbols Is Nothing, DefaultPreprocessorSymbols, ImmutableArray.CreateRange(preprocessorSymbols)), If(preprocessorSymbols Is Nothing, DefaultPreprocessorSymbols, ImmutableArray.CreateRange(preprocessorSymbols)),
features) features)
If Not languageVersion.IsValid Then ' We test the mapped value, _languageVersion, rather than the parameter, languageVersion,
' which has not had "Latest" mapped to the latest version yet.
If Not _languageVersion.IsValid Then
Throw New ArgumentOutOfRangeException(NameOf(languageVersion)) Throw New ArgumentOutOfRangeException(NameOf(languageVersion))
End If End If
...@@ -114,14 +117,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -114,14 +117,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
MyBase.New(kind, documentationMode) MyBase.New(kind, documentationMode)
Debug.Assert(Not preprocessorSymbols.IsDefault) Debug.Assert(Not preprocessorSymbols.IsDefault)
_languageVersion = languageVersion _specifiedLanguageVersion = languageVersion
_languageVersion = languageVersion.MapSpecifiedToEffectiveVersion
_preprocessorSymbols = preprocessorSymbols _preprocessorSymbols = preprocessorSymbols
_features = features _features = features
End Sub End Sub
Private Sub New(other As VisualBasicParseOptions) Private Sub New(other As VisualBasicParseOptions)
MyClass.New( MyClass.New(
languageVersion:=other._languageVersion, languageVersion:=other._specifiedLanguageVersion,
documentationMode:=other.DocumentationMode, documentationMode:=other.DocumentationMode,
kind:=other.Kind, kind:=other.Kind,
preprocessorSymbols:=other._preprocessorSymbols, preprocessorSymbols:=other._preprocessorSymbols,
...@@ -139,7 +143,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -139,7 +143,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Property End Property
''' <summary> ''' <summary>
''' Returns the parser language version. ''' Returns the specified parser language version.
''' </summary>
Public ReadOnly Property SpecifiedLanguageVersion As LanguageVersion
Get
Return _specifiedLanguageVersion
End Get
End Property
''' <summary>
''' Returns the effective parser language version.
''' </summary> ''' </summary>
Public ReadOnly Property LanguageVersion As LanguageVersion Public ReadOnly Property LanguageVersion As LanguageVersion
Get Get
...@@ -174,16 +187,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -174,16 +187,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' <param name="version">The parser language version.</param> ''' <param name="version">The parser language version.</param>
''' <returns>A new instance of VisualBasicParseOptions if different language version is different; otherwise current instance.</returns> ''' <returns>A new instance of VisualBasicParseOptions if different language version is different; otherwise current instance.</returns>
Public Shadows Function WithLanguageVersion(version As LanguageVersion) As VisualBasicParseOptions Public Shadows Function WithLanguageVersion(version As LanguageVersion) As VisualBasicParseOptions
version = version.MapLatestToVersion() If version = _specifiedLanguageVersion Then
If version = _languageVersion Then
Return Me Return Me
End If End If
If Not version.IsValid Then Dim effectiveVersion = version.MapSpecifiedToEffectiveVersion()
If Not effectiveVersion.IsValid Then
Throw New ArgumentOutOfRangeException(NameOf(version)) Throw New ArgumentOutOfRangeException(NameOf(version))
End If End If
Return New VisualBasicParseOptions(Me) With {._languageVersion = version} Return New VisualBasicParseOptions(Me) With {._specifiedLanguageVersion = version, ._languageVersion = effectiveVersion}
End Function End Function
''' <summary> ''' <summary>
...@@ -311,7 +324,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -311,7 +324,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Return False Return False
End If End If
If Me.LanguageVersion <> other.LanguageVersion Then If Me.SpecifiedLanguageVersion <> other.SpecifiedLanguageVersion Then
Return False Return False
End If End If
...@@ -336,7 +349,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ...@@ -336,7 +349,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' </summary> ''' </summary>
''' <returns>A hashcode representing this instance.</returns> ''' <returns>A hashcode representing this instance.</returns>
Public Overrides Function GetHashCode() As Integer Public Overrides Function GetHashCode() As Integer
Return Hash.Combine(MyBase.GetHashCodeHelper(), CInt(Me.LanguageVersion)) Return Hash.Combine(MyBase.GetHashCodeHelper(), CInt(Me.SpecifiedLanguageVersion))
End Function End Function
End Class End Class
End Namespace End Namespace
...@@ -1008,10 +1008,12 @@ a.vb ...@@ -1008,10 +1008,12 @@ a.vb
parsedArgs = DefaultParse({"/langVERSION:default", "a.vb"}, _baseDirectory) parsedArgs = DefaultParse({"/langVERSION:default", "a.vb"}, _baseDirectory)
parsedArgs.Errors.Verify() parsedArgs.Errors.Verify()
Assert.Equal(LanguageVersion.Default, parsedArgs.ParseOptions.SpecifiedLanguageVersion)
Assert.Equal(LanguageVersion.VisualBasic15, parsedArgs.ParseOptions.LanguageVersion) Assert.Equal(LanguageVersion.VisualBasic15, parsedArgs.ParseOptions.LanguageVersion)
parsedArgs = DefaultParse({"/langVERSION:latest", "a.vb"}, _baseDirectory) parsedArgs = DefaultParse({"/langVERSION:latest", "a.vb"}, _baseDirectory)
parsedArgs.Errors.Verify(Diagnostic(ERRID.ERR_InvalidSwitchValue).WithArguments("langversion", "latest").WithLocation(1, 1)) parsedArgs.Errors.Verify()
Assert.Equal(LanguageVersion.Latest, parsedArgs.ParseOptions.SpecifiedLanguageVersion)
Assert.Equal(LanguageVersion.VisualBasic15, parsedArgs.ParseOptions.LanguageVersion) Assert.Equal(LanguageVersion.VisualBasic15, parsedArgs.ParseOptions.LanguageVersion)
' default: "current version" ' default: "current version"
......
...@@ -270,7 +270,8 @@ End Module" ...@@ -270,7 +270,8 @@ End Module"
For Each version In {LanguageVersion.VisualBasic9, LanguageVersion.VisualBasic10, For Each version In {LanguageVersion.VisualBasic9, LanguageVersion.VisualBasic10,
LanguageVersion.VisualBasic11, LanguageVersion.VisualBasic12, LanguageVersion.VisualBasic11, LanguageVersion.VisualBasic12,
LanguageVersion.VisualBasic14, VisualBasicParseOptions.Default.LanguageVersion} LanguageVersion.VisualBasic14, LanguageVersion.VisualBasic15,
LanguageVersion.Default, LanguageVersion.Latest}
ParseAndVerify(source, version, False, Nothing) ParseAndVerify(source, version, False, Nothing)
Next Next
End Sub End Sub
...@@ -294,7 +295,8 @@ End Namespace" ...@@ -294,7 +295,8 @@ End Namespace"
Diagnostic(ERRID.ERR_LanguageVersion, "Global").WithArguments($"{CInt(version)}.0", "declaring a Global namespace").WithLocation(4, 11)) Diagnostic(ERRID.ERR_LanguageVersion, "Global").WithArguments($"{CInt(version)}.0", "declaring a Global namespace").WithLocation(4, 11))
Next Next
For Each version In {LanguageVersion.VisualBasic11, LanguageVersion.VisualBasic12, LanguageVersion.VisualBasic14, VisualBasicParseOptions.Default.LanguageVersion} For Each version In {LanguageVersion.VisualBasic11, LanguageVersion.VisualBasic12, LanguageVersion.VisualBasic14, LanguageVersion.VisualBasic15,
LanguageVersion.Default, LanguageVersion.Latest}
ParseAndVerify(source, version, False, Nothing) ParseAndVerify(source, version, False, Nothing)
Next Next
End Sub End Sub
......
...@@ -30,9 +30,14 @@ Public Class VisualBasicParseOptionsTests ...@@ -30,9 +30,14 @@ Public Class VisualBasicParseOptionsTests
Dim oldOpt1 = VisualBasicParseOptions.Default Dim oldOpt1 = VisualBasicParseOptions.Default
Dim newOpt1 = oldOpt1.WithLanguageVersion(LanguageVersion.Latest) Dim newOpt1 = oldOpt1.WithLanguageVersion(LanguageVersion.Latest)
Dim newOpt2 = newOpt1.WithLanguageVersion(LanguageVersion.Latest) Dim newOpt2 = newOpt1.WithLanguageVersion(LanguageVersion.Latest)
Assert.Equal(LanguageVersion.Latest.MapLatestToVersion, oldOpt1.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, oldOpt1.LanguageVersion)
Assert.Equal(LanguageVersion.Latest.MapLatestToVersion, newOpt1.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt1.LanguageVersion)
Assert.Equal(LanguageVersion.Latest.MapLatestToVersion, newOpt2.LanguageVersion) Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt2.LanguageVersion)
newOpt1 = oldOpt1.WithLanguageVersion(LanguageVersion.Default)
newOpt2 = newOpt1.WithLanguageVersion(LanguageVersion.Default)
Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, oldOpt1.LanguageVersion)
Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt1.LanguageVersion)
Assert.Equal(LanguageVersion.Latest.MapSpecifiedToEffectiveVersion, newOpt2.LanguageVersion)
End Sub End Sub
<Fact> <Fact>
...@@ -255,6 +260,7 @@ Public Class VisualBasicParseOptionsTests ...@@ -255,6 +260,7 @@ Public Class VisualBasicParseOptionsTests
"Features", "Features",
"LanguageVersion", "LanguageVersion",
"PreprocessorSymbolNames", "PreprocessorSymbolNames",
"PreprocessorSymbols") "PreprocessorSymbols",
"SpecifiedLanguageVersion")
End Sub End Sub
End Class End Class
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册