Package pygccxml :: Package msvc :: Package bsc

# Source Code for Package pygccxml.msvc.bsc

      1  import os
      2  import sys
      3  import logging
      4  from c_wrapper import *
      5  from .. import common_utils as msvc_utils
      6
      7 -class definition_t(object):
      8      #represents some other symbol
      9 -    def __init__( self, def_id, bsc ):
     10          self.__bsc = bsc
     11          self.__def_id = def_id
     12
     13      @property
     14 -    def def_id(self):
     15          return self.__def_id
     16
     17      @utils.cached
     18 -    def location( self ):
     19          module = STRING()
     20          line = LINE()
     21          if not BSCIdefInfo( self.__bsc, self.def_id, byref( module ), byref( line ) ):
     22              raise RuntimeError( "Unable to load information about instance(%s)" % str( self.__def_id ) )
     23          return (module, line)
     24
     25      @utils.cached
     26 -    def file_name(self):
     27          return self.location[0].value
     28
     29      @utils.cached
     30 -    def line(self):
     31          return self.location[1].value
     32
     33 -    def __str__( self ):
     34          return self.file_name + ': %d' % self.line + ' name: %s' % self.as_instance.name
     35
     36      @utils.cached
     37 -    def as_instance(self):
     38          return self.__bsc.create_instance( BSCIinstFrIdef( self.__bsc, self.def_id) )
     39
     40 -class instance_t(object):
     41      #represents some symbol
     42 -    def __init__( self, inst_id, bsc ):
     43          self.__bsc = bsc
     44          self.__inst_id = inst_id
     45
     46      @property
     47 -    def inst_id(self):
     48          return self.__inst_id
     49
     50      @utils.cached
     51 -    def name_type_attribute_mangled_name( self ):
     52          name = STRING()
     53          typ = TYP()
     54          attribute = ATR()
     55          if not BSCIinstInfo( self.__bsc, self.inst_id, byref( name ), byref( typ ), byref( attribute ) ):
     56              raise RuntimeError( "Unable to load information about instance(%s)" % str( self.__inst_id ) )
     57          undecorated_name = msvc_utils.undecorate_name( name.value )
     58          if undecorated_name.startswith( ' ?? ' ):
     59              undecorated_name = undecorated_name[4:]
     60          #BSCFormatDname( self.__bsc, name )
     61          return undecorated_name, typ, attribute, name.value
     62
     63      @utils.cached
     64 -    def mangled_name(self):
     65          return self.name_type_attribute_mangled_name[3]
     66
     67      @utils.cached
     68 -    def name(self):
     69          return self.name_type_attribute_mangled_name[0]
     70
     71      @utils.cached
     72 -    def type(self):
     73          return self.name_type_attribute_mangled_name[1].value
     74
     75      @utils.cached
     76 -    def attribute(self):
     77          return self.name_type_attribute_mangled_name[2].value
     78
     79      @utils.cached
     80 -    def is_class(self):
     81          return self.type in [ enums.TYPES.STRUCNAM
     82                                , enums.TYPES.UNIONNAM
     83                                , enums.TYPES.CLASSNAM  ]
     84
     85 -    def __str__( self ):
     86          tmp = []
     87          if enums.TYPES.has_value( self.type ):
     88              tmp.append( 'type( "%s" )' % enums.TYPES.name_of( self.type ) )
     89          if enums.ATTRIBUTES.has_value( self.attribute ):
     90              tmp.append( 'attribute( "%s" )' % enums.ATTRIBUTES.name_of( self.attribute ) )
     91          tmp.append( 'name( "%s" )' % self.name )
     92          tmp.append( 'mangled name( "%s" )' % self.mangled_name )
     93          return ', '.join( tmp )
     94
     95
     96      @utils.cached
     97 -    def definitions( self ):
     98          definitions_len = ULONG(0)
     99          definitions_ids = pointer( IDEF() )
    100
    101          if not BSCGetDefArray( self.__bsc, self.inst_id, byref( definitions_ids ), byref( definitions_len ) ):
    102              raise RuntimeError( "Unable to call BSCGetDefArray" )
    103
    104          definitions = map( lambda i: definition_t( definitions_ids[i], self.__bsc )
    105                             , range( definitions_len.value ) )
    106
    107          BSCDisposeArray( self.__bsc, definitions_ids )
    108          return definitions
    109
    110      @utils.cached
    111 -    def members( self ):
    112          instances_len = ULONG(0)
    113          instances_ids = pointer( IINST() )
    114
    115          if not BSCGetMembersArray( self.__bsc, self.inst_id, enums.MBF.ALL, byref( instances_ids ), byref( instances_len ) ):
    116              raise RuntimeError( "Unable to call BSCGetMembersArray" )
    117
    118          instances = map( lambda i: self.__bsc.create_instance( instances_ids[i] )
    119                           , range( instances_len.value ) )
    120
    121          BSCDisposeArray( self.__bsc, instances_ids )
    122          return instances
    123
    124      @utils.cached
    125 -    def used_symbols(self):
    126          instances_len = ULONG(0)
    127          instances_ids = pointer( IINST() )
    128
    129          if not BSCGetUsesArray( self.__bsc, self.inst_id, enums.MBF.ALL, byref( instances_ids ), byref( instances_len ) ):
    130              raise RuntimeError( "Unable to call BSCGetUsesArray" )
    131
    132          instances = map( lambda i: self.__bsc.create_instance( instances_ids[i] )
    133                           , range( instances_len.value ) )
    134
    135          BSCDisposeArray( self.__bsc, instances_ids )
    136          return instances
    137
    138      @utils.cached
    139 -    def base_classes(self):
    140          instances_len = ULONG(0)
    141          instances_ids = pointer( IINST() )
    142
    143          if not BSCGetBaseArray( self.__bsc, self.inst_id, byref( instances_ids ), byref( instances_len ) ):
    144              raise RuntimeError( "Unable to call BSCGetBaseArray" )
    145
    146          instances = map( lambda i: self.__bsc.create_instance( instances_ids[i] )
    147                           , range( instances_len.value ) )
    148
    149          BSCDisposeArray( self.__bsc, instances_ids )
    150          return instances
    151
    152      @utils.cached
    153 -    def derived_classes(self):
    154          instances_len = ULONG(0)
    155          instances_ids = pointer( IINST() )
    156
    157          if not BSCGetDervArray( self.__bsc, self.inst_id, byref( instances_ids ), byref( instances_len ) ):
    158              raise RuntimeError( "Unable to call BSCGetDervArray" )
    159
    160          instances = map( lambda i: self.__bsc.create_instance( instances_ids[i] )
    161                           , range( instances_len.value ) )
    162
    163          BSCDisposeArray( self.__bsc, instances_ids )
    164          return instances
    165
    166 -class module_t(object):
    167      #represents file
    168 -    def __init__( self, mod_id, bsc ):
    169          self.__bsc = bsc
    170          self.__mod_id = mod_id
    171
    172      @property
    173 -    def mod_id( self ):
    174          return self.__mod_id
    175
    176      @utils.cached
    177 -    def path( self ):
    178          name = STRING()
    179          BSCImodInfo(self.__bsc, self.__mod_id, byref(name))
    180          return name.value
    181
    182      @utils.cached
    183 -    def instances( self ):
    184          instances_len = ULONG(0)
    185          instances_ids = pointer( IINST() )
    186
    187          if not BSCGetModuleContents( self.__bsc, self.mod_id, enums.MBF.ALL, byref( instances_ids ), byref( instances_len ) ):
    188              raise RuntimeError( "Unable to call BSCGetModuleContents" )
    189
    190          instances = map( lambda i: self.__bsc.create_instance( instances_ids[i] )
    191                           , range( instances_len.value ) )
    192
    193          BSCDisposeArray( self.__bsc, instances_ids )
    194          return instances
    195
    196 -class reader_t( object ):
    197 -    def __init__( self, bsc_file ):
    198          self.logger = utils.loggers.pdb_reader
    199          self.logger.setLevel(logging.INFO)
    200
    201          self.__bsc_file = bsc_file
    202          self.__bsc = pointer( Bsc() )
    203          if not BSCOpen( self.__bsc_file, byref( self.__bsc ) ):
    204              raise RuntimeError( "Unable to open bsc file '%s'" % self.__bsc_file )
    205
    206          self.__instances_cache = {} #inst id : instance_t
    207          self.__bsc.create_instance = lambda inst_id: self.__create_instance( inst_id )
    208
    209      @utils.cached
    210 -    def instances(self):
    211          return self.__instances_cache.values()
    212
    213 -    def __create_instance( self, inst_id ):
    214          try:
    215              return self.__instances_cache[ inst_id ]
    216          except KeyError:
    217              inst = instance_t( inst_id, self.__bsc )
    218              self.__instances_cache[ inst_id ] = inst
    219              return inst
    220
    221 -    def load_instances( self ):
    222          instances_len = ULONG(0)
    223          instances_ids = pointer( IINST() )
    224
    225          if not BSCGetAllGlobalsArray( self.__bsc, enums.MBF.ALL, byref( instances_ids ), byref( instances_len ) ):
    226              raise RuntimeError( "Unable to load all globals symbols" )
    227
    228          for i in range( instances_len.value ):
    229              self.__create_instance( instances_ids[i] )
    230
    231          BSCDisposeArray( self.__bsc, instances_ids )
    232
    233      @utils.cached
    234 -    def is_case_sensitive( self ):
    235          return bool( BSCFCaseSensitive( self.__bsc ) )
    236
    237      @utils.cached
    238 -    def files(self):
    239          module_ids = pointer( IMOD() )
    240          module_len = ULONG()
    241          bs = BSC_STAT()
    242
    243          if not BSCGetAllModulesArray( self.__bsc, module_ids, byref(module_len) ):
    244              raise RuntimeError( "Unable to load all modules" )
    245
    246          modules = map( lambda i: module_t( module_ids[i], self.__bsc )
    247                         , range( module_len.value ) )
    248
    249          BSCDisposeArray( self.__bsc, module_ids )
    250
    251          return modules
    252
    253 -    def print_stat( self ):
    254          stat = BSC_STAT()
    255          BSCGetStatistics( self.__bsc, byref( stat ) )
    256          for f, t in stat._fields_:
    257              print '%s: %s' % ( f, str( getattr( stat, f) ) )
    258
    259 -    def print_classes(self, file_name=None):
    260          for m in self.files:
    261              if file_name and m.path != file_name:
    262                  continue
    263              print 'File: ', m.path
    264              if m.instances:
    265                  print '\tInstances:'
    266                  for inst in m.instances:
    267                      print '\t\t', str(inst)
    268                      if inst.definitions:
    269                          print '\t\t\tDefinitions:'
    270                          for definition in inst.definitions:
    271                              print '\t\t\t\t', str( definition )
    272                      if inst.members:
    273                          print '\t\t\tMembers:'
    274                          for member in inst.members:
    275                              print '\t\t\t\t', str( member )
    276                      if inst.used_symbols:
    277                          print '\t\t\tUsed symbols:'
    278                          for used_symbol in inst.used_symbols:
    279                              print '\t\t\t\t', str( used_symbol )
    280                      if inst.base_classes:
    281                          print '\t\t\tBase classes:'
    282                          for base_class in inst.base_classes:
    283                              print '\t\t\t\t', str( base_class )
    284                      if inst.derived_classes:
    285                          print '\t\t\tDerived classes:'
    286                          for derived_class in inst.derived_classes:
    287                              print '\t\t\t\t', str( derived_class )
    288
    289 -    def __del__( self ):
    290          if self.__bsc:
    291              BSCClose( self.__bsc )
    292