1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
21
using System.Collections.ObjectModel;
23
using ICSharpCode.NRefactory.Utils;
25
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
28
/// Simple compilation implementation.
30
public class SimpleCompilation : ICompilation
32
readonly ISolutionSnapshot solutionSnapshot;
33
readonly ITypeResolveContext context;
34
readonly CacheManager cacheManager = new CacheManager();
35
readonly KnownTypeCache knownTypeCache;
36
readonly IAssembly mainAssembly;
37
readonly IList<IAssembly> assemblies;
38
readonly IList<IAssembly> referencedAssemblies;
39
INamespace rootNamespace;
41
public SimpleCompilation(IUnresolvedAssembly mainAssembly, params IAssemblyReference[] assemblyReferences)
42
: this(new DefaultSolutionSnapshot(), mainAssembly, (IEnumerable<IAssemblyReference>)assemblyReferences)
46
public SimpleCompilation(IUnresolvedAssembly mainAssembly, IEnumerable<IAssemblyReference> assemblyReferences)
47
: this(new DefaultSolutionSnapshot(), mainAssembly, assemblyReferences)
51
public SimpleCompilation(ISolutionSnapshot solutionSnapshot, IUnresolvedAssembly mainAssembly, params IAssemblyReference[] assemblyReferences)
52
: this(solutionSnapshot, mainAssembly, (IEnumerable<IAssemblyReference>)assemblyReferences)
56
public SimpleCompilation(ISolutionSnapshot solutionSnapshot, IUnresolvedAssembly mainAssembly, IEnumerable<IAssemblyReference> assemblyReferences)
58
if (solutionSnapshot == null)
59
throw new ArgumentNullException("solutionSnapshot");
60
if (mainAssembly == null)
61
throw new ArgumentNullException("mainAssembly");
62
if (assemblyReferences == null)
63
throw new ArgumentNullException("assemblyReferences");
64
this.solutionSnapshot = solutionSnapshot;
65
this.context = new SimpleTypeResolveContext(this);
66
this.mainAssembly = mainAssembly.Resolve(context);
67
List<IAssembly> assemblies = new List<IAssembly>();
68
assemblies.Add(this.mainAssembly);
69
List<IAssembly> referencedAssemblies = new List<IAssembly>();
70
foreach (var asmRef in assemblyReferences) {
71
IAssembly asm = asmRef.Resolve(context);
72
if (asm != null && !assemblies.Contains(asm))
74
if (asm != null && !referencedAssemblies.Contains(asm))
75
referencedAssemblies.Add(asm);
77
this.assemblies = assemblies.AsReadOnly();
78
this.referencedAssemblies = referencedAssemblies.AsReadOnly();
79
this.knownTypeCache = new KnownTypeCache(this);
82
public IAssembly MainAssembly {
84
if (mainAssembly == null)
85
throw new InvalidOperationException("Compilation isn't initialized yet");
90
public IList<IAssembly> Assemblies {
92
if (assemblies == null)
93
throw new InvalidOperationException("Compilation isn't initialized yet");
98
[ObsoleteAttribute("Use compilation.Assemblies.Where(asm != compilation.MainAssembly) instead.")]
99
public IList<IAssembly> ReferencedAssemblies {
101
if (referencedAssemblies == null)
102
throw new InvalidOperationException("Compilation isn't initialized yet");
103
return referencedAssemblies;
107
public ITypeResolveContext TypeResolveContext {
108
get { return context; }
111
public INamespace RootNamespace {
113
INamespace ns = LazyInit.VolatileRead(ref this.rootNamespace);
117
if (referencedAssemblies == null)
118
throw new InvalidOperationException("Compilation isn't initialized yet");
119
return LazyInit.GetOrSet(ref this.rootNamespace, CreateRootNamespace());
124
protected virtual INamespace CreateRootNamespace()
126
INamespace[] namespaces = new INamespace[referencedAssemblies.Count + 1];
127
namespaces[0] = mainAssembly.RootNamespace;
128
for (int i = 0; i < referencedAssemblies.Count; i++) {
129
namespaces[i + 1] = referencedAssemblies[i].RootNamespace;
131
return new MergedNamespace(this, namespaces);
134
public CacheManager CacheManager {
135
get { return cacheManager; }
138
public virtual INamespace GetNamespaceForExternAlias(string alias)
143
public IType FindType(KnownTypeCode typeCode)
145
return knownTypeCache.FindType(typeCode);
148
public StringComparer NameComparer {
149
get { return StringComparer.Ordinal; }
152
public ISolutionSnapshot SolutionSnapshot {
153
get { return solutionSnapshot; }
156
public override string ToString()
158
return "[SimpleCompilation " + mainAssembly.AssemblyName + "]";