⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resolver.cs

📁 全功能c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Andrea Paatz" email="andrea@icsharpcode.net"/>
//     <version value="$version"/>
// </file>

using System;
using System.Collections;
using System.Drawing;

using ICSharpCode.SharpDevelop.Services;
using SharpDevelop.Internal.Parser;
using VBBinding.Parser.SharpDevelopTree;
using ICSharpCode.SharpRefactory.Parser.AST.VB;
using ICSharpCode.SharpRefactory.Parser.VB;

namespace VBBinding.Parser
{
	public class Resolver
	{
		IParserService parserService;
		ICompilationUnit cu;
		IClass callingClass;
		LookupTableVisitor lookupTableVisitor;
		
		public IParserService ParserService {
			get {
				return parserService;
			}
		}
		
		public ICompilationUnit CompilationUnit {
			get {
				return cu;
			}
		}
		
		public IClass CallingClass {
			get {
				return callingClass;
			}
		}
		
		bool showStatic = false;
		
		bool inNew = false;
		
		public bool ShowStatic {
			get {
				return showStatic;
			}
			
			set {
				showStatic = value;
			}
		}
		
		int caretLine;
		int caretColumn;
		
		public ResolveResult Resolve(IParserService parserService, string expression, int caretLineNumber, int caretColumn, string fileName, string fileContent)
		{
			expression = expression.TrimStart(null);
			expression = expression.ToLower();
			if (expression.StartsWith("new ")) {
				inNew = true;
				expression = expression.Substring(4);
			} else {
				inNew = false;
			}
			//Console.WriteLine("\nStart Resolving expression : >{0}<", expression);
			
			Expression expr = null;
			this.caretLine     = caretLineNumber;
			this.caretColumn   = caretColumn;
			this.parserService = parserService;
			IParseInformation parseInfo = parserService.GetParseInformation(fileName);
			ICSharpCode.SharpRefactory.Parser.AST.VB.CompilationUnit fileCompilationUnit = parseInfo.MostRecentCompilationUnit.Tag as ICSharpCode.SharpRefactory.Parser.AST.VB.CompilationUnit;
			if (fileCompilationUnit == null) {
//				ICSharpCode.SharpRefactory.Parser.VB.Parser fileParser = new ICSharpCode.SharpRefactory.Parser.VB.Parser();
//				fileParser.Parse(new Lexer(new StringReader(fileContent)));
				//Console.WriteLine("!Warning: no parseinformation!");
				return null;
			}
			VBNetVisitor vBNetVisitor = new VBNetVisitor();
			cu = (ICompilationUnit)vBNetVisitor.Visit(fileCompilationUnit, null);
			if (cu != null) {
				callingClass = parserService.GetInnermostClass(cu, caretLine, caretColumn);
				//Console.WriteLine("CallingClass is " + callingClass == null ? "null" : callingClass.Name);
			}
			lookupTableVisitor = new LookupTableVisitor();
			lookupTableVisitor.Visit(fileCompilationUnit, null);
			
			if (expression == null || expression == "") {
				expr = WithResolve();
				if (expr == null) {
					return null;
				}
			}
			
			if (expression.StartsWith("imports ")) {
				return ImportsResolve(expression);
			}
			//Console.WriteLine("Not in imports >{0}<", expression);
			
			if (InMain()) {
				showStatic = true;
			}
			
			// MyBase and MyClass are no expressions, only MyBase.Identifier and MyClass.Identifier
			if (expression == "mybase") {
				expr = new BaseReferenceExpression();
			} else if (expression == "myclass") {
				expr = new ClassReferenceExpression();
			}
			
			if (expr == null) {
				Lexer l = new Lexer(new StringReader(expression));
				ICSharpCode.SharpRefactory.Parser.VB.Parser p = new ICSharpCode.SharpRefactory.Parser.VB.Parser();
				expr = p.ParseExpression(l);
//				if (expr == null) {
//					//Console.WriteLine("Warning: No Expression from parsing!");
//					return null;
//				}
			}
			
			//Console.WriteLine(expr.ToString());
			TypeVisitor typeVisitor = new TypeVisitor(this);
			IReturnType type = expr.AcceptVisitor(typeVisitor, null) as IReturnType;
			//Console.WriteLine("type visited");
			if (type == null || type.PointerNestingLevel != 0) {
//				Console.WriteLine("Type == null || type.PointerNestingLevel != 0");
				if (type != null) {
					//Console.WriteLine("PointerNestingLevel is " + type.PointerNestingLevel);
				} else {
					//Console.WriteLine("Type == null");
				}
				return null;
			}
			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0) {
				type = new ReturnType("System.Array");
			}
		//	Console.WriteLine("Here: Type is " + type.FullyQualifiedName);
			IClass returnClass = SearchType(type.FullyQualifiedName, callingClass, cu);
			if (returnClass == null) {
//				Console.WriteLine("IClass is null! Trying namespace!");
				// Try if type is Namespace:
				string n = SearchNamespace(type.FullyQualifiedName, cu);
				if (n == null) {
					return null;
				}
				ArrayList content = parserService.GetNamespaceContents(n, false);
				ArrayList classes = new ArrayList();
				for (int i = 0; i < content.Count; ++i) {
					if (content[i] is IClass) {
						if (inNew) {
							IClass c = (IClass)content[i];
//							Console.WriteLine("Testing " + c.Name);
							if ((c.ClassType == ClassType.Class) || (c.ClassType == ClassType.Struct)) {
								classes.Add(c);
//								Console.WriteLine("Added");
							}
						} else {
							classes.Add((IClass)content[i]);
						}
					}
				}
				string[] namespaces = parserService.GetNamespaceList(n, false);
				return new ResolveResult(namespaces, classes);
			}
		//	Console.WriteLine("Returning Result!");
			if (inNew) {
				return new ResolveResult(returnClass, parserService.ListTypes(new ArrayList(), returnClass, callingClass));
			} else {
				return new ResolveResult(returnClass, parserService.ListMembers(new ArrayList(), returnClass, callingClass, showStatic));
			}
		}
		
		bool InMain()
		{
			return false;
		}
		
		Expression WithResolve()
		{
			//Console.WriteLine("in WithResolve");
			Expression expr = null;
			if (lookupTableVisitor.WithStatements != null) {
				//Console.WriteLine("{0} WithStatements", lookupTableVisitor.WithStatements.Count);
				foreach (WithStatement with in lookupTableVisitor.WithStatements) {
//					Console.WriteLine("Position: ({0}/{1})", with.StartLocation, with.EndLocation);
					if (IsInside(new Point(caretColumn, caretLine), with.StartLocation, with.EndLocation)) {
						expr = with.WithExpression;
					}
				}
			}
//			if (expr == null) {
//				Console.WriteLine("No WithStatement found");
//			} else {
//				Console.WriteLine("WithExpression : " + expr);
//			}
			return expr;
		}
		
		ResolveResult ImportsResolve(string expression)
		{
			// expression[expression.Length - 1] != '.'
			// the period that causes this Resove() is not part of the expression
			if (expression[expression.Length - 1] == '.') {
				return null;
			}
			int i;
			for (i = expression.Length - 1; i >= 0; --i) {
				if (!(Char.IsLetterOrDigit(expression[i]) || expression[i] == '_' || expression[i] == '.')) {
					break;
				}
			}
			// no Identifier before the period
			if (i == expression.Length - 1) {
				return null;
			}
			string t = expression.Substring(i + 1);
//			Console.WriteLine("in imports Statement");
			string[] namespaces = parserService.GetNamespaceList(t, false);
			if (namespaces == null || namespaces.Length <= 0) {
				return null;
			}
			return new ResolveResult(namespaces);
		}
		
		bool InStatic()
		{
			IProperty property = GetProperty();
			if (property != null) {
				return property.IsStatic;
			}
			IMethod method = GetMethod();
			if (method != null) {
				return method.IsStatic;
			}
			return false;
		}
		
		public ArrayList SearchMethod(IReturnType type, string memberName)
		{
			if (type == null || type.PointerNestingLevel != 0) {
				return new ArrayList();
			}
			IClass curType;
			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0) {
				curType = SearchType("System.Array", null, null);
			} else {
				curType = SearchType(type.FullyQualifiedName, null, null);
				if (curType == null) {
					return new ArrayList();
				}
			}
			return SearchMethod(new ArrayList(), curType, memberName);
		}
		
		ArrayList SearchMethod(ArrayList methods, IClass curType, string memberName)
		{
			bool isClassInInheritanceTree = parserService.IsClassInInheritanceTree(curType, callingClass, false);
			foreach (IMethod m in curType.Methods) {
				if (m.Name.ToLower() == memberName.ToLower() &&
				    parserService.MustBeShown(curType, m, callingClass, showStatic, isClassInInheritanceTree) &&
				    !((m.Modifiers & ModifierEnum.Override) == ModifierEnum.Override)) {
					methods.Add(m);
				}
			}
			IClass baseClass = parserService.BaseClass(curType, false);
			if (baseClass != null) {
				return SearchMethod(methods, baseClass, memberName);
			}
			showStatic = false;
			return methods;
		}
		
		public ArrayList SearchIndexer(IReturnType type)
		{
			IClass curType = SearchType(type.FullyQualifiedName, null, null);
			if (curType != null) {
				return SearchIndexer(new ArrayList(), curType);
			}
			return new ArrayList();
		}
		
		public ArrayList SearchIndexer(ArrayList indexer, IClass curType)
		{
			bool isClassInInheritanceTree = parserService.IsClassInInheritanceTree(curType, callingClass, false);
			foreach (IIndexer i in curType.Indexer) {
				if (parserService.MustBeShown(curType, i, callingClass, showStatic, isClassInInheritanceTree) && !((i.Modifiers & ModifierEnum.Override) == ModifierEnum.Override)) {
					indexer.Add(i);
				}
			}
			IClass baseClass = parserService.BaseClass(curType, false);
			if (baseClass != null) {
				return SearchIndexer(indexer, baseClass);
			}
			showStatic = false;
			return indexer;
		}
		
		// no methods or indexer
		public IReturnType SearchMember(IReturnType type, string memberName)
		{
			if (type == null || memberName == null || memberName == "") {
				return null;
			}
//			Console.WriteLine("searching member {0} in {1}", memberName, type.Name);
			IClass curType = SearchType(type.FullyQualifiedName, callingClass, cu);
			bool isClassInInheritanceTree = parserService.IsClassInInheritanceTree(curType, callingClass, false);
			
			if (curType == null) {
//				Console.WriteLine("Type not found in SearchMember");
				return null;
			}
			if (type.PointerNestingLevel != 0) {
				return null;
			}
			if (type.ArrayDimensions != null && type.ArrayDimensions.Length > 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -