Browse Source

Add option to conditionally generate bindings

pg
josetr 3 years ago
parent
commit
7a3e5e9144
  1. 83
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 2
      src/Generator/Options.cs

83
src/Generator/Generators/CSharp/CSharpSources.cs

@ -113,8 +113,8 @@ namespace CppSharp.Generators.CSharp
group spec by module.OutputNamespace into @group group spec by module.OutputNamespace into @group
select @group) select @group)
{ {
using (!string.IsNullOrEmpty(group.Key) using (!string.IsNullOrEmpty(group.Key)
? PushWriteBlock(BlockKind.Namespace, $"namespace {group.Key}", NewLineKind.BeforeNextBlock) ? PushWriteBlock(BlockKind.Namespace, $"namespace {group.Key}", NewLineKind.BeforeNextBlock)
: default) : default)
{ {
foreach (var template in from s in @group foreach (var template in from s in @group
@ -188,8 +188,8 @@ namespace CppSharp.Generators.CSharp
var shouldGenerateNamespace = !@namespace.IsInline && !isTranslationUnit && var shouldGenerateNamespace = !@namespace.IsInline && !isTranslationUnit &&
context.Declarations.Any(d => d.IsGenerated || (d is Class && !d.IsIncomplete)); context.Declarations.Any(d => d.IsGenerated || (d is Class && !d.IsIncomplete));
using var _ = shouldGenerateNamespace using var _ = shouldGenerateNamespace
? PushWriteBlock(BlockKind.Namespace, $"namespace {context.Name}", NewLineKind.BeforeNextBlock) ? PushWriteBlock(BlockKind.Namespace, $"namespace {context.Name}", NewLineKind.BeforeNextBlock)
: default; : default;
return base.VisitNamespace(@namespace); return base.VisitNamespace(@namespace);
@ -253,30 +253,40 @@ namespace CppSharp.Generators.CSharp
var classes = EnumerateClasses().ToList(); var classes = EnumerateClasses().ToList();
if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any()) if (classes.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName).Any())
keyword = "struct"; keyword = "struct";
WriteLine($"public unsafe partial {keyword} {parentName}");
WriteOpenBraceAndIndent();
PushBlock(BlockKind.InternalsClass); if (Options.GenerateTypesOnly && Options.PutAllGlobalsInGlobalClass)
GenerateClassInternalHead(new Class { Name = parentName }); parentName = "Globals";
WriteLine($"public unsafe partial {keyword} {parentName}");
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
// Generate all the internal function declarations. if (Options.GenerateBindings)
foreach (var function in context.Functions)
{ {
if ((!function.IsGenerated && !function.IsInternal) || function.IsSynthetized) PushBlock(BlockKind.InternalsClass);
continue; GenerateClassInternalHead(new Class { Name = parentName });
WriteOpenBraceAndIndent();
GenerateInternalFunction(function); if (Options.GenerateBindings)
} {
// Generate all the internal function declarations.
foreach (var function in context.Functions)
{
if ((!function.IsGenerated && !function.IsInternal) || function.IsSynthetized)
continue;
UnindentAndWriteCloseBrace(); GenerateInternalFunction(function);
PopBlock(NewLineKind.BeforeNextBlock); }
}
foreach (var function in context.Functions) UnindentAndWriteCloseBrace();
{ PopBlock(NewLineKind.BeforeNextBlock);
if (!function.IsGenerated) continue;
foreach (var function in context.Functions)
{
if (!function.IsGenerated) continue;
GenerateFunction(function, parentName); GenerateFunction(function, parentName);
}
} }
foreach (var variable in context.Variables.Where( foreach (var variable in context.Variables.Where(
@ -409,7 +419,7 @@ namespace CppSharp.Generators.CSharp
if (!@class.IsGenerated) if (!@class.IsGenerated)
goto exit; goto exit;
if (ShouldGenerateClassNativeField(@class)) if (Options.GenerateBindings && ShouldGenerateClassNativeField(@class))
{ {
PushBlock(BlockKind.Field); PushBlock(BlockKind.Field);
if (@class.IsValueType) if (@class.IsValueType)
@ -483,9 +493,12 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
WriteLine($"private bool __{prop.Field.OriginalName}_OwnsNativeMemory = false;"); WriteLine($"private bool __{prop.Field.OriginalName}_OwnsNativeMemory = false;");
} }
GenerateClassConstructors(@class); if (Options.GenerateBindings)
{
GenerateClassConstructors(@class);
GenerateClassMethods(@class.Methods);
}
GenerateClassMethods(@class.Methods);
GenerateClassVariables(@class); GenerateClassVariables(@class);
GenerateClassProperties(@class); GenerateClassProperties(@class);
@ -512,6 +525,9 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
GenerateClassSpecifier(@class); GenerateClassSpecifier(@class);
var shouldInheritFromIDisposable = !@class.HasBase; var shouldInheritFromIDisposable = !@class.HasBase;
if (Options.GenerateTypesOnly)
shouldInheritFromIDisposable = false;
if (shouldInheritFromIDisposable) if (shouldInheritFromIDisposable)
Write(" : IDisposable"); Write(" : IDisposable");
@ -570,6 +586,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
public void GenerateClassInternals(Class @class) public void GenerateClassInternals(Class @class)
{ {
if (!Options.GenerateBindings)
return;
var sequentialLayout = Options.GenerateSequentialLayout && CanUseSequentialLayout(@class); var sequentialLayout = Options.GenerateSequentialLayout && CanUseSequentialLayout(@class);
PushBlock(BlockKind.InternalsClass); PushBlock(BlockKind.InternalsClass);
@ -800,7 +818,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
} }
} }
if (@class.IsGenerated && isBindingGen && @class.IsRefType && !@class.IsOpaque) if (Options.GenerateBindings && @class.IsGenerated && isBindingGen && @class.IsRefType && !@class.IsOpaque)
{ {
bases.Add("IDisposable"); bases.Add("IDisposable");
} }
@ -1549,13 +1567,26 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
if (prop.IsVirtual && !isOverride && !prop.IsPure) if (prop.IsVirtual && !isOverride && !prop.IsPure)
Write("virtual "); Write("virtual ");
WriteLine($"{printedType} {GetPropertyName(prop)}"); Write($"{printedType} {GetPropertyName(prop)}");
} }
else else
{ {
WriteLine($@"{printedType} { Write($@"{printedType} {
prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}"); prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}");
} }
if (Options.GenerateTypesOnly)
{
if (prop.HasSetter)
Write(@$" {{ get; set; }}");
else
Write(@$" {{ get; }}");
NewLine();
PopBlock(NewLineKind.Never);
continue;
}
WriteLine(String.Empty);
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
if (prop.Field != null) if (prop.Field != null)

2
src/Generator/Options.cs

@ -93,6 +93,8 @@ namespace CppSharp
/// <c>true</c> to generate class templates; otherwise, <c>false</c>. /// <c>true</c> to generate class templates; otherwise, <c>false</c>.
/// </value> /// </value>
public bool GenerateClassTemplates { get; set; } public bool GenerateClassTemplates { get; set; }
public bool GenerateTypesOnly { get; set; } = false;
internal bool GenerateBindings => !GenerateTypesOnly;
public bool AllowRenaming { get; set; } = true; public bool AllowRenaming { get; set; } = true;
public bool PutAllGlobalsInGlobalClass { get; set; } = false; public bool PutAllGlobalsInGlobalClass { get; set; } = false;
public bool GenerateInternalImports; public bool GenerateInternalImports;

Loading…
Cancel
Save