Exemplo n.º 1
0
 public virtual Node LookupTypeOrNamespace(Identifier prefix, Identifier identifier, bool typeOnly, NamespaceScope nsScope, int numTemplateArgs){
   if (identifier == null || nsScope == null){Debug.Assert(false); return null;}
   if (numTemplateArgs != 0 && TargetPlatform.GenericTypeNamesMangleChar != 0)
     identifier = new Identifier(identifier.ToString()+TargetPlatform.GenericTypeNamesMangleChar+numTemplateArgs, identifier.SourceContext);
   TypeNodeList duplicates = null;
   TypeNode t = null;
   if (prefix == null){
     Node n = this.LookupTypeOrNamespace(identifier, typeOnly, nsScope, out duplicates);
     t = n as TypeNode;
     if (t == null) return n;
   }else{
     Node tOrn = this.LookupTypeOrNamespace(prefix, false, nsScope, out duplicates);
     AliasDefinition aliasDef = tOrn as AliasDefinition;
     if (aliasDef != null && aliasDef.AliasedAssemblies != null){
       //The prefix just restricts the assemblies in which to look for identifier
       t = this.LookupType(identifier, aliasDef.AliasedAssemblies, out duplicates);
       if (t != null) goto returnT;
       return identifier;
     }
     Identifier nestedNamespaceFullName = nsScope.GetNamespaceFullNameFor(prefix);
     if (nestedNamespaceFullName != null){
       if (this.IsPrefixForTheGlobalNamespace(nestedNamespaceFullName.Prefix)){
         while (nsScope.Name != Identifier.Empty && nsScope.OuterScope is NamespaceScope)
           nsScope = (NamespaceScope)nsScope.OuterScope;
       }else if (nestedNamespaceFullName.Prefix != null){
         Node n = this.LookupTypeOrNamespace(nestedNamespaceFullName.Prefix, nestedNamespaceFullName, false, 0);
         if (n is TypeNode){
           //TODO: error
           return null;
         }else{
           Identifier nestedNamespaceFullName2 = n as Identifier;
           if (nestedNamespaceFullName2 == null){
             //TODO: error
             return null;
           }else{
             nestedNamespaceFullName = nestedNamespaceFullName2;
           }
         }
       }
       t = nsScope.GetType(nestedNamespaceFullName, identifier, out duplicates);
       if (t == null && !typeOnly){
         nestedNamespaceFullName = nsScope.GetNamespaceFullNameFor(Identifier.For(nestedNamespaceFullName.Name+ "." + identifier.Name));
         if (nestedNamespaceFullName != null) return nestedNamespaceFullName;
       }
     }else{
       Identifier Uri = nsScope.GetUriFor(prefix);
       if (Uri != null){
         t = nsScope.GetType(identifier, out duplicates);
         int numDups = duplicates == null ? 0 : duplicates.Count;
         if (numDups > 1){
           t = null;
           for (int i = 0, n = numDups; i < n; i++){
             TypeNode dup = duplicates[i];
             if (dup == null){numDups--; continue;}
             Identifier dupUri = this.GetUriNamespaceFor(dup);
             if (dupUri != null && dupUri.UniqueIdKey == Uri.UniqueIdKey){
               if (t != null) t = dup;
             }else{
               numDups--;
             }
           }
         }
         if (numDups <= 1 && t != null){
           duplicates = null;
         }
       }else{
         t = this.LookupType(prefix, identifier, nsScope, out duplicates);
       }
     }
   }
   if (this.NoDuplicatesAfterOverloadingOnTemplateParameters(duplicates, numTemplateArgs, ref t))
     duplicates = null;
   if (duplicates != null && duplicates.Count > 1){
     if (t == null){Debug.Assert(false); return null;}
     bool allDuplicatesHaveTheSameName = true;
     for (int i = 0; i < duplicates.Count && allDuplicatesHaveTheSameName; i++){
       TypeNode dup = duplicates[i];
       if (dup == null) continue;
       allDuplicatesHaveTheSameName = dup.FullName == t.FullName;
     }
     if (t.DeclaringModule != this.currentModule && allDuplicatesHaveTheSameName){
       this.HandleError(identifier, Error.MultipleTypeImport, this.GetTypeName(t), t.DeclaringModule.Location);
       goto returnT;
     }
     this.HandleError(identifier, Error.AmbiguousTypeReference, identifier.ToString());
     for (int i = 0, n = duplicates.Count; i < n; i++){
       TypeNode dup = duplicates[i];
       if (dup == null) continue;
       this.HandleRelatedError(dup);
     }
     goto returnT;
   }else if (t != null){
     if (!this.TypeIsVisible(t)) goto done;
   }
   returnT:
     if (t != null){
       this.AddNodePositionAndInfo(identifier, t, IdentifierContexts.AllContext);
       return t;
     }
   done:
     if (t != null){
       if (this.alreadyReported[t.UniqueKey] != null) return t;
       this.alreadyReported[t.UniqueKey] = t;
       this.HandleError(identifier, Error.TypeNotAccessible, this.GetTypeName(t));
       return t;
     }
   return null;
 }
Exemplo n.º 2
0
 public virtual Node LookupTypeOrNamespace(Identifier identifier, bool typeOnly, NamespaceScope nsScope, out TypeNodeList duplicates){
   duplicates = null;
   if (identifier == null || nsScope == null){Debug.Assert(false); return null;}
   TypeNode t = nsScope.GetType(identifier, out duplicates, true);
   if (t != null && t.Name != null && t.Name.UniqueIdKey == identifier.UniqueIdKey){
     AliasDefinition aliasDef = nsScope.GetConflictingAlias(identifier);
     if (aliasDef != null) aliasDef.ConflictingType = t;
   }else if (!typeOnly){
     TrivialHashtable alreadySeenAliases = new TrivialHashtable();
     AliasDefinition aliasDef = nsScope.GetAliasFor(identifier);
     while (aliasDef != null){
       if (aliasDef.AliasedType != null) return (TypeNode)aliasDef.AliasedType;
       if (alreadySeenAliases[aliasDef.UniqueKey] != null) break; //TODO: error?
       alreadySeenAliases[aliasDef.UniqueKey] = aliasDef;
       if (aliasDef.AliasedAssemblies != null) return aliasDef;
       if (aliasDef.AliasedExpression is Identifier)
         aliasDef = nsScope.GetAliasFor(identifier = (Identifier)aliasDef.AliasedExpression);
       else if (aliasDef.AliasedExpression is QualifiedIdentifier)
         return this.LookupTypeOrNamespace(aliasDef.AliasedExpression, false);
       else
         aliasDef = null; 
     }
     Identifier ns = nsScope.GetNamespaceFullNameFor(identifier);
     if (ns != null) return ns;
   }
   return t;
 }