Ejemplo n.º 1
0
 private static SyntaxNode CreateArgument(
     this ISyntaxFactoryService factory,
     IParameterSymbol parameter)
 {
     return(factory.CreateArgument(nameOpt: null, refKind: parameter.RefKind,
                                   expression: factory.CreateIdentifierName(parameter.Name)));
 }
Ejemplo n.º 2
0
        public static IEnumerable <ISymbol> CreateFieldDelegatingConstructor(
            this ISyntaxFactoryService factory,
            string typeName,
            INamedTypeSymbol containingTypeOpt,
            IList <IParameterSymbol> parameters,
            IDictionary <string, ISymbol> parameterToExistingFieldMap,
            IDictionary <string, string> parameterToNewFieldMap,
            CancellationToken cancellationToken)
        {
            var fields     = factory.CreateFieldsForParameters(parameters, parameterToNewFieldMap);
            var statements = factory.CreateAssignmentStatements(parameters, parameterToExistingFieldMap, parameterToNewFieldMap)
                             .Select(s => s.WithAdditionalAnnotations(Simplifier.Annotation));

            foreach (var field in fields)
            {
                yield return(field);
            }

            yield return(CodeGenerationSymbolFactory.CreateConstructorSymbol(
                             attributes: null,
                             accessibility: Accessibility.Public,
                             modifiers: new SymbolModifiers(),
                             typeName: typeName,
                             parameters: parameters,
                             statements: statements.ToList(),
                             thisConstructorArguments: GetThisConstructorArguments(containingTypeOpt, parameterToExistingFieldMap)));
        }
Ejemplo n.º 3
0
        public static IEnumerable <IFieldSymbol> CreateFieldsForParameters(
            this ISyntaxFactoryService factory,
            IList <IParameterSymbol> parameters,
            IDictionary <string, string> parameterToNewFieldMap)
        {
            foreach (var parameter in parameters)
            {
                var refKind       = parameter.RefKind;
                var parameterType = parameter.Type;
                var parameterName = parameter.Name;

                if (refKind != RefKind.Out)
                {
                    // For non-out parameters, create a field and assign the parameter to it.
                    // TODO: I'm not sure that's what we really want for ref parameters.
                    string fieldName;
                    if (TryGetValue(parameterToNewFieldMap, parameterName, out fieldName))
                    {
                        yield return(CodeGenerationSymbolFactory.CreateFieldSymbol(
                                         attributes: null,
                                         accessibility: Accessibility.Private,
                                         modifiers: default(SymbolModifiers),
                                         type: parameterType,
                                         name: parameterToNewFieldMap[parameterName]));
                    }
                }
            }
        }
Ejemplo n.º 4
0
 public static SyntaxNode CreateThrowNotImplementStatement(
     this ISyntaxFactoryService codeDefinitionFactory,
     Compilation compilation)
 {
     return(codeDefinitionFactory.CreateThrowStatement(
                codeDefinitionFactory.CreateObjectCreationExpression(
                    compilation.NotImplementedExceptionType(),
                    SpecializedCollections.EmptyList <SyntaxNode>())));
 }
Ejemplo n.º 5
0
        private static IList <SyntaxNode> CreateGetHashCodeMethodStatements(
            ISyntaxFactoryService factory,
            Compilation compilation,
            INamedTypeSymbol containingType,
            IList <ISymbol> members,
            CancellationToken cancellationToken)
        {
            const string HashCodeName = "hashCode";

            // -1521134295
            var permuteValue = factory.CreateNegateExpression(
                factory.CreateConstantExpression(1521134295));

            var statements = new List <SyntaxNode>();

            var hashCodeNameExpression = factory.CreateIdentifierName(HashCodeName);

            var firstHashValue = ComputeHashValue(factory, compilation, members[0]);

            if (members.Count == 1)
            {
#if false
                return(this.S1.GetHashCode());
#endif
                statements.Add(factory.CreateReturnStatement(firstHashValue));
            }
            else
            {
#if false
                var hashCode = this.S1.GetHashCode();
#endif
                statements.Add(factory.CreateLocalDeclarationStatement(
                                   factory.CreateVariableDeclarator(HashCodeName, firstHashValue)));

                for (var i = 1; i < members.Count; i++)
                {
#if false
                    hashCode = hashCode * 0xA5555529 + value
#endif
                    statements.Add(factory.CreateExpressionStatement(
                                       factory.CreateAssignExpression(hashCodeNameExpression,
                                                                      factory.CreateAddExpression(
                                                                          factory.CreateMultiplyExpression(hashCodeNameExpression, permuteValue),
                                                                          ComputeHashValue(factory, compilation, members[i])))));
                }

#if false
                return(hashCode);
#endif
                statements.Add(factory.CreateReturnStatement(hashCodeNameExpression));
            }

            return(statements);
        }
Ejemplo n.º 6
0
        private static SyntaxNode GetDefaultEqualityComparer(
            ISyntaxFactoryService factory,
            Compilation compilation,
            ISymbol member)
        {
            var equalityComparerType = compilation.EqualityComparerOfTType();
            var constructedType      = equalityComparerType.Construct(GetType(compilation, member));

            return(factory.CreateMemberAccessExpression(
                       factory.CreateTypeReferenceExpression(constructedType),
                       factory.CreateIdentifierName(DefaultName)));
        }
        private static IList<SyntaxNode> CreateGetHashCodeMethodStatements(
            ISyntaxFactoryService factory,
            Compilation compilation,
            INamedTypeSymbol containingType,
            IList<ISymbol> members,
            CancellationToken cancellationToken)
        {
            const string HashCodeName = "hashCode";

            // -1521134295
            var permuteValue = factory.CreateNegateExpression(
                factory.CreateConstantExpression(1521134295));

            var statements = new List<SyntaxNode>();

            var hashCodeNameExpression = factory.CreateIdentifierName(HashCodeName);

            var firstHashValue = ComputeHashValue(factory, compilation, members[0]);
            if (members.Count == 1)
            {
#if false
                return this.S1.GetHashCode();
#endif
                statements.Add(factory.CreateReturnStatement(firstHashValue));
            }
            else
            {
#if false
                var hashCode = this.S1.GetHashCode();
#endif
                statements.Add(factory.CreateLocalDeclarationStatement(
                    factory.CreateVariableDeclarator(HashCodeName, firstHashValue)));

                for (var i = 1; i < members.Count; i++)
                {
#if false
                    hashCode = hashCode * 0xA5555529 + value
#endif
                    statements.Add(factory.CreateExpressionStatement(
                        factory.CreateAssignExpression(hashCodeNameExpression,
                            factory.CreateAddExpression(
                                factory.CreateMultiplyExpression(hashCodeNameExpression, permuteValue),
                                ComputeHashValue(factory, compilation, members[i])))));
                }

#if false
                return hashCode;
#endif
                statements.Add(factory.CreateReturnStatement(hashCodeNameExpression));
            }

            return statements;
        }
Ejemplo n.º 8
0
 public static IEventSymbol OverrideEvent(
     this ISyntaxFactoryService codeFactory,
     IEventSymbol overriddenEvent,
     SymbolModifiers modifiers,
     INamedTypeSymbol newContainingType)
 {
     return(CodeGenerationSymbolFactory.CreateEventSymbol(
                overriddenEvent,
                attributes: null,
                accessibility: overriddenEvent.ComputeResultantAccessibility(newContainingType),
                modifiers: modifiers,
                explicitInterfaceSymbol: null,
                name: overriddenEvent.Name));
 }
Ejemplo n.º 9
0
 public static IMethodSymbol CreateBaseDelegatingConstructor(
     this ISyntaxFactoryService factory,
     IMethodSymbol constructor,
     string typeName)
 {
     // Create a constructor that calls the base constructor.  Note: if there are no
     // parameters then don't bother writing out "base()" it's automatically implied.
     return(CodeGenerationSymbolFactory.CreateConstructorSymbol(
                attributes: null,
                accessibility: Accessibility.Public,
                modifiers: new SymbolModifiers(),
                typeName: typeName,
                parameters: constructor.Parameters,
                statements: null,
                baseConstructorArguments: constructor.Parameters.Length == 0 ? null : factory.CreateArguments(constructor.Parameters)));
 }
Ejemplo n.º 10
0
        private static SyntaxNode ComputeHashValue(
            ISyntaxFactoryService factory,
            Compilation compilation,
            ISymbol member)
        {
            var getHashCodeNameExpression = factory.CreateIdentifierName(GetHashCodeName);
            var thisSymbol = factory.CreateMemberAccessExpression(factory.CreateThisExpression(),
                                                                  factory.CreateIdentifierName(member.Name)).WithAdditionalAnnotations(Simplification.Simplifier.Annotation);

#if false
            EqualityComparer <SType> .Default.GetHashCode(this.S1)
#endif

            return(factory.CreateInvocationExpression(
                       factory.CreateMemberAccessExpression(
                           GetDefaultEqualityComparer(factory, compilation, member),
                           getHashCodeNameExpression),
                       thisSymbol));
        }
Ejemplo n.º 11
0
        public static IMethodSymbol CreateEqualsMethod(
            this ISyntaxFactoryService factory,
            Compilation compilation,
            INamedTypeSymbol containingType,
            IList <ISymbol> symbols,
            CancellationToken cancellationToken)
        {
            var statements = CreateEqualsMethodStatements(factory, compilation, containingType, symbols, cancellationToken);

            return(CodeGenerationSymbolFactory.CreateMethodSymbol(
                       attributes: null,
                       accessibility: Accessibility.Public,
                       modifiers: new SymbolModifiers(isOverride: true),
                       returnType: compilation.GetSpecialType(SpecialType.System_Boolean),
                       explicitInterfaceSymbol: null,
                       name: EqualsName,
                       typeParameters: null,
                       parameters: new[] { CodeGenerationSymbolFactory.CreateParameterSymbol(compilation.GetSpecialType(SpecialType.System_Object), ObjName) },
                       statements: statements));
        }
Ejemplo n.º 12
0
        public static IEnumerable <SyntaxNode> CreateAssignmentStatements(
            this ISyntaxFactoryService factory,
            IList <IParameterSymbol> parameters,
            IDictionary <string, ISymbol> parameterToExistingFieldMap,
            IDictionary <string, string> parameterToNewFieldMap)
        {
            foreach (var parameter in parameters)
            {
                var refKind       = parameter.RefKind;
                var parameterType = parameter.Type;
                var parameterName = parameter.Name;

                if (refKind == RefKind.Out)
                {
                    // If it's an out param, then don't create a field for it.  Instead, assign
                    // assign the default value for that type (i.e. "default(...)") to it.
                    var assignExpression = factory.CreateAssignExpression(
                        factory.CreateIdentifierName(parameterName),
                        factory.CreateDefaultExpression(parameterType));
                    var statement = factory.CreateExpressionStatement(assignExpression);
                    yield return(statement);
                }
                else
                {
                    // For non-out parameters, create a field and assign the parameter to it.
                    // TODO: I'm not sure that's what we really want for ref parameters.
                    string fieldName;
                    if (TryGetValue(parameterToExistingFieldMap, parameterName, out fieldName) ||
                        TryGetValue(parameterToNewFieldMap, parameterName, out fieldName))
                    {
                        var assignExpression = factory.CreateAssignExpression(
                            factory.CreateMemberAccessExpression(
                                factory.CreateThisExpression(),
                                factory.CreateIdentifierName(fieldName)),
                            factory.CreateIdentifierName(parameterName));
                        var statement = factory.CreateExpressionStatement(assignExpression);
                        yield return(statement);
                    }
                }
            }
        }
Ejemplo n.º 13
0
        public static SyntaxNode GenerateThrowStatement(
            ISyntaxFactoryService factory,
            SemanticDocument document,
            string exceptionMetadataName,
            CancellationToken cancellationToken)
        {
            var compilation   = document.SemanticModel.Compilation;
            var exceptionType = compilation.GetTypeByMetadataName(exceptionMetadataName);

            // If we can't find the Exception, we obviously can't generate anything.
            if (exceptionType == null)
            {
                return(null);
            }

            var exceptionCreationExpression = factory.CreateObjectCreationExpression(
                exceptionType,
                SpecializedCollections.EmptyList <SyntaxNode>());

            return(factory.CreateThrowStatement(exceptionCreationExpression));
        }
Ejemplo n.º 14
0
        public static SyntaxNode GenerateThrowStatement(
            ISyntaxFactoryService factory,
            SemanticDocument document,
            string exceptionMetadataName,
            CancellationToken cancellationToken)
        {
            var compilation = document.SemanticModel.Compilation;
            var exceptionType = compilation.GetTypeByMetadataName(exceptionMetadataName);

            // If we can't find the Exception, we obviously can't generate anything.
            if (exceptionType == null)
            {
                return null;
            }

            var exceptionCreationExpression = factory.CreateObjectCreationExpression(
                exceptionType,
                SpecializedCollections.EmptyList<SyntaxNode>());

            return factory.CreateThrowStatement(exceptionCreationExpression);
        }
Ejemplo n.º 15
0
        public static IMethodSymbol OverrideMethod(
            this ISyntaxFactoryService codeFactory,
            IMethodSymbol overriddenMethod,
            SymbolModifiers modifiers,
            INamedTypeSymbol newContainingType,
            Document newDocument,
            CancellationToken cancellationToken)
        {
            // Abstract: Throw not implemented
            if (overriddenMethod.IsAbstract)
            {
                return(CodeGenerationSymbolFactory.CreateMethodSymbol(
                           overriddenMethod,
                           accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                           modifiers: modifiers,
                           statements: new[] { codeFactory.CreateThrowNotImplementStatement(newDocument.Project.GetCompilationAsync(cancellationToken).WaitAndGetResult(cancellationToken)) }));
            }
            else
            {
                // Otherwise, call the base method with the same parameters
                var typeParams = overriddenMethod.GetTypeArguments();
                var body       = codeFactory.CreateInvocationExpression(
                    codeFactory.CreateMemberAccessExpression(codeFactory.CreateBaseExpression(),
                                                             typeParams.IsDefaultOrEmpty
                        ? codeFactory.CreateIdentifierName(overriddenMethod.Name)
                        : codeFactory.CreateGenericName(overriddenMethod.Name, typeParams)),
                    codeFactory.CreateArguments(overriddenMethod.GetParameters()));

                return(CodeGenerationSymbolFactory.CreateMethodSymbol(
                           method: overriddenMethod,
                           accessibility: overriddenMethod.ComputeResultantAccessibility(newContainingType),
                           modifiers: modifiers,
                           statements: ((IMethodSymbol)overriddenMethod).ReturnsVoid
                        ? new SyntaxNode[] { codeFactory.CreateExpressionStatement(body) }
                        : new SyntaxNode[] { codeFactory.CreateReturnStatement(body) }));
            }
        }
Ejemplo n.º 16
0
        public static IPropertySymbol OverrideProperty(
            this ISyntaxFactoryService codeFactory,
            IPropertySymbol overriddenProperty,
            SymbolModifiers modifiers,
            INamedTypeSymbol containingType,
            Document document,
            CancellationToken cancellationToken)
        {
            var getAccessibility = overriddenProperty.GetMethod.ComputeResultantAccessibility(containingType);
            var setAccessibility = overriddenProperty.SetMethod.ComputeResultantAccessibility(containingType);

            SyntaxNode getBody = null;
            SyntaxNode setBody = null;

            // Implement an abstract property by throwing not implemented in accessors.
            if (overriddenProperty.IsAbstract)
            {
                getBody = codeFactory.CreateThrowNotImplementStatement(document.Project.GetCompilationAsync(cancellationToken).WaitAndGetResult(cancellationToken));
                setBody = getBody;
            }
            else if (overriddenProperty.IsIndexer() && document.Project.Language == LanguageNames.CSharp)
            {
                // Indexer: return or set base[]. Only in C#, since VB must refer to these by name.
                getBody = codeFactory.CreateReturnStatement(
                    codeFactory.CreateElementAccessExpression(
                        codeFactory.CreateBaseExpression(),
                        codeFactory.CreateArguments(overriddenProperty.Parameters)));

                setBody = codeFactory.CreateExpressionStatement(
                    codeFactory.CreateAssignExpression(
                        codeFactory.CreateElementAccessExpression(
                            codeFactory.CreateBaseExpression(),
                            codeFactory.CreateArguments(overriddenProperty.Parameters)),
                        codeFactory.CreateIdentifierName("value")));
            }
            else if (overriddenProperty.GetParameters().Any())
            {
                // Call accessors directly if C# overriding VB
                if (document.Project.Language == LanguageNames.CSharp &&
                    SymbolFinder.FindSourceDefinitionAsync(overriddenProperty, document.Project.Solution)
                    .WaitAndGetResult(CancellationToken.None).Language == LanguageNames.VisualBasic)
                {
                    var getName = overriddenProperty.GetMethod != null ? overriddenProperty.GetMethod.Name : null;
                    var setName = overriddenProperty.SetMethod != null ? overriddenProperty.SetMethod.Name : null;

                    getBody = getName == null
                        ? null
                        : codeFactory.CreateReturnStatement(
                        codeFactory.CreateInvocationExpression(
                            codeFactory.CreateMemberAccessExpression(
                                codeFactory.CreateBaseExpression(),
                                codeFactory.CreateIdentifierName(getName)),
                            codeFactory.CreateArguments(overriddenProperty.Parameters)));

                    setBody = setName == null
                        ? null
                        : codeFactory.CreateExpressionStatement(
                        codeFactory.CreateInvocationExpression(
                            codeFactory.CreateMemberAccessExpression(
                                codeFactory.CreateBaseExpression(),
                                codeFactory.CreateIdentifierName(setName)),
                            codeFactory.CreateArguments(overriddenProperty.SetMethod.GetParameters())));
                }
                else
                {
                    getBody = codeFactory.CreateReturnStatement(
                        codeFactory.CreateInvocationExpression(
                            codeFactory.CreateMemberAccessExpression(
                                codeFactory.CreateBaseExpression(),
                                codeFactory.CreateIdentifierName(overriddenProperty.Name)), codeFactory.CreateArguments(overriddenProperty.Parameters)));
                    setBody = codeFactory.CreateExpressionStatement(
                        codeFactory.CreateAssignExpression(
                            codeFactory.CreateInvocationExpression(
                                codeFactory.CreateMemberAccessExpression(
                                    codeFactory.CreateBaseExpression(),
                                    codeFactory.CreateIdentifierName(overriddenProperty.Name)), codeFactory.CreateArguments(overriddenProperty.Parameters)),
                            codeFactory.CreateIdentifierName("value")));
                }
            }
            else
            {
                // Regular property: return or set the base property
                getBody = codeFactory.CreateReturnStatement(
                    codeFactory.CreateMemberAccessExpression(
                        codeFactory.CreateBaseExpression(),
                        codeFactory.CreateIdentifierName(overriddenProperty.Name)));
                setBody = codeFactory.CreateExpressionStatement(
                    codeFactory.CreateAssignExpression(
                        codeFactory.CreateMemberAccessExpression(
                            codeFactory.CreateBaseExpression(),
                            codeFactory.CreateIdentifierName(overriddenProperty.Name)),
                        codeFactory.CreateIdentifierName("value")));
            }

            // Only generate a getter if the base getter is accessible.
            IMethodSymbol accessorGet = null;

            if (overriddenProperty.GetMethod != null && overriddenProperty.GetMethod.IsAccessibleWithin(containingType))
            {
                accessorGet = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    overriddenProperty.GetMethod,
                    accessibility: getAccessibility,
                    statements: new[] { getBody },
                    modifiers: modifiers);
            }

            // Only generate a setter if the base setter is accessible.
            IMethodSymbol accessorSet = null;

            if (overriddenProperty.SetMethod != null &&
                overriddenProperty.SetMethod.IsAccessibleWithin(containingType) &&
                overriddenProperty.SetMethod.DeclaredAccessibility != Accessibility.Private)
            {
                accessorSet = CodeGenerationSymbolFactory.CreateMethodSymbol(
                    overriddenProperty.SetMethod,
                    accessibility: setAccessibility,
                    statements: new[] { setBody },
                    modifiers: modifiers);
            }

            return(CodeGenerationSymbolFactory.CreatePropertySymbol(
                       overriddenProperty,
                       accessibility: overriddenProperty.ComputeResultantAccessibility(containingType),
                       modifiers: modifiers,
                       name: overriddenProperty.Name,
                       isIndexer: overriddenProperty.IsIndexer(),
                       getMethod: accessorGet,
                       setMethod: accessorSet));
        }
        private static IList<SyntaxNode> CreateEqualsMethodStatements(
            ISyntaxFactoryService 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.CreateIdentifierName(localName);

            var objNameExpression = factory.CreateIdentifierName(ObjName);

            var expressions = new List<SyntaxNode>();

            if (containingType.IsValueType)
            {
#if false
                if (!(obj is MyType))
                {
                    return false;
                }
#endif
                var ifStatement = factory.CreateIfStatement(
                    factory.CreateLogicalNotExpression(
                        factory.CreateIsExpression(
                            objNameExpression,
                            containingType)),
                    new[] { factory.CreateReturnStatement(factory.CreateFalseExpression()) });

#if false
                var myType = (MyType)obj;
#endif
                var localDeclaration = factory.CreateLocalDeclarationStatement(
                    factory.CreateVariableDeclarator(localName, factory.CreateCastExpression(containingType, objNameExpression)));

                statements.Add(ifStatement);
                statements.Add(localDeclaration);
            }
            else
            {
#if false
                var myType = obj as MyType;
#endif
                var localDeclaration = factory.CreateLocalDeclarationStatement(
                    factory.CreateVariableDeclarator(localName, factory.CreateAsExpression(objNameExpression, containingType)));

                statements.Add(localDeclaration);

#if false
                myType != null
#endif
                expressions.Add(factory.CreateReferenceNotEqualsExpression(localNameExpression, factory.CreateNullExpression()));
                if (HasExistingBaseEqualsMethod(containingType, cancellationToken))
                {
#if false
                    base.Equals(obj)
#endif
                    expressions.Add(factory.CreateInvocationExpression(
                        factory.CreateMemberAccessExpression(
                            factory.CreateBaseExpression(),
                            factory.CreateIdentifierName(EqualsName)),
                        objNameExpression));
                }
            }

            foreach (var member in members)
            {
                var symbolNameExpression = factory.CreateIdentifierName(member.Name);
                var thisSymbol = factory.CreateMemberAccessExpression(factory.CreateThisExpression(), symbolNameExpression).WithAdditionalAnnotations(Simplification.Simplifier.Annotation);
                var otherSymbol = factory.CreateMemberAccessExpression(localNameExpression, symbolNameExpression);

#if false
                EqualityComparer<SType>.Default.Equals(this.S1, myType.S1)
#endif
                var expression =
                    factory.CreateInvocationExpression(
                        factory.CreateMemberAccessExpression(
                            GetDefaultEqualityComparer(factory, compilation, member),
                            factory.CreateIdentifierName(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.CreateReturnStatement(
                expressions.Aggregate(factory.CreateLogicalAndExpression)));

            return statements;
        }
 private static SyntaxNode GetDefaultEqualityComparer(
     ISyntaxFactoryService factory,
     Compilation compilation,
     ISymbol member)
 {
     var equalityComparerType = compilation.EqualityComparerOfTType();
     var constructedType = equalityComparerType.Construct(GetType(compilation, member));
     return factory.CreateMemberAccessExpression(
         factory.CreateTypeReferenceExpression(constructedType),
         factory.CreateIdentifierName(DefaultName));
 }
Ejemplo n.º 19
0
 internal SyntaxNode CreateMarshalAsArgument(ISyntaxFactoryService syntaxFactoryService, INamedTypeSymbol unmanagedType)
 {
     return syntaxFactoryService.CreateMemberAccessExpression(
         syntaxFactoryService.CreateTypeReferenceExpression(unmanagedType), syntaxFactoryService.CreateIdentifierName(LPWStrText));
 }
Ejemplo n.º 20
0
 public static IList <SyntaxNode> CreateArguments(
     this ISyntaxFactoryService factory,
     ImmutableArray <IParameterSymbol> parameters)
 {
     return(parameters.Select(p => CreateArgument(factory, p)).ToList());
 }
        private static SyntaxNode ComputeHashValue(
            ISyntaxFactoryService factory,
            Compilation compilation,
            ISymbol member)
        {
            var getHashCodeNameExpression = factory.CreateIdentifierName(GetHashCodeName);
            var thisSymbol = factory.CreateMemberAccessExpression(factory.CreateThisExpression(),
                factory.CreateIdentifierName(member.Name)).WithAdditionalAnnotations(Simplification.Simplifier.Annotation);

#if false
            EqualityComparer<SType>.Default.GetHashCode(this.S1)
#endif

            return factory.CreateInvocationExpression(
                factory.CreateMemberAccessExpression(
                    GetDefaultEqualityComparer(factory, compilation, member),
                    getHashCodeNameExpression),
                thisSymbol);
        }
Ejemplo n.º 22
0
 internal SyntaxNode CreateCharSetArgument(ISyntaxFactoryService syntaxFactoryService, INamedTypeSymbol charSetType)
 {
     return syntaxFactoryService.CreateMemberAccessExpression(
         syntaxFactoryService.CreateTypeReferenceExpression(charSetType), syntaxFactoryService.CreateIdentifierName(UnicodeText));
 }
Ejemplo n.º 23
0
        private static IList <SyntaxNode> CreateEqualsMethodStatements(
            ISyntaxFactoryService 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.CreateIdentifierName(localName);

            var objNameExpression = factory.CreateIdentifierName(ObjName);

            var expressions = new List <SyntaxNode>();

            if (containingType.IsValueType)
            {
#if false
                if (!(obj is MyType))
                {
                    return(false);
                }
#endif
                var ifStatement = factory.CreateIfStatement(
                    factory.CreateLogicalNotExpression(
                        factory.CreateIsExpression(
                            objNameExpression,
                            containingType)),
                    new[] { factory.CreateReturnStatement(factory.CreateFalseExpression()) });

#if false
                var myType = (MyType)obj;
#endif
                var localDeclaration = factory.CreateLocalDeclarationStatement(
                    factory.CreateVariableDeclarator(localName, factory.CreateCastExpression(containingType, objNameExpression)));

                statements.Add(ifStatement);
                statements.Add(localDeclaration);
            }
            else
            {
#if false
                var myType = obj as MyType;
#endif
                var localDeclaration = factory.CreateLocalDeclarationStatement(
                    factory.CreateVariableDeclarator(localName, factory.CreateAsExpression(objNameExpression, containingType)));

                statements.Add(localDeclaration);

#if false
                myType != null
#endif
                expressions.Add(factory.CreateReferenceNotEqualsExpression(localNameExpression, factory.CreateNullExpression()));

                if (HasExistingBaseEqualsMethod(containingType, cancellationToken))
                {
#if false
                    base.Equals(obj)
#endif
                    expressions.Add(factory.CreateInvocationExpression(
                                        factory.CreateMemberAccessExpression(
                                            factory.CreateBaseExpression(),
                                            factory.CreateIdentifierName(EqualsName)),
                                        objNameExpression));
                }
            }

            foreach (var member in members)
            {
                var symbolNameExpression = factory.CreateIdentifierName(member.Name);
                var thisSymbol           = factory.CreateMemberAccessExpression(factory.CreateThisExpression(), symbolNameExpression).WithAdditionalAnnotations(Simplification.Simplifier.Annotation);
                var otherSymbol          = factory.CreateMemberAccessExpression(localNameExpression, symbolNameExpression);

#if false
                EqualityComparer <SType> .Default.Equals(this.S1, myType.S1)
#endif
                var expression =
                    factory.CreateInvocationExpression(
                        factory.CreateMemberAccessExpression(
                            GetDefaultEqualityComparer(factory, compilation, member),
                            factory.CreateIdentifierName(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.CreateReturnStatement(
                               expressions.Aggregate(factory.CreateLogicalAndExpression)));

            return(statements);
        }
Ejemplo n.º 24
0
 public static IList <SyntaxNode> CreateThrowNotImplementedStatementBlock(
     this ISyntaxFactoryService codeDefinitionFactory,
     Compilation compilation)
 {
     return(new[] { CreateThrowNotImplementStatement(codeDefinitionFactory, compilation) });
 }