private static INestedUnitNamespace /*?*/ GetNamespace(INameTable nameTable, INamespaceDefinition namespaceDefinition, string typeName, ref int offset) { Contract.Requires(nameTable != null); Contract.Requires(namespaceDefinition != null); Contract.Requires(typeName != null); Contract.Requires(offset >= 0); Contract.Ensures(offset >= 0); int len = typeName.Length; if (offset >= len) { return(null); } int dotPos = typeName.IndexOf('.', offset); if (dotPos < 0) { return(null); } Contract.Assume(dotPos >= offset); //if a dot has been found, it must be at offset or later IName neName = nameTable.GetNameFor(typeName.Substring(offset, dotPos - offset)); foreach (var member in namespaceDefinition.GetMembersNamed(neName, false)) { var nestedNamespace = member as INestedUnitNamespace; if (nestedNamespace == null) { continue; } offset = dotPos + 1; return(nestedNamespace); } return(null); }
private static INestedTypeDefinition /*?*/ GetNestedType(INameTable nameTable, ITypeDefinition typeDefinition, string typeName, ref int offset) { int len = typeName.Length; if (offset >= len) { return(null); } int dotPos = typeName.IndexOf('.', offset); if (dotPos < 0) { return(null); } IName tName = nameTable.GetNameFor(typeName.Substring(offset, dotPos < 0 ? len - offset : dotPos - offset)); foreach (var member in typeDefinition.GetMembersNamed(tName, false)) { var nestedType = member as INestedTypeDefinition; if (nestedType == null) { continue; } if (dotPos < 0) { return(nestedType); } offset = dotPos + 1; return(GetNestedType(nameTable, nestedType, typeName, ref offset)); } return(null); }
private static INestedUnitNamespace /*?*/ GetNamespace(INameTable nameTable, INamespaceDefinition namespaceDefinition, string typeName, ref int offset) { int len = typeName.Length; if (offset >= len) { return(null); } int dotPos = typeName.IndexOf('.', offset); if (dotPos < 0) { return(null); } IName neName = nameTable.GetNameFor(typeName.Substring(offset, dotPos - offset)); foreach (var member in namespaceDefinition.GetMembersNamed(neName, false)) { var nestedNamespace = member as INestedUnitNamespace; if (nestedNamespace == null) { continue; } offset = dotPos + 1; return(nestedNamespace); } return(null); }
private static INamespaceTypeDefinition /*?*/ GetNamespaceType(INameTable nameTable, INamespaceDefinition namespaceDefinition, string typeName, int genericParameterCount, ref int offset) { int len = typeName.Length; if (offset >= len) { return(null); } int dotPos = typeName.IndexOf('.', offset); if (dotPos < 0) { dotPos = len; } IName tName = nameTable.GetNameFor(typeName.Substring(offset, dotPos - offset)); foreach (var member in namespaceDefinition.GetMembersNamed(tName, false)) { var namespaceType = member as INamespaceTypeDefinition; if (namespaceType == null) { continue; } if (namespaceType.GenericParameterCount != genericParameterCount) { continue; } offset = dotPos + 1; return(namespaceType); } return(null); }
private static INestedTypeDefinition /*?*/ GetNestedType(INameTable nameTable, ITypeDefinition typeDefinition, string typeName, int genericParameterCount, ref int offset) { Contract.Requires(nameTable != null); Contract.Requires(typeDefinition != null); Contract.Requires(typeName != null); Contract.Requires(genericParameterCount >= 0); Contract.Requires(offset >= 0); Contract.Ensures(offset >= 0); int len = typeName.Length; if (offset >= len) { return(null); } int dotPos = typeName.IndexOf('.', offset); Contract.Assume(dotPos < 0 || (dotPos >= offset && dotPos < len)); if (dotPos < 0) { dotPos = len; } IName tName = nameTable.GetNameFor(typeName.Substring(offset, dotPos - offset)); foreach (var member in typeDefinition.GetMembersNamed(tName, false)) { var nestedType = member as INestedTypeDefinition; if (nestedType == null) { continue; } if (dotPos == len) { if (nestedType.GenericParameterCount != genericParameterCount) { continue; } return(nestedType); } if (nestedType.GenericParameterCount > genericParameterCount) { continue; } offset = dotPos + 1; var nt = GetNestedType(nameTable, nestedType, typeName, genericParameterCount - nestedType.GenericParameterCount, ref offset); if (nt != null) { return(nt); } } return(null); }
public static AssemblyIdentity Parse(INameTable nameTable, string formattedName) { var name = new System.Reflection.AssemblyName(formattedName); return new AssemblyIdentity(nameTable.GetNameFor(name.Name), name.CultureName, name.Version, name.GetPublicKeyToken(), #if COREFX ""); #else name.CodeBase); #endif }
public static AssemblyIdentity Parse(INameTable nameTable, string formattedName) { var name = new System.Reflection.AssemblyName(formattedName); return(new AssemblyIdentity(nameTable.GetNameFor(name.Name), name.CultureName, name.Version, name.GetPublicKeyToken(), #if COREFX "")); #else name.CodeBase); #endif }
public static AssemblyIdentity Parse(INameTable nameTable, string formattedName) { var name = new System.Reflection.AssemblyName(formattedName); return(new AssemblyIdentity(nameTable.GetNameFor(name.Name), #if FEATURE_ASSEMBLYNAME_CULTUREINFO name.CultureInfo.Name, #else name.CultureName, #endif name.Version, name.GetPublicKeyToken(), #if FEATURE_ASSEMBLYNAME_CODEBASE name.CodeBase)); #else string.Empty); #endif }
private static IMethodBody /*?*/ FirstStatementIsIteratorCreation(IMetadataHost host, ISourceMethodBody possibleIterator, INameTable nameTable, IStatement statement) { ICreateObjectInstance createObjectInstance = GetICreateObjectInstance(statement); if (createObjectInstance == null) { // If the first statement in the method body is not the creation of iterator closure, return a dummy. // Possible corner case not handled: a local is used to hold the constant value for the initial state of the closure. return(null); } ITypeReference closureType /*?*/ = createObjectInstance.MethodToCall.ContainingType; ITypeReference unspecializedClosureType = ContractHelper.Unspecialized(closureType); if (!AttributeHelper.Contains(unspecializedClosureType.Attributes, host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute)) { return(null); } INestedTypeReference closureTypeAsNestedTypeReference = unspecializedClosureType as INestedTypeReference; if (closureTypeAsNestedTypeReference == null) { return(null); } ITypeReference unspecializedClosureContainingType = ContractHelper.Unspecialized(closureTypeAsNestedTypeReference.ContainingType); if (closureType != null && TypeHelper.TypesAreEquivalent(possibleIterator.MethodDefinition.ContainingTypeDefinition, unspecializedClosureContainingType)) { IName MoveNextName = nameTable.GetNameFor("MoveNext"); foreach (ITypeDefinitionMember member in closureType.ResolvedType.GetMembersNamed(MoveNextName, false)) { IMethodDefinition moveNext = member as IMethodDefinition; if (moveNext != null) { ISpecializedMethodDefinition moveNextGeneric = moveNext as ISpecializedMethodDefinition; if (moveNextGeneric != null) { moveNext = moveNextGeneric.UnspecializedVersion.ResolvedMethod; } return(moveNext.Body); } } } return(null); }
private static IMethodBody/*?*/ FirstStatementIsIteratorCreation(IMetadataHost host, ISourceMethodBody possibleIterator, INameTable nameTable, IStatement statement) { ICreateObjectInstance createObjectInstance = GetICreateObjectInstance(statement); if (createObjectInstance == null) { // If the first statement in the method body is not the creation of iterator closure, return a dummy. // Possible corner case not handled: a local is used to hold the constant value for the initial state of the closure. return null; } ITypeReference closureType/*?*/ = createObjectInstance.MethodToCall.ContainingType; ITypeReference unspecializedClosureType = ContractHelper.Unspecialized(closureType); if (!AttributeHelper.Contains(unspecializedClosureType.Attributes, host.PlatformType.SystemRuntimeCompilerServicesCompilerGeneratedAttribute)) return null; INestedTypeReference closureTypeAsNestedTypeReference = unspecializedClosureType as INestedTypeReference; if (closureTypeAsNestedTypeReference == null) return null; ITypeReference unspecializedClosureContainingType = ContractHelper.Unspecialized(closureTypeAsNestedTypeReference.ContainingType); if (closureType != null && TypeHelper.TypesAreEquivalent(possibleIterator.MethodDefinition.ContainingTypeDefinition, unspecializedClosureContainingType)) { IName MoveNextName = nameTable.GetNameFor("MoveNext"); foreach (ITypeDefinitionMember member in closureType.ResolvedType.GetMembersNamed(MoveNextName, false)) { IMethodDefinition moveNext = member as IMethodDefinition; if (moveNext != null) { ISpecializedMethodDefinition moveNextGeneric = moveNext as ISpecializedMethodDefinition; if (moveNextGeneric != null) moveNext = moveNextGeneric.UnspecializedVersion.ResolvedMethod; return moveNext.Body; } } } return null; }
internal PlatformInvokeInformation(ITypeDeclarationMember declaringMember, SourceCustomAttribute dllImportAttribute) { this.importModule = Dummy.ModuleReference; this.importName = declaringMember.Name.Name; this.noMangle = false; this.pinvokeCallingConvention = PInvokeCallingConvention.WinApi; this.stringFormat = StringFormatKind.Unspecified; this.useBestFit = null; this.throwExceptionForUnmappableChar = null; INameTable nameTable = dllImportAttribute.ContainingBlock.NameTable; int bestFitMappingKey = nameTable.GetNameFor("BestFitMapping").UniqueKey; int callingConventionKey = nameTable.GetNameFor("CallingConvention").UniqueKey; int charSetKey = nameTable.GetNameFor("CharSet").UniqueKey; int entryPointKey = nameTable.GetNameFor("EntryPoint").UniqueKey; int exactSpellingKey = nameTable.GetNameFor("ExactSpelling").UniqueKey; int setLastErrorKey = nameTable.GetNameFor("SetLastError").UniqueKey; int throwOnUnmappableCharKey = nameTable.GetNameFor("ThrowOnUnmappableChar").UniqueKey; foreach (Expression expr in dllImportAttribute.Arguments) { CompileTimeConstant cc = expr as CompileTimeConstant; if (cc != null && cc.Value is string) { IName moduleName = expr.NameTable.GetNameFor((string)cc.Value); this.importModule = new Immutable.ModuleReference(dllImportAttribute.ContainingBlock.Compilation.HostEnvironment, new ModuleIdentity(moduleName, string.Empty)); continue; } NamedArgument narg = expr as NamedArgument; if (narg == null) { continue; } int key = narg.ArgumentName.Name.UniqueKey; if (key == bestFitMappingKey) { if (narg.ArgumentValue.Value is bool) { this.useBestFit = (bool)narg.ArgumentValue.Value; } continue; } if (key == callingConventionKey) { if (narg.ArgumentValue.Value is int) { switch ((CallingConvention)(int)narg.ArgumentValue.Value) { case CallingConvention.C: this.pinvokeCallingConvention = PInvokeCallingConvention.CDecl; break; case CallingConvention.Standard: this.pinvokeCallingConvention = PInvokeCallingConvention.StdCall; break; case CallingConvention.ExplicitThis: this.pinvokeCallingConvention = PInvokeCallingConvention.ThisCall; break; case CallingConvention.FastCall: this.pinvokeCallingConvention = PInvokeCallingConvention.FastCall; break; } } continue; } if (key == charSetKey) { if (narg.ArgumentValue.Value is int) { switch ((CharSet)(int)narg.ArgumentValue.Value) { case CharSet.Ansi: this.stringFormat = StringFormatKind.Ansi; break; case CharSet.Auto: this.stringFormat = StringFormatKind.AutoChar; break; case CharSet.Unicode: this.stringFormat = StringFormatKind.Unicode; break; } } continue; } if (key == entryPointKey) { string /*?*/ importName = narg.ArgumentValue.Value as string; if (importName != null) { this.importName = nameTable.GetNameFor(importName); } continue; } if (key == exactSpellingKey) { if (narg.ArgumentValue.Value is bool) { this.noMangle = (bool)narg.ArgumentValue.Value; } continue; } if (key == setLastErrorKey) { if (narg.ArgumentValue.Value is bool) { this.supportsLastError = (bool)narg.ArgumentValue.Value; } continue; } if (key == throwOnUnmappableCharKey) { if (narg.ArgumentValue.Value is bool) { this.throwExceptionForUnmappableChar = (bool)narg.ArgumentValue.Value; } continue; } } }
/*^ #pragma warning disable 2669 * ^*/ /// <summary> /// Allocates a factory for loading assemblies and modules persisted as portable executable (pe) files. /// </summary> /// <param name="metadataReaderHost"> /// The host is used for providing access to pe files (OpenBinaryDocument), /// applying host specific unification policies (UnifyAssembly, UnifyAssemblyReference, UnifyModuleReference) and for deciding /// whether and how to load referenced assemblies and modules (ResolvingAssemblyReference, ResolvingModuleReference). /// </param> public PeReader( IMetadataReaderHost metadataReaderHost ) { this.ErrorsReporter = new MetadataReaderErrorsReporter(); this.metadataReaderHost = metadataReaderHost; this.InternedIdToModuleMap = new Hashtable <Module>(); INameTable nameTable = metadataReaderHost.NameTable; this.Value__ = nameTable.GetNameFor("value__"); this.AsyncCallback = nameTable.GetNameFor("AsyncCallback"); this.ParamArrayAttribute = nameTable.GetNameFor("ParamArrayAttribute"); this.IAsyncResult = nameTable.GetNameFor("IAsyncResult"); this.ICloneable = nameTable.GetNameFor("ICloneable"); this.RuntimeArgumentHandle = nameTable.GetNameFor("RuntimeArgumentHandle"); this.RuntimeFieldHandle = nameTable.GetNameFor("RuntimeFieldHandle"); this.RuntimeMethodHandle = nameTable.GetNameFor("RuntimeMethodHandle"); this.RuntimeTypeHandle = nameTable.GetNameFor("RuntimeTypeHandle"); this.ArgIterator = nameTable.GetNameFor("ArgIterator"); this.IList = nameTable.GetNameFor("IList"); this.Mscorlib = nameTable.GetNameFor("mscorlib"); this.System_Runtime = nameTable.GetNameFor("System.Runtime"); this._Deleted_ = nameTable.GetNameFor("_Deleted*"); }
internal TypeNameParser( INameTable nameTable, string typeName ) { this.NameTable = nameTable; this.TypeName = typeName; this.Length = typeName.Length; this.Version = nameTable.GetNameFor("Version"); this.Retargetable = nameTable.GetNameFor("Retargetable"); this.PublicKeyToken = nameTable.GetNameFor("PublicKeyToken"); this.Culture = nameTable.GetNameFor("Culture"); this.neutral = nameTable.GetNameFor("neutral"); this.CurrentIdentifierInfo = nameTable.EmptyName; this.NextToken(false); }
internal NestedTypeName(INameTable nameTable, NominalTypeName containingTypeName, IName mangledName) { this.ContainingTypeName = containingTypeName; this.Name = mangledName; string nameStr = null; TypeCache.SplitMangledTypeName(mangledName.Value, out nameStr, out this.genericParameterCount); this.unmangledTypeName = nameTable.GetNameFor(nameStr); }
internal NamespaceTypeName(INameTable nameTable, NamespaceName/*?*/ namespaceName, IName name) { this.NamespaceName = namespaceName; this.Name = name; string nameStr = null; TypeCache.SplitMangledTypeName(name.Value, out nameStr, out this.genericParameterCount); if (this.genericParameterCount > 0) this.unmanagledTypeName = nameTable.GetNameFor(nameStr); else this.unmanagledTypeName = name; }
internal NamespaceName(INameTable nameTable, NamespaceName/*?*/ parentNamespaceName, IName name) { this.ParentNamespaceName = parentNamespaceName; this.Name = name; if (parentNamespaceName == null) this.FullyQualifiedName = name; else this.FullyQualifiedName = nameTable.GetNameFor(parentNamespaceName.FullyQualifiedName.Value + "." + name); }
private Assembly TranslateMetadata(IAssemblySymbol assemblySymbol) { Contract.Requires(assemblySymbol != null); Contract.Ensures(Contract.Result <Assembly>() != null); IAssemblyReference cciAssemblyReference = null; Assembly cciAssembly = null; if (assemblySymbolCache.TryGetValue(assemblySymbol, out cciAssemblyReference)) { cciAssembly = cciAssemblyReference as Assembly; System.Diagnostics.Debug.Assert(cciAssembly != null); return(cciAssembly); } var coreAssembly = host.LoadAssembly(host.CoreAssemblySymbolicIdentity); var name = assemblySymbol.Identity.Name; var iname = nameTable.GetNameFor(name); cciAssembly = new Assembly() { Attributes = this.TranslateMetadata(assemblySymbol.GetAttributes()), Name = iname, Locations = Helper.WrapLocations(assemblySymbol.Locations), ModuleName = iname, Kind = ModuleKind.DynamicallyLinkedLibrary, RequiresStartupStub = this.host.PointerSize == 4, TargetRuntimeVersion = coreAssembly.TargetRuntimeVersion, Version = assemblySymbol.Identity.Version, }; cciAssembly.AssemblyReferences.Add(coreAssembly); this.assemblySymbolCache.Add(assemblySymbol, cciAssembly); this.module = cciAssembly; var rootUnitNamespace = new RootUnitNamespace(); cciAssembly.UnitNamespaceRoot = rootUnitNamespace; rootUnitNamespace.Unit = cciAssembly; this.namespaceSymbolCache.Add(assemblySymbol.GlobalNamespace, rootUnitNamespace); var moduleClass = new NamespaceTypeDefinition() { ContainingUnitNamespace = rootUnitNamespace, InternFactory = host.InternFactory, IsClass = true, Name = nameTable.GetNameFor("<Module>"), }; cciAssembly.AllTypes.Add(moduleClass); foreach (var m in assemblySymbol.GlobalNamespace.GetMembers()) { var namespaceSymbol = m as INamespaceSymbol; if (namespaceSymbol != null) { var cciNtd = TranslateMetadata(namespaceSymbol); rootUnitNamespace.Members.Add(cciNtd); continue; } var typeSymbol = m as ITypeSymbol; if (typeSymbol != null) { var namedType = TranslateMetadata((INamedTypeSymbol)typeSymbol); // TODO: fix //namedType.Attributes = TranslateMetadata(typeSymbol.GetAttributes()); var cciType = (INamespaceTypeDefinition)namedType; rootUnitNamespace.Members.Add(cciType); //cciAssembly.AllTypes.Add(cciType); continue; } } //if (this.entryPoint != null) { // cciAssembly.Kind = ModuleKind.ConsoleApplication; // cciAssembly.EntryPoint = this.entryPoint; //} return(cciAssembly); }
/// <summary> /// A base class for an object provided by the application hosting the metadata reader. The object allows the host application /// to control how assembly references are unified, where files are found, how Windows Runtime types and methods are projected to CLR types and methods /// and so on. The object also controls the lifetime of things such as memory mapped files and blocks of unmanaged memory. Be sure to call Dispose on the object when /// it is no longer needed and the associated locks and/or memory must be released immediately. /// </summary> /// <param name="nameTable"> /// A collection of IName instances that represent names that are commonly used during compilation. /// This is a provided as a parameter to the host environment in order to allow more than one host /// environment to co-exist while agreeing on how to map strings to IName instances. /// </param> /// <param name="factory"> /// The intern factory to use when generating keys. When comparing two or more assemblies using /// TypeHelper, MemberHelper, etc. it is necessary to make the hosts use the same intern factory. /// </param> /// <param name="pointerSize">The size of a pointer on the runtime that is the target of the metadata units to be loaded /// into this metadta host. This parameter only matters if the host application wants to work out what the exact layout /// of a struct will be on the target runtime. The framework uses this value in methods such as TypeHelper.SizeOfType and /// TypeHelper.TypeAlignment. If the host application does not care about the pointer size it can provide 0 as the value /// of this parameter. In that case, the first reference to IMetadataHost.PointerSize will probe the list of loaded assemblies /// to find an assembly that either requires 32 bit pointers or 64 bit pointers. If no such assembly is found, the default is 32 bit pointers. /// </param> /// <param name="searchPaths"> /// A collection of strings that are interpreted as valid paths which are used to search for units. /// </param> /// <param name="searchInGAC"> /// Whether the GAC (Global Assembly Cache) should be searched when resolving references. /// </param> /// <param name="projectToCLRTypes">True if the host should project references to certain Windows Runtime types and methods /// to corresponding CLR types and methods, in order to emulate the runtime behavior of the CLR.</param> protected WindowsRuntimeMetadataReaderHost(INameTable nameTable, IInternFactory factory, byte pointerSize, IEnumerable<string> searchPaths, bool searchInGAC, bool projectToCLRTypes) : base(nameTable, factory, pointerSize, searchPaths, searchInGAC) { Contract.Requires(pointerSize == 0 || pointerSize == 4 || pointerSize == 8); this.projectToCLRTypes = projectToCLRTypes; this.AllowMultiple = nameTable.GetNameFor("AllowMultiple"); this.AllowMultipleAttribute = nameTable.GetNameFor("AllowMultipleAttribute"); this.Animation = nameTable.GetNameFor("Animation"); this.Collections = nameTable.GetNameFor("Collections"); this.Controls = nameTable.GetNameFor("Controls"); this.Data = nameTable.GetNameFor("Data"); this.Foundation = nameTable.GetNameFor("Foundation"); this.HResult = nameTable.GetNameFor("HResult"); this.IBindableIterable = nameTable.GetNameFor("IBindableIterable"); this.IBindableVector = nameTable.GetNameFor("IBindableVector"); this.IClosable = nameTable.GetNameFor("IClosable"); this.IIterable = nameTable.GetNameFor("IIterable"); this.IKeyValuePair = nameTable.GetNameFor("IKeyValuePair"); this.IMap = nameTable.GetNameFor("IMap"); this.IMapView = nameTable.GetNameFor("IMapView"); this.INotifyCollectionChanged = nameTable.GetNameFor("INotifyCollectionChanged"); this.INotifyPropertyChanged = nameTable.GetNameFor("INotifyPropertyChanged"); this.Input = nameTable.GetNameFor("Input"); this.Interop = nameTable.GetNameFor("Interop"); this.IReference = nameTable.GetNameFor("IReference"); this.IVector = nameTable.GetNameFor("IVector"); this.IVectorView = nameTable.GetNameFor("IVectorView"); this.Media = nameTable.GetNameFor("Media"); this.Media3D = nameTable.GetNameFor("Media3D"); this.Metadata = nameTable.GetNameFor("Metadata"); this.NotifyCollectionChangedAction = nameTable.GetNameFor("NotifyCollectionChangedAction"); this.NotifyCollectionChangedEventArgs = nameTable.GetNameFor("NotifyCollectionChangedEventArgs"); this.NotifyCollectionChangedEventHandler = nameTable.GetNameFor("NotifyCollectionChangedEventHandler"); this.Primitives = nameTable.GetNameFor("Primitives"); this.PropertyChangedEventArgs = nameTable.GetNameFor("PropertyChangedEventArgs"); this.PropertyChangedEventHandler = nameTable.GetNameFor("PropertyChangedEventHandler"); this.TypeName = nameTable.GetNameFor("TypeName"); this.UI = nameTable.GetNameFor("UI"); this.Windows = nameTable.GetNameFor("Windows"); this.Xaml = nameTable.GetNameFor("Xaml"); }