From 2a8f8f79fffed3046b32f87705b3808526839971 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Wed, 26 Oct 2016 12:24:16 -0700 Subject: [PATCH] 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. --- lib/cretonne/meta/cretonne/__init__.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/cretonne/meta/cretonne/__init__.py b/lib/cretonne/meta/cretonne/__init__.py index e8a82f650e..a8865e1966 100644 --- a/lib/cretonne/meta/cretonne/__init__.py +++ b/lib/cretonne/meta/cretonne/__init__.py @@ -707,11 +707,24 @@ class InstructionFormat(object): self.members.append(member) yield k - # Create `FormatField` instances for the immediates. - if isinstance(k, ImmediateKind): - assert not hasattr(self, member), "Duplicate member name" - field = FormatField(self, i, member) - setattr(self, member, field) + def __getattr__(self, attr): + # type: (str) -> FormatField + """ + Make instruction format members available as attributes. + + 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 def lookup(ins, outs):