예제 #1
0
        protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression, SemanticModel semanticModel)
        {
            var variableNameExtractor = new VariableNameExtractor(semanticModel);

            expression.Accept(variableNameExtractor);

            var typeInfo = semanticModel.GetTypeInfo(variableNameExtractor.VariableIdentifierName);

            if (typeInfo.ConvertedType == null)
            {
                return(null);
            }
            if (!ShouldAnalyzeVariableType(typeInfo.ConvertedType))
            {
                return(null);
            }

            foreach (var visitor in Visitors)
            {
                expression.Accept(visitor);

                if (visitor.IsValid(expression))
                {
                    return(CreateDiagnostic(visitor, expression));
                }
            }
            return(null);
        }
예제 #2
0
        private void ProcessPrefixPostfixOperators(ExpressionSyntax operand, OpCode opCode, bool isPrefix)
        {
            Visit(operand);
            InjectRequiredConversions(operand);

            var assignmentVisitor = new AssignmentVisitor(Context, ilVar);

            var operandInfo = Context.SemanticModel.GetSymbolInfo(operand);

            if (operandInfo.Symbol != null && operandInfo.Symbol.Kind != SymbolKind.Field && operandInfo.Symbol.Kind != SymbolKind.Property) // Fields / Properties requires more complex handling to load the owning reference.
            {
                if (!isPrefix)                                                                                                               // For *postfix* operators we duplicate the value *before* applying the operator...
                {
                    AddCilInstruction(ilVar, OpCodes.Dup);
                }

                AddCilInstruction(ilVar, OpCodes.Ldc_I4_1);
                AddCilInstruction(ilVar, opCode);

                if (isPrefix) // For prefix operators we duplicate the value *after* applying the operator...
                {
                    AddCilInstruction(ilVar, OpCodes.Dup);
                }

                //assign (top of stack to the operand)
                assignmentVisitor.InstructionPrecedingValueToLoad = Context.CurrentLine;
                operand.Accept(assignmentVisitor);
                return;
            }

            var tempLocalName = MethodExtensions.LocalVariableNameFor("tmp_", "tmp_".UniqueId().ToString());

            AddCecilExpression($"var {tempLocalName} = new VariableDefinition({Context.TypeResolver.Resolve(Context.SemanticModel.GetTypeInfo(operand).Type)});");
            AddCecilExpression($"{Context.DefinitionVariables.GetLastOf(MemberKind.Method).VariableName}.Body.Variables.Add({tempLocalName});");

            if (isPrefix)
            {
                AddCilInstruction(ilVar, OpCodes.Ldc_I4_1);
                AddCilInstruction(ilVar, opCode);
            }

            AddCilInstruction(ilVar, OpCodes.Stloc, tempLocalName);
            AddCilInstruction(ilVar, OpCodes.Ldloc, tempLocalName);
            assignmentVisitor.InstructionPrecedingValueToLoad = Context.CurrentLine;
            AddCilInstruction(ilVar, OpCodes.Ldloc, tempLocalName);

            if (!isPrefix)
            {
                AddCilInstruction(ilVar, OpCodes.Ldc_I4_1);
                AddCilInstruction(ilVar, opCode);
            }

            // assign (top of stack to the operand)
            operand.Accept(assignmentVisitor);
        }
예제 #3
0
        public JsExpression VisitUnaryExpression(ExpressionSyntax node, ExpressionSyntax expression)
        {
            ExpressionType op;

            switch (node.CSharpKind())
            {
            case SyntaxKind.LogicalNotExpression:
                op = ExpressionType.Not;
                break;

            case SyntaxKind.UnaryMinusExpression:
                op = ExpressionType.Negate;
                break;

            case SyntaxKind.PostDecrementExpression:
            case SyntaxKind.PostIncrementExpression:
                throw new Exception("Expression trees cannot contain assignment operators.");

            default:
                throw new Exception("Unknown operation: " + node.CSharpKind());
            }

            var makeUnary    = GetExpressionMethod("MakeUnary", Context.Instance.ExpressionType, Context.Instance.Expression, Context.Instance.TypeType);
            var opExpression = idioms.GetEnumValue(Context.Instance.ExpressionType.GetMembers(op.ToString()).OfType <IFieldSymbol>().Single());
            var operand      = expression.Accept(this);
            var type         = model.GetTypeInfo(node).ConvertedType;

            return(idioms.InvokeStatic(
                       makeUnary,
                       opExpression,
                       operand,
                       idioms.TypeOf(type)));
        }
예제 #4
0
        protected ExpressionSyntax GetNewExpression(ExpressionSyntax expression, NodeReplacement replacement)
        {
            var visitor = new MemberAccessExpressionsCSharpSyntaxVisitor();

            expression.Accept(visitor);
            var members = new LinkedList <MemberAccessExpressionSyntax>(visitor.Members);

            var current = members.Last;

            while (current != null)
            {
                if (replacement.IsValidNode(current))
                {
                    // extract custom data into the replacement object
                    replacement.ExtractValues(current.Value);

                    var oldNode = replacement.ComputeOld(current);
                    var newNode = replacement.ComputeNew(current);
                    return(expression.ReplaceNode(oldNode, newNode));
                }
                current = current.Previous;
            }

            throw new System.InvalidOperationException("should not get here");
        }
            public override bool IsValid(ExpressionSyntax expression)
            {
                if (!base.IsValid(expression))
                {
                    return(false);
                }

                var visitor = new MemberAccessExpressionsCSharpSyntaxVisitor();

                expression.Accept(visitor);

                var containKey   = visitor.Members.Find(member => member.Name.Identifier.Text == "ContainKey");
                var containValue = visitor.Members.Find(member => member.Name.Identifier.Text == "ContainValue");

                return(containKey.Parent is InvocationExpressionSyntax keyInvocation &&
                       containValue.Parent is InvocationExpressionSyntax valueInvocation

                       && keyInvocation.ArgumentList.Arguments is SeparatedSyntaxList <ArgumentSyntax> containKeyArguments &&
                       valueInvocation.ArgumentList.Arguments is SeparatedSyntaxList <ArgumentSyntax> containValueArguments

                       && containKeyArguments.First().Expression is MemberAccessExpressionSyntax keyArgument &&
                       containValueArguments.First().Expression is MemberAccessExpressionSyntax valueArgument

                       && keyArgument.Expression is IdentifierNameSyntax keyIdentifier &&
                       valueArgument.Expression is IdentifierNameSyntax valueIdentifier

                       && keyIdentifier.Identifier.Text == valueIdentifier.Identifier.Text);
            }
        protected virtual Diagnostic AnalyzeExpression(ExpressionSyntax expression)
        {
            var visitor = new ConditionalAccessExpressionVisitor();

            expression.Accept(visitor);

            return(visitor.CodeSmells ? Diagnostic.Create(descriptor: Rule, location: expression.GetLocation()) : null);
        }
예제 #7
0
        protected override bool CanRewriteAssertion(ExpressionSyntax expression)
        {
            var visitor = new MemberAccessExpressionsCSharpSyntaxVisitor();

            expression.Accept(visitor);

            var containKey   = visitor.Members.Find(member => member.Name.Identifier.Text == "ContainKey");
            var containValue = visitor.Members.Find(member => member.Name.Identifier.Text == "ContainValue");

            return(!(containKey.Parent is InvocationExpressionSyntax containKeyInvocation && containKeyInvocation.ArgumentList.Arguments.Count > 1 &&
                     containValue.Parent is InvocationExpressionSyntax containValueInvocation && containValueInvocation.ArgumentList.Arguments.Count > 1));
        }
예제 #8
0
 public void SetResult(ExpressionSyntax result = null)
 {
     var setResult = Js.Reference(builder).Member("SetResult");
     if (result != null)
     {
         var statement = setResult.Invoke((JsExpression)result.Accept(Transformer)).Express();
         CurrentState.Add(statement);
     }
     else 
         CurrentState.Add(setResult.Invoke().Express());
     CurrentState.Add(Js.Return());
 }
        protected override bool CanRewriteAssertion(ExpressionSyntax expression)
        {
            var visitor = new MemberAccessExpressionsCSharpSyntaxVisitor();

            expression.Accept(visitor);

            var notBeEmpty = visitor.Members.Find(member => member.Name.Identifier.Text == "NotBeEmpty");
            var notBeNull  = visitor.Members.Find(member => member.Name.Identifier.Text == "NotBeNull");

            return(!(notBeEmpty.Parent is InvocationExpressionSyntax notBeEmptyInvocation && notBeEmptyInvocation.ArgumentList.Arguments.Any() &&
                     notBeNull.Parent is InvocationExpressionSyntax notBeNullInvocation && notBeNullInvocation.ArgumentList.Arguments.Any()));
        }
예제 #10
0
        protected override bool CanRewriteAssertion(ExpressionSyntax expression)
        {
            var visitor = new MemberAccessExpressionsCSharpSyntaxVisitor();

            expression.Accept(visitor);

            var beLessOrEqualTo    = visitor.Members.Find(member => member.Name.Identifier.Text == "BeLessOrEqualTo");
            var beGreaterOrEqualTo = visitor.Members.Find(member => member.Name.Identifier.Text == "BeGreaterOrEqualTo");

            return(!(beLessOrEqualTo.Parent is InvocationExpressionSyntax beLessOrEqualToInvocation && beLessOrEqualToInvocation.ArgumentList.Arguments.Count > 1 &&
                     beGreaterOrEqualTo.Parent is InvocationExpressionSyntax beGreaterOrEqualToInvocation && beGreaterOrEqualToInvocation.ArgumentList.Arguments.Count > 1));
        }
예제 #11
0
    void VisitReturnStatement(ReturnStatementSyntax n)
    {
        ExpressionSyntax expr = n.Expression;
        int args = 0;

        if (expr != null)
        {
            expr.Accept(new ExprBuilder(this));
            args = 1;
        }

        csharpBuilder.returnStmt(args);
    }
예제 #12
0
            private void ValidateThatVariableIsSetToDacFromEvent(ILocalSymbol variableSymbol, ExpressionSyntax variableInitializerExpression)
            {
                if (variableSymbol == null || !_variables.Contains(variableSymbol))
                {
                    return;
                }

                _eventArgsRowWalker.Reset();
                variableInitializerExpression.Accept(_eventArgsRowWalker);

                if (_eventArgsRowWalker.Success)
                {
                    _result.Add(variableSymbol);
                }
            }
예제 #13
0
        public void SetResult(ExpressionSyntax result = null)
        {
            var setResult = Js.Reference(builder).Member("SetResult");

            if (result != null)
            {
                var statement = setResult.Invoke((JsExpression)result.Accept(Transformer)).Express();
                CurrentState.Add(statement);
            }
            else
            {
                CurrentState.Add(setResult.Invoke().Express());
            }
            CurrentState.Add(Js.Return());
        }
예제 #14
0
 private static string LocalVarNameFor(ExpressionSyntax callSite, string typeName, string context)
 {
     return(string.Format("{0}_{1}_{2}", context, typeName, callSite.Accept(NameExtractorVisitor.Instance)));
 }
예제 #15
0
 public static string EvaluateConstantExpression(this ExpressionSyntax expression, SemanticModel semanticModel)
 {
     return(expression.Accept(new ConstantEvaluator(semanticModel)));
 }
 private Tristate EvaluateExpression(ExpressionSyntax expression)
 {
     expression.Accept(_symbolTracker);
     return(_expressionEvaluator.EvaluateExpression(expression));
 }
예제 #17
0
 public CasesAndOr GetCase(ExpressionSyntax parameterNameExpression)
 {
     _Expression = parameterNameExpression;
     _Expression.Accept(this);
     return(_Case);
 }
 private Tristate EvaluateExpression(ExpressionSyntax expression)
 {
     expression.Accept(_symbolTracker);
     return _expressionEvaluator.EvaluateExpression(expression);
 }