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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/NoDefaultConstructorIssue.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
using System.Collections.Generic;
 
2
using System.Linq;
 
3
using ICSharpCode.NRefactory.Semantics;
 
4
using ICSharpCode.NRefactory.TypeSystem;
 
5
using ICSharpCode.NRefactory.CSharp.Resolver;
 
6
 
 
7
namespace ICSharpCode.NRefactory.CSharp.Refactoring
 
8
{
 
9
        [IssueDescription ("CS1729: Base class does not contain a 0 argument constructor",
 
10
                                           Description = "CS1729: Base class does not contain a 0 argument constructor",
 
11
                                           Category = IssueCategories.CompilerErrors,
 
12
                                           Severity = Severity.Error,
 
13
                                           IssueMarker = IssueMarker.Underline)]
 
14
        public class NoDefaultConstructorIssue : ICodeIssueProvider
 
15
        {
 
16
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 
17
                {
 
18
                        return new GatherVisitor(context).GetIssues();
 
19
                }
 
20
 
 
21
                private class GatherVisitor : GatherVisitorBase<NoDefaultConstructorIssue>
 
22
                {
 
23
                        private bool initializerInvoked;
 
24
                        private ConstructorInitializer initializer;
 
25
 
 
26
                        public GatherVisitor(BaseRefactoringContext context)
 
27
                                : base(context)
 
28
                        {
 
29
                        }
 
30
 
 
31
                        public override void VisitTypeDeclaration(TypeDeclaration declaration)
 
32
                        {
 
33
                                var result = ctx.Resolve(declaration) as TypeResolveResult;
 
34
                                var baseType = result.Type.DirectBaseTypes.FirstOrDefault(t => !t.IsKnownType(KnownTypeCode.Object) && t.Kind != TypeKind.Interface);
 
35
 
 
36
                                if (baseType != null)
 
37
                                {
 
38
                                        var baseConstructor = baseType.GetConstructors(c => c.Parameters.Count == 0).FirstOrDefault();
 
39
                                        var memberLookup = new MemberLookup(result.Type.GetDefinition(), ctx.Compilation.MainAssembly, false);
 
40
 
 
41
                                        if (baseConstructor == null || !memberLookup.IsAccessible(baseConstructor, true)) {
 
42
                                                var constructor = result.Type.GetConstructors(f => !f.IsSynthetic).FirstOrDefault();
 
43
 
 
44
                                                if (constructor == null) {
 
45
                                                        // If there are no constructors declared then the base constructor isn't being invoked
 
46
                                                        this.AddIssue(declaration, baseType);
 
47
                                                }
 
48
                                        }
 
49
                                }
 
50
 
 
51
                                base.VisitTypeDeclaration(declaration);
 
52
                        }
 
53
 
 
54
                        public override void VisitConstructorDeclaration(ConstructorDeclaration declaration)
 
55
                        {
 
56
                                var result = ctx.Resolve(declaration) as MemberResolveResult;
 
57
                                if (result == null || result.IsError)
 
58
                                        return;
 
59
 
 
60
                                var baseType = result.Member.DeclaringType.DirectBaseTypes.FirstOrDefault(t => !t.IsKnownType(KnownTypeCode.Object) && t.Kind != TypeKind.Interface);
 
61
 
 
62
                                if (baseType != null) {
 
63
                                        var baseConstructor = baseType.GetConstructors(c => c.Parameters.Count == 0).FirstOrDefault();
 
64
                                        var memberLookup = new MemberLookup(result.Member.DeclaringType.GetDefinition(), ctx.Compilation.MainAssembly, false);
 
65
 
 
66
                                        if (baseConstructor == null || !memberLookup.IsAccessible(baseConstructor, true)) {
 
67
                                                this.initializerInvoked = false;
 
68
                                                this.initializer = null;
 
69
                                
 
70
                                                base.VisitConstructorDeclaration(declaration);
 
71
 
 
72
                                                if (!this.initializerInvoked) {
 
73
                                                        int argumentCount = initializer != null ? initializer.Arguments.Count : 0;
 
74
                                                        this.AddIssue(declaration, baseType, argumentCount);
 
75
                                                }
 
76
                                        }
 
77
                                }
 
78
                        }
 
79
 
 
80
                        public override void VisitConstructorInitializer(ConstructorInitializer initializer)
 
81
                        {
 
82
                                var result = ctx.Resolve(initializer);
 
83
 
 
84
                                if (!result.IsError) {
 
85
                                        this.initializerInvoked = true;
 
86
                                } else {
 
87
                                        this.initializer = initializer;
 
88
                                }
 
89
                        }
 
90
 
 
91
                        private void AddIssue(AstNode node, IType baseType, int argumentCount = 0)
 
92
                        {
 
93
                                var identifier = node.GetChildByRole(Roles.Identifier);
 
94
                                this.AddIssue(
 
95
                                        identifier,
 
96
                                        string.Format(ctx.TranslateString("CS1729: The type '{0}' does not contain a constructor that takes '{1}' arguments"), baseType.Name, argumentCount));
 
97
                        }
 
98
                }
 
99
        }
 
100
}
 
101