From ac6e7655361c36d62deb071c8c9d4a52d2bc0843 Mon Sep 17 00:00:00 2001 From: TomasMatousek Date: Mon, 9 Jun 2014 19:32:37 -0700 Subject: [PATCH] Move serialization from CompilationOptions and ParseOptions to dedicated SerializableCompilationOptions and SerializableParseOptions wrappers. (changeset 1274332) --- .../CSharp/Source/CSharpCodeAnalysis.csproj | 2 + .../CSharp/Source/CSharpCompilationOptions.cs | 41 +----- .../CSharp/Source/CSharpParseOptions.cs | 119 ++++----------- .../Source/CommandLine/CommandLineParser.cs | 3 +- .../CSharpSerializableCompilationOptions.cs | 84 +++++++++++ .../CSharpSerializableParseOptions.cs | 51 +++++++ .../CSharpCompilationOptionsTests.cs | 14 ++ .../Symbol/Compilation/CompilationAPITests.cs | 59 -------- .../Syntax/CSharpCompilerSyntaxTest.csproj | 2 +- ...onsTests.cs => CSharpParseOptionsTests.cs} | 13 +- .../Test/Syntax/Parsing/ParsingTests.cs | 4 - Src/Compilers/Core/Source/CodeAnalysis.csproj | 2 + .../Core/Source/Collections/ArrayElement.cs | 3 - .../Core/Source/Compilation/Compilation.cs | 2 +- .../Source/Compilation/CompilationOptions.cs | 119 +-------------- .../Core/Source/Compilation/ParseOptions.cs | 29 +--- .../Source/Compilation/SubsystemVersion.cs | 1 - .../MetadataReferenceProperties.cs | 18 +-- .../Core/Source/NonPortable/ParseOptions.cs | 117 +++++++++++++++ .../SerializableCompilationOptions.cs | 74 ++++++++++ .../NonPortable/SerializableParseOptions.cs | 32 +++++ .../CSharp/CSharpCompilerTestUtilities.csproj | 2 - .../Test/Utilities/CSharp/CSharpParser.cs | 20 --- .../CSharp/CSharpSyntaxNodeKindProvider.cs | 14 -- .../Source/BasicCodeAnalysis.vbproj | 2 + .../Source/CommandLine/CommandLineParser.vb | 5 +- ...sualBasicSerializableCompilationOptions.vb | 96 +++++++++++++ .../VisualBasicSerializableParseOptions.vb | 79 ++++++++++ .../Source/VisualBasicCompilationOptions.vb | 71 ++------- .../Source/VisualBasicParseOptions.vb | 136 +++++------------- .../VisualBasicCompilationOptionsTests.vb | 72 ++++++---- .../Syntax/BasicCompilerSyntaxTest.vbproj | 2 +- ...sts.vb => VisualBasicParseOptionsTests.vb} | 46 +++--- Src/Test/Utilities/TestBase.cs | 21 +++ .../Core/Workspace/Solution/ProjectInfo.cs | 4 +- Src/Workspaces/CoreTest/SolutionTests.cs | 2 +- 36 files changed, 740 insertions(+), 621 deletions(-) create mode 100644 Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableCompilationOptions.cs create mode 100644 Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableParseOptions.cs rename Src/Compilers/CSharp/Test/Syntax/Parsing/{ParseOptionsTests.cs => CSharpParseOptionsTests.cs} (89%) create mode 100644 Src/Compilers/Core/Source/NonPortable/ParseOptions.cs create mode 100644 Src/Compilers/Core/Source/NonPortable/SerializableCompilationOptions.cs create mode 100644 Src/Compilers/Core/Source/NonPortable/SerializableParseOptions.cs delete mode 100644 Src/Compilers/Test/Utilities/CSharp/CSharpParser.cs delete mode 100644 Src/Compilers/Test/Utilities/CSharp/CSharpSyntaxNodeKindProvider.cs create mode 100644 Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableCompilationOptions.vb create mode 100644 Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableParseOptions.vb rename Src/Compilers/VisualBasic/Test/Syntax/Parser/{ParseOptionsTests.vb => VisualBasicParseOptionsTests.vb} (81%) diff --git a/Src/Compilers/CSharp/Source/CSharpCodeAnalysis.csproj b/Src/Compilers/CSharp/Source/CSharpCodeAnalysis.csproj index 1b9c443a3c943..8a257557cbafe 100644 --- a/Src/Compilers/CSharp/Source/CSharpCodeAnalysis.csproj +++ b/Src/Compilers/CSharp/Source/CSharpCodeAnalysis.csproj @@ -379,6 +379,7 @@ + @@ -465,6 +466,7 @@ + diff --git a/Src/Compilers/CSharp/Source/CSharpCompilationOptions.cs b/Src/Compilers/CSharp/Source/CSharpCompilationOptions.cs index 3b17f5e294a46..9d35276b0dce1 100644 --- a/Src/Compilers/CSharp/Source/CSharpCompilationOptions.cs +++ b/Src/Compilers/CSharp/Source/CSharpCompilationOptions.cs @@ -4,9 +4,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; -using System.Runtime.Serialization; using Microsoft.CodeAnalysis.CSharp.Symbols; -using Microsoft.CodeAnalysis.CSharp.Syntax; using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.CSharp @@ -16,13 +14,8 @@ namespace Microsoft.CodeAnalysis.CSharp /// whether to emit an executable or a library, whether to optimize /// generated code, and so on. /// - [Serializable] - public sealed class CSharpCompilationOptions : CompilationOptions, IEquatable, ISerializable + public sealed class CSharpCompilationOptions : CompilationOptions, IEquatable { - private const string AllowUnsafeString = "AllowUnsafe"; - private const string UsingsString = "Usings"; - private const string RuntimeMetadataVersionString = "RuntimeMetadataVersion"; - /// /// Allow unsafe regions (i.e. unsafe modifiers on members and unsafe blocks). /// @@ -67,14 +60,14 @@ public CSharpCompilationOptions( AssemblyIdentityComparer assemblyIdentityComparer = null, StrongNameProvider strongNameProvider = null) : this(outputKind, moduleName, mainTypeName, scriptClassName, usings, optimize, checkOverflow, allowUnsafe, - cryptoKeyContainer, cryptoKeyFile, delaySign, fileAlignment, baseAddress, platform, generalDiagnosticOption, warningLevel, + cryptoKeyContainer, cryptoKeyFile, delaySign, fileAlignment, baseAddress, platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions, highEntropyVirtualAddressSpace, debugInformationKind, subsystemVersion, runtimeMetadataVersion, concurrentBuild, xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, metadataReferenceProvider, assemblyIdentityComparer, strongNameProvider, MetadataImportOptions.Public, features: ImmutableArray.Empty) { } // Expects correct arguments. - private CSharpCompilationOptions( + internal CSharpCompilationOptions( OutputKind outputKind, string moduleName, string mainTypeName, @@ -109,7 +102,9 @@ private CSharpCompilationOptions( platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions, highEntropyVirtualAddressSpace, debugInformationKind, subsystemVersion, concurrentBuild, xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, metadataReferenceProvider, assemblyIdentityComparer, strongNameProvider, metadataImportOptions, features) { - Initialize(usings, allowUnsafe, runtimeMetadataVersion); + this.Usings = usings.AsImmutableOrEmpty(); + this.AllowUnsafe = allowUnsafe; + this.RuntimeMetadataVersion = runtimeMetadataVersion; } private CSharpCompilationOptions(CSharpCompilationOptions other) : this( @@ -146,30 +141,6 @@ private CSharpCompilationOptions(CSharpCompilationOptions other) : this( { } - private void Initialize(IEnumerable usings, bool allowUnsafe, string runtimeMetadataVersion) - { - this.Usings = usings.AsImmutableOrEmpty(); - this.AllowUnsafe = allowUnsafe; - this.RuntimeMetadataVersion = runtimeMetadataVersion; - } - - private CSharpCompilationOptions(SerializationInfo info, StreamingContext context) - : base(info, context) - { - Initialize( - (string[])info.GetValue(UsingsString, typeof(string[])), - info.GetBoolean(AllowUnsafeString), - info.GetString(RuntimeMetadataVersionString)); - } - - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - base.GetObjectData(info, context); - info.AddValue(UsingsString, this.Usings.ToArray()); - info.AddValue(AllowUnsafeString, this.AllowUnsafe); - info.AddValue(RuntimeMetadataVersionString, this.RuntimeMetadataVersion); - } - public new CSharpCompilationOptions WithOutputKind(OutputKind kind) { if (kind == this.OutputKind) diff --git a/Src/Compilers/CSharp/Source/CSharpParseOptions.cs b/Src/Compilers/CSharp/Source/CSharpParseOptions.cs index 4923b8042b0f8..8de3cb4075463 100644 --- a/Src/Compilers/CSharp/Source/CSharpParseOptions.cs +++ b/Src/Compilers/CSharp/Source/CSharpParseOptions.cs @@ -4,19 +4,14 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; -using Microsoft.CodeAnalysis.CSharp.Symbols; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; -using System.Runtime.Serialization; namespace Microsoft.CodeAnalysis.CSharp { /// /// This class stores several source parsing related options and offers access to their values. /// - [Serializable] - public sealed class CSharpParseOptions : ParseOptions, IEquatable, ISerializable + public sealed class CSharpParseOptions : ParseOptions, IEquatable { /// /// The default parse options. @@ -26,9 +21,9 @@ public sealed class CSharpParseOptions : ParseOptions, IEquatable /// Gets the language version. /// - public readonly LanguageVersion LanguageVersion; + public LanguageVersion LanguageVersion { get; private set; } - internal readonly ImmutableArray PreprocessorSymbols; + internal ImmutableArray PreprocessorSymbols { get; private set; } /// /// Gets the names of defined preprocessor symbols. @@ -38,23 +33,12 @@ public override IEnumerable PreprocessorSymbolNames get { return PreprocessorSymbols; } } - // NOTE: warnaserror[+|-], warnaserror[+|-]:, unsafe[+|-], warn:, nowarn: - public CSharpParseOptions( LanguageVersion languageVersion = LanguageVersion.CSharp6, DocumentationMode documentationMode = DocumentationMode.Parse, SourceCodeKind kind = SourceCodeKind.Regular, - params string[] preprocessorSymbols) - : this(languageVersion, documentationMode, kind, preprocessorSymbols.AsImmutableOrEmpty()) - { - } - - public CSharpParseOptions( - LanguageVersion languageVersion = LanguageVersion.CSharp6, - DocumentationMode documentationMode = DocumentationMode.Parse, - SourceCodeKind kind = SourceCodeKind.Regular, - ImmutableArray preprocessorSymbols = default(ImmutableArray)) - : this(languageVersion, documentationMode, kind, preprocessorSymbols.NullToEmpty(), privateCtor: true) + IEnumerable preprocessorSymbols = null) + : this(languageVersion, documentationMode, kind, preprocessorSymbols.ToImmutableArrayOrEmpty()) { if (!languageVersion.IsValid()) { @@ -66,7 +50,7 @@ public CSharpParseOptions( throw new ArgumentOutOfRangeException("kind"); } - if (!preprocessorSymbols.IsDefaultOrEmpty) + if (preprocessorSymbols != null) { foreach (var preprocessorSymbol in preprocessorSymbols) { @@ -78,13 +62,20 @@ public CSharpParseOptions( } } + private CSharpParseOptions(CSharpParseOptions other) : this( + languageVersion: other.LanguageVersion, + documentationMode: other.DocumentationMode, + kind: other.Kind, + preprocessorSymbols: other.PreprocessorSymbols) + { + } + // No validation internal CSharpParseOptions( LanguageVersion languageVersion, DocumentationMode documentationMode, SourceCodeKind kind, - ImmutableArray preprocessorSymbols, - bool privateCtor) //dummy param to distinguish from public ctor + ImmutableArray preprocessorSymbols) : base(kind, documentationMode) { Debug.Assert(!preprocessorSymbols.IsDefault); @@ -104,23 +95,7 @@ internal CSharpParseOptions( throw new ArgumentOutOfRangeException("kind"); } - return new CSharpParseOptions( - this.LanguageVersion, - this.DocumentationMode, - kind, - this.PreprocessorSymbols, - privateCtor: true - ); - } - - protected override ParseOptions CommonWithKind(SourceCodeKind kind) - { - return WithKind(kind); - } - - protected override ParseOptions CommonWithDocumentationMode(DocumentationMode documentationMode) - { - return WithDocumentationMode(documentationMode); + return new CSharpParseOptions(this) { Kind = kind }; } public CSharpParseOptions WithLanguageVersion(LanguageVersion version) @@ -135,13 +110,7 @@ public CSharpParseOptions WithLanguageVersion(LanguageVersion version) throw new ArgumentOutOfRangeException("version"); } - return new CSharpParseOptions( - version, - this.DocumentationMode, - this.Kind, - this.PreprocessorSymbols, - privateCtor: true - ); + return new CSharpParseOptions(this) { LanguageVersion = version }; } public CSharpParseOptions WithPreprocessorSymbols(IEnumerable preprocessorSymbols) @@ -151,7 +120,7 @@ public CSharpParseOptions WithPreprocessorSymbols(IEnumerable preprocess public CSharpParseOptions WithPreprocessorSymbols(params string[] preprocessorSymbols) { - return WithPreprocessorSymbols(ImmutableArray.Create(preprocessorSymbols)); + return WithPreprocessorSymbols(ImmutableArray.Create(preprocessorSymbols)); } public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray symbols) @@ -166,13 +135,7 @@ public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray symbols return this; } - return new CSharpParseOptions( - this.LanguageVersion, - this.DocumentationMode, - this.Kind, - symbols, - privateCtor: true - ); + return new CSharpParseOptions(this) { PreprocessorSymbols = symbols }; } public new CSharpParseOptions WithDocumentationMode(DocumentationMode documentationMode) @@ -187,13 +150,17 @@ public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray symbols throw new ArgumentOutOfRangeException("documentationMode"); } - return new CSharpParseOptions( - this.LanguageVersion, - documentationMode, - this.Kind, - this.PreprocessorSymbols, - privateCtor: true - ); + return new CSharpParseOptions(this) { DocumentationMode = documentationMode }; + } + + protected override ParseOptions CommonWithKind(SourceCodeKind kind) + { + return WithKind(kind); + } + + protected override ParseOptions CommonWithDocumentationMode(DocumentationMode documentationMode) + { + return WithDocumentationMode(documentationMode); } public override bool Equals(object obj) @@ -221,32 +188,6 @@ public override int GetHashCode() return Hash.Combine(base.GetHashCodeHelper(), Hash.Combine((int)this.LanguageVersion, 0)); - } - - #region "serialization" - - private CSharpParseOptions(SerializationInfo info, StreamingContext context) - : base(info, context) - { - //public readonly LanguageVersion LanguageVersion; - this.LanguageVersion = (LanguageVersion)info.GetValue("LanguageVersion", typeof(LanguageVersion)); - - //internal readonly ImmutableArray PreprocessorSymbols; - this.PreprocessorSymbols = info.GetArray("PreprocessorSymbols"); - } - - public override void GetObjectData(SerializationInfo info, StreamingContext context) - { - base.GetObjectData(info, context); - - //public readonly LanguageVersion LanguageVersion; - info.AddValue("LanguageVersion", this.LanguageVersion, typeof(LanguageVersion)); - - //internal readonly ImmutableArray PreprocessorSymbols; - info.AddArray("PreprocessorSymbols", this.PreprocessorSymbols); - } - - #endregion } } \ No newline at end of file diff --git a/Src/Compilers/CSharp/Source/CommandLine/CommandLineParser.cs b/Src/Compilers/CSharp/Source/CommandLine/CommandLineParser.cs index 2e6ced33948d4..bc1a805b964fa 100644 --- a/Src/Compilers/CSharp/Source/CommandLine/CommandLineParser.cs +++ b/Src/Compilers/CSharp/Source/CommandLine/CommandLineParser.cs @@ -916,8 +916,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable ar languageVersion: languageVersion, preprocessorSymbols: defines.ToImmutableAndFree(), documentationMode: parseDocumentationComments ? DocumentationMode.Diagnose : DocumentationMode.None, - kind: SourceCodeKind.Regular, - privateCtor: true + kind: SourceCodeKind.Regular ); var scriptParseOptions = parseOptions.WithKind(SourceCodeKind.Script); diff --git a/Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableCompilationOptions.cs b/Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableCompilationOptions.cs new file mode 100644 index 0000000000000..2c996fead4eed --- /dev/null +++ b/Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableCompilationOptions.cs @@ -0,0 +1,84 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Linq; +using System.Runtime.Serialization; + +namespace Microsoft.CodeAnalysis.CSharp +{ + [Serializable] + public sealed class CSharpSerializableCompilationOptions : SerializableCompilationOptions + { + private const string AllowUnsafeString = "AllowUnsafe"; + private const string UsingsString = "Usings"; + private const string RuntimeMetadataVersionString = "RuntimeMetadataVersion"; + + private readonly CSharpCompilationOptions options; + + public CSharpSerializableCompilationOptions(CSharpCompilationOptions options) + { + if (options == null) + { + throw new ArgumentNullException("options"); + } + + this.options = options; + } + + private CSharpSerializableCompilationOptions(SerializationInfo info, StreamingContext context) + { + this.options = new CSharpCompilationOptions( + outputKind: (OutputKind)info.GetInt32(OutputKindString), + moduleName: info.GetString(ModuleNameString), + mainTypeName: info.GetString(MainTypeNameString), + scriptClassName: info.GetString(ScriptClassNameString), + usings: (string[])info.GetValue(UsingsString, typeof(string[])), + cryptoKeyContainer: info.GetString(CryptoKeyContainerString), + cryptoKeyFile: info.GetString(CryptoKeyFileString), + delaySign: (bool?)info.GetValue(DelaySignString, typeof(bool?)), + optimize: info.GetBoolean(OptimizeString), + checkOverflow: info.GetBoolean(CheckOverflowString), + allowUnsafe: info.GetBoolean(AllowUnsafeString), + fileAlignment: info.GetInt32(FileAlignmentString), + baseAddress: info.GetUInt64(BaseAddressString), + platform: (Platform)info.GetInt32(PlatformString), + generalDiagnosticOption: (ReportDiagnostic)info.GetInt32(GeneralDiagnosticOptionString), + warningLevel: info.GetInt32(WarningLevelString), + specificDiagnosticOptions: ((Dictionary)info.GetValue(SpecificDiagnosticOptionsString, typeof(Dictionary))).ToImmutableDictionary(), + highEntropyVirtualAddressSpace: info.GetBoolean(HighEntropyVirtualAddressSpaceString), + debugInformationKind: (DebugInformationKind)info.GetInt32(DebugInformationKindString), + subsystemVersion: SubsystemVersion.Create(info.GetInt32(SubsystemVersionMajorString), info.GetInt32(SubsystemVersionMinorString)), + runtimeMetadataVersion: info.GetString(RuntimeMetadataVersionString), + concurrentBuild: info.GetBoolean(ConcurrentBuildString), + xmlReferenceResolver: XmlFileResolver.Default, + sourceReferenceResolver: SourceFileResolver.Default, + metadataReferenceResolver: MetadataFileReferenceResolver.Default, + metadataReferenceProvider: MetadataFileReferenceProvider.Default, + assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default, + strongNameProvider: new DesktopStrongNameProvider(), + metadataImportOptions: (MetadataImportOptions)info.GetByte(MetadataImportOptionsString), + features: ((string[])info.GetValue(FeaturesString, typeof(string[]))).AsImmutable()); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + CommonGetObjectData(options, info, context); + + info.AddValue(UsingsString, options.Usings.ToArray()); + info.AddValue(AllowUnsafeString, options.AllowUnsafe); + info.AddValue(RuntimeMetadataVersionString, options.RuntimeMetadataVersion); + } + + public new CSharpCompilationOptions Options + { + get { return options; } + } + + protected override CompilationOptions CommonOptions + { + get { return options; } + } + } +} diff --git a/Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableParseOptions.cs b/Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableParseOptions.cs new file mode 100644 index 0000000000000..522ef7062b6a2 --- /dev/null +++ b/Src/Compilers/CSharp/Source/NonPortable/CSharpSerializableParseOptions.cs @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Roslyn.Utilities; +using System.Runtime.Serialization; + +namespace Microsoft.CodeAnalysis.CSharp +{ + [Serializable] + public sealed class CSharpSerializableParseOptions : SerializableParseOptions + { + private readonly CSharpParseOptions options; + + public CSharpSerializableParseOptions(CSharpParseOptions options) + { + if (options == null) + { + throw new ArgumentNullException("options"); + } + + this.options = options; + } + + public new ParseOptions Options + { + get { return options; } + } + + protected override ParseOptions CommonOptions + { + get { return options; } + } + + private CSharpSerializableParseOptions(SerializationInfo info, StreamingContext context) + { + this.options = new CSharpParseOptions( + languageVersion: (LanguageVersion)info.GetValue("LanguageVersion", typeof(LanguageVersion)), + documentationMode: (DocumentationMode)info.GetValue("DocumentationMode", typeof(DocumentationMode)), + kind: (SourceCodeKind)info.GetValue("Kind", typeof(SourceCodeKind)), + preprocessorSymbols: info.GetArray("PreprocessorSymbols")); + } + + public override void GetObjectData(SerializationInfo info, StreamingContext context) + { + CommonGetObjectData(options, info, context); + + info.AddValue("LanguageVersion", options.LanguageVersion, typeof(LanguageVersion)); + info.AddArray("PreprocessorSymbols", options.PreprocessorSymbols); + } + } +} diff --git a/Src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs b/Src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs index 5c38159d9acb7..464bb8ea315cb 100644 --- a/Src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs +++ b/Src/Compilers/CSharp/Test/Symbol/Compilation/CSharpCompilationOptionsTests.cs @@ -3,11 +3,14 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.IO; using System.Linq; +using System.Runtime.Serialization.Formatters.Binary; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Test.Utilities; using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; +using Roslyn.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.CSharp.UnitTests @@ -356,5 +359,16 @@ public void TestFieldsForEqualsAndGetHashCode() "RuntimeMetadataVersion", "Usings"); } + + [Fact] + public void Serializability() + { + VerifySerializability(new CSharpSerializableCompilationOptions(new CSharpCompilationOptions( + outputKind: OutputKind.WindowsApplication, + usings: new[] { "F", "G" }, + generalDiagnosticOption: ReportDiagnostic.Hidden, + specificDiagnosticOptions: new[] { KeyValuePair.Create("CS0001", ReportDiagnostic.Suppress) }, + subsystemVersion: SubsystemVersion.Windows2000))); + } } } diff --git a/Src/Compilers/CSharp/Test/Symbol/Compilation/CompilationAPITests.cs b/Src/Compilers/CSharp/Test/Symbol/Compilation/CompilationAPITests.cs index 784b6af7c540f..b7cdf8fac8225 100644 --- a/Src/Compilers/CSharp/Test/Symbol/Compilation/CompilationAPITests.cs +++ b/Src/Compilers/CSharp/Test/Symbol/Compilation/CompilationAPITests.cs @@ -178,7 +178,6 @@ public void EmitToMemoryStreams() } } - // Emit when parameter 'pdbfilename' is null and 'pdbstream' is not null [Fact] public void NegEmit() { @@ -1827,64 +1826,6 @@ public void AppConfig2() c2.VerifyDiagnostics(); } - [Fact] - public void CompilationOptions_Serialize() - { - var options = TestOptions.Dll; - - // Serialize the CompileOptions - MemoryStream o = SerializeToStream(TestOptions.Dll); - var dt = (CSharpCompilationOptions)DeserializeFromStream(o); - - // resolvers are not serializable - dt = dt. - WithMetadataReferenceResolver(options.MetadataReferenceResolver). - WithMetadataReferenceProvider(options.MetadataReferenceProvider). - WithXmlReferenceResolver(options.XmlReferenceResolver). - WithSourceReferenceResolver(options.SourceReferenceResolver). - WithAssemblyIdentityComparer(options.AssemblyIdentityComparer). - WithStrongNameProvider(options.StrongNameProvider); - - Assert.Equal(TestOptions.Dll, dt); - } - - [Fact] - public void ParseOptions_Serialize() - { - var text = @" -class C -{ - void Foo() - { - } -} -"; - var tree = SyntaxFactory.ParseSyntaxTree(text, options: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp5)); - tree.GetCompilationUnitRoot().GetDiagnostics().Verify(); - var p = (CSharpParseOptions)tree.Options; - Assert.Equal(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp5, p.LanguageVersion); - - MemoryStream o = SerializeToStream(p); - CSharpParseOptions dp = (CSharpParseOptions) DeserializeFromStream(o); - Assert.Equal(p, dp); - } - - public static MemoryStream SerializeToStream(Object o) - { - MemoryStream stream = new MemoryStream(); - IFormatter formatter = new BinaryFormatter(); - formatter.Serialize(stream, o); - return stream; - } - - public static object DeserializeFromStream(MemoryStream stream) - { - IFormatter formatter = new BinaryFormatter(); - stream.Seek(0, SeekOrigin.Begin); - Object o = formatter.Deserialize(stream); - return o; - } - [Fact] [WorkItem(797640, "DevDiv")] public void GetMetadataReferenceAPITest() diff --git a/Src/Compilers/CSharp/Test/Syntax/CSharpCompilerSyntaxTest.csproj b/Src/Compilers/CSharp/Test/Syntax/CSharpCompilerSyntaxTest.csproj index d8190ce69d224..c17c6b5f45503 100644 --- a/Src/Compilers/CSharp/Test/Syntax/CSharpCompilerSyntaxTest.csproj +++ b/Src/Compilers/CSharp/Test/Syntax/CSharpCompilerSyntaxTest.csproj @@ -122,7 +122,7 @@ - + diff --git a/Src/Compilers/CSharp/Test/Syntax/Parsing/ParseOptionsTests.cs b/Src/Compilers/CSharp/Test/Syntax/Parsing/CSharpParseOptionsTests.cs similarity index 89% rename from Src/Compilers/CSharp/Test/Syntax/Parsing/ParseOptionsTests.cs rename to Src/Compilers/CSharp/Test/Syntax/Parsing/CSharpParseOptionsTests.cs index 2de63a6768c08..e4d33a7e7c941 100644 --- a/Src/Compilers/CSharp/Test/Syntax/Parsing/ParseOptionsTests.cs +++ b/Src/Compilers/CSharp/Test/Syntax/Parsing/CSharpParseOptionsTests.cs @@ -3,12 +3,13 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using Microsoft.CodeAnalysis.CSharp.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Parsing { - public class ParseOptionsTests + public class CSharpParseOptionsTests : CSharpTestBase { private void TestProperty(Func factory, Func getter, T validValue) { @@ -69,5 +70,15 @@ public void TestFieldsForEqualsAndGetHashCode() "PreprocessorSymbolNames", "PreprocessorSymbols"); } + + [Fact] + public void Serializability() + { + VerifySerializability(new CSharpSerializableParseOptions(new CSharpParseOptions( + languageVersion: LanguageVersion.Experimental, + documentationMode: DocumentationMode.None, + kind: SourceCodeKind.Interactive, + preprocessorSymbols: new[] { "A", "B" }))); + } } } diff --git a/Src/Compilers/CSharp/Test/Syntax/Parsing/ParsingTests.cs b/Src/Compilers/CSharp/Test/Syntax/Parsing/ParsingTests.cs index d914f6f9848a1..7c884a443c48f 100644 --- a/Src/Compilers/CSharp/Test/Syntax/Parsing/ParsingTests.cs +++ b/Src/Compilers/CSharp/Test/Syntax/Parsing/ParsingTests.cs @@ -4,10 +4,6 @@ using System.Collections.Generic; using System.Diagnostics; -using System.Linq; -using Microsoft.CodeAnalysis.CSharp.Symbols; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; using Xunit; namespace Microsoft.CodeAnalysis.CSharp.UnitTests diff --git a/Src/Compilers/Core/Source/CodeAnalysis.csproj b/Src/Compilers/Core/Source/CodeAnalysis.csproj index 3508b0c261932..bfb5daeb506f3 100644 --- a/Src/Compilers/Core/Source/CodeAnalysis.csproj +++ b/Src/Compilers/Core/Source/CodeAnalysis.csproj @@ -348,6 +348,8 @@ + + diff --git a/Src/Compilers/Core/Source/Collections/ArrayElement.cs b/Src/Compilers/Core/Source/Collections/ArrayElement.cs index 96e9799b8498d..5272014c30fe2 100644 --- a/Src/Compilers/Core/Source/Collections/ArrayElement.cs +++ b/Src/Compilers/Core/Source/Collections/ArrayElement.cs @@ -1,12 +1,9 @@ // Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; using System.Diagnostics; -using Microsoft.CodeAnalysis.Text; namespace Microsoft.CodeAnalysis { - [Serializable] [DebuggerDisplay("{Value,nq}")] internal struct ArrayElement { diff --git a/Src/Compilers/Core/Source/Compilation/Compilation.cs b/Src/Compilers/Core/Source/Compilation/Compilation.cs index ad0b79ea04025..847c999de1f70 100644 --- a/Src/Compilers/Core/Source/Compilation/Compilation.cs +++ b/Src/Compilers/Core/Source/Compilation/Compilation.cs @@ -1366,7 +1366,7 @@ public EmitResult EmitMetadataOnly( { if (peStream == null) { - throw new ArgumentNullException("metadataStream"); + throw new ArgumentNullException("peStream"); } return Emit( diff --git a/Src/Compilers/Core/Source/Compilation/CompilationOptions.cs b/Src/Compilers/Core/Source/Compilation/CompilationOptions.cs index 7190c42c17162..5b61074c7cef4 100644 --- a/Src/Compilers/Core/Source/Compilation/CompilationOptions.cs +++ b/Src/Compilers/Core/Source/Compilation/CompilationOptions.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; -using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; using Roslyn.Utilities; @@ -13,31 +12,8 @@ namespace Microsoft.CodeAnalysis /// /// Represents compilation options common to C# and VB. /// - [Serializable] - public abstract class CompilationOptions : ISerializable + public abstract class CompilationOptions { - private const string OutputKindString = "OutputKind"; - private const string ModuleNameString = "ModuleName"; - private const string MainTypeNameString = "MainTypeName"; - private const string ScriptClassNameString = "ScriptClassName"; - private const string CryptoKeyContainerString = "CryptoKeyContainer"; - private const string CryptoKeyFileString = "CryptoKeyFile"; - private const string DelaySignString = "DelaySign"; - private const string CheckOverflowString = "CheckOverflow"; - private const string FileAlignmentString = "FileAlignment"; - private const string BaseAddressString = "BaseAddress"; - private const string PlatformString = "Platform"; - private const string GeneralDiagnosticOptionString = "GeneralDiagnosticOption"; - private const string WarningLevelString = "WarningLevel"; - private const string SpecificDiagnosticOptionsString = "SpecificDiagnosticOptions"; - private const string HighEntropyVirtualAddressSpaceString = "HighEntropyVirtualAddressSpace"; - private const string DebugInformationKindString = "DebugInformationKind"; - private const string OptimizeString = "Optimize"; - private const string ConcurrentBuildString = "ConcurrentBuild"; - private const string SubsystemVersionString = "SubsystemVersion"; - private const string MetadataImportOptionsString = "MetadataImportOptions"; - private const string FeaturesString = "Features"; - /// /// The kind of assembly generated when emitted. /// @@ -232,42 +208,6 @@ internal CompilationOptions( StrongNameProvider strongNameProvider, MetadataImportOptions metadataImportOptions, ImmutableArray features) - { - Initialize( - outputKind, moduleName, mainTypeName, scriptClassName, cryptoKeyContainer, cryptoKeyFile, delaySign, optimize, checkOverflow, fileAlignment, - baseAddress, platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions, highEntropyVirtualAddressSpace, debugInformationKind, - subsystemVersion, concurrentBuild, xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, metadataReferenceProvider, assemblyIdentityComparer, strongNameProvider, - metadataImportOptions, features); - } - - internal void Initialize( - OutputKind outputKind, - string moduleName, - string mainTypeName, - string scriptClassName, - string cryptoKeyContainer, - string cryptoKeyFile, - bool? delaySign, - bool optimize, - bool checkOverflow, - int fileAlignment, - ulong baseAddress, - Platform platform, - ReportDiagnostic generalDiagnosticOption, - int warningLevel, - IEnumerable> specificDiagnosticOptions, - bool highEntropyVirtualAddressSpace, - DebugInformationKind debugInformationKind, - SubsystemVersion subsystemVersion, - bool concurrentBuild, - XmlReferenceResolver xmlReferenceResolver, - SourceReferenceResolver sourceReferenceResolver, - MetadataReferenceResolver metadataReferenceResolver, - MetadataReferenceProvider metadataReferenceProvider, - AssemblyIdentityComparer assemblyIdentityComparer, - StrongNameProvider strongNameProvider, - MetadataImportOptions metadataImportOptions, - ImmutableArray features) { this.OutputKind = outputKind; this.ModuleName = moduleName; @@ -305,63 +245,6 @@ internal void Initialize( }); } - protected CompilationOptions(SerializationInfo info, StreamingContext context) - { - Initialize( - outputKind: (OutputKind)info.GetInt32(OutputKindString), - moduleName: info.GetString(ModuleNameString), - mainTypeName: info.GetString(MainTypeNameString), - scriptClassName: info.GetString(ScriptClassNameString), - cryptoKeyContainer: info.GetString(CryptoKeyContainerString), - cryptoKeyFile: info.GetString(CryptoKeyFileString), - delaySign: (bool?)info.GetValue(DelaySignString, typeof(bool?)), - optimize: info.GetBoolean(OptimizeString), - checkOverflow: info.GetBoolean(CheckOverflowString), - fileAlignment: info.GetInt32(FileAlignmentString), - baseAddress: info.GetUInt64(BaseAddressString), - platform: (Platform)info.GetInt32(PlatformString), - generalDiagnosticOption: (ReportDiagnostic)info.GetInt32(GeneralDiagnosticOptionString), - warningLevel: info.GetInt32(WarningLevelString), - specificDiagnosticOptions: ((Dictionary)info.GetValue(SpecificDiagnosticOptionsString, typeof(Dictionary))).ToImmutableDictionary(), - highEntropyVirtualAddressSpace: info.GetBoolean(HighEntropyVirtualAddressSpaceString), - debugInformationKind: (DebugInformationKind)info.GetInt32(DebugInformationKindString), - subsystemVersion: (SubsystemVersion)info.GetValue(SubsystemVersionString, typeof(SubsystemVersion)), - concurrentBuild: info.GetBoolean(ConcurrentBuildString), - xmlReferenceResolver: XmlFileResolver.Default, - sourceReferenceResolver: SourceFileResolver.Default, - metadataReferenceResolver: MetadataFileReferenceResolver.Default, - metadataReferenceProvider: MetadataFileReferenceProvider.Default, - assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default, - strongNameProvider: new DesktopStrongNameProvider(), - metadataImportOptions: (MetadataImportOptions)info.GetByte(MetadataImportOptionsString), - features: ((string[])info.GetValue(FeaturesString, typeof(string[]))).AsImmutable()); - } - - public virtual void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue(OutputKindString, (int)this.OutputKind); - info.AddValue(ModuleNameString, this.ModuleName); - info.AddValue(MainTypeNameString, this.MainTypeName); - info.AddValue(ScriptClassNameString, this.ScriptClassName); - info.AddValue(CryptoKeyContainerString, this.CryptoKeyContainer); - info.AddValue(CryptoKeyFileString, this.CryptoKeyFile); - info.AddValue(DelaySignString, this.DelaySign); - info.AddValue(CheckOverflowString, this.CheckOverflow); - info.AddValue(FileAlignmentString, this.FileAlignment); - info.AddValue(BaseAddressString, this.BaseAddress); - info.AddValue(PlatformString, (int)this.Platform); - info.AddValue(GeneralDiagnosticOptionString, (int)this.GeneralDiagnosticOption); - info.AddValue(WarningLevelString, this.WarningLevel); - info.AddValue(SpecificDiagnosticOptionsString, new Dictionary(this.SpecificDiagnosticOptions)); - info.AddValue(HighEntropyVirtualAddressSpaceString, this.HighEntropyVirtualAddressSpace); - info.AddValue(DebugInformationKindString, (int)this.DebugInformationKind); - info.AddValue(OptimizeString, this.Optimize); - info.AddValue(SubsystemVersionString, this.SubsystemVersion); - info.AddValue(ConcurrentBuildString, this.ConcurrentBuild); - info.AddValue(MetadataImportOptionsString, (byte)this.MetadataImportOptions); - info.AddValue(FeaturesString, Features.ToArray()); - } - internal bool CanReuseCompilationReferenceManager(CompilationOptions other) { // This condition has to include all options the Assembly Manager depends on when binding references. diff --git a/Src/Compilers/Core/Source/Compilation/ParseOptions.cs b/Src/Compilers/Core/Source/Compilation/ParseOptions.cs index eac68640f6f12..f33abe9f8fb5e 100644 --- a/Src/Compilers/Core/Source/Compilation/ParseOptions.cs +++ b/Src/Compilers/Core/Source/Compilation/ParseOptions.cs @@ -12,19 +12,18 @@ namespace Microsoft.CodeAnalysis /// /// Represents parse options common to C# and VB. /// - [Serializable] - public abstract class ParseOptions : ISerializable + public abstract class ParseOptions { /// /// Specifies whether to parse as regular code files, script files or interactive code. /// - public readonly SourceCodeKind Kind; + public SourceCodeKind Kind { get; protected set; } /// /// Gets a value indicating whether the documentation comments are parsed. /// /// true if documentation comments are parsed, false otherwise. - public readonly DocumentationMode DocumentationMode; + public DocumentationMode DocumentationMode { get; protected set; } internal ParseOptions(SourceCodeKind kind, DocumentationMode documentationMode) { @@ -91,27 +90,5 @@ protected int GetHashCodeHelper() { return !object.Equals(left, right); } - - #region "serialization" - - protected ParseOptions(SerializationInfo info, StreamingContext context) - { - //public readonly SourceCodeKind Kind; - this.Kind = (SourceCodeKind)info.GetValue("Kind", typeof(SourceCodeKind)); - - //public readonly DocumentationMode DocumentationMode; - this.DocumentationMode = (DocumentationMode)info.GetValue("DocumentationMode", typeof(DocumentationMode)); - } - - public virtual void GetObjectData(SerializationInfo info, StreamingContext context) - { - //public readonly SourceCodeKind Kind; - info.AddValue("Kind", Kind, typeof(SourceCodeKind)); - - //public readonly DocumentationMode DocumentationMode; - info.AddValue("DocumentationMode", DocumentationMode, typeof(DocumentationMode)); - } - - #endregion } } diff --git a/Src/Compilers/Core/Source/Compilation/SubsystemVersion.cs b/Src/Compilers/Core/Source/Compilation/SubsystemVersion.cs index 319f6b5049138..bd09b50b25929 100644 --- a/Src/Compilers/Core/Source/Compilation/SubsystemVersion.cs +++ b/Src/Compilers/Core/Source/Compilation/SubsystemVersion.cs @@ -23,7 +23,6 @@ namespace Microsoft.CodeAnalysis /// - Windows 7 6.01 /// - Windows 8 Release Preview 6.02 /// - [Serializable] public struct SubsystemVersion : IEquatable { /// diff --git a/Src/Compilers/Core/Source/MetadataReference/MetadataReferenceProperties.cs b/Src/Compilers/Core/Source/MetadataReference/MetadataReferenceProperties.cs index 36d554afe3f44..8b9c65904ae62 100644 --- a/Src/Compilers/Core/Source/MetadataReference/MetadataReferenceProperties.cs +++ b/Src/Compilers/Core/Source/MetadataReference/MetadataReferenceProperties.cs @@ -6,15 +6,13 @@ using System.Collections.Immutable; using Microsoft.CodeAnalysis.Text; using Roslyn.Utilities; -using System.Runtime.Serialization; namespace Microsoft.CodeAnalysis { /// /// Information about a metadata reference. /// - [Serializable] - public struct MetadataReferenceProperties : IEquatable, ISerializable + public struct MetadataReferenceProperties : IEquatable { private readonly MetadataImageKind kind; private readonly ImmutableArray aliases; @@ -72,20 +70,6 @@ public struct MetadataReferenceProperties : IEquatable("aliases"); - this.embedInteropTypes = info.GetBoolean("embedInteropTypes"); - } - - public void GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue("kind", kind, typeof(MetadataImageKind)); - info.AddArray("aliases", aliases); - info.AddValue("embedInteropTypes", embedInteropTypes); - } - /// /// Returns with specified aliases. /// diff --git a/Src/Compilers/Core/Source/NonPortable/ParseOptions.cs b/Src/Compilers/Core/Source/NonPortable/ParseOptions.cs new file mode 100644 index 0000000000000..eac68640f6f12 --- /dev/null +++ b/Src/Compilers/Core/Source/NonPortable/ParseOptions.cs @@ -0,0 +1,117 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.CodeAnalysis; +using Roslyn.Utilities; +using System.Runtime.Serialization; + +namespace Microsoft.CodeAnalysis +{ + /// + /// Represents parse options common to C# and VB. + /// + [Serializable] + public abstract class ParseOptions : ISerializable + { + /// + /// Specifies whether to parse as regular code files, script files or interactive code. + /// + public readonly SourceCodeKind Kind; + + /// + /// Gets a value indicating whether the documentation comments are parsed. + /// + /// true if documentation comments are parsed, false otherwise. + public readonly DocumentationMode DocumentationMode; + + internal ParseOptions(SourceCodeKind kind, DocumentationMode documentationMode) + { + this.Kind = kind; + this.DocumentationMode = documentationMode; + } + + /// + /// Creates a new options instance with the specified source code kind. + /// + public ParseOptions WithKind(SourceCodeKind kind) + { + return CommonWithKind(kind); + } + + protected abstract ParseOptions CommonWithKind(SourceCodeKind kind); + + /// + /// Creates a new options instance with the specified documentation mode. + /// + public ParseOptions WithDocumentationMode(DocumentationMode documentationMode) + { + return CommonWithDocumentationMode(documentationMode); + } + + protected abstract ParseOptions CommonWithDocumentationMode(DocumentationMode documentationMode); + + /// + /// Names of defined preprocessor symbols. + /// + public abstract IEnumerable PreprocessorSymbolNames { get; } + + public abstract override bool Equals(object obj); + + protected bool EqualsHelper(ParseOptions other) + { + if (object.ReferenceEquals(other, null)) + { + return false; + } + + return + this.Kind == other.Kind && + this.DocumentationMode == other.DocumentationMode && + (this.PreprocessorSymbolNames == null ? other.PreprocessorSymbolNames == null : this.PreprocessorSymbolNames.SequenceEqual(other.PreprocessorSymbolNames, StringComparer.Ordinal)); + } + + public abstract override int GetHashCode(); + + protected int GetHashCodeHelper() + { + return + Hash.Combine((int)this.Kind, + Hash.Combine((int)this.DocumentationMode, + Hash.Combine(Hash.CombineValues(this.PreprocessorSymbolNames, StringComparer.Ordinal), 0))); + } + + public static bool operator ==(ParseOptions left, ParseOptions right) + { + return object.Equals(left, right); + } + + public static bool operator !=(ParseOptions left, ParseOptions right) + { + return !object.Equals(left, right); + } + + #region "serialization" + + protected ParseOptions(SerializationInfo info, StreamingContext context) + { + //public readonly SourceCodeKind Kind; + this.Kind = (SourceCodeKind)info.GetValue("Kind", typeof(SourceCodeKind)); + + //public readonly DocumentationMode DocumentationMode; + this.DocumentationMode = (DocumentationMode)info.GetValue("DocumentationMode", typeof(DocumentationMode)); + } + + public virtual void GetObjectData(SerializationInfo info, StreamingContext context) + { + //public readonly SourceCodeKind Kind; + info.AddValue("Kind", Kind, typeof(SourceCodeKind)); + + //public readonly DocumentationMode DocumentationMode; + info.AddValue("DocumentationMode", DocumentationMode, typeof(DocumentationMode)); + } + + #endregion + } +} diff --git a/Src/Compilers/Core/Source/NonPortable/SerializableCompilationOptions.cs b/Src/Compilers/Core/Source/NonPortable/SerializableCompilationOptions.cs new file mode 100644 index 0000000000000..adbfc54158c0e --- /dev/null +++ b/Src/Compilers/Core/Source/NonPortable/SerializableCompilationOptions.cs @@ -0,0 +1,74 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; + +namespace Microsoft.CodeAnalysis +{ + /// + /// Represents compilation options common to C# and VB. + /// + [Serializable] + public abstract class SerializableCompilationOptions : ISerializable + { + protected const string OutputKindString = "OutputKind"; + protected const string ModuleNameString = "ModuleName"; + protected const string MainTypeNameString = "MainTypeName"; + protected const string ScriptClassNameString = "ScriptClassName"; + protected const string CryptoKeyContainerString = "CryptoKeyContainer"; + protected const string CryptoKeyFileString = "CryptoKeyFile"; + protected const string DelaySignString = "DelaySign"; + protected const string CheckOverflowString = "CheckOverflow"; + protected const string FileAlignmentString = "FileAlignment"; + protected const string BaseAddressString = "BaseAddress"; + protected const string PlatformString = "Platform"; + protected const string GeneralDiagnosticOptionString = "GeneralDiagnosticOption"; + protected const string WarningLevelString = "WarningLevel"; + protected const string SpecificDiagnosticOptionsString = "SpecificDiagnosticOptions"; + protected const string HighEntropyVirtualAddressSpaceString = "HighEntropyVirtualAddressSpace"; + protected const string DebugInformationKindString = "DebugInformationKind"; + protected const string OptimizeString = "Optimize"; + protected const string ConcurrentBuildString = "ConcurrentBuild"; + protected const string SubsystemVersionMajorString = "SubsystemVersionMajor"; + protected const string SubsystemVersionMinorString = "SubsystemVersionMinor"; + protected const string MetadataImportOptionsString = "MetadataImportOptions"; + protected const string FeaturesString = "Features"; + + internal SerializableCompilationOptions() + { + } + + protected static void CommonGetObjectData(CompilationOptions options, SerializationInfo info, StreamingContext context) + { + info.AddValue(OutputKindString, (int)options.OutputKind); + info.AddValue(ModuleNameString, options.ModuleName); + info.AddValue(MainTypeNameString, options.MainTypeName); + info.AddValue(ScriptClassNameString, options.ScriptClassName); + info.AddValue(CryptoKeyContainerString, options.CryptoKeyContainer); + info.AddValue(CryptoKeyFileString, options.CryptoKeyFile); + info.AddValue(DelaySignString, options.DelaySign); + info.AddValue(CheckOverflowString, options.CheckOverflow); + info.AddValue(FileAlignmentString, options.FileAlignment); + info.AddValue(BaseAddressString, options.BaseAddress); + info.AddValue(PlatformString, (int)options.Platform); + info.AddValue(GeneralDiagnosticOptionString, (int)options.GeneralDiagnosticOption); + info.AddValue(WarningLevelString, options.WarningLevel); + info.AddValue(SpecificDiagnosticOptionsString, new Dictionary(options.SpecificDiagnosticOptions)); + info.AddValue(HighEntropyVirtualAddressSpaceString, options.HighEntropyVirtualAddressSpace); + info.AddValue(DebugInformationKindString, (int)options.DebugInformationKind); + info.AddValue(OptimizeString, options.Optimize); + info.AddValue(SubsystemVersionMajorString, options.SubsystemVersion.Major); + info.AddValue(SubsystemVersionMinorString, options.SubsystemVersion.Minor); + info.AddValue(ConcurrentBuildString, options.ConcurrentBuild); + info.AddValue(MetadataImportOptionsString, (byte)options.MetadataImportOptions); + info.AddValue(FeaturesString, options.Features.ToArray()); + } + + public CompilationOptions Options { get { return CommonOptions; } } + protected abstract CompilationOptions CommonOptions { get; } + + public abstract void GetObjectData(SerializationInfo info, StreamingContext context); + } +} diff --git a/Src/Compilers/Core/Source/NonPortable/SerializableParseOptions.cs b/Src/Compilers/Core/Source/NonPortable/SerializableParseOptions.cs new file mode 100644 index 0000000000000..490244950d4b5 --- /dev/null +++ b/Src/Compilers/Core/Source/NonPortable/SerializableParseOptions.cs @@ -0,0 +1,32 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Runtime.Serialization; + +namespace Microsoft.CodeAnalysis +{ + /// + /// Represents parse options common to C# and VB. + /// + [Serializable] + public abstract class SerializableParseOptions : ISerializable + { + internal SerializableParseOptions() + { + } + + public ParseOptions Options { get { return CommonOptions; } } + protected abstract ParseOptions CommonOptions { get; } + + public abstract void GetObjectData(SerializationInfo info, StreamingContext context); + + protected static void CommonGetObjectData(ParseOptions options, SerializationInfo info, StreamingContext context) + { + //public readonly SourceCodeKind Kind; + info.AddValue("Kind", options.Kind, typeof(SourceCodeKind)); + + //public readonly DocumentationMode DocumentationMode; + info.AddValue("DocumentationMode", options.DocumentationMode, typeof(DocumentationMode)); + } + } +} diff --git a/Src/Compilers/Test/Utilities/CSharp/CSharpCompilerTestUtilities.csproj b/Src/Compilers/Test/Utilities/CSharp/CSharpCompilerTestUtilities.csproj index 8cb5e27cf530a..77f77aef2b8d9 100644 --- a/Src/Compilers/Test/Utilities/CSharp/CSharpCompilerTestUtilities.csproj +++ b/Src/Compilers/Test/Utilities/CSharp/CSharpCompilerTestUtilities.csproj @@ -82,8 +82,6 @@ - - diff --git a/Src/Compilers/Test/Utilities/CSharp/CSharpParser.cs b/Src/Compilers/Test/Utilities/CSharp/CSharpParser.cs deleted file mode 100644 index e0b97dd53ec36..0000000000000 --- a/Src/Compilers/Test/Utilities/CSharp/CSharpParser.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - - -// Disabling this part of code which will be fixed once ParseTreeVisualizer changes are completed -#if FALSE -public class CSharpParser : IParser -{ - - public SyntaxNode Parse(String code) - { - return Parse(code, null); - } - - public SyntaxNode Parse(String code, ParseOptions options) - { - return Syntax.ParseCompilationUnit(code); - } - -} -#endif \ No newline at end of file diff --git a/Src/Compilers/Test/Utilities/CSharp/CSharpSyntaxNodeKindProvider.cs b/Src/Compilers/Test/Utilities/CSharp/CSharpSyntaxNodeKindProvider.cs deleted file mode 100644 index 9c5cbbabdb513..0000000000000 --- a/Src/Compilers/Test/Utilities/CSharp/CSharpSyntaxNodeKindProvider.cs +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - - -#if FALSE -public class VBSyntaxNodeKindProvider : ISyntaxNodeKindProvider -{ - - public String get_Kind(SyntaxNode node) - { - return ((CSharpSyntaxNode)node).Kind.ToString(); - } - -} -#endif \ No newline at end of file diff --git a/Src/Compilers/VisualBasic/Source/BasicCodeAnalysis.vbproj b/Src/Compilers/VisualBasic/Source/BasicCodeAnalysis.vbproj index 5d101360f1079..0bbca6bf51874 100644 --- a/Src/Compilers/VisualBasic/Source/BasicCodeAnalysis.vbproj +++ b/Src/Compilers/VisualBasic/Source/BasicCodeAnalysis.vbproj @@ -526,6 +526,8 @@ + + diff --git a/Src/Compilers/VisualBasic/Source/CommandLine/CommandLineParser.vb b/Src/Compilers/VisualBasic/Source/CommandLine/CommandLineParser.vb index a94650cbaff2c..4ec73b8d11d86 100644 --- a/Src/Compilers/VisualBasic/Source/CommandLine/CommandLineParser.vb +++ b/Src/Compilers/VisualBasic/Source/CommandLine/CommandLineParser.vb @@ -1028,11 +1028,10 @@ lVbRuntimePlus: End If Dim parseOptions = New VisualBasicParseOptions( - preprocessorSymbols:=AddPredefinedPreprocessorSymbols(outputKind, defines.AsImmutableOrEmpty()), + languageVersion:=languageVersion, documentationMode:=If(parseDocumentationComments, DocumentationMode.Diagnose, DocumentationMode.None), kind:=SourceCodeKind.Regular, - languageVersion:=languageVersion, - privateCtor:=True) + preprocessorSymbols:=AddPredefinedPreprocessorSymbols(outputKind, defines.AsImmutableOrEmpty())) Dim scriptParseOptions = parseOptions.WithKind(SourceCodeKind.Script) diff --git a/Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableCompilationOptions.vb b/Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableCompilationOptions.vb new file mode 100644 index 0000000000000..963d6f8da4262 --- /dev/null +++ b/Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableCompilationOptions.vb @@ -0,0 +1,96 @@ +' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +Imports System.Collections.Immutable +Imports System.Runtime.Serialization +Imports Microsoft.CodeAnalysis + +Namespace Microsoft.CodeAnalysis.VisualBasic + + Public NotInheritable Class VisualBasicSerializableCompilationOptions + Inherits SerializableCompilationOptions + + Private _options As VisualBasicCompilationOptions + + Private Const GlobalImportsString = "GlobalImports" + Private Const RootNamespaceString = "RootNamespace" + Private Const OptionStrictString = "OptionStrict" + Private Const OptionInferString = "OptionInfer" + Private Const OptionExplicitString = "OptionExplicit" + Private Const OptionCompareTextString = "OptionCompareText" + Private Const EmbedVbCoreRuntimeString = "EmbedVbCoreRuntime" + Private Const ParseOptionsString = "ParseOptions" + + Sub New(options As VisualBasicCompilationOptions) + If options Is Nothing Then + Throw New ArgumentNullException("options") + End If + + _options = options + End Sub + + Friend Sub New(info As SerializationInfo, context As StreamingContext) + Dim serializableOptions = DirectCast(info.GetValue(ParseOptionsString, GetType(VisualBasicSerializableParseOptions)), VisualBasicSerializableParseOptions) + + _options = New VisualBasicCompilationOptions( + outputKind:=DirectCast(info.GetInt32(OutputKindString), OutputKind), + moduleName:=info.GetString(ModuleNameString), + mainTypeName:=info.GetString(MainTypeNameString), + scriptClassName:=info.GetString(ScriptClassNameString), + cryptoKeyContainer:=info.GetString(CryptoKeyContainerString), + cryptoKeyFile:=info.GetString(CryptoKeyFileString), + delaySign:=DirectCast(info.GetValue(DelaySignString, GetType(Boolean?)), Boolean?), + optimize:=info.GetBoolean(OptimizeString), + checkOverflow:=info.GetBoolean(CheckOverflowString), + fileAlignment:=info.GetInt32(FileAlignmentString), + baseAddress:=info.GetUInt64(BaseAddressString), + platform:=DirectCast(info.GetInt32(PlatformString), Platform), + generalDiagnosticOption:=DirectCast(info.GetInt32(GeneralDiagnosticOptionString), ReportDiagnostic), + specificDiagnosticOptions:=DirectCast(info.GetValue(SpecificDiagnosticOptionsString, GetType(Dictionary(Of String, ReportDiagnostic))), Dictionary(Of String, ReportDiagnostic)).ToImmutableDictionary(), + highEntropyVirtualAddressSpace:=info.GetBoolean(HighEntropyVirtualAddressSpaceString), + debugInformationKind:=DirectCast(info.GetInt32(DebugInformationKindString), DebugInformationKind), + subsystemVersion:=SubsystemVersion.Create(info.GetInt32(SubsystemVersionMajorString), info.GetInt32(SubsystemVersionMinorString)), + concurrentBuild:=info.GetBoolean(ConcurrentBuildString), + xmlReferenceResolver:=XmlFileResolver.Default, + sourceReferenceResolver:=SourceFileResolver.Default, + metadataReferenceResolver:=MetadataFileReferenceResolver.Default, + metadataReferenceProvider:=MetadataFileReferenceProvider.Default, + assemblyIdentityComparer:=DesktopAssemblyIdentityComparer.Default, + strongNameProvider:=New DesktopStrongNameProvider(), + metadataImportOptions:=DirectCast(info.GetByte(MetadataImportOptionsString), MetadataImportOptions), + features:=DirectCast(info.GetValue(FeaturesString, GetType(String())), String()).AsImmutable(), + globalImports:=DirectCast(info.GetValue(GlobalImportsString, GetType(String())), String()).Select(AddressOf GlobalImport.Parse), + rootNamespace:=info.GetString(RootNamespaceString), + optionStrict:=CType(info.GetInt32(OptionStrictString), OptionStrict), + optionInfer:=info.GetBoolean(OptionInferString), + optionExplicit:=info.GetBoolean(OptionExplicitString), + optionCompareText:=info.GetBoolean(OptionCompareTextString), + embedVbCoreRuntime:=info.GetBoolean(EmbedVbCoreRuntimeString), + parseOptions:=If(serializableOptions IsNot Nothing, serializableOptions.Options, Nothing)) + End Sub + + Public Overrides Sub GetObjectData(info As SerializationInfo, context As StreamingContext) + CommonGetObjectData(_options, info, context) + + info.AddValue(GlobalImportsString, _options.GlobalImports.Select(Function(g) g.Name).ToArray()) + info.AddValue(RootNamespaceString, _options.RootNamespace) + info.AddValue(OptionStrictString, _options.OptionStrict) + info.AddValue(OptionInferString, _options.OptionInfer) + info.AddValue(OptionExplicitString, _options.OptionExplicit) + info.AddValue(OptionCompareTextString, _options.OptionCompareText) + info.AddValue(EmbedVbCoreRuntimeString, _options.EmbedVbCoreRuntime) + info.AddValue(ParseOptionsString, If(_options.ParseOptions IsNot Nothing, New VisualBasicSerializableParseOptions(_options.ParseOptions), Nothing)) + End Sub + + Public Shadows ReadOnly Property Options As VisualBasicCompilationOptions + Get + Return _options + End Get + End Property + + Protected Overrides ReadOnly Property CommonOptions As CompilationOptions + Get + Return _options + End Get + End Property + End Class +End Namespace diff --git a/Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableParseOptions.vb b/Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableParseOptions.vb new file mode 100644 index 0000000000000..059f5031a58e9 --- /dev/null +++ b/Src/Compilers/VisualBasic/Source/NonPortable/VisualBasicSerializableParseOptions.vb @@ -0,0 +1,79 @@ +' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +Imports System.Runtime.Serialization + +Namespace Microsoft.CodeAnalysis.VisualBasic + + Public Class VisualBasicSerializableParseOptions + Inherits SerializableParseOptions + + Private ReadOnly _options As VisualBasicParseOptions + + Sub New(options As VisualBasicParseOptions) + If options Is Nothing Then + Throw New ArgumentNullException("options") + End If + + _options = options + End Sub + + Public Shadows ReadOnly Property Options As VisualBasicParseOptions + Get + Return _options + End Get + End Property + + Protected Overrides ReadOnly Property CommonOptions As ParseOptions + Get + Return _options + End Get + End Property + + Private Sub New(info As SerializationInfo, context As StreamingContext) + ' We serialize keys and values as two separate arrays + ' to avoid the trouble dealing with situations where deserializer may want to deserialize + ' an array before its elements (which gets complicated when elements are structs). + + Dim keys() As String = DirectCast(info.GetValue("Keys", GetType(String())), String()) + Dim values() As Object = DirectCast(info.GetValue("Values", GetType(Object())), Object()) + + Dim count As Integer = keys.Length + Debug.Assert(values.Length = count) + + Dim builder = ArrayBuilder(Of KeyValuePair(Of String, Object)).GetInstance + + For i As Integer = 0 To count - 1 + builder.Add(New KeyValuePair(Of String, Object)(keys(i), values(i))) + Next + + _options = New VisualBasicParseOptions( + languageVersion:=DirectCast(info.GetValue("LanguageVersion", GetType(LanguageVersion)), LanguageVersion), + documentationMode:=DirectCast(info.GetValue("DocumentationMode", GetType(DocumentationMode)), DocumentationMode), + kind:=DirectCast(info.GetValue("Kind", GetType(SourceCodeKind)), SourceCodeKind), + preprocessorSymbols:=builder.ToImmutableAndFree()) + End Sub + + Public Overrides Sub GetObjectData(info As SerializationInfo, context As StreamingContext) + CommonGetObjectData(_options, info, context) + + ' We serialize keys and values as two separate arrays + ' to avoid the trouble dealing with situations where deserializer may want to deserialize + ' an array before its elements (which gets complicated when elements are structs). + + Dim ppSymbols = _options.PreprocessorSymbols + Dim keys(ppSymbols.Length - 1) As String + Dim values(ppSymbols.Length - 1) As Object + + For i As Integer = 0 To ppSymbols.Length - 1 + Dim sym = ppSymbols(i) + keys(i) = sym.Key + values(i) = sym.Value + Next + + info.AddValue("Keys", keys, GetType(String())) + info.AddValue("Values", values, GetType(Object())) + info.AddValue("LanguageVersion", _options.LanguageVersion, GetType(LanguageVersion)) + End Sub + End Class +End Namespace + diff --git a/Src/Compilers/VisualBasic/Source/VisualBasicCompilationOptions.vb b/Src/Compilers/VisualBasic/Source/VisualBasicCompilationOptions.vb index 59d882d70ab6e..fe0f6bf1fea11 100644 --- a/Src/Compilers/VisualBasic/Source/VisualBasicCompilationOptions.vb +++ b/Src/Compilers/VisualBasic/Source/VisualBasicCompilationOptions.vb @@ -1,19 +1,15 @@ ' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Collections.Immutable -Imports System.Runtime.Serialization Imports Microsoft.CodeAnalysis -Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Namespace Microsoft.CodeAnalysis.VisualBasic ''' ''' A class representing Visual Basic compilation Options. ''' - Public NotInheritable Class VisualBasicCompilationOptions Inherits CompilationOptions Implements IEquatable(Of VisualBasicCompilationOptions) - Implements ISerializable Private Const GlobalImportsString = "GlobalImports" Private Const RootNamespaceString = "RootNamespace" @@ -140,7 +136,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic End Sub - Private Sub New( + Friend Sub New( outputKind As OutputKind, moduleName As String, mainTypeName As String, @@ -205,8 +201,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic metadataImportOptions:=metadataImportOptions, features:=features) - Initialize(globalImports, rootNamespace, optionStrict, optionInfer, optionExplicit, optionCompareText, embedVbCoreRuntime, parseOptions) - + _globalImports = globalImports.AsImmutableOrEmpty() + _rootNamespace = If(rootNamespace, String.Empty) + _optionStrict = optionStrict + _optionInfer = optionInfer + _optionExplicit = optionExplicit + _optionCompareText = optionCompareText + _embedVbCoreRuntime = embedVbCoreRuntime + _parseOptions = parseOptions End Sub Private Sub New(other As VisualBasicCompilationOptions) @@ -247,61 +249,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic features:=other.Features) End Sub - ''' - ''' Initializes an instance of VisualBasicCompilationOptions. - ''' - ''' A SerializationInfo object that contains the information required to serialize the VisualBasicCompilationOptions instance. - ''' A StreamingContext object that contains the source and destination of the serialized stream associated with the VisualBasicCompilationOptions instance. - Protected Sub New(info As SerializationInfo, context As StreamingContext) - MyBase.New(info, context) - Dim importStrings = DirectCast(info.GetValue(GlobalImportsString, GetType(String())), String()) - - Initialize(globalImports:=importStrings.Select(AddressOf GlobalImport.Parse), - rootNamespace:=info.GetString(RootNamespaceString), - optionStrict:=CType(info.GetInt32(OptionStrictString), OptionStrict), - optionInfer:=info.GetBoolean(OptionInferString), - optionExplicit:=info.GetBoolean(OptionExplicitString), - optionCompareText:=info.GetBoolean(OptionCompareTextString), - embedVbCoreRuntime:=info.GetBoolean(EmbedVbCoreRuntimeString), - parseOptions:=DirectCast(info.GetValue(ParseOptionsString, GetType(VisualBasicParseOptions)), VisualBasicParseOptions)) - End Sub - - ''' - ''' Implements the System.Runtime.Serialization.ISerializable interface and returns the data needed to serialize the VisualBasicCompilationOptions instance. - ''' - ''' A SerializationInfo object that contains the information required to serialize the VisualBasicCompilationOptions instance. - ''' A StreamingContext object that contains the source and destination of the serialized stream associated with the VisualBasicCompilationOptions instance. - Public Overrides Sub GetObjectData(info As SerializationInfo, context As StreamingContext) - MyBase.GetObjectData(info, context) - info.AddValue(GlobalImportsString, _globalImports.Select(Function(g) g.Name).ToArray()) - info.AddValue(RootNamespaceString, _rootNamespace) - info.AddValue(OptionStrictString, _optionStrict) - info.AddValue(OptionInferString, _optionInfer) - info.AddValue(OptionExplicitString, _optionExplicit) - info.AddValue(OptionCompareTextString, _optionCompareText) - info.AddValue(EmbedVbCoreRuntimeString, _embedVbCoreRuntime) - info.AddValue(ParseOptionsString, _parseOptions) - End Sub - - Private Shadows Sub Initialize(globalImports As IEnumerable(Of GlobalImport), - rootNamespace As String, - optionStrict As OptionStrict, - optionInfer As Boolean, - optionExplicit As Boolean, - optionCompareText As Boolean, - embedVbCoreRuntime As Boolean, - parseOptions As VisualBasicParseOptions) - _globalImports = globalImports.AsImmutableOrEmpty() - _rootNamespace = If(rootNamespace, String.Empty) - _optionStrict = optionStrict - _optionInfer = optionInfer - _optionExplicit = optionExplicit - _optionCompareText = optionCompareText - _embedVbCoreRuntime = embedVbCoreRuntime - _parseOptions = parseOptions - End Sub - - ''' ''' Gets the global imports collection. ''' diff --git a/Src/Compilers/VisualBasic/Source/VisualBasicParseOptions.vb b/Src/Compilers/VisualBasic/Source/VisualBasicParseOptions.vb index a21ca1675be7d..04385ae8dde4a 100644 --- a/Src/Compilers/VisualBasic/Source/VisualBasicParseOptions.vb +++ b/Src/Compilers/VisualBasic/Source/VisualBasicParseOptions.vb @@ -1,7 +1,6 @@ ' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Collections.Immutable -Imports System.Runtime.Serialization Imports Microsoft.CodeAnalysis Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Imports Microsoft.CodeAnalysis.VisualBasic.SyntaxFacts @@ -10,45 +9,33 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ''' ''' Represents Visual Basic parse options. ''' - Public NotInheritable Class VisualBasicParseOptions Inherits ParseOptions - Implements IEquatable(Of VisualBasicParseOptions), ISerializable + Implements IEquatable(Of VisualBasicParseOptions) Public Shared ReadOnly [Default] As VisualBasicParseOptions = New VisualBasicParseOptions() - - Private ReadOnly _preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) - Private ReadOnly _languageVersion As LanguageVersion - Private Shared _defaultPreprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) - Private Shared ReadOnly Property DefaultPreprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) - Get - If _defaultPreprocessorSymbols.IsDefaultOrEmpty Then - _defaultPreprocessorSymbols = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(KeyValuePair.Create("_MYTYPE", CObj("Empty"))) - End If - Return _defaultPreprocessorSymbols - End Get - End Property + Private _preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) + Private _languageVersion As LanguageVersion ''' ''' Creates an instance of VisualBasicParseOptions. ''' ''' The parser language version. ''' The comentation mode. - ''' The kind of source code. + ''' The kind of source code. ''' An immutable array of KeyValuePair representing pre processor symbols. Public Sub New( Optional languageVersion As LanguageVersion = LanguageVersion.VisualBasic11, - Optional documentationMode As DocumentationMode = Microsoft.CodeAnalysis.DocumentationMode.Parse, + Optional documentationMode As DocumentationMode = DocumentationMode.Parse, Optional kind As SourceCodeKind = SourceCodeKind.Regular, - Optional preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) = Nothing) + Optional preprocessorSymbols As IEnumerable(Of KeyValuePair(Of String, Object)) = Nothing) MyClass.New(languageVersion, documentationMode, kind, - If(preprocessorSymbols.IsDefault, DefaultPreprocessorSymbols, preprocessorSymbols), - privateCtor:=True) + If(preprocessorSymbols Is Nothing, DefaultPreprocessorSymbols, ImmutableArray.CreateRange(preprocessorSymbols))) If Not languageVersion.IsValid Then Throw New ArgumentOutOfRangeException("languageVersion") @@ -58,7 +45,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Throw New ArgumentOutOfRangeException("kind") End If - If Not preprocessorSymbols.IsDefaultOrEmpty Then + If preprocessorSymbols IsNot Nothing Then For Each preprocessorSymbol In preprocessorSymbols If Not IsValidIdentifier(preprocessorSymbol.Key) OrElse @@ -83,8 +70,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic languageVersion As LanguageVersion, documentationMode As DocumentationMode, kind As SourceCodeKind, - preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)), - privateCtor As Boolean) + preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object))) MyBase.New(kind, documentationMode) @@ -93,6 +79,24 @@ Namespace Microsoft.CodeAnalysis.VisualBasic _preprocessorSymbols = preprocessorSymbols End Sub + Private Sub New(other As VisualBasicParseOptions) + MyClass.New( + languageVersion:=other._languageVersion, + documentationMode:=other.DocumentationMode, + kind:=other.Kind, + preprocessorSymbols:=other._preprocessorSymbols) + End Sub + + Private Shared ReadOnly Property DefaultPreprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) + Get + If _defaultPreprocessorSymbols.IsDefaultOrEmpty Then + _defaultPreprocessorSymbols = ImmutableArray.Create(KeyValuePair.Create("_MYTYPE", CObj("Empty"))) + End If + + Return _defaultPreprocessorSymbols + End Get + End Property + ''' ''' Returns the parser language version. ''' @@ -129,7 +133,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic ''' The parser language version. ''' A new instance of VisualBasicParseOptions if different language version is different; otherwise current instance. Public Shadows Function WithLanguageVersion(version As LanguageVersion) As VisualBasicParseOptions - If version = Me.LanguageVersion Then + If version = _languageVersion Then Return Me End If @@ -137,11 +141,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Throw New ArgumentOutOfRangeException("version") End If - Return New VisualBasicParseOptions( - version, - Me.DocumentationMode, - Me.Kind, - Me.PreprocessorSymbols) + Return New VisualBasicParseOptions(Me) With {._languageVersion = version} End Function ''' @@ -158,11 +158,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Throw New ArgumentOutOfRangeException("kind") End If - Return New VisualBasicParseOptions( - Me.LanguageVersion, - Me.DocumentationMode, - kind, - Me.PreprocessorSymbols) + Return New VisualBasicParseOptions(Me) With {.Kind = kind} End Function ''' @@ -179,11 +175,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Throw New ArgumentOutOfRangeException("documentationMode") End If - Return New VisualBasicParseOptions( - Me.LanguageVersion, - documentationMode, - Me.Kind, - Me.PreprocessorSymbols) + Return New VisualBasicParseOptions(Me) With {.DocumentationMode = documentationMode} End Function ''' @@ -218,11 +210,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Return Me End If - Return New VisualBasicParseOptions( - Me.LanguageVersion, - Me.DocumentationMode, - Me.Kind, - symbols) + Return New VisualBasicParseOptions(Me) With {._preprocessorSymbols = symbols} End Function ''' @@ -284,65 +272,5 @@ Namespace Microsoft.CodeAnalysis.VisualBasic Public Overrides Function GetHashCode() As Integer Return Hash.Combine(MyBase.GetHashCodeHelper(), CInt(Me.LanguageVersion)) End Function - -#Region "serialization" - Private Sub New(info As SerializationInfo, context As StreamingContext) - MyBase.New(info, context) - - ' Private ReadOnly _preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) - - ' We serialize keys and values as two separate arrays - ' to avoid the trouble dealing with situations where deserializer may want to deserialize - ' an array before its elements (which gets complicated when elements are structs). - - Dim keys() As String = DirectCast(info.GetValue("Keys", GetType(String())), String()) - Dim values() As Object = DirectCast(info.GetValue("Values", GetType(Object())), Object()) - - Dim count As Integer = keys.Length - Debug.Assert(values.Length = count) - - Dim builder = ArrayBuilder(Of KeyValuePair(Of String, Object)).GetInstance - - For i As Integer = 0 To count - 1 - builder.Add(New KeyValuePair(Of String, Object)(keys(i), values(i))) - Next - - Me._preprocessorSymbols = builder.ToImmutableAndFree - - ' Private ReadOnly _languageVersion As LanguageVersion - Me._languageVersion = DirectCast(info.GetValue("LanguageVersion", GetType(LanguageVersion)), LanguageVersion) - End Sub - - ''' - ''' Implements the System.Runtime.Serialization.ISerializable interface and returns the data needed to serialize the VisualBasicParseOptions instance. - ''' - ''' A SerializationInfo object that contains the information required to serialize the VisualBasicParseOptions instance. - ''' A StreamingContext object that contains the source and destination of the serialized stream associated with the VisualBasicParseOptions instance. - Public Overrides Sub GetObjectData(info As SerializationInfo, context As StreamingContext) - MyBase.GetObjectData(info, context) - - ' Private ReadOnly _preprocessorSymbols As ImmutableArray(Of KeyValuePair(Of String, Object)) - - ' We serialize keys and values as two separate arrays - ' to avoid the trouble dealing with situations where deserializer may want to deserialize - ' an array before its elements (which gets complicated when elements are structs). - - Dim ppSymbols = Me._preprocessorSymbols - Dim keys(ppSymbols.Length - 1) As String - Dim values(ppSymbols.Length - 1) As Object - - For i As Integer = 0 To ppSymbols.Length - 1 - Dim sym = ppSymbols(i) - keys(i) = sym.Key - values(i) = sym.Value - Next - - info.AddValue("Keys", keys, GetType(String())) - info.AddValue("Values", values, GetType(Object())) - - ' Private ReadOnly _languageVersion As LanguageVersion - info.AddValue("LanguageVersion", _languageVersion, GetType(LanguageVersion)) - End Sub -#End Region End Class End Namespace diff --git a/Src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb b/Src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb index 92e48d978bf06..ff8cb93d663b7 100644 --- a/Src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb +++ b/Src/Compilers/VisualBasic/Test/Semantic/Compilation/VisualBasicCompilationOptionsTests.vb @@ -501,16 +501,56 @@ BC2042: The options /vbruntime* and /target:module cannot be combined. ) End Sub + ''' + ''' If this test fails, please update the + ''' And methods to + ''' make sure they are doing the right thing with your New field And then update the baseline + ''' here. + ''' + + Public Sub TestFieldsForEqualsAndGetHashCode() + ReflectionAssert.AssertPublicAndInternalFieldsAndProperties( + (GetType(VisualBasicCompilationOptions)), + "GlobalImports", + "RootNamespace", + "OptionStrict", + "OptionInfer", + "OptionExplicit", + "OptionCompareText", + "EmbedVbCoreRuntime", + "ParseOptions") + End Sub + + + Public Sub Serializability1() + VerifySerializability(New VisualBasicSerializableCompilationOptions(New VisualBasicCompilationOptions( + outputKind:=OutputKind.WindowsApplication, + generalDiagnosticOption:=ReportDiagnostic.Hidden, + specificDiagnosticOptions:={KeyValuePair.Create("VB0001", ReportDiagnostic.Suppress)}, + subsystemVersion:=SubsystemVersion.Windows2000, + globalImports:={GlobalImport.Parse("Foo.Bar")}))) + End Sub + - Public Sub SerializationRoundTrip() + Public Sub Serializability2() + VerifySerializability(New VisualBasicSerializableCompilationOptions(New VisualBasicCompilationOptions( + outputKind:=OutputKind.WindowsApplication, + parseOptions:=New VisualBasicParseOptions( + languageVersion:=LanguageVersion.Experimental, + documentationMode:=DocumentationMode.Diagnose, + preprocessorSymbols:={KeyValuePair.Create(Of String, Object)("s", 1), KeyValuePair.Create(Of String, Object)("t", 2)})))) + End Sub + + + Public Sub Serializability3() Dim parseOptions = New VisualBasicParseOptions( languageVersion:=LanguageVersion.VisualBasic10, - documentationMode:=Microsoft.CodeAnalysis.DocumentationMode.Diagnose, - kind:=Microsoft.CodeAnalysis.SourceCodeKind.Regular, + documentationMode:=DocumentationMode.Diagnose, + kind:=SourceCodeKind.Regular, preprocessorSymbols:=ImmutableArray.Create(New KeyValuePair(Of String, Object)("key", "Value"))) Dim compilationOptions = New VisualBasicCompilationOptions( - Microsoft.CodeAnalysis.OutputKind.ConsoleApplication, + OutputKind.ConsoleApplication, globalImports:={GlobalImport.Parse("Foo.Bar")}, rootNamespace:="Alpha.Beta", optionStrict:=OptionStrict.Custom, @@ -523,10 +563,10 @@ BC2042: The options /vbruntime* and /target:module cannot be combined. Dim stream = New MemoryStream() Dim formatter = New BinaryFormatter() - formatter.Serialize(stream, compilationOptions) + formatter.Serialize(stream, New VisualBasicSerializableCompilationOptions(compilationOptions)) stream.Position = 0 - Dim deserializedCompilationOptions = DirectCast(formatter.Deserialize(stream), VisualBasicCompilationOptions) + Dim deserializedCompilationOptions = DirectCast(formatter.Deserialize(stream), VisualBasicSerializableCompilationOptions).Options Assert.Equal(compilationOptions.GlobalImports.First().Name, deserializedCompilationOptions.GlobalImports.First().Name) @@ -544,25 +584,5 @@ BC2042: The options /vbruntime* and /target:module cannot be combined. deserializedCompilationOptions.EmbedVbCoreRuntime) End Sub - ''' - ''' If this test fails, please update the - ''' And methods to - ''' make sure they are doing the right thing with your New field And then update the baseline - ''' here. - ''' - - Public Sub TestFieldsForEqualsAndGetHashCode() - ReflectionAssert.AssertPublicAndInternalFieldsAndProperties( - (GetType(VisualBasicCompilationOptions)), - "GlobalImports", - "RootNamespace", - "OptionStrict", - "OptionInfer", - "OptionExplicit", - "OptionCompareText", - "EmbedVbCoreRuntime", - "ParseOptions") - End Sub - End Class End Namespace \ No newline at end of file diff --git a/Src/Compilers/VisualBasic/Test/Syntax/BasicCompilerSyntaxTest.vbproj b/Src/Compilers/VisualBasic/Test/Syntax/BasicCompilerSyntaxTest.vbproj index b4ba42906ddc4..4696684520598 100644 --- a/Src/Compilers/VisualBasic/Test/Syntax/BasicCompilerSyntaxTest.vbproj +++ b/Src/Compilers/VisualBasic/Test/Syntax/BasicCompilerSyntaxTest.vbproj @@ -128,7 +128,7 @@ - + diff --git a/Src/Compilers/VisualBasic/Test/Syntax/Parser/ParseOptionsTests.vb b/Src/Compilers/VisualBasic/Test/Syntax/Parser/VisualBasicParseOptionsTests.vb similarity index 81% rename from Src/Compilers/VisualBasic/Test/Syntax/Parser/ParseOptionsTests.vb rename to Src/Compilers/VisualBasic/Test/Syntax/Parser/VisualBasicParseOptionsTests.vb index 2205626ad0662..5726a60d5d7f1 100644 --- a/Src/Compilers/VisualBasic/Test/Syntax/Parser/ParseOptionsTests.vb +++ b/Src/Compilers/VisualBasic/Test/Syntax/Parser/VisualBasicParseOptionsTests.vb @@ -1,12 +1,11 @@ ' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. Imports System.Collections.Immutable -Imports System.IO -Imports System.Runtime.Serialization.Formatters.Binary Imports Roslyn.Test.Utilities - -Public Class ParseOptionsTests +Public Class VisualBasicParseOptionsTests + Inherits BasicTestBase + Private Sub TestProperty(Of T)(factory As Func(Of VisualBasicParseOptions, T, VisualBasicParseOptions), getter As Func(Of VisualBasicParseOptions, T), validValue As T) Dim oldOpt1 = VisualBasicParseOptions.Default Dim newOpt1 = factory(oldOpt1, validValue) @@ -44,23 +43,23 @@ Public Class ParseOptionsTests ' Command line: error BC31030: Project-level conditional compilation constant 'xxx' is not valid: Identifier expected - Dim syms = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(New KeyValuePair(Of String, Object)("", 1)) + Dim syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("", 1)) Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms)) - syms = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(New KeyValuePair(Of String, Object)(" ", 1)) + syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)(" ", 1)) Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms)) - syms = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(New KeyValuePair(Of String, Object)("Good", 1), - New KeyValuePair(Of String, Object)(Nothing, 2)) + syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("Good", 1), + New KeyValuePair(Of String, Object)(Nothing, 2)) Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms)) - syms = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(New KeyValuePair(Of String, Object)("Good", 1), - New KeyValuePair(Of String, Object)("Bad.Symbol", 2)) + syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("Good", 1), + New KeyValuePair(Of String, Object)("Bad.Symbol", 2)) Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms)) - syms = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(New KeyValuePair(Of String, Object)("123", 1), - New KeyValuePair(Of String, Object)("Bad/Symbol", 2), - New KeyValuePair(Of String, Object)("Good", 3)) + syms = ImmutableArray.Create(New KeyValuePair(Of String, Object)("123", 1), + New KeyValuePair(Of String, Object)("Bad/Symbol", 2), + New KeyValuePair(Of String, Object)("Good", 3)) Assert.Throws(Of ArgumentException)(Function() New VisualBasicParseOptions(preprocessorSymbols:=syms)) End Sub @@ -223,21 +222,10 @@ Public Class ParseOptionsTests "PreprocessorSymbols") End Sub - - Public Sub Serialization() - Dim preprocessorSymbols = ImmutableArray.Create(Of KeyValuePair(Of String, Object))(New KeyValuePair(Of String, Object)("Alpha", 1), - New KeyValuePair(Of String, Object)("Beta", "Foo")) - Dim options = New VisualBasicParseOptions(preprocessorSymbols:=preprocessorSymbols) - Dim deserializedOptions As VisualBasicParseOptions - - Dim formatter = New BinaryFormatter - - Using stream As New MemoryStream - formatter.Serialize(stream, options) - stream.Seek(0, SeekOrigin.Begin) - deserializedOptions = DirectCast(formatter.Deserialize(stream), VisualBasicParseOptions) - End Using - - Assert.Equal(expected:=options, actual:=deserializedOptions) + + Public Sub Serializability() + VerifySerializability(New VisualBasicSerializableParseOptions(New VisualBasicParseOptions( + languageVersion:=LanguageVersion.Experimental, + documentationMode:=DocumentationMode.Diagnose))) End Sub End Class diff --git a/Src/Test/Utilities/TestBase.cs b/Src/Test/Utilities/TestBase.cs index defa230d3cdf7..76bd8536f4125 100644 --- a/Src/Test/Utilities/TestBase.cs +++ b/Src/Test/Utilities/TestBase.cs @@ -16,6 +16,8 @@ using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.PdbUtilities; using Xunit; +using System.Runtime.Serialization; +using System.Runtime.Serialization.Formatters.Binary; namespace Roslyn.Test.Utilities { @@ -777,5 +779,24 @@ protected static string RunAndGetOutput(string exeFileName, string arguments = n #endregion + #region Serialization + + public static void VerifySerializability(T obj) + { + Assert.True(obj is ISerializable); + + var formatter = new BinaryFormatter(); + using (var stream = new MemoryStream()) + { + formatter.Serialize(stream, obj); + + stream.Seek(0, SeekOrigin.Begin); + var deserialized = (T)formatter.Deserialize(stream); + stream.Seek(0, SeekOrigin.Begin); + } + } + + #endregion + } } diff --git a/Src/Workspaces/Core/Workspace/Solution/ProjectInfo.cs b/Src/Workspaces/Core/Workspace/Solution/ProjectInfo.cs index ffa8c1db12083..bebc70ab12f7a 100644 --- a/Src/Workspaces/Core/Workspace/Solution/ProjectInfo.cs +++ b/Src/Workspaces/Core/Workspace/Solution/ProjectInfo.cs @@ -50,12 +50,12 @@ public sealed class ProjectInfo public string OutputFilePath { get; private set; } /// - /// The initial compilation options for the project. + /// The initial compilation options for the project, or null if the default options should be used. /// public CompilationOptions CompilationOptions { get; private set; } /// - /// The initial parse options for the source code documents in this project. + /// The initial parse options for the source code documents in this project, or null if the default options should be used. /// public ParseOptions ParseOptions { get; private set; } diff --git a/Src/Workspaces/CoreTest/SolutionTests.cs b/Src/Workspaces/CoreTest/SolutionTests.cs index b3ecd38765b09..862f6e1380989 100644 --- a/Src/Workspaces/CoreTest/SolutionTests.cs +++ b/Src/Workspaces/CoreTest/SolutionTests.cs @@ -437,7 +437,7 @@ public void TestProjectParseOptions() // Parse Options var oldParseOptions = solution.GetProject(project1).ParseOptions; - var newParseOptions = new CSharpParseOptions(preprocessorSymbols: "AFTER"); + var newParseOptions = new CSharpParseOptions(preprocessorSymbols: new[] { "AFTER" }); solution = solution.WithProjectParseOptions(project1, newParseOptions); var newUpdatedParseOptions = solution.GetProject(project1).ParseOptions; Assert.NotEqual(oldParseOptions, newUpdatedParseOptions);