summaryrefslogtreecommitdiff
path: root/Source/AIFramework/VariableMap/Nullness.ssc
blob: bbd1da70c43e519d0e91826b7f89b724a9dedce6 (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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using Microsoft.Contracts;
namespace Microsoft.AbstractInterpretationFramework
{
    using System.Collections;
    using System.Diagnostics;
    using System.Compiler.Analysis;

    public class NullnessLattice : MicroLattice 
    {
        readonly INullnessFactory! factory;

        public NullnessLattice(INullnessFactory! factory) {
            this.factory = factory;
            // base();
        }
        
        enum Value { Bottom, NotNull, Null, MayBeNull }

        private class Elt : Element
        {
            public Value value;

            public Elt (Value v) { this.value = v; }
            
            [Pure]
            public override System.Collections.Generic.ICollection<IVariable!>! FreeVariables()
            {
                return (!) (new System.Collections.Generic.List<IVariable!>()).AsReadOnly();
            }
            
            public override Element! Clone()
            {
                return new Elt(this.value);
            }
        }


        public override Element! Top 
        {
            get { return new Elt(Value.MayBeNull); } 
        }

        public override Element! Bottom 
        { 
            get { return new Elt(Value.Bottom); } 
        }

        public static Element! Null
        { 
            get { return new Elt(Value.Null); } 
        }

        public static Element! NotNull 
        { 
            get { return new Elt(Value.NotNull); } 
        }

        public override bool IsTop (Element! element)
        {
            Elt e = (Elt) element;
            return e.value == Value.MayBeNull;
        }

        public override bool IsBottom    (Element! element)
        {
            Elt e = (Elt) element;
            return e.value == Value.Bottom;
        }

        public override Lattice.Element! NontrivialJoin (Element! first, Element! second)
        {
            Elt a = (Elt) first;
            Elt b = (Elt) second;
            return (a.value == b.value) ? a : (Elt)Top;
        }

        public override Lattice.Element! NontrivialMeet (Element! first, Element! second)
        {
            Elt a = (Elt) first;
            Elt b = (Elt) second;
            return (a.value == b.value) ? a : (Elt)Bottom;
        }
        
        public override Element! Widen (Element! first, Element! second)
        {
            return Join(first,second);
        }

        protected override bool AtMost (Element! first, Element! second) // this <= that
        {
            Elt a = (Elt) first;
            Elt b = (Elt) second;
            return a.value == b.value;
        }

        public override IExpr! ToPredicate(IVariable! var, Element! element) {
            Elt e = (Elt)element;

            if (e.value == Value.NotNull)
            {
                return factory.Neq(var, factory.Null);
            }
            if (e.value == Value.Null)
            {
                return factory.Eq(var, factory.Null);
            }
            assert false;
            throw new System.Exception();
        }

        public override IExpr GetFoldExpr(Element! e) {
            Elt elt = (Elt)e;
            if (elt.value == Value.Null) {
                return factory.Null;
            } else {
                // can't fold into an expression
                return null;
            }
        }

        public override bool Understands(IFunctionSymbol! f, IList/*<IExpr!>*/! args) {
            if (f.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq) ||
                f.Equals(Microsoft.AbstractInterpretationFramework.Value.Neq)) {

                assert args.Count == 2;
                IExpr! arg0 = (IExpr!)args[0];
                IExpr! arg1 = (IExpr!)args[1];

                // Look for "x OP null" or "null OP x" where OP is "==" or "!=".
                if (arg0 is IVariable && arg1 is IFunApp && ((IFunApp)arg1).FunctionSymbol == Ref.Null) {
                    return true;
                } else if (arg1 is IVariable && arg0 is IFunApp && ((IFunApp)arg0).FunctionSymbol == Ref.Null) {
                    return true;
                }
            }
            return false;
        }

        public override Element! EvaluatePredicate(IExpr! e) {
            IFunApp nary = e as IFunApp;
            if (nary != null) {
                bool isEq = nary.FunctionSymbol.Equals(Microsoft.AbstractInterpretationFramework.Value.Eq);
                if (isEq || nary.FunctionSymbol.Equals(Microsoft.AbstractInterpretationFramework.Value.Neq)) {
                    IList/*<IExpr!>*/! args = nary.Arguments;
                    assert args.Count == 2;
                    IExpr! arg0 = (IExpr!)args[0];
                    IExpr! arg1 = (IExpr!)args[1];

                    // Look for "x OP null" or "null OP x" where OP is "==" or "!=".
                    IVariable var = null;
                    if (arg0 is IVariable && arg1 is IFunApp && ((IFunApp)arg1).FunctionSymbol == Ref.Null)
                    {
                        var = (IVariable)arg0;
                    }
                    else if (arg1 is IVariable && arg0 is IFunApp && ((IFunApp)arg0).FunctionSymbol == Ref.Null)
                    {
                        var = (IVariable)arg1;
                    }

                    if (var != null) // found the pattern
                    {
                        return isEq ? Null:NotNull;
                    }
                }
            }
            return Top;
        }
    }

#if false

    public class NullnessMicroLattice : MicroLattice 
    {
        public override MicroLatticeElement Top { get { return NullnessLatticeElement.Top; } }
        public override MicroLatticeElement Bottom { get { return NullnessLatticeElement.Bottom; } }


        public override MicroLatticeElement EvaluateExpression (Expr e, LookupValue lookup)
        {
            if (e is LiteralExpr && ((LiteralExpr)e).Val == null)
            {
                return NullnessLatticeElement.Null;
            }
            return Top;
        }


        public override MicroLatticeElement EvaluatePredicate (Expr e, LookupValue lookup)
        {
            NAryExpr nary = e as NAryExpr;
            if (nary != null && 
                    (nary.Fun.FunctionName.Equals("==") || nary.Fun.FunctionName.Equals("!=")))
            {
                Debug.Assert(nary.Args.Length == 2);

                Expr arg0 = nary.Args[0], arg1 = nary.Args[1];
                Variable var = null;

                // Look for "x OP null" or "null OP x" where OP is "==" or "!=".
                if (arg0 is IdentifierExpr && arg1 is LiteralExpr && ((LiteralExpr)arg1).Val == null)
                {
                    var = ((IdentifierExpr)arg0).Decl;
                }
                else if (arg1 is IdentifierExpr && arg0 is LiteralExpr && ((LiteralExpr)arg0).Val == null)
                {
                    var = ((IdentifierExpr)arg1).Decl;
                }

                if (var != null) // found the pattern
                {
                    return nary.Fun.FunctionName.Equals("==") ? 
                        NullnessLatticeElement.Null : 
                        NullnessLatticeElement.NotNull;
                }
            }
            return Top;
        }
    }

#endif

}