Beispiel #1
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);
        }
Beispiel #2
0
        public static ILFunction CreateBody(MethodDef method, IMethod generatedMethod, MetadataContext cx)
        {
            Assert.NotNull(method.Body);
            Assert.False(method.ArgumentParams.IsDefault);
            if (method.Body is ILSpyMethodBody iLSpyMethod)
            {
                return(iLSpyMethod.BuildBody(generatedMethod, cx));
            }

            var translator    = new CodeTranslator(method, generatedMethod, cx);
            var declaringType = method.Signature.DeclaringType.SpecializeByItself();

            if (!method.Signature.IsStatic)
            {
                var firstArgument = method.ArgumentParams.First();
                if (declaringType.Type.IsValueType)
                {
                    Assert.Equal(TypeReference.ByReferenceType(declaringType), firstArgument.Type);
                }
                else
                {
                    Assert.Equal(declaringType, firstArgument.Type);
                }

                translator.Parameters.Add(
                    firstArgument.Id,
                    new ILVariable(VariableKind.Parameter, cx.GetTypeReference(firstArgument.Type), -1)
                {
                    Name = "this"
                });
            }
            foreach (var(i, (arg, param)) in method.ArgumentParams.Skip(method.Signature.IsStatic ? 0 : 1).ZipTuples(method.Signature.Params).Indexed())
            {
                Assert.Equal(arg.Type, param.Type);
                translator.Parameters.Add(
                    arg.Id,
                    new ILVariable(VariableKind.Parameter, cx.GetTypeReference(param.Type), i)
                {
                    Name = generatedMethod.Parameters[i].Name
                });
            }
            var verificationVarCopy = translator.Parameters.Keys.ToHashSet();

            var statements = translator.TranslateExpression(method.Body);

            Assert.Empty(translator.BreakTargets);
            Assert.Equal(verificationVarCopy, translator.Parameters.Keys.ToHashSet());

            return(translator.BuildTheFunction(statements));
        }
Beispiel #3
0
 private TypeReference OptionallyReference(TypeReference a, bool makeReference) =>
 makeReference?TypeReference.ByReferenceType(a) : a;