Browse Source

Fix #1758: Input var name conflicting with framework class name

pull/1769/head
Siegfried Pammer 7 years ago
parent
commit
83c525c1c2
  1. 53
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/QualifierTests.cs
  2. 1
      ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs
  3. 2
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  4. 78
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs
  5. 8
      ILSpy/Languages/CSharpHighlightingTokenWriter.cs

53
ICSharpCode.Decompiler.Tests/TestCases/Pretty/QualifierTests.cs

@ -128,9 +128,45 @@ namespace ICSharpCode.Decompiler.Tests.Pretty
} }
} }
private class i
{
public static void Test()
{
}
}
private class value
{
public static int item;
public static void Test()
{
}
}
private int fieldConflict; private int fieldConflict;
private int innerConflict; private int innerConflict;
private static int PropertyValueParameterConflictsWithTypeName {
get {
return value.item;
}
set {
QualifierTests.value.item = value;
}
}
private int this[string[] Array] {
get {
System.Array.Sort(Array);
return 0;
}
set {
System.Array.Sort(Array);
QualifierTests.value.item = value;
}
}
private void NoParameters() private void NoParameters()
{ {
Delegate(Parameter); Delegate(Parameter);
@ -209,6 +245,23 @@ namespace ICSharpCode.Decompiler.Tests.Pretty
{ {
} }
private void ParameterConflictsWithTypeName(string[] Array)
{
System.Array.Sort(Array);
}
private void LocalConflictsWithTypeName()
{
for (int i = 0; i < 10; i++) {
QualifierTests.i.Test();
}
}
public QualifierTests(string[] Array)
{
System.Array.Sort(Array);
}
} }
internal static class ZExt internal static class ZExt

1
ICSharpCode.Decompiler/CSharp/CSharpDecompiler.cs

@ -1298,6 +1298,7 @@ namespace ICSharpCode.Decompiler.CSharp
parameter.AddAnnotation(new ILVariableResolveResult(v, method.Parameters[i].Type)); parameter.AddAnnotation(new ILVariableResolveResult(v, method.Parameters[i].Type));
i++; i++;
} }
entityDecl.AddAnnotation(function);
} }
var localSettings = settings.Clone(); var localSettings = settings.Clone();

2
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -203,7 +203,7 @@ namespace ICSharpCode.Decompiler.CSharp
return true; return true;
} }
foreach (var f in function.LocalFunctions.OfType<ILFunction>()) { foreach (var f in function.LocalFunctions) {
if (f.Name == name) if (f.Name == name)
return true; return true;
} }

78
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs

@ -25,6 +25,7 @@ using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.TypeSystem; using ICSharpCode.Decompiler.CSharp.TypeSystem;
using ICSharpCode.Decompiler.Semantics; using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
namespace ICSharpCode.Decompiler.CSharp.Transforms namespace ICSharpCode.Decompiler.CSharp.Transforms
{ {
@ -55,8 +56,7 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
for (int i = 1; i < parts.Length; i++) { for (int i = 1; i < parts.Length; i++) {
nsType = new MemberType { Target = nsType, MemberName = parts[i] }; nsType = new MemberType { Target = nsType, MemberName = parts[i] };
} }
var reference = nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) as TypeOrNamespaceReference; if (nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) is TypeOrNamespaceReference reference)
if (reference != null)
usingScope.Usings.Add(reference); usingScope.Usings.Add(reference);
rootNode.InsertChildAfter(insertionPoint, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole); rootNode.InsertChildAfter(insertionPoint, new UsingDeclaration { Import = nsType }, SyntaxTree.MemberRole);
} }
@ -109,28 +109,15 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
base.VisitNamespaceDeclaration(namespaceDeclaration); base.VisitNamespaceDeclaration(namespaceDeclaration);
currentNamespace = oldNamespace; currentNamespace = oldNamespace;
}/*
public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
{
string oldNamespace = currentNamespace;
if (!(typeDeclaration.Parent is NamespaceDeclaration || typeDeclaration.Parent is TypeDeclaration)) {
var symbol = typeDeclaration.GetSymbol() as ITypeDefinition;
if (symbol != null) {
currentNamespace = symbol.Namespace;
DeclaredNamespaces.Add(currentNamespace);
}
} }
base.VisitTypeDeclaration(typeDeclaration);
currentNamespace = oldNamespace;
}*/
} }
sealed class FullyQualifyAmbiguousTypeNamesVisitor : DepthFirstAstVisitor sealed class FullyQualifyAmbiguousTypeNamesVisitor : DepthFirstAstVisitor
{ {
Stack<CSharpTypeResolveContext> context; readonly Stack<CSharpTypeResolveContext> context;
readonly bool ignoreUsingScope;
TypeSystemAstBuilder astBuilder; TypeSystemAstBuilder astBuilder;
bool ignoreUsingScope;
public FullyQualifyAmbiguousTypeNamesVisitor(TransformContext context, UsingScope usingScope) public FullyQualifyAmbiguousTypeNamesVisitor(TransformContext context, UsingScope usingScope)
{ {
@ -152,9 +139,17 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
this.astBuilder = CreateAstBuilder(currentContext); this.astBuilder = CreateAstBuilder(currentContext);
} }
static TypeSystemAstBuilder CreateAstBuilder(CSharpTypeResolveContext context) static TypeSystemAstBuilder CreateAstBuilder(CSharpTypeResolveContext context, IL.ILFunction function = null)
{ {
return new TypeSystemAstBuilder(new CSharpResolver(context)) { CSharpResolver resolver = new CSharpResolver(context);
if (function != null) {
foreach (var v in function.Variables) {
if (v.Kind != IL.VariableKind.Parameter)
resolver = resolver.AddVariable(new DefaultVariable(v.Type, v.Name));
}
}
return new TypeSystemAstBuilder(resolver) {
AddResolveResultAnnotations = true, AddResolveResultAnnotations = true,
UseAliases = true UseAliases = true
}; };
@ -201,24 +196,55 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration) public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
{
Visit(methodDeclaration, base.VisitMethodDeclaration);
}
public override void VisitAccessor(Accessor accessor)
{
Visit(accessor, base.VisitAccessor);
}
public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
{
Visit(constructorDeclaration, base.VisitConstructorDeclaration);
}
public override void VisitDestructorDeclaration(DestructorDeclaration destructorDeclaration)
{
Visit(destructorDeclaration, base.VisitDestructorDeclaration);
}
public override void VisitOperatorDeclaration(OperatorDeclaration operatorDeclaration)
{
Visit(operatorDeclaration, base.VisitOperatorDeclaration);
}
void Visit<T>(T entityDeclaration, Action<T> baseCall) where T : EntityDeclaration
{ {
if (ignoreUsingScope) { if (ignoreUsingScope) {
base.VisitMethodDeclaration(methodDeclaration); baseCall(entityDeclaration);
return; return;
} }
if (methodDeclaration.GetSymbol() is IMethod method && CSharpDecompiler.IsWindowsFormsInitializeComponentMethod(method)) { if (entityDeclaration.GetSymbol() is IMethod method) {
var previousContext = context.Peek(); var previousContext = context.Peek();
var currentContext = new CSharpTypeResolveContext(previousContext.CurrentModule); CSharpTypeResolveContext currentContext;
if (CSharpDecompiler.IsWindowsFormsInitializeComponentMethod(method)) {
currentContext = new CSharpTypeResolveContext(previousContext.CurrentModule);
} else {
currentContext = previousContext.WithCurrentMember(method);
}
context.Push(currentContext); context.Push(currentContext);
try { try {
astBuilder = CreateAstBuilder(currentContext); var function = entityDeclaration.Annotation<IL.ILFunction>();
base.VisitMethodDeclaration(methodDeclaration); astBuilder = CreateAstBuilder(currentContext, function);
baseCall(entityDeclaration);
} finally { } finally {
astBuilder = CreateAstBuilder(previousContext); astBuilder = CreateAstBuilder(previousContext);
context.Pop(); context.Pop();
} }
} else { } else {
base.VisitMethodDeclaration(methodDeclaration); baseCall(entityDeclaration);
} }
} }

8
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -318,8 +318,14 @@ namespace ICSharpCode.ILSpy
public override void WriteIdentifier(Identifier identifier) public override void WriteIdentifier(Identifier identifier)
{ {
HighlightingColor color = null; HighlightingColor color = null;
if (identifier.Name == "value" && identifier.Ancestors.OfType<Accessor>().FirstOrDefault() is Accessor accessor && accessor.Role != PropertyDeclaration.GetterRole) if (identifier.Name == "value"
&& identifier.Parent?.GetResolveResult() is ILVariableResolveResult rr
&& rr.Variable.Kind == Decompiler.IL.VariableKind.Parameter
&& identifier.Ancestors.OfType<Accessor>().FirstOrDefault() is Accessor accessor
&& accessor.Role != PropertyDeclaration.GetterRole)
{
color = valueKeywordColor; color = valueKeywordColor;
}
if ((identifier.Name == "dynamic" || identifier.Name == "var") && identifier.Parent is AstType) if ((identifier.Name == "dynamic" || identifier.Name == "var") && identifier.Parent is AstType)
color = queryKeywordsColor; color = queryKeywordsColor;
switch (GetCurrentDefinition()) { switch (GetCurrentDefinition()) {

Loading…
Cancel
Save