Commit c4f60987 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

IDL compiler: Implement LiteralConstant for constants and default values

Implements constant values and default values.

Bug: 839389
Change-Id: Id8de1de689eebf9b45bc879175e290711855bb28
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1763662Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689396}
parent 754059d2
...@@ -26,6 +26,7 @@ web_idl/idl_type.py ...@@ -26,6 +26,7 @@ web_idl/idl_type.py
web_idl/includes.py web_idl/includes.py
web_idl/interface.py web_idl/interface.py
web_idl/ir_builder.py web_idl/ir_builder.py
web_idl/literal_constant.py
web_idl/make_copy.py web_idl/make_copy.py
web_idl/namespace.py web_idl/namespace.py
web_idl/operation.py web_idl/operation.py
...@@ -33,4 +34,3 @@ web_idl/reference.py ...@@ -33,4 +34,3 @@ web_idl/reference.py
web_idl/typedef.py web_idl/typedef.py
web_idl/union.py web_idl/union.py
web_idl/user_defined_type.py web_idl/user_defined_type.py
web_idl/values.py
...@@ -36,6 +36,7 @@ web_idl/idl_type.py ...@@ -36,6 +36,7 @@ web_idl/idl_type.py
web_idl/includes.py web_idl/includes.py
web_idl/interface.py web_idl/interface.py
web_idl/ir_builder.py web_idl/ir_builder.py
web_idl/literal_constant.py
web_idl/make_copy.py web_idl/make_copy.py
web_idl/namespace.py web_idl/namespace.py
web_idl/operation.py web_idl/operation.py
...@@ -43,4 +44,3 @@ web_idl/reference.py ...@@ -43,4 +44,3 @@ web_idl/reference.py
web_idl/typedef.py web_idl/typedef.py
web_idl/union.py web_idl/union.py
web_idl/user_defined_type.py web_idl/user_defined_type.py
web_idl/values.py
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
from .composition_parts import WithIdentifier from .composition_parts import WithIdentifier
from .composition_parts import WithOwner from .composition_parts import WithOwner
from .idl_type import IdlType from .idl_type import IdlType
from .literal_constant import LiteralConstant
from .make_copy import make_copy from .make_copy import make_copy
from .values import DefaultValue
class Argument(WithIdentifier, WithOwner): class Argument(WithIdentifier, WithOwner):
...@@ -15,7 +15,7 @@ class Argument(WithIdentifier, WithOwner): ...@@ -15,7 +15,7 @@ class Argument(WithIdentifier, WithOwner):
assert isinstance(index, int) assert isinstance(index, int)
assert isinstance(idl_type, IdlType) assert isinstance(idl_type, IdlType)
assert (default_value is None assert (default_value is None
or isinstance(default_value, DefaultValue)) or isinstance(default_value, LiteralConstant))
WithIdentifier.__init__(self, identifier) WithIdentifier.__init__(self, identifier)
......
...@@ -9,8 +9,8 @@ from .composition_parts import WithExtendedAttributes ...@@ -9,8 +9,8 @@ from .composition_parts import WithExtendedAttributes
from .composition_parts import WithIdentifier from .composition_parts import WithIdentifier
from .composition_parts import WithOwner from .composition_parts import WithOwner
from .idl_type import IdlType from .idl_type import IdlType
from .literal_constant import LiteralConstant
from .make_copy import make_copy from .make_copy import make_copy
from .values import ConstantValue
class Constant(WithIdentifier, WithExtendedAttributes, WithCodeGeneratorInfo, class Constant(WithIdentifier, WithExtendedAttributes, WithCodeGeneratorInfo,
...@@ -29,7 +29,7 @@ class Constant(WithIdentifier, WithExtendedAttributes, WithCodeGeneratorInfo, ...@@ -29,7 +29,7 @@ class Constant(WithIdentifier, WithExtendedAttributes, WithCodeGeneratorInfo,
components=None, components=None,
debug_info=None): debug_info=None):
assert isinstance(idl_type, IdlType) assert isinstance(idl_type, IdlType)
assert isinstance(value, ConstantValue) assert isinstance(value, LiteralConstant)
WithIdentifier.__init__(self, identifier) WithIdentifier.__init__(self, identifier)
WithExtendedAttributes.__init__(self, extended_attributes) WithExtendedAttributes.__init__(self, extended_attributes)
......
...@@ -10,10 +10,10 @@ from .composition_parts import WithIdentifier ...@@ -10,10 +10,10 @@ from .composition_parts import WithIdentifier
from .composition_parts import WithOwner from .composition_parts import WithOwner
from .identifier_ir_map import IdentifierIRMap from .identifier_ir_map import IdentifierIRMap
from .idl_type import IdlType from .idl_type import IdlType
from .literal_constant import LiteralConstant
from .make_copy import make_copy from .make_copy import make_copy
from .reference import RefById from .reference import RefById
from .user_defined_type import UserDefinedType from .user_defined_type import UserDefinedType
from .values import DefaultValue
class Dictionary(UserDefinedType, WithExtendedAttributes, class Dictionary(UserDefinedType, WithExtendedAttributes,
...@@ -123,7 +123,7 @@ class DictionaryMember(WithIdentifier, WithExtendedAttributes, ...@@ -123,7 +123,7 @@ class DictionaryMember(WithIdentifier, WithExtendedAttributes,
debug_info=None): debug_info=None):
assert isinstance(idl_type, IdlType) assert isinstance(idl_type, IdlType)
assert default_value is None or isinstance(default_value, assert default_value is None or isinstance(default_value,
DefaultValue) LiteralConstant)
assert not default_value or idl_type.is_optional assert not default_value or idl_type.is_optional
WithIdentifier.__init__(self, identifier) WithIdentifier.__init__(self, identifier)
......
...@@ -23,11 +23,10 @@ from .interface import Interface ...@@ -23,11 +23,10 @@ from .interface import Interface
from .interface import Iterable from .interface import Iterable
from .interface import Maplike from .interface import Maplike
from .interface import Setlike from .interface import Setlike
from .literal_constant import LiteralConstant
from .namespace import Namespace from .namespace import Namespace
from .operation import Operation from .operation import Operation
from .typedef import Typedef from .typedef import Typedef
from .values import ConstantValue
from .values import DefaultValue
def load_and_register_idl_definitions( def load_and_register_idl_definitions(
...@@ -324,7 +323,7 @@ class _IRBuilder(object): ...@@ -324,7 +323,7 @@ class _IRBuilder(object):
def _build_constant_value(self, node): def _build_constant_value(self, node):
assert node.GetClass() == 'Value' assert node.GetClass() == 'Value'
return ConstantValue() return self._build_literal_constant(node)
def _build_debug_info(self, node): def _build_debug_info(self, node):
return DebugInfo( return DebugInfo(
...@@ -335,7 +334,7 @@ class _IRBuilder(object): ...@@ -335,7 +334,7 @@ class _IRBuilder(object):
def _build_default_value(self, node): def _build_default_value(self, node):
assert node.GetClass() == 'Default' assert node.GetClass() == 'Default'
return DefaultValue() return self._build_literal_constant(node)
def _build_extended_attributes(self, node): def _build_extended_attributes(self, node):
def build_extended_attribute(node): def build_extended_attribute(node):
...@@ -402,6 +401,65 @@ class _IRBuilder(object): ...@@ -402,6 +401,65 @@ class _IRBuilder(object):
value_type=types[1], value_type=types[1],
debug_info=self._build_debug_info(node)) debug_info=self._build_debug_info(node))
def _build_literal_constant(self, node):
assert len(node.GetChildren()) == 0
type_token = node.GetProperty('TYPE')
value_token = node.GetProperty('VALUE')
debug_info = self._build_debug_info(node)
factory = self._idl_type_factory
if type_token == 'NULL':
idl_type = factory.nullable_type(
inner_type=factory.simple_type(
name='any', debug_info=debug_info),
debug_info=debug_info)
assert value_token == 'NULL'
value = None
literal = 'null'
elif type_token == 'boolean':
idl_type = factory.simple_type(
name='boolean', debug_info=debug_info)
assert isinstance(value_token, bool)
value = value_token
literal = 'true' if value else 'false'
elif type_token == 'integer':
idl_type = factory.simple_type(name='long', debug_info=debug_info)
assert isinstance(value_token, str)
value = long(value_token, base=0)
literal = value_token
elif type_token == 'float':
idl_type = factory.simple_type(
name='double', debug_info=debug_info)
assert isinstance(value_token, str)
value = float(value_token)
literal = value_token
elif type_token == 'DOMString':
idl_type = factory.simple_type(
name='DOMString', debug_info=debug_info)
assert isinstance(value_token, str)
value = value_token
literal = '"{}"'.format(value)
elif type_token == 'sequence':
idl_type = factory.sequence_type(
element_type=factory.simple_type(
name='any', debug_info=debug_info),
debug_info=debug_info)
assert value_token == '[]'
value = []
literal = '[]'
elif type_token == 'dictionary':
idl_type = factory.simple_type(
name='object', debug_info=debug_info)
assert value_token == '{}'
value = object()
literal = '{}'
else:
assert False, 'Unknown literal type: {}'.format(type_token)
return LiteralConstant(idl_type=idl_type, value=value, literal=literal)
def _build_maplike(self, node): def _build_maplike(self, node):
assert node.GetClass() == 'Maplike' assert node.GetClass() == 'Maplike'
types = map(self._build_type, node.GetChildren()) types = map(self._build_type, node.GetChildren())
......
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from .idl_type import IdlType
class LiteralConstant(object):
"""
Represents a literal constant.
Literal constants are used as:
- constant values of IDL constants
- default values of IDL operation's arguments
- default values of IDL dictionary members
"""
def __init__(self, idl_type=None, value=None, literal=None):
assert isinstance(idl_type, IdlType)
assert isinstance(literal, str)
self._idl_type = idl_type
self._value = value
self._literal = literal
@property
def idl_type(self):
"""
Returns the type of the literal constant.
The types of the following literals are represented as follows.
- null: any?
- []: sequence<any>
- {}: object
- true / false: boolean
- INTEGER_NUMERICS: long
- FLOATING_POINTS: double
- STRING: DOMString
"""
return self._idl_type
@property
def value(self):
"""
Returns the value as a Python value.
The values of the following literals are represented as follows.
- null: None
- []: list()
- {}: object()
- true / false: True / False
- INTEGER_NUMERICS: an instance of long
- FLOATING_POINTS: an instance of float
- STRING: an instance of str
"""
return self._value
@property
def literal(self):
"""Returns the literal representation."""
return self._literal
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import exceptions
class DefaultValue(object):
def idl_type(self):
"""
Returns either of string, number, boolean(true/false), null, and sequence[].
@return IdlType
"""
raise exceptions.NotImplementedError()
def value(self):
"""
Returns the default value. Actual type depends on the value itself.
@return object(TBD)
"""
raise exceptions.NotImplementedError()
class ConstantValue(object):
def idl_type(self):
"""
Returns either of string, number, boolean(true/false), null, and sequence[].
@return IdlType
"""
raise exceptions.NotImplementedError()
def value(self):
"""
Returns the default value. Actual type depends on the value itself.
@return object(TBD)
"""
raise exceptions.NotImplementedError()
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