Browse Source

Fix icsharpcode/NRefactory#17: Resolving "is" and "as" expressions loses the semantics

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
fb0dbc9bee
  1. 16
      ICSharpCode.NRefactory.CSharp/Resolver/ResolveVisitor.cs
  2. 1
      ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj
  3. 18
      ICSharpCode.NRefactory/Semantics/Conversion.cs
  4. 47
      ICSharpCode.NRefactory/Semantics/TypeIsResolveResult.cs

16
ICSharpCode.NRefactory.CSharp/Resolver/ResolveVisitor.cs

@ -1117,8 +1117,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1117,8 +1117,9 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
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 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1267,8 +1268,15 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
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"

1
ICSharpCode.NRefactory/ICSharpCode.NRefactory.csproj

@ -98,6 +98,7 @@ @@ -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" />

18
ICSharpCode.NRefactory/Semantics/Conversion.cs

@ -69,6 +69,11 @@ namespace ICSharpCode.NRefactory.Semantics @@ -69,6 +69,11 @@ namespace ICSharpCode.NRefactory.Semantics
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 @@ namespace ICSharpCode.NRefactory.Semantics @@ -204,6 +209,10 @@ namespace ICSharpCode.NRefactory.Semantics
get { return type == 8; }
}
public override bool IsTryCast {
get { return type == 9; }
}
public override string ToString()
{
string name = null;
@ -231,6 +240,8 @@ namespace ICSharpCode.NRefactory.Semantics @@ -231,6 +240,8 @@ namespace ICSharpCode.NRefactory.Semantics
return "boxing conversion";
case 8:
return "unboxing conversion";
case 9:
return "try cast";
}
return (isImplicit ? "implicit " : "explicit ") + name + " conversion";
}
@ -337,6 +348,13 @@ namespace ICSharpCode.NRefactory.Semantics @@ -337,6 +348,13 @@ namespace ICSharpCode.NRefactory.Semantics
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; }
}

47
ICSharpCode.NRefactory/Semantics/TypeIsResolveResult.cs

@ -0,0 +1,47 @@ @@ -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;
}
}
}
Loading…
Cancel
Save