Skip to content

Commit 53d5d99

Browse files
committed
Compile time constats WIP
1 parent c7cb245 commit 53d5d99

5 files changed

Lines changed: 51 additions & 60 deletions

File tree

EasySourceGenerators.Abstractions/MethodBody/IMethodBodyBuilder.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,49 @@ public interface IMethodBodyBuilderStage3<TReturnType>
2727

2828
public interface IMethodBodyBuilderStage4ReturnVoidNoArg
2929
{
30+
IMethodBodyBuilderStage5ReturnVoidNoArgWithConstants<TConstants> WithCompileTimeConstants<TConstants>(Func<TConstants> compileTimeConstantsFactory);
3031
IMethodBodyGenerator UseProvidedBody(Action body);
3132
}
3233

34+
public interface IMethodBodyBuilderStage5ReturnVoidNoArgWithConstants<TConstants>
35+
{
36+
IMethodBodyGenerator UseProvidedBody(Action<TConstants> body);
37+
}
38+
3339
public interface IMethodBodyBuilderStage4NoArg<TReturnType>
3440
{
41+
IMethodBodyBuilderStage5NoArgWithConstants<TReturnType, TConstants> WithCompileTimeConstants<TConstants>(Func<TConstants> compileTimeConstantsFactory);
3542
IMethodBodyGenerator UseProvidedBody(Func<TReturnType> body);
3643
IMethodBodyGenerator BodyReturningConstant(Func<TReturnType> constantValueFactory);
3744
}
3845

46+
public interface IMethodBodyBuilderStage5NoArgWithConstants<TReturnType, TConstants>
47+
{
48+
IMethodBodyGenerator UseProvidedBody(Func<TConstants, TReturnType> body);
49+
IMethodBodyGenerator BodyReturningConstant(Func<TConstants, TReturnType> constantValueFactory);
50+
}
51+
3952
public interface IMethodBodyBuilderStage4ReturnVoid<TParam1>
4053
{
54+
IMethodBodyBuilderStage5ReturnVoidWithConstants<TParam1, TConstants> WithCompileTimeConstants<TConstants>(Func<TConstants> compileTimeConstantsFactory);
4155
IMethodBodyGenerator UseProvidedBody(Action<TParam1> body);
4256
}
4357

58+
public interface IMethodBodyBuilderStage5ReturnVoidWithConstants<TParam1, TConstants>
59+
{
60+
IMethodBodyGenerator UseProvidedBody(Action<TConstants, TParam1> body);
61+
}
62+
4463
public interface IMethodBodyBuilderStage4<TParam1, in TReturnType>
4564
{
65+
IMethodBodyBuilderStage5WithConstants<TParam1, TReturnType, TConstants> WithCompileTimeConstants<TConstants>(Func<TConstants> compileTimeConstantsFactory);
66+
4667
IMethodBodyGenerator UseProvidedBody(Func<TParam1, TReturnType> body);
4768
IMethodBodyGenerator BodyReturningConstant(Func<TReturnType> constantValueFactory);
48-
IMethodBodyGeneratorSwitchBody<TParam1, TReturnType> BodyWithSwitchStatement();
69+
}
70+
71+
public interface IMethodBodyBuilderStage5WithConstants<TParam1, in TReturnType, TConstants>
72+
{
73+
IMethodBodyGenerator UseProvidedBody(Func<TConstants, TParam1, TReturnType> body);
74+
IMethodBodyGenerator BodyReturningConstant(Func<TConstants, TReturnType> constantValueFactory);
4975
}
Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
// SwitchCase/SwitchDefault attribute-based generation is commented out pending replacement with a data-driven approach.
2-
// See DataMethodBodyBuilders.cs for details on the planned replacement.
3-
4-
/*
5-
using EasySourceGenerators.Abstractions;
1+
using EasySourceGenerators.Abstractions;
62
// ReSharper disable ConvertClosureToMethodGroup
73

84
namespace EasySourceGenerators.Examples;
95

10-
public static partial class PiExample
6+
public static partial class PiExampleFluent
117
{
128
public static partial int GetPiDecimal(int decimalNumber);
139

1410
[MethodBodyGenerator(nameof(GetPiDecimal))]
15-
[SwitchCase(arg1: 0)]
16-
[SwitchCase(arg1: 1)]
17-
[SwitchCase(arg1: 2)]
18-
static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
19-
SlowMath.CalculatePiDecimal(decimalNumber);
20-
21-
[MethodBodyGenerator(nameof(GetPiDecimal))]
22-
[SwitchDefault]
23-
static Func<int, int> GetPiDecimal_Generator_Fallback() => decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber);
11+
static IMethodBodyGenerator GetPiDecimal_Generator() =>
12+
Generate.MethodBody()
13+
.ForMethod().WithReturnType<int>().WithParameter<int>()
14+
.WithCompileTimeConstants(() => new
15+
{
16+
PrecomputedTargets = (new int[] { 0, 1, 2, 300, 301, 302, 303 }).ToDictionary(i => i, i => SlowMath.CalculatePiDecimal(i))
17+
})
18+
.UseProvidedBody((constants, decimalNumber) =>
19+
{
20+
if (constants.PrecomputedTargets.TryGetValue(decimalNumber, out int precomputedResult)) return precomputedResult;
21+
22+
return SlowMath.CalculatePiDecimal(decimalNumber);
23+
});
2424
}
25-
*/
2625

2726
/*
2827
This will generate the following method:
@@ -34,7 +33,11 @@ public static int GetPiDecimal(int decimalNumber)
3433
case 0: return 3;
3534
case 1: return 1;
3635
case 2: return 4;
36+
case 300: return 3;
37+
case 301: return 7;
38+
case 302: return 2;
39+
case 303: return 4;
3740
default: return CalculatePiDecimal(decimalNumber);
3841
}
3942
}
40-
*/
43+
*/

EasySourceGenerators.Examples/PiExampleFluent.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

EasySourceGenerators.Tests/DelegateBodyTests.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Text;
2-
using EasySourceGenerators.Abstractions;
1+
using EasySourceGenerators.Abstractions;
32
// ReSharper disable InconsistentNaming
43
// ReSharper disable RedundantIfElseBlock
54
// ReSharper disable ConvertSwitchStatementToSwitchExpression
@@ -108,7 +107,7 @@ static partial class DelegateBodyTestClass_WithComplexBody
108107
{
109108
public static partial int PartialMethod(int someParam)
110109
{
111-
int interResult = 0;
110+
int interResult;
112111
113112
switch (someParam)
114113
{
@@ -200,7 +199,7 @@ public static IMethodBodyGenerator JustReturnConstantGenerator() =>
200199
.ForMethod().WithReturnType<int>().WithParameter<int>()
201200
.UseProvidedBody(someParam =>
202201
{
203-
int interResult = 0;
202+
int interResult;
204203

205204
switch (someParam)
206205
{

EasySourceGenerators.sln.DotSettings.user

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AConditionalAttribute_002Ecs_002Fl_003AC_0021_003FUsers_003Fdex3r_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F5878d76d3b81484eafbba90b77c960fed2b38_003F80_003F1d2bb8bd_003FConditionalAttribute_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
77
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACSharpCompilationReference_002Ecs_002Fl_003AC_0021_003FUsers_003Fdex3r_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd18742e05d9d34467e6d756ef95f2def337dc61bd1678295c715359e839bb5_003FCSharpCompilationReference_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
88
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AGeneratorAttribute_002Ecs_002Fl_003AC_0021_003FUsers_003Fdex3r_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003Ff1fac3c62ca6565cc950105beae419771ba372ddd8be9ee61ee7edba4bcc22_003FGeneratorAttribute_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
9+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AObject_002Ecs_002Fl_003AC_0021_003FUsers_003Fdex3r_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F51505665371d472f8bdbc333fa4d888cf49938_003Fc8_003F5d7360d0_003FObject_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
910
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003APrimetivelyUsageExample_002ESomeInt_002Eg_002Ecs_002Fl_003AC_0021_003FUsers_003Fdex3r_003FAppData_003FLocal_003FTemp_003FSourceGeneratedDocuments_003F4B2AFDDAB240A2F5C9F2AF27_003FPrimitively_002ESourceGeneration_003FPrimitively_002EStructs_003FPrimetivelyUsageExample_002ESomeInt_002Eg_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
1011
<s:Boolean x:Key="/Default/Environment/Hierarchy/Build/BuildTool/MsBuildSolutionLoadingOrderingEnabled/@EntryValue">True</s:Boolean>
1112

0 commit comments

Comments
 (0)