Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExpressionFingerprintChain.cs « ExpressionUtil « System.Web.Mvc « src - github.com/mono/aspnetwebstack.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3399dcee2f6c58f37555e77832cbf5b5953dae88 (plain)
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
using System.Collections.Generic;

namespace System.Web.Mvc.ExpressionUtil
{
    // Expression fingerprint chain class
    // Contains information used for generalizing, comparing, and recreating Expression instances
    //
    // Since Expression objects are immutable and are recreated for every invocation of an expression
    // helper method, they can't be compared directly. Fingerprinting Expression objects allows
    // information about them to be abstracted away, and the fingerprints can be directly compared.
    // Consider the process of fingerprinting that all values (parameters, constants, etc.) are hoisted
    // and replaced with dummies. What remains can be decomposed into a sequence of operations on specific
    // types and specific inputs.
    //
    // Some sample fingerprints chains:
    //
    // 2 + 4 -> OP_ADD, CONST:int, NULL, CONST:int
    // 2 + 8 -> OP_ADD, CONST:int, NULL, CONST:int
    // 2.0 + 4.0 -> OP_ADD, CONST:double, NULL, CONST:double
    //
    // 2 + 4 and 2 + 8 have the same fingerprint, but 2.0 + 4.0 has a different fingerprint since its
    // underlying types differ. Note that this looks a bit like prefix notation and is a side effect
    // of how the ExpressionVisitor class recurses into expressions. (Occasionally there will be a NULL
    // in the fingerprint chain, which depending on context can denote a static member, a null Conversion
    // in a BinaryExpression, and so forth.)
    //
    // "Hello " + "world" -> OP_ADD, CONST:string, NULL, CONST:string
    // "Hello " + {model} -> OP_ADD, CONST:string, NULL, PARAM_0:string
    //
    // These string concatenations have different fingerprints since the inputs are provided differently:
    // one is a constant, the other is a parameter.
    //
    // ({model} ?? "sample").Length -> MEMBER_ACCESS(String.Length), OP_COALESCE, PARAM_0:string, NULL, CONST:string
    // ({model} ?? "other sample").Length -> MEMBER_ACCESS(String.Length), OP_COALESCE, PARAM_0:string, NULL, CONST:string
    //
    // These expressions have the same fingerprint since all constants of the same underlying type are
    // treated equally.
    //
    // It's also important that the fingerprints don't reference the actual Expression objects that were
    // used to generate them, as the fingerprints will be cached, and caching a fingerprint that references
    // an Expression will root the Expression (and any objects it references).

    internal sealed class ExpressionFingerprintChain : IEquatable<ExpressionFingerprintChain>
    {
        public readonly List<ExpressionFingerprint> Elements = new List<ExpressionFingerprint>();

        public bool Equals(ExpressionFingerprintChain other)
        {
            // Two chains are considered equal if two elements appearing in the same index in
            // each chain are equal (value equality, not referential equality).

            if (other == null)
            {
                return false;
            }

            if (this.Elements.Count != other.Elements.Count)
            {
                return false;
            }

            for (int i = 0; i < this.Elements.Count; i++)
            {
                if (!Equals(this.Elements[i], other.Elements[i]))
                {
                    return false;
                }
            }

            return true;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as ExpressionFingerprintChain);
        }

        public override int GetHashCode()
        {
            HashCodeCombiner combiner = new HashCodeCombiner();
            Elements.ForEach(combiner.AddFingerprint);
            return combiner.CombinedHash;
        }
    }
}