1   
  2   
  3   
  4   
  5   
  6  """defines few unrelated algorithms, that works on declarations""" 
  7   
  8  import types 
  9   
 38   
 72   
 74      """ 
 75      returns a reference to a named parent declaration 
 76   
 77      @param decl: the child declaration 
 78      @type decl: L{declaration_t} 
 79   
 80      @return: reference to L{declaration_t} or None if not found 
 81      """ 
 82      if not decl: 
 83          return None 
 84   
 85      parent = decl.parent 
 86      while parent and ( not parent.name or parent.name == '::' ): 
 87          parent = parent.parent 
 88      return parent 
  89   
 90   
 92       
 93       
 94      result = filter( None, dpath ) 
 95      result = result[0] + '::'.join( result[1:] ) 
 96      return result 
  97   
119   
121      """ 
122      converts tree representation of declarations to flatten one. 
123   
124      @param decl_or_decls: reference to list of declaration's or single declaration 
125      @type decl_or_decls: L{declaration_t} or [ L{declaration_t} ] 
126   
127      @return: [ all internal declarations ] 
128      """ 
129      import pygccxml.declarations  
130      def proceed_single( decl ): 
131          answer = [ decl ] 
132          if not isinstance( decl, pygccxml.declarations.scopedef_t ): 
133              return answer 
134          for elem in decl.declarations: 
135              if isinstance( elem, pygccxml.declarations.scopedef_t ): 
136                  answer.extend( proceed_single( elem ) ) 
137              else: 
138                  answer.append( elem ) 
139          return answer 
 140   
141      decls = [] 
142      if isinstance( decl_or_decls, types.ListType ): 
143          decls.extend( decl_or_decls ) 
144      else: 
145          decls.append( decl_or_decls ) 
146      answer = [] 
147      for decl in decls: 
148          answer.extend( proceed_single( decl ) ) 
149      return answer 
150   
152      """ 
153      converts tree representation of declarations to flatten one. 
154   
155      @param decl_or_decls: reference to list of declaration's or single declaration 
156      @type decl_or_decls: L{declaration_t} or [ L{declaration_t} ] 
157   
158      @return: [ all internal declarations ] 
159      """ 
160   
161      import pygccxml.declarations 
162      def proceed_single( decl ): 
163          yield decl 
164          if not isinstance( decl, pygccxml.declarations.scopedef_t): 
165              return 
166          for internal in decl.declarations: 
167              if isinstance( internal, pygccxml.declarations.scopedef_t): 
168                  for internal_internal in proceed_single( internal ): 
169                      yield internal_internal 
170              else: 
171                  yield internal 
 172   
173      if isinstance( decl_or_decls, types.ListType ): 
174          for creator in decl_or_decls: 
175              for internal in proceed_single( creator ): 
176                  yield internal 
177      else: 
178          for internal in proceed_single( decl_or_decls ): 
179              yield internal 
180   
189   
191      """ 
192      helper class for different search algorithms. 
193   
194      This class will help developer to match declaration by: 
195          - declaration type, for example L{class_t} or L{operator_t}. 
196          - declaration name 
197          - declaration full name 
198          - reference to parent declaration 
199      """ 
200   
201 -    def __init__( self, type=None, name=None, fullname=None, parent=None ): 
 206   
208          """ 
209          returns True if inst do match one of specified criteria 
210   
211          @param inst: declaration instance 
212          @type inst: L{declaration_t} 
213   
214          @return: bool 
215          """ 
216          answer = True 
217          if None != self.type: 
218              answer &= isinstance( inst, self.type) 
219          if None != self.name: 
220              answer &= inst.name == self.name 
221          if None != self.parent: 
222              answer &= self.parent is inst.parent 
223          if None != self.fullname: 
224              if inst.name: 
225                  answer &= self.fullname == full_name( inst ) 
226              else: 
227                  answer = False 
228          return answer 
 229   
231          """C{return self.does_match_exist(inst)}""" 
232          return self.does_match_exist(inst) 
  233   
234 -def find_all_declarations( declarations 
235                             , type=None 
236                             , name=None 
237                             , parent=None 
238                             , recursive=True 
239                             , fullname=None ): 
 254   
255 -def find_declaration( declarations 
256                        , type=None 
257                        , name=None 
258                        , parent=None 
259                        , recursive=True 
260                        , fullname=None ): 
 261      """ 
262      returns single declaration that match criteria, defined by developer. 
263      If more the one declaration was found None will be returned. 
264   
265      For more information about arguments see L{match_declaration_t} class. 
266   
267      @return: matched declaration L{declaration_t} or None 
268      """ 
269      decl = find_all_declarations( declarations, type=type, name=name, parent=parent, recursive=recursive, fullname=fullname ) 
270      if len( decl ) == 1: 
271          return decl[0] 
 272   
290   
292      """ 
293      returns set of files 
294   
295      Every declaration is declared in some file. This function returns set, that 
296      contains all file names of declarations. 
297   
298      @param decl_or_decls: reference to list of declaration's or single declaration 
299      @type decl_or_decls: L{declaration_t} or [ L{declaration_t} ] 
300   
301      @return: set( declaration file names ) 
302      """ 
303      files = set() 
304      decls = make_flatten( decl_or_decls ) 
305      for decl in decls: 
306          if decl.location: 
307              files.add( decl.location.file_name ) 
308      return files 
 309   
311      """ 
312      exception that is raised, from L{apply_visitor}, when a visitor could not be 
313      applied. 
314   
315      """ 
316 -    def __init__( self, visitor, decl_inst ): 
 317          RuntimeError.__init__( self ) 
318          self.__msg = \ 
319              "Unable to find visit function. Visitor class: %s. Declaration instance class: %s'" \ 
320              % ( visitor.__class__.__name__, decl_inst.__class__.__name__ ) 
  323   
325      """ 
326      applies a visitor on declaration instance 
327   
328      @param visitor: instance 
329      @type visitor: L{type_visitor_t} or L{decl_visitor_t} 
330      """ 
331      fname = 'visit_' + decl_inst.__class__.__name__[:-2]  
332      if not hasattr(visitor, fname ): 
333          raise visit_function_has_not_been_found_t( visitor, decl_inst ) 
334      getattr( visitor, fname )() 
 335