private MinimalCorlib() : base("corlib") { var types = new DefaultUnresolvedTypeDefinition[KnownTypeReference.KnownTypeCodeCount]; for (int i = 0; i < types.Length; i++) { var typeRef = KnownTypeReference.Get((KnownTypeCode)i); if (typeRef != null) { types[i] = new DefaultUnresolvedTypeDefinition(typeRef.Namespace, typeRef.Name); for (int j = 0; j < typeRef.TypeParameterCount; j++) { types[i].TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, j)); } AddTypeDefinition(types[i]); } } for (int i = 0; i < types.Length; i++) { var typeRef = KnownTypeReference.Get((KnownTypeCode)i); if (typeRef != null && typeRef.baseType != KnownTypeCode.None) { types[i].BaseTypes.Add(types[(int)typeRef.baseType]); if (typeRef.baseType == KnownTypeCode.ValueType && i != (int)KnownTypeCode.Enum) { types[i].Kind = TypeKind.Struct; } } } Freeze(); }
IType SearchType(KnownTypeCode typeCode) { KnownTypeReference typeRef = KnownTypeReference.Get(typeCode); if (typeRef == null) { return(SpecialType.UnknownType); } ITypeDefinition typeDef; foreach (IAssembly asm in compilation.ReferencedAssemblies) { typeDef = asm.GetTypeDefinition(typeRef.Namespace, typeRef.Name, typeRef.TypeParameterCount); if (typeDef != null) { return(typeDef); } } typeDef = compilation.MainAssembly.GetTypeDefinition(typeRef.Namespace, typeRef.Name, typeRef.TypeParameterCount); if (typeDef != null) { return(typeDef); } else { return(new UnknownType(typeRef.Namespace, typeRef.Name, typeRef.TypeParameterCount)); } }
public CorlibTypeDefinition(MinimalCorlib corlib, KnownTypeCode typeCode) { this.corlib = corlib; this.typeCode = typeCode; KnownTypeReference ktr = KnownTypeReference.Get(typeCode); this.typeKind = ktr.typeKind; this.MetadataName = ktr.Name + (ktr.TypeParameterCount > 0 ? "`" + ktr.TypeParameterCount : ""); }
public RequiredNamespaceCollector(HashSet <string> namespaces) { this.namespaces = namespaces; for (int i = 0; i < KnownTypeReference.KnownTypeCodeCount; i++) { var ktr = KnownTypeReference.Get((KnownTypeCode)i); if (ktr == null) { continue; } namespaces.Add(ktr.Namespace); } }
public override ITypeReference ToTypeReference(NameLookupMode lookupMode, InterningProvider interningProvider = null) { KnownTypeCode typeCode = GetTypeCodeForPrimitiveType(this.Keyword); if (typeCode == KnownTypeCode.None) { return(new UnknownType(null, this.Keyword)); } else { return(KnownTypeReference.Get(typeCode)); } }
private MinimalCorlib(ICompilation compilation) { this.Compilation = compilation; this.typeDefinitions = new CorlibTypeDefinition[KnownTypeReference.KnownTypeCodeCount]; this.rootNamespace = new CorlibNamespace(this, null, string.Empty, string.Empty); for (int i = 0; i < KnownTypeReference.KnownTypeCodeCount; i++) { if (KnownTypeReference.Get((KnownTypeCode)i) != null) { typeDefinitions[i] = new CorlibTypeDefinition(this, (KnownTypeCode)i); } } }
public override ITypeReference ToTypeReference(SimpleNameLookupMode lookupMode = SimpleNameLookupMode.Type) { KnownTypeCode typeCode = GetTypeCodeForPrimitiveType(this.Keyword); if (typeCode == KnownTypeCode.None) { return(new UnknownType(null, this.Keyword)); } else { return(KnownTypeReference.Get(typeCode)); } }
IType SearchType(KnownTypeCode typeCode) { KnownTypeReference typeRef = KnownTypeReference.Get(typeCode); if (typeRef == null) { return(SpecialType.UnknownType); } var typeName = new TopLevelTypeName(typeRef.Namespace, typeRef.Name, typeRef.TypeParameterCount); foreach (IAssembly asm in compilation.Assemblies) { var typeDef = asm.GetTypeDefinition(typeName); if (typeDef != null) { return(typeDef); } } return(new UnknownType(typeName)); }
internal MetadataTypeDefinition(MetadataModule module, TypeDefinitionHandle handle) { Debug.Assert(module != null); Debug.Assert(!handle.IsNil); this.module = module; this.handle = handle; var metadata = module.metadata; var td = metadata.GetTypeDefinition(handle); this.attributes = td.Attributes; this.fullTypeName = td.GetFullTypeName(metadata); // Find DeclaringType + KnownTypeCode: if (fullTypeName.IsNested) { this.DeclaringTypeDefinition = module.GetDefinition(td.GetDeclaringType()); // Create type parameters: this.TypeParameters = MetadataTypeParameter.Create(module, this.DeclaringTypeDefinition, this, td.GetGenericParameters()); } else { // Create type parameters: this.TypeParameters = MetadataTypeParameter.Create(module, this, td.GetGenericParameters()); var topLevelTypeName = fullTypeName.TopLevelTypeName; for (int i = 0; i < KnownTypeReference.KnownTypeCodeCount; i++) { var ktr = KnownTypeReference.Get((KnownTypeCode)i); if (ktr != null && ktr.TypeName == topLevelTypeName) { this.KnownTypeCode = (KnownTypeCode)i; break; } } } // Find type kind: if ((attributes & TypeAttributes.ClassSemanticsMask) == TypeAttributes.Interface) { this.Kind = TypeKind.Interface; } else if (td.IsEnum(metadata, out var underlyingType)) { this.Kind = TypeKind.Enum; this.EnumUnderlyingType = module.Compilation.FindType(underlyingType.ToKnownTypeCode()); } else if (td.IsValueType(metadata)) { if (KnownTypeCode == KnownTypeCode.Void) { this.Kind = TypeKind.Void; } else { this.Kind = TypeKind.Struct; this.IsByRefLike = td.GetCustomAttributes().HasKnownAttribute(metadata, KnownAttribute.IsByRefLike); this.IsReadOnly = td.GetCustomAttributes().HasKnownAttribute(metadata, KnownAttribute.IsReadOnly); } } else if (td.IsDelegate(metadata)) { this.Kind = TypeKind.Delegate; } else { this.Kind = TypeKind.Class; this.HasExtensionMethods = this.IsStatic && (module.TypeSystemOptions & TypeSystemOptions.ExtensionMethods) == TypeSystemOptions.ExtensionMethods && td.GetCustomAttributes().HasKnownAttribute(metadata, KnownAttribute.Extension); } }
public static bool IsKnownType(this EntityHandle handle, MetadataReader reader, KnownTypeCode knownType) { return(!handle.IsNil && GetFullTypeName(handle, reader) == KnownTypeReference.Get(knownType).TypeName); }
public CorlibTypeDefinition(MinimalCorlib corlib, KnownTypeCode typeCode) { this.corlib = corlib; this.typeCode = typeCode; this.typeKind = KnownTypeReference.Get(typeCode).typeKind; }
public BamlDecompilerTypeSystem(PEFile mainModule, IAssemblyResolver assemblyResolver) { if (mainModule == null) { throw new ArgumentNullException(nameof(mainModule)); } if (assemblyResolver == null) { throw new ArgumentNullException(nameof(assemblyResolver)); } // Load referenced assemblies and type-forwarder references. // This is necessary to make .NET Core/PCL binaries work better. var referencedAssemblies = new List <PEFile>(); var assemblyReferenceQueue = new Queue <(bool IsAssembly, PEFile MainModule, object Reference)>(); var mainMetadata = mainModule.Metadata; foreach (var h in mainMetadata.GetModuleReferences()) { var moduleRef = mainMetadata.GetModuleReference(h); var moduleName = mainMetadata.GetString(moduleRef.Name); foreach (var fileHandle in mainMetadata.AssemblyFiles) { var file = mainMetadata.GetAssemblyFile(fileHandle); if (mainMetadata.StringComparer.Equals(file.Name, moduleName) && file.ContainsMetadata) { assemblyReferenceQueue.Enqueue((false, mainModule, moduleName)); break; } } } foreach (var refs in mainModule.AssemblyReferences) { assemblyReferenceQueue.Enqueue((true, mainModule, refs)); } foreach (var bamlReference in defaultBamlReferences) { assemblyReferenceQueue.Enqueue((true, mainModule, AssemblyNameReference.Parse(bamlReference))); } var comparer = KeyComparer.Create(((bool IsAssembly, PEFile MainModule, object Reference)reference) => reference.IsAssembly ? "A:" + ((IAssemblyReference)reference.Reference).FullName : "M:" + reference.Reference); var processedAssemblyReferences = new HashSet <(bool IsAssembly, PEFile Parent, object Reference)>(comparer); while (assemblyReferenceQueue.Count > 0) { var asmRef = assemblyReferenceQueue.Dequeue(); if (!processedAssemblyReferences.Add(asmRef)) { continue; } PEFile asm; if (asmRef.IsAssembly) { asm = assemblyResolver.Resolve((IAssemblyReference)asmRef.Reference); } else { asm = assemblyResolver.ResolveModule(asmRef.MainModule, (string)asmRef.Reference); } if (asm != null) { referencedAssemblies.Add(asm); var metadata = asm.Metadata; foreach (var h in metadata.ExportedTypes) { var exportedType = metadata.GetExportedType(h); switch (exportedType.Implementation.Kind) { case SRM.HandleKind.AssemblyReference: assemblyReferenceQueue.Enqueue((true, asm, new AssemblyReference(asm, (SRM.AssemblyReferenceHandle)exportedType.Implementation))); break; case SRM.HandleKind.AssemblyFile: var file = metadata.GetAssemblyFile((SRM.AssemblyFileHandle)exportedType.Implementation); assemblyReferenceQueue.Enqueue((false, asm, metadata.GetString(file.Name))); break; } } } } var mainModuleWithOptions = mainModule.WithOptions(TypeSystemOptions.Default); var referencedAssembliesWithOptions = referencedAssemblies.Select(file => file.WithOptions(TypeSystemOptions.Default)); // Primitive types are necessary to avoid assertions in ILReader. // Fallback to MinimalCorlib to provide the primitive types. if (!HasType(KnownTypeCode.Void) || !HasType(KnownTypeCode.Int32)) { Init(mainModule.WithOptions(TypeSystemOptions.Default), referencedAssembliesWithOptions.Concat(new[] { MinimalCorlib.Instance })); } else { Init(mainModuleWithOptions, referencedAssembliesWithOptions); } this.MainModule = (MetadataModule)base.MainModule; bool HasType(KnownTypeCode code) { TopLevelTypeName name = KnownTypeReference.Get(code).TypeName; if (mainModule.GetTypeDefinition(name) != null) { return(true); } foreach (var file in referencedAssemblies) { if (file.GetTypeDefinition(name) != null) { return(true); } } return(false); } }