Пример #1
0
 internal FieldSignatureConverter(PEFileToObjectModel peFileToObjectModel, MetadataObject moduleField, MemoryReader signatureMemoryReader)
     : base(peFileToObjectModel, signatureMemoryReader, moduleField)
 {
     //^ base;
       //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
       this.FirstByte = this.SignatureMemoryReader.ReadByte();
       if (!SignatureHeader.IsFieldSignature(this.FirstByte)) {
     //  Error...
       }
       bool isPinned;
       this.customModifiers = this.GetCustomModifiers(out isPinned);
       this.TypeReference = this.GetTypeReference();
 }
Пример #2
0
 internal MethodDefSignatureConverter(PEFileToObjectModel peFileToObjectModel, MethodDefinition moduleMethod, MemoryReader signatureMemoryReader)
   : base(peFileToObjectModel, signatureMemoryReader, moduleMethod) {
   //  TODO: Check minimum required size of the signature...
   this.FirstByte = this.SignatureMemoryReader.ReadByte();
   if (SignatureHeader.IsGeneric(this.FirstByte)) {
     this.GenericParamCount = (uint)this.SignatureMemoryReader.ReadCompressedUInt32();
   }
   this.paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   byte retByte = this.SignatureMemoryReader.PeekByte(0);
   bool isReturnByReference = false;
   if (retByte == ElementType.Void) {
     this.ReturnTypeReference = peFileToObjectModel.PlatformType.SystemVoid;
     this.SignatureMemoryReader.SkipBytes(1);
   } else if (retByte == ElementType.TypedReference) {
     this.ReturnTypeReference = peFileToObjectModel.PlatformType.SystemTypedReference;
     this.SignatureMemoryReader.SkipBytes(1);
   } else {
     if (retByte == ElementType.ByReference) {
       isReturnByReference = true;
       this.SignatureMemoryReader.SkipBytes(1);
     }
     this.ReturnTypeReference = this.GetTypeReference();
   }
   PEFileReader peFileReader = peFileToObjectModel.PEFileReader;
   uint paramRowCount;
   uint paramRowStart = peFileReader.GetParamInformation(moduleMethod.MethodDefRowId, out paramRowCount);
   uint paramRowEnd = paramRowStart + paramRowCount;
   ParamInfo[] paramInfoArray = new ParamInfo[paramRowCount];
   if (peFileReader.UseParamPtrTable) {
     for (uint paramRowIter = paramRowStart; paramRowIter < paramRowEnd; ++paramRowIter) {
       uint paramRowId = peFileReader.ParamPtrTable.GetParamFor(paramRowIter);
       ParamRow paramRow = peFileReader.ParamTable[paramRowId];
       //  TODO: Error check if seqence is in proper range...
       paramInfoArray[paramRowId - paramRowStart] = new ParamInfo(paramRowId, paramRow.Sequence, peFileToObjectModel.GetNameFromOffset(paramRow.Name), paramRow.Flags);
     }
   } else {
     for (uint paramRowId = paramRowStart; paramRowId < paramRowEnd; ++paramRowId) {
       ParamRow paramRow = peFileReader.ParamTable[paramRowId];
       //  TODO: Error check if seqence is in proper range...
       paramInfoArray[paramRowId - paramRowStart] = new ParamInfo(paramRowId, paramRow.Sequence, peFileToObjectModel.GetNameFromOffset(paramRow.Name), paramRow.Flags);
     }
   }
   if (paramRowCount > 0 && paramInfoArray[0].ParamSequence == 0) {
     ParamFlags paramFlag = paramInfoArray[0].ParamFlags;
     if (isReturnByReference) {
       paramFlag |= ParamFlags.ByReference;
     }
     this.ReturnParameter = new ReturnParameter(this.PEFileToObjectModel, paramInfoArray[0].ParamName, paramFlag, paramInfoArray[0].ParamRowId);
   } else {
     this.ReturnParameter = new ReturnParameter(this.PEFileToObjectModel, Dummy.Name, isReturnByReference ? ParamFlags.ByReference : 0, 0);
   }
   this.ParamInfoArray = paramInfoArray;
   if (this.paramCount > 0) {
     IParameterDefinition[] moduleParamArr = this.GetModuleParameters(true, moduleMethod, this.paramCount);
     this.paramCount = moduleParamArr.Length;
     if (this.paramCount > 0) this.Parameters = moduleParamArr;
   }
 }
Пример #3
0
 internal PropertySignatureConverter(PEFileToObjectModel peFileToObjectModel, PropertyDefinition moduleProperty, MemoryReader signatureMemoryReader)
   : base(peFileToObjectModel, signatureMemoryReader, moduleProperty) {
   this.parameters = Enumerable<IParameterDefinition>.Empty;
   //  TODO: Check minimum required size of the signature...
   this.firstByte = this.SignatureMemoryReader.ReadByte();
   if (!SignatureHeader.IsPropertySignature(this.firstByte)) {
     //  Error...
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.returnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   if (this.SignatureMemoryReader.PeekByte(0) == ElementType.ByReference) {
     this.returnValueIsByReference = true;
     this.SignatureMemoryReader.SkipBytes(1);
   }
   this.type = this.GetTypeReference();
   if (paramCount > 0) {
     IParameterDefinition[] moduleParamArr = this.GetModuleParameters(false, moduleProperty, paramCount);
     if (moduleParamArr.Length > 0) this.parameters = IteratorHelper.GetReadonly(moduleParamArr);
   }
 }
Пример #4
0
 internal SignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   MemoryReader signatureMemoryReader,
   MetadataObject metadataOwnerObject
 )
   //^ requires signatureMemoryReader.Length > 0;
 {
   this.PEFileToObjectModel = peFileToObjectModel;
   this.SignatureMemoryReader = signatureMemoryReader;
   this.MetadataOwnerObject = metadataOwnerObject;
   this.ModuleMethodReference = metadataOwnerObject as IMethodReference;
   this.ModuleMemberReference = metadataOwnerObject as ITypeMemberReference;
   TypeMember/*?*/ moduleTypeMember = metadataOwnerObject as TypeMember;
   if (moduleTypeMember != null) {
     this.ModuleGenericType = moduleTypeMember.ContainingTypeDefinition as TypeBase;
     this.ModuleGenericMethod = moduleTypeMember as GenericMethod;
     return;
   }
   var moduleGenericType = metadataOwnerObject as TypeBase;
   if (moduleGenericType != null) {
     this.ModuleGenericType = moduleGenericType;
     return;
   }
   GenericTypeParameter/*?*/ genericTypeParam = metadataOwnerObject as GenericTypeParameter;
   if (genericTypeParam != null) {
     this.ModuleGenericType = genericTypeParam.OwningGenericType;
     return;
   }
   GenericMethodParameter/*?*/ genericMethodParam = metadataOwnerObject as GenericMethodParameter;
   if (genericMethodParam != null) {
     this.ModuleGenericType = genericMethodParam.OwningGenericMethod.ContainingTypeDefinition as TypeBase;
     this.ModuleGenericMethod = genericMethodParam.OwningGenericMethod;
     return;
   }
 }
Пример #5
0
 /// <summary>
 /// Method to open the module in the MetadataReader. This method loads the module and returns the object corresponding to the opened module.
 /// Also returns the ModuleIDentifier corresponding to the module as the out parameter. Modules are opened as if they are not contained in any assembly.
 /// </summary>
 /// <param name="binaryDocument">The binary document that needes to be opened as an module.</param>
 /// <param name="moduleIdentity">Contains the module identity of the binary document in case it is an module.</param>
 /// <returns>Module that is loaded or Dummy.Module in case module could not be loaded.</returns>
 public IModule OpenModule(
   IBinaryDocument binaryDocument,
   out ModuleIdentity/*?*/ moduleIdentity
 ) {
   moduleIdentity = null;
   lock (GlobalLock.LockingObject) {
     IBinaryDocumentMemoryBlock/*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument);
     if (binaryDocumentMemoryBlock == null) {
       //  Error...
       return Dummy.Module;
     }
     PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock);
     if (peFileReader.ReaderState < ReaderState.Metadata) {
       //  Error...
       return Dummy.Module;
     }
     //^ assert peFileReader.ReaderState >= ReaderState.Metadata;
     if (peFileReader.IsAssembly) {
       AssemblyIdentity assemblyIdentity = this.GetAssemblyIdentifier(peFileReader);
       moduleIdentity = assemblyIdentity;
       Assembly/*?*/ lookupAssembly = this.LookupAssembly(null, assemblyIdentity);
       if (lookupAssembly != null) {
         return lookupAssembly;
       }
     } else {
       moduleIdentity = this.GetModuleIdentifier(peFileReader);
       Module/*?*/ lookupModule = this.LookupModule(null, moduleIdentity);
       if (lookupModule != null) {
         return lookupModule;
       }
     }
     try {
       PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, moduleIdentity, null, this.metadataReaderHost.PointerSize);
       this.LoadedModule(peFileToObjectModel.Module);
       Assembly/*?*/ assembly = peFileToObjectModel.Module as Assembly;
       if (assembly != null) {
         this.OpenMemberModules(binaryDocument, assembly);
       }
       return peFileToObjectModel.Module;
     } catch (MetadataReaderException) {
       //  Error...
     }
   }
   return Dummy.Module;
 }
Пример #6
0
 // Changes the name of any method that's defined in a winmd that implements IClosable.Close() to 'Dispose'
 // so that managed consumers can call type.Dispose() directly (without casting to IDisposable).
 // This is identical to the behavior one sees when one uses ildasm's "/project" option to view the contents
 // of a winmd.
 void IWindowsRuntimeMetadataReaderHost.FixUpNameForMethodThatImplementsIClosable(
   PEFileToObjectModel peFileToObjectModel,
   uint methodDefRowId,
   MethodFlags flags,
   TypeBase parentModuleType,
   ref IName methodName
 ) {
   if (projectToCLRTypes) {
     INamespaceTypeDefinition parentNamespaceType = parentModuleType as INamespaceTypeDefinition;
     if (parentNamespaceType != null && IsWinMD(parentNamespaceType.ContainingUnitNamespace.Unit) && parentNamespaceType.IsClass) {
       // Is the method part of a class that is defined in a winmd?
       // Note: We don't care about structs since WinRT structs can't implement interfaces.
       uint paramCount;
       peFileToObjectModel.PEFileReader.GetParamInformation(methodDefRowId, out paramCount);
       if (paramCount == 0) {
         // Is this a method that takes 0 parameters?
         // Note: Desktop CLR doesn't check whether the method is public (or whether it is named Close) - so we don't either.
         uint methodImplStart, methodImplEnd;
         peFileToObjectModel.GetMethodImplInfoForType(parentModuleType, out methodImplStart, out methodImplEnd);
         // Does this method implement IClosable.Close()?
         for (uint methodImplIter = methodImplStart; methodImplIter < methodImplEnd; ++methodImplIter) {
           MethodImplRow methodImplRow = peFileToObjectModel.PEFileReader.MethodImplTable[methodImplIter];
           uint tokenKind = methodImplRow.MethodBody & TokenTypeIds.TokenTypeMask;
           uint rowId = methodImplRow.MethodBody & TokenTypeIds.RIDMask;
           IMethodReference implementedMethod = peFileToObjectModel.GetMethodReferenceForToken(parentModuleType, methodImplRow.MethodDeclaration);
           INamespaceTypeReference implementedInterface = implementedMethod.ContainingType as INamespaceTypeReference;
           if (implementedInterface != null &&
               tokenKind == TokenTypeIds.MethodDef && rowId == methodDefRowId) {
             string implementedInterfaceName = TypeHelper.GetTypeName(implementedInterface);
             // Note: We will see redirected type IDisposable here (instead of IClosable) since projection support is enabled.
             if (implementedInterfaceName == "System.IDisposable") {
               // Note: We have a bug here. We should be checking whether the class (or one of its base classes) already has a method
               // named Dispose before we introduce a new one with the same signature. However, CLR has the same bug - in this case,
               // the class ends up with two methods named Dispose with the same signature. If someone were to try calling class.Dispose()
               // in this case, they would see an error from the C# compiler (i.e. C# cannot resolve the ambiguity between the two identical
               // methods) - but calling either of the Dispose() methods via their respective interfaces (e.g. ((IDisposable)class).Dispose())
               // should work. The case where we would end up with two Dispose() methods as described above should be very rare for a winmd
               // class. For now, we replicate the buggy CLR behavior since we want to avoid looking at all methods in the class / in all its
               // base classes (some of which may be in a different assembly) here.
               methodName = NameTable.GetNameFor("Dispose");
               break;
             }
           }
         }
       }
     }
   }
 }
Пример #7
0
 internal CustomAttributeDecoder(PEFileToObjectModel peFileToObjectModel, MemoryReader signatureMemoryReader, uint customAttributeRowId,
   IMethodReference attributeConstructor)
   : base(peFileToObjectModel, signatureMemoryReader) {
   this.CustomAttribute = Dummy.CustomAttribute;
   ushort prolog = this.SignatureMemoryReader.ReadUInt16();
   if (prolog != SerializationType.CustomAttributeStart) return;
   int len = attributeConstructor.ParameterCount;
   IMetadataExpression[]/*?*/ exprList = len == 0 ? null : new IMetadataExpression[len];
   int i = 0;
   foreach (var parameter in attributeConstructor.Parameters) {
     var parameterType = parameter.Type;
     if (parameterType is Dummy) {
       //  Error...
       return;
     }
     ExpressionBase/*?*/ argument = this.ReadSerializedValue(parameterType);
     if (argument == null) {
       //  Error...
       this.decodeFailed = true;
       return;
     }
     exprList[i++] = argument;
   }
   IMetadataNamedArgument[]/*?*/ namedArgumentArray = null;
   if (2 <= (int)this.SignatureMemoryReader.RemainingBytes) {
     ushort numOfNamedArgs = this.SignatureMemoryReader.ReadUInt16();
     if (numOfNamedArgs > 0) {
       namedArgumentArray = new IMetadataNamedArgument[numOfNamedArgs];
       for (i = 0; i < numOfNamedArgs; ++i) {
         if (0 >= (int)this.SignatureMemoryReader.RemainingBytes) break;
         bool isField = this.SignatureMemoryReader.ReadByte() == SerializationType.Field;
         ITypeReference/*?*/ memberType = this.GetFieldOrPropType();
         if (memberType == null) {
           //  Error...
           return;
         }
         string/*?*/ memberStr = this.GetSerializedString();
         if (memberStr == null)
           return;
         IName memberName = this.PEFileToObjectModel.NameTable.GetNameFor(memberStr);
         ExpressionBase/*?*/ value = this.ReadSerializedValue(memberType);
         if (value == null) {
           //  Error...
           return;
         }
         ITypeReference/*?*/ moduleTypeRef = attributeConstructor.ContainingType;
         if (moduleTypeRef == null) {
           //  Error...
           return;
         }
         FieldOrPropertyNamedArgumentExpression namedArg = new FieldOrPropertyNamedArgumentExpression(memberName, moduleTypeRef, isField, memberType, value);
         namedArgumentArray[i] = namedArg;
       }
     }
   }
   this.CustomAttribute = peFileToObjectModel.ModuleReader.metadataReaderHost.Rewrite(peFileToObjectModel.Module,
     new CustomAttribute(peFileToObjectModel, customAttributeRowId, attributeConstructor, exprList, namedArgumentArray));
 }
Пример #8
0
 //^ [NotDelayed]
 internal MethodSpecSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   MetadataObject owningObject,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, owningObject) {
   //^ this.GenericTypeArguments = TypeCache.EmptyTypeArray;
   //^ this.SignatureMemoryReader = signatureMemoryReader;
   //^ base;
   byte firstByte = this.SignatureMemoryReader.ReadByte();
   if (!SignatureHeader.IsGenericInstanceSignature(firstByte)) {
     //  MDError
   }
   int typeArgCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   ITypeReference/*?*/[] typeRefArr = new ITypeReference/*?*/[typeArgCount];
   for (int i = 0; i < typeArgCount; ++i) {
     typeRefArr[i] = this.GetTypeReference();
   }
   this.GenericTypeArguments = new EnumerableArrayWrapper<ITypeReference/*?*/, ITypeReference>(typeRefArr, Dummy.TypeReference);
 }
Пример #9
0
 //^ [NotDelayed]
 internal MethodRefSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   MethodReference moduleMethodRef,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleMethodRef) {
   //^ this.ReturnCustomModifiers = TypeCache.EmptyCustomModifierArray;
   this.RequiredParameters = TypeCache.EmptyParameterInfoArray;
   this.VarArgParameters = TypeCache.EmptyParameterInfoArray;
   //^ base;
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   //  TODO: Check minimum required size of the signature...
   byte firstByte = this.SignatureMemoryReader.ReadByte();
   if (SignatureHeader.IsGeneric(firstByte)) {
     this.GenericParamCount = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   byte retByte = this.SignatureMemoryReader.PeekByte(0);
   if (retByte == ElementType.Void) {
     this.ReturnTypeReference = peFileToObjectModel.SystemVoid;
     this.SignatureMemoryReader.SkipBytes(1);
   } else if (retByte == ElementType.TypedReference) {
     this.ReturnTypeReference = peFileToObjectModel.SystemTypedReference;
     this.SignatureMemoryReader.SkipBytes(1);
   } else {
     if (retByte == ElementType.ByReference) {
       this.IsReturnByReference = true;
       this.SignatureMemoryReader.SkipBytes(1);
     }
     this.ReturnTypeReference = this.GetTypeReference();
   }
   if (paramCount > 0) {
     IModuleParameterTypeInformation[] reqModuleParamArr = this.GetModuleParameterTypeInformations(moduleMethodRef, paramCount);
     if (reqModuleParamArr.Length > 0)
       this.RequiredParameters = new EnumerableArrayWrapper<IModuleParameterTypeInformation, IParameterTypeInformation>(reqModuleParamArr, Dummy.ParameterTypeInformation);
     IModuleParameterTypeInformation[] varArgModuleParamArr = this.GetModuleParameterTypeInformations(moduleMethodRef, paramCount - reqModuleParamArr.Length);
     if (varArgModuleParamArr.Length > 0)
       this.VarArgParameters = new EnumerableArrayWrapper<IModuleParameterTypeInformation, IParameterTypeInformation>(varArgModuleParamArr, Dummy.ParameterTypeInformation);
   }
 }
Пример #10
0
 //^ [NotDelayed]
 internal PropertySignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   PropertyDefinition moduleProperty,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleProperty) {
   //^ this.ReturnCustomModifiers = TypeCache.EmptyCustomModifierArray;
   this.Parameters = TypeCache.EmptyParameterArray;
   //^ base;
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   //  TODO: Check minimum required size of the signature...
   this.FirstByte = this.SignatureMemoryReader.ReadByte();
   if (!SignatureHeader.IsPropertySignature(this.FirstByte)) {
     //  Error...
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   if (this.SignatureMemoryReader.PeekByte(0) == ElementType.ByReference) {
     this.ReturnValueIsByReference = true;
     this.SignatureMemoryReader.SkipBytes(1);
   }
   this.ReturnTypeReference = this.GetTypeReference();
   if (paramCount > 0) {
     IModuleParameter[] moduleParamArr = this.GetModuleParameters(false, moduleProperty, paramCount);
     if (moduleParamArr.Length > 0)
       this.Parameters = new EnumerableArrayWrapper<IModuleParameter, IParameterDefinition>(moduleParamArr, Dummy.ParameterDefinition);
   }
 }
Пример #11
0
 //^ [NotDelayed]
 internal SecurityAttributeDecoder20(
   PEFileToObjectModel peFileToObjectModel,
   MemoryReader signatureMemoryReader,
   SecurityAttribute securityAttribute
 )
   : base(peFileToObjectModel, signatureMemoryReader) {
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   this.SecurityAttributes = TypeCache.EmptySecurityAttributes;
   //^ base;
   byte prolog = this.SignatureMemoryReader.ReadByte();
   if (prolog != SerializationType.SecurityAttribute20Start) {
     return;
   }
   int numberOfAttributes = this.SignatureMemoryReader.ReadCompressedUInt32();
   SecurityCustomAttribute[] securityCustomAttributes = new SecurityCustomAttribute[numberOfAttributes];
   for (int i = 0; i < numberOfAttributes; ++i) {
     SecurityCustomAttribute/*?*/ secAttr = this.ReadSecurityAttribute(securityAttribute);
     if (secAttr == null) {
       //  MDError...
       return;
     }
     securityCustomAttributes[i] = secAttr;
   }
   //^ NonNullType.AssertInitialized(securityCustomAttributes);
   this.SecurityAttributes = new EnumerableArrayWrapper<SecurityCustomAttribute, ICustomAttribute>(securityCustomAttributes, Dummy.CustomAttribute);
 }
Пример #12
0
 //^ [NotDelayed]
 internal CustomAttributeDecoder(
   PEFileToObjectModel peFileToObjectModel,
   MemoryReader signatureMemoryReader,
   uint customAttributeRowId,
   IModuleMethodReference attributeConstructor
 )
   : base(peFileToObjectModel, signatureMemoryReader) {
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   this.CustomAttribute = Dummy.CustomAttribute;
   //^ base;
   ushort prolog = this.SignatureMemoryReader.ReadUInt16();
   if (prolog != SerializationType.CustomAttributeStart) {
     return;
   }
   List<ExpressionBase> exprList = new List<ExpressionBase>();
   IModuleParameterTypeInformation[] modParams = attributeConstructor.RequiredModuleParameterInfos.RawArray;
   int len = modParams.Length;
   for (int i = 0; i < len; ++i) {
     IModuleTypeReference/*?*/ moduleTypeRef = modParams[i].ModuleTypeReference;
     if (moduleTypeRef == null) {
       //  Error...
       return;
     }
     ExpressionBase/*?*/ argument = this.ReadSerializedValue(moduleTypeRef);
     if (argument == null) {
       //  Error...
       this.decodeFailed = true;
       return;
     }
     exprList.Add(argument);
   }
   ushort numOfNamedArgs = this.SignatureMemoryReader.ReadUInt16();
   FieldOrPropertyNamedArgumentExpression[]/*?*/ namedArgumentArray = null;
   if (numOfNamedArgs > 0) {
     namedArgumentArray = new FieldOrPropertyNamedArgumentExpression[numOfNamedArgs];
     for (ushort i = 0; i < numOfNamedArgs; ++i) {
       bool isField = this.SignatureMemoryReader.ReadByte() == SerializationType.Field;
       IModuleTypeReference/*?*/ memberType = this.GetFieldOrPropType();
       if (memberType == null) {
         //  Error...
         return;
       }
       string/*?*/ memberStr = this.GetSerializedString();
       if (memberStr == null)
         return;
       IName memberName = this.PEFileToObjectModel.NameTable.GetNameFor(memberStr);
       ExpressionBase/*?*/ value = this.ReadSerializedValue(memberType);
       if (value == null) {
         //  Error...
         return;
       }
       IModuleTypeReference/*?*/ moduleTypeRef = attributeConstructor.OwningTypeReference;
       if (moduleTypeRef == null) {
         //  Error...
         return;
       }
       FieldOrPropertyNamedArgumentExpression namedArg = new FieldOrPropertyNamedArgumentExpression(memberName, moduleTypeRef, isField, memberType, value);
       namedArgumentArray[i] = namedArg;
     }
   }
   EnumerableArrayWrapper<ExpressionBase, IMetadataExpression> arguments = TypeCache.EmptyExpressionList;
   if (exprList.Count > 0)
     arguments = new EnumerableArrayWrapper<ExpressionBase, IMetadataExpression>(exprList.ToArray(), Dummy.Expression);
   EnumerableArrayWrapper<FieldOrPropertyNamedArgumentExpression, IMetadataNamedArgument> namedArguments = TypeCache.EmptyNamedArgumentList;
   if (namedArgumentArray != null)
     namedArguments = new EnumerableArrayWrapper<FieldOrPropertyNamedArgumentExpression, IMetadataNamedArgument>(namedArgumentArray, Dummy.NamedArgument);
   this.CustomAttribute = new CustomAttribute(this.PEFileToObjectModel, customAttributeRowId, attributeConstructor, arguments, namedArguments);
 }
Пример #13
0
 /// <summary>
 /// This method loads a module from a file containing only the metadata section of a PE file. (No headers and no IL.)
 /// </summary>
 /// <param name="binaryDocument">The binary document that needes to be opened as an module.</param>
 /// <returns>Module that is loaded or Dummy.Module in case module could not be loaded.</returns>
 public IModule OpenSnapshot(IBinaryDocument binaryDocument) {
   ModuleIdentity moduleIdentity;
   lock (GlobalLock.LockingObject) {
     IBinaryDocumentMemoryBlock/*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument);
     if (binaryDocumentMemoryBlock == null) {
       //  Error...
       return Dummy.Module;
     }
     PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock, snapshot: true);
     if (peFileReader.ReaderState < ReaderState.Metadata) {
       //  Error...
       return Dummy.Module;
     }
     if (peFileReader.IsAssembly) {
       AssemblyIdentity assemblyIdentity = this.GetAssemblyIdentifier(peFileReader);
       moduleIdentity = assemblyIdentity;
       Assembly/*?*/ lookupAssembly = this.LookupAssembly(null, assemblyIdentity);
       if (lookupAssembly != null) {
         return lookupAssembly;
       }
     } else {
       moduleIdentity = this.GetModuleIdentifier(peFileReader);
       Module/*?*/ lookupModule = this.LookupModule(null, moduleIdentity);
       if (lookupModule != null) {
         return lookupModule;
       }
     }
     if (peFileReader.ReaderState < ReaderState.Metadata) {
       //  Error...
       return Dummy.Module;
     }
     try {
       PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, moduleIdentity, null, this.metadataReaderHost.PointerSize);
       this.LoadedModule(peFileToObjectModel.Module);
       CciEventSource.Log.ModuleOpened(peFileToObjectModel.Module, moduleIdentity, binaryDocument.Length);
       return peFileToObjectModel.Module;
     } catch (MetadataReaderException) {
       //  Error...
     }
   }
   return Dummy.Module;
 }
Пример #14
0
        //  Caller should lock peFileToObjectModel
        internal CoreTypes(PEFileToObjectModel peFileToObjectModel)
        {
            INameTable nameTable = peFileToObjectModel.NameTable;
              PEFileReader peFileReader = peFileToObjectModel.PEFileReader;
              PeReader peReader = peFileToObjectModel.ModuleReader;
              Module module = peFileToObjectModel.Module;
              AssemblyIdentity/*?*/ assemblyIdentity = module.ModuleIdentity as AssemblyIdentity;

              int systemName = nameTable.System.UniqueKey;
              int voidName = nameTable.Void.UniqueKey;
              int booleanName = nameTable.Boolean.UniqueKey;
              int charName = nameTable.Char.UniqueKey;
              int byteName = nameTable.Byte.UniqueKey;
              int sByteName = nameTable.SByte.UniqueKey;
              int int16Name = nameTable.Int16.UniqueKey;
              int uint16Name = nameTable.UInt16.UniqueKey;
              int int32Name = nameTable.Int32.UniqueKey;
              int uint32Name = nameTable.UInt32.UniqueKey;
              int int64Name = nameTable.Int64.UniqueKey;
              int uint64Name = nameTable.UInt64.UniqueKey;
              int stringName = nameTable.String.UniqueKey;
              int intPtrName = nameTable.IntPtr.UniqueKey;
              int uintPtrName = nameTable.UIntPtr.UniqueKey;
              int objectName = nameTable.Object.UniqueKey;
              int singleName = nameTable.Single.UniqueKey;
              int doubleName = nameTable.Double.UniqueKey;
              int decimalName = nameTable.Decimal.UniqueKey;
              int typedReference = nameTable.TypedReference.UniqueKey;
              int enumName = nameTable.Enum.UniqueKey;
              int valueTypeName = nameTable.ValueType.UniqueKey;
              int multicastDelegateName = nameTable.MulticastDelegate.UniqueKey;
              int typeName = nameTable.Type.UniqueKey;
              int arrayName = nameTable.Array.UniqueKey;
              int paramArrayAttributeName = peReader.ParamArrayAttribute.UniqueKey;
              if (assemblyIdentity != null && assemblyIdentity.Equals(peReader.metadataReaderHost.CoreAssemblySymbolicIdentity)) {
            peReader.RegisterCoreAssembly(module as Assembly);
            uint numberOfTypeDefs = peFileReader.TypeDefTable.NumberOfRows;
            for (uint i = 1; i <= numberOfTypeDefs; ++i) {
              TypeDefRow typeDefRow = peFileReader.TypeDefTable[i];
              if (!typeDefRow.IsNested) {
            int namespaceName = peFileToObjectModel.GetNameFromOffset(typeDefRow.Namespace).UniqueKey;
            if (namespaceName == systemName) {
              int typeDefName = peFileToObjectModel.GetNameFromOffset(typeDefRow.Name).UniqueKey;
              if (typeDefName == voidName)
                this.SystemVoid = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Void);
              else if (typeDefName == booleanName)
                this.SystemBoolean = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Boolean);
              else if (typeDefName == charName)
                this.SystemChar = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Char);
              else if (typeDefName == byteName)
                this.SystemByte = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Byte);
              else if (typeDefName == sByteName)
                this.SystemSByte = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.SByte);
              else if (typeDefName == int16Name)
                this.SystemInt16 = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Int16);
              else if (typeDefName == uint16Name)
                this.SystemUInt16 = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt16);
              else if (typeDefName == int32Name)
                this.SystemInt32 = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Int32);
              else if (typeDefName == uint32Name)
                this.SystemUInt32 = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt32);
              else if (typeDefName == int64Name)
                this.SystemInt64 = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Int64);
              else if (typeDefName == uint64Name)
                this.SystemUInt64 = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt64);
              else if (typeDefName == stringName)
                this.SystemString = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.String);
              else if (typeDefName == intPtrName)
                this.SystemIntPtr = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.IntPtr);
              else if (typeDefName == uintPtrName)
                this.SystemUIntPtr = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UIntPtr);
              else if (typeDefName == objectName)
                this.SystemObject = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Object);
              else if (typeDefName == singleName)
                this.SystemSingle = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Single);
              else if (typeDefName == doubleName)
                this.SystemDouble = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Double);
              else if (typeDefName == decimalName)
                this.SystemDecimal = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == typedReference)
                this.SystemTypedReference = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.TypedReference);
              else if (typeDefName == enumName)
                this.SystemEnum = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == valueTypeName)
                this.SystemValueType = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == multicastDelegateName)
                this.SystemMulticastDelegate = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == typeName)
                this.SystemType = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == arrayName)
                this.SystemArray = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == paramArrayAttributeName)
                this.SystemParamArrayAttribute = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            }
              }
            }
              } else {
            uint numberOfTypeRefs = peFileReader.TypeRefTable.NumberOfRows;
            AssemblyReference/*?*/ coreAssemblyRef = peFileToObjectModel.FindAssemblyReference(peReader.metadataReaderHost.CoreAssemblySymbolicIdentity);
            if (coreAssemblyRef == null) {
              //  Error...
              coreAssemblyRef = new AssemblyReference(peFileToObjectModel, 0, peReader.metadataReaderHost.CoreAssemblySymbolicIdentity, AssemblyFlags.Retargetable);
            }
            uint coreAssemblyRefToken = coreAssemblyRef.TokenValue;
            for (uint i = 1; i <= numberOfTypeRefs; ++i) {
              TypeRefRow typeRefRow = peFileReader.TypeRefTable[i];
              if (typeRefRow.ResolutionScope != coreAssemblyRefToken)
            continue;
              int namespaceName = peFileToObjectModel.GetNameFromOffset(typeRefRow.Namespace).UniqueKey;
              if (namespaceName == systemName) {
            int typeDefName = peFileToObjectModel.GetNameFromOffset(typeRefRow.Name).UniqueKey;
            if (typeDefName == voidName)
              this.SystemVoid = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Void);
            else if (typeDefName == booleanName)
              this.SystemBoolean = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Boolean);
            else if (typeDefName == charName)
              this.SystemChar = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Char);
            else if (typeDefName == byteName)
              this.SystemByte = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Byte);
            else if (typeDefName == sByteName)
              this.SystemSByte = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.SByte);
            else if (typeDefName == int16Name)
              this.SystemInt16 = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Int16);
            else if (typeDefName == uint16Name)
              this.SystemUInt16 = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt16);
            else if (typeDefName == int32Name)
              this.SystemInt32 = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Int32);
            else if (typeDefName == uint32Name)
              this.SystemUInt32 = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt32);
            else if (typeDefName == int64Name)
              this.SystemInt64 = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Int64);
            else if (typeDefName == uint64Name)
              this.SystemUInt64 = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt64);
            else if (typeDefName == stringName)
              this.SystemString = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.String);
            else if (typeDefName == intPtrName)
              this.SystemIntPtr = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.IntPtr);
            else if (typeDefName == uintPtrName)
              this.SystemUIntPtr = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UIntPtr);
            else if (typeDefName == objectName)
              this.SystemObject = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Object);
            else if (typeDefName == singleName)
              this.SystemSingle = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Single);
            else if (typeDefName == doubleName)
              this.SystemDouble = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Double);
            else if (typeDefName == decimalName)
              this.SystemDecimal = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == typedReference)
              this.SystemTypedReference = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.TypedReference);
            else if (typeDefName == enumName)
              this.SystemEnum = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == valueTypeName)
              this.SystemValueType = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == multicastDelegateName)
              this.SystemMulticastDelegate = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == typeName)
              this.SystemType = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == arrayName)
              this.SystemArray = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == paramArrayAttributeName)
              this.SystemParamArrayAttribute = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              }
            }
            NamespaceReference systemNSR = peFileToObjectModel.GetNamespaceReferenceForString(coreAssemblyRef, nameTable.System);
            if (this.SystemVoid == null)
              this.SystemVoid = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Void, MetadataReaderSignatureTypeCode.Void);
            if (this.SystemBoolean == null)
              this.SystemBoolean = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Boolean, MetadataReaderSignatureTypeCode.Boolean);
            if (this.SystemChar == null)
              this.SystemChar = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Char, MetadataReaderSignatureTypeCode.Char);
            if (this.SystemByte == null)
              this.SystemByte = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Byte, MetadataReaderSignatureTypeCode.Byte);
            if (this.SystemSByte == null)
              this.SystemSByte = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.SByte, MetadataReaderSignatureTypeCode.SByte);
            if (this.SystemInt16 == null)
              this.SystemInt16 = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Int16, MetadataReaderSignatureTypeCode.Int16);
            if (this.SystemUInt16 == null)
              this.SystemUInt16 = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.UInt16, MetadataReaderSignatureTypeCode.UInt16);
            if (this.SystemInt32 == null)
              this.SystemInt32 = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Int32, MetadataReaderSignatureTypeCode.Int32);
            if (this.SystemUInt32 == null)
              this.SystemUInt32 = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.UInt32, MetadataReaderSignatureTypeCode.UInt32);
            if (this.SystemInt64 == null)
              this.SystemInt64 = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Int64, MetadataReaderSignatureTypeCode.Int64);
            if (this.SystemUInt64 == null)
              this.SystemUInt64 = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.UInt64, MetadataReaderSignatureTypeCode.UInt64);
            if (this.SystemString == null)
              this.SystemString = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.String, MetadataReaderSignatureTypeCode.String);
            if (this.SystemIntPtr == null)
              this.SystemIntPtr = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.IntPtr, MetadataReaderSignatureTypeCode.IntPtr);
            if (this.SystemUIntPtr == null)
              this.SystemUIntPtr = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.UIntPtr, MetadataReaderSignatureTypeCode.UIntPtr);
            if (this.SystemObject == null)
              this.SystemObject = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Object, MetadataReaderSignatureTypeCode.Object);
            if (this.SystemSingle == null)
              this.SystemSingle = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Single, MetadataReaderSignatureTypeCode.Single);
            if (this.SystemDouble == null)
              this.SystemDouble = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Double, MetadataReaderSignatureTypeCode.Double);
            if (this.SystemDecimal == null)
              this.SystemDecimal = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Decimal, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemTypedReference == null)
              this.SystemTypedReference = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.TypedReference, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemEnum == null)
              this.SystemEnum = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Enum, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemValueType == null)
              this.SystemValueType = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.ValueType, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemMulticastDelegate == null)
              this.SystemMulticastDelegate = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.MulticastDelegate, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemType == null)
              this.SystemType = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Type, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemArray == null)
              this.SystemArray = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, nameTable.Array, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            if (this.SystemParamArrayAttribute == null)
              this.SystemParamArrayAttribute = peFileToObjectModel.typeCache.CreateCoreTypeReference(coreAssemblyRef, systemNSR, peReader.ParamArrayAttribute, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              }
        }
Пример #15
0
 //^ [NotDelayed]
 internal TypeSpecSignatureConverter(
   PEFileToObjectModel peFileToObjectModel,
   TypeSpecReference moduleTypeSpecReference,
   MemoryReader signatureMemoryReader
 )
   : base(peFileToObjectModel, signatureMemoryReader, moduleTypeSpecReference.TypeSpecOwner) {
   //^ base;
   //^ this.SignatureMemoryReader = signatureMemoryReader; //TODO: Spec# bug. This assignment should not be necessary.
   byte firstByte = this.SignatureMemoryReader.ReadByte();
   switch (firstByte) {
     case ElementType.GenericTypeInstance:
       this.TypeReference = this.GetModuleGenericTypeInstanceReference(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.ByReference:
       this.TypeReference = this.GetModuleManagedPointerType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.Pointer:
       this.TypeReference = this.GetModulePointerType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.Array:
       this.TypeReference = this.GetModuleMatrixType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.SzArray:
       this.TypeReference = this.GetModuleVectorType(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.FunctionPointer:
       this.TypeReference = this.GetModuleFuntionPointer(TokenTypeIds.TypeSpec | moduleTypeSpecReference.TypeSpecRowId);
       break;
     case ElementType.Class:
     case ElementType.ValueType: {
         uint typeEncoded = (uint)this.SignatureMemoryReader.ReadCompressedUInt32();
         uint typeToken = TypeDefOrRefTag.ConvertToToken(typeEncoded);
         this.TypeReference = this.PEFileToObjectModel.GetTypeReferenceForToken(this.MetadataOwnerObject, typeToken, firstByte == ElementType.ValueType);
       }
       break;
     case ElementType.GenericTypeParameter: {
         ushort ordinal = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
         if (this.ModuleGenericType == null) {
           //  TODO: Error
         } else {
           this.TypeReference = this.ModuleGenericType.GetGenericTypeParameterFromOrdinal(ordinal);
         }
         break;
       }
     case ElementType.GenericMethodParameter: {
         ushort ordinal = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
         if (this.ModuleGenericMethod == null) {
           //  TODO: Error
         } else {
           this.TypeReference = this.ModuleGenericMethod.GetGenericMethodParameterFromOrdinal(ordinal);
         }
         break;
       }
     case ElementType.RequiredModifier:
     case ElementType.OptionalModifier: {
         bool dummyPinned;
         this.SignatureMemoryReader.SkipBytes(-1);
         var customModifiers = this.GetCustomModifiers(out dummyPinned);
         ITypeReference/*?*/ typeReference = this.GetTypeReference();
         if (typeReference == null) {
           //  TODO: Error
         } else {
           this.TypeReference = new ModifiedTypeReference(this.PEFileToObjectModel, typeReference, customModifiers);
         }
         break;
       }
     case ElementType.Boolean:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemBoolean;
       break;
     case ElementType.Char:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemChar;
       break;
     case ElementType.Double:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemFloat64;
       break;
     case ElementType.Int16:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemInt16;
       break;
     case ElementType.Int32:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemInt32;
       break;
     case ElementType.Int64:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemInt64;
       break;
     case ElementType.Int8:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemInt8;
       break;
     case ElementType.IntPtr:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemIntPtr;
       break;
     case ElementType.Object:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemObject;
       break;
     case ElementType.Single:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemFloat32;
       break;
     case ElementType.String:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemString;
       break;
     case ElementType.UInt16:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemUInt16;
       break;
     case ElementType.UInt32:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemUInt32;
       break;
     case ElementType.UInt64:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemUInt64;
       break;
     case ElementType.UInt8:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemUInt8;
       break;
     case ElementType.UIntPtr:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemUIntPtr;
       break;
     case ElementType.Void:
       this.TypeReference = this.PEFileToObjectModel.PlatformType.SystemVoid;
       break;
     default:
       //  Error...
       break;
   }
 }
Пример #16
0
    //  Caller should lock peFileToObjectModel
    internal CoreTypes(PEFileToObjectModel peFileToObjectModel) {
      INameTable nameTable = peFileToObjectModel.NameTable;
      PEFileReader peFileReader = peFileToObjectModel.PEFileReader;
      PeReader peReader = peFileToObjectModel.ModuleReader;
      Module module = peFileToObjectModel.Module;
      AssemblyIdentity/*?*/ assemblyIdentity = module.ModuleIdentity as AssemblyIdentity;

      //This does more than just initialize the five types exposed above, since it is also
      //necessary to initialize any typedefs and typerefs to types with short forms
      //in such a way that they have the correct type codes.

      int systemName = nameTable.System.UniqueKey;
      int voidName = nameTable.Void.UniqueKey;
      int booleanName = nameTable.Boolean.UniqueKey;
      int charName = nameTable.Char.UniqueKey;
      int byteName = nameTable.Byte.UniqueKey;
      int sByteName = nameTable.SByte.UniqueKey;
      int int16Name = nameTable.Int16.UniqueKey;
      int uint16Name = nameTable.UInt16.UniqueKey;
      int int32Name = nameTable.Int32.UniqueKey;
      int uint32Name = nameTable.UInt32.UniqueKey;
      int int64Name = nameTable.Int64.UniqueKey;
      int uint64Name = nameTable.UInt64.UniqueKey;
      int stringName = nameTable.String.UniqueKey;
      int intPtrName = nameTable.IntPtr.UniqueKey;
      int uintPtrName = nameTable.UIntPtr.UniqueKey;
      int objectName = nameTable.Object.UniqueKey;
      int singleName = nameTable.Single.UniqueKey;
      int doubleName = nameTable.Double.UniqueKey;
      int decimalName = nameTable.Decimal.UniqueKey;
      int typedReference = nameTable.TypedReference.UniqueKey;
      int enumName = nameTable.Enum.UniqueKey;
      int valueTypeName = nameTable.ValueType.UniqueKey;
      int multicastDelegateName = nameTable.MulticastDelegate.UniqueKey;
      int typeName = nameTable.Type.UniqueKey;
      int arrayName = nameTable.Array.UniqueKey;
      int paramArrayAttributeName = peReader.ParamArrayAttribute.UniqueKey;
      if (assemblyIdentity != null && assemblyIdentity.Equals(peReader.metadataReaderHost.CoreAssemblySymbolicIdentity)) {
        peReader.RegisterCoreAssembly(module as Assembly);
        uint numberOfTypeDefs = peFileReader.TypeDefTable.NumberOfRows;
        for (uint i = 1; i <= numberOfTypeDefs; ++i) {
          TypeDefRow typeDefRow = peFileReader.TypeDefTable[i];
          if (!typeDefRow.IsNested) {
            int namespaceName = peFileToObjectModel.GetNameFromOffset(typeDefRow.Namespace).UniqueKey;
            if (namespaceName == systemName) {
              int typeDefName = peFileToObjectModel.GetNameFromOffset(typeDefRow.Name).UniqueKey;
              if (typeDefName == voidName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Void);
              else if (typeDefName == booleanName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Boolean);
              else if (typeDefName == charName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Char);
              else if (typeDefName == byteName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Byte);
              else if (typeDefName == sByteName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.SByte);
              else if (typeDefName == int16Name)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Int16);
              else if (typeDefName == uint16Name)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt16);
              else if (typeDefName == int32Name)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Int32);
              else if (typeDefName == uint32Name)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt32);
              else if (typeDefName == int64Name)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Int64);
              else if (typeDefName == uint64Name)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt64);
              else if (typeDefName == stringName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.String);
              else if (typeDefName == intPtrName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.IntPtr);
              else if (typeDefName == uintPtrName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.UIntPtr);
              else if (typeDefName == objectName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Object);
              else if (typeDefName == singleName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Single);
              else if (typeDefName == doubleName)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.Double);
              else if (typeDefName == typedReference)
                peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.TypedReference);
              else if (typeDefName == enumName)
                this.SystemEnum = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == valueTypeName)
                this.SystemValueType = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == multicastDelegateName)
                this.SystemMulticastDelegate = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == typeName)
                this.SystemType = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
              else if (typeDefName == paramArrayAttributeName)
                this.SystemParamArrayAttribute = peFileToObjectModel.GetPredefinedTypeDefinitionAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            }
          }
        }
      } else {
        uint numberOfTypeRefs = peFileReader.TypeRefTable.NumberOfRows;
        AssemblyReference/*?*/ coreAssemblyRef = peFileToObjectModel.FindAssemblyReference(peFileToObjectModel.CoreAssemblySymbolicIdentity);
        if (coreAssemblyRef == null) {
          //  Error...
          coreAssemblyRef = new AssemblyReference(peFileToObjectModel, 1, peFileToObjectModel.CoreAssemblySymbolicIdentity, AssemblyFlags.Retargetable);
        }
        uint coreAssemblyRefToken = coreAssemblyRef.TokenValue;
        for (uint i = 1; i <= numberOfTypeRefs; ++i) {
          TypeRefRow typeRefRow = peFileReader.TypeRefTable[i];
          if (typeRefRow.ResolutionScope != coreAssemblyRefToken)
            continue;
          int namespaceName = peFileToObjectModel.GetNameFromOffset(typeRefRow.Namespace).UniqueKey;
          if (namespaceName == systemName) {
            int typeDefName = peFileToObjectModel.GetNameFromOffset(typeRefRow.Name).UniqueKey;
            if (typeDefName == voidName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Void);
            else if (typeDefName == booleanName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Boolean);
            else if (typeDefName == charName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Char);
            else if (typeDefName == byteName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Byte);
            else if (typeDefName == sByteName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.SByte);
            else if (typeDefName == int16Name)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Int16);
            else if (typeDefName == uint16Name)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt16);
            else if (typeDefName == int32Name)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Int32);
            else if (typeDefName == uint32Name)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt32);
            else if (typeDefName == int64Name)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Int64);
            else if (typeDefName == uint64Name)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UInt64);
            else if (typeDefName == stringName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.String);
            else if (typeDefName == intPtrName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.IntPtr);
            else if (typeDefName == uintPtrName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.UIntPtr);
            else if (typeDefName == objectName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Object);
            else if (typeDefName == singleName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Single);
            else if (typeDefName == doubleName)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.Double);
            else if (typeDefName == typedReference)
              peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.TypedReference);
            else if (typeDefName == enumName)
              this.SystemEnum = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == valueTypeName)
              this.SystemValueType = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == multicastDelegateName)
              this.SystemMulticastDelegate = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == typeName)
              this.SystemType = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
            else if (typeDefName == paramArrayAttributeName)
              this.SystemParamArrayAttribute = peFileToObjectModel.GetPredefinedTypeRefReferenceAtRowWorker(i, MetadataReaderSignatureTypeCode.NotModulePrimitive);
          }
        }
        NamespaceReference systemNSR = peFileToObjectModel.GetNamespaceReferenceForString(coreAssemblyRef, nameTable.System);
        if (this.SystemEnum == null || (peFileToObjectModel.SystemEnumAssembly != null && coreAssemblyRef != peFileToObjectModel.SystemEnumAssembly))
          this.SystemEnum = peFileToObjectModel.typeCache.CreateCoreTypeReference(peFileToObjectModel.SystemEnumAssembly??coreAssemblyRef, systemNSR, nameTable.Enum, MetadataReaderSignatureTypeCode.NotModulePrimitive);
        if (this.SystemValueType == null || (peFileToObjectModel.SystemValueTypeAssembly != null && coreAssemblyRef != peFileToObjectModel.SystemValueTypeAssembly))
          this.SystemValueType = peFileToObjectModel.typeCache.CreateCoreTypeReference(peFileToObjectModel.SystemValueTypeAssembly??coreAssemblyRef, systemNSR, nameTable.ValueType, MetadataReaderSignatureTypeCode.NotModulePrimitive);
        if (this.SystemMulticastDelegate == null || (peFileToObjectModel.SystemMulticastDelegateAssembly != null && coreAssemblyRef != peFileToObjectModel.SystemMulticastDelegateAssembly))
          this.SystemMulticastDelegate = peFileToObjectModel.typeCache.CreateCoreTypeReference(peFileToObjectModel.SystemMulticastDelegateAssembly??coreAssemblyRef, systemNSR, nameTable.MulticastDelegate, MetadataReaderSignatureTypeCode.NotModulePrimitive);
        if (this.SystemType == null || (peFileToObjectModel.SystemTypeAssembly != null && coreAssemblyRef != peFileToObjectModel.SystemTypeAssembly))
          this.SystemType = peFileToObjectModel.typeCache.CreateCoreTypeReference(peFileToObjectModel.SystemTypeAssembly??coreAssemblyRef, systemNSR, nameTable.Type, MetadataReaderSignatureTypeCode.NotModulePrimitive);
        if (this.SystemParamArrayAttribute == null || (peFileToObjectModel.SystemParamArrayAttributeAssembly != null && coreAssemblyRef != peFileToObjectModel.SystemParamArrayAttributeAssembly))
          this.SystemParamArrayAttribute = peFileToObjectModel.typeCache.CreateCoreTypeReference(peFileToObjectModel.SystemParamArrayAttributeAssembly??coreAssemblyRef, systemNSR, peReader.ParamArrayAttribute, MetadataReaderSignatureTypeCode.NotModulePrimitive);
      }
    }
Пример #17
0
 internal MethodRefSignatureConverter(PEFileToObjectModel peFileToObjectModel, MethodReference moduleMethodRef, MemoryReader signatureMemoryReader)
   : base(peFileToObjectModel, signatureMemoryReader, moduleMethodRef) {
   //  TODO: Check minimum required size of the signature...
   byte firstByte = this.SignatureMemoryReader.ReadByte();
   if (SignatureHeader.IsGeneric(firstByte)) {
     this.GenericParamCount = (ushort)this.SignatureMemoryReader.ReadCompressedUInt32();
   }
   int paramCount = this.SignatureMemoryReader.ReadCompressedUInt32();
   bool dummyPinned;
   this.ReturnCustomModifiers = this.GetCustomModifiers(out dummyPinned);
   byte retByte = this.SignatureMemoryReader.PeekByte(0);
   if (retByte == ElementType.Void) {
     this.ReturnTypeReference = peFileToObjectModel.PlatformType.SystemVoid;
     this.SignatureMemoryReader.SkipBytes(1);
   } else if (retByte == ElementType.TypedReference) {
     this.ReturnTypeReference = peFileToObjectModel.PlatformType.SystemTypedReference;
     this.SignatureMemoryReader.SkipBytes(1);
   } else {
     if (retByte == ElementType.ByReference) {
       this.IsReturnByReference = true;
       this.SignatureMemoryReader.SkipBytes(1);
     }
     this.ReturnTypeReference = this.GetTypeReference();
   }
   if (paramCount > 0) {
     this.RequiredParameters = this.GetModuleParameterTypeInformations(moduleMethodRef, paramCount);
     if (this.RequiredParameters.Length < paramCount)
       this.VarArgParameters = this.GetModuleParameterTypeInformations(moduleMethodRef, paramCount - this.RequiredParameters.Length);
   }
 }
Пример #18
0
 /// <summary>
 /// This method is called when an assembly is loaded. This makes sure that all the member modules of the assembly are loaded.
 /// </summary>
 /// <param name="binaryDocument"></param>
 /// <param name="assembly"></param>
 void OpenMemberModules(IBinaryDocument binaryDocument, Assembly assembly) {
   List<Module> memberModuleList = new List<Module>();
   AssemblyIdentity assemblyIdentity = assembly.AssemblyIdentity;
   foreach (IFileReference fileRef in assembly.PEFileToObjectModel.GetFiles()) {
     if (!fileRef.HasMetadata)
       continue;
     IBinaryDocumentMemoryBlock/*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument, fileRef.FileName.Value);
     if (binaryDocumentMemoryBlock == null) {
       //  Error...
       continue;
     }
     try {
       PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock);
       if (peFileReader.ReaderState < ReaderState.Metadata) {
         //  Error...
         continue;
       }
       if (peFileReader.IsAssembly) {
         //  Error...
         continue;
       }
       ModuleIdentity moduleIdentity = this.GetModuleIdentifier(peFileReader, assemblyIdentity);
       PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, moduleIdentity, assembly, this.metadataReaderHost.PointerSize);
       memberModuleList.Add(peFileToObjectModel.Module);
     } catch (MetadataReaderException) {
       continue;
     }
   }
   if (memberModuleList.Count == 0)
     return;
   assembly.SetMemberModules(new EnumerableArrayWrapper<Module, IModule>(memberModuleList.ToArray(), Dummy.Module));
 }
Пример #19
0
 protected AttributeDecoder(
   PEFileToObjectModel peFileToObjectModel,
   MemoryReader signatureMemoryReader
 ) {
   this.PEFileToObjectModel = peFileToObjectModel;
   this.SignatureMemoryReader = signatureMemoryReader;
   this.morePermutationsArePossible = true;
 }
Пример #20
0
 /// <summary>
 /// Method to open the assembly in MetadataReader. This method loads the assembly and returns the object corresponding to the
 /// opened assembly. Also returns the AssemblyIdentifier corresponding to the assembly as the out parameter.
 /// Only assemblies that unify to themselves can be opened i.e. if the unification policy of the compilation host says that mscorlib 1.0 unifies to mscorlib 2.0
 /// then only mscorlib 2.0 can be loaded.
 /// </summary>
 /// <param name="binaryDocument">The binary document that needes to be opened as an assembly.</param>
 /// <param name="assemblyIdentity">Contains the assembly identifier of the binary document in case it is an assembly.</param>
 /// <returns>Assembly that is loaded or Dummy.Assembly in case assembly could not be loaded.</returns>
 public IAssembly OpenAssembly(
   IBinaryDocument binaryDocument,
   out AssemblyIdentity/*?*/ assemblyIdentity
 ) {
   assemblyIdentity = null;
   lock (GlobalLock.LockingObject) {
     IBinaryDocumentMemoryBlock/*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument);
     if (binaryDocumentMemoryBlock == null) {
       //  Error...
       return Dummy.Assembly;
     }
     PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock);
     if (peFileReader.ReaderState < ReaderState.Metadata) {
       //  Error...
       return Dummy.Assembly;
     }
     //^ assert peFileReader.ReaderState >= ReaderState.Metadata;
     if (!peFileReader.IsAssembly) {
       //  Error...
       return Dummy.Assembly;
     }
     assemblyIdentity = this.GetAssemblyIdentifier(peFileReader);
     Assembly/*?*/ lookupAssembly = this.LookupAssembly(null, assemblyIdentity);
     if (lookupAssembly != null) {
       return lookupAssembly;
     }
     try {
       PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, assemblyIdentity, null, this.metadataReaderHost.PointerSize);
       Assembly/*?*/ assembly = peFileToObjectModel.Module as Assembly;
       //^ assert assembly != null;
       this.LoadedModule(assembly);
       this.OpenMemberModules(binaryDocument, assembly);
       return assembly;
     } catch (MetadataReaderException) {
       return Dummy.Assembly;
     }
   }
 }
Пример #21
0
 internal SecurityAttributeDecoder20(PEFileToObjectModel peFileToObjectModel, MemoryReader signatureMemoryReader, SecurityAttribute securityAttribute)
   : base(peFileToObjectModel, signatureMemoryReader) {
   this.SecurityAttributes = Enumerable<ICustomAttribute>.Empty;
   byte prolog = this.SignatureMemoryReader.ReadByte();
   if (prolog != SerializationType.SecurityAttribute20Start) return;
   int numberOfAttributes = this.SignatureMemoryReader.ReadCompressedUInt32();
   var securityCustomAttributes = new ICustomAttribute[numberOfAttributes];
   for (int i = 0; i < numberOfAttributes; ++i) {
     var secAttr = this.ReadSecurityAttribute(securityAttribute);
     if (secAttr == null) {
       //  MDError...
       return;
     }
     securityCustomAttributes[i] = secAttr;
   }
   this.SecurityAttributes = IteratorHelper.GetReadonly(securityCustomAttributes);
 }
Пример #22
0
 // Fixes up names and flags for managed winmd classes if projection support is enabled in the host.
 // - CLR view classes and enums in managed winmds lose the '<CLR>' prefix in their name and become public.
 // - WinRT view classes and enums in managed winmds get a '<WinRT>' prefix in their name and become private.
 // This is identical to the behavior one sees when one uses ildasm's "/project" option to view the contents
 // of a managed winmd.
 void IWindowsRuntimeMetadataReaderHost.FixUpNameAndFlagsForManagedWinMDClassOrEnum(
   PEFileToObjectModel peFileToObjectModel,
   uint typeDefRowId,
   IUnit containingUnit,
   ref IName typeName,
   ref TypeDefFlags flags
 ) {
   IWindowsRuntimeMetadataReaderHost host = peFileToObjectModel.ModuleReader.metadataReaderHost as WindowsRuntimeMetadataReaderHost;
   if (this.projectToCLRTypes) {
     IName baseTypeNamespaceName = null;
     IName baseTypeName = null;
     uint baseTypeToken = peFileToObjectModel.PEFileReader.TypeDefTable.GetExtends(typeDefRowId);
     uint baseTypeRowId = baseTypeToken & TokenTypeIds.RIDMask;
     if (baseTypeRowId != 0) {
       uint tokenType = baseTypeToken & TokenTypeIds.TokenTypeMask;
       switch(tokenType) {
         case TokenTypeIds.TypeDef:
           TypeDefRow baseTypeDefRow = peFileToObjectModel.PEFileReader.TypeDefTable[baseTypeRowId];
           baseTypeNamespaceName = peFileToObjectModel.GetNameFromOffset(baseTypeDefRow.Namespace);
           baseTypeName = peFileToObjectModel.GetNameFromOffset(baseTypeDefRow.Name);
           break;
         case TokenTypeIds.TypeRef:
           TypeRefRow baseTypeRefRow = peFileToObjectModel.PEFileReader.TypeRefTable[baseTypeRowId];
           baseTypeNamespaceName = peFileToObjectModel.GetNameFromOffset(baseTypeRefRow.Namespace);
           baseTypeName = peFileToObjectModel.GetNameFromOffset(baseTypeRefRow.Name);
           break;
         case TokenTypeIds.TypeSpec:
           // We don't care about TypeSpecs because managed winmd types can never inherit generic types.
         default:
           break;
       }
       if (baseTypeName != null) {
         IAssemblyReference containingUnitMscorlibReference = peFileToObjectModel.GetMscorlibReference();
         this.FixUpNameAndFlagsForManagedWinMDClassOrEnum(containingUnit, containingUnitMscorlibReference, baseTypeNamespaceName, baseTypeName, ref typeName, ref flags);
       }
     }
   }
 }