Esempio n. 1
0
        protected override void EmitRead(UcAsp.RPC.ProtoBuf.Compiler.CompilerContext ctx, UcAsp.RPC.ProtoBuf.Compiler.Local valueFrom)
        {
            /* This looks more complex than it is. Look at the non-compiled Read to
             * see what it is trying to do, but note that it needs to cope with a
             * few more scenarios. Note that it picks the **most specific** Add,
             * unlike the runtime version that uses IList when possible. The core
             * is just a "do {list.Add(readValue())} while {thereIsMore}"
             *
             * The complexity is due to:
             *  - value types vs reference types (boxing etc)
             *  - initialization if we need to pass in a value to the tail
             *  - handling whether or not the tail *returns* the value vs updates the input
             */
            bool returnList = ReturnList;

            using (Compiler.Local list = AppendToCollection ? ctx.GetLocalWithValue(ExpectedType, valueFrom) : new Compiler.Local(ctx, declaredType))
                using (Compiler.Local origlist = (returnList && AppendToCollection && !Helpers.IsValueType(ExpectedType)) ? new Compiler.Local(ctx, ExpectedType) : null)
                {
                    if (!AppendToCollection)
                    { // always new
                        ctx.LoadNullRef();
                        ctx.StoreValue(list);
                    }
                    else if (returnList && origlist != null)
                    { // need a copy
                        ctx.LoadValue(list);
                        ctx.StoreValue(origlist);
                    }
                    if (concreteType != null)
                    {
                        ctx.LoadValue(list);
                        Compiler.CodeLabel notNull = ctx.DefineLabel();
                        ctx.BranchIfTrue(notNull, true);
                        ctx.EmitCtor(concreteType);
                        ctx.StoreValue(list);
                        ctx.MarkLabel(notNull);
                    }

                    bool castListForAdd = !add.DeclaringType.IsAssignableFrom(declaredType);
                    EmitReadList(ctx, list, Tail, add, packedWireType, castListForAdd);

                    if (returnList)
                    {
                        if (AppendToCollection && origlist != null)
                        {
                            // remember ^^^^ we had a spare copy of the list on the stack; now we'll compare
                            ctx.LoadValue(origlist);
                            ctx.LoadValue(list); // [orig] [new-value]
                            Compiler.CodeLabel sameList = ctx.DefineLabel(), allDone = ctx.DefineLabel();
                            ctx.BranchIfEqual(sameList, true);
                            ctx.LoadValue(list);
                            ctx.Branch(allDone, true);
                            ctx.MarkLabel(sameList);
                            ctx.LoadNullRef();
                            ctx.MarkLabel(allDone);
                        }
                        else
                        {
                            ctx.LoadValue(list);
                        }
                    }
                }
        }