solidity/docs/layout-of-source-files.rst

281 lines
12 KiB
ReStructuredText
Raw Normal View History

2017-06-19 10:02:03 +00:00
********************************
2015-12-07 20:16:25 +00:00
Layout of a Solidity Source File
********************************
2018-09-13 16:29:14 +00:00
Source files can contain an arbitrary number of
2022-03-15 11:07:28 +00:00
:ref:`contract definitions<contract_structure>`, import_ ,
:ref:`pragma<pragma>` and :ref:`using for<using-for>` directives and
2021-02-04 14:58:06 +00:00
:ref:`struct<structs>`, :ref:`enum<enums>`, :ref:`function<functions>`, :ref:`error<errors>`
2020-09-30 19:31:28 +00:00
and :ref:`constant variable<constants>` definitions.
2018-09-13 16:29:14 +00:00
2020-05-13 14:53:54 +00:00
.. index:: ! license, spdx
SPDX License Identifier
=======================
2022-01-10 07:29:51 +00:00
Trust in smart contracts can be better established if their source code
2020-05-13 14:53:54 +00:00
is available. Since making source code available always touches on legal problems
2020-07-31 14:53:12 +00:00
with regards to copyright, the Solidity compiler encourages the use
2020-05-13 14:53:54 +00:00
of machine-readable `SPDX license identifiers <https://spdx.org>`_.
Every source file should start with a comment indicating its license:
``// SPDX-License-Identifier: MIT``
The compiler does not validate that the license is part of the
`list allowed by SPDX <https://spdx.org/licenses/>`_, but
it does include the supplied string in the :ref:`bytecode metadata <metadata>`.
2020-05-13 14:53:54 +00:00
If you do not want to specify a license or if the source code is
not open-source, please use the special value ``UNLICENSED``.
Note that ``UNLICENSED`` (no usage allowed, not present in SPDX license list)
is different from ``UNLICENSE`` (grants all rights to everyone).
Solidity follows `the npm recommendation <https://docs.npmjs.com/cli/v7/configuring-npm/package-json#license>`_.
2020-05-13 14:53:54 +00:00
Supplying this comment of course does not free you from other
obligations related to licensing like having to mention
a specific license header in each source file or the
original copyright holder.
The comment is recognized by the compiler anywhere in the file at the
file level, but it is recommended to put it at the top of the file.
More information about how to use SPDX license identifiers
can be found at the `SPDX website <https://spdx.org/ids-how>`_.
2018-09-13 16:29:14 +00:00
.. index:: ! pragma
.. _pragma:
Pragmas
=======
2016-08-19 17:57:21 +00:00
The ``pragma`` keyword is used to enable certain compiler features
or checks. A pragma directive is always local to a source file, so
you have to add the pragma to all your files if you want to enable it
2019-12-12 15:19:09 +00:00
in your whole project. If you :ref:`import<import>` another file, the pragma
from that file does *not* automatically apply to the importing file.
.. index:: ! pragma;version
2016-08-19 17:57:21 +00:00
2017-03-16 00:42:42 +00:00
.. _version_pragma:
2016-08-19 17:57:21 +00:00
Version Pragma
2018-09-13 16:29:14 +00:00
--------------
2016-08-19 17:57:21 +00:00
Source files can (and should) be annotated with a version pragma to reject
compilation with future compiler versions that might introduce incompatible
changes. We try to keep these to an absolute minimum and
introduce them in a way that changes in semantics also require changes
in the syntax, but this is not always possible. Because of this, it is always
2016-08-19 17:57:21 +00:00
a good idea to read through the changelog at least for releases that contain
breaking changes. These releases always have versions of the form
2016-08-19 17:57:21 +00:00
``0.x.0`` or ``x.0.0``.
2019-10-28 11:35:01 +00:00
The version pragma is used as follows: ``pragma solidity ^0.5.2;``
2016-08-19 17:57:21 +00:00
A source file with the line above does not compile with a compiler earlier than version 0.5.2,
and it also does not work on a compiler starting from version 0.6.0 (this
2019-12-12 15:19:09 +00:00
second condition is added by using ``^``). Because
there will be no breaking changes until version ``0.6.0``, you can
be sure that your code compiles the way you intended. The exact version of the
compiler is not fixed, so that bugfix releases are still possible.
2016-08-19 17:57:21 +00:00
It is possible to specify more complex rules for the compiler version,
2021-03-23 00:36:53 +00:00
these follow the same syntax used by `npm <https://docs.npmjs.com/cli/v6/using-npm/semver>`_.
2015-12-07 20:16:25 +00:00
.. note::
Using the version pragma *does not* change the version of the compiler.
It also *does not* enable or disable features of the compiler. It just
instructs the compiler to check whether its version matches the one
required by the pragma. If it does not match, the compiler issues
an error.
.. index:: ! ABI coder, ! pragma; abicoder, pragma; ABIEncoderV2
.. _abi_coder:
2020-10-29 18:40:09 +00:00
ABI Coder Pragma
----------------
2018-09-13 16:29:14 +00:00
2020-10-29 18:40:09 +00:00
By using ``pragma abicoder v1`` or ``pragma abicoder v2`` you can
select between the two implementations of the ABI encoder and decoder.
2018-09-13 16:29:14 +00:00
2020-10-29 18:40:09 +00:00
The new ABI coder (v2) is able to encode and decode arbitrarily nested
arrays and structs. Apart from supporting more types, it involves more extensive
validation and safety checks, which may result in higher gas costs, but also heightened
security. It is considered
non-experimental as of Solidity 0.6.0 and it is enabled by default starting
2022-08-30 08:23:15 +00:00
with Solidity 0.8.0. The old ABI coder can still be selected using ``pragma abicoder v1;``.
2018-09-13 16:29:14 +00:00
The set of types supported by the new encoder is a strict superset of
the ones supported by the old one. Contracts that use it can interact with ones
that do not without limitations. The reverse is possible only as long as the
2020-10-29 18:40:09 +00:00
non-``abicoder v2`` contract does not try to make calls that would require
decoding types only supported by the new encoder. The compiler can detect this
2020-10-29 18:40:09 +00:00
and will issue an error. Simply enabling ``abicoder v2`` for your contract is
enough to make the error go away.
.. note::
This pragma applies to all the code defined in the file where it is activated,
regardless of where that code ends up eventually. This means that a contract
2020-10-29 18:40:09 +00:00
whose source file is selected to compile with ABI coder v1
can still contain code that uses the new encoder
by inheriting it from another contract. This is allowed if the new types are only
used internally and not in external function signatures.
2020-10-29 18:40:09 +00:00
.. note::
Up to Solidity 0.7.4, it was possible to select the ABI coder v2
by using ``pragma experimental ABIEncoderV2``, but it was not possible
to explicitly select coder v1 because it was the default.
.. index:: ! pragma; experimental
2020-10-29 18:40:09 +00:00
.. _experimental_pragma:
Experimental Pragma
-------------------
The second pragma is the experimental pragma. It can be used to enable
features of the compiler or language that are not yet enabled by default.
The following experimental pragmas are currently supported:
.. index:: ! pragma; ABIEncoderV2
2020-10-29 18:40:09 +00:00
ABIEncoderV2
~~~~~~~~~~~~
Because the ABI coder v2 is not considered experimental anymore,
it can be selected via ``pragma abicoder v2`` (please see above)
since Solidity 0.7.4.
.. index:: ! pragma; SMTChecker
2018-09-27 12:55:05 +00:00
.. _smt_checker:
2018-09-13 16:29:14 +00:00
SMTChecker
~~~~~~~~~~
This component has to be enabled when the Solidity compiler is built
and therefore it is not available in all Solidity binaries.
The :ref:`build instructions<smt_solvers_build>` explain how to activate this option.
It is activated for the Ubuntu PPA releases in most versions,
2019-12-12 15:21:11 +00:00
but not for the Docker images, Windows binaries or the
statically-built Linux binaries. It can be activated for solc-js via the
`smtCallback <https://github.com/ethereum/solc-js#example-usage-with-smtsolver-callback>`_ if you have an SMT solver
installed locally and run solc-js via node (not via the browser).
2018-09-13 16:29:14 +00:00
2019-03-20 21:44:16 +00:00
If you use ``pragma experimental SMTChecker;``, then you get additional
:ref:`safety warnings<formal_verification>` which are obtained by querying an
SMT solver.
The component does not yet support all features of the Solidity language and
likely outputs many warnings. In case it reports unsupported features, the
analysis may not be fully sound.
2018-09-13 16:29:14 +00:00
.. index:: source file, ! import, module, source unit
2015-12-07 20:16:25 +00:00
.. _import:
2015-12-07 20:16:25 +00:00
Importing other Source Files
============================
2016-01-11 21:33:36 +00:00
Syntax and Semantics
--------------------
2015-12-07 20:16:25 +00:00
2019-12-12 15:19:09 +00:00
Solidity supports import statements to help modularise your code that
are similar to those available in JavaScript
(from ES6 on). However, Solidity does not support the concept of
a `default export <https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export#description>`_.
2015-12-07 20:16:25 +00:00
2016-01-11 21:33:36 +00:00
At a global level, you can use import statements of the following form:
2015-12-07 20:16:25 +00:00
.. code-block:: solidity
2016-01-11 21:33:36 +00:00
import "filename";
2016-01-11 21:33:36 +00:00
The ``filename`` part is called an *import path*.
This statement imports all global symbols from "filename" (and symbols imported there) into the
current global scope (different than in ES6 but backwards-compatible for Solidity).
This form is not recommended for use, because it unpredictably pollutes the namespace.
If you add new top-level items inside "filename", they automatically
appear in all files that import like this from "filename". It is better to import specific
symbols explicitly.
The following example creates a new global symbol ``symbolName`` whose members are all
the global symbols from ``"filename"``:
.. code-block:: solidity
import * as symbolName from "filename";
which results in all global symbols being available in the format ``symbolName.symbol``.
A variant of this syntax that is not part of ES6, but possibly useful is:
2016-01-11 21:33:36 +00:00
.. code-block:: solidity
import "filename" as symbolName;
which is equivalent to ``import * as symbolName from "filename";``.
2016-01-11 21:33:36 +00:00
If there is a naming collision, you can rename symbols while importing. For example,
the code below creates new global symbols ``alias`` and ``symbol2`` which reference
``symbol1`` and ``symbol2`` from inside ``"filename"``, respectively.
.. code-block:: solidity
import {symbol1 as alias, symbol2} from "filename";
2018-10-25 16:28:24 +00:00
.. index:: virtual filesystem, source unit name, import; path, filesystem path, import callback, Remix IDE
Import Paths
------------
In order to be able to support reproducible builds on all platforms, the Solidity compiler has to
abstract away the details of the filesystem where source files are stored.
For this reason import paths do not refer directly to files in the host filesystem.
Instead the compiler maintains an internal database (*virtual filesystem* or *VFS* for short) where
each source unit is assigned a unique *source unit name* which is an opaque and unstructured identifier.
The import path specified in an import statement is translated into a source unit name and used to
find the corresponding source unit in this database.
Using the :ref:`Standard JSON <compiler-api>` API it is possible to directly provide the names and
content of all the source files as a part of the compiler input.
In this case source unit names are truly arbitrary.
If, however, you want the compiler to automatically find and load source code into the VFS, your
source unit names need to be structured in a way that makes it possible for an :ref:`import callback
<import-callback>` to locate them.
When using the command-line compiler the default import callback supports only loading source code
from the host filesystem, which means that your source unit names must be paths.
Some environments provide custom callbacks that are more versatile.
For example the `Remix IDE <https://remix.ethereum.org/>`_ provides one that
lets you `import files from HTTP, IPFS and Swarm URLs or refer directly to packages in NPM registry
<https://remix-ide.readthedocs.io/en/latest/import.html>`_.
For a complete description of the virtual filesystem and the path resolution logic used by the
compiler see :ref:`Path Resolution <path-resolution>`.
2015-12-07 20:16:25 +00:00
.. index:: ! comment, natspec
Comments
========
Single-line comments (``//``) and multi-line comments (``/*...*/``) are possible.
2015-12-07 20:16:25 +00:00
.. code-block:: solidity
2016-02-18 10:08:20 +00:00
// This is a single-line comment.
2016-05-18 15:35:00 +00:00
/*
This is a
multi-line comment.
*/
2016-05-18 15:35:00 +00:00
.. note::
A single-line comment is terminated by any unicode line terminator
(LF, VF, FF, CR, NEL, LS or PS) in UTF-8 encoding. The terminator is still part of
the source code after the comment, so if it is not an ASCII symbol
(these are NEL, LS and PS), it will lead to a parser error.
2016-02-18 10:08:20 +00:00
Additionally, there is another type of comment called a NatSpec comment,
which is detailed in the :ref:`style guide<style_guide_natspec>`. They are written with a
triple slash (``///``) or a double asterisk block (``/** ... */``) and
2016-05-18 15:35:00 +00:00
they should be used directly above function declarations or statements.