diff --git a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs index 0a3dddb15..e20b6d818 100644 --- a/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs @@ -4515,6 +4515,7 @@ namespace ICSharpCode.Decompiler.CSharp expr.Elements.Add(ConstructTuple(subPattern)); } } + expr.AddAnnotation(matchInstruction); return expr; } diff --git a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs index 28bccd952..daa2cbe7f 100644 --- a/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/StatementBuilder.cs @@ -863,6 +863,7 @@ namespace ICSharpCode.Decompiler.CSharp designations.VariableDesignations.Add(ConstructDesignation(subPattern)); } } + designations.AddAnnotation(matchInstruction); return designations; } } diff --git a/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs b/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs index f8810e527..43da07092 100644 --- a/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs +++ b/ICSharpCode.Decompiler/CSharp/Transforms/IntroduceUsingDeclarations.cs @@ -24,6 +24,7 @@ using System.Linq; using ICSharpCode.Decompiler.CSharp.Resolver; using ICSharpCode.Decompiler.CSharp.Syntax; using ICSharpCode.Decompiler.CSharp.TypeSystem; +using ICSharpCode.Decompiler.IL; using ICSharpCode.Decompiler.Semantics; using ICSharpCode.Decompiler.TypeSystem; using ICSharpCode.Decompiler.TypeSystem.Implementation; @@ -102,11 +103,16 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms public override void VisitSimpleType(SimpleType simpleType) { var trr = simpleType.Annotation(); - if (trr != null && !IsParentOfCurrentNamespace(trr.Type.Namespace)) + AddImportedNamespace(trr?.Type); + base.VisitSimpleType(simpleType); // also visit type arguments + } + + private void AddImportedNamespace(IType type) + { + if (type != null && !IsParentOfCurrentNamespace(type.Namespace)) { - ImportedNamespaces.Add(trr.Type.Namespace); + ImportedNamespaces.Add(type.Namespace); } - base.VisitSimpleType(simpleType); // also visit type arguments } public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration) @@ -120,6 +126,49 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms base.VisitNamespaceDeclaration(namespaceDeclaration); currentNamespace = oldNamespace; } + + public override void VisitForeachStatement(ForeachStatement foreachStatement) + { + var annotation = foreachStatement.Annotation(); + if (annotation?.GetEnumeratorCall is CallInstruction { Method.DeclaringType: var type }) + { + AddImportedNamespace(type); + } + base.VisitForeachStatement(foreachStatement); + } + + public override void VisitParenthesizedVariableDesignation(ParenthesizedVariableDesignation parenthesizedVariableDesignation) + { + var annotation = parenthesizedVariableDesignation.Annotation(); + if (annotation?.Method is IMethod { DeclaringType: var type }) + { + AddImportedNamespace(type); + } + base.VisitParenthesizedVariableDesignation(parenthesizedVariableDesignation); + } + + public override void VisitTupleExpression(TupleExpression tupleExpression) + { + var annotation = tupleExpression.Annotation(); + if (annotation?.Method is IMethod { DeclaringType: var type }) + { + AddImportedNamespace(type); + } + base.VisitTupleExpression(tupleExpression); + } + + public override void VisitArrayInitializerExpression(ArrayInitializerExpression arrayInitializerExpression) + { + foreach (var item in arrayInitializerExpression.Elements) + { + var optionalCall = item.Annotation(); + if (optionalCall?.Method is { IsExtensionMethod: true, Name: "Add" }) + { + AddImportedNamespace(optionalCall.Method.DeclaringType); + } + } + base.VisitArrayInitializerExpression(arrayInitializerExpression); + } } sealed class FullyQualifyAmbiguousTypeNamesVisitor : DepthFirstAstVisitor