1 | // Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.Collections.Generic; |
---|
21 | using System.Collections.ObjectModel; |
---|
22 | |
---|
23 | using ICSharpCode.NRefactory.CSharp.Resolver; |
---|
24 | using ICSharpCode.NRefactory.Semantics; |
---|
25 | using ICSharpCode.NRefactory.TypeSystem; |
---|
26 | using ICSharpCode.NRefactory.TypeSystem.Implementation; |
---|
27 | using ICSharpCode.NRefactory.Utils; |
---|
28 | |
---|
29 | namespace ICSharpCode.NRefactory.CSharp.TypeSystem |
---|
30 | { |
---|
31 | /// <summary> |
---|
32 | /// Represents a simple C# name. (a single non-qualified identifier with an optional list of type arguments) |
---|
33 | /// </summary> |
---|
34 | [Serializable] |
---|
35 | public sealed class SimpleTypeOrNamespaceReference : TypeOrNamespaceReference, ISupportsInterning |
---|
36 | { |
---|
37 | readonly string identifier; |
---|
38 | readonly IList<ITypeReference> typeArguments; |
---|
39 | readonly NameLookupMode lookupMode; |
---|
40 | |
---|
41 | public SimpleTypeOrNamespaceReference(string identifier, IList<ITypeReference> typeArguments, NameLookupMode lookupMode = NameLookupMode.Type) |
---|
42 | { |
---|
43 | if (identifier == null) |
---|
44 | throw new ArgumentNullException("identifier"); |
---|
45 | this.identifier = identifier; |
---|
46 | this.typeArguments = typeArguments ?? EmptyList<ITypeReference>.Instance; |
---|
47 | this.lookupMode = lookupMode; |
---|
48 | } |
---|
49 | |
---|
50 | public string Identifier { |
---|
51 | get { return identifier; } |
---|
52 | } |
---|
53 | |
---|
54 | public IList<ITypeReference> TypeArguments { |
---|
55 | get { return typeArguments; } |
---|
56 | } |
---|
57 | |
---|
58 | public NameLookupMode LookupMode { |
---|
59 | get { return lookupMode; } |
---|
60 | } |
---|
61 | |
---|
62 | /// <summary> |
---|
63 | /// Adds a suffix to the identifier. |
---|
64 | /// Does not modify the existing type reference, but returns a new one. |
---|
65 | /// </summary> |
---|
66 | public SimpleTypeOrNamespaceReference AddSuffix(string suffix) |
---|
67 | { |
---|
68 | return new SimpleTypeOrNamespaceReference(identifier + suffix, typeArguments, lookupMode); |
---|
69 | } |
---|
70 | |
---|
71 | public override ResolveResult Resolve(CSharpResolver resolver) |
---|
72 | { |
---|
73 | var typeArgs = typeArguments.Resolve(resolver.CurrentTypeResolveContext); |
---|
74 | return resolver.LookupSimpleNameOrTypeName(identifier, typeArgs, lookupMode); |
---|
75 | } |
---|
76 | |
---|
77 | public override IType ResolveType(CSharpResolver resolver) |
---|
78 | { |
---|
79 | TypeResolveResult trr = Resolve(resolver) as TypeResolveResult; |
---|
80 | return trr != null ? trr.Type : new UnknownType(null, identifier, typeArguments.Count); |
---|
81 | } |
---|
82 | |
---|
83 | public override string ToString() |
---|
84 | { |
---|
85 | if (typeArguments.Count == 0) |
---|
86 | return identifier; |
---|
87 | else |
---|
88 | return identifier + "<" + string.Join(",", typeArguments) + ">"; |
---|
89 | } |
---|
90 | |
---|
91 | int ISupportsInterning.GetHashCodeForInterning() |
---|
92 | { |
---|
93 | int hashCode = 0; |
---|
94 | unchecked { |
---|
95 | hashCode += 1000000021 * identifier.GetHashCode(); |
---|
96 | hashCode += 1000000033 * typeArguments.GetHashCode(); |
---|
97 | hashCode += 1000000087 * (int)lookupMode; |
---|
98 | } |
---|
99 | return hashCode; |
---|
100 | } |
---|
101 | |
---|
102 | bool ISupportsInterning.EqualsForInterning(ISupportsInterning other) |
---|
103 | { |
---|
104 | SimpleTypeOrNamespaceReference o = other as SimpleTypeOrNamespaceReference; |
---|
105 | return o != null && this.identifier == o.identifier |
---|
106 | && this.typeArguments == o.typeArguments && this.lookupMode == o.lookupMode; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|