import ast


def get_declare_info(arg):
    """Given the first argument to the declare function, return the
    (kind, address_space, type, shape), information.

    kind: is a string, 'primitive' or 'matrix'
    address_space: is the address space string.
    type: is the c data type to use.
    shape: is a tuple with the shape of the matrix.  It is None for primitives.
    """
    address_space = ''
    shape = None
    if arg.startswith(('LOCAL_MEM', 'GLOBAL_MEM')):
        idx = arg.index(' ')
        address_space = arg[:idx]
        arg = arg[idx + 1:]
    if arg.startswith('matrix'):
        kind = 'matrix'
        m_arg = ast.literal_eval(arg[7:-1])
        if isinstance(m_arg, tuple) and \
                        len(m_arg) > 1 and \
                isinstance(m_arg[1], str):
            shape = m_arg[0]
            type = m_arg[1]
        else:
            shape = m_arg
            type = 'double'
    else:
        kind = 'primitive'
        type = arg

    return kind, address_space, type, shape