public override void VisitNamedType(NamedTypeSymbol symbol) { _cancellationToken.ThrowIfCancellationRequested(); var sourceTypeSymbol = symbol as SourceMemberContainerTypeSymbol; if ((object)sourceTypeSymbol != null) { if (_moduleBeingBuilt != null) { // In some circumstances (e.g. implicit implementation of an interface method by a non-virtual method in a // base type from another assembly) it is necessary for the compiler to generate explicit implementations for // some interface methods. They don't go in the symbol table, but if we are emitting metadata, then we should // generate MethodDef entries for them. foreach (var synthesizedExplicitImpl in sourceTypeSymbol.GetSynthesizedExplicitImplementations(_cancellationToken)) { _moduleBeingBuilt.AddSynthesizedDefinition(symbol, synthesizedExplicitImpl); } } } foreach (Symbol member in symbol.GetMembers()) { switch (member.Kind) { case SymbolKind.Property: case SymbolKind.NamedType: member.Accept(this); break; } } }
// This method checks if the given PermissionSetAttribute type has a property member with the given propName which is writable, non-generic, public and of string type. private static bool PermissionSetAttributeTypeHasRequiredProperty(NamedTypeSymbol permissionSetType, string propName) { var members = permissionSetType.GetMembers(propName); if (members.Length == 1 && members[0].Kind == SymbolKind.Property) { var property = (PropertySymbol)members[0]; if (property.TypeWithAnnotations.HasType && property.Type.SpecialType == SpecialType.System_String && property.DeclaredAccessibility == Accessibility.Public && property.GetMemberArity() == 0 && (object)property.SetMethod != null && property.SetMethod.DeclaredAccessibility == Accessibility.Public) { return(true); } } return(false); }
private static XElement LoadChildType(NamedTypeSymbol t) { XElement elem = new XElement("type"); elem.Add(new XAttribute("name", t.Name)); if (t.Arity > 0) { string typeParams = string.Empty; foreach (var param in t.TypeParameters) { if (typeParams.Length > 0) { typeParams += ","; } typeParams += param.Name; } elem.Add(new XAttribute("Of", typeParams)); } if (t.BaseType != null) { elem.Add(new XAttribute("base", t.BaseType.ToTestDisplayString())); } var fields = t.GetMembers().Where(m => m.Kind == SymbolKind.Field).OrderBy(f => f.Name).Cast<FieldSymbol>(); elem.Add(from f in fields select LoadField(f)); var childrenTypes = t.GetTypeMembers().OrderBy(c => c, new NameAndArityComparer()); elem.Add(from c in childrenTypes select LoadChildType(c)); return elem; }
private static void CheckInnerClassHelper(NamedTypeSymbol innerClass, string methodName, Symbol interfaceMethod) { var @interface = interfaceMethod.ContainingType; Assert.Equal(1, innerClass.Arity); Assert.Equal(TypeKind.Class, innerClass.TypeKind); Assert.Equal(@interface, innerClass.Interfaces.Single().ConstructedFrom); var innerClassMethod = (MethodSymbol)innerClass.GetMembers(methodName).Single(); var innerClassImplementingMethod = innerClassMethod.ExplicitInterfaceImplementations.Single(); Assert.Equal(interfaceMethod, innerClassImplementingMethod.OriginalDefinition); Assert.Equal(@interface, innerClassImplementingMethod.ContainingType.ConstructedFrom); }
public Test01() { string source = @" using System.Diagnostics; [assembly: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] [module: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] class TestClass { [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] public int testField; [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] public int TestProperty { [return: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] get { return testField; } } [return: DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] [DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] public T TestMethod<[DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] T> ([DebuggerTypeProxyAttribute(typeof(System.Type), Target = typeof(int[]), TargetTypeName = ""IntArrayType"" )] T testParameter) { return testParameter; } }"; var compilation1 = CSharpCompilation.Create("C1", new[] { Parse(source) }, new[] { OldMsCorLib }, TestOptions.ReleaseDll); c1 = new CSharpCompilationReference(compilation1); var c1Assembly = compilation1.Assembly; var compilation2 = CSharpCompilation.Create("C2", references: new MetadataReference[] { NewMsCorLib, c1 }); c2 = new CSharpCompilationReference(compilation2); var c1AsmRef = compilation2.GetReferencedAssemblySymbol(c1); Assert.NotSame(c1Assembly, c1AsmRef); c1MscorLibAssemblyRef = compilation1.GetReferencedAssemblySymbol(OldMsCorLib); c2MscorlibAssemblyRef = compilation2.GetReferencedAssemblySymbol(NewMsCorLib); Assert.NotSame(c1MscorLibAssemblyRef, c2MscorlibAssemblyRef); oldMsCorLib_systemType = c1MscorLibAssemblyRef.GetTypeByMetadataName("System.Type"); newMsCorLib_systemType = c2MscorlibAssemblyRef.GetTypeByMetadataName("System.Type"); Assert.NotSame(oldMsCorLib_systemType, newMsCorLib_systemType); oldMsCorLib_debuggerTypeProxyAttributeType = c1MscorLibAssemblyRef.GetTypeByMetadataName("System.Diagnostics.DebuggerTypeProxyAttribute"); newMsCorLib_debuggerTypeProxyAttributeType = c2MscorlibAssemblyRef.GetTypeByMetadataName("System.Diagnostics.DebuggerTypeProxyAttribute"); Assert.NotSame(oldMsCorLib_debuggerTypeProxyAttributeType, newMsCorLib_debuggerTypeProxyAttributeType); oldMsCorLib_debuggerTypeProxyAttributeCtor = (MethodSymbol)oldMsCorLib_debuggerTypeProxyAttributeType.GetMembers(".ctor").Single( m => ((MethodSymbol)m).ParameterCount == 1 && ((MethodSymbol)m).ParameterTypes[0] == oldMsCorLib_systemType); newMsCorLib_debuggerTypeProxyAttributeCtor = (MethodSymbol)newMsCorLib_debuggerTypeProxyAttributeType.GetMembers(".ctor").Single( m => ((MethodSymbol)m).ParameterCount == 1 && ((MethodSymbol)m).ParameterTypes[0] == newMsCorLib_systemType); Assert.NotSame(oldMsCorLib_debuggerTypeProxyAttributeCtor, newMsCorLib_debuggerTypeProxyAttributeCtor); }
private static bool HasSynthesizedStaticConstructor(NamedTypeSymbol typeSymbol) { foreach (var member in typeSymbol.GetMembers(WellKnownMemberNames.StaticConstructorName)) { if (member.IsImplicitlyDeclared) { return true; } } return false; }
/// <summary> /// Write own documentation comments and then descend into members. /// </summary> public override void VisitNamedType(NamedTypeSymbol symbol) { _cancellationToken.ThrowIfCancellationRequested(); if (_filterTree != null && !symbol.IsDefinedInSourceTree(_filterTree, _filterSpanWithinTree)) { return; } DefaultVisit(symbol); if (!_isForSingleSymbol) { foreach (Symbol member in symbol.GetMembers()) { _cancellationToken.ThrowIfCancellationRequested(); member.Accept(this); } } }
private static void FindExplicitImplementationCollisions( Symbol implementingMember, Symbol implementedMember, BindingDiagnosticBag diagnostics ) { if ((object)implementedMember == null) { return; } NamedTypeSymbol explicitInterfaceType = implementedMember.ContainingType; bool explicitInterfaceTypeIsDefinition = explicitInterfaceType.IsDefinition; //no runtime ref/out ambiguities if this is true foreach ( Symbol collisionCandidateMember in explicitInterfaceType.GetMembers( implementedMember.Name ) ) { if ( collisionCandidateMember.Kind == implementingMember.Kind && implementedMember != collisionCandidateMember ) { // NOTE: we are more precise than Dev10 - we will not generate a diagnostic if the return types differ // because that is enough to distinguish them in the runtime. if ( !explicitInterfaceTypeIsDefinition && MemberSignatureComparer.RuntimeSignatureComparer.Equals( implementedMember, collisionCandidateMember ) ) { bool foundMismatchedRefKind = false; ImmutableArray <ParameterSymbol> implementedMemberParameters = implementedMember.GetParameters(); ImmutableArray <ParameterSymbol> collisionCandidateParameters = collisionCandidateMember.GetParameters(); int numParams = implementedMemberParameters.Length; for (int i = 0; i < numParams; i++) { if ( implementedMemberParameters[i].RefKind != collisionCandidateParameters[i].RefKind ) { foundMismatchedRefKind = true; break; } } if (foundMismatchedRefKind) { diagnostics.Add( ErrorCode.ERR_ExplicitImplCollisionOnRefOut, explicitInterfaceType.Locations[0], explicitInterfaceType, implementedMember ); } else { //UNDONE: related locations for conflicting members - keep iterating to find others? diagnostics.Add( ErrorCode.WRN_ExplicitImplCollision, implementingMember.Locations[0], implementingMember ); } break; } else { if ( MemberSignatureComparer.ExplicitImplementationComparer.Equals( implementedMember, collisionCandidateMember ) ) { // NOTE: this is different from the same error code above. Above, the diagnostic means that // the runtime behavior is ambiguous because the runtime cannot distinguish between two or // more interface members. This diagnostic means that *C#* cannot distinguish between two // or more interface members (because of custom modifiers). diagnostics.Add( ErrorCode.WRN_ExplicitImplCollision, implementingMember.Locations[0], implementingMember ); } } } } }
private void CheckPrivateMembers(NamedTypeSymbol type, bool isFromSource, bool importPrivates) { Symbol member; member = type.GetMembers("Public").SingleOrDefault(); Assert.NotNull(member); member = type.GetMembers("Internal").SingleOrDefault(); Assert.NotNull(member); member = type.GetMembers("Protected").SingleOrDefault(); Assert.NotNull(member); member = type.GetMembers("ProtectedInternal").SingleOrDefault(); Assert.NotNull(member); member = type.GetMembers("Private").SingleOrDefault(); if (isFromSource || importPrivates) { Assert.NotNull(member); } else { Assert.Null(member); } }
private void CheckConstantField(NamedTypeSymbol type, string name, Accessibility declaredAccessibility, SpecialType fieldType, object value) { var field = type.GetMembers(name).SingleOrDefault() as FieldSymbol; Assert.NotNull(field); Assert.True(field.IsStatic); Assert.True(field.IsConst); Assert.Equal(field.DeclaredAccessibility, declaredAccessibility); Assert.Equal(field.Type.SpecialType, fieldType); Assert.Equal(field.ConstantValue, value); }
private void CheckInternalMembers(NamedTypeSymbol type, bool isFromSource) { Assert.NotNull(type.GetMembers("Public").SingleOrDefault()); var member = type.GetMembers("Internal").SingleOrDefault(); if (isFromSource) Assert.NotNull(member); else Assert.Null(member); }
private void CheckEnumType(NamedTypeSymbol type, Accessibility declaredAccessibility, SpecialType underlyingType) { Assert.Equal(type.BaseType.SpecialType, SpecialType.System_Enum); Assert.Equal(type.EnumUnderlyingType.SpecialType, underlyingType); Assert.Equal(type.DeclaredAccessibility, declaredAccessibility); Assert.True(type.IsSealed); // value__ field should not be exposed from type, even though it is public, // since we want to prevent source from accessing the field directly. var field = type.GetMembers(WellKnownMemberNames.EnumBackingFieldName).SingleOrDefault() as FieldSymbol; Assert.Null(field); var sourceType = type as SourceNamedTypeSymbol; if (sourceType != null) { field = sourceType.EnumValueField; Assert.NotNull(field); Assert.Equal(field.Name, WellKnownMemberNames.EnumBackingFieldName); Assert.False(field.IsStatic); Assert.False(field.IsConst); Assert.False(field.IsReadOnly); Assert.Equal(field.DeclaredAccessibility, Accessibility.Public); // Dev10: value__ is public Assert.Equal(field.Type, type.EnumUnderlyingType); var module = new PEAssemblyBuilder((SourceAssemblySymbol)sourceType.ContainingAssembly, EmitOptions.Default, OutputKind.DynamicallyLinkedLibrary, GetDefaultModulePropertiesForSerialization(), SpecializedCollections.EmptyEnumerable<ResourceDescription>()); var context = new EmitContext(module, null, new DiagnosticBag()); var typeDefinition = (Microsoft.Cci.ITypeDefinition)type; var fieldDefinition = typeDefinition.GetFields(context).First(); Assert.Same(fieldDefinition, field); // Dev10: value__ field is the first field. Assert.True(fieldDefinition.IsSpecialName); Assert.True(fieldDefinition.IsRuntimeSpecial); context.Diagnostics.Verify(); } }
private void CheckEnumConstant(NamedTypeSymbol type, string name, object value) { var field = type.GetMembers(name).SingleOrDefault() as FieldSymbol; Assert.NotNull(field); Assert.True(field.IsStatic); Assert.True(field.IsConst); // TODO: DeclaredAccessibility should be NotApplicable. //Assert.Equal(field.DeclaredAccessibility, Accessibility.NotApplicable); Assert.Equal(field.Type, type); Assert.Equal(field.ConstantValue, value); var sourceType = type as SourceNamedTypeSymbol; if (sourceType != null) { var fieldDefinition = (Microsoft.Cci.IFieldDefinition)field; Assert.False(fieldDefinition.IsSpecialName); Assert.False(fieldDefinition.IsRuntimeSpecial); } }
private static PropertySymbol FindIndexerWithParameterCount(NamedTypeSymbol type, int parameterCount) { return type.GetMembers().Where(s => s.Kind == SymbolKind.Property).Cast<PropertySymbol>().Where(p => p.Parameters.Length == parameterCount).Single(); }
public BoundExpression Property(NamedTypeSymbol receiver, string name) { // TODO: unroll loop and add diagnostics for failure var property = receiver.GetMembers(name).OfType<PropertySymbol>().Single(); Debug.Assert(property.IsStatic); return Call(null, property.GetMethod); }
private bool EmbedMatchingInterfaceMethods(NamedTypeSymbol sourceInterface, CSharpSyntaxNode syntaxNodeOpt, DiagnosticBag diagnostics) { bool foundMatch = false; foreach (Symbol m in sourceInterface.GetMembers(UnderlyingEvent.MetadataName)) { if (m.Kind == SymbolKind.Method) { TypeManager.EmbedMethodIfNeedTo((MethodSymbol)m, syntaxNodeOpt, diagnostics); foundMatch = true; } } return foundMatch; }
private void VerifyMethodAndAccessorSame(NamedTypeSymbol type, PropertySymbol property, MethodSymbol accessor) { Assert.NotNull(accessor); Assert.Equal(type, accessor.ContainingType); Assert.Equal(type, accessor.ContainingSymbol); var method = type.GetMembers(accessor.Name).Single(); Assert.NotNull(method); Assert.Equal(accessor, method); Assert.True(accessor.MethodKind == MethodKind.PropertyGet || accessor.MethodKind == MethodKind.PropertySet, "Accessor kind: " + accessor.MethodKind.ToString()); Assert.Equal(accessor.AssociatedSymbol, property); }