Merge branch 'cecil-light'
[mono.git] / mcs / tools / tuner / Mono.Tuner / AdjustVisibility.cs
1 //
2 // AdjustVisibilityStep.cs
3 //
4 // Author:
5 //   Jb Evain (jbevain@novell.com)
6 //
7 // (C) 2007 Novell, Inc.
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 using System.Collections;
31 using System.Collections.Generic;
32
33 using Mono.Linker;
34 using Mono.Linker.Steps;
35
36 using Mono.Cecil;
37
38 namespace Mono.Tuner {
39
40         public class AdjustVisibility : BaseStep {
41
42                 static readonly object internalized_key = new object ();
43
44                 protected override void ProcessAssembly (AssemblyDefinition assembly)
45                 {
46                         if (Annotations.GetAction (assembly) != AssemblyAction.Link)
47                                 return;
48
49                         ProcessTypes (assembly.MainModule.Types);
50                 }
51
52                 void ProcessTypes (ICollection types)
53                 {
54                         foreach (TypeDefinition type in types)
55                                 ProcessType (type);
56                 }
57
58                 void ProcessType (TypeDefinition type)
59                 {
60                         if (!IsPublic (type))
61                                 return;
62
63                         if (!IsMarkedAsPublic (type)) {
64                                 SetInternalVisibility (type);
65                                 return;
66                         }
67
68                         if (type.IsEnum)
69                                 return;
70
71                         ProcessFields (type.Fields);
72                         ProcessMethods (type.Methods);
73                 }
74
75                 static bool IsPublic (TypeDefinition type)
76                 {
77                         return type.DeclaringType == null ? type.IsPublic : type.IsNestedPublic;
78                 }
79
80                 void SetInternalVisibility (TypeDefinition type)
81                 {
82                         type.Attributes &= ~TypeAttributes.VisibilityMask;
83                         if (type.DeclaringType == null)
84                                 type.Attributes |= TypeAttributes.NotPublic;
85                         else
86                                 type.Attributes |= TypeAttributes.NestedAssembly;
87
88                         MarkInternalized (type);
89                 }
90
91                 void ProcessMethods (ICollection methods)
92                 {
93                         foreach (MethodDefinition method in methods)
94                                 ProcessMethod (method);
95                 }
96
97                 void ProcessMethod (MethodDefinition method)
98                 {
99                         if (IsMarkedAsPublic (method))
100                                 return;
101
102                         if (method.IsPublic)
103                                 SetInternalVisibility (method);
104                         else if (method.IsFamily || method.IsFamilyOrAssembly)
105                                 SetProtectedAndInternalVisibility (method);
106                 }
107
108                 void SetInternalVisibility (MethodDefinition method)
109                 {
110                         method.Attributes &= ~MethodAttributes.MemberAccessMask;
111                         method.Attributes |= MethodAttributes.Assembly;
112
113                         MarkInternalized (method);
114                 }
115
116                 void SetProtectedAndInternalVisibility (MethodDefinition method)
117                 {
118                         method.Attributes &= ~MethodAttributes.MemberAccessMask;
119                         method.Attributes |= MethodAttributes.FamANDAssem;
120
121                         MarkInternalized (method);
122                 }
123
124                 bool IsMarkedAsPublic (IMetadataTokenProvider provider)
125                 {
126                         return Annotations.IsPublic (provider);
127                 }
128
129                 void ProcessFields (IEnumerable<FieldDefinition> fields)
130                 {
131                         foreach (FieldDefinition field in fields)
132                                 ProcessField (field);
133                 }
134
135                 void ProcessField (FieldDefinition field)
136                 {
137                         if (IsMarkedAsPublic (field))
138                                 return;
139
140                         if (field.IsPublic)
141                                 SetInternalVisibility (field);
142                         else if (field.IsFamily || field.IsFamilyOrAssembly)
143                                 SetProtectedAndInternalVisibility (field);
144                 }
145
146                 void SetInternalVisibility (FieldDefinition field)
147                 {
148                         field.Attributes &= ~FieldAttributes.FieldAccessMask;
149                         field.Attributes |= FieldAttributes.Assembly;
150
151                         MarkInternalized (field);
152                 }
153
154                 void SetProtectedAndInternalVisibility (FieldDefinition field)
155                 {
156                         field.Attributes &= ~FieldAttributes.FieldAccessMask;
157                         field.Attributes |= FieldAttributes.FamANDAssem;
158
159                         MarkInternalized (field);
160                 }
161
162                 void MarkInternalized (IMetadataTokenProvider provider)
163                 {
164                         TunerAnnotations.Internalized (Context, provider);
165                 }
166         }
167 }