Skip to content

Commit

Permalink
Add back support for config mapped names
Browse files Browse the repository at this point in the history
  • Loading branch information
lithiumtoast committed Jan 2, 2025
1 parent 0b4d3c1 commit 91ec2c6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Bottlenose Labs Inc. (https://github.com/bottlenoselabs). All rights reserved.
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System.Collections.Immutable;
using NuGet.Frameworks;

namespace C2CS.GenerateCSharpCode;
Expand All @@ -23,6 +24,8 @@ public class InputSanitized

public string CodeRegionFooter { get; init; } = string.Empty;

public ImmutableDictionary<string, string> MappedNames { get; init; } = ImmutableDictionary<string, string>.Empty;

public bool IsEnabledGenerateCSharpRuntimeCode { get; init; }

public bool IsEnabledFunctionPointers { get; init; }
Expand Down
26 changes: 25 additions & 1 deletion src/cs/production/c2cs.Tool/GenerateCSharpCode/InputSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.IO.Abstractions;
using bottlenoselabs.Common.Tools;
Expand All @@ -15,7 +17,6 @@ public override InputSanitized Sanitize(InputUnsanitized unsanitizedInput)
{
var inputFilePath = InputFilePath(unsanitizedInput.InputCrossPlatformFfiFilePath);
var outputFileDirectory = OutputFileDirectory(unsanitizedInput.OutputCSharpCodeFileDirectory);

var className = ClassName(unsanitizedInput.ClassName);
var libraryName = LibraryName(unsanitizedInput.LibraryName, className);
var targetFramework = TargetFramework(unsanitizedInput.TargetFrameworkMoniker);
Expand All @@ -30,6 +31,7 @@ public override InputSanitized Sanitize(InputUnsanitized unsanitizedInput)
NamespaceName = NamespaceName(unsanitizedInput.NamespaceName),
CodeRegionHeader = HeaderCodeRegion(unsanitizedInput.HeaderCodeRegionFilePath),
CodeRegionFooter = FooterCodeRegion(unsanitizedInput.FooterCodeRegionFilePath),
MappedNames = MappedNames(unsanitizedInput.MappedNames),
IsEnabledGenerateCSharpRuntimeCode = unsanitizedInput.IsEnabledGeneratingRuntimeCode ?? true,
IsEnabledFunctionPointers = IsEnabledFunctionPointers(unsanitizedInput, targetFramework),
IsEnabledRuntimeMarshalling = IsEnabledRuntimeMarshalling(unsanitizedInput, targetFramework),
Expand Down Expand Up @@ -164,6 +166,28 @@ private NuGetFramework TargetFramework(string? targetFrameworkMoniker)
return nuGetFramework;
}

private ImmutableDictionary<string, string> MappedNames(ImmutableArray<InputUnsanitizedMappedName>? mappedNames)
{
if (mappedNames == null)
{
return ImmutableDictionary<string, string>.Empty;
}

var dictionary = new Dictionary<string, string>();

foreach (var mappedName in mappedNames)
{
if (string.IsNullOrEmpty(mappedName.Source) || string.IsNullOrEmpty(mappedName.Target))
{
continue;
}

dictionary.Add(mappedName.Source, mappedName.Target);
}

return dictionary.ToImmutableDictionary();
}

private static bool IsEnabledFunctionPointers(InputUnsanitized unsanitizedInput, NuGetFramework nuGetFramework)
{
// Function pointers are supported only in C# 9+ and .NET 5+
Expand Down
6 changes: 6 additions & 0 deletions src/cs/production/c2cs.Tool/GenerateCSharpCode/NameMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public NameMapper(CodeGeneratorContext context)
_cSharpNamesByCNames.Add("FILE*", "IntPtr");
_cSharpNamesByCNames.Add("DIR*", "IntPtr");
_cSharpNamesByCNames.Add("va_list", "IntPtr");

// config specified
foreach (var (source, target) in context.Input.MappedNames)
{
_cSharpNamesByCNames.Add(source, target);
}
}

public string GetIdentifierCSharp(string nameC)
Expand Down

0 comments on commit 91ec2c6

Please sign in to comment.