public TypeWithAnnotations ToTypeWithAnnotations( CSharpCompilation compilation, bool asAnnotatedType = false ) { if (Type?.IsTypeParameterDisallowingAnnotationInCSharp8() == true) { var type = TypeWithAnnotations.Create(Type, NullableAnnotation.NotAnnotated); return(State == NullableFlowState.MaybeDefault ? type.SetIsAnnotated(compilation) : type); } NullableAnnotation annotation = asAnnotatedType ? ( Type?.IsValueType == true ? NullableAnnotation.NotAnnotated : NullableAnnotation.Annotated ) : ( State.IsNotNull() || Type?.CanContainNull() == false ? NullableAnnotation.NotAnnotated : NullableAnnotation.Annotated ); return(TypeWithAnnotations.Create(this.Type, annotation)); }
/// <summary> /// Return the nullable annotation to use when two annotations are expected to be "compatible", which means /// they could be the same. These are the "invariant" merging rules. (NotAnnotated wins over Annotated which wins over Oblivious) /// </summary> public static NullableAnnotation EnsureCompatible(this NullableAnnotation a, NullableAnnotation b) => (a, b) switch { (NullableAnnotation.Oblivious, _) => b, (_, NullableAnnotation.Oblivious) => a, _ => a < b ? a : b, };
public CodeGenerationTypeParameterSymbol( INamedTypeSymbol containingType, ImmutableArray <AttributeData> attributes, VarianceKind varianceKind, string name, NullableAnnotation nullableAnnotation, ImmutableArray <ITypeSymbol> constraintTypes, bool hasConstructorConstraint, bool hasReferenceConstraint, bool hasValueConstraint, bool hasUnmanagedConstraint, bool hasNotNullConstraint, int ordinal ) : base( containingType?.ContainingAssembly, containingType, attributes, Accessibility.NotApplicable, default, name, SpecialType.None, nullableAnnotation ) { this.Variance = varianceKind; this.ConstraintTypes = constraintTypes; this.Ordinal = ordinal; this.HasConstructorConstraint = hasConstructorConstraint; this.HasReferenceTypeConstraint = hasReferenceConstraint; this.HasValueTypeConstraint = hasValueConstraint; this.HasUnmanagedTypeConstraint = hasUnmanagedConstraint; this.HasNotNullConstraint = hasNotNullConstraint; }
public CodeGenerationNamedTypeSymbol( IAssemblySymbol containingAssembly, INamedTypeSymbol containingType, ImmutableArray <AttributeData> attributes, Accessibility declaredAccessibility, DeclarationModifiers modifiers, TypeKind typeKind, string name, ImmutableArray <ITypeParameterSymbol> typeParameters, INamedTypeSymbol baseType, ImmutableArray <INamedTypeSymbol> interfaces, SpecialType specialType, NullableAnnotation nullableAnnotation, ImmutableArray <ISymbol> members, ImmutableArray <CodeGenerationAbstractNamedTypeSymbol> typeMembers, INamedTypeSymbol enumUnderlyingType) : base(containingAssembly, containingType, attributes, declaredAccessibility, modifiers, name, specialType, nullableAnnotation, typeMembers) { _typeKind = typeKind; _typeParameters = typeParameters.NullToEmpty(); _baseType = baseType; _interfaces = interfaces.NullToEmpty(); _members = members.NullToEmpty(); _enumUnderlyingType = enumUnderlyingType; this.OriginalDefinition = this; }
public TypeWithAnnotations ToAnnotatedTypeWithAnnotations() { NullableAnnotation annotation = Type?.IsTypeParameterDisallowingAnnotation() == true ? NullableAnnotation.NotAnnotated : NullableAnnotation.Annotated; return(TypeWithAnnotations.Create(this.Type, annotation)); }
public TypeReference MakeNullable(NullableAnnotation value = NullableAnnotation.Nullable) { var type = Clone(); type.Nullable = value; return(type); }
protected TypeSymbolWithNullableAnnotation(ITypeSymbol wrappedSymbol, NullableAnnotation nullability) { Debug.Assert(!(wrappedSymbol is TypeSymbolWithNullableAnnotation)); WrappedSymbol = wrappedSymbol; Nullability = nullability; }
protected override CodeGenerationTypeSymbol CloneWithNullableAnnotation(NullableAnnotation nullableAnnotation) { return(new CodeGenerationTypeParameterSymbol( this.ContainingType, this.GetAttributes(), this.Variance, this.Name, nullableAnnotation, this.ConstraintTypes, this.HasConstructorConstraint, this.HasReferenceTypeConstraint, this.HasValueTypeConstraint, this.HasUnmanagedTypeConstraint, this.HasNotNullConstraint, this.Ordinal)); }
protected override CodeGenerationTypeSymbol CloneWithNullableAnnotation( NullableAnnotation nullableAnnotation ) { // We ignore the nullableAnnotation parameter because pointer types can't be nullable. return(new CodeGenerationPointerTypeSymbol(this.PointedAtType)); }
public TypeWithAnnotations ToTypeWithAnnotations() { NullableAnnotation annotation = this.State.IsNotNull() || Type?.CanContainNull() == false || Type?.IsTypeParameterDisallowingAnnotation() == true ? NullableAnnotation.NotAnnotated : NullableAnnotation.Annotated; return(TypeWithAnnotations.Create(this.Type, annotation)); }
public static T WithNullability <T>(this T typeSymbol, NullableAnnotation nullability) where T : class, ITypeSymbol { if (typeSymbol == null) { return(null); } if (typeSymbol is TypeSymbolWithNullableAnnotation typeSymbolWithNullability) { // Check if the wrapper already has the same top-level nullability; in that case we don't need to re-create one. if (typeSymbolWithNullability.Nullability == nullability) { return(typeSymbol); } // No reason to wrap a wrapper, so unwrap it typeSymbol = (T)typeSymbolWithNullability.WrappedSymbol; } return(typeSymbol switch { IArrayTypeSymbol arrayTypeSymbol => (ITypeSymbol) new ArrayTypeSymbolWithNullableAnnotation(arrayTypeSymbol, nullability), IDynamicTypeSymbol dynamicTypeSymbol => (ITypeSymbol) new DynamicTypeSymbolWithNullableAnnotation(dynamicTypeSymbol, nullability), INamedTypeSymbol namedTypeSymbol => (ITypeSymbol) new NamedTypeSymbolWithNullableAnnotation(namedTypeSymbol, nullability), IPointerTypeSymbol pointerType => (ITypeSymbol) new PointerTypeSymbolWithNullableAnnotation(pointerType, nullability), ITypeParameterSymbol typeParameterSymbol => (ITypeSymbol) new TypeParameterSymbolWithNullableAnnotation(typeParameterSymbol, nullability), _ => throw ExceptionUtilities.UnexpectedValue(typeSymbol) } as T);
protected override CodeGenerationTypeSymbol CloneWithNullableAnnotation(NullableAnnotation nullableAnnotation) { return(new CodeGenerationConstructedNamedTypeSymbol( (CodeGenerationNamedTypeSymbol)_constructedFrom.WithNullableAnnotation(nullableAnnotation), _typeArguments, this.TypeMembers)); }
protected CodeGenerationAbstractNamedTypeSymbol( IAssemblySymbol containingAssembly, INamedTypeSymbol containingType, ImmutableArray <AttributeData> attributes, Accessibility declaredAccessibility, DeclarationModifiers modifiers, string name, SpecialType specialType, NullableAnnotation nullableAnnotation, ImmutableArray <CodeGenerationAbstractNamedTypeSymbol> typeMembers ) : base( containingAssembly, containingType, attributes, declaredAccessibility, modifiers, name, specialType, nullableAnnotation ) { this.TypeMembers = typeMembers; foreach (var member in typeMembers) { member.ContainingType = this; } }
/// <summary> /// Join nullable annotations from the set of lower bounds for fixing a type parameter. /// This uses the covariant merging rules. /// </summary> public static NullableAnnotation Join(this NullableAnnotation a, NullableAnnotation b) { if (a.IsAnnotated() || b.IsAnnotated()) { return(NullableAnnotation.Annotated); } return((a < b) ? a : b); }
/// <summary> /// Meet two nullable annotations for computing the nullable annotation of a type parameter from upper bounds. /// This uses the contravariant merging rules. /// </summary> public static NullableAnnotation Meet(this NullableAnnotation a, NullableAnnotation b) { if (a.IsNotAnnotated() || b.IsNotAnnotated()) { return(NullableAnnotation.NotAnnotated); } return((a < b) ? a : b); }
public static ITypeSymbol WithNullableAnnotation(this ITypeSymbol type, NullableAnnotation nullableAnnotation) { #if CODE_STYLE // TODO: Remove this #if once 'WithNullableAnnotation' is available in CodeStyle layer. return(type); #else return(type.WithNullableAnnotation(nullableAnnotation)); #endif }
protected override CodeGenerationTypeSymbol CloneWithNullableAnnotation(NullableAnnotation nullableAnnotation) { return(new CodeGenerationNamedTypeSymbol( this.ContainingAssembly, this.ContainingType, this.GetAttributes(), this.DeclaredAccessibility, this.Modifiers, this.TypeKind, this.Name, _typeParameters, _baseType, _interfaces, this.SpecialType, nullableAnnotation, _members, this.TypeMembers, this.EnumUnderlyingType)); }
public ITypeSymbol WithNullableAnnotation(NullableAnnotation nullableAnnotation) { if (this.NullableAnnotation == nullableAnnotation) { return(this); } return(CloneWithNullableAnnotation(nullableAnnotation)); }
public BoundExpressionWithNullability( SyntaxNode syntax, BoundExpression expression, NullableAnnotation nullableAnnotation, TypeSymbol?type ) : this(syntax, expression, nullableAnnotation, type, hasErrors : false) { IsSuppressed = expression.IsSuppressed; }
protected void ExtractNullability(NullableAnnotation annotation) { var ta = annotation.GetTypeAnnotation(); if (ta != Kinds.TypeAnnotation.None) { Context.Emit(Tuples.type_annotation(this, ta)); } }
private TypeWithAnnotations(TypeSymbol defaultType, NullableAnnotation nullableAnnotation, Extensions extensions) { Debug.Assert(defaultType?.IsNullableType() != true || (nullableAnnotation != NullableAnnotation.Oblivious && nullableAnnotation != NullableAnnotation.NotAnnotated)); Debug.Assert(extensions != null); _defaultType = defaultType; NullableAnnotation = nullableAnnotation; _extensions = extensions; }
private TypeWithAnnotations(TypeSymbol defaultType, NullableAnnotation nullableAnnotation, Extensions extensions) { Debug.Assert(defaultType?.IsNullableType() != true || nullableAnnotation == NullableAnnotation.Annotated); Debug.Assert(extensions != null); DefaultType = defaultType; NullableAnnotation = nullableAnnotation; _extensions = extensions; }
protected void PopulateNullability(TextWriter trapFile, NullableAnnotation annotation) { var ta = annotation.GetTypeAnnotation(); if (ta != Kinds.TypeAnnotation.None) { trapFile.type_annotation(this, ta); } }
/// <summary> /// Merges nullability. /// </summary> public static NullableAnnotation MergeNullableAnnotation(this NullableAnnotation a, NullableAnnotation b, VarianceKind variance) { return(variance switch { VarianceKind.In => a.Meet(b), VarianceKind.Out => a.Join(b), VarianceKind.None => a.EnsureCompatible(b), _ => throw ExceptionUtilities.UnexpectedValue(variance) });
/// <summary> /// Set the fields of the builder. /// </summary> /// <remarks> /// This method guarantees: fields will be set once; exactly one caller is /// returned true; and IsNull will return true until all fields are initialized. /// This method does not guarantee that all fields will be set by the same /// caller. Instead, the expectation is that all callers will attempt to initialize /// the builder with equivalent TypeWithAnnotations instances where /// different fields of the builder may be assigned from different instances. /// </remarks> internal bool InterlockedInitialize(TypeWithAnnotations type) { if ((object)_defaultType != null) { return(false); } _nullableAnnotation = type.NullableAnnotation; Interlocked.CompareExchange(ref _extensions, type._extensions, null); return((object)Interlocked.CompareExchange(ref _defaultType, type._defaultType, null) == null); }
public static NullableAnnotation EnsureCompatible(this NullableAnnotation a, NullableAnnotation b) { Debug.Assert(a != NullableAnnotation.Ignored); Debug.Assert(b != NullableAnnotation.Ignored); return((a, b) switch { (NullableAnnotation.Oblivious, _) => b, (_, NullableAnnotation.Oblivious) => a, _ => a < b ? a : b, });
public QueryCategory( QueryFlags flags, ITypeSymbol?itemType, NullableAnnotation itemNullability, ListStrategy listStrategy, ITypeSymbol listType) { Flags = flags; ItemType = itemType; ItemNullability = itemNullability; ListStrategy = listStrategy; ListType = listType; }
/// <summary> /// Check that two nullable annotations are "compatible", which means they could be the same. Return the /// nullable annotation to be used as a result. This uses the invariant merging rules. /// </summary> public static NullableAnnotation EnsureCompatible(this NullableAnnotation a, NullableAnnotation b) { if (a.IsOblivious()) { return(b); } if (b.IsOblivious()) { return(a); } return((a < b) ? a : b); }
protected CodeGenerationTypeSymbol( INamedTypeSymbol containingType, ImmutableArray <AttributeData> attributes, Accessibility declaredAccessibility, DeclarationModifiers modifiers, string name, SpecialType specialType, NullableAnnotation nullableAnnotation) : base(containingType, attributes, declaredAccessibility, modifiers, name) { this.SpecialType = specialType; this.NullableAnnotation = nullableAnnotation; }
public static NullableAnnotation GetNullableAnnotation(ArrayBuilder <TypeSymbolWithAnnotations> types) { NullableAnnotation result = NullableAnnotation.NotAnnotated; foreach (var type in types) { Debug.Assert(!type.IsNull); Debug.Assert(type.Equals(types[0], TypeCompareKind.AllIgnoreOptions)); // This uses the covariant merging rules. result = result.JoinForFixingLowerBounds(type.AsSpeakable().NullableAnnotation); } return(result); }