Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exports from nested classes. #85

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 28 additions & 15 deletions src/dnne-gen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public Generator(string validAssemblyPath)

// Check for platform scenario attributes
AssemblyDefinition asmDef = this.mdReader.GetAssemblyDefinition();
this.assemblyScope = GetOSPlatformScope(asmDef.GetCustomAttributes());
this.assemblyScope = this.GetOSPlatformScope(asmDef.GetCustomAttributes());

ModuleDefinition modDef = this.mdReader.GetModuleDefinition();
this.moduleScope = GetOSPlatformScope(modDef.GetCustomAttributes());
this.moduleScope = this.GetOSPlatformScope(modDef.GetCustomAttributes());
}

public void Emit(string outputFile)
Expand Down Expand Up @@ -118,11 +118,11 @@ public void Emit(TextWriter outputStream)
if (currAttrType == ExportType.None)
{
// Check if method has other supported attributes.
if (TryGetC99DeclCodeAttributeValue(customAttr, out string c99Decl))
if (this.TryGetC99DeclCodeAttributeValue(customAttr, out string c99Decl))
{
additionalCodeStatements.Add(c99Decl);
}
else if (TryGetOSPlatformAttributeValue(customAttr, out bool isSupported, out OSPlatform scen))
else if (this.TryGetOSPlatformAttributeValue(customAttr, out bool isSupported, out OSPlatform scen))
{
if (isSupported)
{
Expand Down Expand Up @@ -211,13 +211,7 @@ public void Emit(TextWriter outputStream)

// Extract method details
var typeDef = this.mdReader.GetTypeDefinition(methodDef.GetDeclaringType());
var classString = this.mdReader.GetString(typeDef.Name);

// Exporting from nested types is not supported.
if (typeDef.IsNested)
{
throw new GeneratorException(this.assemblyPath, $"Method '{this.mdReader.GetString(methodDef.Name)}' is being exported by nested type {classString}.");
}
var enclosingTypeName = this.ComputeEnclosingTypeName(typeDef);

// Process method signature.
MethodSignature<string> signature;
Expand Down Expand Up @@ -275,11 +269,10 @@ public void Emit(TextWriter outputStream)
}
}

var namespaceString = this.mdReader.GetString(typeDef.Namespace);
exportedMethods.Add(new ExportedMethod()
{
Type = exportAttrType,
EnclosingTypeName = namespaceString + Type.Delimiter + classString,
EnclosingTypeName = enclosingTypeName,
MethodName = managedMethodName,
ExportName = exportName,
CallingConvention = callConv,
Expand Down Expand Up @@ -344,6 +337,26 @@ private ExportType GetExportAttributeType(CustomAttribute attribute)
}
}

private string ComputeEnclosingTypeName(TypeDefinition typeDef)
{
var enclosingTypes = new List<string>() { this.mdReader.GetString(typeDef.Name) };
TypeDefinition parentTypeDef = typeDef;
while (parentTypeDef.IsNested)
{
parentTypeDef = this.mdReader.GetTypeDefinition(parentTypeDef.GetDeclaringType());
enclosingTypes.Add(this.mdReader.GetString(parentTypeDef.Name));
}

enclosingTypes.Reverse();
string name = string.Join("+", enclosingTypes);
if (!parentTypeDef.Namespace.IsNil)
{
name = $"{this.mdReader.GetString(parentTypeDef.Namespace)}{Type.Delimiter}{name}";
}

return name;
}

private bool TryGetC99TypeAttributeValue(CustomAttribute attribute, out string c99Type)
{
c99Type = IsAttributeType(this.mdReader, attribute, "DNNE", "C99TypeAttribute")
Expand All @@ -369,7 +382,7 @@ private Scope GetTypeOSPlatformScope(MethodDefinition methodDef)
}

TypeDefinition typeDef = this.mdReader.GetTypeDefinition(typeDefHandle);
var typeScope = GetOSPlatformScope(typeDef.GetCustomAttributes());
var typeScope = this.GetOSPlatformScope(typeDef.GetCustomAttributes());

// Record and return the scenarios.
this.typePlatformScenarios.Add(typeDefHandle, typeScope);
Expand All @@ -383,7 +396,7 @@ private Scope GetOSPlatformScope(CustomAttributeHandleCollection attrs)
foreach (var customAttrHandle in attrs)
{
CustomAttribute customAttr = this.mdReader.GetCustomAttribute(customAttrHandle);
if (TryGetOSPlatformAttributeValue(customAttr, out bool isSupported, out OSPlatform scen))
if (this.TryGetOSPlatformAttributeValue(customAttr, out bool isSupported, out OSPlatform scen))
{
if (isSupported)
{
Expand Down
9 changes: 9 additions & 0 deletions test/DNNE.UnitTests/Consumption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,14 @@ public unsafe void UnsafeExports()
Assert.True(UIntPtr.Zero == ExportingAssembly.UnsafeExports.VoidUIntPtr());
Assert.True(UIntPtr.Zero == ExportingAssembly.UnsafeExports.UnmanagedVoidUIntPtr());
}

[Fact]
public void NestedClassExports()
{
ExportingAssembly.NestedClassExports.Nested1_VoidVoid();
ExportingAssembly.NestedClassExports.Nested1_UnmanagedVoidVoid();
ExportingAssembly.NestedClassExports.Nested2_VoidVoid();
ExportingAssembly.NestedClassExports.Nested2_UnmanagedVoidVoid();
}
}
}
15 changes: 15 additions & 0 deletions test/DNNE.UnitTests/ExportingAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,5 +221,20 @@ public unsafe static class UnsafeExports
[DllImport(nameof(ExportingAssemblyNE))]
public static extern UIntPtr UnmanagedVoidUIntPtr();
}

public unsafe static class NestedClassExports
{
[DllImport(nameof(ExportingAssemblyNE))]
public static extern void Nested1_VoidVoid();

[DllImport(nameof(ExportingAssemblyNE))]
public static extern void Nested1_UnmanagedVoidVoid();

[DllImport(nameof(ExportingAssemblyNE))]
public static extern void Nested2_VoidVoid();

[DllImport(nameof(ExportingAssemblyNE))]
public static extern void Nested2_UnmanagedVoidVoid();
}
}
}
56 changes: 56 additions & 0 deletions test/ExportingAssembly/NestedClassExports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2020 Aaron R Robinson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System.Runtime.InteropServices;

namespace ExportingAssembly
{
public class NestedClassExports
{
public class Nested1
{
public delegate void Nested1_VoidVoidDelegate();

[DNNE.Export]
public static void Nested1_VoidVoid()
{
}

[UnmanagedCallersOnly]
public static void Nested1_UnmanagedVoidVoid()
{
}

public class Nested2
{
public delegate void Nested2_VoidVoidDelegate();

[DNNE.Export]
public static void Nested2_VoidVoid()
{
}

[UnmanagedCallersOnly]
public static void Nested2_UnmanagedVoidVoid()
{
}
}
}
}
}