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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 IdlException < IdlInterface | 55 IdlException < IdlInterface |
56 (same contents as IdlInterface) | 56 (same contents as IdlInterface) |
57 | 57 |
58 TypedObject :: mixin for typedef resolution | 58 TypedObject :: mixin for typedef resolution |
59 | 59 |
60 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | 60 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
61 """ | 61 """ |
62 | 62 |
63 import abc | 63 import abc |
64 | 64 |
65 from idl_types import IdlType, IdlUnionType | 65 from idl_types import IdlType, IdlUnionType, IdlArrayOrSequenceType |
66 | 66 |
67 SPECIAL_KEYWORD_LIST = ['GETTER', 'SETTER', 'DELETER'] | 67 SPECIAL_KEYWORD_LIST = ['GETTER', 'SETTER', 'DELETER'] |
68 STANDARD_TYPEDEFS = { | 68 STANDARD_TYPEDEFS = { |
69 # http://www.w3.org/TR/WebIDL/#common-DOMTimeStamp | 69 # http://www.w3.org/TR/WebIDL/#common-DOMTimeStamp |
70 'DOMTimeStamp': 'unsigned long long', | 70 'DOMTimeStamp': 'unsigned long long', |
71 } | 71 } |
72 | 72 |
73 | 73 |
74 ################################################################################ | 74 ################################################################################ |
75 # TypedObject (mixin for typedef resolution) | 75 # TypedObject (mixin for typedef resolution) |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
736 | 736 |
737 ################################################################################ | 737 ################################################################################ |
738 # Types | 738 # Types |
739 ################################################################################ | 739 ################################################################################ |
740 | 740 |
741 def type_node_to_type(node): | 741 def type_node_to_type(node): |
742 children = node.GetChildren() | 742 children = node.GetChildren() |
743 if len(children) < 1 or len(children) > 2: | 743 if len(children) < 1 or len(children) > 2: |
744 raise ValueError('Type node expects 1 or 2 children (type + optional arr
ay []), got %s (multi-dimensional arrays are not supported).' % len(children)) | 744 raise ValueError('Type node expects 1 or 2 children (type + optional arr
ay []), got %s (multi-dimensional arrays are not supported).' % len(children)) |
745 | 745 |
| 746 is_nullable = node.GetProperty('NULLABLE') or False # syntax: T? |
746 type_node_child = children[0] | 747 type_node_child = children[0] |
| 748 base_type = type_node_inner_to_type(type_node_child, is_nullable=is_nullable
) |
747 | 749 |
748 if len(children) == 2: | 750 if len(children) == 2: |
749 array_node = children[1] | 751 array_node = children[1] |
750 array_node_class = array_node.GetClass() | 752 array_node_class = array_node.GetClass() |
751 if array_node_class != 'Array': | 753 if array_node_class != 'Array': |
752 raise ValueError('Expected Array node as TypeSuffix, got %s node.' %
array_node_class) | 754 raise ValueError('Expected Array node as TypeSuffix, got %s node.' %
array_node_class) |
753 # FIXME: use IdlArrayType instead of is_array, once have that | 755 array_is_nullable = array_node.GetProperty('NULLABLE') or False |
754 is_array = True | 756 return IdlArrayOrSequenceType(base_type, is_array=True, is_nullable=arra
y_is_nullable) |
755 else: | |
756 is_array = False | |
757 | 757 |
758 is_nullable = node.GetProperty('NULLABLE') or False # syntax: T? | 758 return base_type |
759 | |
760 return type_node_inner_to_type(type_node_child, is_array=is_array, is_nullab
le=is_nullable) | |
761 | 759 |
762 | 760 |
763 def type_node_inner_to_type(node, is_array=False, is_nullable=False): | 761 def type_node_inner_to_type(node, is_nullable=False): |
764 # FIXME: remove is_array and is_nullable once have IdlArrayType and IdlNulla
bleType | 762 # FIXME: remove is_nullable once have IdlNullableType |
765 node_class = node.GetClass() | 763 node_class = node.GetClass() |
766 # Note Type*r*ef, not Typedef, meaning the type is an identifier, thus | 764 # Note Type*r*ef, not Typedef, meaning the type is an identifier, thus |
767 # either a typedef shorthand (but not a Typedef declaration itself) or an | 765 # either a typedef shorthand (but not a Typedef declaration itself) or an |
768 # interface type. We do not distinguish these, and just use the type name. | 766 # interface type. We do not distinguish these, and just use the type name. |
769 if node_class in ['PrimitiveType', 'Typeref']: | 767 if node_class in ['PrimitiveType', 'Typeref']: |
770 # unrestricted syntax: unrestricted double | unrestricted float | 768 # unrestricted syntax: unrestricted double | unrestricted float |
771 is_unrestricted = node.GetProperty('UNRESTRICTED') or False | 769 is_unrestricted = node.GetProperty('UNRESTRICTED') or False |
772 return IdlType(node.GetName(), is_array=is_array, is_nullable=is_nullabl
e, is_unrestricted=is_unrestricted) | 770 return IdlType(node.GetName(), is_nullable=is_nullable, is_unrestricted=
is_unrestricted) |
773 elif node_class == 'Any': | 771 elif node_class == 'Any': |
774 return IdlType('any', is_array=is_array, is_nullable=is_nullable) | 772 return IdlType('any', is_nullable=is_nullable) |
775 elif node_class == 'Sequence': | 773 elif node_class == 'Sequence': |
776 if is_array: | |
777 raise ValueError('Arrays of sequences are not supported') | |
778 sequence_is_nullable = node.GetProperty('NULLABLE') or False | 774 sequence_is_nullable = node.GetProperty('NULLABLE') or False |
779 return sequence_node_to_type(node, is_nullable=sequence_is_nullable) | 775 return sequence_node_to_type(node, is_nullable=sequence_is_nullable) |
780 elif node_class == 'UnionType': | 776 elif node_class == 'UnionType': |
781 if is_array: | |
782 raise ValueError('Arrays of unions are not supported') | |
783 return union_type_node_to_idl_union_type(node, is_nullable=is_nullable) | 777 return union_type_node_to_idl_union_type(node, is_nullable=is_nullable) |
784 raise ValueError('Unrecognized node class: %s' % node_class) | 778 raise ValueError('Unrecognized node class: %s' % node_class) |
785 | 779 |
786 | 780 |
787 def sequence_node_to_type(node, is_nullable=False): | 781 def sequence_node_to_type(node, is_nullable=False): |
788 children = node.GetChildren() | 782 children = node.GetChildren() |
789 if len(children) != 1: | 783 if len(children) != 1: |
790 raise ValueError('Sequence node expects exactly 1 child, got %s' % len(c
hildren)) | 784 raise ValueError('Sequence node expects exactly 1 child, got %s' % len(c
hildren)) |
791 sequence_child = children[0] | 785 sequence_child = children[0] |
792 sequence_child_class = sequence_child.GetClass() | 786 sequence_child_class = sequence_child.GetClass() |
793 if sequence_child_class != 'Type': | 787 if sequence_child_class != 'Type': |
794 raise ValueError('Unrecognized node class: %s' % sequence_child_class) | 788 raise ValueError('Unrecognized node class: %s' % sequence_child_class) |
795 element_type = type_node_to_type(sequence_child).base_type | 789 element_type = type_node_to_type(sequence_child) |
796 return IdlType(element_type, is_sequence=True, is_nullable=is_nullable) | 790 return IdlArrayOrSequenceType(element_type, is_sequence=True, is_nullable=is
_nullable) |
797 | 791 |
798 | 792 |
799 def typedef_node_to_type(node): | 793 def typedef_node_to_type(node): |
800 children = node.GetChildren() | 794 children = node.GetChildren() |
801 if len(children) != 1: | 795 if len(children) != 1: |
802 raise ValueError('Typedef node with %s children, expected 1' % len(child
ren)) | 796 raise ValueError('Typedef node with %s children, expected 1' % len(child
ren)) |
803 child = children[0] | 797 child = children[0] |
804 child_class = child.GetClass() | 798 child_class = child.GetClass() |
805 if child_class != 'Type': | 799 if child_class != 'Type': |
806 raise ValueError('Unrecognized node class: %s' % child_class) | 800 raise ValueError('Unrecognized node class: %s' % child_class) |
807 return type_node_to_type(child) | 801 return type_node_to_type(child) |
808 | 802 |
809 | 803 |
810 def union_type_node_to_idl_union_type(node, is_nullable=False): | 804 def union_type_node_to_idl_union_type(node, is_nullable=False): |
811 member_types = [type_node_to_type(member_type_node) | 805 member_types = [type_node_to_type(member_type_node) |
812 for member_type_node in node.GetChildren()] | 806 for member_type_node in node.GetChildren()] |
813 return IdlUnionType(member_types, is_nullable=is_nullable) | 807 return IdlUnionType(member_types, is_nullable=is_nullable) |
OLD | NEW |