Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TypeDesc.cs « Common « TypeSystem « src « Common « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9110d148e1d123a1555b2b5071ee79c71254d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Internal.TypeSystem
{
    /// <summary>
    /// Represents the fundamental base type of all types within the type system.
    /// </summary>
    public abstract partial class TypeDesc : TypeSystemEntity
    {
        public static readonly TypeDesc[] EmptyTypes = new TypeDesc[0];

        /// Inherited types are required to override, and should use the algorithms
        /// in TypeHashingAlgorithms in their implementation.
        public abstract override int GetHashCode();

        public override bool Equals(Object o)
        {
            // Its only valid to compare two TypeDescs in the same context
            Debug.Assert(o == null || !(o is TypeDesc) || Object.ReferenceEquals(((TypeDesc)o).Context, this.Context));
            return Object.ReferenceEquals(this, o);
        }

#if DEBUG
        public static bool operator ==(TypeDesc left, TypeDesc right)
        {
            // Its only valid to compare two TypeDescs in the same context
            Debug.Assert(Object.ReferenceEquals(left, null) || Object.ReferenceEquals(right, null) || Object.ReferenceEquals(left.Context, right.Context));
            return Object.ReferenceEquals(left, right);
        }

        public static bool operator !=(TypeDesc left, TypeDesc right)
        {
            // Its only valid to compare two TypeDescs in the same context
            Debug.Assert(Object.ReferenceEquals(left, null) || Object.ReferenceEquals(right, null) || Object.ReferenceEquals(left.Context, right.Context));
            return !Object.ReferenceEquals(left, right);
        }
#endif

        // The most frequently used type properties are cached here to avoid excesive virtual calls
        private TypeFlags _typeFlags;

        /// <summary>
        /// Gets the generic instantiation information of this type.
        /// For generic definitions, retrieves the generic parameters of the type.
        /// For generic instantiation, retrieves the generic arguments of the type.
        /// </summary>
        public virtual Instantiation Instantiation
        {
            get
            {
                return Instantiation.Empty;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this type has a generic instantiation.
        /// This will be true for generic type instantiations and generic definitions.
        /// </summary>
        public bool HasInstantiation
        {
            get
            {
                return this.Instantiation.Length != 0;
            }
        }

        internal void SetWellKnownType(WellKnownType wellKnownType)
        {
            TypeFlags flags;

            switch (wellKnownType)
            {
                case WellKnownType.Void:
                case WellKnownType.Boolean:
                case WellKnownType.Char:
                case WellKnownType.SByte:
                case WellKnownType.Byte:
                case WellKnownType.Int16:
                case WellKnownType.UInt16:
                case WellKnownType.Int32:
                case WellKnownType.UInt32:
                case WellKnownType.Int64:
                case WellKnownType.UInt64:
                case WellKnownType.IntPtr:
                case WellKnownType.UIntPtr:
                case WellKnownType.Single:
                case WellKnownType.Double:
                    flags = (TypeFlags)wellKnownType;
                    break;

                case WellKnownType.ValueType:
                case WellKnownType.Enum:
                    flags = TypeFlags.Class;
                    break;

                case WellKnownType.Nullable:
                    flags = TypeFlags.Nullable;
                    break;

                case WellKnownType.Object:
                case WellKnownType.String:
                case WellKnownType.Array:
                case WellKnownType.MulticastDelegate:
                case WellKnownType.Exception:
                    flags = TypeFlags.Class;
                    break;

                case WellKnownType.RuntimeTypeHandle:
                case WellKnownType.RuntimeMethodHandle:
                case WellKnownType.RuntimeFieldHandle:
                case WellKnownType.TypedReference:
                case WellKnownType.ByReferenceOfT:
                    flags = TypeFlags.ValueType;
                    break;

                default:
                    throw new ArgumentException();
            }

            _typeFlags = flags;
        }

        protected abstract TypeFlags ComputeTypeFlags(TypeFlags mask);

        [MethodImpl(MethodImplOptions.NoInlining)]
        private TypeFlags InitializeTypeFlags(TypeFlags mask)
        {
            TypeFlags flags = ComputeTypeFlags(mask);

            if ((flags & mask) == 0)
                flags = Context.ComputeTypeFlags(this, flags, mask);

            Debug.Assert((flags & mask) != 0);
            _typeFlags |= flags;

            return flags & mask;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        protected internal TypeFlags GetTypeFlags(TypeFlags mask)
        {
            TypeFlags flags = _typeFlags & mask;
            if (flags != 0)
                return flags;
            return InitializeTypeFlags(mask);
        }

        /// <summary>
        /// Retrieves the category of the type. This is one of the possible values of
        /// <see cref="TypeFlags"/> less than <see cref="TypeFlags.CategoryMask"/>.
        /// </summary>
        public TypeFlags Category
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this type is an interface type.
        /// </summary>
        public bool IsInterface
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask) == TypeFlags.Interface;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this type is a value type (not a reference type).
        /// </summary>
        public bool IsValueType
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask) < TypeFlags.Class;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is one of the primitive types (boolean, char, void,
        /// a floating point, or an integer type).
        /// </summary>
        public bool IsPrimitive
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask) < TypeFlags.ValueType;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is an enum type.
        /// Access <see cref="UnderlyingType"/> to retrieve the underlying integral type.
        /// </summary>
        public bool IsEnum
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask) == TypeFlags.Enum;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a delegate type.
        /// </summary>
        public bool IsDelegate
        {
            get
            {
                var baseType = this.BaseType;
                return (baseType != null) ? baseType.IsWellKnownType(WellKnownType.MulticastDelegate) : false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is System.Void type.
        /// </summary>
        public bool IsVoid
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask) == TypeFlags.Void;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is System.String type.
        /// </summary>
        public bool IsString
        {
            get
            {
                return this.IsWellKnownType(WellKnownType.String);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is System.Object type.
        /// </summary>
        public bool IsObject
        {
            get
            {
                return this.IsWellKnownType(WellKnownType.Object);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a generic definition, or
        /// an instance of System.Nullable`1.
        /// </summary>
        public bool IsNullable
        {
            get
            {
                return this.GetTypeDefinition().IsWellKnownType(WellKnownType.Nullable);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a generic definition, or
        /// an instance of System.ByReference`1.
        /// </summary>
        public bool IsByReferenceOfT
        {
            get
            {
                return this.GetTypeDefinition().IsWellKnownType(WellKnownType.ByReferenceOfT);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is an array type (<see cref="ArrayType"/>).
        /// Note this will return true for both multidimensional array types and vector types.
        /// Use <see cref="IsSzArray"/> to check for vector types.
        /// </summary>
        public bool IsArray
        {
            get
            {
                return this is ArrayType;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a vector type. A vector is a single-dimensional
        /// array with a zero lower bound. To check for arrays in general, use <see cref="IsArray"/>.
        /// </summary>
        public bool IsSzArray
        {
            get
            {
                return this.IsArray && ((ArrayType)this).IsSzArray;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a non-vector array type.
        /// To check for arrays in general, use <see cref="IsArray"/>.
        /// </summary>
        public bool IsMdArray
        {
            get
            {
                return this.IsArray && ((ArrayType)this).IsMdArray;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a managed pointer type (<see cref="ByRefType"/>).
        /// </summary>
        public bool IsByRef
        {
            get
            {
                return this is ByRefType;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is an unmanaged pointer type (<see cref="PointerType"/>).
        /// </summary>
        public bool IsPointer
        {
            get
            {
                return this is PointerType;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is an unmanaged function pointer type (<see cref="FunctionPointerType"/>).
        /// </summary>
        public bool IsFunctionPointer
        {
            get
            {
                return this is FunctionPointerType;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a <see cref="SignatureTypeVariable"/> or <see cref="SignatureMethodVariable"/>.
        /// </summary>
        public bool IsSignatureVariable
        {
            get
            {
                return this is SignatureTypeVariable || this is SignatureMethodVariable;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a generic parameter (<see cref="GenericParameterDesc"/>).
        /// </summary>
        public bool IsGenericParameter
        {
            get
            {
                return GetTypeFlags(TypeFlags.CategoryMask) == TypeFlags.GenericParameter;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a pointer, byref, array, or szarray type,
        /// and can be used as a ParameterizedType.
        /// </summary>
        public bool IsParameterizedType
        {
            get
            {
                TypeFlags flags = GetTypeFlags(TypeFlags.CategoryMask);
                Debug.Assert((flags >= TypeFlags.Array && flags <= TypeFlags.Pointer) == (this is ParameterizedType));
                return (flags >= TypeFlags.Array && flags <= TypeFlags.Pointer);
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a class, an interface, a value type, or a
        /// generic instance of one of them.
        /// </summary>
        public bool IsDefType
        {
            get
            {
                Debug.Assert(GetTypeFlags(TypeFlags.CategoryMask) <= TypeFlags.Interface == this is DefType);
                return GetTypeFlags(TypeFlags.CategoryMask) <= TypeFlags.Interface;
            }
        }

        /// <summary>
        /// Gets a value indicating whether locations of this type refer to an object on the GC heap.
        /// </summary>
        public bool IsGCPointer
        {
            get
            {
                TypeFlags category = GetTypeFlags(TypeFlags.CategoryMask);
                return category == TypeFlags.Class
                    || category == TypeFlags.Array
                    || category == TypeFlags.SzArray
                    || category == TypeFlags.Interface;
            }
        }

        /// <summary>
        /// Gets the type from which this type derives from, or null if there's no such type.
        /// </summary>
        public virtual DefType BaseType
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this type has a base type.
        /// </summary>
        public bool HasBaseType
        {
            get
            {
                return BaseType != null;
            }
        }

        /// <summary>
        /// If this is an enum type, gets the underlying integral type of the enum type.
        /// For all other types, returns 'this'.
        /// </summary>
        public virtual TypeDesc UnderlyingType
        {
            get
            {
                if (!this.IsEnum)
                    return this;

                // TODO: Cache the result?
                foreach (var field in this.GetFields())
                {
                    if (!field.IsStatic)
                        return field.FieldType;
                }

                ThrowHelper.ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, this);
                return null; // Unreachable
            }
        }

        /// <summary>
        /// Gets a value indicating whether this type has a class constructor method.
        /// Use <see cref="GetStaticConstructor"/> to retrieve it.
        /// </summary>
        public bool HasStaticConstructor
        {
            get
            {
                return (GetTypeFlags(TypeFlags.HasStaticConstructor | TypeFlags.HasStaticConstructorComputed) & TypeFlags.HasStaticConstructor) != 0;
            }
        }

        /// <summary>
        /// Gets all methods on this type defined within the type's metadata.
        /// This will not include methods injected by the type system context.
        /// </summary>
        public virtual IEnumerable<MethodDesc> GetMethods()
        {
            return MethodDesc.EmptyMethods;
        }

        /// <summary>
        /// Gets a named method on the type. This method only looks at methods defined
        /// in type's metadata. The <paramref name="signature"/> parameter can be null.
        /// If signature is not specified and there are multiple matches, the first one
        /// is returned. Returns null if method not found.
        /// </summary>
        // TODO: Substitutions, generics, modopts, ...
        public virtual MethodDesc GetMethod(string name, MethodSignature signature)
        {
            foreach (var method in GetMethods())
            {
                if (method.Name == name)
                {
                    if (signature == null || signature.Equals(method.Signature))
                        return method;
                }
            }
            return null;
        }

        /// <summary>
        /// Retrieves the class constructor method of this type.
        /// </summary>
        /// <returns></returns>
        public virtual MethodDesc GetStaticConstructor()
        {
            return null;
        }

        /// <summary>
        /// Retrieves the public parameterless constructor method of the type, or null if there isn't one
        /// or the type is abstract.
        /// </summary>
        public virtual MethodDesc GetDefaultConstructor()
        {
            return null;
        }

        /// <summary>
        /// Gets all fields on the type as defined in the metadata.
        /// </summary>
        public virtual IEnumerable<FieldDesc> GetFields()
        {
            return FieldDesc.EmptyFields;
        }

        /// <summary>
        /// Gets a named field on the type. Returns null if the field wasn't found.
        /// </summary>
        // TODO: Substitutions, generics, modopts, ...
        // TODO: field signature
        public virtual FieldDesc GetField(string name)
        {
            foreach (var field in GetFields())
            {
                if (field.Name == name)
                    return field;
            }
            return null;
        }

        public virtual TypeDesc InstantiateSignature(Instantiation typeInstantiation, Instantiation methodInstantiation)
        {
            return this;
        }

        /// <summary>
        /// Gets the definition of the type. If this is a generic type instance,
        /// this method strips the instantiation (E.g C&lt;int&gt; -> C&lt;T&gt;)
        /// </summary>
        public virtual TypeDesc GetTypeDefinition()
        {
            return this;
        }

        /// <summary>
        /// Gets a value indicating whether this is a type definition. Returns false
        /// if this is an instantiated generic type.
        /// </summary>
        public bool IsTypeDefinition
        {
            get
            {
                return GetTypeDefinition() == this;
            }
        }

        /// <summary>
        /// Determine if two types share the same type definition
        /// </summary>
        public bool HasSameTypeDefinition(TypeDesc otherType)
        {
            return GetTypeDefinition() == otherType.GetTypeDefinition();
        }

        /// <summary>
        /// Gets a value indicating whether this type has a finalizer method.
        /// Use <see cref="GetFinalizer"/> to retrieve the method.
        /// </summary>
        public bool HasFinalizer
        {
            get
            {
                return (GetTypeFlags(TypeFlags.HasFinalizer | TypeFlags.HasFinalizerComputed) & TypeFlags.HasFinalizer) != 0;
            }
        }

        /// <summary>
        /// Gets the finalizer method (an override of the System.Object::Finalize method)
        /// if this type has one. Returns null if the type doesn't define one.
        /// </summary>
        public virtual MethodDesc GetFinalizer()
        {
            return null;
        }

        /// <summary>
        /// Gets a value indicating whether this type has generic variance (the definition of the type
        /// has a generic parameter that is co- or contravariant).
        /// </summary>
        public bool HasVariance
        {
            get
            {
                return (GetTypeFlags(TypeFlags.HasGenericVariance | TypeFlags.HasGenericVarianceComputed) & TypeFlags.HasGenericVariance) != 0;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this type is an uninstantiated definition of a generic type.
        /// </summary>
        public bool IsGenericDefinition
        {
            get
            {
                return HasInstantiation && IsTypeDefinition;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this is a byref-like type
        /// (a <code>TypedReference</code>, <code>Span&lt;T&gt;</code>, etc.).
        /// </summary>
        public bool IsByRefLike
        {
            get
            {
                return (GetTypeFlags(TypeFlags.IsByRefLike | TypeFlags.AttributeCacheComputed) & TypeFlags.IsByRefLike) != 0;
            }
        }
    }
}