Skip to content

Commit c8eb39f

Browse files
pmderodatRoldak
authored andcommitted
langkit.compiled_types: add data structures for null syntax fields
TN: R925-010
1 parent 32d1606 commit c8eb39f

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

langkit/compile_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,7 @@ def compute_astnode_constants(self):
20382038
for f in n.get_parse_fields():
20392039

20402040
# Compute the index
2041-
if f.abstract:
2041+
if f.abstract or f.null:
20422042
assert f._index in (None, -1)
20432043
f._index = -1
20442044
else:

langkit/compiled_types.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,8 @@ class BaseField(AbstractNodeData):
13771377

13781378
prefix = AbstractNodeData.PREFIX_FIELD
13791379

1380+
_null = False
1381+
13801382
def __init__(self, repr=True, doc=None, type=None,
13811383
access_needs_incref=False):
13821384
"""
@@ -1432,6 +1434,15 @@ def __repr__(self):
14321434
def doc(self):
14331435
return self._doc
14341436

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+
14351446

14361447
class Field(BaseField):
14371448
"""
@@ -1440,10 +1451,13 @@ class Field(BaseField):
14401451
"""
14411452
concrete = True
14421453

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):
14441456
super(Field, self).__init__(repr, doc, type)
14451457

1458+
assert not abstract or not null
14461459
self._abstract = abstract
1460+
self._null = null
14471461

14481462
self._overriding_computed = False
14491463
self._overriding = None
@@ -1461,7 +1475,7 @@ def __init__(self, repr=True, doc=None, type=None, abstract=False):
14611475
self._index = None
14621476
"""
14631477
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.
14651479
14661480
:type: int
14671481
"""
@@ -1537,14 +1551,15 @@ def inferred_type(self):
15371551
def index(self):
15381552
"""
15391553
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.
15411555
15421556
:rtype: int
15431557
"""
15441558
assert self._index is not None, (
15451559
'Index for {} is not computed'.format(self.qualname))
15461560
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))
15481563
return self._index
15491564

15501565
@property
@@ -2123,11 +2138,18 @@ def __init__(self, name, location, doc, base, fields,
21232138
for f_n, f_v in self._fields.items():
21242139
base_field = inherited_fields.get(f_n)
21252140
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
21312153

21322154
from langkit.dsl import Annotations
21332155
annotations = annotations or Annotations()

0 commit comments

Comments
 (0)