Exemplo n.º 1
0
        private static void CreateDelegateRemoveMethod(Sink sink, ITypeDefinition type, HashSet <IMethodDefinition> delegates)
        {
            Bpl.Formal a = new Bpl.Formal(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, "a", sink.Heap.RefType), true);
            Bpl.Formal b = new Bpl.Formal(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, "b", sink.Heap.RefType), true);
            Bpl.Formal c = new Bpl.Formal(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, "c", sink.Heap.RefType), false);

            Bpl.IdentifierExpr aExpr = new Bpl.IdentifierExpr(Bpl.Token.NoToken, a);
            Bpl.IdentifierExpr bExpr = new Bpl.IdentifierExpr(Bpl.Token.NoToken, b);
            Bpl.IdentifierExpr cExpr = new Bpl.IdentifierExpr(Bpl.Token.NoToken, c);

            Bpl.Procedure proc = new Bpl.Procedure(
                Bpl.Token.NoToken,
                sink.DelegateRemove(type),
                new List <Bpl.TypeVariable>(),
                new List <Bpl.Variable>(new Bpl.Variable[] { a, b }),
                new List <Bpl.Variable>(new Bpl.Variable[] { c }),
                new List <Bpl.Requires>(),
                new List <Bpl.IdentifierExpr>(),
                new List <Bpl.Ensures>());
            proc.AddAttribute("inline", Bpl.Expr.Literal(1));
            sink.TranslatedProgram.AddTopLevelDeclaration(proc);

            Bpl.StmtListBuilder stmtBuilder = new Bpl.StmtListBuilder();
            stmtBuilder.Add(new Bpl.CallCmd(Bpl.Token.NoToken, "Alloc", new List <Bpl.Expr>(), new List <Bpl.IdentifierExpr>(new Bpl.IdentifierExpr[] { cExpr })));
            foreach (IMethodDefinition defn in delegates)
            {
                Bpl.IdentifierExpr cie = new Bpl.IdentifierExpr(Bpl.Token.NoToken, sink.FindOrCreateDelegateMethodConstant(defn));
                stmtBuilder.Add(new Bpl.AssumeCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Iff, sink.ReadMethod(cie, cExpr), Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.And, sink.ReadMethod(cie, aExpr), Bpl.Expr.Unary(Bpl.Token.NoToken, Bpl.UnaryOperator.Opcode.Not, sink.ReadMethod(cie, bExpr))))));
                stmtBuilder.Add(new Bpl.AssumeCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, sink.ReadReceiver(cie, cExpr), sink.ReadReceiver(cie, aExpr))));
                stmtBuilder.Add(new Bpl.AssumeCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, sink.ReadTypeParameters(cie, cExpr), sink.ReadTypeParameters(cie, aExpr))));
            }
            Bpl.IdentifierExpr nullExpr = new Bpl.IdentifierExpr(Bpl.Token.NoToken, sink.Heap.NullRef);
            Bpl.IfCmd          ifCmd    = BuildIfCmd(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, bExpr, nullExpr), TranslationHelper.BuildAssignCmd(cExpr, aExpr), stmtBuilder.Collect(Bpl.Token.NoToken));
            ifCmd = BuildIfCmd(Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, aExpr, nullExpr), TranslationHelper.BuildAssignCmd(cExpr, nullExpr), ifCmd);

            Bpl.Implementation impl = new Bpl.Implementation(
                Bpl.Token.NoToken,
                sink.DelegateRemove(type),
                new List <Bpl.TypeVariable>(),
                new List <Bpl.Variable>(new Bpl.Variable[] { a, b }),
                new List <Bpl.Variable>(new Bpl.Variable[] { c }),
                new List <Bpl.Variable>(),
                BuildStmtList(ifCmd)
                );
            impl.AddAttribute("inline", Bpl.Expr.Literal(1));
            impl.Proc = proc;
            sink.TranslatedProgram.AddTopLevelDeclaration(impl);
        }