1 | // |
---|
2 | // FullTypeName.cs |
---|
3 | // |
---|
4 | // Author: |
---|
5 | // Mike Krüger <mkrueger@novell.com> |
---|
6 | // |
---|
7 | // Copyright (c) 2010 Novell, Inc (http://www.novell.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 | |
---|
27 | using System; |
---|
28 | using System.Collections.Generic; |
---|
29 | using System.Linq; |
---|
30 | using System.Text; |
---|
31 | using ICSharpCode.NRefactory.CSharp.TypeSystem; |
---|
32 | using ICSharpCode.NRefactory.TypeSystem; |
---|
33 | |
---|
34 | namespace ICSharpCode.NRefactory.CSharp |
---|
35 | { |
---|
36 | public class MemberType : AstType |
---|
37 | { |
---|
38 | public static readonly Role<AstType> TargetRole = new Role<AstType>("Target", AstType.Null); |
---|
39 | |
---|
40 | bool isDoubleColon; |
---|
41 | |
---|
42 | public bool IsDoubleColon { |
---|
43 | get { return isDoubleColon; } |
---|
44 | set { |
---|
45 | ThrowIfFrozen(); |
---|
46 | isDoubleColon = value; |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | public AstType Target { |
---|
51 | get { return GetChildByRole(TargetRole); } |
---|
52 | set { SetChildByRole(TargetRole, value); } |
---|
53 | } |
---|
54 | |
---|
55 | public string MemberName { |
---|
56 | get { |
---|
57 | return GetChildByRole (Roles.Identifier).Name; |
---|
58 | } |
---|
59 | set { |
---|
60 | SetChildByRole (Roles.Identifier, Identifier.Create (value)); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | public Identifier MemberNameToken { |
---|
65 | get { |
---|
66 | return GetChildByRole (Roles.Identifier); |
---|
67 | } |
---|
68 | set { |
---|
69 | SetChildByRole (Roles.Identifier, value); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | public AstNodeCollection<AstType> TypeArguments { |
---|
74 | get { return GetChildrenByRole (Roles.TypeArgument); } |
---|
75 | } |
---|
76 | |
---|
77 | public MemberType () |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | public MemberType (AstType target, string memberName) |
---|
82 | { |
---|
83 | this.Target = target; |
---|
84 | this.MemberName = memberName; |
---|
85 | } |
---|
86 | |
---|
87 | public MemberType (AstType target, string memberName, IEnumerable<AstType> typeArguments) |
---|
88 | { |
---|
89 | this.Target = target; |
---|
90 | this.MemberName = memberName; |
---|
91 | foreach (var arg in typeArguments) { |
---|
92 | AddChild (arg, Roles.TypeArgument); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | public MemberType (AstType target, string memberName, params AstType[] typeArguments) : this (target, memberName, (IEnumerable<AstType>)typeArguments) |
---|
97 | { |
---|
98 | } |
---|
99 | |
---|
100 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
101 | { |
---|
102 | visitor.VisitMemberType (this); |
---|
103 | } |
---|
104 | |
---|
105 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
106 | { |
---|
107 | return visitor.VisitMemberType (this); |
---|
108 | } |
---|
109 | |
---|
110 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
111 | { |
---|
112 | return visitor.VisitMemberType (this, data); |
---|
113 | } |
---|
114 | |
---|
115 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
116 | { |
---|
117 | MemberType o = other as MemberType; |
---|
118 | return o != null && this.IsDoubleColon == o.IsDoubleColon |
---|
119 | && MatchString(this.MemberName, o.MemberName) && this.Target.DoMatch(o.Target, match) |
---|
120 | && this.TypeArguments.DoMatch(o.TypeArguments, match); |
---|
121 | } |
---|
122 | |
---|
123 | public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider = null) |
---|
124 | { |
---|
125 | if (interningProvider == null) |
---|
126 | interningProvider = InterningProvider.Dummy; |
---|
127 | |
---|
128 | TypeOrNamespaceReference t; |
---|
129 | if (this.IsDoubleColon) { |
---|
130 | SimpleType st = this.Target as SimpleType; |
---|
131 | if (st != null) { |
---|
132 | t = interningProvider.Intern(new AliasNamespaceReference(interningProvider.Intern(st.Identifier))); |
---|
133 | } else { |
---|
134 | t = null; |
---|
135 | } |
---|
136 | } else { |
---|
137 | t = this.Target.ToTypeReference(lookupMode, interningProvider) as TypeOrNamespaceReference; |
---|
138 | } |
---|
139 | if (t == null) |
---|
140 | return SpecialType.UnknownType; |
---|
141 | var typeArguments = new List<ITypeReference>(); |
---|
142 | foreach (var ta in this.TypeArguments) { |
---|
143 | typeArguments.Add(ta.ToTypeReference(lookupMode, interningProvider)); |
---|
144 | } |
---|
145 | string memberName = interningProvider.Intern(this.MemberName); |
---|
146 | return interningProvider.Intern(new MemberTypeOrNamespaceReference(t, memberName, interningProvider.InternList(typeArguments), lookupMode)); |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|