IType ITypeReference.Resolve(ITypeResolveContext context)
		{
			// Strictly speaking, we might have to resolve the type in a nested compilation, similar
			// to what we're doing with ConstantExpression.
			// However, in almost all cases this will work correctly - if the resulting type is only available in the
			// nested compilation and not in this, we wouldn't be able to map it anyways.
			var ctx = context as CSharpTypeResolveContext;
			if (ctx == null) {
				ctx = new CSharpTypeResolveContext(context.CurrentAssembly ?? context.Compilation.MainAssembly, null, context.CurrentTypeDefinition, context.CurrentMember);
			}
			return ResolveType(new CSharpResolver(ctx));
			
			// A potential issue might be this scenario:
			
			// Assembly 1:
			//  class A { public class Nested {} }
			
			// Assembly 2: (references asm 1)
			//  class B : A {}
			
			// Assembly 3: (references asm 1 and 2)
			//  class C { public B.Nested Field; }
			
			// Assembly 4: (references asm 1 and 3, but not 2):
			//  uses C.Field;
			
			// Here we would not be able to resolve 'B.Nested' in the compilation of assembly 4, as type B is missing there.
		}
		public CSharpResolver(CSharpTypeResolveContext context)
		{
			if (context == null)
				throw new ArgumentNullException("context");
			this.compilation = context.Compilation;
			this.conversions = CSharpConversions.Get(compilation);
			this.context = context;
			if (context.CurrentTypeDefinition != null)
				currentTypeDefinitionCache = new TypeDefinitionCache(context.CurrentTypeDefinition);
		}
		private CSharpResolver(ICompilation compilation, CSharpConversions conversions, CSharpTypeResolveContext context, bool checkForOverflow, bool isWithinLambdaExpression, TypeDefinitionCache currentTypeDefinitionCache, ImmutableStack<IVariable> localVariableStack, ObjectInitializerContext objectInitializerStack)
		{
			this.compilation = compilation;
			this.conversions = conversions;
			this.context = context;
			this.checkForOverflow = checkForOverflow;
			this.isWithinLambdaExpression = isWithinLambdaExpression;
			this.currentTypeDefinitionCache = currentTypeDefinitionCache;
			this.localVariableStack = localVariableStack;
			this.objectInitializerStack = objectInitializerStack;
		}
		public CSharpParameterCompletionEngine(IDocument document, ICompletionContextProvider completionContextProvider, IParameterCompletionDataFactory factory, IProjectContent content, CSharpTypeResolveContext ctx) : base (content, completionContextProvider, ctx)
		{
			if (document == null) {
				throw new ArgumentNullException("document");
			}
			if (factory == null) {
				throw new ArgumentNullException("factory");
			}
			this.document = document;
			this.factory = factory;
		}
		protected CSharpCompletionEngineBase(IProjectContent content, ICompletionContextProvider completionContextProvider, CSharpTypeResolveContext ctx)
		{
			if (content == null)
				throw new ArgumentNullException("content");
			if (ctx == null)
				throw new ArgumentNullException("ctx");
			if (completionContextProvider == null)
				throw new ArgumentNullException("completionContextProvider");
			
			this.ProjectContent = content;
			this.CompletionContextProvider = completionContextProvider;
			this.ctx = ctx;
		}
		public CSharpResolver(ICompilation compilation)
		{
			if (compilation == null)
				throw new ArgumentNullException("compilation");
			this.compilation = compilation;
			this.conversions = CSharpConversions.Get(compilation);
			this.context = new CSharpTypeResolveContext(compilation.MainAssembly);
			
			var pc = compilation.MainAssembly.UnresolvedAssembly as CSharpProjectContent;
			if (pc != null) {
				this.checkForOverflow = pc.CompilerSettings.CheckForOverflow;
			}
		}
		public CSharpCompletionEngine(IDocument document, ICompletionContextProvider completionContextProvider, ICompletionDataFactory factory, IProjectContent content, CSharpTypeResolveContext ctx) : base (content, completionContextProvider, ctx)
		{
			if (document == null) {
				throw new ArgumentNullException("document");
			}
			if (factory == null) {
				throw new ArgumentNullException("factory");
			}
			this.document = document;
			this.factory = factory;
			// Set defaults for additional input properties
			this.FormattingPolicy = FormattingOptionsFactory.CreateMono();
			this.EolMarker = Environment.NewLine;
			this.IndentString = "\t";
		}
		public ResolvedUsingScope(CSharpTypeResolveContext context, UsingScope usingScope)
		{
			if (context == null)
				throw new ArgumentNullException("context");
			if (usingScope == null)
				throw new ArgumentNullException("usingScope");
			this.parentContext = context;
			this.usingScope = usingScope;
			if (usingScope.Parent != null) {
				if (context.CurrentUsingScope == null)
					throw new InvalidOperationException();
			} else {
				if (context.CurrentUsingScope != null)
					throw new InvalidOperationException();
			}
		}
		IList<IAttribute> GetAttributes(ref IList<IAttribute> field, bool assemblyAttributes)
		{
			IList<IAttribute> result = LazyInit.VolatileRead(ref field);
			if (result != null) {
				return result;
			} else {
				result = new List<IAttribute>();
				foreach (var unresolvedFile in projectContent.Files.OfType<CSharpUnresolvedFile>()) {
					var attributes = assemblyAttributes ? unresolvedFile.AssemblyAttributes : unresolvedFile.ModuleAttributes;
					var context = new CSharpTypeResolveContext(this, unresolvedFile.RootUsingScope.Resolve(compilation));
					foreach (var unresolvedAttr in attributes) {
						result.Add(unresolvedAttr.CreateResolvedAttribute(context));
					}
				}
				return LazyInit.GetOrSet(ref field, result);
			}
		}
		public override CodeGeneratorMemberResult CreateMemberImplementation (ITypeDefinition implementingType,
		                                                                      IUnresolvedTypeDefinition part,
		                                                                      IUnresolvedMember member,
		                                                                      bool explicitDeclaration)
		{
			SetIndentTo (part);
			var options = new CodeGenerationOptions () {
				ExplicitDeclaration = explicitDeclaration,
				ImplementingType = implementingType,
				Part = part
			};
			ITypeResolveContext ctx;

			var doc = IdeApp.Workbench.GetDocument (part.Region.FileName);
			if (doc != null) {
				ctx = doc.ParsedDocument.GetTypeResolveContext (doc.Compilation, implementingType.Region.Begin);
			} else {
				ctx = new CSharpTypeResolveContext (implementingType.Compilation.MainAssembly, null, implementingType, null);
			}
			options.Document = doc;

			if (member is IUnresolvedMethod)
				return GenerateCode ((IMethod) ((IUnresolvedMethod)member).CreateResolved (ctx), options);
			if (member is IUnresolvedProperty)
				return GenerateCode ((IProperty) ((IUnresolvedProperty)member).CreateResolved (ctx), options);
			if (member is IUnresolvedField)
				return GenerateCode ((IField) ((IUnresolvedField)member).CreateResolved (ctx), options);
			if (member is IUnresolvedEvent)
				return GenerateCode ((IEvent) ((IUnresolvedEvent)member).CreateResolved (ctx), options);
			throw new NotSupportedException ("member " +  member + " is not supported.");
		}
		/// <summary>
		/// Resolves the namespace represented by this using scope.
		/// </summary>
		public ResolvedUsingScope Resolve(ICompilation compilation)
		{
			CacheManager cache = compilation.CacheManager;
			ResolvedUsingScope resolved = cache.GetShared(this) as ResolvedUsingScope;
			if (resolved == null) {
				var csContext = new CSharpTypeResolveContext(compilation.MainAssembly, parent != null ? parent.Resolve(compilation) : null);
				resolved = (ResolvedUsingScope)cache.GetOrAddShared(this, new ResolvedUsingScope(csContext, this));
			}
			return resolved;
		}
		internal static ReachabilityAnalysis Create(Statement statement, Func<AstNode, CancellationToken, ResolveResult> resolver, CSharpTypeResolveContext typeResolveContext, CancellationToken cancellationToken)
		{
			var cfgBuilder = new ControlFlowGraphBuilder();
			var cfg = cfgBuilder.BuildControlFlowGraph(statement, resolver, typeResolveContext, cancellationToken);
			return Create(cfg, cancellationToken);
		}
		CSharpResolver WithContext(CSharpTypeResolveContext newContext)
		{
			return new CSharpResolver(compilation, conversions, newContext, checkForOverflow, isWithinLambdaExpression, currentTypeDefinitionCache, localVariableStack, objectInitializerStack);
		}