Files
2026-08-27 10:56:38 -06:00

552 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Symbols;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols;
internal sealed class SourceModuleSymbol : NonMissingModuleSymbol, IAttributeTargetSymbol
{
private readonly SourceAssemblySymbol _assemblySymbol;
private ImmutableArray<AssemblySymbol> _lazyAssembliesToEmbedTypesFrom;
private ThreeState _lazyContainsExplicitDefinitionOfNoPiaLocalTypes;
private readonly DeclarationTable _sources;
private SymbolCompletionState _state;
private CustomAttributesBag<CSharpAttributeData> _lazyCustomAttributesBag;
private ImmutableArray<Location> _locations;
private NamespaceSymbol _globalNamespace;
private bool _hasBadAttributes;
private ThreeState _lazyUseUpdatedEscapeRules;
private ThreeState _lazyRequiresRefSafetyRulesAttribute;
private readonly string _name;
internal bool HasBadAttributes => _hasBadAttributes;
internal override int Ordinal => 0;
internal override Machine Machine
{
get
{
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_0011: Unknown result type (might be due to invalid IL or missing references)
//IL_0013: Unknown result type (might be due to invalid IL or missing references)
//IL_002d: Expected I4, but got Unknown
Platform platform = ((CompilationOptions)DeclaringCompilation.Options).Platform;
return (platform - 2) switch
{
3 => Machine.ArmThumb2,
0 => Machine.Amd64,
4 => Machine.Arm64,
1 => Machine.IA64,
_ => Machine.I386,
};
}
}
internal override bool Bit32Required => (int)((CompilationOptions)DeclaringCompilation.Options).Platform == 1;
internal bool AnyReferencedAssembliesAreLinked => GetAssembliesToEmbedTypesFrom().Length > 0;
internal bool ContainsExplicitDefinitionOfNoPiaLocalTypes
{
get
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0025: Invalid comparison between Unknown and I4
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_0019: Unknown result type (might be due to invalid IL or missing references)
if ((int)_lazyContainsExplicitDefinitionOfNoPiaLocalTypes == 0)
{
_lazyContainsExplicitDefinitionOfNoPiaLocalTypes = ThreeStateHelpers.ToThreeState(NamespaceContainsExplicitDefinitionOfNoPiaLocalTypes(GlobalNamespace));
}
return (int)_lazyContainsExplicitDefinitionOfNoPiaLocalTypes == 2;
}
}
public override NamespaceSymbol GlobalNamespace
{
get
{
if ((object)_globalNamespace == null)
{
BindingDiagnosticBag instance = BindingDiagnosticBag.GetInstance();
SourceNamespaceSymbol value = new SourceNamespaceSymbol(this, this, DeclaringCompilation.MergedRootDeclaration, instance);
if (Interlocked.CompareExchange(ref _globalNamespace, value, null) == null)
{
AddDeclarationDiagnostics(instance);
}
((BindingDiagnosticBag<AssemblySymbol>)(object)instance).Free();
}
return _globalNamespace;
}
}
internal sealed override bool RequiresCompletion => true;
public override ImmutableArray<Location> Locations
{
get
{
if (_locations.IsDefault)
{
ImmutableInterlocked.InterlockedInitialize(ref _locations, ImmutableArrayExtensions.SelectAsArray<SingleNamespaceDeclaration, Location>(DeclaringCompilation.MergedRootDeclaration.Declarations, (Func<SingleNamespaceDeclaration, Location>)((SingleNamespaceDeclaration d) => (Location)(object)d.Location)));
}
return _locations;
}
}
public override string Name => _name;
public override Symbol ContainingSymbol => _assemblySymbol;
public override AssemblySymbol ContainingAssembly => _assemblySymbol;
internal SourceAssemblySymbol ContainingSourceAssembly => _assemblySymbol;
internal override CSharpCompilation DeclaringCompilation => _assemblySymbol.DeclaringCompilation;
internal override ICollection<string> TypeNames => _sources.TypeNames;
internal override ICollection<string> NamespaceNames => _sources.NamespaceNames;
IAttributeTargetSymbol IAttributeTargetSymbol.AttributesOwner => _assemblySymbol;
AttributeLocation IAttributeTargetSymbol.DefaultAttributeLocation => AttributeLocation.Module;
AttributeLocation IAttributeTargetSymbol.AllowedAttributeLocations
{
get
{
if (!ContainingAssembly.IsInteractive)
{
return AttributeLocation.Assembly | AttributeLocation.Module;
}
return AttributeLocation.None;
}
}
internal override bool HasAssemblyCompilationRelaxationsAttribute => ((SourceAssemblySymbol)ContainingAssembly).GetSourceDecodedWellKnownAttributeData()?.HasCompilationRelaxationsAttribute ?? false;
internal override bool HasAssemblyRuntimeCompatibilityAttribute => ((SourceAssemblySymbol)ContainingAssembly).GetSourceDecodedWellKnownAttributeData()?.HasRuntimeCompatibilityAttribute ?? false;
internal override CharSet? DefaultMarshallingCharSet
{
get
{
ModuleWellKnownAttributeData decodedWellKnownAttributeData = GetDecodedWellKnownAttributeData();
if (decodedWellKnownAttributeData == null || !((CommonModuleWellKnownAttributeData)decodedWellKnownAttributeData).HasDefaultCharSetAttribute)
{
return null;
}
return ((CommonModuleWellKnownAttributeData)decodedWellKnownAttributeData).DefaultCharacterSet;
}
}
public sealed override bool AreLocalsZeroed
{
get
{
ModuleWellKnownAttributeData decodedWellKnownAttributeData = GetDecodedWellKnownAttributeData();
if (decodedWellKnownAttributeData == null)
{
return true;
}
return !decodedWellKnownAttributeData.HasSkipLocalsInitAttribute;
}
}
internal override bool UseUpdatedEscapeRules
{
get
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_003b: Unknown result type (might be due to invalid IL or missing references)
//IL_0041: Invalid comparison between Unknown and I4
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
if ((int)_lazyUseUpdatedEscapeRules == 0)
{
bool flag = _assemblySymbol.DeclaringCompilation.IsFeatureEnabled(MessageID.IDS_FeatureRefFields) || _assemblySymbol.RuntimeSupportsByRefFields;
_lazyUseUpdatedEscapeRules = ThreeStateHelpers.ToThreeState(flag);
}
return (int)_lazyUseUpdatedEscapeRules == 2;
}
}
internal sealed override ObsoleteAttributeData? ObsoleteAttributeData
{
get
{
CustomAttributesBag<CSharpAttributeData> lazyCustomAttributesBag = _lazyCustomAttributesBag;
if (lazyCustomAttributesBag != null && lazyCustomAttributesBag.IsDecodedWellKnownAttributeDataComputed)
{
ModuleWellKnownAttributeData obj = (ModuleWellKnownAttributeData)(object)lazyCustomAttributesBag.DecodedWellKnownAttributeData;
if (obj == null)
{
return null;
}
return ((CommonModuleWellKnownAttributeData)obj).ExperimentalAttributeData;
}
if (((SourceAssemblySymbol)ContainingAssembly).GetAttributeDeclarations().IsEmpty)
{
return null;
}
return ObsoleteAttributeData.Uninitialized;
}
}
internal SourceModuleSymbol(SourceAssemblySymbol assemblySymbol, DeclarationTable declarations, string moduleName)
{
_assemblySymbol = assemblySymbol;
_sources = declarations;
_name = moduleName;
}
internal void RecordPresenceOfBadAttributes()
{
_hasBadAttributes = true;
}
internal bool MightContainNoPiaLocalTypes()
{
if (!AnyReferencedAssembliesAreLinked)
{
return ContainsExplicitDefinitionOfNoPiaLocalTypes;
}
return true;
}
internal ImmutableArray<AssemblySymbol> GetAssembliesToEmbedTypesFrom()
{
if (_lazyAssembliesToEmbedTypesFrom.IsDefault)
{
ArrayBuilder<AssemblySymbol> instance = ArrayBuilder<AssemblySymbol>.GetInstance();
ImmutableArray<AssemblySymbol>.Enumerator enumerator = GetReferencedAssemblySymbols().GetEnumerator();
while (enumerator.MoveNext())
{
AssemblySymbol current = enumerator.Current;
if (current.IsLinked)
{
instance.Add(current);
}
}
ImmutableInterlocked.InterlockedCompareExchange(ref _lazyAssembliesToEmbedTypesFrom, instance.ToImmutableAndFree(), default(ImmutableArray<AssemblySymbol>));
}
return _lazyAssembliesToEmbedTypesFrom;
}
private static bool NamespaceContainsExplicitDefinitionOfNoPiaLocalTypes(NamespaceSymbol ns)
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_0023: Invalid comparison between Unknown and I4
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_0028: Invalid comparison between Unknown and I4
ImmutableArray<Symbol>.Enumerator enumerator = ns.GetMembersUnordered().GetEnumerator();
while (enumerator.MoveNext())
{
Symbol current = enumerator.Current;
SymbolKind kind = current.Kind;
if ((int)kind != 11)
{
if ((int)kind == 12 && NamespaceContainsExplicitDefinitionOfNoPiaLocalTypes((NamespaceSymbol)current))
{
return true;
}
}
else if (((NamedTypeSymbol)current).IsExplicitDefinitionOfNoPiaLocalType)
{
return true;
}
}
return false;
}
internal sealed override bool HasComplete(CompletionPart part)
{
return _state.HasComplete(part);
}
internal override void ForceComplete(SourceLocation locationOpt, CancellationToken cancellationToken)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
CompletionPart nextIncompletePart = _state.NextIncompletePart;
switch (nextIncompletePart)
{
case CompletionPart.Attributes:
GetAttributes();
break;
case CompletionPart.StartBaseType:
{
BindingDiagnosticBag bindingDiagnosticBag = null;
if (AnyReferencedAssembliesAreLinked)
{
bindingDiagnosticBag = BindingDiagnosticBag.GetInstance();
ValidateLinkedAssemblies(bindingDiagnosticBag, cancellationToken);
}
if (_state.NotePartComplete(CompletionPart.StartBaseType))
{
if (bindingDiagnosticBag != null)
{
_assemblySymbol.AddDeclarationDiagnostics(bindingDiagnosticBag);
}
_state.NotePartComplete(CompletionPart.FinishBaseType);
}
((BindingDiagnosticBag<AssemblySymbol>)(object)bindingDiagnosticBag)?.Free();
break;
}
case CompletionPart.FinishBaseType:
_state.SpinWaitComplete(CompletionPart.FinishBaseType, cancellationToken);
break;
case CompletionPart.MembersCompleted:
GlobalNamespace.ForceComplete(locationOpt, cancellationToken);
if (GlobalNamespace.HasComplete(CompletionPart.MembersCompleted))
{
_state.NotePartComplete(CompletionPart.MembersCompleted);
break;
}
return;
case CompletionPart.None:
return;
default:
_state.NotePartComplete(nextIncompletePart);
break;
}
_state.SpinWaitComplete(nextIncompletePart, cancellationToken);
}
}
private void ValidateLinkedAssemblies(BindingDiagnosticBag diagnostics, CancellationToken cancellationToken)
{
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
//IL_007b: Unknown result type (might be due to invalid IL or missing references)
//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
//IL_0120: Unknown result type (might be due to invalid IL or missing references)
//IL_0125: Unknown result type (might be due to invalid IL or missing references)
//IL_0131: Unknown result type (might be due to invalid IL or missing references)
//IL_0136: Unknown result type (might be due to invalid IL or missing references)
ImmutableArray<AssemblySymbol>.Enumerator enumerator = GetReferencedAssemblySymbols().GetEnumerator();
string text = default(string);
while (enumerator.MoveNext())
{
AssemblySymbol current = enumerator.Current;
cancellationToken.ThrowIfCancellationRequested();
if (current.IsMissing || !current.IsLinked)
{
continue;
}
bool flag = false;
bool flag2 = false;
ImmutableArray<CSharpAttributeData>.Enumerator enumerator2 = current.GetAttributes().GetEnumerator();
while (enumerator2.MoveNext())
{
CSharpAttributeData current2 = enumerator2.Current;
if (current2.IsTargetAttribute(current, AttributeDescription.GuidAttribute))
{
if (CommonAttributeDataExtensions.TryGetGuidAttributeValue((AttributeData)(object)current2, ref text))
{
flag = true;
}
}
else if (current2.IsTargetAttribute(current, AttributeDescription.ImportedFromTypeLibAttribute))
{
if (((AttributeData)current2).CommonConstructorArguments.Length == 1)
{
flag2 = true;
}
}
else if (current2.IsTargetAttribute(current, AttributeDescription.PrimaryInteropAssemblyAttribute) && ((AttributeData)current2).CommonConstructorArguments.Length == 2)
{
flag2 = true;
}
if (flag && flag2)
{
break;
}
}
AttributeDescription val;
if (!flag)
{
Location singleton = NoLocation.Singleton;
object[] obj = new object[2] { current, null };
val = AttributeDescription.GuidAttribute;
obj[1] = ((AttributeDescription)(ref val)).FullName;
diagnostics.Add(ErrorCode.ERR_NoPIAAssemblyMissingAttribute, singleton, obj);
}
if (!flag2)
{
Location singleton2 = NoLocation.Singleton;
object[] obj2 = new object[3] { current, null, null };
val = AttributeDescription.ImportedFromTypeLibAttribute;
obj2[1] = ((AttributeDescription)(ref val)).FullName;
val = AttributeDescription.PrimaryInteropAssemblyAttribute;
obj2[2] = ((AttributeDescription)(ref val)).FullName;
diagnostics.Add(ErrorCode.ERR_NoPIAAssemblyMissingAttributes, singleton2, obj2);
}
}
}
private CustomAttributesBag<CSharpAttributeData> GetAttributesBag()
{
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
if (_lazyCustomAttributesBag == null || !_lazyCustomAttributesBag.IsSealed)
{
ImmutableArray<SyntaxList<AttributeListSyntax>> attributeDeclarations = ((SourceAssemblySymbol)ContainingAssembly).GetAttributeDeclarations();
if (LoadAndValidateAttributes(OneOrMany.Create<SyntaxList<AttributeListSyntax>>(attributeDeclarations), ref _lazyCustomAttributesBag))
{
_state.NotePartComplete(CompletionPart.Attributes);
}
}
return _lazyCustomAttributesBag;
}
public sealed override ImmutableArray<CSharpAttributeData> GetAttributes()
{
return GetAttributesBag().Attributes;
}
private ModuleWellKnownAttributeData GetDecodedWellKnownAttributeData()
{
CustomAttributesBag<CSharpAttributeData> val = _lazyCustomAttributesBag;
if (val == null || !val.IsDecodedWellKnownAttributeDataComputed)
{
val = GetAttributesBag();
}
return (ModuleWellKnownAttributeData)(object)val.DecodedWellKnownAttributeData;
}
protected override void DecodeWellKnownAttributeImpl(ref DecodeWellKnownAttributeArguments<AttributeSyntax, CSharpAttributeData, AttributeLocation> arguments)
{
//IL_0009: Unknown result type (might be due to invalid IL or missing references)
//IL_0082: Unknown result type (might be due to invalid IL or missing references)
//IL_009d: Unknown result type (might be due to invalid IL or missing references)
CSharpAttributeData attribute = arguments.Attribute;
if (attribute.IsTargetAttribute(this, AttributeDescription.DefaultCharSetAttribute))
{
CharSet constructorArgument = ((AttributeData)attribute).GetConstructorArgument<CharSet>(0, (SpecialType)2);
if (!CommonModuleWellKnownAttributeData.IsValidCharSet(constructorArgument))
{
CSharpSyntaxNode attributeArgumentSyntax = ((AttributeData)(object)attribute).GetAttributeArgumentSyntax(0, arguments.AttributeSyntaxOpt);
((BindingDiagnosticBag)(object)arguments.Diagnostics).Add(ErrorCode.ERR_InvalidAttributeArgument, ((SyntaxNode)attributeArgumentSyntax).Location, arguments.AttributeSyntaxOpt.GetErrorDisplayName());
}
else
{
((CommonModuleWellKnownAttributeData)arguments.GetOrCreateData<ModuleWellKnownAttributeData>()).DefaultCharacterSet = constructorArgument;
}
}
else if (!ReportExplicitUseOfReservedAttributes(in arguments, ReservedAttributes.NullableContextAttribute | ReservedAttributes.NullablePublicOnlyAttribute | ReservedAttributes.RefSafetyRulesAttribute))
{
if (attribute.IsTargetAttribute(this, AttributeDescription.SkipLocalsInitAttribute))
{
CSharpAttributeData.DecodeSkipLocalsInitAttribute<ModuleWellKnownAttributeData>(DeclaringCompilation, ref arguments);
}
else if (attribute.IsTargetAttribute(this, AttributeDescription.ExperimentalAttribute))
{
((CommonModuleWellKnownAttributeData)arguments.GetOrCreateData<ModuleWellKnownAttributeData>()).ExperimentalAttributeData = ((AttributeData)attribute).DecodeExperimentalAttribute();
}
}
}
internal bool RequiresRefSafetyRulesAttribute()
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
//IL_0033: Unknown result type (might be due to invalid IL or missing references)
//IL_0038: Unknown result type (might be due to invalid IL or missing references)
if ((int)_lazyRequiresRefSafetyRulesAttribute == 0)
{
bool flag = UseUpdatedEscapeRules && !isFeatureDisabled(_assemblySymbol.DeclaringCompilation) && namespaceIncludesTypeDeclarations(GlobalNamespace);
_lazyRequiresRefSafetyRulesAttribute = ThreeStateHelpers.ToThreeState(flag);
}
return ThreeStateHelpers.Value(_lazyRequiresRefSafetyRulesAttribute);
static bool isFeatureDisabled(CSharpCompilation compilation)
{
SyntaxTree? obj = compilation.SyntaxTrees.FirstOrDefault();
CSharpParseOptions obj2 = (CSharpParseOptions)(object)((obj != null) ? obj.Options : null);
if (obj2 == null)
{
return false;
}
return ((ParseOptions)obj2).Features?.ContainsKey("noRefSafetyRulesAttribute") == true;
}
static bool namespaceIncludesTypeDeclarations(NamespaceSymbol ns)
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_0023: Invalid comparison between Unknown and I4
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_0028: Invalid comparison between Unknown and I4
ImmutableArray<Symbol>.Enumerator enumerator = ns.GetMembersUnordered().GetEnumerator();
while (enumerator.MoveNext())
{
Symbol current = enumerator.Current;
SymbolKind kind = current.Kind;
if ((int)kind == 11)
{
return true;
}
if ((int)kind == 12 && namespaceIncludesTypeDeclarations((NamespaceSymbol)current))
{
return true;
}
}
return false;
}
}
internal override void AddSynthesizedAttributes(PEModuleBuilder moduleBuilder, ref ArrayBuilder<SynthesizedAttributeData> attributes)
{
//IL_006f: Unknown result type (might be due to invalid IL or missing references)
//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
base.AddSynthesizedAttributes(moduleBuilder, ref attributes);
CSharpCompilation declaringCompilation = _assemblySymbol.DeclaringCompilation;
if (declaringCompilation.Options.AllowUnsafe && !(declaringCompilation.GetWellKnownType((WellKnownType)236) is MissingMetadataTypeSymbol))
{
Symbol.AddSynthesizedAttribute(ref attributes, declaringCompilation.TrySynthesizeAttribute((WellKnownMember)134));
}
if (RequiresRefSafetyRulesAttribute())
{
ImmutableArray<TypedConstant> arguments = ImmutableArray.Create<TypedConstant>(new TypedConstant((ITypeSymbolInternal)(object)declaringCompilation.GetSpecialType((SpecialType)13), (TypedConstantKind)1, (object)11));
Symbol.AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeRefSafetyRulesAttribute(arguments));
}
if (moduleBuilder.ShouldEmitNullablePublicOnlyAttribute())
{
ImmutableArray<TypedConstant> arguments2 = ImmutableArray.Create<TypedConstant>(new TypedConstant((ITypeSymbolInternal)(object)declaringCompilation.GetSpecialType((SpecialType)7), (TypedConstantKind)1, (object)_assemblySymbol.InternalsAreVisible));
Symbol.AddSynthesizedAttribute(ref attributes, moduleBuilder.SynthesizeNullablePublicOnlyAttribute(arguments2));
}
}
public override ModuleMetadata? GetMetadata()
{
return null;
}
}