public static void Create(ISymbol symbol, SymbolKeyWriter visitor) { // Store the body level symbol in two forms. The first, a highly precise form that should find explicit // symbols for the case of resolving a symbol key back in the *same* solution snapshot it was created // from. The second, in a more query-oriented form that can allow the symbol to be found in some cases // even if the solution changed (which is a supported use case for SymbolKey). // // The first way just stores the location of the symbol, which we can then validate during resolution // maps back to the same symbol kind/name. // // The second determines the sequence of symbols of the same kind and same name in the file and keeps // track of our index in that sequence. That way, if trivial edits happen, or symbols with different // names/types are added/removed, we can still find what is likely to be this symbol after the edit. var kind = symbol.Kind; var localName = symbol.Name; visitor.WriteString(localName); visitor.WriteInteger((int)kind); // write out the locations for precision Contract.ThrowIfTrue(symbol.DeclaringSyntaxReferences.IsEmpty && symbol.Locations.IsEmpty); var locations = symbol.Locations.Concat( symbol.DeclaringSyntaxReferences.SelectAsArray(r => r.GetSyntax(visitor.CancellationToken).GetLocation())); visitor.WriteLocationArray(locations); // and the ordinal for resilience visitor.WriteInteger(GetOrdinal()); return; int GetOrdinal() { var syntaxTree = locations[0].SourceTree; var compilation = ((ISourceAssemblySymbol)symbol.ContainingAssembly).Compilation; // Ensure that the tree we're looking at is actually in this compilation. It may not be in the // compilation in the case of work done with a speculative model. if (Contains(compilation.SyntaxTrees, syntaxTree)) { var semanticModel = compilation.GetSemanticModel(syntaxTree); foreach (var possibleSymbol in EnumerateSymbols(semanticModel, kind, localName, visitor.CancellationToken)) { if (possibleSymbol.symbol.Equals(symbol)) { return(possibleSymbol.ordinal); } } } return(int.MaxValue); } }
public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsTupleType); visitor.WriteSymbolKey(symbol.TupleUnderlyingType); visitor.WriteStringArray(symbol.TupleElementNames); var locations = ArrayBuilder<Location>.GetInstance(); for (var i = 0; i < symbol.TupleElementTypes.Length; i++) { locations.Add(symbol.GetMembers("Item" + (i + 1)).FirstOrDefault()?.Locations.FirstOrDefault()); } visitor.WriteLocationArray(locations.ToImmutableAndFree()); }
public sealed override void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsAnonymousType); var properties = symbol.GetMembers().OfType <IPropertySymbol>().ToImmutableArray(); var propertyTypes = properties.SelectAsArray(p => p.Type); var propertyNames = properties.SelectAsArray(p => p.Name); var propertyIsReadOnly = properties.SelectAsArray(p => p.SetMethod == null); var propertyLocations = properties.SelectAsArray(p => p.Locations.FirstOrDefault()); visitor.WriteSymbolKeyArray(propertyTypes); visitor.WriteStringArray(propertyNames); visitor.WriteBooleanArray(propertyIsReadOnly); visitor.WriteLocationArray(propertyLocations); }
public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsAnonymousType); var properties = symbol.GetMembers().OfType<IPropertySymbol>().ToImmutableArray(); var propertyTypes = properties.SelectAsArray(p => p.Type); var propertyNames = properties.SelectAsArray(p => p.Name); var propertyIsReadOnly = properties.SelectAsArray(p => p.SetMethod == null); var propertyLocations = properties.SelectAsArray(p => p.Locations.FirstOrDefault()); visitor.WriteSymbolKeyArray(propertyTypes); visitor.WriteStringArray(propertyNames); visitor.WriteBooleanArray(propertyIsReadOnly); visitor.WriteLocationArray(propertyLocations); }
public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsTupleType); visitor.WriteSymbolKey(symbol.TupleUnderlyingType); visitor.WriteStringArray(symbol.TupleElementNames); var locations = ArrayBuilder <Location> .GetInstance(); for (var i = 0; i < symbol.TupleElementTypes.Length; i++) { locations.Add(symbol.GetMembers("Item" + (i + 1)).FirstOrDefault()?.Locations.FirstOrDefault()); } visitor.WriteLocationArray(locations.ToImmutableAndFree()); }
public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsTupleType); visitor.WriteSymbolKey(symbol.TupleUnderlyingType); var friendlyNames = ArrayBuilder<String>.GetInstance(); var locations = ArrayBuilder<Location>.GetInstance(); foreach (var element in symbol.TupleElements) { friendlyNames.Add(element.IsImplicitlyDeclared ? null : element.Name); locations.Add(element.Locations.FirstOrDefault()); } visitor.WriteStringArray(friendlyNames.ToImmutableAndFree()); visitor.WriteLocationArray(locations.ToImmutableAndFree()); }
public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsTupleType); visitor.WriteSymbolKey(symbol.TupleUnderlyingType); var friendlyNames = ArrayBuilder <String> .GetInstance(); var locations = ArrayBuilder <Location> .GetInstance(); foreach (var element in symbol.TupleElements) { friendlyNames.Add(element.IsImplicitlyDeclared ? null : element.Name); locations.Add(element.Locations.FirstOrDefault()); } visitor.WriteStringArray(friendlyNames.ToImmutableAndFree()); visitor.WriteLocationArray(locations.ToImmutableAndFree()); }
public static void Create(INamedTypeSymbol symbol, SymbolKeyWriter visitor) { Debug.Assert(symbol.IsTupleType); var friendlyNames = ArrayBuilder <string> .GetInstance(); var locations = ArrayBuilder <Location> .GetInstance(); var isError = symbol.TupleUnderlyingType.TypeKind == TypeKind.Error; visitor.WriteBoolean(isError); if (isError) { var elementTypes = ArrayBuilder <ISymbol> .GetInstance(); foreach (var element in symbol.TupleElements) { elementTypes.Add(element.Type); } visitor.WriteSymbolKeyArray(elementTypes.ToImmutableAndFree()); } else { visitor.WriteSymbolKey(symbol.TupleUnderlyingType); } foreach (var element in symbol.TupleElements) { friendlyNames.Add(element.IsImplicitlyDeclared ? null : element.Name); locations.Add(element.Locations.FirstOrDefault() ?? Location.None); } visitor.WriteStringArray(friendlyNames.ToImmutableAndFree()); visitor.WriteLocationArray(locations.ToImmutableAndFree()); }