Create FormatField attributes on demand.
The InstructionFormat objects make their non-value operands available as FormatField attributes for use by predicates etc. Compute these on demand instead of up front. This makes it possible for the mypy tool to infer the types of these attributes from the __getattr__ signature.
This commit is contained in:
@@ -707,11 +707,24 @@ class InstructionFormat(object):
|
|||||||
self.members.append(member)
|
self.members.append(member)
|
||||||
yield k
|
yield k
|
||||||
|
|
||||||
# Create `FormatField` instances for the immediates.
|
def __getattr__(self, attr):
|
||||||
if isinstance(k, ImmediateKind):
|
# type: (str) -> FormatField
|
||||||
assert not hasattr(self, member), "Duplicate member name"
|
"""
|
||||||
field = FormatField(self, i, member)
|
Make instruction format members available as attributes.
|
||||||
setattr(self, member, field)
|
|
||||||
|
Each non-value format member becomes a corresponding `FormatField`
|
||||||
|
attribute.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
i = self.members.index(attr)
|
||||||
|
except ValueError:
|
||||||
|
raise AttributeError(
|
||||||
|
'{} is neither a {} member or a '
|
||||||
|
.format(attr, self.name) +
|
||||||
|
'normal InstructionFormat attribute')
|
||||||
|
field = FormatField(self, i, attr)
|
||||||
|
setattr(self, attr, field)
|
||||||
|
return field
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def lookup(ins, outs):
|
def lookup(ins, outs):
|
||||||
|
|||||||
Reference in New Issue
Block a user