diff --git a/docs/contracts/libraries.rst b/docs/contracts/libraries.rst index bfeebdb77..2e77af746 100644 --- a/docs/contracts/libraries.rst +++ b/docs/contracts/libraries.rst @@ -208,6 +208,48 @@ Restrictions for libraries in comparison to contracts: (These might be lifted at a later point.) +.. _library-selectors: + +Function Signatures and Selectors in Libraries +============================================== + +While external calls to public or external library functions are possible, the calling convention for such calls +is considered to be internal to Solidity and not the same as specified for the regular :ref:`contract ABI`. +External library functions support more argument types than external contract functions, for example recursive structs +and storage pointers. For that reason, the function signatures used to compute the 4-byte selector are computed +following an internal naming schema and arguments of types not supported in the contract ABI use an internal encoding. + +The following identifiers are used for the types in the signatures: + + - Value types, non-storage ``string`` and non-storage ``bytes`` use the same identifiers as in the contract ABI. + - Non-storage array types follow the same convention as in the contract ABI, i.e. ``[]`` for dynamic arrays and + ``[M]`` for fixed-size arrays of ``M`` elements. + - Non-storage structs are referred to by their fully qualified name, i.e. ``C.S`` for ``contract C { struct S { ... } }``. + - Storage pointer types use the type identifier of their corresponding non-storage type, but append a single space + followed by ``storage`` to it. + +The argument encoding is the same as for the regular contract ABI, except for storage pointers, which are encoded as a +``uint256`` value referring to the storage slot to which they point. + +Similarly to the contract ABI, the selector consists of the first four bytes of the Keccak256-hash of the signature. +Its value can be obtained from Solidity using the ``.selector`` member as follows: + +:: + + pragma solidity >0.5.13 <0.7.0; + + library L { + function f(uint256) external {} + } + + contract C { + function g() public pure returns (bytes4) { + return L.f.selector; + } + } + + + .. _call-protection: Call Protection For Libraries