Skip to content

Commit 2b55e2e

Browse files
committed
Add [Pure] on all (appropriate) generated methods
Closes #102
1 parent f79716f commit 2b55e2e

13 files changed

+43
-1
lines changed

src/Directory.Build.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<PackageReference Update="CodeGeneration.Roslyn.BuildTime" Version="$(CodeGenerationRoslynVersion)" />
99
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.9.0" />
1010
<PackageReference Update="System.Collections.Immutable" Version="1.5.0" />
11+
<PackageReference Update="System.Diagnostics.Contracts" Version="4.3.0" />
1112
<PackageReference Update="Validation" Version="2.4.18" />
1213
<PackageReference Update="Microsoft.Build" Version="14.3.0" />
1314
<PackageReference Update="xunit" Version="2.3.1" />

src/ImmutableObjectGraph.Generation/CodeGen+BuilderGen.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected MethodDeclarationSyntax CreateToBuilderMethod()
8787
BuilderTypeName,
8888
ToBuilderMethodName.Identifier)
8989
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
90+
.AddAttributeLists(PureAttributeList)
9091
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
9192
SyntaxFactory.ObjectCreationExpression(
9293
BuilderTypeName,
@@ -110,6 +111,7 @@ protected MethodDeclarationSyntax CreateCreateBuilderMethod()
110111
.AddModifiers(
111112
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
112113
SyntaxFactory.Token(SyntaxKind.StaticKeyword))
114+
.AddAttributeLists(PureAttributeList)
113115
.WithExpressionBody(SyntaxFactory.ArrowExpressionClause(
114116
SyntaxFactory.ObjectCreationExpression(
115117
BuilderTypeName,
@@ -378,6 +380,7 @@ select SyntaxFactory.LocalDeclarationStatement(
378380
SyntaxFactory.IdentifierName(this.generator.applyTo.Identifier),
379381
ToImmutableMethodName.Identifier)
380382
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
383+
.AddAttributeLists(PureAttributeList)
381384
.WithBody(body);
382385

383386
if (this.generator.applyToMetaType.HasAncestor)

src/ImmutableObjectGraph.Generation/CodeGen+CollectionHelpersGen.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ private MethodDeclarationSyntax CreateMethodStarter(SyntaxToken name, MetaField
168168
var method = SyntaxFactory.MethodDeclaration(
169169
GetFullyQualifiedSymbolName(this.generator.applyToSymbol),
170170
name)
171-
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
171+
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
172+
.AddAttributeLists(PureAttributeList);
172173

173174
if (!field.IsLocallyDefined)
174175
{

src/ImmutableObjectGraph.Generation/CodeGen+DeepMutationGen.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ private MethodDeclarationSyntax CreateAddDescendentMethod()
5555
GetFullyQualifiedSymbolName(this.generator.applyToSymbol),
5656
AddDescendentMethodName.Identifier)
5757
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
58+
.AddAttributeLists(PureAttributeList)
5859
.AddParameterListParameters(
5960
SyntaxFactory.Parameter(valueParameterName.Identifier).WithType(this.generator.applyToMetaType.RecursiveType.TypeSyntax),
6061
SyntaxFactory.Parameter(parentParameterName.Identifier).WithType(this.generator.applyToMetaType.RecursiveParent.TypeSyntax))
@@ -129,6 +130,7 @@ private MethodDeclarationSyntax CreateRemoveDescendentMethod()
129130
this.applyTo.TypeSyntax,
130131
RemoveDescendentMethodName.Identifier)
131132
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
133+
.AddAttributeLists(PureAttributeList)
132134
.AddParameterListParameters(
133135
SyntaxFactory.Parameter(valueParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
134136
.WithBody(SyntaxFactory.Block(
@@ -208,6 +210,7 @@ private MethodDeclarationSyntax CreateReplaceDescendentSameIdentityMethod()
208210
this.applyTo.TypeSyntax,
209211
ReplaceDescendentMethodName.Identifier)
210212
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
213+
.AddAttributeLists(PureAttributeList)
211214
.AddParameterListParameters(
212215
SyntaxFactory.Parameter(updatedNodeParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
213216
.WithBody(SyntaxFactory.Block(
@@ -255,6 +258,7 @@ private MethodDeclarationSyntax CreateReplaceDescendentDifferentIdentityMethod()
255258
this.applyTo.TypeSyntax,
256259
ReplaceDescendentMethodName.Identifier)
257260
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
261+
.AddAttributeLists(PureAttributeList)
258262
.AddParameterListParameters(
259263
SyntaxFactory.Parameter(currentParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax),
260264
SyntaxFactory.Parameter(replacementParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))

src/ImmutableObjectGraph.Generation/CodeGen+DefineWithMethodsPerPropertyGen.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected override void GenerateCore()
4545
.WithAdditionalAnnotations()
4646
.AddModifiers(
4747
SyntaxFactory.Token(SyntaxKind.PublicKeyword))
48+
.AddAttributeLists(PureAttributeList)
4849
.AddParameterListParameters(
4950
SyntaxFactory.Parameter(valueParameterName.Identifier)
5051
.WithType(GetFullyQualifiedSymbolName(field.Type)))
@@ -81,6 +82,7 @@ protected override void GenerateCore()
8182
.AddModifiers(
8283
SyntaxFactory.Token(SyntaxKind.NewKeyword),
8384
SyntaxFactory.Token(SyntaxKind.PublicKeyword))
85+
.AddAttributeLists(PureAttributeList)
8486
.AddParameterListParameters(
8587
SyntaxFactory.Parameter(valueParameterName.Identifier)
8688
.WithType(GetFullyQualifiedSymbolName(field.Type)))

src/ImmutableObjectGraph.Generation/CodeGen+DeltaGen.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ protected StructDeclarationSyntax CreateDiffGramStruct()
186186
.AddModifiers(
187187
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
188188
SyntaxFactory.Token(SyntaxKind.StaticKeyword))
189+
.AddAttributeLists(PureAttributeList)
189190
.AddParameterListParameters(
190191
SyntaxFactory.Parameter(beforeParam.Identifier).WithType(this.applyTo.TypeSyntax),
191192
SyntaxFactory.Parameter(afterParam.Identifier).WithType(this.applyTo.TypeSyntax),
@@ -204,6 +205,7 @@ protected StructDeclarationSyntax CreateDiffGramStruct()
204205
.AddModifiers(
205206
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
206207
SyntaxFactory.Token(SyntaxKind.StaticKeyword))
208+
.AddAttributeLists(PureAttributeList)
207209
.AddParameterListParameters(SyntaxFactory.Parameter(valueParam.Identifier).WithType(this.applyTo.TypeSyntax))
208210
.WithBody(SyntaxFactory.Block(
209211
// return new DiffGram(null, value, ChangeKind.Added, default(<#= enumTypeName #>));
@@ -219,6 +221,7 @@ protected StructDeclarationSyntax CreateDiffGramStruct()
219221
.AddModifiers(
220222
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
221223
SyntaxFactory.Token(SyntaxKind.StaticKeyword))
224+
.AddAttributeLists(PureAttributeList)
222225
.AddParameterListParameters(SyntaxFactory.Parameter(valueParam.Identifier).WithType(this.applyTo.TypeSyntax))
223226
.WithBody(SyntaxFactory.Block(
224227
// return new DiffGram(value, null, ChangeKind.Removed, default(<#= enumTypeName #>));

src/ImmutableObjectGraph.Generation/CodeGen+EnumerableRecursiveParentGen.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private MethodDeclarationSyntax CreateGetParentMethod()
5959
var descendentParam = SyntaxFactory.IdentifierName("descendent");
6060
return SyntaxFactory.MethodDeclaration(this.applyTo.RecursiveParent.TypeSyntax, GetParentMethodName.Identifier)
6161
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
62+
.AddAttributeLists(PureAttributeList)
6263
.AddParameterListParameters(SyntaxFactory.Parameter(descendentParam.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
6364
.WithBody(SyntaxFactory.Block(
6465
// return this.GetParent<TRecursiveParent, TRecursiveType>(descendent);
@@ -136,6 +137,7 @@ private void ImplementRecursiveParentInterface()
136137
this.applyTo.RecursiveType.TypeSyntax),
137138
nameof(IRecursiveParent.GetParentedNode))
138139
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
140+
.AddAttributeLists(PureAttributeList)
139141
//.WithExplicitInterfaceSpecifier(SyntaxFactory.ExplicitInterfaceSpecifier(Syntax.GetTypeSyntax(typeof(IRecursiveParent))))
140142
.AddParameterListParameters(RequiredIdentityParameter)
141143
.WithBody(SyntaxFactory.Block(

src/ImmutableObjectGraph.Generation/CodeGen+FastSpineGen.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ protected override void GenerateCore()
266266
this.innerMembers.Add(
267267
SyntaxFactory.MethodDeclaration(Syntax.ImmutableStackOf(this.applyTo.RecursiveType.TypeSyntax), GetSpineMethodName.Identifier)
268268
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
269+
.AddAttributeLists(PureAttributeList)
269270
.AddParameterListParameters(SyntaxFactory.Parameter(descendentParameter.Identifier).WithType(this.applyTo.RecursiveType.TypeSyntax))
270271
.WithBody(SyntaxFactory.Block(
271272
SyntaxFactory.ReturnStatement(
@@ -283,6 +284,7 @@ protected override void GenerateCore()
283284
SyntaxFactory.MethodDeclaration(Syntax.ImmutableStackOf(this.applyTo.RecursiveType.TypeSyntax), GetSpineMethodName.Identifier)
284285
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
285286
.AddParameterListParameters(SyntaxFactory.Parameter(identityParameter.Identifier).WithType(IdentityFieldTypeSyntax))
287+
.AddAttributeLists(PureAttributeList)
286288
.WithBody(SyntaxFactory.Block(
287289
SyntaxFactory.ReturnStatement(
288290
SyntaxFactory.InvocationExpression(
@@ -399,6 +401,7 @@ protected MethodDeclarationSyntax CreateFindMethod()
399401
return SyntaxFactory.MethodDeclaration(this.applyTo.RecursiveType.TypeSyntax, FindMethodName.Identifier)
400402
.AddParameterListParameters(RequiredIdentityParameter)
401403
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
404+
.AddAttributeLists(PureAttributeList)
402405
.WithBody(SyntaxFactory.Block(
403406
// return this.Find<TRecursiveParent, TRecursiveType>(identity);
404407
SyntaxFactory.ReturnStatement(

src/ImmutableObjectGraph.Generation/CodeGen+RootedStructGen.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ protected MemberDeclarationSyntax CreateAsRootProperty()
136136
// public RootedRecursiveParent Root { get; }
137137
var property = SyntaxFactory.PropertyDeclaration(GetRootedTypeSyntax(this.applyTo), AsRootPropertyName.Identifier)
138138
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
139+
.AddAttributeLists(PureAttributeList)
139140
.WithExpressionBody(
140141
SyntaxFactory.ArrowExpressionClause(
141142
// => new RootedRecursiveParent(this, this);
@@ -160,6 +161,7 @@ protected MemberDeclarationSyntax CreateWithRootMethod()
160161
// public RootedTemplateType WithRoot(TRecursiveParent root)
161162
var method = SyntaxFactory.MethodDeclaration(GetRootedTypeSyntax(this.applyTo), WithRootMethodName.Identifier)
162163
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
164+
.AddAttributeLists(PureAttributeList)
163165
.AddParameterListParameters(SyntaxFactory.Parameter(rootParam.Identifier).WithType(this.applyTo.RecursiveParent.TypeSyntax))
164166
.WithBody(SyntaxFactory.Block(
165167
// var spine = root.GetSpine(this);
@@ -514,6 +516,7 @@ protected MethodDeclarationSyntax CreateWithMethod()
514516

515517
return SyntaxFactory.MethodDeclaration(this.typeName, WithMethodName.Identifier)
516518
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
519+
.AddAttributeLists(PureAttributeList)
517520
.WithParameterList(this.generator.CreateParameterList(this.applyTo.AllFields, ParameterStyle.Optional))
518521
.WithBody(SyntaxFactory.Block(
519522
// this.ThrowIfDefault();
@@ -629,6 +632,7 @@ protected MethodDeclarationSyntax CreateCreateMethod()
629632

630633
return SyntaxFactory.MethodDeclaration(this.typeName, CreateMethodName.Identifier)
631634
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword))
635+
.AddAttributeLists(PureAttributeList)
632636
.WithParameterList(this.generator.CreateParameterList(this.applyTo.AllFields, ParameterStyle.OptionalOrRequired))
633637
.WithBody(SyntaxFactory.Block(
634638
SyntaxFactory.LocalDeclarationStatement(SyntaxFactory.VariableDeclaration(varType).AddVariables(
@@ -645,6 +649,7 @@ protected MethodDeclarationSyntax CreateFindMethod()
645649
// public RootedRecursiveType Find(uint identity)
646650
return SyntaxFactory.MethodDeclaration(GetRootedTypeSyntax(this.applyTo.RecursiveTypeFromFamily), SyntaxFactory.Identifier(nameof(RecursiveTypeExtensions.Find)))
647651
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
652+
.AddAttributeLists(PureAttributeList)
648653
.AddParameterListParameters(
649654
SyntaxFactory.Parameter(IdentityParameterName.Identifier).WithType(IdentityFieldTypeSyntax))
650655
.WithBody(SyntaxFactory.Block(
@@ -664,6 +669,7 @@ protected MethodDeclarationSyntax CreateTryFindMethod()
664669
var valueParameter = SyntaxFactory.IdentifierName("value");
665670
return SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.BoolKeyword)), SyntaxFactory.Identifier(nameof(RecursiveTypeExtensions.TryFind)))
666671
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
672+
.AddAttributeLists(PureAttributeList)
667673
.AddParameterListParameters(
668674
SyntaxFactory.Parameter(IdentityParameterName.Identifier).WithType(IdentityFieldTypeSyntax),
669675
SyntaxFactory.Parameter(valueParameter.Identifier).WithType(GetRootedTypeSyntax(this.applyTo.RecursiveTypeFromFamily)).AddModifiers(SyntaxFactory.Token(SyntaxKind.OutKeyword)))
@@ -901,6 +907,7 @@ protected MemberDeclarationSyntax[] CreateWithPropertyMethods()
901907
SyntaxFactory.MethodDeclaration(GetRootedTypeSyntax(this.applyTo), DefineWithMethodsPerPropertyGen.WithPropertyMethodPrefix + field.NameAsProperty)
902908
.AddParameterListParameters(SyntaxFactory.Parameter(valueParam.Identifier).WithType(field.TypeSyntax))
903909
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
910+
.AddAttributeLists(PureAttributeList)
904911
.WithBody(SyntaxFactory.Block(
905912
CallThrowIfDefaultMethod,
906913
// var mutatedLeaf = this.greenNode.With<#= field.NamePascalCase #>(value);
@@ -932,6 +939,7 @@ protected MethodDeclarationSyntax[] CreateToTypeMethods()
932939
GetRootedTypeSyntax(targetType.type),
933940
TypeConversionGen.GetToTypeMethodName(targetType.type.TypeSymbol.Name).Identifier)
934941
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
942+
.AddAttributeLists(PureAttributeList)
935943
.WithParameterList(this.generator.CreateParameterList(targetType.type.GetFieldsBeyond(targetType.CommonAncestor), ParameterStyle.OptionalOrRequired))
936944
.WithBody(SyntaxFactory.Block(
937945
// var newGreenNode = this.greenNode.To<#= targetType.TypeName #>(<# WriteArguments(familyType.GetFieldsBeyond(commonAncestor), ArgSource.Argument); #>);
@@ -987,6 +995,7 @@ protected MethodDeclarationSyntax CreateDictionaryHelperMethod(MetaField field,
987995
GetRootedTypeSyntax(this.applyTo),
988996
methodName.Identifier)
989997
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
998+
.AddAttributeLists(PureAttributeList)
990999
.WithParameterList(methodParameters)
9911000
.WithBody(SyntaxFactory.Block(
9921001
CallThrowIfDefaultMethod,
@@ -1015,6 +1024,7 @@ protected MethodDeclarationSyntax CreateCollectionHelperMethodStarter(MetaField
10151024
GetRootedTypeSyntax(this.applyTo),
10161025
SyntaxFactory.Identifier(verb + term))
10171026
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
1027+
.AddAttributeLists(PureAttributeList)
10181028
.WithBody(SyntaxFactory.Block(
10191029
CallThrowIfDefaultMethod,
10201030
// var mutatedLeaf = this.greenNode.Verb<#= plural #>(values);
@@ -1084,6 +1094,7 @@ protected MemberDeclarationSyntax[] CreateCollectionHelperMethods()
10841094
returnType,
10851095
SyntaxFactory.Identifier("Add" + singular))
10861096
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
1097+
.AddAttributeLists(PureAttributeList)
10871098
.AddParameterListParameters(SyntaxFactory.Parameter(valueParam.Identifier).WithType(this.applyTo.RecursiveTypeFromFamily.TypeSyntax))
10881099
.WithBody(SyntaxFactory.Block(
10891100
CallThrowIfDefaultMethod,
@@ -1129,6 +1140,7 @@ protected MemberDeclarationSyntax[] CreateCollectionHelperMethods()
11291140
GetRootedTypeSyntax(this.applyTo),
11301141
SyntaxFactory.Identifier("Remove" + plural))
11311142
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
1143+
.AddAttributeLists(PureAttributeList)
11321144
.WithParameterList(SyntaxFactory.ParameterList())
11331145
.WithBody(SyntaxFactory.Block(
11341146
CallThrowIfDefaultMethod,
@@ -1184,6 +1196,7 @@ protected MethodDeclarationSyntax CreateChangesSinceMethod()
11841196
Syntax.IReadOnlyListOf(diffTypeName),
11851197
ChangesSinceMethodName.Identifier)
11861198
.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
1199+
.AddAttributeLists(PureAttributeList)
11871200
.AddParameterListParameters(
11881201
SyntaxFactory.Parameter(priorVersionParam.Identifier).WithType(GetRootedTypeSyntax(this.applyTo)))
11891202
.WithBody(SyntaxFactory.Block(

0 commit comments

Comments
 (0)