Move missing type support into universe and optionally allow missing types to be resolved inside all Assemblies.

This commit is contained in:
jfrijters 2011-01-20 14:31:10 +00:00
Родитель 4eb105478e
Коммит 175af3395c
6 изменённых файлов: 61 добавлений и 97 удалений

Просмотреть файл

@ -67,12 +67,7 @@ namespace IKVM.Reflection
// - ResolveType can return a MissingType
internal Type ResolveType(TypeName typeName)
{
return FindType(typeName) ?? GetMissingType(typeName);
}
internal virtual Type GetMissingType(TypeName name)
{
return null;
return FindType(typeName) ?? universe.GetMissingType(this.ManifestModule, null, typeName);
}
public Module[] GetModules()

Просмотреть файл

@ -64,7 +64,6 @@ namespace IKVM.Reflection.Emit
private readonly List<CustomAttributeBuilder> customAttributes = new List<CustomAttributeBuilder>();
private readonly List<CustomAttributeBuilder> declarativeSecurity = new List<CustomAttributeBuilder>();
private readonly List<Type> typeForwarders = new List<Type>();
private Dictionary<ScopedTypeName, Type> missingTypes;
private struct ResourceFile
{
@ -73,34 +72,6 @@ namespace IKVM.Reflection.Emit
internal ResourceAttributes Attributes;
}
private struct ScopedTypeName : IEquatable<ScopedTypeName>
{
private readonly Type declaringType;
private readonly TypeName name;
internal ScopedTypeName(Type declaringType, TypeName name)
{
this.declaringType = declaringType;
this.name = name;
}
public override bool Equals(object obj)
{
ScopedTypeName? other = obj as ScopedTypeName?;
return other != null && ((IEquatable<ScopedTypeName>)other.Value).Equals(this);
}
public override int GetHashCode()
{
return declaringType == null ? name.GetHashCode() : declaringType.GetHashCode() * 7 + name.GetHashCode();
}
bool IEquatable<ScopedTypeName>.Equals(ScopedTypeName other)
{
return other.declaringType == declaringType && other.name == name;
}
}
internal AssemblyBuilder(Universe universe, AssemblyName name, string dir, PermissionSet requiredPermissions, PermissionSet optionalPermissions, PermissionSet refusedPermissions)
: base(universe)
{
@ -557,35 +528,6 @@ namespace IKVM.Reflection.Emit
return null;
}
internal override Type GetMissingType(TypeName name)
{
return GetMissingType(this.ManifestModule, null, name);
}
internal Type GetMissingType(Module module, Type declaringType, TypeName name)
{
if (missingTypes == null)
{
return null;
}
ScopedTypeName stn = new ScopedTypeName(declaringType, name);
Type type;
if (!missingTypes.TryGetValue(stn, out type))
{
type = new MissingType(module, declaringType, name.Namespace, name.Name);
missingTypes.Add(stn, type);
}
return type;
}
public void __EnableMissingTypeResolution()
{
if (missingTypes == null)
{
missingTypes = new Dictionary<ScopedTypeName, Type>();
}
}
public override string ImageRuntimeVersion
{
get { return imageRuntimeVersion; }

Просмотреть файл

@ -1037,11 +1037,6 @@ namespace IKVM.Reflection.Emit
{
get { return token == 0x02000001; }
}
internal override Type ResolveNestedType(TypeName typeName)
{
return base.ResolveNestedType(typeName) ?? ((AssemblyBuilder)ModuleBuilder.Assembly).GetMissingType(this.Module, this, typeName);
}
}
sealed class BakedType : Type

Просмотреть файл

@ -98,7 +98,6 @@ namespace IKVM.Reflection
sealed class MissingAssembly : Assembly
{
private readonly Dictionary<TypeName, Type> types = new Dictionary<TypeName, Type>();
private readonly MissingModule module;
private readonly string name;
@ -109,17 +108,6 @@ namespace IKVM.Reflection
this.name = name;
}
internal override Type GetMissingType(TypeName name)
{
Type type;
if (!types.TryGetValue(name, out type))
{
type = new MissingType(module, null, name.Namespace, name.Name);
types.Add(name, type);
}
return type;
}
public override Type[] GetTypes()
{
throw new MissingAssemblyException(this);
@ -353,7 +341,6 @@ namespace IKVM.Reflection
private readonly Type declaringType;
private readonly string ns;
private readonly string name;
private Dictionary<TypeName, Type> types;
internal MissingType(Module module, Type declaringType, string ns, string name)
{
@ -363,19 +350,9 @@ namespace IKVM.Reflection
this.name = name;
}
internal override Type ResolveNestedType(TypeName typeName)
internal override Type FindNestedType(TypeName name)
{
if (types == null)
{
types = new Dictionary<TypeName, Type>();
}
Type type;
if (!types.TryGetValue(typeName, out type))
{
type = new MissingType(module, this, typeName.Namespace, typeName.Name);
types.Add(typeName, type);
}
return type;
return null;
}
public override Type DeclaringType

Просмотреть файл

@ -747,13 +747,13 @@ namespace IKVM.Reflection
return GetConstructor(bindingAttr, binder, types, modifiers);
}
internal virtual Type ResolveNestedType(TypeName typeName)
internal Type ResolveNestedType(TypeName typeName)
{
return FindNestedType(typeName);
return FindNestedType(typeName) ?? Module.universe.GetMissingType(Module, this, typeName);
}
// unlike the public API, this takes the namespace and name into account
internal Type FindNestedType(TypeName name)
internal virtual Type FindNestedType(TypeName name)
{
foreach (Type type in __GetDeclaredTypes())
{

Просмотреть файл

@ -84,6 +84,8 @@ namespace IKVM.Reflection
private readonly List<AssemblyBuilder> dynamicAssemblies = new List<AssemblyBuilder>();
private readonly Dictionary<string, Assembly> assembliesByName = new Dictionary<string, Assembly>();
private readonly Dictionary<System.Type, Type> importedTypes = new Dictionary<System.Type, Type>();
private Dictionary<ScopedTypeName, Type> missingTypes;
private bool resolveMissingTypes;
private Type typeof_System_Object;
private Type typeof_System_ValueType;
private Type typeof_System_Enum;
@ -800,5 +802,58 @@ namespace IKVM.Reflection
{
return new MissingAssembly(this, assemblyName);
}
public void EnableMissingTypeResolution()
{
resolveMissingTypes = true;
}
private struct ScopedTypeName : IEquatable<ScopedTypeName>
{
private readonly object scope;
private readonly TypeName name;
internal ScopedTypeName(object scope, TypeName name)
{
this.scope = scope;
this.name = name;
}
public override bool Equals(object obj)
{
ScopedTypeName? other = obj as ScopedTypeName?;
return other != null && ((IEquatable<ScopedTypeName>)other.Value).Equals(this);
}
public override int GetHashCode()
{
return scope.GetHashCode() * 7 + name.GetHashCode();
}
bool IEquatable<ScopedTypeName>.Equals(ScopedTypeName other)
{
return other.scope == scope && other.name == name;
}
}
internal Type GetMissingType(Module module, Type declaringType, TypeName typeName)
{
if (resolveMissingTypes || module.Assembly.__IsMissing)
{
if (missingTypes == null)
{
missingTypes = new Dictionary<ScopedTypeName, Type>();
}
ScopedTypeName stn = new ScopedTypeName(declaringType ?? (object)module, typeName);
Type type;
if (!missingTypes.TryGetValue(stn, out type))
{
type = new MissingType(module, declaringType, typeName.Namespace, typeName.Name);
missingTypes.Add(stn, type);
}
return type;
}
return null;
}
}
}