Commit 986d0d81 authored by Yuki Shiino's avatar Yuki Shiino Committed by Commit Bot

IDL compiler: Support FunctionLike.overload_group

Adds new APIs to web_idl.FunctionLike:
  overload_group
  overload_index
Accordingly, set_overload_group is added with guards against abuse.

In order to avoid circular imports, OverloadGroup is moved into
function_like.py with only change in OverloadGroup.__init__.

Bug: 839389
Change-Id: I8cf6b8550596d939eda1acc162959012e1d26b93
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1936691Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#719396}
parent 94389d26
...@@ -38,7 +38,6 @@ web_idl/literal_constant.py ...@@ -38,7 +38,6 @@ 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
web_idl/overload_group.py
web_idl/reference.py web_idl/reference.py
web_idl/runtime_enabled_features.py web_idl/runtime_enabled_features.py
web_idl/typedef.py web_idl/typedef.py
......
...@@ -48,7 +48,6 @@ web_idl/literal_constant.py ...@@ -48,7 +48,6 @@ 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
web_idl/overload_group.py
web_idl/reference.py web_idl/reference.py
web_idl/runtime_enabled_features.py web_idl/runtime_enabled_features.py
web_idl/typedef.py web_idl/typedef.py
......
...@@ -70,7 +70,6 @@ web_idl/literal_constant.py ...@@ -70,7 +70,6 @@ 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
web_idl/overload_group.py
web_idl/reference.py web_idl/reference.py
web_idl/runtime_enabled_features.py web_idl/runtime_enabled_features.py
web_idl/typedef.py web_idl/typedef.py
......
...@@ -44,12 +44,12 @@ from .dictionary import DictionaryMember ...@@ -44,12 +44,12 @@ from .dictionary import DictionaryMember
from .enumeration import Enumeration from .enumeration import Enumeration
from .exposure import Exposure from .exposure import Exposure
from .function_like import FunctionLike from .function_like import FunctionLike
from .function_like import OverloadGroup
from .idl_type import IdlType from .idl_type import IdlType
from .interface import Interface from .interface import Interface
from .namespace import Namespace from .namespace import Namespace
from .operation import Operation from .operation import Operation
from .operation import OperationGroup from .operation import OperationGroup
from .overload_group import OverloadGroup
from .runtime_enabled_features import RuntimeEnabledFeatures from .runtime_enabled_features import RuntimeEnabledFeatures
from .typedef import Typedef from .typedef import Typedef
from .union import Union from .union import Union
......
...@@ -12,8 +12,8 @@ from .composition_parts import WithExtendedAttributes ...@@ -12,8 +12,8 @@ from .composition_parts import WithExtendedAttributes
from .composition_parts import WithOwner from .composition_parts import WithOwner
from .exposure import Exposure from .exposure import Exposure
from .function_like import FunctionLike from .function_like import FunctionLike
from .function_like import OverloadGroup
from .make_copy import make_copy from .make_copy import make_copy
from .overload_group import OverloadGroup
class Constructor(FunctionLike, WithExtendedAttributes, WithCodeGeneratorInfo, class Constructor(FunctionLike, WithExtendedAttributes, WithCodeGeneratorInfo,
......
...@@ -25,11 +25,28 @@ class FunctionLike(WithIdentifier): ...@@ -25,11 +25,28 @@ class FunctionLike(WithIdentifier):
assert isinstance(ir, FunctionLike.IR) assert isinstance(ir, FunctionLike.IR)
WithIdentifier.__init__(self, ir.identifier) WithIdentifier.__init__(self, ir.identifier)
self._overload_group = None
self._arguments = tuple( self._arguments = tuple(
[Argument(arg_ir, self) for arg_ir in ir.arguments]) [Argument(arg_ir, self) for arg_ir in ir.arguments])
self._return_type = ir.return_type self._return_type = ir.return_type
self._is_static = ir.is_static self._is_static = ir.is_static
@property
def overload_group(self):
"""Returns the OverloadGroup that this function belongs to."""
return self._overload_group
def set_overload_group(self, overload_group):
assert isinstance(overload_group, OverloadGroup)
assert self._overload_group is None
assert self in overload_group
self._overload_group = overload_group
@property
def overload_index(self):
"""Returns the index in the OverloadGroup."""
return self.overload_group.index(self)
@property @property
def arguments(self): def arguments(self):
"""Returns a list of arguments.""" """Returns a list of arguments."""
...@@ -56,3 +73,250 @@ class FunctionLike(WithIdentifier): ...@@ -56,3 +73,250 @@ class FunctionLike(WithIdentifier):
return len( return len(
filter(lambda arg: not (arg.is_optional or arg.is_variadic), filter(lambda arg: not (arg.is_optional or arg.is_variadic),
self.arguments)) self.arguments))
class OverloadGroup(WithIdentifier):
class IR(WithIdentifier):
def __init__(self, functions):
assert isinstance(functions, (list, tuple))
assert all(
isinstance(function, FunctionLike.IR)
for function in functions)
assert len(set(
[function.identifier for function in functions])) == 1
assert len(set(
[function.is_static for function in functions])) == 1
WithIdentifier.__init__(self, functions[0].identifier)
self.functions = list(functions)
self.is_static = functions[0].is_static
def __iter__(self):
return iter(self.functions)
def __len__(self):
return len(self.functions)
class EffectiveOverloadItem(object):
"""
Represents an item in an effective overload set.
https://heycam.github.io/webidl/#dfn-effective-overload-set
"""
def __init__(self, function_like, type_list, opt_list):
assert len(type_list) == len(opt_list)
assert isinstance(function_like, FunctionLike)
assert isinstance(type_list, (list, tuple))
assert all(isinstance(idl_type, IdlType) for idl_type in type_list)
assert isinstance(opt_list, (list, tuple))
assert all(
isinstance(optionality, IdlType.Optionality.Type)
for optionality in opt_list)
self._function_like = function_like
self._type_list = tuple(type_list)
self._opt_list = tuple(opt_list)
@property
def function_like(self):
return self._function_like
@property
def type_list(self):
return self._type_list
@property
def opt_list(self):
return self._opt_list
def __init__(self, functions):
assert isinstance(functions, (list, tuple))
assert all(
isinstance(function, FunctionLike) for function in functions)
assert len(set([function.identifier for function in functions])) == 1
assert len(set([function.is_static for function in functions])) == 1
WithIdentifier.__init__(self, functions[0].identifier)
self._functions = tuple(functions)
for function in self._functions:
function.set_overload_group(self)
self._is_static = functions[0].is_static
def __iter__(self):
return iter(self._functions)
def __len__(self):
return len(self._functions)
def index(self, value):
return self._functions.index(value)
@property
def is_static(self):
return self._is_static
@property
def min_num_of_required_arguments(self):
"""
Returns the minimum number of required arguments of overloaded
functions.
"""
return min(map(lambda func: func.num_of_required_arguments, self))
def effective_overload_set(self, argument_count=None):
"""
Returns the effective overload set.
https://heycam.github.io/webidl/#compute-the-effective-overload-set
"""
assert argument_count is None or isinstance(argument_count,
(int, long))
N = argument_count
S = []
F = self
maxarg = max(map(lambda X: len(X.arguments), F))
if N is None:
arg_sizes = [len(X.arguments) for X in F if not X.is_variadic]
N = 1 + (max(arg_sizes) if arg_sizes else 0)
for X in F:
n = len(X.arguments)
S.append(
OverloadGroup.EffectiveOverloadItem(
X, map(lambda arg: arg.idl_type, X.arguments),
map(lambda arg: arg.optionality, X.arguments)))
if X.is_variadic:
for i in xrange(n, max(maxarg, N)):
t = map(lambda arg: arg.idl_type, X.arguments)
o = map(lambda arg: arg.optionality, X.arguments)
for _ in xrange(n, i + 1):
t.append(X.arguments[-1].idl_type)
o.append(X.arguments[-1].optionality)
S.append(OverloadGroup.EffectiveOverloadItem(X, t, o))
t = map(lambda arg: arg.idl_type, X.arguments)
o = map(lambda arg: arg.optionality, X.arguments)
for i in xrange(n - 1, -1, -1):
if X.arguments[i].optionality == IdlType.Optionality.REQUIRED:
break
S.append(OverloadGroup.EffectiveOverloadItem(X, t[:i], o[:i]))
return S
@staticmethod
def distinguishing_argument_index(items_of_effective_overload_set):
"""
Returns the distinguishing argument index.
https://heycam.github.io/webidl/#dfn-distinguishing-argument-index
"""
items = items_of_effective_overload_set
assert isinstance(items, (list, tuple))
assert all(
isinstance(item, OverloadGroup.EffectiveOverloadItem)
for item in items)
assert len(items) > 1
for index in xrange(len(items[0].type_list)):
# Assume that the given items are valid, and we only need to test
# the two types.
if OverloadGroup.are_distinguishable_types(
items[0].type_list[index], items[1].type_list[index]):
return index
assert False
@staticmethod
def are_distinguishable_types(idl_type1, idl_type2):
"""
Returns True if the two given types are distinguishable.
https://heycam.github.io/webidl/#dfn-distinguishable
"""
assert isinstance(idl_type1, IdlType)
assert isinstance(idl_type2, IdlType)
# step 1. If one type includes a nullable type and the other type either
# includes a nullable type, is a union type with flattened member
# types including a dictionary type, or is a dictionary type, ...
type1_nullable = (idl_type1.does_include_nullable_type
or idl_type1.unwrap().is_dictionary)
type2_nullable = (idl_type2.does_include_nullable_type
or idl_type2.unwrap().is_dictionary)
if type1_nullable and type2_nullable:
return False
type1 = idl_type1.unwrap()
type2 = idl_type2.unwrap()
# step 2. If both types are either a union type or nullable union type,
# ...
if type1.is_union and type2.is_union:
for member1 in type1.member_types:
for member2 in type2.member_types:
if not OverloadGroup.are_distinguishable_types(
member1, member2):
return False
return True
# step 3. If one type is a union type or nullable union type, ...
if type1.is_union or type2.is_union:
union = type1 if type1.is_union else type2
other = type2 if type1.is_union else type1
for member in union.member_types:
if not OverloadGroup.are_distinguishable_types(member, other):
return False
return True
# step 4. Consider the two "innermost" types ...
def is_interface_like(idl_type):
return idl_type.is_interface or idl_type.is_buffer_source_type
def is_dictionary_like(idl_type):
return (idl_type.is_dictionary or idl_type.is_record
or idl_type.is_callback_interface)
def is_sequence_like(idl_type):
return idl_type.is_sequence or idl_type.is_frozen_array
if not (type2.is_boolean or type2.is_numeric or type2.is_string
or type2.is_object or type2.is_symbol
or is_interface_like(type2) or type2.is_callback_function
or is_dictionary_like(type2) or is_sequence_like(type2)):
return False # Out of the table
if type1.is_boolean:
return not type2.is_boolean
if type1.is_numeric:
return not type2.is_numeric
if type1.is_string:
return not type2.is_string
if type1.is_object:
return (type2.is_boolean or type2.is_numeric or type2.is_string
or type2.is_symbol)
if type1.is_symbol:
return not type2.is_symbol
if is_interface_like(type1):
if type2.is_object:
return False
if not is_interface_like(type2):
return True
# Additional requirements: The two identified interface-like types
# are not the same, and no single platform object implements both
# interface-like types.
if type1.keyword_typename or type2.keyword_typename:
return type1.keyword_typename != type2.keyword_typename
interface1 = type1.type_definition_object
interface2 = type2.type_definition_object
return not (
interface1 in interface2.inclusive_inherited_interfaces
or interface2 in interface1.inclusive_inherited_interfaces)
if type1.is_callback_function:
return not (type2.is_object or type2.is_callback_function
or is_dictionary_like(type2))
if is_dictionary_like(type1):
return not (type2.is_object or type2.is_callback_function
or is_dictionary_like(type2))
if is_sequence_like(type1):
return not (type2.is_object or is_sequence_like(type2))
return False # Out of the table
...@@ -12,9 +12,9 @@ from .composition_parts import WithExtendedAttributes ...@@ -12,9 +12,9 @@ from .composition_parts import WithExtendedAttributes
from .composition_parts import WithOwner from .composition_parts import WithOwner
from .exposure import Exposure from .exposure import Exposure
from .function_like import FunctionLike from .function_like import FunctionLike
from .function_like import OverloadGroup
from .idl_type import IdlType from .idl_type import IdlType
from .make_copy import make_copy from .make_copy import make_copy
from .overload_group import OverloadGroup
class Operation(FunctionLike, WithExtendedAttributes, WithCodeGeneratorInfo, class Operation(FunctionLike, WithExtendedAttributes, WithCodeGeneratorInfo,
......
# 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 .composition_parts import WithIdentifier
from .function_like import FunctionLike
from .idl_type import IdlType
class OverloadGroup(WithIdentifier):
class IR(WithIdentifier):
def __init__(self, functions):
assert isinstance(functions, (list, tuple))
assert all(
isinstance(function, FunctionLike.IR)
for function in functions)
assert len(set(
[function.identifier for function in functions])) == 1
assert len(set(
[function.is_static for function in functions])) == 1
WithIdentifier.__init__(self, functions[0].identifier)
self.functions = list(functions)
self.is_static = functions[0].is_static
def __iter__(self):
return iter(self.functions)
def __len__(self):
return len(self.functions)
class EffectiveOverloadItem(object):
"""
Represents an item in an effective overload set.
https://heycam.github.io/webidl/#dfn-effective-overload-set
"""
def __init__(self, function_like, type_list, opt_list):
assert len(type_list) == len(opt_list)
assert isinstance(function_like, FunctionLike)
assert isinstance(type_list, (list, tuple))
assert all(isinstance(idl_type, IdlType) for idl_type in type_list)
assert isinstance(opt_list, (list, tuple))
assert all(
isinstance(optionality, IdlType.Optionality.Type)
for optionality in opt_list)
self._function_like = function_like
self._type_list = tuple(type_list)
self._opt_list = tuple(opt_list)
@property
def function_like(self):
return self._function_like
@property
def type_list(self):
return self._type_list
@property
def opt_list(self):
return self._opt_list
def __init__(self, functions):
assert isinstance(functions, (list, tuple))
assert all(
isinstance(function, FunctionLike) for function in functions)
assert len(set([function.identifier for function in functions])) == 1
assert len(set([function.is_static for function in functions])) == 1
WithIdentifier.__init__(self, functions[0].identifier)
self._functions = tuple(functions)
self._is_static = functions[0].is_static
def __iter__(self):
return iter(self._functions)
def __len__(self):
return len(self._functions)
def index(self, value):
return self._functions.index(value)
@property
def is_static(self):
return self._is_static
@property
def min_num_of_required_arguments(self):
"""
Returns the minimum number of required arguments of overloaded
functions.
"""
return min(map(lambda func: func.num_of_required_arguments, self))
def effective_overload_set(self, argument_count=None):
"""
Returns the effective overload set.
https://heycam.github.io/webidl/#compute-the-effective-overload-set
"""
assert argument_count is None or isinstance(argument_count,
(int, long))
N = argument_count
S = []
F = self
maxarg = max(map(lambda X: len(X.arguments), F))
if N is None:
arg_sizes = [len(X.arguments) for X in F if not X.is_variadic]
N = 1 + (max(arg_sizes) if arg_sizes else 0)
for X in F:
n = len(X.arguments)
S.append(
OverloadGroup.EffectiveOverloadItem(
X, map(lambda arg: arg.idl_type, X.arguments),
map(lambda arg: arg.optionality, X.arguments)))
if X.is_variadic:
for i in xrange(n, max(maxarg, N)):
t = map(lambda arg: arg.idl_type, X.arguments)
o = map(lambda arg: arg.optionality, X.arguments)
for _ in xrange(n, i + 1):
t.append(X.arguments[-1].idl_type)
o.append(X.arguments[-1].optionality)
S.append(OverloadGroup.EffectiveOverloadItem(X, t, o))
t = map(lambda arg: arg.idl_type, X.arguments)
o = map(lambda arg: arg.optionality, X.arguments)
for i in xrange(n - 1, -1, -1):
if X.arguments[i].optionality == IdlType.Optionality.REQUIRED:
break
S.append(OverloadGroup.EffectiveOverloadItem(X, t[:i], o[:i]))
return S
@staticmethod
def distinguishing_argument_index(items_of_effective_overload_set):
"""
Returns the distinguishing argument index.
https://heycam.github.io/webidl/#dfn-distinguishing-argument-index
"""
items = items_of_effective_overload_set
assert isinstance(items, (list, tuple))
assert all(
isinstance(item, OverloadGroup.EffectiveOverloadItem)
for item in items)
assert len(items) > 1
for index in xrange(len(items[0].type_list)):
# Assume that the given items are valid, and we only need to test
# the two types.
if OverloadGroup.are_distinguishable_types(
items[0].type_list[index], items[1].type_list[index]):
return index
assert False
@staticmethod
def are_distinguishable_types(idl_type1, idl_type2):
"""
Returns True if the two given types are distinguishable.
https://heycam.github.io/webidl/#dfn-distinguishable
"""
assert isinstance(idl_type1, IdlType)
assert isinstance(idl_type2, IdlType)
# step 1. If one type includes a nullable type and the other type either
# includes a nullable type, is a union type with flattened member
# types including a dictionary type, or is a dictionary type, ...
type1_nullable = (idl_type1.does_include_nullable_type
or idl_type1.unwrap().is_dictionary)
type2_nullable = (idl_type2.does_include_nullable_type
or idl_type2.unwrap().is_dictionary)
if type1_nullable and type2_nullable:
return False
type1 = idl_type1.unwrap()
type2 = idl_type2.unwrap()
# step 2. If both types are either a union type or nullable union type,
# ...
if type1.is_union and type2.is_union:
for member1 in type1.member_types:
for member2 in type2.member_types:
if not OverloadGroup.are_distinguishable_types(
member1, member2):
return False
return True
# step 3. If one type is a union type or nullable union type, ...
if type1.is_union or type2.is_union:
union = type1 if type1.is_union else type2
other = type2 if type1.is_union else type1
for member in union.member_types:
if not OverloadGroup.are_distinguishable_types(member, other):
return False
return True
# step 4. Consider the two "innermost" types ...
def is_interface_like(idl_type):
return idl_type.is_interface or idl_type.is_buffer_source_type
def is_dictionary_like(idl_type):
return (idl_type.is_dictionary or idl_type.is_record
or idl_type.is_callback_interface)
def is_sequence_like(idl_type):
return idl_type.is_sequence or idl_type.is_frozen_array
if not (type2.is_boolean or type2.is_numeric or type2.is_string
or type2.is_object or type2.is_symbol
or is_interface_like(type2) or type2.is_callback_function
or is_dictionary_like(type2) or is_sequence_like(type2)):
return False # Out of the table
if type1.is_boolean:
return not type2.is_boolean
if type1.is_numeric:
return not type2.is_numeric
if type1.is_string:
return not type2.is_string
if type1.is_object:
return (type2.is_boolean or type2.is_numeric or type2.is_string
or type2.is_symbol)
if type1.is_symbol:
return not type2.is_symbol
if is_interface_like(type1):
if type2.is_object:
return False
if not is_interface_like(type2):
return True
# Additional requirements: The two identified interface-like types
# are not the same, and no single platform object implements both
# interface-like types.
if type1.keyword_typename or type2.keyword_typename:
return type1.keyword_typename != type2.keyword_typename
interface1 = type1.type_definition_object
interface2 = type2.type_definition_object
return not (
interface1 in interface2.inclusive_inherited_interfaces
or interface2 in interface1.inclusive_inherited_interfaces)
if type1.is_callback_function:
return not (type2.is_object or type2.is_callback_function
or is_dictionary_like(type2))
if is_dictionary_like(type1):
return not (type2.is_object or type2.is_callback_function
or is_dictionary_like(type2))
if is_sequence_like(type1):
return not (type2.is_object or is_sequence_like(type2))
return False # Out of the table
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