OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import v8_utilities | 5 import v8_utilities |
6 | 6 |
7 | 7 |
8 UNION_H_INCLUDES = frozenset([ | 8 UNION_H_INCLUDES = frozenset([ |
9 'bindings/core/v8/Dictionary.h', | 9 'bindings/core/v8/Dictionary.h', |
10 'bindings/core/v8/ExceptionState.h', | 10 'bindings/core/v8/ExceptionState.h', |
11 'bindings/core/v8/V8Binding.h', | 11 'bindings/core/v8/V8Binding.h', |
12 'platform/heap/Handle.h', | 12 'platform/heap/Handle.h', |
13 ]) | 13 ]) |
14 | 14 |
15 UNION_CPP_INCLUDES_BLACKLIST = frozenset([ | 15 UNION_CPP_INCLUDES_BLACKLIST = frozenset([ |
16 # This header defines static functions needed to implement event handler | 16 # This header defines static functions needed to implement event handler |
17 # attributes in interfaces that implement GlobalEventHandlers. They are not | 17 # attributes in interfaces that implement GlobalEventHandlers. They are not |
18 # needed or used by UnionTypes*.cpp, so including the header causes | 18 # needed or used by UnionTypes*.cpp, so including the header causes |
19 # compilation errors. | 19 # compilation errors. |
20 # FIXME: We should solve this problem in a way that doesn't involve special- | 20 # FIXME: We should solve this problem in a way that doesn't involve special- |
21 # casing a header like this. | 21 # casing a header like this. |
22 'core/dom/GlobalEventHandlers.h', | 22 'core/dom/GlobalEventHandlers.h', |
23 ]) | 23 ]) |
24 | 24 |
25 | 25 |
26 cpp_includes = set() | 26 cpp_includes = set() |
27 header_forward_decls = set() | 27 header_forward_decls = set() |
| 28 header_includes = set() |
28 | 29 |
29 | 30 |
30 def union_context(union_types, interfaces_info): | 31 def union_context(union_types, interfaces_info): |
31 cpp_includes.clear() | 32 cpp_includes.clear() |
32 header_forward_decls.clear() | 33 header_forward_decls.clear() |
| 34 header_includes.clear() |
| 35 header_includes.update(UNION_H_INCLUDES) |
33 | 36 |
34 # For container classes we strip nullable wrappers. For example, | 37 # For container classes we strip nullable wrappers. For example, |
35 # both (A or B)? and (A? or B) will become AOrB. This should be OK | 38 # both (A or B)? and (A? or B) will become AOrB. This should be OK |
36 # because container classes can handle null and it seems that | 39 # because container classes can handle null and it seems that |
37 # distinguishing (A or B)? and (A? or B) doesn't make sense. | 40 # distinguishing (A or B)? and (A? or B) doesn't make sense. |
38 container_cpp_types = set() | 41 container_cpp_types = set() |
39 union_types_for_containers = set() | 42 union_types_for_containers = set() |
40 nullable_cpp_types = set() | |
41 for union_type in union_types: | 43 for union_type in union_types: |
42 cpp_type = union_type.cpp_type | 44 cpp_type = union_type.cpp_type |
43 if cpp_type not in container_cpp_types: | 45 if cpp_type not in container_cpp_types: |
44 union_types_for_containers.add(union_type) | 46 union_types_for_containers.add(union_type) |
45 container_cpp_types.add(cpp_type) | 47 container_cpp_types.add(cpp_type) |
46 if union_type.includes_nullable_type: | |
47 nullable_cpp_types.add(cpp_type) | |
48 | 48 |
49 union_types_for_containers = sorted(union_types_for_containers, | 49 union_types_for_containers = sorted(union_types_for_containers, |
50 key=lambda union_type: union_type.cpp_ty
pe) | 50 key=lambda union_type: union_type.cpp_ty
pe) |
51 nullable_cpp_types = sorted(nullable_cpp_types) | |
52 | 51 |
53 return { | 52 return { |
54 'containers': [container_context(union_type, interfaces_info) | 53 'containers': [container_context(union_type, interfaces_info) |
55 for union_type in union_types_for_containers], | 54 for union_type in union_types_for_containers], |
56 'cpp_includes': sorted(cpp_includes - UNION_CPP_INCLUDES_BLACKLIST), | 55 'cpp_includes': sorted(cpp_includes - UNION_CPP_INCLUDES_BLACKLIST), |
57 'header_forward_decls': sorted(header_forward_decls), | 56 'header_forward_decls': sorted(header_forward_decls), |
58 'header_includes': sorted(UNION_H_INCLUDES), | 57 'header_includes': sorted(header_includes), |
59 'nullable_cpp_types': nullable_cpp_types, | |
60 } | 58 } |
61 | 59 |
62 | 60 |
63 def container_context(union_type, interfaces_info): | 61 def container_context(union_type, interfaces_info): |
64 members = [] | 62 members = [] |
65 | 63 |
66 # These variables refer to member contexts if the given union type has | 64 # These variables refer to member contexts if the given union type has |
67 # corresponding types. They are used for V8 -> impl conversion. | 65 # corresponding types. They are used for V8 -> impl conversion. |
68 array_buffer_type = None | 66 array_buffer_type = None |
69 array_buffer_view_type = None | 67 array_buffer_view_type = None |
70 array_or_sequence_type = None | 68 array_or_sequence_type = None |
71 boolean_type = None | 69 boolean_type = None |
72 dictionary_type = None | 70 dictionary_type = None |
73 interface_types = [] | 71 interface_types = [] |
74 numeric_type = None | 72 numeric_type = None |
| 73 object_type = None |
75 string_type = None | 74 string_type = None |
76 for member in union_type.member_types: | 75 for member in union_type.member_types: |
77 context = member_context(member, interfaces_info) | 76 context = member_context(member, interfaces_info) |
78 members.append(context) | 77 members.append(context) |
79 if member.base_type == 'ArrayBuffer': | 78 if member.base_type == 'ArrayBuffer': |
80 if array_buffer_type: | 79 if array_buffer_type: |
81 raise Exception('%s is ambiguous.' % union_type.name) | 80 raise Exception('%s is ambiguous.' % union_type.name) |
82 array_buffer_type = context | 81 array_buffer_type = context |
83 elif member.base_type == 'ArrayBufferView': | 82 elif member.base_type == 'ArrayBufferView': |
84 if array_buffer_view_type: | 83 if array_buffer_view_type: |
85 raise Exception('%s is ambiguous.' % union_type.name) | 84 raise Exception('%s is ambiguous.' % union_type.name) |
86 array_buffer_view_type = context | 85 array_buffer_view_type = context |
87 # FIXME: Remove generic Dictionary special casing. | 86 elif member.is_dictionary: |
88 elif member.is_dictionary or member.base_type == 'Dictionary': | |
89 if dictionary_type: | 87 if dictionary_type: |
90 raise Exception('%s is ambiguous.' % union_type.name) | 88 raise Exception('%s is ambiguous.' % union_type.name) |
91 dictionary_type = context | 89 dictionary_type = context |
92 elif member.is_array_or_sequence_type: | 90 elif member.is_array_or_sequence_type: |
93 if array_or_sequence_type: | 91 if array_or_sequence_type: |
94 raise Exception('%s is ambiguous.' % union_type.name) | 92 raise Exception('%s is ambiguous.' % union_type.name) |
95 array_or_sequence_type = context | 93 array_or_sequence_type = context |
| 94 # "Dictionary" is an object, rather than an IDL dictionary. |
| 95 elif member.base_type == 'Dictionary': |
| 96 if object_type: |
| 97 raise Exception('%s is ambiguous.' % union_type.name) |
| 98 object_type = context |
96 elif member.is_interface_type: | 99 elif member.is_interface_type: |
97 interface_types.append(context) | 100 interface_types.append(context) |
98 elif member is union_type.boolean_member_type: | 101 elif member is union_type.boolean_member_type: |
99 boolean_type = context | 102 boolean_type = context |
100 elif member is union_type.numeric_member_type: | 103 elif member is union_type.numeric_member_type: |
101 numeric_type = context | 104 numeric_type = context |
102 elif member is union_type.string_member_type: | 105 elif member is union_type.string_member_type: |
103 string_type = context | 106 string_type = context |
104 else: | 107 else: |
105 raise Exception('%s is not supported as an union member.' % member.n
ame) | 108 raise Exception('%s is not supported as an union member.' % member.n
ame) |
106 | 109 |
107 # Nullable restriction checks | 110 # Nullable restriction checks |
108 nullable_members = union_type.number_of_nullable_member_types | 111 nullable_members = union_type.number_of_nullable_member_types |
109 if nullable_members > 1: | 112 if nullable_members > 1: |
110 raise Exception('%s contains more than one nullable members' % union_typ
e.name) | 113 raise Exception('%s contains more than one nullable members' % union_typ
e.name) |
111 if dictionary_type and nullable_members == 1: | 114 if dictionary_type and nullable_members == 1: |
112 raise Exception('%s has a dictionary and a nullable member' % union_type
.name) | 115 raise Exception('%s has a dictionary and a nullable member' % union_type
.name) |
113 | 116 |
114 return { | 117 return { |
115 'array_buffer_type': array_buffer_type, | 118 'array_buffer_type': array_buffer_type, |
116 'array_buffer_view_type': array_buffer_view_type, | 119 'array_buffer_view_type': array_buffer_view_type, |
117 'array_or_sequence_type': array_or_sequence_type, | 120 'array_or_sequence_type': array_or_sequence_type, |
118 'boolean_type': boolean_type, | 121 'boolean_type': boolean_type, |
119 'cpp_class': union_type.cpp_type, | 122 'cpp_class': union_type.cpp_type, |
120 'dictionary_type': dictionary_type, | 123 'dictionary_type': dictionary_type, |
121 'includes_nullable_type': union_type.includes_nullable_type, | 124 'includes_nullable_type': union_type.includes_nullable_type, |
122 'interface_types': interface_types, | 125 'interface_types': interface_types, |
123 'members': members, | 126 'members': members, |
124 'numeric_type': numeric_type, | 127 'numeric_type': numeric_type, |
| 128 'object_type': object_type, |
125 'string_type': string_type, | 129 'string_type': string_type, |
126 'type_string': str(union_type), | 130 'type_string': str(union_type), |
127 } | 131 } |
128 | 132 |
129 | 133 |
| 134 def _update_includes_and_forward_decls(member, interface_info): |
| 135 if interface_info: |
| 136 cpp_includes.update(interface_info.get( |
| 137 'dependencies_include_paths', [])) |
| 138 # TODO(bashi): Workaround for http://crbug.com/524424 |
| 139 # Avoid using forward declaration for IDL dictionaries so that they |
| 140 # aren't imcomplete types in UnionTypes.h. This enables an IDL |
| 141 # dictionary to have a union type which has an IDL dictionary. e.g. |
| 142 # dictionary DictA { (boolean or DictB) member; } |
| 143 # Note that this doesn't cover all cases. We still can't use an IDL |
| 144 # dictionary in a union type when the dictionary contains a union type. |
| 145 # e.g. |
| 146 # void foo((DOMString or DictA) arg); // won't compile |
| 147 if member.is_dictionary: |
| 148 header_includes.update(member.includes_for_type()) |
| 149 else: |
| 150 cpp_includes.update(member.includes_for_type()) |
| 151 header_forward_decls.add(member.implemented_as) |
| 152 else: |
| 153 cpp_includes.update(member.includes_for_type()) |
| 154 |
| 155 |
130 def member_context(member, interfaces_info): | 156 def member_context(member, interfaces_info): |
131 cpp_includes.update(member.includes_for_type()) | |
132 interface_info = interfaces_info.get(member.name, None) | 157 interface_info = interfaces_info.get(member.name, None) |
133 if interface_info: | 158 _update_includes_and_forward_decls(member, interface_info) |
134 cpp_includes.update(interface_info.get('dependencies_include_paths', [])
) | |
135 header_forward_decls.add(member.implemented_as) | |
136 if member.is_nullable: | 159 if member.is_nullable: |
137 member = member.inner_type | 160 member = member.inner_type |
138 return { | 161 return { |
139 'cpp_name': v8_utilities.uncapitalize(member.name), | 162 'cpp_name': v8_utilities.uncapitalize(member.name), |
140 'cpp_type': member.cpp_type_args(used_in_cpp_sequence=True), | 163 'cpp_type': member.cpp_type_args(used_in_cpp_sequence=True), |
141 'cpp_local_type': member.cpp_type, | 164 'cpp_local_type': member.cpp_type, |
142 'cpp_value_to_v8_value': member.cpp_value_to_v8_value( | 165 'cpp_value_to_v8_value': member.cpp_value_to_v8_value( |
143 cpp_value='impl.getAs%s()' % member.name, isolate='isolate', | 166 cpp_value='impl.getAs%s()' % member.name, isolate='isolate', |
144 creation_context='creationContext'), | 167 creation_context='creationContext'), |
145 'enum_values': member.enum_values, | 168 'enum_values': member.enum_values, |
146 'is_traceable': member.is_traceable, | 169 'is_traceable': member.is_traceable, |
147 'rvalue_cpp_type': member.cpp_type_args(used_as_rvalue_type=True), | 170 'rvalue_cpp_type': member.cpp_type_args(used_as_rvalue_type=True), |
148 'specific_type_enum': 'SpecificType' + member.name, | 171 'specific_type_enum': 'SpecificType' + member.name, |
149 'type_name': member.name, | 172 'type_name': member.name, |
150 'v8_value_to_local_cpp_value': member.v8_value_to_local_cpp_value( | 173 'v8_value_to_local_cpp_value': member.v8_value_to_local_cpp_value( |
151 {}, 'v8Value', 'cppValue', isolate='isolate', | 174 {}, 'v8Value', 'cppValue', isolate='isolate', |
152 use_exception_state=True, restricted_float=True), | 175 use_exception_state=True, restricted_float=True), |
153 } | 176 } |
OLD | NEW |