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

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/CSharpProjectContent.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.Linq;
22
 
using System.Runtime.Serialization;
23
 
using ICSharpCode.NRefactory.CSharp.TypeSystem;
24
 
using ICSharpCode.NRefactory.TypeSystem;
25
 
using ICSharpCode.NRefactory.TypeSystem.Implementation;
26
 
using ICSharpCode.NRefactory.Utils;
27
 
 
28
 
namespace ICSharpCode.NRefactory.CSharp
29
 
{
30
 
        [Serializable]
31
 
        public class CSharpProjectContent : IProjectContent
32
 
        {
33
 
                string assemblyName;
34
 
                Dictionary<string, IParsedFile> parsedFiles;
35
 
                List<IAssemblyReference> assemblyReferences;
36
 
                
37
 
                public CSharpProjectContent()
38
 
                {
39
 
                        this.assemblyName = string.Empty;
40
 
                        this.parsedFiles = new Dictionary<string, IParsedFile>(Platform.FileNameComparer);
41
 
                        this.assemblyReferences = new List<IAssemblyReference>();
42
 
                }
43
 
                
44
 
                protected CSharpProjectContent(CSharpProjectContent pc)
45
 
                {
46
 
                        this.assemblyName = pc.assemblyName;
47
 
                        this.parsedFiles = new Dictionary<string, IParsedFile>(pc.parsedFiles, Platform.FileNameComparer);
48
 
                        this.assemblyReferences = new List<IAssemblyReference>(pc.assemblyReferences);
49
 
                        this.Location = pc.Location;
50
 
                }
51
 
                public string Location { get; set; }
52
 
                public IEnumerable<IParsedFile> Files {
53
 
                        get { return parsedFiles.Values; }
54
 
                }
55
 
                
56
 
                public IEnumerable<IAssemblyReference> AssemblyReferences {
57
 
                        get { return assemblyReferences; }
58
 
                }
59
 
                
60
 
                public string AssemblyName {
61
 
                        get { return assemblyName; }
62
 
                }
63
 
                
64
 
                public IEnumerable<IUnresolvedAttribute> AssemblyAttributes {
65
 
                        get {
66
 
                                return this.Files.SelectMany(f => f.AssemblyAttributes);
67
 
                        }
68
 
                }
69
 
                
70
 
                public IEnumerable<IUnresolvedAttribute> ModuleAttributes {
71
 
                        get {
72
 
                                return this.Files.SelectMany(f => f.ModuleAttributes);
73
 
                        }
74
 
                }
75
 
                
76
 
                public IEnumerable<IUnresolvedTypeDefinition> TopLevelTypeDefinitions {
77
 
                        get {
78
 
                                return this.Files.SelectMany(f => f.TopLevelTypeDefinitions);
79
 
                        }
80
 
                }
81
 
                
82
 
                public IParsedFile GetFile(string fileName)
83
 
                {
84
 
                        IParsedFile file;
85
 
                        if (parsedFiles.TryGetValue(fileName, out file))
86
 
                                return file;
87
 
                        else
88
 
                                return null;
89
 
                }
90
 
                
91
 
                public virtual ICompilation CreateCompilation()
92
 
                {
93
 
                        var solutionSnapshot = new DefaultSolutionSnapshot();
94
 
                        ICompilation compilation = new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
95
 
                        solutionSnapshot.AddCompilation(this, compilation);
96
 
                        return compilation;
97
 
                }
98
 
                
99
 
                public virtual ICompilation CreateCompilation(ISolutionSnapshot solutionSnapshot)
100
 
                {
101
 
                        return new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
102
 
                }
103
 
                
104
 
                protected virtual CSharpProjectContent Clone()
105
 
                {
106
 
                        return new CSharpProjectContent(this);
107
 
                }
108
 
                
109
 
                public IProjectContent SetAssemblyName(string newAssemblyName)
110
 
                {
111
 
                        CSharpProjectContent pc = Clone();
112
 
                        pc.assemblyName = newAssemblyName;
113
 
                        return pc;
114
 
                }
115
 
                
116
 
                public IProjectContent AddAssemblyReferences(IEnumerable<IAssemblyReference> references)
117
 
                {
118
 
                        CSharpProjectContent pc = Clone();
119
 
                        pc.assemblyReferences.AddRange(references);
120
 
                        return pc;
121
 
                }
122
 
                
123
 
                public IProjectContent RemoveAssemblyReferences(IEnumerable<IAssemblyReference> references)
124
 
                {
125
 
                        CSharpProjectContent pc = Clone();
126
 
                        pc.assemblyReferences.RemoveAll(r => references.Contains(r));
127
 
                        return pc;
128
 
                }
129
 
                
130
 
                public IProjectContent UpdateProjectContent(IParsedFile oldFile, IParsedFile newFile)
131
 
                {
132
 
                        if (oldFile == null && newFile == null)
133
 
                                return this;
134
 
                        if (oldFile != null && newFile != null) {
135
 
                                if (!Platform.FileNameComparer.Equals(oldFile.FileName, newFile.FileName))
136
 
                                        throw new ArgumentException("When both oldFile and newFile are specified, they must use the same file name.");
137
 
                        }
138
 
                        CSharpProjectContent pc = Clone();
139
 
                        if (newFile == null)
140
 
                                pc.parsedFiles.Remove(oldFile.FileName);
141
 
                        else
142
 
                                pc.parsedFiles[newFile.FileName] = newFile;
143
 
                        return pc;
144
 
                }
145
 
                
146
 
                public IProjectContent UpdateProjectContent(IEnumerable<IParsedFile> oldFiles, IEnumerable<IParsedFile> newFiles)
147
 
                {
148
 
                        CSharpProjectContent pc = Clone();
149
 
                        if (oldFiles != null) {
150
 
                                foreach (var oldFile in oldFiles) {
151
 
                                        pc.parsedFiles.Remove(oldFile.FileName);
152
 
                                }
153
 
                        }
154
 
                        if (newFiles != null) {
155
 
                                foreach (var newFile in newFiles) {
156
 
                                        pc.parsedFiles.Add(newFile.FileName, newFile);
157
 
                                }
158
 
                        }
159
 
                        return pc;
160
 
                }
161
 
                
162
 
                IAssembly IAssemblyReference.Resolve(ITypeResolveContext context)
163
 
                {
164
 
                        if (context == null)
165
 
                                throw new ArgumentNullException("context");
166
 
                        var cache = context.Compilation.CacheManager;
167
 
                        IAssembly asm = (IAssembly)cache.GetShared(this);
168
 
                        if (asm != null) {
169
 
                                return asm;
170
 
                        } else {
171
 
                                asm = new CSharpAssembly(context.Compilation, this);
172
 
                                return (IAssembly)cache.GetOrAddShared(this, asm);
173
 
                        }
174
 
                }
175
 
        }
176
 
}