示例#1
0
        private static Getter?GetGetterForMethod(
            Compilation compilation,
            SerializerTypes types,
            ITypeSymbol rowType,
            IMethodSymbol method,
            Location?location,
            ref ImmutableArray <Diagnostic> diags
            )
        {
            if (method.MethodKind != MethodKind.Ordinary)
            {
                var diag = Diagnostics.MethodMustBeOrdinary(location, method);
                diags = diags.Add(diag);

                return(null);
            }

            var methodReturnType = method.ReturnType;

            if (methodReturnType.SpecialType == SpecialType.System_Void)
            {
                var diag = Diagnostics.MethodMustReturnNonVoid(location, method);
                diags = diags.Add(diag);

                return(null);
            }

            if (method.IsGenericMethod)
            {
                var diag = Diagnostics.MethodCannotBeGeneric(location, method);
                diags = diags.Add(diag);

                return(null);
            }

            bool takesContext;
            bool takesRow;

            if (method.IsStatic)
            {
                // 0 parameters are allow
                // if there is 1 parameter, it may be an `in WriteContext` or the row type
                // if there are 2 parameters, the first must be the row type, and the second must be `in WriteContext`

                var ps = method.Parameters;
                if (ps.Length == 0)
                {
                    takesContext = false;
                    takesRow     = false;
                }
                else if (ps.Length == 1)
                {
                    var p0 = ps[0];
                    if (p0.IsInWriteContext(types.OurTypes))
                    {
                        takesContext = true;
                        takesRow     = false;
                    }
                    else if (p0.IsNormalParameterOfType(compilation, rowType))
                    {
                        takesContext = false;
                        takesRow     = true;
                    }
                    else
                    {
                        var diag = Diagnostics.BadGetterParameters_StaticOne(location, method, rowType);
                        diags = diags.Add(diag);

                        return(null);
                    }
                }
                else if (ps.Length == 2)
                {
                    var p0 = ps[0];
                    if (!p0.IsNormalParameterOfType(compilation, rowType))
                    {
                        var diag = Diagnostics.BadGetterParameters_StaticTwo(location, method, rowType);
                        diags = diags.Add(diag);

                        return(null);
                    }

                    var p1 = ps[1];
                    if (!p1.IsInWriteContext(types.OurTypes))
                    {
                        var diag = Diagnostics.BadGetterParameters_StaticTwo(location, method, rowType);
                        diags = diags.Add(diag);

                        return(null);
                    }

                    takesContext = true;
                    takesRow     = true;
                }
                else
                {
                    var diag = Diagnostics.BadGetterParameters_TooMany(location, method);
                    diags = diags.Add(diag);

                    return(null);
                }
            }
            else
            {
                // 0 is allowed
                // if it takes a parameter, it must be an `in WriteContext`

                takesRow = false;

                var ps = method.Parameters;

                if (ps.Length == 0)
                {
                    takesContext = false;
                }
                else if (ps.Length == 1)
                {
                    var p0 = ps[0];
                    if (!p0.IsInWriteContext(types.OurTypes))
                    {
                        var diag = Diagnostics.BadGetterParameters_InstanceOne(location, method);
                        diags = diags.Add(diag);

                        return(null);
                    }

                    takesContext = true;
                }
                else
                {
                    var diag = Diagnostics.BadGetterParameters_TooMany(location, method);
                    diags = diags.Add(diag);

                    return(null);
                }
            }

            return(new Getter(method, takesRow, takesContext));
        }