private void ResolveType(MosaType type) { GenericArgumentResolver resolver = new GenericArgumentResolver(); MosaType srcType = type; if (type.GenericArguments.Count > 0) { resolver.PushTypeGenericArguments(type.GenericArguments.GetGenericArguments()); srcType = type.ElementType; Debug.Assert(srcType != null); } using (var mosaType = metadata.Controller.MutateType(type)) { if (srcType.BaseType != null) { mosaType.BaseType = metadata.Loader.GetType(resolver.Resolve(srcType.BaseType.GetTypeSig())); } if (srcType.DeclaringType != null) { mosaType.DeclaringType = metadata.Loader.GetType(resolver.Resolve(srcType.DeclaringType.GetTypeSig())); mosaType.Namespace = srcType.DeclaringType.Namespace; } var ifaces = new List <MosaType>(srcType.Interfaces); mosaType.Interfaces.Clear(); for (int i = 0; i < ifaces.Count; i++) { mosaType.Interfaces.Add(metadata.Loader.GetType(resolver.Resolve(ifaces[i].GetTypeSig()))); } mosaType.HasOpenGenericParams = type.GetTypeSig().HasOpenGenericParameter(); ResolveCustomAttributes(mosaType, srcType.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Definition); } // Add type again to make it easier to find metadata.Controller.AddType(type); }
public static TypeSig GetTypeSig(this MosaType type) { return(type.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Signature); }
public void Resolve() { foreach (var unit in metadata.Loader.LoadedUnits) { if (unit is MosaType) { MosaType type = (MosaType)unit; using (var mosaType = metadata.Controller.MutateType(type)) { TypeDef typeDef = type.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Definition; if (typeDef.BaseType != null) { mosaType.BaseType = metadata.Loader.GetType(typeDef.BaseType.ToTypeSig()); } if (typeDef.DeclaringType != null) { mosaType.DeclaringType = metadata.Loader.GetType(typeDef.DeclaringType.ToTypeSig()); } if (typeDef.IsEnum) { mosaType.ElementType = metadata.Loader.GetType(typeDef.GetEnumUnderlyingType()); } foreach (var iface in typeDef.Interfaces) { mosaType.Interfaces.Add(metadata.Loader.GetType(iface.Interface.ToTypeSig())); } if (typeDef.BaseType != null) { ResolveInterfacesInBaseTypes(mosaType, type.BaseType); } } ResolveType(type); } else if (unit is MosaField || unit is MosaMethod || unit is MosaModule || unit is MosaProperty) { resolveQueue.Enqueue(unit); } } while (resolveQueue.Count > 0) { MosaUnit unit = resolveQueue.Dequeue(); if (unit is MosaType) { ResolveType((MosaType)unit); } if (unit is MosaField) { ResolveField((MosaField)unit); } if (unit is MosaMethod) { ResolveMethod((MosaMethod)unit); } if (unit is MosaProperty) { ResolveProperty((MosaProperty)unit); } if (unit is MosaModule) { MosaModule module = (MosaModule)unit; using (var mosaModule = metadata.Controller.MutateModule(module)) ResolveCustomAttributes(mosaModule, module.GetUnderlyingObject <UnitDesc <ModuleDef, object> >().Definition); } } foreach (var module in metadata.Cache.Modules.Values) { ModuleDef moduleDef = module.GetUnderlyingObject <UnitDesc <ModuleDef, object> >().Definition; if (moduleDef.EntryPoint != null) { using (var mosaModule = metadata.Controller.MutateModule(module)) mosaModule.EntryPoint = metadata.Cache.GetMethodByToken(new ScopedToken(moduleDef, moduleDef.EntryPoint.MDToken)); } } while (arrayResolveQueue.Count > 0) { MosaType type = arrayResolveQueue.Dequeue(); type.FinishSZArray(); } }
public void AddType(MosaType type) { typeLookup.Add(type.GetUnderlyingObject<UnitDesc<TypeDef, TypeSig>>().Token, type); }
private MosaType LoadGenericTypeInstanceSig(GenericInstSig typeSig) { //Debug.Assert(false, typeSig.FullName); MosaType origin = GetType(typeSig.GenericType); MosaType result = metadata.Controller.CreateType(origin); var desc = result.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >(); using (var resultType = metadata.Controller.MutateType(result)) { resultType.UnderlyingObject = desc.Clone(typeSig); resultType.ElementType = origin; foreach (var genericArg in typeSig.GenericArguments) { resultType.GenericArguments.Add(GetType(genericArg)); } metadata.Resolver.EnqueueForResolve(result); GenericArgumentResolver resolver = new GenericArgumentResolver(); resolver.PushTypeGenericArguments(typeSig.GenericArguments); for (int i = 0; i < result.Methods.Count; i++) { MosaMethod method = metadata.Controller.CreateMethod(result.Methods[i]); using (var mosaMethod = metadata.Controller.MutateMethod(method)) { mosaMethod.DeclaringType = result; mosaMethod.UnderlyingObject = method.GetUnderlyingObject <UnitDesc <MethodDef, MethodSig> >(); } resultType.Methods[i] = method; metadata.Resolver.EnqueueForResolve(method); } for (int i = 0; i < result.Fields.Count; i++) { MosaField field = metadata.Controller.CreateField(result.Fields[i]); using (var mosaField = metadata.Controller.MutateField(field)) { mosaField.DeclaringType = result; mosaField.UnderlyingObject = field.GetUnderlyingObject <UnitDesc <FieldDef, FieldSig> >(); } resultType.Fields[i] = field; metadata.Resolver.EnqueueForResolve(field); } for (int i = 0; i < result.Properties.Count; i++) { MosaProperty property = metadata.Controller.CreateProperty(result.Properties[i]); PropertySig newSig = property.GetPropertySig().Clone(); newSig.RetType = resolver.Resolve(newSig.RetType); using (var mosaProperty = metadata.Controller.MutateProperty(property)) { mosaProperty.DeclaringType = result; mosaProperty.UnderlyingObject = property.GetUnderlyingObject <UnitDesc <PropertyDef, PropertySig> >(); } resultType.Properties[i] = property; metadata.Resolver.EnqueueForResolve(property); } resultType.HasOpenGenericParams = typeSig.HasOpenGenericParameter(); } metadata.Controller.AddType(result); return(result); }
private MosaType Load(TypeSig typeSig) { if (typeSig is LeafSig) { if (typeSig is TypeDefOrRefSig) { throw new AssemblyLoadException(); // Should have been loaded in MetadataLoader } else if (typeSig is GenericInstSig) { return(LoadGenericTypeInstanceSig((GenericInstSig)typeSig)); } else if (typeSig is GenericSig) { return(LoadGenericParam((GenericSig)typeSig)); } else if (typeSig is FnPtrSig) { MethodSig fnPtr = (MethodSig)((FnPtrSig)typeSig).MethodSig; MosaType returnType = GetType(fnPtr.RetType); List <MosaParameter> pars = new List <MosaParameter>(); for (int i = 0; i < fnPtr.Params.Count; i++) { var parameter = metadata.Controller.CreateParameter(); using (var mosaParameter = metadata.Controller.MutateParameter(parameter)) { mosaParameter.Name = "A_" + i; mosaParameter.ParameterAttributes = MosaParameterAttributes.In; mosaParameter.ParameterType = GetType(fnPtr.Params[i]); } pars.Add(parameter); } return(metadata.TypeSystem.ToFnPtr(new MosaMethodSignature(returnType, pars))); } else { throw new NotSupportedException(); } } else // Non-leaf signature { MosaType elementType = GetType(typeSig.Next); MosaType result; switch (typeSig.ElementType) { case ElementType.Ptr: result = elementType.ToUnmanagedPointer(); using (var ptrType = metadata.Controller.MutateType(result)) ptrType.UnderlyingObject = elementType.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Clone(typeSig); break; case ElementType.ByRef: result = elementType.ToManagedPointer(); using (var ptrType = metadata.Controller.MutateType(result)) ptrType.UnderlyingObject = elementType.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Clone(typeSig); break; case ElementType.CModReqd: case ElementType.CModOpt: result = metadata.Controller.CreateType(elementType); using (var modType = metadata.Controller.MutateType(result)) { modType.Modifier = GetType(((ModifierSig)typeSig).Modifier.ToTypeSig()); modType.UnderlyingObject = elementType.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Clone(typeSig); modType.ElementType = elementType; } break; case ElementType.Pinned: result = elementType; // Pinned types are indicated in MosaLocal return(result); // Don't add again to controller case ElementType.SZArray: GetType(new GenericInstSig(szHelperSig, typeSig.Next)); GetType(new GenericInstSig(iListSig, typeSig.Next)); result = elementType.ToSZArray(); using (var arrayType = metadata.Controller.MutateType(result)) arrayType.UnderlyingObject = elementType.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Clone(typeSig); metadata.Resolver.EnqueueForArrayResolve(result); return(result); case ElementType.Array: ArraySig array = (ArraySig)typeSig; MosaArrayInfo arrayInfo = new MosaArrayInfo(array.LowerBounds, array.Rank, array.Sizes); result = elementType.ToArray(arrayInfo); using (var arrayType = metadata.Controller.MutateType(result)) arrayType.UnderlyingObject = elementType.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Clone(typeSig); break; default: throw new AssemblyLoadException(); } metadata.Controller.AddType(result); return(result); } }
private void ResolveType(MosaType type) { GenericArgumentResolver resolver = new GenericArgumentResolver(); if (type.GenericArguments.Count > 0) resolver.PushTypeGenericArguments(type.GenericArguments.GetGenericArguments()); using (var mosaType = metadata.Controller.MutateType(type)) { if (type.BaseType != null) mosaType.BaseType = metadata.Loader.GetType(resolver.Resolve(type.BaseType.GetTypeSig())); if (type.DeclaringType != null) mosaType.DeclaringType = metadata.Loader.GetType(resolver.Resolve(type.DeclaringType.GetTypeSig())); for (int i = 0; i < type.Interfaces.Count; i++) mosaType.Interfaces[i] = metadata.Loader.GetType(resolver.Resolve(type.Interfaces[i].GetTypeSig())); mosaType.HasOpenGenericParams = type.GetTypeSig().HasOpenGenericParameter(); ResolveCustomAttributes(mosaType, type.GetUnderlyingObject<UnitDesc<TypeDef, TypeSig>>().Definition); } }
public void AddType(MosaType type) { typeLookup.Add(type.GetUnderlyingObject <UnitDesc <TypeDef, TypeSig> >().Token, type); }