File: HelperMethods.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 (332 lines) | stat: -rw-r--r-- 10,717 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
// 
// HelperMethods.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 Mono.CodeContracts.Static.AST;

namespace Mono.CodeContracts.Static.ContractExtraction {
	static class HelperMethods {
		public static Method IsMethodCall (Statement s)
		{
			if (s == null)
				return null;

			var expressionStatement = s as ExpressionStatement;
			if (expressionStatement == null)
				return null;

			var methodCall = expressionStatement.Expression as MethodCall;
			if (methodCall == null)
				return null;

			var binding = methodCall.Callee as MemberBinding;
			if (binding == null)
				return null;

			return binding.BoundMember as Method;
		}

		public static Local ExtractPreamble (Method method, ContractNodes contractNodes, Block contractInitializer, out Block postPreamble)
		{
			postPreamble = null;
			return null;
		}

		public static List<Statement> ExtractContractBlocks (List<Statement> blocks, int firstBlockIndex, int firstStmtIndex, int lastBlockIndex, int lastStmtIndex)
		{
			var result = new List<Statement> ();
			var firstBlock = (Block) blocks [firstBlockIndex];
			var block = new Block (new List<Statement> ());
			if (firstBlock != null) {
				int cnt = firstBlockIndex == lastBlockIndex ? lastStmtIndex + 1 : firstBlock.Statements.Count;
				for (int i = firstStmtIndex; i < cnt; i++) {
					Statement stmt = firstBlock.Statements [i];
					block.Statements.Add (stmt);
					if (stmt != null)
						firstBlock.Statements [i] = null;
				}
			}
			result.Add (block);
			int nextIndex = firstBlockIndex + 1;
			if (nextIndex > lastBlockIndex)
				return result;
			Block newLastBlock = null;
			int lastFullBlockIndex = lastBlockIndex - 1;
			var lastBlock = (Block) blocks [lastBlockIndex];
			if (lastBlock != null && lastStmtIndex == lastBlock.Statements.Count - 1)
				lastFullBlockIndex = lastBlockIndex;
			else {
				newLastBlock = new Block (new List<Statement> ());
				if (block.Statements != null && block.Statements.Count > 0) {
					var branch = block.Statements [block.Statements.Count - 1] as Branch;
					if (branch != null && branch.Target != null && branch.Target == lastBlock)
						branch.Target = newLastBlock;
				}
			}

			for (; nextIndex < lastFullBlockIndex; ++nextIndex) {
				var curBlock = (Block) blocks [nextIndex];
				result.Add (curBlock);
				if (curBlock != null) {
					blocks [nextIndex] = null;
					if (newLastBlock != null && curBlock.Statements != null && curBlock.Statements.Count > 0) {
						var branch = curBlock.Statements [curBlock.Statements.Count - 1] as Branch;
						if (branch != null && branch.Target != null && branch.Target == lastBlock)
							branch.Target = newLastBlock;
					}
				}
			}

			if (newLastBlock != null) {
				for (int i = 0; i < lastStmtIndex + 1; i++) {
					newLastBlock.Statements.Add (lastBlock.Statements [i]);
					lastBlock.Statements [i] = null;
				}

				result.Add (newLastBlock);
			}
			return result;
		}

		public static bool IsCompilerGenerated (TypeNode type)
		{
			throw new NotImplementedException ();
		}

		public static int FindNextRealStatement (List<Statement> stmts, int beginIndex)
		{
			if (stmts == null || stmts.Count <= beginIndex)
				return -1;
			int index = beginIndex;
			while (index < stmts.Count && (stmts [index] == null || stmts [index].NodeType == NodeType.Nop))
				++index;
			return index;
		}

		public static bool IsReferenceAsVisibleAs (Member member, Member asThisMember)
		{
			var type = member as TypeNode;
			if (type != null)
				return IsTypeAsVisibleAs (type, asThisMember);
			var method = member as Method;
			Member member1;
			if (method != null) {
				if (method.HasGenericParameters)
					throw new NotImplementedException ();
				member1 = method;
			} else
				member1 = Unspecialize (member);

			return IsDefinitionAsVisibleAs (member1, asThisMember);
		}

		private static bool IsDefinitionAsVisibleAs (this Member member, Member asThisMember)
		{
			Module memberModule = member.Module;
			Module asThisMemberModule = asThisMember.Module;

			for (Member mbr = member; mbr != null; mbr = mbr.DeclaringType) {
				if (!mbr.IsPublic) {
					bool visible = false;
					for (Member mbr1 = asThisMember; mbr1 != null; mbr1 = mbr1.DeclaringType) {
						if (mbr1.IsAssembly) {
							if ((mbr1.IsPrivate || mbr1.IsAssembly) && memberModule == asThisMemberModule)
								visible = true;
						} else if (mbr1.IsFamily) {
							if (mbr.IsPrivate) {
								if (IsInsideOf (mbr, mbr1) || IsInsideSubclass (mbr, mbr1.DeclaringType))
									visible = true;
							} else if (mbr.IsFamily && (mbr.DeclaringType == mbr1.DeclaringType || IsSubclassOf (mbr.DeclaringType, mbr1.DeclaringType)))
								visible = true;
						} else if (mbr1.IsFamilyOrAssembly) {
							if (mbr.IsPrivate) {
								if (memberModule == asThisMemberModule || IsInsideSubclass (mbr, mbr1.DeclaringType))
									visible = true;
							} else if (mbr.IsAssembly) {
								if (memberModule == asThisMemberModule)
									visible = true;
							} else if (mbr.IsFamily) {
								if (IsSubclassOf (mbr.DeclaringType, mbr1.DeclaringType))
									visible = true;
							} else if (mbr.IsFamilyOrAssembly && memberModule == asThisMemberModule && IsSubclassOf (mbr.DeclaringType, mbr1.DeclaringType))
								visible = true;
						} else if (mbr1.IsPrivate && mbr.IsPrivate && IsInsideOf (mbr, mbr1.DeclaringType))
							visible = true;
					}
					if (!visible)
						return false;
				}
			}
			return true;
		}

		private static bool IsSubclassOf (this TypeNode thisType, TypeNode thatType)
		{
			if (thatType == null)
				return false;
			return thisType.IsAssignableTo (thatType);
		}

		private static bool IsInsideSubclass (this Member member, Member thatValue)
		{
			var targetType = thatValue as TypeNode;
			if (targetType == null)
				return false;

			for (TypeNode declaringType = member.DeclaringType; declaringType != null; declaringType = declaringType.DeclaringType) {
				if (declaringType.IsAssignableTo (targetType))
					return true;
			}
			return false;
		}

		private static bool IsInsideOf (this Member thisValue, Member thatValue)
		{
			var typeNode = thatValue as TypeNode;
			if (typeNode == null)
				return false;
			for (TypeNode declaringType = thisValue.DeclaringType; declaringType != null; declaringType = declaringType.DeclaringType) {
				if (declaringType == typeNode)
					return true;
			}
			return false;
		}

		private static Member Unspecialize (Member member)
		{
			return member;
		}

		private static bool IsTypeAsVisibleAs (this TypeNode type, Member asThisMember)
		{
			if (type == null)
				return true;

			switch (type.NodeType) {
			case NodeType.Reference:
				return ((Reference) type).ElementType.IsTypeAsVisibleAs (asThisMember);
			default:
				if (type.HasGenericParameters)
					throw new NotImplementedException ();

				return IsDefinitionAsVisibleAs (type, asThisMember);
			}
		}

		public static bool IsVisibleFrom (this TypeNode type, TypeNode from)
		{
			TypeNode declaringType = type.DeclaringType;
			if (declaringType != null) {
				if (IsContainedIn (from, declaringType) || IsInheritedFrom (from, type))
					return true;
				if (type.IsNestedFamily)
					return IsInheritedFrom (from, declaringType);
				if (type.IsNestedPublic)
					return IsVisibleFrom (declaringType, from);
				if (type.IsNestedInternal) {
					if (IsInheritedFrom (from, declaringType))
						return true;
					if (declaringType.Module == from.Module)
						return IsVisibleFrom (declaringType, from);

					return false;
				}
				if (type.IsNestedFamilyAndAssembly)
					return from.Module == declaringType.Module && IsInheritedFrom (from, declaringType);
				if ((type.IsAssembly || type.IsNestedAssembly) && declaringType.Module == from.Module)
					return IsVisibleFrom (declaringType, from);

				return false;
			}

			return type.Module == from.Module || type.IsPublic;
		}

		public static bool IsVisibleFrom (this Member member, TypeNode from)
		{
			var type = member as TypeNode;
			if (type != null)
				return type.IsVisibleFrom (from);

			TypeNode declaringType = member.DeclaringType;
			if (from.IsContainedIn (declaringType))
				return true;
			if (member.IsPublic)
				return declaringType.IsVisibleFrom (from);
			if (member.IsFamily)
				return from.IsInheritedFrom (declaringType);
			if (member.IsFamilyAndAssembly)
				return from.Module == declaringType.Module && from.IsInheritedFrom (declaringType);
			if (member.IsFamilyOrAssembly) {
				if (from.IsInheritedFrom (declaringType))
					return true;
				if (from.Module == declaringType.Module)
					return declaringType.IsVisibleFrom (from);

				return false;
			}

			return member.IsAssembly && declaringType.Module == from.Module && declaringType.IsVisibleFrom (from);
		}

		public static bool IsInheritedFrom (this TypeNode type, TypeNode from)
		{
			TypeNode baseClass;
			if (type.HasBaseClass (out baseClass)) {
				if (baseClass == from || baseClass.IsInheritedFrom (from))
					return true;
			}

			return false;
		}

		private static bool HasBaseClass (this TypeNode type, out TypeNode baseClass)
		{
			var clazz = type as Class;
			if (clazz != null && clazz.BaseType != null) {
				baseClass = clazz.BaseType;
				return true;
			}

			baseClass = default(TypeNode);
			return false;
		}

		public static bool IsContainedIn (this TypeNode inner, TypeNode outer)
		{
			if (inner == outer)
				return true;

			if (inner.DeclaringType != null)
				return inner.DeclaringType.IsContainedIn (outer);

			return false;
		}
	}
}