File: Method.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (345 lines) | stat: -rw-r--r-- 7,741 bytes parent folder | download | duplicates (9)
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
// 
// Method.cs
// 
// Authors:
// 	Alexander Chebaturkin (chebaturkin@gmail.com)
// 
// Copyright (C) 2011 Alexander Chebaturkin
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;

namespace Mono.CodeContracts.Static.AST {
	class Method : Member, IEquatable<Method> {
		#region Delegates
		public delegate void MethodContractProvider (Method method);
		#endregion

		private readonly MethodDefinition definition;
		private Block block;
		private MethodContract contract;
		private MethodContractProvider method_contract_provider;
		private List<Parameter> parameters;
		private TypeNode returnType;
		private This thisParameter;

		public Method (MethodDefinition definition)
			: base (NodeType.Method)
		{
			this.definition = definition;
		}

		private Method (MethodDefinition definition, Block block) : base (NodeType.Method)
		{
			this.definition = definition;
			this.block = block;
		}

		public bool HasGenericParameters
		{
			get { return this.definition.HasGenericParameters; }
		}

		public MethodDefinition Definition
		{
			get { return this.definition; }
		}

		public override TypeNode DeclaringType
		{
			get { return TypeNode.Create (this.definition.DeclaringType); }
		}

		public override Module Module
		{
			get { return new Module (this.definition.Module); }
		}

		public Method OverriddenMethod
		{
			get
			{
				if (!this.definition.HasOverrides)
					return null;
				return ParseMethodDefinition (this.definition.Overrides [0].Resolve ());
			}
		}

		public Block Body
		{
			get
			{
				if (this.block == null)
					this.block = ParseMethodBlock (this.definition);
				return this.block;
			}
			set { this.block = value; }
		}

		public MethodContract MethodContract
		{
			get
			{
				if (this.contract == null && ContractProvider != null) {
					MethodContractProvider provider = ContractProvider;
					ContractProvider = null;
					provider (this);
				}
				return this.contract;
			}
			set
			{
				this.contract = value;
				if (value != null)
					this.contract.DeclaringMethod = this;
				ContractProvider = null;
			}
		}

		public MethodContractProvider ContractProvider
		{
			get { return this.method_contract_provider; }
			set
			{
				if (value == null) {
					this.method_contract_provider = null;
					return;
				}

				if (this.method_contract_provider != null)
					this.method_contract_provider += value;
				else
					this.method_contract_provider = value;

				this.contract = null;
			}
		}


		public bool IsFinal
		{
			get { return this.definition.IsFinal; }
		}

		public bool HasBody
		{
			get { return this.definition.HasBody; }
		}

		public override bool IsPrivate
		{
			get { return this.definition.IsPrivate; }
		}

		public override bool IsAssembly
		{
			get { return this.definition.IsAssembly; }
		}

		public override bool IsFamily
		{
			get { return this.definition.IsFamily; }
		}

		public override bool IsFamilyOrAssembly
		{
			get { return this.definition.IsFamilyOrAssembly; }
		}

		public override bool IsFamilyAndAssembly
		{
			get { return this.definition.IsFamilyAndAssembly; }
		}

		public override bool IsPublic
		{
			get { return this.definition.IsPublic; }
		}

		public bool IsProtected
		{
			get { return this.definition.IsFamily; }
		}

		public bool IsProtectedOrInternal
		{
			get { return this.definition.IsFamilyOrAssembly; }
		}

		public bool IsProtectedAndInternal
		{
			get { return this.definition.IsFamilyAndAssembly; }
		}

		public string Name
		{
			get { return this.definition.Name; }
		}

		public string FullName
		{
			get { return this.definition.FullName; }
		}

		public bool HasOverrides
		{
			get { return this.definition.HasOverrides; }
		}

		public bool IsVirtual
		{
			get { return this.definition.IsVirtual; }
		}

		public override bool IsStatic
		{
			get { return this.definition.IsStatic; }
		}

		public bool IsNewSlot
		{
			get { return this.definition.IsNewSlot; }
		}

		public bool IsAbstract
		{
			get { return this.definition.IsAbstract; }
		}

		public bool IsConstructor
		{
			get { return this.definition.IsConstructor; }
		}

		public List<Parameter> Parameters
		{
			get
			{
				if (this.parameters == null)
					this.parameters = this.definition.Parameters.Select (i => new Parameter (i)).ToList ();
				return this.parameters;
			}
			set { this.parameters = value; }
		}

		public bool HasParameters
		{
			get { return Parameters != null && Parameters.Count > 0; }
		}

		public TypeNode ReturnType
		{
			get
			{
				if (this.returnType == null)
					this.returnType = TypeNode.Create (this.definition.ReturnType);
				return this.returnType;
			}
			set { this.returnType = value; }
		}

		public bool IsSetter
		{
			get { return this.definition.IsSetter; }
		}

		public bool IsGetter
		{
			get { return this.definition.IsGetter; }
		}

		public This ThisParameter
		{
			get
			{
				if (this.thisParameter == null && !IsStatic && DeclaringType != null)
					ThisParameter = !DeclaringType.IsValueType ? new This (DeclaringType.SelfInstantiation ()) : new This (DeclaringType.SelfInstantiation ().GetReferenceType ());
				return this.thisParameter;
			}
			private set
			{
				this.thisParameter = value;
				if (value != null)
					this.thisParameter.DeclaringMethod = this;
			}
		}

		public Method DeclaringMethod { get; private set; }

		public List<TypeNode> GenericParameters
		{
			get
			{
				Collection<GenericParameter> genericParameters = this.definition.GenericParameters;
				if (genericParameters == null)
					return null;
				return genericParameters.Select (it => TypeNode.Create (it)).ToList ();
			}
		}

		public IList<Local> Locals
		{
			get
			{
				Collection<VariableDefinition> variables = this.definition.Body.Variables;
				if (variables == null)
					return null;
				return variables.Select (it => new Local (it)).ToList ();
			}
		}

		public bool IsCompilerGenerated
		{
			get { return this.definition.IsCompilerControlled; }
		}

		#region IEquatable<Method> Members
		public bool Equals (Method other)
		{
			return this.definition == other.definition;
		}
		#endregion

		public static Method ParseMethodDefinition (MethodDefinition methodDefinition)
		{
			Block methodBlock = ParseMethodBlock (methodDefinition);

			return new Method (methodDefinition, methodBlock);
		}

		private static Block ParseMethodBlock (MethodDefinition methodDefinition)
		{
			var bp = new BodyParser (methodDefinition);
			return new Block (bp.ParseBlocks ());
		}

		public override string ToString ()
		{
			return string.Format ("Method(Name: {0})", FullName);
		}
	}
}