Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """ Generator for C style prototypes and definitions """ | 7 """ Generator for C style prototypes and definitions """ |
| 8 | 8 |
| 9 import glob | 9 import glob |
| 10 import os | 10 import os |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 return 'return' | 331 return 'return' |
| 332 | 332 |
| 333 # | 333 # |
| 334 # GetComponents | 334 # GetComponents |
| 335 # | 335 # |
| 336 # Returns the signature components of an object as a tuple of | 336 # Returns the signature components of an object as a tuple of |
| 337 # (rtype, name, arrays, callspec) where: | 337 # (rtype, name, arrays, callspec) where: |
| 338 # rtype - The store or return type of the object. | 338 # rtype - The store or return type of the object. |
| 339 # name - The name of the object. | 339 # name - The name of the object. |
| 340 # arrays - A list of array dimensions as [] or [<fixed_num>]. | 340 # arrays - A list of array dimensions as [] or [<fixed_num>]. |
| 341 # args - None of not a function, otherwise a list of parameters. | 341 # args - None if not a function, otherwise a list of parameters. |
| 342 # | 342 # |
| 343 def GetComponents(self, node, release, mode): | 343 def GetComponents(self, node, release, mode): |
| 344 self.LogEnter('GetComponents mode %s for %s %s' % (mode, node, release)) | 344 self.LogEnter('GetComponents mode %s for %s %s' % (mode, node, release)) |
| 345 | 345 |
| 346 # Generate passing type by modifying root type | 346 # Generate passing type by modifying root type |
| 347 rtype = self.GetTypeByMode(node, release, mode) | 347 rtype = self.GetTypeByMode(node, release, mode) |
| 348 if node.IsA('Enum', 'Interface', 'Struct'): | 348 if node.IsA('Enum', 'Interface', 'Struct'): |
| 349 rname = node.GetName() | 349 rname = node.GetName() |
| 350 else: | 350 else: |
| 351 rname = node.GetType(release).GetName() | 351 rname = node.GetType(release).GetName() |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 364 ptype, pname, parray, pspec = self.GetComponents(param, release, mode) | 364 ptype, pname, parray, pspec = self.GetComponents(param, release, mode) |
| 365 callspec.append((ptype, pname, parray, pspec)) | 365 callspec.append((ptype, pname, parray, pspec)) |
| 366 else: | 366 else: |
| 367 callspec = None | 367 callspec = None |
| 368 | 368 |
| 369 self.LogExit('GetComponents: %s, %s, %s, %s' % | 369 self.LogExit('GetComponents: %s, %s, %s, %s' % |
| 370 (rtype, name, arrayspec, callspec)) | 370 (rtype, name, arrayspec, callspec)) |
| 371 return (rtype, name, arrayspec, callspec) | 371 return (rtype, name, arrayspec, callspec) |
| 372 | 372 |
| 373 | 373 |
| 374 def Compose(self, rtype, name, arrayspec, callspec, prefix, func_as_ptr): | 374 def Compose(self, rtype, name, arrayspec, callspec, prefix, func_as_ptr, |
| 375 func_attributes, is_cast): | |
|
jvoung - send to chromium...
2011/11/16 01:45:22
changed func_attributes to ptr_prefix
changed is_c
| |
| 375 self.LogEnter('Compose: %s %s' % (rtype, name)) | 376 self.LogEnter('Compose: %s %s' % (rtype, name)) |
| 376 arrayspec = ''.join(arrayspec) | 377 arrayspec = ''.join(arrayspec) |
| 377 name = '%s%s%s' % (prefix, name, arrayspec) | 378 name = '%s%s%s' % (prefix, name, arrayspec) |
| 378 if callspec is None: | 379 if callspec is None: |
| 379 out = '%s %s' % (rtype, name) | 380 out = '%s %s' % (rtype, name) |
| 380 else: | 381 else: |
| 381 params = [] | 382 params = [] |
| 382 for ptype, pname, parray, pspec in callspec: | 383 for ptype, pname, parray, pspec in callspec: |
| 383 params.append(self.Compose(ptype, pname, parray, pspec, '', True)) | 384 params.append(self.Compose(ptype, pname, parray, pspec, '', True, |
| 384 if func_as_ptr: name = '(*%s)' % name | 385 func_attributes='', is_cast='')) |
| 386 if func_as_ptr: | |
| 387 if is_cast: | |
| 388 name = '(%s*)' % (func_attributes) | |
| 389 else: | |
| 390 name = '(%s*%s)' % (func_attributes, name) | |
| 385 out = '%s %s(%s)' % (rtype, name, ', '.join(params)) | 391 out = '%s %s(%s)' % (rtype, name, ', '.join(params)) |
| 386 self.LogExit('Exit Compose: %s' % out) | 392 self.LogExit('Exit Compose: %s' % out) |
| 387 return out | 393 return out |
| 388 | 394 |
| 389 # | 395 # |
| 390 # GetSignature | 396 # GetSignature |
| 391 # | 397 # |
| 392 # Returns the 'C' style signature of the object | 398 # Returns the 'C' style signature of the object |
| 393 # prefix - A prefix for the object's name | 399 # prefix - A prefix for the object's name |
| 394 # func_as_ptr - Formats a function as a function pointer | 400 # func_as_ptr - Formats a function as a function pointer |
| 395 # | 401 # |
| 396 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True): | 402 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True, |
| 397 self.LogEnter('GetSignature %s %s as func=%s' % (node, mode, func_as_ptr)) | 403 func_attributes='', is_cast=False): |
| 404 self.LogEnter('GetSignature %s %s as func=%s attribs=%s' % | |
| 405 (node, mode, func_as_ptr, func_attributes)) | |
| 398 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) | 406 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) |
| 399 out = self.Compose(rtype, name, arrayspec, callspec, prefix, func_as_ptr) | 407 out = self.Compose(rtype, name, arrayspec, callspec, prefix, |
| 408 func_as_ptr, func_attributes, is_cast) | |
| 400 self.LogExit('Exit GetSignature: %s' % out) | 409 self.LogExit('Exit GetSignature: %s' % out) |
| 401 return out | 410 return out |
| 402 | 411 |
| 403 # Define a Typedef. | 412 # Define a Typedef. |
| 404 def DefineTypedef(self, node, releases, prefix='', comment=False): | 413 def DefineTypedef(self, node, releases, prefix='', comment=False, |
| 414 func_attributes=''): | |
| 405 __pychecker__ = 'unusednames=comment' | 415 __pychecker__ = 'unusednames=comment' |
| 406 release = releases[0] | 416 release = releases[0] |
| 407 out = 'typedef %s;\n' % self.GetSignature(node, release, 'return', | 417 out = 'typedef %s;\n' % self.GetSignature(node, release, 'return', |
| 408 prefix, True) | 418 prefix, True) |
| 409 self.Log('DefineTypedef: %s' % out) | 419 self.Log('DefineTypedef: %s' % out) |
| 410 return out | 420 return out |
| 411 | 421 |
| 412 # Define an Enum. | 422 # Define an Enum. |
| 413 def DefineEnum(self, node, releases, prefix='', comment=False): | 423 def DefineEnum(self, node, releases, prefix='', comment=False): |
| 414 __pychecker__ = 'unusednames=comment,releases' | 424 __pychecker__ = 'unusednames=comment,releases' |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 437 return out | 447 return out |
| 438 | 448 |
| 439 def DefineMember(self, node, releases, prefix='', comment=False): | 449 def DefineMember(self, node, releases, prefix='', comment=False): |
| 440 __pychecker__ = 'unusednames=prefix,comment' | 450 __pychecker__ = 'unusednames=prefix,comment' |
| 441 release = releases[0] | 451 release = releases[0] |
| 442 self.LogEnter('DefineMember %s' % node) | 452 self.LogEnter('DefineMember %s' % node) |
| 443 out = '%s;' % self.GetSignature(node, release, 'store', '', True) | 453 out = '%s;' % self.GetSignature(node, release, 'store', '', True) |
| 444 self.LogExit('Exit DefineMember') | 454 self.LogExit('Exit DefineMember') |
| 445 return out | 455 return out |
| 446 | 456 |
| 447 def DefineStructInternals(self, node, release, suffix='', comment=True): | 457 def GetStructName(self, node, release, is_latest_release=False): |
|
jvoung - send to chromium...
2011/11/16 01:45:22
changed parameter to "include_version" to be more
| |
| 458 suffix = '' | |
| 459 if not is_latest_release: | |
| 460 ver_num = node.GetVersion(release) | |
| 461 suffix = ('_%s' % ver_num).replace('.', '_') | |
| 462 return node.GetName() + suffix | |
| 463 | |
| 464 def DefineStructInternals(self, node, release, | |
| 465 is_latest_release=False, comment=True): | |
| 448 out = '' | 466 out = '' |
| 449 if node.GetProperty('union'): | 467 if node.GetProperty('union'): |
| 450 out += 'union %s%s {\n' % (node.GetName(), suffix) | 468 out += 'union %s {\n' % ( |
| 469 self.GetStructName(node, release, is_latest_release)) | |
| 451 else: | 470 else: |
| 452 out += 'struct %s%s {\n' % (node.GetName(), suffix) | 471 out += 'struct %s {\n' % ( |
| 472 self.GetStructName(node, release, is_latest_release)) | |
| 453 | 473 |
| 454 # Generate Member Functions | 474 # Generate Member Functions |
| 455 members = [] | 475 members = [] |
| 456 for child in node.GetListOf('Member'): | 476 for child in node.GetListOf('Member'): |
| 457 member = self.Define(child, [release], tabs=1, comment=comment) | 477 member = self.Define(child, [release], tabs=1, comment=comment) |
| 458 if not member: | 478 if not member: |
| 459 continue | 479 continue |
| 460 members.append(member) | 480 members.append(member) |
| 461 out += '%s\n};\n' % '\n'.join(members) | 481 out += '%s\n};\n' % '\n'.join(members) |
| 462 return out | 482 return out |
| 463 | 483 |
| 464 | 484 |
| 465 def DefineStruct(self, node, releases, prefix='', comment=False): | 485 def DefineStruct(self, node, releases, prefix='', comment=False): |
| 466 __pychecker__ = 'unusednames=comment,prefix' | 486 __pychecker__ = 'unusednames=comment,prefix' |
| 467 self.LogEnter('DefineStruct %s' % node) | 487 self.LogEnter('DefineStruct %s' % node) |
| 468 out = '' | 488 out = '' |
| 469 build_list = node.GetUniqueReleases(releases) | 489 build_list = node.GetUniqueReleases(releases) |
| 470 | 490 |
| 471 # Build the most recent one with comments | 491 # Build the most recent one with comments |
| 472 out = self.DefineStructInternals(node, build_list[-1], comment=True) | 492 out = self.DefineStructInternals(node, build_list[-1], |
| 493 is_latest_release=True, comment=True) | |
| 473 | 494 |
| 474 # Build the rest without comments and with the version number appended | 495 # Build the rest without comments and with the version number appended |
| 475 for rel in build_list[0:-1]: | 496 for rel in build_list[0:-1]: |
| 476 ver_num = node.GetVersion(rel) | 497 out += '\n' + self.DefineStructInternals(node, rel, |
| 477 ver = ("_%s" % ver_num).replace('.', '_') | 498 is_latest_release=False, |
| 478 out += '\n' + self.DefineStructInternals(node, rel, suffix=ver, | |
| 479 comment=False) | 499 comment=False) |
| 480 | 500 |
| 481 self.LogExit('Exit DefineStruct') | 501 self.LogExit('Exit DefineStruct') |
| 482 return out | 502 return out |
| 483 | 503 |
| 484 | 504 |
| 485 # | 505 # |
| 486 # Copyright and Comment | 506 # Copyright and Comment |
| 487 # | 507 # |
| 488 # Generate a comment or copyright block | 508 # Generate a comment or copyright block |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 607 if f.GetProperty('ERRORS') > 0: | 627 if f.GetProperty('ERRORS') > 0: |
| 608 print 'Skipping %s' % f.GetName() | 628 print 'Skipping %s' % f.GetName() |
| 609 continue | 629 continue |
| 610 print DefineDepends(node) | 630 print DefineDepends(node) |
| 611 for node in f.GetChildren()[2:]: | 631 for node in f.GetChildren()[2:]: |
| 612 print Define(node, comment=True, prefix='tst_') | 632 print Define(node, comment=True, prefix='tst_') |
| 613 | 633 |
| 614 | 634 |
| 615 if __name__ == '__main__': | 635 if __name__ == '__main__': |
| 616 sys.exit(Main(sys.argv[1:])) | 636 sys.exit(Main(sys.argv[1:])) |
| OLD | NEW |