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

IDL compiler: Implement "compute the effective overload set" of Web IDL

Bug: 839389
Change-Id: I4c6d695c91bd2d9329f3767d31c73943875b9bbb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1926273
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718068}
parent 51e0804b
......@@ -54,6 +54,11 @@ class Argument(WithIdentifier, WithOwner):
"""Returns True if this is a variadic argument."""
return self.idl_type.is_variadic
@property
def optionality(self):
"""Returns the optionality value."""
return self.idl_type.optionality
@property
def default_value(self):
"""Returns the default value or None."""
......
......@@ -45,6 +45,11 @@ class FunctionLike(WithIdentifier):
"""Returns True if this is a static function."""
return self._is_static
@property
def is_variadic(self):
"""Returns True if this function takes variadic arguments."""
return bool(self.arguments and self.arguments[-1].is_variadic)
@property
def num_of_required_arguments(self):
"""Returns the number of required arguments."""
......
......@@ -51,7 +51,8 @@ class IdlTypeFactory(object):
attrs_to_be_proxied = (
set(RefById.get_all_attributes(IdlType)).difference(
# attributes not to be proxied
set(('debug_info', 'extended_attributes', 'is_optional'))))
set(('debug_info', 'extended_attributes', 'is_optional',
'optionality'))))
self._ref_by_id_factory = RefByIdFactory(
target_attrs_with_priority=attrs_to_be_proxied)
# |_is_frozen| is initially False and you can create new instances of
......@@ -149,6 +150,12 @@ class IdlType(WithExtendedAttributes, WithDebugInfo):
like record type and promise type.
"""
class Optionality(object):
"""https://heycam.github.io/webidl/#dfn-optionality-value"""
REQUIRED = 0
OPTIONAL = 1
VARIADIC = 2
def __init__(self,
is_optional=False,
extended_attributes=None,
......@@ -398,6 +405,15 @@ class IdlType(WithExtendedAttributes, WithDebugInfo):
"""
return False
@property
def optionality(self):
"""Returns the optionality value."""
if self.is_variadic:
return IdlType.Optionality.VARIADIC
if self.is_optional:
return IdlType.Optionality.OPTIONAL
return IdlType.Optionality.REQUIRED
@property
def original_type(self):
"""Returns the typedef'ed type."""
......
......@@ -4,6 +4,7 @@
from .composition_parts import WithIdentifier
from .function_like import FunctionLike
from .idl_type import IdlType
class OverloadGroup(WithIdentifier):
......@@ -57,8 +58,55 @@ class OverloadGroup(WithIdentifier):
"""
return min(map(lambda func: func.num_of_required_arguments, self))
def effective_overload_set(self, argument_count=None):
"""
Returns the effective overload set.
Returns:
List of (FunctionLike, (IdlType...), (IdlType.Optionality...)).
"""
assert argument_count is None or isinstance(argument_count,
(int, long))
# https://heycam.github.io/webidl/#compute-the-effective-overload-set
N = argument_count
S = []
F = self
maxarg = max(map(lambda X: len(X.arguments), F))
if N is None:
N = 1 + max([len(X.arguments) for X in F if not X.is_variadic])
for X in F:
n = len(X.arguments)
S.append((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((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((X, t[:i], o[:i]))
return S
@staticmethod
def are_distinguishable_types(idl_type1, idl_type2):
"""Returns True if the two given types are distinguishable."""
assert isinstance(idl_type1, IdlType)
assert isinstance(idl_type2, IdlType)
# https://heycam.github.io/webidl/#dfn-distinguishable
# 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
......
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