private static MarshallerKind GetArrayElementMarshallerKind( ArrayType arrayType, MarshalAsDescriptor marshalAs, bool isAnsi) { TypeDesc elementType = arrayType.ElementType; NativeTypeKind nativeType = NativeTypeKind.Invalid; TypeSystemContext context = arrayType.Context; if (marshalAs != null) { nativeType = (NativeTypeKind)marshalAs.ArraySubType; } if (elementType.IsPrimitive) { switch (elementType.Category) { case TypeFlags.Char: switch (nativeType) { case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.AnsiChar); case NativeTypeKind.I2: case NativeTypeKind.U2: return(MarshallerKind.UnicodeChar); default: if (isAnsi) { return(MarshallerKind.AnsiChar); } else { return(MarshallerKind.UnicodeChar); } } case TypeFlags.Boolean: switch (nativeType) { case NativeTypeKind.Boolean: return(MarshallerKind.Bool); case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.CBool); case NativeTypeKind.Invalid: default: return(MarshallerKind.Bool); } case TypeFlags.IntPtr: case TypeFlags.UIntPtr: return(MarshallerKind.BlittableValue); case TypeFlags.Void: return(MarshallerKind.Invalid); case TypeFlags.SByte: case TypeFlags.Int16: case TypeFlags.Int32: case TypeFlags.Int64: case TypeFlags.Byte: case TypeFlags.UInt16: case TypeFlags.UInt32: case TypeFlags.UInt64: case TypeFlags.Single: case TypeFlags.Double: return(MarshallerKind.BlittableValue); default: return(MarshallerKind.Invalid); } } else if (elementType.IsValueType) { if (elementType.IsEnum) { return(MarshallerKind.Enum); } if (InteropTypes.IsSystemDecimal(context, elementType)) { switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: return(MarshallerKind.Decimal); case NativeTypeKind.LPStruct: return(MarshallerKind.BlittableStructPtr); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemGuid(context, elementType)) { switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: return(MarshallerKind.BlittableValue); case NativeTypeKind.LPStruct: return(MarshallerKind.BlittableStructPtr); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemDateTime(context, elementType)) { if (nativeType == NativeTypeKind.Invalid || nativeType == NativeTypeKind.Struct) { return(MarshallerKind.OleDateTime); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsHandleRef(context, elementType)) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.HandleRef); } else { return(MarshallerKind.Invalid); } } else { if (MarshalHelpers.IsBlittableType(elementType)) { switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: return(MarshallerKind.BlittableStruct); default: return(MarshallerKind.Invalid); } } else { // TODO: Differentiate between struct and Union, we only need to support struct not union here return(MarshallerKind.Struct); } } } else if (elementType.IsPointer || elementType.IsFunctionPointer) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (elementType.IsString) { switch (nativeType) { case NativeTypeKind.Invalid: if (isAnsi) { return(MarshallerKind.AnsiString); } else { return(MarshallerKind.UnicodeString); } case NativeTypeKind.LPStr: return(MarshallerKind.AnsiString); case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeString); default: return(MarshallerKind.Invalid); } } // else if (elementType.IsObject) // { // if (nativeType == NativeTypeKind.Invalid) // return MarshallerKind.Variant; // else // return MarshallerKind.Invalid; // } else { return(MarshallerKind.Invalid); } }
internal static MarshallerKind GetMarshallerKind( TypeDesc type, MarshalAsDescriptor marshalAs, bool isReturn, bool isAnsi, MarshallerType marshallerType, out MarshallerKind elementMarshallerKind) { if (type.IsByRef) { type = type.GetParameterType(); } TypeSystemContext context = type.Context; NativeTypeKind nativeType = NativeTypeKind.Invalid; bool isField = marshallerType == MarshallerType.Field; if (marshalAs != null) { nativeType = (NativeTypeKind)marshalAs.Type; } elementMarshallerKind = MarshallerKind.Invalid; // // Determine MarshalerKind // // This mostly resembles desktop CLR and .NET Native code as we need to match their behavior // if (type.IsPrimitive) { switch (type.Category) { case TypeFlags.Void: return(MarshallerKind.VoidReturn); case TypeFlags.Boolean: switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Boolean: return(MarshallerKind.Bool); case NativeTypeKind.U1: case NativeTypeKind.I1: return(MarshallerKind.CBool); default: return(MarshallerKind.Invalid); } case TypeFlags.Char: switch (nativeType) { case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.AnsiChar); case NativeTypeKind.I2: case NativeTypeKind.U2: return(MarshallerKind.UnicodeChar); case NativeTypeKind.Invalid: if (isAnsi) { return(MarshallerKind.AnsiChar); } else { return(MarshallerKind.UnicodeChar); } default: return(MarshallerKind.Invalid); } case TypeFlags.SByte: case TypeFlags.Byte: if (nativeType == NativeTypeKind.I1 || nativeType == NativeTypeKind.U1 || nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int16: case TypeFlags.UInt16: if (nativeType == NativeTypeKind.I2 || nativeType == NativeTypeKind.U2 || nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int32: case TypeFlags.UInt32: if (nativeType == NativeTypeKind.I4 || nativeType == NativeTypeKind.U4 || nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int64: case TypeFlags.UInt64: if (nativeType == NativeTypeKind.I8 || nativeType == NativeTypeKind.U8 || nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.IntPtr: case TypeFlags.UIntPtr: if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Single: if (nativeType == NativeTypeKind.R4 || nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Double: if (nativeType == NativeTypeKind.R8 || nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } default: return(MarshallerKind.Invalid); } } else if (type.IsValueType) { if (type.IsEnum) { return(MarshallerKind.Enum); } if (InteropTypes.IsSystemDateTime(context, type)) { if (nativeType == NativeTypeKind.Invalid || nativeType == NativeTypeKind.Struct) { return(MarshallerKind.OleDateTime); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsHandleRef(context, type)) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.HandleRef); } else { return(MarshallerKind.Invalid); } } switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: if (InteropTypes.IsSystemDecimal(context, type)) { return(MarshallerKind.Decimal); } break; case NativeTypeKind.LPStruct: if (InteropTypes.IsSystemGuid(context, type) || InteropTypes.IsSystemDecimal(context, type)) { if (isField || isReturn) { return(MarshallerKind.Invalid); } else { return(MarshallerKind.BlittableStructPtr); } } break; default: return(MarshallerKind.Invalid); } if (type is MetadataType) { MetadataType metadataType = (MetadataType)type; // the struct type need to be either sequential or explicit. If it is // auto layout we will throw exception. if (!metadataType.IsSequentialLayout && !metadataType.IsExplicitLayout) { throw new InvalidProgramException("The specified structure " + metadataType.Name + " has invalid StructLayout information. It must be either Sequential or Explicit."); } } if (MarshalHelpers.IsBlittableType(type)) { return(MarshallerKind.BlittableStruct); } else { return(MarshallerKind.Struct); } } else if (type.IsSzArray) { if (nativeType == NativeTypeKind.Invalid) { nativeType = NativeTypeKind.Array; } switch (nativeType) { case NativeTypeKind.Array: { if (isField || isReturn) { return(MarshallerKind.Invalid); } var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.AnsiCharArray); } else if (elementMarshallerKind == MarshallerKind.UnicodeChar || // Arrays of unicode char should be marshalled as blittable arrays elementMarshallerKind == MarshallerKind.Enum || elementMarshallerKind == MarshallerKind.BlittableValue) { return(MarshallerKind.BlittableArray); } else { return(MarshallerKind.Array); } } case NativeTypeKind.ByValArray: // fix sized array { var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.ByValAnsiCharArray); } else { return(MarshallerKind.ByValArray); } } default: return(MarshallerKind.Invalid); } } else if (type.IsPointer || type.IsFunctionPointer) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsDelegate) { if (nativeType == NativeTypeKind.Invalid || nativeType == NativeTypeKind.Func) { return(MarshallerKind.FunctionPointer); } else { return(MarshallerKind.Invalid); } } else if (type.IsString) { switch (nativeType) { case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.LPStr: return(MarshallerKind.AnsiString); case NativeTypeKind.LPTStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.ByValTStr: if (isAnsi) { elementMarshallerKind = MarshallerKind.AnsiChar; return(MarshallerKind.ByValAnsiString); } else { elementMarshallerKind = MarshallerKind.UnicodeChar; return(MarshallerKind.ByValUnicodeString); } case NativeTypeKind.Invalid: if (isAnsi) { return(MarshallerKind.AnsiString); } else { return(MarshallerKind.UnicodeString); } default: return(MarshallerKind.Invalid); } } // else if (type.IsObject) // { // if (nativeType == NativeTypeKind.Invalid) // return MarshallerKind.Variant; // else // return MarshallerKind.Invalid; // } else if (InteropTypes.IsStringBuilder(context, type)) { switch (nativeType) { case NativeTypeKind.Invalid: if (isAnsi) { return(MarshallerKind.AnsiStringBuilder); } else { return(MarshallerKind.UnicodeStringBuilder); } case NativeTypeKind.LPStr: return(MarshallerKind.AnsiStringBuilder); case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeStringBuilder); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSafeHandle(context, type)) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.SafeHandle); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsCriticalHandle(context, type)) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.CriticalHandle); } else { return(MarshallerKind.Invalid); } } else { return(MarshallerKind.Invalid); } }
internal static MarshallerKind GetMarshallerKind( TypeDesc type, int?parameterIndex, EmbeddedSignatureData[] customModifierData, MarshalAsDescriptor marshalAs, bool isReturn, bool isAnsi, MarshallerType marshallerType, out MarshallerKind elementMarshallerKind) { elementMarshallerKind = MarshallerKind.Invalid; bool isByRef = false; if (type.IsByRef) { isByRef = true; type = type.GetParameterType(); if (!type.IsPrimitive && type.IsValueType && marshallerType != MarshallerType.Field && HasCopyConstructorCustomModifier(parameterIndex, customModifierData)) { return(MarshallerKind.BlittableValueClassWithCopyCtor); } // Compat note: CLR allows ref returning blittable structs for IJW if (isReturn) { return(MarshallerKind.Invalid); } } TypeSystemContext context = type.Context; NativeTypeKind nativeType = NativeTypeKind.Default; bool isField = marshallerType == MarshallerType.Field; if (marshalAs != null) { nativeType = marshalAs.Type; } // // Determine MarshalerKind // if (type.IsPrimitive) { switch (type.Category) { case TypeFlags.Void: return(MarshallerKind.VoidReturn); case TypeFlags.Boolean: switch (nativeType) { case NativeTypeKind.Default: case NativeTypeKind.Boolean: return(MarshallerKind.Bool); case NativeTypeKind.U1: case NativeTypeKind.I1: return(MarshallerKind.CBool); case NativeTypeKind.VariantBool: if (context.Target.IsWindows) { return(MarshallerKind.VariantBool); } else { return(MarshallerKind.Invalid); } default: return(MarshallerKind.Invalid); } case TypeFlags.Char: switch (nativeType) { case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.AnsiChar); case NativeTypeKind.I2: case NativeTypeKind.U2: return(MarshallerKind.UnicodeChar); case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiChar); } else { return(MarshallerKind.UnicodeChar); } default: return(MarshallerKind.Invalid); } case TypeFlags.SByte: case TypeFlags.Byte: if (nativeType == NativeTypeKind.I1 || nativeType == NativeTypeKind.U1 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int16: case TypeFlags.UInt16: if (nativeType == NativeTypeKind.I2 || nativeType == NativeTypeKind.U2 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int32: case TypeFlags.UInt32: if (nativeType == NativeTypeKind.I4 || nativeType == NativeTypeKind.U4 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int64: case TypeFlags.UInt64: if (nativeType == NativeTypeKind.I8 || nativeType == NativeTypeKind.U8 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.IntPtr: case TypeFlags.UIntPtr: if (nativeType == NativeTypeKind.SysInt || nativeType == NativeTypeKind.SysUInt || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Single: if (nativeType == NativeTypeKind.R4 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Double: if (nativeType == NativeTypeKind.R8 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } default: return(MarshallerKind.Invalid); } } else if (type.IsValueType) { if (type.IsEnum) { return(MarshallerKind.Enum); } if (InteropTypes.IsSystemDateTime(context, type)) { if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct) { return(MarshallerKind.OleDateTime); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsHandleRef(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.HandleRef); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemDecimal(context, type)) { if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) { return(MarshallerKind.Decimal); } else if (nativeType == NativeTypeKind.LPStruct && !isField) { return(MarshallerKind.BlittableStructPtr); } else if (nativeType == NativeTypeKind.Currency) { return(MarshallerKind.OleCurrency); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemGuid(context, type)) { if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableStruct); } else if (nativeType == NativeTypeKind.LPStruct && !isField) { return(MarshallerKind.BlittableStructPtr); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemArgIterator(context, type)) { // Don't want to fall through to the blittable/haslayout case return(MarshallerKind.Invalid); } bool isBlittable = MarshalUtils.IsBlittableType(type); // Blittable generics are allowed to be marshalled with the following exceptions: // * ByReference<T>: This represents an interior pointer and is not actually blittable // * Nullable<T>: We don't want to be locked into the default behavior as we may want special handling later // * Vector64<T>: Represents the __m64 ABI primitive which requires currently unimplemented handling // * Vector128<T>: Represents the __m128 ABI primitive which requires currently unimplemented handling // * Vector256<T>: Represents the __m256 ABI primitive which requires currently unimplemented handling // * Vector<T>: Has a variable size (either __m128 or __m256) and isn't readily usable for interop scenarios // We can't block these types for field scenarios for back-compat reasons. if (type.HasInstantiation && !isField && (!isBlittable || InteropTypes.IsSystemByReference(context, type) || InteropTypes.IsSystemSpan(context, type) || InteropTypes.IsSystemReadOnlySpan(context, type) || InteropTypes.IsSystemNullable(context, type) || InteropTypes.IsSystemRuntimeIntrinsicsVector64T(context, type) || InteropTypes.IsSystemRuntimeIntrinsicsVector128T(context, type) || InteropTypes.IsSystemRuntimeIntrinsicsVector256T(context, type) || InteropTypes.IsSystemNumericsVectorT(context, type))) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (isBlittable) { if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct) { return(MarshallerKind.Invalid); } return(MarshallerKind.BlittableStruct); } else if (((MetadataType)type).HasLayout()) { if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct) { return(MarshallerKind.Invalid); } return(MarshallerKind.Struct); } else { return(MarshallerKind.Invalid); } } else if (type.IsSzArray) { #if READYTORUN // We don't want the additional test/maintenance cost of this in R2R. if (isByRef) { return(MarshallerKind.Invalid); } #else _ = isByRef; #endif if (nativeType == NativeTypeKind.Default) { nativeType = NativeTypeKind.Array; } switch (nativeType) { case NativeTypeKind.Array: { if (isField) { return(MarshallerKind.Invalid); } var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.AnsiCharArray); } else if (elementMarshallerKind == MarshallerKind.UnicodeChar || // Arrays of unicode char should be marshalled as blittable arrays elementMarshallerKind == MarshallerKind.Enum || elementMarshallerKind == MarshallerKind.BlittableValue) { return(MarshallerKind.BlittableArray); } else { return(MarshallerKind.Array); } } case NativeTypeKind.ByValArray: // fix sized array { var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.ByValAnsiCharArray); } else { return(MarshallerKind.ByValArray); } } default: return(MarshallerKind.Invalid); } } else if (type.IsPointer) { if (nativeType == NativeTypeKind.Default) { var pointedAtType = type.GetParameterType(); if (!pointedAtType.IsPrimitive && !type.IsEnum && marshallerType != MarshallerType.Field && HasCopyConstructorCustomModifier(parameterIndex, customModifierData)) { return(MarshallerKind.BlittableValueClassWithCopyCtor); } return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsFunctionPointer) { if (nativeType == NativeTypeKind.Func || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsDelegate) { if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Func) { return(MarshallerKind.FunctionPointer); } else { return(MarshallerKind.Invalid); } } else if (type.IsString) { switch (nativeType) { case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.LPStr: return(MarshallerKind.AnsiString); case NativeTypeKind.LPUTF8Str: return(MarshallerKind.UTF8String); case NativeTypeKind.LPTStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.ByValTStr: if (isAnsi) { elementMarshallerKind = MarshallerKind.AnsiChar; return(MarshallerKind.ByValAnsiString); } else { elementMarshallerKind = MarshallerKind.UnicodeChar; return(MarshallerKind.ByValUnicodeString); } case NativeTypeKind.TBStr: case NativeTypeKind.BStr: return(MarshallerKind.BSTRString); case NativeTypeKind.AnsiBStr: return(MarshallerKind.AnsiBSTRString); case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiString); } else { return(MarshallerKind.UnicodeString); } default: return(MarshallerKind.Invalid); } } else if (type.IsObject) { if (nativeType == NativeTypeKind.AsAny) { return(isAnsi ? MarshallerKind.AsAnyA : MarshallerKind.AsAnyW); } else if (context.Target.IsWindows) { if ((isField && nativeType == NativeTypeKind.Default) || nativeType == NativeTypeKind.Intf || nativeType == NativeTypeKind.IUnknown) { return(MarshallerKind.ComInterface); } else { return(MarshallerKind.Variant); } } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsStringBuilder(context, type)) { switch (nativeType) { case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiStringBuilder); } else { return(MarshallerKind.UnicodeStringBuilder); } case NativeTypeKind.LPStr: return(MarshallerKind.AnsiStringBuilder); case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeStringBuilder); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSafeHandle(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.SafeHandle); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsCriticalHandle(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.CriticalHandle); } else { return(MarshallerKind.Invalid); } } else if (type is MetadataType mdType && mdType.HasLayout()) { if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (!isField && nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.LPStruct) { return(MarshallerKind.LayoutClassPtr); } else if (isField && (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct)) { return(MarshallerKind.LayoutClass); } else { return(MarshallerKind.Invalid); } }
private static MarshallerKind GetArrayElementMarshallerKind( ArrayType arrayType, MarshalAsDescriptor marshalAs, bool isAnsi) { TypeDesc elementType = arrayType.ElementType; NativeTypeKind nativeType = NativeTypeKind.Invalid; TypeSystemContext context = arrayType.Context; if (marshalAs != null) { nativeType = (NativeTypeKind)marshalAs.ArraySubType; } if (elementType.IsPrimitive) { switch (elementType.Category) { case TypeFlags.Char: switch (nativeType) { case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.AnsiChar); case NativeTypeKind.I2: case NativeTypeKind.U2: return(MarshallerKind.UnicodeChar); default: if (isAnsi) { return(MarshallerKind.AnsiChar); } else { return(MarshallerKind.UnicodeChar); } } case TypeFlags.Boolean: switch (nativeType) { case NativeTypeKind.Boolean: return(MarshallerKind.Bool); case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.CBool); case NativeTypeKind.Invalid: default: return(MarshallerKind.Bool); } case TypeFlags.IntPtr: case TypeFlags.UIntPtr: return(MarshallerKind.BlittableValue); case TypeFlags.Void: return(MarshallerKind.Invalid); case TypeFlags.SByte: case TypeFlags.Int16: case TypeFlags.Int32: case TypeFlags.Int64: case TypeFlags.Byte: case TypeFlags.UInt16: case TypeFlags.UInt32: case TypeFlags.UInt64: case TypeFlags.Single: case TypeFlags.Double: return(MarshallerKind.BlittableValue); default: return(MarshallerKind.Invalid); } } else if (elementType.IsValueType) { if (elementType.IsEnum) { return(MarshallerKind.Enum); } if (InteropTypes.IsSystemDecimal(context, elementType)) { switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: return(MarshallerKind.Decimal); case NativeTypeKind.LPStruct: return(MarshallerKind.BlittableStructPtr); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemGuid(context, elementType)) { switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: return(MarshallerKind.BlittableValue); case NativeTypeKind.LPStruct: return(MarshallerKind.BlittableStructPtr); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemDateTime(context, elementType)) { if (nativeType == NativeTypeKind.Invalid || nativeType == NativeTypeKind.Struct) { return(MarshallerKind.OleDateTime); } else { return(MarshallerKind.Invalid); } } /* * TODO: Bring HandleRef to CoreLib * https://github.com/dotnet/corert/issues/2570 * * else if (InteropTypes.IsHandleRef(context, elementType)) * { * return MarshallerKind.HandleRef; * } */ else { if (MarshalHelpers.IsBlittableType(elementType)) { switch (nativeType) { case NativeTypeKind.Invalid: case NativeTypeKind.Struct: return(MarshallerKind.BlittableStruct); default: return(MarshallerKind.Invalid); } } else { // TODO: Differentiate between struct and Union, we only need to support struct not union here return(MarshallerKind.Struct); } } } else // !valueType { if (elementType.IsString) { switch (nativeType) { case NativeTypeKind.Invalid: if (isAnsi) { return(MarshallerKind.AnsiString); } else { return(MarshallerKind.UnicodeString); } case NativeTypeKind.LPStr: return(MarshallerKind.AnsiString); case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeString); default: return(MarshallerKind.Invalid); } } if (elementType.IsObject) { if (nativeType == NativeTypeKind.Invalid) { return(MarshallerKind.Variant); } else { return(MarshallerKind.Invalid); } } if (elementType.IsSzArray) { return(MarshallerKind.Invalid); } if (elementType.IsPointer) { return(MarshallerKind.Invalid); } if (InteropTypes.IsSafeHandle(context, elementType)) { return(MarshallerKind.Invalid); } /* * TODO: Bring CriticalHandle to CoreLib * https://github.com/dotnet/corert/issues/2570 * * if (pInvokeData.IsCriticalHandle(elementType)) * { * return MarshallerKind.Invalid; * } */ } return(MarshallerKind.Invalid); }
internal static MarshallerKind GetMarshallerKind( TypeDesc type, MarshalAsDescriptor marshalAs, bool isReturn, bool isAnsi, MarshallerType marshallerType, out MarshallerKind elementMarshallerKind) { elementMarshallerKind = MarshallerKind.Invalid; bool isByRef = false; if (type.IsByRef) { isByRef = true; type = type.GetParameterType(); // Compat note: CLR allows ref returning blittable structs for IJW if (isReturn) { return(MarshallerKind.Invalid); } } TypeSystemContext context = type.Context; NativeTypeKind nativeType = NativeTypeKind.Default; bool isField = marshallerType == MarshallerType.Field; if (marshalAs != null) { nativeType = marshalAs.Type; } // // Determine MarshalerKind // // This mostly resembles desktop CLR and .NET Native code as we need to match their behavior // if (type.IsPrimitive) { switch (type.Category) { case TypeFlags.Void: return(MarshallerKind.VoidReturn); case TypeFlags.Boolean: switch (nativeType) { case NativeTypeKind.Default: case NativeTypeKind.Boolean: return(MarshallerKind.Bool); case NativeTypeKind.U1: case NativeTypeKind.I1: return(MarshallerKind.CBool); default: return(MarshallerKind.Invalid); } case TypeFlags.Char: switch (nativeType) { case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.AnsiChar); case NativeTypeKind.I2: case NativeTypeKind.U2: return(MarshallerKind.UnicodeChar); case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiChar); } else { return(MarshallerKind.UnicodeChar); } default: return(MarshallerKind.Invalid); } case TypeFlags.SByte: case TypeFlags.Byte: if (nativeType == NativeTypeKind.I1 || nativeType == NativeTypeKind.U1 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int16: case TypeFlags.UInt16: if (nativeType == NativeTypeKind.I2 || nativeType == NativeTypeKind.U2 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int32: case TypeFlags.UInt32: if (nativeType == NativeTypeKind.I4 || nativeType == NativeTypeKind.U4 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int64: case TypeFlags.UInt64: if (nativeType == NativeTypeKind.I8 || nativeType == NativeTypeKind.U8 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.IntPtr: case TypeFlags.UIntPtr: if (nativeType == NativeTypeKind.SysInt || nativeType == NativeTypeKind.SysUInt || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Single: if (nativeType == NativeTypeKind.R4 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Double: if (nativeType == NativeTypeKind.R8 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } default: return(MarshallerKind.Invalid); } } else if (type.IsValueType) { if (type.IsEnum) { return(MarshallerKind.Enum); } if (InteropTypes.IsSystemDateTime(context, type)) { if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct) { return(MarshallerKind.OleDateTime); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsHandleRef(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.HandleRef); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemDecimal(context, type)) { if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) { return(MarshallerKind.Decimal); } else if (nativeType == NativeTypeKind.LPStruct && !isField && !isReturn) { return(MarshallerKind.BlittableStructPtr); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemGuid(context, type)) { if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableStruct); } else if (nativeType == NativeTypeKind.LPStruct && !isField && !isReturn) { return(MarshallerKind.BlittableStructPtr); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemArgIterator(context, type)) { // Don't want to fall through to the blittable/haslayout case return(MarshallerKind.Invalid); } if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (MarshalUtils.IsBlittableType(type)) { if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct) { return(MarshallerKind.Invalid); } return(MarshallerKind.BlittableStruct); } else if (((MetadataType)type).HasLayout()) { if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct) { return(MarshallerKind.Invalid); } return(MarshallerKind.Struct); } else { return(MarshallerKind.Invalid); } } else if (type.IsSzArray) { #if READYTORUN // We don't want the additional test/maintenance cost of this in R2R. if (isByRef) { return(MarshallerKind.Invalid); } #else _ = isByRef; #endif if (nativeType == NativeTypeKind.Default) { nativeType = NativeTypeKind.Array; } switch (nativeType) { case NativeTypeKind.Array: { if (isField || isReturn) { return(MarshallerKind.Invalid); } var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.AnsiCharArray); } else if (elementMarshallerKind == MarshallerKind.UnicodeChar || // Arrays of unicode char should be marshalled as blittable arrays elementMarshallerKind == MarshallerKind.Enum || elementMarshallerKind == MarshallerKind.BlittableValue) { return(MarshallerKind.BlittableArray); } else { return(MarshallerKind.Array); } } case NativeTypeKind.ByValArray: // fix sized array { var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.ByValAnsiCharArray); } else { return(MarshallerKind.ByValArray); } } default: return(MarshallerKind.Invalid); } } else if (type.IsPointer) { TypeDesc parameterType = ((PointerType)type).ParameterType; if ((!parameterType.IsEnum && !parameterType.IsPrimitive && !MarshalUtils.IsBlittableType(parameterType)) || parameterType.IsGCPointer) { // Pointers cannot reference marshaled structures. Use ByRef instead. return(MarshallerKind.Invalid); } if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsFunctionPointer) { if (nativeType == NativeTypeKind.Func || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsDelegate) { if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Func) { return(MarshallerKind.FunctionPointer); } else { return(MarshallerKind.Invalid); } } else if (type.IsString) { switch (nativeType) { case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.LPStr: return(MarshallerKind.AnsiString); case NativeTypeKind.LPUTF8Str: return(MarshallerKind.UTF8String); case NativeTypeKind.LPTStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.ByValTStr: if (isAnsi) { elementMarshallerKind = MarshallerKind.AnsiChar; return(MarshallerKind.ByValAnsiString); } else { elementMarshallerKind = MarshallerKind.UnicodeChar; return(MarshallerKind.ByValUnicodeString); } case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiString); } else { return(MarshallerKind.UnicodeString); } default: return(MarshallerKind.Invalid); } } else if (type.IsObject) { if (nativeType == NativeTypeKind.AsAny) { return(isAnsi ? MarshallerKind.AsAnyA : MarshallerKind.AsAnyW); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsStringBuilder(context, type)) { switch (nativeType) { case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiStringBuilder); } else { return(MarshallerKind.UnicodeStringBuilder); } case NativeTypeKind.LPStr: return(MarshallerKind.AnsiStringBuilder); case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeStringBuilder); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSafeHandle(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.SafeHandle); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsCriticalHandle(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.CriticalHandle); } else { return(MarshallerKind.Invalid); } } else if (type is MetadataType mdType && mdType.HasLayout()) { if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (!isField && nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.LPStruct) { return(MarshallerKind.LayoutClassPtr); } else if (isField && (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct)) { return(MarshallerKind.LayoutClass); } else { return(MarshallerKind.Invalid); } }
internal static MarshallerKind GetMarshallerKind( TypeDesc type, MarshalAsDescriptor marshalAs, bool isReturn, bool isAnsi, MarshallerType marshallerType, out MarshallerKind elementMarshallerKind) { elementMarshallerKind = MarshallerKind.Invalid; bool isByRef = false; if (type.IsByRef) { isByRef = true; type = type.GetParameterType(); // Compat note: CLR allows ref returning blittable structs for IJW if (isReturn) { return(MarshallerKind.Invalid); } } TypeSystemContext context = type.Context; NativeTypeKind nativeType = NativeTypeKind.Default; bool isField = marshallerType == MarshallerType.Field; if (marshalAs != null) { nativeType = marshalAs.Type; } // // Determine MarshalerKind // // This mostly resembles .NET Framework CLR and .NET Native code as we need to match their behavior // if (type.IsPrimitive) { switch (type.Category) { case TypeFlags.Void: return(MarshallerKind.VoidReturn); case TypeFlags.Boolean: switch (nativeType) { case NativeTypeKind.Default: case NativeTypeKind.Boolean: return(MarshallerKind.Bool); case NativeTypeKind.U1: case NativeTypeKind.I1: return(MarshallerKind.CBool); default: return(MarshallerKind.Invalid); } case TypeFlags.Char: switch (nativeType) { case NativeTypeKind.I1: case NativeTypeKind.U1: return(MarshallerKind.AnsiChar); case NativeTypeKind.I2: case NativeTypeKind.U2: return(MarshallerKind.UnicodeChar); case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiChar); } else { return(MarshallerKind.UnicodeChar); } default: return(MarshallerKind.Invalid); } case TypeFlags.SByte: case TypeFlags.Byte: if (nativeType == NativeTypeKind.I1 || nativeType == NativeTypeKind.U1 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int16: case TypeFlags.UInt16: if (nativeType == NativeTypeKind.I2 || nativeType == NativeTypeKind.U2 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int32: case TypeFlags.UInt32: if (nativeType == NativeTypeKind.I4 || nativeType == NativeTypeKind.U4 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Int64: case TypeFlags.UInt64: if (nativeType == NativeTypeKind.I8 || nativeType == NativeTypeKind.U8 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.IntPtr: case TypeFlags.UIntPtr: if (nativeType == NativeTypeKind.SysInt || nativeType == NativeTypeKind.SysUInt || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Single: if (nativeType == NativeTypeKind.R4 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } case TypeFlags.Double: if (nativeType == NativeTypeKind.R8 || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } default: return(MarshallerKind.Invalid); } } else if (type.IsValueType) { if (type.IsEnum) { return(MarshallerKind.Enum); } if (InteropTypes.IsSystemDateTime(context, type)) { if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct) { return(MarshallerKind.OleDateTime); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsHandleRef(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.HandleRef); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemDecimal(context, type)) { if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) { return(MarshallerKind.Decimal); } else if (nativeType == NativeTypeKind.LPStruct && !isField && !isReturn) { return(MarshallerKind.BlittableStructPtr); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemGuid(context, type)) { if (nativeType == NativeTypeKind.Struct || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableStruct); } else if (nativeType == NativeTypeKind.LPStruct && !isField && !isReturn) { return(MarshallerKind.BlittableStructPtr); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSystemArgIterator(context, type)) { // Don't want to fall through to the blittable/haslayout case return(MarshallerKind.Invalid); } bool isBlittable = MarshalUtils.IsBlittableType(type); // Blittable generics are allowed to be marshalled with the following exceptions: // * ByReference<T>: This represents an interior pointer and is not actually blittable // * Nullable<T>: We don't want to be locked into the default behavior as we may want special handling later // * Vector64<T>: Represents the __m64 ABI primitive which requires currently unimplemented handling // * Vector128<T>: Represents the __m128 ABI primitive which requires currently unimplemented handling // * Vector256<T>: Represents the __m256 ABI primitive which requires currently unimplemented handling // * Vector<T>: Has a variable size (either __m128 or __m256) and isn't readily usable for inteorp scenarios if (type.HasInstantiation && (!isBlittable || InteropTypes.IsSystemByReference(context, type) || InteropTypes.IsSystemNullable(context, type) || InteropTypes.IsSystemRuntimeIntrinsicsVector64T(context, type) || InteropTypes.IsSystemRuntimeIntrinsicsVector128T(context, type) || InteropTypes.IsSystemRuntimeIntrinsicsVector256T(context, type) || InteropTypes.IsSystemNumericsVectorT(context, type))) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (isBlittable) { if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct) { return(MarshallerKind.Invalid); } return(MarshallerKind.BlittableStruct); } else if (((MetadataType)type).HasLayout()) { if (nativeType != NativeTypeKind.Default && nativeType != NativeTypeKind.Struct) { return(MarshallerKind.Invalid); } return(MarshallerKind.Struct); } else { return(MarshallerKind.Invalid); } } else if (type.IsSzArray) { #if READYTORUN // We don't want the additional test/maintenance cost of this in R2R. if (isByRef) { return(MarshallerKind.Invalid); } #else _ = isByRef; #endif if (nativeType == NativeTypeKind.Default) { nativeType = NativeTypeKind.Array; } switch (nativeType) { case NativeTypeKind.Array: { if (isField || isReturn) { return(MarshallerKind.Invalid); } var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.AnsiCharArray); } else if (elementMarshallerKind == MarshallerKind.UnicodeChar || // Arrays of unicode char should be marshalled as blittable arrays elementMarshallerKind == MarshallerKind.Enum || elementMarshallerKind == MarshallerKind.BlittableValue) { return(MarshallerKind.BlittableArray); } else { return(MarshallerKind.Array); } } case NativeTypeKind.ByValArray: // fix sized array { var arrayType = (ArrayType)type; elementMarshallerKind = GetArrayElementMarshallerKind( arrayType, marshalAs, isAnsi); // If element is invalid type, the array itself is invalid if (elementMarshallerKind == MarshallerKind.Invalid) { return(MarshallerKind.Invalid); } if (elementMarshallerKind == MarshallerKind.AnsiChar) { return(MarshallerKind.ByValAnsiCharArray); } else { return(MarshallerKind.ByValArray); } } default: return(MarshallerKind.Invalid); } } else if (type.IsPointer) { #if READYTORUN TypeDesc parameterType = ((PointerType)type).ParameterType; if ((!parameterType.IsEnum && !parameterType.IsPrimitive && !MarshalUtils.IsBlittableType(parameterType)) || parameterType.IsGCPointer) { // Pointers cannot reference marshaled structures. Use ByRef instead. return(MarshallerKind.Invalid); } #else // Do not bother enforcing the above artificial restriction // See https://github.com/dotnet/coreclr/issues/27800 #endif if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsFunctionPointer) { if (nativeType == NativeTypeKind.Func || nativeType == NativeTypeKind.Default) { return(MarshallerKind.BlittableValue); } else { return(MarshallerKind.Invalid); } } else if (type.IsDelegate) { if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Func) { return(MarshallerKind.FunctionPointer); } else { return(MarshallerKind.Invalid); } } else if (type.IsString) { switch (nativeType) { case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.LPStr: return(MarshallerKind.AnsiString); case NativeTypeKind.LPUTF8Str: return(MarshallerKind.UTF8String); case NativeTypeKind.LPTStr: return(MarshallerKind.UnicodeString); case NativeTypeKind.ByValTStr: if (isAnsi) { elementMarshallerKind = MarshallerKind.AnsiChar; return(MarshallerKind.ByValAnsiString); } else { elementMarshallerKind = MarshallerKind.UnicodeChar; return(MarshallerKind.ByValUnicodeString); } case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiString); } else { return(MarshallerKind.UnicodeString); } default: return(MarshallerKind.Invalid); } } else if (type.IsObject) { if (nativeType == NativeTypeKind.AsAny) { return(isAnsi ? MarshallerKind.AsAnyA : MarshallerKind.AsAnyW); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsStringBuilder(context, type)) { switch (nativeType) { case NativeTypeKind.Default: if (isAnsi) { return(MarshallerKind.AnsiStringBuilder); } else { return(MarshallerKind.UnicodeStringBuilder); } case NativeTypeKind.LPStr: return(MarshallerKind.AnsiStringBuilder); case NativeTypeKind.LPWStr: return(MarshallerKind.UnicodeStringBuilder); default: return(MarshallerKind.Invalid); } } else if (InteropTypes.IsSafeHandle(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.SafeHandle); } else { return(MarshallerKind.Invalid); } } else if (InteropTypes.IsCriticalHandle(context, type)) { if (nativeType == NativeTypeKind.Default) { return(MarshallerKind.CriticalHandle); } else { return(MarshallerKind.Invalid); } } else if (type is MetadataType mdType && mdType.HasLayout()) { if (type.HasInstantiation) { // Generic types cannot be marshaled. return(MarshallerKind.Invalid); } if (!isField && nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.LPStruct) { return(MarshallerKind.LayoutClassPtr); } else if (isField && (nativeType == NativeTypeKind.Default || nativeType == NativeTypeKind.Struct)) { return(MarshallerKind.LayoutClass); } else { return(MarshallerKind.Invalid); } }