public override void VisitTypeParameter(ITypeParameterSymbol symbol) { ReferenceItem.Parts[SyntaxLanguage.CSharp].Add(new LinkItem { DisplayName = symbol.Name, DisplayQualifiedNames = symbol.Name, }); }
private static TypeParameterSyntax GenerateTypeParameter(ITypeParameterSymbol symbol, CodeGenerationOptions options) { var varianceKeyword = symbol.Variance == VarianceKind.In ? SyntaxFactory.Token(SyntaxKind.InKeyword) : symbol.Variance == VarianceKind.Out ? SyntaxFactory.Token(SyntaxKind.OutKeyword) : default(SyntaxToken); return SyntaxFactory.TypeParameter( AttributeGenerator.GenerateAttributeLists(symbol.GetAttributes(), options), varianceKeyword, symbol.Name.ToIdentifierToken()); }
public sealed override void VisitTypeParameter(ITypeParameterSymbol symbol) { // Using an if, rather than assert condition, to make debugging easier. if (!_acceptableTypeParameters.Contains(symbol)) { Debug.Assert(false, string.Format("Unexpected type parameter {0} owned by {1}", symbol, symbol.ContainingSymbol)); } foreach (var constraintType in symbol.ConstraintTypes) { Visit(constraintType); } }
private SDTypeParameter GetTypeParameter(ITypeParameterSymbol typeParameter) { var sdTypeParameter = new SDTypeParameter { Name = typeParameter.Name, HasDefaultConstructorConstraint = typeParameter.HasConstructorConstraint, HasReferenceTypeConstraint = typeParameter.HasReferenceTypeConstraint, HasValueTypeConstraint = typeParameter.HasValueTypeConstraint }; foreach (var constraintType in typeParameter.ConstraintTypes) { sdTypeParameter.ConstraintTypes.Add(_typeRefParser.GetParsedTypeReference(constraintType)); } return sdTypeParameter; }
public override void VisitTypeParameter(ITypeParameterSymbol symbol) { if (_visited.Add(symbol)) { if (symbol.TypeParameterKind == TypeParameterKind.Method || !_onlyMethodTypeParameters) { if (!_typeParameters.Contains(symbol)) { _typeParameters.Add(symbol); } } foreach (var constraint in symbol.ConstraintTypes) { constraint.Accept(this); } } }
private static void AddConstraintClauses( List<TypeParameterConstraintClauseSyntax> clauses, ITypeParameterSymbol typeParameter) { var constraints = new List<TypeParameterConstraintSyntax>(); if (typeParameter.HasReferenceTypeConstraint) { constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint)); } else if (typeParameter.HasValueTypeConstraint) { constraints.Add(SyntaxFactory.ClassOrStructConstraint(SyntaxKind.StructConstraint)); } var constraintTypes = typeParameter.ConstraintTypes.Where(t => t.TypeKind == TypeKind.Class).Concat( typeParameter.ConstraintTypes.Where(t => t.TypeKind == TypeKind.Interface).Concat( typeParameter.ConstraintTypes.Where(t => t.TypeKind != TypeKind.Class && t.TypeKind != TypeKind.Interface))); foreach (var type in constraintTypes) { if (type.SpecialType != SpecialType.System_Object) { constraints.Add(SyntaxFactory.TypeConstraint(type.GenerateTypeSyntax())); } } if (typeParameter.HasConstructorConstraint) { constraints.Add(SyntaxFactory.ConstructorConstraint()); } if (constraints.Count == 0) { return; } clauses.Add(SyntaxFactory.TypeParameterConstraintClause( typeParameter.Name.ToIdentifierName(), SyntaxFactory.SeparatedList(constraints))); }
public override void VisitTypeParameter(ITypeParameterSymbol symbol) { Append(symbol.Name); }
private static bool CanTypeParameterBeVariant( ITypeParameterSymbol parameter, VarianceKind variance, INamedTypeSymbol namedType, bool requireOutputSafety, bool requireInputSafety, ISymbol context) { switch (namedType.TypeKind) { case TypeKind.Class: case TypeKind.Struct: case TypeKind.Enum: case TypeKind.Interface: case TypeKind.Delegate: case TypeKind.Error: break; default: return true; } var currentNamedType = namedType; while (currentNamedType != null) { for (int i = 0; i < currentNamedType.Arity; i++) { var typeParam = currentNamedType.TypeParameters[i]; var typeArg = currentNamedType.TypeArguments[i]; if (!typeArg.Equals(parameter)) { return false; } var requireOut = false; var requireIn = false; switch (typeParam.Variance) { case VarianceKind.Out: requireOut = requireOutputSafety; requireIn = requireInputSafety; break; case VarianceKind.In: requireOut = requireInputSafety; requireIn = requireOutputSafety; break; case VarianceKind.None: requireIn = true; requireOut = true; break; default: throw new NotSupportedException(); } if (!CanTypeParameterBeVariant(parameter, variance, typeArg, requireOut, requireIn, context)) { return false; } } currentNamedType = currentNamedType.ContainingType; } return true; }
private static bool CheckTypeParameterContraintsInSymbol(ITypeParameterSymbol typeParameter, VarianceKind variance, ISymbol context) { foreach (ITypeSymbol constraintType in typeParameter.ConstraintTypes) { var canBe = CanTypeParameterBeVariant( typeParameter, variance, constraintType, false, true, context); if (!canBe) { return false; } } return true; }
private static bool CheckTypeParameterInEvent(ITypeParameterSymbol typeParameter, VarianceKind variance, IEventSymbol @event) { return CanTypeParameterBeVariant( typeParameter, variance, @event.Type, false, true, @event); }
private static void ReportIssue(ITypeParameterSymbol typeParameter, VarianceKind variance, SyntaxNodeAnalysisContext context) { if (!typeParameter.DeclaringSyntaxReferences.Any()) { return; } var location = typeParameter.DeclaringSyntaxReferences.First().GetSyntax().GetLocation(); if (variance == VarianceKind.In) { context.ReportDiagnostic(Diagnostic.Create(Rule, location, "in", typeParameter.Name, "contravariant")); return; } if (variance == VarianceKind.Out) { context.ReportDiagnostic(Diagnostic.Create(Rule, location, "out", typeParameter.Name, "covariant")); return; } }
private static bool CheckTypeParameter(ITypeParameterSymbol typeParameter, VarianceKind variance, INamedTypeSymbol delegateType, ITypeSymbol returnType, ImmutableArray<IParameterSymbol> parameters) { var canBe = CheckTypeParameterContraintsInSymbol(typeParameter, variance, delegateType); if (!canBe) { return false; } canBe = CanTypeParameterBeVariant(typeParameter, variance, returnType, true, false, delegateType); if (!canBe) { return false; } canBe = CheckTypeParameterInParameters(typeParameter, variance, parameters, delegateType); return canBe; }
private void AddTypeParameterVarianceIfRequired(ITypeParameterSymbol symbol) { if (format.GenericsOptions.IncludesOption(SymbolDisplayGenericsOptions.IncludeVariance)) { switch (symbol.Variance) { case VarianceKind.In: AddKeyword(SyntaxKind.InKeyword); AddSpace(); break; case VarianceKind.Out: AddKeyword(SyntaxKind.OutKeyword); AddSpace(); break; } } }
internal static void CheckConstraints(ITypeParameterSymbol symbol, TypeParameterConstraintKind constraints, params string[] constraintTypes) { Assert.Equal(constraints, GetTypeParameterConstraints(symbol)); CheckISymbols(symbol.ConstraintTypes, constraintTypes); }
public ITypeSymbol GetTypeInferredDuringReduction(ITypeParameterSymbol reducedFromTypeParameter) { // This implementation feels incorrect, but it follows the pattern that other extension method related APIs are using! return(_symbol.GetTypeInferredDuringReduction(reducedFromTypeParameter)); }
public abstract ITypeSymbol GetTypeInferredDuringReduction(ITypeParameterSymbol reducedFromTypeParameter);
public virtual ITypeSymbol GetTypeInferredDuringReduction(ITypeParameterSymbol reducedFromTypeParameter) { throw new NotImplementedException(); }
private static bool TypeParameterHasConstraints(ITypeParameterSymbol typeParam) { return(!typeParam.ConstraintTypes.IsEmpty || typeParam.HasConstructorConstraint || typeParam.HasReferenceTypeConstraint || typeParam.HasValueTypeConstraint); }
public override void VisitTypeParameter(ITypeParameterSymbol typeParameterTypeSymbol) => _typeParameters.Add(typeParameterTypeSymbol);
public virtual void VisitTypeParameter(ITypeParameterSymbol symbol) { DefaultVisit(symbol); }
private IList<SymbolDisplayPart> GetSelectedDisplayParts( ITypeParameterSymbol typeParam, SemanticModel semanticModel, int position, CancellationToken cancellationToken) { var parts = new List<SymbolDisplayPart>(); if (TypeParameterHasConstraints(typeParam)) { parts.Add(Space()); parts.Add(Keyword(SyntaxKind.WhereKeyword)); parts.Add(Space()); parts.Add(new SymbolDisplayPart(SymbolDisplayPartKind.TypeParameterName, typeParam, typeParam.Name)); parts.Add(Space()); parts.Add(Punctuation(SyntaxKind.ColonToken)); parts.Add(Space()); bool needComma = false; // class/struct constraint must be first if (typeParam.HasReferenceTypeConstraint) { parts.Add(Keyword(SyntaxKind.ClassKeyword)); needComma = true; } else if (typeParam.HasValueTypeConstraint) { parts.Add(Keyword(SyntaxKind.StructKeyword)); needComma = true; } foreach (var baseType in typeParam.ConstraintTypes) { if (needComma) { parts.Add(Punctuation(SyntaxKind.CommaToken)); parts.Add(Space()); } parts.AddRange(baseType.ToMinimalDisplayParts(semanticModel, position)); needComma = true; } // ctor constraint must be last if (typeParam.HasConstructorConstraint) { if (needComma) { parts.Add(Punctuation(SyntaxKind.CommaToken)); parts.Add(Space()); } parts.Add(Keyword(SyntaxKind.NewKeyword)); parts.Add(Punctuation(SyntaxKind.OpenParenToken)); parts.Add(Punctuation(SyntaxKind.CloseParenToken)); } } return parts; }
public bool Equals(ITypeParameterSymbol x, ITypeParameterSymbol y) { return(x.Name.Equals(y.Name)); }
ITypeSymbol IMethodSymbol.GetTypeInferredDuringReduction(ITypeParameterSymbol reducedFromTypeParameter) { return(this.GetTypeInferredDuringReduction(reducedFromTypeParameter.EnsureCSharpSymbolOrNull <ITypeParameterSymbol, TypeParameterSymbol>("reducedFromTypeParameter"))); }
private static bool TypeParameterHasConstraints(ITypeParameterSymbol typeParam) { return !typeParam.ConstraintTypes.IsEmpty || typeParam.HasConstructorConstraint || typeParam.HasReferenceTypeConstraint || typeParam.HasValueTypeConstraint; }
private void AnalyzeInvocation(SyntaxNodeAnalysisContext context) { var invocation = context.Node as TInvocationExpressionSyntax; SemanticModel semanticModel = context.SemanticModel; ISymbol symbol = semanticModel.GetSymbolInfo(invocation, context.CancellationToken).Symbol; if (symbol == null || symbol.Kind != SymbolKind.Method || !symbol.Name.StartsWith("Register", StringComparison.Ordinal)) { return; } var method = (IMethodSymbol)symbol; NoteRegisterActionInvocation(method, invocation, semanticModel, context.CancellationToken); bool isRegisterSymbolAction = IsRegisterAction(RegisterSymbolActionName, method, _analysisContext, _compilationStartAnalysisContext); bool isRegisterSyntaxNodeAction = IsRegisterAction(RegisterSyntaxNodeActionName, method, _analysisContext, _compilationStartAnalysisContext, _codeBlockStartAnalysisContext); bool isRegisterCodeBlockStartAction = IsRegisterAction(RegisterCodeBlockStartActionName, method, _analysisContext, _compilationStartAnalysisContext); bool isRegisterOperationAction = IsRegisterAction(RegisterOperationActionName, method, _analysisContext, _compilationStartAnalysisContext, _operationBlockStartAnalysisContext); if (isRegisterSymbolAction || isRegisterSyntaxNodeAction || isRegisterOperationAction) { if (method.Parameters.Length == 2 && method.Parameters[1].IsParams) { IEnumerable <SyntaxNode> arguments = GetArgumentExpressions(invocation); if (arguments != null) { int argumentCount = arguments.Count(); if (argumentCount >= 1) { ITypeSymbol type = semanticModel.GetTypeInfo(arguments.First(), context.CancellationToken).ConvertedType; if (type == null || type.Name.Equals(nameof(Action), StringComparison.Ordinal)) { if (argumentCount == 1) { DiagnosticDescriptor rule; if (isRegisterSymbolAction) { rule = MissingSymbolKindArgumentRule; } else if (isRegisterOperationAction) { rule = MissingOperationKindArgumentRule; } else { rule = MissingSyntaxKindArgumentRule; } SyntaxNode invocationExpression = GetInvocationExpression(invocation); Diagnostic diagnostic = Diagnostic.Create(rule, invocationExpression.GetLocation()); context.ReportDiagnostic(diagnostic); } else if (isRegisterSymbolAction) { foreach (SyntaxNode argument in arguments.Skip(1)) { symbol = semanticModel.GetSymbolInfo(argument, context.CancellationToken).Symbol; if (symbol != null && symbol.Kind == SymbolKind.Field && _symbolKind.Equals(symbol.ContainingType) && !s_supportedSymbolKinds.Contains(symbol.Name)) { Diagnostic diagnostic = Diagnostic.Create(UnsupportedSymbolKindArgumentRule, argument.GetLocation(), symbol.Name); context.ReportDiagnostic(diagnostic); } } } } } } } } if (method.TypeParameters.Length > 0 && (isRegisterSyntaxNodeAction || isRegisterCodeBlockStartAction)) { ITypeSymbol typeArgument = null; if (method.TypeParameters.Length == 1) { if (method.TypeParameters[0].Name == TLanguageKindEnumName) { typeArgument = method.TypeArguments[0]; } } else { ITypeParameterSymbol typeParam = method.TypeParameters.SingleOrDefault(t => t.Name == TLanguageKindEnumName); if (typeParam != null) { int index = method.TypeParameters.IndexOf(typeParam); typeArgument = method.TypeArguments[index]; } } if (typeArgument != null && typeArgument.TypeKind != TypeKind.TypeParameter && typeArgument.TypeKind != TypeKind.Error && !IsSyntaxKind(typeArgument)) { Location location = typeArgument.Locations[0]; if (!location.IsInSource) { SyntaxNode invocationExpression = GetInvocationExpression(invocation); location = invocationExpression.GetLocation(); } Diagnostic diagnostic = Diagnostic.Create(InvalidSyntaxKindTypeArgumentRule, location, typeArgument.Name, TLanguageKindEnumName, method.Name); context.ReportDiagnostic(diagnostic); } } }
private static bool CheckTypeParameter(ITypeParameterSymbol typeParameter, VarianceKind variance, INamedTypeSymbol interfaceType) { if (typeParameter.Variance != VarianceKind.None) { return false; } foreach (INamedTypeSymbol baseInterface in interfaceType.AllInterfaces) { var canBeVariant = CanTypeParameterBeVariant( typeParameter, variance, baseInterface, true, false, baseInterface); if (!canBeVariant) { return false; } } foreach (ISymbol member in interfaceType.GetMembers()) { var canBeVariant = false; switch (member.Kind) { case SymbolKind.Method: canBeVariant = CheckTypeParameterInMethod(typeParameter, variance, (IMethodSymbol)member); if (!canBeVariant) { return false; } break; case SymbolKind.Event: canBeVariant = CheckTypeParameterInEvent(typeParameter, variance, (IEventSymbol)member); if (!canBeVariant) { return false; } break; default: break; } } return true; }
private static KeyValuePair <string, string> CreateTypeConstraint(ITypeParameterSymbol typeParameter) { return(new KeyValuePair <string, string>(typeParameter.Name, typeParameter.ToDisplayString())); }
private static bool CheckTypeParameterInMethod(ITypeParameterSymbol typeParameter, VarianceKind variance, IMethodSymbol method) { var canBe = CheckTypeParameterContraintsInSymbol(typeParameter, variance, method); if (!canBe) { return false; } canBe = CanTypeParameterBeVariant( typeParameter, variance, method.ReturnType, true, false, method); if (!canBe) { return false; } return CheckTypeParameterInParameters(typeParameter, variance, method.Parameters, method); }
private bool HaveSameConstraints(ITypeParameterSymbol typeParameter1, ITypeParameterSymbol typeParameter2) { if (typeParameter1.HasConstructorConstraint != typeParameter2.HasConstructorConstraint || typeParameter1.HasReferenceTypeConstraint != typeParameter2.HasReferenceTypeConstraint || typeParameter1.HasValueTypeConstraint != typeParameter2.HasValueTypeConstraint) { return false; } if (typeParameter1.ConstraintTypes.Length != typeParameter2.ConstraintTypes.Length) { return false; } return typeParameter1.ConstraintTypes.SetEquals( typeParameter2.ConstraintTypes, this.SignatureTypeEquivalenceComparer); }
private static bool CheckTypeParameterInParameters(ITypeParameterSymbol typeParameter, VarianceKind variance, ImmutableArray<IParameterSymbol> parameters, ISymbol context) { foreach (IParameterSymbol param in parameters) { var canBe = CanTypeParameterBeVariant( typeParameter, variance, param.Type, param.RefKind != RefKind.None, true, context); if (!canBe) { return false; } } return true; }
private static SyntaxToken GetVarianceToken(ITypeParameterSymbol t) { if (t.Variance == VarianceKind.In) return SyntaxFactory.Token(SyntaxKind.InKeyword); if (t.Variance == VarianceKind.Out) return SyntaxFactory.Token(SyntaxKind.OutKeyword); return new SyntaxToken(); }
private static bool CanTypeParameterBeVariant( ITypeParameterSymbol parameter, VarianceKind variance, ITypeSymbol type, bool requireOutputSafety, bool requireInputSafety, ISymbol context) { switch (type.Kind) { case SymbolKind.TypeParameter: var typeParam = (ITypeParameterSymbol)type; if (!typeParam.Equals(parameter)) { return true; } return !((requireInputSafety && requireOutputSafety && variance != VarianceKind.None) || (requireOutputSafety && variance == VarianceKind.In) || (requireInputSafety && variance == VarianceKind.Out)); case SymbolKind.ArrayType: return CanTypeParameterBeVariant(parameter, variance, ((IArrayTypeSymbol)type).ElementType, requireOutputSafety, requireInputSafety, context); case SymbolKind.ErrorType: case SymbolKind.NamedType: return CanTypeParameterBeVariant(parameter, variance, (INamedTypeSymbol)type, requireOutputSafety, requireInputSafety, context); default: return true; } }
public int GetHashCode(ITypeParameterSymbol obj) { return(obj.Name.GetHashCode()); }
private SignatureHelpSymbolParameter Convert( ITypeParameterSymbol parameter, SemanticModel semanticModel, int position, IDocumentationCommentFormattingService formatter, CancellationToken cancellationToken) { return new SignatureHelpSymbolParameter( parameter.Name, isOptional: false, documentationFactory: parameter.GetDocumentationPartsFactory(semanticModel, position, formatter), displayParts: parameter.ToMinimalDisplayParts(semanticModel, position, s_minimallyQualifiedFormat), selectedDisplayParts: GetSelectedDisplayParts(parameter, semanticModel, position, cancellationToken)); }
public override ITypeSymbol VisitTypeParameter(ITypeParameterSymbol symbol) { return(symbol); }
private static IEnumerable<TypeParameterConstraintSyntax> GetTypeParameterConstraint(ITypeParameterSymbol symbol) { if (symbol.HasReferenceTypeConstraint) { yield return SyntaxFactory.ClassOrStructConstraint(SyntaxKind.ClassConstraint); } if (symbol.HasValueTypeConstraint) { yield return SyntaxFactory.ClassOrStructConstraint(SyntaxKind.StructConstraint); } if (symbol.ConstraintTypes.Length > 0) { for (int i = 0; i < symbol.ConstraintTypes.Length; i++) { yield return SyntaxFactory.TypeConstraint(GetTypeSyntax(symbol.ConstraintTypes[i])); } } if (symbol.HasConstructorConstraint) { yield return SyntaxFactory.ConstructorConstraint(); } }
public override void VisitTypeParameter(ITypeParameterSymbol symbol) { if (this.isFirstSymbolVisited) { AddTypeParameterVarianceIfRequired(symbol); } //variance and constraints are handled by methods and named types builder.Add(CreatePart(SymbolDisplayPartKind.TypeParameterName, symbol, symbol.Name)); }
private void AddDescriptionForTypeParameter(ITypeParameterSymbol symbol) { Contract.ThrowIfTrue(symbol.TypeParameterKind == TypeParameterKind.Cref); AddToGroup(SymbolDescriptionGroups.MainDescription, ToMinimalDisplayParts(symbol), Space(), PlainText(FeaturesResources.in_), Space(), ToMinimalDisplayParts(symbol.ContainingSymbol, s_typeParameterOwnerFormat)); }
public override void VisitTypeParameter(ITypeParameterSymbol symbol) { if (_finished || _symbolPredicate == null || _symbolPredicate(symbol)) { AddDocumentForMember(symbol, false, new MetadataItems { new MetadataItem(CodeAnalysisKeys.SpecificKind, (k, m) => symbol.TypeParameterKind.ToString()), new MetadataItem(CodeAnalysisKeys.DeclaringType, DocumentFor(symbol.DeclaringType)) }); } }
private static IEnumerable <INamedTypeSymbol> GetConstraintTypes(ITypeParameterSymbol typeParameter) => typeParameter.ConstraintTypes.OfType <INamedTypeSymbol>().SelectMany(ExpandGenericTypes);
private bool DoesTypeReferenceTypeParameter(ITypeSymbol type, ITypeParameterSymbol typeParameter, HashSet<ITypeSymbol> checkedTypes) { if (!checkedTypes.Add(type)) { return false; } if (type == typeParameter || type.GetTypeArguments().Any(t => DoesTypeReferenceTypeParameter(t, typeParameter, checkedTypes))) { return true; } if (type.ContainingType != null && type.Kind != SymbolKind.TypeParameter && DoesTypeReferenceTypeParameter(type.ContainingType, typeParameter, checkedTypes)) { return true; } return false; }
private bool DoesMemberReferenceTypeParameter(ISymbol member, ITypeParameterSymbol typeParameter, HashSet<ITypeSymbol> checkedTypes) { switch (member.Kind) { case SymbolKind.Event: var @event = member as IEventSymbol; return DoesTypeReferenceTypeParameter(@event.Type, typeParameter, checkedTypes); case SymbolKind.Method: var method = member as IMethodSymbol; return method.Parameters.Any(t => DoesTypeReferenceTypeParameter(t.Type, typeParameter, checkedTypes)) || method.TypeParameters.Any(t => t.ConstraintTypes.Any(c => DoesTypeReferenceTypeParameter(c, typeParameter, checkedTypes))) || DoesTypeReferenceTypeParameter(method.ReturnType, typeParameter, checkedTypes); case SymbolKind.Property: var property = member as IPropertySymbol; return property.Parameters.Any(t => DoesTypeReferenceTypeParameter(t.Type, typeParameter, checkedTypes)) || DoesTypeReferenceTypeParameter(property.Type, typeParameter, checkedTypes); default: Debug.Assert(false, FeaturesResources.UnexpectedInterfaceMemberKind, member.Kind.ToString()); return false; } }
public RoslynTypeParameterMetadata(ITypeParameterSymbol symbol) { this.symbol = symbol; }
public override ITypeSymbol GetTypeInferredDuringReduction(ITypeParameterSymbol reducedFromTypeParameter) { throw new InvalidOperationException(); }