private void WriteUsedNameSpaceList(UsedNamespaceList nsl) { if (nsl == null) { return; } for (int i = 0; i < nsl.Count; i++) { WriteUsedNameSpace(nsl[i]); } }
public Looker(Scope scope, ErrorHandler errorHandler, TrivialHashtable scopeFor, TypeSystem typeSystem, TrivialHashtable ambiguousTypes, TrivialHashtable referencedLabels) : base(errorHandler){ //TODO: verify that crucial system types have either been imported or defined by the Parser this.scope = scope; this.AddToAllScopes(this.scope); this.scopeFor = scopeFor; this.ambiguousTypes = ambiguousTypes; this.referencedLabels = referencedLabels; this.alreadyReported = new TrivialHashtable(); this.hasExplicitBaseClass = new TrivialHashtable(); this.typesToKeepUninstantiated = new TrivialHashtable(); this.UsedNamespaces = new UsedNamespaceList(); this.targetFor = new TrivialHashtable(); this.labelList = new IdentifierList(); this.AbstractSealedUsedAsType = Error.NotAType; Debug.Assert(typeSystem != null); this.typeSystem = typeSystem; this.useGenerics = TargetPlatform.UseGenerics; this.inMethodParameter = false; this.inEventContext = false; }
public virtual UsedNamespaceList VisitUsedNamespaceList(UsedNamespaceList usedNspaces, UsedNamespaceList changes, UsedNamespaceList deletions, UsedNamespaceList insertions){ if (usedNspaces == null) return changes; if (changes != null){ if (deletions == null || insertions == null) Debug.Assert(false); else{ } }else if (deletions != null) return null; return usedNspaces; }
public virtual UsedNamespace GetClosestMatch(UsedNamespace/*!*/ nd1, UsedNamespaceList/*!*/ list1, UsedNamespaceList list2, int list1pos, ref int list2start, TrivialHashtable/*!*/ matchedNodes, out Differences closestDifferences, out int list2pos) { closestDifferences = null; list2pos = -1; if (list2 == null) return null; if (nd1 == null || list1 == null || matchedNodes == null || list1pos < 0 || list1pos >= list1.Count || list2start < 0 || list2start >= list2.Count) { Debug.Assert(false); return null; } UsedNamespace closest = null; Differences winnerSoFar = null; for (int j = list2start, m = list2.Count; j < m; j++){ UsedNamespace nd2 = list2[j]; if (list2start == j) list2start++; if (nd2 == null) continue; if (matchedNodes[nd2.UniqueKey] != null) continue; Differences diff = this.GetDifferences(nd1, nd2); if (diff == null){Debug.Assert(false); continue;} if (diff.Similarity <= 0.5){ //Not a good enough match if (list2start == j+1) list2start--; //The next call to GetClosestMatch will start looking at list2start, so this node will be considered then continue; //ignore it for the rest of this call } if (winnerSoFar != null && winnerSoFar.Similarity >= diff.Similarity) continue; winnerSoFar = closestDifferences = diff; closest = nd2; list2pos = j; if (diff.NumberOfDifferences == 0) return closest; //Perfect match, no need to look for other matches } if (closest != null){ //^ assert winnerSoFar != null; //closest is closer to nd1 than any other node in list2, but this is no good if some other node in list1 has a better claim on closest for (int i = list1pos+1, n = list1.Count; i < n; i++){ UsedNamespace nd1alt = list1[i]; if (nd1alt == null) continue; if (matchedNodes[nd1alt.UniqueKey] != null) continue; Differences diff = this.GetDifferences(nd1alt, closest); if (diff == null){Debug.Assert(false); continue;} if (diff.Similarity <= winnerSoFar.Similarity) continue; //nd1alt has a better claim on closest. See if it wants closest. Differences diff2; int j, k = list2start; UsedNamespace nd2alt = this.GetClosestMatch(nd1alt, list1, list2, i, ref k, matchedNodes, out diff2, out j); if (nd2alt != closest){ Debug.Assert(nd2alt != null && diff2 != null && diff2.Similarity >= diff.Similarity); continue; //nd1alt prefers nd2alt to closest, so closest is still available } //nd1alt wants closest, take it out of the running matchedNodes[closest.UniqueKey] = nd1alt; //Now that closest is out of the running, try again k = list2start; UsedNamespace newClosest = this.GetClosestMatch(nd1, list1, list2, i, ref k, matchedNodes, out winnerSoFar, out list2pos); //put closest back in the running so that the next call to this routine will pick it up matchedNodes[closest.UniqueKey] = closest; closest = newClosest; break; } } closestDifferences = winnerSoFar; return closest; }
public virtual Differences VisitUsedNamespaceList(UsedNamespaceList list1, UsedNamespaceList list2, out UsedNamespaceList changes, out UsedNamespaceList deletions, out UsedNamespaceList insertions){ changes = list1 == null ? null : list1.Clone(); deletions = list1 == null ? null : list1.Clone(); insertions = list1 == null ? new UsedNamespaceList() : list1.Clone(); //^ assert insertions != null; Differences differences = new Differences(); for (int j = 0, n = list2 == null ? 0 : list2.Count; j < n; j++){ //^ assert list2 != null; UsedNamespace nd2 = list2[j]; if (nd2 == null) continue; insertions.Add(null); } TrivialHashtable savedDifferencesMapFor = this.differencesMapFor; this.differencesMapFor = null; TrivialHashtable matchedNodes = new TrivialHashtable(); for (int i = 0, k = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){ //^ assert list1 != null && changes != null && deletions != null; UsedNamespace nd1 = list1[i]; if (nd1 == null) continue; Differences diff; int j; UsedNamespace nd2 = this.GetClosestMatch(nd1, list1, list2, i, ref k, matchedNodes, out diff, out j); if (nd2 == null || diff == null){Debug.Assert(nd2 == null && diff == null); continue;} matchedNodes[nd1.UniqueKey] = nd1; matchedNodes[nd2.UniqueKey] = nd2; changes[i] = diff.Changes as UsedNamespace; deletions[i] = diff.Deletions as UsedNamespace; insertions[i] = diff.Insertions as UsedNamespace; insertions[n+j] = nd1; //Records the position of nd2 in list2 in case the change involved a permutation Debug.Assert(diff.Changes == changes[i] && diff.Deletions == deletions[i] && diff.Insertions == insertions[i]); differences.NumberOfDifferences += diff.NumberOfDifferences; differences.NumberOfSimilarities += diff.NumberOfSimilarities; } //Find deletions for (int i = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){ //^ assert list1 != null && changes != null && deletions != null; UsedNamespace nd1 = list1[i]; if (nd1 == null) continue; if (matchedNodes[nd1.UniqueKey] != null) continue; changes[i] = null; deletions[i] = nd1; insertions[i] = null; differences.NumberOfDifferences += 1; } //Find insertions for (int j = 0, n = list1 == null ? 0 : list1.Count, m = list2 == null ? 0 : list2.Count; j < m; j++){ //^ assert list2 != null; UsedNamespace nd2 = list2[j]; if (nd2 == null) continue; if (matchedNodes[nd2.UniqueKey] != null) continue; insertions[n+j] = nd2; //Records nd2 as an insertion into list1, along with its position in list2 differences.NumberOfDifferences += 1; //REVIEW: put the size of the tree here? } if (differences.NumberOfDifferences == 0){ changes = null; deletions = null; insertions = null; } this.differencesMapFor = savedDifferencesMapFor; return differences; }
public virtual UsedNamespaceList VisitUsedNamespaceList(UsedNamespaceList usedNspaces){ if (usedNspaces == null) return null; for (int i = 0, n = usedNspaces.Count; i < n; i++) usedNspaces[i] = this.VisitUsedNamespace(usedNspaces[i]); return usedNspaces; }
public virtual void VisitUsedNamespaceList(UsedNamespaceList usedNspaces) { if (usedNspaces == null) return; for (int i = 0, n = usedNspaces.Count; i < n; i++) this.VisitUsedNamespace(usedNspaces[i]); }
public override UsedNamespaceList VisitUsedNamespaceList(UsedNamespaceList usedNspaces) { if (usedNspaces == null) return null; return base.VisitUsedNamespaceList(usedNspaces.Clone()); }
public virtual UsedNamespaceList VisitUsedNamespaceList(UsedNamespaceList usedNspaces1, UsedNamespaceList usedNspaces2) { if (usedNspaces1 == null) return null; for (int i = 0, n = usedNspaces1.Count, m = usedNspaces2 == null ? 0 : usedNspaces2.Count; i < n; i++) { //^ assert usedNspaces2 != null; if (i >= m) usedNspaces1[i] = this.VisitUsedNamespace(usedNspaces1[i], null); else usedNspaces1[i] = this.VisitUsedNamespace(usedNspaces1[i], usedNspaces2[i]); } return usedNspaces1; }
public Namespace(Identifier name, Identifier fullName, AliasDefinitionList aliasDefinitions, UsedNamespaceList usedNamespaces, NamespaceList nestedNamespaces, TypeNodeList types) : base(NodeType.Namespace) { this.Name = name; this.FullNameId = fullName; if(fullName != null) this.fullName = fullName.ToString(); this.AliasDefinitions = aliasDefinitions; this.NestedNamespaces = nestedNamespaces; this.Types = types; this.UsedNamespaces = usedNamespaces; }
public MethodScope(Class/*!*/ parentScope, UsedNamespaceList usedNamespaces, Method method) { this.baseClass = parentScope; this.UsedNamespaces = usedNamespaces; this.DeclaringModule = parentScope.DeclaringModule; this.DeclaringMethod = method; if(method != null && (method.Flags & MethodFlags.Static) == 0) this.ThisType = this.ThisTypeInstance = method.DeclaringType; if(method != null) this.LexicalSourceExtent = method.SourceContext; }
public MethodScope(Class/*!*/ parentScope, UsedNamespaceList usedNamespaces) : this(parentScope, usedNamespaces, null) { }
public override Namespace VisitNamespace(Namespace nspace){ if (nspace == null) return null; Identifier nsId = nspace.URI == null ? nspace.FullNameId : nspace.URI; Debug.Assert(nsId != null); if (nsId == null) nsId = nspace.Name; if (nsId == null) nsId = Identifier.Empty; Scope savedScope = this.scope; NamespaceScope nsscope = (NamespaceScope)this.scopeFor[nspace.UniqueKey]; if (nsscope == null || nsscope.AssociatedNamespace == null || nsscope.AssociatedModule == null){Debug.Assert(false); return null;} this.scope = nsscope; this.AddToAllScopes(this.scope); this.AddNodePositionAndInfo(nsId, nspace, IdentifierContexts.NullContext); AliasDefinitionList aliases = nspace.AliasDefinitions; TrivialHashtable aliasedType = nsscope.AliasedType = new TrivialHashtable(); TrivialHashtable aliasedNamespace = nsscope.AliasedNamespace = new TrivialHashtable(); for (int i = 0, n = aliases == null ? 0 : aliases.Count; i < n; i++){ AliasDefinition aliasDef = aliases[i]; if (aliasDef == null || aliasDef.Alias == null) continue; int aliasIdKey = aliasDef.Alias.UniqueIdKey; if (aliasDef.AliasedAssemblies != null){ AssemblyReferenceList arefs = this.currentModule == null ? null : this.currentModule.AssemblyReferences; for (int j = 0, m = arefs == null ? 0 : arefs.Count; j < m; j++){ AssemblyReference aref = arefs[j]; if (aref == null) continue; for (int jj = 0, nn = aref.Aliases == null ? 0 : aref.Aliases.Count; jj < nn; jj++){ Identifier aliasId = aref.Aliases[jj]; if (aliasId == null) continue; if (aliasId.UniqueIdKey == aliasIdKey){ aliasDef.AliasedAssemblies.Add(aref); break; } } } continue; } if (aliasDef.AliasedExpression == null) continue; TypeNodeList duplicates = null; TypeNode aType = this.LookupType(aliasDef.AliasedExpression, nsscope, out duplicates); if (aType != null){ aliasDef.AliasedType = new TypeReference(new TypeExpression(aliasDef.AliasedExpression, 0, aType.SourceContext), aType); aliasedType[aliasDef.Alias.UniqueIdKey] = aType; if (duplicates != null && duplicates.Count > 1){ this.HandleError(aliasDef.AliasedExpression, Error.AmbiguousTypeReference, aliasDef.AliasedExpression.ToString()); for (int j = 0, m = duplicates.Count; j < m; j++){ TypeNode dup = duplicates[j]; if (dup == null || dup == aliasDef.AliasedType) continue; this.HandleRelatedError(dup); } } }else{ //TODO: need to leave the AliasedExpression alone (but check it) //Other parts of the Looker must change to stop expecting namespace qualifiers to be just dotted identifiers this.ExpandRootIfAnAlias(ref aliasDef.AliasedExpression, nsscope); aliasDef.AliasedExpression = this.ConvertToIdentifier(aliasDef.AliasedExpression); aliasDef.AliasedNamespace = this.GetNamespaceIfValidAndComplainIfNeeded(aliasDef.AliasedExpression); if (aliasDef.AliasedNamespace != null) aliasedNamespace[aliasDef.Alias.UniqueIdKey] = aliasDef.AliasedNamespace; this.AddNodePositionAndInfo(aliasDef.AliasedExpression, aliasDef, IdentifierContexts.AllContext); } } UsedNamespaceList savedUsedNamespaces = this.UsedNamespaces; UsedNamespaceList usedNamespaces = this.UsedNamespaces = nspace.UsedNamespaces; for (int i = 0, n = usedNamespaces == null ? 0 : usedNamespaces.Count; i < n; i++){ UsedNamespace unSpace = usedNamespaces[i]; if (unSpace == null || unSpace.Namespace == null || unSpace.SourceContext.StartPos < nspace.SourceContext.StartPos) continue; if (unSpace.Namespace.Prefix == null){ Identifier fullName = nsscope.GetNamespaceFullNameFor(unSpace.Namespace); if (fullName == null && nspace.Name != null && nspace.Name != Identifier.Empty){ string nsName = nspace.Name.Name; string nsFullName = nspace.FullName; if (nsFullName != null && nsName != null && nsFullName.Length > nsName.Length) { string nsEnclosingName = nsFullName.Substring(0, nsFullName.Length - nsName.Length); Identifier id = new Identifier(nsEnclosingName + unSpace.Namespace); fullName = nsscope.GetNamespaceFullNameFor(id); } } if (fullName != null) unSpace.Namespace = new Identifier(fullName.Name, unSpace.Namespace.SourceContext); else this.GetNamespaceIfValidAndComplainIfNeeded(unSpace.Namespace); }else if (!this.IsPrefixForTheGlobalNamespace(unSpace.Namespace.Prefix)){ Identifier expandedPrefix = nsscope.GetNamespaceFullNameFor(unSpace.Namespace.Prefix); if (expandedPrefix != null){ expandedPrefix = new Identifier(expandedPrefix.Name, unSpace.Namespace.Prefix.SourceContext); QualifiedIdentifier qual = new QualifiedIdentifier(expandedPrefix, unSpace.Namespace, unSpace.Namespace.SourceContext); qual.Identifier = this.GetUnprefixedIdentifier(unSpace.Namespace); unSpace.Namespace = this.GetNamespaceIfValidAndComplainIfNeeded(qual); }else this.HandleError(unSpace.Namespace.Prefix, Error.AliasNotFound, unSpace.Namespace.Prefix.Name); }else this.GetNamespaceIfValidAndComplainIfNeeded(unSpace.Namespace); this.AddNodePositionAndInfo(unSpace.Namespace, unSpace, IdentifierContexts.AllContext); } AttributeList attributes = nspace.Attributes; if (attributes != null){ nspace.Attributes = null; int n = attributes.Count; AttributeList assemblyAttributes = null; AttributeList moduleAttributes = null; AssemblyNode assem = this.currentModule as AssemblyNode; if (assem != null){ assemblyAttributes = assem.Attributes; if (assemblyAttributes == null) assem.Attributes = assemblyAttributes = new AttributeList(n); moduleAttributes = assem.ModuleAttributes; if (moduleAttributes == null) assem.ModuleAttributes = moduleAttributes = new AttributeList(n); }else{ moduleAttributes = this.currentModule.Attributes; if (moduleAttributes == null) this.currentModule.Attributes = moduleAttributes = new AttributeList(n); } for (int i = 0; i < n; i++){ AttributeNode attr = this.VisitAttributeNode(attributes[i]); if (attr == null) continue; attributes[i] = null; if (attr.Target == AttributeTargets.Assembly && assem != null) assemblyAttributes.Add(attr); else if (attr.Target == AttributeTargets.Module) moduleAttributes.Add(attr); else attributes[i] = attr; } } TypeNodeList types = nspace.Types; for (int i = 0, n = types == null ? 0 : types.Count; i < n; i++) this.Visit(types[i]); NamespaceList nestedNamespaces = nspace.NestedNamespaces; for (int i = 0, n = nestedNamespaces == null ? 0 : nestedNamespaces.Count; i < n; i++) this.VisitNamespace(nestedNamespaces[i]); this.scope = savedScope; this.UsedNamespaces = savedUsedNamespaces; return nspace; }
private UsedNamespaceList Translate(CodeNamespaceImportCollection imports){ int n = imports == null ? 0 : imports.Count; UsedNamespaceList usedNspaces = new UsedNamespaceList(n); for (int i = 0; i < n; i++) usedNspaces.Add(this.Translate(imports[i])); return usedNspaces; }
private void WriteUsedNameSpaceList(UsedNamespaceList nsl) { if (nsl == null) return; for(int i=0; i<nsl.Count; i++) { WriteUsedNameSpace(nsl[i]); } }
public EventingVisitor(Action<UsedNamespaceList> visitUsedNamespaceList) { VisitedUsedNamespaceList += visitUsedNamespaceList; } public event Action<UsedNamespaceList> VisitedUsedNamespaceList; public override UsedNamespaceList VisitUsedNamespaceList(UsedNamespaceList usedNspaces) { if (VisitedUsedNamespaceList != null) VisitedUsedNamespaceList(usedNspaces); return base.VisitUsedNamespaceList(usedNspaces); }