Consensys / solidity-parser-antlr

A Solidity parser for JS built on top of a robust ANTLR4 grammar
MIT License
24 stars 8 forks source link

Allow distinguishing between anonymous, fallback and receive functions #10

Open fvictorio opened 4 years ago

fvictorio commented 4 years ago

Before 0.6, the so-called fallback function (which I'll call anonymous here to avoid confusion) was the only mechanism for handling calls for functions that didn't exist:

function () external payable {}

In 0.6, the anonymous function doesn't exist anymore, and it's replaced by the fallback and receive functions:

fallback() external {}
receive() external payable {}

Right now the parser doesn't seem to give information about this, so in prettier-solidity we had to do something like this: https://github.com/prettier-solidity/prettier-plugin-solidity/pull/244

Not sure about the correct API here. My suggestion would be to have properties like isAnonymous, isFallbackKeyword and isReceiveKeyword (with better names).

A problem here is that the FunctionDefinition already has a lot of properties, and adding these three will probably add some redundancy too. But my suggestion is to just add them, and then do a cleanup of the API in the next major version.

cgewecke commented 4 years ago

This may be separate issue, but there also seems to be problem with 0.5.x contracts that use receive as a regular function name. Reported at eth-gas-reporter #196

This:

pragma solidity ^0.5.0;

contract A {
    function receive(address from, uint256 amount) public;
}

...crashes with

/Users/cgewecke/code/koba/node_modules/solidity-parser-diligence/dist/ASTBuilder.js:127
    return this.visit(ctx.children[0]);
                                  ^

TypeError: Cannot read property '0' of null
    at ASTBuilder.ContractPart (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:127:35)
    at ASTBuilder.visit (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:1304:38)
    at ASTBuilder.<anonymous> (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:1292:19)
    at Array.map (<anonymous>)
    at ASTBuilder.visit (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:1291:16)
    at ASTBuilder.ContractDefinition (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:113:22)
    at ASTBuilder.visit (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:1304:38)
    at ASTBuilder.<anonymous> (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:1292:19)
    at Array.map (<anonymous>)
    at ASTBuilder.visit (node_modules/solidity-parser-diligence/dist/ASTBuilder.js:1291:16)
blitz-1306 commented 4 years ago

Just a side-note that Solidity AST node, that is produced by SolcJS compiler, has kind property, that corresponds to used keyword, like function, receive, fallback or constructor. Since Solidity 0.7.1 there is a freeFunction value also possible.