本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.BoundForEachStatement类的典型用法代码示例。如果您正苦于以下问题:C# BoundForEachStatement类的具体用法?C# BoundForEachStatement怎么用?C# BoundForEachStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BoundForEachStatement类属于Microsoft.CodeAnalysis.CSharp命名空间,在下文中一共展示了BoundForEachStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitForEachStatement
/// <summary>
/// This is the entry point for foreach-loop lowering. It delegates to
/// RewriteEnumeratorForEachStatement
/// RewriteSingleDimensionalArrayForEachStatement
/// RewriteMultiDimensionalArrayForEachStatement
/// RewriteStringForEachStatement
/// </summary>
/// <remarks>
/// We are diverging from the C# 4 spec (and Dev10) to follow the C# 5 spec.
/// The iteration variable will be declared *inside* each loop iteration,
/// rather than outside the loop.
/// </remarks>
public override BoundNode VisitForEachStatement(BoundForEachStatement node)
{
// No point in performing this lowering if the node won't be emitted.
if (node.HasErrors)
{
return node;
}
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
TypeSymbol nodeExpressionType = collectionExpression.Type;
if (nodeExpressionType.Kind == SymbolKind.ArrayType)
{
ArrayTypeSymbol arrayType = (ArrayTypeSymbol)nodeExpressionType;
if (arrayType.IsSZArray)
{
return RewriteSingleDimensionalArrayForEachStatement(node);
}
else
{
return RewriteMultiDimensionalArrayForEachStatement(node);
}
}
else if (nodeExpressionType.SpecialType == SpecialType.System_String)
{
return RewriteStringForEachStatement(node);
}
else
{
return RewriteEnumeratorForEachStatement(node);
}
}
示例2: VisitForEachStatement
public override BoundNode VisitForEachStatement(BoundForEachStatement node)
{
if (IsInside)
{
_labelsInside.Add(node.BreakLabel);
_labelsInside.Add(node.ContinueLabel);
}
return base.VisitForEachStatement(node);
}
示例3: VisitForEachStatement
public override BoundNode VisitForEachStatement(BoundForEachStatement node)
{
if (IsInside)
{
_variablesDeclared.Add(node.IterationVariable);
}
return base.VisitForEachStatement(node);
}
示例4: InstrumentForEachStatement
public virtual BoundStatement InstrumentForEachStatement(BoundForEachStatement original, BoundStatement rewritten)
{
Debug.Assert(original.Syntax.Kind() == SyntaxKind.ForEachStatement);
return InstrumentStatement(original, rewritten);
}
示例5: InstrumentForEachStatementConditionalGotoStart
public override BoundStatement InstrumentForEachStatementConditionalGotoStart(BoundForEachStatement original, BoundStatement branchBack)
{
var syntax = (CommonForEachStatementSyntax)original.Syntax;
return new BoundSequencePointWithSpan(syntax,
base.InstrumentForEachStatementConditionalGotoStart(original, branchBack),
syntax.InKeyword.Span);
}
示例6: InstrumentForEachStatementIterationVarDeclaration
/// <summary>
/// Add sequence point |here|:
///
/// foreach (|Type var| in expr) { }
/// </summary>
/// <remarks>
/// Hit every iteration.
/// </remarks>
public override BoundStatement InstrumentForEachStatementIterationVarDeclaration(BoundForEachStatement original, BoundStatement iterationVarDecl)
{
TextSpan iterationVarDeclSpan;
switch (original.Syntax.Kind())
{
case SyntaxKind.ForEachStatement:
{
var forEachSyntax = (ForEachStatementSyntax)original.Syntax;
iterationVarDeclSpan = TextSpan.FromBounds(forEachSyntax.Type.SpanStart, forEachSyntax.Identifier.Span.End);
break;
}
case SyntaxKind.ForEachComponentStatement:
{
var forEachSyntax = (ForEachComponentStatementSyntax)original.Syntax;
iterationVarDeclSpan = forEachSyntax.VariableComponent.Span;
break;
}
default:
throw ExceptionUtilities.UnexpectedValue(original.Syntax.Kind());
}
return new BoundSequencePointWithSpan(original.Syntax,
base.InstrumentForEachStatementIterationVarDeclaration(original, iterationVarDecl),
iterationVarDeclSpan);
}
示例7: InstrumentForEachStatementDeconstructionVariablesDeclaration
public override BoundStatement InstrumentForEachStatementDeconstructionVariablesDeclaration(BoundForEachStatement original, BoundStatement iterationVarDecl)
{
var forEachSyntax = (ForEachComponentStatementSyntax)original.Syntax;
return new BoundSequencePointWithSpan(forEachSyntax, base.InstrumentForEachStatementDeconstructionVariablesDeclaration(original, iterationVarDecl), forEachSyntax.VariableComponent.Span);
}
示例8: RewriteEnumeratorForEachStatement
/// <summary>
/// Lower a foreach loop that will enumerate a collection using an enumerator.
///
/// E e = ((C)(x)).GetEnumerator()
/// try {
/// while (e.MoveNext()) {
/// V v = (V)(T)e.Current;
/// // body
/// }
/// }
/// finally {
/// // clean up e
/// }
/// </summary>
private BoundStatement RewriteEnumeratorForEachStatement(BoundForEachStatement node)
{
ForEachStatementSyntax forEachSyntax = (ForEachStatementSyntax)node.Syntax;
ForEachEnumeratorInfo enumeratorInfo = node.EnumeratorInfoOpt;
Debug.Assert(enumeratorInfo != null);
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
BoundExpression rewrittenExpression = (BoundExpression)Visit(collectionExpression);
BoundStatement rewrittenBody = (BoundStatement)Visit(node.Body);
TypeSymbol enumeratorType = enumeratorInfo.GetEnumeratorMethod.ReturnType;
TypeSymbol elementType = enumeratorInfo.ElementType;
// E e
LocalSymbol enumeratorVar = _factory.SynthesizedLocal(enumeratorType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachEnumerator);
// Reference to e.
BoundLocal boundEnumeratorVar = MakeBoundLocal(forEachSyntax, enumeratorVar, enumeratorType);
// ((C)(x)).GetEnumerator() or (x).GetEnumerator();
BoundExpression enumeratorVarInitValue = SynthesizeCall(forEachSyntax, rewrittenExpression, enumeratorInfo.GetEnumeratorMethod, enumeratorInfo.CollectionConversion, enumeratorInfo.CollectionType);
// E e = ((C)(x)).GetEnumerator();
BoundStatement enumeratorVarDecl = MakeLocalDeclaration(forEachSyntax, enumeratorVar, enumeratorVarInitValue);
AddForEachExpressionSequencePoint(forEachSyntax, ref enumeratorVarDecl);
// V v
LocalSymbol iterationVar = node.IterationVariable;
//(V)(T)e.Current
BoundExpression iterationVarAssignValue = MakeConversion(
syntax: forEachSyntax,
rewrittenOperand: MakeConversion(
syntax: forEachSyntax,
rewrittenOperand: BoundCall.Synthesized(
syntax: forEachSyntax,
receiverOpt: boundEnumeratorVar,
method: enumeratorInfo.CurrentPropertyGetter),
conversion: enumeratorInfo.CurrentConversion,
rewrittenType: elementType,
@checked: node.Checked),
conversion: node.ElementConversion,
rewrittenType: iterationVar.Type,
@checked: node.Checked);
// V v = (V)(T)e.Current;
BoundStatement iterationVarDecl = MakeLocalDeclaration(forEachSyntax, iterationVar, iterationVarAssignValue);
AddForEachIterationVariableSequencePoint(forEachSyntax, ref iterationVarDecl);
// while (e.MoveNext()) {
// V v = (V)(T)e.Current;
// /* node.Body */
// }
var rewrittenBodyBlock = CreateBlockDeclaringIterationVariable(iterationVar, iterationVarDecl, rewrittenBody, forEachSyntax);
BoundStatement whileLoop = RewriteWhileStatement(
syntax: forEachSyntax,
rewrittenCondition: BoundCall.Synthesized(
syntax: forEachSyntax,
receiverOpt: boundEnumeratorVar,
method: enumeratorInfo.MoveNextMethod),
conditionSequencePointSpan: forEachSyntax.InKeyword.Span,
rewrittenBody: rewrittenBodyBlock,
breakLabel: node.BreakLabel,
continueLabel: node.ContinueLabel,
hasErrors: false);
BoundStatement result;
MethodSymbol disposeMethod;
if (enumeratorInfo.NeedsDisposeMethod && Binder.TryGetSpecialTypeMember(_compilation, SpecialMember.System_IDisposable__Dispose, forEachSyntax, _diagnostics, out disposeMethod))
{
Binder.ReportDiagnosticsIfObsolete(_diagnostics, disposeMethod, forEachSyntax,
hasBaseReceiver: false,
containingMember: _factory.CurrentMethod,
containingType: _factory.CurrentType,
location: enumeratorInfo.Location);
BoundBlock finallyBlockOpt;
var idisposableTypeSymbol = disposeMethod.ContainingType;
var conversions = new TypeConversions(_factory.CurrentMethod.ContainingAssembly.CorLibrary);
//.........这里部分代码省略.........
示例9: RewriteSingleDimensionalArrayForEachStatement
/// <summary>
/// Lower a foreach loop that will enumerate a single-dimensional array.
///
/// A[] a = x;
/// for (int p = 0; p < a.Length; p = p + 1) {
/// V v = (V)a[p];
/// // body
/// }
/// </summary>
/// <remarks>
/// We will follow Dev10 in diverging from the C# 4 spec by ignoring Array's
/// implementation of IEnumerable and just indexing into its elements.
///
/// NOTE: We're assuming that sequence points have already been generated.
/// Otherwise, lowering to for-loops would generated spurious ones.
/// </remarks>
private BoundStatement RewriteSingleDimensionalArrayForEachStatement(BoundForEachStatement node)
{
ForEachStatementSyntax forEachSyntax = (ForEachStatementSyntax)node.Syntax;
BoundExpression collectionExpression = GetUnconvertedCollectionExpression(node);
Debug.Assert(collectionExpression.Type.IsArray());
ArrayTypeSymbol arrayType = (ArrayTypeSymbol)collectionExpression.Type;
Debug.Assert(arrayType.IsSZArray);
TypeSymbol intType = _compilation.GetSpecialType(SpecialType.System_Int32);
TypeSymbol boolType = _compilation.GetSpecialType(SpecialType.System_Boolean);
BoundExpression rewrittenExpression = (BoundExpression)Visit(collectionExpression);
BoundStatement rewrittenBody = (BoundStatement)Visit(node.Body);
// A[] a
LocalSymbol arrayVar = _factory.SynthesizedLocal(arrayType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArray);
// A[] a = /*node.Expression*/;
BoundStatement arrayVarDecl = MakeLocalDeclaration(forEachSyntax, arrayVar, rewrittenExpression);
AddForEachExpressionSequencePoint(forEachSyntax, ref arrayVarDecl);
// Reference to a.
BoundLocal boundArrayVar = MakeBoundLocal(forEachSyntax, arrayVar, arrayType);
// int p
LocalSymbol positionVar = _factory.SynthesizedLocal(intType, syntax: forEachSyntax, kind: SynthesizedLocalKind.ForEachArrayIndex);
// Reference to p.
BoundLocal boundPositionVar = MakeBoundLocal(forEachSyntax, positionVar, intType);
// int p = 0;
BoundStatement positionVarDecl = MakeLocalDeclaration(forEachSyntax, positionVar,
MakeLiteral(forEachSyntax, ConstantValue.Default(SpecialType.System_Int32), intType));
// V v
LocalSymbol iterationVar = node.IterationVariable;
TypeSymbol iterationVarType = iterationVar.Type;
// (V)a[p]
BoundExpression iterationVarInitValue = MakeConversion(
syntax: forEachSyntax,
rewrittenOperand: new BoundArrayAccess(
syntax: forEachSyntax,
expression: boundArrayVar,
indices: ImmutableArray.Create<BoundExpression>(boundPositionVar),
type: arrayType.ElementType),
conversion: node.ElementConversion,
rewrittenType: iterationVarType,
@checked: node.Checked);
// V v = (V)a[p];
BoundStatement iterationVariableDecl = MakeLocalDeclaration(forEachSyntax, iterationVar, iterationVarInitValue);
AddForEachIterationVariableSequencePoint(forEachSyntax, ref iterationVariableDecl);
BoundStatement initializer = new BoundStatementList(forEachSyntax,
statements: ImmutableArray.Create<BoundStatement>(arrayVarDecl, positionVarDecl));
// a.Length
BoundExpression arrayLength = new BoundArrayLength(
syntax: forEachSyntax,
expression: boundArrayVar,
type: intType);
// p < a.Length
BoundExpression exitCondition = new BoundBinaryOperator(
syntax: forEachSyntax,
operatorKind: BinaryOperatorKind.IntLessThan,
left: boundPositionVar,
right: arrayLength,
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: boolType);
// p = p + 1;
BoundStatement positionIncrement = MakePositionIncrement(forEachSyntax, boundPositionVar, intType);
// { V v = (V)a[p]; /* node.Body */ }
//.........这里部分代码省略.........
示例10: InstrumentForEachStatement
private void InstrumentForEachStatement(BoundForEachStatement original, ref BoundStatement result)
{
if (this.Instrument)
{
result = _instrumenter.InstrumentForEachStatement(original, result);
}
}
示例11: InstrumentForEachStatementIterationVarDeclaration
private void InstrumentForEachStatementIterationVarDeclaration(BoundForEachStatement original, ref BoundStatement iterationVarDecl)
{
if (this.Instrument)
{
CommonForEachStatementSyntax forEachSyntax = (CommonForEachStatementSyntax)original.Syntax;
if (forEachSyntax is ForEachComponentStatementSyntax)
{
iterationVarDecl = _instrumenter.InstrumentForEachStatementDeconstructionVariablesDeclaration(original, iterationVarDecl);
}
else
{
iterationVarDecl = _instrumenter.InstrumentForEachStatementIterationVarDeclaration(original, iterationVarDecl);
}
}
}
示例12: InstrumentForEachStatementCollectionVarDeclaration
private void InstrumentForEachStatementCollectionVarDeclaration(BoundForEachStatement original, ref BoundStatement collectionVarDecl)
{
if (this.Instrument)
{
collectionVarDecl = _instrumenter.InstrumentForEachStatementCollectionVarDeclaration(original, collectionVarDecl);
}
}
示例13: LocalOrDeconstructionDeclaration
/// <summary>
/// Takes the expression for the current value of the iteration variable and either
/// (1) assigns it into a local, or
/// (2) deconstructs it into multiple locals (if there is a deconstruct step).
///
/// Produces `V v = /* expression */` or `(D1 d1, ...) = /* expression */`.
/// </summary>
private BoundStatement LocalOrDeconstructionDeclaration(
BoundForEachStatement forEachBound,
LocalSymbol iterationVar,
BoundExpression iterationVarValue,
out ImmutableArray<LocalSymbol> iterationVariables)
{
var forEachSyntax = (CommonForEachStatementSyntax)forEachBound.Syntax;
BoundStatement iterationVarDecl;
BoundForEachDeconstructStep deconstruction = forEachBound.DeconstructionOpt;
if (deconstruction == null)
{
// V v = /* expression */
iterationVarDecl = MakeLocalDeclaration(forEachSyntax, iterationVar, iterationVarValue);
iterationVariables = ImmutableArray.Create(iterationVar);
}
else
{
// (D1 d1, ...) = /* expression */
var assignment = deconstruction.DeconstructionAssignment;
AddPlaceholderReplacement(deconstruction.TargetPlaceholder, iterationVarValue);
BoundExpression loweredAssignment = VisitExpression(assignment);
iterationVarDecl = new BoundExpressionStatement(assignment.Syntax, loweredAssignment);
RemovePlaceholderReplacement(deconstruction.TargetPlaceholder);
iterationVariables = assignment.LeftVariables.SelectAsArray(v => ((BoundLocal)v).LocalSymbol);
}
return iterationVarDecl;
}
示例14: InstrumentForEachStatementGotoContinue
public virtual BoundStatement InstrumentForEachStatementGotoContinue(BoundForEachStatement original, BoundStatement gotoContinue)
{
Debug.Assert(!original.WasCompilerGenerated);
Debug.Assert(original.Syntax.Kind() == SyntaxKind.ForEachStatement);
return gotoContinue;
}
示例15: InstrumentForEachStatementConditionalGotoStart
public virtual BoundStatement InstrumentForEachStatementConditionalGotoStart(BoundForEachStatement original, BoundStatement branchBack)
{
Debug.Assert(!original.WasCompilerGenerated);
Debug.Assert(original.Syntax.Kind() == SyntaxKind.ForEachStatement);
return branchBack;
}