Exemple #1
0
        private List <BoogieCallCmd> GenerateConstructorCall(ContractDefinition contract)
        {
            List <BoogieCallCmd> localStmtList = new List <BoogieCallCmd>();
            string            callee           = TransUtils.GetCanonicalConstructorName(contract);
            List <BoogieExpr> inputs           = new List <BoogieExpr>()
            {
                new BoogieIdentifierExpr("this"),
                new BoogieIdentifierExpr("msgsender_MSG"),
                new BoogieIdentifierExpr("msgvalue_MSG"),
            };

            if (context.IsConstructorDefined(contract))
            {
                FunctionDefinition ctor = context.GetConstructorByContract(contract);
                foreach (VariableDeclaration param in ctor.Parameters.Parameters)
                {
                    string name = TransUtils.GetCanonicalLocalVariableName(param);
                    inputs.Add(new BoogieIdentifierExpr(name));

                    if (param.TypeName is ArrayTypeName array)
                    {
                        localStmtList.Add(new BoogieCallCmd(
                                              "FreshRefGenerator",
                                              new List <BoogieExpr>(), new List <BoogieIdentifierExpr>()
                        {
                            new BoogieIdentifierExpr(name)
                        }));
                    }
                }
            }
            localStmtList.Add(new BoogieCallCmd(callee, inputs, null));
            return(localStmtList);
        }
Exemple #2
0
        private List <BoogieCmd> GenerateConstructorCall(ContractDefinition contract)
        {
            List <BoogieCmd>  localStmtList = new List <BoogieCmd>();
            string            callee        = TransUtils.GetCanonicalConstructorName(contract);
            List <BoogieExpr> inputs        = new List <BoogieExpr>()
            {
                new BoogieIdentifierExpr("this"),
                new BoogieIdentifierExpr("msgsender_MSG"),
                new BoogieIdentifierExpr("msgvalue_MSG"),
            };

            if (context.IsConstructorDefined(contract))
            {
                FunctionDefinition ctor = context.GetConstructorByContract(contract);
                foreach (VariableDeclaration param in ctor.Parameters.Parameters)
                {
                    string name = TransUtils.GetCanonicalLocalVariableName(param, context);
                    inputs.Add(new BoogieIdentifierExpr(name));

                    if (param.TypeName is ArrayTypeName array)
                    {
                        localStmtList.Add(new BoogieCallCmd(
                                              "FreshRefGenerator",
                                              new List <BoogieExpr>(), new List <BoogieIdentifierExpr>()
                        {
                            new BoogieIdentifierExpr(name)
                        }));
                    }
                }
            }

            if (context.TranslateFlags.InstrumentGas)
            {
                var gasVar = new BoogieIdentifierExpr("gas");
                localStmtList.Add(new BoogieAssignCmd(gasVar, new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.SUB, gasVar, new BoogieLiteralExpr(TranslatorContext.CREATE_GAS_COST))));
            }

            localStmtList.Add(new BoogieCallCmd(callee, inputs, null));
            return(localStmtList);
        }
Exemple #3
0
        private List <BoogieCmd> GenerateConstructorCall(ContractDefinition contract)
        {
            List <BoogieCmd>  localStmtList = new List <BoogieCmd>();
            string            callee        = TransUtils.GetCanonicalConstructorName(contract);
            List <BoogieExpr> inputs        = new List <BoogieExpr>()
            {
                new BoogieIdentifierExpr("this"),
                new BoogieIdentifierExpr("msgsender_MSG"),
                new BoogieIdentifierExpr("msgvalue_MSG"),
            };

            if (context.IsConstructorDefined(contract))
            {
                FunctionDefinition ctor = context.GetConstructorByContract(contract);
                if (ctor.StateMutability.Equals(EnumStateMutability.PAYABLE))
                {
                    BoogieExpr assumeExpr = new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.GE,
                                                                      new BoogieIdentifierExpr("msgvalue_MSG"), new BoogieLiteralExpr(BigInteger.Zero));
                    localStmtList.Add(new BoogieAssumeCmd(assumeExpr));

                    localStmtList.Add(new BoogieCommentCmd("---- Logic for payable function START "));
                    var balnSender = new BoogieMapSelect(new BoogieIdentifierExpr("Balance"), new BoogieIdentifierExpr("msgsender_MSG"));
                    var balnThis   = new BoogieMapSelect(new BoogieIdentifierExpr("Balance"), new BoogieIdentifierExpr("this"));
                    var msgVal     = new BoogieIdentifierExpr("msgvalue_MSG");
                    //assume Balance[msg.sender] >= msg.value
                    localStmtList.Add(new BoogieAssumeCmd(new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.GE, balnSender, msgVal)));
                    //balance[msg.sender] = balance[msg.sender] - msg.value
                    localStmtList.Add(new BoogieAssignCmd(balnSender, new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.SUB, balnSender, msgVal)));
                    //balance[this] = balance[this] + msg.value
                    localStmtList.Add(new BoogieAssignCmd(balnThis, new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.ADD, balnThis, msgVal)));
                    localStmtList.Add(new BoogieCommentCmd("---- Logic for payable function END "));
                }
                else
                {
                    BoogieExpr assumeExpr = new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.EQ,
                                                                      new BoogieIdentifierExpr("msgvalue_MSG"), new BoogieLiteralExpr(BigInteger.Zero));
                    localStmtList.Add(new BoogieAssumeCmd(assumeExpr));
                }

                foreach (VariableDeclaration param in ctor.Parameters.Parameters)
                {
                    string name = TransUtils.GetCanonicalLocalVariableName(param, context);
                    inputs.Add(new BoogieIdentifierExpr(name));

                    if (param.TypeName is ArrayTypeName array)
                    {
                        localStmtList.Add(new BoogieCallCmd(
                                              "FreshRefGenerator",
                                              new List <BoogieExpr>(), new List <BoogieIdentifierExpr>()
                        {
                            new BoogieIdentifierExpr(name)
                        }));
                    }
                }
            }
            else
            {
                BoogieExpr assumeExpr = new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.EQ,
                                                                  new BoogieIdentifierExpr("msgvalue_MSG"), new BoogieLiteralExpr(BigInteger.Zero));
                localStmtList.Add(new BoogieAssumeCmd(assumeExpr));
            }

            if (context.TranslateFlags.InstrumentGas)
            {
                var gasVar = new BoogieIdentifierExpr("gas");
                localStmtList.Add(new BoogieAssignCmd(gasVar, new BoogieBinaryOperation(BoogieBinaryOperation.Opcode.SUB, gasVar, new BoogieLiteralExpr(TranslatorContext.CREATE_GAS_COST))));
            }

            localStmtList.Add(new BoogieCallCmd(callee, inputs, null));
            return(localStmtList);
        }