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;
34 namespace Mono.Linker {
36 public class LinkContext {
39 AssemblyAction _coreAction;
41 string _outputDirectory;
42 Hashtable _parameters;
45 AssemblyResolver _resolver;
47 ReaderParameters _readerParameters;
49 AnnotationStore _annotations;
51 public Pipeline Pipeline {
52 get { return _pipeline; }
55 public AnnotationStore Annotations {
56 get { return _annotations; }
59 public string OutputDirectory {
60 get { return _outputDirectory; }
61 set { _outputDirectory = value; }
64 public AssemblyAction CoreAction {
65 get { return _coreAction; }
66 set { _coreAction = value; }
69 public bool LinkSymbols {
70 get { return _linkSymbols; }
71 set { _linkSymbols = value; }
74 public IDictionary Actions {
75 get { return _actions; }
78 public AssemblyResolver Resolver {
79 get { return _resolver; }
82 public LinkContext (Pipeline pipeline)
83 : this (pipeline, new AssemblyResolver ())
87 public LinkContext (Pipeline pipeline, AssemblyResolver resolver)
91 _actions = new Hashtable ();
92 _parameters = new Hashtable ();
93 _annotations = new AnnotationStore ();
94 _readerParameters = new ReaderParameters {
95 AssemblyResolver = _resolver,
99 public TypeDefinition GetType (string fullName)
101 int pos = fullName.IndexOf (",");
102 fullName = fullName.Replace ("+", "/");
104 foreach (AssemblyDefinition asm in GetAssemblies ()) {
105 var type = asm.MainModule.GetType (fullName);
113 string asmname = fullName.Substring (pos + 1);
114 fullName = fullName.Substring (0, pos);
115 AssemblyDefinition assembly = Resolve (AssemblyNameReference.Parse (asmname));
116 return assembly.MainModule.GetType (fullName);
119 public AssemblyDefinition Resolve (string name)
121 if (File.Exists (name)) {
122 AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly (name, _readerParameters);
123 _resolver.CacheAssembly (assembly);
127 return Resolve (new AssemblyNameReference (name, new Version ()));
130 public AssemblyDefinition Resolve (IMetadataScope scope)
132 AssemblyNameReference reference = GetReference (scope);
134 AssemblyDefinition assembly = _resolver.Resolve (reference, _readerParameters);
136 if (SeenFirstTime (assembly)) {
137 SafeReadSymbols (assembly);
138 SetAction (assembly);
144 bool SeenFirstTime (AssemblyDefinition assembly)
146 return !_annotations.HasAction (assembly);
149 public void SafeReadSymbols (AssemblyDefinition assembly)
152 assembly.MainModule.ReadSymbols ();
156 static AssemblyNameReference GetReference (IMetadataScope scope)
158 AssemblyNameReference reference;
159 if (scope is ModuleDefinition) {
160 AssemblyDefinition asm = ((ModuleDefinition) scope).Assembly;
161 reference = asm.Name;
163 reference = (AssemblyNameReference) scope;
168 void SetAction (AssemblyDefinition assembly)
170 AssemblyAction action = AssemblyAction.Link;
172 AssemblyNameDefinition name = assembly.Name;
174 if (_actions.Contains (name.Name))
175 action = (AssemblyAction) _actions [name.Name];
176 else if (IsCore (name))
177 action = _coreAction;
179 _annotations.SetAction (assembly, action);
182 static bool IsCore (AssemblyNameReference name)
186 case "Accessibility":
187 case "Mono.Security":
190 return name.Name.StartsWith ("System")
191 || name.Name.StartsWith ("Microsoft");
195 public AssemblyDefinition [] GetAssemblies ()
197 IDictionary cache = _resolver.AssemblyCache;
198 AssemblyDefinition [] asms = new AssemblyDefinition [cache.Count];
199 cache.Values.CopyTo (asms, 0);
203 public void SetParameter (string key, string value)
205 _parameters [key] = value;
208 public bool HasParameter (string key)
210 return _parameters.Contains (key);
213 public string GetParameter (string key)
215 return (string) _parameters [key];