internal static ShouldSerialize?InferDefaultShouldSerialize( AttributedMembers attrMembers, SerializerTypes types, ITypeSymbol declaringType, string propertyName, ImmutableArray <AttributeSyntax> attrs, Location?location, ref ImmutableArray <Diagnostic> diags ) { var ignoredDiags = ImmutableArray <Diagnostic> .Empty; var shouldSerializeType = Utils.GetTypeConstantWithName(attrMembers, attrs, "ShouldSerializeType", ref ignoredDiags); var shouldSerializeMethod = Utils.GetConstantsWithName <string>(attrMembers, attrs, "ShouldSerializeMethodName", ref ignoredDiags); // if anything _tried_ to set a non-default, don't include any defaults if (!shouldSerializeType.IsEmpty || !shouldSerializeMethod.IsEmpty) { return(null); } foreach (var candidate in declaringType.GetMembers("ShouldSerialize" + propertyName)) { var mtd = candidate as IMethodSymbol; if (mtd == null) { continue; } if (!mtd.ReturnType.Equals(types.BuiltIn.Bool, SymbolEqualityComparer.Default)) { continue; } bool isStatic, takesContext; var ps = mtd.Parameters; if (mtd.IsStatic) { isStatic = true; if (ps.Length == 0) { takesContext = false; } else if (ps.Length == 1) { takesContext = ps[0].IsInWriteContext(types.OurTypes); if (!takesContext && !ps[0].Type.Equals(declaringType, SymbolEqualityComparer.Default)) { var diag = Diagnostics.BadShouldSerializeParameters_StaticOne(location, mtd, ps[0].Type); diags = diags.Add(diag); return(null); } } else { continue; } } else { isStatic = false; takesContext = false; if (ps.Length != 0) { continue; } } return(new ShouldSerialize(mtd, isStatic, false, takesContext)); } return(null); }
private static ShouldSerialize?GetShouldSerialize( Compilation compilation, AttributedMembers attrMembers, SerializerTypes types, INamedTypeSymbol rowType, Location?location, ImmutableArray <AttributeSyntax> attrs, ref ImmutableArray <Diagnostic> diags ) { var shouldSerialize = Utils.GetMethodFromAttribute( attrMembers, "ShouldSerializeType", Diagnostics.ShouldSerializeTypeSpecifiedMultipleTimes, "ShouldSerializeMethodName", Diagnostics.ShouldSerializeMethodNameSpecifiedMultipleTimes, Diagnostics.ShouldSerializeBothMustBeSet, location, attrs, ref diags ); if (shouldSerialize == null) { return(null); } var(type, mtd) = shouldSerialize.Value; var shouldSerializeMtd = Utils.GetMethod(type, mtd, location, ref diags); if (shouldSerializeMtd == null) { return(null); } if (shouldSerializeMtd.IsGenericMethod) { var diag = Diagnostics.MethodCannotBeGeneric(location, shouldSerializeMtd); diags = diags.Add(diag); return(null); } var accessible = shouldSerializeMtd.IsAccessible(attrMembers); if (!accessible) { var diag = Diagnostics.MethodNotPublicOrInternal(location, shouldSerializeMtd); diags = diags.Add(diag); return(null); } if (!shouldSerializeMtd.ReturnType.Equals(types.BuiltIn.Bool, SymbolEqualityComparer.Default)) { var diag = Diagnostics.MethodMustReturnBool(location, shouldSerializeMtd); diags = diags.Add(diag); return(null); } var shouldSerializeParams = shouldSerializeMtd.Parameters; var isStatic = shouldSerializeMtd.IsStatic; bool takesContext; bool takesRow; if (isStatic) { // can legally take // 1. no parameters // 2. one parameter, a type which declares the annotated member // 3. one parameter, in WriteContext // 4. two parameters // 1. the type which declares the annotated member, or one which can be assigned to it, and // 2. in WriteContext if (shouldSerializeParams.Length == 0) { // fine! takesContext = false; takesRow = false; } else if (shouldSerializeParams.Length == 1) { var p0 = shouldSerializeParams[0]; if (p0.IsInWriteContext(types.OurTypes)) { takesRow = false; takesContext = true; } else { if (!p0.IsNormalParameterOfType(compilation, rowType)) { var diag = Diagnostics.BadShouldSerializeParameters_StaticOne(location, shouldSerializeMtd, rowType); diags = diags.Add(diag); return(null); } takesRow = true; takesContext = false; } } else if (shouldSerializeParams.Length == 2) { var p0 = shouldSerializeParams[0]; if (!p0.IsNormalParameterOfType(compilation, rowType)) { var diag = Diagnostics.BadShouldSerializeParameters_StaticTwo(location, shouldSerializeMtd, rowType); diags = diags.Add(diag); return(null); } var p1 = shouldSerializeParams[1]; if (!p1.IsInWriteContext(types.OurTypes)) { var diag = Diagnostics.BadShouldSerializeParameters_StaticTwo(location, shouldSerializeMtd, rowType); diags = diags.Add(diag); return(null); } takesRow = true; takesContext = true; } else { var diag = Diagnostics.BadShouldSerializeParameters_TooMany(location, shouldSerializeMtd); diags = diags.Add(diag); return(null); } } else { // can legally take // 1. no parameters // 2. one in WriteContext parameter var onType = shouldSerializeMtd.ContainingType; if (!onType.Equals(rowType, SymbolEqualityComparer.Default)) { var diag = Diagnostics.ShouldSerializeInstanceOnWrongType(location, shouldSerializeMtd, rowType); diags = diags.Add(diag); return(null); } takesRow = true; if (shouldSerializeParams.Length == 0) { takesContext = false; } else if (shouldSerializeParams.Length == 1) { var p0 = shouldSerializeParams[0]; if (!p0.IsInWriteContext(types.OurTypes)) { var diag = Diagnostics.BadShouldSerializeParameters_InstanceOne(location, shouldSerializeMtd); diags = diags.Add(diag); return(null); } takesContext = true; } else { var diag = Diagnostics.BadShouldSerializeParameters_TooMany(location, shouldSerializeMtd); diags = diags.Add(diag); return(null); } } return(new ShouldSerialize(shouldSerializeMtd, isStatic, takesRow, takesContext)); }