The base architecture for code-contracts analysis
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.Analysis.HeapAnalysis.Paths / AccessPathFilter.cs
1 // 
2 // AccessPathFilter.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30
31 namespace Mono.CodeContracts.Static.Analysis.HeapAnalysis.Paths {
32         class AccessPathFilter<TMember> {
33                 public static AccessPathFilter<TMember> NoFilter = new AccessPathFilter<TMember> ();
34                 private readonly Flags flags;
35                 private readonly TMember member;
36                 private readonly MemberFilter member_filter;
37
38                 private AccessPathFilter ()
39                 {
40                         this.flags = Flags.AllowLocals;
41                         this.member_filter = MemberFilter.NoFilter;
42                 }
43
44                 private AccessPathFilter (TMember member, MemberFilter memberFilter)
45                 {
46                         this.member_filter = memberFilter;
47                         this.member = member;
48                 }
49
50                 public bool AllowLocal
51                 {
52                         get { return (this.flags & Flags.AllowLocals) == Flags.AllowLocals; }
53                 }
54
55                 public bool HasVisibilityMember
56                 {
57                         get { return this.member_filter != MemberFilter.NoFilter; }
58                 }
59
60                 public TMember VisibilityMember
61                 {
62                         get { return this.member; }
63                 }
64
65                 public static AccessPathFilter<TMember> FromPrecondition (TMember member)
66                 {
67                         return new AccessPathFilter<TMember> (member, MemberFilter.FromPrecondition);
68                 }
69
70                 public static AccessPathFilter<TMember> FromPostcondition (TMember member)
71                 {
72                         return new AccessPathFilter<TMember> (member, MemberFilter.FromPostcondition);
73                 }
74
75                 public static AccessPathFilter<TMember> IsVisibleFrom (TMember member)
76                 {
77                         return new AccessPathFilter<TMember> (member, MemberFilter.FromMethodBody);
78                 }
79
80                 public bool FilterOutPathElement<P> (P element)
81                         where P : IVisibilityCheck<TMember>
82                 {
83                         switch (this.member_filter) {
84                         case MemberFilter.FromPrecondition:
85                                 return !element.IsAsVisibleAs (this.member);
86                         case MemberFilter.FromPostcondition:
87                                 return !element.IfRootIsParameter || !element.IsVisibleFrom (this.member);
88                         case MemberFilter.FromMethodBody:
89                                 return !element.IsVisibleFrom (this.member);
90                         default:
91                                 return false;
92                         }
93                 }
94
95                 #region Nested type: Flags
96                 [Flags]
97                 private enum Flags {
98                         AllowLocals = 1,
99                         RequireParameter = 2
100                 }
101                 #endregion
102
103                 #region Nested type: MemberFilter
104                 private enum MemberFilter {
105                         NoFilter = 0,
106                         FromPrecondition,
107                         FromPostcondition,
108                         FromMethodBody
109                 }
110                 #endregion
111         }
112 }