~ubuntu-branches/ubuntu/lucid/monodevelop/lucid

« 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-02-02 11:39:59 UTC
  • mfrom: (1.2.6 upstream) (1.3.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100202113959-s4exdz7er7igylz2
Tags: 2.2.1+dfsg-1
* New upstream release
* debian/control:
  + Standards version 3.8.4 (no changes needed)
* debian/patches/remove_support_for_non_debian_functionality.patch,
  debian/patches/remove_support_for_soft_debugger.patch,
  debian/patches/remove_support_for_moonlight.patch,
  debian/rules:
  + Split patch into two pieces, to make it easier to enable either
    SDB or Moonlight support with a rebuild
* debian/monodevelop-moonlight.install,
  debian/monodevelop-debugger-sdb.install,
  debian/control:
  + Create packaging data for the Soft Debugger addin and Moonlight addin -
    and comment them out of debian/control as we can't provide them on
    Debian for now

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
using ICSharpCode.NRefactory.Ast;
37
37
using System.Reflection;
38
38
using Mono.Debugging.Client;
 
39
using Mono.Debugging.Backend;
39
40
 
40
41
namespace Mono.Debugging.Evaluation
41
42
{
72
73
                        IParser parser = ParserFactory.CreateParser (SupportedLanguage.CSharp, codeStream);
73
74
                        Expression expObj = parser.ParseExpression ();
74
75
                        if (expObj == null)
75
 
                                throw new EvaluatorException ("Could not parse expression '" + exp + "'");
 
76
                                throw new EvaluatorException ("Could not parse expression '{0}'", exp);
76
77
                        EvaluatorVisitor ev = new EvaluatorVisitor (ctx, exp, expectedType, userVariables);
77
78
                        return (ValueReference) expObj.AcceptVisitor (ev, null);
78
79
                }
 
80
                
 
81
                public string Resolve (DebuggerSession session, SourceLocation location, string exp)
 
82
                {
 
83
                        if (exp.StartsWith ("?"))
 
84
                                return "?" + Resolve (session, location, exp.Substring (1).Trim ());
 
85
                        if (exp.StartsWith ("var "))
 
86
                                return "var " + Resolve (session, location, exp.Substring (4).Trim (' ','\t'));
 
87
 
 
88
                        StringReader codeStream = new StringReader (exp);
 
89
                        IParser parser = ParserFactory.CreateParser (SupportedLanguage.CSharp, codeStream);
 
90
                        Expression expObj = parser.ParseExpression ();
 
91
                        if (expObj == null)
 
92
                                throw new EvaluatorException ("Could not parse expression '{0}'", exp);
 
93
                        NRefactoryResolverVisitor ev = new NRefactoryResolverVisitor (session, location, exp);
 
94
                        expObj.AcceptVisitor (ev, null);
 
95
                        return ev.GetResolvedExpression ();
 
96
                }
79
97
        }
80
98
 
81
99
        class EvaluatorVisitor: AbstractAstVisitor
136
154
                
137
155
                public override object VisitTypeReferenceExpression (ICSharpCode.NRefactory.Ast.TypeReferenceExpression typeReferenceExpression, object data)
138
156
                {
 
157
                        if (typeReferenceExpression.TypeReference.IsGlobal) {
 
158
                                string name = typeReferenceExpression.TypeReference.Type;
 
159
                                object type = ctx.Options.AllowImplicitTypeLoading ? ctx.Adapter.ForceLoadType (ctx, name) : ctx.Adapter.GetType (ctx, name);
 
160
                                if (type != null)
 
161
                                        return new TypeValueReference (ctx, type);
 
162
        
 
163
                                if (!ctx.Options.AllowImplicitTypeLoading) {
 
164
                                        string[] namespaces = ctx.Adapter.GetImportedNamespaces (ctx);
 
165
                                        if (namespaces.Length > 0) {
 
166
                                                // Look in namespaces
 
167
                                                foreach (string ns in namespaces) {
 
168
                                                        if (name == ns || ns.StartsWith (name + "."))
 
169
                                                                return new NamespaceValueReference (ctx, name);
 
170
                                                }
 
171
                                        }
 
172
                                } else {
 
173
                                        // Assume it is a namespace.
 
174
                                        return new NamespaceValueReference (ctx, name);
 
175
                                }
 
176
                        }                       
139
177
                        throw CreateNotSupportedError ();
140
178
                }
141
179
 
232
270
                        if (res != null)
233
271
                                return LiteralValueReference.CreateTargetObjectLiteral (ctx, name, res);
234
272
                        else
235
 
                                return LiteralValueReference.CreateObjectLiteral (ctx, name, new LiteralExp ("No return value."));
 
273
                                return LiteralValueReference.CreateObjectLiteral (ctx, name, new EvaluationResult ("No return value."));
236
274
                }
237
275
                
238
276
                public override object VisitInnerClassTypeReference (ICSharpCode.NRefactory.Ast.InnerClassTypeReference innerClassTypeReference, object data)
266
304
                
267
305
                public override object VisitIdentifierExpression (ICSharpCode.NRefactory.Ast.IdentifierExpression identifierExpression, object data)
268
306
                {
269
 
                        string name = identifierExpression.Identifier;
270
 
                        
 
307
                        return VisitIdentifier (identifierExpression.Identifier);
 
308
                }
 
309
                
 
310
                object VisitIdentifier (string name)
 
311
                {
271
312
                        // Look in user defined variables
272
313
                        
273
314
                        ValueReference userVar;
314
355
                                if (namespaces.Length > 0) {
315
356
                                        // Look in namespaces
316
357
                                        foreach (string ns in namespaces) {
317
 
                                                vtype = ctx.Adapter.GetType (ctx, ns + "." + name);
 
358
                                                string nm = ns + "." + name;
 
359
                                                vtype = ctx.Options.AllowImplicitTypeLoading ? ctx.Adapter.ForceLoadType (ctx, nm) : ctx.Adapter.GetType (ctx, nm);
318
360
                                                if (vtype != null)
319
361
                                                        return new TypeValueReference (ctx, vtype);
320
362
                                        }
352
394
                }
353
395
                
354
396
                public override object VisitCastExpression (ICSharpCode.NRefactory.Ast.CastExpression castExpression, object data)
355
 
                {
 
397
                {
356
398
                        ValueReference val = (ValueReference)castExpression.Expression.AcceptVisitor (this, data);
357
399
                        TypeValueReference type = castExpression.CastTo.AcceptVisitor (this, data) as TypeValueReference;
358
400
                        if (type == null)
384
426
                                                throw CreateParseError ("Left operand of logical And must be a boolean");
385
427
                                        if (!(bool)val)
386
428
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, false);
387
 
                                        return rightExp.AcceptVisitor (this, data);
 
429
                                        ValueReference vr = (ValueReference) rightExp.AcceptVisitor (this, data);
 
430
                                        if (ctx.Adapter.GetTypeName (ctx, vr.Type) != "System.Boolean")
 
431
                                                throw CreateParseError ("Right operand of logical And must be a boolean");
 
432
                                        return vr;
388
433
                                }
389
434
                                case BinaryOperatorType.LogicalOr: {
390
435
                                        object val = left.ObjectValue;
392
437
                                                throw CreateParseError ("Left operand of logical Or must be a boolean");
393
438
                                        if ((bool)val)
394
439
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, true);
395
 
                                        return rightExp.AcceptVisitor (this, data);
 
440
                                        ValueReference vr = (ValueReference) rightExp.AcceptVisitor (this, data);
 
441
                                        if (ctx.Adapter.GetTypeName (ctx, vr.Type) != "System.Boolean")
 
442
                                                throw CreateParseError ("Right operand of logical Or must be a boolean");
 
443
                                        return vr;
396
444
                                }
397
445
                        }
398
446
 
399
447
                        ValueReference right = (ValueReference) rightExp.AcceptVisitor (this, data);
 
448
                        object targetVal1 = left.Value;
 
449
                        object targetVal2 = right.Value;
400
450
                        object val1 = left.ObjectValue;
401
451
                        object val2 = right.ObjectValue;
402
 
 
 
452
                        
403
453
                        if (oper == BinaryOperatorType.Add || oper == BinaryOperatorType.Concat) {
404
454
                                if (val1 is string || val2 is string) {
405
455
                                        if (!(val1 is string) && val1 != null)
406
 
                                                val1 = left.CallToString ();
 
456
                                                val1 = ctx.Adapter.CallToString (ctx, targetVal1);
407
457
                                        if (!(val2 is string) && val2 != null)
408
 
                                                val2 = right.CallToString ();
 
458
                                                val2 = ctx.Adapter.CallToString (ctx, targetVal2);
409
459
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, (string) val1 + (string) val2);
410
460
                                }
411
461
                        }
412
462
                        
413
463
                        if ((oper == BinaryOperatorType.ExclusiveOr) && (val1 is bool) && (val2 is bool))
414
464
                                return LiteralValueReference.CreateObjectLiteral (ctx, name, (bool)val1 ^ (bool)val2);
415
 
                        
416
 
                        switch (oper) {
417
 
                                case BinaryOperatorType.Equality:
418
 
                                        if (val1 == null || val2 == null)
 
465
 
 
466
                        if ((val1 == null || !val1.GetType ().IsPrimitive) && (val2 == null || !val2.GetType ().IsPrimitive)) {
 
467
                                switch (oper) {
 
468
                                        case BinaryOperatorType.Equality:
 
469
                                                if (val1 == null || val2 == null)
 
470
                                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 == val2);
 
471
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, val1.Equals (val2));
 
472
                                        case BinaryOperatorType.InEquality:
 
473
                                                if (val1 == null || val2 == null)
 
474
                                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 != val2);
 
475
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, !val1.Equals (val2));
 
476
                                        case BinaryOperatorType.ReferenceEquality:
419
477
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 == val2);
420
 
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1.Equals (val2));
421
 
                                case BinaryOperatorType.InEquality:
422
 
                                        if (val1 == null || val2 == null)
 
478
                                        case BinaryOperatorType.ReferenceInequality:
423
479
                                                return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 != val2);
424
 
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, !val1.Equals (val2));
425
 
                                case BinaryOperatorType.ReferenceEquality:
426
 
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 == val2);
427
 
                                case BinaryOperatorType.ReferenceInequality:
428
 
                                        return LiteralValueReference.CreateObjectLiteral (ctx, name, val1 != val2);
429
 
                                case BinaryOperatorType.Concat:
430
 
                                        throw CreateParseError ("Invalid binary operator.");
 
480
                                        case BinaryOperatorType.Concat:
 
481
                                                throw CreateParseError ("Invalid binary operator.");
 
482
                                }
431
483
                        }
432
484
                        
433
485
                        if (val1 == null || val2 == null || (val1 is bool) || (val2 is bool))
434
486
                                throw CreateParseError ("Invalid operands in binary operator");
435
487
                        
436
488
                        long v1, v2;
 
489
                        object longType = ctx.Adapter.GetType (ctx, "System.Int64");
437
490
                        
438
491
                        try {
439
 
                                v1 = Convert.ToInt64 (val1);
440
 
                                v2 = Convert.ToInt64 (val2);
 
492
                                object c1 = ctx.Adapter.Cast (ctx, targetVal1, longType);
 
493
                                v1 = (long) ctx.Adapter.TargetObjectToObject (ctx, c1);
 
494
                                        
 
495
                                object c2 = ctx.Adapter.Cast (ctx, targetVal2, longType);
 
496
                                v2 = (long) ctx.Adapter.TargetObjectToObject (ctx, c2);
441
497
                        } catch {
442
498
                                throw CreateParseError ("Invalid operands in binary operator");
443
499
                        }
461
517
                                case BinaryOperatorType.GreaterThanOrEqual: res = v1 >= v2; break;
462
518
                                case BinaryOperatorType.LessThan: res = v1 < v2; break;
463
519
                                case BinaryOperatorType.LessThanOrEqual: res = v1 <= v2; break;
 
520
                                case BinaryOperatorType.ReferenceEquality:
 
521
                                case BinaryOperatorType.Equality: res = v1 == v2; break;
 
522
                                case BinaryOperatorType.ReferenceInequality:
 
523
                                case BinaryOperatorType.InEquality: res = v1 != v2; break;
464
524
                                default: throw CreateParseError ("Invalid binary operator.");
465
525
                        }
466
526
                        
467
527
                        if (!(res is bool))
468
528
                                res = (long) Convert.ChangeType (res, GetCommonType (v1, v2));
 
529
                        
 
530
                        if (ctx.Adapter.IsEnum (ctx, targetVal1)) {
 
531
                                object tval = ctx.Adapter.Cast (ctx, ctx.Adapter.CreateValue (ctx, res), ctx.Adapter.GetValueType (ctx, targetVal1));
 
532
                                return LiteralValueReference.CreateTargetObjectLiteral (ctx, name, tval);
 
533
                        }
 
534
                        if (ctx.Adapter.IsEnum (ctx, targetVal2)) {
 
535
                                object tval = ctx.Adapter.Cast (ctx, ctx.Adapter.CreateValue (ctx, res), ctx.Adapter.GetValueType (ctx, targetVal2));
 
536
                                return LiteralValueReference.CreateTargetObjectLiteral (ctx, name, tval);
 
537
                        }
 
538
                        
469
539
                        return LiteralValueReference.CreateObjectLiteral (ctx, name, res);
470
540
                }
471
541