From a1a8243e2611e8e15107a571308c983bb87f69a8 Mon Sep 17 00:00:00 2001 From: Eusebiu Marcu Date: Sun, 27 Feb 2011 16:29:31 +0200 Subject: [PATCH] Evaluate instance fields. --- .../Ast/ExpressionExtensionMethods.cs | 6 ++- .../Visitors/ExpressionEvaluator.cs | 40 +++++++++---------- .../Models/TreeModel/ChildNodesOfObject.cs | 21 +--------- .../Models/TreeModel/ExpressionNode.cs | 24 +++++++---- 4 files changed, 42 insertions(+), 49 deletions(-) diff --git a/Debugger/Debugger.Core/NRefactory/Ast/ExpressionExtensionMethods.cs b/Debugger/Debugger.Core/NRefactory/Ast/ExpressionExtensionMethods.cs index 1a4129e26..1d3dd96cb 100644 --- a/Debugger/Debugger.Core/NRefactory/Ast/ExpressionExtensionMethods.cs +++ b/Debugger/Debugger.Core/NRefactory/Ast/ExpressionExtensionMethods.cs @@ -305,7 +305,11 @@ namespace ICSharpCode.NRefactory.Ast // public static DebugType ResolveType(this AstNode expr, Debugger.AppDomain appDomain) { - return expr.GetStaticType(); + var result = expr.GetStaticType(); + if (result != null) + return result; + + return DebugType.CreateFromType(appDomain, expr.Annotation()); // if (expr is AstType && expr.GetStaticType() != null) // return expr.GetStaticType(); diff --git a/Debugger/Debugger.Core/NRefactory/Visitors/ExpressionEvaluator.cs b/Debugger/Debugger.Core/NRefactory/Visitors/ExpressionEvaluator.cs index e2cf5dc6b..938b9d6f2 100644 --- a/Debugger/Debugger.Core/NRefactory/Visitors/ExpressionEvaluator.cs +++ b/Debugger/Debugger.Core/NRefactory/Visitors/ExpressionEvaluator.cs @@ -351,6 +351,7 @@ namespace ICSharpCode.NRefactory.Visitors { TypedValue val = Evaluate(castExpression.Expression); DebugType castTo = castExpression.Type.ResolveType(context.AppDomain); + if (castTo.IsPrimitive && val.Type.IsPrimitive && castTo != val.Type) { object oldVal = val.PrimitiveValue; object newVal; @@ -367,7 +368,7 @@ namespace ICSharpCode.NRefactory.Visitors throw new GetValueException("Can not cast {0} to {1}", val.Value.Type.FullName, castTo.FullName); return new TypedValue(val.Value, castTo); } - + public override object VisitIdentifierExpression(IdentifierExpression identifierExpression, object data) { string identifier = identifierExpression.Identifier; @@ -403,7 +404,7 @@ namespace ICSharpCode.NRefactory.Visitors // Instance class members // Note that the method might be generated instance method that represents anonymous method - TypedValue thisValue = GetThisValue(); + TypedValue thisValue = this.GetThisValue(); if (thisValue != null) { IDebugMemberInfo instMember = (IDebugMemberInfo)thisValue.Type.GetMember(identifier, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, DebugType.IsFieldOrNonIndexedProperty); if (instMember != null) @@ -419,7 +420,7 @@ namespace ICSharpCode.NRefactory.Visitors throw new GetValueException("Identifier \"" + identifier + "\" not found in this context"); } - + public override object VisitIndexerExpression(IndexerExpression indexerExpression, object data) { TypedValue target = Evaluate(indexerExpression.Target); @@ -453,7 +454,7 @@ namespace ICSharpCode.NRefactory.Visitors ); } } - + public override object VisitInvocationExpression(InvocationExpression invocationExpression, object data) { TypedValue target; @@ -491,7 +492,7 @@ namespace ICSharpCode.NRefactory.Visitors return null; return new TypedValue(retVal, (DebugType)method.ReturnType); } - + public override object VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression, object data) { if (!objectCreateExpression.Initializer.IsNull) @@ -505,7 +506,7 @@ namespace ICSharpCode.NRefactory.Visitors Value val = (Value)ctor.Invoke(GetValues(ctorArgs)); return new TypedValue(val, type); } - + public override object VisitArrayCreateExpression(ArrayCreateExpression arrayCreateExpression, object data) { if (arrayCreateExpression.AdditionalArraySpecifiers.Count() != 0) @@ -532,7 +533,7 @@ namespace ICSharpCode.NRefactory.Visitors } return new TypedValue(array, type); } - + public override object VisitMemberReferenceExpression(MemberReferenceExpression memberReferenceExpression, object data) { TypedValue target; @@ -558,26 +559,23 @@ namespace ICSharpCode.NRefactory.Visitors ((IDebugMemberInfo)memberInfos[0]).MemberType ); } - + public override object VisitParenthesizedExpression(ParenthesizedExpression parenthesizedExpression, object data) { return Evaluate(parenthesizedExpression.Expression); } - + public override object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data) { return CreateValue(primitiveExpression.Value); } - + TypedValue GetThisValue() { // This is needed so that captured 'this' is supported - DebugLocalVariableInfo thisVar = context.MethodInfo.GetLocalVariableThis(); - if (thisVar != null) - return new TypedValue(thisVar.GetValue(context), (DebugType)thisVar.LocalType); - return null; + return new TypedValue(context.GetThisValue(), (DebugType)context.MethodInfo.DeclaringType); } - + public override object VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression, object data) { TypedValue thisValue = GetThisValue(); @@ -585,9 +583,9 @@ namespace ICSharpCode.NRefactory.Visitors throw new GetValueException(context.MethodInfo.FullName + " is static method and does not have \"this\""); return thisValue; } - + #region Binary and unary expressions - + public override object VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data) { TypedValue value = Evaluate(unaryOperatorExpression.Expression); @@ -664,7 +662,7 @@ namespace ICSharpCode.NRefactory.Visitors throw new EvaluateException(unaryOperatorExpression, "Can not use the unary operator {0} on type {1}", op.ToString(), value.Type.FullName); } - + /// /// Perform given arithmetic operation. /// The arguments must be already converted to the correct types. @@ -732,7 +730,7 @@ namespace ICSharpCode.NRefactory.Visitors return null; } - + public override object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data) { BinaryOperatorType op = binaryOperatorExpression.Operator; @@ -843,7 +841,7 @@ namespace ICSharpCode.NRefactory.Visitors throw new EvaluateException(binaryOperatorExpression, "Can not use the binary operator {0} on types {1} and {2}", op.ToString(), left.Type.FullName, right.Type.FullName); } - + /// /// Perform given arithmetic operation. /// The arguments must be already converted to the correct types. @@ -1024,7 +1022,7 @@ namespace ICSharpCode.NRefactory.Visitors return null; } } - + #endregion } } diff --git a/Debugger/ILSpy.Debugger/Models/TreeModel/ChildNodesOfObject.cs b/Debugger/ILSpy.Debugger/Models/TreeModel/ChildNodesOfObject.cs index 1080b4d57..b262d396c 100644 --- a/Debugger/ILSpy.Debugger/Models/TreeModel/ChildNodesOfObject.cs +++ b/Debugger/ILSpy.Debugger/Models/TreeModel/ChildNodesOfObject.cs @@ -100,7 +100,7 @@ namespace ILSpy.Debugger.Models.TreeModel // This is needed for expanding IEnumerable var type = new SimpleType() { Identifier = typeof(IList).FullName }; - type.AddAnnotation(DebugType.CreateFromType(Mscorlib, typeof(IList))); + type.AddAnnotation(typeof(IList)); targetObject = new CastExpression() { Expression = targetObject.Clone(), Type = type }; @@ -151,24 +151,5 @@ namespace ILSpy.Debugger.Models.TreeModel } } } - - static Module mscorlib; - - public static Module Mscorlib { - get { - if (mscorlib != null) return mscorlib; - foreach (var appDomain in WindowsDebugger.CurrentProcess.AppDomains) { - foreach(Module m in appDomain.Process.Modules) { - if (m.Name == "mscorlib.dll" && - m.AppDomain == appDomain) { - mscorlib = m; - return mscorlib; - } - } - } - - throw new DebuggerException("Mscorlib not loaded"); - } - } } } diff --git a/Debugger/ILSpy.Debugger/Models/TreeModel/ExpressionNode.cs b/Debugger/ILSpy.Debugger/Models/TreeModel/ExpressionNode.cs index 4107b7ada..59fcc204f 100644 --- a/Debugger/ILSpy.Debugger/Models/TreeModel/ExpressionNode.cs +++ b/Debugger/ILSpy.Debugger/Models/TreeModel/ExpressionNode.cs @@ -149,13 +149,23 @@ namespace ILSpy.Debugger.Models.TreeModel Value val; try { - if (expression is IdentifierExpression) { - var frame = WindowsDebugger.DebuggedProcess.SelectedThread.MostRecentStackFrame; - int token = frame.MethodInfo.MetadataToken; - List list; - if (ILAstBuilder.MemberLocalVariables.TryGetValue(token, out list)) { - var variable = list.Find(v => v.Name == Name); - if (variable != null) { + var frame = WindowsDebugger.DebuggedProcess.SelectedThread.MostRecentStackFrame; + int token = frame.MethodInfo.MetadataToken; + // get the target name + int index = Name.IndexOf('.'); + string targetName = Name; + if (index != -1) { + targetName = Name.Substring(0, index); + } + + List list; + if (ILAstBuilder.MemberLocalVariables.TryGetValue(token, out list)) { + var variable = list.Find(v => v.Name == targetName); + if (variable != null) { + if (expression is MemberReferenceExpression) { + var memberExpression = (MemberReferenceExpression)expression; + memberExpression.Target.AddAnnotation(new int[] { variable.OriginalVariable.Index }); + } else { expression.AddAnnotation(new int[] { variable.OriginalVariable.Index }); } }