From 2364623f3cb0b1681ba8f82e3489ae8d0367238d Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:37:17 -0600 Subject: [PATCH 01/17] Move fields to base class --- .../TensorContractionBuilder.cs | 13 ++---------- .../TensorContractionBuilderBase.cs | 20 +++++++++++++++++++ .../TensorSelfContractionBuilder.cs | 18 ----------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs index 747efad2..f399d87f 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs @@ -50,15 +50,6 @@ namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; /// Tensor contractions builder internal sealed class TensorContractionBuilder : TensorContractionBuilderBase { - private static readonly GenericNameSyntax s_indexToContract = GenericName( - Identifier("Index")) - .WithTypeArgumentList( - TypeArgumentList( - SeparatedList(new SyntaxNodeOrToken[] { - IdentifierName("Upper"), - Token(SyntaxKind.CommaToken), - IdentifierName("IC") }))); - public TensorContractionBuilder(SourceProductionContext context, ImmutableArray methodInformationArray) : base(context, methodInformationArray) { } @@ -257,7 +248,7 @@ private static MemberDeclarationSyntax ResetTypeParameterConstraints(MemberDecla .First(); var newArgs = args.RemoveNode(args.Arguments.Last(), SyntaxRemoveOptions.KeepNoTrivia); - newArgs = newArgs!.InsertNodesAfter(newArgs!.Arguments[2], [s_indexToContract]); + newArgs = newArgs!.InsertNodesAfter(newArgs!.Arguments[2], [s_rightIndex]); return memberDeclaration.ReplaceNode(args, newArgs); } @@ -268,7 +259,7 @@ private static MemberDeclarationSyntax ResetTypeParameters(MemberDeclarationSynt var args = param.TypeArgumentList()!; var newArgs = args.RemoveNode(args.Arguments.Last(), SyntaxRemoveOptions.KeepNoTrivia); - newArgs = newArgs!.InsertNodesAfter(newArgs!.Arguments[2], [s_indexToContract]); + newArgs = newArgs!.InsertNodesAfter(newArgs!.Arguments[2], [s_rightIndex]); return memberDeclaration.ReplaceNode(args, newArgs); } diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilderBase.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilderBase.cs index 7fd26c1a..63b48e42 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilderBase.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilderBase.cs @@ -26,12 +26,32 @@ // using System.Collections.Immutable; +using Microsoft.CodeAnalysis.CSharp; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; /// A base class for tensor contraction builders internal abstract class TensorContractionBuilderBase { + private protected static readonly GenericNameSyntax s_leftIndex = GenericName( + Identifier("Index")) + .WithTypeArgumentList( + TypeArgumentList( + SeparatedList(new SyntaxNodeOrToken[] { + IdentifierName("Lower"), + Token(SyntaxKind.CommaToken), + IdentifierName("IC") }))); + + private protected static readonly GenericNameSyntax s_rightIndex = GenericName( + Identifier("Index")) + .WithTypeArgumentList( + TypeArgumentList( + SeparatedList(new SyntaxNodeOrToken[] { + IdentifierName("Upper"), + Token(SyntaxKind.CommaToken), + IdentifierName("IC") }))); + private protected readonly SourceProductionContext _context; private protected readonly ImmutableArray _methodInformationArray; diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs index 6bdf6bfd..cc71aa17 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs @@ -35,24 +35,6 @@ namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; /// Tensor self contractions builder internal sealed class TensorSelfContractionBuilder : TensorContractionBuilderBase { - private static readonly GenericNameSyntax s_leftIndex = GenericName( - Identifier("Index")) - .WithTypeArgumentList( - TypeArgumentList( - SeparatedList(new SyntaxNodeOrToken[] { - IdentifierName("Lower"), - Token(SyntaxKind.CommaToken), - IdentifierName("IC") }))); - - private static readonly GenericNameSyntax s_rightIndex = GenericName( - Identifier("Index")) - .WithTypeArgumentList( - TypeArgumentList( - SeparatedList(new SyntaxNodeOrToken[] { - IdentifierName("Upper"), - Token(SyntaxKind.CommaToken), - IdentifierName("IC") }))); - public TensorSelfContractionBuilder(SourceProductionContext context, ImmutableArray methodInformationArray) : base(context, methodInformationArray) { } From a8593e47edb1824b1e1d2c5b554925fbbcbf489b Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:47:13 -0600 Subject: [PATCH 02/17] Update Extensions.cs - Rename parameter --- src/Mathematics.NET.SourceGenerators/Extensions.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/Extensions.cs b/src/Mathematics.NET.SourceGenerators/Extensions.cs index 854ea5ea..164c5454 100644 --- a/src/Mathematics.NET.SourceGenerators/Extensions.cs +++ b/src/Mathematics.NET.SourceGenerators/Extensions.cs @@ -35,12 +35,12 @@ namespace Mathematics.NET.SourceGenerators; internal static class Extensions { /// Create a name syntax from a namespace. - /// A string representing a namespace + /// A string representing a namespace /// A name syntax - public static NameSyntax CreateNameSyntaxFromNamespace(this string namespaceName) + public static NameSyntax CreateNameSyntaxFromNamespace(this string namespaceString) { - Debug.Assert(!namespaceName.Contains(' '), "The namespace string must not contain any spaces."); - ReadOnlySpan names = namespaceName.Split('.'); + Debug.Assert(!namespaceString.Contains(' '), "The namespace string must not contain any spaces."); + ReadOnlySpan names = namespaceString.Split('.'); NameSyntax result = IdentifierName(names[0]); for (int i = 1; i < names.Length; i++) From 37a113917e478c0b41ca9941d08385268e691da9 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:47:46 -0600 Subject: [PATCH 03/17] Update TensorContractionGenerator.cs - Remove unused parameter --- .../DifferentialGeometry/TensorContractionGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionGenerator.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionGenerator.cs index c65298b7..db3a8946 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionGenerator.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionGenerator.cs @@ -40,7 +40,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .CreateSyntaxProvider(CouldBeGenerateTensorContractionAttribute, GetTensorContractionOrNull) .Where(x => x is not null); var compilation = context.CompilationProvider.Combine(provider.Collect()); - context.RegisterSourceOutput(compilation, (context, source) => GenerateCode(context, source.Left, source.Right)); + context.RegisterSourceOutput(compilation, (context, source) => GenerateCode(context, source.Right)); } private static bool CouldBeGenerateTensorContractionAttribute(SyntaxNode syntaxNode, CancellationToken token) @@ -53,7 +53,7 @@ private static MethodInformation GetTensorContractionOrNull(GeneratorSyntaxConte return new(attribute, (MethodDeclarationSyntax)attribute.Parent!.Parent!); } - private void GenerateCode(SourceProductionContext context, Compilation compilation, ImmutableArray information) + private void GenerateCode(SourceProductionContext context, ImmutableArray information) { var tensorContractions = new TensorContractionBuilder(context, information); context.AddSource("DifGeo.Contractions.g.cs", tensorContractions.GenerateSource().GetText(Encoding.UTF8).ToString()); From 9755018cf8d324d4eb8cfef7636e26bc7d7aaeaa Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:48:06 -0600 Subject: [PATCH 04/17] Update TensorSelfContractionGenerator.cs - Remove unused parameter --- .../DifferentialGeometry/TensorSelfContractionGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionGenerator.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionGenerator.cs index 982866fb..071a02aa 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionGenerator.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionGenerator.cs @@ -40,7 +40,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .CreateSyntaxProvider(CouldBeTensorSelfContractionAttribute, GetTensorSelfContractionOrNull) .Where(x => x is not null); var compilation = context.CompilationProvider.Combine(provider.Collect()); - context.RegisterSourceOutput(compilation, (context, source) => GenerateCode(context, source.Left, source.Right)); + context.RegisterSourceOutput(compilation, (context, source) => GenerateCode(context, source.Right)); } private static bool CouldBeTensorSelfContractionAttribute(SyntaxNode syntaxNode, CancellationToken token) @@ -53,7 +53,7 @@ private static MethodInformation GetTensorSelfContractionOrNull(GeneratorSyntaxC return new(attribute, (MethodDeclarationSyntax)attribute.Parent!.Parent!); } - private void GenerateCode(SourceProductionContext context, Compilation compilation, ImmutableArray information) + private void GenerateCode(SourceProductionContext context, ImmutableArray information) { var tensorSelfContractions = new TensorSelfContractionBuilder(context, information); context.AddSource("DifGeo.SelfContractions.g.cs", tensorSelfContractions.GenerateSource().GetText(Encoding.UTF8).ToString()); From d2cb61cdad73a81e6d150c1ed4e5c1730d946f47 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:36:11 -0600 Subject: [PATCH 05/17] Update NameSyntaxComparer.cs --- .../NameSyntaxComparer.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators.Public/NameSyntaxComparer.cs b/src/Mathematics.NET.SourceGenerators.Public/NameSyntaxComparer.cs index 4f01a69d..a34ea566 100644 --- a/src/Mathematics.NET.SourceGenerators.Public/NameSyntaxComparer.cs +++ b/src/Mathematics.NET.SourceGenerators.Public/NameSyntaxComparer.cs @@ -35,13 +35,7 @@ public bool Equals(NameSyntax? x, NameSyntax? y) { return x.GetNameValueOrDefault() == y.GetNameValueOrDefault(); } - - if (x is null && y is null) - { - return true; - } - - return false; + return x == y; } public int GetHashCode(NameSyntax? obj) => obj?.GetNameValueOrDefault()?.GetHashCode() ?? 0; From 181d91a7672f36b2b7d4d7d4475d693e5ac0c8bb Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 14:46:19 -0600 Subject: [PATCH 06/17] Update SymbolGenerator.cs - Fix a bug where a symbol that was not defined in a namespace would stop the generating process for correctly-defined symbols. --- .../Symbols/SymbolGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs b/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs index a79c8e0e..e8331c09 100644 --- a/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs +++ b/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs @@ -73,7 +73,7 @@ private void GenerateCode(SourceProductionContext context, ImmutableArray Date: Wed, 31 Jan 2024 14:49:50 -0600 Subject: [PATCH 07/17] Update SymbolGenerator.cs - Remove null-forgiving operator since it is not necessary --- .../Symbols/SymbolGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs b/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs index e8331c09..c111fac9 100644 --- a/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs +++ b/src/Mathematics.NET.SourceGenerators.Public/Symbols/SymbolGenerator.cs @@ -75,7 +75,7 @@ private void GenerateCode(SourceProductionContext context, ImmutableArray Date: Wed, 31 Jan 2024 15:46:54 -0600 Subject: [PATCH 08/17] Update Extensions.cs - Remove new line character that appears with symbols generated in non-file-scoped namespaces. --- src/Mathematics.NET.SourceGenerators.Public/Extensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mathematics.NET.SourceGenerators.Public/Extensions.cs b/src/Mathematics.NET.SourceGenerators.Public/Extensions.cs index 658ff31a..e98232f4 100644 --- a/src/Mathematics.NET.SourceGenerators.Public/Extensions.cs +++ b/src/Mathematics.NET.SourceGenerators.Public/Extensions.cs @@ -81,7 +81,7 @@ public static NameSyntax CreateNameSyntaxFromNamespace(this string namespaceName if (structDeclaration.Parent is NamespaceDeclarationSyntax namespaceDeclaration) { - return namespaceDeclaration.Name; + return namespaceDeclaration.Name.WithoutTrailingTrivia(); } return default; From 25e04cfb349522e9be994ad3f1451d57fb6b0483 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 15:53:15 -0600 Subject: [PATCH 09/17] Fix test --- ...or_SymbolNotDeclaredInANamespace_ReturnsAnError.verified.txt | 2 +- .../Symbols/SymbolGeneratorTests.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/Snapshots/SymbolGeneratorTests.SourceGenerator_SymbolNotDeclaredInANamespace_ReturnsAnError.verified.txt b/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/Snapshots/SymbolGeneratorTests.SourceGenerator_SymbolNotDeclaredInANamespace_ReturnsAnError.verified.txt index 9241acc3..40c5da43 100644 --- a/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/Snapshots/SymbolGeneratorTests.SourceGenerator_SymbolNotDeclaredInANamespace_ReturnsAnError.verified.txt +++ b/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/Snapshots/SymbolGeneratorTests.SourceGenerator_SymbolNotDeclaredInANamespace_ReturnsAnError.verified.txt @@ -5,7 +5,7 @@ Title: Invalid symbol declaration, Severity: Error, WarningLevel: 0, - Location: : (0,31)-(0,36), + Location: : (2,31)-(2,36), HelpLink: https://mathematics.hamlettanyavong.com/guide/diagnostic_messages/symbols/sgs0001.html, MessageFormat: Symbols must be declared in namespaces., Message: Symbols must be declared in namespaces., diff --git a/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/SymbolGeneratorTests.cs b/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/SymbolGeneratorTests.cs index a2cf3228..e8c69e97 100644 --- a/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/SymbolGeneratorTests.cs +++ b/tests/Mathematics.NET.Tests.SourceGenerators.Public/Symbols/SymbolGeneratorTests.cs @@ -73,6 +73,8 @@ namespace B.C.D public void SourceGenerator_SymbolNotDeclaredInANamespace_ReturnsAnError() { var source = """ + namespace A { } + [Symbol] public partial struct Alpha; """; From bae142b9cb8f221a1a7a55a189fb399ef2a92dfe Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 16:50:46 -0600 Subject: [PATCH 10/17] Create index swapper - Create TypeArgumentIndexSwapRewriter - Use the new rewriter to simplify index swapping process - Remove code that is no longer needed - Create new method for swapping indices --- .../DifGeoGeneratorExtensions.cs | 36 ++--------- .../Models/IndexStructure.cs | 42 ------------- .../TensorContractionBuilder.cs | 14 ++--- .../TensorSelfContractionBuilder.cs | 15 ++--- .../TypeArgumentIndexSwapRewriter.cs | 63 +++++++++++++++++++ 5 files changed, 79 insertions(+), 91 deletions(-) delete mode 100644 src/Mathematics.NET.SourceGenerators/DifferentialGeometry/Models/IndexStructure.cs create mode 100644 src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TypeArgumentIndexSwapRewriter.cs diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs index 0c4a7519..be452268 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs @@ -25,10 +25,6 @@ // SOFTWARE. // -using System.Runtime.CompilerServices; -using Mathematics.NET.SourceGenerators.DifferentialGeometry.Models; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; /// Syntax helper for differential geometry generators @@ -43,36 +39,12 @@ internal static MemberDeclarationSyntax GenerateTwinContraction(this MemberDecla return (MemberDeclarationSyntax)walker.Visit(memberDeclaration); } - /// Get the index structure of a tensor. - /// A member declaration syntax - /// An integer representing the current parameter position—the position of the tensor in question in the parameter list - /// An index structure - internal static IndexStructure GetIndexStructure(this MemberDeclarationSyntax memberDeclaration, int position) - { - if (memberDeclaration.ParameterList() is ParameterListSyntax paramList) - { - var args = paramList.Parameters[position].TypeArgumentList()!.Arguments; - var index = args.IndexOf(args.Last(x => x is GenericNameSyntax name && name.Identifier.Text == "Index")); - return new(index, args.Count); - } - return default; - } - - /// Swap the current index with the index immediately to its right. + /// Swap the index to contract with the index immediately to its right. /// A type argument list syntax - /// An integer representing the current index position /// A type argument list syntax with the specified indices swapped - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal static TypeArgumentListSyntax SwapCurrentIndexWithNextIndex(this TypeArgumentListSyntax typeArgumentListSyntax, int index) + internal static TypeArgumentListSyntax SwapContractIndexWithNextIndex(this TypeArgumentListSyntax typeArgumentListSyntax) { - var args = typeArgumentListSyntax.Arguments; - var currentIndex = args[index]; - var nextIndex = args[index + 1]; - - var newArgs = args.Replace(currentIndex, nextIndex); - nextIndex = newArgs[index + 1]; - newArgs = newArgs.Replace(nextIndex, currentIndex); - - return TypeArgumentList(newArgs); + TypeArgumentIndexSwapRewriter rewriter = new(typeArgumentListSyntax); + return (TypeArgumentListSyntax)rewriter.Visit(typeArgumentListSyntax); } } diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/Models/IndexStructure.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/Models/IndexStructure.cs deleted file mode 100644 index 96d0d1b1..00000000 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/Models/IndexStructure.cs +++ /dev/null @@ -1,42 +0,0 @@ -// -// Mathematics.NET -// https://github.com/HamletTanyavong/Mathematics.NET -// -// MIT License -// -// Copyright (c) 2023 Hamlet Tanyavong -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// - -namespace Mathematics.NET.SourceGenerators.DifferentialGeometry.Models; - -/// Represents the index structure of a tensor -internal readonly record struct IndexStructure -{ - public IndexStructure(int position, int indexCount) - { - ContractPosition = position; - IndexCount = indexCount; - } - - public readonly int ContractPosition { get; } - - public readonly int IndexCount { get; } -} diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs index f399d87f..65a770a5 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs @@ -265,10 +265,8 @@ private static MemberDeclarationSyntax ResetTypeParameters(MemberDeclarationSynt private static MemberDeclarationSyntax SwapIndices(MemberDeclarationSyntax memberDeclaration, IndexPosition position) { - var indexStructure = memberDeclaration.GetIndexStructure((int)position); - - memberDeclaration = SwapTypeParameters(memberDeclaration, position, indexStructure); - memberDeclaration = SwapTypeParameterConstraints(memberDeclaration, position, indexStructure); + memberDeclaration = SwapTypeParameters(memberDeclaration, position); + memberDeclaration = SwapTypeParameterConstraints(memberDeclaration, position); memberDeclaration = SwapMultiplyExpressionComponents(memberDeclaration, position); return memberDeclaration; @@ -299,7 +297,7 @@ private static MemberDeclarationSyntax SwapMultiplyExpressionComponents(MemberDe private static MemberDeclarationSyntax SwapRightIndices(MemberDeclarationSyntax memberDeclaration) => SwapIndices(memberDeclaration, IndexPosition.Right); - private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclarationSyntax memberDeclaration, IndexPosition position, IndexStructure indexStructure) + private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclarationSyntax memberDeclaration, IndexPosition position) { var constraints = memberDeclaration .ChildNodes() @@ -311,17 +309,17 @@ private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclar .OfType() .First(); - var newArgs = args.SwapCurrentIndexWithNextIndex(indexStructure.ContractPosition); + var newArgs = args.SwapContractIndexWithNextIndex(); var newConstraints = constraints.ReplaceNode(args, newArgs); return memberDeclaration.ReplaceNode(constraints, newConstraints); } - private static MemberDeclarationSyntax SwapTypeParameters(MemberDeclarationSyntax memberDeclaration, IndexPosition position, IndexStructure indexStructure) + private static MemberDeclarationSyntax SwapTypeParameters(MemberDeclarationSyntax memberDeclaration, IndexPosition position) { var param = memberDeclaration.ParameterList()!.Parameters[(int)position]; var args = param.TypeArgumentList()!; - var newParam = param.ReplaceNode(args, args.SwapCurrentIndexWithNextIndex(indexStructure.ContractPosition)); + var newParam = param.ReplaceNode(args, args.SwapContractIndexWithNextIndex()); return memberDeclaration.ReplaceNode(param, newParam); } } diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs index cc71aa17..25daad24 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs @@ -26,7 +26,6 @@ // using System.Collections.Immutable; -using Mathematics.NET.SourceGenerators.DifferentialGeometry.Models; using Microsoft.CodeAnalysis.CSharp; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -253,10 +252,8 @@ private static MemberDeclarationSyntax ResetTypeParameters(MemberDeclarationSynt private static MemberDeclarationSyntax SwapIndices(MemberDeclarationSyntax memberDeclaration) { - var indexStructure = memberDeclaration.GetIndexStructure(0); - - memberDeclaration = SwapTypeParameters(memberDeclaration, indexStructure); - memberDeclaration = SwapTypeParameterConstraints(memberDeclaration, indexStructure); + memberDeclaration = SwapTypeParameters(memberDeclaration); + memberDeclaration = SwapTypeParameterConstraints(memberDeclaration); memberDeclaration = SwapElementAccessComponents(memberDeclaration); return memberDeclaration; @@ -279,7 +276,7 @@ private static MemberDeclarationSyntax SwapElementAccessComponents(MemberDeclara return memberDeclaration.ReplaceNode(args, newArgs); } - private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclarationSyntax memberDeclaration, IndexStructure indexStructure) + private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclarationSyntax memberDeclaration) { var constraints = memberDeclaration .ChildNodes() @@ -290,17 +287,17 @@ private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclar .OfType() .First(); - var newArgs = args.SwapCurrentIndexWithNextIndex(indexStructure.ContractPosition); + var newArgs = args.SwapContractIndexWithNextIndex(); var newConstraints = constraints.ReplaceNode(args, newArgs); return memberDeclaration.ReplaceNode(constraints, newConstraints); } - private static MemberDeclarationSyntax SwapTypeParameters(MemberDeclarationSyntax memberDeclaration, IndexStructure indexStructure) + private static MemberDeclarationSyntax SwapTypeParameters(MemberDeclarationSyntax memberDeclaration) { var param = memberDeclaration.ParameterList()!.Parameters[0]; var args = param.TypeArgumentList()!; - var newParam = param.ReplaceNode(args, args.SwapCurrentIndexWithNextIndex(indexStructure.ContractPosition)); + var newParam = param.ReplaceNode(args, args.SwapContractIndexWithNextIndex()); return memberDeclaration.ReplaceNode(param, newParam); } } diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TypeArgumentIndexSwapRewriter.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TypeArgumentIndexSwapRewriter.cs new file mode 100644 index 00000000..5191e6bb --- /dev/null +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TypeArgumentIndexSwapRewriter.cs @@ -0,0 +1,63 @@ +// +// Mathematics.NET +// https://github.com/HamletTanyavong/Mathematics.NET +// +// MIT License +// +// Copyright (c) 2023 Hamlet Tanyavong +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +// + +using Microsoft.CodeAnalysis.CSharp; + +namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; + +/// A C# syntax rewriter that swaps indices in a type argument list +internal sealed class TypeArgumentIndexSwapRewriter : CSharpSyntaxRewriter +{ + GenericNameSyntax? _indexToContract; + IdentifierNameSyntax? _indexToSwap; + + // Since the index to contract should always be to the left of the index to swap, we + // can look to the immediate right of the index to contract to find the index to swap. + public TypeArgumentIndexSwapRewriter(TypeArgumentListSyntax typeArgumentList) + { + _indexToContract = (GenericNameSyntax)typeArgumentList.Arguments.Last(x => x is GenericNameSyntax name && name.Identifier.Text == "Index"); + _indexToSwap = (IdentifierNameSyntax)typeArgumentList.Arguments.SkipWhile(x => x != _indexToContract).Skip(1).First(); + } + + public override SyntaxNode? VisitGenericName(GenericNameSyntax node) + { + if (node == _indexToContract) + { + return _indexToSwap; + } + return node; + } + + public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) + { + if (node == _indexToSwap) + { + return _indexToContract; + } + return node; + } +} From 20b7497f631f2bc659aae8c16b11d23c210c0bf0 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:02:05 -0600 Subject: [PATCH 11/17] Rename rewriter --- ...pRewriter.cs => BracketedArgumentIndexSwapRewriter.cs} | 8 ++++---- .../DifferentialGeometry/TensorContractionBuilder.cs | 2 +- .../DifferentialGeometry/TensorSelfContractionBuilder.cs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/Mathematics.NET.SourceGenerators/DifferentialGeometry/{IndexSwapRewriter.cs => BracketedArgumentIndexSwapRewriter.cs} (85%) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/IndexSwapRewriter.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/BracketedArgumentIndexSwapRewriter.cs similarity index 85% rename from src/Mathematics.NET.SourceGenerators/DifferentialGeometry/IndexSwapRewriter.cs rename to src/Mathematics.NET.SourceGenerators/DifferentialGeometry/BracketedArgumentIndexSwapRewriter.cs index 68bfdac2..05148bdb 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/IndexSwapRewriter.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/BracketedArgumentIndexSwapRewriter.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -29,13 +29,13 @@ namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; -/// A C# syntax rewriter that swaps tensor index orders -internal sealed class IndexSwapRewriter : CSharpSyntaxRewriter +/// A C# syntax rewriter that swaps indices in a bracketed argument list +internal sealed class BracketedArgumentIndexSwapRewriter : CSharpSyntaxRewriter { private readonly ArgumentSyntax _indexToContract; private readonly ArgumentSyntax _indexToSwap; - public IndexSwapRewriter(BracketedArgumentListSyntax bracketedArgumentList, string indexName) + public BracketedArgumentIndexSwapRewriter(BracketedArgumentListSyntax bracketedArgumentList, string indexName) { _indexToContract = bracketedArgumentList.Arguments.Last(x => x.Expression is IdentifierNameSyntax name && name.Identifier.Text == indexName); _indexToSwap = GetIndexToSwap(bracketedArgumentList); diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs index 65a770a5..09e7fc3c 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs @@ -288,7 +288,7 @@ private static MemberDeclarationSyntax SwapMultiplyExpressionComponents(MemberDe var args = position == IndexPosition.Left ? multiplyExpression.Left.DescendantNodes().OfType().First() : multiplyExpression.Right.DescendantNodes().OfType().First(); - var indexSwapper = new IndexSwapRewriter(args, variableName); + var indexSwapper = new BracketedArgumentIndexSwapRewriter(args, variableName); var newArgs = (BracketedArgumentListSyntax)indexSwapper.Visit(args); return memberDeclaration.ReplaceNode(args, newArgs); diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs index 25daad24..169ec4dd 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs @@ -270,7 +270,7 @@ private static MemberDeclarationSyntax SwapElementAccessComponents(MemberDeclara var variableName = forStatement.Declaration!.Variables[0].Identifier.Text; var args = addAssignmentExpression.Right.ChildNodes().OfType().First(); - var indexSwapper = new IndexSwapRewriter(args, variableName); + var indexSwapper = new BracketedArgumentIndexSwapRewriter(args, variableName); var newArgs = (BracketedArgumentListSyntax)indexSwapper.Visit(args); return memberDeclaration.ReplaceNode(args, newArgs); From 5180d0cbb0480010f37798069245c8a3d40749af Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:33:36 -0600 Subject: [PATCH 12/17] Update DifGeoGeneratorExtensions.cs - Add method for swapping indices --- .../DifferentialGeometry/DifGeoGeneratorExtensions.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs index be452268..f1ca7eb4 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs @@ -47,4 +47,14 @@ internal static TypeArgumentListSyntax SwapContractIndexWithNextIndex(this TypeA TypeArgumentIndexSwapRewriter rewriter = new(typeArgumentListSyntax); return (TypeArgumentListSyntax)rewriter.Visit(typeArgumentListSyntax); } + + /// Swap the index to contract with the index immediately to its right. + /// A bracketed argument list syntax + /// The name of the iteration index + /// A bracketed argument list syntax with the specified indices swapped + internal static BracketedArgumentListSyntax SwapIterationIndexWithNextIndex(this BracketedArgumentListSyntax bracketedArgumentListSyntax, string iterationIndexName) + { + BracketedArgumentIndexSwapRewriter rewriter = new(bracketedArgumentListSyntax, iterationIndexName); + return (BracketedArgumentListSyntax)rewriter.Visit(bracketedArgumentListSyntax); + } } From a96c5f9250ce4ae9460727b6d14ab867ed3162a4 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:33:50 -0600 Subject: [PATCH 13/17] Update TensorContractionBuilder.cs --- .../DifferentialGeometry/TensorContractionBuilder.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs index 09e7fc3c..8fb34778 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs @@ -288,10 +288,7 @@ private static MemberDeclarationSyntax SwapMultiplyExpressionComponents(MemberDe var args = position == IndexPosition.Left ? multiplyExpression.Left.DescendantNodes().OfType().First() : multiplyExpression.Right.DescendantNodes().OfType().First(); - var indexSwapper = new BracketedArgumentIndexSwapRewriter(args, variableName); - var newArgs = (BracketedArgumentListSyntax)indexSwapper.Visit(args); - - return memberDeclaration.ReplaceNode(args, newArgs); + return memberDeclaration.ReplaceNode(args, args.SwapIterationIndexWithNextIndex(variableName)); } private static MemberDeclarationSyntax SwapRightIndices(MemberDeclarationSyntax memberDeclaration) From 70c3bb7baa93b4a634be169e483f82aa98cc74be Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:33:53 -0600 Subject: [PATCH 14/17] Update TensorSelfContractionBuilder.cs --- .../DifferentialGeometry/TensorSelfContractionBuilder.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs index 169ec4dd..bb393dae 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs @@ -268,12 +268,8 @@ private static MemberDeclarationSyntax SwapElementAccessComponents(MemberDeclara var forStatement = (ForStatementSyntax)addAssignmentExpression.Parent!.Parent!.Parent!; var variableName = forStatement.Declaration!.Variables[0].Identifier.Text; - var args = addAssignmentExpression.Right.ChildNodes().OfType().First(); - var indexSwapper = new BracketedArgumentIndexSwapRewriter(args, variableName); - var newArgs = (BracketedArgumentListSyntax)indexSwapper.Visit(args); - - return memberDeclaration.ReplaceNode(args, newArgs); + return memberDeclaration.ReplaceNode(args, args.SwapIterationIndexWithNextIndex(variableName)); } private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclarationSyntax memberDeclaration) From 6c3533b9a4f544960330c3c137b882e0b55440c1 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:36:04 -0600 Subject: [PATCH 15/17] Rename rewriter --- .../DifferentialGeometry/DifGeoGeneratorExtensions.cs | 2 +- .../{FlipIndexRewriter.cs => FlipIndexPositionRewriter.cs} | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename src/Mathematics.NET.SourceGenerators/DifferentialGeometry/{FlipIndexRewriter.cs => FlipIndexPositionRewriter.cs} (87%) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs index f1ca7eb4..b8cc7745 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/DifGeoGeneratorExtensions.cs @@ -35,7 +35,7 @@ internal static class DifGeoGeneratorExtensions /// A member declaration syntax internal static MemberDeclarationSyntax GenerateTwinContraction(this MemberDeclarationSyntax memberDeclaration) { - FlipIndexRewriter walker = new(); + FlipIndexPositionRewriter walker = new(); return (MemberDeclarationSyntax)walker.Visit(memberDeclaration); } diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/FlipIndexRewriter.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/FlipIndexPositionRewriter.cs similarity index 87% rename from src/Mathematics.NET.SourceGenerators/DifferentialGeometry/FlipIndexRewriter.cs rename to src/Mathematics.NET.SourceGenerators/DifferentialGeometry/FlipIndexPositionRewriter.cs index 20ce6134..6c002bd3 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/FlipIndexRewriter.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/FlipIndexPositionRewriter.cs @@ -1,4 +1,4 @@ -// +// // Mathematics.NET // https://github.com/HamletTanyavong/Mathematics.NET // @@ -29,8 +29,8 @@ namespace Mathematics.NET.SourceGenerators.DifferentialGeometry; -/// A syntax walker that flips lower indices to upper indices and vice versa -internal sealed class FlipIndexRewriter : CSharpSyntaxRewriter +/// A C# syntax rewriter that flips lower indices to upper indices and vice versa +internal sealed class FlipIndexPositionRewriter : CSharpSyntaxRewriter { private static readonly IdentifierNameSyntax s_lower = SyntaxFactory.IdentifierName("Lower"); private static readonly IdentifierNameSyntax s_upper = SyntaxFactory.IdentifierName("Upper"); From 545a8c4d1c7c0941a9b8ae5a610fa5a75be04b13 Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:37:37 -0600 Subject: [PATCH 16/17] Update TensorContractionBuilder.cs - Rename variable --- .../DifferentialGeometry/TensorContractionBuilder.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs index 8fb34778..7cccc71c 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorContractionBuilder.cs @@ -283,12 +283,11 @@ private static MemberDeclarationSyntax SwapMultiplyExpressionComponents(MemberDe .First(x => x.IsKind(SyntaxKind.MultiplyExpression)); var forStatement = (ForStatementSyntax)multiplyExpression.Parent!.Parent!.Parent!.Parent!; - var variableName = forStatement.Declaration!.Variables[0].Identifier.Text; - + var iterationIndexName = forStatement.Declaration!.Variables[0].Identifier.Text; var args = position == IndexPosition.Left ? multiplyExpression.Left.DescendantNodes().OfType().First() : multiplyExpression.Right.DescendantNodes().OfType().First(); - return memberDeclaration.ReplaceNode(args, args.SwapIterationIndexWithNextIndex(variableName)); + return memberDeclaration.ReplaceNode(args, args.SwapIterationIndexWithNextIndex(iterationIndexName)); } private static MemberDeclarationSyntax SwapRightIndices(MemberDeclarationSyntax memberDeclaration) From 2c7861e55e7235da6e21cad312429b674c18911a Mon Sep 17 00:00:00 2001 From: Hamlet Tanyavong <34531738+HamletTanyavong@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:37:46 -0600 Subject: [PATCH 17/17] Update TensorSelfContractionBuilder.cs - Rename variable --- .../DifferentialGeometry/TensorSelfContractionBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs index bb393dae..2fe3f1e8 100644 --- a/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs +++ b/src/Mathematics.NET.SourceGenerators/DifferentialGeometry/TensorSelfContractionBuilder.cs @@ -267,9 +267,9 @@ private static MemberDeclarationSyntax SwapElementAccessComponents(MemberDeclara .First(x => x.IsKind(SyntaxKind.AddAssignmentExpression)); var forStatement = (ForStatementSyntax)addAssignmentExpression.Parent!.Parent!.Parent!; - var variableName = forStatement.Declaration!.Variables[0].Identifier.Text; + var iterationIndexName = forStatement.Declaration!.Variables[0].Identifier.Text; var args = addAssignmentExpression.Right.ChildNodes().OfType().First(); - return memberDeclaration.ReplaceNode(args, args.SwapIterationIndexWithNextIndex(variableName)); + return memberDeclaration.ReplaceNode(args, args.SwapIterationIndexWithNextIndex(iterationIndexName)); } private static MemberDeclarationSyntax SwapTypeParameterConstraints(MemberDeclarationSyntax memberDeclaration)