"""
Arguments objects.
"""


class Signature(object):
    _immutable_ = True
    _immutable_fields_ = ["argnames[*]"]
    __slots__ = ("argnames", "varargname", "kwargname")

    def __init__(self, argnames, varargname=None, kwargname=None):
        self.argnames = argnames
        self.varargname = varargname
        self.kwargname = kwargname

    def find_argname(self, name):
        try:
            return self.argnames.index(name)
        except ValueError:
            return -1

    def num_argnames(self):
        return len(self.argnames)

    def has_vararg(self):
        return self.varargname is not None

    def has_kwarg(self):
        return self.kwargname is not None

    def scope_length(self):
        scopelen = len(self.argnames)
        scopelen += self.has_vararg()
        scopelen += self.has_kwarg()
        return scopelen

    def getallvarnames(self):
        argnames = self.argnames
        if self.varargname is not None:
            argnames = argnames + [self.varargname]
        if self.kwargname is not None:
            argnames = argnames + [self.kwargname]
        return argnames

    def __repr__(self):
        return "Signature(%r, %r, %r)" % (
                self.argnames, self.varargname, self.kwargname)

    def __eq__(self, other):
        if not isinstance(other, Signature):
            return NotImplemented
        return (self.argnames == other.argnames and
                self.varargname == other.varargname and
                self.kwargname == other.kwargname)

    def __ne__(self, other):
        if not isinstance(other, Signature):
            return NotImplemented
        return not self == other

    # make it look tuply for its use in the annotator

    def __len__(self):
        return 3

    def __getitem__(self, i):
        if i == 0:
            return self.argnames
        if i == 1:
            return self.varargname
        if i == 2:
            return self.kwargname
        raise IndexError


class CallSpec(object):
    """Represents the arguments passed into a function call, i.e. the
    `a, b, *c, **d` part in `return func(a, b, *c, **d)`.
    """
    def __init__(self, args_w, keywords=None, w_stararg=None,
            w_starstararg=None):
        self.w_stararg = w_stararg
        assert w_starstararg is None, "No **-unpacking in RPython"
        assert isinstance(args_w, list)
        self.arguments_w = args_w
        self.keywords = keywords or {}

    def flatten(self):
        """ Argument <-> list of w_objects together with "shape" information """
        shape_cnt  = len(self.arguments_w)    # Number of positional args
        shape_keys = tuple(sorted(self.keywords))
        shape_star = self.w_stararg is not None   # Flag: presence of *arg
        shape_stst = False # Flag: presence of **kwds
        data_w = self.arguments_w + [self.keywords[key] for key in shape_keys]
        if shape_star:
            data_w.append(self.w_stararg)
        return (shape_cnt, shape_keys, shape_star, shape_stst), data_w
