File: ExpressionFingerprintChain.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (79 lines) | stat: -rw-r--r-- 3,674 bytes parent folder | download | duplicates (9)
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
namespace System.Web.Mvc.ExpressionUtil {
    using System;
    using System.Collections.Generic;

    // 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 (!Object.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;
        }

    }
}