Files

589 lines
21 KiB
C#
Raw Permalink Normal View History

2026-08-27 10:56:38 -06:00
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Reflection.PortableExecutable;
using Microsoft.Cci;
using Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Symbols;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols;
internal abstract class AssemblySymbol : Symbol, IAssemblySymbolInternal, ISymbolInternal
{
private AssemblySymbol _corLibrary;
private static readonly char[] s_nestedTypeNameSeparators = new char[1] { '+' };
internal AssemblySymbol CorLibrary => _corLibrary;
internal abstract TypeConversions TypeConversions { get; }
public override string Name => Identity.Name;
public abstract AssemblyIdentity Identity { get; }
AssemblyIdentity IAssemblySymbolInternal.Identity => Identity;
IAssemblySymbolInternal IAssemblySymbolInternal.CorLibrary => (IAssemblySymbolInternal)(object)CorLibrary;
public abstract Version AssemblyVersionPattern { get; }
internal Machine Machine => Modules[0].Machine;
internal bool Bit32Required => Modules[0].Bit32Required;
public abstract NamespaceSymbol GlobalNamespace { get; }
public abstract ImmutableArray<ModuleSymbol> Modules { get; }
public sealed override SymbolKind Kind => (SymbolKind)2;
public sealed override AssemblySymbol ContainingAssembly => null;
internal abstract bool IsMissing { get; }
public sealed override Accessibility DeclaredAccessibility => (Accessibility)0;
public sealed override bool IsStatic => false;
public sealed override bool IsVirtual => false;
public sealed override bool IsOverride => false;
public sealed override bool IsAbstract => false;
public sealed override bool IsSealed => false;
public sealed override bool IsExtern => false;
public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences => ImmutableArray<SyntaxReference>.Empty;
public virtual bool IsInteractive => false;
public sealed override Symbol ContainingSymbol => null;
internal virtual bool KeepLookingForDeclaredSpecialTypes
{
get
{
throw ExceptionUtilities.Unreachable("/_/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs", 395);
}
}
internal bool RuntimeSupportsDefaultInterfaceImplementation => RuntimeSupportsFeature((SpecialMember)120);
internal bool RuntimeSupportsStaticAbstractMembersInInterfaces => RuntimeSupportsFeature((SpecialMember)123);
internal bool RuntimeSupportsNumericIntPtr
{
get
{
if ((object)CorLibrary != null)
{
return RuntimeSupportsFeature((SpecialMember)124);
}
return false;
}
}
internal bool RuntimeSupportsInlineArrayTypes => (object)GetSpecialTypeMember((SpecialMember)127) != null;
internal bool RuntimeSupportsUnmanagedSignatureCallingConvention => RuntimeSupportsFeature((SpecialMember)121);
internal bool RuntimeSupportsByRefFields => RuntimeSupportsFeature((SpecialMember)125);
internal bool RuntimeSupportsCovariantReturnsOfClasses
{
get
{
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_001d: Invalid comparison between Unknown and I4
if (RuntimeSupportsFeature((SpecialMember)122))
{
NamedTypeSymbol specialType = GetSpecialType((SpecialType)45);
if ((object)specialType != null)
{
return (int)specialType.TypeKind == 2;
}
return false;
}
return false;
}
}
internal abstract bool IsLinked { get; }
public abstract ICollection<string> TypeNames { get; }
public abstract ICollection<string> NamespaceNames { get; }
public abstract bool MightContainExtensionMethods { get; }
internal static TypeSymbol DynamicType => DynamicTypeSymbol.Instance;
internal NamedTypeSymbol ObjectType => GetSpecialType((SpecialType)1);
internal abstract ImmutableArray<byte> PublicKey { get; }
internal void SetCorLibrary(AssemblySymbol corLibrary)
{
_corLibrary = corLibrary;
}
internal NamespaceSymbol GetAssemblyNamespace(NamespaceSymbol namespaceSymbol)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0027: Invalid comparison between Unknown and I4
if (namespaceSymbol.IsGlobalNamespace)
{
return GlobalNamespace;
}
NamespaceSymbol containingNamespace = namespaceSymbol.ContainingNamespace;
if ((object)containingNamespace == null)
{
return GlobalNamespace;
}
if ((int)namespaceSymbol.NamespaceKind == 2 && namespaceSymbol.ContainingAssembly == this)
{
return namespaceSymbol;
}
NamespaceSymbol assemblyNamespace = GetAssemblyNamespace(containingNamespace);
if ((object)assemblyNamespace == containingNamespace)
{
return namespaceSymbol;
}
return assemblyNamespace?.GetNestedNamespace(namespaceSymbol.Name);
}
internal override TResult Accept<TArgument, TResult>(CSharpSymbolVisitor<TArgument, TResult> visitor, TArgument argument)
{
return visitor.VisitAssembly(this, argument);
}
public override void Accept(CSharpSymbolVisitor visitor)
{
visitor.VisitAssembly(this);
}
public override TResult Accept<TResult>(CSharpSymbolVisitor<TResult> visitor)
{
return visitor.VisitAssembly(this);
}
internal AssemblySymbol()
{
}
internal abstract NamedTypeSymbol? LookupDeclaredTopLevelMetadataType(ref MetadataTypeName emittedName);
internal abstract NamedTypeSymbol LookupDeclaredOrForwardedTopLevelMetadataType(ref MetadataTypeName emittedName, ConsList<AssemblySymbol>? visitedAssemblies);
public NamedTypeSymbol? ResolveForwardedType(string fullyQualifiedMetadataName)
{
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Unknown result type (might be due to invalid IL or missing references)
if (fullyQualifiedMetadataName == null)
{
throw new ArgumentNullException("fullyQualifiedMetadataName");
}
MetadataTypeName emittedName = MetadataTypeName.FromFullName(fullyQualifiedMetadataName, false, -1);
return TryLookupForwardedMetadataTypeWithCycleDetection(ref emittedName, null);
}
internal virtual NamedTypeSymbol? TryLookupForwardedMetadataTypeWithCycleDetection(ref MetadataTypeName emittedName, ConsList<AssemblySymbol>? visitedAssemblies)
{
return null;
}
internal ErrorTypeSymbol CreateCycleInTypeForwarderErrorTypeSymbol(ref MetadataTypeName emittedName)
{
DiagnosticInfo errorInfo = (DiagnosticInfo)(object)new CSDiagnosticInfo(ErrorCode.ERR_CycleInTypeForwarder, ((MetadataTypeName)(ref emittedName)).FullName, Name);
return new MissingMetadataTypeSymbol.TopLevel(Modules[0], ref emittedName, errorInfo);
}
internal ErrorTypeSymbol CreateMultipleForwardingErrorTypeSymbol(ref MetadataTypeName emittedName, ModuleSymbol forwardingModule, AssemblySymbol destination1, AssemblySymbol destination2)
{
CSDiagnosticInfo errorInfo = new CSDiagnosticInfo(ErrorCode.ERR_TypeForwardedToMultipleAssemblies, forwardingModule, this, ((MetadataTypeName)(ref emittedName)).FullName, destination1, destination2);
return new MissingMetadataTypeSymbol.TopLevel(forwardingModule, ref emittedName, (DiagnosticInfo?)(object)errorInfo);
}
internal abstract IEnumerable<NamedTypeSymbol> GetAllTopLevelForwardedTypes();
internal abstract NamedTypeSymbol GetDeclaredSpecialType(SpecialType type);
internal virtual void RegisterDeclaredSpecialType(NamedTypeSymbol corType)
{
throw ExceptionUtilities.Unreachable("/_/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs", 384);
}
internal virtual NamedTypeSymbol GetNativeIntegerType(NamedTypeSymbol underlyingType)
{
throw ExceptionUtilities.Unreachable("/_/src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs", 404);
}
public bool SupportsRuntimeCapability(RuntimeCapability capability)
{
//IL_0000: Unknown result type (might be due to invalid IL or missing references)
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Expected I4, but got Unknown
return (capability - 1) switch
{
0 => RuntimeSupportsByRefFields,
1 => RuntimeSupportsCovariantReturnsOfClasses,
2 => RuntimeSupportsDefaultInterfaceImplementation,
3 => RuntimeSupportsNumericIntPtr,
4 => RuntimeSupportsUnmanagedSignatureCallingConvention,
5 => RuntimeSupportsStaticAbstractMembersInInterfaces,
6 => RuntimeSupportsInlineArrayTypes,
_ => false,
};
}
protected bool RuntimeSupportsFeature(SpecialMember feature)
{
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0013: Invalid comparison between Unknown and I4
//IL_001e: Unknown result type (might be due to invalid IL or missing references)
NamedTypeSymbol specialType = GetSpecialType((SpecialType)44);
if ((object)specialType != null && (int)specialType.TypeKind == 2 && specialType.IsStatic)
{
return (object)GetSpecialTypeMember(feature) != null;
}
return false;
}
internal abstract ImmutableArray<AssemblySymbol> GetNoPiaResolutionAssemblies();
internal abstract void SetNoPiaResolutionAssemblies(ImmutableArray<AssemblySymbol> assemblies);
internal abstract ImmutableArray<AssemblySymbol> GetLinkedReferencedAssemblies();
internal abstract void SetLinkedReferencedAssemblies(ImmutableArray<AssemblySymbol> assemblies);
IEnumerable<ImmutableArray<byte>> IAssemblySymbolInternal.GetInternalsVisibleToPublicKeys(string simpleName)
{
return GetInternalsVisibleToPublicKeys(simpleName);
}
internal abstract IEnumerable<ImmutableArray<byte>> GetInternalsVisibleToPublicKeys(string simpleName);
IEnumerable<string> IAssemblySymbolInternal.GetInternalsVisibleToAssemblyNames()
{
return GetInternalsVisibleToAssemblyNames();
}
internal abstract IEnumerable<string> GetInternalsVisibleToAssemblyNames();
bool IAssemblySymbolInternal.AreInternalsVisibleToThisAssembly(IAssemblySymbolInternal otherAssembly)
{
return AreInternalsVisibleToThisAssembly((AssemblySymbol)(object)otherAssembly);
}
internal abstract bool AreInternalsVisibleToThisAssembly(AssemblySymbol other);
internal virtual bool GetGuidString(out string guidString)
{
return GetGuidStringDefaultImplementation(out guidString);
}
internal NamedTypeSymbol GetSpecialType(SpecialType type)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
return CorLibrary.GetDeclaredSpecialType(type);
}
internal NamedTypeSymbol GetPrimitiveType(PrimitiveTypeCode type)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
return GetSpecialType(SpecialTypes.GetTypeFromMetadataName(type));
}
public NamedTypeSymbol? GetTypeByMetadataName(string fullyQualifiedMetadataName)
{
if (fullyQualifiedMetadataName == null)
{
throw new ArgumentNullException("fullyQualifiedMetadataName");
}
(AssemblySymbol, AssemblySymbol) conflicts;
return GetTypeByMetadataName(fullyQualifiedMetadataName, includeReferences: false, isWellKnownType: false, out conflicts);
}
internal NamedTypeSymbol? GetTypeByMetadataName(string metadataName, bool includeReferences, bool isWellKnownType, out (AssemblySymbol, AssemblySymbol) conflicts, bool useCLSCompliantNameArityEncoding = false, DiagnosticBag? warnings = null, bool ignoreCorLibraryDuplicatedTypes = false)
{
//IL_0075: Unknown result type (might be due to invalid IL or missing references)
//IL_007a: Unknown result type (might be due to invalid IL or missing references)
//IL_001d: Unknown result type (might be due to invalid IL or missing references)
//IL_0022: Unknown result type (might be due to invalid IL or missing references)
//IL_0043: Unknown result type (might be due to invalid IL or missing references)
//IL_0048: Unknown result type (might be due to invalid IL or missing references)
NamedTypeSymbol namedTypeSymbol;
if (metadataName.IndexOf('+') >= 0)
{
string[] array = metadataName.Split(s_nestedTypeNameSeparators);
MetadataTypeName metadataName2 = MetadataTypeName.FromFullName(array[0], useCLSCompliantNameArityEncoding, -1);
namedTypeSymbol = GetTopLevelTypeByMetadataName(ref metadataName2, null, includeReferences, isWellKnownType, out conflicts, warnings, ignoreCorLibraryDuplicatedTypes);
if ((object)namedTypeSymbol == null)
{
return null;
}
for (int i = 1; i < array.Length; i++)
{
metadataName2 = MetadataTypeName.FromTypeName(array[i], false, -1);
namedTypeSymbol = namedTypeSymbol.LookupMetadataType(ref metadataName2);
if ((object)namedTypeSymbol == null)
{
return null;
}
if (isWellKnownType && !IsValidWellKnownType(namedTypeSymbol))
{
return null;
}
}
}
else
{
MetadataTypeName metadataName2 = MetadataTypeName.FromFullName(metadataName, useCLSCompliantNameArityEncoding, -1);
namedTypeSymbol = GetTopLevelTypeByMetadataName(ref metadataName2, null, includeReferences, isWellKnownType, out conflicts, warnings, ignoreCorLibraryDuplicatedTypes);
}
return namedTypeSymbol;
}
internal TypeSymbol? GetTypeByReflectionType(Type type)
{
//IL_01a3: Unknown result type (might be due to invalid IL or missing references)
//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
//IL_0138: Unknown result type (might be due to invalid IL or missing references)
//IL_013d: Unknown result type (might be due to invalid IL or missing references)
TypeInfo typeInfo = type.GetTypeInfo();
if (typeInfo.IsArray)
{
TypeSymbol typeByReflectionType = GetTypeByReflectionType(typeInfo.GetElementType());
if ((object)typeByReflectionType == null)
{
return null;
}
int arrayRank = typeInfo.GetArrayRank();
return ArrayTypeSymbol.CreateCSharpArray(this, TypeWithAnnotations.Create(typeByReflectionType), arrayRank);
}
if (typeInfo.IsPointer)
{
TypeSymbol typeByReflectionType2 = GetTypeByReflectionType(typeInfo.GetElementType());
if ((object)typeByReflectionType2 == null)
{
return null;
}
return new PointerTypeSymbol(TypeWithAnnotations.Create(typeByReflectionType2));
}
if (typeInfo.DeclaringType != null)
{
Type[] genericTypeArguments = typeInfo.GenericTypeArguments;
int currentTypeArgument = 0;
TypeInfo typeInfo2 = (typeInfo.IsGenericType ? typeInfo.GetGenericTypeDefinition().GetTypeInfo() : typeInfo);
ArrayBuilder<TypeInfo> instance = ArrayBuilder<TypeInfo>.GetInstance();
while (true)
{
instance.Add(typeInfo2);
if (typeInfo2.DeclaringType == null)
{
break;
}
typeInfo2 = typeInfo2.DeclaringType.GetTypeInfo();
}
int num = instance.Count - 1;
NamedTypeSymbol namedTypeSymbol = (NamedTypeSymbol)GetTypeByReflectionType(instance[num].AsType());
if ((object)namedTypeSymbol == null)
{
return null;
}
while (--num >= 0)
{
int num2 = instance[num].GenericTypeParameters.Length - instance[num + 1].GenericTypeParameters.Length;
MetadataTypeName emittedTypeName = MetadataTypeName.FromTypeName(instance[num].Name, false, num2);
namedTypeSymbol = namedTypeSymbol.LookupMetadataType(ref emittedTypeName);
if ((object)namedTypeSymbol == null)
{
return null;
}
namedTypeSymbol = ApplyGenericArguments(namedTypeSymbol, genericTypeArguments, ref currentTypeArgument);
if ((object)namedTypeSymbol == null)
{
return null;
}
}
instance.Free();
return namedTypeSymbol;
}
AssemblyIdentity assemblyOpt = AssemblyIdentity.FromAssemblyDefinition(typeInfo.Assembly);
MetadataTypeName metadataName = MetadataTypeName.FromNamespaceAndTypeName(typeInfo.Namespace ?? string.Empty, typeInfo.Name, false, typeInfo.GenericTypeArguments.Length);
(AssemblySymbol, AssemblySymbol) conflicts;
NamedTypeSymbol topLevelTypeByMetadataName = GetTopLevelTypeByMetadataName(ref metadataName, assemblyOpt, includeReferences: true, isWellKnownType: false, out conflicts);
if ((object)topLevelTypeByMetadataName == null)
{
return null;
}
int currentTypeArgument2 = 0;
Type[] genericTypeArguments2 = typeInfo.GenericTypeArguments;
return ApplyGenericArguments(topLevelTypeByMetadataName, genericTypeArguments2, ref currentTypeArgument2);
}
private NamedTypeSymbol? ApplyGenericArguments(NamedTypeSymbol symbol, Type[] typeArguments, ref int currentTypeArgument)
{
if (typeArguments.Length - currentTypeArgument == 0)
{
return symbol;
}
int length = symbol.TypeArgumentsWithAnnotationsNoUseSiteDiagnostics.Length;
ArrayBuilder<TypeWithAnnotations> instance = ArrayBuilder<TypeWithAnnotations>.GetInstance(length);
for (int i = 0; i < length; i++)
{
TypeSymbol typeByReflectionType = GetTypeByReflectionType(typeArguments[currentTypeArgument++]);
if ((object)typeByReflectionType == null)
{
return null;
}
instance.Add(TypeWithAnnotations.Create(typeByReflectionType));
}
return symbol.ConstructIfGeneric(instance.ToImmutableAndFree());
}
internal NamedTypeSymbol? GetTopLevelTypeByMetadataName(ref MetadataTypeName metadataName, AssemblyIdentity? assemblyOpt, bool includeReferences, bool isWellKnownType, out (AssemblySymbol, AssemblySymbol) conflicts, DiagnosticBag? warnings = null, bool ignoreCorLibraryDuplicatedTypes = false)
{
//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
conflicts = default((AssemblySymbol, AssemblySymbol));
NamedTypeSymbol namedTypeSymbol = GetTopLevelTypeByMetadataName(this, ref metadataName, assemblyOpt);
if (isWellKnownType && !IsValidWellKnownType(namedTypeSymbol))
{
namedTypeSymbol = null;
}
if ((object)namedTypeSymbol != null || !includeReferences)
{
return namedTypeSymbol;
}
bool flag = isWellKnownType && warnings != null;
bool flag2 = false;
if ((object)CorLibrary != this && !CorLibrary.IsMissing && !flag && !ignoreCorLibraryDuplicatedTypes)
{
NamedTypeSymbol topLevelTypeByMetadataName = GetTopLevelTypeByMetadataName(CorLibrary, ref metadataName, assemblyOpt);
flag2 = true;
if (isValidCandidate(topLevelTypeByMetadataName, isWellKnownType))
{
return topLevelTypeByMetadataName;
}
}
ArrayBuilder<AssemblySymbol> instance = ArrayBuilder<AssemblySymbol>.GetInstance();
if (assemblyOpt != (AssemblyIdentity)null)
{
instance.AddRange(((CommonReferenceManager<CSharpCompilation, AssemblySymbol>)(object)DeclaringCompilation.GetBoundReferenceManager()).ReferencedAssemblies);
}
else
{
DeclaringCompilation.GetUnaliasedReferencedAssemblies(instance);
}
Enumerator<AssemblySymbol> enumerator = instance.GetEnumerator();
while (enumerator.MoveNext())
{
AssemblySymbol current = enumerator.Current;
if (flag2 && (object)current == CorLibrary)
{
continue;
}
NamedTypeSymbol topLevelTypeByMetadataName2 = GetTopLevelTypeByMetadataName(current, ref metadataName, assemblyOpt);
if (!isValidCandidate(topLevelTypeByMetadataName2, isWellKnownType))
{
continue;
}
if ((object)namedTypeSymbol != null)
{
if (ignoreCorLibraryDuplicatedTypes)
{
if (IsInCorLib(topLevelTypeByMetadataName2))
{
continue;
}
if (IsInCorLib(namedTypeSymbol))
{
namedTypeSymbol = topLevelTypeByMetadataName2;
continue;
}
}
if (warnings == null)
{
conflicts = (namedTypeSymbol.ContainingAssembly, topLevelTypeByMetadataName2.ContainingAssembly);
namedTypeSymbol = null;
}
else
{
warnings.Add(ErrorCode.WRN_MultiplePredefTypes, NoLocation.Singleton, namedTypeSymbol, namedTypeSymbol.ContainingAssembly);
}
break;
}
namedTypeSymbol = topLevelTypeByMetadataName2;
}
instance.Free();
return namedTypeSymbol;
bool isValidCandidate([NotNullWhen(true)] NamedTypeSymbol? candidate, bool flag3)
{
if ((object)candidate != null && (!flag3 || IsValidWellKnownType(candidate)))
{
return !candidate.IsHiddenByCodeAnalysisEmbeddedAttribute();
}
return false;
}
}
private bool IsInCorLib(NamedTypeSymbol type)
{
return (object)type.ContainingAssembly == CorLibrary;
}
private bool IsValidWellKnownType(NamedTypeSymbol? result)
{
//IL_0004: Unknown result type (might be due to invalid IL or missing references)
//IL_000a: Invalid comparison between Unknown and I4
//IL_000f: Unknown result type (might be due to invalid IL or missing references)
//IL_0015: Invalid comparison between Unknown and I4
if ((object)result == null || (int)result.TypeKind == 6)
{
return false;
}
if ((int)result.DeclaredAccessibility != 6)
{
return Symbol.IsSymbolAccessible(result, this);
}
return true;
}
private static NamedTypeSymbol? GetTopLevelTypeByMetadataName(AssemblySymbol assembly, ref MetadataTypeName metadataName, AssemblyIdentity? assemblyOpt)
{
if (assemblyOpt != (AssemblyIdentity)null && !assemblyOpt.Equals(assembly.Identity))
{
return null;
}
return assembly.LookupDeclaredTopLevelMetadataType(ref metadataName);
}
internal virtual Symbol GetDeclaredSpecialTypeMember(SpecialMember member)
{
return null;
}
internal virtual Symbol GetSpecialTypeMember(SpecialMember member)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
return CorLibrary.GetDeclaredSpecialTypeMember(member);
}
public abstract AssemblyMetadata GetMetadata();
protected override ISymbol CreateISymbol()
{
return (ISymbol)(object)new NonSourceAssemblySymbol(this);
}
}