Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate default constructors for contracts that don't have any #423

Merged
merged 28 commits into from
Feb 28, 2022

Conversation

ivoysey
Copy link
Collaborator

@ivoysey ivoysey commented Feb 23, 2022

This PR implements the production of default constructors for contracts that have no constructor. For example, on the test case

    contract TwoIntContainer{
        int x;
        int a;

        transaction set(int value) {
            x = value;
            a = value*value;
        }

        transaction get() returns int{
            return x+a;
        }
    }

    main contract ICGeneratedCons{
        int f;
        TwoIntContainer@Owned ic1;
        TwoIntContainer@Owned ic2;
        int x;

        transaction main() returns int{
            return (10);
        }
    }

you get

    function ICGeneratedCons___ICGeneratedCons0xfa583fc4(this, con_arg__f, con_arg_ic1_x, con_arg_ic1_a, con_arg_ic2_x, con_arg_ic2_a, con_arg__x) {
        // default constructor generated by the compiler
        // this.f := con_arg__f
        switch gt(add(this, 0), shl(255, 1))
        case true {sstore(add(this, 0), con_arg__f)}
        case false {mstore(add(this, 0), con_arg__f)}

        let _tmp_2 := allocate_memory(64)
        TwoIntContainer___TwoIntContainer0x5b1f0346(_tmp_2, con_arg_ic1_x, con_arg_ic1_a)
        let _tmp_1 := allocate_memory(64)
        TwoIntContainer___TwoIntContainer0x5b1f0346(_tmp_1, con_arg_ic2_x, con_arg_ic2_a)

        // this.x := con_arg__x
        switch gt(add(this, 96), shl(255, 1))
        case true {sstore(add(this, 96), con_arg__x)}
        case false {mstore(add(this, 96), con_arg__x)}
    }


    function TwoIntContainer___TwoIntContainer0x5b1f0346(this, con_arg__x, con_arg__a) {
        // default constructor generated by the compiler
        // this.x := con_arg__x
        switch gt(add(this, 0), shl(255, 1))
        case true {sstore(add(this, 0), con_arg__x)}
        case false {mstore(add(this, 0), con_arg__x)}

        // this.a := con_arg__a
        switch gt(add(this, 32), shl(255, 1))
        case true {sstore(add(this, 32), con_arg__a)}
        case false {mstore(add(this, 32), con_arg__a)}
    }

As house keeping matters this PR also includes

  • more uniform naming of transactions in the flattened object
  • an update to the test harness to be able to call transactions from the main contract with this change
  • removing a needless boolean variable threaded through translation to indicate if the current scope is from the main contract or not
  • documentation and helper functions in the utility file
  • general code hygiene

@ivoysey ivoysey requested a review from mcoblenz February 25, 2022 17:51
@ivoysey ivoysey linked an issue Feb 25, 2022 that may be closed by this pull request
@ivoysey ivoysey changed the title Insertconstructors generate default constructors for contracts that don't have any Feb 25, 2022
@ivoysey ivoysey marked this pull request as ready for review February 27, 2022 14:35
@ivoysey ivoysey merged commit 0256d7d into master Feb 28, 2022
@ivoysey ivoysey deleted the insertconstructors branch February 28, 2022 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants