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;
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;
28
namespace ICSharpCode.NRefactory.CSharp
31
public class CSharpProjectContent : IProjectContent
34
Dictionary<string, IParsedFile> parsedFiles;
35
List<IAssemblyReference> assemblyReferences;
37
public CSharpProjectContent()
39
this.assemblyName = string.Empty;
40
this.parsedFiles = new Dictionary<string, IParsedFile>(Platform.FileNameComparer);
41
this.assemblyReferences = new List<IAssemblyReference>();
44
protected CSharpProjectContent(CSharpProjectContent pc)
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;
51
public string Location { get; set; }
52
public IEnumerable<IParsedFile> Files {
53
get { return parsedFiles.Values; }
56
public IEnumerable<IAssemblyReference> AssemblyReferences {
57
get { return assemblyReferences; }
60
public string AssemblyName {
61
get { return assemblyName; }
64
public IEnumerable<IUnresolvedAttribute> AssemblyAttributes {
66
return this.Files.SelectMany(f => f.AssemblyAttributes);
70
public IEnumerable<IUnresolvedAttribute> ModuleAttributes {
72
return this.Files.SelectMany(f => f.ModuleAttributes);
76
public IEnumerable<IUnresolvedTypeDefinition> TopLevelTypeDefinitions {
78
return this.Files.SelectMany(f => f.TopLevelTypeDefinitions);
82
public IParsedFile GetFile(string fileName)
85
if (parsedFiles.TryGetValue(fileName, out file))
91
public virtual ICompilation CreateCompilation()
93
var solutionSnapshot = new DefaultSolutionSnapshot();
94
ICompilation compilation = new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
95
solutionSnapshot.AddCompilation(this, compilation);
99
public virtual ICompilation CreateCompilation(ISolutionSnapshot solutionSnapshot)
101
return new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
104
protected virtual CSharpProjectContent Clone()
106
return new CSharpProjectContent(this);
109
public IProjectContent SetAssemblyName(string newAssemblyName)
111
CSharpProjectContent pc = Clone();
112
pc.assemblyName = newAssemblyName;
116
public IProjectContent AddAssemblyReferences(IEnumerable<IAssemblyReference> references)
118
CSharpProjectContent pc = Clone();
119
pc.assemblyReferences.AddRange(references);
123
public IProjectContent RemoveAssemblyReferences(IEnumerable<IAssemblyReference> references)
125
CSharpProjectContent pc = Clone();
126
pc.assemblyReferences.RemoveAll(r => references.Contains(r));
130
public IProjectContent UpdateProjectContent(IParsedFile oldFile, IParsedFile newFile)
132
if (oldFile == null && newFile == null)
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.");
138
CSharpProjectContent pc = Clone();
140
pc.parsedFiles.Remove(oldFile.FileName);
142
pc.parsedFiles[newFile.FileName] = newFile;
146
public IProjectContent UpdateProjectContent(IEnumerable<IParsedFile> oldFiles, IEnumerable<IParsedFile> newFiles)
148
CSharpProjectContent pc = Clone();
149
if (oldFiles != null) {
150
foreach (var oldFile in oldFiles) {
151
pc.parsedFiles.Remove(oldFile.FileName);
154
if (newFiles != null) {
155
foreach (var newFile in newFiles) {
156
pc.parsedFiles.Add(newFile.FileName, newFile);
162
IAssembly IAssemblyReference.Resolve(ITypeResolveContext context)
165
throw new ArgumentNullException("context");
166
var cache = context.Compilation.CacheManager;
167
IAssembly asm = (IAssembly)cache.GetShared(this);
171
asm = new CSharpAssembly(context.Compilation, this);
172
return (IAssembly)cache.GetOrAddShared(this, asm);