Skip to content

Commit

Permalink
Use declaring syntax as a baseline for generated syntax for containin…
Browse files Browse the repository at this point in the history
…g types. (#108)
  • Loading branch information
jkoritzinsky authored Sep 17, 2020
1 parent e787027 commit 4bb5cb0
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions DllImportGenerator/DllImportGenerator/DllImportStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,14 @@ public static DllImportStub Create(
INamedTypeSymbol currType = method.ContainingType;
while (!(currType is null))
{
var visibility = currType.DeclaredAccessibility switch
{
Accessibility.Public => SyntaxKind.PublicKeyword,
Accessibility.Private => SyntaxKind.PrivateKeyword,
Accessibility.Protected => SyntaxKind.ProtectedKeyword,
Accessibility.Internal => SyntaxKind.InternalKeyword,
_ => throw new NotSupportedException(), // [TODO] Proper error message
};

TypeDeclarationSyntax typeDecl = currType.TypeKind switch
{
TypeKind.Class => ClassDeclaration(currType.Name),
TypeKind.Struct => StructDeclaration(currType.Name),
_ => throw new NotSupportedException(), // [TODO] Proper error message
};

typeDecl = typeDecl.AddModifiers(
Token(visibility),
Token(SyntaxKind.PartialKeyword));
// Use the declaring syntax as a basis for this type declaration.
// Since we're generating source for the method, we know that the current type
// has to be declared in source.
TypeDeclarationSyntax typeDecl = (TypeDeclarationSyntax)currType.DeclaringSyntaxReferences[0].GetSyntax();
// Remove current members and attributes so we don't double declare them.
typeDecl = typeDecl.WithMembers(List<MemberDeclarationSyntax>())
.WithAttributeLists(List<AttributeListSyntax>());

containingTypes.Add(typeDecl);

currType = currType.ContainingType;
Expand Down

0 comments on commit 4bb5cb0

Please sign in to comment.