Classes/Expressions.ps1

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using namespace System.Reflection
using namespace System.Collections.ObjectModel
using namespace System.Management.Automation.Language

class TypeExpressionHelper {
    [type] $Type;

    hidden [bool] $encloseWithBrackets;
    hidden [bool] $needsProxy;

    TypeExpressionHelper ([type] $type) {
        $this.Type = $Type
    }
    static [string] Create ([type] $type) {
        return [TypeExpressionHelper]::Create($type, $true)
    }
    static [string] Create ([type] $type, [bool] $encloseWithBrackets) {
        $helper = [TypeExpressionHelper]::new($type)
        $helper.encloseWithBrackets = $encloseWithBrackets
        return $helper.Create()
    }
   [string] Create () {
        # Non public types can't be retrieved with a type literal expression and need to be retrieved
        # from their assembly directly. The easiest way is to get a type from the same assembly and
        # get the assembly from that. The goal here is to build it as short as possible, hopefully
        # retaining some semblance of readability.
        if (-not $this.Type.IsPublic -or $this.Type.GenericTypeArguments.IsPublic -contains $false) {
            $this.needsProxy = $true
            return $this.CreateProxy()
        }
        else {
            return $this.CreateLiteral()
        }
    }
    hidden [string] CreateProxy () {
        $builder = [System.Text.StringBuilder]::new('[')
        $assembly = $this.Type.Assembly

        # First check if there are any type accelerators in the same assembly.
        $choices = $this.GetAccelerators().GetEnumerator().Where{ $PSItem.Value.Assembly -eq $assembly }.Key

        if (-not $choices) {
            # Then as a last resort pull every type from the assembly. This takes a extra second or
            # two the first time.
            $choices = $assembly.GetTypes().ToString
        }

        $builder.
            Append(($choices | Sort-Object Length)[0]).
            Append('].Assembly.GetType(''')

        if ($this.Type.GenericTypeArguments) {
            # Using the GetType method on the full name doesn't work for every type/combination, so
            # we use the MakeGenericType method.
            return $builder.AppendFormat('{0}.{1}'').MakeGenericType(', $this.Type.Namespace, $this.Type.Name).
                Append($this.GetGenericArguments()).
                Append(')').
                ToString()
        }
        else {
            return $builder.
                AppendFormat('{0}'')', $this.Type.ToString()).
                ToString()
        }
    }
    hidden [string] CreateLiteral () {
        $builder = [System.Text.StringBuilder]::new()
        # If we are building the type name as a generic type argument in a type literal we don't want
        # to enclose it with brackets.
        if ($this.encloseWithBrackets) { $builder.Append('[') }

        if ($this.Type.GenericTypeArguments) {
            $builder.
                AppendFormat('{0}.{1}', $this.Type.Namespace, $this.Type.Name).
                Append('[').
                Append($this.GetGenericArguments()).
                Append(']')
        }
        else {
            $name = $this.GetAccelerators().
                GetEnumerator().
                Where{ $PSItem.Value -eq $this.Type }.
                Key |
                Sort-Object Length

            if (-not $name) { $name = ($this.Type.Name -as [type]).Name }
            if (-not $name) { $name = $this.Type.ToString() }

            if ($name.Count -gt 1) { $name = $name[0] }

            $builder.Append($name)
        }

        if ($this.encloseWithBrackets) { $builder.Append(']') }

        return $builder.ToString()
    }
    hidden [string] GetGenericArguments () {
        $typeArguments = $this.Type.GenericTypeArguments

        $enclose = $false
        if ($this.needsProxy) { $enclose = $true }

        return $typeArguments.ForEach{
            [TypeExpressionHelper]::Create($PSItem, $enclose)
        } -join ', '
    }
    hidden [System.Collections.Generic.Dictionary[string, type]] GetAccelerators () {
       return [ref].Assembly.GetType('System.Management.Automation.TypeAccelerators')::Get
    }
}

class ExtendedMemberExpressionAst : MemberExpressionAst {
    [type] $InferredType;
    [MemberInfo] $InferredMember;
    [BindingFlags] $BindingFlags;
    [ReadOnlyCollection[ExpressionAst]] $Arguments;

    ExtendedMemberExpressionAst ([IScriptExtent] $extent,
                                 [ExpressionAst] $expression,
                                 [CommandElementAst] $member,
                                 [bool] $static,
                                 [ReadOnlyCollection[ExpressionAst]] $arguments) :
                                 base($extent, $expression, $member, $static) {

        try {
            $this.Arguments      = $arguments
            $this.InferredMember = GetInferredMember -Ast $this
            $this.InferredType   = ($this.InferredMember.ReturnType,
                                    $this.InferredMember.PropertyType,
                                    $this.InferredMember.FieldType).
                                    Where({ $PSItem }, 'First')[0]

            $this.BindingFlags   = $this.InferredMember.GetType().
                GetProperty('BindingFlags', [BindingFlags]'Instance, NonPublic').
                GetValue($this.InferredMember)
        } catch {
            $this.InferredType = [object]
        }
    }
    static [ExtendedMemberExpressionAst] op_Implicit ([MemberExpressionAst] $ast) {

        $expression = $ast.Expression.Copy()
        if ($expression -is [MemberExpressionAst]) {
            $expression = [ExtendedMemberExpressionAst]$expression
        }
        $newAst = [ExtendedMemberExpressionAst]::new(
            $ast.Extent,
            $expression,
            $ast.Member.Copy(),
            $ast.Static,
            $ast.Arguments
        )

        if ($ast.Parent) {
            $ast.Parent.GetType().
                GetMethod('SetParent', [BindingFlags]'Instance, NonPublic').
                Invoke($ast.Parent, $newAst)
        }

        return $newAst
    }
}