public override object VisitForStatement(ForStatement forStatement, object data)
		{
			// uses LocalVariableDeclaration, we just have to put the end location on the stack
			if (forStatement.EmbeddedStatement.EndLocation.IsEmpty) {
				return base.VisitForStatement(forStatement, data);
			} else {
				endLocationStack.Push(forStatement.EmbeddedStatement.EndLocation);
				base.VisitForStatement(forStatement, data);
				endLocationStack.Pop();
				return null;
			}
		}
		public virtual object VisitForStatement(ForStatement forStatement, object data) {
			Debug.Assert((forStatement != null));
			Debug.Assert((forStatement.Initializers != null));
			Debug.Assert((forStatement.Condition != null));
			Debug.Assert((forStatement.Iterator != null));
			Debug.Assert((forStatement.EmbeddedStatement != null));
			foreach (Statement o in forStatement.Initializers) {
				Debug.Assert(o != null);
				o.AcceptVisitor(this, data);
			}
			forStatement.Condition.AcceptVisitor(this, data);
			foreach (Statement o in forStatement.Iterator) {
				Debug.Assert(o != null);
				o.AcceptVisitor(this, data);
			}
			return forStatement.EmbeddedStatement.AcceptVisitor(this, data);
		}
		public sealed override object VisitForStatement(ForStatement forStatement, object data) {
			this.BeginVisit(forStatement);
			object result = this.TrackedVisitForStatement(forStatement, data);
			this.EndVisit(forStatement);
			return result;
		}
示例#4
0
	void EmbeddedStatement(
#line  1550 "Frames/cs.ATG" 
out Statement statement) {

#line  1552 "Frames/cs.ATG" 
		TypeReference type = null;
		Expression expr = null;
		Statement embeddedStatement = null;
		statement = null;
		

#line  1558 "Frames/cs.ATG" 
		Location startLocation = la.Location; 
		if (la.kind == 16) {
			Block(
#line  1560 "Frames/cs.ATG" 
out statement);
		} else if (la.kind == 11) {
			lexer.NextToken();

#line  1563 "Frames/cs.ATG" 
			statement = new EmptyStatement(); 
		} else if (
#line  1566 "Frames/cs.ATG" 
UnCheckedAndLBrace()) {

#line  1566 "Frames/cs.ATG" 
			Statement block; bool isChecked = true; 
			if (la.kind == 58) {
				lexer.NextToken();
			} else if (la.kind == 118) {
				lexer.NextToken();

#line  1567 "Frames/cs.ATG" 
				isChecked = false;
			} else SynErr(197);
			Block(
#line  1568 "Frames/cs.ATG" 
out block);

#line  1568 "Frames/cs.ATG" 
			statement = isChecked ? (Statement)new CheckedStatement(block) : (Statement)new UncheckedStatement(block); 
		} else if (la.kind == 79) {
			IfStatement(
#line  1571 "Frames/cs.ATG" 
out statement);
		} else if (la.kind == 110) {
			lexer.NextToken();

#line  1573 "Frames/cs.ATG" 
			List<SwitchSection> switchSections = new List<SwitchSection>(); 
			Expect(20);
			Expr(
#line  1574 "Frames/cs.ATG" 
out expr);
			Expect(21);
			Expect(16);
			SwitchSections(
#line  1575 "Frames/cs.ATG" 
switchSections);
			Expect(17);

#line  1577 "Frames/cs.ATG" 
			statement = new SwitchStatement(expr, switchSections); 
		} else if (la.kind == 125) {
			lexer.NextToken();
			Expect(20);
			Expr(
#line  1580 "Frames/cs.ATG" 
out expr);
			Expect(21);
			EmbeddedStatement(
#line  1581 "Frames/cs.ATG" 
out embeddedStatement);

#line  1582 "Frames/cs.ATG" 
			statement = new DoLoopStatement(expr, embeddedStatement, ConditionType.While, ConditionPosition.Start);
		} else if (la.kind == 65) {
			lexer.NextToken();
			EmbeddedStatement(
#line  1584 "Frames/cs.ATG" 
out embeddedStatement);
			Expect(125);
			Expect(20);
			Expr(
#line  1585 "Frames/cs.ATG" 
out expr);
			Expect(21);
			Expect(11);

#line  1586 "Frames/cs.ATG" 
			statement = new DoLoopStatement(expr, embeddedStatement, ConditionType.While, ConditionPosition.End); 
		} else if (la.kind == 76) {
			lexer.NextToken();

#line  1588 "Frames/cs.ATG" 
			List<Statement> initializer = null; List<Statement> iterator = null; 
			Expect(20);
			if (StartOf(6)) {
				ForInitializer(
#line  1589 "Frames/cs.ATG" 
out initializer);
			}
			Expect(11);
			if (StartOf(6)) {
				Expr(
#line  1590 "Frames/cs.ATG" 
out expr);
			}
			Expect(11);
			if (StartOf(6)) {
				ForIterator(
#line  1591 "Frames/cs.ATG" 
out iterator);
			}
			Expect(21);
			EmbeddedStatement(
#line  1592 "Frames/cs.ATG" 
out embeddedStatement);

#line  1593 "Frames/cs.ATG" 
			statement = new ForStatement(initializer, expr, iterator, embeddedStatement); 
		} else if (la.kind == 77) {
			lexer.NextToken();
			Expect(20);
			Type(
#line  1595 "Frames/cs.ATG" 
out type);
			Identifier();

#line  1595 "Frames/cs.ATG" 
			string varName = t.val; 
			Expect(81);
			Expr(
#line  1596 "Frames/cs.ATG" 
out expr);
			Expect(21);
			EmbeddedStatement(
#line  1597 "Frames/cs.ATG" 
out embeddedStatement);

#line  1598 "Frames/cs.ATG" 
			statement = new ForeachStatement(type, varName , expr, embeddedStatement); 
		} else if (la.kind == 53) {
			lexer.NextToken();
			Expect(11);

#line  1601 "Frames/cs.ATG" 
			statement = new BreakStatement(); 
		} else if (la.kind == 61) {
			lexer.NextToken();
			Expect(11);

#line  1602 "Frames/cs.ATG" 
			statement = new ContinueStatement(); 
		} else if (la.kind == 78) {
			GotoStatement(
#line  1603 "Frames/cs.ATG" 
out statement);
		} else if (
#line  1605 "Frames/cs.ATG" 
IsYieldStatement()) {
			Expect(132);
			if (la.kind == 101) {
				lexer.NextToken();
				Expr(
#line  1606 "Frames/cs.ATG" 
out expr);

#line  1606 "Frames/cs.ATG" 
				statement = new YieldStatement(new ReturnStatement(expr)); 
			} else if (la.kind == 53) {
				lexer.NextToken();

#line  1607 "Frames/cs.ATG" 
				statement = new YieldStatement(new BreakStatement()); 
			} else SynErr(198);
			Expect(11);
		} else if (la.kind == 101) {
			lexer.NextToken();
			if (StartOf(6)) {
				Expr(
#line  1610 "Frames/cs.ATG" 
out expr);
			}
			Expect(11);

#line  1610 "Frames/cs.ATG" 
			statement = new ReturnStatement(expr); 
		} else if (la.kind == 112) {
			lexer.NextToken();
			if (StartOf(6)) {
				Expr(
#line  1611 "Frames/cs.ATG" 
out expr);
			}
			Expect(11);

#line  1611 "Frames/cs.ATG" 
			statement = new ThrowStatement(expr); 
		} else if (StartOf(6)) {
			StatementExpr(
#line  1614 "Frames/cs.ATG" 
out statement);
			while (!(la.kind == 0 || la.kind == 11)) {SynErr(199); lexer.NextToken(); }
			Expect(11);
		} else if (la.kind == 114) {
			TryStatement(
#line  1617 "Frames/cs.ATG" 
out statement);
		} else if (la.kind == 86) {
			lexer.NextToken();
			Expect(20);
			Expr(
#line  1620 "Frames/cs.ATG" 
out expr);
			Expect(21);
			EmbeddedStatement(
#line  1621 "Frames/cs.ATG" 
out embeddedStatement);

#line  1621 "Frames/cs.ATG" 
			statement = new LockStatement(expr, embeddedStatement); 
		} else if (la.kind == 121) {

#line  1624 "Frames/cs.ATG" 
			Statement resourceAcquisitionStmt = null; 
			lexer.NextToken();
			Expect(20);
			ResourceAcquisition(
#line  1626 "Frames/cs.ATG" 
out resourceAcquisitionStmt);
			Expect(21);
			EmbeddedStatement(
#line  1627 "Frames/cs.ATG" 
out embeddedStatement);

#line  1627 "Frames/cs.ATG" 
			statement = new UsingStatement(resourceAcquisitionStmt, embeddedStatement); 
		} else if (la.kind == 119) {
			lexer.NextToken();
			Block(
#line  1630 "Frames/cs.ATG" 
out embeddedStatement);

#line  1630 "Frames/cs.ATG" 
			statement = new UnsafeStatement(embeddedStatement); 
		} else if (la.kind == 74) {

#line  1632 "Frames/cs.ATG" 
			Statement pointerDeclarationStmt = null; 
			lexer.NextToken();
			Expect(20);
			ResourceAcquisition(
#line  1634 "Frames/cs.ATG" 
out pointerDeclarationStmt);
			Expect(21);
			EmbeddedStatement(
#line  1635 "Frames/cs.ATG" 
out embeddedStatement);

#line  1635 "Frames/cs.ATG" 
			statement = new FixedStatement(pointerDeclarationStmt, embeddedStatement); 
		} else SynErr(200);

#line  1637 "Frames/cs.ATG" 
		if (statement != null) {
		statement.StartLocation = startLocation;
		statement.EndLocation = t.EndLocation;
		}
		
	}
示例#5
0
			public void VisitStatement (ForStatement statement, MethodProperties meth)
			{
				meth.CyclometricComplexity++;
				VisitExpression(statement.Condition, meth);
				
				foreach(var innerStatement in ((ForStatement)statement).EmbeddedStatement.Children)
					VisitStatement(innerStatement, meth);
			}
		public virtual object TrackedVisitForStatement(ForStatement forStatement, object data) {
			return base.VisitForStatement(forStatement, data);
		}
		public virtual object VisitForStatement(ForStatement forStatement, object data) {
			Debug.Assert((forStatement != null));
			Debug.Assert((forStatement.Initializers != null));
			Debug.Assert((forStatement.Condition != null));
			Debug.Assert((forStatement.Iterator != null));
			Debug.Assert((forStatement.EmbeddedStatement != null));
			for (int i = 0; i < forStatement.Initializers.Count; i++) {
				Statement o = forStatement.Initializers[i];
				Debug.Assert(o != null);
				nodeStack.Push(o);
				o.AcceptVisitor(this, data);
				o = (Statement)nodeStack.Pop();
				if (o == null)
					forStatement.Initializers.RemoveAt(i--);
				else
					forStatement.Initializers[i] = o;
			}
			nodeStack.Push(forStatement.Condition);
			forStatement.Condition.AcceptVisitor(this, data);
			forStatement.Condition = ((Expression)(nodeStack.Pop()));
			for (int i = 0; i < forStatement.Iterator.Count; i++) {
				Statement o = forStatement.Iterator[i];
				Debug.Assert(o != null);
				nodeStack.Push(o);
				o.AcceptVisitor(this, data);
				o = (Statement)nodeStack.Pop();
				if (o == null)
					forStatement.Iterator.RemoveAt(i--);
				else
					forStatement.Iterator[i] = o;
			}
			nodeStack.Push(forStatement.EmbeddedStatement);
			forStatement.EmbeddedStatement.AcceptVisitor(this, data);
			forStatement.EmbeddedStatement = ((Statement)(nodeStack.Pop()));
			return null;
		}
		public virtual object VisitForStatement(ForStatement forStatement, object data) {
			throw new global::System.NotImplementedException("ForStatement");
		}
		void ConvertForStatement(ForStatement forStatement)
		{
			//   ForStatement -> ForNextStatement when for-loop is simple
			
			// only the following forms of the for-statement are allowed:
			// for (TypeReference name = start; name < oneAfterEnd; name += step)
			// for (name = start; name < oneAfterEnd; name += step)
			// for (TypeReference name = start; name <= end; name += step)
			// for (name = start; name <= end; name += step)
			// for (TypeReference name = start; name > oneAfterEnd; name -= step)
			// for (name = start; name > oneAfterEnd; name -= step)
			// for (TypeReference name = start; name >= end; name -= step)
			// for (name = start; name >= end; name -= step)
			
			// check if the form is valid and collect TypeReference, name, start, end and step
			if (forStatement.Initializers.Count != 1)
				return;
			if (forStatement.Iterator.Count != 1)
				return;
			ExpressionStatement statement = forStatement.Iterator[0] as ExpressionStatement;
			if (statement == null)
				return;
			AssignmentExpression iterator = statement.Expression as AssignmentExpression;
			if (iterator == null || (iterator.Op != AssignmentOperatorType.Add && iterator.Op != AssignmentOperatorType.Subtract))
				return;
			IdentifierExpression iteratorIdentifier = iterator.Left as IdentifierExpression;
			if (iteratorIdentifier == null)
				return;
			PrimitiveExpression stepExpression = iterator.Right as PrimitiveExpression;
			if (stepExpression == null || !(stepExpression.Value is int))
				return;
			int step = (int)stepExpression.Value;
			if (iterator.Op == AssignmentOperatorType.Subtract)
				step = -step;
			
			BinaryOperatorExpression condition = forStatement.Condition as BinaryOperatorExpression;
			if (condition == null || !(condition.Left is IdentifierExpression))
				return;
			if ((condition.Left as IdentifierExpression).Identifier != iteratorIdentifier.Identifier)
				return;
			Expression end;
			if (iterator.Op == AssignmentOperatorType.Subtract) {
				if (condition.Op == BinaryOperatorType.GreaterThanOrEqual) {
					end = condition.Right;
				} else if (condition.Op == BinaryOperatorType.GreaterThan) {
					end = Expression.AddInteger(condition.Right, 1);
				} else {
					return;
				}
			} else {
				if (condition.Op == BinaryOperatorType.LessThanOrEqual) {
					end = condition.Right;
				} else if (condition.Op == BinaryOperatorType.LessThan) {
					end = Expression.AddInteger(condition.Right, -1);
				} else {
					return;
				}
			}
			
			Expression start;
			TypeReference typeReference = null;
			LocalVariableDeclaration varDecl = forStatement.Initializers[0] as LocalVariableDeclaration;
			if (varDecl != null) {
				if (varDecl.Variables.Count != 1
				    || varDecl.Variables[0].Name != iteratorIdentifier.Identifier
				    || varDecl.Variables[0].Initializer == null)
					return;
				typeReference = varDecl.GetTypeForVariable(0);
				start = varDecl.Variables[0].Initializer;
			} else {
				statement = forStatement.Initializers[0] as ExpressionStatement;
				if (statement == null)
					return;
				AssignmentExpression assign = statement.Expression as AssignmentExpression;
				if (assign == null || assign.Op != AssignmentOperatorType.Assign)
					return;
				if (!(assign.Left is IdentifierExpression))
					return;
				if ((assign.Left as IdentifierExpression).Identifier != iteratorIdentifier.Identifier)
					return;
				start = assign.Right;
			}
			
			ReplaceCurrentNode(
				new ForNextStatement {
					TypeReference = typeReference,
					VariableName = iteratorIdentifier.Identifier,
					Start = start,
					End = end,
					Step = (step == 1) ? null : new PrimitiveExpression(step, step.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)),
					EmbeddedStatement = forStatement.EmbeddedStatement
				});
		}
		public override object VisitForStatement(ForStatement forStatement, object data)
		{
			base.VisitForStatement(forStatement, data);
			ConvertForStatement(forStatement);
			return null;
		}
		public override object VisitForStatement(ForStatement forStatement, object data)
		{
			CodeIterationStatement forLoop = new CodeIterationStatement();
			breakableStack.Push(new Breakable());

			codeStack.Push(NullStmtCollection);

			if (forStatement.Initializers.Count > 0)
			{
				if (forStatement.Initializers.Count > 1)
				{
					throw new NotSupportedException("CodeDom does not support Multiple For-Loop Initializer Statements");
				}

				foreach (object o in forStatement.Initializers)
				{
					if (o is Expression)
					{
						forLoop.InitStatement = new CodeExpressionStatement((CodeExpression)((Expression)o).AcceptVisitor(this, data));
					}
					if (o is Statement)
					{
						forLoop.InitStatement = (CodeStatement)((Statement)o).AcceptVisitor(this, data);
					}
				}
			}
			else
			{
				// RG: need to handle empty InitStatement
				forLoop.InitStatement = new CodeExpressionStatement(new CodeSnippetExpression());
			}
			
			if (forStatement.Condition == null) {
				forLoop.TestExpression = new CodePrimitiveExpression(true);
			} else {
				forLoop.TestExpression = (CodeExpression)forStatement.Condition.AcceptVisitor(this, data);
			}
			
			codeStack.Push(forLoop.Statements);
			forStatement.EmbeddedStatement.AcceptVisitor(this, data);
			codeStack.Pop();

			if (forStatement.Iterator.Count > 0)
			{
				if (forStatement.Initializers.Count > 1)
				{
					throw new NotSupportedException("CodeDom does not support Multiple For-Loop Iterator Statements");
				}

				foreach (Statement stmt in forStatement.Iterator)
				{
					forLoop.IncrementStatement = (CodeStatement)stmt.AcceptVisitor(this, data);
				}
			}
			else
			{
				// RG: need to handle empty IncrementStatement
				forLoop.IncrementStatement = new CodeExpressionStatement(new CodeSnippetExpression());
			}

			codeStack.Pop();

			Breakable breakable = breakableStack.Pop();

			if (breakable.IsContinue)
			{
				forLoop.Statements.Add(new CodeSnippetStatement());
				forLoop.Statements.Add(new CodeLabeledStatement("continue" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
			}

			AddStmt(forLoop);

			if (breakable.IsBreak)
			{
				AddStmt(new CodeLabeledStatement("break" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
			}
			
			return forLoop;
		}