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