OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ Generator for C style prototypes and definitions """ | 6 """ Generator for C style prototypes and definitions """ |
7 | 7 |
8 import glob | 8 import glob |
9 import os | 9 import os |
10 import sys | 10 import sys |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 'store': '%s', | 88 'store': '%s', |
89 'return': '%s' | 89 'return': '%s' |
90 }, | 90 }, |
91 'Enum': { | 91 'Enum': { |
92 'in': '%s', | 92 'in': '%s', |
93 'inout': '%s*', | 93 'inout': '%s*', |
94 'out': '%s*', | 94 'out': '%s*', |
95 'store': '%s', | 95 'store': '%s', |
96 'return': '%s' | 96 'return': '%s' |
97 }, | 97 }, |
| 98 'Interface': { |
| 99 'in': 'const %s*', |
| 100 'inout': '%s*', |
| 101 'out': '%s**', |
| 102 'return': '%s*', |
| 103 'store': '%s*' |
| 104 }, |
98 'Struct': { | 105 'Struct': { |
99 'in': 'const %s*', | 106 'in': 'const %s*', |
100 'inout': '%s*', | 107 'inout': '%s*', |
101 'out': '%s*', | 108 'out': '%s*', |
102 'return': ' %s*', | 109 'return': ' %s*', |
103 'store': '%s' | 110 'store': '%s' |
104 }, | 111 }, |
105 'blob_t': { | 112 'blob_t': { |
106 'in': 'const %s', | 113 'in': 'const %s', |
107 'inout': '%s', | 114 'inout': '%s', |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 self.LogEnter('GetTypeName of %s rel=%s' % (node, release)) | 243 self.LogEnter('GetTypeName of %s rel=%s' % (node, release)) |
237 | 244 |
238 # For Members, Params, and Typedefs get the type it refers to otherwise | 245 # For Members, Params, and Typedefs get the type it refers to otherwise |
239 # the node in question is it's own type (struct, union etc...) | 246 # the node in question is it's own type (struct, union etc...) |
240 if node.IsA('Member', 'Param', 'Typedef'): | 247 if node.IsA('Member', 'Param', 'Typedef'): |
241 typeref = node.GetType(release) | 248 typeref = node.GetType(release) |
242 else: | 249 else: |
243 typeref = node | 250 typeref = node |
244 | 251 |
245 if typeref is None: | 252 if typeref is None: |
| 253 node.Error('No type at release %s.' % release) |
246 raise CGenError('No type for %s' % node) | 254 raise CGenError('No type for %s' % node) |
247 | 255 |
248 # If the type is a (BuiltIn) Type then return it's name | 256 # If the type is a (BuiltIn) Type then return it's name |
249 # remapping as needed | 257 # remapping as needed |
250 if typeref.IsA('Type'): | 258 if typeref.IsA('Type'): |
251 name = CGen.RemapName.get(typeref.GetName(), None) | 259 name = CGen.RemapName.get(typeref.GetName(), None) |
252 if name is None: name = typeref.GetName() | 260 if name is None: name = typeref.GetName() |
253 name = '%s%s' % (prefix, name) | 261 name = '%s%s' % (prefix, name) |
254 | 262 |
| 263 # For Interfaces, use the name + version |
| 264 elif typeref.IsA('Interface'): |
| 265 rel = typeref.first_release[release] |
| 266 name = 'struct %s%s' % (prefix, self.GetStructName(typeref, rel, True)) |
| 267 |
255 # For structures, preceed with 'struct' or 'union' as appropriate | 268 # For structures, preceed with 'struct' or 'union' as appropriate |
256 elif typeref.IsA('Interface', 'Struct'): | 269 elif typeref.IsA('Struct'): |
257 if typeref.GetProperty('union'): | 270 if typeref.GetProperty('union'): |
258 name = 'union %s%s' % (prefix, typeref.GetName()) | 271 name = 'union %s%s' % (prefix, typeref.GetName()) |
259 else: | 272 else: |
260 name = 'struct %s%s' % (prefix, typeref.GetName()) | 273 name = 'struct %s%s' % (prefix, typeref.GetName()) |
261 | 274 |
262 # If it's an enum, or typedef then return the Enum's name | 275 # If it's an enum, or typedef then return the Enum's name |
263 elif typeref.IsA('Enum', 'Typedef'): | 276 elif typeref.IsA('Enum', 'Typedef'): |
264 # The enum may have skipped having a typedef, we need prefix with 'enum'. | 277 # The enum may have skipped having a typedef, we need prefix with 'enum'. |
265 if typeref.GetProperty('notypedef'): | 278 if typeref.GetProperty('notypedef'): |
266 name = 'enum %s%s' % (prefix, typeref.GetName()) | 279 name = 'enum %s%s' % (prefix, typeref.GetName()) |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 (node, mode, func_as_ptr)) | 433 (node, mode, func_as_ptr)) |
421 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) | 434 rtype, name, arrayspec, callspec = self.GetComponents(node, release, mode) |
422 out = self.Compose(rtype, name, arrayspec, callspec, prefix, | 435 out = self.Compose(rtype, name, arrayspec, callspec, prefix, |
423 func_as_ptr, ptr_prefix, include_name) | 436 func_as_ptr, ptr_prefix, include_name) |
424 self.LogExit('Exit GetSignature: %s' % out) | 437 self.LogExit('Exit GetSignature: %s' % out) |
425 return out | 438 return out |
426 | 439 |
427 # Define a Typedef. | 440 # Define a Typedef. |
428 def DefineTypedef(self, node, releases, prefix='', comment=False): | 441 def DefineTypedef(self, node, releases, prefix='', comment=False): |
429 __pychecker__ = 'unusednames=comment' | 442 __pychecker__ = 'unusednames=comment' |
430 release = releases[0] | 443 build_list = node.GetUniqueReleases(releases) |
431 out = 'typedef %s;\n' % self.GetSignature(node, release, 'return', | 444 |
| 445 # TODO(noelallen) : Bug 157017 finish multiversion support |
| 446 if len(build_list) != 1: |
| 447 node.Error('Can not support multiple versions of node.') |
| 448 assert len(build_list) == 1 |
| 449 |
| 450 out = 'typedef %s;\n' % self.GetSignature(node, build_list[0], 'return', |
432 prefix, True) | 451 prefix, True) |
433 self.Log('DefineTypedef: %s' % out) | 452 self.Log('DefineTypedef: %s' % out) |
434 return out | 453 return out |
435 | 454 |
436 # Define an Enum. | 455 # Define an Enum. |
437 def DefineEnum(self, node, releases, prefix='', comment=False): | 456 def DefineEnum(self, node, releases, prefix='', comment=False): |
438 __pychecker__ = 'unusednames=comment,releases' | 457 __pychecker__ = 'unusednames=comment,releases' |
439 self.LogEnter('DefineEnum %s' % node) | 458 self.LogEnter('DefineEnum %s' % node) |
440 name = '%s%s' % (prefix, node.GetName()) | 459 name = '%s%s' % (prefix, node.GetName()) |
441 notypedef = node.GetProperty('notypedef') | 460 notypedef = node.GetProperty('notypedef') |
442 unnamed = node.GetProperty('unnamed') | 461 unnamed = node.GetProperty('unnamed') |
| 462 |
443 if unnamed: | 463 if unnamed: |
444 out = 'enum {' | 464 out = 'enum {' |
445 elif notypedef: | 465 elif notypedef: |
446 out = 'enum %s {' % name | 466 out = 'enum %s {' % name |
447 else: | 467 else: |
448 out = 'typedef enum {' | 468 out = 'typedef enum {' |
449 enumlist = [] | 469 enumlist = [] |
450 for child in node.GetListOf('EnumItem'): | 470 for child in node.GetListOf('EnumItem'): |
451 value = child.GetProperty('VALUE') | 471 value = child.GetProperty('VALUE') |
452 comment_txt = GetNodeComments(child, tabs=1) | 472 comment_txt = GetNodeComments(child, tabs=1) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 out += '%s\n};\n' % '\n'.join(members) | 518 out += '%s\n};\n' % '\n'.join(members) |
499 return out | 519 return out |
500 | 520 |
501 | 521 |
502 def DefineStruct(self, node, releases, prefix='', comment=False): | 522 def DefineStruct(self, node, releases, prefix='', comment=False): |
503 __pychecker__ = 'unusednames=comment,prefix' | 523 __pychecker__ = 'unusednames=comment,prefix' |
504 self.LogEnter('DefineStruct %s' % node) | 524 self.LogEnter('DefineStruct %s' % node) |
505 out = '' | 525 out = '' |
506 build_list = node.GetUniqueReleases(releases) | 526 build_list = node.GetUniqueReleases(releases) |
507 | 527 |
| 528 # TODO(noelallen) : Bug 157017 finish multiversion support |
| 529 if node.IsA('Struct'): |
| 530 if len(build_list) != 1: |
| 531 node.Error('Can not support multiple versions of node.') |
| 532 assert len(build_list) == 1 |
| 533 |
| 534 |
508 if node.IsA('Interface'): | 535 if node.IsA('Interface'): |
509 # Build the most recent one versioned, with comments | 536 # Build the most recent one versioned, with comments |
510 out = self.DefineStructInternals(node, build_list[-1], | 537 out = self.DefineStructInternals(node, build_list[-1], |
511 include_version=True, comment=True) | 538 include_version=True, comment=True) |
512 | 539 |
513 # Define an unversioned typedef for the most recent version | 540 # Define an unversioned typedef for the most recent version |
514 out += '\ntypedef struct %s %s;\n' % ( | 541 out += '\ntypedef struct %s %s;\n' % ( |
515 self.GetStructName(node, build_list[-1], include_version=True), | 542 self.GetStructName(node, build_list[-1], include_version=True), |
516 self.GetStructName(node, build_list[-1], include_version=False)) | 543 self.GetStructName(node, build_list[-1], include_version=False)) |
517 else: | 544 else: |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 ErrOut.Log('Failed generator test.') | 674 ErrOut.Log('Failed generator test.') |
648 else: | 675 else: |
649 InfoOut.Log('Passed generator test.') | 676 InfoOut.Log('Passed generator test.') |
650 return total_errs | 677 return total_errs |
651 | 678 |
652 def Main(args): | 679 def Main(args): |
653 filenames = ParseOptions(args) | 680 filenames = ParseOptions(args) |
654 if GetOption('test'): | 681 if GetOption('test'): |
655 return TestFiles(filenames) | 682 return TestFiles(filenames) |
656 ast = ParseFiles(filenames) | 683 ast = ParseFiles(filenames) |
| 684 cgen = CGen() |
657 for f in ast.GetListOf('File'): | 685 for f in ast.GetListOf('File'): |
658 if f.GetProperty('ERRORS') > 0: | 686 if f.GetProperty('ERRORS') > 0: |
659 print 'Skipping %s' % f.GetName() | 687 print 'Skipping %s' % f.GetName() |
660 continue | 688 continue |
661 print DefineDepends(node) | |
662 for node in f.GetChildren()[2:]: | 689 for node in f.GetChildren()[2:]: |
663 print Define(node, comment=True, prefix='tst_') | 690 print cgen.Define(node, comment=True, prefix='tst_') |
664 | 691 |
665 | 692 |
666 if __name__ == '__main__': | 693 if __name__ == '__main__': |
667 sys.exit(Main(sys.argv[1:])) | 694 sys.exit(Main(sys.argv[1:])) |
668 | 695 |
OLD | NEW |