| 1 | """
 | 
| 2 | const.py
 | 
| 3 | """
 | 
| 4 | 
 | 
| 5 | DEFAULT_INT_WIDTH = 3  # 24 bits
 | 
| 6 | 
 | 
| 7 | # 2^24 - 1 is used as an invalid/uninitialized value for ASDL integers.
 | 
| 8 | 
 | 
| 9 | # Why?  We have a few use cases for invalid/sentinel values:
 | 
| 10 | # - span_id, line_id.  Sometimes we don't have a span ID.
 | 
| 11 | # - file descriptor: 'read x < f.txt' vs 'read x 0< f.txt'
 | 
| 12 | #
 | 
| 13 | # Other options for representation:
 | 
| 14 | #
 | 
| 15 | # 1. ADSL could use signed integers, then -1 is valid.
 | 
| 16 | # 2. Use a type like fd = None | Some(int fd)
 | 
| 17 | #
 | 
| 18 | # I don't like #1 because ASDL is lazily-decoded, and then we have to do sign
 | 
| 19 | # extension on demand.  (24 bits to 32 or 64).  As far as I can tell, sign
 | 
| 20 | # extension requires a branch, at least in portable C (on the sign bit).
 | 
| 21 | #
 | 
| 22 | # The second option is semantically cleaner.  But it needlessly
 | 
| 23 | # inflates the size of both the source code and the data.  Instead of having a
 | 
| 24 | # single "inline" integer, we would need a reference to another value.
 | 
| 25 | #
 | 
| 26 | # We could also try to do some fancy thing like fd = None |
 | 
| 27 | # Range<1..max_fd>(fd), with smart encoding.  But that is overkill for these
 | 
| 28 | # use cases.
 | 
| 29 | #
 | 
| 30 | # Using InvalidInt instead of -1 seems like a good compromise.
 | 
| 31 | 
 | 
| 32 | NO_INTEGER = (1 << (DEFAULT_INT_WIDTH * 8)) - 1
 | 
| 33 | 
 | 
| 34 | # NOTE: In Python: 1 << (n * 8) - 1 is wrong!  I thought that bit shift would
 | 
| 35 | # have higher precedence.
 |