@@ -1377,6 +1377,8 @@ class BaseField(AbstractNodeData):
1377
1377
1378
1378
prefix = AbstractNodeData .PREFIX_FIELD
1379
1379
1380
+ _null = False
1381
+
1380
1382
def __init__ (self , repr = True , doc = None , type = None ,
1381
1383
access_needs_incref = False ):
1382
1384
"""
@@ -1432,6 +1434,15 @@ def __repr__(self):
1432
1434
def doc (self ):
1433
1435
return self ._doc
1434
1436
1437
+ @property
1438
+ def null (self ):
1439
+ """
1440
+ Return whether this field is always supposed to be null.
1441
+
1442
+ :rtype: bool
1443
+ """
1444
+ return self ._null
1445
+
1435
1446
1436
1447
class Field (BaseField ):
1437
1448
"""
@@ -1440,10 +1451,13 @@ class Field(BaseField):
1440
1451
"""
1441
1452
concrete = True
1442
1453
1443
- def __init__ (self , repr = True , doc = None , type = None , abstract = False ):
1454
+ def __init__ (self , repr = True , doc = None , type = None , abstract = False ,
1455
+ null = False ):
1444
1456
super (Field , self ).__init__ (repr , doc , type )
1445
1457
1458
+ assert not abstract or not null
1446
1459
self ._abstract = abstract
1460
+ self ._null = null
1447
1461
1448
1462
self ._overriding_computed = False
1449
1463
self ._overriding = None
@@ -1461,7 +1475,7 @@ def __init__(self, repr=True, doc=None, type=None, abstract=False):
1461
1475
self ._index = None
1462
1476
"""
1463
1477
0-based index for this parsing field in the owning AST node's children
1464
- list. This is -1 for abstract fields.
1478
+ list. This is -1 for abstract or null fields.
1465
1479
1466
1480
:type: int
1467
1481
"""
@@ -1537,14 +1551,15 @@ def inferred_type(self):
1537
1551
def index (self ):
1538
1552
"""
1539
1553
Return the 0-based index of this parsing field in the owning AST node's
1540
- children list. Only concrete fields have an index.
1554
+ children list. Only non-null concrete fields have an index.
1541
1555
1542
1556
:rtype: int
1543
1557
"""
1544
1558
assert self ._index is not None , (
1545
1559
'Index for {} is not computed' .format (self .qualname ))
1546
1560
assert self ._index != - 1 , (
1547
- 'Trying to get index of abstract field {}' .format (self .qualname ))
1561
+ 'Trying to get index of abstract/null field {}'
1562
+ .format (self .qualname ))
1548
1563
return self ._index
1549
1564
1550
1565
@property
@@ -2123,11 +2138,18 @@ def __init__(self, name, location, doc, base, fields,
2123
2138
for f_n , f_v in self ._fields .items ():
2124
2139
base_field = inherited_fields .get (f_n )
2125
2140
if isinstance (f_v , Field ):
2126
- f_v .overriding = (base_field
2127
- if (base_field and
2128
- isinstance (base_field , Field ) and
2129
- base_field .abstract )
2130
- else None )
2141
+ if (
2142
+ base_field and
2143
+ isinstance (base_field , Field ) and
2144
+ base_field .abstract
2145
+ ):
2146
+ f_v .overriding = base_field
2147
+ # Null fields are not initialized with a type, so they must
2148
+ # inherit their type from the abstract field they override.
2149
+ if f_v .null :
2150
+ f_v ._type = base_field ._type
2151
+ else :
2152
+ f_v .overriding = None
2131
2153
2132
2154
from langkit .dsl import Annotations
2133
2155
annotations = annotations or Annotations ()
0 commit comments