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 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 return 'return' | 335 return 'return' |
336 | 336 |
337 # | 337 # |
338 # GetComponents | 338 # GetComponents |
339 # | 339 # |
340 # Returns the signature components of an object as a tuple of | 340 # Returns the signature components of an object as a tuple of |
341 # (rtype, name, arrays, callspec) where: | 341 # (rtype, name, arrays, callspec) where: |
342 # rtype - The store or return type of the object. | 342 # rtype - The store or return type of the object. |
343 # name - The name of the object. | 343 # name - The name of the object. |
344 # arrays - A list of array dimensions as [] or [<fixed_num>]. | 344 # arrays - A list of array dimensions as [] or [<fixed_num>]. |
345 # args - None of not a function, otherwise a list of parameters. | 345 # args - None if not a function, otherwise a list of parameters. |
346 # | 346 # |
347 def GetComponents(self, node, release, mode): | 347 def GetComponents(self, node, release, mode): |
348 self.LogEnter('GetComponents mode %s for %s %s' % (mode, node, release)) | 348 self.LogEnter('GetComponents mode %s for %s %s' % (mode, node, release)) |
349 | 349 |
350 # Generate passing type by modifying root type | 350 # Generate passing type by modifying root type |
351 rtype = self.GetTypeByMode(node, release, mode) | 351 rtype = self.GetTypeByMode(node, release, mode) |
352 if node.IsA('Enum', 'Interface', 'Struct'): | 352 if node.IsA('Enum', 'Interface', 'Struct'): |
353 rname = node.GetName() | 353 rname = node.GetName() |
354 else: | 354 else: |
355 rname = node.GetType(release).GetName() | 355 rname = node.GetType(release).GetName() |
(...skipping 12 matching lines...) Expand all Loading... |
368 ptype, pname, parray, pspec = self.GetComponents(param, release, mode) | 368 ptype, pname, parray, pspec = self.GetComponents(param, release, mode) |
369 callspec.append((ptype, pname, parray, pspec)) | 369 callspec.append((ptype, pname, parray, pspec)) |
370 else: | 370 else: |
371 callspec = None | 371 callspec = None |
372 | 372 |
373 self.LogExit('GetComponents: %s, %s, %s, %s' % | 373 self.LogExit('GetComponents: %s, %s, %s, %s' % |
374 (rtype, name, arrayspec, callspec)) | 374 (rtype, name, arrayspec, callspec)) |
375 return (rtype, name, arrayspec, callspec) | 375 return (rtype, name, arrayspec, callspec) |
376 | 376 |
377 | 377 |
378 def Compose(self, rtype, name, arrayspec, callspec, prefix, func_as_ptr): | 378 def Compose(self, rtype, name, arrayspec, callspec, prefix, func_as_ptr, |
| 379 ptr_prefix, include_name): |
379 self.LogEnter('Compose: %s %s' % (rtype, name)) | 380 self.LogEnter('Compose: %s %s' % (rtype, name)) |
380 arrayspec = ''.join(arrayspec) | 381 arrayspec = ''.join(arrayspec) |
381 name = '%s%s%s' % (prefix, name, arrayspec) | 382 if not include_name: |
| 383 name = prefix + arrayspec |
| 384 else: |
| 385 name = prefix + name + arrayspec |
382 if callspec is None: | 386 if callspec is None: |
383 out = '%s %s' % (rtype, name) | 387 out = '%s %s' % (rtype, name) |
384 else: | 388 else: |
385 params = [] | 389 params = [] |
386 for ptype, pname, parray, pspec in callspec: | 390 for ptype, pname, parray, pspec in callspec: |
387 params.append(self.Compose(ptype, pname, parray, pspec, '', True)) | 391 params.append(self.Compose(ptype, pname, parray, pspec, '', True, |
388 if func_as_ptr: name = '(*%s)' % name | 392 ptr_prefix='', include_name=True)) |
| 393 if func_as_ptr: |
| 394 name = '(%s*%s)' % (ptr_prefix, name) |
389 out = '%s %s(%s)' % (rtype, name, ', '.join(params)) | 395 out = '%s %s(%s)' % (rtype, name, ', '.join(params)) |
390 self.LogExit('Exit Compose: %s' % out) | 396 self.LogExit('Exit Compose: %s' % out) |
391 return out | 397 return out |
392 | 398 |
393 # | 399 # |
394 # GetSignature | 400 # GetSignature |
395 # | 401 # |
396 # Returns the 'C' style signature of the object | 402 # Returns the 'C' style signature of the object |
397 # prefix - A prefix for the object's name | 403 # prefix - A prefix for the object's name |
398 # func_as_ptr - Formats a function as a function pointer | 404 # func_as_ptr - Formats a function as a function pointer |
| 405 # ptr_prefix - A prefix that goes before the "*" for a function pointer |
| 406 # include_name - If true, include member name in the signature. |
| 407 # If false, leave it out. In any case, prefix and ptr_prefix |
| 408 # are always included. |
399 # | 409 # |
400 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True): | 410 def GetSignature(self, node, release, mode, prefix='', func_as_ptr=True, |
401 self.LogEnter('GetSignature %s %s as func=%s' % (node, mode, func_as_ptr)) | 411 ptr_prefix='', include_name=True): |
| 412 self.LogEnter('GetSignature %s %s as func=%s' % |
| 413 (node, mode, func_as_ptr)) |
402 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) | 414 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) |
403 out = self.Compose(rtype, name, arrayspec, callspec, prefix, func_as_ptr) | 415 out = self.Compose(rtype, name, arrayspec, callspec, prefix, |
| 416 func_as_ptr, ptr_prefix, include_name) |
404 self.LogExit('Exit GetSignature: %s' % out) | 417 self.LogExit('Exit GetSignature: %s' % out) |
405 return out | 418 return out |
406 | 419 |
407 # Define a Typedef. | 420 # Define a Typedef. |
408 def DefineTypedef(self, node, releases, prefix='', comment=False): | 421 def DefineTypedef(self, node, releases, prefix='', comment=False): |
409 __pychecker__ = 'unusednames=comment' | 422 __pychecker__ = 'unusednames=comment' |
410 release = releases[0] | 423 release = releases[0] |
411 out = 'typedef %s;\n' % self.GetSignature(node, release, 'return', | 424 out = 'typedef %s;\n' % self.GetSignature(node, release, 'return', |
412 prefix, True) | 425 prefix, True) |
413 self.Log('DefineTypedef: %s' % out) | 426 self.Log('DefineTypedef: %s' % out) |
(...skipping 30 matching lines...) Expand all Loading... |
444 return out | 457 return out |
445 | 458 |
446 def DefineMember(self, node, releases, prefix='', comment=False): | 459 def DefineMember(self, node, releases, prefix='', comment=False): |
447 __pychecker__ = 'unusednames=prefix,comment' | 460 __pychecker__ = 'unusednames=prefix,comment' |
448 release = releases[0] | 461 release = releases[0] |
449 self.LogEnter('DefineMember %s' % node) | 462 self.LogEnter('DefineMember %s' % node) |
450 out = '%s;' % self.GetSignature(node, release, 'store', '', True) | 463 out = '%s;' % self.GetSignature(node, release, 'store', '', True) |
451 self.LogExit('Exit DefineMember') | 464 self.LogExit('Exit DefineMember') |
452 return out | 465 return out |
453 | 466 |
454 def DefineStructInternals(self, node, release, suffix='', comment=True): | 467 def GetStructName(self, node, release, include_version=False): |
| 468 suffix = '' |
| 469 if include_version: |
| 470 ver_num = node.GetVersion(release) |
| 471 suffix = ('_%s' % ver_num).replace('.', '_') |
| 472 return node.GetName() + suffix |
| 473 |
| 474 def DefineStructInternals(self, node, release, |
| 475 include_version=False, comment=True): |
455 out = '' | 476 out = '' |
456 if node.GetProperty('union'): | 477 if node.GetProperty('union'): |
457 out += 'union %s%s {\n' % (node.GetName(), suffix) | 478 out += 'union %s {\n' % ( |
| 479 self.GetStructName(node, release, include_version)) |
458 else: | 480 else: |
459 out += 'struct %s%s {\n' % (node.GetName(), suffix) | 481 out += 'struct %s {\n' % ( |
| 482 self.GetStructName(node, release, include_version)) |
460 | 483 |
461 # Generate Member Functions | 484 # Generate Member Functions |
462 members = [] | 485 members = [] |
463 for child in node.GetListOf('Member'): | 486 for child in node.GetListOf('Member'): |
464 member = self.Define(child, [release], tabs=1, comment=comment) | 487 member = self.Define(child, [release], tabs=1, comment=comment) |
465 if not member: | 488 if not member: |
466 continue | 489 continue |
467 members.append(member) | 490 members.append(member) |
468 out += '%s\n};\n' % '\n'.join(members) | 491 out += '%s\n};\n' % '\n'.join(members) |
469 return out | 492 return out |
470 | 493 |
471 | 494 |
472 def DefineStruct(self, node, releases, prefix='', comment=False): | 495 def DefineStruct(self, node, releases, prefix='', comment=False): |
473 __pychecker__ = 'unusednames=comment,prefix' | 496 __pychecker__ = 'unusednames=comment,prefix' |
474 self.LogEnter('DefineStruct %s' % node) | 497 self.LogEnter('DefineStruct %s' % node) |
475 out = '' | 498 out = '' |
476 build_list = node.GetUniqueReleases(releases) | 499 build_list = node.GetUniqueReleases(releases) |
477 | 500 |
478 # Build the most recent one with comments | 501 # Build the most recent one with comments |
479 out = self.DefineStructInternals(node, build_list[-1], comment=True) | 502 out = self.DefineStructInternals(node, build_list[-1], |
| 503 include_version=False, comment=True) |
480 | 504 |
481 # Build the rest without comments and with the version number appended | 505 # Build the rest without comments and with the version number appended |
482 for rel in build_list[0:-1]: | 506 for rel in build_list[0:-1]: |
483 ver_num = node.GetVersion(rel) | 507 out += '\n' + self.DefineStructInternals(node, rel, |
484 ver = ("_%s" % ver_num).replace('.', '_') | 508 include_version=True, |
485 out += '\n' + self.DefineStructInternals(node, rel, suffix=ver, | |
486 comment=False) | 509 comment=False) |
487 | 510 |
488 self.LogExit('Exit DefineStruct') | 511 self.LogExit('Exit DefineStruct') |
489 return out | 512 return out |
490 | 513 |
491 | 514 |
492 # | 515 # |
493 # Copyright and Comment | 516 # Copyright and Comment |
494 # | 517 # |
495 # Generate a comment or copyright block | 518 # Generate a comment or copyright block |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 if f.GetProperty('ERRORS') > 0: | 637 if f.GetProperty('ERRORS') > 0: |
615 print 'Skipping %s' % f.GetName() | 638 print 'Skipping %s' % f.GetName() |
616 continue | 639 continue |
617 print DefineDepends(node) | 640 print DefineDepends(node) |
618 for node in f.GetChildren()[2:]: | 641 for node in f.GetChildren()[2:]: |
619 print Define(node, comment=True, prefix='tst_') | 642 print Define(node, comment=True, prefix='tst_') |
620 | 643 |
621 | 644 |
622 if __name__ == '__main__': | 645 if __name__ == '__main__': |
623 sys.exit(Main(sys.argv[1:])) | 646 sys.exit(Main(sys.argv[1:])) |
OLD | NEW |