1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.Collections.Generic;
21
using System.Collections.ObjectModel;
22
using System.Diagnostics;
26
using ICSharpCode.NRefactory.TypeSystem.Implementation;
28
namespace ICSharpCode.NRefactory.TypeSystem
31
/// Represents the intersection of several types.
33
public class IntersectionType : AbstractType
35
readonly ReadOnlyCollection<IType> types;
37
public ReadOnlyCollection<IType> Types {
41
private IntersectionType(IType[] types)
43
Debug.Assert(types.Length >= 2);
44
this.types = Array.AsReadOnly(types);
47
public static IType Create(IEnumerable<IType> types)
49
IType[] arr = types.Distinct().ToArray();
50
foreach (IType type in arr) {
52
throw new ArgumentNullException();
55
return SpecialType.UnknownType;
56
else if (arr.Length == 1)
59
return new IntersectionType(arr);
62
public override TypeKind Kind {
63
get { return TypeKind.Intersection; }
66
public override string Name {
68
StringBuilder b = new StringBuilder();
69
foreach (var t in types) {
78
public override string ReflectionName {
80
StringBuilder b = new StringBuilder();
81
foreach (var t in types) {
84
b.Append(t.ReflectionName);
90
public override bool? IsReferenceType {
92
foreach (var t in types) {
93
bool? isReferenceType = t.IsReferenceType;
94
if (isReferenceType.HasValue)
95
return isReferenceType.Value;
101
public override int GetHashCode()
105
foreach (var t in types) {
107
hashCode += t.GetHashCode();
113
public override bool Equals(IType other)
115
IntersectionType o = other as IntersectionType;
116
if (o != null && types.Count == o.types.Count) {
117
for (int i = 0; i < types.Count; i++) {
118
if (!types[i].Equals(o.types[i]))
126
public override IEnumerable<IType> DirectBaseTypes {
127
get { return types; }
130
public override ITypeReference ToTypeReference()
132
throw new NotSupportedException();
135
public override IEnumerable<IMethod> GetMethods(Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
137
return GetMembersHelper.GetMethods(this, FilterNonStatic(filter), options);
140
public override IEnumerable<IMethod> GetMethods(IList<IType> typeArguments, Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
142
return GetMembersHelper.GetMethods(this, typeArguments, filter, options);
145
public override IEnumerable<IProperty> GetProperties(Predicate<IUnresolvedProperty> filter, GetMemberOptions options)
147
return GetMembersHelper.GetProperties(this, FilterNonStatic(filter), options);
150
public override IEnumerable<IField> GetFields(Predicate<IUnresolvedField> filter, GetMemberOptions options)
152
return GetMembersHelper.GetFields(this, FilterNonStatic(filter), options);
155
public override IEnumerable<IEvent> GetEvents(Predicate<IUnresolvedEvent> filter, GetMemberOptions options)
157
return GetMembersHelper.GetEvents(this, FilterNonStatic(filter), options);
160
public override IEnumerable<IMember> GetMembers(Predicate<IUnresolvedMember> filter, GetMemberOptions options)
162
return GetMembersHelper.GetMembers(this, FilterNonStatic(filter), options);
165
public override IEnumerable<IMethod> GetAccessors(Predicate<IUnresolvedMethod> filter, GetMemberOptions options)
167
return GetMembersHelper.GetAccessors(this, FilterNonStatic(filter), options);
170
static Predicate<T> FilterNonStatic<T>(Predicate<T> filter) where T : class, IUnresolvedMember
173
return member => !member.IsStatic;
175
return member => !member.IsStatic && filter(member);