예제 #1
0
        OverloadResolutionErrors IsUnambiguousCall(ExpectedTargetDetails expectedTargetDetails, IMethod method,
                                                   ResolveResult target, IType[] typeArguments, IList <TranslatedExpression> arguments,
                                                   string[] argumentNames,
                                                   out IParameterizedMember foundMember)
        {
            foundMember = null;
            var lookup = new MemberLookup(resolver.CurrentTypeDefinition, resolver.CurrentTypeDefinition.ParentAssembly);
            var or     = new OverloadResolution(resolver.Compilation,
                                                arguments.SelectArray(a => a.ResolveResult),
                                                argumentNames: argumentNames,
                                                typeArguments: typeArguments,
                                                conversions: expressionBuilder.resolver.conversions);

            if (expectedTargetDetails.CallOpCode == OpCode.NewObj)
            {
                foreach (IMethod ctor in method.DeclaringType.GetConstructors())
                {
                    if (lookup.IsAccessible(ctor, allowProtectedAccess: resolver.CurrentTypeDefinition == method.DeclaringTypeDefinition))
                    {
                        or.AddCandidate(ctor);
                    }
                }
            }
            else if (target == null)
            {
                var result = resolver.ResolveSimpleName(method.Name, typeArguments, isInvocationTarget: true) as MethodGroupResolveResult;
                if (result == null)
                {
                    return(OverloadResolutionErrors.AmbiguousMatch);
                }
                or.AddMethodLists(result.MethodsGroupedByDeclaringType.ToArray());
            }
            else
            {
                var result = lookup.Lookup(target, method.Name, EmptyList <IType> .Instance, isInvocation: true) as MethodGroupResolveResult;
                if (result == null)
                {
                    return(OverloadResolutionErrors.AmbiguousMatch);
                }
                or.AddMethodLists(result.MethodsGroupedByDeclaringType.ToArray());
            }
            if (or.BestCandidateErrors != OverloadResolutionErrors.None)
            {
                return(or.BestCandidateErrors);
            }
            if (or.IsAmbiguous)
            {
                return(OverloadResolutionErrors.AmbiguousMatch);
            }
            foundMember = or.GetBestCandidateWithSubstitutedTypeArguments();
            if (!IsAppropriateCallTarget(expectedTargetDetails, method, foundMember))
            {
                return(OverloadResolutionErrors.AmbiguousMatch);
            }
            return(OverloadResolutionErrors.None);
        }
예제 #2
0
        static bool IsNameUsed(CSharpResolver resolver, QueryExpression query, string name)
        {
            if (resolver.ResolveSimpleName(name, new List <IType>()) is LocalResolveResult)
            {
                return(true);
            }

            if (query.Ancestors.OfType <VariableInitializer>().Any(variable => variable.Name == name))
            {
                return(true);
            }

            if (query.Ancestors.OfType <BlockStatement>()
                .Any(blockStatement => DeclaresLocalVariable(blockStatement, name)))
            {
                return(true);
            }

            return(query.Descendants.OfType <Identifier> ().Any(identifier => identifier.Name == name));
        }
        AstType ConvertTypeHelper(ITypeDefinition typeDef, IList <IType> typeArguments)
        {
            Debug.Assert(typeArguments.Count >= typeDef.TypeParameterCount);

            string keyword = KnownTypeReference.GetCSharpNameByTypeCode(typeDef.KnownTypeCode);

            if (keyword != null)
            {
                return(new PrimitiveType(keyword));
            }

            // The number of type parameters belonging to outer classes
            int outerTypeParameterCount;

            if (typeDef.DeclaringType != null)
            {
                outerTypeParameterCount = typeDef.DeclaringType.TypeParameterCount;
            }
            else
            {
                outerTypeParameterCount = 0;
            }

            if (resolver != null)
            {
                // Look if there's an alias to the target type
                if (UseAliases)
                {
                    for (ResolvedUsingScope usingScope = resolver.CurrentUsingScope; usingScope != null; usingScope = usingScope.Parent)
                    {
                        foreach (var pair in usingScope.UsingAliases)
                        {
                            if (pair.Value is TypeResolveResult)
                            {
                                if (TypeMatches(pair.Value.Type, typeDef, typeArguments))
                                {
                                    return(new SimpleType(pair.Key));
                                }
                            }
                        }
                    }
                }

                IList <IType> localTypeArguments;
                if (typeDef.TypeParameterCount > outerTypeParameterCount)
                {
                    localTypeArguments = new IType[typeDef.TypeParameterCount - outerTypeParameterCount];
                    for (int i = 0; i < localTypeArguments.Count; i++)
                    {
                        localTypeArguments[i] = typeArguments[outerTypeParameterCount + i];
                    }
                }
                else
                {
                    localTypeArguments = EmptyList <IType> .Instance;
                }
                ResolveResult     rr  = resolver.ResolveSimpleName(typeDef.Name, localTypeArguments);
                TypeResolveResult trr = rr as TypeResolveResult;
                if (trr != null || (localTypeArguments.Count == 0 && resolver.IsVariableReferenceWithSameType(rr, typeDef.Name, out trr)))
                {
                    if (!trr.IsError && TypeMatches(trr.Type, typeDef, typeArguments))
                    {
                        // We can use the short type name
                        SimpleType shortResult = new SimpleType(typeDef.Name);
                        AddTypeArguments(shortResult, typeDef, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
                        return(shortResult);
                    }
                }
            }

            if (AlwaysUseShortTypeNames)
            {
                var shortResult = new SimpleType(typeDef.Name);
                AddTypeArguments(shortResult, typeDef, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
                return(shortResult);
            }
            MemberType result = new MemberType();

            if (typeDef.DeclaringTypeDefinition != null)
            {
                // Handle nested types
                result.Target = ConvertTypeHelper(typeDef.DeclaringTypeDefinition, typeArguments);
            }
            else
            {
                // Handle top-level types
                if (string.IsNullOrEmpty(typeDef.Namespace))
                {
                    result.Target        = new SimpleType("global");
                    result.IsDoubleColon = true;
                }
                else
                {
                    result.Target = ConvertNamespace(typeDef.Namespace);
                }
            }
            result.MemberName = typeDef.Name;
            AddTypeArguments(result, typeDef, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
            return(result);
        }
예제 #4
0
		public override ResolveResult Resolve(CSharpResolver resolver)
		{
			return resolver.ResolveSimpleName(identifier, ResolveTypes(resolver, typeArguments));
		}
예제 #5
0
 public override ResolveResult Resolve(CSharpResolver resolver)
 {
     return(resolver.ResolveSimpleName(identifier, typeArguments.Resolve(resolver.CurrentTypeResolveContext)));
 }
예제 #6
0
        AstType ConvertTypeHelper(ITypeDefinition typeDef, IList <IType> typeArguments)
        {
            Debug.Assert(typeArguments.Count >= typeDef.TypeParameterCount);
            TypeCode typeCode = ReflectionHelper.GetTypeCode(typeDef);

            if (typeCode != TypeCode.Empty)
            {
                string keyword = ReflectionHelper.GetCSharpNameByTypeCode(typeCode);
                if (keyword != null)
                {
                    return(new PrimitiveType(keyword));
                }
            }
            // There is no type code for System.Void
            if (typeDef.Kind == TypeKind.Void)
            {
                return(new PrimitiveType("void"));
            }

            // The number of type parameters belonging to outer classes
            int outerTypeParameterCount;

            if (typeDef.DeclaringType != null)
            {
                outerTypeParameterCount = typeDef.DeclaringType.TypeParameterCount;
            }
            else
            {
                outerTypeParameterCount = 0;
            }

            if (resolver != null)
            {
                // Look if there's an alias to the target type
                for (UsingScope usingScope = resolver.CurrentUsingScope; usingScope != null; usingScope = usingScope.Parent)
                {
                    foreach (var pair in usingScope.UsingAliases)
                    {
                        IType type = pair.Value.Resolve(resolver.Context);
                        if (TypeMatches(type, typeDef, typeArguments))
                        {
                            return(new SimpleType(pair.Key));
                        }
                    }
                }

                IList <IType> localTypeArguments;
                if (typeDef.TypeParameterCount > outerTypeParameterCount)
                {
                    localTypeArguments = new IType[typeDef.TypeParameterCount - outerTypeParameterCount];
                    for (int i = 0; i < localTypeArguments.Count; i++)
                    {
                        localTypeArguments[i] = typeArguments[outerTypeParameterCount + i];
                    }
                }
                else
                {
                    localTypeArguments = EmptyList <IType> .Instance;
                }
                TypeResolveResult trr = resolver.ResolveSimpleName(typeDef.Name, localTypeArguments) as TypeResolveResult;
                if (trr != null && !trr.IsError && TypeMatches(trr.Type, typeDef, typeArguments))
                {
                    // We can use the short type name
                    SimpleType shortResult = new SimpleType(typeDef.Name);
                    AddTypeArguments(shortResult, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
                    return(shortResult);
                }
            }

            MemberType result = new MemberType();

            if (typeDef.DeclaringTypeDefinition != null)
            {
                // Handle nested types
                result.Target = ConvertTypeHelper(typeDef.DeclaringTypeDefinition, typeArguments);
            }
            else
            {
                // Handle top-level types
                if (string.IsNullOrEmpty(typeDef.Namespace))
                {
                    result.Target        = new SimpleType("global");
                    result.IsDoubleColon = true;
                }
                else
                {
                    result.Target = ConvertNamespace(typeDef.Namespace);
                }
            }
            result.MemberName = typeDef.Name;
            AddTypeArguments(result, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
            return(result);
        }
예제 #7
0
 public override ResolveResult Resolve(CSharpResolver resolver)
 {
     return(resolver.ResolveSimpleName(identifier, ResolveTypes(resolver, typeArguments)));
 }
예제 #8
0
        AstType ConvertTypeDefinition(ITypeDefinition typeDef, IList <IType> typeArguments)
        {
            Debug.Assert(typeArguments.Count >= typeDef.TypeParameterCount);
            switch (ReflectionHelper.GetTypeCode(typeDef))
            {
            case TypeCode.Object:
                return(new PrimitiveType("object"));

            case TypeCode.Boolean:
                return(new PrimitiveType("bool"));

            case TypeCode.Char:
                return(new PrimitiveType("char"));

            case TypeCode.SByte:
                return(new PrimitiveType("sbyte"));

            case TypeCode.Byte:
                return(new PrimitiveType("byte"));

            case TypeCode.Int16:
                return(new PrimitiveType("short"));

            case TypeCode.UInt16:
                return(new PrimitiveType("ushort"));

            case TypeCode.Int32:
                return(new PrimitiveType("int"));

            case TypeCode.UInt32:
                return(new PrimitiveType("uint"));

            case TypeCode.Int64:
                return(new PrimitiveType("long"));

            case TypeCode.UInt64:
                return(new PrimitiveType("ulong"));

            case TypeCode.Single:
                return(new PrimitiveType("float"));

            case TypeCode.Double:
                return(new PrimitiveType("double"));

            case TypeCode.Decimal:
                return(new PrimitiveType("decimal"));

            case TypeCode.String:
                return(new PrimitiveType("string"));
            }
            // There is no type code for System.Void
            if (typeDef != null && typeDef.Namespace == "System" && typeDef.Name == "Void" && typeDef.TypeParameterCount == 0)
            {
                return(new PrimitiveType("void"));
            }
            if (resolver != null && TypeArgumentsTrivial(typeArguments, OuterTypeParameterCount(typeDef)))
            {
                TypeResolveResult trr = resolver.ResolveSimpleName(typeDef.Name, typeArguments) as TypeResolveResult;
                if (trr != null && !trr.IsError && trr.Type.GetDefinition() == typeDef)
                {
                    // We can use the short type name
                    SimpleType shortResult = new SimpleType(typeDef.Name);
                    AddTypeArguments(shortResult, typeArguments, OuterTypeParameterCount(typeDef), typeDef.TypeParameterCount);
                    return(shortResult);
                }
            }

            MemberType result = new MemberType();

            if (typeDef.DeclaringTypeDefinition != null)
            {
                // Handle nested types
                result.Target = ConvertTypeDefinition(typeDef.DeclaringTypeDefinition, typeArguments);
            }
            else
            {
                // Handle top-level types
                if (string.IsNullOrEmpty(typeDef.Namespace))
                {
                    result.Target        = new SimpleType("global");
                    result.IsDoubleColon = true;
                }
                else
                {
                    result.Target = ConvertNamespace(typeDef.Namespace);
                }
            }
            result.MemberName = typeDef.Name;
            AddTypeArguments(result, typeArguments, OuterTypeParameterCount(typeDef), typeDef.TypeParameterCount);
            return(result);
        }
예제 #9
0
        AstType ConvertTypeDefinition(ITypeDefinition typeDef, IList <IType> typeArguments)
        {
            Debug.Assert(typeArguments.Count >= typeDef.TypeParameterCount);
            switch (ReflectionHelper.GetTypeCode(typeDef))
            {
            case TypeCode.Object:
                return(new PrimitiveType("object"));

            case TypeCode.Boolean:
                return(new PrimitiveType("bool"));

            case TypeCode.Char:
                return(new PrimitiveType("char"));

            case TypeCode.SByte:
                return(new PrimitiveType("sbyte"));

            case TypeCode.Byte:
                return(new PrimitiveType("byte"));

            case TypeCode.Int16:
                return(new PrimitiveType("short"));

            case TypeCode.UInt16:
                return(new PrimitiveType("ushort"));

            case TypeCode.Int32:
                return(new PrimitiveType("int"));

            case TypeCode.UInt32:
                return(new PrimitiveType("uint"));

            case TypeCode.Int64:
                return(new PrimitiveType("long"));

            case TypeCode.UInt64:
                return(new PrimitiveType("ulong"));

            case TypeCode.Single:
                return(new PrimitiveType("float"));

            case TypeCode.Double:
                return(new PrimitiveType("double"));

            case TypeCode.Decimal:
                return(new PrimitiveType("decimal"));

            case TypeCode.String:
                return(new PrimitiveType("string"));
            }
            // There is no type code for System.Void
            if (typeDef.Kind == TypeKind.Void)
            {
                return(new PrimitiveType("void"));
            }

            // The number of type parameters belonging to outer classes
            int outerTypeParameterCount;

            if (typeDef.DeclaringType != null)
            {
                outerTypeParameterCount = typeDef.DeclaringType.TypeParameterCount;
            }
            else
            {
                outerTypeParameterCount = 0;
            }

            if (resolver != null)
            {
                // Look if there's an alias to the target type
                for (UsingScope usingScope = resolver.UsingScope; usingScope != null; usingScope = usingScope.Parent)
                {
                    foreach (var pair in usingScope.UsingAliases)
                    {
                        IType type = pair.Value.Resolve(resolver.Context);
                        if (TypeMatches(type, typeDef, typeArguments))
                        {
                            return(new SimpleType(pair.Key));
                        }
                    }
                }

                IList <IType> localTypeArguments;
                if (typeDef.TypeParameterCount > outerTypeParameterCount)
                {
                    localTypeArguments = new IType[typeDef.TypeParameterCount - outerTypeParameterCount];
                    for (int i = 0; i < localTypeArguments.Count; i++)
                    {
                        localTypeArguments[i] = typeArguments[outerTypeParameterCount + i];
                    }
                }
                else
                {
                    localTypeArguments = EmptyList <IType> .Instance;
                }
                TypeResolveResult trr = resolver.ResolveSimpleName(typeDef.Name, localTypeArguments) as TypeResolveResult;
                if (trr != null && !trr.IsError && TypeMatches(trr.Type, typeDef, typeArguments))
                {
                    // We can use the short type name
                    SimpleType shortResult = new SimpleType(typeDef.Name);
                    AddTypeArguments(shortResult, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
                    return(shortResult);
                }
            }

            MemberType result = new MemberType();

            if (typeDef.DeclaringTypeDefinition != null)
            {
                // Handle nested types
                result.Target = ConvertTypeDefinition(typeDef.DeclaringTypeDefinition, typeArguments);
            }
            else
            {
                // Handle top-level types
                if (string.IsNullOrEmpty(typeDef.Namespace))
                {
                    result.Target        = new SimpleType("global");
                    result.IsDoubleColon = true;
                }
                else
                {
                    result.Target = ConvertNamespace(typeDef.Namespace);
                }
            }
            result.MemberName = typeDef.Name;
            AddTypeArguments(result, typeArguments, outerTypeParameterCount, typeDef.TypeParameterCount);
            return(result);
        }