From 0604630da89745b7677cab294bb64c5d526257dc Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Wed, 25 Mar 2026 15:14:41 +0000
Subject: [PATCH 1/4] Initial plan
From c7cb245b73042c437f494fb9331382b0070b00df Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Wed, 25 Mar 2026 15:30:22 +0000
Subject: [PATCH 2/4] feat: extract delegate body from UseProvidedBody syntax
for source generation
Add DelegateBodySyntaxExtractor to extract lambda body from syntax tree.
Support both expression lambdas (e.g. () => 42) and block lambdas with
complex control flow (if/else, switch, multi-statement bodies).
Add EmitWithBody to PartialMethodSourceEmitter for arbitrary body emission.
Fix test lambda parameter names to match actual method parameters.
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dex3r/EasySourceGenerators/sessions/e1b5542a-d819-423d-a4b7-f97407a7d1b4
---
.../DelegateBodySyntaxExtractor.cs | 157 ++++++++++++++++++
.../GeneratesMethodGenerationPipeline.cs | 38 ++++-
.../GeneratesMethodPatternSourceBuilder.cs | 13 ++
.../PartialMethodSourceEmitter.cs | 13 ++
.../DelegateBodyTests.cs | 18 +-
.../EasySourceGenerators.Tests.csproj | 1 +
6 files changed, 229 insertions(+), 11 deletions(-)
create mode 100644 EasySourceGenerators.Generators/IncrementalGenerators/DelegateBodySyntaxExtractor.cs
diff --git a/EasySourceGenerators.Generators/IncrementalGenerators/DelegateBodySyntaxExtractor.cs b/EasySourceGenerators.Generators/IncrementalGenerators/DelegateBodySyntaxExtractor.cs
new file mode 100644
index 0000000..4ca71fc
--- /dev/null
+++ b/EasySourceGenerators.Generators/IncrementalGenerators/DelegateBodySyntaxExtractor.cs
@@ -0,0 +1,157 @@
+using System;
+using System.Linq;
+using System.Text;
+using Microsoft.CodeAnalysis.CSharp.Syntax;
+
+namespace EasySourceGenerators.Generators.IncrementalGenerators;
+
+///
+/// Extracts the delegate body source code from a UseProvidedBody(...) invocation
+/// within a generator method's syntax tree. The extracted body is re-indented to match
+/// the target method body indentation (8 spaces).
+///
+internal static class DelegateBodySyntaxExtractor
+{
+ private const string MethodBodyIndent = " ";
+
+ ///
+ /// Attempts to find a UseProvidedBody(...) call in the given generator method syntax
+ /// and extract the lambda body. Returns null if no such call is found.
+ /// For expression lambdas, returns a single return {expr}; line.
+ /// For block lambdas, returns the block body re-indented to the method body level.
+ ///
+ internal static string? TryExtractDelegateBody(MethodDeclarationSyntax generatorMethodSyntax)
+ {
+ InvocationExpressionSyntax? invocation = generatorMethodSyntax
+ .DescendantNodes()
+ .OfType()
+ .FirstOrDefault(inv =>
+ inv.Expression is MemberAccessExpressionSyntax memberAccess &&
+ memberAccess.Name.Identifier.Text == "UseProvidedBody");
+
+ if (invocation == null)
+ {
+ return null;
+ }
+
+ ArgumentSyntax? argument = invocation.ArgumentList.Arguments.FirstOrDefault();
+ if (argument?.Expression is not LambdaExpressionSyntax lambda)
+ {
+ return null;
+ }
+
+ if (lambda.Body is ExpressionSyntax expression)
+ {
+ string expressionText = expression.ToFullString().Trim();
+ return expressionText;
+ }
+
+ if (lambda.Body is BlockSyntax block)
+ {
+ return ExtractBlockBody(block);
+ }
+
+ return null;
+ }
+
+ ///
+ /// Extracts the content of a block body (between { and }),
+ /// determines the base indentation, and re-indents all lines to the method body level.
+ /// Blank lines between statements are preserved with method body indentation.
+ ///
+ private static string? ExtractBlockBody(BlockSyntax block)
+ {
+ string blockText = block.ToFullString();
+ string[] lines = blockText.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
+
+ int openIndex = -1;
+ int closeIndex = -1;
+
+ for (int i = 0; i < lines.Length; i++)
+ {
+ if (openIndex == -1 && lines[i].TrimEnd().EndsWith("{", StringComparison.Ordinal))
+ {
+ openIndex = i;
+ break;
+ }
+ }
+
+ for (int i = lines.Length - 1; i >= 0; i--)
+ {
+ string trimmed = lines[i].Trim();
+ if (trimmed.StartsWith("}", StringComparison.Ordinal))
+ {
+ closeIndex = i;
+ break;
+ }
+ }
+
+ if (openIndex == -1 || closeIndex == -1 || closeIndex <= openIndex)
+ {
+ return null;
+ }
+
+ string[] contentLines = new string[closeIndex - openIndex - 1];
+ Array.Copy(lines, openIndex + 1, contentLines, 0, contentLines.Length);
+
+ if (contentLines.Length == 0)
+ {
+ return null;
+ }
+
+ int minIndent = int.MaxValue;
+ foreach (string line in contentLines)
+ {
+ if (string.IsNullOrWhiteSpace(line))
+ {
+ continue;
+ }
+
+ int indent = 0;
+ foreach (char c in line)
+ {
+ if (c == ' ')
+ {
+ indent++;
+ }
+ else if (c == '\t')
+ {
+ indent += 4;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ if (indent < minIndent)
+ {
+ minIndent = indent;
+ }
+ }
+
+ if (minIndent == int.MaxValue)
+ {
+ minIndent = 0;
+ }
+
+ StringBuilder result = new();
+ for (int i = 0; i < contentLines.Length; i++)
+ {
+ string line = contentLines[i];
+
+ if (string.IsNullOrWhiteSpace(line))
+ {
+ result.AppendLine(MethodBodyIndent);
+ }
+ else
+ {
+ string stripped = minIndent <= line.Length ? line.Substring(minIndent) : line.TrimStart();
+ string trimmedEnd = stripped.TrimEnd();
+ result.AppendLine(MethodBodyIndent + trimmedEnd);
+ }
+ }
+
+ return result.ToString().TrimEnd('\n', '\r');
+ }
+}
diff --git a/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodGenerationPipeline.cs b/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodGenerationPipeline.cs
index 802c97f..c622a39 100644
--- a/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodGenerationPipeline.cs
+++ b/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodGenerationPipeline.cs
@@ -96,8 +96,9 @@ private static string GenerateSourceForGroup(
}
///
- /// Generates source code from a fluent body pattern, executing the generator method
- /// and extracting the return value from the fluent API result.
+ /// Generates source code from a fluent body pattern. First attempts to extract the delegate
+ /// body from a UseProvidedBody call in the syntax tree. If no such call is found,
+ /// falls back to executing the generator method and extracting the return value.
///
private static string GenerateFromFluentBodyPattern(
SourceProductionContext context,
@@ -106,6 +107,18 @@ private static string GenerateFromFluentBodyPattern(
INamedTypeSymbol containingType,
Compilation compilation)
{
+ string? delegateBody = DelegateBodySyntaxExtractor.TryExtractDelegateBody(methodInfo.Syntax);
+ if (delegateBody != null)
+ {
+ bool isVoidReturn = partialMethod.ReturnType.SpecialType == SpecialType.System_Void;
+ string bodyLines = FormatDelegateBodyForEmit(delegateBody, isVoidReturn);
+
+ return GeneratesMethodPatternSourceBuilder.GeneratePartialMethodWithBody(
+ containingType,
+ partialMethod,
+ bodyLines);
+ }
+
(FluentBodyResult? result, string? error) = GeneratesMethodExecutionRuntime.ExecuteFluentBodyGeneratorMethod(
methodInfo.Symbol,
partialMethod,
@@ -127,6 +140,27 @@ private static string GenerateFromFluentBodyPattern(
result!.ReturnValue);
}
+ ///
+ /// Formats the extracted delegate body for emission. Expression bodies are wrapped in
+ /// return {expr};. Block bodies are used as-is (already re-indented by the extractor).
+ ///
+ private static string FormatDelegateBodyForEmit(string delegateBody, bool isVoidReturn)
+ {
+ bool isBlockBody = delegateBody.Contains("\n");
+
+ if (isBlockBody)
+ {
+ return delegateBody;
+ }
+
+ if (isVoidReturn)
+ {
+ return $" {delegateBody};";
+ }
+
+ return $" return {delegateBody};";
+ }
+
///
/// Generates source code from a simple pattern, executing the generator method
/// and using its return value as the partial method's return expression.
diff --git a/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodPatternSourceBuilder.cs b/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodPatternSourceBuilder.cs
index 215668b..2488e2d 100644
--- a/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodPatternSourceBuilder.cs
+++ b/EasySourceGenerators.Generators/IncrementalGenerators/GeneratesMethodPatternSourceBuilder.cs
@@ -28,6 +28,19 @@ internal static string GenerateSimplePartialMethod(
return PartialMethodSourceEmitter.Emit(emitData, returnValueLiteral);
}
+ ///
+ /// Generates a complete C# source file containing a partial method implementation
+ /// with the given body lines (already indented to the method body level).
+ ///
+ internal static string GeneratePartialMethodWithBody(
+ INamedTypeSymbol containingType,
+ IMethodSymbol partialMethod,
+ string bodyLines)
+ {
+ PartialMethodEmitData emitData = RoslynSymbolDataMapper.ToPartialMethodEmitData(containingType, partialMethod);
+ return PartialMethodSourceEmitter.EmitWithBody(emitData, bodyLines);
+ }
+
///
/// Formats a string value as a C# literal expression based on the target return type.
/// Delegates to .
diff --git a/EasySourceGenerators.Generators/SourceEmitting/PartialMethodSourceEmitter.cs b/EasySourceGenerators.Generators/SourceEmitting/PartialMethodSourceEmitter.cs
index ce557ae..4ea7d75 100644
--- a/EasySourceGenerators.Generators/SourceEmitting/PartialMethodSourceEmitter.cs
+++ b/EasySourceGenerators.Generators/SourceEmitting/PartialMethodSourceEmitter.cs
@@ -26,6 +26,19 @@ internal static string Emit(PartialMethodEmitData data, string? returnValueLiter
return builder.ToString();
}
+ ///
+ /// Generates a complete C# source file containing a partial method implementation
+ /// with the given body lines (already indented to the method body level).
+ ///
+ internal static string EmitWithBody(PartialMethodEmitData data, string bodyLines)
+ {
+ StringBuilder builder = new();
+ AppendFileHeader(builder, data);
+ builder.AppendLine(bodyLines);
+ AppendClosingBraces(builder);
+ return builder.ToString();
+ }
+
///
/// Appends the auto-generated file header, namespace declaration, type declaration,
/// and method signature opening to the .
diff --git a/EasySourceGenerators.Tests/DelegateBodyTests.cs b/EasySourceGenerators.Tests/DelegateBodyTests.cs
index 6cdd5a9..5fe5dc7 100644
--- a/EasySourceGenerators.Tests/DelegateBodyTests.cs
+++ b/EasySourceGenerators.Tests/DelegateBodyTests.cs
@@ -57,7 +57,7 @@ static partial class DelegateBodyTestClass_WithIf
{
public static partial int PartialMethod(int someParam)
{
- if (someParamHere > 0)
+ if (someParam > 0)
{
return 42;
}
@@ -83,7 +83,7 @@ static partial class DelegateBodyTestClass_WithSwitch
{
public static partial int PartialMethod(int someParam)
{
- switch (someParamHere)
+ switch (someParam)
{
case -1: return 6;
case 0: return 7;
@@ -110,7 +110,7 @@ public static partial int PartialMethod(int someParam)
{
int interResult = 0;
- switch (someParamHere)
+ switch (someParam)
{
case -1: interResult = 6; break;
case 0: interResult = 7; break;
@@ -157,9 +157,9 @@ public static partial class DelegateBodyTestClass_WithIf
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
Generate.MethodBody()
.ForMethod().WithReturnType().WithParameter()
- .UseProvidedBody(someParamHere =>
+ .UseProvidedBody(someParam =>
{
- if (someParamHere > 0)
+ if (someParam > 0)
{
return 42;
}
@@ -178,9 +178,9 @@ public static partial class DelegateBodyTestClass_WithSwitch
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
Generate.MethodBody()
.ForMethod().WithReturnType().WithParameter()
- .UseProvidedBody(someParamHere =>
+ .UseProvidedBody(someParam =>
{
- switch (someParamHere)
+ switch (someParam)
{
case -1: return 6;
case 0: return 7;
@@ -198,11 +198,11 @@ public static partial class DelegateBodyTestClass_WithComplexBody
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
Generate.MethodBody()
.ForMethod().WithReturnType().WithParameter()
- .UseProvidedBody(someParamHere =>
+ .UseProvidedBody(someParam =>
{
int interResult = 0;
- switch (someParamHere)
+ switch (someParam)
{
case -1: interResult = 6; break;
case 0: interResult = 7; break;
diff --git a/EasySourceGenerators.Tests/EasySourceGenerators.Tests.csproj b/EasySourceGenerators.Tests/EasySourceGenerators.Tests.csproj
index 80a8386..5e00169 100644
--- a/EasySourceGenerators.Tests/EasySourceGenerators.Tests.csproj
+++ b/EasySourceGenerators.Tests/EasySourceGenerators.Tests.csproj
@@ -25,6 +25,7 @@
+
From 53d5d99e9e38148d0bb754fac8fe692ffb5503d0 Mon Sep 17 00:00:00 2001
From: "mateusz.krzaczek"
Date: Wed, 25 Mar 2026 16:56:01 +0100
Subject: [PATCH 3/4] Compile time constats WIP
---
.../MethodBody/IMethodBodyBuilder.cs | 28 +++++++++++++-
EasySourceGenerators.Examples/PiExample.cs | 37 +++++++++---------
.../PiExampleFluent.cs | 38 -------------------
.../DelegateBodyTests.cs | 7 ++--
EasySourceGenerators.sln.DotSettings.user | 1 +
5 files changed, 51 insertions(+), 60 deletions(-)
delete mode 100644 EasySourceGenerators.Examples/PiExampleFluent.cs
diff --git a/EasySourceGenerators.Abstractions/MethodBody/IMethodBodyBuilder.cs b/EasySourceGenerators.Abstractions/MethodBody/IMethodBodyBuilder.cs
index 506b006..c972d20 100644
--- a/EasySourceGenerators.Abstractions/MethodBody/IMethodBodyBuilder.cs
+++ b/EasySourceGenerators.Abstractions/MethodBody/IMethodBodyBuilder.cs
@@ -27,23 +27,49 @@ public interface IMethodBodyBuilderStage3
public interface IMethodBodyBuilderStage4ReturnVoidNoArg
{
+ IMethodBodyBuilderStage5ReturnVoidNoArgWithConstants WithCompileTimeConstants(Func compileTimeConstantsFactory);
IMethodBodyGenerator UseProvidedBody(Action body);
}
+public interface IMethodBodyBuilderStage5ReturnVoidNoArgWithConstants
+{
+ IMethodBodyGenerator UseProvidedBody(Action body);
+}
+
public interface IMethodBodyBuilderStage4NoArg
{
+ IMethodBodyBuilderStage5NoArgWithConstants WithCompileTimeConstants(Func compileTimeConstantsFactory);
IMethodBodyGenerator UseProvidedBody(Func body);
IMethodBodyGenerator BodyReturningConstant(Func constantValueFactory);
}
+public interface IMethodBodyBuilderStage5NoArgWithConstants
+{
+ IMethodBodyGenerator UseProvidedBody(Func body);
+ IMethodBodyGenerator BodyReturningConstant(Func constantValueFactory);
+}
+
public interface IMethodBodyBuilderStage4ReturnVoid
{
+ IMethodBodyBuilderStage5ReturnVoidWithConstants WithCompileTimeConstants(Func compileTimeConstantsFactory);
IMethodBodyGenerator UseProvidedBody(Action body);
}
+public interface IMethodBodyBuilderStage5ReturnVoidWithConstants
+{
+ IMethodBodyGenerator UseProvidedBody(Action body);
+}
+
public interface IMethodBodyBuilderStage4
{
+ IMethodBodyBuilderStage5WithConstants WithCompileTimeConstants(Func compileTimeConstantsFactory);
+
IMethodBodyGenerator UseProvidedBody(Func body);
IMethodBodyGenerator BodyReturningConstant(Func constantValueFactory);
- IMethodBodyGeneratorSwitchBody BodyWithSwitchStatement();
+}
+
+public interface IMethodBodyBuilderStage5WithConstants
+{
+ IMethodBodyGenerator UseProvidedBody(Func body);
+ IMethodBodyGenerator BodyReturningConstant(Func constantValueFactory);
}
\ No newline at end of file
diff --git a/EasySourceGenerators.Examples/PiExample.cs b/EasySourceGenerators.Examples/PiExample.cs
index f901bfc..0a96bc9 100644
--- a/EasySourceGenerators.Examples/PiExample.cs
+++ b/EasySourceGenerators.Examples/PiExample.cs
@@ -1,28 +1,27 @@
-// SwitchCase/SwitchDefault attribute-based generation is commented out pending replacement with a data-driven approach.
-// See DataMethodBodyBuilders.cs for details on the planned replacement.
-
-/*
-using EasySourceGenerators.Abstractions;
+using EasySourceGenerators.Abstractions;
// ReSharper disable ConvertClosureToMethodGroup
namespace EasySourceGenerators.Examples;
-public static partial class PiExample
+public static partial class PiExampleFluent
{
public static partial int GetPiDecimal(int decimalNumber);
[MethodBodyGenerator(nameof(GetPiDecimal))]
- [SwitchCase(arg1: 0)]
- [SwitchCase(arg1: 1)]
- [SwitchCase(arg1: 2)]
- static int GetPiDecimal_Generator_Specialized(int decimalNumber) =>
- SlowMath.CalculatePiDecimal(decimalNumber);
-
- [MethodBodyGenerator(nameof(GetPiDecimal))]
- [SwitchDefault]
- static Func GetPiDecimal_Generator_Fallback() => decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber);
+ static IMethodBodyGenerator GetPiDecimal_Generator() =>
+ Generate.MethodBody()
+ .ForMethod().WithReturnType().WithParameter()
+ .WithCompileTimeConstants(() => new
+ {
+ PrecomputedTargets = (new int[] { 0, 1, 2, 300, 301, 302, 303 }).ToDictionary(i => i, i => SlowMath.CalculatePiDecimal(i))
+ })
+ .UseProvidedBody((constants, decimalNumber) =>
+ {
+ if (constants.PrecomputedTargets.TryGetValue(decimalNumber, out int precomputedResult)) return precomputedResult;
+
+ return SlowMath.CalculatePiDecimal(decimalNumber);
+ });
}
-*/
/*
This will generate the following method:
@@ -34,7 +33,11 @@ public static int GetPiDecimal(int decimalNumber)
case 0: return 3;
case 1: return 1;
case 2: return 4;
+ case 300: return 3;
+ case 301: return 7;
+ case 302: return 2;
+ case 303: return 4;
default: return CalculatePiDecimal(decimalNumber);
}
}
-*/
\ No newline at end of file
+*/
diff --git a/EasySourceGenerators.Examples/PiExampleFluent.cs b/EasySourceGenerators.Examples/PiExampleFluent.cs
deleted file mode 100644
index f49bb8c..0000000
--- a/EasySourceGenerators.Examples/PiExampleFluent.cs
+++ /dev/null
@@ -1,38 +0,0 @@
-//TODO: Replace SwitchCase with new version
-
-// using EasySourceGenerators.Abstractions;
-// // ReSharper disable ConvertClosureToMethodGroup
-//
-// namespace EasySourceGenerators.Examples;
-//
-// public static partial class PiExampleFluent
-// {
-// public static partial int GetPiDecimal(int decimalNumber);
-//
-// [MethodBodyGenerator(nameof(GetPiDecimal))]
-// static IMethodBodyGenerator GetPiDecimal_Generator() =>
-// Generate.MethodBody()
-// .ForMethod().WithReturnType().WithParameter()
-// .BodyWithSwitchStatement()
-// .ForCases(0, 1, 2, 300, 301, 302, 303).ReturnConstantValue(decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber))
-// .ForDefaultCase().UseProvidedBody(decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber));
-// }
-//
-// /*
-// This will generate the following method:
-//
-// public static int GetPiDecimal(int decimalNumber)
-// {
-// switch (decimalNumber)
-// {
-// case 0: return 3;
-// case 1: return 1;
-// case 2: return 4;
-// case 300: return 3;
-// case 301: return 7;
-// case 302: return 2;
-// case 303: return 4;
-// default: return CalculatePiDecimal(decimalNumber);
-// }
-// }
-// */
diff --git a/EasySourceGenerators.Tests/DelegateBodyTests.cs b/EasySourceGenerators.Tests/DelegateBodyTests.cs
index 5fe5dc7..10ebf05 100644
--- a/EasySourceGenerators.Tests/DelegateBodyTests.cs
+++ b/EasySourceGenerators.Tests/DelegateBodyTests.cs
@@ -1,5 +1,4 @@
-using System.Text;
-using EasySourceGenerators.Abstractions;
+using EasySourceGenerators.Abstractions;
// ReSharper disable InconsistentNaming
// ReSharper disable RedundantIfElseBlock
// ReSharper disable ConvertSwitchStatementToSwitchExpression
@@ -108,7 +107,7 @@ static partial class DelegateBodyTestClass_WithComplexBody
{
public static partial int PartialMethod(int someParam)
{
- int interResult = 0;
+ int interResult;
switch (someParam)
{
@@ -200,7 +199,7 @@ public static IMethodBodyGenerator JustReturnConstantGenerator() =>
.ForMethod().WithReturnType().WithParameter()
.UseProvidedBody(someParam =>
{
- int interResult = 0;
+ int interResult;
switch (someParam)
{
diff --git a/EasySourceGenerators.sln.DotSettings.user b/EasySourceGenerators.sln.DotSettings.user
index b83e4d6..3a5a825 100644
--- a/EasySourceGenerators.sln.DotSettings.user
+++ b/EasySourceGenerators.sln.DotSettings.user
@@ -6,6 +6,7 @@
ForceIncluded
ForceIncluded
ForceIncluded
+ ForceIncluded
ForceIncluded
True
From adb47c0204545c7dabad910c6a394bda53368151 Mon Sep 17 00:00:00 2001
From: "mateusz.krzaczek"
Date: Wed, 25 Mar 2026 16:58:42 +0100
Subject: [PATCH 4/4] Compile time constants WIP
---
.../DataBuilding/DataMethodBodyBuilders.cs | 33 -------------------
.../DataBuilding/DataRecords.cs | 3 +-
2 files changed, 2 insertions(+), 34 deletions(-)
diff --git a/EasySourceGenerators.Generators/DataBuilding/DataMethodBodyBuilders.cs b/EasySourceGenerators.Generators/DataBuilding/DataMethodBodyBuilders.cs
index 1f7c915..cef2554 100644
--- a/EasySourceGenerators.Generators/DataBuilding/DataMethodBodyBuilders.cs
+++ b/EasySourceGenerators.Generators/DataBuilding/DataMethodBodyBuilders.cs
@@ -42,39 +42,6 @@ public record DataMethodBodyBuilderStage4(BodyGenerationDa
public IMethodBodyGenerator BodyReturningConstant(Func constantValueFactory) =>
new DataMethodBodyGenerator(Data with { ReturnConstantValueFactory = constantValueFactory });
-
- public IMethodBodyGeneratorSwitchBody BodyWithSwitchStatement()
- => throw new NotImplementedException(); //TODO: Remove explicit SwitchStatements with `for` and `constants`, like so:
- /*
- .ForMethod().WithReturnType().WithParameter()
- .BodyWithSwitchStatement()
- .ForCases(0, 1, 2, 300, 301, 302, 303).ReturnConstantValue(decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber))
- .ForDefaultCase().UseProvidedBody(decimalNumber => SlowMath.CalculatePiDecimal(decimalNumber));
-
- Will be replaced with:
-
- .ForMethod().WithReturnType().WithParameter()
- .WithCompileTimeConstants(() => new
- {
- PrecomputedTargets = new HashSet(new int[] { 0, 1, 2, 300, 301, 302, 303 })
- })
- .UseProvidedBody(decimalNumber, constants =>
- {
- if (constants.PrecomputedTargets.Contains(decimalNumber)) return SlowMath.CalculatePiDecimal(decimalNumber);
- else return SlowMath.CalculatePiDecimal(decimalNumber);
- });
-
- Or (in the case of PI), just simply:
-
- .ForMethod().WithReturnType().WithParameter()
- .UseProvidedBody(decimalNumber =>
- {
- var precomputedTargets = new HashSet(new int[] { 0, 1, 2, 300, 301, 302, 303 })
-
- if (precomputedTargets.Contains(decimalNumber)) return SlowMath.CalculatePiDecimal(decimalNumber);
- else return SlowMath.CalculatePiDecimal(decimalNumber);
- });
- */
}
public record DataMethodBodyBuilderStage4NoArg(BodyGenerationData Data) : IMethodBodyBuilderStage4NoArg
diff --git a/EasySourceGenerators.Generators/DataBuilding/DataRecords.cs b/EasySourceGenerators.Generators/DataBuilding/DataRecords.cs
index 76f2fea..1c46424 100644
--- a/EasySourceGenerators.Generators/DataBuilding/DataRecords.cs
+++ b/EasySourceGenerators.Generators/DataBuilding/DataRecords.cs
@@ -6,5 +6,6 @@ public record BodyGenerationData(
Type? ReturnType = null,
Type[]? ParametersTypes = null,
Delegate? RuntimeDelegateBody = null,
- Delegate? ReturnConstantValueFactory = null
+ Delegate? ReturnConstantValueFactory = null,
+ object? CompileTimeConstants = null
);
\ No newline at end of file