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 30 matching lines...) Expand all Loading... | |
| 41 a generic call, via the resolve_typedefs() method. | 41 a generic call, via the resolve_typedefs() method. |
| 42 | 42 |
| 43 Class hierarchy (mostly containment, '<' for inheritance): | 43 Class hierarchy (mostly containment, '<' for inheritance): |
| 44 | 44 |
| 45 IdlDefinitions | 45 IdlDefinitions |
| 46 IdlCallbackFunction < TypedObject | 46 IdlCallbackFunction < TypedObject |
| 47 IdlEnum :: FIXME: remove, just use a dict for enums | 47 IdlEnum :: FIXME: remove, just use a dict for enums |
| 48 IdlInterface | 48 IdlInterface |
| 49 IdlAttribute < TypedObject | 49 IdlAttribute < TypedObject |
| 50 IdlConstant < TypedObject | 50 IdlConstant < TypedObject |
| 51 IdlLiteral | |
| 51 IdlOperation < TypedObject | 52 IdlOperation < TypedObject |
| 52 IdlArgument < TypedObject | 53 IdlArgument < TypedObject |
| 54 IdlStringifier | |
| 53 IdlException < IdlInterface | 55 IdlException < IdlInterface |
| 54 (same contents as IdlInterface) | 56 (same contents as IdlInterface) |
| 55 | 57 |
| 56 TypedObject :: mixin for typedef resolution | 58 TypedObject :: mixin for typedef resolution |
| 57 | 59 |
| 58 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | 60 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler |
| 59 """ | 61 """ |
| 60 | 62 |
| 61 import abc | 63 import abc |
| 62 | 64 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 254 | 256 |
| 255 class IdlInterface(object): | 257 class IdlInterface(object): |
| 256 def __init__(self, node=None): | 258 def __init__(self, node=None): |
| 257 self.attributes = [] | 259 self.attributes = [] |
| 258 self.constants = [] | 260 self.constants = [] |
| 259 self.constructors = [] | 261 self.constructors = [] |
| 260 self.custom_constructors = [] | 262 self.custom_constructors = [] |
| 261 self.extended_attributes = {} | 263 self.extended_attributes = {} |
| 262 self.operations = [] | 264 self.operations = [] |
| 263 self.parent = None | 265 self.parent = None |
| 266 self.stringifier = None | |
| 264 if not node: # Early exit for IdlException.__init__ | 267 if not node: # Early exit for IdlException.__init__ |
| 265 return | 268 return |
| 266 | 269 |
| 267 self.is_callback = node.GetProperty('CALLBACK') or False | 270 self.is_callback = node.GetProperty('CALLBACK') or False |
| 268 self.is_exception = False | 271 self.is_exception = False |
| 269 # FIXME: uppercase 'Partial' => 'PARTIAL' in base IDL parser | 272 # FIXME: uppercase 'Partial' => 'PARTIAL' in base IDL parser |
| 270 self.is_partial = node.GetProperty('Partial') or False | 273 self.is_partial = node.GetProperty('Partial') or False |
| 271 self.name = node.GetName() | 274 self.name = node.GetName() |
| 272 | 275 |
| 273 children = node.GetChildren() | 276 children = node.GetChildren() |
| 274 for child in children: | 277 for child in children: |
| 275 child_class = child.GetClass() | 278 child_class = child.GetClass() |
| 276 if child_class == 'Attribute': | 279 if child_class == 'Attribute': |
| 277 self.attributes.append(IdlAttribute(child)) | 280 self.attributes.append(IdlAttribute(child)) |
| 278 elif child_class == 'Const': | 281 elif child_class == 'Const': |
| 279 self.constants.append(IdlConstant(child)) | 282 self.constants.append(IdlConstant(child)) |
| 280 elif child_class == 'ExtAttributes': | 283 elif child_class == 'ExtAttributes': |
| 281 extended_attributes = ext_attributes_node_to_extended_attributes (child) | 284 extended_attributes = ext_attributes_node_to_extended_attributes (child) |
| 282 self.constructors, self.custom_constructors = ( | 285 self.constructors, self.custom_constructors = ( |
| 283 extended_attributes_to_constructors(extended_attributes)) | 286 extended_attributes_to_constructors(extended_attributes)) |
| 284 clear_constructor_attributes(extended_attributes) | 287 clear_constructor_attributes(extended_attributes) |
| 285 self.extended_attributes = extended_attributes | 288 self.extended_attributes = extended_attributes |
| 286 elif child_class == 'Operation': | 289 elif child_class == 'Operation': |
| 287 self.operations.append(IdlOperation(child)) | 290 self.operations.append(IdlOperation(child)) |
| 288 elif child_class == 'Inherit': | 291 elif child_class == 'Inherit': |
| 289 self.parent = child.GetName() | 292 self.parent = child.GetName() |
| 293 elif child_class == 'Stringifier': | |
| 294 self.stringifier = IdlStringifier(child) | |
| 295 self.process_stringifier() | |
| 290 else: | 296 else: |
| 291 raise ValueError('Unrecognized node class: %s' % child_class) | 297 raise ValueError('Unrecognized node class: %s' % child_class) |
| 292 | 298 |
| 293 def resolve_typedefs(self, typedefs): | 299 def resolve_typedefs(self, typedefs): |
| 294 for attribute in self.attributes: | 300 for attribute in self.attributes: |
| 295 attribute.resolve_typedefs(typedefs) | 301 attribute.resolve_typedefs(typedefs) |
| 296 for constant in self.constants: | 302 for constant in self.constants: |
| 297 constant.resolve_typedefs(typedefs) | 303 constant.resolve_typedefs(typedefs) |
| 298 for constructor in self.constructors: | 304 for constructor in self.constructors: |
| 299 constructor.resolve_typedefs(typedefs) | 305 constructor.resolve_typedefs(typedefs) |
| 300 for custom_constructor in self.custom_constructors: | 306 for custom_constructor in self.custom_constructors: |
| 301 custom_constructor.resolve_typedefs(typedefs) | 307 custom_constructor.resolve_typedefs(typedefs) |
| 302 for operation in self.operations: | 308 for operation in self.operations: |
| 303 operation.resolve_typedefs(typedefs) | 309 operation.resolve_typedefs(typedefs) |
| 304 | 310 |
| 311 def process_stringifier(self): | |
| 312 """Add the stringifier's synthesized 'toString' operation. | |
| 313 | |
| 314 Also add the stringifier's attribute or named operation child, if it has | |
| 315 one, as a regular attribute/operation of this interface.""" | |
| 316 if self.stringifier.attribute: | |
| 317 self.attributes.append(self.stringifier.attribute) | |
| 318 elif self.stringifier.operation and self.stringifier.operation.name: | |
| 319 self.operations.append(self.stringifier.operation) | |
| 320 self.operations.append(self.stringifier.synthesized_operation()) | |
| 321 | |
| 305 def merge(self, other): | 322 def merge(self, other): |
| 306 """Merge in another interface's members (e.g., partial interface)""" | 323 """Merge in another interface's members (e.g., partial interface)""" |
| 307 self.attributes.extend(other.attributes) | 324 self.attributes.extend(other.attributes) |
| 308 self.constants.extend(other.constants) | 325 self.constants.extend(other.constants) |
| 309 self.operations.extend(other.operations) | 326 self.operations.extend(other.operations) |
| 310 | 327 |
| 311 | 328 |
| 312 class IdlException(IdlInterface): | 329 class IdlException(IdlInterface): |
| 313 # Properly exceptions and interfaces are distinct, and thus should inherit a | 330 # Properly exceptions and interfaces are distinct, and thus should inherit a |
| 314 # common base class (say, "IdlExceptionOrInterface"). | 331 # common base class (say, "IdlExceptionOrInterface"). |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 555 # have None instead of an arguments node, but have the same meaning as using | 572 # have None instead of an arguments node, but have the same meaning as using |
| 556 # an empty argument list, [Constructor()], so special-case this. | 573 # an empty argument list, [Constructor()], so special-case this. |
| 557 # http://www.w3.org/TR/WebIDL/#Constructor | 574 # http://www.w3.org/TR/WebIDL/#Constructor |
| 558 if node is None: | 575 if node is None: |
| 559 return [] | 576 return [] |
| 560 return [IdlArgument(argument_node) | 577 return [IdlArgument(argument_node) |
| 561 for argument_node in node.GetChildren()] | 578 for argument_node in node.GetChildren()] |
| 562 | 579 |
| 563 | 580 |
| 564 ################################################################################ | 581 ################################################################################ |
| 582 # Stringifiers | |
| 583 ################################################################################ | |
| 584 | |
| 585 class IdlStringifier(object): | |
| 586 def __init__(self, node): | |
| 587 self.attribute = None | |
| 588 self.operation = None | |
| 589 self.extended_attributes = {} | |
| 590 | |
| 591 for child in node.GetChildren(): | |
| 592 child_class = child.GetClass() | |
| 593 if child_class == 'Attribute': | |
| 594 self.attribute = IdlAttribute(child) | |
| 595 elif child_class == 'Operation': | |
| 596 self.operation = IdlOperation(child) | |
| 597 elif child_class == 'ExtAttributes': | |
| 598 self.extended_attributes = ext_attributes_node_to_extended_attri butes(child) | |
| 599 else: | |
| 600 raise ValueError('Unrecognized node class: %s' % child_class) | |
| 601 | |
| 602 # Copy the stringifier's extended attributes (such as [Unforgable]) onto | |
| 603 # the underlying attribute or operation, if there is one. | |
| 604 if self.attribute or self.operation: | |
| 605 (self.attribute or self.operation).extended_attributes.update( | |
| 606 self.extended_attributes) | |
| 607 | |
| 608 def synthesized_operation(self): | |
| 609 """Return a synthesized 'toString' operation from the stringifier.""" | |
|
Jens Widell
2014/06/23 14:08:07
I don't know if we think this is too much logic to
haraken
2014/06/23 16:33:18
Yeah, this looks like a bit too much logic in idl_
Jens Widell
2014/06/24 08:00:29
Moved this code into interface_context() in v8_int
| |
| 610 operation = IdlOperation() | |
| 611 operation.name = 'toString' | |
| 612 operation.idl_type = IdlType('DOMString') | |
| 613 operation.extended_attributes.update(self.extended_attributes) | |
| 614 if self.attribute: | |
| 615 operation.extended_attributes['ImplementedAs'] = self.attribute.name | |
| 616 elif self.operation and self.operation.name: | |
| 617 operation.extended_attributes['ImplementedAs'] = self.operation.name | |
| 618 return operation | |
| 619 | |
| 620 | |
| 621 ################################################################################ | |
| 565 # Extended attributes | 622 # Extended attributes |
| 566 ################################################################################ | 623 ################################################################################ |
| 567 | 624 |
| 568 def ext_attributes_node_to_extended_attributes(node): | 625 def ext_attributes_node_to_extended_attributes(node): |
| 569 """ | 626 """ |
| 570 Returns: | 627 Returns: |
| 571 Dictionary of {ExtAttributeName: ExtAttributeValue}. | 628 Dictionary of {ExtAttributeName: ExtAttributeValue}. |
| 572 Value is usually a string, with three exceptions: | 629 Value is usually a string, with three exceptions: |
| 573 Constructors: value is a list of Arguments nodes, corresponding to | 630 Constructors: value is a list of Arguments nodes, corresponding to |
| 574 possible signatures of the constructor. | 631 possible signatures of the constructor. |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 747 child_class = child.GetClass() | 804 child_class = child.GetClass() |
| 748 if child_class != 'Type': | 805 if child_class != 'Type': |
| 749 raise ValueError('Unrecognized node class: %s' % child_class) | 806 raise ValueError('Unrecognized node class: %s' % child_class) |
| 750 return type_node_to_type(child) | 807 return type_node_to_type(child) |
| 751 | 808 |
| 752 | 809 |
| 753 def union_type_node_to_idl_union_type(node, is_nullable=False): | 810 def union_type_node_to_idl_union_type(node, is_nullable=False): |
| 754 member_types = [type_node_to_type(member_type_node) | 811 member_types = [type_node_to_type(member_type_node) |
| 755 for member_type_node in node.GetChildren()] | 812 for member_type_node in node.GetChildren()] |
| 756 return IdlUnionType(member_types, is_nullable=is_nullable) | 813 return IdlUnionType(member_types, is_nullable=is_nullable) |
| OLD | NEW |