| 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, IdlArrayType, IdlSequenceType |
| 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 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 | 748 |
| 749 ################################################################################ | 749 ################################################################################ |
| 750 # Types | 750 # Types |
| 751 ################################################################################ | 751 ################################################################################ |
| 752 | 752 |
| 753 def type_node_to_type(node): | 753 def type_node_to_type(node): |
| 754 children = node.GetChildren() | 754 children = node.GetChildren() |
| 755 if len(children) < 1 or len(children) > 2: | 755 if len(children) < 1 or len(children) > 2: |
| 756 raise ValueError('Type node expects 1 or 2 children (type + optional arr
ay []), got %s (multi-dimensional arrays are not supported).' % len(children)) | 756 raise ValueError('Type node expects 1 or 2 children (type + optional arr
ay []), got %s (multi-dimensional arrays are not supported).' % len(children)) |
| 757 | 757 |
| 758 is_nullable = node.GetProperty('NULLABLE') or False # syntax: T? |
| 758 type_node_child = children[0] | 759 type_node_child = children[0] |
| 760 base_type = type_node_inner_to_type(type_node_child, is_nullable=is_nullable
) |
| 759 | 761 |
| 760 if len(children) == 2: | 762 if len(children) == 2: |
| 761 array_node = children[1] | 763 array_node = children[1] |
| 762 array_node_class = array_node.GetClass() | 764 array_node_class = array_node.GetClass() |
| 763 if array_node_class != 'Array': | 765 if array_node_class != 'Array': |
| 764 raise ValueError('Expected Array node as TypeSuffix, got %s node.' %
array_node_class) | 766 raise ValueError('Expected Array node as TypeSuffix, got %s node.' %
array_node_class) |
| 765 # FIXME: use IdlArrayType instead of is_array, once have that | 767 array_is_nullable = array_node.GetProperty('NULLABLE') or False |
| 766 is_array = True | 768 return IdlArrayType(base_type, is_nullable=array_is_nullable) |
| 767 else: | |
| 768 is_array = False | |
| 769 | 769 |
| 770 is_nullable = node.GetProperty('NULLABLE') or False # syntax: T? | 770 return base_type |
| 771 | |
| 772 return type_node_inner_to_type(type_node_child, is_array=is_array, is_nullab
le=is_nullable) | |
| 773 | 771 |
| 774 | 772 |
| 775 def type_node_inner_to_type(node, is_array=False, is_nullable=False): | 773 def type_node_inner_to_type(node, is_nullable=False): |
| 776 # FIXME: remove is_array and is_nullable once have IdlArrayType and IdlNulla
bleType | 774 # FIXME: remove is_nullable once have IdlNullableType |
| 777 node_class = node.GetClass() | 775 node_class = node.GetClass() |
| 778 # Note Type*r*ef, not Typedef, meaning the type is an identifier, thus | 776 # Note Type*r*ef, not Typedef, meaning the type is an identifier, thus |
| 779 # either a typedef shorthand (but not a Typedef declaration itself) or an | 777 # either a typedef shorthand (but not a Typedef declaration itself) or an |
| 780 # interface type. We do not distinguish these, and just use the type name. | 778 # interface type. We do not distinguish these, and just use the type name. |
| 781 if node_class in ['PrimitiveType', 'Typeref']: | 779 if node_class in ['PrimitiveType', 'Typeref']: |
| 782 # unrestricted syntax: unrestricted double | unrestricted float | 780 # unrestricted syntax: unrestricted double | unrestricted float |
| 783 is_unrestricted = node.GetProperty('UNRESTRICTED') or False | 781 is_unrestricted = node.GetProperty('UNRESTRICTED') or False |
| 784 return IdlType(node.GetName(), is_array=is_array, is_nullable=is_nullabl
e, is_unrestricted=is_unrestricted) | 782 return IdlType(node.GetName(), is_nullable=is_nullable, is_unrestricted=
is_unrestricted) |
| 785 elif node_class == 'Any': | 783 elif node_class == 'Any': |
| 786 return IdlType('any', is_array=is_array, is_nullable=is_nullable) | 784 return IdlType('any', is_nullable=is_nullable) |
| 787 elif node_class == 'Sequence': | 785 elif node_class == 'Sequence': |
| 788 if is_array: | |
| 789 raise ValueError('Arrays of sequences are not supported') | |
| 790 sequence_is_nullable = node.GetProperty('NULLABLE') or False | 786 sequence_is_nullable = node.GetProperty('NULLABLE') or False |
| 791 return sequence_node_to_type(node, is_nullable=sequence_is_nullable) | 787 return sequence_node_to_type(node, is_nullable=sequence_is_nullable) |
| 792 elif node_class == 'UnionType': | 788 elif node_class == 'UnionType': |
| 793 if is_array: | |
| 794 raise ValueError('Arrays of unions are not supported') | |
| 795 return union_type_node_to_idl_union_type(node, is_nullable=is_nullable) | 789 return union_type_node_to_idl_union_type(node, is_nullable=is_nullable) |
| 796 raise ValueError('Unrecognized node class: %s' % node_class) | 790 raise ValueError('Unrecognized node class: %s' % node_class) |
| 797 | 791 |
| 798 | 792 |
| 799 def sequence_node_to_type(node, is_nullable=False): | 793 def sequence_node_to_type(node, is_nullable=False): |
| 800 children = node.GetChildren() | 794 children = node.GetChildren() |
| 801 if len(children) != 1: | 795 if len(children) != 1: |
| 802 raise ValueError('Sequence node expects exactly 1 child, got %s' % len(c
hildren)) | 796 raise ValueError('Sequence node expects exactly 1 child, got %s' % len(c
hildren)) |
| 803 sequence_child = children[0] | 797 sequence_child = children[0] |
| 804 sequence_child_class = sequence_child.GetClass() | 798 sequence_child_class = sequence_child.GetClass() |
| 805 if sequence_child_class != 'Type': | 799 if sequence_child_class != 'Type': |
| 806 raise ValueError('Unrecognized node class: %s' % sequence_child_class) | 800 raise ValueError('Unrecognized node class: %s' % sequence_child_class) |
| 807 element_type = type_node_to_type(sequence_child).base_type | 801 element_type = type_node_to_type(sequence_child) |
| 808 return IdlType(element_type, is_sequence=True, is_nullable=is_nullable) | 802 return IdlSequenceType(element_type, is_nullable=is_nullable) |
| 809 | 803 |
| 810 | 804 |
| 811 def typedef_node_to_type(node): | 805 def typedef_node_to_type(node): |
| 812 children = node.GetChildren() | 806 children = node.GetChildren() |
| 813 if len(children) != 1: | 807 if len(children) != 1: |
| 814 raise ValueError('Typedef node with %s children, expected 1' % len(child
ren)) | 808 raise ValueError('Typedef node with %s children, expected 1' % len(child
ren)) |
| 815 child = children[0] | 809 child = children[0] |
| 816 child_class = child.GetClass() | 810 child_class = child.GetClass() |
| 817 if child_class != 'Type': | 811 if child_class != 'Type': |
| 818 raise ValueError('Unrecognized node class: %s' % child_class) | 812 raise ValueError('Unrecognized node class: %s' % child_class) |
| 819 return type_node_to_type(child) | 813 return type_node_to_type(child) |
| 820 | 814 |
| 821 | 815 |
| 822 def union_type_node_to_idl_union_type(node, is_nullable=False): | 816 def union_type_node_to_idl_union_type(node, is_nullable=False): |
| 823 member_types = [type_node_to_type(member_type_node) | 817 member_types = [type_node_to_type(member_type_node) |
| 824 for member_type_node in node.GetChildren()] | 818 for member_type_node in node.GetChildren()] |
| 825 return IdlUnionType(member_types, is_nullable=is_nullable) | 819 return IdlUnionType(member_types, is_nullable=is_nullable) |
| OLD | NEW |