/*
 * public static bool ClrSetTypedReference(TypedReference target,
 *										   Object value);
 */
ILBool _IL_TypedReference_ClrSetTypedReference(ILExecThread *_thread,
										       ILTypedRef target,
										       ILObject *value)
{
	ILType *type;
	if(target.type == 0 || target.value == 0)
	{
		/* This is an invalid typed reference */
		return 0;
	}
	type = ILClassToType(target.type);
	if(ILType_IsPrimitive(type) || ILType_IsValueType(type))
	{
		if(!ILExecThreadUnbox(_thread, type, value, target.value))
		{
			return 0;
		}
		return 1;
	}
	else if(ILTypeAssignCompatible
				(ILProgramItem_Image(_thread->method),
			     (value ? ILClassToType(GetObjectClass(value)) : 0),
			     type))
	{
		*((ILObject **)(target.value)) = value;
		return 1;
	}
	else
	{
		return 0;
	}
}
ILBool _IL_Object_Equals(ILExecThread *thread, ILObject *_this, ILObject *obj)
{
	ILClass *classInfo;
	ILUInt32 size;

	/* Handle the easy cases first */
	if(_this == obj)
	{
		return 1;
	}
	else if(!obj)
	{
		return 0;
	}

	/* Check to see if both are value types with the same type */
	classInfo = GetObjectClass(_this);
	if(classInfo != GetObjectClass(obj) || !ILClassIsValueType(classInfo))
	{
		return 0;
	}

	/* Perform a bitwise comparison on the values */
	size = ILSizeOfType(thread, ILClassToType(classInfo));
	if(!size)
	{
		return 1;
	}
	else
	{
		return !ILMemCmp((void *)_this, (void *)obj, size);
	}
}
Beispiel #3
0
ILType *ILProgramItemToType(ILProgramItem *item)
{
	if(!item)
	{
		return 0;
	}
	switch(item->token & IL_META_TOKEN_MASK)
	{
		case IL_META_TOKEN_TYPE_DEF:
		case IL_META_TOKEN_TYPE_REF:
		case IL_META_TOKEN_EXPORTED_TYPE:
		{
			ILType *type;
			ILClass *info = (ILClass *)item;

			if((type = _ILClass_Synthetic(info)) != 0)
			{
				return type;
			}
			return ILClassToType(info);
		}
		break;

		case IL_META_TOKEN_TYPE_SPEC:
		{
			return _ILTypeSpec_Type((ILTypeSpec *)item);
		}
		break;
	}

	return 0;
}
/*
 * public TypedReference GetNextArg(RuntimeTypeHandle type);
 *
 * Note: this version is typically used by unmanaged C++ code, where
 * it is expected that the next argument is of a particular type.
 * We check the type and return the value.  This is a little stricter
 * than other implementations, but it is also a lot safer.
 */
ILTypedRef _IL_ArgIterator_GetNextArg_RuntimeTypeHandle(ILExecThread *_thread,
														void *_this,
														void *type)
{
	void *actualType = *((void **)type);
	int actualTypePrim;
	ILTypedRef ref;

	/* Convert the actual type into its primitive form */
	actualTypePrim = ArgTypeToPrimitive
		(ILClassToType((ILClass *)actualType));

	/* Get the next reference from the argument list */
	ref = _IL_ArgIterator_GetNextArg_(_thread, _this);
	if(!(ref.type))
	{
		/* An exception was thrown at the end of the list */
		return ref;
	}

	/* Convert the argument type into its primitive form and compare */
	if(ArgTypeToPrimitive(ILClassToType((ILClass *)(ref.type)))
			!= actualTypePrim || actualTypePrim == IL_META_ELEMTYPE_END)
	{
		ILExecThreadThrowSystem(_thread, "System.InvalidOperationException",
								"Invalid_BadEnumeratorPosition");
		ref.type = 0;
		ref.value = 0;
		return ref;
	}
	if(actualTypePrim == IL_META_ELEMTYPE_VALUETYPE && ref.type != actualType)
	{
		ILExecThreadThrowSystem(_thread, "System.InvalidOperationException",
								"Invalid_BadEnumeratorPosition");
		ref.type = 0;
		ref.value = 0;
		return ref;
	}

	/* Convert the typed reference into the requested type and return it */
	ref.type = actualType;
	return ref;
}
Beispiel #5
0
ILClass *ILClassInstantiate(ILImage *image, ILType *classType,
							ILType *classArgs, ILType *methodArgs)
{
	ILClass *classInfo;
	ILType *type;

	/* Bail out early if the type does not need instantiation */
	if(!ILType_IsWith(classType) && !ILTypeNeedsInstantiation(classType))
	{
		return ILClassFromType(image, 0, classType, 0);
	}

	/* Search for a synthetic type that matches the expanded
	   form of the class type, in case we already instantiated
	   this class previously.  We do this in such a way that we
	   won't need to call "ILTypeInstantiate" unless necessary */
	classInfo = _ILTypeToSyntheticInstantiation(image, classType, classArgs, methodArgs);
	if(classInfo)
	{
		if((classInfo->attributes & IL_META_TYPEDEF_CLASS_EXPANDED) == 0)
		{
			classArgs = ILClassToType(classInfo);
			if(!ExpandInstantiations(image, classInfo, classType, classArgs))
			{
				return 0;
			}
		}
		return classInfo;
	}

	/* Instantiate the class type */
	type = ILTypeInstantiate(image->context, classType, classArgs, methodArgs);
	if(!type)
	{
		return 0;
	}

	/* Create a synthetic type for the expanded form */
	classInfo = ILClassFromType(image, 0, type, 0);
	if(!classInfo)
	{
		return 0;
	}
	if(!ExpandInstantiations(image, classInfo, type, type))
	{
		return 0;
	}
	return classInfo;
}
/*
 * private static Object InternalGetUnitializaedObject(Type type);
 */
ILObject *_IL_FormatterServices_InternalGetUninitializedObject
				(ILExecThread *_thread, ILObject *type)
{
	ILClass *classInfo = _ILGetClrClass(_thread, type);
	ILType *classType;
	if(classInfo)
	{
		classType = ILClassToType(classInfo);
		if(classType && (ILType_IsClass(classType) ||
						 ILType_IsValueType(classType) ||
						 ILType_IsPrimitive(classType)) &&
		   !ILTypeIsStringClass(classType))
		{
			return _ILEngineAllocObject(_thread, classInfo);
		}
	}
	return 0;
}
/*
 * public static Object ToObject();
 */
ILObject *_IL_TypedReference_ToObject(ILExecThread *_thread, ILTypedRef value)
{
	if(value.type && value.value)
	{
		if(!ILClassIsValueType((ILClass *)(value.type)))
		{
			/* Refers to an object reference which is returned as-is */
			return *((ILObject **)(value.value));
		}
		else
		{
			/* Refers to a value type instance which should be boxed */
			return ILExecThreadBox
				(_thread, ILClassToType((ILClass *)(value.type)), value.value);
		}
	}
	else
	{
		return 0;
	}
}
Beispiel #8
0
/* 
 * Find the nearest type in the type2 heirarchy that is assign 
 * compatible to type1.
 */
static ILType* TryCommonType(ILImage* image, ILType * type1, ILType *type2)
{
	ILClass *classInfo;

	if(type1==NULL) return type2;
	if(type2==NULL) return type1;

	/* Note: boxing conversions are not allowed because both
	 * types have to be reference types.
	 */ 
	if(ILTypeAssignCompatibleNonBoxing(image, type1,type2))
	{
		return type2;
	}

	classInfo=ILClassFromType(image, 0, type2, 0);
	
	if((classInfo = ILClass_ParentClass(classInfo)) != NULL)
	{
		return TryCommonType(image, type1, ILClassToType(classInfo));
	}
	
	return type2;
}
Beispiel #9
0
/*
 * Expand a class instance.
 */
ILClass *ILClassExpand(ILImage *image, ILClass *classInfo,
					   ILType *classArgs, ILType *methodArgs)
{
	ILType *type = ILClassToType(classInfo);
	return ILClassInstantiate(image, type, classArgs, methodArgs);
}