Пример #1
0
        public static IdentifierUsage GetUsage(SyntaxNode identifier)
        {
            if (identifier.Parent is MemberAccessExpressionSyntax memberAccess)
            {
                return(GetUsageForOperation(memberAccess.Parent));
            }

            if (identifier.Parent is ElementAccessExpressionSyntax elementAccess)
            {
                return(GetUsageForOperation(elementAccess.Parent));
            }

            IdentifierUsage GetUsageForOperation(SyntaxNode operationNode)
            {
                if (operationNode is AssignmentExpressionSyntax assignent)
                {
                    switch (assignent.Kind())
                    {
                    case SyntaxKind.SimpleAssignmentExpression:
                        return(IdentifierUsage.WrittenTo());

                    case SyntaxKind.AddAssignmentExpression:
                    case SyntaxKind.SubtractAssignmentExpression:
                        //TODO: add more assignment expressions
                        return(IdentifierUsage.ReadFromAndWrittenTo());
                    }
                }
                else if (operationNode is PostfixUnaryExpressionSyntax postfix)
                {
                    return(IdentifierUsage.ReadFromAndWrittenTo());
                }
                else if (operationNode is ArgumentSyntax argument)
                {
                    var kind = argument.RefKindKeyword.Kind();

                    if (kind == SyntaxKind.RefKeyword)
                    {
                        return(IdentifierUsage.ReadFromAndWrittenTo());
                    }
                    if (kind == SyntaxKind.OutKeyword)
                    {
                        return(IdentifierUsage.WrittenTo());
                    }
                }

                return(IdentifierUsage.ReadFrom());
            }

            return(GetUsageForOperation(identifier.Parent));
        }
Пример #2
0
 public static TResult Match <TResult>(this IdentifierUsage instance, Func <IdentifierUsage.ReadFromAndWrittenToCase, TResult> readFromAndWrittenToCaseCase, Func <IdentifierUsage.WrittenToCase, TResult> writtenToCaseCase, Func <IdentifierUsage.ReadFromCase, TResult> readFromCaseCase)
 {
     if (instance is IdentifierUsage.ReadFromAndWrittenToCase readFromAndWrittenToCase)
     {
         return(readFromAndWrittenToCaseCase(readFromAndWrittenToCase));
     }
     if (instance is IdentifierUsage.WrittenToCase writtenToCase)
     {
         return(writtenToCaseCase(writtenToCase));
     }
     if (instance is IdentifierUsage.ReadFromCase readFromCase)
     {
         return(readFromCaseCase(readFromCase));
     }
     throw new Exception("Invalid IdentifierUsage type");
 }
Пример #3
0
        public static void Match(this IdentifierUsage instance, Action <IdentifierUsage.ReadFromAndWrittenToCase> readFromAndWrittenToCaseCase, Action <IdentifierUsage.WrittenToCase> writtenToCaseCase, Action <IdentifierUsage.ReadFromCase> readFromCaseCase)
        {
            if (instance is IdentifierUsage.ReadFromAndWrittenToCase readFromAndWrittenToCase)
            {
                readFromAndWrittenToCaseCase(readFromAndWrittenToCase);
                return;
            }

            if (instance is IdentifierUsage.WrittenToCase writtenToCase)
            {
                writtenToCaseCase(writtenToCase);
                return;
            }

            if (instance is IdentifierUsage.ReadFromCase readFromCase)
            {
                readFromCaseCase(readFromCase);
                return;
            }

            throw new Exception("Invalid IdentifierUsage type");
        }