1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
|
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Cci;
using Microsoft.Cci.MetadataReader;
using Microsoft.Cci.MutableCodeModel;
using Microsoft.Cci.Contracts;
using Microsoft.Cci.ILToCodeModel;
using Bpl = Microsoft.Boogie;
using System.Diagnostics.Contracts;
using TranslationPlugins;
using BytecodeTranslator.Phone;
namespace BytecodeTranslator
{
public class MostNestedTryStatementTraverser : CodeTraverser {
Dictionary<IName, ITryCatchFinallyStatement> mostNestedTryStatement = new Dictionary<IName, ITryCatchFinallyStatement>();
ITryCatchFinallyStatement currStatement = null;
public override void TraverseChildren(ILabeledStatement labeledStatement) {
if (currStatement != null)
mostNestedTryStatement.Add(labeledStatement.Label, currStatement);
base.TraverseChildren(labeledStatement);
}
public override void TraverseChildren(ITryCatchFinallyStatement tryCatchFinallyStatement) {
ITryCatchFinallyStatement savedStatement = currStatement;
currStatement = tryCatchFinallyStatement;
base.TraverseChildren(tryCatchFinallyStatement);
currStatement = savedStatement;
}
public ITryCatchFinallyStatement MostNestedTryStatement(IName label) {
if (!mostNestedTryStatement.ContainsKey(label))
return null;
return mostNestedTryStatement[label];
}
}
public class StatementTraverser : CodeTraverser {
public readonly TraverserFactory factory;
readonly Sink sink;
public readonly PdbReader/*?*/ PdbReader;
public readonly Bpl.StmtListBuilder StmtBuilder = new Bpl.StmtListBuilder();
private bool contractContext;
internal readonly Stack<IExpression> operandStack = new Stack<IExpression>();
private bool captureState;
private static int captureStateCounter = 0;
#region Constructors
public StatementTraverser(Sink sink, PdbReader/*?*/ pdbReader, bool contractContext, TraverserFactory factory) {
this.sink = sink;
this.factory = factory;
PdbReader = pdbReader;
this.contractContext = contractContext;
this.captureState = sink.Options.captureState;
this.PreorderVisitor = new SourceContextEmitter(this);
}
#endregion
#region Helper Methods
Bpl.Expr ExpressionFor(IExpression expression) {
ExpressionTraverser etrav = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
etrav.Traverse(expression);
Contract.Assert(etrav.TranslatedExpressions.Count == 1);
return etrav.TranslatedExpressions.Pop();
}
public ICollection<ITypeDefinition>/*?*/ TranslateMethod(IMethodDefinition method) {
var methodBody = method.Body as ISourceMethodBody;
if (methodBody == null) return null;
var block = methodBody.Block as BlockStatement;
// TODO: Error if cast fails?
ICollection<ITypeDefinition> newTypes = null;
if (block != null) {
var remover = new AnonymousDelegateRemover(this.sink.host, this.PdbReader);
newTypes = remover.RemoveAnonymousDelegates(methodBody.MethodDefinition, block);
}
this.Traverse(methodBody);
return newTypes;
}
#endregion
#region Helper Classes
class SourceContextEmitter : CodeVisitor {
StatementTraverser parent;
public SourceContextEmitter(StatementTraverser parent) {
this.parent = parent;
}
public override void Visit(IStatement statement) {
EmitSourceContext(statement);
if (this.parent.sink.Options.captureState) {
var tok = statement.Token();
var state = String.Format("s{0}", StatementTraverser.captureStateCounter++);
var attrib = new Bpl.QKeyValue(tok, "captureState ", new List<object> { state }, null);
this.parent.StmtBuilder.Add(
new Bpl.AssumeCmd(tok, Bpl.Expr.True, attrib)
);
}
}
private void EmitSourceContext(IStatement statement) {
if (statement is IEmptyStatement) return;
var tok = statement.Token();
string fileName = null;
int lineNumber = 0;
if (this.parent.PdbReader != null) {
var slocs = this.parent.PdbReader.GetClosestPrimarySourceLocationsFor(statement.Locations);
foreach (var sloc in slocs) {
fileName = sloc.Document.Location;
lineNumber = sloc.StartLine;
break;
}
if (fileName != null) {
var attrib = new Bpl.QKeyValue(tok, "sourceLine", new List<object> { Bpl.Expr.Literal((int)lineNumber) }, null);
attrib = new Bpl.QKeyValue(tok, "sourceFile", new List<object> { fileName }, attrib);
this.parent.StmtBuilder.Add(
new Bpl.AssertCmd(tok, Bpl.Expr.True, attrib)
);
}
}
}
}
#endregion
//public override void Visit(ISourceMethodBody methodBody) {
// var block = methodBody.Block as BlockStatement;
// // TODO: Error if cast fails?
// if (block != null) {
// var remover = new AnonymousDelegateRemover(this.sink.host, this.PdbReader);
// var newTypes = remover.RemoveAnonymousDelegates(methodBody.MethodDefinition, block);
// }
// base.Visit(methodBody);
//}
public override void TraverseChildren(IBlockStatement block) {
foreach (var s in block.Statements) {
this.Traverse(s);
}
}
#region Basic Statements
public override void TraverseChildren(IAssertStatement assertStatement) {
Bpl.Expr conditionExpr = ExpressionFor(assertStatement.Condition);
Bpl.Type conditionType = this.sink.CciTypeToBoogie(assertStatement.Condition.Type);
if (conditionType == this.sink.Heap.RefType) {
conditionExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, conditionExpr, Bpl.Expr.Ident(this.sink.Heap.NullRef));
}
else if (conditionType == Bpl.Type.Int) {
conditionExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, conditionExpr, Bpl.Expr.Literal(0));
}
else {
System.Diagnostics.Debug.Assert(conditionType == Bpl.Type.Bool);
}
if (this.sink.Options.getMeHere) {
StmtBuilder.Add(new Bpl.AssumeCmd(assertStatement.Token(), conditionExpr));
} else {
StmtBuilder.Add(new Bpl.AssertCmd(assertStatement.Token(), conditionExpr));
}
}
public override void TraverseChildren(IAssumeStatement assumeStatement) {
Bpl.Expr conditionExpr = ExpressionFor(assumeStatement.Condition);
Bpl.Type conditionType = this.sink.CciTypeToBoogie(assumeStatement.Condition.Type);
if (conditionType == this.sink.Heap.RefType) {
conditionExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, conditionExpr, Bpl.Expr.Ident(this.sink.Heap.NullRef));
}
else if (conditionType == Bpl.Type.Int) {
conditionExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, conditionExpr, Bpl.Expr.Literal(0));
}
else {
System.Diagnostics.Debug.Assert(conditionType == Bpl.Type.Bool);
}
StmtBuilder.Add(new Bpl.AssumeCmd(assumeStatement.Token(), conditionExpr));
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) Works, but still a stub</remarks>
/// <param name="conditionalStatement"></param>
public override void TraverseChildren(IConditionalStatement conditionalStatement) {
StatementTraverser thenTraverser = this.factory.MakeStatementTraverser(this.sink, this.PdbReader, this.contractContext);
StatementTraverser elseTraverser = this.factory.MakeStatementTraverser(this.sink, this.PdbReader, this.contractContext);
ExpressionTraverser condTraverser = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
if (this.sink.Options.instrumentBranches) {
var tok = conditionalStatement.Token();
thenTraverser.StmtBuilder.Add(
new Bpl.AssumeCmd(tok, Bpl.Expr.True, new Bpl.QKeyValue(Bpl.Token.NoToken, "breadcrumb", new List<object> { Bpl.Expr.Literal(this.NextUniqueNumber()) }, null))
);
elseTraverser.StmtBuilder.Add(
new Bpl.AssumeCmd(tok, Bpl.Expr.True, new Bpl.QKeyValue(Bpl.Token.NoToken, "breadcrumb", new List<object> { Bpl.Expr.Literal(this.NextUniqueNumber()) }, null))
);
}
condTraverser.Traverse(conditionalStatement.Condition);
thenTraverser.Traverse(conditionalStatement.TrueBranch);
elseTraverser.Traverse(conditionalStatement.FalseBranch);
Bpl.Expr conditionExpr = condTraverser.TranslatedExpressions.Pop();
Bpl.Type conditionType = this.sink.CciTypeToBoogie(conditionalStatement.Condition.Type);
if (conditionType == this.sink.Heap.RefType) {
conditionExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, conditionExpr, Bpl.Expr.Ident(this.sink.Heap.NullRef));
}
else if (conditionType == Bpl.Type.Int) {
conditionExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, conditionExpr, Bpl.Expr.Literal(0));
}
else {
System.Diagnostics.Debug.Assert(conditionType == Bpl.Type.Bool);
}
Bpl.IfCmd ifcmd = new Bpl.IfCmd(conditionalStatement.Token(),
conditionExpr,
thenTraverser.StmtBuilder.Collect(conditionalStatement.TrueBranch.Token()),
null,
elseTraverser.StmtBuilder.Collect(conditionalStatement.FalseBranch.Token())
);
StmtBuilder.Add(ifcmd);
}
private static int counter = 0;
internal int NextUniqueNumber() {
return counter++;
}
/// <summary>
///
/// </summary>
/// <param name="expressionStatement"></param>
/// <remarks> TODO: might be wrong for the general case</remarks>
public override void TraverseChildren(IExpressionStatement expressionStatement) {
ExpressionTraverser etrav = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
etrav.Traverse(expressionStatement.Expression);
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) Not Implemented</remarks>
/// <param name="breakStatement"></param>
public override void TraverseChildren(IBreakStatement breakStatement) {
throw new TranslationException("Break statements are not handled");
//StmtBuilder.Add(new Bpl.BreakCmd(breakStatement.Token(), "I dont know"));
}
public override void TraverseChildren(IContinueStatement continueStatement) {
throw new TranslationException("Continue statements are not handled");
}
public override void TraverseChildren(ISwitchStatement switchStatement) {
var eTraverser = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
eTraverser.Traverse(switchStatement.Expression);
var conditionExpr = eTraverser.TranslatedExpressions.Pop();
// Can't depend on default case existing or its index in the collection.
var switchCases = new List<ISwitchCase>();
ISwitchCase defaultCase = null;
foreach (var switchCase in switchStatement.Cases) {
if (switchCase.IsDefault) {
defaultCase = switchCase;
} else {
switchCases.Add(switchCase);
}
}
Bpl.StmtList defaultStmts = null;
if (defaultCase != null) {
var defaultBodyTraverser = this.factory.MakeStatementTraverser(this.sink, this.PdbReader, this.contractContext);
defaultBodyTraverser.Traverse(defaultCase.Body);
defaultStmts = defaultBodyTraverser.StmtBuilder.Collect(defaultCase.Token());
}
Bpl.IfCmd ifCmd = null;
for (int i = switchCases.Count-1; 0 <= i; i--) {
var switchCase = switchCases[i];
var scTraverser = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
scTraverser.Traverse(switchCase.Expression);
var scConditionExpr = scTraverser.TranslatedExpressions.Pop();
var condition = Bpl.Expr.Eq(conditionExpr, scConditionExpr);
var scBodyTraverser = this.factory.MakeStatementTraverser(this.sink, this.PdbReader, this.contractContext);
scBodyTraverser.Traverse(switchCase.Body);
ifCmd = new Bpl.IfCmd(switchCase.Token(),
condition,
scBodyTraverser.StmtBuilder.Collect(switchCase.Token()),
ifCmd,
defaultStmts);
defaultStmts = null; // default body goes only into the innermost if-then-else
}
StmtBuilder.Add(ifCmd);
}
/// <summary>
/// If the local declaration has an initial value, then generate the
/// statement "loc := e" from it.
/// Special case: if "loc" is a struct, then treat it as a call to
/// the default ctor.
/// Otherwise ignore it.
/// </summary>
public override void TraverseChildren(ILocalDeclarationStatement localDeclarationStatement) {
var initVal = localDeclarationStatement.InitialValue;
var typ = localDeclarationStatement.LocalVariable.Type;
var isStruct = TranslationHelper.IsStruct(typ);
if (initVal == null && !isStruct)
return;
var boogieLocal = this.sink.FindOrCreateLocalVariable(localDeclarationStatement.LocalVariable);
var boogieLocalExpr = Bpl.Expr.Ident(boogieLocal);
var tok = localDeclarationStatement.Token();
Bpl.Expr e = null;
var structCopy = isStruct && initVal != null && !(initVal is IDefaultValue);
// then a struct value of type S is being assigned: "lhs := s"
// model this as the statement "call lhs := S..#copy_ctor(s)" that does the bit-wise copying
if (isStruct) {
var defaultValue = new DefaultValue() {
DefaultValueType = typ,
Locations = new List<ILocation>(localDeclarationStatement.Locations),
Type = typ,
};
var e2 = ExpressionFor(defaultValue);
StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok, boogieLocalExpr, e2));
if (structCopy) {
var proc = this.sink.FindOrCreateProcedureForStructCopy(typ);
e = ExpressionFor(initVal);
StmtBuilder.Add(new Bpl.CallCmd(tok, proc.Name, new List<Bpl.Expr> { e, boogieLocalExpr, }, new List<Bpl.IdentifierExpr>()));
}
} else {
e = ExpressionFor(initVal);
StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok, boogieLocalExpr, e));
}
return;
}
public override void TraverseChildren(IPushStatement pushStatement) {
var tok = pushStatement.Token();
var val = pushStatement.ValueToPush;
var dup = val as IDupValue;
IExpression e;
if (dup != null) {
e = this.operandStack.Peek();
} else {
e = val;
}
this.operandStack.Push(e);
return;
}
/// <summary>
///
/// </summary>
/// <remarks>(mschaef) not implemented</remarks>
/// <param name="returnStatement"></param>
public override void TraverseChildren(IReturnStatement returnStatement) {
Bpl.IToken tok = returnStatement.Token();
if (returnStatement.Expression != null) {
ExpressionTraverser etrav = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
etrav.Traverse(returnStatement.Expression);
if (this.sink.ReturnVariable == null || etrav.TranslatedExpressions.Count < 1) {
throw new TranslationException(String.Format("{0} returns a value that is not supported by the function", returnStatement.ToString()));
}
StmtBuilder.Add(Bpl.Cmd.SimpleAssign(tok,
new Bpl.IdentifierExpr(tok, this.sink.ReturnVariable), etrav.TranslatedExpressions.Pop()));
}
// FEEDBACK TODO extract into a method
if (PhoneCodeHelper.instance().PhoneFeedbackToggled) {
IMethodDefinition methodTranslated = sink.getMethodBeingTranslated();
if (methodTranslated != null && PhoneCodeHelper.instance().isMethodInputHandlerOrFeedbackOverride(methodTranslated) &&
!PhoneCodeHelper.instance().isMethodIgnoredForFeedback(methodTranslated)) {
Bpl.AssertCmd falseAssertion = new Bpl.AssertCmd(Bpl.Token.NoToken, Bpl.LiteralExpr.False);
StmtBuilder.Add(falseAssertion);
}
}
StmtBuilder.Add(new Bpl.ReturnCmd(returnStatement.Token()));
}
#endregion
#region Goto and Labels
public override void TraverseChildren(IGotoStatement gotoStatement) {
IName target = gotoStatement.TargetStatement.Label;
ITryCatchFinallyStatement targetStatement = this.sink.MostNestedTryStatement(target);
int count = 0;
while (count < this.sink.nestedTryCatchFinallyStatements.Count) {
int index = this.sink.nestedTryCatchFinallyStatements.Count - count - 1;
ITryCatchFinallyStatement nestedStatement = this.sink.nestedTryCatchFinallyStatements[index].Item1;
if (targetStatement == nestedStatement)
break;
int labelId;
string label;
this.sink.AddEscapingEdge(nestedStatement, out labelId, out label);
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Literal(labelId)));
string finallyLabel = this.sink.FindOrCreateFinallyLabel(nestedStatement);
StmtBuilder.Add(new Bpl.GotoCmd(gotoStatement.Token(), new Bpl.StringSeq(finallyLabel)));
StmtBuilder.AddLabelCmd(label);
count++;
}
StmtBuilder.Add(new Bpl.GotoCmd(gotoStatement.Token(), new Bpl.StringSeq(target.Value)));
}
/// <summary>
///
/// </summary>
/// <remarks> (mschaef) not sure if there is more work to do</remarks>
/// <param name="labeledStatement"></param>
public override void TraverseChildren(ILabeledStatement labeledStatement) {
StmtBuilder.AddLabelCmd(labeledStatement.Label.Value);
base.Traverse(labeledStatement.Statement);
}
#endregion
#region Looping Statements
public override void TraverseChildren(IWhileDoStatement whileDoStatement) {
throw new TranslationException("WhileDo statements are not handled");
}
public override void TraverseChildren(IForEachStatement forEachStatement) {
throw new TranslationException("ForEach statements are not handled");
}
public override void TraverseChildren(IForStatement forStatement) {
throw new TranslationException("For statements are not handled");
}
#endregion
public void GenerateDispatchContinuation(ITryCatchFinallyStatement tryCatchFinallyStatement) {
string continuationLabel = this.sink.FindOrCreateContinuationLabel(tryCatchFinallyStatement);
Bpl.IfCmd elseIfCmd = new Bpl.IfCmd(Bpl.Token.NoToken, Bpl.Expr.Literal(true),
TranslationHelper.BuildStmtList(new Bpl.GotoCmd(Bpl.Token.NoToken, new Bpl.StringSeq(continuationLabel))), null, null);
List<string> edges = sink.EscapingEdges(tryCatchFinallyStatement);
Bpl.IdentifierExpr labelExpr = Bpl.Expr.Ident(this.sink.LabelVariable);
for (int i = 0; i < edges.Count; i++) {
string label = edges[i];
Bpl.GotoCmd gotoCmd = new Bpl.GotoCmd(Bpl.Token.NoToken, new Bpl.StringSeq(label));
Bpl.Expr targetExpr = Bpl.Expr.Literal(i);
elseIfCmd = new Bpl.IfCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, labelExpr, targetExpr),
TranslationHelper.BuildStmtList(gotoCmd), elseIfCmd, null);
}
this.StmtBuilder.Add(elseIfCmd);
}
private void RaiseExceptionHelper(Bpl.StmtListBuilder builder) {
int count = this.sink.nestedTryCatchFinallyStatements.Count;
if (count == 0) {
// FEEDBACK TODO unfortunately return statements are created here too
// FEEDBACK TODO extract into a method
if (PhoneCodeHelper.instance().PhoneFeedbackToggled) {
IMethodDefinition methodTranslated = sink.getMethodBeingTranslated();
if (methodTranslated != null && PhoneCodeHelper.instance().isMethodInputHandlerOrFeedbackOverride(methodTranslated) &&
!PhoneCodeHelper.instance().isMethodIgnoredForFeedback(methodTranslated)) {
Bpl.AssertCmd falseAssertion = new Bpl.AssertCmd(Bpl.Token.NoToken, Bpl.LiteralExpr.False);
builder.Add(falseAssertion);
}
}
builder.Add(new Bpl.ReturnCmd(Bpl.Token.NoToken));
}
else {
Tuple<ITryCatchFinallyStatement, Sink.TryCatchFinallyContext> topOfStack = this.sink.nestedTryCatchFinallyStatements[count - 1];
string exceptionTarget;
if (topOfStack.Item2 == Sink.TryCatchFinallyContext.InTry) {
exceptionTarget = this.sink.FindOrCreateCatchLabel(topOfStack.Item1);
}
else if (topOfStack.Item2 == Sink.TryCatchFinallyContext.InCatch) {
builder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Literal(-1)));
exceptionTarget = this.sink.FindOrCreateFinallyLabel(topOfStack.Item1);
}
else {
exceptionTarget = this.sink.FindOrCreateContinuationLabel(topOfStack.Item1);
}
builder.Add(new Bpl.GotoCmd(Bpl.Token.NoToken, new Bpl.StringSeq(exceptionTarget)));
}
}
public void RaiseException() {
RaiseExceptionHelper(StmtBuilder);
}
public void RaiseException(Bpl.Expr e) {
Bpl.StmtListBuilder builder = new Bpl.StmtListBuilder();
RaiseExceptionHelper(builder);
Bpl.IfCmd ifCmd = new Bpl.IfCmd(Bpl.Token.NoToken, e, builder.Collect(Bpl.Token.NoToken), null, null);
StmtBuilder.Add(ifCmd);
}
public override void TraverseChildren(ITryCatchFinallyStatement tryCatchFinallyStatement) {
this.sink.nestedTryCatchFinallyStatements.Add(new Tuple<ITryCatchFinallyStatement, Sink.TryCatchFinallyContext>(tryCatchFinallyStatement, Sink.TryCatchFinallyContext.InTry));
this.Traverse(tryCatchFinallyStatement.TryBody);
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Literal(-1)));
StmtBuilder.Add(new Bpl.GotoCmd(Bpl.Token.NoToken, new Bpl.StringSeq(this.sink.FindOrCreateFinallyLabel(tryCatchFinallyStatement))));
this.sink.nestedTryCatchFinallyStatements.RemoveAt(this.sink.nestedTryCatchFinallyStatements.Count - 1);
StmtBuilder.AddLabelCmd(this.sink.FindOrCreateCatchLabel(tryCatchFinallyStatement));
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LocalExcVariable), Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable)));
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.Heap.NullRef)));
List<Bpl.StmtList> catchStatements = new List<Bpl.StmtList>();
List<Bpl.Expr> typeReferences = new List<Bpl.Expr>();
this.sink.nestedTryCatchFinallyStatements.Add(new Tuple<ITryCatchFinallyStatement, Sink.TryCatchFinallyContext>(tryCatchFinallyStatement, Sink.TryCatchFinallyContext.InCatch));
foreach (ICatchClause catchClause in tryCatchFinallyStatement.CatchClauses) {
typeReferences.Insert(0, this.sink.FindOrCreateTypeReference(catchClause.ExceptionType, true));
StatementTraverser catchTraverser = this.factory.MakeStatementTraverser(this.sink, this.PdbReader, this.contractContext);
if (catchClause.ExceptionContainer != Dummy.LocalVariable) {
Bpl.Variable catchClauseVariable = this.sink.FindOrCreateLocalVariable(catchClause.ExceptionContainer);
catchTraverser.StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(catchClauseVariable), Bpl.Expr.Ident(this.sink.LocalExcVariable)));
}
catchTraverser.Traverse(catchClause.Body);
catchTraverser.StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Literal(-1)));
catchTraverser.StmtBuilder.Add(new Bpl.GotoCmd(Bpl.Token.NoToken, new Bpl.StringSeq(this.sink.FindOrCreateFinallyLabel(tryCatchFinallyStatement))));
catchStatements.Insert(0, catchTraverser.StmtBuilder.Collect(catchClause.Token()));
}
Bpl.IfCmd elseIfCmd = new Bpl.IfCmd(Bpl.Token.NoToken, Bpl.Expr.Literal(false), TranslationHelper.BuildStmtList(new Bpl.ReturnCmd(Bpl.Token.NoToken)), null, null);
Bpl.Expr dynTypeOfOperand = this.sink.Heap.DynamicType(Bpl.Expr.Ident(this.sink.LocalExcVariable));
for (int i = 0; i < catchStatements.Count; i++) {
Bpl.Expr expr = new Bpl.NAryExpr(Bpl.Token.NoToken, new Bpl.FunctionCall(this.sink.Heap.Subtype), new Bpl.ExprSeq(dynTypeOfOperand, typeReferences[i]));
elseIfCmd = new Bpl.IfCmd(Bpl.Token.NoToken, expr, catchStatements[i], elseIfCmd, null);
}
this.StmtBuilder.Add(elseIfCmd);
this.StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.LocalExcVariable)));
RaiseException();
this.sink.nestedTryCatchFinallyStatements.RemoveAt(this.sink.nestedTryCatchFinallyStatements.Count - 1);
this.StmtBuilder.AddLabelCmd(this.sink.FindOrCreateFinallyLabel(tryCatchFinallyStatement));
if (tryCatchFinallyStatement.FinallyBody != null) {
this.sink.nestedTryCatchFinallyStatements.Add(new Tuple<ITryCatchFinallyStatement, Sink.TryCatchFinallyContext>(tryCatchFinallyStatement, Sink.TryCatchFinallyContext.InFinally));
Bpl.Variable savedExcVariable = this.sink.CreateFreshLocal(this.sink.Heap.RefType);
Bpl.Variable savedLabelVariable = this.sink.CreateFreshLocal(Bpl.Type.Int);
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(savedExcVariable), Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable)));
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(savedLabelVariable), Bpl.Expr.Ident(this.sink.LabelVariable)));
this.Traverse(tryCatchFinallyStatement.FinallyBody);
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(savedExcVariable)));
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Ident(savedLabelVariable)));
this.sink.nestedTryCatchFinallyStatements.RemoveAt(this.sink.nestedTryCatchFinallyStatements.Count - 1);
}
GenerateDispatchContinuation(tryCatchFinallyStatement);
StmtBuilder.AddLabelCmd(this.sink.FindOrCreateContinuationLabel(tryCatchFinallyStatement));
Bpl.Expr raiseExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.Heap.NullRef));
RaiseException(raiseExpr);
}
public override void TraverseChildren(IThrowStatement throwStatement) {
ExpressionTraverser exceptionTraverser = this.factory.MakeExpressionTraverser(this.sink, this, this.contractContext);
exceptionTraverser.Traverse(throwStatement.Exception);
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), exceptionTraverser.TranslatedExpressions.Pop()));
RaiseException();
}
public override void TraverseChildren(IRethrowStatement rethrowStatement) {
StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.LocalExcVariable)));
RaiseException();
}
}
}
|