Exemplo n.º 1
0
        /// <summary>
        /// Returns the System.String that represents the current TypedConstant.
        /// </summary>
        /// <returns>A System.String that represents the current TypedConstant.</returns>
        public static ExpressionSyntax ToExpression(this TypedConstant constant)
        {
            if (constant.IsNull)
            {
                return(LiteralExpression(SyntaxKind.NullLiteralExpression));
            }

            if (constant.Kind == TypedConstantKind.Array)
            {
                throw new NotSupportedException($"Unsupported TypedConstant: {constant.ToCSharpString()}");
            }

            if (constant.Kind == TypedConstantKind.Type)
            {
                Debug.Assert(constant.Value is object);
                return(TypeOfExpression(((ITypeSymbol)constant.Value).ToTypeSyntax()));
            }

            if (constant.Kind == TypedConstantKind.Enum)
            {
                return(DisplayEnumConstant(constant));
            }

            return(ParseExpression(constant.ToCSharpString()));
        }
 /// <summary>
 /// Gets the display string of <see cref="InsecureSymbol"/> or <see cref="InsecureAttribute"/>.
 /// </summary>
 /// <returns>Display string of <see cref="InsecureSymbol"/> or <see cref="InsecureAttribute"/>.</returns>
 public string GetDisplayString()
 {
     if (this.InsecureSymbol != null)
     {
         return(this.InsecureSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
     }
     else if (this.InsecureAttributeTypedConstant != null)
     {
         TypedConstant t = (TypedConstant)this.InsecureAttributeTypedConstant !;
         return(t.ToCSharpString());
     }
     else
     {
         throw new NotImplementedException("Unhandled case");
     }
 }
Exemplo n.º 3
0
        private static ImmutableHashSet <string> GetDirectImmutableException(AttributeData attrData)
        {
            SyntaxNode syntaxNode = attrData
                                    .ApplicationSyntaxReference?
                                    .GetSyntax();

            AttributeSyntax attrSyntax = syntaxNode as AttributeSyntax;

            ExpressionSyntax flagsExpression;

            // If we can't get the attribute syntax but we know the Immutable attribute exists, we're analyzing a type
            // from another assembly. We can analyze it by parsing its value back into syntax.
            if (attrSyntax == null)
            {
                TypedConstant exceptValue = attrData
                                            .NamedArguments
                                            .FirstOrDefault(kvp => kvp.Key == "Except")
                                            .Value;

                flagsExpression = exceptValue.IsNull
                                        ? null
                                        : SyntaxFactory.ParseExpression(exceptValue.ToCSharpString());
            }
            else
            {
                AttributeArgumentSyntax foundArg = attrSyntax
                                                   .ArgumentList?
                                                   .Arguments
                                                   .FirstOrDefault(
                    // Get the first argument that is defined by the "Except = ..." syntax
                    arg => arg.NameEquals?.Name?.Identifier.ValueText == "Except"
                    );

                flagsExpression = foundArg?.Expression;
            }

            if (flagsExpression == null)
            {
                // We have the Immutable attribute but no Except value, so we just return the defaults
                return(DefaultImmutabilityExceptions);
            }

            var set = ExceptFlagValuesToSet(flagsExpression);

            return(set);
        }
Exemplo n.º 4
0
 protected override string ToString(TypedConstant typedConstant)
 => typedConstant.ToCSharpString();
Exemplo n.º 5
0
        void Format(InlineCollection block, TypedConstant constant)
        {
            if (constant.IsNull)
            {
                block.Add("null".Render(Keyword));
                return;
            }
            switch (constant.Kind)
            {
            case TypedConstantKind.Primitive:
                if (constant.Value is bool)
                {
                    block.Add(WpfHelper.Render((bool)constant.Value ? "true" : "false", Keyword));
                }
                else if (constant.Value is string)
                {
                    block.Add(constant.ToCSharpString().Render(Text));
                }
                else
                {
                    block.Add(constant.ToCSharpString().Render(Number));
                }
                break;

            case TypedConstantKind.Enum:
                var en = constant.ToCSharpString();
                if (en.IndexOf('|') != -1)
                {
                    var items = constant.Type.GetMembers().Where(i => {
                        var field = i as IFieldSymbol;
                        return(field != null &&
                               field.HasConstantValue &&
                               UnsafeArithmeticHelper.Equals(UnsafeArithmeticHelper.And(constant.Value, field.ConstantValue), field.ConstantValue) &&
                               UnsafeArithmeticHelper.IsZero(field.ConstantValue) == false);
                    });
                    var flags = items.ToArray();
                    for (int i = 0; i < flags.Length; i++)
                    {
                        if (i > 0)
                        {
                            block.Add(" | ");
                        }
                        block.Add((constant.Type.Name + "." + flags[i].Name).Render(Enum));
                    }
                }
                else
                {
                    block.Add((constant.Type.Name + en.Substring(en.LastIndexOf('.'))).Render(Enum));
                }
                break;

            case TypedConstantKind.Type:
                block.Add("typeof".Render(Keyword));
                block.Add("(");
                Format(block, constant.Value as ISymbol, null, false);
                block.Add(")");
                break;

            case TypedConstantKind.Array:
                block.Add("new".Render(Keyword));
                block.Add("[] { ");
                bool c = false;
                foreach (var item in constant.Values)
                {
                    if (c)
                    {
                        block.Add(", ");
                    }
                    else
                    {
                        c = true;
                    }
                    Format(block, item);
                }
                block.Add(" }");
                break;

            default:
                block.Add(constant.ToCSharpString());
                break;
            }
        }
 private static string GetArgumentValue(TypedConstant typedConstant)
 {
     switch (typedConstant.Kind)
     {
         case TypedConstantKind.Error:
         case TypedConstantKind.Enum:
         case TypedConstantKind.Primitive:
             return typedConstant.Value.ToString();
         case TypedConstantKind.Type:
         case TypedConstantKind.Array:
             return typedConstant.ToCSharpString();
         default:
             throw new ArgumentOutOfRangeException(nameof(typedConstant));
     }
 }
Exemplo n.º 7
0
        internal void ToUIText(TextBlock block, TypedConstant constant)
        {
            switch (constant.Kind)
            {
            case TypedConstantKind.Primitive:
                if (constant.Value is bool)
                {
                    block.Append((bool)constant.Value ? "true" : "false", Keyword);
                }
                else if (constant.Value is string)
                {
                    block.Append(constant.ToCSharpString(), Text);
                }
                else
                {
                    block.Append(constant.ToCSharpString(), Number);
                }
                break;

            case TypedConstantKind.Enum:
                var en = constant.ToCSharpString();
                if (en.IndexOf('|') != -1)
                {
                    var items = constant.Type.GetMembers().Where(i => {
                        var field = i as IFieldSymbol;
                        return(field != null &&
                               field.HasConstantValue != false &&
                               UnsafeArithmeticHelper.Equals(UnsafeArithmeticHelper.And(constant.Value, field.ConstantValue), field.ConstantValue) &&
                               UnsafeArithmeticHelper.IsZero(field.ConstantValue) == false);
                    });
                    var flags = items.ToArray();
                    for (int i = 0; i < flags.Length; i++)
                    {
                        if (i > 0)
                        {
                            block.Append(" | ");
                        }
                        block.Append(constant.Type.Name + "." + flags[i].Name, Enum);
                    }
                }
                else
                {
                    block.Append(constant.Type.Name + en.Substring(en.LastIndexOf('.')), Enum);
                }
                break;

            case TypedConstantKind.Type:
                block.Append("typeof", Keyword).Append("(")
                .AddSymbol((constant.Value as ITypeSymbol), null, this)
                .Append(")");
                break;

            case TypedConstantKind.Array:
                block.Append("{");
                bool c = false;
                foreach (var item in constant.Values)
                {
                    if (c == false)
                    {
                        c = true;
                    }
                    else
                    {
                        block.Append(", ");
                    }
                    ToUIText(block, item);
                }
                block.Append("}");
                break;

            default:
                block.Append(constant.ToCSharpString());
                break;
            }
        }