1 | // Copyright (c) 2010-2013 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.Collections.ObjectModel; |
---|
22 | using System.Diagnostics; |
---|
23 | using System.Linq; |
---|
24 | using System.Text; |
---|
25 | |
---|
26 | using ICSharpCode.NRefactory.TypeSystem.Implementation; |
---|
27 | |
---|
28 | namespace ICSharpCode.NRefactory.TypeSystem |
---|
29 | { |
---|
30 | /// <summary> |
---|
31 | /// Represents the intersection of several types. |
---|
32 | /// </summary> |
---|
33 | public class IntersectionType : AbstractType |
---|
34 | { |
---|
35 | readonly ReadOnlyCollection<IType> types; |
---|
36 | |
---|
37 | public ReadOnlyCollection<IType> Types { |
---|
38 | get { return types; } |
---|
39 | } |
---|
40 | |
---|
41 | private IntersectionType(IType[] types) |
---|
42 | { |
---|
43 | Debug.Assert(types.Length >= 2); |
---|
44 | this.types = Array.AsReadOnly(types); |
---|
45 | } |
---|
46 | |
---|
47 | public static IType Create(IEnumerable<IType> types) |
---|
48 | { |
---|
49 | IType[] arr = types.Distinct().ToArray(); |
---|
50 | foreach (IType type in arr) { |
---|
51 | if (type == null) |
---|
52 | throw new ArgumentNullException(); |
---|
53 | } |
---|
54 | if (arr.Length == 0) |
---|
55 | return SpecialType.UnknownType; |
---|
56 | else if (arr.Length == 1) |
---|
57 | return arr[0]; |
---|
58 | else |
---|
59 | return new IntersectionType(arr); |
---|
60 | } |
---|
61 | |
---|
62 | public override TypeKind Kind { |
---|
63 | get { return TypeKind.Intersection; } |
---|
64 | } |
---|
65 | |
---|
66 | public override string Name { |
---|
67 | get { |
---|
68 | StringBuilder b = new StringBuilder(); |
---|
69 | foreach (var t in types) { |
---|
70 | if (b.Length > 0) |
---|
71 | b.Append(" & "); |
---|
72 | b.Append(t.Name); |
---|
73 | } |
---|
74 | return b.ToString(); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public override string ReflectionName { |
---|
79 | get { |
---|
80 | StringBuilder b = new StringBuilder(); |
---|
81 | foreach (var t in types) { |
---|
82 | if (b.Length > 0) |
---|
83 | b.Append(" & "); |
---|
84 | b.Append(t.ReflectionName); |
---|
85 | } |
---|
86 | return b.ToString(); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | public override bool? IsReferenceType { |
---|
91 | get { |
---|
92 | foreach (var t in types) { |
---|
93 | bool? isReferenceType = t.IsReferenceType; |
---|
94 | if (isReferenceType.HasValue) |
---|
95 | return isReferenceType.Value; |
---|
96 | } |
---|
97 | return null; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | public override int GetHashCode() |
---|
102 | { |
---|
103 | int hashCode = 0; |
---|
104 | unchecked { |
---|
105 | foreach (var t in types) { |
---|
106 | hashCode *= 7137517; |
---|
107 | hashCode += t.GetHashCode(); |
---|
108 | } |
---|
109 | } |
---|
110 | return hashCode; |
---|
111 | } |
---|
112 | |
---|
113 | public override bool Equals(IType other) |
---|
114 | { |
---|
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])) |
---|
119 | return false; |
---|
120 | } |
---|
121 | return true; |
---|
122 | } |
---|
123 | return false; |
---|
124 | } |
---|
125 | |
---|
126 | public override IEnumerable<IType> DirectBaseTypes { |
---|
127 | get { return types; } |
---|
128 | } |
---|
129 | |
---|
130 | public override ITypeReference ToTypeReference() |
---|
131 | { |
---|
132 | throw new NotSupportedException(); |
---|
133 | } |
---|
134 | |
---|
135 | public override IEnumerable<IMethod> GetMethods(Predicate<IUnresolvedMethod> filter, GetMemberOptions options) |
---|
136 | { |
---|
137 | return GetMembersHelper.GetMethods(this, FilterNonStatic(filter), options); |
---|
138 | } |
---|
139 | |
---|
140 | public override IEnumerable<IMethod> GetMethods(IList<IType> typeArguments, Predicate<IUnresolvedMethod> filter, GetMemberOptions options) |
---|
141 | { |
---|
142 | return GetMembersHelper.GetMethods(this, typeArguments, filter, options); |
---|
143 | } |
---|
144 | |
---|
145 | public override IEnumerable<IProperty> GetProperties(Predicate<IUnresolvedProperty> filter, GetMemberOptions options) |
---|
146 | { |
---|
147 | return GetMembersHelper.GetProperties(this, FilterNonStatic(filter), options); |
---|
148 | } |
---|
149 | |
---|
150 | public override IEnumerable<IField> GetFields(Predicate<IUnresolvedField> filter, GetMemberOptions options) |
---|
151 | { |
---|
152 | return GetMembersHelper.GetFields(this, FilterNonStatic(filter), options); |
---|
153 | } |
---|
154 | |
---|
155 | public override IEnumerable<IEvent> GetEvents(Predicate<IUnresolvedEvent> filter, GetMemberOptions options) |
---|
156 | { |
---|
157 | return GetMembersHelper.GetEvents(this, FilterNonStatic(filter), options); |
---|
158 | } |
---|
159 | |
---|
160 | public override IEnumerable<IMember> GetMembers(Predicate<IUnresolvedMember> filter, GetMemberOptions options) |
---|
161 | { |
---|
162 | return GetMembersHelper.GetMembers(this, FilterNonStatic(filter), options); |
---|
163 | } |
---|
164 | |
---|
165 | public override IEnumerable<IMethod> GetAccessors(Predicate<IUnresolvedMethod> filter, GetMemberOptions options) |
---|
166 | { |
---|
167 | return GetMembersHelper.GetAccessors(this, FilterNonStatic(filter), options); |
---|
168 | } |
---|
169 | |
---|
170 | static Predicate<T> FilterNonStatic<T>(Predicate<T> filter) where T : class, IUnresolvedMember |
---|
171 | { |
---|
172 | if (filter == null) |
---|
173 | return member => !member.IsStatic; |
---|
174 | else |
---|
175 | return member => !member.IsStatic && filter(member); |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|