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 ICSharpCode.NRefactory.CSharp.Resolver; |
---|
22 | using ICSharpCode.NRefactory.TypeSystem; |
---|
23 | |
---|
24 | namespace ICSharpCode.NRefactory.CSharp |
---|
25 | { |
---|
26 | /// <summary> |
---|
27 | /// A type reference in the C# AST. |
---|
28 | /// </summary> |
---|
29 | public abstract class AstType : AstNode |
---|
30 | { |
---|
31 | #region Null |
---|
32 | public new static readonly AstType Null = new NullAstType (); |
---|
33 | |
---|
34 | sealed class NullAstType : AstType |
---|
35 | { |
---|
36 | public override bool IsNull { |
---|
37 | get { |
---|
38 | return true; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
43 | { |
---|
44 | visitor.VisitNullNode(this); |
---|
45 | } |
---|
46 | |
---|
47 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
48 | { |
---|
49 | return visitor.VisitNullNode(this); |
---|
50 | } |
---|
51 | |
---|
52 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
53 | { |
---|
54 | return visitor.VisitNullNode(this, data); |
---|
55 | } |
---|
56 | |
---|
57 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
58 | { |
---|
59 | return other == null || other.IsNull; |
---|
60 | } |
---|
61 | |
---|
62 | public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider) |
---|
63 | { |
---|
64 | return SpecialType.UnknownType; |
---|
65 | } |
---|
66 | } |
---|
67 | #endregion |
---|
68 | |
---|
69 | #region PatternPlaceholder |
---|
70 | public static implicit operator AstType(PatternMatching.Pattern pattern) |
---|
71 | { |
---|
72 | return pattern != null ? new PatternPlaceholder(pattern) : null; |
---|
73 | } |
---|
74 | |
---|
75 | sealed class PatternPlaceholder : AstType, PatternMatching.INode |
---|
76 | { |
---|
77 | readonly PatternMatching.Pattern child; |
---|
78 | |
---|
79 | public PatternPlaceholder(PatternMatching.Pattern child) |
---|
80 | { |
---|
81 | this.child = child; |
---|
82 | } |
---|
83 | |
---|
84 | public override NodeType NodeType { |
---|
85 | get { return NodeType.Pattern; } |
---|
86 | } |
---|
87 | |
---|
88 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
89 | { |
---|
90 | visitor.VisitPatternPlaceholder (this, child); |
---|
91 | } |
---|
92 | |
---|
93 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
94 | { |
---|
95 | return visitor.VisitPatternPlaceholder (this, child); |
---|
96 | } |
---|
97 | |
---|
98 | public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
---|
99 | { |
---|
100 | return visitor.VisitPatternPlaceholder (this, child, data); |
---|
101 | } |
---|
102 | |
---|
103 | public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider) |
---|
104 | { |
---|
105 | throw new NotSupportedException(); |
---|
106 | } |
---|
107 | |
---|
108 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
109 | { |
---|
110 | return child.DoMatch(other, match); |
---|
111 | } |
---|
112 | |
---|
113 | bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo) |
---|
114 | { |
---|
115 | return child.DoMatchCollection(role, pos, match, backtrackingInfo); |
---|
116 | } |
---|
117 | } |
---|
118 | #endregion |
---|
119 | |
---|
120 | public override NodeType NodeType { |
---|
121 | get { return NodeType.TypeReference; } |
---|
122 | } |
---|
123 | |
---|
124 | public new AstType Clone() |
---|
125 | { |
---|
126 | return (AstType)base.Clone(); |
---|
127 | } |
---|
128 | |
---|
129 | /// <summary> |
---|
130 | /// Gets whether this type is a SimpleType "var". |
---|
131 | /// </summary> |
---|
132 | public bool IsVar() |
---|
133 | { |
---|
134 | SimpleType st = this as SimpleType; |
---|
135 | return st != null && st.Identifier == "var" && st.TypeArguments.Count == 0; |
---|
136 | } |
---|
137 | |
---|
138 | /// <summary> |
---|
139 | /// Create an ITypeReference for this AstType. |
---|
140 | /// Uses the context (ancestors of this node) to determine the correct <see cref="NameLookupMode"/>. |
---|
141 | /// </summary> |
---|
142 | /// <remarks> |
---|
143 | /// The resulting type reference will read the context information from the |
---|
144 | /// <see cref="ITypeResolveContext"/>: |
---|
145 | /// For resolving type parameters, the CurrentTypeDefinition/CurrentMember is used. |
---|
146 | /// For resolving simple names, the current namespace and usings from the CurrentUsingScope |
---|
147 | /// (on CSharpTypeResolveContext only) is used. |
---|
148 | /// </remarks> |
---|
149 | public ITypeReference ToTypeReference(InterningProvider interningProvider = null) |
---|
150 | { |
---|
151 | return ToTypeReference(GetNameLookupMode(), interningProvider); |
---|
152 | } |
---|
153 | |
---|
154 | /// <summary> |
---|
155 | /// Create an ITypeReference for this AstType. |
---|
156 | /// </summary> |
---|
157 | /// <remarks> |
---|
158 | /// The resulting type reference will read the context information from the |
---|
159 | /// <see cref="ITypeResolveContext"/>: |
---|
160 | /// For resolving type parameters, the CurrentTypeDefinition/CurrentMember is used. |
---|
161 | /// For resolving simple names, the current namespace and usings from the CurrentUsingScope |
---|
162 | /// (on CSharpTypeResolveContext only) is used. |
---|
163 | /// </remarks> |
---|
164 | public abstract ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider = null); |
---|
165 | |
---|
166 | /// <summary> |
---|
167 | /// Gets the name lookup mode from the context (looking at the ancestors of this <see cref="AstType"/>). |
---|
168 | /// </summary> |
---|
169 | public NameLookupMode GetNameLookupMode() |
---|
170 | { |
---|
171 | AstType outermostType = this; |
---|
172 | while (outermostType.Parent is AstType) |
---|
173 | outermostType = (AstType)outermostType.Parent; |
---|
174 | |
---|
175 | if (outermostType.Parent is UsingDeclaration || outermostType.Parent is UsingAliasDeclaration) { |
---|
176 | return NameLookupMode.TypeInUsingDeclaration; |
---|
177 | } else if (outermostType.Role == Roles.BaseType) { |
---|
178 | // Use BaseTypeReference for a type's base type, and for a constraint on a type. |
---|
179 | // Do not use it for a constraint on a method. |
---|
180 | if (outermostType.Parent is TypeDeclaration || (outermostType.Parent is Constraint && outermostType.Parent.Parent is TypeDeclaration)) |
---|
181 | return NameLookupMode.BaseTypeReference; |
---|
182 | } |
---|
183 | return NameLookupMode.Type; |
---|
184 | } |
---|
185 | |
---|
186 | /// <summary> |
---|
187 | /// Creates a pointer type from this type by nesting it in a <see cref="ComposedType"/>. |
---|
188 | /// If this type already is a pointer type, this method just increases the PointerRank of the existing pointer type. |
---|
189 | /// </summary> |
---|
190 | public virtual AstType MakePointerType() |
---|
191 | { |
---|
192 | return new ComposedType { BaseType = this }.MakePointerType(); |
---|
193 | } |
---|
194 | |
---|
195 | /// <summary> |
---|
196 | /// Creates an array type from this type by nesting it in a <see cref="ComposedType"/>. |
---|
197 | /// If this type already is an array type, the additional rank is prepended to the existing array specifier list. |
---|
198 | /// Thus, <c>new SimpleType("T").MakeArrayType(1).MakeArrayType(2)</c> will result in "T[,][]". |
---|
199 | /// </summary> |
---|
200 | public virtual AstType MakeArrayType(int rank = 1) |
---|
201 | { |
---|
202 | return new ComposedType { BaseType = this }.MakeArrayType(rank); |
---|
203 | } |
---|
204 | |
---|
205 | /// <summary> |
---|
206 | /// Creates a nullable type from this type by nesting it in a <see cref="ComposedType"/>. |
---|
207 | /// </summary> |
---|
208 | public AstType MakeNullableType() |
---|
209 | { |
---|
210 | return new ComposedType { BaseType = this, HasNullableSpecifier = true }; |
---|
211 | } |
---|
212 | |
---|
213 | /// <summary> |
---|
214 | /// Builds an expression that can be used to access a static member on this type. |
---|
215 | /// </summary> |
---|
216 | public MemberReferenceExpression Member(string memberName) |
---|
217 | { |
---|
218 | return new TypeReferenceExpression { Type = this }.Member(memberName); |
---|
219 | } |
---|
220 | |
---|
221 | /// <summary> |
---|
222 | /// Builds an expression that can be used to access a static member on this type. |
---|
223 | /// </summary> |
---|
224 | public MemberType MemberType(string memberName, params AstType[] typeArguments) |
---|
225 | { |
---|
226 | var memberType = new MemberType(this, memberName); |
---|
227 | memberType.TypeArguments.AddRange(typeArguments); |
---|
228 | return memberType; |
---|
229 | } |
---|
230 | |
---|
231 | /// <summary> |
---|
232 | /// Builds an expression that can be used to access a static member on this type. |
---|
233 | /// </summary> |
---|
234 | public MemberType MemberType(string memberName, IEnumerable<AstType> typeArguments) |
---|
235 | { |
---|
236 | var memberType = new MemberType(this, memberName); |
---|
237 | memberType.TypeArguments.AddRange(typeArguments); |
---|
238 | return memberType; |
---|
239 | } |
---|
240 | |
---|
241 | /// <summary> |
---|
242 | /// Builds an invocation expression using this type as target. |
---|
243 | /// </summary> |
---|
244 | public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments) |
---|
245 | { |
---|
246 | return new TypeReferenceExpression { Type = this }.Invoke(methodName, arguments); |
---|
247 | } |
---|
248 | |
---|
249 | /// <summary> |
---|
250 | /// Builds an invocation expression using this type as target. |
---|
251 | /// </summary> |
---|
252 | public InvocationExpression Invoke(string methodName, params Expression[] arguments) |
---|
253 | { |
---|
254 | return new TypeReferenceExpression { Type = this }.Invoke(methodName, arguments); |
---|
255 | } |
---|
256 | |
---|
257 | /// <summary> |
---|
258 | /// Builds an invocation expression using this type as target. |
---|
259 | /// </summary> |
---|
260 | public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments) |
---|
261 | { |
---|
262 | return new TypeReferenceExpression { Type = this }.Invoke(methodName, typeArguments, arguments); |
---|
263 | } |
---|
264 | |
---|
265 | /// <summary> |
---|
266 | /// Creates a simple AstType from a dotted name. |
---|
267 | /// Does not support generics, arrays, etc. - just simple dotted names, |
---|
268 | /// e.g. namespace names. |
---|
269 | /// </summary> |
---|
270 | public static AstType Create(string dottedName) |
---|
271 | { |
---|
272 | string[] parts = dottedName.Split('.'); |
---|
273 | AstType type = new SimpleType(parts[0]); |
---|
274 | for (int i = 1; i < parts.Length; i++) { |
---|
275 | type = new MemberType(type, parts[i]); |
---|
276 | } |
---|
277 | return type; |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|