本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.BoundTryStatement类的典型用法代码示例。如果您正苦于以下问题:C# BoundTryStatement类的具体用法?C# BoundTryStatement怎么用?C# BoundTryStatement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BoundTryStatement类属于Microsoft.CodeAnalysis.CSharp命名空间,在下文中一共展示了BoundTryStatement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
BoundBlock tryBlock = (BoundBlock)this.Visit(node.TryBlock);
var origSawAwait = _sawAwait;
_sawAwait = false;
var optimizing = _compilation.Options.OptimizationLevel == OptimizationLevel.Release;
ImmutableArray<BoundCatchBlock> catchBlocks =
// When optimizing and we have a try block without side-effects, we can discard the catch blocks.
(optimizing && !HasSideEffects(tryBlock)) ? ImmutableArray<BoundCatchBlock>.Empty
: this.VisitList(node.CatchBlocks);
BoundBlock finallyBlockOpt = (BoundBlock)this.Visit(node.FinallyBlockOpt);
_sawAwaitInExceptionHandler |= _sawAwait;
_sawAwait |= origSawAwait;
if (optimizing && !HasSideEffects(finallyBlockOpt))
{
finallyBlockOpt = null;
}
return (catchBlocks.IsDefaultOrEmpty && finallyBlockOpt == null)
? (BoundNode)tryBlock
: (BoundNode)node.Update(tryBlock, catchBlocks, finallyBlockOpt, node.PreferFaultHandler);
}
示例2: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
BoundBlock tryBlock = (BoundBlock)this.Visit(node.TryBlock);
this.ExceptionHandleNesting++;
ImmutableArray<BoundCatchBlock> catchBlocks = (ImmutableArray<BoundCatchBlock>)this.VisitList(node.CatchBlocks);
BoundBlock finallyBlockOpt = (BoundBlock)this.Visit(node.FinallyBlockOpt);
this.ExceptionHandleNesting--;
return node.Update(tryBlock, catchBlocks, finallyBlockOpt, node.PreferFaultHandler);
}
示例3: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
BoundBlock tryBlock = (BoundBlock)this.Visit(node.TryBlock);
var origSawAwait = _sawAwait;
_sawAwait = false;
ImmutableArray<BoundCatchBlock> catchBlocks = (ImmutableArray<BoundCatchBlock>)this.VisitList(node.CatchBlocks);
BoundBlock finallyBlockOpt = (BoundBlock)this.Visit(node.FinallyBlockOpt);
_sawAwaitInExceptionHandler |= _sawAwait;
_sawAwait |= origSawAwait;
return node.Update(tryBlock, catchBlocks, finallyBlockOpt, node.PreferFaultHandler);
}
示例4: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
var origSeenYield = _seenYield;
var origLabels = this.currentLabels;
// sibling try blocks do not see each other's yields
_seenYield = false;
this.currentLabels = null;
base.VisitTryStatement(node);
if (_seenYield)
{
// this try yields !
var yieldingTryLabels = _labelsInYieldingTrys;
if (yieldingTryLabels == null)
{
_labelsInYieldingTrys = yieldingTryLabels = new Dictionary<BoundTryStatement, HashSet<LabelSymbol>>();
}
yieldingTryLabels.Add(node, currentLabels);
currentLabels = origLabels;
}
else
{
// this is a boring non-yielding try
// currentLabels = currentLabels U origLabels ;
if (currentLabels == null)
{
currentLabels = origLabels;
}
else if (origLabels != null)
{
currentLabels.UnionWith(origLabels);
}
}
_seenYield = _seenYield | origSeenYield;
return null;
}
开发者ID:noahstein,项目名称:roslyn,代码行数:42,代码来源:IteratorMethodToStateMachineRewriter.YieldsInTryAnalysis.cs
示例5: ContainsYields
private bool ContainsYields(BoundTryStatement statement)
{
return _yieldsInTryAnalysis.ContainsYields(statement);
}
示例6: PushFrame
private IteratorFinallyFrame PushFrame(BoundTryStatement statement)
{
var state = _nextFinalizeState--;
var finallyMethod = MakeSynthesizedFinally(state);
var newFrame = new IteratorFinallyFrame(_currentFinallyFrame, state, finallyMethod, _yieldsInTryAnalysis.Labels(statement));
newFrame.AddState(state);
_currentFinallyFrame = newFrame;
return newFrame;
}
示例7: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
// if node contains no yields, do regular rewrite in the current frame.
if (!ContainsYields(node))
{
_tryNestingLevel++;
var result = node.Update(
(BoundBlock)Visit(node.TryBlock),
VisitList(node.CatchBlocks),
(BoundBlock)Visit(node.FinallyBlockOpt),
node.PreferFaultHandler);
_tryNestingLevel--;
return result;
}
Debug.Assert(node.CatchBlocks.IsEmpty, "try with yields must have no catches");
Debug.Assert(node.FinallyBlockOpt != null, "try with yields must have finally");
// rewrite TryBlock in a new frame.
var frame = PushFrame(node);
_tryNestingLevel++;
var rewrittenBody = (BoundStatement)this.Visit(node.TryBlock);
Debug.Assert(!frame.IsRoot());
Debug.Assert(frame.parent.knownStates.ContainsValue(frame), "parent must be aware about states in the child frame");
var finallyMethod = frame.handler;
var origMethod = F.CurrentMethod;
// rewrite finally block into a Finally method.
F.CurrentMethod = finallyMethod;
var rewrittenHandler = (BoundStatement)this.Visit(node.FinallyBlockOpt);
_tryNestingLevel--;
PopFrame();
// {
// this.state = parentFinalizeState;
// body;
// return;
// }
Debug.Assert(frame.parent.finalizeState == _currentFinallyFrame.finalizeState);
rewrittenHandler = F.Block(
F.Assignment(F.Field(F.This(), stateField), F.Literal(frame.parent.finalizeState)),
rewrittenHandler,
F.Return()
);
F.CloseMethod(rewrittenHandler);
F.CurrentMethod = origMethod;
var bodyStatements = ArrayBuilder<BoundStatement>.GetInstance();
// add a call to the handler after the try body.
//
// {
// this.state = finalizeState;
// body;
// this.Finally(); // will reset the state to the finally state of the parent.
// }
bodyStatements.Add(F.Assignment(F.Field(F.This(), stateField), F.Literal(frame.finalizeState)));
bodyStatements.Add(rewrittenBody);
bodyStatements.Add(F.ExpressionStatement(F.Call(F.This(), finallyMethod)));
// handle proxy labels if have any
if (frame.proxyLabels != null)
{
var dropThrough = F.GenerateLabel("dropThrough");
bodyStatements.Add(F.Goto(dropThrough));
var parent = frame.parent;
foreach (var p in frame.proxyLabels)
{
var proxy = p.Value;
var destination = p.Key;
// branch lands here
bodyStatements.Add(F.Label(proxy));
// finalize current state, proceed to destination.
bodyStatements.Add(F.ExpressionStatement(F.Call(F.This(), finallyMethod)));
// let the parent forward the branch appropriately
var parentProxy = parent.ProxyLabelIfNeeded(destination);
bodyStatements.Add(F.Goto(parentProxy));
}
bodyStatements.Add(F.Label(dropThrough));
}
return F.Block(bodyStatements.ToImmutableAndFree());
}
示例8: PushFrame
private AwaitFinallyFrame PushFrame(BoundTryStatement statement)
{
var newFrame = new AwaitFinallyFrame(currentAwaitFinallyFrame, analysis.Labels(statement));
currentAwaitFinallyFrame = newFrame;
return newFrame;
}
示例9: ContainsYields
/// <summary>
/// Returns true if given try or any of its nested try blocks contain yields
/// </summary>
public bool ContainsYields(BoundTryStatement statement)
{
return _labelsInYieldingTrys != null && _labelsInYieldingTrys.ContainsKey(statement);
}
示例10: FinallyContainsAwaits
/// <summary>
/// Returns true if a finally of the given try contains awaits
/// </summary>
public bool FinallyContainsAwaits(BoundTryStatement statement)
{
return _labelsInInterestingTry != null && _labelsInInterestingTry.ContainsKey(statement);
}
示例11: PushFrame
private AwaitFinallyFrame PushFrame(BoundTryStatement statement)
{
var newFrame = new AwaitFinallyFrame(_currentAwaitFinallyFrame, _analysis.Labels(statement), (TryStatementSyntax)statement.Syntax);
_currentAwaitFinallyFrame = newFrame;
return newFrame;
}
示例12: RewriteFinalizedRegion
/// <summary>
/// Rewrites Try/Catch part of the Try/Catch/Finally
/// </summary>
private BoundStatement RewriteFinalizedRegion(BoundTryStatement node)
{
var rewrittenTry = (BoundBlock)this.VisitBlock(node.TryBlock);
var catches = node.CatchBlocks;
if (catches.IsDefaultOrEmpty)
{
return rewrittenTry;
}
var origAwaitCatchFrame = _currentAwaitCatchFrame;
_currentAwaitCatchFrame = null;
var rewrittenCatches = this.VisitList(node.CatchBlocks);
BoundStatement tryWithCatches = _F.Try(rewrittenTry, rewrittenCatches);
var currentAwaitCatchFrame = _currentAwaitCatchFrame;
if (currentAwaitCatchFrame != null)
{
var handledLabel = _F.GenerateLabel("handled");
var handlersList = currentAwaitCatchFrame.handlers;
var handlers = ArrayBuilder<BoundSwitchSection>.GetInstance(handlersList.Count);
for (int i = 0, l = handlersList.Count; i < l; i++)
{
handlers.Add(_F.SwitchSection(
i + 1,
_F.Block(
handlersList[i],
_F.Goto(handledLabel))));
}
tryWithCatches = _F.Block(
ImmutableArray.Create<LocalSymbol>(
currentAwaitCatchFrame.pendingCaughtException,
currentAwaitCatchFrame.pendingCatch).
AddRange(currentAwaitCatchFrame.GetHoistedLocals()),
ImmutableArray<LocalFunctionSymbol>.Empty,
_F.HiddenSequencePoint(),
_F.Assignment(
_F.Local(currentAwaitCatchFrame.pendingCatch),
_F.Default(currentAwaitCatchFrame.pendingCatch.Type)),
tryWithCatches,
_F.HiddenSequencePoint(),
_F.Switch(
_F.Local(currentAwaitCatchFrame.pendingCatch),
handlers.ToImmutableAndFree()),
_F.HiddenSequencePoint(),
_F.Label(handledLabel));
}
_currentAwaitCatchFrame = origAwaitCatchFrame;
return tryWithCatches;
}
示例13: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
var tryStatementSyntax = node.Syntax;
// If you add a syntax kind to the assertion below, please also ensure
// that the scenario has been tested with Edit-and-Continue.
Debug.Assert(
tryStatementSyntax.IsKind(SyntaxKind.TryStatement) ||
tryStatementSyntax.IsKind(SyntaxKind.UsingStatement) ||
tryStatementSyntax.IsKind(SyntaxKind.ForEachStatement));
BoundStatement finalizedRegion;
BoundBlock rewrittenFinally;
var finallyContainsAwaits = _analysis.FinallyContainsAwaits(node);
if (!finallyContainsAwaits)
{
finalizedRegion = RewriteFinalizedRegion(node);
rewrittenFinally = (BoundBlock)this.Visit(node.FinallyBlockOpt);
if (rewrittenFinally == null)
{
return finalizedRegion;
}
var asTry = finalizedRegion as BoundTryStatement;
if (asTry != null)
{
// since finalized region is a try we can just attach finally to it
Debug.Assert(asTry.FinallyBlockOpt == null);
return asTry.Update(asTry.TryBlock, asTry.CatchBlocks, rewrittenFinally, asTry.PreferFaultHandler);
}
else
{
// wrap finalizedRegion into a Try with a finally.
return _F.Try((BoundBlock)finalizedRegion, ImmutableArray<BoundCatchBlock>.Empty, rewrittenFinally);
}
}
// rewrite finalized region (try and catches) in the current frame
var frame = PushFrame(node);
finalizedRegion = RewriteFinalizedRegion(node);
rewrittenFinally = (BoundBlock)this.VisitBlock(node.FinallyBlockOpt);
PopFrame();
var exceptionType = _F.SpecialType(SpecialType.System_Object);
var pendingExceptionLocal = new SynthesizedLocal(_F.CurrentMethod, exceptionType, SynthesizedLocalKind.TryAwaitPendingException, tryStatementSyntax);
var finallyLabel = _F.GenerateLabel("finallyLabel");
var pendingBranchVar = new SynthesizedLocal(_F.CurrentMethod, _F.SpecialType(SpecialType.System_Int32), SynthesizedLocalKind.TryAwaitPendingBranch, tryStatementSyntax);
var catchAll = _F.Catch(_F.Local(pendingExceptionLocal), _F.Block());
var catchAndPendException = _F.Try(
_F.Block(
finalizedRegion,
_F.HiddenSequencePoint(),
_F.Goto(finallyLabel),
PendBranches(frame, pendingBranchVar, finallyLabel)),
ImmutableArray.Create(catchAll));
var syntheticFinally = _F.Block(
_F.HiddenSequencePoint(),
_F.Label(finallyLabel),
rewrittenFinally,
_F.HiddenSequencePoint(),
UnpendException(pendingExceptionLocal),
UnpendBranches(
frame,
pendingBranchVar,
pendingExceptionLocal));
var locals = ArrayBuilder<LocalSymbol>.GetInstance();
var statements = ArrayBuilder<BoundStatement>.GetInstance();
statements.Add(_F.HiddenSequencePoint());
locals.Add(pendingExceptionLocal);
statements.Add(_F.Assignment(_F.Local(pendingExceptionLocal), _F.Default(pendingExceptionLocal.Type)));
locals.Add(pendingBranchVar);
statements.Add(_F.Assignment(_F.Local(pendingBranchVar), _F.Default(pendingBranchVar.Type)));
LocalSymbol returnLocal = frame.returnValue;
if (returnLocal != null)
{
locals.Add(returnLocal);
}
statements.Add(catchAndPendException);
statements.Add(syntheticFinally);
var completeTry = _F.Block(
locals.ToImmutableAndFree(),
ImmutableArray<LocalFunctionSymbol>.Empty,
statements.ToImmutableAndFree());
return completeTry;
}
示例14: VisitTryStatement
public override BoundNode VisitTryStatement(BoundTryStatement node)
{
this.Visit(node.TryBlock);
this.VisitList(node.CatchBlocks);
this.Visit(node.FinallyBlockOpt);
return null;
}
示例15: RewriteEnumeratorForEachStatement
//.........这里部分代码省略.........
localFunctions: ImmutableArray<LocalFunctionSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(disposeStmt));
}
else
{
Debug.Assert(!enumeratorType.IsSealed);
// IDisposable d
LocalSymbol disposableVar = _factory.SynthesizedLocal(idisposableTypeSymbol);
// Reference to d.
BoundLocal boundDisposableVar = MakeBoundLocal(forEachSyntax, disposableVar, idisposableTypeSymbol);
BoundTypeExpression boundIDisposableTypeExpr = new BoundTypeExpression(forEachSyntax,
aliasOpt: null,
type: idisposableTypeSymbol);
// e as IDisposable
BoundExpression disposableVarInitValue = new BoundAsOperator(forEachSyntax,
operand: boundEnumeratorVar,
targetType: boundIDisposableTypeExpr,
conversion: Conversion.ExplicitReference, // Explicit so the emitter won't optimize it away.
type: idisposableTypeSymbol);
// IDisposable d = e as IDisposable;
BoundStatement disposableVarDecl = MakeLocalDeclaration(forEachSyntax, disposableVar, disposableVarInitValue);
// if (d != null) d.Dispose();
BoundStatement ifStmt = RewriteIfStatement(
syntax: forEachSyntax,
rewrittenCondition: new BoundBinaryOperator(forEachSyntax,
operatorKind: BinaryOperatorKind.NotEqual, // reference equality
left: boundDisposableVar,
right: MakeLiteral(forEachSyntax,
constantValue: ConstantValue.Null,
type: null),
constantValueOpt: null,
methodOpt: null,
resultKind: LookupResultKind.Viable,
type: _compilation.GetSpecialType(SpecialType.System_Boolean)),
rewrittenConsequence: new BoundExpressionStatement(forEachSyntax,
expression: BoundCall.Synthesized(
syntax: forEachSyntax,
receiverOpt: boundDisposableVar,
method: disposeMethod)),
rewrittenAlternativeOpt: null,
hasErrors: false);
// IDisposable d = e as IDisposable;
// if (d != null) d.Dispose();
finallyBlockOpt = new BoundBlock(forEachSyntax,
locals: ImmutableArray.Create<LocalSymbol>(disposableVar),
localFunctions: ImmutableArray<LocalFunctionSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(disposableVarDecl, ifStmt));
}
// try {
// while (e.MoveNext()) {
// V v = (V)(T)e.Current;
// /* loop body */
// }
// }
// finally {
// /* dispose of e */
// }
BoundStatement tryFinally = new BoundTryStatement(forEachSyntax,
tryBlock: new BoundBlock(forEachSyntax,
locals: ImmutableArray<LocalSymbol>.Empty,
localFunctions: ImmutableArray<LocalFunctionSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(whileLoop)),
catchBlocks: ImmutableArray<BoundCatchBlock>.Empty,
finallyBlockOpt: finallyBlockOpt);
// E e = ((C)(x)).GetEnumerator();
// try {
// /* as above */
result = new BoundBlock(
syntax: forEachSyntax,
locals: ImmutableArray.Create(enumeratorVar),
localFunctions: ImmutableArray<LocalFunctionSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(enumeratorVarDecl, tryFinally));
}
else
{
// E e = ((C)(x)).GetEnumerator();
// while (e.MoveNext()) {
// V v = (V)(T)e.Current;
// /* loop body */
// }
result = new BoundBlock(
syntax: forEachSyntax,
locals: ImmutableArray.Create(enumeratorVar),
localFunctions: ImmutableArray<LocalFunctionSymbol>.Empty,
statements: ImmutableArray.Create<BoundStatement>(enumeratorVarDecl, whileLoop));
}
AddForEachKeywordSequencePoint(forEachSyntax, ref result);
return result;
}