-
Notifications
You must be signed in to change notification settings - Fork 4k
/
DeconstructionInfo.cs
79 lines (70 loc) · 3.07 KB
/
DeconstructionInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Immutable;
namespace Microsoft.CodeAnalysis.CSharp
{
// Deconstructions are represented internally as a tree of conversions, but
// since they are not conversions from the language perspective we use a wrapper to
// abstract the public API from the implementation.
/// <summary>
/// The representation of a deconstruction as a tree of Deconstruct methods and conversions.
/// Methods only appear in non-terminal nodes. All terminal nodes have a Conversion.
///
/// Here's an example:
/// A deconstruction like <c>(int x1, (long x2, long x3)) = deconstructable1</c> with
/// <c>Deconstructable1.Deconstruct(out int y1, out Deconstructable2 y2)</c> and
/// <c>Deconstructable2.Deconstruct(out int z1, out int z2)</c> is represented as 5 DeconstructionInfo nodes.
///
/// The top-level node has a <see cref="Method"/> (Deconstructable1.Deconstruct), no <see cref="Conversion"/>, but has two <see cref="Nested"/> nodes.
/// Its first nested node has no <see cref="Method"/>, but has a <see cref="Conversion"/> (Identity).
/// Its second nested node has a <see cref="Method"/> (Deconstructable2.Deconstruct), no <see cref="Conversion"/>, and two <see cref="Nested"/> nodes.
/// Those last two nested nodes have no <see cref="Method"/>, but each have a <see cref="Conversion"/> (ImplicitNumeric, from int to long).
/// </summary>
public struct DeconstructionInfo
{
private readonly Conversion _conversion;
/// <summary>
/// The Deconstruct method (if any) for this non-terminal position in the deconstruction tree.
/// </summary>
public IMethodSymbol? Method
{
get
{
return _conversion.Kind == ConversionKind.Deconstruction
? _conversion.MethodSymbol
: null;
}
}
/// <summary>
/// The conversion for a terminal position in the deconstruction tree.
/// </summary>
public Conversion? Conversion
{
get
{
return _conversion.Kind == ConversionKind.Deconstruction
? null
: (Conversion?)_conversion;
}
}
/// <summary>
/// The children for this deconstruction node.
/// </summary>
public ImmutableArray<DeconstructionInfo> Nested
{
get
{
var underlyingConversions = _conversion.UnderlyingConversions;
return underlyingConversions.IsDefault
? ImmutableArray<DeconstructionInfo>.Empty
: underlyingConversions.SelectAsArray(c => new DeconstructionInfo(c));
}
}
internal DeconstructionInfo(Conversion conversion)
{
_conversion = conversion;
}
}
}