Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7fea27a

Browse files
Rebuild API shape (#52079)
* Rebuild API shapeThis breaks up `BuildConstructor` into a simple hierarchy with bothCSharp and Visual Basic implementations. The name `CompilationFactory`was chosen after a bit of offline conversation and mostly due to thisrepresenting a factory of sorts. As with all other API names it will bereviewed before making this API officially public / shipping.Note: there is no intended functional change with this PR, it is purelyjust refactoring the API shape around.* Apply suggestions from code reviewCo-authored-by: Rikki Gibson <rikkigibson@gmail.com>* PR Feedback* PR feedback* PR feedbackCo-authored-by: Rikki Gibson <rikkigibson@gmail.com>
1 parent1e94706 commit7fea27a

File tree

10 files changed

+440
-398
lines changed

10 files changed

+440
-398
lines changed

‎src/Compilers/Core/Rebuild/BuildConstructor.cs‎

Lines changed: 0 additions & 345 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
usingSystem;
6+
usingSystem.Collections.Immutable;
7+
usingSystem.Diagnostics;
8+
usingSystem.IO;
9+
usingMicrosoft.Cci;
10+
usingMicrosoft.CodeAnalysis;
11+
usingMicrosoft.CodeAnalysis.CSharp;
12+
usingMicrosoft.CodeAnalysis.Text;
13+
usingCS=Microsoft.CodeAnalysis.CSharp;
14+
15+
namespaceBuildValidator
16+
{
17+
publicsealedclassCSharpCompilationFactory:CompilationFactory
18+
{
19+
publicnewCSharpParseOptionsParseOptions{get;}
20+
publicnewCSharpCompilationOptionsCompilationOptions{get;}
21+
22+
protectedoverrideParseOptionsCommonParseOptions=>ParseOptions;
23+
protectedoverrideCompilationOptionsCommonCompilationOptions=>CompilationOptions;
24+
25+
privateCSharpCompilationFactory(
26+
stringassemblyFileName,
27+
CompilationOptionsReaderoptionsReader,
28+
CSharpParseOptionsparseOptions,
29+
CSharpCompilationOptionscompilationOptions)
30+
:base(assemblyFileName,optionsReader)
31+
{
32+
Debug.Assert(optionsReader.GetLanguageName()==LanguageNames.CSharp);
33+
ParseOptions=parseOptions;
34+
CompilationOptions=compilationOptions;
35+
}
36+
37+
internalstaticnewCSharpCompilationFactoryCreate(stringassemblyFileName,CompilationOptionsReaderoptionsReader)
38+
{
39+
Debug.Assert(optionsReader.GetLanguageName()==LanguageNames.CSharp);
40+
var(compilationOptions,parseOptions)=CreateCSharpCompilationOptions(assemblyFileName,optionsReader);
41+
returnnewCSharpCompilationFactory(assemblyFileName,optionsReader,parseOptions,compilationOptions);
42+
}
43+
44+
publicoverrideSyntaxTreeCreateSyntaxTree(stringfilePath,SourceTextsourceText)
45+
=>CSharpSyntaxTree.ParseText(sourceText,ParseOptions,filePath);
46+
47+
publicoverrideCompilationCreateCompilation(
48+
ImmutableArray<SyntaxTree>syntaxTrees,
49+
ImmutableArray<MetadataReference>metadataReferences)
50+
=>CSharpCompilation.Create(
51+
Path.GetFileNameWithoutExtension(AssemblyFileName),
52+
syntaxTrees:syntaxTrees,
53+
references:metadataReferences,
54+
options:CompilationOptions);
55+
56+
privatestatic(CSharpCompilationOptions,CSharpParseOptions)CreateCSharpCompilationOptions(stringassemblyFileName,CompilationOptionsReaderoptionsReader)
57+
{
58+
varpdbCompilationOptions=optionsReader.GetMetadataCompilationOptions();
59+
60+
varlangVersionString=pdbCompilationOptions.GetUniqueOption(CompilationOptionNames.LanguageVersion);
61+
pdbCompilationOptions.TryGetUniqueOption(CompilationOptionNames.Optimization,outvaroptimization);
62+
pdbCompilationOptions.TryGetUniqueOption(CompilationOptionNames.Platform,outvarplatform);
63+
64+
// TODO: Check portability policy if needed
65+
// pdbCompilationOptions.TryGetValue("portability-policy", out var portabilityPolicyString);
66+
pdbCompilationOptions.TryGetUniqueOption(CompilationOptionNames.Define,outvardefine);
67+
pdbCompilationOptions.TryGetUniqueOption(CompilationOptionNames.Checked,outvarcheckedString);
68+
pdbCompilationOptions.TryGetUniqueOption(CompilationOptionNames.Nullable,outvarnullable);
69+
pdbCompilationOptions.TryGetUniqueOption(CompilationOptionNames.Unsafe,outvarunsafeString);
70+
71+
CS.LanguageVersionFacts.TryParse(langVersionString,outvarlangVersion);
72+
73+
varpreprocessorSymbols=define==null
74+
?ImmutableArray<string>.Empty
75+
:define.Split(',').ToImmutableArray();
76+
77+
varparseOptions=CSharpParseOptions.Default.WithLanguageVersion(langVersion)
78+
.WithPreprocessorSymbols(preprocessorSymbols);
79+
80+
var(optimizationLevel,plus)=GetOptimizationLevel(optimization);
81+
82+
varnullableOptions=nullableisnull
83+
?NullableContextOptions.Disable
84+
:(NullableContextOptions)Enum.Parse(typeof(NullableContextOptions),nullable);
85+
86+
varcompilationOptions=newCSharpCompilationOptions(
87+
optionsReader.GetOutputKind(),
88+
reportSuppressedDiagnostics:false,
89+
90+
moduleName:assemblyFileName,
91+
mainTypeName:optionsReader.GetMainTypeName(),
92+
scriptClassName:null,
93+
usings:null,
94+
optimizationLevel,
95+
!string.IsNullOrEmpty(checkedString)&&bool.Parse(checkedString),
96+
!string.IsNullOrEmpty(unsafeString)&&bool.Parse(unsafeString),
97+
cryptoKeyContainer:null,
98+
cryptoKeyFile:null,
99+
cryptoPublicKey:optionsReader.GetPublicKey()?.ToImmutableArray()??default,
100+
delaySign:null,
101+
GetPlatform(platform),
102+
103+
// presence of diagnostics is expected to not affect emit.
104+
ReportDiagnostic.Suppress,
105+
warningLevel:4,
106+
specificDiagnosticOptions:null,
107+
108+
concurrentBuild:true,
109+
deterministic:true,
110+
111+
xmlReferenceResolver:null,
112+
sourceReferenceResolver:null,
113+
metadataReferenceResolver:null,
114+
115+
assemblyIdentityComparer:null,
116+
strongNameProvider:null,
117+
publicSign:false,
118+
119+
metadataImportOptions:MetadataImportOptions.Public,
120+
nullableContextOptions:nullableOptions);
121+
compilationOptions.DebugPlusMode=plus;
122+
123+
return(compilationOptions,parseOptions);
124+
}
125+
}
126+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
usingSystem;
6+
usingSystem.Collections.Immutable;
7+
usingSystem.IO;
8+
usingSystem.Linq;
9+
usingSystem.Reflection.PortableExecutable;
10+
usingSystem.Threading;
11+
usingMicrosoft.CodeAnalysis;
12+
usingMicrosoft.CodeAnalysis.Emit;
13+
usingMicrosoft.CodeAnalysis.Text;
14+
15+
namespaceBuildValidator
16+
{
17+
publicabstractclassCompilationFactory
18+
{
19+
publicstringAssemblyFileName{get;}
20+
publicCompilationOptionsReaderOptionsReader{get;}
21+
publicParseOptionsParseOptions=>CommonParseOptions;
22+
publicCompilationOptionsCompilationOptions=>CommonCompilationOptions;
23+
24+
protectedabstractParseOptionsCommonParseOptions{get;}
25+
protectedabstractCompilationOptionsCommonCompilationOptions{get;}
26+
27+
protectedCompilationFactory(stringassemblyFileName,CompilationOptionsReaderoptionsReader)
28+
{
29+
AssemblyFileName=assemblyFileName;
30+
OptionsReader=optionsReader;
31+
}
32+
33+
publicstaticCompilationFactoryCreate(stringassemblyFileName,CompilationOptionsReaderoptionsReader)
34+
=>optionsReader.GetLanguageName()switch
35+
{
36+
LanguageNames.CSharp=>CSharpCompilationFactory.Create(assemblyFileName,optionsReader),
37+
LanguageNames.VisualBasic=>VisualBasicCompilationFactory.Create(assemblyFileName,optionsReader),
38+
varlanguage=>thrownewInvalidDataException($"{assemblyFileName} has unsupported language{language}")
39+
};
40+
41+
publicabstractSyntaxTreeCreateSyntaxTree(stringfilePath,SourceTextsourceText);
42+
43+
publicabstractCompilationCreateCompilation(
44+
ImmutableArray<SyntaxTree>syntaxTrees,
45+
ImmutableArray<MetadataReference>metadataReferences);
46+
47+
publicEmitResultEmit(
48+
StreamrebuildPeStream,
49+
ImmutableArray<SyntaxTree>syntaxTrees,
50+
ImmutableArray<MetadataReference>metadataReferences,
51+
CancellationTokencancellationToken)
52+
=>Emit(
53+
rebuildPeStream,
54+
CreateCompilation(syntaxTrees,metadataReferences),
55+
cancellationToken);
56+
57+
publicEmitResultEmit(
58+
StreamrebuildPeStream,
59+
CompilationrebuildCompilation,
60+
CancellationTokencancellationToken)
61+
{
62+
varembeddedTexts=rebuildCompilation.SyntaxTrees
63+
.Select(st=>(path:st.FilePath,text:st.GetText()))
64+
.Where(pair=>pair.text.CanBeEmbedded)
65+
.Select(pair=>EmbeddedText.FromSource(pair.path,pair.text))
66+
.ToImmutableArray();
67+
68+
returnEmit(
69+
rebuildPeStream,
70+
rebuildCompilation,
71+
embeddedTexts,
72+
cancellationToken);
73+
}
74+
75+
publicunsafeEmitResultEmit(
76+
StreamrebuildPeStream,
77+
CompilationrebuildCompilation,
78+
ImmutableArray<EmbeddedText>embeddedTexts,
79+
CancellationTokencancellationToken)
80+
{
81+
varpeHeader=OptionsReader.PeReader.PEHeaders.PEHeader!;
82+
varwin32Resources=OptionsReader.PeReader.GetSectionData(peHeader.ResourceTableDirectory.RelativeVirtualAddress);
83+
usingvarwin32ResourceStream=win32Resources.Pointer!=null
84+
?newUnmanagedMemoryStream(win32Resources.Pointer,win32Resources.Length)
85+
:null;
86+
87+
varsourceLink=OptionsReader.GetSourceLinkUTF8();
88+
89+
vardebugEntryPoint=getDebugEntryPoint();
90+
91+
varemitResult=rebuildCompilation.Emit(
92+
peStream:rebuildPeStream,
93+
pdbStream:null,
94+
xmlDocumentationStream:null,
95+
win32Resources:win32ResourceStream,
96+
useRawWin32Resources:true,
97+
manifestResources:OptionsReader.GetManifestResources(),
98+
options:newEmitOptions(
99+
debugInformationFormat:DebugInformationFormat.Embedded,
100+
highEntropyVirtualAddressSpace:(peHeader.DllCharacteristics&DllCharacteristics.HighEntropyVirtualAddressSpace)!=0,
101+
subsystemVersion:SubsystemVersion.Create(peHeader.MajorSubsystemVersion,peHeader.MinorSubsystemVersion)),
102+
debugEntryPoint:debugEntryPoint,
103+
metadataPEStream:null,
104+
pdbOptionsBlobReader:OptionsReader.GetMetadataCompilationOptionsBlobReader(),
105+
sourceLinkStream:sourceLink!=null?newMemoryStream(sourceLink):null,
106+
embeddedTexts:embeddedTexts,
107+
cancellationToken:cancellationToken);
108+
109+
returnemitResult;
110+
111+
IMethodSymbol?getDebugEntryPoint()
112+
{
113+
if(OptionsReader.GetMainMethodInfo()is(stringmainTypeName,stringmainMethodName))
114+
{
115+
vartypeSymbol=rebuildCompilation.GetTypeByMetadataName(mainTypeName);
116+
if(typeSymbolisobject)
117+
{
118+
varmethodSymbols=typeSymbol
119+
.GetMembers(mainMethodName)
120+
.OfType<IMethodSymbol>();
121+
returnmethodSymbols.FirstOrDefault();
122+
}
123+
}
124+
125+
returnnull;
126+
}
127+
}
128+
129+
protectedstatic(OptimizationLevel,bool)GetOptimizationLevel(string?optimizationLevel)
130+
=>optimizationLevelswitch
131+
{
132+
null or"debug"=>(OptimizationLevel.Debug,false),
133+
"debug-plus"=>(OptimizationLevel.Debug,true),
134+
"release"=>(OptimizationLevel.Release,false),
135+
_=>thrownewInvalidDataException($"Optimization\"{optimizationLevel}\" level not recognized")
136+
};
137+
138+
protectedstaticPlatformGetPlatform(string?platform)
139+
=>platformisnull
140+
?Platform.AnyCpu
141+
:(Platform)Enum.Parse(typeof(Platform),platform);
142+
}
143+
}

‎src/Compilers/Core/Rebuild/Records.cs‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,4 @@
99

1010
namespaceBuildValidator
1111
{
12-
publicrecordSyntaxTreeInfo(
13-
stringFilePath,
14-
SourceTextSourceText)
15-
{
16-
publicstaticSyntaxTreeInfoCreate(SyntaxTreesyntaxTree,CancellationTokencancellationToken=default)=>
17-
newSyntaxTreeInfo(syntaxTree.FilePath,syntaxTree.GetText(cancellationToken));
18-
}
1912
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp