static ApiParameter CreateParameter( ISymbol symbol, MetadataItem item, bool isReturn = false ) { var type = symbol switch { IParameterSymbol s => s.Type, IEventSymbol s => s.Type, IPropertySymbol s => s.Type, IFieldSymbol s => s.Type, _ => throw new ArgumentException( $"Symbol {symbol.Kind} is not supported", nameof(symbol) ) }; return(ApiParameter.Create(symbol, item, type.ToDisplayString(), isReturn)); } MetadataItem?GetMetadataItem(ISymbol symbol) { var item = DefaultVisit(symbol); if (item == null) { return(null); } item.Type = symbol.GetMemberTypeFromSymbol(); return(item.Type == MemberType.Default ? null : item); } List <MetadataItem> VisitDescendants <T>( IEnumerable <T> children, Func <T, IEnumerable <T> > getChildren, Func <T, bool> filter ) where T : ISymbol { var result = new List <MetadataItem>(); var stack = new Stack <T>(children.Reverse()); while (stack.Count > 0) { var child = stack.Pop(); if (filter(child)) { result.AddNotNull(child.Accept(this)); } foreach (var m in getChildren(child).Reverse()) { stack.Push(m); } } return(result); }
void AddMethodSyntax(IMethodSymbol symbol, MetadataItem item) { if (!symbol.ReturnsVoid) { item.Syntax.Return = CreateApiParameter(symbol, symbol.ReturnType, true); } item.Syntax.Parameters = symbol.Parameters .Select(p => CreateApiParameter(p, p.Type, false)) .ToList(); ApiParameter CreateApiParameter(ISymbol p, ITypeSymbol type, bool isReturn) { var param = ApiParameter.Create(p, item, type.ToDisplayString(), isReturn); param.Attributes = GetAttributeInfo(p.GetAttributes()); return(param); } }
public override MetadataItem VisitNamedType(INamedTypeSymbol symbol) { var item = DefaultVisit(symbol); if (item == null) { return(null); } GenerateInheritance(symbol, item, ApiFilter); if (!symbol.IsStatic) { item.ExtensionMethods = GenerateExtensionMethods(symbol); } item.Type = symbol.GetMemberTypeFromTypeKind(); item.EnsureSyntax(); item.Syntax.Content = symbol.GenerateSyntax(item.Type, ApiFilter); item.Syntax.TypeParameters = symbol.TypeParameters .Select(x => ApiParameter.Create(x, item)) .ToList(); if (symbol.TypeKind == TypeKind.Delegate) { AddMethodSyntax(symbol.DelegateInvokeMethod !, item); } item.Modifiers = symbol.GenerateNamedTypeModifiers(); item.Items = symbol.GetMembers() .Where(s => !(s is INamedTypeSymbol)) .Select(x => x.Accept(this).SomeNotNull()) .Values() .ToList(); item.Attributes = GetAttributeInfo(symbol.GetAttributes()); return(item); }
public override MetadataItem VisitMethod(IMethodSymbol symbol) { var item = GetMetadataItem(symbol); if (item == null) { return(null); } item.EnsureSyntax(); if (symbol.TypeParameters.IsNotEmpty()) { item.Syntax.TypeParameters = symbol.TypeParameters .Select(p => ApiParameter.Create(p, item)) .ToList(); } AddMethodSyntax(symbol, item); var syntaxContent = symbol.GenerateSyntax(item.Type, ApiFilter); if (!string.IsNullOrWhiteSpace(syntaxContent)) { item.Syntax.Content = syntaxContent; } item.Modifiers = symbol.GetMemberModifiersAsStrings(); item.Implements = symbol.GetMethodImplements(ApiFilter); item.Attributes = GetAttributeInfo(symbol.GetAttributes()); item.IsExplicitInterfaceImplementation = !symbol.ExplicitInterfaceImplementations.IsEmpty; item.IsExtensionMethod = symbol.IsExtensionMethod; return(item); }