# Path: krakatau-lib/src/main/resources/Lib/Krakatau/ssa/objtypes.py
# def TypeTT(baset, dim):
# def baset(tt): return tt[0]
# def dim(tt): return tt[1]
# def withDimInc(tt, inc): return TypeTT(baset(tt), dim(tt)+inc)
# def withNoDim(tt): return TypeTT(baset(tt), 0)
# def isBaseTClass(tt): return not baset(tt).startswith('.')
# def className(tt): return baset(tt) if not baset(tt).startswith('.') else None
# def primName(tt): return baset(tt)[1:] if baset(tt).startswith('.') else None
# def isSubtype(env, x, y):
# def commonSupertype(env, tts):
# def verifierToSynthetic_seq(vtypes):
# def verifierToSynthetic(vtype):
# def declTypeToActual(env, decltype):
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/verifier/descriptors.py
# def parseFieldDescriptor(desc_str, unsynthesize=True):
#     rval = parseFieldDescriptors(desc_str, unsynthesize)
# 
#     cat = 2 if (rval and rval[0] in cat2tops) else 1
#     if len(rval) != cat:
#         raise ValueError('Incorrect number of fields in descriptor, expected {} but found {}'.format(cat, len(rval)))
#     return rval
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/java/reserved.py



import struct
        import traceback
from ..ssa import objtypes
from ..verifier.descriptors import parseFieldDescriptor
from . import ast, ast2, javamethod, throws
from .reserved import reserved_identifiers
def _getField(field):
    flags = [x.lower() for x in sorted(field.flags) if x not in ('SYNTHETIC','ENUM')]
    desc = field.descriptor
    dtype = objtypes.verifierToSynthetic(parseFieldDescriptor(desc, unsynthesize=False)[0])

    initexpr = None
    if field.static:
        cpool = field.class_.cpool
        const_attrs = [data for name,data in field.attributes if name == 'ConstantValue']
        if const_attrs:
            assert(len(const_attrs) == 1)
            data = const_attrs[0]
            index = struct.unpack('>h', data)[0]
            initexpr = loadConstValue(cpool, index)
    return ast2.FieldDef(' '.join(flags), ast.TypeName(dtype), field.class_, field.name, desc, initexpr)

def _getMethod(method, cb, forbidden_identifiers, skip_errors):
    try:
        graph = cb(method) if method.code is not None else None
        print 'Decompiling method', method.name.encode('utf8'), method.descriptor.encode('utf8')
        code_ast = javamethod.generateAST(method, graph, forbidden_identifiers)
        return code_ast
    except Exception as e:
        if not skip_errors:
            raise

        message = traceback.format_exc()
        code_ast = javamethod.generateAST(method, None, forbidden_identifiers)
        code_ast.comments.add(message)
        print message

# Path: krakatau-lib/src/main/resources/Lib/Krakatau/ssa/objtypes.py
# def TypeTT(baset, dim):
# def baset(tt): return tt[0]
# def dim(tt): return tt[1]
# def withDimInc(tt, inc): return TypeTT(baset(tt), dim(tt)+inc)
# def withNoDim(tt): return TypeTT(baset(tt), 0)
# def isBaseTClass(tt): return not baset(tt).startswith('.')
# def className(tt): return baset(tt) if not baset(tt).startswith('.') else None
# def primName(tt): return baset(tt)[1:] if baset(tt).startswith('.') else None
# def isSubtype(env, x, y):
# def commonSupertype(env, tts):
# def verifierToSynthetic_seq(vtypes):
# def verifierToSynthetic(vtype):
# def declTypeToActual(env, decltype):
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/verifier/descriptors.py
# def parseFieldDescriptor(desc_str, unsynthesize=True):
#     rval = parseFieldDescriptors(desc_str, unsynthesize)
# 
#     cat = 2 if (rval and rval[0] in cat2tops) else 1
#     if len(rval) != cat:
#         raise ValueError('Incorrect number of fields in descriptor, expected {} but found {}'.format(cat, len(rval)))
#     return rval
# 
# Path: krakatau-lib/src/main/resources/Lib/Krakatau/java/reserved.py



# Path: krakatau-lib/src/main/resources/Lib/Krakatau/java/javaclass.py
import struct
        import traceback
from ..ssa import objtypes
from ..verifier.descriptors import parseFieldDescriptor
from . import ast, ast2, javamethod, throws
from .reserved import reserved_identifiers

def _getField(field):
    flags = [x.lower() for x in sorted(field.flags) if x not in ('SYNTHETIC','ENUM')]
    desc = field.descriptor
    dtype = objtypes.verifierToSynthetic(parseFieldDescriptor(desc, unsynthesize=False)[0])

    initexpr = None
    if field.static:
        cpool = field.class_.cpool
        const_attrs = [data for name,data in field.attributes if name == 'ConstantValue']
        if const_attrs:
            assert(len(const_attrs) == 1)
            data = const_attrs[0]
            index = struct.unpack('>h', data)[0]
            initexpr = loadConstValue(cpool, index)
    return ast2.FieldDef(' '.join(flags), ast.TypeName(dtype), field.class_, field.name, desc, initexpr)

def _getMethod(method, cb, forbidden_identifiers, skip_errors):
    try:
        graph = cb(method) if method.code is not None else None
        print 'Decompiling method', method.name.encode('utf8'), method.descriptor.encode('utf8')
        code_ast = javamethod.generateAST(method, graph, forbidden_identifiers)
        return code_ast
    except Exception as e:
        if not skip_errors:
            raise

        message = traceback.format_exc()
        code_ast = javamethod.generateAST(method, None, forbidden_identifiers)
        code_ast.comments.add(message)
        print message

        return code_ast