1 | // |
---|
2 | // MethodBody.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Jb Evain (jbevain@gmail.com) |
---|
6 | // |
---|
7 | // Copyright (c) 2008 - 2011 Jb Evain |
---|
8 | // |
---|
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: |
---|
16 | // |
---|
17 | // The above copyright notice and this permission notice shall be |
---|
18 | // included in all copies or substantial portions of the Software. |
---|
19 | // |
---|
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. |
---|
27 | // |
---|
28 | |
---|
29 | using System; |
---|
30 | |
---|
31 | using Mono.Collections.Generic; |
---|
32 | |
---|
33 | namespace Mono.Cecil.Cil { |
---|
34 | |
---|
35 | public sealed class MethodBody : IVariableDefinitionProvider { |
---|
36 | |
---|
37 | readonly internal MethodDefinition method; |
---|
38 | |
---|
39 | internal ParameterDefinition this_parameter; |
---|
40 | internal int max_stack_size; |
---|
41 | internal int code_size; |
---|
42 | internal bool init_locals; |
---|
43 | internal MetadataToken local_var_token; |
---|
44 | |
---|
45 | internal Collection<Instruction> instructions; |
---|
46 | internal Collection<ExceptionHandler> exceptions; |
---|
47 | internal Collection<VariableDefinition> variables; |
---|
48 | Scope scope; |
---|
49 | |
---|
50 | public MethodDefinition Method { |
---|
51 | get { return method; } |
---|
52 | } |
---|
53 | |
---|
54 | public int MaxStackSize { |
---|
55 | get { return max_stack_size; } |
---|
56 | set { max_stack_size = value; } |
---|
57 | } |
---|
58 | |
---|
59 | public int CodeSize { |
---|
60 | get { return code_size; } |
---|
61 | } |
---|
62 | |
---|
63 | public bool InitLocals { |
---|
64 | get { return init_locals; } |
---|
65 | set { init_locals = value; } |
---|
66 | } |
---|
67 | |
---|
68 | public MetadataToken LocalVarToken { |
---|
69 | get { return local_var_token; } |
---|
70 | set { local_var_token = value; } |
---|
71 | } |
---|
72 | |
---|
73 | public Collection<Instruction> Instructions { |
---|
74 | get { return instructions ?? (instructions = new InstructionCollection ()); } |
---|
75 | } |
---|
76 | |
---|
77 | public bool HasExceptionHandlers { |
---|
78 | get { return !exceptions.IsNullOrEmpty (); } |
---|
79 | } |
---|
80 | |
---|
81 | public Collection<ExceptionHandler> ExceptionHandlers { |
---|
82 | get { return exceptions ?? (exceptions = new Collection<ExceptionHandler> ()); } |
---|
83 | } |
---|
84 | |
---|
85 | public bool HasVariables { |
---|
86 | get { return !variables.IsNullOrEmpty (); } |
---|
87 | } |
---|
88 | |
---|
89 | public Collection<VariableDefinition> Variables { |
---|
90 | get { return variables ?? (variables = new VariableDefinitionCollection ()); } |
---|
91 | } |
---|
92 | |
---|
93 | public Scope Scope { |
---|
94 | get { return scope; } |
---|
95 | set { scope = value; } |
---|
96 | } |
---|
97 | |
---|
98 | public ParameterDefinition ThisParameter { |
---|
99 | get { |
---|
100 | if (method == null || method.DeclaringType == null) |
---|
101 | throw new NotSupportedException (); |
---|
102 | |
---|
103 | return this_parameter ?? (this_parameter = new ParameterDefinition ("0", ParameterAttributes.None, method.DeclaringType)); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | public MethodBody (MethodDefinition method) |
---|
108 | { |
---|
109 | this.method = method; |
---|
110 | } |
---|
111 | |
---|
112 | public ILProcessor GetILProcessor () |
---|
113 | { |
---|
114 | return new ILProcessor (this); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | public interface IVariableDefinitionProvider { |
---|
119 | bool HasVariables { get; } |
---|
120 | Collection<VariableDefinition> Variables { get; } |
---|
121 | } |
---|
122 | |
---|
123 | class VariableDefinitionCollection : Collection<VariableDefinition> { |
---|
124 | |
---|
125 | internal VariableDefinitionCollection () |
---|
126 | { |
---|
127 | } |
---|
128 | |
---|
129 | internal VariableDefinitionCollection (int capacity) |
---|
130 | : base (capacity) |
---|
131 | { |
---|
132 | } |
---|
133 | |
---|
134 | protected override void OnAdd (VariableDefinition item, int index) |
---|
135 | { |
---|
136 | item.index = index; |
---|
137 | } |
---|
138 | |
---|
139 | protected override void OnInsert (VariableDefinition item, int index) |
---|
140 | { |
---|
141 | item.index = index; |
---|
142 | |
---|
143 | for (int i = index; i < size; i++) |
---|
144 | items [i].index = i + 1; |
---|
145 | } |
---|
146 | |
---|
147 | protected override void OnSet (VariableDefinition item, int index) |
---|
148 | { |
---|
149 | item.index = index; |
---|
150 | } |
---|
151 | |
---|
152 | protected override void OnRemove (VariableDefinition item, int index) |
---|
153 | { |
---|
154 | item.index = -1; |
---|
155 | |
---|
156 | for (int i = index + 1; i < size; i++) |
---|
157 | items [i].index = i - 1; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | class InstructionCollection : Collection<Instruction> { |
---|
162 | |
---|
163 | internal InstructionCollection () |
---|
164 | { |
---|
165 | } |
---|
166 | |
---|
167 | internal InstructionCollection (int capacity) |
---|
168 | : base (capacity) |
---|
169 | { |
---|
170 | } |
---|
171 | |
---|
172 | protected override void OnAdd (Instruction item, int index) |
---|
173 | { |
---|
174 | if (index == 0) |
---|
175 | return; |
---|
176 | |
---|
177 | var previous = items [index - 1]; |
---|
178 | previous.next = item; |
---|
179 | item.previous = previous; |
---|
180 | } |
---|
181 | |
---|
182 | protected override void OnInsert (Instruction item, int index) |
---|
183 | { |
---|
184 | if (size == 0) |
---|
185 | return; |
---|
186 | |
---|
187 | var current = items [index]; |
---|
188 | if (current == null) { |
---|
189 | var last = items [index - 1]; |
---|
190 | last.next = item; |
---|
191 | item.previous = last; |
---|
192 | return; |
---|
193 | } |
---|
194 | |
---|
195 | var previous = current.previous; |
---|
196 | if (previous != null) { |
---|
197 | previous.next = item; |
---|
198 | item.previous = previous; |
---|
199 | } |
---|
200 | |
---|
201 | current.previous = item; |
---|
202 | item.next = current; |
---|
203 | } |
---|
204 | |
---|
205 | protected override void OnSet (Instruction item, int index) |
---|
206 | { |
---|
207 | var current = items [index]; |
---|
208 | |
---|
209 | item.previous = current.previous; |
---|
210 | item.next = current.next; |
---|
211 | |
---|
212 | current.previous = null; |
---|
213 | current.next = null; |
---|
214 | } |
---|
215 | |
---|
216 | protected override void OnRemove (Instruction item, int index) |
---|
217 | { |
---|
218 | var previous = item.previous; |
---|
219 | if (previous != null) |
---|
220 | previous.next = item.next; |
---|
221 | |
---|
222 | var next = item.next; |
---|
223 | if (next != null) |
---|
224 | next.previous = item.previous; |
---|
225 | |
---|
226 | item.previous = null; |
---|
227 | item.next = null; |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|