@@ -34,6 +34,61 @@ by the above rules, state variables from different contracts do share the same s
3434The elements of structs and arrays are stored after each other, just as if they were given
3535as individual values.
3636
37+ If a contract specifies a :ref: `custom storage layout<custom-storage-layout> `, the slots which
38+ the state variables occupy are shifted according the value defined as the layout base.
39+ The custom layout is specified in the most derived contract and, following the order explained
40+ above, starting from the most base-ward contract's variables, all storage slots are adjusted.
41+
42+ In the following example, contract ``C `` inherits from contracts ``A `` and ``B `` and also
43+ specifies a custom storage base slot.
44+ The result is that all variable storage slots of the inheritance tree will be adjusted according to
45+ the value specified by ``C ``.
46+
47+ .. code-block :: solidity
48+
49+ // SPDX-License-Identifier: GPL-3.0
50+ pragma solidity ^0.8.29;
51+
52+ struct S {
53+ int32 a;
54+ bool b;
55+ }
56+
57+ contract A {
58+ uint x;
59+ uint transient y;
60+ uint constant w = 10;
61+ uint immutable z = 12;
62+ }
63+
64+ contract B {
65+ uint16 i;
66+ uint16 j;
67+ S s;
68+ int8 k;
69+ }
70+
71+ contract C is A, B layout at 42 {
72+ uint8[10] register;
73+ bool flag;
74+ }
75+
76+ In the example, the storage layout starts with the inherited
77+ state variable ``x `` stored at the specified base slot ``42 ``.
78+ Transient, constant and immutable variables are stored in separate
79+ locations and, thus, ``y ``, ``w `` and ``z``don't interfere with the layout.
80+ The next variables ``i `` and ``j `` need 2 bytes each and can be packed in
81+ the next slot ``43 ``, at offsets ``0 `` and ``2 `` respectively.
82+ Since ``s `` is a struct, its two members are packed contiguously, demanding
83+ both 5 bytes.
84+ Even though they still could fit in slot ``43 ``, structs and static arrays
85+ always start a new slot as well as any other item after them.
86+ So, according to that ``s `` is placed at slot ``44 `` and the next variable,
87+ ``k `` at slot ``45 ``.
88+ Then, variable ``register `` which is an array of 10 positions, starts at slot ``46 ``
89+ and demands 80 bytes. Finally, variable, ``flag ``, start at slot ``47 ``, because,
90+ as explained before, variables after structs and arrays always start a new slot.
91+
3792.. warning ::
3893 When using elements that are smaller than 32 bytes, your contract's gas usage may be higher.
3994 This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller
0 commit comments