| 1 |
""" |
| 2 |
Arguments objects. |
| 3 |
""" |
| 4 |
from rpython.flowspace.model import const |
| 5 |
|
| 6 |
class Signature(object): |
| 7 |
_immutable_ = True |
| 8 |
_immutable_fields_ = ["argnames[*]"] |
| 9 |
__slots__ = ("argnames", "varargname", "kwargname") |
| 10 |
|
| 11 |
def __init__(self, argnames, varargname=None, kwargname=None): |
| 12 |
self.argnames = argnames |
| 13 |
self.varargname = varargname |
| 14 |
self.kwargname = kwargname |
| 15 |
|
| 16 |
def find_argname(self, name): |
| 17 |
try: |
| 18 |
return self.argnames.index(name) |
| 19 |
except ValueError: |
| 20 |
return -1 |
| 21 |
|
| 22 |
def num_argnames(self): |
| 23 |
return len(self.argnames) |
| 24 |
|
| 25 |
def has_vararg(self): |
| 26 |
return self.varargname is not None |
| 27 |
|
| 28 |
def has_kwarg(self): |
| 29 |
return self.kwargname is not None |
| 30 |
|
| 31 |
def scope_length(self): |
| 32 |
scopelen = len(self.argnames) |
| 33 |
scopelen += self.has_vararg() |
| 34 |
scopelen += self.has_kwarg() |
| 35 |
return scopelen |
| 36 |
|
| 37 |
def getallvarnames(self): |
| 38 |
argnames = self.argnames |
| 39 |
if self.varargname is not None: |
| 40 |
argnames = argnames + [self.varargname] |
| 41 |
if self.kwargname is not None: |
| 42 |
argnames = argnames + [self.kwargname] |
| 43 |
return argnames |
| 44 |
|
| 45 |
def __repr__(self): |
| 46 |
return "Signature(%r, %r, %r)" % ( |
| 47 |
self.argnames, self.varargname, self.kwargname) |
| 48 |
|
| 49 |
def __eq__(self, other): |
| 50 |
if not isinstance(other, Signature): |
| 51 |
return NotImplemented |
| 52 |
return (self.argnames == other.argnames and |
| 53 |
self.varargname == other.varargname and |
| 54 |
self.kwargname == other.kwargname) |
| 55 |
|
| 56 |
def __ne__(self, other): |
| 57 |
if not isinstance(other, Signature): |
| 58 |
return NotImplemented |
| 59 |
return not self == other |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
def __len__(self): |
| 64 |
return 3 |
| 65 |
|
| 66 |
def __getitem__(self, i): |
| 67 |
if i == 0: |
| 68 |
return self.argnames |
| 69 |
if i == 1: |
| 70 |
return self.varargname |
| 71 |
if i == 2: |
| 72 |
return self.kwargname |
| 73 |
raise IndexError |
| 74 |
|
| 75 |
|
| 76 |
class CallSpec(object): |
| 77 |
"""Represents the arguments passed into a function call, i.e. the |
| 78 |
`a, b, *c, **d` part in `return func(a, b, *c, **d)`. |
| 79 |
""" |
| 80 |
def __init__(self, args_w, keywords=None, w_stararg=None): |
| 81 |
self.w_stararg = w_stararg |
| 82 |
assert isinstance(args_w, list) |
| 83 |
self.arguments_w = args_w |
| 84 |
self.keywords = keywords or {} |
| 85 |
|
| 86 |
def __repr__(self): |
| 87 |
""" NOT_RPYTHON """ |
| 88 |
name = self.__class__.__name__ |
| 89 |
if not self.keywords: |
| 90 |
return '%s(%s)' % (name, self.arguments_w,) |
| 91 |
else: |
| 92 |
return '%s(%s, %s)' % (name, self.arguments_w, self.keywords) |
| 93 |
|
| 94 |
def flatten(self): |
| 95 |
""" Argument <-> list of w_objects together with "shape" information """ |
| 96 |
shape_cnt, shape_keys, shape_star = self._rawshape() |
| 97 |
data_w = self.arguments_w + [self.keywords[key] for key in shape_keys] |
| 98 |
if shape_star: |
| 99 |
data_w.append(self.w_stararg) |
| 100 |
return (shape_cnt, shape_keys, shape_star), data_w |
| 101 |
|
| 102 |
def _rawshape(self): |
| 103 |
shape_cnt = len(self.arguments_w) |
| 104 |
shape_keys = tuple(sorted(self.keywords)) |
| 105 |
shape_star = self.w_stararg is not None |
| 106 |
return shape_cnt, shape_keys, shape_star |
| 107 |
|
| 108 |
def as_list(self): |
| 109 |
assert not self.keywords |
| 110 |
if self.w_stararg is None: |
| 111 |
return self.arguments_w |
| 112 |
else: |
| 113 |
return self.arguments_w + [const(x) for x in self.w_stararg.value] |
| 114 |
|
| 115 |
@classmethod |
| 116 |
def fromshape(cls, (shape_cnt, shape_keys, shape_star), data_w): |
| 117 |
args_w = data_w[:shape_cnt] |
| 118 |
p = end_keys = shape_cnt + len(shape_keys) |
| 119 |
if shape_star: |
| 120 |
w_star = data_w[p] |
| 121 |
p += 1 |
| 122 |
else: |
| 123 |
w_star = None |
| 124 |
return cls(args_w, dict(zip(shape_keys, data_w[shape_cnt:end_keys])), |
| 125 |
w_star) |
| 126 |
|