-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make constructors private for parts of the ComInterfaceGenerator that…
… should only be created from their static methods (#101740) Moves ComClassInfo to its own file and moves the Func that produces them in the ComClassGenerator pipeline to a static method on ComClassInfo. Makes the constructors for ComClassInfo, ComInterfaceContext, ComInterfaceInfo, and `ComMethoInfo private to enforce that they are only created from the static creation methods. Creates more static SpecialTypeInfo types for sbyte, uint, short, and ushort. Changes pattern matching on records to use property pattern matching rather than the deconstruction notation for records. Since the constructors are not accessible, neither are the deconstruct methods.
- Loading branch information
1 parent
9c9155d
commit e36b937
Showing
12 changed files
with
171 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/libraries/System.Runtime.InteropServices/gen/ComInterfaceGenerator/ComClassInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Microsoft.Interop | ||
{ | ||
internal sealed record ComClassInfo | ||
{ | ||
public string ClassName { get; init; } | ||
public ContainingSyntaxContext ContainingSyntaxContext { get; init; } | ||
public ContainingSyntax ClassSyntax { get; init; } | ||
public SequenceEqualImmutableArray<string> ImplementedInterfacesNames { get; init; } | ||
|
||
private ComClassInfo(string className, ContainingSyntaxContext containingSyntaxContext, ContainingSyntax classSyntax, SequenceEqualImmutableArray<string> implementedInterfacesNames) | ||
{ | ||
ClassName = className; | ||
ContainingSyntaxContext = containingSyntaxContext; | ||
ClassSyntax = classSyntax; | ||
ImplementedInterfacesNames = implementedInterfacesNames; | ||
} | ||
|
||
public static DiagnosticOr<ComClassInfo> From(INamedTypeSymbol type, ClassDeclarationSyntax syntax, bool unsafeCodeIsEnabled) | ||
{ | ||
if (!unsafeCodeIsEnabled) | ||
{ | ||
return DiagnosticOr<ComClassInfo>.From(DiagnosticInfo.Create(GeneratorDiagnostics.RequiresAllowUnsafeBlocks, syntax.Identifier.GetLocation())); | ||
} | ||
|
||
if (!syntax.IsInPartialContext(out _)) | ||
{ | ||
return DiagnosticOr<ComClassInfo>.From( | ||
DiagnosticInfo.Create( | ||
GeneratorDiagnostics.InvalidAttributedClassMissingPartialModifier, | ||
syntax.Identifier.GetLocation(), | ||
type.ToDisplayString())); | ||
} | ||
|
||
ImmutableArray<string>.Builder names = ImmutableArray.CreateBuilder<string>(); | ||
foreach (INamedTypeSymbol iface in type.AllInterfaces) | ||
{ | ||
AttributeData? generatedComInterfaceAttribute = iface.GetAttributes().FirstOrDefault(attr => attr.AttributeClass?.ToDisplayString() == TypeNames.GeneratedComInterfaceAttribute); | ||
if (generatedComInterfaceAttribute is not null) | ||
{ | ||
var attributeData = GeneratedComInterfaceCompilationData.GetDataFromAttribute(generatedComInterfaceAttribute); | ||
if (attributeData.Options.HasFlag(ComInterfaceOptions.ManagedObjectWrapper)) | ||
{ | ||
names.Add(iface.ToDisplayString()); | ||
} | ||
} | ||
} | ||
|
||
if (names.Count == 0) | ||
{ | ||
return DiagnosticOr<ComClassInfo>.From(DiagnosticInfo.Create(GeneratorDiagnostics.ClassDoesNotImplementAnyGeneratedComInterface, | ||
syntax.Identifier.GetLocation(), | ||
type.ToDisplayString())); | ||
} | ||
|
||
return DiagnosticOr<ComClassInfo>.From( | ||
new ComClassInfo( | ||
type.ToDisplayString(), | ||
new ContainingSyntaxContext(syntax), | ||
new ContainingSyntax(syntax.Modifiers, syntax.Kind(), syntax.Identifier, syntax.TypeParameterList), | ||
new(names.ToImmutable()))); | ||
} | ||
|
||
public bool Equals(ComClassInfo? other) | ||
{ | ||
return other is not null | ||
&& ClassName == other.ClassName | ||
&& ContainingSyntaxContext.Equals(other.ContainingSyntaxContext) | ||
&& ImplementedInterfacesNames.SequenceEqual(other.ImplementedInterfacesNames); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(ClassName, ContainingSyntaxContext, ImplementedInterfacesNames); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.