private void GenerateInheritance(INamedTypeSymbol symbol, MetadataItem item) { Dictionary <string, string> dict = null; if (symbol.TypeKind == TypeKind.Class || symbol.TypeKind == TypeKind.Struct) { var type = symbol; var inheritance = new List <string>(); dict = new Dictionary <string, string>(); var typeParamterNames = symbol.IsGenericType ? symbol.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; while (type != null) { // TODO: special handles for errorType: change to System.Object if (type.Kind == SymbolKind.ErrorType) { inheritance.Add("System.Object"); break; } if (type != symbol) { inheritance.Add(AddSpecReference(type, typeParamterNames)); } AddInheritedMembers(symbol, type, dict, typeParamterNames); type = type.BaseType; } if (symbol.TypeKind == TypeKind.Class) { inheritance.Reverse(); item.Inheritance = inheritance; } if (symbol.AllInterfaces.Length > 0) { item.Implements = (from t in symbol.AllInterfaces where FilterVisitor.CanVisitApi(t) select AddSpecReference(t, typeParamterNames)).ToList(); if (item.Implements.Count == 0) { item.Implements = null; } } } else if (symbol.TypeKind == TypeKind.Interface) { dict = new Dictionary <string, string>(); var typeParamterNames = symbol.IsGenericType ? symbol.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; AddInheritedMembers(symbol, symbol, dict, typeParamterNames); for (int i = 0; i < symbol.AllInterfaces.Length; i++) { AddInheritedMembers(symbol, symbol.AllInterfaces[i], dict, typeParamterNames); } } if (dict != null) { var inheritedMembers = (from r in dict.Values where r != null select r).ToList(); item.InheritedMembers = inheritedMembers.Count > 0 ? inheritedMembers : null; } }
protected override void AddExplicitlyCastedLiteralValue(INamedTypeSymbol namedType, SpecialType type, object value) { AddPunctuation(SyntaxKind.OpenParenToken); namedType.Accept(this.NotFirstVisitor); AddPunctuation(SyntaxKind.CloseParenToken); AddLiteralValue(type, value); }
public override MetadataItem VisitNamedType(INamedTypeSymbol symbol) { var item = DefaultVisit(symbol); if (item == null) { return(null); } GenerateInheritance(symbol, item); item.Type = VisitorHelper.GetMemberTypeFromTypeKind(symbol.TypeKind); if (item.Syntax == null) { item.Syntax = new SyntaxDetail { Content = new SortedList <SyntaxLanguage, string>() }; } if (item.Syntax.Content == null) { item.Syntax.Content = new SortedList <SyntaxLanguage, string>(); } _generator.GenerateSyntax(item.Type, symbol, item.Syntax, this); if (symbol.TypeParameters.Length > 0) { if (item.Syntax.TypeParameters == null) { item.Syntax.TypeParameters = new List <ApiParameter>(); } foreach (var p in symbol.TypeParameters) { var param = VisitorHelper.GetTypeParameterDescription(p, item, GetTripleSlashCommentParserContext(item, _preserveRawInlineComments)); item.Syntax.TypeParameters.Add(param); } } if (symbol.TypeKind == TypeKind.Delegate) { var typeGenericParameters = symbol.IsGenericType ? symbol.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; AddMethodSyntax(symbol.DelegateInvokeMethod, item, typeGenericParameters, EmptyListOfString); } _generator.GenerateNamedType(symbol, item, this); item.Items = new List <MetadataItem>(); foreach (var member in symbol.GetMembers().Where(s => !(s is INamedTypeSymbol))) { var memberItem = member.Accept(this); if (memberItem != null) { item.Items.Add(memberItem); } } AddReference(symbol); return(item); }
private void WriteUnmanagedCallConv(INamedTypeSymbol symbol) { var name = symbol.Name; if (name.StartsWith("CallConv")) { Append(name.Substring("CallConv".Length)); } else { // We should never encounter this type of call // conv but we'll handle it gracefully regardless symbol.Accept(this); } }
private void AddExplicitInterfaceIfRequired <T>(ImmutableArray <T> implementedMethods) where T : ISymbol { if (format.MemberOptions.IncludesOption(SymbolDisplayMemberOptions.IncludeExplicitInterface) && !implementedMethods.IsEmpty) { var implementedMethod = implementedMethods[0]; Debug.Assert(implementedMethod.ContainingType != null); INamedTypeSymbol containingType = implementedMethod.ContainingType; if (containingType != null) { containingType.Accept(this.NotFirstVisitor); AddPunctuation(SyntaxKind.DotToken); } } }
private void GenerateExtensionMethods(INamedTypeSymbol symbol, MetadataItem item) { var extensions = new List <string>(); foreach (var pair in _extensionMethods.Where(p => p.Key.Language == symbol.Language)) { ITypeSymbol retargetedSymbol = symbol; // get retargeted symbol for cross-assembly case. if (pair.Key != _currentCompilation) { var compilation = pair.Key.References.Any(r => r.Display == _currentCompilationRef.Display) ? pair.Key : pair.Key.AddReferences(new[] { _currentCompilationRef }); retargetedSymbol = compilation.FindSymbol <INamedTypeSymbol>(symbol); } if (retargetedSymbol == null) { continue; } foreach (var e in pair.Value) { var reduced = e.ReduceExtensionMethod(retargetedSymbol); if ((object)reduced != null) { // update reference // Roslyn could get the instaniated type. e.g. // <code> // public class Foo<T> {} // public class FooImple<T> : Foo<Foo<T[]>> {} // public static class Extension { public static void Play<Tool, Way>(this Foo<Tool> foo, Tool t, Way w) {} } // </code> // Roslyn generated id for the reduced extension method of FooImple<T> is like "Play``2(Foo{`0[]},``1)" var typeParamterNames = symbol.IsGenericType ? symbol.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; var methodGenericParameters = reduced.IsGenericMethod ? (from p in reduced.TypeParameters select p.Name).ToList() : EmptyListOfString; var id = AddSpecReference(reduced, typeParamterNames, methodGenericParameters); extensions.Add(id); } } } item.ExtensionMethods = extensions.Count > 0 ? extensions : null; }
static void GenerateInheritance( INamedTypeSymbol symbol, MetadataItem item, IApiFilter apiFilter ) { var dict = new Dictionary <string, string?>(); switch (symbol.TypeKind) { case TypeKind.Class: case TypeKind.Struct: { var type = symbol; var inheritance = new List <string>(); var typeParameterNames = symbol.IsGenericType ? symbol.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; while (type != null) { inheritance.AddWhen(type.Kind == SymbolKind.ErrorType, "System.Object"); inheritance.AddWhen(!type.Equals(symbol), type.ToDisplayString()); AddInheritedMembers(symbol, type, dict, typeParameterNames, apiFilter); type = type.BaseType; } if (symbol.TypeKind == TypeKind.Class) { inheritance.Reverse(); item.Inheritance = inheritance; } item.Implements = symbol.AllInterfaces .Where(t => apiFilter.CanVisitApi(t)) .Select(x => x.ToDisplayString()) .ToList(); break; } case TypeKind.Interface: { var typeParameterNames = symbol.IsGenericType ? symbol.Accept(TypeGenericParameterNameVisitor.Instance) : EmptyListOfString; AddInheritedMembers(symbol, symbol, dict, typeParameterNames, apiFilter); foreach (var t in symbol.AllInterfaces) { AddInheritedMembers(symbol, t, dict, typeParameterNames, apiFilter); } break; } } item.InheritedMembers = dict.Values.Where(r => r != null).ToList() !; }