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;
31 using System.Collections.Generic;
34 using Mono.Linker.Steps;
38 namespace Mono.Tuner {
40 public class AdjustVisibility : BaseStep {
42 static readonly object internalized_key = new object ();
44 protected override void ProcessAssembly (AssemblyDefinition assembly)
46 if (Annotations.GetAction (assembly) != AssemblyAction.Link)
49 ProcessTypes (assembly.MainModule.Types);
52 void ProcessTypes (ICollection types)
54 foreach (TypeDefinition type in types)
58 void ProcessType (TypeDefinition type)
63 if (!IsMarkedAsPublic (type)) {
64 SetInternalVisibility (type);
71 ProcessFields (type.Fields);
72 ProcessMethods (type.Methods);
75 static bool IsPublic (TypeDefinition type)
77 return type.DeclaringType == null ? type.IsPublic : type.IsNestedPublic;
80 void SetInternalVisibility (TypeDefinition type)
82 type.Attributes &= ~TypeAttributes.VisibilityMask;
83 if (type.DeclaringType == null)
84 type.Attributes |= TypeAttributes.NotPublic;
86 type.Attributes |= TypeAttributes.NestedAssembly;
88 MarkInternalized (type);
91 void ProcessMethods (ICollection methods)
93 foreach (MethodDefinition method in methods)
94 ProcessMethod (method);
97 void ProcessMethod (MethodDefinition method)
99 if (IsMarkedAsPublic (method))
103 SetInternalVisibility (method);
104 else if (method.IsFamily || method.IsFamilyOrAssembly)
105 SetProtectedAndInternalVisibility (method);
108 void SetInternalVisibility (MethodDefinition method)
110 method.Attributes &= ~MethodAttributes.MemberAccessMask;
111 method.Attributes |= MethodAttributes.Assembly;
113 MarkInternalized (method);
116 void SetProtectedAndInternalVisibility (MethodDefinition method)
118 method.Attributes &= ~MethodAttributes.MemberAccessMask;
119 method.Attributes |= MethodAttributes.FamANDAssem;
121 MarkInternalized (method);
124 bool IsMarkedAsPublic (IMetadataTokenProvider provider)
126 return Annotations.IsPublic (provider);
129 void ProcessFields (IEnumerable<FieldDefinition> fields)
131 foreach (FieldDefinition field in fields)
132 ProcessField (field);
135 void ProcessField (FieldDefinition field)
137 if (IsMarkedAsPublic (field))
141 SetInternalVisibility (field);
142 else if (field.IsFamily || field.IsFamilyOrAssembly)
143 SetProtectedAndInternalVisibility (field);
146 void SetInternalVisibility (FieldDefinition field)
148 field.Attributes &= ~FieldAttributes.FieldAccessMask;
149 field.Attributes |= FieldAttributes.Assembly;
151 MarkInternalized (field);
154 void SetProtectedAndInternalVisibility (FieldDefinition field)
156 field.Attributes &= ~FieldAttributes.FieldAccessMask;
157 field.Attributes |= FieldAttributes.FamANDAssem;
159 MarkInternalized (field);
162 void MarkInternalized (IMetadataTokenProvider provider)
164 TunerAnnotations.Internalized (Context, provider);