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.Globalization; |
---|
22 | using System.Linq; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.TypeSystem.Implementation |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// Represents an unresolved type definition. |
---|
28 | /// </summary> |
---|
29 | [Serializable] |
---|
30 | public class DefaultUnresolvedTypeDefinition : AbstractUnresolvedEntity, IUnresolvedTypeDefinition |
---|
31 | { |
---|
32 | TypeKind kind = TypeKind.Class; |
---|
33 | string namespaceName; |
---|
34 | IList<ITypeReference> baseTypes; |
---|
35 | IList<IUnresolvedTypeParameter> typeParameters; |
---|
36 | IList<IUnresolvedTypeDefinition> nestedTypes; |
---|
37 | IList<IUnresolvedMember> members; |
---|
38 | |
---|
39 | public DefaultUnresolvedTypeDefinition() |
---|
40 | { |
---|
41 | this.SymbolKind = SymbolKind.TypeDefinition; |
---|
42 | } |
---|
43 | |
---|
44 | public DefaultUnresolvedTypeDefinition(string fullName) |
---|
45 | { |
---|
46 | string namespaceName; |
---|
47 | string name; |
---|
48 | int idx = fullName.LastIndexOf ('.'); |
---|
49 | if (idx > 0) { |
---|
50 | namespaceName = fullName.Substring (0, idx); |
---|
51 | name = fullName.Substring (idx + 1); |
---|
52 | } else { |
---|
53 | namespaceName = ""; |
---|
54 | name = fullName; |
---|
55 | } |
---|
56 | |
---|
57 | this.SymbolKind = SymbolKind.TypeDefinition; |
---|
58 | this.namespaceName = namespaceName; |
---|
59 | this.Name = name; |
---|
60 | } |
---|
61 | |
---|
62 | public DefaultUnresolvedTypeDefinition(string namespaceName, string name) |
---|
63 | { |
---|
64 | this.SymbolKind = SymbolKind.TypeDefinition; |
---|
65 | this.namespaceName = namespaceName; |
---|
66 | this.Name = name; |
---|
67 | } |
---|
68 | |
---|
69 | public DefaultUnresolvedTypeDefinition(IUnresolvedTypeDefinition declaringTypeDefinition, string name) |
---|
70 | { |
---|
71 | this.SymbolKind = SymbolKind.TypeDefinition; |
---|
72 | this.DeclaringTypeDefinition = declaringTypeDefinition; |
---|
73 | this.namespaceName = declaringTypeDefinition.Namespace; |
---|
74 | this.Name = name; |
---|
75 | this.UnresolvedFile = declaringTypeDefinition.UnresolvedFile; |
---|
76 | } |
---|
77 | |
---|
78 | protected override void FreezeInternal() |
---|
79 | { |
---|
80 | base.FreezeInternal(); |
---|
81 | baseTypes = FreezableHelper.FreezeList(baseTypes); |
---|
82 | typeParameters = FreezableHelper.FreezeListAndElements(typeParameters); |
---|
83 | nestedTypes = FreezableHelper.FreezeListAndElements(nestedTypes); |
---|
84 | members = FreezableHelper.FreezeListAndElements(members); |
---|
85 | } |
---|
86 | |
---|
87 | public override object Clone() |
---|
88 | { |
---|
89 | var copy = (DefaultUnresolvedTypeDefinition)base.Clone(); |
---|
90 | if (baseTypes != null) |
---|
91 | copy.baseTypes = new List<ITypeReference>(baseTypes); |
---|
92 | if (typeParameters != null) |
---|
93 | copy.typeParameters = new List<IUnresolvedTypeParameter>(typeParameters); |
---|
94 | if (nestedTypes != null) |
---|
95 | copy.nestedTypes = new List<IUnresolvedTypeDefinition>(nestedTypes); |
---|
96 | if (members != null) |
---|
97 | copy.members = new List<IUnresolvedMember>(members); |
---|
98 | return copy; |
---|
99 | } |
---|
100 | |
---|
101 | public TypeKind Kind { |
---|
102 | get { return kind; } |
---|
103 | set { |
---|
104 | ThrowIfFrozen(); |
---|
105 | kind = value; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | public bool AddDefaultConstructorIfRequired { |
---|
110 | get { return flags[FlagAddDefaultConstructorIfRequired]; } |
---|
111 | set { |
---|
112 | ThrowIfFrozen(); |
---|
113 | flags[FlagAddDefaultConstructorIfRequired] = value; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | public bool? HasExtensionMethods { |
---|
118 | get { |
---|
119 | if (flags[FlagHasExtensionMethods]) |
---|
120 | return true; |
---|
121 | else if (flags[FlagHasNoExtensionMethods]) |
---|
122 | return false; |
---|
123 | else |
---|
124 | return null; |
---|
125 | } |
---|
126 | set { |
---|
127 | ThrowIfFrozen(); |
---|
128 | flags[FlagHasExtensionMethods] = (value == true); |
---|
129 | flags[FlagHasNoExtensionMethods] = (value == false); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | public bool IsPartial { |
---|
134 | get { return flags[FlagPartialTypeDefinition]; } |
---|
135 | set { |
---|
136 | ThrowIfFrozen(); |
---|
137 | flags[FlagPartialTypeDefinition] = value; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | public override string Namespace { |
---|
142 | get { return namespaceName; } |
---|
143 | set { |
---|
144 | if (value == null) |
---|
145 | throw new ArgumentNullException("value"); |
---|
146 | ThrowIfFrozen(); |
---|
147 | namespaceName = value; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | public override string ReflectionName { |
---|
152 | get { |
---|
153 | return this.FullTypeName.ReflectionName; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | public FullTypeName FullTypeName { |
---|
158 | get { |
---|
159 | IUnresolvedTypeDefinition declaringTypeDef = this.DeclaringTypeDefinition; |
---|
160 | if (declaringTypeDef != null) { |
---|
161 | return declaringTypeDef.FullTypeName.NestedType(this.Name, this.TypeParameters.Count - declaringTypeDef.TypeParameters.Count); |
---|
162 | } else { |
---|
163 | return new TopLevelTypeName(namespaceName, this.Name, this.TypeParameters.Count); |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | public IList<ITypeReference> BaseTypes { |
---|
169 | get { |
---|
170 | if (baseTypes == null) |
---|
171 | baseTypes = new List<ITypeReference>(); |
---|
172 | return baseTypes; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | public IList<IUnresolvedTypeParameter> TypeParameters { |
---|
177 | get { |
---|
178 | if (typeParameters == null) |
---|
179 | typeParameters = new List<IUnresolvedTypeParameter>(); |
---|
180 | return typeParameters; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | public IList<IUnresolvedTypeDefinition> NestedTypes { |
---|
185 | get { |
---|
186 | if (nestedTypes == null) |
---|
187 | nestedTypes = new List<IUnresolvedTypeDefinition>(); |
---|
188 | return nestedTypes; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | public IList<IUnresolvedMember> Members { |
---|
193 | get { |
---|
194 | if (members == null) |
---|
195 | members = new List<IUnresolvedMember>(); |
---|
196 | return members; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | public IEnumerable<IUnresolvedMethod> Methods { |
---|
201 | get { |
---|
202 | return Members.OfType<IUnresolvedMethod> (); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | public IEnumerable<IUnresolvedProperty> Properties { |
---|
207 | get { |
---|
208 | return Members.OfType<IUnresolvedProperty> (); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | public IEnumerable<IUnresolvedField> Fields { |
---|
213 | get { |
---|
214 | return Members.OfType<IUnresolvedField> (); |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | public IEnumerable<IUnresolvedEvent> Events { |
---|
219 | get { |
---|
220 | return Members.OfType<IUnresolvedEvent> (); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | public IType Resolve(ITypeResolveContext context) |
---|
226 | { |
---|
227 | if (context == null) |
---|
228 | throw new ArgumentNullException("context"); |
---|
229 | if (context.CurrentAssembly == null) |
---|
230 | throw new ArgumentException("An ITypeDefinition cannot be resolved in a context without a current assembly."); |
---|
231 | return context.CurrentAssembly.GetTypeDefinition(this.FullTypeName) |
---|
232 | ?? (IType)new UnknownType(this.Namespace, this.Name, this.TypeParameters.Count); |
---|
233 | } |
---|
234 | |
---|
235 | public virtual ITypeResolveContext CreateResolveContext(ITypeResolveContext parentContext) |
---|
236 | { |
---|
237 | return parentContext; |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|