|
|
|
|
@ -17,11 +17,11 @@
@@ -17,11 +17,11 @@
|
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Linq; |
|
|
|
|
using ICSharpCode.Decompiler.CSharp.Resolver; |
|
|
|
|
using ICSharpCode.Decompiler.CSharp.Syntax; |
|
|
|
|
using ICSharpCode.Decompiler.CSharp.TypeSystem; |
|
|
|
|
using ICSharpCode.Decompiler.Semantics; |
|
|
|
|
using ICSharpCode.Decompiler.TypeSystem; |
|
|
|
|
using ICSharpCode.Decompiler.Util; |
|
|
|
|
|
|
|
|
|
@ -33,98 +33,60 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
@@ -33,98 +33,60 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
|
|
|
|
|
public class IntroduceExtensionMethods : DepthFirstAstVisitor, IAstTransform |
|
|
|
|
{ |
|
|
|
|
TransformContext context; |
|
|
|
|
UsingScope rootUsingScope; |
|
|
|
|
UsingScope usingScope; |
|
|
|
|
IMember currentMember; |
|
|
|
|
CSharpTypeResolveContext resolveContext; |
|
|
|
|
CSharpResolver resolver; |
|
|
|
|
|
|
|
|
|
public void Run(AstNode rootNode, TransformContext context) |
|
|
|
|
{ |
|
|
|
|
this.context = context; |
|
|
|
|
this.usingScope = this.rootUsingScope = rootNode.Annotation<UsingScope>(); |
|
|
|
|
currentMember = context.DecompiledMember; |
|
|
|
|
SetContext(); |
|
|
|
|
InitializeContext(rootNode.Annotation<UsingScope>()); |
|
|
|
|
rootNode.AcceptVisitor(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void SetContext() |
|
|
|
|
{ |
|
|
|
|
this.usingScope = rootUsingScope; |
|
|
|
|
string ns = currentMember?.Namespace ?? context.DecompiledTypeDefinition?.Namespace; |
|
|
|
|
if (ns != null) { |
|
|
|
|
foreach (var name in ns.Split('.')) |
|
|
|
|
usingScope = new UsingScope(usingScope, name); |
|
|
|
|
} |
|
|
|
|
resolveContext = new CSharpTypeResolveContext( |
|
|
|
|
context.DecompiledAssembly, |
|
|
|
|
usingScope.Resolve(context.TypeSystem.Compilation), |
|
|
|
|
currentMember?.DeclaringTypeDefinition ?? context.DecompiledTypeDefinition, |
|
|
|
|
currentMember); |
|
|
|
|
resolver = new CSharpResolver(resolveContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) |
|
|
|
|
{ |
|
|
|
|
currentMember = methodDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitMethodDeclaration(methodDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
} |
|
|
|
|
Stack<CSharpTypeResolveContext> resolveContextStack = new Stack<CSharpTypeResolveContext>(); |
|
|
|
|
|
|
|
|
|
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration) |
|
|
|
|
void InitializeContext(UsingScope usingScope) |
|
|
|
|
{ |
|
|
|
|
currentMember = constructorDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitConstructorDeclaration(constructorDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration) |
|
|
|
|
{ |
|
|
|
|
currentMember = destructorDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitDestructorDeclaration(destructorDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration) |
|
|
|
|
{ |
|
|
|
|
currentMember = propertyDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitPropertyDeclaration(propertyDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration) |
|
|
|
|
{ |
|
|
|
|
currentMember = fieldDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitFieldDeclaration(fieldDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
this.resolveContextStack = new Stack<CSharpTypeResolveContext>(); |
|
|
|
|
if (!string.IsNullOrEmpty(context.DecompiledTypeDefinition?.Namespace)) { |
|
|
|
|
foreach (string ns in context.DecompiledTypeDefinition.Namespace.Split('.')) { |
|
|
|
|
usingScope = new UsingScope(usingScope, ns); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
var currentContext = new CSharpTypeResolveContext(context.TypeSystem.MainAssembly, usingScope.Resolve(context.TypeSystem.Compilation), context.DecompiledTypeDefinition); |
|
|
|
|
this.resolveContextStack.Push(currentContext); |
|
|
|
|
this.resolver = new CSharpResolver(currentContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitEventDeclaration(EventDeclaration eventDeclaration) |
|
|
|
|
public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration) |
|
|
|
|
{ |
|
|
|
|
currentMember = eventDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitEventDeclaration(eventDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
var previousContext = resolveContextStack.Peek(); |
|
|
|
|
var usingScope = previousContext.CurrentUsingScope.UnresolvedUsingScope; |
|
|
|
|
foreach (string ident in namespaceDeclaration.Identifiers) { |
|
|
|
|
usingScope = new UsingScope(usingScope, ident); |
|
|
|
|
} |
|
|
|
|
var currentContext = new CSharpTypeResolveContext(previousContext.CurrentAssembly, usingScope.Resolve(previousContext.Compilation)); |
|
|
|
|
resolveContextStack.Push(currentContext); |
|
|
|
|
try { |
|
|
|
|
this.resolver = new CSharpResolver(currentContext); |
|
|
|
|
base.VisitNamespaceDeclaration(namespaceDeclaration); |
|
|
|
|
} finally { |
|
|
|
|
this.resolver = new CSharpResolver(previousContext); |
|
|
|
|
resolveContextStack.Pop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration) |
|
|
|
|
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration) |
|
|
|
|
{ |
|
|
|
|
currentMember = eventDeclaration.GetSymbol() as IMember; |
|
|
|
|
if (currentMember == null) return; |
|
|
|
|
SetContext(); |
|
|
|
|
base.VisitCustomEventDeclaration(eventDeclaration); |
|
|
|
|
currentMember = null; |
|
|
|
|
var previousContext = resolveContextStack.Peek(); |
|
|
|
|
var currentContext = previousContext.WithCurrentTypeDefinition(typeDeclaration.GetSymbol() as ITypeDefinition); |
|
|
|
|
resolveContextStack.Push(currentContext); |
|
|
|
|
try { |
|
|
|
|
this.resolver = new CSharpResolver(currentContext); |
|
|
|
|
base.VisitTypeDeclaration(typeDeclaration); |
|
|
|
|
} finally { |
|
|
|
|
this.resolver = new CSharpResolver(previousContext); |
|
|
|
|
resolveContextStack.Pop(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public override void VisitInvocationExpression(InvocationExpression invocationExpression) |
|
|
|
|
@ -141,7 +103,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
@@ -141,7 +103,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
|
|
|
|
|
var rr = resolver.ResolveMemberAccess(target, method.Name, typeArguments, NameLookupMode.InvocationTarget) as MethodGroupResolveResult; |
|
|
|
|
if (rr == null) |
|
|
|
|
return; |
|
|
|
|
var or = rr.PerformOverloadResolution(resolveContext.Compilation, args, allowExtensionMethods: true); |
|
|
|
|
var or = rr.PerformOverloadResolution(resolver.CurrentTypeResolveContext.Compilation, args, allowExtensionMethods: true); |
|
|
|
|
if (or == null || or.IsAmbiguous || !method.Equals(or.GetBestCandidateWithSubstitutedTypeArguments())) |
|
|
|
|
return; |
|
|
|
|
if (firstArgument is NullReferenceExpression) |
|
|
|
|
|