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

IDL compiler: Improve IdlType.unwrap

Improves ease-of-use of IdlType.unwrap.

  idl_type.unwrap()  # Unwrap both of typedef and nullable
  idl_type.unwrap(typedef=True)  # Unwrap only typedef

This style works better when we'll add the third 'unwrappable'.

Bug: 839389
Change-Id: I2b6a9a81a57dac8228eba32c676c9a21c00284a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1896479
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712464}
parent a92bf594
...@@ -180,17 +180,39 @@ class IdlType(WithExtendedAttributes, WithDebugInfo): ...@@ -180,17 +180,39 @@ class IdlType(WithExtendedAttributes, WithDebugInfo):
""" """
callback(self) callback(self)
def unwrap(self, nullable=True, typedef=True): def unwrap(self, nullable=None, typedef=None):
""" """
Returns the body part of the actual type, i.e. returns the interesting Returns the body part of the actual type, i.e. returns the interesting
part of this type. part of this type.
Args: Args:
nullable: Unwraps a nullable type and returns |inner_type| if True. nullable:
typedef: Dereferences a typedef type and returns |original_type| if typedef:
True. All these arguments take tri-state value: True, False, or None.
True unwraps that type, False stops unwrapping that type. All
of specified arguments' values must be consistent, and mixture
of True and False is not allowed. Unspecified arguments are
automatically set to the opposite value. If no argument is
specified, unwraps all types.
""" """
return self switches = {
'nullable': nullable,
'typedef': typedef,
}
value_counts = {None: 0, False: 0, True: 0}
for value in switches.itervalues():
assert value is None or isinstance(value, bool)
value_counts[value] += 1
assert value_counts[False] == 0 or value_counts[True] == 0, (
"Specify only True or False arguments. Unspecified arguments are "
"automatically set to the opposite value.")
default = value_counts[True] == 0
for arg, value in switches.iteritems():
if value is None:
switches[arg] = default
return self._unwrap(switches)
@property @property
def does_include_nullable_type(self): def does_include_nullable_type(self):
...@@ -417,6 +439,9 @@ class IdlType(WithExtendedAttributes, WithDebugInfo): ...@@ -417,6 +439,9 @@ class IdlType(WithExtendedAttributes, WithDebugInfo):
return '{}{}'.format(type_name_inner, ''.join( return '{}{}'.format(type_name_inner, ''.join(
sorted(self.extended_attributes.keys()))) sorted(self.extended_attributes.keys())))
def _unwrap(self, switches):
return self
class SimpleType(IdlType): class SimpleType(IdlType):
""" """
...@@ -656,12 +681,6 @@ class TypedefType(IdlType, WithIdentifier): ...@@ -656,12 +681,6 @@ class TypedefType(IdlType, WithIdentifier):
callback(self) callback(self)
self.original_type.apply_to_all_composing_elements(callback) self.original_type.apply_to_all_composing_elements(callback)
def unwrap(self, nullable=True, typedef=True):
if typedef:
return self.original_type.unwrap(
nullable=nullable, typedef=typedef)
return self
@property @property
def does_include_nullable_type(self): def does_include_nullable_type(self):
return self.original_type.does_include_nullable_type return self.original_type.does_include_nullable_type
...@@ -674,6 +693,11 @@ class TypedefType(IdlType, WithIdentifier): ...@@ -674,6 +693,11 @@ class TypedefType(IdlType, WithIdentifier):
def original_type(self): def original_type(self):
return self._typedef.idl_type return self._typedef.idl_type
def _unwrap(self, switches):
if switches['typedef']:
return self.original_type._unwrap(switches)
return self
class _ArrayLikeType(IdlType): class _ArrayLikeType(IdlType):
def __init__(self, def __init__(self,
...@@ -1050,11 +1074,6 @@ class NullableType(IdlType): ...@@ -1050,11 +1074,6 @@ class NullableType(IdlType):
callback(self) callback(self)
self.inner_type.apply_to_all_composing_elements(callback) self.inner_type.apply_to_all_composing_elements(callback)
def unwrap(self, nullable=True, typedef=True):
if nullable:
return self.inner_type.unwrap(nullable=nullable, typedef=typedef)
return self
@property @property
def does_include_nullable_type(self): def does_include_nullable_type(self):
return True return True
...@@ -1066,3 +1085,8 @@ class NullableType(IdlType): ...@@ -1066,3 +1085,8 @@ class NullableType(IdlType):
@property @property
def inner_type(self): def inner_type(self):
return self._inner_type return self._inner_type
def _unwrap(self, switches):
if switches['nullable']:
return self.inner_type._unwrap(switches)
return self
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