Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Side by Side Diff: bindings/scripts/idl_definitions.py

Issue 1660113002: Updated to Chrome 45 (2454) moved from SVN to git. Base URL: https://github.com/dart-lang/webcore.git@roll_45
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « bindings/scripts/idl_compiler.py ('k') | bindings/scripts/idl_reader.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13 matching lines...) Expand all
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 28
29 """Blink IDL Intermediate Representation (IR) classes. 29 """Blink IDL Intermediate Representation (IR) classes.
30 30
31 Classes are primarily constructors, which build an IdlDefinitions object 31 Classes are primarily constructors, which build an IdlDefinitions object
32 (and various contained objects) from an AST (produced by blink_idl_parser). 32 (and various contained objects) from an AST (produced by blink_idl_parser).
33 33
34 This is in two steps: 34 IR stores typedefs and they are resolved by the code generator.
35 * Constructors walk the AST, creating objects.
36 * Typedef resolution.
37
38 Typedefs are all resolved here, and not stored in IR.
39 35
40 Typedef resolution uses some auxiliary classes and OOP techniques to make this 36 Typedef resolution uses some auxiliary classes and OOP techniques to make this
41 a generic call, via the resolve_typedefs() method. 37 a generic call. See TypedefResolver class in code_generator_v8.py.
42 38
43 Class hierarchy (mostly containment, '<' for inheritance): 39 Class hierarchy (mostly containment, '<' for inheritance):
44 40
45 IdlDefinitions 41 IdlDefinitions
46 IdlCallbackFunction < TypedObject 42 IdlCallbackFunction < TypedObject
47 IdlEnum :: FIXME: remove, just use a dict for enums 43 IdlEnum :: FIXME: remove, just use a dict for enums
48 IdlInterface 44 IdlInterface
49 IdlAttribute < TypedObject 45 IdlAttribute < TypedObject
50 IdlConstant < TypedObject 46 IdlConstant < TypedObject
51 IdlLiteral 47 IdlLiteral
52 IdlOperation < TypedObject 48 IdlOperation < TypedObject
53 IdlArgument < TypedObject 49 IdlArgument < TypedObject
50 IdlSerializer
54 IdlStringifier 51 IdlStringifier
52 IdlIterable < IdlIterableOrMaplikeOrSetlike
53 IdlMaplike < IdlIterableOrMaplikeOrSetlike
54 IdlSetlike < IdlIterableOrMaplikeOrSetlike
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 :: Object with one or more attributes that is a type.
59
60 IdlArgument is 'picklable', as it is stored in interfaces_info.
59 61
60 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler 62 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler
61 """ 63 """
62 64
63 import abc 65 import abc
64 66
65 from idl_types import IdlType, IdlUnionType, IdlArrayType, IdlSequenceType, IdlN ullableType 67 from idl_types import IdlType, IdlUnionType, IdlArrayType, IdlSequenceType, IdlN ullableType
66 68
67 SPECIAL_KEYWORD_LIST = ['GETTER', 'SETTER', 'DELETER'] 69 SPECIAL_KEYWORD_LIST = ['GETTER', 'SETTER', 'DELETER']
68 STANDARD_TYPEDEFS = {
69 # http://www.w3.org/TR/WebIDL/#common-DOMTimeStamp
70 'DOMTimeStamp': 'unsigned long long',
71 }
72 70
73 71
74 ################################################################################ 72 ################################################################################
75 # TypedObject (mixin for typedef resolution) 73 # TypedObject
76 ################################################################################ 74 ################################################################################
77 75
78 class TypedObject(object): 76 class TypedObject(object):
79 """Object with a type, such as an Attribute or Operation (return value). 77 """Object with a type, such as an Attribute or Operation (return value).
80 78
81 The type can be an actual type, or can be a typedef, which must be resolved 79 The type can be an actual type, or can be a typedef, which must be resolved
82 before passing data to the code generator. 80 by the TypedefResolver before passing data to the code generator.
83 """ 81 """
84 __metaclass__ = abc.ABCMeta 82 __metaclass__ = abc.ABCMeta
85 idl_type = None 83 idl_type_attributes = ('idl_type',)
86
87 def resolve_typedefs(self, typedefs):
88 """Resolve typedefs to actual types in the object."""
89 # Constructors don't have their own return type, because it's the
90 # interface itself.
91 if not self.idl_type:
92 return
93 # Need to re-assign self.idl_type, not just mutate idl_type,
94 # since type(idl_type) may change.
95 self.idl_type = self.idl_type.resolve_typedefs(typedefs)
96 84
97 85
98 ################################################################################ 86 ################################################################################
99 # Definitions (main container class) 87 # Definitions (main container class)
100 ################################################################################ 88 ################################################################################
101 89
102 class IdlDefinitions(object): 90 class IdlDefinitions(object):
103 def __init__(self, idl_name, node): 91 def __init__(self, idl_name, node):
104 """Args: node: AST root node, class == 'File'""" 92 """Args: node: AST root node, class == 'File'"""
105 self.callback_functions = {} 93 self.callback_functions = {}
106 self.dictionaries = {} 94 self.dictionaries = {}
107 self.enumerations = {} 95 self.enumerations = {}
96 self.implements = []
108 self.interfaces = {} 97 self.interfaces = {}
109 self.idl_name = idl_name 98 self.idl_name = idl_name
99 self.typedefs = {}
110 100
111 node_class = node.GetClass() 101 node_class = node.GetClass()
112 if node_class != 'File': 102 if node_class != 'File':
113 raise ValueError('Unrecognized node class: %s' % node_class) 103 raise ValueError('Unrecognized node class: %s' % node_class)
114 104
115 typedefs = dict((typedef_name, IdlType(type_name))
116 for typedef_name, type_name in
117 STANDARD_TYPEDEFS.iteritems())
118
119 children = node.GetChildren() 105 children = node.GetChildren()
120 for child in children: 106 for child in children:
121 child_class = child.GetClass() 107 child_class = child.GetClass()
122 if child_class == 'Interface': 108 if child_class == 'Interface':
123 interface = IdlInterface(idl_name, child) 109 interface = IdlInterface(idl_name, child)
124 self.interfaces[interface.name] = interface 110 self.interfaces[interface.name] = interface
125 elif child_class == 'Exception': 111 elif child_class == 'Exception':
126 exception = IdlException(idl_name, child) 112 exception = IdlException(idl_name, child)
127 # For simplicity, treat exceptions as interfaces 113 # For simplicity, treat exceptions as interfaces
128 self.interfaces[exception.name] = exception 114 self.interfaces[exception.name] = exception
129 elif child_class == 'Typedef': 115 elif child_class == 'Typedef':
130 type_name = child.GetName() 116 typedef = IdlTypedef(child)
131 typedefs[type_name] = typedef_node_to_type(child) 117 self.typedefs[typedef.name] = typedef
132 elif child_class == 'Enum': 118 elif child_class == 'Enum':
133 enumeration = IdlEnum(idl_name, child) 119 enumeration = IdlEnum(idl_name, child)
134 self.enumerations[enumeration.name] = enumeration 120 self.enumerations[enumeration.name] = enumeration
135 elif child_class == 'Callback': 121 elif child_class == 'Callback':
136 callback_function = IdlCallbackFunction(idl_name, child) 122 callback_function = IdlCallbackFunction(idl_name, child)
137 self.callback_functions[callback_function.name] = callback_funct ion 123 self.callback_functions[callback_function.name] = callback_funct ion
138 elif child_class == 'Implements': 124 elif child_class == 'Implements':
139 # Implements is handled at the interface merging step 125 self.implements.append(IdlImplement(child))
140 pass
141 elif child_class == 'Dictionary': 126 elif child_class == 'Dictionary':
142 dictionary = IdlDictionary(idl_name, child) 127 dictionary = IdlDictionary(idl_name, child)
143 self.dictionaries[dictionary.name] = dictionary 128 self.dictionaries[dictionary.name] = dictionary
144 else: 129 else:
145 raise ValueError('Unrecognized node class: %s' % child_class) 130 raise ValueError('Unrecognized node class: %s' % child_class)
146 131
147 # Typedefs are not stored in IR: 132 def accept(self, visitor):
148 # Resolve typedefs with the actual types and then discard the Typedefs. 133 visitor.visit_definitions(self)
149 # http://www.w3.org/TR/WebIDL/#idl-typedefs 134 for interface in self.interfaces.itervalues():
150 self.resolve_typedefs(typedefs) 135 interface.accept(visitor)
151
152 def resolve_typedefs(self, typedefs):
153 for callback_function in self.callback_functions.itervalues(): 136 for callback_function in self.callback_functions.itervalues():
154 callback_function.resolve_typedefs(typedefs) 137 callback_function.accept(visitor)
155 for interface in self.interfaces.itervalues(): 138 for dictionary in self.dictionaries.itervalues():
156 interface.resolve_typedefs(typedefs) 139 dictionary.accept(visitor)
140 for typedef in self.typedefs.itervalues():
141 typedef.accept(visitor)
157 142
158 def update(self, other): 143 def update(self, other):
159 """Update with additional IdlDefinitions.""" 144 """Update with additional IdlDefinitions."""
160 for interface_name, new_interface in other.interfaces.iteritems(): 145 for interface_name, new_interface in other.interfaces.iteritems():
161 if not new_interface.is_partial: 146 if not new_interface.is_partial:
162 # Add as new interface 147 # Add as new interface
163 self.interfaces[interface_name] = new_interface 148 self.interfaces[interface_name] = new_interface
164 continue 149 continue
165 150
166 # Merge partial to existing interface 151 # Merge partial to existing interface
(...skipping 22 matching lines...) Expand all
189 type_node, arguments_node = children 174 type_node, arguments_node = children
190 arguments_node_class = arguments_node.GetClass() 175 arguments_node_class = arguments_node.GetClass()
191 if arguments_node_class != 'Arguments': 176 if arguments_node_class != 'Arguments':
192 raise ValueError('Expected Arguments node, got %s' % arguments_node_ class) 177 raise ValueError('Expected Arguments node, got %s' % arguments_node_ class)
193 178
194 self.idl_name = idl_name 179 self.idl_name = idl_name
195 self.name = node.GetName() 180 self.name = node.GetName()
196 self.idl_type = type_node_to_type(type_node) 181 self.idl_type = type_node_to_type(type_node)
197 self.arguments = arguments_node_to_arguments(idl_name, arguments_node) 182 self.arguments = arguments_node_to_arguments(idl_name, arguments_node)
198 183
199 def resolve_typedefs(self, typedefs): 184 def accept(self, visitor):
200 TypedObject.resolve_typedefs(self, typedefs) 185 visitor.visit_callback_function(self)
201 for argument in self.arguments: 186 for argument in self.arguments:
202 argument.resolve_typedefs(typedefs) 187 argument.accept(visitor)
203 188
204 189
205 ################################################################################ 190 ################################################################################
206 # Dictionary 191 # Dictionary
207 ################################################################################ 192 ################################################################################
208 193
209 class IdlDictionary(object): 194 class IdlDictionary(object):
210 def __init__(self, idl_name, node): 195 def __init__(self, idl_name, node):
211 self.extended_attributes = {} 196 self.extended_attributes = {}
212 self.is_partial = node.GetProperty('Partial') or False 197 self.is_partial = bool(node.GetProperty('Partial'))
213 self.idl_name = idl_name 198 self.idl_name = idl_name
214 self.name = node.GetName() 199 self.name = node.GetName()
215 self.members = [] 200 self.members = []
216 self.parent = None 201 self.parent = None
217 for child in node.GetChildren(): 202 for child in node.GetChildren():
218 child_class = child.GetClass() 203 child_class = child.GetClass()
219 if child_class == 'Inherit': 204 if child_class == 'Inherit':
220 self.parent = child.GetName() 205 self.parent = child.GetName()
221 elif child_class == 'Key': 206 elif child_class == 'Key':
222 self.members.append(IdlDictionaryMember(idl_name, child)) 207 self.members.append(IdlDictionaryMember(idl_name, child))
223 elif child_class == 'ExtAttributes': 208 elif child_class == 'ExtAttributes':
224 self.extended_attributes = ( 209 self.extended_attributes = (
225 ext_attributes_node_to_extended_attributes(idl_name, child)) 210 ext_attributes_node_to_extended_attributes(idl_name, child))
226 else: 211 else:
227 raise ValueError('Unrecognized node class: %s' % child_class) 212 raise ValueError('Unrecognized node class: %s' % child_class)
228 213
214 def accept(self, visitor):
215 visitor.visit_dictionary(self)
216 for member in self.members:
217 member.accept(visitor)
229 218
230 class IdlDictionaryMember(object): 219
220 class IdlDictionaryMember(TypedObject):
231 def __init__(self, idl_name, node): 221 def __init__(self, idl_name, node):
232 self.default_value = None 222 self.default_value = None
233 self.extended_attributes = {} 223 self.extended_attributes = {}
234 self.idl_type = None 224 self.idl_type = None
235 self.idl_name = idl_name 225 self.idl_name = idl_name
226 self.is_required = bool(node.GetProperty('REQUIRED'))
236 self.name = node.GetName() 227 self.name = node.GetName()
237 for child in node.GetChildren(): 228 for child in node.GetChildren():
238 child_class = child.GetClass() 229 child_class = child.GetClass()
239 if child_class == 'Type': 230 if child_class == 'Type':
240 self.idl_type = type_node_to_type(child) 231 self.idl_type = type_node_to_type(child)
241 elif child_class == 'Default': 232 elif child_class == 'Default':
242 self.default_value = default_node_to_idl_literal(child) 233 self.default_value = default_node_to_idl_literal(child)
243 elif child_class == 'ExtAttributes': 234 elif child_class == 'ExtAttributes':
244 self.extended_attributes = ( 235 self.extended_attributes = (
245 ext_attributes_node_to_extended_attributes(idl_name, child)) 236 ext_attributes_node_to_extended_attributes(idl_name, child))
246 else: 237 else:
247 raise ValueError('Unrecognized node class: %s' % child_class) 238 raise ValueError('Unrecognized node class: %s' % child_class)
248 239
240 def accept(self, visitor):
241 visitor.visit_dictionary_member(self)
242
249 243
250 ################################################################################ 244 ################################################################################
251 # Enumerations 245 # Enumerations
252 ################################################################################ 246 ################################################################################
253 247
254 class IdlEnum(object): 248 class IdlEnum(object):
255 # FIXME: remove, just treat enums as a dictionary 249 # FIXME: remove, just treat enums as a dictionary
256 def __init__(self, idl_name, node): 250 def __init__(self, idl_name, node):
257 self.idl_name = idl_name 251 self.idl_name = idl_name
258 self.name = node.GetName() 252 self.name = node.GetName()
259 self.values = [] 253 self.values = []
260 for child in node.GetChildren(): 254 for child in node.GetChildren():
261 self.values.append(child.GetName()) 255 self.values.append(child.GetName())
262 256
263 257
264 ################################################################################ 258 ################################################################################
259 # Typedefs
260 ################################################################################
261
262 class IdlTypedef(object):
263 idl_type_attributes = ('idl_type',)
264
265 def __init__(self, node):
266 self.name = node.GetName()
267 self.idl_type = typedef_node_to_type(node)
268
269 def accept(self, visitor):
270 visitor.visit_typedef(self)
271
272
273 ################################################################################
265 # Interfaces and Exceptions 274 # Interfaces and Exceptions
266 ################################################################################ 275 ################################################################################
267 276
268 class IdlInterface(object): 277 class IdlInterface(object):
269 def __init__(self, idl_name, node=None): 278 def __init__(self, idl_name, node=None):
270 self.attributes = [] 279 self.attributes = []
271 self.constants = [] 280 self.constants = []
272 self.constructors = [] 281 self.constructors = []
273 self.custom_constructors = [] 282 self.custom_constructors = []
274 self.extended_attributes = {} 283 self.extended_attributes = {}
275 self.operations = [] 284 self.operations = []
276 self.parent = None 285 self.parent = None
286 self.serializer = None
277 self.stringifier = None 287 self.stringifier = None
288 self.iterable = None
289 self.maplike = None
290 self.setlike = None
291 self.original_interface = None
292 self.partial_interfaces = []
278 if not node: # Early exit for IdlException.__init__ 293 if not node: # Early exit for IdlException.__init__
279 return 294 return
280 295
281 self.is_callback = node.GetProperty('CALLBACK') or False 296 self.is_callback = bool(node.GetProperty('CALLBACK'))
282 self.is_exception = False 297 self.is_exception = False
283 # FIXME: uppercase 'Partial' => 'PARTIAL' in base IDL parser 298 # FIXME: uppercase 'Partial' => 'PARTIAL' in base IDL parser
284 self.is_partial = node.GetProperty('Partial') or False 299 self.is_partial = bool(node.GetProperty('Partial'))
285 self.idl_name = idl_name 300 self.idl_name = idl_name
286 self.name = node.GetName() 301 self.name = node.GetName()
302 self.idl_type = IdlType(self.name)
287 303
288 children = node.GetChildren() 304 children = node.GetChildren()
289 for child in children: 305 for child in children:
290 child_class = child.GetClass() 306 child_class = child.GetClass()
291 if child_class == 'Attribute': 307 if child_class == 'Attribute':
292 self.attributes.append(IdlAttribute(idl_name, child)) 308 self.attributes.append(IdlAttribute(idl_name, child))
293 elif child_class == 'Const': 309 elif child_class == 'Const':
294 self.constants.append(IdlConstant(idl_name, child)) 310 self.constants.append(IdlConstant(idl_name, child))
295 elif child_class == 'ExtAttributes': 311 elif child_class == 'ExtAttributes':
296 extended_attributes = ext_attributes_node_to_extended_attributes (idl_name, child) 312 extended_attributes = ext_attributes_node_to_extended_attributes (idl_name, child)
297 self.constructors, self.custom_constructors = ( 313 self.constructors, self.custom_constructors = (
298 extended_attributes_to_constructors(idl_name, extended_attri butes)) 314 extended_attributes_to_constructors(idl_name, extended_attri butes))
299 clear_constructor_attributes(extended_attributes) 315 clear_constructor_attributes(extended_attributes)
300 self.extended_attributes = extended_attributes 316 self.extended_attributes = extended_attributes
301 elif child_class == 'Operation': 317 elif child_class == 'Operation':
302 self.operations.append(IdlOperation(idl_name, child)) 318 self.operations.append(IdlOperation(idl_name, child))
303 elif child_class == 'Inherit': 319 elif child_class == 'Inherit':
304 self.parent = child.GetName() 320 self.parent = child.GetName()
321 elif child_class == 'Serializer':
322 self.serializer = IdlSerializer(idl_name, child)
323 self.process_serializer()
305 elif child_class == 'Stringifier': 324 elif child_class == 'Stringifier':
306 self.stringifier = IdlStringifier(idl_name, child) 325 self.stringifier = IdlStringifier(idl_name, child)
307 self.process_stringifier() 326 self.process_stringifier()
327 elif child_class == 'Iterable':
328 self.iterable = IdlIterable(idl_name, child)
329 elif child_class == 'Maplike':
330 self.maplike = IdlMaplike(idl_name, child)
331 elif child_class == 'Setlike':
332 self.setlike = IdlSetlike(idl_name, child)
308 else: 333 else:
309 raise ValueError('Unrecognized node class: %s' % child_class) 334 raise ValueError('Unrecognized node class: %s' % child_class)
310 335
311 def resolve_typedefs(self, typedefs): 336 if len(filter(None, [self.iterable, self.maplike, self.setlike])) > 1:
337 raise ValueError('Interface can only have one of iterable<>, maplike <> and setlike<>.')
338
339 def accept(self, visitor):
340 visitor.visit_interface(self)
312 for attribute in self.attributes: 341 for attribute in self.attributes:
313 attribute.resolve_typedefs(typedefs) 342 attribute.accept(visitor)
314 for constant in self.constants: 343 for constant in self.constants:
315 constant.resolve_typedefs(typedefs) 344 constant.accept(visitor)
316 for constructor in self.constructors: 345 for constructor in self.constructors:
317 constructor.resolve_typedefs(typedefs) 346 constructor.accept(visitor)
318 for custom_constructor in self.custom_constructors: 347 for custom_constructor in self.custom_constructors:
319 custom_constructor.resolve_typedefs(typedefs) 348 custom_constructor.accept(visitor)
320 for operation in self.operations: 349 for operation in self.operations:
321 operation.resolve_typedefs(typedefs) 350 operation.accept(visitor)
351 if self.iterable:
352 self.iterable.accept(visitor)
353 elif self.maplike:
354 self.maplike.accept(visitor)
355 elif self.setlike:
356 self.setlike.accept(visitor)
357
358 def process_serializer(self):
359 """Add the serializer's named operation child, if it has one, as a regul ar
360 operation of this interface."""
361 if self.serializer.operation:
362 self.operations.append(self.serializer.operation)
322 363
323 def process_stringifier(self): 364 def process_stringifier(self):
324 """Add the stringifier's attribute or named operation child, if it has 365 """Add the stringifier's attribute or named operation child, if it has
325 one, as a regular attribute/operation of this interface.""" 366 one, as a regular attribute/operation of this interface."""
326 if self.stringifier.attribute: 367 if self.stringifier.attribute:
327 self.attributes.append(self.stringifier.attribute) 368 self.attributes.append(self.stringifier.attribute)
328 elif self.stringifier.operation: 369 elif self.stringifier.operation:
329 self.operations.append(self.stringifier.operation) 370 self.operations.append(self.stringifier.operation)
330 371
331 def merge(self, other): 372 def merge(self, other):
(...skipping 11 matching lines...) Expand all
343 # restricted subclass of interfaces. 384 # restricted subclass of interfaces.
344 # http://www.w3.org/TR/WebIDL/#idl-exceptions 385 # http://www.w3.org/TR/WebIDL/#idl-exceptions
345 def __init__(self, idl_name, node): 386 def __init__(self, idl_name, node):
346 # Exceptions are similar to Interfaces, but simpler 387 # Exceptions are similar to Interfaces, but simpler
347 IdlInterface.__init__(self, idl_name) 388 IdlInterface.__init__(self, idl_name)
348 self.is_callback = False 389 self.is_callback = False
349 self.is_exception = True 390 self.is_exception = True
350 self.is_partial = False 391 self.is_partial = False
351 self.idl_name = idl_name 392 self.idl_name = idl_name
352 self.name = node.GetName() 393 self.name = node.GetName()
394 self.idl_type = IdlType(self.name)
353 395
354 children = node.GetChildren() 396 children = node.GetChildren()
355 for child in children: 397 for child in children:
356 child_class = child.GetClass() 398 child_class = child.GetClass()
357 if child_class == 'Attribute': 399 if child_class == 'Attribute':
358 attribute = IdlAttribute(idl_name, child) 400 attribute = IdlAttribute(idl_name, child)
359 self.attributes.append(attribute) 401 self.attributes.append(attribute)
360 elif child_class == 'Const': 402 elif child_class == 'Const':
361 self.constants.append(IdlConstant(idl_name, child)) 403 self.constants.append(IdlConstant(idl_name, child))
362 elif child_class == 'ExtAttributes': 404 elif child_class == 'ExtAttributes':
363 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) 405 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child)
364 elif child_class == 'ExceptionOperation': 406 elif child_class == 'ExceptionOperation':
365 self.operations.append(IdlOperation.from_exception_operation_nod e(idl_name, child)) 407 self.operations.append(IdlOperation.from_exception_operation_nod e(idl_name, child))
366 else: 408 else:
367 raise ValueError('Unrecognized node class: %s' % child_class) 409 raise ValueError('Unrecognized node class: %s' % child_class)
368 410
369 411
370 ################################################################################ 412 ################################################################################
371 # Attributes 413 # Attributes
372 ################################################################################ 414 ################################################################################
373 415
374 class IdlAttribute(TypedObject): 416 class IdlAttribute(TypedObject):
375 def __init__(self, idl_name, node): 417 def __init__(self, idl_name, node):
376 self.is_read_only = node.GetProperty('READONLY') or False 418 self.is_read_only = bool(node.GetProperty('READONLY'))
377 self.is_static = node.GetProperty('STATIC') or False 419 self.is_static = bool(node.GetProperty('STATIC'))
378 self.idl_name = idl_name 420 self.idl_name = idl_name
379 self.name = node.GetName() 421 self.name = node.GetName()
380 # Defaults, overridden below 422 # Defaults, overridden below
381 self.idl_type = None 423 self.idl_type = None
382 self.extended_attributes = {} 424 self.extended_attributes = {}
383 425
384 children = node.GetChildren() 426 children = node.GetChildren()
385 for child in children: 427 for child in children:
386 child_class = child.GetClass() 428 child_class = child.GetClass()
387 if child_class == 'Type': 429 if child_class == 'Type':
388 self.idl_type = type_node_to_type(child) 430 self.idl_type = type_node_to_type(child)
389 elif child_class == 'ExtAttributes': 431 elif child_class == 'ExtAttributes':
390 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) 432 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child)
391 else: 433 else:
392 raise ValueError('Unrecognized node class: %s' % child_class) 434 raise ValueError('Unrecognized node class: %s' % child_class)
393 435
436 def accept(self, visitor):
437 visitor.visit_attribute(self)
438
394 439
395 ################################################################################ 440 ################################################################################
396 # Constants 441 # Constants
397 ################################################################################ 442 ################################################################################
398 443
399 class IdlConstant(TypedObject): 444 class IdlConstant(TypedObject):
400 def __init__(self, idl_name, node): 445 def __init__(self, idl_name, node):
401 children = node.GetChildren() 446 children = node.GetChildren()
402 num_children = len(children) 447 num_children = len(children)
403 if num_children < 2 or num_children > 3: 448 if num_children < 2 or num_children > 3:
(...skipping 16 matching lines...) Expand all
420 self.value = value_node.GetProperty('VALUE') 465 self.value = value_node.GetProperty('VALUE')
421 else: 466 else:
422 self.value = value_node.GetName() 467 self.value = value_node.GetName()
423 468
424 if num_children == 3: 469 if num_children == 3:
425 ext_attributes_node = children[2] 470 ext_attributes_node = children[2]
426 self.extended_attributes = ext_attributes_node_to_extended_attribute s(idl_name, ext_attributes_node) 471 self.extended_attributes = ext_attributes_node_to_extended_attribute s(idl_name, ext_attributes_node)
427 else: 472 else:
428 self.extended_attributes = {} 473 self.extended_attributes = {}
429 474
475 def accept(self, visitor):
476 visitor.visit_constant(self)
477
430 478
431 ################################################################################ 479 ################################################################################
432 # Literals 480 # Literals
433 ################################################################################ 481 ################################################################################
434 482
435 class IdlLiteral(object): 483 class IdlLiteral(object):
436 def __init__(self, idl_type, value): 484 def __init__(self, idl_type, value):
437 self.idl_type = idl_type 485 self.idl_type = idl_type
438 self.value = value 486 self.value = value
439 self.is_null = False 487 self.is_null = False
(...skipping 27 matching lines...) Expand all
467 idl_type = node.GetProperty('TYPE') 515 idl_type = node.GetProperty('TYPE')
468 if idl_type == 'DOMString': 516 if idl_type == 'DOMString':
469 value = node.GetProperty('NAME') 517 value = node.GetProperty('NAME')
470 if '"' in value or '\\' in value: 518 if '"' in value or '\\' in value:
471 raise ValueError('Unsupported string value: %r' % value) 519 raise ValueError('Unsupported string value: %r' % value)
472 return IdlLiteral(idl_type, value) 520 return IdlLiteral(idl_type, value)
473 if idl_type == 'integer': 521 if idl_type == 'integer':
474 return IdlLiteral(idl_type, int(node.GetProperty('NAME'), base=0)) 522 return IdlLiteral(idl_type, int(node.GetProperty('NAME'), base=0))
475 if idl_type == 'float': 523 if idl_type == 'float':
476 return IdlLiteral(idl_type, float(node.GetProperty('VALUE'))) 524 return IdlLiteral(idl_type, float(node.GetProperty('VALUE')))
477 if idl_type == 'boolean': 525 if idl_type in ['boolean', 'sequence']:
478 return IdlLiteral(idl_type, node.GetProperty('VALUE')) 526 return IdlLiteral(idl_type, node.GetProperty('VALUE'))
479 if idl_type == 'NULL': 527 if idl_type == 'NULL':
480 return IdlLiteralNull() 528 return IdlLiteralNull()
481 raise ValueError('Unrecognized default value type: %s' % idl_type) 529 raise ValueError('Unrecognized default value type: %s' % idl_type)
482 530
483 531
484 ################################################################################ 532 ################################################################################
485 # Operations 533 # Operations
486 ################################################################################ 534 ################################################################################
487 535
488 class IdlOperation(TypedObject): 536 class IdlOperation(TypedObject):
489 def __init__(self, idl_name, node=None): 537 def __init__(self, idl_name, node=None):
490 self.arguments = [] 538 self.arguments = []
491 self.extended_attributes = {} 539 self.extended_attributes = {}
492 self.specials = [] 540 self.specials = []
493 self.is_constructor = False 541 self.is_constructor = False
542 self.idl_name = idl_name
543 self.idl_type = None
544 self.is_static = False
494 545
495 if not node: 546 if not node:
496 self.is_static = False
497 return 547 return
498 self.idl_name = idl_name 548
499 self.name = node.GetName() # FIXME: should just be: or '' 549 self.name = node.GetName() # FIXME: should just be: or ''
500 # FIXME: AST should use None internally 550 # FIXME: AST should use None internally
501 if self.name == '_unnamed_': 551 if self.name == '_unnamed_':
502 self.name = '' 552 self.name = ''
503 553
504 self.is_static = node.GetProperty('STATIC') or False 554 self.is_static = bool(node.GetProperty('STATIC'))
505 property_dictionary = node.GetProperties() 555 property_dictionary = node.GetProperties()
506 for special_keyword in SPECIAL_KEYWORD_LIST: 556 for special_keyword in SPECIAL_KEYWORD_LIST:
507 if special_keyword in property_dictionary: 557 if special_keyword in property_dictionary:
508 self.specials.append(special_keyword.lower()) 558 self.specials.append(special_keyword.lower())
509 559
510 self.idl_type = None
511 children = node.GetChildren() 560 children = node.GetChildren()
512 for child in children: 561 for child in children:
513 child_class = child.GetClass() 562 child_class = child.GetClass()
514 if child_class == 'Arguments': 563 if child_class == 'Arguments':
515 self.arguments = arguments_node_to_arguments(idl_name, child) 564 self.arguments = arguments_node_to_arguments(idl_name, child)
516 elif child_class == 'Type': 565 elif child_class == 'Type':
517 self.idl_type = type_node_to_type(child) 566 self.idl_type = type_node_to_type(child)
518 elif child_class == 'ExtAttributes': 567 elif child_class == 'ExtAttributes':
519 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) 568 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child)
520 else: 569 else:
(...skipping 21 matching lines...) Expand all
542 return operation 591 return operation
543 592
544 @classmethod 593 @classmethod
545 def constructor_from_arguments_node(cls, name, idl_name, arguments_node): 594 def constructor_from_arguments_node(cls, name, idl_name, arguments_node):
546 constructor = cls(idl_name) 595 constructor = cls(idl_name)
547 constructor.name = name 596 constructor.name = name
548 constructor.arguments = arguments_node_to_arguments(idl_name, arguments_ node) 597 constructor.arguments = arguments_node_to_arguments(idl_name, arguments_ node)
549 constructor.is_constructor = True 598 constructor.is_constructor = True
550 return constructor 599 return constructor
551 600
552 def resolve_typedefs(self, typedefs): 601 def accept(self, visitor):
553 TypedObject.resolve_typedefs(self, typedefs) 602 visitor.visit_operation(self)
554 for argument in self.arguments: 603 for argument in self.arguments:
555 argument.resolve_typedefs(typedefs) 604 argument.accept(visitor)
556 605
557 606
558 ################################################################################ 607 ################################################################################
559 # Arguments 608 # Arguments
560 ################################################################################ 609 ################################################################################
561 610
562 class IdlArgument(TypedObject): 611 class IdlArgument(TypedObject):
563 def __init__(self, idl_name, node): 612 def __init__(self, idl_name, node=None):
564 self.extended_attributes = {} 613 self.extended_attributes = {}
565 self.idl_type = None 614 self.idl_type = None
566 self.is_optional = node.GetProperty('OPTIONAL') # syntax: (optional T) 615 self.is_optional = False # syntax: (optional T)
567 self.is_variadic = False # syntax: (T...) 616 self.is_variadic = False # syntax: (T...)
568 self.idl_name = idl_name 617 self.idl_name = idl_name
618 self.default_value = None
619
620 if not node:
621 return
622
623 self.is_optional = node.GetProperty('OPTIONAL')
569 self.name = node.GetName() 624 self.name = node.GetName()
570 self.default_value = None
571 625
572 children = node.GetChildren() 626 children = node.GetChildren()
573 for child in children: 627 for child in children:
574 child_class = child.GetClass() 628 child_class = child.GetClass()
575 if child_class == 'Type': 629 if child_class == 'Type':
576 self.idl_type = type_node_to_type(child) 630 self.idl_type = type_node_to_type(child)
577 elif child_class == 'ExtAttributes': 631 elif child_class == 'ExtAttributes':
578 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child) 632 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child)
579 elif child_class == 'Argument': 633 elif child_class == 'Argument':
580 child_name = child.GetName() 634 child_name = child.GetName()
581 if child_name != '...': 635 if child_name != '...':
582 raise ValueError('Unrecognized Argument node; expected "..." , got "%s"' % child_name) 636 raise ValueError('Unrecognized Argument node; expected "..." , got "%s"' % child_name)
583 self.is_variadic = child.GetProperty('ELLIPSIS') or False 637 self.is_variadic = bool(child.GetProperty('ELLIPSIS'))
584 elif child_class == 'Default': 638 elif child_class == 'Default':
585 self.default_value = default_node_to_idl_literal(child) 639 self.default_value = default_node_to_idl_literal(child)
586 else: 640 else:
587 raise ValueError('Unrecognized node class: %s' % child_class) 641 raise ValueError('Unrecognized node class: %s' % child_class)
588 642
643 def __getstate__(self):
644 # FIXME: Return a picklable object which has enough information to
645 # unpickle.
646 return {}
647
648 def __setstate__(self, state):
649 pass
650
651 def accept(self, visitor):
652 visitor.visit_argument(self)
653
589 654
590 def arguments_node_to_arguments(idl_name, node): 655 def arguments_node_to_arguments(idl_name, node):
591 # [Constructor] and [CustomConstructor] without arguments (the bare form) 656 # [Constructor] and [CustomConstructor] without arguments (the bare form)
592 # have None instead of an arguments node, but have the same meaning as using 657 # have None instead of an arguments node, but have the same meaning as using
593 # an empty argument list, [Constructor()], so special-case this. 658 # an empty argument list, [Constructor()], so special-case this.
594 # http://www.w3.org/TR/WebIDL/#Constructor 659 # http://www.w3.org/TR/WebIDL/#Constructor
595 if node is None: 660 if node is None:
596 return [] 661 return []
597 return [IdlArgument(idl_name, argument_node) 662 return [IdlArgument(idl_name, argument_node)
598 for argument_node in node.GetChildren()] 663 for argument_node in node.GetChildren()]
599 664
600 665
601 ################################################################################ 666 ################################################################################
667 # Serializers
668 ################################################################################
669
670 class IdlSerializer(object):
671 def __init__(self, idl_name, node):
672 self.attribute_name = node.GetProperty('ATTRIBUTE')
673 self.attribute_names = None
674 self.operation = None
675 self.extended_attributes = {}
676 self.is_attribute = False
677 self.is_getter = False
678 self.is_inherit = False
679 self.is_list = False
680 self.is_map = False
681 self.idl_name = idl_name
682
683 for child in node.GetChildren():
684 child_class = child.GetClass()
685 if child_class == 'Operation':
686 self.operation = IdlOperation(idl_name, child)
687 elif child_class == 'List':
688 self.is_list = True
689 self.is_getter = bool(child.GetProperty('GETTER'))
690 self.attributes = child.GetProperty('ATTRIBUTES')
691 elif child_class == 'Map':
692 self.is_map = True
693 self.is_attribute = bool(child.GetProperty('ATTRIBUTE'))
694 self.is_getter = bool(child.GetProperty('GETTER'))
695 self.is_inherit = bool(child.GetProperty('INHERIT'))
696 self.attributes = child.GetProperty('ATTRIBUTES')
697 elif child_class == 'ExtAttributes':
698 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child)
699 else:
700 raise ValueError('Unrecognized node class: %s' % child_class)
701
702
703 ################################################################################
602 # Stringifiers 704 # Stringifiers
603 ################################################################################ 705 ################################################################################
604 706
605 class IdlStringifier(object): 707 class IdlStringifier(object):
606 def __init__(self, idl_name, node): 708 def __init__(self, idl_name, node):
607 self.attribute = None 709 self.attribute = None
608 self.operation = None 710 self.operation = None
609 self.extended_attributes = {} 711 self.extended_attributes = {}
610 self.idl_name = idl_name 712 self.idl_name = idl_name
611 713
(...skipping 11 matching lines...) Expand all
623 raise ValueError('Unrecognized node class: %s' % child_class) 725 raise ValueError('Unrecognized node class: %s' % child_class)
624 726
625 # Copy the stringifier's extended attributes (such as [Unforgable]) onto 727 # Copy the stringifier's extended attributes (such as [Unforgable]) onto
626 # the underlying attribute or operation, if there is one. 728 # the underlying attribute or operation, if there is one.
627 if self.attribute or self.operation: 729 if self.attribute or self.operation:
628 (self.attribute or self.operation).extended_attributes.update( 730 (self.attribute or self.operation).extended_attributes.update(
629 self.extended_attributes) 731 self.extended_attributes)
630 732
631 733
632 ################################################################################ 734 ################################################################################
735 # Iterable, Maplike, Setlike
736 ################################################################################
737
738 class IdlIterableOrMaplikeOrSetlike(TypedObject):
739 def __init__(self, idl_name, node):
740 self.extended_attributes = {}
741 self.type_children = []
742
743 for child in node.GetChildren():
744 child_class = child.GetClass()
745 if child_class == 'ExtAttributes':
746 self.extended_attributes = ext_attributes_node_to_extended_attri butes(idl_name, child)
747 elif child_class == 'Type':
748 self.type_children.append(child)
749 else:
750 raise ValueError('Unrecognized node class: %s' % child_class)
751
752
753 class IdlIterable(IdlIterableOrMaplikeOrSetlike):
754 idl_type_attributes = ('key_type', 'value_type')
755
756 def __init__(self, idl_name, node):
757 super(IdlIterable, self).__init__(idl_name, node)
758
759 if len(self.type_children) == 1:
760 self.key_type = None
761 self.value_type = type_node_to_type(self.type_children[0])
762 elif len(self.type_children) == 2:
763 self.key_type = type_node_to_type(self.type_children[0])
764 self.value_type = type_node_to_type(self.type_children[1])
765 else:
766 raise ValueError('Unexpected number of type children: %d' % len(self .type_children))
767 del self.type_children
768
769 def accept(self, visitor):
770 visitor.visit_iterable(self)
771
772
773 class IdlMaplike(IdlIterableOrMaplikeOrSetlike):
774 idl_type_attributes = ('key_type', 'value_type')
775
776 def __init__(self, idl_name, node):
777 super(IdlMaplike, self).__init__(idl_name, node)
778
779 self.is_read_only = bool(node.GetProperty('READONLY'))
780
781 if len(self.type_children) == 2:
782 self.key_type = type_node_to_type(self.type_children[0])
783 self.value_type = type_node_to_type(self.type_children[1])
784 else:
785 raise ValueError('Unexpected number of children: %d' % len(self.type _children))
786 del self.type_children
787
788 def accept(self, visitor):
789 visitor.visit_maplike(self)
790
791
792 class IdlSetlike(IdlIterableOrMaplikeOrSetlike):
793 idl_type_attributes = ('value_type',)
794
795 def __init__(self, idl_name, node):
796 super(IdlSetlike, self).__init__(idl_name, node)
797
798 self.is_read_only = bool(node.GetProperty('READONLY'))
799
800 if len(self.type_children) == 1:
801 self.value_type = type_node_to_type(self.type_children[0])
802 else:
803 raise ValueError('Unexpected number of children: %d' % len(self.type _children))
804 del self.type_children
805
806 def accept(self, visitor):
807 visitor.visit_setlike(self)
808
809
810 ################################################################################
811 # Implement statements
812 ################################################################################
813
814 class IdlImplement(object):
815 def __init__(self, node):
816 self.left_interface = node.GetName()
817 self.right_interface = node.GetProperty('REFERENCE')
818
819
820 ################################################################################
633 # Extended attributes 821 # Extended attributes
634 ################################################################################ 822 ################################################################################
635 823
824 class Exposure:
825 """An Exposure holds one Exposed or RuntimeEnabled condition.
826 Each exposure has two properties: exposed and runtime_enabled.
827 Exposure(e, r) corresponds to [Exposed(e r)]. Exposure(e) corresponds to
828 [Exposed=e].
829 """
830 def __init__(self, exposed, runtime_enabled=None):
831 self.exposed = exposed
832 self.runtime_enabled = runtime_enabled
833
834
636 def ext_attributes_node_to_extended_attributes(idl_name, node): 835 def ext_attributes_node_to_extended_attributes(idl_name, node):
637 """ 836 """
638 Returns: 837 Returns:
639 Dictionary of {ExtAttributeName: ExtAttributeValue}. 838 Dictionary of {ExtAttributeName: ExtAttributeValue}.
640 Value is usually a string, with these exceptions: 839 Value is usually a string, with these exceptions:
641 Constructors: value is a list of Arguments nodes, corresponding to 840 Constructors: value is a list of Arguments nodes, corresponding to
642 possible signatures of the constructor. 841 possible signatures of the constructor.
643 CustomConstructors: value is a list of Arguments nodes, corresponding to 842 CustomConstructors: value is a list of Arguments nodes, corresponding to
644 possible signatures of the custom constructor. 843 possible signatures of the custom constructor.
645 NamedConstructor: value is a Call node, corresponding to the single 844 NamedConstructor: value is a Call node, corresponding to the single
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 if child_class and child_class != 'Arguments': 878 if child_class and child_class != 'Arguments':
680 raise ValueError('[CustomConstructor] only supports Arguments as child, but has child of class: %s' % child_class) 879 raise ValueError('[CustomConstructor] only supports Arguments as child, but has child of class: %s' % child_class)
681 custom_constructors.append(child) 880 custom_constructors.append(child)
682 elif name == 'NamedConstructor': 881 elif name == 'NamedConstructor':
683 if child_class and child_class != 'Call': 882 if child_class and child_class != 'Call':
684 raise ValueError('[NamedConstructor] only supports Call as child , but has child of class: %s' % child_class) 883 raise ValueError('[NamedConstructor] only supports Call as child , but has child of class: %s' % child_class)
685 extended_attributes[name] = child 884 extended_attributes[name] = child
686 elif name == 'SetWrapperReferenceTo': 885 elif name == 'SetWrapperReferenceTo':
687 if not child: 886 if not child:
688 raise ValueError('[SetWrapperReferenceTo] requires a child, but has none.') 887 raise ValueError('[SetWrapperReferenceTo] requires a child, but has none.')
888 children = child.GetChildren()
889 if len(children) != 1:
890 raise ValueError('[SetWrapperReferenceTo] supports only one chil d.')
689 if child_class != 'Arguments': 891 if child_class != 'Arguments':
690 raise ValueError('[SetWrapperReferenceTo] only supports Argument s as child, but has child of class: %s' % child_class) 892 raise ValueError('[SetWrapperReferenceTo] only supports Argument s as child, but has child of class: %s' % child_class)
691 extended_attributes[name] = arguments_node_to_arguments(idl_name, ch ild) 893 extended_attributes[name] = IdlArgument(idl_name, children[0])
894 elif name == 'Exposed':
895 if child_class and child_class != 'Arguments':
896 raise ValueError('[Exposed] only supports Arguments as child, bu t has child of class: %s' % child_class)
897 exposures = []
898 if child_class == 'Arguments':
899 exposures = [Exposure(exposed=str(arg.idl_type),
900 runtime_enabled=arg.name)
901 for arg in arguments_node_to_arguments('*', child)]
902 else:
903 value = extended_attribute_node.GetProperty('VALUE')
904 if type(value) is str:
905 exposures = [Exposure(exposed=value)]
906 else:
907 exposures = [Exposure(exposed=v) for v in value]
908 extended_attributes[name] = exposures
692 elif child: 909 elif child:
693 raise ValueError('ExtAttributes node with unexpected children: %s' % name) 910 raise ValueError('ExtAttributes node with unexpected children: %s' % name)
694 else: 911 else:
695 value = extended_attribute_node.GetProperty('VALUE') 912 value = extended_attribute_node.GetProperty('VALUE')
696 extended_attributes[name] = value 913 extended_attributes[name] = value
697 914
698 # Store constructors and custom constructors in special list attributes, 915 # Store constructors and custom constructors in special list attributes,
699 # which are deleted later. Note plural in key. 916 # which are deleted later. Note plural in key.
700 if constructors: 917 if constructors:
701 extended_attributes['Constructors'] = constructors 918 extended_attributes['Constructors'] = constructors
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 return base_type 991 return base_type
775 992
776 993
777 def type_node_inner_to_type(node): 994 def type_node_inner_to_type(node):
778 node_class = node.GetClass() 995 node_class = node.GetClass()
779 # Note Type*r*ef, not Typedef, meaning the type is an identifier, thus 996 # Note Type*r*ef, not Typedef, meaning the type is an identifier, thus
780 # either a typedef shorthand (but not a Typedef declaration itself) or an 997 # either a typedef shorthand (but not a Typedef declaration itself) or an
781 # interface type. We do not distinguish these, and just use the type name. 998 # interface type. We do not distinguish these, and just use the type name.
782 if node_class in ['PrimitiveType', 'Typeref']: 999 if node_class in ['PrimitiveType', 'Typeref']:
783 # unrestricted syntax: unrestricted double | unrestricted float 1000 # unrestricted syntax: unrestricted double | unrestricted float
784 is_unrestricted = node.GetProperty('UNRESTRICTED') or False 1001 is_unrestricted = bool(node.GetProperty('UNRESTRICTED'))
785 return IdlType(node.GetName(), is_unrestricted=is_unrestricted) 1002 return IdlType(node.GetName(), is_unrestricted=is_unrestricted)
786 elif node_class == 'Any': 1003 elif node_class == 'Any':
787 return IdlType('any') 1004 return IdlType('any')
788 elif node_class == 'Sequence': 1005 elif node_class == 'Sequence':
789 return sequence_node_to_type(node) 1006 return sequence_node_to_type(node)
790 elif node_class == 'UnionType': 1007 elif node_class == 'UnionType':
791 return union_type_node_to_idl_union_type(node) 1008 return union_type_node_to_idl_union_type(node)
1009 elif node_class == 'Promise':
1010 return IdlType('Promise')
792 raise ValueError('Unrecognized node class: %s' % node_class) 1011 raise ValueError('Unrecognized node class: %s' % node_class)
793 1012
794 1013
795 def sequence_node_to_type(node): 1014 def sequence_node_to_type(node):
796 children = node.GetChildren() 1015 children = node.GetChildren()
797 if len(children) != 1: 1016 if len(children) != 1:
798 raise ValueError('Sequence node expects exactly 1 child, got %s' % len(c hildren)) 1017 raise ValueError('Sequence node expects exactly 1 child, got %s' % len(c hildren))
799 sequence_child = children[0] 1018 sequence_child = children[0]
800 sequence_child_class = sequence_child.GetClass() 1019 sequence_child_class = sequence_child.GetClass()
801 if sequence_child_class != 'Type': 1020 if sequence_child_class != 'Type':
(...skipping 13 matching lines...) Expand all
815 child_class = child.GetClass() 1034 child_class = child.GetClass()
816 if child_class != 'Type': 1035 if child_class != 'Type':
817 raise ValueError('Unrecognized node class: %s' % child_class) 1036 raise ValueError('Unrecognized node class: %s' % child_class)
818 return type_node_to_type(child) 1037 return type_node_to_type(child)
819 1038
820 1039
821 def union_type_node_to_idl_union_type(node): 1040 def union_type_node_to_idl_union_type(node):
822 member_types = [type_node_to_type(member_type_node) 1041 member_types = [type_node_to_type(member_type_node)
823 for member_type_node in node.GetChildren()] 1042 for member_type_node in node.GetChildren()]
824 return IdlUnionType(member_types) 1043 return IdlUnionType(member_types)
1044
1045
1046 ################################################################################
1047 # Visitor
1048 ################################################################################
1049
1050 class Visitor(object):
1051 """Abstract visitor class for IDL definitions traverse."""
1052
1053 def visit_definitions(self, definitions):
1054 pass
1055
1056 def visit_typed_object(self, typed_object):
1057 pass
1058
1059 def visit_callback_function(self, callback_function):
1060 self.visit_typed_object(callback_function)
1061
1062 def visit_dictionary(self, dictionary):
1063 pass
1064
1065 def visit_dictionary_member(self, member):
1066 self.visit_typed_object(member)
1067
1068 def visit_interface(self, interface):
1069 pass
1070
1071 def visit_typedef(self, typedef):
1072 self.visit_typed_object(typedef)
1073
1074 def visit_attribute(self, attribute):
1075 self.visit_typed_object(attribute)
1076
1077 def visit_constant(self, constant):
1078 self.visit_typed_object(constant)
1079
1080 def visit_operation(self, operation):
1081 self.visit_typed_object(operation)
1082
1083 def visit_argument(self, argument):
1084 self.visit_typed_object(argument)
1085
1086 def visit_iterable(self, iterable):
1087 self.visit_typed_object(iterable)
1088
1089 def visit_maplike(self, maplike):
1090 self.visit_typed_object(maplike)
1091
1092 def visit_setlike(self, setlike):
1093 self.visit_typed_object(setlike)
OLDNEW
« no previous file with comments | « bindings/scripts/idl_compiler.py ('k') | bindings/scripts/idl_reader.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698