Skip to content

Commit

Permalink
Add OperationInvoker source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
g7ed6e committed Oct 28, 2023
1 parent 6a652f6 commit fd8ec88
Show file tree
Hide file tree
Showing 24 changed files with 2,078 additions and 0 deletions.
26 changes: 26 additions & 0 deletions System.ServiceModel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.ServiceModel.UnixDom
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Binding.UDS.IntegrationTests", "src\System.Private.ServiceModel\tests\Scenarios\Binding\UDS\Binding.UDS.IntegrationTests.csproj", "{B7C7D4F1-DE4D-421B-9CE9-C7320A503D58}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ServiceModel.BuildTools.Roslyn4.0", "src\System.ServiceModel.BuildTools\src\System.ServiceModel.BuildTools.Roslyn4.0.csproj", "{C03919A5-503D-4FC1-A9C2-F534EE25D84B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ServiceModel.BuildTools.Roslyn3.11", "src\System.ServiceModel.BuildTools\src\System.ServiceModel.BuildTools.Roslyn3.11.csproj", "{83BB41BB-CB6E-4D32-9CD1-384FEAE88949}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ServiceModel.BuildTools.Roslyn3.11.Tests", "src\System.ServiceModel.BuildTools\tests\System.ServiceModel.BuildTools.Roslyn3.11.Tests.csproj", "{5D1B7FB8-3A9B-487F-96C0-76ACC43D98A2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.ServiceModel.BuildTools.Roslyn4.0.Tests", "src\System.ServiceModel.BuildTools\tests\System.ServiceModel.BuildTools.Roslyn4.0.Tests.csproj", "{6B670970-CAD2-446E-AC83-2BF097E4CD5E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -259,6 +267,22 @@ Global
{B7C7D4F1-DE4D-421B-9CE9-C7320A503D58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7C7D4F1-DE4D-421B-9CE9-C7320A503D58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7C7D4F1-DE4D-421B-9CE9-C7320A503D58}.Release|Any CPU.Build.0 = Release|Any CPU
{C03919A5-503D-4FC1-A9C2-F534EE25D84B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C03919A5-503D-4FC1-A9C2-F534EE25D84B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C03919A5-503D-4FC1-A9C2-F534EE25D84B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C03919A5-503D-4FC1-A9C2-F534EE25D84B}.Release|Any CPU.Build.0 = Release|Any CPU
{83BB41BB-CB6E-4D32-9CD1-384FEAE88949}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83BB41BB-CB6E-4D32-9CD1-384FEAE88949}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83BB41BB-CB6E-4D32-9CD1-384FEAE88949}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83BB41BB-CB6E-4D32-9CD1-384FEAE88949}.Release|Any CPU.Build.0 = Release|Any CPU
{5D1B7FB8-3A9B-487F-96C0-76ACC43D98A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D1B7FB8-3A9B-487F-96C0-76ACC43D98A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D1B7FB8-3A9B-487F-96C0-76ACC43D98A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D1B7FB8-3A9B-487F-96C0-76ACC43D98A2}.Release|Any CPU.Build.0 = Release|Any CPU
{6B670970-CAD2-446E-AC83-2BF097E4CD5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6B670970-CAD2-446E-AC83-2BF097E4CD5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6B670970-CAD2-446E-AC83-2BF097E4CD5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6B670970-CAD2-446E-AC83-2BF097E4CD5E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -296,6 +320,8 @@ Global
{E8E40B62-E737-4768-82C2-039E90ED9A39} = {D6302510-AB10-4775-BCE9-98FA96FDEB76}
{88918456-A2B2-431F-BB95-BAAD2818BFC7} = {DFDC71CF-6E65-481D-99D7-C35ED7EF6D4E}
{B7C7D4F1-DE4D-421B-9CE9-C7320A503D58} = {D6302510-AB10-4775-BCE9-98FA96FDEB76}
{5D1B7FB8-3A9B-487F-96C0-76ACC43D98A2} = {DFDC71CF-6E65-481D-99D7-C35ED7EF6D4E}
{6B670970-CAD2-446E-AC83-2BF097E4CD5E} = {DFDC71CF-6E65-481D-99D7-C35ED7EF6D4E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E0638FAC-BA6B-4E18-BAE6-468C3191BE58}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

126 changes: 126 additions & 0 deletions src/System.ServiceModel.BuildTools/src/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace System.ServiceModel.BuildTools
{
internal static class MethodSymbolExtensions
{
public static bool? IsGeneratedCode(this IMethodSymbol methodSymbol)
=> methodSymbol.Locations.FirstOrDefault()?.SourceTree?.FilePath.EndsWith(".g.cs");

public static bool IsMatchingUserProvidedMethod(this IMethodSymbol methodSymbol, IMethodSymbol userProvidedMethodSymbol, INamedTypeSymbol? coreWCFInjectedAttribute, INamedTypeSymbol? fromServicesAttribute)
{
int parameterFound = 0;
if (methodSymbol.Name != userProvidedMethodSymbol.Name)
{
return false;
}

var parameters = methodSymbol.Parameters;

for (int i = 0,j = 0; i < userProvidedMethodSymbol.Parameters.Length; i++)
{
IParameterSymbol parameterSymbol = userProvidedMethodSymbol.Parameters[i];
if (parameterSymbol.GetOneAttributeOf(coreWCFInjectedAttribute, fromServicesAttribute) is not null)
{
continue;
}

if (parameterSymbol.IsMatchingParameter(parameters[j]))
{
j++;
parameterFound++;
}
}

return parameterFound == parameters.Length;
}
}

internal static class ParameterSymbolExtensions
{
public static bool IsMatchingParameter(this IParameterSymbol symbol, IParameterSymbol parameterSymbol)
=> SymbolEqualityComparer.Default.Equals(symbol.Type, parameterSymbol.Type);
}

internal static class SymbolExtensions
{
public static AttributeData? GetOneAttributeOf(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols)
{
if (attributeTypeSymbols.Length == 0)
{
return null;
}

foreach (var attribute in symbol.GetAttributes())
{
foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null))
{
if (SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, namedTypeSymbol))
{
return attribute;
}
}
}

return null;
}

public static bool HasOneAttributeInheritFrom(this ISymbol symbol, params INamedTypeSymbol?[] attributeTypeSymbols)
{
if (attributeTypeSymbols.Length == 0)
{
return false;
}

foreach (var attribute in symbol.GetAttributes())
{
foreach (var @interface in attribute.AttributeClass!.AllInterfaces)
{
foreach (var namedTypeSymbol in attributeTypeSymbols.Where(static s => s is not null))
{
if (SymbolEqualityComparer.Default.Equals(@interface, namedTypeSymbol))
{
return true;
}
}
}
}

return false;
}
}

internal static class NamedTypeSymbolExtensions
{
public static bool IsPartial(this INamedTypeSymbol namedTypeSymbol, out INamedTypeSymbol parentType)
{
bool result = namedTypeSymbol.DeclaringSyntaxReferences.Select(static s => s.GetSyntax()).OfType<ClassDeclarationSyntax>().All(static c => c.Modifiers.Any(static m => m.IsKind(SyntaxKind.PartialKeyword)));
if (result && namedTypeSymbol.ContainingType != null)
{
return namedTypeSymbol.ContainingType.IsPartial(out parentType);
}
parentType = namedTypeSymbol;
return result;
}
}

internal static class TypedConstantExtensions
{
public static string ToSafeCSharpString(this TypedConstant typedConstant)
{
if (typedConstant.Kind == TypedConstantKind.Array)
{
return $"new [] {typedConstant.ToCSharpString()}";
}

return typedConstant.ToCSharpString();

}
}
}
41 changes: 41 additions & 0 deletions src/System.ServiceModel.BuildTools/src/Indentor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.ServiceModel.BuildTools
{
internal sealed class Indentor
{
const string ____ = " ";
const string ________ = " ";
const string ____________ = " ";
const string ________________ = " ";
const string ____________________ = " ";
const string ________________________ = " ";
const string ____________________________ = " ";
const string ________________________________ = " ";
public int Level { get; private set; } = 0;
public void Increment()
{
Level++;
}

public void Decrement()
{
Level--;
}

public override string ToString() => Level switch
{
0 => string.Empty,
1 => ____,
2 => ________,
3 => ____________,
4 => ________________,
5 => ____________________,
6 => ________________________,
7 => ____________________________,
8 => ________________________________,
_ => throw new InvalidOperationException()
};
}
}
Loading

0 comments on commit fd8ec88

Please sign in to comment.