Browse Source

Add tuple types to type system and syntax tree.

pull/1134/head
Daniel Grunwald 7 years ago
parent
commit
d78d423d10
  1. 15
      ICSharpCode.Decompiler.Tests/Semantics/ConversionTests.cs
  2. 39
      ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs
  3. 5
      ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs
  4. 38
      ICSharpCode.Decompiler/CSharp/Resolver/CSharpOperators.cs
  5. 12
      ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs
  6. 34
      ICSharpCode.Decompiler/CSharp/Syntax/DepthFirstAstVisitor.cs
  7. 51
      ICSharpCode.Decompiler/CSharp/Syntax/Expressions/TupleExpression.cs
  8. 6
      ICSharpCode.Decompiler/CSharp/Syntax/IAstVisitor.cs
  9. 63
      ICSharpCode.Decompiler/CSharp/Syntax/TupleAstType.cs
  10. 18
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs
  11. 3
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  12. 6
      ICSharpCode.Decompiler/TypeSystem/Implementation/SpecializedMember.cs
  13. 5
      ICSharpCode.Decompiler/TypeSystem/ParameterListComparer.cs
  14. 2
      ICSharpCode.Decompiler/TypeSystem/ParameterizedType.cs
  15. 271
      ICSharpCode.Decompiler/TypeSystem/TupleType.cs
  16. 6
      ICSharpCode.Decompiler/TypeSystem/TypeKind.cs
  17. 7
      ICSharpCode.Decompiler/TypeSystem/TypeVisitor.cs
  18. 20
      ICSharpCode.Decompiler/Util/CollectionExtensions.cs

15
ICSharpCode.Decompiler.Tests/Semantics/ConversionTests.cs

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using ICSharpCode.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.Semantics;
using ICSharpCode.Decompiler.Tests.TypeSystem;
@ -88,6 +89,20 @@ namespace ICSharpCode.Decompiler.Tests.Semantics @@ -88,6 +89,20 @@ namespace ICSharpCode.Decompiler.Tests.Semantics
Assert.AreEqual(C.None, ImplicitConversion(typeof(List<List<object>[,]>), typeof(List<List<dynamic>[]>)));
}
[Test]
public void TupleIdentityConversions()
{
var intType = compilation.FindType(typeof(int));
var stringType = compilation.FindType(typeof(string));
Assert.AreEqual(C.IdentityConversion, conversions.ImplicitConversion(
new TupleType(compilation, ImmutableArray.Create(intType, stringType), ImmutableArray.Create("a", "b")),
new TupleType(compilation, ImmutableArray.Create(intType, stringType), ImmutableArray.Create("a", "c"))));
Assert.AreEqual(C.None, conversions.ImplicitConversion(
new TupleType(compilation, ImmutableArray.Create(intType, stringType), ImmutableArray.Create("a", "b")),
new TupleType(compilation, ImmutableArray.Create(stringType, intType), ImmutableArray.Create("a", "b"))));
}
[Test]
public void PrimitiveConversions()
{

39
ICSharpCode.Decompiler/CSharp/OutputVisitor/CSharpOutputVisitor.cs

@ -24,6 +24,7 @@ using System.Linq; @@ -24,6 +24,7 @@ using System.Linq;
using ICSharpCode.Decompiler.CSharp.Syntax;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
using ICSharpCode.Decompiler.TypeSystem;
using ICSharpCode.Decompiler.Util;
using Attribute = ICSharpCode.Decompiler.CSharp.Syntax.Attribute;
namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
@ -1061,7 +1062,17 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -1061,7 +1062,17 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
throwExpression.Expression.AcceptVisitor(this);
EndNode(throwExpression);
}
public virtual void VisitTupleExpression(TupleExpression tupleExpression)
{
Debug.Assert(tupleExpression.Elements.Count >= 2);
StartNode(tupleExpression);
LPar();
WriteCommaSeparatedList(tupleExpression.Elements);
RPar();
EndNode(tupleExpression);
}
public virtual void VisitTypeOfExpression(TypeOfExpression typeOfExpression)
{
StartNode(typeOfExpression);
@ -2269,7 +2280,31 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor @@ -2269,7 +2280,31 @@ namespace ICSharpCode.Decompiler.CSharp.OutputVisitor
WriteTypeArguments(memberType.TypeArguments);
EndNode(memberType);
}
public virtual void VisitTupleType(TupleAstType tupleType)
{
Debug.Assert(tupleType.ElementTypes.Count >= 2);
StartNode(tupleType);
LPar();
if (tupleType.ElementNames.Any()) {
bool isFirst = true;
foreach (var (type, name) in tupleType.ElementTypes.Zip(tupleType.ElementNames)) {
if (isFirst) {
isFirst = false;
} else {
Comma(type);
}
type.AcceptVisitor(this);
Space();
name.AcceptVisitor(this);
}
} else {
WriteCommaSeparatedList(tupleType.ElementTypes);
}
RPar();
EndNode(tupleType);
}
public virtual void VisitComposedType(ComposedType composedType)
{
StartNode(composedType);

5
ICSharpCode.Decompiler/CSharp/Resolver/CSharpConversions.cs

@ -310,6 +310,11 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -310,6 +310,11 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
else
return base.VisitOtherType(type);
}
public override IType VisitTupleType(TupleType type)
{
return type.UnderlyingType;
}
}
#endregion

38
ICSharpCode.Decompiler/CSharp/Resolver/CSharpOperators.cs

@ -1055,8 +1055,10 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -1055,8 +1055,10 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
: base((IMethod)nonLiftedMethod.MemberDefinition, nonLiftedMethod.Substitution)
{
this.nonLiftedOperator = nonLiftedMethod;
var substitution = new MakeNullableVisitor(nonLiftedMethod.Compilation, nonLiftedMethod.Substitution);
this.Parameters = base.CreateParameters(substitution);
var compilation = nonLiftedMethod.Compilation;
var substitution = nonLiftedMethod.Substitution;
this.Parameters = base.CreateParameters(
type => NullableType.Create(compilation, type.AcceptVisitor(substitution)));
// Comparison operators keep the 'bool' return type even when lifted.
if (IsComparisonOperator(nonLiftedMethod))
this.ReturnType = nonLiftedMethod.ReturnType;
@ -1078,38 +1080,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -1078,38 +1080,6 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
return nonLiftedOperator.GetHashCode() ^ 0x7191254;
}
}
sealed class MakeNullableVisitor : TypeVisitor
{
readonly ICompilation compilation;
readonly TypeParameterSubstitution typeParameterSubstitution;
public MakeNullableVisitor(ICompilation compilation, TypeParameterSubstitution typeParameterSubstitution)
{
this.compilation = compilation;
this.typeParameterSubstitution = typeParameterSubstitution;
}
public override IType VisitTypeDefinition(ITypeDefinition type)
{
return NullableType.Create(compilation, type.AcceptVisitor(typeParameterSubstitution));
}
public override IType VisitTypeParameter(ITypeParameter type)
{
return NullableType.Create(compilation, type.AcceptVisitor(typeParameterSubstitution));
}
public override IType VisitParameterizedType(ParameterizedType type)
{
return NullableType.Create(compilation, type.AcceptVisitor(typeParameterSubstitution));
}
public override IType VisitOtherType(IType type)
{
return NullableType.Create(compilation, type.AcceptVisitor(typeParameterSubstitution));
}
}
#endregion
}

12
ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs

@ -593,8 +593,8 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -593,8 +593,8 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
return;
}
// Handle parameterized type:
ParameterizedType pU = U as ParameterizedType;
ParameterizedType pV = V as ParameterizedType;
ParameterizedType pU = U.TupleUnderlyingTypeOrSelf() as ParameterizedType;
ParameterizedType pV = V.TupleUnderlyingTypeOrSelf() as ParameterizedType;
if (pU != null && pV != null
&& object.Equals(pU.GenericType, pV.GenericType)
&& pU.TypeParameterCount == pV.TypeParameterCount)
@ -644,7 +644,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -644,7 +644,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
// Handle array types:
ArrayType arrU = U as ArrayType;
ArrayType arrV = V as ArrayType;
ParameterizedType pV = V as ParameterizedType;
ParameterizedType pV = V.TupleUnderlyingTypeOrSelf() as ParameterizedType;
if (arrU != null && arrV != null && arrU.Dimensions == arrV.Dimensions) {
MakeLowerBoundInference(arrU.ElementType, arrV.ElementType);
return;
@ -656,7 +656,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -656,7 +656,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
if (pV != null) {
ParameterizedType uniqueBaseType = null;
foreach (IType baseU in U.GetAllBaseTypes()) {
ParameterizedType pU = baseU as ParameterizedType;
ParameterizedType pU = baseU.TupleUnderlyingTypeOrSelf() as ParameterizedType;
if (pU != null && object.Equals(pU.GenericType, pV.GenericType) && pU.TypeParameterCount == pV.TypeParameterCount) {
if (uniqueBaseType == null)
uniqueBaseType = pU;
@ -730,7 +730,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -730,7 +730,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
// Handle array types:
ArrayType arrU = U as ArrayType;
ArrayType arrV = V as ArrayType;
ParameterizedType pU = U as ParameterizedType;
ParameterizedType pU = U.TupleUnderlyingTypeOrSelf() as ParameterizedType;
if (arrV != null && arrU != null && arrU.Dimensions == arrV.Dimensions) {
MakeUpperBoundInference(arrU.ElementType, arrV.ElementType);
return;
@ -742,7 +742,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -742,7 +742,7 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
if (pU != null) {
ParameterizedType uniqueBaseType = null;
foreach (IType baseV in V.GetAllBaseTypes()) {
ParameterizedType pV = baseV as ParameterizedType;
ParameterizedType pV = baseV.TupleUnderlyingTypeOrSelf() as ParameterizedType;
if (pV != null && object.Equals(pU.GenericType, pV.GenericType) && pU.TypeParameterCount == pV.TypeParameterCount) {
if (uniqueBaseType == null)
uniqueBaseType = pV;

34
ICSharpCode.Decompiler/CSharp/Syntax/DepthFirstAstVisitor.cs

@ -115,7 +115,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -115,7 +115,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{
VisitChildren (memberType);
}
public virtual void VisitTupleType(TupleAstType tupleType)
{
VisitChildren(tupleType);
}
public virtual void VisitAttribute (Attribute attribute)
{
VisitChildren (attribute);
@ -536,6 +541,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -536,6 +541,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
VisitChildren (throwExpression);
}
public virtual void VisitTupleExpression(TupleExpression tupleExpression)
{
VisitChildren (tupleExpression);
}
public virtual void VisitTypeOfExpression (TypeOfExpression typeOfExpression)
{
VisitChildren (typeOfExpression);
@ -747,6 +757,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -747,6 +757,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{
return VisitChildren (memberType);
}
public virtual T VisitTupleType(TupleAstType tupleType)
{
return VisitChildren (tupleType);
}
public virtual T VisitAttribute (Attribute attribute)
{
@ -1168,6 +1183,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1168,6 +1183,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return VisitChildren (throwExpression);
}
public virtual T VisitTupleExpression (TupleExpression tupleExpression)
{
return VisitChildren (tupleExpression);
}
public virtual T VisitTypeOfExpression (TypeOfExpression typeOfExpression)
{
return VisitChildren (typeOfExpression);
@ -1379,7 +1399,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1379,7 +1399,12 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
{
return VisitChildren (memberType, data);
}
public virtual S VisitTupleType(TupleAstType tupleType, T data)
{
return VisitChildren (tupleType, data);
}
public virtual S VisitAttribute (Attribute attribute, T data)
{
return VisitChildren (attribute, data);
@ -1800,6 +1825,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1800,6 +1825,11 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return VisitChildren (throwExpression, data);
}
public virtual S VisitTupleExpression (TupleExpression tupleExpression, T data)
{
return VisitChildren (tupleExpression, data);
}
public virtual S VisitTypeOfExpression (TypeOfExpression typeOfExpression, T data)
{
return VisitChildren (typeOfExpression, data);

51
ICSharpCode.Decompiler/CSharp/Syntax/Expressions/TupleExpression.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// Copyright (c) 2018 Daniel Grunwald
//
// 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.Decompiler.CSharp.Syntax.PatternMatching;
namespace ICSharpCode.Decompiler.CSharp.Syntax
{
public class TupleExpression : Expression
{
public AstNodeCollection<Expression> Elements {
get { return GetChildrenByRole(Roles.Expression); }
}
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitTupleExpression(this);
}
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{
return visitor.VisitTupleExpression(this);
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitTupleExpression(this, data);
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return other is TupleExpression tuple
&& Elements.DoMatch(tuple.Elements, match);
}
}
}

6
ICSharpCode.Decompiler/CSharp/Syntax/IAstVisitor.cs

@ -56,6 +56,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -56,6 +56,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
void VisitStackAllocExpression(StackAllocExpression stackAllocExpression);
void VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression);
void VisitThrowExpression(ThrowExpression throwExpression);
void VisitTupleExpression(TupleExpression tupleExpression);
void VisitTypeOfExpression(TypeOfExpression typeOfExpression);
void VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression);
void VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression);
@ -133,6 +134,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -133,6 +134,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
void VisitSyntaxTree(SyntaxTree syntaxTree);
void VisitSimpleType(SimpleType simpleType);
void VisitMemberType(MemberType memberType);
void VisitTupleType(TupleAstType tupleType);
void VisitComposedType(ComposedType composedType);
void VisitArraySpecifier(ArraySpecifier arraySpecifier);
void VisitPrimitiveType(PrimitiveType primitiveType);
@ -194,6 +196,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -194,6 +196,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
S VisitStackAllocExpression(StackAllocExpression stackAllocExpression);
S VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression);
S VisitThrowExpression(ThrowExpression throwExpression);
S VisitTupleExpression(TupleExpression tupleExpression);
S VisitTypeOfExpression(TypeOfExpression typeOfExpression);
S VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression);
S VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression);
@ -271,6 +274,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -271,6 +274,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
S VisitSyntaxTree(SyntaxTree syntaxTree);
S VisitSimpleType(SimpleType simpleType);
S VisitMemberType(MemberType memberType);
S VisitTupleType(TupleAstType tupleType);
S VisitComposedType(ComposedType composedType);
S VisitArraySpecifier(ArraySpecifier arraySpecifier);
S VisitPrimitiveType(PrimitiveType primitiveType);
@ -332,6 +336,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -332,6 +336,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
S VisitStackAllocExpression(StackAllocExpression stackAllocExpression, T data);
S VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression, T data);
S VisitThrowExpression(ThrowExpression throwExpression, T data);
S VisitTupleExpression(TupleExpression tupleExpression, T data);
S VisitTypeOfExpression(TypeOfExpression typeOfExpression, T data);
S VisitTypeReferenceExpression(TypeReferenceExpression typeReferenceExpression, T data);
S VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, T data);
@ -409,6 +414,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -409,6 +414,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
S VisitSyntaxTree(SyntaxTree syntaxTree, T data);
S VisitSimpleType(SimpleType simpleType, T data);
S VisitMemberType(MemberType memberType, T data);
S VisitTupleType(TupleAstType tupleType, T data);
S VisitComposedType(ComposedType composedType, T data);
S VisitArraySpecifier(ArraySpecifier arraySpecifier, T data);
S VisitPrimitiveType(PrimitiveType primitiveType, T data);

63
ICSharpCode.Decompiler/CSharp/Syntax/TupleAstType.cs

@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
// Copyright (c) 2018 Daniel Grunwald
//
// 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.Decompiler.CSharp.Resolver;
using ICSharpCode.Decompiler.CSharp.Syntax.PatternMatching;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.Decompiler.CSharp.Syntax
{
public class TupleAstType : AstType
{
public AstNodeCollection<AstType> ElementTypes {
get { return GetChildrenByRole(Roles.TypeArgument); }
}
public AstNodeCollection<Identifier> ElementNames {
get { return GetChildrenByRole(Roles.Identifier); }
}
public override void AcceptVisitor(IAstVisitor visitor)
{
visitor.VisitTupleType(this);
}
public override T AcceptVisitor<T>(IAstVisitor<T> visitor)
{
return visitor.VisitTupleType(this);
}
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitTupleType(this, data);
}
public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider = null)
{
throw new NotSupportedException();
}
protected internal override bool DoMatch(AstNode other, Match match)
{
return other is TupleAstType o
&& ElementTypes.DoMatch(o.ElementTypes, match)
&& ElementNames.DoMatch(o.ElementNames, match);
}
}
}

18
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -231,15 +231,25 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -231,15 +231,25 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
return ConvertType(typeWithElementType.ElementType);
}
}
ParameterizedType pt = type as ParameterizedType;
if (pt != null) {
if (type is ParameterizedType pt) {
if (pt.IsKnownType(KnownTypeCode.NullableOfT)) {
return ConvertType(pt.TypeArguments[0]).MakeNullableType();
}
return ConvertTypeHelper(pt.GenericType, pt.TypeArguments);
}
ITypeDefinition typeDef = type as ITypeDefinition;
if (typeDef != null) {
if (type is TupleType tuple) {
var astType = new TupleAstType();
if (tuple.HasCustomElementNames) {
foreach (var (etype, ename) in tuple.TupleElementTypes.Zip(tuple.TupleElementNames)) {
astType.ElementTypes.Add(ConvertType(etype));
astType.ElementNames.Add(Identifier.Create(ename));
}
} else {
astType.ElementTypes.AddRange(tuple.TupleElementTypes.Select(ConvertType));
}
return astType;
}
if (type is ITypeDefinition typeDef) {
if (typeDef.TypeParameterCount > 0) {
// Unbound type
IType[] typeArguments = new IType[typeDef.TypeParameterCount];

3
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -107,6 +107,7 @@ @@ -107,6 +107,7 @@
<Compile Include="CSharp\Syntax\Expressions\StackAllocExpression.cs" />
<Compile Include="CSharp\Syntax\Expressions\ThisReferenceExpression.cs" />
<Compile Include="CSharp\Syntax\Expressions\ThrowExpression.cs" />
<Compile Include="CSharp\Syntax\Expressions\TupleExpression.cs" />
<Compile Include="CSharp\Syntax\Expressions\TypeOfExpression.cs" />
<Compile Include="CSharp\Syntax\Expressions\TypeReferenceExpression.cs" />
<Compile Include="CSharp\Syntax\Expressions\UnaryOperatorExpression.cs" />
@ -178,6 +179,7 @@ @@ -178,6 +179,7 @@
<Compile Include="CSharp\Syntax\SyntaxTree.cs" />
<Compile Include="CSharp\Syntax\TextLocation.cs" />
<Compile Include="CSharp\Syntax\TokenRole.cs" />
<Compile Include="CSharp\Syntax\TupleAstType.cs" />
<Compile Include="CSharp\Syntax\TypeMembers\Accessor.cs" />
<Compile Include="CSharp\Syntax\TypeMembers\ConstructorDeclaration.cs" />
<Compile Include="CSharp\Syntax\TypeMembers\DestructorDeclaration.cs" />
@ -323,6 +325,7 @@ @@ -323,6 +325,7 @@
<Compile Include="IL\Transforms\StatementTransform.cs" />
<Compile Include="IL\Transforms\TransformCollectionAndObjectInitializers.cs" />
<Compile Include="Output\TextTokenWriter.cs" />
<Compile Include="TypeSystem\TupleType.cs" />
<Compile Include="Util\GraphVizGraph.cs" />
<Compile Include="Util\KeyComparer.cs" />
<Compile Include="Util\LongDict.cs" />

6
ICSharpCode.Decompiler/TypeSystem/Implementation/SpecializedMember.cs

@ -303,7 +303,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -303,7 +303,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
if (result != null)
return result;
else
return LazyInit.GetOrSet(ref this.parameters, CreateParameters(this.Substitution));
return LazyInit.GetOrSet(ref this.parameters, CreateParameters(t => t.AcceptVisitor(this.Substitution)));
}
protected set {
// This setter is used for LiftedUserDefinedOperator, a special case of specialized member
@ -315,7 +315,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -315,7 +315,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
}
}
protected IParameter[] CreateParameters(TypeVisitor substitution)
protected IParameter[] CreateParameters(Func<IType, IType> substitution)
{
var paramDefs = ((IParameterizedMember)this.baseMember).Parameters;
if (paramDefs.Count == 0) {
@ -324,7 +324,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -324,7 +324,7 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
var parameters = new IParameter[paramDefs.Count];
for (int i = 0; i < parameters.Length; i++) {
var p = paramDefs[i];
IType newType = p.Type.AcceptVisitor(substitution);
IType newType = substitution(p.Type);
parameters[i] = new DefaultParameter(
newType, p.Name, this,
p.Attributes, p.IsRef, p.IsOut,

5
ICSharpCode.Decompiler/TypeSystem/ParameterListComparer.cs

@ -52,6 +52,11 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -52,6 +52,11 @@ namespace ICSharpCode.Decompiler.TypeSystem
return SpecialType.Dynamic;
return base.VisitTypeDefinition(type);
}
public override IType VisitTupleType(TupleType type)
{
return type.UnderlyingType.AcceptVisitor(this);
}
}
static readonly NormalizeTypeVisitor normalizationVisitor = new NormalizeTypeVisitor();

2
ICSharpCode.Decompiler/TypeSystem/ParameterizedType.cs

@ -271,6 +271,8 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -271,6 +271,8 @@ namespace ICSharpCode.Decompiler.TypeSystem
public bool Equals(IType other)
{
if (this == other)
return true;
ParameterizedType c = other as ParameterizedType;
if (c == null || !genericType.Equals(c.genericType) || typeArguments.Length != c.typeArguments.Length)
return false;

271
ICSharpCode.Decompiler/TypeSystem/TupleType.cs

@ -1,17 +1,278 @@ @@ -1,17 +1,278 @@
using System;
// Copyright (c) 2018 Daniel Grunwald
//
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Text;
using System.Diagnostics;
using System.Linq;
using ICSharpCode.Decompiler.TypeSystem.Implementation;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.TypeSystem
{
public sealed class TupleType
public sealed class TupleType : AbstractType, ICompilationProvider
{
public const int RestPosition = 8;
const int RestIndex = RestPosition - 1;
public ICompilation Compilation { get; }
/// <summary>
/// Gets the underlying <c>System.ValueType</c> type.
/// </summary>
public ParameterizedType UnderlyingType { get; }
public ImmutableArray<IField> TupleElements { get; }
/// <summary>
/// Gets the tuple elements.
/// </summary>
public ImmutableArray<IType> TupleElementTypes { get; }
/// <summary>
/// Gets the names of the tuple elements.
/// </summary>
public ImmutableArray<string> TupleElementNames { get; }
public bool HasCustomElementNames { get; }
public TupleType(ICompilation compilation, ImmutableArray<IType> elementTypes)
{
this.Compilation = compilation;
this.UnderlyingType = CreateUnderlyingType(compilation, elementTypes);
this.TupleElementTypes = elementTypes;
this.TupleElementNames = Enumerable.Range(1, elementTypes.Length).Select(p => "Item" + p).ToImmutableArray();
this.HasCustomElementNames = false;
}
public TupleType(ICompilation compilation, ImmutableArray<IType> elementTypes, ImmutableArray<string> elementNames)
{
this.Compilation = compilation;
this.UnderlyingType = CreateUnderlyingType(compilation, elementTypes);
this.TupleElementTypes = elementTypes;
this.TupleElementNames = elementNames;
this.HasCustomElementNames = true;
}
static ParameterizedType CreateUnderlyingType(ICompilation compilation, ImmutableArray<IType> elementTypes)
{
int remainder = (elementTypes.Length - 1) % (RestPosition - 1) + 1;
Debug.Assert(remainder >= 1 && remainder < RestPosition);
int pos = elementTypes.Length - remainder;
var type = new ParameterizedType(
compilation.FindType(new TopLevelTypeName("System", "ValueTuple", remainder)),
elementTypes.Slice(pos));
while (pos > 0) {
pos -= (RestPosition - 1);
type = new ParameterizedType(
compilation.FindType(new TopLevelTypeName("System", "ValueTuple", RestPosition)),
elementTypes.Slice(pos, RestPosition - 1).Concat(new[] { type }));
}
Debug.Assert(pos == 0);
return type;
}
/// <summary>
/// Gets whether the specified type is a valid underlying type for a tuple.
/// Also returns type for tuple types themselves.
/// </summary>
public static bool IsTupleCompatible(IType type, out int tupleCardinality)
{
switch (type.Kind) {
case TypeKind.Tuple:
tupleCardinality = ((TupleType)type).TupleElementNames.Length;
return true;
case TypeKind.Class:
case TypeKind.Struct:
if (type.Namespace == "System" && type.Name == "ValueType") {
int tpc = type.TypeParameterCount;
if (tpc > 0 && tpc < RestPosition) {
tupleCardinality = tpc;
return true;
} else if (tpc == RestPosition && type is ParameterizedType pt) {
if (IsTupleCompatible(pt.TypeArguments[RestIndex], out tupleCardinality)) {
tupleCardinality += RestPosition - 1;
return true;
}
}
}
break;
}
tupleCardinality = 0;
return false;
}
static bool CollectTupleElementTypes(IType type, List<IType> output)
{
switch (type.Kind) {
case TypeKind.Tuple:
output.AddRange(((TupleType)type).TupleElementTypes);
return true;
case TypeKind.Class:
case TypeKind.Struct:
if (type.Namespace == "System" && type.Name == "ValueType") {
int tpc = type.TypeParameterCount;
if (tpc > 0 && tpc < RestPosition) {
output.AddRange(type.TypeArguments);
return true;
} else if (tpc == RestPosition) {
output.AddRange(type.TypeArguments.Take(RestPosition - 1));
return CollectTupleElementTypes(type.TypeArguments[RestIndex], output);
}
}
break;
}
return false;
}
public override TypeKind Kind => TypeKind.Tuple;
public override bool? IsReferenceType => UnderlyingType.IsReferenceType;
public override int TypeParameterCount => 0;
public override IReadOnlyList<ITypeParameter> TypeParameters => EmptyList<ITypeParameter>.Instance;
public override IReadOnlyList<IType> TypeArguments => EmptyList<IType>.Instance;
public override IEnumerable<IType> DirectBaseTypes => UnderlyingType.DirectBaseTypes;
public override string FullName => UnderlyingType.FullName;
public override string Name => UnderlyingType.Name;
public override string ReflectionName => UnderlyingType.ReflectionName;
public override string Namespace => UnderlyingType.Namespace;
public override bool Equals(IType other)
{
var o = other as TupleType;
if (o == null)
return false;
if (!UnderlyingType.Equals(o.UnderlyingType))
return false;
return UnderlyingType.Equals(o.UnderlyingType)
&& TupleElementNames.SequenceEqual(o.TupleElementNames);
}
public override int GetHashCode()
{
unchecked {
int hash = UnderlyingType.GetHashCode();
foreach (string name in TupleElementNames) {
hash *= 31;
hash += name.GetHashCode();
}
return hash;
}
}
public override IType AcceptVisitor(TypeVisitor visitor)
{
return visitor.VisitTupleType(this);
}
public override IType VisitChildren(TypeVisitor visitor)
{
IType[] newElementTypes = null;
for (int i = 0; i < TupleElementTypes.Length; i++) {
IType type = TupleElementTypes[i];
var newType = type.AcceptVisitor(visitor);
if (newType != type) {
if (newElementTypes == null) {
newElementTypes = TupleElementTypes.ToArray();
}
newElementTypes[i] = newType;
}
}
if (newElementTypes != null) {
return new TupleType(this.Compilation, newElementTypes.ToImmutableArray(), this.TupleElementNames);
} else {
return this;
}
}
public override IEnumerable<IMethod> GetAccessors(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetAccessors(filter, options);
}
public override IEnumerable<IMethod> GetConstructors(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.IgnoreInheritedMembers)
{
// CS8181 'new' cannot be used with tuple type. Use a tuple literal expression instead.
return EmptyList<IMethod>.Instance;
}
public override ITypeDefinition GetDefinition()
{
return UnderlyingType.GetDefinition();
}
public override IEnumerable<IEvent> GetEvents(Predicate<IUnresolvedEvent> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetEvents(filter, options);
}
public override IEnumerable<IField> GetFields(Predicate<IUnresolvedField> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
// The fields from the underlying type (Item1..Item7 and Rest)
foreach (var field in UnderlyingType.GetFields(filter, options)) {
yield return field;
}
for (int i = 0; i <= TupleElementTypes.Length; i++) {
var type = TupleElementTypes[i];
var name = TupleElementNames[i];
int pos = i + 1;
string itemName = "Item" + pos;
if (name != itemName)
yield return MakeField(type, name);
if (pos >= RestPosition)
yield return MakeField(type, itemName);
}
}
private IField MakeField(IType type, string name)
{
throw new NotImplementedException();
}
public override IEnumerable<IMethod> GetMethods(Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetMethods(filter, options);
}
public override IEnumerable<IMethod> GetMethods(IReadOnlyList<IType> typeArguments, Predicate<IUnresolvedMethod> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetMethods(typeArguments, filter, options);
}
public override IEnumerable<IType> GetNestedTypes(Predicate<ITypeDefinition> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetNestedTypes(filter, options);
}
public override IEnumerable<IType> GetNestedTypes(IReadOnlyList<IType> typeArguments, Predicate<ITypeDefinition> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetNestedTypes(typeArguments, filter, options);
}
public override IEnumerable<IProperty> GetProperties(Predicate<IUnresolvedProperty> filter = null, GetMemberOptions options = GetMemberOptions.None)
{
return UnderlyingType.GetProperties(filter, options);
}
}
public static class TupleTypeExtensions
{
public static IType TupleUnderlyingTypeOrSelf(this IType type)
{
return (type as TupleType)?.UnderlyingType ?? type;
}
}
}

6
ICSharpCode.Decompiler/TypeSystem/TypeKind.cs

@ -81,5 +81,11 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -81,5 +81,11 @@ namespace ICSharpCode.Decompiler.TypeSystem
Intersection,
/// <see cref="SpecialType.ArgList"/>
ArgList,
/// <summary>A C# 7 tuple type.
/// E.g. <code>(string, int)</code>
/// Note: <code>System.ValueTuple&lt;string, int&gt;</code> is not considered a tuple type.
/// </summary>
/// <see cref="TupleType"/>
Tuple,
}
}

7
ICSharpCode.Decompiler/TypeSystem/TypeVisitor.cs

@ -52,7 +52,12 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -52,7 +52,12 @@ namespace ICSharpCode.Decompiler.TypeSystem
{
return type.VisitChildren(this);
}
public virtual IType VisitTupleType(TupleType type)
{
return type.VisitChildren(this);
}
public virtual IType VisitOtherType(IType type)
{
return type.VisitChildren(this);

20
ICSharpCode.Decompiler/Util/CollectionExtensions.cs

@ -12,6 +12,26 @@ namespace ICSharpCode.Decompiler.Util @@ -12,6 +12,26 @@ namespace ICSharpCode.Decompiler.Util
value = pair.Value;
}
public static IEnumerable<(A, B)> Zip<A, B>(this IEnumerable<A> input1, IEnumerable<B> input2)
{
return input1.Zip(input2, (a, b) => (a, b));
}
public static IEnumerable<T> Slice<T>(this IReadOnlyList<T> input, int offset, int length)
{
for (int i = offset; i < offset + length; i++) {
yield return input[i];
}
}
public static IEnumerable<T> Slice<T>(this IReadOnlyList<T> input, int offset)
{
int length = input.Count;
for (int i = offset; i < length; i++) {
yield return input[i];
}
}
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> input)
{
return new HashSet<T>(input);

Loading…
Cancel
Save