merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / System / System.CodeDom / CodeTypeReference.cs
1 //
2 // System.CodeDom CodeTypeReferenceExpression Class implementation
3 //
4 // Author:
5 //   Daniel Stodden (stodden@in.tum.de)
6 //   Marek Safar (marek.safar@seznam.cz)
7 //
8 // (C) 2001 Ximian, Inc.
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Runtime.InteropServices;
33 using System.Text;
34
35 namespace System.CodeDom
36 {
37         [Serializable]
38         [ClassInterface(ClassInterfaceType.AutoDispatch)]
39         [ComVisible(true)]
40         public class CodeTypeReference : CodeObject
41         {
42                 private string baseType;
43                 private CodeTypeReference arrayType;
44                 private int rank;
45                 private bool isInterface;
46
47 #if NET_2_0
48                 CodeTypeReferenceCollection typeArguments;
49                 CodeTypeReferenceOptions codeTypeReferenceOption;
50 #endif
51
52                 //
53                 // Constructors
54                 //
55
56 #if NET_2_0
57                 public CodeTypeReference ()
58                 {
59                 }
60 #endif
61
62 #if NET_2_0
63                 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
64 #endif
65                 public CodeTypeReference (string baseType)
66                 {
67                         Parse (baseType);
68                 }
69
70 #if NET_2_0
71                 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
72 #endif
73                 public CodeTypeReference (Type baseType)
74                 {
75 #if NET_2_0
76                         if (baseType == null) {
77                                 throw new ArgumentNullException ("baseType");
78                         }
79 #endif
80                         if (baseType.IsArray) {
81                                 this.rank = baseType.GetArrayRank ();
82                                 this.arrayType = new CodeTypeReference (baseType.GetElementType ());
83                                 this.baseType = arrayType.BaseType;
84                         } else {
85                                 Parse (baseType.FullName);
86                         }
87                         this.isInterface = baseType.IsInterface;
88                 }
89
90                 public CodeTypeReference( CodeTypeReference arrayType, int rank )
91                 {
92                         this.baseType = null;
93                         this.rank = rank;
94                         this.arrayType = arrayType;
95                 }
96
97 #if NET_2_0
98                 [MonoTODO("We should parse basetype from right to left in 2.0 profile.")]
99 #endif
100                 public CodeTypeReference( string baseType, int rank )
101                         : this (new CodeTypeReference (baseType), rank)
102                 {
103                 }
104
105 #if NET_2_0
106                 public CodeTypeReference( CodeTypeParameter typeParameter ) :
107                         this (typeParameter.Name)
108                 {
109                         this.codeTypeReferenceOption = CodeTypeReferenceOptions.GenericTypeParameter;
110                 }
111
112                 public CodeTypeReference( string typeName, CodeTypeReferenceOptions codeTypeReferenceOption ) :
113                         this (typeName)
114                 {
115                         this.codeTypeReferenceOption = codeTypeReferenceOption;
116                 }
117
118                 public CodeTypeReference( Type type, CodeTypeReferenceOptions codeTypeReferenceOption ) :
119                         this (type)
120                 {
121                         this.codeTypeReferenceOption = codeTypeReferenceOption;
122                 }
123
124                 public CodeTypeReference( string typeName, params CodeTypeReference[] typeArguments ) :
125                         this (typeName)
126                 {
127                         TypeArguments.AddRange (typeArguments);
128                 }
129 #endif
130
131                 //
132                 // Properties
133                 //
134
135                 public CodeTypeReference ArrayElementType
136                 {
137                         get {
138                                 return arrayType;
139                         }
140                         set {
141                                 arrayType = value;
142                         }
143                 }
144                 
145                 public int ArrayRank {
146                         get {
147                                 return rank;
148                         }
149                         set {
150                                 rank = value;
151                         }
152                 }
153
154                 public string BaseType {
155                         get {
156                                 if (arrayType != null && rank > 0) {
157                                         return arrayType.BaseType;
158                                 }
159
160                                 if (baseType == null)
161                                         return String.Empty;
162
163                                 return baseType;
164                         }
165                         set {
166                                 baseType = value;
167                         }
168                 }
169
170                 internal bool IsInterface {
171                         get { return isInterface; }
172                 }
173
174                 private void Parse (string baseType)
175                 {
176                         if (baseType == null || baseType.Length == 0) {
177                                 this.baseType = typeof (void).FullName;
178                                 return;
179                         }
180
181 #if NET_2_0
182                         int array_start = baseType.IndexOf ('[');
183                         if (array_start == -1) {
184                                 this.baseType = baseType;
185                                 return;
186                         }
187
188                         int array_end = baseType.LastIndexOf (']');
189                         if (array_end < array_start) {
190                                 this.baseType = baseType;
191                                 return;
192                         }
193
194                         string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
195
196                         if ((array_end - array_start) != args.Length) {
197                                 this.baseType = baseType.Substring (0, array_start);
198                                 int escapeCount = 0;
199                                 int scanPos = array_start;
200                                 StringBuilder tb = new StringBuilder();
201                                 while (scanPos < baseType.Length) {
202                                         char currentChar = baseType[scanPos];
203                                         
204                                         switch (currentChar) {
205                                                 case '[':
206                                                         if (escapeCount > 1 && tb.Length > 0) {
207                                                                 tb.Append (currentChar);
208                                                         }
209                                                         escapeCount++;
210                                                         break;
211                                                 case ']':
212                                                         escapeCount--;
213                                                         if (escapeCount > 1 && tb.Length > 0) {
214                                                                 tb.Append (currentChar);
215                                                         }
216
217                                                         if (tb.Length != 0 && (escapeCount % 2) == 0) {
218                                                                 TypeArguments.Add (tb.ToString ());
219                                                                 tb.Length = 0;
220                                                         }
221                                                         break;
222                                                 case ',':
223                                                         if (escapeCount > 1) {
224                                                                 // skip anything after the type name until we 
225                                                                 // reach the next separator
226                                                                 while (scanPos + 1 < baseType.Length) {
227                                                                         if (baseType[scanPos + 1] == ']') {
228                                                                                 break;
229                                                                         }
230                                                                         scanPos++;
231                                                                 }
232                                                         } else if (tb.Length > 0) {
233                                                                 CodeTypeReference typeArg = new CodeTypeReference (tb.ToString ());
234                                                                 TypeArguments.Add (typeArg);
235                                                                 tb.Length = 0;
236                                                         }
237                                                         break;
238                                                 default:
239                                                         tb.Append (currentChar);
240                                                         break;
241                                         }
242                                         scanPos++;
243                                 }
244                         } else {
245                                 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
246                                 rank = args.Length;
247                         }
248 #else
249                         int array_start = baseType.LastIndexOf ('[');
250                         if (array_start == -1) {
251                                 this.baseType = baseType;
252                                 return;
253                         }
254
255                         int array_end = baseType.LastIndexOf (']');
256                         if (array_end < array_start) {
257                                 this.baseType = baseType;
258                                 return;
259                         }
260
261                         string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
262
263                         bool isArray = true;
264                         foreach (string arg in args) {
265                                 if (arg.Length != 0) {
266                                         isArray = false;
267                                         break;
268                                 }
269                         }
270                         if (isArray) {
271                                 arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
272                                 rank = args.Length;
273                         } else {
274                                 this.baseType = baseType;
275                         }
276 #endif
277                 }
278
279 #if NET_2_0
280                 [ComVisible (false)]
281                 public CodeTypeReferenceOptions Options {
282                         get {
283                                 return codeTypeReferenceOption;
284                         }
285                         set {
286                                 codeTypeReferenceOption = value;
287                         }
288                 }
289
290                 [ComVisible (false)]
291                 public CodeTypeReferenceCollection TypeArguments {
292                         get {
293                                 if (typeArguments == null)
294                                         typeArguments = new CodeTypeReferenceCollection ();
295                                 return typeArguments;
296                         }
297                 }
298 #endif
299
300         }
301 }