本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.BoundStatement类的典型用法代码示例。如果您正苦于以下问题:C# BoundStatement类的具体用法?C# BoundStatement怎么用?C# BoundStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BoundStatement类属于Microsoft.CodeAnalysis.CSharp命名空间,在下文中一共展示了BoundStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddSequencePoint
internal static BoundStatement AddSequencePoint(BlockSyntax blockSyntax, BoundStatement rewrittenStatement, bool isPrimaryCtor)
{
TextSpan span;
if (isPrimaryCtor)
{
// For a primary constructor block: ... [|{|] ... }
return new BoundSequencePointWithSpan(blockSyntax, rewrittenStatement, blockSyntax.OpenBraceToken.Span);
}
var parent = blockSyntax.Parent as ConstructorDeclarationSyntax;
if (parent != null)
{
span = CreateSpanForConstructorDeclaration(parent);
}
else
{
// This inserts a sequence points to any prologue code for method declarations.
var start = blockSyntax.Parent.SpanStart;
var end = blockSyntax.OpenBraceToken.GetPreviousToken().Span.End;
span = TextSpan.FromBounds(start, end);
}
return new BoundSequencePointWithSpan(blockSyntax, rewrittenStatement, span);
}
示例2: TryCreate
public static DynamicAnalysisInjector TryCreate(MethodSymbol method, BoundStatement methodBody, SyntheticBoundNodeFactory methodBodyFactory, DiagnosticBag diagnostics, DebugDocumentProvider debugDocumentProvider, Instrumenter previous)
{
// Do not instrument implicitly-declared methods, except for constructors.
// Instrument implicit constructors in order to instrument member initializers.
if (method.IsImplicitlyDeclared && !method.IsImplicitConstructor)
{
return null;
}
// Do not instrument methods marked with or in scope of ExcludeFromCodeCoverageAttribute.
if (IsExcludedFromCodeCoverage(method))
{
return null;
}
MethodSymbol createPayload = GetCreatePayload(methodBodyFactory.Compilation, methodBody.Syntax, diagnostics);
// Do not instrument any methods if CreatePayload is not present.
if ((object)createPayload == null)
{
return null;
}
// Do not instrument CreatePayload if it is part of the current compilation (which occurs only during testing).
// CreatePayload will fail at run time with an infinite recursion if it is instrumented.
if (method.Equals(createPayload))
{
return null;
}
return new DynamicAnalysisInjector(method, methodBody, methodBodyFactory, createPayload, diagnostics, debugDocumentProvider, previous);
}
示例3: StateMachineRewriter
protected StateMachineRewriter(
BoundStatement body,
MethodSymbol method,
SynthesizedContainer stateMachineType,
VariableSlotAllocator slotAllocatorOpt,
TypeCompilationState compilationState,
DiagnosticBag diagnostics)
{
Debug.Assert(body != null);
Debug.Assert(method != null);
Debug.Assert(stateMachineType != null);
Debug.Assert(compilationState != null);
Debug.Assert(diagnostics != null);
this.body = body;
this.method = method;
this.stateMachineType = stateMachineType;
this.slotAllocatorOpt = slotAllocatorOpt;
this.synthesizedLocalOrdinals = new SynthesizedLocalOrdinalsDispenser();
this.diagnostics = diagnostics;
this.F = new SyntheticBoundNodeFactory(method, body.Syntax, compilationState, diagnostics);
Debug.Assert(F.CurrentType == method.ContainingType);
Debug.Assert(F.Syntax == body.Syntax);
}
示例4: Rewrite
/// <summary>
/// Rewrite an async method into a state machine class.
/// </summary>
/// <param name="body">The original body of the method</param>
/// <param name="method">The method's identity</param>
/// <param name="compilationState">The collection of generated methods that result from this transformation and which must be emitted</param>
/// <param name="diagnostics">Diagnostic bag for diagnostics.</param>
/// <param name="generateDebugInfo"></param>
/// <param name="stateMachineType"></param>
internal static BoundStatement Rewrite(
BoundStatement body,
MethodSymbol method,
TypeCompilationState compilationState,
DiagnosticBag diagnostics,
bool generateDebugInfo,
out AsyncStateMachine stateMachineType)
{
if (!method.IsAsync)
{
stateMachineType = null;
return body;
}
// The CLR doesn't support adding fields to structs, so in order to enable EnC in an async method we need to generate a class.
var typeKind = compilationState.Compilation.Options.EnableEditAndContinue ? TypeKind.Class : TypeKind.Struct;
var bodyWithAwaitLifted = AwaitLiftingRewriter.Rewrite(body, method, compilationState, diagnostics);
stateMachineType = new AsyncStateMachine(method, typeKind);
compilationState.ModuleBuilderOpt.CompilationState.SetStateMachineType(method, stateMachineType);
var rewriter = new AsyncRewriter(bodyWithAwaitLifted, method, stateMachineType, compilationState, diagnostics, generateDebugInfo);
if (!rewriter.constructedSuccessfully)
{
return body;
}
return rewriter.Rewrite();
}
示例5: HasSideEffects
/// <summary>
/// Is there any code to execute in the given statement that could have side-effects,
/// such as throwing an exception? This implementation is conservative, in the sense
/// that it may return true when the statement actually may have no side effects.
/// </summary>
private static bool HasSideEffects(BoundStatement statement)
{
if (statement == null) return false;
switch (statement.Kind)
{
case BoundKind.NoOpStatement:
return true;
case BoundKind.Block:
{
var block = (BoundBlock)statement;
foreach (var stmt in block.Statements)
{
if (HasSideEffects(stmt)) return true;
}
return false;
}
case BoundKind.SequencePoint:
{
var sequence = (BoundSequencePoint)statement;
return HasSideEffects(sequence.StatementOpt);
}
case BoundKind.SequencePointWithSpan:
{
var sequence = (BoundSequencePointWithSpan)statement;
return HasSideEffects(sequence.StatementOpt);
}
default:
return true;
}
}
示例6: AppendImplicitReturn
// insert the implicit "return" statement at the end of the method body
// Normally, we wouldn't bother attaching syntax trees to compiler-generated nodes, but these
// ones are going to have sequence points.
internal static BoundBlock AppendImplicitReturn(BoundStatement node, MethodSymbol method = null, CSharpSyntaxNode syntax = null)
{
if (syntax == null)
{
syntax = node.Syntax;
}
BoundStatement ret =
(object)method != null && (object)method.IteratorElementType != null
? BoundYieldBreakStatement.Synthesized(syntax) as BoundStatement
: BoundReturnStatement.Synthesized(syntax, null);
if (syntax.Kind == SyntaxKind.Block)
{
var blockSyntax = (BlockSyntax)syntax;
ret = new BoundSequencePointWithSpan(
blockSyntax,
ret,
blockSyntax.CloseBraceToken.Span)
{ WasCompilerGenerated = true };
}
switch (node.Kind)
{
case BoundKind.Block:
var block = (BoundBlock)node;
return block.Update(block.LocalsOpt, block.Statements.Add(ret));
default:
return new BoundBlock(syntax, ImmutableArray<LocalSymbol>.Empty, ImmutableArray.Create(ret, node));
}
}
示例7: Rewrite
/// <summary>
/// Rewrite an iterator method into a state machine class.
/// </summary>
/// <param name="body">The original body of the method</param>
/// <param name="method">The method's identity</param>
/// <param name="compilationState">The collection of generated methods that result from this transformation and which must be emitted</param>
/// <param name="diagnostics">Diagnostic bag for diagnostics.</param>
/// <param name="generateDebugInfo"></param>
internal static BoundStatement Rewrite(
BoundStatement body,
MethodSymbol method,
TypeCompilationState compilationState,
DiagnosticBag diagnostics,
bool generateDebugInfo)
{
TypeSymbol elementType = method.IteratorElementType;
if ((object)elementType == null)
{
return body;
}
// Figure out what kind of iterator we are generating.
bool isEnumerable;
switch (method.ReturnType.OriginalDefinition.SpecialType)
{
case SpecialType.System_Collections_IEnumerable:
case SpecialType.System_Collections_Generic_IEnumerable_T:
isEnumerable = true;
break;
case SpecialType.System_Collections_IEnumerator:
case SpecialType.System_Collections_Generic_IEnumerator_T:
isEnumerable = false;
break;
default:
throw ExceptionUtilities.UnexpectedValue(method.ReturnType.OriginalDefinition.SpecialType);
}
var iteratorClass = new IteratorStateMachine(method, isEnumerable, elementType, compilationState);
return new IteratorRewriter(body, method, isEnumerable, iteratorClass, compilationState, diagnostics, generateDebugInfo).Rewrite();
}
示例8: Rewrite
/// <summary>
/// Rewrite an async method into a state machine type.
/// </summary>
internal static BoundStatement Rewrite(
BoundStatement body,
MethodSymbol method,
int methodOrdinal,
VariableSlotAllocator slotAllocatorOpt,
TypeCompilationState compilationState,
DiagnosticBag diagnostics,
out AsyncStateMachine stateMachineType)
{
if (!method.IsAsync)
{
stateMachineType = null;
return body;
}
// The CLR doesn't support adding fields to structs, so in order to enable EnC in an async method we need to generate a class.
var typeKind = compilationState.Compilation.Options.EnableEditAndContinue ? TypeKind.Class : TypeKind.Struct;
var bodyWithAwaitLifted = AwaitExpressionSpiller.Rewrite(body, method, compilationState, diagnostics);
stateMachineType = new AsyncStateMachine(slotAllocatorOpt, compilationState, method, methodOrdinal, typeKind);
compilationState.ModuleBuilderOpt.CompilationState.SetStateMachineType(method, stateMachineType);
var rewriter = new AsyncRewriter(bodyWithAwaitLifted, method, methodOrdinal, stateMachineType, slotAllocatorOpt, compilationState, diagnostics);
if (!rewriter.VerifyPresenceOfRequiredAPIs())
{
return body;
}
return rewriter.Rewrite();
}
示例9: AddSequencePoint
internal static BoundStatement AddSequencePoint(UsingStatementSyntax usingSyntax, BoundStatement rewrittenStatement)
{
int start = usingSyntax.Span.Start;
int end = usingSyntax.CloseParenToken.Span.End;
TextSpan span = TextSpan.FromBounds(start, end);
return new BoundSequencePointWithSpan(usingSyntax, rewrittenStatement, span);
}
示例10: RewriteIfStatement
private static BoundStatement RewriteIfStatement(
CSharpSyntaxNode syntax,
ImmutableArray<LocalSymbol> locals,
BoundExpression rewrittenCondition,
BoundStatement rewrittenConsequence,
BoundStatement rewrittenAlternativeOpt,
bool hasErrors)
{
var afterif = new GeneratedLabelSymbol("afterif");
var builder = ArrayBuilder<BoundStatement>.GetInstance();
if (rewrittenAlternativeOpt == null)
{
// if (condition)
// consequence;
//
// becomes
//
// GotoIfFalse condition afterif;
// consequence;
// afterif:
builder.Add(new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, false, afterif));
builder.Add(rewrittenConsequence);
}
else
{
// if (condition)
// consequence;
// else
// alternative
//
// becomes
//
// GotoIfFalse condition alt;
// consequence
// goto afterif;
// alt:
// alternative;
// afterif:
var alt = new GeneratedLabelSymbol("alternative");
builder.Add(new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, false, alt));
builder.Add(rewrittenConsequence);
builder.Add(new BoundGotoStatement(syntax, afterif));
builder.Add(new BoundLabelStatement(syntax, alt));
builder.Add(rewrittenAlternativeOpt);
}
builder.Add(new BoundLabelStatement(syntax, afterif));
if (!locals.IsDefaultOrEmpty)
{
return new BoundBlock(syntax, locals, builder.ToImmutableAndFree(), hasErrors);
}
return new BoundStatementList(syntax, builder.ToImmutableAndFree(), hasErrors);
}
示例11: RewriteWhileStatement
private BoundStatement RewriteWhileStatement(
BoundLoopStatement loop,
BoundExpression rewrittenCondition,
BoundStatement rewrittenBody,
GeneratedLabelSymbol breakLabel,
GeneratedLabelSymbol continueLabel,
bool hasErrors)
{
Debug.Assert(loop.Kind == BoundKind.WhileStatement || loop.Kind == BoundKind.ForEachStatement);
// while (condition)
// body;
//
// becomes
//
// goto continue;
// start:
// {
// body
// continue:
// GotoIfTrue condition start;
// }
// break:
SyntaxNode syntax = loop.Syntax;
var startLabel = new GeneratedLabelSymbol("start");
BoundStatement ifConditionGotoStart = new BoundConditionalGoto(rewrittenCondition.Syntax, rewrittenCondition, true, startLabel);
BoundStatement gotoContinue = new BoundGotoStatement(syntax, continueLabel);
if (this.Instrument && !loop.WasCompilerGenerated)
{
switch (loop.Kind)
{
case BoundKind.WhileStatement:
ifConditionGotoStart = _instrumenter.InstrumentWhileStatementConditionalGotoStartOrBreak((BoundWhileStatement)loop, ifConditionGotoStart);
break;
case BoundKind.ForEachStatement:
ifConditionGotoStart = _instrumenter.InstrumentForEachStatementConditionalGotoStart((BoundForEachStatement)loop, ifConditionGotoStart);
break;
default:
throw ExceptionUtilities.UnexpectedValue(loop.Kind);
}
// mark the initial jump as hidden. We do it to tell that this is not a part of previous statement. This
// jump may be a target of another jump (for example if loops are nested) and that would give the
// impression that the previous statement is being re-executed.
gotoContinue = new BoundSequencePoint(null, gotoContinue);
}
return BoundStatementList.Synthesized(syntax, hasErrors,
gotoContinue,
new BoundLabelStatement(syntax, startLabel),
rewrittenBody,
new BoundLabelStatement(syntax, continueLabel),
ifConditionGotoStart,
new BoundLabelStatement(syntax, breakLabel));
}
示例12: RewriteIfStatement
private static BoundStatement RewriteIfStatement(
CSharpSyntaxNode syntax,
BoundExpression rewrittenCondition,
BoundStatement rewrittenConsequence,
BoundStatement rewrittenAlternativeOpt,
bool hasErrors)
{
return RewriteIfStatement(syntax, ImmutableArray<LocalSymbol>.Empty, rewrittenCondition, rewrittenConsequence, rewrittenAlternativeOpt, hasErrors);
}
示例13: AddSequencePoint
internal static BoundStatement AddSequencePoint(VariableDeclaratorSyntax declaratorSyntax, BoundStatement rewrittenStatement)
{
SyntaxNode node;
TextSpan? part;
GetBreakpointSpan(declaratorSyntax, out node, out part);
var result = BoundSequencePoint.Create(declaratorSyntax, part, rewrittenStatement);
result.WasCompilerGenerated = rewrittenStatement.WasCompilerGenerated;
return result;
}
示例14: MethodWithBody
internal MethodWithBody(MethodSymbol method, BoundStatement body, ImportChain importChainOpt)
{
Debug.Assert(method != null);
Debug.Assert(body != null);
this.Method = method;
this.Body = body;
this.ImportChainOpt = importChainOpt;
}
示例15: VisitFixedStatement
public override BoundNode VisitFixedStatement(BoundFixedStatement node)
{
int numFixedLocals = node.Locals.Length;
var localBuilder = ArrayBuilder<LocalSymbol>.GetInstance(numFixedLocals);
localBuilder.AddRange(node.Locals);
var statementBuilder = ArrayBuilder<BoundStatement>.GetInstance(numFixedLocals + 1 + 1); //+1 for body, +1 for hidden seq point
var cleanup = new BoundStatement[numFixedLocals];
ImmutableArray<BoundLocalDeclaration> localDecls = node.Declarations.LocalDeclarations;
Debug.Assert(localDecls.Length == numFixedLocals);
for (int i = 0; i < numFixedLocals; i++)
{
BoundLocalDeclaration localDecl = localDecls[i];
LocalSymbol temp;
LocalSymbol localToClear;
statementBuilder.Add(InitializeFixedStatementLocal(localDecl, factory, out temp, out localToClear));
if (!ReferenceEquals(temp, null))
{
localBuilder.Add(temp);
}
// NOTE: Dev10 nulls out the locals in declaration order (as opposed to "popping" them in reverse order).
cleanup[i] = factory.Assignment(factory.Local(localToClear), factory.Null(localToClear.Type));
}
BoundStatement rewrittenBody = VisitStatement(node.Body);
statementBuilder.Add(rewrittenBody);
statementBuilder.Add(factory.HiddenSequencePoint());
Debug.Assert(statementBuilder.Count == numFixedLocals + 1 + 1);
// In principle, the cleanup code (i.e. nulling out the pinned variables) is always
// in a finally block. However, we can optimize finally away (keeping the cleanup
// code) in cases where both of the following are true:
// 1) there are no branches out of the fixed statement; and
// 2) the fixed statement is not in a try block (syntactic or synthesized).
if (IsInTryBlock(node) || HasGotoOut(rewrittenBody))
{
return factory.Block(
localBuilder.ToImmutableAndFree(),
new BoundTryStatement(
factory.Syntax,
factory.Block(statementBuilder.ToImmutableAndFree()),
ImmutableArray<BoundCatchBlock>.Empty,
factory.Block(cleanup)));
}
else
{
statementBuilder.AddRange(cleanup);
return factory.Block(localBuilder.ToImmutableAndFree(), statementBuilder.ToImmutableAndFree());
}
}