linker: add per method preserve information
[mono.git] / mcs / tools / linker / Mono.Linker / Annotations.cs
1 //
2 // Annotations.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.Cecil;
34
35 namespace Mono.Linker {
36
37         public class AnnotationStore {
38
39                 readonly Dictionary<AssemblyDefinition, AssemblyAction> assembly_actions = new Dictionary<AssemblyDefinition, AssemblyAction> ();
40                 readonly Dictionary<MethodDefinition, MethodAction> method_actions = new Dictionary<MethodDefinition, MethodAction> ();
41                 readonly HashSet<IMetadataTokenProvider> marked = new HashSet<IMetadataTokenProvider> ();
42                 readonly HashSet<IMetadataTokenProvider> processed = new HashSet<IMetadataTokenProvider> ();
43                 readonly Dictionary<TypeDefinition, TypePreserve> preserved_types = new Dictionary<TypeDefinition, TypePreserve> ();
44                 readonly Dictionary<IMemberDefinition, List<MethodDefinition>> preserved_methods = new Dictionary<IMemberDefinition, List<MethodDefinition>> ();
45                 readonly HashSet<IMetadataTokenProvider> public_api = new HashSet<IMetadataTokenProvider> ();
46                 readonly Dictionary<MethodDefinition, List<MethodDefinition>> override_methods = new Dictionary<MethodDefinition, List<MethodDefinition>> ();
47                 readonly Dictionary<MethodDefinition, List<MethodDefinition>> base_methods = new Dictionary<MethodDefinition, List<MethodDefinition>> ();
48
49                 readonly Dictionary<object, Dictionary<IMetadataTokenProvider, object>> custom_annotations = new Dictionary<object, Dictionary<IMetadataTokenProvider, object>> ();
50
51                 public AssemblyAction GetAction (AssemblyDefinition assembly)
52                 {
53                         AssemblyAction action;
54                         if (assembly_actions.TryGetValue (assembly, out action))
55                                 return action;
56
57                         throw new NotSupportedException ();
58                 }
59
60                 public MethodAction GetAction (MethodDefinition method)
61                 {
62                         MethodAction action;
63                         if (method_actions.TryGetValue (method, out action))
64                                 return action;
65
66                         return MethodAction.Nothing;
67                 }
68
69                 public void SetAction (AssemblyDefinition assembly, AssemblyAction action)
70                 {
71                         assembly_actions [assembly] = action;
72                 }
73
74                 public bool HasAction (AssemblyDefinition assembly)
75                 {
76                         return assembly_actions.ContainsKey (assembly);
77                 }
78
79                 public void SetAction (MethodDefinition method, MethodAction action)
80                 {
81                         method_actions [method] = action;
82                 }
83
84                 public void Mark (IMetadataTokenProvider provider)
85                 {
86                         marked.Add (provider);
87                 }
88
89                 public bool IsMarked (IMetadataTokenProvider provider)
90                 {
91                         return marked.Contains (provider);
92                 }
93
94                 public void Processed (IMetadataTokenProvider provider)
95                 {
96                         processed.Add (provider);
97                 }
98
99                 public bool IsProcessed (IMetadataTokenProvider provider)
100                 {
101                         return processed.Contains (provider);
102                 }
103
104                 public bool IsPreserved (TypeDefinition type)
105                 {
106                         return preserved_types.ContainsKey (type);
107                 }
108
109                 public void SetPreserve (TypeDefinition type, TypePreserve preserve)
110                 {
111                         preserved_types [type] = preserve;
112                 }
113
114                 public TypePreserve GetPreserve (TypeDefinition type)
115                 {
116                         TypePreserve preserve;
117                         if (preserved_types.TryGetValue (type, out preserve))
118                                 return preserve;
119
120                         throw new NotSupportedException ();
121                 }
122
123                 public void SetPublic (IMetadataTokenProvider provider)
124                 {
125                         public_api.Add (provider);
126                 }
127
128                 public bool IsPublic (IMetadataTokenProvider provider)
129                 {
130                         return public_api.Contains (provider);
131                 }
132
133                 public void AddOverride (MethodDefinition @base, MethodDefinition @override)
134                 {
135                         var methods = GetOverrides (@base);
136                         if (methods == null) {
137                                 methods = new List<MethodDefinition> ();
138                                 override_methods [@base] = methods;
139                         }
140
141                         methods.Add (@override);
142                 }
143
144                 public List<MethodDefinition> GetOverrides (MethodDefinition method)
145                 {
146                         List<MethodDefinition> overrides;
147                         if (override_methods.TryGetValue (method, out overrides))
148                                 return overrides;
149
150                         return null;
151                 }
152
153                 public void AddBaseMethod (MethodDefinition method, MethodDefinition @base)
154                 {
155                         var methods = GetBaseMethods (method);
156                         if (methods == null) {
157                                 methods = new List<MethodDefinition> ();
158                                 base_methods [method] = methods;
159                         }
160
161                         methods.Add (@base);
162                 }
163
164                 public List<MethodDefinition> GetBaseMethods (MethodDefinition method)
165                 {
166                         List<MethodDefinition> bases;
167                         if (base_methods.TryGetValue (method, out bases))
168                                 return bases;
169
170                         return null;
171                 }
172
173                 public List<MethodDefinition> GetPreservedMethods (TypeDefinition type)
174                 {
175                         return GetPreservedMethods (type as IMemberDefinition);
176                 }
177
178                 public void AddPreservedMethod (TypeDefinition type, MethodDefinition method)
179                 {
180                         AddPreservedMethod (type as IMemberDefinition, method);
181                 }
182
183                 public List<MethodDefinition> GetPreservedMethods (MethodDefinition method)
184                 {
185                         return GetPreservedMethods (method as IMemberDefinition);
186                 }
187
188                 public void AddPreservedMethod (MethodDefinition key, MethodDefinition method)
189                 {
190                         AddPreservedMethod (key as IMemberDefinition, method);
191                 }
192
193                 List<MethodDefinition> GetPreservedMethods (IMemberDefinition definition)
194                 {
195                         List<MethodDefinition> preserved;
196                         if (preserved_methods.TryGetValue (definition, out preserved))
197                                 return preserved;
198
199                         return null;
200                 }
201
202                 void AddPreservedMethod (IMemberDefinition definition, MethodDefinition method)
203                 {
204                         var methods = GetPreservedMethods (definition);
205                         if (methods == null) {
206                                 methods = new List<MethodDefinition> ();
207                                 preserved_methods [definition] = methods;
208                         }
209
210                         methods.Add (method);
211                 }
212
213                 public Dictionary<IMetadataTokenProvider, object> GetCustomAnnotations (object key)
214                 {
215                         Dictionary<IMetadataTokenProvider, object> slots;
216                         if (custom_annotations.TryGetValue (key, out slots))
217                                 return slots;
218
219                         slots = new Dictionary<IMetadataTokenProvider, object> ();
220                         custom_annotations.Add (key, slots);
221                         return slots;
222                 }
223         }
224 }