1.1 --- a/common.py Tue Dec 06 22:58:16 2016 +0100
1.2 +++ b/common.py Wed Dec 07 01:05:36 2016 +0100
1.3 @@ -227,7 +227,7 @@
1.4 # Return a reference for the constant.
1.5
1.6 objpath = self.get_object_path(constant_name)
1.7 - name_ref = ConstantValueRef(constant_name, ref.instance_of(), value)
1.8 + name_ref = ConstantValueRef(constant_name, ref.instance_of(objpath), value)
1.9
1.10 # Record the value and type for the constant.
1.11
2.1 --- a/generator.py Tue Dec 06 22:58:16 2016 +0100
2.2 +++ b/generator.py Wed Dec 07 01:05:36 2016 +0100
2.3 @@ -833,13 +833,13 @@
2.4
2.5 # References to constant literals.
2.6
2.7 - if kind == "<instance>":
2.8 - attr_path = "%s.%s" % (path, name)
2.9 + if kind == "<instance>" and ref.is_constant_alias():
2.10 + alias = ref.get_name()
2.11
2.12 # Obtain a constant value directly assigned to the attribute.
2.13
2.14 - if self.optimiser.constant_numbers.has_key(attr_path):
2.15 - constant_number = self.optimiser.constant_numbers[attr_path]
2.16 + if self.optimiser.constant_numbers.has_key(alias):
2.17 + constant_number = self.optimiser.constant_numbers[alias]
2.18 constant_value = "__const%d" % constant_number
2.19 return "%s /* %s */" % (constant_value, name)
2.20
3.1 --- a/importer.py Tue Dec 06 22:58:16 2016 +0100
3.2 +++ b/importer.py Wed Dec 07 01:05:36 2016 +0100
3.3 @@ -477,8 +477,28 @@
3.4 need initialising before the callable can be used.
3.5 """
3.6
3.7 - return ref.has_kind("<function>") and self.function_defaults.get(ref.get_origin()) or \
3.8 - ref.has_kind("<class>") and self.function_defaults.get("%s.__init__" % ref.get_origin())
3.9 + # Find the function or method associated with the reference.
3.10 +
3.11 + if ref.has_kind("<function>"):
3.12 + origin = ref.get_origin()
3.13 + elif ref.has_kind("<class>"):
3.14 + origin = "%s.__init__" % ref.get_origin()
3.15 + else:
3.16 + return False
3.17 +
3.18 + # Find any defaults for the function or method.
3.19 +
3.20 + defaults = self.function_defaults.get(origin)
3.21 + if not defaults:
3.22 + return False
3.23 +
3.24 + # Identify non-constant defaults.
3.25 +
3.26 + for name, ref in defaults:
3.27 + if not ref.is_constant_alias():
3.28 + return True
3.29 +
3.30 + return False
3.31
3.32 def order_modules(self):
3.33
4.1 --- a/referencing.py Tue Dec 06 22:58:16 2016 +0100
4.2 +++ b/referencing.py Wed Dec 07 01:05:36 2016 +0100
4.3 @@ -125,11 +125,14 @@
4.4 static = self.static()
4.5 return static and static.origin or None
4.6
4.7 - def instance_of(self):
4.8 + def instance_of(self, alias=None):
4.9
4.10 - "Return a reference to an instance of the referenced class."
4.11 + """
4.12 + Return a reference to an instance of the referenced class, indicating an
4.13 + 'alias' for the instance if specified.
4.14 + """
4.15
4.16 - return self.has_kind("<class>") and Reference("<instance>", self.origin) or None
4.17 + return self.has_kind("<class>") and Reference("<instance>", self.origin, alias) or None
4.18
4.19 def as_var(self):
4.20
4.21 @@ -196,6 +199,13 @@
4.22
4.23 return ancestors
4.24
4.25 + def is_constant_alias(self):
4.26 +
4.27 + "Return whether this reference is an alias for a constant."
4.28 +
4.29 + name = self.get_name()
4.30 + return name and name.rsplit(".")[-1].startswith("$c")
4.31 +
4.32 def get_types(self):
4.33
4.34 "Return class, instance-only and module types for this reference."
5.1 --- a/results.py Tue Dec 06 22:58:16 2016 +0100
5.2 +++ b/results.py Wed Dec 07 01:05:36 2016 +0100
5.3 @@ -130,6 +130,9 @@
5.4 def has_kind(self, kinds):
5.5 return self.ref and self.ref.has_kind(kinds)
5.6
5.7 + def is_constant_alias(self):
5.8 + return self.ref and self.ref.is_constant_alias()
5.9 +
5.10 class ResolvedNameRef(ResolvedRef, NameRef):
5.11
5.12 "A resolved name-based reference."
6.1 --- a/translator.py Tue Dec 06 22:58:16 2016 +0100
6.2 +++ b/translator.py Wed Dec 07 01:05:36 2016 +0100
6.3 @@ -98,9 +98,9 @@
6.4
6.5 # Determine whether a qualified name is involved.
6.6
6.7 - t = (self.get_name() or self.name).rsplit(".", 1)
6.8 + t = (not self.is_constant_alias() and self.get_name() or self.name).rsplit(".", 1)
6.9 parent = len(t) > 1 and t[0] or None
6.10 - attrname = encode_path(t[-1])
6.11 + attrname = t[-1] and encode_path(t[-1])
6.12
6.13 # Assignments.
6.14
6.15 @@ -1021,7 +1021,10 @@
6.16 else:
6.17 continue
6.18
6.19 - if name_ref:
6.20 + # Generate default initialisers except when constants are employed.
6.21 + # Constants should be used when populating the function structures.
6.22 +
6.23 + if name_ref and not isinstance(name_ref, TrConstantValueRef):
6.24 defaults.append("__SETDEFAULT(%s, %s, %s)" % (instance_name, i, name_ref))
6.25
6.26 return defaults