예제 #1
0
파일: Expression.cs 프로젝트: luchua-bc/ql
        /// <summary>
        /// Creates a generated expression from a typed constant.
        /// </summary>
        public static Expression?CreateGenerated(Context cx, TypedConstant constant, IExpressionParentEntity parent,
                                                 int childIndex, Extraction.Entities.Location location)
        {
            if (constant.IsNull ||
                constant.Type is null)
            {
                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));

            case TypedConstantKind.Error:
            default:
                cx.ExtractionError("Couldn't extract constant in attribute", constant.ToString(), location);
                return(null);
            }
        }