a5c9b5e689045704cdb11bc4ed6359e29d2b81f5
[mono.git] / mcs / tools / linker / Mono.Linker.Steps / AdjustVisibilityStep.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.Collections;
30 using Mono.Cecil;
31
32 namespace Mono.Linker.Steps {
33
34         public class AdjustVisibilityStep : BaseStep {
35
36                 protected override void ProcessAssembly (AssemblyDefinition assembly)
37                 {
38                         foreach (TypeDefinition type in assembly.MainModule.Types)
39                                 ProcessType (type);
40                 }
41
42                 static void ProcessType (TypeDefinition type)
43                 {
44                         ProcessFields (type.Fields);
45                         ProcessMethods (type.Constructors);
46                         ProcessMethods (type.Methods);
47
48                         if (!IsPublic (type))
49                                 return;
50
51                         if (IsMarkedAsPublic (type))
52                                 return;
53
54                         SetInternalVisibility (type);
55                 }
56
57                 static void SetInternalVisibility (TypeDefinition type)
58                 {
59                         type.Attributes &= ~TypeAttributes.VisibilityMask;
60                         if (type.DeclaringType == null)
61                                 type.Attributes |= TypeAttributes.NotPublic;
62                         else
63                                 type.Attributes |= TypeAttributes.NestedAssembly;
64                 }
65
66                 static void ProcessMethods (ICollection methods)
67                 {
68                         foreach (MethodDefinition method in methods)
69                                 ProcessMethod (method);
70                 }
71
72                 static void ProcessMethod (MethodDefinition method)
73                 {
74                         if (!IsPublic (method))
75                                 return;
76
77                         if (IsMarkedAsPublic (method))
78                                 return;
79
80                         SetInternalVisibility (method);
81                 }
82
83                 static void SetInternalVisibility (MethodDefinition method)
84                 {
85                         method.Attributes &= ~MethodAttributes.MemberAccessMask;
86                         method.Attributes |= MethodAttributes.Assem;
87                 }
88
89                 static bool IsMarkedAsPublic (IAnnotationProvider provider)
90                 {
91                         return Annotations.IsPublic (provider);
92                 }
93
94                 static bool IsPublic (MethodDefinition method)
95                 {
96                         return (method.Attributes & MethodAttributes.Public) != 0;
97                 }
98
99                 static bool IsPublic (FieldDefinition field)
100                 {
101                         return (field.Attributes & FieldAttributes.Public) != 0;
102                 }
103
104                 static bool IsPublic (TypeDefinition type)
105                 {
106                         return (type.Attributes & TypeAttributes.Public) != 0;
107                 }
108
109                 static void ProcessFields (FieldDefinitionCollection fields)
110                 {
111                         foreach (FieldDefinition field in fields)
112                                 ProcessField (field);
113                 }
114
115                 static void ProcessField (FieldDefinition field)
116                 {
117                         if (!IsPublic (field))
118                                 return;
119
120                         if (IsMarkedAsPublic (field))
121                                 return;
122
123                         SetInternalVisibility (field);
124                 }
125
126                 static void SetInternalVisibility (FieldDefinition field)
127                 {
128                         field.Attributes &= ~FieldAttributes.FieldAccessMask;
129                         field.Attributes |= FieldAttributes.Assembly;
130                 }
131         }
132 }