2 // AdjustVisibilityStep.cs
5 // Jb Evain (jbevain@novell.com)
7 // (C) 2007 Novell, Inc.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System
.Collections
;
33 using Mono
.Linker
.Steps
;
37 namespace Mono
.Tuner
{
39 public class AdjustVisibility
: BaseStep
{
41 protected override void ProcessAssembly (AssemblyDefinition assembly
)
43 if (Annotations
.GetAction (assembly
) != AssemblyAction
.Link
)
46 ProcessTypes (assembly
.MainModule
.Types
);
49 static void ProcessTypes (ICollection types
)
51 foreach (TypeDefinition type
in types
)
55 static void ProcessType (TypeDefinition type
)
60 if (!IsMarkedAsPublic (type
)) {
61 SetInternalVisibility (type
);
68 ProcessFields (type
.Fields
);
69 ProcessMethods (type
.Constructors
);
70 ProcessMethods (type
.Methods
);
73 static bool IsPublic (TypeDefinition type
)
75 return type
.DeclaringType
== null ? type
.IsPublic
: type
.IsNestedPublic
;
78 static void SetInternalVisibility (TypeDefinition type
)
80 type
.Attributes
&= ~TypeAttributes
.VisibilityMask
;
81 if (type
.DeclaringType
== null)
82 type
.Attributes
|= TypeAttributes
.NotPublic
;
84 type
.Attributes
|= TypeAttributes
.NestedAssembly
;
86 TunerAnnotations
.Internalized (type
);
89 static void ProcessMethods (ICollection methods
)
91 foreach (MethodDefinition method
in methods
)
92 ProcessMethod (method
);
95 static void ProcessMethod (MethodDefinition method
)
97 if (IsMarkedAsPublic (method
))
101 SetInternalVisibility (method
);
102 else if (method
.IsFamily
|| method
.IsFamilyOrAssembly
)
103 SetProtectedAndInternalVisibility (method
);
106 static void SetInternalVisibility (MethodDefinition method
)
108 method
.Attributes
&= ~MethodAttributes
.MemberAccessMask
;
109 method
.Attributes
|= MethodAttributes
.Assem
;
111 TunerAnnotations
.Internalized (method
);
114 static void SetProtectedAndInternalVisibility (MethodDefinition method
)
116 method
.Attributes
&= ~MethodAttributes
.MemberAccessMask
;
117 method
.Attributes
|= MethodAttributes
.FamANDAssem
;
119 TunerAnnotations
.Internalized (method
);
122 static bool IsMarkedAsPublic (IAnnotationProvider provider
)
124 return Annotations
.IsPublic (provider
);
127 static void ProcessFields (FieldDefinitionCollection fields
)
129 foreach (FieldDefinition field
in fields
)
130 ProcessField (field
);
133 static void ProcessField (FieldDefinition field
)
135 if (IsMarkedAsPublic (field
))
139 SetInternalVisibility (field
);
140 else if (field
.IsFamily
|| field
.IsFamilyOrAssembly
)
141 SetProtectedAndInternalVisibility (field
);
144 static void SetInternalVisibility (FieldDefinition field
)
146 field
.Attributes
&= ~FieldAttributes
.FieldAccessMask
;
147 field
.Attributes
|= FieldAttributes
.Assembly
;
149 TunerAnnotations
.Internalized (field
);
152 static void SetProtectedAndInternalVisibility (FieldDefinition field
)
154 field
.Attributes
&= ~FieldAttributes
.FieldAccessMask
;
155 field
.Attributes
|= FieldAttributes
.FamANDAssem
;
157 TunerAnnotations
.Internalized (field
);