private static void InsertDeclarations( [NotNull] IInvocationExpression invocation, ref LocalList <IDeclarationStatement> declarations) { IBlock block = invocation.GetContainingNode <IBlock>(true).NotNull(); ICSharpStatement anchor = invocation.GetContainingStatement(); for (var i = declarations.Count - 1; i >= 0; i--) { declarations[i] = block.AddStatementBefore(declarations[i], anchor); anchor = declarations[i]; } }
internal static CodeContractAssertion FromInvocationExpression(IInvocationExpression invocationExpression) { Contract.Requires(invocationExpression != null); var statement = invocationExpression.GetContainingStatement(); Contract.Assert(statement != null); ContractAssertionType?assertionType = GetContractAssertionType(invocationExpression); if (assertionType == null) { return(null); } Contract.Assert(invocationExpression.Arguments.Count != 0, "Invocation expression should have at least one argument!"); IExpression originalPredicateExpression = invocationExpression.Arguments[0].Expression; var predicateExpression = PredicateExpression.Create(originalPredicateExpression); var message = ExtractMessage(invocationExpression); // TODO: switch to dictionary of factory methods? switch (assertionType.Value) { case ContractAssertionType.Requires: return(new ContractRequires(statement, invocationExpression, predicateExpression, message)); case ContractAssertionType.Ensures: return(new ContractEnsures(statement, predicateExpression, message)); case ContractAssertionType.Invariant: return(new ContractInvariant(statement, predicateExpression, message)); case ContractAssertionType.Assert: return(new ContractAssert(statement, predicateExpression, message)); case ContractAssertionType.Assume: return(new ContractAssume(statement, predicateExpression, message)); default: Contract.Assert(false, "Unknown assertion type: " + assertionType.Value); return(null); } }
internal static CodeContractAssertion FromInvocationExpression(IInvocationExpression invocationExpression) { Contract.Requires(invocationExpression != null); var statement = invocationExpression.GetContainingStatement(); Contract.Assert(statement != null); ContractAssertionType? assertionType = GetContractAssertionType(invocationExpression); if (assertionType == null) return null; Contract.Assert(invocationExpression.Arguments.Count != 0, "Invocation expression should have at least one argument!"); IExpression originalPredicateExpression = invocationExpression.Arguments[0].Expression; var predicateExpression = PredicateExpression.Create(originalPredicateExpression); var message = ExtractMessage(invocationExpression); // TODO: switch to dictionary of factory methods? switch (assertionType.Value) { case ContractAssertionType.Requires: return new ContractRequires(statement, invocationExpression, predicateExpression, message); case ContractAssertionType.Ensures: return new ContractEnsures(statement, predicateExpression, message); case ContractAssertionType.Invariant: return new ContractInvariant(statement, predicateExpression, message); case ContractAssertionType.Assert: return new ContractAssert(statement, predicateExpression, message); case ContractAssertionType.Assume: return new ContractAssume(statement, predicateExpression, message); default: Contract.Assert(false, "Unknown assertion type: " + assertionType.Value); return null; } }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var oldMethod = myExpression.Reference.Resolve().DeclaredElement as IMethod; if (oldMethod == null) { return(null); } var factory = CSharpElementFactory.GetInstance(myInvocationExpression); var builder = FactoryArgumentsBuilder.Create(); if (myExpression.QualifierExpression != null) { builder.Argument(myExpression.QualifierExpression); builder.Append("."); } builder.Append(myNewMethod.ShortName); builder.Append("("); var returnType = oldMethod.ReturnType; var oldArguments = myInvocationExpression.Arguments; var firstPositionalArgIdx = GetPositionalArgumentIndex(oldArguments); var curArgIdx = 0; var newParameters = myNewMethod.Parameters; var argumentIndex = 0; // insert new argument to correct position. If we should use positional argument, we will check it and use. for (int i = 0; i < newParameters.Count; i++) { var parameter = newParameters[i]; if (parameter.Type.Equals(returnType)) { var referenceExpression = factory.CreateReferenceExpression(GetUniqueName(myInvocationExpression, ResultParamName)); IArgument argument = curArgIdx > firstPositionalArgIdx ? factory.CreateArgument(ParameterKind.VALUE, ResultParamName, referenceExpression) : factory.CreateArgument(ParameterKind.VALUE, referenceExpression); builder.Argument(argument); argumentIndex = i; } else { builder.Argument(oldArguments[curArgIdx]); curArgIdx++; } if (i + 1 != newParameters.Count) { builder.Append(","); } } builder.Append(")"); var newInvocation = factory.CreateExpression(builder.ToString(), builder.ToArguments()); var newDeclaration = (IDeclarationStatement)factory.CreateStatement("var $0 = $1;", GetUniqueName(myInvocationExpression, "size"), newInvocation); var oldStatement = myInvocationExpression.GetContainingStatement().NotNull("oldStatement != null"); IDeclarationStatement result; if (oldStatement is IExpressionStatement) { result = oldStatement.ReplaceBy(newDeclaration); } else { var declaration = (oldStatement as IDeclarationStatement).NotNull(nameof(oldStatement) + " is not IDeclarationStatement"); // if only one declaration just replace it if (declaration.Declaration.Declarators.Count == 1) { result = oldStatement.ReplaceBy(newDeclaration); } else { // There are several declaration, exclude our and transform it. var expression = myInvocationExpression.GetContainingParenthesizedExpression(); var currentInitializer = ExpressionInitializerNavigator.GetByValue(expression); var selectedDeclarator = LocalVariableDeclarationNavigator.GetByInitial(currentInitializer).NotNull("selectedDeclarator != null"); MultipleDeclarationUtil.SplitDeclarationBefore(selectedDeclarator); MultipleDeclarationUtil.SplitDeclarationAfter(selectedDeclarator); result = declaration.ReplaceBy(newDeclaration); } } var actualArgument = result.Descendants <IArgumentList>().First().Arguments[argumentIndex]; if (!actualArgument.IsValid()) { return(null); } // allow user to decide which array he will use var hotspotsRegistry = new HotspotsRegistry(newInvocation.GetPsiServices()); hotspotsRegistry.Register(new ITreeNode[] { actualArgument }); return(BulbActionUtils.ExecuteHotspotSession(hotspotsRegistry, actualArgument.GetDocumentRange())); }
public ICSharpStatement GetContainingStatement() { return(_invocationExpression.GetContainingStatement()); }