-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds a public property for each primary contructor parameter (#273)
- Loading branch information
Showing
5 changed files
with
166 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Cecilifier.Core/CodeGeneration/PrimaryConstructor.Generator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Cecilifier.Core.AST; | ||
using Cecilifier.Core.Extensions; | ||
using Cecilifier.Core.Mappings; | ||
using Cecilifier.Core.Misc; | ||
using Cecilifier.Core.Naming; | ||
using Cecilifier.Core.Variables; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace Cecilifier.Core.CodeGeneration; | ||
|
||
public class PrimaryConstructorGenerator | ||
{ | ||
internal static void AddPropertiesFrom(IVisitorContext context, string typeDefinitionVariable, TypeDeclarationSyntax type) | ||
{ | ||
if (type.ParameterList is null) | ||
return; | ||
|
||
foreach (var parameter in type.ParameterList.Parameters) | ||
{ | ||
AddPropertyFor(context, parameter, typeDefinitionVariable); | ||
context.WriteNewLine(); | ||
} | ||
} | ||
|
||
private static void AddPropertyFor(IVisitorContext context, ParameterSyntax parameter, string typeDefinitionVariable) | ||
{ | ||
using var _ = LineInformationTracker.Track(context, parameter); | ||
|
||
context.WriteComment($"Property: {parameter.Identifier.Text} (primary constructor)"); | ||
var propDefVar = context.Naming.SyntheticVariable(parameter.Identifier.Text, ElementKind.Property); | ||
var paramSymbol = context.SemanticModel.GetDeclaredSymbol(parameter).EnsureNotNull<ISymbol, IParameterSymbol>(); | ||
var exps = CecilDefinitionsFactory.PropertyDefinition(propDefVar, parameter.Identifier.Text, context.TypeResolver.Resolve(paramSymbol.Type)); | ||
|
||
context.WriteCecilExpressions(exps); | ||
context.WriteCecilExpression($"{typeDefinitionVariable}.Properties.Add({propDefVar});"); | ||
context.WriteNewLine(); | ||
context.WriteNewLine(); | ||
|
||
var declaringTypeVariable = context.DefinitionVariables.GetLastOf(VariableMemberKind.Type); | ||
if (!declaringTypeVariable.IsValid) | ||
throw new InvalidOperationException(); | ||
|
||
var publicPropertyMethodAttributes = "MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName"; | ||
var propertyType = context.SemanticModel.GetDeclaredSymbol(parameter).GetMemberType(); | ||
var propertyData = new PropertyGenerationData( | ||
declaringTypeVariable.MemberName, | ||
declaringTypeVariable.VariableName, | ||
false, // TODO | ||
propDefVar, | ||
parameter.Identifier.Text, | ||
new Dictionary<string, string> | ||
{ | ||
["get"] = publicPropertyMethodAttributes, | ||
["set"] = publicPropertyMethodAttributes | ||
}, | ||
false, | ||
context.TypeResolver.Resolve(propertyType), | ||
propertyType.ToDisplayString(), | ||
Array.Empty<ParameterSpec>(), | ||
"FieldAttributes.Private", //TODO: Constant | ||
OpCodes.Stfld, | ||
OpCodes.Ldfld); | ||
|
||
PropertyGenerator propertyGenerator = new (context); | ||
|
||
AddGetter(); | ||
context.WriteNewLine(); | ||
AddInit(); | ||
|
||
void AddGetter() | ||
{ | ||
context.WriteComment($"{propertyData.Name} getter"); | ||
var getMethodVar = context.Naming.SyntheticVariable($"get{propertyData.Name}", ElementKind.Method); | ||
// properties for primary ctor parameters cannot override base properties, so hasCovariantReturn = false and overridenMethod = null (none) | ||
using (propertyGenerator.AddGetterMethodDeclaration(in propertyData, getMethodVar, false, $"get_{propertyData.Name}", null)) | ||
{ | ||
var ilVar = context.Naming.ILProcessor($"get{propertyData.Name}"); | ||
context.WriteCecilExpressions([$"var {ilVar} = {getMethodVar}.Body.GetILProcessor();"]); | ||
|
||
propertyGenerator.AddAutoGetterMethodImplementation(in propertyData, ilVar); | ||
} | ||
} | ||
|
||
void AddInit() | ||
{ | ||
context.WriteComment($"{propertyData.Name} init"); | ||
var setMethodVar = context.Naming.SyntheticVariable($"set{propertyData.Name}", ElementKind.Method); | ||
using (propertyGenerator.AddSetterMethodDeclaration(in propertyData, setMethodVar, true, $"set_{propertyData.Name}", null)) | ||
{ | ||
var ilVar = context.Naming.ILProcessor($"set{propertyData.Name}"); | ||
context.WriteCecilExpressions([$"var {ilVar} = {setMethodVar}.Body.GetILProcessor();"]); | ||
|
||
propertyGenerator.AddAutoSetterMethodImplementation(in propertyData, ilVar); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Cecilifier.Core.AST; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace Cecilifier.Core.CodeGeneration; | ||
|
||
public class RecordGenerator | ||
{ | ||
internal static void AddSyntheticMembers(IVisitorContext context, string recordTypeDefinitionVariable, TypeDeclarationSyntax record) | ||
{ | ||
PrimaryConstructorGenerator.AddPropertiesFrom(context, recordTypeDefinitionVariable, record); | ||
} | ||
} |