5 // Jb Evain (jbevain@gmail.com)
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;
36 namespace Mono.Linker {
38 public class LinkContext {
41 AssemblyAction _coreAction;
43 string _outputDirectory;
44 Hashtable _parameters;
47 AssemblyResolver _resolver;
49 ReaderParameters _readerParameters;
50 ISymbolReaderProvider _symbolReaderProvider;
51 ISymbolWriterProvider _symbolWriterProvider;
53 AnnotationStore _annotations;
55 public Pipeline Pipeline {
56 get { return _pipeline; }
59 public AnnotationStore Annotations {
60 get { return _annotations; }
63 public string OutputDirectory {
64 get { return _outputDirectory; }
65 set { _outputDirectory = value; }
68 public AssemblyAction CoreAction {
69 get { return _coreAction; }
70 set { _coreAction = value; }
73 public bool LinkSymbols {
74 get { return _linkSymbols; }
75 set { _linkSymbols = value; }
78 public IDictionary Actions {
79 get { return _actions; }
82 public AssemblyResolver Resolver {
83 get { return _resolver; }
86 public ISymbolReaderProvider SymbolReaderProvider {
87 get { return _symbolReaderProvider; }
89 _symbolReaderProvider = value;
90 _readerParameters.SymbolReaderProvider = value;
94 public ISymbolWriterProvider SymbolWriterProvider {
95 get { return _symbolWriterProvider; }
96 set { _symbolWriterProvider = value; }
99 public LinkContext (Pipeline pipeline)
100 : this (pipeline, new AssemblyResolver ())
104 public LinkContext (Pipeline pipeline, AssemblyResolver resolver)
106 _pipeline = pipeline;
107 _resolver = resolver;
108 _actions = new Hashtable ();
109 _parameters = new Hashtable ();
110 _annotations = new AnnotationStore ();
111 _readerParameters = new ReaderParameters {
112 AssemblyResolver = _resolver,
116 public TypeDefinition GetType (string fullName)
118 int pos = fullName.IndexOf (",");
119 fullName = fullName.Replace ("+", "/");
121 foreach (AssemblyDefinition asm in GetAssemblies ()) {
122 var type = asm.MainModule.GetType (fullName);
130 string asmname = fullName.Substring (pos + 1);
131 fullName = fullName.Substring (0, pos);
132 AssemblyDefinition assembly = Resolve (AssemblyNameReference.Parse (asmname));
133 return assembly.MainModule.GetType (fullName);
136 public AssemblyDefinition Resolve (string name)
138 if (File.Exists (name)) {
139 AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly (name, _readerParameters);
140 _resolver.CacheAssembly (assembly);
144 return Resolve (new AssemblyNameReference (name, new Version ()));
147 public AssemblyDefinition Resolve (IMetadataScope scope)
149 AssemblyNameReference reference = GetReference (scope);
151 AssemblyDefinition assembly = _resolver.Resolve (reference, _readerParameters);
153 if (SeenFirstTime (assembly)) {
154 SafeReadSymbols (assembly);
155 SetAction (assembly);
161 bool SeenFirstTime (AssemblyDefinition assembly)
163 return !_annotations.HasAction (assembly);
166 public void SafeReadSymbols (AssemblyDefinition assembly)
172 if (_symbolReaderProvider != null) {
173 var symbolReader = _symbolReaderProvider.GetSymbolReader (
175 assembly.MainModule.FullyQualifiedName);
177 _annotations.AddSymbolReader (assembly, symbolReader);
178 assembly.MainModule.ReadSymbols (symbolReader);
180 assembly.MainModule.ReadSymbols ();
184 static AssemblyNameReference GetReference (IMetadataScope scope)
186 AssemblyNameReference reference;
187 if (scope is ModuleDefinition) {
188 AssemblyDefinition asm = ((ModuleDefinition) scope).Assembly;
189 reference = asm.Name;
191 reference = (AssemblyNameReference) scope;
196 void SetAction (AssemblyDefinition assembly)
198 AssemblyAction action = AssemblyAction.Link;
200 AssemblyNameDefinition name = assembly.Name;
202 if (_actions.Contains (name.Name))
203 action = (AssemblyAction) _actions [name.Name];
204 else if (IsCore (name))
205 action = _coreAction;
207 _annotations.SetAction (assembly, action);
210 static bool IsCore (AssemblyNameReference name)
214 case "Accessibility":
215 case "Mono.Security":
218 return name.Name.StartsWith ("System")
219 || name.Name.StartsWith ("Microsoft");
223 public AssemblyDefinition [] GetAssemblies ()
225 IDictionary cache = _resolver.AssemblyCache;
226 AssemblyDefinition [] asms = new AssemblyDefinition [cache.Count];
227 cache.Values.CopyTo (asms, 0);
231 public void SetParameter (string key, string value)
233 _parameters [key] = value;
236 public bool HasParameter (string key)
238 return _parameters.Contains (key);
241 public string GetParameter (string key)
243 return (string) _parameters [key];