~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/TypeSystem/Implementation/GetMembersHelper.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
ļ»æ// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
2
 
// 
3
 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
 
// software and associated documentation files (the "Software"), to deal in the Software
5
 
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
 
// to whom the Software is furnished to do so, subject to the following conditions:
8
 
// 
9
 
// The above copyright notice and this permission notice shall be included in all copies or
10
 
// substantial portions of the Software.
11
 
// 
12
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
 
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
 
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
 
// DEALINGS IN THE SOFTWARE.
18
 
 
19
 
using System;
20
 
using System.Collections.Generic;
21
 
using System.Linq;
22
 
using ICSharpCode.NRefactory.Utils;
23
 
 
24
 
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
25
 
{
26
 
        /// <summary>
27
 
        /// Provides helper methods for implementing GetMembers() on IType-implementations.
28
 
        /// Note: GetMembersHelper will recursively call back into IType.GetMembers(), but only with
29
 
        /// both GetMemberOptions.IgnoreInheritedMembers and GetMemberOptions.ReturnMemberDefinitions set,
30
 
        /// and only the 'simple' overloads (not taking type arguments).
31
 
        /// 
32
 
        /// Ensure that your IType implementation does not use the GetMembersHelper if both flags are set,
33
 
        /// otherwise you'll get a StackOverflowException!
34
 
        /// </summary>
35
 
        static class GetMembersHelper
36
 
        {
37
 
                #region GetNestedTypes
38
 
                public static IEnumerable<IType> GetNestedTypes(IType type, Predicate<ITypeDefinition> filter, GetMemberOptions options)
39
 
                {
40
 
                        return GetNestedTypes(type, null, filter, options);
41
 
                }
42
 
                
43
 
                public static IEnumerable<IType> GetNestedTypes(IType type, IList<IType> nestedTypeArguments, Predicate<ITypeDefinition> filter, GetMemberOptions options)
44
 
                {
45
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
46
 
                                return GetNestedTypesImpl(type, nestedTypeArguments, filter, options);
47
 
                        } else {
48
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetNestedTypesImpl(t, nestedTypeArguments, filter, options));
49
 
                        }
50
 
                }
51
 
                
52
 
                static IEnumerable<IType> GetNestedTypesImpl(IType outerType, IList<IType> nestedTypeArguments, Predicate<ITypeDefinition> filter, GetMemberOptions options)
53
 
                {
54
 
                        ITypeDefinition outerTypeDef = outerType.GetDefinition();
55
 
                        if (outerTypeDef == null)
56
 
                                yield break;
57
 
                        
58
 
                        int outerTypeParameterCount = outerTypeDef.TypeParameterCount;
59
 
                        ParameterizedType pt = outerType as ParameterizedType;
60
 
                        foreach (ITypeDefinition nestedType in outerTypeDef.NestedTypes) {
61
 
                                int totalTypeParameterCount = nestedType.TypeParameterCount;
62
 
                                if (nestedTypeArguments != null) {
63
 
                                        if (totalTypeParameterCount - outerTypeParameterCount != nestedTypeArguments.Count)
64
 
                                                continue;
65
 
                                }
66
 
                                if (!(filter == null || filter(nestedType)))
67
 
                                        continue;
68
 
                                
69
 
                                if (totalTypeParameterCount == 0 || (options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
70
 
                                        yield return nestedType;
71
 
                                } else {
72
 
                                        // We need to parameterize the nested type
73
 
                                        IType[] newTypeArguments = new IType[totalTypeParameterCount];
74
 
                                        for (int i = 0; i < outerTypeParameterCount; i++) {
75
 
                                                newTypeArguments[i] = pt != null ? pt.GetTypeArgument(i) : outerTypeDef.TypeParameters[i];
76
 
                                        }
77
 
                                        for (int i = outerTypeParameterCount; i < totalTypeParameterCount; i++) {
78
 
                                                if (nestedTypeArguments != null)
79
 
                                                        newTypeArguments[i] = nestedTypeArguments[i - outerTypeParameterCount];
80
 
                                                else
81
 
                                                        newTypeArguments[i] = SpecialType.UnboundTypeArgument;
82
 
                                        }
83
 
                                        yield return new ParameterizedType(nestedType, newTypeArguments);
84
 
                                }
85
 
                        }
86
 
                }
87
 
                #endregion
88
 
                
89
 
                #region GetMethods
90
 
                public static IEnumerable<IMethod> GetMethods(IType type, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
91
 
                {
92
 
                        return GetMethods(type, null, filter, options);
93
 
                }
94
 
                
95
 
                public static IEnumerable<IMethod> GetMethods(IType type, IList<IType> typeArguments, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
96
 
                {
97
 
                        if (typeArguments != null && typeArguments.Count > 0) {
98
 
                                filter = FilterTypeParameterCount(typeArguments.Count).And(filter);
99
 
                        }
100
 
                        
101
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
102
 
                                return GetMethodsImpl(type, typeArguments, filter, options);
103
 
                        } else {
104
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetMethodsImpl(t, typeArguments, filter, options));
105
 
                        }
106
 
                }
107
 
                
108
 
                static Predicate<IUnresolvedMethod> FilterTypeParameterCount(int expectedTypeParameterCount)
109
 
                {
110
 
                        return m => m.TypeParameters.Count == expectedTypeParameterCount;
111
 
                }
112
 
                
113
 
                const GetMemberOptions declaredMembers = GetMemberOptions.IgnoreInheritedMembers | GetMemberOptions.ReturnMemberDefinitions;
114
 
                
115
 
                static IEnumerable<IMethod> GetMethodsImpl(IType baseType, IList<IType> methodTypeArguments, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
116
 
                {
117
 
                        IEnumerable<IMethod> declaredMethods = baseType.GetMethods(filter, options | declaredMembers);
118
 
                        
119
 
                        ParameterizedType pt = baseType as ParameterizedType;
120
 
                        if ((options & GetMemberOptions.ReturnMemberDefinitions) == 0
121
 
                            && (pt != null || (methodTypeArguments != null && methodTypeArguments.Count > 0)))
122
 
                        {
123
 
                                TypeParameterSubstitution substitution = null;
124
 
                                foreach (IMethod m in declaredMethods) {
125
 
                                        if (methodTypeArguments != null && methodTypeArguments.Count > 0) {
126
 
                                                if (m.TypeParameters.Count != methodTypeArguments.Count)
127
 
                                                        continue;
128
 
                                        }
129
 
                                        if (substitution == null) {
130
 
                                                if (pt != null)
131
 
                                                        substitution = pt.GetSubstitution(methodTypeArguments);
132
 
                                                else
133
 
                                                        substitution = new TypeParameterSubstitution(null, methodTypeArguments);
134
 
                                        }
135
 
                                        yield return new SpecializedMethod(m, substitution);
136
 
                                }
137
 
                        } else {
138
 
                                foreach (IMethod m in declaredMethods) {
139
 
                                        yield return m;
140
 
                                }
141
 
                        }
142
 
                }
143
 
                #endregion
144
 
                
145
 
                #region GetConstructors
146
 
                public static IEnumerable<IMethod> GetConstructors(IType type, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
147
 
                {
148
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
149
 
                                return GetConstructorsImpl(type, filter, options);
150
 
                        } else {
151
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetConstructorsImpl(t, filter, options));
152
 
                        }
153
 
                }
154
 
                
155
 
                static IEnumerable<IMethod> GetConstructorsImpl(IType baseType, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
156
 
                {
157
 
                        IEnumerable<IMethod> declaredCtors = baseType.GetConstructors(filter, options | declaredMembers);
158
 
                        if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
159
 
                                return declaredCtors;
160
 
                        }
161
 
                        
162
 
                        ParameterizedType pt = baseType as ParameterizedType;
163
 
                        if (pt != null) {
164
 
                                var substitution = pt.GetSubstitution();
165
 
                                return declaredCtors.Select(m => new SpecializedMethod(m, substitution) { DeclaringType = pt });
166
 
                        } else {
167
 
                                return declaredCtors;
168
 
                        }
169
 
                }
170
 
                #endregion
171
 
                
172
 
                #region GetProperties
173
 
                public static IEnumerable<IProperty> GetProperties(IType type, Predicate<IUnresolvedProperty> filter, GetMemberOptions options)
174
 
                {
175
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
176
 
                                return GetPropertiesImpl(type, filter, options);
177
 
                        } else {
178
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetPropertiesImpl(t, filter, options));
179
 
                        }
180
 
                }
181
 
                
182
 
                static IEnumerable<IProperty> GetPropertiesImpl(IType baseType, Predicate<IUnresolvedProperty> filter, GetMemberOptions options)
183
 
                {
184
 
                        IEnumerable<IProperty> declaredProperties = baseType.GetProperties(filter, options | declaredMembers);
185
 
                        if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
186
 
                                return declaredProperties;
187
 
                        }
188
 
                        
189
 
                        ParameterizedType pt = baseType as ParameterizedType;
190
 
                        if (pt != null) {
191
 
                                var substitution = pt.GetSubstitution();
192
 
                                return declaredProperties.Select(m => new SpecializedProperty(m, substitution) { DeclaringType = pt });
193
 
                        } else {
194
 
                                return declaredProperties;
195
 
                        }
196
 
                }
197
 
                #endregion
198
 
                
199
 
                #region GetFields
200
 
                public static IEnumerable<IField> GetFields(IType type, Predicate<IUnresolvedField> filter, GetMemberOptions options)
201
 
                {
202
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
203
 
                                return GetFieldsImpl(type, filter, options);
204
 
                        } else {
205
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetFieldsImpl(t, filter, options));
206
 
                        }
207
 
                }
208
 
                
209
 
                static IEnumerable<IField> GetFieldsImpl(IType baseType, Predicate<IUnresolvedField> filter, GetMemberOptions options)
210
 
                {
211
 
                        IEnumerable<IField> declaredFields = baseType.GetFields(filter, options | declaredMembers);
212
 
                        if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
213
 
                                return declaredFields;
214
 
                        }
215
 
                        
216
 
                        ParameterizedType pt = baseType as ParameterizedType;
217
 
                        if (pt != null) {
218
 
                                var substitution = pt.GetSubstitution();
219
 
                                return declaredFields.Select(m => new SpecializedField(m, substitution) { DeclaringType = pt });
220
 
                        } else {
221
 
                                return declaredFields;
222
 
                        }
223
 
                }
224
 
                #endregion
225
 
                
226
 
                #region GetEvents
227
 
                public static IEnumerable<IEvent> GetEvents(IType type, Predicate<IUnresolvedEvent> filter, GetMemberOptions options)
228
 
                {
229
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
230
 
                                return GetEventsImpl(type, filter, options);
231
 
                        } else {
232
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetEventsImpl(t, filter, options));
233
 
                        }
234
 
                }
235
 
                
236
 
                static IEnumerable<IEvent> GetEventsImpl(IType baseType, Predicate<IUnresolvedEvent> filter, GetMemberOptions options)
237
 
                {
238
 
                        IEnumerable<IEvent> declaredEvents = baseType.GetEvents(filter, options | declaredMembers);
239
 
                        if ((options & GetMemberOptions.ReturnMemberDefinitions) == GetMemberOptions.ReturnMemberDefinitions) {
240
 
                                return declaredEvents;
241
 
                        }
242
 
                        
243
 
                        ParameterizedType pt = baseType as ParameterizedType;
244
 
                        if (pt != null) {
245
 
                                var substitution = pt.GetSubstitution();
246
 
                                return declaredEvents.Select(m => new SpecializedEvent(m, substitution) { DeclaringType = pt });
247
 
                        } else {
248
 
                                return declaredEvents;
249
 
                        }
250
 
                }
251
 
                #endregion
252
 
                
253
 
                #region GetMembers
254
 
                public static IEnumerable<IMember> GetMembers(IType type, Predicate<IUnresolvedMember> filter, GetMemberOptions options)
255
 
                {
256
 
                        if ((options & GetMemberOptions.IgnoreInheritedMembers) == GetMemberOptions.IgnoreInheritedMembers) {
257
 
                                return GetMembersImpl(type, filter, options);
258
 
                        } else {
259
 
                                return type.GetNonInterfaceBaseTypes().SelectMany(t => GetMembersImpl(t, filter, options));
260
 
                        }
261
 
                }
262
 
                
263
 
                static IEnumerable<IMember> GetMembersImpl(IType baseType, Predicate<IUnresolvedMember> filter, GetMemberOptions options)
264
 
                {
265
 
                        foreach (var m in GetMethodsImpl(baseType, null, filter, options))
266
 
                                yield return m;
267
 
                        foreach (var m in GetPropertiesImpl(baseType, filter, options))
268
 
                                yield return m;
269
 
                        foreach (var m in GetFieldsImpl(baseType, filter, options))
270
 
                                yield return m;
271
 
                        foreach (var m in GetEventsImpl(baseType, filter, options))
272
 
                                yield return m;
273
 
                }
274
 
                #endregion
275
 
        }
276
 
}