protected IMethodSymbol CreateSet(string originalFieldName, IFieldSymbol field, SyntaxGenerator factory) { var assigned = !field.IsStatic ? factory.MemberAccessExpression( factory.ThisExpression(), factory.IdentifierName(originalFieldName)) : factory.IdentifierName(originalFieldName); var body = factory.ExpressionStatement( factory.AssignmentStatement( assigned.WithAdditionalAnnotations(Simplifier.Annotation), factory.IdentifierName("value"))); return CodeGenerationSymbolFactory.CreateAccessorSymbol(SpecializedCollections.EmptyList<AttributeData>(), Accessibility.NotApplicable, new[] { body }.ToList()); }
private static IList<SyntaxNode> CreateEqualsMethodStatements( SyntaxGenerator factory, Compilation compilation, INamedTypeSymbol containingType, IEnumerable<ISymbol> members, CancellationToken cancellationToken) { var statements = new List<SyntaxNode>(); var parts = StringBreaker.BreakIntoWordParts(containingType.Name); string localName = "v"; for (int i = parts.Count - 1; i >= 0; i--) { var p = parts[i]; if (char.IsLetter(containingType.Name[p.Start])) { localName = containingType.Name.Substring(p.Start, p.Length).ToCamelCase(); break; } } var localNameExpression = factory.IdentifierName(localName); var objNameExpression = factory.IdentifierName(ObjName); var expressions = new List<SyntaxNode>(); if (containingType.IsValueType) { #if false if (!(obj is MyType)) { return false; } #endif var ifStatement = factory.IfStatement( factory.LogicalNotExpression( factory.IsTypeExpression( objNameExpression, containingType)), new[] { factory.ReturnStatement(factory.FalseLiteralExpression()) }); #if false var myType = (MyType)obj; #endif var localDeclaration = factory.LocalDeclarationStatement(localName, factory.CastExpression(containingType, objNameExpression)); statements.Add(ifStatement); statements.Add(localDeclaration); } else { #if false var myType = obj as MyType; #endif var localDeclaration = factory.LocalDeclarationStatement(localName, factory.TryCastExpression(objNameExpression, containingType)); statements.Add(localDeclaration); #if false myType != null #endif expressions.Add(factory.ReferenceNotEqualsExpression(localNameExpression, factory.NullLiteralExpression())); if (HasExistingBaseEqualsMethod(containingType, cancellationToken)) { #if false base.Equals(obj) #endif expressions.Add(factory.InvocationExpression( factory.MemberAccessExpression( factory.BaseExpression(), factory.IdentifierName(EqualsName)), objNameExpression)); } } foreach (var member in members) { var symbolNameExpression = factory.IdentifierName(member.Name); var thisSymbol = factory.MemberAccessExpression(factory.ThisExpression(), symbolNameExpression).WithAdditionalAnnotations(Simplification.Simplifier.Annotation); var otherSymbol = factory.MemberAccessExpression(localNameExpression, symbolNameExpression); #if false EqualityComparer<SType>.Default.Equals(this.S1, myType.S1) #endif var expression = factory.InvocationExpression( factory.MemberAccessExpression( GetDefaultEqualityComparer(factory, compilation, member), factory.IdentifierName(EqualsName)), thisSymbol, otherSymbol); expressions.Add(expression); } #if false return myType != null && base.Equals(obj) && EqualityComparer<int>.Default.Equals(this.S1, myType.S1) && ...; #endif statements.Add(factory.ReturnStatement( expressions.Aggregate(factory.LogicalAndExpression))); return statements; }
private SyntaxNode CreateThroughExpression(SyntaxGenerator factory) { var through = ThroughMember.IsStatic ? GenerateName(factory, State.ClassOrStructType.IsGenericType) : factory.ThisExpression(); through = factory.MemberAccessExpression( through, factory.IdentifierName(ThroughMember.Name)); var throughMemberType = ThroughMember.GetMemberType(); if ((State.InterfaceTypes != null) && (throughMemberType != null)) { // In the case of 'implement interface through field / property' , we need to know what // interface we are implementing so that we can insert casts to this interface on every // usage of the field in the generated code. Without these casts we would end up generating // code that fails compilation in certain situations. // // For example consider the following code. // class C : IReadOnlyList<int> { int[] field; } // When applying the 'implement interface through field' code fix in the above example, // we need to generate the following code to implement the Count property on IReadOnlyList<int> // class C : IReadOnlyList<int> { int[] field; int Count { get { ((IReadOnlyList<int>)field).Count; } ...} // as opposed to the following code which will fail to compile (because the array field // doesn't have a property named .Count) - // class C : IReadOnlyList<int> { int[] field; int Count { get { field.Count; } ...} // // The 'InterfaceTypes' property on the state object always contains only one item // in the case of C# i.e. it will contain exactly the interface we are trying to implement. // This is also the case most of the time in the case of VB, except in certain error conditions // (recursive / circular cases) where the span of the squiggle for the corresponding // diagnostic (BC30149) changes and 'InterfaceTypes' ends up including all interfaces // in the Implements clause. For the purposes of inserting the above cast, we ignore the // uncommon case and optimize for the common one - in other words, we only apply the cast // in cases where we can unambiguously figure out which interface we are trying to implement. var interfaceBeingImplemented = State.InterfaceTypes.SingleOrDefault(); if ((interfaceBeingImplemented != null) && (!throughMemberType.Equals(interfaceBeingImplemented))) { through = factory.CastExpression(interfaceBeingImplemented, through.WithAdditionalAnnotations(Simplifier.Annotation)); var facts = this.Document.GetLanguageService<ISyntaxFactsService>(); through = facts.Parenthesize(through); } } return through.WithAdditionalAnnotations(Simplifier.Annotation); }
private static SyntaxNode ComputeHashValue( SyntaxGenerator factory, Compilation compilation, ISymbol member) { var getHashCodeNameExpression = factory.IdentifierName(GetHashCodeName); var thisSymbol = factory.MemberAccessExpression(factory.ThisExpression(), factory.IdentifierName(member.Name)).WithAdditionalAnnotations(Simplification.Simplifier.Annotation); #if false EqualityComparer<SType>.Default.GetHashCode(this.S1) #endif return factory.InvocationExpression( factory.MemberAccessExpression( GetDefaultEqualityComparer(factory, compilation, member), getHashCodeNameExpression), thisSymbol); }