Skip to content

Commit

Permalink
static context II
Browse files Browse the repository at this point in the history
  • Loading branch information
Sholtee committed Apr 25, 2022
1 parent 053f5e6 commit 24fa29a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 15 deletions.
13 changes: 7 additions & 6 deletions SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ namespace Solti.Utils.Proxy.Internals
internal partial class ClassSyntaxFactoryBase
{
/// <summary>
/// private static readonly System.Object paramName [= ...];
/// [private|public] static readonly System.Object paramName [= ...];
/// </summary>
#if DEBUG
internal
#endif
protected FieldDeclarationSyntax ResolveStaticGlobal(ITypeInfo type, string name, ExpressionSyntax? initializer = null)
protected FieldDeclarationSyntax ResolveStaticGlobal(ITypeInfo type, string name, ExpressionSyntax? initializer = null, bool @private = true)
{
VariableDeclaratorSyntax declarator = VariableDeclarator
(
Expand Down Expand Up @@ -48,7 +48,7 @@ protected FieldDeclarationSyntax ResolveStaticGlobal(ITypeInfo type, string name
(
new SyntaxToken[]
{
Token(SyntaxKind.PrivateKeyword),
Token(@private ? SyntaxKind.PrivateKeyword : SyntaxKind.PublicKeyword),
Token(SyntaxKind.StaticKeyword),
Token(SyntaxKind.ReadOnlyKeyword)
}
Expand All @@ -57,16 +57,17 @@ protected FieldDeclarationSyntax ResolveStaticGlobal(ITypeInfo type, string name
}

/// <summary>
/// private static readonly System.Object paramName [= ...];
/// [private|public] static readonly System.Object paramName [= ...];
/// </summary>
#if DEBUG
internal
#endif
protected FieldDeclarationSyntax ResolveStaticGlobal<T>(string name, ExpressionSyntax? initializer = null) => ResolveStaticGlobal
protected FieldDeclarationSyntax ResolveStaticGlobal<T>(string name, ExpressionSyntax? initializer = null, bool @private = true) => ResolveStaticGlobal
(
MetadataTypeInfo.CreateFrom(typeof(T)),
name,
initializer
initializer,
@private
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private ExpressionSyntax AmendTarget(ExpressionSyntax? target, IMemberInfo membe
member switch
{
FieldDeclarationSyntax fld => ToIdentifierName(fld),
ClassDeclarationSyntax cls => (SimpleNameSyntax) ToIdentifierName(cls),
_ => throw new NotSupportedException() // TODO: method, prop, etc
}
);
Expand Down
14 changes: 14 additions & 0 deletions SRC/Private/SyntaxFactories/ClassSyntaxFactoryBase.Variable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,19 @@ protected LocalDeclarationStatementSyntax ResolveLocal(ITypeInfo type, string na
(
ToIdentifierName(field)
);

#if DEBUG
internal
#endif
protected static NameSyntax ToIdentifierName(ClassDeclarationSyntax cls) => cls.TypeParameterList is null //TODO: move to ClassSyntaxFactoryBase.Identifier.cs
? IdentifierName(cls.Identifier)
: GenericName
(
cls.Identifier,
TypeArgumentList
(
cls.TypeParameterList.Parameters.ToSyntaxList(ga => (TypeSyntax) IdentifierName(ga.Identifier))
)
);
}
}
55 changes: 55 additions & 0 deletions SRC/Private/SyntaxFactories/ProxySyntaxFactory.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
********************************************************************************/
using System;
using System.Collections.Generic;
using System.Diagnostics;

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

Expand Down Expand Up @@ -264,5 +266,58 @@ ParenthesizedLambdaExpressionSyntax ResolveInvokeTarget(IMethodInfo method, Acti
Argument(lambda)
)
);

/// <summary>
/// private static class WrapperXxX[T1, T2, ...] <br/>
/// { <br/>
/// public static readonly MethodContext Value = new MethodContext((object target, object[] args) => <br/>
/// { <br/>
/// System.Int32 cb_a = (System.Int32)args[0]; <br/>
/// System.String cb_b; <br/>
/// T1 cb_c = (T1)args[2]; <br/>
/// ... <br/>
/// }); <br/>
/// }
/// </summary>
#if DEBUG
internal
#else
private
#endif
ClassDeclarationSyntax ResolveMethodContext(ParenthesizedLambdaExpressionSyntax lambda, IEnumerable<ITypeInfo> genericArguments) =>
ClassDeclaration
(
$"Wrapper{lambda.GetMD5HashCode()}"
)
.WithModifiers
(
TokenList
(
new SyntaxToken[]
{
Token(SyntaxKind.PublicKeyword),
Token(SyntaxKind.StaticKeyword)
}
)
)
.WithTypeParameterList
(
TypeParameterList
(
genericArguments.ToSyntaxList(ga => TypeParameter(ga.Name))
)
)
.AddMembers
(
ResolveStaticGlobal<MethodContext>
(
$"Value",
ResolveObject<MethodContext>
(
Argument(lambda)
),
@private: false
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ protected override ClassDeclarationSyntax ResolveMethods(ClassDeclarationSyntax
#endif
protected override ClassDeclarationSyntax ResolveMethod(ClassDeclarationSyntax cls, object context, IMethodInfo method)
{
FieldDeclarationSyntax methodCtx = ResolveMethodContext
(
ResolveInvokeTarget(method)
);
MemberDeclarationSyntax methodCtx = method is IGenericMethodInfo genericMethod
? ResolveMethodContext
(
ResolveInvokeTarget(method),
genericMethod.GenericArguments
)
: ResolveMethodContext
(
ResolveInvokeTarget(method)
);

return cls.AddMembers
(
Expand All @@ -97,9 +103,18 @@ IEnumerable<StatementSyntax> BuildBody()
List<StatementSyntax> statements = new();

LocalDeclarationStatementSyntax argsArray = ResolveArgumentsArray(method);

statements.Add(argsArray);

MemberAccessExpressionSyntax accessContext = StaticMemberAccess(cls, methodCtx);
if (method is IGenericMethodInfo) accessContext = SimpleMemberAccess
(
accessContext,
ToIdentifierName
(
(FieldDeclarationSyntax) ((ClassDeclarationSyntax) methodCtx).Members.Single()!
)
);

InvocationExpressionSyntax invocation = InvokeMethod
(
Invoke,
Expand All @@ -110,10 +125,7 @@ IEnumerable<StatementSyntax> BuildBody()
ResolveObject<InvocationContext>
(
ToArgument(argsArray),
Argument
(
StaticMemberAccess(cls, methodCtx)
)
Argument(accessContext)
)
)
);
Expand Down

0 comments on commit 24fa29a

Please sign in to comment.