protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
        {
            var typeDecl = node.GetParent <TypeDeclaration>();

            if (typeDecl == null)
            {
                return(false);
            }
            var entityDecl          = node.GetParent <EntityDeclaration>();
            var memberResolveResult = ctx.Resolve(entityDecl) as MemberResolveResult;

            if (memberResolveResult == null)
            {
                return(false);
            }
            var typeResolveResult = ctx.Resolve(typeDecl) as TypeResolveResult;

            if (typeResolveResult == null)
            {
                return(false);
            }

            var sourceMember = memberResolveResult.Member;

            return(typeResolveResult.Type.GetMembers(m => m.Name == variableName).Any(m2 => IsAccessible(sourceMember, m2)));
        }
            Func <Expression, bool> GetChecker(Expression expression, IList <IdentifierExpression> identifiers)
            {
                // TODO: This only works for simple cases.
                IList <IMember>   members;
                IList <IVariable> locals;
                var identifierResolveResults = identifiers.Select(identifier => context.Resolve(identifier)).ToList();

                SplitResolveResults(identifierResolveResults, out members, out locals);

                if (expression is InvocationExpression || expression is ObjectCreateExpression)
                {
                    return(node => {
                        if (node is InvocationExpression || node is ObjectCreateExpression)
                        {
                            // We don't know what these might do, so assume it will change the initializer
                            return true;
                        }
                        var binaryOperator = node as BinaryOperatorExpression;
                        if (binaryOperator != null)
                        {
                            var resolveResult = context.Resolve(binaryOperator) as OperatorResolveResult;
                            if (resolveResult == null)
                            {
                                return false;
                            }
                            // Built-in operators are ok, user defined ones not so much
                            return resolveResult.UserDefinedOperatorMethod != null;
                        }
                        return IsConflictingAssignment(node, identifiers, members, locals);
                    });
                }
                else if (expression is IdentifierExpression)
                {
                    var initializerDependsOnMembers       = identifierResolveResults.Any(result => result is MemberResolveResult);
                    var initializerDependsOnReferenceType = identifierResolveResults.Any(result => result.Type.IsReferenceType == true);
                    return(node => {
                        if ((node is InvocationExpression || node is ObjectCreateExpression) &&
                            (initializerDependsOnMembers || initializerDependsOnReferenceType))
                        {
                            // Anything can happen...
                            return true;
                        }
                        var binaryOperator = node as BinaryOperatorExpression;
                        if (binaryOperator != null)
                        {
                            var resolveResult = context.Resolve(binaryOperator) as OperatorResolveResult;
                            if (resolveResult == null)
                            {
                                return false;
                            }
                            return resolveResult.UserDefinedOperatorMethod != null;
                        }
                        return IsConflictingAssignment(node, identifiers, members, locals);
                    });
                }

                return(node => false);
            }
Example #3
0
            void HandlePotentialWrite(Expression expression)
            {
                var variableResolveResult = ctx.Resolve(expression) as LocalResolveResult;

                if (variableResolveResult == null)
                {
                    return;
                }
                variableWritten |= variableResolveResult.Equals(parameter);
            }
 public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
 {
     if (!IsTargetOfInvocation(identifierExpression))
     {
         var mgr = ctx.Resolve(identifierExpression) as MethodGroupResolveResult;
         if (mgr != null)
         {
             UsedMethods.AddRange(mgr.Methods);
         }
     }
     base.VisitIdentifierExpression(identifierExpression);
 }
Example #5
0
            public UsingInfo(AstNode node, BaseRefactoringContext context)
            {
                var importAndAlias = GetImportAndAlias(node);

                Node = node;

                Alias = importAndAlias.Item2;
                Name  = importAndAlias.Item1.ToString();

                IsAlias = Alias != null;

                ResolveResult rr;

                if (node.Ancestors.Contains(context.RootNode))
                {
                    rr = context.Resolve(importAndAlias.Item1);
                }
                else
                {
                    // It's possible that we're looking at a new using that
                    // isn't part of the AST.
                    var resolver = new CSharpAstResolver(new CSharpResolver(context.Compilation), node);
                    rr = resolver.Resolve(importAndAlias.Item1);
                }

                var nrr = rr as NamespaceResolveResult;

                HasTypesFromOtherAssemblies = nrr != null && nrr.Namespace.ContributingAssemblies.Any(a => !a.IsMainAssembly);

                IsSystem = HasTypesFromOtherAssemblies && (Name == "System" || Name.StartsWith("System.", StringComparison.Ordinal));
            }
		protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
		{
			var typeDecl = node.GetParent<TypeDeclaration>();
			if (typeDecl == null)
				return false;
			var entityDecl = node.GetParent<EntityDeclaration>();
			var memberResolveResult = ctx.Resolve(entityDecl) as MemberResolveResult;
			if (memberResolveResult == null)
				return false;
			var typeResolveResult = ctx.Resolve(typeDecl) as TypeResolveResult;
			if (typeResolveResult == null)
				return false;

			var sourceMember = memberResolveResult.Member;

			return typeResolveResult.Type.GetMembers(m => m.Name == variableName).Any(m2 => IsAccessible(sourceMember, m2));
		}
            private static void ExecuteScriptToFixStaticMethodIssue(Script script,
                                                                    BaseRefactoringContext context,
                                                                    AstNode methodDeclaration)
            {
                var clonedDeclaration = (MethodDeclaration)methodDeclaration.Clone();

                clonedDeclaration.Modifiers |= Modifiers.Static;
                script.Replace(methodDeclaration, clonedDeclaration);
                var rr     = context.Resolve(methodDeclaration) as MemberResolveResult;
                var method = (IMethod)rr.Member;

                //method.ImplementedInterfaceMembers.Any(m => methodGroupResolveResult.Methods.Contains((IMethod)m));

                script.DoGlobalOperationOn(rr.Member,
                                           (fctx, fscript, fnode) => { DoStaticMethodGlobalOperation(fnode, fctx, rr, fscript); });
            }
Example #8
0
            public override void VisitInvocationExpression(InvocationExpression invocationExpression)
            {
                base.VisitInvocationExpression(invocationExpression);
                if (!IsCallDependentOnCurrentInstance(invocationExpression))
                {
                    // Call within current class scope without 'this' or 'base'
                    return;
                }
                var targetMethod = context.Resolve(invocationExpression) as InvocationResolveResult;

                if (targetMethod == null)
                {
                    return;
                }
                if (targetMethod.IsVirtualCall)
                {
                    AddIssue(invocationExpression,
                             context.TranslateString("Constructors should not call virtual members"));
                }
            }
Example #9
0
            public override void VisitInvocationExpression(InvocationExpression invocationExpression)
            {
                base.VisitInvocationExpression(invocationExpression);

                // Speed up the inspector by discarding some invocations early
                var hasEligibleArgument = invocationExpression.Arguments.Any(argument => {
                    var primitiveArg = argument as PrimitiveExpression;
                    return(primitiveArg != null && primitiveArg.Value is string);
                });

                if (!hasEligibleArgument)
                {
                    return;
                }

                var invocationResolveResult = context.Resolve(invocationExpression) as CSharpInvocationResolveResult;

                if (invocationResolveResult == null)
                {
                    return;
                }
                Expression         formatArgument;
                IList <Expression> formatArguments;
                TextLocation       formatStart;

                if (!FormatStringHelper.TryGetFormattingParameters(invocationResolveResult, invocationExpression,
                                                                   out formatArgument, out formatStart, out formatArguments, null))
                {
                    return;
                }
                var primitiveArgument = formatArgument as PrimitiveExpression;

                if (primitiveArgument == null || !(primitiveArgument.Value is string))
                {
                    return;
                }
                var format        = (string)primitiveArgument.Value;
                var parsingResult = context.ParseFormatString(format);

                CheckSegments(parsingResult.Segments, formatStart, formatArguments, invocationExpression);
            }
            public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression)
            {
                var parameters = objectCreateExpression.Arguments;

                if (parameters.Count != 2)
                {
                    return;
                }
                var firstParam  = parameters.FirstOrNullObject() as PrimitiveExpression;
                var secondParam = parameters.LastOrNullObject() as PrimitiveExpression;

                if (firstParam == null || !(firstParam.Value is string) ||
                    secondParam == null || !(secondParam.Value is string))
                {
                    return;
                }
                var type = context.Resolve(objectCreateExpression.Type) as TypeResolveResult;

                if (type == null)
                {
                    return;
                }
                var leftLength  = (firstParam.Value as string).Length;
                var rightLength = (secondParam.Value as string).Length;

                Func <int, int, bool> func;

                if (!rules.TryGetValue(type.Type.FullName, out func))
                {
                    return;
                }
                if (!func(leftLength, rightLength))
                {
                    AddIssue(objectCreateExpression,
                             context.TranslateString("The parameters are in the wrong order"),
                             GetAction(objectCreateExpression, firstParam, secondParam));
                }
            }