1
// Copyright (c) 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 ICSharpCode.NRefactory.CSharp.Resolver;
22
using ICSharpCode.NRefactory.TypeSystem;
24
namespace ICSharpCode.NRefactory.CSharp
27
/// A type reference in the C# AST.
29
public abstract class AstType : AstNode
32
public new static readonly AstType Null = new NullAstType ();
34
sealed class NullAstType : AstType
36
public override bool IsNull {
42
public override void AcceptVisitor (IAstVisitor visitor)
46
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
51
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
56
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
58
return other == null || other.IsNull;
61
public override ITypeReference ToTypeReference(NameLookupMode lookupMode)
63
return SpecialType.UnknownType;
68
#region PatternPlaceholder
69
public static implicit operator AstType(PatternMatching.Pattern pattern)
71
return pattern != null ? new PatternPlaceholder(pattern) : null;
74
sealed class PatternPlaceholder : AstType, PatternMatching.INode
76
readonly PatternMatching.Pattern child;
78
public PatternPlaceholder(PatternMatching.Pattern child)
83
public override NodeType NodeType {
84
get { return NodeType.Pattern; }
87
public override void AcceptVisitor (IAstVisitor visitor)
89
visitor.VisitPatternPlaceholder (this, child);
92
public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
94
return visitor.VisitPatternPlaceholder (this, child);
97
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
99
return visitor.VisitPatternPlaceholder (this, child, data);
102
public override ITypeReference ToTypeReference(NameLookupMode lookupMode)
104
throw new NotSupportedException();
107
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
109
return child.DoMatch(other, match);
112
bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
114
return child.DoMatchCollection(role, pos, match, backtrackingInfo);
119
public override NodeType NodeType {
120
get { return NodeType.TypeReference; }
123
public new AstType Clone()
125
return (AstType)base.Clone();
129
/// Create an ITypeReference for this AstType.
132
/// The resulting type reference will read the context information from the
133
/// <see cref="ITypeResolveContext"/>:
134
/// For resolving type parameters, the CurrentTypeDefinition/CurrentMember is used.
135
/// For resolving simple names, the current namespace and usings from the CurrentUsingScope
136
/// (on CSharpTypeResolveContext only) is used.
138
public abstract ITypeReference ToTypeReference(NameLookupMode lookupMode = NameLookupMode.Type);
141
/// Creates a pointer type from this type by nesting it in a <see cref="ComposedType"/>.
142
/// If this type already is a pointer type, this method just increases the PointerRank of the existing pointer type.
144
public virtual AstType MakePointerType()
146
return new ComposedType { BaseType = this }.MakePointerType();
150
/// Creates an array type from this type by nesting it in a <see cref="ComposedType"/>.
151
/// If this type already is an array type, the additional rank is prepended to the existing array specifier list.
152
/// Thus, <c>new SimpleType("T").MakeArrayType(1).MakeArrayType(2)</c> will result in "T[,][]".
154
public virtual AstType MakeArrayType(int rank = 1)
156
return new ComposedType { BaseType = this }.MakeArrayType(rank);
160
/// Creates a nullable type from this type by nesting it in a <see cref="ComposedType"/>.
162
public AstType MakeNullableType()
164
return new ComposedType { BaseType = this, HasNullableSpecifier = true };
168
/// Builds an expression that can be used to access a static member on this type.
170
public MemberReferenceExpression Member(string memberName)
172
return new TypeReferenceExpression { Type = this }.Member(memberName);
176
/// Builds an expression that can be used to access a static member on this type.
178
public MemberType MemberType(string memberName, params AstType[] typeArguments)
180
var memberType = new MemberType(this, memberName);
181
memberType.TypeArguments.AddRange(typeArguments);
186
/// Builds an expression that can be used to access a static member on this type.
188
public MemberType MemberType(string memberName, IEnumerable<AstType> typeArguments)
190
var memberType = new MemberType(this, memberName);
191
memberType.TypeArguments.AddRange(typeArguments);
196
/// Builds an invocation expression using this type as target.
198
public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
200
return new TypeReferenceExpression { Type = this }.Invoke(methodName, arguments);
204
/// Builds an invocation expression using this type as target.
206
public InvocationExpression Invoke(string methodName, params Expression[] arguments)
208
return new TypeReferenceExpression { Type = this }.Invoke(methodName, arguments);
212
/// Builds an invocation expression using this type as target.
214
public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
216
return new TypeReferenceExpression { Type = this }.Invoke(methodName, typeArguments, arguments);