Why are some IDs and referenced declarations negative numbers in AST?

Each solc process instantiates the parser once, for all source files,
And the node ids of the parser start with 0 then auto increment

So it appears as though you shouldn’t have negatives

These are assigned to definitions of global builtins, which do not have definitions in the source file and only exist internally so that the compiler can point at something. They get negative IDs to differentiate them from user’s definitions. See GlobalContext.cpp for a full list:

You won’t ever see them in AST node’s id field though. Only in referencedDeclaration and possibly some other fields that refer to IDs of other AST nodes.

1 Like