~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/CSharp/Analysis/ControlFlow.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
2
 
// 
3
 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
 
// software and associated documentation files (the "Software"), to deal in the Software
5
 
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
 
// to whom the Software is furnished to do so, subject to the following conditions:
8
 
// 
9
 
// The above copyright notice and this permission notice shall be included in all copies or
10
 
// substantial portions of the Software.
11
 
// 
12
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
 
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
 
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
 
// DEALINGS IN THE SOFTWARE.
18
 
 
19
 
using System;
20
 
using System.Collections.Generic;
21
 
using System.Diagnostics;
22
 
using System.Linq;
23
 
using System.Threading;
24
 
 
25
 
using ICSharpCode.NRefactory.CSharp.Resolver;
26
 
using ICSharpCode.NRefactory.TypeSystem;
27
 
 
28
 
namespace ICSharpCode.NRefactory.CSharp.Analysis
29
 
{
30
 
        /// <summary>
31
 
        /// Represents a node in the control flow graph of a C# method.
32
 
        /// </summary>
33
 
        public class ControlFlowNode
34
 
        {
35
 
                public readonly Statement PreviousStatement;
36
 
                public readonly Statement NextStatement;
37
 
                
38
 
                public readonly ControlFlowNodeType Type;
39
 
                
40
 
                public readonly List<ControlFlowEdge> Outgoing = new List<ControlFlowEdge>();
41
 
                public readonly List<ControlFlowEdge> Incoming = new List<ControlFlowEdge>();
42
 
                
43
 
                public ControlFlowNode(Statement previousStatement, Statement nextStatement, ControlFlowNodeType type)
44
 
                {
45
 
                        if (previousStatement == null && nextStatement == null)
46
 
                                throw new ArgumentException("previousStatement and nextStatement must not be both null");
47
 
                        this.PreviousStatement = previousStatement;
48
 
                        this.NextStatement = nextStatement;
49
 
                        this.Type = type;
50
 
                }
51
 
        }
52
 
        
53
 
        public enum ControlFlowNodeType
54
 
        {
55
 
                /// <summary>
56
 
                /// Unknown node type
57
 
                /// </summary>
58
 
                None,
59
 
                /// <summary>
60
 
                /// Node in front of a statement
61
 
                /// </summary>
62
 
                StartNode,
63
 
                /// <summary>
64
 
                /// Node between two statements
65
 
                /// </summary>
66
 
                BetweenStatements,
67
 
                /// <summary>
68
 
                /// Node at the end of a statement list
69
 
                /// </summary>
70
 
                EndNode,
71
 
                /// <summary>
72
 
                /// Node representing the position before evaluating the condition of a loop.
73
 
                /// </summary>
74
 
                LoopCondition
75
 
        }
76
 
        
77
 
        public class ControlFlowEdge
78
 
        {
79
 
                public readonly ControlFlowNode From;
80
 
                public readonly ControlFlowNode To;
81
 
                public readonly ControlFlowEdgeType Type;
82
 
                
83
 
                List<TryCatchStatement> jumpOutOfTryFinally;
84
 
                
85
 
                public ControlFlowEdge(ControlFlowNode from, ControlFlowNode to, ControlFlowEdgeType type)
86
 
                {
87
 
                        if (from == null)
88
 
                                throw new ArgumentNullException("from");
89
 
                        if (to == null)
90
 
                                throw new ArgumentNullException("to");
91
 
                        this.From = from;
92
 
                        this.To = to;
93
 
                        this.Type = type;
94
 
                }
95
 
                
96
 
                internal void AddJumpOutOfTryFinally(TryCatchStatement tryFinally)
97
 
                {
98
 
                        if (jumpOutOfTryFinally == null)
99
 
                                jumpOutOfTryFinally = new List<TryCatchStatement>();
100
 
                        jumpOutOfTryFinally.Add(tryFinally);
101
 
                }
102
 
                
103
 
                /// <summary>
104
 
                /// Gets whether this control flow edge is leaving any try-finally statements.
105
 
                /// </summary>
106
 
                public bool IsLeavingTryFinally {
107
 
                        get { return jumpOutOfTryFinally != null; }
108
 
                }
109
 
                
110
 
                /// <summary>
111
 
                /// Gets the try-finally statements that this control flow edge is leaving.
112
 
                /// </summary>
113
 
                public IEnumerable<TryCatchStatement> TryFinallyStatements {
114
 
                        get { return jumpOutOfTryFinally ?? EmptyList<TryCatchStatement>.Instance; }
115
 
                }
116
 
        }
117
 
        
118
 
        public enum ControlFlowEdgeType
119
 
        {
120
 
                /// <summary>
121
 
                /// Regular control flow.
122
 
                /// </summary>
123
 
                Normal,
124
 
                /// <summary>
125
 
                /// Conditional control flow (edge taken if condition is true)
126
 
                /// </summary>
127
 
                ConditionTrue,
128
 
                /// <summary>
129
 
                /// Conditional control flow (edge taken if condition is false)
130
 
                /// </summary>
131
 
                ConditionFalse,
132
 
                /// <summary>
133
 
                /// A jump statement (goto, goto case, break or continue)
134
 
                /// </summary>
135
 
                Jump
136
 
        }
137
 
        
138
 
        /// <summary>
139
 
        /// Constructs the control flow graph for C# statements.
140
 
        /// </summary>
141
 
        public class ControlFlowGraphBuilder
142
 
        {
143
 
                // Written according to the reachability rules in the C# spec (§8.1 End points and reachability)
144
 
                
145
 
                protected virtual ControlFlowNode CreateNode(Statement previousStatement, Statement nextStatement, ControlFlowNodeType type)
146
 
                {
147
 
                        return new ControlFlowNode(previousStatement, nextStatement, type);
148
 
                }
149
 
                
150
 
                protected virtual ControlFlowEdge CreateEdge(ControlFlowNode from, ControlFlowNode to, ControlFlowEdgeType type)
151
 
                {
152
 
                        return new ControlFlowEdge(from, to, type);
153
 
                }
154
 
                
155
 
                Statement rootStatement;
156
 
                ResolveVisitor resolveVisitor;
157
 
                List<ControlFlowNode> nodes;
158
 
                Dictionary<string, ControlFlowNode> labels;
159
 
                List<ControlFlowNode> gotoStatements;
160
 
                
161
 
                public IList<ControlFlowNode> BuildControlFlowGraph(Statement statement, ITypeResolveContext context)
162
 
                {
163
 
                        return BuildControlFlowGraph(statement, context, CancellationToken.None);
164
 
                }
165
 
                
166
 
                public IList<ControlFlowNode> BuildControlFlowGraph(Statement statement, ITypeResolveContext context, CancellationToken cancellationToken)
167
 
                {
168
 
                        return BuildControlFlowGraph(statement, new ResolveVisitor(
169
 
                                new CSharpResolver(context, cancellationToken),
170
 
                                null));
171
 
                }
172
 
                
173
 
                public IList<ControlFlowNode> BuildControlFlowGraph(Statement statement, ResolveVisitor resolveVisitor)
174
 
                {
175
 
                        if (statement == null)
176
 
                                throw new ArgumentNullException("statement");
177
 
                        if (resolveVisitor == null)
178
 
                                throw new ArgumentNullException("resolveVisitor");
179
 
                        
180
 
                        NodeCreationVisitor nodeCreationVisitor = new NodeCreationVisitor();
181
 
                        nodeCreationVisitor.builder = this;
182
 
                        try {
183
 
                                this.nodes = new List<ControlFlowNode>();
184
 
                                this.labels = new Dictionary<string, ControlFlowNode>();
185
 
                                this.gotoStatements = new List<ControlFlowNode>();
186
 
                                this.rootStatement = statement;
187
 
                                this.resolveVisitor = resolveVisitor;
188
 
                                ControlFlowNode entryPoint = CreateStartNode(statement);
189
 
                                statement.AcceptVisitor(nodeCreationVisitor, entryPoint);
190
 
                                
191
 
                                // Resolve goto statements:
192
 
                                foreach (ControlFlowNode gotoStmt in gotoStatements) {
193
 
                                        string label = ((GotoStatement)gotoStmt.NextStatement).Label;
194
 
                                        ControlFlowNode labelNode;
195
 
                                        if (labels.TryGetValue(label, out labelNode))
196
 
                                                nodeCreationVisitor.Connect(gotoStmt, labelNode, ControlFlowEdgeType.Jump);
197
 
                                }
198
 
                                
199
 
                                AnnotateLeaveEdgesWithTryFinallyBlocks();
200
 
                                
201
 
                                return nodes;
202
 
                        } finally {
203
 
                                this.nodes = null;
204
 
                                this.labels = null;
205
 
                                this.gotoStatements = null;
206
 
                                this.rootStatement = null;
207
 
                                this.resolveVisitor = null;
208
 
                        }
209
 
                }
210
 
                
211
 
                void AnnotateLeaveEdgesWithTryFinallyBlocks()
212
 
                {
213
 
                        foreach (ControlFlowEdge edge in nodes.SelectMany(n => n.Outgoing)) {
214
 
                                if (edge.Type != ControlFlowEdgeType.Jump) {
215
 
                                        // Only jumps are potential candidates for leaving try-finally blocks.
216
 
                                        // Note that the regular edges leaving try or catch blocks are already annotated by the visitor.
217
 
                                        continue;
218
 
                                }
219
 
                                Statement gotoStatement = edge.From.NextStatement;
220
 
                                Debug.Assert(gotoStatement is GotoStatement || gotoStatement is GotoDefaultStatement || gotoStatement is GotoCaseStatement || gotoStatement is BreakStatement || gotoStatement is ContinueStatement);
221
 
                                Statement targetStatement = edge.To.PreviousStatement ?? edge.To.NextStatement;
222
 
                                if (gotoStatement.Parent == targetStatement.Parent)
223
 
                                        continue;
224
 
                                HashSet<TryCatchStatement> targetParentTryCatch = new HashSet<TryCatchStatement>(targetStatement.Ancestors.OfType<TryCatchStatement>());
225
 
                                for (AstNode node = gotoStatement.Parent; node != null; node = node.Parent) {
226
 
                                        TryCatchStatement leftTryCatch = node as TryCatchStatement;
227
 
                                        if (leftTryCatch != null) {
228
 
                                                if (targetParentTryCatch.Contains(leftTryCatch))
229
 
                                                        break;
230
 
                                                if (!leftTryCatch.FinallyBlock.IsNull)
231
 
                                                        edge.AddJumpOutOfTryFinally(leftTryCatch);
232
 
                                        }
233
 
                                }
234
 
                        }
235
 
                }
236
 
                
237
 
                #region Create*Node
238
 
                ControlFlowNode CreateStartNode(Statement statement)
239
 
                {
240
 
                        ControlFlowNode node = CreateNode(null, statement, ControlFlowNodeType.StartNode);
241
 
                        nodes.Add(node);
242
 
                        return node;
243
 
                }
244
 
                
245
 
                ControlFlowNode CreateSpecialNode(Statement statement, ControlFlowNodeType type, bool addToNodeList = true)
246
 
                {
247
 
                        ControlFlowNode node = CreateNode(null, statement, type);
248
 
                        if (addToNodeList)
249
 
                                nodes.Add(node);
250
 
                        return node;
251
 
                }
252
 
                
253
 
                ControlFlowNode CreateEndNode(Statement statement, bool addToNodeList = true)
254
 
                {
255
 
                        Statement nextStatement;
256
 
                        if (statement == rootStatement) {
257
 
                                nextStatement = null;
258
 
                        } else {
259
 
                                // Find the next statement in the same role:
260
 
                                AstNode next = statement;
261
 
                                do {
262
 
                                        next = next.NextSibling;
263
 
                                } while (next != null && next.Role != statement.Role);
264
 
                                nextStatement = next as Statement;
265
 
                        }
266
 
                        ControlFlowNodeType type = nextStatement != null ? ControlFlowNodeType.BetweenStatements : ControlFlowNodeType.EndNode;
267
 
                        ControlFlowNode node = CreateNode(statement, nextStatement, type);
268
 
                        if (addToNodeList)
269
 
                                nodes.Add(node);
270
 
                        return node;
271
 
                }
272
 
                #endregion
273
 
                
274
 
                #region Constant evaluation
275
 
                /// <summary>
276
 
                /// Gets/Sets whether to handle only primitive expressions as constants (no complex expressions like "a + b").
277
 
                /// </summary>
278
 
                public bool EvaluateOnlyPrimitiveConstants { get; set; }
279
 
                
280
 
                /// <summary>
281
 
                /// Evaluates an expression.
282
 
                /// </summary>
283
 
                /// <returns>The constant value of the expression; or null if the expression is not a constant.</returns>
284
 
                ConstantResolveResult EvaluateConstant(Expression expr)
285
 
                {
286
 
                        if (EvaluateOnlyPrimitiveConstants) {
287
 
                                if (!(expr is PrimitiveExpression || expr is NullReferenceExpression))
288
 
                                        return null;
289
 
                        }
290
 
                        return resolveVisitor.Resolve(expr) as ConstantResolveResult;
291
 
                }
292
 
                
293
 
                /// <summary>
294
 
                /// Evaluates an expression.
295
 
                /// </summary>
296
 
                /// <returns>The value of the constant boolean expression; or null if the value is not a constant boolean expression.</returns>
297
 
                bool? EvaluateCondition(Expression expr)
298
 
                {
299
 
                        ConstantResolveResult rr = EvaluateConstant(expr);
300
 
                        if (rr != null)
301
 
                                return rr.ConstantValue as bool?;
302
 
                        else
303
 
                                return null;
304
 
                }
305
 
                
306
 
                bool AreEqualConstants(ConstantResolveResult c1, ConstantResolveResult c2)
307
 
                {
308
 
                        if (c1 == null || c2 == null)
309
 
                                return false;
310
 
                        CSharpResolver r = new CSharpResolver(resolveVisitor.TypeResolveContext, resolveVisitor.CancellationToken);
311
 
                        ResolveResult c = r.ResolveBinaryOperator(BinaryOperatorType.Equality, c1, c2);
312
 
                        return c.IsCompileTimeConstant && (c.ConstantValue as bool?) == true;
313
 
                }
314
 
                #endregion
315
 
                
316
 
                sealed class NodeCreationVisitor : DepthFirstAstVisitor<ControlFlowNode, ControlFlowNode>
317
 
                {
318
 
                        // 'data' parameter: input control flow node (start of statement being visited)
319
 
                        // Return value: result control flow node (end of statement being visited)
320
 
                        
321
 
                        internal ControlFlowGraphBuilder builder;
322
 
                        Stack<ControlFlowNode> breakTargets = new Stack<ControlFlowNode>();
323
 
                        Stack<ControlFlowNode> continueTargets = new Stack<ControlFlowNode>();
324
 
                        List<ControlFlowNode> gotoCaseOrDefault = new List<ControlFlowNode>();
325
 
                        
326
 
                        internal ControlFlowEdge Connect(ControlFlowNode from, ControlFlowNode to, ControlFlowEdgeType type = ControlFlowEdgeType.Normal)
327
 
                        {
328
 
                                ControlFlowEdge edge = builder.CreateEdge(from, to, type);
329
 
                                from.Outgoing.Add(edge);
330
 
                                to.Incoming.Add(edge);
331
 
                                return edge;
332
 
                        }
333
 
                        
334
 
                        /// <summary>
335
 
                        /// Creates an end node for <c>stmt</c> and connects <c>from</c> with the new node.
336
 
                        /// </summary>
337
 
                        ControlFlowNode CreateConnectedEndNode(Statement stmt, ControlFlowNode from)
338
 
                        {
339
 
                                ControlFlowNode newNode = builder.CreateEndNode(stmt);
340
 
                                Connect(from, newNode);
341
 
                                return newNode;
342
 
                        }
343
 
                        
344
 
                        protected override ControlFlowNode VisitChildren(AstNode node, ControlFlowNode data)
345
 
                        {
346
 
                                // We have overrides for all possible expressions and should visit expressions only.
347
 
                                throw new NotImplementedException();
348
 
                        }
349
 
                        
350
 
                        public override ControlFlowNode VisitBlockStatement(BlockStatement blockStatement, ControlFlowNode data)
351
 
                        {
352
 
                                // C# 4.0 spec: §8.2 Blocks
353
 
                                ControlFlowNode childNode = HandleStatementList(blockStatement.Statements, data);
354
 
                                return CreateConnectedEndNode(blockStatement, childNode);
355
 
                        }
356
 
                        
357
 
                        ControlFlowNode HandleStatementList(AstNodeCollection<Statement> statements, ControlFlowNode source)
358
 
                        {
359
 
                                ControlFlowNode childNode = null;
360
 
                                foreach (Statement stmt in statements) {
361
 
                                        if (childNode == null) {
362
 
                                                childNode = builder.CreateStartNode(stmt);
363
 
                                                if (source != null)
364
 
                                                        Connect(source, childNode);
365
 
                                        }
366
 
                                        Debug.Assert(childNode.NextStatement == stmt);
367
 
                                        childNode = stmt.AcceptVisitor(this, childNode);
368
 
                                        Debug.Assert(childNode.PreviousStatement == stmt);
369
 
                                }
370
 
                                return childNode ?? source;
371
 
                        }
372
 
                        
373
 
                        public override ControlFlowNode VisitEmptyStatement(EmptyStatement emptyStatement, ControlFlowNode data)
374
 
                        {
375
 
                                return CreateConnectedEndNode(emptyStatement, data);
376
 
                        }
377
 
                        
378
 
                        public override ControlFlowNode VisitLabelStatement(LabelStatement labelStatement, ControlFlowNode data)
379
 
                        {
380
 
                                ControlFlowNode end = CreateConnectedEndNode(labelStatement, data);
381
 
                                builder.labels[labelStatement.Label] = end;
382
 
                                return end;
383
 
                        }
384
 
                        
385
 
                        public override ControlFlowNode VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement, ControlFlowNode data)
386
 
                        {
387
 
                                return CreateConnectedEndNode(variableDeclarationStatement, data);
388
 
                        }
389
 
                        
390
 
                        public override ControlFlowNode VisitExpressionStatement(ExpressionStatement expressionStatement, ControlFlowNode data)
391
 
                        {
392
 
                                return CreateConnectedEndNode(expressionStatement, data);
393
 
                        }
394
 
                        
395
 
                        public override ControlFlowNode VisitIfElseStatement(IfElseStatement ifElseStatement, ControlFlowNode data)
396
 
                        {
397
 
                                bool? cond = builder.EvaluateCondition(ifElseStatement.Condition);
398
 
                                ControlFlowNode trueBegin = builder.CreateStartNode(ifElseStatement.TrueStatement);
399
 
                                if (cond != false)
400
 
                                        Connect(data, trueBegin, ControlFlowEdgeType.ConditionTrue);
401
 
                                ControlFlowNode trueEnd = ifElseStatement.TrueStatement.AcceptVisitor(this, trueBegin);
402
 
                                ControlFlowNode falseEnd;
403
 
                                if (ifElseStatement.FalseStatement.IsNull) {
404
 
                                        falseEnd = null;
405
 
                                } else {
406
 
                                        ControlFlowNode falseBegin = builder.CreateStartNode(ifElseStatement.FalseStatement);
407
 
                                        if (cond != true)
408
 
                                                Connect(data, falseBegin, ControlFlowEdgeType.ConditionFalse);
409
 
                                        falseEnd = ifElseStatement.FalseStatement.AcceptVisitor(this, falseBegin);
410
 
                                }
411
 
                                ControlFlowNode end = builder.CreateEndNode(ifElseStatement);
412
 
                                Connect(trueEnd, end);
413
 
                                if (falseEnd != null) {
414
 
                                        Connect(falseEnd, end);
415
 
                                } else if (cond != true) {
416
 
                                        Connect(data, end, ControlFlowEdgeType.ConditionFalse);
417
 
                                }
418
 
                                return end;
419
 
                        }
420
 
                        
421
 
                        public override ControlFlowNode VisitSwitchStatement(SwitchStatement switchStatement, ControlFlowNode data)
422
 
                        {
423
 
                                // First, figure out which switch section will get called (if the expression is constant):
424
 
                                ConstantResolveResult constant = builder.EvaluateConstant(switchStatement.Expression);
425
 
                                SwitchSection defaultSection = null;
426
 
                                SwitchSection sectionMatchedByConstant = null;
427
 
                                foreach (SwitchSection section in switchStatement.SwitchSections) {
428
 
                                        foreach (CaseLabel label in section.CaseLabels) {
429
 
                                                if (label.Expression.IsNull) {
430
 
                                                        defaultSection = section;
431
 
                                                } else if (constant != null) {
432
 
                                                        ConstantResolveResult labelConstant = builder.EvaluateConstant(label.Expression);
433
 
                                                        if (builder.AreEqualConstants(constant, labelConstant))
434
 
                                                                sectionMatchedByConstant = section;
435
 
                                                }
436
 
                                        }
437
 
                                }
438
 
                                if (constant != null && sectionMatchedByConstant == null)
439
 
                                        sectionMatchedByConstant = defaultSection;
440
 
                                
441
 
                                int gotoCaseOrDefaultInOuterScope = gotoCaseOrDefault.Count;
442
 
                                
443
 
                                ControlFlowNode end = builder.CreateEndNode(switchStatement, addToNodeList: false);
444
 
                                breakTargets.Push(end);
445
 
                                foreach (SwitchSection section in switchStatement.SwitchSections) {
446
 
                                        if (constant == null || section == sectionMatchedByConstant) {
447
 
                                                HandleStatementList(section.Statements, data);
448
 
                                        } else {
449
 
                                                // This section is unreachable: pass null to HandleStatementList.
450
 
                                                HandleStatementList(section.Statements, null);
451
 
                                        }
452
 
                                        // Don't bother connecting the ends of the sections: the 'break' statement takes care of that.
453
 
                                }
454
 
                                breakTargets.Pop();
455
 
                                if (defaultSection == null && sectionMatchedByConstant == null) {
456
 
                                        Connect(data, end);
457
 
                                }
458
 
                                
459
 
                                if (gotoCaseOrDefault.Count > gotoCaseOrDefaultInOuterScope) {
460
 
                                        // Resolve 'goto case' statements:
461
 
                                        throw new NotImplementedException();
462
 
                                }
463
 
                                
464
 
                                builder.nodes.Add(end);
465
 
                                return end;
466
 
                        }
467
 
                        
468
 
                        public override ControlFlowNode VisitGotoCaseStatement(GotoCaseStatement gotoCaseStatement, ControlFlowNode data)
469
 
                        {
470
 
                                gotoCaseOrDefault.Add(data);
471
 
                                return builder.CreateEndNode(gotoCaseStatement);
472
 
                        }
473
 
                        
474
 
                        public override ControlFlowNode VisitGotoDefaultStatement(GotoDefaultStatement gotoDefaultStatement, ControlFlowNode data)
475
 
                        {
476
 
                                gotoCaseOrDefault.Add(data);
477
 
                                return builder.CreateEndNode(gotoDefaultStatement);
478
 
                        }
479
 
                        
480
 
                        public override ControlFlowNode VisitWhileStatement(WhileStatement whileStatement, ControlFlowNode data)
481
 
                        {
482
 
                                // <data> <condition> while (cond) { <bodyStart> embeddedStmt; <bodyEnd> } <end>
483
 
                                ControlFlowNode end = builder.CreateEndNode(whileStatement, addToNodeList: false);
484
 
                                ControlFlowNode conditionNode = builder.CreateSpecialNode(whileStatement, ControlFlowNodeType.LoopCondition);
485
 
                                breakTargets.Push(end);
486
 
                                continueTargets.Push(conditionNode);
487
 
                                
488
 
                                Connect(data, conditionNode);
489
 
                                
490
 
                                bool? cond = builder.EvaluateCondition(whileStatement.Condition);
491
 
                                ControlFlowNode bodyStart = builder.CreateStartNode(whileStatement.EmbeddedStatement);
492
 
                                if (cond != false)
493
 
                                        Connect(conditionNode, bodyStart, ControlFlowEdgeType.ConditionTrue);
494
 
                                ControlFlowNode bodyEnd = whileStatement.EmbeddedStatement.AcceptVisitor(this, bodyStart);
495
 
                                Connect(bodyEnd, conditionNode);
496
 
                                if (cond != true)
497
 
                                        Connect(conditionNode, end, ControlFlowEdgeType.ConditionFalse);
498
 
                                
499
 
                                breakTargets.Pop();
500
 
                                continueTargets.Pop();
501
 
                                builder.nodes.Add(end);
502
 
                                return end;
503
 
                        }
504
 
                        
505
 
                        public override ControlFlowNode VisitDoWhileStatement(DoWhileStatement doWhileStatement, ControlFlowNode data)
506
 
                        {
507
 
                                // <data> do { <bodyStart> embeddedStmt; <bodyEnd>} <condition> while(cond); <end>
508
 
                                ControlFlowNode end = builder.CreateEndNode(doWhileStatement, addToNodeList: false);
509
 
                                ControlFlowNode conditionNode = builder.CreateSpecialNode(doWhileStatement, ControlFlowNodeType.LoopCondition, addToNodeList: false);
510
 
                                breakTargets.Push(end);
511
 
                                continueTargets.Push(conditionNode);
512
 
                                
513
 
                                ControlFlowNode bodyStart = builder.CreateStartNode(doWhileStatement.EmbeddedStatement);
514
 
                                Connect(data, bodyStart);
515
 
                                ControlFlowNode bodyEnd = doWhileStatement.EmbeddedStatement.AcceptVisitor(this, bodyStart);
516
 
                                Connect(bodyEnd, conditionNode);
517
 
                                
518
 
                                bool? cond = builder.EvaluateCondition(doWhileStatement.Condition);
519
 
                                if (cond != false)
520
 
                                        Connect(conditionNode, bodyStart, ControlFlowEdgeType.ConditionTrue);
521
 
                                if (cond != true)
522
 
                                        Connect(conditionNode, end, ControlFlowEdgeType.ConditionFalse);
523
 
                                
524
 
                                breakTargets.Pop();
525
 
                                continueTargets.Pop();
526
 
                                builder.nodes.Add(conditionNode);
527
 
                                builder.nodes.Add(end);
528
 
                                return end;
529
 
                        }
530
 
                        
531
 
                        public override ControlFlowNode VisitForStatement(ForStatement forStatement, ControlFlowNode data)
532
 
                        {
533
 
                                data = HandleStatementList(forStatement.Initializers, data);
534
 
                                // for (initializers <data>; <condition>cond; <iteratorStart>iterators<iteratorEnd>) { <bodyStart> embeddedStmt; <bodyEnd> } <end>
535
 
                                ControlFlowNode end = builder.CreateEndNode(forStatement, addToNodeList: false);
536
 
                                ControlFlowNode conditionNode = builder.CreateSpecialNode(forStatement, ControlFlowNodeType.LoopCondition);
537
 
                                Connect(data, conditionNode);
538
 
                                
539
 
                                int iteratorStartNodeID = builder.nodes.Count;
540
 
                                ControlFlowNode iteratorEnd = HandleStatementList(forStatement.Iterators, null);
541
 
                                ControlFlowNode iteratorStart;
542
 
                                if (iteratorEnd != null) {
543
 
                                        iteratorStart = builder.nodes[iteratorStartNodeID];
544
 
                                        Connect(iteratorEnd, conditionNode);
545
 
                                } else {
546
 
                                        iteratorStart = conditionNode;
547
 
                                }
548
 
                                
549
 
                                breakTargets.Push(end);
550
 
                                continueTargets.Push(iteratorStart);
551
 
                                
552
 
                                ControlFlowNode bodyStart = builder.CreateStartNode(forStatement.EmbeddedStatement);
553
 
                                ControlFlowNode bodyEnd = forStatement.EmbeddedStatement.AcceptVisitor(this, bodyStart);
554
 
                                Connect(bodyEnd, iteratorStart);
555
 
                                
556
 
                                breakTargets.Pop();
557
 
                                continueTargets.Pop();
558
 
                                
559
 
                                bool? cond = forStatement.Condition.IsNull ? true : builder.EvaluateCondition(forStatement.Condition);
560
 
                                if (cond != false)
561
 
                                        Connect(conditionNode, bodyStart, ControlFlowEdgeType.ConditionTrue);
562
 
                                if (cond != true)
563
 
                                        Connect(conditionNode, end, ControlFlowEdgeType.ConditionFalse);
564
 
                                
565
 
                                builder.nodes.Add(end);
566
 
                                return end;
567
 
                        }
568
 
                        
569
 
                        ControlFlowNode HandleEmbeddedStatement(Statement embeddedStatement, ControlFlowNode source)
570
 
                        {
571
 
                                if (embeddedStatement == null || embeddedStatement.IsNull)
572
 
                                        return source;
573
 
                                ControlFlowNode bodyStart = builder.CreateStartNode(embeddedStatement);
574
 
                                if (source != null)
575
 
                                        Connect(source, bodyStart);
576
 
                                return embeddedStatement.AcceptVisitor(this, bodyStart);
577
 
                        }
578
 
                        
579
 
                        public override ControlFlowNode VisitForeachStatement(ForeachStatement foreachStatement, ControlFlowNode data)
580
 
                        {
581
 
                                // <data> foreach (<condition>...) { <bodyStart>embeddedStmt<bodyEnd> } <end>
582
 
                                ControlFlowNode end = builder.CreateEndNode(foreachStatement, addToNodeList: false);
583
 
                                ControlFlowNode conditionNode = builder.CreateSpecialNode(foreachStatement, ControlFlowNodeType.LoopCondition);
584
 
                                Connect(data, conditionNode);
585
 
                                
586
 
                                breakTargets.Push(end);
587
 
                                continueTargets.Push(conditionNode);
588
 
                                
589
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(foreachStatement.EmbeddedStatement, conditionNode);
590
 
                                Connect(bodyEnd, conditionNode);
591
 
                                
592
 
                                breakTargets.Pop();
593
 
                                continueTargets.Pop();
594
 
                                
595
 
                                Connect(conditionNode, end);
596
 
                                builder.nodes.Add(end);
597
 
                                return end;
598
 
                        }
599
 
                        
600
 
                        public override ControlFlowNode VisitBreakStatement(BreakStatement breakStatement, ControlFlowNode data)
601
 
                        {
602
 
                                if (breakTargets.Count > 0)
603
 
                                        Connect(data, breakTargets.Peek(), ControlFlowEdgeType.Jump);
604
 
                                return builder.CreateEndNode(breakStatement);
605
 
                        }
606
 
                        
607
 
                        public override ControlFlowNode VisitContinueStatement(ContinueStatement continueStatement, ControlFlowNode data)
608
 
                        {
609
 
                                if (continueTargets.Count > 0)
610
 
                                        Connect(data, continueTargets.Peek(), ControlFlowEdgeType.Jump);
611
 
                                return builder.CreateEndNode(continueStatement);
612
 
                        }
613
 
                        
614
 
                        public override ControlFlowNode VisitGotoStatement(GotoStatement gotoStatement, ControlFlowNode data)
615
 
                        {
616
 
                                builder.gotoStatements.Add(data);
617
 
                                return builder.CreateEndNode(gotoStatement);
618
 
                        }
619
 
                        
620
 
                        public override ControlFlowNode VisitReturnStatement(ReturnStatement returnStatement, ControlFlowNode data)
621
 
                        {
622
 
                                return builder.CreateEndNode(returnStatement); // end not connected with data
623
 
                        }
624
 
                        
625
 
                        public override ControlFlowNode VisitThrowStatement(ThrowStatement throwStatement, ControlFlowNode data)
626
 
                        {
627
 
                                return builder.CreateEndNode(throwStatement); // end not connected with data
628
 
                        }
629
 
                        
630
 
                        public override ControlFlowNode VisitTryCatchStatement(TryCatchStatement tryCatchStatement, ControlFlowNode data)
631
 
                        {
632
 
                                ControlFlowNode end = builder.CreateEndNode(tryCatchStatement, addToNodeList: false);
633
 
                                var edge = Connect(HandleEmbeddedStatement(tryCatchStatement.TryBlock, data), end);
634
 
                                if (!tryCatchStatement.FinallyBlock.IsNull)
635
 
                                        edge.AddJumpOutOfTryFinally(tryCatchStatement);
636
 
                                foreach (CatchClause cc in tryCatchStatement.CatchClauses) {
637
 
                                        edge = Connect(HandleEmbeddedStatement(cc.Body, data), end);
638
 
                                        if (!tryCatchStatement.FinallyBlock.IsNull)
639
 
                                                edge.AddJumpOutOfTryFinally(tryCatchStatement);
640
 
                                }
641
 
                                if (!tryCatchStatement.FinallyBlock.IsNull) {
642
 
                                        // Don't connect the end of the try-finally block to anything.
643
 
                                        // Consumers of the CFG will have to special-case try-finally.
644
 
                                        HandleEmbeddedStatement(tryCatchStatement.FinallyBlock, data);
645
 
                                }
646
 
                                builder.nodes.Add(end);
647
 
                                return end;
648
 
                        }
649
 
                        
650
 
                        public override ControlFlowNode VisitCheckedStatement(CheckedStatement checkedStatement, ControlFlowNode data)
651
 
                        {
652
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(checkedStatement.Body, data);
653
 
                                return CreateConnectedEndNode(checkedStatement, bodyEnd);
654
 
                        }
655
 
                        
656
 
                        public override ControlFlowNode VisitUncheckedStatement(UncheckedStatement uncheckedStatement, ControlFlowNode data)
657
 
                        {
658
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(uncheckedStatement.Body, data);
659
 
                                return CreateConnectedEndNode(uncheckedStatement, bodyEnd);
660
 
                        }
661
 
                        
662
 
                        public override ControlFlowNode VisitLockStatement(LockStatement lockStatement, ControlFlowNode data)
663
 
                        {
664
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(lockStatement.EmbeddedStatement, data);
665
 
                                return CreateConnectedEndNode(lockStatement, bodyEnd);
666
 
                        }
667
 
                        
668
 
                        public override ControlFlowNode VisitUsingStatement(UsingStatement usingStatement, ControlFlowNode data)
669
 
                        {
670
 
                                data = HandleEmbeddedStatement(usingStatement.ResourceAcquisition as Statement, data);
671
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(usingStatement.EmbeddedStatement, data);
672
 
                                return CreateConnectedEndNode(usingStatement, bodyEnd);
673
 
                        }
674
 
                        
675
 
                        public override ControlFlowNode VisitYieldReturnStatement(YieldReturnStatement yieldStatement, ControlFlowNode data)
676
 
                        {
677
 
                                return CreateConnectedEndNode(yieldStatement, data);
678
 
                        }
679
 
                        
680
 
                        public override ControlFlowNode VisitYieldBreakStatement(YieldBreakStatement yieldBreakStatement, ControlFlowNode data)
681
 
                        {
682
 
                                return builder.CreateEndNode(yieldBreakStatement); // end not connected with data
683
 
                        }
684
 
                        
685
 
                        public override ControlFlowNode VisitUnsafeStatement(UnsafeStatement unsafeStatement, ControlFlowNode data)
686
 
                        {
687
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(unsafeStatement.Body, data);
688
 
                                return CreateConnectedEndNode(unsafeStatement, bodyEnd);
689
 
                        }
690
 
                        
691
 
                        public override ControlFlowNode VisitFixedStatement(FixedStatement fixedStatement, ControlFlowNode data)
692
 
                        {
693
 
                                ControlFlowNode bodyEnd = HandleEmbeddedStatement(fixedStatement.EmbeddedStatement, data);
694
 
                                return CreateConnectedEndNode(fixedStatement, bodyEnd);
695
 
                        }
696
 
                }
697
 
        }
698
 
}