Skip to content

Commit b16e809

Browse files
authored
Introduce GenerateFreeStandingFunctionsClassName option (#1782)
* Introduce `GenerateFreeStandingFunctionsClassName` option * Support CLI and fixes
1 parent 03874e7 commit b16e809

File tree

11 files changed

+73
-63
lines changed

11 files changed

+73
-63
lines changed

src/Generator/Generators/CLI/CLIHeaders.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public void GenerateFunctions(DeclarationContext decl)
219219
{
220220
PushBlock(BlockKind.FunctionsClass, decl);
221221

222-
WriteLine("public ref class {0}", TranslationUnit.FileNameWithoutExtension);
222+
WriteLine("public ref class {0}", Options.GenerateFreeStandingFunctionsClassName(TranslationUnit));
223223
WriteLine("{");
224224
WriteLine("public:");
225225
Indent();

src/Generator/Generators/CLI/CLISources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ public void GenerateFunction(Function function, DeclarationContext @namespace)
909909
GenerateDeclarationCommon(function);
910910

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

914914
Write("{0} {1}::{2}(", function.ReturnType, classSig,
915915
function.Name);

src/Generator/Generators/CLI/CLITypePrinter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public override TypePrinterResult VisitDeclaration(Declaration decl)
329329
var result = string.Join("::", names);
330330
var translationUnit = decl.Namespace as TranslationUnit;
331331
if (translationUnit != null && translationUnit.HasFunctions &&
332-
rootNamespace == translationUnit.FileNameWithoutExtension)
332+
rootNamespace == Options.GenerateFreeStandingFunctionsClassName(translationUnit))
333333
return "::" + result;
334334
return result;
335335
}

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public virtual void GenerateNamespaceFunctionsAndVariables(DeclarationContext co
250250
if (!context.Functions.Any(f => f.IsGenerated) && !hasGlobalVariables)
251251
return;
252252

253-
var parentName = SafeIdentifier(context.TranslationUnit.FileNameWithoutExtension);
253+
var parentName = SafeIdentifier(Context.Options.GenerateFreeStandingFunctionsClassName(context.TranslationUnit));
254254
var isStruct = EnumerateClasses()
255255
.ToList()
256256
.FindAll(cls => cls.IsValueType && cls.Name == parentName && context.QualifiedLogicalName == cls.Namespace.QualifiedLogicalName)

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ specialization.OriginalNamespace is Class &&
575575
}
576576

577577
if (decl is Variable && !(decl.Namespace is Class))
578-
names.Push(decl.TranslationUnit.FileNameWithoutExtension);
578+
names.Push(Options.GenerateFreeStandingFunctionsClassName(decl.TranslationUnit));
579579

580580
while (!(ctx is TranslationUnit))
581581
{

src/Generator/Options.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ public bool DoAllModulesHaveLibraries() =>
172172
public string IncludePrefix;
173173
public Func<TranslationUnit, string> GenerateName;
174174

175+
/// <summary>
176+
/// By default the classes in which free standing functions are contained are named like the header they are in
177+
/// this options allows you to customize this behavior.
178+
/// </summary>
179+
public Func<TranslationUnit, string> GenerateFreeStandingFunctionsClassName = tu => tu.FileNameWithoutExtension;
180+
175181
/// <summary>
176182
/// Set this option to the kind of comments that you want generated
177183
/// in the source code. This overrides the default kind set by the

src/Generator/Passes/MoveFunctionToClassPass.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ private Class FindClassToMoveFunctionTo(Function function)
5757
}
5858
else
5959
{
60-
string name = (function.Namespace as TranslationUnit)?.FileNameWithoutExtension ??
60+
var tu = function.Namespace as TranslationUnit;
61+
string name = tu != null ? Options.GenerateFreeStandingFunctionsClassName(tu) :
6162
function.Namespace.Name;
6263
@class = ASTContext.FindClass(
6364
name, ignoreCase: true).FirstOrDefault(

tests/dotnet/CLI/CLI.Gen.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public override void Setup(Driver driver)
6868
{
6969
driver.Options.GenerateFinalizers = true;
7070
driver.Options.GenerateObjectOverrides = true;
71+
driver.Options.GenerateFreeStandingFunctionsClassName = tu => tu.FileNameWithoutExtension + "Cool";
7172
base.Setup(driver);
7273
}
7374

tests/dotnet/CLI/CLI.Tests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public void TestMultipleConstantArraysParamsTestMethod()
7878
byte[] bytes2 = Encoding.ASCII.GetBytes("TestMulti2");
7979
sbyte[] sbytes2 = Array.ConvertAll(bytes2, q => Convert.ToSByte(q));
8080

81-
string s = CLI.CLI.MultipleConstantArraysParamsTestMethod(sbytes, sbytes2);
81+
string s = CLI.CLICool.MultipleConstantArraysParamsTestMethod(sbytes, sbytes2);
8282
Assert.AreEqual("TestMultiTestMulti2", s);
8383
}
8484

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

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

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

113-
string ret = CLI.CLI.StructWithNestedUnionTestMethod(val);
113+
string ret = CLI.CLICool.StructWithNestedUnionTestMethod(val);
114114

115115
Assert.AreEqual("TestUnions", ret);
116116
}
@@ -146,7 +146,7 @@ public void TestUnionWithNestedStructTestMethod()
146146
Assert.AreEqual(10, unionWithNestedStruct.NestedStruct.SzText.Length);
147147
Assert.AreEqual("TestUnions", unionWithNestedStruct.NestedStruct.SzText);
148148

149-
string ret = CLI.CLI.UnionWithNestedStructTestMethod(unionWithNestedStruct);
149+
string ret = CLI.CLICool.UnionWithNestedStructTestMethod(unionWithNestedStruct);
150150

151151
Assert.AreEqual("TestUnions", ret);
152152
}
@@ -172,7 +172,7 @@ public void TestUnionWithNestedStructArrayTestMethod()
172172

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

175-
string ret = CLI.CLI.UnionWithNestedStructArrayTestMethod(unionWithNestedStructArray);
175+
string ret = CLI.CLICool.UnionWithNestedStructArrayTestMethod(unionWithNestedStructArray);
176176

177177
Assert.AreEqual("TestUnion1TestUnion2", ret);
178178
}

tests/dotnet/CSharp/CSharp.Gen.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public override void Setup(Driver driver)
2626

2727
driver.ParserOptions.UnityBuild = true;
2828
driver.ParserOptions.AddSupportedFunctionTemplates("FunctionTemplate");
29+
30+
driver.Options.GenerateFreeStandingFunctionsClassName = t => t.FileNameWithoutExtension + "Cool";
2931
}
3032

3133
public override void SetupPasses(Driver driver)

0 commit comments

Comments
 (0)