Skip to content

Commit

Permalink
Reformat + cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Oct 28, 2024
1 parent ea8160e commit 7879322
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 81 deletions.
157 changes: 121 additions & 36 deletions src/Bindgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Generator(GeneratorOptions options)

public void Generate()
{
var parserOptions = new CppParserOptions
var parserOptions = new CppParserOptions
{
AdditionalArguments = { "-xc", "-std=gnu99" },
ParseAsCpp = false,
Expand Down Expand Up @@ -172,12 +172,14 @@ public void Generate()
testBuilder.AppendLine($"public class {options.GeneratedClass}Test");
testBuilder.AppendLine("{");

testBuilder.Append($$"""
private unsafe void CheckType<T>() where T : unmanaged
{
Assert.True(BlittableHelper.IsBlittable<T>());
}
""");
testBuilder.Append(
$$"""
private unsafe void CheckType<T>() where T : unmanaged
{
Assert.True(BlittableHelper.IsBlittable<T>());
}
"""
);

testBuilder.AppendLine();
testBuilder.AppendLine();
Expand Down Expand Up @@ -336,7 +338,13 @@ private string GenerateStruct(CppClass cppStruct, out string output)
GenerateComments(field, ref output);

var pointerCount = 0;
string fieldType = ConvertCppTypeToCSharp(structName, field.Name, field.Type, ref pointerCount, out var arraySize);
string fieldType = ConvertCppTypeToCSharp(
structName,
field.Name,
field.Type,
ref pointerCount,
out var arraySize
);
string fieldName = MapName(field.Name, toPascalCase: true);

if (arraySize > 0)
Expand Down Expand Up @@ -377,7 +385,13 @@ private string GenerateStruct(CppClass cppStruct, out string output)
foreach (var field in cppStruct.Fields)
{
var pointerCount = 0;
string fieldType = ConvertCppTypeToCSharp(structName, field.Name, field.Type, ref pointerCount, out var arraySize);
string fieldType = ConvertCppTypeToCSharp(
structName,
field.Name,
field.Type,
ref pointerCount,
out var arraySize
);
string fieldName = MapName(field.Name, toCamelCase: true);

if (!first)
Expand Down Expand Up @@ -419,7 +433,13 @@ private string GenerateFunction(CppFunction function, out string output)
}

var pointerCount = 0;
string returnType = ConvertCppTypeToCSharp(function.Name, "return", function.ReturnType, ref pointerCount, out _);
string returnType = ConvertCppTypeToCSharp(
function.Name,
"return",
function.ReturnType,
ref pointerCount,
out _
);
returnType = AppendPointer(returnType, pointerCount);

string functionName = MapName(function.Name);
Expand All @@ -429,7 +449,13 @@ private string GenerateFunction(CppFunction function, out string output)
foreach (var parameter in function.Parameters)
{
var paramPointerCount = 0;
string paramType = ConvertCppTypeToCSharp(functionName, parameter.Name, parameter.Type, ref paramPointerCount, out _);
string paramType = ConvertCppTypeToCSharp(
functionName,
parameter.Name,
parameter.Type,
ref paramPointerCount,
out _
);
string paramName = MapName(parameter.Name);
bool isArray = options.DetectArray(function.Name, parameter.Name);

Expand Down Expand Up @@ -527,22 +553,26 @@ private string ConvertCppTypeToCSharp(
switch (cppType)
{
case CppPrimitiveType primitiveType:
return MapType(parent, cppName, primitiveType.Kind switch
{
CppPrimitiveKind.Void => "void",
CppPrimitiveKind.Bool => skipHighOrder ? "sbyte" : "NativeBool",
CppPrimitiveKind.Char => "sbyte",
CppPrimitiveKind.Short => "short",
CppPrimitiveKind.Int => "int",
CppPrimitiveKind.Long => "long",
CppPrimitiveKind.Float => "float",
CppPrimitiveKind.Double => "double",
CppPrimitiveKind.UnsignedChar => "byte",
CppPrimitiveKind.UnsignedShort => "ushort",
CppPrimitiveKind.UnsignedInt => "uint",
CppPrimitiveKind.UnsignedLong => "ulong",
_ => throw new NotSupportedException($"Unsupported primitive type: {primitiveType.Kind}"),
});
return MapType(
parent,
cppName,
primitiveType.Kind switch
{
CppPrimitiveKind.Void => "void",
CppPrimitiveKind.Bool => skipHighOrder ? "sbyte" : "NativeBool",
CppPrimitiveKind.Char => "sbyte",
CppPrimitiveKind.Short => "short",
CppPrimitiveKind.Int => "int",
CppPrimitiveKind.Long => "long",
CppPrimitiveKind.Float => "float",
CppPrimitiveKind.Double => "double",
CppPrimitiveKind.UnsignedChar => "byte",
CppPrimitiveKind.UnsignedShort => "ushort",
CppPrimitiveKind.UnsignedInt => "uint",
CppPrimitiveKind.UnsignedLong => "ulong",
_ => throw new NotSupportedException($"Unsupported primitive type: {primitiveType.Kind}"),
}
);
case CppPointerType pointerType:
if (pointerCount == 0 && !skipHighOrder)
{
Expand All @@ -566,17 +596,52 @@ private string ConvertCppTypeToCSharp(
}

pointerCount++;
return ConvertCppTypeToCSharp(parent, cppName, pointerType.ElementType, ref pointerCount, out _, skipHighOrder);
return ConvertCppTypeToCSharp(
parent,
cppName,
pointerType.ElementType,
ref pointerCount,
out _,
skipHighOrder
);
case CppReferenceType referenceType:
pointerCount++;
return ConvertCppTypeToCSharp(parent, cppName, referenceType.ElementType, ref pointerCount, out _, skipHighOrder);
return ConvertCppTypeToCSharp(
parent,
cppName,
referenceType.ElementType,
ref pointerCount,
out _,
skipHighOrder
);
case CppArrayType arrayType:
arraySize = arrayType.Size;
return ConvertCppTypeToCSharp(parent, cppName, arrayType.ElementType, ref pointerCount, out _, skipHighOrder);
return ConvertCppTypeToCSharp(
parent,
cppName,
arrayType.ElementType,
ref pointerCount,
out _,
skipHighOrder
);
case CppQualifiedType qualifiedType:
return ConvertCppTypeToCSharp(parent, cppName, qualifiedType.ElementType, ref pointerCount, out _, skipHighOrder);
return ConvertCppTypeToCSharp(
parent,
cppName,
qualifiedType.ElementType,
ref pointerCount,
out _,
skipHighOrder
);
case CppTypedef typedefType:
return ConvertCppTypeToCSharp(parent, cppName, typedefType.ElementType, ref pointerCount, out _, skipHighOrder);
return ConvertCppTypeToCSharp(
parent,
cppName,
typedefType.ElementType,
ref pointerCount,
out _,
skipHighOrder
);
case CppClass cppClass:
return MapType(parent, cppName, cppClass.Name);
case CppEnum cppEnum:
Expand All @@ -597,7 +662,14 @@ private string ConvertCppTypeToCSharp(
foreach (var parameter in cppFunctionType.Parameters)
{
var paramPointerCount = 0;
string paramType = ConvertCppTypeToCSharp(cppName, parameter.Name, parameter.Type, ref paramPointerCount, out _, true);
string paramType = ConvertCppTypeToCSharp(
cppName,
parameter.Name,
parameter.Type,
ref paramPointerCount,
out _,
true
);
paramType = AppendPointer(paramType, paramPointerCount);
parameters.Add(paramType);
}
Expand All @@ -612,7 +684,10 @@ private string ConvertCppTypeToCSharp(
private string MapName(string name, bool toPascalCase = false, bool toCamelCase = false)
{
var o = options.TransformName(name) ?? name;
o = toCamelCase ? ToCamelCase(o) : toPascalCase ? ToPascalCase(o) : o;
o =
toCamelCase ? ToCamelCase(o)
: toPascalCase ? ToPascalCase(o)
: o;

if (reservedKeywords.Contains(o))
{
Expand All @@ -634,7 +709,7 @@ private string MapType(string parent, string name, string type)
"uint8_t" => "byte",
"int64_t" => "long",
"uint64_t" => "ulong",
_ => options.TransformType(parent, name, type) ?? type
_ => options.TransformType(parent, name, type) ?? type,
};
}

Expand All @@ -661,7 +736,17 @@ private static bool IsSimpleRef(CppFunction fn, string name, string type)
}

var baseName = name.EndsWith("s") ? name.Substring(0, name.Length - 1) : name;
var possibleSuffixes = new List<string> { "Count", "sCount", "Length", "sLength", "Size", "sSize", "Len", "sLen" };
var possibleSuffixes = new List<string>
{
"Count",
"sCount",
"Length",
"sLength",
"Size",
"sSize",
"Len",
"sLen",
};

foreach (var p in fn.Parameters)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Bindgen/GeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ public struct GeneratorOptions
public Func<string, string, bool> DetectArray = (parent, name) => false;
public Dictionary<string, string> ExistingTypes = new();

public GeneratorOptions()
{
}
public GeneratorOptions() { }
}
79 changes: 37 additions & 42 deletions src/Raylib.NET.Bindgen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,47 @@
var libPath = "../../lib";
var includePath = args[0];

var transformEnum = (string parent, string name) => parent switch
{
"IsKeyPressed" or "IsKeyPressedRepeat" or "IsKeyDown" or "IsKeyReleased" or "IsKeyUp" or "GetKeyPressed" => name switch
{
"key" or "return" => "KeyboardKey",
_ => null
},
"IsMouseButtonPressed" or "IsMouseButtonDown" or "IsMouseButtonReleased" or "IsMouseButtonUp" => name switch
var transformEnum = (string parent, string name) =>
parent switch
{
"button" => "MouseButton",
_ => null
},
"IsGampadButtonPressed" or "IsGamepadButtonDown" or "IsGamepadButtonReleased" or "IsGamepadButtonUp" or "GetGamepadButtonPressed" => name switch
{
"button" or "return" => "GamepadButton",
_ => null
},
"GuiGetStyle" or "GuiSetStyle" => name switch
{
"control" => "GuiControl",
_ => null
},
_ => null
};
"IsKeyPressed" or "IsKeyPressedRepeat" or "IsKeyDown" or "IsKeyReleased" or "IsKeyUp" or "GetKeyPressed" =>
name switch
{
"key" or "return" => "KeyboardKey",
_ => null,
},
"IsMouseButtonPressed" or "IsMouseButtonDown" or "IsMouseButtonReleased" or "IsMouseButtonUp" => name switch
{
"button" => "MouseButton",
_ => null,
},
"IsGampadButtonPressed"
or "IsGamepadButtonDown"
or "IsGamepadButtonReleased"
or "IsGamepadButtonUp"
or "GetGamepadButtonPressed" => name switch
{
"button" or "return" => "GamepadButton",
_ => null,
},
"GuiGetStyle" or "GuiSetStyle" => name switch
{
"control" => "GuiControl",
_ => null,
},
_ => null,
};

var options = new GeneratorOptions
{
DetectArray = (string parent, string name) => {
if (parent == "rlGenTextureMipmaps") {
return true;
}
if (parent == "rlMultMatrixf") {
return true;
}
if (parent == "rlSetShader") {
return true;
}
if (parent.StartsWith("Unload")) {
return true;
}
return false;
},
DetectArray = (string parent, string name) =>
parent switch
{
"rlGenTextureMipmaps" => true,
"rlMultMatrixf" => true,
"rlSetShader" => true,
_ => parent.StartsWith("Unload"),
},
TransformType = (string parent, string name, string type) =>
type switch
{
Expand Down

0 comments on commit 7879322

Please sign in to comment.