Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add primary constructor & Record parameter list #59

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ You can checkout this Github repository or you can use the NuGet package:

**Install using the command line from the Package Manager:**
```bash
Install-Package SoloX.GeneratorTools.Core.CSharp -version 1.0.0-alpha.37
Install-Package SoloX.GeneratorTools.Core.CSharp -version 1.0.0-alpha.38
```

**Install using the .Net CLI:**
```bash
dotnet add package SoloX.GeneratorTools.Core.CSharp --version 1.0.0-alpha.37
dotnet add package SoloX.GeneratorTools.Core.CSharp --version 1.0.0-alpha.38
```

**Install editing your project file (csproj):**
```xml
<PackageReference Include="SoloX.GeneratorTools.Core.CSharp" Version="1.0.0-alpha.37" />
<PackageReference Include="SoloX.GeneratorTools.Core.CSharp" Version="1.0.0-alpha.38" />
```

## The use case
Expand Down
2 changes: 1 addition & 1 deletion src/SharedProperties.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>1.0.0-alpha.37</Version>
<Version>1.0.0-alpha.38</Version>
<Authors>Xavier Solau</Authors>
<Copyright>Copyright © 2021 Xavier Solau</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using SoloX.GeneratorTools.Core.CSharp.Extensions.Utils;
using System;
using SoloX.GeneratorTools.Core.CSharp.Model.Impl.Loader.Metadata;
using Microsoft.Extensions.Logging;

namespace SoloX.GeneratorTools.Core.CSharp.Extensions
{
Expand All @@ -29,8 +30,9 @@ public static class CSharpGeneratorServiceCollectionExtensions
/// Add dependency injections for the CSharp tools generator.
/// </summary>
/// <param name="services">The service collection where to setup dependencies.</param>
/// <param name="loggerFactory">Specific logger factory to use or null.</param>
/// <returns>The input services once setup is done.</returns>
public static IServiceCollection AddCSharpToolsGenerator(this IServiceCollection services)
public static IServiceCollection AddCSharpToolsGenerator(this IServiceCollection services, ILoggerFactory loggerFactory = null)
{
if (services == null)
{
Expand Down Expand Up @@ -72,7 +74,15 @@ public static IServiceCollection AddCSharpToolsGenerator(this IServiceCollection
.AddSingleton<IParserDeclarationFactory, ParserDeclarationFactory>()
.AddSingleton<ICSharpWorkspaceItemFactory, CSharpWorkspaceItemFactory>()
.AddTransient<ICSharpWorkspaceFactory, CSharpWorkspaceFactory>()
.AddTransient<IGeneratorLoggerFactory, GeneratorLoggerFactory>();
.AddTransient<IGeneratorLoggerFactory>(
r =>
{
if (loggerFactory != null)
{
return new GeneratorLoggerFactory(loggerFactory);
}
return new GeneratorLoggerFactory(r.GetRequiredService<ILoggerFactory>());
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ public override void VisitInvocationExpression(InvocationExpressionSyntax node)
base.VisitInvocationExpression(node);
}

public override void VisitAwaitExpression(AwaitExpressionSyntax node)
{
WriteToken(node.AwaitKeyword);
Visit(node.Expression);
}

public override void VisitLocalDeclarationStatement(LocalDeclarationStatementSyntax node)
{
if (!ProcessRepeatAffectation(node))
Expand Down Expand Up @@ -772,6 +778,92 @@ public override void VisitLiteralExpression(LiteralExpressionSyntax node)
this.WriteNode(node);
}

public override void VisitTryStatement(TryStatementSyntax node)
{
this.WriteToken(node.TryKeyword);
this.Visit(node.Block);

if (node.Catches != null)
{
foreach (var catche in node.Catches)
{
this.Visit(catche);
}
}

if (node.Finally != null)
{
this.Visit(node.Finally);
}
}

public override void VisitCatchClause(CatchClauseSyntax node)
{
this.WriteToken(node.CatchKeyword);

if (node.Declaration != null)
{
this.Visit(node.Declaration);
}

this.Visit(node.Block);
}

public override void VisitCatchDeclaration(CatchDeclarationSyntax node)
{
this.WriteToken(node.OpenParenToken);

this.Visit(node.Type);

if (node.Identifier != null)
{
this.VisitToken(node.Identifier);
}

this.WriteToken(node.CloseParenToken);
}

public override void VisitQualifiedName(QualifiedNameSyntax node)
{
this.Visit(node.Left);
this.WriteToken(node.DotToken);
this.Visit(node.Right);
}

public override void VisitFinallyClause(FinallyClauseSyntax node)
{
this.WriteToken(node.FinallyKeyword);
this.Visit(node.Block);
}

public override void VisitSimpleLambdaExpression(SimpleLambdaExpressionSyntax node)
{
if (node.AsyncKeyword != null)
{
this.WriteToken(node.AsyncKeyword);
}

this.WriteParameter(node.Parameter);

this.WriteToken(node.ArrowToken);

this.Visit(node.Body);
}

public override void VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
{
if (node.AsyncKeyword != null)
{
this.WriteToken(node.AsyncKeyword);
}

this.Visit(node.ParameterList);

this.WriteToken(node.ArrowToken);

this.Visit(node.Body);
}

private bool ProcessRepeatAffectation(LocalDeclarationStatementSyntax node)
{
if (node.Declaration.Variables.Count > 1)
Expand Down Expand Up @@ -1009,6 +1101,14 @@ private void WriteConstructorDeclaration(ConstructorDeclarationSyntax node)
this.Write(node.Modifiers.ToFullString());
this.WriteToken(node.Identifier);
this.WriteNode(node.ParameterList);

if (node.Initializer != null)
{
this.WriteToken(node.Initializer.ColonToken);
this.WriteToken(node.Initializer.ThisOrBaseKeyword);
this.Visit(node.Initializer.ArgumentList);
}

this.Visit(node.Body);
}

Expand Down Expand Up @@ -1142,7 +1242,12 @@ private void WriteParameter(ParameterSyntax node)
this.WriteAttributeLists(node.AttributeLists);

this.Write(node.Modifiers.ToFullString());
this.Write(node.Type.ToFullString());

if (node.Type != null)
{
this.Write(node.Type.ToFullString());
}

this.Write(node.Identifier.ToFullString());

if (node.Default != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public interface IDeclaration<out TNode>
/// </summary>
bool IsValueType { get; }

/// <summary>
/// Tells if this is a record type.
/// </summary>
bool IsRecordType { get; }

/// <summary>
/// Gets the declaration syntax node provider.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ public abstract class ADeclaration<TNode> : ADeclaration, IDeclaration<TNode>
/// <param name="usingDirectives">The current using directive available for this class.</param>
/// <param name="location">The location of the declaration.</param>
/// <param name="isValueType">Tells if this is a valueType.</param>
/// <param name="isRecordType">Tells if this is a record type.</param>
protected ADeclaration(
string nameSpace,
string name,
ISyntaxNodeProvider<TNode> syntaxNodeProvider,
IUsingDirectives usingDirectives,
string location,
bool isValueType)
bool isValueType,
bool isRecordType)
{
DeclarationNameSpace = nameSpace;
Name = name;
Expand All @@ -77,6 +79,7 @@ protected ADeclaration(
FullName = GetFullName(nameSpace, name);
Location = location;
IsValueType = isValueType;
IsRecordType = isRecordType;
}

/// <inheritdoc/>
Expand All @@ -91,6 +94,9 @@ protected ADeclaration(
/// <inheritdoc/>
public bool IsValueType { get; protected set; }

/// <inheritdoc/>
public bool IsRecordType { get; protected set; }

/// <inheritdoc/>
public ISyntaxNodeProvider<TNode> SyntaxNodeProvider { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public abstract class AGenericDeclaration<TNode> : ADeclaration<TNode>, IGeneric
/// <param name="location">The location of the declaration.</param>
/// <param name="loader">The loader to use when deep loading the declaration.</param>
/// <param name="isValueType">Tells if this is a valueType.</param>
/// <param name="isRecordType">Tells if this is a record type.</param>
protected AGenericDeclaration(
string nameSpace,
string name,
ISyntaxNodeProvider<TNode> syntaxNodeProvider,
IUsingDirectives usingDirectives,
string location,
AGenericDeclarationLoader<TNode> loader,
bool isValueType)
: base(nameSpace, name, syntaxNodeProvider, usingDirectives, location, isValueType)
bool isValueType,
bool isRecordType)
: base(nameSpace, name, syntaxNodeProvider, usingDirectives, location, isValueType, isRecordType)
{
this.loader = loader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public ClassDeclaration(
usingDirectives,
location,
loader,
false,
false)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public EnumDeclaration(
IUsingDirectives usingDirectives,
string location,
AEnumDeclarationLoader loader)
: base(nameSpace, name, syntaxNodeProvider, usingDirectives, location, true)
: base(nameSpace, name, syntaxNodeProvider, usingDirectives, location, true, false)
{
this.loader = loader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class GenericParameterDeclaration : ADeclaration<TypeParameterSyntax>, IG
/// <param name="name">The declaration name.</param>
/// <param name="syntaxNodeProvider">The declaration syntax node provider.</param>
public GenericParameterDeclaration(string name, ISyntaxNodeProvider<TypeParameterSyntax> syntaxNodeProvider)
: base(string.Empty, name, syntaxNodeProvider, NoUsingDirectives.Instance, null, false)
: base(string.Empty, name, syntaxNodeProvider, NoUsingDirectives.Instance, null, false, false)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public InterfaceDeclaration(
usingDirectives,
location,
loader,
false,
false)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,21 @@ private static void LoadProperties(

var attributeList = LoadCustomAttributes(metadataReader, declaration, resolver, customAttributeHandles);

var propertyType = propertySignature.ReturnType;

memberList.Add(
new PropertyDeclaration(
propertyName,
propertyType,
new MetadataPropertySyntaxNodeProvider<PropertyDeclarationSyntax>(),
attributeList,
!getterHandle.IsNil,
!setterHandle.IsNil));
var compilerAttribute = attributeList.FirstOrDefault(attribute => attribute.DeclarationUse.Declaration.FullName == typeof(CompilerGeneratedAttribute).FullName);

if (compilerAttribute == null)
{
var propertyType = propertySignature.ReturnType;

memberList.Add(
new PropertyDeclaration(
propertyName,
propertyType,
new MetadataPropertySyntaxNodeProvider<PropertyDeclarationSyntax>(),
attributeList,
!getterHandle.IsNil,
!setterHandle.IsNil));
}
}
}

Expand Down Expand Up @@ -327,13 +332,15 @@ private static void LoadExtends(MetadataReader metadataReader, AGenericDeclarati
}
}

var extendedInterfaceFilter = BuildExtendedInterfaceFilter(metadataReader, typeDefinition, declaration.IsValueType);

foreach (var interfaceImplementation in typeDefinition.GetInterfaceImplementations())
{
if (!interfaceImplementation.IsNil)
{
var interfaceDeclarationUse = GetDeclarationUseFrom(metadataReader, interfaceImplementation, resolver, declaration);

if (interfaceDeclarationUse != null)
if (interfaceDeclarationUse != null && extendedInterfaceFilter(interfaceDeclarationUse))
{
uses.Add(interfaceDeclarationUse);
}
Expand All @@ -343,6 +350,26 @@ private static void LoadExtends(MetadataReader metadataReader, AGenericDeclarati
declaration.Extends = uses;
}

private static Func<IDeclarationUse<SyntaxNode>, bool> BuildExtendedInterfaceFilter(MetadataReader metadataReader, TypeDefinition typeDefinition, bool valueType)
{
if ((valueType && ProbeRecordStructType(metadataReader, typeDefinition)) || (!valueType && ProbeRecordType(metadataReader, typeDefinition)))
{
// IEquatable must be excluded since it is compiler generated on record type.
var equatableFullName = GetNameWithoutGeneric(typeof(IEquatable<>).FullName);
return t =>
{
if (t.Declaration.FullName == equatableFullName)
{
return false;
}
return true;
};
}

return t => true;
}


private static void LoadGenericParameters(MetadataReader metadataReader, AGenericDeclaration<TNode> declaration, IDeclarationResolver resolver)
{
var typeDefinitionHandle = declaration.GetData<TypeDefinitionHandle>();
Expand Down
Loading
Loading