Ejemplo n.º 1
0
 private static ExpressionSyntax GenerateEqualsValueClauseWorker(
     IParameterSymbol parameter,
     object value)
 {
     return(ExpressionGenerator.GenerateExpression(parameter.Type, value, canUseFieldReference: true));
 }
Ejemplo n.º 2
0
        private static ExpressionSyntax CreateEnumMemberValue(EnumDeclarationSyntax destinationOpt, IFieldSymbol enumMember)
        {
            if (!enumMember.HasConstantValue)
            {
                return(null);
            }

            if (!(enumMember.ConstantValue is byte) &&
                !(enumMember.ConstantValue is sbyte) &&
                !(enumMember.ConstantValue is ushort) &&
                !(enumMember.ConstantValue is short) &&
                !(enumMember.ConstantValue is int) &&
                !(enumMember.ConstantValue is uint) &&
                !(enumMember.ConstantValue is long) &&
                !(enumMember.ConstantValue is ulong))
            {
                return(null);
            }

            var value = IntegerUtilities.ToInt64(enumMember.ConstantValue);

            if (destinationOpt != null)
            {
                if (destinationOpt.Members.Count == 0)
                {
                    if (value == 0)
                    {
                        return(null);
                    }
                }
                else
                {
                    // Don't generate an initializer if no other members have them, and our value
                    // would be correctly inferred from our position.
                    if (destinationOpt.Members.Count == value &&
                        destinationOpt.Members.All(m => m.EqualsValue == null))
                    {
                        return(null);
                    }

                    // Existing members, try to stay consistent with their style.
                    var lastMember = destinationOpt.Members.LastOrDefault(m => m.EqualsValue != null);
                    if (lastMember != null)
                    {
                        var lastExpression = lastMember.EqualsValue.Value;
                        if (lastExpression.Kind() == SyntaxKind.LeftShiftExpression &&
                            IntegerUtilities.HasOneBitSet(value))
                        {
                            var binaryExpression = (BinaryExpressionSyntax)lastExpression;
                            if (binaryExpression.Left.Kind() == SyntaxKind.NumericLiteralExpression)
                            {
                                var numericLiteral = (LiteralExpressionSyntax)binaryExpression.Left;
                                if (numericLiteral.Token.ValueText == "1")
                                {
                                    // The user is left shifting ones, stick with that pattern
                                    var shiftValue = IntegerUtilities.LogBase2(value);
                                    return(SyntaxFactory.BinaryExpression(
                                               SyntaxKind.LeftShiftExpression,
                                               SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal("1", 1)),
                                               SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(shiftValue.ToString(), shiftValue))));
                                }
                            }
                        }
                        else if (lastExpression.Kind() == SyntaxKind.NumericLiteralExpression)
                        {
                            var numericLiteral = (LiteralExpressionSyntax)lastExpression;
                            var numericToken   = numericLiteral.Token;
                            var numericText    = numericToken.ToString();

                            if (numericText.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                            {
                                // Hex
                                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
                                                                       SyntaxFactory.Literal(numericText.Substring(0, 2) + value.ToString("X"), value)));
                            }
                            else if (numericText.StartsWith("0b", StringComparison.OrdinalIgnoreCase))
                            {
                                return(SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression,
                                                                       SyntaxFactory.Literal(numericText.Substring(0, 2) + Convert.ToString(value, 2), value)));
                            }
                        }
                    }
                }
            }

            var namedType      = enumMember.Type as INamedTypeSymbol;
            var underlyingType = namedType != null ? namedType.EnumUnderlyingType : null;

            return(ExpressionGenerator.GenerateNonEnumValueExpression(
                       underlyingType,
                       enumMember.ConstantValue,
                       canUseFieldReference: true));
        }