Exemplo n.º 1
0
        public StringAllocatorMethodNode(MethodDesc constructorMethod)
        {
            Debug.Assert(constructorMethod.IsConstructor && constructorMethod.OwningType.IsString);

            // Find the allocator method that matches the constructor signature.
            var signatureBuilder = new MethodSignatureBuilder(constructorMethod.Signature);

            signatureBuilder.Flags      = MethodSignatureFlags.Static;
            signatureBuilder.ReturnType = constructorMethod.OwningType;

            _allocationMethod = constructorMethod.OwningType.GetKnownMethod("Ctor", signatureBuilder.ToSignature());
        }
Exemplo n.º 2
0
        private IReadOnlyList <MethodSignature> GetMethodSignatures(IComponentProvider provider)
        {
            var boolSymbol = _symbols.GetSymbol <bool>();
            var intSymbol  = _symbols.GetSymbol <int>();
            var memberType = _member.ReturnType !.Symbol;

            // Incoming Data type
            var discovery  = new MethodArg(RefKind.In, provider.DiscoveryType, "parent");
            var sourceType = new MethodArg(RefKind.In, _member.ContainingFullType.Symbol, "source");

            // Buffers
            var readOnlySpan = new MethodArg(_symbols.GetSymbol(typeof(ReadOnlySpan <byte>)), "readOnlySpan");
            var span         = new MethodArg(_symbols.GetSymbol(typeof(Span <byte>)), "span");
            var cursor       = new MethodArg(RefKind.Ref, _symbols.GetSymbol(typeof(SequenceCursor <byte>)), "cursor");

            // Return Data
            var readBytes    = new MethodArg(RefKind.Out, intSymbol, "readBytes");
            var writtenBytes = new MethodArg(RefKind.Out, intSymbol, "writtenBytes");
            var result       = new MethodArg(RefKind.Out, memberType, "result");

            return(new MethodSignature[] {
                MethodSignatureBuilder.CreateConstructor(_typeName, (provider.DiscoveryType, "parser")),
                MethodSignatureBuilder.CreateConstructor(_typeName, (provider.DiscoveryType, "parser"), (RefKind.Out, boolSymbol, "isSuccess")),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.TryDeserializeState(0))
                .AddArgument(discovery, readOnlySpan, readBytes, result)
                .WithReturn <bool>(),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.TryDeserializeState(0))
                .AddArgument(discovery, cursor, result)
                .WithReturn <bool>(),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.DeserializeState(0))
                .AddArgument(discovery, readOnlySpan, result)
                .WithReturn(memberType),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.DeserializeState(0))
                .AddArgument(discovery, cursor)
                .WithReturn(memberType),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.TrySerializeState(0))
                .AddArgument(discovery, sourceType, span, writtenBytes)
                .WithReturn(boolSymbol),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.SerializeState(0))
                .AddArgument(discovery, sourceType, span)
                .WithReturn(intSymbol),

                MethodSignatureBuilder.CreateMethod(_typeName, MemberMethod.GetLengthState(0))
                .AddArgument(discovery, sourceType)
                .WithReturn(intSymbol)
            });
Exemplo n.º 3
0
        public void CanBuildSignatureWithNoParams()
        {
            // arrange
            var signatureBuilder = new MethodSignatureBuilder();
            var methodDefinition = new MethodDefinition("Foo", MethodAttributes.Public, new TypeDefinition("System", "Int32", TypeAttributes.Public))
            {
                DeclaringType = new TypeDefinition("Namespace", "Class", TypeAttributes.Public)
            };

            // act
            var signature = signatureBuilder.CreateSignature(methodDefinition);

            // assert
            Assert.AreEqual("Namespace.Class.Foo;1;();System.Int32", signature);
        }
Exemplo n.º 4
0
        public void CanBuildSignatureWithMultipleParams()
        {
            // arrange
            var signatureBuilder = new MethodSignatureBuilder();
            var methodDefinition = new MethodDefinition("Foo", MethodAttributes.Public, new TypeDefinition("System", "Int32", TypeAttributes.Public))
            {
                DeclaringType = new TypeDefinition("Namespace", "Class", TypeAttributes.Public),
                Parameters    =
                {
                    new ParameterDefinition("bar1", ParameterAttributes.None, new TypeDefinition("System", "Int32",   TypeAttributes.Public)),
                    new ParameterDefinition("bar2", ParameterAttributes.None, new TypeDefinition("System", "Int16",   TypeAttributes.Public)),
                    new ParameterDefinition("bar3", ParameterAttributes.None, new TypeDefinition("System", "Boolean", TypeAttributes.Public))
                }
            };

            // act
            var signature = signatureBuilder.CreateSignature(methodDefinition);

            // assert
            Assert.AreEqual("Namespace.Class.Foo;1;(System.Int32,System.Int16,System.Boolean);System.Int32", signature);
        }
Exemplo n.º 5
0
        public void TestSignatureMatches()
        {
            MetadataType atomType    = _testModule.GetType("", "Atom");
            MetadataType aType       = _testModule.GetType("", "A`1");
            MetadataType aOfAtomType = aType.MakeInstantiatedType(new Instantiation(atomType));


            MetadataType           baseClassType            = _testModule.GetType("", "BaseClass`2");
            MethodDesc             baseClassMethod          = baseClassType.GetMethods().Single(m => string.Equals(m.Name, "Method"));
            MethodSignature        baseClassMethodSignature = baseClassMethod.Signature;
            MethodSignatureBuilder matchingSignatureBuilder = new MethodSignatureBuilder(baseClassMethodSignature);

            matchingSignatureBuilder[0] = aOfAtomType;
            matchingSignatureBuilder[1] = atomType;
            MethodSignature matchingSignature = matchingSignatureBuilder.ToSignature();

            MetadataType             derivedClassType    = _testModule.GetType("", "DerivedClass");
            IEnumerable <MethodDesc> derivedClassMethods = derivedClassType.GetMethods().Where(m => string.Equals(m.Name, "Method"));
            IEnumerable <bool>       matches             = derivedClassMethods.Select(m => matchingSignature.Equals(m.Signature));
            int matchCount = matches.Select(b => b ? 1 : 0).Sum();

            Assert.Equal(1, matchCount);
        }
Exemplo n.º 6
0
        public override MethodIL EmitIL()
        {
            // Target has the same signature as the Invoke method, except it's static.
            MethodSignatureBuilder builder = new MethodSignatureBuilder(Signature);

            builder.Flags = Signature.Flags | MethodSignatureFlags.Static;

            var          emitter    = new ILEmitter();
            ILCodeStream codeStream = emitter.NewCodeStream();

            // Load all arguments except 'this'
            for (int i = 0; i < Signature.Length; i++)
            {
                codeStream.EmitLdArg(i + 1);
            }

            // Indirectly call the delegate target static method.
            codeStream.EmitLdArg(0);
            codeStream.Emit(ILOpcode.ldfld, emitter.NewToken(ExtraFunctionPointerOrDataField));
            codeStream.Emit(ILOpcode.calli, emitter.NewToken(builder.ToSignature()));
            codeStream.Emit(ILOpcode.ret);

            return(emitter.Link(this));
        }
 private MethodSignature TryPattern(GeneratingTypeName fullName, IComponentProvider provider)
 => MethodSignatureBuilder.CreateConstructor(fullName, (provider.DiscoveryType, discovery), (RefKind.Out, _symbolSource.GetSymbol <bool>(), isSuccess));
 private MethodSignature NonTryPattern(GeneratingTypeName fullName, IComponentProvider provider)
 => MethodSignatureBuilder.CreateConstructor(fullName, (provider.DiscoveryType, discovery));