Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NR5CompatibiltyExtensions.cs « MonoDevelop.Ide.TypeSystem « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de720d862a8d245e2f5f2228243005b1fb164840 (plain)
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
//
// NR5CompatibiltyExtensions.cs
//
// Author:
//       Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2015 Xamarin Inc. (http://xamarin.com)
//
// 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 System.Reflection;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Shared.Extensions;
using MonoDevelop.Core;
using Microsoft.CodeAnalysis.Shared.Utilities;

namespace MonoDevelop.Ide.TypeSystem
{
	public static class NR5CompatibiltyExtensions
	{
		/// <summary>
		/// Gets the full name of the metadata.
		/// In case symbol is not INamedTypeSymbol it returns raw MetadataName
		/// Example: Generic type returns T1, T2...
		/// </summary>
		/// <returns>The full metadata name.</returns>
		/// <param name="symbol">Symbol.</param>
		public static string GetFullMetadataName (this ITypeSymbol symbol)
		{
			//This is for comaptibility with NR5 reflection name in case of generic types like T1, T2...
			var namedTypeSymbol = symbol as INamedTypeSymbol;
			return namedTypeSymbol != null ? GetFullMetadataName (namedTypeSymbol) : symbol.MetadataName;
		}

		/// <summary>
		/// Gets the full MetadataName(ReflectionName in NR5).
		/// Example: Namespace1.Namespace2.Classs1+NestedClassWithTwoGenericTypes`2+NestedClassWithoutGenerics
		/// </summary>
		/// <returns>The full metadata name.</returns>
		/// <param name="symbol">Symbol.</param>
		public static string GetFullMetadataName (this INamedTypeSymbol symbol)
		{
			var fullName = new StringBuilder (symbol.MetadataName);
			var parentType = symbol.ContainingType;
			while (parentType != null) {
				fullName.Insert (0, '+');
				fullName.Insert (0, parentType.MetadataName);
				parentType = parentType.ContainingType;
			}
			var ns = symbol.ContainingNamespace;
			while (ns != null && !ns.IsGlobalNamespace) {
				fullName.Insert (0, '.');
				fullName.Insert (0, ns.MetadataName);
				ns = ns.ContainingNamespace;
			}
			return fullName.ToString ();
		}

		/// <summary>
		/// Determines if derived from baseType. Includes itself and all base classes, but does not include interfaces.
		/// </summary>
		/// <returns><c>true</c> if is derived from class the specified type baseType; otherwise, <c>false</c>.</returns>
		/// <param name="type">Type.</param>
		/// <param name="baseType">Base type.</param>
		public static bool IsDerivedFromClass (this INamedTypeSymbol type, INamedTypeSymbol baseType)
		{
			//NR5 is returning true also for same type
			for (; type != null; type = type.BaseType) {
				if (type == baseType) {
					return true;
				}
			}
			return false;
		}

		public static IEnumerable<INamedTypeSymbol> GetAllTypes (this INamespaceSymbol namespaceSymbol)
		{
			return namespaceSymbol.GetAllTypes (CancellationToken.None);
		}

		public static IEnumerable<ITypeSymbol> GetBaseTypesAndThis (this ITypeSymbol type)
		{
			var current = type;
			while (current != null) {
				yield return current;
				current = current.BaseType;
			}
		}

		public static ITypeSymbol GetReturnType (this ISymbol symbol)
		{
			if (symbol == null)
				throw new ArgumentNullException (nameof (symbol));
			switch (symbol.Kind) {
			case SymbolKind.Field:
				var field = (IFieldSymbol)symbol;
				return field.Type;
			case SymbolKind.Method:
				var method = (IMethodSymbol)symbol;
				if (method.MethodKind == MethodKind.Constructor)
					return method.ContainingType;
				return method.ReturnType;
			case SymbolKind.Property:
				var property = (IPropertySymbol)symbol;
				return property.Type;
			case SymbolKind.Event:
				var evt = (IEventSymbol)symbol;
				return evt.Type;
			case SymbolKind.Parameter:
				var param = (IParameterSymbol)symbol;
				return param.Type;
			case SymbolKind.Local:
				var local = (ILocalSymbol)symbol;
				return local.Type;
			}
			return null;
		}


		/// <summary>
		/// Gets the full name of the namespace.
		/// </summary>
		public static string GetFullName (this INamespaceSymbol ns)
		{
			return ns.ToDisplayString (SymbolDisplayFormat.CSharpErrorMessageFormat);
		}

		/// <summary>
		/// Gets the full name. The full name is no 1:1 representation of a type it's missing generics and it has a poor
		/// representation for inner types (just dot separated).
		/// DO NOT use this method unless you're know what you do. It's only implemented for legacy code.
		/// </summary>
		public static string GetFullName (this ITypeSymbol type)
		{
			return type.ToDisplayString (SymbolDisplayFormat.CSharpErrorMessageFormat);
		}

		public static IEnumerable<INamedTypeSymbol> GetAllTypesInMainAssembly (this Compilation compilation, CancellationToken cancellationToken = default (CancellationToken))
		{
			if (compilation == null)
				throw new ArgumentNullException (nameof (compilation));
			return compilation.Assembly.GlobalNamespace.GetAllTypes (cancellationToken);
		}

		public static IEnumerable<T> GetAccessibleMembersInThisAndBaseTypes<T> (this ITypeSymbol containingType, ISymbol within) where T : class, ISymbol
		{
			if (containingType == null)
				return Enumerable.Empty<T> ();

			var types = containingType.GetBaseTypesAndThis ();
			return types.SelectMany (x => x.GetMembers ().OfType<T> ().Where (m => m.IsAccessibleWithin (within)));
		}

		/// <summary>
		/// Gets all base classes.
		/// </summary>
		/// <returns>The all base classes.</returns>
		/// <param name="type">Type.</param>
		public static IEnumerable<INamedTypeSymbol> GetAllBaseClasses (this INamedTypeSymbol type, bool includeSuperType = false)
		{
			if (!includeSuperType)
				type = type.BaseType;
			while (type != null) {
				yield return type;
				type = type.BaseType;
			}
		}

		public static bool IsDefinedInMetadata (this ISymbol symbol)
		{
			return symbol.Locations.Any (loc => loc.IsInMetadata);
		}

		public static bool IsDefinedInSource (this ISymbol symbol)
		{
			return symbol.Locations.All (loc => loc.IsInSource);
		}

		//public static DeclarationModifiers GetSymbolModifiers(this ISymbol symbol)
		//{
		//	// ported from roslyn source - why they didn't use DeclarationModifiers.From (symbol) ?
		//	return DeclarationModifiers.None
		//		                           .WithIsStatic (symbol.IsStatic)
		//		                           .WithIsAbstract (symbol.IsAbstract)
		//		                           .WithIsUnsafe (symbol.IsUnsafe ())
		//		                           .WithIsVirtual (symbol.IsVirtual)
		//		                           .WithIsOverride (symbol.IsOverride)
		//		                           .WithIsSealed (symbol.IsSealed);
		//}

		public static IEnumerable<SyntaxReference> GetDeclarations (this ISymbol symbol)
		{
			return symbol != null
				? symbol.DeclaringSyntaxReferences.AsEnumerable ()
					: Enumerable.Empty<SyntaxReference> ();
		}

		public static bool IsSameAssemblyOrHasFriendAccessTo (this IAssemblySymbol assembly, IAssemblySymbol toAssembly)
		{
			return
				Equals (assembly, toAssembly) ||
				(assembly.IsInteractive && toAssembly.IsInteractive) ||
				toAssembly.GivesAccessTo (assembly);
		}

		/// <summary>
		/// Returns the component category.
		/// [System.ComponentModel.CategoryAttribute (CATEGORY)]
		/// </summary>
		/// <param name="symbol">Symbol.</param>
		public static string GetComponentCategory (this ISymbol symbol)
		{
			if (symbol == null)
				throw new ArgumentNullException (nameof (symbol));
			var browsableState = symbol.GetAttributes ().FirstOrDefault (attr => attr.AttributeClass.Name == "CategoryAttribute" && attr.AttributeClass.ContainingNamespace.MetadataName == "System.ComponentModel");
			if (browsableState != null && browsableState.ConstructorArguments.Length == 1) {
				try {
					return (string)browsableState.ConstructorArguments [0].Value;
				} catch {
				}
			}
			return null;
		}


		/// <summary>
		/// Returns true if the type is public and was tagged with
		/// [System.ComponentModel.ToolboxItem (true)]
		/// </summary>
		/// <returns><c>true</c> if is designer browsable the specified symbol; otherwise, <c>false</c>.</returns>
		/// <param name="symbol">Symbol.</param>
		public static bool IsToolboxItem (this ITypeSymbol symbol)
		{
			if (symbol == null)
				throw new ArgumentNullException (nameof (symbol));
			if (symbol.DeclaredAccessibility != Accessibility.Public)
				return false;
			var toolboxItemAttr = symbol.GetAttributes ().FirstOrDefault (attr => attr.AttributeClass.Name == "ToolboxItemAttribute" && attr.AttributeClass.ContainingNamespace.MetadataName == "System.ComponentModel");
			if (toolboxItemAttr != null && toolboxItemAttr.ConstructorArguments.Length == 1) {
				try {
					return (bool)toolboxItemAttr.ConstructorArguments [0].Value;
				} catch {
				}
			}
			return false;
		}

		/// <summary>
		/// Returns true if the symbol wasn't tagged with
		/// [System.ComponentModel.BrowsableAttribute (false)]
		/// </summary>
		/// <returns><c>true</c> if is designer browsable the specified symbol; otherwise, <c>false</c>.</returns>
		/// <param name="symbol">Symbol.</param>
		public static bool IsDesignerBrowsable (this ISymbol symbol)
		{
			if (symbol == null)
				throw new ArgumentNullException (nameof (symbol));
			var browsableState = symbol.GetAttributes ().FirstOrDefault (attr => attr.AttributeClass.Name == "BrowsableAttribute" && attr.AttributeClass.ContainingNamespace.MetadataName == "System.ComponentModel");
			if (browsableState != null && browsableState.ConstructorArguments.Length == 1) {
				try {
					return (bool)browsableState.ConstructorArguments [0].Value;
				} catch {
				}
			}
			return true;
		}

		public static bool IsAccessibleWithinMD (this ISymbol symbol, ISymbol within, ITypeSymbol throughTypeOpt = null)
		{
			return symbol.IsAccessibleWithin (within, throughTypeOpt);
		}

		public static IEnumerable<INamedTypeSymbol> GetBaseTypesMD (this ITypeSymbol symbol)
		{
			foreach (var item in symbol.GetBaseTypes ())
				yield return item;
		}

		public static TSymbol GetEnclosingSymbolMD<TSymbol> (this SemanticModel semanticModel, int position, CancellationToken cancellationToken)
			where TSymbol : ISymbol
		{
			for (var symbol = semanticModel.GetEnclosingSymbol (position, cancellationToken);
				 symbol != null;
				 symbol = symbol.ContainingSymbol) {
				if (symbol is TSymbol) {
					return (TSymbol)symbol;
				}
			}

			return default (TSymbol);
		}

		public static IEnumerable<INamedTypeSymbol> GetAllTypesMD (this INamespaceSymbol namespaceSymbol, CancellationToken cancellationToken = default (CancellationToken))
		{
			foreach (var item in namespaceSymbol.GetAllTypes (cancellationToken))
				yield return item;
		}
	}

	public static class SignatureComparerMD
	{
		public static bool HaveSameSignature (ISymbol symbol1, ISymbol symbol2, bool caseSensitive)
		{
			return SignatureComparer.Instance.HaveSameSignature (symbol1, symbol2, caseSensitive);
		}
	}
}