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.
29 using System.Collections;
32 namespace Mono.Linker.Steps {
34 public class AdjustVisibilityStep : BaseStep {
36 protected override void ProcessAssembly (AssemblyDefinition assembly)
38 foreach (TypeDefinition type in assembly.MainModule.Types)
42 static void ProcessType (TypeDefinition type)
44 ProcessFields (type.Fields);
45 ProcessMethods (type.Constructors);
46 ProcessMethods (type.Methods);
51 if (IsMarkedAsPublic (type))
54 SetInternalVisibility (type);
57 static void SetInternalVisibility (TypeDefinition type)
59 type.Attributes &= ~TypeAttributes.VisibilityMask;
60 if (type.DeclaringType == null)
61 type.Attributes |= TypeAttributes.NotPublic;
63 type.Attributes |= TypeAttributes.NestedAssembly;
66 static void ProcessMethods (ICollection methods)
68 foreach (MethodDefinition method in methods)
69 ProcessMethod (method);
72 static void ProcessMethod (MethodDefinition method)
74 if (!IsPublic (method))
77 if (IsMarkedAsPublic (method))
80 SetInternalVisibility (method);
83 static void SetInternalVisibility (MethodDefinition method)
85 method.Attributes &= ~MethodAttributes.MemberAccessMask;
86 method.Attributes |= MethodAttributes.Assem;
89 static bool IsMarkedAsPublic (IAnnotationProvider provider)
91 return Annotations.IsPublic (provider);
94 static bool IsPublic (MethodDefinition method)
96 return (method.Attributes & MethodAttributes.Public) != 0;
99 static bool IsPublic (FieldDefinition field)
101 return (field.Attributes & FieldAttributes.Public) != 0;
104 static bool IsPublic (TypeDefinition type)
106 return (type.Attributes & TypeAttributes.Public) != 0;
109 static void ProcessFields (FieldDefinitionCollection fields)
111 foreach (FieldDefinition field in fields)
112 ProcessField (field);
115 static void ProcessField (FieldDefinition field)
117 if (!IsPublic (field))
120 if (IsMarkedAsPublic (field))
123 SetInternalVisibility (field);
126 static void SetInternalVisibility (FieldDefinition field)
128 field.Attributes &= ~FieldAttributes.FieldAccessMask;
129 field.Attributes |= FieldAttributes.Assembly;