1 | // |
---|
2 | // ComposedType.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 | using System; |
---|
27 | using System.Collections.Generic; |
---|
28 | using System.Linq; |
---|
29 | using System.Text; |
---|
30 | using ICSharpCode.NRefactory.TypeSystem; |
---|
31 | |
---|
32 | namespace ICSharpCode.NRefactory.CSharp |
---|
33 | { |
---|
34 | public class ComposedType : AstType |
---|
35 | { |
---|
36 | public static readonly TokenRole NullableRole = new TokenRole("?"); |
---|
37 | public static readonly TokenRole PointerRole = new TokenRole("*"); |
---|
38 | public static readonly Role<ArraySpecifier> ArraySpecifierRole = new Role<ArraySpecifier>("ArraySpecifier"); |
---|
39 | |
---|
40 | public AstType BaseType { |
---|
41 | get { return GetChildByRole(Roles.Type); } |
---|
42 | set { SetChildByRole(Roles.Type, value); } |
---|
43 | } |
---|
44 | |
---|
45 | public bool HasNullableSpecifier { |
---|
46 | get { |
---|
47 | return !GetChildByRole(NullableRole).IsNull; |
---|
48 | } |
---|
49 | set { |
---|
50 | SetChildByRole(NullableRole, value ? new CSharpTokenNode(TextLocation.Empty, null) : null); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | public CSharpTokenNode NullableSpecifierToken { |
---|
55 | get { |
---|
56 | return GetChildByRole(NullableRole); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | public int PointerRank { |
---|
61 | get { |
---|
62 | return GetChildrenByRole(PointerRole).Count; |
---|
63 | } |
---|
64 | set { |
---|
65 | if (value < 0) |
---|
66 | throw new ArgumentOutOfRangeException(); |
---|
67 | int d = this.PointerRank; |
---|
68 | while (d > value) { |
---|
69 | GetChildByRole(PointerRole).Remove(); |
---|
70 | d--; |
---|
71 | } |
---|
72 | while (d < value) { |
---|
73 | InsertChildBefore(GetChildByRole(PointerRole), new CSharpTokenNode(TextLocation.Empty, PointerRole), PointerRole); |
---|
74 | d++; |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | public AstNodeCollection<ArraySpecifier> ArraySpecifiers { |
---|
80 | get { return GetChildrenByRole (ArraySpecifierRole); } |
---|
81 | } |
---|
82 | |
---|
83 | public AstNodeCollection<CSharpTokenNode> PointerTokens { |
---|
84 | get { return GetChildrenByRole (PointerRole); } |
---|
85 | } |
---|
86 | |
---|
87 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
88 | { |
---|
89 | visitor.VisitComposedType (this); |
---|
90 | } |
---|
91 | |
---|
92 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
93 | { |
---|
94 | return visitor.VisitComposedType (this); |
---|
95 | } |
---|
96 | |
---|
97 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
98 | { |
---|
99 | return visitor.VisitComposedType (this, data); |
---|
100 | } |
---|
101 | |
---|
102 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
103 | { |
---|
104 | ComposedType o = other as ComposedType; |
---|
105 | return o != null && this.HasNullableSpecifier == o.HasNullableSpecifier && this.PointerRank == o.PointerRank |
---|
106 | && this.BaseType.DoMatch(o.BaseType, match) |
---|
107 | && this.ArraySpecifiers.DoMatch(o.ArraySpecifiers, match); |
---|
108 | } |
---|
109 | |
---|
110 | public override string ToString(CSharpFormattingOptions formattingOptions) |
---|
111 | { |
---|
112 | StringBuilder b = new StringBuilder(); |
---|
113 | b.Append(this.BaseType.ToString()); |
---|
114 | if (this.HasNullableSpecifier) |
---|
115 | b.Append('?'); |
---|
116 | b.Append('*', this.PointerRank); |
---|
117 | foreach (var arraySpecifier in this.ArraySpecifiers) { |
---|
118 | b.Append('['); |
---|
119 | b.Append(',', arraySpecifier.Dimensions - 1); |
---|
120 | b.Append(']'); |
---|
121 | } |
---|
122 | return b.ToString(); |
---|
123 | } |
---|
124 | |
---|
125 | public override AstType MakePointerType() |
---|
126 | { |
---|
127 | if (ArraySpecifiers.Any()) { |
---|
128 | return base.MakePointerType(); |
---|
129 | } else { |
---|
130 | this.PointerRank++; |
---|
131 | return this; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | public override AstType MakeArrayType(int dimensions) |
---|
136 | { |
---|
137 | InsertChildBefore(this.ArraySpecifiers.FirstOrDefault(), new ArraySpecifier(dimensions), ArraySpecifierRole); |
---|
138 | return this; |
---|
139 | } |
---|
140 | |
---|
141 | public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider = null) |
---|
142 | { |
---|
143 | if (interningProvider == null) |
---|
144 | interningProvider = InterningProvider.Dummy; |
---|
145 | ITypeReference t = this.BaseType.ToTypeReference(lookupMode, interningProvider); |
---|
146 | if (this.HasNullableSpecifier) { |
---|
147 | t = interningProvider.Intern(NullableType.Create(t)); |
---|
148 | } |
---|
149 | int pointerRank = this.PointerRank; |
---|
150 | for (int i = 0; i < pointerRank; i++) { |
---|
151 | t = interningProvider.Intern(new PointerTypeReference(t)); |
---|
152 | } |
---|
153 | foreach (var a in this.ArraySpecifiers.Reverse()) { |
---|
154 | t = interningProvider.Intern(new ArrayTypeReference(t, a.Dimensions)); |
---|
155 | } |
---|
156 | return t; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /// <summary> |
---|
161 | /// [,,,] |
---|
162 | /// </summary> |
---|
163 | public class ArraySpecifier : AstNode |
---|
164 | { |
---|
165 | public override NodeType NodeType { |
---|
166 | get { |
---|
167 | return NodeType.Unknown; |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | public ArraySpecifier() |
---|
172 | { |
---|
173 | } |
---|
174 | |
---|
175 | public ArraySpecifier(int dimensions) |
---|
176 | { |
---|
177 | this.Dimensions = dimensions; |
---|
178 | } |
---|
179 | |
---|
180 | public CSharpTokenNode LBracketToken { |
---|
181 | get { return GetChildByRole (Roles.LBracket); } |
---|
182 | } |
---|
183 | |
---|
184 | public int Dimensions { |
---|
185 | get { return 1 + GetChildrenByRole(Roles.Comma).Count; } |
---|
186 | set { |
---|
187 | int d = this.Dimensions; |
---|
188 | while (d > value) { |
---|
189 | GetChildByRole(Roles.Comma).Remove(); |
---|
190 | d--; |
---|
191 | } |
---|
192 | while (d < value) { |
---|
193 | InsertChildBefore(GetChildByRole(Roles.Comma), new CSharpTokenNode(TextLocation.Empty, Roles.Comma), Roles.Comma); |
---|
194 | d++; |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | public CSharpTokenNode RBracketToken { |
---|
200 | get { return GetChildByRole (Roles.RBracket); } |
---|
201 | } |
---|
202 | |
---|
203 | public override void AcceptVisitor (IAstVisitor visitor) |
---|
204 | { |
---|
205 | visitor.VisitArraySpecifier (this); |
---|
206 | } |
---|
207 | |
---|
208 | public override T AcceptVisitor<T> (IAstVisitor<T> visitor) |
---|
209 | { |
---|
210 | return visitor.VisitArraySpecifier (this); |
---|
211 | } |
---|
212 | |
---|
213 | public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data) |
---|
214 | { |
---|
215 | return visitor.VisitArraySpecifier(this, data); |
---|
216 | } |
---|
217 | |
---|
218 | protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
---|
219 | { |
---|
220 | ArraySpecifier o = other as ArraySpecifier; |
---|
221 | return o != null && this.Dimensions == o.Dimensions; |
---|
222 | } |
---|
223 | |
---|
224 | public override string ToString(CSharpFormattingOptions formattingOptions) |
---|
225 | { |
---|
226 | return "[" + new string(',', this.Dimensions - 1) + "]"; |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|