~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/TypeSystem/Implementation/SimpleCompilation.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
2
 
// 
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:
8
 
// 
9
 
// The above copyright notice and this permission notice shall be included in all copies or
10
 
// substantial portions of the Software.
11
 
// 
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.
18
 
 
19
 
using System;
20
 
using System.Collections.Generic;
21
 
using System.Collections.ObjectModel;
22
 
using System.Linq;
23
 
using ICSharpCode.NRefactory.Utils;
24
 
 
25
 
namespace ICSharpCode.NRefactory.TypeSystem.Implementation
26
 
{
27
 
        /// <summary>
28
 
        /// Simple compilation implementation.
29
 
        /// </summary>
30
 
        public class SimpleCompilation : ICompilation
31
 
        {
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;
40
 
                
41
 
                public SimpleCompilation(IUnresolvedAssembly mainAssembly, params IAssemblyReference[] assemblyReferences)
42
 
                        : this(new DefaultSolutionSnapshot(), mainAssembly, (IEnumerable<IAssemblyReference>)assemblyReferences)
43
 
                {
44
 
                }
45
 
                
46
 
                public SimpleCompilation(IUnresolvedAssembly mainAssembly, IEnumerable<IAssemblyReference> assemblyReferences)
47
 
                        : this(new DefaultSolutionSnapshot(), mainAssembly, assemblyReferences)
48
 
                {
49
 
                }
50
 
                
51
 
                public SimpleCompilation(ISolutionSnapshot solutionSnapshot, IUnresolvedAssembly mainAssembly, params IAssemblyReference[] assemblyReferences)
52
 
                        : this(solutionSnapshot, mainAssembly, (IEnumerable<IAssemblyReference>)assemblyReferences)
53
 
                {
54
 
                }
55
 
                
56
 
                public SimpleCompilation(ISolutionSnapshot solutionSnapshot, IUnresolvedAssembly mainAssembly, IEnumerable<IAssemblyReference> assemblyReferences)
57
 
                {
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))
73
 
                                        assemblies.Add(asm);
74
 
                                if (asm != null && !referencedAssemblies.Contains(asm))
75
 
                                        referencedAssemblies.Add(asm);
76
 
                        }
77
 
                        this.assemblies = assemblies.AsReadOnly();
78
 
                        this.referencedAssemblies = referencedAssemblies.AsReadOnly();
79
 
                        this.knownTypeCache = new KnownTypeCache(this);
80
 
                }
81
 
                
82
 
                public IAssembly MainAssembly {
83
 
                        get {
84
 
                                if (mainAssembly == null)
85
 
                                        throw new InvalidOperationException("Compilation isn't initialized yet");
86
 
                                return mainAssembly;
87
 
                        }
88
 
                }
89
 
                
90
 
                public IList<IAssembly> Assemblies {
91
 
                        get {
92
 
                                if (assemblies == null)
93
 
                                        throw new InvalidOperationException("Compilation isn't initialized yet");
94
 
                                return assemblies;
95
 
                        }
96
 
                }
97
 
                
98
 
                [ObsoleteAttribute("Use compilation.Assemblies.Where(asm != compilation.MainAssembly) instead.")]
99
 
                public IList<IAssembly> ReferencedAssemblies {
100
 
                        get {
101
 
                                if (referencedAssemblies == null)
102
 
                                        throw new InvalidOperationException("Compilation isn't initialized yet");
103
 
                                return referencedAssemblies;
104
 
                        }
105
 
                }
106
 
                
107
 
                public ITypeResolveContext TypeResolveContext {
108
 
                        get { return context; }
109
 
                }
110
 
                
111
 
                public INamespace RootNamespace {
112
 
                        get {
113
 
                                INamespace ns = LazyInit.VolatileRead(ref this.rootNamespace);
114
 
                                if (ns != null) {
115
 
                                        return ns;
116
 
                                } else {
117
 
                                        if (referencedAssemblies == null)
118
 
                                                throw new InvalidOperationException("Compilation isn't initialized yet");
119
 
                                        return LazyInit.GetOrSet(ref this.rootNamespace, CreateRootNamespace());
120
 
                                }
121
 
                        }
122
 
                }
123
 
                
124
 
                protected virtual INamespace CreateRootNamespace()
125
 
                {
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;
130
 
                        }
131
 
                        return new MergedNamespace(this, namespaces);
132
 
                }
133
 
                
134
 
                public CacheManager CacheManager {
135
 
                        get { return cacheManager; }
136
 
                }
137
 
                
138
 
                public virtual INamespace GetNamespaceForExternAlias(string alias)
139
 
                {
140
 
                        return null;
141
 
                }
142
 
                
143
 
                public IType FindType(KnownTypeCode typeCode)
144
 
                {
145
 
                        return knownTypeCache.FindType(typeCode);
146
 
                }
147
 
                
148
 
                public StringComparer NameComparer {
149
 
                        get { return StringComparer.Ordinal; }
150
 
                }
151
 
                
152
 
                public ISolutionSnapshot SolutionSnapshot {
153
 
                        get { return solutionSnapshot; }
154
 
                }
155
 
                
156
 
                public override string ToString()
157
 
                {
158
 
                        return "[SimpleCompilation " + mainAssembly.AssemblyName + "]";
159
 
                }
160
 
        }
161
 
}