/NRefactory/ICSharpCode.NRefactory.CSharp/Ast/Expressions/CastExpression.cs

http://github.com/icsharpcode/ILSpy · C# · 152 lines · 104 code · 20 blank · 28 comment · 3 complexity · 5dfbbd1fdf3478f2fb26d20aa6455615 MD5 · raw file

  1. //
  2. // CastExpression.cs
  3. //
  4. // Author:
  5. // Mike Krüger <mkrueger@novell.com>
  6. //
  7. // Copyright (c) 2009 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System.Collections.Generic;
  27. using System;
  28. namespace ICSharpCode.NRefactory.CSharp
  29. {
  30. /// <summary>
  31. /// (CastTo)Expression
  32. /// </summary>
  33. public class CastExpression : Expression
  34. {
  35. public CSharpTokenNode LParToken {
  36. get { return GetChildByRole (Roles.LPar); }
  37. }
  38. public AstType Type {
  39. get { return GetChildByRole (Roles.Type); }
  40. set { SetChildByRole (Roles.Type, value); }
  41. }
  42. public CSharpTokenNode RParToken {
  43. get { return GetChildByRole (Roles.RPar); }
  44. }
  45. public Expression Expression {
  46. get { return GetChildByRole (Roles.Expression); }
  47. set { SetChildByRole (Roles.Expression, value); }
  48. }
  49. public CastExpression ()
  50. {
  51. }
  52. public CastExpression (AstType castToType, Expression expression)
  53. {
  54. AddChild (castToType, Roles.Type);
  55. AddChild (expression, Roles.Expression);
  56. }
  57. public override void AcceptVisitor (IAstVisitor visitor)
  58. {
  59. visitor.VisitCastExpression (this);
  60. }
  61. public override T AcceptVisitor<T> (IAstVisitor<T> visitor)
  62. {
  63. return visitor.VisitCastExpression (this);
  64. }
  65. public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
  66. {
  67. return visitor.VisitCastExpression (this, data);
  68. }
  69. protected internal override bool DoMatch(AstNode other, PatternMatching.Match match)
  70. {
  71. CastExpression o = other as CastExpression;
  72. return o != null && this.Type.DoMatch(o.Type, match) && this.Expression.DoMatch(o.Expression, match);
  73. }
  74. #region Builder methods
  75. public override MemberReferenceExpression Member(string memberName)
  76. {
  77. return new MemberReferenceExpression { Target = this, MemberName = memberName };
  78. }
  79. public override IndexerExpression Indexer(IEnumerable<Expression> arguments)
  80. {
  81. IndexerExpression expr = new IndexerExpression();
  82. expr.Target = new ParenthesizedExpression(this);
  83. expr.Arguments.AddRange(arguments);
  84. return expr;
  85. }
  86. public override IndexerExpression Indexer(params Expression[] arguments)
  87. {
  88. IndexerExpression expr = new IndexerExpression();
  89. expr.Target = new ParenthesizedExpression(this);
  90. expr.Arguments.AddRange(arguments);
  91. return expr;
  92. }
  93. public override InvocationExpression Invoke(string methodName, IEnumerable<AstType> typeArguments, IEnumerable<Expression> arguments)
  94. {
  95. InvocationExpression ie = new InvocationExpression();
  96. MemberReferenceExpression mre = new MemberReferenceExpression();
  97. mre.Target = new ParenthesizedExpression(this);
  98. mre.MemberName = methodName;
  99. mre.TypeArguments.AddRange(typeArguments);
  100. ie.Target = mre;
  101. ie.Arguments.AddRange(arguments);
  102. return ie;
  103. }
  104. public override InvocationExpression Invoke(IEnumerable<Expression> arguments)
  105. {
  106. InvocationExpression ie = new InvocationExpression();
  107. ie.Target = new ParenthesizedExpression(this);
  108. ie.Arguments.AddRange(arguments);
  109. return ie;
  110. }
  111. public override InvocationExpression Invoke(params Expression[] arguments)
  112. {
  113. InvocationExpression ie = new InvocationExpression();
  114. ie.Target = new ParenthesizedExpression(this);
  115. ie.Arguments.AddRange(arguments);
  116. return ie;
  117. }
  118. public override CastExpression CastTo(AstType type)
  119. {
  120. return new CastExpression { Type = type, Expression = new ParenthesizedExpression(this) };
  121. }
  122. public override AsExpression CastAs(AstType type)
  123. {
  124. return new AsExpression { Type = type, Expression = new ParenthesizedExpression(this) };
  125. }
  126. public override IsExpression IsType(AstType type)
  127. {
  128. return new IsExpression { Type = type, Expression = new ParenthesizedExpression(this) };
  129. }
  130. #endregion
  131. }
  132. }