Package pygccxml :: Package declarations :: Module declaration |

# Source Code for Module pygccxml.declarations.declaration

      1  # Copyright 2004-2008 Roman Yakovenko.
      2  # Distributed under the Boost Software License, Version 1.0. (See
      3  # accompanying file LICENSE_1_0.txt or copy at
      4  # http://www.boost.org/LICENSE_1_0.txt)
      5
      6  """
      7  defines 2 important classes
      8
      9  This module defines:
     10  * declaration_t - base class for all pygccxml defined classes, which describe
     11    a C++ declaration
     12  * location_t - provides information about physical location of the declaration
     13  """
     14
     15  import algorithm
     16  import templates
     17  import algorithms_cache
     18
     19 -class location_t(object):
     20      """provides information about the location of the declaration within the source file"""
     21
     22 -    def __init__(self, file_name='', line=-1 ):
     23          self._file_name = file_name
     24          self._line = line
     25
     26 -    def __eq__(self, other):
     27          if not isinstance( other, self.__class__ ):
     28              return False
     29          return self.line == other.line \
     30                 and self.file_name == other.file_name
     31
     32 -    def __ne__( self, other):
     33          return not self.__eq__( other )
     34
     35 -    def __lt__( self, other ):
     36          if not isinstance( other, location_t ):
     37              return self.__class__.__name__ < other.__class__.__name__
     38          return ( self.file_name, self.line ) < ( other.file_name, other.line )
     39
     40 -    def _get_file_name(self):
     41          return self._file_name
     42 -    def _set_file_name(self, new_file_name):
     43          self._file_name = new_file_name
     44      file_name = property( _get_file_name, _set_file_name
     45                            , doc="""absolute source file name, type string""" )
     46
     47 -    def _get_line( self ):
     48          return self._line
     49 -    def _set_line( self, new_line ):
     50          self._line = new_line
     51      line = property( _get_line, _set_line, doc="""line number, type int""")
     52
     53 -    def as_tuple( self ):
     54          """return tuple(self.file_name, self.line)"""
     55          return (self.file_name, self.line)
     56
     57 -class declaration_t( object ):
     58      """base class for all classes that represent a C++ declaration"""
     59
     60 -    def __init__( self, name='', location=None, is_artificial=False, mangled=None, demangled=None, attributes=None ):
     61          self._name = name
     62          self._location = location
     63          self._is_artificial = is_artificial
     64          self._mangled = mangled
     65          self._demangled = demangled
     66          self._attributes = attributes
     67          self._parent = None
     68          self._cache = algorithms_cache.declaration_algs_cache_t()
     69          self._compiler = None
     70          self._partial_name = None
     71
     72 -    def __str__(self):
     73          """Default __str__ method.
     74
     75          This version just returns the decl_string and the class.
     76          Derived classes may override this method to provide more detailed
     77          information.
     78
     79          A __str__ method for a declaration should always provide enough
     80          information so that it uniquely identifies the declaration and
     81          the user is able to find the declaration in his source code.
     82          """
     83          name = self.decl_string
     84          if name[:2]=="::":
     85              name = name[2:]
     86          # Append the declaration class
     87          cls = self.__class__.__name__
     88          if cls[-2:]=="_t":
     89              cls = cls[:-2]
     90          cls = cls.replace( '_', ' ' )
     91          return "%s [%s]"%(name, cls)
     92
     93      @staticmethod
     94 -    def _sorted_list( some_list ):
     95          """implementation details"""
     96          some_list.sort()
     97          return some_list
     98
     99 -    def _get__cmp__items( self ):
    100          """implementation details"""
    101          #Every derived class should implement this method. This method should
    102          #return a list of items, that should be compared.
    103
    104          print '_get__cmp__items not implemented for class ', self.__class__.__name__
    105          raise NotImplemented()
    106
    107 -    def _get__cmp__data(self):
    108          """implementation details"""
    109          data = [ algorithm.declaration_path( self.parent ), self.name, self.location ]
    110          data.extend( self._get__cmp__items() )
    111          return data
    112
    113 -    def __eq__(self, other):
    114          """
    115          function will return true, if both declarations refers to the same object.
    116          This function could be implemented in terms of _get__cmp__data, but in
    117          this case it will downgrade performance. self.mangled property is not
    118          compared, because it could be chaned from one compilation time to an
    119          other.
    120          """
    121          if not isinstance( other, self.__class__ ):
    122              return False
    123          return self.name == other.name \
    124                 and self.location == other.location \
    125                 and algorithm.declaration_path( self.parent ) \
    126                     == algorithm.declaration_path( other.parent )
    127
    128 -    def __ne__( self, other):
    129          """return not self.__eq__( other )"""
    130          return not self.__eq__( other )
    131
    132 -    def __lt__(self, other):
    133          """
    134          C{if not isinstance( other, self.__class__ ):}
    135          C{    return self.__class__.__name__ < other.__class__.__name__}
    136          C{return self._get__cmp__data() < other._get__cmp__data()}
    137          """
    138          if not isinstance( other, self.__class__ ):
    139              return self.__class__.__name__ < other.__class__.__name__
    140          return self._get__cmp__data() < other._get__cmp__data()
    141
    142 -    def _get_name_impl( self ):
    143          return self._name
    144
    145 -    def _get_name( self ):
    146          return self._get_name_impl()
    147
    148 -    def _on_rename( self ):
    149          pass
    150
    151 -    def _set_name( self, new_name ):
    152          previous_name = self._name
    153          self._name = new_name
    154          self._partial_name = None
    155          self.cache.reset_name_based()
    156          if previous_name: #the was a rename and not initial "set"
    157              self._on_rename()
    158
    159      name = property( _get_name, _set_name
    160                       , doc="""Declaration name
    161                       @type: str
    162                       """)
    163
    164 -    def _get_partial_name_impl( self ):
    165          return self.name
    166
    167      @property
    168 -    def partial_name( self ):
    169          """declaration name, without template default arguments
    170          Right now std containers is the only classes that support this functionality"""
    171          if None is self._partial_name:
    172              self._partial_name = self._get_partial_name_impl()
    173          return self._partial_name
    174
    175 -    def _get_parent(self):
    176          return self._parent
    177 -    def _set_parent(self, new_parent):
    178          if new_parent:
    179              assert( isinstance( new_parent, declaration_t ) )
    180          self._parent = new_parent
    181      parent = property( _get_parent, _set_parent
    182                         , doc="""Reference to parent declaration
    183                         @type: declaration_t
    184                         """)
    185
    186 -    def __get_top_parent(self):
    187          parent = self.parent
    188          me = self
    189          while True:
    190              if not parent:
    191                  return me
    192              else:
    193                  me = parent
    194                  parent = me.parent
    195      top_parent = property( __get_top_parent,
    196                             doc="""reference to top parent declaration
    197                             @type: declaration_t
    198                             """ )
    199
    200 -    def _get_location( self ):
    201          return self._location
    202 -    def _set_location( self, new_location ):
    203          self._location = new_location
    204      location = property( _get_location, _set_location
    205                          , doc="""Location of the declaration within source file
    206                          @type: L{location_t}
    207                          """)
    208
    209 -    def _get_is_artificial( self ):
    210          return self._is_artificial
    211 -    def _set_is_artificial( self, new_artificial ):
    212          self._is_artificial = new_artificial
    213      is_artificial = property( _get_is_artificial, _set_is_artificial
    214                                , doc="""Describes whether declaration is compiler generated or not
    215                                @type: bool
    216                                """)
    217
    218 -    def _get_mangled( self ):
    219          return self._mangled
    220 -    def _set_mangled( self, mangled ):
    221          self._mangled = mangled
    222      mangled = property( _get_mangled, _set_mangled
    223                          , doc="""Compiler generated declaration name
    224                          @type: str
    225                          """ )
    226
    227 -    def _get_demangled( self ):
    228          return self._demangled
    229 -    def _set_demangled( self, demangled ):
    230          self._demangled = demangled
    231      demangled = property( _get_demangled, _set_demangled
    232                          , doc="""Demangled compiler generated declaration name
    233                          @type: str
    234                          """ )
    235
    236 -    def _get_attributes( self ):
    237          return self._attributes
    238 -    def _set_attributes( self, attributes ):
    239          self._attributes = attributes
    240      attributes = property( _get_attributes, _set_attributes
    241                          , doc="""GCCXML attributes, set using __attribute__((gccxml("...")))
    242                          @type: str
    243                          """ )
    244
    245 -    def create_decl_string(self, with_defaults=True):
    246          return algorithm.full_name( self, with_defaults )
    247
    248      @property
    249 -    def decl_string(self):
    250          """declaration full name"""
    251          return self.create_decl_string()
    252
    253      @property
    254 -    def partial_decl_string(self):
    255          """declaration full name"""
    256          return self.create_decl_string(with_defaults=False)
    257
    258      @property
    259 -    def cache( self ):
    260          """implementation details
    261
    262          reference to instance of L{algorithms_cache.algorithms_cache_t} class.
    263          """
    264          return self._cache
    265
    266 -    def i_depend_on_them( self, recursive=True ):
    267          """return list of all types and declarations the declaration depends on"""
    268          print self
    269          raise NotImplementedError()
    270
    271 -    def _get_compiler( self ):
    272          return self._compiler
    273 -    def _set_compiler( self, compiler ):
    274          self._compiler = compiler
    275      compiler = property( _get_compiler, _set_compiler
    276                          , doc="""compiler name + version
    277                          @type: str""" )
    278