Merge pull request #3213 from henricm/fix-for-win-securestring-to-bstr
[mono.git] / mcs / class / Mono.CodeContracts / Mono.CodeContracts.Static.AST / TypeNode.cs
1 // 
2 // TypeNode.cs
3 // 
4 // Authors:
5 //      Alexander Chebaturkin (chebaturkin@gmail.com)
6 // 
7 // Copyright (C) 2011 Alexander Chebaturkin
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //  
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 // 
28
29 using System;
30 using System.Collections.Generic;
31 using System.Linq;
32 using Mono.Cecil;
33
34 namespace Mono.CodeContracts.Static.AST {
35         class TypeNode : Member, IEquatable<TypeNode> {
36                 private TypeNode base_type;
37                 private List<Method> methods;
38                 private List<TypeNode> nestedTypes;
39                 private List<Property> properties;
40
41                 protected TypeNode () : base (NodeType.TypeNode)
42                 {
43                 }
44
45                 protected TypeNode (NodeType nodeType)
46                         : base (nodeType)
47                 {
48                 }
49
50                 protected TypeNode (TypeReference typeReference) : this ()
51                 {
52                         TypeDefinition = typeReference as TypeDefinition ?? typeReference.Resolve ();
53                 }
54
55                 public TypeDefinition TypeDefinition { get; set; }
56
57                 public IEnumerable<TypeNode> Interfaces
58                 {
59                         get
60                         {
61                                 if (TypeDefinition == null)
62                                         return null;
63                                 return TypeDefinition.Interfaces.Select (i => new TypeNode (i.InterfaceType));
64                         }
65                 }
66
67                 public TypeNode BaseType
68                 {
69                         get
70                         {
71                                 if (this.base_type == null && TypeDefinition != null)
72                                         this.base_type = new TypeNode (TypeDefinition.BaseType);
73                                 return this.base_type;
74                         }
75                         set { this.base_type = value; }
76                 }
77
78                 public virtual string FullName
79                 {
80                         get { return TypeDefinition == null ? "<null>" : TypeDefinition.FullName; }
81                 }
82
83                 public List<Property> Properties
84                 {
85                         get
86                         {
87                                 if (this.properties == null)
88                                         this.properties = TypeDefinition.Properties.Select (it => new Property (it)).ToList ();
89                                 return this.properties;
90                         }
91                         set { this.properties = value; }
92                 }
93
94                 public List<Method> Methods
95                 {
96                         get
97                         {
98                                 if (this.methods == null)
99                                         this.methods = TypeDefinition.Methods.Select (it => new Method (it)).ToList ();
100                                 return this.methods;
101                         }
102                         set { this.methods = value; }
103                 }
104
105                 public List<TypeNode> NestedTypes
106                 {
107                         get
108                         {
109                                 if (this.nestedTypes == null)
110                                         this.nestedTypes = TypeDefinition.NestedTypes.Select (it => new TypeNode (it)).ToList ();
111                                 return this.nestedTypes;
112                         }
113                         set { this.nestedTypes = value; }
114                 }
115
116                 public virtual string Name
117                 {
118                         get { return TypeDefinition.Name; }
119                 }
120
121                 public static TypeNode Create (TypeReference typeReference)
122                 {
123                         TypeDefinition typeDefinition = typeReference.Resolve ();
124                         if (typeDefinition == null)
125                                 return null;
126                         if (typeDefinition.IsClass)
127                                 return new Class (typeDefinition);
128
129                         return new TypeNode (typeDefinition);
130                 }
131
132                 public bool IsAssignableTo (TypeNode targetType)
133                 {
134                         if (this == CoreSystemTypes.Instance.TypeVoid)
135                                 return false;
136                         if (targetType == this)
137                                 return true;
138                         if (this == CoreSystemTypes.Instance.TypeObject)
139                                 return false;
140                         if (targetType == CoreSystemTypes.Instance.TypeObject || BaseType.IsAssignableTo (targetType))
141                                 return true;
142                         IEnumerable<TypeNode> interfaces = Interfaces;
143                         if (interfaces == null || !interfaces.Any ())
144                                 return false;
145                         foreach (TypeNode iface in interfaces) {
146                                 if (iface != null && iface.IsAssignableTo (targetType))
147                                         return true;
148                         }
149                         return false;
150                 }
151
152                 public TypeNode GetReferenceType ()
153                 {
154                         return new Reference (this);
155                 }
156
157                 public override string ToString ()
158                 {
159                         return string.Format ("Type({0})", FullName);
160                 }
161
162                 public TypeNode SelfInstantiation ()
163                 {
164                         //todo: implement this for generic
165                         return this;
166                 }
167
168                 public TypeNode GetArrayType (int rank)
169                 {
170                         return new ArrayTypeNode (this, 0, rank);
171                 }
172
173                 #region Implementation of IEquatable<TypeNode>
174                 public bool Equals (TypeNode other)
175                 {
176                         return TypeDefinition == other.TypeDefinition;
177                 }
178
179                 public override int GetHashCode ()
180                 {
181                         return TypeDefinition.GetHashCode ();
182                 }
183
184                 public override bool Equals (object obj)
185                 {
186                         return Equals (obj as TypeNode);
187                 }
188                 #endregion
189
190                 #region Overrides of Member
191                 private int classSize;
192                 private bool classSizeSpecified;
193
194                 public override bool IsStatic
195                 {
196                         get { return false; }
197                 }
198
199                 public override TypeNode DeclaringType
200                 {
201                         get { return Create (TypeDefinition.DeclaringType); }
202                 }
203
204                 public override Module Module
205                 {
206                         get
207                         {
208                                 if (TypeDefinition == null)
209                                         return null;
210                                 return new Module (TypeDefinition.Module);
211                         }
212                 }
213
214                 public override bool IsPublic
215                 {
216                         get { return TypeDefinition != null && TypeDefinition.IsPublic; }
217                 }
218
219                 public override bool IsAssembly
220                 {
221                         get { return TypeDefinition != null && TypeDefinition.IsNotPublic; }
222                 }
223
224                 public override bool IsPrivate
225                 {
226                         get { return false; }
227                 }
228
229                 public override bool IsFamily
230                 {
231                         get { return false; }
232                 }
233
234                 public override bool IsFamilyOrAssembly
235                 {
236                         get { return IsFamily || IsAssembly; }
237                 }
238
239                 public override bool IsFamilyAndAssembly
240                 {
241                         get { return IsFamily && IsAssembly; }
242                 }
243
244                 public virtual bool IsValueType
245                 {
246                         get { return TypeDefinition != null && TypeDefinition.IsValueType; }
247                 }
248
249                 public virtual bool IsStruct
250                 {
251                         get { return TypeDefinition != null && TypeDefinition.IsValueType; }
252                 }
253
254                 public virtual bool IsArray
255                 {
256                         get { return TypeDefinition != null && TypeDefinition.IsArray; }
257                 }
258
259                 public virtual bool IsInterface
260                 {
261                         get { return TypeDefinition != null && TypeDefinition.IsInterface; }
262                 }
263
264                 public virtual bool HasGenericParameters
265                 {
266                         get { return TypeDefinition != null && TypeDefinition.HasGenericParameters; }
267                 }
268
269                 public virtual bool IsNestedFamily
270                 {
271                         get { return TypeDefinition != null && TypeDefinition.IsNestedFamily; }
272                 }
273
274                 public virtual bool IsNestedPublic
275                 {
276                         get { return TypeDefinition != null && TypeDefinition.IsNestedPublic; }
277                 }
278
279                 public virtual bool IsNestedInternal
280                 {
281                         get { return TypeDefinition != null && TypeDefinition.IsNestedAssembly; }
282                 }
283
284                 public virtual bool IsNestedFamilyAndAssembly
285                 {
286                         get { return TypeDefinition != null && TypeDefinition.IsNestedFamilyAndAssembly; }
287                 }
288
289                 public virtual bool IsNestedAssembly
290                 {
291                         get { return TypeDefinition != null && TypeDefinition.IsNestedAssembly; }
292                 }
293
294                 public virtual bool IsPrimitive
295                 {
296                         get { return TypeDefinition != null && TypeDefinition.IsPrimitive; }
297                 }
298
299                 public virtual bool IsEnum
300                 {
301                         get { return TypeDefinition != null && TypeDefinition.IsEnum; }
302                 }
303
304                 public virtual bool IsClass
305                 {
306                         get { return TypeDefinition != null && TypeDefinition.IsClass; }
307                 }
308
309                 public virtual IEnumerable<Field> Fields
310                 {
311                         get
312                         {
313                                 if (TypeDefinition == null)
314                                         return null;
315                                 return TypeDefinition.Fields.Select (it => new Field (it));
316                         }
317                 }
318
319                 public int ClassSize
320                 {
321                         get
322                         {
323                                 if (!this.classSizeSpecified && TypeDefinition != null)
324                                         ClassSize = TypeDefinition.ClassSize;
325                                 return this.classSize;
326                         }
327                         set
328                         {
329                                 this.classSize = value;
330                                 this.classSizeSpecified = true;
331                         }
332                 }
333                 #endregion
334         }
335 }