/* * Determine if a type is a sub-class of a specific class. */ static int IsSubClass(ILType *type, ILClass *classInfo) { ILClass *typeClass; if(type == 0) { /* The type is "null", which is always a sub-class */ return 1; } else if(ILType_IsClass(type) || ILType_IsValueType(type)) { typeClass = ILType_ToClass(type); if(ILClassInheritsFrom(typeClass, classInfo) || ILClassImplements(typeClass, classInfo)) { return 1; } return 0; } else if((typeClass = ILClassFromType(ILClassToImage(classInfo), 0, type, 0)) != 0) { if(ILClassInheritsFrom(typeClass, classInfo) || ILClassImplements(typeClass, classInfo)) { return 1; } return 0; } else { return 0; } }
ILClass *_ILGetClrClass(ILExecThread *thread, ILObject *type) { if(type) { /* Make sure that "type" is an instance of "ClrType" */ if(ILClassInheritsFrom(GetObjectClass(type), thread->process->clrTypeClass)) { return (ILClass *)(((System_Reflection *)type)->privateData); } else { return 0; } } else { return 0; } }
/* * Determine if a stack item is assignment-compatible with * a particular memory slot (argument, local, field, etc). */ static int AssignCompatible(ILMethod *method, ILEngineStackItem *item, ILType *type, int unsafeAllowed) { ILImage *image; ILClass *classInfo; ILClass *classInfo2; ILMethod *methodRef; ILType *objType; /* Check for safe and unsafe pointer assignments */ if(item->engineType == ILEngineType_I) { methodRef = MethodRefToMethod(item->typeInfo); if(methodRef) { /* Assigning a method reference, obtained via "ldftn" or "ldvirtftn", to a method pointer destination */ if(ILTypeIdentical(ILMethod_Signature(methodRef), type)) { return 1; } } else if(item->typeInfo != 0 && ILType_IsComplex(item->typeInfo)) { /* May be trying to assign a method pointer to a method type */ if(ILType_IsMethod(item->typeInfo)) { if(ILTypeIdentical(item->typeInfo, type)) { return 1; } } } if(unsafeAllowed) { if(type != 0 && ILType_IsComplex(type)) { if((ILType_Kind(type) & IL_TYPE_COMPLEX_METHOD) != 0 || ILType_Kind(type) == IL_TYPE_COMPLEX_PTR) { return 1; } } } } /* Check for regular assignments */ if(item->engineType == ILEngineType_I4 || item->engineType == ILEngineType_I) { type = ILTypeGetEnumType(type); switch((unsigned long)type) { case (unsigned long)ILType_Boolean: case (unsigned long)ILType_Int8: case (unsigned long)ILType_UInt8: case (unsigned long)ILType_Int16: case (unsigned long)ILType_UInt16: case (unsigned long)ILType_Char: case (unsigned long)ILType_Int32: case (unsigned long)ILType_UInt32: case (unsigned long)ILType_Int: case (unsigned long)ILType_UInt: return 1; default: break; } if(!unsafeAllowed) { return 0; } /* Allow a native int to be assigned to a complex type */ if(type != 0 && ILType_IsComplex(type) && item->engineType == ILEngineType_I) { if(ILType_Kind(type) == IL_TYPE_COMPLEX_PTR || ILType_Kind(type) == IL_TYPE_COMPLEX_BYREF) { return 1; } } return 0; } else if(item->engineType == ILEngineType_I8) { type = ILTypeGetEnumType(type); return (type == ILType_Int64 || type == ILType_UInt64); } else if(item->engineType == ILEngineType_F) { return (type == ILType_Float32 || type == ILType_Float64 || type == ILType_Float); } else if(item->engineType == ILEngineType_O) { if(!(item->typeInfo)) { /* A "null" constant was pushed, which is compatible with any object reference type */ return IsObjectRef(type); } if(!IsObjectRef(type) || !IsObjectRef(item->typeInfo)) { /* Both types must be object references */ return 0; } /* make a copy to avoid unecessary complications */ objType=item->typeInfo; if(ILType_IsArray(type) && ILType_IsArray(objType) && (ILTypeGetRank(type) == ILTypeGetRank(objType))) { objType=ILTypeGetElemType(objType); type=ILTypeGetElemType(type); } image = ILProgramItem_Image(method); classInfo = ILClassResolve(ILClassFromType(image, 0, type, 0)); classInfo2 = ILClassResolve (ILClassFromType(image, 0, objType, 0)); if(classInfo && classInfo2) { /* Is the type a regular class or an interface? */ if(!ILClass_IsInterface(classInfo)) { /* Regular class: the value must inherit from the type */ if(ILClassInheritsFrom(classInfo2, classInfo)) { return 1; } /* If "classInfo2" is an interface, then the conversion is OK if "type" is "System.Object", because all interfaces inherit from "System.Object", even though the metadata doesn't explicitly say so */ if(ILClass_IsInterface(classInfo2)) { return ILTypeIsObjectClass(type); } /* The conversion is not OK */ return 0; } else { /* Interface which the value must implement or inherit from */ return ILClassImplements(classInfo2, classInfo) || ILClassInheritsFrom(classInfo2, classInfo); } } else { return 0; } } else if(item->engineType == ILEngineType_MV) { /* Can only assign managed values to an exact type destination */ return ILTypeIdentical(item->typeInfo, type); } else if(item->engineType == ILEngineType_TypedRef) { /* The type must be "typedref" */ return (type == ILType_TypedRef); } else if(item->engineType == ILEngineType_M || item->engineType == ILEngineType_T) { /* Cannot assign managed pointers to variables or fields, unless we are in "unsafe" mode */ if(!unsafeAllowed) { return 0; } /* Allow an assignment to any pointer, reference, or native destination, regardless of type. This allows C/C++ code to arbitrarily cast pointers via assignment */ if(type != 0 && ILType_IsComplex(type)) { if(ILType_Kind(type) == IL_TYPE_COMPLEX_PTR || ILType_Kind(type) == IL_TYPE_COMPLEX_BYREF || (ILType_Kind(type) & IL_TYPE_COMPLEX_METHOD) != 0) { return 1; } } else if(type == ILType_Int || type == ILType_UInt) { return 1; } return 0; } else { /* Invalid type: never assignment-compatible with anything */ return 0; } }