Skip to content

Commit

Permalink
Introduce GenerateFreeStandingFunctionsClassName option (#1782)
Browse files Browse the repository at this point in the history
* Introduce `GenerateFreeStandingFunctionsClassName` option

* Support CLI and fixes
  • Loading branch information
Saalvage authored Oct 20, 2023
1 parent 03874e7 commit b16e809
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/Generator/Generators/CLI/CLIHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void GenerateFunctions(DeclarationContext decl)
{
PushBlock(BlockKind.FunctionsClass, decl);

WriteLine("public ref class {0}", TranslationUnit.FileNameWithoutExtension);
WriteLine("public ref class {0}", Options.GenerateFreeStandingFunctionsClassName(TranslationUnit));
WriteLine("{");
WriteLine("public:");
Indent();
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CLI/CLISources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ public void GenerateFunction(Function function, DeclarationContext @namespace)
GenerateDeclarationCommon(function);

var classSig = string.Format("{0}::{1}", QualifiedIdentifier(@namespace),
TranslationUnit.FileNameWithoutExtension);
Options.GenerateFreeStandingFunctionsClassName(TranslationUnit));

Write("{0} {1}::{2}(", function.ReturnType, classSig,
function.Name);
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CLI/CLITypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public override TypePrinterResult VisitDeclaration(Declaration decl)
var result = string.Join("::", names);
var translationUnit = decl.Namespace as TranslationUnit;
if (translationUnit != null && translationUnit.HasFunctions &&
rootNamespace == translationUnit.FileNameWithoutExtension)
rootNamespace == Options.GenerateFreeStandingFunctionsClassName(translationUnit))
return "::" + result;
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public virtual void GenerateNamespaceFunctionsAndVariables(DeclarationContext co
if (!context.Functions.Any(f => f.IsGenerated) && !hasGlobalVariables)
return;

var parentName = SafeIdentifier(context.TranslationUnit.FileNameWithoutExtension);
var parentName = SafeIdentifier(Context.Options.GenerateFreeStandingFunctionsClassName(context.TranslationUnit));
var isStruct = EnumerateClasses()
.ToList()
.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName)
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpTypePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ specialization.OriginalNamespace is Class &&
}

if (decl is Variable && !(decl.Namespace is Class))
names.Push(decl.TranslationUnit.FileNameWithoutExtension);
names.Push(Options.GenerateFreeStandingFunctionsClassName(decl.TranslationUnit));

while (!(ctx is TranslationUnit))
{
Expand Down
6 changes: 6 additions & 0 deletions src/Generator/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public bool DoAllModulesHaveLibraries() =>
public string IncludePrefix;
public Func<TranslationUnit, string> GenerateName;

/// <summary>
/// By default the classes in which free standing functions are contained are named like the header they are in
/// this options allows you to customize this behavior.
/// </summary>
public Func<TranslationUnit, string> GenerateFreeStandingFunctionsClassName = tu => tu.FileNameWithoutExtension;

/// <summary>
/// Set this option to the kind of comments that you want generated
/// in the source code. This overrides the default kind set by the
Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Passes/MoveFunctionToClassPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ private Class FindClassToMoveFunctionTo(Function function)
}
else
{
string name = (function.Namespace as TranslationUnit)?.FileNameWithoutExtension ??
var tu = function.Namespace as TranslationUnit;
string name = tu != null ? Options.GenerateFreeStandingFunctionsClassName(tu) :
function.Namespace.Name;
@class = ASTContext.FindClass(
name, ignoreCase: true).FirstOrDefault(
Expand Down
1 change: 1 addition & 0 deletions tests/dotnet/CLI/CLI.Gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public override void Setup(Driver driver)
{
driver.Options.GenerateFinalizers = true;
driver.Options.GenerateObjectOverrides = true;
driver.Options.GenerateFreeStandingFunctionsClassName = tu => tu.FileNameWithoutExtension + "Cool";
base.Setup(driver);
}

Expand Down
10 changes: 5 additions & 5 deletions tests/dotnet/CLI/CLI.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void TestMultipleConstantArraysParamsTestMethod()
byte[] bytes2 = Encoding.ASCII.GetBytes("TestMulti2");
sbyte[] sbytes2 = Array.ConvertAll(bytes2, q => Convert.ToSByte(q));

string s = CLI.CLI.MultipleConstantArraysParamsTestMethod(sbytes, sbytes2);
string s = CLI.CLICool.MultipleConstantArraysParamsTestMethod(sbytes, sbytes2);
Assert.AreEqual("TestMultiTestMulti2", s);
}

Expand All @@ -88,7 +88,7 @@ public void TestMultipleConstantArraysParamsTestMethodLongerSourceArray()
byte[] bytes = Encoding.ASCII.GetBytes("TestMultipleConstantArraysParamsTestMethodLongerSourceArray");
sbyte[] sbytes = Array.ConvertAll(bytes, q => Convert.ToSByte(q));

Assert.Throws<InvalidOperationException>(() => CLI.CLI.MultipleConstantArraysParamsTestMethod(sbytes, new sbyte[] { }));
Assert.Throws<InvalidOperationException>(() => CLI.CLICool.MultipleConstantArraysParamsTestMethod(sbytes, new sbyte[] { }));
}

[Test]
Expand All @@ -110,7 +110,7 @@ public void TestStructWithNestedUnionTestMethod()
Assert.AreEqual(10, val.NestedUnion.SzText.Length);
Assert.AreEqual("TestUnions", val.NestedUnion.SzText);

string ret = CLI.CLI.StructWithNestedUnionTestMethod(val);
string ret = CLI.CLICool.StructWithNestedUnionTestMethod(val);

Assert.AreEqual("TestUnions", ret);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ public void TestUnionWithNestedStructTestMethod()
Assert.AreEqual(10, unionWithNestedStruct.NestedStruct.SzText.Length);
Assert.AreEqual("TestUnions", unionWithNestedStruct.NestedStruct.SzText);

string ret = CLI.CLI.UnionWithNestedStructTestMethod(unionWithNestedStruct);
string ret = CLI.CLICool.UnionWithNestedStructTestMethod(unionWithNestedStruct);

Assert.AreEqual("TestUnions", ret);
}
Expand All @@ -172,7 +172,7 @@ public void TestUnionWithNestedStructArrayTestMethod()

Assert.AreEqual(2, unionWithNestedStructArray.NestedStructs.Length);

string ret = CLI.CLI.UnionWithNestedStructArrayTestMethod(unionWithNestedStructArray);
string ret = CLI.CLICool.UnionWithNestedStructArrayTestMethod(unionWithNestedStructArray);

Assert.AreEqual("TestUnion1TestUnion2", ret);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/dotnet/CSharp/CSharp.Gen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public override void Setup(Driver driver)

driver.ParserOptions.UnityBuild = true;
driver.ParserOptions.AddSupportedFunctionTemplates("FunctionTemplate");

driver.Options.GenerateFreeStandingFunctionsClassName = t => t.FileNameWithoutExtension + "Cool";
}

public override void SetupPasses(Driver driver)
Expand Down
Loading

0 comments on commit b16e809

Please sign in to comment.