This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 262
/
IType.cs
441 lines (402 loc) · 18.4 KB
/
IType.cs
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
//
// 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.Diagnostics.Contracts;
namespace ICSharpCode.NRefactory.TypeSystem
{
/// <summary>
/// This interface represents a resolved type in the type system.
/// </summary>
/// <remarks>
/// <para>
/// A type is potentially
/// - a type definition (<see cref="ITypeDefiniton"/>, i.e. a class, struct, interface, delegate, or built-in primitive type)
/// - a parameterized type (<see cref="ParameterizedType"/>, e.g. List<int>)
/// - a type parameter (<see cref="ITypeParameter"/>, e.g. T)
/// - an array (<see cref="ArrayType"/>)
/// - a pointer (<see cref="PointerType"/>)
/// - a managed reference (<see cref="ByReferenceType"/>)
/// - one of the special types (<see cref="SharedTypes.UnknownType"/>, <see cref="SharedTypes.Null"/>,
/// <see cref="SharedTypes.Dynamic"/>, <see cref="SharedTypes.UnboundTypeArgument"/>)
///
/// The <see cref="IType.Kind"/> property can be used to switch on the kind of a type.
/// </para>
/// <para>
/// IType uses the null object pattern: <see cref="SharedTypes.UnknownType"/> serves as the null object.
/// Methods or properties returning IType never return null unless documented otherwise.
/// </para>
/// <para>
/// Types should be compared for equality using the <see cref="IType.Equals(IType)"/> method.
/// Identical types do not necessarily use the same object reference.
/// </para>
/// </remarks>
#if WITH_CONTRACTS
[ContractClass(typeof(ITypeContract))]
#endif
public interface IType : ITypeReference, INamedElement, IEquatable<IType>
{
/// <summary>
/// Gets the type kind.
/// </summary>
TypeKind Kind { get; }
/// <summary>
/// Gets whether the type is a reference type or value type.
/// </summary>
/// <returns>
/// true, if the type is a reference type.
/// false, if the type is a value type.
/// null, if the type is not known (e.g. unconstrained generic type parameter or type not found)
/// </returns>
/// <remarks>
/// The resolve context is required for type parameters with a constraint "T : SomeType":
/// the type parameter is a reference type iff SomeType is a class type.
/// </remarks>
bool? IsReferenceType(ITypeResolveContext context);
/// <summary>
/// Gets the underlying type definition.
/// Can return null for types which do not have a type definition (for example arrays, pointers, type parameters).
///
/// For partial classes, this method always returns the <see cref="CompoundTypeDefinition"/>.
/// </summary>
ITypeDefinition GetDefinition();
/// <summary>
/// Gets the parent type, if this is a nested type.
/// Returns null for top-level types.
/// </summary>
IType DeclaringType { get; }
/// <summary>
/// Gets the number of type parameters.
/// </summary>
int TypeParameterCount { get; }
/// <summary>
/// Calls ITypeVisitor.Visit for this type.
/// </summary>
/// <returns>The return value of the ITypeVisitor.Visit call</returns>
IType AcceptVisitor(TypeVisitor visitor);
/// <summary>
/// Calls ITypeVisitor.Visit for all children of this type, and reconstructs this type with the children based
/// on the return values of the visit calls.
/// </summary>
/// <returns>A copy of this type, with all children replaced by the return value of the corresponding visitor call.
/// If the visitor returned the original types for all children (or if there are no children), returns <c>this</c>.
/// </returns>
IType VisitChildren(TypeVisitor visitor);
/// <summary>
/// Gets the direct base types.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <returns>Returns the direct base types including interfaces</returns>
IEnumerable<IType> GetBaseTypes(ITypeResolveContext context);
/// <summary>
/// Gets inner classes (including inherited inner classes).
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which types to return.
/// The filter is tested on the original type definitions (before parameterization).</param>
/// <remarks>
/// <para>
/// If the nested type is generic, this method will return a parameterized type,
/// where the additional type parameters are set to <see cref="SharedType.UnboundTypeArgument"/>.
/// </para>
/// <para>
/// Type parameters belonging to the outer class will have the value copied from the outer type
/// if it is a parameterized type. Otherwise, those existing type parameters will be self-parameterized,
/// and thus 'leaked' to the caller in the same way the GetMembers() method does not specialize members
/// from an <see cref="ITypeDefinition"/> and 'leaks' type parameters in member signatures.
/// </para>
/// </remarks>
/// <example>
/// <code>
/// class Base<T> {
/// class Nested<X> {}
/// }
/// class Derived<A, B> : Base<B> {}
///
/// Derived[string,int].GetNestedTypes() = { Base`1+Nested`1[int, unbound] }
/// Derived.GetNestedTypes() = { Base`1+Nested`1[`1, unbound] }
/// Base[`1].GetNestedTypes() = { Base`1+Nested`1[`1, unbound] }
/// Base.GetNestedTypes() = { Base`1+Nested`1[`0, unbound] }
/// </code>
/// </example>
IEnumerable<IType> GetNestedTypes(ITypeResolveContext context, Predicate<ITypeDefinition> filter = null, GetMemberOptions options = GetMemberOptions.None);
// Note that we cannot 'leak' the additional type parameter as we leak the normal type parameters, because
// the index might collide. For example,
// class Base<T> { class Nested<X> {} }
// class Derived<A, B> : Base<B> { }
//
// Derived<string, int>.GetNestedTypes() = Base+Nested<int, UnboundTypeArgument>
// Derived.GetNestedTypes() = Base+Nested<`1, >
// Here `1 refers to B, and there's no way to return X as it would collide with B.
/// <summary>
/// Gets inner classes (including inherited inner classes)
/// that have <c>typeArguments.Count</c> additional type parameters.
/// </summary>
/// <param name="typeArguments">The type arguments passed to the inner class</param>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which types to return.
/// The filter is tested on the original type definitions (before parameterization).</param>
/// <remarks>
/// Type parameters belonging to the outer class will have the value copied from the outer type
/// if it is a parameterized type. Otherwise, those existing type parameters will be self-parameterized,
/// and thus 'leaked' to the caller in the same way the GetMembers() method does not specialize members
/// from an <see cref="ITypeDefinition"/> and 'leaks' type parameters in member signatures.
/// </remarks>
IEnumerable<IType> GetNestedTypes(IList<IType> typeArguments, ITypeResolveContext context, Predicate<ITypeDefinition> filter = null, GetMemberOptions options = GetMemberOptions.None);
/// <summary>
/// Gets all instance constructors for this type.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which constructors to return.
/// The filter is tested on the original method definitions (before specialization).</param>
/// <remarks>
/// <para>The result does not include constructors in base classes or static constructors.</para>
/// <para>
/// For methods on parameterized types, type substitution will be performed on the method signature,
/// and the appropriate <see cref="SpecializedMethod"/> will be returned.
/// </para>
/// </remarks>
IEnumerable<IMethod> GetConstructors(ITypeResolveContext context, Predicate<IMethod> filter = null, GetMemberOptions options = GetMemberOptions.IgnoreInheritedMembers);
/// <summary>
/// Gets all methods that can be called on this type.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which methods to return.
/// The filter is tested on the original method definitions (before specialization).</param>
/// <remarks>
/// <para>
/// The result does not include constructors.
/// </para>
/// <para>
/// For methods on parameterized types, type substitution will be performed on the method signature,
/// and the appropriate <see cref="SpecializedMethod"/> will be returned.
/// </para>
/// <para>
/// If the method being returned is generic, and this type is a parameterized type where the type
/// arguments involve another method's type parameters, the resulting specialized signature
/// will be ambiguous as to which method a type parameter belongs to.
/// For example, "List[[``0]].GetMethods()" will return "ConvertAll(Converter`2[[``0, ``0]])".
///
/// If possible, use the other GetMethods() overload to supply type arguments to the method,
/// so that both class and method type parameter can be substituted at the same time, so that
/// the ambiguity can be avoided.
/// </para>
/// </remarks>
IEnumerable<IMethod> GetMethods(ITypeResolveContext context, Predicate<IMethod> filter = null, GetMemberOptions options = GetMemberOptions.None);
/// <summary>
/// Gets all generic methods that can be called on this type with the specified type arguments.
/// </summary>
/// <param name="typeArguments">The type arguments used for the method call.</param>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which methods to return.
/// The filter is tested on the original method definitions (before specialization).</param>
/// <remarks>
/// <para>The result does not include constructors.</para>
/// <para>
/// Type substitution will be performed on the method signature, creating a <see cref="SpecializedMethod"/>
/// with the specified type arguments.
/// </para>
/// <para>
/// When the list of type arguments is empty, this method acts like the GetMethods() overload without
/// the type arguments parameter - that is, it also returns generic methods,
/// and the other overload's remarks about ambiguous signatures apply here as well.
/// </para>
/// </remarks>
IEnumerable<IMethod> GetMethods(IList<IType> typeArguments, ITypeResolveContext context, Predicate<IMethod> filter = null, GetMemberOptions options = GetMemberOptions.None);
/// <summary>
/// Gets all properties that can be called on this type.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which properties to return.
/// The filter is tested on the original property definitions (before specialization).</param>
/// <remarks>
/// For properties on parameterized types, type substitution will be performed on the property signature,
/// and the appropriate <see cref="SpecializedProperty"/> will be returned.
/// </remarks>
IEnumerable<IProperty> GetProperties(ITypeResolveContext context, Predicate<IProperty> filter = null, GetMemberOptions options = GetMemberOptions.None);
/// <summary>
/// Gets all fields that can be accessed on this type.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which constructors to return.
/// The filter is tested on the original field definitions (before specialization).</param>
/// <remarks>
/// For fields on parameterized types, type substitution will be performed on the field's return type,
/// and the appropriate <see cref="SpecializedField"/> will be returned.
/// </remarks>
IEnumerable<IField> GetFields(ITypeResolveContext context, Predicate<IField> filter = null, GetMemberOptions options = GetMemberOptions.None);
/// <summary>
/// Gets all events that can be accessed on this type.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which events to return.
/// The filter is tested on the original event definitions (before specialization).</param>
/// <remarks>
/// For fields on parameterized types, type substitution will be performed on the event's return type,
/// and the appropriate <see cref="SpecializedEvent"/> will be returned.
/// </remarks>
IEnumerable<IEvent> GetEvents(ITypeResolveContext context, Predicate<IEvent> filter = null, GetMemberOptions options = GetMemberOptions.None);
/// <summary>
/// Gets all members that can be called on this type.
/// </summary>
/// <param name="context">The context used for resolving type references</param>
/// <param name="filter">The filter used to select which members to return.
/// The filter is tested on the original member definitions (before specialization).</param>
/// <remarks>
/// <para>
/// The resulting list is the union of GetFields(), GetProperties(), GetMethods() and GetEvents().
/// It does not include constructors.
/// For parameterized types, type substitution will be performed.
/// </para>
/// <para>
/// For generic methods, the remarks about ambiguous signatures from the
/// <see cref="GetMethods(ITypeResolveContext, Predicate{IMethod})"/> method apply here as well.
/// </para>
/// </remarks>
IEnumerable<IMember> GetMembers(ITypeResolveContext context, Predicate<IMember> filter = null, GetMemberOptions options = GetMemberOptions.None);
}
[Flags]
public enum GetMemberOptions
{
/// <summary>
/// No options specified - this is the default.
/// Members will be specialized, and inherited members will be included.
/// </summary>
None = 0x00,
/// <summary>
/// Do not specialize the returned members - directly return the definitions.
/// </summary>
ReturnMemberDefinitions = 0x01,
/// <summary>
/// Do not list inherited members - only list members defined directly on this type.
/// </summary>
IgnoreInheritedMembers = 0x02
}
#if WITH_CONTRACTS
[ContractClassFor(typeof(IType))]
abstract class ITypeContract : ITypeReferenceContract, IType
{
bool? IType.IsReferenceType(ITypeResolveContext context)
{
Contract.Requires(context != null);
return null;
}
int IType.TypeParameterCount {
get {
Contract.Ensures(Contract.Result<int>() >= 0);
return 0;
}
}
IType IType.DeclaringType {
get { return null; }
}
IEnumerable<IType> IType.GetBaseTypes(ITypeResolveContext context)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IType>>() != null);
return null;
}
IEnumerable<IType> IType.GetNestedTypes(ITypeResolveContext context, Predicate<ITypeDefinition> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IType>>() != null);
return null;
}
IEnumerable<IMethod> IType.GetMethods(ITypeResolveContext context, Predicate<IMethod> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IMethod>>() != null);
return null;
}
IEnumerable<IMethod> IType.GetConstructors(ITypeResolveContext context, Predicate<IMethod> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IMethod>>() != null);
return null;
}
IEnumerable<IProperty> IType.GetProperties(ITypeResolveContext context, Predicate<IProperty> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IProperty>>() != null);
return null;
}
IEnumerable<IField> IType.GetFields(ITypeResolveContext context, Predicate<IField> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IField>>() != null);
return null;
}
IEnumerable<IEvent> IType.GetEvents(ITypeResolveContext context, Predicate<IEvent> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IEvent>>() != null);
return null;
}
IEnumerable<IMember> IType.GetEvents(ITypeResolveContext context, Predicate<IMember> filter, GetMemberOptions options)
{
Contract.Requires(context != null);
Contract.Ensures(Contract.Result<IEnumerable<IMember>>() != null);
return null;
}
string INamedElement.FullName {
get {
Contract.Ensures(Contract.Result<string>() != null);
return null;
}
}
string INamedElement.Name {
get {
Contract.Ensures(Contract.Result<string>() != null);
return null;
}
}
string INamedElement.Namespace {
get {
Contract.Ensures(Contract.Result<string>() != null);
return null;
}
}
string INamedElement.ReflectionName {
get {
Contract.Ensures(Contract.Result<string>() != null);
return null;
}
}
ITypeDefinition IType.GetDefinition()
{
return null;
}
bool IEquatable<IType>.Equals(IType other)
{
return false;
}
IType IType.AcceptVisitor(TypeVisitor visitor)
{
Contract.Requires(visitor != null);
Contract.Ensures(Contract.Result<IType>() != null);
return this;
}
IType IType.VisitChildren(TypeVisitor visitor)
{
Contract.Requires(visitor != null);
Contract.Ensures(Contract.Result<IType>() != null);
return this;
}
}
#endif
}