Browse Source

Fixed resolving NamedExpressions in anonymous type creation expressions.

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
418f7090cb
  1. 2
      ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs
  2. 7
      ICSharpCode.NRefactory.CSharp/Resolver/ResolveVisitor.cs
  3. 64
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/AnonymousTypeTests.cs
  4. 3
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
  5. 17
      ICSharpCode.NRefactory/TypeSystem/AnonymousType.cs
  6. 3
      ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMember.cs

2
ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs

@ -1127,7 +1127,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -1127,7 +1127,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
this.nonLiftedOperator = nonLiftedMethod;
// Comparison operators keep the 'bool' return type even when lifted.
if (IsComparisonOperator(nonLiftedMethod))
this.returnType = nonLiftedMethod.ReturnType;
this.ReturnType = nonLiftedMethod.ReturnType;
}
public IList<IParameter> NonLiftedParameters {

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

@ -948,7 +948,6 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -948,7 +948,6 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
// 7.6.10.6 Anonymous object creation expressions
List<IUnresolvedProperty> properties = new List<IUnresolvedProperty>();
foreach (var expr in anonymousTypeCreateExpression.Initializers) {
Scan(expr);
Expression resolveExpr;
var name = GetAnonymousTypePropertyName(expr, out resolveExpr);
if (!string.IsNullOrEmpty(name)) {
@ -966,7 +965,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -966,7 +965,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
properties.Add(property);
}
}
return new ResolveResult(new AnonymousType(resolver.Compilation, properties));
IType anonymousType = new AnonymousType(resolver.Compilation, properties);
resolver = resolver.PushInitializerType(anonymousType);
ScanChildren(anonymousTypeCreateExpression);
resolver = resolver.PopInitializerType();
return new ResolveResult(anonymousType);
}
sealed class VarTypeReference : ITypeReference

64
ICSharpCode.NRefactory.Tests/CSharp/Resolver/AnonymousTypeTests.cs

@ -0,0 +1,64 @@ @@ -0,0 +1,64 @@
// 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 System.Linq;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
[TestFixture]
public class AnonymousTypeTests : ResolverTestBase
{
const string programStart = @"using System;
using System.Collections.Generic;
using System.Linq;
class Test {
void M(IEnumerable<string> list1, IEnumerable<int> list2) {
";
const string programEnd = " } }";
[Test]
public void Zip()
{
string program = programStart + "$var$ q = list1.Zip(list2, (a,b) => new { a, b });" + programEnd;
var rr = Resolve<TypeResolveResult>(program);
Assert.AreEqual("System.Collections.Generic.IEnumerable", rr.Type.FullName);
var type = (AnonymousType)((ParameterizedType)rr.Type).TypeArguments.Single();
Assert.AreEqual(TypeKind.Anonymous, type.Kind);
Assert.AreEqual(2, type.Properties.Count);
Assert.AreEqual("a", type.Properties[0].Name);
Assert.AreEqual("b", type.Properties[1].Name);
Assert.AreEqual("System.String", type.Properties[0].ReturnType.ReflectionName);
Assert.AreEqual("System.Int32", type.Properties[1].ReturnType.ReflectionName);
}
[Test]
public void ZipItem1()
{
string program = programStart + "var q = list1.Zip(list2, (a,b) => new { $Item1 = a$, Item2 = b });" + programEnd;
var rr = Resolve<MemberResolveResult>(program);
Assert.AreEqual(TypeKind.Anonymous, rr.Member.DeclaringType.Kind);
Assert.AreEqual("Item1", rr.Member.Name);
Assert.AreEqual(EntityType.Property, rr.Member.EntityType);
Assert.AreEqual("System.String", rr.Member.ReturnType.FullName);
}
}
}

3
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{63D3B27A-D966-4902-90B3-30290E1692F1}</ProjectGuid>
@ -146,6 +146,7 @@ @@ -146,6 +146,7 @@
<Compile Include="CSharp\Parser\GeneralScope\AttributeSectionTests.cs" />
<Compile Include="CSharp\Parser\ParseUtil.cs" />
<Compile Include="CSharp\Refactoring\TypeSystemAstBuilderTests.cs" />
<Compile Include="CSharp\Resolver\AnonymousTypeTests.cs" />
<Compile Include="CSharp\Resolver\ArrayCreateTests.cs" />
<Compile Include="CSharp\Resolver\AttributeTests.cs" />
<Compile Include="CSharp\Resolver\BinaryOperatorTests.cs" />

17
ICSharpCode.NRefactory/TypeSystem/AnonymousType.cs

@ -42,7 +42,22 @@ namespace ICSharpCode.NRefactory.TypeSystem @@ -42,7 +42,22 @@ namespace ICSharpCode.NRefactory.TypeSystem
this.compilation = compilation;
this.unresolvedProperties = properties.ToArray();
var context = new SimpleTypeResolveContext(compilation.MainAssembly);
this.resolvedProperties = new ProjectedList<ITypeResolveContext, IUnresolvedProperty, IProperty>(context, unresolvedProperties, (c, p) => (IProperty)p.CreateResolved(c));
this.resolvedProperties = new ProjectedList<ITypeResolveContext, IUnresolvedProperty, IProperty>(context, unresolvedProperties, (c, p) => new AnonymousTypeProperty(p, c, this));
}
sealed class AnonymousTypeProperty : DefaultResolvedProperty, IEntity
{
readonly AnonymousType declaringType;
public AnonymousTypeProperty(IUnresolvedProperty unresolved, ITypeResolveContext parentContext, AnonymousType declaringType)
: base(unresolved, parentContext)
{
this.declaringType = declaringType;
}
IType IEntity.DeclaringType {
get { return declaringType; }
}
}
public override ITypeReference ToTypeReference()

3
ICSharpCode.NRefactory/TypeSystem/Implementation/SpecializedMember.cs

@ -30,7 +30,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -30,7 +30,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
{
readonly IType declaringType;
readonly IMember memberDefinition;
protected IType returnType;
IType returnType;
protected SpecializedMember(IType declaringType, IMember memberDefinition)
{
@ -92,6 +92,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation @@ -92,6 +92,7 @@ namespace ICSharpCode.NRefactory.TypeSystem.Implementation
public IType ReturnType {
get { return returnType; }
protected set { returnType = value; }
}
public bool IsVirtual {

Loading…
Cancel
Save