Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 47 IdlLiteral | 47 IdlLiteral |
| 48 IdlOperation < TypedObject | 48 IdlOperation < TypedObject |
| 49 IdlArgument < TypedObject | 49 IdlArgument < TypedObject |
| 50 IdlStringifier | 50 IdlStringifier |
| 51 IdlIterable < IdlIterableOrMaplikeOrSetlike | 51 IdlIterable < IdlIterableOrMaplikeOrSetlike |
| 52 IdlMaplike < IdlIterableOrMaplikeOrSetlike | 52 IdlMaplike < IdlIterableOrMaplikeOrSetlike |
| 53 IdlSetlike < IdlIterableOrMaplikeOrSetlike | 53 IdlSetlike < IdlIterableOrMaplikeOrSetlike |
| 54 IdlException < IdlInterface | 54 IdlException < IdlInterface |
| 55 (same contents as IdlInterface) | 55 (same contents as IdlInterface) |
| 56 | 56 |
| 57 TypedObject :: Object with a type. | 57 TypedObject :: Object with one or more attributes that is a type. |
| 58 | 58 |
| 59 IdlArgument is 'picklable', as it is stored in interfaces_info. | 59 IdlArgument is 'picklable', as it is stored in interfaces_info. |
| 60 | 60 |
| 61 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | 61 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
| 62 """ | 62 """ |
| 63 | 63 |
| 64 import abc | 64 import abc |
| 65 | 65 |
| 66 from idl_types import IdlType, IdlUnionType, IdlArrayType, IdlSequenceType, IdlN ullableType | 66 from idl_types import IdlType, IdlUnionType, IdlArrayType, IdlSequenceType, IdlN ullableType |
| 67 | 67 |
| 68 SPECIAL_KEYWORD_LIST = ['GETTER', 'SETTER', 'DELETER'] | 68 SPECIAL_KEYWORD_LIST = ['GETTER', 'SETTER', 'DELETER'] |
| 69 | 69 |
| 70 | 70 |
| 71 ################################################################################ | 71 ################################################################################ |
| 72 # TypedObject | 72 # TypedObject |
| 73 ################################################################################ | 73 ################################################################################ |
| 74 | 74 |
| 75 class TypedObject(object): | 75 class TypedObject(object): |
| 76 """Object with a type, such as an Attribute or Operation (return value). | 76 """Object with a type, such as an Attribute or Operation (return value). |
| 77 | 77 |
| 78 The type can be an actual type, or can be a typedef, which must be resolved | 78 The type can be an actual type, or can be a typedef, which must be resolved |
| 79 by the TypedefResolver before passing data to the code generator. | 79 by the TypedefResolver before passing data to the code generator. |
| 80 """ | 80 """ |
| 81 __metaclass__ = abc.ABCMeta | 81 __metaclass__ = abc.ABCMeta |
| 82 idl_type = None | 82 idl_type_attributes = ('idl_type',) |
| 83 | 83 |
| 84 | 84 |
| 85 ################################################################################ | 85 ################################################################################ |
| 86 # Definitions (main container class) | 86 # Definitions (main container class) |
| 87 ################################################################################ | 87 ################################################################################ |
| 88 | 88 |
| 89 class IdlDefinitions(object): | 89 class IdlDefinitions(object): |
| 90 def __init__(self, idl_name, node): | 90 def __init__(self, idl_name, node): |
| 91 """Args: node: AST root node, class == 'File'""" | 91 """Args: node: AST root node, class == 'File'""" |
| 92 self.callback_functions = {} | 92 self.callback_functions = {} |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 ext_attributes_node_to_extended_attributes(idl_name, child)) | 208 ext_attributes_node_to_extended_attributes(idl_name, child)) |
| 209 else: | 209 else: |
| 210 raise ValueError('Unrecognized node class: %s' % child_class) | 210 raise ValueError('Unrecognized node class: %s' % child_class) |
| 211 | 211 |
| 212 def accept(self, visitor): | 212 def accept(self, visitor): |
| 213 visitor.visit_dictionary(self) | 213 visitor.visit_dictionary(self) |
| 214 for member in self.members: | 214 for member in self.members: |
| 215 member.accept(visitor) | 215 member.accept(visitor) |
| 216 | 216 |
| 217 | 217 |
| 218 class IdlDictionaryMember(object): | 218 class IdlDictionaryMember(TypedObject): |
| 219 def __init__(self, idl_name, node): | 219 def __init__(self, idl_name, node): |
| 220 self.default_value = None | 220 self.default_value = None |
| 221 self.extended_attributes = {} | 221 self.extended_attributes = {} |
| 222 self.idl_type = None | 222 self.idl_type = None |
| 223 self.idl_name = idl_name | 223 self.idl_name = idl_name |
| 224 self.name = node.GetName() | 224 self.name = node.GetName() |
| 225 for child in node.GetChildren(): | 225 for child in node.GetChildren(): |
| 226 child_class = child.GetClass() | 226 child_class = child.GetClass() |
| 227 if child_class == 'Type': | 227 if child_class == 'Type': |
| 228 self.idl_type = type_node_to_type(child) | 228 self.idl_type = type_node_to_type(child) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 for attribute in self.attributes: | 319 for attribute in self.attributes: |
| 320 attribute.accept(visitor) | 320 attribute.accept(visitor) |
| 321 for constant in self.constants: | 321 for constant in self.constants: |
| 322 constant.accept(visitor) | 322 constant.accept(visitor) |
| 323 for constructor in self.constructors: | 323 for constructor in self.constructors: |
| 324 constructor.accept(visitor) | 324 constructor.accept(visitor) |
| 325 for custom_constructor in self.custom_constructors: | 325 for custom_constructor in self.custom_constructors: |
| 326 custom_constructor.accept(visitor) | 326 custom_constructor.accept(visitor) |
| 327 for operation in self.operations: | 327 for operation in self.operations: |
| 328 operation.accept(visitor) | 328 operation.accept(visitor) |
| 329 if self.iterable: | |
|
bashi
2015/02/12 12:36:19
Question: Could a dictionary have a member which i
Jens Widell
2015/02/12 12:47:51
You can't have
dictionary Foo {
iterable<DO
bashi
2015/02/12 13:08:19
Oops, I just overlooked collapsed lines (L219-L318
| |
| 330 self.iterable.accept(visitor) | |
| 331 elif self.maplike: | |
| 332 self.maplike.accept(visitor) | |
| 333 elif self.setlike: | |
| 334 self.setlike.accept(visitor) | |
| 329 | 335 |
| 330 def process_stringifier(self): | 336 def process_stringifier(self): |
| 331 """Add the stringifier's attribute or named operation child, if it has | 337 """Add the stringifier's attribute or named operation child, if it has |
| 332 one, as a regular attribute/operation of this interface.""" | 338 one, as a regular attribute/operation of this interface.""" |
| 333 if self.stringifier.attribute: | 339 if self.stringifier.attribute: |
| 334 self.attributes.append(self.stringifier.attribute) | 340 self.attributes.append(self.stringifier.attribute) |
| 335 elif self.stringifier.operation: | 341 elif self.stringifier.operation: |
| 336 self.operations.append(self.stringifier.operation) | 342 self.operations.append(self.stringifier.operation) |
| 337 | 343 |
| 338 def merge(self, other): | 344 def merge(self, other): |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 # Operations | 505 # Operations |
| 500 ################################################################################ | 506 ################################################################################ |
| 501 | 507 |
| 502 class IdlOperation(TypedObject): | 508 class IdlOperation(TypedObject): |
| 503 def __init__(self, idl_name, node=None): | 509 def __init__(self, idl_name, node=None): |
| 504 self.arguments = [] | 510 self.arguments = [] |
| 505 self.extended_attributes = {} | 511 self.extended_attributes = {} |
| 506 self.specials = [] | 512 self.specials = [] |
| 507 self.is_constructor = False | 513 self.is_constructor = False |
| 508 self.idl_name = idl_name | 514 self.idl_name = idl_name |
| 515 self.idl_type = None | |
| 509 self.is_static = False | 516 self.is_static = False |
| 510 | 517 |
| 511 if not node: | 518 if not node: |
| 512 return | 519 return |
| 513 | 520 |
| 514 self.name = node.GetName() # FIXME: should just be: or '' | 521 self.name = node.GetName() # FIXME: should just be: or '' |
| 515 # FIXME: AST should use None internally | 522 # FIXME: AST should use None internally |
| 516 if self.name == '_unnamed_': | 523 if self.name == '_unnamed_': |
| 517 self.name = '' | 524 self.name = '' |
| 518 | 525 |
| 519 self.is_static = bool(node.GetProperty('STATIC')) | 526 self.is_static = bool(node.GetProperty('STATIC')) |
| 520 property_dictionary = node.GetProperties() | 527 property_dictionary = node.GetProperties() |
| 521 for special_keyword in SPECIAL_KEYWORD_LIST: | 528 for special_keyword in SPECIAL_KEYWORD_LIST: |
| 522 if special_keyword in property_dictionary: | 529 if special_keyword in property_dictionary: |
| 523 self.specials.append(special_keyword.lower()) | 530 self.specials.append(special_keyword.lower()) |
| 524 | 531 |
| 525 self.idl_type = None | |
| 526 children = node.GetChildren() | 532 children = node.GetChildren() |
| 527 for child in children: | 533 for child in children: |
| 528 child_class = child.GetClass() | 534 child_class = child.GetClass() |
| 529 if child_class == 'Arguments': | 535 if child_class == 'Arguments': |
| 530 self.arguments = arguments_node_to_arguments(idl_name, child) | 536 self.arguments = arguments_node_to_arguments(idl_name, child) |
| 531 elif child_class == 'Type': | 537 elif child_class == 'Type': |
| 532 self.idl_type = type_node_to_type(child) | 538 self.idl_type = type_node_to_type(child) |
| 533 elif child_class == 'ExtAttributes': | 539 elif child_class == 'ExtAttributes': |
| 534 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) | 540 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) |
| 535 else: | 541 else: |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 657 # the underlying attribute or operation, if there is one. | 663 # the underlying attribute or operation, if there is one. |
| 658 if self.attribute or self.operation: | 664 if self.attribute or self.operation: |
| 659 (self.attribute or self.operation).extended_attributes.update( | 665 (self.attribute or self.operation).extended_attributes.update( |
| 660 self.extended_attributes) | 666 self.extended_attributes) |
| 661 | 667 |
| 662 | 668 |
| 663 ################################################################################ | 669 ################################################################################ |
| 664 # Iterable, Maplike, Setlike | 670 # Iterable, Maplike, Setlike |
| 665 ################################################################################ | 671 ################################################################################ |
| 666 | 672 |
| 667 class IdlIterableOrMaplikeOrSetlike(object): | 673 class IdlIterableOrMaplikeOrSetlike(TypedObject): |
| 668 def __init__(self, idl_name, node): | 674 def __init__(self, idl_name, node): |
| 669 self.extended_attributes = {} | 675 self.extended_attributes = {} |
| 670 self.type_children = [] | 676 self.type_children = [] |
| 671 | 677 |
| 672 for child in node.GetChildren(): | 678 for child in node.GetChildren(): |
| 673 child_class = child.GetClass() | 679 child_class = child.GetClass() |
| 674 if child_class == 'ExtAttributes': | 680 if child_class == 'ExtAttributes': |
| 675 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) | 681 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) |
| 676 elif child_class == 'Type': | 682 elif child_class == 'Type': |
| 677 self.type_children.append(child) | 683 self.type_children.append(child) |
| 678 else: | 684 else: |
| 679 raise ValueError('Unrecognized node class: %s' % child_class) | 685 raise ValueError('Unrecognized node class: %s' % child_class) |
| 680 | 686 |
| 681 | 687 |
| 682 class IdlIterable(IdlIterableOrMaplikeOrSetlike): | 688 class IdlIterable(IdlIterableOrMaplikeOrSetlike): |
| 689 idl_type_attributes = ('key_type', 'value_type') | |
| 690 | |
| 683 def __init__(self, idl_name, node): | 691 def __init__(self, idl_name, node): |
| 684 super(IdlIterable, self).__init__(idl_name, node) | 692 super(IdlIterable, self).__init__(idl_name, node) |
| 685 | 693 |
| 686 if len(self.type_children) == 1: | 694 if len(self.type_children) == 1: |
| 687 self.key_type = None | 695 self.key_type = None |
| 688 self.value_type = type_node_to_type(self.type_children[0]) | 696 self.value_type = type_node_to_type(self.type_children[0]) |
| 689 elif len(self.type_children) == 2: | 697 elif len(self.type_children) == 2: |
| 690 self.key_type = type_node_to_type(self.type_children[0]) | 698 self.key_type = type_node_to_type(self.type_children[0]) |
| 691 self.value_type = type_node_to_type(self.type_children[1]) | 699 self.value_type = type_node_to_type(self.type_children[1]) |
| 692 else: | 700 else: |
| 693 raise ValueError('Unexpected number of type children: %d' % len(self .type_children)) | 701 raise ValueError('Unexpected number of type children: %d' % len(self .type_children)) |
| 694 del self.type_children | 702 del self.type_children |
| 695 | 703 |
| 704 def accept(self, visitor): | |
| 705 visitor.visit_iterable(self) | |
| 706 | |
| 696 | 707 |
| 697 class IdlMaplike(IdlIterableOrMaplikeOrSetlike): | 708 class IdlMaplike(IdlIterableOrMaplikeOrSetlike): |
| 709 idl_type_attributes = ('key_type', 'value_type') | |
| 710 | |
| 698 def __init__(self, idl_name, node): | 711 def __init__(self, idl_name, node): |
| 699 super(IdlMaplike, self).__init__(idl_name, node) | 712 super(IdlMaplike, self).__init__(idl_name, node) |
| 700 | 713 |
| 701 self.is_read_only = bool(node.GetProperty('READONLY')) | 714 self.is_read_only = bool(node.GetProperty('READONLY')) |
| 702 | 715 |
| 703 if len(self.type_children) == 2: | 716 if len(self.type_children) == 2: |
| 704 self.key_type = type_node_to_type(self.type_children[0]) | 717 self.key_type = type_node_to_type(self.type_children[0]) |
| 705 self.value_type = type_node_to_type(self.type_children[1]) | 718 self.value_type = type_node_to_type(self.type_children[1]) |
| 706 else: | 719 else: |
| 707 raise ValueError('Unexpected number of children: %d' % len(self.type _children)) | 720 raise ValueError('Unexpected number of children: %d' % len(self.type _children)) |
| 708 del self.type_children | 721 del self.type_children |
| 709 | 722 |
| 723 def accept(self, visitor): | |
| 724 visitor.visit_maplike(self) | |
| 725 | |
| 710 | 726 |
| 711 class IdlSetlike(IdlIterableOrMaplikeOrSetlike): | 727 class IdlSetlike(IdlIterableOrMaplikeOrSetlike): |
| 728 idl_type_attributes = ('value_type',) | |
| 729 | |
| 712 def __init__(self, idl_name, node): | 730 def __init__(self, idl_name, node): |
| 713 super(IdlSetlike, self).__init__(idl_name, node) | 731 super(IdlSetlike, self).__init__(idl_name, node) |
| 714 | 732 |
| 715 self.is_read_only = bool(node.GetProperty('READONLY')) | 733 self.is_read_only = bool(node.GetProperty('READONLY')) |
| 716 | 734 |
| 717 if len(self.type_children) == 1: | 735 if len(self.type_children) == 1: |
| 718 self.value_type = type_node_to_type(self.type_children[0]) | 736 self.value_type = type_node_to_type(self.type_children[0]) |
| 719 else: | 737 else: |
| 720 raise ValueError('Unexpected number of children: %d' % len(self.type _children)) | 738 raise ValueError('Unexpected number of children: %d' % len(self.type _children)) |
| 721 del self.type_children | 739 del self.type_children |
| 722 | 740 |
| 741 def accept(self, visitor): | |
| 742 visitor.visit_setlike(self) | |
| 743 | |
| 723 | 744 |
| 724 ################################################################################ | 745 ################################################################################ |
| 725 # Implement statements | 746 # Implement statements |
| 726 ################################################################################ | 747 ################################################################################ |
| 727 | 748 |
| 728 class IdlImplement(object): | 749 class IdlImplement(object): |
| 729 def __init__(self, node): | 750 def __init__(self, node): |
| 730 self.left_interface = node.GetName() | 751 self.left_interface = node.GetName() |
| 731 self.right_interface = node.GetProperty('REFERENCE') | 752 self.right_interface = node.GetProperty('REFERENCE') |
| 732 | 753 |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 957 ################################################################################ | 978 ################################################################################ |
| 958 # Visitor | 979 # Visitor |
| 959 ################################################################################ | 980 ################################################################################ |
| 960 | 981 |
| 961 class Visitor(object): | 982 class Visitor(object): |
| 962 """Abstract visitor class for IDL definitions traverse.""" | 983 """Abstract visitor class for IDL definitions traverse.""" |
| 963 | 984 |
| 964 def visit_definitions(self, definitions): | 985 def visit_definitions(self, definitions): |
| 965 pass | 986 pass |
| 966 | 987 |
| 988 def visit_typed_object(self, typed_object): | |
| 989 pass | |
| 990 | |
| 967 def visit_callback_function(self, callback_function): | 991 def visit_callback_function(self, callback_function): |
| 968 pass | 992 self.visit_typed_object(callback_function) |
| 969 | 993 |
| 970 def visit_dictionary(self, dictionary): | 994 def visit_dictionary(self, dictionary): |
| 971 pass | 995 pass |
| 972 | 996 |
| 973 def visit_dictionary_member(self, member): | 997 def visit_dictionary_member(self, member): |
| 974 pass | 998 self.visit_typed_object(member) |
| 975 | 999 |
| 976 def visit_interface(self, interface): | 1000 def visit_interface(self, interface): |
| 977 pass | 1001 pass |
| 978 | 1002 |
| 979 def visit_attribute(self, attribute): | 1003 def visit_attribute(self, attribute): |
| 980 pass | 1004 self.visit_typed_object(attribute) |
| 981 | 1005 |
| 982 def visit_constant(self, constant): | 1006 def visit_constant(self, constant): |
| 983 pass | 1007 self.visit_typed_object(constant) |
| 984 | 1008 |
| 985 def visit_operation(self, operation): | 1009 def visit_operation(self, operation): |
| 986 pass | 1010 self.visit_typed_object(operation) |
| 987 | 1011 |
| 988 def visit_argument(self, argument): | 1012 def visit_argument(self, argument): |
| 989 pass | 1013 self.visit_typed_object(argument) |
| 1014 | |
| 1015 def visit_iterable(self, iterable): | |
| 1016 self.visit_typed_object(iterable) | |
| 1017 | |
| 1018 def visit_maplike(self, maplike): | |
| 1019 self.visit_typed_object(maplike) | |
| 1020 | |
| 1021 def visit_setlike(self, setlike): | |
| 1022 self.visit_typed_object(setlike) | |
| OLD | NEW |