-
Notifications
You must be signed in to change notification settings - Fork 4k
/
CSharpOptionsSerializationService.cs
75 lines (61 loc) · 3.65 KB
/
CSharpOptionsSerializationService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Composition;
using System.Threading;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Serialization;
[ExportLanguageService(typeof(IOptionsSerializationService), LanguageNames.CSharp), Shared]
internal class CSharpOptionsSerializationService : AbstractOptionsSerializationService
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpOptionsSerializationService()
{
}
public override void WriteTo(CompilationOptions options, ObjectWriter writer, CancellationToken cancellationToken)
{
WriteCompilationOptionsTo(options, writer, cancellationToken);
var csharpOptions = (CSharpCompilationOptions)options;
writer.WriteArray(csharpOptions.Usings, static (w, u) => w.WriteString(u));
writer.WriteBoolean(csharpOptions.AllowUnsafe);
writer.WriteByte((byte)csharpOptions.NullableContextOptions);
}
public override void WriteTo(ParseOptions options, ObjectWriter writer)
{
WriteParseOptionsTo(options, writer);
var csharpOptions = (CSharpParseOptions)options;
writer.WriteInt32((int)csharpOptions.SpecifiedLanguageVersion);
writer.WriteArray(options.PreprocessorSymbolNames.ToImmutableArrayOrEmpty(), static (w, p) => w.WriteString(p));
}
public override CompilationOptions ReadCompilationOptionsFrom(ObjectReader reader, CancellationToken cancellationToken)
{
var (outputKind, reportSuppressedDiagnostics, moduleName, mainTypeName, scriptClassName,
optimizationLevel, checkOverflow, cryptoKeyContainer, cryptoKeyFile, cryptoPublicKey,
delaySign, platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions,
concurrentBuild, deterministic, publicSign, metadataImportOptions,
xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, assemblyIdentityComparer,
strongNameProvider) = ReadCompilationOptionsPieces(reader, cancellationToken);
var usings = reader.ReadArray(static r => r.ReadString());
var allowUnsafe = reader.ReadBoolean();
var nullableContextOptions = (NullableContextOptions)reader.ReadByte();
return new CSharpCompilationOptions(
outputKind, reportSuppressedDiagnostics, moduleName, mainTypeName, scriptClassName, usings, optimizationLevel, checkOverflow, allowUnsafe,
cryptoKeyContainer, cryptoKeyFile, cryptoPublicKey, delaySign, platform, generalDiagnosticOption, warningLevel, specificDiagnosticOptions, concurrentBuild,
deterministic, xmlReferenceResolver, sourceReferenceResolver, metadataReferenceResolver, assemblyIdentityComparer, strongNameProvider, publicSign,
metadataImportOptions, nullableContextOptions);
}
public override ParseOptions ReadParseOptionsFrom(ObjectReader reader, CancellationToken cancellationToken)
{
var (kind, documentationMode, features) = ReadParseOptionsPieces(reader, cancellationToken);
var languageVersion = (LanguageVersion)reader.ReadInt32();
var preprocessorSymbolNames = reader.ReadArray(static r => r.ReadString());
var options = new CSharpParseOptions(languageVersion, documentationMode, kind, preprocessorSymbolNames);
return options.WithFeatures(features);
}
}