Commit 30b65a70 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

IDL compiler: Minor refactoring of common.py

- rename common.py to composition_parts.py
- make Identifier and Component own classes
- remove non-informative comments

Bug: 839389
Change-Id: Ia7b5d17810bb6ad39873effa3a7e2fc8ffeda348
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1724270Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682138}
parent e6bd26ab
......@@ -55,7 +55,7 @@ group("web_idl_pylib") {
"web_idl/attribute.py",
"web_idl/callback_function.py",
"web_idl/callback_interface.py",
"web_idl/common.py",
"web_idl/composition_parts.py",
"web_idl/constant.py",
"web_idl/constructor.py",
"web_idl/database.py",
......
......@@ -47,7 +47,7 @@ def main():
filepaths = utilities.read_idl_files_list_from_file(options.idl_list_file)
parser = blink_idl_parser.BlinkIDLParser()
ast_group = web_idl.AstGroup(options.component)
ast_group = web_idl.AstGroup(web_idl.Component(options.component))
for filepath in filepaths:
ast_group.add_ast_node(blink_idl_parser.parse_file(parser, filepath))
ast_group.write_to_file(options.output)
......
......@@ -3,11 +3,13 @@
# found in the LICENSE file.
from .ast_group import AstGroup
from .composition_parts import Component
from .database import Database
from .database_builder import build_database
__all__ = [
"AstGroup",
"Component",
"Database",
"build_database",
]
......@@ -2,10 +2,10 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithIdentifier
from .common import WithExtendedAttributes
from .common import WithCodeGeneratorInfo
from .common import WithOwner
from .composition_parts import WithIdentifier
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithOwner
from .idl_type import IdlType
from .values import DefaultValue
......
......@@ -4,14 +4,14 @@
import pickle
from .common import Component
from .composition_parts import Component
class AstGroup(object):
"""A set of Web IDL ASTs grouped by component."""
def __init__(self, component=None):
assert component is None or isinstance(component, Component)
def __init__(self, component):
assert isinstance(component, Component)
self._nodes = []
self._component = component
......
......@@ -3,12 +3,13 @@
# found in the LICENSE file.
import exceptions
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExposure
from .common import WithExtendedAttributes
from .common import WithIdentifier
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExposure
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier
from .idl_member import IdlMember
from .idl_type import IdlType
......
......@@ -3,10 +3,11 @@
# found in the LICENSE file.
import exceptions
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExtendedAttributes
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExtendedAttributes
from .identifier_ir_map import IdentifierIRMap
from .user_defined_type import UserDefinedType
......
......@@ -3,10 +3,11 @@
# found in the LICENSE file.
import exceptions
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExtendedAttributes
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExtendedAttributes
from .identifier_ir_map import IdentifierIRMap
from .user_defined_type import UserDefinedType
......
......@@ -8,12 +8,12 @@ from .extended_attribute import ExtendedAttributes
from .exposure import Exposure
Identifier = str
class Identifier(str):
pass
class WithIdentifier(object):
"""WithIdentifier class is an interface that indicates the class has an
identifier."""
"""Implements |identifier| as a readonly attribute."""
def __init__(self, identifier):
assert isinstance(identifier, Identifier)
......@@ -21,17 +21,11 @@ class WithIdentifier(object):
@property
def identifier(self):
"""
Returns the identifier.
@return Identifier
"""
return self._identifier
# ExtendedAttribute and ExtendedAttributes are defined in extended_attribute.py
class WithExtendedAttributes(object):
"""WithExtendedAttributes class is an interface that indicates the implemented
class can have extended attributes."""
"""Implements |extended_attributes| as a readonly attribute."""
def __init__(self, extended_attributes=None):
assert (extended_attributes is None
......@@ -40,21 +34,18 @@ class WithExtendedAttributes(object):
@property
def extended_attributes(self):
"""
Returns the extended attributes.
@return ExtendedAttributes
"""
return self._extended_attributes
class CodeGeneratorInfo(dict):
"""A bag of properties to be used by bindings code generators"""
def make_copy(self):
return copy.deepcopy(self)
class WithCodeGeneratorInfo(object):
"""WithCodeGeneratorInfo class is an interface that its inheritances can
provide some information for code generators."""
"""Implements |code_generator_info| as a readonly attribute."""
def __init__(self, code_generator_info=None):
assert (code_generator_info is None
......@@ -63,16 +54,11 @@ class WithCodeGeneratorInfo(object):
@property
def code_generator_info(self):
"""
Returns information for code generator.
@return CodeGeneratorInfo
"""
return self._code_generator_info
class WithExposure(object):
"""WithExposure class is an interface that its inheritances can have Exposed
extended attributes."""
"""Implements |exposures| as a readonly attribute."""
def __init__(self, exposures=None):
assert (exposures is None
......@@ -82,21 +68,21 @@ class WithExposure(object):
@property
def exposures(self):
"""
Returns a set of Exposure's that are applicable on an object.
https://heycam.github.io/webidl/#Exposed
@return tuple(Expsure)
"""
return self._exposures
Component = str
class Component(str):
"""
Represents a component that is a Blink-specific layering concept, such as
'core' and 'modules'.
"""
pass
class WithComponent(object):
"""
Implements |components| which is a Blink-specific layering concept of
components, such as 'core' and 'modules'.
Implements |components| as a readonly attribute.
A single IDL definition such as 'interface' may consist from multiple IDL
fragments like partial interfaces and mixins, which may exist across
......@@ -125,7 +111,6 @@ class WithComponent(object):
def components(self):
"""
Returns a list of components' names where this definition is defined
@return tuple(Component)
"""
return tuple(self._components)
......@@ -214,8 +199,7 @@ class DebugInfo(object):
class WithDebugInfo(object):
"""WithDebugInfo class is an interface that its inheritances can have
DebugInfo."""
"""Implements |debug_info| as a readonly attribute."""
def __init__(self, debug_info=None):
assert debug_info is None or isinstance(debug_info, DebugInfo)
......@@ -223,24 +207,16 @@ class WithDebugInfo(object):
@property
def debug_info(self):
"""Returns DebugInfo."""
return self._debug_info
class WithOwner(object):
"""WithOwner class provides information about who owns this object.
If this object is a member, it points either of an interface,
a namespace, a dictionary, and so on. If this object is an argument,
it points a function like object."""
"""Implements |owner| as a readonly attribute."""
def __init__(self, owner):
assert isinstance(owner, object) # None is okay
assert isinstance(owner, object) and owner is not None
self._owner = owner
@property
def owner(self):
"""
Returns the owner of this instance.
@return object
"""
return self._owner
......@@ -2,12 +2,12 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExtendedAttributes
from .common import WithExposure
from .common import WithIdentifier
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithExposure
from .composition_parts import WithIdentifier
from .idl_member import IdlMember
from .idl_type import IdlType
from .values import ConstantValue
......
......@@ -3,9 +3,10 @@
# found in the LICENSE file.
import exceptions
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithOwner
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithOwner
from .idl_member import IdlMember
......
......@@ -2,12 +2,12 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExtendedAttributes
from .common import WithIdentifier
from .common import WithOwner
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier
from .composition_parts import WithOwner
from .identifier_ir_map import IdentifierIRMap
from .idl_type import IdlType
from .reference import RefById
......
......@@ -3,10 +3,11 @@
# found in the LICENSE file.
import exceptions
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExtendedAttributes
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExtendedAttributes
from .identifier_ir_map import IdentifierIRMap
from .user_defined_type import UserDefinedType
......
......@@ -2,7 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithIdentifier
from .composition_parts import Identifier
from .composition_parts import WithIdentifier
class IdentifierIRMap(object):
......@@ -169,6 +170,7 @@ class IdentifierIRMap(object):
If |skip_current_phase| is True, skips the current phase when looking
for the identifier.
"""
assert isinstance(identifier, Identifier)
start_phase = self._current_phase - (1 if skip_current_phase else 0)
for irs_per_phase in self._single_value_irs[start_phase::-1]:
for irs_per_kind in irs_per_phase.values():
......
......@@ -2,13 +2,13 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExposure
from .common import WithExtendedAttributes
from .common import WithIdentifier
from .common import WithOwner
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExposure
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier
from .composition_parts import WithOwner
class IdlMember(WithIdentifier, WithExtendedAttributes, WithCodeGeneratorInfo,
......
......@@ -6,10 +6,10 @@ import exceptions
from blinkbuild.name_style_converter import NameStyleConverter
from .common import WithCodeGeneratorInfo
from .common import WithDebugInfo
from .common import WithExtendedAttributes
from .common import WithIdentifier
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithDebugInfo
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier
from .reference import Proxy
from .reference import RefById
from .typedef import Typedef
......
......@@ -3,9 +3,11 @@
# found in the LICENSE file.
import exceptions
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .composition_parts import Identifier
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .identifier_ir_map import IdentifierIRMap
......@@ -20,6 +22,9 @@ class Includes(WithComponent, WithDebugInfo):
code_generator_info=None,
component=None,
debug_info=None):
assert isinstance(interface_identifier, Identifier)
assert isinstance(mixin_identifier, Identifier)
# Includes statements are treated similarly to partial
# definitions, and it's convenient for IdlCompiler that
# 'includes' are grouped by interface's identifier, i.e.
......
......@@ -5,11 +5,11 @@
import exceptions
from .attribute import Attribute
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExposure
from .common import WithExtendedAttributes
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExposure
from .composition_parts import WithExtendedAttributes
from .constant import Constant
from .identifier_ir_map import IdentifierIRMap
from .idl_member import IdlMember
......
......@@ -7,8 +7,10 @@ from .ast_group import AstGroup
from .attribute import Attribute
from .callback_function import CallbackFunction
from .callback_interface import CallbackInterface
from .common import DebugInfo
from .common import Location
from .composition_parts import Component
from .composition_parts import DebugInfo
from .composition_parts import Identifier
from .composition_parts import Location
from .constant import Constant
from .dictionary import Dictionary
from .dictionary import DictionaryMember
......@@ -48,7 +50,7 @@ def load_and_register_idl_definitions(
for filepath in filepaths:
asts_per_component = AstGroup.read_from_file(filepath)
component = asts_per_component.component
component = Component(asts_per_component.component)
builder = _IRBuilder(
component=component,
create_ref_to_idl_def=create_ref_to_idl_def,
......@@ -128,7 +130,7 @@ class _IRBuilder(object):
# |property_handlers|.
return Interface.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
is_partial=bool(node.GetProperty('PARTIAL')),
is_mixin=bool(node.GetProperty('MIXIN')),
inherited=inherited,
......@@ -144,7 +146,7 @@ class _IRBuilder(object):
def _build_namespace(self, node):
namespace = Namespace.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
is_partial=bool(node.GetProperty('PARTIAL')),
component=self._component,
debug_info=self._build_debug_info(node))
......@@ -158,7 +160,7 @@ class _IRBuilder(object):
extended_attributes = self._take_extended_attributes(child_nodes)
assert len(child_nodes) == 0
return Attribute.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
idl_type=idl_type,
is_static=bool(node.GetProperty('STATIC')),
is_readonly=bool(node.GetProperty('READONLY')),
......@@ -176,7 +178,7 @@ class _IRBuilder(object):
# constant, hence we need to skip one level.
idl_type = self._build_type_internal(child_nodes)
return Constant.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
value=value,
idl_type=idl_type,
extended_attributes=extended_attributes,
......@@ -190,7 +192,7 @@ class _IRBuilder(object):
extended_attributes = self._take_extended_attributes(child_nodes)
assert len(child_nodes) == 0
return Operation.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
arguments=arguments,
return_type=return_type,
is_static=bool(node.GetProperty('STATIC')),
......@@ -212,7 +214,7 @@ class _IRBuilder(object):
own_members = map(self._build_dictionary_member, child_nodes)
return Dictionary.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
is_partial=bool(node.GetProperty('PARTIAL')),
inherited=inherited,
own_members=own_members,
......@@ -231,7 +233,7 @@ class _IRBuilder(object):
assert len(child_nodes) == 0
return DictionaryMember.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
idl_type=idl_type,
default_value=default_value,
extended_attributes=extended_attributes,
......@@ -240,7 +242,7 @@ class _IRBuilder(object):
def _build_callback_interface(self, node):
callback_interface = CallbackInterface.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
component=self._component,
debug_info=self._build_debug_info(node))
# TODO(peria): Build members and register them in |callback_interface|
......@@ -248,7 +250,7 @@ class _IRBuilder(object):
def _build_callback_function(self, node):
callback_function = CallbackFunction.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
component=self._component,
debug_info=self._build_debug_info(node))
# TODO(peria): Build members and register them in |callback_function|
......@@ -256,7 +258,7 @@ class _IRBuilder(object):
def _build_enumeration(self, node):
enumeration = Enumeration.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
values=[child.GetName() for child in node.GetChildren()],
component=self._component,
debug_info=self._build_debug_info(node))
......@@ -268,7 +270,7 @@ class _IRBuilder(object):
assert len(child_nodes) == 0
typedef = Typedef.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
idl_type=idl_type,
component=self._component,
debug_info=self._build_debug_info(node))
......@@ -276,8 +278,8 @@ class _IRBuilder(object):
def _build_includes(self, node):
includes = Includes.IR(
interface_identifier=node.GetName(),
mixin_identifier=node.GetProperty('REFERENCE'),
interface_identifier=Identifier(node.GetName()),
mixin_identifier=Identifier(node.GetProperty('REFERENCE')),
component=self._component,
debug_info=self._build_debug_info(node))
return includes
......@@ -296,7 +298,7 @@ class _IRBuilder(object):
extended_attributes = self._take_extended_attributes(child_nodes)
assert len(child_nodes) == 0
return Argument.IR(
identifier=node.GetName(),
identifier=Identifier(node.GetName()),
index=index,
idl_type=idl_type,
default_value=default_value,
......@@ -329,8 +331,8 @@ class _IRBuilder(object):
def _build_inheritance(self, node):
assert node.GetClass() == 'Inherit'
return self._create_ref_to_idl_def(node.GetName(),
self._build_debug_info(node))
return self._create_ref_to_idl_def(
Identifier(node.GetName()), self._build_debug_info(node))
def _build_is_variadic_argument(self, node):
# idl_parser produces the following tree to indicate an argument is
......@@ -434,10 +436,9 @@ class _IRBuilder(object):
debug_info=self._build_debug_info(node))
def build_reference_type(node, extended_attributes):
identifier = node.GetName()
return self._idl_type_factory.reference_type(
ref_to_idl_type=self._create_ref_to_idl_type(
identifier, self._build_debug_info(node)),
Identifier(node.GetName()), self._build_debug_info(node)),
is_optional=is_optional,
extended_attributes=extended_attributes,
debug_info=self._build_debug_info(node))
......
......@@ -3,12 +3,13 @@
# found in the LICENSE file.
import exceptions
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExposure
from .common import WithExtendedAttributes
from .common import WithIdentifier
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExposure
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier
from .identifier_ir_map import IdentifierIRMap
......
......@@ -3,14 +3,15 @@
# found in the LICENSE file.
import exceptions
from .argument import Argument
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithExposure
from .common import WithExtendedAttributes
from .common import WithIdentifier
from .common import WithOwner
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithExposure
from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier
from .composition_parts import WithOwner
from .idl_member import IdlMember
from .idl_type import IdlType
......
......@@ -2,8 +2,8 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import DebugInfo
from .common import WithIdentifier
from .composition_parts import DebugInfo
from .composition_parts import WithIdentifier
class Proxy(object):
......
......@@ -2,10 +2,10 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithCodeGeneratorInfo
from .common import WithComponent
from .common import WithDebugInfo
from .common import WithIdentifier
from .composition_parts import WithCodeGeneratorInfo
from .composition_parts import WithComponent
from .composition_parts import WithDebugInfo
from .composition_parts import WithIdentifier
from .identifier_ir_map import IdentifierIRMap
......
......@@ -2,7 +2,7 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .common import WithIdentifier
from .composition_parts import WithIdentifier
class UserDefinedType(WithIdentifier):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment