~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/NRefactoryResolver/TypeVisitor.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
// created on 22.08.2003 at 19:02
 
5
 
 
6
using System;
 
7
using System.Collections.Generic;
 
8
using System.Text;
 
9
 
 
10
using ICSharpCode.NRefactory;
 
11
using ICSharpCode.NRefactory.Ast;
 
12
using ICSharpCode.NRefactory.Visitors;
 
13
 
 
14
namespace ICSharpCode.SharpDevelop.Dom.NRefactoryResolver
 
15
{
 
16
        // TODO: Rename this class, the visitor functionality was moved to ResolveVisitor
 
17
        public static class TypeVisitor
 
18
        {
 
19
                [Flags]
 
20
                public enum ReturnTypeOptions
 
21
                {
 
22
                        None = 0,
 
23
                        Lazy = 1,
 
24
                        BaseTypeReference = 2
 
25
                }
 
26
                
 
27
                public static IReturnType CreateReturnType(TypeReference reference, NRefactoryResolver resolver)
 
28
                {
 
29
                        return CreateReturnType(reference,
 
30
                                                resolver.CallingClass, resolver.CallingMember,
 
31
                                                resolver.CaretLine, resolver.CaretColumn,
 
32
                                                resolver.ProjectContent, ReturnTypeOptions.None);
 
33
                }
 
34
                
 
35
                public static IReturnType CreateReturnType(TypeReference reference, IClass callingClass,
 
36
                                                           IMember callingMember, int caretLine, int caretColumn,
 
37
                                                           IProjectContent projectContent,
 
38
                                                           ReturnTypeOptions options)
 
39
                {
 
40
                        if (reference == null) return null;
 
41
                        if (reference.IsNull) return null;
 
42
                        if (reference is InnerClassTypeReference) {
 
43
                                reference = ((InnerClassTypeReference)reference).CombineToNormalTypeReference();
 
44
                        }
 
45
                        
 
46
                        bool useLazyReturnType = (options & ReturnTypeOptions.Lazy) == ReturnTypeOptions.Lazy;
 
47
                        bool isBaseTypeReference = (options & ReturnTypeOptions.BaseTypeReference) == ReturnTypeOptions.BaseTypeReference;
 
48
                        
 
49
                        LanguageProperties languageProperties = projectContent.Language;
 
50
                        IReturnType t = null;
 
51
                        if (callingClass != null && !reference.IsGlobal) {
 
52
                                foreach (ITypeParameter tp in callingClass.TypeParameters) {
 
53
                                        if (languageProperties.NameComparer.Equals(tp.Name, reference.Type)) {
 
54
                                                t = new GenericReturnType(tp);
 
55
                                                break;
 
56
                                        }
 
57
                                }
 
58
                                IMethod callingMethod = callingMember as IMethod;
 
59
                                if (t == null && callingMethod != null) {
 
60
                                        foreach (ITypeParameter tp in callingMethod.TypeParameters) {
 
61
                                                if (languageProperties.NameComparer.Equals(tp.Name, reference.Type)) {
 
62
                                                        t = new GenericReturnType(tp);
 
63
                                                        break;
 
64
                                                }
 
65
                                        }
 
66
                                }
 
67
                        }
 
68
                        if (t == null && reference.Type == "dynamic") {
 
69
                                t = new DynamicReturnType(projectContent);
 
70
                        }
 
71
                        if (t == null) {
 
72
                                int typeParameterCount = reference.GenericTypes.Count;
 
73
                                if (reference.IsKeyword) {
 
74
                                        // keyword-type like void, int, string etc.
 
75
                                        IClass c = projectContent.GetClass(reference.Type, typeParameterCount);
 
76
                                        if (c != null)
 
77
                                                t = c.DefaultReturnType;
 
78
                                        else
 
79
                                                t = new GetClassReturnType(projectContent, reference.Type, typeParameterCount);
 
80
                                } else {
 
81
                                        if (useLazyReturnType || isBaseTypeReference) {
 
82
                                                if (reference.IsGlobal) {
 
83
                                                        t = new GetClassReturnType(projectContent, reference.Type, typeParameterCount);
 
84
                                                } else if (callingClass != null) {
 
85
                                                        SearchClassReturnType scrt = new SearchClassReturnType(projectContent, callingClass, caretLine, caretColumn, reference.Type, typeParameterCount);
 
86
                                                        if (isBaseTypeReference)
 
87
                                                                scrt.LookForInnerClassesInDeclaringClass = false;
 
88
                                                        t = scrt;
 
89
                                                }
 
90
                                        } else {
 
91
                                                IClass c;
 
92
                                                if (reference.IsGlobal) {
 
93
                                                        c = projectContent.GetClass(reference.Type, typeParameterCount);
 
94
                                                        t = (c != null) ? c.DefaultReturnType : null;
 
95
                                                } else if (callingClass != null) {
 
96
                                                        t = projectContent.SearchType(new SearchTypeRequest(reference.Type, typeParameterCount, callingClass, caretLine, caretColumn)).Result;
 
97
                                                }
 
98
                                                if (t == null) {
 
99
                                                        return null;
 
100
                                                }
 
101
                                        }
 
102
                                }
 
103
                        }
 
104
                        if (reference.GenericTypes.Count > 0) {
 
105
                                IReturnType[] para = new IReturnType[reference.GenericTypes.Count];
 
106
                                for (int i = 0; i < reference.GenericTypes.Count; ++i) {
 
107
                                        para[i] = CreateReturnType(reference.GenericTypes[i], callingClass, callingMember, caretLine, caretColumn, projectContent, options);
 
108
                                }
 
109
                                t = new ConstructedReturnType(t, para);
 
110
                        }
 
111
                        for (int i = 0; i < reference.PointerNestingLevel; i++) {
 
112
                                t = new PointerReturnType(t);
 
113
                        }
 
114
                        return WrapArray(projectContent, t, reference);
 
115
                }
 
116
                
 
117
                static IReturnType WrapArray(IProjectContent pc, IReturnType t, TypeReference reference)
 
118
                {
 
119
                        if (reference.IsArrayType) {
 
120
                                for (int i = reference.RankSpecifier.Length - 1; i >= 0; --i) {
 
121
                                        int dimensions = reference.RankSpecifier[i] + 1;
 
122
                                        if (dimensions > 0) {
 
123
                                                t = new ArrayReturnType(pc, t, dimensions);
 
124
                                        }
 
125
                                }
 
126
                        }
 
127
                        return t;
 
128
                }
 
129
        }
 
130
}