PageRenderTime 34ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/NRefactory/ICSharpCode.NRefactory.VB/Ast/TypeName/AstType.cs

http://github.com/icsharpcode/ILSpy
C# | 163 lines | 126 code | 20 blank | 17 comment | 6 complexity | 5f109f26bf2dbc442bc57e45e8c8385f MD5 | raw file
  1// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2// This code is distributed under MIT X11 license (for details please see \doc\license.txt)
  3using System;
  4using System.Collections.Generic;
  5using System.Linq;
  6
  7namespace ICSharpCode.NRefactory.VB.Ast
  8{
  9	/// <summary>
 10	/// A type reference in the VB AST.
 11	/// </summary>
 12	public abstract class AstType : AstNode
 13	{
 14		#region Null
 15		public new static readonly AstType Null = new NullAstType();
 16		
 17		sealed class NullAstType : AstType
 18		{
 19			public override bool IsNull {
 20				get {
 21					return true;
 22				}
 23			}
 24			
 25			public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
 26			{
 27				return default (S);
 28			}
 29			
 30			protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 31			{
 32				return other == null || other.IsNull;
 33			}
 34		}
 35		#endregion
 36		
 37		#region PatternPlaceholder
 38		public static implicit operator AstType(PatternMatching.Pattern pattern)
 39		{
 40			return pattern != null ? new PatternPlaceholder(pattern) : null;
 41		}
 42		
 43		sealed class PatternPlaceholder : AstType, PatternMatching.INode
 44		{
 45			readonly PatternMatching.Pattern child;
 46			
 47			public PatternPlaceholder(PatternMatching.Pattern child)
 48			{
 49				this.child = child;
 50			}
 51			
 52			public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
 53			{
 54				return visitor.VisitPatternPlaceholder(this, child, data);
 55			}
 56			
 57			protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
 58			{
 59				return child.DoMatch(other, match);
 60			}
 61			
 62			bool PatternMatching.INode.DoMatchCollection(Role role, PatternMatching.INode pos, PatternMatching.Match match, PatternMatching.BacktrackingInfo backtrackingInfo)
 63			{
 64				return child.DoMatchCollection(role, pos, match, backtrackingInfo);
 65			}
 66		}
 67		#endregion
 68		
 69		public virtual AstType MakeArrayType(int rank = 1)
 70		{
 71			return new ComposedType { BaseType = this }.MakeArrayType(rank);
 72		}
 73		
 74		public static AstType FromName(string fullName)
 75		{
 76			if (string.IsNullOrEmpty(fullName))
 77				throw new ArgumentNullException("fullName");
 78			fullName = fullName.Trim();
 79			if (!fullName.Contains("."))
 80				return new SimpleType(fullName);
 81			string[] parts = fullName.Split('.');
 82			
 83			AstType type = new SimpleType(parts.First());
 84			
 85			foreach (var part in parts.Skip(1)) {
 86				type = new QualifiedType(type, part);
 87			}
 88			
 89			return type;
 90		}
 91		
 92		/// <summary>
 93		/// Builds an expression that can be used to access a static member on this type.
 94		/// </summary>
 95		public MemberAccessExpression Member(string memberName)
 96		{
 97			return new TypeReferenceExpression { Type = this }.Member(memberName);
 98		}
 99		
100		/// <summary>
101		/// Builds an invocation expression using this type as target.
102		/// </summary>
103		public InvocationExpression Invoke(string methodName, IEnumerable<Expression> arguments)
104		{
105			return new TypeReferenceExpression { Type = this }.Invoke(methodName, arguments);
106		}
107		
108		/// <summary>
109		/// Builds an invocation expression using this type as target.
110		/// </summary>
111		public InvocationExpression Invoke(string methodName, params Expression[] arguments)
112		{
113			return new TypeReferenceExpression { Type = this }.Invoke(methodName, arguments);
114		}
115		
116		/// <summary>
117		/// Builds an invocation expression using this type as target.
118		/// </summary>
119		public InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
120		{
121			return new TypeReferenceExpression { Type = this }.Invoke(methodName, typeArguments, arguments);
122		}
123		
124		public static AstType Create(Type type)
125		{
126			switch (Type.GetTypeCode(type)) {
127				case TypeCode.Object:
128					return new PrimitiveType("Object");
129				case TypeCode.Boolean:
130					return new PrimitiveType("Boolean");
131				case TypeCode.Char:
132					return new PrimitiveType("Char");
133				case TypeCode.SByte:
134					return new PrimitiveType("SByte");
135				case TypeCode.Byte:
136					return new PrimitiveType("Byte");
137				case TypeCode.Int16:
138					return new PrimitiveType("Short");
139				case TypeCode.UInt16:
140					return new PrimitiveType("UShort");
141				case TypeCode.Int32:
142					return new PrimitiveType("Integer");
143				case TypeCode.UInt32:
144					return new PrimitiveType("UInteger");
145				case TypeCode.Int64:
146					return new PrimitiveType("Long");
147				case TypeCode.UInt64:
148					return new PrimitiveType("ULong");
149				case TypeCode.Single:
150					return new PrimitiveType("Single");
151				case TypeCode.Double:
152					return new PrimitiveType("Double");
153				case TypeCode.Decimal:
154					return new PrimitiveType("Decimal");
155				case TypeCode.String:
156					return new PrimitiveType("String");
157				case TypeCode.DateTime:
158					return new PrimitiveType("Date");
159			}
160			return new SimpleType(type.FullName); // TODO: implement this correctly
161		}
162	}
163}