예제 #1
0
        /// <summary>
        /// Creates a generated expression from a typed constant.
        /// </summary>
        public static Expression CreateGenerated(Context cx, TypedConstant constant, IExpressionParentEntity parent,
                                                 int childIndex, Semmle.Extraction.Entities.Location location)
        {
            if (constant.IsNull)
            {
                return(Literal.CreateGeneratedNullLiteral(cx, parent, childIndex, location));
            }

            switch (constant.Kind)
            {
            case TypedConstantKind.Primitive:
                return(Literal.CreateGenerated(cx, parent, childIndex, constant.Type, constant.Value, location));

            case TypedConstantKind.Enum:
                // Enum value is generated in the following format: (Enum)value
                Action <Expression, int> createChild = (parent, index) => Literal.CreateGenerated(cx, parent, index, ((INamedTypeSymbol)constant.Type).EnumUnderlyingType, constant.Value, location);
                var cast = Cast.CreateGenerated(cx, parent, childIndex, constant.Type, constant.Value, createChild, location);
                return(cast);

            case TypedConstantKind.Type:
                var type = ((ITypeSymbol)constant.Value).OriginalDefinition;
                return(TypeOf.CreateGenerated(cx, parent, childIndex, type, location));

            case TypedConstantKind.Array:
                // Single dimensional arrays are in the following format:
                // * new Type[N] { item1, item2, ..., itemN }
                // * new Type[0]
                //
                // itemI is generated recursively.
                return(NormalArrayCreation.CreateGenerated(cx, parent, childIndex, constant.Type, constant.Values, location));

            default:
                cx.ExtractionError("Couldn't extract constant in attribute", constant.ToString(), location);
                return(null);
            }
        }
        public static Expression CreateGenerated(Context cx, IExpressionParentEntity parent, int childIndex, ITypeSymbol type, IEnumerable <TypedConstant> items, Semmle.Extraction.Entities.Location location)
        {
            var info = new ExpressionInfo(
                cx,
                AnnotatedTypeSymbol.CreateNotAnnotated(type),
                location,
                ExprKind.ARRAY_CREATION,
                parent,
                childIndex,
                true,
                null);

            var arrayCreation = new Expression(info);

            var length = items.Count();

            Literal.CreateGenerated(cx, arrayCreation, 0, cx.Compilation.GetSpecialType(SpecialType.System_Int32), length, location);

            if (length > 0)
            {
                var arrayInit = ArrayInitializer.CreateGenerated(cx, arrayCreation, InitializerIndex, location);
                var child     = 0;
                foreach (var item in items)
                {
                    Expression.CreateGenerated(cx, item, arrayInit, child++, location);
                }
            }

            return(arrayCreation);
        }