[Mono.CSharp] Simplify .sources structure
[mono.git] / mcs / class / corlib / System.Reflection.Emit / DerivedTypes.cs
1 //
2 // System.Reflection.Emit.DerivedTypes.cs
3 //
4 // Authors:
5 //      Rodrigo Kumpera <rkumpera@novell.com>
6 //
7 //
8 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if !FULL_AOT_RUNTIME
31 using System.Reflection;
32 using System.Reflection.Emit;
33 using System.Collections;
34 using System.Runtime.CompilerServices;
35 using System.Globalization;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Text;
39
40
41 namespace System.Reflection.Emit
42 {
43         [StructLayout (LayoutKind.Sequential)]
44         abstract partial class SymbolType
45         {
46                 internal Type m_baseType;
47
48                 internal SymbolType (Type elementType)
49                 {
50                         this.m_baseType = elementType;
51                 }
52
53                 internal abstract String FormatName (string elementName);
54
55                 protected override bool IsArrayImpl ()
56                 {
57                         return false;
58                 }
59
60                 protected override bool IsByRefImpl ()
61                 {
62                         return false;
63                 }
64
65                 protected override bool IsPointerImpl ()
66                 {
67                         return false;
68                 }
69
70                 public override Type MakeArrayType ()
71                 {
72                         return new ArrayType (this, 0);
73                 }
74
75                 public override Type MakeArrayType (int rank)
76                 {
77                         if (rank < 1)
78                                 throw new IndexOutOfRangeException ();
79                         return new ArrayType (this, rank);
80                 }
81
82                 public override Type MakeByRefType ()
83                 {
84                         return new ByRefType (this);
85                 }
86
87                 public override Type MakePointerType ()
88                 {
89                         return new PointerType (this);
90                 }
91
92                 public override string ToString ()
93                 {
94                         return FormatName (m_baseType.ToString ());
95                 }
96
97                 public override string AssemblyQualifiedName {
98                         get {
99                                 string fullName = FormatName (m_baseType.FullName);
100                                 if (fullName == null)
101                                         return null;
102                                 return fullName + ", " + m_baseType.Assembly.FullName;
103                         }
104                 }
105
106
107                 public override string FullName {
108                         get {
109                                 return FormatName (m_baseType.FullName);
110                         }
111                 }
112
113                 public override string Name {
114                         get {
115                                 return FormatName (m_baseType.Name);
116                         }
117                 }
118         
119                 public override Type UnderlyingSystemType {
120                         get {
121                                 return this;
122                         }
123                 }
124
125                 internal override bool IsUserType {
126                         get {
127                                 return m_baseType.IsUserType;
128                         }
129                 }
130
131                 // Called from the runtime to return the corresponding finished Type object
132                 internal override Type RuntimeResolve () {
133                         return InternalResolve ();
134                 }
135         }
136
137         [StructLayout (LayoutKind.Sequential)]
138         internal class ArrayType : SymbolType
139         {
140                 int rank;
141
142                 internal ArrayType (Type elementType, int rank) : base (elementType)
143                 {
144                         this.rank = rank;
145                 }
146
147                 internal int GetEffectiveRank ()
148                 {
149                         return rank;
150                 }
151
152                 internal override Type InternalResolve ()
153                 {
154                         Type et = m_baseType.InternalResolve ();
155                         if (rank == 0)
156                                 return et.MakeArrayType ();
157                         return et.MakeArrayType (rank);
158                 }
159
160                 internal override Type RuntimeResolve ()
161                 {
162                         Type et = m_baseType.RuntimeResolve ();
163                         if (rank == 0)
164                                 return et.MakeArrayType ();
165                         return et.MakeArrayType (rank);
166                 }
167
168                 protected override bool IsArrayImpl ()
169                 {
170                         return true;
171                 }
172
173                 public override int GetArrayRank ()
174                 {
175                         return (rank == 0) ? 1 : rank;
176                 }
177
178                 internal override String FormatName (string elementName)
179                 {
180                         if (elementName == null)
181                                 return null;
182                         StringBuilder sb = new StringBuilder (elementName);
183                         sb.Append ("[");
184                         for (int i = 1; i < rank; ++i)
185                                 sb.Append (",");
186                         if (rank == 1)
187                                 sb.Append ("*");
188                         sb.Append ("]");
189                         return sb.ToString ();
190                 }
191         }
192
193         [StructLayout (LayoutKind.Sequential)]
194         internal class ByRefType : SymbolType
195         {
196                 internal ByRefType (Type elementType) : base (elementType)
197                 {
198                 }
199
200                 internal override Type InternalResolve ()
201                 {
202                         return m_baseType.InternalResolve ().MakeByRefType (); 
203                 }
204
205                 protected override bool IsByRefImpl ()
206                 {
207                         return true;
208                 }
209
210                 internal override String FormatName (string elementName)
211                 {
212                         if (elementName == null)
213                                 return null;
214                         return elementName + "&";
215                 }
216
217                 public override Type MakeArrayType ()
218                 {
219                         throw new ArgumentException ("Cannot create an array type of a byref type");
220                 }
221
222                 public override Type MakeArrayType (int rank)
223                 {
224                         throw new ArgumentException ("Cannot create an array type of a byref type");
225                 }
226
227                 public override Type MakeByRefType ()
228                 {
229                         throw new ArgumentException ("Cannot create a byref type of an already byref type");
230                 }
231
232                 public override Type MakePointerType ()
233                 {
234                         throw new ArgumentException ("Cannot create a pointer type of a byref type");
235                 }
236         }
237
238         [StructLayout (LayoutKind.Sequential)]
239         internal class PointerType : SymbolType
240         {
241                 internal PointerType (Type elementType) : base (elementType)
242                 {
243                 }
244
245                 internal override Type InternalResolve ()
246                 {
247                         return m_baseType.InternalResolve ().MakePointerType (); 
248                 }
249
250                 protected override bool IsPointerImpl ()
251                 {
252                         return true;
253                 }
254
255                 internal override String FormatName (string elementName)
256                 {
257                         if (elementName == null)
258                                 return null;
259                         return elementName + "*";
260                 }
261         }
262
263 }
264 #endif