{
if (!type.IsArrayTypeWithoutGenericInterfaces())
{
- MetadataType arrayShadowType = type.Context.SystemModule.GetType("System", "Array`1", throwIfNotFound: false);
+ MetadataType arrayShadowType = type.Context.SystemModule.GetType("System", "Array`1", NotFoundBehavior.ReturnNull);
if (arrayShadowType != null)
{
return arrayShadowType.MakeInstantiatedType(((ArrayType)type).ElementType);
// BadImageFormatException
BadImageFormatGeneric,
+ BadImageFormatSpecific,
}
}
{
// Require System.Object to be present as a minimal sanity check.
// The set of required well-known types is not strictly defined since different .NET profiles implement different subsets.
- MetadataType type = systemModule.GetType("System", s_wellKnownTypeNames[typeIndex], typeIndex == (int)WellKnownType.Object);
+ MetadataType type = systemModule.GetType("System", s_wellKnownTypeNames[typeIndex], typeIndex == (int)WellKnownType.Object ? NotFoundBehavior.Throw : NotFoundBehavior.ReturnNull);
if (type != null)
{
type.SetWellKnownType((WellKnownType)(typeIndex + 1));
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System;
using System.Collections.Generic;
namespace Internal.TypeSystem
/// <summary>
/// Gets a type in this module with the specified name.
+ /// If notFoundBehavior == NotFoundBehavior.ReturnResolutionFailure
+ /// then ModuleDesc.GetTypeResolutionFailure will be set to the failure, and the function will return null
/// </summary>
- public abstract MetadataType GetType(string nameSpace, string name, bool throwIfNotFound = true);
+ public abstract MetadataType GetType(string nameSpace, string name, NotFoundBehavior notFoundBehavior = NotFoundBehavior.Throw);
+
+ [ThreadStatic]
+ public static ResolutionFailure GetTypeResolutionFailure;
/// <summary>
/// Gets the global <Module> type.
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace Internal.TypeSystem
+{
+ public enum NotFoundBehavior
+ {
+ Throw,
+ ReturnNull,
+ ReturnResolutionFailure
+ }
+}
\ No newline at end of file
<data name="BadImageFormatGeneric" xml:space="preserve">
<value>The format of a DLL or executable being loaded is invalid</value>
</data>
+ <data name="BadImageFormatSpecific" xml:space="preserve">
+ <value>The format of a DLL or executable being loaded is invalid with {0}</value>
+ </data>
</root>
--- /dev/null
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+namespace Internal.TypeSystem
+{
+ public sealed class ResolutionFailure
+ {
+ private enum FailureType
+ {
+ TypeLoadException1,
+ TypeLoadException2,
+ TypeLoadException3,
+ MissingMethodException1,
+ MissingFieldException1,
+ MissingAssemblyException1,
+ }
+
+ private ResolutionFailure() { }
+
+ private FailureType _failureType;
+ private string _namespace;
+ private string _name;
+ private string _moduleName;
+ private ModuleDesc _module;
+ private TypeDesc _owningType;
+ private MethodSignature _methodSignature;
+
+
+ public static ResolutionFailure GetTypeLoadResolutionFailure(string nestedTypeName, ModuleDesc module)
+ {
+ ResolutionFailure failure = new ResolutionFailure();
+ failure._failureType = FailureType.TypeLoadException1;
+ failure._name = nestedTypeName;
+ failure._module = module;
+ return failure;
+ }
+
+ public static ResolutionFailure GetTypeLoadResolutionFailure(string @namespace, string name, ModuleDesc module)
+ {
+ ResolutionFailure failure = new ResolutionFailure();
+ failure._failureType = FailureType.TypeLoadException2;
+ failure._namespace = @namespace;
+ failure._name = name;
+ failure._module = module;
+ return failure;
+ }
+
+ public static ResolutionFailure GetTypeLoadResolutionFailure(string @namespace, string name, string moduleName)
+ {
+ ResolutionFailure failure = new ResolutionFailure();
+ failure._failureType = FailureType.TypeLoadException3;
+ failure._namespace = @namespace;
+ failure._name = name;
+ failure._moduleName = moduleName;
+ return failure;
+ }
+
+ public static ResolutionFailure GetMissingMethodFailure(TypeDesc owningType, string methodName, MethodSignature signature)
+ {
+ ResolutionFailure failure = new ResolutionFailure();
+ failure._failureType = FailureType.MissingMethodException1;
+ failure._methodSignature = signature;
+ failure._name = methodName;
+ failure._owningType = owningType;
+ return failure;
+ }
+
+ public static ResolutionFailure GetMissingFieldFailure(TypeDesc owningType, string fieldName)
+ {
+ ResolutionFailure failure = new ResolutionFailure();
+ failure._failureType = FailureType.MissingMethodException1;
+ failure._name = fieldName;
+ failure._owningType = owningType;
+ return failure;
+ }
+
+ public static ResolutionFailure GetAssemblyResolutionFailure(string simpleName)
+ {
+ ResolutionFailure failure = new ResolutionFailure();
+ failure._failureType = FailureType.MissingAssemblyException1;
+ failure._name = simpleName;
+ return failure;
+ }
+
+ public void Throw()
+ {
+ switch(_failureType)
+ {
+ case FailureType.TypeLoadException1:
+ ThrowHelper.ThrowTypeLoadException(_name, _module);
+ break;
+ case FailureType.TypeLoadException2:
+ ThrowHelper.ThrowTypeLoadException(_namespace, _name, _module);
+ break;
+ case FailureType.TypeLoadException3:
+ ThrowHelper.ThrowTypeLoadException(_namespace, _name, _moduleName);
+ break;
+ case FailureType.MissingMethodException1:
+ ThrowHelper.ThrowMissingMethodException(_owningType, _name, _methodSignature);
+ break;
+ case FailureType.MissingFieldException1:
+ ThrowHelper.ThrowMissingFieldException(_owningType, _name);
+ break;
+ case FailureType.MissingAssemblyException1:
+ ThrowHelper.ThrowFileNotFoundException(ExceptionStringID.FileLoadErrorGeneric, _name);
+ break;
+ }
+ }
+ }
+}
ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, Format.Type(@namespace, name), Format.Module(module));
}
+ public static void ThrowTypeLoadException(string @namespace, string name, string moduleName)
+ {
+ ThrowTypeLoadException(ExceptionStringID.ClassLoadGeneral, Format.Type(@namespace, name), moduleName);
+ }
+
[System.Diagnostics.DebuggerHidden]
public static void ThrowTypeLoadException(TypeDesc type)
{
throw new TypeSystemException.BadImageFormatException();
}
+ [System.Diagnostics.DebuggerHidden]
+ public static void ThrowBadImageFormatException(string message)
+ {
+ throw new TypeSystemException.BadImageFormatException(message);
+ }
+
private static partial class Format
{
public static string OwningModule(TypeDesc type)
: base(ExceptionStringID.BadImageFormatGeneric)
{
}
+
+ internal BadImageFormatException(string reason)
+ : base(ExceptionStringID.BadImageFormatSpecific, reason)
+ {
+
+ }
}
}
}
namespaceName = fullName.Substring(0, split);
typeName = fullName.Substring(split + 1);
}
- return module.GetType(namespaceName, typeName, throwIfNotFound);
+ return module.GetType(namespaceName, typeName, throwIfNotFound ? NotFoundBehavior.Throw : NotFoundBehavior.ReturnNull);
}
private static AssemblyName FindAssemblyIfNamePresent(string name)
var metadataReader = MetadataReader;
BlobReader signatureReader = metadataReader.GetBlobReader(metadataReader.GetFieldDefinition(_handle).Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(Module, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(Module, signatureReader, NotFoundBehavior.Throw);
var fieldType = parser.ParseFieldSignature();
return (_fieldType = fieldType);
}
if ((definition.Attributes & FieldAttributes.HasFieldMarshal) != 0)
{
BlobReader marshalAsReader = reader.GetBlobReader(definition.GetMarshallingDescriptor());
- EcmaSignatureParser parser = new EcmaSignatureParser(_type.EcmaModule, marshalAsReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(_type.EcmaModule, marshalAsReader, NotFoundBehavior.Throw);
return parser.ParseMarshalAsDescriptor();
}
var metadataReader = MetadataReader;
BlobReader signatureReader = metadataReader.GetBlobReader(metadataReader.GetMethodDefinition(_handle).Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(Module, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(Module, signatureReader, NotFoundBehavior.Throw);
var signature = parser.ParseMethodSignature();
return (_signature = signature);
}
{
MetadataReader metadataReader = MetadataReader;
BlobReader marshalAsReader = metadataReader.GetBlobReader(parameter.GetMarshallingDescriptor());
- EcmaSignatureParser parser = new EcmaSignatureParser(Module, marshalAsReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(Module, marshalAsReader, NotFoundBehavior.Throw);
MarshalAsDescriptor marshalAs = parser.ParseMarshalAsDescriptor();
Debug.Assert(marshalAs != null);
return marshalAs;
{
MethodDefinitionHandle methodDefinitionHandle = (MethodDefinitionHandle)handle;
TypeDefinitionHandle typeDefinitionHandle = _module._metadataReader.GetMethodDefinition(methodDefinitionHandle).GetDeclaringType();
- EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
+ EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle, NotFoundBehavior.Throw);
item = new EcmaMethod(type, methodDefinitionHandle);
}
break;
{
FieldDefinitionHandle fieldDefinitionHandle = (FieldDefinitionHandle)handle;
TypeDefinitionHandle typeDefinitionHandle = _module._metadataReader.GetFieldDefinition(fieldDefinitionHandle).GetDeclaringType();
- EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle);
+ EcmaType type = (EcmaType)_module.GetObject(typeDefinitionHandle, NotFoundBehavior.Throw);
item = new EcmaField(type, fieldDefinitionHandle);
}
break;
break;
default:
- throw new BadImageFormatException("Unknown metadata token type: " + handle.Kind);
+ ThrowHelper.ThrowBadImageFormatException("unknown metadata token type: " + handle.Kind);
+ item = null;
+ break;
}
switch (handle.Kind)
}
// Bad metadata
- throw new BadImageFormatException();
+ ThrowHelper.ThrowBadImageFormatException();
+ return null;
}
}
}
}
- public sealed override MetadataType GetType(string nameSpace, string name, bool throwIfNotFound = true)
+ public sealed override MetadataType GetType(string nameSpace, string name, NotFoundBehavior notFoundBehavior)
{
var stringComparer = _metadataReader.StringComparer;
{
if (exportedType.IsForwarder)
{
- Object implementation = GetObject(exportedType.Implementation);
+ Object implementation = GetObject(exportedType.Implementation, notFoundBehavior);
+
+ if (implementation == null)
+ return null;
if (implementation is ModuleDesc)
{
return ((ModuleDesc)(implementation)).GetType(nameSpace, name);
}
+ else if (implementation is ResolutionFailure failure)
+ {
+ ModuleDesc.GetTypeResolutionFailure = failure;
+ return null;
+ }
// TODO
throw new NotImplementedException();
}
}
- if (throwIfNotFound)
- ThrowHelper.ThrowTypeLoadException(nameSpace, name, this);
+ if (notFoundBehavior != NotFoundBehavior.ReturnNull)
+ {
+ var failure = ResolutionFailure.GetTypeLoadResolutionFailure(nameSpace, name, this);
+ if (notFoundBehavior == NotFoundBehavior.Throw)
+ failure.Throw();
+
+ ModuleDesc.GetTypeResolutionFailure = failure;
+ return null;
+ }
return null;
}
public TypeDesc GetType(EntityHandle handle)
{
- TypeDesc type = GetObject(handle) as TypeDesc;
+ TypeDesc type = GetObject(handle, NotFoundBehavior.Throw) as TypeDesc;
if (type == null)
- throw new BadImageFormatException("Type expected");
+ ThrowHelper.ThrowBadImageFormatException($"type expected for handle {handle.ToString()}");
return type;
}
public MethodDesc GetMethod(EntityHandle handle)
{
- MethodDesc method = GetObject(handle) as MethodDesc;
+ MethodDesc method = GetObject(handle, NotFoundBehavior.Throw) as MethodDesc;
if (method == null)
- throw new BadImageFormatException("Method expected");
+ ThrowHelper.ThrowBadImageFormatException($"method expected for handle {handle.ToString()}");
return method;
}
public FieldDesc GetField(EntityHandle handle)
{
- FieldDesc field = GetObject(handle) as FieldDesc;
+ FieldDesc field = GetObject(handle, NotFoundBehavior.Throw) as FieldDesc;
if (field == null)
- throw new BadImageFormatException("Field expected");
+ ThrowHelper.ThrowBadImageFormatException($"field expected for handle {handle.ToString()}");
return field;
}
- public Object GetObject(EntityHandle handle)
+ public Object GetObject(EntityHandle handle, NotFoundBehavior notFoundBehavior = NotFoundBehavior.Throw)
{
IEntityHandleObject obj = _resolvedTokens.GetOrCreateValue(handle);
if (obj is EcmaObjectLookupWrapper)
{
- return ((EcmaObjectLookupWrapper)obj).Object;
+ object result = ((EcmaObjectLookupWrapper)obj).Object;
+ if ((result is ResolutionFailure failure) && (notFoundBehavior != NotFoundBehavior.ReturnResolutionFailure))
+ {
+ if (notFoundBehavior == NotFoundBehavior.ReturnNull)
+ return null;
+ else
+ failure.Throw();
+ }
+ return result;
}
else
{
{
MethodSpecification methodSpecification = _metadataReader.GetMethodSpecification(handle);
- MethodDesc methodDef = GetMethod(methodSpecification.Method);
+ object resolvedMethod = GetObject(methodSpecification.Method, NotFoundBehavior.ReturnResolutionFailure);
+ if (resolvedMethod is ResolutionFailure)
+ return resolvedMethod;
+
+ MethodDesc methodDef = resolvedMethod as MethodDesc;
+ if (methodDef == null)
+ ThrowHelper.ThrowBadImageFormatException($"method expected for handle {handle.ToString()}");
BlobReader signatureReader = _metadataReader.GetBlobReader(methodSpecification.Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader, NotFoundBehavior.ReturnResolutionFailure);
TypeDesc[] instantiation = parser.ParseMethodSpecSignature();
+
+ if (instantiation == null)
+ return parser.ResolutionFailure;
+
return Context.GetInstantiatedMethod(methodDef, new Instantiation(instantiation));
}
{
StandaloneSignature signature = _metadataReader.GetStandaloneSignature(handle);
BlobReader signatureReader = _metadataReader.GetBlobReader(signature.Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader, NotFoundBehavior.ReturnResolutionFailure);
MethodSignature methodSig = parser.ParseMethodSignature();
+ if (methodSig == null)
+ return parser.ResolutionFailure;
return methodSig;
}
TypeSpecification typeSpecification = _metadataReader.GetTypeSpecification(handle);
BlobReader signatureReader = _metadataReader.GetBlobReader(typeSpecification.Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader, NotFoundBehavior.ReturnResolutionFailure);
- return parser.ParseType();
+ TypeDesc parsedType = parser.ParseType();
+ if (parsedType == null)
+ return parser.ResolutionFailure;
+ else
+ return parsedType;
}
private Object ResolveMemberReference(MemberReferenceHandle handle)
{
MemberReference memberReference = _metadataReader.GetMemberReference(handle);
- Object parent = GetObject(memberReference.Parent);
+ Object parent = GetObject(memberReference.Parent, NotFoundBehavior.ReturnResolutionFailure);
+
+ if (parent is ResolutionFailure)
+ return parent;
TypeDesc parentTypeDesc = parent as TypeDesc;
if (parentTypeDesc != null)
{
BlobReader signatureReader = _metadataReader.GetBlobReader(memberReference.Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(this, signatureReader, NotFoundBehavior.ReturnResolutionFailure);
string name = _metadataReader.GetString(memberReference.Name);
if (field != null)
return field;
- ThrowHelper.ThrowMissingFieldException(parentTypeDesc, name);
+ return ResolutionFailure.GetMissingFieldFailure(parentTypeDesc, name);
}
else
{
MethodSignature sig = parser.ParseMethodSignature();
+ if (sig == null)
+ return parser.ResolutionFailure;
TypeDesc typeDescToInspect = parentTypeDesc;
Instantiation substitution = default(Instantiation);
typeDescToInspect = baseType;
} while (typeDescToInspect != null);
- ThrowHelper.ThrowMissingMethodException(parentTypeDesc, name, sig);
+ return ResolutionFailure.GetMissingMethodFailure(parentTypeDesc, name, sig);
}
}
else if (parent is MethodDesc)
throw new NotImplementedException("MemberRef to a global function or variable.");
}
- throw new BadImageFormatException();
+ ThrowHelper.ThrowBadImageFormatException();
+ return null;
}
private Object ResolveTypeReference(TypeReferenceHandle handle)
{
TypeReference typeReference = _metadataReader.GetTypeReference(handle);
- Object resolutionScope = GetObject(typeReference.ResolutionScope);
+ Object resolutionScope = GetObject(typeReference.ResolutionScope, NotFoundBehavior.ReturnResolutionFailure);
+ if (resolutionScope is ResolutionFailure)
+ {
+ return resolutionScope;
+ }
if (resolutionScope is ModuleDesc)
{
- return ((ModuleDesc)(resolutionScope)).GetType(_metadataReader.GetString(typeReference.Namespace), _metadataReader.GetString(typeReference.Name));
+ object result = ((ModuleDesc)(resolutionScope)).GetType(_metadataReader.GetString(typeReference.Namespace), _metadataReader.GetString(typeReference.Name), NotFoundBehavior.ReturnResolutionFailure);
+ if (result == null)
+ result = ModuleDesc.GetTypeResolutionFailure;
+ return result;
}
else
if (resolutionScope is MetadataType)
if (result != null)
return result;
- ThrowHelper.ThrowTypeLoadException(typeName, ((MetadataType)resolutionScope).Module);
+ return ResolutionFailure.GetTypeLoadResolutionFailure(typeName, ((MetadataType)resolutionScope).Module);
}
// TODO
an.CultureName = _metadataReader.GetString(assemblyReference.Culture);
an.ContentType = GetContentTypeFromAssemblyFlags(assemblyReference.Flags);
- return _moduleResolver.ResolveAssembly(an);
+ var assembly = _moduleResolver.ResolveAssembly(an, throwIfNotFound: false);
+ if (assembly == null)
+ return ResolutionFailure.GetAssemblyResolutionFailure(an.Name);
+ else
+ return assembly;
}
private Object ResolveExportedType(ExportedTypeHandle handle)
{
ExportedType exportedType = _metadataReader.GetExportedType(handle);
- var implementation = GetObject(exportedType.Implementation);
+ var implementation = GetObject(exportedType.Implementation, NotFoundBehavior.ReturnResolutionFailure);
if (implementation is ModuleDesc)
{
var module = (ModuleDesc)implementation;
string nameSpace = _metadataReader.GetString(exportedType.Namespace);
string name = _metadataReader.GetString(exportedType.Name);
- return module.GetType(nameSpace, name);
+ return module.GetType(nameSpace, name, NotFoundBehavior.ReturnResolutionFailure);
}
else
if (implementation is MetadataType)
string name = _metadataReader.GetString(exportedType.Name);
var nestedType = type.GetNestedType(name);
if (nestedType == null)
- ThrowHelper.ThrowTypeLoadException(name, this);
+ return ResolutionFailure.GetTypeLoadResolutionFailure(name, this);
return nestedType;
}
+ else if (implementation is ResolutionFailure)
+ {
+ return implementation;
+ }
else
{
- throw new BadImageFormatException("Unknown metadata token type for exported type");
+ ThrowHelper.ThrowBadImageFormatException();
+ return null;
}
}
public struct EcmaSignatureParser
{
private TypeSystemContext _tsc;
- private Func<EntityHandle, TypeDesc> _typeResolver;
+ private Func<EntityHandle, NotFoundBehavior, TypeDesc> _typeResolver;
+ private NotFoundBehavior _notFoundBehavior;
private EcmaModule _ecmaModule;
private BlobReader _reader;
+ private ResolutionFailure _resolutionFailure;
private Stack<int> _indexStack;
private List<EmbeddedSignatureData> _embeddedSignatureDataList;
- public EcmaSignatureParser(TypeSystemContext tsc, Func<EntityHandle, TypeDesc> typeResolver, BlobReader reader)
+ public EcmaSignatureParser(TypeSystemContext tsc, Func<EntityHandle, NotFoundBehavior, TypeDesc> typeResolver, BlobReader reader, NotFoundBehavior notFoundBehavior)
{
+ _notFoundBehavior = notFoundBehavior;
_ecmaModule = null;
_tsc = tsc;
_typeResolver = typeResolver;
_reader = reader;
_indexStack = null;
_embeddedSignatureDataList = null;
+ _resolutionFailure = null;
}
- public EcmaSignatureParser(EcmaModule ecmaModule, BlobReader reader)
+ public EcmaSignatureParser(EcmaModule ecmaModule, BlobReader reader, NotFoundBehavior notFoundBehavior)
{
+ _notFoundBehavior = notFoundBehavior;
_ecmaModule = ecmaModule;
_tsc = ecmaModule.Context;
_typeResolver = null;
_reader = reader;
_indexStack = null;
_embeddedSignatureDataList = null;
+ _resolutionFailure = null;
}
+ void SetResolutionFailure(ResolutionFailure failure)
+ {
+ if (_resolutionFailure == null)
+ _resolutionFailure = failure;
+ }
+
+ public ResolutionFailure ResolutionFailure => _resolutionFailure;
+
private TypeDesc ResolveHandle(EntityHandle handle)
{
+ object resolvedValue;
if (_ecmaModule != null)
- return _ecmaModule.GetType(handle);
+ {
+ resolvedValue = _ecmaModule.GetObject(handle, _notFoundBehavior);
+ }
else
- return _typeResolver(handle);
+ {
+ resolvedValue = _typeResolver(handle, _notFoundBehavior);
+ }
+
+ if (resolvedValue == null)
+ return null;
+ if (resolvedValue is ResolutionFailure failure)
+ {
+ SetResolutionFailure(failure);
+ return null;
+ }
+ if (resolvedValue is TypeDesc type)
+ {
+ return type;
+ }
+ else
+ {
+ throw new BadImageFormatException("Type expected");
+ }
}
private TypeDesc GetWellKnownType(WellKnownType wellKnownType)
private TypeDesc ParseType(SignatureTypeCode typeCode)
{
-
if (_indexStack != null)
{
int was = _indexStack.Pop();
case SignatureTypeCode.TypeHandle:
return ResolveHandle(_reader.ReadTypeHandle());
case SignatureTypeCode.SZArray:
- return _tsc.GetArrayType(ParseType());
+ {
+ var elementType = ParseType();
+ if (elementType == null)
+ return null;
+ return _tsc.GetArrayType(elementType);
+ }
case SignatureTypeCode.Array:
{
var elementType = ParseType();
for (int j = 0; j < lowerBoundsCount; j++)
_reader.ReadCompressedInteger();
- return _tsc.GetArrayType(elementType, rank);
+ if (elementType != null)
+ return _tsc.GetArrayType(elementType, rank);
+ else
+ return null;
}
case SignatureTypeCode.ByReference:
- return ParseType().MakeByRefType();
+ {
+ TypeDesc byRefedType = ParseType();
+ if (byRefedType != null)
+ return byRefedType.MakeByRefType();
+ else
+ return null;
+ }
case SignatureTypeCode.Pointer:
- return _tsc.GetPointerType(ParseType());
+ {
+ TypeDesc pointedAtType = ParseType();
+ if (pointedAtType != null)
+ return _tsc.GetPointerType(pointedAtType);
+ else
+ return null;
+ }
case SignatureTypeCode.GenericTypeParameter:
return _tsc.GetSignatureVariable(_reader.ReadCompressedInteger(), false);
case SignatureTypeCode.GenericMethodParameter:
case SignatureTypeCode.GenericTypeInstance:
{
TypeDesc typeDef = ParseType();
- MetadataType metadataTypeDef = typeDef as MetadataType;
- if (metadataTypeDef == null)
- throw new BadImageFormatException();
+ MetadataType metadataTypeDef = null;
+
+ if (typeDef != null)
+ {
+ metadataTypeDef = typeDef as MetadataType;
+ if (metadataTypeDef == null)
+ throw new BadImageFormatException();
+ }
TypeDesc[] instance = new TypeDesc[_reader.ReadCompressedInteger()];
for (int i = 0; i < instance.Length; i++)
+ {
instance[i] = ParseType();
- return _tsc.GetInstantiatedType(metadataTypeDef, new Instantiation(instance));
+ if (instance[i] == null)
+ metadataTypeDef = null;
+ }
+
+ if (metadataTypeDef != null)
+ return _tsc.GetInstantiatedType(metadataTypeDef, new Instantiation(instance));
+ else
+ return null;
}
case SignatureTypeCode.TypedReference:
return GetWellKnownType(WellKnownType.TypedReference);
case SignatureTypeCode.FunctionPointer:
- return _tsc.GetFunctionPointerType(ParseMethodSignatureInternal(skipEmbeddedSignatureData: true));
+ MethodSignature sig = ParseMethodSignatureInternal(skipEmbeddedSignatureData: true);
+ if (sig != null)
+ return _tsc.GetFunctionPointerType(sig);
+ else
+ return null;
default:
throw new BadImageFormatException();
}
EmbeddedSignatureData[] embeddedSignatureDataArray = (_embeddedSignatureDataList == null || _embeddedSignatureDataList.Count == 0 || skipEmbeddedSignatureData) ? null : _embeddedSignatureDataList.ToArray();
- return new MethodSignature(flags, arity, returnType, parameters, embeddedSignatureDataArray);
+ if (_resolutionFailure == null)
+ return new MethodSignature(flags, arity, returnType, parameters, embeddedSignatureDataArray);
+ else
+ return null;
}
public PropertySignature ParsePropertySignature()
{
+ // As PropertySignature is a struct, we cannot return null
+ if (_notFoundBehavior != NotFoundBehavior.Throw)
+ throw new ArgumentException();
+
SignatureHeader header = _reader.ReadSignatureHeader();
if (header.Kind != SignatureKind.Property)
throw new BadImageFormatException();
{
locals = Array.Empty<LocalVariableDefinition>();
}
- return locals;
+ if (_resolutionFailure == null)
+ return locals;
+ else
+ return null;
}
public TypeDesc[] ParseMethodSpecSignature()
{
arguments[i] = ParseType();
}
- return arguments;
+ if (_resolutionFailure == null)
+ return arguments;
+ else
+ return null;
}
public MarshalAsDescriptor ParseMarshalAsDescriptor()
return Array.Empty<LocalVariableDefinition>();
BlobReader signatureReader = metadataReader.GetBlobReader(metadataReader.GetStandaloneSignature(localSignature).Signature);
- EcmaSignatureParser parser = new EcmaSignatureParser(_module, signatureReader);
+ EcmaSignatureParser parser = new EcmaSignatureParser(_module, signatureReader, NotFoundBehavior.Throw);
LocalVariableDefinition[] locals = parser.ParseLocalsSignature();
Interlocked.CompareExchange(ref _locals, locals, null);
return _ilExceptionRegions;
}
- public override object GetObject(int token)
+ public override object GetObject(int token, NotFoundBehavior notFoundBehavior = NotFoundBehavior.Throw)
{
// UserStrings cannot be wrapped in EntityHandle
if ((token & 0xFF000000) == 0x70000000)
return _module.GetUserString(MetadataTokens.UserStringHandle(token));
- return _module.GetObject(MetadataTokens.EntityHandle(token));
+ return _module.GetObject(MetadataTokens.EntityHandle(token), notFoundBehavior);
}
}
}
public static MetadataType GetOptionalHelperType(this TypeSystemContext context, string name)
{
- MetadataType helperType = context.SystemModule.GetType(HelperTypesNamespace, name, throwIfNotFound: false);
+ MetadataType helperType = context.SystemModule.GetType(HelperTypesNamespace, name, NotFoundBehavior.ReturnNull);
return helperType;
}
/// </summary>
public static MetadataType GetKnownType(this ModuleDesc module, string @namespace, string name)
{
- MetadataType type = module.GetType(@namespace, name, false);
+ MetadataType type = module.GetType(@namespace, name, NotFoundBehavior.ReturnNull);
if (type == null)
{
throw new InvalidOperationException(
return (clone == null) ? locals : clone;
}
- public override Object GetObject(int token)
+ public override Object GetObject(int token, NotFoundBehavior notFoundBehavior)
{
- Object o = _methodIL.GetObject(token);
+ Object o = _methodIL.GetObject(token, notFoundBehavior);
if (o is MethodDesc)
{
/// (typically a <see cref="MethodDesc"/>, <see cref="FieldDesc"/>, <see cref="TypeDesc"/>,
/// or <see cref="MethodSignature"/>).
/// </summary>
- public abstract Object GetObject(int token);
+ public abstract Object GetObject(int token, NotFoundBehavior notFoundBehavior = NotFoundBehavior.ReturnNull);
/// <summary>
/// Gets a list of exception regions this method body defines.
{
return _locals;
}
- public override Object GetObject(int token)
+ public override Object GetObject(int token, NotFoundBehavior notFoundBehavior)
{
return _tokens[(token & 0xFFFFFF) - 1];
}
<Compile Include="$(ToolsCommonPath)TypeSystem\Common\ModuleDesc.cs">
<Link>TypeSystem\Common\ModuleDesc.cs</Link>
</Compile>
+ <Compile Include="$(ToolsCommonPath)TypeSystem\Common\NotFoundBehavior.cs">
+ <Link>TypeSystem\Common\NotFoundBehavior.cs</Link>
+ </Compile>
+ <Compile Include="$(ToolsCommonPath)TypeSystem\Common\ResolutionFailure.cs">
+ <Link>TypeSystem\Common\ResolutionFailure.cs</Link>
+ </Compile>
<Compile Include="$(ToolsCommonPath)TypeSystem\Common\TypeSystemEntity.cs">
<Link>TypeSystem\Common\TypeSystemEntity.cs</Link>
</Compile>
int count = 0;
for (int i = 0; i < s_genericRuntimeInterfacesNames.Length; ++i)
{
- MetadataType runtimeInterface =_systemModule.GetType("System.Collections.Generic", s_genericRuntimeInterfacesNames[i], false);
+ MetadataType runtimeInterface =_systemModule.GetType("System.Collections.Generic", s_genericRuntimeInterfacesNames[i], NotFoundBehavior.ReturnNull);
if (runtimeInterface != null)
_genericRuntimeInterfaces[count++] = runtimeInterface;
};
case CorTokenType.mdtMethodDef:
case CorTokenType.mdtMemberRef:
case CorTokenType.mdtMethodSpec:
- object metadataObject = ecmaModule.GetObject(System.Reflection.Metadata.Ecma335.MetadataTokens.EntityHandle((int)entry.Token));
+ object metadataObject = ecmaModule.GetObject(System.Reflection.Metadata.Ecma335.MetadataTokens.EntityHandle((int)entry.Token), NotFoundBehavior.ReturnNull);
if (metadataObject is MethodDesc)
{
associatedMethod = (MethodDesc)metadataObject;
if (!(m is EcmaModule))
continue;
- foundType = (EcmaType)m.GetType(typeNamespace, typeName, throwIfNotFound: false);
+ foundType = (EcmaType)m.GetType(typeNamespace, typeName, NotFoundBehavior.ReturnNull);
if (foundType != null)
{
externalModule = foundType.EcmaModule;
}
else
{
- foundType = (EcmaType)externalModule.GetType(typeNamespace, typeName, throwIfNotFound: false);
+ foundType = (EcmaType)externalModule.GetType(typeNamespace, typeName, NotFoundBehavior.ReturnNull);
}
if (foundType == null)
{
if (EcmaModule.MetadataReader.GetTableRowCount(TableIndex.AssemblyRef) < index)
return null;
- return EcmaModule.GetObject(MetadataTokens.EntityHandle(((int)CorTokenType.mdtAssemblyRef) | index)) as EcmaModule;
+ return EcmaModule.GetObject(MetadataTokens.EntityHandle(((int)CorTokenType.mdtAssemblyRef) | index), NotFoundBehavior.ReturnNull) as EcmaModule;
}
}
// token type is 0, therefore it can't be a type
return new TypeSystemEntityOrUnknown((int)token);
}
- return new TypeSystemEntityOrUnknown((TypeDesc)_ilBody.GetObject((int)token));
+ TypeDesc foundType = _ilBody.GetObject((int)token, NotFoundBehavior.ReturnNull) as TypeDesc;
+ if (foundType == null)
+ {
+ return new TypeSystemEntityOrUnknown((int)token & 0x00FFFFFF);
+ }
+ return new TypeSystemEntityOrUnknown(foundType);
}
catch
{
metadataObject = null;
try
{
- metadataObject = ilBody.GetObject(token);
+ metadataObject = ilBody.GetObject(token, NotFoundBehavior.ReturnNull);
+ if (metadataObject == null)
+ metadataObject = metadataNotResolvable;
}
catch (TypeSystemException)
{
throw new NotImplementedException();
}
- public override MetadataType GetType(string nameSpace, string name, bool throwIfNotFound = true)
+ public override MetadataType GetType(string nameSpace, string name, NotFoundBehavior notFoundBehavior)
{
TypeSystemContext context = Context;
return Context.UniversalCanonType;
else
{
- if (throwIfNotFound)
+ if (notFoundBehavior != NotFoundBehavior.ReturnNull)
{
- throw new TypeLoadException($"{nameSpace}.{name}");
+ var failure = ResolutionFailure.GetTypeLoadResolutionFailure(nameSpace, name, "System.Private.Canon");
+ ModuleDesc.GetTypeResolutionFailure = failure;
+ if (notFoundBehavior == NotFoundBehavior.Throw)
+ failure.Throw();
+
+ return null;
}
return null;
}
<Compile Include="..\..\Common\TypeSystem\Common\ModuleDesc.cs">
<Link>TypeSystem\Common\ModuleDesc.cs</Link>
</Compile>
+ <Compile Include="..\..\Common\TypeSystem\Common\NotFoundBehavior.cs">
+ <Link>TypeSystem\Common\NotFoundBehavior.cs</Link>
+ </Compile>
+ <Compile Include="..\..\Common\TypeSystem\Common\ResolutionFailure.cs">
+ <Link>TypeSystem\Common\ResolutionFailure.cs</Link>
+ </Compile>
<Compile Include="..\..\Common\TypeSystem\Common\TypeSystemEntity.cs">
<Link>TypeSystem\Common\TypeSystemEntity.cs</Link>
</Compile>
MethodDesc IR2RSignatureTypeProvider<TypeDesc, MethodDesc, R2RSigProviderContext>.GetMethodFromMemberRef(MetadataReader reader, MemberReferenceHandle handle, TypeDesc owningTypeOverride)
{
var ecmaModule = (EcmaModule)_tsc.GetModuleForSimpleName(reader.GetString(reader.GetAssemblyDefinition().Name));
- var method = (MethodDesc)ecmaModule.GetObject(handle);
+ var method = (MethodDesc)ecmaModule.GetObject(handle, NotFoundBehavior.ReturnNull);
+ if (method == null)
+ {
+ return null;
+ }
if (owningTypeOverride != null)
{
return _tsc.GetMethodForInstantiatedType(method.GetTypicalMethodDefinition(), (InstantiatedType)owningTypeOverride);
MethodDesc IR2RSignatureTypeProvider<TypeDesc, MethodDesc, R2RSigProviderContext>.GetMethodFromMethodDef(MetadataReader reader, MethodDefinitionHandle handle, TypeDesc owningTypeOverride)
{
var ecmaModule = (EcmaModule)_tsc.GetModuleForSimpleName(reader.GetString(reader.GetAssemblyDefinition().Name));
- var method = (MethodDesc)ecmaModule.GetObject(handle);
+ var method = (MethodDesc)ecmaModule.GetObject(handle, NotFoundBehavior.ReturnNull);
+ if (method == null)
+ {
+ return null;
+ }
if (owningTypeOverride != null)
{
return _tsc.GetMethodForInstantiatedType(method.GetTypicalMethodDefinition(), (InstantiatedType)owningTypeOverride);
TypeDesc ISimpleTypeProvider<TypeDesc>.GetTypeFromDefinition(MetadataReader reader, TypeDefinitionHandle handle, byte rawTypeKind)
{
var ecmaModule = (EcmaModule)_tsc.GetModuleForSimpleName(reader.GetString(reader.GetAssemblyDefinition().Name));
- return (TypeDesc)ecmaModule.GetObject(handle);
+ return (TypeDesc)ecmaModule.GetObject(handle, NotFoundBehavior.ReturnNull);
}
TypeDesc ISimpleTypeProvider<TypeDesc>.GetTypeFromReference(MetadataReader reader, TypeReferenceHandle handle, byte rawTypeKind)
{
var ecmaModule = (EcmaModule)_tsc.GetModuleForSimpleName(reader.GetString(reader.GetAssemblyDefinition().Name));
- return (TypeDesc)ecmaModule.GetObject(handle);
+ return (TypeDesc)ecmaModule.GetObject(handle, NotFoundBehavior.ReturnNull);
}
TypeDesc ISignatureTypeProvider<TypeDesc, R2RSigProviderContext>.GetTypeFromSpecification(MetadataReader reader, R2RSigProviderContext genericContext, TypeSpecificationHandle handle, byte rawTypeKind)
{
var ecmaModule = (EcmaModule)_tsc.GetModuleForSimpleName(reader.GetString(reader.GetAssemblyDefinition().Name));
- return (TypeDesc)ecmaModule.GetObject(handle);
+ return (TypeDesc)ecmaModule.GetObject(handle, NotFoundBehavior.ReturnNull);
}
}
}
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
{
TypeRefSignatureParserProvider parserHelper = new TypeRefSignatureParserProvider(this, peInfo.handleLookup);
- Func<EntityHandle, TypeDesc> resolverFunc = ResolveTypeRefForPeInfo;
+ Func<EntityHandle, NotFoundBehavior, TypeDesc> resolverFunc = ResolveTypeRefForPeInfo;
int memberRefRowCount = peInfo.reader.GetTableRowCount(TableIndex.MemberRef);
for (int row = 1; row <= memberRefRowCount; row++)
{
continue;
}
- EcmaSignatureParser ecmaSigParse = new EcmaSignatureParser(this, ResolveTypeRefForPeInfo, peInfo.reader.GetBlobReader(memberRef.Signature));
+ EcmaSignatureParser ecmaSigParse = new EcmaSignatureParser(this, ResolveTypeRefForPeInfo, peInfo.reader.GetBlobReader(memberRef.Signature), NotFoundBehavior.ReturnNull);
string name = peInfo.reader.GetString(memberRef.Name);
if (memberRef.GetKind() == MemberReferenceKind.Method)
}
}
- TypeDesc ResolveTypeRefForPeInfo(EntityHandle handle)
+ TypeDesc ResolveTypeRefForPeInfo(EntityHandle handle, NotFoundBehavior notFoundBehavior)
{
+ Debug.Assert(notFoundBehavior == NotFoundBehavior.ReturnNull);
TypeRefTypeSystemType type = null;
if (handle.Kind == HandleKind.TypeReference)
{
return type;
}
- public override MetadataType GetType(string nameSpace, string name, bool throwIfNotFound = true)
+ public override MetadataType GetType(string nameSpace, string name, NotFoundBehavior notFoundBehavior)
{
MetadataType type = GetTypeInternal(nameSpace, name);
- if ((type == null) && throwIfNotFound)
- ThrowHelper.ThrowTypeLoadException(nameSpace, name, this);
+ if ((type == null) && notFoundBehavior != NotFoundBehavior.ReturnNull)
+ {
+ ResolutionFailure failure = ResolutionFailure.GetTypeLoadResolutionFailure(nameSpace, name, this);
+ ModuleDesc.GetTypeResolutionFailure = failure;
+ if (notFoundBehavior == NotFoundBehavior.Throw)
+ failure.Throw();
+ }
return type;
}
}