Esempio n. 1
0
        public void RefReturn()
        {
            var arrayP = ParameterExpression.Create(new ArrayType(TypeSignature.String, 1), "a");

            cx.AddTestExpr(Expression.ArrayIndex(
                               arrayP,
                               Expression.Constant(11)
                               ), arrayP);

            var marrayP = ParameterExpression.Create(new ArrayType(TypeSignature.String, 3), "a");

            cx.AddTestExpr(Expression.ArrayIndex(
                               marrayP,
                               Expression.Constant(11),
                               Expression.Constant(12),
                               Expression.Constant(13)
                               ), marrayP);

            var refP = ParameterExpression.Create(new ByReferenceType(TypeSignature.Int32), "r");

            cx.AddTestExpr(refP, refP);


            var myTupleP = ParameterExpression.Create(new ByReferenceType(TypeReference.FromType(typeof((int, int)))), "myTuple");

            cx.AddTestExpr(Expression.FieldAccess(FieldReference.FromLambda <(int, int)>(r => r.Item1), target: myTupleP), myTupleP);

            check.CheckOutput(cx);
        }
Esempio n. 2
0
        public void TransdelegateFunctionConversion()
        {
            var predicate  = TypeReference.FromType(typeof(Predicate <string>));
            var predicateP = ParameterExpression.Create(predicate, "predicateP");

            cx.AddTestExpr(Expression.FunctionConversion(predicateP, TypeReference.FromType(typeof(Func <string, bool>))), predicateP);
            check.CheckOutput(cx);
        }
Esempio n. 3
0
        public void Nullable_Read()
        {
            var p = ParameterExpression.Create(TypeReference.FromType(typeof(int?)), "a");

            cx.AddTestExpr(ExpressionFactory.Nullable_HasValue(p), p);
            cx.AddTestExpr(ExpressionFactory.Nullable_Value(p), p);

            check.CheckOutput(cx);
        }
Esempio n. 4
0
        public void RefReturnCondition()
        {
            var refP1 = ParameterExpression.Create(new ByReferenceType(TypeSignature.Int32), "r1");
            var refP2 = ParameterExpression.Create(new ByReferenceType(TypeSignature.Int32), "r2");

            cx.AddTestExpr(refP1, refP1);
            cx.AddTestExpr(Expression.Conditional(pBool1, refP1, refP2), refP1, refP2, pBool1);

            check.CheckOutput(cx);
        }
Esempio n. 5
0
        public void NameSanitization()
        {
            var stringT = TypeReference.FromType(typeof(string));

            var type = TypeSignature.Class("MyType", ns, Accessibility.APublic);

            cx.AddType(TypeDef.Empty(type).AddMember(
                           // Should be renamed, there is collision with virtual object.Equals
                           new FieldDef(new FieldSignature(type, "Equals", Accessibility.APublic, stringT, false, true))
                           ));
            var type2 = TypeSignature.Class("MyType2", ns, Accessibility.APublic);

            cx.AddType(TypeDef.Empty(type2).AddMember(
                           // OK, no collision here
                           new MethodDef(
                               MethodSignature.Instance("Equals", type2, Accessibility.APublic, TypeSignature.Boolean),
                               ImmutableArray.Create(ParameterExpression.CreateThisParam(type2)),
                               Expression.Constant(true)
                               )
                           ));
            var type3 = TypeSignature.Class("MyType3", ns, Accessibility.APublic);

            cx.AddType(TypeDef.Empty(type3).AddMember(
                           // Should be renamed
                           new MethodDef(
                               MethodSignature.Instance("Equals", type3, Accessibility.APublic, TypeSignature.Boolean, new MethodParameter(TypeSignature.Object, "obj2")),
                               ImmutableArray.Create(ParameterExpression.CreateThisParam(type3), ParameterExpression.Create(TypeSignature.Object, "obj2")),
                               Expression.Constant(true)
                               )
                           ));
            var type4 = TypeSignature.Class("MyType4", ns, Accessibility.APublic);

            cx.AddType(TypeDef.Empty(type4).AddMember(
                           // Should be renamed
                           new MethodDef(
                               MethodSignature.Static("Equals", type4, Accessibility.APublic, TypeSignature.Boolean, new MethodParameter(TypeSignature.Object, "obj2")),
                               ImmutableArray.Create(ParameterExpression.Create(TypeSignature.Object, "obj2")),
                               Expression.Constant(true)
                               )
                           ));
            var type5 = TypeSignature.Class("MyType5", ns, Accessibility.APublic);

            cx.AddType(TypeDef.Empty(type5).AddMember(
                           // OK, this is override
                           new MethodDef(
                               MethodSignature.Instance("Equals", type5, Accessibility.APublic, TypeSignature.Boolean, new MethodParameter(TypeSignature.Object, "obj2")).With(isOverride: true, isVirtual: true),
                               ImmutableArray.Create(ParameterExpression.CreateThisParam(type5), ParameterExpression.Create(TypeSignature.Object, "obj2")),
                               Expression.Constant(true)
                               )
                           ));
            check.CheckOutput(cx);
        }
Esempio n. 6
0
        public void StructMethodCall(bool reference, bool dereference)
        {
            cx.AddType(MyStructWithMethod());

            var thisP = ParameterExpression.Create(OptionallyReference(MyStruct, reference), "this");

            var target = dereference ? Expression.Dereference(thisP) : thisP;

            var body = Expression.MethodCall(MyStruct_Method, ImmutableArray <Expression> .Empty, target);

            cx.AddTestExpr(body, thisP);
            check.CheckOutput(cx, $"ref={reference}&deref={dereference}");
        }
Esempio n. 7
0
        public void StructBoxing()
        {
            cx.AddType(MyStructDef);
            var p = ParameterExpression.Create(MyStruct, "p");

            cx.AddTestExpr(Expression.ReferenceConversion(p, TypeSignature.Object), p);
            cx.AddTestExpr(Expression.MethodCall(
                               cx.GetMemberMethods(TypeSignature.Object.NotGeneric()).Single(m => m.Signature.Name == "GetHashCode"),
                               args: ImmutableArray <Expression> .Empty,
                               Expression.ReferenceConversion(p, TypeSignature.Object)
                               ), p);
            check.CheckOutput(cx);
        }
Esempio n. 8
0
        public void StructReadonlyField()
        {
            var roField = new FieldSignature(MyStruct, "ROField", Accessibility.APublic, TypeSignature.Int32, false, true);

            cx.AddType(MyStructDef.AddMember(new FieldDef(roField)));

            var thisP = ParameterExpression.Create(TypeReference.ByReferenceType(MyStruct), "this");

            var body = Expression.FieldAccess(roField, thisP).Dereference();

            cx.AddTestExpr(body, thisP);
            check.CheckOutput(cx);
        }
Esempio n. 9
0
        public void String_Concat_Objects()
        {
            var pString  = ParameterExpression.Create(TypeSignature.String, "s");
            var pInt     = ParameterExpression.Create(TypeSignature.Int64, "int");
            var pIntNull = ParameterExpression.Create(TypeSignature.NullableOfT.Specialize(TypeSignature.Int64), "intn");

            cx.AddTestExpr(ExpressionFactory.String_Concat());
            cx.AddTestExpr(ExpressionFactory.String_Concat(pInt), pInt);
            cx.AddTestExpr(ExpressionFactory.String_Concat(Expression.Constant(12), Expression.Constant("--")));
            cx.AddTestExpr(ExpressionFactory.String_Concat(pInt, Expression.Constant("; "), pIntNull), pInt, pIntNull);

            check.CheckOutput(cx);
        }
Esempio n. 10
0
        public void String_Concat()
        {
            var pString = ParameterExpression.Create(TypeSignature.String, "s");

            cx.AddTestExpr(ExpressionFactory.String_Concat());
            cx.AddTestExpr(ExpressionFactory.String_Concat(pString), pString);
            cx.AddTestExpr(ExpressionFactory.String_Concat(pString, pString), pString);
            cx.AddTestExpr(ExpressionFactory.String_Concat(pString, Expression.Constant("; "), pString), pString);
            cx.AddTestExpr(ExpressionFactory.String_Concat(pString, Expression.Constant("; "), pString, Expression.Constant("; "), pString), pString);
            cx.AddTestExpr(ExpressionFactory.String_Concat(pString, Expression.Constant("; "), ExpressionFactory.String_Concat(pString, Expression.Constant("; "), pString, Expression.Constant("; "), pString), Expression.Constant("; "), pString), pString);

            check.CheckOutput(cx);
        }
Esempio n. 11
0
        public void GenericMethodInGenericTypes()
        {
            var ns       = NamespaceSignature.Parse("MyNamespace");
            var t1       = new GenericParameter(Guid.NewGuid(), "T1");
            var t2       = new GenericParameter(Guid.NewGuid(), "T2");
            var tresult  = new GenericParameter(Guid.NewGuid(), "TResult");
            var rootType = TypeSignature.Class("MyType", ns, Accessibility.APublic, true, false, t1);

            var(f, p) = PropertyBuilders.CreateAutoProperty(rootType, "A", t1, isReadOnly: false);

            var map_fn_type = new FunctionType(new [] { new MethodParameter(t1, "a") }, tresult);
            var map_sgn     = MethodSignature.Instance("Map", rootType, Accessibility.APublic,
                                                       rootType.Specialize(tresult),
                                                       new [] { tresult },
                                                       new MethodParameter(map_fn_type.TryGetDelegate(), "func")
                                                       );
            var tmp     = ParameterExpression.Create(map_sgn.ResultType, "result");
            var map_def = MethodDef.Create(map_sgn, (@this, fn) =>
                                           Expression.Block(
                                               new [] {
                tmp.Ref()
                .CallMethod(p.Setter.Signature.Specialize(new TypeReference[] { tresult }, null),
                            fn.Read().FunctionConvert(map_fn_type).Invoke(@this.Ref().CallMethod(p.Getter.Signature.SpecializeFromDeclaringType()))
                            )
            },
                                               result: tmp
                                               )
                                           .Where(tmp, Expression.NewObject(MethodSignature.ImplicitConstructor(rootType).Specialize(new TypeReference[] { tresult }, null)))
                                           );


            var type = TypeSignature.Class("MyNestedType", rootType, Accessibility.APublic, true, false, t2);

            var t3     = new GenericParameter(Guid.NewGuid(), "T3");
            var method = MethodSignature.Instance(
                "M", type, Accessibility.APublic,
                returnType: type.Specialize(t1, t3),
                typeParameters: new [] { t3 });

            var td = TypeDef.Empty(type)
                     .AddMember(MethodDef.Create(method, @this =>
                                                 Expression.NewObject(
                                                     MethodSignature.ImplicitConstructor(type).Specialize(new TypeReference[] { t1, t3 }, null),
                                                     ImmutableArray <Expression> .Empty
                                                     )
                                                 ))
            ;

            cx.AddType(TypeDef.Empty(rootType).AddMember(td, f, p, map_def));
            check.CheckOutput(cx);
        }
Esempio n. 12
0
        public void StructFieldAccess(bool reference, bool dereference)
        {
            cx.AddType(MyStructDef);

            var thisP = ParameterExpression.Create(OptionallyReference(MyStruct, reference), "this");

            var target = dereference ? Expression.Dereference(thisP) : thisP;
            var body   =
                Expression.FieldAccess(MyStruct_IntField, target)
                .ReferenceAssign(Expression.Constant(0));

            cx.AddTestExpr(body, thisP);
            check.CheckOutput(cx, $"ref={reference}&deref={dereference}");
        }
Esempio n. 13
0
        public void ArrayIndex()
        {
            var arrayP = ParameterExpression.Create(new ArrayType(TypeSignature.String, 1), "a");

            cx.AddTestExpr(Expression.ArrayIndex(
                               arrayP,
                               Expression.Constant(11)
                               ).Dereference(), arrayP);

            cx.AddTestExpr(Expression.ArrayIndex(
                               arrayP,
                               Expression.Constant(11)
                               ).ReferenceAssign(Expression.Constant("abc")), arrayP);

            check.CheckOutput(cx);
        }
Esempio n. 14
0
        public void TrivialFunctionConversions()
        {
            var stringFunc  = new FunctionType(ImmutableArray <MethodParameter> .Empty, TypeSignature.String).TryGetDelegate();
            var objectFunc  = new FunctionType(ImmutableArray <MethodParameter> .Empty, TypeSignature.Object).TryGetDelegate();
            var pStringFunc = ParameterExpression.Create(stringFunc, "stringFunc");

            cx.AddTestExpr(Expression.ReferenceConversion(pStringFunc, objectFunc), pStringFunc);
            cx.AddTestExpr(Expression.FunctionConversion(pStringFunc, objectFunc), pStringFunc);
            cx.AddTestExpr(Expression.FunctionConversion(Expression.FunctionConversion(pStringFunc, new FunctionType(ImmutableArray <MethodParameter> .Empty, TypeSignature.String)), stringFunc), pStringFunc);
            cx.AddTestExpr(Expression.FunctionConversion(Expression.FunctionConversion(pStringFunc, new FunctionType(ImmutableArray <MethodParameter> .Empty, TypeSignature.Object)), objectFunc), pStringFunc);

            // var predicate = cx.FindType(typeof(Predicate<string>));
            // var predicateP = ParameterExpression.Create(predicate, "predicateP");
            // cx.AddTestExpr(Expression.FunctionConversion(predicateP, cx.FindType(typeof(Func<string, bool>))), predicateP);
            check.CheckOutput(cx);
        }
Esempio n. 15
0
        public void StructInterface()
        {
            var icloneable = TypeSignature.FromType(typeof(ICloneable));
            var method     = new MethodSignature(MyStruct, ImmutableArray <MethodParameter> .Empty, "Clone", TypeSignature.Object, false, Accessibility.APublic, false, false, false, false, ImmutableArray <GenericParameter> .Empty);
            var thisP      = ParameterExpression.CreateThisParam(method);
            var methodDef  = new MethodDef(method, ImmutableArray.Create(thisP), Expression.ReferenceConversion(thisP, TypeSignature.Object));

            cx.AddType(MyStructDef.AddMember(methodDef).AddImplements(new SpecializedType(icloneable, ImmutableArray <TypeReference> .Empty)));

            var interfaceClone = cx.GetMemberMethods(icloneable.NotGeneric()).Single(m => m.Signature.Name == "Clone");

            var p = ParameterExpression.Create(MyStruct, "p");

            cx.AddTestExpr(Expression.MethodCall(method, ImmutableArray <Expression> .Empty, p), p);
            cx.AddTestExpr(Expression.MethodCall(interfaceClone, ImmutableArray <Expression> .Empty, Expression.ReferenceConversion(p, icloneable)), p);

            check.CheckOutput(cx);
        }
Esempio n. 16
0
        public void LocalFunction()
        {
            ParameterExpression pA  = ParameterExpression.Create(TypeSignature.Boolean, "a");
            Expression          fn2 = Expression.Function(
                Expression.Conditional(pA, Expression.Constant(1), Expression.Constant(2)),
                pA
                );
            ParameterExpression localFn2 = ParameterExpression.Create(fn2.Type(), "fn2");

            cx.AddTestExpr(
                Expression.Binary("+",
                                  localFn2.Read().Invoke(Expression.Constant(true)),
                                  localFn2.Read().Invoke(Expression.Constant(false))
                                  )
                .Where(localFn2, fn2)
                );

            check.CheckOutput(cx);
        }
Esempio n. 17
0
        public void LocalFunction()
        {
            var fn1 = Expression.Function(Expression.Constant(1));
            var v1  = ParameterExpression.Create(fn1.Type(), "v1");
            var fn2 = Expression.Function(
                Expression.Conditional(
                    pBool1,
                    Expression.ReferenceConversion(Expression.Invoke(v1, ImmutableArray <Expression> .Empty), TypeSignature.Object),
                    Expression.Constant <object>(null)
                    ),
                pBool1);
            var v2 = ParameterExpression.Create(fn2.Type(), "v2");

            cx.AddTestExpr(
                Expression.LetIn(v1, fn1, Expression.LetIn(v2, fn2,
                                                           Expression.Invoke(v2, ImmutableArray.Create(Expression.Constant(true)))
                                                           )));
            check.CheckOutput(cx);
        }
Esempio n. 18
0
        TypeDef MyStructWithConstructor()
        {
            var methodSignature = MethodSignature.Constructor(MyStruct, Accessibility.APublic, new MethodParameter(GuidType, "id"), new MethodParameter(TypeSignature.Int32, "count"));

            var thisP  = ParameterExpression.CreateThisParam(MyStruct);
            var idP    = ParameterExpression.Create(GuidType, "id");
            var countP = ParameterExpression.Create(TypeSignature.Int32, "count");

            var method = MethodDef.Create(methodSignature, (thisP, idP, countP) =>
                                          Expression.Block(
                                              ImmutableArray.Create(
                                                  Expression.FieldAccess(MyStruct_GuidField, thisP).ReferenceAssign(idP),
                                                  Expression.FieldAccess(MyStruct_IntField, thisP).ReferenceAssign(countP)
                                                  ),
                                              result: Expression.Nop
                                              )
                                          );

            return(MyStructDef.AddMember(method));
        }
Esempio n. 19
0
        public void Variables()
        {
            var myVar = ParameterExpression.Create(TypeSignature.Int32, "myVar");

            cx.AddTestExpr(Expression.LetIn(
                               myVar, Expression.Constant(42),
                               target: Expression.Binary("+", myVar, myVar)
                               ));

            var mutableVar = ParameterExpression.CreateMutable(TypeSignature.Int32, "mutableVar");

            cx.AddTestExpr(Expression.LetIn(
                               mutableVar, Expression.Constant(42),
                               target: Expression.Binary("+",
                                                         mutableVar,
                                                         new [] {
                mutableVar.Assign(Expression.Constant(30))
            }.ToBlock(mutableVar)
                                                         )
                               ));

            var declaringType = TypeSignature.StaticClass("TestClass", NamespaceSignature.Parse("NS"), Accessibility.APublic);
            var method        = MethodSignature.Static(
                "M",
                declaringType,
                Accessibility.APublic,
                TypeSignature.Int32,
                new MethodParameter(TypeSignature.Int32, "a"));
            var argA = ParameterExpression.CreateMutable(TypeSignature.Int32, "a");
            var body = new [] {
                argA.Assign(Expression.Binary("+", argA, Expression.Constant(1)))
            }.ToBlock(result: argA);
            var methodDef = new MethodDef(method, new [] { argA }, body);

            cx.AddType(TypeDef.Empty(declaringType).AddMember(methodDef));

            check.CheckOutput(cx);
        }
Esempio n. 20
0
        public void Functions()
        {
            // parameterless `() => 1`

            Expression fn1 = Expression.Function(Expression.Constant(1));

            // (bool a) => a ? 1 : 2

            ParameterExpression pA  = ParameterExpression.Create(TypeSignature.Boolean, "a");
            Expression          fn2 = Expression.Function(
                Expression.Conditional(pA, Expression.Constant(1), Expression.Constant(2)),
                pA
                );

            cx.AddTestExpr(fn1.Invoke());
            cx.AddTestExpr(fn2.Invoke(Expression.Constant(true)));

            var func = TypeReference.FromType(typeof(Func <bool, int>));

            cx.AddTestExpr(fn2.FunctionConvert(func));

            check.CheckOutput(cx);
        }
Esempio n. 21
0
        public void GenericClass()
        {
            var paramT         = GenericParameter.Create("T");
            var myContainerSgn = TypeSignature.Class(
                "MyContainer",
                NamespaceSignature.Parse("NS"),
                Accessibility.APublic,
                genericParameters: new [] { paramT }
                );

            var(item_field, item_prop) = PropertyBuilders.CreateAutoProperty(
                myContainerSgn,
                name: "Item",
                propertyType: paramT,
                isReadOnly: false
                );

            var listType = TypeSignature.FromType(typeof(List <>))
                           .Specialize(paramT);
            var toListSgn = MethodSignature.Instance(
                "ToList",
                myContainerSgn,
                Accessibility.APublic,
                returnType: listType
                );

            var toListDef = MethodDef.Create(toListSgn, thisParam => {
                var resultVar = ParameterExpression.Create(listType, "result");
                var listCtor  = MethodReference.FromLambda(() => new List <int>())
                                .Signature
                                .Specialize(paramT);
                var listAdd = MethodReference.FromLambda <List <int> >(l => l.Add(0))
                              .Signature
                              .Specialize(paramT);

                return(Expression.LetIn(
                           // result = new List<T>()
                           resultVar, Expression.NewObject(listCtor),
                           new [] {
                    // result.Add(this.Item)
                    resultVar.Read().CallMethod(listAdd,
                                                thisParam.Read().ReadField(item_field.Signature.SpecializeFromDeclaringType())
                                                )
                }.ToBlock(resultVar)
                           ));
            });

            var copyFromSgn = MethodSignature.Instance(
                "CopyFrom",
                myContainerSgn,
                Accessibility.APublic,
                returnType: TypeSignature.Void,
                new MethodParameter(
                    myContainerSgn.SpecializeByItself(),
                    "other"
                    )
                );

            var copyFromDef = MethodDef.Create(copyFromSgn, (thisParam, otherParam) => {
                var field = item_field.Signature.SpecializeFromDeclaringType();
                return(thisParam.Read().AssignField(
                           field,
                           otherParam.Read().ReadField(field)
                           ));
            });


            var myContainerDef =
                TypeDef.Empty(myContainerSgn)
                .AddMember(item_field, item_prop, toListDef, copyFromDef);

            cx.AddType(myContainerDef);

            check.CheckOutput(cx);
        }