~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/VariableHidesMemberIssue/VariableHidesMemberIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// VariableHidesMemberIssue.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System.Collections.Generic;
 
27
using System.Linq;
 
28
using ICSharpCode.NRefactory.Semantics;
 
29
using ICSharpCode.NRefactory.TypeSystem;
 
30
using System;
 
31
 
 
32
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
33
{
 
34
        public abstract class VariableHidesMemberIssue : ICodeIssueProvider
 
35
        {
 
36
                public abstract IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context);
 
37
                
 
38
                protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
 
39
                {
 
40
                        var typeDecl = node.GetParent<TypeDeclaration>();
 
41
                        if (typeDecl == null)
 
42
                                return false;
 
43
                        var entityDecl = node.GetParent<EntityDeclaration>();
 
44
                        var memberResolveResult = ctx.Resolve(entityDecl) as MemberResolveResult;
 
45
                        if (memberResolveResult == null)
 
46
                                return false;
 
47
                        var typeResolveResult = ctx.Resolve(typeDecl) as TypeResolveResult;
 
48
                        if (typeResolveResult == null)
 
49
                                return false;
 
50
 
 
51
                        var sourceMember = memberResolveResult.Member;
 
52
 
 
53
                        return typeResolveResult.Type.GetMembers(m => m.Name == variableName).Any(m2 => IsAccessible(sourceMember, m2));
 
54
                }
 
55
 
 
56
                static bool IsAccessible(IMember sourceMember, IMember targetMember)
 
57
                {
 
58
                        if (sourceMember.IsStatic != targetMember.IsStatic)
 
59
                                return false;
 
60
 
 
61
                        var sourceType = sourceMember.DeclaringType;
 
62
                        var targetType = targetMember.DeclaringType;
 
63
                        switch (targetMember.Accessibility) {
 
64
                                case Accessibility.None:
 
65
                                        return false;
 
66
                                case Accessibility.Private:
 
67
                                        // check for members of outer classes (private members of outer classes can be accessed)
 
68
                                        var targetTypeDefinition = targetType.GetDefinition();
 
69
                                        for (var t = sourceType.GetDefinition(); t != null; t = t.DeclaringTypeDefinition) {
 
70
                                                if (t.Equals(targetTypeDefinition))
 
71
                                                        return true;
 
72
                                        }
 
73
                                        return false;
 
74
                                case Accessibility.Public:
 
75
                                        return true;
 
76
                                case Accessibility.Protected:
 
77
                                        return IsProtectedAccessible(sourceType, targetType);
 
78
                                case Accessibility.Internal:
 
79
                                        return IsInternalAccessible(sourceMember.ParentAssembly, targetMember.ParentAssembly);
 
80
                                case Accessibility.ProtectedOrInternal:
 
81
                                        return IsInternalAccessible(sourceMember.ParentAssembly, targetMember.ParentAssembly) || IsProtectedAccessible(sourceType, targetType);
 
82
                                case Accessibility.ProtectedAndInternal:
 
83
                                        return IsInternalAccessible(sourceMember.ParentAssembly, targetMember.ParentAssembly) && IsProtectedAccessible(sourceType, targetType);
 
84
                                default:
 
85
                                        throw new Exception("Invalid value for Accessibility");
 
86
                        }
 
87
                }
 
88
 
 
89
                static bool IsProtectedAccessible(IType sourceType, IType targetType)
 
90
                {
 
91
                        return sourceType.GetAllBaseTypes().Any(type => targetType.Equals(type));
 
92
                }
 
93
 
 
94
                static bool IsInternalAccessible(IAssembly sourceAssembly, IAssembly targetAssembly)
 
95
                {
 
96
                        return sourceAssembly.InternalsVisibleTo(targetAssembly);
 
97
                }
 
98
 
 
99
        }
 
100
}