Esempio n. 1
0
 public static IList <ITypeParameterSymbol> GetReferencedTypeParameters(
     this ITypeSymbol type, IList <ITypeParameterSymbol> result = null)
 {
     result = result ?? new List <ITypeParameterSymbol>();
     type?.Accept(new CollectTypeParameterSymbolsVisitor(result, onlyMethodTypeParameters: false));
     return(result);
 }
Esempio n. 2
0
 private static ITypeSymbol RemoveUnavailableTypeParameters(
     this ITypeSymbol type,
     Compilation compilation,
     ISet <string> availableTypeParameterNames)
 {
     return(type?.Accept(new UnavailableTypeParameterRemover(compilation, availableTypeParameterNames)));
 }
            public static IEnumerable<ITypeParameterSymbol> Collect(ITypeSymbol typeSymbol)
            {
                var collector = new TypeParameterCollector();
                typeSymbol.Accept(collector);

                return collector._typeParameters;
            }
            public static IEnumerable <ITypeParameterSymbol> Collect(ITypeSymbol typeSymbol)
            {
                var collector = new TypeParameterCollector();

                typeSymbol.Accept(collector);

                return(collector._typeParameters);
            }
Esempio n. 5
0
 public static ITypeSymbol RemoveUnnamedErrorTypes(
     this ITypeSymbol type,
     Compilation compilation)
 {
     return(type == null
         ? null
         : type.Accept(new UnnamedErrorTypeRemover(compilation)));
 }
Esempio n. 6
0
 public static ITypeSymbol RemoveAnonymousTypes(
     this ITypeSymbol type,
     Compilation compilation)
 {
     return(type == null
         ? null
         : type.Accept(new AnonymousTypeRemover(compilation)));
 }
 public static ITypeSymbol SubstituteTypes <TType1, TType2>(
     this ITypeSymbol type,
     IDictionary <TType1, TType2> mapping,
     ITypeGenerator typeGenerator)
     where TType1 : ITypeSymbol
     where TType2 : ITypeSymbol
 {
     return(type?.Accept(new SubstituteTypesVisitor <TType1, TType2>(mapping, typeGenerator)));
 }
 public static ITypeSymbol ReplaceTypeParametersBasedOnTypeConstraints(
     this ITypeSymbol type,
     Compilation compilation,
     IEnumerable <ITypeParameterSymbol> availableTypeParameters,
     Solution solution,
     CancellationToken cancellationToken)
 {
     return(type?.Accept(new ReplaceTypeParameterBasedOnTypeConstraintVisitor(compilation, availableTypeParameters.Select(t => t.Name).ToSet(), solution, cancellationToken)));
 }
        private void VisitArrayType(IArrayTypeSymbol symbol, TypeSymbolWithAnnotations typeOpt)
        {
            if (TryAddAlias(symbol, builder))
            {
                return;
            }

            //See spec section 12.1 for the order of rank specifiers
            //e.g. int[][,][,,] is stored as
            //     ArrayType
            //         Rank = 1
            //         ElementType = ArrayType
            //             Rank = 2
            //             ElementType = ArrayType
            //                 Rank = 3
            //                 ElementType = int

            if (format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.ReverseArrayRankSpecifiers))
            {
                // Ironically, reverse order is simpler - we just have to recurse on the element type and then add a rank specifier.
                symbol.ElementType.Accept(this);
                AddArrayRank(symbol);
                return;
            }

            TypeSymbolWithAnnotations underlyingTypeWithAnnotations;
            ITypeSymbol underlyingType = symbol;

            do
            {
                underlyingTypeWithAnnotations = (underlyingType as ArrayTypeSymbol)?.ElementType ?? default;
                underlyingType = ((IArrayTypeSymbol)underlyingType).ElementType;
            }while (underlyingType.Kind == SymbolKind.ArrayType && !ShouldAddNullableAnnotation(underlyingTypeWithAnnotations));

            if (underlyingTypeWithAnnotations.HasType)
            {
                VisitTypeSymbolWithAnnotations(underlyingTypeWithAnnotations);
            }
            else
            {
                underlyingType.Accept(this.NotFirstVisitor);
            }

            var arrayType = symbol;

            while (arrayType != null && arrayType != underlyingType)
            {
                if (!this.isFirstSymbolVisited)
                {
                    AddCustomModifiersIfRequired(arrayType.CustomModifiers, leadingSpace: true);
                }

                AddArrayRank(arrayType);
                arrayType = arrayType.ElementType as IArrayTypeSymbol;
            }
        }
 private void AddConstantValue(ITypeSymbol type, object constantValue, bool preferNumericValueOrExpandedFlagsForEnum = false)
 {
     if (constantValue != null)
     {
         AddNonNullConstantValue(type, constantValue, preferNumericValueOrExpandedFlagsForEnum);
     }
     else if (type.IsReferenceType || type.TypeKind == TypeKind.PointerType || type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
     {
         AddKeyword(SyntaxKind.NullKeyword);
     }
     else
     {
         AddKeyword(SyntaxKind.DefaultKeyword);
         AddPunctuation(SyntaxKind.OpenParenToken);
         type.Accept(this.NotFirstVisitor);
         AddPunctuation(SyntaxKind.CloseParenToken);
     }
 }
Esempio n. 11
0
 private void AddConstantValue(ITypeSymbol type, object constantValue, bool preferNumericValueOrExpandedFlagsForEnum = false)
 {
     if (constantValue != null)
     {
         AddNonNullConstantValue(type, constantValue, preferNumericValueOrExpandedFlagsForEnum);
     }
     else if (type.IsReferenceType || type.TypeKind == TypeKind.Pointer || type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
     {
         AddKeyword(SyntaxKind.NullKeyword);
     }
     else
     {
         AddKeyword(SyntaxKind.DefaultKeyword);
         AddPunctuation(SyntaxKind.OpenParenToken);
         type.Accept(this.NotFirstVisitor);
         AddPunctuation(SyntaxKind.CloseParenToken);
     }
 }
Esempio n. 12
0
        public override void VisitRangeVariable(IRangeVariableSymbol symbol)
        {
            if (format.LocalOptions.IncludesOption(SymbolDisplayLocalOptions.IncludeType))
            {
                ITypeSymbol type = GetRangeVariableType(symbol);

                if (type != null && type.TypeKind != TypeKind.Error)
                {
                    type.Accept(this);
                }
                else
                {
                    builder.Add(CreatePart(SymbolDisplayPartKind.ErrorTypeName, type, "?"));
                }

                AddSpace();
            }

            builder.Add(CreatePart(SymbolDisplayPartKind.RangeVariableName, symbol, symbol.Name));
        }
Esempio n. 13
0
 private void AddConstantValue(ITypeSymbol type, object constantValue, bool preferNumericValueOrExpandedFlagsForEnum = false)
 {
     if (constantValue != null)
     {
         AddNonNullConstantValue(type, constantValue, preferNumericValueOrExpandedFlagsForEnum);
     }
     else if (type.IsReferenceType || type.TypeKind == TypeKind.Pointer || ITypeSymbolHelpers.IsNullableType(type))
     {
         AddKeyword(SyntaxKind.NullKeyword);
     }
     else
     {
         AddKeyword(SyntaxKind.DefaultKeyword);
         if (!format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.AllowDefaultLiteral))
         {
             AddPunctuation(SyntaxKind.OpenParenToken);
             type.Accept(this.NotFirstVisitor);
             AddPunctuation(SyntaxKind.CloseParenToken);
         }
     }
 }
Esempio n. 14
0
        public static string Get(ITypeSymbol type, string data)
        {
            switch (type.SpecialType)
            {
            case SpecialType.System_Object:
            case SpecialType.System_Void:
            case SpecialType.System_MulticastDelegate:
            case SpecialType.System_Delegate:
            case SpecialType.System_IntPtr:
            case SpecialType.System_UIntPtr:
            case SpecialType.System_RuntimeArgumentHandle:
            case SpecialType.System_RuntimeFieldHandle:
            case SpecialType.System_RuntimeMethodHandle:
            case SpecialType.System_RuntimeTypeHandle:
            case SpecialType.System_IAsyncResult:
            case SpecialType.System_AsyncCallback:
                return(null);    //unsupported type

            case SpecialType.System_Collections_IEnumerable:
            case SpecialType.System_Collections_Generic_IEnumerable_T:
            case SpecialType.System_Collections_Generic_IList_T:
            case SpecialType.System_Collections_Generic_ICollection_T:
            case SpecialType.System_Collections_IEnumerator:
            case SpecialType.System_Collections_Generic_IEnumerator_T:
            case SpecialType.System_Collections_Generic_IReadOnlyList_T:
            case SpecialType.System_Collections_Generic_IReadOnlyCollection_T:
            case SpecialType.System_Runtime_CompilerServices_IsVolatile:
            case SpecialType.System_ArgIterator:
                return(null);    //unsupported collection type

            case SpecialType.System_IDisposable:
            case SpecialType.System_Enum:
            case SpecialType.System_TypedReference:
            case SpecialType.System_ValueType:
            case SpecialType.System_Nullable_T:
            case SpecialType.System_DateTime:
            case SpecialType.System_Array:
                Debug.Assert(false);     //td;
                return(null);

            case SpecialType.System_Boolean:
                return($"{data} === 'true'");

            case SpecialType.System_Char:

            case SpecialType.System_SByte:
            case SpecialType.System_Byte:
            case SpecialType.System_Int16:
            case SpecialType.System_UInt16:
            case SpecialType.System_Int32:
            case SpecialType.System_UInt32:
            case SpecialType.System_Int64:
            case SpecialType.System_UInt64:
            case SpecialType.System_Decimal:
            case SpecialType.System_Single:
            case SpecialType.System_Double:
            case SpecialType.System_String:
                return(data);
            }

            var isCompilationType = type.DeclaringSyntaxReferences.Any();

            if (!isCompilationType)
            {
                return(data);
            }

            var visitor = new ResponseVisitor(data);

            type.Accept(visitor);

            if (!visitor.Success)
            {
                return(null);
            }

            return(visitor.Result);
        }
        private void AddTypeParameterConstraints(ImmutableArray <ITypeSymbol> typeArguments)
        {
            if (this.isFirstSymbolVisited && format.GenericsOptions.IncludesOption(SymbolDisplayGenericsOptions.IncludeTypeConstraints))
            {
                foreach (var typeArg in typeArguments)
                {
                    if (typeArg.Kind == SymbolKind.TypeParameter)
                    {
                        var typeParam = (ITypeParameterSymbol)typeArg;

                        if (TypeParameterHasConstraints(typeParam))
                        {
                            AddSpace();
                            AddKeyword(SyntaxKind.WhereKeyword);
                            AddSpace();

                            typeParam.Accept(this.NotFirstVisitor);

                            AddSpace();
                            AddPunctuation(SyntaxKind.ColonToken);
                            AddSpace();

                            bool needComma           = false;
                            var  typeParameterSymbol = typeParam as TypeParameterSymbol;

                            //class/struct constraint must be first
                            if (typeParam.HasReferenceTypeConstraint)
                            {
                                AddKeyword(SyntaxKind.ClassKeyword);

                                switch (typeParameterSymbol?.ReferenceTypeConstraintIsNullable) // https://github.com/dotnet/roslyn/issues/26198 Switch to public API when we will have one.
                                {
                                case true:
                                    if (format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier))
                                    {
                                        AddPunctuation(SyntaxKind.QuestionToken);
                                    }
                                    break;

                                case false:
                                    if (format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.IncludeNonNullableTypeModifier))
                                    {
                                        AddPunctuation(SyntaxKind.ExclamationToken);
                                    }
                                    break;
                                }

                                needComma = true;
                            }
                            else if (typeParam.HasUnmanagedTypeConstraint)
                            {
                                builder.Add(new SymbolDisplayPart(SymbolDisplayPartKind.Keyword, null, "unmanaged"));
                                needComma = true;
                            }
                            else if (typeParam.HasValueTypeConstraint)
                            {
                                AddKeyword(SyntaxKind.StructKeyword);
                                needComma = true;
                            }

                            ImmutableArray <TypeSymbolWithAnnotations>?annotatedConstraints = typeParameterSymbol?.ConstraintTypesNoUseSiteDiagnostics; // https://github.com/dotnet/roslyn/issues/26198 Switch to public API when we will have one.

                            for (int i = 0; i < typeParam.ConstraintTypes.Length; i++)
                            {
                                ITypeSymbol baseType = typeParam.ConstraintTypes[i];
                                if (needComma)
                                {
                                    AddPunctuation(SyntaxKind.CommaToken);
                                    AddSpace();
                                }

                                if (annotatedConstraints.HasValue)
                                {
                                    VisitTypeSymbolWithAnnotations(annotatedConstraints.GetValueOrDefault()[i], this.NotFirstVisitor);
                                }
                                else
                                {
                                    baseType.Accept(this.NotFirstVisitor);
                                }

                                needComma = true;
                            }

                            //ctor constraint must be last
                            if (typeParam.HasConstructorConstraint)
                            {
                                if (needComma)
                                {
                                    AddPunctuation(SyntaxKind.CommaToken);
                                    AddSpace();
                                }

                                AddKeyword(SyntaxKind.NewKeyword);
                                AddPunctuation(SyntaxKind.OpenParenToken);
                                AddPunctuation(SyntaxKind.CloseParenToken);
                            }
                        }
                    }
                }
            }
        }
 public static Accessibility DetermineMinimalAccessibility(this ITypeSymbol typeSymbol)
 => typeSymbol.Accept(MinimalAccessibilityVisitor.Instance);
 public static ExpressionSyntax GenerateExpressionSyntax(
     this ITypeSymbol typeSymbol)
 {
     return(typeSymbol.Accept(ExpressionSyntaxGeneratorVisitor.Instance) !.WithAdditionalAnnotations(Simplifier.Annotation));
 }
Esempio n. 18
0
        private void AddTypeParameterConstraints(ImmutableArray <ITypeSymbol> typeArguments)
        {
            if (this.isFirstSymbolVisited && format.GenericsOptions.IncludesOption(SymbolDisplayGenericsOptions.IncludeTypeConstraints))
            {
                foreach (var typeArg in typeArguments)
                {
                    if (typeArg.Kind == SymbolKind.TypeParameter)
                    {
                        var typeParam = (ITypeParameterSymbol)typeArg;

                        if (TypeParameterHasConstraints(typeParam))
                        {
                            AddSpace();
                            AddKeyword(SyntaxKind.WhereKeyword);
                            AddSpace();

                            typeParam.Accept(this.NotFirstVisitor);

                            AddSpace();
                            AddPunctuation(SyntaxKind.ColonToken);
                            AddSpace();

                            bool needComma = false;

                            //class/struct constraint must be first
                            if (typeParam.HasReferenceTypeConstraint)
                            {
                                AddKeyword(SyntaxKind.ClassKeyword);

                                switch (typeParam.ReferenceTypeConstraintNullableAnnotation)
                                {
                                case CodeAnalysis.NullableAnnotation.Annotated:
                                    if (format.MiscellaneousOptions.IncludesOption(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier))
                                    {
                                        AddPunctuation(SyntaxKind.QuestionToken);
                                    }
                                    break;

                                case CodeAnalysis.NullableAnnotation.NotAnnotated:
                                    if (format.CompilerInternalOptions.IncludesOption(SymbolDisplayCompilerInternalOptions.IncludeNonNullableTypeModifier))
                                    {
                                        AddPunctuation(SyntaxKind.ExclamationToken);
                                    }
                                    break;
                                }

                                needComma = true;
                            }
                            else if (typeParam.HasUnmanagedTypeConstraint)
                            {
                                builder.Add(new SymbolDisplayPart(SymbolDisplayPartKind.Keyword, null, "unmanaged"));
                                needComma = true;
                            }
                            else if (typeParam.HasValueTypeConstraint)
                            {
                                AddKeyword(SyntaxKind.StructKeyword);
                                needComma = true;
                            }
                            else if (typeParam.HasNotNullConstraint)
                            {
                                builder.Add(new SymbolDisplayPart(SymbolDisplayPartKind.Keyword, null, "notnull"));
                                needComma = true;
                            }

                            for (int i = 0; i < typeParam.ConstraintTypes.Length; i++)
                            {
                                ITypeSymbol baseType = typeParam.ConstraintTypes[i];
                                if (needComma)
                                {
                                    AddPunctuation(SyntaxKind.CommaToken);
                                    AddSpace();
                                }

                                baseType.Accept(this.NotFirstVisitor);
                                needComma = true;
                            }

                            //ctor constraint must be last
                            if (typeParam.HasConstructorConstraint)
                            {
                                if (needComma)
                                {
                                    AddPunctuation(SyntaxKind.CommaToken);
                                    AddSpace();
                                }

                                AddKeyword(SyntaxKind.NewKeyword);
                                AddPunctuation(SyntaxKind.OpenParenToken);
                                AddPunctuation(SyntaxKind.CloseParenToken);
                            }
                        }
                    }
                }
            }
        }