From 810ff6d84021a9c327e9ae0a38a41da04edb18f8 Mon Sep 17 00:00:00 2001 From: Lucas Girouard-Stranks <519592+lithiumtoast@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:57:43 -0400 Subject: [PATCH 1/2] First pass in converting to using `c2ffi` --- .github/workflows/test.yml | 6 +- docs/README.md | 14 +- docs/SUPPORTED-PLATFORMS.md | 4 +- .../Generated/AssemblyAttributes.gen.cs | 2 +- .../helloworld-app/Generated/Runtime.gen.cs | 23 +- .../Generated/my_c_library.gen.cs | 10 +- .../Program.cs | 10 +- .../config-generate-cs.json | 2 +- .../ffi-x/cross-platform.json | 308 +++++++++ .../ffi/aarch64-apple-darwin.json | 600 ++++++++++++++++++ .../ffi/x86_64-apple-darwin.json | 600 ++++++++++++++++++ .../Attributes/CConstAttribute.cs | 15 - .../C2CS.Runtime/Attributes/CNodeAttribute.cs | 18 - src/cs/production/C2CS.Tool/C2CS.Tool.csproj | 2 +- .../C2CS.Tool/CommandLineInterface.cs | 6 +- .../BuildCLibrary/BuildCLibraryCommand.cs | 4 +- .../BuildCLibrary/BuildCLibraryTool.cs | 12 +- .../Domain/CMakeLibraryBuilder.cs | 4 +- .../Input/BuildCLibraryInputSanitizer.cs | 6 +- .../Input/Sanitized/BuildCLibraryInput.cs | 2 +- .../Input/Unsanitized/BuildCLibraryOptions.cs | 2 +- .../Output/BuildCLibraryOutput.cs | 4 +- .../BuildCLibrary/Startup.cs | 6 +- .../Data/CSharpAbstractSyntaxTree.cs | 2 +- .../WriteCodeCSharp/Data/CSharpAliasType.cs | 15 +- .../WriteCodeCSharp/Data/CSharpConstant.cs | 7 +- .../WriteCodeCSharp/Data/CSharpEnum.cs | 19 +- .../WriteCodeCSharp/Data/CSharpEnumValue.cs | 7 +- .../WriteCodeCSharp/Data/CSharpFunction.cs | 17 +- .../Data/CSharpFunctionCallingConvention.cs | 2 +- .../Data/CSharpFunctionParameter.cs | 7 +- .../Data/CSharpFunctionPointer.cs | 17 +- .../WriteCodeCSharp/Data/CSharpMacroObject.cs | 7 +- .../WriteCodeCSharp/Data/CSharpMappedName.cs | 2 +- .../WriteCodeCSharp/Data/CSharpNode.cs | 7 +- .../WriteCodeCSharp/Data/CSharpOpaqueType.cs | 7 +- .../WriteCodeCSharp/Data/CSharpParameter.cs | 15 +- .../WriteCodeCSharp/Data/CSharpStruct.cs | 7 +- .../WriteCodeCSharp/Data/CSharpStructField.cs | 17 +- .../WriteCodeCSharp/Data/CSharpType.cs} | 15 +- .../CodeGenerator/CSharpCodeGenerator.cs | 6 +- .../CSharpCodeGeneratorContext.cs | 13 +- .../CSharpCodeGeneratorOptions.cs | 2 +- .../CodeGenerator/CSharpLibraryCompiler.cs | 4 +- .../Domain/CodeGenerator/CSharpProject.cs | 2 +- .../CodeGenerator/CSharpProjectDocument.cs | 2 +- .../Diagnostics/CSharpCompileDiagnostic.cs | 2 +- .../CSharpCompileSkipDiagnostic.cs | 2 +- .../Domain/CodeGenerator/ExtensionsRoslyn.cs | 2 +- .../CodeGenerator/GenerateCodeHandler.cs | 4 +- .../Handlers/AliasTypeCodeGenerator.cs | 15 +- .../Handlers/ConstantCodeGenerator.cs | 7 +- .../Handlers/EnumCodeGenerator.cs | 7 +- .../Handlers/FunctionCodeGenerator.cs | 15 +- .../Handlers/FunctionPointerCodeGenerator.cs | 12 +- .../Handlers/MacroCodeGenerator.cs | 8 +- .../Handlers/OpaqueTypeCodeGenerator.cs | 7 +- .../Handlers/StructCodeGenerator.cs | 37 +- .../Domain/Mapper/CSharpCodeMapper.cs | 273 +++----- .../Domain/Mapper/CSharpCodeMapperContext.cs | 45 ++ .../Domain/Mapper/CSharpCodeMapperOptions.cs | 4 +- .../Input/Sanitized/WriteCodeCSharpInput.cs | 6 +- .../Unsanitized/WriteCSharpCodeOptions.cs | 2 +- .../WriteCSharpCodeOptionsMappedName.cs | 2 +- .../Input/WriteCodeCSharpInputSanitizer.cs | 12 +- .../Output/WriteCodeCSharpOutput.cs | 6 +- .../WriteCodeCSharp/Startup.cs | 6 +- .../WriteCodeCSharp/WriteCodeCSharpCommand.cs | 2 +- .../WriteCodeCSharp/WriteCodeCSharpTool.cs | 36 +- .../Domain/Mapper/CSharpCodeMapperContext.cs | 26 - .../C2CS.Tool/Internal/StringExtensions.cs | 24 + src/cs/production/C2CS.Tool/Startup.cs | 4 +- .../tests/C2CS.Tests/TestFixtureCSharpCode.cs | 26 +- src/cs/tests/C2CS.Tests/TestHost.cs | 2 +- 74 files changed, 1903 insertions(+), 550 deletions(-) create mode 100644 src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi-x/cross-platform.json create mode 100644 src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/aarch64-apple-darwin.json create mode 100644 src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/x86_64-apple-darwin.json delete mode 100644 src/cs/production/C2CS.Runtime/Attributes/CConstAttribute.cs delete mode 100644 src/cs/production/C2CS.Runtime/Attributes/CNodeAttribute.cs rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/BuildCLibraryCommand.cs (94%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/BuildCLibraryTool.cs (79%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/Domain/CMakeLibraryBuilder.cs (98%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs (95%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs (92%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs (97%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/Output/BuildCLibraryOutput.cs (79%) rename src/cs/production/C2CS.Tool/{Features => Commands}/BuildCLibrary/Startup.cs (78%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs (95%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpAliasType.cs (68%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpConstant.cs (85%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpEnum.cs (59%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpEnumValue.cs (84%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpFunction.cs (75%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs (87%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpFunctionParameter.cs (84%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpFunctionPointer.cs (72%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpMacroObject.cs (86%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpMappedName.cs (92%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpNode.cs (93%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpOpaqueType.cs (82%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpParameter.cs (67%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpStruct.cs (87%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Data/CSharpStructField.cs (77%) rename src/cs/production/C2CS.Tool/{Features/WriteCodeCSharp/Data/CSharpTypeInfo.cs => Commands/WriteCodeCSharp/Data/CSharpType.cs} (77%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs (98%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs (92%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs (93%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs (97%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs (86%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs (85%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs (90%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs (90%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs (97%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs (93%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs (63%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs (80%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs (93%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs (85%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs (79%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs (83%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs (79%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs (81%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs (83%) create mode 100644 src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs (97%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs (76%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs (99%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs (93%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs (95%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs (79%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/Startup.cs (88%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/WriteCodeCSharpCommand.cs (96%) rename src/cs/production/C2CS.Tool/{Features => Commands}/WriteCodeCSharp/WriteCodeCSharpTool.cs (77%) delete mode 100644 src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs create mode 100644 src/cs/production/C2CS.Tool/Internal/StringExtensions.cs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8acaf767..59556b31 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,7 +27,7 @@ jobs: if: matrix.platform.os == 'windows-latest' run: | source "${{ github.workspace }}\.github\scripts\install-clang-x64-windows.sh" - + - name: "Install Clang: Linux" if: matrix.platform.os == 'ubuntu-latest' run: | @@ -39,9 +39,9 @@ jobs: with: dotnet-version: '8.x' - - name: "Install CAstFfi" + - name: "Install c2ffi" shell: bash - run: dotnet tool install --global bottlenoselabs.CAstFfi.Tool + run: dotnet tool install --global bottlenoselabs.c2ffi.tool - name: "Test .NET solution" run: | diff --git a/docs/README.md b/docs/README.md index d4dafa21..d5e7dafa 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,7 +7,7 @@ - [Latest release of `C2CS`](#latest-release-of-c2cs) - [Latest pre-release of `C2CS`](#latest-pre-release-of-c2cs) - [How to use `C2CS`](#how-to-use-c2cs) - - [Installing and using `CAstFfi`](#installing-and-using-castffi) + - [Installing and using `c2ffi`](#installing-and-using-c2ffi) - [Execute `c2cs`](#execute-c2cs) - [How to use `C2CS.Runtime`](#how-to-use-c2csruntime) - [Building `C2CS` from source](#building-c2cs-from-source) @@ -51,17 +51,17 @@ dotnet nuget locals all --clear ## How to use `C2CS` -To generate C# bindings for a C library you need to install and use the `CAstFfi` tool and setup a couple configuration files. See the [`helloworld`](../src/cs/examples/helloworld/) example projects for an example. +To generate C# bindings for a C library you need to install and use the `c2ffi` tool and setup a couple configuration files. See the [`helloworld`](../src/cs/examples/helloworld/) example projects for an example. -### Installing and using `CAstFfi` +### Installing and using `c2ffi` -See the auxiliary project `Getting Started` section: https://github.com/bottlenoselabs/CAstFfi#getting-started. +See the auxiliary project `Getting Started` section: https://github.com/bottlenoselabs/c2ffi#getting-started. -You should extract all the platform specific abstract syntax trees you wish to have as target platforms. See the [`helloworld-compile-c-library-and-generate-bindings`](../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/) for example configuration files for Windows, macOS, and Linux platforms. +You should extract all the platform specific FFIs you wish to have as target platforms. See the [`helloworld-compile-c-library-and-generate-bindings`](../src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/) for example configuration files for Windows, macOS, and Linux platforms. -Once all the platform abstract syntax trees are extracted to a directory, merge them together into a cross-platform abstract syntax tree using `CAstFfi merge` option. +Once all the platform FFIs are extracted to a directory, merge them together into a cross-platform FFI using `c2ffi merge` option. -Once you have a cross-platform abstract syntax tree, you are ready to use `c2cs`. +Once you have a cross-platform FFI `.json` file, you are ready to use `c2cs`. ### Execute `c2cs` diff --git a/docs/SUPPORTED-PLATFORMS.md b/docs/SUPPORTED-PLATFORMS.md index 1c60ec13..819bccb4 100644 --- a/docs/SUPPORTED-PLATFORMS.md +++ b/docs/SUPPORTED-PLATFORMS.md @@ -9,7 +9,7 @@ - [Problem](#problem) - [Solution](#solution) - [Calling C platform code from your .NET application](#calling-c-platform-code-from-your-net-application) - - [Dynamic loading & dynamic linking](#dynamic-loading--dynamic-linking) + - [Dynamic loading \& dynamic linking](#dynamic-loading--dynamic-linking) - [Dynamic loading](#dynamic-loading) - [Dynamic linking](#dynamic-linking) - [Static linking](#static-linking) @@ -58,7 +58,7 @@ While Clang is free and open-source, an annoying problem is that some target pla `C2CS` the main flow has evolved into two programs to overcome this problem. -1. `ast`: (NOTE: Moved to https://github.com/bottlenoselabs/CAstFfi !) Reading C code and extracting information of the target platform for generating C# code later in form a C abstract syntax tree `.json` file. The extracted information _**does not contain any source code**_; it is purely meta data about the bitwidth and names of functions, structs, enums, typedefs, etc. +1. `ast`: (NOTE: Moved to https://github.com/bottlenoselabs/c2ffi !) Reading C code and extracting information of the target platform for generating C# code later in from a C ffi `.json` file. The extracted information _**does not contain any source code**_; it is purely meta data about the bitwidth and names of functions, structs, enums, typedefs, etc. 2. `cs`: (NOTE: now just the main `c2cs` program!) Reading one or more previously generated C abstract syntax tree `.json` files to generate C# code for the specified target platforms. This allows the `ast` program of `C2CS` to run and extract the information for the target platforms with restrictive system headers. The resulting `.json` files of the C abstract syntax trees can be moved to any operating system to generate the C# code for multiple target platforms at once using the `cs` program of `C2CS`. diff --git a/src/cs/examples/helloworld/helloworld-app/Generated/AssemblyAttributes.gen.cs b/src/cs/examples/helloworld/helloworld-app/Generated/AssemblyAttributes.gen.cs index 35629c30..64359f59 100644 --- a/src/cs/examples/helloworld/helloworld-app/Generated/AssemblyAttributes.gen.cs +++ b/src/cs/examples/helloworld/helloworld-app/Generated/AssemblyAttributes.gen.cs @@ -1,6 +1,6 @@ // To disable generating this file set `isEnabledGenerateAssemblyAttributes` to `false` in the config file for generating C# code. // -// This code was generated by the following tool on 2023-11-22 17:54:54 GMT-05:00: +// This code was generated by the following tool on 2024-04-10 18:55:28 GMT-04:00: // https://github.com/bottlenoselabs/c2cs (v0.0.0.0) // // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. diff --git a/src/cs/examples/helloworld/helloworld-app/Generated/Runtime.gen.cs b/src/cs/examples/helloworld/helloworld-app/Generated/Runtime.gen.cs index c691771c..bf234ebd 100644 --- a/src/cs/examples/helloworld/helloworld-app/Generated/Runtime.gen.cs +++ b/src/cs/examples/helloworld/helloworld-app/Generated/Runtime.gen.cs @@ -2,7 +2,7 @@ // To disable generating this file set `isEnabledGeneratingRuntimeCode` to `false` in the config file for generating C# code. // -// This code was generated by the following tool on 2023-11-22 17:54:54 GMT-05:00: +// This code was generated by the following tool on 2024-04-10 18:55:28 GMT-04:00: // https://github.com/bottlenoselabs/c2cs (v0.0.0.0) // // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. @@ -24,27 +24,6 @@ namespace bottlenoselabs.C2CS.Runtime { - /// - /// Specifies a C `const` for a parameter or return value. - /// - [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)] - public sealed class CConstAttribute : Attribute - { - // marker - } - - /// - /// Specifies a C node with a specific kind for a C# type. - /// - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Enum | AttributeTargets.Field)] - public sealed class CNodeAttribute : Attribute - { - /// - /// Gets or sets the C node kind. - /// - public string Kind { get; set; } = string.Empty; - } - /// /// A boolean value type with the same memory layout as a in both managed and unmanaged contexts; /// equivalent to a standard bool found in C/C++/ObjC where 0 is false and any other value is diff --git a/src/cs/examples/helloworld/helloworld-app/Generated/my_c_library.gen.cs b/src/cs/examples/helloworld/helloworld-app/Generated/my_c_library.gen.cs index a38f2cf2..8a64b632 100644 --- a/src/cs/examples/helloworld/helloworld-app/Generated/my_c_library.gen.cs +++ b/src/cs/examples/helloworld/helloworld-app/Generated/my_c_library.gen.cs @@ -1,6 +1,6 @@ // -// This code was generated by the following tool on 2023-11-22 17:54:54 GMT-05:00: +// This code was generated by the following tool on 2024-04-10 18:55:28 GMT-04:00: // https://github.com/bottlenoselabs/c2cs (v0.0.0.0) // // Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. @@ -28,32 +28,26 @@ public static unsafe partial class my_c_library #region API - [CNode(Kind = "Function")] [LibraryImport(LibraryName, EntryPoint = "hw_hello_world")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] public static partial void hw_hello_world(); - [CNode(Kind = "Function")] [LibraryImport(LibraryName, EntryPoint = "hw_invoke_callback")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] public static partial void hw_invoke_callback(FnPtr_CString_Void f, CString s); - [CNode(Kind = "Function")] [LibraryImport(LibraryName, EntryPoint = "hw_pass_enum")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] public static partial void hw_pass_enum(hw_my_enum_week_day e); - [CNode(Kind = "Function")] [LibraryImport(LibraryName, EntryPoint = "hw_pass_integers_by_reference")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] public static partial void hw_pass_integers_by_reference(ushort* a, int* b, ulong* c); - [CNode(Kind = "Function")] [LibraryImport(LibraryName, EntryPoint = "hw_pass_integers_by_value")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] public static partial void hw_pass_integers_by_value(ushort a, int b, ulong c); - [CNode(Kind = "Function")] [LibraryImport(LibraryName, EntryPoint = "hw_pass_string")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] public static partial void hw_pass_string(CString s); @@ -62,14 +56,12 @@ public static unsafe partial class my_c_library #region Types - [CNode(Kind = "FunctionPointer")] [StructLayout(LayoutKind.Sequential)] public struct FnPtr_CString_Void { public delegate* unmanaged Pointer; } - [CNode(Kind = "Enum")] public enum hw_my_enum_week_day : int { HW_MY_ENUM_WEEK_DAY_UNKNOWN = 0, diff --git a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/Program.cs b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/Program.cs index b91d3aa8..059af49d 100644 --- a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/Program.cs +++ b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/Program.cs @@ -39,16 +39,16 @@ private static bool GenerateBindingsCSharp(string sourceDirectoryPath) { var bindgenConfigFilePath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "config-extract.json")); - var extractShellOutput = $"castffi extract --config {bindgenConfigFilePath}".ExecuteShellCommand(); + var extractShellOutput = $"c2ffi extract --config {bindgenConfigFilePath}".ExecuteShellCommand(); if (extractShellOutput.ExitCode != 0) { return false; } - var abstractSyntaxTreeDirectoryPath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ast")); - var mergedAbstractSyntaxTreeFilePath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ast", "cross-platform.json")); - var astShellOutput = $"castffi merge --inputDirectoryPath {abstractSyntaxTreeDirectoryPath} --outputFilePath {mergedAbstractSyntaxTreeFilePath}".ExecuteShellCommand(); - if (astShellOutput.ExitCode != 0) + var ffiDirectoryPath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ffi")); + var mergedFfiFilePath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ffi-x", "cross-platform.json")); + var mergedFfiShellOutput = $"c2ffi merge --inputDirectoryPath {ffiDirectoryPath} --outputFilePath {mergedFfiFilePath}".ExecuteShellCommand(); + if (mergedFfiShellOutput.ExitCode != 0) { return false; } diff --git a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/config-generate-cs.json b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/config-generate-cs.json index a9955cb4..18bf9aef 100644 --- a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/config-generate-cs.json +++ b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/config-generate-cs.json @@ -1,5 +1,5 @@ { - "inputFilePath": "./ast/cross-platform.json", + "inputFilePath": "./ffi-x/cross-platform.json", "outputFileDirectory": "./../helloworld-app/Generated", "namespaceName": "my_c_library_namespace", "className": "my_c_library", diff --git a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi-x/cross-platform.json b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi-x/cross-platform.json new file mode 100644 index 00000000..3ef9fc16 --- /dev/null +++ b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi-x/cross-platform.json @@ -0,0 +1,308 @@ +{ + "platforms": [ + "aarch64-apple-darwin", + "x86_64-apple-darwin" + ], + "macroObjects": {}, + "variables": {}, + "functions": { + "hw_hello_world": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [] + }, + "hw_invoke_callback": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [ + { + "name": "f", + "type": { + "name": "void (const char *)", + "kind": "functionPointer", + "size_of": 1, + "align_of": 4 + } + }, + { + "name": "s", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1 + } + } + } + ] + }, + "hw_pass_enum": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [ + { + "name": "e", + "type": { + "name": "hw_my_enum_week_day", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "inner_type": { + "name": "enum hw_my_enum_week_day", + "kind": "enum", + "size_of": 4, + "align_of": 4 + } + } + } + ] + }, + "hw_pass_integers_by_reference": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [ + { + "name": "a", + "type": { + "name": "const uint16_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "uint16_t", + "kind": "typeAlias", + "size_of": 2, + "align_of": 2, + "inner_type": { + "name": "unsigned short", + "kind": "primitive", + "size_of": 2, + "align_of": 2 + } + } + } + }, + { + "name": "b", + "type": { + "name": "const int32_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "int32_t", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "inner_type": { + "name": "int", + "kind": "primitive", + "size_of": 4, + "align_of": 4 + } + } + } + }, + { + "name": "c", + "type": { + "name": "const uint64_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "uint64_t", + "kind": "typeAlias", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "unsigned long long", + "kind": "primitive", + "size_of": 8, + "align_of": 8 + } + } + } + } + ] + }, + "hw_pass_integers_by_value": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [ + { + "name": "a", + "type": { + "name": "uint16_t", + "kind": "typeAlias", + "size_of": 2, + "align_of": 2, + "inner_type": { + "name": "unsigned short", + "kind": "primitive", + "size_of": 2, + "align_of": 2 + } + } + }, + { + "name": "b", + "type": { + "name": "int32_t", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "inner_type": { + "name": "int", + "kind": "primitive", + "size_of": 4, + "align_of": 4 + } + } + }, + { + "name": "c", + "type": { + "name": "uint64_t", + "kind": "typeAlias", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "unsigned long long", + "kind": "primitive", + "size_of": 8, + "align_of": 8 + } + } + } + ] + }, + "hw_pass_string": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [ + { + "name": "s", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1 + } + } + } + ] + } + }, + "records": {}, + "enums": { + "enum hw_my_enum_week_day": { + "size_of": 4, + "values": [ + { + "name": "HW_MY_ENUM_WEEK_DAY_UNKNOWN" + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_MONDAY", + "value": 1 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_TUESDAY", + "value": 2 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_WEDNESDAY", + "value": 3 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_THURSDAY", + "value": 4 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_FRIDAY", + "value": 5 + }, + { + "name": "_HW_MY_ENUM_WEEK_DAY_FORCE_U32", + "value": 2147483647 + } + ] + } + }, + "typeAliases": { + "hw_my_enum_week_day": { + "underlyingType": { + "name": "enum hw_my_enum_week_day", + "kind": "enum", + "size_of": 4, + "align_of": 4 + } + } + }, + "opaqueTypes": {}, + "functionPointers": { + "void (const char *)": { + "type": { + "name": "void (const char *)", + "kind": "functionPointer", + "size_of": 1, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "return_type": { + "name": "void", + "kind": "primitive" + }, + "parameters": [ + { + "name": "", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1 + } + } + } + ], + "CallingConvention": "cdecl" + } + } +} \ No newline at end of file diff --git a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/aarch64-apple-darwin.json b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/aarch64-apple-darwin.json new file mode 100644 index 00000000..7d2f207b --- /dev/null +++ b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/aarch64-apple-darwin.json @@ -0,0 +1,600 @@ +{ + "fileName": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "platformRequested": "aarch64-apple-darwin", + "platformActual": "arm64-apple-macosx13.0.0", + "systemIncludeDirectories": [ + "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/include", + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include" + ], + "userIncludeDirectories": [ + "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include" + ], + "pointerSize": 8, + "macroObjects": {}, + "variables": {}, + "functions": { + "hw_hello_world": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 21, + "column": 28 + } + }, + "hw_invoke_callback": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "f", + "type": { + "name": "void (const char *)", + "kind": "functionPointer", + "size_of": 1, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + { + "name": "s", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 22, + "column": 28 + } + }, + "hw_pass_enum": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "e", + "type": { + "name": "hw_my_enum_week_day", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 19, + "column": 3 + }, + "inner_type": { + "name": "enum hw_my_enum_week_day", + "kind": "enum", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 11, + "column": 14 + } + } + }, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 19, + "column": 3 + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 26, + "column": 28 + } + }, + "hw_pass_integers_by_reference": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "a", + "type": { + "name": "const uint16_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "uint16_t", + "kind": "typeAlias", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "_uint16_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint16_t.h", + "line": 31, + "column": 24, + "isSystem": true + }, + "inner_type": { + "name": "unsigned short", + "kind": "primitive", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + { + "name": "b", + "type": { + "name": "const int32_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "int32_t", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "_int32_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/sys/_types/_int32_t.h", + "line": 30, + "column": 33, + "isSystem": true + }, + "inner_type": { + "name": "int", + "kind": "primitive", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + { + "name": "c", + "type": { + "name": "const uint64_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "uint64_t", + "kind": "typeAlias", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "_uint64_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint64_t.h", + "line": 31, + "column": 28, + "isSystem": true + }, + "inner_type": { + "name": "unsigned long long", + "kind": "primitive", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 25, + "column": 28 + } + }, + "hw_pass_integers_by_value": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "a", + "type": { + "name": "uint16_t", + "kind": "typeAlias", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "_uint16_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint16_t.h", + "line": 31, + "column": 24, + "isSystem": true + }, + "inner_type": { + "name": "unsigned short", + "kind": "primitive", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "_uint16_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint16_t.h", + "line": 31, + "column": 24, + "isSystem": true + } + }, + { + "name": "b", + "type": { + "name": "int32_t", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "_int32_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/sys/_types/_int32_t.h", + "line": 30, + "column": 33, + "isSystem": true + }, + "inner_type": { + "name": "int", + "kind": "primitive", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "_int32_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/sys/_types/_int32_t.h", + "line": 30, + "column": 33, + "isSystem": true + } + }, + { + "name": "c", + "type": { + "name": "uint64_t", + "kind": "typeAlias", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "_uint64_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint64_t.h", + "line": 31, + "column": 28, + "isSystem": true + }, + "inner_type": { + "name": "unsigned long long", + "kind": "primitive", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "_uint64_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint64_t.h", + "line": 31, + "column": 28, + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 24, + "column": 28 + } + }, + "hw_pass_string": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "s", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 23, + "column": 28 + } + } + }, + "records": {}, + "enums": { + "enum hw_my_enum_week_day": { + "size_of": 4, + "values": [ + { + "name": "HW_MY_ENUM_WEEK_DAY_UNKNOWN" + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_MONDAY", + "value": 1 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_TUESDAY", + "value": 2 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_WEDNESDAY", + "value": 3 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_THURSDAY", + "value": 4 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_FRIDAY", + "value": 5 + }, + { + "name": "_HW_MY_ENUM_WEEK_DAY_FORCE_U32", + "value": 2147483647 + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 11, + "column": 14 + } + } + }, + "typeAliases": { + "hw_my_enum_week_day": { + "underlyingType": { + "name": "enum hw_my_enum_week_day", + "kind": "enum", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 11, + "column": 14 + } + }, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 19, + "column": 3 + } + } + }, + "opaqueTypes": {}, + "functionPointers": { + "void (const char *)": { + "type": { + "name": "void (const char *)", + "kind": "functionPointer", + "size_of": 1, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + } + ], + "CallingConvention": "cdecl", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } +} \ No newline at end of file diff --git a/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/x86_64-apple-darwin.json b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/x86_64-apple-darwin.json new file mode 100644 index 00000000..281ff886 --- /dev/null +++ b/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/ffi/x86_64-apple-darwin.json @@ -0,0 +1,600 @@ +{ + "fileName": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "platformRequested": "x86_64-apple-darwin", + "platformActual": "x86_64-apple-macosx13.0.0", + "systemIncludeDirectories": [ + "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.3/include", + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include" + ], + "userIncludeDirectories": [ + "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include" + ], + "pointerSize": 8, + "macroObjects": {}, + "variables": {}, + "functions": { + "hw_hello_world": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 21, + "column": 28 + } + }, + "hw_invoke_callback": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "f", + "type": { + "name": "void (const char *)", + "kind": "functionPointer", + "size_of": 1, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + { + "name": "s", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 22, + "column": 28 + } + }, + "hw_pass_enum": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "e", + "type": { + "name": "hw_my_enum_week_day", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 19, + "column": 3 + }, + "inner_type": { + "name": "enum hw_my_enum_week_day", + "kind": "enum", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 11, + "column": 14 + } + } + }, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 19, + "column": 3 + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 26, + "column": 28 + } + }, + "hw_pass_integers_by_reference": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "a", + "type": { + "name": "const uint16_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "uint16_t", + "kind": "typeAlias", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "_uint16_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint16_t.h", + "line": 31, + "column": 24, + "isSystem": true + }, + "inner_type": { + "name": "unsigned short", + "kind": "primitive", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + { + "name": "b", + "type": { + "name": "const int32_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "int32_t", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "_int32_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/sys/_types/_int32_t.h", + "line": 30, + "column": 33, + "isSystem": true + }, + "inner_type": { + "name": "int", + "kind": "primitive", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + { + "name": "c", + "type": { + "name": "const uint64_t *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "uint64_t", + "kind": "typeAlias", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "_uint64_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint64_t.h", + "line": 31, + "column": 28, + "isSystem": true + }, + "inner_type": { + "name": "unsigned long long", + "kind": "primitive", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 25, + "column": 28 + } + }, + "hw_pass_integers_by_value": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "a", + "type": { + "name": "uint16_t", + "kind": "typeAlias", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "_uint16_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint16_t.h", + "line": 31, + "column": 24, + "isSystem": true + }, + "inner_type": { + "name": "unsigned short", + "kind": "primitive", + "size_of": 2, + "align_of": 2, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "_uint16_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint16_t.h", + "line": 31, + "column": 24, + "isSystem": true + } + }, + { + "name": "b", + "type": { + "name": "int32_t", + "kind": "typeAlias", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "_int32_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/sys/_types/_int32_t.h", + "line": 30, + "column": 33, + "isSystem": true + }, + "inner_type": { + "name": "int", + "kind": "primitive", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "_int32_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/sys/_types/_int32_t.h", + "line": 30, + "column": 33, + "isSystem": true + } + }, + { + "name": "c", + "type": { + "name": "uint64_t", + "kind": "typeAlias", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "_uint64_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint64_t.h", + "line": 31, + "column": 28, + "isSystem": true + }, + "inner_type": { + "name": "unsigned long long", + "kind": "primitive", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "_uint64_t.h", + "filePath": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.3.sdk/usr/include/_types/_uint64_t.h", + "line": 31, + "column": 28, + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 24, + "column": 28 + } + }, + "hw_pass_string": { + "calling_convention": "cdecl", + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "s", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + }, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 23, + "column": 28 + } + } + }, + "records": {}, + "enums": { + "enum hw_my_enum_week_day": { + "size_of": 4, + "values": [ + { + "name": "HW_MY_ENUM_WEEK_DAY_UNKNOWN" + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_MONDAY", + "value": 1 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_TUESDAY", + "value": 2 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_WEDNESDAY", + "value": 3 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_THURSDAY", + "value": 4 + }, + { + "name": "HW_MY_ENUM_WEEK_DAY_FRIDAY", + "value": 5 + }, + { + "name": "_HW_MY_ENUM_WEEK_DAY_FORCE_U32", + "value": 2147483647 + } + ], + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 11, + "column": 14 + } + } + }, + "typeAliases": { + "hw_my_enum_week_day": { + "underlyingType": { + "name": "enum hw_my_enum_week_day", + "kind": "enum", + "size_of": 4, + "align_of": 4, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 11, + "column": 14 + } + }, + "location": { + "fileName": "my_c_library.h", + "filePath": "/Users/lstranks/Programming/bottlenoselabs/c2cs/src/cs/examples/helloworld/helloworld-compile-c-library-and-generate-bindings/my_c_library/include/my_c_library.h", + "line": 19, + "column": 3 + } + } + }, + "opaqueTypes": {}, + "functionPointers": { + "void (const char *)": { + "type": { + "name": "void (const char *)", + "kind": "functionPointer", + "size_of": 1, + "align_of": 4, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "return_type": { + "name": "void", + "kind": "primitive", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + }, + "parameters": [ + { + "name": "", + "type": { + "name": "const char *", + "kind": "pointer", + "size_of": 8, + "align_of": 8, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + }, + "inner_type": { + "name": "const char", + "kind": "primitive", + "size_of": 1, + "align_of": 1, + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } + } + ], + "CallingConvention": "cdecl", + "location": { + "fileName": "", + "filePath": "", + "isSystem": true + } + } + } +} \ No newline at end of file diff --git a/src/cs/production/C2CS.Runtime/Attributes/CConstAttribute.cs b/src/cs/production/C2CS.Runtime/Attributes/CConstAttribute.cs deleted file mode 100644 index 1758ca57..00000000 --- a/src/cs/production/C2CS.Runtime/Attributes/CConstAttribute.cs +++ /dev/null @@ -1,15 +0,0 @@ -// 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; - -namespace bottlenoselabs.C2CS.Runtime; - -/// -/// Specifies a C `const` for a parameter or return value. -/// -[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.ReturnValue)] -public sealed class CConstAttribute : Attribute -{ - // marker -} diff --git a/src/cs/production/C2CS.Runtime/Attributes/CNodeAttribute.cs b/src/cs/production/C2CS.Runtime/Attributes/CNodeAttribute.cs deleted file mode 100644 index a11ac396..00000000 --- a/src/cs/production/C2CS.Runtime/Attributes/CNodeAttribute.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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; - -namespace bottlenoselabs.C2CS.Runtime; - -/// -/// Specifies a C node with a specific kind for a C# type. -/// -[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Enum | AttributeTargets.Field)] -public sealed class CNodeAttribute : Attribute -{ - /// - /// Gets or sets the C node kind. - /// - public string Kind { get; set; } = string.Empty; -} diff --git a/src/cs/production/C2CS.Tool/C2CS.Tool.csproj b/src/cs/production/C2CS.Tool/C2CS.Tool.csproj index fe202b72..3c4b1b19 100644 --- a/src/cs/production/C2CS.Tool/C2CS.Tool.csproj +++ b/src/cs/production/C2CS.Tool/C2CS.Tool.csproj @@ -28,7 +28,7 @@ - + diff --git a/src/cs/production/C2CS.Tool/CommandLineInterface.cs b/src/cs/production/C2CS.Tool/CommandLineInterface.cs index 51e5caec..db002467 100644 --- a/src/cs/production/C2CS.Tool/CommandLineInterface.cs +++ b/src/cs/production/C2CS.Tool/CommandLineInterface.cs @@ -2,10 +2,10 @@ // Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. using System.CommandLine; -using C2CS.Features.BuildCLibrary; -using C2CS.Features.WriteCodeCSharp; +using C2CS.Commands.BuildCLibrary; +using C2CS.Commands.WriteCodeCSharp; -namespace C2CS.Foundation; +namespace C2CS; internal sealed class CommandLineInterface : RootCommand { diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/BuildCLibraryCommand.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryCommand.cs similarity index 94% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/BuildCLibraryCommand.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryCommand.cs index 76ba5c2f..c1c90300 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/BuildCLibraryCommand.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryCommand.cs @@ -2,14 +2,12 @@ // Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. using System; -using System.Collections; -using System.Collections.Generic; using System.Collections.Immutable; using System.CommandLine; using System.Linq; using JetBrains.Annotations; -namespace C2CS.Features.BuildCLibrary; +namespace C2CS.Commands.BuildCLibrary; [UsedImplicitly] public sealed class BuildCLibraryCommand : Command diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/BuildCLibraryTool.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs similarity index 79% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/BuildCLibraryTool.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs index 9c6e8b0c..6a587a96 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/BuildCLibraryTool.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs @@ -4,14 +4,14 @@ using System.Collections.Immutable; using System.IO.Abstractions; using bottlenoselabs.Common.Tools; -using C2CS.Features.BuildCLibrary.Domain; -using C2CS.Features.BuildCLibrary.Input; -using C2CS.Features.BuildCLibrary.Input.Sanitized; -using C2CS.Features.BuildCLibrary.Input.Unsanitized; -using C2CS.Features.BuildCLibrary.Output; +using C2CS.Commands.BuildCLibrary.Input; +using C2CS.Commands.BuildCLibrary.Input.Sanitized; +using C2CS.Commands.BuildCLibrary.Input.Unsanitized; +using C2CS.Commands.BuildCLibrary.Output; using Microsoft.Extensions.Logging; +using CMakeLibraryBuilder = C2CS.Commands.BuildCLibrary.Domain.CMakeLibraryBuilder; -namespace C2CS.Features.BuildCLibrary; +namespace C2CS.Commands.BuildCLibrary; public class BuildCLibraryTool : Tool { diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Domain/CMakeLibraryBuilder.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs similarity index 98% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/Domain/CMakeLibraryBuilder.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs index 8392dc78..fef22a4a 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Domain/CMakeLibraryBuilder.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs @@ -8,10 +8,10 @@ using System.IO.Abstractions; using System.Linq; using bottlenoselabs.Common; -using C2CS.Features.BuildCLibrary.Input.Sanitized; +using C2CS.Commands.BuildCLibrary.Input.Sanitized; using Microsoft.Extensions.Logging; -namespace C2CS.Features.BuildCLibrary.Domain; +namespace C2CS.Commands.BuildCLibrary.Domain; public partial class CMakeLibraryBuilder { diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs similarity index 95% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs index 650abbdf..ad603f14 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/BuildCLibraryInputSanitizer.cs @@ -6,10 +6,10 @@ using System.IO.Abstractions; using System.Linq; using bottlenoselabs.Common.Tools; -using C2CS.Features.BuildCLibrary.Input.Sanitized; -using C2CS.Features.BuildCLibrary.Input.Unsanitized; +using C2CS.Commands.BuildCLibrary.Input.Sanitized; +using C2CS.Commands.BuildCLibrary.Input.Unsanitized; -namespace C2CS.Features.BuildCLibrary.Input; +namespace C2CS.Commands.BuildCLibrary.Input; public class BuildCLibraryInputSanitizer : ToolInputSanitizer { diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs similarity index 92% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs index b7a93a85..c5018641 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/Sanitized/BuildCLibraryInput.cs @@ -3,7 +3,7 @@ using System.Collections.Immutable; -namespace C2CS.Features.BuildCLibrary.Input.Sanitized; +namespace C2CS.Commands.BuildCLibrary.Input.Sanitized; public class BuildCLibraryInput { diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs similarity index 97% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs index eb3b85a0..5860ef00 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Input/Unsanitized/BuildCLibraryOptions.cs @@ -6,7 +6,7 @@ using bottlenoselabs.Common.Tools; using JetBrains.Annotations; -namespace C2CS.Features.BuildCLibrary.Input.Unsanitized; +namespace C2CS.Commands.BuildCLibrary.Input.Unsanitized; // NOTE: This class is considered un-sanitized input; all strings and other types could be null. [PublicAPI] diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Output/BuildCLibraryOutput.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Output/BuildCLibraryOutput.cs similarity index 79% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/Output/BuildCLibraryOutput.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Output/BuildCLibraryOutput.cs index d55062d0..bd75b9a0 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Output/BuildCLibraryOutput.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Output/BuildCLibraryOutput.cs @@ -2,9 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. using bottlenoselabs.Common.Tools; -using C2CS.Features.BuildCLibrary.Input.Sanitized; +using C2CS.Commands.BuildCLibrary.Input.Sanitized; -namespace C2CS.Features.BuildCLibrary.Output; +namespace C2CS.Commands.BuildCLibrary.Output; public class BuildCLibraryOutput : ToolOutput { diff --git a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Startup.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Startup.cs similarity index 78% rename from src/cs/production/C2CS.Tool/Features/BuildCLibrary/Startup.cs rename to src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Startup.cs index 437fddc6..2a430ecf 100644 --- a/src/cs/production/C2CS.Tool/Features/BuildCLibrary/Startup.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Startup.cs @@ -1,11 +1,11 @@ // 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 C2CS.Features.BuildCLibrary.Domain; -using C2CS.Features.BuildCLibrary.Input; +using C2CS.Commands.BuildCLibrary.Input; using Microsoft.Extensions.DependencyInjection; +using CMakeLibraryBuilder = C2CS.Commands.BuildCLibrary.Domain.CMakeLibraryBuilder; -namespace C2CS.Features.BuildCLibrary; +namespace C2CS.Commands.BuildCLibrary; public static class Startup { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs similarity index 95% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs index 4e395256..1ef45464 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs @@ -4,7 +4,7 @@ using System.Collections.Immutable; using bottlenoselabs.Common; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed record CSharpAbstractSyntaxTree { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpAliasType.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAliasType.cs similarity index 68% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpAliasType.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAliasType.cs index ec69994b..db1fcd6a 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpAliasType.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAliasType.cs @@ -4,22 +4,21 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpAliasType : CSharpNode { - public readonly CSharpTypeInfo UnderlyingTypeInfo; + public readonly CSharpType UnderlyingType; public CSharpAliasType( string name, string className, string cName, int? sizeOf, - CSharpTypeInfo underlyingTypeInfo, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + CSharpType underlyingType) + : base(name, className, cName, sizeOf) { - UnderlyingTypeInfo = underlyingTypeInfo; + UnderlyingType = underlyingType; } public override bool Equals(CSharpNode? other) @@ -29,13 +28,13 @@ public override bool Equals(CSharpNode? other) return false; } - return UnderlyingTypeInfo == other2.UnderlyingTypeInfo; + return UnderlyingType == other2.UnderlyingType; } public override int GetHashCode() { var baseHashCode = base.GetHashCode(); - var hashCode = HashCode.Combine(baseHashCode, UnderlyingTypeInfo); + var hashCode = HashCode.Combine(baseHashCode, UnderlyingType); return hashCode; } } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpConstant.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpConstant.cs similarity index 85% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpConstant.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpConstant.cs index 96059219..d63c9f08 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpConstant.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpConstant.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpConstant : CSharpNode { @@ -18,9 +18,8 @@ public CSharpConstant( string cName, int? sizeOf, string type, - string value, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + string value) + : base(name, className, cName, sizeOf) { Type = type; Value = value; diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpEnum.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpEnum.cs similarity index 59% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpEnum.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpEnum.cs index baeb845d..267cdd29 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpEnum.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpEnum.cs @@ -6,24 +6,20 @@ using System.Linq; using C2CS.Foundation; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpEnum : CSharpNode { - public readonly CSharpTypeInfo IntegerTypeInfo; - public readonly ImmutableArray Values; public CSharpEnum( string name, string className, string cName, - CSharpTypeInfo integerTypeInfo, - ImmutableArray values, - ImmutableArray attributes) - : base(name, className, cName, integerTypeInfo.SizeOf, attributes) + int sizeOf, + ImmutableArray values) + : base(name, className, cName, sizeOf) { - IntegerTypeInfo = integerTypeInfo; Values = values; } @@ -34,16 +30,15 @@ public override bool Equals(CSharpNode? other) return false; } - return IntegerTypeInfo == other2.IntegerTypeInfo && - Values.SequenceEqual(other2.Values); + return SizeOf == other2.SizeOf; } public override int GetHashCode() { var baseHashCode = base.GetHashCode(); - var integerTypeHashCode = IntegerTypeInfo.GetHashCode(); + var sizeOfHashCode = SizeOf.GetHashCode(); var valuesHashCode = Values.GetHashCodeMembers(); - var hashCode = HashCode.Combine(baseHashCode, integerTypeHashCode, valuesHashCode); + var hashCode = HashCode.Combine(baseHashCode, sizeOfHashCode, valuesHashCode); return hashCode; } } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpEnumValue.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpEnumValue.cs similarity index 84% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpEnumValue.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpEnumValue.cs index 2c562b88..14fc8f7e 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpEnumValue.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpEnumValue.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpEnumValue : CSharpNode { @@ -15,9 +15,8 @@ public CSharpEnumValue( string className, string cName, int? sizeOf, - long value, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + long value) + : base(name, className, cName, sizeOf) { Value = value; } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunction.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunction.cs similarity index 75% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunction.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunction.cs index 8966ec42..6bf964e3 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunction.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunction.cs @@ -6,7 +6,7 @@ using System.Linq; using C2CS.Foundation; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpFunction : CSharpNode { @@ -14,7 +14,7 @@ public sealed class CSharpFunction : CSharpNode public readonly ImmutableArray Parameters; - public readonly CSharpTypeInfo ReturnTypeInfo; + public readonly CSharpType ReturnType; public CSharpFunction( string name, @@ -22,13 +22,12 @@ public CSharpFunction( string originalName, int? sizeOf, CSharpFunctionCallingConvention callingConvention, - CSharpTypeInfo returnTypeInfo, - ImmutableArray parameters, - ImmutableArray attributes) - : base(name, className, originalName, sizeOf, attributes) + CSharpType returnType, + ImmutableArray parameters) + : base(name, className, originalName, sizeOf) { CallingConvention = callingConvention; - ReturnTypeInfo = returnTypeInfo; + ReturnType = returnType; Parameters = parameters; } @@ -40,7 +39,7 @@ public override bool Equals(CSharpNode? other) } var result = CallingConvention == other2.CallingConvention && - ReturnTypeInfo == other2.ReturnTypeInfo && + ReturnType == other2.ReturnType && Parameters.SequenceEqual(other2.Parameters); return result; @@ -50,7 +49,7 @@ public override int GetHashCode() { var baseHashCode = base.GetHashCode(); var parameters = Parameters.GetHashCodeMembers(); - var hashCode = HashCode.Combine(baseHashCode, CallingConvention, ReturnTypeInfo, parameters); + var hashCode = HashCode.Combine(baseHashCode, CallingConvention, ReturnType, parameters); return hashCode; } } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs similarity index 87% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs index 4048facb..05fe1d5d 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionCallingConvention.cs @@ -1,7 +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. -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public enum CSharpFunctionCallingConvention { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionParameter.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionParameter.cs similarity index 84% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionParameter.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionParameter.cs index 81d7b488..3aa97cb9 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionParameter.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionParameter.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpFunctionParameter : CSharpNode { @@ -15,9 +15,8 @@ public CSharpFunctionParameter( string className, string cName, int? sizeOf, - string typeName, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + string typeName) + : base(name, className, cName, sizeOf) { TypeName = typeName; } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionPointer.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionPointer.cs similarity index 72% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionPointer.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionPointer.cs index 025a8587..9b5da3d7 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpFunctionPointer.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpFunctionPointer.cs @@ -6,26 +6,25 @@ using System.Linq; using C2CS.Foundation; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpFunctionPointer : CSharpNode { public readonly ImmutableArray Parameters; - public readonly CSharpTypeInfo ReturnTypeInfo; + public readonly CSharpType ReturnType; public CSharpFunctionPointer( string name, string className, string cName, int? sizeOf, - CSharpTypeInfo returnTypeInfo, - ImmutableArray parameters, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + CSharpType returnType, + ImmutableArray parameters) + : base(name, className, cName, sizeOf) { Parameters = parameters; - ReturnTypeInfo = returnTypeInfo; + ReturnType = returnType; } public override bool Equals(CSharpNode? other) @@ -35,7 +34,7 @@ public override bool Equals(CSharpNode? other) return false; } - return ReturnTypeInfo == other2.ReturnTypeInfo && + return ReturnType == other2.ReturnType && Parameters.SequenceEqual(other2.Parameters); } @@ -43,7 +42,7 @@ public override int GetHashCode() { var baseHashCode = base.GetHashCode(); var parametersHashCode = Parameters.GetHashCodeMembers(); - var hashCode = HashCode.Combine(baseHashCode, ReturnTypeInfo, parametersHashCode); + var hashCode = HashCode.Combine(baseHashCode, ReturnType, parametersHashCode); return hashCode; } } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpMacroObject.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpMacroObject.cs similarity index 86% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpMacroObject.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpMacroObject.cs index 45f5a123..23aff7fc 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpMacroObject.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpMacroObject.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpMacroObject : CSharpNode { @@ -20,9 +20,8 @@ public CSharpMacroObject( int? sizeOf, string type, string value, - bool isConstant, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + bool isConstant) + : base(name, className, cName, sizeOf) { Type = type; Value = value; diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpMappedName.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpMappedName.cs similarity index 92% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpMappedName.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpMappedName.cs index 24552e2f..a7f19a5c 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpMappedName.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpMappedName.cs @@ -3,7 +3,7 @@ using System.Text.Json.Serialization; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; // NOTE: Properties are required for System.Text.Json serialization public sealed class CSharpMappedName diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpNode.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpNode.cs similarity index 93% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpNode.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpNode.cs index 21013b2c..09655c70 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpNode.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpNode.cs @@ -4,7 +4,7 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public abstract class CSharpNode : IEquatable, IComparable { @@ -13,21 +13,18 @@ public abstract class CSharpNode : IEquatable, IComparable Attributes; protected CSharpNode( string name, string className, string cName, - int? sizeOf, - ImmutableArray attributes) + int? sizeOf) { Name = name; ClassName = className; FullName = string.IsNullOrEmpty(className) ? name : className + "." + name; CName = cName; SizeOf = sizeOf; - Attributes = attributes; } public virtual bool Equals(CSharpNode? other) diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpOpaqueType.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpOpaqueType.cs similarity index 82% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpOpaqueType.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpOpaqueType.cs index 86a386b8..726a572f 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpOpaqueType.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpOpaqueType.cs @@ -4,16 +4,15 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpOpaqueType : CSharpNode { public CSharpOpaqueType( string name, string className, - string cName, - ImmutableArray attributes) - : base(name, className, cName, null, attributes) + string cName) + : base(name, className, cName, null) { } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpParameter.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpParameter.cs similarity index 67% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpParameter.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpParameter.cs index 2e49566e..e17936f9 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpParameter.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpParameter.cs @@ -4,22 +4,21 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpParameter : CSharpNode { - public readonly CSharpTypeInfo TypeInfo; + public readonly CSharpType Type; public CSharpParameter( string name, string className, string cName, int? sizeOf, - CSharpTypeInfo typeInfo, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + CSharpType type) + : base(name, className, cName, sizeOf) { - TypeInfo = typeInfo; + Type = type; } public override bool Equals(CSharpNode? other) @@ -29,13 +28,13 @@ public override bool Equals(CSharpNode? other) return false; } - return TypeInfo == other2.TypeInfo; + return Type == other2.Type; } public override int GetHashCode() { var baseHashCode = base.GetHashCode(); - var hashCode = HashCode.Combine(baseHashCode, TypeInfo); + var hashCode = HashCode.Combine(baseHashCode, Type); return hashCode; } } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpStruct.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpStruct.cs similarity index 87% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpStruct.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpStruct.cs index 4924ec69..92048691 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpStruct.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpStruct.cs @@ -6,7 +6,7 @@ using System.Linq; using C2CS.Foundation; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpStruct : CSharpNode { @@ -21,9 +21,8 @@ public CSharpStruct( int sizeOf, int alignOf, ImmutableArray fields, - ImmutableArray nestedStructs, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + ImmutableArray nestedStructs) + : base(name, className, cName, sizeOf) { AlignOf = alignOf; Fields = fields; diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpStructField.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpStructField.cs similarity index 77% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpStructField.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpStructField.cs index a95e854d..fefa7897 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpStructField.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpStructField.cs @@ -4,27 +4,26 @@ using System; using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed class CSharpStructField : CSharpNode { public readonly string BackingFieldName; public readonly bool IsWrapped; public readonly int OffsetOf; - public readonly CSharpTypeInfo TypeInfo; + public readonly CSharpType Type; public CSharpStructField( string name, string className, string cName, int? sizeOf, - CSharpTypeInfo typeInfo, + CSharpType type, int offsetOf, - bool isWrapped, - ImmutableArray attributes) - : base(name, className, cName, sizeOf, attributes) + bool isWrapped) + : base(name, className, cName, sizeOf) { - TypeInfo = typeInfo; + Type = type; OffsetOf = offsetOf; IsWrapped = isWrapped; BackingFieldName = name.StartsWith('@') ? $"_{name[1..]}" : $"_{name}"; @@ -40,13 +39,13 @@ public override bool Equals(CSharpNode? other) return BackingFieldName == other2.BackingFieldName && IsWrapped == other2.IsWrapped && OffsetOf == other2.OffsetOf && - TypeInfo == other2.TypeInfo; + Type == other2.Type; } public override int GetHashCode() { var baseHashCOde = base.GetHashCode(); - var hashCode = HashCode.Combine(baseHashCOde, BackingFieldName, IsWrapped, OffsetOf, TypeInfo); + var hashCode = HashCode.Combine(baseHashCOde, BackingFieldName, IsWrapped, OffsetOf, Type); return hashCode; } } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpTypeInfo.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpType.cs similarity index 77% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpTypeInfo.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpType.cs index db30e3d7..34f807a4 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Data/CSharpTypeInfo.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpType.cs @@ -2,11 +2,10 @@ // Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. using System; -using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Data; +namespace C2CS.Commands.WriteCodeCSharp.Data; -public sealed class CSharpTypeInfo : IEquatable +public sealed class CSharpType : IEquatable { public string Name { get; init; } = string.Empty; @@ -24,9 +23,7 @@ public sealed class CSharpTypeInfo : IEquatable public bool IsArray => ArraySizeOf > 0; - public ImmutableArray Attributes { get; init; } - - public bool Equals(CSharpTypeInfo? other) + public bool Equals(CSharpType? other) { if (ReferenceEquals(null, other)) { @@ -52,10 +49,10 @@ public override string ToString() public override bool Equals(object? obj) { - return Equals(obj as CSharpTypeInfo); + return Equals(obj as CSharpType); } - public static bool operator ==(CSharpTypeInfo? a, CSharpTypeInfo? b) + public static bool operator ==(CSharpType? a, CSharpType? b) { if (a is null) { @@ -70,7 +67,7 @@ public override bool Equals(object? obj) return a.Equals(b); } - public static bool operator !=(CSharpTypeInfo? a, CSharpTypeInfo? b) => !(a == b); + public static bool operator !=(CSharpType? a, CSharpType? b) => !(a == b); public override int GetHashCode() { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs similarity index 98% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs index f7afb332..2e5d3b32 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGenerator.cs @@ -8,8 +8,8 @@ using System.IO; using System.Reflection; using bottlenoselabs.Common.Diagnostics; -using C2CS.Features.WriteCodeCSharp.Data; -using C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +using C2CS.Commands.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; using C2CS.Foundation; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -17,7 +17,7 @@ using Microsoft.Extensions.DependencyInjection; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public sealed class CSharpCodeGenerator { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs similarity index 92% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs index 865d9a25..85eb0e71 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorContext.cs @@ -7,11 +7,11 @@ using System.Linq; using System.Text; using bottlenoselabs.Common.Tools; -using C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public sealed class CSharpCodeGeneratorContext { @@ -72,15 +72,8 @@ public string GenerateCodeParameters(ImmutableArray parameters, for (var i = 0; i < parameters.Length; i++) { var parameter = parameters[i]; - var attributes = GenerateCodeAttributes(parameter.Attributes); - if (!string.IsNullOrWhiteSpace(attributes)) - { - _stringBuilder.Append(attributes); - _stringBuilder.Append(' '); - } - - _stringBuilder.Append(parameter.TypeInfo.FullName); + _stringBuilder.Append(parameter.Type.FullName); if (includeNames) { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs similarity index 93% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs index ec183ea3..f6ba139a 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpCodeGeneratorOptions.cs @@ -1,7 +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. -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public sealed class CSharpCodeGeneratorOptions { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs similarity index 97% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs index 69037899..8baf11c5 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpLibraryCompiler.cs @@ -7,9 +7,9 @@ using System.Reflection; using bottlenoselabs.Common; using bottlenoselabs.Common.Diagnostics; -using C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Diagnostics; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Diagnostics; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public class CSharpLibraryCompiler { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs similarity index 86% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs index 362f15be..0d5db4ab 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpProject.cs @@ -3,7 +3,7 @@ using System.Collections.Immutable; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public class CSharpProject { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs similarity index 85% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs index c2d387bc..79d4c3b3 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/CSharpProjectDocument.cs @@ -1,7 +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. -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public class CSharpProjectDocument { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs similarity index 90% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs index b25bec92..0ff9e372 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileDiagnostic.cs @@ -3,7 +3,7 @@ using bottlenoselabs.Common.Diagnostics; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Diagnostics; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Diagnostics; public sealed class CSharpCompileDiagnostic : Diagnostic { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs similarity index 90% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs index 4d9dd7a3..0cdbe767 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Diagnostics/CSharpCompileSkipDiagnostic.cs @@ -3,7 +3,7 @@ using bottlenoselabs.Common.Diagnostics; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Diagnostics; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Diagnostics; public class CSharpCompileSkipDiagnostic : Diagnostic { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs similarity index 97% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs index 87aeedb5..1242959a 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/ExtensionsRoslyn.cs @@ -8,7 +8,7 @@ using Microsoft.CodeAnalysis.Formatting; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; [PublicAPI] public static partial class ExtensionsRoslyn diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs similarity index 93% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs index e8e7883d..c1eb0464 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/GenerateCodeHandler.cs @@ -2,11 +2,11 @@ // Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. using System; -using C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; public abstract class GenerateCodeHandler { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs similarity index 63% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs index aafcc9df..2de33bb7 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/AliasTypeCodeGenerator.cs @@ -1,12 +1,12 @@ // 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 C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class AliasTypeCodeGenerator : GenerateCodeHandler { @@ -18,18 +18,15 @@ public AliasTypeCodeGenerator( protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, CSharpAliasType node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); - var code = $@" -{attributesString} -[StructLayout(LayoutKind.Explicit, Size = {node.UnderlyingTypeInfo.SizeOf}, Pack = {node.UnderlyingTypeInfo.AlignOf})] +[StructLayout(LayoutKind.Explicit, Size = {node.UnderlyingType.SizeOf}, Pack = {node.UnderlyingType.AlignOf})] public struct {node.Name} {{ [FieldOffset(0)] - public {node.UnderlyingTypeInfo.Name} Data; + public {node.UnderlyingType.Name} Data; - public static implicit operator {node.UnderlyingTypeInfo.Name}({node.Name} data) => data.Data; - public static implicit operator {node.Name}({node.UnderlyingTypeInfo.Name} data) => new() {{Data = data}}; + public static implicit operator {node.UnderlyingType.Name}({node.Name} data) => data.Data; + public static implicit operator {node.Name}({node.UnderlyingType.Name} data) => new() {{Data = data}}; }} "; diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs similarity index 80% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs index 03682000..0b927e6c 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/ConstantCodeGenerator.cs @@ -1,12 +1,12 @@ // 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 C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class ConstantCodeGenerator : GenerateCodeHandler { @@ -18,10 +18,7 @@ public ConstantCodeGenerator( protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, CSharpConstant node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); - var code = $@" -{attributesString} public const {node.Type} {node.Name} = {node.Value}; "; diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs similarity index 93% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs index 5b7b6969..cfe6cb40 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/EnumCodeGenerator.cs @@ -4,13 +4,13 @@ using System; using System.Collections.Immutable; using System.Linq; -using C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class EnumCodeGenerator : GenerateCodeHandler { @@ -22,8 +22,6 @@ public EnumCodeGenerator( protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, CSharpEnum node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); - var enumName = node.Name; var enumSizeOf = node.SizeOf!.Value; var enumIntegerTypeName = enumSizeOf switch @@ -41,7 +39,6 @@ protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, C var members = string.Join(",\n", valuesString); var code = $@" -{attributesString} public enum {enumName} : {enumIntegerTypeName} {{ {members} diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs similarity index 85% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs index fd1e480c..72260437 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionCodeGenerator.cs @@ -4,12 +4,12 @@ using System; using System.Collections.Generic; using System.Linq; -using C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class FunctionCodeGenerator : GenerateCodeHandler { @@ -32,21 +32,17 @@ protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, C var callingConvention = FunctionCallingConventionDllImport(node.CallingConvention); var dllImportParametersString = string.Join(',', "LibraryName", $"EntryPoint = \"{node.CName}\"", callingConvention); - var attributesString = context.GenerateCodeAttributes(node.Attributes); - code = $@" -{attributesString} [DllImport({dllImportParametersString})] -public static extern {node.ReturnTypeInfo.FullName} {node.Name}({parametersString}); +public static extern {node.ReturnType.FullName} {node.Name}({parametersString}); "; } else { var callingConvention = FunctionCallingConventionLibraryImport(node.CallingConvention); - var attributesString = context.GenerateCodeAttributes(node.Attributes); var libraryImportParameters = new List { "LibraryName", $"EntryPoint = \"{node.CName}\"" }; - if (node.ReturnTypeInfo.Name == "string" || + if (node.ReturnType.Name == "string" || parametersString.Contains("string", StringComparison.InvariantCulture)) { libraryImportParameters.Add("StringMarshalling = StringMarshalling.Utf8"); @@ -55,10 +51,9 @@ protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, C var libraryImportParametersString = string.Join(",", libraryImportParameters); code = $@" -{attributesString} [LibraryImport({libraryImportParametersString})] [UnmanagedCallConv(CallConvs = new[] {{ typeof({callingConvention}) }})] -public static partial {node.ReturnTypeInfo.FullName} {node.Name}({parametersString}); +public static partial {node.ReturnType.FullName} {node.Name}({parametersString}); "; } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs similarity index 79% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs index c7f4b531..eff0c3c2 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/FunctionPointerCodeGenerator.cs @@ -1,12 +1,12 @@ // 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 C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public sealed class FunctionPointerCodeGenerator : GenerateCodeHandler { @@ -34,12 +34,10 @@ protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, C private string GenerateCodeFunctionPointer(CSharpCodeGeneratorContext context, CSharpFunctionPointer node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); var parameterTypesString = context.GenerateCodeParameters(node.Parameters, false); - var parameterTypesAndReturnTypeString = string.IsNullOrEmpty(parameterTypesString) ? node.ReturnTypeInfo.FullName : $"{parameterTypesString}, {node.ReturnTypeInfo.FullName}"; + var parameterTypesAndReturnTypeString = string.IsNullOrEmpty(parameterTypesString) ? node.ReturnType.FullName : $"{parameterTypesString}, {node.ReturnType.FullName}"; var code = $@" -{attributesString} [StructLayout(LayoutKind.Sequential)] public struct {node.Name} {{ @@ -51,16 +49,14 @@ public struct {node.Name} private string GenerateCodeDelegate(CSharpCodeGeneratorContext context, CSharpFunctionPointer node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); var parameterTypesString = context.GenerateCodeParameters(node.Parameters); var code = $@" -{attributesString} [StructLayout(LayoutKind.Sequential)] public struct {node.Name} {{ [UnmanagedFunctionPointer(CallingConvention.Cdecl)] - public unsafe delegate {node.ReturnTypeInfo.FullName} @delegate({parameterTypesString}); + public unsafe delegate {node.ReturnType.FullName} @delegate({parameterTypesString}); public IntPtr Pointer; diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs similarity index 83% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs index 9540f0f2..cafebcac 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/MacroCodeGenerator.cs @@ -1,12 +1,12 @@ // 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 C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class MacroCodeGenerator : GenerateCodeHandler { @@ -18,8 +18,6 @@ public MacroCodeGenerator( protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, CSharpMacroObject node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); - var name = node.Name; if (context.NameAlreadyExists(node.FullName)) { @@ -30,14 +28,12 @@ protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, C if (node.IsConstant) { code = $@" -{attributesString} public const {node.Type} {name} = {node.Value}; "; } else { code = $@" -{attributesString} public static {node.Type} {name} = ({node.Type}){node.Value}; "; } diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs similarity index 79% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs index 7e2da06a..c88ff71a 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/OpaqueTypeCodeGenerator.cs @@ -1,12 +1,12 @@ // 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 C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class OpaqueTypeCodeGenerator : GenerateCodeHandler { @@ -18,10 +18,7 @@ public OpaqueTypeCodeGenerator( protected override SyntaxNode GenerateCode(CSharpCodeGeneratorContext context, CSharpOpaqueType node) { - var attributesString = context.GenerateCodeAttributes(node.Attributes); - var code = $@" -{attributesString} [StructLayout(LayoutKind.Sequential)] public struct {node.Name} {{ diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs similarity index 81% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs index 974aab13..cbe09ea8 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/CodeGenerator/Handlers/StructCodeGenerator.cs @@ -4,12 +4,12 @@ using System; using System.Collections.Immutable; using System.Linq; -using C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator.Handlers; +namespace C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator.Handlers; public class StructCodeGenerator : GenerateCodeHandler { @@ -29,10 +29,8 @@ private StructDeclarationSyntax Struct(CSharpCodeGeneratorContext context, CShar var memberSyntaxes = StructMembers(context, @struct.Name, @struct.Fields, @struct.NestedStructs); var memberStrings = memberSyntaxes.Select(x => x.ToFullString()); var members = string.Join("\n\n", memberStrings); - var attributesString = context.GenerateCodeAttributes(@struct.Attributes); var code = $@" -{attributesString} [StructLayout(LayoutKind.Explicit, Size = {@struct.SizeOf}, Pack = {@struct.AlignOf})] public struct {@struct.Name} {{ @@ -77,7 +75,7 @@ private void StructFields( { foreach (var field in fields) { - if (field.TypeInfo.IsArray) + if (field.Type.IsArray) { var fieldMember = EmitStructFieldFixedBuffer(context, field); builder.Add(fieldMember); @@ -98,15 +96,12 @@ private FieldDeclarationSyntax StructField( CSharpCodeGeneratorContext context, CSharpStructField field) { - var attributesString = context.GenerateCodeAttributes(field.Attributes); - string code; - if (field.TypeInfo.Name == "CString") + if (field.Type.Name == "CString") { code = $@" -{attributesString} -[FieldOffset({field.OffsetOf})] // size = {field.TypeInfo.SizeOf} -public {field.TypeInfo.FullName} _{field.Name}; +[FieldOffset({field.OffsetOf})] // size = {field.Type.SizeOf} +public {field.Type.FullName} _{field.Name}; public string {field.Name} {{ @@ -124,9 +119,8 @@ public string {field.Name} else { code = $@" -{attributesString} -[FieldOffset({field.OffsetOf})] // size = {field.TypeInfo.SizeOf} -public {field.TypeInfo.FullName} {field.Name}; +[FieldOffset({field.OffsetOf})] // size = {field.Type.SizeOf} +public {field.Type.FullName} {field.Name}; ".Trim(); } @@ -138,12 +132,9 @@ private FieldDeclarationSyntax EmitStructFieldFixedBuffer( CSharpCodeGeneratorContext context, CSharpStructField field) { - var attributesString = context.GenerateCodeAttributes(field.Attributes); - var code = $@" -{attributesString} -[FieldOffset({field.OffsetOf})] // size = {field.TypeInfo.SizeOf} -public fixed byte {field.BackingFieldName}[{field.TypeInfo.SizeOf}]; // {field.TypeInfo.OriginalName} +[FieldOffset({field.OffsetOf})] // size = {field.Type.SizeOf} +public fixed byte {field.BackingFieldName}[{field.Type.SizeOf}]; // {field.Type.OriginalName} ".Trim(); return context.ParseMemberCode(code); @@ -156,7 +147,7 @@ private PropertyDeclarationSyntax StructFieldFixedBufferProperty( { string code; - if (field.TypeInfo.Name == "CString") + if (field.Type.Name == "CString") { code = $@" public string {field.Name} @@ -173,7 +164,7 @@ public string {field.Name} }} ".Trim(); } - else if (field.TypeInfo.Name == "CStringWide") + else if (field.Type.Name == "CStringWide") { code = $@" public string {field.Name} @@ -192,7 +183,7 @@ public string {field.Name} } else { - var fieldTypeName = field.TypeInfo.Name; + var fieldTypeName = field.Type.Name; var elementType = fieldTypeName[..^1]; if (elementType.EndsWith('*')) { @@ -207,7 +198,7 @@ public readonly Span<{elementType}> {field.Name} fixed ({structName}*@this = &this) {{ var pointer = &@this->{field.BackingFieldName}[0]; - var span = new Span<{elementType}>(pointer, {field.TypeInfo.ArraySizeOf}); + var span = new Span<{elementType}>(pointer, {field.Type.ArraySizeOf}); return span; }} }} diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs similarity index 83% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs index f9b45a52..bf86cf7c 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs @@ -7,13 +7,13 @@ using System.Globalization; using System.Linq; using System.Text.RegularExpressions; -using bottlenoselabs.C2CS.Runtime; using bottlenoselabs.Common.Diagnostics; -using C2CS.Features.WriteCodeCSharp.Data; -using C2CS.Foundation; -using CAstFfi.Data; +using C2CS.Commands.WriteCodeCSharp.Data; +using C2CS.Internal; +using c2ffi.Data; +using c2ffi.Data.Nodes; -namespace C2CS.Features.WriteCodeCSharp.Domain.Mapper; +namespace C2CS.Commands.WriteCodeCSharp.Domain.Mapper; public sealed class CSharpCodeMapper { @@ -67,21 +67,19 @@ public CSharpCodeMapper(CSharpCodeMapperOptions options) } public CSharpAbstractSyntaxTree Map( - DiagnosticsSink diagnostics, CAbstractSyntaxTreeCrossPlatform astC) + CFfiCrossPlatform ffi, + DiagnosticsSink diagnostics) { - var enumNames = astC.Enums.Values.Select(x => x.Name.TrimEnd('_')).ToImmutableHashSet(); - var context = new CSharpCodeMapperContext( - astC.Records, astC.FunctionPointers, enumNames); - - var functionsC = astC.Functions.Values.ToImmutableArray(); - var functionNamesC = astC.Functions.Keys.ToImmutableHashSet(); - var functionPointersC = astC.FunctionPointers.Values.ToImmutableArray(); - var recordsC = astC.Records.Values.ToImmutableArray(); - var typeAliasesC = astC.TypeAliases.Values.ToImmutableArray(); - var opaqueTypesC = astC.OpaqueTypes.Values.ToImmutableArray(); - var enumsC = astC.Enums.Values.ToImmutableArray(); - var macroObjectsC = astC.MacroObjects.Values.ToImmutableArray(); - var enumConstantsC = astC.EnumConstants.Values.ToImmutableArray(); + var context = new CSharpCodeMapperContext(ffi); + + var functionsC = ffi.Functions.Values.ToImmutableArray(); + var functionNamesC = ffi.Functions.Keys.ToImmutableHashSet(); + var functionPointersC = ffi.FunctionPointers.Values.ToImmutableArray(); + var recordsC = ffi.Records.Values.ToImmutableArray(); + var typeAliasesC = ffi.TypeAliases.Values.ToImmutableArray(); + var opaqueTypesC = ffi.OpaqueTypes.Values.ToImmutableArray(); + var enumsC = ffi.Enums.Values.ToImmutableArray(); + var macroObjectsC = ffi.MacroObjects.Values.ToImmutableArray(); var functions = Functions(context, functionsC); var structs = Structs(context, recordsC, functionNamesC); @@ -90,9 +88,8 @@ public CSharpAbstractSyntaxTree Map( var functionPointers = FunctionPointers(context, functionPointersC); var opaqueStructs = OpaqueStructs(context, opaqueTypesC); var macroObjects = MacroObjects(context, macroObjectsC); - var enumConstants = EnumConstants(context, enumConstantsC); - var astCSharp = new CSharpAbstractSyntaxTree + var ast = new CSharpAbstractSyntaxTree { Functions = functions, FunctionPointers = functionPointers, @@ -100,11 +97,10 @@ public CSharpAbstractSyntaxTree Map( AliasStructs = aliasStructs, OpaqueStructs = opaqueStructs, Enums = enums, - MacroObjects = macroObjects, - Constants = enumConstants + MacroObjects = macroObjects }; - return astCSharp; + return ast; } private ImmutableArray Functions( @@ -132,10 +128,9 @@ private ImmutableArray Functions( private CSharpFunction Function(CSharpCodeMapperContext context, CFunction cFunction) { var nameCSharp = cFunction.Name; - var returnTypeCSharp = TypeCSharp(context, cFunction.ReturnTypeInfo); + var returnTypeCSharp = TypeCSharp(context, cFunction.ReturnType); var callingConvention = CSharpFunctionCallingConvention(cFunction.CallingConvention); var parameters = CSharpFunctionParameters(context, nameCSharp, cFunction.Parameters); - var attributes = Attributes(cFunction); var className = ClassName(nameCSharp, out var nameCSharpMapped); var nameCSharpFinal = IdiomaticName(nameCSharpMapped, false); @@ -146,8 +141,7 @@ private CSharpFunction Function(CSharpCodeMapperContext context, CFunction cFunc null, callingConvention, returnTypeCSharp, - parameters, - attributes); + parameters); return result; } @@ -182,6 +176,13 @@ private ImmutableArray CSharpFunctionParameters( parameterNames.Add(parameterName); var value = FunctionParameter(context, functionName, functionExternParameterC, parameterName); + if (value.TypeName.Contains("uint16_t", StringComparison.InvariantCultureIgnoreCase)) + { + var value2 = + FunctionParameter(context, functionName, functionExternParameterC, parameterName); + Console.WriteLine(); + } + builder.Add(value); } @@ -234,7 +235,7 @@ private CSharpFunctionParameter FunctionParameter( string parameterName) { var name = SanitizeIdentifier(parameterName); - var typeC = cFunctionParameter.TypeInfo; + var typeC = cFunctionParameter.Type; var typeCSharp = TypeCSharp(context, typeC); var typeCSharpName = typeCSharp.FullName; @@ -247,7 +248,6 @@ private CSharpFunctionParameter FunctionParameter( StringComparison.InvariantCulture); } - var attributes = ImmutableArray.Empty; var nameFinal = IdiomaticName(name, false, isParameter: true); var functionParameterCSharp = new CSharpFunctionParameter( @@ -255,8 +255,7 @@ private CSharpFunctionParameter FunctionParameter( typeCSharp.ClassName, cFunctionParameter.Name, typeC.SizeOf, - typeCSharpName, - attributes); + typeCSharpName); return functionParameterCSharp; } @@ -289,7 +288,7 @@ private ImmutableArray FunctionPointers( Dictionary names, CFunctionPointer cFunctionPointer) { - var functionPointerType = cFunctionPointer.TypeInfo; + var functionPointerType = cFunctionPointer.Type; var typeNameCSharp = TypeNameCSharpFunctionPointer(context, functionPointerType.Name, cFunctionPointer); if (names.ContainsKey(typeNameCSharp)) { @@ -297,11 +296,10 @@ private ImmutableArray FunctionPointers( return null; } - var returnTypeC = cFunctionPointer.ReturnTypeInfo; + var returnTypeC = cFunctionPointer.ReturnType; var returnTypeCSharp = TypeCSharp(context, returnTypeC); var parameters = FunctionPointerParameters(context, cFunctionPointer.Parameters); - var attributes = Attributes(cFunctionPointer); var className = ClassName( typeNameCSharp, out var typeNameCSharpMapped); @@ -313,8 +311,7 @@ private ImmutableArray FunctionPointers( cFunctionPointer.Name, functionPointerType.SizeOf, returnTypeCSharp, - parameters, - attributes); + parameters); names.Add(typeNameCSharp, cFunctionPointer); @@ -349,17 +346,15 @@ private CSharpParameter FunctionPointerParameter( string parameterName) { var name = SanitizeIdentifier(parameterName); - var typeC = cFunctionPointerParameter.TypeInfo; + var typeC = cFunctionPointerParameter.Type; var typeCSharp = TypeCSharp(context, typeC); - var attributes = ImmutableArray.Empty; var result = new CSharpParameter( name, typeCSharp.ClassName, cFunctionPointerParameter.Name, typeC.SizeOf, - typeCSharp, - attributes); + typeCSharp); return result; } @@ -406,7 +401,6 @@ private CSharpStruct Struct( var (fields, nestedRecords) = StructFields(context, cRecord.Name, cRecord.Fields); var nestedStructs = Structs(context, nestedRecords, functionNames); - var attributes = Attributes(cRecord); var className = ClassName(name, out var nameCSharpMapped); var cSharpNameFinal = IdiomaticName(nameCSharpMapped, false); @@ -417,8 +411,7 @@ private CSharpStruct Struct( cRecord.SizeOf, cRecord.AlignOf, fields, - nestedStructs, - attributes); + nestedStructs); } private (ImmutableArray Fields, ImmutableArray NestedRecords) StructFields( @@ -445,7 +438,7 @@ private void StructField( CRecordField field, ImmutableArray.Builder resultFields) { - if (string.IsNullOrEmpty(field.Name) && context.Records.TryGetValue(field.TypeInfo.Name, out var @struct)) + if (string.IsNullOrEmpty(field.Name) && context.Records.TryGetValue(field.Type.Name, out var @struct)) { var (value, _) = StructFields(context, structName, @struct.Fields); resultFields.AddRange(value); @@ -463,11 +456,10 @@ private CSharpStructField StructField( { var nameCSharp = SanitizeIdentifier(cField.Name); var nameCSharpFinal = IdiomaticName(nameCSharp, false); - var typeC = cField.TypeInfo; + var typeC = cField.Type; var typeInfoCSharp = TypeCSharp(context, typeC); var offsetOf = cField.OffsetOf; var isWrapped = typeInfoCSharp.IsArray && !IsValidFixedBufferType(typeInfoCSharp.Name); - var attributes = ImmutableArray.Empty; var result = new CSharpStructField( nameCSharpFinal, @@ -476,8 +468,7 @@ private CSharpStructField StructField( typeC.SizeOf, typeInfoCSharp, offsetOf, - isWrapped, - attributes); + isWrapped); return result; } @@ -507,8 +498,7 @@ private ImmutableArray OpaqueStructs( private CSharpOpaqueType OpaqueDataStruct(COpaqueType cOpaqueType) { - var nameCSharp = TypeNameCSharpRaw(cOpaqueType.Name, cOpaqueType.SizeOf); - var attributes = Attributes(cOpaqueType); + var nameCSharp = TypeNameCSharpRaw(cOpaqueType.Name, 0); var className = ClassName( nameCSharp, out var nameCSharpMapped); var nameCSharpFinal = IdiomaticName(nameCSharpMapped, false); @@ -516,8 +506,7 @@ private CSharpOpaqueType OpaqueDataStruct(COpaqueType cOpaqueType) var opaqueTypeCSharp = new CSharpOpaqueType( nameCSharpFinal, className, - cOpaqueType.Name, - attributes); + cOpaqueType.Name); return opaqueTypeCSharp; } @@ -530,8 +519,7 @@ private ImmutableArray AliasStructs( // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator foreach (var typedef in typedefs) { - if (_builtinAliases.Contains(typedef.Name) || - _ignoredNames.Contains(typedef.Name)) + if (IsIgnored(typedef.Name)) { continue; } @@ -559,9 +547,8 @@ private CSharpAliasType AliasStruct( CSharpCodeMapperContext context, CTypeAlias cTypeAlias) { - var underlyingTypeC = cTypeAlias.UnderlyingTypeInfo; + var underlyingTypeC = cTypeAlias.UnderlyingType; var underlyingTypeCSharp = TypeCSharp(context, underlyingTypeC); - var attributes = Attributes(cTypeAlias); var className = ClassName( cTypeAlias.Name, out var cSharpNameMapped); @@ -572,8 +559,7 @@ private CSharpAliasType AliasStruct( className, cTypeAlias.Name, underlyingTypeC.SizeOf, - underlyingTypeCSharp, - attributes); + underlyingTypeCSharp); return result; } @@ -588,7 +574,7 @@ private ImmutableArray Enums( foreach (var enumC in enums) { var value = Enum(context, enumC); - if (_ignoredNames.Contains(value.Name)) + if (IsIgnored(value.Name)) { continue; } @@ -604,10 +590,13 @@ private CSharpEnum Enum( CSharpCodeMapperContext context, CEnum cEnum) { - var integerTypeC = cEnum.IntegerTypeInfo; - var integerType = TypeCSharp(context, integerTypeC); - var attributes = Attributes(cEnum); - var className = ClassName(cEnum.Name, out var cSharpNameMapped); + var name = cEnum.Name; + if (name.StartsWith("enum ", StringComparison.InvariantCulture)) + { + name = name.ReplaceFirst("enum ", string.Empty, StringComparison.InvariantCulture); + } + + var className = ClassName(name, out var cSharpNameMapped); var cSharpNameFinal = IdiomaticName(cSharpNameMapped, false); var values = EnumValues(context, cEnum.Values, cSharpNameFinal); @@ -615,9 +604,8 @@ private CSharpEnum Enum( cSharpNameFinal, className, cEnum.Name, - integerType, - values, - attributes); + cEnum.SizeOf, + values); return result; } @@ -644,7 +632,6 @@ private CSharpEnumValue EnumValue( { var cSharpName = cEnumValue.Name; var value = cEnumValue.Value; - var attributes = ImmutableArray.Empty; var className = ClassName( cSharpName, out var cSharpNameName); var cSharpNameFinal = IdiomaticName(cSharpNameName, false, cSharpEnumName); @@ -654,8 +641,7 @@ private CSharpEnumValue EnumValue( className, cEnumValue.Name, null, - value, - attributes); + value); return result; } @@ -685,11 +671,11 @@ private CSharpMacroObject MacroObject( CSharpCodeMapperContext context, CMacroObject cMacroObject) { - var typeKind = cMacroObject.TypeInfo.Kind; - var isConstant = typeKind is CKind.Primitive; - var typeSize = cMacroObject.TypeInfo.SizeOf; + var typeKind = cMacroObject.Type.NodeKind; + var isConstant = typeKind is CNodeKind.Primitive; + var typeSize = cMacroObject.Type.SizeOf; - var typeName = TypeNameCSharp(context, cMacroObject.TypeInfo); + var typeName = TypeNameCSharp(context, cMacroObject.Type); if (typeName == "CString") { typeName = "string"; @@ -706,7 +692,6 @@ private CSharpMacroObject MacroObject( value += "f"; } - var attributes = Attributes(cMacroObject); var className = ClassName(cMacroObject.Name, out var cSharpNameMapped); var cSharpNameFinal = IdiomaticName(cSharpNameMapped, false, isMacroObject: true); @@ -717,48 +702,7 @@ private CSharpMacroObject MacroObject( typeSize, typeName, value, - isConstant, - attributes); - return result; - } - - private ImmutableArray EnumConstants( - CSharpCodeMapperContext context, - ImmutableArray enumConstants) - { - var builder = ImmutableArray.CreateBuilder(enumConstants.Length); - - // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator - foreach (var enumConstant in enumConstants) - { - if (_ignoredNames.Contains(enumConstant.Name)) - { - continue; - } - - var value = EnumConstant(context, enumConstant); - builder.Add(value); - } - - builder.Sort(); - return builder.ToImmutable(); - } - - private CSharpConstant EnumConstant(CSharpCodeMapperContext context, CEnumConstant cEnumConstant) - { - var typeNameCSharp = TypeNameCSharp(context, cEnumConstant.TypeInfo); - var attributes = Attributes(cEnumConstant); - var className = ClassName(cEnumConstant.Name, out var cSharpNameMapped); - var cSharpNameFinal = IdiomaticName(cSharpNameMapped, false); - - var result = new CSharpConstant( - cSharpNameFinal, - className, - cEnumConstant.Name, - cEnumConstant.TypeInfo.SizeOf, - typeNameCSharp, - cEnumConstant.Value, - attributes); + isConstant); return result; } @@ -797,31 +741,23 @@ private string ClassName( return nameSpace; } - private CSharpTypeInfo TypeCSharp( + private CSharpType TypeCSharp( CSharpCodeMapperContext context, - CTypeInfo typeInfo) + CType type) { - var nameCSharp = TypeNameCSharp(context, typeInfo); - - var attributesBuilder = ImmutableArray.CreateBuilder(); - if (typeInfo.IsConst) - { - var constAttribute = new CConstAttribute(); - attributesBuilder.Add(constAttribute); - } + var nameCSharp = TypeNameCSharp(context, type); var className = ClassName(nameCSharp, out var nameCSharpMapped); var nameCSharpFinal = IdiomaticName(nameCSharpMapped, true); - var result = new CSharpTypeInfo + var result = new CSharpType { Name = nameCSharpFinal, ClassName = className, - OriginalName = typeInfo.Name, - SizeOf = typeInfo.SizeOf, - AlignOf = typeInfo.AlignOf, - ArraySizeOf = typeInfo.ArraySizeOf, - Attributes = attributesBuilder.ToImmutable() + OriginalName = type.Name, + SizeOf = type.SizeOf ?? 0, + AlignOf = type.AlignOf, + ArraySizeOf = type.ArraySizeOf }; return result; @@ -937,34 +873,29 @@ private string IdiomaticName(string name, bool isType, string enumName = "", boo private string TypeNameCSharp( CSharpCodeMapperContext context, - CTypeInfo typeInfo) + CType type) { - var forceUnsigned = typeInfo.Kind == CKind.EnumValue; + var forceUnsigned = type.NodeKind == CNodeKind.EnumValue; - if (typeInfo.Kind == CKind.FunctionPointer) + if (type.NodeKind == CNodeKind.FunctionPointer) { - var functionPointer = context.FunctionPointers[typeInfo.Name]; - return TypeNameCSharpFunctionPointer(context, typeInfo.Name, functionPointer); + var functionPointer = context.FunctionPointers[type.Name]; + return TypeNameCSharpFunctionPointer(context, type.Name, functionPointer); } - var typeName = typeInfo.Name; + var typeName = type.Name; if (typeName.Contains("const ", StringComparison.InvariantCulture)) { typeName = typeName.Replace("const ", string.Empty, StringComparison.InvariantCulture); } - if (typeName.Contains("*const", StringComparison.InvariantCulture)) + if (type.NodeKind is CNodeKind.Pointer or CNodeKind.Array) { - typeName = typeName.Replace("*const", "*", StringComparison.InvariantCulture); - } - - if (typeInfo.Kind is CKind.Pointer or CKind.Array) - { - typeName = TypeNameCSharpPointer(typeName, typeInfo.InnerTypeInfo); + typeName = TypeNameCSharpPointer(typeName, type.InnerType); } else { - typeName = TypeNameCSharpRaw(typeName, typeInfo.SizeOf, forceUnsigned); + typeName = TypeNameCSharpRaw(typeName, type.SizeOf ?? 0, forceUnsigned); } // TODO: https://github.com/lithiumtoast/c2cs/issues/15 @@ -998,7 +929,7 @@ private string TypeNameCSharpFunctionPointer( private string CreateFunctionPointerName(CSharpCodeMapperContext context, CFunctionPointer functionPointer) { - var returnTypeC = functionPointer.ReturnTypeInfo; + var returnTypeC = functionPointer.ReturnType; var returnTypeCSharp = TypeCSharp(context, returnTypeC); var returnTypeNameCSharp = returnTypeCSharp.FullName.Replace("*", "Ptr", StringComparison.InvariantCulture); var returnTypeNameCSharpParts = returnTypeNameCSharp.Split(IdentifierSeparatorCharacters, StringSplitOptions.RemoveEmptyEntries); @@ -1010,7 +941,7 @@ private string CreateFunctionPointerName(CSharpCodeMapperContext context, CFunct var parameterStringsCSharp = new List(); foreach (var parameter in functionPointer.Parameters) { - var typeCSharp = TypeCSharp(context, parameter.TypeInfo); + var typeCSharp = TypeCSharp(context, parameter.Type); var typeNameCSharp = typeCSharp.FullName.Replace("*", "Ptr", StringComparison.InvariantCulture); var typeNameCSharpParts = typeNameCSharp.Split(IdentifierSeparatorCharacters, StringSplitOptions.RemoveEmptyEntries); @@ -1027,7 +958,7 @@ private string CreateFunctionPointerName(CSharpCodeMapperContext context, CFunct return functionPointerNameCSharp; } - private string TypeNameCSharpPointer(string typeName, CTypeInfo? innerTypeInfo) + private string TypeNameCSharpPointer(string typeName, CType? innerType) { var pointerTypeName = typeName; @@ -1046,39 +977,40 @@ private string TypeNameCSharpPointer(string typeName, CTypeInfo? innerTypeInfo) pointerTypeName = pointerTypeName[..x] + "*" + pointerTypeName[(y + 1)..]; } - if (pointerTypeName.StartsWith("char*", StringComparison.InvariantCulture)) + if (pointerTypeName.StartsWith("char *", StringComparison.InvariantCulture)) { - return pointerTypeName.Replace("char*", "CString", StringComparison.InvariantCulture); + return pointerTypeName.ReplaceFirst("char *", "CString", StringComparison.InvariantCulture); } - if (pointerTypeName.StartsWith("wchar_t*", StringComparison.InvariantCulture)) + if (pointerTypeName.StartsWith("wchar_t *", StringComparison.InvariantCulture)) { - return pointerTypeName.Replace("wchar_t*", "CStringWide", StringComparison.InvariantCulture); + return pointerTypeName.ReplaceFirst("wchar_t *", "CStringWide", StringComparison.InvariantCulture); } - if (pointerTypeName.StartsWith("FILE*", StringComparison.InvariantCulture)) + if (pointerTypeName.StartsWith("FILE *", StringComparison.InvariantCulture)) { - return pointerTypeName.Replace("FILE*", "nint", StringComparison.InvariantCulture); + return pointerTypeName.ReplaceFirst("FILE *", "nint", StringComparison.InvariantCulture); } - if (pointerTypeName.StartsWith("DIR*", StringComparison.InvariantCulture)) + if (pointerTypeName.StartsWith("DIR *", StringComparison.InvariantCulture)) { - return pointerTypeName.Replace("DIR*", "nint", StringComparison.InvariantCulture); + return pointerTypeName.ReplaceFirst("DIR *", "nint", StringComparison.InvariantCulture); } - var elementTypeName = pointerTypeName.TrimEnd('*'); - var pointersTypeName = pointerTypeName[elementTypeName.Length..]; + var elementTypeName = pointerTypeName.TrimEnd('*').TrimEnd(); + var pointersTypeName = pointerTypeName[elementTypeName.Length..] + .Replace(" ", string.Empty, StringComparison.InvariantCulture); if (elementTypeName.Length == 0) { return "void" + pointersTypeName; } - if (innerTypeInfo == null) + if (innerType == null) { return "void*"; } - var mappedElementTypeName = TypeNameCSharpRaw(elementTypeName, innerTypeInfo.SizeOf); + var mappedElementTypeName = TypeNameCSharpRaw(elementTypeName, innerType.SizeOf ?? 0); var result = mappedElementTypeName + pointersTypeName; return result; } @@ -1198,6 +1130,12 @@ private string TypeNameMapFloatingPoint(int sizeOf) }; } + private bool IsIgnored(string name) + { + return _builtinAliases.Contains(name) || + _ignoredNames.Contains(name); + } + private static string SanitizeIdentifier(string name) { var result = name; @@ -1308,15 +1246,4 @@ private static bool IsValidFixedBufferType(string typeString) _ => false }; } - - private static ImmutableArray Attributes(CNode cMacroObject) - { - return new Attribute[] - { - new CNodeAttribute - { - Kind = cMacroObject.Kind.ToString() - } - }.ToImmutableArray(); - } } diff --git a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs new file mode 100644 index 00000000..cc02bebb --- /dev/null +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs @@ -0,0 +1,45 @@ +// 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; +using System.Collections.Immutable; +using System.Linq; +using C2CS.Internal; +using c2ffi.Data; +using c2ffi.Data.Nodes; + +namespace C2CS.Commands.WriteCodeCSharp.Domain.Mapper; + +public class CSharpCodeMapperContext +{ + public readonly ImmutableSortedDictionary FunctionPointers; + + public readonly ImmutableSortedDictionary Records; + + public readonly ImmutableHashSet EnumNames; + + public CSharpCodeMapperContext(CFfiCrossPlatform ffi) + { + Records = ffi.Records; + FunctionPointers = ffi.FunctionPointers; + EnumNames = GetEnumNames(ffi); + } + + private static ImmutableHashSet GetEnumNames(CFfiCrossPlatform ffi) + { + var result = ffi.Enums.Values.Select(GetEnumName).ToImmutableHashSet(); + return result; + } + + private static string GetEnumName(CEnum @enum) + { + var name = @enum.Name; + + if (name.StartsWith("enum ", StringComparison.InvariantCulture)) + { + name = name.ReplaceFirst("enum ", string.Empty, StringComparison.InvariantCulture); + } + + return name.TrimEnd('_'); + } +} diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs similarity index 97% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs index c6910613..a9465e08 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperOptions.cs @@ -3,9 +3,9 @@ using System.Collections.Generic; using System.Collections.Immutable; -using C2CS.Features.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Data; -namespace C2CS.Features.WriteCodeCSharp.Domain.Mapper; +namespace C2CS.Commands.WriteCodeCSharp.Domain.Mapper; public sealed class CSharpCodeMapperOptions { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs similarity index 76% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs index 9db18658..4ba501af 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Sanitized/WriteCodeCSharpInput.cs @@ -1,10 +1,10 @@ // 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 C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; -using C2CS.Features.WriteCodeCSharp.Domain.Mapper; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; +using C2CS.Commands.WriteCodeCSharp.Domain.Mapper; -namespace C2CS.Features.WriteCodeCSharp.Input.Sanitized; +namespace C2CS.Commands.WriteCodeCSharp.Input.Sanitized; public sealed class WriteCodeCSharpInput { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs similarity index 99% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs index 5f6a2658..0b08e8fe 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptions.cs @@ -6,7 +6,7 @@ using bottlenoselabs.Common.Tools; using JetBrains.Annotations; -namespace C2CS.Features.WriteCodeCSharp.Input.Unsanitized; +namespace C2CS.Commands.WriteCodeCSharp.Input.Unsanitized; // NOTE: This class is considered un-sanitized input; all strings and other types could be null. [PublicAPI] diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs similarity index 93% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs index 71bf9dea..00163785 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/Unsanitized/WriteCSharpCodeOptionsMappedName.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Features.WriteCodeCSharp.Input.Unsanitized; +namespace C2CS.Commands.WriteCodeCSharp.Input.Unsanitized; /// /// A pair of source and target names for renaming. diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs similarity index 95% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs index ef2757ef..bc1aa927 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Input/WriteCodeCSharpInputSanitizer.cs @@ -7,13 +7,13 @@ using System.IO.Abstractions; using System.Linq; using bottlenoselabs.Common.Tools; -using C2CS.Features.WriteCodeCSharp.Data; -using C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; -using C2CS.Features.WriteCodeCSharp.Domain.Mapper; -using C2CS.Features.WriteCodeCSharp.Input.Sanitized; -using C2CS.Features.WriteCodeCSharp.Input.Unsanitized; +using C2CS.Commands.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; +using C2CS.Commands.WriteCodeCSharp.Domain.Mapper; +using C2CS.Commands.WriteCodeCSharp.Input.Sanitized; +using C2CS.Commands.WriteCodeCSharp.Input.Unsanitized; -namespace C2CS.Features.WriteCodeCSharp.Input; +namespace C2CS.Commands.WriteCodeCSharp.Input; public sealed class WriteCodeCSharpInputSanitizer : ToolInputSanitizer { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs similarity index 79% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs index 6c0e49e7..3bdc4d00 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Output/WriteCodeCSharpOutput.cs @@ -3,10 +3,10 @@ using System.Reflection; using bottlenoselabs.Common.Tools; -using C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; -using C2CS.Features.WriteCodeCSharp.Input.Sanitized; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; +using C2CS.Commands.WriteCodeCSharp.Input.Sanitized; -namespace C2CS.Features.WriteCodeCSharp.Output; +namespace C2CS.Commands.WriteCodeCSharp.Output; public class WriteCodeCSharpOutput : ToolOutput { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Startup.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Startup.cs similarity index 88% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Startup.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Startup.cs index 93263eb4..0b463f1a 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Startup.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Startup.cs @@ -4,11 +4,11 @@ using System.Collections.Immutable; using System.Linq; using System.Reflection; -using C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; -using C2CS.Features.WriteCodeCSharp.Input; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; +using C2CS.Commands.WriteCodeCSharp.Input; using Microsoft.Extensions.DependencyInjection; -namespace C2CS.Features.WriteCodeCSharp; +namespace C2CS.Commands.WriteCodeCSharp; public static class Startup { diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/WriteCodeCSharpCommand.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpCommand.cs similarity index 96% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/WriteCodeCSharpCommand.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpCommand.cs index e3c7b443..463ef687 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/WriteCodeCSharpCommand.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpCommand.cs @@ -5,7 +5,7 @@ using System.CommandLine; using JetBrains.Annotations; -namespace C2CS.Features.WriteCodeCSharp; +namespace C2CS.Commands.WriteCodeCSharp; [UsedImplicitly] public sealed class WriteCodeCSharpCommand : Command diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/WriteCodeCSharpTool.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs similarity index 77% rename from src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/WriteCodeCSharpTool.cs rename to src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs index abb1b6ed..283c0610 100644 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/WriteCodeCSharpTool.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs @@ -6,22 +6,23 @@ using System.IO.Abstractions; using System.Reflection; using bottlenoselabs.Common.Tools; -using C2CS.Features.WriteCodeCSharp.Data; -using C2CS.Features.WriteCodeCSharp.Domain.CodeGenerator; -using C2CS.Features.WriteCodeCSharp.Domain.Mapper; -using C2CS.Features.WriteCodeCSharp.Input; -using C2CS.Features.WriteCodeCSharp.Input.Sanitized; -using C2CS.Features.WriteCodeCSharp.Input.Unsanitized; -using C2CS.Features.WriteCodeCSharp.Output; -using CAstFfi.Data; -using CAstFfi.Data.Serialization; +using C2CS.Commands.WriteCodeCSharp.Data; +using C2CS.Commands.WriteCodeCSharp.Domain.CodeGenerator; +using C2CS.Commands.WriteCodeCSharp.Domain.Mapper; +using C2CS.Commands.WriteCodeCSharp.Input; +using C2CS.Commands.WriteCodeCSharp.Input.Sanitized; +using C2CS.Commands.WriteCodeCSharp.Input.Unsanitized; +using C2CS.Commands.WriteCodeCSharp.Output; +using c2ffi.Data; +using c2ffi.Data.Serialization; using Microsoft.Extensions.Logging; -namespace C2CS.Features.WriteCodeCSharp; +namespace C2CS.Commands.WriteCodeCSharp; public sealed class WriteCodeCSharpTool : Tool { private readonly IServiceProvider _services; + private readonly IFileSystem _fileSystem; public WriteCodeCSharpTool( ILogger logger, @@ -31,11 +32,12 @@ public WriteCodeCSharpTool( : base(logger, inputSanitizer, fileSystem) { _services = services; + _fileSystem = fileSystem; } protected override void Execute(WriteCodeCSharpInput input, WriteCodeCSharpOutput output) { - var abstractSyntaxTreesC = LoadCAbstractSyntaxTree(input.InputFilePath); + var abstractSyntaxTreesC = LoadCFfi(input.InputFilePath); var nodesPerPlatform = MapCNodesToCSharpNodes( abstractSyntaxTreesC, @@ -55,25 +57,25 @@ protected override void Execute(WriteCodeCSharpInput input, WriteCodeCSharpOutpu } } - private CAbstractSyntaxTreeCrossPlatform LoadCAbstractSyntaxTree(string filePath) + private CFfiCrossPlatform LoadCFfi(string filePath) { - BeginStep("Load C abstract syntax tree"); + BeginStep("Load C cross-platform FFI"); - var ast = CJsonSerializer.ReadAbstractSyntaxTreeCrossPlatform(filePath); + var ffi = Json.ReadFfiCrossPlatform(_fileSystem, filePath); EndStep(); - return ast; + return ffi; } private CSharpAbstractSyntaxTree MapCNodesToCSharpNodes( - CAbstractSyntaxTreeCrossPlatform abstractSyntaxTree, + CFfiCrossPlatform ffi, CSharpCodeMapperOptions options) { BeginStep("Map C syntax tree nodes to C#"); var mapper = new CSharpCodeMapper(options); - var result = mapper.Map(Diagnostics, abstractSyntaxTree); + var result = mapper.Map(ffi, Diagnostics); EndStep(); diff --git a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs b/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs deleted file mode 100644 index 5fa8e545..00000000 --- a/src/cs/production/C2CS.Tool/Features/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapperContext.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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 CAstFfi.Data; - -namespace C2CS.Features.WriteCodeCSharp.Domain.Mapper; - -public class CSharpCodeMapperContext -{ - public readonly ImmutableDictionary FunctionPointers; - - public readonly ImmutableDictionary Records; - - public readonly ImmutableHashSet EnumNames; - - public CSharpCodeMapperContext( - ImmutableDictionary records, - ImmutableDictionary functionPointers, - ImmutableHashSet enumNames) - { - Records = records; - FunctionPointers = functionPointers; - EnumNames = enumNames; - } -} diff --git a/src/cs/production/C2CS.Tool/Internal/StringExtensions.cs b/src/cs/production/C2CS.Tool/Internal/StringExtensions.cs new file mode 100644 index 00000000..26bab8e9 --- /dev/null +++ b/src/cs/production/C2CS.Tool/Internal/StringExtensions.cs @@ -0,0 +1,24 @@ +// 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; + +namespace C2CS.Internal; + +public static class StringExtensions +{ + public static string ReplaceFirst( + this string text, string search, string replace, StringComparison stringComparison) + { + var indexOf = text.IndexOf(search, stringComparison); + if (indexOf < 0) + { + return text; + } + + var before = text.AsSpan(0, indexOf); + var after = text.AsSpan(indexOf + search.Length); + var result = string.Concat(before, replace, after); + return result; + } +} diff --git a/src/cs/production/C2CS.Tool/Startup.cs b/src/cs/production/C2CS.Tool/Startup.cs index de4e804f..7288f698 100644 --- a/src/cs/production/C2CS.Tool/Startup.cs +++ b/src/cs/production/C2CS.Tool/Startup.cs @@ -75,7 +75,7 @@ private static void ConfigureServices(IServiceCollection services, string[]? arg services.AddHostedService(); services.AddSingleton(); - Features.WriteCodeCSharp.Startup.ConfigureServices(services); - Features.BuildCLibrary.Startup.ConfigureServices(services); + Commands.WriteCodeCSharp.Startup.ConfigureServices(services); + Commands.BuildCLibrary.Startup.ConfigureServices(services); } } diff --git a/src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs b/src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs index 8af0df57..45888719 100644 --- a/src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs +++ b/src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs @@ -8,10 +8,10 @@ using System.Reflection; using System.Runtime.InteropServices; using bottlenoselabs.Common; -using C2CS.Features.BuildCLibrary.Domain; -using C2CS.Features.BuildCLibrary.Input.Sanitized; -using C2CS.Features.WriteCodeCSharp; -using C2CS.Features.WriteCodeCSharp.Output; +using C2CS.Commands.BuildCLibrary.Domain; +using C2CS.Commands.BuildCLibrary.Input.Sanitized; +using C2CS.Commands.WriteCodeCSharp; +using C2CS.Commands.WriteCodeCSharp.Output; using C2CS.Tests.Data.Models; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -114,16 +114,16 @@ private static void GenerateCAbstractSyntaxTree( string bindgenConfigFilePath, string sourceDirectoryPath) { - var extractShellOutput = $"castffi extract --config {bindgenConfigFilePath}".ExecuteShellCommand(); - Assert.True(extractShellOutput.ExitCode == 0, "error extracting platform ASTs"); - - var abstractSyntaxTreeDirectoryPath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ast")); - var mergedAbstractSyntaxTreeFilePath = - Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ast", "cross-platform.json")); - var astShellOutput = - $"castffi merge --inputDirectoryPath {abstractSyntaxTreeDirectoryPath} --outputFilePath {mergedAbstractSyntaxTreeFilePath}" + var extractShellOutput = $"c2ffi extract --config {bindgenConfigFilePath}".ExecuteShellCommand(); + Assert.True(extractShellOutput.ExitCode == 0, "error extracting platform FFIs"); + + var ffiDirectoryPath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ffi")); + var mergedFffFilePath = + Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ffi-x", "cross-platform.json")); + var ffiShellOutput = + $"c2ffi merge --inputDirectoryPath {ffiDirectoryPath} --outputFilePath {mergedFffFilePath}" .ExecuteShellCommand(); - Assert.True(astShellOutput.ExitCode == 0, "error merging platform ASTs"); + Assert.True(ffiShellOutput.ExitCode == 0, "error merging platform ASTs"); } private WriteCodeCSharpOutput Run(string sourceDirectoryPath) diff --git a/src/cs/tests/C2CS.Tests/TestHost.cs b/src/cs/tests/C2CS.Tests/TestHost.cs index b2518b4f..c9dcdec2 100644 --- a/src/cs/tests/C2CS.Tests/TestHost.cs +++ b/src/cs/tests/C2CS.Tests/TestHost.cs @@ -2,7 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the Git repository root directory for full license information. using System; -using C2CS.Features.BuildCLibrary.Domain; +using C2CS.Commands.BuildCLibrary.Domain; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; From 4414afe86fcca6e45b906c428f547ad529c729d0 Mon Sep 17 00:00:00 2001 From: Lucas Girouard-Stranks <519592+lithiumtoast@users.noreply.github.com> Date: Sun, 14 Apr 2024 12:26:43 -0400 Subject: [PATCH 2/2] Changes and fixes for `c2ffi` --- .gitignore | 8 +- .../ffi_helper/include/ffi_helper.h | 52 ++++- src/c/tests/README.md | 5 - src/c/tests/_container_library/build.sh | 8 - src/c/tests/_container_library/main.c | 6 - src/c/tests/enums/EnumForceSInt16.h | 21 -- src/c/tests/enums/EnumForceSInt32.h | 21 -- src/c/tests/enums/EnumForceSInt8.h | 21 -- src/c/tests/enums/EnumForceUInt16.h | 21 -- src/c/tests/enums/EnumForceUInt32.h | 21 -- src/c/tests/enums/EnumForceUInt8.h | 21 -- src/c/tests/enums/_index.h | 8 - src/c/tests/enums/enum_uint8/config.json | 27 +++ src/c/tests/enums/enum_uint8/main.c | 9 + src/c/tests/enums/enum_week_day/config.json | 27 +++ src/c/tests/enums/enum_week_day/main.c | 14 ++ ...eDef_FunctionPointer_ReturnVoid_ArgsVoid.h | 8 - src/c/tests/function_pointers/_index.h | 3 - .../function_pointer_void/config.json | 24 ++ .../function_pointer_void/main.c | 9 + .../function_attributed/config.json} | 12 +- .../functions/function_attributed/main.c | 5 + .../functions/function_ignored/config.json | 31 +++ src/c/tests/functions/function_ignored/main.c | 18 ++ .../tests/functions/function_int/config.json | 27 +++ src/c/tests/functions/function_int/main.c | 7 + .../function_int_params_int/config.json | 27 +++ .../functions/function_int_params_int/main.c | 7 + .../functions/function_internal/config.json | 31 +++ .../tests/functions/function_internal/main.c | 22 ++ .../config.json | 27 +++ .../main.c | 8 + .../function_void}/CMakeLists.txt | 8 +- .../function_void/config-extract.json | 27 +++ .../function_void/config-generate-cs.json | 8 + src/c/tests/functions/function_void/main.c | 6 + .../tests/macro_objects/MacroObjectIntValue.h | 8 - src/c/tests/macro_objects/_index.h | 1 - .../macro_object_ignored/config.json | 31 +++ .../macro_objects/macro_object_ignored/main.c | 8 + .../macro_object_int/config.json | 27 +++ .../macro_objects/macro_object_int/main.c | 4 + .../macro_object_invalid/config.json | 27 +++ .../macro_objects/macro_object_invalid/main.c | 4 + .../macro_object_no_value/config.json | 27 +++ .../macro_object_no_value/main.c | 4 + .../macro_object_string/config.json | 24 ++ .../macro_objects/macro_object_string/main.c | 4 + .../macro_object_uint64/config.json | 24 ++ .../macro_objects/macro_object_uint64/main.c | 5 + .../opaque_types/opaque_struct/config.json | 27 +++ src/c/tests/opaque_types/opaque_struct/main.c | 6 + .../opaque_typedef_struct/config.json | 27 +++ .../opaque_types/opaque_typedef_struct/main.c | 6 + .../struct_anonymous_char_int/config.json | 27 +++ .../structs/struct_anonymous_char_int/main.c | 12 + .../struct_anonymous_nested/config.json | 27 +++ .../structs/struct_anonymous_nested/main.c | 18 ++ src/c/tests/structs/struct_int/config.json | 27 +++ src/c/tests/structs/struct_int/main.c | 9 + .../union_anonymous_char_int/config.json | 27 +++ .../unions/union_anonymous_char_int/main.c | 12 + .../unions/union_anonymous_nested/config.json | 27 +++ .../unions/union_anonymous_nested/main.c | 18 ++ src/c/tests/unions/union_int_int/config.json | 27 +++ src/c/tests/unions/union_int_int/main.c | 10 + .../variables/variable_ignored/config.json | 31 +++ src/c/tests/variables/variable_ignored/main.c | 8 + .../tests/variables/variable_int/config.json | 27 +++ src/c/tests/variables/variable_int/main.c | 4 + src/cs/production/C2CS.Tool/C2CS.Tool.csproj | 2 +- .../BuildCLibrary/BuildCLibraryTool.cs | 5 + .../Domain/CMakeLibraryBuilder.cs | 1 + .../Data/CSharpAbstractSyntaxTree.cs | 3 +- .../Domain/Mapper/CSharpCodeMapper.cs | 100 ++++---- .../WriteCodeCSharp/WriteCodeCSharpTool.cs | 5 + src/cs/tests/C2CS.Tests/C2CS.Tests.csproj | 3 + .../TestCSharpCodeAbstractSyntaxTree.cs | 26 --- .../Data/Values/Enums/EnumForceSInt16.json | 34 --- .../Data/Values/Enums/EnumForceSInt32.json | 34 --- .../Data/Values/Enums/EnumForceSInt8.json | 34 --- .../Data/Values/Enums/EnumForceUInt16.json | 34 --- .../Data/Values/Enums/EnumForceUInt32.json | 34 --- .../Data/Values/Enums/EnumForceUInt8.json | 34 --- .../MacroObjects/MacroObjectIntValue.json | 5 - .../Functions/function_void/Test.cs | 30 +++ .../C2CS.Tests/Helpers/FileSystemHelper.cs | 68 ++++++ .../Models/CSharpTestAbstractSyntaxTree.cs | 34 +++ .../{Data => }/Models/CSharpTestEnum.cs | 2 +- .../{Data => }/Models/CSharpTestEnumMember.cs | 2 +- .../{Data => }/Models/CSharpTestFunction.cs | 2 +- .../Models/CSharpTestFunctionParameter.cs | 2 +- .../Models/CSharpTestMacroObject.cs | 2 +- .../{Data => }/Models/CSharpTestStruct.cs | 2 +- .../Models/CSharpTestStructField.cs | 2 +- .../Models/CSharpTestStructLayout.cs | 2 +- src/cs/tests/C2CS.Tests/TestBase.cs | 134 ----------- src/cs/tests/C2CS.Tests/TestCSharpCode.cs | 58 ----- src/cs/tests/C2CS.Tests/TestHost.cs | 4 +- ...reCSharpCode.cs => WriteCSharpCodeTest.cs} | 216 ++++-------------- src/cs/tests/C2CS.Tests/appsettings.json | 17 -- src/cs/tests/C2CS.Tests/config-cs.json | 7 - 102 files changed, 1250 insertions(+), 868 deletions(-) delete mode 100644 src/c/tests/README.md delete mode 100755 src/c/tests/_container_library/build.sh delete mode 100644 src/c/tests/_container_library/main.c delete mode 100644 src/c/tests/enums/EnumForceSInt16.h delete mode 100644 src/c/tests/enums/EnumForceSInt32.h delete mode 100644 src/c/tests/enums/EnumForceSInt8.h delete mode 100644 src/c/tests/enums/EnumForceUInt16.h delete mode 100644 src/c/tests/enums/EnumForceUInt32.h delete mode 100644 src/c/tests/enums/EnumForceUInt8.h delete mode 100644 src/c/tests/enums/_index.h create mode 100644 src/c/tests/enums/enum_uint8/config.json create mode 100644 src/c/tests/enums/enum_uint8/main.c create mode 100644 src/c/tests/enums/enum_week_day/config.json create mode 100644 src/c/tests/enums/enum_week_day/main.c delete mode 100644 src/c/tests/function_pointers/TypeDef_FunctionPointer_ReturnVoid_ArgsVoid.h delete mode 100644 src/c/tests/function_pointers/_index.h create mode 100644 src/c/tests/function_pointers/function_pointer_void/config.json create mode 100644 src/c/tests/function_pointers/function_pointer_void/main.c rename src/{cs/tests/C2CS.Tests/config-extract.json => c/tests/functions/function_attributed/config.json} (56%) create mode 100644 src/c/tests/functions/function_attributed/main.c create mode 100644 src/c/tests/functions/function_ignored/config.json create mode 100644 src/c/tests/functions/function_ignored/main.c create mode 100644 src/c/tests/functions/function_int/config.json create mode 100644 src/c/tests/functions/function_int/main.c create mode 100644 src/c/tests/functions/function_int_params_int/config.json create mode 100644 src/c/tests/functions/function_int_params_int/main.c create mode 100644 src/c/tests/functions/function_internal/config.json create mode 100644 src/c/tests/functions/function_internal/main.c create mode 100644 src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/config.json create mode 100644 src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/main.c rename src/c/tests/{_container_library => functions/function_void}/CMakeLists.txt (50%) create mode 100644 src/c/tests/functions/function_void/config-extract.json create mode 100644 src/c/tests/functions/function_void/config-generate-cs.json create mode 100644 src/c/tests/functions/function_void/main.c delete mode 100644 src/c/tests/macro_objects/MacroObjectIntValue.h delete mode 100644 src/c/tests/macro_objects/_index.h create mode 100644 src/c/tests/macro_objects/macro_object_ignored/config.json create mode 100644 src/c/tests/macro_objects/macro_object_ignored/main.c create mode 100644 src/c/tests/macro_objects/macro_object_int/config.json create mode 100644 src/c/tests/macro_objects/macro_object_int/main.c create mode 100644 src/c/tests/macro_objects/macro_object_invalid/config.json create mode 100644 src/c/tests/macro_objects/macro_object_invalid/main.c create mode 100644 src/c/tests/macro_objects/macro_object_no_value/config.json create mode 100644 src/c/tests/macro_objects/macro_object_no_value/main.c create mode 100644 src/c/tests/macro_objects/macro_object_string/config.json create mode 100644 src/c/tests/macro_objects/macro_object_string/main.c create mode 100644 src/c/tests/macro_objects/macro_object_uint64/config.json create mode 100644 src/c/tests/macro_objects/macro_object_uint64/main.c create mode 100644 src/c/tests/opaque_types/opaque_struct/config.json create mode 100644 src/c/tests/opaque_types/opaque_struct/main.c create mode 100644 src/c/tests/opaque_types/opaque_typedef_struct/config.json create mode 100644 src/c/tests/opaque_types/opaque_typedef_struct/main.c create mode 100644 src/c/tests/structs/struct_anonymous_char_int/config.json create mode 100644 src/c/tests/structs/struct_anonymous_char_int/main.c create mode 100644 src/c/tests/structs/struct_anonymous_nested/config.json create mode 100644 src/c/tests/structs/struct_anonymous_nested/main.c create mode 100644 src/c/tests/structs/struct_int/config.json create mode 100644 src/c/tests/structs/struct_int/main.c create mode 100644 src/c/tests/unions/union_anonymous_char_int/config.json create mode 100644 src/c/tests/unions/union_anonymous_char_int/main.c create mode 100644 src/c/tests/unions/union_anonymous_nested/config.json create mode 100644 src/c/tests/unions/union_anonymous_nested/main.c create mode 100644 src/c/tests/unions/union_int_int/config.json create mode 100644 src/c/tests/unions/union_int_int/main.c create mode 100644 src/c/tests/variables/variable_ignored/config.json create mode 100644 src/c/tests/variables/variable_ignored/main.c create mode 100644 src/c/tests/variables/variable_int/config.json create mode 100644 src/c/tests/variables/variable_int/main.c delete mode 100644 src/cs/tests/C2CS.Tests/Data/Models/TestCSharpCodeAbstractSyntaxTree.cs delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt16.json delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt32.json delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt8.json delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt16.json delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt32.json delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt8.json delete mode 100644 src/cs/tests/C2CS.Tests/Data/Values/MacroObjects/MacroObjectIntValue.json create mode 100644 src/cs/tests/C2CS.Tests/Functions/function_void/Test.cs create mode 100644 src/cs/tests/C2CS.Tests/Helpers/FileSystemHelper.cs create mode 100644 src/cs/tests/C2CS.Tests/Models/CSharpTestAbstractSyntaxTree.cs rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestEnum.cs (95%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestEnumMember.cs (94%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestFunction.cs (95%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestFunctionParameter.cs (94%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestMacroObject.cs (94%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestStruct.cs (95%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestStructField.cs (95%) rename src/cs/tests/C2CS.Tests/{Data => }/Models/CSharpTestStructLayout.cs (94%) delete mode 100644 src/cs/tests/C2CS.Tests/TestBase.cs delete mode 100644 src/cs/tests/C2CS.Tests/TestCSharpCode.cs rename src/cs/tests/C2CS.Tests/{TestFixtureCSharpCode.cs => WriteCSharpCodeTest.cs} (61%) delete mode 100644 src/cs/tests/C2CS.Tests/appsettings.json delete mode 100644 src/cs/tests/C2CS.Tests/config-cs.json diff --git a/.gitignore b/.gitignore index c4154501..126d8cef 100644 --- a/.gitignore +++ b/.gitignore @@ -22,8 +22,12 @@ lib/ *.dylib *.so -# C2CS artifacts -ast/ +# c2ffi artifacts +src/c/tests/**/ffi/ +src/c/tests/**/ffi-x/ + +# c2cs artifacts +src/c/tests/**/cs # macOS .DS_Store diff --git a/src/c/production/ffi_helper/include/ffi_helper.h b/src/c/production/ffi_helper/include/ffi_helper.h index 4923fa72..3eea2076 100644 --- a/src/c/production/ffi_helper/include/ffi_helper.h +++ b/src/c/production/ffi_helper/include/ffi_helper.h @@ -1,6 +1,8 @@ -// Provides macros, types, and functions that make P/Invoke with C# easier. +// Provides macros, types, and functions that make FFI easier. + #pragma once -#include + +#define FFI_COMPILER_CLANG __clang__ #if defined(__APPLE__) && __has_include("TargetConditionals.h") #include @@ -30,7 +32,8 @@ #define FFI_TARGET_ENV_GNU defined(__GNUC__) #endif -#if FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_GNU +#if FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_GNU // Windows (GNU) + #if FFI_TARGET_CPU_X64 #define FFI_PLATFORM_NAME "x86_64-pc-windows-gnu" #elif FFI_TARGET_CPU_X86 @@ -39,8 +42,11 @@ #define FFI_PLATFORM_NAME "aarch64-pc-windows-gnu" #else #error "Unknown computer architecture for Windows (GNU)." + #define FFI_PLATFORM_NAME 0 #endif -#elif FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_MSVC + +#elif FFI_TARGET_OS_WINDOWS && FFI_TARGET_ENV_MSVC // Windows (Microsoft Visual C++) + #if FFI_TARGET_CPU_X64 #define FFI_PLATFORM_NAME "x86_64-pc-windows-msvc" #elif FFI_TARGET_CPU_X86 @@ -49,8 +55,11 @@ #define FFI_PLATFORM_NAME "aarch64-pc-windows-msvc" #else #error "Unknown computer architecture for Windows (Microsoft Visual C++)." + #define FFI_PLATFORM_NAME 0 #endif -#elif FFI_TARGET_OS_LINUX + +#elif FFI_TARGET_OS_LINUX // Linux + #if FFI_TARGET_CPU_X64 #define FFI_PLATFORM_NAME "x86_64-unknown-linux-gnu" #elif FFI_TARGET_CPU_X86 @@ -59,8 +68,11 @@ #define FFI_PLATFORM_NAME "aarch64-unknown-linux-gnu" #else #error "Unknown computer architecture for Linux." + #define FFI_PLATFORM_NAME 0 #endif -#elif FFI_TARGET_OS_MACOS + +#elif FFI_TARGET_OS_MACOS // macOS + #if FFI_TARGET_CPU_X64 #define FFI_PLATFORM_NAME "x86_64-apple-darwin" #elif FFI_TARGET_CPU_X86 @@ -69,8 +81,11 @@ #define FFI_PLATFORM_NAME "aarch64-apple-darwin" #else #error "Unknown computer architecture for macOS." + #define FFI_PLATFORM_NAME 0 #endif -#elif FFI_TARGET_OS_IOS + +#elif FFI_TARGET_OS_IOS // iOS + #if FFI_TARGET_CPU_X64 #define FFI_PLATFORM_NAME "x86_64-apple-ios" #elif FFI_TARGET_CPU_X86 @@ -79,13 +94,28 @@ #define FFI_PLATFORM_NAME "aarch64-apple-ios" #else #error "Unknown computer architecture for iOS." + #define FFI_PLATFORM_NAME 0 #endif -#else + +#else // Unknown + + #error "Unknown computer architecture for unknown platform name." #define FFI_PLATFORM_NAME 0 + #endif #if FFI_TARGET_OS_WINDOWS - #define FFI_API_DECL __declspec(dllexport) + #if FFI_COMPILER_CLANG + #define FFI_API_DECL __declspec(dllexport) __attribute__ ((visibility("default"))) + #else + #define FFI_API_DECL __declspec(dllexport) + #endif #else - #define FFI_API_DECL extern -#endif \ No newline at end of file + #define FFI_API_DECL extern __attribute__ ((visibility("default"))) +#endif + +// Returns the current platform name. +FFI_API_DECL const char* ffi_get_platform_name() +{ + return FFI_PLATFORM_NAME; +} diff --git a/src/c/tests/README.md b/src/c/tests/README.md deleted file mode 100644 index 89323e71..00000000 --- a/src/c/tests/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Tests: C code - -The "_container_library" directory is the CMake project for all the C code used for tests. - -Other directories are test specific. Each test specific directory has a `_index.h` which lists the files for that specific directory. \ No newline at end of file diff --git a/src/c/tests/_container_library/build.sh b/src/c/tests/_container_library/build.sh deleted file mode 100755 index 3721aaa4..00000000 --- a/src/c/tests/_container_library/build.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -OUPUT_DIRECTORY=$DIRECTORY/bin - -rm -rf $DIRECTORY/cmake-build-release -cmake -S $DIRECTORY -B cmake-build-release -DCMAKE_BUILD_TYPE=Release -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=$OUPUT_DIRECTORY -DCMAKE_RUNTIME_OUTPUT_DIRECTORY=$OUPUT_DIRECTORY -DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=$OUPUT_DIRECTORY -cmake --build cmake-build-release --config Release -rm -rf $DIRECTORY/cmake-build-release \ No newline at end of file diff --git a/src/c/tests/_container_library/main.c b/src/c/tests/_container_library/main.c deleted file mode 100644 index ff25e9d8..00000000 --- a/src/c/tests/_container_library/main.c +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include "ffi_helper.h" - -#include "../enums/_index.h" -#include "../macro_objects/_index.h" -#include "../function_pointers/_index.h" diff --git a/src/c/tests/enums/EnumForceSInt16.h b/src/c/tests/enums/EnumForceSInt16.h deleted file mode 100644 index 4d20292a..00000000 --- a/src/c/tests/enums/EnumForceSInt16.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -typedef enum EnumForceSInt16 { - ENUM_FORCE_SINT16_DAY_UNKNOWN, - ENUM_FORCE_SINT16_DAY_MONDAY, - ENUM_FORCE_SINT16_DAY_TUESDAY, - ENUM_FORCE_SINT16_DAY_WEDNESDAY, - ENUM_FORCE_SINT16_DAY_THURSDAY, - ENUM_FORCE_SINT16_DAY_FRIDAY, - _ENUM_FORCE_SINT16 = 0x7FFF -} EnumForceSInt16; - -FFI_API_DECL void EnumForceSInt16__print_EnumForceSInt16(const EnumForceSInt16 e) -{ - printf("%d\n", e); // Print used for testing -} - -FFI_API_DECL EnumForceSInt16 EnumForceSInt16__return_EnumForceSInt16(const EnumForceSInt16 e) -{ - return e; -} \ No newline at end of file diff --git a/src/c/tests/enums/EnumForceSInt32.h b/src/c/tests/enums/EnumForceSInt32.h deleted file mode 100644 index 51b55441..00000000 --- a/src/c/tests/enums/EnumForceSInt32.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -typedef enum EnumForceSInt32 { - ENUM_FORCE_SINT32_DAY_UNKNOWN, - ENUM_FORCE_SINT32_DAY_MONDAY, - ENUM_FORCE_SINT32_DAY_TUESDAY, - ENUM_FORCE_SINT32_DAY_WEDNESDAY, - ENUM_FORCE_SINT32_DAY_THURSDAY, - ENUM_FORCE_SINT32_DAY_FRIDAY, - _ENUM_FORCE_SINT32 = 0x7FFFFFFFUL -} EnumForceSInt32; - -FFI_API_DECL void EnumForceSInt32__print_EnumForceSInt32(const EnumForceSInt32 e) -{ - printf("%d\n", e); // Print used for testing -} - -FFI_API_DECL EnumForceSInt32 EnumForceSInt32__return_EnumForceSInt32(const EnumForceSInt32 e) -{ - return e; -} \ No newline at end of file diff --git a/src/c/tests/enums/EnumForceSInt8.h b/src/c/tests/enums/EnumForceSInt8.h deleted file mode 100644 index 3eec1dab..00000000 --- a/src/c/tests/enums/EnumForceSInt8.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -typedef enum EnumForceSInt8 { - ENUM_FORCE_SINT8_DAY_UNKNOWN, - ENUM_FORCE_SINT8_DAY_MONDAY, - ENUM_FORCE_SINT8_DAY_TUESDAY, - ENUM_FORCE_SINT8_DAY_WEDNESDAY, - ENUM_FORCE_SINT8_DAY_THURSDAY, - ENUM_FORCE_SINT8_DAY_FRIDAY, - _ENUM_FORCE_SINT8 = 0x7F -} EnumForceSInt8; - -FFI_API_DECL void EnumForceSInt8__print_EnumForceSInt8(const EnumForceSInt8 e) -{ - printf("%d\n", e); // Print used for testing -} - -FFI_API_DECL EnumForceSInt8 EnumForceSInt8__return_EnumForceSInt8(const EnumForceSInt8 e) -{ - return e; -} \ No newline at end of file diff --git a/src/c/tests/enums/EnumForceUInt16.h b/src/c/tests/enums/EnumForceUInt16.h deleted file mode 100644 index b08e0c2e..00000000 --- a/src/c/tests/enums/EnumForceUInt16.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -typedef enum EnumForceUInt16 { - ENUM_FORCE_UINT16_DAY_UNKNOWN, - ENUM_FORCE_UINT16_DAY_MONDAY, - ENUM_FORCE_UINT16_DAY_TUESDAY, - ENUM_FORCE_UINT16_DAY_WEDNESDAY, - ENUM_FORCE_UINT16_DAY_THURSDAY, - ENUM_FORCE_UINT16_DAY_FRIDAY, - _ENUM_FORCE_UINT16 = 0xFFFF -} EnumForceUInt16; - -FFI_API_DECL void EnumForceUInt16__print_EnumForceUInt16(const EnumForceUInt16 e) -{ - printf("%d\n", e); // Print used for testing -} - -FFI_API_DECL EnumForceUInt16 EnumForceUInt16__return_EnumForceUInt16(const EnumForceUInt16 e) -{ - return e; -} \ No newline at end of file diff --git a/src/c/tests/enums/EnumForceUInt32.h b/src/c/tests/enums/EnumForceUInt32.h deleted file mode 100644 index 6907ae8e..00000000 --- a/src/c/tests/enums/EnumForceUInt32.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -typedef enum EnumForceUInt32 { - ENUM_FORCE_UINT32_DAY_UNKNOWN, - ENUM_FORCE_UINT32_DAY_MONDAY, - ENUM_FORCE_UINT32_DAY_TUESDAY, - ENUM_FORCE_UINT32_DAY_WEDNESDAY, - ENUM_FORCE_UINT32_DAY_THURSDAY, - ENUM_FORCE_UINT32_DAY_FRIDAY, - _ENUM_FORCE_UINT32 = 0xFFFFFFFFUL -} EnumForceUInt32; - -FFI_API_DECL void EnumForceUInt32__print_EnumForceUInt32(const EnumForceUInt32 e) -{ - printf("%lu\n", e); // Print used for testing -} - -FFI_API_DECL EnumForceUInt32 EnumForceUInt32__return_EnumForceUInt32(const EnumForceUInt32 e) -{ - return e; -} \ No newline at end of file diff --git a/src/c/tests/enums/EnumForceUInt8.h b/src/c/tests/enums/EnumForceUInt8.h deleted file mode 100644 index 9ee524d8..00000000 --- a/src/c/tests/enums/EnumForceUInt8.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -typedef enum EnumForceUInt8 { - ENUM_FORCE_UINT8_DAY_UNKNOWN, - ENUM_FORCE_UINT8_DAY_MONDAY, - ENUM_FORCE_UINT8_DAY_TUESDAY, - ENUM_FORCE_UINT8_DAY_WEDNESDAY, - ENUM_FORCE_UINT8_DAY_THURSDAY, - ENUM_FORCE_UINT8_DAY_FRIDAY, - _ENUM_FORCE_UINT8 = 0xFF -} EnumForceUInt8; - -FFI_API_DECL void EnumForceUInt8__print_EnumForceUInt8(const EnumForceUInt8 e) -{ - printf("%d\n", e); // Print used for testing -} - -FFI_API_DECL EnumForceUInt8 EnumForceUInt8__return_EnumForceUInt8(const EnumForceUInt8 e) -{ - return e; -} \ No newline at end of file diff --git a/src/c/tests/enums/_index.h b/src/c/tests/enums/_index.h deleted file mode 100644 index f5844786..00000000 --- a/src/c/tests/enums/_index.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#include "EnumForceSInt8.h" -#include "EnumForceSInt16.h" -#include "EnumForceSInt32.h" -#include "EnumForceUInt8.h" -#include "EnumForceUInt16.h" -#include "EnumForceUInt32.h" \ No newline at end of file diff --git a/src/c/tests/enums/enum_uint8/config.json b/src/c/tests/enums/enum_uint8/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/enums/enum_uint8/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/enums/enum_uint8/main.c b/src/c/tests/enums/enum_uint8/main.c new file mode 100644 index 00000000..d89b2e6e --- /dev/null +++ b/src/c/tests/enums/enum_uint8/main.c @@ -0,0 +1,9 @@ +#include +#include "ffi_helper.h" + +enum enum_uint8 { + ENUM_UINT8_MIN = 0, + ENUM_UINT8_MAX = 255 +} enum_uint8; + +FFI_API_DECL enum enum_uint8 enum_uint8; diff --git a/src/c/tests/enums/enum_week_day/config.json b/src/c/tests/enums/enum_week_day/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/enums/enum_week_day/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/enums/enum_week_day/main.c b/src/c/tests/enums/enum_week_day/main.c new file mode 100644 index 00000000..3efe1328 --- /dev/null +++ b/src/c/tests/enums/enum_week_day/main.c @@ -0,0 +1,14 @@ +#include +#include "ffi_helper.h" + +typedef enum enum_week_day { + ENUM_WEEK_DAY_UNKNOWN = -1, + ENUM_WEEK_DAY_MONDAY = 1, + ENUM_WEEK_DAY_TUESDAY = 2, + ENUM_WEEK_DAY_WEDNESDAY = 3, + ENUM_WEEK_DAY_THURSDAY = 4, + ENUM_WEEK_DAY_FRIDAY = 5, + _ENUM_WEEK_DAY_MAX = 6 +} enum_week_day; + +FFI_API_DECL enum_week_day enum_week_day; diff --git a/src/c/tests/function_pointers/TypeDef_FunctionPointer_ReturnVoid_ArgsVoid.h b/src/c/tests/function_pointers/TypeDef_FunctionPointer_ReturnVoid_ArgsVoid.h deleted file mode 100644 index 1d0cc916..00000000 --- a/src/c/tests/function_pointers/TypeDef_FunctionPointer_ReturnVoid_ArgsVoid.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -typedef void (*TypeDef_FunctionPointer_ReturnVoid_ArgsVoid) (void); - -FFI_API_DECL void TypeDef_FunctionPointer_ReturnVoid_ArgsVoid__invoke(TypeDef_FunctionPointer_ReturnVoid_ArgsVoid functionPointer) -{ - functionPointer(); -} \ No newline at end of file diff --git a/src/c/tests/function_pointers/_index.h b/src/c/tests/function_pointers/_index.h deleted file mode 100644 index 74bc366c..00000000 --- a/src/c/tests/function_pointers/_index.h +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#include "TypeDef_FunctionPointer_ReturnVoid_ArgsVoid.h" \ No newline at end of file diff --git a/src/c/tests/function_pointers/function_pointer_void/config.json b/src/c/tests/function_pointers/function_pointer_void/config.json new file mode 100644 index 00000000..8b4b5916 --- /dev/null +++ b/src/c/tests/function_pointers/function_pointer_void/config.json @@ -0,0 +1,24 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/function_pointers/function_pointer_void/main.c b/src/c/tests/function_pointers/function_pointer_void/main.c new file mode 100644 index 00000000..3fd1f07d --- /dev/null +++ b/src/c/tests/function_pointers/function_pointer_void/main.c @@ -0,0 +1,9 @@ +#include +#include "ffi_helper.h" + +typedef void (*function_pointer_void)(); + +FFI_API_DECL void function(function_pointer_void fnptr) +{ + (*fnptr)(); +} diff --git a/src/cs/tests/C2CS.Tests/config-extract.json b/src/c/tests/functions/function_attributed/config.json similarity index 56% rename from src/cs/tests/C2CS.Tests/config-extract.json rename to src/c/tests/functions/function_attributed/config.json index df0d372d..b3d8b8e5 100644 --- a/src/cs/tests/C2CS.Tests/config-extract.json +++ b/src/c/tests/functions/function_attributed/config.json @@ -1,17 +1,19 @@ { - "inputFilePath": "../../../c/tests/_container_library/main.c", + "inputFilePath": "./main.c", "userIncludeDirectories": [ - "../../../c/production/ffi_helper/include", - "../../../c/tests/_container_library" + "../../../production/ffi_helper/include" ], - "targetPlatforms": { + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { "windows": { "x86_64-pc-windows-msvc": {}, "aarch64-pc-windows-msvc": {} }, "macos": { "aarch64-apple-darwin": {}, - "x86_64-apple-darwin": {} + "x86_64-apple-darwin": {}, }, "linux": { "x86_64-unknown-linux-gnu": {}, diff --git a/src/c/tests/functions/function_attributed/main.c b/src/c/tests/functions/function_attributed/main.c new file mode 100644 index 00000000..2ac3912c --- /dev/null +++ b/src/c/tests/functions/function_attributed/main.c @@ -0,0 +1,5 @@ +#include +#include "ffi_helper.h" + +extern FFI_API_DECL __attribute__((malloc)) void* __cdecl function_attributed(size_t size); + diff --git a/src/c/tests/functions/function_ignored/config.json b/src/c/tests/functions/function_ignored/config.json new file mode 100644 index 00000000..44a5961d --- /dev/null +++ b/src/c/tests/functions/function_ignored/config.json @@ -0,0 +1,31 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "ignoredFunctions": [ + "function_not_allowed", + "function_ignored*" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/functions/function_ignored/main.c b/src/c/tests/functions/function_ignored/main.c new file mode 100644 index 00000000..6ef88b55 --- /dev/null +++ b/src/c/tests/functions/function_ignored/main.c @@ -0,0 +1,18 @@ +#include +#include "ffi_helper.h" + +FFI_API_DECL void function_allowed() +{ +} + +FFI_API_DECL void function_not_allowed() +{ +} + +FFI_API_DECL void function_ignored1() +{ +} + +FFI_API_DECL void function_ignored2() +{ +} diff --git a/src/c/tests/functions/function_int/config.json b/src/c/tests/functions/function_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/functions/function_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/functions/function_int/main.c b/src/c/tests/functions/function_int/main.c new file mode 100644 index 00000000..43afebad --- /dev/null +++ b/src/c/tests/functions/function_int/main.c @@ -0,0 +1,7 @@ +#include +#include "ffi_helper.h" + +FFI_API_DECL int function_int() +{ + return 42; +} diff --git a/src/c/tests/functions/function_int_params_int/config.json b/src/c/tests/functions/function_int_params_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/functions/function_int_params_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/functions/function_int_params_int/main.c b/src/c/tests/functions/function_int_params_int/main.c new file mode 100644 index 00000000..ef28a4ab --- /dev/null +++ b/src/c/tests/functions/function_int_params_int/main.c @@ -0,0 +1,7 @@ +#include +#include "ffi_helper.h" + +FFI_API_DECL int function_int_params_int(int a) +{ + return a; +} diff --git a/src/c/tests/functions/function_internal/config.json b/src/c/tests/functions/function_internal/config.json new file mode 100644 index 00000000..8e6def6f --- /dev/null +++ b/src/c/tests/functions/function_internal/config.json @@ -0,0 +1,31 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "defines": { + "BAD_CUSTOM_API_DECL": "extern", + "GOOD_CUSTOM_API_DECL": "extern __attribute__ ((visibility(\"default\")))" + }, + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/functions/function_internal/main.c b/src/c/tests/functions/function_internal/main.c new file mode 100644 index 00000000..4f74a6ba --- /dev/null +++ b/src/c/tests/functions/function_internal/main.c @@ -0,0 +1,22 @@ +#include +#include "ffi_helper.h" + +void function_internal_1() +{ +} + +FFI_API_DECL void function_internal_2() +{ +} + +UNKNOWN_CUSTOM_API_DECL void function_internal_3() +{ +} + +BAD_CUSTOM_API_DECL void function_internal_4() +{ +} + +GOOD_CUSTOM_API_DECL void function_internal_5() +{ +} diff --git a/src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/config.json b/src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/main.c b/src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/main.c new file mode 100644 index 00000000..56307878 --- /dev/null +++ b/src/c/tests/functions/function_uint64_params_uint8_uint16_uint32/main.c @@ -0,0 +1,8 @@ +#include +#include +#include "ffi_helper.h" + +FFI_API_DECL uint64_t function_uint64_params_uint8_uint16_uint32(uint8_t a, uint16_t b, uint32_t c) +{ + return a + b + c; +} diff --git a/src/c/tests/_container_library/CMakeLists.txt b/src/c/tests/functions/function_void/CMakeLists.txt similarity index 50% rename from src/c/tests/_container_library/CMakeLists.txt rename to src/c/tests/functions/function_void/CMakeLists.txt index 6b3284ed..6c68f178 100644 --- a/src/c/tests/_container_library/CMakeLists.txt +++ b/src/c/tests/functions/function_void/CMakeLists.txt @@ -1,13 +1,13 @@ cmake_minimum_required(VERSION 3.16) -project(_container_library C) +project(test C) set(CMAKE_C_STANDARD 11) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin) -include_directories(_container_library PRIVATE ../../production/ffi_helper/include) +include_directories(test PRIVATE ../../../production/ffi_helper/include) -add_library(_container_library SHARED ${SOURCE_FILES} main.c) -set_target_properties(_container_library PROPERTIES LINKER_LANGUAGE C) \ No newline at end of file +add_library(test SHARED ${SOURCE_FILES} main.c) +set_target_properties(test PROPERTIES LINKER_LANGUAGE C) diff --git a/src/c/tests/functions/function_void/config-extract.json b/src/c/tests/functions/function_void/config-extract.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/functions/function_void/config-extract.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/functions/function_void/config-generate-cs.json b/src/c/tests/functions/function_void/config-generate-cs.json new file mode 100644 index 00000000..590ea0b4 --- /dev/null +++ b/src/c/tests/functions/function_void/config-generate-cs.json @@ -0,0 +1,8 @@ +{ + "inputFilePath": "./ffi-x/cross-platform.json", + "outputFileDirectory": "./cs", + "namespaceName": "test_namespace", + "className": "test_class", + "isEnabledFileScopedNamespace": false, + "isEnabledLibraryImport": true +} diff --git a/src/c/tests/functions/function_void/main.c b/src/c/tests/functions/function_void/main.c new file mode 100644 index 00000000..4480c30f --- /dev/null +++ b/src/c/tests/functions/function_void/main.c @@ -0,0 +1,6 @@ +#include +#include "ffi_helper.h" + +FFI_API_DECL void function_void() +{ +} diff --git a/src/c/tests/macro_objects/MacroObjectIntValue.h b/src/c/tests/macro_objects/MacroObjectIntValue.h deleted file mode 100644 index 9c1963f8..00000000 --- a/src/c/tests/macro_objects/MacroObjectIntValue.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -#define MACRO_OBJECT_INT_VALUE 42; - -FFI_API_DECL int MacroObjectInt__return_int() -{ - return MACRO_OBJECT_INT_VALUE; -} \ No newline at end of file diff --git a/src/c/tests/macro_objects/_index.h b/src/c/tests/macro_objects/_index.h deleted file mode 100644 index af9edbf1..00000000 --- a/src/c/tests/macro_objects/_index.h +++ /dev/null @@ -1 +0,0 @@ -#include "MacroObjectIntValue.h" \ No newline at end of file diff --git a/src/c/tests/macro_objects/macro_object_ignored/config.json b/src/c/tests/macro_objects/macro_object_ignored/config.json new file mode 100644 index 00000000..755aa778 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_ignored/config.json @@ -0,0 +1,31 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "ignoredMacroObjects": [ + "MACRO_OBJECT_NOT_ALLOWED", + "MACRO_OBJECT_IGNORED*" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/macro_objects/macro_object_ignored/main.c b/src/c/tests/macro_objects/macro_object_ignored/main.c new file mode 100644 index 00000000..1a8b3ddb --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_ignored/main.c @@ -0,0 +1,8 @@ +#include +#include "ffi_helper.h" + +#define MACRO_OBJECT_NOT_ALLOWED 42 +#define MACRO_OBJECT_ALLOWED 42 + +#define MACRO_OBJECT_IGNORED_1 42 +#define MACRO_OBJECT_IGNORED_2 42 diff --git a/src/c/tests/macro_objects/macro_object_int/config.json b/src/c/tests/macro_objects/macro_object_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/macro_objects/macro_object_int/main.c b/src/c/tests/macro_objects/macro_object_int/main.c new file mode 100644 index 00000000..e4272123 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_int/main.c @@ -0,0 +1,4 @@ +#include +#include "ffi_helper.h" + +#define MACRO_OBJECT_INT 42 diff --git a/src/c/tests/macro_objects/macro_object_invalid/config.json b/src/c/tests/macro_objects/macro_object_invalid/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_invalid/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/macro_objects/macro_object_invalid/main.c b/src/c/tests/macro_objects/macro_object_invalid/main.c new file mode 100644 index 00000000..42cc1449 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_invalid/main.c @@ -0,0 +1,4 @@ +#include +#include "ffi_helper.h" + +#define MACRO_OBJECT_INVALID = 42; diff --git a/src/c/tests/macro_objects/macro_object_no_value/config.json b/src/c/tests/macro_objects/macro_object_no_value/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_no_value/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/macro_objects/macro_object_no_value/main.c b/src/c/tests/macro_objects/macro_object_no_value/main.c new file mode 100644 index 00000000..4e3f3b51 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_no_value/main.c @@ -0,0 +1,4 @@ +#include +#include "ffi_helper.h" + +#define MACRO_OBJECT_NO_VALUE diff --git a/src/c/tests/macro_objects/macro_object_string/config.json b/src/c/tests/macro_objects/macro_object_string/config.json new file mode 100644 index 00000000..8b4b5916 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_string/config.json @@ -0,0 +1,24 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/macro_objects/macro_object_string/main.c b/src/c/tests/macro_objects/macro_object_string/main.c new file mode 100644 index 00000000..231ea950 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_string/main.c @@ -0,0 +1,4 @@ +#include +#include "ffi_helper.h" + +#define MACRO_OBJECT_STRING "42" diff --git a/src/c/tests/macro_objects/macro_object_uint64/config.json b/src/c/tests/macro_objects/macro_object_uint64/config.json new file mode 100644 index 00000000..8b4b5916 --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_uint64/config.json @@ -0,0 +1,24 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/macro_objects/macro_object_uint64/main.c b/src/c/tests/macro_objects/macro_object_uint64/main.c new file mode 100644 index 00000000..7aeb659e --- /dev/null +++ b/src/c/tests/macro_objects/macro_object_uint64/main.c @@ -0,0 +1,5 @@ +#include +#include +#include "ffi_helper.h" + +#define MACRO_OBJECT_UINT64 ((uint64_t)42) diff --git a/src/c/tests/opaque_types/opaque_struct/config.json b/src/c/tests/opaque_types/opaque_struct/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/opaque_types/opaque_struct/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/opaque_types/opaque_struct/main.c b/src/c/tests/opaque_types/opaque_struct/main.c new file mode 100644 index 00000000..f3cb55ce --- /dev/null +++ b/src/c/tests/opaque_types/opaque_struct/main.c @@ -0,0 +1,6 @@ +#include +#include "ffi_helper.h" + +struct opaque_struct; + +FFI_API_DECL struct opaque_struct opaque_struct; diff --git a/src/c/tests/opaque_types/opaque_typedef_struct/config.json b/src/c/tests/opaque_types/opaque_typedef_struct/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/opaque_types/opaque_typedef_struct/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/opaque_types/opaque_typedef_struct/main.c b/src/c/tests/opaque_types/opaque_typedef_struct/main.c new file mode 100644 index 00000000..391e09c6 --- /dev/null +++ b/src/c/tests/opaque_types/opaque_typedef_struct/main.c @@ -0,0 +1,6 @@ +#include +#include "ffi_helper.h" + +typedef struct opaque_struct_t opaque_struct_t; + +FFI_API_DECL opaque_struct_t opaque_struct; diff --git a/src/c/tests/structs/struct_anonymous_char_int/config.json b/src/c/tests/structs/struct_anonymous_char_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/structs/struct_anonymous_char_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/structs/struct_anonymous_char_int/main.c b/src/c/tests/structs/struct_anonymous_char_int/main.c new file mode 100644 index 00000000..e0546ec9 --- /dev/null +++ b/src/c/tests/structs/struct_anonymous_char_int/main.c @@ -0,0 +1,12 @@ +#include +#include "ffi_helper.h" + +struct struct_anonymous_char_int +{ + struct { + char a; + int b; + }; +}; + +FFI_API_DECL struct struct_anonymous_char_int struct_anonymous_char_int; diff --git a/src/c/tests/structs/struct_anonymous_nested/config.json b/src/c/tests/structs/struct_anonymous_nested/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/structs/struct_anonymous_nested/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/structs/struct_anonymous_nested/main.c b/src/c/tests/structs/struct_anonymous_nested/main.c new file mode 100644 index 00000000..7ea74350 --- /dev/null +++ b/src/c/tests/structs/struct_anonymous_nested/main.c @@ -0,0 +1,18 @@ +#include +#include "ffi_helper.h" + +struct struct_anonymous_nested +{ + struct { + struct { + char a; + int b; + }; + struct { + char c; + int d; + }; + }; +}; + +FFI_API_DECL struct struct_anonymous_nested struct_anonymous_nested; diff --git a/src/c/tests/structs/struct_int/config.json b/src/c/tests/structs/struct_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/structs/struct_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/structs/struct_int/main.c b/src/c/tests/structs/struct_int/main.c new file mode 100644 index 00000000..70106a30 --- /dev/null +++ b/src/c/tests/structs/struct_int/main.c @@ -0,0 +1,9 @@ +#include +#include "ffi_helper.h" + +struct struct_int +{ + int a; +}; + +FFI_API_DECL struct struct_int struct_int; diff --git a/src/c/tests/unions/union_anonymous_char_int/config.json b/src/c/tests/unions/union_anonymous_char_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/unions/union_anonymous_char_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/unions/union_anonymous_char_int/main.c b/src/c/tests/unions/union_anonymous_char_int/main.c new file mode 100644 index 00000000..228920b8 --- /dev/null +++ b/src/c/tests/unions/union_anonymous_char_int/main.c @@ -0,0 +1,12 @@ +#include +#include "ffi_helper.h" + +union union_anonymous_char_int +{ + union { + char a; + int b; + }; +}; + +FFI_API_DECL union union_anonymous_char_int union_anonymous_char_int; diff --git a/src/c/tests/unions/union_anonymous_nested/config.json b/src/c/tests/unions/union_anonymous_nested/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/unions/union_anonymous_nested/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/unions/union_anonymous_nested/main.c b/src/c/tests/unions/union_anonymous_nested/main.c new file mode 100644 index 00000000..e497d70d --- /dev/null +++ b/src/c/tests/unions/union_anonymous_nested/main.c @@ -0,0 +1,18 @@ +#include +#include "ffi_helper.h" + +union union_anonymous_nested +{ + union { + union { + char a; + int b; + }; + union { + char c; + int d; + }; + }; +}; + +FFI_API_DECL union union_anonymous_nested union_anonymous_nested; diff --git a/src/c/tests/unions/union_int_int/config.json b/src/c/tests/unions/union_int_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/unions/union_int_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/unions/union_int_int/main.c b/src/c/tests/unions/union_int_int/main.c new file mode 100644 index 00000000..dc87d4fc --- /dev/null +++ b/src/c/tests/unions/union_int_int/main.c @@ -0,0 +1,10 @@ +#include +#include "ffi_helper.h" + +union union_int_int +{ + int a; + int b; +} + +FFI_API_DECL union union_int_int union_int_int; diff --git a/src/c/tests/variables/variable_ignored/config.json b/src/c/tests/variables/variable_ignored/config.json new file mode 100644 index 00000000..bc240fa9 --- /dev/null +++ b/src/c/tests/variables/variable_ignored/config.json @@ -0,0 +1,31 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "ignoredVariables": [ + "variable_not_allowed", + "variable_ignored*" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/variables/variable_ignored/main.c b/src/c/tests/variables/variable_ignored/main.c new file mode 100644 index 00000000..5935485f --- /dev/null +++ b/src/c/tests/variables/variable_ignored/main.c @@ -0,0 +1,8 @@ +#include +#include "ffi_helper.h" + +FFI_API_DECL int variable_allowed; +FFI_API_DECL int variable_not_allowed; + +FFI_API_DECL int variable_ignored_1; +FFI_API_DECL int variable_ignored_2; diff --git a/src/c/tests/variables/variable_int/config.json b/src/c/tests/variables/variable_int/config.json new file mode 100644 index 00000000..1ed6c076 --- /dev/null +++ b/src/c/tests/variables/variable_int/config.json @@ -0,0 +1,27 @@ +{ + "inputFilePath": "./main.c", + "userIncludeDirectories": [ + "../../../production/ffi_helper/include" + ], + "ignoredIncludeFiles": [ + "../../../production/ffi_helper/include/ffi_helper.h" + ], + "targetPlatforms": { + "windows": { + "i686-pc-windows-msvc": {}, + "x86_64-pc-windows-msvc": {}, + "aarch64-pc-windows-msvc": {} + }, + "macos": { + "i686-apple-darwin": {}, + "aarch64-apple-darwin": {}, + "x86_64-apple-darwin": {}, + "aarch64-apple-ios": {} + }, + "linux": { + "i686-unknown-linux-gnu": {}, + "x86_64-unknown-linux-gnu": {}, + "aarch64-unknown-linux-gnu": {} + } + } +} diff --git a/src/c/tests/variables/variable_int/main.c b/src/c/tests/variables/variable_int/main.c new file mode 100644 index 00000000..152d3c64 --- /dev/null +++ b/src/c/tests/variables/variable_int/main.c @@ -0,0 +1,4 @@ +#include +#include "ffi_helper.h" + +FFI_API_DECL int variable_int; diff --git a/src/cs/production/C2CS.Tool/C2CS.Tool.csproj b/src/cs/production/C2CS.Tool/C2CS.Tool.csproj index 3c4b1b19..345023d3 100644 --- a/src/cs/production/C2CS.Tool/C2CS.Tool.csproj +++ b/src/cs/production/C2CS.Tool/C2CS.Tool.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs index 6a587a96..93bb1ada 100644 --- a/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/BuildCLibraryTool.cs @@ -29,6 +29,11 @@ public BuildCLibraryTool( _cMakeLibraryBuilder = cMakeLibraryBuilder; } + public new BuildCLibraryOutput Run(string configurationFilePath) + { + return base.Run(configurationFilePath); + } + protected override void Execute( BuildCLibraryInput input, BuildCLibraryOutput output) diff --git a/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs index fef22a4a..599bbcb2 100644 --- a/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs +++ b/src/cs/production/C2CS.Tool/Commands/BuildCLibrary/Domain/CMakeLibraryBuilder.cs @@ -9,6 +9,7 @@ using System.Linq; using bottlenoselabs.Common; using C2CS.Commands.BuildCLibrary.Input.Sanitized; +using c2ffi.Data; using Microsoft.Extensions.Logging; namespace C2CS.Commands.BuildCLibrary.Domain; diff --git a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs index 1ef45464..ac9a2bda 100644 --- a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Data/CSharpAbstractSyntaxTree.cs @@ -3,13 +3,12 @@ using System.Collections.Immutable; using bottlenoselabs.Common; +using c2ffi.Data; namespace C2CS.Commands.WriteCodeCSharp.Data; public sealed record CSharpAbstractSyntaxTree { - public ImmutableArray Platforms { get; init; } - public ImmutableArray Functions { get; init; } public ImmutableArray FunctionPointers { get; init; } diff --git a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs index bf86cf7c..9732ad4a 100644 --- a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/Domain/Mapper/CSharpCodeMapper.cs @@ -368,15 +368,8 @@ private ImmutableArray Structs( foreach (var record in records) { - if (_builtinAliases.Contains(record.Name) || - _ignoredNames.Contains(record.Name)) - { - // short circuit, prevents emitting the type - continue; - } - var value = Struct(context, record, functionNames); - if (_ignoredNames.Contains(value.Name)) + if (value == null) { continue; } @@ -388,12 +381,18 @@ private ImmutableArray Structs( return builder.ToImmutable(); } - private CSharpStruct Struct( + private CSharpStruct? Struct( CSharpCodeMapperContext context, CRecord cRecord, ImmutableHashSet functionNames) { - var name = cRecord.Name; + var name = TypeNameCSharpRaw(cRecord.Name, 0); + + if (IsIgnored(name)) + { + return null; + } + if (functionNames.Contains(cRecord.Name)) { name = cRecord.Name + "_"; @@ -482,25 +481,29 @@ private ImmutableArray OpaqueStructs( // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator foreach (var opaqueDataTypeC in cOpaqueDataTypes) { - var value = OpaqueDataStruct(opaqueDataTypeC); - - if (_ignoredNames.Contains(value.Name)) + var opaqueStruct = OpaqueStruct(opaqueDataTypeC); + if (opaqueStruct == null) { continue; } - builder.Add(value); + builder.Add(opaqueStruct); } builder.Sort(); return builder.ToImmutable(); } - private CSharpOpaqueType OpaqueDataStruct(COpaqueType cOpaqueType) + private CSharpOpaqueType? OpaqueStruct(COpaqueType cOpaqueType) { - var nameCSharp = TypeNameCSharpRaw(cOpaqueType.Name, 0); + var name = TypeNameCSharpRaw(cOpaqueType.Name, 0); + if (IsIgnored(name)) + { + return null; + } + var className = ClassName( - nameCSharp, out var nameCSharpMapped); + name, out var nameCSharpMapped); var nameCSharpFinal = IdiomaticName(nameCSharpMapped, false); var opaqueTypeCSharp = new CSharpOpaqueType( @@ -519,36 +522,36 @@ private ImmutableArray AliasStructs( // ReSharper disable once ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator foreach (var typedef in typedefs) { - if (IsIgnored(typedef.Name)) - { - continue; - } - - if (context.EnumNames.Contains(typedef.Name)) - { - // Skip if there is already an enum with the same name - continue; - } - - var value = AliasStruct(context, typedef); - if (_ignoredNames.Contains(value.Name)) + var aliasStruct = AliasStruct(context, typedef); + if (aliasStruct == null) { continue; } - builder.Add(value); + builder.Add(aliasStruct); } builder.Sort(); return builder.ToImmutable(); } - private CSharpAliasType AliasStruct( + private CSharpAliasType? AliasStruct( CSharpCodeMapperContext context, CTypeAlias cTypeAlias) { + if (IsIgnored(cTypeAlias.Name)) + { + return null; + } + var underlyingTypeC = cTypeAlias.UnderlyingType; var underlyingTypeCSharp = TypeCSharp(context, underlyingTypeC); + + if (underlyingTypeCSharp.Name == cTypeAlias.Name) + { + return null; + } + var className = ClassName( cTypeAlias.Name, out var cSharpNameMapped); @@ -558,7 +561,7 @@ private CSharpAliasType AliasStruct( cSharpNameFinal, className, cTypeAlias.Name, - underlyingTypeC.SizeOf, + underlyingTypeC.SizeOf ?? 0, underlyingTypeCSharp); return result; @@ -746,7 +749,6 @@ private CSharpType TypeCSharp( CType type) { var nameCSharp = TypeNameCSharp(context, type); - var className = ClassName(nameCSharp, out var nameCSharpMapped); var nameCSharpFinal = IdiomaticName(nameCSharpMapped, true); @@ -756,7 +758,7 @@ private CSharpType TypeCSharp( ClassName = className, OriginalName = type.Name, SizeOf = type.SizeOf ?? 0, - AlignOf = type.AlignOf, + AlignOf = type.AlignOf ?? 0, ArraySizeOf = type.ArraySizeOf }; @@ -1015,8 +1017,23 @@ private string TypeNameCSharpPointer(string typeName, CType? innerType) return result; } - private string TypeNameCSharpRaw(string typeName, int sizeOf, bool forceUnsignedInteger = false) + private string TypeNameCSharpRaw(string typeName, int? sizeOf = null, bool forceUnsignedInteger = false) { + if (typeName.StartsWith("struct ", StringComparison.InvariantCulture)) + { + typeName = typeName.ReplaceFirst("struct ", string.Empty, StringComparison.InvariantCulture); + } + + if (typeName.StartsWith("union ", StringComparison.InvariantCulture)) + { + typeName = typeName.ReplaceFirst("union ", string.Empty, StringComparison.InvariantCulture); + } + + if (typeName.StartsWith("enum ", StringComparison.InvariantCulture)) + { + typeName = typeName.ReplaceFirst("enum ", string.Empty, StringComparison.InvariantCulture); + } + if (_userTypeNameAliases.TryGetValue(typeName, out var aliasName)) { return aliasName; @@ -1065,7 +1082,7 @@ private string TypeNameCSharpRaw(string typeName, int sizeOf, bool forceUnsigned case "unsigned long long": case "unsigned long long int": case "size_t": - return TypeNameMapUnsignedInteger(sizeOf); + return TypeNameMapUnsignedInteger(sizeOf!.Value); case "signed char": case "short": @@ -1083,12 +1100,17 @@ private string TypeNameCSharpRaw(string typeName, int sizeOf, bool forceUnsigned case "long long int": case "signed long long int": case "ssize_t": - return forceUnsignedInteger ? TypeNameMapUnsignedInteger(sizeOf) : TypeNameMapSignedInteger(sizeOf); + if (forceUnsignedInteger) + { + return TypeNameMapUnsignedInteger(sizeOf!.Value); + } + + return TypeNameMapSignedInteger(sizeOf!.Value); case "float": case "double": case "long double": - return TypeNameMapFloatingPoint(sizeOf); + return TypeNameMapFloatingPoint(sizeOf!.Value); default: return typeName; diff --git a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs index 283c0610..b8d65ee6 100644 --- a/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs +++ b/src/cs/production/C2CS.Tool/Commands/WriteCodeCSharp/WriteCodeCSharpTool.cs @@ -35,6 +35,11 @@ public WriteCodeCSharpTool( _fileSystem = fileSystem; } + public new WriteCodeCSharpOutput Run(string configurationFilePath) + { + return base.Run(configurationFilePath); + } + protected override void Execute(WriteCodeCSharpInput input, WriteCodeCSharpOutput output) { var abstractSyntaxTreesC = LoadCFfi(input.InputFilePath); diff --git a/src/cs/tests/C2CS.Tests/C2CS.Tests.csproj b/src/cs/tests/C2CS.Tests/C2CS.Tests.csproj index 363c2703..40e67cf6 100644 --- a/src/cs/tests/C2CS.Tests/C2CS.Tests.csproj +++ b/src/cs/tests/C2CS.Tests/C2CS.Tests.csproj @@ -52,5 +52,8 @@ + + + diff --git a/src/cs/tests/C2CS.Tests/Data/Models/TestCSharpCodeAbstractSyntaxTree.cs b/src/cs/tests/C2CS.Tests/Data/Models/TestCSharpCodeAbstractSyntaxTree.cs deleted file mode 100644 index 4f280325..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Models/TestCSharpCodeAbstractSyntaxTree.cs +++ /dev/null @@ -1,26 +0,0 @@ -// 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; - -namespace C2CS.Tests.Data.Models; - -public sealed class TestCSharpCodeAbstractSyntaxTree -{ - public readonly ImmutableDictionary Enums; - public readonly ImmutableDictionary Methods; - public readonly ImmutableDictionary MacroObjects; - public readonly ImmutableDictionary Structs; - - public TestCSharpCodeAbstractSyntaxTree( - ImmutableDictionary enums, - ImmutableDictionary methods, - ImmutableDictionary macroObjects, - ImmutableDictionary structs) - { - Enums = enums; - Methods = methods; - MacroObjects = macroObjects; - Structs = structs; - } -} diff --git a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt16.json b/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt16.json deleted file mode 100644 index 37f3dce4..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt16.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "EnumForceSInt16", - "base_type": "int", - "members": [ - { - "name": "ENUM_FORCE_SINT16_DAY_UNKNOWN", - "value": "0" - }, - { - "name": "ENUM_FORCE_SINT16_DAY_MONDAY", - "value": "1" - }, - { - "name": "ENUM_FORCE_SINT16_DAY_TUESDAY", - "value": "2" - }, - { - "name": "ENUM_FORCE_SINT16_DAY_WEDNESDAY", - "value": "3" - }, - { - "name": "ENUM_FORCE_SINT16_DAY_THURSDAY", - "value": "4" - }, - { - "name": "ENUM_FORCE_SINT16_DAY_FRIDAY", - "value": "5" - }, - { - "name": "_ENUM_FORCE_SINT16", - "value": "32767" - } - ] -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt32.json b/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt32.json deleted file mode 100644 index edb13bf4..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt32.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "EnumForceSInt32", - "base_type": "int", - "members": [ - { - "name": "ENUM_FORCE_SINT32_DAY_UNKNOWN", - "value": "0" - }, - { - "name": "ENUM_FORCE_SINT32_DAY_MONDAY", - "value": "1" - }, - { - "name": "ENUM_FORCE_SINT32_DAY_TUESDAY", - "value": "2" - }, - { - "name": "ENUM_FORCE_SINT32_DAY_WEDNESDAY", - "value": "3" - }, - { - "name": "ENUM_FORCE_SINT32_DAY_THURSDAY", - "value": "4" - }, - { - "name": "ENUM_FORCE_SINT32_DAY_FRIDAY", - "value": "5" - }, - { - "name": "_ENUM_FORCE_SINT32", - "value": "2147483647" - } - ] -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt8.json b/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt8.json deleted file mode 100644 index 5ae63293..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceSInt8.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "EnumForceSInt8", - "base_type": "int", - "members": [ - { - "name": "ENUM_FORCE_SINT8_DAY_UNKNOWN", - "value": "0" - }, - { - "name": "ENUM_FORCE_SINT8_DAY_MONDAY", - "value": "1" - }, - { - "name": "ENUM_FORCE_SINT8_DAY_TUESDAY", - "value": "2" - }, - { - "name": "ENUM_FORCE_SINT8_DAY_WEDNESDAY", - "value": "3" - }, - { - "name": "ENUM_FORCE_SINT8_DAY_THURSDAY", - "value": "4" - }, - { - "name": "ENUM_FORCE_SINT8_DAY_FRIDAY", - "value": "5" - }, - { - "name": "_ENUM_FORCE_SINT8", - "value": "127" - } - ] -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt16.json b/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt16.json deleted file mode 100644 index 2c2c9603..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt16.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "EnumForceUInt16", - "base_type": "int", - "members": [ - { - "name": "ENUM_FORCE_UINT16_DAY_UNKNOWN", - "value": "0" - }, - { - "name": "ENUM_FORCE_UINT16_DAY_MONDAY", - "value": "1" - }, - { - "name": "ENUM_FORCE_UINT16_DAY_TUESDAY", - "value": "2" - }, - { - "name": "ENUM_FORCE_UINT16_DAY_WEDNESDAY", - "value": "3" - }, - { - "name": "ENUM_FORCE_UINT16_DAY_THURSDAY", - "value": "4" - }, - { - "name": "ENUM_FORCE_UINT16_DAY_FRIDAY", - "value": "5" - }, - { - "name": "_ENUM_FORCE_UINT16", - "value": "65535" - } - ] -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt32.json b/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt32.json deleted file mode 100644 index 381d55b8..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt32.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "EnumForceUInt32", - "base_type": "int", - "members": [ - { - "name": "ENUM_FORCE_UINT32_DAY_UNKNOWN", - "value": "0" - }, - { - "name": "ENUM_FORCE_UINT32_DAY_MONDAY", - "value": "1" - }, - { - "name": "ENUM_FORCE_UINT32_DAY_TUESDAY", - "value": "2" - }, - { - "name": "ENUM_FORCE_UINT32_DAY_WEDNESDAY", - "value": "3" - }, - { - "name": "ENUM_FORCE_UINT32_DAY_THURSDAY", - "value": "4" - }, - { - "name": "ENUM_FORCE_UINT32_DAY_FRIDAY", - "value": "5" - }, - { - "name": "_ENUM_FORCE_UINT32", - "value": "-1" - } - ] -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt8.json b/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt8.json deleted file mode 100644 index d3824597..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/Enums/EnumForceUInt8.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "EnumForceUInt8", - "base_type": "int", - "members": [ - { - "name": "ENUM_FORCE_UINT8_DAY_UNKNOWN", - "value": "0" - }, - { - "name": "ENUM_FORCE_UINT8_DAY_MONDAY", - "value": "1" - }, - { - "name": "ENUM_FORCE_UINT8_DAY_TUESDAY", - "value": "2" - }, - { - "name": "ENUM_FORCE_UINT8_DAY_WEDNESDAY", - "value": "3" - }, - { - "name": "ENUM_FORCE_UINT8_DAY_THURSDAY", - "value": "4" - }, - { - "name": "ENUM_FORCE_UINT8_DAY_FRIDAY", - "value": "5" - }, - { - "name": "_ENUM_FORCE_UINT8", - "value": "255" - } - ] -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Data/Values/MacroObjects/MacroObjectIntValue.json b/src/cs/tests/C2CS.Tests/Data/Values/MacroObjects/MacroObjectIntValue.json deleted file mode 100644 index 68d94880..00000000 --- a/src/cs/tests/C2CS.Tests/Data/Values/MacroObjects/MacroObjectIntValue.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "MACRO_OBJECT_INT_VALUE", - "type_name": "int", - "value": "42" -} \ No newline at end of file diff --git a/src/cs/tests/C2CS.Tests/Functions/function_void/Test.cs b/src/cs/tests/C2CS.Tests/Functions/function_void/Test.cs new file mode 100644 index 00000000..5080ab3a --- /dev/null +++ b/src/cs/tests/C2CS.Tests/Functions/function_void/Test.cs @@ -0,0 +1,30 @@ +// 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 C2CS.Tests.Models; +using FluentAssertions; +using Xunit; + +#pragma warning disable CA1707 + +namespace C2CS.Tests.Functions.function_void; + +public class Test : WriteCSharpCodeTest +{ + private const string FunctionName = "function_void"; + + [Fact] + public void Function() + { + var ast = GetCSharpAbstractSyntaxTree( + $"src/c/tests/functions/{FunctionName}"); + FunctionExists(ast); + } + + private void FunctionExists(CSharpTestAbstractSyntaxTree ast) + { + var function = ast.GetFunction(FunctionName); + function.ReturnTypeName.Should().Be("void"); + function.Parameters.Should().BeEmpty(); + } +} diff --git a/src/cs/tests/C2CS.Tests/Helpers/FileSystemHelper.cs b/src/cs/tests/C2CS.Tests/Helpers/FileSystemHelper.cs new file mode 100644 index 00000000..21cd1eba --- /dev/null +++ b/src/cs/tests/C2CS.Tests/Helpers/FileSystemHelper.cs @@ -0,0 +1,68 @@ +// 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; +using System.Diagnostics.CodeAnalysis; +using System.IO.Abstractions; +using SearchOption = System.IO.SearchOption; + +namespace C2CS.Tests.Helpers; + +[ExcludeFromCodeCoverage] +public class FileSystemHelper +{ + private readonly IFileSystem _fileSystem; + private string? _gitRepositoryRootDirectoryPath; + + public string GitRepositoryRootDirectoryPath => _gitRepositoryRootDirectoryPath ??= FindGitRepositoryRootDirectoryPath(); + + public FileSystemHelper(IFileSystem fileSystem) + { + _fileSystem = fileSystem; + } + + public string GetFullFilePath(string relativeFilePath) + { + var rootDirectoryPath = GitRepositoryRootDirectoryPath; + var filePath = _fileSystem.Path.Combine(rootDirectoryPath, relativeFilePath); + + if (!_fileSystem.File.Exists(filePath)) + { + throw new InvalidOperationException($"Could not find file path: {filePath}"); + } + + return filePath; + } + + public string GetFullDirectoryPath(string relativeDirectoryPath) + { + var rootDirectoryPath = GitRepositoryRootDirectoryPath; + var directoryPath = _fileSystem.Path.Combine(rootDirectoryPath, relativeDirectoryPath); + if (!_fileSystem.Directory.Exists(directoryPath)) + { + throw new InvalidOperationException($"Could not find directory path: {relativeDirectoryPath}"); + } + + return directoryPath; + } + + private string FindGitRepositoryRootDirectoryPath() + { + var baseDirectory = AppContext.BaseDirectory; + var directoryInfo = _fileSystem.DirectoryInfo.New(baseDirectory); + while (true) + { + var files = directoryInfo.GetFiles(".gitignore", SearchOption.TopDirectoryOnly); + if (files.Length > 0) + { + return directoryInfo.FullName; + } + + directoryInfo = directoryInfo.Parent; + if (directoryInfo == null) + { + return string.Empty; + } + } + } +} diff --git a/src/cs/tests/C2CS.Tests/Models/CSharpTestAbstractSyntaxTree.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestAbstractSyntaxTree.cs new file mode 100644 index 00000000..972bb2dc --- /dev/null +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestAbstractSyntaxTree.cs @@ -0,0 +1,34 @@ +// 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 Xunit; + +namespace C2CS.Tests.Models; + +public sealed class CSharpTestAbstractSyntaxTree +{ + private readonly ImmutableDictionary _enums; + private readonly ImmutableDictionary _functions; + private readonly ImmutableDictionary _macroObjects; + private readonly ImmutableDictionary _structs; + + public CSharpTestAbstractSyntaxTree( + ImmutableDictionary enums, + ImmutableDictionary functions, + ImmutableDictionary macroObjects, + ImmutableDictionary structs) + { + _enums = enums; + _functions = functions; + _macroObjects = macroObjects; + _structs = structs; + } + + public CSharpTestFunction GetFunction(string name) + { + var exists = _functions.TryGetValue(name, out var value); + Assert.True(exists, $"The C# method '{name}' does not exist"); + return value!; + } +} diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestEnum.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestEnum.cs similarity index 95% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestEnum.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestEnum.cs index 8373758e..6139ffb1 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestEnum.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestEnum.cs @@ -5,7 +5,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; #pragma warning disable CA1036 [PublicAPI] diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestEnumMember.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestEnumMember.cs similarity index 94% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestEnumMember.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestEnumMember.cs index 94103c3b..bc3d80d3 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestEnumMember.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestEnumMember.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestEnumMember diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestFunction.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestFunction.cs similarity index 95% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestFunction.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestFunction.cs index 97cdba2e..ed60fea6 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestFunction.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestFunction.cs @@ -5,7 +5,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestFunction diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestFunctionParameter.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestFunctionParameter.cs similarity index 94% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestFunctionParameter.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestFunctionParameter.cs index 290aed53..3ef60d73 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestFunctionParameter.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestFunctionParameter.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestFunctionParameter diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestMacroObject.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestMacroObject.cs similarity index 94% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestMacroObject.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestMacroObject.cs index 4ce20cfb..db870bfa 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestMacroObject.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestMacroObject.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestMacroObject diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStruct.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestStruct.cs similarity index 95% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStruct.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestStruct.cs index cf5bf76f..ac178474 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStruct.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestStruct.cs @@ -5,7 +5,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestStruct diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStructField.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestStructField.cs similarity index 95% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStructField.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestStructField.cs index 89b1a883..23742e9d 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStructField.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestStructField.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestStructField diff --git a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStructLayout.cs b/src/cs/tests/C2CS.Tests/Models/CSharpTestStructLayout.cs similarity index 94% rename from src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStructLayout.cs rename to src/cs/tests/C2CS.Tests/Models/CSharpTestStructLayout.cs index c51cdb3f..a734ddec 100644 --- a/src/cs/tests/C2CS.Tests/Data/Models/CSharpTestStructLayout.cs +++ b/src/cs/tests/C2CS.Tests/Models/CSharpTestStructLayout.cs @@ -4,7 +4,7 @@ using System.Text.Json.Serialization; using JetBrains.Annotations; -namespace C2CS.Tests.Data.Models; +namespace C2CS.Tests.Models; [PublicAPI] public class CSharpTestStructLayout diff --git a/src/cs/tests/C2CS.Tests/TestBase.cs b/src/cs/tests/C2CS.Tests/TestBase.cs deleted file mode 100644 index 7aeeb4e2..00000000 --- a/src/cs/tests/C2CS.Tests/TestBase.cs +++ /dev/null @@ -1,134 +0,0 @@ -// 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; -using System.Globalization; -using System.IO; -using System.IO.Abstractions; -using System.Text.Json; -using System.Text.Json.Serialization; -using FluentAssertions; -using Microsoft.Extensions.DependencyInjection; - -namespace C2CS.Tests; - -public abstract class TestBase -{ - private readonly string _baseDataFilesDirectory; - private readonly IFileSystem _fileSystem; - private readonly JsonSerializerOptions _jsonSerializerOptions; - private readonly bool _regenerateDataFiles; - private readonly string _sourceDirectoryPath; - - protected IServiceProvider Services { get; } - - protected TestBase(string baseDataFilesDirectory, bool regenerateDataFiles = false) - { - _baseDataFilesDirectory = baseDataFilesDirectory; - - Services = TestHost.Services; - - _fileSystem = Services.GetService()!; - var path = _fileSystem.Path; - - _sourceDirectoryPath = path.Combine(GetGitDirectory(), "src", "cs", "tests", "C2CS.Tests"); - _baseDataFilesDirectory = path.Combine(_sourceDirectoryPath, baseDataFilesDirectory); - _regenerateDataFiles = regenerateDataFiles; - - _jsonSerializerOptions = new JsonSerializerOptions - { - WriteIndented = true, - Converters = - { - new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) - }, - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull - }; - } - - protected void AssertValue(string name, T value, string directory) - { - var nameNormalized = name; - if (name.Contains('_', StringComparison.InvariantCulture)) - { -#pragma warning disable CA1308 - var nameAsWords = name.ToLowerInvariant().Replace("_", " ", StringComparison.InvariantCulture); -#pragma warning restore CA1308 - var nameAsWordsTitleCased = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(nameAsWords); - nameNormalized = nameAsWordsTitleCased.Replace(" ", string.Empty, StringComparison.InvariantCulture); - } - - var jsonFilePath = _fileSystem.Path.Combine( - _baseDataFilesDirectory, directory, $"{nameNormalized}.json"); - if (_regenerateDataFiles) - { - RegenerateDataFile(jsonFilePath, value); - } - - var expectedValue = ReadValueFromFile(jsonFilePath); - value.Should().BeEquivalentTo( - expectedValue, - o => o.ComparingByMembers(), - $"because that is what the JSON file has `{jsonFilePath}`"); - } - - private void RegenerateDataFile(string filePath, T value) - { - WriteValueToFile(filePath, value); - } - - private T? ReadValueFromFile(string filePath) - { - var fileContents = File.ReadAllText(filePath); - return JsonSerializer.Deserialize(fileContents, _jsonSerializerOptions); - } - - private void WriteValueToFile(string filePath, T value) - { - var fullFilePath = _fileSystem.Path.GetFullPath(filePath); - - var outputDirectory = _fileSystem.Path.GetDirectoryName(fullFilePath)!; - if (string.IsNullOrEmpty(outputDirectory)) - { - outputDirectory = AppContext.BaseDirectory; - fullFilePath = Path.Combine(Environment.CurrentDirectory, fullFilePath); - } - - if (!_fileSystem.Directory.Exists(outputDirectory)) - { - _fileSystem.Directory.CreateDirectory(outputDirectory); - } - - if (_fileSystem.File.Exists(fullFilePath)) - { - _fileSystem.File.Delete(fullFilePath); - } - - var fileContents = JsonSerializer.Serialize(value, _jsonSerializerOptions); - - using var fileStream = _fileSystem.File.OpenWrite(fullFilePath); - using var textWriter = new StreamWriter(fileStream); - textWriter.Write(fileContents); - textWriter.Close(); - fileStream.Close(); - } - - private static string GetGitDirectory() - { - Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "../../../../src/cs/tests/C2CS.Tests")); - - var currentDirectory = AppContext.BaseDirectory; - while (!string.IsNullOrEmpty(currentDirectory) && Directory.Exists(currentDirectory)) - { - var files = Directory.GetFiles(currentDirectory, "*.gitignore"); - if (files.Length == 1) - { - return currentDirectory; - } - - currentDirectory = Directory.GetParent(currentDirectory)?.FullName ?? string.Empty; - } - - throw new InvalidOperationException("Could not find Git root directory"); - } -} diff --git a/src/cs/tests/C2CS.Tests/TestCSharpCode.cs b/src/cs/tests/C2CS.Tests/TestCSharpCode.cs deleted file mode 100644 index 01724109..00000000 --- a/src/cs/tests/C2CS.Tests/TestCSharpCode.cs +++ /dev/null @@ -1,58 +0,0 @@ -// 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 JetBrains.Annotations; -using Microsoft.Extensions.DependencyInjection; -using Xunit; - -namespace C2CS.Tests; - -[PublicAPI] -public sealed class TestCSharpCode : TestBase -{ - public static TheoryData EnumNames() => new() - { - "EnumForceSInt8", - "EnumForceSInt16", - "EnumForceSInt32", - "EnumForceUInt8", - "EnumForceUInt16", - "EnumForceUInt32", - }; - - [Theory] - [MemberData(nameof(EnumNames))] - public void Enum(string name) - { - var value = _fixture.GetEnum(name); - AssertValue(name, value, "Enums"); - } - - public static TheoryData MacroObjectNames() => new() - { - "MACRO_OBJECT_INT_VALUE" - }; - - [Theory] - [MemberData(nameof(MacroObjectNames))] - public void MacroObject(string name) - { - var value = _fixture.GetMacroObject(name); - AssertValue(name, value, "MacroObjects"); - } - - [Fact] - public void Compiles() - { - _fixture.AssertCSharpCodeCompiles(_fixture.Output); - } - - private readonly TestFixtureCSharpCode _fixture; - - public TestCSharpCode() - : base("Data/Values", regenerateDataFiles: false) - { - var services = TestHost.Services; - _fixture = services.GetService()!; - } -} diff --git a/src/cs/tests/C2CS.Tests/TestHost.cs b/src/cs/tests/C2CS.Tests/TestHost.cs index c9dcdec2..201f7c6c 100644 --- a/src/cs/tests/C2CS.Tests/TestHost.cs +++ b/src/cs/tests/C2CS.Tests/TestHost.cs @@ -3,6 +3,7 @@ using System; using C2CS.Commands.BuildCLibrary.Domain; +using C2CS.Tests.Helpers; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -25,8 +26,7 @@ private static IHostBuilder HostBuilder() private static void ConfigureServices(IServiceCollection services) { + services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); - services.AddSingleton(); } } diff --git a/src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs b/src/cs/tests/C2CS.Tests/WriteCSharpCodeTest.cs similarity index 61% rename from src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs rename to src/cs/tests/C2CS.Tests/WriteCSharpCodeTest.cs index 45888719..f2da5269 100644 --- a/src/cs/tests/C2CS.Tests/TestFixtureCSharpCode.cs +++ b/src/cs/tests/C2CS.Tests/WriteCSharpCodeTest.cs @@ -3,16 +3,17 @@ using System; using System.Collections.Immutable; +using System.Diagnostics.CodeAnalysis; using System.Globalization; -using System.IO; -using System.Reflection; -using System.Runtime.InteropServices; +using System.IO.Abstractions; using bottlenoselabs.Common; using C2CS.Commands.BuildCLibrary.Domain; using C2CS.Commands.BuildCLibrary.Input.Sanitized; using C2CS.Commands.WriteCodeCSharp; using C2CS.Commands.WriteCodeCSharp.Output; -using C2CS.Tests.Data.Models; +using C2CS.Tests.Helpers; +using C2CS.Tests.Models; +using JetBrains.Annotations; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.DependencyInjection; @@ -20,36 +21,38 @@ namespace C2CS.Tests; -public sealed class TestFixtureCSharpCode +[PublicAPI] +[ExcludeFromCodeCoverage] +public abstract class WriteCSharpCodeTest { - private readonly TestCSharpCodeAbstractSyntaxTree _abstractSyntaxTree; - private const string ClassName = "bottlenoselabs._container_library"; - private readonly Type? _classType; + private readonly IFileSystem _fileSystem; + private readonly FileSystemHelper _fileSystemHelper; + private readonly WriteCodeCSharpTool _tool; - public WriteCodeCSharpOutput Output { get; } - - public TestFixtureCSharpCode() + protected WriteCSharpCodeTest() { - var sourceDirectoryPath = GetSourceDirectoryPath(); - var bindgenConfigFilePath = Path.Combine(sourceDirectoryPath, "config-extract.json"); - GenerateCAbstractSyntaxTree(bindgenConfigFilePath, sourceDirectoryPath); - - BuildCLibrary(sourceDirectoryPath, ImmutableArray.Empty); - Output = Run(sourceDirectoryPath); - Assert.True(Output.IsSuccess); - _abstractSyntaxTree = CreateCSharpAbstractSyntaxTree(Output); + var services = TestHost.Services; + _fileSystem = services.GetService()!; + _fileSystemHelper = services.GetService()!; + _tool = services.GetService()!; + } - var assembly = Output.Assembly; - if (assembly != null) - { - _classType = assembly.GetType(ClassName); - NativeLibrary.SetDllImportResolver(assembly, NativeLibraryResolver); - } + public CSharpTestAbstractSyntaxTree GetCSharpAbstractSyntaxTree(string directoryPath) + { + var fullDirectoryPath = _fileSystemHelper.GetFullDirectoryPath(directoryPath); + BuildCLibrary(fullDirectoryPath); + GenerateCrossPlatformFfi(fullDirectoryPath); + var cSharpConfigFilePath = _fileSystem.Path.Combine(fullDirectoryPath, "config-generate-cs.json"); + var output = _tool.Run(cSharpConfigFilePath); + Assert.True(output.IsSuccess); + var ast = CreateCSharpAbstractSyntaxTree(output); + AssertCSharpCodeCompiles(output); + return ast; } public void AssertCSharpCodeCompiles(WriteCodeCSharpOutput output) { - if (!Output.Input.GeneratorOptions.IsEnabledVerifyCSharpCodeCompiles) + if (!output.Input.GeneratorOptions.IsEnabledVerifyCSharpCodeCompiles) { return; } @@ -57,121 +60,40 @@ public void AssertCSharpCodeCompiles(WriteCodeCSharpOutput output) Assert.True(output.Assembly != null, "Error compiling generated C# code."); } - public CSharpTestEnum GetEnum(string name) - { - var exists = _abstractSyntaxTree.Enums.TryGetValue(name, out var value); - Assert.True(exists, $"The enum `{name}` does not exist."); - - Assert.True(_classType != null, $"The class `{ClassName}` does not exist."); - var enumType = _classType!.GetNestedType(name); - Assert.True(enumType != null, $"The enum type `{name}` does not exist."); - var enumValues = enumType!.GetEnumValues(); - - AssertPInvokeEnum(name, enumValues); - - return value!; - } - - public CSharpTestFunction GetFunction(string name) - { - var exists = _abstractSyntaxTree.Methods.TryGetValue(name, out var value); - Assert.True(exists, $"The function `{name}` does not exist."); - return value!; - } - - public CSharpTestStruct GetStruct(string name) - { - var exists = _abstractSyntaxTree.Structs.TryGetValue(name, out var value); - Assert.True(exists, $"The struct `{name}` does not exist."); - return value!; - } - - public CSharpTestMacroObject GetMacroObject(string name) - { - var exists = _abstractSyntaxTree.MacroObjects.TryGetValue(name, out var value); - Assert.True(exists, $"The macro object `{name}` does not exist."); - return value!; - } - - private static void BuildCLibrary(string sourceDirectoryPath, ImmutableArray additionalCMakeArguments) + private static void BuildCLibrary( + string cMakeDirectoryPath, ImmutableArray? additionalCMakeArguments = null) { var services = TestHost.Services; var cmakeLibraryBuilder = services.GetService()!; - var cMakeDirectoryPath = - Path.GetFullPath(Path.Combine(sourceDirectoryPath, "..", "..", "..", "..", "src", "c", "tests", "_container_library")); - var input = new BuildCLibraryInput { CMakeDirectoryPath = cMakeDirectoryPath, OutputDirectoryPath = AppContext.BaseDirectory }; - var result = cmakeLibraryBuilder.BuildLibrary(input, additionalCMakeArguments); + var result = cmakeLibraryBuilder.BuildLibrary(input, additionalCMakeArguments ?? ImmutableArray.Empty); Assert.True(result, "Failed to build C library."); } - private static void GenerateCAbstractSyntaxTree( - string bindgenConfigFilePath, - string sourceDirectoryPath) + private void GenerateCrossPlatformFfi(string fullDirectoryPath) { - var extractShellOutput = $"c2ffi extract --config {bindgenConfigFilePath}".ExecuteShellCommand(); + var extractConfigFilePath = _fileSystem.Path.Combine(fullDirectoryPath, "config-extract.json"); + var extractShellOutput = $"c2ffi extract --config {extractConfigFilePath}".ExecuteShellCommand(); Assert.True(extractShellOutput.ExitCode == 0, "error extracting platform FFIs"); - - var ffiDirectoryPath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ffi")); - var mergedFffFilePath = - Path.GetFullPath(Path.Combine(sourceDirectoryPath, "ffi-x", "cross-platform.json")); + var ffiDirectoryPath = _fileSystem.Path.Combine(fullDirectoryPath, "ffi"); + var crossFfiFilePath = _fileSystem.Path.Combine(fullDirectoryPath, "ffi-x", "cross-platform.json"); var ffiShellOutput = - $"c2ffi merge --inputDirectoryPath {ffiDirectoryPath} --outputFilePath {mergedFffFilePath}" + $"c2ffi merge --inputDirectoryPath {ffiDirectoryPath} --outputFilePath {crossFfiFilePath}" .ExecuteShellCommand(); - Assert.True(ffiShellOutput.ExitCode == 0, "error merging platform ASTs"); - } - - private WriteCodeCSharpOutput Run(string sourceDirectoryPath) - { - var services = TestHost.Services; - var writeCodeCSharpTool = services.GetService()!; - - var configGenerateCSharpCodeFilePath = Path.GetFullPath(Path.Combine(sourceDirectoryPath, "config-cs.json")); - var outputWriteCSharp = writeCodeCSharpTool.Run(configGenerateCSharpCodeFilePath); - Assert.True(outputWriteCSharp != null); - Assert.True(outputWriteCSharp!.Diagnostics.Length == 0, $"Diagnostics were reported when writing C# code: {outputWriteCSharp.OutputFileDirectory}"); - Assert.True(outputWriteCSharp.IsSuccess, "Writing C# code failed."); - return outputWriteCSharp; + Assert.True(ffiShellOutput.ExitCode == 0, "error merging platform FFIs"); } - private void AssertPInvokeEnum(string name, Array enumValues) - { - // TODO: Inter process communication - - var enumPrintMethodName = $"{name}__print_{name}"; - Assert.True(_classType != null, $"The class `{ClassName}` does not exist."); - var enumPrintMethod = _classType!.GetMethod(enumPrintMethodName); - Assert.True(enumPrintMethod != null, $"The enum method `{enumPrintMethodName}` does not exist."); - foreach (var enumValue in enumValues) - { - var enumPrintMethodResult = enumPrintMethod!.Invoke(null, new[] { enumValue }); - Assert.True(enumPrintMethodResult == null, $"Unexpected result from enum print method `{enumPrintMethodName}`"); - } - - var enumReturnMethodName = $"{name}__return_{name}"; - var enumReturnMethod = _classType.GetMethod(enumReturnMethodName); - Assert.True(enumReturnMethod != null, $"The enum method `{enumReturnMethodName}` does not exist."); - - foreach (var enumValue in enumValues) - { - var enumReturnMethodResult = enumReturnMethod!.Invoke(null, new[] { enumValue }); - Assert.True( - enumReturnMethodResult!.Equals(enumValue), - $"Unexpected result from enum return method `{enumReturnMethodName}`"); - } - } - - private TestCSharpCodeAbstractSyntaxTree CreateCSharpAbstractSyntaxTree( + private CSharpTestAbstractSyntaxTree CreateCSharpAbstractSyntaxTree( WriteCodeCSharpOutput output) { var codeFilePath = - Path.Combine(output.OutputFileDirectory, $"{output.Input.GeneratorOptions.ClassName}.gen.cs"); - var code = File.ReadAllText(codeFilePath); + _fileSystem.Path.Combine(output.OutputFileDirectory, $"{output.Input.GeneratorOptions.ClassName}.gen.cs"); + var code = _fileSystem.File.ReadAllText(codeFilePath); var syntaxTree = CSharpSyntaxTree.ParseText(code); var compilationUnitSyntax = syntaxTree.GetCompilationUnitRoot(); var generatorOptions = output.Input.GeneratorOptions; @@ -186,7 +108,7 @@ private TestCSharpCodeAbstractSyntaxTree CreateCSharpAbstractSyntaxTree( Assert.True(@class != null); Assert.True(@class!.Identifier.ToString() == generatorOptions.ClassName); - var methodsByNameBuilder = ImmutableDictionary.CreateBuilder(); + var functionsByNameBuilder = ImmutableDictionary.CreateBuilder(); var enumsByNameBuilder = ImmutableDictionary.CreateBuilder(); var structsByNameBuilder = ImmutableDictionary.CreateBuilder(); var macroObjectsByNameBuilder = ImmutableDictionary.CreateBuilder(); @@ -195,38 +117,20 @@ private TestCSharpCodeAbstractSyntaxTree CreateCSharpAbstractSyntaxTree( { CreateTestNode( member, - methodsByNameBuilder, + functionsByNameBuilder, enumsByNameBuilder, structsByNameBuilder, macroObjectsByNameBuilder); } - var ast = new TestCSharpCodeAbstractSyntaxTree( + var ast = new CSharpTestAbstractSyntaxTree( enumsByNameBuilder.ToImmutable(), - methodsByNameBuilder.ToImmutable(), + functionsByNameBuilder.ToImmutable(), macroObjectsByNameBuilder.ToImmutable(), structsByNameBuilder.ToImmutable()); return ast; } - private static nint NativeLibraryResolver( - string libraryName, - Assembly assembly, - DllImportSearchPath? searchPath) - { - var fileName = Native.OperatingSystem switch - { - NativeOperatingSystem.Windows => "_container_library.dll", - NativeOperatingSystem.macOS => "lib_container_library.dylib", - NativeOperatingSystem.Linux => "lib_container_library.so", - _ => throw new NotImplementedException() - }; - - var filePath = Path.Combine(AppContext.BaseDirectory, fileName); - var handle = NativeLibrary.Load(filePath); - return handle; - } - private void CreateTestNode( MemberDeclarationSyntax member, ImmutableDictionary.Builder methodsByNameBuilder, @@ -497,32 +401,4 @@ private AttributeSyntax GetAttribute(string name, MemberDeclarationSyntax syntax return result!; } - - private static string GetSourceDirectoryPath() - { - var gitRepositoryPath = GetGitRepositoryPath(); - var sourceDirectoryPath = - Path.GetFullPath(Path.Combine(gitRepositoryPath, "src", "cs", "tests", "C2CS.Tests")); - return sourceDirectoryPath; - } - - private static string GetGitRepositoryPath() - { - var baseDirectory = AppContext.BaseDirectory; - var directoryInfo = new DirectoryInfo(baseDirectory); - while (true) - { - var files = directoryInfo.GetFiles(".gitignore", SearchOption.TopDirectoryOnly); - if (files.Length > 0) - { - return directoryInfo.FullName; - } - - directoryInfo = directoryInfo.Parent; - if (directoryInfo == null) - { - return string.Empty; - } - } - } } diff --git a/src/cs/tests/C2CS.Tests/appsettings.json b/src/cs/tests/C2CS.Tests/appsettings.json deleted file mode 100644 index 6c46bbfc..00000000 --- a/src/cs/tests/C2CS.Tests/appsettings.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "Logging": { - "Console": { - "LogLevel": { - "Default": "Warning", - "C2CS": "Information" - }, - "FormatterOptions": { - "ColorBehavior": "Disabled", - "SingleLine": true, - "IncludeScopes": true, - "TimestampFormat": "yyyy-dd-MM HH:mm:ss ", - "UseUtcTimestamp": true - } - } - } -} diff --git a/src/cs/tests/C2CS.Tests/config-cs.json b/src/cs/tests/C2CS.Tests/config-cs.json deleted file mode 100644 index c84ae6b3..00000000 --- a/src/cs/tests/C2CS.Tests/config-cs.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "inputFilePath": "./ast/cross-platform.json", - "outputFileDirectory": "./Generated", - "className": "_container_library", - "libraryName": "_container_library", - "namespaceName": "bottlenoselabs" -}