~ubuntu-branches/ubuntu/natty/monodevelop/natty

« back to all changes in this revision

Viewing changes to src/core/Mono.Debugging/Mono.Debugging.Evaluation/NRefactoryEvaluator.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-01-07 19:06:58 UTC
  • mto: (1.6.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 46.
  • Revision ID: james.westby@ubuntu.com-20100107190658-z9z95lgk4kwfes7p
ImportĀ upstreamĀ versionĀ 2.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
using ICSharpCode.NRefactory.Visitors;
36
36
using ICSharpCode.NRefactory.Ast;
37
37
using System.Reflection;
 
38
using Mono.Debugging.Client;
38
39
 
39
40
namespace Mono.Debugging.Evaluation
40
41
{
42
43
        {
43
44
                Dictionary<string,ValueReference> userVariables = new Dictionary<string, ValueReference> ();
44
45
                
45
 
                public override ValueReference Evaluate (EvaluationContext ctx, string exp, EvaluationOptions options)
 
46
                public override ValueReference Evaluate (EvaluationContext ctx, string exp, object expectedType)
46
47
                {
47
48
                        if (exp.StartsWith ("?"))
48
49
                                exp = exp.Substring (1).Trim ();
72
73
                        Expression expObj = parser.ParseExpression ();
73
74
                        if (expObj == null)
74
75
                                throw new EvaluatorException ("Could not parse expression '" + exp + "'");
75
 
                        EvaluatorVisitor ev = new EvaluatorVisitor (ctx, exp, options, userVariables);
 
76
                        EvaluatorVisitor ev = new EvaluatorVisitor (ctx, exp, expectedType, userVariables);
76
77
                        return (ValueReference) expObj.AcceptVisitor (ev, null);
77
78
                }
78
79
        }
80
81
        class EvaluatorVisitor: AbstractAstVisitor
81
82
        {
82
83
                EvaluationContext ctx;
 
84
                EvaluationOptions options;
83
85
                string name;
84
 
                EvaluationOptions options;
 
86
                object expectedType;
85
87
                Dictionary<string,ValueReference> userVariables;
86
88
 
87
 
                public EvaluatorVisitor (EvaluationContext ctx, string name, EvaluationOptions options, Dictionary<string,ValueReference> userVariables)
 
89
                public EvaluatorVisitor (EvaluationContext ctx, string name, object expectedType, Dictionary<string,ValueReference> userVariables)
88
90
                {
89
91
                        this.ctx = ctx;
90
92
                        this.name = name;
91
 
                        this.options = options;
 
93
                        this.expectedType = expectedType;
92
94
                        this.userVariables = userVariables;
 
95
                        this.options = ctx.Options;
93
96
                }
94
97
                
95
98
                public override object VisitUnaryOperatorExpression (ICSharpCode.NRefactory.Ast.UnaryOperatorExpression unaryOperatorExpression, object data)
159
162
                {
160
163
                        if (primitiveExpression.Value != null)
161
164
                                return LiteralValueReference.CreateObjectLiteral (ctx, name, primitiveExpression.Value);
162
 
                        else if (options.ExpectedType != null)
163
 
                                return new NullValueReference (ctx, options.ExpectedType);
 
165
                        else if (expectedType != null)
 
166
                                return new NullValueReference (ctx, expectedType);
164
167
                        else
165
168
                                return new NullValueReference (ctx, ctx.Adapter.GetType (ctx, "System.Object"));
166
169
                }
177
180
                
178
181
                public override object VisitInvocationExpression (ICSharpCode.NRefactory.Ast.InvocationExpression invocationExpression, object data)
179
182
                {
180
 
                        if (!options.CanEvaluateMethods)
 
183
                        if (!options.AllowMethodEvaluation)
181
184
                                throw CreateNotSupportedError ();
182
185
 
183
186
                        ValueReference target = null;
288
291
                        ValueReference thisobj = ctx.Adapter.GetThisReference (ctx);
289
292
                        object thistype = ctx.Adapter.GetEnclosingType (ctx);
290
293
 
291
 
                        var = ctx.Adapter.GetMember (ctx, thistype, thisobj != null ? thisobj.Value : null, name);
 
294
                        var = ctx.Adapter.GetMember (ctx, thisobj, thistype, thisobj != null ? thisobj.Value : null, name);
292
295
                        if (var != null)
293
296
                                return var;
294
297
 
376
379
                        
377
380
                        switch (oper) {
378
381
                                case BinaryOperatorType.LogicalAnd: {
379
 
                                        if (!(bool)left.ObjectValue)
380
 
                                                return left;
 
382
                                        object val = left.ObjectValue;
 
383
                                        if (!(val is bool))
 
384
                                                throw CreateParseError ("Left operand of logical And must be a boolean");
 
385
                                        if (!(bool)val)
 
386
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, false);
381
387
                                        return rightExp.AcceptVisitor (this, data);
382
388
                                }
383
389
                                case BinaryOperatorType.LogicalOr: {
384
 
                                        if ((bool)left.ObjectValue)
385
 
                                                return left;
 
390
                                        object val = left.ObjectValue;
 
391
                                        if (!(val is bool))
 
392
                                                throw CreateParseError ("Left operand of logical Or must be a boolean");
 
393
                                        if ((bool)val)
 
394
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, true);
386
395
                                        return rightExp.AcceptVisitor (this, data);
387
396
                                }
388
397
                        }
393
402
 
394
403
                        if (oper == BinaryOperatorType.Add || oper == BinaryOperatorType.Concat) {
395
404
                                if (val1 is string || val2 is string) {
396
 
                                        if (!(val1 is string))
 
405
                                        if (!(val1 is string) && val1 != null)
397
406
                                                val1 = left.CallToString ();
398
 
                                        if (!(val2 is string))
 
407
                                        if (!(val2 is string) && val2 != null)
399
408
                                                val2 = right.CallToString ();
400
409
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, (string) val1 + (string) val2);
401
410
                                }
402
411
                        }
403
412
                        
404
 
                        if ((oper == BinaryOperatorType.ExclusiveOr) && (val1 is bool) && !(val2 is bool))
 
413
                        if ((oper == BinaryOperatorType.ExclusiveOr) && (val1 is bool) && (val2 is bool))
405
414
                                return LiteralValueReference.CreateObjectLiteral (ctx, name, (bool)val1 ^ (bool)val2);
406
415
                        
407
416
                        switch (oper) {
408
417
                                case BinaryOperatorType.Equality:
 
418
                                        if (val1 == null || val2 == null)
 
419
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 == val2);
409
420
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1.Equals (val2));
410
421
                                case BinaryOperatorType.InEquality:
 
422
                                        if (val1 == null || val2 == null)
 
423
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 != val2);
411
424
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, !val1.Equals (val2));
412
425
                                case BinaryOperatorType.ReferenceEquality:
413
426
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 == val2);
417
430
                                        throw CreateParseError ("Invalid binary operator.");
418
431
                        }
419
432
                        
420
 
                        long v1 = Convert.ToInt64 (left.ObjectValue);
421
 
                        long v2 = Convert.ToInt64 (right.ObjectValue);
 
433
                        if (val1 == null || val2 == null || (val1 is bool) || (val2 is bool))
 
434
                                throw CreateParseError ("Invalid operands in binary operator");
 
435
                        
 
436
                        long v1, v2;
 
437
                        
 
438
                        try {
 
439
                                v1 = Convert.ToInt64 (val1);
 
440
                                v2 = Convert.ToInt64 (val2);
 
441
                        } catch {
 
442
                                throw CreateParseError ("Invalid operands in binary operator");
 
443
                        }
 
444
                        
422
445
                        object res;
423
446
                        
424
447
                        switch (oper) {
480
503
                
481
504
                public override object VisitAssignmentExpression (ICSharpCode.NRefactory.Ast.AssignmentExpression assignmentExpression, object data)
482
505
                {
483
 
                        if (!options.CanEvaluateMethods)
 
506
                        if (!options.AllowMethodEvaluation)
484
507
                                throw CreateNotSupportedError ();
485
508
                        
486
509
                        ValueReference left = (ValueReference) assignmentExpression.Left.AcceptVisitor (this, data);