public override void Visit(IAnonymousDelegate del)
 {
     foreach (var r in del.Body.Statements.Where(s => s is IReturnStatement))
     {
         AnonymousDelegateReturns.Add((IReturnStatement)r);
     }
 }
 public override void Visit(IAnonymousDelegate anonymousDelegate)
 {
     if (Process(anonymousDelegate))
     {
         visitor.Visit(anonymousDelegate);
     }
     base.Visit(anonymousDelegate);
 }
Exemplo n.º 3
0
        private bool PrintLambdaBody(IAnonymousDelegate lambda)
        {
            if (lambda.Body.Statements.Count(s => !(s is IEmptyStatement)) != 1)
            {
                return(false);
            }
            var nonEmptyStatement = lambda.Body.Statements.First(s => !(s is IEmptyStatement));

            var returnStatement = nonEmptyStatement as IReturnStatement;

            if (returnStatement != null && returnStatement.Expression != null)
            {
                this.Traverse(returnStatement.Expression);
                return(true);
            }
            var expressionStatement = nonEmptyStatement as IExpressionStatement;

            if (expressionStatement != null)
            {
                this.Traverse(expressionStatement.Expression);
                return(true);
            }
            return(false);
        }
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate) {
   var saved = this.currentAnonymousDelegate;
   this.currentAnonymousDelegate = anonymousDelegate;
   foreach (var parameter in anonymousDelegate.Parameters)
     this.definitionsToIgnore[parameter] = true;
   base.TraverseChildren(anonymousDelegate);
   this.currentAnonymousDelegate = saved;
   if (saved != null) {
     if (this.anonymousDelegatesThatCaptureThis.ContainsKey(anonymousDelegate))
       this.anonymousDelegatesThatCaptureThis[saved] = true;
     if (this.anonymousDelegatesThatCaptureLocalsOrParameters.ContainsKey(anonymousDelegate))
       this.anonymousDelegatesThatCaptureLocalsOrParameters[saved] = true;
   }
 }
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate) {
   var nonEmptyStatementCount = 0;
   IStatement nonEmptyStatement = null;
   foreach (var statement in anonymousDelegate.Body.Statements) {
     if (!(statement is IEmptyStatement)) {
       nonEmptyStatementCount++;
       nonEmptyStatement = statement;
     }
   }
   if (nonEmptyStatementCount == 1) {
     var returnStatement = nonEmptyStatement as IReturnStatement;
     if (returnStatement != null && returnStatement.Expression != null) {
       this.Traverse(anonymousDelegate.Parameters);
       this.sourceEmitterOutput.Write(" => ");
       this.Traverse(returnStatement.Expression);
       return;
     }
     var expressionStatement = nonEmptyStatement as IExpressionStatement;
     if (expressionStatement != null && anonymousDelegate.ReturnType.TypeCode == PrimitiveTypeCode.Void) {
       this.Traverse(anonymousDelegate.Parameters);
       this.sourceEmitterOutput.Write(" => ");
       this.Traverse(expressionStatement.Expression);
       return;
     }
   }
   this.sourceEmitterOutput.Write("delegate ");
   this.Traverse(anonymousDelegate.Parameters);
   this.sourceEmitterOutput.WriteLine(" {");
   this.sourceEmitterOutput.IncreaseIndent();
   this.Traverse(anonymousDelegate.Body.Statements);
   this.sourceEmitterOutput.DecreaseIndent();
   this.sourceEmitterOutput.Write("}", true);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Performs some computation with the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public virtual void Visit(IAnonymousDelegate anonymousDelegate)
 {
     this.Visit((IExpression)anonymousDelegate);
 }
Exemplo n.º 7
0
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate) {
   this.foundAnonymousDelegate = true;
   if (this.foundYield)
     this.StopTraversal = true;
   else
     base.TraverseChildren(anonymousDelegate);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Performs some computation with the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public virtual void Visit(IAnonymousDelegate anonymousDelegate)
 {
 }
Exemplo n.º 9
0
 /// <summary>
 /// Traverses the children of the anonymous delegate expression.
 /// </summary>
 public virtual void TraverseChildren(IAnonymousDelegate anonymousDelegate)
 {
     Contract.Requires(anonymousDelegate != null);
       this.TraverseChildren((IExpression)anonymousDelegate);
       if (this.StopTraversal) return;
       this.Traverse(anonymousDelegate.Parameters);
       if (this.StopTraversal) return;
       this.Traverse(anonymousDelegate.Body);
       if (this.StopTraversal) return;
       this.Traverse(anonymousDelegate.ReturnType);
       if (this.StopTraversal) return;
       if (anonymousDelegate.ReturnValueIsModified)
     this.Traverse(anonymousDelegate.ReturnValueCustomModifiers);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Do not replace pops in lambdas: they must not be confused with pops that
 /// are not within them.
 /// </summary>
 public override IExpression Rewrite(IAnonymousDelegate anonymousDelegate) {
   return anonymousDelegate;
 }
Exemplo n.º 11
0
 public virtual void onASTElement(IAnonymousDelegate anonymousDelegate)
 {
 }
 /// <summary>
 /// Rewrites the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public virtual IExpression Rewrite(IAnonymousDelegate anonymousDelegate)
 {
     return anonymousDelegate;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Do not count pops in lambdas: they must not be confused with pops that
 /// are not within them.
 /// </summary>
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate) {
 }
Exemplo n.º 14
0
 public override void Visit(IAnonymousDelegate anonymousDelegate)
 {
     allElements.Add(new InvokInfo(Traverser, "IAnonymousDelegate", anonymousDelegate));
 }
Exemplo n.º 15
0
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate)
 {
     var saved = this.currentAnonymousDelegate;
       this.currentAnonymousDelegate = anonymousDelegate;
       base.TraverseChildren(anonymousDelegate);
       this.currentAnonymousDelegate = saved;
 }
Exemplo n.º 16
0
 /// <summary>
 /// Generates IL for the specified anonymous delegate.
 /// </summary>
 /// <param name="anonymousDelegate">The anonymous delegate.</param>
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate)
 {
     Contract.Assume(false, "IAnonymousDelegate nodes must be replaced before trying to convert the Code Model to IL.");
 }
Exemplo n.º 17
0
 public virtual void onASTElement(IAnonymousDelegate anonymousDelegate) { }
Exemplo n.º 18
0
    /// <summary>
    /// Returns a deep copy of the given anonymous delegate expression.
    /// </summary>
    /// <param name="anonymousDelegate"></param>
    public AnonymousDelegate Copy(IAnonymousDelegate anonymousDelegate) {
      Contract.Requires(anonymousDelegate != null);
      Contract.Ensures(Contract.Result<AnonymousDelegate>() != null);

      var mutableCopy = this.shallowCopier.Copy(anonymousDelegate);
      this.CopyChildren((Expression)mutableCopy);
      mutableCopy.Parameters = this.Copy(mutableCopy.Parameters);
      mutableCopy.Body = this.Copy((BlockStatement)mutableCopy.Body);
      mutableCopy.ReturnType = this.Copy(mutableCopy.ReturnType);
      if (mutableCopy.ReturnValueIsModified)
        mutableCopy.ReturnValueCustomModifiers = this.Copy(mutableCopy.ReturnValueCustomModifiers);
      return mutableCopy;
    }
Exemplo n.º 19
0
 public void Visit(IAnonymousDelegate anonymousDelegate)
 {
     this.result = this.rewriter.Rewrite(anonymousDelegate);
 }
Exemplo n.º 20
0
    /// <summary>
    /// Returns a shallow copy of the given anonymous delegate expression.
    /// </summary>
    /// <param name="anonymousDelegate"></param>
    public AnonymousDelegate Copy(IAnonymousDelegate anonymousDelegate) {
      Contract.Requires(anonymousDelegate != null);
      Contract.Ensures(Contract.Result<AnonymousDelegate>() != null);

      return new AnonymousDelegate(anonymousDelegate);
    }
Exemplo n.º 21
0
        public override void TraverseChildren(IAnonymousDelegate anonymousDelegate)
{ MethodEnter(anonymousDelegate);
            base.TraverseChildren(anonymousDelegate);
     MethodExit();   }
 /// <summary>
 /// Performs some computation with the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public virtual void Visit(IAnonymousDelegate anonymousDelegate)
 {
 }
Exemplo n.º 23
0
 /// <summary>
 /// Traverses the anonymous delegate expression.
 /// </summary>
 public void Traverse(IAnonymousDelegate anonymousDelegate)
 {
     Contract.Requires(anonymousDelegate != null);
       if (this.preorderVisitor != null) this.preorderVisitor.Visit(anonymousDelegate);
       if (this.StopTraversal) return;
       this.TraverseChildren(anonymousDelegate);
       if (this.StopTraversal) return;
       if (this.postorderVisitor != null) this.postorderVisitor.Visit(anonymousDelegate);
 }
Exemplo n.º 24
0
 public void Visit(IAnonymousDelegate anonymousDelegate)
 {
     Contract.Requires(anonymousDelegate != null);
       throw new NotImplementedException();
 }
Exemplo n.º 25
0
 public void Visit(IAnonymousDelegate anonymousDelegate)
 {
     this.traverser.Traverse(anonymousDelegate);
 }
Exemplo n.º 26
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public AnonymousDelegate(IAnonymousDelegate anonymousDelegate)
     : base(anonymousDelegate)
 {
     this.body = anonymousDelegate.Body;
       this.callingConvention = anonymousDelegate.CallingConvention;
       this.parameters = new List<IParameterDefinition>(anonymousDelegate.Parameters);
       this.returnType = anonymousDelegate.ReturnType;
       this.returnValueCustomModifiers = new List<ICustomModifier>(anonymousDelegate.ReturnValueCustomModifiers);
       this.returnValueIsByRef = anonymousDelegate.ReturnValueIsByRef;
 }
Exemplo n.º 27
0
 //^ ensures this.path.Count == old(this.path.Count);
 /// <summary>
 /// Traverses the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public virtual void Visit(IAnonymousDelegate anonymousDelegate)
 {
     if (this.stopTraversal) return;
       //^ int oldCount = this.path.Count;
       this.path.Push(anonymousDelegate);
       this.Visit(anonymousDelegate.Parameters);
       this.Visit(anonymousDelegate.Body);
       //^ assume this.path.Count == oldCount+1; //True because all of the virtual methods of this class promise not decrease this.path.Count.
       this.path.Pop();
 }
 public override void Visit(IAnonymousDelegate anonymousDelegate)
 {
     if(Process(anonymousDelegate)){visitor.Visit(anonymousDelegate);}
     base.Visit(anonymousDelegate);
 }
Exemplo n.º 29
0
 public void Visit(IAnonymousDelegate anonymousDelegate)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 30
0
 /// <summary>
 /// Visits the specified anonymous method.
 /// </summary>
 /// <param name="anonymousMethod">The anonymous method.</param>
 public override void Visit(IAnonymousDelegate anonymousMethod)
 {
     AnonymousDelegate mutableAnonymousDelegate = new AnonymousDelegate(anonymousMethod);
     this.resultExpression = this.myCodeCopier.DeepCopy(mutableAnonymousDelegate);
 }
Exemplo n.º 31
0
 /// <summary>
 /// Returns a deep copy of the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public AnonymousDelegate Copy(IAnonymousDelegate anonymousDelegate)
 {
     var mutableCopy = this.shallowCopier.Copy(anonymousDelegate);
       this.CopyChildren((Expression)mutableCopy);
       mutableCopy.Parameters = this.Copy(mutableCopy.Parameters);
       mutableCopy.Body = this.Copy((BlockStatement)mutableCopy.Body);
       mutableCopy.ReturnType = this.Copy(mutableCopy.ReturnType);
       if (mutableCopy.ReturnValueIsModified)
     mutableCopy.ReturnValueCustomModifiers = this.Copy(mutableCopy.ReturnValueCustomModifiers);
       return mutableCopy;
 }
Exemplo n.º 32
0
 /// <summary>
 /// Returns a shallow copy of the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public AnonymousDelegate Copy(IAnonymousDelegate anonymousDelegate)
 {
     return new AnonymousDelegate(anonymousDelegate);
 }
 /// <summary>
 /// Rewrites the given anonymous delegate expression.
 /// </summary>
 public override IExpression Rewrite(IAnonymousDelegate anonymousDelegate) {
   if (this.isInsideAnonymousMethod) return base.Rewrite(anonymousDelegate);
   IMethodReference method = this.CreateClosureMethod((AnonymousDelegate)anonymousDelegate);
   var createDelegate = new CreateDelegateInstance() {
     MethodToCallViaDelegate = method,
     Type = anonymousDelegate.Type
   };
   if (!method.IsStatic) {
     //TODO: if there is reason to believe the delegate will be constructed in a loop, but its closure is constructed before the loop, then cache the delegate in a local
     //that is in the same scope as the closure instance
     if (method.ContainingType == this.currentClosureInstance)
       createDelegate.Instance = this.currentClosureObject;
     else //non static peer method
       createDelegate.Instance = new ThisReference() {
         Type = NamedTypeDefinition.SelfInstance((INamedTypeDefinition)this.method.ContainingTypeDefinition, this.host.InternFactory)
       };
   } else if ((method.CallingConvention & CallingConvention.Generic) == 0) {
     //cache the delegate in a static field (we can only do this if method is not generic, i.e. when at most one instance will be created).
     var cache = this.CreateStaticCacheField(anonymousDelegate.Type);
     var boundField = new BoundExpression() { Definition = cache, Type = cache.Type };
     var statements = new List<IStatement>(1);
     var conditional = new ConditionalStatement() {
       Condition = new Equality() {
         LeftOperand = boundField,
         RightOperand = new CompileTimeConstant() { Value = null, Type = cache.Type },
         Type = this.host.PlatformType.SystemBoolean
       },
       TrueBranch = new ExpressionStatement() {
         Expression = new Assignment() {
           Target = new TargetExpression() { Definition = cache, Type = cache.Type },
           Source = createDelegate,
           Type = cache.Type
         }
       }
     };
     statements.Add(conditional);
     return new BlockExpression() {
       BlockStatement = new BlockStatement() { Statements = statements },
       Expression = boundField
     };
   }
   return createDelegate;
 }
Exemplo n.º 34
0
 public void Visit(IAnonymousDelegate anonymousDelegate)
 {
     this.result = this.copier.Copy(anonymousDelegate);
 }
Exemplo n.º 35
0
    private bool PrintLambdaBody(IAnonymousDelegate lambda) {
      if (lambda.Body.Statements.Count(s => !(s is IEmptyStatement)) != 1) return false;
      var nonEmptyStatement = lambda.Body.Statements.First(s => !(s is IEmptyStatement));

      var returnStatement = nonEmptyStatement as IReturnStatement;
      if (returnStatement != null && returnStatement.Expression != null) {
        this.Traverse(returnStatement.Expression);
        return true;
      }
      var expressionStatement = nonEmptyStatement as IExpressionStatement;
      if (expressionStatement != null) {
        this.Traverse(expressionStatement.Expression);
        return true;
      }
      return false;
    }
Exemplo n.º 36
0
 public override IExpression Rewrite(IAnonymousDelegate anonymousDelegate)
 {
     return(anonymousDelegate);
 }
Exemplo n.º 37
0
 public override void TraverseChildren(IAnonymousDelegate anonymousDelegate)
 {
     MethodEnter(anonymousDelegate);
     base.TraverseChildren(anonymousDelegate);
     MethodExit();
 }
Exemplo n.º 38
0
 /// <summary>
 /// Rewrites the given anonymous delegate expression.
 /// </summary>
 /// <param name="anonymousDelegate"></param>
 public virtual IExpression Rewrite(IAnonymousDelegate anonymousDelegate)
 {
     var mutableAnonymousDelegate = anonymousDelegate as AnonymousDelegate;
       if (mutableAnonymousDelegate == null) return anonymousDelegate;
       this.RewriteChildren(mutableAnonymousDelegate);
       return mutableAnonymousDelegate;
 }