public static void ReportCasePatternNotSupported(this SyntaxNodeAnalysisContext context, PatternSyntax pattern)
        {
            var diagnostic = Diagnostic.Create(
                ExhaustiveMatchAnalyzer.CasePatternNotSupported,
                pattern.GetLocation(), pattern.ToString());

            context.ReportDiagnostic(diagnostic);
        }
Esempio n. 2
0
        private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
        {
            var isVar = optionalType is null;

            if (!(designation is null) && cx.GetModel(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
            {
                var type = Entities.Type.Create(cx, symbol.GetAnnotatedType());
                VariableDeclaration.Create(cx, symbol, type, optionalType, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 1);
            }
Esempio n. 3
0
        public static Expression CreatePattern(this Context cx, PatternSyntax syntax, IExpressionParentEntity parent, int child)
        {
            switch (syntax)
            {
            case ConstantPatternSyntax constantPattern:
                return(Expression.Create(cx, constantPattern.Expression, parent, child));

            case DeclarationPatternSyntax declPattern:
                // Creates a single local variable declaration.
            {
                if (declPattern.Designation is VariableDesignationSyntax designation && cx.Model(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
                {
                    var type = Type.Create(cx, symbol.Type);

                    return(VariableDeclaration.Create(cx, symbol, type, cx.Create(syntax.GetLocation()), cx.Create(designation.GetLocation()), false, parent, child));
                }
                throw new InternalError(syntax, "Is pattern not handled");
            }

            case RecursivePatternSyntax recPattern:
                return(new RecursivePattern(cx, recPattern, parent, child));

            case VarPatternSyntax varPattern:
                switch (varPattern.Designation)
                {
                case ParenthesizedVariableDesignationSyntax parDesignation:
                    return(VariableDeclaration.CreateParenthesized(cx, varPattern, parDesignation, parent, child));

                case SingleVariableDesignationSyntax varDesignation:
                    if (cx.Model(syntax).GetDeclaredSymbol(varDesignation) is ILocalSymbol symbol)
                    {
                        var type = Type.Create(cx, symbol.Type);

                        return(VariableDeclaration.Create(cx, symbol, type, cx.Create(syntax.GetLocation()), cx.Create(varDesignation.GetLocation()), false, parent, child));
                    }
                    else
                    {
                        throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation.");
                    }

                default:
                    throw new InternalError("var pattern designation is unhandled");
                }

            case DiscardPatternSyntax dp:
                return(new Discard(cx, dp, parent, child));

            default:
                throw new InternalError(syntax, "Is pattern not handled");
            }
        }
Esempio n. 4
0
        private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
        {
            bool isVar = optionalType is null;

            if (!isVar)
            {
                Expressions.TypeAccess.Create(cx, optionalType, this, 1);
            }

            if (!(designation is null) && cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
            {
                var type = Type.Create(cx, symbol.Type);

                if (isVar)
                {
                    new Expression(new ExpressionInfo(cx, type, cx.Create(varKeyword.GetLocation()), ExprKind.TYPE_ACCESS, this, 1, false, null));
                }

                VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 2);
            }
        }
Esempio n. 5
0
        public static ITypeSymbol GetMatchedTypeSymbol(
            this PatternSyntax pattern,
            SyntaxNodeAnalysisContext context,
            ITypeSymbol type,
            HashSet <ITypeSymbol> allCases,
            bool isClosed)
        {
            ITypeSymbol symbolUsed;

            switch (pattern)
            {
            case DeclarationPatternSyntax declarationPattern:
                symbolUsed = context.GetDeclarationType(declarationPattern);
                break;

            case DiscardPatternSyntax _:
            case ConstantPatternSyntax constantPattern
                when constantPattern.IsNull():
                // Ignored
                return(null);

            case VarPatternSyntax _:
            case RecursivePatternSyntax _:
            default:
                context.ReportCasePatternNotSupported(pattern);
                return(null);
            }

            if (isClosed && !allCases.Contains(symbolUsed))
            {
                var diagnostic = Diagnostic.Create(ExhaustiveMatchAnalyzer.MatchMustBeOnCaseType,
                                                   pattern.GetLocation(), symbolUsed.GetFullName(), type.GetFullName());
                context.ReportDiagnostic(diagnostic);
            }

            return(symbolUsed);
        }
Esempio n. 6
0
        internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionParentEntity parent, int child)
        {
            switch (syntax)
            {
            case ParenthesizedPatternSyntax parenthesizedPattern:
                return(Pattern.Create(cx, parenthesizedPattern.Pattern, parent, child));

            case ConstantPatternSyntax constantPattern:
                return(Expression.Create(cx, constantPattern.Expression, parent, child));

            case TypePatternSyntax typePattern:
                return(Expressions.TypeAccess.Create(cx, typePattern.Type, parent, child));

            case UnaryPatternSyntax unaryPattern:
                return(new UnaryPattern(cx, unaryPattern, parent, child));

            case BinaryPatternSyntax binaryPattern:
                return(new BinaryPattern(cx, binaryPattern, parent, child));

            case DeclarationPatternSyntax declPattern:
                // Creates a single local variable declaration.
            {
                if (declPattern.Designation is VariableDesignationSyntax designation)
                {
                    if (cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
                    {
                        var type = symbol.GetAnnotatedType();
                        return(VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child));
                    }
                    if (designation is DiscardDesignationSyntax)
                    {
                        return(Expressions.TypeAccess.Create(cx, declPattern.Type, parent, child));
                    }
                    throw new InternalError(designation, "Designation pattern not handled");
                }
                throw new InternalError(declPattern, "Declaration pattern not handled");
            }

            case RecursivePatternSyntax recPattern:
                return(new RecursivePattern(cx, recPattern, parent, child));

            case RelationalPatternSyntax relPattern:
                return(new RelationalPattern(cx, relPattern, parent, child));

            case VarPatternSyntax varPattern:
                switch (varPattern.Designation)
                {
                case ParenthesizedVariableDesignationSyntax parDesignation:
                    return(VariableDeclaration.CreateParenthesized(cx, varPattern, parDesignation, parent, child));

                case SingleVariableDesignationSyntax varDesignation:
                    if (cx.GetModel(syntax).GetDeclaredSymbol(varDesignation) is ILocalSymbol symbol)
                    {
                        var type = symbol.GetAnnotatedType();

                        return(VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), true, parent, child));
                    }

                    throw new InternalError(varPattern, "Unable to get the declared symbol of the var pattern designation.");

                case DiscardDesignationSyntax discard:
                    return(new Expressions.Discard(cx, discard, parent, child));

                default:
                    throw new InternalError($"var pattern designation of type {varPattern.Designation.GetType()} is unhandled");
                }

            case DiscardPatternSyntax dp:
                return(new Discard(cx, dp, parent, child));

            default:
                throw new InternalError(syntax, "Pattern not handled");
            }
        }
Esempio n. 7
0
        private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, VariableDesignationSyntax designation)
        {
            var isVar = optionalType is null;

            switch (designation)
            {
            case SingleVariableDesignationSyntax _:
                if (cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
                {
                    var type = Type.Create(cx, symbol.GetAnnotatedType());
                    Expressions.VariableDeclaration.Create(cx, symbol, type, optionalType, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 0);
                }
                break;

            case DiscardDesignationSyntax discard:
                if (isVar)
                {
                    new Expressions.Discard(cx, discard, this, 0);
                }
                else
                {
                    Expressions.TypeAccess.Create(cx, optionalType, this, 0);
                }
                break;

            case null:
                break;

            case ParenthesizedVariableDesignationSyntax paren:
                Expressions.VariableDeclaration.CreateParenthesized(cx, (VarPatternSyntax)pattern, paren, this, 0);
                break;

            default:
                throw new InternalError(pattern, "Unhandled designation in case statement");
            }
        }
Esempio n. 8
0
        private void PopulatePattern(PatternSyntax pattern, TypeSyntax optionalType, SyntaxToken varKeyword, VariableDesignationSyntax designation)
        {
            var isVar = optionalType is null;

            if (!isVar)
            {
                Expressions.TypeAccess.Create(cx, optionalType, this, 1);
            }

            switch (designation)
            {
            case SingleVariableDesignationSyntax _:
                if (cx.Model(pattern).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
                {
                    var type = Type.Create(cx, symbol.Type);

                    if (isVar)
                    {
                        new Expression(new ExpressionInfo(cx, type, cx.Create(varKeyword.GetLocation()), ExprKind.TYPE_ACCESS, this, 1, false, null));
                    }

                    Expressions.VariableDeclaration.Create(cx, symbol, type, cx.Create(pattern.GetLocation()), cx.Create(designation.GetLocation()), isVar, this, 0);
                }
                break;

            case DiscardDesignationSyntax discard:
                new Expressions.Discard(cx, discard, this, 0);
                break;

            case null:
                break;

            case ParenthesizedVariableDesignationSyntax paren:
                Expressions.VariableDeclaration.CreateParenthesized(cx, (VarPatternSyntax)pattern, paren, this, 0);
                break;

            default:
                throw new InternalError(pattern, "Unhandled designation in case statement");
            }
        }