Trees | Indices | Help |
|
---|
|
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 classes, that describes "callable" declarations 8 9 This modules contains definition for next C++ declarations: 10 - operator 11 - member 12 - free 13 - function 14 - member 15 - free 16 - constructor 17 - destructor 18 """ 19 20 import cpptypes 21 import algorithm 22 import declaration 23 import type_traits 24 import dependencies 25 import call_invocation28 """class that defines "virtuality" constants""" 29 NOT_VIRTUAL = 'not virtual' 30 VIRTUAL = 'virtual' 31 PURE_VIRTUAL = 'pure virtual' 32 ALL = [NOT_VIRTUAL, VIRTUAL, PURE_VIRTUAL]33 #preserving backward compatebility34 FUNCTION_VIRTUALITY_TYPES = VIRTUALITY_TYPES 35 36 #First level in hierarchy of calldef 37 -class argument_t(object):38 """ 39 class, that describes argument of "callable" declaration 40 """ 4112643 object.__init__(self) 44 self._name = name 45 self._default_value = default_value 46 self._type = type 47 self._attributes = attributes4850 """constructs new argument_t instance 51 52 return argument_t( name=keywd.get( 'name', self.name ) 53 , type=keywd.get( 'type', self.type ) 54 , default_value=keywd.get( 'default_value', self.default_value ) 55 , attributes=keywd.get( 'attributes', self.attributes ) ) 56 57 """ 58 return argument_t( name=keywd.get( 'name', self.name ) 59 , type=keywd.get( 'type', self.type ) 60 , default_value=keywd.get( 'default_value', self.default_value ) 61 , attributes=keywd.get( 'attributes', self.attributes ) )6264 if self.ellipsis: 65 return "..." 66 else: 67 if self.default_value==None: 68 return "%s %s"%(self.type, self.name) 69 else: 70 return "%s %s=%s"%(self.type, self.name, self.default_value)7173 if not isinstance( other, self.__class__ ): 74 return False 75 return self.name == other.name \ 76 and self.default_value == other.default_value \ 77 and self.type == other.type7880 return not self.__eq__( other )8183 if not isinstance( other, self.__class__ ): 84 return self.__class__.__name__ < other.__class__.__name__ 85 return self.name < other.name \ 86 and self.default_value < other.default_value \ 87 and self.type < other.type8892 self._name = name93 name = property( _get_name, _set_name 94 , doc="""Argument name. 95 @type: str""" ) 96 97 @property99 """bool, if True argument represents ellipsis ( "..." ) in function definition""" 100 return isinstance( self.type, cpptypes.ellipsis_t )101105 self._default_value = default_value106 default_value = property( _get_default_value, _set_default_value 107 , doc="""Argument's default value or None. 108 @type: str""") 109113 self._type = type114 type = property( _get_type, _set_type 115 , doc="""The type of the argument. 116 @type: L{type_t}""") 117121 self._attributes = attributes122 attributes = property( _get_attributes, _set_attributes 123 , doc="""GCCXML attributes, set using __attribute__((gccxml("..."))) 124 @type: str 125 """ )129 """base class for all "callable" declarations"""322130 - def __init__( self, name='', arguments=None, exceptions=None, return_type=None, has_extern=False, does_throw=True ):131 declaration.declaration_t.__init__( self, name ) 132 if not arguments: 133 arguments = [] 134 self._arguments = arguments 135 if not exceptions: 136 exceptions = [] 137 self._does_throw = does_throw 138 self._exceptions = exceptions 139 self._return_type = return_type 140 self._has_extern = has_extern 141 self._demangled_name = None142 146148 """implementation details""" 149 items = [ self.arguments 150 , self.return_type 151 , self.has_extern 152 , self.does_throw 153 , self._sorted_list( self.exceptions ) ] 154 items.extend( self._get__cmp__call_items() ) 155 return items156158 if not declaration.declaration_t.__eq__( self, other ): 159 return False 160 return self.return_type == other.return_type \ 161 and self.arguments == other.arguments \ 162 and self.has_extern == other.has_extern \ 163 and self.does_throw == other.does_throw \ 164 and self._sorted_list( self.exceptions ) \ 165 == other._sorted_list( other.exceptions )166170 self._arguments = arguments171 arguments = property( _get_arguments , _set_arguments 172 , doc="""The argument list. 173 @type: list of L{argument_t}""") 174 175 @property 178 179 @property 183 184 @property186 """list of all required arguments""" 187 r_args = [] 188 for arg in self.arguments: 189 if not arg.default_value: 190 r_args.append( arg ) 191 else: 192 break 193 return r_args194 195 @property197 """list of all optional arguments, the arguments that have default value""" 198 return self.arguments[ len( self.required_args ) : ]199203 self._does_throw = does_throw204 does_throw = property( _get_does_throw, _set_does_throw, 205 doc="""If False, than function does not throw any exception. 206 In this case, function was declared with empty throw 207 statement. 208 """) 209213 self._exceptions = exceptions214 exceptions = property( _get_exceptions, _set_exceptions 215 , doc="""The list of exceptions. 216 @type: list of L{declaration_t}""") 217221 self._return_type = return_type222 return_type = property( _get_return_type, _set_return_type 223 , doc='''The type of the return value of the "callable" or None (constructors). 224 @type: L{type_t} 225 ''') 226 @property228 """A list of overloaded "callables" (i.e. other callables with the same name within the same scope. 229 230 @type: list of L{calldef_t} 231 """ 232 if not self.parent: 233 return [] 234 # finding all functions with the same name 235 return self.parent.calldefs( name=self.name 236 , function=lambda decl: not (decl is self ) 237 , allow_empty=True 238 , recursive=False )239243 self._has_extern = has_extern244 has_extern = property( _get_has_extern, _set_has_extern, 245 doc="""Was this callable declared as "extern"? 246 @type: bool 247 """) 248250 """implementation details""" 251 demangled = demangled.strip() 252 parent_fname = algorithm.full_name( self.parent ) 253 if parent_fname.startswith( '::' ) and not demangled.startswith( '::' ): 254 parent_fname = parent_fname[2:] 255 demangled = demangled[ len( parent_fname ): ] 256 return demangled257259 if not self.demangled: 260 self._demangled_name = '' 261 262 if self._demangled_name: 263 return self._demangled_name 264 265 if self._demangled_name == '': 266 return self.name 267 268 demangled = self.demangled 269 if self.return_type: 270 return_type = type_traits.remove_alias( self.return_type ).decl_string 271 272 if return_type.startswith( '::' ) and not self.demangled.startswith( '::' ): 273 return_type = return_type[2:] 274 demangled = self.demangled 275 if demangled.startswith( return_type ): 276 demangled = demangled[ len( return_type ): ] 277 demangled = demangled.strip() 278 #removing scope 279 demangled_name = call_invocation.name( self.__remove_parent_fname( demangled ) ) 280 if demangled_name.startswith( '::' ): 281 demangled_name = demangled_name[2:] 282 #to be on the safe side 283 if demangled_name.startswith( self.name ): 284 self._demangled_name = demangled_name 285 return self._demangled_name 286 287 #well, I am going to try an other strategy 288 fname = algorithm.full_name( self ) 289 found = self.demangled.find( fname ) 290 if -1 == found: 291 if fname.startswith( '::' ): 292 fname = fname[2:] 293 found = self.demangled.find( fname ) 294 if -1 == found: 295 self._demangled_name = '' 296 return self.name 297 demangled_name = call_invocation.name( self.demangled[ found: ] ) 298 demangled_name = self.__remove_parent_fname( demangled_name ) 299 if demangled_name.startswith( '::' ): 300 demangled_name = demangled_name[2:] 301 #to be on the safe side 302 if demangled_name.startswith( self.name ): 303 self._demangled_name = demangled_name 304 return self._demangled_name 305 #if -1 == found: 306 self._demangled_name = '' 307 return self.name308 309 demangled_name = property( _get_demangled_name 310 , doc="returns function demangled name. It can help you to deal with function template instantiations") 311313 report_dependency = lambda *args, **keywd: dependencies.dependency_info_t( self, *args, **keywd ) 314 answer = [] 315 if self.return_type: 316 answer.append( report_dependency( self.return_type, hint="return type" ) ) 317 map( lambda arg: answer.append( report_dependency( arg.type ) ) 318 , self.arguments ) 319 map( lambda exception: answer.append( report_dependency( exception, hint="exception" ) ) 320 , self.exceptions ) 321 return answer325 """base class for "callable" declarations that defined within C++ class or struct"""413327 calldef_t.__init__( self, *args, **keywords ) 328 self._virtuality = virtuality 329 self._has_const = has_const 330 self._has_static = has_static331333 # Get the full name of the calldef... 334 name = algorithm.full_name(self) 335 if name[:2]=="::": 336 name = name[2:] 337 # Add the arguments... 338 args = map(lambda a: str(a), self.arguments) 339 res = "%s(%s)"%(name, ", ".join(args)) 340 # Add the return type... 341 if self.return_type!=None: 342 res = "%s %s"%(self.return_type, res) 343 # const? 344 if self.has_const: 345 res += " const" 346 # static? 347 if self.has_static: 348 res = "static "+res 349 # Append the declaration class 350 cls = self.__class__.__name__ 351 if cls[-2:]=="_t": 352 cls = cls[:-2] 353 cls = cls.replace( '_', ' ' ) 354 return "%s [%s]"%(res, cls)355 359361 if not calldef_t.__eq__( self, other ): 362 return False 363 return self.virtuality == other.virtuality \ 364 and self.has_static == other.has_static \ 365 and self.has_const == other.has_const366 372 virtuality = property( get_virtuality, set_virtuality 373 , doc="""Describes the "virtuality" of the member (as defined by the string constants in the class L{VIRTUALITY_TYPES}). 374 @type: str""") 375 378 access_type = property( _get_access_type 379 , doc="""Return the access type of the member (as defined by the string constants in the class L{ACCESS_TYPES}. 380 @type: str""") 381385 self._has_const = has_const386 has_const = property( _get_has_const, _set_has_const 387 , doc="""describes, whether "callable" has const modifier or not""") 388392 self._has_static = has_static393 has_static = property( _get_has_static, _set_has_static 394 , doc="""describes, whether "callable" has static modifier or not""") 395397 """returns function type. See L{type_t} hierarchy""" 398 if self.has_static: 399 return cpptypes.free_function_type_t( return_type=self.return_type 400 , arguments_types=[ arg.type for arg in self.arguments ] ) 401 else: 402 return cpptypes.member_function_type_t( class_inst=self.parent 403 , return_type=self.return_type 404 , arguments_types=[ arg.type for arg in self.arguments ] 405 , has_const=self.has_const )406408 f_type = self.function_type() 409 if with_defaults: 410 return f_type.decl_string 411 else: 412 return f_type.partial_decl_string415 """base class for "callable" declarations that defined within C++ namespace""" 418455420 # Get the full name of the calldef... 421 name = algorithm.full_name(self) 422 if name[:2]=="::": 423 name = name[2:] 424 # Add the arguments... 425 args = map(lambda a: str(a), self.arguments) 426 res = "%s(%s)"%(name, ", ".join(args)) 427 # Add the return type... 428 if self.return_type!=None: 429 res = "%s %s"%(self.return_type, res) 430 # extern? 431 if self.has_extern: 432 res = "extern "+res 433 # Append the declaration class 434 cls = self.__class__.__name__ 435 if cls[-2:]=="_t": 436 cls = cls[:-2] 437 cls = cls.replace( '_', ' ' ) 438 return "%s [%s]"%(res, cls)439 443445 """returns function type. See L{type_t} hierarchy""" 446 return cpptypes.free_function_type_t( return_type=self.return_type 447 , arguments_types=[ arg.type for arg in self.arguments ] )448450 f_type = self.function_type() 451 if with_defaults: 452 return f_type.decl_string 453 else: 454 return f_type.partial_decl_string458 """base class for "operator" declarations""" 459 OPERATOR_WORD_LEN = len( 'operator' )467 473461 object.__init__(self)462 463 @property465 "operator's symbol. For example: operator+, symbol is equal to '+'" 466 return self.name[operator_t.OPERATOR_WORD_LEN:].strip()475 """describes constructor declaration""" 478513 519480 # Get the full name of the calldef... 481 name = algorithm.full_name(self) 482 if name[:2]=="::": 483 name = name[2:] 484 # Add the arguments... 485 args = map(lambda a: str(a), self.arguments) 486 res = "%s(%s)"%(name, ", ".join(args)) 487 # Append the declaration class 488 cls = 'constructor' 489 if self.is_copy_constructor: 490 cls = 'copy ' + cls 491 return "%s [%s]"%(res, cls)492 493 @property495 """returns True if described declaration is copy constructor, otherwise False""" 496 args = self.arguments 497 if 1 != len( args ): 498 return False 499 arg = args[0] 500 if not type_traits.is_reference( arg.type ): 501 return False 502 if not type_traits.is_const( arg.type.base ): 503 return False 504 unaliased = type_traits.remove_alias( arg.type.base ) 505 #unaliased now refers to const_t instance 506 if not isinstance( unaliased.base, cpptypes.declarated_t ): 507 return False 508 return id(unaliased.base.declaration) == id(self.parent)509 510 @property512 return not bool( self.arguments )521 """describes member operator declaration"""526523 member_calldef_t.__init__( self, *args, **keywords ) 524 operator_t.__init__( self, *args, **keywords ) 525 self.__class_types = None528 """describes casting operator declaration"""532 537530 member_calldef_t.__init__( self, *args, **keywords ) 531 operator_t.__init__( self, *args, **keywords )539 """describes free operator declaration"""562541 free_calldef_t.__init__( self, *args, **keywords ) 542 operator_t.__init__( self, *args, **keywords ) 543 self.__class_types = None544 545 @property547 """list of class/class declaration types, extracted from the operator arguments""" 548 if None is self.__class_types: 549 self.__class_types = [] 550 for type_ in self.argument_types: 551 decl = None 552 type_ = type_traits.remove_reference( type_ ) 553 if type_traits.is_class( type_ ): 554 decl = type_traits.class_traits.get_declaration( type_ ) 555 elif type_traits.is_class_declaration( type_ ): 556 decl = type_traits.class_declaration_traits.get_declaration( type_ ) 557 else: 558 pass 559 if decl: 560 self.__class_types.append( decl ) 561 return self.__class_types
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Mon Oct 20 08:51:21 2008 | http://epydoc.sourceforge.net |