2 // SubroutineWithHandlersBuilder.cs
5 // Alexander Chebaturkin (chebaturkin@gmail.com)
7 // Copyright (C) 2011 Alexander Chebaturkin
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
31 using Mono.CodeContracts.Static.AST;
32 using Mono.CodeContracts.Static.ControlFlow.Blocks;
33 using Mono.CodeContracts.Static.DataStructures;
34 using Mono.CodeContracts.Static.Providers;
36 namespace Mono.CodeContracts.Static.ControlFlow.Subroutines.Builders {
37 class SubroutineWithHandlersBuilder<Label, Handler> : SubroutineBuilder<Label> {
38 private readonly Dictionary<Label, Handler> handler_starting_at = new Dictionary<Label, Handler> ();
39 private readonly Method method;
40 private readonly Dictionary<Label, Queue<Handler>> subroutine_handler_end_list = new Dictionary<Label, Queue<Handler>> ();
41 private readonly Dictionary<Label, Queue<Handler>> try_end_list = new Dictionary<Label, Queue<Handler>> ();
42 private readonly Dictionary<Label, Stack<Handler>> try_start_list = new Dictionary<Label, Stack<Handler>> ();
43 private Sequence<SubroutineWithHandlers<Label, Handler>> subroutine_stack;
45 public SubroutineWithHandlersBuilder (IMethodCodeProvider<Label, Handler> codeProvider,
46 SubroutineFacade subroutineFacade,
49 : base (codeProvider, subroutineFacade, entry)
52 ComputeTryBlockStartAndEndInfo (this.method);
56 private new IMethodCodeProvider<Label, Handler> CodeProvider
58 get { return (IMethodCodeProvider<Label, Handler>) base.CodeProvider; }
61 protected SubroutineWithHandlers<Label, Handler> CurrentSubroutineWithHandlers
63 get { return this.subroutine_stack.Head; }
66 private Sequence<Handler> CurrentProtectingHanlders
68 get { return CurrentSubroutineWithHandlers.CurrentProtectingHandlers; }
69 set { CurrentSubroutineWithHandlers.CurrentProtectingHandlers = value; }
72 public override SubroutineBase<Label> CurrentSubroutine
74 get { return this.subroutine_stack.Head; }
77 private void ComputeTryBlockStartAndEndInfo (Method method)
79 foreach (Handler handler in CodeProvider.GetTryBlocks (method)) {
80 if (CodeProvider.IsFilterHandler (handler))
81 AddTargetLabel (CodeProvider.FilterExpressionStart (handler));
82 AddTargetLabel (CodeProvider.HandlerStart (handler));
83 AddTargetLabel (CodeProvider.HandlerEnd (handler));
84 AddTryStart (handler);
86 AddHandlerEnd (handler);
87 this.handler_starting_at.Add (CodeProvider.HandlerStart (handler), handler);
91 private void AddHandlerEnd (Handler handler)
93 if (!IsFaultOrFinally (handler))
96 Label handlerEnd = CodeProvider.HandlerEnd (handler);
98 this.subroutine_handler_end_list.TryGetValue (handlerEnd, out queue);
100 queue = new Queue<Handler> ();
101 this.subroutine_handler_end_list [handlerEnd] = queue;
103 queue.Enqueue (handler);
104 AddTargetLabel (handlerEnd);
107 private void AddTryEnd (Handler handler)
109 Label tryEnd = CodeProvider.TryEnd (handler);
110 Queue<Handler> queue;
111 this.try_end_list.TryGetValue (tryEnd, out queue);
113 queue = new Queue<Handler> ();
114 this.try_end_list [tryEnd] = queue;
116 queue.Enqueue (handler);
117 AddTargetLabel (tryEnd);
120 private void AddTryStart (Handler handler)
122 Label tryStart = CodeProvider.TryStart (handler);
123 Stack<Handler> stack;
124 this.try_start_list.TryGetValue (tryStart, out stack);
126 stack = new Stack<Handler> ();
127 this.try_start_list [tryStart] = stack;
129 stack.Push (handler);
130 AddTargetLabel (tryStart);
133 public CFGBlock BuildBlocks (Label entry, SubroutineWithHandlers<Label, Handler> subroutine)
135 this.subroutine_stack = Sequence<SubroutineWithHandlers<Label, Handler>>.Cons (subroutine, null);
136 return base.BuildBlocks (entry);
139 public override void RecordInformationSameAsOtherBlock (BlockWithLabels<Label> newBlock, BlockWithLabels<Label> currentBlock)
141 Sequence<Handler> list;
142 if (!CurrentSubroutineWithHandlers.ProtectingHandlers.TryGetValue (currentBlock, out list))
144 CurrentSubroutineWithHandlers.ProtectingHandlers.Add (newBlock, list);
147 public override BlockWithLabels<Label> RecordInformationForNewBlock (Label currentLabel, BlockWithLabels<Label> previousBlock)
149 BlockWithLabels<Label> result = null;
150 Queue<Handler> handlerEnd = GetHandlerEnd (currentLabel);
151 if (handlerEnd != null) {
152 foreach (Handler handler in handlerEnd) {
153 this.subroutine_stack.Head.Commit ();
154 this.subroutine_stack = this.subroutine_stack.Tail;
155 previousBlock = null;
158 Queue<Handler> tryEnd = GetTryEnd (currentLabel);
159 if (tryEnd != null) {
160 foreach (Handler handler in tryEnd) {
161 if (!Equals (handler, CurrentProtectingHanlders.Head))
162 throw new InvalidOperationException ("wrong handler");
163 CurrentProtectingHanlders = CurrentProtectingHanlders.Tail;
167 if (IsHandlerStart (currentLabel, out handler1)) {
168 if (IsFaultOrFinally (handler1)) {
169 SubroutineWithHandlers<Label, Handler> sub = !CodeProvider.IsFaultHandler (handler1)
170 ? new FinallySubroutine<Label, Handler> (this.SubroutineFacade, currentLabel, this)
171 : (FaultFinallySubroutineBase<Label, Handler>) new FaultSubroutine<Label, Handler> (this.SubroutineFacade, currentLabel, this);
172 CurrentSubroutineWithHandlers.FaultFinallySubroutines.Add (handler1, sub);
173 this.subroutine_stack = this.subroutine_stack.Cons (sub);
174 previousBlock = null;
176 result = CurrentSubroutineWithHandlers.CreateCatchFilterHeader (handler1, currentLabel);
179 result = base.RecordInformationForNewBlock (currentLabel, previousBlock);
180 Stack<Handler> tryStart = GetTryStart (currentLabel);
181 if (tryStart != null) {
182 foreach (Handler handler in tryStart)
183 CurrentProtectingHanlders = CurrentProtectingHanlders.Cons (handler);
186 CurrentSubroutineWithHandlers.ProtectingHandlers.Add (result, CurrentProtectingHanlders);
190 private bool IsFaultOrFinally (Handler handler)
192 return CodeProvider.IsFaultHandler (handler) || CodeProvider.IsFinallyHandler (handler);
195 private bool IsHandlerStart (Label currentLabel, out Handler handler)
197 return this.handler_starting_at.TryGetValue (currentLabel, out handler);
200 private Stack<Handler> GetTryStart (Label currentLabel)
202 Stack<Handler> queue;
203 this.try_start_list.TryGetValue (currentLabel, out queue);
207 private Queue<Handler> GetTryEnd (Label currentLabel)
209 Queue<Handler> queue;
210 this.try_end_list.TryGetValue (currentLabel, out queue);
214 private Queue<Handler> GetHandlerEnd (Label currentLabel)
216 Queue<Handler> queue;
217 this.subroutine_handler_end_list.TryGetValue (currentLabel, out queue);