Esempio n. 1
0
        public AstType ConvertNamespace(string namespaceName)
        {
            if (resolver != null) {
                // Look if there's an alias to the target namespace
                if (UseAliases) {
                    for (ResolvedUsingScope usingScope = resolver.CurrentUsingScope; usingScope != null; usingScope = usingScope.Parent) {
                        foreach (var pair in usingScope.UsingAliases) {
                            NamespaceResolveResult nrr = pair.Value as NamespaceResolveResult;
                            if (nrr != null && nrr.NamespaceName == namespaceName)
                                return new SimpleType(pair.Key);
                        }
                    }
                }
            }

            int pos = namespaceName.LastIndexOf('.');
            if (pos < 0) {
                if (IsValidNamespace(namespaceName)) {
                    return new SimpleType(namespaceName);
                } else {
                    return new MemberType {
                        Target = new SimpleType("global"),
                        IsDoubleColon = true,
                        MemberName = namespaceName
                    };
                }
            } else {
                string parentNamespace = namespaceName.Substring(0, pos);
                string localNamespace = namespaceName.Substring(pos + 1);
                return new MemberType {
                    Target = ConvertNamespace(parentNamespace),
                    MemberName = localNamespace
                };
            }
        }
Esempio n. 2
0
        Expression LookInUsingScopeNamespace(ResolveContext rc, ResolvedUsingScope usingScope, INamespace n, string identifier, IList <IType> typeArguments, bool parameterizeResultType)
        {
            if (n == null)
            {
                return(null);
            }
            // first look for a namespace
            int k = typeArguments.Count;

            if (k == 0)
            {
                INamespace childNamespace = n.GetChildNamespace(identifier);
                if (childNamespace != null)
                {
                    if (usingScope != null && usingScope.HasAlias(identifier))
                    {
                        rc.Report.Error(7, loc, "The name `{0}' is ambigious", name);
                        return(new TypeExpression((IType) new UnknownTypeSpec(null, identifier), Location)); // ambigious
                    }

                    return(new AliasNamespace(childNamespace, Location));
                }
            }
            // then look for a type
            ITypeDefinition def = n.GetTypeDefinition(identifier, k);

            if (def != null)
            {
                IType result = def;
                if (parameterizeResultType && k > 0)
                {
                    result = new ParameterizedTypeSpec(def, typeArguments);
                }

                if (usingScope != null && usingScope.HasAlias(identifier))
                {
                    rc.Report.Error(7, loc, "The name `{0}' is ambigious", name);
                    return(new TypeExpression((IType) new UnknownTypeSpec(null, identifier), Location)); // ambigious
                }
                else
                {
                    return(new TypeExpression(result, Location));
                }
            }
            return(null);
        }
Esempio n. 3
0
        public override Expression Resolve(ResolveContext resolver)
        {
            // resolved cache
            if (_resolved)
            {
                return(this);
            }

            if (name == "global")
            {
                _resolved = true;
                ns        = resolver.compilation.RootNamespace;
                return(this);
            }


            for (ResolvedUsingScope n = resolver.CurrentUsingScope; n != null; n = n.Parent)
            {
                if (n.ExternAliases.Contains(name))
                {
                    return(ResolveExternAlias(resolver, name));
                }
                foreach (var pair in n.UsingAliases)
                {
                    if (pair.Key == name)
                    {
                        if (pair.Value is AliasNamespace)
                        {
                            return(pair.Value);
                        }
                        else
                        {
                            resolver.Report.Error(6, loc, "The name `{0}' does not exist in the current context", name);
                            return(ErrorResult);
                        }
                    }
                }
            }

            resolver.Report.Error(6, loc, "The name `{0}' does not exist in the current context", name);
            return(ErrorResult);
        }
Esempio n. 4
0
        /// <summary>
        /// Looks up an alias (identifier in front of :: operator)
        /// </summary>
        public Expression ResolveAlias(ResolveContext rc, string identifier)
        {
            if (identifier == "global")
            {
                return(new AliasNamespace(rc.compilation.RootNamespace, Location));
            }

            for (ResolvedUsingScope n = rc.CurrentUsingScope; n != null; n = n.Parent)
            {
                if (n.ExternAliases.Contains(identifier))
                {
                    return(ResolveExternAlias(rc, identifier));
                }
                foreach (var pair in n.UsingAliases)
                {
                    if (pair.Key == identifier)
                    {
                        return((Expression)(pair.Value as AliasNamespace) ?? ErrorResult);
                    }
                }
            }
            return(ErrorResult);
        }
        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);
        }
 ResolveResult LookInUsingScopeNamespace(ResolvedUsingScope usingScope, INamespace n, string identifier, IList<IType> typeArguments, bool parameterizeResultType)
 {
     if (n == null)
         return null;
     // first look for a namespace
     int k = typeArguments.Count;
     if (k == 0) {
         INamespace childNamespace = n.GetChildNamespace(identifier);
         if (childNamespace != null) {
             if (usingScope != null && usingScope.HasAlias(identifier))
                 return new AmbiguousTypeResolveResult(new UnknownType(null, identifier));
             return new NamespaceResolveResult(childNamespace);
         }
     }
     // then look for a type
     ITypeDefinition def = n.GetTypeDefinition(identifier, k);
     if (def != null) {
         IType result = def;
         if (parameterizeResultType && k > 0) {
             result = new ParameterizedType(def, typeArguments);
         }
         if (usingScope != null && usingScope.HasAlias(identifier))
             return new AmbiguousTypeResolveResult(result);
         else
             return new TypeResolveResult(result);
     }
     return null;
 }
 /// <summary>
 /// Sets the current using scope that is used to look up identifiers as class names.
 /// </summary>
 public TypeScriptResolver WithCurrentUsingScope(ResolvedUsingScope usingScope)
 {
     return WithContext(context.WithUsingScope(usingScope));
 }
Esempio n. 8
0
        Expression LookInCurrentUsingScope(ResolveContext rc, string identifier, IList <IType> typeArguments, bool isInUsingDeclaration, bool parameterizeResultType)
        {
            // look in current namespace definitions
            ResolvedUsingScope currentUsingScope = rc.CurrentUsingScope;

            for (ResolvedUsingScope u = currentUsingScope; u != null; u = u.Parent)
            {
                var resultInNamespace = LookInUsingScopeNamespace(rc, u, u.Namespace, identifier, typeArguments, parameterizeResultType);
                if (resultInNamespace != null)
                {
                    return(resultInNamespace);
                }
                // then look for aliases:
                if (typeArguments.Count == 0)
                {
                    if (u.ExternAliases.Contains(identifier))
                    {
                        return(ResolveExternAlias(rc, identifier));
                    }
                    if (!(isInUsingDeclaration && u == currentUsingScope))
                    {
                        foreach (var pair in u.UsingAliases)
                        {
                            if (pair.Key == identifier)
                            {
                                return(pair.Value.ShallowClone());
                            }
                        }
                    }
                }
                // finally, look in the imported namespaces:
                if (!(isInUsingDeclaration && u == currentUsingScope))
                {
                    IType firstResult = null;
                    foreach (var importedNamespace in u.Usings)
                    {
                        ITypeDefinition def = importedNamespace.GetTypeDefinition(identifier, typeArguments.Count);
                        if (def != null)
                        {
                            IType resultType;
                            if (parameterizeResultType && typeArguments.Count > 0)
                            {
                                resultType = new ParameterizedTypeSpec(def, typeArguments);
                            }
                            else
                            {
                                resultType = def;
                            }

                            if (firstResult == null || !TopLevelTypeDefinitionIsAccessible(rc, firstResult.GetDefinition()))
                            {
                                if (TopLevelTypeDefinitionIsAccessible(rc, resultType.GetDefinition()))
                                {
                                    firstResult = resultType;
                                }
                            }
                            else if (TopLevelTypeDefinitionIsAccessible(rc, def))
                            {
                                rc.Report.Error(7, loc, "The name `{0}' is ambigious", name);
                                return(new TypeExpression(firstResult, Location)); // ambigious
                            }
                        }
                    }
                    if (firstResult != null)
                    {
                        return(new TypeExpression(firstResult, Location));
                    }
                }
                // if we didn't find anything: repeat lookup with parent namespace
            }
            return(null);
        }