Ejemplo n.º 1
0
        static string SuggestGlobalAlternatives(Compiler compiler, AstIdentifier id, int?typeParamCount, IEnumerable <string> extraSymbols)
        {
            var msg = "There is nothing named " + id.GetParameterizedSymbol(typeParamCount).Quote() + " accessible in this scope. ";

            if (id.Symbol.Length < 2)
            {
                return(msg);
            }

            var similars = FindSimilarSymbols(compiler, id.Symbol);

            if (extraSymbols != null)
            {
                similars = FindSimilarSymbols(extraSymbols, id.Symbol).Concat(similars);
            }

            if (similars.Any())
            {
                msg += SuggestDidYouMisspell(compiler, id.Symbol, similars);
                return(msg + " Could you be missing a package reference?");
            }
            else
            {
                msg += "Are you missing a package reference?";
            }

            return(msg);
        }
Ejemplo n.º 2
0
        public PartialExpression TryResolveUsingNamespace(Namescope namescope, AstIdentifier id, int?typeParamCount)
        {
            var usings = TryGetUsings(namescope, id.Source);

            if (usings == null || usings.Namespaces.Count == 0)
            {
                return(null);
            }

            var pil = new List <PartialExpression>();

            foreach (var ns in usings.Namespaces)
            {
                var pi = TryResolveMember(ns, id, typeParamCount, null);
                if (pi == null)
                {
                    continue;
                }

                if (pi.ExpressionType == PartialExpressionType.Type ||
                    pi.ExpressionType == PartialExpressionType.Block)
                {
                    pil.Add(pi);
                }
            }

            if (pil.Count == 1)
            {
                return(pil[0]);
            }

            if (pil.Count > 1)
            {
                // Check if it is an overloadable generic type
                if (pil[0].ExpressionType == PartialExpressionType.Type)
                {
                    var  parent   = (pil[0] as PartialType).Type.Parent;
                    bool allEqual = true;

                    for (int i = 1; i < pil.Count; i++)
                    {
                        if (!(pil[i] is PartialType) || (pil[i] as PartialType).Type.Parent != parent)
                        {
                            allEqual = false;
                            break;
                        }
                    }

                    if (allEqual)
                    {
                        return(pil[0]);
                    }
                }

                Log.Error(id.Source, ErrorCode.E3109, id.GetParameterizedSymbol(typeParamCount).Quote() + " is ambiguous" + SuggestCandidates(pil));
                return(PartialExpression.Invalid);
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static string GetUnresolvedIdentifierError(this Compiler compiler, AstIdentifier id, int?typeParamCount, IEnumerable <string> extraSymbols = null)
        {
            string msg = "There is no identifier named " + id.GetParameterizedSymbol(typeParamCount).Quote() + " accessible in this scope. ";

            if (compiler.Environment.SkipVerboseErrors)
            {
                return(msg);
            }

            var syms = FindMatchingSymbols(compiler, id.Symbol).ToArray();

            if (syms.Length > 0)
            {
                return(SuggestAlternatives(compiler, msg, syms, true));
            }
            else
            {
                return(SuggestGlobalAlternatives(compiler, id, typeParamCount, extraSymbols));
            }
        }
        public PartialExpression TryResolveTypeMember(DataType dt, AstIdentifier id, int?typeParamCount, AstExpression qualifier, Expression instance)
        {
            var result = NameResolver.TryGetTypeMemberCached(dt, id.Symbol, typeParamCount);

            if (result == null ||
                dt.Methods.Count == 1 && dt.Methods[0].GenericType == dt)
            {
                return(null);
            }

            if (dt.IsFlattenedDefinition)
            {
                return(PartialError(id.Source, ErrorCode.I0000, "Invalid member-access on non-parameterized type"));
            }

            var methods = result as IReadOnlyList <Method>;

            if (methods != null)
            {
                return(new PartialMethodGroup(id.Source, instance, !AllowStaticContext(qualifier, instance), methods));
            }

            var literal = result as Literal;

            if (literal != null)
            {
                if (AllowStaticContext(qualifier, instance))
                {
                    ILVerifier.VerifyConstUsage(id.Source, literal, Function);
                    return(new PartialValue(new Constant(id.Source, literal.ReturnType, literal.Value is AstExpression
                            ? Compiler.CompileConstant((AstExpression)literal.Value, literal.DeclaringType, literal.ReturnType).Value
                            : literal.Value)));
                }

                return(PartialError(id.Source, ErrorCode.E0000, literal.Quote() + " is a constant -- qualify with the type name"));
            }

            var field = result as Field;

            if (field != null)
            {
                if (field.IsStatic)
                {
                    if (instance != null)
                    {
                        return(AllowStaticContext(qualifier, instance)
                            ? new PartialField(id.Source, field, null)
                            : PartialError(id.Source, ErrorCode.E3117, field.Quote() + " is static -- qualify with the type name"));
                    }
                }
                else
                {
                    if (instance == null)
                    {
                        return(qualifier == null && TryResolveDataTypeIdentifier(id) == field.ReturnType
                            ? new PartialType(id.Source, field.ReturnType)
                            : PartialError(id.Source, ErrorCode.E3118, field.Quote() + " is non-static and cannot be accessed from a static context"));
                    }
                }

                return(new PartialField(id.Source, field, instance));
            }

            var ev = result as Event;

            if (ev != null)
            {
                if (ev.IsStatic)
                {
                    if (instance != null)
                    {
                        return(AllowStaticContext(qualifier, instance)
                            ? new PartialEvent(id.Source, ev, null)
                            : PartialError(id.Source, ErrorCode.E3119, ev.Quote() + " is static -- qualify with the type name"));
                    }
                }
                else
                {
                    if (instance == null)
                    {
                        return(qualifier == null && TryResolveDataTypeIdentifier(id) == ev.ReturnType
                            ? new PartialType(id.Source, ev.ReturnType)
                            : PartialError(id.Source, ErrorCode.E3120, ev.Quote() + " is non-static and cannot be accessed from a static context"));
                    }
                }

                return(new PartialEvent(id.Source, ev, instance));
            }

            var prop = result as Property;

            if (prop != null)
            {
                if (prop.IsStatic)
                {
                    if (instance != null)
                    {
                        return(AllowStaticContext(qualifier, instance)
                            ? new PartialProperty(id.Source, prop, null)
                            : PartialError(id.Source, ErrorCode.E3121, prop.Quote() + " is static -- qualify with the type name"));
                    }
                }
                else
                {
                    if (instance == null)
                    {
                        return(qualifier == null && TryResolveDataTypeIdentifier(id) == prop.ReturnType
                            ? new PartialType(id.Source, prop.ReturnType)
                            : PartialError(id.Source, ErrorCode.E3122, prop.Quote() + " is non-static and cannot be accessed from a static context"));
                    }
                }

                return(new PartialProperty(id.Source, prop, instance));
            }

            var genericParam = result as GenericParameterType;

            if (genericParam != null)
            {
                return(qualifier == null ?
                       new PartialType(id.Source, genericParam) :
                       null);
            }

            var innerType  = result as DataType;
            var innerBlock = result as Block;

            if (innerType != null || innerBlock != null)
            {
                return(AllowStaticContext(qualifier, instance)
                    ? (innerType != null
                        ? (PartialExpression) new PartialType(id.Source, innerType)
                        :                     new PartialBlock(id.Source, innerBlock))
                    : PartialError(id.Source, ErrorCode.E0000, "Cannot reference block or type " + result.Quote() + " through an expression -- qualify with the type name"));
            }

            return(result is List <object>
                   ?PartialError(id.Source, ErrorCode.E0000, (dt + "." + id.GetParameterizedSymbol(typeParamCount)).Quote() + " is ambiguous. This can be resolved using an explicit cast to a more specific interface type")
                       : result as PartialExpression ??
                       PartialError(id.Source, ErrorCode.I0000, "Unknown type member: " + result));
        }
Ejemplo n.º 5
0
        public PartialExpression TryResolveUsingType(Namescope namescope, AstIdentifier id, int?typeParamCount)
        {
            var usings = TryGetUsings(namescope, id.Source);

            if (usings == null || usings.Types.Count == 0)
            {
                return(null);
            }

            var methods    = new List <Method>();
            var properties = new List <Property>();
            var fields     = new List <Field>();
            var events     = new List <Event>();
            var literals   = new List <Literal>();

            foreach (var dt in usings.Types)
            {
                dt.PopulateMembers();

                for (int i = 0, l = dt.Methods.Count; i < l; i++)
                {
                    var m = dt.Methods[i];
                    if (m.UnoName == id.Symbol && m.IsPublic && m.IsStatic)
                    {
                        if (typeParamCount != null && (!m.IsGenericDefinition || m.GenericParameters.Length != typeParamCount.Value))
                        {
                            continue;
                        }

                        methods.Add(m);
                    }
                }

                if (typeParamCount == null)
                {
                    for (int i = 0, l = dt.Properties.Count; i < l; i++)
                    {
                        var m = dt.Properties[i];
                        if (m.UnoName == id.Symbol && m.IsPublic && m.IsStatic && m.Parameters.Length == 0)
                        {
                            properties.Add(m);
                        }
                    }

                    for (int i = 0, l = dt.Fields.Count; i < l; i++)
                    {
                        var m = dt.Fields[i];
                        if (m.UnoName == id.Symbol && m.IsPublic && m.IsStatic)
                        {
                            fields.Add(m);
                        }
                    }

                    for (int i = 0, l = dt.Events.Count; i < l; i++)
                    {
                        var m = dt.Events[i];
                        if (m.UnoName == id.Symbol && m.IsPublic && m.IsStatic)
                        {
                            events.Add(m);
                        }
                    }

                    for (int i = 0, l = dt.Literals.Count; i < l; i++)
                    {
                        var m = dt.Literals[i];
                        if (m.UnoName == id.Symbol && m.IsPublic)
                        {
                            literals.Add(m);
                        }
                    }
                }
            }

            if (methods.Count == 0 && properties.Count == 0 && fields.Count == 0 && literals.Count == 0)
            {
                return(null);
            }
            if (methods.Count > 0 && properties.Count == 0 && fields.Count == 0 && literals.Count == 0)
            {
                return(new PartialMethodGroup(id.Source, null, false, methods));
            }
            if (properties.Count == 1 && methods.Count == 0 && fields.Count == 0 && literals.Count == 0)
            {
                return(new PartialProperty(id.Source, properties[0], null));
            }
            if (fields.Count == 1 && methods.Count == 0 && properties.Count == 0 && literals.Count == 0)
            {
                return(new PartialField(id.Source, fields[0], null));
            }
            if (events.Count == 1 && methods.Count == 0 && properties.Count == 0 && literals.Count == 0)
            {
                return(new PartialEvent(id.Source, events[0], null));
            }
            if (literals.Count == 1 && fields.Count == 0 && methods.Count == 0 && properties.Count == 0)
            {
                return(new PartialValue(id.Source, new Constant(literals[0].Source, literals[0].ReturnType, literals[0].Value)));
            }

            var all = new List <object>();

            all.AddRange(methods);
            all.AddRange(properties);
            all.AddRange(fields);
            all.AddRange(events);
            all.AddRange(literals);

            return(PartialError(id.Source, ErrorCode.E3110, id.GetParameterizedSymbol(typeParamCount).Quote() + " is ambiguous" + SuggestCandidates(all)));
        }