Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/CSharpProjectContent.cs @ 15377

Last change on this file since 15377 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 9.0 KB
Line 
1// Copyright (c) 2010-2013 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
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using System.Runtime.Serialization;
23using ICSharpCode.NRefactory.CSharp.TypeSystem;
24using ICSharpCode.NRefactory.TypeSystem;
25using ICSharpCode.NRefactory.TypeSystem.Implementation;
26using ICSharpCode.NRefactory.Utils;
27
28namespace ICSharpCode.NRefactory.CSharp
29{
30  [Serializable]
31  public class CSharpProjectContent : IProjectContent
32  {
33    string assemblyName;
34    string fullAssemblyName;
35    string projectFileName;
36    string location;
37    Dictionary<string, IUnresolvedFile> unresolvedFiles;
38    List<IAssemblyReference> assemblyReferences;
39    CompilerSettings compilerSettings;
40   
41    public CSharpProjectContent()
42    {
43      this.unresolvedFiles = new Dictionary<string, IUnresolvedFile>(Platform.FileNameComparer);
44      this.assemblyReferences = new List<IAssemblyReference>();
45      this.compilerSettings = new CompilerSettings();
46      compilerSettings.Freeze();
47    }
48   
49    protected CSharpProjectContent(CSharpProjectContent pc)
50    {
51      this.assemblyName = pc.assemblyName;
52      this.fullAssemblyName = pc.fullAssemblyName;
53      this.projectFileName = pc.projectFileName;
54      this.location = pc.location;
55      this.unresolvedFiles = new Dictionary<string, IUnresolvedFile>(pc.unresolvedFiles, Platform.FileNameComparer);
56      this.assemblyReferences = new List<IAssemblyReference>(pc.assemblyReferences);
57      this.compilerSettings = pc.compilerSettings;
58    }
59   
60    public IEnumerable<IUnresolvedFile> Files {
61      get { return unresolvedFiles.Values; }
62    }
63   
64    public IEnumerable<IAssemblyReference> AssemblyReferences {
65      get { return assemblyReferences; }
66    }
67   
68    public string ProjectFileName {
69      get { return projectFileName; }
70    }
71   
72    public string AssemblyName {
73      get { return assemblyName; }
74    }
75
76    public string FullAssemblyName {
77      get { return fullAssemblyName; }
78    }
79
80    public string Location {
81      get { return location; }
82    }
83
84    public CompilerSettings CompilerSettings {
85      get { return compilerSettings; }
86    }
87   
88    object IProjectContent.CompilerSettings {
89      get { return compilerSettings; }
90    }
91   
92    public IEnumerable<IUnresolvedAttribute> AssemblyAttributes {
93      get {
94        return this.Files.SelectMany(f => f.AssemblyAttributes);
95      }
96    }
97   
98    public IEnumerable<IUnresolvedAttribute> ModuleAttributes {
99      get {
100        return this.Files.SelectMany(f => f.ModuleAttributes);
101      }
102    }
103   
104    public IEnumerable<IUnresolvedTypeDefinition> TopLevelTypeDefinitions {
105      get {
106        return this.Files.SelectMany(f => f.TopLevelTypeDefinitions);
107      }
108    }
109   
110    public IUnresolvedFile GetFile(string fileName)
111    {
112      IUnresolvedFile file;
113      if (unresolvedFiles.TryGetValue(fileName, out file))
114        return file;
115      else
116        return null;
117    }
118   
119    public virtual ICompilation CreateCompilation()
120    {
121      var solutionSnapshot = new DefaultSolutionSnapshot();
122      ICompilation compilation = new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
123      solutionSnapshot.AddCompilation(this, compilation);
124      return compilation;
125    }
126   
127    public virtual ICompilation CreateCompilation(ISolutionSnapshot solutionSnapshot)
128    {
129      return new SimpleCompilation(solutionSnapshot, this, assemblyReferences);
130    }
131   
132    protected virtual CSharpProjectContent Clone()
133    {
134      return new CSharpProjectContent(this);
135    }
136   
137    /// <summary>
138    /// Sets both the short and the full assembly names.
139    /// </summary>
140    /// <param name="newAssemblyName">New full assembly name.</param>
141    public IProjectContent SetAssemblyName(string newAssemblyName)
142    {
143      CSharpProjectContent pc = Clone();
144      pc.fullAssemblyName = newAssemblyName;
145      int pos = newAssemblyName != null ? newAssemblyName.IndexOf(',') : -1;
146      pc.assemblyName = pos < 0 ? newAssemblyName : newAssemblyName.Substring(0, pos);
147      return pc;
148    }
149   
150    public IProjectContent SetProjectFileName(string newProjectFileName)
151    {
152      CSharpProjectContent pc = Clone();
153      pc.projectFileName = newProjectFileName;
154      return pc;
155    }
156
157    public IProjectContent SetLocation(string newLocation)
158    {
159      CSharpProjectContent pc = Clone();
160      pc.location = newLocation;
161      return pc;
162    }
163   
164    public IProjectContent SetCompilerSettings(object compilerSettings)
165    {
166      if (!(compilerSettings is CompilerSettings))
167        throw new ArgumentException("Settings must be an instance of " + typeof(CompilerSettings).FullName, "compilerSettings");
168      CSharpProjectContent pc = Clone();
169      pc.compilerSettings = (CompilerSettings)compilerSettings;
170      pc.compilerSettings.Freeze();
171      return pc;
172    }
173   
174    public IProjectContent AddAssemblyReferences(IEnumerable<IAssemblyReference> references)
175    {
176      return AddAssemblyReferences(references.ToArray());
177    }
178   
179    public IProjectContent AddAssemblyReferences(params IAssemblyReference[] references)
180    {
181      CSharpProjectContent pc = Clone();
182      pc.assemblyReferences.AddRange(references);
183      return pc;
184    }
185   
186    public IProjectContent RemoveAssemblyReferences(IEnumerable<IAssemblyReference> references)
187    {
188      return RemoveAssemblyReferences(references.ToArray());
189    }
190   
191    public IProjectContent RemoveAssemblyReferences(params IAssemblyReference[] references)
192    {
193      CSharpProjectContent pc = Clone();
194      foreach (var r in references)
195        pc.assemblyReferences.Remove(r);
196      return pc;
197    }
198   
199    /// <summary>
200    /// Adds the specified files to the project content.
201    /// If a file with the same name already exists, updated the existing file.
202    /// </summary>
203    public IProjectContent AddOrUpdateFiles(IEnumerable<IUnresolvedFile> newFiles)
204    {
205      CSharpProjectContent pc = Clone();
206      foreach (var file in newFiles) {
207        pc.unresolvedFiles[file.FileName] = file;
208      }
209      return pc;
210    }
211   
212    /// <summary>
213    /// Adds the specified files to the project content.
214    /// If a file with the same name already exists, this method updates the existing file.
215    /// </summary>
216    public IProjectContent AddOrUpdateFiles(params IUnresolvedFile[] newFiles)
217    {
218      return AddOrUpdateFiles((IEnumerable<IUnresolvedFile>)newFiles);
219    }
220   
221    /// <summary>
222    /// Removes the files with the specified names.
223    /// </summary>
224    public IProjectContent RemoveFiles(IEnumerable<string> fileNames)
225    {
226      CSharpProjectContent pc = Clone();
227      foreach (var fileName in fileNames) {
228        pc.unresolvedFiles.Remove(fileName);
229      }
230      return pc;
231    }
232   
233    /// <summary>
234    /// Removes the files with the specified names.
235    /// </summary>
236    public IProjectContent RemoveFiles(params string[] fileNames)
237    {
238      return RemoveFiles((IEnumerable<string>)fileNames);
239    }
240   
241    [Obsolete("Use RemoveFiles/AddOrUpdateFiles instead")]
242    public IProjectContent UpdateProjectContent(IUnresolvedFile oldFile, IUnresolvedFile newFile)
243    {
244      if (oldFile == null && newFile == null)
245        return this;
246      if (oldFile != null && newFile != null) {
247        if (!Platform.FileNameComparer.Equals(oldFile.FileName, newFile.FileName))
248          throw new ArgumentException("When both oldFile and newFile are specified, they must use the same file name.");
249      }
250      CSharpProjectContent pc = Clone();
251      if (newFile == null)
252        pc.unresolvedFiles.Remove(oldFile.FileName);
253      else
254        pc.unresolvedFiles[newFile.FileName] = newFile;
255      return pc;
256    }
257   
258    [Obsolete("Use RemoveFiles/AddOrUpdateFiles instead")]
259    public IProjectContent UpdateProjectContent(IEnumerable<IUnresolvedFile> oldFiles, IEnumerable<IUnresolvedFile> newFiles)
260    {
261      CSharpProjectContent pc = Clone();
262      if (oldFiles != null) {
263        foreach (var oldFile in oldFiles) {
264          pc.unresolvedFiles.Remove(oldFile.FileName);
265        }
266      }
267      if (newFiles != null) {
268        foreach (var newFile in newFiles) {
269          pc.unresolvedFiles.Add(newFile.FileName, newFile);
270        }
271      }
272      return pc;
273    }
274   
275    IAssembly IAssemblyReference.Resolve(ITypeResolveContext context)
276    {
277      if (context == null)
278        throw new ArgumentNullException("context");
279      var cache = context.Compilation.CacheManager;
280      IAssembly asm = (IAssembly)cache.GetShared(this);
281      if (asm != null) {
282        return asm;
283      } else {
284        asm = new CSharpAssembly(context.Compilation, this);
285        return (IAssembly)cache.GetOrAddShared(this, asm);
286      }
287    }
288  }
289}
Note: See TracBrowser for help on using the repository browser.