Esempio n. 1
0
    public override SyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
    {
        // update the expression but not the Elvis itself
        var newExpression = (ExpressionSyntax)Visit(node.Expression);

        return(node.Update(newExpression, node.OperatorToken, node.WhenNotNull));
    }
 public static async Task ComputeRefactoringAsync(RefactoringContext context, ConditionalAccessExpressionSyntax conditionalAccessExpression)
 {
     if (context.IsRefactoringEnabled(RefactoringIdentifiers.WrapCallChain))
     {
         await WrapCallChainRefactoring.ComputeRefactoringsAsync(context, conditionalAccessExpression).ConfigureAwait(false);
     }
 }
Esempio n. 3
0
        public override Evaluation VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            node.WhenNotNull?.Accept <Evaluation>(this);
            node.Expression?.Accept <Evaluation>(this);

            return(base.VisitConditionalAccessExpression(node));
        }
Esempio n. 4
0
 private bool ReplacementBreaksConditionalAccessExpression(ConditionalAccessExpressionSyntax conditionalAccessExpression, ConditionalAccessExpressionSyntax newConditionalAccessExpression)
 {
     return(!SymbolsAreCompatible(conditionalAccessExpression, newConditionalAccessExpression) ||
            !TypesAreCompatible(conditionalAccessExpression, newConditionalAccessExpression) ||
            !SymbolsAreCompatible(conditionalAccessExpression.WhenNotNull, newConditionalAccessExpression.WhenNotNull) ||
            !TypesAreCompatible(conditionalAccessExpression.WhenNotNull, newConditionalAccessExpression.WhenNotNull));
 }
        private Block BuildConditionalAccessExpression(ConditionalAccessExpressionSyntax conditionalAccess, Block currentBlock)
        {
            var whenNotNull = BuildExpression(conditionalAccess.WhenNotNull, CreateBlock(currentBlock));

            return(BuildExpression(conditionalAccess.Expression,
                                   CreateBinaryBranchBlock(conditionalAccess, currentBlock, whenNotNull)));
        }
Esempio n. 6
0
        public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            node.WhenNotNull?.Accept(this);
            node.Expression?.Accept(this);

            base.VisitConditionalAccessExpression(node);
        }
Esempio n. 7
0
        public static void Write(this ConditionalAccessExpressionSyntax syntax,
                                 IIndentedTextWriterWrapper textWriter,
                                 IContext context)
        {
            textWriter.Write("_M.CA(");
            syntax.Expression.Write(textWriter, context);
            textWriter.Write(",function(obj) return ");
            var stringWriter  = new StringWriter();
            var newTextWriter = new IndentedTextWriterWrapper(stringWriter);

            syntax.WhenNotNull.Write(newTextWriter, context);

            if (stringWriter.ToString().StartsWith("("))
            {
                textWriter.Write("((obj % _M.DOT)");
                textWriter.Write(newTextWriter.ToString().Substring(1));
            }
            else
            {
                textWriter.Write("(obj % _M.DOT)");
                textWriter.AppendTextWriter(newTextWriter);
            }

            textWriter.Write("; end)");
        }
Esempio n. 8
0
        private static ExpressionSyntax TryCreateExpressionWithoutConditionalAccess(ConditionalAccessExpressionSyntax conditionalAccess)
        {
            switch (conditionalAccess.WhenNotNull)
            {
            case MemberBindingExpressionSyntax memberBinding:
            {
                return(SimpleMemberAccessExpression(conditionalAccess.Expression, memberBinding.Name));
            }

            case ElementBindingExpressionSyntax elementBinding:
            {
                return(ElementAccessExpression(conditionalAccess.Expression, elementBinding.ArgumentList));
            }

            case InvocationExpressionSyntax invocation:
            {
                if (!(invocation.Expression is MemberBindingExpressionSyntax memberBinding))
                {
                    return(null);
                }

                return(InvocationExpression(SimpleMemberAccessExpression(conditionalAccess.Expression, memberBinding.Name), invocation.ArgumentList));
            }
            }

            return(null);
        }
        public override SyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            ExpressionSyntax whenNotNull = node.WhenNotNull;

            if (whenNotNull?.IsKind(SyntaxKind.MemberBindingExpression) == true)
            {
                var memberBinding = (MemberBindingExpressionSyntax)whenNotNull;

                SimpleNameSyntax name = memberBinding.Name;

                if (name != null &&
                    _textSpans.Contains(name.Span))
                {
                    _textSpans.Remove(node.Span);

                    var expression = (ExpressionSyntax)base.Visit(node.Expression);

                    InvocationExpressionSyntax invocation = InvocationExpression(
                        memberBinding.WithName(IdentifierName(_methodName).WithLeadingTrivia(name.GetLeadingTrivia())),
                        ArgumentList().WithTrailingTrivia(memberBinding.GetTrailingTrivia()));

                    return(node
                           .WithExpression(expression)
                           .WithWhenNotNull(invocation));
                }
            }

            return(base.VisitConditionalAccessExpression(node));
        }
Esempio n. 10
0
 public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     if (Predicate(node.Expression))
     {
         Success = true;
     }
 }
Esempio n. 11
0
        public override Ust VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            Ust        ustNode    = base.Visit(node.Expression);
            Expression expression = ustNode as Expression;

            if (expression == null)
            {
                expression = new MultichildExpression(((ArgsUst)ustNode).Collection, ustNode.TextSpan);
            }
            Expression whenNotNullExpression;

            if (node.WhenNotNull is ElementBindingExpressionSyntax)
            {
                var args = (ArgsUst)VisitElementBindingExpression((ElementBindingExpressionSyntax)node.WhenNotNull);
                whenNotNullExpression = new IndexerExpression(expression, args, args.TextSpan);
            }
            else
            {
                whenNotNullExpression = (Expression)base.Visit(node.WhenNotNull);
            }
            var nullExpr       = new NullLiteral(default(TextSpan));
            var binayOpLiteral = new BinaryOperatorLiteral(BinaryOperator.Equal, default(TextSpan));
            var condition      = new BinaryOperatorExpression(expression, binayOpLiteral, nullExpr, default(TextSpan));

            var result = new ConditionalExpression(condition, nullExpr, whenNotNullExpression, node.GetTextSpan());

            return(result);
        }
Esempio n. 12
0
        private static ExpressionSyntax Reformat(ConditionalAccessExpressionSyntax expression)
        {
            if (!expression.WhenNotNull.IsKind(SyntaxKind.InvocationExpression))
            {
                return(expression);
            }

            var invocation = (InvocationExpressionSyntax)expression.WhenNotNull;

            if (!expression.Expression.IsKind(SyntaxKind.ConditionalAccessExpression) &&
                !expression.Expression.IsKind(SyntaxKind.InvocationExpression) &&
                invocation.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
            {
                var memberAccess = (MemberAccessExpressionSyntax)invocation.Expression;

                var conditionalAccess = SyntaxFactory.ConditionalAccessExpression(
                    expression.Expression,
                    memberAccess.Expression);

                var newMemberAccess = SyntaxFactory.MemberAccessExpression(
                    SyntaxKind.SimpleMemberAccessExpression,
                    conditionalAccess,
                    memberAccess.Name);

                return(SyntaxFactory.InvocationExpression(newMemberAccess, invocation.ArgumentList));
            }
            else
            {
                return(expression);
            }
        }
 public override bool VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     if (!Visit(node.Expression))
     {
         return(false);
     }
     return(Visit(node.WhenNotNull));
 }
 public static Doc Print(ConditionalAccessExpressionSyntax node)
 {
     return(Doc.Concat(
                Node.Print(node.Expression),
                Token.Print(node.OperatorToken),
                Node.Print(node.WhenNotNull)
                ));
 }
 public override ExpressionSyntax VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     return(node
            .WithExpression(
                Visit(node.Expression))
            .WithWhenNotNull(
                Visit(node.WhenNotNull)));
 }
 public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     if (!_foundConditionalAccess)
     {
         _foundConditionalAccess = true;
     }
     base.VisitConditionalAccessExpression(node);
 }
 private Doc PrintConditionalAccessExpressionSyntax(
     ConditionalAccessExpressionSyntax node)
 {
     return(Concat(
                this.Print(node.Expression),
                this.PrintSyntaxToken(node.OperatorToken),
                this.Print(node.WhenNotNull)
                ));
 }
        public override LuaSyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            bool isEmpty = functions_.Count == 0;

            if (isEmpty)
            {
                LuaFunctionExpressionSyntax function = new LuaFunctionExpressionSyntax();
                PushFunction(function);
                blocks_.Push(function.Body);
            }

            var temp = GetTempIdentifier(node.Expression);

            conditionalTemps_.Push(temp);

            var expression = (LuaExpressionSyntax)node.Expression.Accept(this);

            CurBlock.Statements.Add(new LuaLocalVariableDeclaratorSyntax(temp, expression));

            LuaBinaryExpressionSyntax condition   = new LuaBinaryExpressionSyntax(temp, LuaSyntaxNode.Tokens.NotEquals, LuaIdentifierNameSyntax.Nil);
            LuaIfStatementSyntax      ifStatement = new LuaIfStatementSyntax(condition);

            CurBlock.Statements.Add(ifStatement);

            blocks_.Push(ifStatement.Body);
            var whenNotNull = (LuaExpressionSyntax)node.WhenNotNull.Accept(this);

            blocks_.Pop();
            conditionalTemps_.Pop();

            if (node.Parent.IsKind(SyntaxKind.ExpressionStatement))
            {
                if (isEmpty)
                {
                    throw new InvalidOperationException();
                }
                ifStatement.Body.Statements.Add(new LuaExpressionStatementSyntax(whenNotNull));
                return(LuaExpressionSyntax.EmptyExpression);
            }
            else
            {
                LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(temp, whenNotNull);
                ifStatement.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                if (isEmpty)
                {
                    var function = CurFunction;
                    function.AddStatement(new LuaReturnStatementSyntax(temp));
                    blocks_.Pop();
                    PopFunction();
                    return(new LuaInvocationExpressionSyntax(new LuaParenthesizedExpressionSyntax(function)));
                }
                else
                {
                    return(temp);
                }
            }
        }
Esempio n. 19
0
 public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     base.VisitConditionalAccessExpression(node);
     if (node.OperatorToken.Kind() != SyntaxKind.QuestionToken)
     {
         return;
     }
     Count++;
 }
        private void BuildConditionalAccessExpression(ConditionalAccessExpressionSyntax conditionalAccess)
        {
            var successorBlock = currentBlock;

            currentBlock = CreateBlock(currentBlock);
            BuildExpression(conditionalAccess.WhenNotNull);

            currentBlock = CreateBinaryBranchBlock(conditionalAccess, successorBlock, currentBlock);
            BuildExpression(conditionalAccess.Expression);
        }
        /// <summary>
        /// Returns first <see cref="MemberBindingExpressionSyntax"/> of <paramref name="conditionalAccessExpression" />
        /// </summary>
        /// <param name="conditionalAccessExpression">Conditional access to be inspected</param>
        /// <returns>First member binding expression</returns>
        public static MemberBindingExpressionSyntax GetFirstMemberBindingExpression(this ConditionalAccessExpressionSyntax conditionalAccessExpression)
        {
            var firstMemberBindingExpressionOnTheRightOfTheDot = conditionalAccessExpression
                                                                 ?.WhenNotNull
                                                                 ?.DescendantNodesAndSelf()
                                                                 .OfType <MemberBindingExpressionSyntax>()
                                                                 .FirstOrDefault();

            return(firstMemberBindingExpressionOnTheRightOfTheDot);
        }
Esempio n. 22
0
            public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
            {
                if (entryPoint.IsMethodLevel() && node.IsParent <AnonymousFunctionExpressionSyntax>())
                {
                    return;
                }
                nobcounter++;

                base.VisitConditionalAccessExpression(node);
            }
        private static void CollectDisposedSymbol <T>(ConditionalAccessExpressionSyntax conditionalAccess, SemanticModel semanticModel,
                                                      IMethodSymbol disposeMethod, Predicate <T> isSymbolRelevant, ref ImmutableHashSet <T> fieldsDisposed)
            where T : class, ISymbol
        {
            T fieldSymbol;

            if (TryGetDisposedSymbol(conditionalAccess, semanticModel, disposeMethod,
                                     isSymbolRelevant, out fieldSymbol))
            {
                fieldsDisposed = fieldsDisposed.Add(fieldSymbol);
            }
        }
Esempio n. 24
0
        /// <summary>
        /// Runs conditional access expression. (CS6)
        ///   [Syntax] EXPR?.WHEN_NOT_NULL
        /// </summary>
        private HybInstance RunConditionalAccess(ConditionalAccessExpressionSyntax node)
        {
            var left = RunExpression(node.Expression);

            Ctx._bound = left;

            if (left.IsNull() == false)
            {
                return(RunExpression(node.WhenNotNull));
            }
            return(HybInstance.Null());
        }
Esempio n. 25
0
 public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     if (entryPoint.IsMethodLevel() && node.IsParent <AnonymousFunctionExpressionSyntax>())
     {
         return;
     }
     IncreaseHeight();
     EmbeddednessConsideredToIncrease();
     base.VisitConditionalAccessExpression(node);
     currentNL--;
     EmbeddednessConsideredToDecrease();
 }
        private void BuildConditionalAccessExpression(ConditionalAccessExpressionSyntax conditionalAccess)
        {
            // is this required here?
            currentBlock.ReversedInstructions.Add(conditionalAccess);

            var successorBlock = currentBlock;

            currentBlock = CreateBlock(currentBlock);
            BuildExpression(conditionalAccess.WhenNotNull);

            currentBlock = CreateBinaryBranchBlock(conditionalAccess, successorBlock, currentBlock);
            BuildExpression(conditionalAccess.Expression);
        }
Esempio n. 27
0
        public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            if (!PreVisit(node))
            {
                return;
            }

            node.WhenNotNull?.Accept(this);
            node.Expression?.Accept(this);

            base.VisitConditionalAccessExpression(node);

            PostVisit(node);
        }
        public override SyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            var old = insideConditionalExpression;

            insideConditionalExpression = true;
            try
            {
                return(base.VisitConditionalAccessExpression(node));
            }
            finally
            {
                insideConditionalExpression = old;
            }
        }
Esempio n. 29
0
        private bool IsForbiddenUsage(SyntaxNodeAnalysisContext context, ConditionalAccessExpressionSyntax conditionalAccess)
        {
            var memberName = conditionalAccess.GetFirstMemberBindingExpression()?.Name?.Identifier.ValueText;

            if (!string.IsNullOrEmpty(memberName) &&
                _config.ForbiddenMembers.All(forbiddenMember => !memberName.Equals(forbiddenMember, StringComparison.Ordinal)))
            {
                return(false);
            }

            var actualTargetType = context.SemanticModel.GetTypeInfo(conditionalAccess.Expression).Type as INamedTypeSymbol;

            return(actualTargetType != null && _config.ForbiddenTypes.Any(forbidenType => actualTargetType.IsDerivedFrom(forbidenType, context.Compilation)));
        }
Esempio n. 30
0
            public ConditionalAccessInfo(SemanticModel semanticModel, ConditionalAccessExpressionSyntax node)
            {
                Node = node;

                var expressionType = semanticModel.GetTypeInfo(node.Expression).Type;

                ExpressionType = SyntaxFactory.ParseTypeName(expressionType.ToMinimalDisplayString(semanticModel, node.Expression.GetLocation().SourceSpan.Start));

                var resultType = semanticModel.GetTypeInfo(node).Type;

                ResultType = SyntaxFactory.ParseTypeName(resultType.ToMinimalDisplayString(semanticModel, node.GetLocation().SourceSpan.Start));

                IsResultVoid = resultType.SpecialType == SpecialType.System_Void;
                IsComplex    = IsExpressionComplexEnoughToGetATemporaryVariable.IsComplex(semanticModel, node.Expression);
            }
Esempio n. 31
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     this.OnNodeVisited(node);
     if (!this.traverseRootOnly) base.VisitConditionalAccessExpression(node);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="node"></param>
 public override sealed void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     this.OnNodeVisited(node, this.type.IsInstanceOfType(node));
     base.VisitConditionalAccessExpression(node);
 }
Esempio n. 33
0
 public override void VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
 {
     base.VisitConditionalAccessExpression(node);
 }
 public ConditionalAccessExpressionTranslation(ConditionalAccessExpressionSyntax syntax, SyntaxTranslation parent) : base(syntax, parent)
 {
     Expression = syntax.Expression.Get<ExpressionTranslation>(this);
     WhenNotNull = syntax.WhenNotNull.Get<ExpressionTranslation>(this);
          
 }
		public override SyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
		{
			var result = base.VisitConditionalAccessExpression(node);
			var conditionalAccessResult = result as ConditionalAccessExpressionSyntax;

			if (conditionalAccessResult == node || conditionalAccessResult == null)
			{
				return node;
			}

			if (((conditionalAccessResult.WhenNotNull as ParenthesizedExpressionSyntax)?.Expression ?? conditionalAccessResult.WhenNotNull as AwaitExpressionSyntax)?.Kind() == SyntaxKind.AwaitExpression)
			{
				var awaitExpression = (AwaitExpressionSyntax)(conditionalAccessResult.WhenNotNull as ParenthesizedExpressionSyntax)?.Expression ?? conditionalAccessResult.WhenNotNull as AwaitExpressionSyntax;
				var awaitExpressionExpression = awaitExpression?.Expression;

				if (awaitExpressionExpression == null)
				{
					return result;
				}

				var stack = new Stack<ExpressionSyntax>();
				var syntax = awaitExpressionExpression;
				
				while (true)
				{
					if (syntax is MemberAccessExpressionSyntax)
					{
						stack.Push(syntax);
						syntax = ((MemberAccessExpressionSyntax)syntax).Expression;
					}
					else if (syntax is InvocationExpressionSyntax)
					{
						stack.Push(syntax);
						syntax = ((InvocationExpressionSyntax)syntax).Expression;
					}
					else if (syntax is MemberBindingExpressionSyntax)
					{
						var name = ((MemberBindingExpressionSyntax)syntax).Name;

						dynamic current = SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, conditionalAccessResult.Expression, name);

						while (stack.Count > 0)
						{
							dynamic next = stack.Pop();

							current = next.WithExpression(current);
						}

						syntax = current;

						break;
					}
					else
					{
						throw new InvalidOperationException("Unsupported expression " + syntax);
					}
				}

				if (node.Parent == null || node.Parent is StatementSyntax)
				{
					return SyntaxFactory.IfStatement(SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, conditionalAccessResult.Expression, SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), SyntaxFactory.Block(SyntaxFactory.ExpressionStatement(SyntaxFactory.AwaitExpression(syntax))));
				}
				else
				{
					var typeResult = this.semanticModel.GetSpeculativeTypeInfo(node.SpanStart + this.displacement, node, SpeculativeBindingOption.BindAsExpression);

					if (typeResult.Type.IsValueType)
					{
						return SyntaxFactory.ParenthesizedExpression(SyntaxFactory.ConditionalExpression(SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, conditionalAccessResult.Expression, SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), SyntaxFactory.AwaitExpression(syntax), SyntaxFactory.ParenthesizedExpression(SyntaxFactory.CastExpression(SyntaxFactory.ParseTypeName(typeResult.Type.ToString()), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)))));
					}
					else
					{ 
						return SyntaxFactory.ParenthesizedExpression(SyntaxFactory.ConditionalExpression(SyntaxFactory.BinaryExpression(SyntaxKind.NotEqualsExpression, conditionalAccessResult.Expression, SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), SyntaxFactory.AwaitExpression(syntax), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)));
					}
				}
			}

			return result;
		}
 private IEnumerable<TypeInferenceInfo> InferTypeInConditionalAccessExpression(ConditionalAccessExpressionSyntax expression)
 {
     return InferTypes(expression);
 }
Esempio n. 37
0
        public override SyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            this.AppendCompileIssue(node, IssueType.Warning, IssueId.ConditionalAccess);
            this.Visit(node.Expression);
            switch (node.WhenNotNull.Kind())
            {
                case SyntaxKind.ElementBindingExpression:
                    var eb = node.WhenNotNull as ElementBindingExpressionSyntax;
                    _output.TrivialWrite('[');
                    this.Visit(eb.ArgumentList);
                    _output.TrivialWrite(']');
                    break;

                case SyntaxKind.MemberBindingExpression:
                    var mb = node.WhenNotNull as MemberBindingExpressionSyntax;
                    _output.TrivialWrite('.');
                    this.Visit(mb.Name);
                    break;
            }

            return node;
        }
 private IEnumerable<ITypeSymbol> InferTypeInConditionalAccessExpression(ConditionalAccessExpressionSyntax expression)
 {
     return InferTypes(expression);
 }