Skip to content

Commit

Permalink
allow syntax factories to declare custom members II, static context I
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Apr 24, 2022
1 parent 6f46993 commit 053f5e6
Show file tree
Hide file tree
Showing 41 changed files with 653 additions and 608 deletions.
26 changes: 26 additions & 0 deletions SRC/Private/Extensions/CodeAnalysisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -71,5 +74,28 @@ public static NameSyntax Qualify(this IEnumerable<NameSyntax> parts)
right: (SimpleNameSyntax) coll[coll.Count - 1]
);
}

[SuppressMessage("Security", "CA5351:Do Not Use Broken Cryptographic Algorithms")]
public static string GetMD5HashCode(this SyntaxNode node)
{
using MD5 md5 = MD5.Create();

byte[] hash = md5.ComputeHash
(
Encoding.UTF8.GetBytes(node.ToFullString())
);

StringBuilder sb = new();

for (int i = 0; i < hash.Length; i++)
{
sb.Append
(
md5.Hash[i].ToString("X2", null)
);
}

return sb.ToString();
}
}
}
3 changes: 0 additions & 3 deletions SRC/Private/Extensions/Metadata/MemberInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
Expand Down Expand Up @@ -53,8 +52,6 @@ private static MemberInfo DoExtractFrom(LambdaExpression expr)

public static ExtendedMemberInfo ExtractFrom(Delegate accessor) => Cache.GetOrAdd(accessor, static accessor =>
{
Debug.Assert(accessor.Target is null);
MethodInfo method = (MethodInfo) accessor
.Method
.GetInstructions()
Expand Down
4 changes: 2 additions & 2 deletions SRC/Private/SourceGenerators/ProxyEmbedder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public void Initialize(GeneratorInitializationContext context)
public void Execute(GeneratorExecutionContext context)
{
//
// Csak C# 8.0+ tamogatjuk
// Csak C# 7.0+ tamogatjuk
//

if (context.Compilation is not CSharpCompilation { LanguageVersion: >= LanguageVersion.CSharp8 } compilation)
if (context.Compilation is not CSharpCompilation { LanguageVersion: >= LanguageVersion.CSharp7 } compilation)
{
context.ReportDiagnostic
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
********************************************************************************/
using System.Collections.Generic;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -20,13 +21,13 @@ internal partial class ClassSyntaxFactoryBase
#if DEBUG
internal
#endif
protected ConstructorDeclarationSyntax ResolveConstructor(IConstructorInfo ctor, string className)
protected ConstructorDeclarationSyntax ResolveConstructor(IConstructorInfo ctor, SyntaxToken name)
{
IReadOnlyList<IParameterInfo> paramz = ctor.Parameters;

return ConstructorDeclaration
(
identifier: Identifier(className)
name
)
.WithModifiers
(
Expand Down Expand Up @@ -75,11 +76,11 @@ protected ConstructorDeclarationSyntax ResolveConstructor(IConstructorInfo ctor,
#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveConstructors(object context);
protected abstract ClassDeclarationSyntax ResolveConstructors(ClassDeclarationSyntax cls, object context);

#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveConstructor(object context, IConstructorInfo ctor);
protected abstract ClassDeclarationSyntax ResolveConstructor(ClassDeclarationSyntax cls, object context, IConstructorInfo ctor);
}
}
4 changes: 2 additions & 2 deletions SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ protected EventDeclarationSyntax ResolveEvent(IEventInfo @event, CSharpSyntaxNod
#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveEvents(object context);
protected abstract ClassDeclarationSyntax ResolveEvents(ClassDeclarationSyntax cls, object context);

#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveEvent(object context, IEventInfo evt);
protected abstract ClassDeclarationSyntax ResolveEvent(ClassDeclarationSyntax cls, object context, IEventInfo evt);
}
}
16 changes: 15 additions & 1 deletion SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* *
* Author: Denes Solti *
********************************************************************************/
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand Down Expand Up @@ -45,7 +46,7 @@ protected FieldDeclarationSyntax ResolveStaticGlobal(ITypeInfo type, string name
(
TokenList
(
new[]
new SyntaxToken[]
{
Token(SyntaxKind.PrivateKeyword),
Token(SyntaxKind.StaticKeyword),
Expand All @@ -54,5 +55,18 @@ protected FieldDeclarationSyntax ResolveStaticGlobal(ITypeInfo type, string name
)
);
}

/// <summary>
/// private static readonly System.Object paramName [= ...];
/// </summary>
#if DEBUG
internal
#endif
protected FieldDeclarationSyntax ResolveStaticGlobal<T>(string name, ExpressionSyntax? initializer = null) => ResolveStaticGlobal
(
MetadataTypeInfo.CreateFrom(typeof(T)),
name,
initializer
);
}
}
22 changes: 22 additions & 0 deletions SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* *
* Author: Denes Solti *
********************************************************************************/
using System;
using System.Diagnostics;

using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -50,6 +51,27 @@ private ExpressionSyntax AmendTarget(ExpressionSyntax? target, IMemberInfo membe
member
);


#if DEBUG
internal
#endif
protected static MemberAccessExpressionSyntax StaticMemberAccess(ClassDeclarationSyntax target, MemberDeclarationSyntax member) => SimpleMemberAccess
(
AliasQualifiedName
(
IdentifierName
(
Token(SyntaxKind.GlobalKeyword)
),
IdentifierName(target.Identifier)
),
member switch
{
FieldDeclarationSyntax fld => ToIdentifierName(fld),
_ => throw new NotSupportedException() // TODO: method, prop, etc
}
);

/// <summary>
/// [[(Type)] target | [(Type)] this | Namespace.Type].Member
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ protected InvocationExpressionSyntax InvokeMethod(IMethodInfo method, Expression
#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveMethods(object context);
protected abstract ClassDeclarationSyntax ResolveMethods(ClassDeclarationSyntax cls, object context);

#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveMethod(object context, IMethodInfo method);
protected abstract ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ protected IndexerDeclarationSyntax ResolveIndexer(IPropertyInfo property, CSharp
#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveProperties(object context);
protected abstract ClassDeclarationSyntax ResolveProperties(ClassDeclarationSyntax cls, object context);

#if DEBUG
internal
#endif
protected abstract IEnumerable<MemberDeclarationSyntax> ResolveProperty(object context, IPropertyInfo property);
protected abstract ClassDeclarationSyntax ResolveProperty(ClassDeclarationSyntax cls, object context, IPropertyInfo property);
}
}
18 changes: 17 additions & 1 deletion SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ protected LocalDeclarationStatementSyntax ResolveLocal(ITypeInfo type, string na
#if DEBUG
internal
#endif
protected static ArgumentSyntax ToArgument(LocalDeclarationStatementSyntax variable) => Argument(ToIdentifierName(variable));
protected static ArgumentSyntax ToArgument(LocalDeclarationStatementSyntax variable) => Argument
(
ToIdentifierName(variable)
);

#if DEBUG
internal
#endif
protected static IdentifierNameSyntax ToIdentifierName(FieldDeclarationSyntax field) => IdentifierName(field.Declaration.Variables.Single()!.Identifier);

#if DEBUG
internal
#endif
protected static ArgumentSyntax ToArgument(FieldDeclarationSyntax field) => Argument
(
ToIdentifierName(field)
);
}
}
33 changes: 17 additions & 16 deletions SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ internal abstract partial class ClassSyntaxFactoryBase: SyntaxFactoryBase
#if DEBUG
internal
#endif
protected virtual ClassDeclarationSyntax ResolveClass(object context, CancellationToken cancellation) =>
ClassDeclaration
protected virtual ClassDeclarationSyntax ResolveClass(object context, CancellationToken cancellation)
{
ClassDeclarationSyntax cls = ClassDeclaration
(
identifier: ResolveClassName(context)
)
Expand Down Expand Up @@ -50,28 +51,28 @@ internal abstract partial class ClassSyntaxFactoryBase: SyntaxFactoryBase
)
)
)
)
.WithMembers
(
List
(
ResolveMembers(context, cancellation)
)
);

return ResolveMembers(cls, context, cancellation);
}

#if DEBUG
internal
#endif
protected virtual IEnumerable<MemberDeclarationSyntax> ResolveMembers(object context, CancellationToken cancellation)
protected virtual ClassDeclarationSyntax ResolveMembers(ClassDeclarationSyntax cls, object context, CancellationToken cancellation)
{
foreach (Func<object, IEnumerable<MemberDeclarationSyntax>> factory in new Func<object, IEnumerable<MemberDeclarationSyntax>>[] { ResolveConstructors, ResolveMethods, ResolveProperties, ResolveEvents })
foreach (Func<ClassDeclarationSyntax, object, ClassDeclarationSyntax> factory in new Func<ClassDeclarationSyntax, object, ClassDeclarationSyntax>[]
{
ResolveConstructors,
ResolveMethods,
ResolveProperties,
ResolveEvents
})
{
foreach (MemberDeclarationSyntax member in factory(context))
{
cancellation.ThrowIfCancellationRequested();
yield return member;
}
cancellation.ThrowIfCancellationRequested();
cls = factory(cls, context);
}
return cls;
}

#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* *
* Author: Denes Solti *
********************************************************************************/
using System.Collections.Generic;

using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Solti.Utils.Proxy.Internals
Expand All @@ -14,23 +12,21 @@ internal partial class DuckSyntaxFactory
#if DEBUG
internal
#endif
protected override IEnumerable<MemberDeclarationSyntax> ResolveConstructors(object context)
protected override ClassDeclarationSyntax ResolveConstructors(ClassDeclarationSyntax cls, object context)
{
foreach (IConstructorInfo ctor in BaseType.GetPublicConstructors())
{
foreach (MemberDeclarationSyntax member in ResolveConstructor(null!, ctor))
{
yield return member;
}
cls = ResolveConstructor(cls, context, ctor);
}
return cls;
}

#if DEBUG
internal
#endif
protected override IEnumerable<MemberDeclarationSyntax> ResolveConstructor(object context, IConstructorInfo ctor)
{
yield return ResolveConstructor(ctor, ResolveClassName(null!));
}
protected override ClassDeclarationSyntax ResolveConstructor(ClassDeclarationSyntax cls, object context, IConstructorInfo ctor) => cls.AddMembers
(
ResolveConstructor(ctor, cls.Identifier)
);
}
}
Loading

0 comments on commit 053f5e6

Please sign in to comment.