Skip to content

Commit

Permalink
Misc changes (mono#1710)
Browse files Browse the repository at this point in the history
* Minor code refactoring for re-use.

* Add Class.FindVariable helper method.

* Upgrade to .NET 6.
  • Loading branch information
tritao authored Feb 2, 2023
1 parent c4a24b0 commit db7949b
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build/Helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ msvc_cpp_defines = { }
default_gcc_version = "9.0.0"
generate_build_config = true
premake.path = premake.path .. ";" .. path.join(builddir, "modules")
targetframework = "netcoreapp3.1"
targetframework = "net6.0"

function string.starts(str, start)
if str == nil then return end
Expand Down
5 changes: 5 additions & 0 deletions src/AST/Class.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ public Method FindMethodByUSR(string usr)
.FirstOrDefault(m => m.USR == usr);
}

public Variable FindVariable(string name)
{
return Variables.FirstOrDefault(m => m.Name == name);
}

public override T Visit<T>(IDeclVisitor<T> visitor)
{
return visitor.VisitClassDecl(this);
Expand Down
1 change: 1 addition & 0 deletions src/CLI/CppSharp.CLI.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions src/Generator/Generators/C/CppTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,8 @@ public override TypePrinterResult VisitClassTemplateSpecializationDecl(
args.Add(arg.Type.Visit(this));
break;
case TemplateArgument.ArgumentKind.Declaration:
args.Add(arg.Declaration.Visit(this));
if (arg.Declaration != null)
args.Add(arg.Declaration.Visit(this));
break;
case TemplateArgument.ArgumentKind.Integral:
ClassTemplate template = specialization.TemplatedDecl;
Expand All @@ -596,6 +597,7 @@ public override TypePrinterResult VisitClassTemplateSpecializationDecl(
{
args.Add(arg.Integral.ToString(CultureInfo.InvariantCulture));
}

break;
}
}
Expand Down Expand Up @@ -634,7 +636,7 @@ public override TypePrinterResult VisitFunctionDecl(Function function)
CppSharp.AST.Type desugared = function.FunctionType.Type.Desugar();
if (!desugared.IsPointerTo(out functionType))
functionType = (FunctionType)desugared;
string exceptionType = Print(functionType.ExceptionSpecType);
string exceptionType = functionType != null ? Print(functionType.ExceptionSpecType) : "";

var @return = function.OriginalReturnType.Visit(this);
@return.Name = @class + name;
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/Generators/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public virtual bool VisitClassDeclContext(Class @class)
property.Visit(this);
}

VisitClassConstructors(@class);
VisitClassConstructors(@class.Constructors.Where(c => !ASTUtils.CheckIgnoreMethod(c)));
VisitClassMethods(@class);

return true;
Expand All @@ -333,9 +333,9 @@ public virtual void VisitClassMethods(Class @class)
}
}

public virtual void VisitClassConstructors(Class @class)
public virtual void VisitClassConstructors(IEnumerable<Method> constructors)
{
foreach (var ctor in @class.Constructors.Where(c => !ASTUtils.CheckIgnoreMethod(c)))
foreach (var ctor in constructors)
{
ctor.Visit(this);
}
Expand Down
32 changes: 24 additions & 8 deletions src/Generator/Generators/NAPI/NAPIHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using CppSharp.AST;
using CppSharp.AST.Extensions;
Expand All @@ -9,11 +10,9 @@

namespace CppSharp.Generators.Cpp
{
public class NAPICodeGenerator : CCodeGenerator
public class MethodGroupCodeGenerator : CCodeGenerator
{
public override string FileExtension => "cpp";

public NAPICodeGenerator(BindingContext context, IEnumerable<TranslationUnit> units)
protected MethodGroupCodeGenerator(BindingContext context, IEnumerable<TranslationUnit> units)
: base(context, units)
{
}
Expand Down Expand Up @@ -43,9 +42,9 @@ public override bool VisitClassDecl(Class @class)
return VisitClassDeclContext(@class);
}

public override void VisitClassConstructors(Class @class)
public override void VisitClassConstructors(IEnumerable<Method> ctors)
{
var constructors = @class.Constructors.Where(c => c.IsGenerated && !c.IsCopyConstructor)
var constructors = ctors.Where(c => c.IsGenerated && !c.IsCopyConstructor)
.ToList();

if (!constructors.Any())
Expand All @@ -59,7 +58,7 @@ public static bool ShouldGenerate(Function function)
if (!function.IsGenerated)
return false;

if (!(function is Method method))
if (function is not Method method)
return true;

if (method.IsConstructor || method.IsDestructor)
Expand Down Expand Up @@ -88,10 +87,27 @@ public virtual void GenerateMethodGroup(List<Method> @group)
foreach (var method in @group)
{
method.Visit(this);
return;
}
}

public static string GetTranslationUnitName(TranslationUnit unit)
{
var paths = unit.FileRelativePath.Split('/').ToList();
paths = paths.Select(p => Path.GetFileNameWithoutExtension(p.ToLowerInvariant())).ToList();
var name = string.Join('_', paths);
return name;
}
}

public class NAPICodeGenerator : MethodGroupCodeGenerator
{
public override string FileExtension => "cpp";

public NAPICodeGenerator(BindingContext context, IEnumerable<TranslationUnit> units)
: base(context, units)
{
}

public virtual MarshalPrinter<MarshalContext, CppTypePrinter> GetMarshalManagedToNativePrinter(MarshalContext ctx)
{
return new NAPIMarshalManagedToNativePrinter(ctx);
Expand Down
9 changes: 0 additions & 9 deletions src/Generator/Generators/NAPI/NAPISources.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using CppSharp.AST;
Expand All @@ -24,14 +23,6 @@ public NAPISources(BindingContext context, IEnumerable<TranslationUnit> units)
{
}

public static string GetTranslationUnitName(TranslationUnit unit)
{
var paths = unit.FileRelativePath.Split('/').ToList();
paths = paths.Select(p => Path.GetFileNameWithoutExtension(p.ToLowerInvariant())).ToList();
var name = string.Join('_', paths);
return name;
}

public override void Process()
{
GenerateFilePreamble(CommentKind.BCPL);
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/QuickJS/QuickJSSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public QuickJSClassFuncDef(BindingContext context) : base(context, null)
{
}

public override void VisitClassConstructors(Class @class)
public override void VisitClassConstructors(IEnumerable<Method> ctors)
{
}

Expand Down

0 comments on commit db7949b

Please sign in to comment.