/* static Dictionary<string, int> table = new Dictionary<string, int> (); * public static void PrintIndex () * { * List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>> (table); * list.Sort (delegate (KeyValuePair<string, int> left, KeyValuePair<string, int> right) { * return left.Value.CompareTo (right.Value); * }); * Console.WriteLine ("--------"); * foreach (KeyValuePair<string, int> p in list) { * if (p.Value < 400) * continue; * Console.WriteLine (p.Key + "-" + p.Value); * } * }*/ public static IReturnType GetSharedReturnType(IReturnType returnType) { if (returnType == null) { return(null); } if (returnType.PointerNestingLevel != 0 || returnType.ArrayDimensions != 0 || returnType.GenericArguments.Count != 0) { return(returnType); } string invariantString = returnType.ToInvariantString(); int index; if (tableIndex.TryGetValue(invariantString, out index)) { return(returnTypeTable[index]); } /* * if (!table.ContainsKey (invariantString)) * table[invariantString] = 0; * table[invariantString]++;*/ return(returnType); }
public virtual INode Visit(IReturnType type, string data) { ReturnTypeCount++; stats.ReturnTypes++; foreach (IReturnTypePart p in type.Parts) { Visit(p, data + "Part/"); } foreach (IReturnType rt in SharedTypes) { if (object.ReferenceEquals(rt, type)) { return(null); } } var sysRt = DomReturnType.GetSharedReturnType(type, true); if (sysRt != null) { if (object.ReferenceEquals(sysRt, type)) { return(null); } } Failures.Add(data + type.ToInvariantString()); return(null); }
public override MonoDevelop.Projects.Dom.INode Visit(IReturnType type, object data) { if (type.ToInvariantString() == replaceType.ToInvariantString()) { return(base.Visit(replaceWith, data)); } return(base.Visit(type, data)); }
public void OututReturnType(IReturnType returnType) { if (returnType == null) { output.Append("<null>"); return; } output.Append(returnType.ToInvariantString()); }
public override object Visit(IReturnType returnType, Dictionary <string, IType> data) { IType underlyingType; if (data.TryGetValue(returnType.ToInvariantString(), out underlyingType)) { ((DomReturnType)returnType).Type = underlyingType; } return(base.Visit(returnType, data)); }
public override INode Visit(IReturnType type, object data) { IReturnType rt = (IReturnType)base.Visit(type, data); if (BuiltInTypes.Contains(rt.FullName)) { return(rt); } DomReturnType returnType = new DomReturnType(rt); string longest = ""; string lastAlias = null; if (callingType != null && returnType.DecoratedFullName.StartsWith(callingType.FullName)) { int p1 = System.Math.Min(callingType.FullName.Count(ch => ch == '.') + 1, returnType.Parts.Count - 1); returnType.Parts.RemoveRange(0, p1); returnType.Namespace = ""; return(returnType); } foreach (IUsing u in unit.Usings.Where(u => u.ValidRegion.Contains(location))) { foreach (string ns in u.Namespaces.Where(ns => returnType.Namespace == ns || returnType.Namespace != null && returnType.Namespace.StartsWith(ns + "."))) { if (longest.Length < ns.Length) { longest = ns; } } foreach (KeyValuePair <string, IReturnType> alias in u.Aliases) { if (alias.Value.ToInvariantString() == type.ToInvariantString()) { lastAlias = alias.Key; } } } if (lastAlias != null) { return(new DomReturnType(lastAlias)); } if (longest.Length > 0 && returnType.Namespace == longest) { returnType.Namespace = ""; } return(returnType); }
internal static int GetIndex(IReturnType returnType) { if (returnType.PointerNestingLevel != 0 || returnType.ArrayDimensions != 0 || returnType.GenericArguments.Count != 0) { return(-1); } string invariantString = returnType.ToInvariantString(); int index; if (tableIndex.TryGetValue(invariantString, out index)) { return(index); } return(-1); }
internal IReturnType GetSharedReturnType(IReturnType rt) { string id = rt.ToInvariantString(); IReturnType s; if (!returnTypeCache.TryGetValue(id, out s)) { s = DomReturnType.GetSharedReturnType(rt, true); if (s == null) { s = rt; returnTypeCache [id] = rt; } } return(s); }
internal IReturnType GetSharedReturnType(IReturnType rt) { string id = rt.ToInvariantString(); IReturnType s; if (returnTypeCache.TryGetValue(id, out s)) { return(s); } s = DomReturnType.GetSharedReturnType(rt); if (object.ReferenceEquals(s, rt)) { returnTypeCache [id] = rt; } return(s); }
public bool IsCompatible(IReturnType baseType, IReturnType type) { if (baseType.ToInvariantString() == type.ToInvariantString()) { return(true); } ProjectDom dom = null; if (CallingType == null) { return(false); } dom = CallingType.SourceProjectDom; IType b = dom.SearchType(CallingType.CompilationUnit, CallingType, CallingType.Location, baseType); IType t = dom.SearchType(CallingType.CompilationUnit, CallingType, CallingType.Location, type); if (b == null || t == null) { return(false); } return(dom.GetInheritanceTree(t).Any(tBase => tBase.DecoratedFullName == b.DecoratedFullName)); }
public bool IsCompatible (IReturnType baseType, IReturnType type) { if (baseType.ToInvariantString () == type.ToInvariantString ()) return true; ProjectDom dom = null; if (CallingType == null) return false; dom = CallingType.SourceProjectDom; IType b = dom.SearchType (CallingType.CompilationUnit, CallingType, CallingType.Location, baseType); IType t = dom.SearchType (CallingType.CompilationUnit, CallingType, CallingType.Location, type); if (b == null || t == null) return false; return dom.GetInheritanceTree (t).Any (tBase => tBase.DecoratedFullName == b.DecoratedFullName); }
internal IReturnType GetSharedReturnType (IReturnType rt) { string id = rt.ToInvariantString (); IReturnType s; if (returnTypeCache.TryGetValue (id, out s)) return s; s = DomReturnType.GetSharedReturnType (rt); if (object.ReferenceEquals (s, rt)) returnTypeCache [id] = rt; return s; }
internal ResolveResult ResolveLambda(ResolveVisitor visitor, Expression lambdaExpression) { if (expressions.Contains(lambdaExpression)) { return(null); } expressions.Add(lambdaExpression); if (lambdaExpression.Parent is LambdaExpression) { return(ResolveLambda(visitor, lambdaExpression.Parent as Expression)); } if (lambdaExpression.Parent is ParenthesizedExpression) { return(ResolveLambda(visitor, lambdaExpression.Parent as Expression)); } if (lambdaExpression.Parent is AssignmentExpression) { return(visitor.Resolve(((AssignmentExpression)lambdaExpression.Parent).Left)); } if (lambdaExpression.Parent is CastExpression) { return(visitor.Resolve(((CastExpression)lambdaExpression.Parent))); } if (lambdaExpression.Parent is VariableDeclaration) { VariableDeclaration varDec = (VariableDeclaration)lambdaExpression.Parent; return(resolver.GetFunctionParameterType(resolver.ResolveIdentifier(visitor, varDec.Name))); } if (lambdaExpression.Parent is InvocationExpression) { LambdaExpression lambda = (LambdaExpression)lambdaExpression; ResolveResult lambdaReturnType = null; if (!lambda.ExpressionBody.IsNull) { DomLocation old = resolver.resolvePosition; try { resolver.resolvePosition = new DomLocation((resolver.CallingMember != null ? resolver.CallingMember.Location.Line : 0) + lambda.ExpressionBody.StartLocation.Line - 2, lambda.ExpressionBody.StartLocation.Column - 1); lambdaReturnType = visitor.Resolve(lambda.ExpressionBody); } finally { resolver.resolvePosition = old; } } InvocationExpression invocation = (InvocationExpression)lambdaExpression.Parent; MethodResolveResult result = visitor.Resolve(invocation.TargetObject) as MethodResolveResult; if (result == null) { MonoDevelop.Core.LoggingService.LogWarning("No compatible method found :" + invocation.TargetObject); return(null); } result.ResolveExtensionMethods(); for (int i = 0; i < invocation.Arguments.Count; i++) { if (invocation.Arguments[i] == lambdaExpression && i < result.MostLikelyMethod.Parameters.Count) { IParameter parameter = result.MostLikelyMethod.Parameters[i]; IReturnType returnType = parameter.ReturnType; IType type = resolver.Dom.GetType(returnType); bool isResolved = false; if (type != null && type.ClassType == MonoDevelop.Projects.Dom.ClassType.Delegate) { IMethod invocationMethod = type.Methods.First(); if (invocationMethod.Parameters.Count > 0) { if (lambdaReturnType == null || lambdaReturnType.ResolvedType == null || string.IsNullOrEmpty(lambdaReturnType.ResolvedType.FullName)) { returnType = invocationMethod.Parameters[System.Math.Min(i, invocationMethod.Parameters.Count - 1)].ReturnType; } else { returnType = (IReturnType) new TypeReplaceVisitor(invocationMethod.ReturnType, lambdaReturnType.ResolvedType).Visit(returnType, null); } isResolved = true; } } if (!isResolved) { while (returnType.GenericArguments.Count > 0) { returnType = returnType.GenericArguments[0]; } } string invariantString = returnType.ToInvariantString(); if (returnTypeDictionary.ContainsKey(invariantString)) { return(returnTypeDictionary[invariantString]); } ResolveResult createdResult = visitor.CreateResult(returnType); // if (!(returnType.Type is AnonymousType)) returnTypeDictionary[invariantString] = createdResult; return(createdResult); } } if (lambdaReturnType != null && !string.IsNullOrEmpty(lambdaReturnType.ResolvedType.FullName)) { return(lambdaReturnType); } foreach (Expression arg in invocation.Arguments) { var argType = arg is LambdaExpression ? DomReturnType.Void : visitor.GetTypeSafe(arg); result.AddArgument(argType); } result.ResolveExtensionMethods(); //Console.WriteLine ("maybe method:" + result.MostLikelyMethod); for (int i = 0; i < invocation.Arguments.Count; i++) { if (invocation.Arguments [i] == lambdaExpression && i < result.MostLikelyMethod.Parameters.Count) { IParameter parameterType = result.MostLikelyMethod.Parameters [i]; //Console.WriteLine (i + " par: " + parameterType); if (parameterType.ReturnType.Name == "Func" && parameterType.ReturnType.GenericArguments.Count > 0) { return(visitor.CreateResult(parameterType.ReturnType.GenericArguments[0])); } } } return(result); } if (lambdaExpression.Parent is ObjectCreateExpression) { ObjectCreateExpression objectCreateExpression = (ObjectCreateExpression)lambdaExpression.Parent; int index = objectCreateExpression.Parameters.IndexOf(lambdaExpression); if (index < 0) { return(null); } MemberResolveResult resolvedCreateExpression = visitor.Resolve(objectCreateExpression) as MemberResolveResult; if (resolvedCreateExpression != null) { IMethod method = resolvedCreateExpression.ResolvedMember as IMethod; if (method != null && index < method.Parameters.Count) { return(new ParameterResolveResult(method.Parameters[index])); } else { return(null); } } } return(null); }
public string GetComponentTypeOf(Func <string, string> callback, string varName) { if (callback == null) { return("var"); } string var = callback(varName); ITextEditorResolver textEditorResolver = CurrentContext.Document.GetContent <ITextEditorResolver> (); if (textEditorResolver != null) { ResolveResult result = textEditorResolver.GetLanguageItem(CurrentContext.Document.TextEditor.CursorPosition, var); if (result != null) { IReturnType componentType = DomType.GetComponentType(CurrentContext.ProjectDom, result.ResolvedType); if (componentType != null) { Ambience ambience = AmbienceService.GetAmbience(CurrentContext.Template.MimeType); return(ambience != null?ambience.GetString(componentType, OutputFlags.IncludeGenerics) : componentType.ToInvariantString()); } } } return("var"); }
CompletionDataList CreateTypeCompletionData (DomLocation location, IType callingType, ExpressionContext context, IReturnType returnType, IReturnType returnTypeUnresolved) { CompletionDataList result = new ProjectDomCompletionDataList (); // "var o = new " needs special treatment. if (returnType == null && returnTypeUnresolved != null && returnTypeUnresolved.FullName == "var") returnType = returnTypeUnresolved = DomReturnType.Object; // ExpressionContext.TypeExpressionContext tce = context as ExpressionContext.TypeExpressionContext; CompletionDataCollector col = new CompletionDataCollector (dom, result, Document.CompilationUnit, callingType, location); IType type = null; if (returnType != null) type = dom.GetType (returnType); if (type == null) type = dom.SearchType (Document.CompilationUnit, (MonoDevelop.Projects.Dom.INode)Document.CompilationUnit ?? callingType, returnTypeUnresolved); // special handling for nullable types: Bug 674516 - new completion for nullables should not include "Nullable" if (type is InstantiatedType && ((InstantiatedType)type).UninstantiatedType.FullName == "System.Nullable" && ((InstantiatedType)type).GenericParameters.Count == 1) { var genericParameter = ((InstantiatedType)type).GenericParameters[0]; returnType = returnTypeUnresolved = Document.CompilationUnit.ShortenTypeName (genericParameter, location); type = dom.SearchType (Document.CompilationUnit, (MonoDevelop.Projects.Dom.INode)Document.CompilationUnit ?? callingType, genericParameter); } if (type == null || !(type.IsAbstract || type.ClassType == ClassType.Interface)) { if (type == null || type.ConstructorCount == 0 || type.Methods.Any (c => c.IsConstructor && c.IsAccessibleFrom (dom, callingType, type, callingType != null && dom.GetInheritanceTree (callingType).Any (x => x.FullName == type.FullName)))) { if (returnTypeUnresolved != null) { col.FullyQualify = true; CompletionData unresovedCompletionData = col.Add (returnTypeUnresolved); col.FullyQualify = false; // don't set default completion string for arrays, since it interferes with: // string[] arr = new string[] vs new { "a"} if (returnTypeUnresolved.ArrayDimensions == 0) result.DefaultCompletionString = StripGenerics (unresovedCompletionData.CompletionText); } else { CompletionData unresovedCompletionData = col.Add (returnType); if (returnType.ArrayDimensions == 0) result.DefaultCompletionString = StripGenerics (unresovedCompletionData.CompletionText); } } } // if (tce != null && tce.Type != null) { // result.DefaultCompletionString = StripGenerics (col.AddCompletionData (result, tce.Type).CompletionString); // } // else { // } if (type == null) return result; HashSet<string> usedNamespaces = new HashSet<string> (GetUsedNamespaces ()); if (type.FullName == DomReturnType.Object.FullName) AddPrimitiveTypes (col); foreach (IType curType in dom.GetSubclasses (type)) { if (context != null && context.FilterEntry (curType)) continue; if ((curType.TypeModifier & TypeModifier.HasOnlyHiddenConstructors) == TypeModifier.HasOnlyHiddenConstructors) continue; if (usedNamespaces.Contains (curType.Namespace)) { if (curType.ConstructorCount > 0) { if (!(curType.Methods.Any (c => c.IsConstructor && c.IsAccessibleFrom (dom, curType, callingType, callingType != null && dom.GetInheritanceTree (callingType).Any (x => x.FullName == curType.FullName))))) continue; } col.Add (curType); } else { string nsName = curType.Namespace; int idx = nsName.IndexOf ('.'); if (idx >= 0) nsName = nsName.Substring (0, idx); col.Add (new Namespace (nsName)); } } // add aliases if (returnType != null) { foreach (IUsing u in Document.CompilationUnit.Usings) { foreach (KeyValuePair<string, IReturnType> alias in u.Aliases) { if (alias.Value.ToInvariantString () == returnType.ToInvariantString ()) result.Add (alias.Key, "md-class"); } } } return result; }
internal IReturnType GetSharedReturnType (IReturnType rt) { string id = rt.ToInvariantString (); IReturnType s; if (!returnTypeCache.TryGetValue (id, out s)) { s = DomReturnType.GetSharedReturnType (rt, true); if (s == null) { s = rt; returnTypeCache [id] = rt; } } return s; }
CompletionDataList CreateTypeCompletionData(DomLocation location, IType callingType, ExpressionContext context, IReturnType returnType, IReturnType returnTypeUnresolved) { CompletionDataList result = new ProjectDomCompletionDataList (); // "var o = new " needs special treatment. if (returnType == null && returnTypeUnresolved != null && returnTypeUnresolved.FullName == "var") returnType = returnTypeUnresolved = DomReturnType.Object; // ExpressionContext.TypeExpressionContext tce = context as ExpressionContext.TypeExpressionContext; CompletionDataCollector col = new CompletionDataCollector (result, Document.CompilationUnit, location); IType type = null; if (returnType != null) type = dom.GetType (returnType); if (type == null) type = dom.SearchType (new SearchTypeRequest (Document.CompilationUnit, returnTypeUnresolved, null)); if (type == null || !(type.IsAbstract || type.ClassType == ClassType.Interface)) { if (type == null || type.ConstructorCount == 0 || type.Methods.Any (c => c.IsConstructor && c.IsAccessibleFrom (dom, callingType, type, callingType != null && dom.GetInheritanceTree (callingType).Any (x => x.FullName == type.FullName)))) { if (returnTypeUnresolved != null) { col.FullyQualify = true; ICompletionData unresovedCompletionData = col.Add (returnTypeUnresolved); col.FullyQualify = false; result.DefaultCompletionString = StripGenerics (unresovedCompletionData.CompletionText); } else { ICompletionData unresovedCompletionData = col.Add (returnType); result.DefaultCompletionString = StripGenerics (unresovedCompletionData.CompletionText); } } } // if (tce != null && tce.Type != null) { // result.DefaultCompletionString = StripGenerics (col.AddCompletionData (result, tce.Type).CompletionString); // } // else { // } if (type == null) return result; HashSet<string> usedNamespaces = new HashSet<string> (GetUsedNamespaces ()); if (type.FullName == DomReturnType.Object.FullName) AddPrimitiveTypes (col); foreach (IType curType in dom.GetSubclasses (type)) { if (context != null && context.FilterEntry (curType)) continue; if ((curType.TypeModifier & TypeModifier.HasOnlyHiddenConstructors) == TypeModifier.HasOnlyHiddenConstructors) continue; if (usedNamespaces.Contains (curType.Namespace)) { if (curType.ConstructorCount > 0) { if (!(curType.Methods.Any (c => c.IsConstructor && c.IsAccessibleFrom (dom, curType, callingType, callingType != null && dom.GetInheritanceTree (callingType).Any (x => x.FullName == curType.FullName))))) continue; } col.Add (curType); } else { string nsName = curType.Namespace; int idx = nsName.IndexOf ('.'); if (idx >= 0) nsName = nsName.Substring (0, idx); col.Add (new Namespace (nsName)); } } // add aliases if (returnType != null) { foreach (IUsing u in Document.CompilationUnit.Usings) { foreach (KeyValuePair<string, IReturnType> alias in u.Aliases) { if (alias.Value.ToInvariantString () == returnType.ToInvariantString ()) result.Add (alias.Key, "md-class"); } } } return result; }