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;
35 namespace Mono.Linker {
37 public class AnnotationStore {
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>> ();
49 readonly Dictionary<object, Dictionary<IMetadataTokenProvider, object>> custom_annotations = new Dictionary<object, Dictionary<IMetadataTokenProvider, object>> ();
51 public AssemblyAction GetAction (AssemblyDefinition assembly)
53 AssemblyAction action;
54 if (assembly_actions.TryGetValue (assembly, out action))
57 throw new NotSupportedException ();
60 public MethodAction GetAction (MethodDefinition method)
63 if (method_actions.TryGetValue (method, out action))
66 return MethodAction.Nothing;
69 public void SetAction (AssemblyDefinition assembly, AssemblyAction action)
71 assembly_actions [assembly] = action;
74 public bool HasAction (AssemblyDefinition assembly)
76 return assembly_actions.ContainsKey (assembly);
79 public void SetAction (MethodDefinition method, MethodAction action)
81 method_actions [method] = action;
84 public void Mark (IMetadataTokenProvider provider)
86 marked.Add (provider);
89 public bool IsMarked (IMetadataTokenProvider provider)
91 return marked.Contains (provider);
94 public void Processed (IMetadataTokenProvider provider)
96 processed.Add (provider);
99 public bool IsProcessed (IMetadataTokenProvider provider)
101 return processed.Contains (provider);
104 public bool IsPreserved (TypeDefinition type)
106 return preserved_types.ContainsKey (type);
109 public void SetPreserve (TypeDefinition type, TypePreserve preserve)
111 preserved_types [type] = preserve;
114 public TypePreserve GetPreserve (TypeDefinition type)
116 TypePreserve preserve;
117 if (preserved_types.TryGetValue (type, out preserve))
120 throw new NotSupportedException ();
123 public void SetPublic (IMetadataTokenProvider provider)
125 public_api.Add (provider);
128 public bool IsPublic (IMetadataTokenProvider provider)
130 return public_api.Contains (provider);
133 public void AddOverride (MethodDefinition @base, MethodDefinition @override)
135 var methods = GetOverrides (@base);
136 if (methods == null) {
137 methods = new List<MethodDefinition> ();
138 override_methods [@base] = methods;
141 methods.Add (@override);
144 public List<MethodDefinition> GetOverrides (MethodDefinition method)
146 List<MethodDefinition> overrides;
147 if (override_methods.TryGetValue (method, out overrides))
153 public void AddBaseMethod (MethodDefinition method, MethodDefinition @base)
155 var methods = GetBaseMethods (method);
156 if (methods == null) {
157 methods = new List<MethodDefinition> ();
158 base_methods [method] = methods;
164 public List<MethodDefinition> GetBaseMethods (MethodDefinition method)
166 List<MethodDefinition> bases;
167 if (base_methods.TryGetValue (method, out bases))
173 public List<MethodDefinition> GetPreservedMethods (TypeDefinition type)
175 return GetPreservedMethods (type as IMemberDefinition);
178 public void AddPreservedMethod (TypeDefinition type, MethodDefinition method)
180 AddPreservedMethod (type as IMemberDefinition, method);
183 public List<MethodDefinition> GetPreservedMethods (MethodDefinition method)
185 return GetPreservedMethods (method as IMemberDefinition);
188 public void AddPreservedMethod (MethodDefinition key, MethodDefinition method)
190 AddPreservedMethod (key as IMemberDefinition, method);
193 List<MethodDefinition> GetPreservedMethods (IMemberDefinition definition)
195 List<MethodDefinition> preserved;
196 if (preserved_methods.TryGetValue (definition, out preserved))
202 void AddPreservedMethod (IMemberDefinition definition, MethodDefinition method)
204 var methods = GetPreservedMethods (definition);
205 if (methods == null) {
206 methods = new List<MethodDefinition> ();
207 preserved_methods [definition] = methods;
210 methods.Add (method);
213 public Dictionary<IMetadataTokenProvider, object> GetCustomAnnotations (object key)
215 Dictionary<IMetadataTokenProvider, object> slots;
216 if (custom_annotations.TryGetValue (key, out slots))
219 slots = new Dictionary<IMetadataTokenProvider, object> ();
220 custom_annotations.Add (key, slots);