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