Skip to content

Commit

Permalink
Move serialization from CompilationOptions and ParseOptions to dedica…
Browse files Browse the repository at this point in the history
…ted SerializableCompilationOptions and SerializableParseOptions wrappers. (changeset 1274332)
  • Loading branch information
tmat committed Jun 20, 2014
1 parent 5689b8b commit ac6e765
Show file tree
Hide file tree
Showing 36 changed files with 740 additions and 621 deletions.
2 changes: 2 additions & 0 deletions Src/Compilers/CSharp/Source/CSharpCodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@
<Compile Include="Lowering\AsyncRewriter\AsyncRewriter.cs" />
<Compile Include="Lowering\AsyncRewriter\AsyncStateMachine.cs" />
<Compile Include="Lowering\AsyncRewriter\AwaitLiftingRewriter.cs" />
<Compile Include="NonPortable\CSharpSerializableParseOptions.cs" />
<Compile Include="Lowering\DiagnosticsPass_ExpressionTrees.cs" />
<Compile Include="Lowering\DiagnosticsPass_Warnings.cs" />
<Compile Include="Lowering\Extensions.cs" />
Expand Down Expand Up @@ -465,6 +466,7 @@
<Compile Include="Lowering\SyntheticBoundNodeFactory.cs" />
<Compile Include="Lowering\UnmatchedGotoFinder.cs" />
<Compile Include="NonPortable\CSharpDesktopExtensions.cs" />
<Compile Include="NonPortable\CSharpSerializableCompilationOptions.cs" />
<Compile Include="Parser\AbstractLexer.cs" />
<Compile Include="Parser\BlendedNode.cs" />
<Compile Include="Parser\Blender.cs" />
Expand Down
41 changes: 6 additions & 35 deletions Src/Compilers/CSharp/Source/CSharpCompilationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,13 +14,8 @@ namespace Microsoft.CodeAnalysis.CSharp
/// whether to emit an executable or a library, whether to optimize
/// generated code, and so on.
/// </summary>
[Serializable]
public sealed class CSharpCompilationOptions : CompilationOptions, IEquatable<CSharpCompilationOptions>, ISerializable
public sealed class CSharpCompilationOptions : CompilationOptions, IEquatable<CSharpCompilationOptions>
{
private const string AllowUnsafeString = "AllowUnsafe";
private const string UsingsString = "Usings";
private const string RuntimeMetadataVersionString = "RuntimeMetadataVersion";

/// <summary>
/// Allow unsafe regions (i.e. unsafe modifiers on members and unsafe blocks).
/// </summary>
Expand Down Expand Up @@ -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<string>.Empty)
{
}

// Expects correct arguments.
private CSharpCompilationOptions(
internal CSharpCompilationOptions(
OutputKind outputKind,
string moduleName,
string mainTypeName,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -146,30 +141,6 @@ private CSharpCompilationOptions(CSharpCompilationOptions other) : this(
{
}

private void Initialize(IEnumerable<string> 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)
Expand Down
119 changes: 30 additions & 89 deletions Src/Compilers/CSharp/Source/CSharpParseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// This class stores several source parsing related options and offers access to their values.
/// </summary>
[Serializable]
public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOptions>, ISerializable
public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOptions>
{
/// <summary>
/// The default parse options.
Expand All @@ -26,9 +21,9 @@ public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOpt
/// <summary>
/// Gets the language version.
/// </summary>
public readonly LanguageVersion LanguageVersion;
public LanguageVersion LanguageVersion { get; private set; }

internal readonly ImmutableArray<string> PreprocessorSymbols;
internal ImmutableArray<string> PreprocessorSymbols { get; private set; }

/// <summary>
/// Gets the names of defined preprocessor symbols.
Expand All @@ -38,23 +33,12 @@ public override IEnumerable<string> PreprocessorSymbolNames
get { return PreprocessorSymbols; }
}

// NOTE: warnaserror[+|-], warnaserror[+|-]:<warn list>, unsafe[+|-], warn:<n>, nowarn:<warn list>

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<string> preprocessorSymbols = default(ImmutableArray<string>))
: this(languageVersion, documentationMode, kind, preprocessorSymbols.NullToEmpty(), privateCtor: true)
IEnumerable<string> preprocessorSymbols = null)
: this(languageVersion, documentationMode, kind, preprocessorSymbols.ToImmutableArrayOrEmpty())
{
if (!languageVersion.IsValid())
{
Expand All @@ -66,7 +50,7 @@ public CSharpParseOptions(
throw new ArgumentOutOfRangeException("kind");
}

if (!preprocessorSymbols.IsDefaultOrEmpty)
if (preprocessorSymbols != null)
{
foreach (var preprocessorSymbol in preprocessorSymbols)
{
Expand All @@ -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<string> preprocessorSymbols,
bool privateCtor) //dummy param to distinguish from public ctor
ImmutableArray<string> preprocessorSymbols)
: base(kind, documentationMode)
{
Debug.Assert(!preprocessorSymbols.IsDefault);
Expand All @@ -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)
Expand All @@ -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<string> preprocessorSymbols)
Expand All @@ -151,7 +120,7 @@ public CSharpParseOptions WithPreprocessorSymbols(IEnumerable<string> preprocess

public CSharpParseOptions WithPreprocessorSymbols(params string[] preprocessorSymbols)
{
return WithPreprocessorSymbols(ImmutableArray.Create<string>(preprocessorSymbols));
return WithPreprocessorSymbols(ImmutableArray.Create(preprocessorSymbols));
}

public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray<string> symbols)
Expand All @@ -166,13 +135,7 @@ public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray<string> 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)
Expand All @@ -187,13 +150,17 @@ public CSharpParseOptions WithPreprocessorSymbols(ImmutableArray<string> 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)
Expand Down Expand Up @@ -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<string> PreprocessorSymbols;
this.PreprocessorSymbols = info.GetArray<string>("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<string> PreprocessorSymbols;
info.AddArray("PreprocessorSymbols", this.PreprocessorSymbols);
}

#endregion
}
}
3 changes: 1 addition & 2 deletions Src/Compilers/CSharp/Source/CommandLine/CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> 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);
Expand Down
Loading

0 comments on commit ac6e765

Please sign in to comment.