Statement TransformForeachOnArray(ForStatement forStatement) { if (!context.Settings.ForEachStatement) { return(null); } Match m = forOnArrayPattern.Match(forStatement); if (!m.Success) { return(null); } var itemVariable = m.Get <IdentifierExpression>("itemVariable").Single().GetILVariable(); var indexVariable = m.Get <IdentifierExpression>("indexVariable").Single().GetILVariable(); var arrayVariable = m.Get <IdentifierExpression>("arrayVariable").Single().GetILVariable(); var loopContainer = forStatement.Annotation <IL.BlockContainer>(); if (itemVariable == null || indexVariable == null || arrayVariable == null) { return(null); } if (!itemVariable.IsSingleDefinition || (itemVariable.CaptureScope != null && itemVariable.CaptureScope != loopContainer)) { return(null); } if (indexVariable.StoreCount != 2 || indexVariable.LoadCount != 3 || indexVariable.AddressCount != 0) { return(null); } var body = new BlockStatement(); foreach (var statement in m.Get <Statement>("statements")) { body.Statements.Add(statement.Detach()); } var foreachStmt = new ForeachStatement { VariableType = context.Settings.AnonymousTypes && itemVariable.Type.ContainsAnonymousType() ? new SimpleType("var") : context.TypeSystemAstBuilder.ConvertType(itemVariable.Type), VariableName = itemVariable.Name, InExpression = m.Get <IdentifierExpression>("arrayVariable").Single().Detach(), EmbeddedStatement = body }; foreachStmt.CopyAnnotationsFrom(forStatement); itemVariable.Kind = IL.VariableKind.ForeachLocal; // Add the variable annotation for highlighting (TokenTextWriter expects it directly on the ForeachStatement). foreachStmt.AddAnnotation(new ILVariableResolveResult(itemVariable, itemVariable.Type)); // TODO : add ForeachAnnotation forStatement.ReplaceWith(foreachStmt); return(foreachStmt); }
bool MatchForeachOnMultiDimArray(IL.ILVariable[] upperBounds, IL.ILVariable collection, Statement firstInitializerStatement, out IdentifierExpression foreachVariable, out IList <Statement> statements, out IL.ILVariable[] lowerBounds) { int i = 0; foreachVariable = null; statements = null; lowerBounds = new IL.ILVariable[upperBounds.Length]; Statement stmt = firstInitializerStatement; Match m = default(Match); while (i < upperBounds.Length && MatchLowerBound(i, out IL.ILVariable indexVariable, collection, stmt)) { m = forOnArrayMultiDimPattern.Match(stmt.GetNextStatement()); if (!m.Success) { return(false); } var upperBound = m.Get <IdentifierExpression>("upperBoundVariable").Single().GetILVariable(); if (upperBounds[i] != upperBound) { return(false); } stmt = m.Get <Statement>("lowerBoundAssign").Single(); lowerBounds[i] = indexVariable; i++; } var m2 = foreachVariableOnMultArrayAssignPattern.Match(stmt); if (!m2.Success) { return(false); } var collection2 = m2.Get <IdentifierExpression>("collection").Single().GetILVariable(); if (collection2 != collection) { return(false); } foreachVariable = m2.Get <IdentifierExpression>("variable").Single(); statements = m.Get <Statement>("statements").ToList(); return(true); }