# Path: javadecompiler/Krakatau/classfile.py
# class ClassFile(object):
#     flagVals = {'PUBLIC':0x0001,
#                 'FINAL':0x0010,
#                 'SUPER':0x0020,
#                 'INTERFACE':0x0200,
#                 'ABSTRACT':0x0400,
#                 'SYNTHETIC':0x1000,
#                 'ANNOTATION':0x2000,
#                 'ENUM':0x4000,
# 
#                 # These flags are only used for InnerClasses attributes
#                 'PRIVATE':0x0002,
#                 'PROTECTED':0x0004,
#                 'STATIC':0x0008,
#                 }
# 
#     def __init__(self, bytestream):
#         magic, minor, major = bytestream.get('>LHH')
#         assert(magic == 0xCAFEBABE)
#         self.version = major,minor
# 
#         const_pool_raw = get_cp_raw(bytestream)
#         flags, self.this, self.super = bytestream.get('>HHH')
# 
#         interface_count = bytestream.get('>H')
#         self.interfaces_raw = [bytestream.get('>H') for _ in range(interface_count)]
# 
#         self.fields_raw = get_fields_raw(bytestream)
#         self.methods_raw = get_methods_raw(bytestream)
# 
#         ic_indices = [i for i,x in enumerate(const_pool_raw) if x == (1, ("InnerClasses",))]
#         self.attributes_raw = get_attributes_raw(bytestream, ic_indices)
#         assert(bytestream.size() == 0)
# 
#         self.flags = set(name for name,mask in ClassFile.flagVals.items() if (mask & flags))
#         self.cpool = constant_pool.ConstPool(const_pool_raw)
#         self.name = self.cpool.getArgsCheck('Class', self.this)
#         self.elementsLoaded = False
# 
#         self.env = self.supername = self.hierarchy = None
#         self.fields = self.methods = self.attributes = None
# 
#     def loadSupers(self, env, name, subclasses):
#         self.env = env
#         assert(self.name == name)
# 
#         if self.super:
#             self.supername = self.cpool.getArgsCheck('Class', self.super)
#             # if superclass is cached, we can assume it is free from circular inheritance
#             # since it must have been loaded successfully on a previous run
#             if not self.env.isCached(self.supername):
#                 self.env.getClass(self.supername, subclasses + (name,), partial=True)
#             self.hierarchy = self.env.getSupers(self.supername) + (self.name,)
#         else:
#             assert(name == 'java/lang/Object')
#             self.supername = None
#             self.hierarchy = (self.name,)
# 
#     def loadElements(self, keepRaw=False):
#         if self.elementsLoaded:
#             return
#         self.fields = [field.Field(m, self, keepRaw) for m in self.fields_raw]
#         self.methods = [method.Method(m, self, keepRaw) for m in self.methods_raw]
#         self.attributes = fixAttributeNames(self.attributes_raw, self.cpool)
# 
#         self.fields_raw = self.methods_raw = None
#         if not keepRaw:
#             self.attributes_raw = None
#         self.elementsLoaded = True
# 
#     def getSuperclassHierarchy(self):
#         return self.hierarchy
# 
# Path: javadecompiler/Krakatau/assembler/assembler.py
# class PoolRef(object):
#     def __init__(self, *args, **kwargs):
#         self.index = kwargs.get('index')
#         self.lbl = kwargs.get('lbl')
#         self.args = args
# 
#     def toIndex(self, pool, forbidden=(), **kwargs):
#         if self.index is not None:
#             return self.index
#         if self.lbl:
#             self.index = pool.getLabel(self.lbl, forbidden, **kwargs)
#         else:
#             self.args = [(x.toIndex(pool) if isinstance(x, PoolRef) else x) for x in self.args]
#             self.index = pool.getItem(*self.args, **kwargs)
#         return self.index


import ast, struct
import itertools
    import inspect
from ..classfile import ClassFile
from ..method import Method
from ..field import Field
from .tokenize import tokens, wordget, flags
from .assembler import PoolRef
from .codes import handle_codes
from .codes import newarr_codes
from .codes import et_tags
    from ply import yacc
    from ply import yacc

#Generic
def p_generic_attribute_dir(p):
    '''generic_attribute_dir : D_ATTRIBUTE utf8ref STRING_LITERAL'''
    p[0] = p[1], (p[2], p[3])

def p_generic_codeattribute_dir(p):
    '''generic_codeattribute_dir : D_CODEATTRIBUTE utf8ref STRING_LITERAL'''
    p[0] = p[1], (p[2], p[3])

#######################################################################
#Stack map stuff
addRule(nothing, 'endstack', 'D_END STACK') #directives are not expected to end with a sep

def assign1All(p):p[0] = tuple(p[1:])
addRule(assign1All, 'verification_type', 'TOP', 'INTEGER', 'FLOAT', 'DOUBLE', 'LONG', 'NULL', 'UNINITIALIZEDTHIS',
                                        'OBJECT classref', 'UNINITIALIZED lbl')
listRule('verification_type')
addRule(assign2, 'locals_vtlist', 'LOCALS verification_types sep')
addRule(assign2, 'stack_vtlist', 'STACK verification_types sep')

def p_stack_dir(p):
    '''stack_dir_rest : SAME
                    | SAME_EXTENDED
                    | CHOP intl
                    | SAME_LOCALS_1_STACK_ITEM sep stack_vtlist endstack
                    | SAME_LOCALS_1_STACK_ITEM_EXTENDED sep stack_vtlist endstack
                    | APPEND sep locals_vtlist endstack
                    | FULL sep locals_vtlist stack_vtlist endstack'''
    p[0] = '.stackmap', tuple(p[1:])

# Path: javadecompiler/Krakatau/classfile.py
# class ClassFile(object):
#     flagVals = {'PUBLIC':0x0001,
#                 'FINAL':0x0010,
#                 'SUPER':0x0020,
#                 'INTERFACE':0x0200,
#                 'ABSTRACT':0x0400,
#                 'SYNTHETIC':0x1000,
#                 'ANNOTATION':0x2000,
#                 'ENUM':0x4000,
# 
#                 # These flags are only used for InnerClasses attributes
#                 'PRIVATE':0x0002,
#                 'PROTECTED':0x0004,
#                 'STATIC':0x0008,
#                 }
# 
#     def __init__(self, bytestream):
#         magic, minor, major = bytestream.get('>LHH')
#         assert(magic == 0xCAFEBABE)
#         self.version = major,minor
# 
#         const_pool_raw = get_cp_raw(bytestream)
#         flags, self.this, self.super = bytestream.get('>HHH')
# 
#         interface_count = bytestream.get('>H')
#         self.interfaces_raw = [bytestream.get('>H') for _ in range(interface_count)]
# 
#         self.fields_raw = get_fields_raw(bytestream)
#         self.methods_raw = get_methods_raw(bytestream)
# 
#         ic_indices = [i for i,x in enumerate(const_pool_raw) if x == (1, ("InnerClasses",))]
#         self.attributes_raw = get_attributes_raw(bytestream, ic_indices)
#         assert(bytestream.size() == 0)
# 
#         self.flags = set(name for name,mask in ClassFile.flagVals.items() if (mask & flags))
#         self.cpool = constant_pool.ConstPool(const_pool_raw)
#         self.name = self.cpool.getArgsCheck('Class', self.this)
#         self.elementsLoaded = False
# 
#         self.env = self.supername = self.hierarchy = None
#         self.fields = self.methods = self.attributes = None
# 
#     def loadSupers(self, env, name, subclasses):
#         self.env = env
#         assert(self.name == name)
# 
#         if self.super:
#             self.supername = self.cpool.getArgsCheck('Class', self.super)
#             # if superclass is cached, we can assume it is free from circular inheritance
#             # since it must have been loaded successfully on a previous run
#             if not self.env.isCached(self.supername):
#                 self.env.getClass(self.supername, subclasses + (name,), partial=True)
#             self.hierarchy = self.env.getSupers(self.supername) + (self.name,)
#         else:
#             assert(name == 'java/lang/Object')
#             self.supername = None
#             self.hierarchy = (self.name,)
# 
#     def loadElements(self, keepRaw=False):
#         if self.elementsLoaded:
#             return
#         self.fields = [field.Field(m, self, keepRaw) for m in self.fields_raw]
#         self.methods = [method.Method(m, self, keepRaw) for m in self.methods_raw]
#         self.attributes = fixAttributeNames(self.attributes_raw, self.cpool)
# 
#         self.fields_raw = self.methods_raw = None
#         if not keepRaw:
#             self.attributes_raw = None
#         self.elementsLoaded = True
# 
#     def getSuperclassHierarchy(self):
#         return self.hierarchy
# 
# Path: javadecompiler/Krakatau/assembler/assembler.py
# class PoolRef(object):
#     def __init__(self, *args, **kwargs):
#         self.index = kwargs.get('index')
#         self.lbl = kwargs.get('lbl')
#         self.args = args
# 
#     def toIndex(self, pool, forbidden=(), **kwargs):
#         if self.index is not None:
#             return self.index
#         if self.lbl:
#             self.index = pool.getLabel(self.lbl, forbidden, **kwargs)
#         else:
#             self.args = [(x.toIndex(pool) if isinstance(x, PoolRef) else x) for x in self.args]
#             self.index = pool.getItem(*self.args, **kwargs)
#         return self.index


# Path: javadecompiler/Krakatau/assembler/parse.py
import ast, struct
import itertools
    import inspect
from ..classfile import ClassFile
from ..method import Method
from ..field import Field
from .tokenize import tokens, wordget, flags
from .assembler import PoolRef
from .codes import handle_codes
from .codes import newarr_codes
from .codes import et_tags
    from ply import yacc
    from ply import yacc


#Generic
def p_generic_attribute_dir(p):
    '''generic_attribute_dir : D_ATTRIBUTE utf8ref STRING_LITERAL'''
    p[0] = p[1], (p[2], p[3])

def p_generic_codeattribute_dir(p):
    '''generic_codeattribute_dir : D_CODEATTRIBUTE utf8ref STRING_LITERAL'''
    p[0] = p[1], (p[2], p[3])

#######################################################################
#Stack map stuff
addRule(nothing, 'endstack', 'D_END STACK') #directives are not expected to end with a sep

def assign1All(p):p[0] = tuple(p[1:])
addRule(assign1All, 'verification_type', 'TOP', 'INTEGER', 'FLOAT', 'DOUBLE', 'LONG', 'NULL', 'UNINITIALIZEDTHIS',
                                        'OBJECT classref', 'UNINITIALIZED lbl')
listRule('verification_type')
addRule(assign2, 'locals_vtlist', 'LOCALS verification_types sep')
addRule(assign2, 'stack_vtlist', 'STACK verification_types sep')

def p_stack_dir(p):
    '''stack_dir_rest : SAME
                    | SAME_EXTENDED
                    | CHOP intl
                    | SAME_LOCALS_1_STACK_ITEM sep stack_vtlist endstack
                    | SAME_LOCALS_1_STACK_ITEM_EXTENDED sep stack_vtlist endstack
                    | APPEND sep locals_vtlist endstack
                    | FULL sep locals_vtlist stack_vtlist endstack'''
    p[0] = '.stackmap', tuple(p[1:])

addRule(assign2, 'stack_dir', 'D_STACK stack_dir_rest')