Permalink
Browse files

Fix icsharpcode/NRefactory#17: Resolving "is" and "as" expressions lo…

…ses the semantics
  • Loading branch information...
1 parent 2ede14a commit fb0dbc9bee732dd42d9c95ab44391e9edfc3fe3a @dgrunwald dgrunwald committed Mar 7, 2012
@@ -1117,8 +1117,9 @@ ResolveResult IAstVisitor<ResolveResult>.VisitArrayInitializerExpression(ArrayIn
ResolveResult IAstVisitor<ResolveResult>.VisitAsExpression(AsExpression asExpression)
{
if (resolverEnabled) {
- Scan(asExpression.Expression);
- return new ResolveResult(ResolveType(asExpression.Type));
+ ResolveResult input = Resolve(asExpression.Expression);
+ var targetType = ResolveType(asExpression.Type);
+ return new ConversionResolveResult(targetType, input, Conversion.TryCast);
} else {
ScanChildren(asExpression);
return null;
@@ -1267,8 +1268,15 @@ ResolveResult IAstVisitor<ResolveResult>.VisitIndexerExpression(IndexerExpressio
ResolveResult IAstVisitor<ResolveResult>.VisitIsExpression(IsExpression isExpression)
{
- ScanChildren(isExpression);
- return new ResolveResult(resolver.Compilation.FindType(KnownTypeCode.Boolean));
+ if (resolverEnabled) {
+ ResolveResult input = Resolve(isExpression.Expression);
+ IType targetType = ResolveType(isExpression.Type);
+ IType booleanType = resolver.Compilation.FindType(KnownTypeCode.Boolean);
+ return new TypeIsResolveResult(input, targetType, booleanType);
+ } else {
+ ScanChildren(isExpression);
+ return null;
+ }
}
// NamedArgumentExpression is "identifier: Expression"
@@ -98,6 +98,7 @@
<Compile Include="Semantics\OperatorResolveResult.cs" />
<Compile Include="Semantics\ResolveResult.cs" />
<Compile Include="Semantics\ThisResolveResult.cs" />
+ <Compile Include="Semantics\TypeIsResolveResult.cs" />
<Compile Include="Semantics\TypeOfResolveResult.cs" />
<Compile Include="Semantics\TypeResolveResult.cs" />
<Compile Include="Semantics\UnknownMemberResolveResult.cs" />
@@ -69,6 +69,11 @@ public static Conversion EnumerationConversion(bool isImplicit, bool isLifted)
public static readonly Conversion BoxingConversion = new BuiltinConversion(true, 7);
public static readonly Conversion UnboxingConversion = new BuiltinConversion(false, 8);
+ /// <summary>
+ /// C# 'as' cast.
+ /// </summary>
+ public static readonly Conversion TryCast = new BuiltinConversion(false, 9);
+
public static Conversion UserDefinedImplicitConversion(IMethod operatorMethod, bool isLifted)
{
if (operatorMethod == null)
@@ -204,6 +209,10 @@ public BuiltinConversion(bool isImplicit, byte type)
get { return type == 8; }
}
+ public override bool IsTryCast {
+ get { return type == 9; }
+ }
+
public override string ToString()
{
string name = null;
@@ -231,6 +240,8 @@ public override string ToString()
return "boxing conversion";
case 8:
return "unboxing conversion";
+ case 9:
+ return "try cast";
}
return (isImplicit ? "implicit " : "explicit ") + name + " conversion";
}
@@ -337,6 +348,13 @@ public override int GetHashCode()
get { return false; }
}
+ /// <summary>
+ /// Gets whether the conversion is an '<c>as</c>' cast.
+ /// </summary>
+ public virtual bool IsTryCast {
+ get { return false; }
+ }
+
public virtual bool IsIdentityConversion {
get { return false; }
}
@@ -0,0 +1,47 @@
+// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this
+// software and associated documentation files (the "Software"), to deal in the Software
+// without restriction, including without limitation the rights to use, copy, modify, merge,
+// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
+// to whom the Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all copies or
+// substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
+// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
+// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+
+using System;
+using ICSharpCode.NRefactory.TypeSystem;
+
+namespace ICSharpCode.NRefactory.Semantics
+{
+ /// <summary>
+ /// Resolve result for a C# 'is' expression.
+ /// "Input is TargetType".
+ /// </summary>
+ public class TypeIsResolveResult : ResolveResult
+ {
+ public readonly ResolveResult Input;
+ /// <summary>
+ /// Type that is being compared with.
+ /// </summary>
+ public readonly IType TargetType;
+
+ public TypeIsResolveResult(ResolveResult input, IType targetType, IType booleanType)
+ : base(booleanType)
+ {
+ if (input == null)
+ throw new ArgumentNullException("input");
+ if (targetType == null)
+ throw new ArgumentNullException("targetType");
+ this.Input = input;
+ this.TargetType = targetType;
+ }
+ }
+}

0 comments on commit fb0dbc9

Please sign in to comment.