private static void GetSortedEnumFields( INamedTypeSymbol enumType, ArrayBuilder <EnumField> enumFields ) { var underlyingSpecialType = enumType.EnumUnderlyingType.SpecialType; foreach (var member in enumType.GetMembers()) { if (member.Kind == SymbolKind.Field) { var field = (IFieldSymbol)member; if (field.HasConstantValue) { var enumField = new EnumField( field.Name, EnumUtilities.ConvertEnumUnderlyingTypeToUInt64( field.ConstantValue, underlyingSpecialType ), field ); enumFields.Add(enumField); } } } enumFields.Sort(EnumField.Comparer); }
internal void Freeze() { int wasFrozen = Interlocked.Exchange(ref _frozen, 1); if (wasFrozen != 0) { throw new InvalidOperationException(); } // Sort fields. ArrayBuilder <SynthesizedStaticField> fieldsBuilder = ArrayBuilder <SynthesizedStaticField> .GetInstance(_mappedFields.Count + (_mvidField != null ? 1 : 0)); fieldsBuilder.AddRange(_mappedFields.Values); if (_mvidField != null) { fieldsBuilder.Add(_mvidField); } fieldsBuilder.AddRange(_instrumentationPayloadRootFields.Values); fieldsBuilder.Sort(FieldComparer.Instance); _orderedSynthesizedFields = fieldsBuilder.ToImmutableAndFree(); // Sort methods. _orderedSynthesizedMethods = _synthesizedMethods.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).AsImmutable(); // Sort proxy types. _orderedProxyTypes = _proxyTypes.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value).AsImmutable(); }
public static void SortVariables( Compilation compilation, ArrayBuilder <VariableInfo> variables ) { var cancellationTokenType = compilation.GetTypeByMetadataName( typeof(CancellationToken).FullName ); variables.Sort((v1, v2) => Compare(v1, v2, cancellationTokenType)); }
public void ObfuscatedNamespaceNames_02() { var result = new ArrayBuilder <IGrouping <string, TypeDefinitionHandle> >(); foreach (var namespaceName in new[] { ".a", ".b" }) { result.Add( new Grouping <string, TypeDefinitionHandle>( namespaceName, new[] { new TypeDefinitionHandle() } ) ); } result.Sort(new PEModule.TypesByNamespaceSortComparer(StringComparer.Ordinal)); // This is equivalent to the result of PEModule.GroupTypesByNamespaceOrThrow IEnumerable <IGrouping <string, TypeDefinitionHandle> > typesByNS = result; // The following code is equivalent to code in PENamespaceSymbol.LoadAllMembers IEnumerable <IGrouping <string, TypeDefinitionHandle> > nestedTypes = null; IEnumerable < KeyValuePair <string, IEnumerable <IGrouping <string, TypeDefinitionHandle> > > > nestedNamespaces = null; MetadataHelpers.GetInfoForImmediateNamespaceMembers( true, // global namespace 0, // global namespace typesByNS, StringComparer.Ordinal, out nestedTypes, out nestedNamespaces ); var nestedNS = nestedNamespaces.Single(); Assert.Equal("", nestedNS.Key); Assert.Equal(2, nestedNS.Value.Count()); MetadataHelpers.GetInfoForImmediateNamespaceMembers( false, nestedNS.Key.Length, nestedNS.Value, StringComparer.Ordinal, out nestedTypes, out nestedNamespaces ); Assert.Equal(2, nestedNamespaces.Count()); Assert.Equal("a", nestedNamespaces.ElementAt(0).Key); Assert.Equal("b", nestedNamespaces.ElementAt(1).Key); }
public void ObfuscatedNamespaceNames_01() { var result = new ArrayBuilder <IGrouping <string, TypeDefinitionHandle> >(); foreach (var namespaceName in new[] { "A.", "A.a", "A..", "A.-" }) { result.Add( new Grouping <string, TypeDefinitionHandle>( namespaceName, new[] { new TypeDefinitionHandle() } ) ); } result.Sort(new PEModule.TypesByNamespaceSortComparer(StringComparer.Ordinal)); // This is equivalent to the result of PEModule.GroupTypesByNamespaceOrThrow IEnumerable <IGrouping <string, TypeDefinitionHandle> > typesByNS = result; // The following code is equivalent to code in PENamespaceSymbol.LoadAllMembers IEnumerable <IGrouping <string, TypeDefinitionHandle> > nestedTypes = null; IEnumerable < KeyValuePair <string, IEnumerable <IGrouping <string, TypeDefinitionHandle> > > > nestedNamespaces = null; MetadataHelpers.GetInfoForImmediateNamespaceMembers( false, "A".Length, typesByNS, StringComparer.Ordinal, out nestedTypes, out nestedNamespaces ); // We don't expect duplicate keys in nestedNamespaces at this point. Assert.False( nestedNamespaces.GroupBy(pair => pair.Key).Where(g => g.Count() > 1).Any() ); var array = nestedNamespaces.ToArray(); Assert.Equal(3, array.Length); Assert.Equal("", array[0].Key); Assert.Equal(2, array[0].Value.Count()); Assert.Equal("-", array[1].Key); Assert.Equal(1, array[1].Value.Count()); Assert.Equal("a", array[2].Key); Assert.Equal(1, array[2].Value.Count()); }
private static void FillEnumFields(ArrayBuilder <EnumField> fields, Type lmrType) { var fieldInfos = lmrType.GetFields(); var enumTypeCode = Type.GetTypeCode(lmrType); foreach (var info in fieldInfos) { if (!info.IsSpecialName) // Skip __value. { fields.Add(new EnumField(info.Name, ConvertEnumUnderlyingTypeToUInt64(info.GetRawConstantValue(), enumTypeCode))); } } fields.Sort(EnumField.Comparer); }
private ImmutableArray <StatementSyntax> Analyze() { bool badRegion = false; // only one pass is needed. Scan(ref badRegion); if (badRegion) { return(ImmutableArray <StatementSyntax> .Empty); } _branchesOutOf.Sort((x, y) => x.SpanStart - y.SpanStart); return(_branchesOutOf.ToImmutable()); }
/// <summary> /// The set of synthesized delegates created by /// this AnonymousTypeManager. /// </summary> private void GetCreatedSynthesizedDelegates(ArrayBuilder<SynthesizedDelegateSymbol> builder) { Debug.Assert(!builder.Any()); var delegates = _lazySynthesizedDelegates; if (delegates != null) { foreach (var template in delegates.Values) { if (ReferenceEquals(template.Manager, this)) { builder.Add(template.Delegate); } } builder.Sort(SynthesizedDelegateSymbolComparer.Instance); } }
public override ImmutableArray <Cci.INamespaceTypeDefinition> GetTypes(DiagnosticBag diagnostics, HashSet <string> namesOfTopLevelTypes) { if (_frozen.IsDefault) { ArrayBuilder <TEmbeddedType> builder = ArrayBuilder <TEmbeddedType> .GetInstance(); builder.AddRange(EmbeddedTypesMap.Values); builder.Sort(TypeComparer.Instance); if (ImmutableInterlocked.InterlockedInitialize(ref _frozen, builder.ToImmutableAndFree())) { if (_frozen.Length > 0) { Cci.INamespaceTypeDefinition prev = _frozen[0]; bool reportedDuplicate = HasNameConflict(namesOfTopLevelTypes, _frozen[0], diagnostics); for (int i = 1; i < _frozen.Length; i++) { Cci.INamespaceTypeDefinition current = _frozen[i]; if (prev.NamespaceName == current.NamespaceName && prev.Name == current.Name) { if (!reportedDuplicate) { Debug.Assert(_frozen[i - 1] == prev); // ERR_DuplicateLocalTypes3/ERR_InteropTypesWithSameNameAndGuid ReportNameCollisionBetweenEmbeddedTypes(_frozen[i - 1], _frozen[i], diagnostics); reportedDuplicate = true; } } else { prev = current; reportedDuplicate = HasNameConflict(namesOfTopLevelTypes, _frozen[i], diagnostics); } } OnGetTypesCompleted(_frozen, diagnostics); } } } return(StaticCast <Cci.INamespaceTypeDefinition> .From(_frozen)); }
/// <summary> /// The set of anonymous type templates created by /// this AnonymousTypeManager, in fixed order. /// </summary> private void GetCreatedAnonymousTypeTemplates(ArrayBuilder<AnonymousTypeTemplateSymbol> builder) { Debug.Assert(!builder.Any()); var anonymousTypes = _lazyAnonymousTypeTemplates; if (anonymousTypes != null) { foreach (var template in anonymousTypes.Values) { if (ReferenceEquals(template.Manager, this)) { builder.Add(template); } } // Sort type templates using smallest location builder.Sort(new AnonymousTypeComparer(this.Compilation)); } }
/// <summary> /// Gets all scopes that contain variables. /// </summary> internal ImmutableArray <Cci.LocalScope> GetAllScopesWithLocals() { ArrayBuilder <Cci.LocalScope> result = ArrayBuilder <Cci.LocalScope> .GetInstance(); ScopeBounds rootBounds = _rootScope.GetLocalScopes(result); int expectedRootScopeLength = rootBounds.End - rootBounds.Begin; // Add root scope if it was not already added. // we add it even if it does not contain any locals if (result.Count > 0 && result[result.Count - 1].Length != expectedRootScopeLength) { result.Add(new Cci.LocalScope( 0, expectedRootScopeLength, ImmutableArray <Cci.ILocalDefinition> .Empty, ImmutableArray <Cci.ILocalDefinition> .Empty)); } // scopes should be sorted by position and size result.Sort(ScopeComparer.Instance); return(result.ToImmutableAndFree()); }
private static void FillEnumFields(ArrayBuilder<EnumField> fields, Type lmrType) { var fieldInfos = lmrType.GetFields(); var enumTypeCode = Type.GetTypeCode(lmrType); foreach (var info in fieldInfos) { if (!info.IsSpecialName) // Skip __value. { fields.Add(new EnumField(info.Name, ConvertEnumUnderlyingTypeToUInt64(info.GetRawConstantValue(), enumTypeCode))); } } fields.Sort(EnumField.Comparer); }
internal static void AppendTypeMembers( this Type type, ArrayBuilder <MemberAndDeclarationInfo> includedMembers, Predicate <MemberInfo> predicate, Type declaredType, DkmClrAppDomain appDomain, bool includeInherited, bool hideNonPublic) { Debug.Assert(!type.IsInterface); var memberLocation = DeclarationInfo.FromSubTypeOfDeclaredType; var previousDeclarationMap = includeInherited ? new Dictionary <string, DeclarationInfo>() : null; int inheritanceLevel = 0; while (!type.IsObject()) { if (type.Equals(declaredType)) { Debug.Assert(memberLocation == DeclarationInfo.FromSubTypeOfDeclaredType); memberLocation = DeclarationInfo.FromDeclaredTypeOrBase; } // Get the state from DebuggerBrowsableAttributes for the members of the current type. var browsableState = DkmClrType.Create(appDomain, type).GetDebuggerBrowsableAttributeState(); // Hide non-public members if hideNonPublic is specified (intended to reflect the // DkmInspectionContext's DkmEvaluationFlags), and the type is from an assembly // with no symbols. var hideNonPublicBehavior = DeclarationInfo.None; if (hideNonPublic) { var moduleInstance = appDomain.FindClrModuleInstance(type.Module.ModuleVersionId); if (moduleInstance == null || moduleInstance.Module == null) { // Synthetic module or no symbols loaded. hideNonPublicBehavior = DeclarationInfo.HideNonPublic; } } foreach (var member in type.GetMembers(MemberBindingFlags)) { if (!predicate(member)) { continue; } var memberName = member.Name; // This represents information about the immediately preceding (more derived) // declaration with the same name as the current member. var previousDeclaration = DeclarationInfo.None; var memberNameAlreadySeen = false; if (includeInherited) { memberNameAlreadySeen = previousDeclarationMap.TryGetValue(memberName, out previousDeclaration); if (memberNameAlreadySeen) { // There was a name conflict, so we'll need to include the declaring // type of the member to disambiguate. previousDeclaration |= DeclarationInfo.IncludeTypeInMemberName; } // Update previous member with name hiding (casting) and declared location information for next time. previousDeclarationMap[memberName] = (previousDeclaration & ~(DeclarationInfo.RequiresExplicitCast | DeclarationInfo.FromSubTypeOfDeclaredType)) | member.AccessingBaseMemberWithSameNameRequiresExplicitCast() | memberLocation; } Debug.Assert(memberNameAlreadySeen != (previousDeclaration == DeclarationInfo.None)); // Decide whether to include this member in the list of members to display. if (!memberNameAlreadySeen || previousDeclaration.IsSet(DeclarationInfo.RequiresExplicitCast)) { DkmClrDebuggerBrowsableAttributeState?browsableStateValue = null; if (browsableState != null) { DkmClrDebuggerBrowsableAttributeState value; if (browsableState.TryGetValue(memberName, out value)) { browsableStateValue = value; } } if (memberLocation.IsSet(DeclarationInfo.FromSubTypeOfDeclaredType)) { // If the current type is a sub-type of the declared type, then // we always need to insert a cast to access the member previousDeclaration |= DeclarationInfo.RequiresExplicitCast; } else if (previousDeclaration.IsSet(DeclarationInfo.FromSubTypeOfDeclaredType)) { // If the immediately preceding member (less derived) was // declared on a sub-type of the declared type, then we'll // ignore the casting bit. Accessing a member through the // declared type is the same as casting to that type, so // the cast would be redundant. previousDeclaration &= ~DeclarationInfo.RequiresExplicitCast; } previousDeclaration |= hideNonPublicBehavior; includedMembers.Add(new MemberAndDeclarationInfo(member, browsableStateValue, previousDeclaration, inheritanceLevel)); } } if (!includeInherited) { break; } type = type.BaseType; inheritanceLevel++; } includedMembers.Sort(MemberAndDeclarationInfo.Comparer); }
public void ObfuscatedNamespaceNames_02() { var result = new ArrayBuilder<IGrouping<string, TypeDefinitionHandle>>(); foreach (var namespaceName in new[] { ".a", ".b" }) { result.Add(new Grouping<string, TypeDefinitionHandle>(namespaceName, new[] { new TypeDefinitionHandle() })); } result.Sort(new PEModule.TypesByNamespaceSortComparer(StringComparer.Ordinal)); // This is equivalent to the result of PEModule.GroupTypesByNamespaceOrThrow IEnumerable<IGrouping<string, TypeDefinitionHandle>> typesByNS = result; // The following code is equivalent to code in PENamespaceSymbol.LoadAllMembers IEnumerable<IGrouping<string, TypeDefinitionHandle>> nestedTypes = null; IEnumerable<KeyValuePair<string, IEnumerable<IGrouping<string, TypeDefinitionHandle>>>> nestedNamespaces = null; MetadataHelpers.GetInfoForImmediateNamespaceMembers( true, // global namespace 0, // global namespace typesByNS, StringComparer.Ordinal, out nestedTypes, out nestedNamespaces); var nestedNS = nestedNamespaces.Single(); Assert.Equal("", nestedNS.Key); Assert.Equal(2, nestedNS.Value.Count()); MetadataHelpers.GetInfoForImmediateNamespaceMembers( false, nestedNS.Key.Length, nestedNS.Value, StringComparer.Ordinal, out nestedTypes, out nestedNamespaces); Assert.Equal(2, nestedNamespaces.Count()); Assert.Equal("a", nestedNamespaces.ElementAt(0).Key); Assert.Equal("b", nestedNamespaces.ElementAt(1).Key); }
public void ObfuscatedNamespaceNames_01() { var result = new ArrayBuilder<IGrouping<string, TypeDefinitionHandle>>(); foreach (var namespaceName in new[] { "A.", "A.a", "A..", "A.-" }) { result.Add(new Grouping<string, TypeDefinitionHandle>(namespaceName, new[] { new TypeDefinitionHandle() })); } result.Sort(new PEModule.TypesByNamespaceSortComparer(StringComparer.Ordinal)); // This is equivalent to the result of PEModule.GroupTypesByNamespaceOrThrow IEnumerable<IGrouping<string, TypeDefinitionHandle>> typesByNS = result; // The following code is equivalent to code in PENamespaceSymbol.LoadAllMembers IEnumerable<IGrouping<string, TypeDefinitionHandle>> nestedTypes = null; IEnumerable<KeyValuePair<string, IEnumerable<IGrouping<string, TypeDefinitionHandle>>>> nestedNamespaces = null; MetadataHelpers.GetInfoForImmediateNamespaceMembers( false, "A".Length, typesByNS, StringComparer.Ordinal, out nestedTypes, out nestedNamespaces); // We don't expect duplicate keys in nestedNamespaces at this point. Assert.False(nestedNamespaces.GroupBy(pair => pair.Key).Where(g => g.Count() > 1).Any()); var array = nestedNamespaces.ToArray(); Assert.Equal(3, array.Length); Assert.Equal("", array[0].Key); Assert.Equal(2, array[0].Value.Count()); Assert.Equal("-", array[1].Key); Assert.Equal(1, array[1].Value.Count()); Assert.Equal("a", array[2].Key); Assert.Equal(1, array[2].Value.Count()); }
private static void GetSortedEnumFields( INamedTypeSymbol enumType, ArrayBuilder<EnumField> enumFields) { var underlyingSpecialType = enumType.EnumUnderlyingType.SpecialType; foreach (var member in enumType.GetMembers()) { if (member.Kind == SymbolKind.Field) { var field = (IFieldSymbol)member; if (field.HasConstantValue) { var enumField = new EnumField(field.Name, EnumUtilities.ConvertEnumUnderlyingTypeToUInt64(field.ConstantValue, underlyingSpecialType), field); enumFields.Add(enumField); } } } enumFields.Sort(EnumField.Comparer); }