private void FixSpacingAroundKeywordForeach(IForEachStatement item)
 {
     List<string> keywordSearch = new List<string> { "foreach" };
     foreach (var key in keywordSearch)
     {
         item.Text = this.whiteSpaceHelper.RemoveWhiteSpaceAroundKeyword(item.Text, key);
     }
 }
        private void CheckIssuesForReferencedLocalOrField(CheckCodeIssuesEventArgs ea, IForEachStatement loop, IAnonymousMethodExpression lambda, IElement element)
        {
            if (element.ElementType == LanguageElementType.ElementReferenceExpression)
            {
                IElement declaration = element.GetDeclaration();
                if (declaration.ElementType == LanguageElementType.Variable || declaration.ElementType == LanguageElementType.InitializedVariable)
                {
                    bool isLoopVariable = loop.LoopVariable == declaration;
                    bool declarationIsParentedByLoop = declaration.IsParentedBy(loop);
                    if (isLoopVariable || !declarationIsParentedByLoop)
                    {
                        ea.AddWarning(element.ToLanguageElement().Range, "Possible unintended closure scope misuse");
                    }
                }
                return;
            }

            foreach (IElement child in element.Children)
            {
                CheckIssuesForReferencedLocalOrField(ea, loop, lambda, child);
            }
        }
 public override void Visit(IForEachStatement forEachStatement)
 {
     if(Process(forEachStatement)){visitor.Visit(forEachStatement);}
     base.Visit(forEachStatement);
 }
 public override void TraverseChildren(IForEachStatement forEachStatement) {
   if (this.currentAnonymousDelegate != null)
     this.definitionsToIgnore[forEachStatement.Variable] = true;
   base.TraverseChildren(forEachStatement);
 }
Beispiel #5
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="forEachStatement"></param>
 public ForEachStatement(IForEachStatement forEachStatement)
   : base(forEachStatement) {
   this.body = forEachStatement.Body;
   this.collection = forEachStatement.Collection;
   this.variable = forEachStatement.Variable;
 }
Beispiel #6
0
 public void Visit(IForEachStatement forEachStatement)
 {
     Contract.Requires(forEachStatement != null);
       throw new NotImplementedException();
 }
Beispiel #7
0
 public void Visit(IForEachStatement forEachStatement)
 {
     this.traverser.Traverse(forEachStatement);
 }
Beispiel #8
0
 /// <summary>
 /// Traverses the foreach statement.
 /// </summary>
 public void Traverse(IForEachStatement forEachStatement)
 {
     Contract.Requires(forEachStatement != null);
       if (this.preorderVisitor != null) this.preorderVisitor.Visit(forEachStatement);
       if (this.StopTraversal) return;
       this.TraverseChildren(forEachStatement);
       if (this.StopTraversal) return;
       if (this.postorderVisitor != null) this.postorderVisitor.Visit(forEachStatement);
 }
Beispiel #9
0
    /// <summary>
    /// Returns a shallow copy of the given foreach statement.
    /// </summary>
    /// <param name="forEachStatement"></param>
    public ForEachStatement Copy(IForEachStatement forEachStatement) {
      Contract.Requires(forEachStatement != null);
      Contract.Ensures(Contract.Result<ForEachStatement>() != null);

      return new ForEachStatement(forEachStatement);
    }
Beispiel #10
0
    /// <summary>
    /// Returns a deep copy of the given foreach statement.
    /// </summary>
    /// <param name="forEachStatement"></param>
    public ForEachStatement Copy(IForEachStatement forEachStatement) {
      Contract.Requires(forEachStatement != null);
      Contract.Ensures(Contract.Result<ForEachStatement>() != null);

      var mutableCopy = this.shallowCopier.Copy(forEachStatement);
      mutableCopy.Variable = this.Copy(mutableCopy.Variable);
      this.LocalsInsideCone.Add(forEachStatement.Variable, mutableCopy.Variable);
      mutableCopy.Collection = this.Copy(mutableCopy.Collection);
      mutableCopy.Body = this.Copy(mutableCopy.Body);
      return mutableCopy;
    }
 /// <summary>
 /// Rewrites the given foreach statement.
 /// </summary>
 /// <param name="forEachStatement"></param>
 public virtual IStatement Rewrite(IForEachStatement forEachStatement)
 {
     return forEachStatement;
 }
Beispiel #12
0
        /// <summary />
        public override IStatement Rewrite(IForEachStatement forEachStatement)
        {
            ILocalDefinition foreachLocal;
            var key = forEachStatement.Collection.Type.InternedKey;

            ITypeReference enumeratorType;
            IMethodReference getEnumerator;
            IMethodReference getCurrent;

            var gtir = forEachStatement.Collection.Type as IGenericTypeInstanceReference;
            if (gtir != null)
            {
                var typeArguments = gtir.GenericArguments;
                ITypeReference genericEnumeratorType = new Immutable.GenericTypeInstanceReference(this.host.PlatformType.SystemCollectionsGenericIEnumerator, typeArguments, this.host.InternFactory);
                ITypeReference genericEnumerableType = new Immutable.GenericTypeInstanceReference(this.host.PlatformType.SystemCollectionsGenericIEnumerable, typeArguments, this.host.InternFactory);
                enumeratorType = genericEnumeratorType;
                getEnumerator = new SpecializedMethodReference()
                {
                    CallingConvention = CallingConvention.HasThis,
                    ContainingType = genericEnumerableType,
                    InternFactory = this.host.InternFactory,
                    Name = this.host.NameTable.GetNameFor("GetEnumerator"),
                    Parameters = new List<IParameterTypeInformation>(),
                    Type = genericEnumeratorType,
                    UnspecializedVersion = new MethodReference()
                    {
                        CallingConvention = CallingConvention.HasThis,
                        ContainingType = this.host.PlatformType.SystemCollectionsGenericIEnumerable,
                        InternFactory = this.host.InternFactory,
                        Name = this.host.NameTable.GetNameFor("GetEnumerator"),
                        Parameters = new List<IParameterTypeInformation>(),
                        Type = this.host.PlatformType.SystemCollectionsGenericIEnumerator,
                    },
                };
                var getEnumerator2 = (IMethodReference) 
                    IteratorHelper.First(genericEnumerableType.ResolvedType.GetMembersNamed(this.host.NameTable.GetNameFor("GetEnumerator"), false));
                getEnumerator = getEnumerator2;
                getCurrent = (IMethodReference) IteratorHelper.First(genericEnumeratorType.ResolvedType.GetMembersNamed(this.host.NameTable.GetNameFor("get_Current"), false));
            }
            else
            {
                enumeratorType = this.host.PlatformType.SystemCollectionsIEnumerator;
                getEnumerator = new MethodReference()
                {
                    CallingConvention = CallingConvention.HasThis,
                    ContainingType = enumeratorType,
                    InternFactory = this.host.InternFactory,
                    Name = this.host.NameTable.GetNameFor("GetEnumerator"),
                    Parameters = new List<IParameterTypeInformation>(),
                    Type = this.host.PlatformType.SystemCollectionsIEnumerable,
                };
                getCurrent = new MethodReference()
                {
                    CallingConvention = CallingConvention.HasThis,
                    ContainingType = enumeratorType,
                    InternFactory = this.host.InternFactory,
                    Name = this.host.NameTable.GetNameFor("get_Current"),
                    Parameters = new List<IParameterTypeInformation>(),
                    Type = this.host.PlatformType.SystemObject,
                };
            }

            var initializer = new MethodCall()
                    {
                        Arguments = new List<IExpression>(),
                        IsStaticCall = false,
                        IsVirtualCall = true,
                        MethodToCall = getEnumerator,
                        ThisArgument = forEachStatement.Collection,
                        Type = enumeratorType,
                    };
            IStatement initialization;

            if (!this.foreachLocals.TryGetValue(key, out foreachLocal))
            {
                foreachLocal = new LocalDefinition() { Type = enumeratorType, Name = this.host.NameTable.GetNameFor("CS$5$" + this.foreachLocals.Count) };
                this.foreachLocals.Add(key, foreachLocal);
                initialization = new LocalDeclarationStatement()
                {
                    InitialValue = initializer,
                    LocalVariable = foreachLocal,
                };
            }
            else
            {
                initialization = new ExpressionStatement()
                {
                    Expression = new Assignment()
                    {
                        Source = initializer,
                        Target = new TargetExpression()
                        {
                            Definition = foreachLocal,
                            Instance = null,
                            Type = foreachLocal.Type,
                        },
                        Type = foreachLocal.Type,
                    },
                };
            }

            var newStmts = new List<IStatement>();
            newStmts.Add(new ExpressionStatement(){
                                                Expression = new Assignment(){
                                                     Source = new MethodCall(){
                                                          Arguments = new List<IExpression>(),
                                                          IsStaticCall = false,
                                                          IsVirtualCall = true,
                                                          MethodToCall = getCurrent,
                                                          ThisArgument = new BoundExpression(){
                                                               Definition = foreachLocal,
                                                               Instance = null,
                                                          },
                                                          Type = forEachStatement.Variable.Type,
                                                     },
                                                      Target = new TargetExpression(){
                                                           Definition = forEachStatement.Variable,
                                                           Instance = null,
                                                      },
                                                       Type = forEachStatement.Variable.Type,
                                                },
                                           });
            newStmts.Add(forEachStatement.Body);
            var newBody = new BlockStatement(){ Statements = newStmts,}; 
            var result = new BlockStatement()
            {
                Statements = new List<IStatement>(){
                   initialization,
                   new TryCatchFinallyStatement(){
                       TryBody = new BlockStatement() {
                           Statements = new List<IStatement>(){
                               new WhileDoStatement(){
                                   Body = newBody,
                                   Condition = new MethodCall(){
                                       Arguments = new List<IExpression>(),
                                       IsStaticCall = false,
                                       IsVirtualCall = true,
                                       MethodToCall = moveNext,
                                       ThisArgument = new BoundExpression(){ 
                                           Definition = foreachLocal,
                                           Instance = null,
                                       },
                                       Type = this.host.PlatformType.SystemBoolean,
                                   },
                               },
                           },
                       },
                       FinallyBody = new BlockStatement() {
                           Statements = new List<IStatement>(){
                               new ConditionalStatement(){
                                   Condition = new Equality(){
                                       LeftOperand = new BoundExpression(){ Definition = foreachLocal, Instance = null, Type = foreachLocal.Type, },
                                       RightOperand = new CompileTimeConstant(){ Type = foreachLocal.Type, Value = null, },
                                       Type = this.host.PlatformType.SystemBoolean,
                                   },
                                   FalseBranch = new EmptyStatement(),
                                   TrueBranch = new ExpressionStatement(){
                                       Expression = new MethodCall(){
                                           Arguments = new List<IExpression>(),
                                           IsStaticCall = false,
                                           IsVirtualCall = true,
                                           MethodToCall = this.disposeMethod,
                                           ThisArgument = new BoundExpression(){ 
                                               Definition = foreachLocal,
                                               Instance = null,
                                           },
                                           Type = this.host.PlatformType.SystemVoid,
                                       },
                                   },
                               },
                           },
                       },
                   },
                },
            };
            return result;
        }
Beispiel #13
0
 /// <summary>
 /// Generates IL for the specified for each statement.
 /// </summary>
 /// <param name="forEachStatement">For each statement.</param>
 public override void TraverseChildren(IForEachStatement forEachStatement)
 {
     var arrayType = forEachStatement.Collection.Type as IArrayTypeReference;
       if (arrayType != null && arrayType.IsVector) {
     this.VisitForeachArrayElement(forEachStatement, arrayType);
     return;
       }
       //TODO: special case for enumerator that is sealed and does not implement IDisposable
       base.TraverseChildren(forEachStatement);
       this.lastStatementWasUnconditionalTransfer = false;
 }
Beispiel #14
0
        /// <summary>
        /// Generates IL code for the given for each statement for the special case where the collection is known
        /// to be vector type.
        /// </summary>
        /// <param name="forEachStatement">The foreach statement to visit.</param>
        /// <param name="arrayType">The vector type of the collection.</param>
        public virtual void VisitForeachArrayElement(IForEachStatement forEachStatement, IArrayTypeReference arrayType)
        {
            Contract.Requires(arrayType.IsVector);
              ILGeneratorLabel savedCurrentBreakTarget = this.currentBreakTarget;
              ILGeneratorLabel savedCurrentContinueTarget = this.currentContinueTarget;
              this.currentBreakTarget = new ILGeneratorLabel();
              this.currentContinueTarget = new ILGeneratorLabel();
              if (this.currentTryCatch != null) {
            this.mostNestedTryCatchFor.Add(this.currentBreakTarget, this.currentTryCatch);
            this.mostNestedTryCatchFor.Add(this.currentContinueTarget, this.currentTryCatch);
              }
              ILGeneratorLabel conditionCheck = new ILGeneratorLabel();
              ILGeneratorLabel loopStart = new ILGeneratorLabel();

              this.EmitSequencePoint(forEachStatement.Variable.Locations);
              this.Traverse(forEachStatement.Collection);
              this.generator.Emit(OperationCode.Dup);
              var array = new TemporaryVariable(arrayType, this.method);
              this.VisitAssignmentTo(array);
              var length = new TemporaryVariable(this.host.PlatformType.SystemInt32, this.method);
              this.generator.Emit(OperationCode.Ldlen);
              this.generator.Emit(OperationCode.Conv_I4);
              this.VisitAssignmentTo(length);
              var counter = new TemporaryVariable(this.host.PlatformType.SystemInt32, this.method);
              this.generator.Emit(OperationCode.Ldc_I4_0);
              this.StackSize++;
              this.VisitAssignmentTo(counter);
              this.generator.Emit(OperationCode.Br, conditionCheck);
              this.generator.MarkLabel(loopStart);
              this.LoadLocal(array);
              this.LoadLocal(counter);
              this.LoadVectorElement(arrayType.ElementType);
              this.VisitAssignmentTo(forEachStatement.Variable);
              this.Traverse(forEachStatement.Body);
              this.generator.MarkLabel(this.currentContinueTarget);
              this.LoadLocal(counter);
              this.generator.Emit(OperationCode.Ldc_I4_1);
              this.StackSize++;
              this.generator.Emit(OperationCode.Add);
              this.StackSize--;
              this.VisitAssignmentTo(counter);
              this.generator.MarkLabel(conditionCheck);
              this.EmitSequencePoint(forEachStatement.Collection.Locations);
              this.LoadLocal(counter);
              this.LoadLocal(length);
              this.generator.Emit(OperationCode.Blt, loopStart);
              this.generator.MarkLabel(this.currentBreakTarget);

              this.currentBreakTarget = savedCurrentBreakTarget;
              this.currentContinueTarget = savedCurrentContinueTarget;
        }
 public override void TraverseChildren(IForEachStatement forEachStatement) {
   base.TraverseChildren(forEachStatement);
 }
Beispiel #16
0
 //^ ensures this.path.Count == old(this.path.Count);
 /// <summary>
 /// Traverses the given foreach statement.
 /// </summary>
 /// <param name="forEachStatement"></param>
 public virtual void Visit(IForEachStatement forEachStatement)
 {
     if (this.stopTraversal) return;
       //^ int oldCount = this.path.Count;
       this.path.Push(forEachStatement);
       this.Visit(forEachStatement.Collection);
       this.Visit(forEachStatement.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();
 }
Beispiel #17
0
 /// <summary>
 /// Performs some computation with the given foreach statement.
 /// </summary>
 /// <param name="forEachStatement"></param>
 public virtual void Visit(IForEachStatement forEachStatement)
 {
 }
Beispiel #18
0
    public override void TraverseChildren(IForEachStatement forEachStatement) {
		this.sourceEmitterOutput.Write("foreach (", true);
		this.PrintLocalDefinition(forEachStatement.Variable);
		this.sourceEmitterOutput.Write(" in ");
		this.Traverse(forEachStatement.Collection);
		this.sourceEmitterOutput.WriteLine(")");
		this.Traverse(forEachStatement.Body);
    }
Beispiel #19
0
 /// <summary>
 /// Traverses the children of the foreach statement.
 /// </summary>
 public virtual void TraverseChildren(IForEachStatement forEachStatement)
 {
     Contract.Requires(forEachStatement != null);
       this.TraverseChildren((IStatement)forEachStatement);
       if (this.StopTraversal) return;
       this.Traverse(forEachStatement.Variable);
       if (this.StopTraversal) return;
       this.Traverse(forEachStatement.Collection);
       if (this.StopTraversal) return;
       this.Traverse(forEachStatement.Body);
 }
Beispiel #20
0
 /// <summary>
 /// Visits the specified for each statement.
 /// </summary>
 /// <param name="forEachStatement">For each statement.</param>
 public override void Visit(IForEachStatement forEachStatement)
 {
     ForEachStatement mutableForEachStatement = new ForEachStatement(forEachStatement);
     this.resultStatement = this.myCodeCopier.DeepCopy(mutableForEachStatement);
 }
Beispiel #21
0
 /// <summary>
 /// Performs some computation with the given foreach statement.
 /// </summary>
 /// <param name="forEachStatement"></param>
 public virtual void Visit(IForEachStatement forEachStatement)
 {
     this.Visit((IStatement)forEachStatement);
 }
Beispiel #22
0
 /// <summary>
 /// Returns a deep copy of the given foreach statement.
 /// </summary>
 /// <param name="forEachStatement"></param>
 public ForEachStatement Copy(IForEachStatement forEachStatement)
 {
     var mutableCopy = this.shallowCopier.Copy(forEachStatement);
       mutableCopy.Variable = this.Copy(mutableCopy.Variable);
       this.LocalsInsideCone.Add(forEachStatement.Variable, mutableCopy.Variable);
       mutableCopy.Collection = this.Copy(mutableCopy.Collection);
       mutableCopy.Body = this.Copy(mutableCopy.Body);
       return mutableCopy;
 }
Beispiel #23
0
 public void Visit(IForEachStatement forEachStatement)
 {
     throw new NotImplementedException();
 }
Beispiel #24
0
 /// <summary>
 /// Returns a shallow copy of the given foreach statement.
 /// </summary>
 /// <param name="forEachStatement"></param>
 public ForEachStatement Copy(IForEachStatement forEachStatement)
 {
     return new ForEachStatement(forEachStatement);
 }
 private void FixSpacingForEach(IForEachStatement item)
 {
 }
Beispiel #26
0
 public void Visit(IForEachStatement forEachStatement)
 {
     this.result = this.copier.Copy(forEachStatement);
 }
 public override void TraverseChildren(IForEachStatement forEachStatement) {
   if (this.captures.ContainsKey(forEachStatement.Variable))
     this.scopesWithCapturedLocals[forEachStatement] = true;
   base.TraverseChildren(forEachStatement);
 }
        public override void TraverseChildren(IForEachStatement forEachStatement)
{ MethodEnter(forEachStatement);
            base.TraverseChildren(forEachStatement);
     MethodExit();   }
 public virtual void onASTElement(IForEachStatement forEachStatement) { }
            private void WriteForEachStatement(IForEachStatement value, IFormatter formatter)
            {
                // TODO statement.Variable declaration needs to be rendered some where

                this.WriteStatementSeparator(formatter);

                TextFormatter description = new TextFormatter();
                this.WriteVariableDeclaration(value.Variable, description);

                formatter.WriteLine();
                formatter.WriteKeyword("foreach");
                formatter.Write(" (");
                formatter.WriteReference(value.Variable.Name, description.ToString(), null);
                formatter.WriteKeyword(" in ");
                this.WriteExpression(value.Expression, formatter);
                formatter.Write(") {");
                formatter.WriteLine();
                formatter.WriteIndent();

                if (value.Body != null)
                {
                    this.WriteStatement(value.Body, formatter);
                }

                formatter.WriteLine();
                formatter.WriteOutdent();
                formatter.WriteKeyword("}");
            }