// Copyright (c) AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using ICSharpCode.NRefactory.CSharp.TypeSystem; using ICSharpCode.NRefactory.TypeSystem; using ICSharpCode.NRefactory.TypeSystem.Implementation; using ICSharpCode.NRefactory.Utils; namespace ICSharpCode.NRefactory.CSharp { [Serializable] public class CSharpProjectContent : IProjectContent { string assemblyName; Dictionary parsedFiles; List assemblyReferences; public CSharpProjectContent() { this.assemblyName = string.Empty; this.parsedFiles = new Dictionary(Platform.FileNameComparer); this.assemblyReferences = new List(); } protected CSharpProjectContent(CSharpProjectContent pc) { this.assemblyName = pc.assemblyName; this.parsedFiles = new Dictionary(pc.parsedFiles, Platform.FileNameComparer); this.assemblyReferences = new List(pc.assemblyReferences); } public IEnumerable Files { get { return parsedFiles.Values; } } public IEnumerable AssemblyReferences { get { return assemblyReferences; } } public string AssemblyName { get { return assemblyName; } } public IEnumerable AssemblyAttributes { get { return this.Files.SelectMany(f => f.AssemblyAttributes); } } public IEnumerable ModuleAttributes { get { return this.Files.SelectMany(f => f.ModuleAttributes); } } public IEnumerable TopLevelTypeDefinitions { get { return this.Files.SelectMany(f => f.TopLevelTypeDefinitions); } } public IParsedFile GetFile(string fileName) { IParsedFile file; if (parsedFiles.TryGetValue(fileName, out file)) return file; else return null; } public virtual ICompilation CreateCompilation() { var solutionSnapshot = new DefaultSolutionSnapshot(); ICompilation compilation = new SimpleCompilation(solutionSnapshot, this, assemblyReferences); solutionSnapshot.AddCompilation(this, compilation); return compilation; } public virtual ICompilation CreateCompilation(ISolutionSnapshot solutionSnapshot) { return new SimpleCompilation(solutionSnapshot, this, assemblyReferences); } protected virtual CSharpProjectContent Clone() { return new CSharpProjectContent(this); } public IProjectContent SetAssemblyName(string newAssemblyName) { CSharpProjectContent pc = Clone(); pc.assemblyName = newAssemblyName; return pc; } public IProjectContent AddAssemblyReferences(IEnumerable references) { CSharpProjectContent pc = Clone(); pc.assemblyReferences.AddRange(references); return pc; } public IProjectContent RemoveAssemblyReferences(IEnumerable references) { CSharpProjectContent pc = Clone(); pc.assemblyReferences.RemoveAll(r => references.Contains(r)); return pc; } public IProjectContent UpdateProjectContent(IParsedFile oldFile, IParsedFile newFile) { if (oldFile == null && newFile == null) return this; if (oldFile != null && newFile != null) { if (!Platform.FileNameComparer.Equals(oldFile.FileName, newFile.FileName)) throw new ArgumentException("When both oldFile and newFile are specified, they must use the same file name."); } CSharpProjectContent pc = Clone(); if (newFile == null) pc.parsedFiles.Remove(oldFile.FileName); else pc.parsedFiles[newFile.FileName] = newFile; return pc; } public IProjectContent UpdateProjectContent(IEnumerable oldFiles, IEnumerable newFiles) { CSharpProjectContent pc = Clone(); if (oldFiles != null) { foreach (var oldFile in oldFiles) { pc.parsedFiles.Remove(oldFile.FileName); } } if (newFiles != null) { foreach (var newFile in newFiles) { pc.parsedFiles.Add(newFile.FileName, newFile); } } return pc; } IAssembly IAssemblyReference.Resolve(ITypeResolveContext context) { if (context == null) throw new ArgumentNullException("context"); var cache = context.Compilation.CacheManager; IAssembly asm = (IAssembly)cache.GetShared(this); if (asm != null) { return asm; } else { asm = new CSharpAssembly(context.Compilation, this); return (IAssembly)cache.GetOrAddShared(this, asm); } } } }