本文整理汇总了C#中CodeGen.BeginExceptionBlock方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGen.BeginExceptionBlock方法的具体用法?C# CodeGen.BeginExceptionBlock怎么用?C# CodeGen.BeginExceptionBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGen
的用法示例。
在下文中一共展示了CodeGen.BeginExceptionBlock方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Emit
// ***WITH STATEMENT CODE GENERATION ALGORITHM***
//
//GRAMMAR :=
//with EXPR as VAR:
// BLOCK
//
//CODE GEN :=
//
//mgr = (EXPR)
//exit = mgr.__exit__ # Not calling it yet
//value = mgr.__enter__()
//exc = True
//isTryYielded = False
//try:
//
// VAR = value # Only if "as VAR" is present
// BLOCK
// // if yield happens in the Block,
// // then isTryYielded is set to True by Yield's Code Gen
//except:
// # The exceptional case is handled here
// exc = False
// if not exit(*sys.exc_info()):
// raise
// # The exception is consumed if exit() returns true
//finally:
// # The normal and non-local-goto cases are handled here
// if isTryYielded = False && exc == True :
// exit(None, None, None)
internal override void Emit(CodeGen cg)
{
Slot exc = null;
Slot isTryYielded = null;
Slot exit = null;
if (cg.IsGenerator()) {
exc = cg.Names.GetTempSlot("with", typeof(object));
isTryYielded = cg.Names.GetTempSlot("with", typeof(object));
exit = cg.Names.GetTempSlot("with", typeof(object));
} else {
exc = cg.GetLocalTmp(typeof(object));
isTryYielded = cg.GetLocalTmp(typeof(object));
exit = cg.GetLocalTmp(typeof(object));
}
// mgr = (EXPR)
Slot mgr = cg.GetLocalTmp(typeof(object));
contextManager.Emit(cg);
mgr.EmitSet(cg);
// exit = mgr.__exit__ # not calling it yet
cg.EmitCallerContext();
mgr.EmitGet(cg);
cg.EmitSymbolId("__exit__");
cg.EmitCall(typeof(Ops), "GetAttr");
exit.EmitSet(cg);
mgr.EmitGet(cg);
cg.FreeLocalTmp(mgr);
cg.EmitSymbolId("__enter__");
cg.EmitObjectArray(new Expression[0]);
cg.EmitCall(typeof(Ops), "Invoke", new Type[] { typeof(object), typeof(SymbolId), typeof(object[]) });
Slot value = cg.GetLocalTmp(typeof(object));
value.EmitSet(cg);
// exc = True
cg.EmitConstantBoxed(true);
exc.EmitSet(cg);
Slot choiceVar = null;
if (yieldTargets != null && yieldTargets.Count > 0) {
Label startOfBlock = cg.DefineLabel();
choiceVar = cg.GetLocalTmp(typeof(int));
cg.EmitInt(-1);
choiceVar.EmitSet(cg);
cg.Emit(OpCodes.Br, startOfBlock);
int index = 0;
foreach (YieldTarget yt in yieldTargets) {
cg.MarkLabel(yt.TopBranchTarget);
cg.EmitInt(index++);
choiceVar.EmitSet(cg);
cg.Emit(OpCodes.Br, startOfBlock);
}
cg.MarkLabel(startOfBlock);
}
cg.EmitConstantBoxed(false);
isTryYielded.EmitSet(cg);
Label beforeFinally = cg.DefineLabel();
cg.PushWithTryBlock(isTryYielded);
cg.BeginExceptionBlock();
if (yieldTargets != null && yieldTargets.Count > 0) {
int index = 0;
foreach (YieldTarget yt in yieldTargets) {
//.........这里部分代码省略.........
示例2: EmitSimpleTry
private void EmitSimpleTry(CodeGen cg, TryFlowResult flow)
{
//
// Initialize the flow control flag
//
Slot flowControlFlag = null;
if (flow.Any) {
Debug.Assert(_finally != null);
flowControlFlag = cg.GetLocalTmp(typeof(int));
cg.EmitInt(CodeGen.FinallyExitsNormally);
flowControlFlag.EmitSet(cg);
// If there is a control flow in finally, emit outer:
// try {
// // try block body and all catch handling
// } catch (Exception all) {
// saved = all;
// } finally {
// finally_body
// if (saved != null) {
// throw saved;
// }
// }
if (HaveHandlers()) {
cg.PushExceptionBlock(TargetBlockType.Try, flowControlFlag);
cg.BeginExceptionBlock();
}
}
//******************************************************************
// 1. ENTERING TRY
//******************************************************************
cg.PushExceptionBlock(TargetBlockType.Try, flowControlFlag);
cg.BeginExceptionBlock();
//******************************************************************
// 2. Emit the try statement body
//******************************************************************
_body.Emit(cg);
//cg.EmitSequencePointNone();
//******************************************************************
// 3. Emit the catch blocks
//******************************************************************
if (HaveHandlers()) {
cg.PushExceptionBlock(TargetBlockType.Catch, flowControlFlag);
foreach (CatchBlock cb in _handlers) {
// Begin the strongly typed exception block
cg.BeginCatchBlock(cb.Test);
// Save the exception (if the catch block asked for it) or pop
EmitSaveExceptionOrPop(cg, cb);
//
// Emit the catch block body
//
cb.Body.Emit(cg);
}
cg.PopTargets(TargetBlockType.Catch);
}
//******************************************************************
// 4. Emit the finally block
//******************************************************************
if (_finally != null) {
Slot rethrow = null;
if (flow.Any) {
// If there is a control flow in finally, end the catch
// statement and emit the catch-all and finally clause
// with rethrow at the end.
if (HaveHandlers()) {
cg.EndExceptionBlock();
cg.PopTargets(TargetBlockType.Try);
}
cg.PushExceptionBlock(TargetBlockType.Catch, flowControlFlag);
cg.BeginCatchBlock(typeof(Exception));
rethrow = cg.GetLocalTmp(typeof(Exception));
rethrow.EmitSet(cg);
cg.PopTargets(TargetBlockType.Catch);
}
cg.PushExceptionBlock(TargetBlockType.Finally, flowControlFlag);
cg.BeginFinallyBlock();
//
// Emit the finally block body
//
_finally.Emit(cg);
//.........这里部分代码省略.........
示例3: EmitGenerator
private void EmitGenerator(CodeGen ncg)
{
YieldTarget[] targets = YieldLabelBuilder.BuildYieldTargets(this, ncg);
Label[] jumpTable = new Label[yieldCount];
for (int i = 0; i < yieldCount; i++) jumpTable[i] = targets[i].TopBranchTarget;
ncg.yieldLabels = jumpTable;
// Generator will ofcourse yield, but we are not interested in the isBlockYielded value
// hence push a dummySlot to pass the Assertion.
Slot dummySlot = ncg.GetLocalTmp(typeof(object));
ncg.PushTryBlock(dummySlot);
ncg.BeginExceptionBlock();
ncg.Emit(OpCodes.Ldarg_0);
ncg.EmitFieldGet(typeof(Generator), "location");
ncg.Emit(OpCodes.Switch, jumpTable);
// fall-through on first pass
// yield statements will insert the needed labels after their returns
Body.Emit(ncg);
//free the dummySlot
ncg.FreeLocalTmp(dummySlot);
// fall-through is almost always possible in generators, so this
// is almost always needed
ncg.EmitReturnInGenerator(null);
// special handling for StopIteration thrown in body
ncg.BeginCatchBlock(typeof(StopIterationException));
ncg.EndExceptionBlock();
ncg.EmitReturnInGenerator(null);
ncg.PopTargets();
ncg.Finish();
}
示例4: EmitGeneratorTry
private void EmitGeneratorTry(CodeGen cg, TryFlowResult flow)
{
//
// Initialize the flow control flag
//
Slot flowControlFlag = null;
if (flow.Any) {
flowControlFlag = cg.GetLocalTmp(typeof(int));
cg.EmitInt(CodeGen.FinallyExitsNormally);
flowControlFlag.EmitSet(cg);
}
Slot exception = null;
if (_finally != null) {
exception = cg.GetTemporarySlot(typeof(Exception));
cg.EmitNull();
exception.EmitSet(cg);
}
//******************************************************************
// Entering the try block
//******************************************************************
if (_target != null) {
cg.MarkLabel(_target.EnsureLabel(cg));
}
//******************************************************************
// If we have a 'finally', transform it into try..catch..finally
// and rethrow
//******************************************************************
Label endFinallyBlock = new Label();
if (_finally != null) {
cg.PushExceptionBlock(TargetBlockType.Try, flowControlFlag);
cg.BeginExceptionBlock();
endFinallyBlock = cg.DefineLabel();
//**************************************************************
// If there is a yield in any catch, that catch will be hoisted
// and we need to dispatch to it from here
//**************************************************************
if (_yieldInCatch) {
EmitYieldDispatch(_catchYields, cg);
}
if (YieldInBlock(_finallyYields)) {
foreach (YieldTarget yt in _finallyYields) {
cg.GotoRouter.EmitGet(cg);
cg.EmitInt(yt.Index);
cg.Emit(OpCodes.Beq, endFinallyBlock);
}
}
}
//******************************************************************
// If we have a 'catch', start a try block to handle all the catches
//******************************************************************
Label endCatchBlock = new Label();
if (HaveHandlers()) {
cg.PushExceptionBlock(TargetBlockType.Try, flowControlFlag);
endCatchBlock = cg.BeginExceptionBlock();
}
//******************************************************************
// Emit the try block body
//******************************************************************
// First, emit the dispatch within the try block
EmitYieldDispatch(_tryYields, cg);
// Then, emit the actual body
_body.Emit(cg);
//cg.EmitSequencePointNone();
//******************************************************************
// Emit the catch blocks
//******************************************************************
if (HaveHandlers()) {
List<CatchRecord> catches = new List<CatchRecord>();
cg.PushExceptionBlock(TargetBlockType.Catch, flowControlFlag);
foreach (CatchBlock cb in _handlers) {
cg.BeginCatchBlock(cb.Test);
if (cb.Yield) {
// The catch block body contains yield, therefore
// delay the body emit till after the try block.
Slot slot = cg.GetLocalTmp(cb.Test);
slot.EmitSet(cg);
catches.Add(new CatchRecord(slot, cb));
} else {
// Save the exception (if the catch block asked for it) or pop
EmitSaveExceptionOrPop(cg, cb);
// Emit the body right now, since it doesn't contain yield
cb.Body.Emit(cg);
}
}
//.........这里部分代码省略.........