1.1 --- a/templates/progops.c Sun Oct 30 13:14:45 2016 +0100
1.2 +++ b/templates/progops.c Sun Oct 30 17:54:27 2016 +0100
1.3 @@ -30,7 +30,7 @@
1.4 is specified, starting with any context argument.
1.5 */
1.6
1.7 -__attr __invoke(__attr callable,
1.8 +__attr __invoke(__attr callable, int always_callable,
1.9 unsigned int nkwargs, __param kwcodes[], __attr kwargs[],
1.10 unsigned int nargs, __attr args[])
1.11 {
1.12 @@ -96,7 +96,9 @@
1.13
1.14 /* Call with the prepared arguments. */
1.15
1.16 - return __load_via_object(callable.value, __pos___fn__).fn(allargs);
1.17 + return (always_callable ? __load_via_object(callable.value, __pos___fn__)
1.18 + : __check_and_load_via_object(callable.value, __pos___fn__, __code___fn__)
1.19 + ).fn(allargs);
1.20 }
1.21
1.22 /* Error routines. */
2.1 --- a/templates/progops.h Sun Oct 30 13:14:45 2016 +0100
2.2 +++ b/templates/progops.h Sun Oct 30 17:54:27 2016 +0100
2.3 @@ -5,7 +5,9 @@
2.4 /* Common operations. */
2.5
2.6 __attr __new(const __table *table, __ref cls, int size);
2.7 -__attr __invoke(__attr callable, unsigned int nkwargs, __param kwcodes[], __attr kwargs[], unsigned int nargs, __attr args[]);
2.8 +__attr __invoke(__attr callable, int always_callable,
2.9 + unsigned int nkwargs, __param kwcodes[], __attr kwargs[],
2.10 + unsigned int nargs, __attr args[]);
2.11
2.12 /* Error routines. */
2.13
3.1 --- a/translator.py Sun Oct 30 13:14:45 2016 +0100
3.2 +++ b/translator.py Sun Oct 30 17:54:27 2016 +0100
3.3 @@ -281,10 +281,18 @@
3.4
3.5 return self.importer.objects.get(self.get_namespace_path())
3.6
3.7 - def get_builtin(self, name):
3.8 - return self.importer.get_object("__builtins__.%s" % name)
3.9 + def get_builtin_class(self, name):
3.10 +
3.11 + "Return a reference to the actual object providing 'name'."
3.12 +
3.13 + # NOTE: This makes assumptions about the __builtins__ structure.
3.14
3.15 - def in_method(self, path):
3.16 + return self.importer.get_object("__builtins__.%s.%s" % (name, name))
3.17 +
3.18 + def is_method(self, path):
3.19 +
3.20 + "Return whether 'path' is a method."
3.21 +
3.22 class_name, method_name = path.rsplit(".", 1)
3.23 return self.importer.classes.has_key(class_name) and class_name
3.24
3.25 @@ -362,7 +370,7 @@
3.26 For node 'n', return a reference for the type of the given 'name'.
3.27 """
3.28
3.29 - ref = self.get_builtin(name)
3.30 + ref = self.get_builtin_class(name)
3.31
3.32 if name in ("dict", "list", "tuple"):
3.33 return self.process_literal_sequence_node(n, name, ref, TrLiteralSequenceRef)
3.34 @@ -842,12 +850,6 @@
3.35 else:
3.36 parameters = None
3.37
3.38 - stages = []
3.39 -
3.40 - # First, the invocation target is presented.
3.41 -
3.42 - stages.append("__tmp_target = %s" % expr)
3.43 -
3.44 # Arguments are presented in a temporary frame array with any context
3.45 # always being the first argument (although it may be set to null for
3.46 # invocations where it would be unused).
3.47 @@ -904,19 +906,20 @@
3.48 kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0"
3.49 kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0"
3.50
3.51 - # The callable is then obtained.
3.52 + # First, the invocation expression is presented.
3.53
3.54 - if target:
3.55 - callable = target
3.56 + stages = []
3.57 +
3.58 + # Without a known specific callable, the expression provides the target.
3.59
3.60 - elif self.always_callable:
3.61 - callable = "__load_via_object(__tmp_target.value, %s)" % \
3.62 - encode_symbol("pos", "__fn__")
3.63 + if not target:
3.64 + stages.append(str(expr))
3.65 +
3.66 + # Any specific callable is then obtained.
3.67 +
3.68 else:
3.69 - callable = "__check_and_load_via_object(__tmp_target.value, %s, %s)" % (
3.70 - encode_symbol("pos", "__fn__"), encode_symbol("code", "__fn__"))
3.71 -
3.72 - stages.append(callable)
3.73 + stages.append("__tmp_target = %s" % expr)
3.74 + stages.append(target)
3.75
3.76 # With a known target, the function is obtained directly and called.
3.77
3.78 @@ -927,8 +930,9 @@
3.79 # the callable and argument collections.
3.80
3.81 else:
3.82 - output = "__invoke(\n(\n%s\n),\n%d, %s, %s,\n%d, %s\n)" % (
3.83 + output = "__invoke(\n(\n%s\n),\n%d, %d, %s, %s,\n%d, %s\n)" % (
3.84 ",\n".join(stages),
3.85 + self.always_callable and 1 or 0,
3.86 len(kwargs), kwcodestr, kwargstr,
3.87 len(args), argstr)
3.88
3.89 @@ -1022,8 +1026,7 @@
3.90 # Convert literal references.
3.91
3.92 elif n.name.startswith("$L"):
3.93 - literal_name = n.name[len("$L"):]
3.94 - ref = self.importer.get_object("__builtins__.%s" % literal_name)
3.95 + ref = self.importer.get_module(self.name).special.get(n.name)
3.96 return TrResolvedNameRef(n.name, ref)
3.97
3.98 # Convert operator function names to references.
3.99 @@ -1323,7 +1326,7 @@
3.100
3.101 # Generate any self reference.
3.102
3.103 - if self.in_method(name):
3.104 + if self.is_method(name):
3.105 if define:
3.106 self.writeline("#define self (__args[0])")
3.107 else: