1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Internal.IL;
6 
7 using Debug = System.Diagnostics.Debug;
8 
9 namespace Internal.TypeSystem.Interop
10 {
11     public static class InteropTypes
12     {
GetGC(TypeSystemContext context)13         public static MetadataType GetGC(TypeSystemContext context)
14         {
15             return context.SystemModule.GetKnownType("System", "GC");
16         }
17 
GetSafeHandle(TypeSystemContext context)18         public static MetadataType GetSafeHandle(TypeSystemContext context)
19         {
20             return context.SystemModule.GetKnownType("System.Runtime.InteropServices", "SafeHandle");
21         }
22 
GetCriticalHandle(TypeSystemContext context)23         public static MetadataType GetCriticalHandle(TypeSystemContext context)
24         {
25             return context.SystemModule.GetKnownType("System.Runtime.InteropServices", "CriticalHandle");
26         }
27 
GetMissingMemberException(TypeSystemContext context)28         public static MetadataType GetMissingMemberException(TypeSystemContext context)
29         {
30             return context.SystemModule.GetKnownType("System", "MissingMemberException");
31         }
32 
GetPInvokeMarshal(TypeSystemContext context)33         public static MetadataType GetPInvokeMarshal(TypeSystemContext context)
34         {
35             return context.SystemModule.GetKnownType("System.Runtime.InteropServices", "PInvokeMarshal");
36         }
37 
GetNativeFunctionPointerWrapper(TypeSystemContext context)38         public static MetadataType GetNativeFunctionPointerWrapper(TypeSystemContext context)
39         {
40             return context.SystemModule.GetKnownType("System.Runtime.InteropServices", "NativeFunctionPointerWrapper");
41         }
42 
IsSafeHandle(TypeSystemContext context, TypeDesc type)43         public static bool IsSafeHandle(TypeSystemContext context, TypeDesc type)
44         {
45             return IsOrDerivesFromType(type, GetSafeHandle(context));
46         }
47 
IsCriticalHandle(TypeSystemContext context, TypeDesc type)48         public static bool IsCriticalHandle(TypeSystemContext context, TypeDesc type)
49         {
50             return IsOrDerivesFromType(type, GetCriticalHandle(context));
51         }
52 
IsCoreNamedType(TypeSystemContext context, TypeDesc type, string @namespace, string name)53         private static bool IsCoreNamedType(TypeSystemContext context, TypeDesc type, string @namespace, string name)
54         {
55             return type is MetadataType mdType &&
56                 mdType.Name == name &&
57                 mdType.Namespace == @namespace &&
58                 mdType.Module == context.SystemModule;
59         }
60 
IsHandleRef(TypeSystemContext context, TypeDesc type)61         public static bool IsHandleRef(TypeSystemContext context, TypeDesc type)
62         {
63             return IsCoreNamedType(context, type, "System.Runtime.InteropServices", "HandleRef");
64         }
65 
IsSystemDateTime(TypeSystemContext context, TypeDesc type)66         public static bool IsSystemDateTime(TypeSystemContext context, TypeDesc type)
67         {
68             return IsCoreNamedType(context, type, "System", "DateTime");
69         }
70 
IsStringBuilder(TypeSystemContext context, TypeDesc type)71         public static bool IsStringBuilder(TypeSystemContext context, TypeDesc type)
72         {
73             return IsCoreNamedType(context, type, "System.Text", "StringBuilder");
74         }
75 
IsSystemDecimal(TypeSystemContext context, TypeDesc type)76         public static bool IsSystemDecimal(TypeSystemContext context, TypeDesc type)
77         {
78             return IsCoreNamedType(context, type, "System", "Decimal");
79         }
80 
IsSystemGuid(TypeSystemContext context, TypeDesc type)81         public static bool IsSystemGuid(TypeSystemContext context, TypeDesc type)
82         {
83             return IsCoreNamedType(context, type, "System", "Guid");
84         }
85 
IsOrDerivesFromType(TypeDesc type, MetadataType targetType)86         private static bool IsOrDerivesFromType(TypeDesc type, MetadataType targetType)
87         {
88             while (type != null)
89             {
90                 if (type == targetType)
91                     return true;
92                 type = type.BaseType;
93             }
94             return false;
95         }
96     }
97 }
98